]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/conffile.c
97dc88a06df6762547336959238882ae2f809bc2
[nfs-utils.git] / support / nfs / conffile.c
1 /*      $OpenBSD: conf.c,v 1.55 2003/06/03 14:28:16 ho Exp $    */
2 /*      $EOM: conf.c,v 1.48 2000/12/04 02:04:29 angelos Exp $   */
3
4 /*
5  * Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist.  All rights reserved.
6  * Copyright (c) 2000, 2001, 2002 HÃ¥kan Olsson.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /*
30  * This code was written under funding by Ericsson Radio Systems.
31  */
32
33 #include <sys/param.h>
34 #include <sys/mman.h>
35 #include <sys/socket.h>
36 #include <sys/stat.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <ctype.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <err.h>
47 #include <syslog.h>
48
49 #include "conffile.h"
50 #include "xlog.h"
51
52 static void conf_load_defaults (int);
53 static int conf_set(int , char *, char *, char *, 
54         char *, int , int );
55
56 struct conf_trans {
57         TAILQ_ENTRY (conf_trans) link;
58         int trans;
59         enum conf_op { CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION } op;
60         char *section;
61         char *arg;
62         char *tag;
63         char *value;
64         int override;
65         int is_default;
66 };
67
68 TAILQ_HEAD (conf_trans_head, conf_trans) conf_trans_queue;
69
70 /*
71  * Radix-64 Encoding.
72  */
73 static const u_int8_t bin2asc[]
74   = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
75
76 static const u_int8_t asc2bin[] =
77 {
78   255, 255, 255, 255, 255, 255, 255, 255,
79   255, 255, 255, 255, 255, 255, 255, 255,
80   255, 255, 255, 255, 255, 255, 255, 255,
81   255, 255, 255, 255, 255, 255, 255, 255,
82   255, 255, 255, 255, 255, 255, 255, 255,
83   255, 255, 255,  62, 255, 255, 255,  63,
84    52,  53,  54,  55,  56,  57,  58,  59,
85    60,  61, 255, 255, 255, 255, 255, 255,
86   255,   0,   1,   2,   3,   4,   5,   6,
87     7,   8,   9,  10,  11,  12,  13,  14,
88    15,  16,  17,  18,  19,  20,  21,  22,
89    23,  24,  25, 255, 255, 255, 255, 255,
90   255,  26,  27,  28,  29,  30,  31,  32,
91    33,  34,  35,  36,  37,  38,  39,  40,
92    41,  42,  43,  44,  45,  46,  47,  48,
93    49,  50,  51, 255, 255, 255, 255, 255
94 };
95
96 struct conf_binding {
97   LIST_ENTRY (conf_binding) link;
98   char *section;
99   char *arg;
100   char *tag;
101   char *value;
102   int is_default;
103 };
104
105 char *conf_path;
106 LIST_HEAD (conf_bindings, conf_binding) conf_bindings[256];
107
108 static char *conf_addr;
109
110 static __inline__ u_int8_t
111 conf_hash(char *s)
112 {
113         u_int8_t hash = 0;
114
115         while (*s) {
116                 hash = ((hash << 1) | (hash >> 7)) ^ tolower (*s);
117                 s++;
118         }
119         return hash;
120 }
121
122 /*
123  * Convert letter from upper case to lower case
124  */
125 static inline void upper2lower(char *str)
126 {
127         char *ptr = str;
128
129         while (*ptr) 
130                 *ptr++ = tolower(*ptr);
131 }
132
133
134 /*
135  * Insert a tag-value combination from LINE (the equal sign is at POS)
136  */
137 static int
138 conf_remove_now(char *section, char *tag)
139 {
140         struct conf_binding *cb, *next;
141
142         cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
143         for (; cb; cb = next) {
144                 next = LIST_NEXT(cb, link);
145                 if (strcasecmp(cb->section, section) == 0
146                                 && strcasecmp(cb->tag, tag) == 0) {
147                         LIST_REMOVE(cb, link);
148                         xlog(LOG_INFO,"[%s]:%s->%s removed", section, tag, cb->value);
149                         free(cb->section);
150                         free(cb->arg);
151                         free(cb->tag);
152                         free(cb->value);
153                         free(cb);
154                         return 0;
155                 }
156         }
157         return 1;
158 }
159
160 static int
161 conf_remove_section_now(char *section)
162 {
163   struct conf_binding *cb, *next;
164   int unseen = 1;
165
166         cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
167         for (; cb; cb = next) {
168                 next = LIST_NEXT(cb, link);
169                 if (strcasecmp(cb->section, section) == 0) {
170                         unseen = 0;
171                         LIST_REMOVE(cb, link);
172                         xlog(LOG_INFO, "[%s]:%s->%s removed", section, cb->tag, cb->value);
173                         free(cb->section);
174                         free(cb->arg);
175                         free(cb->tag);
176                         free(cb->value);
177                         free(cb);
178                         }
179                 }
180         return unseen;
181 }
182
183 /*
184  * Insert a tag-value combination from LINE (the equal sign is at POS)
185  * into SECTION of our configuration database.
186  */
187 static int
188 conf_set_now(char *section, char *arg, char *tag, 
189         char *value, int override, int is_default)
190 {
191         struct conf_binding *node = 0;
192
193         if (override)
194                 conf_remove_now(section, tag);
195         else if (conf_get_section(section, arg, tag)) {
196                 if (!is_default) {
197                         xlog(LOG_INFO, "conf_set: duplicate tag [%s]:%s, ignoring...\n", 
198                                 section, tag);
199                 }
200                 return 1;
201         }
202         node = calloc(1, sizeof *node);
203         if (!node) {
204                 xlog_warn("conf_set: calloc (1, %lu) failed", (unsigned long)sizeof *node);
205                 return 1;
206         }
207         node->section = strdup(section);
208         if (arg)
209                 node->arg = strdup(arg);
210         node->tag = strdup(tag);
211         node->value = strdup(value);
212         node->is_default = is_default;
213
214         LIST_INSERT_HEAD(&conf_bindings[conf_hash (section)], node, link);
215         return 0;
216 }
217
218 /*
219  * Parse the line LINE of SZ bytes.  Skip Comments, recognize section
220  * headers and feed tag-value pairs into our configuration database.
221  */
222 static void
223 conf_parse_line(int trans, char *line, size_t sz)
224 {
225         char *val, *ptr;
226         size_t i;
227         int j;
228         static char *section = 0;
229         static char *arg = 0;
230         static int ln = 0;
231
232         /* Lines starting with '#' or ';' are comments.  */
233         ln++;
234         if (*line == '#' || *line == ';')
235                 return;
236
237         /* Ignore blank lines */
238         if (*line == '\0')
239                 return;
240
241         /* Strip off any leading blanks */
242         while (isblank(*line)) 
243                 line++;
244
245         /* '[section]' parsing...  */
246         if (*line == '[') {
247                 line++;
248                 /* Strip off any blanks after '[' */
249                 while (isblank(*line)) 
250                         line++;
251                 for (i = 0; i < sz; i++) {
252                         if (line[i] == ']') {
253                                 break;
254                         }
255                 }
256                 if (section)
257                         free(section);
258                 if (i == sz) {
259                         xlog_warn("conf_parse_line: %d:"
260                                 "non-matched ']', ignoring until next section", ln);
261                         section = 0;
262                         return;
263                 }
264                 /* Strip off any blanks before ']' */
265                 val = line;
266                 while (*val && !isblank(*val)) 
267                         val++, j++;
268                 if (*val)
269                         i = j;
270                 section = malloc(i);
271                 if (!section) {
272                         xlog_warn("conf_parse_line: %d: malloc (%lu) failed", ln,
273                                                 (unsigned long)i);
274                         return;
275                 }
276                 strncpy(section, line, i);
277
278                 if (arg) 
279                         free(arg);
280                 arg = 0;
281
282                 ptr = strchr(val, '"');
283                 if (ptr == NULL)
284                         return;
285                 line = ++ptr;
286                 while (*ptr && *ptr != '"')
287                         ptr++;
288                 if (*ptr == '\0') {
289                         xlog_warn("conf_parse_line: line %d:"
290                                 "non-matched '\"', ignoring until next section", ln);
291                 }  else {
292                         *ptr = '\0';
293                         arg = strdup(line);
294                         if (!arg) 
295                                 xlog_warn("conf_parse_line: %d: malloc arg failed", ln);
296                 }
297                 return;
298         }
299
300         /* Deal with assignments.  */
301         for (i = 0; i < sz; i++) {
302                 if (line[i] == '=') {
303                         /* If no section, we are ignoring the lines.  */
304                         if (!section) {
305                                 xlog_warn("conf_parse_line: %d: ignoring line due to no section", 
306                                         ln);
307                                 return;
308                         }
309                         line[strcspn (line, " \t=")] = '\0';
310                         val = line + i + 1 + strspn (line + i + 1, " \t");
311                         /* Skip trailing whitespace, if any */
312                         for (j = sz - (val - line) - 1; j > 0 && isspace(val[j]); j--)
313                                 val[j] = '\0';
314                         /* XXX Perhaps should we not ignore errors?  */
315                         conf_set(trans, section, arg, line, val, 0, 0);
316                         return;
317                 }
318         }
319         /* Other non-empty lines are weird.  */
320         i = strspn(line, " \t");
321         if (line[i])
322                 xlog_warn("conf_parse_line: %d: syntax error", ln);
323
324         return;
325 }
326
327 /* Parse the mapped configuration file.  */
328 static void
329 conf_parse(int trans, char *buf, size_t sz)
330 {
331         char *cp = buf;
332         char *bufend = buf + sz;
333         char *line;
334
335         line = cp;
336         while (cp < bufend) {
337                 if (*cp == '\n') {
338                         /* Check for escaped newlines.  */
339                         if (cp > buf && *(cp - 1) == '\\')
340                                 *(cp - 1) = *cp = ' ';
341                         else {
342                                 *cp = '\0';
343                                 conf_parse_line(trans, line, cp - line);
344                                 line = cp + 1;
345                         }
346                 }
347                 cp++;
348         }
349         if (cp != line)
350                 xlog_warn("conf_parse: last line non-terminated, ignored.");
351 }
352
353 static void
354 conf_load_defaults(int tr)
355 {
356         /* No defaults */
357         return;
358 }
359
360 void
361 conf_init (void)
362 {
363         unsigned int i;
364
365         for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
366                 LIST_INIT (&conf_bindings[i]);
367
368         TAILQ_INIT (&conf_trans_queue);
369         conf_reinit();
370 }
371
372 /* Open the config file and map it into our address space, then parse it.  */
373 void
374 conf_reinit(void)
375 {
376         struct conf_binding *cb = 0;
377         int fd, trans;
378         unsigned int i;
379         size_t sz;
380         char *new_conf_addr = 0;
381         struct stat sb;
382
383         if ((stat (conf_path, &sb) == 0) || (errno != ENOENT)) {
384                 sz = sb.st_size;
385                 fd = open (conf_path, O_RDONLY, 0);
386                 if (fd == -1) {
387                         xlog_warn("conf_reinit: open (\"%s\", O_RDONLY) failed", conf_path);
388                         return;
389                 }
390
391                 new_conf_addr = malloc(sz);
392                 if (!new_conf_addr) {
393                         xlog_warn("conf_reinit: malloc (%lu) failed", (unsigned long)sz);
394                         goto fail;
395                 }
396
397                 /* XXX I assume short reads won't happen here.  */
398                 if (read (fd, new_conf_addr, sz) != (int)sz) {
399                         xlog_warn("conf_reinit: read (%d, %p, %lu) failed",
400                                 fd, new_conf_addr, (unsigned long)sz);
401                         goto fail;
402                 }
403                 close(fd);
404
405                 trans = conf_begin();
406                 /* XXX Should we not care about errors and rollback?  */
407                 conf_parse(trans, new_conf_addr, sz);
408         }
409         else
410                 trans = conf_begin();
411
412         /* Load default configuration values.  */
413         conf_load_defaults(trans);
414
415         /* Free potential existing configuration.  */
416         if (conf_addr) {
417                 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) {
418                         cb = LIST_FIRST (&conf_bindings[i]);
419                         for (; cb; cb = LIST_FIRST (&conf_bindings[i]))
420                                 conf_remove_now(cb->section, cb->tag);
421                 }
422                 free (conf_addr);
423         }
424
425         conf_end(trans, 1);
426         conf_addr = new_conf_addr;
427         return;
428
429 fail:
430         if (new_conf_addr)
431                 free(new_conf_addr);
432         close (fd);
433 }
434
435 /*
436  * Return the numeric value denoted by TAG in section SECTION or DEF
437  * if that tag does not exist.
438  */
439 int
440 conf_get_num(char *section, char *tag, int def)
441 {
442         char *value = conf_get_str(section, tag);
443
444         if (value)
445                 return atoi(value);
446
447         return def;
448 }
449
450 /* Validate X according to the range denoted by TAG in section SECTION.  */
451 int
452 conf_match_num(char *section, char *tag, int x)
453 {
454         char *value = conf_get_str (section, tag);
455         int val, min, max, n;
456
457         if (!value)
458                 return 0;
459         n = sscanf (value, "%d,%d:%d", &val, &min, &max);
460         switch (n) {
461         case 1:
462                 xlog(LOG_INFO, "conf_match_num: %s:%s %d==%d?", section, tag, val, x);
463                 return x == val;
464         case 3:
465                 xlog(LOG_INFO, "conf_match_num: %s:%s %d<=%d<=%d?", section, 
466                         tag, min, x, max);
467                 return min <= x && max >= x;
468         default:
469                 xlog(LOG_INFO, "conf_match_num: section %s tag %s: invalid number spec %s",
470                         section, tag, value);
471         }
472         return 0;
473 }
474
475 /* Return the string value denoted by TAG in section SECTION.  */
476 char *
477 conf_get_str(char *section, char *tag)
478 {
479         struct conf_binding *cb;
480
481         cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
482         for (; cb; cb = LIST_NEXT (cb, link)) {
483                 if (strcasecmp (section, cb->section) == 0
484                                 && strcasecmp (tag, cb->tag) == 0)
485                         return cb->value;
486         }
487         return 0;
488 }
489 /*
490  * Find a section that may or may not have an argument
491  */
492 char *
493 conf_get_section(char *section, char *arg, char *tag)
494 {
495         struct conf_binding *cb;
496
497         cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
498         for (; cb; cb = LIST_NEXT (cb, link)) {
499                 if (strcasecmp(section, cb->section) != 0)
500                         continue;
501                 if (arg && strcasecmp(arg, cb->arg) != 0)
502                         continue;
503                 if (strcasecmp(tag, cb->tag) != 0)
504                         continue;
505                 return cb->value;
506         }
507         return 0;
508 }
509
510 /*
511  * Build a list of string values out of the comma separated value denoted by
512  * TAG in SECTION.
513  */
514 struct conf_list *
515 conf_get_list(char *section, char *tag)
516 {
517         char *liststr = 0, *p, *field, *t;
518         struct conf_list *list = 0;
519         struct conf_list_node *node;
520
521         list = malloc (sizeof *list);
522         if (!list)
523                 goto cleanup;
524         TAILQ_INIT (&list->fields);
525         list->cnt = 0;
526         liststr = conf_get_str(section, tag);
527         if (!liststr)
528                 goto cleanup;
529         liststr = strdup (liststr);
530         if (!liststr)
531                 goto cleanup;
532         p = liststr;
533         while ((field = strsep (&p, ",")) != NULL) {
534                 /* Skip leading whitespace */
535                 while (isspace (*field))
536                         field++;
537                 /* Skip trailing whitespace */
538                 if (p) {
539                         for (t = p - 1; t > field && isspace (*t); t--)
540                                 *t = '\0';
541                 }
542                 if (*field == '\0') {
543                         xlog(LOG_INFO, "conf_get_list: empty field, ignoring...");
544                         continue;
545                 }
546                 list->cnt++;
547                 node = calloc (1, sizeof *node);
548                 if (!node)
549                         goto cleanup;
550                 node->field = strdup (field);
551                 if (!node->field) {
552                         free(node);
553                         goto cleanup;
554                 }
555                 TAILQ_INSERT_TAIL (&list->fields, node, link);
556         }
557         free (liststr);
558         return list;
559
560 cleanup:
561         if (list)
562                 conf_free_list(list);
563         if (liststr)
564                 free(liststr);
565         return 0;
566 }
567
568 struct conf_list *
569 conf_get_tag_list(char *section)
570 {
571         struct conf_list *list = 0;
572         struct conf_list_node *node;
573         struct conf_binding *cb;
574
575         list = malloc(sizeof *list);
576         if (!list)
577                 goto cleanup;
578         TAILQ_INIT(&list->fields);
579         list->cnt = 0;
580         cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
581         for (; cb; cb = LIST_NEXT(cb, link)) {
582                 if (strcasecmp (section, cb->section) == 0) {
583                         list->cnt++;
584                         node = calloc(1, sizeof *node);
585                         if (!node)
586                                 goto cleanup;
587                         node->field = strdup(cb->tag);
588                         if (!node->field) {
589                                 free(node);
590                                 goto cleanup;
591                         }
592                         TAILQ_INSERT_TAIL(&list->fields, node, link);
593                 }
594         }
595         return list;
596
597 cleanup:
598         if (list)
599                 conf_free_list(list);
600         return 0;
601 }
602
603 /* Decode a PEM encoded buffer.  */
604 int
605 conf_decode_base64 (u_int8_t *out, u_int32_t *len, u_char *buf)
606 {
607         u_int32_t c = 0;
608         u_int8_t c1, c2, c3, c4;
609
610         while (*buf) {
611                 if (*buf > 127 || (c1 = asc2bin[*buf]) == 255)
612                         return 0;
613
614                 buf++;
615                 if (*buf > 127 || (c2 = asc2bin[*buf]) == 255)
616                         return 0;
617
618                 buf++;
619                 if (*buf == '=') {
620                         c3 = c4 = 0;
621                         c++;
622
623                         /* Check last four bit */
624                         if (c2 & 0xF)
625                                 return 0;
626
627                         if (strcmp((char *)buf, "==") == 0)
628                                 buf++;
629                         else
630                                 return 0;
631                 } else if (*buf > 127 || (c3 = asc2bin[*buf]) == 255)
632                         return 0;
633                 else {
634                         if (*++buf == '=') {
635                                 c4 = 0;
636                                 c += 2;
637
638                                 /* Check last two bit */
639                                 if (c3 & 3)
640                                         return 0;
641
642                         if (strcmp((char *)buf, "="))
643                                 return 0;
644                         } else if (*buf > 127 || (c4 = asc2bin[*buf]) == 255)
645                                 return 0;
646                         else
647                                 c += 3;
648                 }
649
650                 buf++;
651                 *out++ = (c1 << 2) | (c2 >> 4);
652                 *out++ = (c2 << 4) | (c3 >> 2);
653                 *out++ = (c3 << 6) | c4;
654         }
655
656         *len = c;
657         return 1;
658 }
659
660 void
661 conf_free_list(struct conf_list *list)
662 {
663         struct conf_list_node *node = TAILQ_FIRST(&list->fields);
664
665         while (node) {
666                 TAILQ_REMOVE(&list->fields, node, link);
667                 if (node->field)
668                         free(node->field);
669                 free (node);
670                 node = TAILQ_FIRST(&list->fields);
671         }
672         free (list);
673 }
674
675 int
676 conf_begin(void)
677 {
678   static int seq = 0;
679
680   return ++seq;
681 }
682
683 static struct conf_trans *
684 conf_trans_node(int transaction, enum conf_op op)
685 {
686         struct conf_trans *node;
687
688         node = calloc (1, sizeof *node);
689         if (!node) {
690                 xlog_warn("conf_trans_node: calloc (1, %lu) failed",
691                 (unsigned long)sizeof *node);
692                 return 0;
693         }
694         node->trans = transaction;
695         node->op = op;
696         TAILQ_INSERT_TAIL (&conf_trans_queue, node, link);
697         return node;
698 }
699
700 /* Queue a set operation.  */
701 static int
702 conf_set(int transaction, char *section, char *arg,
703         char *tag, char *value, int override, int is_default)
704 {
705         struct conf_trans *node;
706
707         node = conf_trans_node(transaction, CONF_SET);
708         if (!node)
709                 return 1;
710         node->section = strdup(section);
711         if (!node->section) {
712                 xlog_warn("conf_set: strdup(\"%s\") failed", section);
713                 goto fail;
714         }
715         /* Make Section names case-insensitive */
716         upper2lower(node->section);
717
718         if (arg) {
719                 node->arg = strdup(arg);
720                 if (!node->arg) {
721                         xlog_warn("conf_set: strdup(\"%s\") failed", arg);
722                         goto fail;
723                 }
724         } else
725                 node->arg = NULL;
726
727         node->tag = strdup(tag);
728         if (!node->tag) {
729                 xlog_warn("conf_set: strdup(\"%s\") failed", tag);
730                 goto fail;
731         }
732         node->value = strdup(value);
733         if (!node->value) {
734                 xlog_warn("conf_set: strdup(\"%s\") failed", value);
735                 goto fail;
736         }
737         node->override = override;
738         node->is_default = is_default;
739         return 0;
740
741 fail:
742         if (node->tag)
743                 free(node->tag);
744         if (node->section)
745                 free(node->section);
746         if (node)
747                 free(node);
748         return 1;
749 }
750
751 /* Queue a remove operation.  */
752 int
753 conf_remove(int transaction, char *section, char *tag)
754 {
755         struct conf_trans *node;
756
757         node = conf_trans_node(transaction, CONF_REMOVE);
758         if (!node)
759                 goto fail;
760         node->section = strdup(section);
761         if (!node->section) {
762                 xlog_warn("conf_remove: strdup(\"%s\") failed", section);
763                 goto fail;
764         }
765         node->tag = strdup(tag);
766         if (!node->tag) {
767                 xlog_warn("conf_remove: strdup(\"%s\") failed", tag);
768                 goto fail;
769         }
770         return 0;
771
772 fail:
773         if (node && node->section)
774                 free (node->section);
775         if (node)
776                 free (node);
777         return 1;
778 }
779
780 /* Queue a remove section operation.  */
781 int
782 conf_remove_section(int transaction, char *section)
783 {
784         struct conf_trans *node;
785
786         node = conf_trans_node(transaction, CONF_REMOVE_SECTION);
787         if (!node)
788                 goto fail;
789         node->section = strdup(section);
790         if (!node->section) {
791                 xlog_warn("conf_remove_section: strdup(\"%s\") failed", section);
792                 goto fail;
793         }
794         return 0;
795
796 fail:
797         if (node)
798                 free(node);
799         return 1;
800 }
801
802 /* Execute all queued operations for this transaction.  Cleanup.  */
803 int
804 conf_end(int transaction, int commit)
805 {
806         struct conf_trans *node, *next;
807
808         for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
809                 next = TAILQ_NEXT(node, link);
810                 if (node->trans == transaction) {
811                         if (commit) {
812                                 switch (node->op) {
813                                 case CONF_SET:
814                                         conf_set_now(node->section, node->arg, 
815                                                 node->tag, node->value, node->override, 
816                                                 node->is_default);
817                                         break;
818                                 case CONF_REMOVE:
819                                         conf_remove_now(node->section, node->tag);
820                                         break;
821                                 case CONF_REMOVE_SECTION:
822                                         conf_remove_section_now(node->section);
823                                         break;
824                                 default:
825                                         xlog(LOG_INFO, "conf_end: unknown operation: %d", node->op);
826                                 }
827                         }
828                         TAILQ_REMOVE (&conf_trans_queue, node, link);
829                         if (node->section)
830                                 free(node->section);
831                         if (node->tag)
832                                 free(node->tag);
833                         if (node->value)
834                                 free(node->value);
835                         free (node);
836                 }
837         }
838         return 0;
839 }
840
841 /*
842  * Dump running configuration upon SIGUSR1.
843  * Configuration is "stored in reverse order", so reverse it again.
844  */
845 struct dumper {
846         char *s, *v;
847         struct dumper *next;
848 };
849
850 static void
851 conf_report_dump(struct dumper *node)
852 {
853         /* Recursive, cleanup when we're done.  */
854         if (node->next)
855                 conf_report_dump(node->next);
856
857         if (node->v)
858                 xlog(LOG_INFO, "%s=\t%s", node->s, node->v);
859         else if (node->s) {
860                 xlog(LOG_INFO, "%s", node->s);
861                 if (strlen(node->s) > 0)
862                         free(node->s);
863         }
864
865         free (node);
866 }
867
868 void
869 conf_report (void)
870 {
871         struct conf_binding *cb, *last = 0;
872         unsigned int i, len, diff_arg = 0;
873         char *current_section = (char *)0;
874         char *current_arg = (char *)0;
875         struct dumper *dumper, *dnode;
876
877         dumper = dnode = (struct dumper *)calloc(1, sizeof *dumper);
878         if (!dumper)
879                 goto mem_fail;
880
881         xlog(LOG_INFO, "conf_report: dumping running configuration");
882
883         for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
884                 for (cb = LIST_FIRST(&conf_bindings[i]); cb; cb = LIST_NEXT(cb, link)) {
885                         if (!cb->is_default) {
886                                 /* Make sure the Section arugment is the same */
887                                 if (current_arg && current_section && cb->arg) {
888                                         if (strcmp(cb->section, current_section) == 0 &&
889                                                 strcmp(cb->arg, current_arg) != 0)
890                                         diff_arg = 1;
891                                 }
892                                 /* Dump this entry.  */
893                                 if (!current_section || strcmp(cb->section, current_section) 
894                                                         || diff_arg) {
895                                         if (current_section || diff_arg) {
896                                                 len = strlen (current_section) + 3;
897                                                 if (current_arg)
898                                                         len += strlen(current_arg) + 3;
899                                                 dnode->s = malloc(len);
900                                                 if (!dnode->s)
901                                                         goto mem_fail;
902
903                                                 if (current_arg)
904                                                         snprintf(dnode->s, len, "[%s \"%s\"]", 
905                                                                 current_section, current_arg);
906                                                 else
907                                                         snprintf(dnode->s, len, "[%s]", current_section);
908
909                                                 dnode->next = 
910                                                         (struct dumper *)calloc(1, sizeof (struct dumper));
911                                                 dnode = dnode->next;
912                                                 if (!dnode)
913                                                         goto mem_fail;
914
915                                                 dnode->s = "";
916                                                 dnode->next = 
917                                                         (struct dumper *)calloc(1, sizeof (struct dumper));
918                                                 dnode = dnode->next;
919                                                 if (!dnode)
920                                                 goto mem_fail;
921                                         }
922                                         current_section = cb->section;
923                                         current_arg = cb->arg;
924                                         diff_arg = 0;
925                                 }
926                                 dnode->s = cb->tag;
927                                 dnode->v = cb->value;
928                                 dnode->next = (struct dumper *)calloc (1, sizeof (struct dumper));
929                                 dnode = dnode->next;
930                                 if (!dnode)
931                                         goto mem_fail;
932                                 last = cb;
933                 }
934         }
935
936         if (last) {
937                 len = strlen(last->section) + 3;
938                 if (last->arg)
939                         len += strlen(last->arg) + 3;
940                 dnode->s = malloc(len);
941                 if (!dnode->s)
942                         goto mem_fail;
943                 if (last->arg)
944                         snprintf(dnode->s, len, "[%s \"%s\"]", last->section, last->arg);
945                 else
946                         snprintf(dnode->s, len, "[%s]", last->section);
947         }
948         conf_report_dump(dumper);
949         return;
950
951 mem_fail:
952         xlog_warn("conf_report: malloc/calloc failed");
953         while ((dnode = dumper) != 0) {
954                 dumper = dumper->next;
955                 if (dnode->s)
956                         free(dnode->s);
957                 free(dnode);
958         }
959         return;
960 }