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