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