reintegrated eap-radius branch into trunk
[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 if (streq(kw->value, "radius"))
629 { /* pseudo-type */
630 conn->eap_type = 253;
631 }
632 else
633 {
634 conn->eap_type = atoi(kw->value);
635 if (conn->eap_type == 0)
636 {
637 plog("# unknown EAP type: %s=%s", kw->entry->name, kw->value);
638 cfg->err++;
639 }
640 }
641 break;
642 }
643 case KW_KEYINGTRIES:
644 if (streq(kw->value, "%forever"))
645 {
646 conn->sa_keying_tries = 0;
647 }
648 else
649 {
650 char *endptr;
651
652 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
653 if (*endptr != '\0')
654 {
655 plog("# bad integer value: %s=%s", kw->entry->name, kw->value);
656 cfg->err++;
657 }
658 }
659 break;
660 case KW_REKEY:
661 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
662 break;
663 case KW_REAUTH:
664 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
665 break;
666 case KW_MOBIKE:
667 KW_POLICY_FLAG("yes", "no", POLICY_MOBIKE)
668 break;
669 case KW_FORCEENCAPS:
670 KW_POLICY_FLAG("yes", "no", POLICY_FORCE_ENCAP)
671 break;
672 case KW_MODECONFIG:
673 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
674 break;
675 case KW_XAUTH:
676 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
677 break;
678 default:
679 break;
680 }
681 }
682
683 handle_dns_failure("left", &conn->left, cfg);
684 handle_dns_failure("right", &conn->right, cfg);
685 handle_firewall("left", &conn->left, cfg);
686 handle_firewall("right", &conn->right, cfg);
687 }
688
689 /*
690 * initialize a conn object with the default conn
691 */
692 static void
693 conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
694 {
695 memcpy(conn, def, sizeof(starter_conn_t));
696 conn->name = clone_str(name, "conn name");
697
698 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
699 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
700 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
701 }
702
703 /*
704 * parse a ca section
705 */
706 static void
707 load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
708 {
709 char *ca_name = (ca->name == NULL)? "%default":ca->name;
710
711 for ( ; kw; kw = kw->next)
712 {
713 bool assigned = FALSE;
714
715 kw_token_t token = kw->entry->token;
716
717 if (token == KW_AUTO)
718 {
719 token = KW_CA_SETUP;
720 }
721 else if (token == KW_ALSO)
722 {
723 if (cfg->parse_also)
724 {
725 also_t *also = alloc_thing(also_t, "also_t");
726
727 also->name = clone_str(kw->value, "also");
728 also->next = ca->also;
729 ca->also = also;
730
731 DBG(DBG_CONTROL,
732 DBG_log(" also=%s", kw->value)
733 )
734 }
735 continue;
736 }
737
738 if (token < KW_CA_FIRST || token > KW_CA_LAST)
739 {
740 plog("# unsupported keyword '%s' in ca '%s'", kw->entry->name, ca_name);
741 cfg->err++;
742 continue;
743 }
744
745 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
746 {
747 plog(" bad argument value in ca '%s'", ca_name);
748 cfg->err++;
749 }
750 }
751
752 /* treat 'route' and 'start' as 'add' */
753 if (ca->startup != STARTUP_NO)
754 ca->startup = STARTUP_ADD;
755 }
756
757 /*
758 * initialize a ca object with the default ca
759 */
760 static void
761 ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
762 {
763 memcpy(ca, def, sizeof(starter_ca_t));
764 ca->name = clone_str(name, "ca name");
765
766 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
767 }
768
769 static kw_list_t*
770 find_also_conn(const char* name, starter_conn_t *conn, starter_config_t *cfg);
771
772 static void
773 load_also_conns(starter_conn_t *conn, also_t *also, starter_config_t *cfg)
774 {
775 while (also != NULL)
776 {
777 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
778
779 if (kw == NULL)
780 {
781 plog(" conn '%s' cannot include '%s'", conn->name, also->name);
782 }
783 else
784 {
785 DBG(DBG_CONTROL,
786 DBG_log("conn '%s' includes '%s'", conn->name, also->name)
787 )
788 /* only load if no error occurred in the first round */
789 if (cfg->err == 0)
790 load_conn(conn, kw, cfg);
791 }
792 also = also->next;
793 }
794 }
795
796 /*
797 * find a conn included by also
798 */
799 static kw_list_t*
800 find_also_conn(const char* name, starter_conn_t *conn, starter_config_t *cfg)
801 {
802 starter_conn_t *c = cfg->conn_first;
803
804 while (c != NULL)
805 {
806 if (streq(name, c->name))
807 {
808 if (conn->visit == c->visit)
809 {
810 plog("# detected also loop");
811 cfg->err++;
812 return NULL;
813 }
814 c->visit = conn->visit;
815 load_also_conns(conn, c->also, cfg);
816 return c->kw;
817 }
818 c = c->next;
819 }
820
821 plog("# also '%s' not found", name);
822 cfg->err++;
823 return NULL;
824 }
825
826 static kw_list_t*
827 find_also_ca(const char* name, starter_ca_t *ca, starter_config_t *cfg);
828
829 static void
830 load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
831 {
832 while (also != NULL)
833 {
834 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
835
836 if (kw == NULL)
837 {
838 plog(" ca '%s' cannot include '%s'", ca->name, also->name);
839 }
840 else
841 {
842 DBG(DBG_CONTROL,
843 DBG_log("ca '%s' includes '%s'", ca->name, also->name)
844 )
845 /* only load if no error occurred in the first round */
846 if (cfg->err == 0)
847 load_ca(ca, kw, cfg);
848 }
849 also = also->next;
850 }
851 }
852
853 /*
854 * find a ca included by also
855 */
856 static kw_list_t*
857 find_also_ca(const char* name, starter_ca_t *ca, starter_config_t *cfg)
858 {
859 starter_ca_t *c = cfg->ca_first;
860
861 while (c != NULL)
862 {
863 if (streq(name, c->name))
864 {
865 if (ca->visit == c->visit)
866 {
867 plog("# detected also loop");
868 cfg->err++;
869 return NULL;
870 }
871 c->visit = ca->visit;
872 load_also_cas(ca, c->also, cfg);
873 return c->kw;
874 }
875 c = c->next;
876 }
877
878 plog("# also '%s' not found", name);
879 cfg->err++;
880 return NULL;
881 }
882
883 /*
884 * free the memory used by also_t objects
885 */
886 static void
887 free_also(also_t *head)
888 {
889 while (head != NULL)
890 {
891 also_t *also = head;
892
893 head = also->next;
894 pfree(also->name);
895 pfree(also);
896 }
897 }
898
899 /*
900 * free the memory used by a starter_conn_t object
901 */
902 static void
903 confread_free_conn(starter_conn_t *conn)
904 {
905 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
906 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
907 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
908 free_also(conn->also);
909 }
910
911 /*
912 * free the memory used by a starter_ca_t object
913 */
914 static void
915 confread_free_ca(starter_ca_t *ca)
916 {
917 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
918 free_also(ca->also);
919 }
920
921 /*
922 * free the memory used by a starter_config_t object
923 */
924 void
925 confread_free(starter_config_t *cfg)
926 {
927 starter_conn_t *conn = cfg->conn_first;
928 starter_ca_t *ca = cfg->ca_first;
929
930 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
931
932 confread_free_conn(&cfg->conn_default);
933
934 while (conn != NULL)
935 {
936 starter_conn_t *conn_aux = conn;
937
938 conn = conn->next;
939 confread_free_conn(conn_aux);
940 pfree(conn_aux);
941 }
942
943 confread_free_ca(&cfg->ca_default);
944
945 while (ca != NULL)
946 {
947 starter_ca_t *ca_aux = ca;
948
949 ca = ca->next;
950 confread_free_ca(ca_aux);
951 pfree(ca_aux);
952 }
953
954 pfree(cfg);
955 }
956
957 /*
958 * load and parse an IPsec configuration file
959 */
960 starter_config_t *
961 confread_load(const char *file)
962 {
963 starter_config_t *cfg = NULL;
964 config_parsed_t *cfgp;
965 section_list_t *sconn, *sca;
966 starter_conn_t *conn;
967 starter_ca_t *ca;
968
969 u_int total_err;
970 u_int visit = 0;
971
972 /* load IPSec configuration file */
973 cfgp = parser_load_conf(file);
974 if (!cfgp)
975 {
976 return NULL;
977 }
978 cfg = (starter_config_t *)alloc_thing(starter_config_t, "starter_config_t");
979
980 /* set default values */
981 default_values(cfg);
982
983 /* determine default route */
984 get_defaultroute(&cfg->defaultroute);
985
986 /* load config setup section */
987 load_setup(cfg, cfgp);
988
989 /* in the first round parse also statements */
990 cfg->parse_also = TRUE;
991
992 /* find %default ca section */
993 for (sca = cfgp->ca_first; sca; sca = sca->next)
994 {
995 if (streq(sca->name, "%default"))
996 {
997 DBG(DBG_CONTROL,
998 DBG_log("Loading ca %%default")
999 )
1000 load_ca(&cfg->ca_default, sca->kw, cfg);
1001 }
1002 }
1003
1004 /* parameters defined in ca %default sections can be overloads */
1005 cfg->ca_default.seen = LEMPTY;
1006
1007 /* load other ca sections */
1008 for (sca = cfgp->ca_first; sca; sca = sca->next)
1009 {
1010 u_int previous_err;
1011
1012 /* skip %default ca section */
1013 if (streq(sca->name, "%default"))
1014 continue;
1015
1016 DBG(DBG_CONTROL,
1017 DBG_log("Loading ca '%s'", sca->name)
1018 )
1019 ca = (starter_ca_t *)alloc_thing(starter_ca_t, "starter_ca_t");
1020
1021 ca_default(sca->name, ca, &cfg->ca_default);
1022 ca->kw = sca->kw;
1023 ca->next = NULL;
1024
1025 previous_err = cfg->err;
1026 load_ca(ca, ca->kw, cfg);
1027 if (cfg->err > previous_err)
1028 {
1029 /* errors occurred - free the ca */
1030 confread_free_ca(ca);
1031 cfg->non_fatal_err += cfg->err - previous_err;
1032 cfg->err = previous_err;
1033 }
1034 else
1035 {
1036 /* success - insert the ca into the chained list */
1037 if (cfg->ca_last)
1038 cfg->ca_last->next = ca;
1039 cfg->ca_last = ca;
1040 if (!cfg->ca_first)
1041 cfg->ca_first = ca;
1042 }
1043 }
1044
1045 for (ca = cfg->ca_first; ca; ca = ca->next)
1046 {
1047 also_t *also = ca->also;
1048
1049 while (also != NULL)
1050 {
1051 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
1052
1053 load_ca(ca, kw, cfg);
1054 also = also->next;
1055 }
1056
1057 if (ca->startup != STARTUP_NO)
1058 ca->state = STATE_TO_ADD;
1059 }
1060
1061 /* find %default conn sections */
1062 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1063 {
1064 if (streq(sconn->name, "%default"))
1065 {
1066 DBG(DBG_CONTROL,
1067 DBG_log("Loading conn %%default")
1068 )
1069 load_conn(&cfg->conn_default, sconn->kw, cfg);
1070 }
1071 }
1072
1073 /* parameter defined in conn %default sections can be overloaded */
1074 cfg->conn_default.seen = LEMPTY;
1075 cfg->conn_default.right.seen = LEMPTY;
1076 cfg->conn_default.left.seen = LEMPTY;
1077
1078 /* load other conn sections */
1079 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1080 {
1081 u_int previous_err;
1082
1083 /* skip %default conn section */
1084 if (streq(sconn->name, "%default"))
1085 continue;
1086
1087 DBG(DBG_CONTROL,
1088 DBG_log("Loading conn '%s'", sconn->name)
1089 )
1090 conn = (starter_conn_t *)alloc_thing(starter_conn_t, "starter_conn_t");
1091
1092 conn_default(sconn->name, conn, &cfg->conn_default);
1093 conn->kw = sconn->kw;
1094 conn->next = NULL;
1095
1096 previous_err = cfg->err;
1097 load_conn(conn, conn->kw, cfg);
1098 if (cfg->err > previous_err)
1099 {
1100 /* error occurred - free the conn */
1101 confread_free_conn(conn);
1102 cfg->non_fatal_err += cfg->err - previous_err;
1103 cfg->err = previous_err;
1104 }
1105 else
1106 {
1107 /* success - insert the conn into the chained list */
1108 if (cfg->conn_last)
1109 cfg->conn_last->next = conn;
1110 cfg->conn_last = conn;
1111 if (!cfg->conn_first)
1112 cfg->conn_first = conn;
1113 }
1114 }
1115
1116 /* in the second round do not parse also statements */
1117 cfg->parse_also = FALSE;
1118
1119 for (ca = cfg->ca_first; ca; ca = ca->next)
1120 {
1121 ca->visit = ++visit;
1122 load_also_cas(ca, ca->also, cfg);
1123
1124 if (ca->startup != STARTUP_NO)
1125 ca->state = STATE_TO_ADD;
1126 }
1127
1128 for (conn = cfg->conn_first; conn; conn = conn->next)
1129 {
1130 conn->visit = ++visit;
1131 load_also_conns(conn, conn->also, cfg);
1132
1133 if (conn->startup != STARTUP_NO)
1134 conn->state = STATE_TO_ADD;
1135 }
1136
1137 parser_free_conf(cfgp);
1138
1139 total_err = cfg->err + cfg->non_fatal_err;
1140 if (total_err > 0)
1141 {
1142 plog("### %d parsing error%s (%d fatal) ###"
1143 , total_err, (total_err > 1)?"s":"", cfg->err);
1144 }
1145
1146 return cfg;
1147 }