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>
38 #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;
63 LRAD_TOKEN value_type;
69 struct conf_item *children;
70 struct conf_item *tail; /* for speed */
71 CONF_SECTION *template;
72 rbtree_t *pair_tree; /* and a partridge.. */
73 rbtree_t *section_tree; /* no jokes here */
74 rbtree_t *name2_tree; /* for sections of the same name2 */
78 const CONF_PARSER *variables;
83 * Internal data that is associated with a configuration section,
84 * so that we don't have to track it separately.
90 void *data; /* user data */
91 void (*free)(void *); /* free user data function */
95 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
96 void *data, void (*data_free)(void *),
98 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
102 * Isolate the scary casts in these tiny provably-safe functions
104 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
108 rad_assert(ci->type == CONF_ITEM_PAIR);
109 return (CONF_PAIR *)ci;
111 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
115 rad_assert(ci->type == CONF_ITEM_SECTION);
116 return (CONF_SECTION *)ci;
118 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
122 return (CONF_ITEM *)cp;
124 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
128 return (CONF_ITEM *)cs;
131 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
135 rad_assert(ci->type == CONF_ITEM_DATA);
136 return (CONF_DATA *)ci;
138 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
142 return (CONF_ITEM *)cd;
146 * Create a new CONF_PAIR
148 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
149 LRAD_TOKEN operator, LRAD_TOKEN value_type,
150 CONF_SECTION *parent)
154 cp = rad_malloc(sizeof(*cp));
155 memset(cp, 0, sizeof(*cp));
156 cp->item.type = CONF_ITEM_PAIR;
157 cp->item.parent = parent;
158 cp->attr = strdup(attr);
159 cp->value = strdup(value);
160 cp->value_type = value_type;
161 cp->operator = operator;
169 void cf_pair_free(CONF_PAIR **cp)
171 if (!cp || !*cp) return;
179 memset(*cp, 0, sizeof(*cp));
187 static void cf_data_free(CONF_DATA **cd)
189 if (!cd || !*cd) return;
191 if ((*cd)->flag != 0) free((*cd)->name);
195 ((*cd)->free)((*cd)->data);
198 memset(*cd, 0, sizeof(*cd));
205 * rbtree callback function
207 static int pair_cmp(const void *a, const void *b)
209 const CONF_PAIR *one = a;
210 const CONF_PAIR *two = b;
212 return strcmp(one->attr, two->attr);
217 * rbtree callback function
219 static int section_cmp(const void *a, const void *b)
221 const CONF_SECTION *one = a;
222 const CONF_SECTION *two = b;
224 return strcmp(one->name1, two->name1);
229 * rbtree callback function
231 static int name2_cmp(const void *a, const void *b)
233 const CONF_SECTION *one = a;
234 const CONF_SECTION *two = b;
236 rad_assert(strcmp(one->name1, two->name1) == 0);
238 if (!one->name2 && !two->name2) return 0;
239 if (!one->name2) return -1;
240 if (!two->name2) return +1;
242 return strcmp(one->name2, two->name2);
247 * rbtree callback function
249 static int data_cmp(const void *a, const void *b)
253 const CONF_DATA *one = a;
254 const CONF_DATA *two = b;
256 rcode = one->flag - two->flag;
257 if (rcode != 0) return rcode;
259 return strcmp(one->name, two->name);
264 * Free strings we've parsed into data structures.
266 static void cf_section_parse_free(void *base, const CONF_PARSER *variables)
271 * Don't automatically free the strings if we're being
272 * called from a module. This is also for clients.c,
273 * where client_free() expects to be able to free the
274 * client structure. If we moved everything to key off
275 * of the config files, we might solve some problems...
277 if (!variables) return;
280 * Free up dynamically allocated string pointers.
282 for (i = 0; variables[i].name != NULL; i++) {
285 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
286 (variables[i].type != PW_TYPE_FILENAME)) {
291 * No base struct offset, data must be the pointer.
292 * If data doesn't exist, ignore the entry, there
293 * must be something wrong.
296 if (!variables[i].data) {
300 p = (char **) variables[i].data;;
302 } else if (variables[i].data) {
303 p = (char **) variables[i].data;;
306 p = (char **) (((char *)base) + variables[i].offset);
316 * Free a CONF_SECTION
318 void cf_section_free(CONF_SECTION **cs)
320 CONF_ITEM *ci, *next;
322 if (!cs || !*cs) return;
324 if ((*cs)->variables) {
325 cf_section_parse_free((*cs)->base, (*cs)->variables);
328 for (ci = (*cs)->children; ci; ci = next) {
332 case CONF_ITEM_PAIR: {
333 CONF_PAIR *pair = cf_itemtopair(ci);
338 case CONF_ITEM_SECTION: {
340 CONF_SECTION *section = cf_itemtosection(ci);
341 cf_section_free(§ion);
345 case CONF_ITEM_DATA: {
346 CONF_DATA *data = cf_itemtodata(ci);
351 default: /* should really be an error. */
360 if ((*cs)->pair_tree)
361 rbtree_free((*cs)->pair_tree);
362 if ((*cs)->section_tree)
363 rbtree_free((*cs)->section_tree);
364 if ((*cs)->name2_tree)
365 rbtree_free((*cs)->name2_tree);
366 if ((*cs)->data_tree)
367 rbtree_free((*cs)->data_tree);
370 * And free the section
373 memset(*cs, 0, sizeof(*cs));
382 * Allocate a CONF_SECTION
384 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
385 CONF_SECTION *parent)
389 if (!name1) return NULL;
391 cs = rad_malloc(sizeof(*cs));
392 memset(cs, 0, sizeof(*cs));
393 cs->item.type = CONF_ITEM_SECTION;
394 cs->item.parent = parent;
395 cs->name1 = strdup(name1);
397 cf_section_free(&cs);
401 if (name2 && *name2) {
402 cs->name2 = strdup(name2);
404 cf_section_free(&cs);
408 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
409 if (!cs->pair_tree) {
410 cf_section_free(&cs);
415 * Don't create a data tree, it may not be needed.
419 * Don't create the section tree here, it may not
423 if (parent) cs->depth = parent->depth + 1;
430 * Add an item to a configuration section.
432 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
435 rad_assert(cs->tail == NULL);
438 rad_assert(cs->tail != NULL);
443 * Update the trees (and tail) for each item added.
445 for (/* nothing */; ci != NULL; ci = ci->next) {
449 * For fast lookups, pair's and sections get
454 rbtree_insert(cs->pair_tree, ci);
457 case CONF_ITEM_SECTION: {
458 const CONF_SECTION *cs_new = cf_itemtosection(ci);
460 if (!cs->section_tree) {
461 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
462 /* ignore any errors */
465 if (cs->section_tree) {
466 rbtree_insert(cs->section_tree, cs_new); }
469 * Two names: find the named instance.
472 CONF_SECTION *old_cs;
476 * CONF_SECTION having
477 * the given name1, and
481 old_cs = rbtree_finddata(cs->section_tree, cs_new);
482 if (!old_cs) return; /* this is a bad error! */
484 if (!old_cs->name2_tree) {
485 old_cs->name2_tree = rbtree_create(name2_cmp,
488 if (old_cs->name2_tree) {
489 rbtree_insert(old_cs->name2_tree, cs_new);
493 } /* was a section */
496 if (!cs->data_tree) {
497 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
500 rbtree_insert(cs->data_tree, ci);
504 default: /* FIXME: assert & error! */
507 } /* switch over conf types */
512 * Expand the variables in an input string.
514 static const char *cf_expand_variables(const char *cf, int *lineno,
515 const CONF_SECTION *outercs,
516 char *output, const char *input)
519 const char *end, *ptr;
521 const CONF_SECTION *parentcs;
524 * Find the master parent conf section.
525 * We can't use mainconfig.config, because we're in the
526 * process of re-building it, and it isn't set up yet...
528 for (parentcs = outercs;
529 parentcs->item.parent != NULL;
530 parentcs = parentcs->item.parent) {
538 * Ignore anything other than "${"
540 if ((*ptr == '$') && (ptr[1] == '{')) {
543 const CONF_SECTION *cs;
546 * FIXME: Add support for ${foo:-bar},
551 * Look for trailing '}', and log a
552 * warning for anything that doesn't match,
553 * and exit with a fatal error.
555 end = strchr(ptr, '}');
558 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
569 * ${.foo} means "foo from the current section"
577 * ${..foo} means "foo from the section
578 * enclosing this section" (etc.)
580 while (*ptr == '.') {
582 cs = cs->item.parent;
589 * ${foo} is local, with
590 * main as lower priority
595 * ${foo.bar.baz} is always rooted
598 for (q = ptr; *q && q != end; q++) {
610 * Find the next section.
613 (*ptr != 0) && (*ptr != '.') &&
621 * The character is a '.', find a
622 * section (as the user has given
623 * us a subsection to find)
628 ptr++; /* skip the period */
631 * Find the sub-section.
633 next = cf_section_sub_find(cs, name);
635 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
640 } else { /* no period, must be a conf-part */
642 * Find in the current referenced
645 cp = cf_pair_find(cs, name);
648 * It it was NOT ${..foo}
650 * top-level config items.
652 if (!up) cp = cf_pair_find(parentcs, name);
655 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
656 cf_section_name1(cs),
661 } /* until cp is non-NULL */
664 * Substitute the value of the variable.
666 strcpy(p, cp->value);
670 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
676 * Look for trailing '}', and log a
677 * warning for anything that doesn't match,
678 * and exit with a fatal error.
680 end = strchr(ptr, '}');
683 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
688 memcpy(name, ptr, end - ptr);
689 name[end - ptr] = '\0';
692 * Get the environment variable.
693 * If none exists, then make it an empty string.
707 * Copy it over verbatim.
711 } /* loop over all of the input string. */
720 * Parses an item (not a CONF_ITEM) into the specified format,
721 * with a default value.
723 * Returns -1 on error, 0 for correctly parsed, and 1 if the
724 * default value was used. Note that the default value will be
725 * used ONLY if the CONF_PAIR is NULL.
727 int cf_item_parse(CONF_SECTION *cs, const char *name,
728 int type, void *data, const char *dflt)
733 lrad_ipaddr_t ipaddr;
737 cp = cf_pair_find(cs, name);
742 return 1; /* nothing to parse, return default value */
750 case PW_TYPE_BOOLEAN:
752 * Allow yes/no and on/off
754 if ((strcasecmp(value, "yes") == 0) ||
755 (strcasecmp(value, "on") == 0)) {
757 } else if ((strcasecmp(value, "no") == 0) ||
758 (strcasecmp(value, "off") == 0)) {
762 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
765 DEBUG2("\t%s = %s", name, value);
768 case PW_TYPE_INTEGER:
769 *(int *)data = strtol(value, 0, 0);
770 DEBUG2("\t%s = %d", name, *(int *)data);
773 case PW_TYPE_STRING_PTR:
780 * Expand variables which haven't already been
781 * expanded automagically when the configuration
787 int lineno = cs->item.lineno;
790 * FIXME: sizeof(buffer)?
792 value = cf_expand_variables("?",
795 if (!value) return -1;
798 DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
799 *q = value ? strdup(value) : NULL;
803 * This is the same as PW_TYPE_STRING_PTR,
804 * except that we also "stat" the file, and
807 case PW_TYPE_FILENAME:
814 * Expand variables which haven't already been
815 * expanded automagically when the configuration
821 int lineno = cs->item.lineno;
824 * FIXME: sizeof(buffer)?
826 value = cf_expand_variables("?",
829 if (!value) return -1;
832 DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
833 *q = value ? strdup(value) : NULL;
836 * And now we "stat" the file.
841 if (stat(*q, &buf) == 0) {
844 mtime = rad_malloc(sizeof(*mtime));
845 *mtime = buf.st_mtime;
847 cf_data_add_internal(cs, *q, mtime, free,
855 * Allow '*' as any address
857 if (strcmp(value, "*") == 0) {
858 *(uint32_t *) data = htonl(INADDR_ANY);
859 DEBUG2("\t%s = *", name);
862 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
863 radlog(L_ERR, "Can't find IP address for host %s", value);
866 DEBUG2("\t%s = %s IP address [%s]", name, value,
867 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
868 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
871 case PW_TYPE_IPV6ADDR:
872 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
873 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
876 DEBUG2("\t%s = %s IPv6 address [%s]", name, value,
877 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
878 memcpy(data, &ipaddr.ipaddr.ip6addr,
879 sizeof(ipaddr.ipaddr.ip6addr));
883 radlog(L_ERR, "type %d not supported yet", type);
886 } /* switch over variable type */
891 static const char *parse_spaces = " ";
894 * Parse a configuration section into user-supplied variables.
896 int cf_section_parse(CONF_SECTION *cs, void *base,
897 const CONF_PARSER *variables)
903 DEBUG2("%.*s%s {", cs->depth, parse_spaces,
906 DEBUG2("%.*s%s %s {", cs->depth, parse_spaces,
907 cs->name1, cs->name2);
911 * Handle the known configuration parameters.
913 for (i = 0; variables[i].name != NULL; i++) {
915 * Handle subsections specially
917 if (variables[i].type == PW_TYPE_SUBSECTION) {
918 const CONF_SECTION *subcs;
919 subcs = cf_section_sub_find(cs, variables[i].name);
922 * If the configuration section is NOT there,
925 * FIXME! This is probably wrong... we should
926 * probably set the items to their default values.
928 if (!subcs) continue;
930 if (!variables[i].dflt) {
931 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
935 if (cf_section_parse(subcs, base,
936 (const CONF_PARSER *) variables[i].dflt) < 0) {
940 } /* else it's a CONF_PAIR */
942 if (variables[i].data) {
943 data = variables[i].data; /* prefer this. */
945 data = ((char *)base) + variables[i].offset;
947 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
952 * Parse the pair we found, or a default value.
954 if (cf_item_parse(cs, variables[i].name, variables[i].type,
955 data, variables[i].dflt) < 0) {
958 } /* for all variables in the configuration section */
960 DEBUG2("%.*s}", cs->depth, parse_spaces);
963 cs->variables = variables;
968 DEBUG2("%.*s}", cs->depth, parse_spaces);
969 cf_section_parse_free(base, variables);
975 * Sanity check the "if" or "elsif", presuming that the first '('
976 * has already been eaten.
978 * We're not really parsing it here, just checking if it's mostly
981 static int condition_looks_ok(const char **ptr)
985 const char *p = *ptr;
997 return 0; /* no trailing slash */
1009 return 0; /* no trailing slash */
1020 if (num_braces == 1) {
1021 const char *q = p + 1;
1024 * Validate that there isn't much
1025 * else after the closing brace.
1027 while ((*q == ' ') || (*q == '\t')) q++;
1036 *ptr = p + 1; /* include the trailing ')' */
1061 * Read a part of the config file.
1063 static int cf_section_read(const char *file, int *lineno, FILE *fp,
1064 CONF_SECTION *current)
1067 CONF_SECTION *this, *css;
1079 this = current; /* add items here */
1082 * Read, checking for line continuations ('\\' at EOL)
1088 * Get data, and remember if we are at EOF.
1090 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1096 * We've filled the buffer, and there isn't
1099 if ((cbuf[len - 1] != '\n') && !feof(fp)) {
1100 radlog(L_ERR, "%s[%d]: Line too long",
1106 * Check for continuations.
1108 if (cbuf[len - 1] == '\n') len--;
1111 * Last character is '\\'. Over-write it,
1112 * and read another line.
1114 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1115 if (len >= (sizeof(buf) - 5)) {
1116 radlog(L_ERR, "%s[%d]: Line too long",
1121 cbuf[len - 1] = '\0';
1127 * We're at EOF, and haven't read anything. Stop.
1129 if (eof && (cbuf == buf)) {
1134 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1136 if ((*buf1 == '#') || (*buf1 == '\0')) {
1141 * The caller eats "name1 name2 {", and calls us
1142 * for the data inside of the section. So if we
1143 * receive a closing brace, then it must mean the
1144 * end of the section.
1146 if (t1 == T_RCBRACE) {
1147 if (this == current) {
1148 radlog(L_ERR, "%s[%d]: Too many closing braces",
1153 this = this->item.parent;
1158 * Allow for $INCLUDE files
1160 * This *SHOULD* work for any level include.
1161 * I really really really hate this file. -cparker
1163 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1164 t2 = getword(&ptr, buf2, sizeof(buf2));
1166 value = cf_expand_variables(file, lineno, this, buf, buf2);
1167 if (!value) return -1;
1169 #ifdef HAVE_DIRENT_H
1173 * Include ALL non-"dot" files in the directory.
1176 if (value[strlen(value) - 1] == '/') {
1179 struct stat stat_buf;
1181 DEBUG2( "Config: including files in directory: %s", value );
1182 dir = opendir(value);
1184 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1185 file, *lineno, value,
1191 * Read the directory, ignoring "." files.
1193 while ((dp = readdir(dir)) != NULL) {
1196 if (dp->d_name[0] == '.') continue;
1199 * Check for valid characters
1201 for (p = dp->d_name; *p != '\0'; p++) {
1202 if (isalpha((int)*p) ||
1205 (*p == '.')) continue;
1208 if (*p != '\0') continue;
1210 snprintf(buf2, sizeof(buf2), "%s%s",
1212 if ((stat(buf2, &stat_buf) != 0) ||
1213 S_ISDIR(stat_buf.st_mode)) continue;
1215 * Read the file into the current
1216 * configuration sectoin.
1218 if (cf_file_include(buf2, this) < 0) {
1226 { /* it was a normal file */
1227 if (cf_file_include(value, this) < 0) {
1232 } /* we were in an include */
1235 * Ensure that the user can't add CONF_PAIRs
1236 * with 'internal' names;
1238 if (buf1[0] == '_') {
1239 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1240 file, *lineno, buf1);
1245 * Grab the next token.
1247 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1257 t3 = getstring(&ptr, buf3, sizeof(buf3));
1260 * Handle variable substitution via ${foo}
1262 if ((t3 == T_BARE_WORD) ||
1263 (t3 == T_DOUBLE_QUOTED_STRING)) {
1264 value = cf_expand_variables(file, lineno, this,
1266 if (!value) return -1;
1272 * Add this CONF_PAIR to our CONF_SECTION
1274 cpn = cf_pair_alloc(buf1, value, t2, t3, this);
1275 cpn->item.lineno = *lineno;
1276 cf_item_add(this, cf_pairtoitem(cpn));
1280 * This horrible code is here to support
1281 * if/then/else failover in the
1282 * authorize, etc. sections. It makes no
1283 * sense anywhere else.
1286 if ((strcmp(buf1, "if") == 0) ||
1287 (strcmp(buf1, "elsif") == 0)) {
1288 const char *end = ptr;
1290 if (!condition_looks_ok(&end)) {
1291 radlog(L_ERR, "%s[%d]: Parse error in condition at: %s",
1292 file, *lineno, ptr);
1296 if ((end - ptr) >= (sizeof(buf2) - 1)) {
1297 radlog(L_ERR, "%s[%d]: Statement too complicated after \"%s\"",
1298 file, *lineno, buf1);
1302 memcpy(buf2 + 1, ptr, end - ptr);
1303 buf2[end - ptr + 1] = '\0';
1309 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1310 file, *lineno, buf1);
1316 * No '=', must be a section or sub-section.
1319 case T_DOUBLE_QUOTED_STRING:
1320 case T_SINGLE_QUOTED_STRING:
1321 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1322 if (t3 != T_LCBRACE) {
1323 radlog(L_ERR, "%s[%d]: Expecting section start brace '{' after \"%s %s\"",
1324 file, *lineno, buf1, buf2);
1330 css = cf_section_alloc(buf1,
1331 t2 == T_LCBRACE ? NULL : buf2,
1334 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1338 cf_item_add(this, cf_sectiontoitem(css));
1339 css->item.lineno = *lineno;
1342 * The current section is now the child section.
1348 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1349 file, *lineno, buf1);
1355 * See if EOF was unexpected ..
1357 if (feof(fp) && (this != current)) {
1358 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1360 cf_section_name1(this), cf_section_lineno(this));
1368 * Include one config file in another.
1370 int cf_file_include(const char *file, CONF_SECTION *cs)
1374 struct stat statbuf;
1377 DEBUG2( "Config: including file: %s", file);
1379 if (stat(file, &statbuf) == 0) {
1381 if ((statbuf.st_mode & S_IWOTH) != 0) {
1382 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1389 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1390 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1397 fp = fopen(file, "r");
1399 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1400 file, strerror(errno));
1405 * Read the section. It's OK to have EOF without a
1406 * matching close brace.
1408 if (cf_section_read(file, &lineno, fp, cs) < 0) {
1414 * Add the filename to the section
1416 mtime = rad_malloc(sizeof(*mtime));
1417 *mtime = statbuf.st_mtime;
1419 cf_data_add_internal(cs, file, mtime, free,
1427 * Bootstrap a config file.
1429 CONF_SECTION *cf_file_read(const char *file)
1433 cs = cf_section_alloc("main", NULL, NULL);
1434 if (!cs) return NULL;
1436 if (cf_file_include(file, cs) < 0) {
1437 cf_section_free(&cs);
1445 * Return a CONF_PAIR within a CONF_SECTION.
1447 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1450 CONF_PAIR *cp = NULL;
1452 if (!cs) cs = mainconfig.config;
1455 * Find the name in the tree, for speed.
1461 cp = rbtree_finddata(cs->pair_tree, &mycp);
1464 * Else find the first one that matches
1466 for (ci = cs->children; ci; ci = ci->next) {
1467 if (ci->type == CONF_ITEM_PAIR) {
1468 return cf_itemtopair(ci);
1473 if (cp || !cs->template) return cp;
1475 return cf_pair_find(cs->template, name);
1479 * Return the attr of a CONF_PAIR
1482 char *cf_pair_attr(CONF_PAIR *pair)
1484 return (pair ? pair->attr : NULL);
1488 * Return the value of a CONF_PAIR
1491 char *cf_pair_value(CONF_PAIR *pair)
1493 return (pair ? pair->value : NULL);
1497 * Copied here for error reporting.
1499 extern void librad_log(const char *, ...);
1502 * Turn a CONF_PAIR into a VALUE_PAIR
1503 * For now, ignore the "value_type" field...
1505 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
1511 librad_log("Internal error");
1515 da = dict_attrbyname(pair->attr);
1517 librad_log("Unknown attribute %s", pair->attr);
1522 librad_log("No value given for attribute %s", pair->attr);
1528 librad_log("Out of memory");
1532 vp->operator = pair->operator;
1534 if ((pair->value_type == T_BARE_WORD) ||
1535 (pair->value_type == T_SINGLE_QUOTED_STRING)) {
1536 if (!pairparsevalue(vp, pair->value)) {
1540 vp->flags.do_xlat = 0;
1542 vp->flags.do_xlat = 1;
1549 * Return the first label of a CONF_SECTION
1552 const char *cf_section_name1(const CONF_SECTION *cs)
1554 return (cs ? cs->name1 : NULL);
1558 * Return the second label of a CONF_SECTION
1561 const char *cf_section_name2(const CONF_SECTION *cs)
1563 return (cs ? cs->name2 : NULL);
1567 * Find a value in a CONF_SECTION
1569 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1573 cp = cf_pair_find(cs, attr);
1575 return (cp ? cp->value : NULL);
1579 * Return the next pair after a CONF_PAIR
1580 * with a certain name (char *attr) If the requested
1581 * attr is NULL, any attr matches.
1584 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1585 const CONF_PAIR *pair, const char *attr)
1590 * If pair is NULL this must be a first time run
1591 * Find the pair with correct name
1595 return cf_pair_find(cs, attr);
1598 ci = cf_pairtoitem(pair)->next;
1600 for (; ci; ci = ci->next) {
1601 if (ci->type != CONF_ITEM_PAIR)
1603 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1607 return cf_itemtopair(ci);
1611 * Find a CONF_SECTION, or return the root if name is NULL
1614 CONF_SECTION *cf_section_find(const char *name)
1617 return cf_section_sub_find(mainconfig.config, name);
1619 return mainconfig.config;
1623 * Find a sub-section in a section
1626 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1631 * Do the fast lookup if possible.
1633 if (name && cs->section_tree) {
1638 return rbtree_finddata(cs->section_tree, &mycs);
1641 for (ci = cs->children; ci; ci = ci->next) {
1642 if (ci->type != CONF_ITEM_SECTION)
1644 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1648 return cf_itemtosection(ci);
1654 * Find a CONF_SECTION with both names.
1656 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1657 const char *name1, const char *name2)
1661 if (!name2) return cf_section_sub_find(cs, name1);
1663 if (!cs) cs = mainconfig.config;
1665 if (name1 && (cs->section_tree)) {
1666 CONF_SECTION mycs, *master_cs;
1671 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1673 return rbtree_finddata(master_cs->name2_tree, &mycs);
1678 * Else do it the old-fashioned way.
1680 for (ci = cs->children; ci; ci = ci->next) {
1681 CONF_SECTION *subcs;
1683 if (ci->type != CONF_ITEM_SECTION)
1686 subcs = cf_itemtosection(ci);
1688 if (!subcs->name2) {
1689 if (strcmp(subcs->name1, name2) == 0) break;
1691 if (strcmp(subcs->name2, name2) == 0) break;
1693 continue; /* don't do the string comparisons below */
1696 if ((strcmp(subcs->name1, name1) == 0) &&
1697 (subcs->name2 != NULL) &&
1698 (strcmp(subcs->name2, name2) == 0))
1702 return cf_itemtosection(ci);
1706 * Return the next subsection after a CONF_SECTION
1707 * with a certain name1 (char *name1). If the requested
1708 * name1 is NULL, any name1 matches.
1711 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1712 CONF_SECTION *subsection,
1718 * If subsection is NULL this must be a first time run
1719 * Find the subsection with correct name
1722 if (subsection == NULL){
1723 ci = section->children;
1725 ci = cf_sectiontoitem(subsection)->next;
1728 for (; ci; ci = ci->next) {
1729 if (ci->type != CONF_ITEM_SECTION)
1731 if ((name1 == NULL) ||
1732 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1736 return cf_itemtosection(ci);
1740 * Return the next item after a CONF_ITEM.
1743 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1746 * If item is NULL this must be a first time run
1747 * Return the first item
1751 return section->children;
1757 int cf_section_lineno(CONF_SECTION *section)
1759 return cf_sectiontoitem(section)->lineno;
1762 int cf_pair_lineno(CONF_PAIR *pair)
1764 return cf_pairtoitem(pair)->lineno;
1767 int cf_item_is_section(CONF_ITEM *item)
1769 return item->type == CONF_ITEM_SECTION;
1771 int cf_item_is_pair(CONF_ITEM *item)
1773 return item->type == CONF_ITEM_PAIR;
1777 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1778 void *data, void (*data_free)(void *))
1782 cd = rad_malloc(sizeof(*cd));
1783 memset(cd, 0, sizeof(*cd));
1785 cd->item.type = CONF_ITEM_DATA;
1786 cd->item.parent = parent;
1787 cd->name = strdup(name);
1789 cd->free = data_free;
1795 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1798 if (!cs || !name) return NULL;
1801 * Find the name in the tree, for speed.
1803 if (cs->data_tree) {
1804 CONF_DATA mycd, *cd;
1808 cd = rbtree_finddata(cs->data_tree, &mycd);
1809 if (cd) return cd->data;
1816 * Find data from a particular section.
1818 void *cf_data_find(CONF_SECTION *cs, const char *name)
1820 return cf_data_find_internal(cs, name, 0);
1825 * Add named data to a configuration section.
1827 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1828 void *data, void (*data_free)(void *),
1833 if (!cs || !name) return -1;
1836 * Already exists. Can't add it.
1838 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1840 cd = cf_data_alloc(cs, name, data, data_free);
1844 cf_item_add(cs, cf_datatoitem(cd));
1850 * Add named data to a configuration section.
1852 int cf_data_add(CONF_SECTION *cs, const char *name,
1853 void *data, void (*data_free)(void *))
1855 return cf_data_add_internal(cs, name, data, data_free, 0);
1860 * Copy CONF_DATA from src to dst
1862 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1865 CONF_ITEM *cd, *next, **last;
1868 * Don't check if s->data_tree is NULL. It's child
1869 * sections may have data, even if this section doesn't.
1872 rad_assert(d->data_tree == NULL);
1873 d->data_tree = s->data_tree;
1874 s->data_tree = NULL;
1877 * Walk through src, moving CONF_ITEM_DATA
1880 last = &(s->children);
1881 for (cd = s->children; cd != NULL; cd = next) {
1885 * Recursively copy data from child sections.
1887 if (cd->type == CONF_ITEM_SECTION) {
1888 CONF_SECTION *s1, *d1;
1890 s1 = cf_itemtosection(cd);
1891 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1893 cf_section_copy_data(s1, d1);
1900 * Not conf data, remember last ptr.
1902 if (cd->type != CONF_ITEM_DATA) {
1908 * Remove it from the src list
1914 * Add it to the dst list
1917 rad_assert(d->tail == NULL);
1920 rad_assert(d->tail != NULL);
1928 * For a CONF_DATA element, stat the filename, if necessary.
1930 static int filename_stat(void *context, void *data)
1933 CONF_DATA *cd = data;
1935 context = context; /* -Wunused */
1937 if (cd->flag != PW_TYPE_FILENAME) return 0;
1939 if (stat(cd->name, &buf) < 0) return -1;
1941 if (buf.st_mtime != *(time_t *) cd->data) return -1;
1948 * Compare two CONF_SECTIONS. The items MUST be in the same
1951 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1953 CONF_ITEM *ca = a->children;
1954 CONF_ITEM *cb = b->children;
1962 if (!ca && !cb) break;
1967 if (ca && ca->type == CONF_ITEM_DATA) {
1971 if (cb && cb->type == CONF_ITEM_DATA) {
1977 * One is smaller than the other. Exit.
1979 if (!ca || !cb) return 0;
1981 if (ca->type != cb->type) return 0;
1984 * Deal with subsections.
1986 if (ca->type == CONF_ITEM_SECTION) {
1987 CONF_SECTION *sa = cf_itemtosection(ca);
1988 CONF_SECTION *sb = cf_itemtosection(cb);
1990 if (!cf_section_cmp(sa, sb)) return 0;
1994 rad_assert(ca->type == CONF_ITEM_PAIR);
1996 pa = cf_itemtopair(ca);
1997 pb = cf_itemtopair(cb);
2000 * Different attr and/or value, Exit.
2002 if ((strcmp(pa->attr, pb->attr) != 0) ||
2003 (strcmp(pa->value, pb->value) != 0)) return 0;
2007 * And go to the next element.
2015 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
2018 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
2023 * They must be the same, say so.
2030 * Migrate CONF_DATA from one section to another.
2032 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
2035 CONF_SECTION *s, *d;
2037 for (ci = src->children; ci != NULL; ci = ci->next) {
2038 if (ci->type != CONF_ITEM_SECTION)
2041 s = cf_itemtosection(ci);
2042 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
2044 if (!d) continue; /* not in new one, don't migrate it */
2047 * A section of the same name is in BOTH src & dst,
2048 * compare the CONF_PAIR's. If they're all the same,
2049 * then copy the CONF_DATA from one to the other.
2051 if (cf_section_cmp(s, d)) {
2052 cf_section_copy_data(s, d);
2056 return 1; /* rcode means anything? */
2059 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
2061 if (!cs || !template || cs->template || template->template) return -1;
2063 cs->template = template;
2070 * JMG dump_config tries to dump the config structure in a readable format
2074 static int dump_config_section(CONF_SECTION *cs, int indent)
2080 /* The DEBUG macro doesn't let me
2081 * for(i=0;i<indent;++i) debugputchar('\t');
2082 * so I had to get creative. --Pac. */
2084 for (ci = cs->children; ci; ci = ci->next) {
2086 case CONF_ITEM_PAIR:
2087 cp=cf_itemtopair(ci);
2088 DEBUG("%.*s%s = %s",
2089 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2090 cp->attr, cp->value);
2093 case CONF_ITEM_SECTION:
2094 scs=cf_itemtosection(ci);
2095 DEBUG("%.*s%s %s%s{",
2096 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2098 scs->name2 ? scs->name2 : "",
2099 scs->name2 ? " " : "");
2100 dump_config_section(scs, indent+1);
2102 indent, "\t\t\t\t\t\t\t\t\t\t\t");
2105 default: /* FIXME: Do more! */
2113 int dump_config(void)
2115 return dump_config_section(mainconfig.config, 0);