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