]> andersk Git - gssapi-openssh.git/blame - openssh/contrib/gnome-ssh-askpass2.c
merged OPENSSH_3_8P1_GSSAPI_20040304 to gpt-branch
[gssapi-openssh.git] / openssh / contrib / gnome-ssh-askpass2.c
CommitLineData
3c0ef626 1/*
e9a17296 2 * Copyright (c) 2000-2002 Damien Miller. All rights reserved.
3c0ef626 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
e54b3d7c 25/* GTK2 support by Nalin Dahyabhai <nalin@redhat.com> */
26
e9a17296 27/*
416fd2a8 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".
e9a17296 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
416fd2a8 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
e9a17296 36 * you don't trust your X server. We grab the keyboard always.
37 */
38
1c14df9e 39#define GRAB_TRIES 16
40#define GRAB_WAIT 250 /* milliseconds */
41
3c0ef626 42/*
43 * Compile with:
44 *
1c14df9e 45 * cc -Wall `pkg-config --cflags gtk+-2.0` \
e54b3d7c 46 * gnome-ssh-askpass2.c -o gnome-ssh-askpass \
47 * `pkg-config --libs gtk+-2.0`
3c0ef626 48 *
49 */
50
51#include <stdlib.h>
52#include <stdio.h>
53#include <string.h>
1c14df9e 54#include <unistd.h>
3c0ef626 55#include <X11/Xlib.h>
e54b3d7c 56#include <gtk/gtk.h>
3c0ef626 57#include <gdk/gdkx.h>
58
e54b3d7c 59static void
60report_failed_grab (const char *what)
3c0ef626 61{
62 GtkWidget *err;
63
e54b3d7c 64 err = gtk_message_dialog_new(NULL, 0,
65 GTK_MESSAGE_ERROR,
66 GTK_BUTTONS_CLOSE,
67 "Could not grab %s. "
68 "A malicious client may be eavesdropping "
69 "on your session.", what);
3c0ef626 70 gtk_window_set_position(GTK_WINDOW(err), GTK_WIN_POS_CENTER);
e54b3d7c 71 gtk_label_set_line_wrap(GTK_LABEL((GTK_MESSAGE_DIALOG(err))->label),
72 TRUE);
73
74 gtk_dialog_run(GTK_DIALOG(err));
75
76 gtk_widget_destroy(err);
77}
3c0ef626 78
e54b3d7c 79static void
80ok_dialog(GtkWidget *entry, gpointer dialog)
81{
82 g_return_if_fail(GTK_IS_DIALOG(dialog));
83 gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
3c0ef626 84}
85
e54b3d7c 86static int
3c0ef626 87passphrase_dialog(char *message)
88{
e54b3d7c 89 const char *failed;
90 char *passphrase, *local;
1c14df9e 91 int result, grab_tries, grab_server, grab_pointer;
92 GtkWidget *dialog, *entry;
e54b3d7c 93 GdkGrabStatus status;
3c0ef626 94
e9a17296 95 grab_server = (getenv("GNOME_SSH_ASKPASS_GRAB_SERVER") != NULL);
96 grab_pointer = (getenv("GNOME_SSH_ASKPASS_GRAB_POINTER") != NULL);
1c14df9e 97 grab_tries = 0;
e9a17296 98
e54b3d7c 99 dialog = gtk_message_dialog_new(NULL, 0,
100 GTK_MESSAGE_QUESTION,
101 GTK_BUTTONS_OK_CANCEL,
102 "%s",
103 message);
3c0ef626 104
105 entry = gtk_entry_new();
416fd2a8 106 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), entry, FALSE,
3c0ef626 107 FALSE, 0);
108 gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
109 gtk_widget_grab_focus(entry);
e54b3d7c 110 gtk_widget_show(entry);
3c0ef626 111
e54b3d7c 112 gtk_window_set_title(GTK_WINDOW(dialog), "OpenSSH");
3c0ef626 113 gtk_window_set_position (GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
e54b3d7c 114 gtk_label_set_line_wrap(GTK_LABEL((GTK_MESSAGE_DIALOG(dialog))->label),
115 TRUE);
3c0ef626 116
117 /* Make <enter> close dialog */
e54b3d7c 118 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
119 g_signal_connect(G_OBJECT(entry), "activate",
120 G_CALLBACK(ok_dialog), dialog);
3c0ef626 121
e54b3d7c 122 /* Grab focus */
123 gtk_widget_show_now(dialog);
e54b3d7c 124 if (grab_pointer) {
1c14df9e 125 for(;;) {
126 status = gdk_pointer_grab(
416fd2a8 127 (GTK_WIDGET(dialog))->window, TRUE, 0, NULL,
1c14df9e 128 NULL, GDK_CURRENT_TIME);
129 if (status == GDK_GRAB_SUCCESS)
130 break;
131 usleep(GRAB_WAIT * 1000);
132 if (++grab_tries > GRAB_TRIES) {
133 failed = "mouse";
134 goto nograb;
135 }
e54b3d7c 136 }
137 }
1c14df9e 138 for(;;) {
139 status = gdk_keyboard_grab((GTK_WIDGET(dialog))->window,
140 FALSE, GDK_CURRENT_TIME);
141 if (status == GDK_GRAB_SUCCESS)
142 break;
143 usleep(GRAB_WAIT * 1000);
144 if (++grab_tries > GRAB_TRIES) {
145 failed = "keyboard";
146 goto nograbkb;
147 }
e54b3d7c 148 }
1c14df9e 149 if (grab_server) {
150 gdk_x11_grab_server();
151 }
152
e54b3d7c 153 result = gtk_dialog_run(GTK_DIALOG(dialog));
3c0ef626 154
155 /* Ungrab */
e9a17296 156 if (grab_server)
157 XUngrabServer(GDK_DISPLAY());
158 if (grab_pointer)
159 gdk_pointer_ungrab(GDK_CURRENT_TIME);
3c0ef626 160 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
161 gdk_flush();
162
163 /* Report passphrase if user selected OK */
e54b3d7c 164 passphrase = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
165 if (result == GTK_RESPONSE_OK) {
166 local = g_locale_from_utf8(passphrase, strlen(passphrase),
167 NULL, NULL, NULL);
168 if (local != NULL) {
169 puts(local);
170 memset(local, '\0', strlen(local));
171 g_free(local);
172 } else {
173 puts(passphrase);
174 }
175 }
3c0ef626 176
177 /* Zero passphrase in memory */
e54b3d7c 178 memset(passphrase, '\b', strlen(passphrase));
3c0ef626 179 gtk_entry_set_text(GTK_ENTRY(entry), passphrase);
e54b3d7c 180 memset(passphrase, '\0', strlen(passphrase));
181 g_free(passphrase);
3c0ef626 182
e54b3d7c 183 gtk_widget_destroy(dialog);
184 return (result == GTK_RESPONSE_OK ? 0 : -1);
3c0ef626 185
186 /* At least one grab failed - ungrab what we got, and report
187 the failure to the user. Note that XGrabServer() cannot
188 fail. */
189 nograbkb:
190 gdk_pointer_ungrab(GDK_CURRENT_TIME);
191 nograb:
e9a17296 192 if (grab_server)
193 XUngrabServer(GDK_DISPLAY());
e54b3d7c 194 gtk_widget_destroy(dialog);
3c0ef626 195
e54b3d7c 196 report_failed_grab(failed);
197
198 return (-1);
3c0ef626 199}
200
201int
202main(int argc, char **argv)
203{
204 char *message;
e54b3d7c 205 int result;
3c0ef626 206
e54b3d7c 207 gtk_init(&argc, &argv);
208
209 if (argc > 1) {
210 message = g_strjoinv(" ", argv + 1);
211 } else {
212 message = g_strdup("Enter your OpenSSH passphrase:");
213 }
3c0ef626 214
215 setvbuf(stdout, 0, _IONBF, 0);
e54b3d7c 216 result = passphrase_dialog(message);
217 g_free(message);
218
219 return (result);
3c0ef626 220}
This page took 0.090318 seconds and 5 git commands to generate.