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