Complain about missing %defaultroute support only if one is actually used
[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
100 cfg->ca_default.seen = LEMPTY;
101 }
102
103 #define KW_POLICY_FLAG(sy, sn, fl) \
104 if (streq(kw->value, sy)) { conn->policy |= fl; } \
105 else if (streq(kw->value, sn)) { conn->policy &= ~fl; } \
106 else { plog("# bad policy value: %s=%s", kw->entry->name, kw->value); cfg->err++; }
107
108 static void
109 load_setup(starter_config_t *cfg, config_parsed_t *cfgp)
110 {
111 kw_list_t *kw;
112
113 DBG(DBG_CONTROL,
114 DBG_log("Loading config setup")
115 )
116
117 for (kw = cfgp->config_setup; kw; kw = kw->next)
118 {
119 bool assigned = FALSE;
120
121 kw_token_t token = kw->entry->token;
122
123 if (token < KW_SETUP_FIRST || token > KW_SETUP_LAST)
124 {
125 plog("# unsupported keyword '%s' in config setup", kw->entry->name);
126 cfg->err++;
127 continue;
128 }
129
130 if (!assign_arg(token, KW_SETUP_FIRST, kw, (char *)cfg, &assigned))
131 {
132 plog(" bad argument value in config setup");
133 cfg->err++;
134 continue;
135 }
136 }
137 }
138
139 static void
140 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 (streq(value, "%modeconfig") || streq(value, "%modecfg") ||
192 streq(value, "%config") || streq(value, "%cfg"))
193 {
194 free(end->srcip);
195 end->srcip = NULL;
196 end->modecfg = TRUE;
197 }
198 else
199 {
200 ip_address addr;
201 ip_subnet net;
202
203 conn->tunnel_addr_family = ip_version(value);
204 if (strchr(value, '/'))
205 { /* CIDR notation, address pool */
206 ugh = ttosubnet(value, 0, conn->tunnel_addr_family, &net);
207 }
208 else if (value[0] != '%')
209 { /* old style fixed srcip, a %poolname otherwise */
210 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
211 }
212 if (ugh != NULL)
213 {
214 plog("# bad addr: %s=%s [%s]", name, value, ugh);
215 goto err;
216 }
217 }
218 conn->policy |= POLICY_TUNNEL;
219 break;
220 case KW_SENDCERT:
221 if (end->sendcert == CERT_YES_SEND)
222 {
223 end->sendcert = CERT_ALWAYS_SEND;
224 }
225 else if (end->sendcert == CERT_NO_SEND)
226 {
227 end->sendcert = CERT_NEVER_SEND;
228 }
229 break;
230 default:
231 break;
232 }
233
234 if (assigned)
235 return;
236
237 /* individual processing of keywords that were not assigned automatically */
238 switch (token)
239 {
240 case KW_HOST:
241 if (streq(value, "%defaultroute"))
242 {
243 if (cfg->defaultroute.defined)
244 {
245 end->addr = cfg->defaultroute.addr;
246 end->nexthop = cfg->defaultroute.nexthop;
247 }
248 else if (!cfg->defaultroute.supported)
249 {
250 plog("%%defaultroute not supported, fallback to %%any");
251 }
252 else
253 {
254 plog("# default route not known: %s=%s", name, value);
255 goto err;
256 }
257 }
258 else if (streq(value, "%any") || streq(value, "%any4"))
259 {
260 anyaddr(conn->addr_family, &end->addr);
261 }
262 else if (streq(value, "%any6"))
263 {
264 conn->addr_family = AF_INET6;
265 anyaddr(conn->addr_family, &end->addr);
266 }
267 else if (streq(value, "%group"))
268 {
269 ip_address any;
270
271 conn->policy |= POLICY_GROUP | POLICY_TUNNEL;
272 anyaddr(conn->addr_family, &end->addr);
273 anyaddr(conn->tunnel_addr_family, &any);
274 end->has_client = TRUE;
275 }
276 else
277 {
278 /* check for allow_any prefix */
279 if (value[0] == '%')
280 {
281 end->allow_any = TRUE;
282 value++;
283 }
284 conn->addr_family = ip_version(value);
285 ugh = ttoaddr(value, 0, conn->addr_family, &end->addr);
286 if (ugh != NULL)
287 {
288 plog("# bad addr: %s=%s [%s]", name, value, ugh);
289 if (streq(ugh, "does not look numeric and name lookup failed"))
290 {
291 end->dns_failed = TRUE;
292 anyaddr(conn->addr_family, &end->addr);
293 }
294 else
295 {
296 goto err;
297 }
298 }
299 }
300 break;
301 case KW_NEXTHOP:
302 if (streq(value, "%defaultroute"))
303 {
304 if (cfg->defaultroute.defined)
305 end->nexthop = cfg->defaultroute.nexthop;
306 else
307 {
308 plog("# default route not known: %s=%s", name, value);
309 goto err;
310 }
311 }
312 else if (streq(value, "%direct"))
313 {
314 ugh = anyaddr(conn->addr_family, &end->nexthop);
315 }
316 else
317 {
318 conn->addr_family = ip_version(value);
319 ugh = ttoaddr(value, 0, conn->addr_family, &end->nexthop);
320 }
321 if (ugh != NULL)
322 {
323 plog("# bad addr: %s=%s [%s]", name, value, ugh);
324 goto err;
325 }
326 break;
327 case KW_SUBNETWITHIN:
328 {
329 ip_subnet net;
330
331 end->has_client = TRUE;
332 end->has_client_wildcard = TRUE;
333 conn->tunnel_addr_family = ip_version(value);
334
335 ugh = ttosubnet(value, 0, ip_version(value), &net);
336 if (ugh != NULL)
337 {
338 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
339 goto err;
340 }
341 end->subnet = clone_str(value);
342 break;
343 }
344 case KW_PROTOPORT:
345 ugh = ttoprotoport(value, 0, &end->protocol, &end->port, &has_port_wildcard);
346 end->has_port_wildcard = has_port_wildcard;
347 break;
348 case KW_NATIP:
349 if (end->srcip)
350 {
351 plog("# natip and sourceip cannot be defined at the same time");
352 goto err;
353 }
354 if (streq(value, "%defaultroute"))
355 {
356 char buf[64];
357
358 if (cfg->defaultroute.defined)
359 {
360 addrtot(&cfg->defaultroute.addr, 0, buf, sizeof(buf));
361 end->srcip = clone_str(buf);
362 }
363 else
364 {
365 plog("# default route not known: %s=%s", name, value);
366 goto err;
367 }
368 }
369 else
370 {
371 ip_address addr;
372
373 conn->tunnel_addr_family = ip_version(value);
374 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
375 if (ugh != NULL)
376 {
377 plog("# bad addr: %s=%s [%s]", name, value, ugh);
378 goto err;
379 }
380 end->srcip = clone_str(value);
381 }
382 end->has_natip = TRUE;
383 conn->policy |= POLICY_TUNNEL;
384 break;
385 default:
386 break;
387 }
388 return;
389
390 err:
391 plog(" bad argument value in conn '%s'", conn_name);
392 cfg->err++;
393 }
394
395 /*
396 * handles left|right=<FQDN> DNS resolution failure
397 */
398 static void
399 handle_dns_failure( const char *label, starter_end_t *end, starter_config_t *cfg)
400 {
401 if (end->dns_failed)
402 {
403 if (end->allow_any)
404 {
405 plog("# fallback to %s=%%any due to '%%' prefix or %sallowany=yes",
406 label, label);
407 }
408 else
409 {
410 /* declare an error */
411 cfg->err++;
412 }
413 }
414 }
415
416 /*
417 * handles left|rightfirewall and left|rightupdown parameters
418 */
419 static void
420 handle_firewall( const char *label, starter_end_t *end, starter_config_t *cfg)
421 {
422 if (end->firewall && (end->seen & LELEM(KW_FIREWALL - KW_END_FIRST)))
423 {
424 if (end->updown != NULL)
425 {
426 plog("# cannot have both %sfirewall and %supdown", label, label);
427 cfg->err++;
428 }
429 else
430 {
431 end->updown = clone_str(firewall_defaults);
432 end->firewall = FALSE;
433 }
434 }
435 }
436
437 /*
438 * parse a conn section
439 */
440 static void
441 load_conn(starter_conn_t *conn, kw_list_t *kw, starter_config_t *cfg)
442 {
443 char *conn_name = (conn->name == NULL)? "%default":conn->name;
444
445 for ( ; kw; kw = kw->next)
446 {
447 bool assigned = FALSE;
448
449 kw_token_t token = kw->entry->token;
450
451 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
452 {
453 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST
454 , kw, conn_name, cfg);
455 continue;
456 }
457 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
458 {
459 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST
460 , kw, conn_name, cfg);
461 continue;
462 }
463
464 if (token == KW_AUTO)
465 {
466 token = KW_CONN_SETUP;
467 }
468 else if (token == KW_ALSO)
469 {
470 if (cfg->parse_also)
471 {
472 also_t *also = malloc_thing(also_t);
473
474 also->name = clone_str(kw->value);
475 also->next = conn->also;
476 conn->also = also;
477
478 DBG(DBG_CONTROL,
479 DBG_log(" also=%s", kw->value)
480 )
481 }
482 continue;
483 }
484
485 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
486 {
487 plog("# unsupported keyword '%s' in conn '%s'"
488 , kw->entry->name, conn_name);
489 cfg->err++;
490 continue;
491 }
492
493 if (!assign_arg(token, KW_CONN_FIRST, kw, (char *)conn, &assigned))
494 {
495 plog(" bad argument value in conn '%s'", conn_name);
496 cfg->err++;
497 continue;
498 }
499
500 if (assigned)
501 continue;
502
503 switch (token)
504 {
505 case KW_TYPE:
506 conn->policy &= ~(POLICY_TUNNEL | POLICY_SHUNT_MASK);
507 if (streq(kw->value, "tunnel"))
508 {
509 conn->policy |= POLICY_TUNNEL;
510 }
511 else if (streq(kw->value, "beet"))
512 {
513 conn->policy |= POLICY_BEET;
514 }
515 else if (streq(kw->value, "transport_proxy"))
516 {
517 conn->policy |= POLICY_PROXY;
518 }
519 else if (streq(kw->value, "passthrough") || streq(kw->value, "pass"))
520 {
521 conn->policy |= POLICY_SHUNT_PASS;
522 }
523 else if (streq(kw->value, "drop"))
524 {
525 conn->policy |= POLICY_SHUNT_DROP;
526 }
527 else if (streq(kw->value, "reject"))
528 {
529 conn->policy |= POLICY_SHUNT_REJECT;
530 }
531 else if (strcmp(kw->value, "transport") != 0)
532 {
533 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
534 cfg->err++;
535 }
536 break;
537 case KW_PFS:
538 KW_POLICY_FLAG("yes", "no", POLICY_PFS)
539 break;
540 case KW_COMPRESS:
541 KW_POLICY_FLAG("yes", "no", POLICY_COMPRESS)
542 break;
543 case KW_AUTH:
544 KW_POLICY_FLAG("ah", "esp", POLICY_AUTHENTICATE)
545 break;
546 case KW_AUTHBY:
547 conn->policy &= ~(POLICY_ID_AUTH_MASK | POLICY_ENCRYPT);
548
549 if (!(streq(kw->value, "never") || streq(kw->value, "eap")))
550 {
551 char *value = kw->value;
552 char *second = strchr(kw->value, '|');
553
554 if (second != NULL)
555 {
556 *second = '\0';
557 }
558
559 /* also handles the cases secret|rsasig and rsasig|secret */
560 for (;;)
561 {
562 if (streq(value, "rsa") || streq(value, "rsasig") ||
563 streq(value, "ecdsa") || streq(value, "ecdsasig") ||
564 streq(value, "pubkey"))
565 {
566 conn->policy |= POLICY_PUBKEY | POLICY_ENCRYPT;
567 }
568 else if (streq(value, "secret") || streq(value, "psk"))
569 {
570 conn->policy |= POLICY_PSK | POLICY_ENCRYPT;
571 }
572 else if (streq(value, "xauthrsasig"))
573 {
574 conn->policy |= POLICY_XAUTH_RSASIG | POLICY_ENCRYPT;
575 }
576 else if (streq(value, "xauthpsk"))
577 {
578 conn->policy |= POLICY_XAUTH_PSK | POLICY_ENCRYPT;
579 }
580 else
581 {
582 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
583 cfg->err++;
584 break;
585 }
586 if (second == NULL)
587 {
588 break;
589 }
590 value = second;
591 second = NULL; /* traverse the loop no more than twice */
592 }
593 }
594 break;
595 case KW_EAP:
596 {
597 char *sep;
598
599 /* check for vendor-type format */
600 sep = strchr(kw->value, '-');
601 if (sep)
602 {
603 *(sep++) = '\0';
604 conn->eap_type = atoi(kw->value);
605 conn->eap_vendor = atoi(sep);
606 if (conn->eap_type == 0 || conn->eap_vendor == 0)
607 {
608 plog("# invalid EAP type: %s=%s", kw->entry->name, kw->value);
609 cfg->err++;
610 }
611 break;
612 }
613 if (streq(kw->value, "aka"))
614 {
615 conn->eap_type = 23;
616 }
617 else if (streq(kw->value, "sim"))
618 {
619 conn->eap_type = 18;
620 }
621 else if (streq(kw->value, "md5"))
622 {
623 conn->eap_type = 4;
624 }
625 else if (streq(kw->value, "gtc"))
626 {
627 conn->eap_type = 6;
628 }
629 else if (streq(kw->value, "mschapv2"))
630 {
631 conn->eap_type = 26;
632 }
633 else if (streq(kw->value, "radius"))
634 { /* pseudo-type */
635 conn->eap_type = 253;
636 }
637 else
638 {
639 conn->eap_type = atoi(kw->value);
640 if (conn->eap_type == 0)
641 {
642 plog("# unknown EAP type: %s=%s", kw->entry->name, kw->value);
643 cfg->err++;
644 }
645 }
646 break;
647 }
648 case KW_KEYINGTRIES:
649 if (streq(kw->value, "%forever"))
650 {
651 conn->sa_keying_tries = 0;
652 }
653 else
654 {
655 char *endptr;
656
657 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
658 if (*endptr != '\0')
659 {
660 plog("# bad integer value: %s=%s", kw->entry->name, kw->value);
661 cfg->err++;
662 }
663 }
664 break;
665 case KW_REKEY:
666 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
667 break;
668 case KW_REAUTH:
669 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
670 break;
671 case KW_MOBIKE:
672 KW_POLICY_FLAG("yes", "no", POLICY_MOBIKE)
673 break;
674 case KW_FORCEENCAPS:
675 KW_POLICY_FLAG("yes", "no", POLICY_FORCE_ENCAP)
676 break;
677 case KW_MODECONFIG:
678 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
679 break;
680 case KW_XAUTH:
681 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
682 break;
683 default:
684 break;
685 }
686 }
687
688 handle_dns_failure("left", &conn->left, cfg);
689 handle_dns_failure("right", &conn->right, cfg);
690 handle_firewall("left", &conn->left, cfg);
691 handle_firewall("right", &conn->right, cfg);
692 }
693
694 /*
695 * initialize a conn object with the default conn
696 */
697 static void
698 conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
699 {
700 memcpy(conn, def, sizeof(starter_conn_t));
701 conn->name = clone_str(name);
702
703 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
704 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
705 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
706 }
707
708 /*
709 * parse a ca section
710 */
711 static void
712 load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
713 {
714 char *ca_name = (ca->name == NULL)? "%default":ca->name;
715
716 for ( ; kw; kw = kw->next)
717 {
718 bool assigned = FALSE;
719
720 kw_token_t token = kw->entry->token;
721
722 if (token == KW_AUTO)
723 {
724 token = KW_CA_SETUP;
725 }
726 else if (token == KW_ALSO)
727 {
728 if (cfg->parse_also)
729 {
730 also_t *also = malloc_thing(also_t);
731
732 also->name = clone_str(kw->value);
733 also->next = ca->also;
734 ca->also = also;
735
736 DBG(DBG_CONTROL,
737 DBG_log(" also=%s", kw->value)
738 )
739 }
740 continue;
741 }
742
743 if (token < KW_CA_FIRST || token > KW_CA_LAST)
744 {
745 plog("# unsupported keyword '%s' in ca '%s'", kw->entry->name, ca_name);
746 cfg->err++;
747 continue;
748 }
749
750 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
751 {
752 plog(" bad argument value in ca '%s'", ca_name);
753 cfg->err++;
754 }
755 }
756
757 /* treat 'route' and 'start' as 'add' */
758 if (ca->startup != STARTUP_NO)
759 ca->startup = STARTUP_ADD;
760 }
761
762 /*
763 * initialize a ca object with the default ca
764 */
765 static void
766 ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
767 {
768 memcpy(ca, def, sizeof(starter_ca_t));
769 ca->name = clone_str(name);
770
771 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
772 }
773
774 static kw_list_t*
775 find_also_conn(const char* name, starter_conn_t *conn, starter_config_t *cfg);
776
777 static void
778 load_also_conns(starter_conn_t *conn, also_t *also, starter_config_t *cfg)
779 {
780 while (also != NULL)
781 {
782 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
783
784 if (kw == NULL)
785 {
786 plog(" conn '%s' cannot include '%s'", conn->name, also->name);
787 }
788 else
789 {
790 DBG(DBG_CONTROL,
791 DBG_log("conn '%s' includes '%s'", conn->name, also->name)
792 )
793 /* only load if no error occurred in the first round */
794 if (cfg->err == 0)
795 load_conn(conn, kw, cfg);
796 }
797 also = also->next;
798 }
799 }
800
801 /*
802 * find a conn included by also
803 */
804 static kw_list_t*
805 find_also_conn(const char* name, starter_conn_t *conn, starter_config_t *cfg)
806 {
807 starter_conn_t *c = cfg->conn_first;
808
809 while (c != NULL)
810 {
811 if (streq(name, c->name))
812 {
813 if (conn->visit == c->visit)
814 {
815 plog("# detected also loop");
816 cfg->err++;
817 return NULL;
818 }
819 c->visit = conn->visit;
820 load_also_conns(conn, c->also, cfg);
821 return c->kw;
822 }
823 c = c->next;
824 }
825
826 plog("# also '%s' not found", name);
827 cfg->err++;
828 return NULL;
829 }
830
831 static kw_list_t*
832 find_also_ca(const char* name, starter_ca_t *ca, starter_config_t *cfg);
833
834 static void
835 load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
836 {
837 while (also != NULL)
838 {
839 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
840
841 if (kw == NULL)
842 {
843 plog(" ca '%s' cannot include '%s'", ca->name, also->name);
844 }
845 else
846 {
847 DBG(DBG_CONTROL,
848 DBG_log("ca '%s' includes '%s'", ca->name, also->name)
849 )
850 /* only load if no error occurred in the first round */
851 if (cfg->err == 0)
852 load_ca(ca, kw, cfg);
853 }
854 also = also->next;
855 }
856 }
857
858 /*
859 * find a ca included by also
860 */
861 static kw_list_t*
862 find_also_ca(const char* name, starter_ca_t *ca, starter_config_t *cfg)
863 {
864 starter_ca_t *c = cfg->ca_first;
865
866 while (c != NULL)
867 {
868 if (streq(name, c->name))
869 {
870 if (ca->visit == c->visit)
871 {
872 plog("# detected also loop");
873 cfg->err++;
874 return NULL;
875 }
876 c->visit = ca->visit;
877 load_also_cas(ca, c->also, cfg);
878 return c->kw;
879 }
880 c = c->next;
881 }
882
883 plog("# also '%s' not found", name);
884 cfg->err++;
885 return NULL;
886 }
887
888 /*
889 * free the memory used by also_t objects
890 */
891 static void
892 free_also(also_t *head)
893 {
894 while (head != NULL)
895 {
896 also_t *also = head;
897
898 head = also->next;
899 free(also->name);
900 free(also);
901 }
902 }
903
904 /*
905 * free the memory used by a starter_conn_t object
906 */
907 static void
908 confread_free_conn(starter_conn_t *conn)
909 {
910 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
911 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
912 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
913 free_also(conn->also);
914 }
915
916 /*
917 * free the memory used by a starter_ca_t object
918 */
919 static void
920 confread_free_ca(starter_ca_t *ca)
921 {
922 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
923 free_also(ca->also);
924 }
925
926 /*
927 * free the memory used by a starter_config_t object
928 */
929 void
930 confread_free(starter_config_t *cfg)
931 {
932 starter_conn_t *conn = cfg->conn_first;
933 starter_ca_t *ca = cfg->ca_first;
934
935 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
936
937 confread_free_conn(&cfg->conn_default);
938
939 while (conn != NULL)
940 {
941 starter_conn_t *conn_aux = conn;
942
943 conn = conn->next;
944 confread_free_conn(conn_aux);
945 free(conn_aux);
946 }
947
948 confread_free_ca(&cfg->ca_default);
949
950 while (ca != NULL)
951 {
952 starter_ca_t *ca_aux = ca;
953
954 ca = ca->next;
955 confread_free_ca(ca_aux);
956 free(ca_aux);
957 }
958
959 free(cfg);
960 }
961
962 /*
963 * load and parse an IPsec configuration file
964 */
965 starter_config_t *
966 confread_load(const char *file)
967 {
968 starter_config_t *cfg = NULL;
969 config_parsed_t *cfgp;
970 section_list_t *sconn, *sca;
971 starter_conn_t *conn;
972 starter_ca_t *ca;
973
974 u_int total_err;
975 u_int visit = 0;
976
977 /* load IPSec configuration file */
978 cfgp = parser_load_conf(file);
979 if (!cfgp)
980 {
981 return NULL;
982 }
983 cfg = malloc_thing(starter_config_t);
984
985 /* set default values */
986 default_values(cfg);
987
988 /* determine default route */
989 get_defaultroute(&cfg->defaultroute);
990
991 /* load config setup section */
992 load_setup(cfg, cfgp);
993
994 /* in the first round parse also statements */
995 cfg->parse_also = TRUE;
996
997 /* find %default ca section */
998 for (sca = cfgp->ca_first; sca; sca = sca->next)
999 {
1000 if (streq(sca->name, "%default"))
1001 {
1002 DBG(DBG_CONTROL,
1003 DBG_log("Loading ca %%default")
1004 )
1005 load_ca(&cfg->ca_default, sca->kw, cfg);
1006 }
1007 }
1008
1009 /* parameters defined in ca %default sections can be overloads */
1010 cfg->ca_default.seen = LEMPTY;
1011
1012 /* load other ca sections */
1013 for (sca = cfgp->ca_first; sca; sca = sca->next)
1014 {
1015 u_int previous_err;
1016
1017 /* skip %default ca section */
1018 if (streq(sca->name, "%default"))
1019 continue;
1020
1021 DBG(DBG_CONTROL,
1022 DBG_log("Loading ca '%s'", sca->name)
1023 )
1024 ca = malloc_thing(starter_ca_t);
1025
1026 ca_default(sca->name, ca, &cfg->ca_default);
1027 ca->kw = sca->kw;
1028 ca->next = NULL;
1029
1030 previous_err = cfg->err;
1031 load_ca(ca, ca->kw, cfg);
1032 if (cfg->err > previous_err)
1033 {
1034 /* errors occurred - free the ca */
1035 confread_free_ca(ca);
1036 cfg->non_fatal_err += cfg->err - previous_err;
1037 cfg->err = previous_err;
1038 }
1039 else
1040 {
1041 /* success - insert the ca into the chained list */
1042 if (cfg->ca_last)
1043 cfg->ca_last->next = ca;
1044 cfg->ca_last = ca;
1045 if (!cfg->ca_first)
1046 cfg->ca_first = ca;
1047 }
1048 }
1049
1050 for (ca = cfg->ca_first; ca; ca = ca->next)
1051 {
1052 also_t *also = ca->also;
1053
1054 while (also != NULL)
1055 {
1056 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
1057
1058 load_ca(ca, kw, cfg);
1059 also = also->next;
1060 }
1061
1062 if (ca->startup != STARTUP_NO)
1063 ca->state = STATE_TO_ADD;
1064 }
1065
1066 /* find %default conn sections */
1067 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1068 {
1069 if (streq(sconn->name, "%default"))
1070 {
1071 DBG(DBG_CONTROL,
1072 DBG_log("Loading conn %%default")
1073 )
1074 load_conn(&cfg->conn_default, sconn->kw, cfg);
1075 }
1076 }
1077
1078 /* parameter defined in conn %default sections can be overloaded */
1079 cfg->conn_default.seen = LEMPTY;
1080 cfg->conn_default.right.seen = LEMPTY;
1081 cfg->conn_default.left.seen = LEMPTY;
1082
1083 /* load other conn sections */
1084 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1085 {
1086 u_int previous_err;
1087
1088 /* skip %default conn section */
1089 if (streq(sconn->name, "%default"))
1090 continue;
1091
1092 DBG(DBG_CONTROL,
1093 DBG_log("Loading conn '%s'", sconn->name)
1094 )
1095 conn = malloc_thing(starter_conn_t);
1096
1097 conn_default(sconn->name, conn, &cfg->conn_default);
1098 conn->kw = sconn->kw;
1099 conn->next = NULL;
1100
1101 previous_err = cfg->err;
1102 load_conn(conn, conn->kw, cfg);
1103 if (cfg->err > previous_err)
1104 {
1105 /* error occurred - free the conn */
1106 confread_free_conn(conn);
1107 cfg->non_fatal_err += cfg->err - previous_err;
1108 cfg->err = previous_err;
1109 }
1110 else
1111 {
1112 /* success - insert the conn into the chained list */
1113 if (cfg->conn_last)
1114 cfg->conn_last->next = conn;
1115 cfg->conn_last = conn;
1116 if (!cfg->conn_first)
1117 cfg->conn_first = conn;
1118 }
1119 }
1120
1121 /* in the second round do not parse also statements */
1122 cfg->parse_also = FALSE;
1123
1124 for (ca = cfg->ca_first; ca; ca = ca->next)
1125 {
1126 ca->visit = ++visit;
1127 load_also_cas(ca, ca->also, cfg);
1128
1129 if (ca->startup != STARTUP_NO)
1130 ca->state = STATE_TO_ADD;
1131 }
1132
1133 for (conn = cfg->conn_first; conn; conn = conn->next)
1134 {
1135 conn->visit = ++visit;
1136 load_also_conns(conn, conn->also, cfg);
1137
1138 if (conn->startup != STARTUP_NO)
1139 conn->state = STATE_TO_ADD;
1140 }
1141
1142 parser_free_conf(cfgp);
1143
1144 total_err = cfg->err + cfg->non_fatal_err;
1145 if (total_err > 0)
1146 {
1147 plog("### %d parsing error%s (%d fatal) ###"
1148 , total_err, (total_err > 1)?"s":"", cfg->err);
1149 }
1150
1151 return cfg;
1152 }