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.
85 /** Internal data that is associated with a configuration section
92 void *data; //!< User data
93 void (*free)(void *); //!< Free user data function
103 CONF_ITEM *tail; //!< For speed.
104 CONF_SECTION *template;
106 rbtree_t *pair_tree; //!< and a partridge..
107 rbtree_t *section_tree; //!< no jokes here.
108 rbtree_t *name2_tree; //!< for sections of the same name2
114 CONF_PARSER const *variables;
117 CONF_SECTION *root_config = NULL;
118 bool cf_new_escape = false;
121 static int cf_data_add_internal(CONF_SECTION *cs, char const *name, void *data,
122 void (*data_free)(void *), int flag);
124 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag);
126 static char const *cf_expand_variables(char const *cf, int *lineno,
127 CONF_SECTION *outercs,
128 char *output, size_t outsize,
132 * Isolate the scary casts in these tiny provably-safe functions
135 /** Cast a CONF_ITEM to a CONF_PAIR
138 CONF_PAIR *cf_item_to_pair(CONF_ITEM const *ci)
142 if (ci == NULL) return NULL;
144 rad_assert(ci->type == CONF_ITEM_PAIR);
146 memcpy(&out, &ci, sizeof(out));
150 /** Cast a CONF_ITEM to a CONF_SECTION
153 CONF_SECTION *cf_item_to_section(CONF_ITEM const *ci)
157 if (ci == NULL) return NULL;
159 rad_assert(ci->type == CONF_ITEM_SECTION);
161 memcpy(&out, &ci, sizeof(out));
165 /** Cast a CONF_PAIR to a CONF_ITEM
168 CONF_ITEM *cf_pair_to_item(CONF_PAIR const *cp)
172 if (cp == NULL) return NULL;
174 memcpy(&out, &cp, sizeof(out));
178 /** Cast a CONF_SECTION to a CONF_ITEM
181 CONF_ITEM *cf_section_to_item(CONF_SECTION const *cs)
185 if (cs == NULL) return NULL;
187 memcpy(&out, &cs, sizeof(out));
191 /** Cast CONF_DATA to a CONF_ITEM
194 static CONF_ITEM *cf_data_to_item(CONF_DATA const *cd)
202 memcpy(&out, &cd, sizeof(out));
206 static int _cf_data_free(CONF_DATA *cd)
208 if (cd->free) cd->free(cd->data);
214 * rbtree callback function
216 static int pair_cmp(void const *a, void const *b)
218 CONF_PAIR const *one = a;
219 CONF_PAIR const *two = b;
221 return strcmp(one->attr, two->attr);
226 * rbtree callback function
228 static int section_cmp(void const *a, void const *b)
230 CONF_SECTION const *one = a;
231 CONF_SECTION const *two = b;
233 return strcmp(one->name1, two->name1);
238 * rbtree callback function
240 static int name2_cmp(void const *a, void const *b)
242 CONF_SECTION const *one = a;
243 CONF_SECTION const *two = b;
245 rad_assert(strcmp(one->name1, two->name1) == 0);
247 if (!one->name2 && !two->name2) return 0;
248 if (one->name2 && !two->name2) return -1;
249 if (!one->name2 && two->name2) return +1;
251 return strcmp(one->name2, two->name2);
256 * rbtree callback function
258 static int data_cmp(void const *a, void const *b)
262 CONF_DATA const *one = a;
263 CONF_DATA const *two = b;
265 rcode = one->flag - two->flag;
266 if (rcode != 0) return rcode;
268 return strcmp(one->name, two->name);
271 static int _cf_section_free(CONF_SECTION *cs)
274 * Name1 and name2 are allocated contiguous with
278 rbtree_free(cs->pair_tree);
279 cs->pair_tree = NULL;
281 if (cs->section_tree) {
282 rbtree_free(cs->section_tree);
283 cs->section_tree = NULL;
285 if (cs->name2_tree) {
286 rbtree_free(cs->name2_tree);
287 cs->name2_tree = NULL;
290 rbtree_free(cs->data_tree);
291 cs->data_tree = NULL;
297 /** Allocate a CONF_PAIR
299 * @param parent CONF_SECTION to hang this CONF_PAIR off of.
301 * @param value of CONF_PAIR.
302 * @param op T_OP_EQ, T_OP_SET etc.
303 * @param lhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
304 * @param rhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
305 * @return NULL on error, else a new CONF_SECTION parented by parent.
307 CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value,
308 FR_TOKEN op, FR_TOKEN lhs_type, FR_TOKEN rhs_type)
312 rad_assert(fr_equality_op[op] || fr_assignment_op[op]);
313 if (!attr) return NULL;
315 cp = talloc_zero(parent, CONF_PAIR);
316 if (!cp) return NULL;
318 cp->item.type = CONF_ITEM_PAIR;
319 cp->item.parent = parent;
320 cp->lhs_type = lhs_type;
321 cp->rhs_type = rhs_type;
324 cp->attr = talloc_typed_strdup(cp, attr);
332 cp->value = talloc_typed_strdup(cp, value);
333 if (!cp->value) goto error;
339 /** Duplicate a CONF_PAIR
341 * @param parent to allocate new pair in.
342 * @param cp to duplicate.
343 * @return NULL on error, else a duplicate of the input pair.
345 CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp)
352 new = cf_pair_alloc(parent, cp->attr, cf_pair_value(cp),
353 cp->op, cp->lhs_type, cp->rhs_type);
355 new->item.lineno = cp->item.lineno;
356 new->item.filename = talloc_strdup(new, cp->item.filename);
362 /** Add a configuration pair to a section
364 * @param parent section to add pair to.
367 void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp)
369 cf_item_add(parent, cf_pair_to_item(cp));
372 /** Allocate a CONF_SECTION
374 * @param parent CONF_SECTION to hang this CONF_SECTION off of.
375 * @param name1 Primary name.
376 * @param name2 Secondary name.
377 * @return NULL on error, else a new CONF_SECTION parented by parent.
379 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
384 if (!name1) return NULL;
387 if (strchr(name2, '$')) {
388 name2 = cf_expand_variables(parent->item.filename,
389 &parent->item.lineno,
391 buffer, sizeof(buffer), name2);
393 ERROR("Failed expanding section name");
399 cs = talloc_zero(parent, CONF_SECTION);
400 if (!cs) return NULL;
402 cs->item.type = CONF_ITEM_SECTION;
403 cs->item.parent = parent;
405 cs->name1 = talloc_typed_strdup(cs, name1);
413 cs->name2 = talloc_typed_strdup(cs, name2);
414 if (!cs->name2) goto error;
417 cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
418 if (!cs->pair_tree) goto error;
420 talloc_set_destructor(cs, _cf_section_free);
423 * Don't create a data tree, it may not be needed.
427 * Don't create the section tree here, it may not
431 if (parent) cs->depth = parent->depth + 1;
436 /** Duplicate a configuration section
438 * @note recursively duplicates any child sections.
439 * @note does not duplicate any data associated with a section, or its child sections.
441 * @param parent section.
442 * @param cs to duplicate.
443 * @param name1 of new section.
444 * @param name2 of new section.
445 * @param copy_meta Copy additional meta data for a section (like template, base, depth and variables).
446 * @return a duplicate of the existing section, or NULL on error.
448 CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs,
449 char const *name1, char const *name2, bool copy_meta)
451 CONF_SECTION *new, *subcs;
455 new = cf_section_alloc(parent, name1, name2);
458 new->template = cs->template;
459 new->base = cs->base;
460 new->depth = cs->depth;
461 new->variables = cs->variables;
464 new->item.lineno = cs->item.lineno;
465 new->item.filename = talloc_strdup(new, cs->item.filename);
467 for (ci = cs->children; ci; ci = ci->next) {
469 case CONF_ITEM_SECTION:
470 subcs = cf_item_to_section(ci);
471 subcs = cf_section_dup(new, subcs,
472 cf_section_name1(subcs), cf_section_name2(subcs),
478 cf_section_add(new, subcs);
482 cp = cf_pair_dup(new, cf_item_to_pair(ci));
487 cf_pair_add(new, cp);
490 case CONF_ITEM_DATA: /* Skip data */
493 case CONF_ITEM_INVALID:
501 void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
503 cf_item_add(parent, &(cs->item));
506 /** Replace pair in a given section with a new pair, of the given value.
508 * @param cs to replace pair in.
509 * @param cp to replace.
510 * @param new value of cp.
511 * @return 0 on success, -1 on failure.
513 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
516 CONF_ITEM *ci, *cn, **last;
518 newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->lhs_type, cp->rhs_type);
519 if (!newp) return -1;
525 * Find the old one from the linked list, and replace it
528 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
530 cn->next = (*last)->next;
537 rbtree_deletebydata(cs->pair_tree, ci);
539 rbtree_insert(cs->pair_tree, cn);
546 * Add an item to a configuration section.
548 void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
550 CONF_ITEM *first = ci;
552 rad_assert((void *)cs != (void *)ci);
554 if (!cs || !ci) return;
557 rad_assert(cs->tail == NULL);
560 rad_assert(cs->tail != NULL);
565 * Update the trees (and tail) for each item added.
567 for (/* nothing */; ci != NULL; ci = ci->next) {
568 rad_assert(ci->next != first); /* simple cycle detection */
573 * For fast lookups, pairs and sections get
578 if (!rbtree_insert(cs->pair_tree, ci)) {
579 CONF_PAIR *cp = cf_item_to_pair(ci);
581 if (strcmp(cp->attr, "confdir") == 0) break;
582 if (!cp->value) break; /* module name, "ok", etc. */
586 case CONF_ITEM_SECTION: {
587 CONF_SECTION *cs_new = cf_item_to_section(ci);
588 CONF_SECTION *name1_cs;
590 if (!cs->section_tree) {
591 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
592 if (!cs->section_tree) {
593 ERROR("Out of memory");
598 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
600 if (!rbtree_insert(cs->section_tree, cs_new)) {
601 ERROR("Failed inserting section into tree");
608 * We already have a section of
609 * this "name1". Add a new
610 * sub-section based on name2.
612 if (!name1_cs->name2_tree) {
613 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
614 if (!name1_cs->name2_tree) {
615 ERROR("Out of memory");
621 * We don't care if this fails.
622 * If the user tries to create
623 * two sections of the same
624 * name1/name2, the duplicate
625 * section is just silently
628 rbtree_insert(name1_cs->name2_tree, cs_new);
630 } /* was a section */
633 if (!cs->data_tree) {
634 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
637 rbtree_insert(cs->data_tree, ci);
641 default: /* FIXME: assert & error! */
644 } /* switch over conf types */
649 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
650 CONF_SECTION *outercs,
655 CONF_SECTION const *cs = outercs;
659 strlcpy(name, ptr, sizeof(name));
663 * ".foo" means "foo from the current section"
669 * Just '.' means the current section
672 return cf_section_to_item(cs);
676 * ..foo means "foo from the section
677 * enclosing this section" (etc.)
680 if (cs->item.parent) {
681 cs = cs->item.parent;
685 * .. means the section
686 * enclosing this section
689 return cf_section_to_item(cs);
694 * "foo.bar.baz" means "from the root"
696 } else if (strchr(p, '.') != NULL) {
697 if (!parentcs) goto no_such_item;
709 if (r && q > r) q = NULL;
710 if (q && q < r) r = NULL;
716 q = strchr(r + 1, ']');
717 if (!q) return NULL; /* parse error */
720 * Points to foo[bar]xx: parse error,
721 * it should be foo[bar] or foo[bar].baz
723 if (q[1] && q[1] != '.') goto no_such_item;
727 next = cf_section_sub_find_name2(cs, p, r + 1);
732 * Points to a named instance of a section.
735 if (!next) goto no_such_item;
736 return &(next->item);
739 q++; /* ensure we skip the ']' and '.' */
743 next = cf_section_sub_find(cs, p);
747 if (!next) break; /* it MAY be a pair in this section! */
753 if (!*p) goto no_such_item;
757 * Find it in the current referenced
760 cp = cf_pair_find(cs, p);
761 if (cp) return &(cp->item);
763 next = cf_section_sub_find(cs, p);
764 if (next) return &(next->item);
767 * "foo" is "in the current section, OR in main".
769 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
775 WARN("No such configuration item %s", ptr);
780 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
782 if (!cs) return NULL;
784 while (cs->item.parent != NULL) {
785 cs = cs->item.parent;
793 * Expand the variables in an input string.
795 static char const *cf_expand_variables(char const *cf, int *lineno,
796 CONF_SECTION *outercs,
797 char *output, size_t outsize,
801 char const *end, *ptr;
802 CONF_SECTION const *parentcs;
806 * Find the master parent conf section.
807 * We can't use main_config.config, because we're in the
808 * process of re-building it, and it isn't set up yet...
810 parentcs = cf_top_section(outercs);
816 * Ignore anything other than "${"
818 if ((*ptr == '$') && (ptr[1] == '{')) {
824 * FIXME: Add support for ${foo:-bar},
829 * Look for trailing '}', and log a
830 * warning for anything that doesn't match,
831 * and exit with a fatal error.
833 end = strchr(ptr, '}');
836 INFO("%s[%d]: Variable expansion missing }",
844 * Can't really happen because input lines are
845 * capped at 8k, which is sizeof(name)
847 if ((size_t) (end - ptr) >= sizeof(name)) {
848 ERROR("%s[%d]: Reference string is too large",
853 memcpy(name, ptr, end - ptr);
854 name[end - ptr] = '\0';
856 q = strchr(name, ':');
861 ci = cf_reference_item(parentcs, outercs, name);
863 ERROR("%s[%d]: Reference \"%s\" not found", cf, *lineno, input);
868 * The expansion doesn't refer to another item or section
869 * it's the property of a section.
872 CONF_SECTION *mycs = cf_item_to_section(ci);
874 if (ci->type != CONF_ITEM_SECTION) {
875 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
879 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
880 case CONF_PROPERTY_NAME:
881 strcpy(p, mycs->name1);
884 case CONF_PROPERTY_INSTANCE:
885 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
889 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
895 } else if (ci->type == CONF_ITEM_PAIR) {
897 * Substitute the value of the variable.
899 cp = cf_item_to_pair(ci);
901 ERROR("%s[%d]: Reference \"%s\" has no value",
906 if (p + strlen(cp->value) >= output + outsize) {
907 ERROR("%s[%d]: Reference \"%s\" is too long",
912 strcpy(p, cp->value);
916 } else if (ci->type == CONF_ITEM_SECTION) {
920 * Adding an entry again to a
921 * section is wrong. We don't
922 * want an infinite loop.
924 if (ci->parent == outercs) {
925 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
930 * Copy the section instead of
933 subcs = cf_item_to_section(ci);
934 subcs = cf_section_dup(outercs, subcs,
935 cf_section_name1(subcs), cf_section_name2(subcs),
938 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
942 subcs->item.filename = ci->filename;
943 subcs->item.lineno = ci->lineno;
944 cf_item_add(outercs, &(subcs->item));
949 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
952 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
958 * Look for trailing '}', and log a
959 * warning for anything that doesn't match,
960 * and exit with a fatal error.
962 end = strchr(ptr, '}');
965 INFO("%s[%d]: Environment variable expansion missing }",
971 * Can't really happen because input lines are
972 * capped at 8k, which is sizeof(name)
974 if ((size_t) (end - ptr) >= sizeof(name)) {
975 ERROR("%s[%d]: Environment variable name is too large",
980 memcpy(name, ptr, end - ptr);
981 name[end - ptr] = '\0';
984 * Get the environment variable.
985 * If none exists, then make it an empty string.
993 if (p + strlen(env) >= output + outsize) {
994 ERROR("%s[%d]: Reference \"%s\" is too long",
1005 * Copy it over verbatim.
1011 if (p >= (output + outsize)) {
1012 ERROR("%s[%d]: Reference \"%s\" is too long",
1013 cf, *lineno, input);
1016 } /* loop over all of the input string. */
1023 static char const parse_spaces[] = " ";
1025 /** Validation function for ipaddr conffile types
1028 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1029 fr_ipaddr_t *ipaddr)
1033 if (strcmp(value, "*") == 0) {
1034 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1035 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1036 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1038 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1039 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1043 case PW_TYPE_IPV4_ADDR:
1044 case PW_TYPE_IPV6_ADDR:
1045 case PW_TYPE_COMBO_IP_ADDR:
1046 switch (ipaddr->af) {
1048 if (ipaddr->prefix != 32) {
1049 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
1057 if (ipaddr->prefix != 128) {
1058 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
1074 * Parses an item (not a CONF_ITEM) into the specified format,
1075 * with a default value.
1077 * Returns -1 on error, -2 if deprecated, 0 for correctly parsed,
1078 * and 1 if the default value was used. Note that the default
1079 * value will be used ONLY if the CONF_PAIR is NULL.
1081 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1084 bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, xlat;
1087 CONF_PAIR const *cp = NULL;
1088 fr_ipaddr_t *ipaddr;
1093 deprecated = (type & PW_TYPE_DEPRECATED);
1094 required = (type & PW_TYPE_REQUIRED);
1095 attribute = (type & PW_TYPE_ATTRIBUTE);
1096 secret = (type & PW_TYPE_SECRET);
1097 file_input = (type == PW_TYPE_FILE_INPUT); /* check, not and */
1098 cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1099 tmpl = (type & PW_TYPE_TMPL);
1100 xlat = (type & PW_TYPE_XLAT);
1102 if (attribute) required = true;
1103 if (required) cant_be_empty = true; /* May want to review this in the future... */
1105 type &= 0xff; /* normal types are small */
1108 cp = cf_pair_find(cs, name);
1120 cf_log_err(&(cs->item), "Configuration item '%s' must have a value", name);
1122 cf_log_err(&(cp->item), "Configuration item '%s' must have a value", name);
1129 if ((value[0] == '\0') && cant_be_empty) {
1132 cf_log_err(&(cs->item), "Configuration item '%s' must not be empty (zero length)", name);
1133 if (!required) cf_log_err(&(cs->item), "Comment item to silence this message");
1135 cf_log_err(&(cp->item), "Configuration item '%s' must not be empty (zero length)", name);
1136 if (!required) cf_log_err(&(cp->item), "Comment item to silence this message");
1142 cf_log_err(&(cs->item), "Configuration item \"%s\" is deprecated", name);
1152 value_pair_tmpl_t *vpt;
1154 if (!value || (cf_pair_value_type(cp) == T_INVALID)) {
1155 *(value_pair_tmpl_t **)data = NULL;
1159 slen = tmpl_afrom_str(cs, &vpt, value, strlen(value),
1160 cf_pair_value_type(cp), REQUEST_CURRENT, PAIR_LIST_REQUEST);
1162 char *spaces, *text;
1164 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1166 cf_log_err_cs(cs, "Failed parsing configuration item '%s'", name);
1167 cf_log_err_cs(cs, "%s", value);
1168 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1170 talloc_free(spaces);
1179 * Don't add default - update with new types.
1181 switch (vpt->type) {
1182 case TMPL_TYPE_LITERAL:
1183 case TMPL_TYPE_ATTR:
1184 case TMPL_TYPE_ATTR_UNDEFINED:
1185 case TMPL_TYPE_LIST:
1186 case TMPL_TYPE_DATA:
1187 case TMPL_TYPE_EXEC:
1188 case TMPL_TYPE_XLAT:
1189 case TMPL_TYPE_XLAT_STRUCT:
1192 case TMPL_TYPE_UNKNOWN:
1193 case TMPL_TYPE_REGEX:
1194 case TMPL_TYPE_REGEX_STRUCT:
1195 case TMPL_TYPE_NULL:
1200 * If the attribute flag is set, the template must be an
1201 * attribute reference.
1203 if (attribute && (vpt->type != TMPL_TYPE_ATTR)) {
1204 cf_log_err(&(cs->item), "Configuration item '%s' must be an attr "
1205 "but is an %s", name, fr_int2str(tmpl_names, vpt->type, "<INVALID>"));
1211 * If the xlat flag is set, the template must be an xlat
1213 if (xlat && (vpt->type != TMPL_TYPE_XLAT_STRUCT)) {
1214 cf_log_err(&(cs->item), "Configuration item '%s' must be an xlat expansion but is an %s",
1215 name, fr_int2str(tmpl_names, vpt->type, "<INVALID>"));
1221 * If we have a type, and the template is an attribute reference
1222 * check that the attribute reference matches the type.
1224 if ((type > 0) && (vpt->type == TMPL_TYPE_ATTR) && (vpt->tmpl_da->type != type)) {
1225 cf_log_err(&(cs->item), "Configuration item '%s' attr must be an %s, but is an %s",
1226 name, fr_int2str(dict_attr_types, type, "<INVALID>"),
1227 fr_int2str(dict_attr_types, vpt->tmpl_da->type, "<INVALID>"));
1231 *(value_pair_tmpl_t **)data = vpt;
1237 case PW_TYPE_BOOLEAN:
1239 * Allow yes/no and on/off
1241 if ((strcasecmp(value, "yes") == 0) ||
1242 (strcasecmp(value, "on") == 0)) {
1243 *(bool *)data = true;
1244 } else if ((strcasecmp(value, "no") == 0) ||
1245 (strcasecmp(value, "off") == 0)) {
1246 *(bool *)data = false;
1248 *(bool *)data = false;
1249 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1250 "variable %s", value, name);
1253 cf_log_info(cs, "%.*s\t%s = %s",
1254 cs->depth, parse_spaces, name, value);
1257 case PW_TYPE_INTEGER:
1259 unsigned long v = strtoul(value, 0, 0);
1262 * Restrict integer values to 0-INT32_MAX, this means
1263 * it will always be safe to cast them to a signed type
1264 * for comparisons, and imposes the same range limit as
1265 * before we switched to using an unsigned type to
1266 * represent config item integers.
1268 if (v > INT32_MAX) {
1269 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1274 *(uint32_t *)data = v;
1275 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1281 unsigned long v = strtoul(value, 0, 0);
1283 if (v > UINT16_MAX) {
1284 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1288 *(uint16_t *)data = (uint16_t) v;
1289 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1293 case PW_TYPE_INTEGER64:
1294 *(uint64_t *)data = strtoull(value, 0, 0);
1295 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1298 case PW_TYPE_SIGNED:
1299 *(int32_t *)data = strtol(value, 0, 0);
1300 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1303 case PW_TYPE_STRING:
1310 * Expand variables which haven't already been
1311 * expanded automagically when the configuration
1314 if (value == dflt) {
1317 lineno = cs->item.lineno;
1319 value = cf_expand_variables("<internal>",
1321 cs, buffer, sizeof(buffer),
1324 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1329 if (required && !value) goto is_required;
1330 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1333 if (!dict_attrbyname(value)) {
1335 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1338 cf_log_err(&(cp->item), "No such attribute '%s'", value);
1345 * Hide secrets when using "radiusd -X".
1347 if (secret && (debug_flag <= 2)) {
1348 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1349 cs->depth, parse_spaces, name);
1351 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1352 cs->depth, parse_spaces, name, value ? value : "(null)");
1354 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1357 * If there's data AND it's an input file, check
1358 * that we can read it. This check allows errors
1359 * to be caught as early as possible, during
1362 if (*q && file_input) {
1365 if (stat(*q, &buf) < 0) {
1366 char user[255], group[255];
1368 ERROR("Unable to open file \"%s\": %s", value, fr_syserror(errno));
1369 ERROR("Our effective user and group was %s:%s",
1370 (rad_prints_uid(NULL, user, sizeof(user), geteuid()) < 0) ?
1372 (rad_prints_gid(NULL, group, sizeof(group), getegid()) < 0) ?
1373 "unknown" : group );
1380 case PW_TYPE_IPV4_ADDR:
1381 case PW_TYPE_IPV4_PREFIX:
1384 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1385 ERROR("%s", fr_strerror());
1388 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1391 case PW_TYPE_IPV6_ADDR:
1392 case PW_TYPE_IPV6_PREFIX:
1395 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1396 ERROR("%s", fr_strerror());
1399 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1402 case PW_TYPE_COMBO_IP_ADDR:
1403 case PW_TYPE_COMBO_IP_PREFIX:
1406 if (fr_pton(ipaddr, value, -1, true) < 0) {
1407 ERROR("%s", fr_strerror());
1410 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1413 case PW_TYPE_TIMEVAL: {
1418 sec = strtoul(value, &end, 10);
1424 len = strlen(end + 1);
1427 ERROR("Too much precision for timeval");
1432 * If they write "0.1", that means
1433 * "10000" microseconds.
1435 sec = strtoul(end + 1, NULL, 10);
1443 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1444 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1445 memcpy(data, &tv, sizeof(tv));
1451 * If we get here, it's a sanity check error.
1452 * It's not an error parsing the configuration
1455 rad_assert(type > PW_TYPE_INVALID);
1456 rad_assert(type < PW_TYPE_MAX);
1458 ERROR("type '%s' is not supported in the configuration files",
1459 fr_int2str(dict_attr_types, type, "?Unknown?"));
1461 } /* switch over variable type */
1466 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1467 if (!cpn) return -1;
1468 cpn->item.filename = "<internal>";
1469 cpn->item.lineno = 0;
1470 cf_item_add(cs, &(cpn->item));
1478 * A copy of cf_section_parse that initializes pointers before
1481 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1482 CONF_PARSER const *variables)
1486 for (i = 0; variables[i].name != NULL; i++) {
1487 if (variables[i].type == PW_TYPE_SUBSECTION) {
1488 CONF_SECTION *subcs;
1490 if (!variables[i].dflt) continue;
1492 subcs = cf_section_sub_find(cs, variables[i].name);
1495 * If there's no subsection in the
1496 * config, BUT the CONF_PARSER wants one,
1497 * then create an empty one. This is so
1498 * that we can track the strings,
1499 * etc. allocated in the subsection.
1502 subcs = cf_section_alloc(cs, variables[i].name,
1506 subcs->item.filename = cs->item.filename;
1507 subcs->item.lineno = cs->item.lineno;
1508 cf_item_add(cs, &(subcs->item));
1511 cf_section_parse_init(subcs, base,
1512 (CONF_PARSER const *) variables[i].dflt);
1516 if ((variables[i].type != PW_TYPE_STRING) &&
1517 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1518 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1522 if (variables[i].data) {
1523 *(char **) variables[i].data = NULL;
1525 *(char **) (((char *)base) + variables[i].offset) = NULL;
1529 } /* for all variables in the configuration section */
1534 * Parse a configuration section into user-supplied variables.
1536 int cf_section_parse(CONF_SECTION *cs, void *base,
1537 CONF_PARSER const *variables)
1543 cs->variables = variables; /* this doesn't hurt anything */
1546 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1549 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1550 cs->name1, cs->name2);
1553 cf_section_parse_init(cs, base, variables);
1556 * Handle the known configuration parameters.
1558 for (i = 0; variables[i].name != NULL; i++) {
1560 * Handle subsections specially
1562 if (variables[i].type == PW_TYPE_SUBSECTION) {
1563 CONF_SECTION *subcs;
1564 subcs = cf_section_sub_find(cs, variables[i].name);
1566 if (!variables[i].dflt || !subcs) {
1567 DEBUG2("Internal sanity check 1 failed in cf_section_parse %s",
1572 if (cf_section_parse(subcs, base,
1573 (CONF_PARSER const *) variables[i].dflt) < 0) {
1577 } /* else it's a CONF_PAIR */
1579 if (variables[i].data) {
1580 data = variables[i].data; /* prefer this. */
1582 data = ((char *)base) + variables[i].offset;
1584 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1589 * Parse the pair we found, or a default value.
1591 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1594 * Be nice, and print the name of the new config item.
1596 if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1597 (variables[i + 1].data == variables[i].data)) {
1598 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1599 variables[i + 1].name);
1604 } /* for all variables in the configuration section */
1606 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1613 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1619 * Check XLAT things in pass 2. But don't cache the xlat stuff anywhere.
1621 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1630 * Handle the known configuration parameters.
1632 for (i = 0; variables[i].name != NULL; i++) {
1636 * Handle subsections specially
1638 if (variables[i].type == PW_TYPE_SUBSECTION) {
1639 CONF_SECTION *subcs;
1640 subcs = cf_section_sub_find(cs, variables[i].name);
1642 if (cf_section_parse_pass2(subcs, base,
1643 (CONF_PARSER const *) variables[i].dflt) < 0) {
1647 } /* else it's a CONF_PAIR */
1649 cp = cf_pair_find(cs, variables[i].name);
1652 if (!cp || !cp->value) continue;
1654 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1655 (cp->rhs_type != T_BARE_WORD)) continue;
1658 * Non-xlat expansions shouldn't have xlat!
1660 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1661 ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1663 * Ignore %{... in shared secrets.
1664 * They're never dynamically expanded.
1666 if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
1668 if (strstr(cp->value, "%{") != NULL) {
1669 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
1670 cp->item.filename ? cp->item.filename : "unknown",
1671 cp->item.lineno ? cp->item.lineno : 0);
1677 * xlat expansions should be parseable.
1679 value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
1682 slen = xlat_tokenize(cs, value, &xlat, &error);
1684 char *spaces, *text;
1686 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
1688 cf_log_err(&cp->item, "Failed parsing expanded string:");
1689 cf_log_err(&cp->item, "%s", text);
1690 cf_log_err(&cp->item, "%s^ %s", spaces, error);
1692 talloc_free(spaces);
1703 * If the "multi" flag is set, check all of them.
1705 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
1706 cp = cf_pair_find_next(cs, cp, cp->attr);
1709 } /* for all variables in the configuration section */
1715 * Merge the template so everyting else "just works".
1717 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
1721 if (!cs || !template) return true;
1723 cs->template = NULL;
1726 * Walk over the template, adding its' entries to the
1727 * current section. But only if the entries don't
1728 * already exist in the current section.
1730 for (ci = template->children; ci; ci = ci->next) {
1731 if (ci->type == CONF_ITEM_PAIR) {
1732 CONF_PAIR *cp1, *cp2;
1735 * It exists, don't over-write it.
1737 cp1 = cf_item_to_pair(ci);
1738 if (cf_pair_find(cs, cp1->attr)) {
1743 * Create a new pair with all of the data
1746 cp2 = cf_pair_dup(cs, cp1);
1747 if (!cp2) return false;
1749 cp2->item.filename = cp1->item.filename;
1750 cp2->item.lineno = cp1->item.lineno;
1752 cf_item_add(cs, &(cp2->item));
1756 if (ci->type == CONF_ITEM_SECTION) {
1757 CONF_SECTION *subcs1, *subcs2;
1759 subcs1 = cf_item_to_section(ci);
1760 rad_assert(subcs1 != NULL);
1762 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
1765 * sub-sections get merged.
1767 if (!cf_template_merge(subcs2, subcs1)) {
1774 * Our section doesn't have a matching
1775 * sub-section. Copy it verbatim from
1778 subcs2 = cf_section_dup(cs, subcs1,
1779 cf_section_name1(subcs1), cf_section_name2(subcs1),
1781 if (!subcs2) return false;
1783 subcs2->item.filename = subcs1->item.filename;
1784 subcs2->item.lineno = subcs1->item.lineno;
1786 cf_item_add(cs, &(subcs2->item));
1790 /* ignore everything else */
1796 static char const *cf_local_file(char const *base, char const *filename,
1797 char *buffer, size_t bufsize)
1802 strlcpy(buffer, base, bufsize);
1804 p = strrchr(buffer, FR_DIR_SEP);
1805 if (!p) return filename;
1806 if (p[1]) { /* ./foo */
1810 dirsize = (p - buffer) + 1;
1812 if ((dirsize + strlen(filename)) >= bufsize) {
1816 strlcpy(p + 1, filename, bufsize - dirsize);
1823 * Read a part of the config file.
1825 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
1826 CONF_SECTION *current)
1829 CONF_SECTION *this, *css, *nextcs;
1838 FR_TOKEN t1 = T_INVALID, t2, t3;
1839 bool has_spaces = false;
1842 fr_cond_t *cond = NULL;
1844 this = current; /* add items here */
1847 * Read, checking for line continuations ('\\' at EOL)
1854 * Get data, and remember if we are at EOF.
1856 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1860 * We read the entire 8k worth of data: complain.
1861 * Note that we don't care if the last character
1862 * is \n: it's still forbidden. This means that
1863 * the maximum allowed length of text is 8k-1, which
1867 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
1868 ERROR("%s[%d]: Line too long",
1875 while (isspace((int) *ptr)) ptr++;
1878 memmove(cbuf, ptr, len - (ptr - cbuf));
1879 len -= (ptr - cbuf);
1884 * Not doing continuations: check for edge
1891 while (*ptr && isspace((int) *ptr)) ptr++;
1893 if (!*ptr || (*ptr == '#')) continue;
1895 } else if (at_eof || (len == 0)) {
1896 ERROR("%s[%d]: Continuation at EOF is illegal",
1902 * See if there's a continuation.
1905 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
1910 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1912 * Check for "suppress spaces" magic.
1914 if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
1918 cbuf[len - 1] = '\0';
1928 * The parser is getting to be evil.
1930 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
1932 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
1936 if (ptr[0] == '%') {
1937 hack = rad_copy_variable(buf1, ptr);
1939 hack = rad_copy_string(buf1, ptr);
1942 ERROR("%s[%d]: Invalid expansion: %s",
1943 filename, *lineno, ptr);
1949 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
1956 ERROR("%s[%d]: Invalid expansion: %s",
1957 filename, *lineno, ptr);
1961 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
1965 * The caller eats "name1 name2 {", and calls us
1966 * for the data inside of the section. So if we
1967 * receive a closing brace, then it must mean the
1968 * end of the section.
1970 if (t1 == T_RCBRACE) {
1971 if (this == current) {
1972 ERROR("%s[%d]: Too many closing braces",
1978 * Merge the template into the existing
1979 * section. This uses more memory, but
1980 * means that templates now work with
1981 * sub-sections, etc.
1983 if (!cf_template_merge(this, this->template)) {
1987 this = this->item.parent;
1988 goto check_for_more;
1992 * Allow for $INCLUDE files
1994 * This *SHOULD* work for any level include.
1995 * I really really really hate this file. -cparker
1997 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
1998 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
1999 bool relative = true;
2001 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2003 ERROR("%s[%d]: Unexpected text after $INCLUDE",
2008 if (buf2[0] == '$') relative = false;
2010 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2);
2011 if (!value) return -1;
2013 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2016 value = cf_local_file(filename, value, buf3,
2019 ERROR("%s[%d]: Directories too deep.",
2026 #ifdef HAVE_DIRENT_H
2030 * Include ALL non-"dot" files in the directory.
2033 if (value[strlen(value) - 1] == '/') {
2036 struct stat stat_buf;
2038 DEBUG2("including files in directory %s", value );
2043 if (stat(value, &stat_buf) < 0) {
2044 ERROR("%s[%d]: Failed reading directory %s: %s",
2046 value, fr_syserror(errno));
2050 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2051 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
2052 "insecure configuration", filename, *lineno, value);
2056 dir = opendir(value);
2058 ERROR("%s[%d]: Error reading directory %s: %s",
2059 filename, *lineno, value,
2060 fr_syserror(errno));
2065 * Read the directory, ignoring "." files.
2067 while ((dp = readdir(dir)) != NULL) {
2070 if (dp->d_name[0] == '.') continue;
2073 * Check for valid characters
2075 for (p = dp->d_name; *p != '\0'; p++) {
2076 if (isalpha((int)*p) ||
2080 (*p == '.')) continue;
2083 if (*p != '\0') continue;
2085 snprintf(buf2, sizeof(buf2), "%s%s",
2087 if ((stat(buf2, &stat_buf) != 0) ||
2088 S_ISDIR(stat_buf.st_mode)) continue;
2090 * Read the file into the current
2091 * configuration section.
2093 if (cf_file_include(this, buf2) < 0) {
2101 { /* it was a normal file */
2102 if (buf1[1] == '-') {
2103 struct stat statbuf;
2105 if (stat(value, &statbuf) < 0) {
2106 WARN("Not including file %s: %s", value, fr_syserror(errno));
2111 if (cf_file_include(this, value) < 0) {
2116 } /* we were in an include */
2118 if (strcasecmp(buf1, "$template") == 0) {
2120 CONF_SECTION *parentcs, *templatecs;
2121 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2124 ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2128 parentcs = cf_top_section(current);
2130 templatecs = cf_section_sub_find(parentcs, "templates");
2132 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2136 ci = cf_reference_item(parentcs, templatecs, buf2);
2137 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2138 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2143 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2147 if (this->template) {
2148 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2152 this->template = cf_item_to_section(ci);
2157 * Ensure that the user can't add CONF_PAIRs
2158 * with 'internal' names;
2160 if (buf1[0] == '_') {
2161 ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2166 * Handle if/elsif specially.
2168 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2170 char const *error = NULL;
2172 CONF_SECTION *server;
2175 * if / elsif MUST be inside of a
2176 * processing section, which MUST in turn
2177 * be inside of a "server" directive.
2179 if (!this->item.parent) {
2181 ERROR("%s[%d]: Invalid location for '%s'",
2182 filename, *lineno, buf1);
2187 * Skip (...) to find the {
2189 slen = fr_condition_tokenize(nextcs, cf_section_to_item(nextcs), ptr, &cond,
2190 &error, FR_COND_TWO_PASS);
2191 memcpy(&p, &ptr, sizeof(p));
2194 if (p[-slen] != '{') goto cond_error;
2200 * This hack is so that the NEXT stage
2201 * doesn't go "too far" in expanding the
2202 * variable. We can parse the conditions
2203 * without expanding the ${...} stuff.
2204 * BUT we don't want to expand all of the
2205 * stuff AFTER the condition. So we do
2208 * The first pass is to discover the end
2209 * of the condition. We then expand THAT
2210 * string, and do a second pass parsing
2211 * the expanded condition.
2217 * If there's a ${...}. If so, expand it.
2219 if (strchr(ptr, '$') != NULL) {
2220 ptr = cf_expand_variables(filename, lineno,
2225 ERROR("%s[%d]: Parse error expanding ${...} in condition",
2229 } /* else leave it alone */
2231 server = this->item.parent;
2232 while ((strcmp(server->name1, "server") != 0) &&
2233 (strcmp(server->name1, "policy") != 0) &&
2234 (strcmp(server->name1, "instantiate") != 0)) {
2235 server = server->item.parent;
2236 if (!server) goto invalid_location;
2239 nextcs = cf_section_alloc(this, buf1, ptr);
2241 ERROR("%s[%d]: Failed allocating memory for section",
2245 nextcs->item.filename = talloc_strdup(nextcs, filename);
2246 nextcs->item.lineno = *lineno;
2248 slen = fr_condition_tokenize(nextcs, cf_section_to_item(nextcs), ptr, &cond,
2249 &error, FR_COND_TWO_PASS);
2250 *p = '{'; /* put it back */
2254 char *spaces, *text;
2256 fr_canonicalize_error(nextcs, &spaces, &text, slen, ptr);
2258 ERROR("%s[%d]: Parse error in condition",
2260 ERROR("%s[%d]: %s", filename, *lineno, text);
2261 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2263 talloc_free(spaces);
2265 talloc_free(nextcs);
2269 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2270 talloc_free(nextcs);
2271 ERROR("%s[%d]: Condition is too large after \"%s\"",
2272 filename, *lineno, buf1);
2277 * Copy the expanded and parsed condition
2278 * into buf2. Then, parse the text after
2279 * the condition, which now MUST be a '{.
2281 * If it wasn't '{' it would have been
2282 * caught in the first pass of
2283 * conditional parsing, above.
2285 memcpy(buf2, ptr, slen);
2290 if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2291 talloc_free(nextcs);
2292 ERROR("%s[%d]: Expected '{' %d",
2293 filename, *lineno, t3);
2298 * Swap the condition with trailing stuff for
2299 * the final condition.
2301 memcpy(&p, &nextcs->name2, sizeof(nextcs->name2));
2303 nextcs->name2 = talloc_typed_strdup(nextcs, buf2);
2309 * Grab the next token.
2311 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2328 case T_OP_CMP_FALSE:
2329 if (!this || (strcmp(this->name1, "update") != 0)) {
2330 ERROR("%s[%d]: Invalid operator in assignment",
2338 while (isspace((int) *ptr)) ptr++;
2341 * New parser: non-quoted strings are
2342 * bare words, and we parse everything
2343 * until the next newline, or the next
2344 * comma. If they have { or } in a bare
2345 * word, well... too bad.
2347 if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2348 && (*ptr != '`') && (*ptr != '/')) {
2349 const char *q = ptr;
2352 while (*q && (*q >= ' ') && (*q != ',') &&
2355 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2356 ERROR("%s[%d]: Parse error: value too long",
2361 memcpy(buf3, ptr, (q - ptr));
2362 buf3[q - ptr] = '\0';
2366 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2369 if (t3 == T_INVALID) {
2370 ERROR("%s[%d]: Parse error: %s",
2377 * These are not allowed. Print a
2378 * helpful error message.
2380 if ((t3 == T_BACK_QUOTED_STRING) &&
2381 (!this || (strcmp(this->name1, "update") != 0))) {
2382 ERROR("%s[%d]: Syntax error: Invalid string `...` in assignment",
2388 * Handle variable substitution via ${foo}
2392 case T_DOUBLE_QUOTED_STRING:
2393 case T_BACK_QUOTED_STRING:
2394 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3);
2395 if (!value) return -1;
2409 * Add this CONF_PAIR to our CONF_SECTION
2412 cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2413 if (!cpn) return -1;
2414 cpn->item.filename = talloc_strdup(cpn, filename);
2415 cpn->item.lineno = *lineno;
2416 cf_item_add(this, &(cpn->item));
2419 * Hacks for escaping
2421 if (!cf_new_escape && !this->item.parent && value &&
2422 (strcmp(buf1, "correct_escapes") == 0) &&
2423 ((strcmp(value, "true") == 0) ||
2424 (strcmp(value, "yes") == 0) ||
2425 (strcmp(value, "1") == 0))) {
2426 cf_new_escape = true;
2430 * Require a comma, unless there's a comment.
2432 while (isspace(*ptr)) ptr++;
2441 * foo = bar # other stuff
2443 if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2445 if (!*ptr || (*ptr == '}')) break;
2447 ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2448 filename, *lineno, value, ptr);
2452 * No '=', must be a section or sub-section.
2455 case T_DOUBLE_QUOTED_STRING:
2456 case T_SINGLE_QUOTED_STRING:
2457 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2458 if (t3 != T_LCBRACE) {
2459 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2460 filename, *lineno, buf1, buf2);
2468 css = cf_section_alloc(this, buf1,
2469 t2 == T_LCBRACE ? NULL : buf2);
2471 ERROR("%s[%d]: Failed allocating memory for section",
2476 css->item.filename = talloc_strdup(css, filename);
2477 css->item.lineno = *lineno;
2478 cf_item_add(this, &(css->item));
2484 cf_item_add(this, &(css->item));
2485 cf_data_add_internal(css, "if", cond, NULL, false);
2486 cond = NULL; /* eaten by the above line */
2490 * There may not be a name2
2492 css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2495 * The current section is now the child section.
2501 ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2506 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2507 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2514 * Done parsing one thing. Skip to EOL if possible.
2516 while (isspace(*ptr)) ptr++;
2518 if (*ptr == '#') continue;
2527 * See if EOF was unexpected ..
2529 if (feof(fp) && (this != current)) {
2530 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2531 filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2539 * Include one config file in another.
2541 int cf_file_include(CONF_SECTION *cs, char const *filename)
2545 struct stat statbuf;
2549 DEBUG2("including configuration file %s", filename);
2551 fp = fopen(filename, "r");
2553 ERROR("Unable to open file \"%s\": %s",
2554 filename, fr_syserror(errno));
2558 if (stat(filename, &statbuf) == 0) {
2560 if ((statbuf.st_mode & S_IWOTH) != 0) {
2562 ERROR("Configuration file %s is globally writable. "
2563 "Refusing to start due to insecure configuration.", filename);
2568 #if 0 && defined(S_IROTH)
2569 if (statbuf.st_mode & S_IROTH) != 0) {
2571 ERROR("Configuration file %s is globally readable. "
2572 "Refusing to start due to insecure configuration", filename);
2578 if (cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT)) {
2580 ERROR("Cannot include the same file twice: \"%s\"", filename);
2586 * Add the filename to the section
2588 mtime = talloc(cs, time_t);
2589 *mtime = statbuf.st_mtime;
2591 if (cf_data_add_internal(cs, filename, mtime, NULL, PW_TYPE_FILE_INPUT) < 0) {
2593 ERROR("Internal error opening file \"%s\"",
2598 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT);
2601 ERROR("Internal error opening file \"%s\"",
2606 if (!cs->item.filename) cs->item.filename = talloc_strdup(cs, filename);
2609 * Read the section. It's OK to have EOF without a
2610 * matching close brace.
2612 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
2622 * Bootstrap a config file.
2624 int cf_file_read(CONF_SECTION *cs, char const *filename)
2629 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
2632 p = strrchr(cp->value, FR_DIR_SEP);
2635 cp->item.filename = "internal";
2636 cp->item.lineno = -1;
2637 cf_item_add(cs, &(cp->item));
2639 if (cf_file_include(cs, filename) < 0) return -1;
2645 void cf_file_free(CONF_SECTION *cs)
2652 * Return a CONF_PAIR within a CONF_SECTION.
2654 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
2656 CONF_PAIR *cp, mycp;
2658 if (!cs || !name) return NULL;
2661 cp = rbtree_finddata(cs->pair_tree, &mycp);
2664 if (!cs->template) return NULL;
2666 return rbtree_finddata(cs->template->pair_tree, &mycp);
2670 * Return the attr of a CONF_PAIR
2673 char const *cf_pair_attr(CONF_PAIR const *pair)
2675 return (pair ? pair->attr : NULL);
2679 * Return the value of a CONF_PAIR
2682 char const *cf_pair_value(CONF_PAIR const *pair)
2684 return (pair ? pair->value : NULL);
2687 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
2689 return (pair ? pair->op : T_INVALID);
2692 /** Return the value (lhs) type
2694 * @param pair to extract value type from.
2695 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2696 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
2698 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
2700 return (pair ? pair->lhs_type : T_INVALID);
2703 /** Return the value (rhs) type
2705 * @param pair to extract value type from.
2706 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2707 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
2709 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
2711 return (pair ? pair->rhs_type : T_INVALID);
2715 * Turn a CONF_PAIR into a VALUE_PAIR
2716 * For now, ignore the "value_type" field...
2718 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
2721 fr_strerror_printf("Internal error");
2726 fr_strerror_printf("No value given for attribute %s", pair->attr);
2731 * false comparisons never match. BUT if it's a "string"
2732 * or `string`, then remember to expand it later.
2734 if ((pair->op != T_OP_CMP_FALSE) &&
2735 ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2736 (pair->rhs_type == T_BACK_QUOTED_STRING))) {
2739 vp = pairmake(pair, NULL, pair->attr, NULL, pair->op);
2744 if (pairmark_xlat(vp, pair->value) < 0) {
2753 return pairmake(pair, NULL, pair->attr, pair->value, pair->op);
2757 * Return the first label of a CONF_SECTION
2760 char const *cf_section_name1(CONF_SECTION const *cs)
2762 return (cs ? cs->name1 : NULL);
2766 * Return the second label of a CONF_SECTION
2769 char const *cf_section_name2(CONF_SECTION const *cs)
2771 return (cs ? cs->name2 : NULL);
2774 /** Return name2 if set, else name1
2777 char const *cf_section_name(CONF_SECTION const *cs)
2781 name = cf_section_name2(cs);
2782 if (name) return name;
2784 return cf_section_name1(cs);
2788 * Find a value in a CONF_SECTION
2790 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
2794 cp = cf_pair_find(cs, attr);
2796 return (cp ? cp->value : NULL);
2800 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
2801 char const *name1, char const *name2)
2804 CONF_ITEM const *ci;
2806 if (!cs || !name1) return NULL;
2808 for (ci = &(cs->item); ci; ci = ci->next) {
2809 if (ci->type != CONF_ITEM_SECTION)
2812 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
2816 their2 = cf_item_to_section(ci)->name2;
2818 if ((!name2 && !their2) ||
2819 (name2 && their2 && (strcmp(name2, their2) == 0))) {
2820 return cf_item_to_section(ci);
2827 /** Find a pair with a name matching attr, after specified pair.
2829 * @param cs to search in.
2830 * @param pair to search from (may be NULL).
2831 * @param attr to find (may be NULL in which case any attribute matches).
2832 * @return the next matching CONF_PAIR or NULL if none matched.
2834 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
2835 CONF_PAIR const *pair, char const *attr)
2839 if (!cs) return NULL;
2842 * If pair is NULL and we're trying to find a specific
2843 * attribute this must be a first time run.
2845 * Find the pair with correct name.
2847 if (!pair && attr) return cf_pair_find(cs, attr);
2850 * Start searching from the next child, or from the head
2851 * of the list of children (if no pair was provided).
2853 for (ci = pair ? pair->item.next : cs->children;
2856 if (ci->type != CONF_ITEM_PAIR) continue;
2858 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
2861 return cf_item_to_pair(ci);
2865 * Find a CONF_SECTION, or return the root if name is NULL
2868 CONF_SECTION *cf_section_find(char const *name)
2871 return cf_section_sub_find(root_config, name);
2876 /** Find a sub-section in a section
2878 * This finds ANY section having the same first name.
2879 * The second name is ignored.
2881 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
2885 if (!cs || !name) return NULL; /* can't find an un-named section */
2888 * No sub-sections have been defined, so none exist.
2890 if (!cs->section_tree) return NULL;
2894 return rbtree_finddata(cs->section_tree, &mycs);
2898 /** Find a CONF_SECTION with both names.
2901 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
2902 char const *name1, char const *name2)
2906 if (!cs) cs = root_config;
2907 if (!cs) return NULL;
2910 CONF_SECTION mycs, *master_cs;
2912 if (!cs->section_tree) return NULL;
2917 master_cs = rbtree_finddata(cs->section_tree, &mycs);
2918 if (!master_cs) return NULL;
2921 * Look it up in the name2 tree. If it's there,
2924 if (master_cs->name2_tree) {
2925 CONF_SECTION *subcs;
2927 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
2928 if (subcs) return subcs;
2932 * We don't insert ourselves into the name2 tree.
2933 * So if there's nothing in the name2 tree, maybe
2934 * *we* are the answer.
2936 if (!master_cs->name2 && name2) return NULL;
2937 if (master_cs->name2 && !name2) return NULL;
2938 if (!master_cs->name2 && !name2) return master_cs;
2940 if (strcmp(master_cs->name2, name2) == 0) {
2948 * Else do it the old-fashioned way.
2950 for (ci = cs->children; ci; ci = ci->next) {
2951 CONF_SECTION *subcs;
2953 if (ci->type != CONF_ITEM_SECTION)
2956 subcs = cf_item_to_section(ci);
2957 if (!subcs->name2) {
2958 if (strcmp(subcs->name1, name2) == 0) break;
2960 if (strcmp(subcs->name2, name2) == 0) break;
2964 return cf_item_to_section(ci);
2968 * Return the next subsection after a CONF_SECTION
2969 * with a certain name1 (char *name1). If the requested
2970 * name1 is NULL, any name1 matches.
2973 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
2974 CONF_SECTION const *subsection,
2979 if (!section) return NULL;
2982 * If subsection is NULL this must be a first time run
2983 * Find the subsection with correct name
2987 ci = section->children;
2989 ci = subsection->item.next;
2992 for (; ci; ci = ci->next) {
2993 if (ci->type != CONF_ITEM_SECTION)
2995 if ((name1 == NULL) ||
2996 (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3000 return cf_item_to_section(ci);
3005 * Return the next section after a CONF_SECTION
3006 * with a certain name1 (char *name1). If the requested
3007 * name1 is NULL, any name1 matches.
3010 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3011 CONF_SECTION const *subsection,
3014 if (!section) return NULL;
3016 if (!section->item.parent) return NULL;
3018 return cf_subsection_find_next(section->item.parent, subsection, name1);
3021 /** Return the next item after a CONF_ITEM.
3024 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3026 if (!section) return NULL;
3029 * If item is NULL this must be a first time run
3030 * Return the first item
3033 return section->children;
3039 static void _pair_count(int *count, CONF_SECTION const *cs)
3041 CONF_ITEM const *ci;
3043 for (ci = cf_item_find_next(cs, NULL);
3045 ci = cf_item_find_next(cs, ci)) {
3047 if (cf_item_is_section(ci)) {
3048 _pair_count(count, cf_item_to_section(ci));
3056 /** Count the number of conf pairs beneath a section
3058 * @param[in] cs to search for items in.
3059 * @return number of pairs nested within section.
3061 int cf_pair_count(CONF_SECTION const *cs)
3065 _pair_count(&count, cs);
3070 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3072 if (!ci) return NULL;
3077 int cf_section_lineno(CONF_SECTION const *section)
3079 return section->item.lineno;
3082 char const *cf_pair_filename(CONF_PAIR const *pair)
3084 return pair->item.filename;
3087 char const *cf_section_filename(CONF_SECTION const *section)
3089 return section->item.filename;
3092 int cf_pair_lineno(CONF_PAIR const *pair)
3094 return pair->item.lineno;
3097 bool cf_item_is_section(CONF_ITEM const *item)
3099 return item->type == CONF_ITEM_SECTION;
3102 bool cf_item_is_pair(CONF_ITEM const *item)
3104 return item->type == CONF_ITEM_PAIR;
3108 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3109 void *data, void (*data_free)(void *))
3113 cd = talloc_zero(parent, CONF_DATA);
3114 if (!cd) return NULL;
3116 cd->item.type = CONF_ITEM_DATA;
3117 cd->item.parent = parent;
3118 cd->name = talloc_typed_strdup(cd, name);
3125 cd->free = data_free;
3128 talloc_set_destructor(cd, _cf_data_free);
3134 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3136 if (!cs || !name) return NULL;
3139 * Find the name in the tree, for speed.
3141 if (cs->data_tree) {
3146 return rbtree_finddata(cs->data_tree, &mycd);
3153 * Find data from a particular section.
3155 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3157 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3159 if (cd) return cd->data;
3165 * Add named data to a configuration section.
3167 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3168 void *data, void (*data_free)(void *),
3173 if (!cs || !name) return -1;
3176 * Already exists. Can't add it.
3178 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3180 cd = cf_data_alloc(cs, name, data, data_free);
3184 cf_item_add(cs, cf_data_to_item(cd));
3190 * Add named data to a configuration section.
3192 int cf_data_add(CONF_SECTION *cs, char const *name,
3193 void *data, void (*data_free)(void *))
3195 return cf_data_add_internal(cs, name, data, data_free, 0);
3198 /** Remove named data from a configuration section
3201 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3207 if (!cs || !name) return NULL;
3208 if (!cs->data_tree) return NULL;
3211 * Find the name in the tree, for speed.
3215 cd = rbtree_finddata(cs->data_tree, &mycd);
3216 if (!cd) return NULL;
3218 talloc_set_destructor(cd, NULL); /* Disarm the destructor */
3219 rbtree_deletebydata(cs->data_tree, &mycd);
3228 * This is here to make the rest of the code easier to read. It
3229 * ties conffile.c to log.c, but it means we don't have to
3230 * pollute every other function with the knowledge of the
3231 * configuration internals.
3233 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3239 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3244 ci->filename ? ci->filename : "unknown",
3245 ci->lineno ? ci->lineno : 0,
3248 ERROR("<unknown>[*]: %s", buffer);
3252 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3258 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3261 rad_assert(cs != NULL);
3264 cs->item.filename ? cs->item.filename : "unknown",
3265 cs->item.lineno ? cs->item.lineno : 0,
3269 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3275 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3278 rad_assert(cp != NULL);
3281 cp->item.filename ? cp->item.filename : "unknown",
3282 cp->item.lineno ? cp->item.lineno : 0,
3286 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3291 if ((debug_flag > 1) && cs) vradlog(L_DBG, fmt, ap);
3296 * Wrapper to simplify the code.
3298 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3304 if (debug_flag > 1 && cs) {
3305 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3307 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3312 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3314 if (!cs) return NULL;
3316 return cs->variables;
3320 * For "switch" and "case" statements.
3322 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3324 if (!cs) return T_INVALID;
3326 return cs->name2_type;