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