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