support of xfrm marks for IKEv2
[strongswan.git] / src / starter / confread.c
1 /* strongSwan IPsec config file parser
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include <freeswan.h>
21
22 #include "../pluto/constants.h"
23 #include "../pluto/defs.h"
24 #include "../pluto/log.h"
25
26 #include "keywords.h"
27 #include "parser.h"
28 #include "confread.h"
29 #include "args.h"
30 #include "interfaces.h"
31
32 /* strings containing a colon are interpreted as an IPv6 address */
33 #define ip_version(string) (strchr(string, '.') ? AF_INET : AF_INET6)
34
35 static const char ike_defaults[] = "aes128-sha1-modp2048,3des-sha1-modp1536";
36 static const char esp_defaults[] = "aes128-sha1,3des-sha1";
37
38 static const char firewall_defaults[] = "ipsec _updown iptables";
39
40 static void default_values(starter_config_t *cfg)
41 {
42 if (cfg == NULL)
43 return;
44
45 memset(cfg, 0, sizeof(struct starter_config));
46
47 /* is there enough space for all seen flags? */
48 assert(KW_SETUP_LAST - KW_SETUP_FIRST <
49 sizeof(cfg->setup.seen) * BITS_PER_BYTE);
50 assert(KW_CONN_LAST - KW_CONN_FIRST <
51 sizeof(cfg->conn_default.seen) * BITS_PER_BYTE);
52 assert(KW_END_LAST - KW_END_FIRST <
53 sizeof(cfg->conn_default.right.seen) * BITS_PER_BYTE);
54 assert(KW_CA_LAST - KW_CA_FIRST <
55 sizeof(cfg->ca_default.seen) * BITS_PER_BYTE);
56
57 cfg->setup.seen = LEMPTY;
58 cfg->setup.fragicmp = TRUE;
59 cfg->setup.hidetos = TRUE;
60 cfg->setup.uniqueids = TRUE;
61 cfg->setup.interfaces = new_list("%defaultroute");
62
63 #ifdef START_CHARON
64 cfg->setup.charonstart = TRUE;
65 #endif
66 #ifdef START_PLUTO
67 cfg->setup.plutostart = TRUE;
68 #endif
69
70 cfg->conn_default.seen = LEMPTY;
71 cfg->conn_default.startup = STARTUP_NO;
72 cfg->conn_default.state = STATE_IGNORE;
73 cfg->conn_default.policy = POLICY_ENCRYPT | POLICY_TUNNEL | POLICY_PUBKEY |
74 POLICY_PFS | POLICY_MOBIKE;
75
76 cfg->conn_default.ike = clone_str(ike_defaults);
77 cfg->conn_default.esp = clone_str(esp_defaults);
78 cfg->conn_default.sa_ike_life_seconds = OAKLEY_ISAKMP_SA_LIFETIME_DEFAULT;
79 cfg->conn_default.sa_ipsec_life_seconds = PLUTO_SA_LIFE_DURATION_DEFAULT;
80 cfg->conn_default.sa_rekey_margin = SA_REPLACEMENT_MARGIN_DEFAULT;
81 cfg->conn_default.sa_rekey_fuzz = SA_REPLACEMENT_FUZZ_DEFAULT;
82 cfg->conn_default.sa_keying_tries = SA_REPLACEMENT_RETRIES_DEFAULT;
83 cfg->conn_default.addr_family = AF_INET;
84 cfg->conn_default.tunnel_addr_family = AF_INET;
85 cfg->conn_default.install_policy = TRUE;
86 cfg->conn_default.dpd_delay = 30; /* seconds */
87 cfg->conn_default.dpd_timeout = 150; /* seconds */
88
89 cfg->conn_default.left.seen = LEMPTY;
90 cfg->conn_default.right.seen = LEMPTY;
91
92 cfg->conn_default.left.sendcert = CERT_SEND_IF_ASKED;
93 cfg->conn_default.right.sendcert = CERT_SEND_IF_ASKED;
94
95 anyaddr(AF_INET, &cfg->conn_default.left.addr);
96 anyaddr(AF_INET, &cfg->conn_default.left.nexthop);
97 anyaddr(AF_INET, &cfg->conn_default.right.addr);
98 anyaddr(AF_INET, &cfg->conn_default.right.nexthop);
99 cfg->conn_default.left.ikeport = 500;
100 cfg->conn_default.right.ikeport = 500;
101
102 cfg->ca_default.seen = LEMPTY;
103 }
104
105 #define KW_POLICY_FLAG(sy, sn, fl) \
106 if (streq(kw->value, sy)) { conn->policy |= fl; } \
107 else if (streq(kw->value, sn)) { conn->policy &= ~fl; } \
108 else { plog("# bad policy value: %s=%s", kw->entry->name, kw->value); cfg->err++; }
109
110 static void load_setup(starter_config_t *cfg, config_parsed_t *cfgp)
111 {
112 kw_list_t *kw;
113
114 DBG(DBG_CONTROL,
115 DBG_log("Loading config setup")
116 )
117
118 for (kw = cfgp->config_setup; kw; kw = kw->next)
119 {
120 bool assigned = FALSE;
121
122 kw_token_t token = kw->entry->token;
123
124 if (token < KW_SETUP_FIRST || token > KW_SETUP_LAST)
125 {
126 plog("# unsupported keyword '%s' in config setup", kw->entry->name);
127 cfg->err++;
128 continue;
129 }
130
131 if (!assign_arg(token, KW_SETUP_FIRST, kw, (char *)cfg, &assigned))
132 {
133 plog(" bad argument value in config setup");
134 cfg->err++;
135 continue;
136 }
137 }
138 }
139
140 static void kw_end(starter_conn_t *conn, starter_end_t *end, kw_token_t token,
141 kw_list_t *kw, char *conn_name, starter_config_t *cfg)
142 {
143 err_t ugh = NULL;
144 bool assigned = FALSE;
145 bool has_port_wildcard; /* set if port is %any */
146
147 char *name = kw->entry->name;
148 char *value = kw->value;
149
150 if (!assign_arg(token, KW_END_FIRST, kw, (char *)end, &assigned))
151 goto err;
152
153 /* post processing of some keywords that were assigned automatically */
154 switch (token)
155 {
156 case KW_SUBNET:
157 if ((strlen(value) >= 6 && strncmp(value,"vhost:",6) == 0)
158 || (strlen(value) >= 5 && strncmp(value,"vnet:",5) == 0))
159 {
160 /* used by pluto only */
161 end->has_virt = TRUE;
162 }
163 else
164 {
165 ip_subnet net;
166 char *pos;
167 int len = 0;
168
169 end->has_client = TRUE;
170 conn->tunnel_addr_family = ip_version(value);
171
172 pos = strchr(value, ',');
173 if (pos)
174 {
175 len = pos - value;
176 }
177 ugh = ttosubnet(value, len, ip_version(value), &net);
178 if (ugh != NULL)
179 {
180 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
181 goto err;
182 }
183 }
184 break;
185 case KW_SOURCEIP:
186 if (end->has_natip)
187 {
188 plog("# natip and sourceip cannot be defined at the same time");
189 goto err;
190 }
191 if (value[0] == '%')
192 {
193 if (streq(value, "%modeconfig") || streq(value, "%modecfg") ||
194 streq(value, "%config") || streq(value, "%cfg"))
195 {
196 /* request ip via config payload */
197 free(end->sourceip);
198 end->sourceip = NULL;
199 end->sourceip_mask = 1;
200 }
201 else
202 { /* %poolname, strip %, serve ip requests */
203 free(end->sourceip);
204 end->sourceip = clone_str(value+1);
205 end->sourceip_mask = 0;
206 }
207 end->modecfg = TRUE;
208 }
209 else
210 {
211 char *pos;
212 ip_address addr;
213 ip_subnet net;
214
215 conn->tunnel_addr_family = ip_version(value);
216 pos = strchr(value, '/');
217
218 if (pos)
219 { /* CIDR notation, address pool */
220 ugh = ttosubnet(value, 0, conn->tunnel_addr_family, &net);
221 if (ugh != NULL)
222 {
223 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
224 goto err;
225 }
226 *pos = '\0';
227 free(end->sourceip);
228 end->sourceip = clone_str(value);
229 end->sourceip_mask = atoi(pos + 1);
230 }
231 else
232 { /* fixed srcip */
233 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
234 if (ugh != NULL)
235 {
236 plog("# bad addr: %s=%s [%s]", name, value, ugh);
237 goto err;
238 }
239 end->sourceip_mask = (conn->tunnel_addr_family == AF_INET) ?
240 32 : 128;
241 }
242 }
243 conn->policy |= POLICY_TUNNEL;
244 break;
245 case KW_SENDCERT:
246 if (end->sendcert == CERT_YES_SEND)
247 {
248 end->sendcert = CERT_ALWAYS_SEND;
249 }
250 else if (end->sendcert == CERT_NO_SEND)
251 {
252 end->sendcert = CERT_NEVER_SEND;
253 }
254 break;
255 default:
256 break;
257 }
258
259 if (assigned)
260 return;
261
262 /* individual processing of keywords that were not assigned automatically */
263 switch (token)
264 {
265 case KW_HOST:
266 if (streq(value, "%defaultroute"))
267 {
268 if (cfg->defaultroute.defined)
269 {
270 end->addr = cfg->defaultroute.addr;
271 end->nexthop = cfg->defaultroute.nexthop;
272 }
273 else if (!cfg->defaultroute.supported)
274 {
275 plog("%%defaultroute not supported, fallback to %%any");
276 }
277 else
278 {
279 plog("# default route not known: %s=%s", name, value);
280 goto err;
281 }
282 }
283 else if (streq(value, "%any") || streq(value, "%any4"))
284 {
285 anyaddr(conn->addr_family, &end->addr);
286 }
287 else if (streq(value, "%any6"))
288 {
289 conn->addr_family = AF_INET6;
290 anyaddr(conn->addr_family, &end->addr);
291 }
292 else if (streq(value, "%group"))
293 {
294 ip_address any;
295
296 conn->policy |= POLICY_GROUP | POLICY_TUNNEL;
297 anyaddr(conn->addr_family, &end->addr);
298 anyaddr(conn->tunnel_addr_family, &any);
299 end->has_client = TRUE;
300 }
301 else
302 {
303 /* check for allow_any prefix */
304 if (value[0] == '%')
305 {
306 end->allow_any = TRUE;
307 value++;
308 }
309 conn->addr_family = ip_version(value);
310 ugh = ttoaddr(value, 0, conn->addr_family, &end->addr);
311 if (ugh != NULL)
312 {
313 plog("# bad addr: %s=%s [%s]", name, value, ugh);
314 if (streq(ugh, "does not look numeric and name lookup failed"))
315 {
316 end->dns_failed = TRUE;
317 anyaddr(conn->addr_family, &end->addr);
318 }
319 else
320 {
321 goto err;
322 }
323 }
324 }
325 break;
326 case KW_NEXTHOP:
327 if (streq(value, "%defaultroute"))
328 {
329 if (cfg->defaultroute.defined)
330 {
331 end->nexthop = cfg->defaultroute.nexthop;
332 }
333 else
334 {
335 plog("# default route not known: %s=%s", name, value);
336 goto err;
337 }
338 }
339 else if (streq(value, "%direct"))
340 {
341 ugh = anyaddr(conn->addr_family, &end->nexthop);
342 }
343 else
344 {
345 conn->addr_family = ip_version(value);
346 ugh = ttoaddr(value, 0, conn->addr_family, &end->nexthop);
347 }
348 if (ugh != NULL)
349 {
350 plog("# bad addr: %s=%s [%s]", name, value, ugh);
351 goto err;
352 }
353 break;
354 case KW_SUBNETWITHIN:
355 {
356 ip_subnet net;
357
358 end->has_client = TRUE;
359 end->has_client_wildcard = TRUE;
360 conn->tunnel_addr_family = ip_version(value);
361
362 ugh = ttosubnet(value, 0, ip_version(value), &net);
363 if (ugh != NULL)
364 {
365 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
366 goto err;
367 }
368 end->subnet = clone_str(value);
369 break;
370 }
371 case KW_PROTOPORT:
372 ugh = ttoprotoport(value, 0, &end->protocol, &end->port, &has_port_wildcard);
373 end->has_port_wildcard = has_port_wildcard;
374 break;
375 case KW_NATIP:
376 if (end->sourceip)
377 {
378 plog("# natip and sourceip cannot be defined at the same time");
379 goto err;
380 }
381 if (streq(value, "%defaultroute"))
382 {
383 char buf[64];
384
385 if (cfg->defaultroute.defined)
386 {
387 addrtot(&cfg->defaultroute.addr, 0, buf, sizeof(buf));
388 end->sourceip = clone_str(buf);
389 }
390 else
391 {
392 plog("# default route not known: %s=%s", name, value);
393 goto err;
394 }
395 }
396 else
397 {
398 ip_address addr;
399
400 conn->tunnel_addr_family = ip_version(value);
401 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
402 if (ugh != NULL)
403 {
404 plog("# bad addr: %s=%s [%s]", name, value, ugh);
405 goto err;
406 }
407 end->sourceip = clone_str(value);
408 }
409 end->has_natip = TRUE;
410 conn->policy |= POLICY_TUNNEL;
411 break;
412 default:
413 break;
414 }
415 return;
416
417 err:
418 plog(" bad argument value in conn '%s'", conn_name);
419 cfg->err++;
420 }
421
422 /*
423 * handles left|right=<FQDN> DNS resolution failure
424 */
425 static void handle_dns_failure(const char *label, starter_end_t *end,
426 starter_config_t *cfg)
427 {
428 if (end->dns_failed)
429 {
430 if (end->allow_any)
431 {
432 plog("# fallback to %s=%%any due to '%%' prefix or %sallowany=yes",
433 label, label);
434 }
435 else
436 {
437 /* declare an error */
438 cfg->err++;
439 }
440 }
441 }
442
443 /*
444 * handles left|rightfirewall and left|rightupdown parameters
445 */
446 static void handle_firewall(const char *label, starter_end_t *end,
447 starter_config_t *cfg)
448 {
449 if (end->firewall && (end->seen & LELEM(KW_FIREWALL - KW_END_FIRST)))
450 {
451 if (end->updown != NULL)
452 {
453 plog("# cannot have both %sfirewall and %supdown", label, label);
454 cfg->err++;
455 }
456 else
457 {
458 end->updown = clone_str(firewall_defaults);
459 end->firewall = FALSE;
460 }
461 }
462 }
463
464 /*
465 * parse a conn section
466 */
467 static void load_conn(starter_conn_t *conn, kw_list_t *kw, starter_config_t *cfg)
468 {
469 char *conn_name = (conn->name == NULL)? "%default":conn->name;
470
471 for ( ; kw; kw = kw->next)
472 {
473 bool assigned = FALSE;
474
475 kw_token_t token = kw->entry->token;
476
477 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
478 {
479 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST
480 , kw, conn_name, cfg);
481 continue;
482 }
483 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
484 {
485 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST
486 , kw, conn_name, cfg);
487 continue;
488 }
489
490 if (token == KW_AUTO)
491 {
492 token = KW_CONN_SETUP;
493 }
494 else if (token == KW_ALSO)
495 {
496 if (cfg->parse_also)
497 {
498 also_t *also = malloc_thing(also_t);
499
500 also->name = clone_str(kw->value);
501 also->next = conn->also;
502 conn->also = also;
503
504 DBG(DBG_CONTROL,
505 DBG_log(" also=%s", kw->value)
506 )
507 }
508 continue;
509 }
510
511 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
512 {
513 plog("# unsupported keyword '%s' in conn '%s'"
514 , kw->entry->name, conn_name);
515 cfg->err++;
516 continue;
517 }
518
519 if (!assign_arg(token, KW_CONN_FIRST, kw, (char *)conn, &assigned))
520 {
521 plog(" bad argument value in conn '%s'", conn_name);
522 cfg->err++;
523 continue;
524 }
525
526 if (assigned)
527 continue;
528
529 switch (token)
530 {
531 case KW_TYPE:
532 conn->policy &= ~(POLICY_TUNNEL | POLICY_SHUNT_MASK);
533 if (streq(kw->value, "tunnel"))
534 {
535 conn->policy |= POLICY_TUNNEL;
536 }
537 else if (streq(kw->value, "beet"))
538 {
539 conn->policy |= POLICY_BEET;
540 }
541 else if (streq(kw->value, "transport_proxy"))
542 {
543 conn->policy |= POLICY_PROXY;
544 }
545 else if (streq(kw->value, "passthrough") || streq(kw->value, "pass"))
546 {
547 conn->policy |= POLICY_SHUNT_PASS;
548 }
549 else if (streq(kw->value, "drop"))
550 {
551 conn->policy |= POLICY_SHUNT_DROP;
552 }
553 else if (streq(kw->value, "reject"))
554 {
555 conn->policy |= POLICY_SHUNT_REJECT;
556 }
557 else if (strcmp(kw->value, "transport") != 0)
558 {
559 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
560 cfg->err++;
561 }
562 break;
563 case KW_PFS:
564 KW_POLICY_FLAG("yes", "no", POLICY_PFS)
565 break;
566 case KW_COMPRESS:
567 KW_POLICY_FLAG("yes", "no", POLICY_COMPRESS)
568 break;
569 case KW_AUTH:
570 KW_POLICY_FLAG("ah", "esp", POLICY_AUTHENTICATE)
571 break;
572 case KW_AUTHBY:
573 conn->policy &= ~(POLICY_ID_AUTH_MASK | POLICY_ENCRYPT);
574
575 if (!(streq(kw->value, "never") || streq(kw->value, "eap")))
576 {
577 char *value = kw->value;
578 char *second = strchr(kw->value, '|');
579
580 if (second != NULL)
581 {
582 *second = '\0';
583 }
584
585 /* also handles the cases secret|rsasig and rsasig|secret */
586 for (;;)
587 {
588 if (streq(value, "rsa") || streq(value, "rsasig") ||
589 streq(value, "ecdsa") || streq(value, "ecdsasig") ||
590 streq(value, "pubkey"))
591 {
592 conn->policy |= POLICY_PUBKEY | POLICY_ENCRYPT;
593 }
594 else if (streq(value, "secret") || streq(value, "psk"))
595 {
596 conn->policy |= POLICY_PSK | POLICY_ENCRYPT;
597 }
598 else if (streq(value, "xauthrsasig"))
599 {
600 conn->policy |= POLICY_XAUTH_RSASIG | POLICY_ENCRYPT;
601 }
602 else if (streq(value, "xauthpsk"))
603 {
604 conn->policy |= POLICY_XAUTH_PSK | POLICY_ENCRYPT;
605 }
606 else
607 {
608 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
609 cfg->err++;
610 break;
611 }
612 if (second == NULL)
613 {
614 break;
615 }
616 value = second;
617 second = NULL; /* traverse the loop no more than twice */
618 }
619 }
620 break;
621 case KW_EAP:
622 {
623 char *sep;
624
625 /* check for vendor-type format */
626 sep = strchr(kw->value, '-');
627 if (sep)
628 {
629 *(sep++) = '\0';
630 conn->eap_type = atoi(kw->value);
631 conn->eap_vendor = atoi(sep);
632 if (conn->eap_type == 0 || conn->eap_vendor == 0)
633 {
634 plog("# invalid EAP type: %s=%s", kw->entry->name, kw->value);
635 cfg->err++;
636 }
637 break;
638 }
639 if (streq(kw->value, "aka"))
640 {
641 conn->eap_type = 23;
642 }
643 else if (streq(kw->value, "sim"))
644 {
645 conn->eap_type = 18;
646 }
647 else if (streq(kw->value, "md5"))
648 {
649 conn->eap_type = 4;
650 }
651 else if (streq(kw->value, "gtc"))
652 {
653 conn->eap_type = 6;
654 }
655 else if (streq(kw->value, "mschapv2"))
656 {
657 conn->eap_type = 26;
658 }
659 else if (streq(kw->value, "radius"))
660 { /* pseudo-type */
661 conn->eap_type = 253;
662 }
663 else
664 {
665 conn->eap_type = atoi(kw->value);
666 if (conn->eap_type == 0)
667 {
668 plog("# unknown EAP type: %s=%s", kw->entry->name, kw->value);
669 cfg->err++;
670 }
671 }
672 break;
673 }
674 case KW_MARK:
675 {
676 char *pos, *endptr;
677
678 pos = strchr(kw->value, '/');
679 if (pos)
680 {
681 *pos = '\0';
682 conn->mark_mask = strtoul(pos+1, &endptr, 0);
683 if (*endptr != '\0')
684 {
685 plog("# invalid mark mask: %s", pos+1);
686 cfg->err++;
687 break;
688 }
689 }
690 else
691 {
692 conn->mark_mask = 0xffffffff;
693 }
694 if (*kw->value == '\0')
695 {
696 conn->mark_value = 0;
697 }
698 else
699 {
700 conn->mark_value = strtoul(kw->value, &endptr, 0);
701 if (*endptr != '\0')
702 {
703 plog("# invalid mark value: %s", kw->value);
704 cfg->err++;
705 }
706 }
707 break;
708 }
709 case KW_KEYINGTRIES:
710 if (streq(kw->value, "%forever"))
711 {
712 conn->sa_keying_tries = 0;
713 }
714 else
715 {
716 char *endptr;
717
718 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
719 if (*endptr != '\0')
720 {
721 plog("# bad integer value: %s=%s", kw->entry->name, kw->value);
722 cfg->err++;
723 }
724 }
725 break;
726 case KW_REKEY:
727 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
728 break;
729 case KW_REAUTH:
730 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
731 break;
732 case KW_MOBIKE:
733 KW_POLICY_FLAG("yes", "no", POLICY_MOBIKE)
734 break;
735 case KW_FORCEENCAPS:
736 KW_POLICY_FLAG("yes", "no", POLICY_FORCE_ENCAP)
737 break;
738 case KW_MODECONFIG:
739 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
740 break;
741 case KW_XAUTH:
742 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
743 break;
744 default:
745 break;
746 }
747 }
748
749 handle_dns_failure("left", &conn->left, cfg);
750 handle_dns_failure("right", &conn->right, cfg);
751 handle_firewall("left", &conn->left, cfg);
752 handle_firewall("right", &conn->right, cfg);
753 }
754
755 /*
756 * initialize a conn object with the default conn
757 */
758 static void conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
759 {
760 memcpy(conn, def, sizeof(starter_conn_t));
761 conn->name = clone_str(name);
762
763 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
764 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
765 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
766 }
767
768 /*
769 * parse a ca section
770 */
771 static void load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
772 {
773 char *ca_name = (ca->name == NULL)? "%default":ca->name;
774
775 for ( ; kw; kw = kw->next)
776 {
777 bool assigned = FALSE;
778
779 kw_token_t token = kw->entry->token;
780
781 if (token == KW_AUTO)
782 {
783 token = KW_CA_SETUP;
784 }
785 else if (token == KW_ALSO)
786 {
787 if (cfg->parse_also)
788 {
789 also_t *also = malloc_thing(also_t);
790
791 also->name = clone_str(kw->value);
792 also->next = ca->also;
793 ca->also = also;
794
795 DBG(DBG_CONTROL,
796 DBG_log(" also=%s", kw->value)
797 )
798 }
799 continue;
800 }
801
802 if (token < KW_CA_FIRST || token > KW_CA_LAST)
803 {
804 plog("# unsupported keyword '%s' in ca '%s'", kw->entry->name, ca_name);
805 cfg->err++;
806 continue;
807 }
808
809 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
810 {
811 plog(" bad argument value in ca '%s'", ca_name);
812 cfg->err++;
813 }
814 }
815
816 /* treat 'route' and 'start' as 'add' */
817 if (ca->startup != STARTUP_NO)
818 ca->startup = STARTUP_ADD;
819 }
820
821 /*
822 * initialize a ca object with the default ca
823 */
824 static void ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
825 {
826 memcpy(ca, def, sizeof(starter_ca_t));
827 ca->name = clone_str(name);
828
829 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
830 }
831
832 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
833 starter_config_t *cfg);
834
835 static void load_also_conns(starter_conn_t *conn, also_t *also,
836 starter_config_t *cfg)
837 {
838 while (also != NULL)
839 {
840 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
841
842 if (kw == NULL)
843 {
844 plog(" conn '%s' cannot include '%s'", conn->name, also->name);
845 }
846 else
847 {
848 DBG(DBG_CONTROL,
849 DBG_log("conn '%s' includes '%s'", conn->name, also->name)
850 )
851 /* only load if no error occurred in the first round */
852 if (cfg->err == 0)
853 load_conn(conn, kw, cfg);
854 }
855 also = also->next;
856 }
857 }
858
859 /*
860 * find a conn included by also
861 */
862 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
863 starter_config_t *cfg)
864 {
865 starter_conn_t *c = cfg->conn_first;
866
867 while (c != NULL)
868 {
869 if (streq(name, c->name))
870 {
871 if (conn->visit == c->visit)
872 {
873 plog("# detected also loop");
874 cfg->err++;
875 return NULL;
876 }
877 c->visit = conn->visit;
878 load_also_conns(conn, c->also, cfg);
879 return c->kw;
880 }
881 c = c->next;
882 }
883
884 plog("# also '%s' not found", name);
885 cfg->err++;
886 return NULL;
887 }
888
889 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
890 starter_config_t *cfg);
891
892 static void load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
893 {
894 while (also != NULL)
895 {
896 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
897
898 if (kw == NULL)
899 {
900 plog(" ca '%s' cannot include '%s'", ca->name, also->name);
901 }
902 else
903 {
904 DBG(DBG_CONTROL,
905 DBG_log("ca '%s' includes '%s'", ca->name, also->name)
906 )
907 /* only load if no error occurred in the first round */
908 if (cfg->err == 0)
909 load_ca(ca, kw, cfg);
910 }
911 also = also->next;
912 }
913 }
914
915 /*
916 * find a ca included by also
917 */
918 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
919 starter_config_t *cfg)
920 {
921 starter_ca_t *c = cfg->ca_first;
922
923 while (c != NULL)
924 {
925 if (streq(name, c->name))
926 {
927 if (ca->visit == c->visit)
928 {
929 plog("# detected also loop");
930 cfg->err++;
931 return NULL;
932 }
933 c->visit = ca->visit;
934 load_also_cas(ca, c->also, cfg);
935 return c->kw;
936 }
937 c = c->next;
938 }
939
940 plog("# also '%s' not found", name);
941 cfg->err++;
942 return NULL;
943 }
944
945 /*
946 * free the memory used by also_t objects
947 */
948 static void free_also(also_t *head)
949 {
950 while (head != NULL)
951 {
952 also_t *also = head;
953
954 head = also->next;
955 free(also->name);
956 free(also);
957 }
958 }
959
960 /*
961 * free the memory used by a starter_conn_t object
962 */
963 static void confread_free_conn(starter_conn_t *conn)
964 {
965 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
966 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
967 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
968 free_also(conn->also);
969 }
970
971 /*
972 * free the memory used by a starter_ca_t object
973 */
974 static void
975 confread_free_ca(starter_ca_t *ca)
976 {
977 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
978 free_also(ca->also);
979 }
980
981 /*
982 * free the memory used by a starter_config_t object
983 */
984 void confread_free(starter_config_t *cfg)
985 {
986 starter_conn_t *conn = cfg->conn_first;
987 starter_ca_t *ca = cfg->ca_first;
988
989 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
990
991 confread_free_conn(&cfg->conn_default);
992
993 while (conn != NULL)
994 {
995 starter_conn_t *conn_aux = conn;
996
997 conn = conn->next;
998 confread_free_conn(conn_aux);
999 free(conn_aux);
1000 }
1001
1002 confread_free_ca(&cfg->ca_default);
1003
1004 while (ca != NULL)
1005 {
1006 starter_ca_t *ca_aux = ca;
1007
1008 ca = ca->next;
1009 confread_free_ca(ca_aux);
1010 free(ca_aux);
1011 }
1012
1013 free(cfg);
1014 }
1015
1016 /*
1017 * load and parse an IPsec configuration file
1018 */
1019 starter_config_t* confread_load(const char *file)
1020 {
1021 starter_config_t *cfg = NULL;
1022 config_parsed_t *cfgp;
1023 section_list_t *sconn, *sca;
1024 starter_conn_t *conn;
1025 starter_ca_t *ca;
1026
1027 u_int total_err;
1028 u_int visit = 0;
1029
1030 /* load IPSec configuration file */
1031 cfgp = parser_load_conf(file);
1032 if (!cfgp)
1033 {
1034 return NULL;
1035 }
1036 cfg = malloc_thing(starter_config_t);
1037
1038 /* set default values */
1039 default_values(cfg);
1040
1041 /* determine default route */
1042 get_defaultroute(&cfg->defaultroute);
1043
1044 /* load config setup section */
1045 load_setup(cfg, cfgp);
1046
1047 /* in the first round parse also statements */
1048 cfg->parse_also = TRUE;
1049
1050 /* find %default ca section */
1051 for (sca = cfgp->ca_first; sca; sca = sca->next)
1052 {
1053 if (streq(sca->name, "%default"))
1054 {
1055 DBG(DBG_CONTROL,
1056 DBG_log("Loading ca %%default")
1057 )
1058 load_ca(&cfg->ca_default, sca->kw, cfg);
1059 }
1060 }
1061
1062 /* parameters defined in ca %default sections can be overloads */
1063 cfg->ca_default.seen = LEMPTY;
1064
1065 /* load other ca sections */
1066 for (sca = cfgp->ca_first; sca; sca = sca->next)
1067 {
1068 u_int previous_err;
1069
1070 /* skip %default ca section */
1071 if (streq(sca->name, "%default"))
1072 continue;
1073
1074 DBG(DBG_CONTROL,
1075 DBG_log("Loading ca '%s'", sca->name)
1076 )
1077 ca = malloc_thing(starter_ca_t);
1078
1079 ca_default(sca->name, ca, &cfg->ca_default);
1080 ca->kw = sca->kw;
1081 ca->next = NULL;
1082
1083 previous_err = cfg->err;
1084 load_ca(ca, ca->kw, cfg);
1085 if (cfg->err > previous_err)
1086 {
1087 /* errors occurred - free the ca */
1088 confread_free_ca(ca);
1089 cfg->non_fatal_err += cfg->err - previous_err;
1090 cfg->err = previous_err;
1091 }
1092 else
1093 {
1094 /* success - insert the ca into the chained list */
1095 if (cfg->ca_last)
1096 cfg->ca_last->next = ca;
1097 cfg->ca_last = ca;
1098 if (!cfg->ca_first)
1099 cfg->ca_first = ca;
1100 }
1101 }
1102
1103 for (ca = cfg->ca_first; ca; ca = ca->next)
1104 {
1105 also_t *also = ca->also;
1106
1107 while (also != NULL)
1108 {
1109 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
1110
1111 load_ca(ca, kw, cfg);
1112 also = also->next;
1113 }
1114
1115 if (ca->startup != STARTUP_NO)
1116 ca->state = STATE_TO_ADD;
1117 }
1118
1119 /* find %default conn sections */
1120 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1121 {
1122 if (streq(sconn->name, "%default"))
1123 {
1124 DBG(DBG_CONTROL,
1125 DBG_log("Loading conn %%default")
1126 )
1127 load_conn(&cfg->conn_default, sconn->kw, cfg);
1128 }
1129 }
1130
1131 /* parameter defined in conn %default sections can be overloaded */
1132 cfg->conn_default.seen = LEMPTY;
1133 cfg->conn_default.right.seen = LEMPTY;
1134 cfg->conn_default.left.seen = LEMPTY;
1135
1136 /* load other conn sections */
1137 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1138 {
1139 u_int previous_err;
1140
1141 /* skip %default conn section */
1142 if (streq(sconn->name, "%default"))
1143 continue;
1144
1145 DBG(DBG_CONTROL,
1146 DBG_log("Loading conn '%s'", sconn->name)
1147 )
1148 conn = malloc_thing(starter_conn_t);
1149
1150 conn_default(sconn->name, conn, &cfg->conn_default);
1151 conn->kw = sconn->kw;
1152 conn->next = NULL;
1153
1154 previous_err = cfg->err;
1155 load_conn(conn, conn->kw, cfg);
1156 if (cfg->err > previous_err)
1157 {
1158 /* error occurred - free the conn */
1159 confread_free_conn(conn);
1160 cfg->non_fatal_err += cfg->err - previous_err;
1161 cfg->err = previous_err;
1162 }
1163 else
1164 {
1165 /* success - insert the conn into the chained list */
1166 if (cfg->conn_last)
1167 cfg->conn_last->next = conn;
1168 cfg->conn_last = conn;
1169 if (!cfg->conn_first)
1170 cfg->conn_first = conn;
1171 }
1172 }
1173
1174 /* in the second round do not parse also statements */
1175 cfg->parse_also = FALSE;
1176
1177 for (ca = cfg->ca_first; ca; ca = ca->next)
1178 {
1179 ca->visit = ++visit;
1180 load_also_cas(ca, ca->also, cfg);
1181
1182 if (ca->startup != STARTUP_NO)
1183 ca->state = STATE_TO_ADD;
1184 }
1185
1186 for (conn = cfg->conn_first; conn; conn = conn->next)
1187 {
1188 conn->visit = ++visit;
1189 load_also_conns(conn, conn->also, cfg);
1190
1191 if (conn->startup != STARTUP_NO)
1192 conn->state = STATE_TO_ADD;
1193 }
1194
1195 parser_free_conf(cfgp);
1196
1197 total_err = cfg->err + cfg->non_fatal_err;
1198 if (total_err > 0)
1199 {
1200 plog("### %d parsing error%s (%d fatal) ###"
1201 , total_err, (total_err > 1)?"s":"", cfg->err);
1202 }
1203
1204 return cfg;
1205 }