-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
xsu.c
819 lines (720 loc) · 25.5 KB
/
xsu.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
/* * C o p y r i g h t *(also read COPYING)* * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2002 <Philip Van Hoof> *
* *
* 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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "config.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <locale.h>
#include <signal.h>
#include <ctype.h>
#include <termios.h>
#include "xsu.h"
static int term = -1;
static FILE *fterm = NULL;
static pid_t pid = 0;
static gint
exec_su_failed (gpointer user_data)
{
#ifdef DEBUG
printf("static gint exec_su_failed (gpointer user_data)\n");
#endif
gtk_exit (EXIT_ERROR);
return FALSE;
}
typedef void SigFunc(int);
static SigFunc *Signal(int signo, SigFunc *func)
{
struct sigaction act, oact;
act.sa_handler = func;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if ( signo == SIGALRM ) {
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT;
#endif
} else {
#ifdef SA_RESTART
act.sa_flags |= SA_RESTART;
#endif
}
if ( sigaction(signo, &act, &oact) < 0 )
return SIG_ERR;
return oact.sa_handler;
}
void sighandler(int sig)
{
#ifdef DEBUG
fprintf(stderr, "Signal %d received, term = %d.\n", sig, term);
#endif
if ( sig == SIGCHLD && term > 0 ) {
int status;
pid_t pid;
pid = wait(&status);
if ( WIFEXITED(status) ) {
#ifdef DEBUG
fprintf(stderr,"Child %d returned\n", pid);
#endif
gtk_exit(WEXITSTATUS(status));
} else if ( WIFSIGNALED(status) ) {
#ifdef DEBUG
fprintf(stderr,"Xsu: subprocess %d has died from signal %d\n", pid, WTERMSIG(status));
#endif
gtk_exit(EXIT_ERROR);
}
close(term); term = -1;
while (gtk_events_pending ())
gtk_main_iteration ();
gtk_main_quit ();
}
}
/* Execute an external command on a pty without going through system() */
int exec_program(const char *prog, char *argv[], pid_t *pid)
{
int fd = -1;
pid_t child;
char slavename[PATH_MAX];
struct termios *stdin_termios = NULL;
struct winsize stdin_winsize;
int havewin = 0;
if (isatty(STDIN_FILENO))
havewin = ioctl(STDIN_FILENO, TIOCGWINSZ, &stdin_winsize) != -1;
if (isatty(STDIN_FILENO) ) {
stdin_termios = malloc(sizeof(struct termios));
if ( tcgetattr(STDIN_FILENO, stdin_termios) < 0) {
perror("tcgetattr");
free(stdin_termios);
stdin_termios = NULL;
} else {
/* stdin_termios->c_lflag |= ICANON;
stdin_termios->c_oflag &= ~ONLCR; */
stdin_termios->c_iflag &= ~(ISTRIP | IGNCR | INLCR | IXOFF | ECHO);
stdin_termios->c_iflag |= (ICRNL | IGNPAR | BRKINT | IXON);
stdin_termios->c_cflag &= ~CSIZE;
stdin_termios->c_cflag |= CREAD | CS8;
}
}
Signal(SIGCHLD, sighandler);
switch( child = pty_fork(&fd, slavename, sizeof(slavename), stdin_termios, havewin ? &stdin_winsize : NULL) ) {
case 0:
execvp(prog, argv);
fprintf(stderr, "execv(%s): %s\n", prog, strerror(errno));
_exit(1);
case -1:
perror("pty_fork");
return -1;
default:
#ifdef DEBUG
fprintf(stderr,"Started program %s, fd = %d (%s), PID = %d\n", prog, fd, slavename, child);
#endif
if (pid)
*pid = child;
break;
}
free(stdin_termios);
return fd;
}
void xsu_perform()
{
/*
Some code from gnomesu, thanks to Hongli Lai <[email protected]>
*/
guint timeout;
gchar *password= g_strdup(gtk_entry_get_text (GTK_ENTRY (gtk_password_textbox)));
const gchar *username=gtk_entry_get_text (GTK_ENTRY (gtk_user_textbox)),
*command=gtk_entry_get_text (GTK_ENTRY (gtk_command_textbox));
gchar *buffer;
char *argv[6], c, line[120];
fd_set fds;
struct timeval delay = { 0, 10*1000 }; /* 10 ms wait */
#ifdef DEBUG
printf("void xsu_perform()\n");
#endif
/* 0.2.1 *
Minor security fix. Password would remain in memory if we don't
clean the password textbox.
*/
gtk_entry_set_text(GTK_ENTRY(gtk_password_textbox),"");
gtk_widget_hide (gtk_xsu_window);
while( gtk_events_pending() ) {
gtk_main_iteration();
}
if (password == NULL)
return;
/* xsu 0.2.1 *
Option to set the DISPLAY environment
system() has been replaced with execlp()
xsu 0.2.2 execlp() has been replaced with execl()
This also fixes the problem that the zvt
widget could not get the child_died signal.
*/
if (set_display_env)
buffer = g_strdup_printf(SET_DISPL_ENV "%s", displ_host, command);
else
buffer = g_strdup_printf("%s", command);
/* xsu 0.2.2 *
Security issue noticed by Havoc
Security fix, execlp exports the PATH which is a security problem. SU_PATH contains
the full path of the su binary found by the configure script.
*/
/* buffer with " and/or ' gives some problems. */
/* buffer = g_strdup_printf("%s - %s -c %s",SU_PATH, username, buffer); */
/* su - username -c command */
argv[0] = su_command;
argv[1] = (char *) username;
argv[2] = "-c";
argv[3] = buffer;
argv[4] = NULL;
term = exec_program(su_command, argv, &pid);
/* We are not a gnome-application anymore but under control of su */
if ( term < 0 ) {
perror ("Could not exec");
gtk_exit (EXIT_ERROR);
}
timeout = gtk_timeout_add (SU_DELAY, exec_su_failed, NULL);
for (;;)
{
char buf[80], *ptr;
int cnt, ret;
/* We can not use fscanf as there might not be a space
character after the initial prompt, hence hanging */
/* fscanf(fd_term, "%80s", buf); */
ptr = buf;
cnt = 0;
do {
ret = read(term, &c, 1);
if ( c == EOF || ret == 0 || isspace((int)c) ) {
*ptr ++ = '\0';
break;
} else {
*ptr ++ = c;
}
cnt++;
if ( cnt==SU_PWD_LEN && !strncmp(buf, SU_PWD_OUT, SU_PWD_LEN) ) {
*ptr ++ = '\0';
break;
}
} while ( cnt < sizeof(buf));
#ifdef DEBUG
fprintf(stderr,"reading from pty: '%s'\n", buf);
#endif
if (strncmp (buf, SU_PWD_OUT, SU_PWD_LEN) == 0)
{
gtk_timeout_remove (timeout);
break;
}
while (gtk_events_pending ())
gtk_main_iteration ();
usleep (5);
}
#ifdef DEBUG
fprintf(stderr, "Got it!\n");
#endif
/* Discard any remaining characters on stdin */
for(;;) {
FD_ZERO(&fds);
FD_SET(term, &fds);
if ( select(term+1, &fds, NULL, NULL, &delay) ) {
if ( read(term, &c, 1) <= 0 ) {
#ifdef DEBUG
fprintf(stderr, "'%c'", c);
#endif
break;
}
} else
break;
}
fterm = fdopen(term, "w+");
setbuf(fterm, NULL);
#ifdef DEBUG
fprintf(stderr,"Sending password: '%s'\n", password);
#endif
/* 0.2.2 *
Minor security fix, clear the password from memory
*/
fprintf(fterm, "%s\n", password);
fflush(fterm);
/* Clear out all the lines of output, we may want to have an option to show those */
while ( !feof(fterm) ) {
if ( fgets(line, sizeof(line), fterm) ) {
#ifdef DEBUG
fprintf(stderr,"Read %d chars: %s", strlen(line), line);
#endif
}
}
g_free(password);
password = NULL;
}
void on_gtk_password_textbox_activate (GtkButton *button, gpointer user_data)
{
#ifdef DEBUG
printf("on_gtk_user_textbox_activate (GtkButton *button, gpointer user_data)\n");
#endif
xsu_perform();
}
void on_gtk_user_textbox_activate (GtkButton *button, gpointer user_data)
{
#ifdef DEBUG
printf("on_gtk_user_textbox_activate (GtkButton *button, gpointer user_data)\n");
#endif
gtk_widget_grab_focus (gtk_password_textbox);
}
void on_gtk_command_textbox_activate (GtkButton *button, gpointer user_data)
{
#ifdef DEBUG
printf("on_gtk_command_textbox_activate (GtkButton *button, gpointer user_data)\n");
#endif
if (GTK_WIDGET_VISIBLE(gtk_user_textbox))
gtk_widget_grab_focus (gtk_user_textbox);
else
gtk_widget_grab_focus (gtk_password_textbox);
}
void on_gtk_cancel_button_clicked (GtkButton *button, gpointer user_data)
{
#ifdef DEBUG
printf("on_gtk_cancel_button_clicked (GtkButton *button, gpointer user_data)\n");
#endif
/* return 3 to tell setup that the user willingly aborted */
gtk_exit(3);
}
void on_gtk_ok_button_clicked (GtkButton *button, gpointer user_data)
{
#ifdef DEBUG
printf("on_gtk_ok_button_clicked (GtkButton *button, gpointer user_data)\n");
#endif
xsu_perform();
}
GtkWidget* create_gtk_pixmap_d(char *xpm[])
{
GdkPixmap *pixmap;
GdkBitmap *bitmap;
pixmap = gdk_pixmap_colormap_create_from_xpm_d(NULL, gdk_colormap_get_system(), &bitmap, NULL, xpm);
return gtk_pixmap_new(pixmap, bitmap);
}
GtkWidget* create_gtk_pixmap(const char *file)
{
GdkPixmap *pixmap;
GdkBitmap *bitmap;
pixmap = gdk_pixmap_colormap_create_from_xpm(NULL, gdk_colormap_get_system(), &bitmap, NULL, file);
return gtk_pixmap_new(pixmap, bitmap);
}
GtkWidget* create_gtk_xsu_window (void)
{
tooltips = gtk_tooltips_new ();
gtk_xsu_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_name (gtk_xsu_window, "gtk_xsu_window");
gtk_object_set_data (GTK_OBJECT (gtk_xsu_window), "gtk_xsu_window", gtk_xsu_window);
if (title_in)
gtk_window_set_title (GTK_WINDOW (gtk_xsu_window), arg_title);
else
gtk_window_set_title (GTK_WINDOW (gtk_xsu_window), _("Login Prompt"));
gtk_window_set_position (GTK_WINDOW (gtk_xsu_window), GTK_WIN_POS_CENTER);
gtk_window_set_policy (GTK_WINDOW (gtk_xsu_window), FALSE, FALSE, FALSE);
gtk_window_table = gtk_table_new (6, 2, FALSE);
gtk_widget_set_name (gtk_window_table, "gtk_window_table");
gtk_widget_ref (gtk_window_table);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_window_table", gtk_window_table,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_window_table);
gtk_container_add (GTK_CONTAINER (gtk_xsu_window), gtk_window_table);
gtk_password_label = gtk_label_new (_("Username : "));
gtk_widget_set_name (gtk_password_label, "gtk_password_label");
gtk_widget_ref (gtk_password_label);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_password_label", gtk_password_label,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_password_label);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_password_label, 0, 1, 2, 3,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
gtk_misc_set_alignment (GTK_MISC (gtk_password_label), 4.91738e-07, 0);
gtk_misc_set_padding (GTK_MISC (gtk_password_label), 24, 8);
gtk_commandtxt_label = gtk_label_new (_("Command :"));
gtk_widget_set_name (gtk_commandtxt_label, "gtk_commandtxt_label");
gtk_widget_ref (gtk_commandtxt_label);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_commandtxt_label", gtk_commandtxt_label,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_commandtxt_label);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_commandtxt_label, 0, 1, 1, 2,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
gtk_misc_set_alignment (GTK_MISC (gtk_commandtxt_label), 0, 7.45058e-09);
gtk_misc_set_padding (GTK_MISC (gtk_commandtxt_label), 24, 6);
if (message_in)
gtk_text_label = gtk_label_new (_(arg_message));
else
gtk_text_label = gtk_label_new (_("This installer requires root privileges.\nPlease enter the correct password for it\nbelow and press [Return] or click OK."));
gtk_widget_set_name (gtk_text_label, "gtk_text_label");
gtk_widget_ref (gtk_text_label);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_text_label", gtk_text_label,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_text_label);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_text_label, 1, 2, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
gtk_label_set_justify (GTK_LABEL (gtk_text_label), GTK_JUSTIFY_LEFT);
gtk_misc_set_alignment (GTK_MISC (gtk_text_label), 0, 7.45058e-09);
gtk_misc_set_padding (GTK_MISC (gtk_text_label), 9, 14);
gtk_keys_pixmap = create_gtk_pixmap_d (keys_xpm);
gtk_widget_set_name (gtk_keys_pixmap, "gtk_keys_pixmap");
gtk_widget_ref (gtk_keys_pixmap);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_keys_pixmap", gtk_keys_pixmap,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_keys_pixmap);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_keys_pixmap, 0, 1, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (GTK_FILL), 0, 0);
gtk_password_textbox = gtk_entry_new ();
gtk_widget_set_name (gtk_password_textbox, "gtk_password_textbox");
gtk_widget_ref (gtk_password_textbox);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_password_textbox", gtk_password_textbox,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_password_textbox);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_password_textbox, 1, 2, 3, 4,
(GtkAttachOptions) (GTK_EXPAND),
(GtkAttachOptions) (0), 0, 0);
gtk_widget_set_usize (gtk_password_textbox, 230, -2);
gtk_tooltips_set_tip (tooltips, gtk_password_textbox, _("Type the password here"), NULL);
gtk_entry_set_visibility (GTK_ENTRY (gtk_password_textbox), FALSE);
gtk_user_textbox = gtk_entry_new ();
gtk_widget_set_name (gtk_user_textbox, "gtk_user_textbox");
gtk_widget_ref (gtk_user_textbox);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_user_textbox", gtk_user_textbox,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_user_textbox);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_user_textbox, 1, 2, 2, 3,
(GtkAttachOptions) (0),
(GtkAttachOptions) (0), 0, 0);
gtk_widget_set_usize (gtk_user_textbox, 230, -2);
gtk_tooltips_set_tip (tooltips, gtk_user_textbox, _("Type the username here"), NULL);
gtk_command_textbox = gtk_entry_new ();
gtk_widget_set_name (gtk_command_textbox, "gtk_command_textbox");
gtk_widget_ref (gtk_command_textbox);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_command_textbox", gtk_command_textbox,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_command_textbox);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_command_textbox, 1, 2, 1, 2,
(GtkAttachOptions) (GTK_EXPAND),
(GtkAttachOptions) (0), 0, 0);
gtk_widget_set_usize (gtk_command_textbox, 230, -2);
gtk_tooltips_set_tip (tooltips, gtk_command_textbox, _("Type the command here"), NULL);
/* xsu 0.2.3 *
The buttons and their size in other languages have been fixed. This
bug was filed by Robert Millan <[email protected]>
*/
gtk_hbox = gtk_hbox_new (FALSE, 0);
gtk_widget_show (gtk_hbox);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_hbox, 1, 2, 5, 6,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 7);
gtk_utilbox = gtk_hbutton_box_new();
gtk_box_pack_start(GTK_BOX(gtk_hbox), gtk_utilbox, FALSE, TRUE, 14);
gtk_button_box_set_layout(GTK_BUTTON_BOX(gtk_utilbox), GTK_BUTTONBOX_DEFAULT_STYLE);
gtk_button_box_set_spacing(GTK_BUTTON_BOX(gtk_utilbox), 8);
gtk_widget_show(gtk_utilbox);
gtk_ok_button = gtk_button_new_with_label(_("OK"));
gtk_box_pack_start(GTK_BOX(gtk_utilbox), gtk_ok_button, FALSE, FALSE, 5);
GTK_WIDGET_SET_FLAGS(gtk_ok_button, GTK_CAN_DEFAULT);
gtk_widget_grab_default(gtk_ok_button);
gtk_widget_show (gtk_ok_button);
gtk_tooltips_set_tip (tooltips, gtk_ok_button, _("Press this button when finished"), NULL);
gtk_cancel_button = gtk_button_new_with_label(_("Cancel"));
gtk_box_pack_start(GTK_BOX(gtk_utilbox), gtk_cancel_button, FALSE, FALSE, 5);
gtk_widget_show (gtk_cancel_button);
GTK_WIDGET_SET_FLAGS(gtk_cancel_button, GTK_CAN_DEFAULT);
gtk_tooltips_set_tip (tooltips, gtk_cancel_button, _("Press this button to Cancel"), NULL);
gtk_txtuser_label = gtk_label_new (_("Password :"));
gtk_widget_set_name (gtk_txtuser_label, "gtk_txtuser_label");
gtk_widget_ref (gtk_txtuser_label);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_txtuser_label", gtk_txtuser_label,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_txtuser_label);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_txtuser_label, 0, 1, 3, 4,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (0), 0, 0);
gtk_misc_set_alignment (GTK_MISC (gtk_txtuser_label), 0, 7.45058e-09);
gtk_misc_set_padding (GTK_MISC (gtk_txtuser_label), 24, 2);
gtk_separator_one = gtk_hseparator_new ();
gtk_widget_set_name (gtk_separator_one, "gtk_separator_one");
gtk_widget_ref (gtk_separator_one);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_separator_one", gtk_separator_one,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_separator_one);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_separator_one, 0, 2, 4, 5,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (GTK_FILL), 0, 8);
gtk_signal_connect (GTK_OBJECT (gtk_user_textbox), "activate",
GTK_SIGNAL_FUNC (on_gtk_user_textbox_activate), gtk_user_textbox);
gtk_signal_connect (GTK_OBJECT (gtk_password_textbox), "activate",
GTK_SIGNAL_FUNC (on_gtk_password_textbox_activate), gtk_password_textbox);
gtk_signal_connect (GTK_OBJECT (gtk_command_textbox), "activate",
GTK_SIGNAL_FUNC (on_gtk_command_textbox_activate), gtk_command_textbox);
gtk_signal_connect (GTK_OBJECT (gtk_cancel_button), "clicked",
GTK_SIGNAL_FUNC (on_gtk_cancel_button_clicked),
NULL);
gtk_signal_connect (GTK_OBJECT (gtk_ok_button), "clicked",
GTK_SIGNAL_FUNC (on_gtk_ok_button_clicked),
NULL);
gtk_object_set_data (GTK_OBJECT (gtk_xsu_window), "tooltips", tooltips);
return gtk_xsu_window;
}
int main (int argc, char *argv[])
{
GtkWidget *gtk_xsu_window;
gchar *arg_command = "",
*arg_username = "root",
*arg_iconfile = "";
gint x;
gboolean command_in=FALSE,
username_in=FALSE,
icon_in=FALSE,
dis_textboxes=FALSE,
hide_textboxes=FALSE,
endofargs=FALSE,
first=TRUE;
/* Set the locale */
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
/* Nope..no popt, maybe next time. Oh and getopt stuff is not
the same in *BSD as with GNU getopt, so I don't immedialty
feel like wanting to change this option parsing :-\.
Some problems about parsing the arguments have been filed by
Jeff Licquia <[email protected]>. I added the "--" option
which stops parsing. (Only " and \ have to be escaped of course,
this depends on your shell)
*/
for (x=0; x<argc; x++)
{
/* xsu 0.2.3 *
Argument "--" stops argument parsing and collects all arguments
after the "--" argument. It will put all these arguments in one
string and use this string in arg_command.
*/
if (!strcmp (argv[x], "--")) endofargs=TRUE;
if (endofargs) {
gchar *buffer;
buffer = g_strdup(argv[x+1]);
if (buffer) {
if (first) {
arg_command = g_strdup_printf("%s",buffer);
first=FALSE;
} else {
arg_command = g_strdup_printf("%s %s", arg_command, buffer);
}
continue;
} else {
command_in=TRUE;
break;
}
}
if ((!strcmp (argv[x], "-c")) || (!strcmp (argv[x], "--command")))
{
if (argv[x+1] != NULL)
{
arg_command = g_strdup(argv[x+1]);
command_in=TRUE;
}
}
if ((!strcmp (argv[x], "-s")) || (!strcmp (argv[x], "--su-command")))
{
if (argv[x+1] != NULL)
{
su_command = g_strdup(argv[x+1]);
}
}
if ((!strcmp (argv[x], "-u")) || (!strcmp (argv[x], "--username")))
{
if (argv[x+1] != NULL)
{
arg_username = g_strdup(argv[x+1]);
username_in=TRUE;
}
}
if ((!strcmp (argv[x], "-m")) || (!strcmp (argv[x], "--message")))
{
if (argv[x+1] != NULL)
{
int i;
arg_message = g_strdup(argv[x+1]);
for (i=0;i<strlen(arg_message);++i)
if (arg_message[i]=='^') arg_message[i]='\n';
for (i=0;i<strlen(arg_message);++i)
if (arg_message[i]=='~') arg_message[i]='\t';
message_in=TRUE;
}
}
/* xsu 0.1.5 *
Option to load another icon
*/
if ((!strcmp (argv[x], "-i")) || (!strcmp (argv[x], "--icon")))
{
if (argv[x+1] != NULL)
{
arg_iconfile = g_strdup(argv[x+1]);
icon_in=TRUE;
}
}
/* xsu 0.1.6 *
Option to set another window title
*/
if ((!strcmp (argv[x], "-t")) || (!strcmp (argv[x], "--title")))
{
if (argv[x+1] != NULL)
{
arg_title = g_strdup(argv[x+1]);
title_in=TRUE;
}
}
/* xsu 0.1.4 *
Option to disable the Command and User entries.
*/
if ((!strcmp (argv[x], "-d")) || (!strcmp (argv[x], "--unadaptable")))
{
dis_textboxes=TRUE;
}
if ((!strcmp (argv[x], "-e")) || (!strcmp (argv[x], "--hide")))
{
hide_textboxes=TRUE;
}
/* xsu 0.2.1 *
Option to set the DISPLAY environment
*/
if ((!strcmp (argv[x], "-a")) || (!strcmp (argv[x], "--set-display")))
{
const char *env = getenv("DISPLAY");
set_display_env=TRUE;
if ((argv[x+1] != NULL) && ((argv[x+1])[0] != '-'))
{ /* if there is a 2e argument and it does not begin with a dash */
displ_host = g_strdup(argv[x+1]);
} else if (env) {
displ_host = g_strdup(env);
} else {
displ_host = g_strdup(":0");
}
}
if ((!strcmp (argv[x], "-v")) || (!strcmp (argv[x], "--version")))
{
printf(NAME " " VERSION " by " AUTHOR "\n");
exit(EXIT_CORRECT);
}
if ((!strcmp (argv[x], "-h")) || (!strcmp (argv[x], "--help")))
{
printf(NAME " " VERSION " by " AUTHOR "\n\n");
printf("Arguments :\n\n");
printf("\t-u|--username \"USERNAME\"\n");
printf("\t-c|--command \"COMMAND\"\n");
printf("\t-s|--su-command \"SU COMMAND\"\n");
printf("\t-m|--message \"MESSAGE\"\n");
printf("\t-i|--icon \"FILENAME\"\n");
printf("\t-t|--title \"WINDOW TITLE\"\n");
printf("\t-a|--set-display [\"HOSTNAME\"]\n");
printf("\t-d|--unadaptable\n");
printf("\t-e|--hide\n");
printf("\t-h|--help\n\n");
printf("ps. for \"message\" :\n");
printf("\t* You can use ^ if you want a newline in the message string.\n");
printf("\t* You can use ~ if you want a tab in the message string.\n\n");
exit(EXIT_CORRECT);
}
}
/* xsu 0.1.0 *
This one is to confuse gnome_init() actually. I don't want
gnome to use the program arguments. And I am to lazy to learn
how to use popt. Well.. if you are not..modify so that it uses
popt and send me the patch :)
xsu 0.1.4 *
Option (dis_textboxes) to disable the Command and User entries.
*/
argc=1;
/* And init GTK ... */
if ( ! gtk_init_check(&argc, &argv) ) {
return 1; /* X11 not available */
}
gtk_xsu_window = create_gtk_xsu_window ();
if ( !su_command ) {
su_command = g_strdup(SU_PATH);
}
if (username_in) /* If there was a username argument */
{
gtk_entry_set_text(GTK_ENTRY(gtk_user_textbox),arg_username);
if (dis_textboxes) gtk_entry_set_editable(GTK_ENTRY(gtk_user_textbox), FALSE);
if (hide_textboxes)
{
gtk_widget_hide(gtk_user_textbox);
gtk_widget_hide(gtk_password_label);
}
} else {
gtk_widget_grab_focus (gtk_user_textbox);
}
if (command_in) /* If there was a command argument */
{
gtk_entry_set_text(GTK_ENTRY(gtk_command_textbox),arg_command);
if (dis_textboxes) gtk_entry_set_editable(GTK_ENTRY(gtk_command_textbox), FALSE);
if (hide_textboxes)
{
gtk_widget_hide(gtk_command_textbox);
gtk_widget_hide(gtk_commandtxt_label);
}
} else {
gtk_widget_grab_focus (gtk_command_textbox);
}
if ((command_in) && (username_in)) /* if both */
{
gtk_widget_grab_focus (gtk_password_textbox);
}
/* xsu 0.1.5 *
Option to load another icon
*/
if (icon_in) {
gtk_widget_destroy(gtk_keys_pixmap);
gtk_keys_pixmap = create_gtk_pixmap(arg_iconfile);
gtk_widget_set_name (gtk_keys_pixmap, "gtk_keys_pixmap");
gtk_widget_ref (gtk_keys_pixmap);
gtk_object_set_data_full (GTK_OBJECT (gtk_xsu_window), "gtk_keys_pixmap", gtk_keys_pixmap,
(GtkDestroyNotify) gtk_widget_unref);
gtk_widget_show (gtk_keys_pixmap);
gtk_table_attach (GTK_TABLE (gtk_window_table), gtk_keys_pixmap, 0, 1, 0, 1,
(GtkAttachOptions) (GTK_FILL),
(GtkAttachOptions) (GTK_FILL), 0, 0);
}
/* All done.. lets go gnome'ing :) */
gtk_widget_show (gtk_xsu_window);
gtk_main ();
/* Wait for child to finish */
if ( pid ) {
#ifdef DEBUG
printf("Waiting for child %d\n", pid);
#endif
waitpid(pid, NULL, 0);
}
if ( fterm ) {
fclose(fterm);
} if ( term > 0 ) {
close(term);
}
g_free(su_command);
return 0;
}