some string fixes
[strongswan.git] / src / charon / plugins / nm / gnome / auth-dialog / main.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2004 Dan Williams
5 * Red Hat, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <gnome-keyring.h>
28 #include <libgnomeui/libgnomeui.h>
29
30 #define NM_DBUS_SERVICE_STRONGSWAN "org.freedesktop.NetworkManager.strongswan"
31
32 static char *lookup(char *name, char *service)
33 {
34 GList *list;
35 GList *iter;
36 char *pass = NULL;
37
38 if (gnome_keyring_find_network_password_sync(g_get_user_name(), NULL, name,
39 NULL, service, NULL, 0, &list) != GNOME_KEYRING_RESULT_OK)
40 {
41 return NULL;
42 }
43
44 for (iter = list; iter; iter = iter->next)
45 {
46 GnomeKeyringNetworkPasswordData *data = iter->data;
47
48 if (strcmp(data->object, "password") == 0 && data->password)
49 {
50 pass = g_strdup(data->password);
51 break;
52 }
53 }
54 gnome_keyring_network_password_list_free(list);
55 return pass;
56 }
57
58 int main (int argc, char *argv[])
59 {
60 static gboolean retry = FALSE;
61 static gchar *name = NULL, *id = NULL, *service = NULL, *keyring = NULL, *pass;
62 GOptionContext *context;
63 GnomeProgram *program = NULL;
64 int exit_status = 1;
65 char buf;
66 guint32 itemid;
67 GtkWidget *dialog;
68 GOptionEntry entries[] = {
69 { "reprompt", 'r', 0, G_OPTION_ARG_NONE, &retry, "Reprompt for passwords", NULL},
70 { "id", 'i', 0, G_OPTION_ARG_STRING, &id, "ID of VPN connection", NULL},
71 { "name", 'n', 0, G_OPTION_ARG_STRING, &name, "Name of VPN connection", NULL},
72 { "service", 's', 0, G_OPTION_ARG_STRING, &service, "VPN service type", NULL},
73 { NULL }
74 };
75
76 bindtextdomain(GETTEXT_PACKAGE, NULL);
77 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
78 textdomain(GETTEXT_PACKAGE);
79
80 context = g_option_context_new ("- strongswan auth dialog");
81 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
82
83 program = gnome_program_init ("nm-strongswan-auth-dialog", VERSION,
84 LIBGNOMEUI_MODULE,
85 argc, argv,
86 GNOME_PARAM_GOPTION_CONTEXT, context,
87 GNOME_PARAM_NONE);
88
89 if (id == NULL || name == NULL || service == NULL)
90 {
91 fprintf (stderr, "Have to supply ID, name, and service\n");
92 g_object_unref (program);
93 return 1;
94 }
95
96 if (strcmp(service, NM_DBUS_SERVICE_STRONGSWAN) != 0)
97 {
98 fprintf(stderr, "This dialog only works with the '%s' service\n",
99 NM_DBUS_SERVICE_STRONGSWAN);
100 g_object_unref (program);
101 return 1;
102 }
103
104 pass = lookup(name, service);
105 if (!pass || retry)
106 {
107 dialog = gnome_password_dialog_new(_("VPN password required"),
108 _("Password required to establish VPN connection:"),
109 NULL, NULL, TRUE);
110 gnome_password_dialog_set_show_remember(GNOME_PASSWORD_DIALOG(dialog), TRUE);
111 gnome_password_dialog_set_show_username(GNOME_PASSWORD_DIALOG(dialog), FALSE);
112 if (pass)
113 {
114 gnome_password_dialog_set_password(GNOME_PASSWORD_DIALOG(dialog), pass);
115 }
116 if (!gnome_password_dialog_run_and_block(GNOME_PASSWORD_DIALOG(dialog)))
117 {
118 g_object_unref (program);
119 return 1;
120 }
121
122 pass = gnome_password_dialog_get_password(GNOME_PASSWORD_DIALOG(dialog));
123 switch (gnome_password_dialog_get_remember(GNOME_PASSWORD_DIALOG(dialog)))
124 {
125 case GNOME_PASSWORD_DIALOG_REMEMBER_NOTHING:
126 break;
127 case GNOME_PASSWORD_DIALOG_REMEMBER_SESSION:
128 keyring = "session";
129 /* FALL */
130 case GNOME_PASSWORD_DIALOG_REMEMBER_FOREVER:
131 if (gnome_keyring_set_network_password_sync(keyring,
132 g_get_user_name(), NULL, name, "password", service, NULL, 0,
133 pass, &itemid) != GNOME_KEYRING_RESULT_OK)
134 {
135 g_warning ("storing password in keyring failed");
136 }
137 break;
138 }
139 }
140 printf("password\n%s\n", pass);
141 printf("\n\n");
142 /* flush output, wait for input */
143 fflush(stdout);
144 fread(&buf, 1, sizeof(buf), stdin);
145 g_object_unref(program);
146 return 0;
147 }
148