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 typedef struct cf_file_t {
121 char const *filename;
125 CONF_SECTION *root_config = NULL;
126 bool cf_new_escape = false;
129 static int cf_data_add_internal(CONF_SECTION *cs, char const *name, void *data,
130 void (*data_free)(void *), int flag);
132 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag);
134 static char const *cf_expand_variables(char const *cf, int *lineno,
135 CONF_SECTION *outercs,
136 char *output, size_t outsize,
137 char const *input, bool *soft_fail);
139 static int cf_file_include(CONF_SECTION *cs, char const *filename_in);
144 * Isolate the scary casts in these tiny provably-safe functions
147 /** Cast a CONF_ITEM to a CONF_PAIR
150 CONF_PAIR *cf_item_to_pair(CONF_ITEM const *ci)
154 if (ci == NULL) return NULL;
156 rad_assert(ci->type == CONF_ITEM_PAIR);
158 memcpy(&out, &ci, sizeof(out));
162 /** Cast a CONF_ITEM to a CONF_SECTION
165 CONF_SECTION *cf_item_to_section(CONF_ITEM const *ci)
169 if (ci == NULL) return NULL;
171 rad_assert(ci->type == CONF_ITEM_SECTION);
173 memcpy(&out, &ci, sizeof(out));
177 /** Cast a CONF_PAIR to a CONF_ITEM
180 CONF_ITEM *cf_pair_to_item(CONF_PAIR const *cp)
184 if (cp == NULL) return NULL;
186 memcpy(&out, &cp, sizeof(out));
190 /** Cast a CONF_SECTION to a CONF_ITEM
193 CONF_ITEM *cf_section_to_item(CONF_SECTION const *cs)
197 if (cs == NULL) return NULL;
199 memcpy(&out, &cs, sizeof(out));
203 /** Cast CONF_DATA to a CONF_ITEM
206 static CONF_ITEM *cf_data_to_item(CONF_DATA const *cd)
214 memcpy(&out, &cd, sizeof(out));
218 static int _cf_data_free(CONF_DATA *cd)
220 if (cd->free) cd->free(cd->data);
226 * rbtree callback function
228 static int pair_cmp(void const *a, void const *b)
230 CONF_PAIR const *one = a;
231 CONF_PAIR const *two = b;
233 return strcmp(one->attr, two->attr);
238 * rbtree callback function
240 static int section_cmp(void const *a, void const *b)
242 CONF_SECTION const *one = a;
243 CONF_SECTION const *two = b;
245 return strcmp(one->name1, two->name1);
250 * rbtree callback function
252 static int name2_cmp(void const *a, void const *b)
254 CONF_SECTION const *one = a;
255 CONF_SECTION const *two = b;
257 rad_assert(strcmp(one->name1, two->name1) == 0);
259 if (!one->name2 && !two->name2) return 0;
260 if (one->name2 && !two->name2) return -1;
261 if (!one->name2 && two->name2) return +1;
263 return strcmp(one->name2, two->name2);
268 * rbtree callback function
270 static int data_cmp(void const *a, void const *b)
274 CONF_DATA const *one = a;
275 CONF_DATA const *two = b;
277 rcode = one->flag - two->flag;
278 if (rcode != 0) return rcode;
280 return strcmp(one->name, two->name);
284 * Functions for tracking filenames.
286 static int filename_cmp(void const *a, void const *b)
288 cf_file_t const *one = a;
289 cf_file_t const *two = b;
291 if (one->buf.st_dev < two->buf.st_dev) return -1;
292 if (one->buf.st_dev > two->buf.st_dev) return +1;
294 if (one->buf.st_ino < two->buf.st_ino) return -1;
295 if (one->buf.st_ino > two->buf.st_ino) return +1;
300 static FILE *cf_file_open(CONF_SECTION *cs, char const *filename)
309 top = cf_top_section(cs);
310 cd = cf_data_find_internal(top, "filename", 0);
311 if (!cd) return NULL;
315 fp = fopen(filename, "r");
317 ERROR("Unable to open file \"%s\": %s",
318 filename, fr_syserror(errno));
324 file = talloc(tree, cf_file_t);
329 file->filename = filename;
331 if (fstat(fd, &file->buf) == 0) {
333 if ((file->buf.st_mode & S_IWOTH) != 0) {
335 ERROR("Configuration file %s is globally writable. "
336 "Refusing to start due to insecure configuration.", filename);
342 if (!rbtree_insert(tree, file)) {
343 ERROR("Cannot include the same file twice: \"%s\"", filename);
352 static int _cf_section_free(CONF_SECTION *cs)
355 * Name1 and name2 are allocated contiguous with
359 rbtree_free(cs->pair_tree);
360 cs->pair_tree = NULL;
362 if (cs->section_tree) {
363 rbtree_free(cs->section_tree);
364 cs->section_tree = NULL;
366 if (cs->name2_tree) {
367 rbtree_free(cs->name2_tree);
368 cs->name2_tree = NULL;
371 rbtree_free(cs->data_tree);
372 cs->data_tree = NULL;
378 /** Allocate a CONF_PAIR
380 * @param parent CONF_SECTION to hang this CONF_PAIR off of.
382 * @param value of CONF_PAIR.
383 * @param op T_OP_EQ, T_OP_SET etc.
384 * @param lhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
385 * @param rhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
386 * @return NULL on error, else a new CONF_SECTION parented by parent.
388 CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value,
389 FR_TOKEN op, FR_TOKEN lhs_type, FR_TOKEN rhs_type)
393 rad_assert(fr_equality_op[op] || fr_assignment_op[op]);
394 if (!attr) return NULL;
396 cp = talloc_zero(parent, CONF_PAIR);
397 if (!cp) return NULL;
399 cp->item.type = CONF_ITEM_PAIR;
400 cp->item.parent = parent;
401 cp->lhs_type = lhs_type;
402 cp->rhs_type = rhs_type;
405 cp->attr = talloc_typed_strdup(cp, attr);
413 cp->value = talloc_typed_strdup(cp, value);
414 if (!cp->value) goto error;
420 /** Duplicate a CONF_PAIR
422 * @param parent to allocate new pair in.
423 * @param cp to duplicate.
424 * @return NULL on error, else a duplicate of the input pair.
426 CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp)
433 new = cf_pair_alloc(parent, cp->attr, cf_pair_value(cp),
434 cp->op, cp->lhs_type, cp->rhs_type);
435 if (!new) return NULL;
437 new->parsed = cp->parsed;
438 new->item.lineno = cp->item.lineno;
441 * Avoid mallocs if possible.
443 if (!cp->item.filename || (strcmp(parent->item.filename, cp->item.filename) == 0)) {
444 new->item.filename = parent->item.filename;
446 new->item.filename = talloc_strdup(new, cp->item.filename);
452 /** Add a configuration pair to a section
454 * @param parent section to add pair to.
457 void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp)
459 cf_item_add(parent, cf_pair_to_item(cp));
462 /** Allocate a CONF_SECTION
464 * @param parent CONF_SECTION to hang this CONF_SECTION off of.
465 * @param name1 Primary name.
466 * @param name2 Secondary name.
467 * @return NULL on error, else a new CONF_SECTION parented by parent.
469 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
474 if (!name1) return NULL;
476 if (name2 && parent) {
477 if (strchr(name2, '$')) {
478 name2 = cf_expand_variables(parent->item.filename,
479 &parent->item.lineno,
481 buffer, sizeof(buffer), name2, NULL);
483 ERROR("Failed expanding section name");
489 cs = talloc_zero(parent, CONF_SECTION);
490 if (!cs) return NULL;
492 cs->item.type = CONF_ITEM_SECTION;
493 cs->item.parent = parent;
495 cs->name1 = talloc_typed_strdup(cs, name1);
503 cs->name2 = talloc_typed_strdup(cs, name2);
504 if (!cs->name2) goto error;
507 cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
508 if (!cs->pair_tree) goto error;
510 talloc_set_destructor(cs, _cf_section_free);
513 * Don't create a data tree, it may not be needed.
517 * Don't create the section tree here, it may not
521 if (parent) cs->depth = parent->depth + 1;
526 /** Duplicate a configuration section
528 * @note recursively duplicates any child sections.
529 * @note does not duplicate any data associated with a section, or its child sections.
531 * @param parent section (may be NULL).
532 * @param cs to duplicate.
533 * @param name1 of new section.
534 * @param name2 of new section.
535 * @param copy_meta Copy additional meta data for a section (like template, base, depth and variables).
536 * @return a duplicate of the existing section, or NULL on error.
538 CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs,
539 char const *name1, char const *name2, bool copy_meta)
541 CONF_SECTION *new, *subcs;
545 new = cf_section_alloc(parent, name1, name2);
548 new->template = cs->template;
549 new->base = cs->base;
550 new->depth = cs->depth;
551 new->variables = cs->variables;
554 new->item.lineno = cs->item.lineno;
556 if (!cs->item.filename || (parent && (strcmp(parent->item.filename, cs->item.filename) == 0))) {
557 new->item.filename = parent->item.filename;
559 new->item.filename = talloc_strdup(new, cs->item.filename);
562 for (ci = cs->children; ci; ci = ci->next) {
564 case CONF_ITEM_SECTION:
565 subcs = cf_item_to_section(ci);
566 subcs = cf_section_dup(new, subcs,
567 cf_section_name1(subcs), cf_section_name2(subcs),
573 cf_section_add(new, subcs);
577 cp = cf_pair_dup(new, cf_item_to_pair(ci));
582 cf_pair_add(new, cp);
585 case CONF_ITEM_DATA: /* Skip data */
588 case CONF_ITEM_INVALID:
596 void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
598 cf_item_add(parent, &(cs->item));
601 /** Replace pair in a given section with a new pair, of the given value.
603 * @param cs to replace pair in.
604 * @param cp to replace.
605 * @param value New value to assign to cp.
606 * @return 0 on success, -1 on failure.
608 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
611 CONF_ITEM *ci, *cn, **last;
613 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->lhs_type, cp->rhs_type);
614 if (!newp) return -1;
620 * Find the old one from the linked list, and replace it
623 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
625 cn->next = (*last)->next;
632 rbtree_deletebydata(cs->pair_tree, ci);
634 rbtree_insert(cs->pair_tree, cn);
641 * Add an item to a configuration section.
643 void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
646 CONF_ITEM *first = ci;
649 rad_assert((void *)cs != (void *)ci);
651 if (!cs || !ci) return;
654 rad_assert(cs->tail == NULL);
657 rad_assert(cs->tail != NULL);
662 * Update the trees (and tail) for each item added.
664 for (/* nothing */; ci != NULL; ci = ci->next) {
665 rad_assert(ci->next != first); /* simple cycle detection */
670 * For fast lookups, pairs and sections get
675 if (!rbtree_insert(cs->pair_tree, ci)) {
676 CONF_PAIR *cp = cf_item_to_pair(ci);
678 if (strcmp(cp->attr, "confdir") == 0) break;
679 if (!cp->value) break; /* module name, "ok", etc. */
683 case CONF_ITEM_SECTION: {
684 CONF_SECTION *cs_new = cf_item_to_section(ci);
685 CONF_SECTION *name1_cs;
687 if (!cs->section_tree) {
688 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
689 if (!cs->section_tree) {
690 ERROR("Out of memory");
695 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
697 if (!rbtree_insert(cs->section_tree, cs_new)) {
698 ERROR("Failed inserting section into tree");
705 * We already have a section of
706 * this "name1". Add a new
707 * sub-section based on name2.
709 if (!name1_cs->name2_tree) {
710 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
711 if (!name1_cs->name2_tree) {
712 ERROR("Out of memory");
718 * We don't care if this fails.
719 * If the user tries to create
720 * two sections of the same
721 * name1/name2, the duplicate
722 * section is just silently
725 rbtree_insert(name1_cs->name2_tree, cs_new);
727 } /* was a section */
730 if (!cs->data_tree) {
731 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
734 rbtree_insert(cs->data_tree, ci);
738 default: /* FIXME: assert & error! */
741 } /* switch over conf types */
746 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
747 CONF_SECTION *outercs,
752 CONF_SECTION const *cs = outercs;
756 if (!cs) goto no_such_item;
758 strlcpy(name, ptr, sizeof(name));
762 * ".foo" means "foo from the current section"
768 * Just '.' means the current section
771 return cf_section_to_item(cs);
775 * ..foo means "foo from the section
776 * enclosing this section" (etc.)
779 if (cs->item.parent) {
780 cs = cs->item.parent;
784 * .. means the section
785 * enclosing this section
788 return cf_section_to_item(cs);
793 * "foo.bar.baz" means "from the root"
795 } else if (strchr(p, '.') != NULL) {
796 if (!parentcs) goto no_such_item;
808 if (r && q > r) q = NULL;
809 if (q && q < r) r = NULL;
815 q = strchr(r + 1, ']');
816 if (!q) return NULL; /* parse error */
819 * Points to foo[bar]xx: parse error,
820 * it should be foo[bar] or foo[bar].baz
822 if (q[1] && q[1] != '.') goto no_such_item;
826 next = cf_section_sub_find_name2(cs, p, r + 1);
831 * Points to a named instance of a section.
834 if (!next) goto no_such_item;
835 return &(next->item);
838 q++; /* ensure we skip the ']' and '.' */
842 next = cf_section_sub_find(cs, p);
846 if (!next) break; /* it MAY be a pair in this section! */
852 if (!*p) goto no_such_item;
856 * Find it in the current referenced
859 cp = cf_pair_find(cs, p);
861 cp->parsed = true; /* conf pairs which are referenced count as parsed */
865 next = cf_section_sub_find(cs, p);
866 if (next) return &(next->item);
869 * "foo" is "in the current section, OR in main".
871 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
881 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
883 if (!cs) return NULL;
885 while (cs->item.parent != NULL) {
886 cs = cs->item.parent;
894 * Expand the variables in an input string.
896 static char const *cf_expand_variables(char const *cf, int *lineno,
897 CONF_SECTION *outercs,
898 char *output, size_t outsize,
899 char const *input, bool *soft_fail)
902 char const *end, *ptr;
903 CONF_SECTION const *parentcs;
906 if (soft_fail) *soft_fail = false;
909 * Find the master parent conf section.
910 * We can't use main_config.config, because we're in the
911 * process of re-building it, and it isn't set up yet...
913 parentcs = cf_top_section(outercs);
919 * Ignore anything other than "${"
921 if ((*ptr == '$') && (ptr[1] == '{')) {
927 * FIXME: Add support for ${foo:-bar},
932 * Look for trailing '}', and log a
933 * warning for anything that doesn't match,
934 * and exit with a fatal error.
936 end = strchr(ptr, '}');
939 INFO("%s[%d]: Variable expansion missing }",
947 * Can't really happen because input lines are
948 * capped at 8k, which is sizeof(name)
950 if ((size_t) (end - ptr) >= sizeof(name)) {
951 ERROR("%s[%d]: Reference string is too large",
956 memcpy(name, ptr, end - ptr);
957 name[end - ptr] = '\0';
959 q = strchr(name, ':');
964 ci = cf_reference_item(parentcs, outercs, name);
966 if (soft_fail) *soft_fail = true;
967 ERROR("%s[%d]: Reference \"%s\" not found", cf, *lineno, input);
972 * The expansion doesn't refer to another item or section
973 * it's the property of a section.
976 CONF_SECTION *mycs = cf_item_to_section(ci);
978 if (ci->type != CONF_ITEM_SECTION) {
979 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
983 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
984 case CONF_PROPERTY_NAME:
985 strcpy(p, mycs->name1);
988 case CONF_PROPERTY_INSTANCE:
989 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
993 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
999 } else if (ci->type == CONF_ITEM_PAIR) {
1001 * Substitute the value of the variable.
1003 cp = cf_item_to_pair(ci);
1006 * If the thing we reference is
1007 * marked up as being expanded in
1008 * pass2, don't expand it now.
1009 * Let it be expanded in pass2.
1012 if (soft_fail) *soft_fail = true;
1014 ERROR("%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
1015 cf, *lineno, input);
1020 ERROR("%s[%d]: Reference \"%s\" has no value",
1021 cf, *lineno, input);
1025 if (p + strlen(cp->value) >= output + outsize) {
1026 ERROR("%s[%d]: Reference \"%s\" is too long",
1027 cf, *lineno, input);
1031 strcpy(p, cp->value);
1035 } else if (ci->type == CONF_ITEM_SECTION) {
1036 CONF_SECTION *subcs;
1039 * Adding an entry again to a
1040 * section is wrong. We don't
1041 * want an infinite loop.
1043 if (ci->parent == outercs) {
1044 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
1049 * Copy the section instead of
1052 subcs = cf_item_to_section(ci);
1053 subcs = cf_section_dup(outercs, subcs,
1054 cf_section_name1(subcs), cf_section_name2(subcs),
1057 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
1061 subcs->item.filename = ci->filename;
1062 subcs->item.lineno = ci->lineno;
1063 cf_item_add(outercs, &(subcs->item));
1068 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
1071 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
1077 * Look for trailing '}', and log a
1078 * warning for anything that doesn't match,
1079 * and exit with a fatal error.
1081 end = strchr(ptr, '}');
1084 INFO("%s[%d]: Environment variable expansion missing }",
1090 * Can't really happen because input lines are
1091 * capped at 8k, which is sizeof(name)
1093 if ((size_t) (end - ptr) >= sizeof(name)) {
1094 ERROR("%s[%d]: Environment variable name is too large",
1099 memcpy(name, ptr, end - ptr);
1100 name[end - ptr] = '\0';
1103 * Get the environment variable.
1104 * If none exists, then make it an empty string.
1112 if (p + strlen(env) >= output + outsize) {
1113 ERROR("%s[%d]: Reference \"%s\" is too long",
1114 cf, *lineno, input);
1124 * Copy it over verbatim.
1130 if (p >= (output + outsize)) {
1131 ERROR("%s[%d]: Reference \"%s\" is too long",
1132 cf, *lineno, input);
1135 } /* loop over all of the input string. */
1142 static char const parse_spaces[] = " ";
1144 /** Validation function for ipaddr conffile types
1147 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1148 fr_ipaddr_t *ipaddr)
1152 if (strcmp(value, "*") == 0) {
1153 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1154 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1155 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1157 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1158 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1162 case PW_TYPE_IPV4_ADDR:
1163 case PW_TYPE_IPV6_ADDR:
1164 case PW_TYPE_COMBO_IP_ADDR:
1165 switch (ipaddr->af) {
1167 if (ipaddr->prefix != 32) {
1168 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
1176 if (ipaddr->prefix != 128) {
1177 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
1192 /** Parses a #CONF_PAIR into a C data type, with a default value.
1194 * Takes fields from a #CONF_PARSER struct and uses them to parse the string value
1195 * of a #CONF_PAIR into a C data type matching the type argument.
1197 * The format of the types are the same as #value_data_t types.
1199 * @note The dflt value will only be used if no matching #CONF_PAIR is found. Empty strings will not
1200 * result in the dflt value being used.
1202 * @param cs to search for matching #CONF_PAIR in.
1203 * @param name of #CONF_PAIR to search for.
1204 * @param type Data type to parse #CONF_PAIR value as.
1205 * Should be one of the following ``data`` types, and one or more of the following ``flag`` types or'd together:
1206 * - ``data`` #PW_TYPE_TMPL - @copybrief PW_TYPE_TMPL
1207 Feeds the value into #tmpl_afrom_str. Value can be
1208 * obtained when processing requests, with #tmpl_expand or #tmpl_aexpand.
1209 * - ``data`` #PW_TYPE_BOOLEAN - @copybrief PW_TYPE_BOOLEAN
1210 * - ``data`` #PW_TYPE_INTEGER - @copybrief PW_TYPE_INTEGER
1211 * - ``data`` #PW_TYPE_SHORT - @copybrief PW_TYPE_SHORT
1212 * - ``data`` #PW_TYPE_INTEGER64 - @copybrief PW_TYPE_INTEGER64
1213 * - ``data`` #PW_TYPE_SIGNED - @copybrief PW_TYPE_SIGNED
1214 * - ``data`` #PW_TYPE_STRING - @copybrief PW_TYPE_STRING
1215 * - ``data`` #PW_TYPE_IPV4_ADDR - @copybrief PW_TYPE_IPV4_ADDR (IPv4 address with prefix 32).
1216 * - ``data`` #PW_TYPE_IPV4_PREFIX - @copybrief PW_TYPE_IPV4_PREFIX (IPv4 address with variable prefix).
1217 * - ``data`` #PW_TYPE_IPV6_ADDR - @copybrief PW_TYPE_IPV6_ADDR (IPv6 address with prefix 128).
1218 * - ``data`` #PW_TYPE_COMBO_IP_ADDR - @copybrief PW_TYPE_COMBO_IP_ADDR (IPv4/IPv6 address with
1220 * - ``data`` #PW_TYPE_COMBO_IP_PREFIX - @copybrief PW_TYPE_COMBO_IP_PREFIX (IPv4/IPv6 address with
1222 * - ``data`` #PW_TYPE_TIMEVAL - @copybrief PW_TYPE_TIMEVAL
1223 * - ``flag`` #PW_TYPE_DEPRECATED - @copybrief PW_TYPE_DEPRECATED
1224 * - ``flag`` #PW_TYPE_REQUIRED - @copybrief PW_TYPE_REQUIRED
1225 * - ``flag`` #PW_TYPE_ATTRIBUTE - @copybrief PW_TYPE_ATTRIBUTE
1226 * - ``flag`` #PW_TYPE_SECRET - @copybrief PW_TYPE_SECRET
1227 * - ``flag`` #PW_TYPE_FILE_INPUT - @copybrief PW_TYPE_FILE_INPUT
1228 * - ``flag`` #PW_TYPE_NOT_EMPTY - @copybrief PW_TYPE_NOT_EMPTY
1229 * @param data Pointer to a global variable, or pointer to a field in the struct being populated with values.
1230 * @param dflt value to use, if no #CONF_PAIR is found.
1231 * @return -1 on error, -2 if deprecated, 0 on success (correctly parsed), 1 if default value was used.
1233 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1236 bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, multi;
1239 CONF_PAIR *cp = NULL;
1240 fr_ipaddr_t *ipaddr;
1245 deprecated = (type & PW_TYPE_DEPRECATED);
1246 required = (type & PW_TYPE_REQUIRED);
1247 attribute = (type & PW_TYPE_ATTRIBUTE);
1248 secret = (type & PW_TYPE_SECRET);
1249 file_input = (type == PW_TYPE_FILE_INPUT); /* check, not and */
1250 cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1251 tmpl = (type & PW_TYPE_TMPL);
1252 multi = (type & PW_TYPE_MULTI);
1254 if (attribute) required = true;
1255 if (required) cant_be_empty = true; /* May want to review this in the future... */
1258 * Everything except templates must have a base type.
1260 if (!(type & 0xff) && !tmpl) {
1261 cf_log_err(&(cs->item), "Configuration item '%s' must have a data type", name);
1265 type &= 0xff; /* normal types are small */
1269 cp = cf_pair_find(cs, name);
1272 * No pairs match the configuration item name in the current
1273 * section, use the default value.
1279 * Something matched, used the CONF_PAIR value.
1282 CONF_PAIR *next = cp;
1287 * @fixme We should actually validate
1288 * the value of the pairs too
1290 if (multi) while ((next = cf_pair_find_next(cs, next, name))) {
1291 next->parsed = true;
1299 cf_log_err(&(cs->item), "Configuration item '%s' must have a value", name);
1301 cf_log_err(&(cp->item), "Configuration item '%s' must have a value", name);
1308 if ((value[0] == '\0') && cant_be_empty) {
1311 cf_log_err(&(cs->item), "Configuration item '%s' must not be empty (zero length)", name);
1312 if (!required) cf_log_err(&(cs->item), "Comment item to silence this message");
1314 cf_log_err(&(cp->item), "Configuration item '%s' must not be empty (zero length)", name);
1315 if (!required) cf_log_err(&(cp->item), "Comment item to silence this message");
1321 cf_log_err(&(cs->item), "Configuration item \"%s\" is deprecated", name);
1327 * Process a value as a LITERAL template. Once all of
1328 * the attrs and xlats are defined, the pass2 code
1329 * converts it to the appropriate type.
1335 *(vp_tmpl_t **)data = NULL;
1339 rad_assert(!attribute);
1340 vpt = tmpl_alloc(cs, TMPL_TYPE_LITERAL, value, strlen(value));
1341 *(vp_tmpl_t **)data = vpt;
1347 case PW_TYPE_BOOLEAN:
1349 * Allow yes/no, true/false, and on/off
1351 if ((strcasecmp(value, "yes") == 0) ||
1352 (strcasecmp(value, "true") == 0) ||
1353 (strcasecmp(value, "on") == 0)) {
1354 *(bool *)data = true;
1355 } else if ((strcasecmp(value, "no") == 0) ||
1356 (strcasecmp(value, "false") == 0) ||
1357 (strcasecmp(value, "off") == 0)) {
1358 *(bool *)data = false;
1360 *(bool *)data = false;
1361 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1362 "variable %s", value, name);
1365 cf_log_info(cs, "%.*s\t%s = %s",
1366 cs->depth, parse_spaces, name, value);
1369 case PW_TYPE_INTEGER:
1371 unsigned long v = strtoul(value, 0, 0);
1374 * Restrict integer values to 0-INT32_MAX, this means
1375 * it will always be safe to cast them to a signed type
1376 * for comparisons, and imposes the same range limit as
1377 * before we switched to using an unsigned type to
1378 * represent config item integers.
1380 if (v > INT32_MAX) {
1381 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1386 *(uint32_t *)data = v;
1387 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1393 unsigned long v = strtoul(value, 0, 0);
1395 if (v > UINT16_MAX) {
1396 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1400 *(uint16_t *)data = (uint16_t) v;
1401 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1405 case PW_TYPE_INTEGER64:
1406 *(uint64_t *)data = strtoull(value, 0, 0);
1407 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1410 case PW_TYPE_SIGNED:
1411 *(int32_t *)data = strtol(value, 0, 0);
1412 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1415 case PW_TYPE_STRING:
1422 * Expand variables which haven't already been
1423 * expanded automagically when the configuration
1426 if (value == dflt) {
1429 lineno = cs->item.lineno;
1431 value = cf_expand_variables("<internal>",
1433 cs, buffer, sizeof(buffer),
1436 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1441 if (required && !value) goto is_required;
1442 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1445 if (!dict_attrbyname(value)) {
1447 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1450 cf_log_err(&(cp->item), "No such attribute '%s'", value);
1457 * Hide secrets when using "radiusd -X".
1459 if (secret && (rad_debug_lvl <= 2)) {
1460 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1461 cs->depth, parse_spaces, name);
1463 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1464 cs->depth, parse_spaces, name, value ? value : "(null)");
1466 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1469 * If there's data AND it's an input file, check
1470 * that we can read it. This check allows errors
1471 * to be caught as early as possible, during
1474 if (*q && file_input) {
1477 if (stat(*q, &buf) < 0) {
1478 char user[255], group[255];
1480 ERROR("Unable to open file \"%s\": %s", value, fr_syserror(errno));
1481 ERROR("Our effective user and group was %s:%s",
1482 (rad_prints_uid(NULL, user, sizeof(user), geteuid()) < 0) ?
1484 (rad_prints_gid(NULL, group, sizeof(group), getegid()) < 0) ?
1485 "unknown" : group );
1492 case PW_TYPE_IPV4_ADDR:
1493 case PW_TYPE_IPV4_PREFIX:
1496 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1497 ERROR("%s", fr_strerror());
1500 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1503 case PW_TYPE_IPV6_ADDR:
1504 case PW_TYPE_IPV6_PREFIX:
1507 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1508 ERROR("%s", fr_strerror());
1511 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1514 case PW_TYPE_COMBO_IP_ADDR:
1515 case PW_TYPE_COMBO_IP_PREFIX:
1518 if (fr_pton(ipaddr, value, -1, true) < 0) {
1519 ERROR("%s", fr_strerror());
1522 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1525 case PW_TYPE_TIMEVAL: {
1530 sec = strtoul(value, &end, 10);
1536 len = strlen(end + 1);
1539 ERROR("Too much precision for timeval");
1544 * If they write "0.1", that means
1545 * "10000" microseconds.
1547 sec = strtoul(end + 1, NULL, 10);
1555 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1556 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1557 memcpy(data, &tv, sizeof(tv));
1563 * If we get here, it's a sanity check error.
1564 * It's not an error parsing the configuration
1567 rad_assert(type > PW_TYPE_INVALID);
1568 rad_assert(type < PW_TYPE_MAX);
1570 ERROR("type '%s' is not supported in the configuration files",
1571 fr_int2str(dict_attr_types, type, "?Unknown?"));
1573 } /* switch over variable type */
1578 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1579 if (!cpn) return -1;
1581 cpn->item.filename = "<internal>";
1582 cpn->item.lineno = 0;
1583 cf_item_add(cs, &(cpn->item));
1591 * A copy of cf_section_parse that initializes pointers before
1594 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1595 CONF_PARSER const *variables)
1599 for (i = 0; variables[i].name != NULL; i++) {
1600 if (variables[i].type == PW_TYPE_SUBSECTION) {
1601 CONF_SECTION *subcs;
1603 if (!variables[i].dflt) continue;
1605 subcs = cf_section_sub_find(cs, variables[i].name);
1608 * If there's no subsection in the
1609 * config, BUT the CONF_PARSER wants one,
1610 * then create an empty one. This is so
1611 * that we can track the strings,
1612 * etc. allocated in the subsection.
1615 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1618 subcs->item.filename = cs->item.filename;
1619 subcs->item.lineno = cs->item.lineno;
1620 cf_item_add(cs, &(subcs->item));
1623 cf_section_parse_init(subcs, (uint8_t *)base + variables[i].offset,
1624 (CONF_PARSER const *) variables[i].dflt);
1628 if ((variables[i].type != PW_TYPE_STRING) &&
1629 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1630 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1634 if (variables[i].data) {
1635 *(char **) variables[i].data = NULL;
1637 *(char **) (((char *)base) + variables[i].offset) = NULL;
1641 } /* for all variables in the configuration section */
1645 static void cf_section_parse_warn(CONF_SECTION *cs)
1649 for (ci = cs->children; ci; ci = ci->next) {
1651 * Don't recurse on sections. We can only safely
1652 * check conf pairs at the same level as the
1653 * section that was just parsed.
1655 if (ci->type == CONF_ITEM_SECTION) continue;
1656 if (ci->type == CONF_ITEM_PAIR) {
1659 cp = cf_item_to_pair(ci);
1660 if (cp->parsed) continue;
1662 WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
1663 cp->item.filename ? cp->item.filename : "unknown",
1664 cp->item.lineno ? cp->item.lineno : 0,
1669 * Skip everything else.
1675 * Parse a configuration section into user-supplied variables.
1677 int cf_section_parse(CONF_SECTION *cs, void *base,
1678 CONF_PARSER const *variables)
1684 cs->variables = variables; /* this doesn't hurt anything */
1687 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1690 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1691 cs->name1, cs->name2);
1694 cf_section_parse_init(cs, base, variables);
1697 * Handle the known configuration parameters.
1699 for (i = 0; variables[i].name != NULL; i++) {
1701 * Handle subsections specially
1703 if (variables[i].type == PW_TYPE_SUBSECTION) {
1704 CONF_SECTION *subcs;
1706 subcs = cf_section_sub_find(cs, variables[i].name);
1708 * Default in this case is overloaded to mean a pointer
1709 * to the CONF_PARSER struct for the subsection.
1711 if (!variables[i].dflt || !subcs) {
1712 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1716 if (cf_section_parse(subcs, (uint8_t *)base + variables[i].offset,
1717 (CONF_PARSER const *) variables[i].dflt) < 0) goto error;
1719 } /* else it's a CONF_PAIR */
1721 if (variables[i].data) {
1722 data = variables[i].data; /* prefer this. */
1724 data = ((char *)base) + variables[i].offset;
1726 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1731 * Parse the pair we found, or a default value.
1733 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1736 * Be nice, and print the name of the new config item.
1738 if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1739 (variables[i + 1].data == variables[i].data)) {
1740 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1741 variables[i + 1].name);
1746 } /* for all variables in the configuration section */
1749 * Warn about items in the configuration which weren't
1750 * checked during parsing.
1752 if (rad_debug_lvl >= 3) cf_section_parse_warn(cs);
1754 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1761 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1767 * Check XLAT things in pass 2. But don't cache the xlat stuff anywhere.
1769 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1778 * Handle the known configuration parameters.
1780 for (i = 0; variables[i].name != NULL; i++) {
1785 * Handle subsections specially
1787 if (variables[i].type == PW_TYPE_SUBSECTION) {
1788 CONF_SECTION *subcs;
1789 subcs = cf_section_sub_find(cs, variables[i].name);
1791 if (cf_section_parse_pass2(subcs, (uint8_t *)base + variables[i].offset,
1792 (CONF_PARSER const *) variables[i].dflt) < 0) {
1796 } /* else it's a CONF_PAIR */
1799 * Figure out which data we need to fix.
1801 if (variables[i].data) {
1802 data = variables[i].data; /* prefer this. */
1804 data = ((char *)base) + variables[i].offset;
1809 cp = cf_pair_find(cs, variables[i].name);
1813 if (!cp || !cp->value || !data) continue;
1815 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1816 (cp->rhs_type != T_BARE_WORD)) continue;
1819 * Non-xlat expansions shouldn't have xlat!
1821 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1822 ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1824 * Ignore %{... in shared secrets.
1825 * They're never dynamically expanded.
1827 if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
1829 if (strstr(cp->value, "%{") != NULL) {
1830 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
1831 cp->item.filename ? cp->item.filename : "unknown",
1832 cp->item.lineno ? cp->item.lineno : 0);
1838 * Parse (and throw away) the xlat string.
1840 * FIXME: All of these should be converted from PW_TYPE_XLAT
1843 if ((variables[i].type & PW_TYPE_XLAT) != 0) {
1845 * xlat expansions should be parseable.
1847 value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
1850 slen = xlat_tokenize(cs, value, &xlat, &error);
1852 char *spaces, *text;
1855 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
1857 cf_log_err(&cp->item, "Failed parsing expanded string:");
1858 cf_log_err(&cp->item, "%s", text);
1859 cf_log_err(&cp->item, "%s^ %s", spaces, error);
1861 talloc_free(spaces);
1873 * Convert the LITERAL template to the actual
1876 if ((variables[i].type & PW_TYPE_TMPL) != 0) {
1879 slen = tmpl_afrom_str(cs, &vpt, cp->value, talloc_array_length(cp->value) - 1,
1881 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1883 error = fr_strerror();
1890 * Don't add default - update with new types.
1892 switch (vpt->type) {
1894 * All attributes should have been defined by this point.
1896 case TMPL_TYPE_ATTR_UNDEFINED:
1897 cf_log_err(&cp->item, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
1900 case TMPL_TYPE_LITERAL:
1901 case TMPL_TYPE_ATTR:
1902 case TMPL_TYPE_LIST:
1903 case TMPL_TYPE_DATA:
1904 case TMPL_TYPE_EXEC:
1905 case TMPL_TYPE_XLAT:
1906 case TMPL_TYPE_XLAT_STRUCT:
1909 case TMPL_TYPE_UNKNOWN:
1910 case TMPL_TYPE_REGEX:
1911 case TMPL_TYPE_REGEX_STRUCT:
1912 case TMPL_TYPE_NULL:
1916 talloc_free(*(vp_tmpl_t **)data);
1917 *(vp_tmpl_t **)data = vpt;
1921 * If the "multi" flag is set, check all of them.
1923 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
1924 cp = cf_pair_find_next(cs, cp, cp->attr);
1927 } /* for all variables in the configuration section */
1933 * Merge the template so everyting else "just works".
1935 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
1939 if (!cs || !template) return true;
1941 cs->template = NULL;
1944 * Walk over the template, adding its' entries to the
1945 * current section. But only if the entries don't
1946 * already exist in the current section.
1948 for (ci = template->children; ci; ci = ci->next) {
1949 if (ci->type == CONF_ITEM_PAIR) {
1950 CONF_PAIR *cp1, *cp2;
1953 * It exists, don't over-write it.
1955 cp1 = cf_item_to_pair(ci);
1956 if (cf_pair_find(cs, cp1->attr)) {
1961 * Create a new pair with all of the data
1964 cp2 = cf_pair_dup(cs, cp1);
1965 if (!cp2) return false;
1967 cp2->item.filename = cp1->item.filename;
1968 cp2->item.lineno = cp1->item.lineno;
1970 cf_item_add(cs, &(cp2->item));
1974 if (ci->type == CONF_ITEM_SECTION) {
1975 CONF_SECTION *subcs1, *subcs2;
1977 subcs1 = cf_item_to_section(ci);
1978 rad_assert(subcs1 != NULL);
1980 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
1983 * sub-sections get merged.
1985 if (!cf_template_merge(subcs2, subcs1)) {
1992 * Our section doesn't have a matching
1993 * sub-section. Copy it verbatim from
1996 subcs2 = cf_section_dup(cs, subcs1,
1997 cf_section_name1(subcs1), cf_section_name2(subcs1),
1999 if (!subcs2) return false;
2001 subcs2->item.filename = subcs1->item.filename;
2002 subcs2->item.lineno = subcs1->item.lineno;
2004 cf_item_add(cs, &(subcs2->item));
2008 /* ignore everything else */
2014 static char const *cf_local_file(char const *base, char const *filename,
2015 char *buffer, size_t bufsize)
2020 strlcpy(buffer, base, bufsize);
2022 p = strrchr(buffer, FR_DIR_SEP);
2023 if (!p) return filename;
2024 if (p[1]) { /* ./foo */
2028 dirsize = (p - buffer) + 1;
2030 if ((dirsize + strlen(filename)) >= bufsize) {
2034 strlcpy(p + 1, filename, bufsize - dirsize);
2041 * Read a part of the config file.
2043 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
2044 CONF_SECTION *current)
2047 CONF_SECTION *this, *css;
2056 FR_TOKEN t1 = T_INVALID, t2, t3;
2057 bool has_spaces = false;
2062 this = current; /* add items here */
2065 * Read, checking for line continuations ('\\' at EOL)
2072 * Get data, and remember if we are at EOF.
2074 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
2078 * We read the entire 8k worth of data: complain.
2079 * Note that we don't care if the last character
2080 * is \n: it's still forbidden. This means that
2081 * the maximum allowed length of text is 8k-1, which
2085 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
2086 ERROR("%s[%d]: Line too long",
2093 while (isspace((int) *ptr)) ptr++;
2096 memmove(cbuf, ptr, len - (ptr - cbuf));
2097 len -= (ptr - cbuf);
2102 * Not doing continuations: check for edge
2109 while (*ptr && isspace((int) *ptr)) ptr++;
2111 if (!*ptr || (*ptr == '#')) continue;
2113 } else if (at_eof || (len == 0)) {
2114 ERROR("%s[%d]: Continuation at EOF is illegal",
2120 * See if there's a continuation.
2123 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
2128 if ((len > 0) && (cbuf[len - 1] == '\\')) {
2130 * Check for "suppress spaces" magic.
2132 if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
2136 cbuf[len - 1] = '\0';
2148 * The parser is getting to be evil.
2150 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
2152 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
2156 if (ptr[0] == '%') {
2157 hack = rad_copy_variable(buf1, ptr);
2159 hack = rad_copy_string(buf1, ptr);
2162 ERROR("%s[%d]: Invalid expansion: %s",
2163 filename, *lineno, ptr);
2169 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2176 ERROR("%s[%d]: Invalid expansion: %s",
2177 filename, *lineno, ptr);
2181 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
2185 * The caller eats "name1 name2 {", and calls us
2186 * for the data inside of the section. So if we
2187 * receive a closing brace, then it must mean the
2188 * end of the section.
2190 if (t1 == T_RCBRACE) {
2191 if (this == current) {
2192 ERROR("%s[%d]: Too many closing braces",
2198 * Merge the template into the existing
2199 * section. This uses more memory, but
2200 * means that templates now work with
2201 * sub-sections, etc.
2203 if (!cf_template_merge(this, this->template)) {
2207 this = this->item.parent;
2208 goto check_for_more;
2211 if (t1 != T_BARE_WORD) goto skip_keywords;
2214 * Allow for $INCLUDE files
2216 * This *SHOULD* work for any level include.
2217 * I really really really hate this file. -cparker
2219 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2220 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2221 bool relative = true;
2223 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2225 ERROR("%s[%d]: Unexpected text after $INCLUDE",
2230 if (buf2[0] == '$') relative = false;
2232 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2, NULL);
2233 if (!value) return -1;
2235 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2238 value = cf_local_file(filename, value, buf3,
2241 ERROR("%s[%d]: Directories too deep.",
2248 #ifdef HAVE_DIRENT_H
2252 * Include ALL non-"dot" files in the directory.
2255 if (value[strlen(value) - 1] == '/') {
2258 struct stat stat_buf;
2260 DEBUG2("including files in directory %s", value );
2265 if (stat(value, &stat_buf) < 0) {
2266 ERROR("%s[%d]: Failed reading directory %s: %s",
2268 value, fr_syserror(errno));
2272 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2273 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
2274 "insecure configuration", filename, *lineno, value);
2278 dir = opendir(value);
2280 ERROR("%s[%d]: Error reading directory %s: %s",
2281 filename, *lineno, value,
2282 fr_syserror(errno));
2287 * Read the directory, ignoring "." files.
2289 while ((dp = readdir(dir)) != NULL) {
2292 if (dp->d_name[0] == '.') continue;
2295 * Check for valid characters
2297 for (p = dp->d_name; *p != '\0'; p++) {
2298 if (isalpha((int)*p) ||
2302 (*p == '.')) continue;
2305 if (*p != '\0') continue;
2307 snprintf(buf2, sizeof(buf2), "%s%s",
2309 if ((stat(buf2, &stat_buf) != 0) ||
2310 S_ISDIR(stat_buf.st_mode)) continue;
2312 * Read the file into the current
2313 * configuration section.
2315 if (cf_file_include(this, buf2) < 0) {
2323 { /* it was a normal file */
2324 if (buf1[1] == '-') {
2325 struct stat statbuf;
2327 if (stat(value, &statbuf) < 0) {
2328 WARN("Not including file %s: %s", value, fr_syserror(errno));
2333 if (cf_file_include(this, value) < 0) {
2338 } /* we were in an include */
2340 if (strcasecmp(buf1, "$template") == 0) {
2342 CONF_SECTION *parentcs, *templatecs;
2343 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2346 ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2350 parentcs = cf_top_section(current);
2352 templatecs = cf_section_sub_find(parentcs, "templates");
2354 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2358 ci = cf_reference_item(parentcs, templatecs, buf2);
2359 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2360 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2365 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2369 if (this->template) {
2370 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2374 this->template = cf_item_to_section(ci);
2379 * Ensure that the user can't add CONF_PAIRs
2380 * with 'internal' names;
2382 if (buf1[0] == '_') {
2383 ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2388 * Handle if/elsif specially.
2390 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2392 char const *error = NULL;
2394 CONF_SECTION *server;
2395 fr_cond_t *cond = NULL;
2398 * if / elsif MUST be inside of a
2399 * processing section, which MUST in turn
2400 * be inside of a "server" directive.
2402 if (!this->item.parent) {
2404 ERROR("%s[%d]: Invalid location for '%s'",
2405 filename, *lineno, buf1);
2410 * Can only have "if" in 3 named sections.
2412 server = this->item.parent;
2414 (strcmp(server->name1, "server") != 0) &&
2415 (strcmp(server->name1, "policy") != 0) &&
2416 (strcmp(server->name1, "instantiate") != 0)) {
2417 server = server->item.parent;
2418 if (!server) goto invalid_location;
2422 * Skip (...) to find the {
2424 slen = fr_condition_tokenize(this, cf_section_to_item(this), ptr, &cond,
2425 &error, FR_COND_TWO_PASS);
2426 memcpy(&p, &ptr, sizeof(p));
2429 if (p[-slen] != '{') goto cond_error;
2435 * This hack is so that the NEXT stage
2436 * doesn't go "too far" in expanding the
2437 * variable. We can parse the conditions
2438 * without expanding the ${...} stuff.
2439 * BUT we don't want to expand all of the
2440 * stuff AFTER the condition. So we do
2443 * The first pass is to discover the end
2444 * of the condition. We then expand THAT
2445 * string, and do a second pass parsing
2446 * the expanded condition.
2452 * If there's a ${...}. If so, expand it.
2454 if (strchr(ptr, '$') != NULL) {
2455 ptr = cf_expand_variables(filename, lineno,
2460 ERROR("%s[%d]: Parse error expanding ${...} in condition",
2464 } /* else leave it alone */
2466 css = cf_section_alloc(this, buf1, ptr);
2468 ERROR("%s[%d]: Failed allocating memory for section",
2472 css->item.filename = filename;
2473 css->item.lineno = *lineno;
2475 slen = fr_condition_tokenize(css, cf_section_to_item(css), ptr, &cond,
2476 &error, FR_COND_TWO_PASS);
2477 *p = '{'; /* put it back */
2481 char *spaces, *text;
2483 fr_canonicalize_error(this, &spaces, &text, slen, ptr);
2485 ERROR("%s[%d]: Parse error in condition",
2487 ERROR("%s[%d]: %s", filename, *lineno, text);
2488 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2490 talloc_free(spaces);
2496 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2498 ERROR("%s[%d]: Condition is too large after \"%s\"",
2499 filename, *lineno, buf1);
2504 * Copy the expanded and parsed condition
2505 * into buf2. Then, parse the text after
2506 * the condition, which now MUST be a '{.
2508 * If it wasn't '{' it would have been
2509 * caught in the first pass of
2510 * conditional parsing, above.
2512 memcpy(buf2, ptr, slen);
2516 if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2518 ERROR("%s[%d]: Expected '{' %d",
2519 filename, *lineno, t3);
2524 * Swap the condition with trailing stuff for
2525 * the final condition.
2527 memcpy(&p, &css->name2, sizeof(css->name2));
2529 css->name2 = talloc_typed_strdup(css, buf2);
2531 cf_item_add(this, &(css->item));
2532 cf_data_add_internal(css, "if", cond, NULL, false);
2535 * The current section is now the child section.
2539 goto check_for_more;
2544 * Grab the next token.
2546 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2563 case T_OP_CMP_FALSE:
2564 if (!this || (strcmp(this->name1, "update") != 0)) {
2565 ERROR("%s[%d]: Invalid operator in assignment",
2573 while (isspace((int) *ptr)) ptr++;
2576 * New parser: non-quoted strings are
2577 * bare words, and we parse everything
2578 * until the next newline, or the next
2579 * comma. If they have { or } in a bare
2580 * word, well... too bad.
2582 if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2583 && (*ptr != '`') && (*ptr != '/')) {
2584 const char *q = ptr;
2587 while (*q && (*q >= ' ') && (*q != ',') &&
2590 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2591 ERROR("%s[%d]: Parse error: value too long",
2596 memcpy(buf3, ptr, (q - ptr));
2597 buf3[q - ptr] = '\0';
2601 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2604 if (t3 == T_INVALID) {
2605 ERROR("%s[%d]: Parse error: %s",
2612 * Allow "foo" by itself, or "foo = bar"
2618 case T_DOUBLE_QUOTED_STRING:
2619 case T_BACK_QUOTED_STRING:
2620 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3, &soft_fail);
2622 if (!soft_fail) return -1;
2625 * References an item which doesn't exist,
2626 * or which is already marked up as being
2627 * expanded in pass2. Wait for pass2 to
2628 * do the expansions.
2646 * Add this CONF_PAIR to our CONF_SECTION
2649 cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2650 if (!cpn) return -1;
2651 cpn->item.filename = filename;
2652 cpn->item.lineno = *lineno;
2654 cf_item_add(this, &(cpn->item));
2657 * Hacks for escaping
2659 if (!cf_new_escape && !this->item.parent && value &&
2660 (strcmp(buf1, "correct_escapes") == 0) &&
2661 ((strcmp(value, "true") == 0) ||
2662 (strcmp(value, "yes") == 0) ||
2663 (strcmp(value, "1") == 0))) {
2664 cf_new_escape = true;
2668 * Require a comma, unless there's a comment.
2670 while (isspace(*ptr)) ptr++;
2679 * foo = bar # other stuff
2681 if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2683 if (!*ptr || (*ptr == '}')) break;
2685 ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2686 filename, *lineno, value, ptr);
2690 * No '=', must be a section or sub-section.
2693 case T_DOUBLE_QUOTED_STRING:
2694 case T_SINGLE_QUOTED_STRING:
2695 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2696 if (t3 != T_LCBRACE) {
2697 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2698 filename, *lineno, buf1, buf2);
2704 css = cf_section_alloc(this, buf1,
2705 t2 == T_LCBRACE ? NULL : buf2);
2707 ERROR("%s[%d]: Failed allocating memory for section",
2712 css->item.filename = filename;
2713 css->item.lineno = *lineno;
2714 cf_item_add(this, &(css->item));
2717 * There may not be a name2
2719 css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2722 * The current section is now the child section.
2728 ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2733 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2734 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2741 * Done parsing one thing. Skip to EOL if possible.
2743 while (isspace(*ptr)) ptr++;
2745 if (*ptr == '#') continue;
2754 * See if EOF was unexpected ..
2756 if (feof(fp) && (this != current)) {
2757 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2758 filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2766 * Include one config file in another.
2768 static int cf_file_include(CONF_SECTION *cs, char const *filename_in)
2772 char const *filename;
2775 * So we only need to do this once.
2777 filename = talloc_strdup(cs, filename_in);
2779 DEBUG2("including configuration file %s", filename);
2781 fp = cf_file_open(cs, filename);
2784 if (!cs->item.filename) cs->item.filename = filename;
2787 * Read the section. It's OK to have EOF without a
2788 * matching close brace.
2790 if (cf_section_read(filename, &lineno, fp, cs) < 0) {
2801 * Do variable expansion in pass2.
2803 * This is a breadth-first expansion. "deep
2805 static int cf_section_pass2(CONF_SECTION *cs)
2809 for (ci = cs->children; ci; ci = ci->next) {
2814 if (ci->type != CONF_ITEM_PAIR) continue;
2816 cp = cf_item_to_pair(ci);
2817 if (!cp->value || !cp->pass2) continue;
2819 rad_assert((cp->rhs_type == T_BARE_WORD) ||
2820 (cp->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2821 (cp->rhs_type == T_BACK_QUOTED_STRING));
2823 value = cf_expand_variables(ci->filename, &ci->lineno, cs, buffer, sizeof(buffer), cp->value, NULL);
2824 if (!value) return -1;
2826 rad_const_free(cp->value);
2827 cp->value = talloc_typed_strdup(cp, value);
2830 for (ci = cs->children; ci; ci = ci->next) {
2831 if (ci->type != CONF_ITEM_SECTION) continue;
2833 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
2841 * Bootstrap a config file.
2843 int cf_file_read(CONF_SECTION *cs, char const *filename)
2849 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
2852 p = strrchr(cp->value, FR_DIR_SEP);
2855 cp->item.filename = "<internal>";
2856 cp->item.lineno = -1;
2857 cf_item_add(cs, &(cp->item));
2859 tree = rbtree_create(cs, filename_cmp, NULL, 0);
2860 if (!tree) return -1;
2862 cf_data_add_internal(cs, "filename", tree, NULL, 0);
2864 if (cf_file_include(cs, filename) < 0) return -1;
2867 * Now that we've read the file, go back through it and
2868 * expand the variables.
2870 if (cf_section_pass2(cs) < 0) return -1;
2876 void cf_file_free(CONF_SECTION *cs)
2883 * Return a CONF_PAIR within a CONF_SECTION.
2885 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
2887 CONF_PAIR *cp, mycp;
2889 if (!cs || !name) return NULL;
2892 cp = rbtree_finddata(cs->pair_tree, &mycp);
2895 if (!cs->template) return NULL;
2897 return rbtree_finddata(cs->template->pair_tree, &mycp);
2901 * Return the attr of a CONF_PAIR
2904 char const *cf_pair_attr(CONF_PAIR const *pair)
2906 return (pair ? pair->attr : NULL);
2910 * Return the value of a CONF_PAIR
2913 char const *cf_pair_value(CONF_PAIR const *pair)
2915 return (pair ? pair->value : NULL);
2918 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
2920 return (pair ? pair->op : T_INVALID);
2923 /** Return the value (lhs) type
2925 * @param pair to extract value type from.
2926 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2927 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
2929 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
2931 return (pair ? pair->lhs_type : T_INVALID);
2934 /** Return the value (rhs) type
2936 * @param pair to extract value type from.
2937 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2938 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
2940 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
2942 return (pair ? pair->rhs_type : T_INVALID);
2946 * Turn a CONF_PAIR into a VALUE_PAIR
2947 * For now, ignore the "value_type" field...
2949 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
2952 fr_strerror_printf("Internal error");
2957 fr_strerror_printf("No value given for attribute %s", pair->attr);
2962 * false comparisons never match. BUT if it's a "string"
2963 * or `string`, then remember to expand it later.
2965 if ((pair->op != T_OP_CMP_FALSE) &&
2966 ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2967 (pair->rhs_type == T_BACK_QUOTED_STRING))) {
2970 vp = pairmake(pair, NULL, pair->attr, NULL, pair->op);
2975 if (pairmark_xlat(vp, pair->value) < 0) {
2984 return pairmake(pair, NULL, pair->attr, pair->value, pair->op);
2988 * Return the first label of a CONF_SECTION
2991 char const *cf_section_name1(CONF_SECTION const *cs)
2993 return (cs ? cs->name1 : NULL);
2997 * Return the second label of a CONF_SECTION
3000 char const *cf_section_name2(CONF_SECTION const *cs)
3002 return (cs ? cs->name2 : NULL);
3005 /** Return name2 if set, else name1
3008 char const *cf_section_name(CONF_SECTION const *cs)
3012 name = cf_section_name2(cs);
3013 if (name) return name;
3015 return cf_section_name1(cs);
3019 * Find a value in a CONF_SECTION
3021 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
3025 cp = cf_pair_find(cs, attr);
3027 return (cp ? cp->value : NULL);
3031 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
3032 char const *name1, char const *name2)
3035 CONF_ITEM const *ci;
3037 if (!cs || !name1) return NULL;
3039 for (ci = &(cs->item); ci; ci = ci->next) {
3040 if (ci->type != CONF_ITEM_SECTION)
3043 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
3047 their2 = cf_item_to_section(ci)->name2;
3049 if ((!name2 && !their2) ||
3050 (name2 && their2 && (strcmp(name2, their2) == 0))) {
3051 return cf_item_to_section(ci);
3058 /** Find a pair with a name matching attr, after specified pair.
3060 * @param cs to search in.
3061 * @param pair to search from (may be NULL).
3062 * @param attr to find (may be NULL in which case any attribute matches).
3063 * @return the next matching CONF_PAIR or NULL if none matched.
3065 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
3066 CONF_PAIR const *pair, char const *attr)
3070 if (!cs) return NULL;
3073 * If pair is NULL and we're trying to find a specific
3074 * attribute this must be a first time run.
3076 * Find the pair with correct name.
3078 if (!pair && attr) return cf_pair_find(cs, attr);
3081 * Start searching from the next child, or from the head
3082 * of the list of children (if no pair was provided).
3084 for (ci = pair ? pair->item.next : cs->children;
3087 if (ci->type != CONF_ITEM_PAIR) continue;
3089 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
3092 return cf_item_to_pair(ci);
3096 * Find a CONF_SECTION, or return the root if name is NULL
3099 CONF_SECTION *cf_section_find(char const *name)
3102 return cf_section_sub_find(root_config, name);
3107 /** Find a sub-section in a section
3109 * This finds ANY section having the same first name.
3110 * The second name is ignored.
3112 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
3116 if (!cs || !name) return NULL; /* can't find an un-named section */
3119 * No sub-sections have been defined, so none exist.
3121 if (!cs->section_tree) return NULL;
3125 return rbtree_finddata(cs->section_tree, &mycs);
3129 /** Find a CONF_SECTION with both names.
3132 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
3133 char const *name1, char const *name2)
3137 if (!cs) cs = root_config;
3138 if (!cs) return NULL;
3141 CONF_SECTION mycs, *master_cs;
3143 if (!cs->section_tree) return NULL;
3148 master_cs = rbtree_finddata(cs->section_tree, &mycs);
3149 if (!master_cs) return NULL;
3152 * Look it up in the name2 tree. If it's there,
3155 if (master_cs->name2_tree) {
3156 CONF_SECTION *subcs;
3158 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
3159 if (subcs) return subcs;
3163 * We don't insert ourselves into the name2 tree.
3164 * So if there's nothing in the name2 tree, maybe
3165 * *we* are the answer.
3167 if (!master_cs->name2 && name2) return NULL;
3168 if (master_cs->name2 && !name2) return NULL;
3169 if (!master_cs->name2 && !name2) return master_cs;
3171 if (strcmp(master_cs->name2, name2) == 0) {
3179 * Else do it the old-fashioned way.
3181 for (ci = cs->children; ci; ci = ci->next) {
3182 CONF_SECTION *subcs;
3184 if (ci->type != CONF_ITEM_SECTION)
3187 subcs = cf_item_to_section(ci);
3188 if (!subcs->name2) {
3189 if (strcmp(subcs->name1, name2) == 0) break;
3191 if (strcmp(subcs->name2, name2) == 0) break;
3195 return cf_item_to_section(ci);
3199 * Return the next subsection after a CONF_SECTION
3200 * with a certain name1 (char *name1). If the requested
3201 * name1 is NULL, any name1 matches.
3204 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
3205 CONF_SECTION const *subsection,
3210 if (!section) return NULL;
3213 * If subsection is NULL this must be a first time run
3214 * Find the subsection with correct name
3218 ci = section->children;
3220 ci = subsection->item.next;
3223 for (; ci; ci = ci->next) {
3224 if (ci->type != CONF_ITEM_SECTION)
3226 if ((name1 == NULL) ||
3227 (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3231 return cf_item_to_section(ci);
3236 * Return the next section after a CONF_SECTION
3237 * with a certain name1 (char *name1). If the requested
3238 * name1 is NULL, any name1 matches.
3241 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3242 CONF_SECTION const *subsection,
3245 if (!section) return NULL;
3247 if (!section->item.parent) return NULL;
3249 return cf_subsection_find_next(section->item.parent, subsection, name1);
3252 /** Return the next item after a CONF_ITEM.
3255 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3257 if (!section) return NULL;
3260 * If item is NULL this must be a first time run
3261 * Return the first item
3264 return section->children;
3270 static void _pair_count(int *count, CONF_SECTION const *cs)
3272 CONF_ITEM const *ci;
3274 for (ci = cf_item_find_next(cs, NULL);
3276 ci = cf_item_find_next(cs, ci)) {
3278 if (cf_item_is_section(ci)) {
3279 _pair_count(count, cf_item_to_section(ci));
3287 /** Count the number of conf pairs beneath a section
3289 * @param[in] cs to search for items in.
3290 * @return number of pairs nested within section.
3292 int cf_pair_count(CONF_SECTION const *cs)
3296 _pair_count(&count, cs);
3301 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3303 if (!ci) return NULL;
3308 int cf_section_lineno(CONF_SECTION const *section)
3310 return section->item.lineno;
3313 char const *cf_pair_filename(CONF_PAIR const *pair)
3315 return pair->item.filename;
3318 char const *cf_section_filename(CONF_SECTION const *section)
3320 return section->item.filename;
3323 int cf_pair_lineno(CONF_PAIR const *pair)
3325 return pair->item.lineno;
3328 bool cf_item_is_section(CONF_ITEM const *item)
3330 return item->type == CONF_ITEM_SECTION;
3333 bool cf_item_is_pair(CONF_ITEM const *item)
3335 return item->type == CONF_ITEM_PAIR;
3339 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3340 void *data, void (*data_free)(void *))
3344 cd = talloc_zero(parent, CONF_DATA);
3345 if (!cd) return NULL;
3347 cd->item.type = CONF_ITEM_DATA;
3348 cd->item.parent = parent;
3349 cd->name = talloc_typed_strdup(cd, name);
3356 cd->free = data_free;
3359 talloc_set_destructor(cd, _cf_data_free);
3365 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3367 if (!cs || !name) return NULL;
3370 * Find the name in the tree, for speed.
3372 if (cs->data_tree) {
3377 return rbtree_finddata(cs->data_tree, &mycd);
3384 * Find data from a particular section.
3386 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3388 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3390 if (cd) return cd->data;
3396 * Add named data to a configuration section.
3398 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3399 void *data, void (*data_free)(void *),
3404 if (!cs || !name) return -1;
3407 * Already exists. Can't add it.
3409 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3411 cd = cf_data_alloc(cs, name, data, data_free);
3415 cf_item_add(cs, cf_data_to_item(cd));
3421 * Add named data to a configuration section.
3423 int cf_data_add(CONF_SECTION *cs, char const *name,
3424 void *data, void (*data_free)(void *))
3426 return cf_data_add_internal(cs, name, data, data_free, 0);
3429 /** Remove named data from a configuration section
3432 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3438 if (!cs || !name) return NULL;
3439 if (!cs->data_tree) return NULL;
3442 * Find the name in the tree, for speed.
3446 cd = rbtree_finddata(cs->data_tree, &mycd);
3447 if (!cd) return NULL;
3449 talloc_set_destructor(cd, NULL); /* Disarm the destructor */
3450 rbtree_deletebydata(cs->data_tree, &mycd);
3459 * This is here to make the rest of the code easier to read. It
3460 * ties conffile.c to log.c, but it means we don't have to
3461 * pollute every other function with the knowledge of the
3462 * configuration internals.
3464 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3470 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3475 ci->filename ? ci->filename : "unknown",
3476 ci->lineno ? ci->lineno : 0,
3479 ERROR("<unknown>[*]: %s", buffer);
3483 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3489 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3492 rad_assert(cs != NULL);
3495 cs->item.filename ? cs->item.filename : "unknown",
3496 cs->item.lineno ? cs->item.lineno : 0,
3500 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3506 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3509 rad_assert(cp != NULL);
3512 cp->item.filename ? cp->item.filename : "unknown",
3513 cp->item.lineno ? cp->item.lineno : 0,
3517 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3522 if ((rad_debug_lvl > 1) && cs) vradlog(L_DBG, fmt, ap);
3527 * Wrapper to simplify the code.
3529 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3535 if (rad_debug_lvl > 1 && cs) {
3536 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3538 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3543 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3545 if (!cs) return NULL;
3547 return cs->variables;
3551 * For "switch" and "case" statements.
3553 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3555 if (!cs) return T_INVALID;
3557 return cs->name2_type;