already had the correct formatting
[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
65 #ifdef START_CHARON
66 cfg->setup.charonstart = TRUE;
67 #endif
68 #ifdef START_PLUTO
69 cfg->setup.plutostart = TRUE;
70 #endif
71
72 cfg->conn_default.seen = LEMPTY;
73 cfg->conn_default.startup = STARTUP_NO;
74 cfg->conn_default.state = STATE_IGNORE;
75 cfg->conn_default.policy = POLICY_ENCRYPT | POLICY_TUNNEL | POLICY_RSASIG |
76 POLICY_PFS | POLICY_MOBIKE;
77
78 cfg->conn_default.ike = clone_str(ike_defaults);
79 cfg->conn_default.esp = clone_str(esp_defaults);
80 cfg->conn_default.sa_ike_life_seconds = OAKLEY_ISAKMP_SA_LIFETIME_DEFAULT;
81 cfg->conn_default.sa_ipsec_life_seconds = PLUTO_SA_LIFE_DURATION_DEFAULT;
82 cfg->conn_default.sa_rekey_margin = SA_REPLACEMENT_MARGIN_DEFAULT;
83 cfg->conn_default.sa_rekey_fuzz = SA_REPLACEMENT_FUZZ_DEFAULT;
84 cfg->conn_default.sa_keying_tries = SA_REPLACEMENT_RETRIES_DEFAULT;
85 cfg->conn_default.addr_family = AF_INET;
86 cfg->conn_default.tunnel_addr_family = AF_INET;
87 cfg->conn_default.install_policy = TRUE;
88 cfg->conn_default.dpd_delay = 30; /* seconds */
89 cfg->conn_default.dpd_timeout = 150; /* seconds */
90
91 cfg->conn_default.left.seen = LEMPTY;
92 cfg->conn_default.right.seen = LEMPTY;
93
94 cfg->conn_default.left.sendcert = CERT_SEND_IF_ASKED;
95 cfg->conn_default.right.sendcert = CERT_SEND_IF_ASKED;
96
97 anyaddr(AF_INET, &cfg->conn_default.left.addr);
98 anyaddr(AF_INET, &cfg->conn_default.left.nexthop);
99 anyaddr(AF_INET, &cfg->conn_default.right.addr);
100 anyaddr(AF_INET, &cfg->conn_default.right.nexthop);
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
111 load_setup(starter_config_t *cfg, config_parsed_t *cfgp)
112 {
113 kw_list_t *kw;
114
115 DBG(DBG_CONTROL,
116 DBG_log("Loading config setup")
117 )
118
119 for (kw = cfgp->config_setup; kw; kw = kw->next)
120 {
121 bool assigned = FALSE;
122
123 kw_token_t token = kw->entry->token;
124
125 if (token < KW_SETUP_FIRST || token > KW_SETUP_LAST)
126 {
127 plog("# unsupported keyword '%s' in config setup", kw->entry->name);
128 cfg->err++;
129 continue;
130 }
131
132 if (!assign_arg(token, KW_SETUP_FIRST, kw, (char *)cfg, &assigned))
133 {
134 plog(" bad argument value in config setup");
135 cfg->err++;
136 continue;
137 }
138 }
139 }
140
141 static void
142 kw_end(starter_conn_t *conn, starter_end_t *end, kw_token_t token
143 , kw_list_t *kw, char *conn_name, starter_config_t *cfg)
144 {
145 err_t ugh = NULL;
146 bool assigned = FALSE;
147 int has_port_wildcard; /* set if port is %any */
148
149 char *name = kw->entry->name;
150 char *value = kw->value;
151
152 if (!assign_arg(token, KW_END_FIRST, kw, (char *)end, &assigned))
153 goto err;
154
155 /* post processing of some keywords that were assigned automatically */
156 switch (token)
157 {
158 case KW_SUBNET:
159 if ((strlen(value) >= 6 && strncmp(value,"vhost:",6) == 0)
160 || (strlen(value) >= 5 && strncmp(value,"vnet:",5) == 0))
161 {
162 /* used by pluto only */
163 end->has_virt = TRUE;
164 }
165 else
166 {
167 ip_subnet net;
168 char *pos;
169 int len = 0;
170
171 end->has_client = TRUE;
172 conn->tunnel_addr_family = ip_version(value);
173
174 pos = strchr(value, ',');
175 if (pos)
176 {
177 len = pos - value;
178 }
179 ugh = ttosubnet(value, len, ip_version(value), &net);
180 if (ugh != NULL)
181 {
182 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
183 goto err;
184 }
185 }
186 break;
187 case KW_SOURCEIP:
188 if (end->has_natip)
189 {
190 plog("# natip and sourceip cannot be defined at the same time");
191 goto err;
192 }
193 if (streq(value, "%modeconfig") || streq(value, "%modecfg") ||
194 streq(value, "%config") || streq(value, "%cfg"))
195 {
196 free(end->srcip);
197 end->srcip = NULL;
198 end->modecfg = TRUE;
199 }
200 else
201 {
202 ip_address addr;
203 ip_subnet net;
204
205 conn->tunnel_addr_family = ip_version(value);
206 if (strchr(value, '/'))
207 { /* CIDR notation, address pool */
208 ugh = ttosubnet(value, 0, conn->tunnel_addr_family, &net);
209 }
210 else if (value[0] != '%')
211 { /* old style fixed srcip, a %poolname otherwise */
212 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
213 }
214 if (ugh != NULL)
215 {
216 plog("# bad addr: %s=%s [%s]", name, value, ugh);
217 goto err;
218 }
219 }
220 conn->policy |= POLICY_TUNNEL;
221 break;
222 case KW_SENDCERT:
223 if (end->sendcert == CERT_YES_SEND)
224 {
225 end->sendcert = CERT_ALWAYS_SEND;
226 }
227 else if (end->sendcert == CERT_NO_SEND)
228 {
229 end->sendcert = CERT_NEVER_SEND;
230 }
231 break;
232 default:
233 break;
234 }
235
236 if (assigned)
237 return;
238
239 /* individual processing of keywords that were not assigned automatically */
240 switch (token)
241 {
242 case KW_HOST:
243 if (streq(value, "%defaultroute"))
244 {
245 if (cfg->defaultroute.defined)
246 {
247 end->addr = cfg->defaultroute.addr;
248 end->nexthop = cfg->defaultroute.nexthop;
249 }
250 else
251 {
252 plog("# default route not known: %s=%s", name, value);
253 goto err;
254 }
255 }
256 else if (streq(value, "%any") || streq(value, "%any4"))
257 {
258 anyaddr(conn->addr_family, &end->addr);
259 }
260 else if (streq(value, "%any6"))
261 {
262 conn->addr_family = AF_INET6;
263 anyaddr(conn->addr_family, &end->addr);
264 }
265 else if (streq(value, "%group"))
266 {
267 ip_address any;
268
269 conn->policy |= POLICY_GROUP | POLICY_TUNNEL;
270 anyaddr(conn->addr_family, &end->addr);
271 anyaddr(conn->tunnel_addr_family, &any);
272 end->has_client = TRUE;
273 }
274 else
275 {
276 /* check for allow_any prefix */
277 if (value[0] == '%')
278 {
279 end->allow_any = TRUE;
280 value++;
281 }
282 conn->addr_family = ip_version(value);
283 ugh = ttoaddr(value, 0, conn->addr_family, &end->addr);
284 if (ugh != NULL)
285 {
286 plog("# bad addr: %s=%s [%s]", name, value, ugh);
287 if (streq(ugh, "does not look numeric and name lookup failed"))
288 {
289 end->dns_failed = TRUE;
290 anyaddr(conn->addr_family, &end->addr);
291 }
292 else
293 {
294 goto err;
295 }
296 }
297 }
298 break;
299 case KW_NEXTHOP:
300 if (streq(value, "%defaultroute"))
301 {
302 if (cfg->defaultroute.defined)
303 end->nexthop = cfg->defaultroute.nexthop;
304 else
305 {
306 plog("# default route not known: %s=%s", name, value);
307 goto err;
308 }
309 }
310 else if (streq(value, "%direct"))
311 {
312 ugh = anyaddr(conn->addr_family, &end->nexthop);
313 }
314 else
315 {
316 conn->addr_family = ip_version(value);
317 ugh = ttoaddr(value, 0, conn->addr_family, &end->nexthop);
318 }
319 if (ugh != NULL)
320 {
321 plog("# bad addr: %s=%s [%s]", name, value, ugh);
322 goto err;
323 }
324 break;
325 case KW_SUBNETWITHIN:
326 {
327 ip_subnet net;
328
329 end->has_client = TRUE;
330 end->has_client_wildcard = TRUE;
331 conn->tunnel_addr_family = ip_version(value);
332
333 ugh = ttosubnet(value, 0, ip_version(value), &net);
334 if (ugh != NULL)
335 {
336 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
337 goto err;
338 }
339 end->subnet = clone_str(value);
340 break;
341 }
342 case KW_PROTOPORT:
343 ugh = ttoprotoport(value, 0, &end->protocol, &end->port, &has_port_wildcard);
344 end->has_port_wildcard = has_port_wildcard;
345 break;
346 case KW_NATIP:
347 if (end->srcip)
348 {
349 plog("# natip and sourceip cannot be defined at the same time");
350 goto err;
351 }
352 if (streq(value, "%defaultroute"))
353 {
354 char buf[64];
355
356 if (cfg->defaultroute.defined)
357 {
358 addrtot(&cfg->defaultroute.addr, 0, buf, sizeof(buf));
359 end->srcip = clone_str(buf);
360 }
361 else
362 {
363 plog("# default route not known: %s=%s", name, value);
364 goto err;
365 }
366 }
367 else
368 {
369 ip_address addr;
370
371 conn->tunnel_addr_family = ip_version(value);
372 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
373 if (ugh != NULL)
374 {
375 plog("# bad addr: %s=%s [%s]", name, value, ugh);
376 goto err;
377 }
378 end->srcip = clone_str(value);
379 }
380 end->has_natip = TRUE;
381 conn->policy |= POLICY_TUNNEL;
382 break;
383 default:
384 break;
385 }
386 return;
387
388 err:
389 plog(" bad argument value in conn '%s'", conn_name);
390 cfg->err++;
391 }
392
393 /*
394 * handles left|right=<FQDN> DNS resolution failure
395 */
396 static void
397 handle_dns_failure( const char *label, starter_end_t *end, starter_config_t *cfg)
398 {
399 if (end->dns_failed)
400 {
401 if (end->allow_any)
402 {
403 plog("# fallback to %s=%%any due to '%%' prefix or %sallowany=yes",
404 label, label);
405 }
406 else
407 {
408 /* declare an error */
409 cfg->err++;
410 }
411 }
412 }
413
414 /*
415 * handles left|rightfirewall and left|rightupdown parameters
416 */
417 static void
418 handle_firewall( const char *label, starter_end_t *end, starter_config_t *cfg)
419 {
420 if (end->firewall && (end->seen & LELEM(KW_FIREWALL - KW_END_FIRST)))
421 {
422 if (end->updown != NULL)
423 {
424 plog("# cannot have both %sfirewall and %supdown", label, label);
425 cfg->err++;
426 }
427 else
428 {
429 end->updown = clone_str(firewall_defaults);
430 end->firewall = FALSE;
431 }
432 }
433 }
434
435 /*
436 * parse a conn section
437 */
438 static void
439 load_conn(starter_conn_t *conn, kw_list_t *kw, starter_config_t *cfg)
440 {
441 char *conn_name = (conn->name == NULL)? "%default":conn->name;
442
443 for ( ; kw; kw = kw->next)
444 {
445 bool assigned = FALSE;
446
447 kw_token_t token = kw->entry->token;
448
449 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
450 {
451 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST
452 , kw, conn_name, cfg);
453 continue;
454 }
455 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
456 {
457 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST
458 , kw, conn_name, cfg);
459 continue;
460 }
461
462 if (token == KW_AUTO)
463 {
464 token = KW_CONN_SETUP;
465 }
466 else if (token == KW_ALSO)
467 {
468 if (cfg->parse_also)
469 {
470 also_t *also = malloc_thing(also_t);
471
472 also->name = clone_str(kw->value);
473 also->next = conn->also;
474 conn->also = also;
475
476 DBG(DBG_CONTROL,
477 DBG_log(" also=%s", kw->value)
478 )
479 }
480 continue;
481 }
482
483 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
484 {
485 plog("# unsupported keyword '%s' in conn '%s'"
486 , kw->entry->name, conn_name);
487 cfg->err++;
488 continue;
489 }
490
491 if (!assign_arg(token, KW_CONN_FIRST, kw, (char *)conn, &assigned))
492 {
493 plog(" bad argument value in conn '%s'", conn_name);
494 cfg->err++;
495 continue;
496 }
497
498 if (assigned)
499 continue;
500
501 switch (token)
502 {
503 case KW_TYPE:
504 conn->policy &= ~(POLICY_TUNNEL | POLICY_SHUNT_MASK);
505 if (streq(kw->value, "tunnel"))
506 {
507 conn->policy |= POLICY_TUNNEL;
508 }
509 else if (streq(kw->value, "beet"))
510 {
511 conn->policy |= POLICY_BEET;
512 }
513 else if (streq(kw->value, "transport_proxy"))
514 {
515 conn->policy |= POLICY_PROXY;
516 }
517 else if (streq(kw->value, "passthrough") || streq(kw->value, "pass"))
518 {
519 conn->policy |= POLICY_SHUNT_PASS;
520 }
521 else if (streq(kw->value, "drop"))
522 {
523 conn->policy |= POLICY_SHUNT_DROP;
524 }
525 else if (streq(kw->value, "reject"))
526 {
527 conn->policy |= POLICY_SHUNT_REJECT;
528 }
529 else if (strcmp(kw->value, "transport") != 0)
530 {
531 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
532 cfg->err++;
533 }
534 break;
535 case KW_PFS:
536 KW_POLICY_FLAG("yes", "no", POLICY_PFS)
537 break;
538 case KW_COMPRESS:
539 KW_POLICY_FLAG("yes", "no", POLICY_COMPRESS)
540 break;
541 case KW_AUTH:
542 KW_POLICY_FLAG("ah", "esp", POLICY_AUTHENTICATE)
543 break;
544 case KW_AUTHBY:
545 conn->policy &= ~(POLICY_ID_AUTH_MASK | POLICY_ENCRYPT);
546
547 if (!(streq(kw->value, "never") || streq(kw->value, "eap")))
548 {
549 char *value = kw->value;
550 char *second = strchr(kw->value, '|');
551
552 if (second != NULL)
553 {
554 *second = '\0';
555 }
556
557 /* also handles the cases secret|rsasig and rsasig|secret */
558 for (;;)
559 {
560 if (streq(value, "rsa") || streq(value, "rsasig"))
561 {
562 conn->policy |= POLICY_RSASIG | 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, "ecdsa") || streq(value, "ecdsasig"))
569 {
570 conn->policy |= POLICY_ECDSASIG | 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 }