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