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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Copyright 2000 The FreeRADIUS server project
25 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
26 * Copyright 2000 Alan DeKok <aland@ox.org>
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
41 #ifdef HAVE_SYS_STAT_H
49 #include "rad_assert.h"
54 static const char rcsid[] =
57 typedef enum conf_type {
58 CONF_ITEM_INVALID = 0,
65 struct conf_item *next;
66 struct conf_part *parent;
80 struct conf_item *children;
81 rbtree_t *pair_tree; /* and a partridge.. */
82 rbtree_t *section_tree; /* no jokes here */
83 rbtree_t *name2_tree; /* for sections of the same name2 */
88 * Internal data that is associated with a configuration section,
89 * so that we don't have to track it separately.
94 void *data; /* user data */
95 void (*free)(void *); /* free user data function */
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);
197 * rbtree callback function
199 static int pair_cmp(const void *a, const void *b)
201 const CONF_PAIR *one = a;
202 const CONF_PAIR *two = b;
204 return strcmp(one->attr, two->attr);
209 * rbtree callback function
211 static int section_cmp(const void *a, const void *b)
213 const CONF_SECTION *one = a;
214 const CONF_SECTION *two = b;
216 return strcmp(one->name1, two->name1);
221 * rbtree callback function
223 static int name2_cmp(const void *a, const void *b)
225 const CONF_SECTION *one = a;
226 const CONF_SECTION *two = b;
228 rad_assert(strcmp(one->name1, two->name1) == 0);
230 if (!one->name2 && !two->name2) return 0;
231 if (!one->name2) return -1;
232 if (!two->name2) return +1;
234 return strcmp(one->name2, two->name2);
239 * Free a CONF_SECTION
241 void cf_section_free(CONF_SECTION **cs)
243 CONF_ITEM *ci, *next;
245 if (!cs || !*cs) return;
247 for (ci = (*cs)->children; ci; ci = next) {
251 case CONF_ITEM_PAIR: {
252 CONF_PAIR *pair = cf_itemtopair(ci);
257 case CONF_ITEM_SECTION: {
259 CONF_SECTION *section = cf_itemtosection(ci);
260 cf_section_free(§ion);
264 case CONF_ITEM_DATA: {
265 CONF_DATA *data = cf_itemtodata(ci);
270 default: /* should really be an error. */
279 if ((*cs)->pair_tree)
280 rbtree_free((*cs)->pair_tree);
281 if ((*cs)->section_tree)
282 rbtree_free((*cs)->section_tree);
283 if ((*cs)->name2_tree)
284 rbtree_free((*cs)->name2_tree);
287 * And free the section
290 memset(*cs, 0, sizeof(*cs));
299 * Allocate a CONF_SECTION
301 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
302 CONF_SECTION *parent)
306 if (name1 == NULL || !name1[0])
309 cs = rad_malloc(sizeof(*cs));
310 memset(cs, 0, sizeof(*cs));
311 cs->item.type = CONF_ITEM_SECTION;
312 cs->item.parent = parent;
313 cs->name1 = strdup(name1);
315 cf_section_free(&cs);
319 if (name2 && *name2) {
320 cs->name2 = strdup(name2);
322 cf_section_free(&cs);
326 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
327 if (!cs->pair_tree) {
328 cf_section_free(&cs);
333 * Don't create the section tree here, it may not
341 * Add an item to a configuration section.
343 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
348 * New entries are added at the bottom of the list.
350 for (last = &(cs->children);
352 last = &((*last)->next)) {
358 * We may be adding a list, rather than just one element.
359 * If so, loop over all entries.
361 for (*last = ci; ci != NULL; ci = ci->next) {
363 * For fast lookups, pair's and sections get
368 rbtree_insert(cs->pair_tree, ci);
371 case CONF_ITEM_SECTION: {
372 const CONF_SECTION *cs_new = cf_itemtosection(ci);
374 if (!cs->section_tree) {
375 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
376 /* ignore any errors */
379 if (cs->section_tree) {
380 rbtree_insert(cs->section_tree, cs_new);
384 * Two names: find the named instance.
387 CONF_SECTION *old_cs;
391 * CONF_SECTION having
392 * the given name1, and
396 old_cs = rbtree_finddata(cs->section_tree, cs_new);
397 if (!old_cs) return; /* this is a bad error! */
399 if (!old_cs->name2_tree) {
400 old_cs->name2_tree = rbtree_create(name2_cmp,
403 if (old_cs->name2_tree) {
404 rbtree_insert(old_cs->name2_tree, cs_new);
408 } /* was a section */
412 * Don't do anything special.
416 default: /* FIXME: assert & error! */
419 } /* switch over conf types */
424 * Expand the variables in an input string.
426 static const char *cf_expand_variables(const char *cf, int *lineno,
427 const CONF_SECTION *outercs,
428 char *output, const char *input)
431 const char *end, *ptr;
433 const CONF_SECTION *parentcs;
436 * Find the master parent conf section.
437 * We can't use mainconfig.config, because we're in the
438 * process of re-building it, and it isn't set up yet...
440 for (parentcs = outercs;
441 parentcs->item.parent != NULL;
442 parentcs = parentcs->item.parent) {
450 * Ignore anything other than "${"
452 if ((*ptr == '$') && (ptr[1] == '{')) {
455 const CONF_SECTION *cs;
458 * FIXME: Add support for ${foo:-bar},
463 * Look for trailing '}', and log a
464 * warning for anything that doesn't match,
465 * and exit with a fatal error.
467 end = strchr(ptr, '}');
470 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
481 * ${.foo} means "foo from the current section"
489 * ${..foo} means "foo from the section
490 * enclosing this section" (etc.)
492 while (*ptr == '.') {
494 cs = cs->item.parent;
501 * ${foo} is local, with
502 * main as lower priority
507 * ${foo.bar.baz} is always rooted
510 for (q = ptr; *q && q != end; q++) {
522 * Find the next section.
525 (*ptr != 0) && (*ptr != '.') &&
533 * The character is a '.', find a
534 * section (as the user has given
535 * us a subsection to find)
540 ptr++; /* skip the period */
543 * Find the sub-section.
545 next = cf_section_sub_find(cs, name);
547 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
552 } else { /* no period, must be a conf-part */
554 * Find in the current referenced
557 cp = cf_pair_find(cs, name);
560 * It it was NOT ${..foo}
562 * top-level config items.
564 if (!up) cp = cf_pair_find(parentcs, name);
567 radlog(L_ERR, "config: No such entry %s for string %s", name, input);
571 } /* until cp is non-NULL */
574 * Substitute the value of the variable.
576 strcpy(p, cp->value);
580 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
586 * Look for trailing '}', and log a
587 * warning for anything that doesn't match,
588 * and exit with a fatal error.
590 end = strchr(ptr, '}');
593 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
598 memcpy(name, ptr, end - ptr);
599 name[end - ptr] = '\0';
602 * Get the environment variable.
603 * If none exists, then make it an empty string.
617 * Copy it over verbatim.
621 } /* loop over all of the input string. */
630 * Parses an item (not a CONF_ITEM) into the specified format,
631 * with a default value.
633 * Returns -1 on error, 0 for correctly parsed, and 1 if the
634 * default value was used. Note that the default value will be
635 * used ONLY if the CONF_PAIR is NULL.
637 int cf_item_parse(const CONF_SECTION *cs, const char *name,
638 int type, void *data, const char *dflt)
643 lrad_ipaddr_t ipaddr;
647 cp = cf_pair_find(cs, name);
652 return 1; /* nothing to parse, return default value */
660 case PW_TYPE_BOOLEAN:
662 * Allow yes/no and on/off
664 if ((strcasecmp(value, "yes") == 0) ||
665 (strcasecmp(value, "on") == 0)) {
667 } else if ((strcasecmp(value, "no") == 0) ||
668 (strcasecmp(value, "off") == 0)) {
672 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
675 DEBUG2(" %s: %s = %s", cs->name1, name, value);
678 case PW_TYPE_INTEGER:
679 *(int *)data = strtol(value, 0, 0);
680 DEBUG2(" %s: %s = %d",
685 case PW_TYPE_STRING_PTR:
692 * Expand variables which haven't already been
693 * expanded automagically when the configuration
699 int lineno = cs->item.lineno;
702 * FIXME: sizeof(buffer)?
704 value = cf_expand_variables("?",
707 if (!value) return -1;
710 DEBUG2(" %s: %s = \"%s\"",
712 value ? value : "(null)");
713 *q = value ? strdup(value) : NULL;
718 * Allow '*' as any address
720 if (strcmp(value, "*") == 0) {
721 *(uint32_t *) data = htonl(INADDR_ANY);
722 DEBUG2(" %s: %s = *", cs->name1, name);
725 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
726 radlog(L_ERR, "Can't find IP address for host %s", value);
729 DEBUG2(" %s: %s = %s IP address [%s]",
730 cs->name1, name, value,
731 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
732 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
735 case PW_TYPE_IPV6ADDR:
736 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
737 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
740 DEBUG2(" %s: %s = %s IPv6 address [%s]",
741 cs->name1, name, value,
742 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
743 memcpy(data, &ipaddr.ipaddr.ip6addr,
744 sizeof(ipaddr.ipaddr.ip6addr));
748 radlog(L_ERR, "type %d not supported yet", type);
751 } /* switch over variable type */
757 * Parse a configuration section into user-supplied variables.
759 int cf_section_parse(const CONF_SECTION *cs, void *base,
760 const CONF_PARSER *variables)
766 * Handle the known configuration parameters.
768 for (i = 0; variables[i].name != NULL; i++) {
770 * Handle subsections specially
772 if (variables[i].type == PW_TYPE_SUBSECTION) {
773 const CONF_SECTION *subcs;
774 subcs = cf_section_sub_find(cs, variables[i].name);
777 * If the configuration section is NOT there,
780 * FIXME! This is probably wrong... we should
781 * probably set the items to their default values.
783 if (!subcs) continue;
785 if (!variables[i].data) {
786 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
790 if (cf_section_parse(subcs, base,
791 (CONF_PARSER *) variables[i].data) < 0) {
795 } /* else it's a CONF_PAIR */
797 if (variables[i].data) {
798 data = variables[i].data; /* prefer this. */
800 data = ((char *)base) + variables[i].offset;
802 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
807 * Parse the pair we found, or a default value.
809 if (cf_item_parse(cs, variables[i].name, variables[i].type,
810 data, variables[i].dflt) < 0) {
813 } /* for all variables in the configuration section */
819 * Used in a few places, so in one function for clarity.
821 static void cf_fixup_children(CONF_SECTION *cs, CONF_SECTION *is)
824 * Add the included conf
825 * to our CONF_SECTION
827 if (is->children != NULL) {
831 * Re-write the parent of the
832 * moved children to be the
833 * upper-layer section.
835 for (ci = is->children; ci; ci = ci->next) {
840 * If there are children, then
841 * move them up a layer.
844 cf_item_add(cs, is->children);
849 * Always free the section for the
852 cf_section_free(&is);
857 * Read a part of the config file.
859 static CONF_SECTION *cf_section_read(const char *cf, int *lineno, FILE *fp,
860 const char *name1, const char *name2,
861 CONF_SECTION *parent)
863 CONF_SECTION *cs, *css;
876 * Ensure that the user can't add CONF_SECTIONs
877 * with 'internal' names;
879 if ((name1 != NULL) && (name1[0] == '_')) {
880 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
886 * Allocate new section.
888 cs = cf_section_alloc(name1, name2, parent);
889 cs->item.lineno = *lineno;
892 * Read, checking for line continuations ('\\' at EOL)
898 * Get data, and remember if we are at EOF.
900 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
906 * We've filled the buffer, and there isn't
909 if ((len == sizeof(buf)) &&
910 (cbuf[len - 1] != '\n')) {
911 radlog(L_ERR, "%s[%d]: Line too long",
913 cf_section_free(&cs);
918 * Check for continuations.
920 if (cbuf[len - 1] == '\n') len--;
923 * Last character is '\\'. Over-write it,
924 * and read another line.
926 if ((len > 0) && (cbuf[len - 1] == '\\')) {
927 cbuf[len - 1] = '\0';
933 * We're at EOF, and haven't read anything. Stop.
935 if (eof && (cbuf == buf)) {
940 t1 = gettoken(&ptr, buf1, sizeof(buf1));
943 * Skip comments and blank lines immediately.
945 if ((*buf1 == '#') || (*buf1 == '\0')) {
950 * Allow for $INCLUDE files
952 * This *SHOULD* work for any level include.
953 * I really really really hate this file. -cparker
955 if (strcasecmp(buf1, "$INCLUDE") == 0) {
958 t2 = getword(&ptr, buf2, sizeof(buf2));
960 value = cf_expand_variables(cf, lineno, cs, buf, buf2);
962 cf_section_free(&cs);
970 * Include ALL non-"dot" files in the directory.
973 if (value[strlen(value) - 1] == '/') {
976 struct stat stat_buf;
978 DEBUG2( "Config: including files in directory: %s", value );
979 dir = opendir(value);
981 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
984 cf_section_free(&cs);
989 * Read the directory, ignoring "." files.
991 while ((dp = readdir(dir)) != NULL) {
994 if (dp->d_name[0] == '.') continue;
997 * Check for valid characters
999 for (p = dp->d_name; *p != '\0'; p++) {
1000 if (isalpha((int)*p) ||
1003 (*p == '.')) continue;
1006 if (*p != '\0') continue;
1008 snprintf(buf2, sizeof(buf2), "%s%s",
1010 if ((stat(buf2, &stat_buf) != 0) ||
1011 S_ISDIR(stat_buf.st_mode)) continue;
1012 if ((is = conf_read(cf, *lineno, buf2, parent)) == NULL) {
1014 cf_section_free(&cs);
1018 cf_fixup_children(cs, is);
1023 { /* it was a normal file */
1024 DEBUG2( "Config: including file: %s", value );
1025 if ((is = conf_read(cf, *lineno, value, parent)) == NULL) {
1026 cf_section_free(&cs);
1029 cf_fixup_children(cs, is);
1032 } /* we were in an include */
1035 * No '=': must be a section or sub-section.
1037 if (strchr(ptr, '=') == NULL) {
1038 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1039 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1041 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1042 t3 = getword(&ptr, buf3, sizeof(buf3));
1046 * See if it's the end of a section.
1048 if (t1 == T_RCBRACE) {
1049 if (name1 == NULL || buf2[0]) {
1050 radlog(L_ERR, "%s[%d]: Unexpected end of section",
1052 cf_section_free(&cs);
1059 * Perhaps a subsection.
1061 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1062 css = cf_section_read(cf, lineno, fp, buf1,
1063 t2==T_LCBRACE ? NULL : buf2, cs);
1065 cf_section_free(&cs);
1068 cf_item_add(cs, cf_sectiontoitem(css));
1074 * Ignore semi-colons.
1080 * Must be a normal attr = value line.
1082 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1084 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1085 (t2 < T_EQSTART || t2 > T_EQEND)) {
1086 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1088 cf_section_free(&cs);
1093 * Ensure that the user can't add CONF_PAIRs
1094 * with 'internal' names;
1096 if (buf1[0] == '_') {
1097 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1099 cf_section_free(&cs);
1104 * Handle variable substitution via ${foo}
1106 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
1108 cf_section_free(&cs);
1114 * Add this CONF_PAIR to our CONF_SECTION
1116 cpn = cf_pair_alloc(buf1, value, t2, parent);
1117 cpn->item.lineno = *lineno;
1118 cf_item_add(cs, cf_pairtoitem(cpn));
1122 * See if EOF was unexpected ..
1124 if (name1 != NULL) {
1125 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
1126 cf_section_free(&cs);
1134 * Read the config file.
1136 CONF_SECTION *conf_read(const char *fromfile, int fromline,
1137 const char *conffile, CONF_SECTION *parent)
1143 if ((fp = fopen(conffile, "r")) == NULL) {
1145 radlog(L_ERR|L_CONS, "%s[%d]: Unable to open file \"%s\": %s",
1146 fromfile, fromline, conffile, strerror(errno));
1148 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1149 conffile, strerror(errno));
1154 cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
1163 * Return a CONF_PAIR within a CONF_SECTION.
1165 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1169 if (!cs) cs = mainconfig.config;
1172 * Find the name in the tree, for speed.
1178 return rbtree_finddata(cs->pair_tree, &mycp);
1182 * Else find the first one
1184 for (ci = cs->children; ci; ci = ci->next) {
1185 if (ci->type == CONF_ITEM_PAIR)
1186 return cf_itemtopair(ci);
1193 * Return the attr of a CONF_PAIR
1196 char *cf_pair_attr(CONF_PAIR *pair)
1198 return (pair ? pair->attr : NULL);
1202 * Return the value of a CONF_PAIR
1205 char *cf_pair_value(CONF_PAIR *pair)
1207 return (pair ? pair->value : NULL);
1211 * Return the first label of a CONF_SECTION
1214 const char *cf_section_name1(const CONF_SECTION *cs)
1216 return (cs ? cs->name1 : NULL);
1220 * Return the second label of a CONF_SECTION
1223 const char *cf_section_name2(const CONF_SECTION *cs)
1225 return (cs ? cs->name2 : NULL);
1229 * Find a value in a CONF_SECTION
1231 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1235 cp = cf_pair_find(cs, attr);
1237 return (cp ? cp->value : NULL);
1241 * Return the next pair after a CONF_PAIR
1242 * with a certain name (char *attr) If the requested
1243 * attr is NULL, any attr matches.
1246 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1247 const CONF_PAIR *pair, const char *attr)
1252 * If pair is NULL this must be a first time run
1253 * Find the pair with correct name
1257 return cf_pair_find(cs, attr);
1260 ci = cf_pairtoitem(pair)->next;
1262 for (; ci; ci = ci->next) {
1263 if (ci->type != CONF_ITEM_PAIR)
1265 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1269 return cf_itemtopair(ci);
1273 * Find a CONF_SECTION, or return the root if name is NULL
1276 CONF_SECTION *cf_section_find(const char *name)
1279 return cf_section_sub_find(mainconfig.config, name);
1281 return mainconfig.config;
1285 * Find a sub-section in a section
1288 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1293 * Do the fast lookup if possible.
1295 if (name && cs->section_tree) {
1300 return rbtree_finddata(cs->section_tree, &mycs);
1303 for (ci = cs->children; ci; ci = ci->next) {
1304 if (ci->type != CONF_ITEM_SECTION)
1306 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1310 return cf_itemtosection(ci);
1316 * Find a CONF_SECTION with both names.
1318 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1319 const char *name1, const char *name2)
1323 if (!name2) return cf_section_sub_find(cs, name1);
1325 if (!cs) cs = mainconfig.config;
1327 if (name1 && (cs->section_tree)) {
1328 CONF_SECTION mycs, *master_cs;
1333 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1335 return rbtree_finddata(master_cs->name2_tree, &mycs);
1340 * Else do it the old-fashioned way.
1342 for (ci = cs->children; ci; ci = ci->next) {
1343 CONF_SECTION *subcs;
1345 if (ci->type != CONF_ITEM_SECTION)
1348 subcs = cf_itemtosection(ci);
1350 if (!subcs->name2) {
1351 if (strcmp(subcs->name1, name2) == 0) break;
1353 if (strcmp(subcs->name2, name2) == 0) break;
1355 continue; /* don't do the string comparisons below */
1358 if ((strcmp(subcs->name1, name1) == 0) &&
1359 (subcs->name2 != NULL) &&
1360 (strcmp(subcs->name2, name2) == 0))
1364 return cf_itemtosection(ci);
1368 * Return the next subsection after a CONF_SECTION
1369 * with a certain name1 (char *name1). If the requested
1370 * name1 is NULL, any name1 matches.
1373 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1374 CONF_SECTION *subsection,
1380 * If subsection is NULL this must be a first time run
1381 * Find the subsection with correct name
1384 if (subsection == NULL){
1385 ci = section->children;
1387 ci = cf_sectiontoitem(subsection)->next;
1390 for (; ci; ci = ci->next) {
1391 if (ci->type != CONF_ITEM_SECTION)
1393 if ((name1 == NULL) ||
1394 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1398 return cf_itemtosection(ci);
1402 * Return the next item after a CONF_ITEM.
1405 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1408 * If item is NULL this must be a first time run
1409 * Return the first item
1413 return section->children;
1419 int cf_section_lineno(CONF_SECTION *section)
1421 return cf_sectiontoitem(section)->lineno;
1424 int cf_pair_lineno(CONF_PAIR *pair)
1426 return cf_pairtoitem(pair)->lineno;
1429 int cf_item_is_section(CONF_ITEM *item)
1431 return item->type == CONF_ITEM_SECTION;
1435 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1436 void *data, void (*data_free)(void *))
1440 cd = rad_malloc(sizeof(*cd));
1441 memset(cd, 0, sizeof(*cd));
1443 cd->item.type = CONF_ITEM_DATA;
1444 cd->item.parent = parent;
1445 cd->name = strdup(name);
1447 cd->free = data_free;
1454 * Find data from a particular section.
1456 void *cf_data_find(CONF_SECTION *cs, const char *name)
1460 if (!cs || !name) return NULL;
1462 for (ci = cs->children; ci != NULL; ci = ci->next) {
1466 * Data is always inserted at the front of the
1469 if (ci->type != CONF_ITEM_DATA) return NULL;
1471 cd = cf_itemtodata(ci);
1472 if (strcmp(name, cd->name) == 0) return cd->data;
1480 * Add named data to a configuration section.
1482 int cf_data_add(CONF_SECTION *cs, const char *name,
1483 void *data, void (*data_free)(void *))
1487 if (!cs || !name || !data) return -1;
1490 * Already exists. Can't add it.
1492 if (cf_data_find(cs, name) != NULL) return -1;
1494 cd = cf_data_alloc(cs, name, data, data_free);
1497 cf_item_add(cs, cf_datatoitem(cd));
1505 * JMG dump_config tries to dump the config structure in a readable format
1509 static int dump_config_section(CONF_SECTION *cs, int indent)
1515 /* The DEBUG macro doesn't let me
1516 * for(i=0;i<indent;++i) debugputchar('\t');
1517 * so I had to get creative. --Pac. */
1519 for (ci = cs->children; ci; ci = ci->next) {
1521 case CONF_ITEM_PAIR:
1522 cp=cf_itemtopair(ci);
1523 DEBUG("%.*s%s = %s",
1524 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1525 cp->attr, cp->value);
1528 case CONF_ITEM_SECTION:
1529 scs=cf_itemtosection(ci);
1530 DEBUG("%.*s%s %s%s{",
1531 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1533 scs->name2 ? scs->name2 : "",
1534 scs->name2 ? " " : "");
1535 dump_config_section(scs, indent+1);
1537 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1540 default: /* FIXME: Do more! */
1548 int dump_config(void)
1550 return dump_config_section(mainconfig.config, 0);