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