]> git.ozlabs.org Git - ppp.git/blob - contrib/pppgetpass/pppgetpass.gtk.c
Nit: use _exit when exec fails and restrict values to 0-255 per POSIX.
[ppp.git] / contrib / pppgetpass / pppgetpass.gtk.c
1 #include <glib.h>
2 #include <gdk/gdk.h>
3 #include <gtk/gtkwindow.h>
4 #include <gtk/gtkmain.h>
5 #include <gtk/gtkbutton.h>
6 #include <gtk/gtkvbox.h>
7 #include <gtk/gtklabel.h>
8 #include <gtk/gtkentry.h>
9 #include <gtk/gtksignal.h>
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <syslog.h>
16
17 int outfd;
18 int err;
19
20 static void okpressed(void *widget, void *clientdata)
21 {
22   GtkWidget *answer=clientdata;
23   gchar *pass;
24   int passlen;
25   ssize_t wrote;
26   (void)widget;
27
28   pass=gtk_entry_get_text(GTK_ENTRY(answer));
29
30   passlen=strlen(pass);
31   if(!passlen)
32     return;
33
34   if((wrote=write(outfd, pass, passlen))!=passlen) {
35     if(wrote<0)
36       syslog(LOG_ERR, "write error on outpipe: %m");
37     else
38       syslog(LOG_ERR, "short write on outpipe");
39     err=1;
40   }
41   gtk_main_quit();
42 }
43
44 int main(int argc, char **argv)
45 {
46   GtkWidget *mainwindow, *vbox, *question, *answer, *ok;
47   char buf[1024];
48   gtk_init(&argc, &argv);
49
50   openlog(argv[0], LOG_PID, LOG_DAEMON);
51   if(argc!=4) {
52     syslog(LOG_WARNING, "Usage error");
53     return 1;
54   }
55   outfd=atoi(argv[3]);
56   mainwindow=gtk_window_new(GTK_WINDOW_TOPLEVEL);
57   gtk_window_set_title(GTK_WINDOW(mainwindow), "pppgetpass");
58   gtk_signal_connect(GTK_OBJECT(mainwindow), "destroy",
59                      GTK_SIGNAL_FUNC(gtk_main_quit), 0);
60
61   vbox=gtk_vbox_new(FALSE, 5);
62   gtk_container_add(GTK_CONTAINER(mainwindow), vbox);
63   gtk_widget_show(vbox);
64
65   if(argv[1][0] && argv[2][0])
66     snprintf(buf, sizeof buf, "Password for PPP client %s on server %s: ", argv[1], argv[2]);
67   else if(argv[1][0] && !argv[2][0])
68     snprintf(buf, sizeof buf, "Password for PPP client %s: ", argv[1]);
69   else if(!argv[1][0] && argv[2][0])
70     snprintf(buf, sizeof buf, "Password for PPP on server %s: ", argv[2]);
71   else
72     snprintf(buf, sizeof buf, "Enter PPP password: ");
73   question=gtk_label_new(buf);
74   gtk_box_pack_start(GTK_BOX(vbox), question, FALSE, TRUE, 0);
75   gtk_widget_show(question);
76
77   answer=gtk_entry_new();
78   gtk_entry_set_visibility(GTK_ENTRY(answer), 0);
79   gtk_box_pack_start(GTK_BOX(vbox), answer, FALSE, TRUE, 0);
80   gtk_widget_show(answer);
81
82   ok=gtk_button_new_with_label("OK");
83   gtk_box_pack_start(GTK_BOX(vbox), ok, FALSE, TRUE, 0);
84   gtk_signal_connect(GTK_OBJECT(ok), "clicked",
85                      GTK_SIGNAL_FUNC(okpressed), answer);
86   gtk_widget_show(ok);
87
88   gtk_widget_show(mainwindow);
89   gtk_main();
90
91   return err;
92 }