Adapted the config backends to the new lifetime configuration.
[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 lifetime_cfg_t *lifetime;
222 traffic_selector_t *ts;
223 ike_sa_t *ike_sa;
224 auth_cfg_t *auth;
225 auth_class_t auth_class = AUTH_CLASS_EAP;
226 certificate_t *cert = NULL;
227 x509_t *x509;
228 bool agent = FALSE;
229
230 /**
231 * Read parameters
232 */
233 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
234 conn = NM_SETTING_CONNECTION(nm_connection_get_setting(connection,
235 NM_TYPE_SETTING_CONNECTION));
236 vpn = NM_SETTING_VPN(nm_connection_get_setting(connection,
237 NM_TYPE_SETTING_VPN));
238 if (priv->name)
239 {
240 free(priv->name);
241 }
242 priv->name = strdup(nm_setting_connection_get_id(conn));
243 DBG1(DBG_CFG, "received initiate for NetworkManager connection %s",
244 priv->name);
245 DBG4(DBG_CFG, "%s",
246 nm_setting_to_string(NM_SETTING(vpn)));
247 address = nm_setting_vpn_get_data_item(vpn, "address");
248 if (!address || !*address)
249 {
250 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
251 "Gateway address missing.");
252 return FALSE;
253 }
254 str = nm_setting_vpn_get_data_item(vpn, "virtual");
255 virtual = str && streq(str, "yes");
256 str = nm_setting_vpn_get_data_item(vpn, "encap");
257 encap = str && streq(str, "yes");
258 str = nm_setting_vpn_get_data_item(vpn, "ipcomp");
259 ipcomp = str && streq(str, "yes");
260 str = nm_setting_vpn_get_data_item(vpn, "method");
261 if (str)
262 {
263 if (streq(str, "psk"))
264 {
265 auth_class = AUTH_CLASS_PSK;
266 }
267 else if (streq(str, "agent"))
268 {
269 auth_class = AUTH_CLASS_PUBKEY;
270 agent = TRUE;
271 }
272 else if (streq(str, "key"))
273 {
274 auth_class = AUTH_CLASS_PUBKEY;
275 }
276 }
277
278 /**
279 * Register credentials
280 */
281 priv->creds->clear(priv->creds);
282
283 /* gateway/CA cert */
284 str = nm_setting_vpn_get_data_item(vpn, "certificate");
285 if (str)
286 {
287 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
288 BUILD_FROM_FILE, str, BUILD_END);
289 priv->creds->set_certificate(priv->creds, cert);
290 }
291 if (!cert)
292 {
293 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
294 "Loading gateway certificate failed.");
295 return FALSE;
296 }
297 x509 = (x509_t*)cert;
298 if (x509->get_flags(x509) & X509_CA)
299 { /* If the user configured a CA certificate, we use the IP/DNS
300 * of the gateway as its identity. This identity will be used for
301 * certificate lookup and requires the configured IP/DNS to be
302 * included in the gateway certificate. */
303 gateway = identification_create_from_string((char*)address);
304 DBG1(DBG_CFG, "using CA certificate, gateway identity '%Y'", gateway);
305 }
306 else
307 { /* For a gateway certificate, we use the cert subject as identity. */
308 gateway = cert->get_subject(cert);
309 gateway = gateway->clone(gateway);
310 DBG1(DBG_CFG, "using gateway certificate, identity '%Y'", gateway);
311 }
312
313 if (auth_class == AUTH_CLASS_EAP)
314 {
315 /* username/password authentication ... */
316 str = nm_setting_vpn_get_data_item(vpn, "user");
317 if (str)
318 {
319 user = identification_create_from_string((char*)str);
320 str = nm_setting_vpn_get_secret(vpn, "password");
321 priv->creds->set_username_password(priv->creds, user, (char*)str);
322 }
323 }
324
325 if (auth_class == AUTH_CLASS_PUBKEY)
326 {
327 /* ... or certificate/private key authenitcation */
328 str = nm_setting_vpn_get_data_item(vpn, "usercert");
329 if (str)
330 {
331 public_key_t *public;
332 private_key_t *private = NULL;
333
334 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
335 BUILD_FROM_FILE, str, BUILD_END);
336 if (!cert)
337 {
338 g_set_error(err, NM_VPN_PLUGIN_ERROR,
339 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
340 "Loading peer certificate failed.");
341 gateway->destroy(gateway);
342 return FALSE;
343 }
344 /* try agent */
345 str = nm_setting_vpn_get_secret(vpn, "agent");
346 if (agent && str)
347 {
348 public = cert->get_public_key(cert);
349 if (public)
350 {
351 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
352 public->get_type(public),
353 BUILD_AGENT_SOCKET, str,
354 BUILD_PUBLIC_KEY, public,
355 BUILD_END);
356 public->destroy(public);
357 }
358 if (!private)
359 {
360 g_set_error(err, NM_VPN_PLUGIN_ERROR,
361 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
362 "Connecting to SSH agent failed.");
363 }
364 }
365 /* ... or key file */
366 str = nm_setting_vpn_get_data_item(vpn, "userkey");
367 if (!agent && str)
368 {
369 chunk_t secret;
370
371 secret.ptr = (char*)nm_setting_vpn_get_secret(vpn, "password");
372 if (secret.ptr)
373 {
374 secret.len = strlen(secret.ptr);
375 }
376 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
377 KEY_RSA, BUILD_FROM_FILE, str,
378 BUILD_PASSPHRASE, secret, BUILD_END);
379 if (!private)
380 {
381 g_set_error(err, NM_VPN_PLUGIN_ERROR,
382 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
383 "Loading private key failed.");
384 }
385 }
386 if (private)
387 {
388 user = cert->get_subject(cert);
389 user = user->clone(user);
390 priv->creds->set_cert_and_key(priv->creds, cert, private);
391 }
392 else
393 {
394 DESTROY_IF(cert);
395 gateway->destroy(gateway);
396 return FALSE;
397 }
398 }
399 }
400
401 if (!user)
402 {
403 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
404 "Configuration parameters missing.");
405 gateway->destroy(gateway);
406 return FALSE;
407 }
408
409 /**
410 * Set up configurations
411 */
412 ike_cfg = ike_cfg_create(TRUE, encap, "0.0.0.0", (char*)address);
413 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
414 peer_cfg = peer_cfg_create(priv->name, 2, ike_cfg,
415 CERT_SEND_IF_ASKED, UNIQUE_REPLACE, 1, /* keyingtries */
416 36000, 0, /* rekey 10h, reauth none */
417 600, 600, /* jitter, over 10min */
418 TRUE, 0, /* mobike, DPD */
419 virtual ? host_create_from_string("0.0.0.0", 0) : NULL,
420 NULL, FALSE, NULL, NULL); /* pool, mediation */
421 auth = auth_cfg_create();
422 auth->add(auth, AUTH_RULE_AUTH_CLASS, auth_class);
423 auth->add(auth, AUTH_RULE_IDENTITY, user);
424 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
425 auth = auth_cfg_create();
426 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
427 auth->add(auth, AUTH_RULE_IDENTITY, gateway);
428 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
429
430 lifetime = lifetime_cfg_create_time(10800 /* 3h */, 10200 /* 2h50min */,
431 300 /* 5min */);
432
433 child_cfg = child_cfg_create(priv->name, lifetime,
434 NULL, TRUE, MODE_TUNNEL, /* updown, hostaccess */
435 ACTION_NONE, ACTION_NONE, ipcomp);
436 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
437 ts = traffic_selector_create_dynamic(0, 0, 65535);
438 child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
439 ts = traffic_selector_create_from_string(0, TS_IPV4_ADDR_RANGE,
440 "0.0.0.0", 0,
441 "255.255.255.255", 65535);
442 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
443 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
444
445 /**
446 * Prepare IKE_SA
447 */
448 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
449 peer_cfg);
450 if (!ike_sa->get_peer_cfg(ike_sa))
451 {
452 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
453 }
454 else
455 {
456 peer_cfg->destroy(peer_cfg);
457 }
458
459 /**
460 * Register listener, enable initiate-failure-detection hooks
461 */
462 priv->ike_sa = ike_sa;
463 priv->listener.ike_state_change = ike_state_change;
464 priv->listener.child_state_change = child_state_change;
465 charon->bus->add_listener(charon->bus, &priv->listener);
466
467 /**
468 * Initiate
469 */
470 if (ike_sa->initiate(ike_sa, child_cfg, 0, NULL, NULL) != SUCCESS)
471 {
472 charon->bus->remove_listener(charon->bus, &priv->listener);
473 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
474
475 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
476 "Initiating failed.");
477 return FALSE;
478 }
479 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
480 return TRUE;
481 }
482
483 /**
484 * NeedSecrets called from NM via DBUS
485 */
486 static gboolean need_secrets(NMVPNPlugin *plugin, NMConnection *connection,
487 char **setting_name, GError **error)
488 {
489 NMSettingVPN *settings;
490 const char *method, *path;
491
492 settings = NM_SETTING_VPN(nm_connection_get_setting(connection,
493 NM_TYPE_SETTING_VPN));
494 method = nm_setting_vpn_get_data_item(settings, "method");
495 if (method)
496 {
497 if (streq(method, "eap"))
498 {
499 if (nm_setting_vpn_get_secret(settings, "password"))
500 {
501 return FALSE;
502 }
503 }
504 else if (streq(method, "agent"))
505 {
506 if (nm_setting_vpn_get_secret(settings, "agent"))
507 {
508 return FALSE;
509 }
510 }
511 else if (streq(method, "key"))
512 {
513 path = nm_setting_vpn_get_data_item(settings, "userkey");
514 if (path)
515 {
516 private_key_t *key;
517 chunk_t secret;
518
519 secret.ptr = (char*)nm_setting_vpn_get_secret(settings, "password");
520 if (secret.ptr)
521 {
522 secret.len = strlen(secret.ptr);
523 }
524 /* try to load/decrypt the private key */
525 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
526 KEY_RSA, BUILD_FROM_FILE, path,
527 BUILD_PASSPHRASE, secret, BUILD_END);
528 if (key)
529 {
530 key->destroy(key);
531 return FALSE;
532 }
533 }
534 }
535 }
536 *setting_name = NM_SETTING_VPN_SETTING_NAME;
537 return TRUE;
538 }
539
540 /**
541 * Disconnect called from NM via DBUS
542 */
543 static gboolean disconnect(NMVPNPlugin *plugin, GError **err)
544 {
545 NMStrongswanPluginPrivate *priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
546 enumerator_t *enumerator;
547 ike_sa_t *ike_sa;
548 u_int id;
549
550 /* our ike_sa pointer might be invalid, lookup sa */
551 enumerator = charon->controller->create_ike_sa_enumerator(charon->controller);
552 while (enumerator->enumerate(enumerator, &ike_sa))
553 {
554 if (priv->ike_sa == ike_sa)
555 {
556 id = ike_sa->get_unique_id(ike_sa);
557 enumerator->destroy(enumerator);
558 charon->controller->terminate_ike(charon->controller, id,
559 controller_cb_empty, NULL);
560 return TRUE;
561 }
562 }
563 enumerator->destroy(enumerator);
564
565 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_GENERAL,
566 "Connection not found.");
567 return FALSE;
568 }
569
570 /**
571 * Initializer
572 */
573 static void nm_strongswan_plugin_init(NMStrongswanPlugin *plugin)
574 {
575 NMStrongswanPluginPrivate *priv;
576
577 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
578 priv->plugin = NM_VPN_PLUGIN(plugin);
579 memset(&priv->listener.log, 0, sizeof(listener_t));
580 priv->listener.child_updown = child_updown;
581 priv->listener.ike_rekey = ike_rekey;
582 }
583
584 /**
585 * Class constructor
586 */
587 static void nm_strongswan_plugin_class_init(
588 NMStrongswanPluginClass *strongswan_class)
589 {
590 NMVPNPluginClass *parent_class = NM_VPN_PLUGIN_CLASS(strongswan_class);
591
592 g_type_class_add_private(G_OBJECT_CLASS(strongswan_class),
593 sizeof(NMStrongswanPluginPrivate));
594 parent_class->connect = connect_;
595 parent_class->need_secrets = need_secrets;
596 parent_class->disconnect = disconnect;
597 }
598
599 /**
600 * Object constructor
601 */
602 NMStrongswanPlugin *nm_strongswan_plugin_new(nm_creds_t *creds,
603 nm_handler_t *handler)
604 {
605 NMStrongswanPlugin *plugin = (NMStrongswanPlugin *)g_object_new (
606 NM_TYPE_STRONGSWAN_PLUGIN,
607 NM_VPN_PLUGIN_DBUS_SERVICE_NAME, NM_DBUS_SERVICE_STRONGSWAN,
608 NULL);
609 if (plugin)
610 {
611 NMStrongswanPluginPrivate *priv;
612
613 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
614 priv->creds = creds;
615 priv->handler = handler;
616 priv->name = NULL;
617 }
618 return plugin;
619 }
620