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