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