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