1 /*
2  * Copyright (c) 2000-2002 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 /* GTK2 support by Nalin Dahyabhai <nalin@redhat.com> */
26 
27 /*
28  * This is a simple GNOME SSH passphrase grabber. To use it, set the
29  * environment variable SSH_ASKPASS to point to the location of
30  * gnome-ssh-askpass before calling "ssh-add < /dev/null".
31  *
32  * There is only two run-time options: if you set the environment variable
33  * "GNOME_SSH_ASKPASS_GRAB_SERVER=true" then gnome-ssh-askpass will grab
34  * the X server. If you set "GNOME_SSH_ASKPASS_GRAB_POINTER=true", then the
35  * pointer will be grabbed too. These may have some benefit to security if
36  * you don't trust your X server. We grab the keyboard always.
37  */
38 
39 #define GRAB_TRIES	16
40 #define GRAB_WAIT	250 /* milliseconds */
41 
42 #define PROMPT_ENTRY	0
43 #define PROMPT_CONFIRM	1
44 #define PROMPT_NONE	2
45 
46 /*
47  * Compile with:
48  *
49  * cc -Wall `pkg-config --cflags gtk+-2.0` \
50  *    gnome-ssh-askpass2.c -o gnome-ssh-askpass \
51  *    `pkg-config --libs gtk+-2.0`
52  *
53  */
54 
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include <X11/Xlib.h>
61 #include <gtk/gtk.h>
62 #include <gdk/gdkx.h>
63 #include <gdk/gdkkeysyms.h>
64 
65 static void
ok_dialog(GtkWidget * entry,gpointer dialog)66 ok_dialog(GtkWidget *entry, gpointer dialog)
67 {
68 	g_return_if_fail(GTK_IS_DIALOG(dialog));
69 	gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
70 }
71 
72 static gboolean
check_none(GtkWidget * widget,GdkEventKey * event,gpointer dialog)73 check_none(GtkWidget *widget, GdkEventKey *event, gpointer dialog)
74 {
75 	switch (event->keyval) {
76 	case GDK_KEY_Escape:
77 		/* esc -> close dialog */
78 		gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
79 		return TRUE;
80 	case GDK_KEY_Tab:
81 		/* tab -> focus close button */
82 		gtk_widget_grab_focus(gtk_dialog_get_widget_for_response(
83 		    dialog, GTK_RESPONSE_CLOSE));
84 		return TRUE;
85 	default:
86 		/* eat all other key events */
87 		return TRUE;
88 	}
89 }
90 
91 static int
parse_env_hex_color(const char * env,GdkColor * c)92 parse_env_hex_color(const char *env, GdkColor *c)
93 {
94 	const char *s;
95 	unsigned long ul;
96 	char *ep;
97 	size_t n;
98 
99 	if ((s = getenv(env)) == NULL)
100 		return 0;
101 
102 	memset(c, 0, sizeof(*c));
103 
104 	/* Permit hex rgb or rrggbb optionally prefixed by '#' or '0x' */
105 	if (*s == '#')
106 		s++;
107 	else if (strncmp(s, "0x", 2) == 0)
108 		s += 2;
109 	n = strlen(s);
110 	if (n != 3 && n != 6)
111 		goto bad;
112 	ul = strtoul(s, &ep, 16);
113 	if (*ep != '\0' || ul > 0xffffff) {
114  bad:
115 		fprintf(stderr, "Invalid $%s - invalid hex color code\n", env);
116 		return 0;
117 	}
118 	/* Valid hex sequence; expand into a GdkColor */
119 	if (n == 3) {
120 		/* 4-bit RGB */
121 		c->red = ((ul >> 8) & 0xf) << 12;
122 		c->green = ((ul >> 4) & 0xf) << 12;
123 		c->blue = (ul & 0xf) << 12;
124 	} else {
125 		/* 8-bit RGB */
126 		c->red = ((ul >> 16) & 0xff) << 8;
127 		c->green = ((ul >> 8) & 0xff) << 8;
128 		c->blue = (ul & 0xff) << 8;
129 	}
130 	return 1;
131 }
132 
133 static int
passphrase_dialog(char * message,int prompt_type)134 passphrase_dialog(char *message, int prompt_type)
135 {
136 	const char *failed;
137 	char *passphrase, *local;
138 	int result, grab_tries, grab_server, grab_pointer;
139 	int buttons, default_response;
140 	GtkWidget *parent_window, *dialog, *entry, *err;
141 	GdkGrabStatus status;
142 	GdkColor fg, bg;
143 	GdkSeat *seat;
144 	GdkDisplay *display;
145 	GdkSeatCapabilities caps;
146 	int fg_set = 0, bg_set = 0;
147 
148 	grab_server = (getenv("GNOME_SSH_ASKPASS_GRAB_SERVER") != NULL);
149 	grab_pointer = (getenv("GNOME_SSH_ASKPASS_GRAB_POINTER") != NULL);
150 	grab_tries = 0;
151 
152 	fg_set = parse_env_hex_color("GNOME_SSH_ASKPASS_FG_COLOR", &fg);
153 	bg_set = parse_env_hex_color("GNOME_SSH_ASKPASS_BG_COLOR", &bg);
154 
155 	/* Create an invisible parent window so that GtkDialog doesn't
156 	 * complain.  */
157 	parent_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
158 
159 	switch (prompt_type) {
160 	case PROMPT_CONFIRM:
161 		buttons = GTK_BUTTONS_YES_NO;
162 		default_response = GTK_RESPONSE_YES;
163 		break;
164 	case PROMPT_NONE:
165 		buttons = GTK_BUTTONS_CLOSE;
166 		default_response = GTK_RESPONSE_CLOSE;
167 		break;
168 	default:
169 		buttons = GTK_BUTTONS_OK_CANCEL;
170 		default_response = GTK_RESPONSE_OK;
171 		break;
172 	}
173 
174 	dialog = gtk_message_dialog_new(GTK_WINDOW(parent_window), 0,
175 	    GTK_MESSAGE_QUESTION, buttons, "%s", message);
176 
177 	gtk_window_set_title(GTK_WINDOW(dialog), "OpenSSH");
178 	gtk_window_set_position (GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
179 	gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
180 	gtk_dialog_set_default_response(GTK_DIALOG(dialog), default_response);
181 	gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
182 
183 	if (fg_set)
184 		gtk_widget_modify_fg(dialog, GTK_STATE_NORMAL, &fg);
185 	if (bg_set)
186 		gtk_widget_modify_bg(dialog, GTK_STATE_NORMAL, &bg);
187 
188 	if (prompt_type == PROMPT_ENTRY || prompt_type == PROMPT_NONE) {
189 		entry = gtk_entry_new();
190 		if (fg_set)
191 			gtk_widget_modify_fg(entry, GTK_STATE_NORMAL, &fg);
192 		if (bg_set)
193 			gtk_widget_modify_bg(entry, GTK_STATE_NORMAL, &bg);
194 		gtk_box_pack_start(
195 		    GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),
196 		    entry, FALSE, FALSE, 0);
197 		gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
198 		gtk_widget_grab_focus(entry);
199 		if (prompt_type == PROMPT_ENTRY) {
200 			gtk_widget_show(entry);
201 			/* Make <enter> close dialog */
202 			g_signal_connect(G_OBJECT(entry), "activate",
203 					 G_CALLBACK(ok_dialog), dialog);
204 		} else {
205 			/*
206 			 * Ensure the 'close' button is not focused by default
207 			 * but is still reachable via tab. This is a bit of a
208 			 * hack - it uses a hidden entry that responds to a
209 			 * couple of keypress events (escape and tab only).
210 			 */
211 			gtk_widget_realize(entry);
212 			g_signal_connect(G_OBJECT(entry), "key_press_event",
213 			    G_CALLBACK(check_none), dialog);
214 		}
215 	}
216 	/* Grab focus */
217 	gtk_widget_show_now(dialog);
218 	display = gtk_widget_get_display(GTK_WIDGET(dialog));
219 	seat = gdk_display_get_default_seat(display);
220 	caps = GDK_SEAT_CAPABILITY_KEYBOARD;
221 	if (grab_pointer)
222 		caps |= GDK_SEAT_CAPABILITY_ALL_POINTING;
223 	if (grab_server)
224 		caps = GDK_SEAT_CAPABILITY_ALL;
225 	for (;;) {
226 		status = gdk_seat_grab(seat, gtk_widget_get_window(dialog),
227 		    caps, TRUE, NULL, NULL, NULL, NULL);
228 		if (status == GDK_GRAB_SUCCESS)
229 			break;
230 		usleep(GRAB_WAIT * 1000);
231 		if (++grab_tries > GRAB_TRIES)
232 			goto nograb;
233 	}
234 
235 	result = gtk_dialog_run(GTK_DIALOG(dialog));
236 
237 	/* Ungrab */
238 	gdk_seat_ungrab(seat);
239 	gdk_display_flush(display);
240 
241 	/* Report passphrase if user selected OK */
242 	if (prompt_type == PROMPT_ENTRY) {
243 		passphrase = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
244 		if (result == GTK_RESPONSE_OK) {
245 			local = g_locale_from_utf8(passphrase,
246 			    strlen(passphrase), NULL, NULL, NULL);
247 			if (local != NULL) {
248 				puts(local);
249 				memset(local, '\0', strlen(local));
250 				g_free(local);
251 			} else {
252 				puts(passphrase);
253 			}
254 		}
255 		/* Zero passphrase in memory */
256 		memset(passphrase, '\b', strlen(passphrase));
257 		gtk_entry_set_text(GTK_ENTRY(entry), passphrase);
258 		memset(passphrase, '\0', strlen(passphrase));
259 		g_free(passphrase);
260 	}
261 
262 	gtk_widget_destroy(dialog);
263 	if (result != GTK_RESPONSE_OK && result != GTK_RESPONSE_YES)
264 		return -1;
265 	return 0;
266 
267  nograb:
268 	gtk_widget_destroy(dialog);
269 	err = gtk_message_dialog_new(GTK_WINDOW(parent_window), 0,
270 	    GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
271 	    "Could not grab input. A malicious client may be eavesdropping "
272 	    "on your session.");
273 	gtk_window_set_position(GTK_WINDOW(err), GTK_WIN_POS_CENTER);
274 	gtk_dialog_run(GTK_DIALOG(err));
275 	gtk_widget_destroy(err);
276 	return -1;
277 }
278 
279 int
main(int argc,char ** argv)280 main(int argc, char **argv)
281 {
282 	char *message, *prompt_mode;
283 	int result, prompt_type = PROMPT_ENTRY;
284 
285 	gtk_init(&argc, &argv);
286 
287 	if (argc > 1) {
288 		message = g_strjoinv(" ", argv + 1);
289 	} else {
290 		message = g_strdup("Enter your OpenSSH passphrase:");
291 	}
292 
293 	if ((prompt_mode = getenv("SSH_ASKPASS_PROMPT")) != NULL) {
294 		if (strcasecmp(prompt_mode, "confirm") == 0)
295 			prompt_type = PROMPT_CONFIRM;
296 		else if (strcasecmp(prompt_mode, "none") == 0)
297 			prompt_type = PROMPT_NONE;
298 	}
299 
300 	setvbuf(stdout, 0, _IONBF, 0);
301 	result = passphrase_dialog(message, prompt_type);
302 	g_free(message);
303 
304 	return (result);
305 }
306