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