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 static 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; //!< Sibling.
67 struct conf_part *parent; //!< Parent.
68 int lineno; //!< The line number the config item began on.
69 char const *filename; //!< The file the config item was parsed from.
70 CONF_ITEM_TYPE type; //!< Whether the config item is a config_pair, conf_section or conf_data.
73 /** Configuration AVP similar to a VALUE_PAIR
78 char const *attr; //!< Attribute name
79 char const *value; //!< Attribute value
80 FR_TOKEN op; //!< Operator e.g. =, :=
81 FR_TOKEN lhs_type; //!< Name quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
82 FR_TOKEN rhs_type; //!< Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
83 bool pass2; //!< do expansion in pass2.
84 bool parsed; //!< Was this item used during parsing?
87 /** Internal data that is associated with a configuration section
94 void *data; //!< User data
95 void (*free)(void *); //!< Free user data function
100 char const *name1; //!< First name token. Given ``foo bar {}`` would be ``foo``.
101 char const *name2; //!< Second name token. Given ``foo bar {}`` would be ``bar``.
103 FR_TOKEN name2_type; //!< The type of quoting around name2.
106 CONF_ITEM *tail; //!< For speed.
107 CONF_SECTION *template;
109 rbtree_t *pair_tree; //!< and a partridge..
110 rbtree_t *section_tree; //!< no jokes here.
111 rbtree_t *name2_tree; //!< for sections of the same name2
117 CONF_PARSER const *variables;
120 CONF_SECTION *root_config = NULL;
121 bool cf_new_escape = false;
124 static int cf_data_add_internal(CONF_SECTION *cs, char const *name, void *data,
125 void (*data_free)(void *), int flag);
127 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag);
129 static char const *cf_expand_variables(char const *cf, int *lineno,
130 CONF_SECTION *outercs,
131 char *output, size_t outsize,
132 char const *input, bool *soft_fail);
135 * Isolate the scary casts in these tiny provably-safe functions
138 /** Cast a CONF_ITEM to a CONF_PAIR
141 CONF_PAIR *cf_item_to_pair(CONF_ITEM const *ci)
145 if (ci == NULL) return NULL;
147 rad_assert(ci->type == CONF_ITEM_PAIR);
149 memcpy(&out, &ci, sizeof(out));
153 /** Cast a CONF_ITEM to a CONF_SECTION
156 CONF_SECTION *cf_item_to_section(CONF_ITEM const *ci)
160 if (ci == NULL) return NULL;
162 rad_assert(ci->type == CONF_ITEM_SECTION);
164 memcpy(&out, &ci, sizeof(out));
168 /** Cast a CONF_PAIR to a CONF_ITEM
171 CONF_ITEM *cf_pair_to_item(CONF_PAIR const *cp)
175 if (cp == NULL) return NULL;
177 memcpy(&out, &cp, sizeof(out));
181 /** Cast a CONF_SECTION to a CONF_ITEM
184 CONF_ITEM *cf_section_to_item(CONF_SECTION const *cs)
188 if (cs == NULL) return NULL;
190 memcpy(&out, &cs, sizeof(out));
194 /** Cast CONF_DATA to a CONF_ITEM
197 static CONF_ITEM *cf_data_to_item(CONF_DATA const *cd)
205 memcpy(&out, &cd, sizeof(out));
209 static int _cf_data_free(CONF_DATA *cd)
211 if (cd->free) cd->free(cd->data);
217 * rbtree callback function
219 static int pair_cmp(void const *a, void const *b)
221 CONF_PAIR const *one = a;
222 CONF_PAIR const *two = b;
224 return strcmp(one->attr, two->attr);
229 * rbtree callback function
231 static int section_cmp(void const *a, void const *b)
233 CONF_SECTION const *one = a;
234 CONF_SECTION const *two = b;
236 return strcmp(one->name1, two->name1);
241 * rbtree callback function
243 static int name2_cmp(void const *a, void const *b)
245 CONF_SECTION const *one = a;
246 CONF_SECTION const *two = b;
248 rad_assert(strcmp(one->name1, two->name1) == 0);
250 if (!one->name2 && !two->name2) return 0;
251 if (one->name2 && !two->name2) return -1;
252 if (!one->name2 && two->name2) return +1;
254 return strcmp(one->name2, two->name2);
259 * rbtree callback function
261 static int data_cmp(void const *a, void const *b)
265 CONF_DATA const *one = a;
266 CONF_DATA const *two = b;
268 rcode = one->flag - two->flag;
269 if (rcode != 0) return rcode;
271 return strcmp(one->name, two->name);
274 static int _cf_section_free(CONF_SECTION *cs)
277 * Name1 and name2 are allocated contiguous with
281 rbtree_free(cs->pair_tree);
282 cs->pair_tree = NULL;
284 if (cs->section_tree) {
285 rbtree_free(cs->section_tree);
286 cs->section_tree = NULL;
288 if (cs->name2_tree) {
289 rbtree_free(cs->name2_tree);
290 cs->name2_tree = NULL;
293 rbtree_free(cs->data_tree);
294 cs->data_tree = NULL;
300 /** Allocate a CONF_PAIR
302 * @param parent CONF_SECTION to hang this CONF_PAIR off of.
304 * @param value of CONF_PAIR.
305 * @param op T_OP_EQ, T_OP_SET etc.
306 * @param lhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
307 * @param rhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
308 * @return NULL on error, else a new CONF_SECTION parented by parent.
310 CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value,
311 FR_TOKEN op, FR_TOKEN lhs_type, FR_TOKEN rhs_type)
315 rad_assert(fr_equality_op[op] || fr_assignment_op[op]);
316 if (!attr) return NULL;
318 cp = talloc_zero(parent, CONF_PAIR);
319 if (!cp) return NULL;
321 cp->item.type = CONF_ITEM_PAIR;
322 cp->item.parent = parent;
323 cp->lhs_type = lhs_type;
324 cp->rhs_type = rhs_type;
327 cp->attr = talloc_typed_strdup(cp, attr);
335 cp->value = talloc_typed_strdup(cp, value);
336 if (!cp->value) goto error;
342 /** Duplicate a CONF_PAIR
344 * @param parent to allocate new pair in.
345 * @param cp to duplicate.
346 * @return NULL on error, else a duplicate of the input pair.
348 CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp)
355 new = cf_pair_alloc(parent, cp->attr, cf_pair_value(cp),
356 cp->op, cp->lhs_type, cp->rhs_type);
357 if (!new) return NULL;
359 new->parsed = cp->parsed;
360 new->item.lineno = cp->item.lineno;
363 * Avoid mallocs if possible.
365 if (!cp->item.filename || (strcmp(parent->item.filename, cp->item.filename) == 0)) {
366 new->item.filename = parent->item.filename;
368 new->item.filename = talloc_strdup(new, cp->item.filename);
374 /** Add a configuration pair to a section
376 * @param parent section to add pair to.
379 void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp)
381 cf_item_add(parent, cf_pair_to_item(cp));
384 /** Allocate a CONF_SECTION
386 * @param parent CONF_SECTION to hang this CONF_SECTION off of.
387 * @param name1 Primary name.
388 * @param name2 Secondary name.
389 * @return NULL on error, else a new CONF_SECTION parented by parent.
391 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
396 if (!name1) return NULL;
398 if (name2 && parent) {
399 if (strchr(name2, '$')) {
400 name2 = cf_expand_variables(parent->item.filename,
401 &parent->item.lineno,
403 buffer, sizeof(buffer), name2, NULL);
405 ERROR("Failed expanding section name");
411 cs = talloc_zero(parent, CONF_SECTION);
412 if (!cs) return NULL;
414 cs->item.type = CONF_ITEM_SECTION;
415 cs->item.parent = parent;
417 cs->name1 = talloc_typed_strdup(cs, name1);
425 cs->name2 = talloc_typed_strdup(cs, name2);
426 if (!cs->name2) goto error;
429 cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
430 if (!cs->pair_tree) goto error;
432 talloc_set_destructor(cs, _cf_section_free);
435 * Don't create a data tree, it may not be needed.
439 * Don't create the section tree here, it may not
443 if (parent) cs->depth = parent->depth + 1;
448 /** Duplicate a configuration section
450 * @note recursively duplicates any child sections.
451 * @note does not duplicate any data associated with a section, or its child sections.
453 * @param parent section.
454 * @param cs to duplicate.
455 * @param name1 of new section.
456 * @param name2 of new section.
457 * @param copy_meta Copy additional meta data for a section (like template, base, depth and variables).
458 * @return a duplicate of the existing section, or NULL on error.
460 CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs,
461 char const *name1, char const *name2, bool copy_meta)
463 CONF_SECTION *new, *subcs;
467 new = cf_section_alloc(parent, name1, name2);
470 new->template = cs->template;
471 new->base = cs->base;
472 new->depth = cs->depth;
473 new->variables = cs->variables;
476 new->item.lineno = cs->item.lineno;
478 if (!cs->item.filename || (strcmp(parent->item.filename, cs->item.filename) == 0)) {
479 new->item.filename = parent->item.filename;
481 new->item.filename = talloc_strdup(new, cs->item.filename);
484 for (ci = cs->children; ci; ci = ci->next) {
486 case CONF_ITEM_SECTION:
487 subcs = cf_item_to_section(ci);
488 subcs = cf_section_dup(new, subcs,
489 cf_section_name1(subcs), cf_section_name2(subcs),
495 cf_section_add(new, subcs);
499 cp = cf_pair_dup(new, cf_item_to_pair(ci));
504 cf_pair_add(new, cp);
507 case CONF_ITEM_DATA: /* Skip data */
510 case CONF_ITEM_INVALID:
518 void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
520 cf_item_add(parent, &(cs->item));
523 /** Replace pair in a given section with a new pair, of the given value.
525 * @param cs to replace pair in.
526 * @param cp to replace.
527 * @param value New value to assign to cp.
528 * @return 0 on success, -1 on failure.
530 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
533 CONF_ITEM *ci, *cn, **last;
535 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->lhs_type, cp->rhs_type);
536 if (!newp) return -1;
542 * Find the old one from the linked list, and replace it
545 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
547 cn->next = (*last)->next;
554 rbtree_deletebydata(cs->pair_tree, ci);
556 rbtree_insert(cs->pair_tree, cn);
563 * Add an item to a configuration section.
565 void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
568 CONF_ITEM *first = ci;
571 rad_assert((void *)cs != (void *)ci);
573 if (!cs || !ci) return;
576 rad_assert(cs->tail == NULL);
579 rad_assert(cs->tail != NULL);
584 * Update the trees (and tail) for each item added.
586 for (/* nothing */; ci != NULL; ci = ci->next) {
587 rad_assert(ci->next != first); /* simple cycle detection */
592 * For fast lookups, pairs and sections get
597 if (!rbtree_insert(cs->pair_tree, ci)) {
598 CONF_PAIR *cp = cf_item_to_pair(ci);
600 if (strcmp(cp->attr, "confdir") == 0) break;
601 if (!cp->value) break; /* module name, "ok", etc. */
605 case CONF_ITEM_SECTION: {
606 CONF_SECTION *cs_new = cf_item_to_section(ci);
607 CONF_SECTION *name1_cs;
609 if (!cs->section_tree) {
610 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
611 if (!cs->section_tree) {
612 ERROR("Out of memory");
617 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
619 if (!rbtree_insert(cs->section_tree, cs_new)) {
620 ERROR("Failed inserting section into tree");
627 * We already have a section of
628 * this "name1". Add a new
629 * sub-section based on name2.
631 if (!name1_cs->name2_tree) {
632 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
633 if (!name1_cs->name2_tree) {
634 ERROR("Out of memory");
640 * We don't care if this fails.
641 * If the user tries to create
642 * two sections of the same
643 * name1/name2, the duplicate
644 * section is just silently
647 rbtree_insert(name1_cs->name2_tree, cs_new);
649 } /* was a section */
652 if (!cs->data_tree) {
653 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
656 rbtree_insert(cs->data_tree, ci);
660 default: /* FIXME: assert & error! */
663 } /* switch over conf types */
668 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
669 CONF_SECTION *outercs,
674 CONF_SECTION const *cs = outercs;
678 if (!cs) goto no_such_item;
680 strlcpy(name, ptr, sizeof(name));
684 * ".foo" means "foo from the current section"
690 * Just '.' means the current section
693 return cf_section_to_item(cs);
697 * ..foo means "foo from the section
698 * enclosing this section" (etc.)
701 if (cs->item.parent) {
702 cs = cs->item.parent;
706 * .. means the section
707 * enclosing this section
710 return cf_section_to_item(cs);
715 * "foo.bar.baz" means "from the root"
717 } else if (strchr(p, '.') != NULL) {
718 if (!parentcs) goto no_such_item;
730 if (r && q > r) q = NULL;
731 if (q && q < r) r = NULL;
737 q = strchr(r + 1, ']');
738 if (!q) return NULL; /* parse error */
741 * Points to foo[bar]xx: parse error,
742 * it should be foo[bar] or foo[bar].baz
744 if (q[1] && q[1] != '.') goto no_such_item;
748 next = cf_section_sub_find_name2(cs, p, r + 1);
753 * Points to a named instance of a section.
756 if (!next) goto no_such_item;
757 return &(next->item);
760 q++; /* ensure we skip the ']' and '.' */
764 next = cf_section_sub_find(cs, p);
768 if (!next) break; /* it MAY be a pair in this section! */
774 if (!*p) goto no_such_item;
778 * Find it in the current referenced
781 cp = cf_pair_find(cs, p);
783 cp->parsed = true; /* conf pairs which are referenced count as parsed */
787 next = cf_section_sub_find(cs, p);
788 if (next) return &(next->item);
791 * "foo" is "in the current section, OR in main".
793 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
803 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
805 if (!cs) return NULL;
807 while (cs->item.parent != NULL) {
808 cs = cs->item.parent;
816 * Expand the variables in an input string.
818 static char const *cf_expand_variables(char const *cf, int *lineno,
819 CONF_SECTION *outercs,
820 char *output, size_t outsize,
821 char const *input, bool *soft_fail)
824 char const *end, *ptr;
825 CONF_SECTION const *parentcs;
828 if (soft_fail) *soft_fail = false;
831 * Find the master parent conf section.
832 * We can't use main_config.config, because we're in the
833 * process of re-building it, and it isn't set up yet...
835 parentcs = cf_top_section(outercs);
841 * Ignore anything other than "${"
843 if ((*ptr == '$') && (ptr[1] == '{')) {
849 * FIXME: Add support for ${foo:-bar},
854 * Look for trailing '}', and log a
855 * warning for anything that doesn't match,
856 * and exit with a fatal error.
858 end = strchr(ptr, '}');
861 INFO("%s[%d]: Variable expansion missing }",
869 * Can't really happen because input lines are
870 * capped at 8k, which is sizeof(name)
872 if ((size_t) (end - ptr) >= sizeof(name)) {
873 ERROR("%s[%d]: Reference string is too large",
878 memcpy(name, ptr, end - ptr);
879 name[end - ptr] = '\0';
881 q = strchr(name, ':');
886 ci = cf_reference_item(parentcs, outercs, name);
888 if (soft_fail) *soft_fail = true;
889 ERROR("%s[%d]: Reference \"%s\" not found", cf, *lineno, input);
894 * The expansion doesn't refer to another item or section
895 * it's the property of a section.
898 CONF_SECTION *mycs = cf_item_to_section(ci);
900 if (ci->type != CONF_ITEM_SECTION) {
901 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
905 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
906 case CONF_PROPERTY_NAME:
907 strcpy(p, mycs->name1);
910 case CONF_PROPERTY_INSTANCE:
911 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
915 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
921 } else if (ci->type == CONF_ITEM_PAIR) {
923 * Substitute the value of the variable.
925 cp = cf_item_to_pair(ci);
928 * If the thing we reference is
929 * marked up as being expanded in
930 * pass2, don't expand it now.
931 * Let it be expanded in pass2.
934 if (soft_fail) *soft_fail = true;
936 ERROR("%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
942 ERROR("%s[%d]: Reference \"%s\" has no value",
947 if (p + strlen(cp->value) >= output + outsize) {
948 ERROR("%s[%d]: Reference \"%s\" is too long",
953 strcpy(p, cp->value);
957 } else if (ci->type == CONF_ITEM_SECTION) {
961 * Adding an entry again to a
962 * section is wrong. We don't
963 * want an infinite loop.
965 if (ci->parent == outercs) {
966 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
971 * Copy the section instead of
974 subcs = cf_item_to_section(ci);
975 subcs = cf_section_dup(outercs, subcs,
976 cf_section_name1(subcs), cf_section_name2(subcs),
979 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
983 subcs->item.filename = ci->filename;
984 subcs->item.lineno = ci->lineno;
985 cf_item_add(outercs, &(subcs->item));
990 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
993 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
999 * Look for trailing '}', and log a
1000 * warning for anything that doesn't match,
1001 * and exit with a fatal error.
1003 end = strchr(ptr, '}');
1006 INFO("%s[%d]: Environment variable expansion missing }",
1012 * Can't really happen because input lines are
1013 * capped at 8k, which is sizeof(name)
1015 if ((size_t) (end - ptr) >= sizeof(name)) {
1016 ERROR("%s[%d]: Environment variable name is too large",
1021 memcpy(name, ptr, end - ptr);
1022 name[end - ptr] = '\0';
1025 * Get the environment variable.
1026 * If none exists, then make it an empty string.
1034 if (p + strlen(env) >= output + outsize) {
1035 ERROR("%s[%d]: Reference \"%s\" is too long",
1036 cf, *lineno, input);
1046 * Copy it over verbatim.
1052 if (p >= (output + outsize)) {
1053 ERROR("%s[%d]: Reference \"%s\" is too long",
1054 cf, *lineno, input);
1057 } /* loop over all of the input string. */
1064 static char const parse_spaces[] = " ";
1066 /** Validation function for ipaddr conffile types
1069 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1070 fr_ipaddr_t *ipaddr)
1074 if (strcmp(value, "*") == 0) {
1075 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1076 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1077 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1079 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1080 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1084 case PW_TYPE_IPV4_ADDR:
1085 case PW_TYPE_IPV6_ADDR:
1086 case PW_TYPE_COMBO_IP_ADDR:
1087 switch (ipaddr->af) {
1089 if (ipaddr->prefix != 32) {
1090 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
1098 if (ipaddr->prefix != 128) {
1099 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
1114 /** Parses a #CONF_PAIR into a C data type, with a default value.
1116 * Takes fields from a #CONF_PARSER struct and uses them to parse the string value
1117 * of a #CONF_PAIR into a C data type matching the type argument.
1119 * The format of the types are the same as #value_data_t types.
1121 * @note The dflt value will only be used if no matching #CONF_PAIR is found. Empty strings will not
1122 * result in the dflt value being used.
1124 * @param cs to search for matching #CONF_PAIR in.
1125 * @param name of #CONF_PAIR to search for.
1126 * @param type Data type to parse #CONF_PAIR value as.
1127 * Should be one of the following ``data`` types, and one or more of the following ``flag`` types or'd together:
1128 * - ``data`` #PW_TYPE_TMPL - @copybrief PW_TYPE_TMPL
1129 Feeds the value into #tmpl_afrom_str. Value can be
1130 * obtained when processing requests, with #tmpl_expand or #tmpl_aexpand.
1131 * - ``data`` #PW_TYPE_BOOLEAN - @copybrief PW_TYPE_BOOLEAN
1132 * - ``data`` #PW_TYPE_INTEGER - @copybrief PW_TYPE_INTEGER
1133 * - ``data`` #PW_TYPE_SHORT - @copybrief PW_TYPE_SHORT
1134 * - ``data`` #PW_TYPE_INTEGER64 - @copybrief PW_TYPE_INTEGER64
1135 * - ``data`` #PW_TYPE_SIGNED - @copybrief PW_TYPE_SIGNED
1136 * - ``data`` #PW_TYPE_STRING - @copybrief PW_TYPE_STRING
1137 * - ``data`` #PW_TYPE_IPV4_ADDR - @copybrief PW_TYPE_IPV4_ADDR (IPv4 address with prefix 32).
1138 * - ``data`` #PW_TYPE_IPV4_PREFIX - @copybrief PW_TYPE_IPV4_PREFIX (IPv4 address with variable prefix).
1139 * - ``data`` #PW_TYPE_IPV6_ADDR - @copybrief PW_TYPE_IPV6_ADDR (IPv6 address with prefix 128).
1140 * - ``data`` #PW_TYPE_COMBO_IP_ADDR - @copybrief PW_TYPE_COMBO_IP_ADDR (IPv4/IPv6 address with
1142 * - ``data`` #PW_TYPE_COMBO_IP_PREFIX - @copybrief PW_TYPE_COMBO_IP_PREFIX (IPv4/IPv6 address with
1144 * - ``data`` #PW_TYPE_TIMEVAL - @copybrief PW_TYPE_TIMEVAL
1145 * - ``flag`` #PW_TYPE_DEPRECATED - @copybrief PW_TYPE_DEPRECATED
1146 * - ``flag`` #PW_TYPE_REQUIRED - @copybrief PW_TYPE_REQUIRED
1147 * - ``flag`` #PW_TYPE_ATTRIBUTE - @copybrief PW_TYPE_ATTRIBUTE
1148 * - ``flag`` #PW_TYPE_SECRET - @copybrief PW_TYPE_SECRET
1149 * - ``flag`` #PW_TYPE_FILE_INPUT - @copybrief PW_TYPE_FILE_INPUT
1150 * - ``flag`` #PW_TYPE_NOT_EMPTY - @copybrief PW_TYPE_NOT_EMPTY
1151 * @param data Pointer to a global variable, or pointer to a field in the struct being populated with values.
1152 * @param dflt value to use, if no #CONF_PAIR is found.
1153 * @return -1 on error, -2 if deprecated, 0 on success (correctly parsed), 1 if default value was used.
1155 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1158 bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, xlat, multi;
1161 CONF_PAIR *cp = NULL;
1162 fr_ipaddr_t *ipaddr;
1167 deprecated = (type & PW_TYPE_DEPRECATED);
1168 required = (type & PW_TYPE_REQUIRED);
1169 attribute = (type & PW_TYPE_ATTRIBUTE);
1170 secret = (type & PW_TYPE_SECRET);
1171 file_input = (type == PW_TYPE_FILE_INPUT); /* check, not and */
1172 cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1173 tmpl = (type & PW_TYPE_TMPL);
1174 xlat = (type & PW_TYPE_XLAT);
1175 multi = (type & PW_TYPE_MULTI);
1177 if (attribute) required = true;
1178 if (required) cant_be_empty = true; /* May want to review this in the future... */
1181 * Everything except templates must have a base type.
1183 if (!(type & 0xff) && !tmpl) {
1184 cf_log_err(&(cs->item), "Configuration item '%s' must have a data type", name);
1188 type &= 0xff; /* normal types are small */
1192 cp = cf_pair_find(cs, name);
1195 * No pairs match the configuration item name in the current
1196 * section, use the default value.
1202 * Something matched, used the CONF_PAIR value.
1205 CONF_PAIR *next = cp;
1210 * @fixme We should actually validate
1211 * the value of the pairs too
1213 if (multi) while ((next = cf_pair_find_next(cs, next, name))) {
1214 next->parsed = true;
1222 cf_log_err(&(cs->item), "Configuration item '%s' must have a value", name);
1224 cf_log_err(&(cp->item), "Configuration item '%s' must have a value", name);
1231 if ((value[0] == '\0') && cant_be_empty) {
1234 cf_log_err(&(cs->item), "Configuration item '%s' must not be empty (zero length)", name);
1235 if (!required) cf_log_err(&(cs->item), "Comment item to silence this message");
1237 cf_log_err(&(cp->item), "Configuration item '%s' must not be empty (zero length)", name);
1238 if (!required) cf_log_err(&(cp->item), "Comment item to silence this message");
1244 cf_log_err(&(cs->item), "Configuration item \"%s\" is deprecated", name);
1250 * Process a value as a LITERAL template. Once all of
1251 * the attrs and xlats are defined, the pass2 code
1252 * converts it to the appropriate type.
1258 *(vp_tmpl_t **)data = NULL;
1262 rad_assert(!attribute);
1264 vpt = tmpl_alloc(cs, TMPL_TYPE_LITERAL, value, strlen(value));
1265 *(vp_tmpl_t **)data = vpt;
1271 case PW_TYPE_BOOLEAN:
1273 * Allow yes/no, true/false, and on/off
1275 if ((strcasecmp(value, "yes") == 0) ||
1276 (strcasecmp(value, "true") == 0) ||
1277 (strcasecmp(value, "on") == 0)) {
1278 *(bool *)data = true;
1279 } else if ((strcasecmp(value, "no") == 0) ||
1280 (strcasecmp(value, "false") == 0) ||
1281 (strcasecmp(value, "off") == 0)) {
1282 *(bool *)data = false;
1284 *(bool *)data = false;
1285 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1286 "variable %s", value, name);
1289 cf_log_info(cs, "%.*s\t%s = %s",
1290 cs->depth, parse_spaces, name, value);
1293 case PW_TYPE_INTEGER:
1295 unsigned long v = strtoul(value, 0, 0);
1298 * Restrict integer values to 0-INT32_MAX, this means
1299 * it will always be safe to cast them to a signed type
1300 * for comparisons, and imposes the same range limit as
1301 * before we switched to using an unsigned type to
1302 * represent config item integers.
1304 if (v > INT32_MAX) {
1305 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1310 *(uint32_t *)data = v;
1311 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1317 unsigned long v = strtoul(value, 0, 0);
1319 if (v > UINT16_MAX) {
1320 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1324 *(uint16_t *)data = (uint16_t) v;
1325 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1329 case PW_TYPE_INTEGER64:
1330 *(uint64_t *)data = strtoull(value, 0, 0);
1331 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1334 case PW_TYPE_SIGNED:
1335 *(int32_t *)data = strtol(value, 0, 0);
1336 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1339 case PW_TYPE_STRING:
1346 * Expand variables which haven't already been
1347 * expanded automagically when the configuration
1350 if (value == dflt) {
1353 lineno = cs->item.lineno;
1355 value = cf_expand_variables("<internal>",
1357 cs, buffer, sizeof(buffer),
1360 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1365 if (required && !value) goto is_required;
1366 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1369 if (!dict_attrbyname(value)) {
1371 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1374 cf_log_err(&(cp->item), "No such attribute '%s'", value);
1381 * Hide secrets when using "radiusd -X".
1383 if (secret && (rad_debug_lvl <= 2)) {
1384 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1385 cs->depth, parse_spaces, name);
1387 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1388 cs->depth, parse_spaces, name, value ? value : "(null)");
1390 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1393 * If there's data AND it's an input file, check
1394 * that we can read it. This check allows errors
1395 * to be caught as early as possible, during
1398 if (*q && file_input) {
1401 if (stat(*q, &buf) < 0) {
1402 char user[255], group[255];
1404 ERROR("Unable to open file \"%s\": %s", value, fr_syserror(errno));
1405 ERROR("Our effective user and group was %s:%s",
1406 (rad_prints_uid(NULL, user, sizeof(user), geteuid()) < 0) ?
1408 (rad_prints_gid(NULL, group, sizeof(group), getegid()) < 0) ?
1409 "unknown" : group );
1416 case PW_TYPE_IPV4_ADDR:
1417 case PW_TYPE_IPV4_PREFIX:
1420 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1421 ERROR("%s", fr_strerror());
1424 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1427 case PW_TYPE_IPV6_ADDR:
1428 case PW_TYPE_IPV6_PREFIX:
1431 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1432 ERROR("%s", fr_strerror());
1435 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1438 case PW_TYPE_COMBO_IP_ADDR:
1439 case PW_TYPE_COMBO_IP_PREFIX:
1442 if (fr_pton(ipaddr, value, -1, true) < 0) {
1443 ERROR("%s", fr_strerror());
1446 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1449 case PW_TYPE_TIMEVAL: {
1454 sec = strtoul(value, &end, 10);
1460 len = strlen(end + 1);
1463 ERROR("Too much precision for timeval");
1468 * If they write "0.1", that means
1469 * "10000" microseconds.
1471 sec = strtoul(end + 1, NULL, 10);
1479 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1480 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1481 memcpy(data, &tv, sizeof(tv));
1487 * If we get here, it's a sanity check error.
1488 * It's not an error parsing the configuration
1491 rad_assert(type > PW_TYPE_INVALID);
1492 rad_assert(type < PW_TYPE_MAX);
1494 ERROR("type '%s' is not supported in the configuration files",
1495 fr_int2str(dict_attr_types, type, "?Unknown?"));
1497 } /* switch over variable type */
1502 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1503 if (!cpn) return -1;
1505 cpn->item.filename = "<internal>";
1506 cpn->item.lineno = 0;
1507 cf_item_add(cs, &(cpn->item));
1515 * A copy of cf_section_parse that initializes pointers before
1518 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1519 CONF_PARSER const *variables)
1523 for (i = 0; variables[i].name != NULL; i++) {
1524 if (variables[i].type == PW_TYPE_SUBSECTION) {
1525 CONF_SECTION *subcs;
1527 if (!variables[i].dflt) continue;
1529 subcs = cf_section_sub_find(cs, variables[i].name);
1532 * If there's no subsection in the
1533 * config, BUT the CONF_PARSER wants one,
1534 * then create an empty one. This is so
1535 * that we can track the strings,
1536 * etc. allocated in the subsection.
1539 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1542 subcs->item.filename = cs->item.filename;
1543 subcs->item.lineno = cs->item.lineno;
1544 cf_item_add(cs, &(subcs->item));
1547 cf_section_parse_init(subcs, (uint8_t *)base + variables[i].offset,
1548 (CONF_PARSER const *) variables[i].dflt);
1552 if ((variables[i].type != PW_TYPE_STRING) &&
1553 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1554 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1558 if (variables[i].data) {
1559 *(char **) variables[i].data = NULL;
1561 *(char **) (((char *)base) + variables[i].offset) = NULL;
1565 } /* for all variables in the configuration section */
1569 static void cf_section_parse_warn(CONF_SECTION *cs)
1573 for (ci = cs->children; ci; ci = ci->next) {
1575 * Don't recurse on sections. We can only safely
1576 * check conf pairs at the same level as the
1577 * section that was just parsed.
1579 if (ci->type == CONF_ITEM_SECTION) continue;
1580 if (ci->type == CONF_ITEM_PAIR) {
1583 cp = cf_item_to_pair(ci);
1584 if (cp->parsed) continue;
1586 WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
1587 cp->item.filename ? cp->item.filename : "unknown",
1588 cp->item.lineno ? cp->item.lineno : 0,
1593 * Skip everything else.
1599 * Parse a configuration section into user-supplied variables.
1601 int cf_section_parse(CONF_SECTION *cs, void *base,
1602 CONF_PARSER const *variables)
1608 cs->variables = variables; /* this doesn't hurt anything */
1611 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1614 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1615 cs->name1, cs->name2);
1618 cf_section_parse_init(cs, base, variables);
1621 * Handle the known configuration parameters.
1623 for (i = 0; variables[i].name != NULL; i++) {
1625 * Handle subsections specially
1627 if (variables[i].type == PW_TYPE_SUBSECTION) {
1628 CONF_SECTION *subcs;
1630 subcs = cf_section_sub_find(cs, variables[i].name);
1632 * Default in this case is overloaded to mean a pointer
1633 * to the CONF_PARSER struct for the subsection.
1635 if (!variables[i].dflt || !subcs) {
1636 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1640 if (cf_section_parse(subcs, (uint8_t *)base + variables[i].offset,
1641 (CONF_PARSER const *) variables[i].dflt) < 0) goto error;
1643 } /* else it's a CONF_PAIR */
1645 if (variables[i].data) {
1646 data = variables[i].data; /* prefer this. */
1648 data = ((char *)base) + variables[i].offset;
1650 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1655 * Parse the pair we found, or a default value.
1657 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1660 * Be nice, and print the name of the new config item.
1662 if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1663 (variables[i + 1].data == variables[i].data)) {
1664 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1665 variables[i + 1].name);
1670 } /* for all variables in the configuration section */
1673 * Warn about items in the configuration which weren't
1674 * checked during parsing.
1676 if (rad_debug_lvl >= 3) cf_section_parse_warn(cs);
1678 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1685 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1691 * Check XLAT things in pass 2. But don't cache the xlat stuff anywhere.
1693 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1702 * Handle the known configuration parameters.
1704 for (i = 0; variables[i].name != NULL; i++) {
1709 * Handle subsections specially
1711 if (variables[i].type == PW_TYPE_SUBSECTION) {
1712 CONF_SECTION *subcs;
1713 subcs = cf_section_sub_find(cs, variables[i].name);
1715 if (cf_section_parse_pass2(subcs, (uint8_t *)base + variables[i].offset,
1716 (CONF_PARSER const *) variables[i].dflt) < 0) {
1720 } /* else it's a CONF_PAIR */
1723 * Figure out which data we need to fix.
1725 if (variables[i].data) {
1726 data = variables[i].data; /* prefer this. */
1728 data = ((char *)base) + variables[i].offset;
1733 cp = cf_pair_find(cs, variables[i].name);
1737 if (!cp || !cp->value || !data) continue;
1739 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1740 (cp->rhs_type != T_BARE_WORD)) continue;
1743 * Non-xlat expansions shouldn't have xlat!
1745 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1746 ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1748 * Ignore %{... in shared secrets.
1749 * They're never dynamically expanded.
1751 if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
1753 if (strstr(cp->value, "%{") != NULL) {
1754 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
1755 cp->item.filename ? cp->item.filename : "unknown",
1756 cp->item.lineno ? cp->item.lineno : 0);
1762 * Parse (and throw away) the xlat string.
1764 * FIXME: All of these should be converted from PW_TYPE_XLAT
1767 if ((variables[i].type & PW_TYPE_XLAT) != 0) {
1769 * xlat expansions should be parseable.
1771 value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
1774 slen = xlat_tokenize(cs, value, &xlat, &error);
1776 char *spaces, *text;
1779 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
1781 cf_log_err(&cp->item, "Failed parsing expanded string:");
1782 cf_log_err(&cp->item, "%s", text);
1783 cf_log_err(&cp->item, "%s^ %s", spaces, error);
1785 talloc_free(spaces);
1797 * Convert the LITERAL template to the actual
1800 if ((variables[i].type & PW_TYPE_TMPL) != 0) {
1803 slen = tmpl_afrom_str(cs, &vpt, cp->value, talloc_array_length(cp->value) - 1,
1805 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1807 error = fr_strerror();
1814 * Don't add default - update with new types.
1816 switch (vpt->type) {
1818 * All attributes should have been defined by this point.
1820 case TMPL_TYPE_ATTR_UNDEFINED:
1821 cf_log_err(&cp->item, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
1824 case TMPL_TYPE_LITERAL:
1825 case TMPL_TYPE_ATTR:
1826 case TMPL_TYPE_LIST:
1827 case TMPL_TYPE_DATA:
1828 case TMPL_TYPE_EXEC:
1829 case TMPL_TYPE_XLAT:
1830 case TMPL_TYPE_XLAT_STRUCT:
1833 case TMPL_TYPE_UNKNOWN:
1834 case TMPL_TYPE_REGEX:
1835 case TMPL_TYPE_REGEX_STRUCT:
1836 case TMPL_TYPE_NULL:
1840 talloc_free(*(vp_tmpl_t **)data);
1841 *(vp_tmpl_t **)data = vpt;
1845 * If the "multi" flag is set, check all of them.
1847 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
1848 cp = cf_pair_find_next(cs, cp, cp->attr);
1851 } /* for all variables in the configuration section */
1857 * Merge the template so everyting else "just works".
1859 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
1863 if (!cs || !template) return true;
1865 cs->template = NULL;
1868 * Walk over the template, adding its' entries to the
1869 * current section. But only if the entries don't
1870 * already exist in the current section.
1872 for (ci = template->children; ci; ci = ci->next) {
1873 if (ci->type == CONF_ITEM_PAIR) {
1874 CONF_PAIR *cp1, *cp2;
1877 * It exists, don't over-write it.
1879 cp1 = cf_item_to_pair(ci);
1880 if (cf_pair_find(cs, cp1->attr)) {
1885 * Create a new pair with all of the data
1888 cp2 = cf_pair_dup(cs, cp1);
1889 if (!cp2) return false;
1891 cp2->item.filename = cp1->item.filename;
1892 cp2->item.lineno = cp1->item.lineno;
1894 cf_item_add(cs, &(cp2->item));
1898 if (ci->type == CONF_ITEM_SECTION) {
1899 CONF_SECTION *subcs1, *subcs2;
1901 subcs1 = cf_item_to_section(ci);
1902 rad_assert(subcs1 != NULL);
1904 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
1907 * sub-sections get merged.
1909 if (!cf_template_merge(subcs2, subcs1)) {
1916 * Our section doesn't have a matching
1917 * sub-section. Copy it verbatim from
1920 subcs2 = cf_section_dup(cs, subcs1,
1921 cf_section_name1(subcs1), cf_section_name2(subcs1),
1923 if (!subcs2) return false;
1925 subcs2->item.filename = subcs1->item.filename;
1926 subcs2->item.lineno = subcs1->item.lineno;
1928 cf_item_add(cs, &(subcs2->item));
1932 /* ignore everything else */
1938 static char const *cf_local_file(char const *base, char const *filename,
1939 char *buffer, size_t bufsize)
1944 strlcpy(buffer, base, bufsize);
1946 p = strrchr(buffer, FR_DIR_SEP);
1947 if (!p) return filename;
1948 if (p[1]) { /* ./foo */
1952 dirsize = (p - buffer) + 1;
1954 if ((dirsize + strlen(filename)) >= bufsize) {
1958 strlcpy(p + 1, filename, bufsize - dirsize);
1965 * Read a part of the config file.
1967 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
1968 CONF_SECTION *current)
1971 CONF_SECTION *this, *css;
1980 FR_TOKEN t1 = T_INVALID, t2, t3;
1981 bool has_spaces = false;
1986 this = current; /* add items here */
1989 * Read, checking for line continuations ('\\' at EOL)
1996 * Get data, and remember if we are at EOF.
1998 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
2002 * We read the entire 8k worth of data: complain.
2003 * Note that we don't care if the last character
2004 * is \n: it's still forbidden. This means that
2005 * the maximum allowed length of text is 8k-1, which
2009 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
2010 ERROR("%s[%d]: Line too long",
2017 while (isspace((int) *ptr)) ptr++;
2020 memmove(cbuf, ptr, len - (ptr - cbuf));
2021 len -= (ptr - cbuf);
2026 * Not doing continuations: check for edge
2033 while (*ptr && isspace((int) *ptr)) ptr++;
2035 if (!*ptr || (*ptr == '#')) continue;
2037 } else if (at_eof || (len == 0)) {
2038 ERROR("%s[%d]: Continuation at EOF is illegal",
2044 * See if there's a continuation.
2047 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
2052 if ((len > 0) && (cbuf[len - 1] == '\\')) {
2054 * Check for "suppress spaces" magic.
2056 if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
2060 cbuf[len - 1] = '\0';
2072 * The parser is getting to be evil.
2074 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
2076 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
2080 if (ptr[0] == '%') {
2081 hack = rad_copy_variable(buf1, ptr);
2083 hack = rad_copy_string(buf1, ptr);
2086 ERROR("%s[%d]: Invalid expansion: %s",
2087 filename, *lineno, ptr);
2093 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2100 ERROR("%s[%d]: Invalid expansion: %s",
2101 filename, *lineno, ptr);
2105 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
2109 * The caller eats "name1 name2 {", and calls us
2110 * for the data inside of the section. So if we
2111 * receive a closing brace, then it must mean the
2112 * end of the section.
2114 if (t1 == T_RCBRACE) {
2115 if (this == current) {
2116 ERROR("%s[%d]: Too many closing braces",
2122 * Merge the template into the existing
2123 * section. This uses more memory, but
2124 * means that templates now work with
2125 * sub-sections, etc.
2127 if (!cf_template_merge(this, this->template)) {
2131 this = this->item.parent;
2132 goto check_for_more;
2135 if (t1 != T_BARE_WORD) goto skip_keywords;
2138 * Allow for $INCLUDE files
2140 * This *SHOULD* work for any level include.
2141 * I really really really hate this file. -cparker
2143 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2144 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2145 bool relative = true;
2147 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2149 ERROR("%s[%d]: Unexpected text after $INCLUDE",
2154 if (buf2[0] == '$') relative = false;
2156 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2, NULL);
2157 if (!value) return -1;
2159 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2162 value = cf_local_file(filename, value, buf3,
2165 ERROR("%s[%d]: Directories too deep.",
2172 #ifdef HAVE_DIRENT_H
2176 * Include ALL non-"dot" files in the directory.
2179 if (value[strlen(value) - 1] == '/') {
2182 struct stat stat_buf;
2184 DEBUG2("including files in directory %s", value );
2189 if (stat(value, &stat_buf) < 0) {
2190 ERROR("%s[%d]: Failed reading directory %s: %s",
2192 value, fr_syserror(errno));
2196 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2197 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
2198 "insecure configuration", filename, *lineno, value);
2202 dir = opendir(value);
2204 ERROR("%s[%d]: Error reading directory %s: %s",
2205 filename, *lineno, value,
2206 fr_syserror(errno));
2211 * Read the directory, ignoring "." files.
2213 while ((dp = readdir(dir)) != NULL) {
2216 if (dp->d_name[0] == '.') continue;
2219 * Check for valid characters
2221 for (p = dp->d_name; *p != '\0'; p++) {
2222 if (isalpha((int)*p) ||
2226 (*p == '.')) continue;
2229 if (*p != '\0') continue;
2231 snprintf(buf2, sizeof(buf2), "%s%s",
2233 if ((stat(buf2, &stat_buf) != 0) ||
2234 S_ISDIR(stat_buf.st_mode)) continue;
2236 * Read the file into the current
2237 * configuration section.
2239 if (cf_file_include(this, buf2) < 0) {
2247 { /* it was a normal file */
2248 if (buf1[1] == '-') {
2249 struct stat statbuf;
2251 if (stat(value, &statbuf) < 0) {
2252 WARN("Not including file %s: %s", value, fr_syserror(errno));
2257 if (cf_file_include(this, value) < 0) {
2262 } /* we were in an include */
2264 if (strcasecmp(buf1, "$template") == 0) {
2266 CONF_SECTION *parentcs, *templatecs;
2267 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2270 ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2274 parentcs = cf_top_section(current);
2276 templatecs = cf_section_sub_find(parentcs, "templates");
2278 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2282 ci = cf_reference_item(parentcs, templatecs, buf2);
2283 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2284 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2289 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2293 if (this->template) {
2294 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2298 this->template = cf_item_to_section(ci);
2303 * Ensure that the user can't add CONF_PAIRs
2304 * with 'internal' names;
2306 if (buf1[0] == '_') {
2307 ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2312 * Handle if/elsif specially.
2314 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2316 char const *error = NULL;
2318 CONF_SECTION *server;
2319 fr_cond_t *cond = NULL;
2322 * if / elsif MUST be inside of a
2323 * processing section, which MUST in turn
2324 * be inside of a "server" directive.
2326 if (!this->item.parent) {
2328 ERROR("%s[%d]: Invalid location for '%s'",
2329 filename, *lineno, buf1);
2334 * Can only have "if" in 3 named sections.
2336 server = this->item.parent;
2338 (strcmp(server->name1, "server") != 0) &&
2339 (strcmp(server->name1, "policy") != 0) &&
2340 (strcmp(server->name1, "instantiate") != 0)) {
2341 server = server->item.parent;
2342 if (!server) goto invalid_location;
2346 * Skip (...) to find the {
2348 slen = fr_condition_tokenize(this, cf_section_to_item(this), ptr, &cond,
2349 &error, FR_COND_TWO_PASS);
2350 memcpy(&p, &ptr, sizeof(p));
2353 if (p[-slen] != '{') goto cond_error;
2359 * This hack is so that the NEXT stage
2360 * doesn't go "too far" in expanding the
2361 * variable. We can parse the conditions
2362 * without expanding the ${...} stuff.
2363 * BUT we don't want to expand all of the
2364 * stuff AFTER the condition. So we do
2367 * The first pass is to discover the end
2368 * of the condition. We then expand THAT
2369 * string, and do a second pass parsing
2370 * the expanded condition.
2376 * If there's a ${...}. If so, expand it.
2378 if (strchr(ptr, '$') != NULL) {
2379 ptr = cf_expand_variables(filename, lineno,
2384 ERROR("%s[%d]: Parse error expanding ${...} in condition",
2388 } /* else leave it alone */
2390 css = cf_section_alloc(this, buf1, ptr);
2392 ERROR("%s[%d]: Failed allocating memory for section",
2396 css->item.filename = filename;
2397 css->item.lineno = *lineno;
2399 slen = fr_condition_tokenize(css, cf_section_to_item(css), ptr, &cond,
2400 &error, FR_COND_TWO_PASS);
2401 *p = '{'; /* put it back */
2405 char *spaces, *text;
2407 fr_canonicalize_error(this, &spaces, &text, slen, ptr);
2409 ERROR("%s[%d]: Parse error in condition",
2411 ERROR("%s[%d]: %s", filename, *lineno, text);
2412 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2414 talloc_free(spaces);
2420 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2422 ERROR("%s[%d]: Condition is too large after \"%s\"",
2423 filename, *lineno, buf1);
2428 * Copy the expanded and parsed condition
2429 * into buf2. Then, parse the text after
2430 * the condition, which now MUST be a '{.
2432 * If it wasn't '{' it would have been
2433 * caught in the first pass of
2434 * conditional parsing, above.
2436 memcpy(buf2, ptr, slen);
2440 if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2442 ERROR("%s[%d]: Expected '{' %d",
2443 filename, *lineno, t3);
2448 * Swap the condition with trailing stuff for
2449 * the final condition.
2451 memcpy(&p, &css->name2, sizeof(css->name2));
2453 css->name2 = talloc_typed_strdup(css, buf2);
2455 cf_item_add(this, &(css->item));
2456 cf_data_add_internal(css, "if", cond, NULL, false);
2459 * The current section is now the child section.
2463 goto check_for_more;
2468 * Grab the next token.
2470 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2487 case T_OP_CMP_FALSE:
2488 if (!this || (strcmp(this->name1, "update") != 0)) {
2489 ERROR("%s[%d]: Invalid operator in assignment",
2497 while (isspace((int) *ptr)) ptr++;
2500 * New parser: non-quoted strings are
2501 * bare words, and we parse everything
2502 * until the next newline, or the next
2503 * comma. If they have { or } in a bare
2504 * word, well... too bad.
2506 if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2507 && (*ptr != '`') && (*ptr != '/')) {
2508 const char *q = ptr;
2511 while (*q && (*q >= ' ') && (*q != ',') &&
2514 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2515 ERROR("%s[%d]: Parse error: value too long",
2520 memcpy(buf3, ptr, (q - ptr));
2521 buf3[q - ptr] = '\0';
2525 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2528 if (t3 == T_INVALID) {
2529 ERROR("%s[%d]: Parse error: %s",
2536 * Allow "foo" by itself, or "foo = bar"
2542 case T_DOUBLE_QUOTED_STRING:
2543 case T_BACK_QUOTED_STRING:
2544 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3, &soft_fail);
2546 if (!soft_fail) return -1;
2549 * References an item which doesn't exist,
2550 * or which is already marked up as being
2551 * expanded in pass2. Wait for pass2 to
2552 * do the expansions.
2570 * Add this CONF_PAIR to our CONF_SECTION
2573 cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2574 if (!cpn) return -1;
2575 cpn->item.filename = filename;
2576 cpn->item.lineno = *lineno;
2578 cf_item_add(this, &(cpn->item));
2581 * Hacks for escaping
2583 if (!cf_new_escape && !this->item.parent && value &&
2584 (strcmp(buf1, "correct_escapes") == 0) &&
2585 ((strcmp(value, "true") == 0) ||
2586 (strcmp(value, "yes") == 0) ||
2587 (strcmp(value, "1") == 0))) {
2588 cf_new_escape = true;
2592 * Require a comma, unless there's a comment.
2594 while (isspace(*ptr)) ptr++;
2603 * foo = bar # other stuff
2605 if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2607 if (!*ptr || (*ptr == '}')) break;
2609 ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2610 filename, *lineno, value, ptr);
2614 * No '=', must be a section or sub-section.
2617 case T_DOUBLE_QUOTED_STRING:
2618 case T_SINGLE_QUOTED_STRING:
2619 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2620 if (t3 != T_LCBRACE) {
2621 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2622 filename, *lineno, buf1, buf2);
2628 css = cf_section_alloc(this, buf1,
2629 t2 == T_LCBRACE ? NULL : buf2);
2631 ERROR("%s[%d]: Failed allocating memory for section",
2636 css->item.filename = filename;
2637 css->item.lineno = *lineno;
2638 cf_item_add(this, &(css->item));
2641 * There may not be a name2
2643 css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2646 * The current section is now the child section.
2652 ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2657 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2658 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2665 * Done parsing one thing. Skip to EOL if possible.
2667 while (isspace(*ptr)) ptr++;
2669 if (*ptr == '#') continue;
2678 * See if EOF was unexpected ..
2680 if (feof(fp) && (this != current)) {
2681 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2682 filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2690 * Include one config file in another.
2692 int cf_file_include(CONF_SECTION *cs, char const *filename_in)
2696 struct stat statbuf;
2699 char const *filename;
2702 * So we only need to do this once.
2704 filename = talloc_strdup(cs, filename_in);
2706 DEBUG2("including configuration file %s", filename);
2708 fp = fopen(filename, "r");
2710 ERROR("Unable to open file \"%s\": %s",
2711 filename, fr_syserror(errno));
2715 if (stat(filename, &statbuf) == 0) {
2717 if ((statbuf.st_mode & S_IWOTH) != 0) {
2719 ERROR("Configuration file %s is globally writable. "
2720 "Refusing to start due to insecure configuration.", filename);
2726 if (cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT)) {
2728 ERROR("Cannot include the same file twice: \"%s\"", filename);
2734 * Add the filename to the section
2736 mtime = talloc(cs, time_t);
2737 *mtime = statbuf.st_mtime;
2739 if (cf_data_add_internal(cs, filename, mtime, NULL, PW_TYPE_FILE_INPUT) < 0) {
2741 ERROR("Internal error opening file \"%s\"",
2746 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT);
2749 ERROR("Internal error opening file \"%s\"",
2754 if (!cs->item.filename) cs->item.filename = filename;
2757 * Read the section. It's OK to have EOF without a
2758 * matching close brace.
2760 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
2771 * Do variable expansion in pass2.
2773 * This is a breadth-first expansion. "deep
2775 static int cf_section_pass2(CONF_SECTION *cs)
2779 for (ci = cs->children; ci; ci = ci->next) {
2784 if (ci->type != CONF_ITEM_PAIR) continue;
2786 cp = cf_item_to_pair(ci);
2787 if (!cp->value || !cp->pass2) continue;
2789 rad_assert((cp->rhs_type == T_BARE_WORD) ||
2790 (cp->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2791 (cp->rhs_type == T_BACK_QUOTED_STRING));
2793 value = cf_expand_variables(ci->filename, &ci->lineno, cs, buffer, sizeof(buffer), cp->value, NULL);
2794 if (!value) return -1;
2796 rad_const_free(cp->value);
2797 cp->value = talloc_typed_strdup(cp, value);
2800 for (ci = cs->children; ci; ci = ci->next) {
2801 if (ci->type != CONF_ITEM_SECTION) continue;
2803 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
2811 * Bootstrap a config file.
2813 int cf_file_read(CONF_SECTION *cs, char const *filename)
2818 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
2821 p = strrchr(cp->value, FR_DIR_SEP);
2824 cp->item.filename = "<internal>";
2825 cp->item.lineno = -1;
2826 cf_item_add(cs, &(cp->item));
2828 if (cf_file_include(cs, filename) < 0) return -1;
2831 * Now that we've read the file, go back through it and
2832 * expand the variables.
2834 if (cf_section_pass2(cs) < 0) return -1;
2840 void cf_file_free(CONF_SECTION *cs)
2847 * Return a CONF_PAIR within a CONF_SECTION.
2849 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
2851 CONF_PAIR *cp, mycp;
2853 if (!cs || !name) return NULL;
2856 cp = rbtree_finddata(cs->pair_tree, &mycp);
2859 if (!cs->template) return NULL;
2861 return rbtree_finddata(cs->template->pair_tree, &mycp);
2865 * Return the attr of a CONF_PAIR
2868 char const *cf_pair_attr(CONF_PAIR const *pair)
2870 return (pair ? pair->attr : NULL);
2874 * Return the value of a CONF_PAIR
2877 char const *cf_pair_value(CONF_PAIR const *pair)
2879 return (pair ? pair->value : NULL);
2882 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
2884 return (pair ? pair->op : T_INVALID);
2887 /** Return the value (lhs) type
2889 * @param pair to extract value type from.
2890 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2891 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
2893 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
2895 return (pair ? pair->lhs_type : T_INVALID);
2898 /** Return the value (rhs) type
2900 * @param pair to extract value type from.
2901 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2902 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
2904 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
2906 return (pair ? pair->rhs_type : T_INVALID);
2910 * Turn a CONF_PAIR into a VALUE_PAIR
2911 * For now, ignore the "value_type" field...
2913 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
2916 fr_strerror_printf("Internal error");
2921 fr_strerror_printf("No value given for attribute %s", pair->attr);
2926 * false comparisons never match. BUT if it's a "string"
2927 * or `string`, then remember to expand it later.
2929 if ((pair->op != T_OP_CMP_FALSE) &&
2930 ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2931 (pair->rhs_type == T_BACK_QUOTED_STRING))) {
2934 vp = pairmake(pair, NULL, pair->attr, NULL, pair->op);
2939 if (pairmark_xlat(vp, pair->value) < 0) {
2948 return pairmake(pair, NULL, pair->attr, pair->value, pair->op);
2952 * Return the first label of a CONF_SECTION
2955 char const *cf_section_name1(CONF_SECTION const *cs)
2957 return (cs ? cs->name1 : NULL);
2961 * Return the second label of a CONF_SECTION
2964 char const *cf_section_name2(CONF_SECTION const *cs)
2966 return (cs ? cs->name2 : NULL);
2969 /** Return name2 if set, else name1
2972 char const *cf_section_name(CONF_SECTION const *cs)
2976 name = cf_section_name2(cs);
2977 if (name) return name;
2979 return cf_section_name1(cs);
2983 * Find a value in a CONF_SECTION
2985 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
2989 cp = cf_pair_find(cs, attr);
2991 return (cp ? cp->value : NULL);
2995 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
2996 char const *name1, char const *name2)
2999 CONF_ITEM const *ci;
3001 if (!cs || !name1) return NULL;
3003 for (ci = &(cs->item); ci; ci = ci->next) {
3004 if (ci->type != CONF_ITEM_SECTION)
3007 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
3011 their2 = cf_item_to_section(ci)->name2;
3013 if ((!name2 && !their2) ||
3014 (name2 && their2 && (strcmp(name2, their2) == 0))) {
3015 return cf_item_to_section(ci);
3022 /** Find a pair with a name matching attr, after specified pair.
3024 * @param cs to search in.
3025 * @param pair to search from (may be NULL).
3026 * @param attr to find (may be NULL in which case any attribute matches).
3027 * @return the next matching CONF_PAIR or NULL if none matched.
3029 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
3030 CONF_PAIR const *pair, char const *attr)
3034 if (!cs) return NULL;
3037 * If pair is NULL and we're trying to find a specific
3038 * attribute this must be a first time run.
3040 * Find the pair with correct name.
3042 if (!pair && attr) return cf_pair_find(cs, attr);
3045 * Start searching from the next child, or from the head
3046 * of the list of children (if no pair was provided).
3048 for (ci = pair ? pair->item.next : cs->children;
3051 if (ci->type != CONF_ITEM_PAIR) continue;
3053 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
3056 return cf_item_to_pair(ci);
3060 * Find a CONF_SECTION, or return the root if name is NULL
3063 CONF_SECTION *cf_section_find(char const *name)
3066 return cf_section_sub_find(root_config, name);
3071 /** Find a sub-section in a section
3073 * This finds ANY section having the same first name.
3074 * The second name is ignored.
3076 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
3080 if (!cs || !name) return NULL; /* can't find an un-named section */
3083 * No sub-sections have been defined, so none exist.
3085 if (!cs->section_tree) return NULL;
3089 return rbtree_finddata(cs->section_tree, &mycs);
3093 /** Find a CONF_SECTION with both names.
3096 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
3097 char const *name1, char const *name2)
3101 if (!cs) cs = root_config;
3102 if (!cs) return NULL;
3105 CONF_SECTION mycs, *master_cs;
3107 if (!cs->section_tree) return NULL;
3112 master_cs = rbtree_finddata(cs->section_tree, &mycs);
3113 if (!master_cs) return NULL;
3116 * Look it up in the name2 tree. If it's there,
3119 if (master_cs->name2_tree) {
3120 CONF_SECTION *subcs;
3122 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
3123 if (subcs) return subcs;
3127 * We don't insert ourselves into the name2 tree.
3128 * So if there's nothing in the name2 tree, maybe
3129 * *we* are the answer.
3131 if (!master_cs->name2 && name2) return NULL;
3132 if (master_cs->name2 && !name2) return NULL;
3133 if (!master_cs->name2 && !name2) return master_cs;
3135 if (strcmp(master_cs->name2, name2) == 0) {
3143 * Else do it the old-fashioned way.
3145 for (ci = cs->children; ci; ci = ci->next) {
3146 CONF_SECTION *subcs;
3148 if (ci->type != CONF_ITEM_SECTION)
3151 subcs = cf_item_to_section(ci);
3152 if (!subcs->name2) {
3153 if (strcmp(subcs->name1, name2) == 0) break;
3155 if (strcmp(subcs->name2, name2) == 0) break;
3159 return cf_item_to_section(ci);
3163 * Return the next subsection after a CONF_SECTION
3164 * with a certain name1 (char *name1). If the requested
3165 * name1 is NULL, any name1 matches.
3168 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
3169 CONF_SECTION const *subsection,
3174 if (!section) return NULL;
3177 * If subsection is NULL this must be a first time run
3178 * Find the subsection with correct name
3182 ci = section->children;
3184 ci = subsection->item.next;
3187 for (; ci; ci = ci->next) {
3188 if (ci->type != CONF_ITEM_SECTION)
3190 if ((name1 == NULL) ||
3191 (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3195 return cf_item_to_section(ci);
3200 * Return the next section after a CONF_SECTION
3201 * with a certain name1 (char *name1). If the requested
3202 * name1 is NULL, any name1 matches.
3205 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3206 CONF_SECTION const *subsection,
3209 if (!section) return NULL;
3211 if (!section->item.parent) return NULL;
3213 return cf_subsection_find_next(section->item.parent, subsection, name1);
3216 /** Return the next item after a CONF_ITEM.
3219 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3221 if (!section) return NULL;
3224 * If item is NULL this must be a first time run
3225 * Return the first item
3228 return section->children;
3234 static void _pair_count(int *count, CONF_SECTION const *cs)
3236 CONF_ITEM const *ci;
3238 for (ci = cf_item_find_next(cs, NULL);
3240 ci = cf_item_find_next(cs, ci)) {
3242 if (cf_item_is_section(ci)) {
3243 _pair_count(count, cf_item_to_section(ci));
3251 /** Count the number of conf pairs beneath a section
3253 * @param[in] cs to search for items in.
3254 * @return number of pairs nested within section.
3256 int cf_pair_count(CONF_SECTION const *cs)
3260 _pair_count(&count, cs);
3265 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3267 if (!ci) return NULL;
3272 int cf_section_lineno(CONF_SECTION const *section)
3274 return section->item.lineno;
3277 char const *cf_pair_filename(CONF_PAIR const *pair)
3279 return pair->item.filename;
3282 char const *cf_section_filename(CONF_SECTION const *section)
3284 return section->item.filename;
3287 int cf_pair_lineno(CONF_PAIR const *pair)
3289 return pair->item.lineno;
3292 bool cf_item_is_section(CONF_ITEM const *item)
3294 return item->type == CONF_ITEM_SECTION;
3297 bool cf_item_is_pair(CONF_ITEM const *item)
3299 return item->type == CONF_ITEM_PAIR;
3303 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3304 void *data, void (*data_free)(void *))
3308 cd = talloc_zero(parent, CONF_DATA);
3309 if (!cd) return NULL;
3311 cd->item.type = CONF_ITEM_DATA;
3312 cd->item.parent = parent;
3313 cd->name = talloc_typed_strdup(cd, name);
3320 cd->free = data_free;
3323 talloc_set_destructor(cd, _cf_data_free);
3329 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3331 if (!cs || !name) return NULL;
3334 * Find the name in the tree, for speed.
3336 if (cs->data_tree) {
3341 return rbtree_finddata(cs->data_tree, &mycd);
3348 * Find data from a particular section.
3350 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3352 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3354 if (cd) return cd->data;
3360 * Add named data to a configuration section.
3362 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3363 void *data, void (*data_free)(void *),
3368 if (!cs || !name) return -1;
3371 * Already exists. Can't add it.
3373 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3375 cd = cf_data_alloc(cs, name, data, data_free);
3379 cf_item_add(cs, cf_data_to_item(cd));
3385 * Add named data to a configuration section.
3387 int cf_data_add(CONF_SECTION *cs, char const *name,
3388 void *data, void (*data_free)(void *))
3390 return cf_data_add_internal(cs, name, data, data_free, 0);
3393 /** Remove named data from a configuration section
3396 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3402 if (!cs || !name) return NULL;
3403 if (!cs->data_tree) return NULL;
3406 * Find the name in the tree, for speed.
3410 cd = rbtree_finddata(cs->data_tree, &mycd);
3411 if (!cd) return NULL;
3413 talloc_set_destructor(cd, NULL); /* Disarm the destructor */
3414 rbtree_deletebydata(cs->data_tree, &mycd);
3423 * This is here to make the rest of the code easier to read. It
3424 * ties conffile.c to log.c, but it means we don't have to
3425 * pollute every other function with the knowledge of the
3426 * configuration internals.
3428 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3434 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3439 ci->filename ? ci->filename : "unknown",
3440 ci->lineno ? ci->lineno : 0,
3443 ERROR("<unknown>[*]: %s", buffer);
3447 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3453 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3456 rad_assert(cs != NULL);
3459 cs->item.filename ? cs->item.filename : "unknown",
3460 cs->item.lineno ? cs->item.lineno : 0,
3464 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3470 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3473 rad_assert(cp != NULL);
3476 cp->item.filename ? cp->item.filename : "unknown",
3477 cp->item.lineno ? cp->item.lineno : 0,
3481 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3486 if ((rad_debug_lvl > 1) && cs) vradlog(L_DBG, fmt, ap);
3491 * Wrapper to simplify the code.
3493 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3499 if (rad_debug_lvl > 1 && cs) {
3500 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3502 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3507 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3509 if (!cs) return NULL;
3511 return cs->variables;
3515 * For "switch" and "case" statements.
3517 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3519 if (!cs) return T_INVALID;
3521 return cs->name2_type;