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