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/radiusd.h>
33 #include <freeradius-devel/rad_assert.h>
38 #ifdef HAVE_SYS_STAT_H
45 typedef enum conf_type {
46 CONF_ITEM_INVALID = 0,
53 struct conf_item *next;
54 struct conf_part *parent;
68 struct conf_item *children;
69 struct conf_item *tail; /* for speed */
70 rbtree_t *pair_tree; /* and a partridge.. */
71 rbtree_t *section_tree; /* no jokes here */
72 rbtree_t *name2_tree; /* for sections of the same name2 */
75 const CONF_PARSER *variables;
80 * Internal data that is associated with a configuration section,
81 * so that we don't have to track it separately.
87 void *data; /* user data */
88 void (*free)(void *); /* free user data function */
92 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
93 void *data, void (*data_free)(void *),
95 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
99 * Isolate the scary casts in these tiny provably-safe functions
101 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
105 rad_assert(ci->type == CONF_ITEM_PAIR);
106 return (CONF_PAIR *)ci;
108 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
112 rad_assert(ci->type == CONF_ITEM_SECTION);
113 return (CONF_SECTION *)ci;
115 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
119 return (CONF_ITEM *)cp;
121 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
125 return (CONF_ITEM *)cs;
128 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
132 rad_assert(ci->type == CONF_ITEM_DATA);
133 return (CONF_DATA *)ci;
135 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
139 return (CONF_ITEM *)cd;
143 * Create a new CONF_PAIR
145 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
146 LRAD_TOKEN operator, CONF_SECTION *parent)
150 cp = rad_malloc(sizeof(*cp));
151 memset(cp, 0, sizeof(*cp));
152 cp->item.type = CONF_ITEM_PAIR;
153 cp->item.parent = parent;
154 cp->attr = strdup(attr);
155 cp->value = strdup(value);
156 cp->operator = operator;
164 void cf_pair_free(CONF_PAIR **cp)
166 if (!cp || !*cp) return;
174 memset(*cp, 0, sizeof(*cp));
182 static void cf_data_free(CONF_DATA **cd)
184 if (!cd || !*cd) return;
190 ((*cd)->free)((*cd)->data);
193 memset(*cd, 0, sizeof(*cd));
200 * rbtree callback function
202 static int pair_cmp(const void *a, const void *b)
204 const CONF_PAIR *one = a;
205 const CONF_PAIR *two = b;
207 return strcmp(one->attr, two->attr);
212 * rbtree callback function
214 static int section_cmp(const void *a, const void *b)
216 const CONF_SECTION *one = a;
217 const CONF_SECTION *two = b;
219 return strcmp(one->name1, two->name1);
224 * rbtree callback function
226 static int name2_cmp(const void *a, const void *b)
228 const CONF_SECTION *one = a;
229 const CONF_SECTION *two = b;
231 rad_assert(strcmp(one->name1, two->name1) == 0);
233 if (!one->name2 && !two->name2) return 0;
234 if (!one->name2) return -1;
235 if (!two->name2) return +1;
237 return strcmp(one->name2, two->name2);
242 * rbtree callback function
244 static int data_cmp(const void *a, const void *b)
248 const CONF_DATA *one = a;
249 const CONF_DATA *two = b;
251 rcode = one->flag - two->flag;
252 if (rcode != 0) return rcode;
254 return strcmp(one->name, two->name);
259 * Free strings we've parsed into data structures.
261 static void cf_section_parse_free(void *base, const CONF_PARSER *variables)
266 * Don't automatically free the strings if we're being
267 * called from a module. This is also for clients.c,
268 * where client_free() expects to be able to free the
269 * client structure. If we moved everything to key off
270 * of the config files, we might solve some problems...
272 if (!variables) return;
275 * Free up dynamically allocated string pointers.
277 for (i = 0; variables[i].name != NULL; i++) {
280 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
281 (variables[i].type != PW_TYPE_FILENAME)) {
286 * No base struct offset, data must be the pointer.
287 * If data doesn't exist, ignore the entry, there
288 * must be something wrong.
291 if (!variables[i].data) {
295 p = (char **) variables[i].data;;
297 } else if (variables[i].data) {
298 p = (char **) variables[i].data;;
301 p = (char **) (((char *)base) + variables[i].offset);
311 * Free a CONF_SECTION
313 void cf_section_free(CONF_SECTION **cs)
315 CONF_ITEM *ci, *next;
317 if (!cs || !*cs) return;
319 if ((*cs)->variables) {
320 cf_section_parse_free((*cs)->base, (*cs)->variables);
323 for (ci = (*cs)->children; ci; ci = next) {
327 case CONF_ITEM_PAIR: {
328 CONF_PAIR *pair = cf_itemtopair(ci);
333 case CONF_ITEM_SECTION: {
335 CONF_SECTION *section = cf_itemtosection(ci);
336 cf_section_free(§ion);
340 case CONF_ITEM_DATA: {
341 CONF_DATA *data = cf_itemtodata(ci);
346 default: /* should really be an error. */
355 if ((*cs)->pair_tree)
356 rbtree_free((*cs)->pair_tree);
357 if ((*cs)->section_tree)
358 rbtree_free((*cs)->section_tree);
359 if ((*cs)->name2_tree)
360 rbtree_free((*cs)->name2_tree);
361 if ((*cs)->data_tree)
362 rbtree_free((*cs)->data_tree);
365 * And free the section
368 memset(*cs, 0, sizeof(*cs));
377 * Allocate a CONF_SECTION
379 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
380 CONF_SECTION *parent)
384 if (!name1) return NULL;
386 cs = rad_malloc(sizeof(*cs));
387 memset(cs, 0, sizeof(*cs));
388 cs->item.type = CONF_ITEM_SECTION;
389 cs->item.parent = parent;
390 cs->name1 = strdup(name1);
392 cf_section_free(&cs);
396 if (name2 && *name2) {
397 cs->name2 = strdup(name2);
399 cf_section_free(&cs);
403 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
404 if (!cs->pair_tree) {
405 cf_section_free(&cs);
410 * Don't create a data tree, it may not be needed.
414 * Don't create the section tree here, it may not
422 * Add an item to a configuration section.
424 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
427 rad_assert(cs->tail == NULL);
430 rad_assert(cs->tail != NULL);
435 * Update the trees (and tail) for each item added.
437 for (/* nothing */; ci != NULL; ci = ci->next) {
441 * For fast lookups, pair's and sections get
446 rbtree_insert(cs->pair_tree, ci);
449 case CONF_ITEM_SECTION: {
450 const CONF_SECTION *cs_new = cf_itemtosection(ci);
452 if (!cs->section_tree) {
453 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
454 /* ignore any errors */
457 if (cs->section_tree) {
458 rbtree_insert(cs->section_tree, cs_new); }
461 * Two names: find the named instance.
464 CONF_SECTION *old_cs;
468 * CONF_SECTION having
469 * the given name1, and
473 old_cs = rbtree_finddata(cs->section_tree, cs_new);
474 if (!old_cs) return; /* this is a bad error! */
476 if (!old_cs->name2_tree) {
477 old_cs->name2_tree = rbtree_create(name2_cmp,
480 if (old_cs->name2_tree) {
481 rbtree_insert(old_cs->name2_tree, cs_new);
485 } /* was a section */
488 if (!cs->data_tree) {
489 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
492 rbtree_insert(cs->data_tree, ci);
496 default: /* FIXME: assert & error! */
499 } /* switch over conf types */
504 * Expand the variables in an input string.
506 static const char *cf_expand_variables(const char *cf, int *lineno,
507 const CONF_SECTION *outercs,
508 char *output, const char *input)
511 const char *end, *ptr;
513 const CONF_SECTION *parentcs;
516 * Find the master parent conf section.
517 * We can't use mainconfig.config, because we're in the
518 * process of re-building it, and it isn't set up yet...
520 for (parentcs = outercs;
521 parentcs->item.parent != NULL;
522 parentcs = parentcs->item.parent) {
530 * Ignore anything other than "${"
532 if ((*ptr == '$') && (ptr[1] == '{')) {
535 const CONF_SECTION *cs;
538 * FIXME: Add support for ${foo:-bar},
543 * Look for trailing '}', and log a
544 * warning for anything that doesn't match,
545 * and exit with a fatal error.
547 end = strchr(ptr, '}');
550 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
561 * ${.foo} means "foo from the current section"
569 * ${..foo} means "foo from the section
570 * enclosing this section" (etc.)
572 while (*ptr == '.') {
574 cs = cs->item.parent;
581 * ${foo} is local, with
582 * main as lower priority
587 * ${foo.bar.baz} is always rooted
590 for (q = ptr; *q && q != end; q++) {
602 * Find the next section.
605 (*ptr != 0) && (*ptr != '.') &&
613 * The character is a '.', find a
614 * section (as the user has given
615 * us a subsection to find)
620 ptr++; /* skip the period */
623 * Find the sub-section.
625 next = cf_section_sub_find(cs, name);
627 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
632 } else { /* no period, must be a conf-part */
634 * Find in the current referenced
637 cp = cf_pair_find(cs, name);
640 * It it was NOT ${..foo}
642 * top-level config items.
644 if (!up) cp = cf_pair_find(parentcs, name);
647 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
648 cf_section_name1(cs),
653 } /* until cp is non-NULL */
656 * Substitute the value of the variable.
658 strcpy(p, cp->value);
662 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
668 * Look for trailing '}', and log a
669 * warning for anything that doesn't match,
670 * and exit with a fatal error.
672 end = strchr(ptr, '}');
675 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
680 memcpy(name, ptr, end - ptr);
681 name[end - ptr] = '\0';
684 * Get the environment variable.
685 * If none exists, then make it an empty string.
699 * Copy it over verbatim.
703 } /* loop over all of the input string. */
712 * Parses an item (not a CONF_ITEM) into the specified format,
713 * with a default value.
715 * Returns -1 on error, 0 for correctly parsed, and 1 if the
716 * default value was used. Note that the default value will be
717 * used ONLY if the CONF_PAIR is NULL.
719 int cf_item_parse(CONF_SECTION *cs, const char *name,
720 int type, void *data, const char *dflt)
725 lrad_ipaddr_t ipaddr;
729 cp = cf_pair_find(cs, name);
734 return 1; /* nothing to parse, return default value */
742 case PW_TYPE_BOOLEAN:
744 * Allow yes/no and on/off
746 if ((strcasecmp(value, "yes") == 0) ||
747 (strcasecmp(value, "on") == 0)) {
749 } else if ((strcasecmp(value, "no") == 0) ||
750 (strcasecmp(value, "off") == 0)) {
754 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
758 DEBUG2(" %s %s: %s = %s", cs->name1, cs->name2, name, value);
760 DEBUG2(" %s: %s = %s", cs->name1, name, value);
764 case PW_TYPE_INTEGER:
765 *(int *)data = strtol(value, 0, 0);
767 DEBUG2(" %s %s: %s = %d",
768 cs->name1, cs->name2, name,
771 DEBUG2(" %s: %s = %d",
777 case PW_TYPE_STRING_PTR:
784 * Expand variables which haven't already been
785 * expanded automagically when the configuration
791 int lineno = cs->item.lineno;
794 * FIXME: sizeof(buffer)?
796 value = cf_expand_variables("?",
799 if (!value) return -1;
803 DEBUG2(" %s %s: %s = \"%s\"",
804 cs->name1, cs->name2, name,
805 value ? value : "(null)");
807 DEBUG2(" %s: %s = \"%s\"",
809 value ? value : "(null)");
811 *q = value ? strdup(value) : NULL;
815 * This is the same as PW_TYPE_STRING_PTR,
816 * except that we also "stat" the file, and
819 case PW_TYPE_FILENAME:
826 * Expand variables which haven't already been
827 * expanded automagically when the configuration
833 int lineno = cs->item.lineno;
836 * FIXME: sizeof(buffer)?
838 value = cf_expand_variables("?",
841 if (!value) return -1;
845 DEBUG2(" %s %s: %s = \"%s\"",
846 cs->name1, cs->name2, name,
847 value ? value : "(null)");
849 DEBUG2(" %s: %s = \"%s\"",
851 value ? value : "(null)");
853 *q = value ? strdup(value) : NULL;
856 * And now we "stat" the file.
861 if (stat(*q, &buf) == 0) {
864 mtime = rad_malloc(sizeof(*mtime));
865 *mtime = buf.st_mtime;
867 cf_data_add_internal(cs, *q, mtime, free,
875 * Allow '*' as any address
877 if (strcmp(value, "*") == 0) {
878 *(uint32_t *) data = htonl(INADDR_ANY);
879 DEBUG2(" %s: %s = *", cs->name1, name);
882 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
883 radlog(L_ERR, "Can't find IP address for host %s", value);
887 DEBUG2(" %s %s: %s = %s IP address [%s]",
888 cs->name1, cs->name2, name, value,
889 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
891 DEBUG2(" %s: %s = %s IP address [%s]",
892 cs->name1, name, value,
893 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
895 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
898 case PW_TYPE_IPV6ADDR:
899 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
900 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
904 DEBUG2(" %s %s: %s = %s IPv6 address [%s]",
905 cs->name1, cs->name2, name, value,
906 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
908 DEBUG2(" %s: %s = %s IPv6 address [%s]",
909 cs->name1, name, value,
910 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
912 memcpy(data, &ipaddr.ipaddr.ip6addr,
913 sizeof(ipaddr.ipaddr.ip6addr));
917 radlog(L_ERR, "type %d not supported yet", type);
920 } /* switch over variable type */
926 * Parse a configuration section into user-supplied variables.
928 int cf_section_parse(CONF_SECTION *cs, void *base,
929 const CONF_PARSER *variables)
935 * Handle the known configuration parameters.
937 for (i = 0; variables[i].name != NULL; i++) {
939 * Handle subsections specially
941 if (variables[i].type == PW_TYPE_SUBSECTION) {
942 const CONF_SECTION *subcs;
943 subcs = cf_section_sub_find(cs, variables[i].name);
946 * If the configuration section is NOT there,
949 * FIXME! This is probably wrong... we should
950 * probably set the items to their default values.
952 if (!subcs) continue;
954 if (!variables[i].dflt) {
955 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
956 cf_section_parse_free(base, variables);
960 if (cf_section_parse(subcs, base,
961 (const CONF_PARSER *) variables[i].dflt) < 0) {
962 cf_section_parse_free(base, variables);
966 } /* else it's a CONF_PAIR */
968 if (variables[i].data) {
969 data = variables[i].data; /* prefer this. */
971 data = ((char *)base) + variables[i].offset;
973 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
974 cf_section_parse_free(base, variables);
979 * Parse the pair we found, or a default value.
981 if (cf_item_parse(cs, variables[i].name, variables[i].type,
982 data, variables[i].dflt) < 0) {
983 cf_section_parse_free(base, variables);
986 } /* for all variables in the configuration section */
989 cs->variables = variables;
996 * Read a part of the config file.
998 static int cf_section_read(const char *file, int *lineno, FILE *fp,
999 CONF_SECTION *current)
1002 CONF_SECTION *this, *css;
1014 this = current; /* add items here */
1017 * Read, checking for line continuations ('\\' at EOL)
1023 * Get data, and remember if we are at EOF.
1025 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1031 * We've filled the buffer, and there isn't
1034 if ((len == (sizeof(buf) - 1)) &&
1035 (cbuf[len - 1] != '\n')) {
1036 radlog(L_ERR, "%s[%d]: Line too long",
1042 * Check for continuations.
1044 if (cbuf[len - 1] == '\n') len--;
1047 * Last character is '\\'. Over-write it,
1048 * and read another line.
1050 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1051 cbuf[len - 1] = '\0';
1057 * We're at EOF, and haven't read anything. Stop.
1059 if (eof && (cbuf == buf)) {
1064 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1066 if ((*buf1 == '#') || (*buf1 == '\0')) {
1071 * The caller eats "name1 name2 {", and calls us
1072 * for the data inside of the section. So if we
1073 * receive a closing brace, then it must mean the
1074 * end of the section.
1076 if (t1 == T_RCBRACE) {
1077 if (this == current) {
1078 radlog(L_ERR, "%s[%d]: Too many closing braces",
1083 this = this->item.parent;
1088 * Allow for $INCLUDE files
1090 * This *SHOULD* work for any level include.
1091 * I really really really hate this file. -cparker
1093 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1094 t2 = getword(&ptr, buf2, sizeof(buf2));
1096 value = cf_expand_variables(file, lineno, this, buf, buf2);
1097 if (!value) return -1;
1099 #ifdef HAVE_DIRENT_H
1103 * Include ALL non-"dot" files in the directory.
1106 if (value[strlen(value) - 1] == '/') {
1109 struct stat stat_buf;
1111 DEBUG2( "Config: including files in directory: %s", value );
1112 dir = opendir(value);
1114 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1115 file, *lineno, value,
1121 * Read the directory, ignoring "." files.
1123 while ((dp = readdir(dir)) != NULL) {
1126 if (dp->d_name[0] == '.') continue;
1129 * Check for valid characters
1131 for (p = dp->d_name; *p != '\0'; p++) {
1132 if (isalpha((int)*p) ||
1135 (*p == '.')) continue;
1138 if (*p != '\0') continue;
1140 snprintf(buf2, sizeof(buf2), "%s%s",
1142 if ((stat(buf2, &stat_buf) != 0) ||
1143 S_ISDIR(stat_buf.st_mode)) continue;
1145 * Read the file into the current
1146 * configuration sectoin.
1148 if (cf_file_include(buf2, this) < 0) {
1156 { /* it was a normal file */
1157 if (cf_file_include(value, this) < 0) {
1162 } /* we were in an include */
1165 * No '=': must be a section or sub-section.
1167 if (strchr(ptr, '=') == NULL) {
1168 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1169 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1171 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1172 t3 = getword(&ptr, buf3, sizeof(buf3));
1176 * Perhaps a subsection.
1178 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1179 css = cf_section_alloc(buf1,
1180 t2 == T_LCBRACE ? NULL : buf2,
1183 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1186 cf_item_add(this, cf_sectiontoitem(css));
1187 css->item.lineno = *lineno;
1190 * The current section is now the child section.
1197 * Ignore semi-colons.
1203 * Must be a normal attr = value line.
1205 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1207 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1208 (t2 < T_EQSTART || t2 > T_EQEND)) {
1209 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1215 * Ensure that the user can't add CONF_PAIRs
1216 * with 'internal' names;
1218 if (buf1[0] == '_') {
1219 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1220 file, *lineno, buf1);
1225 * Handle variable substitution via ${foo}
1227 value = cf_expand_variables(file, lineno, this, buf, buf3);
1228 if (!value) return -1;
1232 * Add this CONF_PAIR to our CONF_SECTION
1234 cpn = cf_pair_alloc(buf1, value, t2, this);
1235 cpn->item.lineno = *lineno;
1236 cf_item_add(this, cf_pairtoitem(cpn));
1240 * See if EOF was unexpected ..
1242 if (feof(fp) && (this != current)) {
1243 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1245 cf_section_name1(this), cf_section_lineno(this));
1253 * Include one config file in another.
1255 int cf_file_include(const char *file, CONF_SECTION *cs)
1259 struct stat statbuf;
1262 DEBUG2( "Config: including file: %s", file);
1264 if (stat(file, &statbuf) == 0) {
1265 if ((statbuf.st_mode & S_IWOTH) != 0) {
1266 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1271 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1272 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1278 fp = fopen(file, "r");
1280 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1281 file, strerror(errno));
1286 * Read the section. It's OK to have EOF without a
1287 * matching close brace.
1289 if (cf_section_read(file, &lineno, fp, cs) < 0) {
1295 * Add the filename to the section
1297 mtime = rad_malloc(sizeof(*mtime));
1298 *mtime = statbuf.st_mtime;
1300 cf_data_add_internal(cs, file, mtime, free,
1308 * Bootstrap a config file.
1310 CONF_SECTION *cf_file_read(const char *file)
1314 cs = cf_section_alloc("main", NULL, NULL);
1315 if (!cs) return NULL;
1317 if (cf_file_include(file, cs) < 0) {
1318 cf_section_free(&cs);
1326 * Return a CONF_PAIR within a CONF_SECTION.
1328 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1332 if (!cs) cs = mainconfig.config;
1335 * Find the name in the tree, for speed.
1341 return rbtree_finddata(cs->pair_tree, &mycp);
1345 * Else find the first one
1347 for (ci = cs->children; ci; ci = ci->next) {
1348 if (ci->type == CONF_ITEM_PAIR)
1349 return cf_itemtopair(ci);
1356 * Return the attr of a CONF_PAIR
1359 char *cf_pair_attr(CONF_PAIR *pair)
1361 return (pair ? pair->attr : NULL);
1365 * Return the value of a CONF_PAIR
1368 char *cf_pair_value(CONF_PAIR *pair)
1370 return (pair ? pair->value : NULL);
1374 * Return the first label of a CONF_SECTION
1377 const char *cf_section_name1(const CONF_SECTION *cs)
1379 return (cs ? cs->name1 : NULL);
1383 * Return the second label of a CONF_SECTION
1386 const char *cf_section_name2(const CONF_SECTION *cs)
1388 return (cs ? cs->name2 : NULL);
1392 * Find a value in a CONF_SECTION
1394 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1398 cp = cf_pair_find(cs, attr);
1400 return (cp ? cp->value : NULL);
1404 * Return the next pair after a CONF_PAIR
1405 * with a certain name (char *attr) If the requested
1406 * attr is NULL, any attr matches.
1409 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1410 const CONF_PAIR *pair, const char *attr)
1415 * If pair is NULL this must be a first time run
1416 * Find the pair with correct name
1420 return cf_pair_find(cs, attr);
1423 ci = cf_pairtoitem(pair)->next;
1425 for (; ci; ci = ci->next) {
1426 if (ci->type != CONF_ITEM_PAIR)
1428 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1432 return cf_itemtopair(ci);
1436 * Find a CONF_SECTION, or return the root if name is NULL
1439 CONF_SECTION *cf_section_find(const char *name)
1442 return cf_section_sub_find(mainconfig.config, name);
1444 return mainconfig.config;
1448 * Find a sub-section in a section
1451 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1456 * Do the fast lookup if possible.
1458 if (name && cs->section_tree) {
1463 return rbtree_finddata(cs->section_tree, &mycs);
1466 for (ci = cs->children; ci; ci = ci->next) {
1467 if (ci->type != CONF_ITEM_SECTION)
1469 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1473 return cf_itemtosection(ci);
1479 * Find a CONF_SECTION with both names.
1481 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1482 const char *name1, const char *name2)
1486 if (!name2) return cf_section_sub_find(cs, name1);
1488 if (!cs) cs = mainconfig.config;
1490 if (name1 && (cs->section_tree)) {
1491 CONF_SECTION mycs, *master_cs;
1496 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1498 return rbtree_finddata(master_cs->name2_tree, &mycs);
1503 * Else do it the old-fashioned way.
1505 for (ci = cs->children; ci; ci = ci->next) {
1506 CONF_SECTION *subcs;
1508 if (ci->type != CONF_ITEM_SECTION)
1511 subcs = cf_itemtosection(ci);
1513 if (!subcs->name2) {
1514 if (strcmp(subcs->name1, name2) == 0) break;
1516 if (strcmp(subcs->name2, name2) == 0) break;
1518 continue; /* don't do the string comparisons below */
1521 if ((strcmp(subcs->name1, name1) == 0) &&
1522 (subcs->name2 != NULL) &&
1523 (strcmp(subcs->name2, name2) == 0))
1527 return cf_itemtosection(ci);
1531 * Return the next subsection after a CONF_SECTION
1532 * with a certain name1 (char *name1). If the requested
1533 * name1 is NULL, any name1 matches.
1536 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1537 CONF_SECTION *subsection,
1543 * If subsection is NULL this must be a first time run
1544 * Find the subsection with correct name
1547 if (subsection == NULL){
1548 ci = section->children;
1550 ci = cf_sectiontoitem(subsection)->next;
1553 for (; ci; ci = ci->next) {
1554 if (ci->type != CONF_ITEM_SECTION)
1556 if ((name1 == NULL) ||
1557 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1561 return cf_itemtosection(ci);
1565 * Return the next item after a CONF_ITEM.
1568 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1571 * If item is NULL this must be a first time run
1572 * Return the first item
1576 return section->children;
1582 int cf_section_lineno(CONF_SECTION *section)
1584 return cf_sectiontoitem(section)->lineno;
1587 int cf_pair_lineno(CONF_PAIR *pair)
1589 return cf_pairtoitem(pair)->lineno;
1592 int cf_item_is_section(CONF_ITEM *item)
1594 return item->type == CONF_ITEM_SECTION;
1596 int cf_item_is_pair(CONF_ITEM *item)
1598 return item->type == CONF_ITEM_PAIR;
1602 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1603 void *data, void (*data_free)(void *))
1607 cd = rad_malloc(sizeof(*cd));
1608 memset(cd, 0, sizeof(*cd));
1610 cd->item.type = CONF_ITEM_DATA;
1611 cd->item.parent = parent;
1612 cd->name = strdup(name);
1614 cd->free = data_free;
1620 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1623 if (!cs || !name) return NULL;
1626 * Find the name in the tree, for speed.
1628 if (cs->data_tree) {
1629 CONF_DATA mycd, *cd;
1633 cd = rbtree_finddata(cs->data_tree, &mycd);
1634 if (cd) return cd->data;
1641 * Find data from a particular section.
1643 void *cf_data_find(CONF_SECTION *cs, const char *name)
1645 return cf_data_find_internal(cs, name, 0);
1650 * Add named data to a configuration section.
1652 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1653 void *data, void (*data_free)(void *),
1658 if (!cs || !name) return -1;
1661 * Already exists. Can't add it.
1663 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1665 cd = cf_data_alloc(cs, name, data, data_free);
1669 cf_item_add(cs, cf_datatoitem(cd));
1675 * Add named data to a configuration section.
1677 int cf_data_add(CONF_SECTION *cs, const char *name,
1678 void *data, void (*data_free)(void *))
1680 return cf_data_add_internal(cs, name, data, data_free, 0);
1685 * Copy CONF_DATA from src to dst
1687 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1690 CONF_ITEM *cd, *next, **last;
1693 * Don't check if s->data_tree is NULL. It's child
1694 * sections may have data, even if this section doesn't.
1697 rad_assert(d->data_tree == NULL);
1698 d->data_tree = s->data_tree;
1699 s->data_tree = NULL;
1702 * Walk through src, moving CONF_ITEM_DATA
1705 last = &(s->children);
1706 for (cd = s->children; cd != NULL; cd = next) {
1710 * Recursively copy data from child sections.
1712 if (cd->type == CONF_ITEM_SECTION) {
1713 CONF_SECTION *s1, *d1;
1715 s1 = cf_itemtosection(cd);
1716 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1718 cf_section_copy_data(s1, d1);
1725 * Not conf data, remember last ptr.
1727 if (cd->type != CONF_ITEM_DATA) {
1733 * Remove it from the src list
1739 * Add it to the dst list
1742 rad_assert(d->tail == NULL);
1745 rad_assert(d->tail != NULL);
1753 * For a CONF_DATA element, stat the filename, if necessary.
1755 static int filename_stat(void *context, void *data)
1758 CONF_DATA *cd = data;
1760 context = context; /* -Wunused */
1762 if (cd->flag != PW_TYPE_FILENAME) return 0;
1764 if (stat(cd->name, &buf) < 0) return -1;
1766 if (buf.st_mtime != *(time_t *) cd->data) return -1;
1773 * Compare two CONF_SECTIONS. The items MUST be in the same
1776 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1778 CONF_ITEM *ca = a->children;
1779 CONF_ITEM *cb = b->children;
1787 if (!ca && !cb) break;
1792 if (ca && ca->type == CONF_ITEM_DATA) {
1796 if (cb && cb->type == CONF_ITEM_DATA) {
1802 * One is smaller than the other. Exit.
1804 if (!ca || !cb) return 0;
1806 if (ca->type != cb->type) return 0;
1809 * Deal with subsections.
1811 if (ca->type == CONF_ITEM_SECTION) {
1812 CONF_SECTION *sa = cf_itemtosection(ca);
1813 CONF_SECTION *sb = cf_itemtosection(cb);
1815 if (!cf_section_cmp(sa, sb)) return 0;
1819 rad_assert(ca->type == CONF_ITEM_PAIR);
1821 pa = cf_itemtopair(ca);
1822 pb = cf_itemtopair(cb);
1825 * Different attr and/or value, Exit.
1827 if ((strcmp(pa->attr, pb->attr) != 0) ||
1828 (strcmp(pa->value, pb->value) != 0)) return 0;
1832 * And go to the next element.
1840 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
1843 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
1848 * They must be the same, say so.
1857 * Migrate CONF_DATA from one section to another.
1859 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1862 CONF_SECTION *s, *d;
1864 for (ci = src->children; ci != NULL; ci = ci->next) {
1865 if (ci->type != CONF_ITEM_SECTION)
1868 s = cf_itemtosection(ci);
1869 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1871 if (!d) continue; /* not in new one, don't migrate it */
1874 * A section of the same name is in BOTH src & dst,
1875 * compare the CONF_PAIR's. If they're all the same,
1876 * then copy the CONF_DATA from one to the other.
1878 if (cf_section_cmp(s, d)) {
1879 cf_section_copy_data(s, d);
1883 return 1; /* rcode means anything? */
1889 * JMG dump_config tries to dump the config structure in a readable format
1893 static int dump_config_section(CONF_SECTION *cs, int indent)
1899 /* The DEBUG macro doesn't let me
1900 * for(i=0;i<indent;++i) debugputchar('\t');
1901 * so I had to get creative. --Pac. */
1903 for (ci = cs->children; ci; ci = ci->next) {
1905 case CONF_ITEM_PAIR:
1906 cp=cf_itemtopair(ci);
1907 DEBUG("%.*s%s = %s",
1908 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1909 cp->attr, cp->value);
1912 case CONF_ITEM_SECTION:
1913 scs=cf_itemtosection(ci);
1914 DEBUG("%.*s%s %s%s{",
1915 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1917 scs->name2 ? scs->name2 : "",
1918 scs->name2 ? " " : "");
1919 dump_config_section(scs, indent+1);
1921 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1924 default: /* FIXME: Do more! */
1932 int dump_config(void)
1934 return dump_config_section(mainconfig.config, 0);