804ec6e99c3083e11a7bff2acade70acf0db543b
[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 "../pluto/constants.h"
26 #include "../pluto/defs.h"
27 #include "../pluto/log.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 plog("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 { plog("# 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 DBG(DBG_CONTROL,
129 DBG_log("Loading config setup")
130 )
131
132 for (kw = cfgp->config_setup; kw; kw = kw->next)
133 {
134 bool assigned = FALSE;
135
136 kw_token_t token = kw->entry->token;
137
138 if ((int)token < KW_SETUP_FIRST || token > KW_SETUP_LAST)
139 {
140 plog("# unsupported keyword '%s' in config setup", kw->entry->name);
141 cfg->err++;
142 continue;
143 }
144
145 if (!assign_arg(token, KW_SETUP_FIRST, kw, (char *)cfg, &assigned))
146 {
147 plog(" bad argument value in config setup");
148 cfg->err++;
149 continue;
150 }
151 }
152
153 /* verify the executables are actually available (some distros split
154 * packages but enabled both) */
155 #ifdef START_CHARON
156 cfg->setup.charonstart = cfg->setup.charonstart &&
157 daemon_exists("charon", CHARON_CMD);
158 #else
159 cfg->setup.charonstart = FALSE;
160 #endif
161 #ifdef START_PLUTO
162 cfg->setup.plutostart = cfg->setup.plutostart &&
163 daemon_exists("pluto", PLUTO_CMD);
164 #else
165 cfg->setup.plutostart = FALSE;
166 #endif
167 }
168
169 static void kw_end(starter_conn_t *conn, starter_end_t *end, kw_token_t token,
170 kw_list_t *kw, char *conn_name, starter_config_t *cfg)
171 {
172 err_t ugh = NULL;
173 bool assigned = FALSE;
174 bool has_port_wildcard; /* set if port is %any */
175
176 char *name = kw->entry->name;
177 char *value = kw->value;
178
179 if (!assign_arg(token, KW_END_FIRST, kw, (char *)end, &assigned))
180 goto err;
181
182 /* post processing of some keywords that were assigned automatically */
183 switch (token)
184 {
185 case KW_HOST:
186 free(end->host);
187 end->host = NULL;
188 if (streq(value, "%defaultroute"))
189 {
190 if (cfg->defaultroute.defined)
191 {
192 end->addr = cfg->defaultroute.addr;
193 end->nexthop = cfg->defaultroute.nexthop;
194 }
195 else if (!cfg->defaultroute.supported)
196 {
197 plog("%%defaultroute not supported, fallback to %%any");
198 }
199 else
200 {
201 plog("# default route not known: %s=%s", name, value);
202 goto err;
203 }
204 }
205 else if (streq(value, "%any") || streq(value, "%any4"))
206 {
207 anyaddr(conn->addr_family, &end->addr);
208 }
209 else if (streq(value, "%any6"))
210 {
211 conn->addr_family = AF_INET6;
212 anyaddr(conn->addr_family, &end->addr);
213 }
214 else if (streq(value, "%group"))
215 {
216 ip_address any;
217
218 conn->policy |= POLICY_GROUP | POLICY_TUNNEL;
219 anyaddr(conn->addr_family, &end->addr);
220 anyaddr(conn->tunnel_addr_family, &any);
221 end->has_client = TRUE;
222 }
223 else
224 {
225 /* check for allow_any prefix */
226 if (value[0] == '%')
227 {
228 end->allow_any = TRUE;
229 value++;
230 }
231 conn->addr_family = ip_version(value);
232 ugh = ttoaddr(value, 0, conn->addr_family, &end->addr);
233 if (ugh != NULL)
234 {
235 plog("# bad addr: %s=%s [%s]", name, value, ugh);
236 if (streq(ugh, "does not look numeric and name lookup failed"))
237 {
238 end->dns_failed = TRUE;
239 anyaddr(conn->addr_family, &end->addr);
240 }
241 else
242 {
243 goto err;
244 }
245 }
246 end->host = clone_str(value);
247 }
248 break;
249 case KW_SUBNET:
250 if ((strlen(value) >= 6 && strncmp(value,"vhost:",6) == 0)
251 || (strlen(value) >= 5 && strncmp(value,"vnet:",5) == 0))
252 {
253 /* used by pluto only */
254 end->has_virt = TRUE;
255 }
256 else
257 {
258 ip_subnet net;
259 char *pos;
260 int len = 0;
261
262 end->has_client = TRUE;
263 conn->tunnel_addr_family = ip_version(value);
264
265 pos = strchr(value, ',');
266 if (pos)
267 {
268 len = pos - value;
269 }
270 ugh = ttosubnet(value, len, ip_version(value), &net);
271 if (ugh != NULL)
272 {
273 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
274 goto err;
275 }
276 }
277 break;
278 case KW_SOURCEIP:
279 if (end->has_natip)
280 {
281 plog("# natip and sourceip cannot be defined at the same time");
282 goto err;
283 }
284 if (value[0] == '%')
285 {
286 if (streq(value, "%modeconfig") || streq(value, "%modecfg") ||
287 streq(value, "%config") || streq(value, "%cfg"))
288 {
289 /* request ip via config payload */
290 free(end->sourceip);
291 end->sourceip = NULL;
292 end->sourceip_mask = 1;
293 }
294 else
295 { /* %poolname, strip %, serve ip requests */
296 free(end->sourceip);
297 end->sourceip = clone_str(value+1);
298 end->sourceip_mask = 0;
299 }
300 end->modecfg = TRUE;
301 }
302 else
303 {
304 char *pos;
305 ip_address addr;
306 ip_subnet net;
307
308 conn->tunnel_addr_family = ip_version(value);
309 pos = strchr(value, '/');
310
311 if (pos)
312 { /* CIDR notation, address pool */
313 ugh = ttosubnet(value, 0, conn->tunnel_addr_family, &net);
314 if (ugh != NULL)
315 {
316 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
317 goto err;
318 }
319 *pos = '\0';
320 free(end->sourceip);
321 end->sourceip = clone_str(value);
322 end->sourceip_mask = atoi(pos + 1);
323 }
324 else
325 { /* fixed srcip */
326 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &addr);
327 if (ugh != NULL)
328 {
329 plog("# bad addr: %s=%s [%s]", name, value, ugh);
330 goto err;
331 }
332 end->sourceip_mask = (conn->tunnel_addr_family == AF_INET) ?
333 32 : 128;
334 }
335 }
336 conn->policy |= POLICY_TUNNEL;
337 break;
338 case KW_SENDCERT:
339 if (end->sendcert == CERT_YES_SEND)
340 {
341 end->sendcert = CERT_ALWAYS_SEND;
342 }
343 else if (end->sendcert == CERT_NO_SEND)
344 {
345 end->sendcert = CERT_NEVER_SEND;
346 }
347 break;
348 default:
349 break;
350 }
351
352 if (assigned)
353 return;
354
355 /* individual processing of keywords that were not assigned automatically */
356 switch (token)
357 {
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, starter_conn_t *conn)
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 if (!end->host)
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"))
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_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 plog("# bad integer value: %s=%s", kw->entry->name, kw->value);
721 cfg->err++;
722 }
723 }
724 break;
725 case KW_KEYINGTRIES:
726 if (streq(kw->value, "%forever"))
727 {
728 conn->sa_keying_tries = 0;
729 }
730 else
731 {
732 char *endptr;
733
734 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
735 if (*endptr != '\0')
736 {
737 plog("# bad integer value: %s=%s", kw->entry->name, kw->value);
738 cfg->err++;
739 }
740 }
741 break;
742 case KW_REKEY:
743 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
744 break;
745 case KW_REAUTH:
746 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
747 break;
748 case KW_MOBIKE:
749 KW_POLICY_FLAG("yes", "no", POLICY_MOBIKE)
750 break;
751 case KW_FORCEENCAPS:
752 KW_POLICY_FLAG("yes", "no", POLICY_FORCE_ENCAP)
753 break;
754 case KW_MODECONFIG:
755 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
756 break;
757 case KW_XAUTH:
758 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
759 break;
760 default:
761 break;
762 }
763 }
764
765 handle_dns_failure("left", &conn->left, cfg, conn);
766 handle_dns_failure("right", &conn->right, cfg, conn);
767 handle_firewall("left", &conn->left, cfg);
768 handle_firewall("right", &conn->right, cfg);
769 }
770
771 /*
772 * initialize a conn object with the default conn
773 */
774 static void conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
775 {
776 memcpy(conn, def, sizeof(starter_conn_t));
777 conn->name = clone_str(name);
778
779 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
780 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
781 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
782 }
783
784 /*
785 * parse a ca section
786 */
787 static void load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
788 {
789 char *ca_name = (ca->name == NULL)? "%default":ca->name;
790
791 for ( ; kw; kw = kw->next)
792 {
793 bool assigned = FALSE;
794
795 kw_token_t token = kw->entry->token;
796
797 if (token == KW_AUTO)
798 {
799 token = KW_CA_SETUP;
800 }
801 else if (token == KW_ALSO)
802 {
803 if (cfg->parse_also)
804 {
805 also_t *also = malloc_thing(also_t);
806
807 also->name = clone_str(kw->value);
808 also->next = ca->also;
809 ca->also = also;
810
811 DBG(DBG_CONTROL,
812 DBG_log(" also=%s", kw->value)
813 )
814 }
815 continue;
816 }
817
818 if (token < KW_CA_FIRST || token > KW_CA_LAST)
819 {
820 plog("# unsupported keyword '%s' in ca '%s'", kw->entry->name, ca_name);
821 cfg->err++;
822 continue;
823 }
824
825 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
826 {
827 plog(" bad argument value in ca '%s'", ca_name);
828 cfg->err++;
829 }
830 }
831
832 /* treat 'route' and 'start' as 'add' */
833 if (ca->startup != STARTUP_NO)
834 ca->startup = STARTUP_ADD;
835 }
836
837 /*
838 * initialize a ca object with the default ca
839 */
840 static void ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
841 {
842 memcpy(ca, def, sizeof(starter_ca_t));
843 ca->name = clone_str(name);
844
845 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
846 }
847
848 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
849 starter_config_t *cfg);
850
851 static void load_also_conns(starter_conn_t *conn, also_t *also,
852 starter_config_t *cfg)
853 {
854 while (also != NULL)
855 {
856 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
857
858 if (kw == NULL)
859 {
860 plog(" conn '%s' cannot include '%s'", conn->name, also->name);
861 }
862 else
863 {
864 DBG(DBG_CONTROL,
865 DBG_log("conn '%s' includes '%s'", conn->name, also->name)
866 )
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 plog("# 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 plog("# 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 plog(" ca '%s' cannot include '%s'", ca->name, also->name);
917 }
918 else
919 {
920 DBG(DBG_CONTROL,
921 DBG_log("ca '%s' includes '%s'", ca->name, also->name)
922 )
923 /* only load if no error occurred in the first round */
924 if (cfg->err == 0)
925 load_ca(ca, kw, cfg);
926 }
927 also = also->next;
928 }
929 }
930
931 /*
932 * find a ca included by also
933 */
934 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
935 starter_config_t *cfg)
936 {
937 starter_ca_t *c = cfg->ca_first;
938
939 while (c != NULL)
940 {
941 if (streq(name, c->name))
942 {
943 if (ca->visit == c->visit)
944 {
945 plog("# detected also loop");
946 cfg->err++;
947 return NULL;
948 }
949 c->visit = ca->visit;
950 load_also_cas(ca, c->also, cfg);
951 return c->kw;
952 }
953 c = c->next;
954 }
955
956 plog("# also '%s' not found", name);
957 cfg->err++;
958 return NULL;
959 }
960
961 /*
962 * free the memory used by also_t objects
963 */
964 static void free_also(also_t *head)
965 {
966 while (head != NULL)
967 {
968 also_t *also = head;
969
970 head = also->next;
971 free(also->name);
972 free(also);
973 }
974 }
975
976 /*
977 * free the memory used by a starter_conn_t object
978 */
979 static void confread_free_conn(starter_conn_t *conn)
980 {
981 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
982 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
983 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
984 free_also(conn->also);
985 }
986
987 /*
988 * free the memory used by a starter_ca_t object
989 */
990 static void
991 confread_free_ca(starter_ca_t *ca)
992 {
993 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
994 free_also(ca->also);
995 }
996
997 /*
998 * free the memory used by a starter_config_t object
999 */
1000 void confread_free(starter_config_t *cfg)
1001 {
1002 starter_conn_t *conn = cfg->conn_first;
1003 starter_ca_t *ca = cfg->ca_first;
1004
1005 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
1006
1007 confread_free_conn(&cfg->conn_default);
1008
1009 while (conn != NULL)
1010 {
1011 starter_conn_t *conn_aux = conn;
1012
1013 conn = conn->next;
1014 confread_free_conn(conn_aux);
1015 free(conn_aux);
1016 }
1017
1018 confread_free_ca(&cfg->ca_default);
1019
1020 while (ca != NULL)
1021 {
1022 starter_ca_t *ca_aux = ca;
1023
1024 ca = ca->next;
1025 confread_free_ca(ca_aux);
1026 free(ca_aux);
1027 }
1028
1029 free(cfg);
1030 }
1031
1032 /*
1033 * load and parse an IPsec configuration file
1034 */
1035 starter_config_t* confread_load(const char *file)
1036 {
1037 starter_config_t *cfg = NULL;
1038 config_parsed_t *cfgp;
1039 section_list_t *sconn, *sca;
1040 starter_conn_t *conn;
1041 starter_ca_t *ca;
1042
1043 u_int total_err;
1044 u_int visit = 0;
1045
1046 /* load IPSec configuration file */
1047 cfgp = parser_load_conf(file);
1048 if (!cfgp)
1049 {
1050 return NULL;
1051 }
1052 cfg = malloc_thing(starter_config_t);
1053
1054 /* set default values */
1055 default_values(cfg);
1056
1057 /* determine default route */
1058 get_defaultroute(&cfg->defaultroute);
1059
1060 /* load config setup section */
1061 load_setup(cfg, cfgp);
1062
1063 /* in the first round parse also statements */
1064 cfg->parse_also = TRUE;
1065
1066 /* find %default ca section */
1067 for (sca = cfgp->ca_first; sca; sca = sca->next)
1068 {
1069 if (streq(sca->name, "%default"))
1070 {
1071 DBG(DBG_CONTROL,
1072 DBG_log("Loading ca %%default")
1073 )
1074 load_ca(&cfg->ca_default, sca->kw, cfg);
1075 }
1076 }
1077
1078 /* parameters defined in ca %default sections can be overloads */
1079 cfg->ca_default.seen = LEMPTY;
1080
1081 /* load other ca sections */
1082 for (sca = cfgp->ca_first; sca; sca = sca->next)
1083 {
1084 u_int previous_err;
1085
1086 /* skip %default ca section */
1087 if (streq(sca->name, "%default"))
1088 continue;
1089
1090 DBG(DBG_CONTROL,
1091 DBG_log("Loading ca '%s'", sca->name)
1092 )
1093 ca = malloc_thing(starter_ca_t);
1094
1095 ca_default(sca->name, ca, &cfg->ca_default);
1096 ca->kw = sca->kw;
1097 ca->next = NULL;
1098
1099 previous_err = cfg->err;
1100 load_ca(ca, ca->kw, cfg);
1101 if (cfg->err > previous_err)
1102 {
1103 /* errors occurred - free the ca */
1104 confread_free_ca(ca);
1105 cfg->non_fatal_err += cfg->err - previous_err;
1106 cfg->err = previous_err;
1107 }
1108 else
1109 {
1110 /* success - insert the ca into the chained list */
1111 if (cfg->ca_last)
1112 cfg->ca_last->next = ca;
1113 cfg->ca_last = ca;
1114 if (!cfg->ca_first)
1115 cfg->ca_first = ca;
1116 }
1117 }
1118
1119 for (ca = cfg->ca_first; ca; ca = ca->next)
1120 {
1121 also_t *also = ca->also;
1122
1123 while (also != NULL)
1124 {
1125 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
1126
1127 load_ca(ca, kw, cfg);
1128 also = also->next;
1129 }
1130
1131 if (ca->startup != STARTUP_NO)
1132 ca->state = STATE_TO_ADD;
1133 }
1134
1135 /* find %default conn sections */
1136 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1137 {
1138 if (streq(sconn->name, "%default"))
1139 {
1140 DBG(DBG_CONTROL,
1141 DBG_log("Loading conn %%default")
1142 )
1143 load_conn(&cfg->conn_default, sconn->kw, cfg);
1144 }
1145 }
1146
1147 /* parameter defined in conn %default sections can be overloaded */
1148 cfg->conn_default.seen = LEMPTY;
1149 cfg->conn_default.right.seen = LEMPTY;
1150 cfg->conn_default.left.seen = LEMPTY;
1151
1152 /* load other conn sections */
1153 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1154 {
1155 u_int previous_err;
1156
1157 /* skip %default conn section */
1158 if (streq(sconn->name, "%default"))
1159 continue;
1160
1161 DBG(DBG_CONTROL,
1162 DBG_log("Loading conn '%s'", sconn->name)
1163 )
1164 conn = malloc_thing(starter_conn_t);
1165
1166 conn_default(sconn->name, conn, &cfg->conn_default);
1167 conn->kw = sconn->kw;
1168 conn->next = NULL;
1169
1170 previous_err = cfg->err;
1171 load_conn(conn, conn->kw, cfg);
1172 if (cfg->err > previous_err)
1173 {
1174 /* error occurred - free the conn */
1175 confread_free_conn(conn);
1176 cfg->non_fatal_err += cfg->err - previous_err;
1177 cfg->err = previous_err;
1178 }
1179 else
1180 {
1181 /* success - insert the conn into the chained list */
1182 if (cfg->conn_last)
1183 cfg->conn_last->next = conn;
1184 cfg->conn_last = conn;
1185 if (!cfg->conn_first)
1186 cfg->conn_first = conn;
1187 }
1188 }
1189
1190 /* in the second round do not parse also statements */
1191 cfg->parse_also = FALSE;
1192
1193 for (ca = cfg->ca_first; ca; ca = ca->next)
1194 {
1195 ca->visit = ++visit;
1196 load_also_cas(ca, ca->also, cfg);
1197
1198 if (ca->startup != STARTUP_NO)
1199 ca->state = STATE_TO_ADD;
1200 }
1201
1202 for (conn = cfg->conn_first; conn; conn = conn->next)
1203 {
1204 conn->visit = ++visit;
1205 load_also_conns(conn, conn->also, cfg);
1206
1207 if (conn->startup != STARTUP_NO)
1208 conn->state = STATE_TO_ADD;
1209 }
1210
1211 parser_free_conf(cfgp);
1212
1213 total_err = cfg->err + cfg->non_fatal_err;
1214 if (total_err > 0)
1215 {
1216 plog("### %d parsing error%s (%d fatal) ###"
1217 , total_err, (total_err > 1)?"s":"", cfg->err);
1218 }
1219
1220 return cfg;
1221 }