913b0ba7d97e5f56b67634435308246228b8c6fb
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
54 static gboolean grab_all = FALSE;
55
56 static gboolean pass_ack;
57 static gchar *last_pass = NULL;
58
59 static void passphrase_ok_cb(GtkWidget *widget, gpointer data);
60 static void passphrase_cancel_cb(GtkWidget *widget, gpointer data);
61 static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
62                                gpointer data);
63 static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
64                                        gpointer data);
65 static gchar* passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint,
66                               gint prev_bad);
67
68 static GtkWidget *create_description(const gchar *uid_hint,
69                                      const gchar *pass_hint, gint prev_bad);
70
71 void
72 gpgmegtk_set_passphrase_grab(gint yes)
73 {
74     grab_all = yes;
75 }
76
77 static gchar*
78 passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
79 {
80     gchar *the_passphrase = NULL;
81     GtkWidget *vbox;
82     GtkWidget *confirm_box;
83     GtkWidget *window;
84     GtkWidget *pass_entry;
85     GtkWidget *ok_button;
86     GtkWidget *cancel_button;
87
88     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
89     gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
90     gtk_widget_set_size_request(window, 450, -1);
91     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
92     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
93     gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
94     g_signal_connect(G_OBJECT(window), "delete_event",
95                      G_CALLBACK(passphrase_deleted), NULL);
96     g_signal_connect(G_OBJECT(window), "key_press_event",
97                      G_CALLBACK(passphrase_key_pressed), NULL);
98     MANAGE_WINDOW_SIGNALS_CONNECT(window);
99     manage_window_set_transient(GTK_WINDOW(window));
100
101     vbox = gtk_vbox_new(FALSE, 8);
102     gtk_container_add(GTK_CONTAINER(window), vbox);
103     gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
104
105     if (uid_hint || pass_hint) {
106         GtkWidget *label;
107         label = create_description (uid_hint, pass_hint, prev_bad);
108         gtk_box_pack_start (GTK_BOX(vbox), label, FALSE, FALSE, 0);
109     }
110
111     pass_entry = gtk_entry_new();
112     gtk_box_pack_start(GTK_BOX(vbox), pass_entry, FALSE, FALSE, 0);
113     gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE);
114     gtk_widget_grab_focus(pass_entry);
115
116     gtkut_stock_button_set_create(&confirm_box, &ok_button, GTK_STOCK_OK,
117                                   &cancel_button, GTK_STOCK_CANCEL,
118                                   NULL, NULL);
119     gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
120     gtk_widget_grab_default(ok_button);
121
122     g_signal_connect(G_OBJECT(ok_button), "clicked",
123                      G_CALLBACK(passphrase_ok_cb), NULL);
124     g_signal_connect(G_OBJECT(pass_entry), "activate",
125                      G_CALLBACK(passphrase_ok_cb), NULL);
126     g_signal_connect(G_OBJECT(cancel_button), "clicked",
127                      G_CALLBACK(passphrase_cancel_cb), NULL);
128
129     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
130     if (grab_all)   
131         gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
132     
133     gtk_widget_show_all(window);
134
135     if (grab_all) {
136         int err = 0, cnt = 0;
137         /* make sure that window is viewable */
138         gtk_widget_show_now(window);
139         gdk_flush();
140         while(gtk_events_pending())
141                 gtk_main_iteration();
142 #ifdef GDK_WINDOWING_X11
143         gdk_x11_display_grab(gdk_display_get_default());
144 #endif /* GDK_WINDOWING_X11 */
145 try_again:
146         if ((err = gdk_pointer_grab(window->window, TRUE, 0,
147                              window->window, NULL, GDK_CURRENT_TIME))) {
148             if (err == GDK_GRAB_NOT_VIEWABLE && cnt < 10) {
149                 cnt++;
150                 g_warning("trying to grab mouse again\n");
151                 goto try_again;
152             } else {
153 #ifdef GDK_WINDOWING_X11
154                 gdk_x11_display_ungrab(gdk_display_get_default());
155 #endif /* GDK_WINDOWING_X11 */
156                 g_warning("OOPS: Could not grab mouse\n");
157                 gtk_widget_destroy(window);
158                 return NULL;
159             }
160         }
161         if (gdk_keyboard_grab(window->window, FALSE, GDK_CURRENT_TIME)) {
162             gdk_display_pointer_ungrab(gdk_display_get_default(),
163                                        GDK_CURRENT_TIME);
164 #ifdef GDK_WINDOWING_X11
165             gdk_x11_display_ungrab(gdk_display_get_default());
166 #endif /* GDK_WINDOWING_X11 */
167             g_warning("OOPS: Could not grab keyboard\n");
168             gtk_widget_destroy(window);
169             return NULL;
170         }
171     }
172
173     gtk_main();
174
175     if (grab_all) {
176         gdk_display_keyboard_ungrab(gdk_display_get_default(),
177                                     GDK_CURRENT_TIME);
178         gdk_display_pointer_ungrab(gdk_display_get_default(), GDK_CURRENT_TIME);
179 #ifdef GDK_WINDOWING_X11
180         gdk_x11_display_ungrab(gdk_display_get_default());
181 #endif /* GDK_WINDOWING_X11 */
182         gdk_flush();
183     }
184
185     manage_window_focus_out(window, NULL, NULL);
186
187     if (pass_ack) {
188         const gchar *entry_text;
189         entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry));
190         if (entry_text) /* Hmmm: Do we really need this? */
191             the_passphrase = g_strdup (entry_text);
192     }
193     gtk_widget_destroy (window);
194
195     return the_passphrase;
196 }
197
198
199 static void 
200 passphrase_ok_cb(GtkWidget *widget, gpointer data)
201 {
202     pass_ack = TRUE;
203     gtk_main_quit();
204 }
205
206 static void 
207 passphrase_cancel_cb(GtkWidget *widget, gpointer data)
208 {
209     pass_ack = FALSE;
210     gtk_main_quit();
211 }
212
213
214 static gint
215 passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
216 {
217     passphrase_cancel_cb(NULL, NULL);
218     return TRUE;
219 }
220
221
222 static gboolean
223 passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
224 {
225     if (event && event->keyval == GDK_Escape)
226         passphrase_cancel_cb(NULL, NULL);
227     return FALSE;
228 }
229
230 static gint 
231 linelen (const gchar *s)
232 {
233     gint i;
234
235     for (i = 0; *s && *s != '\n'; s++, i++)
236         ;
237
238     return i;
239 }
240
241 static GtkWidget *
242 create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
243 {
244     const gchar *uid = NULL, *info = NULL;
245     gchar *buf;
246     GtkWidget *label;
247
248     if (!uid_hint)
249         uid = _("[no user id]");
250     else
251         uid = uid_hint;
252     if (!pass_hint)
253         info = "";
254     else
255         info = pass_hint;
256
257     buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
258                            "  %.*s  \n"
259                            "(%.*s)\n"),
260                            prev_bad ?
261                            _("Bad passphrase! Try again...\n\n") : "",
262                            linelen (uid), uid, linelen (info), info);
263
264     label = gtk_label_new (buf);
265     gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
266     g_free (buf);
267
268     return label;
269 }
270
271 static int free_passphrase(gpointer _unused)
272 {
273     if (last_pass != NULL) {
274         munlock(last_pass, strlen(last_pass));
275         g_free(last_pass);
276         last_pass = NULL;
277         debug_print("%% passphrase removed");
278     }
279     
280     return FALSE;
281 }
282
283 gpgme_error_t
284 gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
285         const char *passphrase_hint, int prev_bad, int fd)
286 {
287     const char *pass;
288
289     if (prefs_gpg_get_config()->store_passphrase && last_pass != NULL && !prev_bad) {
290         write(fd, last_pass, strlen(last_pass));
291         write(fd, "\n", 1);
292         return GPG_ERR_NO_ERROR;
293     }
294     gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
295     debug_print ("%% requesting passphrase for '%s': ", uid_hint);
296     pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad);
297     gpgmegtk_free_passphrase();
298     if (!pass) {
299         debug_print ("%% cancel passphrase entry");
300         write(fd, "\n", 1);
301         return GPG_ERR_CANCELED;
302     }
303     else {
304         if (prefs_gpg_get_config()->store_passphrase) {
305             last_pass = g_strdup(pass);
306             if (mlock(last_pass, strlen(last_pass)) == -1)
307                 debug_print("%% locking passphrase failed");
308
309             if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
310                 gtk_timeout_add(prefs_gpg_get_config()->store_passphrase_timeout*60*1000,
311                                 free_passphrase, NULL);
312             }
313         }
314         debug_print ("%% sending passphrase");
315     }
316     write(fd, pass, strlen(pass));
317     write(fd, "\n", 1);
318     return GPG_ERR_NO_ERROR;
319 }
320
321 void gpgmegtk_free_passphrase()
322 {
323     (void)free_passphrase(NULL); // could be inline
324 }
325
326 #endif /* USE_GPGME */