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