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