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;
64 LRAD_TOKEN value_type;
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 LRAD_TOKEN operator, LRAD_TOKEN value_type,
151 CONF_SECTION *parent)
155 cp = rad_malloc(sizeof(*cp));
156 memset(cp, 0, sizeof(*cp));
157 cp->item.type = CONF_ITEM_PAIR;
158 cp->item.parent = parent;
159 cp->attr = strdup(attr);
160 if (value) cp->value = strdup(value);
161 cp->value_type = value_type;
162 cp->operator = operator;
170 void cf_pair_free(CONF_PAIR **cp)
172 if (!cp || !*cp) return;
180 memset(*cp, 0, sizeof(*cp));
188 static void cf_data_free(CONF_DATA **cd)
190 if (!cd || !*cd) return;
192 if ((*cd)->flag != 0) free((*cd)->name);
196 ((*cd)->free)((*cd)->data);
199 memset(*cd, 0, sizeof(*cd));
206 * rbtree callback function
208 static int pair_cmp(const void *a, const void *b)
210 const CONF_PAIR *one = a;
211 const CONF_PAIR *two = b;
213 return strcmp(one->attr, two->attr);
218 * rbtree callback function
220 static int section_cmp(const void *a, const void *b)
222 const CONF_SECTION *one = a;
223 const CONF_SECTION *two = b;
225 return strcmp(one->name1, two->name1);
230 * rbtree callback function
232 static int name2_cmp(const void *a, const void *b)
234 const CONF_SECTION *one = a;
235 const CONF_SECTION *two = b;
237 rad_assert(strcmp(one->name1, two->name1) == 0);
239 if (!one->name2 && !two->name2) return 0;
240 if (!one->name2) return -1;
241 if (!two->name2) return +1;
243 return strcmp(one->name2, two->name2);
248 * rbtree callback function
250 static int data_cmp(const void *a, const void *b)
254 const CONF_DATA *one = a;
255 const CONF_DATA *two = b;
257 rcode = one->flag - two->flag;
258 if (rcode != 0) return rcode;
260 return strcmp(one->name, two->name);
265 * Free strings we've parsed into data structures.
267 static void cf_section_parse_free(void *base, const CONF_PARSER *variables)
272 * Don't automatically free the strings if we're being
273 * called from a module. This is also for clients.c,
274 * where client_free() expects to be able to free the
275 * client structure. If we moved everything to key off
276 * of the config files, we might solve some problems...
278 if (!variables) return;
281 * Free up dynamically allocated string pointers.
283 for (i = 0; variables[i].name != NULL; i++) {
286 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
287 (variables[i].type != PW_TYPE_FILENAME)) {
292 * No base struct offset, data must be the pointer.
293 * If data doesn't exist, ignore the entry, there
294 * must be something wrong.
297 if (!variables[i].data) {
301 p = (char **) variables[i].data;;
303 } else if (variables[i].data) {
304 p = (char **) variables[i].data;;
307 p = (char **) (((char *)base) + variables[i].offset);
317 * Free a CONF_SECTION
319 void cf_section_free(CONF_SECTION **cs)
321 CONF_ITEM *ci, *next;
323 if (!cs || !*cs) return;
325 if ((*cs)->variables) {
326 cf_section_parse_free((*cs)->base, (*cs)->variables);
329 for (ci = (*cs)->children; ci; ci = next) {
333 case CONF_ITEM_PAIR: {
334 CONF_PAIR *pair = cf_itemtopair(ci);
339 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 static const CONF_ITEM *cf_reference_item(const CONF_SECTION *parentcs,
513 const CONF_SECTION *outercs,
517 const CONF_SECTION *cs = outercs, *next;
521 strlcpy(name, ptr, sizeof(name));
525 * ".foo" means "foo from the current section"
531 * ..foo means "foo from the section
532 * enclosing this section" (etc.)
536 cs = cs->item.parent;
541 * "foo.bar.baz" means "from the root"
543 } else if (strchr(p, '.') != NULL) {
548 char *q = strchr(p, '.');
553 next = cf_section_sub_find(cs, p);
556 if (!next) break; /* it MAY be a pair in this section! */
562 if (!*p) return NULL;
566 * Find it in the current referenced
569 cp = cf_pair_find(cs, p);
570 if (cp) return cf_pairtoitem(cp);
572 next = cf_section_sub_find(cs, p);
573 if (next) return cf_sectiontoitem(next);
576 * "foo" is "in the current section, OR in main".
578 if ((p == name) && (cs != parentcs)) {
587 CONF_SECTION *cf_top_section(const CONF_SECTION *cs)
589 while (cs->item.parent != NULL) {
590 cs = cs->item.parent;
598 * Expand the variables in an input string.
600 static const char *cf_expand_variables(const char *cf, int *lineno,
601 const CONF_SECTION *outercs,
602 char *output, const char *input)
605 const char *end, *ptr;
606 const CONF_SECTION *parentcs;
610 * Find the master parent conf section.
611 * We can't use mainconfig.config, because we're in the
612 * process of re-building it, and it isn't set up yet...
614 parentcs = cf_top_section(outercs);
620 * Ignore anything other than "${"
622 if ((*ptr == '$') && (ptr[1] == '{')) {
627 * FIXME: Add support for ${foo:-bar},
632 * Look for trailing '}', and log a
633 * warning for anything that doesn't match,
634 * and exit with a fatal error.
636 end = strchr(ptr, '}');
639 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
647 * Can't really happen because input lines are
648 * capped at 8k, which is sizeof(name)
650 if ((end - ptr) >= sizeof(name)) {
651 radlog(L_ERR, "%s[%d]: Reference string is too large",
656 memcpy(name, ptr, end - ptr);
657 name[end - ptr] = '\0';
659 ci = cf_reference_item(parentcs, outercs, name);
660 if (!ci || (ci->type != CONF_ITEM_PAIR)) {
661 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
667 * Substitute the value of the variable.
669 cp = cf_itemtopair(ci);
670 strcpy(p, cp->value);
674 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
680 * Look for trailing '}', and log a
681 * warning for anything that doesn't match,
682 * and exit with a fatal error.
684 end = strchr(ptr, '}');
687 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
693 * Can't really happen because input lines are
694 * capped at 8k, which is sizeof(name)
696 if ((end - ptr) >= sizeof(name)) {
697 radlog(L_ERR, "%s[%d]: Environment variable name is too large",
702 memcpy(name, ptr, end - ptr);
703 name[end - ptr] = '\0';
706 * Get the environment variable.
707 * If none exists, then make it an empty string.
721 * Copy it over verbatim.
725 } /* loop over all of the input string. */
734 * Parses an item (not a CONF_ITEM) into the specified format,
735 * with a default value.
737 * Returns -1 on error, 0 for correctly parsed, and 1 if the
738 * default value was used. Note that the default value will be
739 * used ONLY if the CONF_PAIR is NULL.
741 int cf_item_parse(CONF_SECTION *cs, const char *name,
742 int type, void *data, const char *dflt)
747 lrad_ipaddr_t ipaddr;
751 cp = cf_pair_find(cs, name);
756 return 1; /* nothing to parse, return default value */
768 case PW_TYPE_BOOLEAN:
770 * Allow yes/no and on/off
772 if ((strcasecmp(value, "yes") == 0) ||
773 (strcasecmp(value, "on") == 0)) {
775 } else if ((strcasecmp(value, "no") == 0) ||
776 (strcasecmp(value, "off") == 0)) {
780 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
783 DEBUG2("\t%s = %s", name, value);
786 case PW_TYPE_INTEGER:
787 *(int *)data = strtol(value, 0, 0);
788 DEBUG2("\t%s = %d", name, *(int *)data);
791 case PW_TYPE_STRING_PTR:
798 * Expand variables which haven't already been
799 * expanded automagically when the configuration
805 int lineno = cs->item.lineno;
808 * FIXME: sizeof(buffer)?
810 value = cf_expand_variables("?",
813 if (!value) return -1;
816 DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
817 *q = value ? strdup(value) : NULL;
821 * This is the same as PW_TYPE_STRING_PTR,
822 * except that we also "stat" the file, and
825 case PW_TYPE_FILENAME:
832 * Expand variables which haven't already been
833 * expanded automagically when the configuration
839 int lineno = cs->item.lineno;
842 * FIXME: sizeof(buffer)?
844 value = cf_expand_variables("?",
847 if (!value) return -1;
850 DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
851 *q = value ? strdup(value) : NULL;
854 * And now we "stat" the file.
859 if (stat(*q, &buf) == 0) {
862 mtime = rad_malloc(sizeof(*mtime));
863 *mtime = buf.st_mtime;
865 cf_data_add_internal(cs, *q, mtime, free,
873 * Allow '*' as any address
875 if (strcmp(value, "*") == 0) {
876 *(uint32_t *) data = htonl(INADDR_ANY);
877 DEBUG2("\t%s = *", name);
880 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
881 radlog(L_ERR, "Can't find IP address for host %s", value);
885 if (strspn(value, "0123456789.") == strlen(value)) {
886 DEBUG2("\t%s = %s", name, value);
888 DEBUG2("\t%s = %s IP address [%s]", name, value,
889 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
891 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
894 case PW_TYPE_IPV6ADDR:
895 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
896 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
899 DEBUG2("\t%s = %s IPv6 address [%s]", name, value,
900 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
901 memcpy(data, &ipaddr.ipaddr.ip6addr,
902 sizeof(ipaddr.ipaddr.ip6addr));
906 radlog(L_ERR, "type %d not supported yet", type);
909 } /* switch over variable type */
914 static const char *parse_spaces = " ";
917 * Parse a configuration section into user-supplied variables.
919 int cf_section_parse(CONF_SECTION *cs, void *base,
920 const CONF_PARSER *variables)
926 DEBUG2("%.*s%s {", cs->depth, parse_spaces,
929 DEBUG2("%.*s%s %s {", cs->depth, parse_spaces,
930 cs->name1, cs->name2);
934 * Handle the known configuration parameters.
936 for (i = 0; variables[i].name != NULL; i++) {
938 * Handle subsections specially
940 if (variables[i].type == PW_TYPE_SUBSECTION) {
941 const CONF_SECTION *subcs;
942 subcs = cf_section_sub_find(cs, variables[i].name);
945 * If the configuration section is NOT there,
948 * FIXME! This is probably wrong... we should
949 * probably set the items to their default values.
951 if (!subcs) continue;
953 if (!variables[i].dflt) {
954 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
958 if (cf_section_parse(subcs, base,
959 (const CONF_PARSER *) variables[i].dflt) < 0) {
963 } /* else it's a CONF_PAIR */
965 if (variables[i].data) {
966 data = variables[i].data; /* prefer this. */
968 data = ((char *)base) + variables[i].offset;
970 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
975 * Parse the pair we found, or a default value.
977 if (cf_item_parse(cs, variables[i].name, variables[i].type,
978 data, variables[i].dflt) < 0) {
981 } /* for all variables in the configuration section */
983 DEBUG2("%.*s}", cs->depth, parse_spaces);
986 cs->variables = variables;
991 DEBUG2("%.*s}", cs->depth, parse_spaces);
992 cf_section_parse_free(base, variables);
998 * Sanity check the "if" or "elsif", presuming that the first '('
999 * has already been eaten.
1001 * We're not really parsing it here, just checking if it's mostly
1004 static int condition_looks_ok(const char **ptr)
1008 const char *p = *ptr;
1020 return 0; /* no trailing slash */
1032 return 0; /* no trailing slash */
1043 if (num_braces == 1) {
1044 const char *q = p + 1;
1047 * Validate that there isn't much
1048 * else after the closing brace.
1050 while ((*q == ' ') || (*q == '\t')) q++;
1059 *ptr = p + 1; /* include the trailing ')' */
1083 static const char *cf_local_file(CONF_SECTION *cs, const char *local,
1084 char *buffer, size_t bufsize)
1088 CONF_SECTION *parentcs = cf_top_section(cs);
1090 p = strrchr(parentcs->item.filename, '/');
1091 if (!p) return local;
1093 dirsize = (p - parentcs->item.filename);
1095 if ((dirsize + strlen(local)) >= bufsize) {
1099 memcpy(buffer, parentcs->item.filename, dirsize);
1100 strlcpy(buffer + dirsize, local, bufsize - dirsize);
1107 * Read a part of the config file.
1109 static int cf_section_read(const char *filename, int *lineno, FILE *fp,
1110 CONF_SECTION *current)
1113 CONF_SECTION *this, *css;
1125 this = current; /* add items here */
1128 * Read, checking for line continuations ('\\' at EOL)
1134 * Get data, and remember if we are at EOF.
1136 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1142 * We've filled the buffer, and there isn't
1145 if ((cbuf[len - 1] != '\n') && !feof(fp)) {
1146 radlog(L_ERR, "%s[%d]: Line too long",
1152 * Check for continuations.
1154 if (cbuf[len - 1] == '\n') len--;
1157 * Last character is '\\'. Over-write it,
1158 * and read another line.
1160 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1161 if (len >= (sizeof(buf) - 5)) {
1162 radlog(L_ERR, "%s[%d]: Line too long",
1167 cbuf[len - 1] = '\0';
1173 * We're at EOF, and haven't read anything. Stop.
1175 if (eof && (cbuf == buf)) {
1180 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1182 if ((*buf1 == '#') || (*buf1 == '\0')) {
1187 * The caller eats "name1 name2 {", and calls us
1188 * for the data inside of the section. So if we
1189 * receive a closing brace, then it must mean the
1190 * end of the section.
1192 if (t1 == T_RCBRACE) {
1193 if (this == current) {
1194 radlog(L_ERR, "%s[%d]: Too many closing braces",
1199 this = this->item.parent;
1204 * Allow for $INCLUDE files
1206 * This *SHOULD* work for any level include.
1207 * I really really really hate this file. -cparker
1209 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
1210 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
1211 t2 = getword(&ptr, buf2, sizeof(buf2));
1213 value = cf_expand_variables(filename, lineno, this, buf, buf2);
1214 if (!value) return -1;
1216 if (value[0] != '/') {
1217 value = cf_local_file(current, value, buf3,
1220 radlog(L_ERR, "%s[%d]: Directories too deep.",
1227 #ifdef HAVE_DIRENT_H
1231 * Include ALL non-"dot" files in the directory.
1234 if (value[strlen(value) - 1] == '/') {
1237 struct stat stat_buf;
1239 DEBUG2("including files in directory %s", value );
1240 dir = opendir(value);
1242 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1243 filename, *lineno, value,
1249 * Read the directory, ignoring "." files.
1251 while ((dp = readdir(dir)) != NULL) {
1254 if (dp->d_name[0] == '.') continue;
1257 * Check for valid characters
1259 for (p = dp->d_name; *p != '\0'; p++) {
1260 if (isalpha((int)*p) ||
1264 (*p == '.')) continue;
1267 if (*p != '\0') continue;
1269 snprintf(buf2, sizeof(buf2), "%s%s",
1271 if ((stat(buf2, &stat_buf) != 0) ||
1272 S_ISDIR(stat_buf.st_mode)) continue;
1274 * Read the file into the current
1275 * configuration sectoin.
1277 if (cf_file_include(buf2, this) < 0) {
1285 { /* it was a normal file */
1286 if (buf1[1] == '-') {
1287 struct stat statbuf;
1289 if (stat(value, &statbuf) < 0) {
1290 DEBUG("WARNING: Not including file %s: %s", value, strerror(errno));
1295 if (cf_file_include(value, this) < 0) {
1300 } /* we were in an include */
1302 if (strcasecmp(buf1, "$template") == 0) {
1304 CONF_SECTION *parentcs;
1305 t2 = getword(&ptr, buf2, sizeof(buf2));
1307 parentcs = cf_top_section(current);
1309 ci = cf_reference_item(parentcs, this, buf2);
1310 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1311 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
1312 filename, *lineno, buf2);
1316 if (this->template) {
1317 radlog(L_ERR, "%s[%d]: Section already has a template",
1322 this->template = cf_itemtosection(ci);
1327 * Ensure that the user can't add CONF_PAIRs
1328 * with 'internal' names;
1330 if (buf1[0] == '_') {
1331 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1332 filename, *lineno, buf1);
1337 * Grab the next token.
1339 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1352 if (!this || (strcmp(this->name1, "update") != 0)) {
1353 radlog(L_ERR, "%s[%d]: Invalid operator in assignment",
1361 t3 = getstring(&ptr, buf3, sizeof(buf3));
1364 * Handle variable substitution via ${foo}
1366 if ((t3 == T_BARE_WORD) ||
1367 (t3 == T_DOUBLE_QUOTED_STRING)) {
1368 value = cf_expand_variables(filename, lineno, this,
1370 if (!value) return -1;
1371 } else if ((t3 == T_EOL) ||
1379 * Add this CONF_PAIR to our CONF_SECTION
1381 cpn = cf_pair_alloc(buf1, value, t2, t3, this);
1382 cpn->item.filename = filename;
1383 cpn->item.lineno = *lineno;
1384 cf_item_add(this, cf_pairtoitem(cpn));
1388 * This horrible code is here to support
1389 * if/then/else failover in the
1390 * authorize, etc. sections. It makes no
1391 * sense anywhere else.
1394 if ((strcmp(buf1, "if") == 0) ||
1395 (strcmp(buf1, "elsif") == 0)) {
1396 const char *end = ptr;
1397 CONF_SECTION *server;
1399 if (!condition_looks_ok(&end)) {
1400 radlog(L_ERR, "%s[%d]: Parse error in condition at: %s",
1401 filename, *lineno, ptr);
1405 if ((end - ptr) >= (sizeof(buf2) - 1)) {
1406 radlog(L_ERR, "%s[%d]: Statement too complicated after \"%s\"",
1407 filename, *lineno, buf1);
1412 * More sanity checking. This is
1413 * getting to be a horrible hack.
1417 if (strcmp(server->name1, "server") == 0) break;
1418 server = server->item.parent;
1422 radlog(L_ERR, "%s[%d]: Processing directives such as \"%s\" cannot be used here.",
1423 filename, *lineno, buf1);
1428 memcpy(buf2 + 1, ptr, end - ptr);
1429 buf2[end - ptr + 1] = '\0';
1435 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1436 filename, *lineno, buf1);
1443 * No '=', must be a section or sub-section.
1446 case T_DOUBLE_QUOTED_STRING:
1447 case T_SINGLE_QUOTED_STRING:
1448 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1449 if (t3 != T_LCBRACE) {
1450 radlog(L_ERR, "%s[%d]: Expecting section start brace '{' after \"%s %s\"",
1451 filename, *lineno, buf1, buf2);
1457 css = cf_section_alloc(buf1,
1458 t2 == T_LCBRACE ? NULL : buf2,
1461 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1465 cf_item_add(this, cf_sectiontoitem(css));
1466 css->item.filename = filename;
1467 css->item.lineno = *lineno;
1470 * The current section is now the child section.
1476 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1477 filename, *lineno, buf1);
1483 * See if EOF was unexpected ..
1485 if (feof(fp) && (this != current)) {
1486 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1488 cf_section_name1(this), cf_section_lineno(this));
1496 * Include one config file in another.
1498 int cf_file_include(const char *filename, CONF_SECTION *cs)
1502 struct stat statbuf;
1506 DEBUG2( "including configuration file %s", filename);
1508 if (stat(filename, &statbuf) == 0) {
1510 if ((statbuf.st_mode & S_IWOTH) != 0) {
1511 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1518 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1519 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1526 fp = fopen(filename, "r");
1528 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1529 filename, strerror(errno));
1534 * Add the filename to the section
1536 mtime = rad_malloc(sizeof(*mtime));
1537 *mtime = statbuf.st_mtime;
1539 if (cf_data_add_internal(cs, filename, mtime, free,
1540 PW_TYPE_FILENAME) < 0) {
1541 radlog(L_ERR|L_CONS, "Internal error open file \"%s\"",
1546 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILENAME);
1548 radlog(L_ERR|L_CONS, "Internal error open file \"%s\"",
1553 if (!cs->item.filename) cs->item.filename = filename;
1556 * Read the section. It's OK to have EOF without a
1557 * matching close brace.
1559 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
1569 * Bootstrap a config file.
1571 CONF_SECTION *cf_file_read(const char *filename)
1575 cs = cf_section_alloc("main", NULL, NULL);
1576 if (!cs) return NULL;
1578 if (cf_file_include(filename, cs) < 0) {
1579 cf_section_free(&cs);
1587 * Return a CONF_PAIR within a CONF_SECTION.
1589 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1592 CONF_PAIR *cp = NULL;
1594 if (!cs) return NULL;
1597 * Find the name in the tree, for speed.
1603 cp = rbtree_finddata(cs->pair_tree, &mycp);
1606 * Else find the first one that matches
1608 for (ci = cs->children; ci; ci = ci->next) {
1609 if (ci->type == CONF_ITEM_PAIR) {
1610 return cf_itemtopair(ci);
1615 if (cp || !cs->template) return cp;
1617 return cf_pair_find(cs->template, name);
1621 * Return the attr of a CONF_PAIR
1624 char *cf_pair_attr(CONF_PAIR *pair)
1626 return (pair ? pair->attr : NULL);
1630 * Return the value of a CONF_PAIR
1633 char *cf_pair_value(CONF_PAIR *pair)
1635 return (pair ? pair->value : NULL);
1639 * Copied here for error reporting.
1641 extern void librad_log(const char *, ...);
1644 * Turn a CONF_PAIR into a VALUE_PAIR
1645 * For now, ignore the "value_type" field...
1647 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
1653 librad_log("Internal error");
1657 da = dict_attrbyname(pair->attr);
1659 librad_log("Unknown attribute %s", pair->attr);
1664 librad_log("No value given for attribute %s", pair->attr);
1670 librad_log("Out of memory");
1674 vp->operator = pair->operator;
1676 if ((pair->value_type == T_BARE_WORD) ||
1677 (pair->value_type == T_SINGLE_QUOTED_STRING)) {
1678 if (!pairparsevalue(vp, pair->value)) {
1682 vp->flags.do_xlat = 0;
1684 vp->flags.do_xlat = 1;
1691 * Return the first label of a CONF_SECTION
1694 const char *cf_section_name1(const CONF_SECTION *cs)
1696 return (cs ? cs->name1 : NULL);
1700 * Return the second label of a CONF_SECTION
1703 const char *cf_section_name2(const CONF_SECTION *cs)
1705 return (cs ? cs->name2 : NULL);
1709 * Find a value in a CONF_SECTION
1711 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1715 cp = cf_pair_find(cs, attr);
1717 return (cp ? cp->value : NULL);
1721 * Return the next pair after a CONF_PAIR
1722 * with a certain name (char *attr) If the requested
1723 * attr is NULL, any attr matches.
1726 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1727 const CONF_PAIR *pair, const char *attr)
1732 * If pair is NULL this must be a first time run
1733 * Find the pair with correct name
1737 return cf_pair_find(cs, attr);
1740 ci = cf_pairtoitem(pair)->next;
1742 for (; ci; ci = ci->next) {
1743 if (ci->type != CONF_ITEM_PAIR)
1745 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1749 return cf_itemtopair(ci);
1753 * Find a CONF_SECTION, or return the root if name is NULL
1756 CONF_SECTION *cf_section_find(const char *name)
1759 return cf_section_sub_find(mainconfig.config, name);
1761 return mainconfig.config;
1765 * Find a sub-section in a section
1768 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1773 * Do the fast lookup if possible.
1775 if (name && cs->section_tree) {
1780 return rbtree_finddata(cs->section_tree, &mycs);
1783 for (ci = cs->children; ci; ci = ci->next) {
1784 if (ci->type != CONF_ITEM_SECTION)
1786 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1790 return cf_itemtosection(ci);
1796 * Find a CONF_SECTION with both names.
1798 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1799 const char *name1, const char *name2)
1803 if (!cs) cs = mainconfig.config;
1805 if (name1 && (cs->section_tree)) {
1806 CONF_SECTION mycs, *master_cs;
1811 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1813 return rbtree_finddata(master_cs->name2_tree, &mycs);
1818 * Else do it the old-fashioned way.
1820 for (ci = cs->children; ci; ci = ci->next) {
1821 CONF_SECTION *subcs;
1823 if (ci->type != CONF_ITEM_SECTION)
1826 subcs = cf_itemtosection(ci);
1828 if (!subcs->name2) {
1829 if (strcmp(subcs->name1, name2) == 0) break;
1831 if (strcmp(subcs->name2, name2) == 0) break;
1833 continue; /* don't do the string comparisons below */
1836 if ((strcmp(subcs->name1, name1) == 0) &&
1837 (subcs->name2 != NULL) &&
1838 (strcmp(subcs->name2, name2) == 0))
1842 return cf_itemtosection(ci);
1846 * Return the next subsection after a CONF_SECTION
1847 * with a certain name1 (char *name1). If the requested
1848 * name1 is NULL, any name1 matches.
1851 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1852 CONF_SECTION *subsection,
1858 * If subsection is NULL this must be a first time run
1859 * Find the subsection with correct name
1862 if (subsection == NULL){
1863 ci = section->children;
1865 ci = cf_sectiontoitem(subsection)->next;
1868 for (; ci; ci = ci->next) {
1869 if (ci->type != CONF_ITEM_SECTION)
1871 if ((name1 == NULL) ||
1872 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1876 return cf_itemtosection(ci);
1881 * Return the next section after a CONF_SECTION
1882 * with a certain name1 (char *name1). If the requested
1883 * name1 is NULL, any name1 matches.
1886 CONF_SECTION *cf_section_find_next(CONF_SECTION *section,
1887 CONF_SECTION *subsection,
1890 if (!section->item.parent) return NULL;
1892 return cf_subsection_find_next(section->item.parent, subsection, name1);
1896 * Return the next item after a CONF_ITEM.
1899 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1902 * If item is NULL this must be a first time run
1903 * Return the first item
1907 return section->children;
1913 int cf_section_lineno(CONF_SECTION *section)
1915 return cf_sectiontoitem(section)->lineno;
1918 const char *cf_pair_filename(CONF_PAIR *pair)
1920 return cf_pairtoitem(pair)->filename;
1923 const char *cf_section_filename(CONF_SECTION *section)
1925 return cf_sectiontoitem(section)->filename;
1928 int cf_pair_lineno(CONF_PAIR *pair)
1930 return cf_pairtoitem(pair)->lineno;
1933 int cf_item_is_section(CONF_ITEM *item)
1935 return item->type == CONF_ITEM_SECTION;
1937 int cf_item_is_pair(CONF_ITEM *item)
1939 return item->type == CONF_ITEM_PAIR;
1943 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1944 void *data, void (*data_free)(void *))
1948 cd = rad_malloc(sizeof(*cd));
1949 memset(cd, 0, sizeof(*cd));
1951 cd->item.type = CONF_ITEM_DATA;
1952 cd->item.parent = parent;
1953 cd->name = strdup(name);
1955 cd->free = data_free;
1961 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1964 if (!cs || !name) return NULL;
1967 * Find the name in the tree, for speed.
1969 if (cs->data_tree) {
1974 return rbtree_finddata(cs->data_tree, &mycd);
1981 * Find data from a particular section.
1983 void *cf_data_find(CONF_SECTION *cs, const char *name)
1985 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
1987 if (cd) return cd->data;
1993 * Add named data to a configuration section.
1995 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1996 void *data, void (*data_free)(void *),
2001 if (!cs || !name) return -1;
2004 * Already exists. Can't add it.
2006 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
2008 cd = cf_data_alloc(cs, name, data, data_free);
2012 cf_item_add(cs, cf_datatoitem(cd));
2018 * Add named data to a configuration section.
2020 int cf_data_add(CONF_SECTION *cs, const char *name,
2021 void *data, void (*data_free)(void *))
2023 return cf_data_add_internal(cs, name, data, data_free, 0);
2028 * Copy CONF_DATA from src to dst
2030 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
2033 CONF_ITEM *cd, *next, **last;
2036 * Don't check if s->data_tree is NULL. It's child
2037 * sections may have data, even if this section doesn't.
2040 rad_assert(d->data_tree == NULL);
2041 d->data_tree = s->data_tree;
2042 s->data_tree = NULL;
2045 * Walk through src, moving CONF_ITEM_DATA
2048 last = &(s->children);
2049 for (cd = s->children; cd != NULL; cd = next) {
2053 * Recursively copy data from child sections.
2055 if (cd->type == CONF_ITEM_SECTION) {
2056 CONF_SECTION *s1, *d1;
2058 s1 = cf_itemtosection(cd);
2059 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
2061 cf_section_copy_data(s1, d1);
2068 * Not conf data, remember last ptr.
2070 if (cd->type != CONF_ITEM_DATA) {
2076 * Remove it from the src list
2082 * Add it to the dst list
2085 rad_assert(d->tail == NULL);
2088 rad_assert(d->tail != NULL);
2096 * For a CONF_DATA element, stat the filename, if necessary.
2098 static int filename_stat(void *context, void *data)
2101 CONF_DATA *cd = data;
2103 context = context; /* -Wunused */
2105 if (cd->flag != PW_TYPE_FILENAME) return 0;
2107 if (stat(cd->name, &buf) < 0) return -1;
2109 if (buf.st_mtime != *(time_t *) cd->data) return -1;
2116 * Compare two CONF_SECTIONS. The items MUST be in the same
2119 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
2121 CONF_ITEM *ca = a->children;
2122 CONF_ITEM *cb = b->children;
2130 if (!ca && !cb) break;
2135 if (ca && ca->type == CONF_ITEM_DATA) {
2139 if (cb && cb->type == CONF_ITEM_DATA) {
2145 * One is smaller than the other. Exit.
2147 if (!ca || !cb) return 0;
2149 if (ca->type != cb->type) return 0;
2152 * Deal with subsections.
2154 if (ca->type == CONF_ITEM_SECTION) {
2155 CONF_SECTION *sa = cf_itemtosection(ca);
2156 CONF_SECTION *sb = cf_itemtosection(cb);
2158 if (!cf_section_cmp(sa, sb)) return 0;
2162 rad_assert(ca->type == CONF_ITEM_PAIR);
2164 pa = cf_itemtopair(ca);
2165 pb = cf_itemtopair(cb);
2168 * Different attr and/or value, Exit.
2170 if ((strcmp(pa->attr, pb->attr) != 0) ||
2171 (strcmp(pa->value, pb->value) != 0)) return 0;
2175 * And go to the next element.
2183 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
2186 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
2191 * They must be the same, say so.
2198 * Migrate CONF_DATA from one section to another.
2200 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
2203 CONF_SECTION *s, *d;
2205 for (ci = src->children; ci != NULL; ci = ci->next) {
2206 if (ci->type != CONF_ITEM_SECTION)
2209 s = cf_itemtosection(ci);
2210 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
2212 if (!d) continue; /* not in new one, don't migrate it */
2215 * A section of the same name is in BOTH src & dst,
2216 * compare the CONF_PAIR's. If they're all the same,
2217 * then copy the CONF_DATA from one to the other.
2219 if (cf_section_cmp(s, d)) {
2220 cf_section_copy_data(s, d);
2224 return 1; /* rcode means anything? */
2227 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
2229 if (!cs || !template || cs->template || template->template) return -1;
2231 cs->template = template;
2238 * This is here to make the rest of the code easier to read. It
2239 * ties conffile.c to log.c, but it means we don't have to
2242 void cf_log_err(CONF_ITEM *ci, const char *fmt, ...)
2248 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2251 radlog(L_ERR, "%s[%d]: %s", ci->filename, ci->lineno, buffer);
2257 * JMG dump_config tries to dump the config structure in a readable format
2261 static int dump_config_section(CONF_SECTION *cs, int indent)
2267 /* The DEBUG macro doesn't let me
2268 * for(i=0;i<indent;++i) debugputchar('\t');
2269 * so I had to get creative. --Pac. */
2271 for (ci = cs->children; ci; ci = ci->next) {
2273 case CONF_ITEM_PAIR:
2274 cp=cf_itemtopair(ci);
2275 DEBUG("%.*s%s = %s",
2276 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2277 cp->attr, cp->value);
2280 case CONF_ITEM_SECTION:
2281 scs=cf_itemtosection(ci);
2282 DEBUG("%.*s%s %s%s{",
2283 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2285 scs->name2 ? scs->name2 : "",
2286 scs->name2 ? " " : "");
2287 dump_config_section(scs, indent+1);
2289 indent, "\t\t\t\t\t\t\t\t\t\t\t");
2292 default: /* FIXME: Do more! */
2300 int dump_config(CONF_SECTION *cs)
2302 return dump_config_section(cs, 0);