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