starter: No special handling for left|rightsubnet, just pass it on as string.
[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 <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <assert.h>
22
23 #include <library.h>
24 #include <debug.h>
25
26 #include "../pluto/constants.h"
27 #include "../pluto/defs.h"
28
29 #include "keywords.h"
30 #include "confread.h"
31 #include "args.h"
32 #include "files.h"
33
34
35 static const char ike_defaults[] = "aes128-sha1-modp2048,3des-sha1-modp1536";
36 static const char esp_defaults[] = "aes128-sha1-modp2048,3des-sha1-modp1536";
37
38 static const char firewall_defaults[] = "ipsec _updown iptables";
39
40 static bool daemon_exists(char *daemon, char *path)
41 {
42 struct stat st;
43 if (stat(path, &st) != 0)
44 {
45 DBG1(DBG_APP, "Disabling %sstart option, '%s' not found", daemon, path);
46 return FALSE;
47 }
48 return TRUE;
49 }
50
51 static void default_values(starter_config_t *cfg)
52 {
53 if (cfg == NULL)
54 return;
55
56 memset(cfg, 0, sizeof(struct starter_config));
57
58 /* is there enough space for all seen flags? */
59 assert(KW_SETUP_LAST - KW_SETUP_FIRST <
60 sizeof(cfg->setup.seen) * BITS_PER_BYTE);
61 assert(KW_CONN_LAST - KW_CONN_FIRST <
62 sizeof(cfg->conn_default.seen) * BITS_PER_BYTE);
63 assert(KW_END_LAST - KW_END_FIRST <
64 sizeof(cfg->conn_default.right.seen) * BITS_PER_BYTE);
65 assert(KW_CA_LAST - KW_CA_FIRST <
66 sizeof(cfg->ca_default.seen) * BITS_PER_BYTE);
67
68 cfg->setup.seen = SEEN_NONE;
69 cfg->setup.fragicmp = TRUE;
70 cfg->setup.hidetos = TRUE;
71 cfg->setup.uniqueids = TRUE;
72 cfg->setup.interfaces = new_list("%defaultroute");
73
74 #ifdef START_CHARON
75 cfg->setup.charonstart = TRUE;
76 #endif
77 #ifdef START_PLUTO
78 cfg->setup.plutostart = TRUE;
79 #endif
80
81 cfg->conn_default.seen = SEEN_NONE;
82 cfg->conn_default.startup = STARTUP_NO;
83 cfg->conn_default.state = STATE_IGNORE;
84 cfg->conn_default.mode = MODE_TUNNEL;
85 cfg->conn_default.policy = POLICY_MOBIKE;
86
87 cfg->conn_default.ike = strdupnull(ike_defaults);
88 cfg->conn_default.esp = strdupnull(esp_defaults);
89 cfg->conn_default.sa_ike_life_seconds = OAKLEY_ISAKMP_SA_LIFETIME_DEFAULT;
90 cfg->conn_default.sa_ipsec_life_seconds = PLUTO_SA_LIFE_DURATION_DEFAULT;
91 cfg->conn_default.sa_rekey_margin = SA_REPLACEMENT_MARGIN_DEFAULT;
92 cfg->conn_default.sa_rekey_fuzz = SA_REPLACEMENT_FUZZ_DEFAULT;
93 cfg->conn_default.sa_keying_tries = SA_REPLACEMENT_RETRIES_DEFAULT;
94 cfg->conn_default.install_policy = TRUE;
95 cfg->conn_default.dpd_delay = 30; /* seconds */
96 cfg->conn_default.dpd_timeout = 150; /* seconds */
97
98 cfg->conn_default.left.seen = SEEN_NONE;
99 cfg->conn_default.right.seen = SEEN_NONE;
100
101 cfg->conn_default.left.sendcert = CERT_SEND_IF_ASKED;
102 cfg->conn_default.right.sendcert = CERT_SEND_IF_ASKED;
103
104 cfg->conn_default.left.ikeport = 500;
105 cfg->conn_default.right.ikeport = 500;
106
107 cfg->ca_default.seen = SEEN_NONE;
108 }
109
110 #define KW_POLICY_FLAG(sy, sn, fl) \
111 if (streq(kw->value, sy)) { conn->policy |= fl; } \
112 else if (streq(kw->value, sn)) { conn->policy &= ~fl; } \
113 else { DBG1(DBG_APP, "# bad policy value: %s=%s", kw->entry->name, kw->value); cfg->err++; }
114
115 static void load_setup(starter_config_t *cfg, config_parsed_t *cfgp)
116 {
117 kw_list_t *kw;
118
119 DBG2(DBG_APP, "Loading config setup");
120
121 for (kw = cfgp->config_setup; kw; kw = kw->next)
122 {
123 bool assigned = FALSE;
124
125 kw_token_t token = kw->entry->token;
126
127 if ((int)token < KW_SETUP_FIRST || token > KW_SETUP_LAST)
128 {
129 DBG1(DBG_APP, "# unsupported keyword '%s' in config setup",
130 kw->entry->name);
131 cfg->err++;
132 continue;
133 }
134
135 if (!assign_arg(token, KW_SETUP_FIRST, kw, (char *)cfg, &assigned))
136 {
137 DBG1(DBG_APP, " bad argument value in config setup");
138 cfg->err++;
139 continue;
140 }
141 }
142
143 /* verify the executables are actually available (some distros split
144 * packages but enabled both) */
145 #ifdef START_CHARON
146 cfg->setup.charonstart = cfg->setup.charonstart &&
147 daemon_exists("charon", CHARON_CMD);
148 #else
149 cfg->setup.charonstart = FALSE;
150 #endif
151 #ifdef START_PLUTO
152 cfg->setup.plutostart = cfg->setup.plutostart &&
153 daemon_exists("pluto", PLUTO_CMD);
154 #else
155 cfg->setup.plutostart = FALSE;
156 #endif
157 }
158
159 static void kw_end(starter_conn_t *conn, starter_end_t *end, kw_token_t token,
160 kw_list_t *kw, char *conn_name, starter_config_t *cfg)
161 {
162 err_t ugh = NULL;
163 bool assigned = FALSE;
164 bool has_port_wildcard; /* set if port is %any */
165
166 char *name = kw->entry->name;
167 char *value = kw->value;
168
169 if (!assign_arg(token, KW_END_FIRST, kw, (char *)end, &assigned))
170 goto err;
171
172 /* post processing of some keywords that were assigned automatically */
173 switch (token)
174 {
175 case KW_HOST:
176 if (value && strlen(value) > 0 && value[0] == '%')
177 { /* allow_any prefix */
178 end->allow_any = TRUE;
179 value++;
180 }
181 free(end->host);
182 end->host = strdupnull(value);
183 break;
184 case KW_SOURCEIP:
185 if (end->has_natip)
186 {
187 DBG1(DBG_APP, "# natip and sourceip cannot be defined at the same time");
188 goto err;
189 }
190 if (value[0] == '%')
191 {
192 if (streq(value, "%modeconfig") || streq(value, "%modecfg") ||
193 streq(value, "%config") || streq(value, "%cfg"))
194 {
195 /* request ip via config payload */
196 free(end->sourceip);
197 end->sourceip = NULL;
198 end->sourceip_mask = 1;
199 }
200 else
201 { /* %poolname, strip %, serve ip requests */
202 free(end->sourceip);
203 end->sourceip = strdupnull(value+1);
204 end->sourceip_mask = 0;
205 }
206 end->modecfg = TRUE;
207 }
208 else
209 {
210 host_t *host;
211 char *pos;
212
213 pos = strchr(value, '/');
214 if (pos)
215 { /* CIDR notation, address pool */
216 *pos = '\0';
217 host = host_create_from_string(value, 0);
218 if (!host)
219 {
220 DBG1(DBG_APP, "# bad subnet: %s=%s", name, value);
221 goto err;
222 }
223 host->destroy(host);
224 free(end->sourceip);
225 end->sourceip = strdupnull(value);
226 end->sourceip_mask = atoi(pos + 1);
227 }
228 else
229 { /* fixed srcip */
230 host = host_create_from_string(value, 0);
231 if (!host)
232 {
233 DBG1(DBG_APP, "# bad addr: %s=%s", name, value);
234 goto err;
235 }
236 end->sourceip_mask = (host->get_family(host) == AF_INET) ?
237 32 : 128;
238 host->destroy(host);
239 }
240 }
241 conn->mode = MODE_TUNNEL;
242 conn->proxy_mode = FALSE;
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_PROTOPORT:
265 ugh = ttoprotoport(value, 0, &end->protocol, &end->port, &has_port_wildcard);
266 end->has_port_wildcard = has_port_wildcard;
267 break;
268 case KW_NATIP:
269 {
270 host_t *host;
271 if (end->sourceip)
272 {
273 DBG1(DBG_APP, "# natip and sourceip cannot be defined at the same time");
274 goto err;
275 }
276 host = host_create_from_string(value, 0);
277 if (!host)
278 {
279 DBG1(DBG_APP, "# bad addr: %s=%s", name, value);
280 goto err;
281 }
282 host->destroy(host);
283 end->sourceip = strdupnull(value);
284 end->has_natip = TRUE;
285 conn->mode = MODE_TUNNEL;
286 conn->proxy_mode = FALSE;
287 break;
288 }
289 default:
290 break;
291 }
292 return;
293
294 err:
295 DBG1(DBG_APP, " bad argument value in conn '%s'", conn_name);
296 cfg->err++;
297 }
298
299 /*
300 * handles left|rightfirewall and left|rightupdown parameters
301 */
302 static void handle_firewall(const char *label, starter_end_t *end,
303 starter_config_t *cfg)
304 {
305 if (end->firewall && (end->seen & SEEN_KW(KW_FIREWALL, KW_END_FIRST)))
306 {
307 if (end->updown != NULL)
308 {
309 DBG1(DBG_APP, "# cannot have both %sfirewall and %supdown", label,
310 label);
311 cfg->err++;
312 }
313 else
314 {
315 end->updown = strdupnull(firewall_defaults);
316 end->firewall = FALSE;
317 }
318 }
319 }
320
321 static bool handle_mark(char *value, mark_t *mark)
322 {
323 char *pos, *endptr;
324
325 pos = strchr(value, '/');
326 if (pos)
327 {
328 *pos = '\0';
329 mark->mask = strtoul(pos+1, &endptr, 0);
330 if (*endptr != '\0')
331 {
332 DBG1(DBG_APP, "# invalid mark mask: %s", pos+1);
333 return FALSE;
334 }
335 }
336 else
337 {
338 mark->mask = 0xffffffff;
339 }
340 if (value == '\0')
341 {
342 mark->value = 0;
343 }
344 else
345 {
346 mark->value = strtoul(value, &endptr, 0);
347 if (*endptr != '\0')
348 {
349 DBG1(DBG_APP, "# invalid mark value: %s", value);
350 return FALSE;
351 }
352 }
353 return TRUE;
354 }
355
356 /*
357 * parse a conn section
358 */
359 static void load_conn(starter_conn_t *conn, kw_list_t *kw, starter_config_t *cfg)
360 {
361 char *conn_name = (conn->name == NULL)? "%default":conn->name;
362
363 for ( ; kw; kw = kw->next)
364 {
365 bool assigned = FALSE;
366
367 kw_token_t token = kw->entry->token;
368
369 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
370 {
371 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST
372 , kw, conn_name, cfg);
373 continue;
374 }
375 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
376 {
377 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST
378 , kw, conn_name, cfg);
379 continue;
380 }
381
382 if (token == KW_AUTO)
383 {
384 token = KW_CONN_SETUP;
385 }
386 else if (token == KW_ALSO)
387 {
388 if (cfg->parse_also)
389 {
390 also_t *also = malloc_thing(also_t);
391
392 also->name = strdupnull(kw->value);
393 also->next = conn->also;
394 conn->also = also;
395
396 DBG2(DBG_APP, " also=%s", kw->value);
397 }
398 continue;
399 }
400
401 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
402 {
403 DBG1(DBG_APP, "# unsupported keyword '%s' in conn '%s'",
404 kw->entry->name, conn_name);
405 cfg->err++;
406 continue;
407 }
408
409 if (!assign_arg(token, KW_CONN_FIRST, kw, (char *)conn, &assigned))
410 {
411 DBG1(DBG_APP, " bad argument value in conn '%s'", conn_name);
412 cfg->err++;
413 continue;
414 }
415
416 if (assigned)
417 continue;
418
419 switch (token)
420 {
421 case KW_TYPE:
422 conn->mode = MODE_TRANSPORT;
423 conn->proxy_mode = FALSE;
424 if (streq(kw->value, "tunnel"))
425 {
426 conn->mode = MODE_TUNNEL;
427 }
428 else if (streq(kw->value, "beet"))
429 {
430 conn->mode = MODE_BEET;
431 }
432 else if (streq(kw->value, "transport_proxy"))
433 {
434 conn->mode = MODE_TRANSPORT;
435 conn->proxy_mode = TRUE;
436 }
437 else if (streq(kw->value, "passthrough") || streq(kw->value, "pass"))
438 {
439 conn->mode = MODE_PASS;
440 }
441 else if (streq(kw->value, "drop") || streq(kw->value, "reject"))
442 {
443 conn->mode = MODE_DROP;
444 }
445 else if (!streq(kw->value, "transport"))
446 {
447 DBG1(DBG_APP, "# bad policy value: %s=%s", kw->entry->name,
448 kw->value);
449 cfg->err++;
450 }
451 break;
452 case KW_COMPRESS:
453 KW_POLICY_FLAG("yes", "no", POLICY_COMPRESS)
454 break;
455 case KW_AUTH:
456 KW_POLICY_FLAG("ah", "esp", POLICY_AUTHENTICATE)
457 break;
458 case KW_MARK:
459 if (!handle_mark(kw->value, &conn->mark_in))
460 {
461 cfg->err++;
462 break;
463 }
464 conn->mark_out = conn->mark_in;
465 break;
466 case KW_MARK_IN:
467 if (!handle_mark(kw->value, &conn->mark_in))
468 {
469 cfg->err++;
470 }
471 break;
472 case KW_MARK_OUT:
473 if (!handle_mark(kw->value, &conn->mark_out))
474 {
475 cfg->err++;
476 }
477 break;
478 case KW_TFC:
479 if (streq(kw->value, "%mtu"))
480 {
481 conn->tfc = -1;
482 }
483 else
484 {
485 char *endptr;
486
487 conn->tfc = strtoul(kw->value, &endptr, 10);
488 if (*endptr != '\0')
489 {
490 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
491 kw->value);
492 cfg->err++;
493 }
494 }
495 break;
496 case KW_KEYINGTRIES:
497 if (streq(kw->value, "%forever"))
498 {
499 conn->sa_keying_tries = 0;
500 }
501 else
502 {
503 char *endptr;
504
505 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
506 if (*endptr != '\0')
507 {
508 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
509 kw->value);
510 cfg->err++;
511 }
512 }
513 break;
514 case KW_REKEY:
515 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
516 break;
517 case KW_REAUTH:
518 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
519 break;
520 case KW_MOBIKE:
521 KW_POLICY_FLAG("yes", "no", POLICY_MOBIKE)
522 break;
523 case KW_FORCEENCAPS:
524 KW_POLICY_FLAG("yes", "no", POLICY_FORCE_ENCAP)
525 break;
526 case KW_MODECONFIG:
527 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
528 break;
529 case KW_XAUTH:
530 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
531 break;
532 default:
533 break;
534 }
535 }
536
537 handle_firewall("left", &conn->left, cfg);
538 handle_firewall("right", &conn->right, cfg);
539 }
540
541 /*
542 * initialize a conn object with the default conn
543 */
544 static void conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
545 {
546 memcpy(conn, def, sizeof(starter_conn_t));
547 conn->name = strdupnull(name);
548
549 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
550 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
551 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
552 }
553
554 /*
555 * parse a ca section
556 */
557 static void load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
558 {
559 char *ca_name = (ca->name == NULL)? "%default":ca->name;
560
561 for ( ; kw; kw = kw->next)
562 {
563 bool assigned = FALSE;
564
565 kw_token_t token = kw->entry->token;
566
567 if (token == KW_AUTO)
568 {
569 token = KW_CA_SETUP;
570 }
571 else if (token == KW_ALSO)
572 {
573 if (cfg->parse_also)
574 {
575 also_t *also = malloc_thing(also_t);
576
577 also->name = strdupnull(kw->value);
578 also->next = ca->also;
579 ca->also = also;
580
581 DBG2(DBG_APP, " also=%s", kw->value);
582 }
583 continue;
584 }
585
586 if (token < KW_CA_FIRST || token > KW_CA_LAST)
587 {
588 DBG1(DBG_APP, "# unsupported keyword '%s' in ca '%s'",
589 kw->entry->name, ca_name);
590 cfg->err++;
591 continue;
592 }
593
594 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
595 {
596 DBG1(DBG_APP, " bad argument value in ca '%s'", ca_name);
597 cfg->err++;
598 }
599 }
600
601 /* treat 'route' and 'start' as 'add' */
602 if (ca->startup != STARTUP_NO)
603 ca->startup = STARTUP_ADD;
604 }
605
606 /*
607 * initialize a ca object with the default ca
608 */
609 static void ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
610 {
611 memcpy(ca, def, sizeof(starter_ca_t));
612 ca->name = strdupnull(name);
613
614 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
615 }
616
617 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
618 starter_config_t *cfg);
619
620 static void load_also_conns(starter_conn_t *conn, also_t *also,
621 starter_config_t *cfg)
622 {
623 while (also != NULL)
624 {
625 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
626
627 if (kw == NULL)
628 {
629 DBG1(DBG_APP, " conn '%s' cannot include '%s'", conn->name,
630 also->name);
631 }
632 else
633 {
634 DBG2(DBG_APP, "conn '%s' includes '%s'", conn->name, also->name);
635 /* only load if no error occurred in the first round */
636 if (cfg->err == 0)
637 load_conn(conn, kw, cfg);
638 }
639 also = also->next;
640 }
641 }
642
643 /*
644 * find a conn included by also
645 */
646 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
647 starter_config_t *cfg)
648 {
649 starter_conn_t *c = cfg->conn_first;
650
651 while (c != NULL)
652 {
653 if (streq(name, c->name))
654 {
655 if (conn->visit == c->visit)
656 {
657 DBG1(DBG_APP, "# detected also loop");
658 cfg->err++;
659 return NULL;
660 }
661 c->visit = conn->visit;
662 load_also_conns(conn, c->also, cfg);
663 return c->kw;
664 }
665 c = c->next;
666 }
667
668 DBG1(DBG_APP, "# also '%s' not found", name);
669 cfg->err++;
670 return NULL;
671 }
672
673 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
674 starter_config_t *cfg);
675
676 static void load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
677 {
678 while (also != NULL)
679 {
680 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
681
682 if (kw == NULL)
683 {
684 DBG1(DBG_APP, " ca '%s' cannot include '%s'", ca->name,
685 also->name);
686 }
687 else
688 {
689 DBG2(DBG_APP, "ca '%s' includes '%s'", ca->name, also->name);
690 /* only load if no error occurred in the first round */
691 if (cfg->err == 0)
692 load_ca(ca, kw, cfg);
693 }
694 also = also->next;
695 }
696 }
697
698 /*
699 * find a ca included by also
700 */
701 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
702 starter_config_t *cfg)
703 {
704 starter_ca_t *c = cfg->ca_first;
705
706 while (c != NULL)
707 {
708 if (streq(name, c->name))
709 {
710 if (ca->visit == c->visit)
711 {
712 DBG1(DBG_APP, "# detected also loop");
713 cfg->err++;
714 return NULL;
715 }
716 c->visit = ca->visit;
717 load_also_cas(ca, c->also, cfg);
718 return c->kw;
719 }
720 c = c->next;
721 }
722
723 DBG1(DBG_APP, "# also '%s' not found", name);
724 cfg->err++;
725 return NULL;
726 }
727
728 /*
729 * free the memory used by also_t objects
730 */
731 static void free_also(also_t *head)
732 {
733 while (head != NULL)
734 {
735 also_t *also = head;
736
737 head = also->next;
738 free(also->name);
739 free(also);
740 }
741 }
742
743 /*
744 * free the memory used by a starter_conn_t object
745 */
746 static void confread_free_conn(starter_conn_t *conn)
747 {
748 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
749 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
750 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
751 free_also(conn->also);
752 }
753
754 /*
755 * free the memory used by a starter_ca_t object
756 */
757 static void
758 confread_free_ca(starter_ca_t *ca)
759 {
760 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
761 free_also(ca->also);
762 }
763
764 /*
765 * free the memory used by a starter_config_t object
766 */
767 void confread_free(starter_config_t *cfg)
768 {
769 starter_conn_t *conn = cfg->conn_first;
770 starter_ca_t *ca = cfg->ca_first;
771
772 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
773
774 confread_free_conn(&cfg->conn_default);
775
776 while (conn != NULL)
777 {
778 starter_conn_t *conn_aux = conn;
779
780 conn = conn->next;
781 confread_free_conn(conn_aux);
782 free(conn_aux);
783 }
784
785 confread_free_ca(&cfg->ca_default);
786
787 while (ca != NULL)
788 {
789 starter_ca_t *ca_aux = ca;
790
791 ca = ca->next;
792 confread_free_ca(ca_aux);
793 free(ca_aux);
794 }
795
796 free(cfg);
797 }
798
799 /*
800 * load and parse an IPsec configuration file
801 */
802 starter_config_t* confread_load(const char *file)
803 {
804 starter_config_t *cfg = NULL;
805 config_parsed_t *cfgp;
806 section_list_t *sconn, *sca;
807 starter_conn_t *conn;
808 starter_ca_t *ca;
809
810 u_int total_err;
811 u_int visit = 0;
812
813 /* load IPSec configuration file */
814 cfgp = parser_load_conf(file);
815 if (!cfgp)
816 {
817 return NULL;
818 }
819 cfg = malloc_thing(starter_config_t);
820
821 /* set default values */
822 default_values(cfg);
823
824 /* load config setup section */
825 load_setup(cfg, cfgp);
826
827 /* in the first round parse also statements */
828 cfg->parse_also = TRUE;
829
830 /* find %default ca section */
831 for (sca = cfgp->ca_first; sca; sca = sca->next)
832 {
833 if (streq(sca->name, "%default"))
834 {
835 DBG2(DBG_APP, "Loading ca %%default");
836 load_ca(&cfg->ca_default, sca->kw, cfg);
837 }
838 }
839
840 /* parameters defined in ca %default sections can be overloads */
841 cfg->ca_default.seen = SEEN_NONE;
842
843 /* load other ca sections */
844 for (sca = cfgp->ca_first; sca; sca = sca->next)
845 {
846 u_int previous_err;
847
848 /* skip %default ca section */
849 if (streq(sca->name, "%default"))
850 continue;
851
852 DBG2(DBG_APP, "Loading ca '%s'", sca->name);
853 ca = malloc_thing(starter_ca_t);
854
855 ca_default(sca->name, ca, &cfg->ca_default);
856 ca->kw = sca->kw;
857 ca->next = NULL;
858
859 previous_err = cfg->err;
860 load_ca(ca, ca->kw, cfg);
861 if (cfg->err > previous_err)
862 {
863 /* errors occurred - free the ca */
864 confread_free_ca(ca);
865 cfg->non_fatal_err += cfg->err - previous_err;
866 cfg->err = previous_err;
867 }
868 else
869 {
870 /* success - insert the ca into the chained list */
871 if (cfg->ca_last)
872 cfg->ca_last->next = ca;
873 cfg->ca_last = ca;
874 if (!cfg->ca_first)
875 cfg->ca_first = ca;
876 }
877 }
878
879 for (ca = cfg->ca_first; ca; ca = ca->next)
880 {
881 also_t *also = ca->also;
882
883 while (also != NULL)
884 {
885 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
886
887 load_ca(ca, kw, cfg);
888 also = also->next;
889 }
890
891 if (ca->startup != STARTUP_NO)
892 ca->state = STATE_TO_ADD;
893 }
894
895 /* find %default conn sections */
896 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
897 {
898 if (streq(sconn->name, "%default"))
899 {
900 DBG2(DBG_APP, "Loading conn %%default");
901 load_conn(&cfg->conn_default, sconn->kw, cfg);
902 }
903 }
904
905 /* parameters defined in conn %default sections can be overloaded */
906 cfg->conn_default.seen = SEEN_NONE;
907 cfg->conn_default.right.seen = SEEN_NONE;
908 cfg->conn_default.left.seen = SEEN_NONE;
909
910 /* load other conn sections */
911 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
912 {
913 u_int previous_err;
914
915 /* skip %default conn section */
916 if (streq(sconn->name, "%default"))
917 continue;
918
919 DBG2(DBG_APP, "Loading conn '%s'", sconn->name);
920 conn = malloc_thing(starter_conn_t);
921
922 conn_default(sconn->name, conn, &cfg->conn_default);
923 conn->kw = sconn->kw;
924 conn->next = NULL;
925
926 previous_err = cfg->err;
927 load_conn(conn, conn->kw, cfg);
928 if (cfg->err > previous_err)
929 {
930 /* error occurred - free the conn */
931 confread_free_conn(conn);
932 cfg->non_fatal_err += cfg->err - previous_err;
933 cfg->err = previous_err;
934 }
935 else
936 {
937 /* success - insert the conn into the chained list */
938 if (cfg->conn_last)
939 cfg->conn_last->next = conn;
940 cfg->conn_last = conn;
941 if (!cfg->conn_first)
942 cfg->conn_first = conn;
943 }
944 }
945
946 /* in the second round do not parse also statements */
947 cfg->parse_also = FALSE;
948
949 for (ca = cfg->ca_first; ca; ca = ca->next)
950 {
951 ca->visit = ++visit;
952 load_also_cas(ca, ca->also, cfg);
953
954 if (ca->startup != STARTUP_NO)
955 ca->state = STATE_TO_ADD;
956 }
957
958 for (conn = cfg->conn_first; conn; conn = conn->next)
959 {
960 conn->visit = ++visit;
961 load_also_conns(conn, conn->also, cfg);
962
963 if (conn->startup != STARTUP_NO)
964 conn->state = STATE_TO_ADD;
965 }
966
967 parser_free_conf(cfgp);
968
969 total_err = cfg->err + cfg->non_fatal_err;
970 if (total_err > 0)
971 {
972 DBG1(DBG_APP, "### %d parsing error%s (%d fatal) ###",
973 total_err, (total_err > 1)?"s":"", cfg->err);
974 }
975
976 return cfg;
977 }