fcf79faa0fb6367851e28ed7a05c27b677fe3678
[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, "virtual");
451 virtual = streq(str, "yes");
452 str = nm_setting_vpn_get_data_item(vpn, "encap");
453 ike.force_encap = streq(str, "yes");
454 str = nm_setting_vpn_get_data_item(vpn, "ipcomp");
455 child.options |= streq(str, "yes") ? OPT_IPCOMP : 0;
456 str = nm_setting_vpn_get_data_item(vpn, "method");
457 if (streq(str, "psk"))
458 {
459 auth_class = AUTH_CLASS_PSK;
460 }
461 else if (streq(str, "agent"))
462 {
463 auth_class = AUTH_CLASS_PUBKEY;
464 agent = TRUE;
465 }
466 else if (streq(str, "key"))
467 {
468 auth_class = AUTH_CLASS_PUBKEY;
469 }
470 else if (streq(str, "smartcard"))
471 {
472 auth_class = AUTH_CLASS_PUBKEY;
473 smartcard = TRUE;
474 }
475
476 /**
477 * Register credentials
478 */
479 priv->creds->clear(priv->creds);
480
481 /* gateway/CA cert */
482 str = nm_setting_vpn_get_data_item(vpn, "certificate");
483 if (str)
484 {
485 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
486 BUILD_FROM_FILE, str, BUILD_END);
487 if (!cert)
488 {
489 g_set_error(err, NM_VPN_PLUGIN_ERROR,
490 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
491 "Loading gateway certificate failed.");
492 return FALSE;
493 }
494 priv->creds->add_certificate(priv->creds, cert);
495 }
496 else
497 {
498 /* no certificate defined, fall back to system-wide CA certificates */
499 priv->creds->load_ca_dir(priv->creds, lib->settings->get_str(
500 lib->settings, "charon-nm.ca_dir", NM_CA_DIR));
501 }
502
503 str = nm_setting_vpn_get_data_item(vpn, "remote-identity");
504 if (str)
505 {
506 gateway = identification_create_from_string((char*)str);
507 }
508 else if (cert)
509 {
510 x509 = (x509_t*)cert;
511 if (!(x509->get_flags(x509) & X509_CA))
512 { /* for server certificates, we use the subject as identity */
513 gateway = cert->get_subject(cert);
514 gateway = gateway->clone(gateway);
515 }
516 }
517 if (!gateway || gateway->get_type(gateway) == ID_ANY)
518 {
519 /* if the user configured a CA certificate (or an invalid identity),
520 * we use the IP/hostname of the server */
521 gateway = identification_create_from_string(ike.remote);
522 loose_gateway_id = TRUE;
523 }
524 DBG1(DBG_CFG, "using gateway identity '%Y'", gateway);
525
526 if (auth_class == AUTH_CLASS_EAP ||
527 auth_class == AUTH_CLASS_PSK)
528 {
529 /* username/password or PSK authentication ... */
530 str = nm_setting_vpn_get_data_item(vpn, "user");
531 if (str)
532 {
533 user = identification_create_from_string((char*)str);
534 str = nm_setting_vpn_get_secret(vpn, "password");
535 if (auth_class == AUTH_CLASS_PSK &&
536 strlen(str) < 20)
537 {
538 g_set_error(err, NM_VPN_PLUGIN_ERROR,
539 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
540 "pre-shared key is too short.");
541 gateway->destroy(gateway);
542 user->destroy(user);
543 return FALSE;
544 }
545 priv->creds->set_username_password(priv->creds, user, (char*)str);
546 }
547 }
548
549 if (auth_class == AUTH_CLASS_PUBKEY)
550 {
551 if (smartcard)
552 {
553 char *pin;
554
555 pin = (char*)nm_setting_vpn_get_secret(vpn, "password");
556 if (pin)
557 {
558 user = find_smartcard_key(priv, pin);
559 }
560 if (!user)
561 {
562 g_set_error(err, NM_VPN_PLUGIN_ERROR,
563 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
564 "no usable smartcard certificate found.");
565 gateway->destroy(gateway);
566 return FALSE;
567 }
568 }
569 /* ... or certificate/private key authentication */
570 else if ((str = nm_setting_vpn_get_data_item(vpn, "usercert")))
571 {
572 public_key_t *public;
573 private_key_t *private = NULL;
574
575 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
576 BUILD_FROM_FILE, str, BUILD_END);
577 if (!cert)
578 {
579 g_set_error(err, NM_VPN_PLUGIN_ERROR,
580 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
581 "Loading peer certificate failed.");
582 gateway->destroy(gateway);
583 return FALSE;
584 }
585 /* try agent */
586 str = nm_setting_vpn_get_secret(vpn, "agent");
587 if (agent && str)
588 {
589 public = cert->get_public_key(cert);
590 if (public)
591 {
592 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
593 public->get_type(public),
594 BUILD_AGENT_SOCKET, str,
595 BUILD_PUBLIC_KEY, public,
596 BUILD_END);
597 public->destroy(public);
598 }
599 if (!private)
600 {
601 g_set_error(err, NM_VPN_PLUGIN_ERROR,
602 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
603 "Connecting to SSH agent failed.");
604 }
605 }
606 /* ... or key file */
607 str = nm_setting_vpn_get_data_item(vpn, "userkey");
608 if (!agent && str)
609 {
610 char *secret;
611
612 secret = (char*)nm_setting_vpn_get_secret(vpn, "password");
613 if (secret)
614 {
615 priv->creds->set_key_password(priv->creds, secret);
616 }
617 private = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
618 KEY_ANY, BUILD_FROM_FILE, str, BUILD_END);
619 if (!private)
620 {
621 g_set_error(err, NM_VPN_PLUGIN_ERROR,
622 NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
623 "Loading private key failed.");
624 }
625 }
626 if (private)
627 {
628 user = cert->get_subject(cert);
629 user = user->clone(user);
630 priv->creds->set_cert_and_key(priv->creds, cert, private);
631 }
632 else
633 {
634 DESTROY_IF(cert);
635 gateway->destroy(gateway);
636 return FALSE;
637 }
638 }
639 }
640
641 if (!user)
642 {
643 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_BAD_ARGUMENTS,
644 "Configuration parameters missing.");
645 gateway->destroy(gateway);
646 return FALSE;
647 }
648
649 /**
650 * Set up configurations
651 */
652 ike_cfg = ike_cfg_create(&ike);
653
654 str = nm_setting_vpn_get_data_item(vpn, "proposal");
655 proposal = streq(str, "yes");
656 str = nm_setting_vpn_get_data_item(vpn, "ike");
657 if (proposal && str && strlen(str))
658 {
659 enumerator = enumerator_create_token(str, ";", "");
660 while (enumerator->enumerate(enumerator, &str))
661 {
662 prop = proposal_create_from_string(PROTO_IKE, str);
663 if (!prop)
664 {
665 g_set_error(err, NM_VPN_PLUGIN_ERROR,
666 NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
667 "Invalid IKE proposal.");
668 enumerator->destroy(enumerator);
669 ike_cfg->destroy(ike_cfg);
670 gateway->destroy(gateway);
671 user->destroy(user);
672 return FALSE;
673 }
674 ike_cfg->add_proposal(ike_cfg, prop);
675 }
676 enumerator->destroy(enumerator);
677 }
678 else
679 {
680 ike_cfg->add_proposal(ike_cfg, proposal_create_default(PROTO_IKE));
681 ike_cfg->add_proposal(ike_cfg, proposal_create_default_aead(PROTO_IKE));
682 }
683
684 peer_cfg = peer_cfg_create(priv->name, ike_cfg, &peer);
685 if (virtual)
686 {
687 peer_cfg->add_virtual_ip(peer_cfg, host_create_any(AF_INET));
688 peer_cfg->add_virtual_ip(peer_cfg, host_create_any(AF_INET6));
689 }
690 auth = auth_cfg_create();
691 auth->add(auth, AUTH_RULE_AUTH_CLASS, auth_class);
692 auth->add(auth, AUTH_RULE_IDENTITY, user);
693 peer_cfg->add_auth_cfg(peer_cfg, auth, TRUE);
694 auth = auth_cfg_create();
695 if (auth_class == AUTH_CLASS_PSK)
696 {
697 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PSK);
698 }
699 else
700 {
701 auth->add(auth, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
702 }
703 auth->add(auth, AUTH_RULE_IDENTITY, gateway);
704 auth->add(auth, AUTH_RULE_IDENTITY_LOOSE, loose_gateway_id);
705 peer_cfg->add_auth_cfg(peer_cfg, auth, FALSE);
706
707 child_cfg = child_cfg_create(priv->name, &child);
708 str = nm_setting_vpn_get_data_item(vpn, "esp");
709 if (proposal && str && strlen(str))
710 {
711 enumerator = enumerator_create_token(str, ";", "");
712 while (enumerator->enumerate(enumerator, &str))
713 {
714 prop = proposal_create_from_string(PROTO_ESP, str);
715 if (!prop)
716 {
717 g_set_error(err, NM_VPN_PLUGIN_ERROR,
718 NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
719 "Invalid ESP proposal.");
720 enumerator->destroy(enumerator);
721 child_cfg->destroy(child_cfg);
722 peer_cfg->destroy(peer_cfg);
723 return FALSE;
724 }
725 child_cfg->add_proposal(child_cfg, prop);
726 }
727 enumerator->destroy(enumerator);
728 }
729 else
730 {
731 child_cfg->add_proposal(child_cfg, proposal_create_default(PROTO_ESP));
732 child_cfg->add_proposal(child_cfg, proposal_create_default_aead(PROTO_ESP));
733 }
734 ts = traffic_selector_create_dynamic(0, 0, 65535);
735 child_cfg->add_traffic_selector(child_cfg, TRUE, ts);
736 ts = traffic_selector_create_from_cidr("0.0.0.0/0", 0, 0, 65535);
737 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
738 ts = traffic_selector_create_from_cidr("::/0", 0, 0, 65535);
739 child_cfg->add_traffic_selector(child_cfg, FALSE, ts);
740 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
741
742 /**
743 * Prepare IKE_SA
744 */
745 ike_sa = charon->ike_sa_manager->checkout_by_config(charon->ike_sa_manager,
746 peer_cfg);
747 if (!ike_sa)
748 {
749 peer_cfg->destroy(peer_cfg);
750 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
751 "IKE version not supported.");
752 return FALSE;
753 }
754 if (!ike_sa->get_peer_cfg(ike_sa))
755 {
756 ike_sa->set_peer_cfg(ike_sa, peer_cfg);
757 }
758 peer_cfg->destroy(peer_cfg);
759
760 /**
761 * Register listener, enable initiate-failure-detection hooks
762 */
763 priv->ike_sa = ike_sa;
764 priv->listener.ike_state_change = ike_state_change;
765 priv->listener.child_state_change = child_state_change;
766 charon->bus->add_listener(charon->bus, &priv->listener);
767
768 /**
769 * Initiate
770 */
771 child_cfg->get_ref(child_cfg);
772 if (ike_sa->initiate(ike_sa, child_cfg, 0, NULL, NULL) != SUCCESS)
773 {
774 charon->bus->remove_listener(charon->bus, &priv->listener);
775 charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
776
777 g_set_error(err, NM_VPN_PLUGIN_ERROR, NM_VPN_PLUGIN_ERROR_LAUNCH_FAILED,
778 "Initiating failed.");
779 return FALSE;
780 }
781 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
782 return TRUE;
783 }
784
785 /**
786 * NeedSecrets called from NM via DBUS
787 */
788 static gboolean need_secrets(NMVpnServicePlugin *plugin, NMConnection *connection,
789 const char **setting_name, GError **error)
790 {
791 NMSettingVpn *settings;
792 const char *method, *path;
793
794 settings = NM_SETTING_VPN(nm_connection_get_setting(connection,
795 NM_TYPE_SETTING_VPN));
796 method = nm_setting_vpn_get_data_item(settings, "method");
797 if (method)
798 {
799 if (streq(method, "eap") || streq(method, "psk"))
800 {
801 if (nm_setting_vpn_get_secret(settings, "password"))
802 {
803 return FALSE;
804 }
805 }
806 else if (streq(method, "agent"))
807 {
808 if (nm_setting_vpn_get_secret(settings, "agent"))
809 {
810 return FALSE;
811 }
812 }
813 else if (streq(method, "key"))
814 {
815 path = nm_setting_vpn_get_data_item(settings, "userkey");
816 if (path)
817 {
818 private_key_t *key;
819
820 /* try to load/decrypt the private key */
821 key = lib->creds->create(lib->creds, CRED_PRIVATE_KEY,
822 KEY_ANY, BUILD_FROM_FILE, path, BUILD_END);
823 if (key)
824 {
825 key->destroy(key);
826 return FALSE;
827 }
828 else if (nm_setting_vpn_get_secret(settings, "password"))
829 {
830 return FALSE;
831 }
832 }
833 }
834 else if (streq(method, "smartcard"))
835 {
836 if (nm_setting_vpn_get_secret(settings, "password"))
837 {
838 return FALSE;
839 }
840 }
841 }
842 *setting_name = NM_SETTING_VPN_SETTING_NAME;
843 return TRUE;
844 }
845
846 /**
847 * The actual disconnection
848 */
849 static gboolean do_disconnect(gpointer plugin)
850 {
851 NMStrongswanPluginPrivate *priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
852 enumerator_t *enumerator;
853 ike_sa_t *ike_sa;
854 u_int id;
855
856 /* our ike_sa pointer might be invalid, lookup sa */
857 enumerator = charon->controller->create_ike_sa_enumerator(
858 charon->controller, TRUE);
859 while (enumerator->enumerate(enumerator, &ike_sa))
860 {
861 if (priv->ike_sa == ike_sa)
862 {
863 id = ike_sa->get_unique_id(ike_sa);
864 enumerator->destroy(enumerator);
865 charon->controller->terminate_ike(charon->controller, id, FALSE,
866 controller_cb_empty, NULL, 0);
867 return FALSE;
868 }
869 }
870 enumerator->destroy(enumerator);
871
872 g_debug("Connection not found.");
873 return FALSE;
874 }
875
876 /**
877 * Disconnect called from NM via DBUS
878 */
879 static gboolean disconnect(NMVpnServicePlugin *plugin, GError **err)
880 {
881 /* enqueue the actual disconnection, because we may be called in
882 * response to a listener_t callback and the SA enumeration would
883 * possibly deadlock. */
884 g_idle_add(do_disconnect, plugin);
885
886 return TRUE;
887 }
888
889 /**
890 * Initializer
891 */
892 static void nm_strongswan_plugin_init(NMStrongswanPlugin *plugin)
893 {
894 NMStrongswanPluginPrivate *priv;
895
896 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
897 priv->plugin = NM_VPN_SERVICE_PLUGIN(plugin);
898 memset(&priv->listener, 0, sizeof(listener_t));
899 priv->listener.child_updown = child_updown;
900 priv->listener.ike_rekey = ike_rekey;
901 priv->name = NULL;
902 }
903
904 /**
905 * Class constructor
906 */
907 static void nm_strongswan_plugin_class_init(
908 NMStrongswanPluginClass *strongswan_class)
909 {
910 NMVpnServicePluginClass *parent_class = NM_VPN_SERVICE_PLUGIN_CLASS(strongswan_class);
911
912 parent_class->connect = connect_;
913 parent_class->need_secrets = need_secrets;
914 parent_class->disconnect = disconnect;
915 }
916
917 /**
918 * Object constructor
919 */
920 NMStrongswanPlugin *nm_strongswan_plugin_new(nm_creds_t *creds,
921 nm_handler_t *handler)
922 {
923 GError *error = NULL;
924
925 NMStrongswanPlugin *plugin = (NMStrongswanPlugin *)g_initable_new (
926 NM_TYPE_STRONGSWAN_PLUGIN,
927 NULL,
928 &error,
929 NM_VPN_SERVICE_PLUGIN_DBUS_SERVICE_NAME, NM_DBUS_SERVICE_STRONGSWAN,
930 NULL);
931
932 if (plugin)
933 {
934 NMStrongswanPluginPrivate *priv;
935
936 /* the rest of the initialization happened in _init above */
937 priv = NM_STRONGSWAN_PLUGIN_GET_PRIVATE(plugin);
938 priv->creds = creds;
939 priv->handler = handler;
940 }
941 else
942 {
943 g_warning ("Failed to initialize a plugin instance: %s", error->message);
944 g_error_free (error);
945 }
946
947 return plugin;
948 }