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