Pass complete port range over stroke interface for more flexibility
[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
302 {
303 svc = getservbyname(port, NULL);
304 if (svc)
305 {
306 end->from_port = end->to_port = ntohs(svc->s_port);
307 }
308 else
309 {
310 p = strtol(port, &endptr, 0);
311 if ((*port && *endptr) || p < 0 || p > 0xffff)
312 {
313 DBG1(DBG_APP, "# bad port: %s=%s", name, value);
314 goto err;
315 }
316 end->from_port = end->to_port = (u_int16_t)p;
317 }
318 }
319 if (sep)
320 { /* restore the original text in case also= is used */
321 *sep = '/';
322 }
323 break;
324 }
325 default:
326 break;
327 }
328 return;
329
330 err:
331 DBG1(DBG_APP, " bad argument value in conn '%s'", conn_name);
332 cfg->err++;
333 }
334
335 /*
336 * handles left|rightfirewall and left|rightupdown parameters
337 */
338 static void handle_firewall(const char *label, starter_end_t *end,
339 starter_config_t *cfg)
340 {
341 if (end->firewall && (end->seen & SEEN_KW(KW_FIREWALL, KW_END_FIRST)))
342 {
343 if (end->updown != NULL)
344 {
345 DBG1(DBG_APP, "# cannot have both %sfirewall and %supdown", label,
346 label);
347 cfg->err++;
348 }
349 else
350 {
351 end->updown = strdupnull(firewall_defaults);
352 end->firewall = FALSE;
353 }
354 }
355 }
356
357 static bool handle_mark(char *value, mark_t *mark)
358 {
359 char *sep, *endptr;
360
361 sep = strchr(value, '/');
362 if (sep)
363 {
364 *sep = '\0';
365 mark->mask = strtoul(sep+1, &endptr, 0);
366 if (*endptr != '\0')
367 {
368 DBG1(DBG_APP, "# invalid mark mask: %s", sep+1);
369 return FALSE;
370 }
371 }
372 else
373 {
374 mark->mask = 0xffffffff;
375 }
376 if (value == '\0')
377 {
378 mark->value = 0;
379 }
380 else
381 {
382 mark->value = strtoul(value, &endptr, 0);
383 if (*endptr != '\0')
384 {
385 DBG1(DBG_APP, "# invalid mark value: %s", value);
386 return FALSE;
387 }
388 }
389 if (sep)
390 { /* restore the original text in case also= is used */
391 *sep = '/';
392 }
393 /* apply the mask to ensure the value is in range */
394 mark->value &= mark->mask;
395 return TRUE;
396 }
397
398 /*
399 * parse a conn section
400 */
401 static void load_conn(starter_conn_t *conn, kw_list_t *kw, starter_config_t *cfg)
402 {
403 char *conn_name = (conn->name == NULL)? "%default":conn->name;
404
405 for ( ; kw; kw = kw->next)
406 {
407 bool assigned = FALSE;
408
409 kw_token_t token = kw->entry->token;
410
411 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
412 {
413 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST
414 , kw, conn_name, cfg);
415 continue;
416 }
417 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
418 {
419 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST
420 , kw, conn_name, cfg);
421 continue;
422 }
423
424 if (token == KW_AUTO)
425 {
426 token = KW_CONN_SETUP;
427 }
428 else if (token == KW_ALSO)
429 {
430 if (cfg->parse_also)
431 {
432 also_t *also = malloc_thing(also_t);
433
434 also->name = strdupnull(kw->value);
435 also->next = conn->also;
436 conn->also = also;
437
438 DBG2(DBG_APP, " also=%s", kw->value);
439 }
440 continue;
441 }
442
443 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
444 {
445 DBG1(DBG_APP, "# unsupported keyword '%s' in conn '%s'",
446 kw->entry->name, conn_name);
447 cfg->err++;
448 continue;
449 }
450
451 if (is_deprecated(token, kw, conn_name))
452 {
453 cfg->non_fatal_err++;
454 continue;
455 }
456
457 if (!assign_arg(token, KW_CONN_FIRST, kw, (char *)conn, &assigned))
458 {
459 DBG1(DBG_APP, " bad argument value in conn '%s'", conn_name);
460 cfg->err++;
461 continue;
462 }
463
464 if (assigned)
465 continue;
466
467 switch (token)
468 {
469 case KW_TYPE:
470 conn->mode = MODE_TRANSPORT;
471 conn->proxy_mode = FALSE;
472 if (streq(kw->value, "tunnel"))
473 {
474 conn->mode = MODE_TUNNEL;
475 }
476 else if (streq(kw->value, "beet"))
477 {
478 conn->mode = MODE_BEET;
479 }
480 else if (streq(kw->value, "transport_proxy"))
481 {
482 conn->mode = MODE_TRANSPORT;
483 conn->proxy_mode = TRUE;
484 }
485 else if (streq(kw->value, "passthrough") || streq(kw->value, "pass"))
486 {
487 conn->mode = MODE_PASS;
488 }
489 else if (streq(kw->value, "drop") || streq(kw->value, "reject"))
490 {
491 conn->mode = MODE_DROP;
492 }
493 else if (!streq(kw->value, "transport"))
494 {
495 DBG1(DBG_APP, "# bad policy value: %s=%s", kw->entry->name,
496 kw->value);
497 cfg->err++;
498 }
499 break;
500 case KW_COMPRESS:
501 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_COMPRESS)
502 break;
503 case KW_AUTH:
504 KW_SA_OPTION_FLAG("ah", "esp", SA_OPTION_AUTHENTICATE)
505 break;
506 case KW_MARK:
507 if (!handle_mark(kw->value, &conn->mark_in))
508 {
509 cfg->err++;
510 break;
511 }
512 conn->mark_out = conn->mark_in;
513 break;
514 case KW_MARK_IN:
515 if (!handle_mark(kw->value, &conn->mark_in))
516 {
517 cfg->err++;
518 }
519 break;
520 case KW_MARK_OUT:
521 if (!handle_mark(kw->value, &conn->mark_out))
522 {
523 cfg->err++;
524 }
525 break;
526 case KW_TFC:
527 if (streq(kw->value, "%mtu"))
528 {
529 conn->tfc = -1;
530 }
531 else
532 {
533 char *endptr;
534
535 conn->tfc = strtoul(kw->value, &endptr, 10);
536 if (*endptr != '\0')
537 {
538 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
539 kw->value);
540 cfg->err++;
541 }
542 }
543 break;
544 case KW_KEYINGTRIES:
545 if (streq(kw->value, "%forever"))
546 {
547 conn->sa_keying_tries = 0;
548 }
549 else
550 {
551 char *endptr;
552
553 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
554 if (*endptr != '\0')
555 {
556 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
557 kw->value);
558 cfg->err++;
559 }
560 }
561 break;
562 case KW_REKEY:
563 KW_SA_OPTION_FLAG("no", "yes", SA_OPTION_DONT_REKEY)
564 break;
565 case KW_REAUTH:
566 KW_SA_OPTION_FLAG("no", "yes", SA_OPTION_DONT_REAUTH)
567 break;
568 case KW_MOBIKE:
569 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_MOBIKE)
570 break;
571 case KW_FORCEENCAPS:
572 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_FORCE_ENCAP)
573 break;
574 case KW_MODECONFIG:
575 KW_SA_OPTION_FLAG("push", "pull", SA_OPTION_MODECFG_PUSH)
576 break;
577 case KW_XAUTH:
578 KW_SA_OPTION_FLAG("server", "client", SA_OPTION_XAUTH_SERVER)
579 break;
580 default:
581 break;
582 }
583 }
584
585 handle_firewall("left", &conn->left, cfg);
586 handle_firewall("right", &conn->right, cfg);
587 }
588
589 /*
590 * initialize a conn object with the default conn
591 */
592 static void conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
593 {
594 memcpy(conn, def, sizeof(starter_conn_t));
595 conn->name = strdupnull(name);
596
597 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
598 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
599 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
600 }
601
602 /*
603 * parse a ca section
604 */
605 static void load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
606 {
607 char *ca_name = (ca->name == NULL)? "%default":ca->name;
608
609 for ( ; kw; kw = kw->next)
610 {
611 bool assigned = FALSE;
612
613 kw_token_t token = kw->entry->token;
614
615 if (token == KW_AUTO)
616 {
617 token = KW_CA_SETUP;
618 }
619 else if (token == KW_ALSO)
620 {
621 if (cfg->parse_also)
622 {
623 also_t *also = malloc_thing(also_t);
624
625 also->name = strdupnull(kw->value);
626 also->next = ca->also;
627 ca->also = also;
628
629 DBG2(DBG_APP, " also=%s", kw->value);
630 }
631 continue;
632 }
633
634 if (token < KW_CA_FIRST || token > KW_CA_LAST)
635 {
636 DBG1(DBG_APP, "# unsupported keyword '%s' in ca '%s'",
637 kw->entry->name, ca_name);
638 cfg->err++;
639 continue;
640 }
641
642 if (is_deprecated(token, kw, ca_name))
643 {
644 cfg->non_fatal_err++;
645 continue;
646 }
647
648 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
649 {
650 DBG1(DBG_APP, " bad argument value in ca '%s'", ca_name);
651 cfg->err++;
652 }
653 }
654
655 /* treat 'route' and 'start' as 'add' */
656 if (ca->startup != STARTUP_NO)
657 ca->startup = STARTUP_ADD;
658 }
659
660 /*
661 * initialize a ca object with the default ca
662 */
663 static void ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
664 {
665 memcpy(ca, def, sizeof(starter_ca_t));
666 ca->name = strdupnull(name);
667
668 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
669 }
670
671 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
672 starter_config_t *cfg);
673
674 static void load_also_conns(starter_conn_t *conn, also_t *also,
675 starter_config_t *cfg)
676 {
677 while (also != NULL)
678 {
679 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
680
681 if (kw == NULL)
682 {
683 DBG1(DBG_APP, " conn '%s' cannot include '%s'", conn->name,
684 also->name);
685 }
686 else
687 {
688 DBG2(DBG_APP, "conn '%s' includes '%s'", conn->name, also->name);
689 /* only load if no error occurred in the first round */
690 if (cfg->err == 0)
691 load_conn(conn, kw, cfg);
692 }
693 also = also->next;
694 }
695 }
696
697 /*
698 * find a conn included by also
699 */
700 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
701 starter_config_t *cfg)
702 {
703 starter_conn_t *c = cfg->conn_first;
704
705 while (c != NULL)
706 {
707 if (streq(name, c->name))
708 {
709 if (conn->visit == c->visit)
710 {
711 DBG1(DBG_APP, "# detected also loop");
712 cfg->err++;
713 return NULL;
714 }
715 c->visit = conn->visit;
716 load_also_conns(conn, c->also, cfg);
717 return c->kw;
718 }
719 c = c->next;
720 }
721
722 DBG1(DBG_APP, "# also '%s' not found", name);
723 cfg->err++;
724 return NULL;
725 }
726
727 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
728 starter_config_t *cfg);
729
730 static void load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
731 {
732 while (also != NULL)
733 {
734 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
735
736 if (kw == NULL)
737 {
738 DBG1(DBG_APP, " ca '%s' cannot include '%s'", ca->name,
739 also->name);
740 }
741 else
742 {
743 DBG2(DBG_APP, "ca '%s' includes '%s'", ca->name, also->name);
744 /* only load if no error occurred in the first round */
745 if (cfg->err == 0)
746 load_ca(ca, kw, cfg);
747 }
748 also = also->next;
749 }
750 }
751
752 /*
753 * find a ca included by also
754 */
755 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
756 starter_config_t *cfg)
757 {
758 starter_ca_t *c = cfg->ca_first;
759
760 while (c != NULL)
761 {
762 if (streq(name, c->name))
763 {
764 if (ca->visit == c->visit)
765 {
766 DBG1(DBG_APP, "# detected also loop");
767 cfg->err++;
768 return NULL;
769 }
770 c->visit = ca->visit;
771 load_also_cas(ca, c->also, cfg);
772 return c->kw;
773 }
774 c = c->next;
775 }
776
777 DBG1(DBG_APP, "# also '%s' not found", name);
778 cfg->err++;
779 return NULL;
780 }
781
782 /*
783 * free the memory used by also_t objects
784 */
785 static void free_also(also_t *head)
786 {
787 while (head != NULL)
788 {
789 also_t *also = head;
790
791 head = also->next;
792 free(also->name);
793 free(also);
794 }
795 }
796
797 /*
798 * free the memory used by a starter_conn_t object
799 */
800 static void confread_free_conn(starter_conn_t *conn)
801 {
802 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
803 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
804 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
805 free_also(conn->also);
806 }
807
808 /*
809 * free the memory used by a starter_ca_t object
810 */
811 static void
812 confread_free_ca(starter_ca_t *ca)
813 {
814 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
815 free_also(ca->also);
816 }
817
818 /*
819 * free the memory used by a starter_config_t object
820 */
821 void confread_free(starter_config_t *cfg)
822 {
823 starter_conn_t *conn = cfg->conn_first;
824 starter_ca_t *ca = cfg->ca_first;
825
826 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
827
828 confread_free_conn(&cfg->conn_default);
829
830 while (conn != NULL)
831 {
832 starter_conn_t *conn_aux = conn;
833
834 conn = conn->next;
835 confread_free_conn(conn_aux);
836 free(conn_aux);
837 }
838
839 confread_free_ca(&cfg->ca_default);
840
841 while (ca != NULL)
842 {
843 starter_ca_t *ca_aux = ca;
844
845 ca = ca->next;
846 confread_free_ca(ca_aux);
847 free(ca_aux);
848 }
849
850 free(cfg);
851 }
852
853 /*
854 * load and parse an IPsec configuration file
855 */
856 starter_config_t* confread_load(const char *file)
857 {
858 starter_config_t *cfg = NULL;
859 config_parsed_t *cfgp;
860 section_list_t *sconn, *sca;
861 starter_conn_t *conn;
862 starter_ca_t *ca;
863
864 u_int total_err;
865 u_int visit = 0;
866
867 /* load IPSec configuration file */
868 cfgp = parser_load_conf(file);
869 if (!cfgp)
870 {
871 return NULL;
872 }
873 cfg = malloc_thing(starter_config_t);
874
875 /* set default values */
876 default_values(cfg);
877
878 /* load config setup section */
879 load_setup(cfg, cfgp);
880
881 /* in the first round parse also statements */
882 cfg->parse_also = TRUE;
883
884 /* find %default ca section */
885 for (sca = cfgp->ca_first; sca; sca = sca->next)
886 {
887 if (streq(sca->name, "%default"))
888 {
889 DBG2(DBG_APP, "Loading ca %%default");
890 load_ca(&cfg->ca_default, sca->kw, cfg);
891 }
892 }
893
894 /* parameters defined in ca %default sections can be overloads */
895 cfg->ca_default.seen = SEEN_NONE;
896
897 /* load other ca sections */
898 for (sca = cfgp->ca_first; sca; sca = sca->next)
899 {
900 u_int previous_err;
901
902 /* skip %default ca section */
903 if (streq(sca->name, "%default"))
904 continue;
905
906 DBG2(DBG_APP, "Loading ca '%s'", sca->name);
907 ca = malloc_thing(starter_ca_t);
908
909 ca_default(sca->name, ca, &cfg->ca_default);
910 ca->kw = sca->kw;
911 ca->next = NULL;
912
913 previous_err = cfg->err;
914 load_ca(ca, ca->kw, cfg);
915 if (cfg->err > previous_err)
916 {
917 /* errors occurred - free the ca */
918 confread_free_ca(ca);
919 cfg->non_fatal_err += cfg->err - previous_err;
920 cfg->err = previous_err;
921 }
922 else
923 {
924 /* success - insert the ca into the chained list */
925 if (cfg->ca_last)
926 cfg->ca_last->next = ca;
927 cfg->ca_last = ca;
928 if (!cfg->ca_first)
929 cfg->ca_first = ca;
930 }
931 }
932
933 for (ca = cfg->ca_first; ca; ca = ca->next)
934 {
935 also_t *also = ca->also;
936
937 while (also != NULL)
938 {
939 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
940
941 load_ca(ca, kw, cfg);
942 also = also->next;
943 }
944
945 if (ca->startup != STARTUP_NO)
946 ca->state = STATE_TO_ADD;
947 }
948
949 /* find %default conn sections */
950 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
951 {
952 if (streq(sconn->name, "%default"))
953 {
954 DBG2(DBG_APP, "Loading conn %%default");
955 load_conn(&cfg->conn_default, sconn->kw, cfg);
956 }
957 }
958
959 /* parameters defined in conn %default sections can be overloaded */
960 cfg->conn_default.seen = SEEN_NONE;
961 cfg->conn_default.right.seen = SEEN_NONE;
962 cfg->conn_default.left.seen = SEEN_NONE;
963
964 /* load other conn sections */
965 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
966 {
967 u_int previous_err;
968
969 /* skip %default conn section */
970 if (streq(sconn->name, "%default"))
971 continue;
972
973 DBG2(DBG_APP, "Loading conn '%s'", sconn->name);
974 conn = malloc_thing(starter_conn_t);
975
976 conn_default(sconn->name, conn, &cfg->conn_default);
977 conn->kw = sconn->kw;
978 conn->next = NULL;
979
980 previous_err = cfg->err;
981 load_conn(conn, conn->kw, cfg);
982 if (cfg->err > previous_err)
983 {
984 /* error occurred - free the conn */
985 confread_free_conn(conn);
986 cfg->non_fatal_err += cfg->err - previous_err;
987 cfg->err = previous_err;
988 }
989 else
990 {
991 /* success - insert the conn into the chained list */
992 if (cfg->conn_last)
993 cfg->conn_last->next = conn;
994 cfg->conn_last = conn;
995 if (!cfg->conn_first)
996 cfg->conn_first = conn;
997 }
998 }
999
1000 /* in the second round do not parse also statements */
1001 cfg->parse_also = FALSE;
1002
1003 for (ca = cfg->ca_first; ca; ca = ca->next)
1004 {
1005 ca->visit = ++visit;
1006 load_also_cas(ca, ca->also, cfg);
1007
1008 if (ca->startup != STARTUP_NO)
1009 ca->state = STATE_TO_ADD;
1010 }
1011
1012 for (conn = cfg->conn_first; conn; conn = conn->next)
1013 {
1014 conn->visit = ++visit;
1015 load_also_conns(conn, conn->also, cfg);
1016
1017 if (conn->startup != STARTUP_NO)
1018 conn->state = STATE_TO_ADD;
1019 }
1020
1021 parser_free_conf(cfgp);
1022
1023 total_err = cfg->err + cfg->non_fatal_err;
1024 if (total_err > 0)
1025 {
1026 DBG1(DBG_APP, "### %d parsing error%s (%d fatal) ###",
1027 total_err, (total_err > 1)?"s":"", cfg->err);
1028 }
1029
1030 return cfg;
1031 }