cec9399e326f60ec86eb08ce69bc8cc3a2dc9cd0
[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_SCRIPT " _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(daemon_name, 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_MARK:
525 if (!handle_mark(kw->value, &conn->mark_in))
526 {
527 cfg->err++;
528 break;
529 }
530 conn->mark_out = conn->mark_in;
531 break;
532 case KW_MARK_IN:
533 if (!handle_mark(kw->value, &conn->mark_in))
534 {
535 cfg->err++;
536 }
537 break;
538 case KW_MARK_OUT:
539 if (!handle_mark(kw->value, &conn->mark_out))
540 {
541 cfg->err++;
542 }
543 break;
544 case KW_TFC:
545 if (streq(kw->value, "%mtu"))
546 {
547 conn->tfc = -1;
548 }
549 else
550 {
551 char *endptr;
552
553 conn->tfc = 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_KEYINGTRIES:
563 if (streq(kw->value, "%forever"))
564 {
565 conn->sa_keying_tries = 0;
566 }
567 else
568 {
569 char *endptr;
570
571 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
572 if (*endptr != '\0')
573 {
574 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
575 kw->value);
576 cfg->err++;
577 }
578 }
579 break;
580 case KW_REKEY:
581 KW_SA_OPTION_FLAG("no", "yes", SA_OPTION_DONT_REKEY)
582 break;
583 case KW_REAUTH:
584 KW_SA_OPTION_FLAG("no", "yes", SA_OPTION_DONT_REAUTH)
585 break;
586 case KW_MOBIKE:
587 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_MOBIKE)
588 break;
589 case KW_FORCEENCAPS:
590 KW_SA_OPTION_FLAG("yes", "no", SA_OPTION_FORCE_ENCAP)
591 break;
592 case KW_MODECONFIG:
593 KW_SA_OPTION_FLAG("push", "pull", SA_OPTION_MODECFG_PUSH)
594 break;
595 case KW_XAUTH:
596 KW_SA_OPTION_FLAG("server", "client", SA_OPTION_XAUTH_SERVER)
597 break;
598 default:
599 break;
600 }
601 }
602
603 handle_firewall("left", &conn->left, cfg);
604 handle_firewall("right", &conn->right, cfg);
605 }
606
607 /*
608 * initialize a conn object with the default conn
609 */
610 static void conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
611 {
612 memcpy(conn, def, sizeof(starter_conn_t));
613 conn->name = strdupnull(name);
614
615 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
616 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
617 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
618 }
619
620 /*
621 * parse a ca section
622 */
623 static void load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
624 {
625 char *ca_name = (ca->name == NULL)? "%default":ca->name;
626
627 for ( ; kw; kw = kw->next)
628 {
629 bool assigned = FALSE;
630
631 kw_token_t token = kw->entry->token;
632
633 if (token == KW_AUTO)
634 {
635 token = KW_CA_SETUP;
636 }
637 else if (token == KW_ALSO)
638 {
639 if (cfg->parse_also)
640 {
641 also_t *also = malloc_thing(also_t);
642
643 also->name = strdupnull(kw->value);
644 also->next = ca->also;
645 ca->also = also;
646
647 DBG2(DBG_APP, " also=%s", kw->value);
648 }
649 continue;
650 }
651
652 if (token < KW_CA_FIRST || token > KW_CA_LAST)
653 {
654 DBG1(DBG_APP, "# unsupported keyword '%s' in ca '%s'",
655 kw->entry->name, ca_name);
656 cfg->err++;
657 continue;
658 }
659
660 if (is_deprecated(token, kw, ca_name))
661 {
662 cfg->non_fatal_err++;
663 continue;
664 }
665
666 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
667 {
668 DBG1(DBG_APP, " bad argument value in ca '%s'", ca_name);
669 cfg->err++;
670 }
671 }
672
673 /* treat 'route' and 'start' as 'add' */
674 if (ca->startup != STARTUP_NO)
675 ca->startup = STARTUP_ADD;
676 }
677
678 /*
679 * initialize a ca object with the default ca
680 */
681 static void ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
682 {
683 memcpy(ca, def, sizeof(starter_ca_t));
684 ca->name = strdupnull(name);
685
686 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
687 }
688
689 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
690 starter_config_t *cfg);
691
692 static void load_also_conns(starter_conn_t *conn, also_t *also,
693 starter_config_t *cfg)
694 {
695 while (also != NULL)
696 {
697 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
698
699 if (kw == NULL)
700 {
701 DBG1(DBG_APP, " conn '%s' cannot include '%s'", conn->name,
702 also->name);
703 }
704 else
705 {
706 DBG2(DBG_APP, "conn '%s' includes '%s'", conn->name, also->name);
707 /* only load if no error occurred in the first round */
708 if (cfg->err == 0)
709 load_conn(conn, kw, cfg);
710 }
711 also = also->next;
712 }
713 }
714
715 /*
716 * find a conn included by also
717 */
718 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
719 starter_config_t *cfg)
720 {
721 starter_conn_t *c = cfg->conn_first;
722
723 while (c != NULL)
724 {
725 if (streq(name, c->name))
726 {
727 if (conn->visit == c->visit)
728 {
729 DBG1(DBG_APP, "# detected also loop");
730 cfg->err++;
731 return NULL;
732 }
733 c->visit = conn->visit;
734 load_also_conns(conn, c->also, cfg);
735 return c->kw;
736 }
737 c = c->next;
738 }
739
740 DBG1(DBG_APP, "# also '%s' not found", name);
741 cfg->err++;
742 return NULL;
743 }
744
745 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
746 starter_config_t *cfg);
747
748 static void load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
749 {
750 while (also != NULL)
751 {
752 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
753
754 if (kw == NULL)
755 {
756 DBG1(DBG_APP, " ca '%s' cannot include '%s'", ca->name,
757 also->name);
758 }
759 else
760 {
761 DBG2(DBG_APP, "ca '%s' includes '%s'", ca->name, also->name);
762 /* only load if no error occurred in the first round */
763 if (cfg->err == 0)
764 load_ca(ca, kw, cfg);
765 }
766 also = also->next;
767 }
768 }
769
770 /*
771 * find a ca included by also
772 */
773 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
774 starter_config_t *cfg)
775 {
776 starter_ca_t *c = cfg->ca_first;
777
778 while (c != NULL)
779 {
780 if (streq(name, c->name))
781 {
782 if (ca->visit == c->visit)
783 {
784 DBG1(DBG_APP, "# detected also loop");
785 cfg->err++;
786 return NULL;
787 }
788 c->visit = ca->visit;
789 load_also_cas(ca, c->also, cfg);
790 return c->kw;
791 }
792 c = c->next;
793 }
794
795 DBG1(DBG_APP, "# also '%s' not found", name);
796 cfg->err++;
797 return NULL;
798 }
799
800 /*
801 * free the memory used by also_t objects
802 */
803 static void free_also(also_t *head)
804 {
805 while (head != NULL)
806 {
807 also_t *also = head;
808
809 head = also->next;
810 free(also->name);
811 free(also);
812 }
813 }
814
815 /*
816 * free the memory used by a starter_conn_t object
817 */
818 static void confread_free_conn(starter_conn_t *conn)
819 {
820 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
821 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
822 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
823 free_also(conn->also);
824 }
825
826 /*
827 * free the memory used by a starter_ca_t object
828 */
829 static void
830 confread_free_ca(starter_ca_t *ca)
831 {
832 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
833 free_also(ca->also);
834 }
835
836 /*
837 * free the memory used by a starter_config_t object
838 */
839 void confread_free(starter_config_t *cfg)
840 {
841 starter_conn_t *conn = cfg->conn_first;
842 starter_ca_t *ca = cfg->ca_first;
843
844 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
845
846 confread_free_conn(&cfg->conn_default);
847
848 while (conn != NULL)
849 {
850 starter_conn_t *conn_aux = conn;
851
852 conn = conn->next;
853 confread_free_conn(conn_aux);
854 free(conn_aux);
855 }
856
857 confread_free_ca(&cfg->ca_default);
858
859 while (ca != NULL)
860 {
861 starter_ca_t *ca_aux = ca;
862
863 ca = ca->next;
864 confread_free_ca(ca_aux);
865 free(ca_aux);
866 }
867
868 free(cfg);
869 }
870
871 /*
872 * load and parse an IPsec configuration file
873 */
874 starter_config_t* confread_load(const char *file)
875 {
876 starter_config_t *cfg = NULL;
877 config_parsed_t *cfgp;
878 section_list_t *sconn, *sca;
879 starter_conn_t *conn;
880 starter_ca_t *ca;
881
882 u_int total_err;
883 u_int visit = 0;
884
885 /* load IPSec configuration file */
886 cfgp = parser_load_conf(file);
887 if (!cfgp)
888 {
889 return NULL;
890 }
891 cfg = malloc_thing(starter_config_t);
892
893 /* set default values */
894 default_values(cfg);
895
896 /* load config setup section */
897 load_setup(cfg, cfgp);
898
899 /* in the first round parse also statements */
900 cfg->parse_also = TRUE;
901
902 /* find %default ca section */
903 for (sca = cfgp->ca_first; sca; sca = sca->next)
904 {
905 if (streq(sca->name, "%default"))
906 {
907 DBG2(DBG_APP, "Loading ca %%default");
908 load_ca(&cfg->ca_default, sca->kw, cfg);
909 }
910 }
911
912 /* parameters defined in ca %default sections can be overloads */
913 cfg->ca_default.seen = SEEN_NONE;
914
915 /* load other ca sections */
916 for (sca = cfgp->ca_first; sca; sca = sca->next)
917 {
918 u_int previous_err;
919
920 /* skip %default ca section */
921 if (streq(sca->name, "%default"))
922 continue;
923
924 DBG2(DBG_APP, "Loading ca '%s'", sca->name);
925 ca = malloc_thing(starter_ca_t);
926
927 ca_default(sca->name, ca, &cfg->ca_default);
928 ca->kw = sca->kw;
929 ca->next = NULL;
930
931 previous_err = cfg->err;
932 load_ca(ca, ca->kw, cfg);
933 if (cfg->err > previous_err)
934 {
935 /* errors occurred - free the ca */
936 confread_free_ca(ca);
937 cfg->non_fatal_err += cfg->err - previous_err;
938 cfg->err = previous_err;
939 }
940 else
941 {
942 /* success - insert the ca into the chained list */
943 if (cfg->ca_last)
944 cfg->ca_last->next = ca;
945 cfg->ca_last = ca;
946 if (!cfg->ca_first)
947 cfg->ca_first = ca;
948 }
949 }
950
951 for (ca = cfg->ca_first; ca; ca = ca->next)
952 {
953 also_t *also = ca->also;
954
955 while (also != NULL)
956 {
957 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
958
959 load_ca(ca, kw, cfg);
960 also = also->next;
961 }
962
963 if (ca->startup != STARTUP_NO)
964 ca->state = STATE_TO_ADD;
965 }
966
967 /* find %default conn sections */
968 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
969 {
970 if (streq(sconn->name, "%default"))
971 {
972 DBG2(DBG_APP, "Loading conn %%default");
973 load_conn(&cfg->conn_default, sconn->kw, cfg);
974 }
975 }
976
977 /* parameters defined in conn %default sections can be overloaded */
978 cfg->conn_default.seen = SEEN_NONE;
979 cfg->conn_default.right.seen = SEEN_NONE;
980 cfg->conn_default.left.seen = SEEN_NONE;
981
982 /* load other conn sections */
983 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
984 {
985 u_int previous_err;
986
987 /* skip %default conn section */
988 if (streq(sconn->name, "%default"))
989 continue;
990
991 DBG2(DBG_APP, "Loading conn '%s'", sconn->name);
992 conn = malloc_thing(starter_conn_t);
993
994 conn_default(sconn->name, conn, &cfg->conn_default);
995 conn->kw = sconn->kw;
996 conn->next = NULL;
997
998 previous_err = cfg->err;
999 load_conn(conn, conn->kw, cfg);
1000 if (cfg->err > previous_err)
1001 {
1002 /* error occurred - free the conn */
1003 confread_free_conn(conn);
1004 cfg->non_fatal_err += cfg->err - previous_err;
1005 cfg->err = previous_err;
1006 }
1007 else
1008 {
1009 /* success - insert the conn into the chained list */
1010 if (cfg->conn_last)
1011 cfg->conn_last->next = conn;
1012 cfg->conn_last = conn;
1013 if (!cfg->conn_first)
1014 cfg->conn_first = conn;
1015 }
1016 }
1017
1018 /* in the second round do not parse also statements */
1019 cfg->parse_also = FALSE;
1020
1021 for (ca = cfg->ca_first; ca; ca = ca->next)
1022 {
1023 ca->visit = ++visit;
1024 load_also_cas(ca, ca->also, cfg);
1025
1026 if (ca->startup != STARTUP_NO)
1027 ca->state = STATE_TO_ADD;
1028 }
1029
1030 for (conn = cfg->conn_first; conn; conn = conn->next)
1031 {
1032 conn->visit = ++visit;
1033 load_also_conns(conn, conn->also, cfg);
1034
1035 if (conn->startup != STARTUP_NO)
1036 conn->state = STATE_TO_ADD;
1037 }
1038
1039 parser_free_conf(cfgp);
1040
1041 total_err = cfg->err + cfg->non_fatal_err;
1042 if (total_err > 0)
1043 {
1044 DBG1(DBG_APP, "### %d parsing error%s (%d fatal) ###",
1045 total_err, (total_err > 1)?"s":"", cfg->err);
1046 }
1047
1048 return cfg;
1049 }