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