b4432aae06bf132e465dd5ca3ab61b6e911aa2f4
[strongswan.git] / src / frontends / gnome / auth-dialog / main.c
1 /*
2 * Copyright (C) 2015 Lubomir Rintel
3 *
4 * Copyright (C) 2013-2016 Tobias Brunner
5 * Copyright (C) 2008-2011 Martin Willi
6 * HSR Hochschule fuer Technik Rapperswil
7 *
8 * Copyright (C) 2004 Dan Williams
9 * Red Hat, Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <libsecret/secret.h>
32
33 #include <NetworkManager.h>
34 #include <nm-vpn-service-plugin.h>
35 #include <nma-vpn-password-dialog.h>
36
37 #define NM_DBUS_SERVICE_STRONGSWAN "org.freedesktop.NetworkManager.strongswan"
38
39 #define KEYRING_UUID_TAG "connection-uuid"
40 #define KEYRING_SN_TAG "setting-name"
41 #define KEYRING_SK_TAG "setting-key"
42
43 static const SecretSchema network_manager_secret_schema = {
44 "org.freedesktop.NetworkManager.Connection",
45 SECRET_SCHEMA_DONT_MATCH_NAME,
46 {
47 { KEYRING_UUID_TAG, SECRET_SCHEMA_ATTRIBUTE_STRING },
48 { KEYRING_SN_TAG, SECRET_SCHEMA_ATTRIBUTE_STRING },
49 { KEYRING_SK_TAG, SECRET_SCHEMA_ATTRIBUTE_STRING },
50 { NULL, 0 },
51 }
52 };
53
54 #define UI_KEYFILE_GROUP "VPN Plugin UI"
55
56 static char *keyring_lookup_secret(const char *uuid, const char *secret_name)
57 {
58 GHashTable *attrs;
59 GList *list;
60 char *secret = NULL;
61
62 attrs = secret_attributes_build(&network_manager_secret_schema,
63 KEYRING_UUID_TAG, uuid,
64 KEYRING_SN_TAG, NM_SETTING_VPN_SETTING_NAME,
65 KEYRING_SK_TAG, secret_name,
66 NULL);
67
68 list = secret_service_search_sync (NULL, &network_manager_secret_schema, attrs,
69 SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK | SECRET_SEARCH_LOAD_SECRETS,
70 NULL, NULL);
71 if (list && list->data)
72 {
73 SecretItem *item = list->data;
74 SecretValue *value = secret_item_get_secret (item);
75
76 if (value)
77 {
78 secret = g_strdup (secret_value_get (value, NULL));
79 secret_value_unref (value);
80 }
81 }
82
83 g_list_free_full (list, g_object_unref);
84 g_hash_table_unref (attrs);
85 return secret;
86 }
87
88 static void keyfile_add_entry_info(GKeyFile *keyfile, const gchar *key, const gchar *value,
89 const gchar *label, gboolean is_secret, gboolean should_ask)
90 {
91 g_key_file_set_string (keyfile, key, "Value", value);
92 g_key_file_set_string (keyfile, key, "Label", label);
93 g_key_file_set_boolean (keyfile, key, "IsSecret", is_secret);
94 g_key_file_set_boolean (keyfile, key, "ShouldAsk", should_ask);
95 }
96
97 static void keyfile_print_stdout (GKeyFile *keyfile)
98 {
99 gchar *data;
100 gsize length;
101
102 data = g_key_file_to_data (keyfile, &length, NULL);
103
104 fputs (data, stdout);
105
106 g_free (data);
107 }
108
109 static gboolean get_secrets(const char *type, const char *uuid, const char *name, gboolean retry,
110 gboolean allow_interaction, gboolean external_ui_mode,
111 const char *in_pw, char **out_pw, NMSettingSecretFlags flags)
112 {
113 NMAVpnPasswordDialog *dialog;
114 char *prompt, *pw = NULL;
115 const char *new_pw = NULL;
116 guint32 minlen = 0;
117
118 if (!(flags & NM_SETTING_SECRET_FLAG_NOT_SAVED) &&
119 !(flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED))
120 {
121 if (in_pw)
122 {
123 pw = g_strdup (in_pw);
124 }
125 else
126 {
127 pw = keyring_lookup_secret (uuid, "password");
128 }
129 }
130 if (flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)
131 {
132 g_free (pw);
133 return TRUE;
134 }
135 if (!strcmp(type, "eap"))
136 {
137 prompt = g_strdup_printf (_("EAP password required to establish VPN connection '%s'."),
138 name);
139 }
140 else if (!strcmp(type, "key"))
141 {
142 prompt = g_strdup_printf (_("Private key decryption password required to establish VPN connection '%s'."),
143 name);
144 }
145 else if (!strcmp(type, "psk"))
146 {
147 prompt = g_strdup_printf (_("Pre-shared key required to establish VPN connection '%s' (min. 20 characters)."),
148 name);
149 minlen = 20;
150 }
151 else /* smartcard */
152 {
153 prompt = g_strdup_printf (_("Smartcard PIN required to establish VPN connection '%s'."),
154 name);
155 }
156 if (external_ui_mode)
157 {
158 GKeyFile *keyfile;
159
160 keyfile = g_key_file_new ();
161
162 g_key_file_set_integer (keyfile, UI_KEYFILE_GROUP, "Version", 2);
163 g_key_file_set_string (keyfile, UI_KEYFILE_GROUP, "Description", prompt);
164 g_key_file_set_string (keyfile, UI_KEYFILE_GROUP, "Title", _("Authenticate VPN"));
165
166 keyfile_add_entry_info (keyfile, "password", pw ?: "", _("Password:"), TRUE, allow_interaction);
167
168 keyfile_print_stdout (keyfile);
169 g_key_file_unref (keyfile);
170 goto out;
171 }
172 else if (!allow_interaction ||
173 (!retry && pw && !(flags & NM_SETTING_SECRET_FLAG_NOT_SAVED)))
174 {
175 /* If we can't prompt the user, just return the existing password. Do the same
176 * if we don't nee a new password (!retry) and have an existing saved one */
177 *out_pw = pw;
178 g_free (prompt);
179 return TRUE;
180 }
181
182 dialog = (NMAVpnPasswordDialog*)nma_vpn_password_dialog_new(_("Authenticate VPN"), prompt, NULL);
183 nma_vpn_password_dialog_set_show_password_secondary(dialog, FALSE);
184
185 if (pw && !(flags & NM_SETTING_SECRET_FLAG_NOT_SAVED))
186 {
187 nma_vpn_password_dialog_set_password(dialog, pw);
188 }
189 gtk_widget_show (GTK_WIDGET (dialog));
190
191 too_short_retry:
192 if (nma_vpn_password_dialog_run_and_block (dialog))
193 {
194 new_pw = nma_vpn_password_dialog_get_password(dialog);
195 if (new_pw && minlen && strlen(new_pw) < minlen)
196 {
197 goto too_short_retry;
198 }
199 else if (new_pw)
200 {
201 *out_pw = g_strdup (new_pw);
202 }
203 }
204 gtk_widget_hide (GTK_WIDGET (dialog));
205 gtk_widget_destroy (GTK_WIDGET (dialog));
206 out:
207 g_free (prompt);
208 return TRUE;
209 }
210
211 static void print_secret (const char *secret_name, gchar *secret)
212 {
213 if (secret)
214 {
215 printf("%s\n%s\n", secret_name, secret);
216 g_free(secret);
217 }
218 printf("\n\n");
219 fflush(stdout);
220 }
221
222 static void wait_for_quit (void)
223 {
224 GString *str;
225 char c;
226 ssize_t n;
227 time_t start;
228
229 str = g_string_sized_new (10);
230 start = time (NULL);
231 do {
232 errno = 0;
233 n = read (0, &c, 1);
234 if (n == 0 || (n < 0 && errno == EAGAIN))
235 g_usleep (G_USEC_PER_SEC / 10);
236 else if (n == 1) {
237 g_string_append_c (str, c);
238 if (strstr (str->str, "QUIT") || (str->len > 10))
239 break;
240 } else
241 break;
242 } while (time (NULL) < start + 20);
243 g_string_free (str, TRUE);
244 }
245
246 int main (int argc, char *argv[])
247 {
248 gboolean retry = FALSE, allow_interaction = FALSE, external_ui_mode = FALSE;
249 gchar *name = NULL, *uuid = NULL, *service = NULL, *pass = NULL;
250 GHashTable *data = NULL, *secrets = NULL;
251 NMSettingSecretFlags flags = NM_SETTING_SECRET_FLAG_NONE;
252 GOptionContext *context;
253 char *agent, *type;
254 int status = 0;
255 GOptionEntry entries[] = {
256 { "reprompt", 'r', 0, G_OPTION_ARG_NONE, &retry, "Reprompt for passwords", NULL},
257 { "uuid", 'u', 0, G_OPTION_ARG_STRING, &uuid, "UUID of VPN connection", NULL},
258 { "name", 'n', 0, G_OPTION_ARG_STRING, &name, "Name of VPN connection", NULL},
259 { "service", 's', 0, G_OPTION_ARG_STRING, &service, "VPN service type", NULL},
260 { "allow-interaction", 'i', 0, G_OPTION_ARG_NONE, &allow_interaction, "Allow user interaction", NULL},
261 { "external-ui-mode", 0, 0, G_OPTION_ARG_NONE, &external_ui_mode, "External UI mode", NULL},
262 { NULL }
263 };
264
265 bindtextdomain(GETTEXT_PACKAGE, NULL);
266 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
267 textdomain(GETTEXT_PACKAGE);
268
269 gtk_init (&argc, &argv);
270
271 context = g_option_context_new ("- strongswan auth dialog");
272 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
273 g_option_context_parse (context, &argc, &argv, NULL);
274 g_option_context_free (context);
275
276 if (uuid == NULL || name == NULL || service == NULL)
277 {
278 fprintf (stderr, "Have to supply UUID, name, and service\n");
279 return 1;
280 }
281
282 if (strcmp(service, NM_DBUS_SERVICE_STRONGSWAN) != 0)
283 {
284 fprintf(stderr, "This dialog only works with the '%s' service\n",
285 NM_DBUS_SERVICE_STRONGSWAN);
286 return 1;
287 }
288
289 if (!nm_vpn_service_plugin_read_vpn_details (0, &data, &secrets))
290 {
291 fprintf(stderr, "Failed to read '%s' (%s) data and secrets from stdin.\n",
292 name, uuid);
293 return 1;
294 }
295
296 type = g_hash_table_lookup (data, "method");
297 if (!type)
298 {
299 fprintf(stderr, "Connection lookup failed\n");
300 status = 1;
301 goto out;
302 }
303
304 if (!strcmp(type, "eap") || !strcmp(type, "key") ||
305 !strcmp(type, "psk") || !strcmp(type, "smartcard"))
306 {
307 nm_vpn_service_plugin_get_secret_flags (secrets, "password", &flags);
308 if (!get_secrets(type, uuid, name, retry, allow_interaction, external_ui_mode,
309 g_hash_table_lookup (secrets, "password"), &pass, flags))
310 {
311 status = 1;
312 }
313 else if (!external_ui_mode)
314 {
315 print_secret("password", pass);
316 wait_for_quit ();
317 }
318 }
319 else if (!strcmp(type, "agent"))
320 {
321 agent = getenv("SSH_AUTH_SOCK");
322 if (agent)
323 {
324 if (external_ui_mode)
325 {
326 GKeyFile *keyfile;
327
328 keyfile = g_key_file_new ();
329
330 g_key_file_set_integer (keyfile, UI_KEYFILE_GROUP, "Version", 2);
331 g_key_file_set_string (keyfile, UI_KEYFILE_GROUP, "Description", "SSH agent");
332 g_key_file_set_string (keyfile, UI_KEYFILE_GROUP, "Title", _("Authenticate VPN"));
333
334 keyfile_add_entry_info (keyfile, "agent", agent, "SSH agent socket", TRUE, FALSE);
335
336 keyfile_print_stdout (keyfile);
337 g_key_file_unref (keyfile);
338 }
339 else
340 {
341 print_secret("agent", g_strdup (agent));
342 wait_for_quit ();
343 }
344 }
345 else if (allow_interaction)
346 {
347 GtkWidget *dialog;
348 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR,
349 GTK_BUTTONS_OK,
350 _("Configuration uses ssh-agent for authentication, "
351 "but ssh-agent is not running!"));
352 gtk_dialog_run (GTK_DIALOG (dialog));
353 gtk_widget_destroy (dialog);
354 }
355 }
356
357 out:
358 if (data)
359 {
360 g_hash_table_unref (data);
361 }
362 if (secrets)
363 {
364 g_hash_table_unref(secrets);
365 }
366 return status;
367 }