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