starter: Some whitespace cleanup.
[strongswan.git] / src / starter / confread.c
1 /* strongSwan IPsec config file parser
2 * Copyright (C) 2001-2002 Mathieu Lafon - Arkoon Network Security
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include <freeswan.h>
21
22 #include <eap/eap.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-sha1-modp2048,3des-sha1-modp1536";
38 static const char esp_defaults[] = "aes128-sha1,3des-sha1";
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_PUBKEY |
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 cfg->conn_default.left.ikeport = 500;
102 cfg->conn_default.right.ikeport = 500;
103
104 cfg->ca_default.seen = LEMPTY;
105 }
106
107 #define KW_POLICY_FLAG(sy, sn, fl) \
108 if (streq(kw->value, sy)) { conn->policy |= fl; } \
109 else if (streq(kw->value, sn)) { conn->policy &= ~fl; } \
110 else { plog("# bad policy value: %s=%s", kw->entry->name, kw->value); cfg->err++; }
111
112 static void load_setup(starter_config_t *cfg, config_parsed_t *cfgp)
113 {
114 kw_list_t *kw;
115
116 DBG(DBG_CONTROL,
117 DBG_log("Loading config setup")
118 )
119
120 for (kw = cfgp->config_setup; kw; kw = kw->next)
121 {
122 bool assigned = FALSE;
123
124 kw_token_t token = kw->entry->token;
125
126 if (token < KW_SETUP_FIRST || token > KW_SETUP_LAST)
127 {
128 plog("# unsupported keyword '%s' in config setup", kw->entry->name);
129 cfg->err++;
130 continue;
131 }
132
133 if (!assign_arg(token, KW_SETUP_FIRST, kw, (char *)cfg, &assigned))
134 {
135 plog(" bad argument value in config setup");
136 cfg->err++;
137 continue;
138 }
139 }
140 }
141
142 static void 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 bool 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 (value[0] == '%')
194 {
195 if (streq(value, "%modeconfig") || streq(value, "%modecfg") ||
196 streq(value, "%config") || streq(value, "%cfg"))
197 {
198 /* request ip via config payload */
199 free(end->sourceip);
200 end->sourceip = NULL;
201 end->sourceip_mask = 1;
202 }
203 else
204 { /* %poolname, strip %, serve ip requests */
205 free(end->sourceip);
206 end->sourceip = clone_str(value+1);
207 end->sourceip_mask = 0;
208 }
209 end->modecfg = TRUE;
210 }
211 else
212 {
213 char *pos;
214 ip_address addr;
215 ip_subnet net;
216
217 conn->tunnel_addr_family = ip_version(value);
218 pos = strchr(value, '/');
219
220 if (pos)
221 { /* CIDR notation, address pool */
222 ugh = ttosubnet(value, 0, conn->tunnel_addr_family, &net);
223 if (ugh != NULL)
224 {
225 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
226 goto err;
227 }
228 *pos = '\0';
229 free(end->sourceip);
230 end->sourceip = clone_str(value);
231 end->sourceip_mask = atoi(pos + 1);
232 }
233 else
234 { /* fixed srcip */
235 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
236 if (ugh != NULL)
237 {
238 plog("# bad addr: %s=%s [%s]", name, value, ugh);
239 goto err;
240 }
241 end->sourceip_mask = (conn->tunnel_addr_family == AF_INET) ?
242 32 : 128;
243 }
244 }
245 conn->policy |= POLICY_TUNNEL;
246 break;
247 case KW_SENDCERT:
248 if (end->sendcert == CERT_YES_SEND)
249 {
250 end->sendcert = CERT_ALWAYS_SEND;
251 }
252 else if (end->sendcert == CERT_NO_SEND)
253 {
254 end->sendcert = CERT_NEVER_SEND;
255 }
256 break;
257 default:
258 break;
259 }
260
261 if (assigned)
262 return;
263
264 /* individual processing of keywords that were not assigned automatically */
265 switch (token)
266 {
267 case KW_HOST:
268 if (streq(value, "%defaultroute"))
269 {
270 if (cfg->defaultroute.defined)
271 {
272 end->addr = cfg->defaultroute.addr;
273 end->nexthop = cfg->defaultroute.nexthop;
274 }
275 else if (!cfg->defaultroute.supported)
276 {
277 plog("%%defaultroute not supported, fallback to %%any");
278 }
279 else
280 {
281 plog("# default route not known: %s=%s", name, value);
282 goto err;
283 }
284 }
285 else if (streq(value, "%any") || streq(value, "%any4"))
286 {
287 anyaddr(conn->addr_family, &end->addr);
288 }
289 else if (streq(value, "%any6"))
290 {
291 conn->addr_family = AF_INET6;
292 anyaddr(conn->addr_family, &end->addr);
293 }
294 else if (streq(value, "%group"))
295 {
296 ip_address any;
297
298 conn->policy |= POLICY_GROUP | POLICY_TUNNEL;
299 anyaddr(conn->addr_family, &end->addr);
300 anyaddr(conn->tunnel_addr_family, &any);
301 end->has_client = TRUE;
302 }
303 else
304 {
305 /* check for allow_any prefix */
306 if (value[0] == '%')
307 {
308 end->allow_any = TRUE;
309 value++;
310 }
311 conn->addr_family = ip_version(value);
312 ugh = ttoaddr(value, 0, conn->addr_family, &end->addr);
313 if (ugh != NULL)
314 {
315 plog("# bad addr: %s=%s [%s]", name, value, ugh);
316 if (streq(ugh, "does not look numeric and name lookup failed"))
317 {
318 end->dns_failed = TRUE;
319 anyaddr(conn->addr_family, &end->addr);
320 }
321 else
322 {
323 goto err;
324 }
325 }
326 }
327 break;
328 case KW_NEXTHOP:
329 if (streq(value, "%defaultroute"))
330 {
331 if (cfg->defaultroute.defined)
332 {
333 end->nexthop = cfg->defaultroute.nexthop;
334 }
335 else
336 {
337 plog("# default route not known: %s=%s", name, value);
338 goto err;
339 }
340 }
341 else if (streq(value, "%direct"))
342 {
343 ugh = anyaddr(conn->addr_family, &end->nexthop);
344 }
345 else
346 {
347 conn->addr_family = ip_version(value);
348 ugh = ttoaddr(value, 0, conn->addr_family, &end->nexthop);
349 }
350 if (ugh != NULL)
351 {
352 plog("# bad addr: %s=%s [%s]", name, value, ugh);
353 goto err;
354 }
355 break;
356 case KW_SUBNETWITHIN:
357 {
358 ip_subnet net;
359
360 end->has_client = TRUE;
361 end->has_client_wildcard = TRUE;
362 conn->tunnel_addr_family = ip_version(value);
363
364 ugh = ttosubnet(value, 0, ip_version(value), &net);
365 if (ugh != NULL)
366 {
367 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
368 goto err;
369 }
370 end->subnet = clone_str(value);
371 break;
372 }
373 case KW_PROTOPORT:
374 ugh = ttoprotoport(value, 0, &end->protocol, &end->port, &has_port_wildcard);
375 end->has_port_wildcard = has_port_wildcard;
376 break;
377 case KW_NATIP:
378 if (end->sourceip)
379 {
380 plog("# natip and sourceip cannot be defined at the same time");
381 goto err;
382 }
383 if (streq(value, "%defaultroute"))
384 {
385 char buf[64];
386
387 if (cfg->defaultroute.defined)
388 {
389 addrtot(&cfg->defaultroute.addr, 0, buf, sizeof(buf));
390 end->sourceip = clone_str(buf);
391 }
392 else
393 {
394 plog("# default route not known: %s=%s", name, value);
395 goto err;
396 }
397 }
398 else
399 {
400 ip_address addr;
401
402 conn->tunnel_addr_family = ip_version(value);
403 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
404 if (ugh != NULL)
405 {
406 plog("# bad addr: %s=%s [%s]", name, value, ugh);
407 goto err;
408 }
409 end->sourceip = clone_str(value);
410 }
411 end->has_natip = TRUE;
412 conn->policy |= POLICY_TUNNEL;
413 break;
414 default:
415 break;
416 }
417 return;
418
419 err:
420 plog(" bad argument value in conn '%s'", conn_name);
421 cfg->err++;
422 }
423
424 /*
425 * handles left|right=<FQDN> DNS resolution failure
426 */
427 static void handle_dns_failure(const char *label, starter_end_t *end,
428 starter_config_t *cfg)
429 {
430 if (end->dns_failed)
431 {
432 if (end->allow_any)
433 {
434 plog("# fallback to %s=%%any due to '%%' prefix or %sallowany=yes",
435 label, label);
436 }
437 else
438 {
439 /* declare an error */
440 cfg->err++;
441 }
442 }
443 }
444
445 /*
446 * handles left|rightfirewall and left|rightupdown parameters
447 */
448 static void handle_firewall(const char *label, starter_end_t *end,
449 starter_config_t *cfg)
450 {
451 if (end->firewall && (end->seen & LELEM(KW_FIREWALL - KW_END_FIRST)))
452 {
453 if (end->updown != NULL)
454 {
455 plog("# cannot have both %sfirewall and %supdown", label, label);
456 cfg->err++;
457 }
458 else
459 {
460 end->updown = clone_str(firewall_defaults);
461 end->firewall = FALSE;
462 }
463 }
464 }
465
466 static bool handle_mark(char *value, mark_t *mark)
467 {
468 char *pos, *endptr;
469
470 pos = strchr(value, '/');
471 if (pos)
472 {
473 *pos = '\0';
474 mark->mask = strtoul(pos+1, &endptr, 0);
475 if (*endptr != '\0')
476 {
477 plog("# invalid mark mask: %s", pos+1);
478 return FALSE;
479 }
480 }
481 else
482 {
483 mark->mask = 0xffffffff;
484 }
485 if (value == '\0')
486 {
487 mark->value = 0;
488 }
489 else
490 {
491 mark->value = strtoul(value, &endptr, 0);
492 if (*endptr != '\0')
493 {
494 plog("# invalid mark value: %s", value);
495 return FALSE;
496 }
497 }
498 return TRUE;
499 }
500
501 /*
502 * parse a conn section
503 */
504 static void load_conn(starter_conn_t *conn, kw_list_t *kw, starter_config_t *cfg)
505 {
506 char *conn_name = (conn->name == NULL)? "%default":conn->name;
507
508 for ( ; kw; kw = kw->next)
509 {
510 bool assigned = FALSE;
511
512 kw_token_t token = kw->entry->token;
513
514 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
515 {
516 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST
517 , kw, conn_name, cfg);
518 continue;
519 }
520 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
521 {
522 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST
523 , kw, conn_name, cfg);
524 continue;
525 }
526
527 if (token == KW_AUTO)
528 {
529 token = KW_CONN_SETUP;
530 }
531 else if (token == KW_ALSO)
532 {
533 if (cfg->parse_also)
534 {
535 also_t *also = malloc_thing(also_t);
536
537 also->name = clone_str(kw->value);
538 also->next = conn->also;
539 conn->also = also;
540
541 DBG(DBG_CONTROL,
542 DBG_log(" also=%s", kw->value)
543 )
544 }
545 continue;
546 }
547
548 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
549 {
550 plog("# unsupported keyword '%s' in conn '%s'"
551 , kw->entry->name, conn_name);
552 cfg->err++;
553 continue;
554 }
555
556 if (!assign_arg(token, KW_CONN_FIRST, kw, (char *)conn, &assigned))
557 {
558 plog(" bad argument value in conn '%s'", conn_name);
559 cfg->err++;
560 continue;
561 }
562
563 if (assigned)
564 continue;
565
566 switch (token)
567 {
568 case KW_TYPE:
569 conn->policy &= ~(POLICY_TUNNEL | POLICY_SHUNT_MASK);
570 if (streq(kw->value, "tunnel"))
571 {
572 conn->policy |= POLICY_TUNNEL;
573 }
574 else if (streq(kw->value, "beet"))
575 {
576 conn->policy |= POLICY_BEET;
577 }
578 else if (streq(kw->value, "transport_proxy"))
579 {
580 conn->policy |= POLICY_PROXY;
581 }
582 else if (streq(kw->value, "passthrough") || streq(kw->value, "pass"))
583 {
584 conn->policy |= POLICY_SHUNT_PASS;
585 }
586 else if (streq(kw->value, "drop"))
587 {
588 conn->policy |= POLICY_SHUNT_DROP;
589 }
590 else if (streq(kw->value, "reject"))
591 {
592 conn->policy |= POLICY_SHUNT_REJECT;
593 }
594 else if (strcmp(kw->value, "transport") != 0)
595 {
596 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
597 cfg->err++;
598 }
599 break;
600 case KW_PFS:
601 KW_POLICY_FLAG("yes", "no", POLICY_PFS)
602 break;
603 case KW_COMPRESS:
604 KW_POLICY_FLAG("yes", "no", POLICY_COMPRESS)
605 break;
606 case KW_AUTH:
607 KW_POLICY_FLAG("ah", "esp", POLICY_AUTHENTICATE)
608 break;
609 case KW_AUTHBY:
610 conn->policy &= ~(POLICY_ID_AUTH_MASK | POLICY_ENCRYPT);
611
612 if (!(streq(kw->value, "never") || streq(kw->value, "eap")))
613 {
614 char *value = kw->value;
615 char *second = strchr(kw->value, '|');
616
617 if (second != NULL)
618 {
619 *second = '\0';
620 }
621
622 /* also handles the cases secret|rsasig and rsasig|secret */
623 for (;;)
624 {
625 if (streq(value, "rsa") || streq(value, "rsasig") ||
626 streq(value, "ecdsa") || streq(value, "ecdsasig") ||
627 streq(value, "pubkey"))
628 {
629 conn->policy |= POLICY_PUBKEY | POLICY_ENCRYPT;
630 }
631 else if (streq(value, "secret") || streq(value, "psk"))
632 {
633 conn->policy |= POLICY_PSK | POLICY_ENCRYPT;
634 }
635 else if (streq(value, "xauthrsasig"))
636 {
637 conn->policy |= POLICY_XAUTH_RSASIG | POLICY_ENCRYPT;
638 }
639 else if (streq(value, "xauthpsk"))
640 {
641 conn->policy |= POLICY_XAUTH_PSK | POLICY_ENCRYPT;
642 }
643 else
644 {
645 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
646 cfg->err++;
647 break;
648 }
649 if (second == NULL)
650 {
651 break;
652 }
653 value = second;
654 second = NULL; /* traverse the loop no more than twice */
655 }
656 }
657 break;
658 case KW_EAP:
659 {
660 char *sep;
661
662 /* check for vendor-type format */
663 sep = strchr(kw->value, '-');
664 if (sep)
665 {
666 *(sep++) = '\0';
667 conn->eap_type = atoi(kw->value);
668 conn->eap_vendor = atoi(sep);
669 if (conn->eap_type == 0 || conn->eap_vendor == 0)
670 {
671 plog("# invalid EAP type: %s=%s", kw->entry->name, kw->value);
672 cfg->err++;
673 }
674 break;
675 }
676 conn->eap_type = eap_type_from_string(kw->value);
677 if (conn->eap_type == 0)
678 {
679 conn->eap_type = atoi(kw->value);
680 if (conn->eap_type == 0)
681 {
682 plog("# unknown EAP type: %s=%s", kw->entry->name, kw->value);
683 cfg->err++;
684 }
685 }
686 break;
687 }
688 case KW_MARK:
689 if (!handle_mark(kw->value, &conn->mark_in))
690 {
691 cfg->err++;
692 break;
693 }
694 conn->mark_out = conn->mark_in;
695 break;
696 case KW_MARK_IN:
697 if (!handle_mark(kw->value, &conn->mark_in))
698 {
699 cfg->err++;
700 }
701 break;
702 case KW_MARK_OUT:
703 if (!handle_mark(kw->value, &conn->mark_out))
704 {
705 cfg->err++;
706 }
707 break;
708 case KW_KEYINGTRIES:
709 if (streq(kw->value, "%forever"))
710 {
711 conn->sa_keying_tries = 0;
712 }
713 else
714 {
715 char *endptr;
716
717 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
718 if (*endptr != '\0')
719 {
720 plog("# bad integer value: %s=%s", kw->entry->name, kw->value);
721 cfg->err++;
722 }
723 }
724 break;
725 case KW_REKEY:
726 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
727 break;
728 case KW_REAUTH:
729 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
730 break;
731 case KW_MOBIKE:
732 KW_POLICY_FLAG("yes", "no", POLICY_MOBIKE)
733 break;
734 case KW_FORCEENCAPS:
735 KW_POLICY_FLAG("yes", "no", POLICY_FORCE_ENCAP)
736 break;
737 case KW_MODECONFIG:
738 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
739 break;
740 case KW_XAUTH:
741 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
742 break;
743 default:
744 break;
745 }
746 }
747
748 handle_dns_failure("left", &conn->left, cfg);
749 handle_dns_failure("right", &conn->right, cfg);
750 handle_firewall("left", &conn->left, cfg);
751 handle_firewall("right", &conn->right, cfg);
752 }
753
754 /*
755 * initialize a conn object with the default conn
756 */
757 static void conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
758 {
759 memcpy(conn, def, sizeof(starter_conn_t));
760 conn->name = clone_str(name);
761
762 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
763 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
764 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
765 }
766
767 /*
768 * parse a ca section
769 */
770 static void load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
771 {
772 char *ca_name = (ca->name == NULL)? "%default":ca->name;
773
774 for ( ; kw; kw = kw->next)
775 {
776 bool assigned = FALSE;
777
778 kw_token_t token = kw->entry->token;
779
780 if (token == KW_AUTO)
781 {
782 token = KW_CA_SETUP;
783 }
784 else if (token == KW_ALSO)
785 {
786 if (cfg->parse_also)
787 {
788 also_t *also = malloc_thing(also_t);
789
790 also->name = clone_str(kw->value);
791 also->next = ca->also;
792 ca->also = also;
793
794 DBG(DBG_CONTROL,
795 DBG_log(" also=%s", kw->value)
796 )
797 }
798 continue;
799 }
800
801 if (token < KW_CA_FIRST || token > KW_CA_LAST)
802 {
803 plog("# unsupported keyword '%s' in ca '%s'", kw->entry->name, ca_name);
804 cfg->err++;
805 continue;
806 }
807
808 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
809 {
810 plog(" bad argument value in ca '%s'", ca_name);
811 cfg->err++;
812 }
813 }
814
815 /* treat 'route' and 'start' as 'add' */
816 if (ca->startup != STARTUP_NO)
817 ca->startup = STARTUP_ADD;
818 }
819
820 /*
821 * initialize a ca object with the default ca
822 */
823 static void ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
824 {
825 memcpy(ca, def, sizeof(starter_ca_t));
826 ca->name = clone_str(name);
827
828 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
829 }
830
831 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
832 starter_config_t *cfg);
833
834 static void load_also_conns(starter_conn_t *conn, also_t *also,
835 starter_config_t *cfg)
836 {
837 while (also != NULL)
838 {
839 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
840
841 if (kw == NULL)
842 {
843 plog(" conn '%s' cannot include '%s'", conn->name, also->name);
844 }
845 else
846 {
847 DBG(DBG_CONTROL,
848 DBG_log("conn '%s' includes '%s'", conn->name, also->name)
849 )
850 /* only load if no error occurred in the first round */
851 if (cfg->err == 0)
852 load_conn(conn, kw, cfg);
853 }
854 also = also->next;
855 }
856 }
857
858 /*
859 * find a conn included by also
860 */
861 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
862 starter_config_t *cfg)
863 {
864 starter_conn_t *c = cfg->conn_first;
865
866 while (c != NULL)
867 {
868 if (streq(name, c->name))
869 {
870 if (conn->visit == c->visit)
871 {
872 plog("# detected also loop");
873 cfg->err++;
874 return NULL;
875 }
876 c->visit = conn->visit;
877 load_also_conns(conn, 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 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
889 starter_config_t *cfg);
890
891 static void load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
892 {
893 while (also != NULL)
894 {
895 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
896
897 if (kw == NULL)
898 {
899 plog(" ca '%s' cannot include '%s'", ca->name, also->name);
900 }
901 else
902 {
903 DBG(DBG_CONTROL,
904 DBG_log("ca '%s' includes '%s'", ca->name, also->name)
905 )
906 /* only load if no error occurred in the first round */
907 if (cfg->err == 0)
908 load_ca(ca, kw, cfg);
909 }
910 also = also->next;
911 }
912 }
913
914 /*
915 * find a ca included by also
916 */
917 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
918 starter_config_t *cfg)
919 {
920 starter_ca_t *c = cfg->ca_first;
921
922 while (c != NULL)
923 {
924 if (streq(name, c->name))
925 {
926 if (ca->visit == c->visit)
927 {
928 plog("# detected also loop");
929 cfg->err++;
930 return NULL;
931 }
932 c->visit = ca->visit;
933 load_also_cas(ca, c->also, cfg);
934 return c->kw;
935 }
936 c = c->next;
937 }
938
939 plog("# also '%s' not found", name);
940 cfg->err++;
941 return NULL;
942 }
943
944 /*
945 * free the memory used by also_t objects
946 */
947 static void free_also(also_t *head)
948 {
949 while (head != NULL)
950 {
951 also_t *also = head;
952
953 head = also->next;
954 free(also->name);
955 free(also);
956 }
957 }
958
959 /*
960 * free the memory used by a starter_conn_t object
961 */
962 static void confread_free_conn(starter_conn_t *conn)
963 {
964 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
965 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
966 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
967 free_also(conn->also);
968 }
969
970 /*
971 * free the memory used by a starter_ca_t object
972 */
973 static void
974 confread_free_ca(starter_ca_t *ca)
975 {
976 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
977 free_also(ca->also);
978 }
979
980 /*
981 * free the memory used by a starter_config_t object
982 */
983 void confread_free(starter_config_t *cfg)
984 {
985 starter_conn_t *conn = cfg->conn_first;
986 starter_ca_t *ca = cfg->ca_first;
987
988 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
989
990 confread_free_conn(&cfg->conn_default);
991
992 while (conn != NULL)
993 {
994 starter_conn_t *conn_aux = conn;
995
996 conn = conn->next;
997 confread_free_conn(conn_aux);
998 free(conn_aux);
999 }
1000
1001 confread_free_ca(&cfg->ca_default);
1002
1003 while (ca != NULL)
1004 {
1005 starter_ca_t *ca_aux = ca;
1006
1007 ca = ca->next;
1008 confread_free_ca(ca_aux);
1009 free(ca_aux);
1010 }
1011
1012 free(cfg);
1013 }
1014
1015 /*
1016 * load and parse an IPsec configuration file
1017 */
1018 starter_config_t* confread_load(const char *file)
1019 {
1020 starter_config_t *cfg = NULL;
1021 config_parsed_t *cfgp;
1022 section_list_t *sconn, *sca;
1023 starter_conn_t *conn;
1024 starter_ca_t *ca;
1025
1026 u_int total_err;
1027 u_int visit = 0;
1028
1029 /* load IPSec configuration file */
1030 cfgp = parser_load_conf(file);
1031 if (!cfgp)
1032 {
1033 return NULL;
1034 }
1035 cfg = malloc_thing(starter_config_t);
1036
1037 /* set default values */
1038 default_values(cfg);
1039
1040 /* determine default route */
1041 get_defaultroute(&cfg->defaultroute);
1042
1043 /* load config setup section */
1044 load_setup(cfg, cfgp);
1045
1046 /* in the first round parse also statements */
1047 cfg->parse_also = TRUE;
1048
1049 /* find %default ca section */
1050 for (sca = cfgp->ca_first; sca; sca = sca->next)
1051 {
1052 if (streq(sca->name, "%default"))
1053 {
1054 DBG(DBG_CONTROL,
1055 DBG_log("Loading ca %%default")
1056 )
1057 load_ca(&cfg->ca_default, sca->kw, cfg);
1058 }
1059 }
1060
1061 /* parameters defined in ca %default sections can be overloads */
1062 cfg->ca_default.seen = LEMPTY;
1063
1064 /* load other ca sections */
1065 for (sca = cfgp->ca_first; sca; sca = sca->next)
1066 {
1067 u_int previous_err;
1068
1069 /* skip %default ca section */
1070 if (streq(sca->name, "%default"))
1071 continue;
1072
1073 DBG(DBG_CONTROL,
1074 DBG_log("Loading ca '%s'", sca->name)
1075 )
1076 ca = malloc_thing(starter_ca_t);
1077
1078 ca_default(sca->name, ca, &cfg->ca_default);
1079 ca->kw = sca->kw;
1080 ca->next = NULL;
1081
1082 previous_err = cfg->err;
1083 load_ca(ca, ca->kw, cfg);
1084 if (cfg->err > previous_err)
1085 {
1086 /* errors occurred - free the ca */
1087 confread_free_ca(ca);
1088 cfg->non_fatal_err += cfg->err - previous_err;
1089 cfg->err = previous_err;
1090 }
1091 else
1092 {
1093 /* success - insert the ca into the chained list */
1094 if (cfg->ca_last)
1095 cfg->ca_last->next = ca;
1096 cfg->ca_last = ca;
1097 if (!cfg->ca_first)
1098 cfg->ca_first = ca;
1099 }
1100 }
1101
1102 for (ca = cfg->ca_first; ca; ca = ca->next)
1103 {
1104 also_t *also = ca->also;
1105
1106 while (also != NULL)
1107 {
1108 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
1109
1110 load_ca(ca, kw, cfg);
1111 also = also->next;
1112 }
1113
1114 if (ca->startup != STARTUP_NO)
1115 ca->state = STATE_TO_ADD;
1116 }
1117
1118 /* find %default conn sections */
1119 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1120 {
1121 if (streq(sconn->name, "%default"))
1122 {
1123 DBG(DBG_CONTROL,
1124 DBG_log("Loading conn %%default")
1125 )
1126 load_conn(&cfg->conn_default, sconn->kw, cfg);
1127 }
1128 }
1129
1130 /* parameter defined in conn %default sections can be overloaded */
1131 cfg->conn_default.seen = LEMPTY;
1132 cfg->conn_default.right.seen = LEMPTY;
1133 cfg->conn_default.left.seen = LEMPTY;
1134
1135 /* load other conn sections */
1136 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1137 {
1138 u_int previous_err;
1139
1140 /* skip %default conn section */
1141 if (streq(sconn->name, "%default"))
1142 continue;
1143
1144 DBG(DBG_CONTROL,
1145 DBG_log("Loading conn '%s'", sconn->name)
1146 )
1147 conn = malloc_thing(starter_conn_t);
1148
1149 conn_default(sconn->name, conn, &cfg->conn_default);
1150 conn->kw = sconn->kw;
1151 conn->next = NULL;
1152
1153 previous_err = cfg->err;
1154 load_conn(conn, conn->kw, cfg);
1155 if (cfg->err > previous_err)
1156 {
1157 /* error occurred - free the conn */
1158 confread_free_conn(conn);
1159 cfg->non_fatal_err += cfg->err - previous_err;
1160 cfg->err = previous_err;
1161 }
1162 else
1163 {
1164 /* success - insert the conn into the chained list */
1165 if (cfg->conn_last)
1166 cfg->conn_last->next = conn;
1167 cfg->conn_last = conn;
1168 if (!cfg->conn_first)
1169 cfg->conn_first = conn;
1170 }
1171 }
1172
1173 /* in the second round do not parse also statements */
1174 cfg->parse_also = FALSE;
1175
1176 for (ca = cfg->ca_first; ca; ca = ca->next)
1177 {
1178 ca->visit = ++visit;
1179 load_also_cas(ca, ca->also, cfg);
1180
1181 if (ca->startup != STARTUP_NO)
1182 ca->state = STATE_TO_ADD;
1183 }
1184
1185 for (conn = cfg->conn_first; conn; conn = conn->next)
1186 {
1187 conn->visit = ++visit;
1188 load_also_conns(conn, conn->also, cfg);
1189
1190 if (conn->startup != STARTUP_NO)
1191 conn->state = STATE_TO_ADD;
1192 }
1193
1194 parser_free_conf(cfgp);
1195
1196 total_err = cfg->err + cfg->non_fatal_err;
1197 if (total_err > 0)
1198 {
1199 plog("### %d parsing error%s (%d fatal) ###"
1200 , total_err, (total_err > 1)?"s":"", cfg->err);
1201 }
1202
1203 return cfg;
1204 }