Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 1 | /* |
Damien Miller | 8b1c22b | 2000-03-15 12:13:01 +1100 | [diff] [blame] | 2 | Compile with: |
| 3 | |
| 4 | cc `gnome-config --cflags gnome gnomeui` \ |
| 5 | gnome-ssh-askpass.c -o gnome-ssh-askpass \ |
| 6 | `gnome-config --libs gnome gnomeui` |
| 7 | |
| 8 | */ |
| 9 | |
| 10 | /* |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 11 | ** |
| 12 | ** GNOME ssh passphrase requestor |
| 13 | ** |
| 14 | ** Damien Miller <djm@ibs.com.au> |
| 15 | ** |
| 16 | ** Copyright 1999 Internet Business Solutions |
| 17 | ** |
| 18 | ** Permission is hereby granted, free of charge, to any person |
| 19 | ** obtaining a copy of this software and associated documentation |
| 20 | ** files (the "Software"), to deal in the Software without |
| 21 | ** restriction, including without limitation the rights to use, copy, |
| 22 | ** modify, merge, publish, distribute, sublicense, and/or sell copies |
| 23 | ** of the Software, and to permit persons to whom the Software is |
| 24 | ** furnished to do so, subject to the following conditions: |
| 25 | ** |
| 26 | ** The above copyright notice and this permission notice shall be |
| 27 | ** included in all copies or substantial portions of the Software. |
| 28 | ** |
| 29 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 30 | ** KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 31 | ** WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
| 32 | ** AND NONINFRINGEMENT. IN NO EVENT SHALL DAMIEN MILLER OR INTERNET |
| 33 | ** BUSINESS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 35 | ** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 36 | ** OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | ** |
| 38 | ** Except as contained in this notice, the name of Internet Business |
| 39 | ** Solutions shall not be used in advertising or otherwise to promote |
| 40 | ** the sale, use or other dealings in this Software without prior |
| 41 | ** written authorization from Internet Business Solutions. |
| 42 | ** |
| 43 | */ |
| 44 | |
| 45 | #include <stdlib.h> |
| 46 | #include <stdio.h> |
| 47 | #include <string.h> |
| 48 | #include <gnome.h> |
Damien Miller | b9a692d | 1999-11-12 12:09:36 +1100 | [diff] [blame] | 49 | #include <X11/Xlib.h> |
| 50 | #include <gdk/gdkx.h> |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 51 | |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 52 | void |
| 53 | report_failed_grab (void) |
| 54 | { |
| 55 | GtkWidget *err; |
| 56 | |
| 57 | err = gnome_message_box_new("Could not grab keyboard or mouse.\n" |
| 58 | "A malicious client may be eavesdropping on your session.", |
| 59 | GNOME_MESSAGE_BOX_ERROR, "EXIT", NULL); |
| 60 | gtk_window_set_position(GTK_WINDOW(err), GTK_WIN_POS_CENTER); |
| 61 | gtk_object_set(GTK_OBJECT(err), "type", GTK_WINDOW_POPUP, NULL); |
| 62 | |
| 63 | gnome_dialog_run_and_close(GNOME_DIALOG(err)); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | passphrase_dialog(char *message) |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 68 | { |
| 69 | char *passphrase; |
| 70 | int result; |
| 71 | |
| 72 | GtkWidget *dialog, *entry, *label; |
| 73 | |
| 74 | dialog = gnome_dialog_new("OpenSSH", GNOME_STOCK_BUTTON_OK, |
| 75 | GNOME_STOCK_BUTTON_CANCEL, NULL); |
| 76 | |
| 77 | label = gtk_label_new(message); |
| 78 | gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox), label, FALSE, |
| 79 | FALSE, 0); |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 80 | |
| 81 | entry = gtk_entry_new(); |
| 82 | gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dialog)->vbox), entry, FALSE, |
| 83 | FALSE, 0); |
| 84 | gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE); |
| 85 | gtk_widget_grab_focus(entry); |
Damien Miller | b9a692d | 1999-11-12 12:09:36 +1100 | [diff] [blame] | 86 | |
| 87 | /* Center window and prepare for grab */ |
| 88 | gtk_object_set(GTK_OBJECT(dialog), "type", GTK_WINDOW_POPUP, NULL); |
| 89 | gnome_dialog_set_default(GNOME_DIALOG(dialog), 0); |
| 90 | gtk_window_set_position (GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); |
| 91 | gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, TRUE); |
| 92 | gnome_dialog_close_hides(GNOME_DIALOG(dialog), TRUE); |
| 93 | gtk_container_set_border_width(GTK_CONTAINER(GNOME_DIALOG(dialog)->vbox), GNOME_PAD); |
| 94 | gtk_widget_show_all(dialog); |
| 95 | |
| 96 | /* Grab focus */ |
| 97 | XGrabServer(GDK_DISPLAY()); |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 98 | if (gdk_pointer_grab(dialog->window, TRUE, 0, |
| 99 | NULL, NULL, GDK_CURRENT_TIME)) |
| 100 | goto nograb; |
| 101 | if (gdk_keyboard_grab(dialog->window, FALSE, GDK_CURRENT_TIME)) |
| 102 | goto nograbkb; |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 103 | |
Damien Miller | 2221872 | 1999-11-22 12:51:42 +1100 | [diff] [blame] | 104 | /* Make <enter> close dialog */ |
| 105 | gnome_dialog_editable_enters(GNOME_DIALOG(dialog), GTK_EDITABLE(entry)); |
| 106 | |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 107 | /* Run dialog */ |
| 108 | result = gnome_dialog_run(GNOME_DIALOG(dialog)); |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 109 | |
Damien Miller | b9a692d | 1999-11-12 12:09:36 +1100 | [diff] [blame] | 110 | /* Ungrab */ |
| 111 | XUngrabServer(GDK_DISPLAY()); |
| 112 | gdk_pointer_ungrab(GDK_CURRENT_TIME); |
| 113 | gdk_keyboard_ungrab(GDK_CURRENT_TIME); |
| 114 | gdk_flush(); |
| 115 | |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 116 | /* Report passphrase if user selected OK */ |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 117 | passphrase = gtk_entry_get_text(GTK_ENTRY(entry)); |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 118 | if (result == 0) |
| 119 | puts(passphrase); |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 120 | |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 121 | /* Zero passphrase in memory */ |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 122 | memset(passphrase, '\0', strlen(passphrase)); |
| 123 | gtk_entry_set_text(GTK_ENTRY(entry), passphrase); |
| 124 | |
| 125 | gnome_dialog_close(GNOME_DIALOG(dialog)); |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 126 | return; |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 127 | |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 128 | /* At least one grab failed - ungrab what we got, and report |
| 129 | the failure to the user. Note that XGrabServer() cannot |
| 130 | fail. */ |
| 131 | nograbkb: |
| 132 | gdk_pointer_ungrab(GDK_CURRENT_TIME); |
| 133 | nograb: |
| 134 | XUngrabServer(GDK_DISPLAY()); |
| 135 | gnome_dialog_close(GNOME_DIALOG(dialog)); |
| 136 | |
| 137 | report_failed_grab(); |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 138 | } |
| 139 | |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 140 | int |
| 141 | main(int argc, char **argv) |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 142 | { |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 143 | char *message; |
| 144 | |
| 145 | gnome_init("GNOME ssh-askpass", "0.1", argc, argv); |
| 146 | |
| 147 | if (argc == 2) |
| 148 | message = argv[1]; |
| 149 | else |
| 150 | message = "Enter your OpenSSH passphrase:"; |
| 151 | |
Damien Miller | 5314ae7 | 2000-06-07 20:08:19 +1000 | [diff] [blame] | 152 | setvbuf(stdout, 0, _IONBF, 0); |
| 153 | passphrase_dialog(message); |
Damien Miller | d3dcfaf | 1999-11-09 13:52:33 +1100 | [diff] [blame] | 154 | return 0; |
| 155 | } |