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