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 METHOD(backend_t
, create_peer_cfg_enumerator
, enumerator_t
*,
58 private_stroke_config_t
*this, identification_t
*me
, identification_t
*other
)
60 this->mutex
->lock(this->mutex
);
61 return enumerator_create_cleaner(this->list
->create_enumerator(this->list
),
62 (void*)this->mutex
->unlock
, this->mutex
);
66 * filter function for ike configs
68 static bool ike_filter(void *data
, peer_cfg_t
**in
, ike_cfg_t
**out
)
70 *out
= (*in
)->get_ike_cfg(*in
);
74 METHOD(backend_t
, create_ike_cfg_enumerator
, enumerator_t
*,
75 private_stroke_config_t
*this, host_t
*me
, host_t
*other
)
77 this->mutex
->lock(this->mutex
);
78 return enumerator_create_filter(this->list
->create_enumerator(this->list
),
79 (void*)ike_filter
, this->mutex
,
80 (void*)this->mutex
->unlock
);
83 METHOD(backend_t
, get_peer_cfg_by_name
, peer_cfg_t
*,
84 private_stroke_config_t
*this, char *name
)
86 enumerator_t
*e1
, *e2
;
87 peer_cfg_t
*current
, *found
= NULL
;
90 this->mutex
->lock(this->mutex
);
91 e1
= this->list
->create_enumerator(this->list
);
92 while (e1
->enumerate(e1
, ¤t
))
94 /* compare peer_cfgs name first */
95 if (streq(current
->get_name(current
), name
))
98 found
->get_ref(found
);
101 /* compare all child_cfg names otherwise */
102 e2
= current
->create_child_cfg_enumerator(current
);
103 while (e2
->enumerate(e2
, &child
))
105 if (streq(child
->get_name(child
), name
))
108 found
->get_ref(found
);
119 this->mutex
->unlock(this->mutex
);
124 * parse a proposal string, either into ike_cfg or child_cfg
126 static void add_proposals(private_stroke_config_t
*this, char *string
,
127 ike_cfg_t
*ike_cfg
, child_cfg_t
*child_cfg
)
133 proposal_t
*proposal
;
134 protocol_id_t proto
= PROTO_ESP
;
140 strict
= string
+ strlen(string
) - 1;
149 while ((single
= strsep(&string
, ",")))
151 proposal
= proposal_create_from_string(proto
, single
);
156 ike_cfg
->add_proposal(ike_cfg
, proposal
);
160 child_cfg
->add_proposal(child_cfg
, proposal
);
164 DBG1(DBG_CFG
, "skipped invalid proposal string: %s", single
);
170 /* add default porposal to the end if not strict */
174 ike_cfg
->add_proposal(ike_cfg
, proposal_create_default(PROTO_IKE
));
178 child_cfg
->add_proposal(child_cfg
, proposal_create_default(PROTO_ESP
));
183 * Build an IKE config from a stroke message
185 static ike_cfg_t
*build_ike_cfg(private_stroke_config_t
*this, stroke_msg_t
*msg
)
187 stroke_end_t tmp_end
;
192 host
= host_create_from_dns(msg
->add_conn
.other
.address
, 0, 0);
195 interface
= hydra
->kernel_interface
->get_interface(
196 hydra
->kernel_interface
, host
);
200 DBG2(DBG_CFG
, "left is other host, swapping ends");
201 tmp_end
= msg
->add_conn
.me
;
202 msg
->add_conn
.me
= msg
->add_conn
.other
;
203 msg
->add_conn
.other
= tmp_end
;
208 host
= host_create_from_dns(msg
->add_conn
.me
.address
, 0, 0);
211 interface
= hydra
->kernel_interface
->get_interface(
212 hydra
->kernel_interface
, host
);
216 DBG1(DBG_CFG
, "left nor right host is our side, "
217 "assuming left=local");
227 ike_cfg
= ike_cfg_create(msg
->add_conn
.other
.sendcert
!= CERT_NEVER_SEND
,
228 msg
->add_conn
.force_encap
,
229 msg
->add_conn
.me
.address
,
230 msg
->add_conn
.me
.allow_any
,
231 msg
->add_conn
.me
.ikeport
,
232 msg
->add_conn
.other
.address
,
233 msg
->add_conn
.other
.allow_any
,
234 msg
->add_conn
.other
.ikeport
);
235 add_proposals(this, msg
->add_conn
.algorithms
.ike
, ike_cfg
, NULL
);
240 * Add CRL constraint to config
242 static void build_crl_policy(auth_cfg_t
*cfg
, bool local
, int policy
)
244 /* CRL/OCSP policy, for remote config only */
250 /* if yes, we require a GOOD validation */
251 cfg
->add(cfg
, AUTH_RULE_CRL_VALIDATION
, VALIDATION_GOOD
);
253 case CRL_STRICT_IFURI
:
254 /* for ifuri, a SKIPPED validation is sufficient */
255 cfg
->add(cfg
, AUTH_RULE_CRL_VALIDATION
, VALIDATION_SKIPPED
);
264 * Parse public key / signature strength constraints
266 static void parse_pubkey_constraints(char *auth
, auth_cfg_t
*cfg
)
268 enumerator_t
*enumerator
;
269 bool rsa
= FALSE
, ecdsa
= FALSE
, rsa_len
= FALSE
, ecdsa_len
= FALSE
;
273 enumerator
= enumerator_create_token(auth
, "-", "");
274 while (enumerator
->enumerate(enumerator
, &token
))
280 signature_scheme_t scheme
;
283 { "md5", SIGN_RSA_EMSA_PKCS1_MD5
, KEY_RSA
, },
284 { "sha1", SIGN_RSA_EMSA_PKCS1_SHA1
, KEY_RSA
, },
285 { "sha224", SIGN_RSA_EMSA_PKCS1_SHA224
, KEY_RSA
, },
286 { "sha256", SIGN_RSA_EMSA_PKCS1_SHA256
, KEY_RSA
, },
287 { "sha384", SIGN_RSA_EMSA_PKCS1_SHA384
, KEY_RSA
, },
288 { "sha512", SIGN_RSA_EMSA_PKCS1_SHA512
, KEY_RSA
, },
289 { "sha1", SIGN_ECDSA_WITH_SHA1_DER
, KEY_ECDSA
, },
290 { "sha256", SIGN_ECDSA_WITH_SHA256_DER
, KEY_ECDSA
, },
291 { "sha384", SIGN_ECDSA_WITH_SHA384_DER
, KEY_ECDSA
, },
292 { "sha512", SIGN_ECDSA_WITH_SHA512_DER
, KEY_ECDSA
, },
293 { "sha256", SIGN_ECDSA_256
, KEY_ECDSA
, },
294 { "sha384", SIGN_ECDSA_384
, KEY_ECDSA
, },
295 { "sha512", SIGN_ECDSA_521
, KEY_ECDSA
, },
298 if (rsa_len
|| ecdsa_len
)
299 { /* expecting a key strength token */
300 strength
= atoi(token
);
305 cfg
->add(cfg
, AUTH_RULE_RSA_STRENGTH
, (uintptr_t)strength
);
309 cfg
->add(cfg
, AUTH_RULE_ECDSA_STRENGTH
, (uintptr_t)strength
);
312 rsa_len
= ecdsa_len
= FALSE
;
318 if (streq(token
, "rsa"))
320 rsa
= rsa_len
= TRUE
;
323 if (streq(token
, "ecdsa"))
325 ecdsa
= ecdsa_len
= TRUE
;
328 if (streq(token
, "pubkey"))
333 for (i
= 0; i
< countof(schemes
); i
++)
335 if (streq(schemes
[i
].name
, token
))
337 /* for each matching string, allow the scheme, if:
338 * - it is an RSA scheme, and we enforced RSA
339 * - it is an ECDSA scheme, and we enforced ECDSA
340 * - it is not a key type specific scheme
342 if ((rsa
&& schemes
[i
].key
== KEY_RSA
) ||
343 (ecdsa
&& schemes
[i
].key
== KEY_ECDSA
) ||
346 cfg
->add(cfg
, AUTH_RULE_SIGNATURE_SCHEME
,
347 (uintptr_t)schemes
[i
].scheme
);
354 DBG1(DBG_CFG
, "ignoring invalid auth token: '%s'", token
);
357 enumerator
->destroy(enumerator
);
361 * build authentication config
363 static auth_cfg_t
*build_auth_cfg(private_stroke_config_t
*this,
364 stroke_msg_t
*msg
, bool local
, bool primary
)
366 identification_t
*identity
;
367 certificate_t
*certificate
;
368 char *auth
, *id
, *pubkey
, *cert
, *ca
, *groups
;
369 stroke_end_t
*end
, *other_end
;
375 end
= &msg
->add_conn
.me
;
376 other_end
= &msg
->add_conn
.other
;
380 end
= &msg
->add_conn
.other
;
381 other_end
= &msg
->add_conn
.me
;
388 { /* leftid/rightid fallback to address */
393 if (ca
&& streq(ca
, "%same"))
403 { /* leftid2 falls back to leftid */
408 if (ca
&& streq(ca
, "%same"))
421 { /* no second authentication round, fine. But load certificates
422 * for other purposes (EAP-TLS) */
425 certificate
= this->cred
->load_peer(this->cred
, cert
);
428 certificate
->destroy(certificate
);
435 cfg
= auth_cfg_create();
437 /* add identity and peer certifcate */
438 identity
= identification_create_from_string(id
);
441 certificate
= this->cred
->load_peer(this->cred
, cert
);
446 this->ca
->check_for_hash_and_url(this->ca
, certificate
);
448 cfg
->add(cfg
, AUTH_RULE_SUBJECT_CERT
, certificate
);
449 if (identity
->get_type(identity
) == ID_ANY
||
450 !certificate
->has_subject(certificate
, identity
))
452 DBG1(DBG_CFG
, " id '%Y' not confirmed by certificate, "
453 "defaulting to '%Y'", identity
,
454 certificate
->get_subject(certificate
));
455 identity
->destroy(identity
);
456 identity
= certificate
->get_subject(certificate
);
457 identity
= identity
->clone(identity
);
461 if (identity
->get_type(identity
) != ID_ANY
)
463 cfg
->add(cfg
, AUTH_RULE_IDENTITY
, identity
);
467 identity
->destroy(identity
);
470 /* add raw RSA public key */
471 pubkey
= end
->rsakey
;
472 if (pubkey
&& !streq(pubkey
, "") && !streq(pubkey
, "%cert"))
474 certificate
= this->cred
->load_pubkey(this->cred
, KEY_RSA
, pubkey
,
478 cfg
->add(cfg
, AUTH_RULE_SUBJECT_CERT
, certificate
);
485 identity
= identification_create_from_string(ca
);
486 certificate
= lib
->credmgr
->get_cert(lib
->credmgr
, CERT_X509
,
487 KEY_ANY
, identity
, TRUE
);
488 identity
->destroy(identity
);
491 cfg
->add(cfg
, AUTH_RULE_CA_CERT
, certificate
);
495 DBG1(DBG_CFG
, "CA certificate \"%s\" not found, discarding CA "
501 groups
= primary ? end
->groups
: end
->groups2
;
504 enumerator_t
*enumerator
;
507 enumerator
= enumerator_create_token(groups
, ",", " ");
508 while (enumerator
->enumerate(enumerator
, &group
))
510 cfg
->add(cfg
, AUTH_RULE_GROUP
,
511 identification_create_from_string(group
));
513 enumerator
->destroy(enumerator
);
516 /* certificatePolicies */
517 if (end
->cert_policy
)
519 enumerator_t
*enumerator
;
522 enumerator
= enumerator_create_token(end
->cert_policy
, ",", " ");
523 while (enumerator
->enumerate(enumerator
, &policy
))
525 cfg
->add(cfg
, AUTH_RULE_CERT_POLICY
, strdup(policy
));
527 enumerator
->destroy(enumerator
);
530 /* authentication metod (class, actually) */
531 if (strneq(auth
, "pubkey", strlen("pubkey")) ||
532 strneq(auth
, "rsa", strlen("rsa")) ||
533 strneq(auth
, "ecdsa", strlen("ecdsa")))
535 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PUBKEY
);
536 build_crl_policy(cfg
, local
, msg
->add_conn
.crl_policy
);
538 parse_pubkey_constraints(auth
, cfg
);
540 else if (streq(auth
, "psk") || streq(auth
, "secret"))
542 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_PSK
);
544 else if (strneq(auth
, "xauth", 5))
548 pos
= strchr(auth
, '-');
551 cfg
->add(cfg
, AUTH_RULE_XAUTH_BACKEND
, strdup(++pos
));
553 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_XAUTH
);
554 if (msg
->add_conn
.xauth_identity
)
556 cfg
->add(cfg
, AUTH_RULE_XAUTH_IDENTITY
,
557 identification_create_from_string(msg
->add_conn
.xauth_identity
));
560 else if (strneq(auth
, "eap", 3))
562 enumerator_t
*enumerator
;
564 int i
= 0, type
= 0, vendor
;
566 cfg
->add(cfg
, AUTH_RULE_AUTH_CLASS
, AUTH_CLASS_EAP
);
568 /* parse EAP string, format: eap[-type[-vendor]] */
569 enumerator
= enumerator_create_token(auth
, "-", " ");
570 while (enumerator
->enumerate(enumerator
, &str
))
575 type
= eap_type_from_string(str
);
581 DBG1(DBG_CFG
, "unknown EAP method: %s", str
);
585 cfg
->add(cfg
, AUTH_RULE_EAP_TYPE
, type
);
593 cfg
->add(cfg
, AUTH_RULE_EAP_VENDOR
, vendor
);
597 DBG1(DBG_CFG
, "unknown EAP vendor: %s", str
);
606 enumerator
->destroy(enumerator
);
608 if (msg
->add_conn
.eap_identity
)
610 if (streq(msg
->add_conn
.eap_identity
, "%identity"))
612 identity
= identification_create_from_encoding(ID_ANY
,
617 identity
= identification_create_from_string(
618 msg
->add_conn
.eap_identity
);
620 cfg
->add(cfg
, AUTH_RULE_EAP_IDENTITY
, identity
);
622 if (msg
->add_conn
.aaa_identity
)
624 cfg
->add(cfg
, AUTH_RULE_AAA_IDENTITY
,
625 identification_create_from_string(msg
->add_conn
.aaa_identity
));
630 if (!streq(auth
, "any"))
632 DBG1(DBG_CFG
, "authentication method %s unknown, fallback to any",
635 build_crl_policy(cfg
, local
, msg
->add_conn
.crl_policy
);
641 * build a peer_cfg from a stroke msg
643 static peer_cfg_t
*build_peer_cfg(private_stroke_config_t
*this,
644 stroke_msg_t
*msg
, ike_cfg_t
*ike_cfg
)
646 identification_t
*peer_id
= NULL
;
647 peer_cfg_t
*mediated_by
= NULL
;
649 unique_policy_t unique
;
650 u_int32_t rekey
= 0, reauth
= 0, over
, jitter
;
651 peer_cfg_t
*peer_cfg
;
652 auth_cfg_t
*auth_cfg
;
655 if (msg
->add_conn
.ikeme
.mediation
&& msg
->add_conn
.ikeme
.mediated_by
)
657 DBG1(DBG_CFG
, "a mediation connection cannot be a mediated connection "
658 "at the same time, aborting");
662 if (msg
->add_conn
.ikeme
.mediation
)
664 /* force unique connections for mediation connections */
665 msg
->add_conn
.unique
= 1;
668 if (msg
->add_conn
.ikeme
.mediated_by
)
670 mediated_by
= charon
->backends
->get_peer_cfg_by_name(charon
->backends
,
671 msg
->add_conn
.ikeme
.mediated_by
);
674 DBG1(DBG_CFG
, "mediation connection '%s' not found, aborting",
675 msg
->add_conn
.ikeme
.mediated_by
);
678 if (!mediated_by
->is_mediation(mediated_by
))
680 DBG1(DBG_CFG
, "connection '%s' as referred to by '%s' is "
681 "no mediation connection, aborting",
682 msg
->add_conn
.ikeme
.mediated_by
, msg
->add_conn
.name
);
683 mediated_by
->destroy(mediated_by
);
686 if (msg
->add_conn
.ikeme
.peerid
)
688 peer_id
= identification_create_from_string(msg
->add_conn
.ikeme
.peerid
);
690 else if (msg
->add_conn
.other
.id
)
692 peer_id
= identification_create_from_string(msg
->add_conn
.other
.id
);
697 jitter
= msg
->add_conn
.rekey
.margin
* msg
->add_conn
.rekey
.fuzz
/ 100;
698 over
= msg
->add_conn
.rekey
.margin
;
699 if (msg
->add_conn
.rekey
.reauth
)
701 reauth
= msg
->add_conn
.rekey
.ike_lifetime
- over
;
705 rekey
= msg
->add_conn
.rekey
.ike_lifetime
- over
;
707 if (msg
->add_conn
.me
.sourceip_mask
)
709 if (msg
->add_conn
.me
.sourceip
)
711 vip
= host_create_from_string(msg
->add_conn
.me
.sourceip
, 0);
714 { /* if it is set to something like %poolname, request an address */
715 if (msg
->add_conn
.me
.subnets
)
716 { /* use the same family as in local subnet, if any */
717 if (strchr(msg
->add_conn
.me
.subnets
, '.'))
719 vip
= host_create_any(AF_INET
);
723 vip
= host_create_any(AF_INET6
);
726 else if (msg
->add_conn
.other
.subnets
)
727 { /* use the same family as in remote subnet, if any */
728 if (strchr(msg
->add_conn
.other
.subnets
, '.'))
730 vip
= host_create_any(AF_INET
);
734 vip
= host_create_any(AF_INET6
);
739 if (strchr(ike_cfg
->get_my_addr(ike_cfg
, NULL
), ':'))
741 vip
= host_create_any(AF_INET6
);
745 vip
= host_create_any(AF_INET
);
750 switch (msg
->add_conn
.unique
)
753 case 2: /* replace */
754 unique
= UNIQUE_REPLACE
;
757 unique
= UNIQUE_KEEP
;
763 if (msg
->add_conn
.dpd
.action
== 0)
764 { /* dpdaction=none disables DPD */
765 msg
->add_conn
.dpd
.delay
= 0;
768 /* other.sourceip is managed in stroke_attributes. If it is set, we define
769 * the pool name as the connection name, which the attribute provider
770 * uses to serve pool addresses. */
771 peer_cfg
= peer_cfg_create(msg
->add_conn
.name
,
772 msg
->add_conn
.version
, ike_cfg
,
773 msg
->add_conn
.me
.sendcert
, unique
,
774 msg
->add_conn
.rekey
.tries
, rekey
, reauth
, jitter
, over
,
775 msg
->add_conn
.mobike
, msg
->add_conn
.aggressive
,
776 msg
->add_conn
.dpd
.delay
, msg
->add_conn
.dpd
.timeout
,
777 vip
, msg
->add_conn
.other
.sourceip_mask ?
778 msg
->add_conn
.name
: msg
->add_conn
.other
.sourceip
,
779 msg
->add_conn
.ikeme
.mediation
, mediated_by
, peer_id
);
781 /* build leftauth= */
782 auth_cfg
= build_auth_cfg(this, msg
, TRUE
, TRUE
);
785 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, TRUE
);
788 { /* we require at least one config on our side */
789 peer_cfg
->destroy(peer_cfg
);
792 /* build leftauth2= */
793 auth_cfg
= build_auth_cfg(this, msg
, TRUE
, FALSE
);
796 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, TRUE
);
798 /* build rightauth= */
799 auth_cfg
= build_auth_cfg(this, msg
, FALSE
, TRUE
);
802 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, FALSE
);
804 /* build rightauth2= */
805 auth_cfg
= build_auth_cfg(this, msg
, FALSE
, FALSE
);
808 peer_cfg
->add_auth_cfg(peer_cfg
, auth_cfg
, FALSE
);
814 * build a traffic selector from a stroke_end
816 static void add_ts(private_stroke_config_t
*this,
817 stroke_end_t
*end
, child_cfg_t
*child_cfg
, bool local
)
819 traffic_selector_t
*ts
;
823 ts
= traffic_selector_create_dynamic(end
->protocol
,
824 end
->port ? end
->port
: 0, end
->port ? end
->port
: 65535);
825 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
833 net
= host_create_from_string(end
->address
, 0);
836 ts
= traffic_selector_create_from_subnet(net
, 0, end
->protocol
,
838 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
843 char *del
, *start
, *bits
;
845 start
= end
->subnets
;
850 del
= strchr(start
, ',');
855 bits
= strchr(start
, '/');
859 intbits
= atoi(bits
+ 1);
862 net
= host_create_from_string(start
, 0);
865 ts
= traffic_selector_create_from_subnet(net
, intbits
,
866 end
->protocol
, end
->port
);
867 child_cfg
->add_traffic_selector(child_cfg
, local
, ts
);
871 DBG1(DBG_CFG
, "invalid subnet: %s, skipped", start
);
881 * map starter magic values to our action type
883 static action_t
map_action(int starter_action
)
885 switch (starter_action
)
889 case 3: /* =restart */
890 return ACTION_RESTART
;
897 * build a child config from the stroke message
899 static child_cfg_t
*build_child_cfg(private_stroke_config_t
*this,
902 child_cfg_t
*child_cfg
;
903 lifetime_cfg_t lifetime
= {
905 .life
= msg
->add_conn
.rekey
.ipsec_lifetime
,
906 .rekey
= msg
->add_conn
.rekey
.ipsec_lifetime
- msg
->add_conn
.rekey
.margin
,
907 .jitter
= msg
->add_conn
.rekey
.margin
* msg
->add_conn
.rekey
.fuzz
/ 100
910 .life
= msg
->add_conn
.rekey
.life_bytes
,
911 .rekey
= msg
->add_conn
.rekey
.life_bytes
- msg
->add_conn
.rekey
.margin_bytes
,
912 .jitter
= msg
->add_conn
.rekey
.margin_bytes
* msg
->add_conn
.rekey
.fuzz
/ 100
915 .life
= msg
->add_conn
.rekey
.life_packets
,
916 .rekey
= msg
->add_conn
.rekey
.life_packets
- msg
->add_conn
.rekey
.margin_packets
,
917 .jitter
= msg
->add_conn
.rekey
.margin_packets
* msg
->add_conn
.rekey
.fuzz
/ 100
921 .value
= msg
->add_conn
.mark_in
.value
,
922 .mask
= msg
->add_conn
.mark_in
.mask
925 .value
= msg
->add_conn
.mark_out
.value
,
926 .mask
= msg
->add_conn
.mark_out
.mask
929 child_cfg
= child_cfg_create(
930 msg
->add_conn
.name
, &lifetime
, msg
->add_conn
.me
.updown
,
931 msg
->add_conn
.me
.hostaccess
, msg
->add_conn
.mode
, ACTION_NONE
,
932 map_action(msg
->add_conn
.dpd
.action
),
933 map_action(msg
->add_conn
.close_action
), msg
->add_conn
.ipcomp
,
934 msg
->add_conn
.inactivity
, msg
->add_conn
.reqid
,
935 &mark_in
, &mark_out
, msg
->add_conn
.tfc
);
936 child_cfg
->set_mipv6_options(child_cfg
, msg
->add_conn
.proxy_mode
,
937 msg
->add_conn
.install_policy
);
938 add_ts(this, &msg
->add_conn
.me
, child_cfg
, TRUE
);
939 add_ts(this, &msg
->add_conn
.other
, child_cfg
, FALSE
);
941 add_proposals(this, msg
->add_conn
.algorithms
.esp
, NULL
, child_cfg
);
946 METHOD(stroke_config_t
, add
, void,
947 private_stroke_config_t
*this, stroke_msg_t
*msg
)
949 ike_cfg_t
*ike_cfg
, *existing_ike
;
950 peer_cfg_t
*peer_cfg
, *existing
;
951 child_cfg_t
*child_cfg
;
952 enumerator_t
*enumerator
;
953 bool use_existing
= FALSE
;
955 ike_cfg
= build_ike_cfg(this, msg
);
960 peer_cfg
= build_peer_cfg(this, msg
, ike_cfg
);
963 ike_cfg
->destroy(ike_cfg
);
967 enumerator
= create_peer_cfg_enumerator(this, NULL
, NULL
);
968 while (enumerator
->enumerate(enumerator
, &existing
))
970 existing_ike
= existing
->get_ike_cfg(existing
);
971 if (existing
->equals(existing
, peer_cfg
) &&
972 existing_ike
->equals(existing_ike
, peer_cfg
->get_ike_cfg(peer_cfg
)))
975 peer_cfg
->destroy(peer_cfg
);
977 peer_cfg
->get_ref(peer_cfg
);
978 DBG1(DBG_CFG
, "added child to existing configuration '%s'",
979 peer_cfg
->get_name(peer_cfg
));
983 enumerator
->destroy(enumerator
);
985 child_cfg
= build_child_cfg(this, msg
);
988 peer_cfg
->destroy(peer_cfg
);
991 peer_cfg
->add_child_cfg(peer_cfg
, child_cfg
);
995 peer_cfg
->destroy(peer_cfg
);
999 /* add config to backend */
1000 DBG1(DBG_CFG
, "added configuration '%s'", msg
->add_conn
.name
);
1001 this->mutex
->lock(this->mutex
);
1002 this->list
->insert_last(this->list
, peer_cfg
);
1003 this->mutex
->unlock(this->mutex
);
1007 METHOD(stroke_config_t
, del
, void,
1008 private_stroke_config_t
*this, stroke_msg_t
*msg
)
1010 enumerator_t
*enumerator
, *children
;
1013 bool deleted
= FALSE
;
1015 this->mutex
->lock(this->mutex
);
1016 enumerator
= this->list
->create_enumerator(this->list
);
1017 while (enumerator
->enumerate(enumerator
, (void**)&peer
))
1021 /* remove any child with such a name */
1022 children
= peer
->create_child_cfg_enumerator(peer
);
1023 while (children
->enumerate(children
, &child
))
1025 if (streq(child
->get_name(child
), msg
->del_conn
.name
))
1027 peer
->remove_child_cfg(peer
, children
);
1028 child
->destroy(child
);
1036 children
->destroy(children
);
1038 /* if peer config matches, or has no children anymore, remove it */
1039 if (!keep
|| streq(peer
->get_name(peer
), msg
->del_conn
.name
))
1041 this->list
->remove_at(this->list
, enumerator
);
1042 peer
->destroy(peer
);
1046 enumerator
->destroy(enumerator
);
1047 this->mutex
->unlock(this->mutex
);
1051 DBG1(DBG_CFG
, "deleted connection '%s'", msg
->del_conn
.name
);
1055 DBG1(DBG_CFG
, "connection '%s' not found", msg
->del_conn
.name
);
1059 METHOD(stroke_config_t
, set_user_credentials
, void,
1060 private_stroke_config_t
*this, stroke_msg_t
*msg
, FILE *prompt
)
1062 enumerator_t
*enumerator
, *children
, *remote_auth
;
1063 peer_cfg_t
*peer
, *found
= NULL
;
1064 auth_cfg_t
*auth_cfg
, *remote_cfg
;
1065 auth_class_t auth_class
;
1067 identification_t
*id
, *identity
, *gw
= NULL
;
1068 shared_key_type_t type
= SHARED_ANY
;
1069 chunk_t password
= chunk_empty
;
1071 this->mutex
->lock(this->mutex
);
1072 enumerator
= this->list
->create_enumerator(this->list
);
1073 while (enumerator
->enumerate(enumerator
, (void**)&peer
))
1074 { /* find the peer (or child) config with the given name */
1075 if (streq(peer
->get_name(peer
), msg
->user_creds
.name
))
1081 children
= peer
->create_child_cfg_enumerator(peer
);
1082 while (children
->enumerate(children
, &child
))
1084 if (streq(child
->get_name(child
), msg
->user_creds
.name
))
1090 children
->destroy(children
);
1098 enumerator
->destroy(enumerator
);
1102 DBG1(DBG_CFG
, " no config named '%s'", msg
->user_creds
.name
);
1103 fprintf(prompt
, "no config named '%s'\n", msg
->user_creds
.name
);
1104 this->mutex
->unlock(this->mutex
);
1108 id
= identification_create_from_string(msg
->user_creds
.username
);
1109 if (strlen(msg
->user_creds
.username
) == 0 ||
1110 !id
|| id
->get_type(id
) == ID_ANY
)
1112 DBG1(DBG_CFG
, " invalid username '%s'", msg
->user_creds
.username
);
1113 fprintf(prompt
, "invalid username '%s'\n", msg
->user_creds
.username
);
1114 this->mutex
->unlock(this->mutex
);
1119 /* replace/set the username in the first EAP auth_cfg, also look for a
1120 * suitable remote ID.
1121 * note that adding the identity here is not fully thread-safe as the
1122 * peer_cfg and in turn the auth_cfg could be in use. for the default use
1123 * case (setting user credentials before upping the connection) this will
1124 * not be a problem, though. */
1125 enumerator
= found
->create_auth_cfg_enumerator(found
, TRUE
);
1126 remote_auth
= found
->create_auth_cfg_enumerator(found
, FALSE
);
1127 while (enumerator
->enumerate(enumerator
, (void**)&auth_cfg
))
1129 if (remote_auth
->enumerate(remote_auth
, (void**)&remote_cfg
))
1130 { /* fall back on rightid, in case aaa_identity is not specified */
1131 identity
= remote_cfg
->get(remote_cfg
, AUTH_RULE_IDENTITY
);
1132 if (identity
&& identity
->get_type(identity
) != ID_ANY
)
1138 auth_class
= (uintptr_t)auth_cfg
->get(auth_cfg
, AUTH_RULE_AUTH_CLASS
);
1139 if (auth_class
== AUTH_CLASS_EAP
)
1141 auth_cfg
->add(auth_cfg
, AUTH_RULE_EAP_IDENTITY
, id
->clone(id
));
1142 /* if aaa_identity is specified use that as remote ID */
1143 identity
= auth_cfg
->get(auth_cfg
, AUTH_RULE_AAA_IDENTITY
);
1144 if (identity
&& identity
->get_type(identity
) != ID_ANY
)
1148 DBG1(DBG_CFG
, " configured EAP-Identity %Y", id
);
1153 enumerator
->destroy(enumerator
);
1154 remote_auth
->destroy(remote_auth
);
1155 /* clone the gw ID before unlocking the mutex */
1160 this->mutex
->unlock(this->mutex
);
1162 if (type
== SHARED_ANY
)
1164 DBG1(DBG_CFG
, " config '%s' unsuitable for user credentials",
1165 msg
->user_creds
.name
);
1166 fprintf(prompt
, "config '%s' unsuitable for user credentials\n",
1167 msg
->user_creds
.name
);
1173 if (msg
->user_creds
.password
)
1177 pass
= msg
->user_creds
.password
;
1178 password
= chunk_clone(chunk_create(pass
, strlen(pass
)));
1179 memwipe(pass
, strlen(pass
));
1182 { /* prompt the user for the password */
1185 fprintf(prompt
, "Password:\n");
1186 if (fgets(buf
, sizeof(buf
), prompt
))
1188 password
= chunk_clone(chunk_create(buf
, strlen(buf
)));
1189 if (password
.len
> 0)
1190 { /* trim trailing \n */
1193 memwipe(buf
, sizeof(buf
));
1199 shared_key_t
*shared
;
1200 linked_list_t
*owners
;
1202 shared
= shared_key_create(type
, password
);
1204 owners
= linked_list_create();
1205 owners
->insert_last(owners
, id
->clone(id
));
1206 if (gw
&& gw
->get_type(gw
) != ID_ANY
)
1208 owners
->insert_last(owners
, gw
->clone(gw
));
1209 DBG1(DBG_CFG
, " added %N secret for %Y %Y", shared_key_type_names
,
1214 DBG1(DBG_CFG
, " added %N secret for %Y", shared_key_type_names
,
1217 this->cred
->add_shared(this->cred
, shared
, owners
);
1218 DBG4(DBG_CFG
, " secret: %#B", &password
);
1221 { /* in case a user answers the password prompt by just pressing enter */
1222 chunk_clear(&password
);
1228 METHOD(stroke_config_t
, destroy
, void,
1229 private_stroke_config_t
*this)
1231 this->list
->destroy_offset(this->list
, offsetof(peer_cfg_t
, destroy
));
1232 this->mutex
->destroy(this->mutex
);
1239 stroke_config_t
*stroke_config_create(stroke_ca_t
*ca
, stroke_cred_t
*cred
)
1241 private_stroke_config_t
*this;
1246 .create_peer_cfg_enumerator
= _create_peer_cfg_enumerator
,
1247 .create_ike_cfg_enumerator
= _create_ike_cfg_enumerator
,
1248 .get_peer_cfg_by_name
= _get_peer_cfg_by_name
,
1252 .set_user_credentials
= _set_user_credentials
,
1253 .destroy
= _destroy
,
1255 .list
= linked_list_create(),
1256 .mutex
= mutex_create(MUTEX_TYPE_RECURSIVE
),
1261 return &this->public;