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 #pragma GCC visibility push(hidden)
54 static void conf_load_defaults(void);
55 static int conf_set(int , char *, char *, char *,
59 TAILQ_ENTRY (conf_trans) link;
61 enum conf_op { CONF_SET, CONF_REMOVE, CONF_REMOVE_SECTION } op;
70 TAILQ_HEAD (conf_trans_head, conf_trans) conf_trans_queue;
75 static const u_int8_t bin2asc[]
76 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
78 static const u_int8_t asc2bin[] =
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, 255, 255, 255, 255, 255,
84 255, 255, 255, 255, 255, 255, 255, 255,
85 255, 255, 255, 62, 255, 255, 255, 63,
86 52, 53, 54, 55, 56, 57, 58, 59,
87 60, 61, 255, 255, 255, 255, 255, 255,
88 255, 0, 1, 2, 3, 4, 5, 6,
89 7, 8, 9, 10, 11, 12, 13, 14,
90 15, 16, 17, 18, 19, 20, 21, 22,
91 23, 24, 25, 255, 255, 255, 255, 255,
92 255, 26, 27, 28, 29, 30, 31, 32,
93 33, 34, 35, 36, 37, 38, 39, 40,
94 41, 42, 43, 44, 45, 46, 47, 48,
95 49, 50, 51, 255, 255, 255, 255, 255
99 LIST_ENTRY (conf_binding) link;
108 LIST_HEAD (conf_bindings, conf_binding) conf_bindings[256];
110 static char *conf_addr;
112 static __inline__ u_int8_t
118 hash = ((hash << 1) | (hash >> 7)) ^ tolower (*s);
125 * Insert a tag-value combination from LINE (the equal sign is at POS)
128 conf_remove_now(char *section, char *tag)
130 struct conf_binding *cb, *next;
132 cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
133 for (; cb; cb = next) {
134 next = LIST_NEXT(cb, link);
135 if (strcasecmp(cb->section, section) == 0
136 && strcasecmp(cb->tag, tag) == 0) {
137 LIST_REMOVE(cb, link);
138 xlog(LOG_INFO,"[%s]:%s->%s removed", section, tag, cb->value);
151 conf_remove_section_now(char *section)
153 struct conf_binding *cb, *next;
156 cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
157 for (; cb; cb = next) {
158 next = LIST_NEXT(cb, link);
159 if (strcasecmp(cb->section, section) == 0) {
161 LIST_REMOVE(cb, link);
162 xlog(LOG_INFO, "[%s]:%s->%s removed", section, cb->tag, cb->value);
174 * Insert a tag-value combination from LINE (the equal sign is at POS)
175 * into SECTION of our configuration database.
178 conf_set_now(char *section, char *arg, char *tag,
179 char *value, int override, int is_default)
181 struct conf_binding *node = 0;
184 conf_remove_now(section, tag);
185 else if (conf_get_section(section, arg, tag)) {
187 xlog(LOG_INFO, "conf_set: duplicate tag [%s]:%s, ignoring...\n",
192 node = calloc(1, sizeof *node);
194 xlog_warn("conf_set: calloc (1, %lu) failed", (unsigned long)sizeof *node);
197 node->section = strdup(section);
199 node->arg = strdup(arg);
200 node->tag = strdup(tag);
201 node->value = strdup(value);
202 node->is_default = is_default;
204 LIST_INSERT_HEAD(&conf_bindings[conf_hash (section)], node, link);
209 * Parse the line LINE of SZ bytes. Skip Comments, recognize section
210 * headers and feed tag-value pairs into our configuration database.
213 conf_parse_line(int trans, char *line, size_t sz)
218 static char *section = 0;
219 static char *arg = 0;
222 /* Lines starting with '#' or ';' are comments. */
224 /* Ignore blank lines */
228 /* Strip off any leading blanks */
229 while (isblank(*line))
232 if (*line == '#' || *line == ';')
235 /* '[section]' parsing... */
238 /* Strip off any blanks after '[' */
239 while (isblank(*line))
241 for (i = 0; i < sz; i++) {
242 if (line[i] == ']') {
249 xlog_warn("config file error: line %d: "
250 "non-matched ']', ignoring until next section", ln);
254 /* Strip off any blanks before ']' */
257 while (*val && !isblank(*val))
261 section = malloc(i+1);
263 xlog_warn("conf_parse_line: %d: malloc (%lu) failed", ln,
267 strncpy(section, line, i);
274 ptr = strchr(val, '"');
278 while (*ptr && *ptr != '"' && *ptr != ']')
280 if (*ptr == '\0' || *ptr == ']') {
281 xlog_warn("config file error: line %d: "
282 "non-matched '\"', ignoring until next section", ln);
287 xlog_warn("conf_parse_line: %d: malloc arg failed", ln);
292 /* Deal with assignments. */
293 for (i = 0; i < sz; i++) {
294 if (line[i] == '=') {
295 /* If no section, we are ignoring the lines. */
297 xlog_warn("config file error: line %d: "
298 "ignoring line due to no section", ln);
301 line[strcspn (line, " \t=")] = '\0';
302 val = line + i + 1 + strspn (line + i + 1, " \t");
304 while (val[valsize++]);
306 /* Skip trailing spaces and comments */
307 for (j = 0; j < valsize; j++) {
308 if (val[j] == '#' || val[j] == ';' || isspace(val[j])) {
313 /* XXX Perhaps should we not ignore errors? */
314 conf_set(trans, section, arg, line, val, 0, 0);
318 /* Other non-empty lines are weird. */
319 i = strspn(line, " \t");
321 xlog_warn("config file error: line %d:", ln);
326 /* Parse the mapped configuration file. */
328 conf_parse(int trans, char *buf, size_t sz)
331 char *bufend = buf + sz;
335 while (cp < bufend) {
337 /* Check for escaped newlines. */
338 if (cp > buf && *(cp - 1) == '\\')
339 *(cp - 1) = *cp = ' ';
342 conf_parse_line(trans, line, cp - line);
349 xlog_warn("conf_parse: last line non-terminated, ignored.");
353 conf_load_defaults(void)
364 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
365 LIST_INIT (&conf_bindings[i]);
367 TAILQ_INIT (&conf_trans_queue);
371 /* Open the config file and map it into our address space, then parse it. */
375 struct conf_binding *cb = 0;
379 char *new_conf_addr = 0;
382 if ((stat (conf_path, &sb) == 0) || (errno != ENOENT)) {
384 fd = open (conf_path, O_RDONLY, 0);
386 xlog_warn("conf_reinit: open (\"%s\", O_RDONLY) failed", conf_path);
390 new_conf_addr = malloc(sz);
391 if (!new_conf_addr) {
392 xlog_warn("conf_reinit: malloc (%lu) failed", (unsigned long)sz);
396 /* XXX I assume short reads won't happen here. */
397 if (read (fd, new_conf_addr, sz) != (int)sz) {
398 xlog_warn("conf_reinit: read (%d, %p, %lu) failed",
399 fd, new_conf_addr, (unsigned long)sz);
404 trans = conf_begin();
405 /* XXX Should we not care about errors and rollback? */
406 conf_parse(trans, new_conf_addr, sz);
409 trans = conf_begin();
411 /* Load default configuration values. */
412 conf_load_defaults();
414 /* Free potential existing configuration. */
416 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++) {
417 cb = LIST_FIRST (&conf_bindings[i]);
418 for (; cb; cb = LIST_FIRST (&conf_bindings[i]))
419 conf_remove_now(cb->section, cb->tag);
425 conf_addr = new_conf_addr;
435 * Return the numeric value denoted by TAG in section SECTION or DEF
436 * if that tag does not exist.
439 conf_get_num(char *section, char *tag, int def)
441 char *value = conf_get_str(section, tag);
449 /* Validate X according to the range denoted by TAG in section SECTION. */
451 conf_match_num(char *section, char *tag, int x)
453 char *value = conf_get_str (section, tag);
454 int val, min, max, n;
458 n = sscanf (value, "%d,%d:%d", &val, &min, &max);
461 xlog(LOG_INFO, "conf_match_num: %s:%s %d==%d?", section, tag, val, x);
464 xlog(LOG_INFO, "conf_match_num: %s:%s %d<=%d<=%d?", section,
466 return min <= x && max >= x;
468 xlog(LOG_INFO, "conf_match_num: section %s tag %s: invalid number spec %s",
469 section, tag, value);
474 /* Return the string value denoted by TAG in section SECTION. */
476 conf_get_str(char *section, char *tag)
478 struct conf_binding *cb;
480 cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
481 for (; cb; cb = LIST_NEXT (cb, link)) {
482 if (strcasecmp (section, cb->section) == 0
483 && strcasecmp (tag, cb->tag) == 0)
489 * Find a section that may or may not have an argument
492 conf_get_section(char *section, char *arg, char *tag)
494 struct conf_binding *cb;
496 cb = LIST_FIRST (&conf_bindings[conf_hash (section)]);
497 for (; cb; cb = LIST_NEXT (cb, link)) {
498 if (strcasecmp(section, cb->section) != 0)
500 if (arg && strcasecmp(arg, cb->arg) != 0)
502 if (strcasecmp(tag, cb->tag) != 0)
510 * Build a list of string values out of the comma separated value denoted by
514 conf_get_list(char *section, char *tag)
516 char *liststr = 0, *p, *field, *t;
517 struct conf_list *list = 0;
518 struct conf_list_node *node;
520 list = malloc (sizeof *list);
523 TAILQ_INIT (&list->fields);
525 liststr = conf_get_str(section, tag);
528 liststr = strdup (liststr);
532 while ((field = strsep (&p, ",")) != NULL) {
533 /* Skip leading whitespace */
534 while (isspace (*field))
536 /* Skip trailing whitespace */
538 for (t = p - 1; t > field && isspace (*t); t--)
541 if (*field == '\0') {
542 xlog(LOG_INFO, "conf_get_list: empty field, ignoring...");
546 node = calloc (1, sizeof *node);
549 node->field = strdup (field);
554 TAILQ_INSERT_TAIL (&list->fields, node, link);
561 conf_free_list(list);
568 conf_get_tag_list(char *section)
570 struct conf_list *list = 0;
571 struct conf_list_node *node;
572 struct conf_binding *cb;
574 list = malloc(sizeof *list);
577 TAILQ_INIT(&list->fields);
579 cb = LIST_FIRST(&conf_bindings[conf_hash (section)]);
580 for (; cb; cb = LIST_NEXT(cb, link)) {
581 if (strcasecmp (section, cb->section) == 0) {
583 node = calloc(1, sizeof *node);
586 node->field = strdup(cb->tag);
591 TAILQ_INSERT_TAIL(&list->fields, node, link);
598 conf_free_list(list);
602 /* Decode a PEM encoded buffer. */
604 conf_decode_base64 (u_int8_t *out, u_int32_t *len, u_char *buf)
607 u_int8_t c1, c2, c3, c4;
610 if (*buf > 127 || (c1 = asc2bin[*buf]) == 255)
614 if (*buf > 127 || (c2 = asc2bin[*buf]) == 255)
622 /* Check last four bit */
626 if (strcmp((char *)buf, "==") == 0)
630 } else if (*buf > 127 || (c3 = asc2bin[*buf]) == 255)
637 /* Check last two bit */
641 if (strcmp((char *)buf, "="))
643 } else if (*buf > 127 || (c4 = asc2bin[*buf]) == 255)
650 *out++ = (c1 << 2) | (c2 >> 4);
651 *out++ = (c2 << 4) | (c3 >> 2);
652 *out++ = (c3 << 6) | c4;
660 conf_free_list(struct conf_list *list)
662 struct conf_list_node *node = TAILQ_FIRST(&list->fields);
665 TAILQ_REMOVE(&list->fields, node, link);
669 node = TAILQ_FIRST(&list->fields);
682 static struct conf_trans *
683 conf_trans_node(int transaction, enum conf_op op)
685 struct conf_trans *node;
687 node = calloc (1, sizeof *node);
689 xlog_warn("conf_trans_node: calloc (1, %lu) failed",
690 (unsigned long)sizeof *node);
693 node->trans = transaction;
695 TAILQ_INSERT_TAIL (&conf_trans_queue, node, link);
699 /* Queue a set operation. */
701 conf_set(int transaction, char *section, char *arg,
702 char *tag, char *value, int override, int is_default)
704 struct conf_trans *node;
706 node = conf_trans_node(transaction, CONF_SET);
709 node->section = strdup(section);
710 if (!node->section) {
711 xlog_warn("conf_set: strdup(\"%s\") failed", section);
714 /* Make Section names case-insensitive */
715 upper2lower(node->section);
718 node->arg = strdup(arg);
720 xlog_warn("conf_set: strdup(\"%s\") failed", arg);
726 node->tag = strdup(tag);
728 xlog_warn("conf_set: strdup(\"%s\") failed", tag);
731 node->value = strdup(value);
733 xlog_warn("conf_set: strdup(\"%s\") failed", value);
736 node->override = override;
737 node->is_default = is_default;
750 /* Queue a remove operation. */
752 conf_remove(int transaction, char *section, char *tag)
754 struct conf_trans *node;
756 node = conf_trans_node(transaction, CONF_REMOVE);
759 node->section = strdup(section);
760 if (!node->section) {
761 xlog_warn("conf_remove: strdup(\"%s\") failed", section);
764 node->tag = strdup(tag);
766 xlog_warn("conf_remove: strdup(\"%s\") failed", tag);
772 if (node && node->section)
773 free (node->section);
779 /* Queue a remove section operation. */
781 conf_remove_section(int transaction, char *section)
783 struct conf_trans *node;
785 node = conf_trans_node(transaction, CONF_REMOVE_SECTION);
788 node->section = strdup(section);
789 if (!node->section) {
790 xlog_warn("conf_remove_section: strdup(\"%s\") failed", section);
801 /* Execute all queued operations for this transaction. Cleanup. */
803 conf_end(int transaction, int commit)
805 struct conf_trans *node, *next;
807 for (node = TAILQ_FIRST(&conf_trans_queue); node; node = next) {
808 next = TAILQ_NEXT(node, link);
809 if (node->trans == transaction) {
813 conf_set_now(node->section, node->arg,
814 node->tag, node->value, node->override,
818 conf_remove_now(node->section, node->tag);
820 case CONF_REMOVE_SECTION:
821 conf_remove_section_now(node->section);
824 xlog(LOG_INFO, "conf_end: unknown operation: %d", node->op);
827 TAILQ_REMOVE (&conf_trans_queue, node, link);
841 * Dump running configuration upon SIGUSR1.
842 * Configuration is "stored in reverse order", so reverse it again.
850 conf_report_dump(struct dumper *node)
852 /* Recursive, cleanup when we're done. */
854 conf_report_dump(node->next);
857 xlog(LOG_INFO, "%s=\t%s", node->s, node->v);
859 xlog(LOG_INFO, "%s", node->s);
860 if (strlen(node->s) > 0)
870 struct conf_binding *cb, *last = 0;
871 unsigned int i, len, diff_arg = 0;
872 char *current_section = (char *)0;
873 char *current_arg = (char *)0;
874 struct dumper *dumper, *dnode;
876 dumper = dnode = (struct dumper *)calloc(1, sizeof *dumper);
880 xlog(LOG_INFO, "conf_report: dumping running configuration");
882 for (i = 0; i < sizeof conf_bindings / sizeof conf_bindings[0]; i++)
883 for (cb = LIST_FIRST(&conf_bindings[i]); cb; cb = LIST_NEXT(cb, link)) {
884 if (!cb->is_default) {
885 /* Make sure the Section arugment is the same */
886 if (current_arg && current_section && cb->arg) {
887 if (strcmp(cb->section, current_section) == 0 &&
888 strcmp(cb->arg, current_arg) != 0)
891 /* Dump this entry. */
892 if (!current_section || strcmp(cb->section, current_section)
894 if (current_section || diff_arg) {
895 len = strlen (current_section) + 3;
897 len += strlen(current_arg) + 3;
898 dnode->s = malloc(len);
903 snprintf(dnode->s, len, "[%s \"%s\"]",
904 current_section, current_arg);
906 snprintf(dnode->s, len, "[%s]", current_section);
909 (struct dumper *)calloc(1, sizeof (struct dumper));
916 (struct dumper *)calloc(1, sizeof (struct dumper));
921 current_section = cb->section;
922 current_arg = cb->arg;
926 dnode->v = cb->value;
927 dnode->next = (struct dumper *)calloc (1, sizeof (struct dumper));
936 len = strlen(last->section) + 3;
938 len += strlen(last->arg) + 3;
939 dnode->s = malloc(len);
943 snprintf(dnode->s, len, "[%s \"%s\"]", last->section, last->arg);
945 snprintf(dnode->s, len, "[%s]", last->section);
947 conf_report_dump(dumper);
951 xlog_warn("conf_report: malloc/calloc failed");
952 while ((dnode = dumper) != 0) {
953 dumper = dumper->next;