412a97fa380cdb712d97ccddade926afcd46fb24
[strongswan.git] / src / charon / plugins / nm / nm_service.c
1 /*
2 * Copyright (C) 2008-2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <nm-setting-vpn.h>
17 #include <nm-setting-connection.h>
18 #include "nm_service.h"
19
20 #include <daemon.h>
21 #include <utils/host.h>
22 #include <utils/identification.h>
23 #include <config/peer_cfg.h>
24 #include <credentials/certificates/x509.h>
25
26 #include <stdio.h>
27
28 G_DEFINE_TYPE(NMStrongswanPlugin, nm_strongswan_plugin, NM_TYPE_VPN_PLUGIN)
29
30 /**
31 * Private data of NMStrongswanPlugin
32 */
33 typedef struct {
34 /* implements bus listener interface */
35 listener_t listener;
36 /* IKE_SA we are listening on */
37 ike_sa_t *ike_sa;
38 /* backref to public plugin */
39 NMVPNPlugin *plugin;
40 /* credentials to use for authentication */
41 nm_creds_t *creds;
42 /* attribute handler for DNS/NBNS server information */
43 nm_handler_t *handler;
44 /* name of the connection */
45 char *name;
46 } NMStrongswanPluginPrivate;
47
48 #define NM_STRONGSWAN_PLUGIN_GET_PRIVATE(o) \
49 (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
50 NM_TYPE_STRONGSWAN_PLUGIN, NMStrongswanPluginPrivate))
51
52 /**
53 * convert enumerated handler chunks to a UINT_ARRAY GValue
54 */
55 static GValue* handler_to_val(nm_handler_t *handler,
56 configuration_attribute_type_t type)
57 {
58 GValue *val;
59 GArray *array;
60 enumerator_t *enumerator;
61 chunk_t chunk;
62
63 enumerator = handler->create_enumerator(handler, type);
64 array = g_array_new (FALSE, TRUE, sizeof (guint32));
65 while (enumerator->enumerate(enumerator, &chunk))
66 {
67 g_array_append_val (array, *(u_int32_t*)chunk.ptr);
68 }
69 enumerator->destroy(enumerator);
70 val = g_slice_new0 (GValue);
71 g_value_init (val, DBUS_TYPE_G_UINT_ARRAY);
72 g_value_set_boxed (val, array);
73
74 return val;
75 }
76
77 /**
78 * signal IPv4 config to NM, set connection as established
79 */
80 static void signal_ipv4_config(NMVPNPlugin *plugin,
81 ike_sa_t *ike_sa, child_sa_t *child_sa)
82 {
83 GValue *val;
84 GHashTable *config;
85 host_t *me, *other;
86 nm_handler_t *handler;
87
88 config = g_hash_table_new(g_str_hash, g_str_equal);
89 me = ike_sa->get_my_host(ike_sa);
90 other = ike_sa->get_other_host(ike_sa);
91 handler = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin)->handler;
92
93 /* NM requires a tundev, but netkey does not use one. Passing an invalid
94 * iface makes NM complain, but it accepts it without fiddling on eth0. */
95 val = g_slice_new0 (GValue);
96 g_value_init (val, G_TYPE_STRING);
97 g_value_set_string (val, "none");
98 g_hash_table_insert (config, NM_VPN_PLUGIN_IP4_CONFIG_TUNDEV, val);
99
100 val = g_slice_new0(GValue);
101 g_value_init(val, G_TYPE_UINT);
102 g_value_set_uint(val, *(u_int32_t*)me->get_address(me).ptr);
103 g_hash_table_insert(config, NM_VPN_PLUGIN_IP4_CONFIG_ADDRESS, val);
104
105 val = g_slice_new0(GValue);
106 g_value_init(val, G_TYPE_UINT);
107 g_value_set_uint(val, me->get_address(me).len * 8);
108 g_hash_table_insert(config, NM_VPN_PLUGIN_IP4_CONFIG_PREFIX, val);
109
110 val = handler_to_val(handler, INTERNAL_IP4_DNS);
111 g_hash_table_insert(config, NM_VPN_PLUGIN_IP4_CONFIG_DNS, val);
112
113 val = handler_to_val(handler, INTERNAL_IP4_NBNS);
114 g_hash_table_insert(config, NM_VPN_PLUGIN_IP4_CONFIG_NBNS, val);
115
116 handler->reset(handler);
117
118 nm_vpn_plugin_set_ip4_config(plugin, config);
119 }
120
121 /**
122 * signal failure to NM, connecting failed
123 */
124 static void signal_failure(NMVPNPlugin *plugin, NMVPNPluginFailure failure)
125 {
126 nm_handler_t *handler = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin)->handler;
127
128 handler->reset(handler);
129
130 /* TODO: NM does not handle this failure!? */
131 nm_vpn_plugin_failure(plugin, failure);
132 nm_vpn_plugin_set_state(plugin, NM_VPN_SERVICE_STATE_STOPPED);
133 }
134
135 /**
136 * Implementation of listener_t.ike_state_change
137 */
138 static bool ike_state_change(listener_t *listener, ike_sa_t *ike_sa,
139 ike_sa_state_t state)
140 {
141 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
142
143 if (private->ike_sa == ike_sa && state == IKE_DESTROYING)
144 {
145 signal_failure(private->plugin, NM_VPN_PLUGIN_FAILURE_LOGIN_FAILED);
146 return FALSE;
147 }
148 return TRUE;
149 }
150
151 /**
152 * Implementation of listener_t.child_state_change
153 */
154 static bool child_state_change(listener_t *listener, ike_sa_t *ike_sa,
155 child_sa_t *child_sa, child_sa_state_t state)
156 {
157 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
158
159 if (private->ike_sa == ike_sa && state == CHILD_DESTROYING)
160 {
161 signal_failure(private->plugin, NM_VPN_PLUGIN_FAILURE_CONNECT_FAILED);
162 return FALSE;
163 }
164 return TRUE;
165 }
166
167 /**
168 * Implementation of listener_t.child_updown
169 */
170 static bool child_updown(listener_t *listener, ike_sa_t *ike_sa,
171 child_sa_t *child_sa, bool up)
172 {
173 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
174
175 if (private->ike_sa == ike_sa)
176 {
177 if (up)
178 { /* disable initiate-failure-detection hooks */
179 private->listener.ike_state_change = NULL;
180 private->listener.child_state_change = NULL;
181 signal_ipv4_config(private->plugin, ike_sa, child_sa);
182 }
183 else
184 {
185 signal_failure(private->plugin, NM_VPN_PLUGIN_FAILURE_CONNECT_FAILED);
186 return FALSE;
187 }
188 }
189 return TRUE;
190 }
191
192 /**
193 * Implementation of listener_t.ike_rekey
194 */
195 static bool ike_rekey(listener_t *listener, ike_sa_t *old, ike_sa_t *new)
196 {
197 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
198
199 if (private->ike_sa == old)
200 { /* follow a rekeyed IKE_SA */
201 private->ike_sa = new;
202 }
203 return TRUE;
204 }
205
206 /**
207 * Connect function called from NM via DBUS
208 */
209 static gboolean connect_(NMVPNPlugin *plugin, NMConnection *connection,
210 GError **err)
211 {
212 NMStrongswanPluginPrivate *priv;
213 NMSettingConnection *conn;
214 NMSettingVPN *vpn;
215 identification_t *user = NULL, *gateway;
216 const char *address, *str;
217 bool virtual, encap, ipcomp;
218 ike_cfg_t *ike_cfg;
219 peer_cfg_t *peer_cfg;
220 child_cfg_t *child_cfg;
221 traffic_selector_t *ts;
222 ike_sa_t *ike_sa;
223 auth_cfg_t *auth;
224 auth_class_t auth_class = AUTH_CLASS_EAP;
225 certificate_t *cert = NULL;
226 x509_t *x509;
227 bool agent = FALSE;
228 lifetime_cfg_t lifetime = {
229 .time = {
230 .life = 10800 /* 3h */,
231 .rekey = 10200 /* 2h50min */,
232 .jitter = 300 /* 5min */
233 }
234 };
235
236 /**
237 * Read parameters
238 */
239 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
240 conn = NM_SETTING_CONNECTION(nm_connection_get_setting(connection,
241 NM_TYPE_SETTING_CONNECTION));
242 vpn = NM_SETTING_VPN(nm_connection_get_setting(connection,
243 NM_TYPE_SETTING_VPN));
244 if (priv->name)
245 {
246 free(priv->name);
247 }
248 priv->name = strdup(nm_setting_connection_get_id(conn));
249 DBG1(DBG_CFG, "received initiate for NetworkManager connection %s",
250 priv->name);
251 DBG4(DBG_CFG, "%s",
252 nm_setting_to_string(NM_SETTING(vpn)));
253 address = nm_setting_vpn_get_data_item(vpn, "address");
254 if (!address || !*address)
255 {
256 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
257 "Gateway address missing.");
258 return FALSE;
259 }
260 str = nm_setting_vpn_get_data_item(vpn, "virtual");
261 virtual = str && streq(str, "yes");
262 str = nm_setting_vpn_get_data_item(vpn, "encap");
263 encap = str && streq(str, "yes");
264 str = nm_setting_vpn_get_data_item(vpn, "ipcomp");
265 ipcomp = str && streq(str, "yes");
266 str = nm_setting_vpn_get_data_item(vpn, "method");
267 if (str)
268 {
269 if (streq(str, "psk"))
270 {
271 auth_class = AUTH_CLASS_PSK;
272 }
273 else if (streq(str, "agent"))
274 {
275 auth_class = AUTH_CLASS_PUBKEY;
276 agent = TRUE;
277 }
278 else if (streq(str, "key"))
279 {
280 auth_class = AUTH_CLASS_PUBKEY;
281 }
282 }
283
284 /**
285 * Register credentials
286 */
287 priv->creds->clear(priv->creds);
288
289 /* gateway/CA cert */
290 str = nm_setting_vpn_get_data_item(vpn, "certificate");
291 if (str)
292 {
293 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
294 BUILD_FROM_FILE, str, BUILD_END);
295 priv->creds->set_certificate(priv->creds, cert);
296 }
297 if (!cert)
298 {
299 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
300 "Loading gateway certificate failed.");
301 return FALSE;
302 }
303 x509 = (x509_t*)cert;
304 if (x509->get_flags(x509) & X509_CA)
305 { /* If the user configured a CA certificate, we use the IP/DNS
306 * of the gateway as its identity. This identity will be used for
307 * certificate lookup and requires the configured IP/DNS to be
308 * included in the gateway certificate. */
309 gateway = identification_create_from_string((char*)address);
310 DBG1(DBG_CFG, "using CA certificate, gateway identity '%Y'", gateway);
311 }
312 else
313 { /* For a gateway certificate, we use the cert subject as identity. */
314 gateway = cert->get_subject(cert);
315 gateway = gateway->clone(gateway);
316 DBG1(DBG_CFG, "using gateway certificate, identity '%Y'", gateway);
317 }
318
319 if (auth_class == AUTH_CLASS_EAP)
320 {
321 /* username/password authentication ... */
322 str = nm_setting_vpn_get_data_item(vpn, "user");
323 if (str)
324 {
325 user = identification_create_from_string((char*)str);
326 str = nm_setting_vpn_get_secret(vpn, "password");
327 priv->creds->set_username_password(priv->creds, user, (char*)str);
328 }
329 }
330
331 if (auth_class == AUTH_CLASS_PUBKEY)
332 {
333 /* ... or certificate/private key authenitcation */
334 str = nm_setting_vpn_get_data_item(vpn, "usercert");
335 if (str)
336 {
337 public_key_t *public;
338 private_key_t *private = NULL;
339
340 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
341 BUILD_FROM_FILE, str, BUILD_END);
342 if (!cert)
343 {
344 g_set_error(err, NM_VPN_PLUGIN_ERROR,
345 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
346 "Loading peer certificate failed.");
347 gateway->destroy(gateway);
348 return FALSE;
349 }
350 /* try agent */
351 str = nm_setting_vpn_get_secret(vpn, "agent");
352 if (agent && str)
353 {
354 public = cert->get_public_key(cert);
355 if (public)
356 {
357 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
358 public->get_type(public),
359 BUILD_AGENT_SOCKET, str,
360 BUILD_PUBLIC_KEY, public,
361 BUILD_END);
362 public->destroy(public);
363 }
364 if (!private)
365 {
366 g_set_error(err, NM_VPN_PLUGIN_ERROR,
367 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
368 "Connecting to SSH agent failed.");
369 }
370 }
371 /* ... or key file */
372 str = nm_setting_vpn_get_data_item(vpn, "userkey");
373 if (!agent && str)
374 {
375 chunk_t secret;
376
377 secret.ptr = (char*)nm_setting_vpn_get_secret(vpn, "password");
378 if (secret.ptr)
379 {
380 secret.len = strlen(secret.ptr);
381 }
382 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
383 KEY_RSA, BUILD_FROM_FILE, str,
384 BUILD_PASSPHRASE, secret, BUILD_END);
385 if (!private)
386 {
387 g_set_error(err, NM_VPN_PLUGIN_ERROR,
388 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
389 "Loading private key failed.");
390 }
391 }
392 if (private)
393 {
394 user = cert->get_subject(cert);
395 user = user->clone(user);
396 priv->creds->set_cert_and_key(priv->creds, cert, private);
397 }
398 else
399 {
400 DESTROY_IF(cert);
401 gateway->destroy(gateway);
402 return FALSE;
403 }
404 }
405 }
406
407 if (!user)
408 {
409 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
410 "Configuration parameters missing.");
411 gateway->destroy(gateway);
412 return FALSE;
413 }
414
415 /**
416 * Set up configurations
417 */
418 ike_cfg = ike_cfg_create(TRUE, encap, "0.0.0.0", (char*)address);
419 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
420 peer_cfg = peer_cfg_create(priv->name, 2, ike_cfg,
421 CERT_SEND_IF_ASKED, UNIQUE_REPLACE, 1, /* keyingtries */
422 36000, 0, /* rekey 10h, reauth none */
423 600, 600, /* jitter, over 10min */
424 TRUE, 0, /* mobike, DPD */
425 virtual ? host_create_from_string("0.0.0.0", 0) : NULL,
426 NULL, FALSE, NULL, NULL); /* pool, mediation */
427 auth = auth_cfg_create();
428 auth->add(auth, AUTH_RULE_AUTH_CLASS, auth_class);
429 auth->add(auth, AUTH_RULE_IDENTITY, user);
430 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
431 auth = auth_cfg_create();
432 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
433 auth->add(auth, AUTH_RULE_IDENTITY, gateway);
434 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
435
436 child_cfg = child_cfg_create(priv->name, &lifetime,
437 NULL, TRUE, MODE_TUNNEL, /* updown, hostaccess */
438 ACTION_NONE, ACTION_NONE, ipcomp);
439 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
440 ts = traffic_selector_create_dynamic(0, 0, 65535);
441 child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
442 ts = traffic_selector_create_from_string(0, TS_IPV4_ADDR_RANGE,
443 "0.0.0.0", 0,
444 "255.255.255.255", 65535);
445 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
446 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
447
448 /**
449 * Prepare IKE_SA
450 */
451 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
452 peer_cfg);
453 if (!ike_sa->get_peer_cfg(ike_sa))
454 {
455 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
456 }
457 else
458 {
459 peer_cfg->destroy(peer_cfg);
460 }
461
462 /**
463 * Register listener, enable initiate-failure-detection hooks
464 */
465 priv->ike_sa = ike_sa;
466 priv->listener.ike_state_change = ike_state_change;
467 priv->listener.child_state_change = child_state_change;
468 charon->bus->add_listener(charon->bus, &priv->listener);
469
470 /**
471 * Initiate
472 */
473 if (ike_sa->initiate(ike_sa, child_cfg, 0, NULL, NULL) != SUCCESS)
474 {
475 charon->bus->remove_listener(charon->bus, &priv->listener);
476 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
477
478 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
479 "Initiating failed.");
480 return FALSE;
481 }
482 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
483 return TRUE;
484 }
485
486 /**
487 * NeedSecrets called from NM via DBUS
488 */
489 static gboolean need_secrets(NMVPNPlugin *plugin, NMConnection *connection,
490 char **setting_name, GError **error)
491 {
492 NMSettingVPN *settings;
493 const char *method, *path;
494
495 settings = NM_SETTING_VPN(nm_connection_get_setting(connection,
496 NM_TYPE_SETTING_VPN));
497 method = nm_setting_vpn_get_data_item(settings, "method");
498 if (method)
499 {
500 if (streq(method, "eap"))
501 {
502 if (nm_setting_vpn_get_secret(settings, "password"))
503 {
504 return FALSE;
505 }
506 }
507 else if (streq(method, "agent"))
508 {
509 if (nm_setting_vpn_get_secret(settings, "agent"))
510 {
511 return FALSE;
512 }
513 }
514 else if (streq(method, "key"))
515 {
516 path = nm_setting_vpn_get_data_item(settings, "userkey");
517 if (path)
518 {
519 private_key_t *key;
520 chunk_t secret;
521
522 secret.ptr = (char*)nm_setting_vpn_get_secret(settings, "password");
523 if (secret.ptr)
524 {
525 secret.len = strlen(secret.ptr);
526 }
527 /* try to load/decrypt the private key */
528 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
529 KEY_RSA, BUILD_FROM_FILE, path,
530 BUILD_PASSPHRASE, secret, BUILD_END);
531 if (key)
532 {
533 key->destroy(key);
534 return FALSE;
535 }
536 }
537 }
538 }
539 *setting_name = NM_SETTING_VPN_SETTING_NAME;
540 return TRUE;
541 }
542
543 /**
544 * Disconnect called from NM via DBUS
545 */
546 static gboolean disconnect(NMVPNPlugin *plugin, GError **err)
547 {
548 NMStrongswanPluginPrivate *priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
549 enumerator_t *enumerator;
550 ike_sa_t *ike_sa;
551 u_int id;
552
553 /* our ike_sa pointer might be invalid, lookup sa */
554 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
555 while (enumerator->enumerate(enumerator, &ike_sa))
556 {
557 if (priv->ike_sa == ike_sa)
558 {
559 id = ike_sa->get_unique_id(ike_sa);
560 enumerator->destroy(enumerator);
561 charon->controller->terminate_ike(charon->controller, id,
562 controller_cb_empty, NULL);
563 return TRUE;
564 }
565 }
566 enumerator->destroy(enumerator);
567
568 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_GENERAL,
569 "Connection not found.");
570 return FALSE;
571 }
572
573 /**
574 * Initializer
575 */
576 static void nm_strongswan_plugin_init(NMStrongswanPlugin *plugin)
577 {
578 NMStrongswanPluginPrivate *priv;
579
580 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
581 priv->plugin = NM_VPN_PLUGIN(plugin);
582 memset(&priv->listener.log, 0, sizeof(listener_t));
583 priv->listener.child_updown = child_updown;
584 priv->listener.ike_rekey = ike_rekey;
585 }
586
587 /**
588 * Class constructor
589 */
590 static void nm_strongswan_plugin_class_init(
591 NMStrongswanPluginClass *strongswan_class)
592 {
593 NMVPNPluginClass *parent_class = NM_VPN_PLUGIN_CLASS(strongswan_class);
594
595 g_type_class_add_private(G_OBJECT_CLASS(strongswan_class),
596 sizeof(NMStrongswanPluginPrivate));
597 parent_class->connect = connect_;
598 parent_class->need_secrets = need_secrets;
599 parent_class->disconnect = disconnect;
600 }
601
602 /**
603 * Object constructor
604 */
605 NMStrongswanPlugin *nm_strongswan_plugin_new(nm_creds_t *creds,
606 nm_handler_t *handler)
607 {
608 NMStrongswanPlugin *plugin = (NMStrongswanPlugin *)g_object_new (
609 NM_TYPE_STRONGSWAN_PLUGIN,
610 NM_VPN_PLUGIN_DBUS_SERVICE_NAME, NM_DBUS_SERVICE_STRONGSWAN,
611 NULL);
612 if (plugin)
613 {
614 NMStrongswanPluginPrivate *priv;
615
616 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
617 priv->creds = creds;
618 priv->handler = handler;
619 priv->name = NULL;
620 }
621 return plugin;
622 }
623