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