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