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