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;
127 CONF_SECTION *root_config = NULL;
128 bool cf_new_escape = false;
131 static int cf_data_add_internal(CONF_SECTION *cs, char const *name, void *data,
132 void (*data_free)(void *), int flag);
134 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag);
136 static char const *cf_expand_variables(char const *cf, int *lineno,
137 CONF_SECTION *outercs,
138 char *output, size_t outsize,
139 char const *input, bool *soft_fail);
141 static int cf_file_include(CONF_SECTION *cs, char const *filename_in);
146 * Isolate the scary casts in these tiny provably-safe functions
149 /** Cast a CONF_ITEM to a CONF_PAIR
152 CONF_PAIR *cf_item_to_pair(CONF_ITEM const *ci)
156 if (ci == NULL) return NULL;
158 rad_assert(ci->type == CONF_ITEM_PAIR);
160 memcpy(&out, &ci, sizeof(out));
164 /** Cast a CONF_ITEM to a CONF_SECTION
167 CONF_SECTION *cf_item_to_section(CONF_ITEM const *ci)
171 if (ci == NULL) return NULL;
173 rad_assert(ci->type == CONF_ITEM_SECTION);
175 memcpy(&out, &ci, sizeof(out));
179 /** Cast a CONF_PAIR to a CONF_ITEM
182 CONF_ITEM *cf_pair_to_item(CONF_PAIR const *cp)
186 if (cp == NULL) return NULL;
188 memcpy(&out, &cp, sizeof(out));
192 /** Cast a CONF_SECTION to a CONF_ITEM
195 CONF_ITEM *cf_section_to_item(CONF_SECTION const *cs)
199 if (cs == NULL) return NULL;
201 memcpy(&out, &cs, sizeof(out));
205 /** Cast CONF_DATA to a CONF_ITEM
208 static CONF_ITEM *cf_data_to_item(CONF_DATA const *cd)
216 memcpy(&out, &cd, sizeof(out));
220 static int _cf_data_free(CONF_DATA *cd)
222 if (cd->free) cd->free(cd->data);
228 * rbtree callback function
230 static int pair_cmp(void const *a, void const *b)
232 CONF_PAIR const *one = a;
233 CONF_PAIR const *two = b;
235 return strcmp(one->attr, two->attr);
240 * rbtree callback function
242 static int section_cmp(void const *a, void const *b)
244 CONF_SECTION const *one = a;
245 CONF_SECTION const *two = b;
247 return strcmp(one->name1, two->name1);
252 * rbtree callback function
254 static int name2_cmp(void const *a, void const *b)
256 CONF_SECTION const *one = a;
257 CONF_SECTION const *two = b;
259 rad_assert(strcmp(one->name1, two->name1) == 0);
261 if (!one->name2 && !two->name2) return 0;
262 if (one->name2 && !two->name2) return -1;
263 if (!one->name2 && two->name2) return +1;
265 return strcmp(one->name2, two->name2);
270 * rbtree callback function
272 static int data_cmp(void const *a, void const *b)
276 CONF_DATA const *one = a;
277 CONF_DATA const *two = b;
279 rcode = one->flag - two->flag;
280 if (rcode != 0) return rcode;
282 return strcmp(one->name, two->name);
286 * Functions for tracking filenames.
288 static int filename_cmp(void const *a, void const *b)
290 cf_file_t const *one = a;
291 cf_file_t const *two = b;
293 if (one->buf.st_dev < two->buf.st_dev) return -1;
294 if (one->buf.st_dev > two->buf.st_dev) return +1;
296 if (one->buf.st_ino < two->buf.st_ino) return -1;
297 if (one->buf.st_ino > two->buf.st_ino) return +1;
302 static FILE *cf_file_open(CONF_SECTION *cs, char const *filename)
310 top = cf_top_section(cs);
311 cd = cf_data_find_internal(top, "filename", 0);
312 if (!cd) return NULL;
316 fp = fopen(filename, "r");
318 ERROR("Unable to open file \"%s\": %s",
319 filename, fr_syserror(errno));
323 file = talloc(tree, cf_file_t);
329 file->filename = filename;
333 if (stat(filename, &file->buf) < 0) {
335 if ((file->buf.st_mode & S_IWOTH) != 0) {
337 ERROR("Configuration file %s is globally writable. "
338 "Refusing to start due to insecure configuration.", filename);
345 * We can include the same file twice. e.g. when it
346 * contains common definitions, such as for SQL.
348 * Though the admin should really use templates for that.
350 if (!rbtree_insert(tree, file)) {
358 * Do some checks on the file as an "input" file. i.e. one read
361 static bool cf_file_input(CONF_SECTION *cs, char const *filename)
368 top = cf_top_section(cs);
369 cd = cf_data_find_internal(top, "filename", 0);
370 if (!cd) return false;
373 file = talloc(tree, cf_file_t);
374 if (!file) return false;
376 file->filename = filename;
380 if (stat(filename, &file->buf) < 0) {
381 ERROR("Unable to open file \"%s\": %s", filename, fr_syserror(errno));
387 if ((file->buf.st_mode & S_IWOTH) != 0) {
388 ERROR("Configuration file %s is globally writable. "
389 "Refusing to start due to insecure configuration.", filename);
395 * It's OK to include the same file twice...
397 if (!rbtree_insert(tree, file)) {
407 * Return 0 for keep going, 1 for stop.
409 static int file_callback(void *ctx, void *data)
413 cf_file_t *file = data;
416 * The file doesn't exist or we can no longer read it.
418 if (stat(file->filename, &buf) < 0) {
419 *rcode = CF_FILE_ERROR;
424 * The file changed, we'll need to re-read it.
426 if (buf.st_mtime != file->buf.st_mtime) {
428 * Set none -> whatever
430 if (*rcode == CF_FILE_NONE) {
432 *rcode = CF_FILE_CONFIG;
436 *rcode = CF_FILE_MODULE;
442 * A module WAS changed, but now we discover that
443 * a main config file has changed. We might as
444 * well re-load everything.
446 if ((*rcode == CF_FILE_MODULE) && !file->input) {
447 *rcode = CF_FILE_CONFIG;
457 * See if any of the files have changed.
459 int cf_file_changed(CONF_SECTION *cs)
466 top = cf_top_section(cs);
467 cd = cf_data_find_internal(top, "filename", 0);
468 if (!cd) return true;
472 rcode = CF_FILE_NONE;
473 (void) rbtree_walk(tree, RBTREE_IN_ORDER, file_callback, &rcode);
478 static int _cf_section_free(CONF_SECTION *cs)
481 * Name1 and name2 are allocated contiguous with
485 rbtree_free(cs->pair_tree);
486 cs->pair_tree = NULL;
488 if (cs->section_tree) {
489 rbtree_free(cs->section_tree);
490 cs->section_tree = NULL;
492 if (cs->name2_tree) {
493 rbtree_free(cs->name2_tree);
494 cs->name2_tree = NULL;
497 rbtree_free(cs->data_tree);
498 cs->data_tree = NULL;
504 /** Allocate a CONF_PAIR
506 * @param parent CONF_SECTION to hang this CONF_PAIR off of.
508 * @param value of CONF_PAIR.
509 * @param op T_OP_EQ, T_OP_SET etc.
510 * @param lhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
511 * @param rhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
512 * @return NULL on error, else a new CONF_SECTION parented by parent.
514 CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value,
515 FR_TOKEN op, FR_TOKEN lhs_type, FR_TOKEN rhs_type)
519 rad_assert(fr_equality_op[op] || fr_assignment_op[op]);
520 if (!attr) return NULL;
522 cp = talloc_zero(parent, CONF_PAIR);
523 if (!cp) return NULL;
525 cp->item.type = CONF_ITEM_PAIR;
526 cp->item.parent = parent;
527 cp->lhs_type = lhs_type;
528 cp->rhs_type = rhs_type;
531 cp->attr = talloc_typed_strdup(cp, attr);
539 cp->value = talloc_typed_strdup(cp, value);
540 if (!cp->value) goto error;
546 /** Duplicate a CONF_PAIR
548 * @param parent to allocate new pair in.
549 * @param cp to duplicate.
550 * @return NULL on error, else a duplicate of the input pair.
552 CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp)
559 new = cf_pair_alloc(parent, cp->attr, cf_pair_value(cp),
560 cp->op, cp->lhs_type, cp->rhs_type);
561 if (!new) return NULL;
563 new->parsed = cp->parsed;
564 new->item.lineno = cp->item.lineno;
567 * Avoid mallocs if possible.
569 if (!cp->item.filename || (strcmp(parent->item.filename, cp->item.filename) == 0)) {
570 new->item.filename = parent->item.filename;
572 new->item.filename = talloc_strdup(new, cp->item.filename);
578 /** Add a configuration pair to a section
580 * @param parent section to add pair to.
583 void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp)
585 cf_item_add(parent, cf_pair_to_item(cp));
588 /** Allocate a CONF_SECTION
590 * @param parent CONF_SECTION to hang this CONF_SECTION off of.
591 * @param name1 Primary name.
592 * @param name2 Secondary name.
593 * @return NULL on error, else a new CONF_SECTION parented by parent.
595 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
600 if (!name1) return NULL;
602 if (name2 && parent) {
603 if (strchr(name2, '$')) {
604 name2 = cf_expand_variables(parent->item.filename,
605 &parent->item.lineno,
607 buffer, sizeof(buffer), name2, NULL);
609 ERROR("Failed expanding section name");
615 cs = talloc_zero(parent, CONF_SECTION);
616 if (!cs) return NULL;
618 cs->item.type = CONF_ITEM_SECTION;
619 cs->item.parent = parent;
621 cs->name1 = talloc_typed_strdup(cs, name1);
629 cs->name2 = talloc_typed_strdup(cs, name2);
630 if (!cs->name2) goto error;
633 cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
634 if (!cs->pair_tree) goto error;
636 talloc_set_destructor(cs, _cf_section_free);
639 * Don't create a data tree, it may not be needed.
643 * Don't create the section tree here, it may not
647 if (parent) cs->depth = parent->depth + 1;
652 /** Duplicate a configuration section
654 * @note recursively duplicates any child sections.
655 * @note does not duplicate any data associated with a section, or its child sections.
657 * @param parent section (may be NULL).
658 * @param cs to duplicate.
659 * @param name1 of new section.
660 * @param name2 of new section.
661 * @param copy_meta Copy additional meta data for a section (like template, base, depth and variables).
662 * @return a duplicate of the existing section, or NULL on error.
664 CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs,
665 char const *name1, char const *name2, bool copy_meta)
667 CONF_SECTION *new, *subcs;
671 new = cf_section_alloc(parent, name1, name2);
674 new->template = cs->template;
675 new->base = cs->base;
676 new->depth = cs->depth;
677 new->variables = cs->variables;
680 new->item.lineno = cs->item.lineno;
682 if (!cs->item.filename || (parent && (strcmp(parent->item.filename, cs->item.filename) == 0))) {
683 new->item.filename = parent->item.filename;
685 new->item.filename = talloc_strdup(new, cs->item.filename);
688 for (ci = cs->children; ci; ci = ci->next) {
690 case CONF_ITEM_SECTION:
691 subcs = cf_item_to_section(ci);
692 subcs = cf_section_dup(new, subcs,
693 cf_section_name1(subcs), cf_section_name2(subcs),
699 cf_section_add(new, subcs);
703 cp = cf_pair_dup(new, cf_item_to_pair(ci));
708 cf_pair_add(new, cp);
711 case CONF_ITEM_DATA: /* Skip data */
714 case CONF_ITEM_INVALID:
722 void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
724 cf_item_add(parent, &(cs->item));
727 /** Replace pair in a given section with a new pair, of the given value.
729 * @param cs to replace pair in.
730 * @param cp to replace.
731 * @param value New value to assign to cp.
732 * @return 0 on success, -1 on failure.
734 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
737 CONF_ITEM *ci, *cn, **last;
739 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->lhs_type, cp->rhs_type);
740 if (!newp) return -1;
746 * Find the old one from the linked list, and replace it
749 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
751 cn->next = (*last)->next;
758 rbtree_deletebydata(cs->pair_tree, ci);
760 rbtree_insert(cs->pair_tree, cn);
767 * Add an item to a configuration section.
769 void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
772 CONF_ITEM *first = ci;
775 rad_assert((void *)cs != (void *)ci);
777 if (!cs || !ci) return;
780 rad_assert(cs->tail == NULL);
783 rad_assert(cs->tail != NULL);
788 * Update the trees (and tail) for each item added.
790 for (/* nothing */; ci != NULL; ci = ci->next) {
791 rad_assert(ci->next != first); /* simple cycle detection */
796 * For fast lookups, pairs and sections get
801 if (!rbtree_insert(cs->pair_tree, ci)) {
802 CONF_PAIR *cp = cf_item_to_pair(ci);
804 if (strcmp(cp->attr, "confdir") == 0) break;
805 if (!cp->value) break; /* module name, "ok", etc. */
809 case CONF_ITEM_SECTION: {
810 CONF_SECTION *cs_new = cf_item_to_section(ci);
811 CONF_SECTION *name1_cs;
813 if (!cs->section_tree) {
814 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
815 if (!cs->section_tree) {
816 ERROR("Out of memory");
821 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
823 if (!rbtree_insert(cs->section_tree, cs_new)) {
824 ERROR("Failed inserting section into tree");
831 * We already have a section of
832 * this "name1". Add a new
833 * sub-section based on name2.
835 if (!name1_cs->name2_tree) {
836 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
837 if (!name1_cs->name2_tree) {
838 ERROR("Out of memory");
844 * We don't care if this fails.
845 * If the user tries to create
846 * two sections of the same
847 * name1/name2, the duplicate
848 * section is just silently
851 rbtree_insert(name1_cs->name2_tree, cs_new);
853 } /* was a section */
856 if (!cs->data_tree) {
857 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
860 rbtree_insert(cs->data_tree, ci);
864 default: /* FIXME: assert & error! */
867 } /* switch over conf types */
872 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
873 CONF_SECTION *outercs,
878 CONF_SECTION const *cs = outercs;
882 if (!cs) goto no_such_item;
884 strlcpy(name, ptr, sizeof(name));
888 * ".foo" means "foo from the current section"
894 * Just '.' means the current section
897 return cf_section_to_item(cs);
901 * ..foo means "foo from the section
902 * enclosing this section" (etc.)
905 if (cs->item.parent) {
906 cs = cs->item.parent;
910 * .. means the section
911 * enclosing this section
914 return cf_section_to_item(cs);
919 * "foo.bar.baz" means "from the root"
921 } else if (strchr(p, '.') != NULL) {
922 if (!parentcs) goto no_such_item;
934 if (r && q > r) q = NULL;
935 if (q && q < r) r = NULL;
941 q = strchr(r + 1, ']');
942 if (!q) return NULL; /* parse error */
945 * Points to foo[bar]xx: parse error,
946 * it should be foo[bar] or foo[bar].baz
948 if (q[1] && q[1] != '.') goto no_such_item;
952 next = cf_section_sub_find_name2(cs, p, r + 1);
957 * Points to a named instance of a section.
960 if (!next) goto no_such_item;
961 return &(next->item);
964 q++; /* ensure we skip the ']' and '.' */
968 next = cf_section_sub_find(cs, p);
972 if (!next) break; /* it MAY be a pair in this section! */
978 if (!*p) goto no_such_item;
982 * Find it in the current referenced
985 cp = cf_pair_find(cs, p);
987 cp->parsed = true; /* conf pairs which are referenced count as parsed */
991 next = cf_section_sub_find(cs, p);
992 if (next) return &(next->item);
995 * "foo" is "in the current section, OR in main".
997 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
1007 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
1009 if (!cs) return NULL;
1011 while (cs->item.parent != NULL) {
1012 cs = cs->item.parent;
1020 * Expand the variables in an input string.
1022 static char const *cf_expand_variables(char const *cf, int *lineno,
1023 CONF_SECTION *outercs,
1024 char *output, size_t outsize,
1025 char const *input, bool *soft_fail)
1028 char const *end, *ptr;
1029 CONF_SECTION const *parentcs;
1032 if (soft_fail) *soft_fail = false;
1035 * Find the master parent conf section.
1036 * We can't use main_config.config, because we're in the
1037 * process of re-building it, and it isn't set up yet...
1039 parentcs = cf_top_section(outercs);
1045 * Ignore anything other than "${"
1047 if ((*ptr == '$') && (ptr[1] == '{')) {
1053 * FIXME: Add support for ${foo:-bar},
1058 * Look for trailing '}', and log a
1059 * warning for anything that doesn't match,
1060 * and exit with a fatal error.
1062 end = strchr(ptr, '}');
1065 INFO("%s[%d]: Variable expansion missing }",
1073 * Can't really happen because input lines are
1074 * capped at 8k, which is sizeof(name)
1076 if ((size_t) (end - ptr) >= sizeof(name)) {
1077 ERROR("%s[%d]: Reference string is too large",
1082 memcpy(name, ptr, end - ptr);
1083 name[end - ptr] = '\0';
1085 q = strchr(name, ':');
1090 ci = cf_reference_item(parentcs, outercs, name);
1092 if (soft_fail) *soft_fail = true;
1093 ERROR("%s[%d]: Reference \"${%s}\" not found", cf, *lineno, name);
1098 * The expansion doesn't refer to another item or section
1099 * it's the property of a section.
1102 CONF_SECTION *mycs = cf_item_to_section(ci);
1104 if (ci->type != CONF_ITEM_SECTION) {
1105 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
1109 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
1110 case CONF_PROPERTY_NAME:
1111 strcpy(p, mycs->name1);
1114 case CONF_PROPERTY_INSTANCE:
1115 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
1119 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
1125 } else if (ci->type == CONF_ITEM_PAIR) {
1127 * Substitute the value of the variable.
1129 cp = cf_item_to_pair(ci);
1132 * If the thing we reference is
1133 * marked up as being expanded in
1134 * pass2, don't expand it now.
1135 * Let it be expanded in pass2.
1138 if (soft_fail) *soft_fail = true;
1140 ERROR("%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
1141 cf, *lineno, input);
1146 ERROR("%s[%d]: Reference \"%s\" has no value",
1147 cf, *lineno, input);
1151 if (p + strlen(cp->value) >= output + outsize) {
1152 ERROR("%s[%d]: Reference \"%s\" is too long",
1153 cf, *lineno, input);
1157 strcpy(p, cp->value);
1161 } else if (ci->type == CONF_ITEM_SECTION) {
1162 CONF_SECTION *subcs;
1165 * Adding an entry again to a
1166 * section is wrong. We don't
1167 * want an infinite loop.
1169 if (ci->parent == outercs) {
1170 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
1175 * Copy the section instead of
1178 subcs = cf_item_to_section(ci);
1179 subcs = cf_section_dup(outercs, subcs,
1180 cf_section_name1(subcs), cf_section_name2(subcs),
1183 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
1187 subcs->item.filename = ci->filename;
1188 subcs->item.lineno = ci->lineno;
1189 cf_item_add(outercs, &(subcs->item));
1194 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
1197 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
1203 * Look for trailing '}', and log a
1204 * warning for anything that doesn't match,
1205 * and exit with a fatal error.
1207 end = strchr(ptr, '}');
1210 INFO("%s[%d]: Environment variable expansion missing }",
1216 * Can't really happen because input lines are
1217 * capped at 8k, which is sizeof(name)
1219 if ((size_t) (end - ptr) >= sizeof(name)) {
1220 ERROR("%s[%d]: Environment variable name is too large",
1225 memcpy(name, ptr, end - ptr);
1226 name[end - ptr] = '\0';
1229 * Get the environment variable.
1230 * If none exists, then make it an empty string.
1238 if (p + strlen(env) >= output + outsize) {
1239 ERROR("%s[%d]: Reference \"%s\" is too long",
1240 cf, *lineno, input);
1250 * Copy it over verbatim.
1256 if (p >= (output + outsize)) {
1257 ERROR("%s[%d]: Reference \"%s\" is too long",
1258 cf, *lineno, input);
1261 } /* loop over all of the input string. */
1268 static char const parse_spaces[] = " ";
1270 /** Validation function for ipaddr conffile types
1273 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1274 fr_ipaddr_t *ipaddr)
1278 if (strcmp(value, "*") == 0) {
1279 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1280 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1281 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1283 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1284 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1288 case PW_TYPE_IPV4_ADDR:
1289 case PW_TYPE_IPV6_ADDR:
1290 case PW_TYPE_COMBO_IP_ADDR:
1291 switch (ipaddr->af) {
1293 if (ipaddr->prefix != 32) {
1294 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
1302 if (ipaddr->prefix != 128) {
1303 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
1318 /** Parses a #CONF_PAIR into a C data type, with a default value.
1320 * Takes fields from a #CONF_PARSER struct and uses them to parse the string value
1321 * of a #CONF_PAIR into a C data type matching the type argument.
1323 * The format of the types are the same as #value_data_t types.
1325 * @note The dflt value will only be used if no matching #CONF_PAIR is found. Empty strings will not
1326 * result in the dflt value being used.
1328 * @param cs to search for matching #CONF_PAIR in.
1329 * @param name of #CONF_PAIR to search for.
1330 * @param type Data type to parse #CONF_PAIR value as.
1331 * Should be one of the following ``data`` types, and one or more of the following ``flag`` types or'd together:
1332 * - ``data`` #PW_TYPE_TMPL - @copybrief PW_TYPE_TMPL
1333 Feeds the value into #tmpl_afrom_str. Value can be
1334 * obtained when processing requests, with #tmpl_expand or #tmpl_aexpand.
1335 * - ``data`` #PW_TYPE_BOOLEAN - @copybrief PW_TYPE_BOOLEAN
1336 * - ``data`` #PW_TYPE_INTEGER - @copybrief PW_TYPE_INTEGER
1337 * - ``data`` #PW_TYPE_SHORT - @copybrief PW_TYPE_SHORT
1338 * - ``data`` #PW_TYPE_INTEGER64 - @copybrief PW_TYPE_INTEGER64
1339 * - ``data`` #PW_TYPE_SIGNED - @copybrief PW_TYPE_SIGNED
1340 * - ``data`` #PW_TYPE_STRING - @copybrief PW_TYPE_STRING
1341 * - ``data`` #PW_TYPE_IPV4_ADDR - @copybrief PW_TYPE_IPV4_ADDR (IPv4 address with prefix 32).
1342 * - ``data`` #PW_TYPE_IPV4_PREFIX - @copybrief PW_TYPE_IPV4_PREFIX (IPv4 address with variable prefix).
1343 * - ``data`` #PW_TYPE_IPV6_ADDR - @copybrief PW_TYPE_IPV6_ADDR (IPv6 address with prefix 128).
1344 * - ``data`` #PW_TYPE_COMBO_IP_ADDR - @copybrief PW_TYPE_COMBO_IP_ADDR (IPv4/IPv6 address with
1346 * - ``data`` #PW_TYPE_COMBO_IP_PREFIX - @copybrief PW_TYPE_COMBO_IP_PREFIX (IPv4/IPv6 address with
1348 * - ``data`` #PW_TYPE_TIMEVAL - @copybrief PW_TYPE_TIMEVAL
1349 * - ``flag`` #PW_TYPE_DEPRECATED - @copybrief PW_TYPE_DEPRECATED
1350 * - ``flag`` #PW_TYPE_REQUIRED - @copybrief PW_TYPE_REQUIRED
1351 * - ``flag`` #PW_TYPE_ATTRIBUTE - @copybrief PW_TYPE_ATTRIBUTE
1352 * - ``flag`` #PW_TYPE_SECRET - @copybrief PW_TYPE_SECRET
1353 * - ``flag`` #PW_TYPE_FILE_INPUT - @copybrief PW_TYPE_FILE_INPUT
1354 * - ``flag`` #PW_TYPE_NOT_EMPTY - @copybrief PW_TYPE_NOT_EMPTY
1355 * @param data Pointer to a global variable, or pointer to a field in the struct being populated with values.
1356 * @param dflt value to use, if no #CONF_PAIR is found.
1357 * @return -1 on error, -2 if deprecated, 0 on success (correctly parsed), 1 if default value was used.
1359 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1362 bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, multi;
1365 CONF_PAIR *cp = NULL;
1366 fr_ipaddr_t *ipaddr;
1368 CONF_ITEM *c_item = &cs->item;
1372 deprecated = (type & PW_TYPE_DEPRECATED);
1373 required = (type & PW_TYPE_REQUIRED);
1374 attribute = (type & PW_TYPE_ATTRIBUTE);
1375 secret = (type & PW_TYPE_SECRET);
1376 file_input = (type == PW_TYPE_FILE_INPUT); /* check, not and */
1377 cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1378 tmpl = (type & PW_TYPE_TMPL);
1379 multi = (type & PW_TYPE_MULTI);
1381 if (attribute) required = true;
1382 if (required) cant_be_empty = true; /* May want to review this in the future... */
1385 * Everything except templates must have a base type.
1387 if (!(type & 0xff) && !tmpl) {
1388 cf_log_err(c_item, "Configuration item '%s' must have a data type", name);
1392 type &= 0xff; /* normal types are small */
1396 cp = cf_pair_find(cs, name);
1399 * No pairs match the configuration item name in the current
1400 * section, use the default value.
1406 * Something matched, used the CONF_PAIR value.
1409 CONF_PAIR *next = cp;
1415 * @fixme We should actually validate
1416 * the value of the pairs too
1418 if (multi) while ((next = cf_pair_find_next(cs, next, name))) {
1419 next->parsed = true;
1426 cf_log_err(c_item, "Configuration item '%s' must have a value", name);
1433 if ((value[0] == '\0') && cant_be_empty) {
1435 cf_log_err(c_item, "Configuration item '%s' must not be empty (zero length)", name);
1438 cf_log_err(c_item, "Comment item to silence this message");
1444 cf_log_err(c_item, "Configuration item \"%s\" is deprecated", name);
1450 * Process a value as a LITERAL template. Once all of
1451 * the attrs and xlats are defined, the pass2 code
1452 * converts it to the appropriate type.
1458 *(vp_tmpl_t **)data = NULL;
1462 rad_assert(!attribute);
1463 vpt = tmpl_alloc(cs, TMPL_TYPE_LITERAL, value, strlen(value));
1464 *(vp_tmpl_t **)data = vpt;
1470 case PW_TYPE_BOOLEAN:
1472 * Allow yes/no, true/false, and on/off
1474 if ((strcasecmp(value, "yes") == 0) ||
1475 (strcasecmp(value, "true") == 0) ||
1476 (strcasecmp(value, "on") == 0)) {
1477 *(bool *)data = true;
1478 } else if ((strcasecmp(value, "no") == 0) ||
1479 (strcasecmp(value, "false") == 0) ||
1480 (strcasecmp(value, "off") == 0)) {
1481 *(bool *)data = false;
1483 *(bool *)data = false;
1484 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1485 "variable %s", value, name);
1488 cf_log_info(cs, "%.*s\t%s = %s",
1489 cs->depth, parse_spaces, name, value);
1492 case PW_TYPE_INTEGER:
1494 unsigned long v = strtoul(value, 0, 0);
1497 * Restrict integer values to 0-INT32_MAX, this means
1498 * it will always be safe to cast them to a signed type
1499 * for comparisons, and imposes the same range limit as
1500 * before we switched to using an unsigned type to
1501 * represent config item integers.
1503 if (v > INT32_MAX) {
1504 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1509 *(uint32_t *)data = v;
1510 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1516 unsigned long v = strtoul(value, 0, 0);
1518 if (v > UINT16_MAX) {
1519 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1523 *(uint16_t *)data = (uint16_t) v;
1524 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1528 case PW_TYPE_INTEGER64:
1529 *(uint64_t *)data = strtoull(value, 0, 0);
1530 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1533 case PW_TYPE_SIGNED:
1534 *(int32_t *)data = strtol(value, 0, 0);
1535 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1538 case PW_TYPE_STRING:
1545 * Expand variables which haven't already been
1546 * expanded automagically when the configuration
1549 if (value == dflt) {
1552 lineno = cs->item.lineno;
1554 value = cf_expand_variables("<internal>",
1556 cs, buffer, sizeof(buffer),
1559 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1564 if (required && !value) goto is_required;
1565 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1568 if (!dict_attrbyname(value)) {
1570 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1573 cf_log_err(&(cp->item), "No such attribute '%s'", value);
1580 * Hide secrets when using "radiusd -X".
1582 if (secret && (rad_debug_lvl <= 2)) {
1583 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1584 cs->depth, parse_spaces, name);
1586 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1587 cs->depth, parse_spaces, name, value ? value : "(null)");
1589 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1592 * If there's data AND it's an input file, check
1593 * that we can read it. This check allows errors
1594 * to be caught as early as possible, during
1597 if (*q && file_input && !cf_file_input(cs, *q)) {
1602 case PW_TYPE_IPV4_ADDR:
1603 case PW_TYPE_IPV4_PREFIX:
1606 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1607 ERROR("%s", fr_strerror());
1610 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1613 case PW_TYPE_IPV6_ADDR:
1614 case PW_TYPE_IPV6_PREFIX:
1617 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1618 ERROR("%s", fr_strerror());
1621 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1624 case PW_TYPE_COMBO_IP_ADDR:
1625 case PW_TYPE_COMBO_IP_PREFIX:
1628 if (fr_pton(ipaddr, value, -1, true) < 0) {
1629 ERROR("%s", fr_strerror());
1632 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1635 case PW_TYPE_TIMEVAL: {
1640 sec = strtoul(value, &end, 10);
1646 len = strlen(end + 1);
1649 ERROR("Too much precision for timeval");
1654 * If they write "0.1", that means
1655 * "10000" microseconds.
1657 sec = strtoul(end + 1, NULL, 10);
1665 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1666 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1667 memcpy(data, &tv, sizeof(tv));
1673 * If we get here, it's a sanity check error.
1674 * It's not an error parsing the configuration
1677 rad_assert(type > PW_TYPE_INVALID);
1678 rad_assert(type < PW_TYPE_MAX);
1680 ERROR("type '%s' is not supported in the configuration files",
1681 fr_int2str(dict_attr_types, type, "?Unknown?"));
1683 } /* switch over variable type */
1688 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1689 if (!cpn) return -1;
1691 cpn->item.filename = "<internal>";
1692 cpn->item.lineno = 0;
1693 cf_item_add(cs, &(cpn->item));
1701 * A copy of cf_section_parse that initializes pointers before
1704 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1705 CONF_PARSER const *variables)
1709 for (i = 0; variables[i].name != NULL; i++) {
1710 if (variables[i].type == PW_TYPE_SUBSECTION) {
1711 CONF_SECTION *subcs;
1713 if (!variables[i].dflt) continue;
1715 subcs = cf_section_sub_find(cs, variables[i].name);
1718 * If there's no subsection in the
1719 * config, BUT the CONF_PARSER wants one,
1720 * then create an empty one. This is so
1721 * that we can track the strings,
1722 * etc. allocated in the subsection.
1725 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1728 subcs->item.filename = cs->item.filename;
1729 subcs->item.lineno = cs->item.lineno;
1730 cf_item_add(cs, &(subcs->item));
1733 cf_section_parse_init(subcs, (uint8_t *)base + variables[i].offset,
1734 (CONF_PARSER const *) variables[i].dflt);
1738 if ((variables[i].type != PW_TYPE_STRING) &&
1739 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1740 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1744 if (variables[i].data) {
1745 *(char **) variables[i].data = NULL;
1747 *(char **) (((char *)base) + variables[i].offset) = NULL;
1751 } /* for all variables in the configuration section */
1755 static void cf_section_parse_warn(CONF_SECTION *cs)
1759 for (ci = cs->children; ci; ci = ci->next) {
1761 * Don't recurse on sections. We can only safely
1762 * check conf pairs at the same level as the
1763 * section that was just parsed.
1765 if (ci->type == CONF_ITEM_SECTION) continue;
1766 if (ci->type == CONF_ITEM_PAIR) {
1769 cp = cf_item_to_pair(ci);
1770 if (cp->parsed) continue;
1772 WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
1773 cp->item.filename ? cp->item.filename : "unknown",
1774 cp->item.lineno ? cp->item.lineno : 0,
1779 * Skip everything else.
1785 * Parse a configuration section into user-supplied variables.
1787 int cf_section_parse(CONF_SECTION *cs, void *base,
1788 CONF_PARSER const *variables)
1794 cs->variables = variables; /* this doesn't hurt anything */
1797 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1800 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1801 cs->name1, cs->name2);
1804 cf_section_parse_init(cs, base, variables);
1807 * Handle the known configuration parameters.
1809 for (i = 0; variables[i].name != NULL; i++) {
1811 * Handle subsections specially
1813 if (variables[i].type == PW_TYPE_SUBSECTION) {
1814 CONF_SECTION *subcs;
1816 subcs = cf_section_sub_find(cs, variables[i].name);
1818 * Default in this case is overloaded to mean a pointer
1819 * to the CONF_PARSER struct for the subsection.
1821 if (!variables[i].dflt || !subcs) {
1822 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1826 if (cf_section_parse(subcs, (uint8_t *)base + variables[i].offset,
1827 (CONF_PARSER const *) variables[i].dflt) < 0) goto error;
1829 } /* else it's a CONF_PAIR */
1831 if (variables[i].data) {
1832 data = variables[i].data; /* prefer this. */
1834 data = ((char *)base) + variables[i].offset;
1836 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1841 * Parse the pair we found, or a default value.
1843 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1846 * Be nice, and print the name of the new config item.
1848 if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1849 (variables[i + 1].data == variables[i].data)) {
1850 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1851 variables[i + 1].name);
1856 } /* for all variables in the configuration section */
1859 * Warn about items in the configuration which weren't
1860 * checked during parsing.
1862 if (rad_debug_lvl >= 3) cf_section_parse_warn(cs);
1864 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1871 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1877 * Check XLAT things in pass 2. But don't cache the xlat stuff anywhere.
1879 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1888 * Handle the known configuration parameters.
1890 for (i = 0; variables[i].name != NULL; i++) {
1895 * Handle subsections specially
1897 if (variables[i].type == PW_TYPE_SUBSECTION) {
1898 CONF_SECTION *subcs;
1899 subcs = cf_section_sub_find(cs, variables[i].name);
1901 if (cf_section_parse_pass2(subcs, (uint8_t *)base + variables[i].offset,
1902 (CONF_PARSER const *) variables[i].dflt) < 0) {
1906 } /* else it's a CONF_PAIR */
1909 * Figure out which data we need to fix.
1911 if (variables[i].data) {
1912 data = variables[i].data; /* prefer this. */
1914 data = ((char *)base) + variables[i].offset;
1919 cp = cf_pair_find(cs, variables[i].name);
1923 if (!cp || !cp->value || !data) continue;
1925 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1926 (cp->rhs_type != T_BARE_WORD)) continue;
1929 * Non-xlat expansions shouldn't have xlat!
1931 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1932 ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1934 * Ignore %{... in shared secrets.
1935 * They're never dynamically expanded.
1937 if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
1939 if (strstr(cp->value, "%{") != NULL) {
1940 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
1941 cp->item.filename ? cp->item.filename : "unknown",
1942 cp->item.lineno ? cp->item.lineno : 0);
1948 * Parse (and throw away) the xlat string.
1950 * FIXME: All of these should be converted from PW_TYPE_XLAT
1953 if ((variables[i].type & PW_TYPE_XLAT) != 0) {
1955 * xlat expansions should be parseable.
1957 value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
1960 slen = xlat_tokenize(cs, value, &xlat, &error);
1962 char *spaces, *text;
1965 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
1967 cf_log_err(&cp->item, "Failed parsing expanded string:");
1968 cf_log_err(&cp->item, "%s", text);
1969 cf_log_err(&cp->item, "%s^ %s", spaces, error);
1971 talloc_free(spaces);
1983 * Convert the LITERAL template to the actual
1986 if ((variables[i].type & PW_TYPE_TMPL) != 0) {
1989 slen = tmpl_afrom_str(cs, &vpt, cp->value, talloc_array_length(cp->value) - 1,
1991 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
1993 error = fr_strerror();
2000 * Don't add default - update with new types.
2002 switch (vpt->type) {
2004 * All attributes should have been defined by this point.
2006 case TMPL_TYPE_ATTR_UNDEFINED:
2007 cf_log_err(&cp->item, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
2010 case TMPL_TYPE_LITERAL:
2011 case TMPL_TYPE_ATTR:
2012 case TMPL_TYPE_LIST:
2013 case TMPL_TYPE_DATA:
2014 case TMPL_TYPE_EXEC:
2015 case TMPL_TYPE_XLAT:
2016 case TMPL_TYPE_XLAT_STRUCT:
2019 case TMPL_TYPE_UNKNOWN:
2020 case TMPL_TYPE_REGEX:
2021 case TMPL_TYPE_REGEX_STRUCT:
2022 case TMPL_TYPE_NULL:
2026 talloc_free(*(vp_tmpl_t **)data);
2027 *(vp_tmpl_t **)data = vpt;
2031 * If the "multi" flag is set, check all of them.
2033 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
2034 cp = cf_pair_find_next(cs, cp, cp->attr);
2037 } /* for all variables in the configuration section */
2043 * Merge the template so everyting else "just works".
2045 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
2049 if (!cs || !template) return true;
2051 cs->template = NULL;
2054 * Walk over the template, adding its' entries to the
2055 * current section. But only if the entries don't
2056 * already exist in the current section.
2058 for (ci = template->children; ci; ci = ci->next) {
2059 if (ci->type == CONF_ITEM_PAIR) {
2060 CONF_PAIR *cp1, *cp2;
2063 * It exists, don't over-write it.
2065 cp1 = cf_item_to_pair(ci);
2066 if (cf_pair_find(cs, cp1->attr)) {
2071 * Create a new pair with all of the data
2074 cp2 = cf_pair_dup(cs, cp1);
2075 if (!cp2) return false;
2077 cp2->item.filename = cp1->item.filename;
2078 cp2->item.lineno = cp1->item.lineno;
2080 cf_item_add(cs, &(cp2->item));
2084 if (ci->type == CONF_ITEM_SECTION) {
2085 CONF_SECTION *subcs1, *subcs2;
2087 subcs1 = cf_item_to_section(ci);
2088 rad_assert(subcs1 != NULL);
2090 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
2093 * sub-sections get merged.
2095 if (!cf_template_merge(subcs2, subcs1)) {
2102 * Our section doesn't have a matching
2103 * sub-section. Copy it verbatim from
2106 subcs2 = cf_section_dup(cs, subcs1,
2107 cf_section_name1(subcs1), cf_section_name2(subcs1),
2109 if (!subcs2) return false;
2111 subcs2->item.filename = subcs1->item.filename;
2112 subcs2->item.lineno = subcs1->item.lineno;
2114 cf_item_add(cs, &(subcs2->item));
2118 /* ignore everything else */
2124 static char const *cf_local_file(char const *base, char const *filename,
2125 char *buffer, size_t bufsize)
2130 strlcpy(buffer, base, bufsize);
2132 p = strrchr(buffer, FR_DIR_SEP);
2133 if (!p) return filename;
2134 if (p[1]) { /* ./foo */
2138 dirsize = (p - buffer) + 1;
2140 if ((dirsize + strlen(filename)) >= bufsize) {
2144 strlcpy(p + 1, filename, bufsize - dirsize);
2151 * Read a part of the config file.
2153 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
2154 CONF_SECTION *current)
2157 CONF_SECTION *this, *css;
2166 FR_TOKEN t1 = T_INVALID, t2, t3;
2167 bool has_spaces = false;
2172 this = current; /* add items here */
2175 * Read, checking for line continuations ('\\' at EOL)
2182 * Get data, and remember if we are at EOF.
2184 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
2188 * We read the entire 8k worth of data: complain.
2189 * Note that we don't care if the last character
2190 * is \n: it's still forbidden. This means that
2191 * the maximum allowed length of text is 8k-1, which
2195 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
2196 ERROR("%s[%d]: Line too long",
2203 while (isspace((int) *ptr)) ptr++;
2206 memmove(cbuf, ptr, len - (ptr - cbuf));
2207 len -= (ptr - cbuf);
2212 * Not doing continuations: check for edge
2219 while (*ptr && isspace((int) *ptr)) ptr++;
2221 if (!*ptr || (*ptr == '#')) continue;
2223 } else if (at_eof || (len == 0)) {
2224 ERROR("%s[%d]: Continuation at EOF is illegal",
2230 * See if there's a continuation.
2233 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
2238 if ((len > 0) && (cbuf[len - 1] == '\\')) {
2240 * Check for "suppress spaces" magic.
2242 if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
2246 cbuf[len - 1] = '\0';
2258 * The parser is getting to be evil.
2260 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
2262 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
2266 if (ptr[0] == '%') {
2267 hack = rad_copy_variable(buf1, ptr);
2269 hack = rad_copy_string(buf1, ptr);
2272 ERROR("%s[%d]: Invalid expansion: %s",
2273 filename, *lineno, ptr);
2279 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2286 ERROR("%s[%d]: Invalid expansion: %s",
2287 filename, *lineno, ptr);
2291 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
2295 * The caller eats "name1 name2 {", and calls us
2296 * for the data inside of the section. So if we
2297 * receive a closing brace, then it must mean the
2298 * end of the section.
2300 if (t1 == T_RCBRACE) {
2301 if (this == current) {
2302 ERROR("%s[%d]: Too many closing braces",
2308 * Merge the template into the existing
2309 * section. This uses more memory, but
2310 * means that templates now work with
2311 * sub-sections, etc.
2313 if (!cf_template_merge(this, this->template)) {
2317 this = this->item.parent;
2318 goto check_for_more;
2321 if (t1 != T_BARE_WORD) goto skip_keywords;
2324 * Allow for $INCLUDE files
2326 * This *SHOULD* work for any level include.
2327 * I really really really hate this file. -cparker
2329 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2330 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2331 bool relative = true;
2333 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2335 ERROR("%s[%d]: Unexpected text after $INCLUDE",
2340 if (buf2[0] == '$') relative = false;
2342 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2, NULL);
2343 if (!value) return -1;
2345 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2348 value = cf_local_file(filename, value, buf3,
2351 ERROR("%s[%d]: Directories too deep.",
2358 #ifdef HAVE_DIRENT_H
2362 * Include ALL non-"dot" files in the directory.
2365 if (value[strlen(value) - 1] == '/') {
2368 struct stat stat_buf;
2370 DEBUG2("including files in directory %s", value );
2375 if (stat(value, &stat_buf) < 0) {
2376 ERROR("%s[%d]: Failed reading directory %s: %s",
2378 value, fr_syserror(errno));
2382 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2383 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
2384 "insecure configuration", filename, *lineno, value);
2388 dir = opendir(value);
2390 ERROR("%s[%d]: Error reading directory %s: %s",
2391 filename, *lineno, value,
2392 fr_syserror(errno));
2397 * Read the directory, ignoring "." files.
2399 while ((dp = readdir(dir)) != NULL) {
2402 if (dp->d_name[0] == '.') continue;
2405 * Check for valid characters
2407 for (p = dp->d_name; *p != '\0'; p++) {
2408 if (isalpha((int)*p) ||
2412 (*p == '.')) continue;
2415 if (*p != '\0') continue;
2417 snprintf(buf2, sizeof(buf2), "%s%s",
2419 if ((stat(buf2, &stat_buf) != 0) ||
2420 S_ISDIR(stat_buf.st_mode)) continue;
2422 * Read the file into the current
2423 * configuration section.
2425 if (cf_file_include(this, buf2) < 0) {
2433 { /* it was a normal file */
2434 if (buf1[1] == '-') {
2435 struct stat statbuf;
2437 if (stat(value, &statbuf) < 0) {
2438 WARN("Not including file %s: %s", value, fr_syserror(errno));
2443 if (cf_file_include(this, value) < 0) {
2448 } /* we were in an include */
2450 if (strcasecmp(buf1, "$template") == 0) {
2452 CONF_SECTION *parentcs, *templatecs;
2453 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2456 ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2460 parentcs = cf_top_section(current);
2462 templatecs = cf_section_sub_find(parentcs, "templates");
2464 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2468 ci = cf_reference_item(parentcs, templatecs, buf2);
2469 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2470 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2475 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2479 if (this->template) {
2480 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2484 this->template = cf_item_to_section(ci);
2489 * Ensure that the user can't add CONF_PAIRs
2490 * with 'internal' names;
2492 if (buf1[0] == '_') {
2493 ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2498 * Handle if/elsif specially.
2500 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2502 char const *error = NULL;
2504 CONF_SECTION *server;
2505 fr_cond_t *cond = NULL;
2508 * if / elsif MUST be inside of a
2509 * processing section, which MUST in turn
2510 * be inside of a "server" directive.
2512 if (!this->item.parent) {
2514 ERROR("%s[%d]: Invalid location for '%s'",
2515 filename, *lineno, buf1);
2520 * Can only have "if" in 3 named sections.
2522 server = this->item.parent;
2524 (strcmp(server->name1, "server") != 0) &&
2525 (strcmp(server->name1, "policy") != 0) &&
2526 (strcmp(server->name1, "instantiate") != 0)) {
2527 server = server->item.parent;
2528 if (!server) goto invalid_location;
2532 * Skip (...) to find the {
2534 slen = fr_condition_tokenize(this, cf_section_to_item(this), ptr, &cond,
2535 &error, FR_COND_TWO_PASS);
2536 memcpy(&p, &ptr, sizeof(p));
2539 if (p[-slen] != '{') goto cond_error;
2545 * This hack is so that the NEXT stage
2546 * doesn't go "too far" in expanding the
2547 * variable. We can parse the conditions
2548 * without expanding the ${...} stuff.
2549 * BUT we don't want to expand all of the
2550 * stuff AFTER the condition. So we do
2553 * The first pass is to discover the end
2554 * of the condition. We then expand THAT
2555 * string, and do a second pass parsing
2556 * the expanded condition.
2562 * If there's a ${...}. If so, expand it.
2564 if (strchr(ptr, '$') != NULL) {
2565 ptr = cf_expand_variables(filename, lineno,
2570 ERROR("%s[%d]: Parse error expanding ${...} in condition",
2574 } /* else leave it alone */
2576 css = cf_section_alloc(this, buf1, ptr);
2578 ERROR("%s[%d]: Failed allocating memory for section",
2582 css->item.filename = filename;
2583 css->item.lineno = *lineno;
2585 slen = fr_condition_tokenize(css, cf_section_to_item(css), ptr, &cond,
2586 &error, FR_COND_TWO_PASS);
2587 *p = '{'; /* put it back */
2591 char *spaces, *text;
2593 fr_canonicalize_error(this, &spaces, &text, slen, ptr);
2595 ERROR("%s[%d]: Parse error in condition",
2597 ERROR("%s[%d]: %s", filename, *lineno, text);
2598 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2600 talloc_free(spaces);
2606 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2608 ERROR("%s[%d]: Condition is too large after \"%s\"",
2609 filename, *lineno, buf1);
2614 * Copy the expanded and parsed condition
2615 * into buf2. Then, parse the text after
2616 * the condition, which now MUST be a '{.
2618 * If it wasn't '{' it would have been
2619 * caught in the first pass of
2620 * conditional parsing, above.
2622 memcpy(buf2, ptr, slen);
2626 if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2628 ERROR("%s[%d]: Expected '{' %d",
2629 filename, *lineno, t3);
2634 * Swap the condition with trailing stuff for
2635 * the final condition.
2637 memcpy(&p, &css->name2, sizeof(css->name2));
2639 css->name2 = talloc_typed_strdup(css, buf2);
2641 cf_item_add(this, &(css->item));
2642 cf_data_add_internal(css, "if", cond, NULL, false);
2645 * The current section is now the child section.
2649 goto check_for_more;
2654 * Grab the next token.
2656 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2673 case T_OP_CMP_FALSE:
2674 if (!this || (strcmp(this->name1, "update") != 0)) {
2675 ERROR("%s[%d]: Invalid operator in assignment",
2683 while (isspace((int) *ptr)) ptr++;
2686 * New parser: non-quoted strings are
2687 * bare words, and we parse everything
2688 * until the next newline, or the next
2689 * comma. If they have { or } in a bare
2690 * word, well... too bad.
2692 if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2693 && (*ptr != '`') && (*ptr != '/')) {
2694 const char *q = ptr;
2697 while (*q && (*q >= ' ') && (*q != ',') &&
2700 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2701 ERROR("%s[%d]: Parse error: value too long",
2706 memcpy(buf3, ptr, (q - ptr));
2707 buf3[q - ptr] = '\0';
2711 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2714 if (t3 == T_INVALID) {
2715 ERROR("%s[%d]: Parse error: %s",
2722 * Allow "foo" by itself, or "foo = bar"
2728 case T_DOUBLE_QUOTED_STRING:
2729 case T_BACK_QUOTED_STRING:
2730 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3, &soft_fail);
2732 if (!soft_fail) return -1;
2735 * References an item which doesn't exist,
2736 * or which is already marked up as being
2737 * expanded in pass2. Wait for pass2 to
2738 * do the expansions.
2756 * Add this CONF_PAIR to our CONF_SECTION
2759 cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2760 if (!cpn) return -1;
2761 cpn->item.filename = filename;
2762 cpn->item.lineno = *lineno;
2764 cf_item_add(this, &(cpn->item));
2767 * Hacks for escaping
2769 if (!cf_new_escape && !this->item.parent && value &&
2770 (strcmp(buf1, "correct_escapes") == 0) &&
2771 ((strcmp(value, "true") == 0) ||
2772 (strcmp(value, "yes") == 0) ||
2773 (strcmp(value, "1") == 0))) {
2774 cf_new_escape = true;
2778 * Require a comma, unless there's a comment.
2780 while (isspace(*ptr)) ptr++;
2789 * foo = bar # other stuff
2791 if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2793 if (!*ptr || (*ptr == '}')) break;
2795 ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2796 filename, *lineno, value, ptr);
2800 * No '=', must be a section or sub-section.
2803 case T_DOUBLE_QUOTED_STRING:
2804 case T_SINGLE_QUOTED_STRING:
2805 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2806 if (t3 != T_LCBRACE) {
2807 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2808 filename, *lineno, buf1, buf2);
2814 css = cf_section_alloc(this, buf1,
2815 t2 == T_LCBRACE ? NULL : buf2);
2817 ERROR("%s[%d]: Failed allocating memory for section",
2822 css->item.filename = filename;
2823 css->item.lineno = *lineno;
2824 cf_item_add(this, &(css->item));
2827 * There may not be a name2
2829 css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2832 * The current section is now the child section.
2838 ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2843 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2844 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2851 * Done parsing one thing. Skip to EOL if possible.
2853 while (isspace(*ptr)) ptr++;
2855 if (*ptr == '#') continue;
2864 * See if EOF was unexpected ..
2866 if (feof(fp) && (this != current)) {
2867 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2868 filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2876 * Include one config file in another.
2878 static int cf_file_include(CONF_SECTION *cs, char const *filename_in)
2882 char const *filename;
2885 * So we only need to do this once.
2887 filename = talloc_strdup(cs, filename_in);
2889 DEBUG2("including configuration file %s", filename);
2891 fp = cf_file_open(cs, filename);
2894 if (!cs->item.filename) cs->item.filename = filename;
2897 * Read the section. It's OK to have EOF without a
2898 * matching close brace.
2900 if (cf_section_read(filename, &lineno, fp, cs) < 0) {
2911 * Do variable expansion in pass2.
2913 * This is a breadth-first expansion. "deep
2915 static int cf_section_pass2(CONF_SECTION *cs)
2919 for (ci = cs->children; ci; ci = ci->next) {
2924 if (ci->type != CONF_ITEM_PAIR) continue;
2926 cp = cf_item_to_pair(ci);
2927 if (!cp->value || !cp->pass2) continue;
2929 rad_assert((cp->rhs_type == T_BARE_WORD) ||
2930 (cp->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2931 (cp->rhs_type == T_BACK_QUOTED_STRING));
2933 value = cf_expand_variables(ci->filename, &ci->lineno, cs, buffer, sizeof(buffer), cp->value, NULL);
2934 if (!value) return -1;
2936 rad_const_free(cp->value);
2937 cp->value = talloc_typed_strdup(cp, value);
2940 for (ci = cs->children; ci; ci = ci->next) {
2941 if (ci->type != CONF_ITEM_SECTION) continue;
2943 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
2951 * Bootstrap a config file.
2953 int cf_file_read(CONF_SECTION *cs, char const *filename)
2959 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
2962 p = strrchr(cp->value, FR_DIR_SEP);
2965 cp->item.filename = "<internal>";
2966 cp->item.lineno = -1;
2967 cf_item_add(cs, &(cp->item));
2969 tree = rbtree_create(cs, filename_cmp, NULL, 0);
2970 if (!tree) return -1;
2972 cf_data_add_internal(cs, "filename", tree, NULL, 0);
2974 if (cf_file_include(cs, filename) < 0) return -1;
2977 * Now that we've read the file, go back through it and
2978 * expand the variables.
2980 if (cf_section_pass2(cs) < 0) return -1;
2986 void cf_file_free(CONF_SECTION *cs)
2993 * Return a CONF_PAIR within a CONF_SECTION.
2995 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
2997 CONF_PAIR *cp, mycp;
2999 if (!cs || !name) return NULL;
3002 cp = rbtree_finddata(cs->pair_tree, &mycp);
3005 if (!cs->template) return NULL;
3007 return rbtree_finddata(cs->template->pair_tree, &mycp);
3011 * Return the attr of a CONF_PAIR
3014 char const *cf_pair_attr(CONF_PAIR const *pair)
3016 return (pair ? pair->attr : NULL);
3020 * Return the value of a CONF_PAIR
3023 char const *cf_pair_value(CONF_PAIR const *pair)
3025 return (pair ? pair->value : NULL);
3028 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
3030 return (pair ? pair->op : T_INVALID);
3033 /** Return the value (lhs) type
3035 * @param pair to extract value type from.
3036 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3037 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3039 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
3041 return (pair ? pair->lhs_type : T_INVALID);
3044 /** Return the value (rhs) type
3046 * @param pair to extract value type from.
3047 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3048 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3050 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
3052 return (pair ? pair->rhs_type : T_INVALID);
3056 * Turn a CONF_PAIR into a VALUE_PAIR
3057 * For now, ignore the "value_type" field...
3059 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
3062 fr_strerror_printf("Internal error");
3067 fr_strerror_printf("No value given for attribute %s", pair->attr);
3072 * false comparisons never match. BUT if it's a "string"
3073 * or `string`, then remember to expand it later.
3075 if ((pair->op != T_OP_CMP_FALSE) &&
3076 ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
3077 (pair->rhs_type == T_BACK_QUOTED_STRING))) {
3080 vp = pairmake(pair, NULL, pair->attr, NULL, pair->op);
3085 if (pairmark_xlat(vp, pair->value) < 0) {
3094 return pairmake(pair, NULL, pair->attr, pair->value, pair->op);
3098 * Return the first label of a CONF_SECTION
3101 char const *cf_section_name1(CONF_SECTION const *cs)
3103 return (cs ? cs->name1 : NULL);
3107 * Return the second label of a CONF_SECTION
3110 char const *cf_section_name2(CONF_SECTION const *cs)
3112 return (cs ? cs->name2 : NULL);
3115 /** Return name2 if set, else name1
3118 char const *cf_section_name(CONF_SECTION const *cs)
3122 name = cf_section_name2(cs);
3123 if (name) return name;
3125 return cf_section_name1(cs);
3129 * Find a value in a CONF_SECTION
3131 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
3135 cp = cf_pair_find(cs, attr);
3137 return (cp ? cp->value : NULL);
3141 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
3142 char const *name1, char const *name2)
3145 CONF_ITEM const *ci;
3147 if (!cs || !name1) return NULL;
3149 for (ci = &(cs->item); ci; ci = ci->next) {
3150 if (ci->type != CONF_ITEM_SECTION)
3153 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
3157 their2 = cf_item_to_section(ci)->name2;
3159 if ((!name2 && !their2) ||
3160 (name2 && their2 && (strcmp(name2, their2) == 0))) {
3161 return cf_item_to_section(ci);
3168 /** Find a pair with a name matching attr, after specified pair.
3170 * @param cs to search in.
3171 * @param pair to search from (may be NULL).
3172 * @param attr to find (may be NULL in which case any attribute matches).
3173 * @return the next matching CONF_PAIR or NULL if none matched.
3175 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
3176 CONF_PAIR const *pair, char const *attr)
3180 if (!cs) return NULL;
3183 * If pair is NULL and we're trying to find a specific
3184 * attribute this must be a first time run.
3186 * Find the pair with correct name.
3188 if (!pair && attr) return cf_pair_find(cs, attr);
3191 * Start searching from the next child, or from the head
3192 * of the list of children (if no pair was provided).
3194 for (ci = pair ? pair->item.next : cs->children;
3197 if (ci->type != CONF_ITEM_PAIR) continue;
3199 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
3202 return cf_item_to_pair(ci);
3206 * Find a CONF_SECTION, or return the root if name is NULL
3209 CONF_SECTION *cf_section_find(char const *name)
3212 return cf_section_sub_find(root_config, name);
3217 /** Find a sub-section in a section
3219 * This finds ANY section having the same first name.
3220 * The second name is ignored.
3222 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
3226 if (!cs || !name) return NULL; /* can't find an un-named section */
3229 * No sub-sections have been defined, so none exist.
3231 if (!cs->section_tree) return NULL;
3235 return rbtree_finddata(cs->section_tree, &mycs);
3239 /** Find a CONF_SECTION with both names.
3242 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
3243 char const *name1, char const *name2)
3247 if (!cs) cs = root_config;
3248 if (!cs) return NULL;
3251 CONF_SECTION mycs, *master_cs;
3253 if (!cs->section_tree) return NULL;
3258 master_cs = rbtree_finddata(cs->section_tree, &mycs);
3259 if (!master_cs) return NULL;
3262 * Look it up in the name2 tree. If it's there,
3265 if (master_cs->name2_tree) {
3266 CONF_SECTION *subcs;
3268 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
3269 if (subcs) return subcs;
3273 * We don't insert ourselves into the name2 tree.
3274 * So if there's nothing in the name2 tree, maybe
3275 * *we* are the answer.
3277 if (!master_cs->name2 && name2) return NULL;
3278 if (master_cs->name2 && !name2) return NULL;
3279 if (!master_cs->name2 && !name2) return master_cs;
3281 if (strcmp(master_cs->name2, name2) == 0) {
3289 * Else do it the old-fashioned way.
3291 for (ci = cs->children; ci; ci = ci->next) {
3292 CONF_SECTION *subcs;
3294 if (ci->type != CONF_ITEM_SECTION)
3297 subcs = cf_item_to_section(ci);
3298 if (!subcs->name2) {
3299 if (strcmp(subcs->name1, name2) == 0) break;
3301 if (strcmp(subcs->name2, name2) == 0) break;
3305 return cf_item_to_section(ci);
3309 * Return the next subsection after a CONF_SECTION
3310 * with a certain name1 (char *name1). If the requested
3311 * name1 is NULL, any name1 matches.
3314 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
3315 CONF_SECTION const *subsection,
3320 if (!section) return NULL;
3323 * If subsection is NULL this must be a first time run
3324 * Find the subsection with correct name
3328 ci = section->children;
3330 ci = subsection->item.next;
3333 for (; ci; ci = ci->next) {
3334 if (ci->type != CONF_ITEM_SECTION)
3336 if ((name1 == NULL) ||
3337 (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3341 return cf_item_to_section(ci);
3346 * Return the next section after a CONF_SECTION
3347 * with a certain name1 (char *name1). If the requested
3348 * name1 is NULL, any name1 matches.
3351 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3352 CONF_SECTION const *subsection,
3355 if (!section) return NULL;
3357 if (!section->item.parent) return NULL;
3359 return cf_subsection_find_next(section->item.parent, subsection, name1);
3362 /** Return the next item after a CONF_ITEM.
3365 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3367 if (!section) return NULL;
3370 * If item is NULL this must be a first time run
3371 * Return the first item
3374 return section->children;
3380 static void _pair_count(int *count, CONF_SECTION const *cs)
3382 CONF_ITEM const *ci;
3384 for (ci = cf_item_find_next(cs, NULL);
3386 ci = cf_item_find_next(cs, ci)) {
3388 if (cf_item_is_section(ci)) {
3389 _pair_count(count, cf_item_to_section(ci));
3397 /** Count the number of conf pairs beneath a section
3399 * @param[in] cs to search for items in.
3400 * @return number of pairs nested within section.
3402 int cf_pair_count(CONF_SECTION const *cs)
3406 _pair_count(&count, cs);
3411 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3413 if (!ci) return NULL;
3418 int cf_section_lineno(CONF_SECTION const *section)
3420 return section->item.lineno;
3423 char const *cf_pair_filename(CONF_PAIR const *pair)
3425 return pair->item.filename;
3428 char const *cf_section_filename(CONF_SECTION const *section)
3430 return section->item.filename;
3433 int cf_pair_lineno(CONF_PAIR const *pair)
3435 return pair->item.lineno;
3438 bool cf_item_is_section(CONF_ITEM const *item)
3440 return item->type == CONF_ITEM_SECTION;
3443 bool cf_item_is_pair(CONF_ITEM const *item)
3445 return item->type == CONF_ITEM_PAIR;
3449 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3450 void *data, void (*data_free)(void *))
3454 cd = talloc_zero(parent, CONF_DATA);
3455 if (!cd) return NULL;
3457 cd->item.type = CONF_ITEM_DATA;
3458 cd->item.parent = parent;
3459 cd->name = talloc_typed_strdup(cd, name);
3466 cd->free = data_free;
3469 talloc_set_destructor(cd, _cf_data_free);
3475 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3477 if (!cs || !name) return NULL;
3480 * Find the name in the tree, for speed.
3482 if (cs->data_tree) {
3487 return rbtree_finddata(cs->data_tree, &mycd);
3494 * Find data from a particular section.
3496 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3498 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3500 if (cd) return cd->data;
3506 * Add named data to a configuration section.
3508 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3509 void *data, void (*data_free)(void *),
3514 if (!cs || !name) return -1;
3517 * Already exists. Can't add it.
3519 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3521 cd = cf_data_alloc(cs, name, data, data_free);
3525 cf_item_add(cs, cf_data_to_item(cd));
3531 * Add named data to a configuration section.
3533 int cf_data_add(CONF_SECTION *cs, char const *name,
3534 void *data, void (*data_free)(void *))
3536 return cf_data_add_internal(cs, name, data, data_free, 0);
3539 /** Remove named data from a configuration section
3542 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3548 if (!cs || !name) return NULL;
3549 if (!cs->data_tree) return NULL;
3552 * Find the name in the tree, for speed.
3556 cd = rbtree_finddata(cs->data_tree, &mycd);
3557 if (!cd) return NULL;
3559 talloc_set_destructor(cd, NULL); /* Disarm the destructor */
3560 rbtree_deletebydata(cs->data_tree, &mycd);
3569 * This is here to make the rest of the code easier to read. It
3570 * ties conffile.c to log.c, but it means we don't have to
3571 * pollute every other function with the knowledge of the
3572 * configuration internals.
3574 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3580 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3585 ci->filename ? ci->filename : "unknown",
3586 ci->lineno ? ci->lineno : 0,
3589 ERROR("<unknown>[*]: %s", buffer);
3593 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3599 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3602 rad_assert(cs != NULL);
3605 cs->item.filename ? cs->item.filename : "unknown",
3606 cs->item.lineno ? cs->item.lineno : 0,
3610 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3616 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3619 rad_assert(cp != NULL);
3622 cp->item.filename ? cp->item.filename : "unknown",
3623 cp->item.lineno ? cp->item.lineno : 0,
3627 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3632 if ((rad_debug_lvl > 1) && cs) vradlog(L_DBG, fmt, ap);
3637 * Wrapper to simplify the code.
3639 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3645 if (rad_debug_lvl > 1 && cs) {
3646 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3648 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3653 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3655 if (!cs) return NULL;
3657 return cs->variables;
3661 * For "switch" and "case" statements.
3663 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3665 if (!cs) return T_INVALID;
3667 return cs->name2_type;