47afc55593ca475d7c731edbed1317f58e8beea3
[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 = clone_str(ike_defaults);
90 cfg->conn_default.esp = clone_str(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 = clone_str(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 = clone_str(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 = clone_str(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 = clone_str(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 = clone_str(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 = clone_str(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 = clone_str(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_AUTHBY:
592 conn->policy &= ~(POLICY_ID_AUTH_MASK | POLICY_ENCRYPT);
593
594 if (!streq(kw->value, "never"))
595 {
596 char *value = kw->value;
597 char *second = strchr(kw->value, '|');
598
599 if (second != NULL)
600 {
601 *second = '\0';
602 }
603
604 /* also handles the cases secret|rsasig and rsasig|secret */
605 for (;;)
606 {
607 if (streq(value, "rsa") || streq(value, "rsasig") ||
608 streq(value, "ecdsa") || streq(value, "ecdsasig") ||
609 streq(value, "pubkey"))
610 {
611 conn->policy |= POLICY_PUBKEY | POLICY_ENCRYPT;
612 }
613 else if (streq(value, "secret") || streq(value, "psk"))
614 {
615 conn->policy |= POLICY_PSK | POLICY_ENCRYPT;
616 }
617 else if (streq(value, "xauthrsasig"))
618 {
619 conn->policy |= POLICY_XAUTH_RSASIG | POLICY_ENCRYPT;
620 }
621 else if (streq(value, "xauthpsk"))
622 {
623 conn->policy |= POLICY_XAUTH_PSK | POLICY_ENCRYPT;
624 }
625 else
626 {
627 DBG1(DBG_APP, "# bad policy value: %s=%s",
628 kw->entry->name, kw->value);
629 cfg->err++;
630 break;
631 }
632 if (second == NULL)
633 {
634 break;
635 }
636 value = second;
637 second = NULL; /* traverse the loop no more than twice */
638 }
639 }
640 break;
641 case KW_MARK:
642 if (!handle_mark(kw->value, &conn->mark_in))
643 {
644 cfg->err++;
645 break;
646 }
647 conn->mark_out = conn->mark_in;
648 break;
649 case KW_MARK_IN:
650 if (!handle_mark(kw->value, &conn->mark_in))
651 {
652 cfg->err++;
653 }
654 break;
655 case KW_MARK_OUT:
656 if (!handle_mark(kw->value, &conn->mark_out))
657 {
658 cfg->err++;
659 }
660 break;
661 case KW_TFC:
662 if (streq(kw->value, "%mtu"))
663 {
664 conn->tfc = -1;
665 }
666 else
667 {
668 char *endptr;
669
670 conn->tfc = strtoul(kw->value, &endptr, 10);
671 if (*endptr != '\0')
672 {
673 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
674 kw->value);
675 cfg->err++;
676 }
677 }
678 break;
679 case KW_KEYINGTRIES:
680 if (streq(kw->value, "%forever"))
681 {
682 conn->sa_keying_tries = 0;
683 }
684 else
685 {
686 char *endptr;
687
688 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
689 if (*endptr != '\0')
690 {
691 DBG1(DBG_APP, "# bad integer value: %s=%s", kw->entry->name,
692 kw->value);
693 cfg->err++;
694 }
695 }
696 break;
697 case KW_REKEY:
698 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
699 break;
700 case KW_REAUTH:
701 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
702 break;
703 case KW_MOBIKE:
704 KW_POLICY_FLAG("yes", "no", POLICY_MOBIKE)
705 break;
706 case KW_FORCEENCAPS:
707 KW_POLICY_FLAG("yes", "no", POLICY_FORCE_ENCAP)
708 break;
709 case KW_MODECONFIG:
710 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
711 break;
712 case KW_XAUTH:
713 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
714 break;
715 default:
716 break;
717 }
718 }
719
720 handle_dns_failure("left", &conn->left, cfg, conn);
721 handle_dns_failure("right", &conn->right, cfg, conn);
722 handle_firewall("left", &conn->left, cfg);
723 handle_firewall("right", &conn->right, cfg);
724 }
725
726 /*
727 * initialize a conn object with the default conn
728 */
729 static void conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
730 {
731 memcpy(conn, def, sizeof(starter_conn_t));
732 conn->name = clone_str(name);
733
734 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
735 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
736 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
737 }
738
739 /*
740 * parse a ca section
741 */
742 static void load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
743 {
744 char *ca_name = (ca->name == NULL)? "%default":ca->name;
745
746 for ( ; kw; kw = kw->next)
747 {
748 bool assigned = FALSE;
749
750 kw_token_t token = kw->entry->token;
751
752 if (token == KW_AUTO)
753 {
754 token = KW_CA_SETUP;
755 }
756 else if (token == KW_ALSO)
757 {
758 if (cfg->parse_also)
759 {
760 also_t *also = malloc_thing(also_t);
761
762 also->name = clone_str(kw->value);
763 also->next = ca->also;
764 ca->also = also;
765
766 DBG2(DBG_APP, " also=%s", kw->value);
767 }
768 continue;
769 }
770
771 if (token < KW_CA_FIRST || token > KW_CA_LAST)
772 {
773 DBG1(DBG_APP, "# unsupported keyword '%s' in ca '%s'",
774 kw->entry->name, ca_name);
775 cfg->err++;
776 continue;
777 }
778
779 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
780 {
781 DBG1(DBG_APP, " bad argument value in ca '%s'", ca_name);
782 cfg->err++;
783 }
784 }
785
786 /* treat 'route' and 'start' as 'add' */
787 if (ca->startup != STARTUP_NO)
788 ca->startup = STARTUP_ADD;
789 }
790
791 /*
792 * initialize a ca object with the default ca
793 */
794 static void ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
795 {
796 memcpy(ca, def, sizeof(starter_ca_t));
797 ca->name = clone_str(name);
798
799 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
800 }
801
802 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
803 starter_config_t *cfg);
804
805 static void load_also_conns(starter_conn_t *conn, also_t *also,
806 starter_config_t *cfg)
807 {
808 while (also != NULL)
809 {
810 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
811
812 if (kw == NULL)
813 {
814 DBG1(DBG_APP, " conn '%s' cannot include '%s'", conn->name,
815 also->name);
816 }
817 else
818 {
819 DBG2(DBG_APP, "conn '%s' includes '%s'", conn->name, also->name);
820 /* only load if no error occurred in the first round */
821 if (cfg->err == 0)
822 load_conn(conn, kw, cfg);
823 }
824 also = also->next;
825 }
826 }
827
828 /*
829 * find a conn included by also
830 */
831 static kw_list_t* find_also_conn(const char* name, starter_conn_t *conn,
832 starter_config_t *cfg)
833 {
834 starter_conn_t *c = cfg->conn_first;
835
836 while (c != NULL)
837 {
838 if (streq(name, c->name))
839 {
840 if (conn->visit == c->visit)
841 {
842 DBG1(DBG_APP, "# detected also loop");
843 cfg->err++;
844 return NULL;
845 }
846 c->visit = conn->visit;
847 load_also_conns(conn, c->also, cfg);
848 return c->kw;
849 }
850 c = c->next;
851 }
852
853 DBG1(DBG_APP, "# also '%s' not found", name);
854 cfg->err++;
855 return NULL;
856 }
857
858 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
859 starter_config_t *cfg);
860
861 static void load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
862 {
863 while (also != NULL)
864 {
865 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
866
867 if (kw == NULL)
868 {
869 DBG1(DBG_APP, " ca '%s' cannot include '%s'", ca->name,
870 also->name);
871 }
872 else
873 {
874 DBG2(DBG_APP, "ca '%s' includes '%s'", ca->name, also->name);
875 /* only load if no error occurred in the first round */
876 if (cfg->err == 0)
877 load_ca(ca, kw, cfg);
878 }
879 also = also->next;
880 }
881 }
882
883 /*
884 * find a ca included by also
885 */
886 static kw_list_t* find_also_ca(const char* name, starter_ca_t *ca,
887 starter_config_t *cfg)
888 {
889 starter_ca_t *c = cfg->ca_first;
890
891 while (c != NULL)
892 {
893 if (streq(name, c->name))
894 {
895 if (ca->visit == c->visit)
896 {
897 DBG1(DBG_APP, "# detected also loop");
898 cfg->err++;
899 return NULL;
900 }
901 c->visit = ca->visit;
902 load_also_cas(ca, c->also, cfg);
903 return c->kw;
904 }
905 c = c->next;
906 }
907
908 DBG1(DBG_APP, "# also '%s' not found", name);
909 cfg->err++;
910 return NULL;
911 }
912
913 /*
914 * free the memory used by also_t objects
915 */
916 static void free_also(also_t *head)
917 {
918 while (head != NULL)
919 {
920 also_t *also = head;
921
922 head = also->next;
923 free(also->name);
924 free(also);
925 }
926 }
927
928 /*
929 * free the memory used by a starter_conn_t object
930 */
931 static void confread_free_conn(starter_conn_t *conn)
932 {
933 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
934 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
935 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
936 free_also(conn->also);
937 }
938
939 /*
940 * free the memory used by a starter_ca_t object
941 */
942 static void
943 confread_free_ca(starter_ca_t *ca)
944 {
945 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
946 free_also(ca->also);
947 }
948
949 /*
950 * free the memory used by a starter_config_t object
951 */
952 void confread_free(starter_config_t *cfg)
953 {
954 starter_conn_t *conn = cfg->conn_first;
955 starter_ca_t *ca = cfg->ca_first;
956
957 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
958
959 confread_free_conn(&cfg->conn_default);
960
961 while (conn != NULL)
962 {
963 starter_conn_t *conn_aux = conn;
964
965 conn = conn->next;
966 confread_free_conn(conn_aux);
967 free(conn_aux);
968 }
969
970 confread_free_ca(&cfg->ca_default);
971
972 while (ca != NULL)
973 {
974 starter_ca_t *ca_aux = ca;
975
976 ca = ca->next;
977 confread_free_ca(ca_aux);
978 free(ca_aux);
979 }
980
981 free(cfg);
982 }
983
984 /*
985 * load and parse an IPsec configuration file
986 */
987 starter_config_t* confread_load(const char *file)
988 {
989 starter_config_t *cfg = NULL;
990 config_parsed_t *cfgp;
991 section_list_t *sconn, *sca;
992 starter_conn_t *conn;
993 starter_ca_t *ca;
994
995 u_int total_err;
996 u_int visit = 0;
997
998 /* load IPSec configuration file */
999 cfgp = parser_load_conf(file);
1000 if (!cfgp)
1001 {
1002 return NULL;
1003 }
1004 cfg = malloc_thing(starter_config_t);
1005
1006 /* set default values */
1007 default_values(cfg);
1008
1009 /* load config setup section */
1010 load_setup(cfg, cfgp);
1011
1012 /* in the first round parse also statements */
1013 cfg->parse_also = TRUE;
1014
1015 /* find %default ca section */
1016 for (sca = cfgp->ca_first; sca; sca = sca->next)
1017 {
1018 if (streq(sca->name, "%default"))
1019 {
1020 DBG2(DBG_APP, "Loading ca %%default");
1021 load_ca(&cfg->ca_default, sca->kw, cfg);
1022 }
1023 }
1024
1025 /* parameters defined in ca %default sections can be overloads */
1026 cfg->ca_default.seen = LEMPTY;
1027
1028 /* load other ca sections */
1029 for (sca = cfgp->ca_first; sca; sca = sca->next)
1030 {
1031 u_int previous_err;
1032
1033 /* skip %default ca section */
1034 if (streq(sca->name, "%default"))
1035 continue;
1036
1037 DBG2(DBG_APP, "Loading ca '%s'", sca->name);
1038 ca = malloc_thing(starter_ca_t);
1039
1040 ca_default(sca->name, ca, &cfg->ca_default);
1041 ca->kw = sca->kw;
1042 ca->next = NULL;
1043
1044 previous_err = cfg->err;
1045 load_ca(ca, ca->kw, cfg);
1046 if (cfg->err > previous_err)
1047 {
1048 /* errors occurred - free the ca */
1049 confread_free_ca(ca);
1050 cfg->non_fatal_err += cfg->err - previous_err;
1051 cfg->err = previous_err;
1052 }
1053 else
1054 {
1055 /* success - insert the ca into the chained list */
1056 if (cfg->ca_last)
1057 cfg->ca_last->next = ca;
1058 cfg->ca_last = ca;
1059 if (!cfg->ca_first)
1060 cfg->ca_first = ca;
1061 }
1062 }
1063
1064 for (ca = cfg->ca_first; ca; ca = ca->next)
1065 {
1066 also_t *also = ca->also;
1067
1068 while (also != NULL)
1069 {
1070 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
1071
1072 load_ca(ca, kw, cfg);
1073 also = also->next;
1074 }
1075
1076 if (ca->startup != STARTUP_NO)
1077 ca->state = STATE_TO_ADD;
1078 }
1079
1080 /* find %default conn sections */
1081 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1082 {
1083 if (streq(sconn->name, "%default"))
1084 {
1085 DBG2(DBG_APP, "Loading conn %%default");
1086 load_conn(&cfg->conn_default, sconn->kw, cfg);
1087 }
1088 }
1089
1090 /* parameter defined in conn %default sections can be overloaded */
1091 cfg->conn_default.seen = LEMPTY;
1092 cfg->conn_default.right.seen = LEMPTY;
1093 cfg->conn_default.left.seen = LEMPTY;
1094
1095 /* load other conn sections */
1096 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
1097 {
1098 u_int previous_err;
1099
1100 /* skip %default conn section */
1101 if (streq(sconn->name, "%default"))
1102 continue;
1103
1104 DBG2(DBG_APP, "Loading conn '%s'", sconn->name);
1105 conn = malloc_thing(starter_conn_t);
1106
1107 conn_default(sconn->name, conn, &cfg->conn_default);
1108 conn->kw = sconn->kw;
1109 conn->next = NULL;
1110
1111 previous_err = cfg->err;
1112 load_conn(conn, conn->kw, cfg);
1113 if (cfg->err > previous_err)
1114 {
1115 /* error occurred - free the conn */
1116 confread_free_conn(conn);
1117 cfg->non_fatal_err += cfg->err - previous_err;
1118 cfg->err = previous_err;
1119 }
1120 else
1121 {
1122 /* success - insert the conn into the chained list */
1123 if (cfg->conn_last)
1124 cfg->conn_last->next = conn;
1125 cfg->conn_last = conn;
1126 if (!cfg->conn_first)
1127 cfg->conn_first = conn;
1128 }
1129 }
1130
1131 /* in the second round do not parse also statements */
1132 cfg->parse_also = FALSE;
1133
1134 for (ca = cfg->ca_first; ca; ca = ca->next)
1135 {
1136 ca->visit = ++visit;
1137 load_also_cas(ca, ca->also, cfg);
1138
1139 if (ca->startup != STARTUP_NO)
1140 ca->state = STATE_TO_ADD;
1141 }
1142
1143 for (conn = cfg->conn_first; conn; conn = conn->next)
1144 {
1145 conn->visit = ++visit;
1146 load_also_conns(conn, conn->also, cfg);
1147
1148 if (conn->startup != STARTUP_NO)
1149 conn->state = STATE_TO_ADD;
1150 }
1151
1152 parser_free_conf(cfgp);
1153
1154 total_err = cfg->err + cfg->non_fatal_err;
1155 if (total_err > 0)
1156 {
1157 DBG1(DBG_APP, "### %d parsing error%s (%d fatal) ###",
1158 total_err, (total_err > 1)?"s":"", cfg->err);
1159 }
1160
1161 return cfg;
1162 }