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) {
292 * Prefer the data, if it's there.
293 * Else use the base + offset.
295 if (!variables[i].data) {
302 p = (char **) (variables[i].data);
312 * Free a CONF_SECTION
314 void cf_section_free(CONF_SECTION **cs)
316 CONF_ITEM *ci, *next;
318 if (!cs || !*cs) return;
320 if ((*cs)->variables) {
321 cf_section_parse_free((*cs)->base, (*cs)->variables);
324 for (ci = (*cs)->children; ci; ci = next) {
328 case CONF_ITEM_PAIR: {
329 CONF_PAIR *pair = cf_itemtopair(ci);
334 case CONF_ITEM_SECTION: {
336 CONF_SECTION *section = cf_itemtosection(ci);
337 cf_section_free(§ion);
341 case CONF_ITEM_DATA: {
342 CONF_DATA *data = cf_itemtodata(ci);
347 default: /* should really be an error. */
356 if ((*cs)->pair_tree)
357 rbtree_free((*cs)->pair_tree);
358 if ((*cs)->section_tree)
359 rbtree_free((*cs)->section_tree);
360 if ((*cs)->name2_tree)
361 rbtree_free((*cs)->name2_tree);
362 if ((*cs)->data_tree)
363 rbtree_free((*cs)->data_tree);
366 * And free the section
369 memset(*cs, 0, sizeof(*cs));
378 * Allocate a CONF_SECTION
380 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
381 CONF_SECTION *parent)
385 if (!name1) return NULL;
387 cs = rad_malloc(sizeof(*cs));
388 memset(cs, 0, sizeof(*cs));
389 cs->item.type = CONF_ITEM_SECTION;
390 cs->item.parent = parent;
391 cs->name1 = strdup(name1);
393 cf_section_free(&cs);
397 if (name2 && *name2) {
398 cs->name2 = strdup(name2);
400 cf_section_free(&cs);
404 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
405 if (!cs->pair_tree) {
406 cf_section_free(&cs);
411 * Don't create a data tree, it may not be needed.
415 * Don't create the section tree here, it may not
423 * Add an item to a configuration section.
425 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
428 rad_assert(cs->tail == NULL);
431 rad_assert(cs->tail != NULL);
436 * Update the trees (and tail) for each item added.
438 for (/* nothing */; ci != NULL; ci = ci->next) {
442 * For fast lookups, pair's and sections get
447 rbtree_insert(cs->pair_tree, ci);
450 case CONF_ITEM_SECTION: {
451 const CONF_SECTION *cs_new = cf_itemtosection(ci);
453 if (!cs->section_tree) {
454 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
455 /* ignore any errors */
458 if (cs->section_tree) {
459 rbtree_insert(cs->section_tree, cs_new); }
462 * Two names: find the named instance.
465 CONF_SECTION *old_cs;
469 * CONF_SECTION having
470 * the given name1, and
474 old_cs = rbtree_finddata(cs->section_tree, cs_new);
475 if (!old_cs) return; /* this is a bad error! */
477 if (!old_cs->name2_tree) {
478 old_cs->name2_tree = rbtree_create(name2_cmp,
481 if (old_cs->name2_tree) {
482 rbtree_insert(old_cs->name2_tree, cs_new);
486 } /* was a section */
489 if (!cs->data_tree) {
490 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
493 rbtree_insert(cs->data_tree, ci);
497 default: /* FIXME: assert & error! */
500 } /* switch over conf types */
505 * Expand the variables in an input string.
507 static const char *cf_expand_variables(const char *cf, int *lineno,
508 const CONF_SECTION *outercs,
509 char *output, const char *input)
512 const char *end, *ptr;
514 const CONF_SECTION *parentcs;
517 * Find the master parent conf section.
518 * We can't use mainconfig.config, because we're in the
519 * process of re-building it, and it isn't set up yet...
521 for (parentcs = outercs;
522 parentcs->item.parent != NULL;
523 parentcs = parentcs->item.parent) {
531 * Ignore anything other than "${"
533 if ((*ptr == '$') && (ptr[1] == '{')) {
536 const CONF_SECTION *cs;
539 * FIXME: Add support for ${foo:-bar},
544 * Look for trailing '}', and log a
545 * warning for anything that doesn't match,
546 * and exit with a fatal error.
548 end = strchr(ptr, '}');
551 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
562 * ${.foo} means "foo from the current section"
570 * ${..foo} means "foo from the section
571 * enclosing this section" (etc.)
573 while (*ptr == '.') {
575 cs = cs->item.parent;
582 * ${foo} is local, with
583 * main as lower priority
588 * ${foo.bar.baz} is always rooted
591 for (q = ptr; *q && q != end; q++) {
603 * Find the next section.
606 (*ptr != 0) && (*ptr != '.') &&
614 * The character is a '.', find a
615 * section (as the user has given
616 * us a subsection to find)
621 ptr++; /* skip the period */
624 * Find the sub-section.
626 next = cf_section_sub_find(cs, name);
628 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
633 } else { /* no period, must be a conf-part */
635 * Find in the current referenced
638 cp = cf_pair_find(cs, name);
641 * It it was NOT ${..foo}
643 * top-level config items.
645 if (!up) cp = cf_pair_find(parentcs, name);
648 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
649 cf_section_name1(cs),
654 } /* until cp is non-NULL */
657 * Substitute the value of the variable.
659 strcpy(p, cp->value);
663 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
669 * Look for trailing '}', and log a
670 * warning for anything that doesn't match,
671 * and exit with a fatal error.
673 end = strchr(ptr, '}');
676 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
681 memcpy(name, ptr, end - ptr);
682 name[end - ptr] = '\0';
685 * Get the environment variable.
686 * If none exists, then make it an empty string.
700 * Copy it over verbatim.
704 } /* loop over all of the input string. */
713 * Parses an item (not a CONF_ITEM) into the specified format,
714 * with a default value.
716 * Returns -1 on error, 0 for correctly parsed, and 1 if the
717 * default value was used. Note that the default value will be
718 * used ONLY if the CONF_PAIR is NULL.
720 int cf_item_parse(CONF_SECTION *cs, const char *name,
721 int type, void *data, const char *dflt)
726 lrad_ipaddr_t ipaddr;
730 cp = cf_pair_find(cs, name);
735 return 1; /* nothing to parse, return default value */
743 case PW_TYPE_BOOLEAN:
745 * Allow yes/no and on/off
747 if ((strcasecmp(value, "yes") == 0) ||
748 (strcasecmp(value, "on") == 0)) {
750 } else if ((strcasecmp(value, "no") == 0) ||
751 (strcasecmp(value, "off") == 0)) {
755 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
758 DEBUG2(" %s: %s = %s", cs->name1, name, value);
761 case PW_TYPE_INTEGER:
762 *(int *)data = strtol(value, 0, 0);
763 DEBUG2(" %s: %s = %d",
768 case PW_TYPE_STRING_PTR:
775 * Expand variables which haven't already been
776 * expanded automagically when the configuration
782 int lineno = cs->item.lineno;
785 * FIXME: sizeof(buffer)?
787 value = cf_expand_variables("?",
790 if (!value) return -1;
793 DEBUG2(" %s: %s = \"%s\"",
795 value ? value : "(null)");
796 *q = value ? strdup(value) : NULL;
800 * This is the same as PW_TYPE_STRING_PTR,
801 * except that we also "stat" the file, and
804 case PW_TYPE_FILENAME:
811 * Expand variables which haven't already been
812 * expanded automagically when the configuration
818 int lineno = cs->item.lineno;
821 * FIXME: sizeof(buffer)?
823 value = cf_expand_variables("?",
826 if (!value) return -1;
829 DEBUG2(" %s: %s = \"%s\"",
831 value ? value : "(null)");
832 *q = value ? strdup(value) : NULL;
835 * And now we "stat" the file.
840 if (stat(*q, &buf) == 0) {
843 mtime = rad_malloc(sizeof(*mtime));
844 *mtime = buf.st_mtime;
846 cf_data_add_internal(cs, *q, mtime, free,
854 * Allow '*' as any address
856 if (strcmp(value, "*") == 0) {
857 *(uint32_t *) data = htonl(INADDR_ANY);
858 DEBUG2(" %s: %s = *", cs->name1, name);
861 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
862 radlog(L_ERR, "Can't find IP address for host %s", value);
865 DEBUG2(" %s: %s = %s IP address [%s]",
866 cs->name1, name, value,
867 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
868 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
871 case PW_TYPE_IPV6ADDR:
872 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
873 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
876 DEBUG2(" %s: %s = %s IPv6 address [%s]",
877 cs->name1, name, value,
878 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
879 memcpy(data, &ipaddr.ipaddr.ip6addr,
880 sizeof(ipaddr.ipaddr.ip6addr));
884 radlog(L_ERR, "type %d not supported yet", type);
887 } /* switch over variable type */
893 * Parse a configuration section into user-supplied variables.
895 int cf_section_parse(CONF_SECTION *cs, void *base,
896 const CONF_PARSER *variables)
902 * Handle the known configuration parameters.
904 for (i = 0; variables[i].name != NULL; i++) {
906 * Handle subsections specially
908 if (variables[i].type == PW_TYPE_SUBSECTION) {
909 const CONF_SECTION *subcs;
910 subcs = cf_section_sub_find(cs, variables[i].name);
913 * If the configuration section is NOT there,
916 * FIXME! This is probably wrong... we should
917 * probably set the items to their default values.
919 if (!subcs) continue;
921 if (!variables[i].dflt) {
922 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
923 cf_section_parse_free(base, variables);
927 if (cf_section_parse(subcs, base,
928 (const CONF_PARSER *) variables[i].dflt) < 0) {
929 cf_section_parse_free(base, variables);
933 } /* else it's a CONF_PAIR */
935 if (variables[i].data) {
936 data = variables[i].data; /* prefer this. */
938 data = ((char *)base) + variables[i].offset;
940 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
941 cf_section_parse_free(base, variables);
946 * Parse the pair we found, or a default value.
948 if (cf_item_parse(cs, variables[i].name, variables[i].type,
949 data, variables[i].dflt) < 0) {
950 cf_section_parse_free(base, variables);
953 } /* for all variables in the configuration section */
956 cs->variables = variables;
963 * Read a part of the config file.
965 static int cf_section_read(const char *file, int *lineno, FILE *fp,
966 CONF_SECTION *current)
969 CONF_SECTION *this, *css;
981 this = current; /* add items here */
984 * Read, checking for line continuations ('\\' at EOL)
990 * Get data, and remember if we are at EOF.
992 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
998 * We've filled the buffer, and there isn't
1001 if ((len == (sizeof(buf) - 1)) &&
1002 (cbuf[len - 1] != '\n')) {
1003 radlog(L_ERR, "%s[%d]: Line too long",
1009 * Check for continuations.
1011 if (cbuf[len - 1] == '\n') len--;
1014 * Last character is '\\'. Over-write it,
1015 * and read another line.
1017 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1018 cbuf[len - 1] = '\0';
1024 * We're at EOF, and haven't read anything. Stop.
1026 if (eof && (cbuf == buf)) {
1031 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1033 if ((*buf1 == '#') || (*buf1 == '\0')) {
1038 * The caller eats "name1 name2 {", and calls us
1039 * for the data inside of the section. So if we
1040 * receive a closing brace, then it must mean the
1041 * end of the section.
1043 if (t1 == T_RCBRACE) {
1044 if (this == current) {
1045 radlog(L_ERR, "%s[%d]: Too many closing braces",
1050 this = this->item.parent;
1055 * Allow for $INCLUDE files
1057 * This *SHOULD* work for any level include.
1058 * I really really really hate this file. -cparker
1060 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1061 t2 = getword(&ptr, buf2, sizeof(buf2));
1063 value = cf_expand_variables(file, lineno, this, buf, buf2);
1064 if (!value) return -1;
1066 #ifdef HAVE_DIRENT_H
1070 * Include ALL non-"dot" files in the directory.
1073 if (value[strlen(value) - 1] == '/') {
1076 struct stat stat_buf;
1078 DEBUG2( "Config: including files in directory: %s", value );
1079 dir = opendir(value);
1081 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1082 file, *lineno, value,
1088 * Read the directory, ignoring "." files.
1090 while ((dp = readdir(dir)) != NULL) {
1093 if (dp->d_name[0] == '.') continue;
1096 * Check for valid characters
1098 for (p = dp->d_name; *p != '\0'; p++) {
1099 if (isalpha((int)*p) ||
1102 (*p == '.')) continue;
1105 if (*p != '\0') continue;
1107 snprintf(buf2, sizeof(buf2), "%s%s",
1109 if ((stat(buf2, &stat_buf) != 0) ||
1110 S_ISDIR(stat_buf.st_mode)) continue;
1112 * Read the file into the current
1113 * configuration sectoin.
1115 if (cf_file_include(buf2, this) < 0) {
1123 { /* it was a normal file */
1124 if (cf_file_include(value, this) < 0) {
1129 } /* we were in an include */
1132 * No '=': must be a section or sub-section.
1134 if (strchr(ptr, '=') == NULL) {
1135 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1136 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1138 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1139 t3 = getword(&ptr, buf3, sizeof(buf3));
1143 * Perhaps a subsection.
1145 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1146 css = cf_section_alloc(buf1,
1147 t2 == T_LCBRACE ? NULL : buf2,
1150 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1153 cf_item_add(this, cf_sectiontoitem(css));
1154 css->item.lineno = *lineno;
1157 * The current section is now the child section.
1164 * Ignore semi-colons.
1170 * Must be a normal attr = value line.
1172 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1174 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1175 (t2 < T_EQSTART || t2 > T_EQEND)) {
1176 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1182 * Ensure that the user can't add CONF_PAIRs
1183 * with 'internal' names;
1185 if (buf1[0] == '_') {
1186 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1187 file, *lineno, buf1);
1192 * Handle variable substitution via ${foo}
1194 value = cf_expand_variables(file, lineno, this, buf, buf3);
1195 if (!value) return -1;
1199 * Add this CONF_PAIR to our CONF_SECTION
1201 cpn = cf_pair_alloc(buf1, value, t2, this);
1202 cpn->item.lineno = *lineno;
1203 cf_item_add(this, cf_pairtoitem(cpn));
1207 * See if EOF was unexpected ..
1209 if (feof(fp) && (this != current)) {
1210 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1212 cf_section_name1(this), cf_section_lineno(this));
1220 * Include one config file in another.
1222 int cf_file_include(const char *file, CONF_SECTION *cs)
1226 struct stat statbuf;
1229 DEBUG2( "Config: including file: %s", file);
1231 if (stat(file, &statbuf) == 0) {
1232 if ((statbuf.st_mode & S_IWOTH) != 0) {
1233 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1238 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1239 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1245 fp = fopen(file, "r");
1247 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1248 file, strerror(errno));
1253 * Read the section. It's OK to have EOF without a
1254 * matching close brace.
1256 if (cf_section_read(file, &lineno, fp, cs) < 0) {
1262 * Add the filename to the section
1264 mtime = rad_malloc(sizeof(*mtime));
1265 *mtime = statbuf.st_mtime;
1267 cf_data_add_internal(cs, file, mtime, free,
1275 * Bootstrap a config file.
1277 CONF_SECTION *cf_file_read(const char *file)
1281 cs = cf_section_alloc("main", NULL, NULL);
1282 if (!cs) return NULL;
1284 if (cf_file_include(file, cs) < 0) {
1285 cf_section_free(&cs);
1293 * Return a CONF_PAIR within a CONF_SECTION.
1295 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1299 if (!cs) cs = mainconfig.config;
1302 * Find the name in the tree, for speed.
1308 return rbtree_finddata(cs->pair_tree, &mycp);
1312 * Else find the first one
1314 for (ci = cs->children; ci; ci = ci->next) {
1315 if (ci->type == CONF_ITEM_PAIR)
1316 return cf_itemtopair(ci);
1323 * Return the attr of a CONF_PAIR
1326 char *cf_pair_attr(CONF_PAIR *pair)
1328 return (pair ? pair->attr : NULL);
1332 * Return the value of a CONF_PAIR
1335 char *cf_pair_value(CONF_PAIR *pair)
1337 return (pair ? pair->value : NULL);
1341 * Return the first label of a CONF_SECTION
1344 const char *cf_section_name1(const CONF_SECTION *cs)
1346 return (cs ? cs->name1 : NULL);
1350 * Return the second label of a CONF_SECTION
1353 const char *cf_section_name2(const CONF_SECTION *cs)
1355 return (cs ? cs->name2 : NULL);
1359 * Find a value in a CONF_SECTION
1361 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1365 cp = cf_pair_find(cs, attr);
1367 return (cp ? cp->value : NULL);
1371 * Return the next pair after a CONF_PAIR
1372 * with a certain name (char *attr) If the requested
1373 * attr is NULL, any attr matches.
1376 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1377 const CONF_PAIR *pair, const char *attr)
1382 * If pair is NULL this must be a first time run
1383 * Find the pair with correct name
1387 return cf_pair_find(cs, attr);
1390 ci = cf_pairtoitem(pair)->next;
1392 for (; ci; ci = ci->next) {
1393 if (ci->type != CONF_ITEM_PAIR)
1395 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1399 return cf_itemtopair(ci);
1403 * Find a CONF_SECTION, or return the root if name is NULL
1406 CONF_SECTION *cf_section_find(const char *name)
1409 return cf_section_sub_find(mainconfig.config, name);
1411 return mainconfig.config;
1415 * Find a sub-section in a section
1418 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1423 * Do the fast lookup if possible.
1425 if (name && cs->section_tree) {
1430 return rbtree_finddata(cs->section_tree, &mycs);
1433 for (ci = cs->children; ci; ci = ci->next) {
1434 if (ci->type != CONF_ITEM_SECTION)
1436 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1440 return cf_itemtosection(ci);
1446 * Find a CONF_SECTION with both names.
1448 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1449 const char *name1, const char *name2)
1453 if (!name2) return cf_section_sub_find(cs, name1);
1455 if (!cs) cs = mainconfig.config;
1457 if (name1 && (cs->section_tree)) {
1458 CONF_SECTION mycs, *master_cs;
1463 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1465 return rbtree_finddata(master_cs->name2_tree, &mycs);
1470 * Else do it the old-fashioned way.
1472 for (ci = cs->children; ci; ci = ci->next) {
1473 CONF_SECTION *subcs;
1475 if (ci->type != CONF_ITEM_SECTION)
1478 subcs = cf_itemtosection(ci);
1480 if (!subcs->name2) {
1481 if (strcmp(subcs->name1, name2) == 0) break;
1483 if (strcmp(subcs->name2, name2) == 0) break;
1485 continue; /* don't do the string comparisons below */
1488 if ((strcmp(subcs->name1, name1) == 0) &&
1489 (subcs->name2 != NULL) &&
1490 (strcmp(subcs->name2, name2) == 0))
1494 return cf_itemtosection(ci);
1498 * Return the next subsection after a CONF_SECTION
1499 * with a certain name1 (char *name1). If the requested
1500 * name1 is NULL, any name1 matches.
1503 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1504 CONF_SECTION *subsection,
1510 * If subsection is NULL this must be a first time run
1511 * Find the subsection with correct name
1514 if (subsection == NULL){
1515 ci = section->children;
1517 ci = cf_sectiontoitem(subsection)->next;
1520 for (; ci; ci = ci->next) {
1521 if (ci->type != CONF_ITEM_SECTION)
1523 if ((name1 == NULL) ||
1524 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1528 return cf_itemtosection(ci);
1532 * Return the next item after a CONF_ITEM.
1535 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1538 * If item is NULL this must be a first time run
1539 * Return the first item
1543 return section->children;
1549 int cf_section_lineno(CONF_SECTION *section)
1551 return cf_sectiontoitem(section)->lineno;
1554 int cf_pair_lineno(CONF_PAIR *pair)
1556 return cf_pairtoitem(pair)->lineno;
1559 int cf_item_is_section(CONF_ITEM *item)
1561 return item->type == CONF_ITEM_SECTION;
1563 int cf_item_is_pair(CONF_ITEM *item)
1565 return item->type == CONF_ITEM_PAIR;
1569 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1570 void *data, void (*data_free)(void *))
1574 cd = rad_malloc(sizeof(*cd));
1575 memset(cd, 0, sizeof(*cd));
1577 cd->item.type = CONF_ITEM_DATA;
1578 cd->item.parent = parent;
1579 cd->name = strdup(name);
1581 cd->free = data_free;
1587 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1590 if (!cs || !name) return NULL;
1593 * Find the name in the tree, for speed.
1595 if (cs->data_tree) {
1596 CONF_DATA mycd, *cd;
1600 cd = rbtree_finddata(cs->data_tree, &mycd);
1601 if (cd) return cd->data;
1608 * Find data from a particular section.
1610 void *cf_data_find(CONF_SECTION *cs, const char *name)
1612 return cf_data_find_internal(cs, name, 0);
1617 * Add named data to a configuration section.
1619 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1620 void *data, void (*data_free)(void *),
1625 if (!cs || !name) return -1;
1628 * Already exists. Can't add it.
1630 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1632 cd = cf_data_alloc(cs, name, data, data_free);
1636 cf_item_add(cs, cf_datatoitem(cd));
1642 * Add named data to a configuration section.
1644 int cf_data_add(CONF_SECTION *cs, const char *name,
1645 void *data, void (*data_free)(void *))
1647 return cf_data_add_internal(cs, name, data, data_free, 0);
1652 * Copy CONF_DATA from src to dst
1654 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1657 CONF_ITEM *cd, *next, **last;
1660 * Don't check if s->data_tree is NULL. It's child
1661 * sections may have data, even if this section doesn't.
1664 rad_assert(d->data_tree == NULL);
1665 d->data_tree = s->data_tree;
1666 s->data_tree = NULL;
1669 * Walk through src, moving CONF_ITEM_DATA
1672 last = &(s->children);
1673 for (cd = s->children; cd != NULL; cd = next) {
1677 * Recursively copy data from child sections.
1679 if (cd->type == CONF_ITEM_SECTION) {
1680 CONF_SECTION *s1, *d1;
1682 s1 = cf_itemtosection(cd);
1683 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1685 cf_section_copy_data(s1, d1);
1692 * Not conf data, remember last ptr.
1694 if (cd->type != CONF_ITEM_DATA) {
1700 * Remove it from the src list
1706 * Add it to the dst list
1709 rad_assert(d->tail == NULL);
1712 rad_assert(d->tail != NULL);
1720 * For a CONF_DATA element, stat the filename, if necessary.
1722 static int filename_stat(void *context, void *data)
1725 CONF_DATA *cd = data;
1727 context = context; /* -Wunused */
1729 if (cd->flag != PW_TYPE_FILENAME) return 0;
1731 if (stat(cd->name, &buf) < 0) return -1;
1733 if (buf.st_mtime != *(time_t *) cd->data) return -1;
1740 * Compare two CONF_SECTIONS. The items MUST be in the same
1743 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1745 CONF_ITEM *ca = a->children;
1746 CONF_ITEM *cb = b->children;
1754 if (!ca && !cb) break;
1759 if (ca && ca->type == CONF_ITEM_DATA) {
1763 if (cb && cb->type == CONF_ITEM_DATA) {
1769 * One is smaller than the other. Exit.
1771 if (!ca || !cb) return 0;
1773 if (ca->type != cb->type) return 0;
1776 * Deal with subsections.
1778 if (ca->type == CONF_ITEM_SECTION) {
1779 CONF_SECTION *sa = cf_itemtosection(ca);
1780 CONF_SECTION *sb = cf_itemtosection(cb);
1782 if (!cf_section_cmp(sa, sb)) return 0;
1786 rad_assert(ca->type == CONF_ITEM_PAIR);
1788 pa = cf_itemtopair(ca);
1789 pb = cf_itemtopair(cb);
1792 * Different attr and/or value, Exit.
1794 if ((strcmp(pa->attr, pb->attr) != 0) ||
1795 (strcmp(pa->value, pb->value) != 0)) return 0;
1799 * And go to the next element.
1807 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
1810 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
1815 * They must be the same, say so.
1824 * Migrate CONF_DATA from one section to another.
1826 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1829 CONF_SECTION *s, *d;
1831 for (ci = src->children; ci != NULL; ci = ci->next) {
1832 if (ci->type != CONF_ITEM_SECTION)
1835 s = cf_itemtosection(ci);
1836 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1838 if (!d) continue; /* not in new one, don't migrate it */
1841 * A section of the same name is in BOTH src & dst,
1842 * compare the CONF_PAIR's. If they're all the same,
1843 * then copy the CONF_DATA from one to the other.
1845 if (cf_section_cmp(s, d)) {
1846 cf_section_copy_data(s, d);
1850 return 1; /* rcode means anything? */
1856 * JMG dump_config tries to dump the config structure in a readable format
1860 static int dump_config_section(CONF_SECTION *cs, int indent)
1866 /* The DEBUG macro doesn't let me
1867 * for(i=0;i<indent;++i) debugputchar('\t');
1868 * so I had to get creative. --Pac. */
1870 for (ci = cs->children; ci; ci = ci->next) {
1872 case CONF_ITEM_PAIR:
1873 cp=cf_itemtopair(ci);
1874 DEBUG("%.*s%s = %s",
1875 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1876 cp->attr, cp->value);
1879 case CONF_ITEM_SECTION:
1880 scs=cf_itemtosection(ci);
1881 DEBUG("%.*s%s %s%s{",
1882 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1884 scs->name2 ? scs->name2 : "",
1885 scs->name2 ? " " : "");
1886 dump_config_section(scs, indent+1);
1888 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1891 default: /* FIXME: Do more! */
1899 int dump_config(void)
1901 return dump_config_section(mainconfig.config, 0);