3ef77b1b2acbdaf44737645880bbcfeb280f2a2b
[claws.git] / src / plugins / pgpcore / passphrase.c
1 /* passphrase.c - GTK+ based passphrase callback
2  *      Copyright (C) 2001-2012 Werner Koch (dd9jn) and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
16  * 
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/gdk.h>
28 #include <gdk/gdkkeysyms.h>
29 #ifdef GDK_WINDOWING_X11
30 #  include <gdk/gdkx.h>
31 #endif /* GDK_WINDOWING_X11 */
32 #include <gtk/gtk.h> 
33 #include <string.h>
34 #include <sys/types.h>
35 #ifdef G_OS_WIN32
36 #include <w32lib.h>
37 #else
38 #include <sys/mman.h>
39 #endif
40
41 #include "passphrase.h"
42 #include "prefs_common.h"
43 #include "prefs_gpg.h"
44 #include "manage_window.h"
45 #include "utils.h"
46 #include "mainwindow.h"
47 #include "summaryview.h"
48
49 static gboolean grab_all = FALSE;
50
51 static gboolean pass_ack;
52 static gchar *last_pass = NULL;
53
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,
57                                gpointer data);
58 static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
59                                        gpointer data);
60
61 static GtkWidget *create_description(const gchar *uid_hint,
62                                      const gchar *pass_hint, gint prev_bad, gint new_key);
63
64 void
65 gpgmegtk_set_passphrase_grab(gint yes)
66 {
67     grab_all = yes;
68 }
69
70 gchar*
71 passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gint new_key)
72 {
73     gchar *the_passphrase = NULL;
74     GtkWidget *vbox, *hbox;
75     GtkWidget *confirm_box;
76     GtkWidget *window;
77     GtkWidget *pass_entry;
78     GtkWidget *ok_button;
79     GtkWidget *cancel_button;
80     GdkWindow *gdkwin;
81
82     SummaryView *summaryview = mainwindow_get_mainwindow()->summaryview;
83     
84     gtk_menu_popdown(GTK_MENU(summaryview->popupmenu));
85
86     window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "passphrase");
87     gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
88     gtk_window_set_default_size(GTK_WINDOW(window), 375, 100);
89     gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
90     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
91     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
92     g_signal_connect(G_OBJECT(window), "delete_event",
93                      G_CALLBACK(passphrase_deleted), NULL);
94     g_signal_connect(G_OBJECT(window), "key_press_event",
95                      G_CALLBACK(passphrase_key_pressed), NULL);
96     MANAGE_WINDOW_SIGNALS_CONNECT(window);
97     manage_window_set_transient(GTK_WINDOW(window));
98
99     vbox = gtk_vbox_new(FALSE, 8);
100     gtk_container_add(GTK_CONTAINER(window), vbox);
101     gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
102
103     if (uid_hint || pass_hint) {
104         GtkWidget *label, *icon;
105         label = create_description (uid_hint, pass_hint, prev_bad, new_key);
106         icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
107                                 GTK_ICON_SIZE_DIALOG); 
108
109         hbox = gtk_hbox_new (FALSE, 12);
110         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
111         gtk_widget_show (hbox);
112         gtk_box_pack_start (GTK_BOX(hbox), icon, FALSE, FALSE, 0);
113         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
114         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
115     }
116
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 #ifdef MAEMO
121     hildon_gtk_entry_set_input_mode(GTK_ENTRY(pass_entry), 
122         HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
123 #endif
124     gtk_widget_grab_focus(pass_entry);
125
126     gtkut_stock_button_set_create(&confirm_box, 
127                                   &cancel_button, GTK_STOCK_CANCEL,
128                                   &ok_button, GTK_STOCK_OK,
129                                   NULL, NULL);
130
131     gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
132     gtk_widget_grab_default(ok_button);
133
134     g_signal_connect(G_OBJECT(ok_button), "clicked",
135                      G_CALLBACK(passphrase_ok_cb), NULL);
136     g_signal_connect(G_OBJECT(pass_entry), "activate",
137                      G_CALLBACK(passphrase_ok_cb), NULL);
138     g_signal_connect(G_OBJECT(cancel_button), "clicked",
139                      G_CALLBACK(passphrase_cancel_cb), NULL);
140
141     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
142     if (grab_all)   
143         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
144     
145     gtk_widget_show_all(window);
146
147     if (grab_all) {
148         int err = 0, cnt = 0;
149         /* make sure that window is viewable */
150         gtk_widget_show_now(window);
151         gdkwin = gtk_widget_get_window(window);
152         gdk_window_process_updates(gdkwin, TRUE);
153         gdk_flush();
154         while(gtk_events_pending()) {
155                 gtk_main_iteration();
156         }
157 try_again:
158         if ((err = gdk_pointer_grab(gdkwin, TRUE, 0,
159                              gdkwin, NULL, GDK_CURRENT_TIME))) {
160             if (err == GDK_GRAB_NOT_VIEWABLE && cnt < 10) {
161                 cnt++;
162                 g_warning("trying to grab mouse again\n");
163                 gtk_main_iteration();
164                 goto try_again;
165             } else {
166                 g_warning("OOPS: Could not grab mouse\n");
167                 gtk_widget_destroy(window);
168                 return NULL;
169             }
170         }
171         if (gdk_keyboard_grab(gdkwin, FALSE, GDK_CURRENT_TIME)) {
172             gdk_display_pointer_ungrab(gdk_display_get_default(),
173                                        GDK_CURRENT_TIME);
174             g_warning("OOPS: Could not grab keyboard\n");
175             gtk_widget_destroy(window);
176             return NULL;
177         }
178     }
179
180     gtk_main();
181
182     if (grab_all) {
183         gdk_display_keyboard_ungrab(gdk_display_get_default(),
184                                     GDK_CURRENT_TIME);
185         gdk_display_pointer_ungrab(gdk_display_get_default(), GDK_CURRENT_TIME);
186         gdk_flush();
187     }
188
189     manage_window_focus_out(window, NULL, NULL);
190
191     if (pass_ack) {
192         const gchar *entry_text;
193         entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry));
194         the_passphrase = g_locale_from_utf8(entry_text, -1, NULL, NULL, NULL);
195         if (the_passphrase == NULL) 
196             the_passphrase = g_strdup (entry_text);
197     }
198     gtk_widget_destroy (window);
199
200     return the_passphrase;
201 }
202
203
204 static void 
205 passphrase_ok_cb(GtkWidget *widget, gpointer data)
206 {
207     pass_ack = TRUE;
208     gtk_main_quit();
209 }
210
211 static void 
212 passphrase_cancel_cb(GtkWidget *widget, gpointer data)
213 {
214     pass_ack = FALSE;
215     gtk_main_quit();
216 }
217
218
219 static gint
220 passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
221 {
222     passphrase_cancel_cb(NULL, NULL);
223     return TRUE;
224 }
225
226
227 static gboolean
228 passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
229 {
230     if (event && event->keyval == GDK_KEY_Escape)
231         passphrase_cancel_cb(NULL, NULL);
232     return FALSE;
233 }
234
235 static gint 
236 linelen (const gchar *s)
237 {
238     gint i;
239
240     for (i = 0; *s && *s != '\n'; s++, i++)
241         ;
242
243     return i;
244 }
245
246 static GtkWidget *
247 create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gint new_key)
248 {
249     const gchar *uid = NULL, *info = NULL;
250     gchar *buf;
251     GtkWidget *label;
252     gchar *my_uid = NULL;
253     if (!uid_hint)
254         uid = _("[no user id]");
255     else
256         uid = uid_hint;
257     if (!pass_hint)
258         info = "";
259     else
260         info = pass_hint;
261
262     my_uid = g_strdup(uid);
263     while (strchr(my_uid, '<')) 
264         *(strchr(my_uid, '<')) = '(';
265     while (strchr(my_uid, '>')) 
266         *(strchr(my_uid, '>')) = ')';
267
268     if (new_key == 1) {
269             buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">%sPlease enter the passphrase for the new key:</span>\n\n"
270                            "%.*s\n"),
271                            prev_bad ?
272                            _("Passphrases did not match.\n") : "",
273                            linelen (my_uid), my_uid);
274     } else if (new_key == 2) {
275             buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">Please re-enter the passphrase for the new key:</span>\n\n"
276                            "%.*s\n"),
277                            linelen (my_uid), my_uid);
278     } else {
279             buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">%sPlease enter the passphrase for:</span>\n\n"
280                            "%.*s\n"),
281                            prev_bad ?
282                            _("Bad passphrase.\n") : "",
283                            linelen (my_uid), my_uid);
284     }
285     g_free(my_uid);
286     label = gtk_label_new (buf);
287     gtk_label_set_use_markup(GTK_LABEL (label), TRUE);
288     gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
289     gtk_label_set_line_wrap(GTK_LABEL (label), TRUE);
290     g_free (buf);
291
292     return label;
293 }
294
295 static int free_passphrase(gpointer _unused)
296 {
297     if (last_pass != NULL) {
298 #ifndef G_PLATFORM_WIN32
299         munlock(last_pass, strlen(last_pass));
300 #endif
301         g_free(last_pass);
302         last_pass = NULL;
303         debug_print("%% passphrase removed\n");
304     }
305     
306     return FALSE;
307 }
308
309 gpgme_error_t
310 gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
311         const char *passphrase_hint, int prev_bad, int fd)
312 {
313     char *pass = NULL;
314
315     if (prefs_gpg_get_config()->store_passphrase && last_pass && !prev_bad)
316         pass = g_strdup(last_pass);
317     else {
318         gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
319         debug_print ("%% requesting passphrase for '%s'\n ", uid_hint);
320         pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad, FALSE);
321         gpgmegtk_free_passphrase();
322         if (!pass) {
323             debug_print ("%% cancel passphrase entry\n");
324             if (write(fd, "\n", 1) != 1)
325                 debug_print("short write");
326
327             return GPG_ERR_CANCELED;
328         }
329         else {
330             if (prefs_gpg_get_config()->store_passphrase) {
331                 last_pass = g_strdup(pass);
332 #ifndef G_PLATFORM_WIN32
333                 if (mlock(last_pass, strlen(last_pass)) == -1)
334                     debug_print("%% locking passphrase failed\n");
335 #endif
336                 if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
337                         g_timeout_add(prefs_gpg_get_config()
338                                       ->store_passphrase_timeout*60*1000,
339                                       free_passphrase, NULL);
340                 }
341             }
342             debug_print ("%% sending passphrase\n");
343         }
344     }
345
346 #ifdef G_OS_WIN32
347     {
348         /* Under Windows FD is actually a System handle. */
349         DWORD nwritten;
350         WriteFile ((HANDLE)fd, pass, strlen (pass), &nwritten, NULL);
351         WriteFile ((HANDLE)fd, "\n", 1, &nwritten, NULL);
352     }
353 #else
354     if (write(fd, pass, strlen(pass)) != strlen(pass))
355         debug_print("Short write");
356
357     if (write(fd, "\n", 1) != 1)
358         debug_print("Short write");
359 #endif
360     g_free(pass);
361
362     return GPG_ERR_NO_ERROR;
363 }
364
365 void gpgmegtk_free_passphrase()
366 {
367     (void)free_passphrase(NULL); /* could be inline */
368 }
369
370 #endif /* USE_GPGME */