da25f60b9bfb009221f707ef82de470271e77cc4
[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
185 {
186 bool fallback_to_any = FALSE;
187
188 if (value[0] == '%')
189 {
190 fallback_to_any = TRUE;
191 end->allow_any = TRUE;
192 value++;
193 }
194 conn->addr_family = ip_version(value);
195 ugh = ttoaddr(value, 0, conn->addr_family, &end->addr);
196 if (ugh != NULL)
197 {
198 plog("# bad addr: %s=%s [%s]", name, value, ugh);
199 if (fallback_to_any)
200 {
201 plog("# fallback to %s=%%any due to '%%' prefix");
202 anyaddr(conn->addr_family, &end->addr);
203 end->allow_any = FALSE;
204 cfg->non_fatal_err++;
205 }
206 else
207 {
208 goto err;
209 }
210 }
211 }
212 break;
213 case KW_NEXTHOP:
214 if (streq(value, "%defaultroute"))
215 {
216 if (cfg->defaultroute.defined)
217 end->nexthop = cfg->defaultroute.nexthop;
218 else
219 {
220 plog("# default route not known: %s=%s", name, value);
221 goto err;
222 }
223 }
224 else if (streq(value, "%direct"))
225 {
226 ugh = anyaddr(conn->addr_family, &end->nexthop);
227 }
228 else
229 {
230 conn->addr_family = ip_version(value);
231 ugh = ttoaddr(value, 0, conn->addr_family, &end->nexthop);
232 }
233 if (ugh != NULL)
234 {
235 plog("# bad addr: %s=%s [%s]", name, value, ugh);
236 goto err;
237 }
238 break;
239 case KW_SUBNET:
240 if ((strlen(value) >= 6 && strncmp(value,"vhost:",6) == 0)
241 || (strlen(value) >= 5 && strncmp(value,"vnet:",5) == 0))
242 {
243 end->virt = clone_str(value, "virt");
244 }
245 else
246 {
247 end->has_client = TRUE;
248 conn->tunnel_addr_family = ip_version(value);
249 ugh = ttosubnet(value, 0, conn->tunnel_addr_family, &end->subnet);
250 if (ugh != NULL)
251 {
252 plog("# bad subnet: %s=%s [%s]", name, value, ugh);
253 goto err;
254 }
255 }
256 break;
257 case KW_SUBNETWITHIN:
258 end->has_client = TRUE;
259 end->has_client_wildcard = TRUE;
260 conn->tunnel_addr_family = ip_version(value);
261 ugh = ttosubnet(value, 0, conn->tunnel_addr_family, &end->subnet);
262 break;
263 case KW_PROTOPORT:
264 ugh = ttoprotoport(value, 0, &end->protocol, &end->port, &has_port_wildcard);
265 end->has_port_wildcard = has_port_wildcard;
266 break;
267 case KW_SOURCEIP:
268 if (end->has_natip)
269 {
270 plog("# natip and sourceip cannot be defined at the same time");
271 goto err;
272 }
273 if (streq(value, "%modeconfig") || streq(value, "%modecfg") ||
274 streq(value, "%config") || streq(value, "%cfg"))
275 {
276 end->modecfg = TRUE;
277 }
278 else
279 {
280 conn->tunnel_addr_family = ip_version(value);
281 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &end->srcip);
282 if (ugh != NULL)
283 {
284 plog("# bad addr: %s=%s [%s]", name, value, ugh);
285 goto err;
286 }
287 end->has_srcip = TRUE;
288 }
289 conn->policy |= POLICY_TUNNEL;
290 break;
291 case KW_NATIP:
292 if (end->has_srcip)
293 {
294 plog("# natip and sourceip cannot be defined at the same time");
295 goto err;
296 }
297 if (streq(value, "%defaultroute"))
298 {
299 if (cfg->defaultroute.defined)
300 {
301 end->srcip = cfg->defaultroute.addr;
302 }
303 else
304 {
305 plog("# default route not known: %s=%s", name, value);
306 goto err;
307 }
308 }
309 else
310 {
311 conn->tunnel_addr_family = ip_version(value);
312 ugh = ttoaddr(value, 0, conn->tunnel_addr_family, &end->srcip);
313 if (ugh != NULL)
314 {
315 plog("# bad addr: %s=%s [%s]", name, value, ugh);
316 goto err;
317 }
318 }
319 end->has_natip = TRUE;
320 conn->policy |= POLICY_TUNNEL;
321 break;
322 default:
323 break;
324 }
325 return;
326
327 err:
328 plog(" bad argument value in conn '%s'", conn_name);
329 cfg->err++;
330 }
331
332 /*
333 * handles left|rightfirewall and left|rightupdown parameters
334 */
335 static void
336 handle_firewall( const char *label, starter_end_t *end, starter_config_t *cfg)
337 {
338 if (end->firewall && (end->seen & LELEM(KW_FIREWALL - KW_END_FIRST)))
339 {
340 if (end->updown != NULL)
341 {
342 plog("# cannot have both %sfirewall and %supdown", label, label);
343 cfg->err++;
344 }
345 else
346 {
347 end->updown = clone_str(firewall_defaults, "firewall_defaults");
348 end->firewall = FALSE;
349 }
350 }
351 }
352
353 /*
354 * parse a conn section
355 */
356 static void
357 load_conn(starter_conn_t *conn, kw_list_t *kw, starter_config_t *cfg)
358 {
359 char *conn_name = (conn->name == NULL)? "%default":conn->name;
360
361 for ( ; kw; kw = kw->next)
362 {
363 bool assigned = FALSE;
364
365 kw_token_t token = kw->entry->token;
366
367 if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
368 {
369 kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST
370 , kw, conn_name, cfg);
371 continue;
372 }
373 else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
374 {
375 kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST
376 , kw, conn_name, cfg);
377 continue;
378 }
379
380 if (token == KW_AUTO)
381 {
382 token = KW_CONN_SETUP;
383 }
384 else if (token == KW_ALSO)
385 {
386 if (cfg->parse_also)
387 {
388 also_t *also = alloc_thing(also_t, "also_t");
389
390 also->name = clone_str(kw->value, "also");
391 also->next = conn->also;
392 conn->also = also;
393
394 DBG(DBG_CONTROL,
395 DBG_log(" also=%s", kw->value)
396 )
397 }
398 continue;
399 }
400
401 if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
402 {
403 plog("# unsupported keyword '%s' in conn '%s'"
404 , kw->entry->name, conn_name);
405 cfg->err++;
406 continue;
407 }
408
409 if (!assign_arg(token, KW_CONN_FIRST, kw, (char *)conn, &assigned))
410 {
411 plog(" bad argument value in conn '%s'", conn_name);
412 cfg->err++;
413 continue;
414 }
415
416 if (assigned)
417 continue;
418
419 switch (token)
420 {
421 case KW_TYPE:
422 conn->policy &= ~(POLICY_TUNNEL | POLICY_SHUNT_MASK);
423 if (streq(kw->value, "tunnel"))
424 conn->policy |= POLICY_TUNNEL;
425 else if (streq(kw->value, "beet"))
426 conn->policy |= POLICY_BEET;
427 else if (streq(kw->value, "passthrough") || streq(kw->value, "pass"))
428 conn->policy |= POLICY_SHUNT_PASS;
429 else if (streq(kw->value, "drop"))
430 conn->policy |= POLICY_SHUNT_DROP;
431 else if (streq(kw->value, "reject"))
432 conn->policy |= POLICY_SHUNT_REJECT;
433 else if (strcmp(kw->value, "transport") != 0)
434 {
435 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
436 cfg->err++;
437 }
438 break;
439 case KW_PFS:
440 KW_POLICY_FLAG("yes", "no", POLICY_PFS)
441 break;
442 case KW_COMPRESS:
443 KW_POLICY_FLAG("yes", "no", POLICY_COMPRESS)
444 break;
445 case KW_AUTH:
446 KW_POLICY_FLAG("ah", "esp", POLICY_AUTHENTICATE)
447 break;
448 case KW_AUTHBY:
449 conn->policy &= ~(POLICY_ID_AUTH_MASK | POLICY_ENCRYPT);
450
451 if (!(streq(kw->value, "never") || streq(kw->value, "eap")))
452 {
453 char *value = kw->value;
454 char *second = strchr(kw->value, '|');
455
456 if (second != NULL)
457 *second = '\0';
458
459 /* also handles the cases secret|rsasig and rsasig|secret */
460 for (;;)
461 {
462 if (streq(value, "rsasig"))
463 conn->policy |= POLICY_RSASIG | POLICY_ENCRYPT;
464 else if (streq(value, "secret") || streq(value, "psk"))
465 conn->policy |= POLICY_PSK | POLICY_ENCRYPT;
466 else if (streq(value, "xauthrsasig"))
467 conn->policy |= POLICY_XAUTH_RSASIG | POLICY_ENCRYPT;
468 else if (streq(value, "xauthpsk"))
469 conn->policy |= POLICY_XAUTH_PSK | POLICY_ENCRYPT;
470 else
471 {
472 plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
473 cfg->err++;
474 break;
475 }
476 if (second == NULL)
477 break;
478 value = second;
479 second = NULL; /* traverse the loop no more than twice */
480 }
481 }
482 break;
483 case KW_EAP:
484 /* TODO: a gperf function for all EAP types */
485 if (streq(kw->value, "aka"))
486 conn->eap = 23;
487 else if (streq(kw->value, "sim"))
488 {
489 conn->eap = 18;
490
491 }
492 else
493 {
494 conn->eap = atoi(kw->value);
495 if (conn->eap == 0)
496 {
497 plog("# unknown EAP type: %s=%s", kw->entry->name, kw->value);
498 cfg->err++;
499 }
500 }
501 break;
502 case KW_REKEY:
503 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
504 break;
505 case KW_REAUTH:
506 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
507 break;
508 case KW_MODECONFIG:
509 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
510 break;
511 case KW_XAUTH:
512 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
513 break;
514 default:
515 break;
516 }
517 }
518 handle_firewall("left", &conn->left, cfg);
519 handle_firewall("right", &conn->right, cfg);
520 }
521
522 /*
523 * initialize a conn object with the default conn
524 */
525 static void
526 conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
527 {
528 memcpy(conn, def, sizeof(starter_conn_t));
529 conn->name = clone_str(name, "conn name");
530
531 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
532 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
533 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
534 }
535
536 /*
537 * parse a ca section
538 */
539 static void
540 load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
541 {
542 char *ca_name = (ca->name == NULL)? "%default":ca->name;
543
544 for ( ; kw; kw = kw->next)
545 {
546 bool assigned = FALSE;
547
548 kw_token_t token = kw->entry->token;
549
550 if (token == KW_AUTO)
551 {
552 token = KW_CA_SETUP;
553 }
554 else if (token == KW_ALSO)
555 {
556 if (cfg->parse_also)
557 {
558 also_t *also = alloc_thing(also_t, "also_t");
559
560 also->name = clone_str(kw->value, "also");
561 also->next = ca->also;
562 ca->also = also;
563
564 DBG(DBG_CONTROL,
565 DBG_log(" also=%s", kw->value)
566 )
567 }
568 continue;
569 }
570
571 if (token < KW_CA_FIRST || token > KW_CA_LAST)
572 {
573 plog("# unsupported keyword '%s' in ca '%s'", kw->entry->name, ca_name);
574 cfg->err++;
575 continue;
576 }
577
578 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
579 {
580 plog(" bad argument value in ca '%s'", ca_name);
581 cfg->err++;
582 }
583 }
584
585 /* treat 'route' and 'start' as 'add' */
586 if (ca->startup != STARTUP_NO)
587 ca->startup = STARTUP_ADD;
588 }
589
590 /*
591 * initialize a ca object with the default ca
592 */
593 static void
594 ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
595 {
596 memcpy(ca, def, sizeof(starter_ca_t));
597 ca->name = clone_str(name, "ca name");
598
599 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
600 }
601
602 static kw_list_t*
603 find_also_conn(const char* name, starter_conn_t *conn, starter_config_t *cfg);
604
605 static void
606 load_also_conns(starter_conn_t *conn, also_t *also, starter_config_t *cfg)
607 {
608 while (also != NULL)
609 {
610 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
611
612 if (kw == NULL)
613 {
614 plog(" conn '%s' cannot include '%s'", conn->name, also->name);
615 }
616 else
617 {
618 DBG(DBG_CONTROL,
619 DBG_log("conn '%s' includes '%s'", conn->name, also->name)
620 )
621 /* only load if no error occurred in the first round */
622 if (cfg->err == 0)
623 load_conn(conn, kw, cfg);
624 }
625 also = also->next;
626 }
627 }
628
629 /*
630 * find a conn included by also
631 */
632 static kw_list_t*
633 find_also_conn(const char* name, starter_conn_t *conn, starter_config_t *cfg)
634 {
635 starter_conn_t *c = cfg->conn_first;
636
637 while (c != NULL)
638 {
639 if (streq(name, c->name))
640 {
641 if (conn->visit == c->visit)
642 {
643 plog("# detected also loop");
644 cfg->err++;
645 return NULL;
646 }
647 c->visit = conn->visit;
648 load_also_conns(conn, c->also, cfg);
649 return c->kw;
650 }
651 c = c->next;
652 }
653
654 plog("# also '%s' not found", name);
655 cfg->err++;
656 return NULL;
657 }
658
659 static kw_list_t*
660 find_also_ca(const char* name, starter_ca_t *ca, starter_config_t *cfg);
661
662 static void
663 load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
664 {
665 while (also != NULL)
666 {
667 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
668
669 if (kw == NULL)
670 {
671 plog(" ca '%s' cannot include '%s'", ca->name, also->name);
672 }
673 else
674 {
675 DBG(DBG_CONTROL,
676 DBG_log("ca '%s' includes '%s'", ca->name, also->name)
677 )
678 /* only load if no error occurred in the first round */
679 if (cfg->err == 0)
680 load_ca(ca, kw, cfg);
681 }
682 also = also->next;
683 }
684 }
685
686 /*
687 * find a ca included by also
688 */
689 static kw_list_t*
690 find_also_ca(const char* name, starter_ca_t *ca, starter_config_t *cfg)
691 {
692 starter_ca_t *c = cfg->ca_first;
693
694 while (c != NULL)
695 {
696 if (streq(name, c->name))
697 {
698 if (ca->visit == c->visit)
699 {
700 plog("# detected also loop");
701 cfg->err++;
702 return NULL;
703 }
704 c->visit = ca->visit;
705 load_also_cas(ca, c->also, cfg);
706 return c->kw;
707 }
708 c = c->next;
709 }
710
711 plog("# also '%s' not found", name);
712 cfg->err++;
713 return NULL;
714 }
715
716 /*
717 * free the memory used by also_t objects
718 */
719 static void
720 free_also(also_t *head)
721 {
722 while (head != NULL)
723 {
724 also_t *also = head;
725
726 head = also->next;
727 pfree(also->name);
728 pfree(also);
729 }
730 }
731
732 /*
733 * free the memory used by a starter_conn_t object
734 */
735 static void
736 confread_free_conn(starter_conn_t *conn)
737 {
738 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
739 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
740 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
741 free_also(conn->also);
742 }
743
744 /*
745 * free the memory used by a starter_ca_t object
746 */
747 static void
748 confread_free_ca(starter_ca_t *ca)
749 {
750 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
751 free_also(ca->also);
752 }
753
754 /*
755 * free the memory used by a starter_config_t object
756 */
757 void
758 confread_free(starter_config_t *cfg)
759 {
760 starter_conn_t *conn = cfg->conn_first;
761 starter_ca_t *ca = cfg->ca_first;
762
763 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
764
765 confread_free_conn(&cfg->conn_default);
766
767 while (conn != NULL)
768 {
769 starter_conn_t *conn_aux = conn;
770
771 conn = conn->next;
772 confread_free_conn(conn_aux);
773 pfree(conn_aux);
774 }
775
776 confread_free_ca(&cfg->ca_default);
777
778 while (ca != NULL)
779 {
780 starter_ca_t *ca_aux = ca;
781
782 ca = ca->next;
783 confread_free_ca(ca_aux);
784 pfree(ca_aux);
785 }
786
787 pfree(cfg);
788 }
789
790 /*
791 * load and parse an IPsec configuration file
792 */
793 starter_config_t *
794 confread_load(const char *file)
795 {
796 starter_config_t *cfg = NULL;
797 config_parsed_t *cfgp;
798 section_list_t *sconn, *sca;
799 starter_conn_t *conn;
800 starter_ca_t *ca;
801
802 u_int total_err;
803 u_int visit = 0;
804
805 /* load IPSec configuration file */
806 cfgp = parser_load_conf(file);
807 if (!cfgp)
808 return NULL;
809
810 cfg = (starter_config_t *)alloc_thing(starter_config_t, "starter_config_t");
811
812 /* set default values */
813 default_values(cfg);
814
815 /* determine default route */
816 get_defaultroute(&cfg->defaultroute);
817
818 /* load config setup section */
819 load_setup(cfg, cfgp);
820
821 /* in the first round parse also statements */
822 cfg->parse_also = TRUE;
823
824 /* find %default ca section */
825 for (sca = cfgp->ca_first; sca; sca = sca->next)
826 {
827 if (streq(sca->name, "%default"))
828 {
829 DBG(DBG_CONTROL,
830 DBG_log("Loading ca %%default")
831 )
832 load_ca(&cfg->ca_default, sca->kw, cfg);
833 }
834 }
835
836 /* parameters defined in ca %default sections can be overloads */
837 cfg->ca_default.seen = LEMPTY;
838
839 /* load other ca sections */
840 for (sca = cfgp->ca_first; sca; sca = sca->next)
841 {
842 u_int previous_err;
843
844 /* skip %default ca section */
845 if (streq(sca->name, "%default"))
846 continue;
847
848 DBG(DBG_CONTROL,
849 DBG_log("Loading ca '%s'", sca->name)
850 )
851 ca = (starter_ca_t *)alloc_thing(starter_ca_t, "starter_ca_t");
852
853 ca_default(sca->name, ca, &cfg->ca_default);
854 ca->kw = sca->kw;
855 ca->next = NULL;
856
857 previous_err = cfg->err;
858 load_ca(ca, ca->kw, cfg);
859 if (cfg->err > previous_err)
860 {
861 /* errors occurred - free the ca */
862 confread_free_ca(ca);
863 cfg->non_fatal_err += cfg->err - previous_err;
864 cfg->err = previous_err;
865 }
866 else
867 {
868 /* success - insert the ca into the chained list */
869 if (cfg->ca_last)
870 cfg->ca_last->next = ca;
871 cfg->ca_last = ca;
872 if (!cfg->ca_first)
873 cfg->ca_first = ca;
874 }
875 }
876
877 for (ca = cfg->ca_first; ca; ca = ca->next)
878 {
879 also_t *also = ca->also;
880
881 while (also != NULL)
882 {
883 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
884
885 load_ca(ca, kw, cfg);
886 also = also->next;
887 }
888
889 if (ca->startup != STARTUP_NO)
890 ca->state = STATE_TO_ADD;
891 }
892
893 /* find %default conn sections */
894 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
895 {
896 if (streq(sconn->name, "%default"))
897 {
898 DBG(DBG_CONTROL,
899 DBG_log("Loading conn %%default")
900 )
901 load_conn(&cfg->conn_default, sconn->kw, cfg);
902 }
903 }
904
905 /* parameter defined in conn %default sections can be overloaded */
906 cfg->conn_default.seen = LEMPTY;
907 cfg->conn_default.right.seen = LEMPTY;
908 cfg->conn_default.left.seen = LEMPTY;
909
910 /* load other conn sections */
911 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
912 {
913 u_int previous_err;
914
915 /* skip %default conn section */
916 if (streq(sconn->name, "%default"))
917 continue;
918
919 DBG(DBG_CONTROL,
920 DBG_log("Loading conn '%s'", sconn->name)
921 )
922 conn = (starter_conn_t *)alloc_thing(starter_conn_t, "starter_conn_t");
923
924 conn_default(sconn->name, conn, &cfg->conn_default);
925 conn->kw = sconn->kw;
926 conn->next = NULL;
927
928 previous_err = cfg->err;
929 load_conn(conn, conn->kw, cfg);
930 if (cfg->err > previous_err)
931 {
932 /* error occurred - free the conn */
933 confread_free_conn(conn);
934 cfg->non_fatal_err += cfg->err - previous_err;
935 cfg->err = previous_err;
936 }
937 else
938 {
939 /* success - insert the conn into the chained list */
940 if (cfg->conn_last)
941 cfg->conn_last->next = conn;
942 cfg->conn_last = conn;
943 if (!cfg->conn_first)
944 cfg->conn_first = conn;
945 }
946 }
947
948 /* in the second round do not parse also statements */
949 cfg->parse_also = FALSE;
950
951 for (ca = cfg->ca_first; ca; ca = ca->next)
952 {
953 ca->visit = ++visit;
954 load_also_cas(ca, ca->also, cfg);
955
956 if (ca->startup != STARTUP_NO)
957 ca->state = STATE_TO_ADD;
958 }
959
960 for (conn = cfg->conn_first; conn; conn = conn->next)
961 {
962 conn->visit = ++visit;
963 load_also_conns(conn, conn->also, cfg);
964
965 if (conn->startup != STARTUP_NO)
966 conn->state = STATE_TO_ADD;
967 }
968
969 parser_free_conf(cfgp);
970
971 total_err = cfg->err + cfg->non_fatal_err;
972 if (total_err > 0)
973 {
974 plog("### %d parsing error%s (%d fatal) ###"
975 , total_err, (total_err > 1)?"s":"", cfg->err);
976 }
977
978 return cfg;
979 }