2 * Copyright (C) 2012-2014 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
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>.
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
17 #include "stroke_config.h"
21 #include <threading/mutex.h>
22 #include <utils/lexparser.h>
26 typedef struct private_stroke_config_t private_stroke_config_t
;
29 * private data of stroke_config
31 struct private_stroke_config_t
{
36 stroke_config_t
public;
44 * mutex to lock config list
59 * Virtual IP pool / DNS backend
61 stroke_attribute_t
*attributes
;
64 METHOD(backend_t
, create_peer_cfg_enumerator
, enumerator_t
*,
65 private_stroke_config_t
*this, identification_t
*me
, identification_t
*other
)
67 this->mutex
->lock(this->mutex
);
68 return enumerator_create_cleaner(this->list
->create_enumerator(this->list
),
69 (void*)this->mutex
->unlock
, this->mutex
);
73 * filter function for ike configs
75 static bool ike_filter(void *data
, peer_cfg_t
**in
, ike_cfg_t
**out
)
77 *out
= (*in
)->get_ike_cfg(*in
);
81 METHOD(backend_t
, create_ike_cfg_enumerator
, enumerator_t
*,
82 private_stroke_config_t
*this, host_t
*me
, host_t
*other
)
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
);
90 METHOD(backend_t
, get_peer_cfg_by_name
, peer_cfg_t
*,
91 private_stroke_config_t
*this, char *name
)
93 enumerator_t
*e1
, *e2
;
94 peer_cfg_t
*current
, *found
= NULL
;
97 this->mutex
->lock(this->mutex
);
98 e1
= this->list
->create_enumerator(this->list
);
99 while (e1
->enumerate(e1
, ¤t
))
101 /* compare peer_cfgs name first */
102 if (streq(current
->get_name(current
), name
))
105 found
->get_ref(found
);
108 /* compare all child_cfg names otherwise */
109 e2
= current
->create_child_cfg_enumerator(current
);
110 while (e2
->enumerate(e2
, &child
))
112 if (streq(child
->get_name(child
), name
))
115 found
->get_ref(found
);
126 this->mutex
->unlock(this->mutex
);
131 * parse a proposal string, either into ike_cfg or child_cfg
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
)
140 proposal_t
*proposal
;
142 strict
= string
+ strlen(string
) - 1;
151 while ((single
= strsep(&string
, ",")))
153 proposal
= proposal_create_from_string(proto
, single
);
158 ike_cfg
->add_proposal(ike_cfg
, proposal
);
162 child_cfg
->add_proposal(child_cfg
, proposal
);
166 DBG1(DBG_CFG
, "skipped invalid proposal string: %s", single
);
172 /* add default porposal to the end if not strict */
176 ike_cfg
->add_proposal(ike_cfg
, proposal_create_default(proto
));
177 ike_cfg
->add_proposal(ike_cfg
, proposal_create_default_aead(proto
));
181 child_cfg
->add_proposal(child_cfg
, proposal_create_default(proto
));
182 child_cfg
->add_proposal(child_cfg
, proposal_create_default_aead(proto
));
187 * Build an IKE config from a stroke message
189 static ike_cfg_t
*build_ike_cfg(private_stroke_config_t
*this, stroke_msg_t
*msg
)
191 enumerator_t
*enumerator
;
192 stroke_end_t tmp_end
;
196 char me
[256], other
[256], *token
;
197 bool swapped
= FALSE
;;
199 enumerator
= enumerator_create_token(msg
->add_conn
.other
.address
, ",", " ");
200 while (enumerator
->enumerate(enumerator
, &token
))
202 if (!strchr(token
, '/'))
204 host
= host_create_from_dns(token
, 0, 0);
207 if (hydra
->kernel_interface
->get_interface(
208 hydra
->kernel_interface
, host
, NULL
))
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
;
220 enumerator
->destroy(enumerator
);
224 enumerator
= enumerator_create_token(msg
->add_conn
.me
.address
, ",", " ");
225 while (enumerator
->enumerate(enumerator
, &token
))
227 if (!strchr(token
, '/'))
229 host
= host_create_from_dns(token
, 0, 0);
232 if (!hydra
->kernel_interface
->get_interface(
233 hydra
->kernel_interface
, host
, NULL
))
235 DBG1(DBG_CFG
, "left nor right host is our side, "
236 "assuming left=local");
242 enumerator
->destroy(enumerator
);
245 if (msg
->add_conn
.me
.allow_any
)
247 snprintf(me
, sizeof(me
), "%s,0.0.0.0/0,::/0",
248 msg
->add_conn
.me
.address
);
250 if (msg
->add_conn
.other
.allow_any
)
252 snprintf(other
, sizeof(other
), "%s,0.0.0.0/0,::/0",
253 msg
->add_conn
.other
.address
);
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
,
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
);
270 add_proposals(this, msg
->add_conn
.algorithms
.ike
, ike_cfg
, NULL
, PROTO_IKE
);
275 * Add CRL constraint to config
277 static void build_crl_policy(auth_cfg_t
*cfg
, bool local
, int policy
)
279 /* CRL/OCSP policy, for remote config only */
285 /* if yes, we require a GOOD validation */
286 cfg
->add(cfg
, AUTH_RULE_CRL_VALIDATION
, VALIDATION_GOOD
);
288 case CRL_STRICT_IFURI
:
289 /* for ifuri, a SKIPPED validation is sufficient */
290 cfg
->add(cfg
, AUTH_RULE_CRL_VALIDATION
, VALIDATION_SKIPPED
);
299 * Parse public key / signature strength constraints
301 static void parse_pubkey_constraints(char *auth
, auth_cfg_t
*cfg
)
303 enumerator_t
*enumerator
;
304 bool rsa
= FALSE
, ecdsa
= FALSE
, rsa_len
= FALSE
, ecdsa_len
= FALSE
;
308 enumerator
= enumerator_create_token(auth
, "-", "");
309 while (enumerator
->enumerate(enumerator
, &token
))
315 signature_scheme_t scheme
;
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
, },
333 if (rsa_len
|| ecdsa_len
)
334 { /* expecting a key strength token */
335 strength
= atoi(token
);
340 cfg
->add(cfg
, AUTH_RULE_RSA_STRENGTH
, (uintptr_t)strength
);
344 cfg
->add(cfg
, AUTH_RULE_ECDSA_STRENGTH
, (uintptr_t)strength
);
347 rsa_len
= ecdsa_len
= FALSE
;
353 if (streq(token
, "rsa"))
355 rsa
= rsa_len
= TRUE
;
358 if (streq(token
, "ecdsa"))
360 ecdsa
= ecdsa_len
= TRUE
;
363 if (streq(token
, "pubkey"))
368 for (i
= 0; i
< countof(schemes
); i
++)
370 if (streq(schemes
[i
].name
, token
))
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
377 if ((rsa
&& schemes
[i
].key
== KEY_RSA
) ||
378 (ecdsa
&& schemes
[i
].key
== KEY_ECDSA
) ||
381 cfg
->add(cfg
, AUTH_RULE_SIGNATURE_SCHEME
,
382 (uintptr_t)schemes
[i
].scheme
);
389 DBG1(DBG_CFG
, "ignoring invalid auth token: '%s'", token
);
392 enumerator
->destroy(enumerator
);
396 * build authentication config
398 static auth_cfg_t
*build_auth_cfg(private_stroke_config_t
*this,
399 stroke_msg_t
*msg
, bool local
, bool primary
)
401 identification_t
*identity
;
402 certificate_t
*certificate
;
403 char *auth
, *id
, *pubkey
, *cert
, *ca
, *groups
;
404 stroke_end_t
*end
, *other_end
;
411 end
= &msg
->add_conn
.me
;
412 other_end
= &msg
->add_conn
.other
;
416 end
= &msg
->add_conn
.other
;
417 other_end
= &msg
->add_conn
.me
;
424 { /* leftid/rightid fallback to address */
429 if (ca
&& streq(ca
, "%same"))
439 { /* leftid2 falls back to leftid */
444 if (ca
&& streq(ca
, "%same"))
449 if (id
&& *id
== '%' && !streq(id
, "%any") && !streq(id
, "%any6"))
450 { /* has only an effect on rightid/2 */
462 { /* no second authentication round, fine. But load certificates
463 * for other purposes (EAP-TLS) */
466 certificate
= this->cred
->load_peer(this->cred
, cert
);
469 certificate
->destroy(certificate
);
476 cfg
= auth_cfg_create();
478 /* add identity and peer certificate */
479 identity
= identification_create_from_string(id
);
482 enumerator_t
*enumerator
;
483 bool has_subject
= FALSE
;
484 certificate_t
*first
= NULL
;
486 enumerator
= enumerator_create_token(cert
, ",", " ");
487 while (enumerator
->enumerate(enumerator
, &cert
))
489 certificate
= this->cred
->load_peer(this->cred
, cert
);
494 this->ca
->check_for_hash_and_url(this->ca
, certificate
);
496 cfg
->add(cfg
, AUTH_RULE_SUBJECT_CERT
, certificate
);
501 if (identity
->get_type(identity
) != ID_ANY
&&
502 certificate
->has_subject(certificate
, identity
))
508 enumerator
->destroy(enumerator
);
510 if (first
&& !has_subject
)
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
);
519 /* add raw RSA public key */
520 pubkey
= end
->rsakey
;
521 if (pubkey
&& !streq(pubkey
, "") && !streq(pubkey
, "%cert"))
523 certificate
= this->cred
->load_pubkey(this->cred
, pubkey
, identity
);
526 cfg
->add(cfg
, AUTH_RULE_SUBJECT_CERT
, certificate
);
529 if (identity
->get_type(identity
) != ID_ANY
)
531 cfg
->add(cfg
, AUTH_RULE_IDENTITY
, identity
);
534 cfg
->add(cfg
, AUTH_RULE_IDENTITY_LOOSE
, TRUE
);
539 identity
->destroy(identity
);
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
);
551 cfg
->add(cfg
, AUTH_RULE_CA_CERT
, certificate
);
555 DBG1(DBG_CFG
, "CA certificate \"%s\" not found, discarding CA "
561 groups
= primary ? end
->groups
: end
->groups2
;
564 enumerator_t
*enumerator
;
567 enumerator
= enumerator_create_token(groups
, ",", " ");
568 while (enumerator
->enumerate(enumerator
, &group
))
570 cfg
->add(cfg
, AUTH_RULE_GROUP
,
571 identification_create_from_string(group
));
573 enumerator
->destroy(enumerator
);
576 /* certificatePolicies */
577 if (end
->cert_policy
)
579 enumerator_t
*enumerator
;
582 enumerator
= enumerator_create_token(end
->cert_policy
, ",", " ");
583 while (enumerator
->enumerate(enumerator
, &policy
))
585 cfg
->add(cfg
, AUTH_RULE_CERT_POLICY
, strdup(policy
));
587 enumerator
->destroy(enumerator
);
590 /* authentication metod (class, actually) */
591 if (strpfx(auth
, "pubkey") ||
592 strpfx(auth
, "rsa") ||
593 strpfx(auth
, "ecdsa"))
595 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
596 build_crl_policy(cfg
, local
, msg
->add_conn
.crl_policy
);
598 parse_pubkey_constraints(auth
, cfg
);
600 else if (streq(auth
, "psk") || streq(auth
, "secret"))
602 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PSK
);
604 else if (strpfx(auth
, "xauth"))
608 pos
= strchr(auth
, '-');
611 cfg
->add(cfg
, AUTH_RULE_XAUTH_BACKEND
, strdup(++pos
));
613 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_XAUTH
);
614 if (msg
->add_conn
.xauth_identity
)
616 cfg
->add(cfg
, AUTH_RULE_XAUTH_IDENTITY
,
617 identification_create_from_string(msg
->add_conn
.xauth_identity
));
620 else if (strpfx(auth
, "eap"))
622 eap_vendor_type_t
*type
;
624 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_EAP
);
626 type
= eap_vendor_type_from_string(auth
);
629 cfg
->add(cfg
, AUTH_RULE_EAP_TYPE
, type
->type
);
632 cfg
->add(cfg
, AUTH_RULE_EAP_VENDOR
, type
->vendor
);
637 if (msg
->add_conn
.eap_identity
)
639 if (streq(msg
->add_conn
.eap_identity
, "%identity"))
641 identity
= identification_create_from_encoding(ID_ANY
,
646 identity
= identification_create_from_string(
647 msg
->add_conn
.eap_identity
);
649 cfg
->add(cfg
, AUTH_RULE_EAP_IDENTITY
, identity
);
651 if (msg
->add_conn
.aaa_identity
)
653 cfg
->add(cfg
, AUTH_RULE_AAA_IDENTITY
,
654 identification_create_from_string(msg
->add_conn
.aaa_identity
));
659 if (!streq(auth
, "any"))
661 DBG1(DBG_CFG
, "authentication method %s unknown, fallback to any",
664 build_crl_policy(cfg
, local
, msg
->add_conn
.crl_policy
);
670 * build a mem_pool_t from an address range
672 static mem_pool_t
*create_pool_range(char *str
)
677 if (!host_create_from_range(str
, &from
, &to
))
681 pool
= mem_pool_create_range(str
, from
, to
);
688 * build a peer_cfg from a stroke msg
690 static peer_cfg_t
*build_peer_cfg(private_stroke_config_t
*this,
691 stroke_msg_t
*msg
, ike_cfg_t
*ike_cfg
)
693 identification_t
*peer_id
= NULL
;
694 peer_cfg_t
*mediated_by
= NULL
;
695 unique_policy_t unique
;
696 u_int32_t rekey
= 0, reauth
= 0, over
, jitter
;
697 peer_cfg_t
*peer_cfg
;
698 auth_cfg_t
*auth_cfg
;
701 if (msg
->add_conn
.ikeme
.mediation
&& msg
->add_conn
.ikeme
.mediated_by
)
703 DBG1(DBG_CFG
, "a mediation connection cannot be a mediated connection "
704 "at the same time, aborting");
708 if (msg
->add_conn
.ikeme
.mediation
)
710 /* force unique connections for mediation connections */
711 msg
->add_conn
.unique
= 1;
714 if (msg
->add_conn
.ikeme
.mediated_by
)
716 mediated_by
= charon
->backends
->get_peer_cfg_by_name(charon
->backends
,
717 msg
->add_conn
.ikeme
.mediated_by
);
720 DBG1(DBG_CFG
, "mediation connection '%s' not found, aborting",
721 msg
->add_conn
.ikeme
.mediated_by
);
724 if (!mediated_by
->is_mediation(mediated_by
))
726 DBG1(DBG_CFG
, "connection '%s' as referred to by '%s' is "
727 "no mediation connection, aborting",
728 msg
->add_conn
.ikeme
.mediated_by
, msg
->add_conn
.name
);
729 mediated_by
->destroy(mediated_by
);
732 if (msg
->add_conn
.ikeme
.peerid
)
734 peer_id
= identification_create_from_string(msg
->add_conn
.ikeme
.peerid
);
736 else if (msg
->add_conn
.other
.id
)
738 peer_id
= identification_create_from_string(msg
->add_conn
.other
.id
);
743 jitter
= msg
->add_conn
.rekey
.margin
* msg
->add_conn
.rekey
.fuzz
/ 100;
744 over
= msg
->add_conn
.rekey
.margin
;
745 if (msg
->add_conn
.rekey
.reauth
)
747 reauth
= msg
->add_conn
.rekey
.ike_lifetime
- over
;
751 rekey
= msg
->add_conn
.rekey
.ike_lifetime
- over
;
753 switch (msg
->add_conn
.unique
)
756 case 2: /* replace */
757 unique
= UNIQUE_REPLACE
;
760 unique
= UNIQUE_KEEP
;
763 unique
= UNIQUE_NEVER
;
769 if (msg
->add_conn
.dpd
.action
== 0)
770 { /* dpdaction=none disables DPD */
771 msg
->add_conn
.dpd
.delay
= 0;
774 /* other.sourceip is managed in stroke_attributes. If it is set, we define
775 * the pool name as the connection name, which the attribute provider
776 * uses to serve pool addresses. */
777 peer_cfg
= peer_cfg_create(msg
->add_conn
.name
, ike_cfg
,
778 msg
->add_conn
.me
.sendcert
, unique
,
779 msg
->add_conn
.rekey
.tries
, rekey
, reauth
, jitter
, over
,
780 msg
->add_conn
.mobike
, msg
->add_conn
.aggressive
,
781 msg
->add_conn
.pushmode
== 0,
782 msg
->add_conn
.dpd
.delay
, msg
->add_conn
.dpd
.timeout
,
783 msg
->add_conn
.ikeme
.mediation
, mediated_by
, peer_id
);
785 if (msg
->add_conn
.other
.sourceip
)
787 enumerator_t
*enumerator
;
790 enumerator
= enumerator_create_token(msg
->add_conn
.other
.sourceip
,
792 while (enumerator
->enumerate(enumerator
, &token
))
794 if (streq(token
, "%modeconfig") || streq(token
, "%modecfg") ||
795 streq(token
, "%config") || streq(token
, "%cfg") ||
796 streq(token
, "%config4") || streq(token
, "%config6"))
798 /* empty pool, uses connection name */
799 this->attributes
->add_pool(this->attributes
,
800 mem_pool_create(msg
->add_conn
.name
, NULL
, 0));
801 peer_cfg
->add_pool(peer_cfg
, msg
->add_conn
.name
);
803 else if (*token
== '%')
805 /* external named pool */
806 peer_cfg
->add_pool(peer_cfg
, token
+ 1);
810 /* in-memory pool, using range or CIDR notation */
815 pool
= create_pool_range(token
);
818 base
= host_create_from_subnet(token
, &bits
);
821 pool
= mem_pool_create(token
, base
, bits
);
827 this->attributes
->add_pool(this->attributes
, pool
);
828 peer_cfg
->add_pool(peer_cfg
, token
);
832 DBG1(DBG_CFG
, "IP pool %s invalid, ignored", token
);
836 enumerator
->destroy(enumerator
);
839 if (msg
->add_conn
.me
.sourceip
&& msg
->add_conn
.other
.sourceip
)
841 DBG1(DBG_CFG
, "'%s' has both left- and rightsourceip, but IKE can "
842 "negotiate one virtual IP only, ignoring local virtual IP",
845 else if (msg
->add_conn
.me
.sourceip
)
847 enumerator_t
*enumerator
;
850 enumerator
= enumerator_create_token(msg
->add_conn
.me
.sourceip
, ",", " ");
851 while (enumerator
->enumerate(enumerator
, &token
))
855 if (streq(token
, "%modeconfig") || streq(token
, "%modecfg") ||
856 streq(token
, "%config") || streq(token
, "%cfg"))
857 { /* try to deduce an address family */
858 if (msg
->add_conn
.me
.subnets
)
859 { /* use the same family as in local subnet, if any */
860 if (strchr(msg
->add_conn
.me
.subnets
, '.'))
862 vip
= host_create_any(AF_INET
);
866 vip
= host_create_any(AF_INET6
);
869 else if (msg
->add_conn
.other
.subnets
)
870 { /* use the same family as in remote subnet, if any */
871 if (strchr(msg
->add_conn
.other
.subnets
, '.'))
873 vip
= host_create_any(AF_INET
);
877 vip
= host_create_any(AF_INET6
);
882 char *addr
, *next
, *hit
;
884 /* guess virtual IP family based on local address. If
885 * multiple addresses are specified, we look at the first
886 * only, as with leftallowany a ::/0 is always appended. */
887 addr
= ike_cfg
->get_my_addr(ike_cfg
);
888 next
= strchr(addr
, ',');
889 hit
= strchr(addr
, ':');
890 if (hit
&& (!next
|| hit
< next
))
892 vip
= host_create_any(AF_INET6
);
896 vip
= host_create_any(AF_INET
);
900 else if (streq(token
, "%config4"))
902 vip
= host_create_any(AF_INET
);
904 else if (streq(token
, "%config6"))
906 vip
= host_create_any(AF_INET6
);
910 vip
= host_create_from_string(token
, 0);
913 DBG1(DBG_CFG
, "ignored invalid subnet token: %s", token
);
919 peer_cfg
->add_virtual_ip(peer_cfg
, vip
);
922 enumerator
->destroy(enumerator
);
925 /* build leftauth= */
926 auth_cfg
= build_auth_cfg(this, msg
, TRUE
, TRUE
);
929 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, TRUE
);
932 { /* we require at least one config on our side */
933 peer_cfg
->destroy(peer_cfg
);
936 /* build leftauth2= */
937 auth_cfg
= build_auth_cfg(this, msg
, TRUE
, FALSE
);
940 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, TRUE
);
942 /* build rightauth= */
943 auth_cfg
= build_auth_cfg(this, msg
, FALSE
, TRUE
);
946 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, FALSE
);
948 /* build rightauth2= */
949 auth_cfg
= build_auth_cfg(this, msg
, FALSE
, FALSE
);
952 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, FALSE
);
958 * Parse a protoport specifier
960 static bool parse_protoport(char *token
, u_int16_t
*from_port
,
961 u_int16_t
*to_port
, u_int8_t
*protocol
)
963 char *sep
, *port
= "", *endptr
;
964 struct protoent
*proto
;
968 sep
= strrchr(token
, ']');
975 sep
= strchr(token
, '/');
977 { /* protocol/port */
982 if (streq(token
, "%any"))
988 proto
= getprotobyname(token
);
991 *protocol
= proto
->p_proto
;
995 p
= strtol(token
, &endptr
, 0);
996 if ((*token
&& *endptr
) || p
< 0 || p
> 0xff)
1000 *protocol
= (u_int8_t
)p
;
1003 if (streq(port
, "%any"))
1008 else if (streq(port
, "%opaque"))
1010 *from_port
= 0xffff;
1015 svc
= getservbyname(port
, NULL
);
1018 *from_port
= *to_port
= ntohs(svc
->s_port
);
1022 p
= strtol(port
, &endptr
, 0);
1023 if (p
< 0 || p
> 0xffff)
1031 p
= strtol(port
, &endptr
, 0);
1032 if (p
< 0 || p
> 0xffff)
1048 * build a traffic selector from a stroke_end
1050 static void add_ts(private_stroke_config_t
*this,
1051 stroke_end_t
*end
, child_cfg_t
*child_cfg
, bool local
)
1053 traffic_selector_t
*ts
;
1057 ts
= traffic_selector_create_dynamic(end
->protocol
,
1058 end
->from_port
, end
->to_port
);
1059 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
1067 net
= host_create_from_string(end
->address
, 0);
1070 ts
= traffic_selector_create_from_subnet(net
, 0, end
->protocol
,
1071 end
->from_port
, end
->to_port
);
1072 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
1077 enumerator_t
*enumerator
;
1079 u_int16_t from_port
, to_port
;
1082 enumerator
= enumerator_create_token(end
->subnets
, ",", " ");
1083 while (enumerator
->enumerate(enumerator
, &subnet
))
1085 from_port
= end
->from_port
;
1086 to_port
= end
->to_port
;
1087 proto
= end
->protocol
;
1089 pos
= strchr(subnet
, '[');
1093 if (!parse_protoport(pos
, &from_port
, &to_port
, &proto
))
1095 DBG1(DBG_CFG
, "invalid proto/port: %s, skipped subnet",
1100 if (streq(subnet
, "%dynamic"))
1102 ts
= traffic_selector_create_dynamic(proto
,
1103 from_port
, to_port
);
1107 ts
= traffic_selector_create_from_cidr(subnet
, proto
,
1108 from_port
, to_port
);
1112 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
1116 DBG1(DBG_CFG
, "invalid subnet: %s, skipped", subnet
);
1119 enumerator
->destroy(enumerator
);
1125 * map starter magic values to our action type
1127 static action_t
map_action(int starter_action
)
1129 switch (starter_action
)
1132 return ACTION_ROUTE
;
1133 case 3: /* =restart */
1134 return ACTION_RESTART
;
1141 * build a child config from the stroke message
1143 static child_cfg_t
*build_child_cfg(private_stroke_config_t
*this,
1146 child_cfg_t
*child_cfg
;
1147 lifetime_cfg_t lifetime
= {
1149 .life
= msg
->add_conn
.rekey
.ipsec_lifetime
,
1150 .rekey
= msg
->add_conn
.rekey
.ipsec_lifetime
- msg
->add_conn
.rekey
.margin
,
1151 .jitter
= msg
->add_conn
.rekey
.margin
* msg
->add_conn
.rekey
.fuzz
/ 100
1154 .life
= msg
->add_conn
.rekey
.life_bytes
,
1155 .rekey
= msg
->add_conn
.rekey
.life_bytes
- msg
->add_conn
.rekey
.margin_bytes
,
1156 .jitter
= msg
->add_conn
.rekey
.margin_bytes
* msg
->add_conn
.rekey
.fuzz
/ 100
1159 .life
= msg
->add_conn
.rekey
.life_packets
,
1160 .rekey
= msg
->add_conn
.rekey
.life_packets
- msg
->add_conn
.rekey
.margin_packets
,
1161 .jitter
= msg
->add_conn
.rekey
.margin_packets
* msg
->add_conn
.rekey
.fuzz
/ 100
1165 .value
= msg
->add_conn
.mark_in
.value
,
1166 .mask
= msg
->add_conn
.mark_in
.mask
1169 .value
= msg
->add_conn
.mark_out
.value
,
1170 .mask
= msg
->add_conn
.mark_out
.mask
1173 child_cfg
= child_cfg_create(
1174 msg
->add_conn
.name
, &lifetime
, msg
->add_conn
.me
.updown
,
1175 msg
->add_conn
.me
.hostaccess
, msg
->add_conn
.mode
, ACTION_NONE
,
1176 map_action(msg
->add_conn
.dpd
.action
),
1177 map_action(msg
->add_conn
.close_action
), msg
->add_conn
.ipcomp
,
1178 msg
->add_conn
.inactivity
, msg
->add_conn
.reqid
,
1179 &mark_in
, &mark_out
, msg
->add_conn
.tfc
);
1180 if (msg
->add_conn
.replay_window
!= -1)
1182 child_cfg
->set_replay_window(child_cfg
, msg
->add_conn
.replay_window
);
1184 child_cfg
->set_mipv6_options(child_cfg
, msg
->add_conn
.proxy_mode
,
1185 msg
->add_conn
.install_policy
);
1186 add_ts(this, &msg
->add_conn
.me
, child_cfg
, TRUE
);
1187 add_ts(this, &msg
->add_conn
.other
, child_cfg
, FALSE
);
1189 if (msg
->add_conn
.algorithms
.ah
)
1191 add_proposals(this, msg
->add_conn
.algorithms
.ah
,
1192 NULL
, child_cfg
, PROTO_AH
);
1196 add_proposals(this, msg
->add_conn
.algorithms
.esp
,
1197 NULL
, child_cfg
, PROTO_ESP
);
1202 METHOD(stroke_config_t
, add
, void,
1203 private_stroke_config_t
*this, stroke_msg_t
*msg
)
1205 ike_cfg_t
*ike_cfg
, *existing_ike
;
1206 peer_cfg_t
*peer_cfg
, *existing
;
1207 child_cfg_t
*child_cfg
;
1208 enumerator_t
*enumerator
;
1209 bool use_existing
= FALSE
;
1211 ike_cfg
= build_ike_cfg(this, msg
);
1216 peer_cfg
= build_peer_cfg(this, msg
, ike_cfg
);
1219 ike_cfg
->destroy(ike_cfg
);
1223 enumerator
= create_peer_cfg_enumerator(this, NULL
, NULL
);
1224 while (enumerator
->enumerate(enumerator
, &existing
))
1226 existing_ike
= existing
->get_ike_cfg(existing
);
1227 if (existing
->equals(existing
, peer_cfg
) &&
1228 existing_ike
->equals(existing_ike
, peer_cfg
->get_ike_cfg(peer_cfg
)))
1230 use_existing
= TRUE
;
1231 peer_cfg
->destroy(peer_cfg
);
1232 peer_cfg
= existing
;
1233 peer_cfg
->get_ref(peer_cfg
);
1234 DBG1(DBG_CFG
, "added child to existing configuration '%s'",
1235 peer_cfg
->get_name(peer_cfg
));
1239 enumerator
->destroy(enumerator
);
1241 child_cfg
= build_child_cfg(this, msg
);
1244 peer_cfg
->destroy(peer_cfg
);
1247 peer_cfg
->add_child_cfg(peer_cfg
, child_cfg
);
1251 peer_cfg
->destroy(peer_cfg
);
1255 /* add config to backend */
1256 DBG1(DBG_CFG
, "added configuration '%s'", msg
->add_conn
.name
);
1257 this->mutex
->lock(this->mutex
);
1258 this->list
->insert_last(this->list
, peer_cfg
);
1259 this->mutex
->unlock(this->mutex
);
1263 METHOD(stroke_config_t
, del
, void,
1264 private_stroke_config_t
*this, stroke_msg_t
*msg
)
1266 enumerator_t
*enumerator
, *children
;
1269 bool deleted
= FALSE
;
1271 this->mutex
->lock(this->mutex
);
1272 enumerator
= this->list
->create_enumerator(this->list
);
1273 while (enumerator
->enumerate(enumerator
, &peer
))
1277 /* remove any child with such a name */
1278 children
= peer
->create_child_cfg_enumerator(peer
);
1279 while (children
->enumerate(children
, &child
))
1281 if (streq(child
->get_name(child
), msg
->del_conn
.name
))
1283 peer
->remove_child_cfg(peer
, children
);
1284 child
->destroy(child
);
1292 children
->destroy(children
);
1294 /* if peer config has no children anymore, remove it */
1297 this->list
->remove_at(this->list
, enumerator
);
1298 peer
->destroy(peer
);
1301 enumerator
->destroy(enumerator
);
1302 this->mutex
->unlock(this->mutex
);
1306 DBG1(DBG_CFG
, "deleted connection '%s'", msg
->del_conn
.name
);
1310 DBG1(DBG_CFG
, "connection '%s' not found", msg
->del_conn
.name
);
1314 METHOD(stroke_config_t
, set_user_credentials
, void,
1315 private_stroke_config_t
*this, stroke_msg_t
*msg
, FILE *prompt
)
1317 enumerator_t
*enumerator
, *children
, *remote_auth
;
1318 peer_cfg_t
*peer
, *found
= NULL
;
1319 auth_cfg_t
*auth_cfg
, *remote_cfg
;
1320 auth_class_t auth_class
;
1322 identification_t
*id
, *identity
, *gw
= NULL
;
1323 shared_key_type_t type
= SHARED_ANY
;
1324 chunk_t password
= chunk_empty
;
1326 this->mutex
->lock(this->mutex
);
1327 enumerator
= this->list
->create_enumerator(this->list
);
1328 while (enumerator
->enumerate(enumerator
, (void**)&peer
))
1329 { /* find the peer (or child) config with the given name */
1330 if (streq(peer
->get_name(peer
), msg
->user_creds
.name
))
1336 children
= peer
->create_child_cfg_enumerator(peer
);
1337 while (children
->enumerate(children
, &child
))
1339 if (streq(child
->get_name(child
), msg
->user_creds
.name
))
1345 children
->destroy(children
);
1353 enumerator
->destroy(enumerator
);
1357 DBG1(DBG_CFG
, " no config named '%s'", msg
->user_creds
.name
);
1358 fprintf(prompt
, "no config named '%s'\n", msg
->user_creds
.name
);
1359 this->mutex
->unlock(this->mutex
);
1363 id
= identification_create_from_string(msg
->user_creds
.username
);
1364 if (strlen(msg
->user_creds
.username
) == 0 ||
1365 !id
|| id
->get_type(id
) == ID_ANY
)
1367 DBG1(DBG_CFG
, " invalid username '%s'", msg
->user_creds
.username
);
1368 fprintf(prompt
, "invalid username '%s'\n", msg
->user_creds
.username
);
1369 this->mutex
->unlock(this->mutex
);
1374 /* replace/set the username in the first EAP/XAuth auth_cfg, also look for
1375 * a suitable remote ID.
1376 * note that adding the identity here is not fully thread-safe as the
1377 * peer_cfg and in turn the auth_cfg could be in use. for the default use
1378 * case (setting user credentials before upping the connection) this will
1379 * not be a problem, though. */
1380 enumerator
= found
->create_auth_cfg_enumerator(found
, TRUE
);
1381 remote_auth
= found
->create_auth_cfg_enumerator(found
, FALSE
);
1382 while (enumerator
->enumerate(enumerator
, (void**)&auth_cfg
))
1384 if (remote_auth
->enumerate(remote_auth
, (void**)&remote_cfg
))
1385 { /* fall back on rightid, in case aaa_identity is not specified */
1386 identity
= remote_cfg
->get(remote_cfg
, AUTH_RULE_IDENTITY
);
1387 if (identity
&& identity
->get_type(identity
) != ID_ANY
)
1393 auth_class
= (uintptr_t)auth_cfg
->get(auth_cfg
, AUTH_RULE_AUTH_CLASS
);
1394 if (auth_class
== AUTH_CLASS_EAP
|| auth_class
== AUTH_CLASS_XAUTH
)
1396 if (auth_class
== AUTH_CLASS_EAP
)
1398 auth_cfg
->add(auth_cfg
, AUTH_RULE_EAP_IDENTITY
, id
->clone(id
));
1399 /* if aaa_identity is specified use that as remote ID */
1400 identity
= auth_cfg
->get(auth_cfg
, AUTH_RULE_AAA_IDENTITY
);
1401 if (identity
&& identity
->get_type(identity
) != ID_ANY
)
1405 DBG1(DBG_CFG
, " configured EAP-Identity %Y", id
);
1409 auth_cfg
->add(auth_cfg
, AUTH_RULE_XAUTH_IDENTITY
,
1411 DBG1(DBG_CFG
, " configured XAuth username %Y", id
);
1417 enumerator
->destroy(enumerator
);
1418 remote_auth
->destroy(remote_auth
);
1419 /* clone the gw ID before unlocking the mutex */
1424 this->mutex
->unlock(this->mutex
);
1426 if (type
== SHARED_ANY
)
1428 DBG1(DBG_CFG
, " config '%s' unsuitable for user credentials",
1429 msg
->user_creds
.name
);
1430 fprintf(prompt
, "config '%s' unsuitable for user credentials\n",
1431 msg
->user_creds
.name
);
1437 if (msg
->user_creds
.password
)
1441 pass
= msg
->user_creds
.password
;
1442 password
= chunk_clone(chunk_create(pass
, strlen(pass
)));
1443 memwipe(pass
, strlen(pass
));
1446 { /* prompt the user for the password */
1449 fprintf(prompt
, "Password:\n");
1450 if (fgets(buf
, sizeof(buf
), prompt
))
1452 password
= chunk_clone(chunk_create(buf
, strlen(buf
)));
1453 if (password
.len
> 0)
1454 { /* trim trailing \n */
1457 memwipe(buf
, sizeof(buf
));
1463 shared_key_t
*shared
;
1464 linked_list_t
*owners
;
1466 shared
= shared_key_create(type
, password
);
1468 owners
= linked_list_create();
1469 owners
->insert_last(owners
, id
->clone(id
));
1470 if (gw
&& gw
->get_type(gw
) != ID_ANY
)
1472 owners
->insert_last(owners
, gw
->clone(gw
));
1473 DBG1(DBG_CFG
, " added %N secret for %Y %Y", shared_key_type_names
,
1478 DBG1(DBG_CFG
, " added %N secret for %Y", shared_key_type_names
,
1481 this->cred
->add_shared(this->cred
, shared
, owners
);
1482 DBG4(DBG_CFG
, " secret: %#B", &password
);
1485 { /* in case a user answers the password prompt by just pressing enter */
1486 chunk_clear(&password
);
1492 METHOD(stroke_config_t
, destroy
, void,
1493 private_stroke_config_t
*this)
1495 this->list
->destroy_offset(this->list
, offsetof(peer_cfg_t
, destroy
));
1496 this->mutex
->destroy(this->mutex
);
1503 stroke_config_t
*stroke_config_create(stroke_ca_t
*ca
, stroke_cred_t
*cred
,
1504 stroke_attribute_t
*attributes
)
1506 private_stroke_config_t
*this;
1511 .create_peer_cfg_enumerator
= _create_peer_cfg_enumerator
,
1512 .create_ike_cfg_enumerator
= _create_ike_cfg_enumerator
,
1513 .get_peer_cfg_by_name
= _get_peer_cfg_by_name
,
1517 .set_user_credentials
= _set_user_credentials
,
1518 .destroy
= _destroy
,
1520 .list
= linked_list_create(),
1521 .mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
),
1524 .attributes
= attributes
,
1527 return &this->public;