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>
29 #include <freeradius-devel/ident.h>
32 #include <freeradius-devel/radiusd.h>
33 #include <freeradius-devel/rad_assert.h>
39 #ifdef HAVE_SYS_STAT_H
45 typedef enum conf_type {
46 CONF_ITEM_INVALID = 0,
53 struct conf_item *next;
54 struct conf_part *parent;
70 struct conf_item *children;
71 struct conf_item *tail; /* for speed */
72 CONF_SECTION *template;
73 rbtree_t *pair_tree; /* and a partridge.. */
74 rbtree_t *section_tree; /* no jokes here */
75 rbtree_t *name2_tree; /* for sections of the same name2 */
79 const CONF_PARSER *variables;
84 * Internal data that is associated with a configuration section,
85 * so that we don't have to track it separately.
91 void *data; /* user data */
92 void (*free)(void *); /* free user data function */
95 extern int check_config;
97 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
98 void *data, void (*data_free)(void *),
100 static void *cf_data_find_internal(const CONF_SECTION *cs, const char *name,
102 static const char *cf_expand_variables(const char *cf, int *lineno,
103 CONF_SECTION *outercs,
104 char *output, size_t outsize,
107 int cf_log_config = 1;
108 int cf_log_modules = 1;
111 * Isolate the scary casts in these tiny provably-safe functions
113 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
117 rad_assert(ci->type == CONF_ITEM_PAIR);
118 return (CONF_PAIR *)ci;
120 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
124 rad_assert(ci->type == CONF_ITEM_SECTION);
125 return (CONF_SECTION *)ci;
127 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
131 return (CONF_ITEM *)cp;
133 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
137 return (CONF_ITEM *)cs;
140 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
144 rad_assert(ci->type == CONF_ITEM_DATA);
145 return (CONF_DATA *)ci;
147 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
151 return (CONF_ITEM *)cd;
155 * Create a new CONF_PAIR
157 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
158 FR_TOKEN op, FR_TOKEN value_type,
159 CONF_SECTION *parent)
162 size_t attr_len, value_len = 0;
165 if (!attr) return NULL;
166 attr_len = strlen(attr) + 1;
167 if (value) value_len = strlen(value) + 1;
169 p = rad_malloc(sizeof(*cp) + attr_len + value_len);
171 cp = (CONF_PAIR *) p;
172 memset(cp, 0, sizeof(*cp));
173 cp->item.type = CONF_ITEM_PAIR;
174 cp->item.parent = parent;
177 memcpy(p, attr, attr_len);
182 memcpy(p, value, value_len);
185 cp->value_type = value_type;
194 void cf_pair_free(CONF_PAIR **cp)
196 if (!cp || !*cp) return;
199 * attr && value are allocated contiguous with cp.
203 memset(*cp, 0, sizeof(cp));
211 static void cf_data_free(CONF_DATA **cd)
213 if (!cd || !*cd) return;
215 /* name is allocated contiguous with cd */
219 ((*cd)->free)((*cd)->data);
222 memset(*cd, 0, sizeof(cd));
229 * rbtree callback function
231 static int pair_cmp(const void *a, const void *b)
233 const CONF_PAIR *one = a;
234 const CONF_PAIR *two = b;
236 return strcmp(one->attr, two->attr);
241 * rbtree callback function
243 static int section_cmp(const void *a, const void *b)
245 const CONF_SECTION *one = a;
246 const CONF_SECTION *two = b;
248 return strcmp(one->name1, two->name1);
253 * rbtree callback function
255 static int name2_cmp(const void *a, const void *b)
257 const CONF_SECTION *one = a;
258 const CONF_SECTION *two = b;
260 rad_assert(strcmp(one->name1, two->name1) == 0);
262 if (!one->name2 && !two->name2) return 0;
263 if (!one->name2) return -1;
264 if (!two->name2) return +1;
266 return strcmp(one->name2, two->name2);
271 * rbtree callback function
273 static int data_cmp(const void *a, const void *b)
277 const CONF_DATA *one = a;
278 const CONF_DATA *two = b;
280 rcode = one->flag - two->flag;
281 if (rcode != 0) return rcode;
283 return strcmp(one->name, two->name);
288 * Free strings we've parsed into data structures.
290 void cf_section_parse_free(CONF_SECTION *cs, void *base)
293 const CONF_PARSER *variables = cs->variables;
296 * Don't automatically free the strings if we're being
297 * called from a module. This is also for clients.c,
298 * where client_free() expects to be able to free the
299 * client structure. If we moved everything to key off
300 * of the config files, we might solve some problems...
302 if (!variables) return;
305 * Free up dynamically allocated string pointers.
307 for (i = 0; variables[i].name != NULL; i++) {
311 type = variables[i].type & ~PW_TYPE_DEPRECATED;
313 if (type == PW_TYPE_SUBSECTION) {
315 subcs = cf_section_sub_find(cs, variables[i].name);
317 if (!subcs) continue;
319 if (!variables[i].dflt) continue;
321 cf_section_parse_free(subcs, base);
325 if ((type != PW_TYPE_STRING_PTR) &&
326 (type != PW_TYPE_FILENAME)) {
331 * No base struct offset, data must be the pointer.
332 * If data doesn't exist, ignore the entry, there
333 * must be something wrong.
336 if (!variables[i].data) {
340 p = (char **) variables[i].data;;
342 } else if (variables[i].data) {
343 p = (char **) variables[i].data;;
346 p = (char **) (((char *)base) + variables[i].offset);
353 cs->variables = NULL;
358 * Free a CONF_SECTION
360 void cf_section_free(CONF_SECTION **cs)
362 CONF_ITEM *ci, *next;
364 if (!cs || !*cs) return;
366 cf_section_parse_free(*cs, (*cs)->base);
368 for (ci = (*cs)->children; ci; ci = next) {
372 case CONF_ITEM_PAIR: {
373 CONF_PAIR *pair = cf_itemtopair(ci);
378 case CONF_ITEM_SECTION: {
379 CONF_SECTION *section = cf_itemtosection(ci);
380 cf_section_free(§ion);
384 case CONF_ITEM_DATA: {
385 CONF_DATA *data = cf_itemtodata(ci);
390 default: /* should really be an error. */
394 (*cs)->children = NULL;
397 * Name1 and name2 are allocated contiguous with
400 if ((*cs)->pair_tree) {
401 rbtree_free((*cs)->pair_tree);
402 (*cs)->pair_tree = NULL;
404 if ((*cs)->section_tree) {
405 rbtree_free((*cs)->section_tree);
406 (*cs)->section_tree = NULL;
408 if ((*cs)->name2_tree) {
409 rbtree_free((*cs)->name2_tree);
410 (*cs)->name2_tree = NULL;
412 if ((*cs)->data_tree) {
413 rbtree_free((*cs)->data_tree);
414 (*cs)->data_tree = NULL;
418 * And free the section
421 memset(*cs, 0, sizeof(cs));
430 * Allocate a CONF_SECTION
432 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
433 CONF_SECTION *parent)
435 size_t name1_len, name2_len = 0;
440 if (!name1) return NULL;
442 name1_len = strlen(name1) + 1;
444 if (strchr(name2, '$')) {
445 name2 = cf_expand_variables(parent->item.filename,
446 &parent->item.lineno,
448 buffer, sizeof(buffer), name2);
450 radlog(L_ERR, "Failed expanding section name");
454 name2_len = strlen(name2) + 1;
457 p = rad_malloc(sizeof(*cs) + name1_len + name2_len);
459 cs = (CONF_SECTION *) p;
460 memset(cs, 0, sizeof(*cs));
461 cs->item.type = CONF_ITEM_SECTION;
462 cs->item.parent = parent;
465 memcpy(p, name1, name1_len);
468 if (name2 && *name2) {
470 memcpy(p, name2, name2_len);
474 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
475 if (!cs->pair_tree) {
476 cf_section_free(&cs);
481 * Don't create a data tree, it may not be needed.
485 * Don't create the section tree here, it may not
489 if (parent) cs->depth = parent->depth + 1;
495 * Replace pair in a given section with a new pair,
496 * of the given value.
498 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, const char *value)
501 CONF_ITEM *ci, *cn, **last;
503 newp = cf_pair_alloc(cp->attr, value, cp->op, cp->value_type,
505 if (!newp) return -1;
511 * Find the old one from the linked list, and replace it
514 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
516 cn->next = (*last)->next;
523 rbtree_deletebydata(cs->pair_tree, ci);
525 rbtree_insert(cs->pair_tree, cn);
532 * Add an item to a configuration section.
534 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
536 if (!cs || !ci) return;
539 rad_assert(cs->tail == NULL);
542 rad_assert(cs->tail != NULL);
547 * Update the trees (and tail) for each item added.
549 for (/* nothing */; ci != NULL; ci = ci->next) {
553 * For fast lookups, pair's and sections get
558 rbtree_insert(cs->pair_tree, ci);
561 case CONF_ITEM_SECTION: {
562 CONF_SECTION *cs_new = cf_itemtosection(ci);
564 if (!cs->section_tree) {
565 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
566 if (!cs->section_tree) {
567 radlog(L_ERR, "Out of memory");
572 rbtree_insert(cs->section_tree, cs_new);
575 * Two names: find the named instance.
578 CONF_SECTION *old_cs;
582 * CONF_SECTION having
583 * the given name1, and
587 old_cs = rbtree_finddata(cs->section_tree, cs_new);
588 if (!old_cs) return; /* this is a bad error! */
590 if (!old_cs->name2_tree) {
591 old_cs->name2_tree = rbtree_create(name2_cmp,
594 if (old_cs->name2_tree) {
595 rbtree_insert(old_cs->name2_tree, cs_new);
599 } /* was a section */
602 if (!cs->data_tree) {
603 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
606 rbtree_insert(cs->data_tree, ci);
610 default: /* FIXME: assert & error! */
613 } /* switch over conf types */
618 CONF_ITEM *cf_reference_item(const CONF_SECTION *parentcs,
619 CONF_SECTION *outercs,
624 const CONF_SECTION *cs = outercs;
628 strlcpy(name, ptr, sizeof(name));
632 * ".foo" means "foo from the current section"
638 * Just '.' means the current section
641 return cf_sectiontoitem(cs);
645 * ..foo means "foo from the section
646 * enclosing this section" (etc.)
650 cs = cs->item.parent;
655 * "foo.bar.baz" means "from the root"
657 } else if (strchr(p, '.') != NULL) {
658 if (!parentcs) goto no_such_item;
670 if (r && q > r) q = NULL;
671 if (q && q < r) r = NULL;
677 q = strchr(r + 1, ']');
678 if (!q) return NULL; /* parse error */
681 * Points to foo[bar]xx: parse error,
682 * it should be foo[bar] or foo[bar].baz
684 if (q[1] && q[1] != '.') goto no_such_item;
688 next = cf_section_sub_find_name2(cs, p, r + 1);
693 * Points to a named instance of a section.
696 if (!next) goto no_such_item;
697 return &(next->item);
700 q++; /* ensure we skip the ']' and '.' */
704 next = cf_section_sub_find(cs, p);
708 if (!next) break; /* it MAY be a pair in this section! */
714 if (!*p) goto no_such_item;
718 * Find it in the current referenced
721 cp = cf_pair_find(cs, p);
722 if (cp) return &(cp->item);
724 next = cf_section_sub_find(cs, p);
725 if (next) return &(next->item);
728 * "foo" is "in the current section, OR in main".
730 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
736 DEBUG2W("No such configuration item %s", ptr);
741 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
743 if (!cs) return NULL;
745 while (cs->item.parent != NULL) {
746 cs = cs->item.parent;
754 * Expand the variables in an input string.
756 static const char *cf_expand_variables(const char *cf, int *lineno,
757 CONF_SECTION *outercs,
758 char *output, size_t outsize,
762 const char *end, *ptr;
763 const CONF_SECTION *parentcs;
767 * Find the master parent conf section.
768 * We can't use mainconfig.config, because we're in the
769 * process of re-building it, and it isn't set up yet...
771 parentcs = cf_top_section(outercs);
777 * Ignore anything other than "${"
779 if ((*ptr == '$') && (ptr[1] == '{')) {
784 * FIXME: Add support for ${foo:-bar},
789 * Look for trailing '}', and log a
790 * warning for anything that doesn't match,
791 * and exit with a fatal error.
793 end = strchr(ptr, '}');
796 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
804 * Can't really happen because input lines are
805 * capped at 8k, which is sizeof(name)
807 if ((size_t) (end - ptr) >= sizeof(name)) {
808 radlog(L_ERR, "%s[%d]: Reference string is too large",
813 memcpy(name, ptr, end - ptr);
814 name[end - ptr] = '\0';
816 ci = cf_reference_item(parentcs, outercs, name);
817 if (!ci || (ci->type != CONF_ITEM_PAIR)) {
818 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
824 * Substitute the value of the variable.
826 cp = cf_itemtopair(ci);
828 radlog(L_ERR, "%s[%d]: Reference \"%s\" has no value",
833 if (p + strlen(cp->value) >= output + outsize) {
834 radlog(L_ERR, "%s[%d]: Reference \"%s\" is too long",
839 strcpy(p, cp->value);
843 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
849 * Look for trailing '}', and log a
850 * warning for anything that doesn't match,
851 * and exit with a fatal error.
853 end = strchr(ptr, '}');
856 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
862 * Can't really happen because input lines are
863 * capped at 8k, which is sizeof(name)
865 if ((size_t) (end - ptr) >= sizeof(name)) {
866 radlog(L_ERR, "%s[%d]: Environment variable name is too large",
871 memcpy(name, ptr, end - ptr);
872 name[end - ptr] = '\0';
875 * Get the environment variable.
876 * If none exists, then make it an empty string.
884 if (p + strlen(env) >= output + outsize) {
885 radlog(L_ERR, "%s[%d]: Reference \"%s\" is too long",
896 * Copy it over verbatim.
902 if (p >= (output + outsize)) {
903 radlog(L_ERR, "%s[%d]: Reference \"%s\" is too long",
907 } /* loop over all of the input string. */
916 * Parses an item (not a CONF_ITEM) into the specified format,
917 * with a default value.
919 * Returns -1 on error, 0 for correctly parsed, and 1 if the
920 * default value was used. Note that the default value will be
921 * used ONLY if the CONF_PAIR is NULL.
923 int cf_item_parse(CONF_SECTION *cs, const char *name,
924 int type, void *data, const char *dflt)
926 int deprecated, rcode = 0;
930 const CONF_PAIR *cp = NULL;
933 deprecated = (type & PW_TYPE_DEPRECATED);
934 type &= ~PW_TYPE_DEPRECATED;
936 if (cs) cp = cf_pair_find(cs, name);
941 return 1; /* nothing to parse, return default value */
953 cf_log_err(&(cs->item), "\"%s\" is deprecated. Please replace it with the up-to-date configuration", name);
960 case PW_TYPE_BOOLEAN:
962 * Allow yes/no and on/off
964 if ((strcasecmp(value, "yes") == 0) ||
965 (strcasecmp(value, "on") == 0)) {
967 } else if ((strcasecmp(value, "no") == 0) ||
968 (strcasecmp(value, "off") == 0)) {
972 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
975 cf_log_info(cs, "\t%s = %s", name, value);
978 case PW_TYPE_INTEGER:
979 *(int *)data = strtol(value, 0, 0);
980 cf_log_info(cs, "\t%s = %d", name, *(int *)data);
983 case PW_TYPE_STRING_PTR:
990 * Expand variables which haven't already been
991 * expanded automagically when the configuration
999 if (cs) lineno = cs->item.lineno;
1002 * FIXME: sizeof(buffer)?
1004 value = cf_expand_variables("<internal>",
1006 cs, buffer, sizeof(buffer),
1009 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1014 cf_log_info(cs, "\t%s = \"%s\"", name, value ? value : "(null)");
1015 *q = value ? strdup(value) : NULL;
1019 * This is the same as PW_TYPE_STRING_PTR,
1020 * except that we also "stat" the file, and
1023 case PW_TYPE_FILENAME:
1030 * Expand variables which haven't already been
1031 * expanded automagically when the configuration
1034 if ((value == dflt) && cs) {
1040 * FIXME: sizeof(buffer)?
1042 value = cf_expand_variables("?",
1044 cs, buffer, sizeof(buffer),
1046 if (!value) return -1;
1049 cf_log_info(cs, "\t%s = \"%s\"", name, value);
1050 *q = value ? strdup(value) : NULL;
1053 * And now we "stat" the file.
1055 * FIXME: This appears to leak memory on exit,
1056 * and we don't use this information. So it's
1057 * commented out for now.
1062 if (stat(*q, &buf) == 0) {
1065 mtime = rad_malloc(sizeof(*mtime));
1066 *mtime = buf.st_mtime;
1068 cf_data_add_internal(cs, *q, mtime, free,
1074 case PW_TYPE_IPADDR:
1076 * Allow '*' as any address
1078 if (strcmp(value, "*") == 0) {
1079 *(uint32_t *) data = htonl(INADDR_ANY);
1080 cf_log_info(cs, "\t%s = *", name);
1083 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
1084 radlog(L_ERR, "Can't find IP address for host %s", value);
1088 if (strspn(value, "0123456789.") == strlen(value)) {
1089 cf_log_info(cs, "\t%s = %s", name, value);
1091 cf_log_info(cs, "\t%s = %s IP address [%s]", name, value,
1092 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
1094 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
1097 case PW_TYPE_IPV6ADDR:
1098 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
1099 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
1102 cf_log_info(cs, "\t%s = %s IPv6 address [%s]", name, value,
1103 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
1104 memcpy(data, &ipaddr.ipaddr.ip6addr,
1105 sizeof(ipaddr.ipaddr.ip6addr));
1109 radlog(L_ERR, "type %d not supported yet", type);
1111 } /* switch over variable type */
1116 cpn = cf_pair_alloc(name, value, T_OP_SET, T_BARE_WORD, cs);
1117 if (!cpn) return -1;
1118 cpn->item.filename = "<internal>";
1119 cpn->item.lineno = 0;
1120 cf_item_add(cs, &(cpn->item));
1126 static const char *parse_spaces = " ";
1129 * A copy of cf_section_parse that initializes pointers before
1132 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1133 const CONF_PARSER *variables)
1138 for (i = 0; variables[i].name != NULL; i++) {
1139 if (variables[i].type == PW_TYPE_SUBSECTION) {
1140 CONF_SECTION *subcs;
1141 subcs = cf_section_sub_find(cs, variables[i].name);
1142 if (!subcs) continue;
1144 if (!variables[i].dflt) continue;
1146 cf_section_parse_init(subcs, base,
1147 (const CONF_PARSER *) variables[i].dflt);
1151 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
1152 (variables[i].type != PW_TYPE_FILENAME)) {
1156 if (variables[i].data) {
1157 data = variables[i].data; /* prefer this. */
1159 data = ((char *)base) + variables[i].offset;
1164 *(char **) data = NULL;
1165 } /* for all variables in the configuration section */
1169 * Parse a configuration section into user-supplied variables.
1171 int cf_section_parse(CONF_SECTION *cs, void *base,
1172 const CONF_PARSER *variables)
1177 cs->variables = variables; /* this doesn't hurt anything */
1180 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1183 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1184 cs->name1, cs->name2);
1187 cf_section_parse_init(cs, base, variables);
1190 * Handle the known configuration parameters.
1192 for (i = 0; variables[i].name != NULL; i++) {
1194 * Handle subsections specially
1196 if (variables[i].type == PW_TYPE_SUBSECTION) {
1197 CONF_SECTION *subcs;
1198 subcs = cf_section_sub_find(cs, variables[i].name);
1201 * If the configuration section is NOT there,
1204 * FIXME! This is probably wrong... we should
1205 * probably set the items to their default values.
1207 if (!subcs) continue;
1209 if (!variables[i].dflt) {
1210 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
1214 if (cf_section_parse(subcs, base,
1215 (const CONF_PARSER *) variables[i].dflt) < 0) {
1219 } /* else it's a CONF_PAIR */
1221 if (variables[i].data) {
1222 data = variables[i].data; /* prefer this. */
1224 data = ((char *)base) + variables[i].offset;
1226 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1231 * Parse the pair we found, or a default value.
1233 if (cf_item_parse(cs, variables[i].name, variables[i].type,
1234 data, variables[i].dflt) < 0) {
1237 } /* for all variables in the configuration section */
1239 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1246 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1247 cf_section_parse_free(cs, base);
1253 * Sanity check the "if" or "elsif", presuming that the first '('
1254 * has already been eaten.
1256 * We're not really parsing it here, just checking if it's mostly
1259 static int condition_looks_ok(const char **ptr)
1263 const char *p = *ptr;
1275 return 0; /* no trailing slash */
1287 return 0; /* no trailing slash */
1298 if (num_braces == 1) {
1299 const char *q = p + 1;
1302 * Validate that there isn't much
1303 * else after the closing brace.
1305 while ((*q == ' ') || (*q == '\t')) q++;
1311 DEBUG2("Expected open brace '{' after condition at %s", p);
1315 *ptr = p + 1; /* include the trailing ')' */
1335 DEBUG3("Unexpected error");
1340 static const char *cf_local_file(CONF_SECTION *cs, const char *local,
1341 char *buffer, size_t bufsize)
1345 CONF_SECTION *parentcs = cf_top_section(cs);
1347 p = strrchr(parentcs->item.filename, FR_DIR_SEP);
1348 if (!p) return local;
1350 dirsize = (p - parentcs->item.filename) + 1;
1352 if ((dirsize + strlen(local)) >= bufsize) {
1356 memcpy(buffer, parentcs->item.filename, dirsize);
1357 strlcpy(buffer + dirsize, local, bufsize - dirsize);
1362 static int seen_too_much(const char *filename, int lineno, const char *ptr)
1365 if (isspace(*ptr)) {
1370 if (*ptr == '#') return FALSE;
1376 radlog(L_ERR, "%s[%d] Unexpected text %s. See \"man unlang\"",
1377 filename, lineno, ptr);
1386 * Read a part of the config file.
1388 static int cf_section_read(const char *filename, int *lineno, FILE *fp,
1389 CONF_SECTION *current)
1392 CONF_SECTION *this, *css;
1405 this = current; /* add items here */
1408 * Read, checking for line continuations ('\\' at EOL)
1414 * Get data, and remember if we are at EOF.
1416 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1420 * We read the entire 8k worth of data: complain.
1421 * Note that we don't care if the last character
1422 * is \n: it's still forbidden. This means that
1423 * the maximum allowed length of text is 8k-1, which
1427 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
1428 radlog(L_ERR, "%s[%d]: Line too long",
1435 while (isspace((int) *ptr)) ptr++;
1438 memmove(cbuf, ptr, len - (ptr - cbuf));
1439 len -= (ptr - cbuf);
1444 * Not doing continuations: check for edge
1451 while (*ptr && isspace((int) *ptr)) ptr++;
1453 if (!*ptr || (*ptr == '#')) continue;
1455 } else if (at_eof || (len == 0)) {
1456 radlog(L_ERR, "%s[%d]: Continuation at EOF is illegal",
1462 * See if there's a continuation.
1465 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
1470 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1472 * Check for "suppress spaces" magic.
1474 if (!spaces && (len > 2) && (cbuf[len - 2] == '"')) {
1478 cbuf[len - 1] = '\0';
1487 * The parser is getting to be evil.
1489 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
1491 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
1495 if (ptr[0] == '%') {
1496 hack = rad_copy_variable(buf1, ptr);
1498 hack = rad_copy_string(buf1, ptr);
1501 radlog(L_ERR, "%s[%d]: Invalid expansion: %s",
1502 filename, *lineno, ptr);
1509 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1516 radlog(L_ERR, "%s[%d]: Invalid expansion: %s",
1517 filename, *lineno, ptr);
1521 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1525 * The caller eats "name1 name2 {", and calls us
1526 * for the data inside of the section. So if we
1527 * receive a closing brace, then it must mean the
1528 * end of the section.
1530 if (t1 == T_RCBRACE) {
1531 if (this == current) {
1532 radlog(L_ERR, "%s[%d]: Too many closing braces",
1537 this = this->item.parent;
1538 if (seen_too_much(filename, *lineno, ptr)) return -1;
1543 * Allow for $INCLUDE files
1545 * This *SHOULD* work for any level include.
1546 * I really really really hate this file. -cparker
1548 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
1549 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
1552 t2 = getword(&ptr, buf2, sizeof(buf2));
1554 if (buf2[0] == '$') relative = 0;
1556 value = cf_expand_variables(filename, lineno, this, buf, sizeof(buf), buf2);
1557 if (!value) return -1;
1559 if (!FR_DIR_IS_RELATIVE(value)) relative = 0;
1562 value = cf_local_file(current, value, buf3,
1565 radlog(L_ERR, "%s[%d]: Directories too deep.",
1572 #ifdef HAVE_DIRENT_H
1576 * Include ALL non-"dot" files in the directory.
1579 if (value[strlen(value) - 1] == '/') {
1582 struct stat stat_buf;
1584 DEBUG2("including files in directory %s", value );
1589 if (stat(value, &stat_buf) < 0) {
1590 radlog(L_ERR, "%s[%d]: Failed reading directory %s: %s",
1592 value, strerror(errno));
1596 if ((stat_buf.st_mode & S_IWOTH) != 0) {
1597 radlog(L_ERR, "%s[%d]: Directory %s is globally writable. Refusing to start due to insecure configuration.",
1598 filename, *lineno, value);
1602 dir = opendir(value);
1604 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1605 filename, *lineno, value,
1611 * Read the directory, ignoring "." files.
1613 while ((dp = readdir(dir)) != NULL) {
1616 if (dp->d_name[0] == '.') continue;
1619 * Check for valid characters
1621 for (p = dp->d_name; *p != '\0'; p++) {
1622 if (isalpha((int)*p) ||
1626 (*p == '.')) continue;
1629 if (*p != '\0') continue;
1631 snprintf(buf2, sizeof(buf2), "%s%s",
1633 if ((stat(buf2, &stat_buf) != 0) ||
1634 S_ISDIR(stat_buf.st_mode)) continue;
1636 * Read the file into the current
1637 * configuration sectoin.
1639 if (cf_file_include(buf2, this) < 0) {
1647 { /* it was a normal file */
1648 if (buf1[1] == '-') {
1649 struct stat statbuf;
1651 if (stat(value, &statbuf) < 0) {
1652 DEBUGW("Not including file %s: %s", value, strerror(errno));
1657 if (cf_file_include(value, this) < 0) {
1662 } /* we were in an include */
1664 if (strcasecmp(buf1, "$template") == 0) {
1666 CONF_SECTION *parentcs, *templatecs;
1667 t2 = getword(&ptr, buf2, sizeof(buf2));
1669 parentcs = cf_top_section(current);
1671 templatecs = cf_section_sub_find(parentcs, "templates");
1673 radlog(L_ERR, "%s[%d]: No \"templates\" section for reference \"%s\"",
1674 filename, *lineno, buf2);
1678 ci = cf_reference_item(parentcs, templatecs, buf2);
1679 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1680 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
1681 filename, *lineno, buf2);
1685 if (this->template) {
1686 radlog(L_ERR, "%s[%d]: Section already has a template",
1691 this->template = cf_itemtosection(ci);
1696 * Ensure that the user can't add CONF_PAIRs
1697 * with 'internal' names;
1699 if (buf1[0] == '_') {
1700 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1701 filename, *lineno, buf1);
1706 * Grab the next token.
1708 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1724 case T_OP_CMP_FALSE:
1725 if (!this || (strcmp(this->name1, "update") != 0)) {
1726 radlog(L_ERR, "%s[%d]: Invalid operator in assignment",
1734 t3 = getstring(&ptr, buf3, sizeof(buf3));
1735 if (t3 == T_OP_INVALID) {
1736 radlog(L_ERR, "%s[%d]: Parse error: %s",
1743 * These are not allowed. Print a
1744 * helpful error message.
1746 if ((t3 == T_BACK_QUOTED_STRING) &&
1747 (!this || (strcmp(this->name1, "update") != 0))) {
1748 radlog(L_ERR, "%s[%d]: Syntax error: Invalid string `...` in assignment",
1754 * Handle variable substitution via ${foo}
1756 if ((t3 == T_BARE_WORD) ||
1757 (t3 == T_DOUBLE_QUOTED_STRING)) {
1758 value = cf_expand_variables(filename, lineno, this,
1759 buf, sizeof(buf), buf3);
1760 if (!value) return -1;
1761 } else if ((t3 == T_EOL) ||
1769 * Add this CONF_PAIR to our CONF_SECTION
1772 cpn = cf_pair_alloc(buf1, value, t2, t3, this);
1773 cpn->item.filename = filename;
1774 cpn->item.lineno = *lineno;
1775 cf_item_add(this, &(cpn->item));
1779 * This horrible code is here to support
1780 * if/then/else failover in the
1781 * authorize, etc. sections. It makes no
1782 * sense anywhere else.
1785 if ((strcmp(buf1, "if") == 0) ||
1786 (strcmp(buf1, "elsif") == 0)) {
1787 const char *end = ptr;
1788 CONF_SECTION *server;
1790 if (!condition_looks_ok(&end)) {
1791 radlog(L_ERR, "%s[%d]: Parse error in condition at: %s",
1792 filename, *lineno, ptr);
1796 if ((size_t) (end - ptr) >= (sizeof(buf2) - 1)) {
1797 radlog(L_ERR, "%s[%d]: Statement too complicated after \"%s\"",
1798 filename, *lineno, buf1);
1803 * More sanity checking. This is
1804 * getting to be a horrible hack.
1808 if (strcmp(server->name1, "server") == 0) break;
1809 server = server->item.parent;
1813 radlog(L_ERR, "%s[%d]: Processing directives such as \"%s\" cannot be used here.",
1814 filename, *lineno, buf1);
1819 memcpy(buf2 + 1, ptr, end - ptr);
1820 buf2[end - ptr + 1] = '\0';
1824 if (gettoken(&ptr, buf3, sizeof(buf3)) != T_LCBRACE) {
1825 radlog(L_ERR, "%s[%d]: Expected '{'",
1832 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1833 filename, *lineno, buf1);
1840 * No '=', must be a section or sub-section.
1843 case T_DOUBLE_QUOTED_STRING:
1844 case T_SINGLE_QUOTED_STRING:
1845 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1846 if (t3 != T_LCBRACE) {
1847 radlog(L_ERR, "%s[%d]: Expecting section start brace '{' after \"%s %s\"",
1848 filename, *lineno, buf1, buf2);
1855 if (seen_too_much(filename, *lineno, ptr)) return -1;
1857 css = cf_section_alloc(buf1,
1858 t2 == T_LCBRACE ? NULL : buf2,
1861 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1865 cf_item_add(this, &(css->item));
1866 css->item.filename = filename;
1867 css->item.lineno = *lineno;
1870 * The current section is now the child section.
1876 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1877 filename, *lineno, buf1);
1883 * See if EOF was unexpected ..
1885 if (feof(fp) && (this != current)) {
1886 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1888 cf_section_name1(this), cf_section_lineno(this));
1896 * Include one config file in another.
1898 int cf_file_include(const char *filename, CONF_SECTION *cs)
1902 struct stat statbuf;
1906 DEBUG2( "including configuration file %s", filename);
1908 fp = fopen(filename, "r");
1910 radlog(L_ERR, "Unable to open file \"%s\": %s",
1911 filename, strerror(errno));
1915 if (stat(filename, &statbuf) == 0) {
1917 if ((statbuf.st_mode & S_IWOTH) != 0) {
1919 radlog(L_ERR, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1926 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1928 radlog(L_ERR, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1935 if (cf_data_find_internal(cs, filename, PW_TYPE_FILENAME)) {
1937 radlog(L_ERR, "Cannot include the same file twice: \"%s\"",
1943 * Add the filename to the section
1945 mtime = rad_malloc(sizeof(*mtime));
1946 *mtime = statbuf.st_mtime;
1948 if (cf_data_add_internal(cs, filename, mtime, free,
1949 PW_TYPE_FILENAME) < 0) {
1951 radlog(L_ERR, "Internal error opening file \"%s\"",
1956 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILENAME);
1959 radlog(L_ERR, "Internal error opening file \"%s\"",
1964 if (!cs->item.filename) cs->item.filename = filename;
1967 * Read the section. It's OK to have EOF without a
1968 * matching close brace.
1970 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
1980 * Bootstrap a config file.
1982 CONF_SECTION *cf_file_read(const char *filename)
1988 cs = cf_section_alloc("main", NULL, NULL);
1989 if (!cs) return NULL;
1991 cp = cf_pair_alloc("confdir", filename, T_OP_SET, T_BARE_WORD, cs);
1992 if (!cp) return NULL;
1994 p = strrchr(cp->value, FR_DIR_SEP);
1997 cp->item.filename = "internal";
1998 cp->item.lineno = 0;
1999 cf_item_add(cs, &(cp->item));
2001 if (cf_file_include(filename, cs) < 0) {
2002 cf_section_free(&cs);
2010 * Return a CONF_PAIR within a CONF_SECTION.
2012 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
2015 CONF_PAIR *cp = NULL;
2017 if (!cs) return NULL;
2020 * Find the name in the tree, for speed.
2026 cp = rbtree_finddata(cs->pair_tree, &mycp);
2029 * Else find the first one that matches
2031 for (ci = cs->children; ci; ci = ci->next) {
2032 if (ci->type == CONF_ITEM_PAIR) {
2033 return cf_itemtopair(ci);
2038 if (cp || !cs->template) return cp;
2040 return cf_pair_find(cs->template, name);
2044 * Return the attr of a CONF_PAIR
2047 const char *cf_pair_attr(const CONF_PAIR *pair)
2049 return (pair ? pair->attr : NULL);
2053 * Return the value of a CONF_PAIR
2056 const char *cf_pair_value(const CONF_PAIR *pair)
2058 return (pair ? pair->value : NULL);
2061 FR_TOKEN cf_pair_operator(const CONF_PAIR *pair)
2063 return (pair ? pair->op : T_OP_INVALID);
2067 * Return the value type, should be one of the following:
2068 * T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
2069 * T_DOUBLE_QUOTED_STRING or T_OP_INVALID if the pair is NULL.
2071 FR_TOKEN cf_pair_value_type(const CONF_PAIR *pair)
2073 return (pair ? pair->value_type : T_OP_INVALID);
2077 * Copied here for error reporting.
2079 extern void fr_strerror_printf(const char *, ...);
2082 * Turn a CONF_PAIR into a VALUE_PAIR
2083 * For now, ignore the "value_type" field...
2085 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
2088 fr_strerror_printf("Internal error");
2093 fr_strerror_printf("No value given for attribute %s", pair->attr);
2098 * FALSE comparisons never match. BUT if it's a "string"
2099 * or `string`, then remember to expand it later.
2101 if ((pair->op != T_OP_CMP_FALSE) &&
2102 ((pair->value_type == T_DOUBLE_QUOTED_STRING) ||
2103 (pair->value_type == T_BACK_QUOTED_STRING))) {
2106 vp = pairmake(pair->attr, NULL, pair->op);
2111 if (pairmark_xlat(vp, pair->value) < 0) {
2120 return pairmake(pair->attr, pair->value, pair->op);
2124 * Return the first label of a CONF_SECTION
2127 const char *cf_section_name1(const CONF_SECTION *cs)
2129 return (cs ? cs->name1 : NULL);
2133 * Return the second label of a CONF_SECTION
2136 const char *cf_section_name2(const CONF_SECTION *cs)
2138 return (cs ? cs->name2 : NULL);
2142 * Find a value in a CONF_SECTION
2144 const char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
2148 cp = cf_pair_find(cs, attr);
2150 return (cp ? cp->value : NULL);
2154 CONF_SECTION *cf_section_find_name2(const CONF_SECTION *section,
2155 const char *name1, const char *name2)
2160 if (!section || !name1) return NULL;
2162 for (ci = &(section->item); ci; ci = ci->next) {
2163 if (ci->type != CONF_ITEM_SECTION)
2166 if (strcmp(cf_itemtosection(ci)->name1, name1) != 0)
2169 their2 = cf_itemtosection(ci)->name2;
2171 if ((!name2 && !their2) ||
2172 (name2 && their2 && (strcmp(name2, their2) == 0))) {
2173 return cf_itemtosection(ci);
2181 * Return the next pair after a CONF_PAIR
2182 * with a certain name (char *attr) If the requested
2183 * attr is NULL, any attr matches.
2186 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
2187 const CONF_PAIR *pair, const char *attr)
2191 if (!cs) return NULL;
2194 * If pair is NULL this must be a first time run
2195 * Find the pair with correct name
2198 if (!pair) return cf_pair_find(cs, attr);
2200 for (ci = pair->item.next; ci; ci = ci->next) {
2201 if (ci->type != CONF_ITEM_PAIR)
2204 if (!attr || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
2208 return cf_itemtopair(ci);
2212 * Find a CONF_SECTION, or return the root if name is NULL
2215 CONF_SECTION *cf_section_find(const char *name)
2218 return cf_section_sub_find(mainconfig.config, name);
2220 return mainconfig.config;
2224 * Find a sub-section in a section
2227 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
2231 if (!name) return NULL; /* can't find an un-named section */
2234 * Do the fast lookup if possible.
2236 if (cs->section_tree) {
2241 return rbtree_finddata(cs->section_tree, &mycs);
2244 for (ci = cs->children; ci; ci = ci->next) {
2245 if (ci->type != CONF_ITEM_SECTION)
2247 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
2251 return cf_itemtosection(ci);
2257 * Find a CONF_SECTION with both names.
2259 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
2260 const char *name1, const char *name2)
2264 if (!cs) cs = mainconfig.config;
2266 if (name1 && (cs->section_tree)) {
2267 CONF_SECTION mycs, *master_cs;
2272 master_cs = rbtree_finddata(cs->section_tree, &mycs);
2274 return rbtree_finddata(master_cs->name2_tree, &mycs);
2279 * Else do it the old-fashioned way.
2281 for (ci = cs->children; ci; ci = ci->next) {
2282 CONF_SECTION *subcs;
2284 if (ci->type != CONF_ITEM_SECTION)
2287 subcs = cf_itemtosection(ci);
2289 if (!subcs->name2) {
2290 if (strcmp(subcs->name1, name2) == 0) break;
2292 if (strcmp(subcs->name2, name2) == 0) break;
2294 continue; /* don't do the string comparisons below */
2297 if ((strcmp(subcs->name1, name1) == 0) &&
2298 (subcs->name2 != NULL) &&
2299 (strcmp(subcs->name2, name2) == 0))
2303 return cf_itemtosection(ci);
2307 * Return the next subsection after a CONF_SECTION
2308 * with a certain name1 (char *name1). If the requested
2309 * name1 is NULL, any name1 matches.
2312 CONF_SECTION *cf_subsection_find_next(const CONF_SECTION *section,
2313 const CONF_SECTION *subsection,
2318 if (!section) return NULL;
2321 * If subsection is NULL this must be a first time run
2322 * Find the subsection with correct name
2326 ci = section->children;
2328 ci = subsection->item.next;
2331 for (; ci; ci = ci->next) {
2332 if (ci->type != CONF_ITEM_SECTION)
2334 if ((name1 == NULL) ||
2335 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
2339 return cf_itemtosection(ci);
2344 * Return the next section after a CONF_SECTION
2345 * with a certain name1 (char *name1). If the requested
2346 * name1 is NULL, any name1 matches.
2349 CONF_SECTION *cf_section_find_next(const CONF_SECTION *section,
2350 const CONF_SECTION *subsection,
2353 if (!section) return NULL;
2355 if (!section->item.parent) return NULL;
2357 return cf_subsection_find_next(section->item.parent, subsection, name1);
2361 * Return the next item after a CONF_ITEM.
2364 CONF_ITEM *cf_item_find_next(const CONF_SECTION *section, const CONF_ITEM *item)
2366 if (!section) return NULL;
2369 * If item is NULL this must be a first time run
2370 * Return the first item
2374 return section->children;
2380 CONF_SECTION *cf_item_parent(const CONF_ITEM *ci)
2382 if (!ci) return NULL;
2387 int cf_section_lineno(const CONF_SECTION *section)
2389 return section->item.lineno;
2392 const char *cf_pair_filename(const CONF_PAIR *pair)
2394 return pair->item.filename;
2397 const char *cf_section_filename(const CONF_SECTION *section)
2399 return section->item.filename;
2402 int cf_pair_lineno(const CONF_PAIR *pair)
2404 return pair->item.lineno;
2407 int cf_item_is_section(const CONF_ITEM *item)
2409 return item->type == CONF_ITEM_SECTION;
2411 int cf_item_is_pair(const CONF_ITEM *item)
2413 return item->type == CONF_ITEM_PAIR;
2417 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
2418 void *data, void (*data_free)(void *))
2424 name_len = strlen(name) + 1;
2426 p = rad_malloc(sizeof(*cd) + name_len);
2427 cd = (CONF_DATA *) p;
2428 memset(cd, 0, sizeof(*cd));
2430 cd->item.type = CONF_ITEM_DATA;
2431 cd->item.parent = parent;
2433 cd->free = data_free;
2436 memcpy(p, name, name_len);
2442 static void *cf_data_find_internal(const CONF_SECTION *cs, const char *name,
2445 if (!cs || !name) return NULL;
2448 * Find the name in the tree, for speed.
2450 if (cs->data_tree) {
2455 return rbtree_finddata(cs->data_tree, &mycd);
2462 * Find data from a particular section.
2464 void *cf_data_find(const CONF_SECTION *cs, const char *name)
2466 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
2468 if (cd) return cd->data;
2474 * Add named data to a configuration section.
2476 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
2477 void *data, void (*data_free)(void *),
2482 if (!cs || !name) return -1;
2485 * Already exists. Can't add it.
2487 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
2489 cd = cf_data_alloc(cs, name, data, data_free);
2493 cf_item_add(cs, cf_datatoitem(cd));
2499 * Add named data to a configuration section.
2501 int cf_data_add(CONF_SECTION *cs, const char *name,
2502 void *data, void (*data_free)(void *))
2504 return cf_data_add_internal(cs, name, data, data_free, 0);
2509 * Copy CONF_DATA from src to dst
2511 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
2514 CONF_ITEM *cd, *next, **last;
2517 * Don't check if s->data_tree is NULL. It's child
2518 * sections may have data, even if this section doesn't.
2521 rad_assert(d->data_tree == NULL);
2522 d->data_tree = s->data_tree;
2523 s->data_tree = NULL;
2526 * Walk through src, moving CONF_ITEM_DATA
2529 last = &(s->children);
2530 for (cd = s->children; cd != NULL; cd = next) {
2534 * Recursively copy data from child sections.
2536 if (cd->type == CONF_ITEM_SECTION) {
2537 CONF_SECTION *s1, *d1;
2539 s1 = cf_itemtosection(cd);
2540 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
2542 cf_section_copy_data(s1, d1);
2549 * Not conf data, remember last ptr.
2551 if (cd->type != CONF_ITEM_DATA) {
2557 * Remove it from the src list
2563 * Add it to the dst list
2566 rad_assert(d->tail == NULL);
2569 rad_assert(d->tail != NULL);
2577 * For a CONF_DATA element, stat the filename, if necessary.
2579 static int filename_stat(void *context, void *data)
2582 CONF_DATA *cd = data;
2584 context = context; /* -Wunused */
2586 if (cd->flag != PW_TYPE_FILENAME) return 0;
2588 if (stat(cd->name, &buf) < 0) return -1;
2590 if (buf.st_mtime != *(time_t *) cd->data) return -1;
2597 * Compare two CONF_SECTIONS. The items MUST be in the same
2600 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
2602 CONF_ITEM *ca = a->children;
2603 CONF_ITEM *cb = b->children;
2611 if (!ca && !cb) break;
2616 if (ca && ca->type == CONF_ITEM_DATA) {
2620 if (cb && cb->type == CONF_ITEM_DATA) {
2626 * One is smaller than the other. Exit.
2628 if (!ca || !cb) return 0;
2630 if (ca->type != cb->type) return 0;
2633 * Deal with subsections.
2635 if (ca->type == CONF_ITEM_SECTION) {
2636 CONF_SECTION *sa = cf_itemtosection(ca);
2637 CONF_SECTION *sb = cf_itemtosection(cb);
2639 if (!cf_section_cmp(sa, sb)) return 0;
2643 rad_assert(ca->type == CONF_ITEM_PAIR);
2645 pa = cf_itemtopair(ca);
2646 pb = cf_itemtopair(cb);
2649 * Different attr and/or value, Exit.
2651 if ((strcmp(pa->attr, pb->attr) != 0) ||
2652 (strcmp(pa->value, pb->value) != 0)) return 0;
2656 * And go to the next element.
2664 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
2667 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
2672 * They must be the same, say so.
2679 * Migrate CONF_DATA from one section to another.
2681 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
2684 CONF_SECTION *s, *d;
2686 for (ci = src->children; ci != NULL; ci = ci->next) {
2687 if (ci->type != CONF_ITEM_SECTION)
2690 s = cf_itemtosection(ci);
2691 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
2693 if (!d) continue; /* not in new one, don't migrate it */
2696 * A section of the same name is in BOTH src & dst,
2697 * compare the CONF_PAIR's. If they're all the same,
2698 * then copy the CONF_DATA from one to the other.
2700 if (cf_section_cmp(s, d)) {
2701 cf_section_copy_data(s, d);
2705 return 1; /* rcode means anything? */
2709 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
2711 if (!cs || !template || cs->template || template->template) return -1;
2713 cs->template = template;
2720 * This is here to make the rest of the code easier to read. It
2721 * ties conffile.c to log.c, but it means we don't have to
2722 * pollute every other function with the knowledge of the
2723 * configuration internals.
2725 void cf_log_err(const CONF_ITEM *ci, const char *fmt, ...)
2731 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2734 radlog(L_ERR, "%s[%d]: %s",
2735 ci ? ci->filename : "unknown",
2736 ci ? ci->lineno : 0,
2741 void cf_log_info(const CONF_SECTION *cs, const char *fmt, ...)
2746 if (debug_flag > 1 && cf_log_config && cs) vradlog(L_DBG, fmt, ap);
2751 * Wrapper to simplify the code.
2753 void cf_log_module(const CONF_SECTION *cs, const char *fmt, ...)
2759 if (debug_flag > 1 && cf_log_modules && cs) {
2760 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2762 radlog(L_DBG, " Module: %s", buffer);
2767 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
2769 if (!cs) return NULL;
2771 return cs->variables;
2776 * JMG dump_config tries to dump the config structure in a readable format
2780 static int dump_config_section(const CONF_SECTION *cs, int indent)
2786 /* The DEBUG macro doesn't let me
2787 * for(i=0;i<indent;++i) debugputchar('\t');
2788 * so I had to get creative. --Pac. */
2790 for (ci = cs->children; ci; ci = ci->next) {
2792 case CONF_ITEM_PAIR:
2793 cp=cf_itemtopair(ci);
2794 DEBUG("%.*s%s = %s",
2795 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2796 cp->attr, cp->value);
2799 case CONF_ITEM_SECTION:
2800 scs=cf_itemtosection(ci);
2801 DEBUG("%.*s%s %s%s{",
2802 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2804 scs->name2 ? scs->name2 : "",
2805 scs->name2 ? " " : "");
2806 dump_config_section(scs, indent+1);
2808 indent, "\t\t\t\t\t\t\t\t\t\t\t");
2811 default: /* FIXME: Do more! */
2819 int dump_config(CONF_SECTION *cs)
2821 return dump_config_section(cs, 0);
2825 static const char *cf_pair_print_value(const CONF_PAIR *cp,
2826 char *buffer, size_t buflen)
2830 if (!cp->value) return "";
2832 switch (cp->value_type) {
2835 snprintf(buffer, buflen, "%s", cp->value);
2838 case T_SINGLE_QUOTED_STRING:
2839 snprintf(buffer, buflen, "'%s'", cp->value);
2842 case T_DOUBLE_QUOTED_STRING:
2844 fr_print_string(cp->value, strlen(cp->value),
2845 buffer + 1, buflen - 3);
2846 p = buffer + strlen(buffer); /* yuck... */
2856 int cf_pair2xml(FILE *fp, const CONF_PAIR *cp)
2858 fprintf(fp, "<%s>", cp->attr);
2863 const char *q = cp->value;
2865 while (*q && (p < (buffer + sizeof(buffer) - 1))) {
2867 memcpy(p, "&", 4);
2870 } else if (q[0] == '<') {
2871 memcpy(p, "<", 4);
2874 } else if (q[0] == '>') {
2875 memcpy(p, ">", 4);
2885 fprintf(fp, "%s", buffer);
2888 fprintf(fp, "</%s>\n", cp->attr);
2893 int cf_section2xml(FILE *fp, const CONF_SECTION *cs)
2895 CONF_ITEM *ci, *next;
2900 fprintf(fp, "<%s>\n", cs->name1);
2902 fprintf(fp, "<_name2>%s</_name2>\n", cs->name2);
2906 * Loop over contents.
2908 for (ci = cs->children; ci; ci = next) {
2912 case CONF_ITEM_PAIR:
2913 if (!cf_pair2xml(fp, (CONF_PAIR *) ci)) return 0;
2916 case CONF_ITEM_SECTION:
2917 if (!cf_section2xml(fp, (CONF_SECTION *) ci)) return 0;
2920 default: /* should really be an error. */
2926 fprintf(fp, "</%s>\n", cs->name1);
2928 return 1; /* success */
2931 int cf_pair2file(FILE *fp, const CONF_PAIR *cp)
2935 fprintf(fp, "\t%s = %s\n", cp->attr,
2936 cf_pair_print_value(cp, buffer, sizeof(buffer)));
2941 int cf_section2file(FILE *fp, const CONF_SECTION *cs)
2943 const CONF_ITEM *ci, *next;
2949 fprintf(fp, "%s {\n", cs->name1);
2951 fprintf(fp, "%s %s {\n",
2952 cs->name1, cs->name2);
2956 * Loop over contents.
2958 for (ci = cs->children; ci; ci = next) {
2962 case CONF_ITEM_PAIR:
2963 if (!cf_pair2file(fp, (const CONF_PAIR *) ci)) return 0;
2966 case CONF_ITEM_SECTION:
2967 if (!cf_section2file(fp, (const CONF_SECTION *) ci)) return 0;
2970 default: /* should really be an error. */
2978 return 1; /* success */