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