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