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