root / src / passphrase.c
History | View | Annotate | Download (9.9 kB)
| 1 | 1 | hiro | /* passphrase.c - GTK+ based passphrase callback
|
|---|---|---|---|
| 2 | 1 | hiro | * Copyright (C) 2001 Werner Koch (dd9jn) |
| 3 | 1 | hiro | * |
| 4 | 1 | hiro | * This program is free software; you can redistribute it and/or modify |
| 5 | 1 | hiro | * it under the terms of the GNU General Public License as published by |
| 6 | 1 | hiro | * the Free Software Foundation; either version 2 of the License, or |
| 7 | 1 | hiro | * (at your option) any later version. |
| 8 | 1 | hiro | * |
| 9 | 1 | hiro | * This program is distributed in the hope that it will be useful, |
| 10 | 1 | hiro | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | 1 | hiro | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | 1 | hiro | * GNU General Public License for more details. |
| 13 | 1 | hiro | * |
| 14 | 1 | hiro | * You should have received a copy of the GNU General Public License |
| 15 | 1 | hiro | * along with this program; if not, write to the Free Software |
| 16 | 1 | hiro | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | 1 | hiro | */ |
| 18 | 1 | hiro | |
| 19 | 1 | hiro | #ifdef HAVE_CONFIG_H
|
| 20 | 1 | hiro | # include <config.h> |
| 21 | 1 | hiro | #endif
|
| 22 | 1 | hiro | |
| 23 | 1 | hiro | #if USE_GPGME
|
| 24 | 1 | hiro | |
| 25 | 1 | hiro | #include <glib.h> |
| 26 | 92 | hiro | #include <glib/gi18n.h> |
| 27 | 160 | hiro | #include <gdk/gdktypes.h> |
| 28 | 1 | hiro | #include <gdk/gdkkeysyms.h> |
| 29 | 160 | hiro | #include <gdk/gdkdisplay.h> |
| 30 | 1 | hiro | #ifdef GDK_WINDOWING_X11
|
| 31 | 160 | hiro | # include <gdk/gdkx.h> |
| 32 | 1 | hiro | #endif /* GDK_WINDOWING_X11 */ |
| 33 | 1 | hiro | #include <gtk/gtkmain.h> |
| 34 | 1 | hiro | #include <gtk/gtkwidget.h> |
| 35 | 1 | hiro | #include <gtk/gtkwindow.h> |
| 36 | 1 | hiro | #include <gtk/gtkvbox.h> |
| 37 | 1 | hiro | #include <gtk/gtktable.h> |
| 38 | 1 | hiro | #include <gtk/gtklabel.h> |
| 39 | 1 | hiro | #include <gtk/gtkentry.h> |
| 40 | 1 | hiro | #include <gtk/gtkhbbox.h> |
| 41 | 1 | hiro | #include <gtk/gtkbutton.h> |
| 42 | 1 | hiro | #include <gtk/gtksignal.h> |
| 43 | 535 | hiro | #include <gtk/gtkstock.h> |
| 44 | 92 | hiro | #include <string.h> |
| 45 | 92 | hiro | #include <sys/types.h> |
| 46 | 733 | hiro | #if HAVE_SYS_MMAN_H
|
| 47 | 733 | hiro | # include <sys/mman.h> |
| 48 | 733 | hiro | #endif
|
| 49 | 1 | hiro | |
| 50 | 797 | hiro | #ifdef G_OS_WIN32
|
| 51 | 797 | hiro | # include <windows.h> |
| 52 | 797 | hiro | #endif
|
| 53 | 797 | hiro | |
| 54 | 1 | hiro | #include "passphrase.h" |
| 55 | 1 | hiro | #include "prefs_common.h" |
| 56 | 1 | hiro | #include "manage_window.h" |
| 57 | 1 | hiro | #include "utils.h" |
| 58 | 535 | hiro | #include "gtkutils.h" |
| 59 | 1 | hiro | |
| 60 | 160 | hiro | static gboolean grab_all = FALSE;
|
| 61 | 1 | hiro | |
| 62 | 1 | hiro | static gboolean pass_ack;
|
| 63 | 1 | hiro | static gchar *last_pass = NULL; |
| 64 | 1 | hiro | |
| 65 | 1 | hiro | static void passphrase_ok_cb(GtkWidget *widget, gpointer data); |
| 66 | 1 | hiro | static void passphrase_cancel_cb(GtkWidget *widget, gpointer data); |
| 67 | 1 | hiro | static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
|
| 68 | 1 | hiro | gpointer data); |
| 69 | 1 | hiro | static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
|
| 70 | 1 | hiro | gpointer data); |
| 71 | 56 | hiro | static gchar* passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, |
| 72 | 56 | hiro | gint prev_bad); |
| 73 | 1 | hiro | |
| 74 | 56 | hiro | static GtkWidget *create_description(const gchar *uid_hint, |
| 75 | 56 | hiro | const gchar *pass_hint, gint prev_bad);
|
| 76 | 1 | hiro | |
| 77 | 1 | hiro | void
|
| 78 | 56 | hiro | gpgmegtk_set_passphrase_grab(gint yes) |
| 79 | 1 | hiro | {
|
| 80 | 1 | hiro | grab_all = yes; |
| 81 | 1 | hiro | } |
| 82 | 1 | hiro | |
| 83 | 1 | hiro | static gchar*
|
| 84 | 56 | hiro | passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad) |
| 85 | 1 | hiro | {
|
| 86 | 1 | hiro | gchar *the_passphrase = NULL;
|
| 87 | 1 | hiro | GtkWidget *vbox; |
| 88 | 1 | hiro | GtkWidget *confirm_box; |
| 89 | 1 | hiro | GtkWidget *window; |
| 90 | 1 | hiro | GtkWidget *pass_entry; |
| 91 | 1 | hiro | GtkWidget *ok_button; |
| 92 | 1 | hiro | GtkWidget *cancel_button; |
| 93 | 1 | hiro | |
| 94 | 1 | hiro | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 95 | 1 | hiro | gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
|
| 96 | 1 | hiro | gtk_widget_set_size_request(window, 450, -1); |
| 97 | 1 | hiro | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
| 98 | 1 | hiro | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
| 99 | 1 | hiro | gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE); |
| 100 | 1 | hiro | g_signal_connect(G_OBJECT(window), "delete_event",
|
| 101 | 1 | hiro | G_CALLBACK(passphrase_deleted), NULL);
|
| 102 | 1 | hiro | g_signal_connect(G_OBJECT(window), "key_press_event",
|
| 103 | 1 | hiro | G_CALLBACK(passphrase_key_pressed), NULL);
|
| 104 | 1 | hiro | MANAGE_WINDOW_SIGNALS_CONNECT(window); |
| 105 | 1 | hiro | manage_window_set_transient(GTK_WINDOW(window)); |
| 106 | 1 | hiro | |
| 107 | 1 | hiro | vbox = gtk_vbox_new(FALSE, 8);
|
| 108 | 1 | hiro | gtk_container_add(GTK_CONTAINER(window), vbox); |
| 109 | 36 | hiro | gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
|
| 110 | 1 | hiro | |
| 111 | 56 | hiro | if (uid_hint || pass_hint) {
|
| 112 | 1 | hiro | GtkWidget *label; |
| 113 | 56 | hiro | label = create_description (uid_hint, pass_hint, prev_bad); |
| 114 | 36 | hiro | gtk_box_pack_start (GTK_BOX(vbox), label, FALSE, FALSE, 0);
|
| 115 | 1 | hiro | } |
| 116 | 1 | hiro | |
| 117 | 1 | hiro | pass_entry = gtk_entry_new(); |
| 118 | 36 | hiro | gtk_box_pack_start(GTK_BOX(vbox), pass_entry, FALSE, FALSE, 0);
|
| 119 | 36 | hiro | gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE); |
| 120 | 36 | hiro | gtk_widget_grab_focus(pass_entry); |
| 121 | 1 | hiro | |
| 122 | 36 | hiro | gtkut_stock_button_set_create(&confirm_box, &ok_button, GTK_STOCK_OK, |
| 123 | 36 | hiro | &cancel_button, GTK_STOCK_CANCEL, |
| 124 | 36 | hiro | NULL, NULL); |
| 125 | 1 | hiro | gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
|
| 126 | 36 | hiro | gtk_widget_grab_default(ok_button); |
| 127 | 1 | hiro | |
| 128 | 1 | hiro | g_signal_connect(G_OBJECT(ok_button), "clicked",
|
| 129 | 1 | hiro | G_CALLBACK(passphrase_ok_cb), NULL);
|
| 130 | 1 | hiro | g_signal_connect(G_OBJECT(pass_entry), "activate",
|
| 131 | 1 | hiro | G_CALLBACK(passphrase_ok_cb), NULL);
|
| 132 | 1 | hiro | g_signal_connect(G_OBJECT(cancel_button), "clicked",
|
| 133 | 1 | hiro | G_CALLBACK(passphrase_cancel_cb), NULL);
|
| 134 | 1 | hiro | |
| 135 | 1 | hiro | gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
| 136 | 1 | hiro | if (grab_all)
|
| 137 | 1 | hiro | gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE); |
| 138 | 1 | hiro | |
| 139 | 1 | hiro | gtk_widget_show_all(window); |
| 140 | 1 | hiro | |
| 141 | 1 | hiro | if (grab_all) {
|
| 142 | 160 | hiro | /* make sure that window is viewable
|
| 143 | 160 | hiro | * FIXME: this is still not enough */ |
| 144 | 160 | hiro | gtk_widget_show_now(window); |
| 145 | 160 | hiro | gdk_flush(); |
| 146 | 1 | hiro | #ifdef GDK_WINDOWING_X11
|
| 147 | 160 | hiro | gdk_x11_display_grab(gdk_display_get_default()); |
| 148 | 1 | hiro | #endif /* GDK_WINDOWING_X11 */ |
| 149 | 160 | hiro | if (gdk_pointer_grab(window->window, TRUE, 0, |
| 150 | 160 | hiro | window->window, NULL, GDK_CURRENT_TIME)) {
|
| 151 | 1 | hiro | #ifdef GDK_WINDOWING_X11
|
| 152 | 160 | hiro | gdk_x11_display_ungrab(gdk_display_get_default()); |
| 153 | 1 | hiro | #endif /* GDK_WINDOWING_X11 */ |
| 154 | 160 | hiro | g_warning("OOPS: Could not grab mouse\n");
|
| 155 | 160 | hiro | gtk_widget_destroy(window); |
| 156 | 1 | hiro | return NULL; |
| 157 | 1 | hiro | } |
| 158 | 160 | hiro | if (gdk_keyboard_grab(window->window, FALSE, GDK_CURRENT_TIME)) {
|
| 159 | 160 | hiro | gdk_display_pointer_ungrab(gdk_display_get_default(), |
| 160 | 160 | hiro | GDK_CURRENT_TIME); |
| 161 | 1 | hiro | #ifdef GDK_WINDOWING_X11
|
| 162 | 160 | hiro | gdk_x11_display_ungrab(gdk_display_get_default()); |
| 163 | 1 | hiro | #endif /* GDK_WINDOWING_X11 */ |
| 164 | 160 | hiro | g_warning("OOPS: Could not grab keyboard\n");
|
| 165 | 160 | hiro | gtk_widget_destroy(window); |
| 166 | 1 | hiro | return NULL; |
| 167 | 1 | hiro | } |
| 168 | 1 | hiro | } |
| 169 | 1 | hiro | |
| 170 | 1 | hiro | gtk_main(); |
| 171 | 1 | hiro | |
| 172 | 1 | hiro | if (grab_all) {
|
| 173 | 160 | hiro | gdk_display_keyboard_ungrab(gdk_display_get_default(), |
| 174 | 160 | hiro | GDK_CURRENT_TIME); |
| 175 | 160 | hiro | gdk_display_pointer_ungrab(gdk_display_get_default(), GDK_CURRENT_TIME); |
| 176 | 1 | hiro | #ifdef GDK_WINDOWING_X11
|
| 177 | 160 | hiro | gdk_x11_display_ungrab(gdk_display_get_default()); |
| 178 | 1 | hiro | #endif /* GDK_WINDOWING_X11 */ |
| 179 | 1 | hiro | gdk_flush(); |
| 180 | 1 | hiro | } |
| 181 | 1 | hiro | |
| 182 | 1 | hiro | manage_window_focus_out(window, NULL, NULL); |
| 183 | 1 | hiro | |
| 184 | 1 | hiro | if (pass_ack) {
|
| 185 | 1 | hiro | const gchar *entry_text;
|
| 186 | 1 | hiro | entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry)); |
| 187 | 936 | hiro | the_passphrase = g_locale_from_utf8(entry_text, -1, NULL, NULL, NULL); |
| 188 | 936 | hiro | if (!the_passphrase)
|
| 189 | 936 | hiro | the_passphrase = g_strdup(entry_text); |
| 190 | 1 | hiro | } |
| 191 | 797 | hiro | gtk_widget_destroy(window); |
| 192 | 1 | hiro | |
| 193 | 1 | hiro | return the_passphrase;
|
| 194 | 1 | hiro | } |
| 195 | 1 | hiro | |
| 196 | 1 | hiro | |
| 197 | 1 | hiro | static void |
| 198 | 1 | hiro | passphrase_ok_cb(GtkWidget *widget, gpointer data) |
| 199 | 1 | hiro | {
|
| 200 | 1 | hiro | pass_ack = TRUE; |
| 201 | 1 | hiro | gtk_main_quit(); |
| 202 | 1 | hiro | } |
| 203 | 1 | hiro | |
| 204 | 1 | hiro | static void |
| 205 | 1 | hiro | passphrase_cancel_cb(GtkWidget *widget, gpointer data) |
| 206 | 1 | hiro | {
|
| 207 | 1 | hiro | pass_ack = FALSE; |
| 208 | 1 | hiro | gtk_main_quit(); |
| 209 | 1 | hiro | } |
| 210 | 1 | hiro | |
| 211 | 1 | hiro | |
| 212 | 1 | hiro | static gint
|
| 213 | 1 | hiro | passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data) |
| 214 | 1 | hiro | {
|
| 215 | 1 | hiro | passphrase_cancel_cb(NULL, NULL); |
| 216 | 1 | hiro | return TRUE;
|
| 217 | 1 | hiro | } |
| 218 | 1 | hiro | |
| 219 | 1 | hiro | |
| 220 | 1 | hiro | static gboolean
|
| 221 | 1 | hiro | passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data) |
| 222 | 1 | hiro | {
|
| 223 | 1 | hiro | if (event && event->keyval == GDK_Escape)
|
| 224 | 1 | hiro | passphrase_cancel_cb(NULL, NULL); |
| 225 | 1 | hiro | return FALSE;
|
| 226 | 1 | hiro | } |
| 227 | 1 | hiro | |
| 228 | 1 | hiro | static gint
|
| 229 | 1 | hiro | linelen (const gchar *s)
|
| 230 | 1 | hiro | {
|
| 231 | 1 | hiro | gint i; |
| 232 | 1 | hiro | |
| 233 | 1 | hiro | for (i = 0; *s && *s != '\n'; s++, i++) |
| 234 | 1 | hiro | ; |
| 235 | 1 | hiro | |
| 236 | 1 | hiro | return i;
|
| 237 | 1 | hiro | } |
| 238 | 1 | hiro | |
| 239 | 1 | hiro | static GtkWidget *
|
| 240 | 56 | hiro | create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad) |
| 241 | 1 | hiro | {
|
| 242 | 56 | hiro | const gchar *uid = NULL, *info = NULL; |
| 243 | 1 | hiro | gchar *buf; |
| 244 | 1 | hiro | GtkWidget *label; |
| 245 | 1 | hiro | |
| 246 | 56 | hiro | if (!uid_hint)
|
| 247 | 1 | hiro | uid = _("[no user id]");
|
| 248 | 56 | hiro | else
|
| 249 | 56 | hiro | uid = uid_hint; |
| 250 | 56 | hiro | if (!pass_hint)
|
| 251 | 1 | hiro | info = "";
|
| 252 | 56 | hiro | else
|
| 253 | 56 | hiro | info = pass_hint; |
| 254 | 1 | hiro | |
| 255 | 1 | hiro | buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
|
| 256 | 1 | hiro | " %.*s \n"
|
| 257 | 1 | hiro | "(%.*s)\n"),
|
| 258 | 56 | hiro | prev_bad ? |
| 259 | 1 | hiro | _("Bad passphrase! Try again...\n\n") : "", |
| 260 | 1 | hiro | linelen (uid), uid, linelen (info), info); |
| 261 | 1 | hiro | |
| 262 | 1 | hiro | label = gtk_label_new (buf); |
| 263 | 36 | hiro | gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT); |
| 264 | 1 | hiro | g_free (buf); |
| 265 | 1 | hiro | |
| 266 | 1 | hiro | return label;
|
| 267 | 1 | hiro | } |
| 268 | 1 | hiro | |
| 269 | 1 | hiro | static int free_passphrase(gpointer _unused) |
| 270 | 1 | hiro | {
|
| 271 | 1 | hiro | if (last_pass != NULL) { |
| 272 | 733 | hiro | #if HAVE_MLOCK
|
| 273 | 1 | hiro | munlock(last_pass, strlen(last_pass)); |
| 274 | 733 | hiro | #endif
|
| 275 | 1 | hiro | g_free(last_pass); |
| 276 | 1 | hiro | last_pass = NULL;
|
| 277 | 797 | hiro | debug_print("%% passphrase removed\n");
|
| 278 | 1 | hiro | } |
| 279 | 1 | hiro | |
| 280 | 1 | hiro | return FALSE;
|
| 281 | 1 | hiro | } |
| 282 | 1 | hiro | |
| 283 | 56 | hiro | gpgme_error_t |
| 284 | 56 | hiro | gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint, |
| 285 | 56 | hiro | const char *passphrase_hint, int prev_bad, int fd) |
| 286 | 1 | hiro | {
|
| 287 | 1 | hiro | const char *pass; |
| 288 | 797 | hiro | #ifdef G_OS_WIN32
|
| 289 | 797 | hiro | HANDLE hd = (HANDLE)fd; |
| 290 | 797 | hiro | DWORD n; |
| 291 | 797 | hiro | #endif
|
| 292 | 1 | hiro | |
| 293 | 56 | hiro | if (prefs_common.store_passphrase && last_pass != NULL && !prev_bad) { |
| 294 | 797 | hiro | #ifdef G_OS_WIN32
|
| 295 | 797 | hiro | WriteFile(hd, last_pass, strlen(last_pass), &n, NULL);
|
| 296 | 797 | hiro | WriteFile(hd, "\n", 1, &n, NULL); |
| 297 | 797 | hiro | #else
|
| 298 | 56 | hiro | write(fd, last_pass, strlen(last_pass)); |
| 299 | 56 | hiro | write(fd, "\n", 1); |
| 300 | 797 | hiro | #endif
|
| 301 | 56 | hiro | return GPG_ERR_NO_ERROR;
|
| 302 | 1 | hiro | } |
| 303 | 1 | hiro | gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab); |
| 304 | 797 | hiro | debug_print ("%% requesting passphrase for `%s':\n", uid_hint);
|
| 305 | 56 | hiro | pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad); |
| 306 | 1 | hiro | gpgmegtk_free_passphrase(); |
| 307 | 1 | hiro | if (!pass) {
|
| 308 | 797 | hiro | debug_print ("%% cancel passphrase entry\n");
|
| 309 | 797 | hiro | #ifdef G_OS_WIN32
|
| 310 | 797 | hiro | WriteFile(hd, "\n", 1, &n, NULL); |
| 311 | 797 | hiro | CloseHandle(hd); /* somehow it will block without this */
|
| 312 | 797 | hiro | #else
|
| 313 | 56 | hiro | write(fd, "\n", 1); |
| 314 | 797 | hiro | #endif
|
| 315 | 56 | hiro | return GPG_ERR_CANCELED;
|
| 316 | 1 | hiro | } |
| 317 | 1 | hiro | else {
|
| 318 | 1 | hiro | if (prefs_common.store_passphrase) {
|
| 319 | 1 | hiro | last_pass = g_strdup(pass); |
| 320 | 733 | hiro | #if HAVE_MLOCK
|
| 321 | 1 | hiro | if (mlock(last_pass, strlen(last_pass)) == -1) |
| 322 | 797 | hiro | debug_print("%% locking passphrase failed\n");
|
| 323 | 733 | hiro | #endif
|
| 324 | 1 | hiro | |
| 325 | 1 | hiro | if (prefs_common.store_passphrase_timeout > 0) { |
| 326 | 2764 | hiro | g_timeout_add_full(G_PRIORITY_LOW, prefs_common.store_passphrase_timeout * 60 * 1000, free_passphrase, NULL, NULL); |
| 327 | 1 | hiro | } |
| 328 | 1 | hiro | } |
| 329 | 797 | hiro | debug_print ("%% sending passphrase\n");
|
| 330 | 1 | hiro | } |
| 331 | 797 | hiro | #ifdef G_OS_WIN32
|
| 332 | 797 | hiro | WriteFile(hd, pass, strlen(pass), &n, NULL);
|
| 333 | 797 | hiro | WriteFile(hd, "\n", 1, &n, NULL); |
| 334 | 797 | hiro | #else
|
| 335 | 56 | hiro | write(fd, pass, strlen(pass)); |
| 336 | 56 | hiro | write(fd, "\n", 1); |
| 337 | 797 | hiro | #endif
|
| 338 | 56 | hiro | return GPG_ERR_NO_ERROR;
|
| 339 | 1 | hiro | } |
| 340 | 1 | hiro | |
| 341 | 1 | hiro | void gpgmegtk_free_passphrase()
|
| 342 | 1 | hiro | {
|
| 343 | 1 | hiro | (void)free_passphrase(NULL); // could be inline |
| 344 | 1 | hiro | } |
| 345 | 1 | hiro | |
| 346 | 1 | hiro | #endif /* USE_GPGME */ |