1 /* passphrase.c - GTK+ based passphrase callback
2 * Copyright (C) 2001 Werner Koch (dd9jn)
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.
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.
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.
26 #include <sys/types.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gdk/gdkx.h> /* GDK_DISPLAY() */
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkwidget.h>
33 #include <gtk/gtkwindow.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtktable.h>
36 #include <gtk/gtklabel.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkhbbox.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkfilesel.h>
41 #include <gtk/gtksignal.h>
44 #include "passphrase.h"
45 #include "prefs_common.h"
46 #include "manage_window.h"
49 static int grab_all = 0;
51 static gboolean pass_ack;
52 static gchar *last_pass = NULL;
54 static void passphrase_ok_cb(GtkWidget *widget, gpointer data);
55 static void passphrase_cancel_cb(GtkWidget *widget, gpointer data);
56 static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
58 static void passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
60 static gchar* passphrase_mbox (const gchar *desc);
63 static GtkWidget *create_description (const gchar *desc);
66 gpgmegtk_set_passphrase_grab (gint yes)
72 passphrase_mbox (const gchar *desc)
74 gchar *the_passphrase = NULL;
77 GtkWidget *pass_label;
78 GtkWidget *confirm_box;
80 GtkWidget *pass_entry;
82 GtkWidget *cancel_button;
84 window = gtk_window_new(GTK_WINDOW_DIALOG);
85 gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
86 gtk_widget_set_usize(window, 450, -1);
87 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
88 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
89 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
90 gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
91 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
92 GTK_SIGNAL_FUNC(passphrase_deleted), NULL);
93 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
94 GTK_SIGNAL_FUNC(passphrase_key_pressed), NULL);
95 MANAGE_WINDOW_SIGNALS_CONNECT(window);
96 manage_window_set_transient(GTK_WINDOW(window));
98 vbox = gtk_vbox_new(FALSE, 8);
99 gtk_container_add(GTK_CONTAINER(window), vbox);
103 label = create_description (desc);
104 gtk_box_pack_start (GTK_BOX(vbox), label, TRUE, TRUE, 0);
107 table = gtk_table_new(2, 2, FALSE);
108 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
109 gtk_container_set_border_width(GTK_CONTAINER(table), 8);
110 gtk_table_set_row_spacings(GTK_TABLE(table), 12);
111 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
114 pass_label = gtk_label_new("");
115 gtk_table_attach (GTK_TABLE(table), pass_label, 0, 1, 0, 1,
116 GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
117 gtk_misc_set_alignment (GTK_MISC (pass_label), 1, 0.5);
119 pass_entry = gtk_entry_new();
120 gtk_table_attach (GTK_TABLE(table), pass_entry, 1, 2, 0, 1,
121 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
122 gtk_entry_set_visibility (GTK_ENTRY(pass_entry), FALSE);
123 gtk_widget_grab_focus (pass_entry);
126 confirm_box = gtk_hbutton_box_new ();
127 gtk_button_box_set_layout (GTK_BUTTON_BOX(confirm_box), GTK_BUTTONBOX_END);
128 gtk_button_box_set_spacing (GTK_BUTTON_BOX(confirm_box), 5);
130 ok_button = gtk_button_new_with_label (_("OK"));
131 GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
132 gtk_box_pack_start (GTK_BOX(confirm_box), ok_button, TRUE, TRUE, 0);
134 cancel_button = gtk_button_new_with_label (_("Cancel"));
135 GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
136 gtk_box_pack_start(GTK_BOX(confirm_box), cancel_button, TRUE, TRUE, 0);
138 gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
139 gtk_widget_grab_default (ok_button);
141 gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
142 GTK_SIGNAL_FUNC(passphrase_ok_cb), NULL);
143 gtk_signal_connect(GTK_OBJECT(pass_entry), "activate",
144 GTK_SIGNAL_FUNC(passphrase_ok_cb), NULL);
145 gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
146 GTK_SIGNAL_FUNC(passphrase_cancel_cb), NULL);
149 gtk_object_set (GTK_OBJECT(window), "type", GTK_WINDOW_POPUP, NULL);
150 gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
152 gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
154 gtk_widget_show_all(window);
156 /* don't use XIM on entering passphrase */
157 gtkut_editable_disable_im(GTK_EDITABLE(pass_entry));
160 XGrabServer(GDK_DISPLAY());
161 if ( gdk_pointer_grab ( window->window, TRUE, 0,
162 NULL, NULL, GDK_CURRENT_TIME)) {
163 XUngrabServer ( GDK_DISPLAY() );
164 g_warning ("OOPS: Could not grab mouse\n");
165 gtk_widget_destroy (window);
168 if ( gdk_keyboard_grab( window->window, FALSE, GDK_CURRENT_TIME )) {
169 gdk_pointer_ungrab (GDK_CURRENT_TIME);
170 XUngrabServer ( GDK_DISPLAY() );
171 g_warning ("OOPS: Could not grab keyboard\n");
172 gtk_widget_destroy (window);
180 XUngrabServer (GDK_DISPLAY());
181 gdk_pointer_ungrab (GDK_CURRENT_TIME);
182 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
186 manage_window_focus_out(window, NULL, NULL);
189 the_passphrase = gtk_entry_get_text(GTK_ENTRY(pass_entry));
190 if (the_passphrase) /* Hmmm: Do we really need this? */
191 the_passphrase = g_strdup (the_passphrase);
193 gtk_widget_destroy (window);
195 return the_passphrase;
200 passphrase_ok_cb(GtkWidget *widget, gpointer data)
207 passphrase_cancel_cb(GtkWidget *widget, gpointer data)
215 passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
217 passphrase_cancel_cb(NULL, NULL);
223 passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
225 if (event && event->keyval == GDK_Escape)
226 passphrase_cancel_cb(NULL, NULL);
230 linelen (const gchar *s)
234 for (i = 0; *s && *s != '\n'; s++, i++)
241 create_description (const gchar *desc)
243 const gchar *cmd = NULL, *uid = NULL, *info = NULL;
248 uid = strchr (cmd, '\n');
250 info = strchr (++uid, '\n');
256 uid = _("[no user id]");
260 buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
263 !strncmp (cmd, "TRY_AGAIN", 9 ) ?
264 _("Bad passphrase! Try again...\n\n") : "",
265 linelen (uid), uid, linelen (info), info);
267 label = gtk_label_new (buf);
273 static int free_passphrase(gpointer _unused)
275 if (last_pass != NULL) {
276 munlock(last_pass, strlen(last_pass));
279 debug_print("%% passphrase removed");
286 gpgmegtk_passphrase_cb (void *opaque, const char *desc, void **r_hd)
288 struct passphrase_cb_info_s *info = opaque;
289 GpgmeCtx ctx = info ? info->c : NULL;
293 /* FIXME: cleanup by looking at *r_hd */
296 if (prefs_common.store_passphrase && last_pass != NULL &&
297 strncmp(desc, "TRY_AGAIN", 9) != 0)
298 return g_strdup(last_pass);
300 gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab);
301 debug_print ("%% requesting passphrase for `%s': ", desc);
302 pass = passphrase_mbox (desc);
303 gpgmegtk_free_passphrase();
305 debug_print ("%% cancel passphrase entry");
309 if (prefs_common.store_passphrase) {
310 last_pass = g_strdup(pass);
311 if (mlock(last_pass, strlen(last_pass)) == -1)
312 debug_print("%% locking passphrase failed");
314 if (prefs_common.store_passphrase_timeout > 0) {
315 gtk_timeout_add(prefs_common.store_passphrase_timeout*60*1000,
316 free_passphrase, NULL);
319 debug_print ("%% sending passphrase");
325 void gpgmegtk_free_passphrase(void)
327 (void)free_passphrase(NULL); /* could be inline */
330 #endif /* USE_GPGME */