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 CONF_SECTION *template;
71 rbtree_t *pair_tree; /* and a partridge.. */
72 rbtree_t *section_tree; /* no jokes here */
73 rbtree_t *name2_tree; /* for sections of the same name2 */
77 const CONF_PARSER *variables;
82 * Internal data that is associated with a configuration section,
83 * so that we don't have to track it separately.
89 void *data; /* user data */
90 void (*free)(void *); /* free user data function */
94 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
95 void *data, void (*data_free)(void *),
97 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
101 * Isolate the scary casts in these tiny provably-safe functions
103 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
107 rad_assert(ci->type == CONF_ITEM_PAIR);
108 return (CONF_PAIR *)ci;
110 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
114 rad_assert(ci->type == CONF_ITEM_SECTION);
115 return (CONF_SECTION *)ci;
117 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
121 return (CONF_ITEM *)cp;
123 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
127 return (CONF_ITEM *)cs;
130 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
134 rad_assert(ci->type == CONF_ITEM_DATA);
135 return (CONF_DATA *)ci;
137 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
141 return (CONF_ITEM *)cd;
145 * Create a new CONF_PAIR
147 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
148 LRAD_TOKEN operator, CONF_SECTION *parent)
152 cp = rad_malloc(sizeof(*cp));
153 memset(cp, 0, sizeof(*cp));
154 cp->item.type = CONF_ITEM_PAIR;
155 cp->item.parent = parent;
156 cp->attr = strdup(attr);
157 cp->value = strdup(value);
158 cp->operator = operator;
166 void cf_pair_free(CONF_PAIR **cp)
168 if (!cp || !*cp) return;
176 memset(*cp, 0, sizeof(*cp));
184 static void cf_data_free(CONF_DATA **cd)
186 if (!cd || !*cd) return;
188 if ((*cd)->flag != 0) free((*cd)->name);
192 ((*cd)->free)((*cd)->data);
195 memset(*cd, 0, sizeof(*cd));
202 * rbtree callback function
204 static int pair_cmp(const void *a, const void *b)
206 const CONF_PAIR *one = a;
207 const CONF_PAIR *two = b;
209 return strcmp(one->attr, two->attr);
214 * rbtree callback function
216 static int section_cmp(const void *a, const void *b)
218 const CONF_SECTION *one = a;
219 const CONF_SECTION *two = b;
221 return strcmp(one->name1, two->name1);
226 * rbtree callback function
228 static int name2_cmp(const void *a, const void *b)
230 const CONF_SECTION *one = a;
231 const CONF_SECTION *two = b;
233 rad_assert(strcmp(one->name1, two->name1) == 0);
235 if (!one->name2 && !two->name2) return 0;
236 if (!one->name2) return -1;
237 if (!two->name2) return +1;
239 return strcmp(one->name2, two->name2);
244 * rbtree callback function
246 static int data_cmp(const void *a, const void *b)
250 const CONF_DATA *one = a;
251 const CONF_DATA *two = b;
253 rcode = one->flag - two->flag;
254 if (rcode != 0) return rcode;
256 return strcmp(one->name, two->name);
261 * Free strings we've parsed into data structures.
263 static void cf_section_parse_free(void *base, const CONF_PARSER *variables)
268 * Don't automatically free the strings if we're being
269 * called from a module. This is also for clients.c,
270 * where client_free() expects to be able to free the
271 * client structure. If we moved everything to key off
272 * of the config files, we might solve some problems...
274 if (!variables) return;
277 * Free up dynamically allocated string pointers.
279 for (i = 0; variables[i].name != NULL; i++) {
282 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
283 (variables[i].type != PW_TYPE_FILENAME)) {
288 * No base struct offset, data must be the pointer.
289 * If data doesn't exist, ignore the entry, there
290 * must be something wrong.
293 if (!variables[i].data) {
297 p = (char **) variables[i].data;;
299 } else if (variables[i].data) {
300 p = (char **) variables[i].data;;
303 p = (char **) (((char *)base) + variables[i].offset);
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
420 if (parent) cs->depth = parent->depth + 1;
427 * Add an item to a configuration section.
429 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
432 rad_assert(cs->tail == NULL);
435 rad_assert(cs->tail != NULL);
440 * Update the trees (and tail) for each item added.
442 for (/* nothing */; ci != NULL; ci = ci->next) {
446 * For fast lookups, pair's and sections get
451 rbtree_insert(cs->pair_tree, ci);
454 case CONF_ITEM_SECTION: {
455 const CONF_SECTION *cs_new = cf_itemtosection(ci);
457 if (!cs->section_tree) {
458 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
459 /* ignore any errors */
462 if (cs->section_tree) {
463 rbtree_insert(cs->section_tree, cs_new); }
466 * Two names: find the named instance.
469 CONF_SECTION *old_cs;
473 * CONF_SECTION having
474 * the given name1, and
478 old_cs = rbtree_finddata(cs->section_tree, cs_new);
479 if (!old_cs) return; /* this is a bad error! */
481 if (!old_cs->name2_tree) {
482 old_cs->name2_tree = rbtree_create(name2_cmp,
485 if (old_cs->name2_tree) {
486 rbtree_insert(old_cs->name2_tree, cs_new);
490 } /* was a section */
493 if (!cs->data_tree) {
494 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
497 rbtree_insert(cs->data_tree, ci);
501 default: /* FIXME: assert & error! */
504 } /* switch over conf types */
509 * Expand the variables in an input string.
511 static const char *cf_expand_variables(const char *cf, int *lineno,
512 const CONF_SECTION *outercs,
513 char *output, const char *input)
516 const char *end, *ptr;
518 const CONF_SECTION *parentcs;
521 * Find the master parent conf section.
522 * We can't use mainconfig.config, because we're in the
523 * process of re-building it, and it isn't set up yet...
525 for (parentcs = outercs;
526 parentcs->item.parent != NULL;
527 parentcs = parentcs->item.parent) {
535 * Ignore anything other than "${"
537 if ((*ptr == '$') && (ptr[1] == '{')) {
540 const CONF_SECTION *cs;
543 * FIXME: Add support for ${foo:-bar},
548 * Look for trailing '}', and log a
549 * warning for anything that doesn't match,
550 * and exit with a fatal error.
552 end = strchr(ptr, '}');
555 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
566 * ${.foo} means "foo from the current section"
574 * ${..foo} means "foo from the section
575 * enclosing this section" (etc.)
577 while (*ptr == '.') {
579 cs = cs->item.parent;
586 * ${foo} is local, with
587 * main as lower priority
592 * ${foo.bar.baz} is always rooted
595 for (q = ptr; *q && q != end; q++) {
607 * Find the next section.
610 (*ptr != 0) && (*ptr != '.') &&
618 * The character is a '.', find a
619 * section (as the user has given
620 * us a subsection to find)
625 ptr++; /* skip the period */
628 * Find the sub-section.
630 next = cf_section_sub_find(cs, name);
632 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
637 } else { /* no period, must be a conf-part */
639 * Find in the current referenced
642 cp = cf_pair_find(cs, name);
645 * It it was NOT ${..foo}
647 * top-level config items.
649 if (!up) cp = cf_pair_find(parentcs, name);
652 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
653 cf_section_name1(cs),
658 } /* until cp is non-NULL */
661 * Substitute the value of the variable.
663 strcpy(p, cp->value);
667 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
673 * Look for trailing '}', and log a
674 * warning for anything that doesn't match,
675 * and exit with a fatal error.
677 end = strchr(ptr, '}');
680 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
685 memcpy(name, ptr, end - ptr);
686 name[end - ptr] = '\0';
689 * Get the environment variable.
690 * If none exists, then make it an empty string.
704 * Copy it over verbatim.
708 } /* loop over all of the input string. */
717 * Parses an item (not a CONF_ITEM) into the specified format,
718 * with a default value.
720 * Returns -1 on error, 0 for correctly parsed, and 1 if the
721 * default value was used. Note that the default value will be
722 * used ONLY if the CONF_PAIR is NULL.
724 int cf_item_parse(CONF_SECTION *cs, const char *name,
725 int type, void *data, const char *dflt)
730 lrad_ipaddr_t ipaddr;
734 cp = cf_pair_find(cs, name);
739 return 1; /* nothing to parse, return default value */
747 case PW_TYPE_BOOLEAN:
749 * Allow yes/no and on/off
751 if ((strcasecmp(value, "yes") == 0) ||
752 (strcasecmp(value, "on") == 0)) {
754 } else if ((strcasecmp(value, "no") == 0) ||
755 (strcasecmp(value, "off") == 0)) {
759 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
762 DEBUG2("\t%s = %s", name, value);
765 case PW_TYPE_INTEGER:
766 *(int *)data = strtol(value, 0, 0);
767 DEBUG2("\t%s = %d", name, *(int *)data);
770 case PW_TYPE_STRING_PTR:
777 * Expand variables which haven't already been
778 * expanded automagically when the configuration
784 int lineno = cs->item.lineno;
787 * FIXME: sizeof(buffer)?
789 value = cf_expand_variables("?",
792 if (!value) return -1;
795 DEBUG2("\t%s = \"%s\"", name, 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("\t%s = \"%s\"", name, value ? value : "(null)");
830 *q = value ? strdup(value) : NULL;
833 * And now we "stat" the file.
838 if (stat(*q, &buf) == 0) {
841 mtime = rad_malloc(sizeof(*mtime));
842 *mtime = buf.st_mtime;
844 cf_data_add_internal(cs, *q, mtime, free,
852 * Allow '*' as any address
854 if (strcmp(value, "*") == 0) {
855 *(uint32_t *) data = htonl(INADDR_ANY);
856 DEBUG2("\t%s = *", name);
859 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
860 radlog(L_ERR, "Can't find IP address for host %s", value);
863 DEBUG2("\t%s = %s IP address [%s]", name, value,
864 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
865 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
868 case PW_TYPE_IPV6ADDR:
869 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
870 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
873 DEBUG2("\t%s = %s IPv6 address [%s]", name, value,
874 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
875 memcpy(data, &ipaddr.ipaddr.ip6addr,
876 sizeof(ipaddr.ipaddr.ip6addr));
880 radlog(L_ERR, "type %d not supported yet", type);
883 } /* switch over variable type */
888 static const char *parse_spaces = " ";
891 * Parse a configuration section into user-supplied variables.
893 int cf_section_parse(CONF_SECTION *cs, void *base,
894 const CONF_PARSER *variables)
900 DEBUG2("%.*s%s {", cs->depth, parse_spaces,
903 DEBUG2("%.*s%s %s {", cs->depth, parse_spaces,
904 cs->name1, cs->name2);
908 * Handle the known configuration parameters.
910 for (i = 0; variables[i].name != NULL; i++) {
912 * Handle subsections specially
914 if (variables[i].type == PW_TYPE_SUBSECTION) {
915 const CONF_SECTION *subcs;
916 subcs = cf_section_sub_find(cs, variables[i].name);
919 * If the configuration section is NOT there,
922 * FIXME! This is probably wrong... we should
923 * probably set the items to their default values.
925 if (!subcs) continue;
927 if (!variables[i].dflt) {
928 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
932 if (cf_section_parse(subcs, base,
933 (const CONF_PARSER *) variables[i].dflt) < 0) {
937 } /* else it's a CONF_PAIR */
939 if (variables[i].data) {
940 data = variables[i].data; /* prefer this. */
942 data = ((char *)base) + variables[i].offset;
944 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
949 * Parse the pair we found, or a default value.
951 if (cf_item_parse(cs, variables[i].name, variables[i].type,
952 data, variables[i].dflt) < 0) {
955 } /* for all variables in the configuration section */
957 DEBUG2("%.*s}", cs->depth, parse_spaces);
960 cs->variables = variables;
965 DEBUG2("%.*s}", cs->depth, parse_spaces);
966 cf_section_parse_free(base, variables);
972 * Read a part of the config file.
974 static int cf_section_read(const char *file, int *lineno, FILE *fp,
975 CONF_SECTION *current)
978 CONF_SECTION *this, *css;
990 this = current; /* add items here */
993 * Read, checking for line continuations ('\\' at EOL)
999 * Get data, and remember if we are at EOF.
1001 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1007 * We've filled the buffer, and there isn't
1010 if ((cbuf[len - 1] != '\n') && !feof(fp)) {
1011 radlog(L_ERR, "%s[%d]: Line too long",
1017 * Check for continuations.
1019 if (cbuf[len - 1] == '\n') len--;
1022 * Last character is '\\'. Over-write it,
1023 * and read another line.
1025 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1026 if (len >= (sizeof(buf) - 5)) {
1027 radlog(L_ERR, "%s[%d]: Line too long",
1032 cbuf[len - 1] = '\0';
1038 * We're at EOF, and haven't read anything. Stop.
1040 if (eof && (cbuf == buf)) {
1045 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1047 if ((*buf1 == '#') || (*buf1 == '\0')) {
1052 * The caller eats "name1 name2 {", and calls us
1053 * for the data inside of the section. So if we
1054 * receive a closing brace, then it must mean the
1055 * end of the section.
1057 if (t1 == T_RCBRACE) {
1058 if (this == current) {
1059 radlog(L_ERR, "%s[%d]: Too many closing braces",
1064 this = this->item.parent;
1069 * Allow for $INCLUDE files
1071 * This *SHOULD* work for any level include.
1072 * I really really really hate this file. -cparker
1074 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1075 t2 = getword(&ptr, buf2, sizeof(buf2));
1077 value = cf_expand_variables(file, lineno, this, buf, buf2);
1078 if (!value) return -1;
1080 #ifdef HAVE_DIRENT_H
1084 * Include ALL non-"dot" files in the directory.
1087 if (value[strlen(value) - 1] == '/') {
1090 struct stat stat_buf;
1092 DEBUG2( "Config: including files in directory: %s", value );
1093 dir = opendir(value);
1095 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1096 file, *lineno, value,
1102 * Read the directory, ignoring "." files.
1104 while ((dp = readdir(dir)) != NULL) {
1107 if (dp->d_name[0] == '.') continue;
1110 * Check for valid characters
1112 for (p = dp->d_name; *p != '\0'; p++) {
1113 if (isalpha((int)*p) ||
1116 (*p == '.')) continue;
1119 if (*p != '\0') continue;
1121 snprintf(buf2, sizeof(buf2), "%s%s",
1123 if ((stat(buf2, &stat_buf) != 0) ||
1124 S_ISDIR(stat_buf.st_mode)) continue;
1126 * Read the file into the current
1127 * configuration sectoin.
1129 if (cf_file_include(buf2, this) < 0) {
1137 { /* it was a normal file */
1138 if (cf_file_include(value, this) < 0) {
1143 } /* we were in an include */
1146 * Ensure that the user can't add CONF_PAIRs
1147 * with 'internal' names;
1149 if (buf1[0] == '_') {
1150 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1151 file, *lineno, buf1);
1156 * Grab the next token.
1158 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1164 t3 = getword(&ptr, buf3, sizeof(buf3));
1168 * Handle variable substitution via ${foo}
1170 value = cf_expand_variables(file, lineno, this,
1172 if (!value) return -1;
1176 * Add this CONF_PAIR to our CONF_SECTION
1178 cpn = cf_pair_alloc(buf1, value, t2, this);
1179 cpn->item.lineno = *lineno;
1180 cf_item_add(this, cf_pairtoitem(cpn));
1184 * No '=', must be a section or sub-section.
1187 case T_DOUBLE_QUOTED_STRING:
1188 case T_SINGLE_QUOTED_STRING:
1189 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1190 if (t3 != T_LCBRACE) {
1191 radlog(L_ERR, "%s[%d]: Expecting section start brace '{' after \"%s %s\"",
1192 file, *lineno, buf1, buf2);
1197 css = cf_section_alloc(buf1,
1198 t2 == T_LCBRACE ? NULL : buf2,
1201 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1205 cf_item_add(this, cf_sectiontoitem(css));
1206 css->item.lineno = *lineno;
1209 * The current section is now the child section.
1215 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1216 file, *lineno, buf1);
1222 * See if EOF was unexpected ..
1224 if (feof(fp) && (this != current)) {
1225 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1227 cf_section_name1(this), cf_section_lineno(this));
1235 * Include one config file in another.
1237 int cf_file_include(const char *file, CONF_SECTION *cs)
1241 struct stat statbuf;
1244 DEBUG2( "Config: including file: %s", file);
1246 if (stat(file, &statbuf) == 0) {
1248 if ((statbuf.st_mode & S_IWOTH) != 0) {
1249 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1256 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1257 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1264 fp = fopen(file, "r");
1266 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1267 file, strerror(errno));
1272 * Read the section. It's OK to have EOF without a
1273 * matching close brace.
1275 if (cf_section_read(file, &lineno, fp, cs) < 0) {
1281 * Add the filename to the section
1283 mtime = rad_malloc(sizeof(*mtime));
1284 *mtime = statbuf.st_mtime;
1286 cf_data_add_internal(cs, file, mtime, free,
1294 * Bootstrap a config file.
1296 CONF_SECTION *cf_file_read(const char *file)
1300 cs = cf_section_alloc("main", NULL, NULL);
1301 if (!cs) return NULL;
1303 if (cf_file_include(file, cs) < 0) {
1304 cf_section_free(&cs);
1312 * Return a CONF_PAIR within a CONF_SECTION.
1314 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1317 CONF_PAIR *cp = NULL;
1319 if (!cs) cs = mainconfig.config;
1322 * Find the name in the tree, for speed.
1328 cp = rbtree_finddata(cs->pair_tree, &mycp);
1331 * Else find the first one that matches
1333 for (ci = cs->children; ci; ci = ci->next) {
1334 if (ci->type == CONF_ITEM_PAIR) {
1335 return cf_itemtopair(ci);
1340 if (cp || !cs->template) return cp;
1342 return cf_pair_find(cs->template, name);
1346 * Return the attr of a CONF_PAIR
1349 char *cf_pair_attr(CONF_PAIR *pair)
1351 return (pair ? pair->attr : NULL);
1355 * Return the value of a CONF_PAIR
1358 char *cf_pair_value(CONF_PAIR *pair)
1360 return (pair ? pair->value : NULL);
1364 * Return the first label of a CONF_SECTION
1367 const char *cf_section_name1(const CONF_SECTION *cs)
1369 return (cs ? cs->name1 : NULL);
1373 * Return the second label of a CONF_SECTION
1376 const char *cf_section_name2(const CONF_SECTION *cs)
1378 return (cs ? cs->name2 : NULL);
1382 * Find a value in a CONF_SECTION
1384 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1388 cp = cf_pair_find(cs, attr);
1390 return (cp ? cp->value : NULL);
1394 * Return the next pair after a CONF_PAIR
1395 * with a certain name (char *attr) If the requested
1396 * attr is NULL, any attr matches.
1399 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1400 const CONF_PAIR *pair, const char *attr)
1405 * If pair is NULL this must be a first time run
1406 * Find the pair with correct name
1410 return cf_pair_find(cs, attr);
1413 ci = cf_pairtoitem(pair)->next;
1415 for (; ci; ci = ci->next) {
1416 if (ci->type != CONF_ITEM_PAIR)
1418 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1422 return cf_itemtopair(ci);
1426 * Find a CONF_SECTION, or return the root if name is NULL
1429 CONF_SECTION *cf_section_find(const char *name)
1432 return cf_section_sub_find(mainconfig.config, name);
1434 return mainconfig.config;
1438 * Find a sub-section in a section
1441 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1446 * Do the fast lookup if possible.
1448 if (name && cs->section_tree) {
1453 return rbtree_finddata(cs->section_tree, &mycs);
1456 for (ci = cs->children; ci; ci = ci->next) {
1457 if (ci->type != CONF_ITEM_SECTION)
1459 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1463 return cf_itemtosection(ci);
1469 * Find a CONF_SECTION with both names.
1471 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1472 const char *name1, const char *name2)
1476 if (!name2) return cf_section_sub_find(cs, name1);
1478 if (!cs) cs = mainconfig.config;
1480 if (name1 && (cs->section_tree)) {
1481 CONF_SECTION mycs, *master_cs;
1486 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1488 return rbtree_finddata(master_cs->name2_tree, &mycs);
1493 * Else do it the old-fashioned way.
1495 for (ci = cs->children; ci; ci = ci->next) {
1496 CONF_SECTION *subcs;
1498 if (ci->type != CONF_ITEM_SECTION)
1501 subcs = cf_itemtosection(ci);
1503 if (!subcs->name2) {
1504 if (strcmp(subcs->name1, name2) == 0) break;
1506 if (strcmp(subcs->name2, name2) == 0) break;
1508 continue; /* don't do the string comparisons below */
1511 if ((strcmp(subcs->name1, name1) == 0) &&
1512 (subcs->name2 != NULL) &&
1513 (strcmp(subcs->name2, name2) == 0))
1517 return cf_itemtosection(ci);
1521 * Return the next subsection after a CONF_SECTION
1522 * with a certain name1 (char *name1). If the requested
1523 * name1 is NULL, any name1 matches.
1526 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1527 CONF_SECTION *subsection,
1533 * If subsection is NULL this must be a first time run
1534 * Find the subsection with correct name
1537 if (subsection == NULL){
1538 ci = section->children;
1540 ci = cf_sectiontoitem(subsection)->next;
1543 for (; ci; ci = ci->next) {
1544 if (ci->type != CONF_ITEM_SECTION)
1546 if ((name1 == NULL) ||
1547 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1551 return cf_itemtosection(ci);
1555 * Return the next item after a CONF_ITEM.
1558 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1561 * If item is NULL this must be a first time run
1562 * Return the first item
1566 return section->children;
1572 int cf_section_lineno(CONF_SECTION *section)
1574 return cf_sectiontoitem(section)->lineno;
1577 int cf_pair_lineno(CONF_PAIR *pair)
1579 return cf_pairtoitem(pair)->lineno;
1582 int cf_item_is_section(CONF_ITEM *item)
1584 return item->type == CONF_ITEM_SECTION;
1586 int cf_item_is_pair(CONF_ITEM *item)
1588 return item->type == CONF_ITEM_PAIR;
1592 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1593 void *data, void (*data_free)(void *))
1597 cd = rad_malloc(sizeof(*cd));
1598 memset(cd, 0, sizeof(*cd));
1600 cd->item.type = CONF_ITEM_DATA;
1601 cd->item.parent = parent;
1602 cd->name = strdup(name);
1604 cd->free = data_free;
1610 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1613 if (!cs || !name) return NULL;
1616 * Find the name in the tree, for speed.
1618 if (cs->data_tree) {
1619 CONF_DATA mycd, *cd;
1623 cd = rbtree_finddata(cs->data_tree, &mycd);
1624 if (cd) return cd->data;
1631 * Find data from a particular section.
1633 void *cf_data_find(CONF_SECTION *cs, const char *name)
1635 return cf_data_find_internal(cs, name, 0);
1640 * Add named data to a configuration section.
1642 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1643 void *data, void (*data_free)(void *),
1648 if (!cs || !name) return -1;
1651 * Already exists. Can't add it.
1653 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1655 cd = cf_data_alloc(cs, name, data, data_free);
1659 cf_item_add(cs, cf_datatoitem(cd));
1665 * Add named data to a configuration section.
1667 int cf_data_add(CONF_SECTION *cs, const char *name,
1668 void *data, void (*data_free)(void *))
1670 return cf_data_add_internal(cs, name, data, data_free, 0);
1675 * Copy CONF_DATA from src to dst
1677 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1680 CONF_ITEM *cd, *next, **last;
1683 * Don't check if s->data_tree is NULL. It's child
1684 * sections may have data, even if this section doesn't.
1687 rad_assert(d->data_tree == NULL);
1688 d->data_tree = s->data_tree;
1689 s->data_tree = NULL;
1692 * Walk through src, moving CONF_ITEM_DATA
1695 last = &(s->children);
1696 for (cd = s->children; cd != NULL; cd = next) {
1700 * Recursively copy data from child sections.
1702 if (cd->type == CONF_ITEM_SECTION) {
1703 CONF_SECTION *s1, *d1;
1705 s1 = cf_itemtosection(cd);
1706 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1708 cf_section_copy_data(s1, d1);
1715 * Not conf data, remember last ptr.
1717 if (cd->type != CONF_ITEM_DATA) {
1723 * Remove it from the src list
1729 * Add it to the dst list
1732 rad_assert(d->tail == NULL);
1735 rad_assert(d->tail != NULL);
1743 * For a CONF_DATA element, stat the filename, if necessary.
1745 static int filename_stat(void *context, void *data)
1748 CONF_DATA *cd = data;
1750 context = context; /* -Wunused */
1752 if (cd->flag != PW_TYPE_FILENAME) return 0;
1754 if (stat(cd->name, &buf) < 0) return -1;
1756 if (buf.st_mtime != *(time_t *) cd->data) return -1;
1763 * Compare two CONF_SECTIONS. The items MUST be in the same
1766 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1768 CONF_ITEM *ca = a->children;
1769 CONF_ITEM *cb = b->children;
1777 if (!ca && !cb) break;
1782 if (ca && ca->type == CONF_ITEM_DATA) {
1786 if (cb && cb->type == CONF_ITEM_DATA) {
1792 * One is smaller than the other. Exit.
1794 if (!ca || !cb) return 0;
1796 if (ca->type != cb->type) return 0;
1799 * Deal with subsections.
1801 if (ca->type == CONF_ITEM_SECTION) {
1802 CONF_SECTION *sa = cf_itemtosection(ca);
1803 CONF_SECTION *sb = cf_itemtosection(cb);
1805 if (!cf_section_cmp(sa, sb)) return 0;
1809 rad_assert(ca->type == CONF_ITEM_PAIR);
1811 pa = cf_itemtopair(ca);
1812 pb = cf_itemtopair(cb);
1815 * Different attr and/or value, Exit.
1817 if ((strcmp(pa->attr, pb->attr) != 0) ||
1818 (strcmp(pa->value, pb->value) != 0)) return 0;
1822 * And go to the next element.
1830 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
1833 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
1838 * They must be the same, say so.
1845 * Migrate CONF_DATA from one section to another.
1847 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1850 CONF_SECTION *s, *d;
1852 for (ci = src->children; ci != NULL; ci = ci->next) {
1853 if (ci->type != CONF_ITEM_SECTION)
1856 s = cf_itemtosection(ci);
1857 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1859 if (!d) continue; /* not in new one, don't migrate it */
1862 * A section of the same name is in BOTH src & dst,
1863 * compare the CONF_PAIR's. If they're all the same,
1864 * then copy the CONF_DATA from one to the other.
1866 if (cf_section_cmp(s, d)) {
1867 cf_section_copy_data(s, d);
1871 return 1; /* rcode means anything? */
1874 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
1876 if (!cs || !template || cs->template || template->template) return -1;
1878 cs->template = template;
1885 * JMG dump_config tries to dump the config structure in a readable format
1889 static int dump_config_section(CONF_SECTION *cs, int indent)
1895 /* The DEBUG macro doesn't let me
1896 * for(i=0;i<indent;++i) debugputchar('\t');
1897 * so I had to get creative. --Pac. */
1899 for (ci = cs->children; ci; ci = ci->next) {
1901 case CONF_ITEM_PAIR:
1902 cp=cf_itemtopair(ci);
1903 DEBUG("%.*s%s = %s",
1904 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1905 cp->attr, cp->value);
1908 case CONF_ITEM_SECTION:
1909 scs=cf_itemtosection(ci);
1910 DEBUG("%.*s%s %s%s{",
1911 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1913 scs->name2 ? scs->name2 : "",
1914 scs->name2 ? " " : "");
1915 dump_config_section(scs, indent+1);
1917 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1920 default: /* FIXME: Do more! */
1928 int dump_config(void)
1930 return dump_config_section(mainconfig.config, 0);