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