-
Notifications
You must be signed in to change notification settings - Fork 7
/
libgksu.c
2910 lines (2521 loc) · 74.8 KB
/
libgksu.c
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
/*
* Gksu -- a library providing access to su functionality
* Copyright (C) 2004-2009 Gustavo Noronha Silva
* Portions Copyright (C) 2009 VMware, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <pty.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <errno.h>
#include <glibtop.h>
#include <glibtop/procstate.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#define SN_API_NOT_YET_FROZEN
#include <libsn/sn.h>
#include <gtk/gtk.h>
#include <locale.h>
#include <gconf/gconf-client.h>
#include <gnome-keyring.h>
#include "defines.h"
#include "../config.h"
#include "libgksu.h"
#include "../libgksuui/gksuui-dialog.h"
static void
gksu_context_launch_complete (GksuContext *context);
static void
read_line (int fd, gchar *buffer, int n);
GType
gksu_error_get_type (void)
{
static GType etype = 0;
if (etype == 0) {
static const GEnumValue values[] = {
{ GKSU_ERROR_HELPER, "GKSU_ERROR_HELPER", "helper" },
{ GKSU_ERROR_NOCOMMAND, "GKSU_ERROR_NOCOMMAND", "nocommand" },
{ GKSU_ERROR_NOPASSWORD, "GKSU_ERROR_NOPASSWORD", "nopassword" },
{ GKSU_ERROR_FORK, "GKSU_ERROR_FORK", "fork" },
{ GKSU_ERROR_EXEC, "GKSU_ERROR_EXEC", "exec" },
{ GKSU_ERROR_PIPE, "GKSU_ERROR_PIPE", "pipe" },
{ GKSU_ERROR_PIPEREAD, "GKSU_ERROR_PIPEREAD", "piperead" },
{ GKSU_ERROR_WRONGPASS, "GKSU_ERROR_WRONGPASS", "wrongpass" },
{ GKSU_ERROR_CHILDFAILED, "GKSU_ERROR_CHILDFAILED", "childfailed" },
{ GKSU_ERROR_CANCELED, "GKSU_ERROR_CANCELED", "canceled" },
{ GKSU_ERROR_WRONGAUTOPASS, "GKSU_ERROR_WRONGAUTOPASS", "wrongautopass" },
{ 0, NULL, NULL }
};
etype = g_enum_register_static ("GksuError", values);
}
return etype;
}
static pid_t
test_lock(const char* fname)
{
int FD = open(fname, 0);
if(FD < 0) {
if(errno == ENOENT) {
// File does not exist
return 0;
} else {
perror("open");
return(-1);
}
}
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl(FD, F_GETLK, &fl) < 0) {
g_critical("fcntl error");
close(FD);
return(-1);
}
close(FD);
// lock is available
if(fl.l_type == F_UNLCK)
return(0);
// file is locked by another process
return (fl.l_pid);
}
static int
get_lock(const char *File)
{
int FD = open(File,O_RDWR | O_CREAT | O_TRUNC,0640);
if (FD < 0)
{
// Read only .. cant have locking problems there.
if (errno == EROFS)
{
g_warning(_("Not using locking for read only lock file %s"),File);
return dup(0); // Need something for the caller to close
}
// Feh.. We do this to distinguish the lock vs open case..
errno = EPERM;
return -1;
}
fcntl(FD,F_SETFD, FD_CLOEXEC);
// Aquire a write lock
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl(FD,F_SETLK,&fl) == -1)
{
if (errno == ENOLCK)
{
g_warning(_("Not using locking for nfs mounted lock file %s"), File);
unlink(File);
close(FD);
return dup(0); // Need something for the caller to close
}
int Tmp = errno;
close(FD);
errno = Tmp;
return -1;
}
return FD;
}
#define GRAB_TRIES 16
#define GRAB_WAIT 250 /* milliseconds */
typedef enum
{
FAILED_GRAB_MOUSE,
FAILED_GRAB_KEYBOARD
} FailedGrabWhat;
void
report_failed_grab (FailedGrabWhat what)
{
GtkWidget *dialog;
dialog = g_object_new (GTK_TYPE_MESSAGE_DIALOG,
"message-type", GTK_MESSAGE_WARNING,
"buttons", GTK_BUTTONS_CLOSE,
NULL);
switch (what)
{
case FAILED_GRAB_MOUSE:
gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG(dialog),
_("<b><big>Could not grab your mouse.</big></b>"
"\n\n"
"A malicious client may be eavesdropping "
"on your session or you may have just clicked "
"a menu or some application just decided to get "
"focus."
"\n\n"
"Try again."));
break;
case FAILED_GRAB_KEYBOARD:
gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG(dialog),
_("<b><big>Could not grab your keyboard.</big></b>"
"\n\n"
"A malicious client may be eavesdropping "
"on your session or you may have just clicked "
"a menu or some application just decided to get "
"focus."
"\n\n"
"Try again."));
break;
}
gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_destroy (dialog);
while (gtk_events_pending ())
gtk_main_iteration ();
}
int
grab_keyboard_and_mouse (GtkWidget *dialog)
{
GdkGrabStatus status;
gint grab_tries = 0;
gint lock = -1;
pid_t pid;
gchar *fname = g_strdup (getenv ("GKSU_LOCK_FILE"));
if (fname == NULL)
fname = g_strdup_printf ("%s/.gksu.lock", getenv ("HOME"));
pid = test_lock (fname);
if (pid != 0)
{
g_warning ("Lock taken by pid: %i. Exiting.", pid);
exit (0);
}
lock = get_lock(fname);
if( lock < 0)
g_warning ("Unable to create lock file.");
g_free (fname);
gdk_threads_enter ();
gtk_widget_show_all (dialog);
/* reset cursor */
gdk_window_set_cursor(dialog->window, gdk_cursor_new(GDK_LEFT_PTR));
for(;;)
{
status = gdk_pointer_grab ((GTK_WIDGET(dialog))->window, TRUE, 0, NULL,
NULL, GDK_CURRENT_TIME);
if (status == GDK_GRAB_SUCCESS)
break;
usleep (GRAB_WAIT * 1000);
if (++grab_tries > GRAB_TRIES)
{
gtk_widget_hide (dialog);
report_failed_grab (FAILED_GRAB_MOUSE);
exit (1);
break;
}
}
for(;;)
{
status = gdk_keyboard_grab ((GTK_WIDGET(dialog))->window,
FALSE, GDK_CURRENT_TIME);
if (status == GDK_GRAB_SUCCESS)
break;
usleep(GRAB_WAIT * 1000);
if (++grab_tries > GRAB_TRIES)
{
gtk_widget_hide (dialog);
report_failed_grab (FAILED_GRAB_KEYBOARD);
exit (1);
break;
}
}
/* we "raise" the window because there is a race here for
* focus-follow-mouse and auto-raise WMs that may put the window
* in the background and confuse users
*/
gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
while (gtk_events_pending ())
gtk_main_iteration ();
return lock;
}
void
ungrab_keyboard_and_mouse (int lock)
{
/* Ungrab */
gdk_pointer_ungrab(GDK_CURRENT_TIME);
gdk_keyboard_ungrab(GDK_CURRENT_TIME);
gdk_flush();
gdk_threads_leave();
close(lock);
}
static gchar*
get_gnome_keyring_password (GksuContext *context)
{
GnomeKeyringAttributeList *attributes;
GnomeKeyringAttribute attribute;
GnomeKeyringResult result;
GList *list;
attributes = gnome_keyring_attribute_list_new ();
attribute.name = g_strdup ("user");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup (gksu_context_get_user (context));
g_array_append_val (attributes, attribute);
attribute.name = g_strdup ("type");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup ("local");
g_array_append_val (attributes, attribute);
attribute.name = g_strdup ("creator");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup ("gksu");
g_array_append_val (attributes, attribute);
list = g_list_alloc();
result = gnome_keyring_find_items_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET,
attributes,
&list);
gnome_keyring_attribute_list_free (attributes);
if (
(result == GNOME_KEYRING_RESULT_OK) &&
(g_list_length(list) >= 1)
)
{
GnomeKeyringFound *found = list->data;
gint password_length = strlen (found->secret);
gchar *password;
password = g_locale_from_utf8 (found->secret,
password_length,
NULL, NULL, NULL);
password_length = strlen (password);
if (password[password_length-1] == '\n')
password[password_length-1] = '\0';
return password;
}
return NULL;
}
static void
keyring_create_item_cb (GnomeKeyringResult result,
guint32 id, gpointer keyring_loop)
{
g_main_loop_quit (keyring_loop);
}
static void
set_gnome_keyring_password (GksuContext *context, gchar *password)
{
GConfClient *gconf_client;
gboolean save_to_keyring;
gconf_client = context->gconf_client;
save_to_keyring = gconf_client_get_bool (gconf_client, BASE_PATH"save-to-keyring", NULL);
if (password && save_to_keyring)
{
static GMainLoop *keyring_loop = NULL;
GnomeKeyringAttributeList *attributes;
GnomeKeyringAttribute attribute;
GnomeKeyringResult result;
gchar *keyring_name;
gchar *key_name;
attributes = gnome_keyring_attribute_list_new ();
attribute.name = g_strdup ("user");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup (gksu_context_get_user (context));
g_array_append_val (attributes, attribute);
attribute.name = g_strdup ("type");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup ("local");
g_array_append_val (attributes, attribute);
attribute.name = g_strdup ("creator");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup ("gksu");
g_array_append_val (attributes, attribute);
key_name = g_strdup_printf ("Local password for user %s",
gksu_context_get_user (context));
keyring_loop = g_main_loop_new (NULL, FALSE);
keyring_name = gconf_client_get_string (gconf_client, BASE_PATH"save-keyring", NULL);
if (keyring_name == NULL)
keyring_name = g_strdup ("session");
/* make sure the keyring exists; if an error occurs, use
the session keyring */
result = gnome_keyring_create_sync(keyring_name, NULL);
if ((result != GNOME_KEYRING_RESULT_OK) &&
(result != GNOME_KEYRING_RESULT_ALREADY_EXISTS))
keyring_name = g_strdup ("session");
gnome_keyring_item_create (keyring_name,
GNOME_KEYRING_ITEM_GENERIC_SECRET,
key_name,
attributes,
password,
TRUE,
keyring_create_item_cb,
keyring_loop, NULL);
gnome_keyring_attribute_list_free (attributes);
g_free (keyring_name);
g_main_loop_run (keyring_loop);
}
}
static void
unset_gnome_keyring_password (GksuContext *context)
{
GConfClient *gconf_client;
gboolean save_to_keyring;
GnomeKeyringAttributeList *attributes;
GnomeKeyringAttribute attribute;
GnomeKeyringResult result;
GList *list;
gconf_client = context->gconf_client;
save_to_keyring = gconf_client_get_bool (gconf_client, BASE_PATH"save-to-keyring", NULL);
if (save_to_keyring)
{
attributes = gnome_keyring_attribute_list_new ();
attribute.name = g_strdup ("user");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup (gksu_context_get_user (context));
g_array_append_val (attributes, attribute);
attribute.name = g_strdup ("type");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup ("local");
g_array_append_val (attributes, attribute);
attribute.name = g_strdup ("creator");
attribute.type = GNOME_KEYRING_ATTRIBUTE_TYPE_STRING;
attribute.value.string = g_strdup ("gksu");
g_array_append_val (attributes, attribute);
list = g_list_alloc();
result = gnome_keyring_find_items_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET,
attributes,
&list);
gnome_keyring_attribute_list_free (attributes);
if (
(result == GNOME_KEYRING_RESULT_OK) &&
(g_list_length(list) == 1)
)
{
GnomeKeyringFound *found = list->data;
gnome_keyring_item_delete_sync (found->keyring,
found->item_id);
}
}
}
void
get_configuration_options (GksuContext *context)
{
GConfClient *gconf_client = context->gconf_client;
gboolean force_grab;
context->grab = !gconf_client_get_bool (gconf_client, BASE_PATH "disable-grab",
NULL);
force_grab = gconf_client_get_bool (gconf_client, BASE_PATH "force-grab",
NULL);
if (force_grab)
context->grab = TRUE;
context->sudo_mode = gconf_client_get_bool (gconf_client, BASE_PATH "sudo-mode",
NULL);
// UBUNTU specific - if the sudo mode is not set, check if we actualy have
// a registered schema for it, upgrades sometimes blew it away (lp: #59079)
if(context->sudo_mode == FALSE) {
GConfValue* v = gconf_client_get_default_from_schema(gconf_client, BASE_PATH "sudo-mode", NULL);
if(!v)
context->sudo_mode = TRUE;
gconf_value_free(v);
}
}
/**
* su_ask_password:
* @context: a #GksuContext
* @prompt: the prompt that should be used instead of "Password: "
* @data: data that is passed by gksu_*_full
* @error: a pointer to pointer #GError that will be filled with
* data if an error happens.
*
* This is a convenience function to create a #GksuuiDialog and
* request the password.
*
* Returns: a newly allocated gchar containing the password
* or NULL if an error happens or the user cancels the action
*/
static gchar*
su_ask_password (GksuContext *context, gchar *prompt,
gpointer data, GError **error)
{
GtkWidget *dialog = NULL;
gchar *msg;
gchar *password = NULL, *tmp = NULL;
int retvalue = 0;
int lock = 0;
GQuark gksu_quark;
gksu_quark = g_quark_from_string (PACKAGE);
if (context->grab)
dialog = g_object_new (GKSUUI_TYPE_DIALOG,
"type", GTK_WINDOW_POPUP,
"sudo-mode", context->sudo_mode,
NULL);
else
dialog = gksuui_dialog_new (context->sudo_mode);
if (prompt)
gksuui_dialog_set_prompt (GKSUUI_DIALOG(dialog), _(prompt));
if (context->message)
gksuui_dialog_set_message (GKSUUI_DIALOG(dialog), context->message);
else
{
gchar *command = NULL;
if (context->description)
command = context->description;
else
command = context->command;
if (context->sudo_mode)
{
if (!strcmp(context->user, "root"))
msg = g_strdup_printf (_("<b><big>Enter your password to perform"
" administrative tasks</big></b>\n\n"
"The application '%s' lets you "
"modify essential parts of your "
"system."),
g_markup_escape_text(command, -1));
else
msg = g_strdup_printf (_("<b><big>Enter your password to run "
"the application '%s' as user %s"
"</big></b>"),
g_markup_escape_text(command, -1), context->user);
}
else
{
if (strcmp(gksu_context_get_user (context), "root") == 0)
msg = g_strdup_printf (_("<b><big>Enter the administrative password"
"</big></b>\n\n"
"The application '%s' lets you "
"modify essential parts of your "
"system."),
command);
else
msg = g_strdup_printf (_("<b><big>Enter the password of %s to run "
"the application '%s'"
"</big></b>"),
context->user, command);
}
gksuui_dialog_set_message (GKSUUI_DIALOG(dialog), msg);
g_free (msg);
}
if (context->alert)
gksuui_dialog_set_alert (GKSUUI_DIALOG(dialog), context->alert);
if (context->grab)
lock = grab_keyboard_and_mouse (dialog);
retvalue = gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_hide (dialog);
if (context->grab)
ungrab_keyboard_and_mouse (lock);
while (gtk_events_pending ())
gtk_main_iteration ();
if (retvalue != GTK_RESPONSE_OK)
{
switch (retvalue)
{
case GTK_RESPONSE_CANCEL:
case GTK_RESPONSE_DELETE_EVENT:
g_set_error (error, gksu_quark,
GKSU_ERROR_CANCELED,
_("Password prompt canceled."));
if (context->sn_context)
gksu_context_launch_complete (context);
}
gtk_widget_destroy (dialog);
while (gtk_events_pending ())
gtk_main_iteration ();
return NULL;
}
tmp = gksuui_dialog_get_password (GKSUUI_DIALOG(dialog));
password = g_locale_from_utf8 (tmp, strlen (tmp), NULL, NULL, NULL);
g_free (tmp);
gtk_widget_destroy (dialog);
while (gtk_events_pending ())
gtk_main_iteration ();
return password;
}
static void
cb_toggled_cb (GtkWidget *button, gpointer data)
{
GConfClient *gconf_client;
gchar *key;
gboolean toggled;
gchar *key_name;
g_return_if_fail (data != NULL);
key_name = (gchar*)data;
gconf_client = gconf_client_get_default ();
toggled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(button));
key = g_strdup_printf (BASE_PATH "%s", key_name);
if (!strcmp (key_name, "display-no-pass-info"))
{
/* the meaning of the key is the exact opposite of the meaning
of the answer - when the check button is checked the key must
be off
*/
gconf_client_set_bool (gconf_client, key, !toggled, NULL);
}
else
gconf_client_set_bool (gconf_client, key, toggled, NULL);
g_object_unref (gconf_client);
g_free (key);
}
void
no_pass (GksuContext *context, gpointer data)
{
GtkWidget *dialog;
GtkWidget *alignment;
GtkWidget *check_button;
gchar *command = NULL;
if (context->description)
command = context->description;
else
command = context->command;
dialog = gtk_message_dialog_new_with_markup (NULL, 0,
GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
_("<b><big>Granted permissions without asking "
"for password</big></b>"
"\n\n"
"The '%s' program was started with "
"the privileges of the %s user without "
"the need to ask for a password, due to "
"your system's authentication mechanism "
"setup."
"\n\n"
"It is possible that you are being allowed "
"to run specific programs as user %s "
"without the need for a password, or that "
"the password is cached."
"\n\n"
"This is not a problem report; it's "
"simply a notification to make sure "
"you are aware of this."),
command,
context->user,
context->user);
alignment = gtk_alignment_new (0.5, 0.5, 0.6, 1);
gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), alignment, TRUE, TRUE, 2);
check_button = gtk_check_button_new_with_mnemonic (_("Do _not display this message again"));
g_signal_connect (G_OBJECT(check_button), "toggled",
G_CALLBACK(cb_toggled_cb), "display-no-pass-info");
gtk_container_add (GTK_CONTAINER(alignment), check_button);
gtk_widget_show_all (dialog);
gtk_dialog_run (GTK_DIALOG(dialog));
gtk_widget_destroy (GTK_WIDGET(dialog));
while (gtk_events_pending ())
gtk_main_iteration ();
}
static void
gksu_prompt_grab (GksuContext *context)
{
GtkWidget *d;
d = gtk_message_dialog_new_with_markup (NULL, 0, GTK_MESSAGE_QUESTION,
GTK_BUTTONS_YES_NO,
_("<b>Would you like your screen to be \"grabbed\"\n"
"while you enter the password?</b>"
"\n\n"
"This means all applications will be paused to avoid\n"
"the eavesdropping of your password by a a malicious\n"
"application while you type it."));
if (gtk_dialog_run (GTK_DIALOG(d)) == GTK_RESPONSE_NO)
context->grab = FALSE;
else
context->grab = TRUE;
gtk_widget_destroy (d);
}
static void
nullify_password (gchar *pass)
{
if (pass)
{
memset(pass, 0, strlen(pass));
g_free (pass);
}
pass = NULL;
}
static gchar *
get_process_name (pid_t pid)
{
static gboolean init;
glibtop_proc_state buf;
if (!init) {
glibtop_init();
init = TRUE;
}
glibtop_get_proc_state (&buf, pid);
return strdup(buf.cmd);
}
static gchar *
get_xauth_token (GksuContext *context, gchar *display)
{
gchar *xauth_bin = NULL;
FILE *xauth_output;
gchar *tmp = NULL;
gchar *xauth = g_new0 (gchar, 256);
/* find out where the xauth binary is located */
if (g_file_test ("/usr/bin/xauth", G_FILE_TEST_IS_EXECUTABLE))
xauth_bin = "/usr/bin/xauth";
else if (g_file_test ("/usr/X11R6/bin/xauth", G_FILE_TEST_IS_EXECUTABLE))
xauth_bin = "/usr/X11R6/bin/xauth";
else
{
fprintf (stderr,
"Failed to obtain xauth key: xauth binary not found "
"at usual locations");
return NULL;
}
/* get the authorization token */
tmp = g_strdup_printf ("%s list %s | "
"head -1 | awk '{ print $3 }'",
xauth_bin,
display);
if ((xauth_output = popen (tmp, "r")) == NULL)
{
fprintf (stderr,
"Failed to obtain xauth key: %s",
strerror(errno));
return NULL;
}
fread (xauth, sizeof(char), 255, xauth_output);
pclose (xauth_output);
g_free (tmp);
if (context->debug)
{
fprintf(stderr,
"xauth: -%s-\n"
"display: -%s-\n",
xauth, display);
}
return xauth;
}
/**
* prepare_xauth:
*
* Sets up the variables with values for the $DISPLAY
* environment variable and xauth-related stuff. Also
* creates a temporary directory to hold a .Xauthority
*
* Returns: TRUE if it suceeds, FALSE if it fails.
*/
static int
prepare_xauth (GksuContext *context)
{
gchar *display = NULL;
gchar *xauth = NULL;
display = g_strdup (getenv ("DISPLAY"));
xauth = get_xauth_token (context, display);
if (xauth == NULL)
{
g_free (display);
return FALSE;
}
/* If xauth is the empty string, then try striping the
* hostname part of the DISPLAY string for getting the
* auth token; this is needed for ssh-forwarded usage
*/
if (!strcmp ("", xauth))
{
gchar *cut_display = NULL;
g_free (xauth);
cut_display = g_strdup (g_strrstr (display, ":"));
xauth = get_xauth_token (context, cut_display);
g_free (display);
display = cut_display;
}
context->xauth = xauth;
context->display = display;
if (context->debug)
{
fprintf(stderr,
"final xauth: -%s-\n"
"final display: -%s-\n",
context->xauth, context->display);
}
return TRUE;
}
/* Write all of buf, even if write(2) is interrupted. */
static ssize_t
full_write (int d, const char *buf, size_t nbytes)
{
ssize_t r, w = 0;
/* Loop until nbytes of buf have been written. */
while (w < nbytes) {
/* Keep trying until write succeeds without interruption. */
do {
r = write(d, buf + w, nbytes - w);
} while (r < 0 && errno == EINTR);
if (r < 0) {
return -1;
}
w += r;
}
return w;
}
static gboolean
copy (const char *fn, const char *dir)
{
int in, out;
int r;
char *newfn;
char buf[BUFSIZ] = "";
newfn = g_strdup_printf("%s/.Xauthority", dir);
out = open(newfn, O_WRONLY | O_CREAT | O_EXCL, 0600);
if (out == -1)
{
if (errno == EEXIST)
fprintf (stderr,
"Impossible to create the .Xauthority file: a file "
"already exists. This might be a security issue; "
"please investigate.");
else
fprintf (stderr,
"Error copying '%s' to '%s': %s",
fn, dir, strerror(errno));
return FALSE;
}
in = open(fn, O_RDONLY);
if (in == -1)
{
fprintf (stderr,
"Error copying '%s' to '%s': %s",
fn, dir, strerror(errno));
return FALSE;
}
while ((r = read(in, buf, BUFSIZ)) > 0)
{
if (full_write(out, buf, r) == -1)
{
fprintf (stderr,
"Error copying '%s' to '%s': %s",
fn, dir, strerror(errno));
return FALSE;
}
}
if (r == -1)
{
fprintf (stderr,
"Error copying '%s' to '%s': %s",
fn, dir, strerror(errno));
return FALSE;
}
return TRUE;
}
static gboolean
sudo_prepare_xauth (GksuContext *context)
{
gchar template[] = "/tmp/" PACKAGE "-XXXXXX";
gboolean error_copying = FALSE;
gchar *xauth = NULL;
context->dir = g_strdup (mkdtemp(template));
if (!context->dir)
{
fprintf (stderr, "%s", strerror(errno));
return FALSE;
}
xauth = g_strdup(g_getenv ("XAUTHORITY"));
if (xauth == NULL)
xauth = g_strdup_printf ("%s/.Xauthority", g_get_home_dir());
error_copying = !copy (xauth, context->dir);
g_free (xauth);
if (error_copying)
return FALSE;
return TRUE;
}
static void
sudo_reset_xauth (GksuContext *context, gchar *xauth,
gchar *xauth_env)
{
/* reset the env var as it was before or clean it */
if (xauth_env)
setenv ("XAUTHORITY", xauth_env, TRUE);
else
unsetenv ("XAUTHORITY");
if (context->debug)
fprintf (stderr, "xauth: %s\nxauth_env: %s\ndir: %s\n",
xauth, xauth_env, context->dir);
unlink (xauth);
rmdir (context->dir);
g_free (xauth);
}
static void
startup_notification_initialize (GksuContext *context)