Statistics
| Revision:

root / src / passphrase.c @ 1

History | View | Annotate | Download (9.9 kB)

1 1 hiro
/* passphrase.c - GTK+ based passphrase callback
2 1 hiro
 *      Copyright (C) 2001 Werner Koch (dd9jn)
3 1 hiro
 *
4 1 hiro
 * This program is free software; you can redistribute it and/or modify
5 1 hiro
 * it under the terms of the GNU General Public License as published by
6 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
7 1 hiro
 * (at your option) any later version.
8 1 hiro
 *
9 1 hiro
 * This program is distributed in the hope that it will be useful,
10 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 1 hiro
 * GNU General Public License for more details.
13 1 hiro
 *
14 1 hiro
 * You should have received a copy of the GNU General Public License
15 1 hiro
 * along with this program; if not, write to the Free Software
16 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 1 hiro
 */
18 1 hiro
19 1 hiro
#ifdef HAVE_CONFIG_H
20 1 hiro
#  include <config.h>
21 1 hiro
#endif
22 1 hiro
23 1 hiro
#if USE_GPGME
24 1 hiro
25 1 hiro
#include <string.h>
26 1 hiro
#include <sys/types.h>
27 1 hiro
#include <sys/mman.h>
28 1 hiro
#include <glib.h>
29 1 hiro
#include <gdk/gdkkeysyms.h>
30 1 hiro
#ifdef GDK_WINDOWING_X11
31 1 hiro
#  include <gdk/gdkx.h>  /* GDK_DISPLAY() */
32 1 hiro
#endif /* GDK_WINDOWING_X11 */
33 1 hiro
#include <gtk/gtkmain.h>
34 1 hiro
#include <gtk/gtkwidget.h>
35 1 hiro
#include <gtk/gtkwindow.h>
36 1 hiro
#include <gtk/gtkvbox.h>
37 1 hiro
#include <gtk/gtktable.h>
38 1 hiro
#include <gtk/gtklabel.h>
39 1 hiro
#include <gtk/gtkentry.h>
40 1 hiro
#include <gtk/gtkhbbox.h>
41 1 hiro
#include <gtk/gtkbutton.h>
42 1 hiro
#include <gtk/gtkfilesel.h>
43 1 hiro
#include <gtk/gtksignal.h>
44 1 hiro
45 1 hiro
#include "intl.h"
46 1 hiro
#include "passphrase.h"
47 1 hiro
#include "prefs_common.h"
48 1 hiro
#include "manage_window.h"
49 1 hiro
#include "utils.h"
50 1 hiro
51 1 hiro
static int grab_all = 0;
52 1 hiro
53 1 hiro
static gboolean pass_ack;
54 1 hiro
static gchar *last_pass = NULL;
55 1 hiro
56 1 hiro
static void passphrase_ok_cb(GtkWidget *widget, gpointer data);
57 1 hiro
static void passphrase_cancel_cb(GtkWidget *widget, gpointer data);
58 1 hiro
static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
59 1 hiro
                               gpointer data);
60 1 hiro
static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
61 1 hiro
                                       gpointer data);
62 1 hiro
static gchar* passphrase_mbox (const gchar *desc);
63 1 hiro
64 1 hiro
65 1 hiro
static GtkWidget *create_description (const gchar *desc);
66 1 hiro
67 1 hiro
void
68 1 hiro
gpgmegtk_set_passphrase_grab (gint yes)
69 1 hiro
{
70 1 hiro
    grab_all = yes;
71 1 hiro
}
72 1 hiro
73 1 hiro
static gchar*
74 1 hiro
passphrase_mbox (const gchar *desc)
75 1 hiro
{
76 1 hiro
    gchar *the_passphrase = NULL;
77 1 hiro
    GtkWidget *vbox;
78 1 hiro
    GtkWidget *table;
79 1 hiro
    GtkWidget *pass_label;
80 1 hiro
    GtkWidget *confirm_box;
81 1 hiro
    GtkWidget *window;
82 1 hiro
    GtkWidget *pass_entry;
83 1 hiro
    GtkWidget *ok_button;
84 1 hiro
    GtkWidget *cancel_button;
85 1 hiro
86 1 hiro
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
87 1 hiro
    gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
88 1 hiro
    gtk_widget_set_size_request(window, 450, -1);
89 1 hiro
    gtk_container_set_border_width(GTK_CONTAINER(window), 4);
90 1 hiro
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
91 1 hiro
    gtk_window_set_modal(GTK_WINDOW(window), TRUE);
92 1 hiro
    gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
93 1 hiro
    g_signal_connect(G_OBJECT(window), "delete_event",
94 1 hiro
                     G_CALLBACK(passphrase_deleted), NULL);
95 1 hiro
    g_signal_connect(G_OBJECT(window), "key_press_event",
96 1 hiro
                     G_CALLBACK(passphrase_key_pressed), NULL);
97 1 hiro
    MANAGE_WINDOW_SIGNALS_CONNECT(window);
98 1 hiro
    manage_window_set_transient(GTK_WINDOW(window));
99 1 hiro
100 1 hiro
    vbox = gtk_vbox_new(FALSE, 8);
101 1 hiro
    gtk_container_add(GTK_CONTAINER(window), vbox);
102 1 hiro
103 1 hiro
    if (desc) {
104 1 hiro
        GtkWidget *label;
105 1 hiro
        label = create_description (desc);
106 1 hiro
        gtk_box_pack_start (GTK_BOX(vbox), label, TRUE, TRUE, 0);
107 1 hiro
    }
108 1 hiro
109 1 hiro
    table = gtk_table_new(2, 2, FALSE);
110 1 hiro
    gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
111 1 hiro
    gtk_container_set_border_width(GTK_CONTAINER(table), 8);
112 1 hiro
    gtk_table_set_row_spacings(GTK_TABLE(table), 12);
113 1 hiro
    gtk_table_set_col_spacings(GTK_TABLE(table), 8);
114 1 hiro
115 1 hiro
116 1 hiro
    pass_label = gtk_label_new("");
117 1 hiro
    gtk_table_attach (GTK_TABLE(table), pass_label, 0, 1, 0, 1,
118 1 hiro
                      GTK_FILL, GTK_EXPAND|GTK_FILL, 0, 0);
119 1 hiro
    gtk_misc_set_alignment (GTK_MISC (pass_label), 1, 0.5);
120 1 hiro
121 1 hiro
    pass_entry = gtk_entry_new();
122 1 hiro
    gtk_table_attach (GTK_TABLE(table), pass_entry, 1, 2, 0, 1,
123 1 hiro
                      GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
124 1 hiro
    gtk_entry_set_visibility (GTK_ENTRY(pass_entry), FALSE);
125 1 hiro
    gtk_widget_grab_focus (pass_entry);
126 1 hiro
127 1 hiro
128 1 hiro
    confirm_box = gtk_hbutton_box_new ();
129 1 hiro
    gtk_button_box_set_layout (GTK_BUTTON_BOX(confirm_box), GTK_BUTTONBOX_END);
130 1 hiro
    gtk_box_set_spacing (GTK_BOX(confirm_box), 5);
131 1 hiro
132 1 hiro
    ok_button = gtk_button_new_with_label (_("OK"));
133 1 hiro
    GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
134 1 hiro
    gtk_box_pack_start (GTK_BOX(confirm_box), ok_button, TRUE, TRUE, 0);
135 1 hiro
136 1 hiro
    cancel_button = gtk_button_new_with_label (_("Cancel"));
137 1 hiro
    GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
138 1 hiro
    gtk_box_pack_start(GTK_BOX(confirm_box), cancel_button, TRUE, TRUE, 0);
139 1 hiro
140 1 hiro
    gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
141 1 hiro
    gtk_widget_grab_default (ok_button);
142 1 hiro
143 1 hiro
    g_signal_connect(G_OBJECT(ok_button), "clicked",
144 1 hiro
                     G_CALLBACK(passphrase_ok_cb), NULL);
145 1 hiro
    g_signal_connect(G_OBJECT(pass_entry), "activate",
146 1 hiro
                     G_CALLBACK(passphrase_ok_cb), NULL);
147 1 hiro
    g_signal_connect(G_OBJECT(cancel_button), "clicked",
148 1 hiro
                     G_CALLBACK(passphrase_cancel_cb), NULL);
149 1 hiro
150 1 hiro
    if (grab_all)
151 1 hiro
        g_object_set (G_OBJECT(window), "type", GTK_WINDOW_POPUP, NULL);
152 1 hiro
    gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
153 1 hiro
    if (grab_all)
154 1 hiro
        gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
155 1 hiro
156 1 hiro
    gtk_widget_show_all(window);
157 1 hiro
158 1 hiro
    /* don't use XIM on entering passphrase */
159 1 hiro
    gtkut_editable_disable_im(GTK_EDITABLE(pass_entry));
160 1 hiro
161 1 hiro
    if (grab_all) {
162 1 hiro
#ifdef GDK_WINDOWING_X11
163 1 hiro
        //XGrabServer(GDK_DISPLAY());
164 1 hiro
#endif /* GDK_WINDOWING_X11 */
165 1 hiro
        if ( gdk_pointer_grab ( window->window, TRUE, 0,
166 1 hiro
                                NULL, NULL, GDK_CURRENT_TIME)) {
167 1 hiro
#ifdef GDK_WINDOWING_X11
168 1 hiro
            //XUngrabServer ( GDK_DISPLAY() );
169 1 hiro
#endif /* GDK_WINDOWING_X11 */
170 1 hiro
            g_warning ("OOPS: Could not grab mouse\n");
171 1 hiro
            gtk_widget_destroy (window);
172 1 hiro
            return NULL;
173 1 hiro
        }
174 1 hiro
        if ( gdk_keyboard_grab( window->window, FALSE, GDK_CURRENT_TIME )) {
175 1 hiro
            gdk_pointer_ungrab (GDK_CURRENT_TIME);
176 1 hiro
#ifdef GDK_WINDOWING_X11
177 1 hiro
            //XUngrabServer ( GDK_DISPLAY() );
178 1 hiro
#endif /* GDK_WINDOWING_X11 */
179 1 hiro
            g_warning ("OOPS: Could not grab keyboard\n");
180 1 hiro
            gtk_widget_destroy (window);
181 1 hiro
            return NULL;
182 1 hiro
        }
183 1 hiro
    }
184 1 hiro
185 1 hiro
    gtk_main();
186 1 hiro
187 1 hiro
    if (grab_all) {
188 1 hiro
#ifdef GDK_WINDOWING_X11
189 1 hiro
        //XUngrabServer (GDK_DISPLAY());
190 1 hiro
#endif /* GDK_WINDOWING_X11 */
191 1 hiro
        gdk_pointer_ungrab (GDK_CURRENT_TIME);
192 1 hiro
        gdk_keyboard_ungrab (GDK_CURRENT_TIME);
193 1 hiro
        gdk_flush();
194 1 hiro
    }
195 1 hiro
196 1 hiro
    manage_window_focus_out(window, NULL, NULL);
197 1 hiro
198 1 hiro
    if (pass_ack) {
199 1 hiro
        const gchar *entry_text;
200 1 hiro
        entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry));
201 1 hiro
        if (entry_text) /* Hmmm: Do we really need this? */
202 1 hiro
            the_passphrase = g_strdup (entry_text);
203 1 hiro
    }
204 1 hiro
    gtk_widget_destroy (window);
205 1 hiro
206 1 hiro
    return the_passphrase;
207 1 hiro
}
208 1 hiro
209 1 hiro
210 1 hiro
static void
211 1 hiro
passphrase_ok_cb(GtkWidget *widget, gpointer data)
212 1 hiro
{
213 1 hiro
    pass_ack = TRUE;
214 1 hiro
    gtk_main_quit();
215 1 hiro
}
216 1 hiro
217 1 hiro
static void
218 1 hiro
passphrase_cancel_cb(GtkWidget *widget, gpointer data)
219 1 hiro
{
220 1 hiro
    pass_ack = FALSE;
221 1 hiro
    gtk_main_quit();
222 1 hiro
}
223 1 hiro
224 1 hiro
225 1 hiro
static gint
226 1 hiro
passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
227 1 hiro
{
228 1 hiro
    passphrase_cancel_cb(NULL, NULL);
229 1 hiro
    return TRUE;
230 1 hiro
}
231 1 hiro
232 1 hiro
233 1 hiro
static gboolean
234 1 hiro
passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
235 1 hiro
{
236 1 hiro
    if (event && event->keyval == GDK_Escape)
237 1 hiro
        passphrase_cancel_cb(NULL, NULL);
238 1 hiro
    return FALSE;
239 1 hiro
}
240 1 hiro
241 1 hiro
static gint
242 1 hiro
linelen (const gchar *s)
243 1 hiro
{
244 1 hiro
    gint i;
245 1 hiro
246 1 hiro
    for (i = 0; *s && *s != '\n'; s++, i++)
247 1 hiro
        ;
248 1 hiro
249 1 hiro
    return i;
250 1 hiro
}
251 1 hiro
252 1 hiro
static GtkWidget *
253 1 hiro
create_description (const gchar *desc)
254 1 hiro
{
255 1 hiro
    const gchar *cmd = NULL, *uid = NULL, *info = NULL;
256 1 hiro
    gchar *buf;
257 1 hiro
    GtkWidget *label;
258 1 hiro
259 1 hiro
    cmd = desc;
260 1 hiro
    uid = strchr (cmd, '\n');
261 1 hiro
    if (uid) {
262 1 hiro
        info = strchr (++uid, '\n');
263 1 hiro
        if (info )
264 1 hiro
            info++;
265 1 hiro
    }
266 1 hiro
267 1 hiro
    if (!uid)
268 1 hiro
        uid = _("[no user id]");
269 1 hiro
    if (!info)
270 1 hiro
        info = "";
271 1 hiro
272 1 hiro
    buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
273 1 hiro
                           "  %.*s  \n"
274 1 hiro
                           "(%.*s)\n"),
275 1 hiro
                           !strncmp (cmd, "TRY_AGAIN", 9 ) ?
276 1 hiro
                           _("Bad passphrase! Try again...\n\n") : "",
277 1 hiro
                           linelen (uid), uid, linelen (info), info);
278 1 hiro
279 1 hiro
    label = gtk_label_new (buf);
280 1 hiro
    g_free (buf);
281 1 hiro
282 1 hiro
    return label;
283 1 hiro
}
284 1 hiro
285 1 hiro
static int free_passphrase(gpointer _unused)
286 1 hiro
{
287 1 hiro
    if (last_pass != NULL) {
288 1 hiro
        munlock(last_pass, strlen(last_pass));
289 1 hiro
        g_free(last_pass);
290 1 hiro
        last_pass = NULL;
291 1 hiro
        debug_print("%% passphrase removed");
292 1 hiro
    }
293 1 hiro
294 1 hiro
    return FALSE;
295 1 hiro
}
296 1 hiro
297 1 hiro
const char*
298 1 hiro
gpgmegtk_passphrase_cb (void *opaque, const char *desc, void **r_hd)
299 1 hiro
{
300 1 hiro
    struct passphrase_cb_info_s *info = opaque;
301 1 hiro
    GpgmeCtx ctx = info ? info->c : NULL;
302 1 hiro
    const char *pass;
303 1 hiro
304 1 hiro
    if (!desc) {
305 1 hiro
        /* FIXME: cleanup by looking at *r_hd */
306 1 hiro
        return NULL;
307 1 hiro
    }
308 1 hiro
    if (prefs_common.store_passphrase && last_pass != NULL &&
309 1 hiro
        strncmp(desc, "TRY_AGAIN", 9) != 0)
310 1 hiro
        return g_strdup(last_pass);
311 1 hiro
312 1 hiro
    gpgmegtk_set_passphrase_grab (prefs_common.passphrase_grab);
313 1 hiro
    debug_print ("%% requesting passphrase for `%s': ", desc);
314 1 hiro
    pass = passphrase_mbox (desc);
315 1 hiro
    gpgmegtk_free_passphrase();
316 1 hiro
    if (!pass) {
317 1 hiro
        debug_print ("%% cancel passphrase entry");
318 1 hiro
        gpgme_cancel (ctx);
319 1 hiro
    }
320 1 hiro
    else {
321 1 hiro
        if (prefs_common.store_passphrase) {
322 1 hiro
            last_pass = g_strdup(pass);
323 1 hiro
            if (mlock(last_pass, strlen(last_pass)) == -1)
324 1 hiro
                debug_print("%% locking passphrase failed");
325 1 hiro
326 1 hiro
            if (prefs_common.store_passphrase_timeout > 0) {
327 1 hiro
                gtk_timeout_add(prefs_common.store_passphrase_timeout*60*1000,
328 1 hiro
                                free_passphrase, NULL);
329 1 hiro
            }
330 1 hiro
        }
331 1 hiro
        debug_print ("%% sending passphrase");
332 1 hiro
    }
333 1 hiro
334 1 hiro
    return pass;
335 1 hiro
}
336 1 hiro
337 1 hiro
void gpgmegtk_free_passphrase()
338 1 hiro
{
339 1 hiro
    (void)free_passphrase(NULL); // could be inline
340 1 hiro
}
341 1 hiro
342 1 hiro
#endif /* USE_GPGME */