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(void *ctx)
221 cd = talloc_get_type_abort(ctx, CONF_DATA);
232 * rbtree callback function
234 static int pair_cmp(void const *a, void const *b)
236 CONF_PAIR const *one = a;
237 CONF_PAIR const *two = b;
239 return strcmp(one->attr, two->attr);
244 * rbtree callback function
246 static int section_cmp(void const *a, void const *b)
248 CONF_SECTION const *one = a;
249 CONF_SECTION const *two = b;
251 return strcmp(one->name1, two->name1);
256 * rbtree callback function
258 static int name2_cmp(void const *a, void const *b)
260 CONF_SECTION const *one = a;
261 CONF_SECTION const *two = b;
263 rad_assert(strcmp(one->name1, two->name1) == 0);
265 if (!one->name2 && !two->name2) return 0;
266 if (one->name2 && !two->name2) return -1;
267 if (!one->name2 && two->name2) return +1;
269 return strcmp(one->name2, two->name2);
274 * rbtree callback function
276 static int data_cmp(void const *a, void const *b)
280 CONF_DATA const *one = a;
281 CONF_DATA const *two = b;
283 rcode = one->flag - two->flag;
284 if (rcode != 0) return rcode;
286 return strcmp(one->name, two->name);
289 static int cf_section_free(void *ctx)
293 cs = talloc_get_type_abort(ctx, CONF_SECTION);
296 * Name1 and name2 are allocated contiguous with
300 rbtree_free(cs->pair_tree);
301 cs->pair_tree = NULL;
303 if (cs->section_tree) {
304 rbtree_free(cs->section_tree);
305 cs->section_tree = NULL;
307 if (cs->name2_tree) {
308 rbtree_free(cs->name2_tree);
309 cs->name2_tree = NULL;
312 rbtree_free(cs->data_tree);
313 cs->data_tree = NULL;
321 * Allocate a CONF_SECTION
323 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1,
329 if (!name1) return NULL;
332 if (strchr(name2, '$')) {
333 name2 = cf_expand_variables(parent->item.filename,
334 &parent->item.lineno,
336 buffer, sizeof(buffer), name2);
338 ERROR("Failed expanding section name");
344 cs = talloc_zero(parent, CONF_SECTION);
345 if (!cs) return NULL;
347 cs->item.type = CONF_ITEM_SECTION;
348 cs->item.parent = parent;
350 cs->name1 = talloc_typed_strdup(cs, name1);
357 if (name2 && *name2) {
358 cs->name2 = talloc_typed_strdup(cs, name2);
359 if (!cs->name2) goto error;
362 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
363 if (!cs->pair_tree) goto error;
365 talloc_set_destructor((void *) cs, cf_section_free);
368 * Don't create a data tree, it may not be needed.
372 * Don't create the section tree here, it may not
376 if (parent) cs->depth = parent->depth + 1;
382 * Replace pair in a given section with a new pair,
383 * of the given value.
385 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
388 CONF_ITEM *ci, *cn, **last;
390 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->value_type);
391 if (!newp) return -1;
397 * Find the old one from the linked list, and replace it
400 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
402 cn->next = (*last)->next;
409 rbtree_deletebydata(cs->pair_tree, ci);
411 rbtree_insert(cs->pair_tree, cn);
418 * Add an item to a configuration section.
420 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
422 if (!cs || !ci) return;
425 rad_assert(cs->tail == NULL);
428 rad_assert(cs->tail != NULL);
433 * Update the trees (and tail) for each item added.
435 for (/* nothing */; ci != NULL; ci = ci->next) {
439 * For fast lookups, pairs and sections get
444 if (!rbtree_insert(cs->pair_tree, ci)) {
445 CONF_PAIR *cp = cf_itemtopair(ci);
447 if (strcmp(cp->attr, "confdir") == 0) break;
448 if (!cp->value) break; /* module name, "ok", etc. */
452 case CONF_ITEM_SECTION: {
453 CONF_SECTION *cs_new = cf_itemtosection(ci);
454 CONF_SECTION *name1_cs;
456 if (!cs->section_tree) {
457 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
458 if (!cs->section_tree) {
459 ERROR("Out of memory");
464 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
466 if (!rbtree_insert(cs->section_tree, cs_new)) {
467 ERROR("Failed inserting section into tree");
475 * We'll ignore these checks for
476 * now. Various sections can be
477 * duplicated, such as "listen",
478 * "update", "if", "else", etc.
480 if (!name1_cs->name2 && !cs_new->name2) {
481 WARN("%s[%d] Duplicate configuration section \"%s { ...}\" %s %d",
482 ci->filename, ci->lineno, cs_new->name1, name1_cs->item.filename, name1_cs->item.lineno);
486 if ((name1_cs->name2 && cs_new->name2) &&
487 (strcmp(name1_cs->name2, cs_new->name2) == 0)) {
488 WARN("%s[%d] Duplicate configuration section \"%s %s { ...}\"",
489 ci->filename, ci->lineno, cs_new->name1, cs_new->name2);
495 * We already have a section of
496 * this "name1". Add a new
497 * sub-section based on name2.
499 if (!name1_cs->name2_tree) {
500 name1_cs->name2_tree = rbtree_create(name2_cmp,
502 if (!name1_cs->name2_tree) {
503 ERROR("Out of memory");
509 * We don't care if this fails.
510 * If the user tries to create
511 * two sections of the same
512 * name1/name2, the duplicate
513 * section is just silently
516 rbtree_insert(name1_cs->name2_tree, cs_new);
518 } /* was a section */
521 if (!cs->data_tree) {
522 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
525 rbtree_insert(cs->data_tree, ci);
529 default: /* FIXME: assert & error! */
532 } /* switch over conf types */
537 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
538 CONF_SECTION *outercs,
543 CONF_SECTION const *cs = outercs;
547 strlcpy(name, ptr, sizeof(name));
551 * ".foo" means "foo from the current section"
557 * Just '.' means the current section
560 return cf_sectiontoitem(cs);
564 * ..foo means "foo from the section
565 * enclosing this section" (etc.)
568 if (cs->item.parent) {
569 cs = cs->item.parent;
573 * .. means the section
574 * enclosing this section
577 return cf_sectiontoitem(cs);
582 * "foo.bar.baz" means "from the root"
584 } else if (strchr(p, '.') != NULL) {
585 if (!parentcs) goto no_such_item;
597 if (r && q > r) q = NULL;
598 if (q && q < r) r = NULL;
604 q = strchr(r + 1, ']');
605 if (!q) return NULL; /* parse error */
608 * Points to foo[bar]xx: parse error,
609 * it should be foo[bar] or foo[bar].baz
611 if (q[1] && q[1] != '.') goto no_such_item;
615 next = cf_section_sub_find_name2(cs, p, r + 1);
620 * Points to a named instance of a section.
623 if (!next) goto no_such_item;
624 return &(next->item);
627 q++; /* ensure we skip the ']' and '.' */
631 next = cf_section_sub_find(cs, p);
635 if (!next) break; /* it MAY be a pair in this section! */
641 if (!*p) goto no_such_item;
645 * Find it in the current referenced
648 cp = cf_pair_find(cs, p);
649 if (cp) return &(cp->item);
651 next = cf_section_sub_find(cs, p);
652 if (next) return &(next->item);
655 * "foo" is "in the current section, OR in main".
657 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
663 WARN("No such configuration item %s", ptr);
668 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
670 if (!cs) return NULL;
672 while (cs->item.parent != NULL) {
673 cs = cs->item.parent;
681 * Expand the variables in an input string.
683 static char const *cf_expand_variables(char const *cf, int *lineno,
684 CONF_SECTION *outercs,
685 char *output, size_t outsize,
689 char const *end, *ptr;
690 CONF_SECTION const *parentcs;
694 * Find the master parent conf section.
695 * We can't use main_config.config, because we're in the
696 * process of re-building it, and it isn't set up yet...
698 parentcs = cf_top_section(outercs);
704 * Ignore anything other than "${"
706 if ((*ptr == '$') && (ptr[1] == '{')) {
712 * FIXME: Add support for ${foo:-bar},
717 * Look for trailing '}', and log a
718 * warning for anything that doesn't match,
719 * and exit with a fatal error.
721 end = strchr(ptr, '}');
724 INFO("%s[%d]: Variable expansion missing }",
732 * Can't really happen because input lines are
733 * capped at 8k, which is sizeof(name)
735 if ((size_t) (end - ptr) >= sizeof(name)) {
736 ERROR("%s[%d]: Reference string is too large",
741 memcpy(name, ptr, end - ptr);
742 name[end - ptr] = '\0';
744 q = strchr(name, ':');
749 ci = cf_reference_item(parentcs, outercs, name);
751 ERROR("%s[%d]: Reference \"%s\" not found", cf, *lineno, input);
756 * The expansion doesn't refer to another item or section
757 * it's the property of a section.
760 CONF_SECTION *mycs = cf_itemtosection(ci);
762 if (ci->type != CONF_ITEM_SECTION) {
763 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
767 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
768 case CONF_PROPERTY_NAME:
769 strcpy(p, mycs->name1);
772 case CONF_PROPERTY_INSTANCE:
773 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
777 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
783 } else if (ci->type == CONF_ITEM_PAIR) {
785 * Substitute the value of the variable.
787 cp = cf_itemtopair(ci);
789 ERROR("%s[%d]: Reference \"%s\" has no value",
794 if (p + strlen(cp->value) >= output + outsize) {
795 ERROR("%s[%d]: Reference \"%s\" is too long",
800 strcpy(p, cp->value);
804 } else if (ci->type == CONF_ITEM_SECTION) {
808 * Adding an entry again to a
809 * section is wrong. We don't
810 * want an infinite loop.
812 if (ci->parent == outercs) {
813 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
818 * Copy the section instead of
821 subcs = cf_template_copy(outercs, cf_itemtosection(ci));
823 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
827 subcs->item.filename = ci->filename;
828 subcs->item.lineno = ci->lineno;
829 cf_item_add(outercs, &(subcs->item));
834 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
837 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
843 * Look for trailing '}', and log a
844 * warning for anything that doesn't match,
845 * and exit with a fatal error.
847 end = strchr(ptr, '}');
850 INFO("%s[%d]: Environment variable expansion missing }",
856 * Can't really happen because input lines are
857 * capped at 8k, which is sizeof(name)
859 if ((size_t) (end - ptr) >= sizeof(name)) {
860 ERROR("%s[%d]: Environment variable name is too large",
865 memcpy(name, ptr, end - ptr);
866 name[end - ptr] = '\0';
869 * Get the environment variable.
870 * If none exists, then make it an empty string.
878 if (p + strlen(env) >= output + outsize) {
879 ERROR("%s[%d]: Reference \"%s\" is too long",
890 * Copy it over verbatim.
896 if (p >= (output + outsize)) {
897 ERROR("%s[%d]: Reference \"%s\" is too long",
901 } /* loop over all of the input string. */
908 static char const parse_spaces[] = " ";
910 /** Validation function for ipaddr conffile types
913 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
918 if (strcmp(value, "*") == 0) {
919 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
920 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
921 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
923 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
924 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
928 case PW_TYPE_IPV4_ADDR:
929 case PW_TYPE_IPV6_ADDR:
930 case PW_TYPE_IP_ADDR:
931 switch (ipaddr->af) {
933 if (ipaddr->prefix != 32) {
934 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
942 if (ipaddr->prefix != 128) {
943 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
959 * Parses an item (not a CONF_ITEM) into the specified format,
960 * with a default value.
962 * Returns -1 on error, -2 if deprecated, 0 for correctly parsed,
963 * and 1 if the default value was used. Note that the default
964 * value will be used ONLY if the CONF_PAIR is NULL.
966 int cf_item_parse(CONF_SECTION *cs, char const *name, int type, void *data, char const *dflt)
969 bool deprecated, required, attribute, secret;
972 CONF_PAIR const *cp = NULL;
978 deprecated = (type & PW_TYPE_DEPRECATED);
979 required = (type & PW_TYPE_REQUIRED);
980 attribute = (type & PW_TYPE_ATTRIBUTE);
981 secret = (type & PW_TYPE_SECRET);
983 type &= 0xff; /* normal types are small */
990 cp = cf_pair_find(cs, name);
1000 cf_log_err(&(cs->item), "Configuration item '%s' must have a value", name);
1006 if (!*value && required) {
1009 cf_log_err(&(cs->item), "Configuration item '%s' must not be empty", name);
1011 cf_log_err(&(cp->item), "Configuration item '%s' must not be empty", name);
1017 cf_log_err(&(cs->item), "Configuration item \"%s\" is deprecated", name);
1023 case PW_TYPE_BOOLEAN:
1025 * Allow yes/no and on/off
1027 if ((strcasecmp(value, "yes") == 0) ||
1028 (strcasecmp(value, "on") == 0)) {
1029 *(bool *)data = true;
1030 } else if ((strcasecmp(value, "no") == 0) ||
1031 (strcasecmp(value, "off") == 0)) {
1032 *(bool *)data = false;
1034 *(bool *)data = false;
1035 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1036 "variable %s", value, name);
1039 cf_log_info(cs, "%.*s\t%s = %s",
1040 cs->depth, parse_spaces, name, value);
1043 case PW_TYPE_INTEGER:
1045 unsigned long v = strtoul(value, 0, 0);
1048 * Restrict integer values to 0-INT32_MAX, this means
1049 * it will always be safe to cast them to a signed type
1050 * for comparisons, and imposes the same range limit as
1051 * before we switched to using an unsigned type to
1052 * represent config item integers.
1054 if (v > INT32_MAX) {
1055 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1060 *(uint32_t *)data = v;
1061 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1067 unsigned long v = strtoul(value, 0, 0);
1069 if (v > UINT16_MAX) {
1070 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1074 *(uint16_t *)data = (uint16_t) v;
1075 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1079 case PW_TYPE_INTEGER64:
1080 *(uint64_t *)data = strtoull(value, 0, 0);
1081 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1084 case PW_TYPE_SIGNED:
1085 *(int32_t *)data = strtol(value, 0, 0);
1086 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1089 case PW_TYPE_STRING:
1096 * Expand variables which haven't already been
1097 * expanded automagically when the configuration
1100 if (value == dflt) {
1103 lineno = cs->item.lineno;
1105 value = cf_expand_variables("<internal>",
1107 cs, buffer, sizeof(buffer),
1110 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1115 if (required && (!value || !*value)) goto is_required;
1118 if (!dict_attrbyname(value)) {
1120 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1123 cf_log_err(&(cp->item), "No such attribute '%s'",
1131 * Hide secrets when using "radiusd -X".
1133 if (secret && (debug_flag <= 2)) {
1134 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1135 cs->depth, parse_spaces, name);
1137 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1138 cs->depth, parse_spaces, name, value ? value : "(null)");
1140 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1144 * This is the same as PW_TYPE_STRING,
1145 * except that we also "stat" the file, and
1148 case PW_TYPE_FILE_INPUT:
1149 case PW_TYPE_FILE_OUTPUT:
1156 * Expand variables which haven't already been
1157 * expanded automagically when the configuration
1160 if ((value == dflt) && cs) {
1163 value = cf_expand_variables("?",
1165 cs, buffer, sizeof(buffer),
1167 if (!value) return -1;
1170 if (required && (!value || !*value)) goto is_required;
1172 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1173 cs->depth, parse_spaces, name, value);
1174 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1177 * If the filename exists and we're supposed to
1178 * read it, check it.
1180 if (*q && (type == PW_TYPE_FILE_INPUT)) {
1183 if (stat(*q, &buf) < 0) {
1184 ERROR("Unable to open file \"%s\": %s",
1185 value, fr_syserror(errno));
1191 case PW_TYPE_IPV4_ADDR:
1192 case PW_TYPE_IPV4_PREFIX:
1195 if (fr_pton4(ipaddr, value, 0, true, false) < 0) {
1196 ERROR("%s", fr_strerror());
1199 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1202 case PW_TYPE_IPV6_ADDR:
1203 case PW_TYPE_IPV6_PREFIX:
1206 if (fr_pton6(ipaddr, value, 0, true, false) < 0) {
1207 ERROR("%s", fr_strerror());
1210 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1213 case PW_TYPE_IP_ADDR:
1214 case PW_TYPE_IP_PREFIX:
1217 if (fr_pton(ipaddr, value, 0, true) < 0) {
1218 ERROR("%s", fr_strerror());
1221 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1224 case PW_TYPE_TIMEVAL: {
1229 sec = strtoul(value, &end, 10);
1233 sec = strlen(end + 1);
1236 ERROR("Too much precision for timeval");
1240 strcpy(buffer, "000000");
1241 memcpy(buffer, end + 1, sec);
1243 sec = strtoul(buffer, NULL, 10);
1246 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1247 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1248 memcpy(data, &tv, sizeof(tv));
1254 * If we get here, it's a sanity check error.
1255 * It's not an error parsing the configuration
1258 rad_assert(type > PW_TYPE_INVALID);
1259 rad_assert(type < PW_TYPE_MAX);
1261 ERROR("type '%s' is not supported in the configuration files",
1262 fr_int2str(dict_attr_types, type, "?Unknown?"));
1264 } /* switch over variable type */
1269 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD);
1270 if (!cpn) return -1;
1271 cpn->item.filename = "<internal>";
1272 cpn->item.lineno = 0;
1273 cf_item_add(cs, &(cpn->item));
1281 * A copy of cf_section_parse that initializes pointers before
1284 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1285 CONF_PARSER const *variables)
1290 for (i = 0; variables[i].name != NULL; i++) {
1291 if (variables[i].type == PW_TYPE_SUBSECTION) {
1292 CONF_SECTION *subcs;
1294 if (!variables[i].dflt) continue;
1296 subcs = cf_section_sub_find(cs, variables[i].name);
1299 * If there's no subsection in the
1300 * config, BUT the CONF_PARSER wants one,
1301 * then create an empty one. This is so
1302 * that we can track the strings,
1303 * etc. allocated in the subsection.
1306 subcs = cf_section_alloc(cs, variables[i].name,
1310 subcs->item.filename = cs->item.filename;
1311 subcs->item.lineno = cs->item.lineno;
1312 cf_item_add(cs, &(subcs->item));
1315 cf_section_parse_init(subcs, base,
1316 (CONF_PARSER const *) variables[i].dflt);
1320 if ((variables[i].type != PW_TYPE_STRING) &&
1321 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1322 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1326 if (variables[i].data) {
1327 data = variables[i].data; /* prefer this. */
1329 data = ((char *)base) + variables[i].offset;
1334 *(char **) data = NULL;
1335 } /* for all variables in the configuration section */
1340 * Parse a configuration section into user-supplied variables.
1342 int cf_section_parse(CONF_SECTION *cs, void *base,
1343 CONF_PARSER const *variables)
1349 cs->variables = variables; /* this doesn't hurt anything */
1352 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1355 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1356 cs->name1, cs->name2);
1359 cf_section_parse_init(cs, base, variables);
1362 * Handle the known configuration parameters.
1364 for (i = 0; variables[i].name != NULL; i++) {
1366 * Handle subsections specially
1368 if (variables[i].type == PW_TYPE_SUBSECTION) {
1369 CONF_SECTION *subcs;
1370 subcs = cf_section_sub_find(cs, variables[i].name);
1372 if (!variables[i].dflt || !subcs) {
1373 DEBUG2("Internal sanity check 1 failed in cf_section_parse %s",
1378 if (cf_section_parse(subcs, base,
1379 (CONF_PARSER const *) variables[i].dflt) < 0) {
1383 } /* else it's a CONF_PAIR */
1385 if (variables[i].data) {
1386 data = variables[i].data; /* prefer this. */
1388 data = ((char *)base) + variables[i].offset;
1390 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1395 * Parse the pair we found, or a default value.
1397 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1400 * Be nice, and print the name of the new config item.
1402 if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1403 (variables[i + 1].data == variables[i].data)) {
1404 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1405 variables[i + 1].name);
1410 } /* for all variables in the configuration section */
1412 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1419 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1424 static CONF_SECTION *cf_template_copy(CONF_SECTION *parent, CONF_SECTION const *template)
1429 cs = cf_section_alloc(parent, template->name1, template->name2);
1430 if (!cs) return NULL;
1432 for (ci = template->children; ci; ci = ci->next) {
1433 if (ci->type == CONF_ITEM_PAIR) {
1434 CONF_PAIR *cp1, *cp2;
1436 cp1 = cf_itemtopair(ci);
1437 cp2 = cf_pair_alloc(cs, cp1->attr, cp1->value, cp1->op, cp1->value_type);
1438 if (!cp2) return false;
1440 cp2->item.filename = cp1->item.filename;
1441 cp2->item.lineno = cp1->item.lineno;
1443 cf_item_add(cs, &(cp2->item));
1447 if (ci->type == CONF_ITEM_SECTION) {
1448 CONF_SECTION *subcs1, *subcs2;
1450 subcs1 = cf_itemtosection(ci);
1451 subcs2 = cf_template_copy(cs, subcs1);
1453 subcs2->item.filename = subcs1->item.filename;
1454 subcs2->item.lineno = subcs1->item.lineno;
1456 cf_item_add(cs, &(subcs2->item));
1460 /* ignore everything else */
1468 * Merge the template so everyting else "just works".
1470 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
1474 if (!cs || !template) return true;
1476 cs->template = NULL;
1479 * Walk over the template, adding its' entries to the
1480 * current section. But only if the entries don't
1481 * already exist in the current section.
1483 for (ci = template->children; ci; ci = ci->next) {
1484 if (ci->type == CONF_ITEM_PAIR) {
1485 CONF_PAIR *cp1, *cp2;
1488 * It exists, don't over-write it.
1490 cp1 = cf_itemtopair(ci);
1491 if (cf_pair_find(cs, cp1->attr)) {
1496 * Create a new pair with all of the data
1499 cp2 = cf_pair_alloc(cs, cp1->attr, cp1->value, cp1->op, cp1->value_type);
1500 if (!cp2) return false;
1502 cp2->item.filename = cp1->item.filename;
1503 cp2->item.lineno = cp1->item.lineno;
1505 cf_item_add(cs, &(cp2->item));
1509 if (ci->type == CONF_ITEM_SECTION) {
1510 CONF_SECTION *subcs1, *subcs2;
1512 subcs1 = cf_itemtosection(ci);
1513 rad_assert(subcs1 != NULL);
1515 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
1518 * sub-sections get merged.
1520 if (!cf_template_merge(subcs2, subcs1)) {
1527 * Our section doesn't have a matching
1528 * sub-section. Copy it verbatim from
1531 subcs2 = cf_template_copy(cs, subcs1);
1532 if (!subcs2) return false;
1534 subcs2->item.filename = subcs1->item.filename;
1535 subcs2->item.lineno = subcs1->item.lineno;
1537 cf_item_add(cs, &(subcs2->item));
1541 /* ignore everything else */
1547 static char const *cf_local_file(char const *base, char const *filename,
1548 char *buffer, size_t bufsize)
1553 strlcpy(buffer, base, bufsize);
1555 p = strrchr(buffer, FR_DIR_SEP);
1556 if (!p) return filename;
1557 if (p[1]) { /* ./foo */
1561 dirsize = (p - buffer) + 1;
1563 if ((dirsize + strlen(filename)) >= bufsize) {
1567 strlcpy(p + 1, filename, bufsize - dirsize);
1572 static int seen_too_much(char const *filename, int lineno, char const *ptr)
1575 if (isspace(*ptr)) {
1580 if (*ptr == '#') return false;
1586 ERROR("%s[%d] Unexpected text %s. See \"man unlang\"",
1587 filename, lineno, ptr);
1596 * Read a part of the config file.
1598 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
1599 CONF_SECTION *current)
1602 CONF_SECTION *this, *css, *nextcs;
1604 char const *ptr, *start;
1610 FR_TOKEN t1, t2, t3;
1611 bool spaces = false;
1614 fr_cond_t *cond = NULL;
1616 this = current; /* add items here */
1619 * Read, checking for line continuations ('\\' at EOL)
1626 * Get data, and remember if we are at EOF.
1628 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1632 * We read the entire 8k worth of data: complain.
1633 * Note that we don't care if the last character
1634 * is \n: it's still forbidden. This means that
1635 * the maximum allowed length of text is 8k-1, which
1639 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
1640 ERROR("%s[%d]: Line too long",
1647 while (isspace((int) *ptr)) ptr++;
1650 memmove(cbuf, ptr, len - (ptr - cbuf));
1651 len -= (ptr - cbuf);
1656 * Not doing continuations: check for edge
1663 while (*ptr && isspace((int) *ptr)) ptr++;
1665 if (!*ptr || (*ptr == '#')) continue;
1667 } else if (at_eof || (len == 0)) {
1668 ERROR("%s[%d]: Continuation at EOF is illegal",
1674 * See if there's a continuation.
1677 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
1682 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1684 * Check for "suppress spaces" magic.
1686 if (!spaces && (len > 2) && (cbuf[len - 2] == '"')) {
1690 cbuf[len - 1] = '\0';
1699 * The parser is getting to be evil.
1701 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
1703 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
1707 if (ptr[0] == '%') {
1708 hack = rad_copy_variable(buf1, ptr);
1710 hack = rad_copy_string(buf1, ptr);
1713 ERROR("%s[%d]: Invalid expansion: %s",
1714 filename, *lineno, ptr);
1720 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
1727 ERROR("%s[%d]: Invalid expansion: %s",
1728 filename, *lineno, ptr);
1732 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
1736 * The caller eats "name1 name2 {", and calls us
1737 * for the data inside of the section. So if we
1738 * receive a closing brace, then it must mean the
1739 * end of the section.
1741 if (t1 == T_RCBRACE) {
1742 if (this == current) {
1743 ERROR("%s[%d]: Too many closing braces",
1749 * Merge the template into the existing
1750 * section. This uses more memory, but
1751 * means that templates now work with
1752 * sub-sections, etc.
1754 if (!cf_template_merge(this, this->template)) {
1758 this = this->item.parent;
1759 if (seen_too_much(filename, *lineno, ptr)) return -1;
1764 * Allow for $INCLUDE files
1766 * This *SHOULD* work for any level include.
1767 * I really really really hate this file. -cparker
1769 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
1770 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
1771 bool relative = true;
1773 t2 = getword(&ptr, buf2, sizeof(buf2), true);
1775 ERROR("%s[%d]: Unexpected text after $INCLUDE",
1780 if (buf2[0] == '$') relative = false;
1782 value = cf_expand_variables(filename, lineno, this, buf, sizeof(buf), buf2);
1783 if (!value) return -1;
1785 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
1788 value = cf_local_file(filename, value, buf3,
1791 ERROR("%s[%d]: Directories too deep.",
1798 #ifdef HAVE_DIRENT_H
1802 * Include ALL non-"dot" files in the directory.
1805 if (value[strlen(value) - 1] == '/') {
1808 struct stat stat_buf;
1810 DEBUG2("including files in directory %s", value );
1815 if (stat(value, &stat_buf) < 0) {
1816 ERROR("%s[%d]: Failed reading directory %s: %s",
1818 value, fr_syserror(errno));
1822 if ((stat_buf.st_mode & S_IWOTH) != 0) {
1823 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to insecure configuration.",
1824 filename, *lineno, value);
1828 dir = opendir(value);
1830 ERROR("%s[%d]: Error reading directory %s: %s",
1831 filename, *lineno, value,
1832 fr_syserror(errno));
1837 * Read the directory, ignoring "." files.
1839 while ((dp = readdir(dir)) != NULL) {
1842 if (dp->d_name[0] == '.') continue;
1845 * Check for valid characters
1847 for (p = dp->d_name; *p != '\0'; p++) {
1848 if (isalpha((int)*p) ||
1852 (*p == '.')) continue;
1855 if (*p != '\0') continue;
1857 snprintf(buf2, sizeof(buf2), "%s%s",
1859 if ((stat(buf2, &stat_buf) != 0) ||
1860 S_ISDIR(stat_buf.st_mode)) continue;
1862 * Read the file into the current
1863 * configuration section.
1865 if (cf_file_include(this, buf2) < 0) {
1873 { /* it was a normal file */
1874 if (buf1[1] == '-') {
1875 struct stat statbuf;
1877 if (stat(value, &statbuf) < 0) {
1878 WARN("Not including file %s: %s", value, fr_syserror(errno));
1883 if (cf_file_include(this, value) < 0) {
1888 } /* we were in an include */
1890 if (strcasecmp(buf1, "$template") == 0) {
1892 CONF_SECTION *parentcs, *templatecs;
1893 t2 = getword(&ptr, buf2, sizeof(buf2), true);
1896 ERROR("%s[%d]: Unexpected text after $TEMPLATE",
1901 parentcs = cf_top_section(current);
1903 templatecs = cf_section_sub_find(parentcs, "templates");
1905 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"",
1906 filename, *lineno, buf2);
1910 ci = cf_reference_item(parentcs, templatecs, buf2);
1911 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1912 ERROR("%s[%d]: Reference \"%s\" not found",
1913 filename, *lineno, buf2);
1918 ERROR("%s[%d]: Internal sanity check error in template reference",
1923 if (this->template) {
1924 ERROR("%s[%d]: Section already has a template",
1929 this->template = cf_itemtosection(ci);
1934 * Ensure that the user can't add CONF_PAIRs
1935 * with 'internal' names;
1937 if (buf1[0] == '_') {
1938 ERROR("%s[%d]: Illegal configuration pair name \"%s\"",
1939 filename, *lineno, buf1);
1944 * Handle if/elsif specially.
1946 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
1948 char const *error = NULL;
1950 CONF_SECTION *server;
1953 * if / elsif MUST be inside of a
1954 * processing section, which MUST in turn
1955 * be inside of a "server" directive.
1957 if (!this->item.parent) {
1959 ERROR("%s[%d]: Invalid location for '%s'",
1960 filename, *lineno, buf1);
1965 * If there's a ${...}. If so, expand it.
1968 if (strchr(ptr, '$') != NULL) {
1970 ptr = cf_expand_variables(filename, lineno,
1975 ERROR("%s[%d]: Parse error expanding ${...} in condition",
1979 } /* else leave it alone */
1981 p = strrchr(ptr, '{'); /* ugh */
1984 server = this->item.parent;
1985 while ((strcmp(server->name1, "server") != 0) &&
1986 (strcmp(server->name1, "policy") != 0)) {
1987 server = server->item.parent;
1988 if (!server) goto invalid_location;
1991 nextcs = cf_section_alloc(this, buf1, ptr);
1993 ERROR("%s[%d]: Failed allocating memory for section",
1997 nextcs->item.filename = talloc_strdup(nextcs, filename);
1998 nextcs->item.lineno = *lineno;
2000 slen = fr_condition_tokenize(nextcs, cf_sectiontoitem(nextcs), ptr, &cond, &error, FR_COND_TWO_PASS);
2008 offset += ptr - start;
2010 spbuf = malloc(offset + 1);
2011 memset(spbuf, ' ', offset);
2012 spbuf[offset] = '\0';
2014 ERROR("%s[%d]: Parse error in condition",
2018 ERROR("%.*s^ %s", (int) offset, spbuf, error);
2019 talloc_free(nextcs);
2024 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2025 talloc_free(nextcs);
2026 ERROR("%s[%d]: Condition is too large after \"%s\"",
2027 filename, *lineno, buf1);
2031 memcpy(buf2, ptr, slen);
2036 if (gettoken(&ptr, buf3, sizeof(buf3), true) != T_LCBRACE) {
2037 talloc_free(nextcs);
2038 ERROR("%s[%d]: Expected '{'",
2044 * Swap the condition with trailing stuff for
2045 * the final condition.
2047 memcpy(&p, &nextcs->name2, sizeof(nextcs->name2));
2049 nextcs->name2 = talloc_typed_strdup(nextcs, buf2);
2055 * Grab the next token.
2057 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2073 case T_OP_CMP_FALSE:
2074 if (!this || (strcmp(this->name1, "update") != 0)) {
2075 ERROR("%s[%d]: Invalid operator in assignment",
2083 t3 = getstring(&ptr, buf3, sizeof(buf3), true);
2084 if (t3 == T_OP_INVALID) {
2085 ERROR("%s[%d]: Parse error: %s",
2092 * These are not allowed. Print a
2093 * helpful error message.
2095 if ((t3 == T_BACK_QUOTED_STRING) &&
2096 (!this || (strcmp(this->name1, "update") != 0))) {
2097 ERROR("%s[%d]: Syntax error: Invalid string `...` in assignment",
2103 * Handle variable substitution via ${foo}
2107 case T_DOUBLE_QUOTED_STRING:
2108 case T_BACK_QUOTED_STRING:
2109 value = cf_expand_variables(filename, lineno, this, buf, sizeof(buf), buf3);
2110 if (!value) return -1;
2123 * Add this CONF_PAIR to our CONF_SECTION
2126 cpn = cf_pair_alloc(this, buf1, value, t2, t3);
2127 if (!cpn) return -1;
2128 cpn->item.filename = talloc_strdup(cpn, filename);
2129 cpn->item.lineno = *lineno;
2130 cf_item_add(this, &(cpn->item));
2134 * No '=', must be a section or sub-section.
2137 case T_DOUBLE_QUOTED_STRING:
2138 case T_SINGLE_QUOTED_STRING:
2139 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2140 if (t3 != T_LCBRACE) {
2141 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2142 filename, *lineno, buf1, buf2);
2149 if (seen_too_much(filename, *lineno, ptr)) {
2150 if (cond) talloc_free(cond);
2155 css = cf_section_alloc(this, buf1,
2156 t2 == T_LCBRACE ? NULL : buf2);
2158 ERROR("%s[%d]: Failed allocating memory for section",
2163 css->item.filename = talloc_strdup(css, filename);
2164 css->item.lineno = *lineno;
2165 cf_item_add(this, &(css->item));
2171 cf_item_add(this, &(css->item));
2172 cf_data_add_internal(css, "if", cond, NULL, false);
2173 cond = NULL; /* eaten by the above line */
2177 * There may not be a name2
2179 css->name2_type = (t2 == T_LCBRACE) ? T_OP_INVALID : t2;
2182 * The current section is now the child section.
2188 ERROR("%s[%d]: Syntax error in '%s': %s",
2189 filename, *lineno, ptr, fr_strerror());
2193 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2194 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2200 * See if EOF was unexpected ..
2202 if (feof(fp) && (this != current)) {
2203 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2205 cf_section_name1(this), cf_section_lineno(this));
2213 * Include one config file in another.
2215 int cf_file_include(CONF_SECTION *cs, char const *filename)
2219 struct stat statbuf;
2223 DEBUG2("including configuration file %s", filename);
2225 fp = fopen(filename, "r");
2227 ERROR("Unable to open file \"%s\": %s",
2228 filename, fr_syserror(errno));
2232 if (stat(filename, &statbuf) == 0) {
2234 if ((statbuf.st_mode & S_IWOTH) != 0) {
2236 ERROR("Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
2243 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
2245 ERROR("Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
2252 if (cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT)) {
2254 ERROR("Cannot include the same file twice: \"%s\"",
2260 * Add the filename to the section
2262 mtime = talloc(cs, time_t);
2263 *mtime = statbuf.st_mtime;
2265 if (cf_data_add_internal(cs, filename, mtime, NULL, PW_TYPE_FILE_INPUT) < 0) {
2267 ERROR("Internal error opening file \"%s\"",
2272 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT);
2275 ERROR("Internal error opening file \"%s\"",
2280 if (!cs->item.filename) cs->item.filename = talloc_strdup(cs, filename);
2283 * Read the section. It's OK to have EOF without a
2284 * matching close brace.
2286 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
2296 * Bootstrap a config file.
2298 CONF_SECTION *cf_file_read(char const *filename)
2304 cs = cf_section_alloc(NULL, "main", NULL);
2305 if (!cs) return NULL;
2307 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD);
2308 if (!cp) return NULL;
2310 p = strrchr(cp->value, FR_DIR_SEP);
2313 cp->item.filename = "internal";
2314 cp->item.lineno = -1;
2315 cf_item_add(cs, &(cp->item));
2317 if (cf_file_include(cs, filename) < 0) {
2326 void cf_file_free(CONF_SECTION *cs)
2333 * Return a CONF_PAIR within a CONF_SECTION.
2335 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
2337 CONF_PAIR *cp, mycp;
2339 if (!cs || !name) return NULL;
2342 cp = rbtree_finddata(cs->pair_tree, &mycp);
2345 if (!cs->template) return NULL;
2347 return rbtree_finddata(cs->template->pair_tree, &mycp);
2351 * Return the attr of a CONF_PAIR
2354 char const *cf_pair_attr(CONF_PAIR const *pair)
2356 return (pair ? pair->attr : NULL);
2360 * Return the value of a CONF_PAIR
2363 char const *cf_pair_value(CONF_PAIR const *pair)
2365 return (pair ? pair->value : NULL);
2368 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
2370 return (pair ? pair->op : T_OP_INVALID);
2374 * Return the value type, should be one of the following:
2375 * T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2376 * T_DOUBLE_QUOTED_STRING or T_OP_INVALID if the pair is NULL.
2378 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
2380 return (pair ? pair->value_type : T_OP_INVALID);
2384 * Turn a CONF_PAIR into a VALUE_PAIR
2385 * For now, ignore the "value_type" field...
2387 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
2390 fr_strerror_printf("Internal error");
2395 fr_strerror_printf("No value given for attribute %s", pair->attr);
2400 * false comparisons never match. BUT if it's a "string"
2401 * or `string`, then remember to expand it later.
2403 if ((pair->op != T_OP_CMP_FALSE) &&
2404 ((pair->value_type == T_DOUBLE_QUOTED_STRING) ||
2405 (pair->value_type == T_BACK_QUOTED_STRING))) {
2408 vp = pairmake(pair, NULL, pair->attr, NULL, pair->op);
2413 if (pairmark_xlat(vp, pair->value) < 0) {
2422 return pairmake(pair, NULL, pair->attr, pair->value, pair->op);
2426 * Return the first label of a CONF_SECTION
2429 char const *cf_section_name1(CONF_SECTION const *cs)
2431 return (cs ? cs->name1 : NULL);
2435 * Return the second label of a CONF_SECTION
2438 char const *cf_section_name2(CONF_SECTION const *cs)
2440 return (cs ? cs->name2 : NULL);
2444 * Find a value in a CONF_SECTION
2446 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
2450 cp = cf_pair_find(cs, attr);
2452 return (cp ? cp->value : NULL);
2456 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
2457 char const *name1, char const *name2)
2460 CONF_ITEM const *ci;
2462 if (!cs || !name1) return NULL;
2464 for (ci = &(cs->item); ci; ci = ci->next) {
2465 if (ci->type != CONF_ITEM_SECTION)
2468 if (strcmp(cf_itemtosection(ci)->name1, name1) != 0) {
2472 their2 = cf_itemtosection(ci)->name2;
2474 if ((!name2 && !their2) ||
2475 (name2 && their2 && (strcmp(name2, their2) == 0))) {
2476 return cf_itemtosection(ci);
2484 * Return the next pair after a CONF_PAIR
2485 * with a certain name (char *attr) If the requested
2486 * attr is NULL, any attr matches.
2489 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
2490 CONF_PAIR const *pair, char const *attr)
2494 if (!cs) return NULL;
2497 * If pair is NULL this must be a first time run
2498 * Find the pair with correct name
2501 if (!pair) return cf_pair_find(cs, attr);
2503 for (ci = pair->item.next; ci; ci = ci->next) {
2504 if (ci->type != CONF_ITEM_PAIR)
2507 if (!attr || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
2511 return cf_itemtopair(ci);
2515 * Find a CONF_SECTION, or return the root if name is NULL
2518 CONF_SECTION *cf_section_find(char const *name)
2521 return cf_section_sub_find(root_config, name);
2526 /** Find a sub-section in a section
2528 * This finds ANY section having the same first name.
2529 * The second name is ignored.
2531 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
2535 if (!cs || !name) return NULL; /* can't find an un-named section */
2538 * No sub-sections have been defined, so none exist.
2540 if (!cs->section_tree) return NULL;
2544 return rbtree_finddata(cs->section_tree, &mycs);
2548 /** Find a CONF_SECTION with both names.
2551 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
2552 char const *name1, char const *name2)
2556 if (!cs) cs = root_config;
2557 if (!cs) return NULL;
2560 CONF_SECTION mycs, *master_cs;
2562 if (!cs->section_tree) return NULL;
2567 master_cs = rbtree_finddata(cs->section_tree, &mycs);
2568 if (!master_cs) return NULL;
2571 * Look it up in the name2 tree. If it's there,
2574 if (master_cs->name2_tree) {
2575 CONF_SECTION *subcs;
2577 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
2578 if (subcs) return subcs;
2582 * We don't insert ourselves into the name2 tree.
2583 * So if there's nothing in the name2 tree, maybe
2584 * *we* are the answer.
2586 if (!master_cs->name2 && name2) return NULL;
2587 if (master_cs->name2 && !name2) return NULL;
2588 if (!master_cs->name2 && !name2) return master_cs;
2590 if (strcmp(master_cs->name2, name2) == 0) {
2598 * Else do it the old-fashioned way.
2600 for (ci = cs->children; ci; ci = ci->next) {
2601 CONF_SECTION *subcs;
2603 if (ci->type != CONF_ITEM_SECTION)
2606 subcs = cf_itemtosection(ci);
2607 if (!subcs->name2) {
2608 if (strcmp(subcs->name1, name2) == 0) break;
2610 if (strcmp(subcs->name2, name2) == 0) break;
2614 return cf_itemtosection(ci);
2618 * Return the next subsection after a CONF_SECTION
2619 * with a certain name1 (char *name1). If the requested
2620 * name1 is NULL, any name1 matches.
2623 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
2624 CONF_SECTION const *subsection,
2629 if (!section) return NULL;
2632 * If subsection is NULL this must be a first time run
2633 * Find the subsection with correct name
2637 ci = section->children;
2639 ci = subsection->item.next;
2642 for (; ci; ci = ci->next) {
2643 if (ci->type != CONF_ITEM_SECTION)
2645 if ((name1 == NULL) ||
2646 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
2650 return cf_itemtosection(ci);
2655 * Return the next section after a CONF_SECTION
2656 * with a certain name1 (char *name1). If the requested
2657 * name1 is NULL, any name1 matches.
2660 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
2661 CONF_SECTION const *subsection,
2664 if (!section) return NULL;
2666 if (!section->item.parent) return NULL;
2668 return cf_subsection_find_next(section->item.parent, subsection, name1);
2672 * Return the next item after a CONF_ITEM.
2675 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
2677 if (!section) return NULL;
2680 * If item is NULL this must be a first time run
2681 * Return the first item
2685 return section->children;
2691 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
2693 if (!ci) return NULL;
2698 int cf_section_lineno(CONF_SECTION const *section)
2700 return section->item.lineno;
2703 char const *cf_pair_filename(CONF_PAIR const *pair)
2705 return pair->item.filename;
2708 char const *cf_section_filename(CONF_SECTION const *section)
2710 return section->item.filename;
2713 int cf_pair_lineno(CONF_PAIR const *pair)
2715 return pair->item.lineno;
2718 int cf_item_is_section(CONF_ITEM const *item)
2720 return item->type == CONF_ITEM_SECTION;
2722 int cf_item_is_pair(CONF_ITEM const *item)
2724 return item->type == CONF_ITEM_PAIR;
2728 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
2729 void *data, void (*data_free)(void *))
2733 cd = talloc_zero(parent, CONF_DATA);
2734 if (!cd) return NULL;
2736 cd->item.type = CONF_ITEM_DATA;
2737 cd->item.parent = parent;
2738 cd->name = talloc_typed_strdup(cd, name);
2745 cd->free = data_free;
2748 talloc_set_destructor((void *) cd, cf_data_free);
2755 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name,
2758 if (!cs || !name) return NULL;
2761 * Find the name in the tree, for speed.
2763 if (cs->data_tree) {
2768 return rbtree_finddata(cs->data_tree, &mycd);
2775 * Find data from a particular section.
2777 void *cf_data_find(CONF_SECTION const *cs, char const *name)
2779 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
2781 if (cd) return cd->data;
2787 * Add named data to a configuration section.
2789 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
2790 void *data, void (*data_free)(void *),
2795 if (!cs || !name) return -1;
2798 * Already exists. Can't add it.
2800 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
2802 cd = cf_data_alloc(cs, name, data, data_free);
2806 cf_item_add(cs, cf_datatoitem(cd));
2812 * Add named data to a configuration section.
2814 int cf_data_add(CONF_SECTION *cs, char const *name,
2815 void *data, void (*data_free)(void *))
2817 return cf_data_add_internal(cs, name, data, data_free, 0);
2821 * This is here to make the rest of the code easier to read. It
2822 * ties conffile.c to log.c, but it means we don't have to
2823 * pollute every other function with the knowledge of the
2824 * configuration internals.
2826 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
2832 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2837 ci->filename ? ci->filename : "unknown",
2838 ci->lineno ? ci->lineno : 0,
2841 ERROR("<unknown>[*]: %s", buffer);
2845 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
2851 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2854 rad_assert(cs != NULL);
2857 cs->item.filename ? cs->item.filename : "unknown",
2858 cs->item.lineno ? cs->item.lineno : 0,
2862 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
2868 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2871 rad_assert(cp != NULL);
2874 cp->item.filename ? cp->item.filename : "unknown",
2875 cp->item.lineno ? cp->item.lineno : 0,
2879 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
2884 if ((debug_flag > 1) && cs) vradlog(L_DBG, fmt, ap);
2889 * Wrapper to simplify the code.
2891 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
2897 if (debug_flag > 1 && cs) {
2898 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2900 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
2905 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
2907 if (!cs) return NULL;
2909 return cs->variables;
2913 * For "switch" and "case" statements.
2915 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
2917 if (!cs) return T_OP_INVALID;
2919 return cs->name2_type;