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