This commit was manufactured by cvs2svn to create branch 'gtk2'.
[claws.git] / src / plugins / pgpmime / 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 <string.h>
26 #include <sys/types.h>
27 #include <sys/mman.h>
28 #include <glib.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gdk/gdkx.h>  /* GDK_DISPLAY() */
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkwidget.h>
33 #include <gtk/gtkwindow.h>
34 #include <gtk/gtkvbox.h>
35 #include <gtk/gtktable.h>
36 #include <gtk/gtklabel.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkhbbox.h>
39 #include <gtk/gtkbutton.h>
40 #include <gtk/gtkfilesel.h>
41 #include <gtk/gtksignal.h>
42
43 #include "intl.h"
44 #include "passphrase.h"
45 #include "prefs_common.h"
46 #include "manage_window.h"
47 #include "utils.h"
48 #include "prefs_gpg.h"
49
50 extern struct GPGConfig prefs_gpg;
51
52 static int grab_all = 0;
53
54 static gboolean pass_ack;
55 static gchar *last_pass = NULL;
56
57 static void passphrase_ok_cb(GtkWidget *widget, gpointer data);
58 static void passphrase_cancel_cb(GtkWidget *widget, gpointer data);
59 static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
60                                gpointer data);
61 static void passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
62                                    gpointer data);
63 static gchar* passphrase_mbox (const gchar *desc);
64
65
66 static GtkWidget *create_description (const gchar *desc);
67
68 void
69 gpgmegtk_set_passphrase_grab (gint yes)
70 {
71     grab_all = yes;
72 }
73
74 static gchar*
75 passphrase_mbox (const gchar *desc)
76 {
77     gchar *the_passphrase = NULL;
78     GtkWidget *vbox;
79     GtkWidget *table;
80     GtkWidget *pass_label;
81     GtkWidget *confirm_box;
82     GtkWidget *window;
83     GtkWidget *pass_entry;
84     GtkWidget *ok_button;
85     GtkWidget *cancel_button;
86
87     window = gtk_window_new(GTK_WINDOW_DIALOG);
88     gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
89     gtk_widget_set_usize(window, 450, -1);
90     gtk_container_set_border_width(GTK_CONTAINER(window), 4);
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     gtk_signal_connect(GTK_OBJECT(window), "delete_event",
95                        GTK_SIGNAL_FUNC(passphrase_deleted), NULL);
96     gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
97                        GTK_SIGNAL_FUNC(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
104     if (desc) {
105         GtkWidget *label;
106         label = create_description (desc);
107         gtk_box_pack_start (GTK_BOX(vbox), label, TRUE, TRUE, 0);
108     }
109
110     table = gtk_table_new(2, 2, FALSE);
111     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
112     gtk_container_set_border_width(GTK_CONTAINER(table), 8);
113     gtk_table_set_row_spacings(GTK_TABLE(table), 12);
114     gtk_table_set_col_spacings(GTK_TABLE(table), 8);
115
116
117     pass_label = gtk_label_new("");
118     gtk_table_attach (GTK_TABLE(table), pass_label, 0, 1, 0, 1,
119                       GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
120     gtk_misc_set_alignment (GTK_MISC (pass_label), 1, 0.5);
121
122     pass_entry = gtk_entry_new();
123     gtk_table_attach (GTK_TABLE(table), pass_entry, 1, 2, 0, 1,
124                       GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
125     gtk_entry_set_visibility (GTK_ENTRY(pass_entry), FALSE);
126     gtk_widget_grab_focus (pass_entry);
127
128
129     confirm_box = gtk_hbutton_box_new ();
130     gtk_button_box_set_layout (GTK_BUTTON_BOX(confirm_box), GTK_BUTTONBOX_END);
131     gtk_button_box_set_spacing (GTK_BUTTON_BOX(confirm_box), 5);
132
133     ok_button = gtk_button_new_with_label (_("OK"));
134     GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
135     gtk_box_pack_start (GTK_BOX(confirm_box), ok_button, TRUE, TRUE, 0);
136
137     cancel_button = gtk_button_new_with_label (_("Cancel"));
138     GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
139     gtk_box_pack_start(GTK_BOX(confirm_box), cancel_button, TRUE, TRUE, 0);
140
141     gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
142     gtk_widget_grab_default (ok_button);
143
144     gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
145                        GTK_SIGNAL_FUNC(passphrase_ok_cb), NULL);
146     gtk_signal_connect(GTK_OBJECT(pass_entry), "activate",
147                        GTK_SIGNAL_FUNC(passphrase_ok_cb), NULL);
148     gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
149                        GTK_SIGNAL_FUNC(passphrase_cancel_cb), NULL);
150
151     if (grab_all)
152         gtk_object_set (GTK_OBJECT(window), "type", GTK_WINDOW_POPUP, NULL);
153     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
154     if (grab_all)   
155         gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
156     
157     gtk_widget_show_all(window);
158
159     /* don't use XIM on entering passphrase */
160     gtkut_editable_disable_im(GTK_EDITABLE(pass_entry));
161
162     if (grab_all) {
163         XGrabServer(GDK_DISPLAY());
164         if ( gdk_pointer_grab ( window->window, TRUE, 0,
165                                 NULL, NULL, GDK_CURRENT_TIME)) {
166             XUngrabServer ( GDK_DISPLAY() );
167             g_warning ("OOPS: Could not grab mouse\n");
168             gtk_widget_destroy (window);
169             return NULL;
170         }
171         if ( gdk_keyboard_grab( window->window, FALSE, GDK_CURRENT_TIME )) {
172             gdk_pointer_ungrab (GDK_CURRENT_TIME);
173             XUngrabServer ( GDK_DISPLAY() );
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         XUngrabServer (GDK_DISPLAY());
184         gdk_pointer_ungrab (GDK_CURRENT_TIME);
185         gdk_keyboard_ungrab (GDK_CURRENT_TIME);
186         gdk_flush();
187     }
188
189     manage_window_focus_out(window, NULL, NULL);
190
191     if (pass_ack) {
192         the_passphrase = gtk_entry_get_text(GTK_ENTRY(pass_entry));
193         if (the_passphrase) /* Hmmm: Do we really need this? */
194             the_passphrase = g_strdup (the_passphrase);
195     }
196     gtk_widget_destroy (window);
197
198     return the_passphrase;
199 }
200
201
202 static void 
203 passphrase_ok_cb(GtkWidget *widget, gpointer data)
204 {
205     pass_ack = TRUE;
206     gtk_main_quit();
207 }
208
209 static void 
210 passphrase_cancel_cb(GtkWidget *widget, gpointer data)
211 {
212     pass_ack = FALSE;
213     gtk_main_quit();
214 }
215
216
217 static gint
218 passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
219 {
220     passphrase_cancel_cb(NULL, NULL);
221     return TRUE;
222 }
223
224
225 static void 
226 passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
227 {
228     if (event && event->keyval == GDK_Escape)
229         passphrase_cancel_cb(NULL, NULL);
230 }
231
232 static gint 
233 linelen (const gchar *s)
234 {
235     gint i;
236
237     for (i = 0; *s && *s != '\n'; s++, i++)
238         ;
239
240     return i;
241 }
242
243 static GtkWidget *
244 create_description (const gchar *desc)
245 {
246     const gchar *cmd = NULL, *uid = NULL, *info = NULL;
247     gchar *buf;
248     GtkWidget *label;
249
250     cmd = desc;
251     uid = strchr (cmd, '\n');
252     if (uid) {
253         info = strchr (++uid, '\n');
254         if (info )
255             info++;
256     }
257
258     if (!uid)
259         uid = _("[no user id]");
260     if (!info)
261         info = "";
262
263     buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
264                            "  %.*s  \n"
265                            "(%.*s)\n"),
266                            !strncmp (cmd, "TRY_AGAIN", 9 ) ?
267                            _("Bad passphrase! Try again...\n\n") : "",
268                            linelen (uid), uid, linelen (info), info);
269
270     label = gtk_label_new (buf);
271     g_free (buf);
272
273     return label;
274 }
275
276 static int free_passphrase(gpointer _unused)
277 {
278     if (last_pass != NULL) {
279         munlock(last_pass, strlen(last_pass));
280         g_free(last_pass);
281         last_pass = NULL;
282         debug_print("%% passphrase removed");
283     }
284     
285     return FALSE;
286 }
287
288 const char*
289 gpgmegtk_passphrase_cb (void *opaque, const char *desc, void **r_hd)
290 {
291     struct passphrase_cb_info_s *info = opaque;
292     GpgmeCtx ctx = info ? info->c : NULL;
293     const char *pass;
294
295     if (!desc) {
296         /* FIXME: cleanup by looking at *r_hd */
297         return NULL;
298     }
299     if (prefs_gpg.store_passphrase && last_pass != NULL &&
300         strncmp(desc, "TRY_AGAIN", 9) != 0)
301         return g_strdup(last_pass);
302
303     gpgmegtk_set_passphrase_grab (prefs_gpg.passphrase_grab);
304     debug_print ("%% requesting passphrase for `%s': ", desc);
305     pass = passphrase_mbox (desc);
306     gpgmegtk_free_passphrase();
307     if (!pass) {
308         debug_print ("%% cancel passphrase entry");
309         gpgme_cancel (ctx);
310     }
311     else {
312         if (prefs_gpg.store_passphrase) {
313             last_pass = g_strdup(pass);
314             if (mlock(last_pass, strlen(last_pass)) == -1)
315                 debug_print("%% locking passphrase failed");
316
317             if (prefs_gpg.store_passphrase_timeout > 0) {
318                 gtk_timeout_add(prefs_gpg.store_passphrase_timeout*60*1000,
319                                 free_passphrase, NULL);
320             }
321         }
322         debug_print ("%% sending passphrase");
323     }
324
325     return pass;
326 }
327
328 void gpgmegtk_free_passphrase(void)
329 {
330     (void)free_passphrase(NULL); /* could be inline */
331 }
332
333 #endif /* USE_GPGME */