stroke: Allow specifying the ipsec.secrets location in strongswan.conf
[strongswan.git] / src / libcharon / plugins / stroke / stroke_config.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "stroke_config.h"
18
19 #include <hydra.h>
20 #include <daemon.h>
21 #include <threading/mutex.h>
22 #include <utils/lexparser.h>
23
24 #include <netdb.h>
25
26 typedef struct private_stroke_config_t private_stroke_config_t;
27
28 /**
29 * private data of stroke_config
30 */
31 struct private_stroke_config_t {
32
33 /**
34 * public functions
35 */
36 stroke_config_t public;
37
38 /**
39 * list of peer_cfg_t
40 */
41 linked_list_t *list;
42
43 /**
44 * mutex to lock config list
45 */
46 mutex_t *mutex;
47
48 /**
49 * ca sections
50 */
51 stroke_ca_t *ca;
52
53 /**
54 * credentials
55 */
56 stroke_cred_t *cred;
57
58 /**
59 * Virtual IP pool / DNS backend
60 */
61 stroke_attribute_t *attributes;
62 };
63
64 METHOD(backend_t, create_peer_cfg_enumerator, enumerator_t*,
65 private_stroke_config_t *this, identification_t *me, identification_t *other)
66 {
67 this->mutex->lock(this->mutex);
68 return enumerator_create_cleaner(this->list->create_enumerator(this->list),
69 (void*)this->mutex->unlock, this->mutex);
70 }
71
72 /**
73 * filter function for ike configs
74 */
75 static bool ike_filter(void *data, peer_cfg_t **in, ike_cfg_t **out)
76 {
77 *out = (*in)->get_ike_cfg(*in);
78 return TRUE;
79 }
80
81 METHOD(backend_t, create_ike_cfg_enumerator, enumerator_t*,
82 private_stroke_config_t *this, host_t *me, host_t *other)
83 {
84 this->mutex->lock(this->mutex);
85 return enumerator_create_filter(this->list->create_enumerator(this->list),
86 (void*)ike_filter, this->mutex,
87 (void*)this->mutex->unlock);
88 }
89
90 METHOD(backend_t, get_peer_cfg_by_name, peer_cfg_t*,
91 private_stroke_config_t *this, char *name)
92 {
93 enumerator_t *e1, *e2;
94 peer_cfg_t *current, *found = NULL;
95 child_cfg_t *child;
96
97 this->mutex->lock(this->mutex);
98 e1 = this->list->create_enumerator(this->list);
99 while (e1->enumerate(e1, &current))
100 {
101 /* compare peer_cfgs name first */
102 if (streq(current->get_name(current), name))
103 {
104 found = current;
105 found->get_ref(found);
106 break;
107 }
108 /* compare all child_cfg names otherwise */
109 e2 = current->create_child_cfg_enumerator(current);
110 while (e2->enumerate(e2, &child))
111 {
112 if (streq(child->get_name(child), name))
113 {
114 found = current;
115 found->get_ref(found);
116 break;
117 }
118 }
119 e2->destroy(e2);
120 if (found)
121 {
122 break;
123 }
124 }
125 e1->destroy(e1);
126 this->mutex->unlock(this->mutex);
127 return found;
128 }
129
130 /**
131 * parse a proposal string, either into ike_cfg or child_cfg
132 */
133 static void add_proposals(private_stroke_config_t *this, char *string,
134 ike_cfg_t *ike_cfg, child_cfg_t *child_cfg, protocol_id_t proto)
135 {
136 if (string)
137 {
138 char *single;
139 char *strict;
140 proposal_t *proposal;
141
142 strict = string + strlen(string) - 1;
143 if (*strict == '!')
144 {
145 *strict = '\0';
146 }
147 else
148 {
149 strict = NULL;
150 }
151 while ((single = strsep(&string, ",")))
152 {
153 proposal = proposal_create_from_string(proto, single);
154 if (proposal)
155 {
156 if (ike_cfg)
157 {
158 ike_cfg->add_proposal(ike_cfg, proposal);
159 }
160 else
161 {
162 child_cfg->add_proposal(child_cfg, proposal);
163 }
164 continue;
165 }
166 DBG1(DBG_CFG, "skipped invalid proposal string: %s", single);
167 }
168 if (strict)
169 {
170 return;
171 }
172 /* add default porposal to the end if not strict */
173 }
174 if (ike_cfg)
175 {
176 ike_cfg->add_proposal(ike_cfg, proposal_create_default(proto));
177 ike_cfg->add_proposal(ike_cfg, proposal_create_default_aead(proto));
178 }
179 else
180 {
181 child_cfg->add_proposal(child_cfg, proposal_create_default(proto));
182 child_cfg->add_proposal(child_cfg, proposal_create_default_aead(proto));
183 }
184 }
185
186 /**
187 * Build an IKE config from a stroke message
188 */
189 static ike_cfg_t *build_ike_cfg(private_stroke_config_t *this, stroke_msg_t *msg)
190 {
191 enumerator_t *enumerator;
192 stroke_end_t tmp_end;
193 ike_cfg_t *ike_cfg;
194 host_t *host;
195 u_int16_t ikeport;
196 char me[256], other[256], *token;
197 bool swapped = FALSE;;
198
199 enumerator = enumerator_create_token(msg->add_conn.other.address, ",", " ");
200 while (enumerator->enumerate(enumerator, &token))
201 {
202 if (!strchr(token, '/'))
203 {
204 host = host_create_from_dns(token, 0, 0);
205 if (host)
206 {
207 if (hydra->kernel_interface->get_interface(
208 hydra->kernel_interface, host, NULL))
209 {
210 DBG2(DBG_CFG, "left is other host, swapping ends");
211 tmp_end = msg->add_conn.me;
212 msg->add_conn.me = msg->add_conn.other;
213 msg->add_conn.other = tmp_end;
214 swapped = TRUE;
215 }
216 host->destroy(host);
217 }
218 }
219 }
220 enumerator->destroy(enumerator);
221
222 if (!swapped)
223 {
224 enumerator = enumerator_create_token(msg->add_conn.me.address, ",", " ");
225 while (enumerator->enumerate(enumerator, &token))
226 {
227 if (!strchr(token, '/'))
228 {
229 host = host_create_from_dns(token, 0, 0);
230 if (host)
231 {
232 if (!hydra->kernel_interface->get_interface(
233 hydra->kernel_interface, host, NULL))
234 {
235 DBG1(DBG_CFG, "left nor right host is our side, "
236 "assuming left=local");
237 }
238 host->destroy(host);
239 }
240 }
241 }
242 enumerator->destroy(enumerator);
243 }
244
245 if (msg->add_conn.me.allow_any)
246 {
247 snprintf(me, sizeof(me), "%s,0.0.0.0/0,::/0",
248 msg->add_conn.me.address);
249 }
250 if (msg->add_conn.other.allow_any)
251 {
252 snprintf(other, sizeof(other), "%s,0.0.0.0/0,::/0",
253 msg->add_conn.other.address);
254 }
255 ikeport = msg->add_conn.me.ikeport;
256 ikeport = (ikeport == IKEV2_UDP_PORT) ?
257 charon->socket->get_port(charon->socket, FALSE) : ikeport;
258 ike_cfg = ike_cfg_create(msg->add_conn.version,
259 msg->add_conn.other.sendcert != CERT_NEVER_SEND,
260 msg->add_conn.force_encap,
261 msg->add_conn.me.allow_any ?
262 me : msg->add_conn.me.address,
263 ikeport,
264 msg->add_conn.other.allow_any ?
265 other : msg->add_conn.other.address,
266 msg->add_conn.other.ikeport,
267 msg->add_conn.fragmentation,
268 msg->add_conn.ikedscp);
269
270 add_proposals(this, msg->add_conn.algorithms.ike, ike_cfg, NULL, PROTO_IKE);
271 return ike_cfg;
272 }
273
274 /**
275 * Add CRL constraint to config
276 */
277 static void build_crl_policy(auth_cfg_t *cfg, bool local, int policy)
278 {
279 /* CRL/OCSP policy, for remote config only */
280 if (!local)
281 {
282 switch (policy)
283 {
284 case CRL_STRICT_YES:
285 /* if yes, we require a GOOD validation */
286 cfg->add(cfg, AUTH_RULE_CRL_VALIDATION, VALIDATION_GOOD);
287 break;
288 case CRL_STRICT_IFURI:
289 /* for ifuri, a SKIPPED validation is sufficient */
290 cfg->add(cfg, AUTH_RULE_CRL_VALIDATION, VALIDATION_SKIPPED);
291 break;
292 default:
293 break;
294 }
295 }
296 }
297
298 /**
299 * Parse public key / signature strength constraints
300 */
301 static void parse_pubkey_constraints(char *auth, auth_cfg_t *cfg)
302 {
303 enumerator_t *enumerator;
304 bool rsa = FALSE, ecdsa = FALSE, rsa_len = FALSE, ecdsa_len = FALSE;
305 int strength;
306 char *token;
307
308 enumerator = enumerator_create_token(auth, "-", "");
309 while (enumerator->enumerate(enumerator, &token))
310 {
311 bool found = FALSE;
312 int i;
313 struct {
314 char *name;
315 signature_scheme_t scheme;
316 key_type_t key;
317 } schemes[] = {
318 { "md5", SIGN_RSA_EMSA_PKCS1_MD5, KEY_RSA, },
319 { "sha1", SIGN_RSA_EMSA_PKCS1_SHA1, KEY_RSA, },
320 { "sha224", SIGN_RSA_EMSA_PKCS1_SHA224, KEY_RSA, },
321 { "sha256", SIGN_RSA_EMSA_PKCS1_SHA256, KEY_RSA, },
322 { "sha384", SIGN_RSA_EMSA_PKCS1_SHA384, KEY_RSA, },
323 { "sha512", SIGN_RSA_EMSA_PKCS1_SHA512, KEY_RSA, },
324 { "sha1", SIGN_ECDSA_WITH_SHA1_DER, KEY_ECDSA, },
325 { "sha256", SIGN_ECDSA_WITH_SHA256_DER, KEY_ECDSA, },
326 { "sha384", SIGN_ECDSA_WITH_SHA384_DER, KEY_ECDSA, },
327 { "sha512", SIGN_ECDSA_WITH_SHA512_DER, KEY_ECDSA, },
328 { "sha256", SIGN_ECDSA_256, KEY_ECDSA, },
329 { "sha384", SIGN_ECDSA_384, KEY_ECDSA, },
330 { "sha512", SIGN_ECDSA_521, KEY_ECDSA, },
331 };
332
333 if (rsa_len || ecdsa_len)
334 { /* expecting a key strength token */
335 strength = atoi(token);
336 if (strength)
337 {
338 if (rsa_len)
339 {
340 cfg->add(cfg, AUTH_RULE_RSA_STRENGTH, (uintptr_t)strength);
341 }
342 else if (ecdsa_len)
343 {
344 cfg->add(cfg, AUTH_RULE_ECDSA_STRENGTH, (uintptr_t)strength);
345 }
346 }
347 rsa_len = ecdsa_len = FALSE;
348 if (strength)
349 {
350 continue;
351 }
352 }
353 if (streq(token, "rsa"))
354 {
355 rsa = rsa_len = TRUE;
356 continue;
357 }
358 if (streq(token, "ecdsa"))
359 {
360 ecdsa = ecdsa_len = TRUE;
361 continue;
362 }
363 if (streq(token, "pubkey"))
364 {
365 continue;
366 }
367
368 for (i = 0; i < countof(schemes); i++)
369 {
370 if (streq(schemes[i].name, token))
371 {
372 /* for each matching string, allow the scheme, if:
373 * - it is an RSA scheme, and we enforced RSA
374 * - it is an ECDSA scheme, and we enforced ECDSA
375 * - it is not a key type specific scheme
376 */
377 if ((rsa && schemes[i].key == KEY_RSA) ||
378 (ecdsa && schemes[i].key == KEY_ECDSA) ||
379 (!rsa && !ecdsa))
380 {
381 cfg->add(cfg, AUTH_RULE_SIGNATURE_SCHEME,
382 (uintptr_t)schemes[i].scheme);
383 }
384 found = TRUE;
385 }
386 }
387 if (!found)
388 {
389 DBG1(DBG_CFG, "ignoring invalid auth token: '%s'", token);
390 }
391 }
392 enumerator->destroy(enumerator);
393 }
394
395 /**
396 * build authentication config
397 */
398 static auth_cfg_t *build_auth_cfg(private_stroke_config_t *this,
399 stroke_msg_t *msg, bool local, bool primary)
400 {
401 identification_t *identity;
402 certificate_t *certificate;
403 char *auth, *id, *pubkey, *cert, *ca, *groups;
404 stroke_end_t *end, *other_end;
405 auth_cfg_t *cfg;
406 bool loose = FALSE;
407
408 /* select strings */
409 if (local)
410 {
411 end = &msg->add_conn.me;
412 other_end = &msg->add_conn.other;
413 }
414 else
415 {
416 end = &msg->add_conn.other;
417 other_end = &msg->add_conn.me;
418 }
419 if (primary)
420 {
421 auth = end->auth;
422 id = end->id;
423 if (!id)
424 { /* leftid/rightid fallback to address */
425 id = end->address;
426 }
427 cert = end->cert;
428 ca = end->ca;
429 if (ca && streq(ca, "%same"))
430 {
431 ca = other_end->ca;
432 }
433 }
434 else
435 {
436 auth = end->auth2;
437 id = end->id2;
438 if (local && !id)
439 { /* leftid2 falls back to leftid */
440 id = end->id;
441 }
442 cert = end->cert2;
443 ca = end->ca2;
444 if (ca && streq(ca, "%same"))
445 {
446 ca = other_end->ca2;
447 }
448 }
449 if (id && *id == '%' && !streq(id, "%any") && !streq(id, "%any6"))
450 { /* has only an effect on rightid/2 */
451 loose = !local;
452 id++;
453 }
454
455 if (!auth)
456 {
457 if (primary)
458 {
459 auth = "pubkey";
460 }
461 else
462 { /* no second authentication round, fine. But load certificates
463 * for other purposes (EAP-TLS) */
464 if (cert)
465 {
466 certificate = this->cred->load_peer(this->cred, cert);
467 if (certificate)
468 {
469 certificate->destroy(certificate);
470 }
471 }
472 return NULL;
473 }
474 }
475
476 cfg = auth_cfg_create();
477
478 /* add identity and peer certificate */
479 identity = identification_create_from_string(id);
480 if (cert)
481 {
482 enumerator_t *enumerator;
483 bool has_subject = FALSE;
484 certificate_t *first = NULL;
485
486 enumerator = enumerator_create_token(cert, ",", " ");
487 while (enumerator->enumerate(enumerator, &cert))
488 {
489 certificate = this->cred->load_peer(this->cred, cert);
490 if (certificate)
491 {
492 if (local)
493 {
494 this->ca->check_for_hash_and_url(this->ca, certificate);
495 }
496 cfg->add(cfg, AUTH_RULE_SUBJECT_CERT, certificate);
497 if (!first)
498 {
499 first = certificate;
500 }
501 if (identity->get_type(identity) != ID_ANY &&
502 certificate->has_subject(certificate, identity))
503 {
504 has_subject = TRUE;
505 }
506 }
507 }
508 enumerator->destroy(enumerator);
509
510 if (first && !has_subject)
511 {
512 DBG1(DBG_CFG, " id '%Y' not confirmed by certificate, "
513 "defaulting to '%Y'", identity, first->get_subject(first));
514 identity->destroy(identity);
515 identity = first->get_subject(first);
516 identity = identity->clone(identity);
517 }
518 }
519 /* add raw RSA public key */
520 pubkey = end->rsakey;
521 if (pubkey && !streq(pubkey, "") && !streq(pubkey, "%cert"))
522 {
523 certificate = this->cred->load_pubkey(this->cred, pubkey, identity);
524 if (certificate)
525 {
526 cfg->add(cfg, AUTH_RULE_SUBJECT_CERT, certificate);
527 }
528 }
529 if (identity->get_type(identity) != ID_ANY)
530 {
531 cfg->add(cfg, AUTH_RULE_IDENTITY, identity);
532 if (loose)
533 {
534 cfg->add(cfg, AUTH_RULE_IDENTITY_LOOSE, TRUE);
535 }
536 }
537 else
538 {
539 identity->destroy(identity);
540 }
541
542 /* CA constraint */
543 if (ca)
544 {
545 identity = identification_create_from_string(ca);
546 certificate = lib->credmgr->get_cert(lib->credmgr, CERT_X509,
547 KEY_ANY, identity, TRUE);
548 identity->destroy(identity);
549 if (certificate)
550 {
551 cfg->add(cfg, AUTH_RULE_CA_CERT, certificate);
552 }
553 else
554 {
555 DBG1(DBG_CFG, "CA certificate \"%s\" not found, discarding CA "
556 "constraint", ca);
557 }
558 }
559
560 /* groups */
561 groups = primary ? end->groups : end->groups2;
562 if (groups)
563 {
564 enumerator_t *enumerator;
565 char *group;
566
567 enumerator = enumerator_create_token(groups, ",", " ");
568 while (enumerator->enumerate(enumerator, &group))
569 {
570 cfg->add(cfg, AUTH_RULE_GROUP,
571 identification_create_from_string(group));
572 }
573 enumerator->destroy(enumerator);
574 }
575
576 /* certificatePolicies */
577 if (end->cert_policy)
578 {
579 enumerator_t *enumerator;
580 char *policy;
581
582 enumerator = enumerator_create_token(end->cert_policy, ",", " ");
583 while (enumerator->enumerate(enumerator, &policy))
584 {
585 cfg->add(cfg, AUTH_RULE_CERT_POLICY, strdup(policy));
586 }
587 enumerator->destroy(enumerator);
588 }
589
590 /* authentication metod (class, actually) */
591 if (strpfx(auth, "pubkey") ||
592 strpfx(auth, "rsa") ||
593 strpfx(auth, "ecdsa"))
594 {
595 cfg->add(cfg, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
596 build_crl_policy(cfg, local, msg->add_conn.crl_policy);
597
598 parse_pubkey_constraints(auth, cfg);
599 }
600 else if (streq(auth, "psk") || streq(auth, "secret"))
601 {
602 cfg->add(cfg, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PSK);
603 }
604 else if (strpfx(auth, "xauth"))
605 {
606 char *pos;
607
608 pos = strchr(auth, '-');
609 if (pos)
610 {
611 cfg->add(cfg, AUTH_RULE_XAUTH_BACKEND, strdup(++pos));
612 }
613 cfg->add(cfg, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_XAUTH);
614 if (msg->add_conn.xauth_identity)
615 {
616 cfg->add(cfg, AUTH_RULE_XAUTH_IDENTITY,
617 identification_create_from_string(msg->add_conn.xauth_identity));
618 }
619 }
620 else if (strpfx(auth, "eap"))
621 {
622 eap_vendor_type_t *type;
623
624 cfg->add(cfg, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_EAP);
625
626 type = eap_vendor_type_from_string(auth);
627 if (type)
628 {
629 cfg->add(cfg, AUTH_RULE_EAP_TYPE, type->type);
630 if (type->vendor)
631 {
632 cfg->add(cfg, AUTH_RULE_EAP_VENDOR, type->vendor);
633 }
634 free(type);
635 }
636
637 if (msg->add_conn.eap_identity)
638 {
639 if (streq(msg->add_conn.eap_identity, "%identity"))
640 {
641 identity = identification_create_from_encoding(ID_ANY,
642 chunk_empty);
643 }
644 else
645 {
646 identity = identification_create_from_string(
647 msg->add_conn.eap_identity);
648 }
649 cfg->add(cfg, AUTH_RULE_EAP_IDENTITY, identity);
650 }
651 if (msg->add_conn.aaa_identity)
652 {
653 cfg->add(cfg, AUTH_RULE_AAA_IDENTITY,
654 identification_create_from_string(msg->add_conn.aaa_identity));
655 }
656 }
657 else
658 {
659 if (!streq(auth, "any"))
660 {
661 DBG1(DBG_CFG, "authentication method %s unknown, fallback to any",
662 auth);
663 }
664 build_crl_policy(cfg, local, msg->add_conn.crl_policy);
665 }
666 return cfg;
667 }
668
669 /**
670 * build a peer_cfg from a stroke msg
671 */
672 static peer_cfg_t *build_peer_cfg(private_stroke_config_t *this,
673 stroke_msg_t *msg, ike_cfg_t *ike_cfg)
674 {
675 identification_t *peer_id = NULL;
676 peer_cfg_t *mediated_by = NULL;
677 unique_policy_t unique;
678 u_int32_t rekey = 0, reauth = 0, over, jitter;
679 peer_cfg_t *peer_cfg;
680 auth_cfg_t *auth_cfg;
681
682 #ifdef ME
683 if (msg->add_conn.ikeme.mediation && msg->add_conn.ikeme.mediated_by)
684 {
685 DBG1(DBG_CFG, "a mediation connection cannot be a mediated connection "
686 "at the same time, aborting");
687 return NULL;
688 }
689
690 if (msg->add_conn.ikeme.mediation)
691 {
692 /* force unique connections for mediation connections */
693 msg->add_conn.unique = 1;
694 }
695
696 if (msg->add_conn.ikeme.mediated_by)
697 {
698 mediated_by = charon->backends->get_peer_cfg_by_name(charon->backends,
699 msg->add_conn.ikeme.mediated_by);
700 if (!mediated_by)
701 {
702 DBG1(DBG_CFG, "mediation connection '%s' not found, aborting",
703 msg->add_conn.ikeme.mediated_by);
704 return NULL;
705 }
706 if (!mediated_by->is_mediation(mediated_by))
707 {
708 DBG1(DBG_CFG, "connection '%s' as referred to by '%s' is "
709 "no mediation connection, aborting",
710 msg->add_conn.ikeme.mediated_by, msg->add_conn.name);
711 mediated_by->destroy(mediated_by);
712 return NULL;
713 }
714 if (msg->add_conn.ikeme.peerid)
715 {
716 peer_id = identification_create_from_string(msg->add_conn.ikeme.peerid);
717 }
718 else if (msg->add_conn.other.id)
719 {
720 peer_id = identification_create_from_string(msg->add_conn.other.id);
721 }
722 }
723 #endif /* ME */
724
725 jitter = msg->add_conn.rekey.margin * msg->add_conn.rekey.fuzz / 100;
726 over = msg->add_conn.rekey.margin;
727 if (msg->add_conn.rekey.reauth)
728 {
729 reauth = msg->add_conn.rekey.ike_lifetime - over;
730 }
731 else
732 {
733 rekey = msg->add_conn.rekey.ike_lifetime - over;
734 }
735 switch (msg->add_conn.unique)
736 {
737 case 1: /* yes */
738 case 2: /* replace */
739 unique = UNIQUE_REPLACE;
740 break;
741 case 3: /* keep */
742 unique = UNIQUE_KEEP;
743 break;
744 case 4: /* never */
745 unique = UNIQUE_NEVER;
746 break;
747 default: /* no */
748 unique = UNIQUE_NO;
749 break;
750 }
751 if (msg->add_conn.dpd.action == 0)
752 { /* dpdaction=none disables DPD */
753 msg->add_conn.dpd.delay = 0;
754 }
755
756 /* other.sourceip is managed in stroke_attributes. If it is set, we define
757 * the pool name as the connection name, which the attribute provider
758 * uses to serve pool addresses. */
759 peer_cfg = peer_cfg_create(msg->add_conn.name, ike_cfg,
760 msg->add_conn.me.sendcert, unique,
761 msg->add_conn.rekey.tries, rekey, reauth, jitter, over,
762 msg->add_conn.mobike, msg->add_conn.aggressive,
763 msg->add_conn.pushmode == 0,
764 msg->add_conn.dpd.delay, msg->add_conn.dpd.timeout,
765 msg->add_conn.ikeme.mediation, mediated_by, peer_id);
766
767 if (msg->add_conn.other.sourceip)
768 {
769 enumerator_t *enumerator;
770 char *token;
771
772 enumerator = enumerator_create_token(msg->add_conn.other.sourceip,
773 ",", " ");
774 while (enumerator->enumerate(enumerator, &token))
775 {
776 if (streq(token, "%modeconfig") || streq(token, "%modecfg") ||
777 streq(token, "%config") || streq(token, "%cfg") ||
778 streq(token, "%config4") || streq(token, "%config6"))
779 {
780 /* empty pool, uses connection name */
781 this->attributes->add_pool(this->attributes,
782 mem_pool_create(msg->add_conn.name, NULL, 0));
783 peer_cfg->add_pool(peer_cfg, msg->add_conn.name);
784 }
785 else if (*token == '%')
786 {
787 /* external named pool */
788 peer_cfg->add_pool(peer_cfg, token + 1);
789 }
790 else
791 {
792 /* in-memory pool, named using CIDR notation */
793 host_t *base;
794 int bits;
795
796 base = host_create_from_subnet(token, &bits);
797 if (base)
798 {
799 this->attributes->add_pool(this->attributes,
800 mem_pool_create(token, base, bits));
801 peer_cfg->add_pool(peer_cfg, token);
802 base->destroy(base);
803 }
804 else
805 {
806 DBG1(DBG_CFG, "IP pool %s invalid, ignored", token);
807 }
808 }
809 }
810 enumerator->destroy(enumerator);
811 }
812
813 if (msg->add_conn.me.sourceip && msg->add_conn.other.sourceip)
814 {
815 DBG1(DBG_CFG, "'%s' has both left- and rightsourceip, but IKE can "
816 "negotiate one virtual IP only, ignoring local virtual IP",
817 msg->add_conn.name);
818 }
819 else if (msg->add_conn.me.sourceip)
820 {
821 enumerator_t *enumerator;
822 char *token;
823
824 enumerator = enumerator_create_token(msg->add_conn.me.sourceip, ",", " ");
825 while (enumerator->enumerate(enumerator, &token))
826 {
827 host_t *vip = NULL;
828
829 if (streq(token, "%modeconfig") || streq(token, "%modecfg") ||
830 streq(token, "%config") || streq(token, "%cfg"))
831 { /* try to deduce an address family */
832 if (msg->add_conn.me.subnets)
833 { /* use the same family as in local subnet, if any */
834 if (strchr(msg->add_conn.me.subnets, '.'))
835 {
836 vip = host_create_any(AF_INET);
837 }
838 else
839 {
840 vip = host_create_any(AF_INET6);
841 }
842 }
843 else if (msg->add_conn.other.subnets)
844 { /* use the same family as in remote subnet, if any */
845 if (strchr(msg->add_conn.other.subnets, '.'))
846 {
847 vip = host_create_any(AF_INET);
848 }
849 else
850 {
851 vip = host_create_any(AF_INET6);
852 }
853 }
854 else
855 {
856 char *addr, *next, *hit;
857
858 /* guess virtual IP family based on local address. If
859 * multiple addresses are specified, we look at the first
860 * only, as with leftallowany a ::/0 is always appended. */
861 addr = ike_cfg->get_my_addr(ike_cfg);
862 next = strchr(addr, ',');
863 hit = strchr(addr, ':');
864 if (hit && (!next || hit < next))
865 {
866 vip = host_create_any(AF_INET6);
867 }
868 else
869 {
870 vip = host_create_any(AF_INET);
871 }
872 }
873 }
874 else if (streq(token, "%config4"))
875 {
876 vip = host_create_any(AF_INET);
877 }
878 else if (streq(token, "%config6"))
879 {
880 vip = host_create_any(AF_INET6);
881 }
882 else
883 {
884 vip = host_create_from_string(token, 0);
885 if (!vip)
886 {
887 DBG1(DBG_CFG, "ignored invalid subnet token: %s", token);
888 }
889 }
890
891 if (vip)
892 {
893 peer_cfg->add_virtual_ip(peer_cfg, vip);
894 }
895 }
896 enumerator->destroy(enumerator);
897 }
898
899 /* build leftauth= */
900 auth_cfg = build_auth_cfg(this, msg, TRUE, TRUE);
901 if (auth_cfg)
902 {
903 peer_cfg->add_auth_cfg(peer_cfg, auth_cfg, TRUE);
904 }
905 else
906 { /* we require at least one config on our side */
907 peer_cfg->destroy(peer_cfg);
908 return NULL;
909 }
910 /* build leftauth2= */
911 auth_cfg = build_auth_cfg(this, msg, TRUE, FALSE);
912 if (auth_cfg)
913 {
914 peer_cfg->add_auth_cfg(peer_cfg, auth_cfg, TRUE);
915 }
916 /* build rightauth= */
917 auth_cfg = build_auth_cfg(this, msg, FALSE, TRUE);
918 if (auth_cfg)
919 {
920 peer_cfg->add_auth_cfg(peer_cfg, auth_cfg, FALSE);
921 }
922 /* build rightauth2= */
923 auth_cfg = build_auth_cfg(this, msg, FALSE, FALSE);
924 if (auth_cfg)
925 {
926 peer_cfg->add_auth_cfg(peer_cfg, auth_cfg, FALSE);
927 }
928 return peer_cfg;
929 }
930
931 /**
932 * Parse a protoport specifier
933 */
934 static bool parse_protoport(char *token, u_int16_t *from_port,
935 u_int16_t *to_port, u_int8_t *protocol)
936 {
937 char *sep, *port = "", *endptr;
938 struct protoent *proto;
939 struct servent *svc;
940 long int p;
941
942 sep = strrchr(token, ']');
943 if (!sep)
944 {
945 return FALSE;
946 }
947 *sep = '\0';
948
949 sep = strchr(token, '/');
950 if (sep)
951 { /* protocol/port */
952 *sep = '\0';
953 port = sep + 1;
954 }
955
956 if (streq(token, "%any"))
957 {
958 *protocol = 0;
959 }
960 else
961 {
962 proto = getprotobyname(token);
963 if (proto)
964 {
965 *protocol = proto->p_proto;
966 }
967 else
968 {
969 p = strtol(token, &endptr, 0);
970 if ((*token && *endptr) || p < 0 || p > 0xff)
971 {
972 return FALSE;
973 }
974 *protocol = (u_int8_t)p;
975 }
976 }
977 if (streq(port, "%any"))
978 {
979 *from_port = 0;
980 *to_port = 0xffff;
981 }
982 else if (streq(port, "%opaque"))
983 {
984 *from_port = 0xffff;
985 *to_port = 0;
986 }
987 else if (*port)
988 {
989 svc = getservbyname(port, NULL);
990 if (svc)
991 {
992 *from_port = *to_port = ntohs(svc->s_port);
993 }
994 else
995 {
996 p = strtol(port, &endptr, 0);
997 if (p < 0 || p > 0xffff)
998 {
999 return FALSE;
1000 }
1001 *from_port = p;
1002 if (*endptr == '-')
1003 {
1004 port = endptr + 1;
1005 p = strtol(port, &endptr, 0);
1006 if (p < 0 || p > 0xffff)
1007 {
1008 return FALSE;
1009 }
1010 }
1011 *to_port = p;
1012 if (*endptr)
1013 {
1014 return FALSE;
1015 }
1016 }
1017 }
1018 return TRUE;
1019 }
1020
1021 /**
1022 * build a traffic selector from a stroke_end
1023 */
1024 static void add_ts(private_stroke_config_t *this,
1025 stroke_end_t *end, child_cfg_t *child_cfg, bool local)
1026 {
1027 traffic_selector_t *ts;
1028
1029 if (end->tohost)
1030 {
1031 ts = traffic_selector_create_dynamic(end->protocol,
1032 end->from_port, end->to_port);
1033 child_cfg->add_traffic_selector(child_cfg, local, ts);
1034 }
1035 else
1036 {
1037 if (!end->subnets)
1038 {
1039 host_t *net;
1040
1041 net = host_create_from_string(end->address, 0);
1042 if (net)
1043 {
1044 ts = traffic_selector_create_from_subnet(net, 0, end->protocol,
1045 end->from_port, end->to_port);
1046 child_cfg->add_traffic_selector(child_cfg, local, ts);
1047 }
1048 }
1049 else
1050 {
1051 enumerator_t *enumerator;
1052 char *subnet, *pos;
1053 u_int16_t from_port, to_port;
1054 u_int8_t proto;
1055
1056 enumerator = enumerator_create_token(end->subnets, ",", " ");
1057 while (enumerator->enumerate(enumerator, &subnet))
1058 {
1059 from_port = end->from_port;
1060 to_port = end->to_port;
1061 proto = end->protocol;
1062
1063 pos = strchr(subnet, '[');
1064 if (pos)
1065 {
1066 *(pos++) = '\0';
1067 if (!parse_protoport(pos, &from_port, &to_port, &proto))
1068 {
1069 DBG1(DBG_CFG, "invalid proto/port: %s, skipped subnet",
1070 pos);
1071 continue;
1072 }
1073 }
1074 if (streq(subnet, "%dynamic"))
1075 {
1076 ts = traffic_selector_create_dynamic(proto,
1077 from_port, to_port);
1078 }
1079 else
1080 {
1081 ts = traffic_selector_create_from_cidr(subnet, proto,
1082 from_port, to_port);
1083 }
1084 if (ts)
1085 {
1086 child_cfg->add_traffic_selector(child_cfg, local, ts);
1087 }
1088 else
1089 {
1090 DBG1(DBG_CFG, "invalid subnet: %s, skipped", subnet);
1091 }
1092 }
1093 enumerator->destroy(enumerator);
1094 }
1095 }
1096 }
1097
1098 /**
1099 * map starter magic values to our action type
1100 */
1101 static action_t map_action(int starter_action)
1102 {
1103 switch (starter_action)
1104 {
1105 case 2: /* =hold */
1106 return ACTION_ROUTE;
1107 case 3: /* =restart */
1108 return ACTION_RESTART;
1109 default:
1110 return ACTION_NONE;
1111 }
1112 }
1113
1114 /**
1115 * build a child config from the stroke message
1116 */
1117 static child_cfg_t *build_child_cfg(private_stroke_config_t *this,
1118 stroke_msg_t *msg)
1119 {
1120 child_cfg_t *child_cfg;
1121 lifetime_cfg_t lifetime = {
1122 .time = {
1123 .life = msg->add_conn.rekey.ipsec_lifetime,
1124 .rekey = msg->add_conn.rekey.ipsec_lifetime - msg->add_conn.rekey.margin,
1125 .jitter = msg->add_conn.rekey.margin * msg->add_conn.rekey.fuzz / 100
1126 },
1127 .bytes = {
1128 .life = msg->add_conn.rekey.life_bytes,
1129 .rekey = msg->add_conn.rekey.life_bytes - msg->add_conn.rekey.margin_bytes,
1130 .jitter = msg->add_conn.rekey.margin_bytes * msg->add_conn.rekey.fuzz / 100
1131 },
1132 .packets = {
1133 .life = msg->add_conn.rekey.life_packets,
1134 .rekey = msg->add_conn.rekey.life_packets - msg->add_conn.rekey.margin_packets,
1135 .jitter = msg->add_conn.rekey.margin_packets * msg->add_conn.rekey.fuzz / 100
1136 }
1137 };
1138 mark_t mark_in = {
1139 .value = msg->add_conn.mark_in.value,
1140 .mask = msg->add_conn.mark_in.mask
1141 };
1142 mark_t mark_out = {
1143 .value = msg->add_conn.mark_out.value,
1144 .mask = msg->add_conn.mark_out.mask
1145 };
1146
1147 child_cfg = child_cfg_create(
1148 msg->add_conn.name, &lifetime, msg->add_conn.me.updown,
1149 msg->add_conn.me.hostaccess, msg->add_conn.mode, ACTION_NONE,
1150 map_action(msg->add_conn.dpd.action),
1151 map_action(msg->add_conn.close_action), msg->add_conn.ipcomp,
1152 msg->add_conn.inactivity, msg->add_conn.reqid,
1153 &mark_in, &mark_out, msg->add_conn.tfc);
1154 if (msg->add_conn.replay_window != -1)
1155 {
1156 child_cfg->set_replay_window(child_cfg, msg->add_conn.replay_window);
1157 }
1158 child_cfg->set_mipv6_options(child_cfg, msg->add_conn.proxy_mode,
1159 msg->add_conn.install_policy);
1160 add_ts(this, &msg->add_conn.me, child_cfg, TRUE);
1161 add_ts(this, &msg->add_conn.other, child_cfg, FALSE);
1162
1163 if (msg->add_conn.algorithms.ah)
1164 {
1165 add_proposals(this, msg->add_conn.algorithms.ah,
1166 NULL, child_cfg, PROTO_AH);
1167 }
1168 else
1169 {
1170 add_proposals(this, msg->add_conn.algorithms.esp,
1171 NULL, child_cfg, PROTO_ESP);
1172 }
1173 return child_cfg;
1174 }
1175
1176 METHOD(stroke_config_t, add, void,
1177 private_stroke_config_t *this, stroke_msg_t *msg)
1178 {
1179 ike_cfg_t *ike_cfg, *existing_ike;
1180 peer_cfg_t *peer_cfg, *existing;
1181 child_cfg_t *child_cfg;
1182 enumerator_t *enumerator;
1183 bool use_existing = FALSE;
1184
1185 ike_cfg = build_ike_cfg(this, msg);
1186 if (!ike_cfg)
1187 {
1188 return;
1189 }
1190 peer_cfg = build_peer_cfg(this, msg, ike_cfg);
1191 if (!peer_cfg)
1192 {
1193 ike_cfg->destroy(ike_cfg);
1194 return;
1195 }
1196
1197 enumerator = create_peer_cfg_enumerator(this, NULL, NULL);
1198 while (enumerator->enumerate(enumerator, &existing))
1199 {
1200 existing_ike = existing->get_ike_cfg(existing);
1201 if (existing->equals(existing, peer_cfg) &&
1202 existing_ike->equals(existing_ike, peer_cfg->get_ike_cfg(peer_cfg)))
1203 {
1204 use_existing = TRUE;
1205 peer_cfg->destroy(peer_cfg);
1206 peer_cfg = existing;
1207 peer_cfg->get_ref(peer_cfg);
1208 DBG1(DBG_CFG, "added child to existing configuration '%s'",
1209 peer_cfg->get_name(peer_cfg));
1210 break;
1211 }
1212 }
1213 enumerator->destroy(enumerator);
1214
1215 child_cfg = build_child_cfg(this, msg);
1216 if (!child_cfg)
1217 {
1218 peer_cfg->destroy(peer_cfg);
1219 return;
1220 }
1221 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
1222
1223 if (use_existing)
1224 {
1225 peer_cfg->destroy(peer_cfg);
1226 }
1227 else
1228 {
1229 /* add config to backend */
1230 DBG1(DBG_CFG, "added configuration '%s'", msg->add_conn.name);
1231 this->mutex->lock(this->mutex);
1232 this->list->insert_last(this->list, peer_cfg);
1233 this->mutex->unlock(this->mutex);
1234 }
1235 }
1236
1237 METHOD(stroke_config_t, del, void,
1238 private_stroke_config_t *this, stroke_msg_t *msg)
1239 {
1240 enumerator_t *enumerator, *children;
1241 peer_cfg_t *peer;
1242 child_cfg_t *child;
1243 bool deleted = FALSE;
1244
1245 this->mutex->lock(this->mutex);
1246 enumerator = this->list->create_enumerator(this->list);
1247 while (enumerator->enumerate(enumerator, &peer))
1248 {
1249 bool keep = FALSE;
1250
1251 /* remove any child with such a name */
1252 children = peer->create_child_cfg_enumerator(peer);
1253 while (children->enumerate(children, &child))
1254 {
1255 if (streq(child->get_name(child), msg->del_conn.name))
1256 {
1257 peer->remove_child_cfg(peer, children);
1258 child->destroy(child);
1259 deleted = TRUE;
1260 }
1261 else
1262 {
1263 keep = TRUE;
1264 }
1265 }
1266 children->destroy(children);
1267
1268 /* if peer config has no children anymore, remove it */
1269 if (!keep)
1270 {
1271 this->list->remove_at(this->list, enumerator);
1272 peer->destroy(peer);
1273 }
1274 }
1275 enumerator->destroy(enumerator);
1276 this->mutex->unlock(this->mutex);
1277
1278 if (deleted)
1279 {
1280 DBG1(DBG_CFG, "deleted connection '%s'", msg->del_conn.name);
1281 }
1282 else
1283 {
1284 DBG1(DBG_CFG, "connection '%s' not found", msg->del_conn.name);
1285 }
1286 }
1287
1288 METHOD(stroke_config_t, set_user_credentials, void,
1289 private_stroke_config_t *this, stroke_msg_t *msg, FILE *prompt)
1290 {
1291 enumerator_t *enumerator, *children, *remote_auth;
1292 peer_cfg_t *peer, *found = NULL;
1293 auth_cfg_t *auth_cfg, *remote_cfg;
1294 auth_class_t auth_class;
1295 child_cfg_t *child;
1296 identification_t *id, *identity, *gw = NULL;
1297 shared_key_type_t type = SHARED_ANY;
1298 chunk_t password = chunk_empty;
1299
1300 this->mutex->lock(this->mutex);
1301 enumerator = this->list->create_enumerator(this->list);
1302 while (enumerator->enumerate(enumerator, (void**)&peer))
1303 { /* find the peer (or child) config with the given name */
1304 if (streq(peer->get_name(peer), msg->user_creds.name))
1305 {
1306 found = peer;
1307 }
1308 else
1309 {
1310 children = peer->create_child_cfg_enumerator(peer);
1311 while (children->enumerate(children, &child))
1312 {
1313 if (streq(child->get_name(child), msg->user_creds.name))
1314 {
1315 found = peer;
1316 break;
1317 }
1318 }
1319 children->destroy(children);
1320 }
1321
1322 if (found)
1323 {
1324 break;
1325 }
1326 }
1327 enumerator->destroy(enumerator);
1328
1329 if (!found)
1330 {
1331 DBG1(DBG_CFG, " no config named '%s'", msg->user_creds.name);
1332 fprintf(prompt, "no config named '%s'\n", msg->user_creds.name);
1333 this->mutex->unlock(this->mutex);
1334 return;
1335 }
1336
1337 id = identification_create_from_string(msg->user_creds.username);
1338 if (strlen(msg->user_creds.username) == 0 ||
1339 !id || id->get_type(id) == ID_ANY)
1340 {
1341 DBG1(DBG_CFG, " invalid username '%s'", msg->user_creds.username);
1342 fprintf(prompt, "invalid username '%s'\n", msg->user_creds.username);
1343 this->mutex->unlock(this->mutex);
1344 DESTROY_IF(id);
1345 return;
1346 }
1347
1348 /* replace/set the username in the first EAP/XAuth auth_cfg, also look for
1349 * a suitable remote ID.
1350 * note that adding the identity here is not fully thread-safe as the
1351 * peer_cfg and in turn the auth_cfg could be in use. for the default use
1352 * case (setting user credentials before upping the connection) this will
1353 * not be a problem, though. */
1354 enumerator = found->create_auth_cfg_enumerator(found, TRUE);
1355 remote_auth = found->create_auth_cfg_enumerator(found, FALSE);
1356 while (enumerator->enumerate(enumerator, (void**)&auth_cfg))
1357 {
1358 if (remote_auth->enumerate(remote_auth, (void**)&remote_cfg))
1359 { /* fall back on rightid, in case aaa_identity is not specified */
1360 identity = remote_cfg->get(remote_cfg, AUTH_RULE_IDENTITY);
1361 if (identity && identity->get_type(identity) != ID_ANY)
1362 {
1363 gw = identity;
1364 }
1365 }
1366
1367 auth_class = (uintptr_t)auth_cfg->get(auth_cfg, AUTH_RULE_AUTH_CLASS);
1368 if (auth_class == AUTH_CLASS_EAP || auth_class == AUTH_CLASS_XAUTH)
1369 {
1370 if (auth_class == AUTH_CLASS_EAP)
1371 {
1372 auth_cfg->add(auth_cfg, AUTH_RULE_EAP_IDENTITY, id->clone(id));
1373 /* if aaa_identity is specified use that as remote ID */
1374 identity = auth_cfg->get(auth_cfg, AUTH_RULE_AAA_IDENTITY);
1375 if (identity && identity->get_type(identity) != ID_ANY)
1376 {
1377 gw = identity;
1378 }
1379 DBG1(DBG_CFG, " configured EAP-Identity %Y", id);
1380 }
1381 else
1382 {
1383 auth_cfg->add(auth_cfg, AUTH_RULE_XAUTH_IDENTITY,
1384 id->clone(id));
1385 DBG1(DBG_CFG, " configured XAuth username %Y", id);
1386 }
1387 type = SHARED_EAP;
1388 break;
1389 }
1390 }
1391 enumerator->destroy(enumerator);
1392 remote_auth->destroy(remote_auth);
1393 /* clone the gw ID before unlocking the mutex */
1394 if (gw)
1395 {
1396 gw = gw->clone(gw);
1397 }
1398 this->mutex->unlock(this->mutex);
1399
1400 if (type == SHARED_ANY)
1401 {
1402 DBG1(DBG_CFG, " config '%s' unsuitable for user credentials",
1403 msg->user_creds.name);
1404 fprintf(prompt, "config '%s' unsuitable for user credentials\n",
1405 msg->user_creds.name);
1406 id->destroy(id);
1407 DESTROY_IF(gw);
1408 return;
1409 }
1410
1411 if (msg->user_creds.password)
1412 {
1413 char *pass;
1414
1415 pass = msg->user_creds.password;
1416 password = chunk_clone(chunk_create(pass, strlen(pass)));
1417 memwipe(pass, strlen(pass));
1418 }
1419 else
1420 { /* prompt the user for the password */
1421 char buf[256];
1422
1423 fprintf(prompt, "Password:\n");
1424 if (fgets(buf, sizeof(buf), prompt))
1425 {
1426 password = chunk_clone(chunk_create(buf, strlen(buf)));
1427 if (password.len > 0)
1428 { /* trim trailing \n */
1429 password.len--;
1430 }
1431 memwipe(buf, sizeof(buf));
1432 }
1433 }
1434
1435 if (password.len)
1436 {
1437 shared_key_t *shared;
1438 linked_list_t *owners;
1439
1440 shared = shared_key_create(type, password);
1441
1442 owners = linked_list_create();
1443 owners->insert_last(owners, id->clone(id));
1444 if (gw && gw->get_type(gw) != ID_ANY)
1445 {
1446 owners->insert_last(owners, gw->clone(gw));
1447 DBG1(DBG_CFG, " added %N secret for %Y %Y", shared_key_type_names,
1448 type, id, gw);
1449 }
1450 else
1451 {
1452 DBG1(DBG_CFG, " added %N secret for %Y", shared_key_type_names,
1453 type, id);
1454 }
1455 this->cred->add_shared(this->cred, shared, owners);
1456 DBG4(DBG_CFG, " secret: %#B", &password);
1457 }
1458 else
1459 { /* in case a user answers the password prompt by just pressing enter */
1460 chunk_clear(&password);
1461 }
1462 id->destroy(id);
1463 DESTROY_IF(gw);
1464 }
1465
1466 METHOD(stroke_config_t, destroy, void,
1467 private_stroke_config_t *this)
1468 {
1469 this->list->destroy_offset(this->list, offsetof(peer_cfg_t, destroy));
1470 this->mutex->destroy(this->mutex);
1471 free(this);
1472 }
1473
1474 /*
1475 * see header file
1476 */
1477 stroke_config_t *stroke_config_create(stroke_ca_t *ca, stroke_cred_t *cred,
1478 stroke_attribute_t *attributes)
1479 {
1480 private_stroke_config_t *this;
1481
1482 INIT(this,
1483 .public = {
1484 .backend = {
1485 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
1486 .create_ike_cfg_enumerator = _create_ike_cfg_enumerator,
1487 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
1488 },
1489 .add = _add,
1490 .del = _del,
1491 .set_user_credentials = _set_user_credentials,
1492 .destroy = _destroy,
1493 },
1494 .list = linked_list_create(),
1495 .mutex = mutex_create(MUTEX_TYPE_RECURSIVE),
1496 .ca = ca,
1497 .cred = cred,
1498 .attributes = attributes,
1499 );
1500
1501 return &this->public;
1502 }