6756af18d797390742bdc657ed4460cba2b0639a
[claws.git] / src / plugins / pgpcore / passphrase.c
1 /* passphrase.c - GTK+ based passphrase callback
2  *      Copyright (C) 2001 Werner Koch (dd9jn)
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 #include <sys/mman.h>
47
48 #include "passphrase.h"
49 #include "prefs_common.h"
50 #include "prefs_gpg.h"
51 #include "manage_window.h"
52 #include "utils.h"
53 #include "mainwindow.h"
54 #include "summaryview.h"
55
56 static gboolean grab_all = FALSE;
57
58 static gboolean pass_ack;
59 static gchar *last_pass = NULL;
60
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,
64                                gpointer data);
65 static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
66                                        gpointer data);
67 static gchar* passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint,
68                               gint prev_bad);
69
70 static GtkWidget *create_description(const gchar *uid_hint,
71                                      const gchar *pass_hint, gint prev_bad);
72
73 void
74 gpgmegtk_set_passphrase_grab(gint yes)
75 {
76     grab_all = yes;
77 }
78
79 static gchar*
80 passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
81 {
82     gchar *the_passphrase = NULL;
83     GtkWidget *vbox, *hbox;
84     GtkWidget *confirm_box;
85     GtkWidget *window;
86     GtkWidget *pass_entry;
87     GtkWidget *ok_button;
88     GtkWidget *cancel_button;
89
90     SummaryView *summaryview = mainwindow_get_mainwindow()->summaryview;
91     
92     gtk_menu_popdown(GTK_MENU(summaryview->popupmenu));
93
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));
106
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);
110
111     if (uid_hint || pass_hint) {
112         GtkWidget *label, *icon;
113         label = create_description (uid_hint, pass_hint, prev_bad);
114         icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
115                                 GTK_ICON_SIZE_DIALOG); 
116
117         hbox = gtk_hbox_new (FALSE, 12);
118         gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
119         gtk_widget_show (hbox);
120         gtk_box_pack_start (GTK_BOX(hbox), icon, FALSE, FALSE, 0);
121         gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
122         gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
123     }
124
125     pass_entry = gtk_entry_new();
126     gtk_box_pack_start(GTK_BOX(vbox), pass_entry, FALSE, FALSE, 0);
127     gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE);
128     gtk_widget_grab_focus(pass_entry);
129
130     gtkut_stock_button_set_create(&confirm_box, &ok_button, GTK_STOCK_OK,
131                                   &cancel_button, GTK_STOCK_CANCEL,
132                                   NULL, NULL);
133     gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
134     gtk_widget_grab_default(ok_button);
135
136     g_signal_connect(G_OBJECT(ok_button), "clicked",
137                      G_CALLBACK(passphrase_ok_cb), NULL);
138     g_signal_connect(G_OBJECT(pass_entry), "activate",
139                      G_CALLBACK(passphrase_ok_cb), NULL);
140     g_signal_connect(G_OBJECT(cancel_button), "clicked",
141                      G_CALLBACK(passphrase_cancel_cb), NULL);
142
143     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
144     if (grab_all)   
145         gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
146     
147     gtk_widget_show_all(window);
148
149     if (grab_all) {
150         int err = 0, cnt = 0;
151         /* make sure that window is viewable */
152         gtk_widget_show_now(window);
153         gdk_flush();
154         while(gtk_events_pending())
155                 gtk_main_iteration();
156 #ifdef GDK_WINDOWING_X11
157         gdk_x11_display_grab(gdk_display_get_default());
158 #endif /* GDK_WINDOWING_X11 */
159 try_again:
160         if ((err = gdk_pointer_grab(window->window, TRUE, 0,
161                              window->window, NULL, GDK_CURRENT_TIME))) {
162             if (err == GDK_GRAB_NOT_VIEWABLE && cnt < 10) {
163                 cnt++;
164                 g_warning("trying to grab mouse again\n");
165                 goto try_again;
166             } else {
167 #ifdef GDK_WINDOWING_X11
168                 gdk_x11_display_ungrab(gdk_display_get_default());
169 #endif /* GDK_WINDOWING_X11 */
170                 g_warning("OOPS: Could not grab mouse\n");
171                 gtk_widget_destroy(window);
172                 return NULL;
173             }
174         }
175         if (gdk_keyboard_grab(window->window, FALSE, GDK_CURRENT_TIME)) {
176             gdk_display_pointer_ungrab(gdk_display_get_default(),
177                                        GDK_CURRENT_TIME);
178 #ifdef GDK_WINDOWING_X11
179             gdk_x11_display_ungrab(gdk_display_get_default());
180 #endif /* GDK_WINDOWING_X11 */
181             g_warning("OOPS: Could not grab keyboard\n");
182             gtk_widget_destroy(window);
183             return NULL;
184         }
185     }
186
187     gtk_main();
188
189     if (grab_all) {
190         gdk_display_keyboard_ungrab(gdk_display_get_default(),
191                                     GDK_CURRENT_TIME);
192         gdk_display_pointer_ungrab(gdk_display_get_default(), GDK_CURRENT_TIME);
193 #ifdef GDK_WINDOWING_X11
194         gdk_x11_display_ungrab(gdk_display_get_default());
195 #endif /* GDK_WINDOWING_X11 */
196         gdk_flush();
197     }
198
199     manage_window_focus_out(window, NULL, NULL);
200
201     if (pass_ack) {
202         const gchar *entry_text;
203         entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry));
204         if (entry_text) /* Hmmm: Do we really need this? */
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)
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     buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">%sPlease enter the passphrase for:</span>\n\n"
278                            "%.*s\n"),
279                            prev_bad ?
280                            _("Bad passphrase.\n") : "",
281                            linelen (my_uid), my_uid);
282     g_free(my_uid);
283     label = gtk_label_new (buf);
284     gtk_label_set_use_markup(GTK_LABEL (label), TRUE);
285     gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
286     g_free (buf);
287
288     return label;
289 }
290
291 static int free_passphrase(gpointer _unused)
292 {
293     if (last_pass != NULL) {
294         munlock(last_pass, strlen(last_pass));
295         g_free(last_pass);
296         last_pass = NULL;
297         debug_print("%% passphrase removed");
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 != NULL && !prev_bad) {
310         write(fd, last_pass, strlen(last_pass));
311         write(fd, "\n", 1);
312         return GPG_ERR_NO_ERROR;
313     }
314     gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
315     debug_print ("%% requesting passphrase for '%s': ", uid_hint);
316     pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad);
317     gpgmegtk_free_passphrase();
318     if (!pass) {
319         debug_print ("%% cancel passphrase entry");
320         write(fd, "\n", 1);
321         return GPG_ERR_CANCELED;
322     }
323     else {
324         if (prefs_gpg_get_config()->store_passphrase) {
325             last_pass = g_strdup(pass);
326             if (mlock(last_pass, strlen(last_pass)) == -1)
327                 debug_print("%% locking passphrase failed");
328
329             if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
330                 gtk_timeout_add(prefs_gpg_get_config()->store_passphrase_timeout*60*1000,
331                                 free_passphrase, NULL);
332             }
333         }
334         debug_print ("%% sending passphrase");
335     }
336     write(fd, pass, strlen(pass));
337     write(fd, "\n", 1);
338     return GPG_ERR_NO_ERROR;
339 }
340
341 void gpgmegtk_free_passphrase()
342 {
343     (void)free_passphrase(NULL); /* could be inline */
344 }
345
346 #endif /* USE_GPGME */