2005-02-24 [colin] 1.0.1cvs15.11
[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 <glib/gi18n.h>
30 #include <gdk/gdkkeysyms.h>
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 #ifdef GDK_WINDOWING_X11
43 #include <gdk/gdkx.h>  /* GDK_DISPLAY() */
44 #endif /* GDK_WINDOWING_X11 */
45
46 #include "passphrase.h"
47 #include "prefs_common.h"
48 #include "manage_window.h"
49 #include "utils.h"
50 #include "prefs_gpg.h"
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 gboolean 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 #warning "passphrase grab does not work"        
72 #if 0
73     grab_all = yes;
74 #else
75     grab_all = FALSE;
76 #endif
77 }
78
79 static gchar*
80 passphrase_mbox (const gchar *desc)
81 {
82     gchar *the_passphrase = NULL;
83     GtkWidget *vbox;
84     GtkWidget *confirm_box;
85     GtkWidget *window;
86     GtkWidget *pass_entry;
87     GtkWidget *ok_button;
88     GtkWidget *cancel_button;
89     gint       grab_result;
90
91     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
92     gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
93     gtk_widget_set_size_request(window, 450, -1);
94     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
95     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
96     gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
97     g_signal_connect(G_OBJECT(window), "delete_event",
98                      G_CALLBACK(passphrase_deleted), NULL);
99     g_signal_connect(G_OBJECT(window), "key_press_event",
100                      G_CALLBACK(passphrase_key_pressed), NULL);
101     MANAGE_WINDOW_SIGNALS_CONNECT(window);
102     manage_window_set_transient(GTK_WINDOW(window));
103
104     vbox = gtk_vbox_new(FALSE, 8);
105     gtk_container_add(GTK_CONTAINER(window), vbox);
106     gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
107
108     if (desc) {
109         GtkWidget *label;
110         label = create_description (desc);
111         gtk_box_pack_start (GTK_BOX(vbox), label, FALSE, FALSE, 0);
112     }
113
114     pass_entry = gtk_entry_new();
115     gtk_box_pack_start(GTK_BOX(vbox), pass_entry, FALSE, FALSE, 0);
116     gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE);
117     gtk_widget_grab_focus(pass_entry);
118
119     gtkut_stock_button_set_create(&confirm_box, &ok_button, GTK_STOCK_OK,
120                                   &cancel_button, GTK_STOCK_CANCEL,
121                                   NULL, NULL);
122     gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
123     gtk_widget_grab_default(ok_button);
124
125     g_signal_connect(G_OBJECT(ok_button), "clicked",
126                      G_CALLBACK(passphrase_ok_cb), NULL);
127     g_signal_connect(G_OBJECT(pass_entry), "activate",
128                      G_CALLBACK(passphrase_ok_cb), NULL);
129     g_signal_connect(G_OBJECT(cancel_button), "clicked",
130                      G_CALLBACK(passphrase_cancel_cb), NULL);
131
132     if (grab_all)
133         g_object_set (G_OBJECT(window), "type", GTK_WINDOW_POPUP, NULL);
134     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
135     if (grab_all)   
136         gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
137     
138     gtk_widget_show_all(window);
139
140     /* don't use XIM on entering passphrase */
141     gtkut_editable_disable_im(GTK_EDITABLE(pass_entry));
142
143     if (grab_all) {
144 #ifdef GDK_WINDOWING_X11
145         XGrabServer(GDK_DISPLAY());
146 #endif /* GDK_WINDOWING_X11 */
147         if ( grab_result = gdk_pointer_grab ( window->window, TRUE, 0,
148                                 NULL, NULL, GDK_CURRENT_TIME)) {
149 #ifdef GDK_WINDOWING_X11
150             XUngrabServer ( GDK_DISPLAY() );
151 #endif /* GDK_WINDOWING_X11 */
152             g_warning ("OOPS: Could not grab mouse (grab status %d)\n",
153                         grab_result);
154             gtk_widget_destroy (window);
155             return NULL;
156         }
157         if ( grab_result = gdk_keyboard_grab( window->window, FALSE, 
158                                                 GDK_CURRENT_TIME )) {
159             gdk_pointer_ungrab (GDK_CURRENT_TIME);
160 #ifdef GDK_WINDOWING_X11
161             XUngrabServer ( GDK_DISPLAY() );
162 #endif /* GDK_WINDOWING_X11 */
163             g_warning ("OOPS: Could not grab keyboard (grab status %d)\n",
164                         grab_result);
165             gtk_widget_destroy (window);
166             return NULL;
167         }
168     }
169
170     gtk_main();
171
172     if (grab_all) {
173 #ifdef GDK_WINDOWING_X11
174         XUngrabServer (GDK_DISPLAY());
175 #endif /* GDK_WINDOWING_X11 */
176         gdk_pointer_ungrab (GDK_CURRENT_TIME);
177         gdk_keyboard_ungrab (GDK_CURRENT_TIME);
178         gdk_flush();
179     }
180
181     manage_window_focus_out(window, NULL, NULL);
182
183     if (pass_ack) {
184         const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry));
185         if (entry_text) /* Hmmm: Do we really need this? */
186             the_passphrase = g_strdup (entry_text);
187     }
188     gtk_widget_destroy (window);
189
190     return the_passphrase;
191 }
192
193
194 static void 
195 passphrase_ok_cb(GtkWidget *widget, gpointer data)
196 {
197     pass_ack = TRUE;
198     gtk_main_quit();
199 }
200
201 static void 
202 passphrase_cancel_cb(GtkWidget *widget, gpointer data)
203 {
204     pass_ack = FALSE;
205     gtk_main_quit();
206 }
207
208
209 static gint
210 passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
211 {
212     passphrase_cancel_cb(NULL, NULL);
213     return TRUE;
214 }
215
216
217 static gboolean
218 passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
219 {
220     if (event && event->keyval == GDK_Escape)
221         passphrase_cancel_cb(NULL, NULL);
222     return FALSE;
223 }
224
225 static gint 
226 linelen (const gchar *s)
227 {
228     gint i;
229
230     for (i = 0; *s && *s != '\n'; s++, i++)
231         ;
232
233     return i;
234 }
235
236 static GtkWidget *
237 create_description (const gchar *desc)
238 {
239     const gchar *cmd = NULL, *uid = NULL, *info = NULL;
240     gchar *buf;
241     GtkWidget *label;
242
243     cmd = desc;
244     uid = strchr (cmd, '\n');
245     if (uid) {
246         info = strchr (++uid, '\n');
247         if (info )
248             info++;
249     }
250
251     if (!uid)
252         uid = _("[no user id]");
253     if (!info)
254         info = "";
255
256     buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
257                            "  %.*s  \n"
258                            "(%.*s)\n"),
259                            !strncmp (cmd, "TRY_AGAIN", 9 ) ?
260                            _("Bad passphrase! Try again...\n\n") : "",
261                            linelen (uid), uid, linelen (info), info);
262
263     label = gtk_label_new (buf);
264     gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
265     g_free (buf);
266
267     return label;
268 }
269
270 static int free_passphrase(gpointer _unused)
271 {
272     if (last_pass != NULL) {
273         munlock(last_pass, strlen(last_pass));
274         g_free(last_pass);
275         last_pass = NULL;
276         debug_print("%% passphrase removed");
277     }
278     
279     return FALSE;
280 }
281
282 const char*
283 gpgmegtk_passphrase_cb (void *opaque, const char *desc, void **r_hd)
284 {
285     struct passphrase_cb_info_s *info = opaque;
286     GpgmeCtx ctx = info ? info->c : NULL;
287     const char *pass;
288
289     if (!desc) {
290         /* FIXME: cleanup by looking at *r_hd */
291         return NULL;
292     }
293     if (prefs_gpg_get_config()->store_passphrase && last_pass != NULL &&
294         strncmp(desc, "TRY_AGAIN", 9) != 0)
295         return g_strdup(last_pass);
296
297     gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
298     debug_print ("%% requesting passphrase for `%s': ", desc);
299     pass = passphrase_mbox (desc);
300     gpgmegtk_free_passphrase();
301     if (!pass) {
302         debug_print ("%% cancel passphrase entry");
303         gpgme_cancel (ctx);
304     }
305     else {
306         if (prefs_gpg_get_config()->store_passphrase) {
307             last_pass = g_strdup(pass);
308             if (mlock(last_pass, strlen(last_pass)) == -1)
309                 debug_print("%% locking passphrase failed");
310
311             if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
312                 gtk_timeout_add(prefs_gpg_get_config()->store_passphrase_timeout*60*1000,
313                                 free_passphrase, NULL);
314             }
315         }
316         debug_print ("%% sending passphrase");
317     }
318
319     return pass;
320 }
321
322 void gpgmegtk_free_passphrase(void)
323 {
324     (void)free_passphrase(NULL); /* could be inline */
325 }
326
327 #endif /* USE_GPGME */