2 * conffile.c Read the radiusd.conf file.
4 * Yep I should learn to use lex & yacc, or at least
5 * write a decent parser. I know how to do that, really :)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Copyright 2000,2006 The FreeRADIUS server project
25 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
26 * Copyright 2000 Alan DeKok <aland@ox.org>
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/parser.h>
33 #include <freeradius-devel/rad_assert.h>
39 #ifdef HAVE_SYS_STAT_H
45 typedef enum conf_property {
46 CONF_PROPERTY_INVALID = 0,
48 CONF_PROPERTY_INSTANCE,
51 const FR_NAME_NUMBER conf_property_name[] = {
52 { "name", CONF_PROPERTY_NAME},
53 { "instance", CONF_PROPERTY_INSTANCE},
58 typedef enum conf_type {
59 CONF_ITEM_INVALID = 0,
66 struct conf_item *next;
67 struct conf_part *parent;
84 struct conf_item *children;
85 struct conf_item *tail; /* for speed */
86 CONF_SECTION *template;
87 rbtree_t *pair_tree; /* and a partridge.. */
88 rbtree_t *section_tree; /* no jokes here */
89 rbtree_t *name2_tree; /* for sections of the same name2 */
93 CONF_PARSER const *variables;
96 CONF_SECTION *root_config = NULL;
99 * Internal data that is associated with a configuration section,
100 * so that we don't have to track it separately.
106 void *data; /* user data */
107 void (*free)(void *); /* free user data function */
110 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
111 void *data, void (*data_free)(void *),
113 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name,
115 static char const *cf_expand_variables(char const *cf, int *lineno,
116 CONF_SECTION *outercs,
117 char *output, size_t outsize,
119 static CONF_SECTION *cf_template_copy(CONF_SECTION *parent, CONF_SECTION const *template);
122 * Isolate the scary casts in these tiny provably-safe functions
124 CONF_PAIR *cf_itemtopair(CONF_ITEM const *ci)
131 rad_assert(ci->type == CONF_ITEM_PAIR);
133 memcpy(&out, &ci, sizeof(out));
136 CONF_SECTION *cf_itemtosection(CONF_ITEM const *ci)
143 rad_assert(ci->type == CONF_ITEM_SECTION);
145 memcpy(&out, &ci, sizeof(out));
148 CONF_ITEM *cf_pairtoitem(CONF_PAIR const *cp)
156 memcpy(&out, &cp, sizeof(out));
159 CONF_ITEM *cf_sectiontoitem(CONF_SECTION const *cs)
167 memcpy(&out, &cs, sizeof(out));
171 static CONF_ITEM *cf_datatoitem(CONF_DATA const *cd)
179 memcpy(&out, &cd, sizeof(out));
184 * Create a new CONF_PAIR
186 static CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr,
187 char const *value, FR_TOKEN op,
192 if (!attr) return NULL;
194 cp = talloc_zero(parent, CONF_PAIR);
195 if (!cp) return NULL;
197 cp->item.type = CONF_ITEM_PAIR;
198 cp->item.parent = parent;
199 cp->value_type = value_type;
202 cp->attr = talloc_typed_strdup(cp, attr);
210 cp->value = talloc_typed_strdup(cp, value);
211 if (!cp->value) goto error;
217 static int _cf_data_free(CONF_DATA *cd)
229 * rbtree callback function
231 static int pair_cmp(void const *a, void const *b)
233 CONF_PAIR const *one = a;
234 CONF_PAIR const *two = b;
236 return strcmp(one->attr, two->attr);
241 * rbtree callback function
243 static int section_cmp(void const *a, void const *b)
245 CONF_SECTION const *one = a;
246 CONF_SECTION const *two = b;
248 return strcmp(one->name1, two->name1);
253 * rbtree callback function
255 static int name2_cmp(void const *a, void const *b)
257 CONF_SECTION const *one = a;
258 CONF_SECTION const *two = b;
260 rad_assert(strcmp(one->name1, two->name1) == 0);
262 if (!one->name2 && !two->name2) return 0;
263 if (one->name2 && !two->name2) return -1;
264 if (!one->name2 && two->name2) return +1;
266 return strcmp(one->name2, two->name2);
271 * rbtree callback function
273 static int data_cmp(void const *a, void const *b)
277 CONF_DATA const *one = a;
278 CONF_DATA const *two = b;
280 rcode = one->flag - two->flag;
281 if (rcode != 0) return rcode;
283 return strcmp(one->name, two->name);
286 static int _cf_section_free(CONF_SECTION *cs)
289 * Name1 and name2 are allocated contiguous with
293 rbtree_free(cs->pair_tree);
294 cs->pair_tree = NULL;
296 if (cs->section_tree) {
297 rbtree_free(cs->section_tree);
298 cs->section_tree = NULL;
300 if (cs->name2_tree) {
301 rbtree_free(cs->name2_tree);
302 cs->name2_tree = NULL;
305 rbtree_free(cs->data_tree);
306 cs->data_tree = NULL;
314 * Allocate a CONF_SECTION
316 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1,
322 if (!name1) return NULL;
325 if (strchr(name2, '$')) {
326 name2 = cf_expand_variables(parent->item.filename,
327 &parent->item.lineno,
329 buffer, sizeof(buffer), name2);
331 ERROR("Failed expanding section name");
337 cs = talloc_zero(parent, CONF_SECTION);
338 if (!cs) return NULL;
340 cs->item.type = CONF_ITEM_SECTION;
341 cs->item.parent = parent;
343 cs->name1 = talloc_typed_strdup(cs, name1);
350 if (name2 && *name2) {
351 cs->name2 = talloc_typed_strdup(cs, name2);
352 if (!cs->name2) goto error;
355 cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
356 if (!cs->pair_tree) goto error;
358 talloc_set_destructor(cs, _cf_section_free);
361 * Don't create a data tree, it may not be needed.
365 * Don't create the section tree here, it may not
369 if (parent) cs->depth = parent->depth + 1;
375 * Replace pair in a given section with a new pair,
376 * of the given value.
378 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
381 CONF_ITEM *ci, *cn, **last;
383 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->value_type);
384 if (!newp) return -1;
390 * Find the old one from the linked list, and replace it
393 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
395 cn->next = (*last)->next;
402 rbtree_deletebydata(cs->pair_tree, ci);
404 rbtree_insert(cs->pair_tree, cn);
411 * Add an item to a configuration section.
413 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
415 if (!cs || !ci) return;
418 rad_assert(cs->tail == NULL);
421 rad_assert(cs->tail != NULL);
426 * Update the trees (and tail) for each item added.
428 for (/* nothing */; ci != NULL; ci = ci->next) {
432 * For fast lookups, pairs and sections get
437 if (!rbtree_insert(cs->pair_tree, ci)) {
438 CONF_PAIR *cp = cf_itemtopair(ci);
440 if (strcmp(cp->attr, "confdir") == 0) break;
441 if (!cp->value) break; /* module name, "ok", etc. */
445 case CONF_ITEM_SECTION: {
446 CONF_SECTION *cs_new = cf_itemtosection(ci);
447 CONF_SECTION *name1_cs;
449 if (!cs->section_tree) {
450 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
451 if (!cs->section_tree) {
452 ERROR("Out of memory");
457 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
459 if (!rbtree_insert(cs->section_tree, cs_new)) {
460 ERROR("Failed inserting section into tree");
468 * We'll ignore these checks for
469 * now. Various sections can be
470 * duplicated, such as "listen",
471 * "update", "if", "else", etc.
473 if (!name1_cs->name2 && !cs_new->name2) {
474 WARN("%s[%d] Duplicate configuration section \"%s { ...}\" %s %d",
475 ci->filename, ci->lineno, cs_new->name1, name1_cs->item.filename, name1_cs->item.lineno);
479 if ((name1_cs->name2 && cs_new->name2) &&
480 (strcmp(name1_cs->name2, cs_new->name2) == 0)) {
481 WARN("%s[%d] Duplicate configuration section \"%s %s { ...}\"",
482 ci->filename, ci->lineno, cs_new->name1, cs_new->name2);
488 * We already have a section of
489 * this "name1". Add a new
490 * sub-section based on name2.
492 if (!name1_cs->name2_tree) {
493 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
494 if (!name1_cs->name2_tree) {
495 ERROR("Out of memory");
501 * We don't care if this fails.
502 * If the user tries to create
503 * two sections of the same
504 * name1/name2, the duplicate
505 * section is just silently
508 rbtree_insert(name1_cs->name2_tree, cs_new);
510 } /* was a section */
513 if (!cs->data_tree) {
514 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
517 rbtree_insert(cs->data_tree, ci);
521 default: /* FIXME: assert & error! */
524 } /* switch over conf types */
529 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
530 CONF_SECTION *outercs,
535 CONF_SECTION const *cs = outercs;
539 strlcpy(name, ptr, sizeof(name));
543 * ".foo" means "foo from the current section"
549 * Just '.' means the current section
552 return cf_sectiontoitem(cs);
556 * ..foo means "foo from the section
557 * enclosing this section" (etc.)
560 if (cs->item.parent) {
561 cs = cs->item.parent;
565 * .. means the section
566 * enclosing this section
569 return cf_sectiontoitem(cs);
574 * "foo.bar.baz" means "from the root"
576 } else if (strchr(p, '.') != NULL) {
577 if (!parentcs) goto no_such_item;
589 if (r && q > r) q = NULL;
590 if (q && q < r) r = NULL;
596 q = strchr(r + 1, ']');
597 if (!q) return NULL; /* parse error */
600 * Points to foo[bar]xx: parse error,
601 * it should be foo[bar] or foo[bar].baz
603 if (q[1] && q[1] != '.') goto no_such_item;
607 next = cf_section_sub_find_name2(cs, p, r + 1);
612 * Points to a named instance of a section.
615 if (!next) goto no_such_item;
616 return &(next->item);
619 q++; /* ensure we skip the ']' and '.' */
623 next = cf_section_sub_find(cs, p);
627 if (!next) break; /* it MAY be a pair in this section! */
633 if (!*p) goto no_such_item;
637 * Find it in the current referenced
640 cp = cf_pair_find(cs, p);
641 if (cp) return &(cp->item);
643 next = cf_section_sub_find(cs, p);
644 if (next) return &(next->item);
647 * "foo" is "in the current section, OR in main".
649 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
655 WARN("No such configuration item %s", ptr);
660 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
662 if (!cs) return NULL;
664 while (cs->item.parent != NULL) {
665 cs = cs->item.parent;
673 * Expand the variables in an input string.
675 static char const *cf_expand_variables(char const *cf, int *lineno,
676 CONF_SECTION *outercs,
677 char *output, size_t outsize,
681 char const *end, *ptr;
682 CONF_SECTION const *parentcs;
686 * Find the master parent conf section.
687 * We can't use main_config.config, because we're in the
688 * process of re-building it, and it isn't set up yet...
690 parentcs = cf_top_section(outercs);
696 * Ignore anything other than "${"
698 if ((*ptr == '$') && (ptr[1] == '{')) {
704 * FIXME: Add support for ${foo:-bar},
709 * Look for trailing '}', and log a
710 * warning for anything that doesn't match,
711 * and exit with a fatal error.
713 end = strchr(ptr, '}');
716 INFO("%s[%d]: Variable expansion missing }",
724 * Can't really happen because input lines are
725 * capped at 8k, which is sizeof(name)
727 if ((size_t) (end - ptr) >= sizeof(name)) {
728 ERROR("%s[%d]: Reference string is too large",
733 memcpy(name, ptr, end - ptr);
734 name[end - ptr] = '\0';
736 q = strchr(name, ':');
741 ci = cf_reference_item(parentcs, outercs, name);
743 ERROR("%s[%d]: Reference \"%s\" not found", cf, *lineno, input);
748 * The expansion doesn't refer to another item or section
749 * it's the property of a section.
752 CONF_SECTION *mycs = cf_itemtosection(ci);
754 if (ci->type != CONF_ITEM_SECTION) {
755 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
759 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
760 case CONF_PROPERTY_NAME:
761 strcpy(p, mycs->name1);
764 case CONF_PROPERTY_INSTANCE:
765 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
769 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
775 } else if (ci->type == CONF_ITEM_PAIR) {
777 * Substitute the value of the variable.
779 cp = cf_itemtopair(ci);
781 ERROR("%s[%d]: Reference \"%s\" has no value",
786 if (p + strlen(cp->value) >= output + outsize) {
787 ERROR("%s[%d]: Reference \"%s\" is too long",
792 strcpy(p, cp->value);
796 } else if (ci->type == CONF_ITEM_SECTION) {
800 * Adding an entry again to a
801 * section is wrong. We don't
802 * want an infinite loop.
804 if (ci->parent == outercs) {
805 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
810 * Copy the section instead of
813 subcs = cf_template_copy(outercs, cf_itemtosection(ci));
815 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
819 subcs->item.filename = ci->filename;
820 subcs->item.lineno = ci->lineno;
821 cf_item_add(outercs, &(subcs->item));
826 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
829 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
835 * Look for trailing '}', and log a
836 * warning for anything that doesn't match,
837 * and exit with a fatal error.
839 end = strchr(ptr, '}');
842 INFO("%s[%d]: Environment variable expansion missing }",
848 * Can't really happen because input lines are
849 * capped at 8k, which is sizeof(name)
851 if ((size_t) (end - ptr) >= sizeof(name)) {
852 ERROR("%s[%d]: Environment variable name is too large",
857 memcpy(name, ptr, end - ptr);
858 name[end - ptr] = '\0';
861 * Get the environment variable.
862 * If none exists, then make it an empty string.
870 if (p + strlen(env) >= output + outsize) {
871 ERROR("%s[%d]: Reference \"%s\" is too long",
882 * Copy it over verbatim.
888 if (p >= (output + outsize)) {
889 ERROR("%s[%d]: Reference \"%s\" is too long",
893 } /* loop over all of the input string. */
900 static char const parse_spaces[] = " ";
902 /** Validation function for ipaddr conffile types
905 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
910 if (strcmp(value, "*") == 0) {
911 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
912 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
913 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
915 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
916 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
920 case PW_TYPE_IPV4_ADDR:
921 case PW_TYPE_IPV6_ADDR:
922 case PW_TYPE_IP_ADDR:
923 switch (ipaddr->af) {
925 if (ipaddr->prefix != 32) {
926 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
934 if (ipaddr->prefix != 128) {
935 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
951 * Parses an item (not a CONF_ITEM) into the specified format,
952 * with a default value.
954 * Returns -1 on error, -2 if deprecated, 0 for correctly parsed,
955 * and 1 if the default value was used. Note that the default
956 * value will be used ONLY if the CONF_PAIR is NULL.
958 int cf_item_parse(CONF_SECTION *cs, char const *name, int type, void *data, char const *dflt)
961 bool deprecated, required, attribute, secret;
964 CONF_PAIR const *cp = NULL;
970 deprecated = (type & PW_TYPE_DEPRECATED);
971 required = (type & PW_TYPE_REQUIRED);
972 attribute = (type & PW_TYPE_ATTRIBUTE);
973 secret = (type & PW_TYPE_SECRET);
975 type &= 0xff; /* normal types are small */
982 cp = cf_pair_find(cs, name);
992 cf_log_err(&(cs->item), "Configuration item '%s' must have a value", name);
998 if (!*value && required) {
1001 cf_log_err(&(cs->item), "Configuration item '%s' must not be empty", name);
1003 cf_log_err(&(cp->item), "Configuration item '%s' must not be empty", name);
1009 cf_log_err(&(cs->item), "Configuration item \"%s\" is deprecated", name);
1015 case PW_TYPE_BOOLEAN:
1017 * Allow yes/no and on/off
1019 if ((strcasecmp(value, "yes") == 0) ||
1020 (strcasecmp(value, "on") == 0)) {
1021 *(bool *)data = true;
1022 } else if ((strcasecmp(value, "no") == 0) ||
1023 (strcasecmp(value, "off") == 0)) {
1024 *(bool *)data = false;
1026 *(bool *)data = false;
1027 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1028 "variable %s", value, name);
1031 cf_log_info(cs, "%.*s\t%s = %s",
1032 cs->depth, parse_spaces, name, value);
1035 case PW_TYPE_INTEGER:
1037 unsigned long v = strtoul(value, 0, 0);
1040 * Restrict integer values to 0-INT32_MAX, this means
1041 * it will always be safe to cast them to a signed type
1042 * for comparisons, and imposes the same range limit as
1043 * before we switched to using an unsigned type to
1044 * represent config item integers.
1046 if (v > INT32_MAX) {
1047 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1052 *(uint32_t *)data = v;
1053 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1059 unsigned long v = strtoul(value, 0, 0);
1061 if (v > UINT16_MAX) {
1062 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1066 *(uint16_t *)data = (uint16_t) v;
1067 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1071 case PW_TYPE_INTEGER64:
1072 *(uint64_t *)data = strtoull(value, 0, 0);
1073 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1076 case PW_TYPE_SIGNED:
1077 *(int32_t *)data = strtol(value, 0, 0);
1078 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1081 case PW_TYPE_STRING:
1088 * Expand variables which haven't already been
1089 * expanded automagically when the configuration
1092 if (value == dflt) {
1095 lineno = cs->item.lineno;
1097 value = cf_expand_variables("<internal>",
1099 cs, buffer, sizeof(buffer),
1102 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1107 if (required && (!value || !*value)) goto is_required;
1110 if (!dict_attrbyname(value)) {
1112 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1115 cf_log_err(&(cp->item), "No such attribute '%s'",
1123 * Hide secrets when using "radiusd -X".
1125 if (secret && (debug_flag <= 2)) {
1126 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1127 cs->depth, parse_spaces, name);
1129 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1130 cs->depth, parse_spaces, name, value ? value : "(null)");
1132 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1136 * This is the same as PW_TYPE_STRING,
1137 * except that we also "stat" the file, and
1140 case PW_TYPE_FILE_INPUT:
1141 case PW_TYPE_FILE_OUTPUT:
1148 * Expand variables which haven't already been
1149 * expanded automagically when the configuration
1152 if ((value == dflt) && cs) {
1155 value = cf_expand_variables("?",
1157 cs, buffer, sizeof(buffer),
1159 if (!value) return -1;
1162 if (required && (!value || !*value)) goto is_required;
1164 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1165 cs->depth, parse_spaces, name, value);
1166 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1169 * If the filename exists and we're supposed to
1170 * read it, check it.
1172 if (*q && (type == PW_TYPE_FILE_INPUT)) {
1175 if (stat(*q, &buf) < 0) {
1176 ERROR("Unable to open file \"%s\": %s",
1177 value, fr_syserror(errno));
1183 case PW_TYPE_IPV4_ADDR:
1184 case PW_TYPE_IPV4_PREFIX:
1187 if (fr_pton4(ipaddr, value, 0, true, false) < 0) {
1188 ERROR("%s", fr_strerror());
1191 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1194 case PW_TYPE_IPV6_ADDR:
1195 case PW_TYPE_IPV6_PREFIX:
1198 if (fr_pton6(ipaddr, value, 0, true, false) < 0) {
1199 ERROR("%s", fr_strerror());
1202 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1205 case PW_TYPE_IP_ADDR:
1206 case PW_TYPE_IP_PREFIX:
1209 if (fr_pton(ipaddr, value, 0, true) < 0) {
1210 ERROR("%s", fr_strerror());
1213 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1216 case PW_TYPE_TIMEVAL: {
1221 sec = strtoul(value, &end, 10);
1225 sec = strlen(end + 1);
1228 ERROR("Too much precision for timeval");
1232 strcpy(buffer, "000000");
1233 memcpy(buffer, end + 1, sec);
1235 sec = strtoul(buffer, NULL, 10);
1238 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1239 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1240 memcpy(data, &tv, sizeof(tv));
1246 * If we get here, it's a sanity check error.
1247 * It's not an error parsing the configuration
1250 rad_assert(type > PW_TYPE_INVALID);
1251 rad_assert(type < PW_TYPE_MAX);
1253 ERROR("type '%s' is not supported in the configuration files",
1254 fr_int2str(dict_attr_types, type, "?Unknown?"));
1256 } /* switch over variable type */
1261 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD);
1262 if (!cpn) return -1;
1263 cpn->item.filename = "<internal>";
1264 cpn->item.lineno = 0;
1265 cf_item_add(cs, &(cpn->item));
1273 * A copy of cf_section_parse that initializes pointers before
1276 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1277 CONF_PARSER const *variables)
1282 for (i = 0; variables[i].name != NULL; i++) {
1283 if (variables[i].type == PW_TYPE_SUBSECTION) {
1284 CONF_SECTION *subcs;
1286 if (!variables[i].dflt) continue;
1288 subcs = cf_section_sub_find(cs, variables[i].name);
1291 * If there's no subsection in the
1292 * config, BUT the CONF_PARSER wants one,
1293 * then create an empty one. This is so
1294 * that we can track the strings,
1295 * etc. allocated in the subsection.
1298 subcs = cf_section_alloc(cs, variables[i].name,
1302 subcs->item.filename = cs->item.filename;
1303 subcs->item.lineno = cs->item.lineno;
1304 cf_item_add(cs, &(subcs->item));
1307 cf_section_parse_init(subcs, base,
1308 (CONF_PARSER const *) variables[i].dflt);
1312 if ((variables[i].type != PW_TYPE_STRING) &&
1313 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1314 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1318 if (variables[i].data) {
1319 data = variables[i].data; /* prefer this. */
1321 data = ((char *)base) + variables[i].offset;
1326 *(char **) data = NULL;
1327 } /* for all variables in the configuration section */
1332 * Parse a configuration section into user-supplied variables.
1334 int cf_section_parse(CONF_SECTION *cs, void *base,
1335 CONF_PARSER const *variables)
1341 cs->variables = variables; /* this doesn't hurt anything */
1344 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1347 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1348 cs->name1, cs->name2);
1351 cf_section_parse_init(cs, base, variables);
1354 * Handle the known configuration parameters.
1356 for (i = 0; variables[i].name != NULL; i++) {
1358 * Handle subsections specially
1360 if (variables[i].type == PW_TYPE_SUBSECTION) {
1361 CONF_SECTION *subcs;
1362 subcs = cf_section_sub_find(cs, variables[i].name);
1364 if (!variables[i].dflt || !subcs) {
1365 DEBUG2("Internal sanity check 1 failed in cf_section_parse %s",
1370 if (cf_section_parse(subcs, base,
1371 (CONF_PARSER const *) variables[i].dflt) < 0) {
1375 } /* else it's a CONF_PAIR */
1377 if (variables[i].data) {
1378 data = variables[i].data; /* prefer this. */
1380 data = ((char *)base) + variables[i].offset;
1382 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1387 * Parse the pair we found, or a default value.
1389 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1392 * Be nice, and print the name of the new config item.
1394 if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1395 (variables[i + 1].data == variables[i].data)) {
1396 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1397 variables[i + 1].name);
1402 } /* for all variables in the configuration section */
1404 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1411 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1416 static CONF_SECTION *cf_template_copy(CONF_SECTION *parent, CONF_SECTION const *template)
1421 cs = cf_section_alloc(parent, template->name1, template->name2);
1422 if (!cs) return NULL;
1424 for (ci = template->children; ci; ci = ci->next) {
1425 if (ci->type == CONF_ITEM_PAIR) {
1426 CONF_PAIR *cp1, *cp2;
1428 cp1 = cf_itemtopair(ci);
1429 cp2 = cf_pair_alloc(cs, cp1->attr, cp1->value, cp1->op, cp1->value_type);
1430 if (!cp2) return false;
1432 cp2->item.filename = cp1->item.filename;
1433 cp2->item.lineno = cp1->item.lineno;
1435 cf_item_add(cs, &(cp2->item));
1439 if (ci->type == CONF_ITEM_SECTION) {
1440 CONF_SECTION *subcs1, *subcs2;
1442 subcs1 = cf_itemtosection(ci);
1443 subcs2 = cf_template_copy(cs, subcs1);
1445 subcs2->item.filename = subcs1->item.filename;
1446 subcs2->item.lineno = subcs1->item.lineno;
1448 cf_item_add(cs, &(subcs2->item));
1452 /* ignore everything else */
1460 * Merge the template so everyting else "just works".
1462 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
1466 if (!cs || !template) return true;
1468 cs->template = NULL;
1471 * Walk over the template, adding its' entries to the
1472 * current section. But only if the entries don't
1473 * already exist in the current section.
1475 for (ci = template->children; ci; ci = ci->next) {
1476 if (ci->type == CONF_ITEM_PAIR) {
1477 CONF_PAIR *cp1, *cp2;
1480 * It exists, don't over-write it.
1482 cp1 = cf_itemtopair(ci);
1483 if (cf_pair_find(cs, cp1->attr)) {
1488 * Create a new pair with all of the data
1491 cp2 = cf_pair_alloc(cs, cp1->attr, cp1->value, cp1->op, cp1->value_type);
1492 if (!cp2) return false;
1494 cp2->item.filename = cp1->item.filename;
1495 cp2->item.lineno = cp1->item.lineno;
1497 cf_item_add(cs, &(cp2->item));
1501 if (ci->type == CONF_ITEM_SECTION) {
1502 CONF_SECTION *subcs1, *subcs2;
1504 subcs1 = cf_itemtosection(ci);
1505 rad_assert(subcs1 != NULL);
1507 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
1510 * sub-sections get merged.
1512 if (!cf_template_merge(subcs2, subcs1)) {
1519 * Our section doesn't have a matching
1520 * sub-section. Copy it verbatim from
1523 subcs2 = cf_template_copy(cs, subcs1);
1524 if (!subcs2) return false;
1526 subcs2->item.filename = subcs1->item.filename;
1527 subcs2->item.lineno = subcs1->item.lineno;
1529 cf_item_add(cs, &(subcs2->item));
1533 /* ignore everything else */
1539 static char const *cf_local_file(char const *base, char const *filename,
1540 char *buffer, size_t bufsize)
1545 strlcpy(buffer, base, bufsize);
1547 p = strrchr(buffer, FR_DIR_SEP);
1548 if (!p) return filename;
1549 if (p[1]) { /* ./foo */
1553 dirsize = (p - buffer) + 1;
1555 if ((dirsize + strlen(filename)) >= bufsize) {
1559 strlcpy(p + 1, filename, bufsize - dirsize);
1564 static int seen_too_much(char const *filename, int lineno, char const *ptr)
1567 if (isspace(*ptr)) {
1572 if (*ptr == '#') return false;
1578 ERROR("%s[%d] Unexpected text %s. See \"man unlang\"",
1579 filename, lineno, ptr);
1588 * Read a part of the config file.
1590 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
1591 CONF_SECTION *current)
1594 CONF_SECTION *this, *css, *nextcs;
1596 char const *ptr, *start;
1602 FR_TOKEN t1, t2, t3;
1603 bool spaces = false;
1606 fr_cond_t *cond = NULL;
1608 this = current; /* add items here */
1611 * Read, checking for line continuations ('\\' at EOL)
1618 * Get data, and remember if we are at EOF.
1620 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1624 * We read the entire 8k worth of data: complain.
1625 * Note that we don't care if the last character
1626 * is \n: it's still forbidden. This means that
1627 * the maximum allowed length of text is 8k-1, which
1631 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
1632 ERROR("%s[%d]: Line too long",
1639 while (isspace((int) *ptr)) ptr++;
1642 memmove(cbuf, ptr, len - (ptr - cbuf));
1643 len -= (ptr - cbuf);
1648 * Not doing continuations: check for edge
1655 while (*ptr && isspace((int) *ptr)) ptr++;
1657 if (!*ptr || (*ptr == '#')) continue;
1659 } else if (at_eof || (len == 0)) {
1660 ERROR("%s[%d]: Continuation at EOF is illegal",
1666 * See if there's a continuation.
1669 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
1674 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1676 * Check for "suppress spaces" magic.
1678 if (!spaces && (len > 2) && (cbuf[len - 2] == '"')) {
1682 cbuf[len - 1] = '\0';
1691 * The parser is getting to be evil.
1693 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
1695 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
1699 if (ptr[0] == '%') {
1700 hack = rad_copy_variable(buf1, ptr);
1702 hack = rad_copy_string(buf1, ptr);
1705 ERROR("%s[%d]: Invalid expansion: %s",
1706 filename, *lineno, ptr);
1712 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
1719 ERROR("%s[%d]: Invalid expansion: %s",
1720 filename, *lineno, ptr);
1724 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
1728 * The caller eats "name1 name2 {", and calls us
1729 * for the data inside of the section. So if we
1730 * receive a closing brace, then it must mean the
1731 * end of the section.
1733 if (t1 == T_RCBRACE) {
1734 if (this == current) {
1735 ERROR("%s[%d]: Too many closing braces",
1741 * Merge the template into the existing
1742 * section. This uses more memory, but
1743 * means that templates now work with
1744 * sub-sections, etc.
1746 if (!cf_template_merge(this, this->template)) {
1750 this = this->item.parent;
1751 if (seen_too_much(filename, *lineno, ptr)) return -1;
1756 * Allow for $INCLUDE files
1758 * This *SHOULD* work for any level include.
1759 * I really really really hate this file. -cparker
1761 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
1762 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
1763 bool relative = true;
1765 t2 = getword(&ptr, buf2, sizeof(buf2), true);
1767 ERROR("%s[%d]: Unexpected text after $INCLUDE",
1772 if (buf2[0] == '$') relative = false;
1774 value = cf_expand_variables(filename, lineno, this, buf, sizeof(buf), buf2);
1775 if (!value) return -1;
1777 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
1780 value = cf_local_file(filename, value, buf3,
1783 ERROR("%s[%d]: Directories too deep.",
1790 #ifdef HAVE_DIRENT_H
1794 * Include ALL non-"dot" files in the directory.
1797 if (value[strlen(value) - 1] == '/') {
1800 struct stat stat_buf;
1802 DEBUG2("including files in directory %s", value );
1807 if (stat(value, &stat_buf) < 0) {
1808 ERROR("%s[%d]: Failed reading directory %s: %s",
1810 value, fr_syserror(errno));
1814 if ((stat_buf.st_mode & S_IWOTH) != 0) {
1815 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to insecure configuration.",
1816 filename, *lineno, value);
1820 dir = opendir(value);
1822 ERROR("%s[%d]: Error reading directory %s: %s",
1823 filename, *lineno, value,
1824 fr_syserror(errno));
1829 * Read the directory, ignoring "." files.
1831 while ((dp = readdir(dir)) != NULL) {
1834 if (dp->d_name[0] == '.') continue;
1837 * Check for valid characters
1839 for (p = dp->d_name; *p != '\0'; p++) {
1840 if (isalpha((int)*p) ||
1844 (*p == '.')) continue;
1847 if (*p != '\0') continue;
1849 snprintf(buf2, sizeof(buf2), "%s%s",
1851 if ((stat(buf2, &stat_buf) != 0) ||
1852 S_ISDIR(stat_buf.st_mode)) continue;
1854 * Read the file into the current
1855 * configuration section.
1857 if (cf_file_include(this, buf2) < 0) {
1865 { /* it was a normal file */
1866 if (buf1[1] == '-') {
1867 struct stat statbuf;
1869 if (stat(value, &statbuf) < 0) {
1870 WARN("Not including file %s: %s", value, fr_syserror(errno));
1875 if (cf_file_include(this, value) < 0) {
1880 } /* we were in an include */
1882 if (strcasecmp(buf1, "$template") == 0) {
1884 CONF_SECTION *parentcs, *templatecs;
1885 t2 = getword(&ptr, buf2, sizeof(buf2), true);
1888 ERROR("%s[%d]: Unexpected text after $TEMPLATE",
1893 parentcs = cf_top_section(current);
1895 templatecs = cf_section_sub_find(parentcs, "templates");
1897 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"",
1898 filename, *lineno, buf2);
1902 ci = cf_reference_item(parentcs, templatecs, buf2);
1903 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1904 ERROR("%s[%d]: Reference \"%s\" not found",
1905 filename, *lineno, buf2);
1910 ERROR("%s[%d]: Internal sanity check error in template reference",
1915 if (this->template) {
1916 ERROR("%s[%d]: Section already has a template",
1921 this->template = cf_itemtosection(ci);
1926 * Ensure that the user can't add CONF_PAIRs
1927 * with 'internal' names;
1929 if (buf1[0] == '_') {
1930 ERROR("%s[%d]: Illegal configuration pair name \"%s\"",
1931 filename, *lineno, buf1);
1936 * Handle if/elsif specially.
1938 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
1940 char const *error = NULL;
1942 CONF_SECTION *server;
1945 * if / elsif MUST be inside of a
1946 * processing section, which MUST in turn
1947 * be inside of a "server" directive.
1949 if (!this->item.parent) {
1951 ERROR("%s[%d]: Invalid location for '%s'",
1952 filename, *lineno, buf1);
1957 * If there's a ${...}. If so, expand it.
1960 if (strchr(ptr, '$') != NULL) {
1962 ptr = cf_expand_variables(filename, lineno,
1967 ERROR("%s[%d]: Parse error expanding ${...} in condition",
1971 } /* else leave it alone */
1973 p = strrchr(ptr, '{'); /* ugh */
1976 server = this->item.parent;
1977 while ((strcmp(server->name1, "server") != 0) &&
1978 (strcmp(server->name1, "policy") != 0)) {
1979 server = server->item.parent;
1980 if (!server) goto invalid_location;
1983 nextcs = cf_section_alloc(this, buf1, ptr);
1985 ERROR("%s[%d]: Failed allocating memory for section",
1989 nextcs->item.filename = talloc_strdup(nextcs, filename);
1990 nextcs->item.lineno = *lineno;
1992 slen = fr_condition_tokenize(nextcs, cf_sectiontoitem(nextcs), ptr, &cond, &error, FR_COND_TWO_PASS);
2000 offset += ptr - start;
2002 spbuf = malloc(offset + 1);
2003 memset(spbuf, ' ', offset);
2004 spbuf[offset] = '\0';
2006 ERROR("%s[%d]: Parse error in condition",
2010 ERROR("%.*s^ %s", (int) offset, spbuf, error);
2011 talloc_free(nextcs);
2016 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2017 talloc_free(nextcs);
2018 ERROR("%s[%d]: Condition is too large after \"%s\"",
2019 filename, *lineno, buf1);
2023 memcpy(buf2, ptr, slen);
2028 if (gettoken(&ptr, buf3, sizeof(buf3), true) != T_LCBRACE) {
2029 talloc_free(nextcs);
2030 ERROR("%s[%d]: Expected '{'",
2036 * Swap the condition with trailing stuff for
2037 * the final condition.
2039 memcpy(&p, &nextcs->name2, sizeof(nextcs->name2));
2041 nextcs->name2 = talloc_typed_strdup(nextcs, buf2);
2047 * Grab the next token.
2049 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2065 case T_OP_CMP_FALSE:
2066 if (!this || (strcmp(this->name1, "update") != 0)) {
2067 ERROR("%s[%d]: Invalid operator in assignment",
2075 t3 = getstring(&ptr, buf3, sizeof(buf3), true);
2076 if (t3 == T_OP_INVALID) {
2077 ERROR("%s[%d]: Parse error: %s",
2084 * These are not allowed. Print a
2085 * helpful error message.
2087 if ((t3 == T_BACK_QUOTED_STRING) &&
2088 (!this || (strcmp(this->name1, "update") != 0))) {
2089 ERROR("%s[%d]: Syntax error: Invalid string `...` in assignment",
2095 * Handle variable substitution via ${foo}
2099 case T_DOUBLE_QUOTED_STRING:
2100 case T_BACK_QUOTED_STRING:
2101 value = cf_expand_variables(filename, lineno, this, buf, sizeof(buf), buf3);
2102 if (!value) return -1;
2115 * Add this CONF_PAIR to our CONF_SECTION
2118 cpn = cf_pair_alloc(this, buf1, value, t2, t3);
2119 if (!cpn) return -1;
2120 cpn->item.filename = talloc_strdup(cpn, filename);
2121 cpn->item.lineno = *lineno;
2122 cf_item_add(this, &(cpn->item));
2126 * No '=', must be a section or sub-section.
2129 case T_DOUBLE_QUOTED_STRING:
2130 case T_SINGLE_QUOTED_STRING:
2131 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2132 if (t3 != T_LCBRACE) {
2133 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2134 filename, *lineno, buf1, buf2);
2141 if (seen_too_much(filename, *lineno, ptr)) {
2142 if (cond) talloc_free(cond);
2147 css = cf_section_alloc(this, buf1,
2148 t2 == T_LCBRACE ? NULL : buf2);
2150 ERROR("%s[%d]: Failed allocating memory for section",
2155 css->item.filename = talloc_strdup(css, filename);
2156 css->item.lineno = *lineno;
2157 cf_item_add(this, &(css->item));
2163 cf_item_add(this, &(css->item));
2164 cf_data_add_internal(css, "if", cond, NULL, false);
2165 cond = NULL; /* eaten by the above line */
2169 * There may not be a name2
2171 css->name2_type = (t2 == T_LCBRACE) ? T_OP_INVALID : t2;
2174 * The current section is now the child section.
2180 ERROR("%s[%d]: Syntax error in '%s': %s",
2181 filename, *lineno, ptr, fr_strerror());
2185 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2186 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2192 * See if EOF was unexpected ..
2194 if (feof(fp) && (this != current)) {
2195 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2197 cf_section_name1(this), cf_section_lineno(this));
2205 * Include one config file in another.
2207 int cf_file_include(CONF_SECTION *cs, char const *filename)
2211 struct stat statbuf;
2215 DEBUG2("including configuration file %s", filename);
2217 fp = fopen(filename, "r");
2219 ERROR("Unable to open file \"%s\": %s",
2220 filename, fr_syserror(errno));
2224 if (stat(filename, &statbuf) == 0) {
2226 if ((statbuf.st_mode & S_IWOTH) != 0) {
2228 ERROR("Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
2235 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
2237 ERROR("Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
2244 if (cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT)) {
2246 ERROR("Cannot include the same file twice: \"%s\"",
2252 * Add the filename to the section
2254 mtime = talloc(cs, time_t);
2255 *mtime = statbuf.st_mtime;
2257 if (cf_data_add_internal(cs, filename, mtime, NULL, PW_TYPE_FILE_INPUT) < 0) {
2259 ERROR("Internal error opening file \"%s\"",
2264 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT);
2267 ERROR("Internal error opening file \"%s\"",
2272 if (!cs->item.filename) cs->item.filename = talloc_strdup(cs, filename);
2275 * Read the section. It's OK to have EOF without a
2276 * matching close brace.
2278 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
2288 * Bootstrap a config file.
2290 CONF_SECTION *cf_file_read(char const *filename)
2296 cs = cf_section_alloc(NULL, "main", NULL);
2297 if (!cs) return NULL;
2299 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD);
2300 if (!cp) return NULL;
2302 p = strrchr(cp->value, FR_DIR_SEP);
2305 cp->item.filename = "internal";
2306 cp->item.lineno = -1;
2307 cf_item_add(cs, &(cp->item));
2309 if (cf_file_include(cs, filename) < 0) {
2318 void cf_file_free(CONF_SECTION *cs)
2325 * Return a CONF_PAIR within a CONF_SECTION.
2327 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
2329 CONF_PAIR *cp, mycp;
2331 if (!cs || !name) return NULL;
2334 cp = rbtree_finddata(cs->pair_tree, &mycp);
2337 if (!cs->template) return NULL;
2339 return rbtree_finddata(cs->template->pair_tree, &mycp);
2343 * Return the attr of a CONF_PAIR
2346 char const *cf_pair_attr(CONF_PAIR const *pair)
2348 return (pair ? pair->attr : NULL);
2352 * Return the value of a CONF_PAIR
2355 char const *cf_pair_value(CONF_PAIR const *pair)
2357 return (pair ? pair->value : NULL);
2360 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
2362 return (pair ? pair->op : T_OP_INVALID);
2366 * Return the value type, should be one of the following:
2367 * T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2368 * T_DOUBLE_QUOTED_STRING or T_OP_INVALID if the pair is NULL.
2370 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
2372 return (pair ? pair->value_type : T_OP_INVALID);
2376 * Turn a CONF_PAIR into a VALUE_PAIR
2377 * For now, ignore the "value_type" field...
2379 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
2382 fr_strerror_printf("Internal error");
2387 fr_strerror_printf("No value given for attribute %s", pair->attr);
2392 * false comparisons never match. BUT if it's a "string"
2393 * or `string`, then remember to expand it later.
2395 if ((pair->op != T_OP_CMP_FALSE) &&
2396 ((pair->value_type == T_DOUBLE_QUOTED_STRING) ||
2397 (pair->value_type == T_BACK_QUOTED_STRING))) {
2400 vp = pairmake(pair, NULL, pair->attr, NULL, pair->op);
2405 if (pairmark_xlat(vp, pair->value) < 0) {
2414 return pairmake(pair, NULL, pair->attr, pair->value, pair->op);
2418 * Return the first label of a CONF_SECTION
2421 char const *cf_section_name1(CONF_SECTION const *cs)
2423 return (cs ? cs->name1 : NULL);
2427 * Return the second label of a CONF_SECTION
2430 char const *cf_section_name2(CONF_SECTION const *cs)
2432 return (cs ? cs->name2 : NULL);
2436 * Find a value in a CONF_SECTION
2438 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
2442 cp = cf_pair_find(cs, attr);
2444 return (cp ? cp->value : NULL);
2448 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
2449 char const *name1, char const *name2)
2452 CONF_ITEM const *ci;
2454 if (!cs || !name1) return NULL;
2456 for (ci = &(cs->item); ci; ci = ci->next) {
2457 if (ci->type != CONF_ITEM_SECTION)
2460 if (strcmp(cf_itemtosection(ci)->name1, name1) != 0) {
2464 their2 = cf_itemtosection(ci)->name2;
2466 if ((!name2 && !their2) ||
2467 (name2 && their2 && (strcmp(name2, their2) == 0))) {
2468 return cf_itemtosection(ci);
2476 * Return the next pair after a CONF_PAIR
2477 * with a certain name (char *attr) If the requested
2478 * attr is NULL, any attr matches.
2481 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
2482 CONF_PAIR const *pair, char const *attr)
2486 if (!cs) return NULL;
2489 * If pair is NULL this must be a first time run
2490 * Find the pair with correct name
2493 if (!pair) return cf_pair_find(cs, attr);
2495 for (ci = pair->item.next; ci; ci = ci->next) {
2496 if (ci->type != CONF_ITEM_PAIR)
2499 if (!attr || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
2503 return cf_itemtopair(ci);
2507 * Find a CONF_SECTION, or return the root if name is NULL
2510 CONF_SECTION *cf_section_find(char const *name)
2513 return cf_section_sub_find(root_config, name);
2518 /** Find a sub-section in a section
2520 * This finds ANY section having the same first name.
2521 * The second name is ignored.
2523 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
2527 if (!cs || !name) return NULL; /* can't find an un-named section */
2530 * No sub-sections have been defined, so none exist.
2532 if (!cs->section_tree) return NULL;
2536 return rbtree_finddata(cs->section_tree, &mycs);
2540 /** Find a CONF_SECTION with both names.
2543 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
2544 char const *name1, char const *name2)
2548 if (!cs) cs = root_config;
2549 if (!cs) return NULL;
2552 CONF_SECTION mycs, *master_cs;
2554 if (!cs->section_tree) return NULL;
2559 master_cs = rbtree_finddata(cs->section_tree, &mycs);
2560 if (!master_cs) return NULL;
2563 * Look it up in the name2 tree. If it's there,
2566 if (master_cs->name2_tree) {
2567 CONF_SECTION *subcs;
2569 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
2570 if (subcs) return subcs;
2574 * We don't insert ourselves into the name2 tree.
2575 * So if there's nothing in the name2 tree, maybe
2576 * *we* are the answer.
2578 if (!master_cs->name2 && name2) return NULL;
2579 if (master_cs->name2 && !name2) return NULL;
2580 if (!master_cs->name2 && !name2) return master_cs;
2582 if (strcmp(master_cs->name2, name2) == 0) {
2590 * Else do it the old-fashioned way.
2592 for (ci = cs->children; ci; ci = ci->next) {
2593 CONF_SECTION *subcs;
2595 if (ci->type != CONF_ITEM_SECTION)
2598 subcs = cf_itemtosection(ci);
2599 if (!subcs->name2) {
2600 if (strcmp(subcs->name1, name2) == 0) break;
2602 if (strcmp(subcs->name2, name2) == 0) break;
2606 return cf_itemtosection(ci);
2610 * Return the next subsection after a CONF_SECTION
2611 * with a certain name1 (char *name1). If the requested
2612 * name1 is NULL, any name1 matches.
2615 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
2616 CONF_SECTION const *subsection,
2621 if (!section) return NULL;
2624 * If subsection is NULL this must be a first time run
2625 * Find the subsection with correct name
2629 ci = section->children;
2631 ci = subsection->item.next;
2634 for (; ci; ci = ci->next) {
2635 if (ci->type != CONF_ITEM_SECTION)
2637 if ((name1 == NULL) ||
2638 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
2642 return cf_itemtosection(ci);
2647 * Return the next section after a CONF_SECTION
2648 * with a certain name1 (char *name1). If the requested
2649 * name1 is NULL, any name1 matches.
2652 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
2653 CONF_SECTION const *subsection,
2656 if (!section) return NULL;
2658 if (!section->item.parent) return NULL;
2660 return cf_subsection_find_next(section->item.parent, subsection, name1);
2664 * Return the next item after a CONF_ITEM.
2667 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
2669 if (!section) return NULL;
2672 * If item is NULL this must be a first time run
2673 * Return the first item
2677 return section->children;
2683 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
2685 if (!ci) return NULL;
2690 int cf_section_lineno(CONF_SECTION const *section)
2692 return section->item.lineno;
2695 char const *cf_pair_filename(CONF_PAIR const *pair)
2697 return pair->item.filename;
2700 char const *cf_section_filename(CONF_SECTION const *section)
2702 return section->item.filename;
2705 int cf_pair_lineno(CONF_PAIR const *pair)
2707 return pair->item.lineno;
2710 int cf_item_is_section(CONF_ITEM const *item)
2712 return item->type == CONF_ITEM_SECTION;
2714 int cf_item_is_pair(CONF_ITEM const *item)
2716 return item->type == CONF_ITEM_PAIR;
2720 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
2721 void *data, void (*data_free)(void *))
2725 cd = talloc_zero(parent, CONF_DATA);
2726 if (!cd) return NULL;
2728 cd->item.type = CONF_ITEM_DATA;
2729 cd->item.parent = parent;
2730 cd->name = talloc_typed_strdup(cd, name);
2737 cd->free = data_free;
2740 talloc_set_destructor(cd, _cf_data_free);
2747 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name,
2750 if (!cs || !name) return NULL;
2753 * Find the name in the tree, for speed.
2755 if (cs->data_tree) {
2760 return rbtree_finddata(cs->data_tree, &mycd);
2767 * Find data from a particular section.
2769 void *cf_data_find(CONF_SECTION const *cs, char const *name)
2771 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
2773 if (cd) return cd->data;
2779 * Add named data to a configuration section.
2781 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
2782 void *data, void (*data_free)(void *),
2787 if (!cs || !name) return -1;
2790 * Already exists. Can't add it.
2792 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
2794 cd = cf_data_alloc(cs, name, data, data_free);
2798 cf_item_add(cs, cf_datatoitem(cd));
2804 * Add named data to a configuration section.
2806 int cf_data_add(CONF_SECTION *cs, char const *name,
2807 void *data, void (*data_free)(void *))
2809 return cf_data_add_internal(cs, name, data, data_free, 0);
2813 * This is here to make the rest of the code easier to read. It
2814 * ties conffile.c to log.c, but it means we don't have to
2815 * pollute every other function with the knowledge of the
2816 * configuration internals.
2818 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
2824 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2829 ci->filename ? ci->filename : "unknown",
2830 ci->lineno ? ci->lineno : 0,
2833 ERROR("<unknown>[*]: %s", buffer);
2837 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
2843 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2846 rad_assert(cs != NULL);
2849 cs->item.filename ? cs->item.filename : "unknown",
2850 cs->item.lineno ? cs->item.lineno : 0,
2854 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
2860 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2863 rad_assert(cp != NULL);
2866 cp->item.filename ? cp->item.filename : "unknown",
2867 cp->item.lineno ? cp->item.lineno : 0,
2871 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
2876 if ((debug_flag > 1) && cs) vradlog(L_DBG, fmt, ap);
2881 * Wrapper to simplify the code.
2883 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
2889 if (debug_flag > 1 && cs) {
2890 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2892 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
2897 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
2899 if (!cs) return NULL;
2901 return cs->variables;
2905 * For "switch" and "case" statements.
2907 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
2909 if (!cs) return T_OP_INVALID;
2911 return cs->name2_type;