]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
2 | * Copyright (c) 2000 Damien Miller. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * 1. Redistributions of source code must retain the above copyright | |
8 | * notice, this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright | |
10 | * notice, this list of conditions and the following disclaimer in the | |
11 | * documentation and/or other materials provided with the distribution. | |
12 | * | |
13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 | */ | |
24 | ||
25 | /* | |
26 | * Compile with: | |
27 | * | |
28 | * cc `gnome-config --cflags gnome gnomeui` \ | |
29 | * gnome-ssh-askpass.c -o gnome-ssh-askpass \ | |
30 | * `gnome-config --libs gnome gnomeui` | |
31 | * | |
32 | */ | |
33 | ||
34 | #include <stdlib.h> | |
35 | #include <stdio.h> | |
36 | #include <string.h> | |
37 | #include <gnome.h> | |
38 | #include <X11/Xlib.h> | |
39 | #include <gdk/gdkx.h> | |
40 | ||
41 | void | |
42 | report_failed_grab (void) | |
43 | { | |
44 | GtkWidget *err; | |
45 | ||
46 | err = gnome_message_box_new("Could not grab keyboard or mouse.\n" | |
47 | "A malicious client may be eavesdropping on your session.", | |
48 | GNOME_MESSAGE_BOX_ERROR, "EXIT", NULL); | |
49 | gtk_window_set_position(GTK_WINDOW(err), GTK_WIN_POS_CENTER); | |
50 | gtk_object_set(GTK_OBJECT(err), "type", GTK_WINDOW_POPUP, NULL); | |
51 | ||
52 | gnome_dialog_run_and_close(GNOME_DIALOG(err)); | |
53 | } | |
54 | ||
55 | void | |
56 | passphrase_dialog(char *message) | |
57 | { | |
58 | char *passphrase; | |
59 | char **messages; | |
60 | int result, i; | |
61 | ||
62 | GtkWidget *dialog, *entry, *label; | |
63 | ||
64 | dialog = gnome_dialog_new("OpenSSH", GNOME_STOCK_BUTTON_OK, | |
65 | GNOME_STOCK_BUTTON_CANCEL, NULL); | |
66 | ||
67 | messages = g_strsplit(message, "\\n", 0); | |
68 | if (messages) | |
69 | for(i = 0; messages[i]; i++) { | |
70 | label = gtk_label_new(messages[i]); | |
71 | gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox), | |
72 | label, FALSE, FALSE, 0); | |
73 | } | |
74 | ||
75 | entry = gtk_entry_new(); | |
76 | gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox), entry, FALSE, | |
77 | FALSE, 0); | |
78 | gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE); | |
79 | gtk_widget_grab_focus(entry); | |
80 | ||
81 | /* Center window and prepare for grab */ | |
82 | gtk_object_set(GTK_OBJECT(dialog), "type", GTK_WINDOW_POPUP, NULL); | |
83 | gnome_dialog_set_default(GNOME_DIALOG(dialog), 0); | |
84 | gtk_window_set_position (GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); | |
85 | gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, TRUE); | |
86 | gnome_dialog_close_hides(GNOME_DIALOG(dialog), TRUE); | |
87 | gtk_container_set_border_width(GTK_CONTAINER(GNOME_DIALOG(dialog)->vbox), | |
88 | GNOME_PAD); | |
89 | gtk_widget_show_all(dialog); | |
90 | ||
91 | /* Grab focus */ | |
92 | XGrabServer(GDK_DISPLAY()); | |
93 | if (gdk_pointer_grab(dialog->window, TRUE, 0, NULL, NULL, | |
94 | GDK_CURRENT_TIME)) | |
95 | goto nograb; | |
96 | if (gdk_keyboard_grab(dialog->window, FALSE, GDK_CURRENT_TIME)) | |
97 | goto nograbkb; | |
98 | ||
99 | /* Make <enter> close dialog */ | |
100 | gnome_dialog_editable_enters(GNOME_DIALOG(dialog), GTK_EDITABLE(entry)); | |
101 | ||
102 | /* Run dialog */ | |
103 | result = gnome_dialog_run(GNOME_DIALOG(dialog)); | |
104 | ||
105 | /* Ungrab */ | |
106 | XUngrabServer(GDK_DISPLAY()); | |
107 | gdk_pointer_ungrab(GDK_CURRENT_TIME); | |
108 | gdk_keyboard_ungrab(GDK_CURRENT_TIME); | |
109 | gdk_flush(); | |
110 | ||
111 | /* Report passphrase if user selected OK */ | |
112 | passphrase = gtk_entry_get_text(GTK_ENTRY(entry)); | |
113 | if (result == 0) | |
114 | puts(passphrase); | |
115 | ||
116 | /* Zero passphrase in memory */ | |
117 | memset(passphrase, '\0', strlen(passphrase)); | |
118 | gtk_entry_set_text(GTK_ENTRY(entry), passphrase); | |
119 | ||
120 | gnome_dialog_close(GNOME_DIALOG(dialog)); | |
121 | return; | |
122 | ||
123 | /* At least one grab failed - ungrab what we got, and report | |
124 | the failure to the user. Note that XGrabServer() cannot | |
125 | fail. */ | |
126 | nograbkb: | |
127 | gdk_pointer_ungrab(GDK_CURRENT_TIME); | |
128 | nograb: | |
129 | XUngrabServer(GDK_DISPLAY()); | |
130 | gnome_dialog_close(GNOME_DIALOG(dialog)); | |
131 | ||
132 | report_failed_grab(); | |
133 | } | |
134 | ||
135 | int | |
136 | main(int argc, char **argv) | |
137 | { | |
138 | char *message; | |
139 | ||
140 | gnome_init("GNOME ssh-askpass", "0.1", argc, argv); | |
141 | ||
142 | if (argc == 2) | |
143 | message = argv[1]; | |
144 | else | |
145 | message = "Enter your OpenSSH passphrase:"; | |
146 | ||
147 | setvbuf(stdout, 0, _IONBF, 0); | |
148 | passphrase_dialog(message); | |
149 | return 0; | |
150 | } |