706e482a21700e649fc9a85adf6646090595360a
[strongswan.git] / src / charon-nm / nm / nm_service.c
1 /*
2 * Copyright (C) 2017 Lubomir Rintel
3 *
4 * Copyright (C) 2013-2019 Tobias Brunner
5 * Copyright (C) 2008-2009 Martin Willi
6 * HSR Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "nm_service.h"
20
21 #include <daemon.h>
22 #include <networking/host.h>
23 #include <utils/identification.h>
24 #include <config/peer_cfg.h>
25 #include <credentials/certificates/x509.h>
26
27 #include <stdio.h>
28
29 /**
30 * Private data of NMStrongswanPlugin
31 */
32 typedef struct {
33 /* implements bus listener interface */
34 listener_t listener;
35 /* IKE_SA we are listening on */
36 ike_sa_t *ike_sa;
37 /* backref to public plugin */
38 NMVpnServicePlugin *plugin;
39 /* credentials to use for authentication */
40 nm_creds_t *creds;
41 /* attribute handler for DNS/NBNS server information */
42 nm_handler_t *handler;
43 /* name of the connection */
44 char *name;
45 } NMStrongswanPluginPrivate;
46
47 G_DEFINE_TYPE_WITH_PRIVATE(NMStrongswanPlugin, nm_strongswan_plugin, NM_TYPE_VPN_SERVICE_PLUGIN)
48
49 #define NM_STRONGSWAN_PLUGIN_GET_PRIVATE(o) \
50 ((NMStrongswanPluginPrivate*) \
51 nm_strongswan_plugin_get_instance_private (o))
52
53 /**
54 * Convert an address chunk to a GValue
55 */
56 static GVariant *addr_to_variant(chunk_t addr)
57 {
58 GVariantBuilder builder;
59 int i;
60
61 switch (addr.len)
62 {
63 case 4:
64 return g_variant_new_uint32 (*(uint32_t*)addr.ptr);
65 case 16:
66 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ay"));
67 for (i = 0; i < addr.len; i++)
68 {
69 g_variant_builder_add (&builder, "y", addr.ptr[i]);
70
71 }
72 return g_variant_builder_end (&builder);
73 default:
74 return NULL;
75 }
76 }
77
78 /**
79 * Convert a host to a GValue
80 */
81 static GVariant *host_to_variant(host_t *host)
82 {
83 return addr_to_variant(host->get_address(host));
84 }
85
86 /**
87 * Convert enumerated handler chunks to a GValue
88 */
89 static GVariant* handler_to_variant(nm_handler_t *handler, char *variant_type,
90 configuration_attribute_type_t type)
91 {
92 GVariantBuilder builder;
93 enumerator_t *enumerator;
94 chunk_t *chunk;
95
96 g_variant_builder_init (&builder, G_VARIANT_TYPE (variant_type));
97
98 enumerator = handler->create_enumerator(handler, type);
99 while (enumerator->enumerate(enumerator, &chunk))
100 {
101 g_variant_builder_add_value (&builder, addr_to_variant(*chunk));
102 }
103 enumerator->destroy(enumerator);
104
105 return g_variant_builder_end (&builder);
106 }
107
108 /**
109 * Signal IP config to NM, set connection as established
110 */
111 static void signal_ip_config(NMVpnServicePlugin *plugin,
112 ike_sa_t *ike_sa, child_sa_t *child_sa)
113 {
114 NMStrongswanPlugin *pub = (NMStrongswanPlugin*)plugin;
115 NMStrongswanPluginPrivate *priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(pub);
116 GVariantBuilder builder, ip4builder, ip6builder;
117 GVariant *ip4config, *ip6config;
118 enumerator_t *enumerator;
119 host_t *me, *other, *vip4 = NULL, *vip6 = NULL;
120 nm_handler_t *handler;
121
122 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
123 g_variant_builder_init (&ip4builder, G_VARIANT_TYPE_VARDICT);
124 g_variant_builder_init (&ip6builder, G_VARIANT_TYPE_VARDICT);
125
126 handler = priv->handler;
127
128 /* NM apparently requires to know the gateway */
129 other = ike_sa->get_other_host(ike_sa);
130 g_variant_builder_add (&builder, "{sv}", NM_VPN_PLUGIN_CONFIG_EXT_GATEWAY,
131 host_to_variant(other));
132
133 /* pass the first virtual IPs we got or use the physical IP */
134 enumerator = ike_sa->create_virtual_ip_enumerator(ike_sa, TRUE);
135 while (enumerator->enumerate(enumerator, &me))
136 {
137 switch (me->get_family(me))
138 {
139 case AF_INET:
140 if (!vip4)
141 {
142 vip4 = me;
143 }
144 break;
145 case AF_INET6:
146 if (!vip6)
147 {
148 vip6 = me;
149 }
150 break;
151 }
152 }
153 enumerator->destroy(enumerator);
154 if (!vip4 && !vip6)
155 {
156 me = ike_sa->get_my_host(ike_sa);
157 switch (me->get_family(me))
158 {
159 case AF_INET:
160 vip4 = me;
161 break;
162 case AF_INET6:
163 vip6 = me;
164 break;
165 }
166 }
167
168 if (vip4)
169 {
170 g_variant_builder_add (&ip4builder, "{sv}", NM_VPN_PLUGIN_IP4_CONFIG_ADDRESS,
171 host_to_variant(vip4));
172 g_variant_builder_add (&ip4builder, "{sv}", NM_VPN_PLUGIN_IP4_CONFIG_PREFIX,
173 g_variant_new_uint32 (vip4->get_address(vip4).len * 8));
174
175 /* prevent NM from changing the default route. we set our own route in our
176 * own routing table
177 */
178 g_variant_builder_add (&ip4builder, "{sv}", NM_VPN_PLUGIN_IP4_CONFIG_NEVER_DEFAULT,
179 g_variant_new_boolean (TRUE));
180
181 g_variant_builder_add (&ip4builder, "{sv}", NM_VPN_PLUGIN_IP4_CONFIG_DNS,
182 handler_to_variant(handler, "au", INTERNAL_IP4_DNS));
183
184 g_variant_builder_add (&ip4builder, "{sv}", NM_VPN_PLUGIN_IP4_CONFIG_NBNS,
185 handler_to_variant(handler, "au", INTERNAL_IP4_NBNS));
186 }
187
188 if (vip6)
189 {
190 g_variant_builder_add (&ip6builder, "{sv}", NM_VPN_PLUGIN_IP6_CONFIG_ADDRESS,
191 host_to_variant(vip6));
192 g_variant_builder_add (&ip6builder, "{sv}", NM_VPN_PLUGIN_IP6_CONFIG_PREFIX,
193 g_variant_new_uint32 (vip6->get_address(vip6).len * 8));
194 g_variant_builder_add (&ip6builder, "{sv}", NM_VPN_PLUGIN_IP6_CONFIG_NEVER_DEFAULT,
195 g_variant_new_boolean (TRUE));
196 g_variant_builder_add (&ip6builder, "{sv}", NM_VPN_PLUGIN_IP6_CONFIG_DNS,
197 handler_to_variant(handler, "aay", INTERNAL_IP6_DNS));
198 /* NM_VPN_PLUGIN_IP6_CONFIG_NBNS is not defined */
199 }
200
201 ip4config = g_variant_builder_end (&ip4builder);
202 if (g_variant_n_children (ip4config))
203 {
204 g_variant_builder_add (&builder, "{sv}", NM_VPN_PLUGIN_CONFIG_HAS_IP4,
205 g_variant_new_boolean (TRUE));
206 }
207 else
208 {
209 g_variant_unref (ip4config);
210 ip4config = NULL;
211 }
212
213 ip6config = g_variant_builder_end (&ip6builder);
214 if (g_variant_n_children (ip6config))
215 {
216 g_variant_builder_add (&builder, "{sv}", NM_VPN_PLUGIN_CONFIG_HAS_IP6,
217 g_variant_new_boolean (TRUE));
218 }
219 else
220 {
221 g_variant_unref (ip6config);
222 ip6config = NULL;
223 }
224
225 handler->reset(handler);
226
227 nm_vpn_service_plugin_set_config (plugin, g_variant_builder_end (&builder));
228 if (ip4config)
229 {
230 nm_vpn_service_plugin_set_ip4_config (plugin, ip4config);
231 }
232 if (ip6config)
233 {
234 nm_vpn_service_plugin_set_ip6_config (plugin, ip6config);
235 }
236 }
237
238 /**
239 * signal failure to NM, connecting failed
240 */
241 static void signal_failure(NMVpnServicePlugin *plugin, NMVpnPluginFailure failure)
242 {
243 NMStrongswanPlugin *pub = (NMStrongswanPlugin*)plugin;
244 nm_handler_t *handler = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(pub)->handler;
245
246 handler->reset(handler);
247
248 nm_vpn_service_plugin_failure(plugin, failure);
249 }
250
251 /**
252 * Implementation of listener_t.ike_state_change
253 */
254 static bool ike_state_change(listener_t *listener, ike_sa_t *ike_sa,
255 ike_sa_state_t state)
256 {
257 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
258
259 if (private->ike_sa == ike_sa && state == IKE_DESTROYING)
260 {
261 signal_failure(private->plugin, NM_VPN_PLUGIN_FAILURE_LOGIN_FAILED);
262 return FALSE;
263 }
264 return TRUE;
265 }
266
267 /**
268 * Implementation of listener_t.child_state_change
269 */
270 static bool child_state_change(listener_t *listener, ike_sa_t *ike_sa,
271 child_sa_t *child_sa, child_sa_state_t state)
272 {
273 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
274
275 if (private->ike_sa == ike_sa && state == CHILD_DESTROYING)
276 {
277 signal_failure(private->plugin, NM_VPN_PLUGIN_FAILURE_CONNECT_FAILED);
278 return FALSE;
279 }
280 return TRUE;
281 }
282
283 /**
284 * Implementation of listener_t.child_updown
285 */
286 static bool child_updown(listener_t *listener, ike_sa_t *ike_sa,
287 child_sa_t *child_sa, bool up)
288 {
289 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
290
291 if (private->ike_sa == ike_sa)
292 {
293 if (up)
294 { /* disable initiate-failure-detection hooks */
295 private->listener.ike_state_change = NULL;
296 private->listener.child_state_change = NULL;
297 signal_ip_config(private->plugin, ike_sa, child_sa);
298 }
299 else
300 {
301 signal_failure(private->plugin, NM_VPN_PLUGIN_FAILURE_CONNECT_FAILED);
302 return FALSE;
303 }
304 }
305 return TRUE;
306 }
307
308 /**
309 * Implementation of listener_t.ike_rekey
310 */
311 static bool ike_rekey(listener_t *listener, ike_sa_t *old, ike_sa_t *new)
312 {
313 NMStrongswanPluginPrivate *private = (NMStrongswanPluginPrivate*)listener;
314
315 if (private->ike_sa == old)
316 { /* follow a rekeyed IKE_SA */
317 private->ike_sa = new;
318 }
319 return TRUE;
320 }
321
322 /**
323 * Find a certificate for which we have a private key on a smartcard
324 */
325 static identification_t *find_smartcard_key(NMStrongswanPluginPrivate *priv,
326 char *pin)
327 {
328 enumerator_t *enumerator, *sans;
329 identification_t *id = NULL;
330 certificate_t *cert;
331 x509_t *x509;
332 private_key_t *key;
333 chunk_t keyid;
334
335 enumerator = lib->credmgr->create_cert_enumerator(lib->credmgr,
336 CERT_X509, KEY_ANY, NULL, FALSE);
337 while (enumerator->enumerate(enumerator, &cert))
338 {
339 x509 = (x509_t*)cert;
340
341 /* there might be a lot of certificates, filter them by usage */
342 if ((x509->get_flags(x509) & X509_CLIENT_AUTH) &&
343 !(x509->get_flags(x509) & X509_CA))
344 {
345 keyid = x509->get_subjectKeyIdentifier(x509);
346 if (keyid.ptr)
347 {
348 /* try to find a private key by the certificate keyid */
349 priv->creds->set_pin(priv->creds, keyid, pin);
350 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
351 KEY_ANY, BUILD_PKCS11_KEYID, keyid, BUILD_END);
352 if (key)
353 {
354 /* prefer a more convenient subjectAltName */
355 sans = x509->create_subjectAltName_enumerator(x509);
356 if (!sans->enumerate(sans, &id))
357 {
358 id = cert->get_subject(cert);
359 }
360 id = id->clone(id);
361 sans->destroy(sans);
362
363 DBG1(DBG_CFG, "using smartcard certificate '%Y'", id);
364 priv->creds->set_cert_and_key(priv->creds,
365 cert->get_ref(cert), key);
366 break;
367 }
368 }
369 }
370 }
371 enumerator->destroy(enumerator);
372 return id;
373 }
374
375 /**
376 * Connect function called from NM via DBUS
377 */
378 static gboolean connect_(NMVpnServicePlugin *plugin, NMConnection *connection,
379 GError **err)
380 {
381 NMStrongswanPlugin *pub = (NMStrongswanPlugin*)plugin;
382 NMStrongswanPluginPrivate *priv;
383 NMSettingConnection *conn;
384 NMSettingVpn *vpn;
385 enumerator_t *enumerator;
386 identification_t *user = NULL, *gateway = NULL;
387 const char *str;
388 bool virtual, proposal;
389 proposal_t *prop;
390 ike_cfg_t *ike_cfg;
391 peer_cfg_t *peer_cfg;
392 child_cfg_t *child_cfg;
393 traffic_selector_t *ts;
394 ike_sa_t *ike_sa;
395 auth_cfg_t *auth;
396 auth_class_t auth_class = AUTH_CLASS_EAP;
397 certificate_t *cert = NULL;
398 x509_t *x509;
399 bool agent = FALSE, smartcard = FALSE, loose_gateway_id = FALSE;
400 ike_cfg_create_t ike = {
401 .version = IKEV2,
402 .local = "%any",
403 .local_port = charon->socket->get_port(charon->socket, FALSE),
404 .remote_port = IKEV2_UDP_PORT,
405 .fragmentation = FRAGMENTATION_YES,
406 };
407 peer_cfg_create_t peer = {
408 .cert_policy = CERT_SEND_IF_ASKED,
409 .unique = UNIQUE_REPLACE,
410 .keyingtries = 1,
411 .rekey_time = 36000, /* 10h */
412 .jitter_time = 600, /* 10min */
413 .over_time = 600, /* 10min */
414 };
415 child_cfg_create_t child = {
416 .lifetime = {
417 .time = {
418 .life = 10800 /* 3h */,
419 .rekey = 10200 /* 2h50min */,
420 .jitter = 300 /* 5min */
421 },
422 },
423 .mode = MODE_TUNNEL,
424 };
425
426 /**
427 * Read parameters
428 */
429 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(pub);
430 conn = NM_SETTING_CONNECTION(nm_connection_get_setting(connection,
431 NM_TYPE_SETTING_CONNECTION));
432 vpn = NM_SETTING_VPN(nm_connection_get_setting(connection,
433 NM_TYPE_SETTING_VPN));
434 if (priv->name)
435 {
436 free(priv->name);
437 }
438 priv->name = strdup(nm_setting_connection_get_id(conn));
439 DBG1(DBG_CFG, "received initiate for NetworkManager connection %s",
440 priv->name);
441 DBG4(DBG_CFG, "%s",
442 nm_setting_to_string(NM_SETTING(vpn)));
443 ike.remote = (char*)nm_setting_vpn_get_data_item(vpn, "address");
444 if (!ike.remote || !*ike.remote)
445 {
446 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
447 "Gateway address missing.");
448 return FALSE;
449 }
450 str = nm_setting_vpn_get_data_item(vpn, "server-port");
451 if (str && strlen(str))
452 {
453 ike.remote_port = settings_value_as_int((char*)str, ike.remote_port);
454 }
455 str = nm_setting_vpn_get_data_item(vpn, "virtual");
456 virtual = streq(str, "yes");
457 str = nm_setting_vpn_get_data_item(vpn, "encap");
458 ike.force_encap = streq(str, "yes");
459 str = nm_setting_vpn_get_data_item(vpn, "ipcomp");
460 child.options |= streq(str, "yes") ? OPT_IPCOMP : 0;
461 str = nm_setting_vpn_get_data_item(vpn, "method");
462 if (streq(str, "psk"))
463 {
464 auth_class = AUTH_CLASS_PSK;
465 }
466 else if (streq(str, "agent"))
467 {
468 auth_class = AUTH_CLASS_PUBKEY;
469 agent = TRUE;
470 }
471 else if (streq(str, "key"))
472 {
473 auth_class = AUTH_CLASS_PUBKEY;
474 }
475 else if (streq(str, "smartcard"))
476 {
477 auth_class = AUTH_CLASS_PUBKEY;
478 smartcard = TRUE;
479 }
480
481 /**
482 * Register credentials
483 */
484 priv->creds->clear(priv->creds);
485
486 /* gateway/CA cert */
487 str = nm_setting_vpn_get_data_item(vpn, "certificate");
488 if (str)
489 {
490 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
491 BUILD_FROM_FILE, str, BUILD_END);
492 if (!cert)
493 {
494 g_set_error(err, NM_VPN_PLUGIN_ERROR,
495 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
496 "Loading gateway certificate failed.");
497 return FALSE;
498 }
499 priv->creds->add_certificate(priv->creds, cert);
500 }
501 else
502 {
503 /* no certificate defined, fall back to system-wide CA certificates */
504 priv->creds->load_ca_dir(priv->creds, lib->settings->get_str(
505 lib->settings, "charon-nm.ca_dir", NM_CA_DIR));
506 }
507
508 str = nm_setting_vpn_get_data_item(vpn, "remote-identity");
509 if (str)
510 {
511 gateway = identification_create_from_string((char*)str);
512 }
513 else if (cert)
514 {
515 x509 = (x509_t*)cert;
516 if (!(x509->get_flags(x509) & X509_CA))
517 { /* for server certificates, we use the subject as identity */
518 gateway = cert->get_subject(cert);
519 gateway = gateway->clone(gateway);
520 }
521 }
522 if (!gateway || gateway->get_type(gateway) == ID_ANY)
523 {
524 /* if the user configured a CA certificate (or an invalid identity),
525 * we use the IP/hostname of the server */
526 gateway = identification_create_from_string(ike.remote);
527 loose_gateway_id = TRUE;
528 }
529 DBG1(DBG_CFG, "using gateway identity '%Y'", gateway);
530
531 if (auth_class == AUTH_CLASS_EAP ||
532 auth_class == AUTH_CLASS_PSK)
533 {
534 /* username/password or PSK authentication ... */
535 str = nm_setting_vpn_get_data_item(vpn, "user");
536 if (str)
537 {
538 user = identification_create_from_string((char*)str);
539 str = nm_setting_vpn_get_secret(vpn, "password");
540 if (auth_class == AUTH_CLASS_PSK &&
541 strlen(str) < 20)
542 {
543 g_set_error(err, NM_VPN_PLUGIN_ERROR,
544 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
545 "pre-shared key is too short.");
546 gateway->destroy(gateway);
547 user->destroy(user);
548 return FALSE;
549 }
550 priv->creds->set_username_password(priv->creds, user, (char*)str);
551 }
552 }
553
554 if (auth_class == AUTH_CLASS_PUBKEY)
555 {
556 if (smartcard)
557 {
558 char *pin;
559
560 pin = (char*)nm_setting_vpn_get_secret(vpn, "password");
561 if (pin)
562 {
563 user = find_smartcard_key(priv, pin);
564 }
565 if (!user)
566 {
567 g_set_error(err, NM_VPN_PLUGIN_ERROR,
568 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
569 "no usable smartcard certificate found.");
570 gateway->destroy(gateway);
571 return FALSE;
572 }
573 }
574 /* ... or certificate/private key authentication */
575 else if ((str = nm_setting_vpn_get_data_item(vpn, "usercert")))
576 {
577 public_key_t *public;
578 private_key_t *private = NULL;
579
580 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
581 BUILD_FROM_FILE, str, BUILD_END);
582 if (!cert)
583 {
584 g_set_error(err, NM_VPN_PLUGIN_ERROR,
585 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
586 "Loading peer certificate failed.");
587 gateway->destroy(gateway);
588 return FALSE;
589 }
590 /* try agent */
591 str = nm_setting_vpn_get_secret(vpn, "agent");
592 if (agent && str)
593 {
594 public = cert->get_public_key(cert);
595 if (public)
596 {
597 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
598 public->get_type(public),
599 BUILD_AGENT_SOCKET, str,
600 BUILD_PUBLIC_KEY, public,
601 BUILD_END);
602 public->destroy(public);
603 }
604 if (!private)
605 {
606 g_set_error(err, NM_VPN_PLUGIN_ERROR,
607 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
608 "Connecting to SSH agent failed.");
609 }
610 }
611 /* ... or key file */
612 str = nm_setting_vpn_get_data_item(vpn, "userkey");
613 if (!agent && str)
614 {
615 char *secret;
616
617 secret = (char*)nm_setting_vpn_get_secret(vpn, "password");
618 if (secret)
619 {
620 priv->creds->set_key_password(priv->creds, secret);
621 }
622 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
623 KEY_ANY, BUILD_FROM_FILE, str, BUILD_END);
624 if (!private)
625 {
626 g_set_error(err, NM_VPN_PLUGIN_ERROR,
627 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
628 "Loading private key failed.");
629 }
630 }
631 if (private)
632 {
633 user = cert->get_subject(cert);
634 user = user->clone(user);
635 priv->creds->set_cert_and_key(priv->creds, cert, private);
636 }
637 else
638 {
639 DESTROY_IF(cert);
640 gateway->destroy(gateway);
641 return FALSE;
642 }
643 }
644 }
645
646 if (!user)
647 {
648 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
649 "Configuration parameters missing.");
650 gateway->destroy(gateway);
651 return FALSE;
652 }
653
654 /**
655 * Set up configurations
656 */
657 ike_cfg = ike_cfg_create(&ike);
658
659 str = nm_setting_vpn_get_data_item(vpn, "proposal");
660 proposal = streq(str, "yes");
661 str = nm_setting_vpn_get_data_item(vpn, "ike");
662 if (proposal && str && strlen(str))
663 {
664 enumerator = enumerator_create_token(str, ";", "");
665 while (enumerator->enumerate(enumerator, &str))
666 {
667 prop = proposal_create_from_string(PROTO_IKE, str);
668 if (!prop)
669 {
670 g_set_error(err, NM_VPN_PLUGIN_ERROR,
671 NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
672 "Invalid IKE proposal.");
673 enumerator->destroy(enumerator);
674 ike_cfg->destroy(ike_cfg);
675 gateway->destroy(gateway);
676 user->destroy(user);
677 return FALSE;
678 }
679 ike_cfg->add_proposal(ike_cfg, prop);
680 }
681 enumerator->destroy(enumerator);
682 }
683 else
684 {
685 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
686 ike_cfg->add_proposal(ike_cfg, proposal_create_default_aead(PROTO_IKE));
687 }
688
689 peer_cfg = peer_cfg_create(priv->name, ike_cfg, &peer);
690 if (virtual)
691 {
692 peer_cfg->add_virtual_ip(peer_cfg, host_create_any(AF_INET));
693 peer_cfg->add_virtual_ip(peer_cfg, host_create_any(AF_INET6));
694 }
695 auth = auth_cfg_create();
696 auth->add(auth, AUTH_RULE_AUTH_CLASS, auth_class);
697 auth->add(auth, AUTH_RULE_IDENTITY, user);
698 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
699 auth = auth_cfg_create();
700 if (auth_class == AUTH_CLASS_PSK)
701 {
702 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PSK);
703 }
704 else
705 {
706 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
707 }
708 auth->add(auth, AUTH_RULE_IDENTITY, gateway);
709 auth->add(auth, AUTH_RULE_IDENTITY_LOOSE, loose_gateway_id);
710 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
711
712 child_cfg = child_cfg_create(priv->name, &child);
713 str = nm_setting_vpn_get_data_item(vpn, "esp");
714 if (proposal && str && strlen(str))
715 {
716 enumerator = enumerator_create_token(str, ";", "");
717 while (enumerator->enumerate(enumerator, &str))
718 {
719 prop = proposal_create_from_string(PROTO_ESP, str);
720 if (!prop)
721 {
722 g_set_error(err, NM_VPN_PLUGIN_ERROR,
723 NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
724 "Invalid ESP proposal.");
725 enumerator->destroy(enumerator);
726 child_cfg->destroy(child_cfg);
727 peer_cfg->destroy(peer_cfg);
728 return FALSE;
729 }
730 child_cfg->add_proposal(child_cfg, prop);
731 }
732 enumerator->destroy(enumerator);
733 }
734 else
735 {
736 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
737 child_cfg->add_proposal(child_cfg, proposal_create_default_aead(PROTO_ESP));
738 }
739 ts = traffic_selector_create_dynamic(0, 0, 65535);
740 child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
741 ts = traffic_selector_create_from_cidr("0.0.0.0/0", 0, 0, 65535);
742 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
743 ts = traffic_selector_create_from_cidr("::/0", 0, 0, 65535);
744 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
745 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
746
747 /**
748 * Prepare IKE_SA
749 */
750 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
751 peer_cfg);
752 if (!ike_sa)
753 {
754 peer_cfg->destroy(peer_cfg);
755 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
756 "IKE version not supported.");
757 return FALSE;
758 }
759 if (!ike_sa->get_peer_cfg(ike_sa))
760 {
761 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
762 }
763 peer_cfg->destroy(peer_cfg);
764
765 /**
766 * Register listener, enable initiate-failure-detection hooks
767 */
768 priv->ike_sa = ike_sa;
769 priv->listener.ike_state_change = ike_state_change;
770 priv->listener.child_state_change = child_state_change;
771 charon->bus->add_listener(charon->bus, &priv->listener);
772
773 /**
774 * Initiate
775 */
776 child_cfg->get_ref(child_cfg);
777 if (ike_sa->initiate(ike_sa, child_cfg, 0, NULL, NULL) != SUCCESS)
778 {
779 charon->bus->remove_listener(charon->bus, &priv->listener);
780 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
781
782 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
783 "Initiating failed.");
784 return FALSE;
785 }
786 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
787 return TRUE;
788 }
789
790 /**
791 * NeedSecrets called from NM via DBUS
792 */
793 static gboolean need_secrets(NMVpnServicePlugin *plugin, NMConnection *connection,
794 const char **setting_name, GError **error)
795 {
796 NMSettingVpn *settings;
797 const char *method, *path;
798
799 settings = NM_SETTING_VPN(nm_connection_get_setting(connection,
800 NM_TYPE_SETTING_VPN));
801 method = nm_setting_vpn_get_data_item(settings, "method");
802 if (method)
803 {
804 if (streq(method, "eap") || streq(method, "psk"))
805 {
806 if (nm_setting_vpn_get_secret(settings, "password"))
807 {
808 return FALSE;
809 }
810 }
811 else if (streq(method, "agent"))
812 {
813 if (nm_setting_vpn_get_secret(settings, "agent"))
814 {
815 return FALSE;
816 }
817 }
818 else if (streq(method, "key"))
819 {
820 path = nm_setting_vpn_get_data_item(settings, "userkey");
821 if (path)
822 {
823 private_key_t *key;
824
825 /* try to load/decrypt the private key */
826 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
827 KEY_ANY, BUILD_FROM_FILE, path, BUILD_END);
828 if (key)
829 {
830 key->destroy(key);
831 return FALSE;
832 }
833 else if (nm_setting_vpn_get_secret(settings, "password"))
834 {
835 return FALSE;
836 }
837 }
838 }
839 else if (streq(method, "smartcard"))
840 {
841 if (nm_setting_vpn_get_secret(settings, "password"))
842 {
843 return FALSE;
844 }
845 }
846 }
847 *setting_name = NM_SETTING_VPN_SETTING_NAME;
848 return TRUE;
849 }
850
851 /**
852 * The actual disconnection
853 */
854 static gboolean do_disconnect(gpointer plugin)
855 {
856 NMStrongswanPluginPrivate *priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
857 enumerator_t *enumerator;
858 ike_sa_t *ike_sa;
859 u_int id;
860
861 /* our ike_sa pointer might be invalid, lookup sa */
862 enumerator = charon->controller->create_ike_sa_enumerator(
863 charon->controller, TRUE);
864 while (enumerator->enumerate(enumerator, &ike_sa))
865 {
866 if (priv->ike_sa == ike_sa)
867 {
868 id = ike_sa->get_unique_id(ike_sa);
869 enumerator->destroy(enumerator);
870 charon->controller->terminate_ike(charon->controller, id, FALSE,
871 controller_cb_empty, NULL, 0);
872 return FALSE;
873 }
874 }
875 enumerator->destroy(enumerator);
876
877 g_debug("Connection not found.");
878 return FALSE;
879 }
880
881 /**
882 * Disconnect called from NM via DBUS
883 */
884 static gboolean disconnect(NMVpnServicePlugin *plugin, GError **err)
885 {
886 /* enqueue the actual disconnection, because we may be called in
887 * response to a listener_t callback and the SA enumeration would
888 * possibly deadlock. */
889 g_idle_add(do_disconnect, plugin);
890
891 return TRUE;
892 }
893
894 /**
895 * Initializer
896 */
897 static void nm_strongswan_plugin_init(NMStrongswanPlugin *plugin)
898 {
899 NMStrongswanPluginPrivate *priv;
900
901 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
902 priv->plugin = NM_VPN_SERVICE_PLUGIN(plugin);
903 memset(&priv->listener, 0, sizeof(listener_t));
904 priv->listener.child_updown = child_updown;
905 priv->listener.ike_rekey = ike_rekey;
906 priv->name = NULL;
907 }
908
909 /**
910 * Class constructor
911 */
912 static void nm_strongswan_plugin_class_init(
913 NMStrongswanPluginClass *strongswan_class)
914 {
915 NMVpnServicePluginClass *parent_class = NM_VPN_SERVICE_PLUGIN_CLASS(strongswan_class);
916
917 parent_class->connect = connect_;
918 parent_class->need_secrets = need_secrets;
919 parent_class->disconnect = disconnect;
920 }
921
922 /**
923 * Object constructor
924 */
925 NMStrongswanPlugin *nm_strongswan_plugin_new(nm_creds_t *creds,
926 nm_handler_t *handler)
927 {
928 GError *error = NULL;
929
930 NMStrongswanPlugin *plugin = (NMStrongswanPlugin *)g_initable_new (
931 NM_TYPE_STRONGSWAN_PLUGIN,
932 NULL,
933 &error,
934 NM_VPN_SERVICE_PLUGIN_DBUS_SERVICE_NAME, NM_DBUS_SERVICE_STRONGSWAN,
935 NULL);
936
937 if (plugin)
938 {
939 NMStrongswanPluginPrivate *priv;
940
941 /* the rest of the initialization happened in _init above */
942 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
943 priv->creds = creds;
944 priv->handler = handler;
945 }
946 else
947 {
948 g_warning ("Failed to initialize a plugin instance: %s", error->message);
949 g_error_free (error);
950 }
951
952 return plugin;
953 }