2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2004 Dan Williams
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>.
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
23 #include <glib/gi18n.h>
25 #include <gnome-keyring.h>
26 #include <libgnomeui/libgnomeui.h>
27 #include <gconf/gconf-client.h>
28 #include <nm-vpn-plugin.h>
29 #include <nm-setting-vpn.h>
30 #include <nm-setting-connection.h>
32 #define NM_DBUS_SERVICE_STRONGSWAN "org.freedesktop.NetworkManager.strongswan"
35 * lookup a password in the keyring
37 static char *lookup_password(char *name
, char *service
)
43 if (gnome_keyring_find_network_password_sync(g_get_user_name(), NULL
, name
,
44 NULL
, service
, NULL
, 0, &list
) != GNOME_KEYRING_RESULT_OK
)
49 for (iter
= list
; iter
; iter
= iter
->next
)
51 GnomeKeyringNetworkPasswordData
*data
= iter
->data
;
53 if (strcmp(data
->object
, "password") == 0 && data
->password
)
55 pass
= g_strdup(data
->password
);
59 gnome_keyring_network_password_list_free(list
);
64 * get the connection type
66 static char* get_connection_type(char *uuid
)
68 GConfClient
*client
= NULL
;
71 char *key
, *str
, *path
, *found
= NULL
, *method
= NULL
;
73 client
= gconf_client_get_default();
75 list
= gconf_client_all_dirs(client
, "/system/networking/connections", NULL
);
76 g_return_val_if_fail(list
, NULL
);
78 for (iter
= list
; iter
; iter
= iter
->next
)
80 path
= (char *) iter
->data
;
82 key
= g_strdup_printf("%s/%s/%s", path
,
83 NM_SETTING_CONNECTION_SETTING_NAME
,
84 NM_SETTING_CONNECTION_UUID
);
85 str
= gconf_client_get_string(client
, key
, NULL
);
88 if (str
&& !strcmp(str
, uuid
))
90 found
= g_strdup(path
);
98 g_slist_foreach(list
, (GFunc
)g_free
, NULL
);
103 key
= g_strdup_printf ("%s/%s/%s", found
,
104 NM_SETTING_VPN_SETTING_NAME
, "method");
105 method
= gconf_client_get_string(client
, key
, NULL
);
109 g_object_unref(client
);
113 int main (int argc
, char *argv
[])
115 gboolean retry
= FALSE
;
116 gchar
*name
= NULL
, *uuid
= NULL
, *service
= NULL
, *keyring
= NULL
, *pass
;
117 GOptionContext
*context
;
118 GnomeProgram
*program
= NULL
;
119 char buf
, *agent
, *type
;
122 GOptionEntry entries
[] = {
123 { "reprompt", 'r', 0, G_OPTION_ARG_NONE
, &retry
, "Reprompt for passwords", NULL
},
124 { "uuid", 'u', 0, G_OPTION_ARG_STRING
, &uuid
, "UUID of VPN connection", NULL
},
125 { "name", 'n', 0, G_OPTION_ARG_STRING
, &name
, "Name of VPN connection", NULL
},
126 { "service", 's', 0, G_OPTION_ARG_STRING
, &service
, "VPN service type", NULL
},
130 bindtextdomain(GETTEXT_PACKAGE
, NULL
);
131 bind_textdomain_codeset(GETTEXT_PACKAGE
, "UTF-8");
132 textdomain(GETTEXT_PACKAGE
);
134 context
= g_option_context_new ("- strongswan auth dialog");
135 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
137 program
= gnome_program_init ("nm-strongswan-auth-dialog", VERSION
,
140 GNOME_PARAM_GOPTION_CONTEXT
, context
,
143 if (uuid
== NULL
|| name
== NULL
|| service
== NULL
)
145 fprintf (stderr
, "Have to supply UUID, name, and service\n");
146 g_object_unref (program
);
150 if (strcmp(service
, NM_DBUS_SERVICE_STRONGSWAN
) != 0)
152 fprintf(stderr
, "This dialog only works with the '%s' service\n",
153 NM_DBUS_SERVICE_STRONGSWAN
);
154 g_object_unref (program
);
158 type
= get_connection_type(uuid
);
161 fprintf(stderr
, "Connection lookup failed\n");
162 g_object_unref (program
);
165 if (!strcmp(type
, "eap") || !strcmp(type
, "key") || !strcmp(type
, "smartcard"))
167 pass
= lookup_password(name
, service
);
170 if (!strcmp(type
, "eap"))
172 dialog
= gnome_password_dialog_new(_("VPN password required"),
173 _("EAP password required to establish VPN connection:"),
175 gnome_password_dialog_set_show_remember(GNOME_PASSWORD_DIALOG(dialog
), TRUE
);
177 else if (!strcmp(type
, "key"))
179 dialog
= gnome_password_dialog_new(_("VPN password required"),
180 _("Private key decryption password required to establish VPN connection:"),
182 gnome_password_dialog_set_show_remember(GNOME_PASSWORD_DIALOG(dialog
), TRUE
);
186 dialog
= gnome_password_dialog_new(_("VPN password required"),
187 _("Smartcard PIN required to establish VPN connection:"),
189 gnome_password_dialog_set_show_remember(GNOME_PASSWORD_DIALOG(dialog
), FALSE
);
191 gnome_password_dialog_set_show_username(GNOME_PASSWORD_DIALOG(dialog
), FALSE
);
194 gnome_password_dialog_set_password(GNOME_PASSWORD_DIALOG(dialog
), pass
);
196 if (!gnome_password_dialog_run_and_block(GNOME_PASSWORD_DIALOG(dialog
)))
198 g_object_unref (program
);
202 pass
= gnome_password_dialog_get_password(GNOME_PASSWORD_DIALOG(dialog
));
203 switch (gnome_password_dialog_get_remember(GNOME_PASSWORD_DIALOG(dialog
)))
205 case GNOME_PASSWORD_DIALOG_REMEMBER_NOTHING
:
207 case GNOME_PASSWORD_DIALOG_REMEMBER_SESSION
:
210 case GNOME_PASSWORD_DIALOG_REMEMBER_FOREVER
:
211 if (gnome_keyring_set_network_password_sync(keyring
,
212 g_get_user_name(), NULL
, name
, "password", service
, NULL
, 0,
213 pass
, &itemid
) != GNOME_KEYRING_RESULT_OK
)
215 g_warning ("storing password in keyring failed");
220 printf("password\n%s\n", pass
);
224 agent
= getenv("SSH_AUTH_SOCK");
227 printf("agent\n%s\n", agent
);
231 dialog
= gtk_message_dialog_new(NULL
, 0, GTK_MESSAGE_ERROR
,
233 _("Configuration uses ssh-agent for authentication, "
234 "but ssh-agent is not running!"));
235 gtk_dialog_run (GTK_DIALOG (dialog
));
236 gtk_widget_destroy (dialog
);
241 /* flush output, wait for input */
243 if (fread(&buf
, 1, sizeof(buf
), stdin
));
244 g_object_unref(program
);