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