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