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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <glib/gi18n.h>
27 #include <gdk/gdktypes.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gdk/gdkdisplay.h>
30 #ifdef GDK_WINDOWING_X11
31 # include <gdk/gdkx.h>
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>
45 #include <sys/types.h>
48 #include "passphrase.h"
49 #include "prefs_common.h"
50 #include "prefs_gpg.h"
51 #include "manage_window.h"
53 #include "mainwindow.h"
54 #include "summaryview.h"
56 static gboolean grab_all = FALSE;
58 static gboolean pass_ack;
59 static gchar *last_pass = NULL;
61 static void passphrase_ok_cb(GtkWidget *widget, gpointer data);
62 static void passphrase_cancel_cb(GtkWidget *widget, gpointer data);
63 static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
65 static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
67 static gchar* passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint,
70 static GtkWidget *create_description(const gchar *uid_hint,
71 const gchar *pass_hint, gint prev_bad);
74 gpgmegtk_set_passphrase_grab(gint yes)
80 passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
82 gchar *the_passphrase = NULL;
84 GtkWidget *confirm_box;
86 GtkWidget *pass_entry;
88 GtkWidget *cancel_button;
90 SummaryView *summaryview = mainwindow_get_mainwindow()->summaryview;
92 gtk_menu_popdown(GTK_MENU(summaryview->popupmenu));
94 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
95 gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
96 gtk_widget_set_size_request(window, 450, -1);
97 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
98 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
99 gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
100 g_signal_connect(G_OBJECT(window), "delete_event",
101 G_CALLBACK(passphrase_deleted), NULL);
102 g_signal_connect(G_OBJECT(window), "key_press_event",
103 G_CALLBACK(passphrase_key_pressed), NULL);
104 MANAGE_WINDOW_SIGNALS_CONNECT(window);
105 manage_window_set_transient(GTK_WINDOW(window));
107 vbox = gtk_vbox_new(FALSE, 8);
108 gtk_container_add(GTK_CONTAINER(window), vbox);
109 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
111 if (uid_hint || pass_hint) {
113 label = create_description (uid_hint, pass_hint, prev_bad);
114 gtk_box_pack_start (GTK_BOX(vbox), label, FALSE, FALSE, 0);
117 pass_entry = gtk_entry_new();
118 gtk_box_pack_start(GTK_BOX(vbox), pass_entry, FALSE, FALSE, 0);
119 gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE);
120 gtk_widget_grab_focus(pass_entry);
122 gtkut_stock_button_set_create(&confirm_box, &ok_button, GTK_STOCK_OK,
123 &cancel_button, GTK_STOCK_CANCEL,
125 gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
126 gtk_widget_grab_default(ok_button);
128 g_signal_connect(G_OBJECT(ok_button), "clicked",
129 G_CALLBACK(passphrase_ok_cb), NULL);
130 g_signal_connect(G_OBJECT(pass_entry), "activate",
131 G_CALLBACK(passphrase_ok_cb), NULL);
132 g_signal_connect(G_OBJECT(cancel_button), "clicked",
133 G_CALLBACK(passphrase_cancel_cb), NULL);
135 gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
137 gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
139 gtk_widget_show_all(window);
142 int err = 0, cnt = 0;
143 /* make sure that window is viewable */
144 gtk_widget_show_now(window);
146 while(gtk_events_pending())
147 gtk_main_iteration();
148 #ifdef GDK_WINDOWING_X11
149 gdk_x11_display_grab(gdk_display_get_default());
150 #endif /* GDK_WINDOWING_X11 */
152 if ((err = gdk_pointer_grab(window->window, TRUE, 0,
153 window->window, NULL, GDK_CURRENT_TIME))) {
154 if (err == GDK_GRAB_NOT_VIEWABLE && cnt < 10) {
156 g_warning("trying to grab mouse again\n");
159 #ifdef GDK_WINDOWING_X11
160 gdk_x11_display_ungrab(gdk_display_get_default());
161 #endif /* GDK_WINDOWING_X11 */
162 g_warning("OOPS: Could not grab mouse\n");
163 gtk_widget_destroy(window);
167 if (gdk_keyboard_grab(window->window, FALSE, GDK_CURRENT_TIME)) {
168 gdk_display_pointer_ungrab(gdk_display_get_default(),
170 #ifdef GDK_WINDOWING_X11
171 gdk_x11_display_ungrab(gdk_display_get_default());
172 #endif /* GDK_WINDOWING_X11 */
173 g_warning("OOPS: Could not grab keyboard\n");
174 gtk_widget_destroy(window);
182 gdk_display_keyboard_ungrab(gdk_display_get_default(),
184 gdk_display_pointer_ungrab(gdk_display_get_default(), GDK_CURRENT_TIME);
185 #ifdef GDK_WINDOWING_X11
186 gdk_x11_display_ungrab(gdk_display_get_default());
187 #endif /* GDK_WINDOWING_X11 */
191 manage_window_focus_out(window, NULL, NULL);
194 const gchar *entry_text;
195 entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry));
196 if (entry_text) /* Hmmm: Do we really need this? */
197 the_passphrase = g_strdup (entry_text);
199 gtk_widget_destroy (window);
201 return the_passphrase;
206 passphrase_ok_cb(GtkWidget *widget, gpointer data)
213 passphrase_cancel_cb(GtkWidget *widget, gpointer data)
221 passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
223 passphrase_cancel_cb(NULL, NULL);
229 passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
231 if (event && event->keyval == GDK_Escape)
232 passphrase_cancel_cb(NULL, NULL);
237 linelen (const gchar *s)
241 for (i = 0; *s && *s != '\n'; s++, i++)
248 create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
250 const gchar *uid = NULL, *info = NULL;
255 uid = _("[no user id]");
263 buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
267 _("Bad passphrase! Try again...\n\n") : "",
268 linelen (uid), uid, linelen (info), info);
270 label = gtk_label_new (buf);
271 gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
277 static int free_passphrase(gpointer _unused)
279 if (last_pass != NULL) {
280 munlock(last_pass, strlen(last_pass));
283 debug_print("%% passphrase removed");
290 gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
291 const char *passphrase_hint, int prev_bad, int fd)
295 if (prefs_gpg_get_config()->store_passphrase && last_pass != NULL && !prev_bad) {
296 write(fd, last_pass, strlen(last_pass));
298 return GPG_ERR_NO_ERROR;
300 gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
301 debug_print ("%% requesting passphrase for '%s': ", uid_hint);
302 pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad);
303 gpgmegtk_free_passphrase();
305 debug_print ("%% cancel passphrase entry");
307 return GPG_ERR_CANCELED;
310 if (prefs_gpg_get_config()->store_passphrase) {
311 last_pass = g_strdup(pass);
312 if (mlock(last_pass, strlen(last_pass)) == -1)
313 debug_print("%% locking passphrase failed");
315 if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
316 gtk_timeout_add(prefs_gpg_get_config()->store_passphrase_timeout*60*1000,
317 free_passphrase, NULL);
320 debug_print ("%% sending passphrase");
322 write(fd, pass, strlen(pass));
324 return GPG_ERR_NO_ERROR;
327 void gpgmegtk_free_passphrase()
329 (void)free_passphrase(NULL); /* could be inline */
332 #endif /* USE_GPGME */