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 (int);
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 * Convert letter from upper case to lower case
125 static inline void upper2lower(char *str)
130 *ptr++ = tolower(*ptr);
135 * Insert a tag-value combination from LINE (the equal sign is at POS)
138 conf_remove_now(char *section, char *tag)
140 struct conf_binding *cb, *next;
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);
161 conf_remove_section_now(char *section)
163 struct conf_binding *cb, *next;
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) {
171 LIST_REMOVE(cb, link);
172 xlog(LOG_INFO, "[%s]:%s->%s removed", section, cb->tag, cb->value);
184 * Insert a tag-value combination from LINE (the equal sign is at POS)
185 * into SECTION of our configuration database.
188 conf_set_now(char *section, char *arg, char *tag,
189 char *value, int override, int is_default)
191 struct conf_binding *node = 0;
194 conf_remove_now(section, tag);
195 else if (conf_get_section(section, arg, tag)) {
197 xlog(LOG_INFO, "conf_set: duplicate tag [%s]:%s, ignoring...\n",
202 node = calloc(1, sizeof *node);
204 xlog_warn("conf_set: calloc (1, %lu) failed", (unsigned long)sizeof *node);
207 node->section = strdup(section);
209 node->arg = strdup(arg);
210 node->tag = strdup(tag);
211 node->value = strdup(value);
212 node->is_default = is_default;
214 LIST_INSERT_HEAD(&conf_bindings[conf_hash (section)], node, link);
219 * Parse the line LINE of SZ bytes. Skip Comments, recognize section
220 * headers and feed tag-value pairs into our configuration database.
223 conf_parse_line(int trans, char *line, size_t sz)
228 static char *section = 0;
229 static char *arg = 0;
232 /* Lines starting with '#' or ';' are comments. */
234 if (*line == '#' || *line == ';')
237 /* Ignore blank lines */
241 /* Strip off any leading blanks */
242 while (isblank(*line))
245 /* '[section]' parsing... */
248 /* Strip off any blanks after '[' */
249 while (isblank(*line))
251 for (i = 0; i < sz; i++) {
252 if (line[i] == ']') {
259 xlog_warn("conf_parse_line: %d:"
260 "non-matched ']', ignoring until next section", ln);
264 /* Strip off any blanks before ']' */
266 while (*val && !isblank(*val))
272 xlog_warn("conf_parse_line: %d: malloc (%lu) failed", ln,
276 strncpy(section, line, i);
282 ptr = strchr(val, '"');
286 while (*ptr && *ptr != '"')
289 xlog_warn("conf_parse_line: line %d:"
290 "non-matched '\"', ignoring until next section", ln);
295 xlog_warn("conf_parse_line: %d: malloc arg failed", ln);
300 /* Deal with assignments. */
301 for (i = 0; i < sz; i++) {
302 if (line[i] == '=') {
303 /* If no section, we are ignoring the lines. */
305 xlog_warn("conf_parse_line: %d: ignoring line due to no section",
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--)
314 /* XXX Perhaps should we not ignore errors? */
315 conf_set(trans, section, arg, line, val, 0, 0);
319 /* Other non-empty lines are weird. */
320 i = strspn(line, " \t");
322 xlog_warn("conf_parse_line: %d: syntax error", ln);
327 /* Parse the mapped configuration file. */
329 conf_parse(int trans, char *buf, size_t sz)
332 char *bufend = buf + sz;
336 while (cp < bufend) {
338 /* Check for escaped newlines. */
339 if (cp > buf && *(cp - 1) == '\\')
340 *(cp - 1) = *cp = ' ';
343 conf_parse_line(trans, line, cp - line);
350 xlog_warn("conf_parse: last line non-terminated, ignored.");
354 conf_load_defaults(int tr)
365 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
366 LIST_INIT (&conf_bindings[i]);
368 TAILQ_INIT (&conf_trans_queue);
372 /* Open the config file and map it into our address space, then parse it. */
376 struct conf_binding *cb = 0;
380 char *new_conf_addr = 0;
383 if ((stat (conf_path, &sb) == 0) || (errno != ENOENT)) {
385 fd = open (conf_path, O_RDONLY, 0);
387 xlog_warn("conf_reinit: open (\"%s\", O_RDONLY) failed", conf_path);
391 new_conf_addr = malloc(sz);
392 if (!new_conf_addr) {
393 xlog_warn("conf_reinit: malloc (%lu) failed", (unsigned long)sz);
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);
405 trans = conf_begin();
406 /* XXX Should we not care about errors and rollback? */
407 conf_parse(trans, new_conf_addr, sz);
410 trans = conf_begin();
412 /* Load default configuration values. */
413 conf_load_defaults(trans);
415 /* Free potential existing configuration. */
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);
426 conf_addr = new_conf_addr;
436 * Return the numeric value denoted by TAG in section SECTION or DEF
437 * if that tag does not exist.
440 conf_get_num(char *section, char *tag, int def)
442 char *value = conf_get_str(section, tag);
450 /* Validate X according to the range denoted by TAG in section SECTION. */
452 conf_match_num(char *section, char *tag, int x)
454 char *value = conf_get_str (section, tag);
455 int val, min, max, n;
459 n = sscanf (value, "%d,%d:%d", &val, &min, &max);
462 xlog(LOG_INFO, "conf_match_num: %s:%s %d==%d?", section, tag, val, x);
465 xlog(LOG_INFO, "conf_match_num: %s:%s %d<=%d<=%d?", section,
467 return min <= x && max >= x;
469 xlog(LOG_INFO, "conf_match_num: section %s tag %s: invalid number spec %s",
470 section, tag, value);
475 /* Return the string value denoted by TAG in section SECTION. */
477 conf_get_str(char *section, char *tag)
479 struct conf_binding *cb;
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)
490 * Find a section that may or may not have an argument
493 conf_get_section(char *section, char *arg, char *tag)
495 struct conf_binding *cb;
497 cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
498 for (; cb; cb = LIST_NEXT (cb, link)) {
499 if (strcasecmp(section, cb->section) != 0)
501 if (arg && strcasecmp(arg, cb->arg) != 0)
503 if (strcasecmp(tag, cb->tag) != 0)
511 * Build a list of string values out of the comma separated value denoted by
515 conf_get_list(char *section, char *tag)
517 char *liststr = 0, *p, *field, *t;
518 struct conf_list *list = 0;
519 struct conf_list_node *node;
521 list = malloc (sizeof *list);
524 TAILQ_INIT (&list->fields);
526 liststr = conf_get_str(section, tag);
529 liststr = strdup (liststr);
533 while ((field = strsep (&p, ",")) != NULL) {
534 /* Skip leading whitespace */
535 while (isspace (*field))
537 /* Skip trailing whitespace */
539 for (t = p - 1; t > field && isspace (*t); t--)
542 if (*field == '\0') {
543 xlog(LOG_INFO, "conf_get_list: empty field, ignoring...");
547 node = calloc (1, sizeof *node);
550 node->field = strdup (field);
555 TAILQ_INSERT_TAIL (&list->fields, node, link);
562 conf_free_list(list);
569 conf_get_tag_list(char *section)
571 struct conf_list *list = 0;
572 struct conf_list_node *node;
573 struct conf_binding *cb;
575 list = malloc(sizeof *list);
578 TAILQ_INIT(&list->fields);
580 cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
581 for (; cb; cb = LIST_NEXT(cb, link)) {
582 if (strcasecmp (section, cb->section) == 0) {
584 node = calloc(1, sizeof *node);
587 node->field = strdup(cb->tag);
592 TAILQ_INSERT_TAIL(&list->fields, node, link);
599 conf_free_list(list);
603 /* Decode a PEM encoded buffer. */
605 conf_decode_base64 (u_int8_t *out, u_int32_t *len, u_char *buf)
608 u_int8_t c1, c2, c3, c4;
611 if (*buf > 127 || (c1 = asc2bin[*buf]) == 255)
615 if (*buf > 127 || (c2 = asc2bin[*buf]) == 255)
623 /* Check last four bit */
627 if (strcmp((char *)buf, "==") == 0)
631 } else if (*buf > 127 || (c3 = asc2bin[*buf]) == 255)
638 /* Check last two bit */
642 if (strcmp((char *)buf, "="))
644 } else if (*buf > 127 || (c4 = asc2bin[*buf]) == 255)
651 *out++ = (c1 << 2) | (c2 >> 4);
652 *out++ = (c2 << 4) | (c3 >> 2);
653 *out++ = (c3 << 6) | c4;
661 conf_free_list(struct conf_list *list)
663 struct conf_list_node *node = TAILQ_FIRST(&list->fields);
666 TAILQ_REMOVE(&list->fields, node, link);
670 node = TAILQ_FIRST(&list->fields);
683 static struct conf_trans *
684 conf_trans_node(int transaction, enum conf_op op)
686 struct conf_trans *node;
688 node = calloc (1, sizeof *node);
690 xlog_warn("conf_trans_node: calloc (1, %lu) failed",
691 (unsigned long)sizeof *node);
694 node->trans = transaction;
696 TAILQ_INSERT_TAIL (&conf_trans_queue, node, link);
700 /* Queue a set operation. */
702 conf_set(int transaction, char *section, char *arg,
703 char *tag, char *value, int override, int is_default)
705 struct conf_trans *node;
707 node = conf_trans_node(transaction, CONF_SET);
710 node->section = strdup(section);
711 if (!node->section) {
712 xlog_warn("conf_set: strdup(\"%s\") failed", section);
715 /* Make Section names case-insensitive */
716 upper2lower(node->section);
719 node->arg = strdup(arg);
721 xlog_warn("conf_set: strdup(\"%s\") failed", arg);
727 node->tag = strdup(tag);
729 xlog_warn("conf_set: strdup(\"%s\") failed", tag);
732 node->value = strdup(value);
734 xlog_warn("conf_set: strdup(\"%s\") failed", value);
737 node->override = override;
738 node->is_default = is_default;
751 /* Queue a remove operation. */
753 conf_remove(int transaction, char *section, char *tag)
755 struct conf_trans *node;
757 node = conf_trans_node(transaction, CONF_REMOVE);
760 node->section = strdup(section);
761 if (!node->section) {
762 xlog_warn("conf_remove: strdup(\"%s\") failed", section);
765 node->tag = strdup(tag);
767 xlog_warn("conf_remove: strdup(\"%s\") failed", tag);
773 if (node && node->section)
774 free (node->section);
780 /* Queue a remove section operation. */
782 conf_remove_section(int transaction, char *section)
784 struct conf_trans *node;
786 node = conf_trans_node(transaction, CONF_REMOVE_SECTION);
789 node->section = strdup(section);
790 if (!node->section) {
791 xlog_warn("conf_remove_section: strdup(\"%s\") failed", section);
802 /* Execute all queued operations for this transaction. Cleanup. */
804 conf_end(int transaction, int commit)
806 struct conf_trans *node, *next;
808 for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
809 next = TAILQ_NEXT(node, link);
810 if (node->trans == transaction) {
814 conf_set_now(node->section, node->arg,
815 node->tag, node->value, node->override,
819 conf_remove_now(node->section, node->tag);
821 case CONF_REMOVE_SECTION:
822 conf_remove_section_now(node->section);
825 xlog(LOG_INFO, "conf_end: unknown operation: %d", node->op);
828 TAILQ_REMOVE (&conf_trans_queue, node, link);
842 * Dump running configuration upon SIGUSR1.
843 * Configuration is "stored in reverse order", so reverse it again.
851 conf_report_dump(struct dumper *node)
853 /* Recursive, cleanup when we're done. */
855 conf_report_dump(node->next);
858 xlog(LOG_INFO, "%s=\t%s", node->s, node->v);
860 xlog(LOG_INFO, "%s", node->s);
861 if (strlen(node->s) > 0)
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;
877 dumper = dnode = (struct dumper *)calloc(1, sizeof *dumper);
881 xlog(LOG_INFO, "conf_report: dumping running configuration");
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)
892 /* Dump this entry. */
893 if (!current_section || strcmp(cb->section, current_section)
895 if (current_section || diff_arg) {
896 len = strlen (current_section) + 3;
898 len += strlen(current_arg) + 3;
899 dnode->s = malloc(len);
904 snprintf(dnode->s, len, "[%s \"%s\"]",
905 current_section, current_arg);
907 snprintf(dnode->s, len, "[%s]", current_section);
910 (struct dumper *)calloc(1, sizeof (struct dumper));
917 (struct dumper *)calloc(1, sizeof (struct dumper));
922 current_section = cb->section;
923 current_arg = cb->arg;
927 dnode->v = cb->value;
928 dnode->next = (struct dumper *)calloc (1, sizeof (struct dumper));
937 len = strlen(last->section) + 3;
939 len += strlen(last->arg) + 3;
940 dnode->s = malloc(len);
944 snprintf(dnode->s, len, "[%s \"%s\"]", last->section, last->arg);
946 snprintf(dnode->s, len, "[%s]", last->section);
948 conf_report_dump(dumper);
952 xlog_warn("conf_report: malloc/calloc failed");
953 while ((dnode = dumper) != 0) {
954 dumper = dumper->next;