]> andersk Git - openssh.git/blob - contrib/gnome-ssh-askpass.c
- Created contrib/ subdirectory. Included helpers from Phil Hands'
[openssh.git] / contrib / gnome-ssh-askpass.c
1 /*
2    Compile with:
3
4    cc `gnome-config --cflags gnome gnomeui` \
5      gnome-ssh-askpass.c -o gnome-ssh-askpass \
6      `gnome-config --libs gnome gnomeui`
7
8 */
9
10 /*
11 **
12 ** GNOME ssh passphrase requestor
13 **
14 ** Damien Miller <djm@ibs.com.au>
15 ** 
16 ** Copyright 1999 Internet Business Solutions
17 **
18 ** Permission is hereby granted, free of charge, to any person
19 ** obtaining a copy of this software and associated documentation
20 ** files (the "Software"), to deal in the Software without
21 ** restriction, including without limitation the rights to use, copy,
22 ** modify, merge, publish, distribute, sublicense, and/or sell copies
23 ** of the Software, and to permit persons to whom the Software is
24 ** furnished to do so, subject to the following conditions:
25 **
26 ** The above copyright notice and this permission notice shall be
27 ** included in all copies or substantial portions of the Software.
28 **
29 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
30 ** KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
31 ** WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
32 ** AND NONINFRINGEMENT.  IN NO EVENT SHALL DAMIEN MILLER OR INTERNET
33 ** BUSINESS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35 ** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
36 ** OR OTHER DEALINGS IN THE SOFTWARE.
37 **
38 ** Except as contained in this notice, the name of Internet Business
39 ** Solutions shall not be used in advertising or otherwise to promote
40 ** the sale, use or other dealings in this Software without prior
41 ** written authorization from Internet Business Solutions.
42 **
43 */
44
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <gnome.h>
49 #include <X11/Xlib.h>
50 #include <gdk/gdkx.h>
51
52 int passphrase_dialog(char **passphrase_p, char *message)
53 {
54         char *passphrase;
55         int result;
56         
57         GtkWidget *dialog, *entry, *label;
58
59         dialog = gnome_dialog_new("OpenSSH", GNOME_STOCK_BUTTON_OK, 
60                                                                           GNOME_STOCK_BUTTON_CANCEL, NULL);
61
62         label = gtk_label_new(message);
63         gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox), label, FALSE, 
64                                                          FALSE, 0);
65
66         entry = gtk_entry_new();
67         gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox), entry, FALSE, 
68                                                          FALSE, 0);
69         gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
70         gtk_widget_grab_focus(entry);
71
72         /* Center window and prepare for grab */
73         gtk_object_set(GTK_OBJECT(dialog), "type", GTK_WINDOW_POPUP, NULL);
74         gnome_dialog_set_default(GNOME_DIALOG(dialog), 0);
75         gtk_window_set_position (GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
76         gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, TRUE);
77         gnome_dialog_close_hides(GNOME_DIALOG(dialog), TRUE);
78         gtk_container_set_border_width(GTK_CONTAINER(GNOME_DIALOG(dialog)->vbox), GNOME_PAD);
79         gtk_widget_show_all(dialog);
80
81         /* Grab focus */
82         XGrabServer(GDK_DISPLAY());
83         gdk_pointer_grab(dialog->window, TRUE, 0, NULL, NULL, GDK_CURRENT_TIME);
84         gdk_keyboard_grab(dialog->window, FALSE, GDK_CURRENT_TIME);
85
86         /* Make <enter> close dialog */
87         gnome_dialog_editable_enters(GNOME_DIALOG(dialog), GTK_EDITABLE(entry));
88
89         /* Run dialog */
90         result = gnome_dialog_run(GNOME_DIALOG(dialog));
91                 
92         /* Ungrab */
93         XUngrabServer(GDK_DISPLAY());
94         gdk_pointer_ungrab(GDK_CURRENT_TIME);
95         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
96         gdk_flush();
97
98         passphrase = gtk_entry_get_text(GTK_ENTRY(entry));
99
100         /* Take copy of passphrase if user selected OK */
101         if (result == 0)        
102                 *passphrase_p = strdup(passphrase);
103         else
104                 *passphrase_p = NULL;
105                 
106         /* Zero existing passphrase */
107         memset(passphrase, '\0', strlen(passphrase));
108         gtk_entry_set_text(GTK_ENTRY(entry), passphrase);
109                         
110         gnome_dialog_close(GNOME_DIALOG(dialog));
111
112         return (result == 0);
113 }
114
115 int main(int argc, char **argv)
116 {
117         char *passphrase;
118         char *message;
119         
120         gnome_init("GNOME ssh-askpass", "0.1", argc, argv);
121
122         if (argc == 2)
123                 message = argv[1];
124         else
125                 message = "Enter your OpenSSH passphrase:";
126
127         if (passphrase_dialog(&passphrase, message))
128         {
129                 puts(passphrase);
130                 memset(passphrase, '\0', strlen(passphrase));
131         }
132         
133         return 0;
134 }
This page took 0.046837 seconds and 5 git commands to generate.