2 * Copyright (C) 2012 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>
24 typedef struct private_stroke_config_t private_stroke_config_t
;
27 * private data of stroke_config
29 struct private_stroke_config_t
{
34 stroke_config_t
public;
42 * mutex to lock config list
57 * Virtual IP pool / DNS backend
59 stroke_attribute_t
*attributes
;
62 METHOD(backend_t
, create_peer_cfg_enumerator
, enumerator_t
*,
63 private_stroke_config_t
*this, identification_t
*me
, identification_t
*other
)
65 this->mutex
->lock(this->mutex
);
66 return enumerator_create_cleaner(this->list
->create_enumerator(this->list
),
67 (void*)this->mutex
->unlock
, this->mutex
);
71 * filter function for ike configs
73 static bool ike_filter(void *data
, peer_cfg_t
**in
, ike_cfg_t
**out
)
75 *out
= (*in
)->get_ike_cfg(*in
);
79 METHOD(backend_t
, create_ike_cfg_enumerator
, enumerator_t
*,
80 private_stroke_config_t
*this, host_t
*me
, host_t
*other
)
82 this->mutex
->lock(this->mutex
);
83 return enumerator_create_filter(this->list
->create_enumerator(this->list
),
84 (void*)ike_filter
, this->mutex
,
85 (void*)this->mutex
->unlock
);
88 METHOD(backend_t
, get_peer_cfg_by_name
, peer_cfg_t
*,
89 private_stroke_config_t
*this, char *name
)
91 enumerator_t
*e1
, *e2
;
92 peer_cfg_t
*current
, *found
= NULL
;
95 this->mutex
->lock(this->mutex
);
96 e1
= this->list
->create_enumerator(this->list
);
97 while (e1
->enumerate(e1
, ¤t
))
99 /* compare peer_cfgs name first */
100 if (streq(current
->get_name(current
), name
))
103 found
->get_ref(found
);
106 /* compare all child_cfg names otherwise */
107 e2
= current
->create_child_cfg_enumerator(current
);
108 while (e2
->enumerate(e2
, &child
))
110 if (streq(child
->get_name(child
), name
))
113 found
->get_ref(found
);
124 this->mutex
->unlock(this->mutex
);
129 * parse a proposal string, either into ike_cfg or child_cfg
131 static void add_proposals(private_stroke_config_t
*this, char *string
,
132 ike_cfg_t
*ike_cfg
, child_cfg_t
*child_cfg
)
138 proposal_t
*proposal
;
139 protocol_id_t proto
= PROTO_ESP
;
145 strict
= string
+ strlen(string
) - 1;
154 while ((single
= strsep(&string
, ",")))
156 proposal
= proposal_create_from_string(proto
, single
);
161 ike_cfg
->add_proposal(ike_cfg
, proposal
);
165 child_cfg
->add_proposal(child_cfg
, proposal
);
169 DBG1(DBG_CFG
, "skipped invalid proposal string: %s", single
);
175 /* add default porposal to the end if not strict */
179 ike_cfg
->add_proposal(ike_cfg
, proposal_create_default(PROTO_IKE
));
183 child_cfg
->add_proposal(child_cfg
, proposal_create_default(PROTO_ESP
));
188 * Build an IKE config from a stroke message
190 static ike_cfg_t
*build_ike_cfg(private_stroke_config_t
*this, stroke_msg_t
*msg
)
192 stroke_end_t tmp_end
;
197 host
= host_create_from_dns(msg
->add_conn
.other
.address
, 0, 0);
200 if (hydra
->kernel_interface
->get_interface(hydra
->kernel_interface
,
203 DBG2(DBG_CFG
, "left is other host, swapping ends");
204 tmp_end
= msg
->add_conn
.me
;
205 msg
->add_conn
.me
= msg
->add_conn
.other
;
206 msg
->add_conn
.other
= tmp_end
;
212 host
= host_create_from_dns(msg
->add_conn
.me
.address
, 0, 0);
215 if (!hydra
->kernel_interface
->get_interface(
216 hydra
->kernel_interface
, host
, NULL
))
218 DBG1(DBG_CFG
, "left nor right host is our side, "
219 "assuming left=local");
225 ikeport
= msg
->add_conn
.me
.ikeport
;
226 ikeport
= (ikeport
== IKEV2_UDP_PORT
) ?
227 charon
->socket
->get_port(charon
->socket
, FALSE
) : ikeport
;
228 ike_cfg
= ike_cfg_create(msg
->add_conn
.other
.sendcert
!= CERT_NEVER_SEND
,
229 msg
->add_conn
.force_encap
,
230 msg
->add_conn
.me
.address
,
231 msg
->add_conn
.me
.allow_any
,
233 msg
->add_conn
.other
.address
,
234 msg
->add_conn
.other
.allow_any
,
235 msg
->add_conn
.other
.ikeport
);
236 add_proposals(this, msg
->add_conn
.algorithms
.ike
, ike_cfg
, NULL
);
241 * Add CRL constraint to config
243 static void build_crl_policy(auth_cfg_t
*cfg
, bool local
, int policy
)
245 /* CRL/OCSP policy, for remote config only */
251 /* if yes, we require a GOOD validation */
252 cfg
->add(cfg
, AUTH_RULE_CRL_VALIDATION
, VALIDATION_GOOD
);
254 case CRL_STRICT_IFURI
:
255 /* for ifuri, a SKIPPED validation is sufficient */
256 cfg
->add(cfg
, AUTH_RULE_CRL_VALIDATION
, VALIDATION_SKIPPED
);
265 * Parse public key / signature strength constraints
267 static void parse_pubkey_constraints(char *auth
, auth_cfg_t
*cfg
)
269 enumerator_t
*enumerator
;
270 bool rsa
= FALSE
, ecdsa
= FALSE
, rsa_len
= FALSE
, ecdsa_len
= FALSE
;
274 enumerator
= enumerator_create_token(auth
, "-", "");
275 while (enumerator
->enumerate(enumerator
, &token
))
281 signature_scheme_t scheme
;
284 { "md5", SIGN_RSA_EMSA_PKCS1_MD5
, KEY_RSA
, },
285 { "sha1", SIGN_RSA_EMSA_PKCS1_SHA1
, KEY_RSA
, },
286 { "sha224", SIGN_RSA_EMSA_PKCS1_SHA224
, KEY_RSA
, },
287 { "sha256", SIGN_RSA_EMSA_PKCS1_SHA256
, KEY_RSA
, },
288 { "sha384", SIGN_RSA_EMSA_PKCS1_SHA384
, KEY_RSA
, },
289 { "sha512", SIGN_RSA_EMSA_PKCS1_SHA512
, KEY_RSA
, },
290 { "sha1", SIGN_ECDSA_WITH_SHA1_DER
, KEY_ECDSA
, },
291 { "sha256", SIGN_ECDSA_WITH_SHA256_DER
, KEY_ECDSA
, },
292 { "sha384", SIGN_ECDSA_WITH_SHA384_DER
, KEY_ECDSA
, },
293 { "sha512", SIGN_ECDSA_WITH_SHA512_DER
, KEY_ECDSA
, },
294 { "sha256", SIGN_ECDSA_256
, KEY_ECDSA
, },
295 { "sha384", SIGN_ECDSA_384
, KEY_ECDSA
, },
296 { "sha512", SIGN_ECDSA_521
, KEY_ECDSA
, },
299 if (rsa_len
|| ecdsa_len
)
300 { /* expecting a key strength token */
301 strength
= atoi(token
);
306 cfg
->add(cfg
, AUTH_RULE_RSA_STRENGTH
, (uintptr_t)strength
);
310 cfg
->add(cfg
, AUTH_RULE_ECDSA_STRENGTH
, (uintptr_t)strength
);
313 rsa_len
= ecdsa_len
= FALSE
;
319 if (streq(token
, "rsa"))
321 rsa
= rsa_len
= TRUE
;
324 if (streq(token
, "ecdsa"))
326 ecdsa
= ecdsa_len
= TRUE
;
329 if (streq(token
, "pubkey"))
334 for (i
= 0; i
< countof(schemes
); i
++)
336 if (streq(schemes
[i
].name
, token
))
338 /* for each matching string, allow the scheme, if:
339 * - it is an RSA scheme, and we enforced RSA
340 * - it is an ECDSA scheme, and we enforced ECDSA
341 * - it is not a key type specific scheme
343 if ((rsa
&& schemes
[i
].key
== KEY_RSA
) ||
344 (ecdsa
&& schemes
[i
].key
== KEY_ECDSA
) ||
347 cfg
->add(cfg
, AUTH_RULE_SIGNATURE_SCHEME
,
348 (uintptr_t)schemes
[i
].scheme
);
355 DBG1(DBG_CFG
, "ignoring invalid auth token: '%s'", token
);
358 enumerator
->destroy(enumerator
);
362 * build authentication config
364 static auth_cfg_t
*build_auth_cfg(private_stroke_config_t
*this,
365 stroke_msg_t
*msg
, bool local
, bool primary
)
367 identification_t
*identity
;
368 certificate_t
*certificate
;
369 char *auth
, *id
, *pubkey
, *cert
, *ca
, *groups
;
370 stroke_end_t
*end
, *other_end
;
377 end
= &msg
->add_conn
.me
;
378 other_end
= &msg
->add_conn
.other
;
382 end
= &msg
->add_conn
.other
;
383 other_end
= &msg
->add_conn
.me
;
390 { /* leftid/rightid fallback to address */
395 if (ca
&& streq(ca
, "%same"))
405 { /* leftid2 falls back to leftid */
410 if (ca
&& streq(ca
, "%same"))
415 if (id
&& *id
== '%' && !streq(id
, "%any"))
416 { /* has only an effect on rightid/2 */
428 { /* no second authentication round, fine. But load certificates
429 * for other purposes (EAP-TLS) */
432 certificate
= this->cred
->load_peer(this->cred
, cert
);
435 certificate
->destroy(certificate
);
442 cfg
= auth_cfg_create();
444 /* add identity and peer certifcate */
445 identity
= identification_create_from_string(id
);
448 certificate
= this->cred
->load_peer(this->cred
, cert
);
453 this->ca
->check_for_hash_and_url(this->ca
, certificate
);
455 cfg
->add(cfg
, AUTH_RULE_SUBJECT_CERT
, certificate
);
456 if (identity
->get_type(identity
) == ID_ANY
||
457 !certificate
->has_subject(certificate
, identity
))
459 DBG1(DBG_CFG
, " id '%Y' not confirmed by certificate, "
460 "defaulting to '%Y'", identity
,
461 certificate
->get_subject(certificate
));
462 identity
->destroy(identity
);
463 identity
= certificate
->get_subject(certificate
);
464 identity
= identity
->clone(identity
);
468 if (identity
->get_type(identity
) != ID_ANY
)
470 cfg
->add(cfg
, AUTH_RULE_IDENTITY
, identity
);
473 cfg
->add(cfg
, AUTH_RULE_IDENTITY_LOOSE
, TRUE
);
478 identity
->destroy(identity
);
481 /* add raw RSA public key */
482 pubkey
= end
->rsakey
;
483 if (pubkey
&& !streq(pubkey
, "") && !streq(pubkey
, "%cert"))
485 certificate
= this->cred
->load_pubkey(this->cred
, KEY_RSA
, pubkey
,
489 cfg
->add(cfg
, AUTH_RULE_SUBJECT_CERT
, certificate
);
496 identity
= identification_create_from_string(ca
);
497 certificate
= lib
->credmgr
->get_cert(lib
->credmgr
, CERT_X509
,
498 KEY_ANY
, identity
, TRUE
);
499 identity
->destroy(identity
);
502 cfg
->add(cfg
, AUTH_RULE_CA_CERT
, certificate
);
506 DBG1(DBG_CFG
, "CA certificate \"%s\" not found, discarding CA "
512 groups
= primary ? end
->groups
: end
->groups2
;
515 enumerator_t
*enumerator
;
518 enumerator
= enumerator_create_token(groups
, ",", " ");
519 while (enumerator
->enumerate(enumerator
, &group
))
521 cfg
->add(cfg
, AUTH_RULE_GROUP
,
522 identification_create_from_string(group
));
524 enumerator
->destroy(enumerator
);
527 /* certificatePolicies */
528 if (end
->cert_policy
)
530 enumerator_t
*enumerator
;
533 enumerator
= enumerator_create_token(end
->cert_policy
, ",", " ");
534 while (enumerator
->enumerate(enumerator
, &policy
))
536 cfg
->add(cfg
, AUTH_RULE_CERT_POLICY
, strdup(policy
));
538 enumerator
->destroy(enumerator
);
541 /* authentication metod (class, actually) */
542 if (strneq(auth
, "pubkey", strlen("pubkey")) ||
543 strneq(auth
, "rsa", strlen("rsa")) ||
544 strneq(auth
, "ecdsa", strlen("ecdsa")))
546 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
547 build_crl_policy(cfg
, local
, msg
->add_conn
.crl_policy
);
549 parse_pubkey_constraints(auth
, cfg
);
551 else if (streq(auth
, "psk") || streq(auth
, "secret"))
553 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PSK
);
555 else if (strneq(auth
, "xauth", 5))
559 pos
= strchr(auth
, '-');
562 cfg
->add(cfg
, AUTH_RULE_XAUTH_BACKEND
, strdup(++pos
));
564 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_XAUTH
);
565 if (msg
->add_conn
.xauth_identity
)
567 cfg
->add(cfg
, AUTH_RULE_XAUTH_IDENTITY
,
568 identification_create_from_string(msg
->add_conn
.xauth_identity
));
571 else if (strneq(auth
, "eap", 3))
573 eap_vendor_type_t
*type
;
575 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_EAP
);
577 type
= eap_vendor_type_from_string(auth
);
580 cfg
->add(cfg
, AUTH_RULE_EAP_TYPE
, type
->type
);
583 cfg
->add(cfg
, AUTH_RULE_EAP_VENDOR
, type
->vendor
);
588 if (msg
->add_conn
.eap_identity
)
590 if (streq(msg
->add_conn
.eap_identity
, "%identity"))
592 identity
= identification_create_from_encoding(ID_ANY
,
597 identity
= identification_create_from_string(
598 msg
->add_conn
.eap_identity
);
600 cfg
->add(cfg
, AUTH_RULE_EAP_IDENTITY
, identity
);
602 if (msg
->add_conn
.aaa_identity
)
604 cfg
->add(cfg
, AUTH_RULE_AAA_IDENTITY
,
605 identification_create_from_string(msg
->add_conn
.aaa_identity
));
610 if (!streq(auth
, "any"))
612 DBG1(DBG_CFG
, "authentication method %s unknown, fallback to any",
615 build_crl_policy(cfg
, local
, msg
->add_conn
.crl_policy
);
621 * build a peer_cfg from a stroke msg
623 static peer_cfg_t
*build_peer_cfg(private_stroke_config_t
*this,
624 stroke_msg_t
*msg
, ike_cfg_t
*ike_cfg
)
626 identification_t
*peer_id
= NULL
;
627 peer_cfg_t
*mediated_by
= NULL
;
628 unique_policy_t unique
;
629 u_int32_t rekey
= 0, reauth
= 0, over
, jitter
;
630 peer_cfg_t
*peer_cfg
;
631 auth_cfg_t
*auth_cfg
;
634 if (msg
->add_conn
.ikeme
.mediation
&& msg
->add_conn
.ikeme
.mediated_by
)
636 DBG1(DBG_CFG
, "a mediation connection cannot be a mediated connection "
637 "at the same time, aborting");
641 if (msg
->add_conn
.ikeme
.mediation
)
643 /* force unique connections for mediation connections */
644 msg
->add_conn
.unique
= 1;
647 if (msg
->add_conn
.ikeme
.mediated_by
)
649 mediated_by
= charon
->backends
->get_peer_cfg_by_name(charon
->backends
,
650 msg
->add_conn
.ikeme
.mediated_by
);
653 DBG1(DBG_CFG
, "mediation connection '%s' not found, aborting",
654 msg
->add_conn
.ikeme
.mediated_by
);
657 if (!mediated_by
->is_mediation(mediated_by
))
659 DBG1(DBG_CFG
, "connection '%s' as referred to by '%s' is "
660 "no mediation connection, aborting",
661 msg
->add_conn
.ikeme
.mediated_by
, msg
->add_conn
.name
);
662 mediated_by
->destroy(mediated_by
);
665 if (msg
->add_conn
.ikeme
.peerid
)
667 peer_id
= identification_create_from_string(msg
->add_conn
.ikeme
.peerid
);
669 else if (msg
->add_conn
.other
.id
)
671 peer_id
= identification_create_from_string(msg
->add_conn
.other
.id
);
676 jitter
= msg
->add_conn
.rekey
.margin
* msg
->add_conn
.rekey
.fuzz
/ 100;
677 over
= msg
->add_conn
.rekey
.margin
;
678 if (msg
->add_conn
.rekey
.reauth
)
680 reauth
= msg
->add_conn
.rekey
.ike_lifetime
- over
;
684 rekey
= msg
->add_conn
.rekey
.ike_lifetime
- over
;
686 switch (msg
->add_conn
.unique
)
689 case 2: /* replace */
690 unique
= UNIQUE_REPLACE
;
693 unique
= UNIQUE_KEEP
;
696 unique
= UNIQUE_NEVER
;
702 if (msg
->add_conn
.dpd
.action
== 0)
703 { /* dpdaction=none disables DPD */
704 msg
->add_conn
.dpd
.delay
= 0;
707 /* other.sourceip is managed in stroke_attributes. If it is set, we define
708 * the pool name as the connection name, which the attribute provider
709 * uses to serve pool addresses. */
710 peer_cfg
= peer_cfg_create(msg
->add_conn
.name
,
711 msg
->add_conn
.version
, ike_cfg
,
712 msg
->add_conn
.me
.sendcert
, unique
,
713 msg
->add_conn
.rekey
.tries
, rekey
, reauth
, jitter
, over
,
714 msg
->add_conn
.mobike
, msg
->add_conn
.aggressive
,
715 msg
->add_conn
.dpd
.delay
, msg
->add_conn
.dpd
.timeout
,
716 msg
->add_conn
.ikeme
.mediation
, mediated_by
, peer_id
);
718 if (msg
->add_conn
.other
.sourceip
)
720 enumerator_t
*enumerator
;
723 enumerator
= enumerator_create_token(msg
->add_conn
.other
.sourceip
,
725 while (enumerator
->enumerate(enumerator
, &token
))
727 if (streq(token
, "%modeconfig") || streq(token
, "%modecfg") ||
728 streq(token
, "%config") || streq(token
, "%cfg") ||
729 streq(token
, "%config4") || streq(token
, "%config6"))
731 /* empty pool, uses connection name */
732 this->attributes
->add_pool(this->attributes
,
733 mem_pool_create(msg
->add_conn
.name
, NULL
, 0));
734 peer_cfg
->add_pool(peer_cfg
, msg
->add_conn
.name
);
736 else if (*token
== '%')
738 /* external named pool */
739 peer_cfg
->add_pool(peer_cfg
, token
+ 1);
743 /* in-memory pool, named using CIDR notation */
747 base
= host_create_from_subnet(token
, &bits
);
750 this->attributes
->add_pool(this->attributes
,
751 mem_pool_create(token
, base
, bits
));
752 peer_cfg
->add_pool(peer_cfg
, token
);
757 DBG1(DBG_CFG
, "IP pool %s invalid, ignored", token
);
761 enumerator
->destroy(enumerator
);
764 if (msg
->add_conn
.me
.sourceip
)
766 enumerator_t
*enumerator
;
769 enumerator
= enumerator_create_token(msg
->add_conn
.me
.sourceip
, ",", " ");
770 while (enumerator
->enumerate(enumerator
, &token
))
774 if (streq(token
, "%modeconfig") || streq(token
, "%modecfg") ||
775 streq(token
, "%config") || streq(token
, "%cfg"))
776 { /* try to deduce an address family */
777 if (msg
->add_conn
.me
.subnets
)
778 { /* use the same family as in local subnet, if any */
779 if (strchr(msg
->add_conn
.me
.subnets
, '.'))
781 vip
= host_create_any(AF_INET
);
785 vip
= host_create_any(AF_INET6
);
788 else if (msg
->add_conn
.other
.subnets
)
789 { /* use the same family as in remote subnet, if any */
790 if (strchr(msg
->add_conn
.other
.subnets
, '.'))
792 vip
= host_create_any(AF_INET
);
796 vip
= host_create_any(AF_INET6
);
801 if (strchr(ike_cfg
->get_my_addr(ike_cfg
, NULL
), ':'))
803 vip
= host_create_any(AF_INET6
);
807 vip
= host_create_any(AF_INET
);
811 else if (streq(token
, "%config4"))
813 vip
= host_create_any(AF_INET
);
815 else if (streq(token
, "%config6"))
817 vip
= host_create_any(AF_INET6
);
821 vip
= host_create_from_string(token
, 0);
824 DBG1(DBG_CFG
, "ignored invalid subnet token: %s", token
);
830 peer_cfg
->add_virtual_ip(peer_cfg
, vip
);
833 enumerator
->destroy(enumerator
);
836 /* build leftauth= */
837 auth_cfg
= build_auth_cfg(this, msg
, TRUE
, TRUE
);
840 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, TRUE
);
843 { /* we require at least one config on our side */
844 peer_cfg
->destroy(peer_cfg
);
847 /* build leftauth2= */
848 auth_cfg
= build_auth_cfg(this, msg
, TRUE
, FALSE
);
851 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, TRUE
);
853 /* build rightauth= */
854 auth_cfg
= build_auth_cfg(this, msg
, FALSE
, TRUE
);
857 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, FALSE
);
859 /* build rightauth2= */
860 auth_cfg
= build_auth_cfg(this, msg
, FALSE
, FALSE
);
863 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, FALSE
);
869 * build a traffic selector from a stroke_end
871 static void add_ts(private_stroke_config_t
*this,
872 stroke_end_t
*end
, child_cfg_t
*child_cfg
, bool local
)
874 traffic_selector_t
*ts
;
878 ts
= traffic_selector_create_dynamic(end
->protocol
,
879 end
->port ? end
->port
: 0, end
->port ? end
->port
: 65535);
880 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
888 net
= host_create_from_string(end
->address
, 0);
891 ts
= traffic_selector_create_from_subnet(net
, 0, end
->protocol
,
893 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
898 char *del
, *start
, *bits
;
900 start
= end
->subnets
;
905 del
= strchr(start
, ',');
910 bits
= strchr(start
, '/');
914 intbits
= atoi(bits
+ 1);
917 net
= host_create_from_string(start
, 0);
920 ts
= traffic_selector_create_from_subnet(net
, intbits
,
921 end
->protocol
, end
->port
);
922 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
926 DBG1(DBG_CFG
, "invalid subnet: %s, skipped", start
);
936 * map starter magic values to our action type
938 static action_t
map_action(int starter_action
)
940 switch (starter_action
)
944 case 3: /* =restart */
945 return ACTION_RESTART
;
952 * build a child config from the stroke message
954 static child_cfg_t
*build_child_cfg(private_stroke_config_t
*this,
957 child_cfg_t
*child_cfg
;
958 lifetime_cfg_t lifetime
= {
960 .life
= msg
->add_conn
.rekey
.ipsec_lifetime
,
961 .rekey
= msg
->add_conn
.rekey
.ipsec_lifetime
- msg
->add_conn
.rekey
.margin
,
962 .jitter
= msg
->add_conn
.rekey
.margin
* msg
->add_conn
.rekey
.fuzz
/ 100
965 .life
= msg
->add_conn
.rekey
.life_bytes
,
966 .rekey
= msg
->add_conn
.rekey
.life_bytes
- msg
->add_conn
.rekey
.margin_bytes
,
967 .jitter
= msg
->add_conn
.rekey
.margin_bytes
* msg
->add_conn
.rekey
.fuzz
/ 100
970 .life
= msg
->add_conn
.rekey
.life_packets
,
971 .rekey
= msg
->add_conn
.rekey
.life_packets
- msg
->add_conn
.rekey
.margin_packets
,
972 .jitter
= msg
->add_conn
.rekey
.margin_packets
* msg
->add_conn
.rekey
.fuzz
/ 100
976 .value
= msg
->add_conn
.mark_in
.value
,
977 .mask
= msg
->add_conn
.mark_in
.mask
980 .value
= msg
->add_conn
.mark_out
.value
,
981 .mask
= msg
->add_conn
.mark_out
.mask
984 child_cfg
= child_cfg_create(
985 msg
->add_conn
.name
, &lifetime
, msg
->add_conn
.me
.updown
,
986 msg
->add_conn
.me
.hostaccess
, msg
->add_conn
.mode
, ACTION_NONE
,
987 map_action(msg
->add_conn
.dpd
.action
),
988 map_action(msg
->add_conn
.close_action
), msg
->add_conn
.ipcomp
,
989 msg
->add_conn
.inactivity
, msg
->add_conn
.reqid
,
990 &mark_in
, &mark_out
, msg
->add_conn
.tfc
);
991 child_cfg
->set_mipv6_options(child_cfg
, msg
->add_conn
.proxy_mode
,
992 msg
->add_conn
.install_policy
);
993 add_ts(this, &msg
->add_conn
.me
, child_cfg
, TRUE
);
994 add_ts(this, &msg
->add_conn
.other
, child_cfg
, FALSE
);
996 add_proposals(this, msg
->add_conn
.algorithms
.esp
, NULL
, child_cfg
);
1001 METHOD(stroke_config_t
, add
, void,
1002 private_stroke_config_t
*this, stroke_msg_t
*msg
)
1004 ike_cfg_t
*ike_cfg
, *existing_ike
;
1005 peer_cfg_t
*peer_cfg
, *existing
;
1006 child_cfg_t
*child_cfg
;
1007 enumerator_t
*enumerator
;
1008 bool use_existing
= FALSE
;
1010 ike_cfg
= build_ike_cfg(this, msg
);
1015 peer_cfg
= build_peer_cfg(this, msg
, ike_cfg
);
1018 ike_cfg
->destroy(ike_cfg
);
1022 enumerator
= create_peer_cfg_enumerator(this, NULL
, NULL
);
1023 while (enumerator
->enumerate(enumerator
, &existing
))
1025 existing_ike
= existing
->get_ike_cfg(existing
);
1026 if (existing
->equals(existing
, peer_cfg
) &&
1027 existing_ike
->equals(existing_ike
, peer_cfg
->get_ike_cfg(peer_cfg
)))
1029 use_existing
= TRUE
;
1030 peer_cfg
->destroy(peer_cfg
);
1031 peer_cfg
= existing
;
1032 peer_cfg
->get_ref(peer_cfg
);
1033 DBG1(DBG_CFG
, "added child to existing configuration '%s'",
1034 peer_cfg
->get_name(peer_cfg
));
1038 enumerator
->destroy(enumerator
);
1040 child_cfg
= build_child_cfg(this, msg
);
1043 peer_cfg
->destroy(peer_cfg
);
1046 peer_cfg
->add_child_cfg(peer_cfg
, child_cfg
);
1050 peer_cfg
->destroy(peer_cfg
);
1054 /* add config to backend */
1055 DBG1(DBG_CFG
, "added configuration '%s'", msg
->add_conn
.name
);
1056 this->mutex
->lock(this->mutex
);
1057 this->list
->insert_last(this->list
, peer_cfg
);
1058 this->mutex
->unlock(this->mutex
);
1062 METHOD(stroke_config_t
, del
, void,
1063 private_stroke_config_t
*this, stroke_msg_t
*msg
)
1065 enumerator_t
*enumerator
, *children
;
1068 bool deleted
= FALSE
;
1070 this->mutex
->lock(this->mutex
);
1071 enumerator
= this->list
->create_enumerator(this->list
);
1072 while (enumerator
->enumerate(enumerator
, (void**)&peer
))
1076 /* remove any child with such a name */
1077 children
= peer
->create_child_cfg_enumerator(peer
);
1078 while (children
->enumerate(children
, &child
))
1080 if (streq(child
->get_name(child
), msg
->del_conn
.name
))
1082 peer
->remove_child_cfg(peer
, children
);
1083 child
->destroy(child
);
1091 children
->destroy(children
);
1093 /* if peer config matches, or has no children anymore, remove it */
1094 if (!keep
|| streq(peer
->get_name(peer
), msg
->del_conn
.name
))
1096 this->list
->remove_at(this->list
, enumerator
);
1097 peer
->destroy(peer
);
1101 enumerator
->destroy(enumerator
);
1102 this->mutex
->unlock(this->mutex
);
1106 DBG1(DBG_CFG
, "deleted connection '%s'", msg
->del_conn
.name
);
1110 DBG1(DBG_CFG
, "connection '%s' not found", msg
->del_conn
.name
);
1114 METHOD(stroke_config_t
, set_user_credentials
, void,
1115 private_stroke_config_t
*this, stroke_msg_t
*msg
, FILE *prompt
)
1117 enumerator_t
*enumerator
, *children
, *remote_auth
;
1118 peer_cfg_t
*peer
, *found
= NULL
;
1119 auth_cfg_t
*auth_cfg
, *remote_cfg
;
1120 auth_class_t auth_class
;
1122 identification_t
*id
, *identity
, *gw
= NULL
;
1123 shared_key_type_t type
= SHARED_ANY
;
1124 chunk_t password
= chunk_empty
;
1126 this->mutex
->lock(this->mutex
);
1127 enumerator
= this->list
->create_enumerator(this->list
);
1128 while (enumerator
->enumerate(enumerator
, (void**)&peer
))
1129 { /* find the peer (or child) config with the given name */
1130 if (streq(peer
->get_name(peer
), msg
->user_creds
.name
))
1136 children
= peer
->create_child_cfg_enumerator(peer
);
1137 while (children
->enumerate(children
, &child
))
1139 if (streq(child
->get_name(child
), msg
->user_creds
.name
))
1145 children
->destroy(children
);
1153 enumerator
->destroy(enumerator
);
1157 DBG1(DBG_CFG
, " no config named '%s'", msg
->user_creds
.name
);
1158 fprintf(prompt
, "no config named '%s'\n", msg
->user_creds
.name
);
1159 this->mutex
->unlock(this->mutex
);
1163 id
= identification_create_from_string(msg
->user_creds
.username
);
1164 if (strlen(msg
->user_creds
.username
) == 0 ||
1165 !id
|| id
->get_type(id
) == ID_ANY
)
1167 DBG1(DBG_CFG
, " invalid username '%s'", msg
->user_creds
.username
);
1168 fprintf(prompt
, "invalid username '%s'\n", msg
->user_creds
.username
);
1169 this->mutex
->unlock(this->mutex
);
1174 /* replace/set the username in the first EAP/XAuth auth_cfg, also look for
1175 * a suitable remote ID.
1176 * note that adding the identity here is not fully thread-safe as the
1177 * peer_cfg and in turn the auth_cfg could be in use. for the default use
1178 * case (setting user credentials before upping the connection) this will
1179 * not be a problem, though. */
1180 enumerator
= found
->create_auth_cfg_enumerator(found
, TRUE
);
1181 remote_auth
= found
->create_auth_cfg_enumerator(found
, FALSE
);
1182 while (enumerator
->enumerate(enumerator
, (void**)&auth_cfg
))
1184 if (remote_auth
->enumerate(remote_auth
, (void**)&remote_cfg
))
1185 { /* fall back on rightid, in case aaa_identity is not specified */
1186 identity
= remote_cfg
->get(remote_cfg
, AUTH_RULE_IDENTITY
);
1187 if (identity
&& identity
->get_type(identity
) != ID_ANY
)
1193 auth_class
= (uintptr_t)auth_cfg
->get(auth_cfg
, AUTH_RULE_AUTH_CLASS
);
1194 if (auth_class
== AUTH_CLASS_EAP
|| auth_class
== AUTH_CLASS_XAUTH
)
1196 if (auth_class
== AUTH_CLASS_EAP
)
1198 auth_cfg
->add(auth_cfg
, AUTH_RULE_EAP_IDENTITY
, id
->clone(id
));
1199 /* if aaa_identity is specified use that as remote ID */
1200 identity
= auth_cfg
->get(auth_cfg
, AUTH_RULE_AAA_IDENTITY
);
1201 if (identity
&& identity
->get_type(identity
) != ID_ANY
)
1205 DBG1(DBG_CFG
, " configured EAP-Identity %Y", id
);
1209 auth_cfg
->add(auth_cfg
, AUTH_RULE_XAUTH_IDENTITY
,
1211 DBG1(DBG_CFG
, " configured XAuth username %Y", id
);
1217 enumerator
->destroy(enumerator
);
1218 remote_auth
->destroy(remote_auth
);
1219 /* clone the gw ID before unlocking the mutex */
1224 this->mutex
->unlock(this->mutex
);
1226 if (type
== SHARED_ANY
)
1228 DBG1(DBG_CFG
, " config '%s' unsuitable for user credentials",
1229 msg
->user_creds
.name
);
1230 fprintf(prompt
, "config '%s' unsuitable for user credentials\n",
1231 msg
->user_creds
.name
);
1237 if (msg
->user_creds
.password
)
1241 pass
= msg
->user_creds
.password
;
1242 password
= chunk_clone(chunk_create(pass
, strlen(pass
)));
1243 memwipe(pass
, strlen(pass
));
1246 { /* prompt the user for the password */
1249 fprintf(prompt
, "Password:\n");
1250 if (fgets(buf
, sizeof(buf
), prompt
))
1252 password
= chunk_clone(chunk_create(buf
, strlen(buf
)));
1253 if (password
.len
> 0)
1254 { /* trim trailing \n */
1257 memwipe(buf
, sizeof(buf
));
1263 shared_key_t
*shared
;
1264 linked_list_t
*owners
;
1266 shared
= shared_key_create(type
, password
);
1268 owners
= linked_list_create();
1269 owners
->insert_last(owners
, id
->clone(id
));
1270 if (gw
&& gw
->get_type(gw
) != ID_ANY
)
1272 owners
->insert_last(owners
, gw
->clone(gw
));
1273 DBG1(DBG_CFG
, " added %N secret for %Y %Y", shared_key_type_names
,
1278 DBG1(DBG_CFG
, " added %N secret for %Y", shared_key_type_names
,
1281 this->cred
->add_shared(this->cred
, shared
, owners
);
1282 DBG4(DBG_CFG
, " secret: %#B", &password
);
1285 { /* in case a user answers the password prompt by just pressing enter */
1286 chunk_clear(&password
);
1292 METHOD(stroke_config_t
, destroy
, void,
1293 private_stroke_config_t
*this)
1295 this->list
->destroy_offset(this->list
, offsetof(peer_cfg_t
, destroy
));
1296 this->mutex
->destroy(this->mutex
);
1303 stroke_config_t
*stroke_config_create(stroke_ca_t
*ca
, stroke_cred_t
*cred
,
1304 stroke_attribute_t
*attributes
)
1306 private_stroke_config_t
*this;
1311 .create_peer_cfg_enumerator
= _create_peer_cfg_enumerator
,
1312 .create_ike_cfg_enumerator
= _create_ike_cfg_enumerator
,
1313 .get_peer_cfg_by_name
= _get_peer_cfg_by_name
,
1317 .set_user_credentials
= _set_user_credentials
,
1318 .destroy
= _destroy
,
1320 .list
= linked_list_create(),
1321 .mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
),
1324 .attributes
= attributes
,
1327 return &this->public;