]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/conffile.c
5f491eb7c8a5de6de270a86b5c70cef08d642383
[nfs-utils.git] / support / nfs / conffile.c
1 /*      $OpenBSD: conf.c,v 1.55 2003/06/03 14:28:16 ho Exp $    */
2 /*      $EOM: conf.c,v 1.48 2000/12/04 02:04:29 angelos Exp $   */
3
4 /*
5  * Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist.  All rights reserved.
6  * Copyright (c) 2000, 2001, 2002 HÃ¥kan Olsson.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /*
30  * This code was written under funding by Ericsson Radio Systems.
31  */
32
33 #include <sys/param.h>
34 #include <sys/mman.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <ctype.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <err.h>
47 #include <syslog.h>
48
49 #include "conffile.h"
50 #include "xlog.h"
51
52 static void conf_load_defaults (int);
53
54 struct conf_trans {
55         TAILQ_ENTRY (conf_trans) link;
56         int trans;
57         enum conf_op { CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION } op;
58         char *section;
59         char *tag;
60         char *value;
61         int override;
62         int is_default;
63 };
64
65 TAILQ_HEAD (conf_trans_head, conf_trans) conf_trans_queue;
66
67 /*
68  * Radix-64 Encoding.
69  */
70 static const u_int8_t bin2asc[]
71   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
72
73 static const u_int8_t asc2bin[] =
74 {
75   255, 255, 255, 255, 255, 255, 255, 255,
76   255, 255, 255, 255, 255, 255, 255, 255,
77   255, 255, 255, 255, 255, 255, 255, 255,
78   255, 255, 255, 255, 255, 255, 255, 255,
79   255, 255, 255, 255, 255, 255, 255, 255,
80   255, 255, 255,  62, 255, 255, 255,  63,
81    52,  53,  54,  55,  56,  57,  58,  59,
82    60,  61, 255, 255, 255, 255, 255, 255,
83   255,   0,   1,   2,   3,   4,   5,   6,
84     7,   8,   9,  10,  11,  12,  13,  14,
85    15,  16,  17,  18,  19,  20,  21,  22,
86    23,  24,  25, 255, 255, 255, 255, 255,
87   255,  26,  27,  28,  29,  30,  31,  32,
88    33,  34,  35,  36,  37,  38,  39,  40,
89    41,  42,  43,  44,  45,  46,  47,  48,
90    49,  50,  51, 255, 255, 255, 255, 255
91 };
92
93 struct conf_binding {
94   LIST_ENTRY (conf_binding) link;
95   char *section;
96   char *tag;
97   char *value;
98   int is_default;
99 };
100
101 char *conf_path;
102 LIST_HEAD (conf_bindings, conf_binding) conf_bindings[256];
103
104 static char *conf_addr;
105
106 static __inline__ u_int8_t
107 conf_hash(char *s)
108 {
109         u_int8_t hash = 0;
110
111         while (*s) {
112                 hash = ((hash << 1) | (hash >> 7)) ^ tolower (*s);
113                 s++;
114         }
115         return hash;
116 }
117
118 /*
119  * Insert a tag-value combination from LINE (the equal sign is at POS)
120  */
121 static int
122 conf_remove_now(char *section, char *tag)
123 {
124         struct conf_binding *cb, *next;
125
126         cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
127         for (; cb; cb = next) {
128                 next = LIST_NEXT(cb, link);
129                 if (strcasecmp(cb->section, section) == 0
130                                 && strcasecmp(cb->tag, tag) == 0) {
131                         LIST_REMOVE(cb, link);
132                         xlog(LOG_INFO,"[%s]:%s->%s removed", section, tag, cb->value);
133                         free(cb->section);
134                         free(cb->tag);
135                         free(cb->value);
136                         free(cb);
137                         return 0;
138                 }
139         }
140         return 1;
141 }
142
143 static int
144 conf_remove_section_now(char *section)
145 {
146   struct conf_binding *cb, *next;
147   int unseen = 1;
148
149         cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
150         for (; cb; cb = next) {
151                 next = LIST_NEXT(cb, link);
152                 if (strcasecmp(cb->section, section) == 0) {
153                         unseen = 0;
154                         LIST_REMOVE(cb, link);
155                         xlog(LOG_INFO, "[%s]:%s->%s removed", section, cb->tag, cb->value);
156                         free(cb->section);
157                         free(cb->tag);
158                         free(cb->value);
159                         free(cb);
160                         }
161                 }
162         return unseen;
163 }
164
165 /*
166  * Insert a tag-value combination from LINE (the equal sign is at POS)
167  * into SECTION of our configuration database.
168  */
169 static int
170 conf_set_now(char *section, char *tag, char *value, int override,
171               int is_default)
172 {
173         struct conf_binding *node = 0;
174
175         if (override)
176                 conf_remove_now(section, tag);
177         else if (conf_get_str(section, tag)) {
178                 if (!is_default) {
179                         xlog(LOG_INFO, "conf_set: duplicate tag [%s]:%s, ignoring...\n", 
180                                 section, tag);
181                 }
182                 return 1;
183         }
184
185         node = calloc(1, sizeof *node);
186         if (!node) {
187                 xlog_warn("conf_set: calloc (1, %lu) failed", (unsigned long)sizeof *node);
188                 return 1;
189         }
190         node->section = strdup(section);
191         node->tag = strdup(tag);
192         node->value = strdup(value);
193         node->is_default = is_default;
194
195         LIST_INSERT_HEAD(&conf_bindings[conf_hash (section)], node, link);
196         return 0;
197 }
198
199 /*
200  * Parse the line LINE of SZ bytes.  Skip Comments, recognize section
201  * headers and feed tag-value pairs into our configuration database.
202  */
203 static void
204 conf_parse_line(int trans, char *line, size_t sz)
205 {
206         char *val;
207         size_t i;
208         int j;
209         static char *section = 0;
210         static int ln = 0;
211
212         /* Lines starting with '#' or ';' are comments.  */
213         ln++;
214         if (*line == '#' || *line == ';')
215                 return;
216
217         /* Ignore blank lines */
218         if (*line == '\0')
219                 return;
220
221         /* Strip off any leading blanks */
222         while (isblank(*line)) 
223                 line++;
224
225         /* '[section]' parsing...  */
226         if (*line == '[') {
227                 line++;
228                 /* Strip off any blanks after '[' */
229                 while (isblank(*line)) 
230                         line++;
231
232                 for (i = 0; i < sz; i++) {
233                         if (line[i] == ']') {
234                                 break;
235                         }
236                 }
237                 if (section)
238                         free(section);
239                 if (i == sz) {
240                         xlog_warn("conf_parse_line: %d:"
241                                 "non-matched ']', ignoring until next section", ln);
242                         section = 0;
243                         return;
244                 }
245                 /* Strip off any blanks before ']' */
246                 val = line;
247                 while (*val && !isblank(*val)) 
248                         val++, j++;
249                 if (*val)
250                         i = j;
251
252                 section = malloc(i);
253                 if (!section) {
254                         xlog_warn("conf_parse_line: %d: malloc (%lu) failed", ln,
255                                                 (unsigned long)i);
256                         return;
257                 }
258                 strncpy(section, line, i);
259                 return;
260         }
261
262         /* Deal with assignments.  */
263         for (i = 0; i < sz; i++) {
264                 if (line[i] == '=') {
265                         /* If no section, we are ignoring the lines.  */
266                         if (!section) {
267                                 xlog_warn("conf_parse_line: %d: ignoring line due to no section", 
268                                         ln);
269                                 return;
270                         }
271                         line[strcspn (line, " \t=")] = '\0';
272                         val = line + i + 1 + strspn (line + i + 1, " \t");
273                         /* Skip trailing whitespace, if any */
274                         for (j = sz - (val - line) - 1; j > 0 && isspace(val[j]); j--)
275                                 val[j] = '\0';
276                         /* XXX Perhaps should we not ignore errors?  */
277                         conf_set(trans, section, line, val, 0, 0);
278                         return;
279                 }
280         }
281         /* Other non-empty lines are weird.  */
282         i = strspn(line, " \t");
283         if (line[i])
284                 xlog_warn("conf_parse_line: %d: syntax error", ln);
285
286         return;
287 }
288
289 /* Parse the mapped configuration file.  */
290 static void
291 conf_parse(int trans, char *buf, size_t sz)
292 {
293         char *cp = buf;
294         char *bufend = buf + sz;
295         char *line;
296
297         line = cp;
298         while (cp < bufend) {
299                 if (*cp == '\n') {
300                         /* Check for escaped newlines.  */
301                         if (cp > buf && *(cp - 1) == '\\')
302                                 *(cp - 1) = *cp = ' ';
303                         else {
304                                 *cp = '\0';
305                                 conf_parse_line(trans, line, cp - line);
306                                 line = cp + 1;
307                         }
308                 }
309                 cp++;
310         }
311         if (cp != line)
312                 xlog_warn("conf_parse: last line non-terminated, ignored.");
313 }
314
315 static void
316 conf_load_defaults(int tr)
317 {
318         /* No defaults */
319         return;
320 }
321
322 void
323 conf_init (void)
324 {
325         unsigned int i;
326
327         for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
328                 LIST_INIT (&conf_bindings[i]);
329
330         TAILQ_INIT (&conf_trans_queue);
331         conf_reinit();
332 }
333
334 /* Open the config file and map it into our address space, then parse it.  */
335 void
336 conf_reinit(void)
337 {
338         struct conf_binding *cb = 0;
339         int fd, trans;
340         unsigned int i;
341         size_t sz;
342         char *new_conf_addr = 0;
343         struct stat sb;
344
345         if ((stat (conf_path, &sb) == 0) || (errno != ENOENT)) {
346                 sz = sb.st_size;
347                 fd = open (conf_path, O_RDONLY, 0);
348                 if (fd == -1) {
349                         xlog_warn("conf_reinit: open (\"%s\", O_RDONLY) failed", conf_path);
350                         return;
351                 }
352
353                 new_conf_addr = malloc(sz);
354                 if (!new_conf_addr) {
355                         xlog_warn("conf_reinit: malloc (%lu) failed", (unsigned long)sz);
356                         goto fail;
357                 }
358
359                 /* XXX I assume short reads won't happen here.  */
360                 if (read (fd, new_conf_addr, sz) != (int)sz) {
361                         xlog_warn("conf_reinit: read (%d, %p, %lu) failed",
362                                 fd, new_conf_addr, (unsigned long)sz);
363                         goto fail;
364                 }
365                 close(fd);
366
367                 trans = conf_begin();
368                 /* XXX Should we not care about errors and rollback?  */
369                 conf_parse(trans, new_conf_addr, sz);
370         }
371         else
372                 trans = conf_begin();
373
374         /* Load default configuration values.  */
375         conf_load_defaults(trans);
376
377         /* Free potential existing configuration.  */
378         if (conf_addr) {
379                 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) {
380                         cb = LIST_FIRST (&conf_bindings[i]);
381                         for (; cb; cb = LIST_FIRST (&conf_bindings[i]))
382                                 conf_remove_now(cb->section, cb->tag);
383                 }
384                 free (conf_addr);
385         }
386
387         conf_end(trans, 1);
388         conf_addr = new_conf_addr;
389         return;
390
391 fail:
392         if (new_conf_addr)
393                 free(new_conf_addr);
394         close (fd);
395 }
396
397 /*
398  * Return the numeric value denoted by TAG in section SECTION or DEF
399  * if that tag does not exist.
400  */
401 int
402 conf_get_num(char *section, char *tag, int def)
403 {
404         char *value = conf_get_str(section, tag);
405
406         if (value)
407                 return atoi(value);
408
409         return def;
410 }
411
412 /* Validate X according to the range denoted by TAG in section SECTION.  */
413 int
414 conf_match_num(char *section, char *tag, int x)
415 {
416         char *value = conf_get_str (section, tag);
417         int val, min, max, n;
418
419         if (!value)
420                 return 0;
421         n = sscanf (value, "%d,%d:%d", &val, &min, &max);
422         switch (n) {
423         case 1:
424                 xlog(LOG_INFO, "conf_match_num: %s:%s %d==%d?", section, tag, val, x);
425                 return x == val;
426         case 3:
427                 xlog(LOG_INFO, "conf_match_num: %s:%s %d<=%d<=%d?", section, 
428                         tag, min, x, max);
429                 return min <= x && max >= x;
430         default:
431                 xlog(LOG_INFO, "conf_match_num: section %s tag %s: invalid number spec %s",
432                         section, tag, value);
433         }
434         return 0;
435 }
436
437 /* Return the string value denoted by TAG in section SECTION.  */
438 char *
439 conf_get_str(char *section, char *tag)
440 {
441         struct conf_binding *cb;
442
443         cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
444         for (; cb; cb = LIST_NEXT (cb, link)) {
445                 if (strcasecmp (section, cb->section) == 0
446                                 && strcasecmp (tag, cb->tag) == 0)
447                         return cb->value;
448         }
449         return 0;
450 }
451
452 /*
453  * Build a list of string values out of the comma separated value denoted by
454  * TAG in SECTION.
455  */
456 struct conf_list *
457 conf_get_list(char *section, char *tag)
458 {
459         char *liststr = 0, *p, *field, *t;
460         struct conf_list *list = 0;
461         struct conf_list_node *node;
462
463         list = malloc (sizeof *list);
464         if (!list)
465                 goto cleanup;
466         TAILQ_INIT (&list->fields);
467         list->cnt = 0;
468         liststr = conf_get_str(section, tag);
469         if (!liststr)
470                 goto cleanup;
471         liststr = strdup (liststr);
472         if (!liststr)
473                 goto cleanup;
474         p = liststr;
475         while ((field = strsep (&p, ",")) != NULL) {
476                 /* Skip leading whitespace */
477                 while (isspace (*field))
478                         field++;
479                 /* Skip trailing whitespace */
480                 if (p) {
481                         for (t = p - 1; t > field && isspace (*t); t--)
482                                 *t = '\0';
483                 }
484                 if (*field == '\0') {
485                         xlog(LOG_INFO, "conf_get_list: empty field, ignoring...");
486                         continue;
487                 }
488                 list->cnt++;
489                 node = calloc (1, sizeof *node);
490                 if (!node)
491                         goto cleanup;
492                 node->field = strdup (field);
493                 if (!node->field) {
494                         free(node);
495                         goto cleanup;
496                 }
497                 TAILQ_INSERT_TAIL (&list->fields, node, link);
498         }
499         free (liststr);
500         return list;
501
502 cleanup:
503         if (list)
504                 conf_free_list(list);
505         if (liststr)
506                 free(liststr);
507         return 0;
508 }
509
510 struct conf_list *
511 conf_get_tag_list(char *section)
512 {
513         struct conf_list *list = 0;
514         struct conf_list_node *node;
515         struct conf_binding *cb;
516
517         list = malloc(sizeof *list);
518         if (!list)
519                 goto cleanup;
520         TAILQ_INIT(&list->fields);
521         list->cnt = 0;
522         cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
523         for (; cb; cb = LIST_NEXT(cb, link)) {
524                 if (strcasecmp (section, cb->section) == 0) {
525                         list->cnt++;
526                         node = calloc(1, sizeof *node);
527                         if (!node)
528                                 goto cleanup;
529                         node->field = strdup(cb->tag);
530                         if (!node->field) {
531                                 free(node);
532                                 goto cleanup;
533                         }
534                         TAILQ_INSERT_TAIL(&list->fields, node, link);
535                 }
536         }
537         return list;
538
539 cleanup:
540         if (list)
541                 conf_free_list(list);
542         return 0;
543 }
544
545 /* Decode a PEM encoded buffer.  */
546 int
547 conf_decode_base64 (u_int8_t *out, u_int32_t *len, u_char *buf)
548 {
549         u_int32_t c = 0;
550         u_int8_t c1, c2, c3, c4;
551
552         while (*buf) {
553                 if (*buf > 127 || (c1 = asc2bin[*buf]) == 255)
554                         return 0;
555
556                 buf++;
557                 if (*buf > 127 || (c2 = asc2bin[*buf]) == 255)
558                         return 0;
559
560                 buf++;
561                 if (*buf == '=') {
562                         c3 = c4 = 0;
563                         c++;
564
565                         /* Check last four bit */
566                         if (c2 & 0xF)
567                                 return 0;
568
569                         if (strcmp((char *)buf, "==") == 0)
570                                 buf++;
571                         else
572                                 return 0;
573                 } else if (*buf > 127 || (c3 = asc2bin[*buf]) == 255)
574                         return 0;
575                 else {
576                         if (*++buf == '=') {
577                                 c4 = 0;
578                                 c += 2;
579
580                                 /* Check last two bit */
581                                 if (c3 & 3)
582                                         return 0;
583
584                         if (strcmp((char *)buf, "="))
585                                 return 0;
586                         } else if (*buf > 127 || (c4 = asc2bin[*buf]) == 255)
587                                 return 0;
588                         else
589                                 c += 3;
590                 }
591
592                 buf++;
593                 *out++ = (c1 << 2) | (c2 >> 4);
594                 *out++ = (c2 << 4) | (c3 >> 2);
595                 *out++ = (c3 << 6) | c4;
596         }
597
598         *len = c;
599         return 1;
600 }
601
602 void
603 conf_free_list(struct conf_list *list)
604 {
605         struct conf_list_node *node = TAILQ_FIRST(&list->fields);
606
607         while (node) {
608                 TAILQ_REMOVE(&list->fields, node, link);
609                 if (node->field)
610                         free(node->field);
611                 free (node);
612                 node = TAILQ_FIRST(&list->fields);
613         }
614         free (list);
615 }
616
617 int
618 conf_begin(void)
619 {
620   static int seq = 0;
621
622   return ++seq;
623 }
624
625 static struct conf_trans *
626 conf_trans_node(int transaction, enum conf_op op)
627 {
628         struct conf_trans *node;
629
630         node = calloc (1, sizeof *node);
631         if (!node) {
632                 xlog_warn("conf_trans_node: calloc (1, %lu) failed",
633                 (unsigned long)sizeof *node);
634                 return 0;
635         }
636         node->trans = transaction;
637         node->op = op;
638         TAILQ_INSERT_TAIL (&conf_trans_queue, node, link);
639         return node;
640 }
641
642 /* Queue a set operation.  */
643 int
644 conf_set(int transaction, char *section, char *tag, 
645         char *value, int override, int is_default)
646 {
647         struct conf_trans *node;
648
649         node = conf_trans_node(transaction, CONF_SET);
650         if (!node)
651                 return 1;
652         node->section = strdup(section);
653         if (!node->section) {
654                 xlog_warn("conf_set: strdup(\"%s\") failed", section);
655                 goto fail;
656         }
657         node->tag = strdup(tag);
658         if (!node->tag) {
659                 xlog_warn("conf_set: strdup(\"%s\") failed", tag);
660                 goto fail;
661         }
662         node->value = strdup(value);
663         if (!node->value) {
664                 xlog_warn("conf_set: strdup(\"%s\") failed", value);
665                 goto fail;
666         }
667         node->override = override;
668         node->is_default = is_default;
669         return 0;
670
671 fail:
672         if (node->tag)
673                 free(node->tag);
674         if (node->section)
675                 free(node->section);
676         if (node)
677                 free(node);
678         return 1;
679 }
680
681 /* Queue a remove operation.  */
682 int
683 conf_remove(int transaction, char *section, char *tag)
684 {
685         struct conf_trans *node;
686
687         node = conf_trans_node(transaction, CONF_REMOVE);
688         if (!node)
689                 goto fail;
690         node->section = strdup(section);
691         if (!node->section) {
692                 xlog_warn("conf_remove: strdup(\"%s\") failed", section);
693                 goto fail;
694         }
695         node->tag = strdup(tag);
696         if (!node->tag) {
697                 xlog_warn("conf_remove: strdup(\"%s\") failed", tag);
698                 goto fail;
699         }
700         return 0;
701
702 fail:
703         if (node && node->section)
704                 free (node->section);
705         if (node)
706                 free (node);
707         return 1;
708 }
709
710 /* Queue a remove section operation.  */
711 int
712 conf_remove_section(int transaction, char *section)
713 {
714         struct conf_trans *node;
715
716         node = conf_trans_node(transaction, CONF_REMOVE_SECTION);
717         if (!node)
718                 goto fail;
719         node->section = strdup(section);
720         if (!node->section) {
721                 xlog_warn("conf_remove_section: strdup(\"%s\") failed", section);
722                 goto fail;
723         }
724         return 0;
725
726 fail:
727         if (node)
728                 free(node);
729         return 1;
730 }
731
732 /* Execute all queued operations for this transaction.  Cleanup.  */
733 int
734 conf_end(int transaction, int commit)
735 {
736         struct conf_trans *node, *next;
737
738         for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
739                 next = TAILQ_NEXT(node, link);
740                 if (node->trans == transaction) {
741                         if (commit) {
742                                 switch (node->op) {
743                                 case CONF_SET:
744                                         conf_set_now(node->section, node->tag, node->value,
745                                         node->override, node->is_default);
746                                         break;
747                                 case CONF_REMOVE:
748                                         conf_remove_now(node->section, node->tag);
749                                         break;
750                                 case CONF_REMOVE_SECTION:
751                                         conf_remove_section_now(node->section);
752                                         break;
753                                 default:
754                                         xlog(LOG_INFO, "conf_end: unknown operation: %d", node->op);
755                                 }
756                         }
757                         TAILQ_REMOVE (&conf_trans_queue, node, link);
758                         if (node->section)
759                                 free(node->section);
760                         if (node->tag)
761                                 free(node->tag);
762                         if (node->value)
763                                 free(node->value);
764                         free (node);
765                 }
766         }
767         return 0;
768 }
769
770 /*
771  * Dump running configuration upon SIGUSR1.
772  * Configuration is "stored in reverse order", so reverse it again.
773  */
774 struct dumper {
775         char *s, *v;
776         struct dumper *next;
777 };
778
779 static void
780 conf_report_dump(struct dumper *node)
781 {
782         /* Recursive, cleanup when we're done.  */
783         if (node->next)
784                 conf_report_dump(node->next);
785
786         if (node->v)
787                 xlog(LOG_INFO, "%s=\t%s", node->s, node->v);
788         else if (node->s) {
789                 xlog(LOG_INFO, "%s", node->s);
790                 if (strlen(node->s) > 0)
791                         free(node->s);
792         }
793
794         free (node);
795 }
796
797 void
798 conf_report (void)
799 {
800         struct conf_binding *cb, *last = 0;
801         unsigned int i, len;
802         char *current_section = (char *)0;
803         struct dumper *dumper, *dnode;
804
805         dumper = dnode = (struct dumper *)calloc(1, sizeof *dumper);
806         if (!dumper)
807                 goto mem_fail;
808
809         xlog(LOG_INFO, "conf_report: dumping running configuration");
810
811         for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
812                 for (cb = LIST_FIRST(&conf_bindings[i]); cb; cb = LIST_NEXT(cb, link)) {
813                         if (!cb->is_default) {
814                                 /* Dump this entry.  */
815                                 if (!current_section || strcmp(cb->section, current_section)) {
816                                         if (current_section) {
817                                                 len = strlen (current_section) + 3;
818                                                 dnode->s = malloc(len);
819                                                 if (!dnode->s)
820                                                         goto mem_fail;
821
822                                                 snprintf(dnode->s, len, "[%s]", current_section);
823                                                 dnode->next = 
824                                                         (struct dumper *)calloc(1, sizeof (struct dumper));
825                                                 dnode = dnode->next;
826                                                 if (!dnode)
827                                                         goto mem_fail;
828
829                                                 dnode->s = "";
830                                                 dnode->next = 
831                                                         (struct dumper *)calloc(1, sizeof (struct dumper));
832                                                 dnode = dnode->next;
833                                                 if (!dnode)
834                                                 goto mem_fail;
835                                         }
836                                         current_section = cb->section;
837                                 }
838                                 dnode->s = cb->tag;
839                                 dnode->v = cb->value;
840                                 dnode->next = (struct dumper *)calloc (1, sizeof (struct dumper));
841                                 dnode = dnode->next;
842                                 if (!dnode)
843                                         goto mem_fail;
844                                 last = cb;
845                 }
846         }
847
848         if (last) {
849                 len = strlen(last->section) + 3;
850                 dnode->s = malloc(len);
851                 if (!dnode->s)
852                         goto mem_fail;
853                 snprintf(dnode->s, len, "[%s]", last->section);
854         }
855         conf_report_dump(dumper);
856         return;
857
858 mem_fail:
859         xlog_warn("conf_report: malloc/calloc failed");
860         while ((dnode = dumper) != 0) {
861                 dumper = dumper->next;
862                 if (dnode->s)
863                         free(dnode->s);
864                 free(dnode);
865         }
866         return;
867 }