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 value New value to assign to 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)
551 CONF_ITEM *first = ci;
554 rad_assert((void *)cs != (void *)ci);
556 if (!cs || !ci) return;
559 rad_assert(cs->tail == NULL);
562 rad_assert(cs->tail != NULL);
567 * Update the trees (and tail) for each item added.
569 for (/* nothing */; ci != NULL; ci = ci->next) {
570 rad_assert(ci->next != first); /* simple cycle detection */
575 * For fast lookups, pairs and sections get
580 if (!rbtree_insert(cs->pair_tree, ci)) {
581 CONF_PAIR *cp = cf_item_to_pair(ci);
583 if (strcmp(cp->attr, "confdir") == 0) break;
584 if (!cp->value) break; /* module name, "ok", etc. */
588 case CONF_ITEM_SECTION: {
589 CONF_SECTION *cs_new = cf_item_to_section(ci);
590 CONF_SECTION *name1_cs;
592 if (!cs->section_tree) {
593 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
594 if (!cs->section_tree) {
595 ERROR("Out of memory");
600 name1_cs = rbtree_finddata(cs->section_tree, cs_new);
602 if (!rbtree_insert(cs->section_tree, cs_new)) {
603 ERROR("Failed inserting section into tree");
610 * We already have a section of
611 * this "name1". Add a new
612 * sub-section based on name2.
614 if (!name1_cs->name2_tree) {
615 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
616 if (!name1_cs->name2_tree) {
617 ERROR("Out of memory");
623 * We don't care if this fails.
624 * If the user tries to create
625 * two sections of the same
626 * name1/name2, the duplicate
627 * section is just silently
630 rbtree_insert(name1_cs->name2_tree, cs_new);
632 } /* was a section */
635 if (!cs->data_tree) {
636 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
639 rbtree_insert(cs->data_tree, ci);
643 default: /* FIXME: assert & error! */
646 } /* switch over conf types */
651 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
652 CONF_SECTION *outercs,
657 CONF_SECTION const *cs = outercs;
661 if (!cs) goto no_such_item;
663 strlcpy(name, ptr, sizeof(name));
667 * ".foo" means "foo from the current section"
673 * Just '.' means the current section
676 return cf_section_to_item(cs);
680 * ..foo means "foo from the section
681 * enclosing this section" (etc.)
684 if (cs->item.parent) {
685 cs = cs->item.parent;
689 * .. means the section
690 * enclosing this section
693 return cf_section_to_item(cs);
698 * "foo.bar.baz" means "from the root"
700 } else if (strchr(p, '.') != NULL) {
701 if (!parentcs) goto no_such_item;
713 if (r && q > r) q = NULL;
714 if (q && q < r) r = NULL;
720 q = strchr(r + 1, ']');
721 if (!q) return NULL; /* parse error */
724 * Points to foo[bar]xx: parse error,
725 * it should be foo[bar] or foo[bar].baz
727 if (q[1] && q[1] != '.') goto no_such_item;
731 next = cf_section_sub_find_name2(cs, p, r + 1);
736 * Points to a named instance of a section.
739 if (!next) goto no_such_item;
740 return &(next->item);
743 q++; /* ensure we skip the ']' and '.' */
747 next = cf_section_sub_find(cs, p);
751 if (!next) break; /* it MAY be a pair in this section! */
757 if (!*p) goto no_such_item;
761 * Find it in the current referenced
764 cp = cf_pair_find(cs, p);
765 if (cp) return &(cp->item);
767 next = cf_section_sub_find(cs, p);
768 if (next) return &(next->item);
771 * "foo" is "in the current section, OR in main".
773 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
779 WARN("No such configuration item %s", ptr);
784 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
786 if (!cs) return NULL;
788 while (cs->item.parent != NULL) {
789 cs = cs->item.parent;
797 * Expand the variables in an input string.
799 static char const *cf_expand_variables(char const *cf, int *lineno,
800 CONF_SECTION *outercs,
801 char *output, size_t outsize,
805 char const *end, *ptr;
806 CONF_SECTION const *parentcs;
810 * Find the master parent conf section.
811 * We can't use main_config.config, because we're in the
812 * process of re-building it, and it isn't set up yet...
814 parentcs = cf_top_section(outercs);
820 * Ignore anything other than "${"
822 if ((*ptr == '$') && (ptr[1] == '{')) {
828 * FIXME: Add support for ${foo:-bar},
833 * Look for trailing '}', and log a
834 * warning for anything that doesn't match,
835 * and exit with a fatal error.
837 end = strchr(ptr, '}');
840 INFO("%s[%d]: Variable expansion missing }",
848 * Can't really happen because input lines are
849 * capped at 8k, which is sizeof(name)
851 if ((size_t) (end - ptr) >= sizeof(name)) {
852 ERROR("%s[%d]: Reference string is too large",
857 memcpy(name, ptr, end - ptr);
858 name[end - ptr] = '\0';
860 q = strchr(name, ':');
865 ci = cf_reference_item(parentcs, outercs, name);
867 ERROR("%s[%d]: Reference \"%s\" not found", cf, *lineno, input);
872 * The expansion doesn't refer to another item or section
873 * it's the property of a section.
876 CONF_SECTION *mycs = cf_item_to_section(ci);
878 if (ci->type != CONF_ITEM_SECTION) {
879 ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
883 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
884 case CONF_PROPERTY_NAME:
885 strcpy(p, mycs->name1);
888 case CONF_PROPERTY_INSTANCE:
889 strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
893 ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
899 } else if (ci->type == CONF_ITEM_PAIR) {
901 * Substitute the value of the variable.
903 cp = cf_item_to_pair(ci);
905 ERROR("%s[%d]: Reference \"%s\" has no value",
910 if (p + strlen(cp->value) >= output + outsize) {
911 ERROR("%s[%d]: Reference \"%s\" is too long",
916 strcpy(p, cp->value);
920 } else if (ci->type == CONF_ITEM_SECTION) {
924 * Adding an entry again to a
925 * section is wrong. We don't
926 * want an infinite loop.
928 if (ci->parent == outercs) {
929 ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
934 * Copy the section instead of
937 subcs = cf_item_to_section(ci);
938 subcs = cf_section_dup(outercs, subcs,
939 cf_section_name1(subcs), cf_section_name2(subcs),
942 ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
946 subcs->item.filename = ci->filename;
947 subcs->item.lineno = ci->lineno;
948 cf_item_add(outercs, &(subcs->item));
953 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
956 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
962 * Look for trailing '}', and log a
963 * warning for anything that doesn't match,
964 * and exit with a fatal error.
966 end = strchr(ptr, '}');
969 INFO("%s[%d]: Environment variable expansion missing }",
975 * Can't really happen because input lines are
976 * capped at 8k, which is sizeof(name)
978 if ((size_t) (end - ptr) >= sizeof(name)) {
979 ERROR("%s[%d]: Environment variable name is too large",
984 memcpy(name, ptr, end - ptr);
985 name[end - ptr] = '\0';
988 * Get the environment variable.
989 * If none exists, then make it an empty string.
997 if (p + strlen(env) >= output + outsize) {
998 ERROR("%s[%d]: Reference \"%s\" is too long",
1009 * Copy it over verbatim.
1015 if (p >= (output + outsize)) {
1016 ERROR("%s[%d]: Reference \"%s\" is too long",
1017 cf, *lineno, input);
1020 } /* loop over all of the input string. */
1027 static char const parse_spaces[] = " ";
1029 /** Validation function for ipaddr conffile types
1032 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1033 fr_ipaddr_t *ipaddr)
1037 if (strcmp(value, "*") == 0) {
1038 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1039 } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1040 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1042 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1043 (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1047 case PW_TYPE_IPV4_ADDR:
1048 case PW_TYPE_IPV6_ADDR:
1049 case PW_TYPE_COMBO_IP_ADDR:
1050 switch (ipaddr->af) {
1052 if (ipaddr->prefix != 32) {
1053 ERROR("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted for non-prefix types",
1061 if (ipaddr->prefix != 128) {
1062 ERROR("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted for non-prefix types",
1078 * Parses an item (not a CONF_ITEM) into the specified format,
1079 * with a default value.
1081 * Returns -1 on error, -2 if deprecated, 0 for correctly parsed,
1082 * and 1 if the default value was used. Note that the default
1083 * value will be used ONLY if the CONF_PAIR is NULL.
1085 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1088 bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, xlat;
1091 CONF_PAIR const *cp = NULL;
1092 fr_ipaddr_t *ipaddr;
1097 deprecated = (type & PW_TYPE_DEPRECATED);
1098 required = (type & PW_TYPE_REQUIRED);
1099 attribute = (type & PW_TYPE_ATTRIBUTE);
1100 secret = (type & PW_TYPE_SECRET);
1101 file_input = (type == PW_TYPE_FILE_INPUT); /* check, not and */
1102 cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1103 tmpl = (type & PW_TYPE_TMPL);
1104 xlat = (type & PW_TYPE_XLAT);
1106 if (attribute) required = true;
1107 if (required) cant_be_empty = true; /* May want to review this in the future... */
1109 type &= 0xff; /* normal types are small */
1112 cp = cf_pair_find(cs, name);
1124 cf_log_err(&(cs->item), "Configuration item '%s' must have a value", name);
1126 cf_log_err(&(cp->item), "Configuration item '%s' must have a value", name);
1133 if ((value[0] == '\0') && cant_be_empty) {
1136 cf_log_err(&(cs->item), "Configuration item '%s' must not be empty (zero length)", name);
1137 if (!required) cf_log_err(&(cs->item), "Comment item to silence this message");
1139 cf_log_err(&(cp->item), "Configuration item '%s' must not be empty (zero length)", name);
1140 if (!required) cf_log_err(&(cp->item), "Comment item to silence this message");
1146 cf_log_err(&(cs->item), "Configuration item \"%s\" is deprecated", name);
1156 value_pair_tmpl_t *vpt;
1158 if (!value || (cf_pair_value_type(cp) == T_INVALID)) {
1159 *(value_pair_tmpl_t **)data = NULL;
1163 slen = tmpl_afrom_str(cs, &vpt, value, strlen(value),
1164 cf_pair_value_type(cp), REQUEST_CURRENT, PAIR_LIST_REQUEST);
1166 char *spaces, *text;
1168 fr_canonicalize_error(cs, &spaces, &text, slen, fr_strerror());
1170 cf_log_err_cs(cs, "Failed parsing configuration item '%s'", name);
1171 cf_log_err_cs(cs, "%s", value);
1172 cf_log_err_cs(cs, "%s^ %s", spaces, text);
1174 talloc_free(spaces);
1183 * Don't add default - update with new types.
1185 switch (vpt->type) {
1186 case TMPL_TYPE_LITERAL:
1187 case TMPL_TYPE_ATTR:
1188 case TMPL_TYPE_ATTR_UNDEFINED:
1189 case TMPL_TYPE_LIST:
1190 case TMPL_TYPE_DATA:
1191 case TMPL_TYPE_EXEC:
1192 case TMPL_TYPE_XLAT:
1193 case TMPL_TYPE_XLAT_STRUCT:
1196 case TMPL_TYPE_UNKNOWN:
1197 case TMPL_TYPE_REGEX:
1198 case TMPL_TYPE_REGEX_STRUCT:
1199 case TMPL_TYPE_NULL:
1204 * If the attribute flag is set, the template must be an
1205 * attribute reference.
1207 if (attribute && (vpt->type != TMPL_TYPE_ATTR)) {
1208 cf_log_err(&(cs->item), "Configuration item '%s' must be an attr "
1209 "but is an %s", name, fr_int2str(tmpl_names, vpt->type, "<INVALID>"));
1215 * If the xlat flag is set, the template must be an xlat
1217 if (xlat && (vpt->type != TMPL_TYPE_XLAT_STRUCT)) {
1218 cf_log_err(&(cs->item), "Configuration item '%s' must be an xlat expansion but is an %s",
1219 name, fr_int2str(tmpl_names, vpt->type, "<INVALID>"));
1225 * If we have a type, and the template is an attribute reference
1226 * check that the attribute reference matches the type.
1228 if ((type > 0) && (vpt->type == TMPL_TYPE_ATTR) && (vpt->tmpl_da->type != type)) {
1229 cf_log_err(&(cs->item), "Configuration item '%s' attr must be an %s, but is an %s",
1230 name, fr_int2str(dict_attr_types, type, "<INVALID>"),
1231 fr_int2str(dict_attr_types, vpt->tmpl_da->type, "<INVALID>"));
1235 *(value_pair_tmpl_t **)data = vpt;
1241 case PW_TYPE_BOOLEAN:
1243 * Allow yes/no and on/off
1245 if ((strcasecmp(value, "yes") == 0) ||
1246 (strcasecmp(value, "on") == 0)) {
1247 *(bool *)data = true;
1248 } else if ((strcasecmp(value, "no") == 0) ||
1249 (strcasecmp(value, "off") == 0)) {
1250 *(bool *)data = false;
1252 *(bool *)data = false;
1253 cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1254 "variable %s", value, name);
1257 cf_log_info(cs, "%.*s\t%s = %s",
1258 cs->depth, parse_spaces, name, value);
1261 case PW_TYPE_INTEGER:
1263 unsigned long v = strtoul(value, 0, 0);
1266 * Restrict integer values to 0-INT32_MAX, this means
1267 * it will always be safe to cast them to a signed type
1268 * for comparisons, and imposes the same range limit as
1269 * before we switched to using an unsigned type to
1270 * represent config item integers.
1272 if (v > INT32_MAX) {
1273 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1278 *(uint32_t *)data = v;
1279 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1285 unsigned long v = strtoul(value, 0, 0);
1287 if (v > UINT16_MAX) {
1288 cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1292 *(uint16_t *)data = (uint16_t) v;
1293 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1297 case PW_TYPE_INTEGER64:
1298 *(uint64_t *)data = strtoull(value, 0, 0);
1299 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1302 case PW_TYPE_SIGNED:
1303 *(int32_t *)data = strtol(value, 0, 0);
1304 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1307 case PW_TYPE_STRING:
1314 * Expand variables which haven't already been
1315 * expanded automagically when the configuration
1318 if (value == dflt) {
1321 lineno = cs->item.lineno;
1323 value = cf_expand_variables("<internal>",
1325 cs, buffer, sizeof(buffer),
1328 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1333 if (required && !value) goto is_required;
1334 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1337 if (!dict_attrbyname(value)) {
1339 cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1342 cf_log_err(&(cp->item), "No such attribute '%s'", value);
1349 * Hide secrets when using "radiusd -X".
1351 if (secret && (debug_flag <= 2)) {
1352 cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1353 cs->depth, parse_spaces, name);
1355 cf_log_info(cs, "%.*s\t%s = \"%s\"",
1356 cs->depth, parse_spaces, name, value ? value : "(null)");
1358 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1361 * If there's data AND it's an input file, check
1362 * that we can read it. This check allows errors
1363 * to be caught as early as possible, during
1366 if (*q && file_input) {
1369 if (stat(*q, &buf) < 0) {
1370 char user[255], group[255];
1372 ERROR("Unable to open file \"%s\": %s", value, fr_syserror(errno));
1373 ERROR("Our effective user and group was %s:%s",
1374 (rad_prints_uid(NULL, user, sizeof(user), geteuid()) < 0) ?
1376 (rad_prints_gid(NULL, group, sizeof(group), getegid()) < 0) ?
1377 "unknown" : group );
1384 case PW_TYPE_IPV4_ADDR:
1385 case PW_TYPE_IPV4_PREFIX:
1388 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1389 ERROR("%s", fr_strerror());
1392 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1395 case PW_TYPE_IPV6_ADDR:
1396 case PW_TYPE_IPV6_PREFIX:
1399 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1400 ERROR("%s", fr_strerror());
1403 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1406 case PW_TYPE_COMBO_IP_ADDR:
1407 case PW_TYPE_COMBO_IP_PREFIX:
1410 if (fr_pton(ipaddr, value, -1, true) < 0) {
1411 ERROR("%s", fr_strerror());
1414 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1417 case PW_TYPE_TIMEVAL: {
1422 sec = strtoul(value, &end, 10);
1428 len = strlen(end + 1);
1431 ERROR("Too much precision for timeval");
1436 * If they write "0.1", that means
1437 * "10000" microseconds.
1439 sec = strtoul(end + 1, NULL, 10);
1447 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1448 cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1449 memcpy(data, &tv, sizeof(tv));
1455 * If we get here, it's a sanity check error.
1456 * It's not an error parsing the configuration
1459 rad_assert(type > PW_TYPE_INVALID);
1460 rad_assert(type < PW_TYPE_MAX);
1462 ERROR("type '%s' is not supported in the configuration files",
1463 fr_int2str(dict_attr_types, type, "?Unknown?"));
1465 } /* switch over variable type */
1470 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1471 if (!cpn) return -1;
1472 cpn->item.filename = "<internal>";
1473 cpn->item.lineno = 0;
1474 cf_item_add(cs, &(cpn->item));
1482 * A copy of cf_section_parse that initializes pointers before
1485 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1486 CONF_PARSER const *variables)
1490 for (i = 0; variables[i].name != NULL; i++) {
1491 if (variables[i].type == PW_TYPE_SUBSECTION) {
1492 CONF_SECTION *subcs;
1494 if (!variables[i].dflt) continue;
1496 subcs = cf_section_sub_find(cs, variables[i].name);
1499 * If there's no subsection in the
1500 * config, BUT the CONF_PARSER wants one,
1501 * then create an empty one. This is so
1502 * that we can track the strings,
1503 * etc. allocated in the subsection.
1506 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1509 subcs->item.filename = cs->item.filename;
1510 subcs->item.lineno = cs->item.lineno;
1511 cf_item_add(cs, &(subcs->item));
1514 cf_section_parse_init(subcs, base,
1515 (CONF_PARSER const *) variables[i].dflt);
1519 if ((variables[i].type != PW_TYPE_STRING) &&
1520 (variables[i].type != PW_TYPE_FILE_INPUT) &&
1521 (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1525 if (variables[i].data) {
1526 *(char **) variables[i].data = NULL;
1528 *(char **) (((char *)base) + variables[i].offset) = NULL;
1532 } /* for all variables in the configuration section */
1537 * Parse a configuration section into user-supplied variables.
1539 int cf_section_parse(CONF_SECTION *cs, void *base,
1540 CONF_PARSER const *variables)
1546 cs->variables = variables; /* this doesn't hurt anything */
1549 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1552 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1553 cs->name1, cs->name2);
1556 cf_section_parse_init(cs, base, variables);
1559 * Handle the known configuration parameters.
1561 for (i = 0; variables[i].name != NULL; i++) {
1563 * Handle subsections specially
1565 if (variables[i].type == PW_TYPE_SUBSECTION) {
1566 CONF_SECTION *subcs;
1568 subcs = cf_section_sub_find(cs, variables[i].name);
1570 * Default in this case is overloaded to mean a pointer
1571 * to the CONF_PARSER struct for the subsection.
1573 if (!variables[i].dflt || !subcs) {
1574 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1578 if (cf_section_parse(subcs, base,
1579 (CONF_PARSER const *) variables[i].dflt) < 0) {
1583 } /* else it's a CONF_PAIR */
1585 if (variables[i].data) {
1586 data = variables[i].data; /* prefer this. */
1588 data = ((char *)base) + variables[i].offset;
1590 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1595 * Parse the pair we found, or a default value.
1597 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1600 * Be nice, and print the name of the new config item.
1602 if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1603 (variables[i + 1].data == variables[i].data)) {
1604 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1605 variables[i + 1].name);
1610 } /* for all variables in the configuration section */
1612 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1619 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1625 * Check XLAT things in pass 2. But don't cache the xlat stuff anywhere.
1627 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1636 * Handle the known configuration parameters.
1638 for (i = 0; variables[i].name != NULL; i++) {
1642 * Handle subsections specially
1644 if (variables[i].type == PW_TYPE_SUBSECTION) {
1645 CONF_SECTION *subcs;
1646 subcs = cf_section_sub_find(cs, variables[i].name);
1648 if (cf_section_parse_pass2(subcs, base,
1649 (CONF_PARSER const *) variables[i].dflt) < 0) {
1653 } /* else it's a CONF_PAIR */
1655 cp = cf_pair_find(cs, variables[i].name);
1658 if (!cp || !cp->value) continue;
1660 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1661 (cp->rhs_type != T_BARE_WORD)) continue;
1664 * Non-xlat expansions shouldn't have xlat!
1666 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1667 ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1669 * Ignore %{... in shared secrets.
1670 * They're never dynamically expanded.
1672 if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
1674 if (strstr(cp->value, "%{") != NULL) {
1675 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
1676 cp->item.filename ? cp->item.filename : "unknown",
1677 cp->item.lineno ? cp->item.lineno : 0);
1683 * xlat expansions should be parseable.
1685 value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
1688 slen = xlat_tokenize(cs, value, &xlat, &error);
1690 char *spaces, *text;
1692 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
1694 cf_log_err(&cp->item, "Failed parsing expanded string:");
1695 cf_log_err(&cp->item, "%s", text);
1696 cf_log_err(&cp->item, "%s^ %s", spaces, error);
1698 talloc_free(spaces);
1709 * If the "multi" flag is set, check all of them.
1711 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
1712 cp = cf_pair_find_next(cs, cp, cp->attr);
1715 } /* for all variables in the configuration section */
1721 * Merge the template so everyting else "just works".
1723 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
1727 if (!cs || !template) return true;
1729 cs->template = NULL;
1732 * Walk over the template, adding its' entries to the
1733 * current section. But only if the entries don't
1734 * already exist in the current section.
1736 for (ci = template->children; ci; ci = ci->next) {
1737 if (ci->type == CONF_ITEM_PAIR) {
1738 CONF_PAIR *cp1, *cp2;
1741 * It exists, don't over-write it.
1743 cp1 = cf_item_to_pair(ci);
1744 if (cf_pair_find(cs, cp1->attr)) {
1749 * Create a new pair with all of the data
1752 cp2 = cf_pair_dup(cs, cp1);
1753 if (!cp2) return false;
1755 cp2->item.filename = cp1->item.filename;
1756 cp2->item.lineno = cp1->item.lineno;
1758 cf_item_add(cs, &(cp2->item));
1762 if (ci->type == CONF_ITEM_SECTION) {
1763 CONF_SECTION *subcs1, *subcs2;
1765 subcs1 = cf_item_to_section(ci);
1766 rad_assert(subcs1 != NULL);
1768 subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
1771 * sub-sections get merged.
1773 if (!cf_template_merge(subcs2, subcs1)) {
1780 * Our section doesn't have a matching
1781 * sub-section. Copy it verbatim from
1784 subcs2 = cf_section_dup(cs, subcs1,
1785 cf_section_name1(subcs1), cf_section_name2(subcs1),
1787 if (!subcs2) return false;
1789 subcs2->item.filename = subcs1->item.filename;
1790 subcs2->item.lineno = subcs1->item.lineno;
1792 cf_item_add(cs, &(subcs2->item));
1796 /* ignore everything else */
1802 static char const *cf_local_file(char const *base, char const *filename,
1803 char *buffer, size_t bufsize)
1808 strlcpy(buffer, base, bufsize);
1810 p = strrchr(buffer, FR_DIR_SEP);
1811 if (!p) return filename;
1812 if (p[1]) { /* ./foo */
1816 dirsize = (p - buffer) + 1;
1818 if ((dirsize + strlen(filename)) >= bufsize) {
1822 strlcpy(p + 1, filename, bufsize - dirsize);
1829 * Read a part of the config file.
1831 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
1832 CONF_SECTION *current)
1835 CONF_SECTION *this, *css, *nextcs;
1844 FR_TOKEN t1 = T_INVALID, t2, t3;
1845 bool has_spaces = false;
1848 fr_cond_t *cond = NULL;
1850 this = current; /* add items here */
1853 * Read, checking for line continuations ('\\' at EOL)
1860 * Get data, and remember if we are at EOF.
1862 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1866 * We read the entire 8k worth of data: complain.
1867 * Note that we don't care if the last character
1868 * is \n: it's still forbidden. This means that
1869 * the maximum allowed length of text is 8k-1, which
1873 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
1874 ERROR("%s[%d]: Line too long",
1881 while (isspace((int) *ptr)) ptr++;
1884 memmove(cbuf, ptr, len - (ptr - cbuf));
1885 len -= (ptr - cbuf);
1890 * Not doing continuations: check for edge
1897 while (*ptr && isspace((int) *ptr)) ptr++;
1899 if (!*ptr || (*ptr == '#')) continue;
1901 } else if (at_eof || (len == 0)) {
1902 ERROR("%s[%d]: Continuation at EOF is illegal",
1908 * See if there's a continuation.
1911 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
1916 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1918 * Check for "suppress spaces" magic.
1920 if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
1924 cbuf[len - 1] = '\0';
1934 * The parser is getting to be evil.
1936 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
1938 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
1942 if (ptr[0] == '%') {
1943 hack = rad_copy_variable(buf1, ptr);
1945 hack = rad_copy_string(buf1, ptr);
1948 ERROR("%s[%d]: Invalid expansion: %s",
1949 filename, *lineno, ptr);
1955 t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
1962 ERROR("%s[%d]: Invalid expansion: %s",
1963 filename, *lineno, ptr);
1967 t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
1971 * The caller eats "name1 name2 {", and calls us
1972 * for the data inside of the section. So if we
1973 * receive a closing brace, then it must mean the
1974 * end of the section.
1976 if (t1 == T_RCBRACE) {
1977 if (this == current) {
1978 ERROR("%s[%d]: Too many closing braces",
1984 * Merge the template into the existing
1985 * section. This uses more memory, but
1986 * means that templates now work with
1987 * sub-sections, etc.
1989 if (!cf_template_merge(this, this->template)) {
1993 this = this->item.parent;
1994 goto check_for_more;
1998 * Allow for $INCLUDE files
2000 * This *SHOULD* work for any level include.
2001 * I really really really hate this file. -cparker
2003 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2004 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2005 bool relative = true;
2007 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2009 ERROR("%s[%d]: Unexpected text after $INCLUDE",
2014 if (buf2[0] == '$') relative = false;
2016 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2);
2017 if (!value) return -1;
2019 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2022 value = cf_local_file(filename, value, buf3,
2025 ERROR("%s[%d]: Directories too deep.",
2032 #ifdef HAVE_DIRENT_H
2036 * Include ALL non-"dot" files in the directory.
2039 if (value[strlen(value) - 1] == '/') {
2042 struct stat stat_buf;
2044 DEBUG2("including files in directory %s", value );
2049 if (stat(value, &stat_buf) < 0) {
2050 ERROR("%s[%d]: Failed reading directory %s: %s",
2052 value, fr_syserror(errno));
2056 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2057 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
2058 "insecure configuration", filename, *lineno, value);
2062 dir = opendir(value);
2064 ERROR("%s[%d]: Error reading directory %s: %s",
2065 filename, *lineno, value,
2066 fr_syserror(errno));
2071 * Read the directory, ignoring "." files.
2073 while ((dp = readdir(dir)) != NULL) {
2076 if (dp->d_name[0] == '.') continue;
2079 * Check for valid characters
2081 for (p = dp->d_name; *p != '\0'; p++) {
2082 if (isalpha((int)*p) ||
2086 (*p == '.')) continue;
2089 if (*p != '\0') continue;
2091 snprintf(buf2, sizeof(buf2), "%s%s",
2093 if ((stat(buf2, &stat_buf) != 0) ||
2094 S_ISDIR(stat_buf.st_mode)) continue;
2096 * Read the file into the current
2097 * configuration section.
2099 if (cf_file_include(this, buf2) < 0) {
2107 { /* it was a normal file */
2108 if (buf1[1] == '-') {
2109 struct stat statbuf;
2111 if (stat(value, &statbuf) < 0) {
2112 WARN("Not including file %s: %s", value, fr_syserror(errno));
2117 if (cf_file_include(this, value) < 0) {
2122 } /* we were in an include */
2124 if (strcasecmp(buf1, "$template") == 0) {
2126 CONF_SECTION *parentcs, *templatecs;
2127 t2 = getword(&ptr, buf2, sizeof(buf2), true);
2130 ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2134 parentcs = cf_top_section(current);
2136 templatecs = cf_section_sub_find(parentcs, "templates");
2138 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2142 ci = cf_reference_item(parentcs, templatecs, buf2);
2143 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2144 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2149 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2153 if (this->template) {
2154 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2158 this->template = cf_item_to_section(ci);
2163 * Ensure that the user can't add CONF_PAIRs
2164 * with 'internal' names;
2166 if (buf1[0] == '_') {
2167 ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2172 * Handle if/elsif specially.
2174 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2176 char const *error = NULL;
2178 CONF_SECTION *server;
2181 * if / elsif MUST be inside of a
2182 * processing section, which MUST in turn
2183 * be inside of a "server" directive.
2185 if (!this->item.parent) {
2187 ERROR("%s[%d]: Invalid location for '%s'",
2188 filename, *lineno, buf1);
2193 * Skip (...) to find the {
2195 slen = fr_condition_tokenize(nextcs, cf_section_to_item(nextcs), ptr, &cond,
2196 &error, FR_COND_TWO_PASS);
2197 memcpy(&p, &ptr, sizeof(p));
2200 if (p[-slen] != '{') goto cond_error;
2206 * This hack is so that the NEXT stage
2207 * doesn't go "too far" in expanding the
2208 * variable. We can parse the conditions
2209 * without expanding the ${...} stuff.
2210 * BUT we don't want to expand all of the
2211 * stuff AFTER the condition. So we do
2214 * The first pass is to discover the end
2215 * of the condition. We then expand THAT
2216 * string, and do a second pass parsing
2217 * the expanded condition.
2223 * If there's a ${...}. If so, expand it.
2225 if (strchr(ptr, '$') != NULL) {
2226 ptr = cf_expand_variables(filename, lineno,
2231 ERROR("%s[%d]: Parse error expanding ${...} in condition",
2235 } /* else leave it alone */
2237 server = this->item.parent;
2238 while ((strcmp(server->name1, "server") != 0) &&
2239 (strcmp(server->name1, "policy") != 0) &&
2240 (strcmp(server->name1, "instantiate") != 0)) {
2241 server = server->item.parent;
2242 if (!server) goto invalid_location;
2245 nextcs = cf_section_alloc(this, buf1, ptr);
2247 ERROR("%s[%d]: Failed allocating memory for section",
2251 nextcs->item.filename = talloc_strdup(nextcs, filename);
2252 nextcs->item.lineno = *lineno;
2254 slen = fr_condition_tokenize(nextcs, cf_section_to_item(nextcs), ptr, &cond,
2255 &error, FR_COND_TWO_PASS);
2256 *p = '{'; /* put it back */
2260 char *spaces, *text;
2262 fr_canonicalize_error(nextcs, &spaces, &text, slen, ptr);
2264 ERROR("%s[%d]: Parse error in condition",
2266 ERROR("%s[%d]: %s", filename, *lineno, text);
2267 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2269 talloc_free(spaces);
2271 talloc_free(nextcs);
2275 if ((size_t) slen >= (sizeof(buf2) - 1)) {
2276 talloc_free(nextcs);
2277 ERROR("%s[%d]: Condition is too large after \"%s\"",
2278 filename, *lineno, buf1);
2283 * Copy the expanded and parsed condition
2284 * into buf2. Then, parse the text after
2285 * the condition, which now MUST be a '{.
2287 * If it wasn't '{' it would have been
2288 * caught in the first pass of
2289 * conditional parsing, above.
2291 memcpy(buf2, ptr, slen);
2296 if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2297 talloc_free(nextcs);
2298 ERROR("%s[%d]: Expected '{' %d",
2299 filename, *lineno, t3);
2304 * Swap the condition with trailing stuff for
2305 * the final condition.
2307 memcpy(&p, &nextcs->name2, sizeof(nextcs->name2));
2309 nextcs->name2 = talloc_typed_strdup(nextcs, buf2);
2315 * Grab the next token.
2317 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2334 case T_OP_CMP_FALSE:
2335 if (!this || (strcmp(this->name1, "update") != 0)) {
2336 ERROR("%s[%d]: Invalid operator in assignment",
2344 while (isspace((int) *ptr)) ptr++;
2347 * New parser: non-quoted strings are
2348 * bare words, and we parse everything
2349 * until the next newline, or the next
2350 * comma. If they have { or } in a bare
2351 * word, well... too bad.
2353 if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2354 && (*ptr != '`') && (*ptr != '/')) {
2355 const char *q = ptr;
2358 while (*q && (*q >= ' ') && (*q != ',') &&
2361 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2362 ERROR("%s[%d]: Parse error: value too long",
2367 memcpy(buf3, ptr, (q - ptr));
2368 buf3[q - ptr] = '\0';
2372 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2375 if (t3 == T_INVALID) {
2376 ERROR("%s[%d]: Parse error: %s",
2383 * These are not allowed. Print a
2384 * helpful error message.
2386 if ((t3 == T_BACK_QUOTED_STRING) &&
2387 (!this || (strcmp(this->name1, "update") != 0))) {
2388 ERROR("%s[%d]: Syntax error: Invalid string `...` in assignment",
2394 * Handle variable substitution via ${foo}
2398 case T_DOUBLE_QUOTED_STRING:
2399 case T_BACK_QUOTED_STRING:
2400 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3);
2401 if (!value) return -1;
2415 * Add this CONF_PAIR to our CONF_SECTION
2418 cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2419 if (!cpn) return -1;
2420 cpn->item.filename = talloc_strdup(cpn, filename);
2421 cpn->item.lineno = *lineno;
2422 cf_item_add(this, &(cpn->item));
2425 * Hacks for escaping
2427 if (!cf_new_escape && !this->item.parent && value &&
2428 (strcmp(buf1, "correct_escapes") == 0) &&
2429 ((strcmp(value, "true") == 0) ||
2430 (strcmp(value, "yes") == 0) ||
2431 (strcmp(value, "1") == 0))) {
2432 cf_new_escape = true;
2436 * Require a comma, unless there's a comment.
2438 while (isspace(*ptr)) ptr++;
2447 * foo = bar # other stuff
2449 if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2451 if (!*ptr || (*ptr == '}')) break;
2453 ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2454 filename, *lineno, value, ptr);
2458 * No '=', must be a section or sub-section.
2461 case T_DOUBLE_QUOTED_STRING:
2462 case T_SINGLE_QUOTED_STRING:
2463 t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2464 if (t3 != T_LCBRACE) {
2465 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2466 filename, *lineno, buf1, buf2);
2474 css = cf_section_alloc(this, buf1,
2475 t2 == T_LCBRACE ? NULL : buf2);
2477 ERROR("%s[%d]: Failed allocating memory for section",
2482 css->item.filename = talloc_strdup(css, filename);
2483 css->item.lineno = *lineno;
2484 cf_item_add(this, &(css->item));
2490 cf_item_add(this, &(css->item));
2491 cf_data_add_internal(css, "if", cond, NULL, false);
2492 cond = NULL; /* eaten by the above line */
2496 * There may not be a name2
2498 css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2501 * The current section is now the child section.
2507 ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2512 ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2513 filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2520 * Done parsing one thing. Skip to EOL if possible.
2522 while (isspace(*ptr)) ptr++;
2524 if (*ptr == '#') continue;
2533 * See if EOF was unexpected ..
2535 if (feof(fp) && (this != current)) {
2536 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2537 filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2545 * Include one config file in another.
2547 int cf_file_include(CONF_SECTION *cs, char const *filename)
2551 struct stat statbuf;
2555 DEBUG2("including configuration file %s", filename);
2557 fp = fopen(filename, "r");
2559 ERROR("Unable to open file \"%s\": %s",
2560 filename, fr_syserror(errno));
2564 if (stat(filename, &statbuf) == 0) {
2566 if ((statbuf.st_mode & S_IWOTH) != 0) {
2568 ERROR("Configuration file %s is globally writable. "
2569 "Refusing to start due to insecure configuration.", filename);
2574 #if 0 && defined(S_IROTH)
2575 if (statbuf.st_mode & S_IROTH) != 0) {
2577 ERROR("Configuration file %s is globally readable. "
2578 "Refusing to start due to insecure configuration", filename);
2584 if (cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT)) {
2586 ERROR("Cannot include the same file twice: \"%s\"", filename);
2592 * Add the filename to the section
2594 mtime = talloc(cs, time_t);
2595 *mtime = statbuf.st_mtime;
2597 if (cf_data_add_internal(cs, filename, mtime, NULL, PW_TYPE_FILE_INPUT) < 0) {
2599 ERROR("Internal error opening file \"%s\"",
2604 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILE_INPUT);
2607 ERROR("Internal error opening file \"%s\"",
2612 if (!cs->item.filename) cs->item.filename = talloc_strdup(cs, filename);
2615 * Read the section. It's OK to have EOF without a
2616 * matching close brace.
2618 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
2628 * Bootstrap a config file.
2630 int cf_file_read(CONF_SECTION *cs, char const *filename)
2635 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
2638 p = strrchr(cp->value, FR_DIR_SEP);
2641 cp->item.filename = "internal";
2642 cp->item.lineno = -1;
2643 cf_item_add(cs, &(cp->item));
2645 if (cf_file_include(cs, filename) < 0) return -1;
2651 void cf_file_free(CONF_SECTION *cs)
2658 * Return a CONF_PAIR within a CONF_SECTION.
2660 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
2662 CONF_PAIR *cp, mycp;
2664 if (!cs || !name) return NULL;
2667 cp = rbtree_finddata(cs->pair_tree, &mycp);
2670 if (!cs->template) return NULL;
2672 return rbtree_finddata(cs->template->pair_tree, &mycp);
2676 * Return the attr of a CONF_PAIR
2679 char const *cf_pair_attr(CONF_PAIR const *pair)
2681 return (pair ? pair->attr : NULL);
2685 * Return the value of a CONF_PAIR
2688 char const *cf_pair_value(CONF_PAIR const *pair)
2690 return (pair ? pair->value : NULL);
2693 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
2695 return (pair ? pair->op : T_INVALID);
2698 /** Return the value (lhs) type
2700 * @param pair to extract value type from.
2701 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2702 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
2704 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
2706 return (pair ? pair->lhs_type : T_INVALID);
2709 /** Return the value (rhs) type
2711 * @param pair to extract value type from.
2712 * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2713 * T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
2715 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
2717 return (pair ? pair->rhs_type : T_INVALID);
2721 * Turn a CONF_PAIR into a VALUE_PAIR
2722 * For now, ignore the "value_type" field...
2724 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
2727 fr_strerror_printf("Internal error");
2732 fr_strerror_printf("No value given for attribute %s", pair->attr);
2737 * false comparisons never match. BUT if it's a "string"
2738 * or `string`, then remember to expand it later.
2740 if ((pair->op != T_OP_CMP_FALSE) &&
2741 ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2742 (pair->rhs_type == T_BACK_QUOTED_STRING))) {
2745 vp = pairmake(pair, NULL, pair->attr, NULL, pair->op);
2750 if (pairmark_xlat(vp, pair->value) < 0) {
2759 return pairmake(pair, NULL, pair->attr, pair->value, pair->op);
2763 * Return the first label of a CONF_SECTION
2766 char const *cf_section_name1(CONF_SECTION const *cs)
2768 return (cs ? cs->name1 : NULL);
2772 * Return the second label of a CONF_SECTION
2775 char const *cf_section_name2(CONF_SECTION const *cs)
2777 return (cs ? cs->name2 : NULL);
2780 /** Return name2 if set, else name1
2783 char const *cf_section_name(CONF_SECTION const *cs)
2787 name = cf_section_name2(cs);
2788 if (name) return name;
2790 return cf_section_name1(cs);
2794 * Find a value in a CONF_SECTION
2796 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
2800 cp = cf_pair_find(cs, attr);
2802 return (cp ? cp->value : NULL);
2806 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
2807 char const *name1, char const *name2)
2810 CONF_ITEM const *ci;
2812 if (!cs || !name1) return NULL;
2814 for (ci = &(cs->item); ci; ci = ci->next) {
2815 if (ci->type != CONF_ITEM_SECTION)
2818 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
2822 their2 = cf_item_to_section(ci)->name2;
2824 if ((!name2 && !their2) ||
2825 (name2 && their2 && (strcmp(name2, their2) == 0))) {
2826 return cf_item_to_section(ci);
2833 /** Find a pair with a name matching attr, after specified pair.
2835 * @param cs to search in.
2836 * @param pair to search from (may be NULL).
2837 * @param attr to find (may be NULL in which case any attribute matches).
2838 * @return the next matching CONF_PAIR or NULL if none matched.
2840 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
2841 CONF_PAIR const *pair, char const *attr)
2845 if (!cs) return NULL;
2848 * If pair is NULL and we're trying to find a specific
2849 * attribute this must be a first time run.
2851 * Find the pair with correct name.
2853 if (!pair && attr) return cf_pair_find(cs, attr);
2856 * Start searching from the next child, or from the head
2857 * of the list of children (if no pair was provided).
2859 for (ci = pair ? pair->item.next : cs->children;
2862 if (ci->type != CONF_ITEM_PAIR) continue;
2864 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
2867 return cf_item_to_pair(ci);
2871 * Find a CONF_SECTION, or return the root if name is NULL
2874 CONF_SECTION *cf_section_find(char const *name)
2877 return cf_section_sub_find(root_config, name);
2882 /** Find a sub-section in a section
2884 * This finds ANY section having the same first name.
2885 * The second name is ignored.
2887 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
2891 if (!cs || !name) return NULL; /* can't find an un-named section */
2894 * No sub-sections have been defined, so none exist.
2896 if (!cs->section_tree) return NULL;
2900 return rbtree_finddata(cs->section_tree, &mycs);
2904 /** Find a CONF_SECTION with both names.
2907 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
2908 char const *name1, char const *name2)
2912 if (!cs) cs = root_config;
2913 if (!cs) return NULL;
2916 CONF_SECTION mycs, *master_cs;
2918 if (!cs->section_tree) return NULL;
2923 master_cs = rbtree_finddata(cs->section_tree, &mycs);
2924 if (!master_cs) return NULL;
2927 * Look it up in the name2 tree. If it's there,
2930 if (master_cs->name2_tree) {
2931 CONF_SECTION *subcs;
2933 subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
2934 if (subcs) return subcs;
2938 * We don't insert ourselves into the name2 tree.
2939 * So if there's nothing in the name2 tree, maybe
2940 * *we* are the answer.
2942 if (!master_cs->name2 && name2) return NULL;
2943 if (master_cs->name2 && !name2) return NULL;
2944 if (!master_cs->name2 && !name2) return master_cs;
2946 if (strcmp(master_cs->name2, name2) == 0) {
2954 * Else do it the old-fashioned way.
2956 for (ci = cs->children; ci; ci = ci->next) {
2957 CONF_SECTION *subcs;
2959 if (ci->type != CONF_ITEM_SECTION)
2962 subcs = cf_item_to_section(ci);
2963 if (!subcs->name2) {
2964 if (strcmp(subcs->name1, name2) == 0) break;
2966 if (strcmp(subcs->name2, name2) == 0) break;
2970 return cf_item_to_section(ci);
2974 * Return the next subsection after a CONF_SECTION
2975 * with a certain name1 (char *name1). If the requested
2976 * name1 is NULL, any name1 matches.
2979 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
2980 CONF_SECTION const *subsection,
2985 if (!section) return NULL;
2988 * If subsection is NULL this must be a first time run
2989 * Find the subsection with correct name
2993 ci = section->children;
2995 ci = subsection->item.next;
2998 for (; ci; ci = ci->next) {
2999 if (ci->type != CONF_ITEM_SECTION)
3001 if ((name1 == NULL) ||
3002 (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3006 return cf_item_to_section(ci);
3011 * Return the next section after a CONF_SECTION
3012 * with a certain name1 (char *name1). If the requested
3013 * name1 is NULL, any name1 matches.
3016 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3017 CONF_SECTION const *subsection,
3020 if (!section) return NULL;
3022 if (!section->item.parent) return NULL;
3024 return cf_subsection_find_next(section->item.parent, subsection, name1);
3027 /** Return the next item after a CONF_ITEM.
3030 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3032 if (!section) return NULL;
3035 * If item is NULL this must be a first time run
3036 * Return the first item
3039 return section->children;
3045 static void _pair_count(int *count, CONF_SECTION const *cs)
3047 CONF_ITEM const *ci;
3049 for (ci = cf_item_find_next(cs, NULL);
3051 ci = cf_item_find_next(cs, ci)) {
3053 if (cf_item_is_section(ci)) {
3054 _pair_count(count, cf_item_to_section(ci));
3062 /** Count the number of conf pairs beneath a section
3064 * @param[in] cs to search for items in.
3065 * @return number of pairs nested within section.
3067 int cf_pair_count(CONF_SECTION const *cs)
3071 _pair_count(&count, cs);
3076 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3078 if (!ci) return NULL;
3083 int cf_section_lineno(CONF_SECTION const *section)
3085 return section->item.lineno;
3088 char const *cf_pair_filename(CONF_PAIR const *pair)
3090 return pair->item.filename;
3093 char const *cf_section_filename(CONF_SECTION const *section)
3095 return section->item.filename;
3098 int cf_pair_lineno(CONF_PAIR const *pair)
3100 return pair->item.lineno;
3103 bool cf_item_is_section(CONF_ITEM const *item)
3105 return item->type == CONF_ITEM_SECTION;
3108 bool cf_item_is_pair(CONF_ITEM const *item)
3110 return item->type == CONF_ITEM_PAIR;
3114 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3115 void *data, void (*data_free)(void *))
3119 cd = talloc_zero(parent, CONF_DATA);
3120 if (!cd) return NULL;
3122 cd->item.type = CONF_ITEM_DATA;
3123 cd->item.parent = parent;
3124 cd->name = talloc_typed_strdup(cd, name);
3131 cd->free = data_free;
3134 talloc_set_destructor(cd, _cf_data_free);
3140 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3142 if (!cs || !name) return NULL;
3145 * Find the name in the tree, for speed.
3147 if (cs->data_tree) {
3152 return rbtree_finddata(cs->data_tree, &mycd);
3159 * Find data from a particular section.
3161 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3163 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3165 if (cd) return cd->data;
3171 * Add named data to a configuration section.
3173 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3174 void *data, void (*data_free)(void *),
3179 if (!cs || !name) return -1;
3182 * Already exists. Can't add it.
3184 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3186 cd = cf_data_alloc(cs, name, data, data_free);
3190 cf_item_add(cs, cf_data_to_item(cd));
3196 * Add named data to a configuration section.
3198 int cf_data_add(CONF_SECTION *cs, char const *name,
3199 void *data, void (*data_free)(void *))
3201 return cf_data_add_internal(cs, name, data, data_free, 0);
3204 /** Remove named data from a configuration section
3207 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3213 if (!cs || !name) return NULL;
3214 if (!cs->data_tree) return NULL;
3217 * Find the name in the tree, for speed.
3221 cd = rbtree_finddata(cs->data_tree, &mycd);
3222 if (!cd) return NULL;
3224 talloc_set_destructor(cd, NULL); /* Disarm the destructor */
3225 rbtree_deletebydata(cs->data_tree, &mycd);
3234 * This is here to make the rest of the code easier to read. It
3235 * ties conffile.c to log.c, but it means we don't have to
3236 * pollute every other function with the knowledge of the
3237 * configuration internals.
3239 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3245 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3250 ci->filename ? ci->filename : "unknown",
3251 ci->lineno ? ci->lineno : 0,
3254 ERROR("<unknown>[*]: %s", buffer);
3258 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3264 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3267 rad_assert(cs != NULL);
3270 cs->item.filename ? cs->item.filename : "unknown",
3271 cs->item.lineno ? cs->item.lineno : 0,
3275 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3281 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3284 rad_assert(cp != NULL);
3287 cp->item.filename ? cp->item.filename : "unknown",
3288 cp->item.lineno ? cp->item.lineno : 0,
3292 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3297 if ((debug_flag > 1) && cs) vradlog(L_DBG, fmt, ap);
3302 * Wrapper to simplify the code.
3304 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3310 if (debug_flag > 1 && cs) {
3311 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3313 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3318 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3320 if (!cs) return NULL;
3322 return cs->variables;
3326 * For "switch" and "case" statements.
3328 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3330 if (!cs) return T_INVALID;
3332 return cs->name2_type;