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 p = (char **) (((char *)base) + variables[i].offset);
317 * Free a CONF_SECTION
319 void cf_section_free(CONF_SECTION **cs)
321 CONF_ITEM *ci, *next;
323 if (!cs || !*cs) return;
325 if ((*cs)->variables) {
326 cf_section_parse_free((*cs)->base, (*cs)->variables);
329 for (ci = (*cs)->children; ci; ci = next) {
333 case CONF_ITEM_PAIR: {
334 CONF_PAIR *pair = cf_itemtopair(ci);
339 case CONF_ITEM_SECTION: {
341 CONF_SECTION *section = cf_itemtosection(ci);
342 cf_section_free(§ion);
346 case CONF_ITEM_DATA: {
347 CONF_DATA *data = cf_itemtodata(ci);
352 default: /* should really be an error. */
361 if ((*cs)->pair_tree)
362 rbtree_free((*cs)->pair_tree);
363 if ((*cs)->section_tree)
364 rbtree_free((*cs)->section_tree);
365 if ((*cs)->name2_tree)
366 rbtree_free((*cs)->name2_tree);
367 if ((*cs)->data_tree)
368 rbtree_free((*cs)->data_tree);
371 * And free the section
374 memset(*cs, 0, sizeof(*cs));
383 * Allocate a CONF_SECTION
385 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
386 CONF_SECTION *parent)
390 if (!name1) return NULL;
392 cs = rad_malloc(sizeof(*cs));
393 memset(cs, 0, sizeof(*cs));
394 cs->item.type = CONF_ITEM_SECTION;
395 cs->item.parent = parent;
396 cs->name1 = strdup(name1);
398 cf_section_free(&cs);
402 if (name2 && *name2) {
403 cs->name2 = strdup(name2);
405 cf_section_free(&cs);
409 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
410 if (!cs->pair_tree) {
411 cf_section_free(&cs);
416 * Don't create a data tree, it may not be needed.
420 * Don't create the section tree here, it may not
428 * Add an item to a configuration section.
430 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
433 rad_assert(cs->tail == NULL);
436 rad_assert(cs->tail != NULL);
441 * Update the trees (and tail) for each item added.
443 for (/* nothing */; ci != NULL; ci = ci->next) {
447 * For fast lookups, pair's and sections get
452 rbtree_insert(cs->pair_tree, ci);
455 case CONF_ITEM_SECTION: {
456 const CONF_SECTION *cs_new = cf_itemtosection(ci);
458 if (!cs->section_tree) {
459 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
460 /* ignore any errors */
463 if (cs->section_tree) {
464 rbtree_insert(cs->section_tree, cs_new); }
467 * Two names: find the named instance.
470 CONF_SECTION *old_cs;
474 * CONF_SECTION having
475 * the given name1, and
479 old_cs = rbtree_finddata(cs->section_tree, cs_new);
480 if (!old_cs) return; /* this is a bad error! */
482 if (!old_cs->name2_tree) {
483 old_cs->name2_tree = rbtree_create(name2_cmp,
486 if (old_cs->name2_tree) {
487 rbtree_insert(old_cs->name2_tree, cs_new);
491 } /* was a section */
494 if (!cs->data_tree) {
495 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
498 rbtree_insert(cs->data_tree, ci);
502 default: /* FIXME: assert & error! */
505 } /* switch over conf types */
510 * Expand the variables in an input string.
512 static const char *cf_expand_variables(const char *cf, int *lineno,
513 const CONF_SECTION *outercs,
514 char *output, const char *input)
517 const char *end, *ptr;
519 const CONF_SECTION *parentcs;
522 * Find the master parent conf section.
523 * We can't use mainconfig.config, because we're in the
524 * process of re-building it, and it isn't set up yet...
526 for (parentcs = outercs;
527 parentcs->item.parent != NULL;
528 parentcs = parentcs->item.parent) {
536 * Ignore anything other than "${"
538 if ((*ptr == '$') && (ptr[1] == '{')) {
541 const CONF_SECTION *cs;
544 * FIXME: Add support for ${foo:-bar},
549 * Look for trailing '}', and log a
550 * warning for anything that doesn't match,
551 * and exit with a fatal error.
553 end = strchr(ptr, '}');
556 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
567 * ${.foo} means "foo from the current section"
575 * ${..foo} means "foo from the section
576 * enclosing this section" (etc.)
578 while (*ptr == '.') {
580 cs = cs->item.parent;
587 * ${foo} is local, with
588 * main as lower priority
593 * ${foo.bar.baz} is always rooted
596 for (q = ptr; *q && q != end; q++) {
608 * Find the next section.
611 (*ptr != 0) && (*ptr != '.') &&
619 * The character is a '.', find a
620 * section (as the user has given
621 * us a subsection to find)
626 ptr++; /* skip the period */
629 * Find the sub-section.
631 next = cf_section_sub_find(cs, name);
633 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
638 } else { /* no period, must be a conf-part */
640 * Find in the current referenced
643 cp = cf_pair_find(cs, name);
646 * It it was NOT ${..foo}
648 * top-level config items.
650 if (!up) cp = cf_pair_find(parentcs, name);
653 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
654 cf_section_name1(cs),
659 } /* until cp is non-NULL */
662 * Substitute the value of the variable.
664 strcpy(p, cp->value);
668 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
674 * Look for trailing '}', and log a
675 * warning for anything that doesn't match,
676 * and exit with a fatal error.
678 end = strchr(ptr, '}');
681 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
686 memcpy(name, ptr, end - ptr);
687 name[end - ptr] = '\0';
690 * Get the environment variable.
691 * If none exists, then make it an empty string.
705 * Copy it over verbatim.
709 } /* loop over all of the input string. */
718 * Parses an item (not a CONF_ITEM) into the specified format,
719 * with a default value.
721 * Returns -1 on error, 0 for correctly parsed, and 1 if the
722 * default value was used. Note that the default value will be
723 * used ONLY if the CONF_PAIR is NULL.
725 int cf_item_parse(CONF_SECTION *cs, const char *name,
726 int type, void *data, const char *dflt)
731 lrad_ipaddr_t ipaddr;
735 cp = cf_pair_find(cs, name);
740 return 1; /* nothing to parse, return default value */
748 case PW_TYPE_BOOLEAN:
750 * Allow yes/no and on/off
752 if ((strcasecmp(value, "yes") == 0) ||
753 (strcasecmp(value, "on") == 0)) {
755 } else if ((strcasecmp(value, "no") == 0) ||
756 (strcasecmp(value, "off") == 0)) {
760 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
763 DEBUG2(" %s: %s = %s", cs->name1, name, value);
766 case PW_TYPE_INTEGER:
767 *(int *)data = strtol(value, 0, 0);
768 DEBUG2(" %s: %s = %d",
773 case PW_TYPE_STRING_PTR:
780 * Expand variables which haven't already been
781 * expanded automagically when the configuration
787 int lineno = cs->item.lineno;
790 * FIXME: sizeof(buffer)?
792 value = cf_expand_variables("?",
795 if (!value) return -1;
798 DEBUG2(" %s: %s = \"%s\"",
800 value ? value : "(null)");
801 *q = value ? strdup(value) : NULL;
805 * This is the same as PW_TYPE_STRING_PTR,
806 * except that we also "stat" the file, and
809 case PW_TYPE_FILENAME:
816 * Expand variables which haven't already been
817 * expanded automagically when the configuration
823 int lineno = cs->item.lineno;
826 * FIXME: sizeof(buffer)?
828 value = cf_expand_variables("?",
831 if (!value) return -1;
834 DEBUG2(" %s: %s = \"%s\"",
836 value ? value : "(null)");
837 *q = value ? strdup(value) : NULL;
840 * And now we "stat" the file.
845 if (stat(*q, &buf) == 0) {
848 mtime = rad_malloc(sizeof(*mtime));
849 *mtime = buf.st_mtime;
851 cf_data_add_internal(cs, *q, mtime, free,
859 * Allow '*' as any address
861 if (strcmp(value, "*") == 0) {
862 *(uint32_t *) data = htonl(INADDR_ANY);
863 DEBUG2(" %s: %s = *", cs->name1, name);
866 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
867 radlog(L_ERR, "Can't find IP address for host %s", value);
870 DEBUG2(" %s: %s = %s IP address [%s]",
871 cs->name1, name, value,
872 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
873 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
876 case PW_TYPE_IPV6ADDR:
877 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
878 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
881 DEBUG2(" %s: %s = %s IPv6 address [%s]",
882 cs->name1, name, value,
883 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
884 memcpy(data, &ipaddr.ipaddr.ip6addr,
885 sizeof(ipaddr.ipaddr.ip6addr));
889 radlog(L_ERR, "type %d not supported yet", type);
892 } /* switch over variable type */
898 * Parse a configuration section into user-supplied variables.
900 int cf_section_parse(CONF_SECTION *cs, void *base,
901 const CONF_PARSER *variables)
907 * Handle the known configuration parameters.
909 for (i = 0; variables[i].name != NULL; i++) {
911 * Handle subsections specially
913 if (variables[i].type == PW_TYPE_SUBSECTION) {
914 const CONF_SECTION *subcs;
915 subcs = cf_section_sub_find(cs, variables[i].name);
918 * If the configuration section is NOT there,
921 * FIXME! This is probably wrong... we should
922 * probably set the items to their default values.
924 if (!subcs) continue;
926 if (!variables[i].dflt) {
927 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
928 cf_section_parse_free(base, variables);
932 if (cf_section_parse(subcs, base,
933 (const CONF_PARSER *) variables[i].dflt) < 0) {
934 cf_section_parse_free(base, variables);
938 } /* else it's a CONF_PAIR */
940 if (variables[i].data) {
941 data = variables[i].data; /* prefer this. */
943 data = ((char *)base) + variables[i].offset;
945 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
946 cf_section_parse_free(base, variables);
951 * Parse the pair we found, or a default value.
953 if (cf_item_parse(cs, variables[i].name, variables[i].type,
954 data, variables[i].dflt) < 0) {
955 cf_section_parse_free(base, variables);
958 } /* for all variables in the configuration section */
961 cs->variables = variables;
968 * Read a part of the config file.
970 static int cf_section_read(const char *file, int *lineno, FILE *fp,
971 CONF_SECTION *current)
974 CONF_SECTION *this, *css;
986 this = current; /* add items here */
989 * Read, checking for line continuations ('\\' at EOL)
995 * Get data, and remember if we are at EOF.
997 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1003 * We've filled the buffer, and there isn't
1006 if ((len == (sizeof(buf) - 1)) &&
1007 (cbuf[len - 1] != '\n')) {
1008 radlog(L_ERR, "%s[%d]: Line too long",
1014 * Check for continuations.
1016 if (cbuf[len - 1] == '\n') len--;
1019 * Last character is '\\'. Over-write it,
1020 * and read another line.
1022 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1023 cbuf[len - 1] = '\0';
1029 * We're at EOF, and haven't read anything. Stop.
1031 if (eof && (cbuf == buf)) {
1036 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1038 if ((*buf1 == '#') || (*buf1 == '\0')) {
1043 * The caller eats "name1 name2 {", and calls us
1044 * for the data inside of the section. So if we
1045 * receive a closing brace, then it must mean the
1046 * end of the section.
1048 if (t1 == T_RCBRACE) {
1049 if (this == current) {
1050 radlog(L_ERR, "%s[%d]: Too many closing braces",
1055 this = this->item.parent;
1060 * Allow for $INCLUDE files
1062 * This *SHOULD* work for any level include.
1063 * I really really really hate this file. -cparker
1065 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1066 t2 = getword(&ptr, buf2, sizeof(buf2));
1068 value = cf_expand_variables(file, lineno, this, buf, buf2);
1069 if (!value) return -1;
1071 #ifdef HAVE_DIRENT_H
1075 * Include ALL non-"dot" files in the directory.
1078 if (value[strlen(value) - 1] == '/') {
1081 struct stat stat_buf;
1083 DEBUG2( "Config: including files in directory: %s", value );
1084 dir = opendir(value);
1086 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1087 file, *lineno, value,
1093 * Read the directory, ignoring "." files.
1095 while ((dp = readdir(dir)) != NULL) {
1098 if (dp->d_name[0] == '.') continue;
1101 * Check for valid characters
1103 for (p = dp->d_name; *p != '\0'; p++) {
1104 if (isalpha((int)*p) ||
1107 (*p == '.')) continue;
1110 if (*p != '\0') continue;
1112 snprintf(buf2, sizeof(buf2), "%s%s",
1114 if ((stat(buf2, &stat_buf) != 0) ||
1115 S_ISDIR(stat_buf.st_mode)) continue;
1117 * Read the file into the current
1118 * configuration sectoin.
1120 if (cf_file_include(buf2, this) < 0) {
1128 { /* it was a normal file */
1129 if (cf_file_include(value, this) < 0) {
1134 } /* we were in an include */
1137 * No '=': must be a section or sub-section.
1139 if (strchr(ptr, '=') == NULL) {
1140 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1141 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1143 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1144 t3 = getword(&ptr, buf3, sizeof(buf3));
1148 * Perhaps a subsection.
1150 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1151 css = cf_section_alloc(buf1,
1152 t2 == T_LCBRACE ? NULL : buf2,
1155 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1158 cf_item_add(this, cf_sectiontoitem(css));
1159 css->item.lineno = *lineno;
1162 * The current section is now the child section.
1169 * Ignore semi-colons.
1175 * Must be a normal attr = value line.
1177 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1179 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1180 (t2 < T_EQSTART || t2 > T_EQEND)) {
1181 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1187 * Ensure that the user can't add CONF_PAIRs
1188 * with 'internal' names;
1190 if (buf1[0] == '_') {
1191 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1192 file, *lineno, buf1);
1197 * Handle variable substitution via ${foo}
1199 value = cf_expand_variables(file, lineno, this, buf, buf3);
1200 if (!value) return -1;
1204 * Add this CONF_PAIR to our CONF_SECTION
1206 cpn = cf_pair_alloc(buf1, value, t2, this);
1207 cpn->item.lineno = *lineno;
1208 cf_item_add(this, cf_pairtoitem(cpn));
1212 * See if EOF was unexpected ..
1214 if (feof(fp) && (this != current)) {
1215 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1217 cf_section_name1(this), cf_section_lineno(this));
1225 * Include one config file in another.
1227 int cf_file_include(const char *file, CONF_SECTION *cs)
1231 struct stat statbuf;
1234 DEBUG2( "Config: including file: %s", file);
1236 if (stat(file, &statbuf) == 0) {
1237 if ((statbuf.st_mode & S_IWOTH) != 0) {
1238 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1243 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1244 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1250 fp = fopen(file, "r");
1252 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1253 file, strerror(errno));
1258 * Read the section. It's OK to have EOF without a
1259 * matching close brace.
1261 if (cf_section_read(file, &lineno, fp, cs) < 0) {
1267 * Add the filename to the section
1269 mtime = rad_malloc(sizeof(*mtime));
1270 *mtime = statbuf.st_mtime;
1272 cf_data_add_internal(cs, file, mtime, free,
1280 * Bootstrap a config file.
1282 CONF_SECTION *cf_file_read(const char *file)
1286 cs = cf_section_alloc("main", NULL, NULL);
1287 if (!cs) return NULL;
1289 if (cf_file_include(file, cs) < 0) {
1290 cf_section_free(&cs);
1298 * Return a CONF_PAIR within a CONF_SECTION.
1300 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1304 if (!cs) cs = mainconfig.config;
1307 * Find the name in the tree, for speed.
1313 return rbtree_finddata(cs->pair_tree, &mycp);
1317 * Else find the first one
1319 for (ci = cs->children; ci; ci = ci->next) {
1320 if (ci->type == CONF_ITEM_PAIR)
1321 return cf_itemtopair(ci);
1328 * Return the attr of a CONF_PAIR
1331 char *cf_pair_attr(CONF_PAIR *pair)
1333 return (pair ? pair->attr : NULL);
1337 * Return the value of a CONF_PAIR
1340 char *cf_pair_value(CONF_PAIR *pair)
1342 return (pair ? pair->value : NULL);
1346 * Return the first label of a CONF_SECTION
1349 const char *cf_section_name1(const CONF_SECTION *cs)
1351 return (cs ? cs->name1 : NULL);
1355 * Return the second label of a CONF_SECTION
1358 const char *cf_section_name2(const CONF_SECTION *cs)
1360 return (cs ? cs->name2 : NULL);
1364 * Find a value in a CONF_SECTION
1366 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1370 cp = cf_pair_find(cs, attr);
1372 return (cp ? cp->value : NULL);
1376 * Return the next pair after a CONF_PAIR
1377 * with a certain name (char *attr) If the requested
1378 * attr is NULL, any attr matches.
1381 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1382 const CONF_PAIR *pair, const char *attr)
1387 * If pair is NULL this must be a first time run
1388 * Find the pair with correct name
1392 return cf_pair_find(cs, attr);
1395 ci = cf_pairtoitem(pair)->next;
1397 for (; ci; ci = ci->next) {
1398 if (ci->type != CONF_ITEM_PAIR)
1400 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1404 return cf_itemtopair(ci);
1408 * Find a CONF_SECTION, or return the root if name is NULL
1411 CONF_SECTION *cf_section_find(const char *name)
1414 return cf_section_sub_find(mainconfig.config, name);
1416 return mainconfig.config;
1420 * Find a sub-section in a section
1423 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1428 * Do the fast lookup if possible.
1430 if (name && cs->section_tree) {
1435 return rbtree_finddata(cs->section_tree, &mycs);
1438 for (ci = cs->children; ci; ci = ci->next) {
1439 if (ci->type != CONF_ITEM_SECTION)
1441 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1445 return cf_itemtosection(ci);
1451 * Find a CONF_SECTION with both names.
1453 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1454 const char *name1, const char *name2)
1458 if (!name2) return cf_section_sub_find(cs, name1);
1460 if (!cs) cs = mainconfig.config;
1462 if (name1 && (cs->section_tree)) {
1463 CONF_SECTION mycs, *master_cs;
1468 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1470 return rbtree_finddata(master_cs->name2_tree, &mycs);
1475 * Else do it the old-fashioned way.
1477 for (ci = cs->children; ci; ci = ci->next) {
1478 CONF_SECTION *subcs;
1480 if (ci->type != CONF_ITEM_SECTION)
1483 subcs = cf_itemtosection(ci);
1485 if (!subcs->name2) {
1486 if (strcmp(subcs->name1, name2) == 0) break;
1488 if (strcmp(subcs->name2, name2) == 0) break;
1490 continue; /* don't do the string comparisons below */
1493 if ((strcmp(subcs->name1, name1) == 0) &&
1494 (subcs->name2 != NULL) &&
1495 (strcmp(subcs->name2, name2) == 0))
1499 return cf_itemtosection(ci);
1503 * Return the next subsection after a CONF_SECTION
1504 * with a certain name1 (char *name1). If the requested
1505 * name1 is NULL, any name1 matches.
1508 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1509 CONF_SECTION *subsection,
1515 * If subsection is NULL this must be a first time run
1516 * Find the subsection with correct name
1519 if (subsection == NULL){
1520 ci = section->children;
1522 ci = cf_sectiontoitem(subsection)->next;
1525 for (; ci; ci = ci->next) {
1526 if (ci->type != CONF_ITEM_SECTION)
1528 if ((name1 == NULL) ||
1529 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1533 return cf_itemtosection(ci);
1537 * Return the next item after a CONF_ITEM.
1540 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1543 * If item is NULL this must be a first time run
1544 * Return the first item
1548 return section->children;
1554 int cf_section_lineno(CONF_SECTION *section)
1556 return cf_sectiontoitem(section)->lineno;
1559 int cf_pair_lineno(CONF_PAIR *pair)
1561 return cf_pairtoitem(pair)->lineno;
1564 int cf_item_is_section(CONF_ITEM *item)
1566 return item->type == CONF_ITEM_SECTION;
1568 int cf_item_is_pair(CONF_ITEM *item)
1570 return item->type == CONF_ITEM_PAIR;
1574 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1575 void *data, void (*data_free)(void *))
1579 cd = rad_malloc(sizeof(*cd));
1580 memset(cd, 0, sizeof(*cd));
1582 cd->item.type = CONF_ITEM_DATA;
1583 cd->item.parent = parent;
1584 cd->name = strdup(name);
1586 cd->free = data_free;
1592 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1595 if (!cs || !name) return NULL;
1598 * Find the name in the tree, for speed.
1600 if (cs->data_tree) {
1601 CONF_DATA mycd, *cd;
1605 cd = rbtree_finddata(cs->data_tree, &mycd);
1606 if (cd) return cd->data;
1613 * Find data from a particular section.
1615 void *cf_data_find(CONF_SECTION *cs, const char *name)
1617 return cf_data_find_internal(cs, name, 0);
1622 * Add named data to a configuration section.
1624 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1625 void *data, void (*data_free)(void *),
1630 if (!cs || !name) return -1;
1633 * Already exists. Can't add it.
1635 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1637 cd = cf_data_alloc(cs, name, data, data_free);
1641 cf_item_add(cs, cf_datatoitem(cd));
1647 * Add named data to a configuration section.
1649 int cf_data_add(CONF_SECTION *cs, const char *name,
1650 void *data, void (*data_free)(void *))
1652 return cf_data_add_internal(cs, name, data, data_free, 0);
1657 * Copy CONF_DATA from src to dst
1659 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1662 CONF_ITEM *cd, *next, **last;
1665 * Don't check if s->data_tree is NULL. It's child
1666 * sections may have data, even if this section doesn't.
1669 rad_assert(d->data_tree == NULL);
1670 d->data_tree = s->data_tree;
1671 s->data_tree = NULL;
1674 * Walk through src, moving CONF_ITEM_DATA
1677 last = &(s->children);
1678 for (cd = s->children; cd != NULL; cd = next) {
1682 * Recursively copy data from child sections.
1684 if (cd->type == CONF_ITEM_SECTION) {
1685 CONF_SECTION *s1, *d1;
1687 s1 = cf_itemtosection(cd);
1688 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1690 cf_section_copy_data(s1, d1);
1697 * Not conf data, remember last ptr.
1699 if (cd->type != CONF_ITEM_DATA) {
1705 * Remove it from the src list
1711 * Add it to the dst list
1714 rad_assert(d->tail == NULL);
1717 rad_assert(d->tail != NULL);
1725 * For a CONF_DATA element, stat the filename, if necessary.
1727 static int filename_stat(void *context, void *data)
1730 CONF_DATA *cd = data;
1732 context = context; /* -Wunused */
1734 if (cd->flag != PW_TYPE_FILENAME) return 0;
1736 if (stat(cd->name, &buf) < 0) return -1;
1738 if (buf.st_mtime != *(time_t *) cd->data) return -1;
1745 * Compare two CONF_SECTIONS. The items MUST be in the same
1748 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1750 CONF_ITEM *ca = a->children;
1751 CONF_ITEM *cb = b->children;
1759 if (!ca && !cb) break;
1764 if (ca && ca->type == CONF_ITEM_DATA) {
1768 if (cb && cb->type == CONF_ITEM_DATA) {
1774 * One is smaller than the other. Exit.
1776 if (!ca || !cb) return 0;
1778 if (ca->type != cb->type) return 0;
1781 * Deal with subsections.
1783 if (ca->type == CONF_ITEM_SECTION) {
1784 CONF_SECTION *sa = cf_itemtosection(ca);
1785 CONF_SECTION *sb = cf_itemtosection(cb);
1787 if (!cf_section_cmp(sa, sb)) return 0;
1791 rad_assert(ca->type == CONF_ITEM_PAIR);
1793 pa = cf_itemtopair(ca);
1794 pb = cf_itemtopair(cb);
1797 * Different attr and/or value, Exit.
1799 if ((strcmp(pa->attr, pb->attr) != 0) ||
1800 (strcmp(pa->value, pb->value) != 0)) return 0;
1804 * And go to the next element.
1812 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
1815 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
1820 * They must be the same, say so.
1829 * Migrate CONF_DATA from one section to another.
1831 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1834 CONF_SECTION *s, *d;
1836 for (ci = src->children; ci != NULL; ci = ci->next) {
1837 if (ci->type != CONF_ITEM_SECTION)
1840 s = cf_itemtosection(ci);
1841 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1843 if (!d) continue; /* not in new one, don't migrate it */
1846 * A section of the same name is in BOTH src & dst,
1847 * compare the CONF_PAIR's. If they're all the same,
1848 * then copy the CONF_DATA from one to the other.
1850 if (cf_section_cmp(s, d)) {
1851 cf_section_copy_data(s, d);
1855 return 1; /* rcode means anything? */
1861 * JMG dump_config tries to dump the config structure in a readable format
1865 static int dump_config_section(CONF_SECTION *cs, int indent)
1871 /* The DEBUG macro doesn't let me
1872 * for(i=0;i<indent;++i) debugputchar('\t');
1873 * so I had to get creative. --Pac. */
1875 for (ci = cs->children; ci; ci = ci->next) {
1877 case CONF_ITEM_PAIR:
1878 cp=cf_itemtopair(ci);
1879 DEBUG("%.*s%s = %s",
1880 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1881 cp->attr, cp->value);
1884 case CONF_ITEM_SECTION:
1885 scs=cf_itemtosection(ci);
1886 DEBUG("%.*s%s %s%s{",
1887 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1889 scs->name2 ? scs->name2 : "",
1890 scs->name2 ? " " : "");
1891 dump_config_section(scs, indent+1);
1893 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1896 default: /* FIXME: Do more! */
1904 int dump_config(void)
1906 return dump_config_section(mainconfig.config, 0);