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