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)) {
415 typedef struct cf_file_callback_t {
417 rb_walker_t callback;
418 CONF_SECTION *modules;
419 } cf_file_callback_t;
423 * Return 0 for keep going, 1 for stop.
425 static int file_callback(void *ctx, void *data)
427 cf_file_callback_t *cb = ctx;
428 cf_file_t *file = data;
432 * The file doesn't exist or we can no longer read it.
434 if (stat(file->filename, &buf) < 0) {
435 cb->rcode = CF_FILE_ERROR;
440 * The file changed, we'll need to re-read it.
442 if (buf.st_mtime != file->buf.st_mtime) {
444 cb->rcode |= CF_FILE_CONFIG;
446 (void) cb->callback(cb->modules, file->cs);
447 cb->rcode |= CF_FILE_MODULE;
456 * See if any of the files have changed.
458 int cf_file_changed(CONF_SECTION *cs, rb_walker_t callback)
462 cf_file_callback_t cb;
465 top = cf_top_section(cs);
466 cd = cf_data_find_internal(top, "filename", 0);
467 if (!cd) return true;
471 cb.rcode = CF_FILE_NONE;
472 cb.callback = callback;
473 cb.modules = cf_section_sub_find(cs, "modules");
475 (void) rbtree_walk(tree, RBTREE_IN_ORDER, file_callback, &cb);
480 static int _cf_section_free(CONF_SECTION *cs)
483 * Name1 and name2 are allocated contiguous with
487 rbtree_free(cs->pair_tree);
488 cs->pair_tree = NULL;
490 if (cs->section_tree) {
491 rbtree_free(cs->section_tree);
492 cs->section_tree = NULL;
494 if (cs->name2_tree) {
495 rbtree_free(cs->name2_tree);
496 cs->name2_tree = NULL;
499 rbtree_free(cs->data_tree);
500 cs->data_tree = NULL;
506 /** Allocate a CONF_PAIR
508 * @param parent CONF_SECTION to hang this CONF_PAIR off of.
510 * @param value of CONF_PAIR.
511 * @param op T_OP_EQ, T_OP_SET etc.
512 * @param lhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
513 * @param rhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
514 * @return NULL on error, else a new CONF_SECTION parented by parent.
516 CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value,
517 FR_TOKEN op, FR_TOKEN lhs_type, FR_TOKEN rhs_type)
521 rad_assert(fr_equality_op[op] || fr_assignment_op[op]);
522 if (!attr) return NULL;
524 cp = talloc_zero(parent, CONF_PAIR);
525 if (!cp) return NULL;
527 cp->item.type = CONF_ITEM_PAIR;
528 cp->item.parent = parent;
529 cp->lhs_type = lhs_type;
530 cp->rhs_type = rhs_type;
533 cp->attr = talloc_typed_strdup(cp, attr);
541 cp->value = talloc_typed_strdup(cp, value);
542 if (!cp->value) goto error;
548 /** Duplicate a CONF_PAIR
550 * @param parent to allocate new pair in.
551 * @param cp to duplicate.
552 * @return NULL on error, else a duplicate of the input pair.
554 CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp)
561 new = cf_pair_alloc(parent, cp->attr, cf_pair_value(cp),
562 cp->op, cp->lhs_type, cp->rhs_type);
563 if (!new) return NULL;
565 new->parsed = cp->parsed;
566 new->item.lineno = cp->item.lineno;
569 * Avoid mallocs if possible.
571 if (!cp->item.filename || (strcmp(parent->item.filename, cp->item.filename) == 0)) {
572 new->item.filename = parent->item.filename;
574 new->item.filename = talloc_strdup(new, cp->item.filename);
580 /** Add a configuration pair to a section
582 * @param parent section to add pair to.
585 void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp)
587 cf_item_add(parent, cf_pair_to_item(cp));
590 /** Allocate a CONF_SECTION
592 * @param parent CONF_SECTION to hang this CONF_SECTION off of.
593 * @param name1 Primary name.
594 * @param name2 Secondary name.
595 * @return NULL on error, else a new CONF_SECTION parented by parent.
597 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
602 if (!name1) return NULL;
604 if (name2 && parent) {
605 if (strchr(name2, '$')) {
606 name2 = cf_expand_variables(parent->item.filename,
607 &parent->item.lineno,
609 buffer, sizeof(buffer), name2, NULL);
611 ERROR("Failed expanding section name");
617 cs = talloc_zero(parent, CONF_SECTION);
618 if (!cs) return NULL;
620 cs->item.type = CONF_ITEM_SECTION;
621 cs->item.parent = parent;
623 cs->name1 = talloc_typed_strdup(cs, name1);
631 cs->name2 = talloc_typed_strdup(cs, name2);
632 if (!cs->name2) goto error;
635 cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
636 if (!cs->pair_tree) goto error;
638 talloc_set_destructor(cs, _cf_section_free);
641 * Don't create a data tree, it may not be needed.
645 * Don't create the section tree here, it may not
649 if (parent) cs->depth = parent->depth + 1;
654 /** Duplicate a configuration section
656 * @note recursively duplicates any child sections.
657 * @note does not duplicate any data associated with a section, or its child sections.
659 * @param parent section (may be NULL).
660 * @param cs to duplicate.
661 * @param name1 of new section.
662 * @param name2 of new section.
663 * @param copy_meta Copy additional meta data for a section (like template, base, depth and variables).
664 * @return a duplicate of the existing section, or NULL on error.
666 CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs,
667 char const *name1, char const *name2, bool copy_meta)
669 CONF_SECTION *new, *subcs;
673 new = cf_section_alloc(parent, name1, name2);
676 new->template = cs->template;
677 new->base = cs->base;
678 new->depth = cs->depth;
679 new->variables = cs->variables;
682 new->item.lineno = cs->item.lineno;
684 if (!cs->item.filename || (parent && (strcmp(parent->item.filename, cs->item.filename) == 0))) {
685 new->item.filename = parent->item.filename;
687 new->item.filename = talloc_strdup(new, cs->item.filename);
690 for (ci = cs->children; ci; ci = ci->next) {
692 case CONF_ITEM_SECTION:
693 subcs = cf_item_to_section(ci);
694 subcs = cf_section_dup(new, subcs,
695 cf_section_name1(subcs), cf_section_name2(subcs),
701 cf_section_add(new, subcs);
705 cp = cf_pair_dup(new, cf_item_to_pair(ci));
710 cf_pair_add(new, cp);
713 case CONF_ITEM_DATA: /* Skip data */
716 case CONF_ITEM_INVALID:
724 void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
726 cf_item_add(parent, &(cs->item));
729 /** Replace pair in a given section with a new pair, of the given value.
731 * @param cs to replace pair in.
732 * @param cp to replace.
733 * @param value New value to assign to cp.
734 * @return 0 on success, -1 on failure.
736 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
739 CONF_ITEM *ci, *cn, **last;
741 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->lhs_type, cp->rhs_type);
742 if (!newp) return -1;
748 * Find the old one from the linked list, and replace it
751 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
753 cn->next = (*last)->next;
760 rbtree_deletebydata(cs->pair_tree, ci);
762 rbtree_insert(cs->pair_tree, cn);
769 * Add an item to a configuration section.
771 void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
774 CONF_ITEM *first = ci;
777 rad_assert((void *)cs != (void *)ci);
779 if (!cs || !ci) return;
782 rad_assert(cs->tail == NULL);
785 rad_assert(cs->tail != NULL);
790 * Update the trees (and tail) for each item added.
792 for (/* nothing */; ci != NULL; ci = ci->next) {
793 rad_assert(ci->next != first); /* simple cycle detection */
798 * For fast lookups, pairs and sections get
803 if (!rbtree_insert(cs->pair_tree, ci)) {
804 CONF_PAIR *cp = cf_item_to_pair(ci);
806 if (strcmp(cp->attr, "confdir") == 0) break;
807 if (!cp->value) break; /* module name, "ok", etc. */
811 case CONF_ITEM_SECTION: {
812 CONF_SECTION *cs_new = cf_item_to_section(ci);
813 CONF_SECTION *name1_cs;
815 if (!cs->section_tree) {
816 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
817 if (!cs->section_tree) {
818 ERROR("Out of memory");
823 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
825 if (!rbtree_insert(cs->section_tree, cs_new)) {
826 ERROR("Failed inserting section into tree");
833 * We already have a section of
834 * this "name1". Add a new
835 * sub-section based on name2.
837 if (!name1_cs->name2_tree) {
838 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
839 if (!name1_cs->name2_tree) {
840 ERROR("Out of memory");
846 * We don't care if this fails.
847 * If the user tries to create
848 * two sections of the same
849 * name1/name2, the duplicate
850 * section is just silently
853 rbtree_insert(name1_cs->name2_tree, cs_new);
855 } /* was a section */
858 if (!cs->data_tree) {
859 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
862 rbtree_insert(cs->data_tree, ci);
866 default: /* FIXME: assert & error! */
869 } /* switch over conf types */
874 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
875 CONF_SECTION *outercs,
880 CONF_SECTION const *cs = outercs;
884 if (!cs) goto no_such_item;
886 strlcpy(name, ptr, sizeof(name));
890 * ".foo" means "foo from the current section"
896 * Just '.' means the current section
899 return cf_section_to_item(cs);
903 * ..foo means "foo from the section
904 * enclosing this section" (etc.)
907 if (cs->item.parent) {
908 cs = cs->item.parent;
912 * .. means the section
913 * enclosing this section
916 return cf_section_to_item(cs);
921 * "foo.bar.baz" means "from the root"
923 } else if (strchr(p, '.') != NULL) {
924 if (!parentcs) goto no_such_item;
936 if (r && q > r) q = NULL;
937 if (q && q < r) r = NULL;
943 q = strchr(r + 1, ']');
944 if (!q) return NULL; /* parse error */
947 * Points to foo[bar]xx: parse error,
948 * it should be foo[bar] or foo[bar].baz
950 if (q[1] && q[1] != '.') goto no_such_item;
954 next = cf_section_sub_find_name2(cs, p, r + 1);
959 * Points to a named instance of a section.
962 if (!next) goto no_such_item;
963 return &(next->item);
966 q++; /* ensure we skip the ']' and '.' */
970 next = cf_section_sub_find(cs, p);
974 if (!next) break; /* it MAY be a pair in this section! */
980 if (!*p) goto no_such_item;
984 * Find it in the current referenced
987 cp = cf_pair_find(cs, p);
989 cp->parsed = true; /* conf pairs which are referenced count as parsed */
993 next = cf_section_sub_find(cs, p);
994 if (next) return &(next->item);
997 * "foo" is "in the current section, OR in main".
999 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
1009 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
1011 if (!cs) return NULL;
1013 while (cs->item.parent != NULL) {
1014 cs = cs->item.parent;
1022 * Expand the variables in an input string.
1024 static char const *cf_expand_variables(char const *cf, int *lineno,
1025 CONF_SECTION *outercs,
1026 char *output, size_t outsize,
1027 char const *input, bool *soft_fail)
1030 char const *end, *ptr;
1031 CONF_SECTION const *parentcs;
1034 if (soft_fail) *soft_fail = false;
1037 * Find the master parent conf section.
1038 * We can't use main_config.config, because we're in the
1039 * process of re-building it, and it isn't set up yet...
1041 parentcs = cf_top_section(outercs);
1047 * Ignore anything other than "${"
1049 if ((*ptr == '$') && (ptr[1] == '{')) {
1055 * FIXME: Add support for ${foo:-bar},
1060 * Look for trailing '}', and log a
1061 * warning for anything that doesn't match,
1062 * and exit with a fatal error.
1064 end = strchr(ptr, '}');
1067 INFO("%s[%d]: Variable expansion missing }",
1075 * Can't really happen because input lines are
1076 * capped at 8k, which is sizeof(name)
1078 if ((size_t) (end - ptr) >= sizeof(name)) {
1079 ERROR("%s[%d]: Reference string is too large",
1084 memcpy(name, ptr, end - ptr);
1085 name[end - ptr] = '\0';
1087 q = strchr(name, ':');
1092 ci = cf_reference_item(parentcs, outercs, name);
1094 if (soft_fail) *soft_fail = true;
1095 ERROR("%s[%d]: Reference \"${%s}\" not found", cf, *lineno, name);
1100 * The expansion doesn't refer to another item or section
1101 * it's the property of a section.
1104 CONF_SECTION *mycs = cf_item_to_section(ci);
1106 if (ci->type != CONF_ITEM_SECTION) {
1107 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
1111 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
1112 case CONF_PROPERTY_NAME:
1113 strcpy(p, mycs->name1);
1116 case CONF_PROPERTY_INSTANCE:
1117 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
1121 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
1127 } else if (ci->type == CONF_ITEM_PAIR) {
1129 * Substitute the value of the variable.
1131 cp = cf_item_to_pair(ci);
1134 * If the thing we reference is
1135 * marked up as being expanded in
1136 * pass2, don't expand it now.
1137 * Let it be expanded in pass2.
1140 if (soft_fail) *soft_fail = true;
1142 ERROR("%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
1143 cf, *lineno, input);
1148 ERROR("%s[%d]: Reference \"%s\" has no value",
1149 cf, *lineno, input);
1153 if (p + strlen(cp->value) >= output + outsize) {
1154 ERROR("%s[%d]: Reference \"%s\" is too long",
1155 cf, *lineno, input);
1159 strcpy(p, cp->value);
1163 } else if (ci->type == CONF_ITEM_SECTION) {
1164 CONF_SECTION *subcs;
1167 * Adding an entry again to a
1168 * section is wrong. We don't
1169 * want an infinite loop.
1171 if (ci->parent == outercs) {
1172 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
1177 * Copy the section instead of
1180 subcs = cf_item_to_section(ci);
1181 subcs = cf_section_dup(outercs, subcs,
1182 cf_section_name1(subcs), cf_section_name2(subcs),
1185 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
1189 subcs->item.filename = ci->filename;
1190 subcs->item.lineno = ci->lineno;
1191 cf_item_add(outercs, &(subcs->item));
1196 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
1199 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
1205 * Look for trailing '}', and log a
1206 * warning for anything that doesn't match,
1207 * and exit with a fatal error.
1209 end = strchr(ptr, '}');
1212 INFO("%s[%d]: Environment variable expansion missing }",
1218 * Can't really happen because input lines are
1219 * capped at 8k, which is sizeof(name)
1221 if ((size_t) (end - ptr) >= sizeof(name)) {
1222 ERROR("%s[%d]: Environment variable name is too large",
1227 memcpy(name, ptr, end - ptr);
1228 name[end - ptr] = '\0';
1231 * Get the environment variable.
1232 * If none exists, then make it an empty string.
1240 if (p + strlen(env) >= output + outsize) {
1241 ERROR("%s[%d]: Reference \"%s\" is too long",
1242 cf, *lineno, input);
1252 * Copy it over verbatim.
1258 if (p >= (output + outsize)) {
1259 ERROR("%s[%d]: Reference \"%s\" is too long",
1260 cf, *lineno, input);
1263 } /* loop over all of the input string. */
1270 static char const parse_spaces[] = " ";
1272 /** Validation function for ipaddr conffile types
1275 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1276 fr_ipaddr_t *ipaddr)
1280 if (strcmp(value, "*") == 0) {
1281 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1282 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1283 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1285 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1286 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1290 case PW_TYPE_IPV4_ADDR:
1291 case PW_TYPE_IPV6_ADDR:
1292 case PW_TYPE_COMBO_IP_ADDR:
1293 switch (ipaddr->af) {
1295 if (ipaddr->prefix != 32) {
1296 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
1304 if (ipaddr->prefix != 128) {
1305 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
1320 /** Parses a #CONF_PAIR into a C data type, with a default value.
1322 * Takes fields from a #CONF_PARSER struct and uses them to parse the string value
1323 * of a #CONF_PAIR into a C data type matching the type argument.
1325 * The format of the types are the same as #value_data_t types.
1327 * @note The dflt value will only be used if no matching #CONF_PAIR is found. Empty strings will not
1328 * result in the dflt value being used.
1330 * **PW_TYPE to data type mappings**
1331 * | PW_TYPE | Data type | Dynamically allocated |
1332 * | ----------------------- | ------------------ | ---------------------- |
1333 * | PW_TYPE_TMPL | ``vp_tmpl_t`` | Yes |
1334 * | PW_TYPE_BOOLEAN | ``bool`` | No |
1335 * | PW_TYPE_INTEGER | ``uint32_t`` | No |
1336 * | PW_TYPE_SHORT | ``uint16_t`` | No |
1337 * | PW_TYPE_INTEGER64 | ``uint64_t`` | No |
1338 * | PW_TYPE_SIGNED | ``int32_t`` | No |
1339 * | PW_TYPE_STRING | ``char const *`` | Yes |
1340 * | PW_TYPE_IPV4_ADDR | ``fr_ipaddr_t`` | No |
1341 * | PW_TYPE_IPV4_PREFIX | ``fr_ipaddr_t`` | No |
1342 * | PW_TYPE_IPV6_ADDR | ``fr_ipaddr_t`` | No |
1343 * | PW_TYPE_IPV6_PREFIX | ``fr_ipaddr_t`` | No |
1344 * | PW_TYPE_COMBO_IP_ADDR | ``fr_ipaddr_t`` | No |
1345 * | PW_TYPE_COMBO_IP_PREFIX | ``fr_ipaddr_t`` | No |
1346 * | PW_TYPE_TIMEVAL | ``struct timeval`` | No |
1348 * @param cs to search for matching #CONF_PAIR in.
1349 * @param name of #CONF_PAIR to search for.
1350 * @param type Data type to parse #CONF_PAIR value as.
1351 * Should be one of the following ``data`` types, and one or more of the following ``flag`` types or'd together:
1352 * - ``data`` #PW_TYPE_TMPL - @copybrief PW_TYPE_TMPL
1353 * Feeds the value into #tmpl_afrom_str. Value can be
1354 * obtained when processing requests, with #tmpl_expand or #tmpl_aexpand.
1355 * - ``data`` #PW_TYPE_BOOLEAN - @copybrief PW_TYPE_BOOLEAN
1356 * - ``data`` #PW_TYPE_INTEGER - @copybrief PW_TYPE_INTEGER
1357 * - ``data`` #PW_TYPE_SHORT - @copybrief PW_TYPE_SHORT
1358 * - ``data`` #PW_TYPE_INTEGER64 - @copybrief PW_TYPE_INTEGER64
1359 * - ``data`` #PW_TYPE_SIGNED - @copybrief PW_TYPE_SIGNED
1360 * - ``data`` #PW_TYPE_STRING - @copybrief PW_TYPE_STRING
1361 * - ``data`` #PW_TYPE_IPV4_ADDR - @copybrief PW_TYPE_IPV4_ADDR (IPv4 address with prefix 32).
1362 * - ``data`` #PW_TYPE_IPV4_PREFIX - @copybrief PW_TYPE_IPV4_PREFIX (IPv4 address with variable prefix).
1363 * - ``data`` #PW_TYPE_IPV6_ADDR - @copybrief PW_TYPE_IPV6_ADDR (IPv6 address with prefix 128).
1364 * - ``data`` #PW_TYPE_IPV6_PREFIX - @copybrief PW_TYPE_IPV6_PREFIX (IPv6 address with variable prefix).
1365 * - ``data`` #PW_TYPE_COMBO_IP_ADDR - @copybrief PW_TYPE_COMBO_IP_ADDR (IPv4/IPv6 address with
1367 * - ``data`` #PW_TYPE_COMBO_IP_PREFIX - @copybrief PW_TYPE_COMBO_IP_PREFIX (IPv4/IPv6 address with
1369 * - ``data`` #PW_TYPE_TIMEVAL - @copybrief PW_TYPE_TIMEVAL
1370 * - ``flag`` #PW_TYPE_DEPRECATED - @copybrief PW_TYPE_DEPRECATED
1371 * - ``flag`` #PW_TYPE_REQUIRED - @copybrief PW_TYPE_REQUIRED
1372 * - ``flag`` #PW_TYPE_ATTRIBUTE - @copybrief PW_TYPE_ATTRIBUTE
1373 * - ``flag`` #PW_TYPE_SECRET - @copybrief PW_TYPE_SECRET
1374 * - ``flag`` #PW_TYPE_FILE_INPUT - @copybrief PW_TYPE_FILE_INPUT
1375 * - ``flag`` #PW_TYPE_NOT_EMPTY - @copybrief PW_TYPE_NOT_EMPTY
1376 * @param data Pointer to a global variable, or pointer to a field in the struct being populated with values.
1377 * @param dflt value to use, if no #CONF_PAIR is found.
1379 * - 1 if default value was used.
1382 * - -2 if deprecated.
1384 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1387 bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, multi;
1390 CONF_PAIR *cp = NULL;
1391 fr_ipaddr_t *ipaddr;
1393 CONF_ITEM *c_item = &cs->item;
1397 deprecated = (type & PW_TYPE_DEPRECATED);
1398 required = (type & PW_TYPE_REQUIRED);
1399 attribute = (type & PW_TYPE_ATTRIBUTE);
1400 secret = (type & PW_TYPE_SECRET);
1401 file_input = (type == PW_TYPE_FILE_INPUT); /* check, not and */
1402 cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1403 tmpl = (type & PW_TYPE_TMPL);
1404 multi = (type & PW_TYPE_MULTI);
1406 if (attribute) required = true;
1407 if (required) cant_be_empty = true; /* May want to review this in the future... */
1410 * Everything except templates must have a base type.
1412 if (!(type & 0xff) && !tmpl) {
1413 cf_log_err(c_item, "Configuration item \"%s\" must have a data type", name);
1417 type &= 0xff; /* normal types are small */
1421 cp = cf_pair_find(cs, name);
1424 * No pairs match the configuration item name in the current
1425 * section, use the default value.
1428 if (deprecated) return 0; /* Don't set the default value */
1433 * Something matched, used the CONF_PAIR value.
1436 CONF_PAIR *next = cp;
1442 * @fixme We should actually validate
1443 * the value of the pairs too
1445 if (multi) while ((next = cf_pair_find_next(cs, next, name))) {
1446 next->parsed = true;
1450 cf_log_err(c_item, "Configuration item \"%s\" is deprecated", name);
1459 cf_log_err(c_item, "Configuration item \"%s\" must have a value", name);
1466 if ((value[0] == '\0') && cant_be_empty) {
1468 cf_log_err(c_item, "Configuration item \"%s\" must not be empty (zero length)", name);
1469 if (!required) cf_log_err(c_item, "Comment item to silence this message");
1476 * Process a value as a LITERAL template. Once all of
1477 * the attrs and xlats are defined, the pass2 code
1478 * converts it to the appropriate type.
1484 *(vp_tmpl_t **)data = NULL;
1488 rad_assert(!attribute);
1489 vpt = tmpl_alloc(cs, TMPL_TYPE_LITERAL, value, strlen(value));
1490 *(vp_tmpl_t **)data = vpt;
1496 case PW_TYPE_BOOLEAN:
1498 * Allow yes/no, true/false, and on/off
1500 if ((strcasecmp(value, "yes") == 0) ||
1501 (strcasecmp(value, "true") == 0) ||
1502 (strcasecmp(value, "on") == 0)) {
1503 *(bool *)data = true;
1504 } else if ((strcasecmp(value, "no") == 0) ||
1505 (strcasecmp(value, "false") == 0) ||
1506 (strcasecmp(value, "off") == 0)) {
1507 *(bool *)data = false;
1509 *(bool *)data = false;
1510 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1511 "variable %s", value, name);
1514 cf_log_info(cs, "%.*s\t%s = %s",
1515 cs->depth, parse_spaces, name, value);
1518 case PW_TYPE_INTEGER:
1520 unsigned long v = strtoul(value, 0, 0);
1523 * Restrict integer values to 0-INT32_MAX, this means
1524 * it will always be safe to cast them to a signed type
1525 * for comparisons, and imposes the same range limit as
1526 * before we switched to using an unsigned type to
1527 * represent config item integers.
1529 if (v > INT32_MAX) {
1530 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1535 *(uint32_t *)data = v;
1536 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1542 unsigned long v = strtoul(value, 0, 0);
1544 if (v > UINT8_MAX) {
1545 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1549 *(uint8_t *)data = (uint8_t) v;
1550 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint8_t *)data);
1556 unsigned long v = strtoul(value, 0, 0);
1558 if (v > UINT16_MAX) {
1559 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1563 *(uint16_t *)data = (uint16_t) v;
1564 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1568 case PW_TYPE_INTEGER64:
1569 *(uint64_t *)data = strtoull(value, 0, 0);
1570 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1573 case PW_TYPE_SIGNED:
1574 *(int32_t *)data = strtol(value, 0, 0);
1575 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1578 case PW_TYPE_STRING:
1585 * Expand variables which haven't already been
1586 * expanded automagically when the configuration
1589 if (value == dflt) {
1592 lineno = cs->item.lineno;
1594 value = cf_expand_variables("<internal>",
1596 cs, buffer, sizeof(buffer),
1599 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1604 if (required && !value) goto is_required;
1605 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1608 if (!dict_attrbyname(value)) {
1610 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1613 cf_log_err(&(cp->item), "No such attribute '%s'", value);
1620 * Hide secrets when using "radiusd -X".
1622 if (secret && (rad_debug_lvl <= 2)) {
1623 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1624 cs->depth, parse_spaces, name);
1626 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1627 cs->depth, parse_spaces, name, value ? value : "(null)");
1629 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1632 * If there's data AND it's an input file, check
1633 * that we can read it. This check allows errors
1634 * to be caught as early as possible, during
1637 if (*q && file_input && !cf_file_input(cs, *q)) {
1642 case PW_TYPE_IPV4_ADDR:
1643 case PW_TYPE_IPV4_PREFIX:
1646 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1647 ERROR("%s", fr_strerror());
1650 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1653 case PW_TYPE_IPV6_ADDR:
1654 case PW_TYPE_IPV6_PREFIX:
1657 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1658 ERROR("%s", fr_strerror());
1661 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1664 case PW_TYPE_COMBO_IP_ADDR:
1665 case PW_TYPE_COMBO_IP_PREFIX:
1668 if (fr_pton(ipaddr, value, -1, AF_UNSPEC, true) < 0) {
1669 ERROR("%s", fr_strerror());
1672 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1675 case PW_TYPE_TIMEVAL: {
1680 sec = strtoul(value, &end, 10);
1686 len = strlen(end + 1);
1689 ERROR("Too much precision for timeval");
1694 * If they write "0.1", that means
1695 * "10000" microseconds.
1697 sec = strtoul(end + 1, NULL, 10);
1705 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1706 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1707 memcpy(data, &tv, sizeof(tv));
1713 * If we get here, it's a sanity check error.
1714 * It's not an error parsing the configuration
1717 rad_assert(type > PW_TYPE_INVALID);
1718 rad_assert(type < PW_TYPE_MAX);
1720 ERROR("type '%s' is not supported in the configuration files",
1721 fr_int2str(dict_attr_types, type, "?Unknown?"));
1723 } /* switch over variable type */
1728 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1729 if (!cpn) return -1;
1731 cpn->item.filename = "<internal>";
1732 cpn->item.lineno = 0;
1733 cf_item_add(cs, &(cpn->item));
1741 * A copy of cf_section_parse that initializes pointers before
1744 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1745 CONF_PARSER const *variables)
1749 for (i = 0; variables[i].name != NULL; i++) {
1750 if (variables[i].type == PW_TYPE_SUBSECTION) {
1751 CONF_SECTION *subcs;
1753 if (!variables[i].dflt) continue;
1755 subcs = cf_section_sub_find(cs, variables[i].name);
1758 * If there's no subsection in the
1759 * config, BUT the CONF_PARSER wants one,
1760 * then create an empty one. This is so
1761 * that we can track the strings,
1762 * etc. allocated in the subsection.
1765 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1768 subcs->item.filename = cs->item.filename;
1769 subcs->item.lineno = cs->item.lineno;
1770 cf_item_add(cs, &(subcs->item));
1773 cf_section_parse_init(subcs, (uint8_t *)base + variables[i].offset,
1774 (CONF_PARSER const *) variables[i].dflt);
1778 if ((variables[i].type != PW_TYPE_STRING) &&
1779 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1780 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1784 if (variables[i].data) {
1785 *(char **) variables[i].data = NULL;
1787 *(char **) (((char *)base) + variables[i].offset) = NULL;
1791 } /* for all variables in the configuration section */
1795 static void cf_section_parse_warn(CONF_SECTION *cs)
1799 for (ci = cs->children; ci; ci = ci->next) {
1801 * Don't recurse on sections. We can only safely
1802 * check conf pairs at the same level as the
1803 * section that was just parsed.
1805 if (ci->type == CONF_ITEM_SECTION) continue;
1806 if (ci->type == CONF_ITEM_PAIR) {
1809 cp = cf_item_to_pair(ci);
1810 if (cp->parsed) continue;
1812 WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
1813 cp->item.filename ? cp->item.filename : "unknown",
1814 cp->item.lineno ? cp->item.lineno : 0,
1819 * Skip everything else.
1824 /** Parse a configuration section into user-supplied variables
1826 * @param cs to parse.
1827 * @param base pointer to a struct to fill with data. Any buffers will also be talloced
1828 * using this parent as a pointer.
1829 * @param variables mappings between struct fields and #CONF_ITEM s.
1832 * - -1 on general error.
1833 * - -2 if a deprecated #CONF_ITEM was found.
1835 int cf_section_parse(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1841 cs->variables = variables; /* this doesn't hurt anything */
1844 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces, cs->name1);
1846 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces, cs->name1, cs->name2);
1849 cf_section_parse_init(cs, base, variables);
1852 * Handle the known configuration parameters.
1854 for (i = 0; variables[i].name != NULL; i++) {
1856 * Handle subsections specially
1858 if (variables[i].type == PW_TYPE_SUBSECTION) {
1859 CONF_SECTION *subcs;
1861 subcs = cf_section_sub_find(cs, variables[i].name);
1863 * Default in this case is overloaded to mean a pointer
1864 * to the CONF_PARSER struct for the subsection.
1866 if (!variables[i].dflt || !subcs) {
1867 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1872 ret = cf_section_parse(subcs, (uint8_t *)base + variables[i].offset,
1873 (CONF_PARSER const *) variables[i].dflt);
1874 if (ret < 0) goto finish;
1876 } /* else it's a CONF_PAIR */
1878 if (variables[i].data) {
1879 data = variables[i].data; /* prefer this. */
1881 data = ((char *)base) + variables[i].offset;
1883 ERROR("Internal sanity check 2 failed in cf_section_parse");
1889 * Parse the pair we found, or a default value.
1891 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1893 case 1: /* Used default */
1900 case -1: /* Parse error */
1903 case -2: /* Deprecated CONF ITEM */
1904 if ((variables[i + 1].offset == variables[i].offset) &&
1905 (variables[i + 1].data == variables[i].data)) {
1906 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1907 variables[i + 1].name);
1911 } /* for all variables in the configuration section */
1914 * Ensure we have a proper terminator, type so we catch
1915 * missing terminators reliably
1917 rad_assert(variables[i].type == -1);
1920 * Warn about items in the configuration which weren't
1921 * checked during parsing.
1923 if (rad_debug_lvl >= 3) cf_section_parse_warn(cs);
1927 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1935 * Check XLAT things in pass 2. But don't cache the xlat stuff anywhere.
1937 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1946 * Handle the known configuration parameters.
1948 for (i = 0; variables[i].name != NULL; i++) {
1953 * Handle subsections specially
1955 if (variables[i].type == PW_TYPE_SUBSECTION) {
1956 CONF_SECTION *subcs;
1957 subcs = cf_section_sub_find(cs, variables[i].name);
1959 if (cf_section_parse_pass2(subcs, (uint8_t *)base + variables[i].offset,
1960 (CONF_PARSER const *) variables[i].dflt) < 0) {
1964 } /* else it's a CONF_PAIR */
1967 * Figure out which data we need to fix.
1969 if (variables[i].data) {
1970 data = variables[i].data; /* prefer this. */
1972 data = ((char *)base) + variables[i].offset;
1977 cp = cf_pair_find(cs, variables[i].name);
1981 if (!cp || !cp->value || !data) continue;
1983 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1984 (cp->rhs_type != T_BARE_WORD)) continue;
1987 * Non-xlat expansions shouldn't have xlat!
1989 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1990 ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1992 * Ignore %{... in shared secrets.
1993 * They're never dynamically expanded.
1995 if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
1997 if (strstr(cp->value, "%{") != NULL) {
1998 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
1999 cp->item.filename ? cp->item.filename : "unknown",
2000 cp->item.lineno ? cp->item.lineno : 0);
2006 * Parse (and throw away) the xlat string.
2008 * FIXME: All of these should be converted from PW_TYPE_XLAT
2011 if ((variables[i].type & PW_TYPE_XLAT) != 0) {
2013 * xlat expansions should be parseable.
2015 value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
2018 slen = xlat_tokenize(cs, value, &xlat, &error);
2020 char *spaces, *text;
2023 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
2025 cf_log_err(&cp->item, "Failed parsing expanded string:");
2026 cf_log_err(&cp->item, "%s", text);
2027 cf_log_err(&cp->item, "%s^ %s", spaces, error);
2029 talloc_free(spaces);
2041 * Convert the LITERAL template to the actual
2044 if ((variables[i].type & PW_TYPE_TMPL) != 0) {
2047 slen = tmpl_afrom_str(cs, &vpt, cp->value, talloc_array_length(cp->value) - 1,
2049 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
2051 error = fr_strerror();
2058 * Don't add default - update with new types.
2060 switch (vpt->type) {
2062 * All attributes should have been defined by this point.
2064 case TMPL_TYPE_ATTR_UNDEFINED:
2065 cf_log_err(&cp->item, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
2068 case TMPL_TYPE_LITERAL:
2069 case TMPL_TYPE_ATTR:
2070 case TMPL_TYPE_LIST:
2071 case TMPL_TYPE_DATA:
2072 case TMPL_TYPE_EXEC:
2073 case TMPL_TYPE_XLAT:
2074 case TMPL_TYPE_XLAT_STRUCT:
2077 case TMPL_TYPE_UNKNOWN:
2078 case TMPL_TYPE_REGEX:
2079 case TMPL_TYPE_REGEX_STRUCT:
2080 case TMPL_TYPE_NULL:
2084 talloc_free(*(vp_tmpl_t **)data);
2085 *(vp_tmpl_t **)data = vpt;
2089 * If the "multi" flag is set, check all of them.
2091 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
2092 cp = cf_pair_find_next(cs, cp, cp->attr);
2095 } /* for all variables in the configuration section */
2101 * Merge the template so everyting else "just works".
2103 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
2107 if (!cs || !template) return true;
2109 cs->template = NULL;
2112 * Walk over the template, adding its' entries to the
2113 * current section. But only if the entries don't
2114 * already exist in the current section.
2116 for (ci = template->children; ci; ci = ci->next) {
2117 if (ci->type == CONF_ITEM_PAIR) {
2118 CONF_PAIR *cp1, *cp2;
2121 * It exists, don't over-write it.
2123 cp1 = cf_item_to_pair(ci);
2124 if (cf_pair_find(cs, cp1->attr)) {
2129 * Create a new pair with all of the data
2132 cp2 = cf_pair_dup(cs, cp1);
2133 if (!cp2) return false;
2135 cp2->item.filename = cp1->item.filename;
2136 cp2->item.lineno = cp1->item.lineno;
2138 cf_item_add(cs, &(cp2->item));
2142 if (ci->type == CONF_ITEM_SECTION) {
2143 CONF_SECTION *subcs1, *subcs2;
2145 subcs1 = cf_item_to_section(ci);
2146 rad_assert(subcs1 != NULL);
2148 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
2151 * sub-sections get merged.
2153 if (!cf_template_merge(subcs2, subcs1)) {
2160 * Our section doesn't have a matching
2161 * sub-section. Copy it verbatim from
2164 subcs2 = cf_section_dup(cs, subcs1,
2165 cf_section_name1(subcs1), cf_section_name2(subcs1),
2167 if (!subcs2) return false;
2169 subcs2->item.filename = subcs1->item.filename;
2170 subcs2->item.lineno = subcs1->item.lineno;
2172 cf_item_add(cs, &(subcs2->item));
2176 /* ignore everything else */
2182 static char const *cf_local_file(char const *base, char const *filename,
2183 char *buffer, size_t bufsize)
2188 strlcpy(buffer, base, bufsize);
2190 p = strrchr(buffer, FR_DIR_SEP);
2191 if (!p) return filename;
2192 if (p[1]) { /* ./foo */
2196 dirsize = (p - buffer) + 1;
2198 if ((dirsize + strlen(filename)) >= bufsize) {
2202 strlcpy(p + 1, filename, bufsize - dirsize);
2209 * Read a part of the config file.
2211 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
2212 CONF_SECTION *current)
2215 CONF_SECTION *this, *css;
2224 FR_TOKEN t1 = T_INVALID, t2, t3;
2225 bool has_spaces = false;
2230 this = current; /* add items here */
2233 * Read, checking for line continuations ('\\' at EOL)
2240 * Get data, and remember if we are at EOF.
2242 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
2246 * We read the entire 8k worth of data: complain.
2247 * Note that we don't care if the last character
2248 * is \n: it's still forbidden. This means that
2249 * the maximum allowed length of text is 8k-1, which
2253 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
2254 ERROR("%s[%d]: Line too long",
2261 while (isspace((int) *ptr)) ptr++;
2264 memmove(cbuf, ptr, len - (ptr - cbuf));
2265 len -= (ptr - cbuf);
2270 * Not doing continuations: check for edge
2277 while (*ptr && isspace((int) *ptr)) ptr++;
2279 if (!*ptr || (*ptr == '#')) continue;
2281 } else if (at_eof || (len == 0)) {
2282 ERROR("%s[%d]: Continuation at EOF is illegal",
2288 * See if there's a continuation.
2291 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
2296 if ((len > 0) && (cbuf[len - 1] == '\\')) {
2298 * Check for "suppress spaces" magic.
2300 if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
2304 cbuf[len - 1] = '\0';
2316 * The parser is getting to be evil.
2318 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
2320 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
2324 if (ptr[0] == '%') {
2325 hack = rad_copy_variable(buf1, ptr);
2327 hack = rad_copy_string(buf1, ptr);
2330 ERROR("%s[%d]: Invalid expansion: %s",
2331 filename, *lineno, ptr);
2337 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2344 ERROR("%s[%d]: Invalid expansion: %s",
2345 filename, *lineno, ptr);
2349 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
2353 * The caller eats "name1 name2 {", and calls us
2354 * for the data inside of the section. So if we
2355 * receive a closing brace, then it must mean the
2356 * end of the section.
2358 if (t1 == T_RCBRACE) {
2359 if (this == current) {
2360 ERROR("%s[%d]: Too many closing braces",
2366 * Merge the template into the existing
2367 * section. This uses more memory, but
2368 * means that templates now work with
2369 * sub-sections, etc.
2371 if (!cf_template_merge(this, this->template)) {
2375 this = this->item.parent;
2376 goto check_for_more;
2379 if (t1 != T_BARE_WORD) goto skip_keywords;
2382 * Allow for $INCLUDE files
2384 * This *SHOULD* work for any level include.
2385 * I really really really hate this file. -cparker
2387 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2388 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2389 bool relative = true;
2391 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2393 ERROR("%s[%d]: Unexpected text after $INCLUDE",
2398 if (buf2[0] == '$') relative = false;
2400 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2, NULL);
2401 if (!value) return -1;
2403 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2406 value = cf_local_file(filename, value, buf3,
2409 ERROR("%s[%d]: Directories too deep.",
2416 #ifdef HAVE_DIRENT_H
2420 * Include ALL non-"dot" files in the directory.
2423 if (value[strlen(value) - 1] == '/') {
2426 struct stat stat_buf;
2428 DEBUG2("including files in directory %s", value );
2433 if (stat(value, &stat_buf) < 0) {
2434 ERROR("%s[%d]: Failed reading directory %s: %s",
2436 value, fr_syserror(errno));
2440 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2441 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
2442 "insecure configuration", filename, *lineno, value);
2446 dir = opendir(value);
2448 ERROR("%s[%d]: Error reading directory %s: %s",
2449 filename, *lineno, value,
2450 fr_syserror(errno));
2455 * Read the directory, ignoring "." files.
2457 while ((dp = readdir(dir)) != NULL) {
2460 if (dp->d_name[0] == '.') continue;
2463 * Check for valid characters
2465 for (p = dp->d_name; *p != '\0'; p++) {
2466 if (isalpha((int)*p) ||
2470 (*p == '.')) continue;
2473 if (*p != '\0') continue;
2475 snprintf(buf2, sizeof(buf2), "%s%s",
2477 if ((stat(buf2, &stat_buf) != 0) ||
2478 S_ISDIR(stat_buf.st_mode)) continue;
2480 * Read the file into the current
2481 * configuration section.
2483 if (cf_file_include(this, buf2) < 0) {
2491 { /* it was a normal file */
2492 if (buf1[1] == '-') {
2493 struct stat statbuf;
2495 if (stat(value, &statbuf) < 0) {
2496 WARN("Not including file %s: %s", value, fr_syserror(errno));
2501 if (cf_file_include(this, value) < 0) {
2506 } /* we were in an include */
2508 if (strcasecmp(buf1, "$template") == 0) {
2510 CONF_SECTION *parentcs, *templatecs;
2511 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2514 ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2518 parentcs = cf_top_section(current);
2520 templatecs = cf_section_sub_find(parentcs, "templates");
2522 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2526 ci = cf_reference_item(parentcs, templatecs, buf2);
2527 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2528 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2533 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2537 if (this->template) {
2538 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2542 this->template = cf_item_to_section(ci);
2547 * Ensure that the user can't add CONF_PAIRs
2548 * with 'internal' names;
2550 if (buf1[0] == '_') {
2551 ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2556 * Handle if/elsif specially.
2558 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2560 char const *error = NULL;
2562 CONF_SECTION *server;
2563 fr_cond_t *cond = NULL;
2566 * if / elsif MUST be inside of a
2567 * processing section, which MUST in turn
2568 * be inside of a "server" directive.
2570 if (!this->item.parent) {
2572 ERROR("%s[%d]: Invalid location for '%s'",
2573 filename, *lineno, buf1);
2578 * Can only have "if" in 3 named sections.
2580 server = this->item.parent;
2582 (strcmp(server->name1, "server") != 0) &&
2583 (strcmp(server->name1, "policy") != 0) &&
2584 (strcmp(server->name1, "instantiate") != 0)) {
2585 server = server->item.parent;
2586 if (!server) goto invalid_location;
2590 * Skip (...) to find the {
2592 slen = fr_condition_tokenize(this, cf_section_to_item(this), ptr, &cond,
2593 &error, FR_COND_TWO_PASS);
2594 memcpy(&p, &ptr, sizeof(p));
2597 if (p[-slen] != '{') goto cond_error;
2603 * This hack is so that the NEXT stage
2604 * doesn't go "too far" in expanding the
2605 * variable. We can parse the conditions
2606 * without expanding the ${...} stuff.
2607 * BUT we don't want to expand all of the
2608 * stuff AFTER the condition. So we do
2611 * The first pass is to discover the end
2612 * of the condition. We then expand THAT
2613 * string, and do a second pass parsing
2614 * the expanded condition.
2620 * If there's a ${...}. If so, expand it.
2622 if (strchr(ptr, '$') != NULL) {
2623 ptr = cf_expand_variables(filename, lineno,
2628 ERROR("%s[%d]: Parse error expanding ${...} in condition",
2632 } /* else leave it alone */
2634 css = cf_section_alloc(this, buf1, ptr);
2636 ERROR("%s[%d]: Failed allocating memory for section",
2640 css->item.filename = filename;
2641 css->item.lineno = *lineno;
2643 slen = fr_condition_tokenize(css, cf_section_to_item(css), ptr, &cond,
2644 &error, FR_COND_TWO_PASS);
2645 *p = '{'; /* put it back */
2649 char *spaces, *text;
2651 fr_canonicalize_error(this, &spaces, &text, slen, ptr);
2653 ERROR("%s[%d]: Parse error in condition",
2655 ERROR("%s[%d]: %s", filename, *lineno, text);
2656 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2658 talloc_free(spaces);
2664 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2666 ERROR("%s[%d]: Condition is too large after \"%s\"",
2667 filename, *lineno, buf1);
2672 * Copy the expanded and parsed condition
2673 * into buf2. Then, parse the text after
2674 * the condition, which now MUST be a '{.
2676 * If it wasn't '{' it would have been
2677 * caught in the first pass of
2678 * conditional parsing, above.
2680 memcpy(buf2, ptr, slen);
2684 if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2686 ERROR("%s[%d]: Expected '{' %d",
2687 filename, *lineno, t3);
2692 * Swap the condition with trailing stuff for
2693 * the final condition.
2695 memcpy(&p, &css->name2, sizeof(css->name2));
2697 css->name2 = talloc_typed_strdup(css, buf2);
2699 cf_item_add(this, &(css->item));
2700 cf_data_add_internal(css, "if", cond, NULL, false);
2703 * The current section is now the child section.
2707 goto check_for_more;
2712 * Grab the next token.
2714 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2731 case T_OP_CMP_FALSE:
2732 if (!this || (strcmp(this->name1, "update") != 0)) {
2733 ERROR("%s[%d]: Invalid operator in assignment",
2741 while (isspace((int) *ptr)) ptr++;
2744 * New parser: non-quoted strings are
2745 * bare words, and we parse everything
2746 * until the next newline, or the next
2747 * comma. If they have { or } in a bare
2748 * word, well... too bad.
2750 if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2751 && (*ptr != '`') && (*ptr != '/')) {
2752 const char *q = ptr;
2755 while (*q && (*q >= ' ') && (*q != ',') &&
2758 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2759 ERROR("%s[%d]: Parse error: value too long",
2764 memcpy(buf3, ptr, (q - ptr));
2765 buf3[q - ptr] = '\0';
2769 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2772 if (t3 == T_INVALID) {
2773 ERROR("%s[%d]: Parse error: %s",
2780 * Allow "foo" by itself, or "foo = bar"
2786 case T_DOUBLE_QUOTED_STRING:
2787 case T_BACK_QUOTED_STRING:
2788 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3, &soft_fail);
2790 if (!soft_fail) return -1;
2793 * References an item which doesn't exist,
2794 * or which is already marked up as being
2795 * expanded in pass2. Wait for pass2 to
2796 * do the expansions.
2814 * Add this CONF_PAIR to our CONF_SECTION
2817 cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2818 if (!cpn) return -1;
2819 cpn->item.filename = filename;
2820 cpn->item.lineno = *lineno;
2822 cf_item_add(this, &(cpn->item));
2825 * Hacks for escaping
2827 if (!cf_new_escape && !this->item.parent && value &&
2828 (strcmp(buf1, "correct_escapes") == 0) &&
2829 ((strcmp(value, "true") == 0) ||
2830 (strcmp(value, "yes") == 0) ||
2831 (strcmp(value, "1") == 0))) {
2832 cf_new_escape = true;
2836 * Require a comma, unless there's a comment.
2838 while (isspace(*ptr)) ptr++;
2847 * foo = bar # other stuff
2849 if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2851 if (!*ptr || (*ptr == '}')) break;
2853 ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2854 filename, *lineno, value, ptr);
2858 * No '=', must be a section or sub-section.
2861 case T_DOUBLE_QUOTED_STRING:
2862 case T_SINGLE_QUOTED_STRING:
2863 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2864 if (t3 != T_LCBRACE) {
2865 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2866 filename, *lineno, buf1, buf2);
2872 css = cf_section_alloc(this, buf1,
2873 t2 == T_LCBRACE ? NULL : buf2);
2875 ERROR("%s[%d]: Failed allocating memory for section",
2880 css->item.filename = filename;
2881 css->item.lineno = *lineno;
2882 cf_item_add(this, &(css->item));
2885 * There may not be a name2
2887 css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2890 * The current section is now the child section.
2896 ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2901 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2902 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2909 * Done parsing one thing. Skip to EOL if possible.
2911 while (isspace(*ptr)) ptr++;
2913 if (*ptr == '#') continue;
2922 * See if EOF was unexpected ..
2924 if (feof(fp) && (this != current)) {
2925 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2926 filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2934 * Include one config file in another.
2936 static int cf_file_include(CONF_SECTION *cs, char const *filename_in)
2940 char const *filename;
2943 * So we only need to do this once.
2945 filename = talloc_strdup(cs, filename_in);
2947 DEBUG2("including configuration file %s", filename);
2949 fp = cf_file_open(cs, filename);
2952 if (!cs->item.filename) cs->item.filename = filename;
2955 * Read the section. It's OK to have EOF without a
2956 * matching close brace.
2958 if (cf_section_read(filename, &lineno, fp, cs) < 0) {
2969 * Do variable expansion in pass2.
2971 * This is a breadth-first expansion. "deep
2973 static int cf_section_pass2(CONF_SECTION *cs)
2977 for (ci = cs->children; ci; ci = ci->next) {
2982 if (ci->type != CONF_ITEM_PAIR) continue;
2984 cp = cf_item_to_pair(ci);
2985 if (!cp->value || !cp->pass2) continue;
2987 rad_assert((cp->rhs_type == T_BARE_WORD) ||
2988 (cp->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2989 (cp->rhs_type == T_BACK_QUOTED_STRING));
2991 value = cf_expand_variables(ci->filename, &ci->lineno, cs, buffer, sizeof(buffer), cp->value, NULL);
2992 if (!value) return -1;
2994 rad_const_free(cp->value);
2995 cp->value = talloc_typed_strdup(cp, value);
2998 for (ci = cs->children; ci; ci = ci->next) {
2999 if (ci->type != CONF_ITEM_SECTION) continue;
3001 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
3009 * Bootstrap a config file.
3011 int cf_file_read(CONF_SECTION *cs, char const *filename)
3017 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3020 p = strrchr(cp->value, FR_DIR_SEP);
3023 cp->item.filename = "<internal>";
3024 cp->item.lineno = -1;
3025 cf_item_add(cs, &(cp->item));
3027 tree = rbtree_create(cs, filename_cmp, NULL, 0);
3028 if (!tree) return -1;
3030 cf_data_add_internal(cs, "filename", tree, NULL, 0);
3032 if (cf_file_include(cs, filename) < 0) return -1;
3035 * Now that we've read the file, go back through it and
3036 * expand the variables.
3038 if (cf_section_pass2(cs) < 0) return -1;
3044 void cf_file_free(CONF_SECTION *cs)
3051 * Return a CONF_PAIR within a CONF_SECTION.
3053 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
3055 CONF_PAIR *cp, mycp;
3057 if (!cs || !name) return NULL;
3060 cp = rbtree_finddata(cs->pair_tree, &mycp);
3063 if (!cs->template) return NULL;
3065 return rbtree_finddata(cs->template->pair_tree, &mycp);
3069 * Return the attr of a CONF_PAIR
3072 char const *cf_pair_attr(CONF_PAIR const *pair)
3074 return (pair ? pair->attr : NULL);
3078 * Return the value of a CONF_PAIR
3081 char const *cf_pair_value(CONF_PAIR const *pair)
3083 return (pair ? pair->value : NULL);
3086 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
3088 return (pair ? pair->op : T_INVALID);
3091 /** Return the value (lhs) type
3093 * @param pair to extract value type from.
3094 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3095 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3097 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
3099 return (pair ? pair->lhs_type : T_INVALID);
3102 /** Return the value (rhs) type
3104 * @param pair to extract value type from.
3105 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3106 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3108 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
3110 return (pair ? pair->rhs_type : T_INVALID);
3114 * Turn a CONF_PAIR into a VALUE_PAIR
3115 * For now, ignore the "value_type" field...
3117 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
3120 fr_strerror_printf("Internal error");
3125 fr_strerror_printf("No value given for attribute %s", pair->attr);
3130 * false comparisons never match. BUT if it's a "string"
3131 * or `string`, then remember to expand it later.
3133 if ((pair->op != T_OP_CMP_FALSE) &&
3134 ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
3135 (pair->rhs_type == T_BACK_QUOTED_STRING))) {
3138 vp = fr_pair_make(pair, NULL, pair->attr, NULL, pair->op);
3143 if (fr_pair_mark_xlat(vp, pair->value) < 0) {
3152 return fr_pair_make(pair, NULL, pair->attr, pair->value, pair->op);
3156 * Return the first label of a CONF_SECTION
3159 char const *cf_section_name1(CONF_SECTION const *cs)
3161 return (cs ? cs->name1 : NULL);
3165 * Return the second label of a CONF_SECTION
3168 char const *cf_section_name2(CONF_SECTION const *cs)
3170 return (cs ? cs->name2 : NULL);
3173 /** Return name2 if set, else name1
3176 char const *cf_section_name(CONF_SECTION const *cs)
3180 name = cf_section_name2(cs);
3181 if (name) return name;
3183 return cf_section_name1(cs);
3187 * Find a value in a CONF_SECTION
3189 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
3193 cp = cf_pair_find(cs, attr);
3195 return (cp ? cp->value : NULL);
3199 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
3200 char const *name1, char const *name2)
3203 CONF_ITEM const *ci;
3205 if (!cs || !name1) return NULL;
3207 for (ci = &(cs->item); ci; ci = ci->next) {
3208 if (ci->type != CONF_ITEM_SECTION)
3211 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
3215 their2 = cf_item_to_section(ci)->name2;
3217 if ((!name2 && !their2) ||
3218 (name2 && their2 && (strcmp(name2, their2) == 0))) {
3219 return cf_item_to_section(ci);
3226 /** Find a pair with a name matching attr, after specified pair.
3228 * @param cs to search in.
3229 * @param pair to search from (may be NULL).
3230 * @param attr to find (may be NULL in which case any attribute matches).
3231 * @return the next matching CONF_PAIR or NULL if none matched.
3233 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
3234 CONF_PAIR const *pair, char const *attr)
3238 if (!cs) return NULL;
3241 * If pair is NULL and we're trying to find a specific
3242 * attribute this must be a first time run.
3244 * Find the pair with correct name.
3246 if (!pair && attr) return cf_pair_find(cs, attr);
3249 * Start searching from the next child, or from the head
3250 * of the list of children (if no pair was provided).
3252 for (ci = pair ? pair->item.next : cs->children;
3255 if (ci->type != CONF_ITEM_PAIR) continue;
3257 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
3260 return cf_item_to_pair(ci);
3264 * Find a CONF_SECTION, or return the root if name is NULL
3267 CONF_SECTION *cf_section_find(char const *name)
3270 return cf_section_sub_find(root_config, name);
3275 /** Find a sub-section in a section
3277 * This finds ANY section having the same first name.
3278 * The second name is ignored.
3280 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
3284 if (!cs || !name) return NULL; /* can't find an un-named section */
3287 * No sub-sections have been defined, so none exist.
3289 if (!cs->section_tree) return NULL;
3293 return rbtree_finddata(cs->section_tree, &mycs);
3297 /** Find a CONF_SECTION with both names.
3300 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
3301 char const *name1, char const *name2)
3305 if (!cs) cs = root_config;
3306 if (!cs) return NULL;
3309 CONF_SECTION mycs, *master_cs;
3311 if (!cs->section_tree) return NULL;
3316 master_cs = rbtree_finddata(cs->section_tree, &mycs);
3317 if (!master_cs) return NULL;
3320 * Look it up in the name2 tree. If it's there,
3323 if (master_cs->name2_tree) {
3324 CONF_SECTION *subcs;
3326 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
3327 if (subcs) return subcs;
3331 * We don't insert ourselves into the name2 tree.
3332 * So if there's nothing in the name2 tree, maybe
3333 * *we* are the answer.
3335 if (!master_cs->name2 && name2) return NULL;
3336 if (master_cs->name2 && !name2) return NULL;
3337 if (!master_cs->name2 && !name2) return master_cs;
3339 if (strcmp(master_cs->name2, name2) == 0) {
3347 * Else do it the old-fashioned way.
3349 for (ci = cs->children; ci; ci = ci->next) {
3350 CONF_SECTION *subcs;
3352 if (ci->type != CONF_ITEM_SECTION)
3355 subcs = cf_item_to_section(ci);
3356 if (!subcs->name2) {
3357 if (strcmp(subcs->name1, name2) == 0) break;
3359 if (strcmp(subcs->name2, name2) == 0) break;
3363 return cf_item_to_section(ci);
3367 * Return the next subsection after a CONF_SECTION
3368 * with a certain name1 (char *name1). If the requested
3369 * name1 is NULL, any name1 matches.
3372 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
3373 CONF_SECTION const *subsection,
3378 if (!section) return NULL;
3381 * If subsection is NULL this must be a first time run
3382 * Find the subsection with correct name
3386 ci = section->children;
3388 ci = subsection->item.next;
3391 for (; ci; ci = ci->next) {
3392 if (ci->type != CONF_ITEM_SECTION)
3394 if ((name1 == NULL) ||
3395 (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3399 return cf_item_to_section(ci);
3404 * Return the next section after a CONF_SECTION
3405 * with a certain name1 (char *name1). If the requested
3406 * name1 is NULL, any name1 matches.
3409 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3410 CONF_SECTION const *subsection,
3413 if (!section) return NULL;
3415 if (!section->item.parent) return NULL;
3417 return cf_subsection_find_next(section->item.parent, subsection, name1);
3420 /** Return the next item after a CONF_ITEM.
3423 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3425 if (!section) return NULL;
3428 * If item is NULL this must be a first time run
3429 * Return the first item
3432 return section->children;
3438 static void _pair_count(int *count, CONF_SECTION const *cs)
3440 CONF_ITEM const *ci;
3442 for (ci = cf_item_find_next(cs, NULL);
3444 ci = cf_item_find_next(cs, ci)) {
3446 if (cf_item_is_section(ci)) {
3447 _pair_count(count, cf_item_to_section(ci));
3455 /** Count the number of conf pairs beneath a section
3457 * @param[in] cs to search for items in.
3458 * @return number of pairs nested within section.
3460 int cf_pair_count(CONF_SECTION const *cs)
3464 _pair_count(&count, cs);
3469 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3471 if (!ci) return NULL;
3476 int cf_section_lineno(CONF_SECTION const *section)
3478 return section->item.lineno;
3481 char const *cf_pair_filename(CONF_PAIR const *pair)
3483 return pair->item.filename;
3486 char const *cf_section_filename(CONF_SECTION const *section)
3488 return section->item.filename;
3491 int cf_pair_lineno(CONF_PAIR const *pair)
3493 return pair->item.lineno;
3496 bool cf_item_is_section(CONF_ITEM const *item)
3498 return item->type == CONF_ITEM_SECTION;
3501 bool cf_item_is_pair(CONF_ITEM const *item)
3503 return item->type == CONF_ITEM_PAIR;
3507 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3508 void *data, void (*data_free)(void *))
3512 cd = talloc_zero(parent, CONF_DATA);
3513 if (!cd) return NULL;
3515 cd->item.type = CONF_ITEM_DATA;
3516 cd->item.parent = parent;
3517 cd->name = talloc_typed_strdup(cd, name);
3524 cd->free = data_free;
3527 talloc_set_destructor(cd, _cf_data_free);
3533 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3535 if (!cs || !name) return NULL;
3538 * Find the name in the tree, for speed.
3540 if (cs->data_tree) {
3545 return rbtree_finddata(cs->data_tree, &mycd);
3552 * Find data from a particular section.
3554 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3556 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3558 if (cd) return cd->data;
3564 * Add named data to a configuration section.
3566 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3567 void *data, void (*data_free)(void *),
3572 if (!cs || !name) return -1;
3575 * Already exists. Can't add it.
3577 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3579 cd = cf_data_alloc(cs, name, data, data_free);
3583 cf_item_add(cs, cf_data_to_item(cd));
3589 * Add named data to a configuration section.
3591 int cf_data_add(CONF_SECTION *cs, char const *name,
3592 void *data, void (*data_free)(void *))
3594 return cf_data_add_internal(cs, name, data, data_free, 0);
3597 /** Remove named data from a configuration section
3600 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3606 if (!cs || !name) return NULL;
3607 if (!cs->data_tree) return NULL;
3610 * Find the name in the tree, for speed.
3614 cd = rbtree_finddata(cs->data_tree, &mycd);
3615 if (!cd) return NULL;
3617 talloc_set_destructor(cd, NULL); /* Disarm the destructor */
3618 rbtree_deletebydata(cs->data_tree, &mycd);
3627 * This is here to make the rest of the code easier to read. It
3628 * ties conffile.c to log.c, but it means we don't have to
3629 * pollute every other function with the knowledge of the
3630 * configuration internals.
3632 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3638 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3643 ci->filename ? ci->filename : "unknown",
3644 ci->lineno ? ci->lineno : 0,
3647 ERROR("<unknown>[*]: %s", buffer);
3651 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3657 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3660 rad_assert(cs != NULL);
3663 cs->item.filename ? cs->item.filename : "unknown",
3664 cs->item.lineno ? cs->item.lineno : 0,
3668 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3674 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3677 rad_assert(cp != NULL);
3680 cp->item.filename ? cp->item.filename : "unknown",
3681 cp->item.lineno ? cp->item.lineno : 0,
3685 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3690 if ((rad_debug_lvl > 1) && cs) vradlog(L_DBG, fmt, ap);
3695 * Wrapper to simplify the code.
3697 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3703 if (rad_debug_lvl > 1 && cs) {
3704 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3706 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3711 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3713 if (!cs) return NULL;
3715 return cs->variables;
3719 * For "switch" and "case" statements.
3721 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3723 if (!cs) return T_INVALID;
3725 return cs->name2_type;