vici: Undo start actions when unloading configs
[strongswan.git] / src / libcharon / plugins / vici / vici_config.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * Copyright (C) 2015 Andreas Steffen
6 * HSR Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /*
20 * Copyright (C) 2014 Timo Teräs <timo.teras@iki.fi>
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 #define _GNU_SOURCE
42
43 #include "vici_config.h"
44 #include "vici_builder.h"
45
46 #include <daemon.h>
47 #include <threading/rwlock.h>
48 #include <collections/array.h>
49 #include <collections/linked_list.h>
50
51 #include <stdio.h>
52
53 /**
54 * Magic value for an undefined lifetime
55 */
56 #define LFT_UNDEFINED (~(u_int64_t)0)
57
58 /**
59 * Default IKE rekey time
60 */
61 #define LFT_DEFAULT_IKE_REKEY (4 * 60 * 60)
62
63 /**
64 * Default CHILD rekey time
65 */
66 #define LFT_DEFAULT_CHILD_REKEY (1 * 60 * 60)
67
68 /**
69 * Undefined replay window
70 */
71 #define REPLAY_UNDEFINED (~(u_int32_t)0)
72
73 typedef struct private_vici_config_t private_vici_config_t;
74
75 /**
76 * Private data of an vici_config_t object.
77 */
78 struct private_vici_config_t {
79
80 /**
81 * Public vici_config_t interface.
82 */
83 vici_config_t public;
84
85 /**
86 * Dispatcher
87 */
88 vici_dispatcher_t *dispatcher;
89
90 /**
91 * List of loaded connections, as peer_cfg_t
92 */
93 linked_list_t *conns;
94
95 /**
96 * Lock for conns list
97 */
98 rwlock_t *lock;
99
100 /**
101 * Auxiliary certification authority information
102 */
103 vici_authority_t *authority;
104
105 };
106
107 METHOD(backend_t, create_peer_cfg_enumerator, enumerator_t*,
108 private_vici_config_t *this, identification_t *me, identification_t *other)
109 {
110 this->lock->read_lock(this->lock);
111 return enumerator_create_cleaner(this->conns->create_enumerator(this->conns),
112 (void*)this->lock->unlock, this->lock);
113 }
114
115 /**
116 * Enumerator filter function for ike configs
117 */
118 static bool ike_filter(void *data, peer_cfg_t **in, ike_cfg_t **out)
119 {
120 *out = (*in)->get_ike_cfg(*in);
121 return TRUE;
122 }
123
124 METHOD(backend_t, create_ike_cfg_enumerator, enumerator_t*,
125 private_vici_config_t *this, host_t *me, host_t *other)
126 {
127 this->lock->read_lock(this->lock);
128 return enumerator_create_filter(this->conns->create_enumerator(this->conns),
129 (void*)ike_filter, this->lock,
130 (void*)this->lock->unlock);
131 }
132
133 METHOD(backend_t, get_peer_cfg_by_name, peer_cfg_t*,
134 private_vici_config_t *this, char *name)
135 {
136 peer_cfg_t *current, *found = NULL;
137 enumerator_t *enumerator;
138
139 this->lock->read_lock(this->lock);
140 enumerator = this->conns->create_enumerator(this->conns);
141 while (enumerator->enumerate(enumerator, &current))
142 {
143 if (streq(current->get_name(current), name))
144 {
145 found = current;
146 found->get_ref(found);
147 break;
148 }
149 }
150 enumerator->destroy(enumerator);
151 this->lock->unlock(this->lock);
152
153 return found;
154 }
155
156 /**
157 * Create a (error) reply message
158 */
159 static vici_message_t* create_reply(char *fmt, ...)
160 {
161 vici_builder_t *builder;
162 va_list args;
163
164 builder = vici_builder_create();
165 builder->add_kv(builder, "success", fmt ? "no" : "yes");
166 if (fmt)
167 {
168 va_start(args, fmt);
169 builder->vadd_kv(builder, "errmsg", fmt, args);
170 va_end(args);
171 }
172 return builder->finalize(builder);
173 }
174
175 /**
176 * A rule to parse a key/value or list item
177 */
178 typedef struct {
179 /** name of the key/value or list */
180 char *name;
181 /** function to parse value */
182 bool (*parse)(void *out, chunk_t value);
183 /** result, passed to parse() */
184 void *out;
185 } parse_rule_t;
186
187 /**
188 * Parse key/values using a rule-set
189 */
190 static bool parse_rules(parse_rule_t *rules, int count, char *name,
191 chunk_t value, vici_message_t **reply)
192 {
193 int i;
194
195 for (i = 0; i < count; i++)
196 {
197 if (streq(name, rules[i].name))
198 {
199 if (rules[i].parse(rules[i].out, value))
200 {
201 return TRUE;
202 }
203 *reply = create_reply("invalid value for: %s, config discarded",
204 name);
205 return FALSE;
206 }
207 }
208 *reply = create_reply("unknown option: %s, config discarded", name);
209 return FALSE;
210 }
211
212 /**
213 * Parse callback data, passed to each callback
214 */
215 typedef struct {
216 private_vici_config_t *this;
217 vici_message_t *reply;
218 } request_data_t;
219
220 /**
221 * Data associated to a peer config
222 */
223 typedef struct {
224 request_data_t *request;
225 u_int32_t version;
226 bool aggressive;
227 bool encap;
228 bool mobike;
229 bool send_certreq;
230 bool pull;
231 cert_policy_t send_cert;
232 u_int64_t dpd_delay;
233 u_int64_t dpd_timeout;
234 fragmentation_t fragmentation;
235 unique_policy_t unique;
236 u_int32_t keyingtries;
237 u_int32_t local_port;
238 u_int32_t remote_port;
239 char *local_addrs;
240 char *remote_addrs;
241 linked_list_t *local;
242 linked_list_t *remote;
243 linked_list_t *proposals;
244 linked_list_t *children;
245 linked_list_t *vips;
246 char *pools;
247 u_int64_t reauth_time;
248 u_int64_t rekey_time;
249 u_int64_t over_time;
250 u_int64_t rand_time;
251 } peer_data_t;
252
253 /**
254 * Log relevant auth config data
255 */
256 static void log_auth(auth_cfg_t *auth)
257 {
258 enumerator_t *enumerator;
259 auth_rule_t rule;
260 union {
261 uintptr_t u;
262 identification_t *id;
263 char *str;
264 } v;
265
266 enumerator = auth->create_enumerator(auth);
267 while (enumerator->enumerate(enumerator, &rule, &v))
268 {
269 switch (rule)
270 {
271 case AUTH_RULE_AUTH_CLASS:
272 DBG2(DBG_CFG, " class = %N", auth_class_names, v.u);
273 break;
274 case AUTH_RULE_EAP_TYPE:
275 DBG2(DBG_CFG, " eap-type = %N", eap_type_names, v.u);
276 break;
277 case AUTH_RULE_EAP_VENDOR:
278 DBG2(DBG_CFG, " eap-vendor = %u", v.u);
279 break;
280 case AUTH_RULE_XAUTH_BACKEND:
281 DBG2(DBG_CFG, " xauth = %s", v.str);
282 break;
283 case AUTH_RULE_CRL_VALIDATION:
284 DBG2(DBG_CFG, " revocation = %N", cert_validation_names, v.u);
285 break;
286 case AUTH_RULE_IDENTITY:
287 DBG2(DBG_CFG, " id = %Y", v.id);
288 break;
289 case AUTH_RULE_AAA_IDENTITY:
290 DBG2(DBG_CFG, " aaa_id = %Y", v.id);
291 break;
292 case AUTH_RULE_EAP_IDENTITY:
293 DBG2(DBG_CFG, " eap_id = %Y", v.id);
294 break;
295 case AUTH_RULE_XAUTH_IDENTITY:
296 DBG2(DBG_CFG, " xauth_id = %Y", v.id);
297 break;
298 case AUTH_RULE_GROUP:
299 DBG2(DBG_CFG, " group = %Y", v.id);
300 break;
301 default:
302 break;
303 }
304 }
305 enumerator->destroy(enumerator);
306 }
307
308 /**
309 * Log parsed peer data
310 */
311 static void log_peer_data(peer_data_t *data)
312 {
313 enumerator_t *enumerator;
314 auth_cfg_t *auth;
315 host_t *host;
316
317 DBG2(DBG_CFG, " version = %u", data->version);
318 DBG2(DBG_CFG, " local_addrs = %s", data->local_addrs);
319 DBG2(DBG_CFG, " remote_addrs = %s", data->remote_addrs);
320 DBG2(DBG_CFG, " local_port = %u", data->local_port);
321 DBG2(DBG_CFG, " remote_port = %u", data->remote_port);
322 DBG2(DBG_CFG, " send_certreq = %u", data->send_certreq);
323 DBG2(DBG_CFG, " send_cert = %N", cert_policy_names, data->send_cert);
324 DBG2(DBG_CFG, " mobike = %u", data->mobike);
325 DBG2(DBG_CFG, " aggressive = %u", data->aggressive);
326 DBG2(DBG_CFG, " encap = %u", data->encap);
327 DBG2(DBG_CFG, " dpd_delay = %llu", data->dpd_delay);
328 DBG2(DBG_CFG, " dpd_timeout = %llu", data->dpd_timeout);
329 DBG2(DBG_CFG, " fragmentation = %u", data->fragmentation);
330 DBG2(DBG_CFG, " unique = %N", unique_policy_names, data->unique);
331 DBG2(DBG_CFG, " keyingtries = %u", data->keyingtries);
332 DBG2(DBG_CFG, " reauth_time = %llu", data->reauth_time);
333 DBG2(DBG_CFG, " rekey_time = %llu", data->rekey_time);
334 DBG2(DBG_CFG, " over_time = %llu", data->over_time);
335 DBG2(DBG_CFG, " rand_time = %llu", data->rand_time);
336 DBG2(DBG_CFG, " proposals = %#P", data->proposals);
337
338 if (data->vips->get_count(data->vips))
339 {
340 DBG2(DBG_CFG, " vips:");
341 }
342 enumerator = data->vips->create_enumerator(data->vips);
343 while (enumerator->enumerate(enumerator, &host))
344 {
345 DBG2(DBG_CFG, " %H", host);
346 }
347 enumerator->destroy(enumerator);
348
349 enumerator = data->local->create_enumerator(data->local);
350 while (enumerator->enumerate(enumerator, &auth))
351 {
352 DBG2(DBG_CFG, " local:");
353 log_auth(auth);
354 }
355 enumerator->destroy(enumerator);
356
357 enumerator = data->remote->create_enumerator(data->remote);
358 while (enumerator->enumerate(enumerator, &auth))
359 {
360 DBG2(DBG_CFG, " remote:");
361 log_auth(auth);
362 }
363 enumerator->destroy(enumerator);
364 }
365
366 /**
367 * Clean up peer config data
368 */
369 static void free_peer_data(peer_data_t *data)
370 {
371 data->local->destroy_offset(data->local,
372 offsetof(auth_cfg_t, destroy));
373 data->remote->destroy_offset(data->remote,
374 offsetof(auth_cfg_t, destroy));
375 data->children->destroy_offset(data->children,
376 offsetof(child_cfg_t, destroy));
377 data->proposals->destroy_offset(data->proposals,
378 offsetof(proposal_t, destroy));
379 data->vips->destroy_offset(data->vips, offsetof(host_t, destroy));
380 free(data->pools);
381 free(data->local_addrs);
382 free(data->remote_addrs);
383 }
384
385 /**
386 * CHILD config data
387 */
388 typedef struct {
389 request_data_t *request;
390 lifetime_cfg_t lft;
391 char* updown;
392 bool hostaccess;
393 bool ipcomp;
394 bool policies;
395 ipsec_mode_t mode;
396 u_int32_t replay_window;
397 action_t dpd_action;
398 action_t start_action;
399 action_t close_action;
400 u_int32_t reqid;
401 u_int32_t tfc;
402 mark_t mark_in;
403 mark_t mark_out;
404 u_int64_t inactivity;
405 linked_list_t *proposals;
406 linked_list_t *local_ts;
407 linked_list_t *remote_ts;
408 } child_data_t;
409
410 /**
411 * Log parsed CHILD config data
412 */
413 static void log_child_data(child_data_t *data, char *name)
414 {
415 DBG2(DBG_CFG, " child %s:", name);
416 DBG2(DBG_CFG, " rekey_time = %llu", data->lft.time.rekey);
417 DBG2(DBG_CFG, " life_time = %llu", data->lft.time.life);
418 DBG2(DBG_CFG, " rand_time = %llu", data->lft.time.jitter);
419 DBG2(DBG_CFG, " rekey_bytes = %llu", data->lft.bytes.rekey);
420 DBG2(DBG_CFG, " life_bytes = %llu", data->lft.bytes.life);
421 DBG2(DBG_CFG, " rand_bytes = %llu", data->lft.bytes.jitter);
422 DBG2(DBG_CFG, " rekey_packets = %llu", data->lft.packets.rekey);
423 DBG2(DBG_CFG, " life_packets = %llu", data->lft.packets.life);
424 DBG2(DBG_CFG, " rand_packets = %llu", data->lft.packets.jitter);
425 DBG2(DBG_CFG, " updown = %s", data->updown);
426 DBG2(DBG_CFG, " hostaccess = %u", data->hostaccess);
427 DBG2(DBG_CFG, " ipcomp = %u", data->ipcomp);
428 DBG2(DBG_CFG, " mode = %N", ipsec_mode_names, data->mode);
429 DBG2(DBG_CFG, " policies = %u", data->policies);
430 if (data->replay_window != REPLAY_UNDEFINED)
431 {
432 DBG2(DBG_CFG, " replay_window = %u", data->replay_window);
433 }
434 DBG2(DBG_CFG, " dpd_action = %N", action_names, data->dpd_action);
435 DBG2(DBG_CFG, " start_action = %N", action_names, data->start_action);
436 DBG2(DBG_CFG, " close_action = %N", action_names, data->close_action);
437 DBG2(DBG_CFG, " reqid = %u", data->reqid);
438 DBG2(DBG_CFG, " tfc = %d", data->tfc);
439 DBG2(DBG_CFG, " mark_in = %u/%u",
440 data->mark_in.value, data->mark_in.mask);
441 DBG2(DBG_CFG, " mark_out = %u/%u",
442 data->mark_out.value, data->mark_out.mask);
443 DBG2(DBG_CFG, " inactivity = %llu", data->inactivity);
444 DBG2(DBG_CFG, " proposals = %#P", data->proposals);
445 DBG2(DBG_CFG, " local_ts = %#R", data->local_ts);
446 DBG2(DBG_CFG, " remote_ts = %#R", data->remote_ts);
447 }
448
449 /**
450 * Clean up CHILD config data
451 */
452 static void free_child_data(child_data_t *data)
453 {
454 data->proposals->destroy_offset(data->proposals,
455 offsetof(proposal_t, destroy));
456 data->local_ts->destroy_offset(data->local_ts,
457 offsetof(traffic_selector_t, destroy));
458 data->remote_ts->destroy_offset(data->remote_ts,
459 offsetof(traffic_selector_t, destroy));
460 free(data->updown);
461 }
462
463 /**
464 * Auth config data
465 */
466 typedef struct {
467 request_data_t *request;
468 auth_cfg_t *cfg;
469 } auth_data_t;
470
471 /**
472 * Common proposal parsing
473 */
474 static bool parse_proposal(linked_list_t *list, protocol_id_t proto, chunk_t v)
475 {
476 char buf[128];
477 proposal_t *proposal;
478
479 if (!vici_stringify(v, buf, sizeof(buf)))
480 {
481 return FALSE;
482 }
483 if (strcaseeq("default", buf))
484 {
485 proposal = proposal_create_default(proto);
486 if (proposal)
487 {
488 list->insert_last(list, proposal);
489 }
490 proposal = proposal_create_default_aead(proto);
491 if (proposal)
492 {
493 list->insert_last(list, proposal);
494 }
495 return TRUE;
496 }
497 proposal = proposal_create_from_string(proto, buf);
498 if (proposal)
499 {
500 list->insert_last(list, proposal);
501 return TRUE;
502 }
503 return FALSE;
504 }
505
506 /**
507 * Parse IKE proposal
508 */
509 CALLBACK(parse_ike_proposal, bool,
510 linked_list_t *out, chunk_t v)
511 {
512 return parse_proposal(out, PROTO_IKE, v);
513 }
514
515 /**
516 * Parse ESP proposal
517 */
518 CALLBACK(parse_esp_proposal, bool,
519 linked_list_t *out, chunk_t v)
520 {
521 return parse_proposal(out, PROTO_ESP, v);
522 }
523
524 /**
525 * Parse AH proposal
526 */
527 CALLBACK(parse_ah_proposal, bool,
528 linked_list_t *out, chunk_t v)
529 {
530 return parse_proposal(out, PROTO_AH, v);
531 }
532
533 /**
534 * Parse a traffic selector
535 */
536 CALLBACK(parse_ts, bool,
537 linked_list_t *out, chunk_t v)
538 {
539 char buf[128], *protoport, *sep, *port = "", *end;
540 traffic_selector_t *ts;
541 struct protoent *protoent;
542 struct servent *svc;
543 long int p;
544 u_int16_t from = 0, to = 0xffff;
545 u_int8_t proto = 0;
546
547 if (!vici_stringify(v, buf, sizeof(buf)))
548 {
549 return FALSE;
550 }
551
552 protoport = strchr(buf, '[');
553 if (protoport)
554 {
555 *(protoport++) = '\0';
556
557 sep = strrchr(protoport, ']');
558 if (!sep)
559 {
560 return FALSE;
561 }
562 *sep = '\0';
563
564 sep = strchr(protoport, '/');
565 if (sep)
566 { /* protocol/port */
567 *sep = '\0';
568 port = sep + 1;
569 }
570
571 if (streq(protoport, "any"))
572 {
573 proto = 0;
574 }
575 else
576 {
577 protoent = getprotobyname(protoport);
578 if (protoent)
579 {
580 proto = protoent->p_proto;
581 }
582 else
583 {
584 p = strtol(protoport, &end, 0);
585 if ((*protoport && *end) || p < 0 || p > 0xff)
586 {
587 return FALSE;
588 }
589 proto = (u_int8_t)p;
590 }
591 }
592 if (streq(port, "opaque"))
593 {
594 from = 0xffff;
595 to = 0;
596 }
597 else if (*port && !streq(port, "any"))
598 {
599 svc = getservbyname(port, NULL);
600 if (svc)
601 {
602 from = to = ntohs(svc->s_port);
603 }
604 else
605 {
606 p = strtol(port, &end, 0);
607 if (p < 0 || p > 0xffff)
608 {
609 return FALSE;
610 }
611 from = p;
612 if (*end == '-')
613 {
614 port = end + 1;
615 p = strtol(port, &end, 0);
616 if (p < 0 || p > 0xffff)
617 {
618 return FALSE;
619 }
620 }
621 to = p;
622 if (*end)
623 {
624 return FALSE;
625 }
626 }
627 }
628 }
629 if (streq(buf, "dynamic"))
630 {
631 ts = traffic_selector_create_dynamic(proto, from, to);
632 }
633 else
634 {
635 ts = traffic_selector_create_from_cidr(buf, proto, from, to);
636 }
637 if (!ts)
638 {
639 return FALSE;
640 }
641 out->insert_last(out, ts);
642 return TRUE;
643 }
644
645 /**
646 * Parse a string
647 */
648 CALLBACK(parse_string, bool,
649 char **out, chunk_t v)
650 {
651 if (!chunk_printable(v, NULL, ' '))
652 {
653 return FALSE;
654 }
655 free(*out);
656 *out = NULL;
657 if (asprintf(out, "%.*s", (int)v.len, v.ptr) == -1)
658 {
659 return FALSE;
660 }
661 return TRUE;
662 }
663
664 /**
665 * Map a string to an integer
666 */
667 typedef struct {
668 char *str;
669 int d;
670 } enum_map_t;
671
672 /**
673 * Parse a string to an integer mapping
674 */
675 static bool parse_map(enum_map_t *map, int count, int *out, chunk_t v)
676 {
677 char buf[128];
678 int i;
679
680 if (!vici_stringify(v, buf, sizeof(buf)))
681 {
682 return FALSE;
683 }
684 for (i = 0; i < count; i++)
685 {
686 if (strcaseeq(map[i].str, buf))
687 {
688 *out = map[i].d;
689 return TRUE;
690 }
691 }
692 return FALSE;
693 }
694
695 /**
696 * Parse a boolean
697 */
698 CALLBACK(parse_bool, bool,
699 bool *out, chunk_t v)
700 {
701 enum_map_t map[] = {
702 { "yes", TRUE },
703 { "true", TRUE },
704 { "enabled", TRUE },
705 { "1", TRUE },
706 { "no", FALSE },
707 { "false", FALSE },
708 { "disabled", FALSE },
709 { "0", FALSE },
710 };
711 int d;
712
713 if (parse_map(map, countof(map), &d, v))
714 {
715 *out = d;
716 return TRUE;
717 }
718 return FALSE;
719 }
720
721 /**
722 * Parse a ipsec_mode_t
723 */
724 CALLBACK(parse_mode, bool,
725 ipsec_mode_t *out, chunk_t v)
726 {
727 enum_map_t map[] = {
728 { "tunnel", MODE_TUNNEL },
729 { "transport", MODE_TRANSPORT },
730 { "beet", MODE_BEET },
731 { "drop", MODE_DROP },
732 { "pass", MODE_PASS },
733 };
734 int d;
735
736 if (parse_map(map, countof(map), &d, v))
737 {
738 *out = d;
739 return TRUE;
740 }
741 return FALSE;
742 }
743
744 /**
745 * Parse an action_t
746 */
747 CALLBACK(parse_action, bool,
748 action_t *out, chunk_t v)
749 {
750 enum_map_t map[] = {
751 { "start", ACTION_RESTART },
752 { "restart", ACTION_RESTART },
753 { "route", ACTION_ROUTE },
754 { "trap", ACTION_ROUTE },
755 { "none", ACTION_NONE },
756 { "clear", ACTION_NONE },
757 };
758 int d;
759
760 if (parse_map(map, countof(map), &d, v))
761 {
762 *out = d;
763 return TRUE;
764 }
765 return FALSE;
766 }
767
768 /**
769 * Parse a u_int32_t
770 */
771 CALLBACK(parse_uint32, bool,
772 u_int32_t *out, chunk_t v)
773 {
774 char buf[16], *end;
775 u_long l;
776
777 if (!vici_stringify(v, buf, sizeof(buf)))
778 {
779 return FALSE;
780 }
781 l = strtoul(buf, &end, 0);
782 if (*end == 0)
783 {
784 *out = l;
785 return TRUE;
786 }
787 return FALSE;
788 }
789
790 /**
791 * Parse a u_int64_t
792 */
793 CALLBACK(parse_uint64, bool,
794 u_int64_t *out, chunk_t v)
795 {
796 char buf[16], *end;
797 unsigned long long l;
798
799 if (!vici_stringify(v, buf, sizeof(buf)))
800 {
801 return FALSE;
802 }
803 l = strtoull(buf, &end, 0);
804 if (*end == 0)
805 {
806 *out = l;
807 return TRUE;
808 }
809 return FALSE;
810 }
811
812 /**
813 * Parse a relative time
814 */
815 CALLBACK(parse_time, bool,
816 u_int64_t *out, chunk_t v)
817 {
818 char buf[16], *end;
819 u_long l;
820
821 if (!vici_stringify(v, buf, sizeof(buf)))
822 {
823 return FALSE;
824 }
825
826 l = strtoul(buf, &end, 0);
827 while (*end == ' ')
828 {
829 end++;
830 }
831 switch (*end)
832 {
833 case 'd':
834 case 'D':
835 l *= 24;
836 /* fall */
837 case 'h':
838 case 'H':
839 l *= 60;
840 /* fall */
841 case 'm':
842 case 'M':
843 l *= 60;
844 /* fall */
845 case 's':
846 case 'S':
847 end++;
848 break;
849 case '\0':
850 break;
851 default:
852 return FALSE;
853 }
854 if (*end)
855 {
856 return FALSE;
857 }
858 *out = l;
859 return TRUE;
860 }
861
862 /**
863 * Parse byte volume
864 */
865 CALLBACK(parse_bytes, bool,
866 u_int64_t *out, chunk_t v)
867 {
868 char buf[16], *end;
869 unsigned long long l;
870
871 if (!vici_stringify(v, buf, sizeof(buf)))
872 {
873 return FALSE;
874 }
875
876 l = strtoull(buf, &end, 0);
877 while (*end == ' ')
878 {
879 end++;
880 }
881 switch (*end)
882 {
883 case 'g':
884 case 'G':
885 l *= 1024;
886 /* fall */
887 case 'm':
888 case 'M':
889 l *= 1024;
890 /* fall */
891 case 'k':
892 case 'K':
893 l *= 1024;
894 end++;
895 break;
896 case '\0':
897 break;
898 default:
899 return FALSE;
900 }
901 if (*end)
902 {
903 return FALSE;
904 }
905 *out = l;
906 return TRUE;
907 }
908
909 /**
910 * Parse a mark_t
911 */
912 CALLBACK(parse_mark, bool,
913 mark_t *out, chunk_t v)
914 {
915 char buf[32];
916
917 if (!vici_stringify(v, buf, sizeof(buf)))
918 {
919 return FALSE;
920 }
921 return mark_from_string(buf, out);
922 }
923
924 /**
925 * Parse TFC padding option
926 */
927 CALLBACK(parse_tfc, bool,
928 u_int32_t *out, chunk_t v)
929 {
930 if (chunk_equals(v, chunk_from_str("mtu")))
931 {
932 *out = -1;
933 return TRUE;
934 }
935 return parse_uint32(out, v);
936 }
937
938 /**
939 * Parse authentication config
940 */
941 CALLBACK(parse_auth, bool,
942 auth_cfg_t *cfg, chunk_t v)
943 {
944 char buf[64], *pos;
945 eap_vendor_type_t *type;
946
947 if (!vici_stringify(v, buf, sizeof(buf)))
948 {
949 return FALSE;
950 }
951 if (strcaseeq(buf, "pubkey"))
952 {
953 cfg->add(cfg, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PUBKEY);
954 return TRUE;
955 }
956 if (strcaseeq(buf, "psk"))
957 {
958 cfg->add(cfg, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_PSK);
959 return TRUE;
960 }
961 if (strcasepfx(buf, "xauth"))
962 {
963 pos = strchr(buf, '-');
964 if (pos)
965 {
966 cfg->add(cfg, AUTH_RULE_XAUTH_BACKEND, strdup(++pos));
967 }
968 cfg->add(cfg, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_XAUTH);
969 return TRUE;
970 }
971 if (strcasepfx(buf, "eap"))
972 {
973 cfg->add(cfg, AUTH_RULE_AUTH_CLASS, AUTH_CLASS_EAP);
974
975 type = eap_vendor_type_from_string(buf);
976 if (type)
977 {
978 cfg->add(cfg, AUTH_RULE_EAP_TYPE, type->type);
979 if (type->vendor)
980 {
981 cfg->add(cfg, AUTH_RULE_EAP_VENDOR, type->vendor);
982 }
983 free(type);
984 }
985 return TRUE;
986 }
987 return FALSE;
988 }
989
990 /**
991 * Parse identity; add as auth rule to config
992 */
993 static bool parse_id(auth_cfg_t *cfg, auth_rule_t rule, chunk_t v)
994 {
995 char buf[256];
996
997 if (!vici_stringify(v, buf, sizeof(buf)))
998 {
999 return FALSE;
1000 }
1001 cfg->add(cfg, rule, identification_create_from_string(buf));
1002 return TRUE;
1003 }
1004
1005 /**
1006 * Parse IKE identity
1007 */
1008 CALLBACK(parse_ike_id, bool,
1009 auth_cfg_t *cfg, chunk_t v)
1010 {
1011 return parse_id(cfg, AUTH_RULE_IDENTITY, v);
1012 }
1013
1014 /**
1015 * Parse AAA identity
1016 */
1017 CALLBACK(parse_aaa_id, bool,
1018 auth_cfg_t *cfg, chunk_t v)
1019 {
1020 return parse_id(cfg, AUTH_RULE_AAA_IDENTITY, v);
1021 }
1022
1023 /**
1024 * Parse EAP identity
1025 */
1026 CALLBACK(parse_eap_id, bool,
1027 auth_cfg_t *cfg, chunk_t v)
1028 {
1029 return parse_id(cfg, AUTH_RULE_EAP_IDENTITY, v);
1030 }
1031
1032 /**
1033 * Parse XAuth identity
1034 */
1035 CALLBACK(parse_xauth_id, bool,
1036 auth_cfg_t *cfg, chunk_t v)
1037 {
1038 return parse_id(cfg, AUTH_RULE_XAUTH_IDENTITY, v);
1039 }
1040
1041 /**
1042 * Parse group membership
1043 */
1044 CALLBACK(parse_group, bool,
1045 auth_cfg_t *cfg, chunk_t v)
1046 {
1047 return parse_id(cfg, AUTH_RULE_GROUP, v);
1048 }
1049
1050 /**
1051 * Parse a certificate; add as auth rule to config
1052 */
1053 static bool parse_cert(auth_data_t *auth, auth_rule_t rule, chunk_t v)
1054 {
1055 vici_authority_t *authority;
1056 certificate_t *cert;
1057
1058 cert = lib->creds->create(lib->creds, CRED_CERTIFICATE, CERT_X509,
1059 BUILD_BLOB_PEM, v, BUILD_END);
1060 if (cert)
1061 {
1062 if (rule == AUTH_RULE_SUBJECT_CERT)
1063 {
1064 authority = auth->request->this->authority;
1065 authority->check_for_hash_and_url(authority, cert);
1066 }
1067 auth->cfg->add(auth->cfg, rule, cert);
1068 return TRUE;
1069 }
1070 return FALSE;
1071 }
1072
1073 /**
1074 * Parse subject certificates
1075 */
1076 CALLBACK(parse_certs, bool,
1077 auth_data_t *auth, chunk_t v)
1078 {
1079 return parse_cert(auth, AUTH_RULE_SUBJECT_CERT, v);
1080 }
1081
1082 /**
1083 * Parse CA certificates
1084 */
1085 CALLBACK(parse_cacerts, bool,
1086 auth_data_t *auth, chunk_t v)
1087 {
1088 return parse_cert(auth, AUTH_RULE_CA_CERT, v);
1089 }
1090
1091 /**
1092 * Parse revocation status
1093 */
1094 CALLBACK(parse_revocation, bool,
1095 auth_cfg_t *cfg, chunk_t v)
1096 {
1097 enum_map_t map[] = {
1098 { "strict", VALIDATION_GOOD },
1099 { "ifuri", VALIDATION_SKIPPED },
1100 { "relaxed", VALIDATION_FAILED },
1101 };
1102 int d;
1103
1104 if (parse_map(map, countof(map), &d, v))
1105 {
1106 if (d != VALIDATION_FAILED)
1107 {
1108 cfg->add(cfg, AUTH_RULE_CRL_VALIDATION, d);
1109 }
1110 return TRUE;
1111 }
1112 return FALSE;
1113 }
1114
1115 /**
1116 * Parse list items to comma separated strings
1117 */
1118 CALLBACK(parse_stringlist, bool,
1119 char **out, chunk_t v)
1120 {
1121 char *current;
1122
1123 if (!chunk_printable(v, NULL, ' '))
1124 {
1125 return FALSE;
1126 }
1127 current = *out;
1128 if (current)
1129 {
1130 if (asprintf(out, "%s, %.*s", current, (int)v.len, v.ptr) == -1)
1131 {
1132 return FALSE;
1133 }
1134 free(current);
1135 }
1136 else
1137 {
1138 if (asprintf(out, "%.*s", (int)v.len, v.ptr) == -1)
1139 {
1140 return FALSE;
1141 }
1142 }
1143 return TRUE;
1144 }
1145
1146 /**
1147 * Parse an fragmentation_t
1148 */
1149 CALLBACK(parse_frag, bool,
1150 fragmentation_t *out, chunk_t v)
1151 {
1152 enum_map_t map[] = {
1153 { "yes", FRAGMENTATION_YES },
1154 { "no", FRAGMENTATION_NO },
1155 { "force", FRAGMENTATION_FORCE },
1156 };
1157 int d;
1158
1159 if (parse_map(map, countof(map), &d, v))
1160 {
1161 *out = d;
1162 return TRUE;
1163 }
1164 return FALSE;
1165 }
1166
1167 /**
1168 * Parse a cert_policy_t
1169 */
1170 CALLBACK(parse_send_cert, bool,
1171 cert_policy_t *out, chunk_t v)
1172 {
1173 enum_map_t map[] = {
1174 { "ifasked", CERT_SEND_IF_ASKED },
1175 { "always", CERT_ALWAYS_SEND },
1176 { "never", CERT_NEVER_SEND },
1177 };
1178 int d;
1179
1180 if (parse_map(map, countof(map), &d, v))
1181 {
1182 *out = d;
1183 return TRUE;
1184 }
1185 return FALSE;
1186 }
1187
1188 /**
1189 * Parse a unique_policy_t
1190 */
1191 CALLBACK(parse_unique, bool,
1192 unique_policy_t *out, chunk_t v)
1193 {
1194 enum_map_t map[] = {
1195 { "never", UNIQUE_NEVER },
1196 { "no", UNIQUE_NO },
1197 { "replace", UNIQUE_REPLACE },
1198 { "keep", UNIQUE_KEEP },
1199 };
1200 int d;
1201
1202 if (parse_map(map, countof(map), &d, v))
1203 {
1204 *out = d;
1205 return TRUE;
1206 }
1207 return FALSE;
1208 }
1209
1210 /**
1211 * Parse host_t into a list
1212 */
1213 CALLBACK(parse_hosts, bool,
1214 linked_list_t *list, chunk_t v)
1215 {
1216 char buf[64];
1217 host_t *host;
1218
1219 if (!vici_stringify(v, buf, sizeof(buf)))
1220 {
1221 return FALSE;
1222 }
1223 host = host_create_from_string(buf, 0);
1224 if (!host)
1225 {
1226 return FALSE;
1227 }
1228 list->insert_last(list, host);
1229 return TRUE;
1230 }
1231
1232 CALLBACK(child_li, bool,
1233 child_data_t *child, vici_message_t *message, char *name, chunk_t value)
1234 {
1235 parse_rule_t rules[] = {
1236 { "ah_proposals", parse_ah_proposal, child->proposals },
1237 { "esp_proposals", parse_esp_proposal, child->proposals },
1238 { "local_ts", parse_ts, child->local_ts },
1239 { "remote_ts", parse_ts, child->remote_ts },
1240 };
1241
1242 return parse_rules(rules, countof(rules), name, value,
1243 &child->request->reply);
1244 }
1245
1246 CALLBACK(child_kv, bool,
1247 child_data_t *child, vici_message_t *message, char *name, chunk_t value)
1248 {
1249 parse_rule_t rules[] = {
1250 { "updown", parse_string, &child->updown },
1251 { "hostaccess", parse_bool, &child->hostaccess },
1252 { "mode", parse_mode, &child->mode },
1253 { "policies", parse_bool, &child->policies },
1254 { "replay_window", parse_uint32, &child->replay_window },
1255 { "rekey_time", parse_time, &child->lft.time.rekey },
1256 { "life_time", parse_time, &child->lft.time.life },
1257 { "rand_time", parse_time, &child->lft.time.jitter },
1258 { "rekey_bytes", parse_bytes, &child->lft.bytes.rekey },
1259 { "life_bytes", parse_bytes, &child->lft.bytes.life },
1260 { "rand_bytes", parse_bytes, &child->lft.bytes.jitter },
1261 { "rekey_packets", parse_uint64, &child->lft.packets.rekey },
1262 { "life_packets", parse_uint64, &child->lft.packets.life },
1263 { "rand_packets", parse_uint64, &child->lft.packets.jitter },
1264 { "dpd_action", parse_action, &child->dpd_action },
1265 { "start_action", parse_action, &child->start_action },
1266 { "close_action", parse_action, &child->close_action },
1267 { "ipcomp", parse_bool, &child->ipcomp },
1268 { "inactivity", parse_time, &child->inactivity },
1269 { "reqid", parse_uint32, &child->reqid },
1270 { "mark_in", parse_mark, &child->mark_in },
1271 { "mark_out", parse_mark, &child->mark_out },
1272 { "tfc_padding", parse_tfc, &child->tfc },
1273 };
1274
1275 return parse_rules(rules, countof(rules), name, value,
1276 &child->request->reply);
1277 }
1278
1279 CALLBACK(auth_li, bool,
1280 auth_data_t *auth, vici_message_t *message, char *name, chunk_t value)
1281 {
1282 parse_rule_t rules[] = {
1283 { "groups", parse_group, auth->cfg },
1284 { "certs", parse_certs, auth },
1285 { "cacerts", parse_cacerts, auth },
1286 };
1287
1288 return parse_rules(rules, countof(rules), name, value,
1289 &auth->request->reply);
1290 }
1291
1292 CALLBACK(auth_kv, bool,
1293 auth_data_t *auth, vici_message_t *message, char *name, chunk_t value)
1294 {
1295 parse_rule_t rules[] = {
1296 { "auth", parse_auth, auth->cfg },
1297 { "id", parse_ike_id, auth->cfg },
1298 { "aaa_id", parse_aaa_id, auth->cfg },
1299 { "eap_id", parse_eap_id, auth->cfg },
1300 { "xauth_id", parse_xauth_id, auth->cfg },
1301 { "revocation", parse_revocation, auth->cfg },
1302 };
1303
1304 return parse_rules(rules, countof(rules), name, value,
1305 &auth->request->reply);
1306 }
1307
1308 CALLBACK(peer_li, bool,
1309 peer_data_t *peer, vici_message_t *message, char *name, chunk_t value)
1310 {
1311 parse_rule_t rules[] = {
1312 { "local_addrs", parse_stringlist, &peer->local_addrs },
1313 { "remote_addrs", parse_stringlist, &peer->remote_addrs },
1314 { "proposals", parse_ike_proposal, peer->proposals },
1315 { "vips", parse_hosts, peer->vips },
1316 { "pools", parse_stringlist, &peer->pools },
1317 };
1318
1319 return parse_rules(rules, countof(rules), name, value,
1320 &peer->request->reply);
1321 }
1322
1323 CALLBACK(peer_kv, bool,
1324 peer_data_t *peer, vici_message_t *message, char *name, chunk_t value)
1325 {
1326 parse_rule_t rules[] = {
1327 { "version", parse_uint32, &peer->version },
1328 { "aggressive", parse_bool, &peer->aggressive },
1329 { "pull", parse_bool, &peer->pull },
1330 { "encap", parse_bool, &peer->encap },
1331 { "mobike", parse_bool, &peer->mobike },
1332 { "dpd_delay", parse_time, &peer->dpd_delay },
1333 { "dpd_timeout", parse_time, &peer->dpd_timeout },
1334 { "fragmentation", parse_frag, &peer->fragmentation },
1335 { "send_certreq", parse_bool, &peer->send_certreq },
1336 { "send_cert", parse_send_cert, &peer->send_cert },
1337 { "keyingtries", parse_uint32, &peer->keyingtries },
1338 { "unique", parse_unique, &peer->unique },
1339 { "local_port", parse_uint32, &peer->local_port },
1340 { "remote_port", parse_uint32, &peer->remote_port },
1341 { "reauth_time", parse_time, &peer->reauth_time },
1342 { "rekey_time", parse_time, &peer->rekey_time },
1343 { "over_time", parse_time, &peer->over_time },
1344 { "rand_time", parse_time, &peer->rand_time },
1345 };
1346
1347 return parse_rules(rules, countof(rules), name, value,
1348 &peer->request->reply);
1349 }
1350
1351 CALLBACK(children_sn, bool,
1352 peer_data_t *peer, vici_message_t *message, vici_parse_context_t *ctx,
1353 char *name)
1354 {
1355 child_data_t child = {
1356 .request = peer->request,
1357 .proposals = linked_list_create(),
1358 .local_ts = linked_list_create(),
1359 .remote_ts = linked_list_create(),
1360 .mode = MODE_TUNNEL,
1361 .policies = TRUE,
1362 .replay_window = REPLAY_UNDEFINED,
1363 .dpd_action = ACTION_NONE,
1364 .start_action = ACTION_NONE,
1365 .close_action = ACTION_NONE,
1366 .lft = {
1367 .time = {
1368 .rekey = LFT_DEFAULT_CHILD_REKEY,
1369 .life = LFT_UNDEFINED,
1370 .jitter = LFT_UNDEFINED,
1371 },
1372 .bytes = {
1373 .rekey = LFT_UNDEFINED,
1374 .life = LFT_UNDEFINED,
1375 .jitter = LFT_UNDEFINED,
1376 },
1377 .packets = {
1378 .rekey = LFT_UNDEFINED,
1379 .life = LFT_UNDEFINED,
1380 .jitter = LFT_UNDEFINED,
1381 },
1382 }
1383 };
1384 child_cfg_t *cfg;
1385 proposal_t *proposal;
1386 traffic_selector_t *ts;
1387
1388 if (!message->parse(message, ctx, NULL, child_kv, child_li, &child))
1389 {
1390 free_child_data(&child);
1391 return FALSE;
1392 }
1393
1394 if (child.local_ts->get_count(child.local_ts) == 0)
1395 {
1396 child.local_ts->insert_last(child.local_ts,
1397 traffic_selector_create_dynamic(0, 0, 65535));
1398 }
1399 if (child.remote_ts->get_count(child.remote_ts) == 0)
1400 {
1401 child.remote_ts->insert_last(child.remote_ts,
1402 traffic_selector_create_dynamic(0, 0, 65535));
1403 }
1404 if (child.proposals->get_count(child.proposals) == 0)
1405 {
1406 proposal = proposal_create_default(PROTO_ESP);
1407 if (proposal)
1408 {
1409 child.proposals->insert_last(child.proposals, proposal);
1410 }
1411 proposal = proposal_create_default_aead(PROTO_ESP);
1412 if (proposal)
1413 {
1414 child.proposals->insert_last(child.proposals, proposal);
1415 }
1416 }
1417
1418 /* if no hard lifetime specified, add one at soft lifetime + 10% */
1419 if (child.lft.time.life == LFT_UNDEFINED)
1420 {
1421 child.lft.time.life = child.lft.time.rekey * 110 / 100;
1422 }
1423 if (child.lft.bytes.life == LFT_UNDEFINED)
1424 {
1425 child.lft.bytes.life = child.lft.bytes.rekey * 110 / 100;
1426 }
1427 if (child.lft.packets.life == LFT_UNDEFINED)
1428 {
1429 child.lft.packets.life = child.lft.packets.rekey * 110 / 100;
1430 }
1431 /* if no soft lifetime specified, add one at hard lifetime - 10% */
1432 if (child.lft.bytes.rekey == LFT_UNDEFINED)
1433 {
1434 child.lft.bytes.rekey = child.lft.bytes.life * 90 / 100;
1435 }
1436 if (child.lft.packets.rekey == LFT_UNDEFINED)
1437 {
1438 child.lft.packets.rekey = child.lft.packets.life * 90 / 100;
1439 }
1440 /* if no rand time defined, use difference of hard and soft */
1441 if (child.lft.time.jitter == LFT_UNDEFINED)
1442 {
1443 child.lft.time.jitter = child.lft.time.life -
1444 min(child.lft.time.life, child.lft.time.rekey);
1445 }
1446 if (child.lft.bytes.jitter == LFT_UNDEFINED)
1447 {
1448 child.lft.bytes.jitter = child.lft.bytes.life -
1449 min(child.lft.bytes.life, child.lft.bytes.rekey);
1450 }
1451 if (child.lft.packets.jitter == LFT_UNDEFINED)
1452 {
1453 child.lft.packets.jitter = child.lft.packets.life -
1454 min(child.lft.packets.life, child.lft.packets.rekey);
1455 }
1456
1457 log_child_data(&child, name);
1458
1459 cfg = child_cfg_create(name, &child.lft, child.updown,
1460 child.hostaccess, child.mode, child.start_action,
1461 child.dpd_action, child.close_action, child.ipcomp,
1462 child.inactivity, child.reqid, &child.mark_in,
1463 &child.mark_out, child.tfc);
1464
1465 cfg->set_mipv6_options(cfg, FALSE, child.policies);
1466
1467 if (child.replay_window != REPLAY_UNDEFINED)
1468 {
1469 cfg->set_replay_window(cfg, child.replay_window);
1470 }
1471 while (child.local_ts->remove_first(child.local_ts,
1472 (void**)&ts) == SUCCESS)
1473 {
1474 cfg->add_traffic_selector(cfg, TRUE, ts);
1475 }
1476 while (child.remote_ts->remove_first(child.remote_ts,
1477 (void**)&ts) == SUCCESS)
1478 {
1479 cfg->add_traffic_selector(cfg, FALSE, ts);
1480 }
1481 while (child.proposals->remove_first(child.proposals,
1482 (void**)&proposal) == SUCCESS)
1483 {
1484 cfg->add_proposal(cfg, proposal);
1485 }
1486
1487 peer->children->insert_last(peer->children, cfg);
1488
1489 free_child_data(&child);
1490
1491 return TRUE;
1492 }
1493
1494 CALLBACK(peer_sn, bool,
1495 peer_data_t *peer, vici_message_t *message, vici_parse_context_t *ctx,
1496 char *name)
1497 {
1498 if (strcaseeq(name, "children"))
1499 {
1500 return message->parse(message, ctx, children_sn, NULL, NULL, peer);
1501 }
1502 if (strcasepfx(name, "local") ||
1503 strcasepfx(name, "remote"))
1504 {
1505 auth_data_t auth = {
1506 .request = peer->request,
1507 .cfg = auth_cfg_create(),
1508 };
1509
1510 if (!message->parse(message, ctx, NULL, auth_kv, auth_li, &auth))
1511 {
1512 auth.cfg->destroy(auth.cfg);
1513 return FALSE;
1514 }
1515
1516 if (!auth.cfg->get(auth.cfg, AUTH_RULE_IDENTITY))
1517 {
1518 identification_t *id;
1519 certificate_t *cert;
1520
1521 cert = auth.cfg->get(auth.cfg, AUTH_RULE_SUBJECT_CERT);
1522 if (cert)
1523 {
1524 id = cert->get_subject(cert);
1525 DBG1(DBG_CFG, " id not specified, defaulting to cert id '%Y'",
1526 id);
1527 auth.cfg->add(auth.cfg, AUTH_RULE_IDENTITY, id->clone(id));
1528 }
1529 }
1530
1531 if (strcasepfx(name, "local"))
1532 {
1533 peer->local->insert_last(peer->local, auth.cfg);
1534 }
1535 else
1536 {
1537 peer->remote->insert_last(peer->remote, auth.cfg);
1538 }
1539 return TRUE;
1540 }
1541 peer->request->reply = create_reply("invalid section: %s", name);
1542 return FALSE;
1543 }
1544
1545 /**
1546 * Find reqid of an existing CHILD_SA
1547 */
1548 static u_int32_t find_reqid(child_cfg_t *cfg)
1549 {
1550 enumerator_t *enumerator, *children;
1551 child_sa_t *child_sa;
1552 ike_sa_t *ike_sa;
1553 u_int32_t reqid;
1554
1555 reqid = charon->traps->find_reqid(charon->traps, cfg);
1556 if (reqid)
1557 { /* already trapped */
1558 return reqid;
1559 }
1560
1561 enumerator = charon->controller->create_ike_sa_enumerator(
1562 charon->controller, TRUE);
1563 while (!reqid && enumerator->enumerate(enumerator, &ike_sa))
1564 {
1565 children = ike_sa->create_child_sa_enumerator(ike_sa);
1566 while (children->enumerate(children, &child_sa))
1567 {
1568 if (streq(cfg->get_name(cfg), child_sa->get_name(child_sa)))
1569 {
1570 reqid = child_sa->get_reqid(child_sa);
1571 break;
1572 }
1573 }
1574 children->destroy(children);
1575 }
1576 enumerator->destroy(enumerator);
1577 return reqid;
1578 }
1579
1580 /**
1581 * Perform start actions associated to a child config
1582 */
1583 static void run_start_action(private_vici_config_t *this, peer_cfg_t *peer_cfg,
1584 child_cfg_t *child_cfg)
1585 {
1586 switch (child_cfg->get_start_action(child_cfg))
1587 {
1588 case ACTION_RESTART:
1589 DBG1(DBG_CFG, "initiating '%s'", child_cfg->get_name(child_cfg));
1590 charon->controller->initiate(charon->controller,
1591 peer_cfg->get_ref(peer_cfg), child_cfg->get_ref(child_cfg),
1592 NULL, NULL, 0, FALSE);
1593 break;
1594 case ACTION_ROUTE:
1595 DBG1(DBG_CFG, "installing '%s'", child_cfg->get_name(child_cfg));
1596 switch (child_cfg->get_mode(child_cfg))
1597 {
1598 case MODE_PASS:
1599 case MODE_DROP:
1600 charon->shunts->install(charon->shunts, child_cfg);
1601 break;
1602 default:
1603 charon->traps->install(charon->traps, peer_cfg, child_cfg,
1604 find_reqid(child_cfg));
1605 break;
1606 }
1607 break;
1608 default:
1609 break;
1610 }
1611 }
1612
1613 /**
1614 * Undo start actions associated to a child config
1615 */
1616 static void clear_start_action(private_vici_config_t *this,
1617 child_cfg_t *child_cfg)
1618 {
1619 enumerator_t *enumerator, *children;
1620 child_sa_t *child_sa;
1621 ike_sa_t *ike_sa;
1622 u_int32_t id = 0, *del;
1623 array_t *ids = NULL;
1624 char *name;
1625
1626 name = child_cfg->get_name(child_cfg);
1627 switch (child_cfg->get_start_action(child_cfg))
1628 {
1629 case ACTION_RESTART:
1630 enumerator = charon->controller->create_ike_sa_enumerator(
1631 charon->controller, TRUE);
1632 while (enumerator->enumerate(enumerator, &ike_sa))
1633 {
1634 children = ike_sa->create_child_sa_enumerator(ike_sa);
1635 while (children->enumerate(children, &child_sa))
1636 {
1637 if (streq(name, child_sa->get_name(child_sa)))
1638 {
1639 id = child_sa->get_unique_id(child_sa);
1640 array_insert_create(&ids, ARRAY_TAIL, &id);
1641 }
1642 }
1643 children->destroy(children);
1644 }
1645 enumerator->destroy(enumerator);
1646
1647 if (array_count(ids))
1648 {
1649 while (array_remove(ids, ARRAY_HEAD, &del))
1650 {
1651 DBG1(DBG_CFG, "closing '%s' #%u", name, *del);
1652 charon->controller->terminate_child(charon->controller,
1653 *del, NULL, NULL, 0);
1654 }
1655 array_destroy(ids);
1656 }
1657 break;
1658 case ACTION_ROUTE:
1659 DBG1(DBG_CFG, "uninstalling '%s'", name);
1660 switch (child_cfg->get_mode(child_cfg))
1661 {
1662 case MODE_PASS:
1663 case MODE_DROP:
1664 charon->shunts->uninstall(charon->shunts, name);
1665 break;
1666 default:
1667 enumerator = charon->traps->create_enumerator(charon->traps);
1668 while (enumerator->enumerate(enumerator, NULL, &child_sa))
1669 {
1670 if (streq(name, child_sa->get_name(child_sa)))
1671 {
1672 id = child_sa->get_reqid(child_sa);
1673 break;
1674 }
1675 }
1676 enumerator->destroy(enumerator);
1677 if (id)
1678 {
1679 charon->traps->uninstall(charon->traps, id);
1680 }
1681 break;
1682 }
1683 break;
1684 default:
1685 break;
1686 }
1687 }
1688
1689 /**
1690 * Run start actions associated to all child configs of a peer config
1691 */
1692 static void run_start_actions(private_vici_config_t *this, peer_cfg_t *peer_cfg)
1693 {
1694 enumerator_t *enumerator;
1695 child_cfg_t *child_cfg;
1696
1697 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
1698 while (enumerator->enumerate(enumerator, &child_cfg))
1699 {
1700 run_start_action(this, peer_cfg, child_cfg);
1701 }
1702 enumerator->destroy(enumerator);
1703 }
1704
1705 /**
1706 * Undo start actions associated to all child configs of a peer config
1707 */
1708 static void clear_start_actions(private_vici_config_t *this,
1709 peer_cfg_t *peer_cfg)
1710 {
1711 enumerator_t *enumerator;
1712 child_cfg_t *child_cfg;
1713
1714 enumerator = peer_cfg->create_child_cfg_enumerator(peer_cfg);
1715 while (enumerator->enumerate(enumerator, &child_cfg))
1716 {
1717 clear_start_action(this, child_cfg);
1718 }
1719 enumerator->destroy(enumerator);
1720 }
1721
1722 /**
1723 * Replace children of a peer config by a new config
1724 */
1725 static void replace_children(private_vici_config_t *this,
1726 peer_cfg_t *from, peer_cfg_t *to)
1727 {
1728 enumerator_t *enumerator;
1729 child_cfg_t *child;
1730
1731 enumerator = to->create_child_cfg_enumerator(to);
1732 while (enumerator->enumerate(enumerator, &child))
1733 {
1734 to->remove_child_cfg(to, enumerator);
1735 clear_start_action(this, child);
1736 child->destroy(child);
1737 }
1738 enumerator->destroy(enumerator);
1739
1740 enumerator = from->create_child_cfg_enumerator(from);
1741 while (enumerator->enumerate(enumerator, &child))
1742 {
1743 from->remove_child_cfg(from, enumerator);
1744 to->add_child_cfg(to, child);
1745 run_start_action(this, to, child);
1746 }
1747 enumerator->destroy(enumerator);
1748 }
1749
1750 /**
1751 * Merge/replace a peer config with existing configs
1752 */
1753 static void merge_config(private_vici_config_t *this, peer_cfg_t *peer_cfg)
1754 {
1755 enumerator_t *enumerator;
1756 peer_cfg_t *current;
1757 ike_cfg_t *ike_cfg;
1758 bool merged = FALSE;
1759
1760 this->lock->write_lock(this->lock);
1761
1762 enumerator = this->conns->create_enumerator(this->conns);
1763 while (enumerator->enumerate(enumerator, &current))
1764 {
1765 if (streq(peer_cfg->get_name(peer_cfg), current->get_name(current)))
1766 {
1767 ike_cfg = current->get_ike_cfg(current);
1768 if (peer_cfg->equals(peer_cfg, current) &&
1769 ike_cfg->equals(ike_cfg, peer_cfg->get_ike_cfg(peer_cfg)))
1770 {
1771 DBG1(DBG_CFG, "updated vici connection: %s",
1772 peer_cfg->get_name(peer_cfg));
1773 replace_children(this, peer_cfg, current);
1774 peer_cfg->destroy(peer_cfg);
1775 }
1776 else
1777 {
1778 DBG1(DBG_CFG, "replaced vici connection: %s",
1779 peer_cfg->get_name(peer_cfg));
1780 this->conns->remove_at(this->conns, enumerator);
1781 clear_start_actions(this, current);
1782 current->destroy(current);
1783 this->conns->insert_last(this->conns, peer_cfg);
1784 run_start_actions(this, peer_cfg);
1785 }
1786 merged = TRUE;
1787 break;
1788 }
1789 }
1790 enumerator->destroy(enumerator);
1791
1792 if (!merged)
1793 {
1794 DBG1(DBG_CFG, "added vici connection: %s", peer_cfg->get_name(peer_cfg));
1795 this->conns->insert_last(this->conns, peer_cfg);
1796 run_start_actions(this, peer_cfg);
1797 }
1798
1799 this->lock->unlock(this->lock);
1800 }
1801
1802 CALLBACK(config_sn, bool,
1803 request_data_t *request, vici_message_t *message,
1804 vici_parse_context_t *ctx, char *name)
1805 {
1806 peer_data_t peer = {
1807 .request = request,
1808 .local = linked_list_create(),
1809 .remote = linked_list_create(),
1810 .vips = linked_list_create(),
1811 .children = linked_list_create(),
1812 .proposals = linked_list_create(),
1813 .mobike = TRUE,
1814 .send_certreq = TRUE,
1815 .pull = TRUE,
1816 .send_cert = CERT_SEND_IF_ASKED,
1817 .version = IKE_ANY,
1818 .remote_port = IKEV2_UDP_PORT,
1819 .fragmentation = FRAGMENTATION_NO,
1820 .unique = UNIQUE_NO,
1821 .keyingtries = 1,
1822 .rekey_time = LFT_UNDEFINED,
1823 .reauth_time = LFT_UNDEFINED,
1824 .over_time = LFT_UNDEFINED,
1825 .rand_time = LFT_UNDEFINED,
1826 };
1827 enumerator_t *enumerator;
1828 peer_cfg_t *peer_cfg;
1829 ike_cfg_t *ike_cfg;
1830 child_cfg_t *child_cfg;
1831 auth_cfg_t *auth_cfg;
1832 proposal_t *proposal;
1833 host_t *host;
1834 char *str;
1835
1836 DBG2(DBG_CFG, " conn %s:", name);
1837
1838 if (!message->parse(message, ctx, peer_sn, peer_kv, peer_li, &peer))
1839 {
1840 free_peer_data(&peer);
1841 return FALSE;
1842 }
1843
1844 if (peer.local->get_count(peer.local) == 0)
1845 {
1846 free_peer_data(&peer);
1847 peer.request->reply = create_reply("missing local auth config");
1848 return FALSE;
1849 }
1850 if (peer.remote->get_count(peer.remote) == 0)
1851 {
1852 auth_cfg = auth_cfg_create();
1853 peer.remote->insert_last(peer.remote, auth_cfg);
1854 }
1855 if (peer.proposals->get_count(peer.proposals) == 0)
1856 {
1857 proposal = proposal_create_default(PROTO_IKE);
1858 if (proposal)
1859 {
1860 peer.proposals->insert_last(peer.proposals, proposal);
1861 }
1862 proposal = proposal_create_default_aead(PROTO_IKE);
1863 if (proposal)
1864 {
1865 peer.proposals->insert_last(peer.proposals, proposal);
1866 }
1867 }
1868 if (!peer.local_addrs)
1869 {
1870 peer.local_addrs = strdup("%any");
1871 }
1872 if (!peer.remote_addrs)
1873 {
1874 peer.remote_addrs = strdup("%any");
1875 }
1876 if (!peer.local_port)
1877 {
1878 peer.local_port = charon->socket->get_port(charon->socket, FALSE);
1879 }
1880
1881 if (peer.rekey_time == LFT_UNDEFINED && peer.reauth_time == LFT_UNDEFINED)
1882 {
1883 /* apply a default rekey time if no rekey/reauth time set */
1884 peer.rekey_time = LFT_DEFAULT_IKE_REKEY;
1885 peer.reauth_time = 0;
1886 }
1887 if (peer.rekey_time == LFT_UNDEFINED)
1888 {
1889 peer.rekey_time = 0;
1890 }
1891 if (peer.reauth_time == LFT_UNDEFINED)
1892 {
1893 peer.reauth_time = 0;
1894 }
1895 if (peer.over_time == LFT_UNDEFINED)
1896 {
1897 /* default over_time to 10% of rekey/reauth time if not given */
1898 peer.over_time = max(peer.rekey_time, peer.reauth_time) / 10;
1899 }
1900 if (peer.rand_time == LFT_UNDEFINED)
1901 {
1902 /* default rand_time to over_time if not given, but don't make it
1903 * longer than half of rekey/rauth time */
1904 if (peer.rekey_time && peer.reauth_time)
1905 {
1906 peer.rand_time = min(peer.rekey_time, peer.reauth_time);
1907 }
1908 else
1909 {
1910 peer.rand_time = max(peer.rekey_time, peer.reauth_time);
1911 }
1912 peer.rand_time = min(peer.over_time, peer.rand_time / 2);
1913 }
1914
1915 log_peer_data(&peer);
1916
1917 ike_cfg = ike_cfg_create(peer.version, peer.send_certreq, peer.encap,
1918 peer.local_addrs, peer.local_port,
1919 peer.remote_addrs, peer.remote_port,
1920 peer.fragmentation, 0);
1921 peer_cfg = peer_cfg_create(name, ike_cfg, peer.send_cert, peer.unique,
1922 peer.keyingtries, peer.rekey_time, peer.reauth_time,
1923 peer.rand_time, peer.over_time, peer.mobike,
1924 peer.aggressive, peer.pull,
1925 peer.dpd_delay, peer.dpd_timeout,
1926 FALSE, NULL, NULL);
1927
1928 while (peer.local->remove_first(peer.local,
1929 (void**)&auth_cfg) == SUCCESS)
1930 {
1931 peer_cfg->add_auth_cfg(peer_cfg, auth_cfg, TRUE);
1932 }
1933 while (peer.remote->remove_first(peer.remote,
1934 (void**)&auth_cfg) == SUCCESS)
1935 {
1936 peer_cfg->add_auth_cfg(peer_cfg, auth_cfg, FALSE);
1937 }
1938 while (peer.children->remove_first(peer.children,
1939 (void**)&child_cfg) == SUCCESS)
1940 {
1941 peer_cfg->add_child_cfg(peer_cfg, child_cfg);
1942 }
1943 while (peer.proposals->remove_first(peer.proposals,
1944 (void**)&proposal) == SUCCESS)
1945 {
1946 ike_cfg->add_proposal(ike_cfg, proposal);
1947 }
1948 while (peer.vips->remove_first(peer.vips, (void**)&host) == SUCCESS)
1949 {
1950 peer_cfg->add_virtual_ip(peer_cfg, host);
1951 }
1952 if (peer.pools)
1953 {
1954 enumerator = enumerator_create_token(peer.pools, ",", " ");
1955 while (enumerator->enumerate(enumerator, &str))
1956 {
1957 peer_cfg->add_pool(peer_cfg, str);
1958 }
1959 enumerator->destroy(enumerator);
1960 }
1961
1962 free_peer_data(&peer);
1963
1964 merge_config(request->this, peer_cfg);
1965
1966 return TRUE;
1967 }
1968
1969 CALLBACK(load_conn, vici_message_t*,
1970 private_vici_config_t *this, char *name, u_int id, vici_message_t *message)
1971 {
1972 request_data_t request = {
1973 .this = this,
1974 };
1975
1976 if (!message->parse(message, NULL, config_sn, NULL, NULL, &request))
1977 {
1978 if (request.reply)
1979 {
1980 return request.reply;
1981 }
1982 return create_reply("parsing request failed");
1983 }
1984 return create_reply(NULL);
1985 }
1986
1987 CALLBACK(unload_conn, vici_message_t*,
1988 private_vici_config_t *this, char *name, u_int id, vici_message_t *message)
1989 {
1990 enumerator_t *enumerator;
1991 peer_cfg_t *cfg;
1992 char *conn_name;
1993 bool found = FALSE;
1994
1995 conn_name = message->get_str(message, NULL, "name");
1996 if (!conn_name)
1997 {
1998 return create_reply("unload: missing connection name");
1999 }
2000
2001 this->lock->write_lock(this->lock);
2002 enumerator = this->conns->create_enumerator(this->conns);
2003 while (enumerator->enumerate(enumerator, &cfg))
2004 {
2005 if (streq(cfg->get_name(cfg), conn_name))
2006 {
2007 this->conns->remove_at(this->conns, enumerator);
2008 clear_start_actions(this, cfg);
2009 cfg->destroy(cfg);
2010 found = TRUE;
2011 break;
2012 }
2013 }
2014 enumerator->destroy(enumerator);
2015 this->lock->unlock(this->lock);
2016
2017 if (!found)
2018 {
2019 return create_reply("unload: connection '%s' not found", conn_name);
2020 }
2021 return create_reply(NULL);
2022 }
2023
2024 CALLBACK(get_conns, vici_message_t*,
2025 private_vici_config_t *this, char *name, u_int id, vici_message_t *message)
2026 {
2027 vici_builder_t *builder;
2028 enumerator_t *enumerator;
2029 peer_cfg_t *cfg;
2030
2031 builder = vici_builder_create();
2032 builder->begin_list(builder, "conns");
2033
2034 this->lock->read_lock(this->lock);
2035 enumerator = this->conns->create_enumerator(this->conns);
2036 while (enumerator->enumerate(enumerator, &cfg))
2037 {
2038 builder->add_li(builder, "%s", cfg->get_name(cfg));
2039 }
2040 enumerator->destroy(enumerator);
2041 this->lock->unlock(this->lock);
2042
2043 builder->end_list(builder);
2044
2045 return builder->finalize(builder);
2046 }
2047
2048 static void manage_command(private_vici_config_t *this,
2049 char *name, vici_command_cb_t cb, bool reg)
2050 {
2051 this->dispatcher->manage_command(this->dispatcher, name,
2052 reg ? cb : NULL, this);
2053 }
2054
2055 /**
2056 * (Un-)register dispatcher functions
2057 */
2058 static void manage_commands(private_vici_config_t *this, bool reg)
2059 {
2060 manage_command(this, "load-conn", load_conn, reg);
2061 manage_command(this, "unload-conn", unload_conn, reg);
2062 manage_command(this, "get-conns", get_conns, reg);
2063 }
2064
2065 METHOD(vici_config_t, destroy, void,
2066 private_vici_config_t *this)
2067 {
2068 manage_commands(this, FALSE);
2069 this->conns->destroy_offset(this->conns, offsetof(peer_cfg_t, destroy));
2070 this->lock->destroy(this->lock);
2071 free(this);
2072 }
2073
2074 /**
2075 * See header
2076 */
2077 vici_config_t *vici_config_create(vici_dispatcher_t *dispatcher,
2078 vici_authority_t *authority)
2079 {
2080 private_vici_config_t *this;
2081
2082 INIT(this,
2083 .public = {
2084 .backend = {
2085 .create_peer_cfg_enumerator = _create_peer_cfg_enumerator,
2086 .create_ike_cfg_enumerator = _create_ike_cfg_enumerator,
2087 .get_peer_cfg_by_name = _get_peer_cfg_by_name,
2088 },
2089 .destroy = _destroy,
2090 },
2091 .dispatcher = dispatcher,
2092 .conns = linked_list_create(),
2093 .lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
2094 .authority = authority,
2095 );
2096
2097 manage_commands(this, TRUE);
2098
2099 return &this->public;
2100 }