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