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 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/autoconf.h>
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
41 #ifdef HAVE_SYS_STAT_H
48 #include <freeradius-devel/radiusd.h>
49 #include <freeradius-devel/rad_assert.h>
50 #include <freeradius-devel/modules.h>
52 static const char rcsid[] =
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 */
88 * Internal data that is associated with a configuration section,
89 * so that we don't have to track it separately.
95 void *data; /* user data */
96 void (*free)(void *); /* free user data function */
100 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
101 void *data, void (*data_free)(void *),
103 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
107 * Isolate the scary casts in these tiny provably-safe functions
109 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
113 rad_assert(ci->type == CONF_ITEM_PAIR);
114 return (CONF_PAIR *)ci;
116 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
120 rad_assert(ci->type == CONF_ITEM_SECTION);
121 return (CONF_SECTION *)ci;
123 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
127 return (CONF_ITEM *)cp;
129 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
133 return (CONF_ITEM *)cs;
136 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
140 rad_assert(ci->type == CONF_ITEM_DATA);
141 return (CONF_DATA *)ci;
143 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
147 return (CONF_ITEM *)cd;
151 * Create a new CONF_PAIR
153 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
154 LRAD_TOKEN operator, CONF_SECTION *parent)
158 cp = rad_malloc(sizeof(*cp));
159 memset(cp, 0, sizeof(*cp));
160 cp->item.type = CONF_ITEM_PAIR;
161 cp->item.parent = parent;
162 cp->attr = strdup(attr);
163 cp->value = strdup(value);
164 cp->operator = operator;
172 void cf_pair_free(CONF_PAIR **cp)
174 if (!cp || !*cp) return;
182 memset(*cp, 0, sizeof(*cp));
190 static void cf_data_free(CONF_DATA **cd)
192 if (!cd || !*cd) return;
198 ((*cd)->free)((*cd)->data);
201 memset(*cd, 0, sizeof(*cd));
208 * rbtree callback function
210 static int pair_cmp(const void *a, const void *b)
212 const CONF_PAIR *one = a;
213 const CONF_PAIR *two = b;
215 return strcmp(one->attr, two->attr);
220 * rbtree callback function
222 static int section_cmp(const void *a, const void *b)
224 const CONF_SECTION *one = a;
225 const CONF_SECTION *two = b;
227 return strcmp(one->name1, two->name1);
232 * rbtree callback function
234 static int name2_cmp(const void *a, const void *b)
236 const CONF_SECTION *one = a;
237 const CONF_SECTION *two = b;
239 rad_assert(strcmp(one->name1, two->name1) == 0);
241 if (!one->name2 && !two->name2) return 0;
242 if (!one->name2) return -1;
243 if (!two->name2) return +1;
245 return strcmp(one->name2, two->name2);
250 * rbtree callback function
252 static int data_cmp(const void *a, const void *b)
256 const CONF_DATA *one = a;
257 const CONF_DATA *two = b;
259 rcode = one->flag - two->flag;
260 if (rcode != 0) return rcode;
262 return strcmp(one->name, two->name);
266 * Free a CONF_SECTION
268 void cf_section_free(CONF_SECTION **cs)
270 CONF_ITEM *ci, *next;
272 if (!cs || !*cs) return;
274 for (ci = (*cs)->children; ci; ci = next) {
278 case CONF_ITEM_PAIR: {
279 CONF_PAIR *pair = cf_itemtopair(ci);
284 case CONF_ITEM_SECTION: {
286 CONF_SECTION *section = cf_itemtosection(ci);
287 cf_section_free(§ion);
291 case CONF_ITEM_DATA: {
292 CONF_DATA *data = cf_itemtodata(ci);
297 default: /* should really be an error. */
306 if ((*cs)->pair_tree)
307 rbtree_free((*cs)->pair_tree);
308 if ((*cs)->section_tree)
309 rbtree_free((*cs)->section_tree);
310 if ((*cs)->name2_tree)
311 rbtree_free((*cs)->name2_tree);
312 if ((*cs)->data_tree)
313 rbtree_free((*cs)->data_tree);
316 * And free the section
319 memset(*cs, 0, sizeof(*cs));
328 * Allocate a CONF_SECTION
330 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
331 CONF_SECTION *parent)
335 if (!name1) return NULL;
337 cs = rad_malloc(sizeof(*cs));
338 memset(cs, 0, sizeof(*cs));
339 cs->item.type = CONF_ITEM_SECTION;
340 cs->item.parent = parent;
341 cs->name1 = strdup(name1);
343 cf_section_free(&cs);
347 if (name2 && *name2) {
348 cs->name2 = strdup(name2);
350 cf_section_free(&cs);
354 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
355 if (!cs->pair_tree) {
356 cf_section_free(&cs);
361 * Don't create a data tree, it may not be needed.
365 * Don't create the section tree here, it may not
373 * Add an item to a configuration section.
375 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
378 rad_assert(cs->tail == NULL);
381 rad_assert(cs->tail != NULL);
386 * Update the trees (and tail) for each item added.
388 for (/* nothing */; ci != NULL; ci = ci->next) {
392 * For fast lookups, pair's and sections get
397 rbtree_insert(cs->pair_tree, ci);
400 case CONF_ITEM_SECTION: {
401 const CONF_SECTION *cs_new = cf_itemtosection(ci);
403 if (!cs->section_tree) {
404 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
405 /* ignore any errors */
408 if (cs->section_tree) {
409 rbtree_insert(cs->section_tree, cs_new); }
412 * Two names: find the named instance.
415 CONF_SECTION *old_cs;
419 * CONF_SECTION having
420 * the given name1, and
424 old_cs = rbtree_finddata(cs->section_tree, cs_new);
425 if (!old_cs) return; /* this is a bad error! */
427 if (!old_cs->name2_tree) {
428 old_cs->name2_tree = rbtree_create(name2_cmp,
431 if (old_cs->name2_tree) {
432 rbtree_insert(old_cs->name2_tree, cs_new);
436 } /* was a section */
439 if (!cs->data_tree) {
440 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
443 rbtree_insert(cs->data_tree, ci);
447 default: /* FIXME: assert & error! */
450 } /* switch over conf types */
455 * Expand the variables in an input string.
457 static const char *cf_expand_variables(const char *cf, int *lineno,
458 const CONF_SECTION *outercs,
459 char *output, const char *input)
462 const char *end, *ptr;
464 const CONF_SECTION *parentcs;
467 * Find the master parent conf section.
468 * We can't use mainconfig.config, because we're in the
469 * process of re-building it, and it isn't set up yet...
471 for (parentcs = outercs;
472 parentcs->item.parent != NULL;
473 parentcs = parentcs->item.parent) {
481 * Ignore anything other than "${"
483 if ((*ptr == '$') && (ptr[1] == '{')) {
486 const CONF_SECTION *cs;
489 * FIXME: Add support for ${foo:-bar},
494 * Look for trailing '}', and log a
495 * warning for anything that doesn't match,
496 * and exit with a fatal error.
498 end = strchr(ptr, '}');
501 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
512 * ${.foo} means "foo from the current section"
520 * ${..foo} means "foo from the section
521 * enclosing this section" (etc.)
523 while (*ptr == '.') {
525 cs = cs->item.parent;
532 * ${foo} is local, with
533 * main as lower priority
538 * ${foo.bar.baz} is always rooted
541 for (q = ptr; *q && q != end; q++) {
553 * Find the next section.
556 (*ptr != 0) && (*ptr != '.') &&
564 * The character is a '.', find a
565 * section (as the user has given
566 * us a subsection to find)
571 ptr++; /* skip the period */
574 * Find the sub-section.
576 next = cf_section_sub_find(cs, name);
578 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
583 } else { /* no period, must be a conf-part */
585 * Find in the current referenced
588 cp = cf_pair_find(cs, name);
591 * It it was NOT ${..foo}
593 * top-level config items.
595 if (!up) cp = cf_pair_find(parentcs, name);
598 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
599 cf_section_name1(cs),
604 } /* until cp is non-NULL */
607 * Substitute the value of the variable.
609 strcpy(p, cp->value);
613 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
619 * Look for trailing '}', and log a
620 * warning for anything that doesn't match,
621 * and exit with a fatal error.
623 end = strchr(ptr, '}');
626 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
631 memcpy(name, ptr, end - ptr);
632 name[end - ptr] = '\0';
635 * Get the environment variable.
636 * If none exists, then make it an empty string.
650 * Copy it over verbatim.
654 } /* loop over all of the input string. */
663 * Parses an item (not a CONF_ITEM) into the specified format,
664 * with a default value.
666 * Returns -1 on error, 0 for correctly parsed, and 1 if the
667 * default value was used. Note that the default value will be
668 * used ONLY if the CONF_PAIR is NULL.
670 int cf_item_parse(CONF_SECTION *cs, const char *name,
671 int type, void *data, const char *dflt)
676 lrad_ipaddr_t ipaddr;
680 cp = cf_pair_find(cs, name);
685 return 1; /* nothing to parse, return default value */
693 case PW_TYPE_BOOLEAN:
695 * Allow yes/no and on/off
697 if ((strcasecmp(value, "yes") == 0) ||
698 (strcasecmp(value, "on") == 0)) {
700 } else if ((strcasecmp(value, "no") == 0) ||
701 (strcasecmp(value, "off") == 0)) {
705 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
708 DEBUG2(" %s: %s = %s", cs->name1, name, value);
711 case PW_TYPE_INTEGER:
712 *(int *)data = strtol(value, 0, 0);
713 DEBUG2(" %s: %s = %d",
718 case PW_TYPE_STRING_PTR:
725 * Expand variables which haven't already been
726 * expanded automagically when the configuration
732 int lineno = cs->item.lineno;
735 * FIXME: sizeof(buffer)?
737 value = cf_expand_variables("?",
740 if (!value) return -1;
743 DEBUG2(" %s: %s = \"%s\"",
745 value ? value : "(null)");
746 *q = value ? strdup(value) : NULL;
750 * This is the same as PW_TYPE_STRING_PTR,
751 * except that we also "stat" the file, and
754 case PW_TYPE_FILENAME:
761 * Expand variables which haven't already been
762 * expanded automagically when the configuration
768 int lineno = cs->item.lineno;
771 * FIXME: sizeof(buffer)?
773 value = cf_expand_variables("?",
776 if (!value) return -1;
779 DEBUG2(" %s: %s = \"%s\"",
781 value ? value : "(null)");
782 *q = value ? strdup(value) : NULL;
785 * And now we "stat" the file.
790 if (stat(*q, &buf) == 0) {
793 mtime = rad_malloc(sizeof(*mtime));
794 *mtime = buf.st_mtime;
796 cf_data_add_internal(cs, *q, mtime, free,
804 * Allow '*' as any address
806 if (strcmp(value, "*") == 0) {
807 *(uint32_t *) data = htonl(INADDR_ANY);
808 DEBUG2(" %s: %s = *", cs->name1, name);
811 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
812 radlog(L_ERR, "Can't find IP address for host %s", value);
815 DEBUG2(" %s: %s = %s IP address [%s]",
816 cs->name1, name, value,
817 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
818 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
821 case PW_TYPE_IPV6ADDR:
822 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
823 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
826 DEBUG2(" %s: %s = %s IPv6 address [%s]",
827 cs->name1, name, value,
828 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
829 memcpy(data, &ipaddr.ipaddr.ip6addr,
830 sizeof(ipaddr.ipaddr.ip6addr));
834 radlog(L_ERR, "type %d not supported yet", type);
837 } /* switch over variable type */
843 * Parse a configuration section into user-supplied variables.
845 int cf_section_parse(const CONF_SECTION *cs, void *base,
846 const CONF_PARSER *variables)
852 * Handle the known configuration parameters.
854 for (i = 0; variables[i].name != NULL; i++) {
856 * Handle subsections specially
858 if (variables[i].type == PW_TYPE_SUBSECTION) {
859 const CONF_SECTION *subcs;
860 subcs = cf_section_sub_find(cs, variables[i].name);
863 * If the configuration section is NOT there,
866 * FIXME! This is probably wrong... we should
867 * probably set the items to their default values.
869 if (!subcs) continue;
871 if (!variables[i].dflt) {
872 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
876 if (cf_section_parse(subcs, base,
877 (const CONF_PARSER *) variables[i].dflt) < 0) {
881 } /* else it's a CONF_PAIR */
883 if (variables[i].data) {
884 data = variables[i].data; /* prefer this. */
886 data = ((char *)base) + variables[i].offset;
888 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
893 * Parse the pair we found, or a default value.
895 if (cf_item_parse(cs, variables[i].name, variables[i].type,
896 data, variables[i].dflt) < 0) {
899 } /* for all variables in the configuration section */
906 * Free strings we've parsed into data structures.
908 void cf_section_parse_free_strings(void *base, const CONF_PARSER *variables)
912 if (!variables) return;
915 * Free up dynamically allocated string pointers.
917 for (i = 0; variables[i].name != NULL; i++) {
920 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
921 (variables[i].type != PW_TYPE_FILENAME)) {
926 * Prefer the data, if it's there.
927 * Else use the base + offset.
929 if (variables[i].data) {
930 p = (char **) &(variables[i].data);
932 p = (char **) (((char *)base) + variables[i].offset);
941 * Read a part of the config file.
943 static int cf_section_read(const char *file, int *lineno, FILE *fp,
944 CONF_SECTION *current)
947 CONF_SECTION *this, *css;
959 this = current; /* add items here */
962 * Read, checking for line continuations ('\\' at EOL)
968 * Get data, and remember if we are at EOF.
970 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
976 * We've filled the buffer, and there isn't
979 if ((len == (sizeof(buf) - 1)) &&
980 (cbuf[len - 1] != '\n')) {
981 radlog(L_ERR, "%s[%d]: Line too long",
987 * Check for continuations.
989 if (cbuf[len - 1] == '\n') len--;
992 * Last character is '\\'. Over-write it,
993 * and read another line.
995 if ((len > 0) && (cbuf[len - 1] == '\\')) {
996 cbuf[len - 1] = '\0';
1002 * We're at EOF, and haven't read anything. Stop.
1004 if (eof && (cbuf == buf)) {
1009 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1011 if ((*buf1 == '#') || (*buf1 == '\0')) {
1016 * The caller eats "name1 name2 {", and calls us
1017 * for the data inside of the section. So if we
1018 * receive a closing brace, then it must mean the
1019 * end of the section.
1021 if (t1 == T_RCBRACE) {
1022 if (this == current) {
1023 radlog(L_ERR, "%s[%d]: Too many closing braces",
1028 this = this->item.parent;
1033 * Allow for $INCLUDE files
1035 * This *SHOULD* work for any level include.
1036 * I really really really hate this file. -cparker
1038 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1039 t2 = getword(&ptr, buf2, sizeof(buf2));
1041 value = cf_expand_variables(file, lineno, this, buf, buf2);
1042 if (!value) return -1;
1044 #ifdef HAVE_DIRENT_H
1048 * Include ALL non-"dot" files in the directory.
1051 if (value[strlen(value) - 1] == '/') {
1054 struct stat stat_buf;
1056 DEBUG2( "Config: including files in directory: %s", value );
1057 dir = opendir(value);
1059 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1060 file, *lineno, value,
1066 * Read the directory, ignoring "." files.
1068 while ((dp = readdir(dir)) != NULL) {
1071 if (dp->d_name[0] == '.') continue;
1074 * Check for valid characters
1076 for (p = dp->d_name; *p != '\0'; p++) {
1077 if (isalpha((int)*p) ||
1080 (*p == '.')) continue;
1083 if (*p != '\0') continue;
1085 snprintf(buf2, sizeof(buf2), "%s%s",
1087 if ((stat(buf2, &stat_buf) != 0) ||
1088 S_ISDIR(stat_buf.st_mode)) continue;
1090 * Read the file into the current
1091 * configuration sectoin.
1093 if (cf_file_include(buf2, this) < 0) {
1101 { /* it was a normal file */
1102 if (cf_file_include(value, this) < 0) {
1107 } /* we were in an include */
1110 * No '=': must be a section or sub-section.
1112 if (strchr(ptr, '=') == NULL) {
1113 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1114 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1116 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1117 t3 = getword(&ptr, buf3, sizeof(buf3));
1121 * Perhaps a subsection.
1123 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1124 css = cf_section_alloc(buf1,
1125 t2 == T_LCBRACE ? NULL : buf2,
1128 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1131 cf_item_add(this, cf_sectiontoitem(css));
1132 css->item.lineno = *lineno;
1135 * The current section is now the child section.
1142 * Ignore semi-colons.
1148 * Must be a normal attr = value line.
1150 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1152 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1153 (t2 < T_EQSTART || t2 > T_EQEND)) {
1154 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1160 * Ensure that the user can't add CONF_PAIRs
1161 * with 'internal' names;
1163 if (buf1[0] == '_') {
1164 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1165 file, *lineno, buf1);
1170 * Handle variable substitution via ${foo}
1172 value = cf_expand_variables(file, lineno, this, buf, buf3);
1173 if (!value) return -1;
1177 * Add this CONF_PAIR to our CONF_SECTION
1179 cpn = cf_pair_alloc(buf1, value, t2, this);
1180 cpn->item.lineno = *lineno;
1181 cf_item_add(this, cf_pairtoitem(cpn));
1185 * See if EOF was unexpected ..
1187 if (feof(fp) && (this != current)) {
1188 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1190 cf_section_name1(this), cf_section_lineno(this));
1198 * Include one config file in another.
1200 int cf_file_include(const char *file, CONF_SECTION *cs)
1206 DEBUG2( "Config: including file: %s", file);
1208 fp = fopen(file, "r");
1210 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1211 file, strerror(errno));
1216 * Read the section. It's OK to have EOF without a
1217 * matching close brace.
1219 if (cf_section_read(file, &lineno, fp, cs) < 0) {
1225 * Add the filename to the section
1227 if (stat(file, &buf) == 0) {
1230 mtime = rad_malloc(sizeof(*mtime));
1231 *mtime = buf.st_mtime;
1233 cf_data_add_internal(cs, file, mtime, free,
1242 * Bootstrap a config file.
1244 CONF_SECTION *cf_file_read(const char *file)
1248 cs = cf_section_alloc("main", NULL, NULL);
1249 if (!cs) return NULL;
1251 if (cf_file_include(file, cs) < 0) {
1252 cf_section_free(&cs);
1260 * Return a CONF_PAIR within a CONF_SECTION.
1262 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1266 if (!cs) cs = mainconfig.config;
1269 * Find the name in the tree, for speed.
1275 return rbtree_finddata(cs->pair_tree, &mycp);
1279 * Else find the first one
1281 for (ci = cs->children; ci; ci = ci->next) {
1282 if (ci->type == CONF_ITEM_PAIR)
1283 return cf_itemtopair(ci);
1290 * Return the attr of a CONF_PAIR
1293 char *cf_pair_attr(CONF_PAIR *pair)
1295 return (pair ? pair->attr : NULL);
1299 * Return the value of a CONF_PAIR
1302 char *cf_pair_value(CONF_PAIR *pair)
1304 return (pair ? pair->value : NULL);
1308 * Return the first label of a CONF_SECTION
1311 const char *cf_section_name1(const CONF_SECTION *cs)
1313 return (cs ? cs->name1 : NULL);
1317 * Return the second label of a CONF_SECTION
1320 const char *cf_section_name2(const CONF_SECTION *cs)
1322 return (cs ? cs->name2 : NULL);
1326 * Find a value in a CONF_SECTION
1328 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1332 cp = cf_pair_find(cs, attr);
1334 return (cp ? cp->value : NULL);
1338 * Return the next pair after a CONF_PAIR
1339 * with a certain name (char *attr) If the requested
1340 * attr is NULL, any attr matches.
1343 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1344 const CONF_PAIR *pair, const char *attr)
1349 * If pair is NULL this must be a first time run
1350 * Find the pair with correct name
1354 return cf_pair_find(cs, attr);
1357 ci = cf_pairtoitem(pair)->next;
1359 for (; ci; ci = ci->next) {
1360 if (ci->type != CONF_ITEM_PAIR)
1362 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1366 return cf_itemtopair(ci);
1370 * Find a CONF_SECTION, or return the root if name is NULL
1373 CONF_SECTION *cf_section_find(const char *name)
1376 return cf_section_sub_find(mainconfig.config, name);
1378 return mainconfig.config;
1382 * Find a sub-section in a section
1385 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1390 * Do the fast lookup if possible.
1392 if (name && cs->section_tree) {
1397 return rbtree_finddata(cs->section_tree, &mycs);
1400 for (ci = cs->children; ci; ci = ci->next) {
1401 if (ci->type != CONF_ITEM_SECTION)
1403 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1407 return cf_itemtosection(ci);
1413 * Find a CONF_SECTION with both names.
1415 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1416 const char *name1, const char *name2)
1420 if (!name2) return cf_section_sub_find(cs, name1);
1422 if (!cs) cs = mainconfig.config;
1424 if (name1 && (cs->section_tree)) {
1425 CONF_SECTION mycs, *master_cs;
1430 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1432 return rbtree_finddata(master_cs->name2_tree, &mycs);
1437 * Else do it the old-fashioned way.
1439 for (ci = cs->children; ci; ci = ci->next) {
1440 CONF_SECTION *subcs;
1442 if (ci->type != CONF_ITEM_SECTION)
1445 subcs = cf_itemtosection(ci);
1447 if (!subcs->name2) {
1448 if (strcmp(subcs->name1, name2) == 0) break;
1450 if (strcmp(subcs->name2, name2) == 0) break;
1452 continue; /* don't do the string comparisons below */
1455 if ((strcmp(subcs->name1, name1) == 0) &&
1456 (subcs->name2 != NULL) &&
1457 (strcmp(subcs->name2, name2) == 0))
1461 return cf_itemtosection(ci);
1465 * Return the next subsection after a CONF_SECTION
1466 * with a certain name1 (char *name1). If the requested
1467 * name1 is NULL, any name1 matches.
1470 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1471 CONF_SECTION *subsection,
1477 * If subsection is NULL this must be a first time run
1478 * Find the subsection with correct name
1481 if (subsection == NULL){
1482 ci = section->children;
1484 ci = cf_sectiontoitem(subsection)->next;
1487 for (; ci; ci = ci->next) {
1488 if (ci->type != CONF_ITEM_SECTION)
1490 if ((name1 == NULL) ||
1491 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1495 return cf_itemtosection(ci);
1499 * Return the next item after a CONF_ITEM.
1502 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1505 * If item is NULL this must be a first time run
1506 * Return the first item
1510 return section->children;
1516 int cf_section_lineno(CONF_SECTION *section)
1518 return cf_sectiontoitem(section)->lineno;
1521 int cf_pair_lineno(CONF_PAIR *pair)
1523 return cf_pairtoitem(pair)->lineno;
1526 int cf_item_is_section(CONF_ITEM *item)
1528 return item->type == CONF_ITEM_SECTION;
1530 int cf_item_is_pair(CONF_ITEM *item)
1532 return item->type == CONF_ITEM_PAIR;
1536 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1537 void *data, void (*data_free)(void *))
1541 cd = rad_malloc(sizeof(*cd));
1542 memset(cd, 0, sizeof(*cd));
1544 cd->item.type = CONF_ITEM_DATA;
1545 cd->item.parent = parent;
1546 cd->name = strdup(name);
1548 cd->free = data_free;
1554 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1557 if (!cs || !name) return NULL;
1560 * Find the name in the tree, for speed.
1562 if (cs->data_tree) {
1563 CONF_DATA mycd, *cd;
1567 cd = rbtree_finddata(cs->data_tree, &mycd);
1568 if (cd) return cd->data;
1575 * Find data from a particular section.
1577 void *cf_data_find(CONF_SECTION *cs, const char *name)
1579 return cf_data_find_internal(cs, name, 0);
1584 * Add named data to a configuration section.
1586 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1587 void *data, void (*data_free)(void *),
1592 if (!cs || !name) return -1;
1595 * Already exists. Can't add it.
1597 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1599 cd = cf_data_alloc(cs, name, data, data_free);
1603 cf_item_add(cs, cf_datatoitem(cd));
1609 * Add named data to a configuration section.
1611 int cf_data_add(CONF_SECTION *cs, const char *name,
1612 void *data, void (*data_free)(void *))
1614 return cf_data_add_internal(cs, name, data, data_free, 0);
1619 * Copy CONF_DATA from src to dst
1621 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1624 CONF_ITEM *cd, *next, **last;
1627 * Don't check if s->data_tree is NULL. It's child
1628 * sections may have data, even if this section doesn't.
1631 rad_assert(d->data_tree == NULL);
1632 d->data_tree = s->data_tree;
1633 s->data_tree = NULL;
1636 * Walk through src, moving CONF_ITEM_DATA
1639 last = &(s->children);
1640 for (cd = s->children; cd != NULL; cd = next) {
1644 * Recursively copy data from child sections.
1646 if (cd->type == CONF_ITEM_SECTION) {
1647 CONF_SECTION *s1, *d1;
1649 s1 = cf_itemtosection(cd);
1650 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1652 cf_section_copy_data(s1, d1);
1659 * Not conf data, remember last ptr.
1661 if (cd->type != CONF_ITEM_DATA) {
1667 * Remove it from the src list
1673 * Add it to the dst list
1676 rad_assert(d->tail == NULL);
1679 rad_assert(d->tail != NULL);
1687 * For a CONF_DATA element, stat the filename, if necessary.
1689 static int filename_stat(void *context, void *data)
1692 CONF_DATA *cd = data;
1694 context = context; /* -Wunused */
1696 if (cd->flag != PW_TYPE_FILENAME) return 0;
1698 if (stat(cd->name, &buf) < 0) return -1;
1700 if (buf.st_mtime != *(time_t *) cd->data) return -1;
1707 * Compare two CONF_SECTIONS. The items MUST be in the same
1710 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1712 CONF_ITEM *ca = a->children;
1713 CONF_ITEM *cb = b->children;
1721 if (!ca && !cb) break;
1726 if (ca && ca->type == CONF_ITEM_DATA) {
1730 if (cb && cb->type == CONF_ITEM_DATA) {
1736 * One is smaller than the other. Exit.
1738 if (!ca || !cb) return 0;
1740 if (ca->type != cb->type) return 0;
1743 * Deal with subsections.
1745 if (ca->type == CONF_ITEM_SECTION) {
1746 CONF_SECTION *sa = cf_itemtosection(ca);
1747 CONF_SECTION *sb = cf_itemtosection(cb);
1749 if (!cf_section_cmp(sa, sb)) return 0;
1753 rad_assert(ca->type == CONF_ITEM_PAIR);
1755 pa = cf_itemtopair(ca);
1756 pb = cf_itemtopair(cb);
1759 * Different attr and/or value, Exit.
1761 if ((strcmp(pa->attr, pb->attr) != 0) ||
1762 (strcmp(pa->value, pb->value) != 0)) return 0;
1766 * And go to the next element.
1774 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
1777 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
1782 * They must be the same, say so.
1791 * Migrate CONF_DATA from one section to another.
1793 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1796 CONF_SECTION *s, *d;
1798 for (ci = src->children; ci != NULL; ci = ci->next) {
1799 if (ci->type != CONF_ITEM_SECTION)
1802 s = cf_itemtosection(ci);
1803 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1805 if (!d) continue; /* not in new one, don't migrate it */
1808 * A section of the same name is in BOTH src & dst,
1809 * compare the CONF_PAIR's. If they're all the same,
1810 * then copy the CONF_DATA from one to the other.
1812 if (cf_section_cmp(s, d)) {
1813 cf_section_copy_data(s, d);
1817 return 1; /* rcode means anything? */
1823 * JMG dump_config tries to dump the config structure in a readable format
1827 static int dump_config_section(CONF_SECTION *cs, int indent)
1833 /* The DEBUG macro doesn't let me
1834 * for(i=0;i<indent;++i) debugputchar('\t');
1835 * so I had to get creative. --Pac. */
1837 for (ci = cs->children; ci; ci = ci->next) {
1839 case CONF_ITEM_PAIR:
1840 cp=cf_itemtopair(ci);
1841 DEBUG("%.*s%s = %s",
1842 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1843 cp->attr, cp->value);
1846 case CONF_ITEM_SECTION:
1847 scs=cf_itemtosection(ci);
1848 DEBUG("%.*s%s %s%s{",
1849 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1851 scs->name2 ? scs->name2 : "",
1852 scs->name2 ? " " : "");
1853 dump_config_section(scs, indent+1);
1855 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1858 default: /* FIXME: Do more! */
1866 int dump_config(void)
1868 return dump_config_section(mainconfig.config, 0);