source: trunk/third/gnome-keyring/gnome-keyring-ask.c @ 21094

Revision 21094, 17.9 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21093, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2/* gnome-keyring-ask.c - Handles graphical authentication for the keyring daemon.
3
4   Copyright (C) 2003 Red Hat, Inc
5
6   Gnome keyring is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License as
8   published by the Free Software Foundation; either version 2 of the
9   License, or (at your option) any later version.
10 
11   Gnome keyring is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20   Author: Alexander Larsson <alexl@redhat.com>
21*/
22
23#include "config.h"
24
25#include <stdio.h>
26#include <string.h>
27#include <locale.h>
28
29#include <gtk/gtk.h>
30
31#include "gnome-keyring-private.h"
32
33#ifdef ENABLE_NLS
34#  include <libintl.h>
35#  define _(String) gettext (String)
36#  ifdef gettext_noop
37#    define N_(String) gettext_noop (String)
38#  else
39#    define N_(String) (String)
40#  endif
41#else
42#  define _(String)
43#  define N_(String) (String)
44#endif
45
46const char *env_app_display_name;
47const char *env_app_pathname;
48const char *env_keyring_name;
49const char *env_item_name;
50
51static char *
52create_markup (const char *primary, const char *secondary)
53{
54        return g_strconcat ("<span weight=\"bold\" size=\"larger\">", primary, "</span>\n\n", secondary, NULL);
55}
56
57enum {
58        KEYRING_NAME_NORMAL,
59        KEYRING_NAME_DEFAULT,
60        KEYRING_NAME_UNKNOWN,
61};
62
63enum {
64        APPLICATION_NAME_DISPLAY_AND_PATH,
65        APPLICATION_NAME_DISPLAY_ONLY,
66        APPLICATION_NAME_PATH_ONLY,
67        APPLICATION_NAME_UNKNOWN,
68};
69
70static gint
71run_dialog (const char *title,
72            const char *primary,
73            const char *secondary,
74            gboolean include_password,
75            char **password_out,
76            guint default_response,
77            const gchar *first_button_text,
78            ...)
79{
80        GtkWidget *dialog;
81        GtkLabel *message_widget;
82        char *message;
83        GtkWidget *entry;
84        gint response;
85        va_list args;
86        const char *text;
87        gint response_id;
88        GtkWidget *hbox;
89        GtkWidget *vbox;
90        GtkWidget *image;
91        const char *password;
92
93        dialog = gtk_dialog_new_with_buttons (title , NULL, 0, NULL);
94        gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
95        gtk_container_set_border_width (GTK_CONTAINER (dialog), 6);
96        gtk_window_set_default_size (GTK_WINDOW (dialog), 300, -1);
97        gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 12);
98        gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
99
100        va_start (args, first_button_text);
101       
102        text = first_button_text;
103        response_id = va_arg (args, gint);
104
105        while (text != NULL) {
106                gtk_dialog_add_button (GTK_DIALOG (dialog), text, response_id);
107                       
108                text = va_arg (args, char*);
109                if (text == NULL) {
110                        break;
111                }
112                response_id = va_arg (args, int);
113        }
114       
115        va_end (args);
116
117        gtk_dialog_set_default_response (GTK_DIALOG (dialog), default_response);
118
119        hbox = gtk_hbox_new (FALSE, 12);
120        gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
121       
122        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
123                            FALSE, FALSE, 0);
124
125        image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION, GTK_ICON_SIZE_DIALOG);
126        gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
127
128        gtk_box_pack_start (GTK_BOX (hbox), image,
129                            FALSE, FALSE, 0);
130
131        vbox = gtk_vbox_new (FALSE, 12);
132        gtk_box_pack_start (GTK_BOX (hbox),
133                            GTK_WIDGET (vbox),
134                            FALSE, FALSE, 0);
135       
136        message = create_markup (primary, secondary);
137        message_widget = GTK_LABEL (gtk_label_new (message));
138        g_free (message);
139        gtk_label_set_use_markup (message_widget, TRUE);
140        gtk_misc_set_alignment (GTK_MISC (message_widget), 0.0, 0.5);
141        gtk_label_set_line_wrap (message_widget, TRUE);
142        gtk_label_set_justify (message_widget,
143                               GTK_JUSTIFY_LEFT);
144        gtk_box_pack_start (GTK_BOX (vbox),
145                            GTK_WIDGET (message_widget),
146                            FALSE, FALSE, 0);
147
148        entry = NULL;
149        if (include_password) {
150                entry = gtk_entry_new ();
151                gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
152                g_signal_connect_swapped (entry,
153                                          "activate",
154                                          G_CALLBACK (gtk_window_activate_default),
155                                          dialog);
156                gtk_box_pack_start (GTK_BOX (vbox),
157                                    entry,
158                                    FALSE, FALSE, 0);
159        }
160
161 retry:
162        gtk_widget_show_all (dialog);
163        response = gtk_dialog_run (GTK_DIALOG (dialog));
164
165        if (include_password && entry != NULL) {
166                password = gtk_entry_get_text (GTK_ENTRY (entry));
167                if (response == GTK_RESPONSE_OK && *password == 0) {
168                        goto retry;
169                }
170                *password_out = g_strdup (password);
171        }
172
173        gtk_widget_destroy (dialog);
174       
175        return response;
176}
177
178
179static int
180get_app_information (void)
181{
182        if (env_app_display_name != NULL) {
183                if (env_app_pathname != NULL) {
184                        return APPLICATION_NAME_DISPLAY_AND_PATH;
185                }
186                return APPLICATION_NAME_DISPLAY_ONLY;
187        }
188        if (env_app_pathname != NULL) {
189                return APPLICATION_NAME_PATH_ONLY;
190        }
191        return APPLICATION_NAME_UNKNOWN;
192}
193
194static int
195get_keyring_information (void)
196{
197        if (env_keyring_name != NULL) {
198                if (strcmp (env_keyring_name, "default") == 0) {
199                        return KEYRING_NAME_DEFAULT;
200                } else {
201                        return KEYRING_NAME_NORMAL;
202                }
203        }
204
205        return KEYRING_NAME_UNKNOWN;
206}
207
208static void
209ask_for_keyring_password (void)
210{
211        char *message;
212        gint response;
213        char *password;
214        char *primary;
215        int app;
216        int keyring;
217       
218        app = get_app_information ();
219        keyring = get_keyring_information ();
220
221        if (app == APPLICATION_NAME_DISPLAY_AND_PATH) {
222                if (keyring == KEYRING_NAME_DEFAULT) {
223                        message = g_strdup_printf (_("The application '%s' (%s) wants access to "
224                                                     "the default keyring, but it is locked"),
225                                                   env_app_display_name, env_app_pathname);
226                } else if (keyring == KEYRING_NAME_NORMAL) {
227                        message = g_strdup_printf (_("The application '%s' (%s) wants access to "
228                                                     "the keyring '%s', but it is locked"),
229                                                   env_app_display_name, env_app_pathname,
230                                                   env_keyring_name);
231                } else /* keyring == KEYRING_NAME_UNKNOWN */ {
232                        message = g_strdup_printf (_("The application '%s' (%s) wants access to "
233                                                     "an unknown keyring, but it is locked"),
234                                                   env_app_display_name, env_app_pathname);
235                }
236        } else if (app == APPLICATION_NAME_DISPLAY_ONLY) {
237                if (keyring == KEYRING_NAME_DEFAULT) {
238                        message = g_strdup_printf (_("The application '%s' wants access to the "
239                                                     "default keyring, but it is locked"),
240                                                   env_app_display_name);
241                } else if (keyring == KEYRING_NAME_NORMAL) {
242                        message = g_strdup_printf (_("The application '%s' wants access to the "
243                                                     "keyring '%s', but it is locked"),
244                                                   env_app_display_name, env_keyring_name);
245                } else /* keyring == KEYRING_NAME_UNKNOWN */ {
246                        message = g_strdup_printf (_("The application '%s' wants access to an "
247                                                     "unknown keyring, but it is locked"),
248                                                   env_app_display_name);
249                }
250        } else if (app == APPLICATION_NAME_PATH_ONLY) {
251                if (keyring == KEYRING_NAME_DEFAULT) {
252                        message = g_strdup_printf (_("The application '%s' wants access to the "
253                                                     "default keyring, but it is locked"),
254                                                   env_app_pathname);
255                }
256                else if (keyring == KEYRING_NAME_NORMAL) {
257                        message = g_strdup_printf (_("The application '%s' wants access to the "
258                                                     "keyring '%s', but it is locked"),
259                                                   env_app_pathname, env_keyring_name);
260                }
261                else /* keyring == KEYRING_NAME_UNKNOWN */ {
262                        message = g_strdup_printf (_("The application '%s' wants access to an "
263                                                     "unknown keyring, but it is locked"),
264                                                   env_app_pathname);
265                }
266        } else { /* app == APPLICATION_NAME_UNKNOWN) */
267                if (keyring == KEYRING_NAME_DEFAULT) {
268                        message = g_strdup_printf (_("An unknown application wants access to the "
269                                                     "default keyring, but it is locked"));
270                }
271                else if (keyring == KEYRING_NAME_NORMAL) {
272                        message = g_strdup_printf (_("An unknown application wants access to the "
273                                                     "keyring '%s', but it is locked"),
274                                                   env_keyring_name);
275                }
276                else /* keyring == KEYRING_NAME_UNKNOWN */ {
277                        message = g_strdup_printf (_("An unknown application wants access to an "
278                                                     "unknown keyring, but it is locked"));
279                }
280        }
281
282        if (env_keyring_name == NULL ||
283            strcmp (env_keyring_name, "default") == 0) {
284                primary = g_strdup (_("Enter password for default keyring to unlock"));
285        } else {
286                primary = g_strdup_printf (_("Enter password for keyring '%s' to unlock"), env_keyring_name);
287        }
288
289        response = run_dialog (_("Unlock Keyring"),
290                               primary,
291                               message,
292                               TRUE, &password,
293                               GTK_RESPONSE_OK,
294                               _("_Deny"), GTK_RESPONSE_CANCEL,
295                               GTK_STOCK_OK, GTK_RESPONSE_OK,
296                               NULL);
297        g_free (message);
298        g_free (primary);
299                   
300        if (response == GTK_RESPONSE_OK) {
301                response = GNOME_KEYRING_ASK_RESPONSE_ALLOW_ONCE;
302        } else {
303                response = GNOME_KEYRING_ASK_RESPONSE_DENY;
304        }
305       
306        printf ("%d\n%s\n", response, password);
307        g_free (password);
308}
309
310static void
311ask_for_new_keyring_password (void)
312{
313        char *message;
314        gint response;
315        int app;
316        int keyring;
317        char *password;
318       
319        app = get_app_information ();
320        if (env_keyring_name == NULL) {
321                env_keyring_name = "";
322        }
323        keyring = get_keyring_information ();
324        g_assert (keyring != KEYRING_NAME_UNKNOWN);
325       
326        message = NULL;
327        if (app == APPLICATION_NAME_DISPLAY_AND_PATH) {
328                if (keyring == KEYRING_NAME_NORMAL) {
329                        message = g_strdup_printf (_("The application '%s' (%s) wants to create a new keyring called '%s'. "
330                                                     "You have to choose the password you want to use for it."),
331                                                   env_app_display_name, env_app_pathname, env_keyring_name);
332                } else if (keyring == KEYRING_NAME_DEFAULT) {
333                        message = g_strdup_printf (_("The application '%s' (%s) wants to create a new default keyring. "
334                                                     "You have to choose the password you want to use for it."),
335                                                   env_app_display_name, env_app_pathname);
336                }
337        } else if (app == APPLICATION_NAME_DISPLAY_ONLY) {
338                if (keyring == KEYRING_NAME_NORMAL) {
339                        message = g_strdup_printf (_("The application '%s' wants to create a new keyring called '%s'. "
340                                                     "You have to choose the password you want to use for it."),
341                                                   env_app_display_name, env_keyring_name);
342                } else if (keyring == KEYRING_NAME_DEFAULT) {
343                        message = g_strdup_printf (_("The application '%s' wants to create a new default keyring. "
344                                                     "You have to choose the password you want to use for it."),
345                                                   env_app_display_name);
346                }
347        } else if (app == APPLICATION_NAME_PATH_ONLY) {
348                if (keyring == KEYRING_NAME_NORMAL) {
349                        message = g_strdup_printf (_("The application '%s' wants to create a new keyring called '%s'. "
350                                                     "You have to choose the password you want to use for it."),
351                                                   env_app_pathname, env_keyring_name);
352                } else if (keyring == KEYRING_NAME_DEFAULT) {
353                        message = g_strdup_printf (_("The application '%s' wants to create a new default keyring. "
354                                                     "You have to choose the password you want to use for it."),
355                                                   env_app_pathname);
356                }
357        } else /* app == APPLICATION_NAME_UNKNOWN */ {
358                if (keyring == KEYRING_NAME_NORMAL) {
359                        message = g_strdup_printf (_("An unkown application wants to create a new keyring called '%s'. "
360                                                     "You have to choose the password you want to use for it."),
361                                                   env_keyring_name);
362                } else if (keyring == KEYRING_NAME_DEFAULT) {
363                        message = g_strdup_printf (_("An unkown application wants to create a new default keyring. "
364                                                     "You have to choose the password you want to use for it."));
365                }
366        }
367
368       
369        response = run_dialog (_("New Keyring Password"),
370                               _("Choose password for new keyring"),
371                               message,
372                               TRUE, &password,
373                               GTK_RESPONSE_OK,
374                               _("_Deny"), GTK_RESPONSE_CANCEL,
375                               GTK_STOCK_OK, GTK_RESPONSE_OK,
376                               NULL);
377        g_free (message);
378       
379        if (response == GTK_RESPONSE_OK) {
380                response = GNOME_KEYRING_ASK_RESPONSE_ALLOW_ONCE;
381        } else {
382                response = GNOME_KEYRING_ASK_RESPONSE_DENY;
383        }
384       
385        printf ("%d\n%s\n", response, password);
386        g_free (password);
387}
388
389static void
390ask_for_default_keyring (void)
391{
392        char *message;
393        gint response;
394        int app;
395        char *password;
396       
397        app = get_app_information ();
398 
399        if (app == APPLICATION_NAME_DISPLAY_AND_PATH) {
400                message = g_strdup_printf (_("The application '%s' (%s) wants to store a password, but there is no default keyring. "
401                                             "To create one, you need to choose the password you wish to use for it."),
402                                           env_app_display_name, env_app_pathname);
403        } else if (app == APPLICATION_NAME_DISPLAY_ONLY) {
404                message = g_strdup_printf (_("The application '%s' wants to store a password, but there is no default keyring. "
405                                             "To create one, you need to choose the password you wish to use for it."),
406                                           env_app_display_name);
407        } else if (app == APPLICATION_NAME_PATH_ONLY) {
408                message = g_strdup_printf (_("The application '%s' wants to store a password, but there is no default keyring. "
409                                             "To create one, you need to choose the password you wish to use for it."),
410                                           env_app_pathname);
411        } else /* app == APPLICATION_NAME_UNKNOWN */ {
412                message = g_strdup_printf (_("An unknown application wants to store a password, but there is no default keyring. "
413                                             "To create one, you need to choose the password you wish to use for it."));
414        }
415
416        response = run_dialog (_("Create Default Keyring"),
417                               _("Choose password for default keyring"),
418                               message,
419                               TRUE, &password,
420                               GTK_RESPONSE_OK,
421                               _("_Deny"), GTK_RESPONSE_CANCEL,
422                               GTK_STOCK_OK, GTK_RESPONSE_OK,
423                               NULL);
424        g_free (message);
425
426        if (response == GTK_RESPONSE_OK) {
427                response = GNOME_KEYRING_ASK_RESPONSE_ALLOW_ONCE;
428        } else {
429                response = GNOME_KEYRING_ASK_RESPONSE_DENY;
430        }
431       
432        printf ("%d\n%s\n", response, password);
433        g_free (password);
434}
435
436
437static void
438ask_for_item_read_write_acccess (void)
439{
440        int app;
441        int keyring;
442        char *primary;
443        char *secondary;
444        const char *item;
445        gint response;
446       
447        app = get_app_information ();
448        keyring = get_keyring_information ();
449        item = env_item_name;
450        if (item == NULL) {
451                item = "";
452        }
453       
454        primary = _("Allow application access to keyring?");
455        if (app == APPLICATION_NAME_DISPLAY_AND_PATH) {
456                if (keyring == KEYRING_NAME_NORMAL) {
457                        secondary = g_strdup_printf (_("The application '%s' (%s) wants to access the password for '%s' in %s."),
458                                                     env_app_display_name, env_app_pathname, item, env_keyring_name);
459                } else if (keyring == KEYRING_NAME_DEFAULT) {
460                        secondary = g_strdup_printf (_("The application '%s' (%s) wants to access the password for '%s' in the default keyring."),
461                                                     env_app_display_name, env_app_pathname, item);
462                } else /* keyring == KEYRING_NAME_UNKNOWN */ {
463                        secondary = g_strdup_printf (_("The application '%s' (%s) wants to access the password for '%s' in an unknown keyring."),
464                                                     env_app_display_name, env_app_pathname, item);
465                }
466        } else if (app == APPLICATION_NAME_DISPLAY_ONLY) {
467                if (keyring == KEYRING_NAME_NORMAL) {
468                        secondary = g_strdup_printf (_("The application '%s' wants to access the password for '%s' in %s."),
469                                                     env_app_display_name, item, env_keyring_name);
470                } else if (keyring == KEYRING_NAME_DEFAULT) {
471                        secondary = g_strdup_printf (_("The application '%s' wants to access the password for '%s' in the default keyring."),
472                                                     env_app_display_name, item);
473                } else /* keyring == KEYRING_NAME_UNKNOWN */ {
474                        secondary = g_strdup_printf (_("The application '%s' wants to access the password for '%s' in an unknown keyring."),
475                                                     env_app_display_name, item);
476                }
477        } else if (app == APPLICATION_NAME_PATH_ONLY) {
478                if (keyring == KEYRING_NAME_NORMAL) {
479                        secondary = g_strdup_printf (_("The application '%s' wants to access the password for '%s' in %s."),
480                                                     env_app_pathname, item, env_keyring_name);
481                } else if (keyring == KEYRING_NAME_DEFAULT) {
482                        secondary = g_strdup_printf (_("The application '%s' wants to access the password for '%s' in the default keyring."),
483                                                     env_app_pathname, item);
484                } else /* keyring == KEYRING_NAME_UNKNOWN */ {
485                        secondary = g_strdup_printf (_("The application '%s' wants to access the password for '%s' in an unknown keyring."),
486                                                     env_app_pathname, item);
487                }
488        } else /* app == APPLICATION_NAME_UNKNOWN */ {
489                if (keyring == KEYRING_NAME_NORMAL) {
490                        secondary = g_strdup_printf (_("An unknown application wants to access the password for '%s' in %s."),
491                                                     item, env_keyring_name);
492                } else if (keyring == KEYRING_NAME_DEFAULT) {
493                        secondary = g_strdup_printf (_("An unknown application wants to access the password for '%s' in the default keyring."),
494                                                     item);
495                } else /* keyring == KEYRING_NAME_UNKNOWN */ {
496                        secondary = g_strdup_printf (_("An unknown application wants to access the password for '%s' in an unknown keyring."),
497                                                     item);
498                }
499        }
500
501        response = run_dialog (_("Allow access"),
502                               primary, secondary,
503                               FALSE, NULL,
504                               2,
505                               _("_Deny"), GTK_RESPONSE_CANCEL,
506                               _("Allow _Once"), 1,
507                               _("_Always Allow"), 2,
508                               NULL);
509        g_free (secondary);
510       
511       
512        if (response == 1) {
513                response = GNOME_KEYRING_ASK_RESPONSE_ALLOW_ONCE;
514        } else if (response == 2) {
515                response = GNOME_KEYRING_ASK_RESPONSE_ALLOW_FOREVER;
516        } else {
517                response = GNOME_KEYRING_ASK_RESPONSE_DENY;
518        }
519       
520        printf ("%d\n", response);
521}
522
523
524
525int
526main (int argc, char *argv[])
527{
528
529        env_app_display_name = g_getenv ("KEYRING_APP_NAME");
530        env_app_pathname = g_getenv ("KEYRING_APP_PATH");
531        env_keyring_name = g_getenv ("KEYRING_NAME");
532        env_item_name = g_getenv ("ITEM_NAME");
533
534        gtk_init (&argc, &argv);
535#ifdef HAVE_LOCALE_H
536        /* internationalisation */
537        setlocale (LC_ALL, "");
538#endif
539
540#ifdef HAVE_GETTEXT
541        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
542        textdomain (GETTEXT_PACKAGE);
543        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
544#endif
545
546        if (argc < 2) {
547                g_print (_("You must specify the type of request to run\n"));
548                return 1;
549        }
550
551        if (strcmp (argv[1], "-k") == 0) {
552                ask_for_keyring_password ();
553        } else if (strcmp (argv[1], "-n") == 0) {
554                ask_for_new_keyring_password ();
555        } else if (strcmp (argv[1], "-i") == 0) {
556                ask_for_item_read_write_acccess ();
557        } else if (strcmp (argv[1], "-d") == 0) {
558                ask_for_default_keyring ();
559        } else {
560                g_print (_("Unknown request type\n"));
561        }
562       
563        return 0;
564}
Note: See TracBrowser for help on using the repository browser.