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