2006-06-15 [colin] 2.3.0cvs16
[claws.git] / src / plugins / pgpcore / passphrase.c
1 /* passphrase.c - GTK+ based passphrase callback
2  *      Copyright (C) 2001-2006 Werner Koch (dd9jn) and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #if USE_GPGME
24
25 #include <glib.h>
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>
44 #include <string.h>
45 #include <sys/types.h>
46 #ifdef G_OS_WIN32
47 #include <windows.h>
48 #else
49 #include <sys/mman.h>
50 #endif
51
52 #include "passphrase.h"
53 #include "prefs_common.h"
54 #include "prefs_gpg.h"
55 #include "manage_window.h"
56 #include "utils.h"
57 #include "mainwindow.h"
58 #include "summaryview.h"
59
60 static gboolean grab_all = FALSE;
61
62 static gboolean pass_ack;
63 static gchar *last_pass = NULL;
64
65 static void passphrase_ok_cb(GtkWidget *widget, gpointer data);
66 static void passphrase_cancel_cb(GtkWidget *widget, gpointer data);
67 static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
68                                gpointer data);
69 static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
70                                        gpointer data);
71 static gchar* passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint,
72                               gint prev_bad);
73
74 static GtkWidget *create_description(const gchar *uid_hint,
75                                      const gchar *pass_hint, gint prev_bad);
76
77 void
78 gpgmegtk_set_passphrase_grab(gint yes)
79 {
80     grab_all = yes;
81 }
82
83 static gchar*
84 passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
85 {
86     gchar *the_passphrase = NULL;
87     GtkWidget *vbox, *hbox;
88     GtkWidget *confirm_box;
89     GtkWidget *window;
90     GtkWidget *pass_entry;
91     GtkWidget *ok_button;
92     GtkWidget *cancel_button;
93
94     SummaryView *summaryview = mainwindow_get_mainwindow()->summaryview;
95     
96     gtk_menu_popdown(GTK_MENU(summaryview->popupmenu));
97
98     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
99     gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
100     gtk_widget_set_size_request(window, 450, -1);
101     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
102     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
103     gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
104     g_signal_connect(G_OBJECT(window), "delete_event",
105                      G_CALLBACK(passphrase_deleted), NULL);
106     g_signal_connect(G_OBJECT(window), "key_press_event",
107                      G_CALLBACK(passphrase_key_pressed), NULL);
108     MANAGE_WINDOW_SIGNALS_CONNECT(window);
109     manage_window_set_transient(GTK_WINDOW(window));
110
111     vbox = gtk_vbox_new(FALSE, 8);
112     gtk_container_add(GTK_CONTAINER(window), vbox);
113     gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
114
115     if (uid_hint || pass_hint) {
116         GtkWidget *label, *icon;
117         label = create_description (uid_hint, pass_hint, prev_bad);
118         icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
119                                 GTK_ICON_SIZE_DIALOG); 
120
121         hbox = gtk_hbox_new (FALSE, 12);
122         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
123         gtk_widget_show (hbox);
124         gtk_box_pack_start (GTK_BOX(hbox), icon, FALSE, FALSE, 0);
125         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
126         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
127     }
128
129     pass_entry = gtk_entry_new();
130     gtk_box_pack_start(GTK_BOX(vbox), pass_entry, FALSE, FALSE, 0);
131     gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE);
132     gtk_widget_grab_focus(pass_entry);
133
134     gtkut_stock_button_set_create(&confirm_box, 
135                                   &cancel_button, GTK_STOCK_CANCEL,
136                                   &ok_button, GTK_STOCK_OK,
137                                   NULL, NULL);
138     gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
139     gtk_widget_grab_default(ok_button);
140
141     g_signal_connect(G_OBJECT(ok_button), "clicked",
142                      G_CALLBACK(passphrase_ok_cb), NULL);
143     g_signal_connect(G_OBJECT(pass_entry), "activate",
144                      G_CALLBACK(passphrase_ok_cb), NULL);
145     g_signal_connect(G_OBJECT(cancel_button), "clicked",
146                      G_CALLBACK(passphrase_cancel_cb), NULL);
147
148     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
149     if (grab_all)   
150         gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
151     
152     gtk_widget_show_all(window);
153
154     if (grab_all) {
155         int err = 0, cnt = 0;
156         /* make sure that window is viewable */
157         gtk_widget_show_now(window);
158         gdk_window_process_updates(window->window, TRUE);
159         gdk_flush();
160         while(gtk_events_pending()) {
161                 gtk_main_iteration();
162         }
163 try_again:
164         if ((err = gdk_pointer_grab(window->window, TRUE, 0,
165                              window->window, NULL, GDK_CURRENT_TIME))) {
166             if (err == GDK_GRAB_NOT_VIEWABLE && cnt < 10) {
167                 cnt++;
168                 g_warning("trying to grab mouse again\n");
169                 gtk_main_iteration();
170                 goto try_again;
171             } else {
172                 g_warning("OOPS: Could not grab mouse\n");
173                 gtk_widget_destroy(window);
174                 return NULL;
175             }
176         }
177         if (gdk_keyboard_grab(window->window, FALSE, GDK_CURRENT_TIME)) {
178             gdk_display_pointer_ungrab(gdk_display_get_default(),
179                                        GDK_CURRENT_TIME);
180             g_warning("OOPS: Could not grab keyboard\n");
181             gtk_widget_destroy(window);
182             return NULL;
183         }
184     }
185
186     gtk_main();
187
188     if (grab_all) {
189         gdk_display_keyboard_ungrab(gdk_display_get_default(),
190                                     GDK_CURRENT_TIME);
191         gdk_display_pointer_ungrab(gdk_display_get_default(), GDK_CURRENT_TIME);
192         gdk_flush();
193     }
194
195     manage_window_focus_out(window, NULL, NULL);
196
197     if (pass_ack) {
198         const gchar *entry_text;
199         entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry));
200         the_passphrase = g_locale_from_utf8(entry_text, -1, NULL, NULL, NULL);
201         if (the_passphrase == NULL) 
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 *uid_hint, const gchar *pass_hint, gint prev_bad)
254 {
255     const gchar *uid = NULL, *info = NULL;
256     gchar *buf;
257     GtkWidget *label;
258     gchar *my_uid = NULL;
259     if (!uid_hint)
260         uid = _("[no user id]");
261     else
262         uid = uid_hint;
263     if (!pass_hint)
264         info = "";
265     else
266         info = pass_hint;
267
268     my_uid = g_strdup(uid);
269     while (strchr(my_uid, '<')) 
270         *(strchr(my_uid, '<')) = '(';
271     while (strchr(my_uid, '>')) 
272         *(strchr(my_uid, '>')) = ')';
273
274     buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">%sPlease enter the passphrase for:</span>\n\n"
275                            "%.*s\n"),
276                            prev_bad ?
277                            _("Bad passphrase.\n") : "",
278                            linelen (my_uid), my_uid);
279     g_free(my_uid);
280     label = gtk_label_new (buf);
281     gtk_label_set_use_markup(GTK_LABEL (label), TRUE);
282     gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
283     gtk_label_set_line_wrap(GTK_LABEL (label), TRUE);
284     g_free (buf);
285
286     return label;
287 }
288
289 static int free_passphrase(gpointer _unused)
290 {
291     if (last_pass != NULL) {
292 #ifndef G_PLATFORM_WIN32
293         munlock(last_pass, strlen(last_pass));
294 #endif
295         g_free(last_pass);
296         last_pass = NULL;
297         debug_print("%% passphrase removed\n");
298     }
299     
300     return FALSE;
301 }
302
303 gpgme_error_t
304 gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
305         const char *passphrase_hint, int prev_bad, int fd)
306 {
307     const char *pass;
308
309     if (prefs_gpg_get_config()->store_passphrase && last_pass && !prev_bad)
310         pass = last_pass;
311     else {
312     gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
313     debug_print ("%% requesting passphrase for '%s'\n ", uid_hint);
314     pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad);
315     gpgmegtk_free_passphrase();
316     if (!pass) {
317         debug_print ("%% cancel passphrase entry\n");
318         write(fd, "\n", 1);
319         return GPG_ERR_CANCELED;
320     }
321     else {
322         if (prefs_gpg_get_config()->store_passphrase) {
323             last_pass = g_strdup(pass);
324 #ifndef G_PLATFORM_WIN32
325             if (mlock(last_pass, strlen(last_pass)) == -1)
326                 debug_print("%% locking passphrase failed\n");
327 #endif
328             if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
329                     gtk_timeout_add(prefs_gpg_get_config()
330                                     ->store_passphrase_timeout*60*1000,
331                                 free_passphrase, NULL);
332             }
333         }
334         debug_print ("%% sending passphrase\n");
335     }
336     }
337
338 #ifdef G_OS_WIN32
339     {
340         /* Under Windows FD is actually a System handle. */
341         DWORD nwritten;
342         WriteFile ((HANDLE)fd, pass, strlen (pass), &nwritten, NULL);
343         WriteFile ((HANDLE)fd, "\n", 1, &nwritten, NULL);
344     }
345 #else
346     write(fd, pass, strlen(pass));
347     write(fd, "\n", 1);
348 #endif
349     return GPG_ERR_NO_ERROR;
350 }
351
352 void gpgmegtk_free_passphrase()
353 {
354     (void)free_passphrase(NULL); /* could be inline */
355 }
356
357 #endif /* USE_GPGME */