cosmetics
[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 {
497 conn->eap = 23;
498 }
499 else if (streq(kw->value, "sim"))
500 {
501 conn->eap = 18;
502 }
503 else
504 {
505 conn->eap = atoi(kw->value);
506 if (conn->eap == 0)
507 {
508 plog("# unknown EAP type: %s=%s", kw->entry->name, kw->value);
509 cfg->err++;
510 }
511 }
512 break;
513 case KW_KEYINGTRIES:
514 if (streq(kw->value, "%forever"))
515 {
516 conn->sa_keying_tries = 0;
517 }
518 else
519 {
520 char *endptr;
521
522 conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
523 if (*endptr != '\0')
524 {
525 plog("# bad integer value: %s=%s", kw->entry->name, kw->value);
526 cfg->err++;
527 }
528 }
529 break;
530 case KW_REKEY:
531 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
532 break;
533 case KW_REAUTH:
534 KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
535 break;
536 case KW_MODECONFIG:
537 KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
538 break;
539 case KW_XAUTH:
540 KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
541 break;
542 default:
543 break;
544 }
545 }
546 handle_firewall("left", &conn->left, cfg);
547 handle_firewall("right", &conn->right, cfg);
548 }
549
550 /*
551 * initialize a conn object with the default conn
552 */
553 static void
554 conn_default(char *name, starter_conn_t *conn, starter_conn_t *def)
555 {
556 memcpy(conn, def, sizeof(starter_conn_t));
557 conn->name = clone_str(name, "conn name");
558
559 clone_args(KW_CONN_FIRST, KW_CONN_LAST, (char *)conn, (char *)def);
560 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left, (char *)&def->left);
561 clone_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right, (char *)&def->right);
562 }
563
564 /*
565 * parse a ca section
566 */
567 static void
568 load_ca(starter_ca_t *ca, kw_list_t *kw, starter_config_t *cfg)
569 {
570 char *ca_name = (ca->name == NULL)? "%default":ca->name;
571
572 for ( ; kw; kw = kw->next)
573 {
574 bool assigned = FALSE;
575
576 kw_token_t token = kw->entry->token;
577
578 if (token == KW_AUTO)
579 {
580 token = KW_CA_SETUP;
581 }
582 else if (token == KW_ALSO)
583 {
584 if (cfg->parse_also)
585 {
586 also_t *also = alloc_thing(also_t, "also_t");
587
588 also->name = clone_str(kw->value, "also");
589 also->next = ca->also;
590 ca->also = also;
591
592 DBG(DBG_CONTROL,
593 DBG_log(" also=%s", kw->value)
594 )
595 }
596 continue;
597 }
598
599 if (token < KW_CA_FIRST || token > KW_CA_LAST)
600 {
601 plog("# unsupported keyword '%s' in ca '%s'", kw->entry->name, ca_name);
602 cfg->err++;
603 continue;
604 }
605
606 if (!assign_arg(token, KW_CA_FIRST, kw, (char *)ca, &assigned))
607 {
608 plog(" bad argument value in ca '%s'", ca_name);
609 cfg->err++;
610 }
611 }
612
613 /* treat 'route' and 'start' as 'add' */
614 if (ca->startup != STARTUP_NO)
615 ca->startup = STARTUP_ADD;
616 }
617
618 /*
619 * initialize a ca object with the default ca
620 */
621 static void
622 ca_default(char *name, starter_ca_t *ca, starter_ca_t *def)
623 {
624 memcpy(ca, def, sizeof(starter_ca_t));
625 ca->name = clone_str(name, "ca name");
626
627 clone_args(KW_CA_FIRST, KW_CA_LAST, (char *)ca, (char *)def);
628 }
629
630 static kw_list_t*
631 find_also_conn(const char* name, starter_conn_t *conn, starter_config_t *cfg);
632
633 static void
634 load_also_conns(starter_conn_t *conn, also_t *also, starter_config_t *cfg)
635 {
636 while (also != NULL)
637 {
638 kw_list_t *kw = find_also_conn(also->name, conn, cfg);
639
640 if (kw == NULL)
641 {
642 plog(" conn '%s' cannot include '%s'", conn->name, also->name);
643 }
644 else
645 {
646 DBG(DBG_CONTROL,
647 DBG_log("conn '%s' includes '%s'", conn->name, also->name)
648 )
649 /* only load if no error occurred in the first round */
650 if (cfg->err == 0)
651 load_conn(conn, kw, cfg);
652 }
653 also = also->next;
654 }
655 }
656
657 /*
658 * find a conn included by also
659 */
660 static kw_list_t*
661 find_also_conn(const char* name, starter_conn_t *conn, starter_config_t *cfg)
662 {
663 starter_conn_t *c = cfg->conn_first;
664
665 while (c != NULL)
666 {
667 if (streq(name, c->name))
668 {
669 if (conn->visit == c->visit)
670 {
671 plog("# detected also loop");
672 cfg->err++;
673 return NULL;
674 }
675 c->visit = conn->visit;
676 load_also_conns(conn, c->also, cfg);
677 return c->kw;
678 }
679 c = c->next;
680 }
681
682 plog("# also '%s' not found", name);
683 cfg->err++;
684 return NULL;
685 }
686
687 static kw_list_t*
688 find_also_ca(const char* name, starter_ca_t *ca, starter_config_t *cfg);
689
690 static void
691 load_also_cas(starter_ca_t *ca, also_t *also, starter_config_t *cfg)
692 {
693 while (also != NULL)
694 {
695 kw_list_t *kw = find_also_ca(also->name, ca, cfg);
696
697 if (kw == NULL)
698 {
699 plog(" ca '%s' cannot include '%s'", ca->name, also->name);
700 }
701 else
702 {
703 DBG(DBG_CONTROL,
704 DBG_log("ca '%s' includes '%s'", ca->name, also->name)
705 )
706 /* only load if no error occurred in the first round */
707 if (cfg->err == 0)
708 load_ca(ca, kw, cfg);
709 }
710 also = also->next;
711 }
712 }
713
714 /*
715 * find a ca included by also
716 */
717 static kw_list_t*
718 find_also_ca(const char* name, starter_ca_t *ca, starter_config_t *cfg)
719 {
720 starter_ca_t *c = cfg->ca_first;
721
722 while (c != NULL)
723 {
724 if (streq(name, c->name))
725 {
726 if (ca->visit == c->visit)
727 {
728 plog("# detected also loop");
729 cfg->err++;
730 return NULL;
731 }
732 c->visit = ca->visit;
733 load_also_cas(ca, c->also, cfg);
734 return c->kw;
735 }
736 c = c->next;
737 }
738
739 plog("# also '%s' not found", name);
740 cfg->err++;
741 return NULL;
742 }
743
744 /*
745 * free the memory used by also_t objects
746 */
747 static void
748 free_also(also_t *head)
749 {
750 while (head != NULL)
751 {
752 also_t *also = head;
753
754 head = also->next;
755 pfree(also->name);
756 pfree(also);
757 }
758 }
759
760 /*
761 * free the memory used by a starter_conn_t object
762 */
763 static void
764 confread_free_conn(starter_conn_t *conn)
765 {
766 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->left);
767 free_args(KW_END_FIRST, KW_END_LAST, (char *)&conn->right);
768 free_args(KW_CONN_NAME, KW_CONN_LAST, (char *)conn);
769 free_also(conn->also);
770 }
771
772 /*
773 * free the memory used by a starter_ca_t object
774 */
775 static void
776 confread_free_ca(starter_ca_t *ca)
777 {
778 free_args(KW_CA_NAME, KW_CA_LAST, (char *)ca);
779 free_also(ca->also);
780 }
781
782 /*
783 * free the memory used by a starter_config_t object
784 */
785 void
786 confread_free(starter_config_t *cfg)
787 {
788 starter_conn_t *conn = cfg->conn_first;
789 starter_ca_t *ca = cfg->ca_first;
790
791 free_args(KW_SETUP_FIRST, KW_SETUP_LAST, (char *)cfg);
792
793 confread_free_conn(&cfg->conn_default);
794
795 while (conn != NULL)
796 {
797 starter_conn_t *conn_aux = conn;
798
799 conn = conn->next;
800 confread_free_conn(conn_aux);
801 pfree(conn_aux);
802 }
803
804 confread_free_ca(&cfg->ca_default);
805
806 while (ca != NULL)
807 {
808 starter_ca_t *ca_aux = ca;
809
810 ca = ca->next;
811 confread_free_ca(ca_aux);
812 pfree(ca_aux);
813 }
814
815 pfree(cfg);
816 }
817
818 /*
819 * load and parse an IPsec configuration file
820 */
821 starter_config_t *
822 confread_load(const char *file)
823 {
824 starter_config_t *cfg = NULL;
825 config_parsed_t *cfgp;
826 section_list_t *sconn, *sca;
827 starter_conn_t *conn;
828 starter_ca_t *ca;
829
830 u_int total_err;
831 u_int visit = 0;
832
833 /* load IPSec configuration file */
834 cfgp = parser_load_conf(file);
835 if (!cfgp)
836 return NULL;
837
838 cfg = (starter_config_t *)alloc_thing(starter_config_t, "starter_config_t");
839
840 /* set default values */
841 default_values(cfg);
842
843 /* determine default route */
844 get_defaultroute(&cfg->defaultroute);
845
846 /* load config setup section */
847 load_setup(cfg, cfgp);
848
849 /* in the first round parse also statements */
850 cfg->parse_also = TRUE;
851
852 /* find %default ca section */
853 for (sca = cfgp->ca_first; sca; sca = sca->next)
854 {
855 if (streq(sca->name, "%default"))
856 {
857 DBG(DBG_CONTROL,
858 DBG_log("Loading ca %%default")
859 )
860 load_ca(&cfg->ca_default, sca->kw, cfg);
861 }
862 }
863
864 /* parameters defined in ca %default sections can be overloads */
865 cfg->ca_default.seen = LEMPTY;
866
867 /* load other ca sections */
868 for (sca = cfgp->ca_first; sca; sca = sca->next)
869 {
870 u_int previous_err;
871
872 /* skip %default ca section */
873 if (streq(sca->name, "%default"))
874 continue;
875
876 DBG(DBG_CONTROL,
877 DBG_log("Loading ca '%s'", sca->name)
878 )
879 ca = (starter_ca_t *)alloc_thing(starter_ca_t, "starter_ca_t");
880
881 ca_default(sca->name, ca, &cfg->ca_default);
882 ca->kw = sca->kw;
883 ca->next = NULL;
884
885 previous_err = cfg->err;
886 load_ca(ca, ca->kw, cfg);
887 if (cfg->err > previous_err)
888 {
889 /* errors occurred - free the ca */
890 confread_free_ca(ca);
891 cfg->non_fatal_err += cfg->err - previous_err;
892 cfg->err = previous_err;
893 }
894 else
895 {
896 /* success - insert the ca into the chained list */
897 if (cfg->ca_last)
898 cfg->ca_last->next = ca;
899 cfg->ca_last = ca;
900 if (!cfg->ca_first)
901 cfg->ca_first = ca;
902 }
903 }
904
905 for (ca = cfg->ca_first; ca; ca = ca->next)
906 {
907 also_t *also = ca->also;
908
909 while (also != NULL)
910 {
911 kw_list_t *kw = find_also_ca(also->name, cfg->ca_first, cfg);
912
913 load_ca(ca, kw, cfg);
914 also = also->next;
915 }
916
917 if (ca->startup != STARTUP_NO)
918 ca->state = STATE_TO_ADD;
919 }
920
921 /* find %default conn sections */
922 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
923 {
924 if (streq(sconn->name, "%default"))
925 {
926 DBG(DBG_CONTROL,
927 DBG_log("Loading conn %%default")
928 )
929 load_conn(&cfg->conn_default, sconn->kw, cfg);
930 }
931 }
932
933 /* parameter defined in conn %default sections can be overloaded */
934 cfg->conn_default.seen = LEMPTY;
935 cfg->conn_default.right.seen = LEMPTY;
936 cfg->conn_default.left.seen = LEMPTY;
937
938 /* load other conn sections */
939 for (sconn = cfgp->conn_first; sconn; sconn = sconn->next)
940 {
941 u_int previous_err;
942
943 /* skip %default conn section */
944 if (streq(sconn->name, "%default"))
945 continue;
946
947 DBG(DBG_CONTROL,
948 DBG_log("Loading conn '%s'", sconn->name)
949 )
950 conn = (starter_conn_t *)alloc_thing(starter_conn_t, "starter_conn_t");
951
952 conn_default(sconn->name, conn, &cfg->conn_default);
953 conn->kw = sconn->kw;
954 conn->next = NULL;
955
956 previous_err = cfg->err;
957 load_conn(conn, conn->kw, cfg);
958 if (cfg->err > previous_err)
959 {
960 /* error occurred - free the conn */
961 confread_free_conn(conn);
962 cfg->non_fatal_err += cfg->err - previous_err;
963 cfg->err = previous_err;
964 }
965 else
966 {
967 /* success - insert the conn into the chained list */
968 if (cfg->conn_last)
969 cfg->conn_last->next = conn;
970 cfg->conn_last = conn;
971 if (!cfg->conn_first)
972 cfg->conn_first = conn;
973 }
974 }
975
976 /* in the second round do not parse also statements */
977 cfg->parse_also = FALSE;
978
979 for (ca = cfg->ca_first; ca; ca = ca->next)
980 {
981 ca->visit = ++visit;
982 load_also_cas(ca, ca->also, cfg);
983
984 if (ca->startup != STARTUP_NO)
985 ca->state = STATE_TO_ADD;
986 }
987
988 for (conn = cfg->conn_first; conn; conn = conn->next)
989 {
990 conn->visit = ++visit;
991 load_also_conns(conn, conn->also, cfg);
992
993 if (conn->startup != STARTUP_NO)
994 conn->state = STATE_TO_ADD;
995 }
996
997 parser_free_conf(cfgp);
998
999 total_err = cfg->err + cfg->non_fatal_err;
1000 if (total_err > 0)
1001 {
1002 plog("### %d parsing error%s (%d fatal) ###"
1003 , total_err, (total_err > 1)?"s":"", cfg->err);
1004 }
1005
1006 return cfg;
1007 }