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