2 * conffile.c Read the radiusd.conf file.
4 * Yep I should learn to use lex & yacc, or at least
5 * write a decent parser. I know how to do that, really :)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * Copyright 2000,2006 The FreeRADIUS server project
25 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
26 * Copyright 2000 Alan DeKok <aland@ox.org>
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/parser.h>
33 #include <freeradius-devel/rad_assert.h>
39 #ifdef HAVE_SYS_STAT_H
45 typedef enum conf_property {
46 CONF_PROPERTY_INVALID = 0,
48 CONF_PROPERTY_INSTANCE,
51 static const FR_NAME_NUMBER conf_property_name[] = {
52 { "name", CONF_PROPERTY_NAME},
53 { "instance", CONF_PROPERTY_INSTANCE},
58 typedef enum conf_type {
59 CONF_ITEM_INVALID = 0,
66 struct conf_item *next; //!< Sibling.
67 struct conf_part *parent; //!< Parent.
68 int lineno; //!< The line number the config item began on.
69 char const *filename; //!< The file the config item was parsed from.
70 CONF_ITEM_TYPE type; //!< Whether the config item is a config_pair, conf_section or conf_data.
73 /** Configuration AVP similar to a VALUE_PAIR
78 char const *attr; //!< Attribute name
79 char const *value; //!< Attribute value
80 FR_TOKEN op; //!< Operator e.g. =, :=
81 FR_TOKEN lhs_type; //!< Name quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
82 FR_TOKEN rhs_type; //!< Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
83 bool pass2; //!< do expansion in pass2.
84 bool parsed; //!< Was this item used during parsing?
87 /** Internal data that is associated with a configuration section
94 void *data; //!< User data
95 void (*free)(void *); //!< Free user data function
100 char const *name1; //!< First name token. Given ``foo bar {}`` would be ``foo``.
101 char const *name2; //!< Second name token. Given ``foo bar {}`` would be ``bar``.
103 FR_TOKEN name2_type; //!< The type of quoting around name2.
106 CONF_ITEM *tail; //!< For speed.
107 CONF_SECTION *template;
109 rbtree_t *pair_tree; //!< and a partridge..
110 rbtree_t *section_tree; //!< no jokes here.
111 rbtree_t *name2_tree; //!< for sections of the same name2
117 CONF_PARSER const *variables;
120 typedef struct cf_file_t {
121 char const *filename;
127 CONF_SECTION *root_config = NULL;
128 bool cf_new_escape = false;
131 static int cf_data_add_internal(CONF_SECTION *cs, char const *name, void *data,
132 void (*data_free)(void *), int flag);
134 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag);
136 static char const *cf_expand_variables(char const *cf, int *lineno,
137 CONF_SECTION *outercs,
138 char *output, size_t outsize,
139 char const *input, bool *soft_fail);
141 static int cf_file_include(CONF_SECTION *cs, char const *filename_in);
146 * Isolate the scary casts in these tiny provably-safe functions
149 /** Cast a CONF_ITEM to a CONF_PAIR
152 CONF_PAIR *cf_item_to_pair(CONF_ITEM const *ci)
156 if (ci == NULL) return NULL;
158 rad_assert(ci->type == CONF_ITEM_PAIR);
160 memcpy(&out, &ci, sizeof(out));
164 /** Cast a CONF_ITEM to a CONF_SECTION
167 CONF_SECTION *cf_item_to_section(CONF_ITEM const *ci)
171 if (ci == NULL) return NULL;
173 rad_assert(ci->type == CONF_ITEM_SECTION);
175 memcpy(&out, &ci, sizeof(out));
179 /** Cast a CONF_PAIR to a CONF_ITEM
182 CONF_ITEM *cf_pair_to_item(CONF_PAIR const *cp)
186 if (cp == NULL) return NULL;
188 memcpy(&out, &cp, sizeof(out));
192 /** Cast a CONF_SECTION to a CONF_ITEM
195 CONF_ITEM *cf_section_to_item(CONF_SECTION const *cs)
199 if (cs == NULL) return NULL;
201 memcpy(&out, &cs, sizeof(out));
205 /** Cast CONF_DATA to a CONF_ITEM
208 static CONF_ITEM *cf_data_to_item(CONF_DATA const *cd)
216 memcpy(&out, &cd, sizeof(out));
220 static int _cf_data_free(CONF_DATA *cd)
222 if (cd->free) cd->free(cd->data);
228 * rbtree callback function
230 static int pair_cmp(void const *a, void const *b)
232 CONF_PAIR const *one = a;
233 CONF_PAIR const *two = b;
235 return strcmp(one->attr, two->attr);
240 * rbtree callback function
242 static int section_cmp(void const *a, void const *b)
244 CONF_SECTION const *one = a;
245 CONF_SECTION const *two = b;
247 return strcmp(one->name1, two->name1);
252 * rbtree callback function
254 static int name2_cmp(void const *a, void const *b)
256 CONF_SECTION const *one = a;
257 CONF_SECTION const *two = b;
259 rad_assert(strcmp(one->name1, two->name1) == 0);
261 if (!one->name2 && !two->name2) return 0;
262 if (one->name2 && !two->name2) return -1;
263 if (!one->name2 && two->name2) return +1;
265 return strcmp(one->name2, two->name2);
270 * rbtree callback function
272 static int data_cmp(void const *a, void const *b)
276 CONF_DATA const *one = a;
277 CONF_DATA const *two = b;
279 rcode = one->flag - two->flag;
280 if (rcode != 0) return rcode;
282 return strcmp(one->name, two->name);
286 * Functions for tracking filenames.
288 static int filename_cmp(void const *a, void const *b)
290 cf_file_t const *one = a;
291 cf_file_t const *two = b;
293 if (one->buf.st_dev < two->buf.st_dev) return -1;
294 if (one->buf.st_dev > two->buf.st_dev) return +1;
296 if (one->buf.st_ino < two->buf.st_ino) return -1;
297 if (one->buf.st_ino > two->buf.st_ino) return +1;
302 static FILE *cf_file_open(CONF_SECTION *cs, char const *filename)
311 top = cf_top_section(cs);
312 cd = cf_data_find_internal(top, "filename", 0);
313 if (!cd) return NULL;
317 fp = fopen(filename, "r");
319 ERROR("Unable to open file \"%s\": %s",
320 filename, fr_syserror(errno));
326 file = talloc(tree, cf_file_t);
332 file->filename = filename;
336 if (fstat(fd, &file->buf) == 0) {
338 if ((file->buf.st_mode & S_IWOTH) != 0) {
339 ERROR("Configuration file %s is globally writable. "
340 "Refusing to start due to insecure configuration.", filename);
350 * We can include the same file twice. e.g. when it
351 * contains common definitions, such as for SQL.
353 * Though the admin should really use templates for that.
355 if (!rbtree_insert(tree, file)) {
363 * Do some checks on the file as an "input" file. i.e. one read
366 static bool cf_file_input(CONF_SECTION *cs, char const *filename)
373 top = cf_top_section(cs);
374 cd = cf_data_find_internal(top, "filename", 0);
375 if (!cd) return false;
379 file = talloc(tree, cf_file_t);
380 if (!file) return false;
382 file->filename = filename;
386 if (stat(filename, &file->buf) < 0) {
387 ERROR("Unable to open file \"%s\": %s", filename, fr_syserror(errno));
393 if ((file->buf.st_mode & S_IWOTH) != 0) {
394 ERROR("Configuration file %s is globally writable. "
395 "Refusing to start due to insecure configuration.", filename);
402 * It's OK to include the same file twice...
404 if (!rbtree_insert(tree, file)) {
414 * Return 0 for keep going, 1 for stop.
416 static int file_callback(void *ctx, void *data)
420 cf_file_t *file = data;
423 * The file doesn't exist or we can no longer read it.
425 if (stat(file->filename, &buf) < 0) {
426 *rcode = CF_FILE_ERROR;
431 * The file changed, we'll need to re-read it.
433 if (buf.st_mtime != file->buf.st_mtime) {
435 * Set none -> whatever
437 if (*rcode == CF_FILE_NONE) {
439 *rcode = CF_FILE_CONFIG;
443 *rcode = CF_FILE_MODULE;
449 * A module WAS changed, but now we discover that
450 * a main config file has changed. We might as
451 * well re-load everything.
453 if ((*rcode == CF_FILE_MODULE) && !file->input) {
454 *rcode = CF_FILE_CONFIG;
464 * See if any of the files have changed.
466 int cf_file_changed(CONF_SECTION *cs)
473 top = cf_top_section(cs);
474 cd = cf_data_find_internal(top, "filename", 0);
475 if (!cd) return true;
479 rcode = CF_FILE_NONE;
480 (void) rbtree_walk(tree, RBTREE_IN_ORDER, file_callback, &rcode);
485 static int _cf_section_free(CONF_SECTION *cs)
488 * Name1 and name2 are allocated contiguous with
492 rbtree_free(cs->pair_tree);
493 cs->pair_tree = NULL;
495 if (cs->section_tree) {
496 rbtree_free(cs->section_tree);
497 cs->section_tree = NULL;
499 if (cs->name2_tree) {
500 rbtree_free(cs->name2_tree);
501 cs->name2_tree = NULL;
504 rbtree_free(cs->data_tree);
505 cs->data_tree = NULL;
511 /** Allocate a CONF_PAIR
513 * @param parent CONF_SECTION to hang this CONF_PAIR off of.
515 * @param value of CONF_PAIR.
516 * @param op T_OP_EQ, T_OP_SET etc.
517 * @param lhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
518 * @param rhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
519 * @return NULL on error, else a new CONF_SECTION parented by parent.
521 CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value,
522 FR_TOKEN op, FR_TOKEN lhs_type, FR_TOKEN rhs_type)
526 rad_assert(fr_equality_op[op] || fr_assignment_op[op]);
527 if (!attr) return NULL;
529 cp = talloc_zero(parent, CONF_PAIR);
530 if (!cp) return NULL;
532 cp->item.type = CONF_ITEM_PAIR;
533 cp->item.parent = parent;
534 cp->lhs_type = lhs_type;
535 cp->rhs_type = rhs_type;
538 cp->attr = talloc_typed_strdup(cp, attr);
546 cp->value = talloc_typed_strdup(cp, value);
547 if (!cp->value) goto error;
553 /** Duplicate a CONF_PAIR
555 * @param parent to allocate new pair in.
556 * @param cp to duplicate.
557 * @return NULL on error, else a duplicate of the input pair.
559 CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp)
566 new = cf_pair_alloc(parent, cp->attr, cf_pair_value(cp),
567 cp->op, cp->lhs_type, cp->rhs_type);
568 if (!new) return NULL;
570 new->parsed = cp->parsed;
571 new->item.lineno = cp->item.lineno;
574 * Avoid mallocs if possible.
576 if (!cp->item.filename || (strcmp(parent->item.filename, cp->item.filename) == 0)) {
577 new->item.filename = parent->item.filename;
579 new->item.filename = talloc_strdup(new, cp->item.filename);
585 /** Add a configuration pair to a section
587 * @param parent section to add pair to.
590 void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp)
592 cf_item_add(parent, cf_pair_to_item(cp));
595 /** Allocate a CONF_SECTION
597 * @param parent CONF_SECTION to hang this CONF_SECTION off of.
598 * @param name1 Primary name.
599 * @param name2 Secondary name.
600 * @return NULL on error, else a new CONF_SECTION parented by parent.
602 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
607 if (!name1) return NULL;
609 if (name2 && parent) {
610 if (strchr(name2, '$')) {
611 name2 = cf_expand_variables(parent->item.filename,
612 &parent->item.lineno,
614 buffer, sizeof(buffer), name2, NULL);
616 ERROR("Failed expanding section name");
622 cs = talloc_zero(parent, CONF_SECTION);
623 if (!cs) return NULL;
625 cs->item.type = CONF_ITEM_SECTION;
626 cs->item.parent = parent;
628 cs->name1 = talloc_typed_strdup(cs, name1);
636 cs->name2 = talloc_typed_strdup(cs, name2);
637 if (!cs->name2) goto error;
640 cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
641 if (!cs->pair_tree) goto error;
643 talloc_set_destructor(cs, _cf_section_free);
646 * Don't create a data tree, it may not be needed.
650 * Don't create the section tree here, it may not
654 if (parent) cs->depth = parent->depth + 1;
659 /** Duplicate a configuration section
661 * @note recursively duplicates any child sections.
662 * @note does not duplicate any data associated with a section, or its child sections.
664 * @param parent section (may be NULL).
665 * @param cs to duplicate.
666 * @param name1 of new section.
667 * @param name2 of new section.
668 * @param copy_meta Copy additional meta data for a section (like template, base, depth and variables).
669 * @return a duplicate of the existing section, or NULL on error.
671 CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs,
672 char const *name1, char const *name2, bool copy_meta)
674 CONF_SECTION *new, *subcs;
678 new = cf_section_alloc(parent, name1, name2);
681 new->template = cs->template;
682 new->base = cs->base;
683 new->depth = cs->depth;
684 new->variables = cs->variables;
687 new->item.lineno = cs->item.lineno;
689 if (!cs->item.filename || (parent && (strcmp(parent->item.filename, cs->item.filename) == 0))) {
690 new->item.filename = parent->item.filename;
692 new->item.filename = talloc_strdup(new, cs->item.filename);
695 for (ci = cs->children; ci; ci = ci->next) {
697 case CONF_ITEM_SECTION:
698 subcs = cf_item_to_section(ci);
699 subcs = cf_section_dup(new, subcs,
700 cf_section_name1(subcs), cf_section_name2(subcs),
706 cf_section_add(new, subcs);
710 cp = cf_pair_dup(new, cf_item_to_pair(ci));
715 cf_pair_add(new, cp);
718 case CONF_ITEM_DATA: /* Skip data */
721 case CONF_ITEM_INVALID:
729 void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
731 cf_item_add(parent, &(cs->item));
734 /** Replace pair in a given section with a new pair, of the given value.
736 * @param cs to replace pair in.
737 * @param cp to replace.
738 * @param value New value to assign to cp.
739 * @return 0 on success, -1 on failure.
741 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
744 CONF_ITEM *ci, *cn, **last;
746 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->lhs_type, cp->rhs_type);
747 if (!newp) return -1;
753 * Find the old one from the linked list, and replace it
756 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
758 cn->next = (*last)->next;
765 rbtree_deletebydata(cs->pair_tree, ci);
767 rbtree_insert(cs->pair_tree, cn);
774 * Add an item to a configuration section.
776 void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
779 CONF_ITEM *first = ci;
782 rad_assert((void *)cs != (void *)ci);
784 if (!cs || !ci) return;
787 rad_assert(cs->tail == NULL);
790 rad_assert(cs->tail != NULL);
795 * Update the trees (and tail) for each item added.
797 for (/* nothing */; ci != NULL; ci = ci->next) {
798 rad_assert(ci->next != first); /* simple cycle detection */
803 * For fast lookups, pairs and sections get
808 if (!rbtree_insert(cs->pair_tree, ci)) {
809 CONF_PAIR *cp = cf_item_to_pair(ci);
811 if (strcmp(cp->attr, "confdir") == 0) break;
812 if (!cp->value) break; /* module name, "ok", etc. */
816 case CONF_ITEM_SECTION: {
817 CONF_SECTION *cs_new = cf_item_to_section(ci);
818 CONF_SECTION *name1_cs;
820 if (!cs->section_tree) {
821 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
822 if (!cs->section_tree) {
823 ERROR("Out of memory");
828 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
830 if (!rbtree_insert(cs->section_tree, cs_new)) {
831 ERROR("Failed inserting section into tree");
838 * We already have a section of
839 * this "name1". Add a new
840 * sub-section based on name2.
842 if (!name1_cs->name2_tree) {
843 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
844 if (!name1_cs->name2_tree) {
845 ERROR("Out of memory");
851 * We don't care if this fails.
852 * If the user tries to create
853 * two sections of the same
854 * name1/name2, the duplicate
855 * section is just silently
858 rbtree_insert(name1_cs->name2_tree, cs_new);
860 } /* was a section */
863 if (!cs->data_tree) {
864 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
867 rbtree_insert(cs->data_tree, ci);
871 default: /* FIXME: assert & error! */
874 } /* switch over conf types */
879 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
880 CONF_SECTION *outercs,
885 CONF_SECTION const *cs = outercs;
889 if (!cs) goto no_such_item;
891 strlcpy(name, ptr, sizeof(name));
895 * ".foo" means "foo from the current section"
901 * Just '.' means the current section
904 return cf_section_to_item(cs);
908 * ..foo means "foo from the section
909 * enclosing this section" (etc.)
912 if (cs->item.parent) {
913 cs = cs->item.parent;
917 * .. means the section
918 * enclosing this section
921 return cf_section_to_item(cs);
926 * "foo.bar.baz" means "from the root"
928 } else if (strchr(p, '.') != NULL) {
929 if (!parentcs) goto no_such_item;
941 if (r && q > r) q = NULL;
942 if (q && q < r) r = NULL;
948 q = strchr(r + 1, ']');
949 if (!q) return NULL; /* parse error */
952 * Points to foo[bar]xx: parse error,
953 * it should be foo[bar] or foo[bar].baz
955 if (q[1] && q[1] != '.') goto no_such_item;
959 next = cf_section_sub_find_name2(cs, p, r + 1);
964 * Points to a named instance of a section.
967 if (!next) goto no_such_item;
968 return &(next->item);
971 q++; /* ensure we skip the ']' and '.' */
975 next = cf_section_sub_find(cs, p);
979 if (!next) break; /* it MAY be a pair in this section! */
985 if (!*p) goto no_such_item;
989 * Find it in the current referenced
992 cp = cf_pair_find(cs, p);
994 cp->parsed = true; /* conf pairs which are referenced count as parsed */
998 next = cf_section_sub_find(cs, p);
999 if (next) return &(next->item);
1002 * "foo" is "in the current section, OR in main".
1004 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
1014 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
1016 if (!cs) return NULL;
1018 while (cs->item.parent != NULL) {
1019 cs = cs->item.parent;
1027 * Expand the variables in an input string.
1029 static char const *cf_expand_variables(char const *cf, int *lineno,
1030 CONF_SECTION *outercs,
1031 char *output, size_t outsize,
1032 char const *input, bool *soft_fail)
1035 char const *end, *ptr;
1036 CONF_SECTION const *parentcs;
1039 if (soft_fail) *soft_fail = false;
1042 * Find the master parent conf section.
1043 * We can't use main_config.config, because we're in the
1044 * process of re-building it, and it isn't set up yet...
1046 parentcs = cf_top_section(outercs);
1052 * Ignore anything other than "${"
1054 if ((*ptr == '$') && (ptr[1] == '{')) {
1060 * FIXME: Add support for ${foo:-bar},
1065 * Look for trailing '}', and log a
1066 * warning for anything that doesn't match,
1067 * and exit with a fatal error.
1069 end = strchr(ptr, '}');
1072 INFO("%s[%d]: Variable expansion missing }",
1080 * Can't really happen because input lines are
1081 * capped at 8k, which is sizeof(name)
1083 if ((size_t) (end - ptr) >= sizeof(name)) {
1084 ERROR("%s[%d]: Reference string is too large",
1089 memcpy(name, ptr, end - ptr);
1090 name[end - ptr] = '\0';
1092 q = strchr(name, ':');
1097 ci = cf_reference_item(parentcs, outercs, name);
1099 if (soft_fail) *soft_fail = true;
1100 ERROR("%s[%d]: Reference \"${%s}\" not found", cf, *lineno, name);
1105 * The expansion doesn't refer to another item or section
1106 * it's the property of a section.
1109 CONF_SECTION *mycs = cf_item_to_section(ci);
1111 if (ci->type != CONF_ITEM_SECTION) {
1112 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
1116 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
1117 case CONF_PROPERTY_NAME:
1118 strcpy(p, mycs->name1);
1121 case CONF_PROPERTY_INSTANCE:
1122 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
1126 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
1132 } else if (ci->type == CONF_ITEM_PAIR) {
1134 * Substitute the value of the variable.
1136 cp = cf_item_to_pair(ci);
1139 * If the thing we reference is
1140 * marked up as being expanded in
1141 * pass2, don't expand it now.
1142 * Let it be expanded in pass2.
1145 if (soft_fail) *soft_fail = true;
1147 ERROR("%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
1148 cf, *lineno, input);
1153 ERROR("%s[%d]: Reference \"%s\" has no value",
1154 cf, *lineno, input);
1158 if (p + strlen(cp->value) >= output + outsize) {
1159 ERROR("%s[%d]: Reference \"%s\" is too long",
1160 cf, *lineno, input);
1164 strcpy(p, cp->value);
1168 } else if (ci->type == CONF_ITEM_SECTION) {
1169 CONF_SECTION *subcs;
1172 * Adding an entry again to a
1173 * section is wrong. We don't
1174 * want an infinite loop.
1176 if (ci->parent == outercs) {
1177 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
1182 * Copy the section instead of
1185 subcs = cf_item_to_section(ci);
1186 subcs = cf_section_dup(outercs, subcs,
1187 cf_section_name1(subcs), cf_section_name2(subcs),
1190 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
1194 subcs->item.filename = ci->filename;
1195 subcs->item.lineno = ci->lineno;
1196 cf_item_add(outercs, &(subcs->item));
1201 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
1204 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
1210 * Look for trailing '}', and log a
1211 * warning for anything that doesn't match,
1212 * and exit with a fatal error.
1214 end = strchr(ptr, '}');
1217 INFO("%s[%d]: Environment variable expansion missing }",
1223 * Can't really happen because input lines are
1224 * capped at 8k, which is sizeof(name)
1226 if ((size_t) (end - ptr) >= sizeof(name)) {
1227 ERROR("%s[%d]: Environment variable name is too large",
1232 memcpy(name, ptr, end - ptr);
1233 name[end - ptr] = '\0';
1236 * Get the environment variable.
1237 * If none exists, then make it an empty string.
1245 if (p + strlen(env) >= output + outsize) {
1246 ERROR("%s[%d]: Reference \"%s\" is too long",
1247 cf, *lineno, input);
1257 * Copy it over verbatim.
1263 if (p >= (output + outsize)) {
1264 ERROR("%s[%d]: Reference \"%s\" is too long",
1265 cf, *lineno, input);
1268 } /* loop over all of the input string. */
1275 static char const parse_spaces[] = " ";
1277 /** Validation function for ipaddr conffile types
1280 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1281 fr_ipaddr_t *ipaddr)
1285 if (strcmp(value, "*") == 0) {
1286 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1287 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1288 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1290 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1291 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1295 case PW_TYPE_IPV4_ADDR:
1296 case PW_TYPE_IPV6_ADDR:
1297 case PW_TYPE_COMBO_IP_ADDR:
1298 switch (ipaddr->af) {
1300 if (ipaddr->prefix != 32) {
1301 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
1309 if (ipaddr->prefix != 128) {
1310 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
1325 /** Parses a #CONF_PAIR into a C data type, with a default value.
1327 * Takes fields from a #CONF_PARSER struct and uses them to parse the string value
1328 * of a #CONF_PAIR into a C data type matching the type argument.
1330 * The format of the types are the same as #value_data_t types.
1332 * @note The dflt value will only be used if no matching #CONF_PAIR is found. Empty strings will not
1333 * result in the dflt value being used.
1335 * @param cs to search for matching #CONF_PAIR in.
1336 * @param name of #CONF_PAIR to search for.
1337 * @param type Data type to parse #CONF_PAIR value as.
1338 * Should be one of the following ``data`` types, and one or more of the following ``flag`` types or'd together:
1339 * - ``data`` #PW_TYPE_TMPL - @copybrief PW_TYPE_TMPL
1340 Feeds the value into #tmpl_afrom_str. Value can be
1341 * obtained when processing requests, with #tmpl_expand or #tmpl_aexpand.
1342 * - ``data`` #PW_TYPE_BOOLEAN - @copybrief PW_TYPE_BOOLEAN
1343 * - ``data`` #PW_TYPE_INTEGER - @copybrief PW_TYPE_INTEGER
1344 * - ``data`` #PW_TYPE_SHORT - @copybrief PW_TYPE_SHORT
1345 * - ``data`` #PW_TYPE_INTEGER64 - @copybrief PW_TYPE_INTEGER64
1346 * - ``data`` #PW_TYPE_SIGNED - @copybrief PW_TYPE_SIGNED
1347 * - ``data`` #PW_TYPE_STRING - @copybrief PW_TYPE_STRING
1348 * - ``data`` #PW_TYPE_IPV4_ADDR - @copybrief PW_TYPE_IPV4_ADDR (IPv4 address with prefix 32).
1349 * - ``data`` #PW_TYPE_IPV4_PREFIX - @copybrief PW_TYPE_IPV4_PREFIX (IPv4 address with variable prefix).
1350 * - ``data`` #PW_TYPE_IPV6_ADDR - @copybrief PW_TYPE_IPV6_ADDR (IPv6 address with prefix 128).
1351 * - ``data`` #PW_TYPE_COMBO_IP_ADDR - @copybrief PW_TYPE_COMBO_IP_ADDR (IPv4/IPv6 address with
1353 * - ``data`` #PW_TYPE_COMBO_IP_PREFIX - @copybrief PW_TYPE_COMBO_IP_PREFIX (IPv4/IPv6 address with
1355 * - ``data`` #PW_TYPE_TIMEVAL - @copybrief PW_TYPE_TIMEVAL
1356 * - ``flag`` #PW_TYPE_DEPRECATED - @copybrief PW_TYPE_DEPRECATED
1357 * - ``flag`` #PW_TYPE_REQUIRED - @copybrief PW_TYPE_REQUIRED
1358 * - ``flag`` #PW_TYPE_ATTRIBUTE - @copybrief PW_TYPE_ATTRIBUTE
1359 * - ``flag`` #PW_TYPE_SECRET - @copybrief PW_TYPE_SECRET
1360 * - ``flag`` #PW_TYPE_FILE_INPUT - @copybrief PW_TYPE_FILE_INPUT
1361 * - ``flag`` #PW_TYPE_NOT_EMPTY - @copybrief PW_TYPE_NOT_EMPTY
1362 * @param data Pointer to a global variable, or pointer to a field in the struct being populated with values.
1363 * @param dflt value to use, if no #CONF_PAIR is found.
1364 * @return -1 on error, -2 if deprecated, 0 on success (correctly parsed), 1 if default value was used.
1366 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1369 bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, multi;
1372 CONF_PAIR *cp = NULL;
1373 fr_ipaddr_t *ipaddr;
1375 CONF_ITEM *c_item = &cs->item;
1379 deprecated = (type & PW_TYPE_DEPRECATED);
1380 required = (type & PW_TYPE_REQUIRED);
1381 attribute = (type & PW_TYPE_ATTRIBUTE);
1382 secret = (type & PW_TYPE_SECRET);
1383 file_input = (type == PW_TYPE_FILE_INPUT); /* check, not and */
1384 cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1385 tmpl = (type & PW_TYPE_TMPL);
1386 multi = (type & PW_TYPE_MULTI);
1388 if (attribute) required = true;
1389 if (required) cant_be_empty = true; /* May want to review this in the future... */
1392 * Everything except templates must have a base type.
1394 if (!(type & 0xff) && !tmpl) {
1395 cf_log_err(c_item, "Configuration item '%s' must have a data type", name);
1399 type &= 0xff; /* normal types are small */
1403 cp = cf_pair_find(cs, name);
1406 * No pairs match the configuration item name in the current
1407 * section, use the default value.
1413 * Something matched, used the CONF_PAIR value.
1416 CONF_PAIR *next = cp;
1422 * @fixme We should actually validate
1423 * the value of the pairs too
1425 if (multi) while ((next = cf_pair_find_next(cs, next, name))) {
1426 next->parsed = true;
1433 cf_log_err(c_item, "Configuration item '%s' must have a value", name);
1440 if ((value[0] == '\0') && cant_be_empty) {
1442 cf_log_err(c_item, "Configuration item '%s' must not be empty (zero length)", name);
1445 cf_log_err(c_item, "Comment item to silence this message");
1451 cf_log_err(c_item, "Configuration item \"%s\" is deprecated", name);
1457 * Process a value as a LITERAL template. Once all of
1458 * the attrs and xlats are defined, the pass2 code
1459 * converts it to the appropriate type.
1465 *(vp_tmpl_t **)data = NULL;
1469 rad_assert(!attribute);
1470 vpt = tmpl_alloc(cs, TMPL_TYPE_LITERAL, value, strlen(value));
1471 *(vp_tmpl_t **)data = vpt;
1477 case PW_TYPE_BOOLEAN:
1479 * Allow yes/no, true/false, and on/off
1481 if ((strcasecmp(value, "yes") == 0) ||
1482 (strcasecmp(value, "true") == 0) ||
1483 (strcasecmp(value, "on") == 0)) {
1484 *(bool *)data = true;
1485 } else if ((strcasecmp(value, "no") == 0) ||
1486 (strcasecmp(value, "false") == 0) ||
1487 (strcasecmp(value, "off") == 0)) {
1488 *(bool *)data = false;
1490 *(bool *)data = false;
1491 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1492 "variable %s", value, name);
1495 cf_log_info(cs, "%.*s\t%s = %s",
1496 cs->depth, parse_spaces, name, value);
1499 case PW_TYPE_INTEGER:
1501 unsigned long v = strtoul(value, 0, 0);
1504 * Restrict integer values to 0-INT32_MAX, this means
1505 * it will always be safe to cast them to a signed type
1506 * for comparisons, and imposes the same range limit as
1507 * before we switched to using an unsigned type to
1508 * represent config item integers.
1510 if (v > INT32_MAX) {
1511 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1516 *(uint32_t *)data = v;
1517 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1523 unsigned long v = strtoul(value, 0, 0);
1525 if (v > UINT8_MAX) {
1526 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1530 *(uint8_t *)data = (uint8_t) v;
1531 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint8_t *)data);
1537 unsigned long v = strtoul(value, 0, 0);
1539 if (v > UINT16_MAX) {
1540 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1544 *(uint16_t *)data = (uint16_t) v;
1545 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1549 case PW_TYPE_INTEGER64:
1550 *(uint64_t *)data = strtoull(value, 0, 0);
1551 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1554 case PW_TYPE_SIGNED:
1555 *(int32_t *)data = strtol(value, 0, 0);
1556 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1559 case PW_TYPE_STRING:
1566 * Expand variables which haven't already been
1567 * expanded automagically when the configuration
1570 if (value == dflt) {
1573 lineno = cs->item.lineno;
1575 value = cf_expand_variables("<internal>",
1577 cs, buffer, sizeof(buffer),
1580 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1585 if (required && !value) goto is_required;
1586 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1589 if (!dict_attrbyname(value)) {
1591 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1594 cf_log_err(&(cp->item), "No such attribute '%s'", value);
1601 * Hide secrets when using "radiusd -X".
1603 if (secret && (rad_debug_lvl <= 2)) {
1604 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1605 cs->depth, parse_spaces, name);
1607 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1608 cs->depth, parse_spaces, name, value ? value : "(null)");
1610 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1613 * If there's data AND it's an input file, check
1614 * that we can read it. This check allows errors
1615 * to be caught as early as possible, during
1618 if (*q && file_input && !cf_file_input(cs, *q)) {
1623 case PW_TYPE_IPV4_ADDR:
1624 case PW_TYPE_IPV4_PREFIX:
1627 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1628 ERROR("%s", fr_strerror());
1631 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1634 case PW_TYPE_IPV6_ADDR:
1635 case PW_TYPE_IPV6_PREFIX:
1638 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1639 ERROR("%s", fr_strerror());
1642 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1645 case PW_TYPE_COMBO_IP_ADDR:
1646 case PW_TYPE_COMBO_IP_PREFIX:
1649 if (fr_pton(ipaddr, value, -1, true) < 0) {
1650 ERROR("%s", fr_strerror());
1653 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1656 case PW_TYPE_TIMEVAL: {
1661 sec = strtoul(value, &end, 10);
1667 len = strlen(end + 1);
1670 ERROR("Too much precision for timeval");
1675 * If they write "0.1", that means
1676 * "10000" microseconds.
1678 sec = strtoul(end + 1, NULL, 10);
1686 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1687 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1688 memcpy(data, &tv, sizeof(tv));
1694 * If we get here, it's a sanity check error.
1695 * It's not an error parsing the configuration
1698 rad_assert(type > PW_TYPE_INVALID);
1699 rad_assert(type < PW_TYPE_MAX);
1701 ERROR("type '%s' is not supported in the configuration files",
1702 fr_int2str(dict_attr_types, type, "?Unknown?"));
1704 } /* switch over variable type */
1709 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1710 if (!cpn) return -1;
1712 cpn->item.filename = "<internal>";
1713 cpn->item.lineno = 0;
1714 cf_item_add(cs, &(cpn->item));
1722 * A copy of cf_section_parse that initializes pointers before
1725 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1726 CONF_PARSER const *variables)
1730 for (i = 0; variables[i].name != NULL; i++) {
1731 if (variables[i].type == PW_TYPE_SUBSECTION) {
1732 CONF_SECTION *subcs;
1734 if (!variables[i].dflt) continue;
1736 subcs = cf_section_sub_find(cs, variables[i].name);
1739 * If there's no subsection in the
1740 * config, BUT the CONF_PARSER wants one,
1741 * then create an empty one. This is so
1742 * that we can track the strings,
1743 * etc. allocated in the subsection.
1746 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1749 subcs->item.filename = cs->item.filename;
1750 subcs->item.lineno = cs->item.lineno;
1751 cf_item_add(cs, &(subcs->item));
1754 cf_section_parse_init(subcs, (uint8_t *)base + variables[i].offset,
1755 (CONF_PARSER const *) variables[i].dflt);
1759 if ((variables[i].type != PW_TYPE_STRING) &&
1760 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1761 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1765 if (variables[i].data) {
1766 *(char **) variables[i].data = NULL;
1768 *(char **) (((char *)base) + variables[i].offset) = NULL;
1772 } /* for all variables in the configuration section */
1776 static void cf_section_parse_warn(CONF_SECTION *cs)
1780 for (ci = cs->children; ci; ci = ci->next) {
1782 * Don't recurse on sections. We can only safely
1783 * check conf pairs at the same level as the
1784 * section that was just parsed.
1786 if (ci->type == CONF_ITEM_SECTION) continue;
1787 if (ci->type == CONF_ITEM_PAIR) {
1790 cp = cf_item_to_pair(ci);
1791 if (cp->parsed) continue;
1793 WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
1794 cp->item.filename ? cp->item.filename : "unknown",
1795 cp->item.lineno ? cp->item.lineno : 0,
1800 * Skip everything else.
1806 * Parse a configuration section into user-supplied variables.
1808 int cf_section_parse(CONF_SECTION *cs, void *base,
1809 CONF_PARSER const *variables)
1815 cs->variables = variables; /* this doesn't hurt anything */
1818 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1821 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1822 cs->name1, cs->name2);
1825 cf_section_parse_init(cs, base, variables);
1828 * Handle the known configuration parameters.
1830 for (i = 0; variables[i].name != NULL; i++) {
1832 * Handle subsections specially
1834 if (variables[i].type == PW_TYPE_SUBSECTION) {
1835 CONF_SECTION *subcs;
1837 subcs = cf_section_sub_find(cs, variables[i].name);
1839 * Default in this case is overloaded to mean a pointer
1840 * to the CONF_PARSER struct for the subsection.
1842 if (!variables[i].dflt || !subcs) {
1843 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1847 if (cf_section_parse(subcs, (uint8_t *)base + variables[i].offset,
1848 (CONF_PARSER const *) variables[i].dflt) < 0) goto error;
1850 } /* else it's a CONF_PAIR */
1852 if (variables[i].data) {
1853 data = variables[i].data; /* prefer this. */
1855 data = ((char *)base) + variables[i].offset;
1857 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1862 * Parse the pair we found, or a default value.
1864 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1867 * Be nice, and print the name of the new config item.
1869 if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1870 (variables[i + 1].data == variables[i].data)) {
1871 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1872 variables[i + 1].name);
1877 } /* for all variables in the configuration section */
1880 * Warn about items in the configuration which weren't
1881 * checked during parsing.
1883 if (rad_debug_lvl >= 3) cf_section_parse_warn(cs);
1885 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1892 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1898 * Check XLAT things in pass 2. But don't cache the xlat stuff anywhere.
1900 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1909 * Handle the known configuration parameters.
1911 for (i = 0; variables[i].name != NULL; i++) {
1916 * Handle subsections specially
1918 if (variables[i].type == PW_TYPE_SUBSECTION) {
1919 CONF_SECTION *subcs;
1920 subcs = cf_section_sub_find(cs, variables[i].name);
1922 if (cf_section_parse_pass2(subcs, (uint8_t *)base + variables[i].offset,
1923 (CONF_PARSER const *) variables[i].dflt) < 0) {
1927 } /* else it's a CONF_PAIR */
1930 * Figure out which data we need to fix.
1932 if (variables[i].data) {
1933 data = variables[i].data; /* prefer this. */
1935 data = ((char *)base) + variables[i].offset;
1940 cp = cf_pair_find(cs, variables[i].name);
1944 if (!cp || !cp->value || !data) continue;
1946 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1947 (cp->rhs_type != T_BARE_WORD)) continue;
1950 * Non-xlat expansions shouldn't have xlat!
1952 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1953 ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1955 * Ignore %{... in shared secrets.
1956 * They're never dynamically expanded.
1958 if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
1960 if (strstr(cp->value, "%{") != NULL) {
1961 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
1962 cp->item.filename ? cp->item.filename : "unknown",
1963 cp->item.lineno ? cp->item.lineno : 0);
1969 * Parse (and throw away) the xlat string.
1971 * FIXME: All of these should be converted from PW_TYPE_XLAT
1974 if ((variables[i].type & PW_TYPE_XLAT) != 0) {
1976 * xlat expansions should be parseable.
1978 value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
1981 slen = xlat_tokenize(cs, value, &xlat, &error);
1983 char *spaces, *text;
1986 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
1988 cf_log_err(&cp->item, "Failed parsing expanded string:");
1989 cf_log_err(&cp->item, "%s", text);
1990 cf_log_err(&cp->item, "%s^ %s", spaces, error);
1992 talloc_free(spaces);
2004 * Convert the LITERAL template to the actual
2007 if ((variables[i].type & PW_TYPE_TMPL) != 0) {
2010 slen = tmpl_afrom_str(cs, &vpt, cp->value, talloc_array_length(cp->value) - 1,
2012 REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
2014 error = fr_strerror();
2021 * Don't add default - update with new types.
2023 switch (vpt->type) {
2025 * All attributes should have been defined by this point.
2027 case TMPL_TYPE_ATTR_UNDEFINED:
2028 cf_log_err(&cp->item, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
2031 case TMPL_TYPE_LITERAL:
2032 case TMPL_TYPE_ATTR:
2033 case TMPL_TYPE_LIST:
2034 case TMPL_TYPE_DATA:
2035 case TMPL_TYPE_EXEC:
2036 case TMPL_TYPE_XLAT:
2037 case TMPL_TYPE_XLAT_STRUCT:
2040 case TMPL_TYPE_UNKNOWN:
2041 case TMPL_TYPE_REGEX:
2042 case TMPL_TYPE_REGEX_STRUCT:
2043 case TMPL_TYPE_NULL:
2047 talloc_free(*(vp_tmpl_t **)data);
2048 *(vp_tmpl_t **)data = vpt;
2052 * If the "multi" flag is set, check all of them.
2054 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
2055 cp = cf_pair_find_next(cs, cp, cp->attr);
2058 } /* for all variables in the configuration section */
2064 * Merge the template so everyting else "just works".
2066 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
2070 if (!cs || !template) return true;
2072 cs->template = NULL;
2075 * Walk over the template, adding its' entries to the
2076 * current section. But only if the entries don't
2077 * already exist in the current section.
2079 for (ci = template->children; ci; ci = ci->next) {
2080 if (ci->type == CONF_ITEM_PAIR) {
2081 CONF_PAIR *cp1, *cp2;
2084 * It exists, don't over-write it.
2086 cp1 = cf_item_to_pair(ci);
2087 if (cf_pair_find(cs, cp1->attr)) {
2092 * Create a new pair with all of the data
2095 cp2 = cf_pair_dup(cs, cp1);
2096 if (!cp2) return false;
2098 cp2->item.filename = cp1->item.filename;
2099 cp2->item.lineno = cp1->item.lineno;
2101 cf_item_add(cs, &(cp2->item));
2105 if (ci->type == CONF_ITEM_SECTION) {
2106 CONF_SECTION *subcs1, *subcs2;
2108 subcs1 = cf_item_to_section(ci);
2109 rad_assert(subcs1 != NULL);
2111 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
2114 * sub-sections get merged.
2116 if (!cf_template_merge(subcs2, subcs1)) {
2123 * Our section doesn't have a matching
2124 * sub-section. Copy it verbatim from
2127 subcs2 = cf_section_dup(cs, subcs1,
2128 cf_section_name1(subcs1), cf_section_name2(subcs1),
2130 if (!subcs2) return false;
2132 subcs2->item.filename = subcs1->item.filename;
2133 subcs2->item.lineno = subcs1->item.lineno;
2135 cf_item_add(cs, &(subcs2->item));
2139 /* ignore everything else */
2145 static char const *cf_local_file(char const *base, char const *filename,
2146 char *buffer, size_t bufsize)
2151 strlcpy(buffer, base, bufsize);
2153 p = strrchr(buffer, FR_DIR_SEP);
2154 if (!p) return filename;
2155 if (p[1]) { /* ./foo */
2159 dirsize = (p - buffer) + 1;
2161 if ((dirsize + strlen(filename)) >= bufsize) {
2165 strlcpy(p + 1, filename, bufsize - dirsize);
2172 * Read a part of the config file.
2174 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
2175 CONF_SECTION *current)
2178 CONF_SECTION *this, *css;
2187 FR_TOKEN t1 = T_INVALID, t2, t3;
2188 bool has_spaces = false;
2193 this = current; /* add items here */
2196 * Read, checking for line continuations ('\\' at EOL)
2203 * Get data, and remember if we are at EOF.
2205 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
2209 * We read the entire 8k worth of data: complain.
2210 * Note that we don't care if the last character
2211 * is \n: it's still forbidden. This means that
2212 * the maximum allowed length of text is 8k-1, which
2216 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
2217 ERROR("%s[%d]: Line too long",
2224 while (isspace((int) *ptr)) ptr++;
2227 memmove(cbuf, ptr, len - (ptr - cbuf));
2228 len -= (ptr - cbuf);
2233 * Not doing continuations: check for edge
2240 while (*ptr && isspace((int) *ptr)) ptr++;
2242 if (!*ptr || (*ptr == '#')) continue;
2244 } else if (at_eof || (len == 0)) {
2245 ERROR("%s[%d]: Continuation at EOF is illegal",
2251 * See if there's a continuation.
2254 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
2259 if ((len > 0) && (cbuf[len - 1] == '\\')) {
2261 * Check for "suppress spaces" magic.
2263 if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
2267 cbuf[len - 1] = '\0';
2279 * The parser is getting to be evil.
2281 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
2283 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
2287 if (ptr[0] == '%') {
2288 hack = rad_copy_variable(buf1, ptr);
2290 hack = rad_copy_string(buf1, ptr);
2293 ERROR("%s[%d]: Invalid expansion: %s",
2294 filename, *lineno, ptr);
2300 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2307 ERROR("%s[%d]: Invalid expansion: %s",
2308 filename, *lineno, ptr);
2312 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
2316 * The caller eats "name1 name2 {", and calls us
2317 * for the data inside of the section. So if we
2318 * receive a closing brace, then it must mean the
2319 * end of the section.
2321 if (t1 == T_RCBRACE) {
2322 if (this == current) {
2323 ERROR("%s[%d]: Too many closing braces",
2329 * Merge the template into the existing
2330 * section. This uses more memory, but
2331 * means that templates now work with
2332 * sub-sections, etc.
2334 if (!cf_template_merge(this, this->template)) {
2338 this = this->item.parent;
2339 goto check_for_more;
2342 if (t1 != T_BARE_WORD) goto skip_keywords;
2345 * Allow for $INCLUDE files
2347 * This *SHOULD* work for any level include.
2348 * I really really really hate this file. -cparker
2350 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2351 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2352 bool relative = true;
2354 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2356 ERROR("%s[%d]: Unexpected text after $INCLUDE",
2361 if (buf2[0] == '$') relative = false;
2363 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2, NULL);
2364 if (!value) return -1;
2366 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2369 value = cf_local_file(filename, value, buf3,
2372 ERROR("%s[%d]: Directories too deep.",
2379 #ifdef HAVE_DIRENT_H
2383 * Include ALL non-"dot" files in the directory.
2386 if (value[strlen(value) - 1] == '/') {
2389 struct stat stat_buf;
2391 DEBUG2("including files in directory %s", value );
2396 if (stat(value, &stat_buf) < 0) {
2397 ERROR("%s[%d]: Failed reading directory %s: %s",
2399 value, fr_syserror(errno));
2403 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2404 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
2405 "insecure configuration", filename, *lineno, value);
2409 dir = opendir(value);
2411 ERROR("%s[%d]: Error reading directory %s: %s",
2412 filename, *lineno, value,
2413 fr_syserror(errno));
2418 * Read the directory, ignoring "." files.
2420 while ((dp = readdir(dir)) != NULL) {
2423 if (dp->d_name[0] == '.') continue;
2426 * Check for valid characters
2428 for (p = dp->d_name; *p != '\0'; p++) {
2429 if (isalpha((int)*p) ||
2433 (*p == '.')) continue;
2436 if (*p != '\0') continue;
2438 snprintf(buf2, sizeof(buf2), "%s%s",
2440 if ((stat(buf2, &stat_buf) != 0) ||
2441 S_ISDIR(stat_buf.st_mode)) continue;
2443 * Read the file into the current
2444 * configuration section.
2446 if (cf_file_include(this, buf2) < 0) {
2454 { /* it was a normal file */
2455 if (buf1[1] == '-') {
2456 struct stat statbuf;
2458 if (stat(value, &statbuf) < 0) {
2459 WARN("Not including file %s: %s", value, fr_syserror(errno));
2464 if (cf_file_include(this, value) < 0) {
2469 } /* we were in an include */
2471 if (strcasecmp(buf1, "$template") == 0) {
2473 CONF_SECTION *parentcs, *templatecs;
2474 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2477 ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2481 parentcs = cf_top_section(current);
2483 templatecs = cf_section_sub_find(parentcs, "templates");
2485 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2489 ci = cf_reference_item(parentcs, templatecs, buf2);
2490 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2491 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2496 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2500 if (this->template) {
2501 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2505 this->template = cf_item_to_section(ci);
2510 * Ensure that the user can't add CONF_PAIRs
2511 * with 'internal' names;
2513 if (buf1[0] == '_') {
2514 ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2519 * Handle if/elsif specially.
2521 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2523 char const *error = NULL;
2525 CONF_SECTION *server;
2526 fr_cond_t *cond = NULL;
2529 * if / elsif MUST be inside of a
2530 * processing section, which MUST in turn
2531 * be inside of a "server" directive.
2533 if (!this->item.parent) {
2535 ERROR("%s[%d]: Invalid location for '%s'",
2536 filename, *lineno, buf1);
2541 * Can only have "if" in 3 named sections.
2543 server = this->item.parent;
2545 (strcmp(server->name1, "server") != 0) &&
2546 (strcmp(server->name1, "policy") != 0) &&
2547 (strcmp(server->name1, "instantiate") != 0)) {
2548 server = server->item.parent;
2549 if (!server) goto invalid_location;
2553 * Skip (...) to find the {
2555 slen = fr_condition_tokenize(this, cf_section_to_item(this), ptr, &cond,
2556 &error, FR_COND_TWO_PASS);
2557 memcpy(&p, &ptr, sizeof(p));
2560 if (p[-slen] != '{') goto cond_error;
2566 * This hack is so that the NEXT stage
2567 * doesn't go "too far" in expanding the
2568 * variable. We can parse the conditions
2569 * without expanding the ${...} stuff.
2570 * BUT we don't want to expand all of the
2571 * stuff AFTER the condition. So we do
2574 * The first pass is to discover the end
2575 * of the condition. We then expand THAT
2576 * string, and do a second pass parsing
2577 * the expanded condition.
2583 * If there's a ${...}. If so, expand it.
2585 if (strchr(ptr, '$') != NULL) {
2586 ptr = cf_expand_variables(filename, lineno,
2591 ERROR("%s[%d]: Parse error expanding ${...} in condition",
2595 } /* else leave it alone */
2597 css = cf_section_alloc(this, buf1, ptr);
2599 ERROR("%s[%d]: Failed allocating memory for section",
2603 css->item.filename = filename;
2604 css->item.lineno = *lineno;
2606 slen = fr_condition_tokenize(css, cf_section_to_item(css), ptr, &cond,
2607 &error, FR_COND_TWO_PASS);
2608 *p = '{'; /* put it back */
2612 char *spaces, *text;
2614 fr_canonicalize_error(this, &spaces, &text, slen, ptr);
2616 ERROR("%s[%d]: Parse error in condition",
2618 ERROR("%s[%d]: %s", filename, *lineno, text);
2619 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2621 talloc_free(spaces);
2627 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2629 ERROR("%s[%d]: Condition is too large after \"%s\"",
2630 filename, *lineno, buf1);
2635 * Copy the expanded and parsed condition
2636 * into buf2. Then, parse the text after
2637 * the condition, which now MUST be a '{.
2639 * If it wasn't '{' it would have been
2640 * caught in the first pass of
2641 * conditional parsing, above.
2643 memcpy(buf2, ptr, slen);
2647 if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2649 ERROR("%s[%d]: Expected '{' %d",
2650 filename, *lineno, t3);
2655 * Swap the condition with trailing stuff for
2656 * the final condition.
2658 memcpy(&p, &css->name2, sizeof(css->name2));
2660 css->name2 = talloc_typed_strdup(css, buf2);
2662 cf_item_add(this, &(css->item));
2663 cf_data_add_internal(css, "if", cond, NULL, false);
2666 * The current section is now the child section.
2670 goto check_for_more;
2675 * Grab the next token.
2677 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2694 case T_OP_CMP_FALSE:
2695 if (!this || (strcmp(this->name1, "update") != 0)) {
2696 ERROR("%s[%d]: Invalid operator in assignment",
2704 while (isspace((int) *ptr)) ptr++;
2707 * New parser: non-quoted strings are
2708 * bare words, and we parse everything
2709 * until the next newline, or the next
2710 * comma. If they have { or } in a bare
2711 * word, well... too bad.
2713 if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2714 && (*ptr != '`') && (*ptr != '/')) {
2715 const char *q = ptr;
2718 while (*q && (*q >= ' ') && (*q != ',') &&
2721 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2722 ERROR("%s[%d]: Parse error: value too long",
2727 memcpy(buf3, ptr, (q - ptr));
2728 buf3[q - ptr] = '\0';
2732 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2735 if (t3 == T_INVALID) {
2736 ERROR("%s[%d]: Parse error: %s",
2743 * Allow "foo" by itself, or "foo = bar"
2749 case T_DOUBLE_QUOTED_STRING:
2750 case T_BACK_QUOTED_STRING:
2751 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3, &soft_fail);
2753 if (!soft_fail) return -1;
2756 * References an item which doesn't exist,
2757 * or which is already marked up as being
2758 * expanded in pass2. Wait for pass2 to
2759 * do the expansions.
2777 * Add this CONF_PAIR to our CONF_SECTION
2780 cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2781 if (!cpn) return -1;
2782 cpn->item.filename = filename;
2783 cpn->item.lineno = *lineno;
2785 cf_item_add(this, &(cpn->item));
2788 * Hacks for escaping
2790 if (!cf_new_escape && !this->item.parent && value &&
2791 (strcmp(buf1, "correct_escapes") == 0) &&
2792 ((strcmp(value, "true") == 0) ||
2793 (strcmp(value, "yes") == 0) ||
2794 (strcmp(value, "1") == 0))) {
2795 cf_new_escape = true;
2799 * Require a comma, unless there's a comment.
2801 while (isspace(*ptr)) ptr++;
2810 * foo = bar # other stuff
2812 if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2814 if (!*ptr || (*ptr == '}')) break;
2816 ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2817 filename, *lineno, value, ptr);
2821 * No '=', must be a section or sub-section.
2824 case T_DOUBLE_QUOTED_STRING:
2825 case T_SINGLE_QUOTED_STRING:
2826 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2827 if (t3 != T_LCBRACE) {
2828 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2829 filename, *lineno, buf1, buf2);
2835 css = cf_section_alloc(this, buf1,
2836 t2 == T_LCBRACE ? NULL : buf2);
2838 ERROR("%s[%d]: Failed allocating memory for section",
2843 css->item.filename = filename;
2844 css->item.lineno = *lineno;
2845 cf_item_add(this, &(css->item));
2848 * There may not be a name2
2850 css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2853 * The current section is now the child section.
2859 ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2864 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2865 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2872 * Done parsing one thing. Skip to EOL if possible.
2874 while (isspace(*ptr)) ptr++;
2876 if (*ptr == '#') continue;
2885 * See if EOF was unexpected ..
2887 if (feof(fp) && (this != current)) {
2888 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2889 filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2897 * Include one config file in another.
2899 static int cf_file_include(CONF_SECTION *cs, char const *filename_in)
2903 char const *filename;
2906 * So we only need to do this once.
2908 filename = talloc_strdup(cs, filename_in);
2910 DEBUG2("including configuration file %s", filename);
2912 fp = cf_file_open(cs, filename);
2915 if (!cs->item.filename) cs->item.filename = filename;
2918 * Read the section. It's OK to have EOF without a
2919 * matching close brace.
2921 if (cf_section_read(filename, &lineno, fp, cs) < 0) {
2932 * Do variable expansion in pass2.
2934 * This is a breadth-first expansion. "deep
2936 static int cf_section_pass2(CONF_SECTION *cs)
2940 for (ci = cs->children; ci; ci = ci->next) {
2945 if (ci->type != CONF_ITEM_PAIR) continue;
2947 cp = cf_item_to_pair(ci);
2948 if (!cp->value || !cp->pass2) continue;
2950 rad_assert((cp->rhs_type == T_BARE_WORD) ||
2951 (cp->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2952 (cp->rhs_type == T_BACK_QUOTED_STRING));
2954 value = cf_expand_variables(ci->filename, &ci->lineno, cs, buffer, sizeof(buffer), cp->value, NULL);
2955 if (!value) return -1;
2957 rad_const_free(cp->value);
2958 cp->value = talloc_typed_strdup(cp, value);
2961 for (ci = cs->children; ci; ci = ci->next) {
2962 if (ci->type != CONF_ITEM_SECTION) continue;
2964 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
2972 * Bootstrap a config file.
2974 int cf_file_read(CONF_SECTION *cs, char const *filename)
2980 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
2983 p = strrchr(cp->value, FR_DIR_SEP);
2986 cp->item.filename = "<internal>";
2987 cp->item.lineno = -1;
2988 cf_item_add(cs, &(cp->item));
2990 tree = rbtree_create(cs, filename_cmp, NULL, 0);
2991 if (!tree) return -1;
2993 cf_data_add_internal(cs, "filename", tree, NULL, 0);
2995 if (cf_file_include(cs, filename) < 0) return -1;
2998 * Now that we've read the file, go back through it and
2999 * expand the variables.
3001 if (cf_section_pass2(cs) < 0) return -1;
3007 void cf_file_free(CONF_SECTION *cs)
3014 * Return a CONF_PAIR within a CONF_SECTION.
3016 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
3018 CONF_PAIR *cp, mycp;
3020 if (!cs || !name) return NULL;
3023 cp = rbtree_finddata(cs->pair_tree, &mycp);
3026 if (!cs->template) return NULL;
3028 return rbtree_finddata(cs->template->pair_tree, &mycp);
3032 * Return the attr of a CONF_PAIR
3035 char const *cf_pair_attr(CONF_PAIR const *pair)
3037 return (pair ? pair->attr : NULL);
3041 * Return the value of a CONF_PAIR
3044 char const *cf_pair_value(CONF_PAIR const *pair)
3046 return (pair ? pair->value : NULL);
3049 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
3051 return (pair ? pair->op : T_INVALID);
3054 /** Return the value (lhs) type
3056 * @param pair to extract value type from.
3057 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3058 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3060 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
3062 return (pair ? pair->lhs_type : T_INVALID);
3065 /** Return the value (rhs) type
3067 * @param pair to extract value type from.
3068 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3069 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3071 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
3073 return (pair ? pair->rhs_type : T_INVALID);
3077 * Turn a CONF_PAIR into a VALUE_PAIR
3078 * For now, ignore the "value_type" field...
3080 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
3083 fr_strerror_printf("Internal error");
3088 fr_strerror_printf("No value given for attribute %s", pair->attr);
3093 * false comparisons never match. BUT if it's a "string"
3094 * or `string`, then remember to expand it later.
3096 if ((pair->op != T_OP_CMP_FALSE) &&
3097 ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
3098 (pair->rhs_type == T_BACK_QUOTED_STRING))) {
3101 vp = pairmake(pair, NULL, pair->attr, NULL, pair->op);
3106 if (pairmark_xlat(vp, pair->value) < 0) {
3115 return pairmake(pair, NULL, pair->attr, pair->value, pair->op);
3119 * Return the first label of a CONF_SECTION
3122 char const *cf_section_name1(CONF_SECTION const *cs)
3124 return (cs ? cs->name1 : NULL);
3128 * Return the second label of a CONF_SECTION
3131 char const *cf_section_name2(CONF_SECTION const *cs)
3133 return (cs ? cs->name2 : NULL);
3136 /** Return name2 if set, else name1
3139 char const *cf_section_name(CONF_SECTION const *cs)
3143 name = cf_section_name2(cs);
3144 if (name) return name;
3146 return cf_section_name1(cs);
3150 * Find a value in a CONF_SECTION
3152 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
3156 cp = cf_pair_find(cs, attr);
3158 return (cp ? cp->value : NULL);
3162 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
3163 char const *name1, char const *name2)
3166 CONF_ITEM const *ci;
3168 if (!cs || !name1) return NULL;
3170 for (ci = &(cs->item); ci; ci = ci->next) {
3171 if (ci->type != CONF_ITEM_SECTION)
3174 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
3178 their2 = cf_item_to_section(ci)->name2;
3180 if ((!name2 && !their2) ||
3181 (name2 && their2 && (strcmp(name2, their2) == 0))) {
3182 return cf_item_to_section(ci);
3189 /** Find a pair with a name matching attr, after specified pair.
3191 * @param cs to search in.
3192 * @param pair to search from (may be NULL).
3193 * @param attr to find (may be NULL in which case any attribute matches).
3194 * @return the next matching CONF_PAIR or NULL if none matched.
3196 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
3197 CONF_PAIR const *pair, char const *attr)
3201 if (!cs) return NULL;
3204 * If pair is NULL and we're trying to find a specific
3205 * attribute this must be a first time run.
3207 * Find the pair with correct name.
3209 if (!pair && attr) return cf_pair_find(cs, attr);
3212 * Start searching from the next child, or from the head
3213 * of the list of children (if no pair was provided).
3215 for (ci = pair ? pair->item.next : cs->children;
3218 if (ci->type != CONF_ITEM_PAIR) continue;
3220 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
3223 return cf_item_to_pair(ci);
3227 * Find a CONF_SECTION, or return the root if name is NULL
3230 CONF_SECTION *cf_section_find(char const *name)
3233 return cf_section_sub_find(root_config, name);
3238 /** Find a sub-section in a section
3240 * This finds ANY section having the same first name.
3241 * The second name is ignored.
3243 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
3247 if (!cs || !name) return NULL; /* can't find an un-named section */
3250 * No sub-sections have been defined, so none exist.
3252 if (!cs->section_tree) return NULL;
3256 return rbtree_finddata(cs->section_tree, &mycs);
3260 /** Find a CONF_SECTION with both names.
3263 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
3264 char const *name1, char const *name2)
3268 if (!cs) cs = root_config;
3269 if (!cs) return NULL;
3272 CONF_SECTION mycs, *master_cs;
3274 if (!cs->section_tree) return NULL;
3279 master_cs = rbtree_finddata(cs->section_tree, &mycs);
3280 if (!master_cs) return NULL;
3283 * Look it up in the name2 tree. If it's there,
3286 if (master_cs->name2_tree) {
3287 CONF_SECTION *subcs;
3289 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
3290 if (subcs) return subcs;
3294 * We don't insert ourselves into the name2 tree.
3295 * So if there's nothing in the name2 tree, maybe
3296 * *we* are the answer.
3298 if (!master_cs->name2 && name2) return NULL;
3299 if (master_cs->name2 && !name2) return NULL;
3300 if (!master_cs->name2 && !name2) return master_cs;
3302 if (strcmp(master_cs->name2, name2) == 0) {
3310 * Else do it the old-fashioned way.
3312 for (ci = cs->children; ci; ci = ci->next) {
3313 CONF_SECTION *subcs;
3315 if (ci->type != CONF_ITEM_SECTION)
3318 subcs = cf_item_to_section(ci);
3319 if (!subcs->name2) {
3320 if (strcmp(subcs->name1, name2) == 0) break;
3322 if (strcmp(subcs->name2, name2) == 0) break;
3326 return cf_item_to_section(ci);
3330 * Return the next subsection after a CONF_SECTION
3331 * with a certain name1 (char *name1). If the requested
3332 * name1 is NULL, any name1 matches.
3335 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
3336 CONF_SECTION const *subsection,
3341 if (!section) return NULL;
3344 * If subsection is NULL this must be a first time run
3345 * Find the subsection with correct name
3349 ci = section->children;
3351 ci = subsection->item.next;
3354 for (; ci; ci = ci->next) {
3355 if (ci->type != CONF_ITEM_SECTION)
3357 if ((name1 == NULL) ||
3358 (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3362 return cf_item_to_section(ci);
3367 * Return the next section 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_section_find_next(CONF_SECTION const *section,
3373 CONF_SECTION const *subsection,
3376 if (!section) return NULL;
3378 if (!section->item.parent) return NULL;
3380 return cf_subsection_find_next(section->item.parent, subsection, name1);
3383 /** Return the next item after a CONF_ITEM.
3386 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3388 if (!section) return NULL;
3391 * If item is NULL this must be a first time run
3392 * Return the first item
3395 return section->children;
3401 static void _pair_count(int *count, CONF_SECTION const *cs)
3403 CONF_ITEM const *ci;
3405 for (ci = cf_item_find_next(cs, NULL);
3407 ci = cf_item_find_next(cs, ci)) {
3409 if (cf_item_is_section(ci)) {
3410 _pair_count(count, cf_item_to_section(ci));
3418 /** Count the number of conf pairs beneath a section
3420 * @param[in] cs to search for items in.
3421 * @return number of pairs nested within section.
3423 int cf_pair_count(CONF_SECTION const *cs)
3427 _pair_count(&count, cs);
3432 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3434 if (!ci) return NULL;
3439 int cf_section_lineno(CONF_SECTION const *section)
3441 return section->item.lineno;
3444 char const *cf_pair_filename(CONF_PAIR const *pair)
3446 return pair->item.filename;
3449 char const *cf_section_filename(CONF_SECTION const *section)
3451 return section->item.filename;
3454 int cf_pair_lineno(CONF_PAIR const *pair)
3456 return pair->item.lineno;
3459 bool cf_item_is_section(CONF_ITEM const *item)
3461 return item->type == CONF_ITEM_SECTION;
3464 bool cf_item_is_pair(CONF_ITEM const *item)
3466 return item->type == CONF_ITEM_PAIR;
3470 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3471 void *data, void (*data_free)(void *))
3475 cd = talloc_zero(parent, CONF_DATA);
3476 if (!cd) return NULL;
3478 cd->item.type = CONF_ITEM_DATA;
3479 cd->item.parent = parent;
3480 cd->name = talloc_typed_strdup(cd, name);
3487 cd->free = data_free;
3490 talloc_set_destructor(cd, _cf_data_free);
3496 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3498 if (!cs || !name) return NULL;
3501 * Find the name in the tree, for speed.
3503 if (cs->data_tree) {
3508 return rbtree_finddata(cs->data_tree, &mycd);
3515 * Find data from a particular section.
3517 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3519 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3521 if (cd) return cd->data;
3527 * Add named data to a configuration section.
3529 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3530 void *data, void (*data_free)(void *),
3535 if (!cs || !name) return -1;
3538 * Already exists. Can't add it.
3540 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3542 cd = cf_data_alloc(cs, name, data, data_free);
3546 cf_item_add(cs, cf_data_to_item(cd));
3552 * Add named data to a configuration section.
3554 int cf_data_add(CONF_SECTION *cs, char const *name,
3555 void *data, void (*data_free)(void *))
3557 return cf_data_add_internal(cs, name, data, data_free, 0);
3560 /** Remove named data from a configuration section
3563 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3569 if (!cs || !name) return NULL;
3570 if (!cs->data_tree) return NULL;
3573 * Find the name in the tree, for speed.
3577 cd = rbtree_finddata(cs->data_tree, &mycd);
3578 if (!cd) return NULL;
3580 talloc_set_destructor(cd, NULL); /* Disarm the destructor */
3581 rbtree_deletebydata(cs->data_tree, &mycd);
3590 * This is here to make the rest of the code easier to read. It
3591 * ties conffile.c to log.c, but it means we don't have to
3592 * pollute every other function with the knowledge of the
3593 * configuration internals.
3595 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3601 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3606 ci->filename ? ci->filename : "unknown",
3607 ci->lineno ? ci->lineno : 0,
3610 ERROR("<unknown>[*]: %s", buffer);
3614 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3620 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3623 rad_assert(cs != NULL);
3626 cs->item.filename ? cs->item.filename : "unknown",
3627 cs->item.lineno ? cs->item.lineno : 0,
3631 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3637 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3640 rad_assert(cp != NULL);
3643 cp->item.filename ? cp->item.filename : "unknown",
3644 cp->item.lineno ? cp->item.lineno : 0,
3648 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3653 if ((rad_debug_lvl > 1) && cs) vradlog(L_DBG, fmt, ap);
3658 * Wrapper to simplify the code.
3660 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3666 if (rad_debug_lvl > 1 && cs) {
3667 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3669 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3674 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3676 if (!cs) return NULL;
3678 return cs->variables;
3682 * For "switch" and "case" statements.
3684 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3686 if (!cs) return T_INVALID;
3688 return cs->name2_type;