2000-03-15 01:13:01 +00:00
|
|
|
/*
|
2002-01-24 13:59:25 +00:00
|
|
|
* Copyright (c) 2000-2002 Damien Miller. All rights reserved.
|
2001-02-16 00:18:58 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2000-03-15 01:13:01 +00:00
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
/* GTK2 support by Nalin Dahyabhai <nalin@redhat.com> */
|
|
|
|
|
2001-12-20 23:28:07 +00:00
|
|
|
/*
|
2003-11-21 12:48:55 +00:00
|
|
|
* This is a simple GNOME SSH passphrase grabber. To use it, set the
|
|
|
|
* environment variable SSH_ASKPASS to point to the location of
|
|
|
|
* gnome-ssh-askpass before calling "ssh-add < /dev/null".
|
2001-12-20 23:28:07 +00:00
|
|
|
*
|
2002-01-24 13:59:25 +00:00
|
|
|
* There is only two run-time options: if you set the environment variable
|
2002-01-24 13:46:04 +00:00
|
|
|
* "GNOME_SSH_ASKPASS_GRAB_SERVER=true" then gnome-ssh-askpass will grab
|
2003-11-21 12:48:55 +00:00
|
|
|
* the X server. If you set "GNOME_SSH_ASKPASS_GRAB_POINTER=true", then the
|
|
|
|
* pointer will be grabbed too. These may have some benefit to security if
|
2002-01-24 13:59:25 +00:00
|
|
|
* you don't trust your X server. We grab the keyboard always.
|
2001-12-20 23:28:07 +00:00
|
|
|
*/
|
|
|
|
|
2003-02-24 00:48:22 +00:00
|
|
|
#define GRAB_TRIES 16
|
|
|
|
#define GRAB_WAIT 250 /* milliseconds */
|
|
|
|
|
1999-11-09 02:52:33 +00:00
|
|
|
/*
|
2001-02-16 00:18:58 +00:00
|
|
|
* Compile with:
|
|
|
|
*
|
2003-02-24 00:48:22 +00:00
|
|
|
* cc -Wall `pkg-config --cflags gtk+-2.0` \
|
2002-09-12 04:49:00 +00:00
|
|
|
* gnome-ssh-askpass2.c -o gnome-ssh-askpass \
|
2002-09-05 06:46:24 +00:00
|
|
|
* `pkg-config --libs gtk+-2.0`
|
2001-02-16 00:18:58 +00:00
|
|
|
*
|
|
|
|
*/
|
1999-11-09 02:52:33 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2003-02-24 00:48:22 +00:00
|
|
|
#include <unistd.h>
|
1999-11-12 01:09:36 +00:00
|
|
|
#include <X11/Xlib.h>
|
2002-09-05 06:46:24 +00:00
|
|
|
#include <gtk/gtk.h>
|
1999-11-12 01:09:36 +00:00
|
|
|
#include <gdk/gdkx.h>
|
1999-11-09 02:52:33 +00:00
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
static void
|
|
|
|
report_failed_grab (const char *what)
|
2000-06-07 10:08:19 +00:00
|
|
|
{
|
|
|
|
GtkWidget *err;
|
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
err = gtk_message_dialog_new(NULL, 0,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Could not grab %s. "
|
|
|
|
"A malicious client may be eavesdropping "
|
|
|
|
"on your session.", what);
|
2000-06-07 10:08:19 +00:00
|
|
|
gtk_window_set_position(GTK_WINDOW(err), GTK_WIN_POS_CENTER);
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_label_set_line_wrap(GTK_LABEL((GTK_MESSAGE_DIALOG(err))->label),
|
|
|
|
TRUE);
|
|
|
|
|
|
|
|
gtk_dialog_run(GTK_DIALOG(err));
|
2000-06-07 10:08:19 +00:00
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_widget_destroy(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ok_dialog(GtkWidget *entry, gpointer dialog)
|
|
|
|
{
|
|
|
|
g_return_if_fail(GTK_IS_DIALOG(dialog));
|
|
|
|
gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
|
2000-06-07 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
2002-09-12 04:49:00 +00:00
|
|
|
static int
|
2000-06-07 10:08:19 +00:00
|
|
|
passphrase_dialog(char *message)
|
1999-11-09 02:52:33 +00:00
|
|
|
{
|
2002-09-05 06:46:24 +00:00
|
|
|
const char *failed;
|
|
|
|
char *passphrase, *local;
|
2003-02-24 00:48:22 +00:00
|
|
|
int result, grab_tries, grab_server, grab_pointer;
|
|
|
|
GtkWidget *dialog, *entry;
|
2002-09-05 06:46:24 +00:00
|
|
|
GdkGrabStatus status;
|
1999-11-09 02:52:33 +00:00
|
|
|
|
2002-01-24 13:46:04 +00:00
|
|
|
grab_server = (getenv("GNOME_SSH_ASKPASS_GRAB_SERVER") != NULL);
|
2002-01-24 13:59:25 +00:00
|
|
|
grab_pointer = (getenv("GNOME_SSH_ASKPASS_GRAB_POINTER") != NULL);
|
2003-02-24 00:48:22 +00:00
|
|
|
grab_tries = 0;
|
2001-12-20 23:28:07 +00:00
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
dialog = gtk_message_dialog_new(NULL, 0,
|
|
|
|
GTK_MESSAGE_QUESTION,
|
|
|
|
GTK_BUTTONS_OK_CANCEL,
|
|
|
|
"%s",
|
|
|
|
message);
|
1999-11-09 02:52:33 +00:00
|
|
|
|
|
|
|
entry = gtk_entry_new();
|
2003-11-21 12:48:55 +00:00
|
|
|
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), entry, FALSE,
|
2001-02-16 00:18:58 +00:00
|
|
|
FALSE, 0);
|
1999-11-09 02:52:33 +00:00
|
|
|
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
|
|
|
|
gtk_widget_grab_focus(entry);
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_widget_show(entry);
|
1999-11-12 01:09:36 +00:00
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_window_set_title(GTK_WINDOW(dialog), "OpenSSH");
|
1999-11-12 01:09:36 +00:00
|
|
|
gtk_window_set_position (GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
|
2008-02-29 11:53:40 +00:00
|
|
|
gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_label_set_line_wrap(GTK_LABEL((GTK_MESSAGE_DIALOG(dialog))->label),
|
|
|
|
TRUE);
|
1999-11-09 02:52:33 +00:00
|
|
|
|
1999-11-22 01:51:42 +00:00
|
|
|
/* Make <enter> close dialog */
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK);
|
|
|
|
g_signal_connect(G_OBJECT(entry), "activate",
|
|
|
|
G_CALLBACK(ok_dialog), dialog);
|
1999-11-22 01:51:42 +00:00
|
|
|
|
2009-11-18 06:51:59 +00:00
|
|
|
gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
|
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
/* Grab focus */
|
|
|
|
gtk_widget_show_now(dialog);
|
|
|
|
if (grab_pointer) {
|
2003-02-24 00:48:22 +00:00
|
|
|
for(;;) {
|
|
|
|
status = gdk_pointer_grab(
|
2003-11-21 12:48:55 +00:00
|
|
|
(GTK_WIDGET(dialog))->window, TRUE, 0, NULL,
|
2003-02-24 00:48:22 +00:00
|
|
|
NULL, GDK_CURRENT_TIME);
|
|
|
|
if (status == GDK_GRAB_SUCCESS)
|
|
|
|
break;
|
|
|
|
usleep(GRAB_WAIT * 1000);
|
|
|
|
if (++grab_tries > GRAB_TRIES) {
|
|
|
|
failed = "mouse";
|
|
|
|
goto nograb;
|
|
|
|
}
|
2002-09-05 06:46:24 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-24 00:48:22 +00:00
|
|
|
for(;;) {
|
|
|
|
status = gdk_keyboard_grab((GTK_WIDGET(dialog))->window,
|
|
|
|
FALSE, GDK_CURRENT_TIME);
|
|
|
|
if (status == GDK_GRAB_SUCCESS)
|
|
|
|
break;
|
|
|
|
usleep(GRAB_WAIT * 1000);
|
|
|
|
if (++grab_tries > GRAB_TRIES) {
|
|
|
|
failed = "keyboard";
|
|
|
|
goto nograbkb;
|
|
|
|
}
|
2002-09-05 06:46:24 +00:00
|
|
|
}
|
2003-02-24 00:48:22 +00:00
|
|
|
if (grab_server) {
|
|
|
|
gdk_x11_grab_server();
|
|
|
|
}
|
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
result = gtk_dialog_run(GTK_DIALOG(dialog));
|
2000-06-07 10:08:19 +00:00
|
|
|
|
1999-11-12 01:09:36 +00:00
|
|
|
/* Ungrab */
|
2001-12-20 23:28:07 +00:00
|
|
|
if (grab_server)
|
|
|
|
XUngrabServer(GDK_DISPLAY());
|
2002-01-24 13:59:25 +00:00
|
|
|
if (grab_pointer)
|
|
|
|
gdk_pointer_ungrab(GDK_CURRENT_TIME);
|
1999-11-12 01:09:36 +00:00
|
|
|
gdk_keyboard_ungrab(GDK_CURRENT_TIME);
|
|
|
|
gdk_flush();
|
|
|
|
|
2000-06-07 10:08:19 +00:00
|
|
|
/* Report passphrase if user selected OK */
|
2002-09-05 06:46:24 +00:00
|
|
|
passphrase = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
|
|
|
|
if (result == GTK_RESPONSE_OK) {
|
|
|
|
local = g_locale_from_utf8(passphrase, strlen(passphrase),
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
if (local != NULL) {
|
|
|
|
puts(local);
|
|
|
|
memset(local, '\0', strlen(local));
|
|
|
|
g_free(local);
|
|
|
|
} else {
|
|
|
|
puts(passphrase);
|
|
|
|
}
|
|
|
|
}
|
1999-11-09 02:52:33 +00:00
|
|
|
|
2000-06-07 10:08:19 +00:00
|
|
|
/* Zero passphrase in memory */
|
2002-09-05 06:46:24 +00:00
|
|
|
memset(passphrase, '\b', strlen(passphrase));
|
1999-11-09 02:52:33 +00:00
|
|
|
gtk_entry_set_text(GTK_ENTRY(entry), passphrase);
|
2002-09-05 06:46:24 +00:00
|
|
|
memset(passphrase, '\0', strlen(passphrase));
|
|
|
|
g_free(passphrase);
|
1999-11-09 02:52:33 +00:00
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_widget_destroy(dialog);
|
2002-09-12 04:49:00 +00:00
|
|
|
return (result == GTK_RESPONSE_OK ? 0 : -1);
|
1999-11-09 02:52:33 +00:00
|
|
|
|
2000-06-07 10:08:19 +00:00
|
|
|
/* At least one grab failed - ungrab what we got, and report
|
|
|
|
the failure to the user. Note that XGrabServer() cannot
|
|
|
|
fail. */
|
|
|
|
nograbkb:
|
|
|
|
gdk_pointer_ungrab(GDK_CURRENT_TIME);
|
|
|
|
nograb:
|
2001-12-20 23:28:07 +00:00
|
|
|
if (grab_server)
|
|
|
|
XUngrabServer(GDK_DISPLAY());
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_widget_destroy(dialog);
|
2000-06-07 10:08:19 +00:00
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
report_failed_grab(failed);
|
2002-09-12 04:49:00 +00:00
|
|
|
|
|
|
|
return (-1);
|
1999-11-09 02:52:33 +00:00
|
|
|
}
|
|
|
|
|
2000-06-07 10:08:19 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
1999-11-09 02:52:33 +00:00
|
|
|
{
|
|
|
|
char *message;
|
2002-09-12 04:49:00 +00:00
|
|
|
int result;
|
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
gtk_init(&argc, &argv);
|
1999-11-09 02:52:33 +00:00
|
|
|
|
2002-09-05 06:46:24 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
message = g_strjoinv(" ", argv + 1);
|
|
|
|
} else {
|
|
|
|
message = g_strdup("Enter your OpenSSH passphrase:");
|
|
|
|
}
|
1999-11-09 02:52:33 +00:00
|
|
|
|
2000-06-07 10:08:19 +00:00
|
|
|
setvbuf(stdout, 0, _IONBF, 0);
|
2002-09-12 04:49:00 +00:00
|
|
|
result = passphrase_dialog(message);
|
2002-09-05 06:46:24 +00:00
|
|
|
g_free(message);
|
|
|
|
|
2002-09-12 04:49:00 +00:00
|
|
|
return (result);
|
1999-11-09 02:52:33 +00:00
|
|
|
}
|