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 */
96 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
97 void *data, void (*data_free)(void *),
99 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
103 * Isolate the scary casts in these tiny provably-safe functions
105 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
109 rad_assert(ci->type == CONF_ITEM_PAIR);
110 return (CONF_PAIR *)ci;
112 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
116 rad_assert(ci->type == CONF_ITEM_SECTION);
117 return (CONF_SECTION *)ci;
119 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
123 return (CONF_ITEM *)cp;
125 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
129 return (CONF_ITEM *)cs;
132 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
136 rad_assert(ci->type == CONF_ITEM_DATA);
137 return (CONF_DATA *)ci;
139 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
143 return (CONF_ITEM *)cd;
147 * Create a new CONF_PAIR
149 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
150 FR_TOKEN operator, FR_TOKEN value_type,
151 CONF_SECTION *parent)
154 size_t attr_len, value_len = 0;
157 if (!attr) return NULL;
158 attr_len = strlen(attr) + 1;
159 if (value) value_len = strlen(value) + 1;
161 p = rad_malloc(sizeof(*cp) + attr_len + value_len);
163 cp = (CONF_PAIR *) p;
164 memset(cp, 0, sizeof(*cp));
165 cp->item.type = CONF_ITEM_PAIR;
166 cp->item.parent = parent;
169 memcpy(p, attr, attr_len);
174 memcpy(p, value, value_len);
177 cp->value_type = value_type;
178 cp->operator = operator;
186 void cf_pair_free(CONF_PAIR **cp)
188 if (!cp || !*cp) return;
191 * attr && value are allocated contiguous with cp.
195 memset(*cp, 0, sizeof(*cp));
203 static void cf_data_free(CONF_DATA **cd)
205 if (!cd || !*cd) return;
207 /* name is allocated contiguous with cd */
211 ((*cd)->free)((*cd)->data);
214 memset(*cd, 0, sizeof(*cd));
221 * rbtree callback function
223 static int pair_cmp(const void *a, const void *b)
225 const CONF_PAIR *one = a;
226 const CONF_PAIR *two = b;
228 return strcmp(one->attr, two->attr);
233 * rbtree callback function
235 static int section_cmp(const void *a, const void *b)
237 const CONF_SECTION *one = a;
238 const CONF_SECTION *two = b;
240 return strcmp(one->name1, two->name1);
245 * rbtree callback function
247 static int name2_cmp(const void *a, const void *b)
249 const CONF_SECTION *one = a;
250 const CONF_SECTION *two = b;
252 rad_assert(strcmp(one->name1, two->name1) == 0);
254 if (!one->name2 && !two->name2) return 0;
255 if (!one->name2) return -1;
256 if (!two->name2) return +1;
258 return strcmp(one->name2, two->name2);
263 * rbtree callback function
265 static int data_cmp(const void *a, const void *b)
269 const CONF_DATA *one = a;
270 const CONF_DATA *two = b;
272 rcode = one->flag - two->flag;
273 if (rcode != 0) return rcode;
275 return strcmp(one->name, two->name);
280 * Free strings we've parsed into data structures.
282 void cf_section_parse_free(CONF_SECTION *cs, void *base)
285 const CONF_PARSER *variables = cs->variables;
288 * Don't automatically free the strings if we're being
289 * called from a module. This is also for clients.c,
290 * where client_free() expects to be able to free the
291 * client structure. If we moved everything to key off
292 * of the config files, we might solve some problems...
294 if (!variables) return;
297 * Free up dynamically allocated string pointers.
299 for (i = 0; variables[i].name != NULL; i++) {
302 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
303 (variables[i].type != PW_TYPE_FILENAME)) {
308 * No base struct offset, data must be the pointer.
309 * If data doesn't exist, ignore the entry, there
310 * must be something wrong.
313 if (!variables[i].data) {
317 p = (char **) variables[i].data;;
319 } else if (variables[i].data) {
320 p = (char **) variables[i].data;;
323 p = (char **) (((char *)base) + variables[i].offset);
333 * Free a CONF_SECTION
335 void cf_section_free(CONF_SECTION **cs)
337 CONF_ITEM *ci, *next;
339 if (!cs || !*cs) return;
341 cf_section_parse_free(*cs, (*cs)->base);
343 for (ci = (*cs)->children; ci; ci = next) {
347 case CONF_ITEM_PAIR: {
348 CONF_PAIR *pair = cf_itemtopair(ci);
353 case CONF_ITEM_SECTION: {
354 CONF_SECTION *section = cf_itemtosection(ci);
355 cf_section_free(§ion);
359 case CONF_ITEM_DATA: {
360 CONF_DATA *data = cf_itemtodata(ci);
365 default: /* should really be an error. */
371 * Name1 and name2 are allocated contiguous with
374 if ((*cs)->pair_tree)
375 rbtree_free((*cs)->pair_tree);
376 if ((*cs)->section_tree)
377 rbtree_free((*cs)->section_tree);
378 if ((*cs)->name2_tree)
379 rbtree_free((*cs)->name2_tree);
380 if ((*cs)->data_tree)
381 rbtree_free((*cs)->data_tree);
384 * And free the section
387 memset(*cs, 0, sizeof(*cs));
396 * Allocate a CONF_SECTION
398 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
399 CONF_SECTION *parent)
401 size_t name1_len, name2_len = 0;
405 if (!name1) return NULL;
407 name1_len = strlen(name1) + 1;
408 if (name2) name2_len = strlen(name2) + 1;
410 p = rad_malloc(sizeof(*cs) + name1_len + name2_len);
412 cs = (CONF_SECTION *) p;
413 memset(cs, 0, sizeof(*cs));
414 cs->item.type = CONF_ITEM_SECTION;
415 cs->item.parent = parent;
418 memcpy(p, name1, name1_len);
421 if (name2 && *name2) {
423 memcpy(p, name2, name2_len);
427 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
428 if (!cs->pair_tree) {
429 cf_section_free(&cs);
434 * Don't create a data tree, it may not be needed.
438 * Don't create the section tree here, it may not
442 if (parent) cs->depth = parent->depth + 1;
449 * Add an item to a configuration section.
451 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
454 rad_assert(cs->tail == NULL);
457 rad_assert(cs->tail != NULL);
462 * Update the trees (and tail) for each item added.
464 for (/* nothing */; ci != NULL; ci = ci->next) {
468 * For fast lookups, pair's and sections get
473 rbtree_insert(cs->pair_tree, ci);
476 case CONF_ITEM_SECTION: {
477 CONF_SECTION *cs_new = cf_itemtosection(ci);
479 if (!cs->section_tree) {
480 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
481 /* ignore any errors */
484 if (cs->section_tree) {
485 rbtree_insert(cs->section_tree, cs_new); }
488 * Two names: find the named instance.
491 CONF_SECTION *old_cs;
495 * CONF_SECTION having
496 * the given name1, and
500 old_cs = rbtree_finddata(cs->section_tree, cs_new);
501 if (!old_cs) return; /* this is a bad error! */
503 if (!old_cs->name2_tree) {
504 old_cs->name2_tree = rbtree_create(name2_cmp,
507 if (old_cs->name2_tree) {
508 rbtree_insert(old_cs->name2_tree, cs_new);
512 } /* was a section */
515 if (!cs->data_tree) {
516 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
519 rbtree_insert(cs->data_tree, ci);
523 default: /* FIXME: assert & error! */
526 } /* switch over conf types */
531 CONF_ITEM *cf_reference_item(const CONF_SECTION *parentcs,
532 CONF_SECTION *outercs,
537 const CONF_SECTION *cs = outercs;
541 strlcpy(name, ptr, sizeof(name));
545 * ".foo" means "foo from the current section"
551 * ..foo means "foo from the section
552 * enclosing this section" (etc.)
556 cs = cs->item.parent;
561 * "foo.bar.baz" means "from the root"
563 } else if (strchr(p, '.') != NULL) {
574 if (r && q > r) q = NULL;
575 if (q && q < r) r = NULL;
581 q = strchr(r + 1, ']');
582 if (!q) return NULL; /* parse error */
585 * Points to foo[bar]xx: parse error,
586 * it should be foo[bar] or foo[bar].baz
588 if (q[1] && q[1] != '.') goto no_such_item;
592 next = cf_section_sub_find_name2(cs, p, r + 1);
597 * Points to a named instance of a section.
600 if (!next) goto no_such_item;
601 return cf_sectiontoitem(next);
604 q++; /* ensure we skip the ']' and '.' */
608 next = cf_section_sub_find(cs, p);
612 if (!next) break; /* it MAY be a pair in this section! */
618 if (!*p) goto no_such_item;
622 * Find it in the current referenced
625 cp = cf_pair_find(cs, p);
626 if (cp) return cf_pairtoitem(cp);
628 next = cf_section_sub_find(cs, p);
629 if (next) return cf_sectiontoitem(next);
632 * "foo" is "in the current section, OR in main".
634 if ((p == name) && (cs != parentcs)) {
640 DEBUG2("WARNING: No such configuration item %s", ptr);
645 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
647 while (cs->item.parent != NULL) {
648 cs = cs->item.parent;
656 * Expand the variables in an input string.
658 static const char *cf_expand_variables(const char *cf, int *lineno,
659 CONF_SECTION *outercs,
660 char *output, const char *input)
663 const char *end, *ptr;
664 const CONF_SECTION *parentcs;
668 * Find the master parent conf section.
669 * We can't use mainconfig.config, because we're in the
670 * process of re-building it, and it isn't set up yet...
672 parentcs = cf_top_section(outercs);
678 * Ignore anything other than "${"
680 if ((*ptr == '$') && (ptr[1] == '{')) {
685 * FIXME: Add support for ${foo:-bar},
690 * Look for trailing '}', and log a
691 * warning for anything that doesn't match,
692 * and exit with a fatal error.
694 end = strchr(ptr, '}');
697 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
705 * Can't really happen because input lines are
706 * capped at 8k, which is sizeof(name)
708 if ((size_t) (end - ptr) >= sizeof(name)) {
709 radlog(L_ERR, "%s[%d]: Reference string is too large",
714 memcpy(name, ptr, end - ptr);
715 name[end - ptr] = '\0';
717 ci = cf_reference_item(parentcs, outercs, name);
718 if (!ci || (ci->type != CONF_ITEM_PAIR)) {
719 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
725 * Substitute the value of the variable.
727 cp = cf_itemtopair(ci);
728 strcpy(p, cp->value);
732 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
738 * Look for trailing '}', and log a
739 * warning for anything that doesn't match,
740 * and exit with a fatal error.
742 end = strchr(ptr, '}');
745 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
751 * Can't really happen because input lines are
752 * capped at 8k, which is sizeof(name)
754 if ((size_t) (end - ptr) >= sizeof(name)) {
755 radlog(L_ERR, "%s[%d]: Environment variable name is too large",
760 memcpy(name, ptr, end - ptr);
761 name[end - ptr] = '\0';
764 * Get the environment variable.
765 * If none exists, then make it an empty string.
779 * Copy it over verbatim.
783 } /* loop over all of the input string. */
792 * Parses an item (not a CONF_ITEM) into the specified format,
793 * with a default value.
795 * Returns -1 on error, 0 for correctly parsed, and 1 if the
796 * default value was used. Note that the default value will be
797 * used ONLY if the CONF_PAIR is NULL.
799 int cf_item_parse(CONF_SECTION *cs, const char *name,
800 int type, void *data, const char *dflt)
809 cp = cf_pair_find(cs, name);
814 return 1; /* nothing to parse, return default value */
826 case PW_TYPE_BOOLEAN:
828 * Allow yes/no and on/off
830 if ((strcasecmp(value, "yes") == 0) ||
831 (strcasecmp(value, "on") == 0)) {
833 } else if ((strcasecmp(value, "no") == 0) ||
834 (strcasecmp(value, "off") == 0)) {
838 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
841 DEBUG2("\t%s = %s", name, value);
844 case PW_TYPE_INTEGER:
845 *(int *)data = strtol(value, 0, 0);
846 DEBUG2("\t%s = %d", name, *(int *)data);
849 case PW_TYPE_STRING_PTR:
856 * Expand variables which haven't already been
857 * expanded automagically when the configuration
863 int lineno = cs->item.lineno;
866 * FIXME: sizeof(buffer)?
868 value = cf_expand_variables("?",
871 if (!value) return -1;
874 DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
875 *q = value ? strdup(value) : NULL;
879 * This is the same as PW_TYPE_STRING_PTR,
880 * except that we also "stat" the file, and
883 case PW_TYPE_FILENAME:
890 * Expand variables which haven't already been
891 * expanded automagically when the configuration
897 int lineno = cs->item.lineno;
900 * FIXME: sizeof(buffer)?
902 value = cf_expand_variables("?",
905 if (!value) return -1;
908 DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
909 *q = value ? strdup(value) : NULL;
912 * And now we "stat" the file.
917 if (stat(*q, &buf) == 0) {
920 mtime = rad_malloc(sizeof(*mtime));
921 *mtime = buf.st_mtime;
923 cf_data_add_internal(cs, *q, mtime, free,
931 * Allow '*' as any address
933 if (strcmp(value, "*") == 0) {
934 *(uint32_t *) data = htonl(INADDR_ANY);
935 DEBUG2("\t%s = *", name);
938 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
939 radlog(L_ERR, "Can't find IP address for host %s", value);
943 if (strspn(value, "0123456789.") == strlen(value)) {
944 DEBUG2("\t%s = %s", name, value);
946 DEBUG2("\t%s = %s IP address [%s]", name, value,
947 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
949 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
952 case PW_TYPE_IPV6ADDR:
953 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
954 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
957 DEBUG2("\t%s = %s IPv6 address [%s]", name, value,
958 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
959 memcpy(data, &ipaddr.ipaddr.ip6addr,
960 sizeof(ipaddr.ipaddr.ip6addr));
964 radlog(L_ERR, "type %d not supported yet", type);
967 } /* switch over variable type */
972 static const char *parse_spaces = " ";
975 * Parse a configuration section into user-supplied variables.
977 int cf_section_parse(CONF_SECTION *cs, void *base,
978 const CONF_PARSER *variables)
983 cs->variables = variables; /* this doesn't hurt anything */
986 DEBUG2("%.*s%s {", cs->depth, parse_spaces,
989 DEBUG2("%.*s%s %s {", cs->depth, parse_spaces,
990 cs->name1, cs->name2);
994 * Handle the known configuration parameters.
996 for (i = 0; variables[i].name != NULL; i++) {
998 * Handle subsections specially
1000 if (variables[i].type == PW_TYPE_SUBSECTION) {
1001 CONF_SECTION *subcs;
1002 subcs = cf_section_sub_find(cs, variables[i].name);
1005 * If the configuration section is NOT there,
1008 * FIXME! This is probably wrong... we should
1009 * probably set the items to their default values.
1011 if (!subcs) continue;
1013 if (!variables[i].dflt) {
1014 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
1018 if (cf_section_parse(subcs, base,
1019 (const CONF_PARSER *) variables[i].dflt) < 0) {
1023 } /* else it's a CONF_PAIR */
1025 if (variables[i].data) {
1026 data = variables[i].data; /* prefer this. */
1028 data = ((char *)base) + variables[i].offset;
1030 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1035 * Parse the pair we found, or a default value.
1037 if (cf_item_parse(cs, variables[i].name, variables[i].type,
1038 data, variables[i].dflt) < 0) {
1041 } /* for all variables in the configuration section */
1043 DEBUG2("%.*s}", cs->depth, parse_spaces);
1050 DEBUG2("%.*s}", cs->depth, parse_spaces);
1051 cf_section_parse_free(cs, base);
1057 * Sanity check the "if" or "elsif", presuming that the first '('
1058 * has already been eaten.
1060 * We're not really parsing it here, just checking if it's mostly
1063 static int condition_looks_ok(char **ptr)
1079 return 0; /* no trailing slash */
1091 return 0; /* no trailing slash */
1102 if (num_braces == 1) {
1103 const char *q = p + 1;
1106 * Validate that there isn't much
1107 * else after the closing brace.
1109 while ((*q == ' ') || (*q == '\t')) q++;
1118 *ptr = p + 1; /* include the trailing ')' */
1142 static const char *cf_local_file(CONF_SECTION *cs, const char *local,
1143 char *buffer, size_t bufsize)
1147 CONF_SECTION *parentcs = cf_top_section(cs);
1149 p = strrchr(parentcs->item.filename, '/');
1150 if (!p) return local;
1152 dirsize = (p - parentcs->item.filename) + 1;
1154 if ((dirsize + strlen(local)) >= bufsize) {
1158 memcpy(buffer, parentcs->item.filename, dirsize);
1159 strlcpy(buffer + dirsize, local, bufsize - dirsize);
1166 * Read a part of the config file.
1168 static int cf_section_read(const char *filename, int *lineno, FILE *fp,
1169 CONF_SECTION *current)
1172 CONF_SECTION *this, *css;
1184 this = current; /* add items here */
1187 * Read, checking for line continuations ('\\' at EOL)
1193 * Get data, and remember if we are at EOF.
1195 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1201 * We've filled the buffer, and there isn't
1204 if ((cbuf[len - 1] != '\n') && !feof(fp)) {
1205 radlog(L_ERR, "%s[%d]: Line too long",
1211 * Check for continuations.
1213 if (cbuf[len - 1] == '\n') len--;
1216 * Last character is '\\'. Over-write it,
1217 * and read another line.
1219 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1220 if (len >= (sizeof(buf) - 5)) {
1221 radlog(L_ERR, "%s[%d]: Line too long",
1226 cbuf[len - 1] = '\0';
1232 * We're at EOF, and haven't read anything. Stop.
1234 if (eof && (cbuf == buf)) {
1239 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1241 if ((*buf1 == '#') || (*buf1 == '\0')) {
1246 * The caller eats "name1 name2 {", and calls us
1247 * for the data inside of the section. So if we
1248 * receive a closing brace, then it must mean the
1249 * end of the section.
1251 if (t1 == T_RCBRACE) {
1252 if (this == current) {
1253 radlog(L_ERR, "%s[%d]: Too many closing braces",
1258 this = this->item.parent;
1263 * Allow for $INCLUDE files
1265 * This *SHOULD* work for any level include.
1266 * I really really really hate this file. -cparker
1268 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
1269 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
1272 t2 = getword(&ptr, buf2, sizeof(buf2));
1274 if (buf2[0] == '$') relative = 0;
1276 value = cf_expand_variables(filename, lineno, this, buf, buf2);
1277 if (!value) return -1;
1279 if (value[0] == '/') relative = 0;
1282 value = cf_local_file(current, value, buf3,
1285 radlog(L_ERR, "%s[%d]: Directories too deep.",
1292 #ifdef HAVE_DIRENT_H
1296 * Include ALL non-"dot" files in the directory.
1299 if (value[strlen(value) - 1] == '/') {
1302 struct stat stat_buf;
1304 DEBUG2("including files in directory %s", value );
1305 dir = opendir(value);
1307 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1308 filename, *lineno, value,
1314 * Read the directory, ignoring "." files.
1316 while ((dp = readdir(dir)) != NULL) {
1319 if (dp->d_name[0] == '.') continue;
1322 * Check for valid characters
1324 for (p = dp->d_name; *p != '\0'; p++) {
1325 if (isalpha((int)*p) ||
1329 (*p == '.')) continue;
1332 if (*p != '\0') continue;
1334 snprintf(buf2, sizeof(buf2), "%s%s",
1336 if ((stat(buf2, &stat_buf) != 0) ||
1337 S_ISDIR(stat_buf.st_mode)) continue;
1339 * Read the file into the current
1340 * configuration sectoin.
1342 if (cf_file_include(buf2, this) < 0) {
1350 { /* it was a normal file */
1351 if (buf1[1] == '-') {
1352 struct stat statbuf;
1354 if (stat(value, &statbuf) < 0) {
1355 DEBUG("WARNING: Not including file %s: %s", value, strerror(errno));
1360 if (cf_file_include(value, this) < 0) {
1365 } /* we were in an include */
1367 if (strcasecmp(buf1, "$template") == 0) {
1369 CONF_SECTION *parentcs;
1370 t2 = getword(&ptr, buf2, sizeof(buf2));
1372 parentcs = cf_top_section(current);
1374 ci = cf_reference_item(parentcs, this, buf2);
1375 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1376 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
1377 filename, *lineno, buf2);
1381 if (this->template) {
1382 radlog(L_ERR, "%s[%d]: Section already has a template",
1387 this->template = cf_itemtosection(ci);
1392 * Ensure that the user can't add CONF_PAIRs
1393 * with 'internal' names;
1395 if (buf1[0] == '_') {
1396 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1397 filename, *lineno, buf1);
1402 * Grab the next token.
1404 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1417 if (!this || (strcmp(this->name1, "update") != 0)) {
1418 radlog(L_ERR, "%s[%d]: Invalid operator in assignment",
1426 t3 = getstring(&ptr, buf3, sizeof(buf3));
1429 * Handle variable substitution via ${foo}
1431 if ((t3 == T_BARE_WORD) ||
1432 (t3 == T_DOUBLE_QUOTED_STRING)) {
1433 value = cf_expand_variables(filename, lineno, this,
1435 if (!value) return -1;
1436 } else if ((t3 == T_EOL) ||
1444 * Add this CONF_PAIR to our CONF_SECTION
1446 cpn = cf_pair_alloc(buf1, value, t2, t3, this);
1447 cpn->item.filename = filename;
1448 cpn->item.lineno = *lineno;
1449 cf_item_add(this, cf_pairtoitem(cpn));
1453 * This horrible code is here to support
1454 * if/then/else failover in the
1455 * authorize, etc. sections. It makes no
1456 * sense anywhere else.
1459 if ((strcmp(buf1, "if") == 0) ||
1460 (strcmp(buf1, "elsif") == 0)) {
1462 CONF_SECTION *server;
1464 if (!condition_looks_ok(&end)) {
1465 radlog(L_ERR, "%s[%d]: Parse error in condition at: %s",
1466 filename, *lineno, ptr);
1470 if ((size_t) (end - ptr) >= (sizeof(buf2) - 1)) {
1471 radlog(L_ERR, "%s[%d]: Statement too complicated after \"%s\"",
1472 filename, *lineno, buf1);
1477 * More sanity checking. This is
1478 * getting to be a horrible hack.
1482 if (strcmp(server->name1, "server") == 0) break;
1483 server = server->item.parent;
1487 radlog(L_ERR, "%s[%d]: Processing directives such as \"%s\" cannot be used here.",
1488 filename, *lineno, buf1);
1493 memcpy(buf2 + 1, ptr, end - ptr);
1494 buf2[end - ptr + 1] = '\0';
1500 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1501 filename, *lineno, buf1);
1508 * No '=', must be a section or sub-section.
1511 case T_DOUBLE_QUOTED_STRING:
1512 case T_SINGLE_QUOTED_STRING:
1513 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1514 if (t3 != T_LCBRACE) {
1515 radlog(L_ERR, "%s[%d]: Expecting section start brace '{' after \"%s %s\"",
1516 filename, *lineno, buf1, buf2);
1522 css = cf_section_alloc(buf1,
1523 t2 == T_LCBRACE ? NULL : buf2,
1526 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1530 cf_item_add(this, cf_sectiontoitem(css));
1531 css->item.filename = filename;
1532 css->item.lineno = *lineno;
1535 * The current section is now the child section.
1541 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1542 filename, *lineno, buf1);
1548 * See if EOF was unexpected ..
1550 if (feof(fp) && (this != current)) {
1551 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1553 cf_section_name1(this), cf_section_lineno(this));
1561 * Include one config file in another.
1563 int cf_file_include(const char *filename, CONF_SECTION *cs)
1567 struct stat statbuf;
1571 DEBUG2( "including configuration file %s", filename);
1573 if (stat(filename, &statbuf) == 0) {
1575 if ((statbuf.st_mode & S_IWOTH) != 0) {
1576 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1583 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1584 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1591 fp = fopen(filename, "r");
1593 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1594 filename, strerror(errno));
1599 * Add the filename to the section
1601 mtime = rad_malloc(sizeof(*mtime));
1602 *mtime = statbuf.st_mtime;
1604 if (cf_data_add_internal(cs, filename, mtime, free,
1605 PW_TYPE_FILENAME) < 0) {
1606 radlog(L_ERR|L_CONS, "Internal error open file \"%s\"",
1611 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILENAME);
1613 radlog(L_ERR|L_CONS, "Internal error open file \"%s\"",
1618 if (!cs->item.filename) cs->item.filename = filename;
1621 * Read the section. It's OK to have EOF without a
1622 * matching close brace.
1624 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
1634 * Bootstrap a config file.
1636 CONF_SECTION *cf_file_read(const char *filename)
1642 cs = cf_section_alloc("main", NULL, NULL);
1643 if (!cs) return NULL;
1645 cp = cf_pair_alloc("confdir", filename, T_OP_SET, T_BARE_WORD, cs);
1646 if (!cp) return NULL;
1648 p = strrchr(cp->value, '/');
1651 cp->item.filename = "internal";
1652 cp->item.lineno = 0;
1653 cf_item_add(cs, cf_pairtoitem(cp));
1655 if (cf_file_include(filename, cs) < 0) {
1656 cf_section_free(&cs);
1664 * Return a CONF_PAIR within a CONF_SECTION.
1666 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1669 CONF_PAIR *cp = NULL;
1671 if (!cs) return NULL;
1674 * Find the name in the tree, for speed.
1680 cp = rbtree_finddata(cs->pair_tree, &mycp);
1683 * Else find the first one that matches
1685 for (ci = cs->children; ci; ci = ci->next) {
1686 if (ci->type == CONF_ITEM_PAIR) {
1687 return cf_itemtopair(ci);
1692 if (cp || !cs->template) return cp;
1694 return cf_pair_find(cs->template, name);
1698 * Return the attr of a CONF_PAIR
1701 const char *cf_pair_attr(CONF_PAIR *pair)
1703 return (pair ? pair->attr : NULL);
1707 * Return the value of a CONF_PAIR
1710 const char *cf_pair_value(CONF_PAIR *pair)
1712 return (pair ? pair->value : NULL);
1716 * Copied here for error reporting.
1718 extern void librad_log(const char *, ...);
1721 * Turn a CONF_PAIR into a VALUE_PAIR
1722 * For now, ignore the "value_type" field...
1724 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
1730 librad_log("Internal error");
1734 da = dict_attrbyname(pair->attr);
1736 librad_log("Unknown attribute %s", pair->attr);
1741 librad_log("No value given for attribute %s", pair->attr);
1747 librad_log("Out of memory");
1751 vp->operator = pair->operator;
1753 if ((pair->value_type == T_BARE_WORD) ||
1754 (pair->value_type == T_SINGLE_QUOTED_STRING)) {
1755 if (!pairparsevalue(vp, pair->value)) {
1759 vp->flags.do_xlat = 0;
1761 vp->flags.do_xlat = 1;
1768 * Return the first label of a CONF_SECTION
1771 const char *cf_section_name1(const CONF_SECTION *cs)
1773 return (cs ? cs->name1 : NULL);
1777 * Return the second label of a CONF_SECTION
1780 const char *cf_section_name2(const CONF_SECTION *cs)
1782 return (cs ? cs->name2 : NULL);
1786 * Find a value in a CONF_SECTION
1788 const char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1792 cp = cf_pair_find(cs, attr);
1794 return (cp ? cp->value : NULL);
1798 * Return the next pair after a CONF_PAIR
1799 * with a certain name (char *attr) If the requested
1800 * attr is NULL, any attr matches.
1803 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1804 CONF_PAIR *pair, const char *attr)
1809 * If pair is NULL this must be a first time run
1810 * Find the pair with correct name
1814 return cf_pair_find(cs, attr);
1817 ci = cf_pairtoitem(pair)->next;
1819 for (; ci; ci = ci->next) {
1820 if (ci->type != CONF_ITEM_PAIR)
1822 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1826 return cf_itemtopair(ci);
1830 * Find a CONF_SECTION, or return the root if name is NULL
1833 CONF_SECTION *cf_section_find(const char *name)
1836 return cf_section_sub_find(mainconfig.config, name);
1838 return mainconfig.config;
1842 * Find a sub-section in a section
1845 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1850 * Do the fast lookup if possible.
1852 if (name && cs->section_tree) {
1857 return rbtree_finddata(cs->section_tree, &mycs);
1860 for (ci = cs->children; ci; ci = ci->next) {
1861 if (ci->type != CONF_ITEM_SECTION)
1863 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1867 return cf_itemtosection(ci);
1873 * Find a CONF_SECTION with both names.
1875 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1876 const char *name1, const char *name2)
1880 if (!cs) cs = mainconfig.config;
1882 if (name1 && (cs->section_tree)) {
1883 CONF_SECTION mycs, *master_cs;
1888 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1890 return rbtree_finddata(master_cs->name2_tree, &mycs);
1895 * Else do it the old-fashioned way.
1897 for (ci = cs->children; ci; ci = ci->next) {
1898 CONF_SECTION *subcs;
1900 if (ci->type != CONF_ITEM_SECTION)
1903 subcs = cf_itemtosection(ci);
1905 if (!subcs->name2) {
1906 if (strcmp(subcs->name1, name2) == 0) break;
1908 if (strcmp(subcs->name2, name2) == 0) break;
1910 continue; /* don't do the string comparisons below */
1913 if ((strcmp(subcs->name1, name1) == 0) &&
1914 (subcs->name2 != NULL) &&
1915 (strcmp(subcs->name2, name2) == 0))
1919 return cf_itemtosection(ci);
1923 * Return the next subsection after a CONF_SECTION
1924 * with a certain name1 (char *name1). If the requested
1925 * name1 is NULL, any name1 matches.
1928 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1929 CONF_SECTION *subsection,
1935 * If subsection is NULL this must be a first time run
1936 * Find the subsection with correct name
1939 if (subsection == NULL){
1940 ci = section->children;
1942 ci = cf_sectiontoitem(subsection)->next;
1945 for (; ci; ci = ci->next) {
1946 if (ci->type != CONF_ITEM_SECTION)
1948 if ((name1 == NULL) ||
1949 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1953 return cf_itemtosection(ci);
1958 * Return the next section after a CONF_SECTION
1959 * with a certain name1 (char *name1). If the requested
1960 * name1 is NULL, any name1 matches.
1963 CONF_SECTION *cf_section_find_next(CONF_SECTION *section,
1964 CONF_SECTION *subsection,
1967 if (!section->item.parent) return NULL;
1969 return cf_subsection_find_next(section->item.parent, subsection, name1);
1973 * Return the next item after a CONF_ITEM.
1976 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1979 * If item is NULL this must be a first time run
1980 * Return the first item
1984 return section->children;
1990 int cf_section_lineno(CONF_SECTION *section)
1992 return cf_sectiontoitem(section)->lineno;
1995 const char *cf_pair_filename(CONF_PAIR *pair)
1997 return cf_pairtoitem(pair)->filename;
2000 const char *cf_section_filename(CONF_SECTION *section)
2002 return cf_sectiontoitem(section)->filename;
2005 int cf_pair_lineno(CONF_PAIR *pair)
2007 return cf_pairtoitem(pair)->lineno;
2010 int cf_item_is_section(CONF_ITEM *item)
2012 return item->type == CONF_ITEM_SECTION;
2014 int cf_item_is_pair(CONF_ITEM *item)
2016 return item->type == CONF_ITEM_PAIR;
2020 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
2021 void *data, void (*data_free)(void *))
2027 name_len = strlen(name) + 1;
2029 p = rad_malloc(sizeof(*cd) + name_len);
2030 cd = (CONF_DATA *) p;
2031 memset(cd, 0, sizeof(*cd));
2033 cd->item.type = CONF_ITEM_DATA;
2034 cd->item.parent = parent;
2036 cd->free = data_free;
2039 memcpy(p, name, name_len);
2045 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
2048 if (!cs || !name) return NULL;
2051 * Find the name in the tree, for speed.
2053 if (cs->data_tree) {
2058 return rbtree_finddata(cs->data_tree, &mycd);
2065 * Find data from a particular section.
2067 void *cf_data_find(CONF_SECTION *cs, const char *name)
2069 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
2071 if (cd) return cd->data;
2077 * Add named data to a configuration section.
2079 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
2080 void *data, void (*data_free)(void *),
2085 if (!cs || !name) return -1;
2088 * Already exists. Can't add it.
2090 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
2092 cd = cf_data_alloc(cs, name, data, data_free);
2096 cf_item_add(cs, cf_datatoitem(cd));
2102 * Add named data to a configuration section.
2104 int cf_data_add(CONF_SECTION *cs, const char *name,
2105 void *data, void (*data_free)(void *))
2107 return cf_data_add_internal(cs, name, data, data_free, 0);
2112 * Copy CONF_DATA from src to dst
2114 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
2117 CONF_ITEM *cd, *next, **last;
2120 * Don't check if s->data_tree is NULL. It's child
2121 * sections may have data, even if this section doesn't.
2124 rad_assert(d->data_tree == NULL);
2125 d->data_tree = s->data_tree;
2126 s->data_tree = NULL;
2129 * Walk through src, moving CONF_ITEM_DATA
2132 last = &(s->children);
2133 for (cd = s->children; cd != NULL; cd = next) {
2137 * Recursively copy data from child sections.
2139 if (cd->type == CONF_ITEM_SECTION) {
2140 CONF_SECTION *s1, *d1;
2142 s1 = cf_itemtosection(cd);
2143 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
2145 cf_section_copy_data(s1, d1);
2152 * Not conf data, remember last ptr.
2154 if (cd->type != CONF_ITEM_DATA) {
2160 * Remove it from the src list
2166 * Add it to the dst list
2169 rad_assert(d->tail == NULL);
2172 rad_assert(d->tail != NULL);
2180 * For a CONF_DATA element, stat the filename, if necessary.
2182 static int filename_stat(void *context, void *data)
2185 CONF_DATA *cd = data;
2187 context = context; /* -Wunused */
2189 if (cd->flag != PW_TYPE_FILENAME) return 0;
2191 if (stat(cd->name, &buf) < 0) return -1;
2193 if (buf.st_mtime != *(time_t *) cd->data) return -1;
2200 * Compare two CONF_SECTIONS. The items MUST be in the same
2203 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
2205 CONF_ITEM *ca = a->children;
2206 CONF_ITEM *cb = b->children;
2214 if (!ca && !cb) break;
2219 if (ca && ca->type == CONF_ITEM_DATA) {
2223 if (cb && cb->type == CONF_ITEM_DATA) {
2229 * One is smaller than the other. Exit.
2231 if (!ca || !cb) return 0;
2233 if (ca->type != cb->type) return 0;
2236 * Deal with subsections.
2238 if (ca->type == CONF_ITEM_SECTION) {
2239 CONF_SECTION *sa = cf_itemtosection(ca);
2240 CONF_SECTION *sb = cf_itemtosection(cb);
2242 if (!cf_section_cmp(sa, sb)) return 0;
2246 rad_assert(ca->type == CONF_ITEM_PAIR);
2248 pa = cf_itemtopair(ca);
2249 pb = cf_itemtopair(cb);
2252 * Different attr and/or value, Exit.
2254 if ((strcmp(pa->attr, pb->attr) != 0) ||
2255 (strcmp(pa->value, pb->value) != 0)) return 0;
2259 * And go to the next element.
2267 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
2270 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
2275 * They must be the same, say so.
2282 * Migrate CONF_DATA from one section to another.
2284 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
2287 CONF_SECTION *s, *d;
2289 for (ci = src->children; ci != NULL; ci = ci->next) {
2290 if (ci->type != CONF_ITEM_SECTION)
2293 s = cf_itemtosection(ci);
2294 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
2296 if (!d) continue; /* not in new one, don't migrate it */
2299 * A section of the same name is in BOTH src & dst,
2300 * compare the CONF_PAIR's. If they're all the same,
2301 * then copy the CONF_DATA from one to the other.
2303 if (cf_section_cmp(s, d)) {
2304 cf_section_copy_data(s, d);
2308 return 1; /* rcode means anything? */
2311 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
2313 if (!cs || !template || cs->template || template->template) return -1;
2315 cs->template = template;
2322 * This is here to make the rest of the code easier to read. It
2323 * ties conffile.c to log.c, but it means we don't have to
2326 void cf_log_err(CONF_ITEM *ci, const char *fmt, ...)
2332 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2335 radlog(L_ERR, "%s[%d]: %s", ci->filename, ci->lineno, buffer);
2341 * JMG dump_config tries to dump the config structure in a readable format
2345 static int dump_config_section(CONF_SECTION *cs, int indent)
2351 /* The DEBUG macro doesn't let me
2352 * for(i=0;i<indent;++i) debugputchar('\t');
2353 * so I had to get creative. --Pac. */
2355 for (ci = cs->children; ci; ci = ci->next) {
2357 case CONF_ITEM_PAIR:
2358 cp=cf_itemtopair(ci);
2359 DEBUG("%.*s%s = %s",
2360 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2361 cp->attr, cp->value);
2364 case CONF_ITEM_SECTION:
2365 scs=cf_itemtosection(ci);
2366 DEBUG("%.*s%s %s%s{",
2367 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2369 scs->name2 ? scs->name2 : "",
2370 scs->name2 ? " " : "");
2371 dump_config_section(scs, indent+1);
2373 indent, "\t\t\t\t\t\t\t\t\t\t\t");
2376 default: /* FIXME: Do more! */
2384 int dump_config(CONF_SECTION *cs)
2386 return dump_config_section(cs, 0);