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