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 bool check_config = false;
47 typedef enum conf_property {
48 CONF_PROPERTY_INVALID = 0,
50 CONF_PROPERTY_INSTANCE,
53 static const FR_NAME_NUMBER conf_property_name[] = {
54 { "name", CONF_PROPERTY_NAME},
55 { "instance", CONF_PROPERTY_INSTANCE},
60 typedef enum conf_type {
61 CONF_ITEM_INVALID = 0,
68 struct conf_item *next; //!< Sibling.
69 struct conf_part *parent; //!< Parent.
70 int lineno; //!< The line number the config item began on.
71 char const *filename; //!< The file the config item was parsed from.
72 CONF_ITEM_TYPE type; //!< Whether the config item is a config_pair, conf_section or conf_data.
75 /** Configuration AVP similar to a VALUE_PAIR
80 char const *attr; //!< Attribute name
81 char const *value; //!< Attribute value
82 FR_TOKEN op; //!< Operator e.g. =, :=
83 FR_TOKEN lhs_type; //!< Name quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
84 FR_TOKEN rhs_type; //!< Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
85 bool pass2; //!< do expansion in pass2.
86 bool parsed; //!< Was this item used during parsing?
89 /** Internal data that is associated with a configuration section
96 void *data; //!< User data
97 void (*free)(void *); //!< Free user data function
102 char const *name1; //!< First name token. Given ``foo bar {}`` would be ``foo``.
103 char const *name2; //!< Second name token. Given ``foo bar {}`` would be ``bar``.
105 FR_TOKEN name2_type; //!< The type of quoting around name2.
108 CONF_ITEM *tail; //!< For speed.
109 CONF_SECTION *template;
111 rbtree_t *pair_tree; //!< and a partridge..
112 rbtree_t *section_tree; //!< no jokes here.
113 rbtree_t *name2_tree; //!< for sections of the same name2
119 CONF_PARSER const *variables;
122 typedef struct cf_file_t {
123 char const *filename;
129 CONF_SECTION *root_config = NULL;
130 bool cf_new_escape = false;
133 static int cf_data_add_internal(CONF_SECTION *cs, char const *name, void *data,
134 void (*data_free)(void *), int flag);
136 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag);
138 static char const *cf_expand_variables(char const *cf, int *lineno,
139 CONF_SECTION *outercs,
140 char *output, size_t outsize,
141 char const *input, bool *soft_fail);
143 static int cf_file_include(CONF_SECTION *cs, char const *filename_in);
148 * Isolate the scary casts in these tiny provably-safe functions
151 /** Cast a CONF_ITEM to a CONF_PAIR
154 CONF_PAIR *cf_item_to_pair(CONF_ITEM const *ci)
158 if (ci == NULL) return NULL;
160 rad_assert(ci->type == CONF_ITEM_PAIR);
162 memcpy(&out, &ci, sizeof(out));
166 /** Cast a CONF_ITEM to a CONF_SECTION
169 CONF_SECTION *cf_item_to_section(CONF_ITEM const *ci)
173 if (ci == NULL) return NULL;
175 rad_assert(ci->type == CONF_ITEM_SECTION);
177 memcpy(&out, &ci, sizeof(out));
181 /** Cast a CONF_PAIR to a CONF_ITEM
184 CONF_ITEM *cf_pair_to_item(CONF_PAIR const *cp)
188 if (cp == NULL) return NULL;
190 memcpy(&out, &cp, sizeof(out));
194 /** Cast a CONF_SECTION to a CONF_ITEM
197 CONF_ITEM *cf_section_to_item(CONF_SECTION const *cs)
201 if (cs == NULL) return NULL;
203 memcpy(&out, &cs, sizeof(out));
207 /** Cast CONF_DATA to a CONF_ITEM
210 static CONF_ITEM *cf_data_to_item(CONF_DATA const *cd)
218 memcpy(&out, &cd, sizeof(out));
222 static int _cf_data_free(CONF_DATA *cd)
224 if (cd->free) cd->free(cd->data);
230 * rbtree callback function
232 static int pair_cmp(void const *a, void const *b)
234 CONF_PAIR const *one = a;
235 CONF_PAIR const *two = b;
237 return strcmp(one->attr, two->attr);
242 * rbtree callback function
244 static int section_cmp(void const *a, void const *b)
246 CONF_SECTION const *one = a;
247 CONF_SECTION const *two = b;
249 return strcmp(one->name1, two->name1);
254 * rbtree callback function
256 static int name2_cmp(void const *a, void const *b)
258 CONF_SECTION const *one = a;
259 CONF_SECTION const *two = b;
261 rad_assert(strcmp(one->name1, two->name1) == 0);
263 if (!one->name2 && !two->name2) return 0;
264 if (one->name2 && !two->name2) return -1;
265 if (!one->name2 && two->name2) return +1;
267 return strcmp(one->name2, two->name2);
272 * rbtree callback function
274 static int data_cmp(void const *a, void const *b)
278 CONF_DATA const *one = a;
279 CONF_DATA const *two = b;
281 rcode = one->flag - two->flag;
282 if (rcode != 0) return rcode;
284 return strcmp(one->name, two->name);
288 * Functions for tracking filenames.
290 static int filename_cmp(void const *a, void const *b)
292 cf_file_t const *one = a;
293 cf_file_t const *two = b;
295 if (one->buf.st_dev < two->buf.st_dev) return -1;
296 if (one->buf.st_dev > two->buf.st_dev) return +1;
298 if (one->buf.st_ino < two->buf.st_ino) return -1;
299 if (one->buf.st_ino > two->buf.st_ino) return +1;
304 static FILE *cf_file_open(CONF_SECTION *cs, char const *filename)
313 top = cf_top_section(cs);
314 cd = cf_data_find_internal(top, "filename", 0);
315 if (!cd) return NULL;
319 fp = fopen(filename, "r");
321 ERROR("Unable to open file \"%s\": %s",
322 filename, fr_syserror(errno));
328 file = talloc(tree, cf_file_t);
334 file->filename = filename;
338 if (fstat(fd, &file->buf) == 0) {
340 if ((file->buf.st_mode & S_IWOTH) != 0) {
341 ERROR("Configuration file %s is globally writable. "
342 "Refusing to start due to insecure configuration.", filename);
352 * We can include the same file twice. e.g. when it
353 * contains common definitions, such as for SQL.
355 * Though the admin should really use templates for that.
357 if (!rbtree_insert(tree, file)) {
365 * Do some checks on the file as an "input" file. i.e. one read
368 static bool cf_file_input(CONF_SECTION *cs, char const *filename)
375 top = cf_top_section(cs);
376 cd = cf_data_find_internal(top, "filename", 0);
377 if (!cd) return false;
381 file = talloc(tree, cf_file_t);
382 if (!file) return false;
384 file->filename = filename;
388 if (stat(filename, &file->buf) < 0) {
389 ERROR("Unable to open file \"%s\": %s", filename, fr_syserror(errno));
395 if ((file->buf.st_mode & S_IWOTH) != 0) {
396 ERROR("Configuration file %s is globally writable. "
397 "Refusing to start due to insecure configuration.", filename);
404 * It's OK to include the same file twice...
406 if (!rbtree_insert(tree, file)) {
416 * Return 0 for keep going, 1 for stop.
418 static int file_callback(void *ctx, void *data)
422 cf_file_t *file = data;
425 * The file doesn't exist or we can no longer read it.
427 if (stat(file->filename, &buf) < 0) {
428 *rcode = CF_FILE_ERROR;
433 * The file changed, we'll need to re-read it.
435 if (buf.st_mtime != file->buf.st_mtime) {
437 * Set none -> whatever
439 if (*rcode == CF_FILE_NONE) {
441 *rcode = CF_FILE_CONFIG;
445 *rcode = CF_FILE_MODULE;
451 * A module WAS changed, but now we discover that
452 * a main config file has changed. We might as
453 * well re-load everything.
455 if ((*rcode == CF_FILE_MODULE) && !file->input) {
456 *rcode = CF_FILE_CONFIG;
466 * See if any of the files have changed.
468 int cf_file_changed(CONF_SECTION *cs)
475 top = cf_top_section(cs);
476 cd = cf_data_find_internal(top, "filename", 0);
477 if (!cd) return true;
481 rcode = CF_FILE_NONE;
482 (void) rbtree_walk(tree, RBTREE_IN_ORDER, file_callback, &rcode);
487 static int _cf_section_free(CONF_SECTION *cs)
490 * Name1 and name2 are allocated contiguous with
494 rbtree_free(cs->pair_tree);
495 cs->pair_tree = NULL;
497 if (cs->section_tree) {
498 rbtree_free(cs->section_tree);
499 cs->section_tree = NULL;
501 if (cs->name2_tree) {
502 rbtree_free(cs->name2_tree);
503 cs->name2_tree = NULL;
506 rbtree_free(cs->data_tree);
507 cs->data_tree = NULL;
513 /** Allocate a CONF_PAIR
515 * @param parent CONF_SECTION to hang this CONF_PAIR off of.
517 * @param value of CONF_PAIR.
518 * @param op T_OP_EQ, T_OP_SET etc.
519 * @param lhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
520 * @param rhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
521 * @return NULL on error, else a new CONF_SECTION parented by parent.
523 CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value,
524 FR_TOKEN op, FR_TOKEN lhs_type, FR_TOKEN rhs_type)
528 rad_assert(fr_equality_op[op] || fr_assignment_op[op]);
529 if (!attr) return NULL;
531 cp = talloc_zero(parent, CONF_PAIR);
532 if (!cp) return NULL;
534 cp->item.type = CONF_ITEM_PAIR;
535 cp->item.parent = parent;
536 cp->lhs_type = lhs_type;
537 cp->rhs_type = rhs_type;
540 cp->attr = talloc_typed_strdup(cp, attr);
548 cp->value = talloc_typed_strdup(cp, value);
549 if (!cp->value) goto error;
555 /** Duplicate a CONF_PAIR
557 * @param parent to allocate new pair in.
558 * @param cp to duplicate.
559 * @return NULL on error, else a duplicate of the input pair.
561 CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp)
568 new = cf_pair_alloc(parent, cp->attr, cf_pair_value(cp),
569 cp->op, cp->lhs_type, cp->rhs_type);
570 if (!new) return NULL;
572 new->parsed = cp->parsed;
573 new->item.lineno = cp->item.lineno;
576 * Avoid mallocs if possible.
578 if (!cp->item.filename || (strcmp(parent->item.filename, cp->item.filename) == 0)) {
579 new->item.filename = parent->item.filename;
581 new->item.filename = talloc_strdup(new, cp->item.filename);
587 /** Add a configuration pair to a section
589 * @param parent section to add pair to.
592 void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp)
594 cf_item_add(parent, cf_pair_to_item(cp));
597 /** Allocate a CONF_SECTION
599 * @param parent CONF_SECTION to hang this CONF_SECTION off of.
600 * @param name1 Primary name.
601 * @param name2 Secondary name.
602 * @return NULL on error, else a new CONF_SECTION parented by parent.
604 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
609 if (!name1) return NULL;
611 if (name2 && parent) {
612 if (strchr(name2, '$')) {
613 name2 = cf_expand_variables(parent->item.filename,
614 &parent->item.lineno,
616 buffer, sizeof(buffer), name2, NULL);
618 ERROR("Failed expanding section name");
624 cs = talloc_zero(parent, CONF_SECTION);
625 if (!cs) return NULL;
627 cs->item.type = CONF_ITEM_SECTION;
628 cs->item.parent = parent;
630 cs->name1 = talloc_typed_strdup(cs, name1);
638 cs->name2 = talloc_typed_strdup(cs, name2);
639 if (!cs->name2) goto error;
642 cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
643 if (!cs->pair_tree) goto error;
645 talloc_set_destructor(cs, _cf_section_free);
648 * Don't create a data tree, it may not be needed.
652 * Don't create the section tree here, it may not
656 if (parent) cs->depth = parent->depth + 1;
661 /** Duplicate a configuration section
663 * @note recursively duplicates any child sections.
664 * @note does not duplicate any data associated with a section, or its child sections.
666 * @param parent section (may be NULL).
667 * @param cs to duplicate.
668 * @param name1 of new section.
669 * @param name2 of new section.
670 * @param copy_meta Copy additional meta data for a section (like template, base, depth and variables).
671 * @return a duplicate of the existing section, or NULL on error.
673 CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs,
674 char const *name1, char const *name2, bool copy_meta)
676 CONF_SECTION *new, *subcs;
680 new = cf_section_alloc(parent, name1, name2);
683 new->template = cs->template;
684 new->base = cs->base;
685 new->depth = cs->depth;
686 new->variables = cs->variables;
689 new->item.lineno = cs->item.lineno;
691 if (!cs->item.filename || (parent && (strcmp(parent->item.filename, cs->item.filename) == 0))) {
692 new->item.filename = parent->item.filename;
694 new->item.filename = talloc_strdup(new, cs->item.filename);
697 for (ci = cs->children; ci; ci = ci->next) {
699 case CONF_ITEM_SECTION:
700 subcs = cf_item_to_section(ci);
701 subcs = cf_section_dup(new, subcs,
702 cf_section_name1(subcs), cf_section_name2(subcs),
708 cf_section_add(new, subcs);
712 cp = cf_pair_dup(new, cf_item_to_pair(ci));
717 cf_pair_add(new, cp);
720 case CONF_ITEM_DATA: /* Skip data */
723 case CONF_ITEM_INVALID:
731 void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
733 cf_item_add(parent, &(cs->item));
736 /** Replace pair in a given section with a new pair, of the given value.
738 * @param cs to replace pair in.
739 * @param cp to replace.
740 * @param value New value to assign to cp.
741 * @return 0 on success, -1 on failure.
743 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
746 CONF_ITEM *ci, *cn, **last;
748 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->lhs_type, cp->rhs_type);
749 if (!newp) return -1;
755 * Find the old one from the linked list, and replace it
758 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
760 cn->next = (*last)->next;
767 rbtree_deletebydata(cs->pair_tree, ci);
769 rbtree_insert(cs->pair_tree, cn);
776 * Add an item to a configuration section.
778 void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
781 CONF_ITEM *first = ci;
784 rad_assert((void *)cs != (void *)ci);
786 if (!cs || !ci) return;
789 rad_assert(cs->tail == NULL);
792 rad_assert(cs->tail != NULL);
797 * Update the trees (and tail) for each item added.
799 for (/* nothing */; ci != NULL; ci = ci->next) {
800 rad_assert(ci->next != first); /* simple cycle detection */
805 * For fast lookups, pairs and sections get
810 if (!rbtree_insert(cs->pair_tree, ci)) {
811 CONF_PAIR *cp = cf_item_to_pair(ci);
813 if (strcmp(cp->attr, "confdir") == 0) break;
814 if (!cp->value) break; /* module name, "ok", etc. */
818 case CONF_ITEM_SECTION: {
819 CONF_SECTION *cs_new = cf_item_to_section(ci);
820 CONF_SECTION *name1_cs;
822 if (!cs->section_tree) {
823 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
824 if (!cs->section_tree) {
825 ERROR("Out of memory");
830 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
832 if (!rbtree_insert(cs->section_tree, cs_new)) {
833 ERROR("Failed inserting section into tree");
840 * We already have a section of
841 * this "name1". Add a new
842 * sub-section based on name2.
844 if (!name1_cs->name2_tree) {
845 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
846 if (!name1_cs->name2_tree) {
847 ERROR("Out of memory");
853 * We don't care if this fails.
854 * If the user tries to create
855 * two sections of the same
856 * name1/name2, the duplicate
857 * section is just silently
860 rbtree_insert(name1_cs->name2_tree, cs_new);
862 } /* was a section */
865 if (!cs->data_tree) {
866 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
869 rbtree_insert(cs->data_tree, ci);
873 default: /* FIXME: assert & error! */
876 } /* switch over conf types */
881 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
882 CONF_SECTION *outercs,
887 CONF_SECTION const *cs = outercs;
891 if (!cs) goto no_such_item;
893 strlcpy(name, ptr, sizeof(name));
897 * ".foo" means "foo from the current section"
903 * Just '.' means the current section
906 return cf_section_to_item(cs);
910 * ..foo means "foo from the section
911 * enclosing this section" (etc.)
914 if (cs->item.parent) {
915 cs = cs->item.parent;
919 * .. means the section
920 * enclosing this section
923 return cf_section_to_item(cs);
928 * "foo.bar.baz" means "from the root"
930 } else if (strchr(p, '.') != NULL) {
931 if (!parentcs) goto no_such_item;
943 if (r && q > r) q = NULL;
944 if (q && q < r) r = NULL;
950 q = strchr(r + 1, ']');
951 if (!q) return NULL; /* parse error */
954 * Points to foo[bar]xx: parse error,
955 * it should be foo[bar] or foo[bar].baz
957 if (q[1] && q[1] != '.') goto no_such_item;
961 next = cf_section_sub_find_name2(cs, p, r + 1);
966 * Points to a named instance of a section.
969 if (!next) goto no_such_item;
970 return &(next->item);
973 q++; /* ensure we skip the ']' and '.' */
977 next = cf_section_sub_find(cs, p);
981 if (!next) break; /* it MAY be a pair in this section! */
987 if (!*p) goto no_such_item;
991 * Find it in the current referenced
994 cp = cf_pair_find(cs, p);
996 cp->parsed = true; /* conf pairs which are referenced count as parsed */
1000 next = cf_section_sub_find(cs, p);
1001 if (next) return &(next->item);
1004 * "foo" is "in the current section, OR in main".
1006 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
1016 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
1018 if (!cs) return NULL;
1020 while (cs->item.parent != NULL) {
1021 cs = cs->item.parent;
1029 * Expand the variables in an input string.
1031 static char const *cf_expand_variables(char const *cf, int *lineno,
1032 CONF_SECTION *outercs,
1033 char *output, size_t outsize,
1034 char const *input, bool *soft_fail)
1037 char const *end, *ptr;
1038 CONF_SECTION const *parentcs;
1041 if (soft_fail) *soft_fail = false;
1044 * Find the master parent conf section.
1045 * We can't use main_config.config, because we're in the
1046 * process of re-building it, and it isn't set up yet...
1048 parentcs = cf_top_section(outercs);
1054 * Ignore anything other than "${"
1056 if ((*ptr == '$') && (ptr[1] == '{')) {
1062 * FIXME: Add support for ${foo:-bar},
1067 * Look for trailing '}', and log a
1068 * warning for anything that doesn't match,
1069 * and exit with a fatal error.
1071 end = strchr(ptr, '}');
1074 INFO("%s[%d]: Variable expansion missing }",
1082 * Can't really happen because input lines are
1083 * capped at 8k, which is sizeof(name)
1085 if ((size_t) (end - ptr) >= sizeof(name)) {
1086 ERROR("%s[%d]: Reference string is too large",
1091 memcpy(name, ptr, end - ptr);
1092 name[end - ptr] = '\0';
1094 q = strchr(name, ':');
1099 ci = cf_reference_item(parentcs, outercs, name);
1101 if (soft_fail) *soft_fail = true;
1102 ERROR("%s[%d]: Reference \"${%s}\" not found", cf, *lineno, name);
1107 * The expansion doesn't refer to another item or section
1108 * it's the property of a section.
1111 CONF_SECTION *mycs = cf_item_to_section(ci);
1113 if (ci->type != CONF_ITEM_SECTION) {
1114 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
1118 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
1119 case CONF_PROPERTY_NAME:
1120 strcpy(p, mycs->name1);
1123 case CONF_PROPERTY_INSTANCE:
1124 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
1128 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
1134 } else if (ci->type == CONF_ITEM_PAIR) {
1136 * Substitute the value of the variable.
1138 cp = cf_item_to_pair(ci);
1141 * If the thing we reference is
1142 * marked up as being expanded in
1143 * pass2, don't expand it now.
1144 * Let it be expanded in pass2.
1147 if (soft_fail) *soft_fail = true;
1149 ERROR("%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
1150 cf, *lineno, input);
1155 ERROR("%s[%d]: Reference \"%s\" has no value",
1156 cf, *lineno, input);
1160 if (p + strlen(cp->value) >= output + outsize) {
1161 ERROR("%s[%d]: Reference \"%s\" is too long",
1162 cf, *lineno, input);
1166 strcpy(p, cp->value);
1170 } else if (ci->type == CONF_ITEM_SECTION) {
1171 CONF_SECTION *subcs;
1174 * Adding an entry again to a
1175 * section is wrong. We don't
1176 * want an infinite loop.
1178 if (ci->parent == outercs) {
1179 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
1184 * Copy the section instead of
1187 subcs = cf_item_to_section(ci);
1188 subcs = cf_section_dup(outercs, subcs,
1189 cf_section_name1(subcs), cf_section_name2(subcs),
1192 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
1196 subcs->item.filename = ci->filename;
1197 subcs->item.lineno = ci->lineno;
1198 cf_item_add(outercs, &(subcs->item));
1203 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
1206 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
1212 * Look for trailing '}', and log a
1213 * warning for anything that doesn't match,
1214 * and exit with a fatal error.
1216 end = strchr(ptr, '}');
1219 INFO("%s[%d]: Environment variable expansion missing }",
1225 * Can't really happen because input lines are
1226 * capped at 8k, which is sizeof(name)
1228 if ((size_t) (end - ptr) >= sizeof(name)) {
1229 ERROR("%s[%d]: Environment variable name is too large",
1234 memcpy(name, ptr, end - ptr);
1235 name[end - ptr] = '\0';
1238 * Get the environment variable.
1239 * If none exists, then make it an empty string.
1247 if (p + strlen(env) >= output + outsize) {
1248 ERROR("%s[%d]: Reference \"%s\" is too long",
1249 cf, *lineno, input);
1259 * Copy it over verbatim.
1265 if (p >= (output + outsize)) {
1266 ERROR("%s[%d]: Reference \"%s\" is too long",
1267 cf, *lineno, input);
1270 } /* loop over all of the input string. */
1277 static char const parse_spaces[] = " ";
1279 /** Validation function for ipaddr conffile types
1282 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1283 fr_ipaddr_t *ipaddr)
1287 if (strcmp(value, "*") == 0) {
1288 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1289 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1290 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1292 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1293 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1297 case PW_TYPE_IPV4_ADDR:
1298 case PW_TYPE_IPV6_ADDR:
1299 case PW_TYPE_COMBO_IP_ADDR:
1300 switch (ipaddr->af) {
1302 if (ipaddr->prefix != 32) {
1303 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
1311 if (ipaddr->prefix != 128) {
1312 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
1327 /** Parses a #CONF_PAIR into a C data type, with a default value.
1329 * Takes fields from a #CONF_PARSER struct and uses them to parse the string value
1330 * of a #CONF_PAIR into a C data type matching the type argument.
1332 * The format of the types are the same as #value_data_t types.
1334 * @note The dflt value will only be used if no matching #CONF_PAIR is found. Empty strings will not
1335 * result in the dflt value being used.
1337 * **PW_TYPE to data type mappings**
1338 * | PW_TYPE | Data type | Dynamically allocated |
1339 * | ----------------------- | ------------------ | ---------------------- |
1340 * | PW_TYPE_TMPL | ``vp_tmpl_t`` | Yes |
1341 * | PW_TYPE_BOOLEAN | ``bool`` | No |
1342 * | PW_TYPE_INTEGER | ``uint32_t`` | No |
1343 * | PW_TYPE_SHORT | ``uint16_t`` | No |
1344 * | PW_TYPE_INTEGER64 | ``uint64_t`` | No |
1345 * | PW_TYPE_SIGNED | ``int32_t`` | No |
1346 * | PW_TYPE_STRING | ``char const *`` | Yes |
1347 * | PW_TYPE_IPV4_ADDR | ``fr_ipaddr_t`` | No |
1348 * | PW_TYPE_IPV4_PREFIX | ``fr_ipaddr_t`` | No |
1349 * | PW_TYPE_IPV6_ADDR | ``fr_ipaddr_t`` | No |
1350 * | PW_TYPE_IPV6_PREFIX | ``fr_ipaddr_t`` | No |
1351 * | PW_TYPE_COMBO_IP_ADDR | ``fr_ipaddr_t`` | No |
1352 * | PW_TYPE_COMBO_IP_PREFIX | ``fr_ipaddr_t`` | No |
1353 * | PW_TYPE_TIMEVAL | ``struct timeval`` | No |
1355 * @param cs to search for matching #CONF_PAIR in.
1356 * @param name of #CONF_PAIR to search for.
1357 * @param type Data type to parse #CONF_PAIR value as.
1358 * Should be one of the following ``data`` types, and one or more of the following ``flag`` types or'd together:
1359 * - ``data`` #PW_TYPE_TMPL - @copybrief PW_TYPE_TMPL
1360 * Feeds the value into #tmpl_afrom_str. Value can be
1361 * obtained when processing requests, with #tmpl_expand or #tmpl_aexpand.
1362 * - ``data`` #PW_TYPE_BOOLEAN - @copybrief PW_TYPE_BOOLEAN
1363 * - ``data`` #PW_TYPE_INTEGER - @copybrief PW_TYPE_INTEGER
1364 * - ``data`` #PW_TYPE_SHORT - @copybrief PW_TYPE_SHORT
1365 * - ``data`` #PW_TYPE_INTEGER64 - @copybrief PW_TYPE_INTEGER64
1366 * - ``data`` #PW_TYPE_SIGNED - @copybrief PW_TYPE_SIGNED
1367 * - ``data`` #PW_TYPE_STRING - @copybrief PW_TYPE_STRING
1368 * - ``data`` #PW_TYPE_IPV4_ADDR - @copybrief PW_TYPE_IPV4_ADDR (IPv4 address with prefix 32).
1369 * - ``data`` #PW_TYPE_IPV4_PREFIX - @copybrief PW_TYPE_IPV4_PREFIX (IPv4 address with variable prefix).
1370 * - ``data`` #PW_TYPE_IPV6_ADDR - @copybrief PW_TYPE_IPV6_ADDR (IPv6 address with prefix 128).
1371 * - ``data`` #PW_TYPE_IPV6_PREFIX - @copybrief PW_TYPE_IPV6_PREFIX (IPv6 address with variable prefix).
1372 * - ``data`` #PW_TYPE_COMBO_IP_ADDR - @copybrief PW_TYPE_COMBO_IP_ADDR (IPv4/IPv6 address with
1374 * - ``data`` #PW_TYPE_COMBO_IP_PREFIX - @copybrief PW_TYPE_COMBO_IP_PREFIX (IPv4/IPv6 address with
1376 * - ``data`` #PW_TYPE_TIMEVAL - @copybrief PW_TYPE_TIMEVAL
1377 * - ``flag`` #PW_TYPE_DEPRECATED - @copybrief PW_TYPE_DEPRECATED
1378 * - ``flag`` #PW_TYPE_REQUIRED - @copybrief PW_TYPE_REQUIRED
1379 * - ``flag`` #PW_TYPE_ATTRIBUTE - @copybrief PW_TYPE_ATTRIBUTE
1380 * - ``flag`` #PW_TYPE_SECRET - @copybrief PW_TYPE_SECRET
1381 * - ``flag`` #PW_TYPE_FILE_INPUT - @copybrief PW_TYPE_FILE_INPUT
1382 * - ``flag`` #PW_TYPE_NOT_EMPTY - @copybrief PW_TYPE_NOT_EMPTY
1383 * @param data Pointer to a global variable, or pointer to a field in the struct being populated with values.
1384 * @param dflt value to use, if no #CONF_PAIR is found.
1386 * - 1 if default value was used.
1389 * - -2 if deprecated.
1391 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1394 bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, multi;
1397 CONF_PAIR *cp = NULL;
1398 fr_ipaddr_t *ipaddr;
1400 CONF_ITEM *c_item = &cs->item;
1404 deprecated = (type & PW_TYPE_DEPRECATED);
1405 required = (type & PW_TYPE_REQUIRED);
1406 attribute = (type & PW_TYPE_ATTRIBUTE);
1407 secret = (type & PW_TYPE_SECRET);
1408 file_input = (type == PW_TYPE_FILE_INPUT); /* check, not and */
1409 cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1410 tmpl = (type & PW_TYPE_TMPL);
1411 multi = (type & PW_TYPE_MULTI);
1413 if (attribute) required = true;
1414 if (required) cant_be_empty = true; /* May want to review this in the future... */
1417 * Everything except templates must have a base type.
1419 if (!(type & 0xff) && !tmpl) {
1420 cf_log_err(c_item, "Configuration item \"%s\" must have a data type", name);
1424 type &= 0xff; /* normal types are small */
1428 cp = cf_pair_find(cs, name);
1431 * No pairs match the configuration item name in the current
1432 * section, use the default value.
1435 if (deprecated) return 0; /* Don't set the default value */
1440 * Something matched, used the CONF_PAIR value.
1443 CONF_PAIR *next = cp;
1449 * @fixme We should actually validate
1450 * the value of the pairs too
1452 if (multi) while ((next = cf_pair_find_next(cs, next, name))) {
1453 next->parsed = true;
1457 cf_log_err(c_item, "Configuration item \"%s\" is deprecated", name);
1466 cf_log_err(c_item, "Configuration item \"%s\" must have a value", name);
1473 if ((value[0] == '\0') && cant_be_empty) {
1475 cf_log_err(c_item, "Configuration item \"%s\" must not be empty (zero length)", name);
1476 if (!required) cf_log_err(c_item, "Comment item to silence this message");
1483 * Process a value as a LITERAL template. Once all of
1484 * the attrs and xlats are defined, the pass2 code
1485 * converts it to the appropriate type.
1491 *(vp_tmpl_t **)data = NULL;
1495 rad_assert(!attribute);
1496 vpt = tmpl_alloc(cs, TMPL_TYPE_LITERAL, value, strlen(value));
1497 *(vp_tmpl_t **)data = vpt;
1503 case PW_TYPE_BOOLEAN:
1505 * Allow yes/no, true/false, and on/off
1507 if ((strcasecmp(value, "yes") == 0) ||
1508 (strcasecmp(value, "true") == 0) ||
1509 (strcasecmp(value, "on") == 0)) {
1510 *(bool *)data = true;
1511 } else if ((strcasecmp(value, "no") == 0) ||
1512 (strcasecmp(value, "false") == 0) ||
1513 (strcasecmp(value, "off") == 0)) {
1514 *(bool *)data = false;
1516 *(bool *)data = false;
1517 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1518 "variable %s", value, name);
1521 cf_log_info(cs, "%.*s\t%s = %s",
1522 cs->depth, parse_spaces, name, value);
1525 case PW_TYPE_INTEGER:
1527 unsigned long v = strtoul(value, 0, 0);
1530 * Restrict integer values to 0-INT32_MAX, this means
1531 * it will always be safe to cast them to a signed type
1532 * for comparisons, and imposes the same range limit as
1533 * before we switched to using an unsigned type to
1534 * represent config item integers.
1536 if (v > INT32_MAX) {
1537 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1542 *(uint32_t *)data = v;
1543 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1549 unsigned long v = strtoul(value, 0, 0);
1551 if (v > UINT8_MAX) {
1552 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1556 *(uint8_t *)data = (uint8_t) v;
1557 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint8_t *)data);
1563 unsigned long v = strtoul(value, 0, 0);
1565 if (v > UINT16_MAX) {
1566 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1570 *(uint16_t *)data = (uint16_t) v;
1571 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1575 case PW_TYPE_INTEGER64:
1576 *(uint64_t *)data = strtoull(value, 0, 0);
1577 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1580 case PW_TYPE_SIGNED:
1581 *(int32_t *)data = strtol(value, 0, 0);
1582 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1585 case PW_TYPE_STRING:
1592 * Expand variables which haven't already been
1593 * expanded automagically when the configuration
1596 if (value == dflt) {
1599 lineno = cs->item.lineno;
1601 value = cf_expand_variables("<internal>",
1603 cs, buffer, sizeof(buffer),
1606 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1611 if (required && !value) goto is_required;
1612 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1615 if (!dict_attrbyname(value)) {
1617 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1620 cf_log_err(&(cp->item), "No such attribute '%s'", value);
1627 * Hide secrets when using "radiusd -X".
1629 if (secret && (rad_debug_lvl <= 2)) {
1630 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1631 cs->depth, parse_spaces, name);
1633 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1634 cs->depth, parse_spaces, name, value ? value : "(null)");
1636 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1639 * If there's data AND it's an input file, check
1640 * that we can read it. This check allows errors
1641 * to be caught as early as possible, during
1644 if (*q && file_input && !cf_file_input(cs, *q)) {
1649 case PW_TYPE_IPV4_ADDR:
1650 case PW_TYPE_IPV4_PREFIX:
1653 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1654 ERROR("%s", fr_strerror());
1657 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1660 case PW_TYPE_IPV6_ADDR:
1661 case PW_TYPE_IPV6_PREFIX:
1664 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1665 ERROR("%s", fr_strerror());
1668 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1671 case PW_TYPE_COMBO_IP_ADDR:
1672 case PW_TYPE_COMBO_IP_PREFIX:
1675 if (fr_pton(ipaddr, value, -1, AF_UNSPEC, true) < 0) {
1676 ERROR("%s", fr_strerror());
1679 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1682 case PW_TYPE_TIMEVAL: {
1687 sec = strtoul(value, &end, 10);
1693 len = strlen(end + 1);
1696 ERROR("Too much precision for timeval");
1701 * If they write "0.1", that means
1702 * "10000" microseconds.
1704 sec = strtoul(end + 1, NULL, 10);
1712 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1713 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1714 memcpy(data, &tv, sizeof(tv));
1720 * If we get here, it's a sanity check error.
1721 * It's not an error parsing the configuration
1724 rad_assert(type > PW_TYPE_INVALID);
1725 rad_assert(type < PW_TYPE_MAX);
1727 ERROR("type '%s' is not supported in the configuration files",
1728 fr_int2str(dict_attr_types, type, "?Unknown?"));
1730 } /* switch over variable type */
1735 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1736 if (!cpn) return -1;
1738 cpn->item.filename = "<internal>";
1739 cpn->item.lineno = 0;
1740 cf_item_add(cs, &(cpn->item));
1748 * A copy of cf_section_parse that initializes pointers before
1751 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1752 CONF_PARSER const *variables)
1756 for (i = 0; variables[i].name != NULL; i++) {
1757 if (variables[i].type == PW_TYPE_SUBSECTION) {
1758 CONF_SECTION *subcs;
1760 if (!variables[i].dflt) continue;
1762 subcs = cf_section_sub_find(cs, variables[i].name);
1765 * If there's no subsection in the
1766 * config, BUT the CONF_PARSER wants one,
1767 * then create an empty one. This is so
1768 * that we can track the strings,
1769 * etc. allocated in the subsection.
1772 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1775 subcs->item.filename = cs->item.filename;
1776 subcs->item.lineno = cs->item.lineno;
1777 cf_item_add(cs, &(subcs->item));
1780 cf_section_parse_init(subcs, (uint8_t *)base + variables[i].offset,
1781 (CONF_PARSER const *) variables[i].dflt);
1785 if ((variables[i].type != PW_TYPE_STRING) &&
1786 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1787 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1791 if (variables[i].data) {
1792 *(char **) variables[i].data = NULL;
1794 *(char **) (((char *)base) + variables[i].offset) = NULL;
1798 } /* for all variables in the configuration section */
1802 static void cf_section_parse_warn(CONF_SECTION *cs)
1806 for (ci = cs->children; ci; ci = ci->next) {
1808 * Don't recurse on sections. We can only safely
1809 * check conf pairs at the same level as the
1810 * section that was just parsed.
1812 if (ci->type == CONF_ITEM_SECTION) continue;
1813 if (ci->type == CONF_ITEM_PAIR) {
1816 cp = cf_item_to_pair(ci);
1817 if (cp->parsed) continue;
1819 WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
1820 cp->item.filename ? cp->item.filename : "unknown",
1821 cp->item.lineno ? cp->item.lineno : 0,
1826 * Skip everything else.
1831 /** Parse a configuration section into user-supplied variables
1833 * @param cs to parse.
1834 * @param base pointer to a struct to fill with data. Any buffers will also be talloced
1835 * using this parent as a pointer.
1836 * @param variables mappings between struct fields and #CONF_ITEM s.
1839 * - -1 on general error.
1840 * - -2 if a deprecated #CONF_ITEM was found.
1842 int cf_section_parse(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1848 cs->variables = variables; /* this doesn't hurt anything */
1851 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces, cs->name1);
1853 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces, cs->name1, cs->name2);
1856 cf_section_parse_init(cs, base, variables);
1859 * Handle the known configuration parameters.
1861 for (i = 0; variables[i].name != NULL; i++) {
1863 * Handle subsections specially
1865 if (variables[i].type == PW_TYPE_SUBSECTION) {
1866 CONF_SECTION *subcs;
1868 subcs = cf_section_sub_find(cs, variables[i].name);
1870 * Default in this case is overloaded to mean a pointer
1871 * to the CONF_PARSER struct for the subsection.
1873 if (!variables[i].dflt || !subcs) {
1874 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1879 ret = cf_section_parse(subcs, (uint8_t *)base + variables[i].offset,
1880 (CONF_PARSER const *) variables[i].dflt);
1881 if (ret < 0) goto finish;
1883 } /* else it's a CONF_PAIR */
1885 if (variables[i].data) {
1886 data = variables[i].data; /* prefer this. */
1888 data = ((char *)base) + variables[i].offset;
1890 ERROR("Internal sanity check 2 failed in cf_section_parse");
1896 * Parse the pair we found, or a default value.
1898 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1900 case 1: /* Used default */
1907 case -1: /* Parse error */
1910 case -2: /* Deprecated CONF ITEM */
1911 if ((variables[i + 1].offset == variables[i].offset) &&
1912 (variables[i + 1].data == variables[i].data)) {
1913 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1914 variables[i + 1].name);
1918 } /* for all variables in the configuration section */
1921 * Ensure we have a proper terminator, type so we catch
1922 * missing terminators reliably
1924 rad_assert(variables[i].type == -1);
1927 * Warn about items in the configuration which weren't
1928 * checked during parsing.
1930 if (rad_debug_lvl >= 3) cf_section_parse_warn(cs);
1934 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1942 * Check XLAT things in pass 2. But don't cache the xlat stuff anywhere.
1944 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1953 * Handle the known configuration parameters.
1955 for (i = 0; variables[i].name != NULL; i++) {
1960 * Handle subsections specially
1962 if (variables[i].type == PW_TYPE_SUBSECTION) {
1963 CONF_SECTION *subcs;
1964 subcs = cf_section_sub_find(cs, variables[i].name);
1966 if (cf_section_parse_pass2(subcs, (uint8_t *)base + variables[i].offset,
1967 (CONF_PARSER const *) variables[i].dflt) < 0) {
1971 } /* else it's a CONF_PAIR */
1974 * Figure out which data we need to fix.
1976 if (variables[i].data) {
1977 data = variables[i].data; /* prefer this. */
1979 data = ((char *)base) + variables[i].offset;
1984 cp = cf_pair_find(cs, variables[i].name);
1988 if (!cp || !cp->value || !data) continue;
1990 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1991 (cp->rhs_type != T_BARE_WORD)) continue;
1994 * Non-xlat expansions shouldn't have xlat!
1996 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1997 ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1999 * Ignore %{... in shared secrets.
2000 * They're never dynamically expanded.
2002 if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
2004 if (strstr(cp->value, "%{") != NULL) {
2005 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
2006 cp->item.filename ? cp->item.filename : "unknown",
2007 cp->item.lineno ? cp->item.lineno : 0);
2013 * Parse (and throw away) the xlat string.
2015 * FIXME: All of these should be converted from PW_TYPE_XLAT
2018 if ((variables[i].type & PW_TYPE_XLAT) != 0) {
2020 * xlat expansions should be parseable.
2022 value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
2025 slen = xlat_tokenize(cs, value, &xlat, &error);
2027 char *spaces, *text;
2030 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
2032 cf_log_err(&cp->item, "Failed parsing expanded string:");
2033 cf_log_err(&cp->item, "%s", text);
2034 cf_log_err(&cp->item, "%s^ %s", spaces, error);
2036 talloc_free(spaces);
2048 * Convert the LITERAL template to the actual
2051 if ((variables[i].type & PW_TYPE_TMPL) != 0) {
2054 slen = tmpl_afrom_str(cs, &vpt, cp->value, talloc_array_length(cp->value) - 1,
2056 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
2058 error = fr_strerror();
2065 * Don't add default - update with new types.
2067 switch (vpt->type) {
2069 * All attributes should have been defined by this point.
2071 case TMPL_TYPE_ATTR_UNDEFINED:
2072 cf_log_err(&cp->item, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
2075 case TMPL_TYPE_LITERAL:
2076 case TMPL_TYPE_ATTR:
2077 case TMPL_TYPE_LIST:
2078 case TMPL_TYPE_DATA:
2079 case TMPL_TYPE_EXEC:
2080 case TMPL_TYPE_XLAT:
2081 case TMPL_TYPE_XLAT_STRUCT:
2084 case TMPL_TYPE_UNKNOWN:
2085 case TMPL_TYPE_REGEX:
2086 case TMPL_TYPE_REGEX_STRUCT:
2087 case TMPL_TYPE_NULL:
2091 talloc_free(*(vp_tmpl_t **)data);
2092 *(vp_tmpl_t **)data = vpt;
2096 * If the "multi" flag is set, check all of them.
2098 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
2099 cp = cf_pair_find_next(cs, cp, cp->attr);
2102 } /* for all variables in the configuration section */
2108 * Merge the template so everyting else "just works".
2110 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
2114 if (!cs || !template) return true;
2116 cs->template = NULL;
2119 * Walk over the template, adding its' entries to the
2120 * current section. But only if the entries don't
2121 * already exist in the current section.
2123 for (ci = template->children; ci; ci = ci->next) {
2124 if (ci->type == CONF_ITEM_PAIR) {
2125 CONF_PAIR *cp1, *cp2;
2128 * It exists, don't over-write it.
2130 cp1 = cf_item_to_pair(ci);
2131 if (cf_pair_find(cs, cp1->attr)) {
2136 * Create a new pair with all of the data
2139 cp2 = cf_pair_dup(cs, cp1);
2140 if (!cp2) return false;
2142 cp2->item.filename = cp1->item.filename;
2143 cp2->item.lineno = cp1->item.lineno;
2145 cf_item_add(cs, &(cp2->item));
2149 if (ci->type == CONF_ITEM_SECTION) {
2150 CONF_SECTION *subcs1, *subcs2;
2152 subcs1 = cf_item_to_section(ci);
2153 rad_assert(subcs1 != NULL);
2155 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
2158 * sub-sections get merged.
2160 if (!cf_template_merge(subcs2, subcs1)) {
2167 * Our section doesn't have a matching
2168 * sub-section. Copy it verbatim from
2171 subcs2 = cf_section_dup(cs, subcs1,
2172 cf_section_name1(subcs1), cf_section_name2(subcs1),
2174 if (!subcs2) return false;
2176 subcs2->item.filename = subcs1->item.filename;
2177 subcs2->item.lineno = subcs1->item.lineno;
2179 cf_item_add(cs, &(subcs2->item));
2183 /* ignore everything else */
2189 static char const *cf_local_file(char const *base, char const *filename,
2190 char *buffer, size_t bufsize)
2195 strlcpy(buffer, base, bufsize);
2197 p = strrchr(buffer, FR_DIR_SEP);
2198 if (!p) return filename;
2199 if (p[1]) { /* ./foo */
2203 dirsize = (p - buffer) + 1;
2205 if ((dirsize + strlen(filename)) >= bufsize) {
2209 strlcpy(p + 1, filename, bufsize - dirsize);
2216 * Read a part of the config file.
2218 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
2219 CONF_SECTION *current)
2222 CONF_SECTION *this, *css;
2231 FR_TOKEN t1 = T_INVALID, t2, t3;
2232 bool has_spaces = false;
2237 this = current; /* add items here */
2240 * Read, checking for line continuations ('\\' at EOL)
2247 * Get data, and remember if we are at EOF.
2249 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
2253 * We read the entire 8k worth of data: complain.
2254 * Note that we don't care if the last character
2255 * is \n: it's still forbidden. This means that
2256 * the maximum allowed length of text is 8k-1, which
2260 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
2261 ERROR("%s[%d]: Line too long",
2268 while (isspace((int) *ptr)) ptr++;
2271 memmove(cbuf, ptr, len - (ptr - cbuf));
2272 len -= (ptr - cbuf);
2277 * Not doing continuations: check for edge
2284 while (*ptr && isspace((int) *ptr)) ptr++;
2286 if (!*ptr || (*ptr == '#')) continue;
2288 } else if (at_eof || (len == 0)) {
2289 ERROR("%s[%d]: Continuation at EOF is illegal",
2295 * See if there's a continuation.
2298 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
2303 if ((len > 0) && (cbuf[len - 1] == '\\')) {
2305 * Check for "suppress spaces" magic.
2307 if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
2311 cbuf[len - 1] = '\0';
2323 * The parser is getting to be evil.
2325 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
2327 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
2331 if (ptr[0] == '%') {
2332 hack = rad_copy_variable(buf1, ptr);
2334 hack = rad_copy_string(buf1, ptr);
2337 ERROR("%s[%d]: Invalid expansion: %s",
2338 filename, *lineno, ptr);
2344 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2351 ERROR("%s[%d]: Invalid expansion: %s",
2352 filename, *lineno, ptr);
2356 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
2360 * The caller eats "name1 name2 {", and calls us
2361 * for the data inside of the section. So if we
2362 * receive a closing brace, then it must mean the
2363 * end of the section.
2365 if (t1 == T_RCBRACE) {
2366 if (this == current) {
2367 ERROR("%s[%d]: Too many closing braces",
2373 * Merge the template into the existing
2374 * section. This uses more memory, but
2375 * means that templates now work with
2376 * sub-sections, etc.
2378 if (!cf_template_merge(this, this->template)) {
2382 this = this->item.parent;
2383 goto check_for_more;
2386 if (t1 != T_BARE_WORD) goto skip_keywords;
2389 * Allow for $INCLUDE files
2391 * This *SHOULD* work for any level include.
2392 * I really really really hate this file. -cparker
2394 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2395 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2396 bool relative = true;
2398 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2400 ERROR("%s[%d]: Unexpected text after $INCLUDE",
2405 if (buf2[0] == '$') relative = false;
2407 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2, NULL);
2408 if (!value) return -1;
2410 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2413 value = cf_local_file(filename, value, buf3,
2416 ERROR("%s[%d]: Directories too deep.",
2423 #ifdef HAVE_DIRENT_H
2427 * Include ALL non-"dot" files in the directory.
2430 if (value[strlen(value) - 1] == '/') {
2433 struct stat stat_buf;
2435 DEBUG2("including files in directory %s", value );
2440 if (stat(value, &stat_buf) < 0) {
2441 ERROR("%s[%d]: Failed reading directory %s: %s",
2443 value, fr_syserror(errno));
2447 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2448 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
2449 "insecure configuration", filename, *lineno, value);
2453 dir = opendir(value);
2455 ERROR("%s[%d]: Error reading directory %s: %s",
2456 filename, *lineno, value,
2457 fr_syserror(errno));
2462 * Read the directory, ignoring "." files.
2464 while ((dp = readdir(dir)) != NULL) {
2467 if (dp->d_name[0] == '.') continue;
2470 * Check for valid characters
2472 for (p = dp->d_name; *p != '\0'; p++) {
2473 if (isalpha((int)*p) ||
2477 (*p == '.')) continue;
2480 if (*p != '\0') continue;
2482 snprintf(buf2, sizeof(buf2), "%s%s",
2484 if ((stat(buf2, &stat_buf) != 0) ||
2485 S_ISDIR(stat_buf.st_mode)) continue;
2487 * Read the file into the current
2488 * configuration section.
2490 if (cf_file_include(this, buf2) < 0) {
2498 { /* it was a normal file */
2499 if (buf1[1] == '-') {
2500 struct stat statbuf;
2502 if (stat(value, &statbuf) < 0) {
2503 WARN("Not including file %s: %s", value, fr_syserror(errno));
2508 if (cf_file_include(this, value) < 0) {
2513 } /* we were in an include */
2515 if (strcasecmp(buf1, "$template") == 0) {
2517 CONF_SECTION *parentcs, *templatecs;
2518 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2521 ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2525 parentcs = cf_top_section(current);
2527 templatecs = cf_section_sub_find(parentcs, "templates");
2529 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2533 ci = cf_reference_item(parentcs, templatecs, buf2);
2534 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2535 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2540 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2544 if (this->template) {
2545 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2549 this->template = cf_item_to_section(ci);
2554 * Ensure that the user can't add CONF_PAIRs
2555 * with 'internal' names;
2557 if (buf1[0] == '_') {
2558 ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2563 * Handle if/elsif specially.
2565 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2567 char const *error = NULL;
2569 CONF_SECTION *server;
2570 fr_cond_t *cond = NULL;
2573 * if / elsif MUST be inside of a
2574 * processing section, which MUST in turn
2575 * be inside of a "server" directive.
2577 if (!this->item.parent) {
2579 ERROR("%s[%d]: Invalid location for '%s'",
2580 filename, *lineno, buf1);
2585 * Can only have "if" in 3 named sections.
2587 server = this->item.parent;
2589 (strcmp(server->name1, "server") != 0) &&
2590 (strcmp(server->name1, "policy") != 0) &&
2591 (strcmp(server->name1, "instantiate") != 0)) {
2592 server = server->item.parent;
2593 if (!server) goto invalid_location;
2597 * Skip (...) to find the {
2599 slen = fr_condition_tokenize(this, cf_section_to_item(this), ptr, &cond,
2600 &error, FR_COND_TWO_PASS);
2601 memcpy(&p, &ptr, sizeof(p));
2604 if (p[-slen] != '{') goto cond_error;
2610 * This hack is so that the NEXT stage
2611 * doesn't go "too far" in expanding the
2612 * variable. We can parse the conditions
2613 * without expanding the ${...} stuff.
2614 * BUT we don't want to expand all of the
2615 * stuff AFTER the condition. So we do
2618 * The first pass is to discover the end
2619 * of the condition. We then expand THAT
2620 * string, and do a second pass parsing
2621 * the expanded condition.
2627 * If there's a ${...}. If so, expand it.
2629 if (strchr(ptr, '$') != NULL) {
2630 ptr = cf_expand_variables(filename, lineno,
2635 ERROR("%s[%d]: Parse error expanding ${...} in condition",
2639 } /* else leave it alone */
2641 css = cf_section_alloc(this, buf1, ptr);
2643 ERROR("%s[%d]: Failed allocating memory for section",
2647 css->item.filename = filename;
2648 css->item.lineno = *lineno;
2650 slen = fr_condition_tokenize(css, cf_section_to_item(css), ptr, &cond,
2651 &error, FR_COND_TWO_PASS);
2652 *p = '{'; /* put it back */
2656 char *spaces, *text;
2658 fr_canonicalize_error(this, &spaces, &text, slen, ptr);
2660 ERROR("%s[%d]: Parse error in condition",
2662 ERROR("%s[%d]: %s", filename, *lineno, text);
2663 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2665 talloc_free(spaces);
2671 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2673 ERROR("%s[%d]: Condition is too large after \"%s\"",
2674 filename, *lineno, buf1);
2679 * Copy the expanded and parsed condition
2680 * into buf2. Then, parse the text after
2681 * the condition, which now MUST be a '{.
2683 * If it wasn't '{' it would have been
2684 * caught in the first pass of
2685 * conditional parsing, above.
2687 memcpy(buf2, ptr, slen);
2691 if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2693 ERROR("%s[%d]: Expected '{' %d",
2694 filename, *lineno, t3);
2699 * Swap the condition with trailing stuff for
2700 * the final condition.
2702 memcpy(&p, &css->name2, sizeof(css->name2));
2704 css->name2 = talloc_typed_strdup(css, buf2);
2706 cf_item_add(this, &(css->item));
2707 cf_data_add_internal(css, "if", cond, NULL, false);
2710 * The current section is now the child section.
2714 goto check_for_more;
2719 * Grab the next token.
2721 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2738 case T_OP_CMP_FALSE:
2739 if (!this || (strcmp(this->name1, "update") != 0)) {
2740 ERROR("%s[%d]: Invalid operator in assignment",
2748 while (isspace((int) *ptr)) ptr++;
2751 * New parser: non-quoted strings are
2752 * bare words, and we parse everything
2753 * until the next newline, or the next
2754 * comma. If they have { or } in a bare
2755 * word, well... too bad.
2757 if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2758 && (*ptr != '`') && (*ptr != '/')) {
2759 const char *q = ptr;
2762 while (*q && (*q >= ' ') && (*q != ',') &&
2765 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2766 ERROR("%s[%d]: Parse error: value too long",
2771 memcpy(buf3, ptr, (q - ptr));
2772 buf3[q - ptr] = '\0';
2776 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2779 if (t3 == T_INVALID) {
2780 ERROR("%s[%d]: Parse error: %s",
2787 * Allow "foo" by itself, or "foo = bar"
2793 case T_DOUBLE_QUOTED_STRING:
2794 case T_BACK_QUOTED_STRING:
2795 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3, &soft_fail);
2797 if (!soft_fail) return -1;
2800 * References an item which doesn't exist,
2801 * or which is already marked up as being
2802 * expanded in pass2. Wait for pass2 to
2803 * do the expansions.
2821 * Add this CONF_PAIR to our CONF_SECTION
2824 cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2825 if (!cpn) return -1;
2826 cpn->item.filename = filename;
2827 cpn->item.lineno = *lineno;
2829 cf_item_add(this, &(cpn->item));
2832 * Hacks for escaping
2834 if (!cf_new_escape && !this->item.parent && value &&
2835 (strcmp(buf1, "correct_escapes") == 0) &&
2836 ((strcmp(value, "true") == 0) ||
2837 (strcmp(value, "yes") == 0) ||
2838 (strcmp(value, "1") == 0))) {
2839 cf_new_escape = true;
2843 * Require a comma, unless there's a comment.
2845 while (isspace(*ptr)) ptr++;
2854 * foo = bar # other stuff
2856 if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2858 if (!*ptr || (*ptr == '}')) break;
2860 ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2861 filename, *lineno, value, ptr);
2865 * No '=', must be a section or sub-section.
2868 case T_DOUBLE_QUOTED_STRING:
2869 case T_SINGLE_QUOTED_STRING:
2870 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2871 if (t3 != T_LCBRACE) {
2872 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2873 filename, *lineno, buf1, buf2);
2879 css = cf_section_alloc(this, buf1,
2880 t2 == T_LCBRACE ? NULL : buf2);
2882 ERROR("%s[%d]: Failed allocating memory for section",
2887 css->item.filename = filename;
2888 css->item.lineno = *lineno;
2889 cf_item_add(this, &(css->item));
2892 * There may not be a name2
2894 css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2897 * The current section is now the child section.
2903 ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2908 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2909 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2916 * Done parsing one thing. Skip to EOL if possible.
2918 while (isspace(*ptr)) ptr++;
2920 if (*ptr == '#') continue;
2929 * See if EOF was unexpected ..
2931 if (feof(fp) && (this != current)) {
2932 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2933 filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2941 * Include one config file in another.
2943 static int cf_file_include(CONF_SECTION *cs, char const *filename_in)
2947 char const *filename;
2950 * So we only need to do this once.
2952 filename = talloc_strdup(cs, filename_in);
2954 DEBUG2("including configuration file %s", filename);
2956 fp = cf_file_open(cs, filename);
2959 if (!cs->item.filename) cs->item.filename = filename;
2962 * Read the section. It's OK to have EOF without a
2963 * matching close brace.
2965 if (cf_section_read(filename, &lineno, fp, cs) < 0) {
2976 * Do variable expansion in pass2.
2978 * This is a breadth-first expansion. "deep
2980 static int cf_section_pass2(CONF_SECTION *cs)
2984 for (ci = cs->children; ci; ci = ci->next) {
2989 if (ci->type != CONF_ITEM_PAIR) continue;
2991 cp = cf_item_to_pair(ci);
2992 if (!cp->value || !cp->pass2) continue;
2994 rad_assert((cp->rhs_type == T_BARE_WORD) ||
2995 (cp->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2996 (cp->rhs_type == T_BACK_QUOTED_STRING));
2998 value = cf_expand_variables(ci->filename, &ci->lineno, cs, buffer, sizeof(buffer), cp->value, NULL);
2999 if (!value) return -1;
3001 rad_const_free(cp->value);
3002 cp->value = talloc_typed_strdup(cp, value);
3005 for (ci = cs->children; ci; ci = ci->next) {
3006 if (ci->type != CONF_ITEM_SECTION) continue;
3008 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
3016 * Bootstrap a config file.
3018 int cf_file_read(CONF_SECTION *cs, char const *filename)
3024 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3027 p = strrchr(cp->value, FR_DIR_SEP);
3030 cp->item.filename = "<internal>";
3031 cp->item.lineno = -1;
3032 cf_item_add(cs, &(cp->item));
3034 tree = rbtree_create(cs, filename_cmp, NULL, 0);
3035 if (!tree) return -1;
3037 cf_data_add_internal(cs, "filename", tree, NULL, 0);
3039 if (cf_file_include(cs, filename) < 0) return -1;
3042 * Now that we've read the file, go back through it and
3043 * expand the variables.
3045 if (cf_section_pass2(cs) < 0) return -1;
3051 void cf_file_free(CONF_SECTION *cs)
3058 * Return a CONF_PAIR within a CONF_SECTION.
3060 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
3062 CONF_PAIR *cp, mycp;
3064 if (!cs || !name) return NULL;
3067 cp = rbtree_finddata(cs->pair_tree, &mycp);
3070 if (!cs->template) return NULL;
3072 return rbtree_finddata(cs->template->pair_tree, &mycp);
3076 * Return the attr of a CONF_PAIR
3079 char const *cf_pair_attr(CONF_PAIR const *pair)
3081 return (pair ? pair->attr : NULL);
3085 * Return the value of a CONF_PAIR
3088 char const *cf_pair_value(CONF_PAIR const *pair)
3090 return (pair ? pair->value : NULL);
3093 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
3095 return (pair ? pair->op : T_INVALID);
3098 /** Return the value (lhs) type
3100 * @param pair to extract value type from.
3101 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3102 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3104 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
3106 return (pair ? pair->lhs_type : T_INVALID);
3109 /** Return the value (rhs) type
3111 * @param pair to extract value type from.
3112 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3113 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3115 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
3117 return (pair ? pair->rhs_type : T_INVALID);
3121 * Turn a CONF_PAIR into a VALUE_PAIR
3122 * For now, ignore the "value_type" field...
3124 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
3127 fr_strerror_printf("Internal error");
3132 fr_strerror_printf("No value given for attribute %s", pair->attr);
3137 * false comparisons never match. BUT if it's a "string"
3138 * or `string`, then remember to expand it later.
3140 if ((pair->op != T_OP_CMP_FALSE) &&
3141 ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
3142 (pair->rhs_type == T_BACK_QUOTED_STRING))) {
3145 vp = fr_pair_make(pair, NULL, pair->attr, NULL, pair->op);
3150 if (fr_pair_mark_xlat(vp, pair->value) < 0) {
3159 return fr_pair_make(pair, NULL, pair->attr, pair->value, pair->op);
3163 * Return the first label of a CONF_SECTION
3166 char const *cf_section_name1(CONF_SECTION const *cs)
3168 return (cs ? cs->name1 : NULL);
3172 * Return the second label of a CONF_SECTION
3175 char const *cf_section_name2(CONF_SECTION const *cs)
3177 return (cs ? cs->name2 : NULL);
3180 /** Return name2 if set, else name1
3183 char const *cf_section_name(CONF_SECTION const *cs)
3187 name = cf_section_name2(cs);
3188 if (name) return name;
3190 return cf_section_name1(cs);
3194 * Find a value in a CONF_SECTION
3196 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
3200 cp = cf_pair_find(cs, attr);
3202 return (cp ? cp->value : NULL);
3206 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
3207 char const *name1, char const *name2)
3210 CONF_ITEM const *ci;
3212 if (!cs || !name1) return NULL;
3214 for (ci = &(cs->item); ci; ci = ci->next) {
3215 if (ci->type != CONF_ITEM_SECTION)
3218 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
3222 their2 = cf_item_to_section(ci)->name2;
3224 if ((!name2 && !their2) ||
3225 (name2 && their2 && (strcmp(name2, their2) == 0))) {
3226 return cf_item_to_section(ci);
3233 /** Find a pair with a name matching attr, after specified pair.
3235 * @param cs to search in.
3236 * @param pair to search from (may be NULL).
3237 * @param attr to find (may be NULL in which case any attribute matches).
3238 * @return the next matching CONF_PAIR or NULL if none matched.
3240 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
3241 CONF_PAIR const *pair, char const *attr)
3245 if (!cs) return NULL;
3248 * If pair is NULL and we're trying to find a specific
3249 * attribute this must be a first time run.
3251 * Find the pair with correct name.
3253 if (!pair && attr) return cf_pair_find(cs, attr);
3256 * Start searching from the next child, or from the head
3257 * of the list of children (if no pair was provided).
3259 for (ci = pair ? pair->item.next : cs->children;
3262 if (ci->type != CONF_ITEM_PAIR) continue;
3264 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
3267 return cf_item_to_pair(ci);
3271 * Find a CONF_SECTION, or return the root if name is NULL
3274 CONF_SECTION *cf_section_find(char const *name)
3277 return cf_section_sub_find(root_config, name);
3282 /** Find a sub-section in a section
3284 * This finds ANY section having the same first name.
3285 * The second name is ignored.
3287 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
3291 if (!cs || !name) return NULL; /* can't find an un-named section */
3294 * No sub-sections have been defined, so none exist.
3296 if (!cs->section_tree) return NULL;
3300 return rbtree_finddata(cs->section_tree, &mycs);
3304 /** Find a CONF_SECTION with both names.
3307 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
3308 char const *name1, char const *name2)
3312 if (!cs) cs = root_config;
3313 if (!cs) return NULL;
3316 CONF_SECTION mycs, *master_cs;
3318 if (!cs->section_tree) return NULL;
3323 master_cs = rbtree_finddata(cs->section_tree, &mycs);
3324 if (!master_cs) return NULL;
3327 * Look it up in the name2 tree. If it's there,
3330 if (master_cs->name2_tree) {
3331 CONF_SECTION *subcs;
3333 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
3334 if (subcs) return subcs;
3338 * We don't insert ourselves into the name2 tree.
3339 * So if there's nothing in the name2 tree, maybe
3340 * *we* are the answer.
3342 if (!master_cs->name2 && name2) return NULL;
3343 if (master_cs->name2 && !name2) return NULL;
3344 if (!master_cs->name2 && !name2) return master_cs;
3346 if (strcmp(master_cs->name2, name2) == 0) {
3354 * Else do it the old-fashioned way.
3356 for (ci = cs->children; ci; ci = ci->next) {
3357 CONF_SECTION *subcs;
3359 if (ci->type != CONF_ITEM_SECTION)
3362 subcs = cf_item_to_section(ci);
3363 if (!subcs->name2) {
3364 if (strcmp(subcs->name1, name2) == 0) break;
3366 if (strcmp(subcs->name2, name2) == 0) break;
3370 return cf_item_to_section(ci);
3374 * Return the next subsection after a CONF_SECTION
3375 * with a certain name1 (char *name1). If the requested
3376 * name1 is NULL, any name1 matches.
3379 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
3380 CONF_SECTION const *subsection,
3385 if (!section) return NULL;
3388 * If subsection is NULL this must be a first time run
3389 * Find the subsection with correct name
3393 ci = section->children;
3395 ci = subsection->item.next;
3398 for (; ci; ci = ci->next) {
3399 if (ci->type != CONF_ITEM_SECTION)
3401 if ((name1 == NULL) ||
3402 (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3406 return cf_item_to_section(ci);
3411 * Return the next section after a CONF_SECTION
3412 * with a certain name1 (char *name1). If the requested
3413 * name1 is NULL, any name1 matches.
3416 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3417 CONF_SECTION const *subsection,
3420 if (!section) return NULL;
3422 if (!section->item.parent) return NULL;
3424 return cf_subsection_find_next(section->item.parent, subsection, name1);
3427 /** Return the next item after a CONF_ITEM.
3430 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3432 if (!section) return NULL;
3435 * If item is NULL this must be a first time run
3436 * Return the first item
3439 return section->children;
3445 static void _pair_count(int *count, CONF_SECTION const *cs)
3447 CONF_ITEM const *ci;
3449 for (ci = cf_item_find_next(cs, NULL);
3451 ci = cf_item_find_next(cs, ci)) {
3453 if (cf_item_is_section(ci)) {
3454 _pair_count(count, cf_item_to_section(ci));
3462 /** Count the number of conf pairs beneath a section
3464 * @param[in] cs to search for items in.
3465 * @return number of pairs nested within section.
3467 int cf_pair_count(CONF_SECTION const *cs)
3471 _pair_count(&count, cs);
3476 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3478 if (!ci) return NULL;
3483 int cf_section_lineno(CONF_SECTION const *section)
3485 return section->item.lineno;
3488 char const *cf_pair_filename(CONF_PAIR const *pair)
3490 return pair->item.filename;
3493 char const *cf_section_filename(CONF_SECTION const *section)
3495 return section->item.filename;
3498 int cf_pair_lineno(CONF_PAIR const *pair)
3500 return pair->item.lineno;
3503 bool cf_item_is_section(CONF_ITEM const *item)
3505 return item->type == CONF_ITEM_SECTION;
3508 bool cf_item_is_pair(CONF_ITEM const *item)
3510 return item->type == CONF_ITEM_PAIR;
3514 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3515 void *data, void (*data_free)(void *))
3519 cd = talloc_zero(parent, CONF_DATA);
3520 if (!cd) return NULL;
3522 cd->item.type = CONF_ITEM_DATA;
3523 cd->item.parent = parent;
3524 cd->name = talloc_typed_strdup(cd, name);
3531 cd->free = data_free;
3534 talloc_set_destructor(cd, _cf_data_free);
3540 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3542 if (!cs || !name) return NULL;
3545 * Find the name in the tree, for speed.
3547 if (cs->data_tree) {
3552 return rbtree_finddata(cs->data_tree, &mycd);
3559 * Find data from a particular section.
3561 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3563 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3565 if (cd) return cd->data;
3571 * Add named data to a configuration section.
3573 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3574 void *data, void (*data_free)(void *),
3579 if (!cs || !name) return -1;
3582 * Already exists. Can't add it.
3584 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3586 cd = cf_data_alloc(cs, name, data, data_free);
3590 cf_item_add(cs, cf_data_to_item(cd));
3596 * Add named data to a configuration section.
3598 int cf_data_add(CONF_SECTION *cs, char const *name,
3599 void *data, void (*data_free)(void *))
3601 return cf_data_add_internal(cs, name, data, data_free, 0);
3604 /** Remove named data from a configuration section
3607 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3613 if (!cs || !name) return NULL;
3614 if (!cs->data_tree) return NULL;
3617 * Find the name in the tree, for speed.
3621 cd = rbtree_finddata(cs->data_tree, &mycd);
3622 if (!cd) return NULL;
3624 talloc_set_destructor(cd, NULL); /* Disarm the destructor */
3625 rbtree_deletebydata(cs->data_tree, &mycd);
3634 * This is here to make the rest of the code easier to read. It
3635 * ties conffile.c to log.c, but it means we don't have to
3636 * pollute every other function with the knowledge of the
3637 * configuration internals.
3639 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3645 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3650 ci->filename ? ci->filename : "unknown",
3651 ci->lineno ? ci->lineno : 0,
3654 ERROR("<unknown>[*]: %s", buffer);
3658 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3664 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3667 rad_assert(cs != NULL);
3670 cs->item.filename ? cs->item.filename : "unknown",
3671 cs->item.lineno ? cs->item.lineno : 0,
3675 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3681 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3684 rad_assert(cp != NULL);
3687 cp->item.filename ? cp->item.filename : "unknown",
3688 cp->item.lineno ? cp->item.lineno : 0,
3692 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3697 if ((rad_debug_lvl > 1) && cs) vradlog(L_DBG, fmt, ap);
3702 * Wrapper to simplify the code.
3704 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3710 if (rad_debug_lvl > 1 && cs) {
3711 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3713 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3718 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3720 if (!cs) return NULL;
3722 return cs->variables;
3726 * For "switch" and "case" statements.
3728 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3730 if (!cs) return T_INVALID;
3732 return cs->name2_type;