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