1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
|
/* mate-about-me.c
* Copyright (C) 2002 Diego Gonzalez
* Copyright (C) 2006 Johannes H. Jensen
*
* Written by: Diego Gonzalez <diego@pemas.net>
* Modified by: Johannes H. Jensen <joh@deworks.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* Parts of this code come from Mate-System-Tools.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* Are all of these needed? */
#include <gdk/gdkkeysyms.h>
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#ifdef __sun
#include <sys/types.h>
#include <signal.h>
#endif
#include "capplet-util.h"
#include "mate-about-me-password.h"
/* Passwd states */
typedef enum {
PASSWD_STATE_NONE, /* Passwd is not asking for anything */
PASSWD_STATE_AUTH, /* Passwd is asking for our current password */
PASSWD_STATE_NEW, /* Passwd is asking for our new password */
PASSWD_STATE_RETYPE, /* Passwd is asking for our retyped new password */
PASSWD_STATE_ERR /* Passwd reported an error but has not yet exited */
} PasswdState;
typedef struct {
GtkBuilder *ui;
/* Commonly used widgets */
GtkEntry *current_password;
GtkEntry *new_password;
GtkEntry *retyped_password;
GtkImage *dialog_image;
GtkLabel *status_label;
/* We need to save message from libpwquality globaly */
gchar *ext_msg;
/* Whether we have authenticated */
gboolean authenticated;
/* Communication with the passwd program */
GPid backend_pid;
GIOChannel *backend_stdin;
GIOChannel *backend_stdout;
GIOChannel *backend_stderr;
GQueue *backend_stdin_queue; /* Write queue to backend_stdin */
/* GMainLoop IDs */
guint backend_child_watch_id; /* g_child_watch_add (PID) */
guint backend_stdout_watch_id; /* g_io_add_watch (stdout) */
guint backend_stderr_watch_id; /* g_io_add_watch (stderr) */
/* State of the passwd program */
PasswdState backend_state;
} PasswordDialog;
/* Buffer size for backend output (64 is too small to store message from libpwquality)*/
#define BUFSIZE 128
/*
* Error handling {{
*/
#define PASSDLG_ERROR (mate_about_me_password_error_quark())
static GQuark mate_about_me_password_error_quark(void)
{
static GQuark q = 0;
if (q == 0) {
q = g_quark_from_static_string("mate_about_me_password_error");
}
return q;
}
/* error codes */
enum {
PASSDLG_ERROR_NONE,
PASSDLG_ERROR_NEW_PASSWORD_EMPTY,
PASSDLG_ERROR_RETYPED_PASSWORD_EMPTY,
PASSDLG_ERROR_PASSWORDS_NOT_EQUAL,
PASSDLG_ERROR_BACKEND, /* Backend error */
PASSDLG_ERROR_USER, /* Generic user error */
PASSDLG_ERROR_FAILED /* Fatal failure, error->message should explain */
};
/*
* }} Error handling
*/
/*
* Prototypes {{
*/
static void
stop_passwd (PasswordDialog *pdialog);
static void
free_passwd_resources (PasswordDialog *pdialog);
static gboolean
io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswordDialog *pdialog);
static void
passdlg_set_auth_state (PasswordDialog *pdialog, gboolean state);
static void
passdlg_set_status (PasswordDialog *pdialog, gchar *msg);
static void
passdlg_set_busy (PasswordDialog *pdialog, gboolean busy);
static void
passdlg_clear (PasswordDialog *pdialog);
static guint
passdlg_refresh_password_state (PasswordDialog *pdialog);
/*
* }} Prototypes
*/
/*
* Spawning and closing of backend {{
*/
/* Child watcher */
static void
child_watch_cb (GPid pid, gint status, PasswordDialog *pdialog)
{
if (WIFEXITED (status)) {
if (WEXITSTATUS (status) >= 255) {
g_warning (_("Child exited unexpectedly"));
}
}
free_passwd_resources (pdialog);
}
/* Spawn passwd backend
* Returns: TRUE on success, FALSE otherwise and sets error appropriately */
static gboolean
spawn_passwd (PasswordDialog *pdialog, GError **error)
{
gchar *argv[2];
gchar *envp[1];
gint my_stdin, my_stdout, my_stderr;
argv[0] = "/usr/bin/passwd"; /* Is it safe to rely on a hard-coded path? */
argv[1] = NULL;
envp[0] = NULL; /* If we pass an empty array as the environment,
* will the childs environment be empty, and the
* locales set to the C default? From the manual:
* "If envp is NULL, the child inherits its
* parent'senvironment."
* If I'm wrong here, we somehow have to set
* the locales here.
*/
if (!g_spawn_async_with_pipes (NULL, /* Working directory */
argv, /* Argument vector */
envp, /* Environment */
G_SPAWN_DO_NOT_REAP_CHILD, /* Flags */
NULL, /* Child setup */
NULL, /* Data to child setup */
&pdialog->backend_pid, /* PID */
&my_stdin, /* Stdin */
&my_stdout, /* Stdout */
&my_stderr, /* Stderr */
error)) { /* GError */
/* An error occurred */
free_passwd_resources (pdialog);
return FALSE;
}
/* Initialize ext_msg with NULL */
pdialog->ext_msg = NULL;
/* Open IO Channels */
pdialog->backend_stdin = g_io_channel_unix_new (my_stdin);
pdialog->backend_stdout = g_io_channel_unix_new (my_stdout);
pdialog->backend_stderr = g_io_channel_unix_new (my_stderr);
/* Set raw encoding */
/* Set nonblocking mode */
if (g_io_channel_set_encoding (pdialog->backend_stdin, NULL, error) != G_IO_STATUS_NORMAL ||
g_io_channel_set_encoding (pdialog->backend_stdout, NULL, error) != G_IO_STATUS_NORMAL ||
g_io_channel_set_encoding (pdialog->backend_stderr, NULL, error) != G_IO_STATUS_NORMAL ||
g_io_channel_set_flags (pdialog->backend_stdin, G_IO_FLAG_NONBLOCK, error) != G_IO_STATUS_NORMAL ||
g_io_channel_set_flags (pdialog->backend_stdout, G_IO_FLAG_NONBLOCK, error) != G_IO_STATUS_NORMAL ||
g_io_channel_set_flags (pdialog->backend_stderr, G_IO_FLAG_NONBLOCK, error) != G_IO_STATUS_NORMAL) {
/* Clean up */
stop_passwd (pdialog);
return FALSE;
}
/* Turn off buffering */
g_io_channel_set_buffered (pdialog->backend_stdin, FALSE);
g_io_channel_set_buffered (pdialog->backend_stdout, FALSE);
g_io_channel_set_buffered (pdialog->backend_stderr, FALSE);
/* Add IO Channel watcher */
pdialog->backend_stdout_watch_id = g_io_add_watch (pdialog->backend_stdout,
G_IO_IN | G_IO_PRI,
(GIOFunc) io_watch_stdout, pdialog);
pdialog->backend_stderr_watch_id = g_io_add_watch (pdialog->backend_stderr,
G_IO_IN | G_IO_PRI,
(GIOFunc) io_watch_stdout, pdialog);
/* Add child watcher */
pdialog->backend_child_watch_id = g_child_watch_add (pdialog->backend_pid, (GChildWatchFunc) child_watch_cb, pdialog);
/* Success! */
return TRUE;
}
/* Stop passwd backend */
static void
stop_passwd (PasswordDialog *pdialog)
{
/* This is the standard way of returning from the dialog with passwd.
* If we return this way we can safely kill passwd as it has completed
* its task.
*/
if (pdialog->backend_pid != -1) {
kill (pdialog->backend_pid, 9);
}
/* We must run free_passwd_resources here and not let our child
* watcher do it, since it will access invalid memory after the
* dialog has been closed and cleaned up.
*
* If we had more than a single thread we'd need to remove
* the child watch before trying to kill the child.
*/
free_passwd_resources (pdialog);
}
/* Clean up passwd resources */
static void
free_passwd_resources (PasswordDialog *pdialog)
{
GError *error = NULL;
/* Remove the child watcher */
if (pdialog->backend_child_watch_id != 0) {
g_source_remove (pdialog->backend_child_watch_id);
pdialog->backend_child_watch_id = 0;
}
/* Close IO channels (internal file descriptors are automatically closed) */
if (pdialog->backend_stdin != NULL) {
if (g_io_channel_shutdown (pdialog->backend_stdin, TRUE, &error) != G_IO_STATUS_NORMAL) {
g_warning (_("Could not shutdown backend_stdin IO channel: %s"), error->message);
g_error_free (error);
error = NULL;
}
g_io_channel_unref (pdialog->backend_stdin);
pdialog->backend_stdin = NULL;
}
if (pdialog->backend_stdout != NULL) {
if (g_io_channel_shutdown (pdialog->backend_stdout, TRUE, &error) != G_IO_STATUS_NORMAL) {
g_warning (_("Could not shutdown backend_stdout IO channel: %s"), error->message);
g_error_free (error);
error = NULL;
}
g_io_channel_unref (pdialog->backend_stdout);
pdialog->backend_stdout = NULL;
}
if (pdialog->backend_stderr != NULL) {
if (g_io_channel_shutdown (pdialog->backend_stderr, TRUE, &error) != G_IO_STATUS_NORMAL) {
g_warning (_("Could not shutdown backend_stderr IO channel: %s"), error->message);
g_error_free (error);
error = NULL;
}
g_io_channel_unref (pdialog->backend_stderr);
pdialog->backend_stderr = NULL;
}
/* Remove IO watcher */
if (pdialog->backend_stdout_watch_id != 0) {
g_source_remove (pdialog->backend_stdout_watch_id);
pdialog->backend_stdout_watch_id = 0;
}
if (pdialog->backend_stderr_watch_id != 0) {
g_source_remove (pdialog->backend_stderr_watch_id);
pdialog->backend_stderr_watch_id = 0;
}
/* Close PID */
if (pdialog->backend_pid != -1) {
g_spawn_close_pid (pdialog->backend_pid);
pdialog->backend_pid = -1;
}
/* Clear backend state */
pdialog->backend_state = PASSWD_STATE_NONE;
}
/*
* }} Spawning and closing of backend
*/
/*
* Backend communication code {{
*/
/* Write the first element of queue through channel */
static void
io_queue_pop (GQueue *queue, GIOChannel *channel)
{
gchar *buf;
gsize bytes_written;
GError *error = NULL;
buf = g_queue_pop_head (queue);
if (buf != NULL) {
if (g_io_channel_write_chars (channel, buf, -1, &bytes_written, &error) != G_IO_STATUS_NORMAL) {
g_warning ("Could not write queue element \"%s\" to channel: %s", buf, error->message);
g_error_free (error);
}
g_free (buf);
}
}
/* Goes through the argument list, checking if one of them occurs in str
* Returns: TRUE as soon as an element is found to match, FALSE otherwise */
static gboolean
is_string_complete (gchar *str, ...)
{
va_list ap;
gchar *arg;
if (strlen (str) == 0) {
return FALSE;
}
va_start (ap, str);
while ((arg = va_arg (ap, char *)) != NULL) {
if (g_strrstr (str, arg) != NULL) {
va_end (ap);
return TRUE;
}
}
va_end (ap);
return FALSE;
}
/* Authentication attempt succeeded. Update the GUI accordingly. */
static void
authenticated_user (PasswordDialog *pdialog, gboolean retry)
{
pdialog->backend_state = PASSWD_STATE_NEW;
if (pdialog->authenticated) {
/* This is a re-authentication
* It succeeded, so pop our new password from the queue */
io_queue_pop (pdialog->backend_stdin_queue, pdialog->backend_stdin);
}
/* Update UI state */
passdlg_set_auth_state (pdialog, TRUE);
if (!retry) {
passdlg_set_status (pdialog, _("Authenticated!"));
}
/* Check to see if the passwords are valid
* (They might be non-empty if the user had to re-authenticate,
* and thus we need to enable the change-password-button) */
passdlg_refresh_password_state (pdialog);
}
/*
* IO watcher for stdout, called whenever there is data to read from the backend.
* This is where most of the actual IO handling happens.
*/
static gboolean
io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswordDialog *pdialog)
{
static GString *str = NULL; /* Persistent buffer */
gchar buf[BUFSIZE]; /* Temporary buffer */
gsize bytes_read;
GError *error = NULL;
gchar *msg = NULL; /* Status error message */
gboolean reinit = FALSE;
/* Initialize buffer */
if (str == NULL) {
str = g_string_new ("");
}
if (g_io_channel_read_chars (source, buf, BUFSIZE, &bytes_read, &error) != G_IO_STATUS_NORMAL) {
if (error) {
g_warning ("IO Channel read error: %s", error->message);
g_error_free (error);
}
return TRUE;
}
str = g_string_append_len (str, buf, bytes_read);
/* In which state is the backend? */
switch (pdialog->backend_state) {
case PASSWD_STATE_AUTH:
/* Passwd is asking for our current password */
if (is_string_complete (str->str, "assword: ", "failure", "wrong", "error", NULL)) {
/* Which response did we get? */
passdlg_set_busy (pdialog, FALSE);
if (g_strrstr (str->str, "New password: ") != NULL) {
/* Authentication successful */
authenticated_user (pdialog, FALSE);
} else {
/* Authentication failed */
if (pdialog->authenticated) {
/* This is a re-auth, and it failed.
* The password must have been changed in the meantime!
* Ask the user to re-authenticate
*/
/* Update status message and auth state */
passdlg_set_status (pdialog, _("Your password has been changed since you initially authenticated! Please re-authenticate."));
} else {
passdlg_set_status (pdialog, _("That password was incorrect."));
}
/* Focus current password */
gtk_widget_grab_focus (GTK_WIDGET (pdialog->current_password));
}
reinit = TRUE;
}
break;
case PASSWD_STATE_NEW:
/* Passwd is asking for our new password */
if (is_string_complete (str->str, " new password: ", NULL)) {
/* Pop retyped password from queue and into IO channel */
io_queue_pop (pdialog->backend_stdin_queue, pdialog->backend_stdin);
reinit = TRUE;
} else if (is_string_complete (str->str, "New password: ", NULL)) {
authenticated_user(pdialog, TRUE);
reinit = TRUE;
}
/* Advance to next state */
pdialog->backend_state = PASSWD_STATE_RETYPE;
break;
case PASSWD_STATE_RETYPE:
/* Passwd is asking for our retyped new password */
if (is_string_complete (str->str, "successfully",
"short",
"longer",
"palindrome",
"dictionary",
"simpl", /* catches both simple and simplistic */
"similar",
"different",
"case",
"wrapped",
"recovered",
"recent",
"unchanged",
"match",
"1 numeric or special",
"failure",
"rotated",
"error",
"BAD PASSWORD",
NULL)) {
/* What response did we get? */
passdlg_set_busy (pdialog, FALSE);
if (g_strrstr (str->str, "successfully") != NULL) {
/* Hooray! */
passdlg_clear (pdialog);
passdlg_set_status (pdialog, _("Your password has been changed."));
} else {
/* Ohnoes! */
/* Focus new password */
gtk_widget_grab_focus (GTK_WIDGET (pdialog->new_password));
if (g_strrstr (str->str, "recovered") != NULL) {
/* What does this indicate?
* "Authentication information cannot be recovered?" from libpam? */
msg = g_strdup_printf (_("System error: %s."), str->str);
} else if (g_strrstr (str->str, "short") != NULL ||
g_strrstr (str->str, "longer") != NULL) {
msg = g_strdup (_("The password is too short."));
} else if (g_strrstr (str->str, "palindrome") != NULL ||
g_strrstr (str->str, "simpl") != NULL ||
g_strrstr (str->str, "dictionary") != NULL) {
msg = g_strdup (_("The password is too simple."));
} else if (g_strrstr (str->str, "similar") != NULL ||
g_strrstr (str->str, "different") != NULL ||
g_strrstr (str->str, "case") != NULL ||
g_strrstr (str->str, "wrapped") != NULL) {
msg = g_strdup (_("The old and new passwords are too similar."));
} else if (g_strrstr (str->str, "1 numeric or special") != NULL) {
msg = g_strdup (_("The new password must contain numeric or special character(s)."));
} else if (g_strrstr (str->str, "unchanged") != NULL ||
g_strrstr (str->str, "match") != NULL) {
msg = g_strdup (_("The old and new passwords are the same."));
} else if (g_strrstr (str->str, "recent") != NULL) {
msg = g_strdup (_("The new password has already been used recently."));
} else if (g_strrstr (str->str, "failure") != NULL) {
/* Authentication failure */
msg = g_strdup (_("Your password has been changed since you initially authenticated! Please re-authenticate."));
passdlg_set_auth_state (pdialog, FALSE);
} else if (g_strrstr (str->str, "BAD PASSWORD") != NULL) {
/* Actual error description from libpwquality is located in the first string */
msg = g_strdup("Password hasn't changed!");
pdialog->ext_msg = g_strdup(g_strsplit(str->str, "\n", 2)[0]);
stop_passwd(pdialog);
}
}
/* child_watch_cb should clean up for us now */
}
reinit = TRUE;
if (msg != NULL) {
/* An error occurred! */
passdlg_clear(pdialog);
passdlg_set_status (pdialog, msg);
/* At this point, passwd might have exited, in which case
* child_watch_cb should clean up for us and remove this watcher.
* On some error conditions though, passwd just re-prompts us
* for our new password. */
if (pdialog->ext_msg != NULL) {
passdlg_clear(pdialog);
passdlg_set_status (pdialog, g_strconcat(msg, "\n", pdialog->ext_msg, NULL));
g_free (pdialog->ext_msg);
}
pdialog->backend_state = PASSWD_STATE_ERR;
/* Clean backen_stdin_queue if error occured */
#if GLIB_CHECK_VERSION(2, 60, 0)
g_queue_clear_full (pdialog->backend_stdin_queue, g_free);
#else
/* TODO: Remove this if/when GLib compatibility gets dropped. */
g_queue_foreach (pdialog->backend_stdin_queue, (GFunc) g_free, NULL);
g_queue_clear (pdialog->backend_stdin_queue);
#endif
g_free (msg);
}
break;
case PASSWD_STATE_NONE:
/* Passwd is not asking for anything yet */
if (is_string_complete (str->str, "assword: ", NULL)) {
/* If the user does not have a password set,
* passwd will immediately ask for the new password,
* so skip the AUTH phase */
if (is_string_complete (str->str, "new", "New", NULL)) {
gchar *pw;
pdialog->backend_state = PASSWD_STATE_NEW;
passdlg_set_busy (pdialog, FALSE);
authenticated_user (pdialog, FALSE);
/* since passwd didn't ask for our old password
* in this case, simply remove it from the queue */
pw = g_queue_pop_head (pdialog->backend_stdin_queue);
g_free (pw);
} else {
pdialog->backend_state = PASSWD_STATE_AUTH;
/* Pop the IO queue, i.e. send current password */
io_queue_pop (pdialog->backend_stdin_queue, pdialog->backend_stdin);
}
reinit = TRUE;
}
break;
default:
/* Passwd has returned an error */
reinit = TRUE;
break;
}
if (reinit) {
g_string_free (str, TRUE);
str = NULL;
}
/* Continue calling us */
return TRUE;
}
/*
* }} Backend communication code
*/
/* Adds the current password to the IO queue */
static void
authenticate (PasswordDialog *pdialog)
{
gchar *s;
s = g_strdup_printf ("%s\n", gtk_entry_get_text (pdialog->current_password));
g_queue_push_tail (pdialog->backend_stdin_queue, s);
}
/* Adds the new password twice to the IO queue */
static void
update_password (PasswordDialog *pdialog)
{
gchar *s;
s = g_strdup_printf ("%s\n", gtk_entry_get_text (pdialog->new_password));
g_queue_push_tail (pdialog->backend_stdin_queue, s);
/* We need to allocate new space because io_queue_pop() g_free()s
* every element of the queue after it's done */
g_queue_push_tail (pdialog->backend_stdin_queue, g_strdup (s));
}
/* Sets dialog busy state according to busy
*
* When busy:
* Sets the cursor to busy
* Disables the interface to prevent that the user interferes
* Reverts all this when non-busy
*
* Note that this function takes into account the
* authentication state of the dialog. So setting the
* dialog to busy and then back to normal should leave
* the dialog unchanged.
*/
static void
passdlg_set_busy (PasswordDialog *pdialog, gboolean busy)
{
GtkBuilder *dialog;
GtkWidget *toplevel;
GdkCursor *cursor = NULL;
GdkDisplay *display;
dialog = pdialog->ui;
/* Set cursor */
toplevel = WID ("change-password");
display = gtk_widget_get_display (toplevel);
if (busy) {
cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
}
gdk_window_set_cursor (gtk_widget_get_window (toplevel), cursor);
gdk_display_flush (display);
if (busy) {
g_object_unref (cursor);
}
/* Disable/Enable UI */
if (pdialog->authenticated) {
/* Authenticated state */
/* Enable/disable new password section */
g_object_set (pdialog->new_password, "sensitive", !busy, NULL);
g_object_set (pdialog->retyped_password, "sensitive", !busy, NULL);
g_object_set (WID ("new-password-label"), "sensitive", !busy, NULL);
g_object_set (WID ("retyped-password-label"), "sensitive", !busy, NULL);
/* Enable/disable change password button */
g_object_set (WID ("change-password-button"), "sensitive", !busy, NULL);
} else {
/* Not-authenticated state */
/* Enable/disable auth section state */
g_object_set (pdialog->current_password, "sensitive", !busy, NULL);
g_object_set (WID ("authenticate-button"), "sensitive", !busy, NULL);
g_object_set (WID ("current-password-label"), "sensitive", !busy, NULL);
}
}
/* Launch an error dialog */
static void
passdlg_error_dialog (GtkWindow *parent, const gchar *title,
const gchar *msg, const gchar *details)
{
GtkWidget *dialog;
dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
"%s", msg);
if (title)
gtk_window_set_title (GTK_WINDOW (dialog), title);
if (details)
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", details);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
/* Set authenticated state of dialog according to state
*
* When in authenticated state:
* Disables authentication-part of interface
* Enables new-password-part of interface
* When in not-authenticated state:
* Enables authentication-part of interface
* Disables new-password-part of interface
* Disables the change-password-button
*/
static void
passdlg_set_auth_state (PasswordDialog *pdialog, gboolean state)
{
GtkBuilder *dialog;
dialog = pdialog->ui;
/* Widgets which require a not-authenticated state to be accessible */
g_object_set (pdialog->current_password, "sensitive", !state, NULL);
g_object_set (WID ("current-password-label"), "sensitive", !state, NULL);
g_object_set (WID ("authenticate-button"), "sensitive", !state, NULL);
/* Widgets which require authentication to be accessible */
g_object_set (pdialog->new_password, "sensitive", state, NULL);
g_object_set (pdialog->retyped_password, "sensitive", state, NULL);
g_object_set (WID ("new-password-label"), "sensitive", state, NULL);
g_object_set (WID ("retyped-password-label"), "sensitive", state, NULL);
if (!state) {
/* Disable change-password-button when in not-authenticated state */
g_object_set (WID ("change-password-button"), "sensitive", FALSE, NULL);
}
pdialog->authenticated = state;
if (state) {
/* Authenticated state */
/* Focus new password */
gtk_widget_grab_focus (GTK_WIDGET (pdialog->new_password));
/* Set open lock image */
gtk_image_set_from_icon_name (GTK_IMAGE (pdialog->dialog_image), "changes-allow", GTK_ICON_SIZE_DIALOG);
} else {
/* Not authenticated state */
/* Focus current password */
gtk_widget_grab_focus (GTK_WIDGET (pdialog->current_password));
/* Set closed lock image */
gtk_image_set_from_icon_name (GTK_IMAGE (pdialog->dialog_image), "changes-prevent", GTK_ICON_SIZE_DIALOG);
}
}
/* Set status field message */
static void
passdlg_set_status (PasswordDialog *pdialog, gchar *msg)
{
g_object_set (pdialog->status_label, "label", msg, NULL);
}
/* Clear dialog (except the status message) */
static void
passdlg_clear (PasswordDialog *pdialog)
{
/* Set non-authenticated state */
passdlg_set_auth_state (pdialog, FALSE);
/* Clear password entries */
gtk_entry_set_text (pdialog->current_password, "");
gtk_entry_set_text (pdialog->new_password, "");
gtk_entry_set_text (pdialog->retyped_password, "");
}
/* Start backend and handle errors
* If backend is already running, stop it
* If an error occurs, show error dialog */
static gboolean
passdlg_spawn_passwd (PasswordDialog *pdialog)
{
GError *error = NULL;
gchar *details;
/* If backend is running, stop it */
stop_passwd (pdialog);
/* Spawn backend */
if (!spawn_passwd (pdialog, &error)) {
GtkWidget *parent = GTK_WIDGET (gtk_builder_get_object (pdialog->ui, "change-password"));
/* translators: Unable to launch <program>: <error message> */
details = g_strdup_printf (_("Unable to launch %s: %s"),
"/usr/bin/passwd", error->message);
passdlg_error_dialog (GTK_WINDOW (parent),
_("Unable to launch backend"),
_("A system error has occurred"),
details);
g_free (details);
g_error_free (error);
return FALSE;
}
return TRUE;
}
/* Called when the "Authenticate" button is clicked */
static void
passdlg_authenticate (GtkButton *button, PasswordDialog *pdialog)
{
/* Set busy as this can be a long process */
passdlg_set_busy (pdialog, TRUE);
/* Update status message */
passdlg_set_status (pdialog, _("Checking password..."));
/* Spawn backend */
if (!passdlg_spawn_passwd (pdialog)) {
passdlg_set_busy (pdialog, FALSE);
return;
}
authenticate (pdialog);
/* Our IO watcher should now handle the rest */
}
/* Validate passwords
* Returns:
* PASSDLG_ERROR_NONE (0) if passwords are valid
* PASSDLG_ERROR_NEW_PASSWORD_EMPTY
* PASSDLG_ERROR_RETYPED_PASSWORD_EMPTY
* PASSDLG_ERROR_PASSWORDS_NOT_EQUAL
*/
static guint
passdlg_validate_passwords (PasswordDialog *pdialog)
{
const gchar *new_password, *retyped_password;
glong nlen, rlen;
new_password = gtk_entry_get_text (pdialog->new_password);
retyped_password = gtk_entry_get_text (pdialog->retyped_password);
nlen = g_utf8_strlen (new_password, -1);
rlen = g_utf8_strlen (retyped_password, -1);
if (nlen == 0) {
/* New password empty */
return PASSDLG_ERROR_NEW_PASSWORD_EMPTY;
}
if (rlen == 0) {
/* Retyped password empty */
return PASSDLG_ERROR_RETYPED_PASSWORD_EMPTY;
}
if (nlen != rlen || strncmp (new_password, retyped_password, nlen) != 0) {
/* Passwords not equal */
return PASSDLG_ERROR_PASSWORDS_NOT_EQUAL;
}
/* Success */
return PASSDLG_ERROR_NONE;
}
/* Refresh the valid password UI state, i.e. re-validate
* and enable/disable the Change Password button.
* Returns: Return value of passdlg_validate_passwords */
static guint
passdlg_refresh_password_state (PasswordDialog *pdialog)
{
GtkBuilder *dialog;
guint ret;
gboolean valid = FALSE;
dialog = pdialog->ui;
ret = passdlg_validate_passwords (pdialog);
if (ret == PASSDLG_ERROR_NONE) {
valid = TRUE;
}
g_object_set (WID ("change-password-button"), "sensitive", valid, NULL);
return ret;
}
/* Called whenever any of the new password fields have changed */
static void
passdlg_check_password (GtkEntry *entry, PasswordDialog *pdialog)
{
guint ret;
ret = passdlg_refresh_password_state (pdialog);
switch (ret) {
case PASSDLG_ERROR_NONE:
passdlg_set_status (pdialog, _("Click <b>Change password</b> to change your password."));
break;
case PASSDLG_ERROR_NEW_PASSWORD_EMPTY:
passdlg_set_status (pdialog, _("Please type your password in the <b>New password</b> field."));
break;
case PASSDLG_ERROR_RETYPED_PASSWORD_EMPTY:
passdlg_set_status (pdialog, _("Please type your password again in the <b>Retype new password</b> field."));
break;
case PASSDLG_ERROR_PASSWORDS_NOT_EQUAL:
passdlg_set_status (pdialog, _("The two passwords are not equal."));
break;
default:
g_warning ("Unknown passdlg_check_password error: %d", ret);
break;
}
}
/* Called when the "Change password" dialog-button is clicked
* Returns: TRUE if we want to keep the dialog running, FALSE otherwise */
static gboolean
passdlg_process_response (PasswordDialog *pdialog, gint response_id)
{
if (response_id == GTK_RESPONSE_OK) {
/* Set busy as this can be a long process */
passdlg_set_busy (pdialog, TRUE);
/* Stop passwd if an error occurred and it is still running */
if (pdialog->backend_state == PASSWD_STATE_ERR) {
/* Stop passwd, free resources */
stop_passwd (pdialog);
}
/* Check that the backend is still running, or that an error
* has occurred but it has not yet exited */
if (pdialog->backend_pid == -1) {
/* If it is not, re-run authentication */
/* Spawn backend */
if (!passdlg_spawn_passwd (pdialog)) {
return TRUE;
}
/* Add current and new passwords to queue */
authenticate (pdialog);
update_password (pdialog);
} else {
/* Only add new passwords to queue */
update_password (pdialog);
/* Pop new password through the backend */
io_queue_pop (pdialog->backend_stdin_queue, pdialog->backend_stdin);
}
/* Our IO watcher should now handle the rest */
/* Keep the dialog running */
return TRUE;
}
return FALSE;
}
/* Activates (moves focus or activates) widget w */
static void
passdlg_activate (GtkEntry *entry, GtkWidget *w)
{
if (GTK_IS_BUTTON (w)) {
gtk_widget_activate (w);
} else {
gtk_widget_grab_focus (w);
}
}
/* Initialize password dialog */
static void
passdlg_init (PasswordDialog *pdialog, GtkWindow *parent)
{
GtkBuilder *dialog;
GtkWidget *wpassdlg;
GtkAccelGroup *group;
GError *error = NULL;
/* Initialize dialog */
dialog = gtk_builder_new ();
if (gtk_builder_add_from_resource (dialog, "/org/mate/mcc/am/mate-about-me-password.ui", &error) == 0)
{
g_warning ("Could not parse UI definition: %s", error->message);
g_error_free (error);
}
pdialog->ui = dialog;
wpassdlg = WID ("change-password");
capplet_set_icon (wpassdlg, "user-info");
group = gtk_accel_group_new ();
/*
* Initialize backend
*/
/* Initialize backend_pid. -1 means the backend is not running */
pdialog->backend_pid = -1;
/* Initialize IO Channels */
pdialog->backend_stdin = NULL;
pdialog->backend_stdout = NULL;
pdialog->backend_stderr = NULL;
/* Initialize write queue */
pdialog->backend_stdin_queue = g_queue_new ();
/* Initialize watchers */
pdialog->backend_child_watch_id = 0;
pdialog->backend_stdout_watch_id = 0;
pdialog->backend_stderr_watch_id = 0;
/* Initialize backend state */
pdialog->backend_state = PASSWD_STATE_NONE;
/*
* Initialize UI
*/
/* Initialize pdialog widgets */
pdialog->current_password = GTK_ENTRY (WID ("current-password"));
pdialog->new_password = GTK_ENTRY (WID ("new-password"));
pdialog->retyped_password = GTK_ENTRY (WID ("retyped-password"));
pdialog->dialog_image = GTK_IMAGE (WID ("dialog-image"));
pdialog->status_label = GTK_LABEL (WID ("status-label"));
/* Initialize accelerators */
gtk_widget_add_accelerator (GTK_WIDGET (pdialog->current_password),
"activate", group,
GDK_KEY_Return, 0,
0);
gtk_widget_add_accelerator (GTK_WIDGET (pdialog->new_password),
"activate", group,
GDK_KEY_Return, 0,
0);
/* Activate authenticate-button when enter is pressed in current-password */
g_signal_connect (pdialog->current_password, "activate",
G_CALLBACK (passdlg_activate),
WID ("authenticate-button"));
/* Activate retyped-password when enter is pressed in new-password */
g_signal_connect (pdialog->new_password, "activate",
G_CALLBACK (passdlg_activate),
pdialog->retyped_password);
/* Clear status message */
passdlg_set_status (pdialog, "");
/* Set non-authenticated state */
passdlg_set_auth_state (pdialog, FALSE);
/* Connect signal handlers */
g_signal_connect (gtk_builder_get_object (dialog, "authenticate-button"), "clicked",
G_CALLBACK (passdlg_authenticate),
pdialog);
/* Verify new passwords on-the-fly */
g_signal_connect (gtk_builder_get_object (dialog, "new-password"), "changed",
G_CALLBACK (passdlg_check_password),
pdialog);
g_signal_connect (gtk_builder_get_object (dialog, "retyped-password"), "changed",
G_CALLBACK (passdlg_check_password),
pdialog);
/* Set misc dialog properties */
gtk_window_set_resizable (GTK_WINDOW (wpassdlg), FALSE);
gtk_window_set_transient_for (GTK_WINDOW (wpassdlg), GTK_WINDOW (parent));
}
/* Main */
void
mate_about_me_password (GtkWindow *parent)
{
PasswordDialog *pdialog;
GtkBuilder *dialog;
GtkWidget *wpassdlg;
gint result;
gboolean response;
/* Initialize dialog */
pdialog = g_new0 (PasswordDialog, 1);
passdlg_init (pdialog, parent);
dialog = pdialog->ui;
wpassdlg = WID ("change-password");
/* Go! */
gtk_widget_show_all (wpassdlg);
do {
result = gtk_dialog_run (GTK_DIALOG (wpassdlg));
response = passdlg_process_response (pdialog, result);
} while (response);
/* Clean up */
stop_passwd (pdialog);
gtk_widget_destroy (wpassdlg);
g_queue_free (pdialog->backend_stdin_queue);
g_object_unref (dialog);
g_free (pdialog);
}
|