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.
279 if (base || !variables) return;
282 * Free up dynamically allocated string pointers.
284 for (i = 0; variables[i].name != NULL; i++) {
287 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
288 (variables[i].type != PW_TYPE_FILENAME)) {
293 * Prefer the data, if it's there.
294 * Else use the base + offset.
296 if (!variables[i].data) {
303 p = (char **) (variables[i].data);
313 * Free a CONF_SECTION
315 void cf_section_free(CONF_SECTION **cs)
317 CONF_ITEM *ci, *next;
319 if (!cs || !*cs) return;
321 if ((*cs)->variables) {
322 cf_section_parse_free((*cs)->base, (*cs)->variables);
325 for (ci = (*cs)->children; ci; ci = next) {
329 case CONF_ITEM_PAIR: {
330 CONF_PAIR *pair = cf_itemtopair(ci);
335 case CONF_ITEM_SECTION: {
337 CONF_SECTION *section = cf_itemtosection(ci);
338 cf_section_free(§ion);
342 case CONF_ITEM_DATA: {
343 CONF_DATA *data = cf_itemtodata(ci);
348 default: /* should really be an error. */
357 if ((*cs)->pair_tree)
358 rbtree_free((*cs)->pair_tree);
359 if ((*cs)->section_tree)
360 rbtree_free((*cs)->section_tree);
361 if ((*cs)->name2_tree)
362 rbtree_free((*cs)->name2_tree);
363 if ((*cs)->data_tree)
364 rbtree_free((*cs)->data_tree);
367 * And free the section
370 memset(*cs, 0, sizeof(*cs));
379 * Allocate a CONF_SECTION
381 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
382 CONF_SECTION *parent)
386 if (!name1) return NULL;
388 cs = rad_malloc(sizeof(*cs));
389 memset(cs, 0, sizeof(*cs));
390 cs->item.type = CONF_ITEM_SECTION;
391 cs->item.parent = parent;
392 cs->name1 = strdup(name1);
394 cf_section_free(&cs);
398 if (name2 && *name2) {
399 cs->name2 = strdup(name2);
401 cf_section_free(&cs);
405 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
406 if (!cs->pair_tree) {
407 cf_section_free(&cs);
412 * Don't create a data tree, it may not be needed.
416 * Don't create the section tree here, it may not
424 * Add an item to a configuration section.
426 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
429 rad_assert(cs->tail == NULL);
432 rad_assert(cs->tail != NULL);
437 * Update the trees (and tail) for each item added.
439 for (/* nothing */; ci != NULL; ci = ci->next) {
443 * For fast lookups, pair's and sections get
448 rbtree_insert(cs->pair_tree, ci);
451 case CONF_ITEM_SECTION: {
452 const CONF_SECTION *cs_new = cf_itemtosection(ci);
454 if (!cs->section_tree) {
455 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
456 /* ignore any errors */
459 if (cs->section_tree) {
460 rbtree_insert(cs->section_tree, cs_new); }
463 * Two names: find the named instance.
466 CONF_SECTION *old_cs;
470 * CONF_SECTION having
471 * the given name1, and
475 old_cs = rbtree_finddata(cs->section_tree, cs_new);
476 if (!old_cs) return; /* this is a bad error! */
478 if (!old_cs->name2_tree) {
479 old_cs->name2_tree = rbtree_create(name2_cmp,
482 if (old_cs->name2_tree) {
483 rbtree_insert(old_cs->name2_tree, cs_new);
487 } /* was a section */
490 if (!cs->data_tree) {
491 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
494 rbtree_insert(cs->data_tree, ci);
498 default: /* FIXME: assert & error! */
501 } /* switch over conf types */
506 * Expand the variables in an input string.
508 static const char *cf_expand_variables(const char *cf, int *lineno,
509 const CONF_SECTION *outercs,
510 char *output, const char *input)
513 const char *end, *ptr;
515 const CONF_SECTION *parentcs;
518 * Find the master parent conf section.
519 * We can't use mainconfig.config, because we're in the
520 * process of re-building it, and it isn't set up yet...
522 for (parentcs = outercs;
523 parentcs->item.parent != NULL;
524 parentcs = parentcs->item.parent) {
532 * Ignore anything other than "${"
534 if ((*ptr == '$') && (ptr[1] == '{')) {
537 const CONF_SECTION *cs;
540 * FIXME: Add support for ${foo:-bar},
545 * Look for trailing '}', and log a
546 * warning for anything that doesn't match,
547 * and exit with a fatal error.
549 end = strchr(ptr, '}');
552 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
563 * ${.foo} means "foo from the current section"
571 * ${..foo} means "foo from the section
572 * enclosing this section" (etc.)
574 while (*ptr == '.') {
576 cs = cs->item.parent;
583 * ${foo} is local, with
584 * main as lower priority
589 * ${foo.bar.baz} is always rooted
592 for (q = ptr; *q && q != end; q++) {
604 * Find the next section.
607 (*ptr != 0) && (*ptr != '.') &&
615 * The character is a '.', find a
616 * section (as the user has given
617 * us a subsection to find)
622 ptr++; /* skip the period */
625 * Find the sub-section.
627 next = cf_section_sub_find(cs, name);
629 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
634 } else { /* no period, must be a conf-part */
636 * Find in the current referenced
639 cp = cf_pair_find(cs, name);
642 * It it was NOT ${..foo}
644 * top-level config items.
646 if (!up) cp = cf_pair_find(parentcs, name);
649 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
650 cf_section_name1(cs),
655 } /* until cp is non-NULL */
658 * Substitute the value of the variable.
660 strcpy(p, cp->value);
664 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
670 * Look for trailing '}', and log a
671 * warning for anything that doesn't match,
672 * and exit with a fatal error.
674 end = strchr(ptr, '}');
677 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
682 memcpy(name, ptr, end - ptr);
683 name[end - ptr] = '\0';
686 * Get the environment variable.
687 * If none exists, then make it an empty string.
701 * Copy it over verbatim.
705 } /* loop over all of the input string. */
714 * Parses an item (not a CONF_ITEM) into the specified format,
715 * with a default value.
717 * Returns -1 on error, 0 for correctly parsed, and 1 if the
718 * default value was used. Note that the default value will be
719 * used ONLY if the CONF_PAIR is NULL.
721 int cf_item_parse(CONF_SECTION *cs, const char *name,
722 int type, void *data, const char *dflt)
727 lrad_ipaddr_t ipaddr;
731 cp = cf_pair_find(cs, name);
736 return 1; /* nothing to parse, return default value */
744 case PW_TYPE_BOOLEAN:
746 * Allow yes/no and on/off
748 if ((strcasecmp(value, "yes") == 0) ||
749 (strcasecmp(value, "on") == 0)) {
751 } else if ((strcasecmp(value, "no") == 0) ||
752 (strcasecmp(value, "off") == 0)) {
756 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
759 DEBUG2(" %s: %s = %s", cs->name1, name, value);
762 case PW_TYPE_INTEGER:
763 *(int *)data = strtol(value, 0, 0);
764 DEBUG2(" %s: %s = %d",
769 case PW_TYPE_STRING_PTR:
776 * Expand variables which haven't already been
777 * expanded automagically when the configuration
783 int lineno = cs->item.lineno;
786 * FIXME: sizeof(buffer)?
788 value = cf_expand_variables("?",
791 if (!value) return -1;
794 DEBUG2(" %s: %s = \"%s\"",
796 value ? value : "(null)");
797 *q = value ? strdup(value) : NULL;
801 * This is the same as PW_TYPE_STRING_PTR,
802 * except that we also "stat" the file, and
805 case PW_TYPE_FILENAME:
812 * Expand variables which haven't already been
813 * expanded automagically when the configuration
819 int lineno = cs->item.lineno;
822 * FIXME: sizeof(buffer)?
824 value = cf_expand_variables("?",
827 if (!value) return -1;
830 DEBUG2(" %s: %s = \"%s\"",
832 value ? value : "(null)");
833 *q = value ? strdup(value) : NULL;
836 * And now we "stat" the file.
841 if (stat(*q, &buf) == 0) {
844 mtime = rad_malloc(sizeof(*mtime));
845 *mtime = buf.st_mtime;
847 cf_data_add_internal(cs, *q, mtime, free,
855 * Allow '*' as any address
857 if (strcmp(value, "*") == 0) {
858 *(uint32_t *) data = htonl(INADDR_ANY);
859 DEBUG2(" %s: %s = *", cs->name1, name);
862 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
863 radlog(L_ERR, "Can't find IP address for host %s", value);
866 DEBUG2(" %s: %s = %s IP address [%s]",
867 cs->name1, name, value,
868 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
869 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
872 case PW_TYPE_IPV6ADDR:
873 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
874 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
877 DEBUG2(" %s: %s = %s IPv6 address [%s]",
878 cs->name1, name, value,
879 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
880 memcpy(data, &ipaddr.ipaddr.ip6addr,
881 sizeof(ipaddr.ipaddr.ip6addr));
885 radlog(L_ERR, "type %d not supported yet", type);
888 } /* switch over variable type */
894 * Parse a configuration section into user-supplied variables.
896 int cf_section_parse(CONF_SECTION *cs, void *base,
897 const CONF_PARSER *variables)
903 * Handle the known configuration parameters.
905 for (i = 0; variables[i].name != NULL; i++) {
907 * Handle subsections specially
909 if (variables[i].type == PW_TYPE_SUBSECTION) {
910 const CONF_SECTION *subcs;
911 subcs = cf_section_sub_find(cs, variables[i].name);
914 * If the configuration section is NOT there,
917 * FIXME! This is probably wrong... we should
918 * probably set the items to their default values.
920 if (!subcs) continue;
922 if (!variables[i].dflt) {
923 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
924 cf_section_parse_free(base, variables);
928 if (cf_section_parse(subcs, base,
929 (const CONF_PARSER *) variables[i].dflt) < 0) {
930 cf_section_parse_free(base, variables);
934 } /* else it's a CONF_PAIR */
936 if (variables[i].data) {
937 data = variables[i].data; /* prefer this. */
939 data = ((char *)base) + variables[i].offset;
941 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
942 cf_section_parse_free(base, variables);
947 * Parse the pair we found, or a default value.
949 if (cf_item_parse(cs, variables[i].name, variables[i].type,
950 data, variables[i].dflt) < 0) {
951 cf_section_parse_free(base, variables);
954 } /* for all variables in the configuration section */
957 cs->variables = variables;
964 * Read a part of the config file.
966 static int cf_section_read(const char *file, int *lineno, FILE *fp,
967 CONF_SECTION *current)
970 CONF_SECTION *this, *css;
982 this = current; /* add items here */
985 * Read, checking for line continuations ('\\' at EOL)
991 * Get data, and remember if we are at EOF.
993 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
999 * We've filled the buffer, and there isn't
1002 if ((len == (sizeof(buf) - 1)) &&
1003 (cbuf[len - 1] != '\n')) {
1004 radlog(L_ERR, "%s[%d]: Line too long",
1010 * Check for continuations.
1012 if (cbuf[len - 1] == '\n') len--;
1015 * Last character is '\\'. Over-write it,
1016 * and read another line.
1018 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1019 cbuf[len - 1] = '\0';
1025 * We're at EOF, and haven't read anything. Stop.
1027 if (eof && (cbuf == buf)) {
1032 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1034 if ((*buf1 == '#') || (*buf1 == '\0')) {
1039 * The caller eats "name1 name2 {", and calls us
1040 * for the data inside of the section. So if we
1041 * receive a closing brace, then it must mean the
1042 * end of the section.
1044 if (t1 == T_RCBRACE) {
1045 if (this == current) {
1046 radlog(L_ERR, "%s[%d]: Too many closing braces",
1051 this = this->item.parent;
1056 * Allow for $INCLUDE files
1058 * This *SHOULD* work for any level include.
1059 * I really really really hate this file. -cparker
1061 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1062 t2 = getword(&ptr, buf2, sizeof(buf2));
1064 value = cf_expand_variables(file, lineno, this, buf, buf2);
1065 if (!value) return -1;
1067 #ifdef HAVE_DIRENT_H
1071 * Include ALL non-"dot" files in the directory.
1074 if (value[strlen(value) - 1] == '/') {
1077 struct stat stat_buf;
1079 DEBUG2( "Config: including files in directory: %s", value );
1080 dir = opendir(value);
1082 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1083 file, *lineno, value,
1089 * Read the directory, ignoring "." files.
1091 while ((dp = readdir(dir)) != NULL) {
1094 if (dp->d_name[0] == '.') continue;
1097 * Check for valid characters
1099 for (p = dp->d_name; *p != '\0'; p++) {
1100 if (isalpha((int)*p) ||
1103 (*p == '.')) continue;
1106 if (*p != '\0') continue;
1108 snprintf(buf2, sizeof(buf2), "%s%s",
1110 if ((stat(buf2, &stat_buf) != 0) ||
1111 S_ISDIR(stat_buf.st_mode)) continue;
1113 * Read the file into the current
1114 * configuration sectoin.
1116 if (cf_file_include(buf2, this) < 0) {
1124 { /* it was a normal file */
1125 if (cf_file_include(value, this) < 0) {
1130 } /* we were in an include */
1133 * No '=': must be a section or sub-section.
1135 if (strchr(ptr, '=') == NULL) {
1136 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1137 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1139 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1140 t3 = getword(&ptr, buf3, sizeof(buf3));
1144 * Perhaps a subsection.
1146 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1147 css = cf_section_alloc(buf1,
1148 t2 == T_LCBRACE ? NULL : buf2,
1151 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1154 cf_item_add(this, cf_sectiontoitem(css));
1155 css->item.lineno = *lineno;
1158 * The current section is now the child section.
1165 * Ignore semi-colons.
1171 * Must be a normal attr = value line.
1173 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1175 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1176 (t2 < T_EQSTART || t2 > T_EQEND)) {
1177 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1183 * Ensure that the user can't add CONF_PAIRs
1184 * with 'internal' names;
1186 if (buf1[0] == '_') {
1187 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1188 file, *lineno, buf1);
1193 * Handle variable substitution via ${foo}
1195 value = cf_expand_variables(file, lineno, this, buf, buf3);
1196 if (!value) return -1;
1200 * Add this CONF_PAIR to our CONF_SECTION
1202 cpn = cf_pair_alloc(buf1, value, t2, this);
1203 cpn->item.lineno = *lineno;
1204 cf_item_add(this, cf_pairtoitem(cpn));
1208 * See if EOF was unexpected ..
1210 if (feof(fp) && (this != current)) {
1211 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1213 cf_section_name1(this), cf_section_lineno(this));
1221 * Include one config file in another.
1223 int cf_file_include(const char *file, CONF_SECTION *cs)
1227 struct stat statbuf;
1230 DEBUG2( "Config: including file: %s", file);
1232 if (stat(file, &statbuf) == 0) {
1233 if ((statbuf.st_mode & S_IWOTH) != 0) {
1234 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1239 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1240 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1246 fp = fopen(file, "r");
1248 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1249 file, strerror(errno));
1254 * Read the section. It's OK to have EOF without a
1255 * matching close brace.
1257 if (cf_section_read(file, &lineno, fp, cs) < 0) {
1263 * Add the filename to the section
1265 mtime = rad_malloc(sizeof(*mtime));
1266 *mtime = statbuf.st_mtime;
1268 cf_data_add_internal(cs, file, mtime, free,
1276 * Bootstrap a config file.
1278 CONF_SECTION *cf_file_read(const char *file)
1282 cs = cf_section_alloc("main", NULL, NULL);
1283 if (!cs) return NULL;
1285 if (cf_file_include(file, cs) < 0) {
1286 cf_section_free(&cs);
1294 * Return a CONF_PAIR within a CONF_SECTION.
1296 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1300 if (!cs) cs = mainconfig.config;
1303 * Find the name in the tree, for speed.
1309 return rbtree_finddata(cs->pair_tree, &mycp);
1313 * Else find the first one
1315 for (ci = cs->children; ci; ci = ci->next) {
1316 if (ci->type == CONF_ITEM_PAIR)
1317 return cf_itemtopair(ci);
1324 * Return the attr of a CONF_PAIR
1327 char *cf_pair_attr(CONF_PAIR *pair)
1329 return (pair ? pair->attr : NULL);
1333 * Return the value of a CONF_PAIR
1336 char *cf_pair_value(CONF_PAIR *pair)
1338 return (pair ? pair->value : NULL);
1342 * Return the first label of a CONF_SECTION
1345 const char *cf_section_name1(const CONF_SECTION *cs)
1347 return (cs ? cs->name1 : NULL);
1351 * Return the second label of a CONF_SECTION
1354 const char *cf_section_name2(const CONF_SECTION *cs)
1356 return (cs ? cs->name2 : NULL);
1360 * Find a value in a CONF_SECTION
1362 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1366 cp = cf_pair_find(cs, attr);
1368 return (cp ? cp->value : NULL);
1372 * Return the next pair after a CONF_PAIR
1373 * with a certain name (char *attr) If the requested
1374 * attr is NULL, any attr matches.
1377 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1378 const CONF_PAIR *pair, const char *attr)
1383 * If pair is NULL this must be a first time run
1384 * Find the pair with correct name
1388 return cf_pair_find(cs, attr);
1391 ci = cf_pairtoitem(pair)->next;
1393 for (; ci; ci = ci->next) {
1394 if (ci->type != CONF_ITEM_PAIR)
1396 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1400 return cf_itemtopair(ci);
1404 * Find a CONF_SECTION, or return the root if name is NULL
1407 CONF_SECTION *cf_section_find(const char *name)
1410 return cf_section_sub_find(mainconfig.config, name);
1412 return mainconfig.config;
1416 * Find a sub-section in a section
1419 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1424 * Do the fast lookup if possible.
1426 if (name && cs->section_tree) {
1431 return rbtree_finddata(cs->section_tree, &mycs);
1434 for (ci = cs->children; ci; ci = ci->next) {
1435 if (ci->type != CONF_ITEM_SECTION)
1437 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1441 return cf_itemtosection(ci);
1447 * Find a CONF_SECTION with both names.
1449 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1450 const char *name1, const char *name2)
1454 if (!name2) return cf_section_sub_find(cs, name1);
1456 if (!cs) cs = mainconfig.config;
1458 if (name1 && (cs->section_tree)) {
1459 CONF_SECTION mycs, *master_cs;
1464 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1466 return rbtree_finddata(master_cs->name2_tree, &mycs);
1471 * Else do it the old-fashioned way.
1473 for (ci = cs->children; ci; ci = ci->next) {
1474 CONF_SECTION *subcs;
1476 if (ci->type != CONF_ITEM_SECTION)
1479 subcs = cf_itemtosection(ci);
1481 if (!subcs->name2) {
1482 if (strcmp(subcs->name1, name2) == 0) break;
1484 if (strcmp(subcs->name2, name2) == 0) break;
1486 continue; /* don't do the string comparisons below */
1489 if ((strcmp(subcs->name1, name1) == 0) &&
1490 (subcs->name2 != NULL) &&
1491 (strcmp(subcs->name2, name2) == 0))
1495 return cf_itemtosection(ci);
1499 * Return the next subsection after a CONF_SECTION
1500 * with a certain name1 (char *name1). If the requested
1501 * name1 is NULL, any name1 matches.
1504 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1505 CONF_SECTION *subsection,
1511 * If subsection is NULL this must be a first time run
1512 * Find the subsection with correct name
1515 if (subsection == NULL){
1516 ci = section->children;
1518 ci = cf_sectiontoitem(subsection)->next;
1521 for (; ci; ci = ci->next) {
1522 if (ci->type != CONF_ITEM_SECTION)
1524 if ((name1 == NULL) ||
1525 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1529 return cf_itemtosection(ci);
1533 * Return the next item after a CONF_ITEM.
1536 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1539 * If item is NULL this must be a first time run
1540 * Return the first item
1544 return section->children;
1550 int cf_section_lineno(CONF_SECTION *section)
1552 return cf_sectiontoitem(section)->lineno;
1555 int cf_pair_lineno(CONF_PAIR *pair)
1557 return cf_pairtoitem(pair)->lineno;
1560 int cf_item_is_section(CONF_ITEM *item)
1562 return item->type == CONF_ITEM_SECTION;
1564 int cf_item_is_pair(CONF_ITEM *item)
1566 return item->type == CONF_ITEM_PAIR;
1570 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1571 void *data, void (*data_free)(void *))
1575 cd = rad_malloc(sizeof(*cd));
1576 memset(cd, 0, sizeof(*cd));
1578 cd->item.type = CONF_ITEM_DATA;
1579 cd->item.parent = parent;
1580 cd->name = strdup(name);
1582 cd->free = data_free;
1588 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1591 if (!cs || !name) return NULL;
1594 * Find the name in the tree, for speed.
1596 if (cs->data_tree) {
1597 CONF_DATA mycd, *cd;
1601 cd = rbtree_finddata(cs->data_tree, &mycd);
1602 if (cd) return cd->data;
1609 * Find data from a particular section.
1611 void *cf_data_find(CONF_SECTION *cs, const char *name)
1613 return cf_data_find_internal(cs, name, 0);
1618 * Add named data to a configuration section.
1620 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1621 void *data, void (*data_free)(void *),
1626 if (!cs || !name) return -1;
1629 * Already exists. Can't add it.
1631 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1633 cd = cf_data_alloc(cs, name, data, data_free);
1637 cf_item_add(cs, cf_datatoitem(cd));
1643 * Add named data to a configuration section.
1645 int cf_data_add(CONF_SECTION *cs, const char *name,
1646 void *data, void (*data_free)(void *))
1648 return cf_data_add_internal(cs, name, data, data_free, 0);
1653 * Copy CONF_DATA from src to dst
1655 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1658 CONF_ITEM *cd, *next, **last;
1661 * Don't check if s->data_tree is NULL. It's child
1662 * sections may have data, even if this section doesn't.
1665 rad_assert(d->data_tree == NULL);
1666 d->data_tree = s->data_tree;
1667 s->data_tree = NULL;
1670 * Walk through src, moving CONF_ITEM_DATA
1673 last = &(s->children);
1674 for (cd = s->children; cd != NULL; cd = next) {
1678 * Recursively copy data from child sections.
1680 if (cd->type == CONF_ITEM_SECTION) {
1681 CONF_SECTION *s1, *d1;
1683 s1 = cf_itemtosection(cd);
1684 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1686 cf_section_copy_data(s1, d1);
1693 * Not conf data, remember last ptr.
1695 if (cd->type != CONF_ITEM_DATA) {
1701 * Remove it from the src list
1707 * Add it to the dst list
1710 rad_assert(d->tail == NULL);
1713 rad_assert(d->tail != NULL);
1721 * For a CONF_DATA element, stat the filename, if necessary.
1723 static int filename_stat(void *context, void *data)
1726 CONF_DATA *cd = data;
1728 context = context; /* -Wunused */
1730 if (cd->flag != PW_TYPE_FILENAME) return 0;
1732 if (stat(cd->name, &buf) < 0) return -1;
1734 if (buf.st_mtime != *(time_t *) cd->data) return -1;
1741 * Compare two CONF_SECTIONS. The items MUST be in the same
1744 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1746 CONF_ITEM *ca = a->children;
1747 CONF_ITEM *cb = b->children;
1755 if (!ca && !cb) break;
1760 if (ca && ca->type == CONF_ITEM_DATA) {
1764 if (cb && cb->type == CONF_ITEM_DATA) {
1770 * One is smaller than the other. Exit.
1772 if (!ca || !cb) return 0;
1774 if (ca->type != cb->type) return 0;
1777 * Deal with subsections.
1779 if (ca->type == CONF_ITEM_SECTION) {
1780 CONF_SECTION *sa = cf_itemtosection(ca);
1781 CONF_SECTION *sb = cf_itemtosection(cb);
1783 if (!cf_section_cmp(sa, sb)) return 0;
1787 rad_assert(ca->type == CONF_ITEM_PAIR);
1789 pa = cf_itemtopair(ca);
1790 pb = cf_itemtopair(cb);
1793 * Different attr and/or value, Exit.
1795 if ((strcmp(pa->attr, pb->attr) != 0) ||
1796 (strcmp(pa->value, pb->value) != 0)) return 0;
1800 * And go to the next element.
1808 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
1811 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
1816 * They must be the same, say so.
1825 * Migrate CONF_DATA from one section to another.
1827 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1830 CONF_SECTION *s, *d;
1832 for (ci = src->children; ci != NULL; ci = ci->next) {
1833 if (ci->type != CONF_ITEM_SECTION)
1836 s = cf_itemtosection(ci);
1837 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1839 if (!d) continue; /* not in new one, don't migrate it */
1842 * A section of the same name is in BOTH src & dst,
1843 * compare the CONF_PAIR's. If they're all the same,
1844 * then copy the CONF_DATA from one to the other.
1846 if (cf_section_cmp(s, d)) {
1847 cf_section_copy_data(s, d);
1851 return 1; /* rcode means anything? */
1857 * JMG dump_config tries to dump the config structure in a readable format
1861 static int dump_config_section(CONF_SECTION *cs, int indent)
1867 /* The DEBUG macro doesn't let me
1868 * for(i=0;i<indent;++i) debugputchar('\t');
1869 * so I had to get creative. --Pac. */
1871 for (ci = cs->children; ci; ci = ci->next) {
1873 case CONF_ITEM_PAIR:
1874 cp=cf_itemtopair(ci);
1875 DEBUG("%.*s%s = %s",
1876 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1877 cp->attr, cp->value);
1880 case CONF_ITEM_SECTION:
1881 scs=cf_itemtosection(ci);
1882 DEBUG("%.*s%s %s%s{",
1883 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1885 scs->name2 ? scs->name2 : "",
1886 scs->name2 ? " " : "");
1887 dump_config_section(scs, indent+1);
1889 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1892 default: /* FIXME: Do more! */
1900 int dump_config(void)
1902 return dump_config_section(mainconfig.config, 0);