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