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 $ */
5 * Copyright (c) 1998, 1999, 2000, 2001 Niklas Hallqvist. All rights reserved.
6 * Copyright (c) 2000, 2001, 2002 HÃ¥kan Olsson. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
30 * This code was written under funding by Ericsson Radio Systems.
33 #include <sys/param.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
52 static void conf_load_defaults(void);
53 static int conf_set(int , char *, char *, char *,
57 TAILQ_ENTRY (conf_trans) link;
59 enum conf_op { CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION } op;
68 TAILQ_HEAD (conf_trans_head, conf_trans) conf_trans_queue;
73 static const u_int8_t bin2asc[]
74 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
76 static const u_int8_t asc2bin[] =
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
97 LIST_ENTRY (conf_binding) link;
106 LIST_HEAD (conf_bindings, conf_binding) conf_bindings[256];
108 static char *conf_addr;
110 static __inline__ u_int8_t
116 hash = ((hash << 1) | (hash >> 7)) ^ tolower (*s);
123 * Insert a tag-value combination from LINE (the equal sign is at POS)
126 conf_remove_now(char *section, char *tag)
128 struct conf_binding *cb, *next;
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);
149 conf_remove_section_now(char *section)
151 struct conf_binding *cb, *next;
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) {
159 LIST_REMOVE(cb, link);
160 xlog(LOG_INFO, "[%s]:%s->%s removed", section, cb->tag, cb->value);
172 * Insert a tag-value combination from LINE (the equal sign is at POS)
173 * into SECTION of our configuration database.
176 conf_set_now(char *section, char *arg, char *tag,
177 char *value, int override, int is_default)
179 struct conf_binding *node = 0;
182 conf_remove_now(section, tag);
183 else if (conf_get_section(section, arg, tag)) {
185 xlog(LOG_INFO, "conf_set: duplicate tag [%s]:%s, ignoring...\n",
190 node = calloc(1, sizeof *node);
192 xlog_warn("conf_set: calloc (1, %lu) failed", (unsigned long)sizeof *node);
195 node->section = strdup(section);
197 node->arg = strdup(arg);
198 node->tag = strdup(tag);
199 node->value = strdup(value);
200 node->is_default = is_default;
202 LIST_INSERT_HEAD(&conf_bindings[conf_hash (section)], node, link);
207 * Parse the line LINE of SZ bytes. Skip Comments, recognize section
208 * headers and feed tag-value pairs into our configuration database.
211 conf_parse_line(int trans, char *line, size_t sz)
216 static char *section = 0;
217 static char *arg = 0;
220 /* Lines starting with '#' or ';' are comments. */
222 /* Ignore blank lines */
226 /* Strip off any leading blanks */
227 while (isblank(*line))
230 if (*line == '#' || *line == ';')
233 /* '[section]' parsing... */
236 /* Strip off any blanks after '[' */
237 while (isblank(*line))
239 for (i = 0; i < sz; i++) {
240 if (line[i] == ']') {
247 xlog_warn("config file error: line %d: "
248 "non-matched ']', ignoring until next section", ln);
252 /* Strip off any blanks before ']' */
255 while (*val && !isblank(*val))
261 xlog_warn("conf_parse_line: %d: malloc (%lu) failed", ln,
265 strncpy(section, line, i);
271 ptr = strchr(val, '"');
275 while (*ptr && *ptr != '"' && *ptr != ']')
277 if (*ptr == '\0' || *ptr == ']') {
278 xlog_warn("config file error: line %d: "
279 "non-matched '\"', ignoring until next section", ln);
284 xlog_warn("conf_parse_line: %d: malloc arg failed", ln);
289 /* Deal with assignments. */
290 for (i = 0; i < sz; i++) {
291 if (line[i] == '=') {
292 /* If no section, we are ignoring the lines. */
294 xlog_warn("config file error: line %d: "
295 "ignoring line due to no section", ln);
298 line[strcspn (line, " \t=")] = '\0';
299 val = line + i + 1 + strspn (line + i + 1, " \t");
301 /* Skip trailing comments, if any */
302 for (j = 0; j < sz - (val - line); j++) {
303 if (val[j] == '#' || val[j] == ';') {
309 /* Skip trailing whitespace, if any */
310 for (j--; j > 0; j--) {
317 /* XXX Perhaps should we not ignore errors? */
318 conf_set(trans, section, arg, line, val, 0, 0);
322 /* Other non-empty lines are weird. */
323 i = strspn(line, " \t");
325 xlog_warn("config file error: line %d:", ln);
330 /* Parse the mapped configuration file. */
332 conf_parse(int trans, char *buf, size_t sz)
335 char *bufend = buf + sz;
339 while (cp < bufend) {
341 /* Check for escaped newlines. */
342 if (cp > buf && *(cp - 1) == '\\')
343 *(cp - 1) = *cp = ' ';
346 conf_parse_line(trans, line, cp - line);
353 xlog_warn("conf_parse: last line non-terminated, ignored.");
357 conf_load_defaults(void)
368 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
369 LIST_INIT (&conf_bindings[i]);
371 TAILQ_INIT (&conf_trans_queue);
375 /* Open the config file and map it into our address space, then parse it. */
379 struct conf_binding *cb = 0;
383 char *new_conf_addr = 0;
386 if ((stat (conf_path, &sb) == 0) || (errno != ENOENT)) {
388 fd = open (conf_path, O_RDONLY, 0);
390 xlog_warn("conf_reinit: open (\"%s\", O_RDONLY) failed", conf_path);
394 new_conf_addr = malloc(sz);
395 if (!new_conf_addr) {
396 xlog_warn("conf_reinit: malloc (%lu) failed", (unsigned long)sz);
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);
408 trans = conf_begin();
409 /* XXX Should we not care about errors and rollback? */
410 conf_parse(trans, new_conf_addr, sz);
413 trans = conf_begin();
415 /* Load default configuration values. */
416 conf_load_defaults();
418 /* Free potential existing configuration. */
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);
429 conf_addr = new_conf_addr;
439 * Return the numeric value denoted by TAG in section SECTION or DEF
440 * if that tag does not exist.
443 conf_get_num(char *section, char *tag, int def)
445 char *value = conf_get_str(section, tag);
453 /* Validate X according to the range denoted by TAG in section SECTION. */
455 conf_match_num(char *section, char *tag, int x)
457 char *value = conf_get_str (section, tag);
458 int val, min, max, n;
462 n = sscanf (value, "%d,%d:%d", &val, &min, &max);
465 xlog(LOG_INFO, "conf_match_num: %s:%s %d==%d?", section, tag, val, x);
468 xlog(LOG_INFO, "conf_match_num: %s:%s %d<=%d<=%d?", section,
470 return min <= x && max >= x;
472 xlog(LOG_INFO, "conf_match_num: section %s tag %s: invalid number spec %s",
473 section, tag, value);
478 /* Return the string value denoted by TAG in section SECTION. */
480 conf_get_str(char *section, char *tag)
482 struct conf_binding *cb;
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)
493 * Find a section that may or may not have an argument
496 conf_get_section(char *section, char *arg, char *tag)
498 struct conf_binding *cb;
500 cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
501 for (; cb; cb = LIST_NEXT (cb, link)) {
502 if (strcasecmp(section, cb->section) != 0)
504 if (arg && strcasecmp(arg, cb->arg) != 0)
506 if (strcasecmp(tag, cb->tag) != 0)
514 * Build a list of string values out of the comma separated value denoted by
518 conf_get_list(char *section, char *tag)
520 char *liststr = 0, *p, *field, *t;
521 struct conf_list *list = 0;
522 struct conf_list_node *node;
524 list = malloc (sizeof *list);
527 TAILQ_INIT (&list->fields);
529 liststr = conf_get_str(section, tag);
532 liststr = strdup (liststr);
536 while ((field = strsep (&p, ",")) != NULL) {
537 /* Skip leading whitespace */
538 while (isspace (*field))
540 /* Skip trailing whitespace */
542 for (t = p - 1; t > field && isspace (*t); t--)
545 if (*field == '\0') {
546 xlog(LOG_INFO, "conf_get_list: empty field, ignoring...");
550 node = calloc (1, sizeof *node);
553 node->field = strdup (field);
558 TAILQ_INSERT_TAIL (&list->fields, node, link);
565 conf_free_list(list);
572 conf_get_tag_list(char *section)
574 struct conf_list *list = 0;
575 struct conf_list_node *node;
576 struct conf_binding *cb;
578 list = malloc(sizeof *list);
581 TAILQ_INIT(&list->fields);
583 cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
584 for (; cb; cb = LIST_NEXT(cb, link)) {
585 if (strcasecmp (section, cb->section) == 0) {
587 node = calloc(1, sizeof *node);
590 node->field = strdup(cb->tag);
595 TAILQ_INSERT_TAIL(&list->fields, node, link);
602 conf_free_list(list);
606 /* Decode a PEM encoded buffer. */
608 conf_decode_base64 (u_int8_t *out, u_int32_t *len, u_char *buf)
611 u_int8_t c1, c2, c3, c4;
614 if (*buf > 127 || (c1 = asc2bin[*buf]) == 255)
618 if (*buf > 127 || (c2 = asc2bin[*buf]) == 255)
626 /* Check last four bit */
630 if (strcmp((char *)buf, "==") == 0)
634 } else if (*buf > 127 || (c3 = asc2bin[*buf]) == 255)
641 /* Check last two bit */
645 if (strcmp((char *)buf, "="))
647 } else if (*buf > 127 || (c4 = asc2bin[*buf]) == 255)
654 *out++ = (c1 << 2) | (c2 >> 4);
655 *out++ = (c2 << 4) | (c3 >> 2);
656 *out++ = (c3 << 6) | c4;
664 conf_free_list(struct conf_list *list)
666 struct conf_list_node *node = TAILQ_FIRST(&list->fields);
669 TAILQ_REMOVE(&list->fields, node, link);
673 node = TAILQ_FIRST(&list->fields);
686 static struct conf_trans *
687 conf_trans_node(int transaction, enum conf_op op)
689 struct conf_trans *node;
691 node = calloc (1, sizeof *node);
693 xlog_warn("conf_trans_node: calloc (1, %lu) failed",
694 (unsigned long)sizeof *node);
697 node->trans = transaction;
699 TAILQ_INSERT_TAIL (&conf_trans_queue, node, link);
703 /* Queue a set operation. */
705 conf_set(int transaction, char *section, char *arg,
706 char *tag, char *value, int override, int is_default)
708 struct conf_trans *node;
710 node = conf_trans_node(transaction, CONF_SET);
713 node->section = strdup(section);
714 if (!node->section) {
715 xlog_warn("conf_set: strdup(\"%s\") failed", section);
718 /* Make Section names case-insensitive */
719 upper2lower(node->section);
722 node->arg = strdup(arg);
724 xlog_warn("conf_set: strdup(\"%s\") failed", arg);
730 node->tag = strdup(tag);
732 xlog_warn("conf_set: strdup(\"%s\") failed", tag);
735 node->value = strdup(value);
737 xlog_warn("conf_set: strdup(\"%s\") failed", value);
740 node->override = override;
741 node->is_default = is_default;
754 /* Queue a remove operation. */
756 conf_remove(int transaction, char *section, char *tag)
758 struct conf_trans *node;
760 node = conf_trans_node(transaction, CONF_REMOVE);
763 node->section = strdup(section);
764 if (!node->section) {
765 xlog_warn("conf_remove: strdup(\"%s\") failed", section);
768 node->tag = strdup(tag);
770 xlog_warn("conf_remove: strdup(\"%s\") failed", tag);
776 if (node && node->section)
777 free (node->section);
783 /* Queue a remove section operation. */
785 conf_remove_section(int transaction, char *section)
787 struct conf_trans *node;
789 node = conf_trans_node(transaction, CONF_REMOVE_SECTION);
792 node->section = strdup(section);
793 if (!node->section) {
794 xlog_warn("conf_remove_section: strdup(\"%s\") failed", section);
805 /* Execute all queued operations for this transaction. Cleanup. */
807 conf_end(int transaction, int commit)
809 struct conf_trans *node, *next;
811 for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
812 next = TAILQ_NEXT(node, link);
813 if (node->trans == transaction) {
817 conf_set_now(node->section, node->arg,
818 node->tag, node->value, node->override,
822 conf_remove_now(node->section, node->tag);
824 case CONF_REMOVE_SECTION:
825 conf_remove_section_now(node->section);
828 xlog(LOG_INFO, "conf_end: unknown operation: %d", node->op);
831 TAILQ_REMOVE (&conf_trans_queue, node, link);
845 * Dump running configuration upon SIGUSR1.
846 * Configuration is "stored in reverse order", so reverse it again.
854 conf_report_dump(struct dumper *node)
856 /* Recursive, cleanup when we're done. */
858 conf_report_dump(node->next);
861 xlog(LOG_INFO, "%s=\t%s", node->s, node->v);
863 xlog(LOG_INFO, "%s", node->s);
864 if (strlen(node->s) > 0)
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;
880 dumper = dnode = (struct dumper *)calloc(1, sizeof *dumper);
884 xlog(LOG_INFO, "conf_report: dumping running configuration");
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)
895 /* Dump this entry. */
896 if (!current_section || strcmp(cb->section, current_section)
898 if (current_section || diff_arg) {
899 len = strlen (current_section) + 3;
901 len += strlen(current_arg) + 3;
902 dnode->s = malloc(len);
907 snprintf(dnode->s, len, "[%s \"%s\"]",
908 current_section, current_arg);
910 snprintf(dnode->s, len, "[%s]", current_section);
913 (struct dumper *)calloc(1, sizeof (struct dumper));
920 (struct dumper *)calloc(1, sizeof (struct dumper));
925 current_section = cb->section;
926 current_arg = cb->arg;
930 dnode->v = cb->value;
931 dnode->next = (struct dumper *)calloc (1, sizeof (struct dumper));
940 len = strlen(last->section) + 3;
942 len += strlen(last->arg) + 3;
943 dnode->s = malloc(len);
947 snprintf(dnode->s, len, "[%s \"%s\"]", last->section, last->arg);
949 snprintf(dnode->s, len, "[%s]", last->section);
951 conf_report_dump(dumper);
955 xlog_warn("conf_report: malloc/calloc failed");
956 while ((dnode = dumper) != 0) {
957 dumper = dumper->next;