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