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