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