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