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