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/autoconf.h>
37 #ifdef HAVE_NETINET_IN_H
38 # include <netinet/in.h>
44 #ifdef HAVE_SYS_STAT_H
51 #include <freeradius-devel/radiusd.h>
52 #include <freeradius-devel/rad_assert.h>
53 #include <freeradius-devel/modules.h>
55 typedef enum conf_type {
56 CONF_ITEM_INVALID = 0,
63 struct conf_item *next;
64 struct conf_part *parent;
78 struct conf_item *children;
79 struct conf_item *tail; /* for speed */
80 rbtree_t *pair_tree; /* and a partridge.. */
81 rbtree_t *section_tree; /* no jokes here */
82 rbtree_t *name2_tree; /* for sections of the same name2 */
85 const CONF_PARSER *variables;
90 * Internal data that is associated with a configuration section,
91 * so that we don't have to track it separately.
97 void *data; /* user data */
98 void (*free)(void *); /* free user data function */
102 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
103 void *data, void (*data_free)(void *),
105 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
109 * Isolate the scary casts in these tiny provably-safe functions
111 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
115 rad_assert(ci->type == CONF_ITEM_PAIR);
116 return (CONF_PAIR *)ci;
118 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
122 rad_assert(ci->type == CONF_ITEM_SECTION);
123 return (CONF_SECTION *)ci;
125 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
129 return (CONF_ITEM *)cp;
131 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
135 return (CONF_ITEM *)cs;
138 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
142 rad_assert(ci->type == CONF_ITEM_DATA);
143 return (CONF_DATA *)ci;
145 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
149 return (CONF_ITEM *)cd;
153 * Create a new CONF_PAIR
155 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
156 LRAD_TOKEN operator, CONF_SECTION *parent)
160 cp = rad_malloc(sizeof(*cp));
161 memset(cp, 0, sizeof(*cp));
162 cp->item.type = CONF_ITEM_PAIR;
163 cp->item.parent = parent;
164 cp->attr = strdup(attr);
165 cp->value = strdup(value);
166 cp->operator = operator;
174 void cf_pair_free(CONF_PAIR **cp)
176 if (!cp || !*cp) return;
184 memset(*cp, 0, sizeof(*cp));
192 static void cf_data_free(CONF_DATA **cd)
194 if (!cd || !*cd) return;
200 ((*cd)->free)((*cd)->data);
203 memset(*cd, 0, sizeof(*cd));
210 * rbtree callback function
212 static int pair_cmp(const void *a, const void *b)
214 const CONF_PAIR *one = a;
215 const CONF_PAIR *two = b;
217 return strcmp(one->attr, two->attr);
222 * rbtree callback function
224 static int section_cmp(const void *a, const void *b)
226 const CONF_SECTION *one = a;
227 const CONF_SECTION *two = b;
229 return strcmp(one->name1, two->name1);
234 * rbtree callback function
236 static int name2_cmp(const void *a, const void *b)
238 const CONF_SECTION *one = a;
239 const CONF_SECTION *two = b;
241 rad_assert(strcmp(one->name1, two->name1) == 0);
243 if (!one->name2 && !two->name2) return 0;
244 if (!one->name2) return -1;
245 if (!two->name2) return +1;
247 return strcmp(one->name2, two->name2);
252 * rbtree callback function
254 static int data_cmp(const void *a, const void *b)
258 const CONF_DATA *one = a;
259 const CONF_DATA *two = b;
261 rcode = one->flag - two->flag;
262 if (rcode != 0) return rcode;
264 return strcmp(one->name, two->name);
269 * Free strings we've parsed into data structures.
271 static void cf_section_parse_free(void *base, const CONF_PARSER *variables)
276 * Don't automatically free the strings if we're being
277 * called from a module. This is also for clients.c,
278 * where client_free() expects to be able to free the
279 * client structure. If we moved everything to key off
280 * of the config files, we might solve some problems...
282 if (!variables) return;
285 * Free up dynamically allocated string pointers.
287 for (i = 0; variables[i].name != NULL; i++) {
290 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
291 (variables[i].type != PW_TYPE_FILENAME)) {
296 * No base struct offset, data must be the pointer.
297 * If data doesn't exist, ignore the entry, there
298 * must be something wrong.
301 if (!variables[i].data) {
305 p = (char **) variables[i].data;;
307 } else if (variables[i].data) {
308 p = (char **) variables[i].data;;
311 p = (char **) (((char *)base) + variables[i].offset);
321 * Free a CONF_SECTION
323 void cf_section_free(CONF_SECTION **cs)
325 CONF_ITEM *ci, *next;
327 if (!cs || !*cs) return;
329 if ((*cs)->variables) {
330 cf_section_parse_free((*cs)->base, (*cs)->variables);
333 for (ci = (*cs)->children; ci; ci = next) {
337 case CONF_ITEM_PAIR: {
338 CONF_PAIR *pair = cf_itemtopair(ci);
343 case CONF_ITEM_SECTION: {
345 CONF_SECTION *section = cf_itemtosection(ci);
346 cf_section_free(§ion);
350 case CONF_ITEM_DATA: {
351 CONF_DATA *data = cf_itemtodata(ci);
356 default: /* should really be an error. */
365 if ((*cs)->pair_tree)
366 rbtree_free((*cs)->pair_tree);
367 if ((*cs)->section_tree)
368 rbtree_free((*cs)->section_tree);
369 if ((*cs)->name2_tree)
370 rbtree_free((*cs)->name2_tree);
371 if ((*cs)->data_tree)
372 rbtree_free((*cs)->data_tree);
375 * And free the section
378 memset(*cs, 0, sizeof(*cs));
387 * Allocate a CONF_SECTION
389 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
390 CONF_SECTION *parent)
394 if (!name1) return NULL;
396 cs = rad_malloc(sizeof(*cs));
397 memset(cs, 0, sizeof(*cs));
398 cs->item.type = CONF_ITEM_SECTION;
399 cs->item.parent = parent;
400 cs->name1 = strdup(name1);
402 cf_section_free(&cs);
406 if (name2 && *name2) {
407 cs->name2 = strdup(name2);
409 cf_section_free(&cs);
413 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
414 if (!cs->pair_tree) {
415 cf_section_free(&cs);
420 * Don't create a data tree, it may not be needed.
424 * Don't create the section tree here, it may not
432 * Add an item to a configuration section.
434 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
437 rad_assert(cs->tail == NULL);
440 rad_assert(cs->tail != NULL);
445 * Update the trees (and tail) for each item added.
447 for (/* nothing */; ci != NULL; ci = ci->next) {
451 * For fast lookups, pair's and sections get
456 rbtree_insert(cs->pair_tree, ci);
459 case CONF_ITEM_SECTION: {
460 const CONF_SECTION *cs_new = cf_itemtosection(ci);
462 if (!cs->section_tree) {
463 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
464 /* ignore any errors */
467 if (cs->section_tree) {
468 rbtree_insert(cs->section_tree, cs_new); }
471 * Two names: find the named instance.
474 CONF_SECTION *old_cs;
478 * CONF_SECTION having
479 * the given name1, and
483 old_cs = rbtree_finddata(cs->section_tree, cs_new);
484 if (!old_cs) return; /* this is a bad error! */
486 if (!old_cs->name2_tree) {
487 old_cs->name2_tree = rbtree_create(name2_cmp,
490 if (old_cs->name2_tree) {
491 rbtree_insert(old_cs->name2_tree, cs_new);
495 } /* was a section */
498 if (!cs->data_tree) {
499 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
502 rbtree_insert(cs->data_tree, ci);
506 default: /* FIXME: assert & error! */
509 } /* switch over conf types */
514 * Expand the variables in an input string.
516 static const char *cf_expand_variables(const char *cf, int *lineno,
517 const CONF_SECTION *outercs,
518 char *output, const char *input)
521 const char *end, *ptr;
523 const CONF_SECTION *parentcs;
526 * Find the master parent conf section.
527 * We can't use mainconfig.config, because we're in the
528 * process of re-building it, and it isn't set up yet...
530 for (parentcs = outercs;
531 parentcs->item.parent != NULL;
532 parentcs = parentcs->item.parent) {
540 * Ignore anything other than "${"
542 if ((*ptr == '$') && (ptr[1] == '{')) {
545 const CONF_SECTION *cs;
548 * FIXME: Add support for ${foo:-bar},
553 * Look for trailing '}', and log a
554 * warning for anything that doesn't match,
555 * and exit with a fatal error.
557 end = strchr(ptr, '}');
560 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
571 * ${.foo} means "foo from the current section"
579 * ${..foo} means "foo from the section
580 * enclosing this section" (etc.)
582 while (*ptr == '.') {
584 cs = cs->item.parent;
591 * ${foo} is local, with
592 * main as lower priority
597 * ${foo.bar.baz} is always rooted
600 for (q = ptr; *q && q != end; q++) {
612 * Find the next section.
615 (*ptr != 0) && (*ptr != '.') &&
623 * The character is a '.', find a
624 * section (as the user has given
625 * us a subsection to find)
630 ptr++; /* skip the period */
633 * Find the sub-section.
635 next = cf_section_sub_find(cs, name);
637 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
642 } else { /* no period, must be a conf-part */
644 * Find in the current referenced
647 cp = cf_pair_find(cs, name);
650 * It it was NOT ${..foo}
652 * top-level config items.
654 if (!up) cp = cf_pair_find(parentcs, name);
657 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
658 cf_section_name1(cs),
663 } /* until cp is non-NULL */
666 * Substitute the value of the variable.
668 strcpy(p, cp->value);
672 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
678 * Look for trailing '}', and log a
679 * warning for anything that doesn't match,
680 * and exit with a fatal error.
682 end = strchr(ptr, '}');
685 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
690 memcpy(name, ptr, end - ptr);
691 name[end - ptr] = '\0';
694 * Get the environment variable.
695 * If none exists, then make it an empty string.
709 * Copy it over verbatim.
713 } /* loop over all of the input string. */
722 * Parses an item (not a CONF_ITEM) into the specified format,
723 * with a default value.
725 * Returns -1 on error, 0 for correctly parsed, and 1 if the
726 * default value was used. Note that the default value will be
727 * used ONLY if the CONF_PAIR is NULL.
729 int cf_item_parse(CONF_SECTION *cs, const char *name,
730 int type, void *data, const char *dflt)
735 lrad_ipaddr_t ipaddr;
739 cp = cf_pair_find(cs, name);
744 return 1; /* nothing to parse, return default value */
752 case PW_TYPE_BOOLEAN:
754 * Allow yes/no and on/off
756 if ((strcasecmp(value, "yes") == 0) ||
757 (strcasecmp(value, "on") == 0)) {
759 } else if ((strcasecmp(value, "no") == 0) ||
760 (strcasecmp(value, "off") == 0)) {
764 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
768 DEBUG2(" %s %s: %s = %s", cs->name1, cs->name2, name, value);
770 DEBUG2(" %s: %s = %s", cs->name1, name, value);
774 case PW_TYPE_INTEGER:
775 *(int *)data = strtol(value, 0, 0);
777 DEBUG2(" %s %s: %s = %d",
778 cs->name1, cs->name2, name,
781 DEBUG2(" %s: %s = %d",
787 case PW_TYPE_STRING_PTR:
794 * Expand variables which haven't already been
795 * expanded automagically when the configuration
801 int lineno = cs->item.lineno;
804 * FIXME: sizeof(buffer)?
806 value = cf_expand_variables("?",
809 if (!value) return -1;
813 DEBUG2(" %s %s: %s = \"%s\"",
814 cs->name1, cs->name2, name,
815 value ? value : "(null)");
817 DEBUG2(" %s: %s = \"%s\"",
819 value ? value : "(null)");
821 *q = value ? strdup(value) : NULL;
825 * This is the same as PW_TYPE_STRING_PTR,
826 * except that we also "stat" the file, and
829 case PW_TYPE_FILENAME:
836 * Expand variables which haven't already been
837 * expanded automagically when the configuration
843 int lineno = cs->item.lineno;
846 * FIXME: sizeof(buffer)?
848 value = cf_expand_variables("?",
851 if (!value) return -1;
855 DEBUG2(" %s %s: %s = \"%s\"",
856 cs->name1, cs->name2, name,
857 value ? value : "(null)");
859 DEBUG2(" %s: %s = \"%s\"",
861 value ? value : "(null)");
863 *q = value ? strdup(value) : NULL;
866 * And now we "stat" the file.
871 if (stat(*q, &buf) == 0) {
874 mtime = rad_malloc(sizeof(*mtime));
875 *mtime = buf.st_mtime;
877 cf_data_add_internal(cs, *q, mtime, free,
885 * Allow '*' as any address
887 if (strcmp(value, "*") == 0) {
888 *(uint32_t *) data = htonl(INADDR_ANY);
889 DEBUG2(" %s: %s = *", cs->name1, name);
892 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
893 radlog(L_ERR, "Can't find IP address for host %s", value);
897 DEBUG2(" %s %s: %s = %s IP address [%s]",
898 cs->name1, cs->name2, name, value,
899 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
901 DEBUG2(" %s: %s = %s IP address [%s]",
902 cs->name1, name, value,
903 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
905 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
908 case PW_TYPE_IPV6ADDR:
909 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
910 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
914 DEBUG2(" %s %s: %s = %s IPv6 address [%s]",
915 cs->name1, cs->name2, name, value,
916 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
918 DEBUG2(" %s: %s = %s IPv6 address [%s]",
919 cs->name1, name, value,
920 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
922 memcpy(data, &ipaddr.ipaddr.ip6addr,
923 sizeof(ipaddr.ipaddr.ip6addr));
927 radlog(L_ERR, "type %d not supported yet", type);
930 } /* switch over variable type */
936 * Parse a configuration section into user-supplied variables.
938 int cf_section_parse(CONF_SECTION *cs, void *base,
939 const CONF_PARSER *variables)
945 * Handle the known configuration parameters.
947 for (i = 0; variables[i].name != NULL; i++) {
949 * Handle subsections specially
951 if (variables[i].type == PW_TYPE_SUBSECTION) {
952 const CONF_SECTION *subcs;
953 subcs = cf_section_sub_find(cs, variables[i].name);
956 * If the configuration section is NOT there,
959 * FIXME! This is probably wrong... we should
960 * probably set the items to their default values.
962 if (!subcs) continue;
964 if (!variables[i].dflt) {
965 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
966 cf_section_parse_free(base, variables);
970 if (cf_section_parse(subcs, base,
971 (const CONF_PARSER *) variables[i].dflt) < 0) {
972 cf_section_parse_free(base, variables);
976 } /* else it's a CONF_PAIR */
978 if (variables[i].data) {
979 data = variables[i].data; /* prefer this. */
981 data = ((char *)base) + variables[i].offset;
983 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
984 cf_section_parse_free(base, variables);
989 * Parse the pair we found, or a default value.
991 if (cf_item_parse(cs, variables[i].name, variables[i].type,
992 data, variables[i].dflt) < 0) {
993 cf_section_parse_free(base, variables);
996 } /* for all variables in the configuration section */
999 cs->variables = variables;
1006 * Read a part of the config file.
1008 static int cf_section_read(const char *file, int *lineno, FILE *fp,
1009 CONF_SECTION *current)
1012 CONF_SECTION *this, *css;
1024 this = current; /* add items here */
1027 * Read, checking for line continuations ('\\' at EOL)
1033 * Get data, and remember if we are at EOF.
1035 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1041 * We've filled the buffer, and there isn't
1044 if ((len == (sizeof(buf) - 1)) &&
1045 (cbuf[len - 1] != '\n')) {
1046 radlog(L_ERR, "%s[%d]: Line too long",
1052 * Check for continuations.
1054 if (cbuf[len - 1] == '\n') len--;
1057 * Last character is '\\'. Over-write it,
1058 * and read another line.
1060 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1061 cbuf[len - 1] = '\0';
1067 * We're at EOF, and haven't read anything. Stop.
1069 if (eof && (cbuf == buf)) {
1074 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1076 if ((*buf1 == '#') || (*buf1 == '\0')) {
1081 * The caller eats "name1 name2 {", and calls us
1082 * for the data inside of the section. So if we
1083 * receive a closing brace, then it must mean the
1084 * end of the section.
1086 if (t1 == T_RCBRACE) {
1087 if (this == current) {
1088 radlog(L_ERR, "%s[%d]: Too many closing braces",
1093 this = this->item.parent;
1098 * Allow for $INCLUDE files
1100 * This *SHOULD* work for any level include.
1101 * I really really really hate this file. -cparker
1103 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1104 t2 = getword(&ptr, buf2, sizeof(buf2));
1106 value = cf_expand_variables(file, lineno, this, buf, buf2);
1107 if (!value) return -1;
1109 #ifdef HAVE_DIRENT_H
1113 * Include ALL non-"dot" files in the directory.
1116 if (value[strlen(value) - 1] == '/') {
1119 struct stat stat_buf;
1121 DEBUG2( "Config: including files in directory: %s", value );
1122 dir = opendir(value);
1124 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1125 file, *lineno, value,
1131 * Read the directory, ignoring "." files.
1133 while ((dp = readdir(dir)) != NULL) {
1136 if (dp->d_name[0] == '.') continue;
1139 * Check for valid characters
1141 for (p = dp->d_name; *p != '\0'; p++) {
1142 if (isalpha((int)*p) ||
1145 (*p == '.')) continue;
1148 if (*p != '\0') continue;
1150 snprintf(buf2, sizeof(buf2), "%s%s",
1152 if ((stat(buf2, &stat_buf) != 0) ||
1153 S_ISDIR(stat_buf.st_mode)) continue;
1155 * Read the file into the current
1156 * configuration sectoin.
1158 if (cf_file_include(buf2, this) < 0) {
1166 { /* it was a normal file */
1167 if (cf_file_include(value, this) < 0) {
1172 } /* we were in an include */
1175 * No '=': must be a section or sub-section.
1177 if (strchr(ptr, '=') == NULL) {
1178 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1179 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1181 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1182 t3 = getword(&ptr, buf3, sizeof(buf3));
1186 * Perhaps a subsection.
1188 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1189 css = cf_section_alloc(buf1,
1190 t2 == T_LCBRACE ? NULL : buf2,
1193 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1196 cf_item_add(this, cf_sectiontoitem(css));
1197 css->item.lineno = *lineno;
1200 * The current section is now the child section.
1207 * Ignore semi-colons.
1213 * Must be a normal attr = value line.
1215 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1217 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1218 (t2 < T_EQSTART || t2 > T_EQEND)) {
1219 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1225 * Ensure that the user can't add CONF_PAIRs
1226 * with 'internal' names;
1228 if (buf1[0] == '_') {
1229 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1230 file, *lineno, buf1);
1235 * Handle variable substitution via ${foo}
1237 value = cf_expand_variables(file, lineno, this, buf, buf3);
1238 if (!value) return -1;
1242 * Add this CONF_PAIR to our CONF_SECTION
1244 cpn = cf_pair_alloc(buf1, value, t2, this);
1245 cpn->item.lineno = *lineno;
1246 cf_item_add(this, cf_pairtoitem(cpn));
1250 * See if EOF was unexpected ..
1252 if (feof(fp) && (this != current)) {
1253 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1255 cf_section_name1(this), cf_section_lineno(this));
1263 * Include one config file in another.
1265 int cf_file_include(const char *file, CONF_SECTION *cs)
1269 struct stat statbuf;
1272 DEBUG2( "Config: including file: %s", file);
1274 if (stat(file, &statbuf) == 0) {
1275 if ((statbuf.st_mode & S_IWOTH) != 0) {
1276 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1281 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1282 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1288 fp = fopen(file, "r");
1290 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1291 file, strerror(errno));
1296 * Read the section. It's OK to have EOF without a
1297 * matching close brace.
1299 if (cf_section_read(file, &lineno, fp, cs) < 0) {
1305 * Add the filename to the section
1307 mtime = rad_malloc(sizeof(*mtime));
1308 *mtime = statbuf.st_mtime;
1310 cf_data_add_internal(cs, file, mtime, free,
1318 * Bootstrap a config file.
1320 CONF_SECTION *cf_file_read(const char *file)
1324 cs = cf_section_alloc("main", NULL, NULL);
1325 if (!cs) return NULL;
1327 if (cf_file_include(file, cs) < 0) {
1328 cf_section_free(&cs);
1336 * Return a CONF_PAIR within a CONF_SECTION.
1338 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1342 if (!cs) cs = mainconfig.config;
1345 * Find the name in the tree, for speed.
1351 return rbtree_finddata(cs->pair_tree, &mycp);
1355 * Else find the first one
1357 for (ci = cs->children; ci; ci = ci->next) {
1358 if (ci->type == CONF_ITEM_PAIR)
1359 return cf_itemtopair(ci);
1366 * Return the attr of a CONF_PAIR
1369 char *cf_pair_attr(CONF_PAIR *pair)
1371 return (pair ? pair->attr : NULL);
1375 * Return the value of a CONF_PAIR
1378 char *cf_pair_value(CONF_PAIR *pair)
1380 return (pair ? pair->value : NULL);
1384 * Return the first label of a CONF_SECTION
1387 const char *cf_section_name1(const CONF_SECTION *cs)
1389 return (cs ? cs->name1 : NULL);
1393 * Return the second label of a CONF_SECTION
1396 const char *cf_section_name2(const CONF_SECTION *cs)
1398 return (cs ? cs->name2 : NULL);
1402 * Find a value in a CONF_SECTION
1404 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1408 cp = cf_pair_find(cs, attr);
1410 return (cp ? cp->value : NULL);
1414 * Return the next pair after a CONF_PAIR
1415 * with a certain name (char *attr) If the requested
1416 * attr is NULL, any attr matches.
1419 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1420 const CONF_PAIR *pair, const char *attr)
1425 * If pair is NULL this must be a first time run
1426 * Find the pair with correct name
1430 return cf_pair_find(cs, attr);
1433 ci = cf_pairtoitem(pair)->next;
1435 for (; ci; ci = ci->next) {
1436 if (ci->type != CONF_ITEM_PAIR)
1438 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1442 return cf_itemtopair(ci);
1446 * Find a CONF_SECTION, or return the root if name is NULL
1449 CONF_SECTION *cf_section_find(const char *name)
1452 return cf_section_sub_find(mainconfig.config, name);
1454 return mainconfig.config;
1458 * Find a sub-section in a section
1461 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1466 * Do the fast lookup if possible.
1468 if (name && cs->section_tree) {
1473 return rbtree_finddata(cs->section_tree, &mycs);
1476 for (ci = cs->children; ci; ci = ci->next) {
1477 if (ci->type != CONF_ITEM_SECTION)
1479 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1483 return cf_itemtosection(ci);
1489 * Find a CONF_SECTION with both names.
1491 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1492 const char *name1, const char *name2)
1496 if (!name2) return cf_section_sub_find(cs, name1);
1498 if (!cs) cs = mainconfig.config;
1500 if (name1 && (cs->section_tree)) {
1501 CONF_SECTION mycs, *master_cs;
1506 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1508 return rbtree_finddata(master_cs->name2_tree, &mycs);
1513 * Else do it the old-fashioned way.
1515 for (ci = cs->children; ci; ci = ci->next) {
1516 CONF_SECTION *subcs;
1518 if (ci->type != CONF_ITEM_SECTION)
1521 subcs = cf_itemtosection(ci);
1523 if (!subcs->name2) {
1524 if (strcmp(subcs->name1, name2) == 0) break;
1526 if (strcmp(subcs->name2, name2) == 0) break;
1528 continue; /* don't do the string comparisons below */
1531 if ((strcmp(subcs->name1, name1) == 0) &&
1532 (subcs->name2 != NULL) &&
1533 (strcmp(subcs->name2, name2) == 0))
1537 return cf_itemtosection(ci);
1541 * Return the next subsection after a CONF_SECTION
1542 * with a certain name1 (char *name1). If the requested
1543 * name1 is NULL, any name1 matches.
1546 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1547 CONF_SECTION *subsection,
1553 * If subsection is NULL this must be a first time run
1554 * Find the subsection with correct name
1557 if (subsection == NULL){
1558 ci = section->children;
1560 ci = cf_sectiontoitem(subsection)->next;
1563 for (; ci; ci = ci->next) {
1564 if (ci->type != CONF_ITEM_SECTION)
1566 if ((name1 == NULL) ||
1567 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1571 return cf_itemtosection(ci);
1575 * Return the next item after a CONF_ITEM.
1578 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1581 * If item is NULL this must be a first time run
1582 * Return the first item
1586 return section->children;
1592 int cf_section_lineno(CONF_SECTION *section)
1594 return cf_sectiontoitem(section)->lineno;
1597 int cf_pair_lineno(CONF_PAIR *pair)
1599 return cf_pairtoitem(pair)->lineno;
1602 int cf_item_is_section(CONF_ITEM *item)
1604 return item->type == CONF_ITEM_SECTION;
1606 int cf_item_is_pair(CONF_ITEM *item)
1608 return item->type == CONF_ITEM_PAIR;
1612 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1613 void *data, void (*data_free)(void *))
1617 cd = rad_malloc(sizeof(*cd));
1618 memset(cd, 0, sizeof(*cd));
1620 cd->item.type = CONF_ITEM_DATA;
1621 cd->item.parent = parent;
1622 cd->name = strdup(name);
1624 cd->free = data_free;
1630 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1633 if (!cs || !name) return NULL;
1636 * Find the name in the tree, for speed.
1638 if (cs->data_tree) {
1639 CONF_DATA mycd, *cd;
1643 cd = rbtree_finddata(cs->data_tree, &mycd);
1644 if (cd) return cd->data;
1651 * Find data from a particular section.
1653 void *cf_data_find(CONF_SECTION *cs, const char *name)
1655 return cf_data_find_internal(cs, name, 0);
1660 * Add named data to a configuration section.
1662 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1663 void *data, void (*data_free)(void *),
1668 if (!cs || !name) return -1;
1671 * Already exists. Can't add it.
1673 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1675 cd = cf_data_alloc(cs, name, data, data_free);
1679 cf_item_add(cs, cf_datatoitem(cd));
1685 * Add named data to a configuration section.
1687 int cf_data_add(CONF_SECTION *cs, const char *name,
1688 void *data, void (*data_free)(void *))
1690 return cf_data_add_internal(cs, name, data, data_free, 0);
1695 * Copy CONF_DATA from src to dst
1697 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1700 CONF_ITEM *cd, *next, **last;
1703 * Don't check if s->data_tree is NULL. It's child
1704 * sections may have data, even if this section doesn't.
1707 rad_assert(d->data_tree == NULL);
1708 d->data_tree = s->data_tree;
1709 s->data_tree = NULL;
1712 * Walk through src, moving CONF_ITEM_DATA
1715 last = &(s->children);
1716 for (cd = s->children; cd != NULL; cd = next) {
1720 * Recursively copy data from child sections.
1722 if (cd->type == CONF_ITEM_SECTION) {
1723 CONF_SECTION *s1, *d1;
1725 s1 = cf_itemtosection(cd);
1726 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1728 cf_section_copy_data(s1, d1);
1735 * Not conf data, remember last ptr.
1737 if (cd->type != CONF_ITEM_DATA) {
1743 * Remove it from the src list
1749 * Add it to the dst list
1752 rad_assert(d->tail == NULL);
1755 rad_assert(d->tail != NULL);
1763 * For a CONF_DATA element, stat the filename, if necessary.
1765 static int filename_stat(void *context, void *data)
1768 CONF_DATA *cd = data;
1770 context = context; /* -Wunused */
1772 if (cd->flag != PW_TYPE_FILENAME) return 0;
1774 if (stat(cd->name, &buf) < 0) return -1;
1776 if (buf.st_mtime != *(time_t *) cd->data) return -1;
1783 * Compare two CONF_SECTIONS. The items MUST be in the same
1786 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1788 CONF_ITEM *ca = a->children;
1789 CONF_ITEM *cb = b->children;
1797 if (!ca && !cb) break;
1802 if (ca && ca->type == CONF_ITEM_DATA) {
1806 if (cb && cb->type == CONF_ITEM_DATA) {
1812 * One is smaller than the other. Exit.
1814 if (!ca || !cb) return 0;
1816 if (ca->type != cb->type) return 0;
1819 * Deal with subsections.
1821 if (ca->type == CONF_ITEM_SECTION) {
1822 CONF_SECTION *sa = cf_itemtosection(ca);
1823 CONF_SECTION *sb = cf_itemtosection(cb);
1825 if (!cf_section_cmp(sa, sb)) return 0;
1829 rad_assert(ca->type == CONF_ITEM_PAIR);
1831 pa = cf_itemtopair(ca);
1832 pb = cf_itemtopair(cb);
1835 * Different attr and/or value, Exit.
1837 if ((strcmp(pa->attr, pb->attr) != 0) ||
1838 (strcmp(pa->value, pb->value) != 0)) return 0;
1842 * And go to the next element.
1850 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
1853 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
1858 * They must be the same, say so.
1867 * Migrate CONF_DATA from one section to another.
1869 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1872 CONF_SECTION *s, *d;
1874 for (ci = src->children; ci != NULL; ci = ci->next) {
1875 if (ci->type != CONF_ITEM_SECTION)
1878 s = cf_itemtosection(ci);
1879 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1881 if (!d) continue; /* not in new one, don't migrate it */
1884 * A section of the same name is in BOTH src & dst,
1885 * compare the CONF_PAIR's. If they're all the same,
1886 * then copy the CONF_DATA from one to the other.
1888 if (cf_section_cmp(s, d)) {
1889 cf_section_copy_data(s, d);
1893 return 1; /* rcode means anything? */
1899 * JMG dump_config tries to dump the config structure in a readable format
1903 static int dump_config_section(CONF_SECTION *cs, int indent)
1909 /* The DEBUG macro doesn't let me
1910 * for(i=0;i<indent;++i) debugputchar('\t');
1911 * so I had to get creative. --Pac. */
1913 for (ci = cs->children; ci; ci = ci->next) {
1915 case CONF_ITEM_PAIR:
1916 cp=cf_itemtopair(ci);
1917 DEBUG("%.*s%s = %s",
1918 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1919 cp->attr, cp->value);
1922 case CONF_ITEM_SECTION:
1923 scs=cf_itemtosection(ci);
1924 DEBUG("%.*s%s %s%s{",
1925 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1927 scs->name2 ? scs->name2 : "",
1928 scs->name2 ? " " : "");
1929 dump_config_section(scs, indent+1);
1931 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1934 default: /* FIXME: Do more! */
1942 int dump_config(void)
1944 return dump_config_section(mainconfig.config, 0);