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
;
198 host
= host_create_from_dns(msg
->add_conn
.other
.address
, 0, 0);
201 interface
= hydra
->kernel_interface
->get_interface(
202 hydra
->kernel_interface
, host
);
206 DBG2(DBG_CFG
, "left is other host, swapping ends");
207 tmp_end
= msg
->add_conn
.me
;
208 msg
->add_conn
.me
= msg
->add_conn
.other
;
209 msg
->add_conn
.other
= tmp_end
;
214 host
= host_create_from_dns(msg
->add_conn
.me
.address
, 0, 0);
217 interface
= hydra
->kernel_interface
->get_interface(
218 hydra
->kernel_interface
, host
);
222 DBG1(DBG_CFG
, "left nor right host is our side, "
223 "assuming left=local");
233 ikeport
= msg
->add_conn
.me
.ikeport
;
234 ikeport
= (ikeport
== IKEV2_UDP_PORT
) ?
235 charon
->socket
->get_port(charon
->socket
, FALSE
) : ikeport
;
236 ike_cfg
= ike_cfg_create(msg
->add_conn
.other
.sendcert
!= CERT_NEVER_SEND
,
237 msg
->add_conn
.force_encap
,
238 msg
->add_conn
.me
.address
,
239 msg
->add_conn
.me
.allow_any
,
241 msg
->add_conn
.other
.address
,
242 msg
->add_conn
.other
.allow_any
,
243 msg
->add_conn
.other
.ikeport
);
244 add_proposals(this, msg
->add_conn
.algorithms
.ike
, ike_cfg
, NULL
);
249 * Add CRL constraint to config
251 static void build_crl_policy(auth_cfg_t
*cfg
, bool local
, int policy
)
253 /* CRL/OCSP policy, for remote config only */
259 /* if yes, we require a GOOD validation */
260 cfg
->add(cfg
, AUTH_RULE_CRL_VALIDATION
, VALIDATION_GOOD
);
262 case CRL_STRICT_IFURI
:
263 /* for ifuri, a SKIPPED validation is sufficient */
264 cfg
->add(cfg
, AUTH_RULE_CRL_VALIDATION
, VALIDATION_SKIPPED
);
273 * Parse public key / signature strength constraints
275 static void parse_pubkey_constraints(char *auth
, auth_cfg_t
*cfg
)
277 enumerator_t
*enumerator
;
278 bool rsa
= FALSE
, ecdsa
= FALSE
, rsa_len
= FALSE
, ecdsa_len
= FALSE
;
282 enumerator
= enumerator_create_token(auth
, "-", "");
283 while (enumerator
->enumerate(enumerator
, &token
))
289 signature_scheme_t scheme
;
292 { "md5", SIGN_RSA_EMSA_PKCS1_MD5
, KEY_RSA
, },
293 { "sha1", SIGN_RSA_EMSA_PKCS1_SHA1
, KEY_RSA
, },
294 { "sha224", SIGN_RSA_EMSA_PKCS1_SHA224
, KEY_RSA
, },
295 { "sha256", SIGN_RSA_EMSA_PKCS1_SHA256
, KEY_RSA
, },
296 { "sha384", SIGN_RSA_EMSA_PKCS1_SHA384
, KEY_RSA
, },
297 { "sha512", SIGN_RSA_EMSA_PKCS1_SHA512
, KEY_RSA
, },
298 { "sha1", SIGN_ECDSA_WITH_SHA1_DER
, KEY_ECDSA
, },
299 { "sha256", SIGN_ECDSA_WITH_SHA256_DER
, KEY_ECDSA
, },
300 { "sha384", SIGN_ECDSA_WITH_SHA384_DER
, KEY_ECDSA
, },
301 { "sha512", SIGN_ECDSA_WITH_SHA512_DER
, KEY_ECDSA
, },
302 { "sha256", SIGN_ECDSA_256
, KEY_ECDSA
, },
303 { "sha384", SIGN_ECDSA_384
, KEY_ECDSA
, },
304 { "sha512", SIGN_ECDSA_521
, KEY_ECDSA
, },
307 if (rsa_len
|| ecdsa_len
)
308 { /* expecting a key strength token */
309 strength
= atoi(token
);
314 cfg
->add(cfg
, AUTH_RULE_RSA_STRENGTH
, (uintptr_t)strength
);
318 cfg
->add(cfg
, AUTH_RULE_ECDSA_STRENGTH
, (uintptr_t)strength
);
321 rsa_len
= ecdsa_len
= FALSE
;
327 if (streq(token
, "rsa"))
329 rsa
= rsa_len
= TRUE
;
332 if (streq(token
, "ecdsa"))
334 ecdsa
= ecdsa_len
= TRUE
;
337 if (streq(token
, "pubkey"))
342 for (i
= 0; i
< countof(schemes
); i
++)
344 if (streq(schemes
[i
].name
, token
))
346 /* for each matching string, allow the scheme, if:
347 * - it is an RSA scheme, and we enforced RSA
348 * - it is an ECDSA scheme, and we enforced ECDSA
349 * - it is not a key type specific scheme
351 if ((rsa
&& schemes
[i
].key
== KEY_RSA
) ||
352 (ecdsa
&& schemes
[i
].key
== KEY_ECDSA
) ||
355 cfg
->add(cfg
, AUTH_RULE_SIGNATURE_SCHEME
,
356 (uintptr_t)schemes
[i
].scheme
);
363 DBG1(DBG_CFG
, "ignoring invalid auth token: '%s'", token
);
366 enumerator
->destroy(enumerator
);
370 * build authentication config
372 static auth_cfg_t
*build_auth_cfg(private_stroke_config_t
*this,
373 stroke_msg_t
*msg
, bool local
, bool primary
)
375 identification_t
*identity
;
376 certificate_t
*certificate
;
377 char *auth
, *id
, *pubkey
, *cert
, *ca
, *groups
;
378 stroke_end_t
*end
, *other_end
;
384 end
= &msg
->add_conn
.me
;
385 other_end
= &msg
->add_conn
.other
;
389 end
= &msg
->add_conn
.other
;
390 other_end
= &msg
->add_conn
.me
;
397 { /* leftid/rightid fallback to address */
402 if (ca
&& streq(ca
, "%same"))
412 { /* leftid2 falls back to leftid */
417 if (ca
&& streq(ca
, "%same"))
430 { /* no second authentication round, fine. But load certificates
431 * for other purposes (EAP-TLS) */
434 certificate
= this->cred
->load_peer(this->cred
, cert
);
437 certificate
->destroy(certificate
);
444 cfg
= auth_cfg_create();
446 /* add identity and peer certifcate */
447 identity
= identification_create_from_string(id
);
450 certificate
= this->cred
->load_peer(this->cred
, cert
);
455 this->ca
->check_for_hash_and_url(this->ca
, certificate
);
457 cfg
->add(cfg
, AUTH_RULE_SUBJECT_CERT
, certificate
);
458 if (identity
->get_type(identity
) == ID_ANY
||
459 !certificate
->has_subject(certificate
, identity
))
461 DBG1(DBG_CFG
, " id '%Y' not confirmed by certificate, "
462 "defaulting to '%Y'", identity
,
463 certificate
->get_subject(certificate
));
464 identity
->destroy(identity
);
465 identity
= certificate
->get_subject(certificate
);
466 identity
= identity
->clone(identity
);
470 if (identity
->get_type(identity
) != ID_ANY
)
472 cfg
->add(cfg
, AUTH_RULE_IDENTITY
, identity
);
476 identity
->destroy(identity
);
479 /* add raw RSA public key */
480 pubkey
= end
->rsakey
;
481 if (pubkey
&& !streq(pubkey
, "") && !streq(pubkey
, "%cert"))
483 certificate
= this->cred
->load_pubkey(this->cred
, KEY_RSA
, pubkey
,
487 cfg
->add(cfg
, AUTH_RULE_SUBJECT_CERT
, certificate
);
494 identity
= identification_create_from_string(ca
);
495 certificate
= lib
->credmgr
->get_cert(lib
->credmgr
, CERT_X509
,
496 KEY_ANY
, identity
, TRUE
);
497 identity
->destroy(identity
);
500 cfg
->add(cfg
, AUTH_RULE_CA_CERT
, certificate
);
504 DBG1(DBG_CFG
, "CA certificate \"%s\" not found, discarding CA "
510 groups
= primary ? end
->groups
: end
->groups2
;
513 enumerator_t
*enumerator
;
516 enumerator
= enumerator_create_token(groups
, ",", " ");
517 while (enumerator
->enumerate(enumerator
, &group
))
519 cfg
->add(cfg
, AUTH_RULE_GROUP
,
520 identification_create_from_string(group
));
522 enumerator
->destroy(enumerator
);
525 /* certificatePolicies */
526 if (end
->cert_policy
)
528 enumerator_t
*enumerator
;
531 enumerator
= enumerator_create_token(end
->cert_policy
, ",", " ");
532 while (enumerator
->enumerate(enumerator
, &policy
))
534 cfg
->add(cfg
, AUTH_RULE_CERT_POLICY
, strdup(policy
));
536 enumerator
->destroy(enumerator
);
539 /* authentication metod (class, actually) */
540 if (strneq(auth
, "pubkey", strlen("pubkey")) ||
541 strneq(auth
, "rsa", strlen("rsa")) ||
542 strneq(auth
, "ecdsa", strlen("ecdsa")))
544 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
545 build_crl_policy(cfg
, local
, msg
->add_conn
.crl_policy
);
547 parse_pubkey_constraints(auth
, cfg
);
549 else if (streq(auth
, "psk") || streq(auth
, "secret"))
551 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PSK
);
553 else if (strneq(auth
, "xauth", 5))
557 pos
= strchr(auth
, '-');
560 cfg
->add(cfg
, AUTH_RULE_XAUTH_BACKEND
, strdup(++pos
));
562 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_XAUTH
);
563 if (msg
->add_conn
.xauth_identity
)
565 cfg
->add(cfg
, AUTH_RULE_XAUTH_IDENTITY
,
566 identification_create_from_string(msg
->add_conn
.xauth_identity
));
569 else if (strneq(auth
, "eap", 3))
571 eap_vendor_type_t
*type
;
573 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_EAP
);
575 type
= eap_vendor_type_from_string(auth
);
578 cfg
->add(cfg
, AUTH_RULE_EAP_TYPE
, type
->type
);
581 cfg
->add(cfg
, AUTH_RULE_EAP_VENDOR
, type
->vendor
);
586 if (msg
->add_conn
.eap_identity
)
588 if (streq(msg
->add_conn
.eap_identity
, "%identity"))
590 identity
= identification_create_from_encoding(ID_ANY
,
595 identity
= identification_create_from_string(
596 msg
->add_conn
.eap_identity
);
598 cfg
->add(cfg
, AUTH_RULE_EAP_IDENTITY
, identity
);
600 if (msg
->add_conn
.aaa_identity
)
602 cfg
->add(cfg
, AUTH_RULE_AAA_IDENTITY
,
603 identification_create_from_string(msg
->add_conn
.aaa_identity
));
608 if (!streq(auth
, "any"))
610 DBG1(DBG_CFG
, "authentication method %s unknown, fallback to any",
613 build_crl_policy(cfg
, local
, msg
->add_conn
.crl_policy
);
619 * build a peer_cfg from a stroke msg
621 static peer_cfg_t
*build_peer_cfg(private_stroke_config_t
*this,
622 stroke_msg_t
*msg
, ike_cfg_t
*ike_cfg
)
624 identification_t
*peer_id
= NULL
;
625 peer_cfg_t
*mediated_by
= NULL
;
626 unique_policy_t unique
;
627 u_int32_t rekey
= 0, reauth
= 0, over
, jitter
;
628 peer_cfg_t
*peer_cfg
;
629 auth_cfg_t
*auth_cfg
;
632 if (msg
->add_conn
.ikeme
.mediation
&& msg
->add_conn
.ikeme
.mediated_by
)
634 DBG1(DBG_CFG
, "a mediation connection cannot be a mediated connection "
635 "at the same time, aborting");
639 if (msg
->add_conn
.ikeme
.mediation
)
641 /* force unique connections for mediation connections */
642 msg
->add_conn
.unique
= 1;
645 if (msg
->add_conn
.ikeme
.mediated_by
)
647 mediated_by
= charon
->backends
->get_peer_cfg_by_name(charon
->backends
,
648 msg
->add_conn
.ikeme
.mediated_by
);
651 DBG1(DBG_CFG
, "mediation connection '%s' not found, aborting",
652 msg
->add_conn
.ikeme
.mediated_by
);
655 if (!mediated_by
->is_mediation(mediated_by
))
657 DBG1(DBG_CFG
, "connection '%s' as referred to by '%s' is "
658 "no mediation connection, aborting",
659 msg
->add_conn
.ikeme
.mediated_by
, msg
->add_conn
.name
);
660 mediated_by
->destroy(mediated_by
);
663 if (msg
->add_conn
.ikeme
.peerid
)
665 peer_id
= identification_create_from_string(msg
->add_conn
.ikeme
.peerid
);
667 else if (msg
->add_conn
.other
.id
)
669 peer_id
= identification_create_from_string(msg
->add_conn
.other
.id
);
674 jitter
= msg
->add_conn
.rekey
.margin
* msg
->add_conn
.rekey
.fuzz
/ 100;
675 over
= msg
->add_conn
.rekey
.margin
;
676 if (msg
->add_conn
.rekey
.reauth
)
678 reauth
= msg
->add_conn
.rekey
.ike_lifetime
- over
;
682 rekey
= msg
->add_conn
.rekey
.ike_lifetime
- over
;
684 switch (msg
->add_conn
.unique
)
687 case 2: /* replace */
688 unique
= UNIQUE_REPLACE
;
691 unique
= UNIQUE_KEEP
;
694 unique
= UNIQUE_NEVER
;
700 if (msg
->add_conn
.dpd
.action
== 0)
701 { /* dpdaction=none disables DPD */
702 msg
->add_conn
.dpd
.delay
= 0;
705 /* other.sourceip is managed in stroke_attributes. If it is set, we define
706 * the pool name as the connection name, which the attribute provider
707 * uses to serve pool addresses. */
708 peer_cfg
= peer_cfg_create(msg
->add_conn
.name
,
709 msg
->add_conn
.version
, ike_cfg
,
710 msg
->add_conn
.me
.sendcert
, unique
,
711 msg
->add_conn
.rekey
.tries
, rekey
, reauth
, jitter
, over
,
712 msg
->add_conn
.mobike
, msg
->add_conn
.aggressive
,
713 msg
->add_conn
.dpd
.delay
, msg
->add_conn
.dpd
.timeout
,
714 msg
->add_conn
.ikeme
.mediation
, mediated_by
, peer_id
);
716 if (msg
->add_conn
.other
.sourceip
)
718 enumerator_t
*enumerator
;
721 enumerator
= enumerator_create_token(msg
->add_conn
.other
.sourceip
,
723 while (enumerator
->enumerate(enumerator
, &token
))
725 if (streq(token
, "%modeconfig") || streq(token
, "%modecfg") ||
726 streq(token
, "%config") || streq(token
, "%cfg") ||
727 streq(token
, "%config4") || streq(token
, "%config6"))
729 /* empty pool, uses connection name */
730 this->attributes
->add_pool(this->attributes
,
731 mem_pool_create(msg
->add_conn
.name
, NULL
, 0));
732 peer_cfg
->add_pool(peer_cfg
, msg
->add_conn
.name
);
734 else if (*token
== '%')
736 /* external named pool */
737 peer_cfg
->add_pool(peer_cfg
, token
+ 1);
741 /* in-memory pool, named using CIDR notation */
745 base
= host_create_from_subnet(token
, &bits
);
748 this->attributes
->add_pool(this->attributes
,
749 mem_pool_create(token
, base
, bits
));
750 peer_cfg
->add_pool(peer_cfg
, token
);
755 DBG1(DBG_CFG
, "IP pool %s invalid, ignored", token
);
759 enumerator
->destroy(enumerator
);
762 if (msg
->add_conn
.me
.sourceip
)
764 enumerator_t
*enumerator
;
767 enumerator
= enumerator_create_token(msg
->add_conn
.me
.sourceip
, ",", " ");
768 while (enumerator
->enumerate(enumerator
, &token
))
772 if (streq(token
, "%modeconfig") || streq(token
, "%modecfg") ||
773 streq(token
, "%config") || streq(token
, "%cfg"))
774 { /* try to deduce an address family */
775 if (msg
->add_conn
.me
.subnets
)
776 { /* use the same family as in local subnet, if any */
777 if (strchr(msg
->add_conn
.me
.subnets
, '.'))
779 vip
= host_create_any(AF_INET
);
783 vip
= host_create_any(AF_INET6
);
786 else if (msg
->add_conn
.other
.subnets
)
787 { /* use the same family as in remote subnet, if any */
788 if (strchr(msg
->add_conn
.other
.subnets
, '.'))
790 vip
= host_create_any(AF_INET
);
794 vip
= host_create_any(AF_INET6
);
799 if (strchr(ike_cfg
->get_my_addr(ike_cfg
, NULL
), ':'))
801 vip
= host_create_any(AF_INET6
);
805 vip
= host_create_any(AF_INET
);
809 else if (streq(token
, "%config4"))
811 vip
= host_create_any(AF_INET
);
813 else if (streq(token
, "%config6"))
815 vip
= host_create_any(AF_INET6
);
819 vip
= host_create_from_string(token
, 0);
822 DBG1(DBG_CFG
, "ignored invalid subnet token: %s", token
);
828 peer_cfg
->add_virtual_ip(peer_cfg
, vip
);
831 enumerator
->destroy(enumerator
);
834 /* build leftauth= */
835 auth_cfg
= build_auth_cfg(this, msg
, TRUE
, TRUE
);
838 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, TRUE
);
841 { /* we require at least one config on our side */
842 peer_cfg
->destroy(peer_cfg
);
845 /* build leftauth2= */
846 auth_cfg
= build_auth_cfg(this, msg
, TRUE
, FALSE
);
849 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, TRUE
);
851 /* build rightauth= */
852 auth_cfg
= build_auth_cfg(this, msg
, FALSE
, TRUE
);
855 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, FALSE
);
857 /* build rightauth2= */
858 auth_cfg
= build_auth_cfg(this, msg
, FALSE
, FALSE
);
861 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, FALSE
);
867 * build a traffic selector from a stroke_end
869 static void add_ts(private_stroke_config_t
*this,
870 stroke_end_t
*end
, child_cfg_t
*child_cfg
, bool local
)
872 traffic_selector_t
*ts
;
876 ts
= traffic_selector_create_dynamic(end
->protocol
,
877 end
->port ? end
->port
: 0, end
->port ? end
->port
: 65535);
878 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
886 net
= host_create_from_string(end
->address
, 0);
889 ts
= traffic_selector_create_from_subnet(net
, 0, end
->protocol
,
891 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
896 char *del
, *start
, *bits
;
898 start
= end
->subnets
;
903 del
= strchr(start
, ',');
908 bits
= strchr(start
, '/');
912 intbits
= atoi(bits
+ 1);
915 net
= host_create_from_string(start
, 0);
918 ts
= traffic_selector_create_from_subnet(net
, intbits
,
919 end
->protocol
, end
->port
);
920 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
924 DBG1(DBG_CFG
, "invalid subnet: %s, skipped", start
);
934 * map starter magic values to our action type
936 static action_t
map_action(int starter_action
)
938 switch (starter_action
)
942 case 3: /* =restart */
943 return ACTION_RESTART
;
950 * build a child config from the stroke message
952 static child_cfg_t
*build_child_cfg(private_stroke_config_t
*this,
955 child_cfg_t
*child_cfg
;
956 lifetime_cfg_t lifetime
= {
958 .life
= msg
->add_conn
.rekey
.ipsec_lifetime
,
959 .rekey
= msg
->add_conn
.rekey
.ipsec_lifetime
- msg
->add_conn
.rekey
.margin
,
960 .jitter
= msg
->add_conn
.rekey
.margin
* msg
->add_conn
.rekey
.fuzz
/ 100
963 .life
= msg
->add_conn
.rekey
.life_bytes
,
964 .rekey
= msg
->add_conn
.rekey
.life_bytes
- msg
->add_conn
.rekey
.margin_bytes
,
965 .jitter
= msg
->add_conn
.rekey
.margin_bytes
* msg
->add_conn
.rekey
.fuzz
/ 100
968 .life
= msg
->add_conn
.rekey
.life_packets
,
969 .rekey
= msg
->add_conn
.rekey
.life_packets
- msg
->add_conn
.rekey
.margin_packets
,
970 .jitter
= msg
->add_conn
.rekey
.margin_packets
* msg
->add_conn
.rekey
.fuzz
/ 100
974 .value
= msg
->add_conn
.mark_in
.value
,
975 .mask
= msg
->add_conn
.mark_in
.mask
978 .value
= msg
->add_conn
.mark_out
.value
,
979 .mask
= msg
->add_conn
.mark_out
.mask
982 child_cfg
= child_cfg_create(
983 msg
->add_conn
.name
, &lifetime
, msg
->add_conn
.me
.updown
,
984 msg
->add_conn
.me
.hostaccess
, msg
->add_conn
.mode
, ACTION_NONE
,
985 map_action(msg
->add_conn
.dpd
.action
),
986 map_action(msg
->add_conn
.close_action
), msg
->add_conn
.ipcomp
,
987 msg
->add_conn
.inactivity
, msg
->add_conn
.reqid
,
988 &mark_in
, &mark_out
, msg
->add_conn
.tfc
);
989 child_cfg
->set_mipv6_options(child_cfg
, msg
->add_conn
.proxy_mode
,
990 msg
->add_conn
.install_policy
);
991 add_ts(this, &msg
->add_conn
.me
, child_cfg
, TRUE
);
992 add_ts(this, &msg
->add_conn
.other
, child_cfg
, FALSE
);
994 add_proposals(this, msg
->add_conn
.algorithms
.esp
, NULL
, child_cfg
);
999 METHOD(stroke_config_t
, add
, void,
1000 private_stroke_config_t
*this, stroke_msg_t
*msg
)
1002 ike_cfg_t
*ike_cfg
, *existing_ike
;
1003 peer_cfg_t
*peer_cfg
, *existing
;
1004 child_cfg_t
*child_cfg
;
1005 enumerator_t
*enumerator
;
1006 bool use_existing
= FALSE
;
1008 ike_cfg
= build_ike_cfg(this, msg
);
1013 peer_cfg
= build_peer_cfg(this, msg
, ike_cfg
);
1016 ike_cfg
->destroy(ike_cfg
);
1020 enumerator
= create_peer_cfg_enumerator(this, NULL
, NULL
);
1021 while (enumerator
->enumerate(enumerator
, &existing
))
1023 existing_ike
= existing
->get_ike_cfg(existing
);
1024 if (existing
->equals(existing
, peer_cfg
) &&
1025 existing_ike
->equals(existing_ike
, peer_cfg
->get_ike_cfg(peer_cfg
)))
1027 use_existing
= TRUE
;
1028 peer_cfg
->destroy(peer_cfg
);
1029 peer_cfg
= existing
;
1030 peer_cfg
->get_ref(peer_cfg
);
1031 DBG1(DBG_CFG
, "added child to existing configuration '%s'",
1032 peer_cfg
->get_name(peer_cfg
));
1036 enumerator
->destroy(enumerator
);
1038 child_cfg
= build_child_cfg(this, msg
);
1041 peer_cfg
->destroy(peer_cfg
);
1044 peer_cfg
->add_child_cfg(peer_cfg
, child_cfg
);
1048 peer_cfg
->destroy(peer_cfg
);
1052 /* add config to backend */
1053 DBG1(DBG_CFG
, "added configuration '%s'", msg
->add_conn
.name
);
1054 this->mutex
->lock(this->mutex
);
1055 this->list
->insert_last(this->list
, peer_cfg
);
1056 this->mutex
->unlock(this->mutex
);
1060 METHOD(stroke_config_t
, del
, void,
1061 private_stroke_config_t
*this, stroke_msg_t
*msg
)
1063 enumerator_t
*enumerator
, *children
;
1066 bool deleted
= FALSE
;
1068 this->mutex
->lock(this->mutex
);
1069 enumerator
= this->list
->create_enumerator(this->list
);
1070 while (enumerator
->enumerate(enumerator
, (void**)&peer
))
1074 /* remove any child with such a name */
1075 children
= peer
->create_child_cfg_enumerator(peer
);
1076 while (children
->enumerate(children
, &child
))
1078 if (streq(child
->get_name(child
), msg
->del_conn
.name
))
1080 peer
->remove_child_cfg(peer
, children
);
1081 child
->destroy(child
);
1089 children
->destroy(children
);
1091 /* if peer config matches, or has no children anymore, remove it */
1092 if (!keep
|| streq(peer
->get_name(peer
), msg
->del_conn
.name
))
1094 this->list
->remove_at(this->list
, enumerator
);
1095 peer
->destroy(peer
);
1099 enumerator
->destroy(enumerator
);
1100 this->mutex
->unlock(this->mutex
);
1104 DBG1(DBG_CFG
, "deleted connection '%s'", msg
->del_conn
.name
);
1108 DBG1(DBG_CFG
, "connection '%s' not found", msg
->del_conn
.name
);
1112 METHOD(stroke_config_t
, set_user_credentials
, void,
1113 private_stroke_config_t
*this, stroke_msg_t
*msg
, FILE *prompt
)
1115 enumerator_t
*enumerator
, *children
, *remote_auth
;
1116 peer_cfg_t
*peer
, *found
= NULL
;
1117 auth_cfg_t
*auth_cfg
, *remote_cfg
;
1118 auth_class_t auth_class
;
1120 identification_t
*id
, *identity
, *gw
= NULL
;
1121 shared_key_type_t type
= SHARED_ANY
;
1122 chunk_t password
= chunk_empty
;
1124 this->mutex
->lock(this->mutex
);
1125 enumerator
= this->list
->create_enumerator(this->list
);
1126 while (enumerator
->enumerate(enumerator
, (void**)&peer
))
1127 { /* find the peer (or child) config with the given name */
1128 if (streq(peer
->get_name(peer
), msg
->user_creds
.name
))
1134 children
= peer
->create_child_cfg_enumerator(peer
);
1135 while (children
->enumerate(children
, &child
))
1137 if (streq(child
->get_name(child
), msg
->user_creds
.name
))
1143 children
->destroy(children
);
1151 enumerator
->destroy(enumerator
);
1155 DBG1(DBG_CFG
, " no config named '%s'", msg
->user_creds
.name
);
1156 fprintf(prompt
, "no config named '%s'\n", msg
->user_creds
.name
);
1157 this->mutex
->unlock(this->mutex
);
1161 id
= identification_create_from_string(msg
->user_creds
.username
);
1162 if (strlen(msg
->user_creds
.username
) == 0 ||
1163 !id
|| id
->get_type(id
) == ID_ANY
)
1165 DBG1(DBG_CFG
, " invalid username '%s'", msg
->user_creds
.username
);
1166 fprintf(prompt
, "invalid username '%s'\n", msg
->user_creds
.username
);
1167 this->mutex
->unlock(this->mutex
);
1172 /* replace/set the username in the first EAP auth_cfg, also look for a
1173 * suitable remote ID.
1174 * note that adding the identity here is not fully thread-safe as the
1175 * peer_cfg and in turn the auth_cfg could be in use. for the default use
1176 * case (setting user credentials before upping the connection) this will
1177 * not be a problem, though. */
1178 enumerator
= found
->create_auth_cfg_enumerator(found
, TRUE
);
1179 remote_auth
= found
->create_auth_cfg_enumerator(found
, FALSE
);
1180 while (enumerator
->enumerate(enumerator
, (void**)&auth_cfg
))
1182 if (remote_auth
->enumerate(remote_auth
, (void**)&remote_cfg
))
1183 { /* fall back on rightid, in case aaa_identity is not specified */
1184 identity
= remote_cfg
->get(remote_cfg
, AUTH_RULE_IDENTITY
);
1185 if (identity
&& identity
->get_type(identity
) != ID_ANY
)
1191 auth_class
= (uintptr_t)auth_cfg
->get(auth_cfg
, AUTH_RULE_AUTH_CLASS
);
1192 if (auth_class
== AUTH_CLASS_EAP
)
1194 auth_cfg
->add(auth_cfg
, AUTH_RULE_EAP_IDENTITY
, id
->clone(id
));
1195 /* if aaa_identity is specified use that as remote ID */
1196 identity
= auth_cfg
->get(auth_cfg
, AUTH_RULE_AAA_IDENTITY
);
1197 if (identity
&& identity
->get_type(identity
) != ID_ANY
)
1201 DBG1(DBG_CFG
, " configured EAP-Identity %Y", id
);
1206 enumerator
->destroy(enumerator
);
1207 remote_auth
->destroy(remote_auth
);
1208 /* clone the gw ID before unlocking the mutex */
1213 this->mutex
->unlock(this->mutex
);
1215 if (type
== SHARED_ANY
)
1217 DBG1(DBG_CFG
, " config '%s' unsuitable for user credentials",
1218 msg
->user_creds
.name
);
1219 fprintf(prompt
, "config '%s' unsuitable for user credentials\n",
1220 msg
->user_creds
.name
);
1226 if (msg
->user_creds
.password
)
1230 pass
= msg
->user_creds
.password
;
1231 password
= chunk_clone(chunk_create(pass
, strlen(pass
)));
1232 memwipe(pass
, strlen(pass
));
1235 { /* prompt the user for the password */
1238 fprintf(prompt
, "Password:\n");
1239 if (fgets(buf
, sizeof(buf
), prompt
))
1241 password
= chunk_clone(chunk_create(buf
, strlen(buf
)));
1242 if (password
.len
> 0)
1243 { /* trim trailing \n */
1246 memwipe(buf
, sizeof(buf
));
1252 shared_key_t
*shared
;
1253 linked_list_t
*owners
;
1255 shared
= shared_key_create(type
, password
);
1257 owners
= linked_list_create();
1258 owners
->insert_last(owners
, id
->clone(id
));
1259 if (gw
&& gw
->get_type(gw
) != ID_ANY
)
1261 owners
->insert_last(owners
, gw
->clone(gw
));
1262 DBG1(DBG_CFG
, " added %N secret for %Y %Y", shared_key_type_names
,
1267 DBG1(DBG_CFG
, " added %N secret for %Y", shared_key_type_names
,
1270 this->cred
->add_shared(this->cred
, shared
, owners
);
1271 DBG4(DBG_CFG
, " secret: %#B", &password
);
1274 { /* in case a user answers the password prompt by just pressing enter */
1275 chunk_clear(&password
);
1281 METHOD(stroke_config_t
, destroy
, void,
1282 private_stroke_config_t
*this)
1284 this->list
->destroy_offset(this->list
, offsetof(peer_cfg_t
, destroy
));
1285 this->mutex
->destroy(this->mutex
);
1292 stroke_config_t
*stroke_config_create(stroke_ca_t
*ca
, stroke_cred_t
*cred
,
1293 stroke_attribute_t
*attributes
)
1295 private_stroke_config_t
*this;
1300 .create_peer_cfg_enumerator
= _create_peer_cfg_enumerator
,
1301 .create_ike_cfg_enumerator
= _create_ike_cfg_enumerator
,
1302 .get_peer_cfg_by_name
= _get_peer_cfg_by_name
,
1306 .set_user_credentials
= _set_user_credentials
,
1307 .destroy
= _destroy
,
1309 .list
= linked_list_create(),
1310 .mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
),
1313 .attributes
= attributes
,
1316 return &this->public;