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>
39 #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;
70 struct conf_item *children;
71 struct conf_item *tail; /* for speed */
72 CONF_SECTION *template;
73 rbtree_t *pair_tree; /* and a partridge.. */
74 rbtree_t *section_tree; /* no jokes here */
75 rbtree_t *name2_tree; /* for sections of the same name2 */
79 const CONF_PARSER *variables;
84 * Internal data that is associated with a configuration section,
85 * so that we don't have to track it separately.
91 void *data; /* user data */
92 void (*free)(void *); /* free user data function */
96 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
97 void *data, void (*data_free)(void *),
99 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
102 int cf_log_config = 1;
103 int cf_log_modules = 1;
106 * Isolate the scary casts in these tiny provably-safe functions
108 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
112 rad_assert(ci->type == CONF_ITEM_PAIR);
113 return (CONF_PAIR *)ci;
115 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
119 rad_assert(ci->type == CONF_ITEM_SECTION);
120 return (CONF_SECTION *)ci;
122 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
126 return (CONF_ITEM *)cp;
128 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
132 return (CONF_ITEM *)cs;
135 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
139 rad_assert(ci->type == CONF_ITEM_DATA);
140 return (CONF_DATA *)ci;
142 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
146 return (CONF_ITEM *)cd;
150 * Create a new CONF_PAIR
152 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
153 FR_TOKEN operator, FR_TOKEN value_type,
154 CONF_SECTION *parent)
157 size_t attr_len, value_len = 0;
160 if (!attr) return NULL;
161 attr_len = strlen(attr) + 1;
162 if (value) value_len = strlen(value) + 1;
164 p = rad_malloc(sizeof(*cp) + attr_len + value_len);
166 cp = (CONF_PAIR *) p;
167 memset(cp, 0, sizeof(*cp));
168 cp->item.type = CONF_ITEM_PAIR;
169 cp->item.parent = parent;
172 memcpy(p, attr, attr_len);
177 memcpy(p, value, value_len);
180 cp->value_type = value_type;
181 cp->operator = operator;
189 void cf_pair_free(CONF_PAIR **cp)
191 if (!cp || !*cp) return;
194 * attr && value are allocated contiguous with cp.
198 memset(*cp, 0, sizeof(*cp));
206 static void cf_data_free(CONF_DATA **cd)
208 if (!cd || !*cd) return;
210 /* name is allocated contiguous with cd */
214 ((*cd)->free)((*cd)->data);
217 memset(*cd, 0, sizeof(*cd));
224 * rbtree callback function
226 static int pair_cmp(const void *a, const void *b)
228 const CONF_PAIR *one = a;
229 const CONF_PAIR *two = b;
231 return strcmp(one->attr, two->attr);
236 * rbtree callback function
238 static int section_cmp(const void *a, const void *b)
240 const CONF_SECTION *one = a;
241 const CONF_SECTION *two = b;
243 return strcmp(one->name1, two->name1);
248 * rbtree callback function
250 static int name2_cmp(const void *a, const void *b)
252 const CONF_SECTION *one = a;
253 const CONF_SECTION *two = b;
255 rad_assert(strcmp(one->name1, two->name1) == 0);
257 if (!one->name2 && !two->name2) return 0;
258 if (!one->name2) return -1;
259 if (!two->name2) return +1;
261 return strcmp(one->name2, two->name2);
266 * rbtree callback function
268 static int data_cmp(const void *a, const void *b)
272 const CONF_DATA *one = a;
273 const CONF_DATA *two = b;
275 rcode = one->flag - two->flag;
276 if (rcode != 0) return rcode;
278 return strcmp(one->name, two->name);
283 * Free strings we've parsed into data structures.
285 void cf_section_parse_free(CONF_SECTION *cs, void *base)
288 const CONF_PARSER *variables = cs->variables;
291 * Don't automatically free the strings if we're being
292 * called from a module. This is also for clients.c,
293 * where client_free() expects to be able to free the
294 * client structure. If we moved everything to key off
295 * of the config files, we might solve some problems...
297 if (!variables) return;
300 * Free up dynamically allocated string pointers.
302 for (i = 0; variables[i].name != NULL; i++) {
305 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
306 (variables[i].type != PW_TYPE_FILENAME)) {
311 * No base struct offset, data must be the pointer.
312 * If data doesn't exist, ignore the entry, there
313 * must be something wrong.
316 if (!variables[i].data) {
320 p = (char **) variables[i].data;;
322 } else if (variables[i].data) {
323 p = (char **) variables[i].data;;
326 p = (char **) (((char *)base) + variables[i].offset);
336 * Free a CONF_SECTION
338 void cf_section_free(CONF_SECTION **cs)
340 CONF_ITEM *ci, *next;
342 if (!cs || !*cs) return;
344 cf_section_parse_free(*cs, (*cs)->base);
346 for (ci = (*cs)->children; ci; ci = next) {
350 case CONF_ITEM_PAIR: {
351 CONF_PAIR *pair = cf_itemtopair(ci);
356 case CONF_ITEM_SECTION: {
357 CONF_SECTION *section = cf_itemtosection(ci);
358 cf_section_free(§ion);
362 case CONF_ITEM_DATA: {
363 CONF_DATA *data = cf_itemtodata(ci);
368 default: /* should really be an error. */
374 * Name1 and name2 are allocated contiguous with
377 if ((*cs)->pair_tree)
378 rbtree_free((*cs)->pair_tree);
379 if ((*cs)->section_tree)
380 rbtree_free((*cs)->section_tree);
381 if ((*cs)->name2_tree)
382 rbtree_free((*cs)->name2_tree);
383 if ((*cs)->data_tree)
384 rbtree_free((*cs)->data_tree);
387 * And free the section
390 memset(*cs, 0, sizeof(*cs));
399 * Allocate a CONF_SECTION
401 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
402 CONF_SECTION *parent)
404 size_t name1_len, name2_len = 0;
408 if (!name1) return NULL;
410 name1_len = strlen(name1) + 1;
411 if (name2) name2_len = strlen(name2) + 1;
413 p = rad_malloc(sizeof(*cs) + name1_len + name2_len);
415 cs = (CONF_SECTION *) p;
416 memset(cs, 0, sizeof(*cs));
417 cs->item.type = CONF_ITEM_SECTION;
418 cs->item.parent = parent;
421 memcpy(p, name1, name1_len);
424 if (name2 && *name2) {
426 memcpy(p, name2, name2_len);
430 cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
431 if (!cs->pair_tree) {
432 cf_section_free(&cs);
437 * Don't create a data tree, it may not be needed.
441 * Don't create the section tree here, it may not
445 if (parent) cs->depth = parent->depth + 1;
451 * Replace pair in a given section with a new pair,
452 * of the given value.
454 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, const char *value)
457 CONF_ITEM *ci, *cn, **last;
459 newp = cf_pair_alloc(cp->attr, value, cp->operator, cp->value_type,
461 if (!newp) return -1;
463 ci = cf_pairtoitem(cp);
464 cn = cf_pairtoitem(newp);
467 * Find the old one from the linked list, and replace it
470 for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
472 cn->next = (*last)->next;
479 rbtree_deletebydata(cs->pair_tree, ci);
481 rbtree_insert(cs->pair_tree, cn);
488 * Add an item to a configuration section.
490 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
493 rad_assert(cs->tail == NULL);
496 rad_assert(cs->tail != NULL);
501 * Update the trees (and tail) for each item added.
503 for (/* nothing */; ci != NULL; ci = ci->next) {
507 * For fast lookups, pair's and sections get
512 rbtree_insert(cs->pair_tree, ci);
515 case CONF_ITEM_SECTION: {
516 CONF_SECTION *cs_new = cf_itemtosection(ci);
518 if (!cs->section_tree) {
519 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
520 if (!cs->section_tree) {
521 radlog(L_ERR, "Out of memory");
526 rbtree_insert(cs->section_tree, cs_new);
529 * Two names: find the named instance.
532 CONF_SECTION *old_cs;
536 * CONF_SECTION having
537 * the given name1, and
541 old_cs = rbtree_finddata(cs->section_tree, cs_new);
542 if (!old_cs) return; /* this is a bad error! */
544 if (!old_cs->name2_tree) {
545 old_cs->name2_tree = rbtree_create(name2_cmp,
548 if (old_cs->name2_tree) {
549 rbtree_insert(old_cs->name2_tree, cs_new);
553 } /* was a section */
556 if (!cs->data_tree) {
557 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
560 rbtree_insert(cs->data_tree, ci);
564 default: /* FIXME: assert & error! */
567 } /* switch over conf types */
572 CONF_ITEM *cf_reference_item(const CONF_SECTION *parentcs,
573 CONF_SECTION *outercs,
578 const CONF_SECTION *cs = outercs;
582 strlcpy(name, ptr, sizeof(name));
586 * ".foo" means "foo from the current section"
592 * ..foo means "foo from the section
593 * enclosing this section" (etc.)
597 cs = cs->item.parent;
602 * "foo.bar.baz" means "from the root"
604 } else if (strchr(p, '.') != NULL) {
605 if (!parentcs) goto no_such_item;
617 if (r && q > r) q = NULL;
618 if (q && q < r) r = NULL;
624 q = strchr(r + 1, ']');
625 if (!q) return NULL; /* parse error */
628 * Points to foo[bar]xx: parse error,
629 * it should be foo[bar] or foo[bar].baz
631 if (q[1] && q[1] != '.') goto no_such_item;
635 next = cf_section_sub_find_name2(cs, p, r + 1);
640 * Points to a named instance of a section.
643 if (!next) goto no_such_item;
644 return cf_sectiontoitem(next);
647 q++; /* ensure we skip the ']' and '.' */
651 next = cf_section_sub_find(cs, p);
655 if (!next) break; /* it MAY be a pair in this section! */
661 if (!*p) goto no_such_item;
665 * Find it in the current referenced
668 cp = cf_pair_find(cs, p);
669 if (cp) return cf_pairtoitem(cp);
671 next = cf_section_sub_find(cs, p);
672 if (next) return cf_sectiontoitem(next);
675 * "foo" is "in the current section, OR in main".
677 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
683 DEBUG2("WARNING: No such configuration item %s", ptr);
688 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
690 while (cs->item.parent != NULL) {
691 cs = cs->item.parent;
699 * Expand the variables in an input string.
701 static const char *cf_expand_variables(const char *cf, int *lineno,
702 CONF_SECTION *outercs,
703 char *output, const char *input)
706 const char *end, *ptr;
707 const CONF_SECTION *parentcs;
711 * Find the master parent conf section.
712 * We can't use mainconfig.config, because we're in the
713 * process of re-building it, and it isn't set up yet...
715 parentcs = cf_top_section(outercs);
721 * Ignore anything other than "${"
723 if ((*ptr == '$') && (ptr[1] == '{')) {
728 * FIXME: Add support for ${foo:-bar},
733 * Look for trailing '}', and log a
734 * warning for anything that doesn't match,
735 * and exit with a fatal error.
737 end = strchr(ptr, '}');
740 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
748 * Can't really happen because input lines are
749 * capped at 8k, which is sizeof(name)
751 if ((size_t) (end - ptr) >= sizeof(name)) {
752 radlog(L_ERR, "%s[%d]: Reference string is too large",
757 memcpy(name, ptr, end - ptr);
758 name[end - ptr] = '\0';
760 ci = cf_reference_item(parentcs, outercs, name);
761 if (!ci || (ci->type != CONF_ITEM_PAIR)) {
762 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
768 * Substitute the value of the variable.
770 cp = cf_itemtopair(ci);
772 radlog(L_ERR, "%s[%d]: Reference \"%s\" has no value",
776 strcpy(p, cp->value);
780 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
786 * Look for trailing '}', and log a
787 * warning for anything that doesn't match,
788 * and exit with a fatal error.
790 end = strchr(ptr, '}');
793 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
799 * Can't really happen because input lines are
800 * capped at 8k, which is sizeof(name)
802 if ((size_t) (end - ptr) >= sizeof(name)) {
803 radlog(L_ERR, "%s[%d]: Environment variable name is too large",
808 memcpy(name, ptr, end - ptr);
809 name[end - ptr] = '\0';
812 * Get the environment variable.
813 * If none exists, then make it an empty string.
827 * Copy it over verbatim.
831 } /* loop over all of the input string. */
840 * Parses an item (not a CONF_ITEM) into the specified format,
841 * with a default value.
843 * Returns -1 on error, 0 for correctly parsed, and 1 if the
844 * default value was used. Note that the default value will be
845 * used ONLY if the CONF_PAIR is NULL.
847 int cf_item_parse(CONF_SECTION *cs, const char *name,
848 int type, void *data, const char *dflt)
854 const CONF_PAIR *cp = NULL;
857 if (cs) cp = cf_pair_find(cs, name);
862 return 1; /* nothing to parse, return default value */
874 case PW_TYPE_BOOLEAN:
876 * Allow yes/no and on/off
878 if ((strcasecmp(value, "yes") == 0) ||
879 (strcasecmp(value, "on") == 0)) {
881 } else if ((strcasecmp(value, "no") == 0) ||
882 (strcasecmp(value, "off") == 0)) {
886 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
889 cf_log_info(cs, "\t%s = %s", name, value);
892 case PW_TYPE_INTEGER:
893 *(int *)data = strtol(value, 0, 0);
894 cf_log_info(cs, "\t%s = %d", name, *(int *)data);
897 case PW_TYPE_STRING_PTR:
904 * Expand variables which haven't already been
905 * expanded automagically when the configuration
911 int lineno = cs->item.lineno;
914 * FIXME: sizeof(buffer)?
916 value = cf_expand_variables("?",
919 if (!value) return -1;
922 cf_log_info(cs, "\t%s = \"%s\"", name, value ? value : "(null)");
923 *q = value ? strdup(value) : NULL;
927 * This is the same as PW_TYPE_STRING_PTR,
928 * except that we also "stat" the file, and
931 case PW_TYPE_FILENAME:
938 * Expand variables which haven't already been
939 * expanded automagically when the configuration
945 int lineno = cs->item.lineno;
948 * FIXME: sizeof(buffer)?
950 value = cf_expand_variables("?",
953 if (!value) return -1;
956 cf_log_info(cs, "\t%s = \"%s\"", name, value ? value : "(null)");
957 *q = value ? strdup(value) : NULL;
960 * And now we "stat" the file.
962 * FIXME: This appears to leak memory on exit,
963 * and we don't use this information. So it's
964 * commented out for now.
969 if (stat(*q, &buf) == 0) {
972 mtime = rad_malloc(sizeof(*mtime));
973 *mtime = buf.st_mtime;
975 cf_data_add_internal(cs, *q, mtime, free,
983 * Allow '*' as any address
985 if (strcmp(value, "*") == 0) {
986 *(uint32_t *) data = htonl(INADDR_ANY);
987 cf_log_info(cs, "\t%s = *", name);
990 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
991 radlog(L_ERR, "Can't find IP address for host %s", value);
995 if (strspn(value, "0123456789.") == strlen(value)) {
996 cf_log_info(cs, "\t%s = %s", name, value);
998 cf_log_info(cs, "\t%s = %s IP address [%s]", name, value,
999 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
1001 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
1004 case PW_TYPE_IPV6ADDR:
1005 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
1006 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
1009 cf_log_info(cs, "\t%s = %s IPv6 address [%s]", name, value,
1010 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
1011 memcpy(data, &ipaddr.ipaddr.ip6addr,
1012 sizeof(ipaddr.ipaddr.ip6addr));
1016 radlog(L_ERR, "type %d not supported yet", type);
1019 } /* switch over variable type */
1024 static const char *parse_spaces = " ";
1027 * Parse a configuration section into user-supplied variables.
1029 int cf_section_parse(CONF_SECTION *cs, void *base,
1030 const CONF_PARSER *variables)
1035 cs->variables = variables; /* this doesn't hurt anything */
1038 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1041 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1042 cs->name1, cs->name2);
1046 * Handle the known configuration parameters.
1048 for (i = 0; variables[i].name != NULL; i++) {
1050 * Handle subsections specially
1052 if (variables[i].type == PW_TYPE_SUBSECTION) {
1053 CONF_SECTION *subcs;
1054 subcs = cf_section_sub_find(cs, variables[i].name);
1057 * If the configuration section is NOT there,
1060 * FIXME! This is probably wrong... we should
1061 * probably set the items to their default values.
1063 if (!subcs) continue;
1065 if (!variables[i].dflt) {
1066 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
1070 if (cf_section_parse(subcs, base,
1071 (const CONF_PARSER *) variables[i].dflt) < 0) {
1075 } /* else it's a CONF_PAIR */
1077 if (variables[i].data) {
1078 data = variables[i].data; /* prefer this. */
1080 data = ((char *)base) + variables[i].offset;
1082 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1087 * Parse the pair we found, or a default value.
1089 if (cf_item_parse(cs, variables[i].name, variables[i].type,
1090 data, variables[i].dflt) < 0) {
1093 } /* for all variables in the configuration section */
1095 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1102 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1103 cf_section_parse_free(cs, base);
1109 * Sanity check the "if" or "elsif", presuming that the first '('
1110 * has already been eaten.
1112 * We're not really parsing it here, just checking if it's mostly
1115 static int condition_looks_ok(const char **ptr)
1119 const char *p = *ptr;
1131 return 0; /* no trailing slash */
1143 return 0; /* no trailing slash */
1154 if (num_braces == 1) {
1155 const char *q = p + 1;
1158 * Validate that there isn't much
1159 * else after the closing brace.
1161 while ((*q == ' ') || (*q == '\t')) q++;
1170 *ptr = p + 1; /* include the trailing ')' */
1194 static const char *cf_local_file(CONF_SECTION *cs, const char *local,
1195 char *buffer, size_t bufsize)
1199 CONF_SECTION *parentcs = cf_top_section(cs);
1201 p = strrchr(parentcs->item.filename, FR_DIR_SEP);
1202 if (!p) return local;
1204 dirsize = (p - parentcs->item.filename) + 1;
1206 if ((dirsize + strlen(local)) >= bufsize) {
1210 memcpy(buffer, parentcs->item.filename, dirsize);
1211 strlcpy(buffer + dirsize, local, bufsize - dirsize);
1218 * Read a part of the config file.
1220 static int cf_section_read(const char *filename, int *lineno, FILE *fp,
1221 CONF_SECTION *current)
1224 CONF_SECTION *this, *css;
1236 this = current; /* add items here */
1239 * Read, checking for line continuations ('\\' at EOL)
1245 * Get data, and remember if we are at EOF.
1247 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1251 * We read the entire 8k worth of data: complain.
1252 * Note that we don't care if the last character
1253 * is \n: it's still forbidden. This means that
1254 * the maximum allowed length of text is 8k-1, which
1258 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
1259 radlog(L_ERR, "%s[%d]: Line too long",
1265 * Not doing continuations: check for edge
1272 while (*ptr && isspace((int) *ptr)) ptr++;
1274 if (!*ptr || (*ptr == '#')) continue;
1276 } else if (at_eof || (len == 0)) {
1277 radlog(L_ERR, "%s[%d]: Continuation at EOF is illegal",
1283 * See if there's a continuation.
1286 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
1291 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1292 cbuf[len - 1] = '\0';
1300 * The parser is getting to be evil.
1302 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
1304 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
1308 if (ptr[0] == '%') {
1309 hack = rad_copy_variable(buf1, ptr);
1311 hack = rad_copy_string(buf1, ptr);
1314 radlog(L_ERR, "%s[%d]: Invalid expansion: %s",
1315 filename, *lineno, ptr);
1322 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1329 radlog(L_ERR, "%s[%d]: Invalid expansion: %s",
1330 filename, *lineno, ptr);
1334 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1338 * The caller eats "name1 name2 {", and calls us
1339 * for the data inside of the section. So if we
1340 * receive a closing brace, then it must mean the
1341 * end of the section.
1343 if (t1 == T_RCBRACE) {
1344 if (this == current) {
1345 radlog(L_ERR, "%s[%d]: Too many closing braces",
1350 this = this->item.parent;
1355 * Allow for $INCLUDE files
1357 * This *SHOULD* work for any level include.
1358 * I really really really hate this file. -cparker
1360 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
1361 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
1364 t2 = getword(&ptr, buf2, sizeof(buf2));
1366 if (buf2[0] == '$') relative = 0;
1368 value = cf_expand_variables(filename, lineno, this, buf, buf2);
1369 if (!value) return -1;
1371 if (!FR_DIR_IS_RELATIVE(value)) relative = 0;
1374 value = cf_local_file(current, value, buf3,
1377 radlog(L_ERR, "%s[%d]: Directories too deep.",
1384 #ifdef HAVE_DIRENT_H
1388 * Include ALL non-"dot" files in the directory.
1391 if (value[strlen(value) - 1] == '/') {
1394 struct stat stat_buf;
1396 DEBUG2("including files in directory %s", value );
1397 dir = opendir(value);
1399 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1400 filename, *lineno, value,
1406 * Read the directory, ignoring "." files.
1408 while ((dp = readdir(dir)) != NULL) {
1411 if (dp->d_name[0] == '.') continue;
1414 * Check for valid characters
1416 for (p = dp->d_name; *p != '\0'; p++) {
1417 if (isalpha((int)*p) ||
1421 (*p == '.')) continue;
1424 if (*p != '\0') continue;
1426 snprintf(buf2, sizeof(buf2), "%s%s",
1428 if ((stat(buf2, &stat_buf) != 0) ||
1429 S_ISDIR(stat_buf.st_mode)) continue;
1431 * Read the file into the current
1432 * configuration sectoin.
1434 if (cf_file_include(buf2, this) < 0) {
1442 { /* it was a normal file */
1443 if (buf1[1] == '-') {
1444 struct stat statbuf;
1446 if (stat(value, &statbuf) < 0) {
1447 DEBUG("WARNING: Not including file %s: %s", value, strerror(errno));
1452 if (cf_file_include(value, this) < 0) {
1457 } /* we were in an include */
1459 if (strcasecmp(buf1, "$template") == 0) {
1461 CONF_SECTION *parentcs;
1462 t2 = getword(&ptr, buf2, sizeof(buf2));
1464 parentcs = cf_top_section(current);
1466 ci = cf_reference_item(parentcs, this, buf2);
1467 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1468 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
1469 filename, *lineno, buf2);
1473 if (this->template) {
1474 radlog(L_ERR, "%s[%d]: Section already has a template",
1479 this->template = cf_itemtosection(ci);
1484 * Ensure that the user can't add CONF_PAIRs
1485 * with 'internal' names;
1487 if (buf1[0] == '_') {
1488 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1489 filename, *lineno, buf1);
1494 * Grab the next token.
1496 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1510 if (!this || (strcmp(this->name1, "update") != 0)) {
1511 radlog(L_ERR, "%s[%d]: Invalid operator in assignment",
1519 t3 = getstring(&ptr, buf3, sizeof(buf3));
1522 * Handle variable substitution via ${foo}
1524 if ((t3 == T_BARE_WORD) ||
1525 (t3 == T_DOUBLE_QUOTED_STRING)) {
1526 value = cf_expand_variables(filename, lineno, this,
1528 if (!value) return -1;
1529 } else if ((t3 == T_EOL) ||
1537 * Add this CONF_PAIR to our CONF_SECTION
1539 cpn = cf_pair_alloc(buf1, value, t2, t3, this);
1540 cpn->item.filename = filename;
1541 cpn->item.lineno = *lineno;
1542 cf_item_add(this, cf_pairtoitem(cpn));
1546 * This horrible code is here to support
1547 * if/then/else failover in the
1548 * authorize, etc. sections. It makes no
1549 * sense anywhere else.
1552 if ((strcmp(buf1, "if") == 0) ||
1553 (strcmp(buf1, "elsif") == 0)) {
1554 const char *end = ptr;
1555 CONF_SECTION *server;
1557 if (!condition_looks_ok(&end)) {
1558 radlog(L_ERR, "%s[%d]: Parse error in condition at: %s",
1559 filename, *lineno, ptr);
1563 if ((size_t) (end - ptr) >= (sizeof(buf2) - 1)) {
1564 radlog(L_ERR, "%s[%d]: Statement too complicated after \"%s\"",
1565 filename, *lineno, buf1);
1570 * More sanity checking. This is
1571 * getting to be a horrible hack.
1575 if (strcmp(server->name1, "server") == 0) break;
1576 server = server->item.parent;
1580 radlog(L_ERR, "%s[%d]: Processing directives such as \"%s\" cannot be used here.",
1581 filename, *lineno, buf1);
1586 memcpy(buf2 + 1, ptr, end - ptr);
1587 buf2[end - ptr + 1] = '\0';
1593 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1594 filename, *lineno, buf1);
1601 * No '=', must be a section or sub-section.
1604 case T_DOUBLE_QUOTED_STRING:
1605 case T_SINGLE_QUOTED_STRING:
1606 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1607 if (t3 != T_LCBRACE) {
1608 radlog(L_ERR, "%s[%d]: Expecting section start brace '{' after \"%s %s\"",
1609 filename, *lineno, buf1, buf2);
1615 css = cf_section_alloc(buf1,
1616 t2 == T_LCBRACE ? NULL : buf2,
1619 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1623 cf_item_add(this, cf_sectiontoitem(css));
1624 css->item.filename = filename;
1625 css->item.lineno = *lineno;
1628 * The current section is now the child section.
1634 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1635 filename, *lineno, buf1);
1641 * See if EOF was unexpected ..
1643 if (feof(fp) && (this != current)) {
1644 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1646 cf_section_name1(this), cf_section_lineno(this));
1654 * Include one config file in another.
1656 int cf_file_include(const char *filename, CONF_SECTION *cs)
1660 struct stat statbuf;
1664 DEBUG2( "including configuration file %s", filename);
1666 if (stat(filename, &statbuf) == 0) {
1668 if ((statbuf.st_mode & S_IWOTH) != 0) {
1669 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1676 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1677 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1684 fp = fopen(filename, "r");
1686 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1687 filename, strerror(errno));
1692 * Add the filename to the section
1694 mtime = rad_malloc(sizeof(*mtime));
1695 *mtime = statbuf.st_mtime;
1697 if (cf_data_add_internal(cs, filename, mtime, free,
1698 PW_TYPE_FILENAME) < 0) {
1700 radlog(L_ERR|L_CONS, "Internal error open file \"%s\"",
1705 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILENAME);
1708 radlog(L_ERR|L_CONS, "Internal error open file \"%s\"",
1713 if (!cs->item.filename) cs->item.filename = filename;
1716 * Read the section. It's OK to have EOF without a
1717 * matching close brace.
1719 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
1729 * Bootstrap a config file.
1731 CONF_SECTION *cf_file_read(const char *filename)
1737 cs = cf_section_alloc("main", NULL, NULL);
1738 if (!cs) return NULL;
1740 cp = cf_pair_alloc("confdir", filename, T_OP_SET, T_BARE_WORD, cs);
1741 if (!cp) return NULL;
1743 p = strrchr(cp->value, FR_DIR_SEP);
1746 cp->item.filename = "internal";
1747 cp->item.lineno = 0;
1748 cf_item_add(cs, cf_pairtoitem(cp));
1750 if (cf_file_include(filename, cs) < 0) {
1751 cf_section_free(&cs);
1759 * Return a CONF_PAIR within a CONF_SECTION.
1761 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1764 CONF_PAIR *cp = NULL;
1766 if (!cs) return NULL;
1769 * Find the name in the tree, for speed.
1775 cp = rbtree_finddata(cs->pair_tree, &mycp);
1778 * Else find the first one that matches
1780 for (ci = cs->children; ci; ci = ci->next) {
1781 if (ci->type == CONF_ITEM_PAIR) {
1782 return cf_itemtopair(ci);
1787 if (cp || !cs->template) return cp;
1789 return cf_pair_find(cs->template, name);
1793 * Return the attr of a CONF_PAIR
1796 const char *cf_pair_attr(CONF_PAIR *pair)
1798 return (pair ? pair->attr : NULL);
1802 * Return the value of a CONF_PAIR
1805 const char *cf_pair_value(CONF_PAIR *pair)
1807 return (pair ? pair->value : NULL);
1811 * Copied here for error reporting.
1813 extern void fr_strerror_printf(const char *, ...);
1816 * Turn a CONF_PAIR into a VALUE_PAIR
1817 * For now, ignore the "value_type" field...
1819 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
1824 fr_strerror_printf("Internal error");
1829 fr_strerror_printf("No value given for attribute %s", pair->attr);
1834 * pairmake handles tags. pairalloc() doesn't.
1836 vp = pairmake(pair->attr, NULL, pair->operator);
1841 if (pair->value_type == T_BARE_WORD) {
1842 if ((vp->type == PW_TYPE_STRING) &&
1843 (pair->value[0] == '0') && (pair->value[1] == 'x')) {
1844 vp->type = PW_TYPE_OCTETS;
1846 if (!pairparsevalue(vp, pair->value)) {
1850 vp->flags.do_xlat = 0;
1852 } else if (pair->value_type == T_SINGLE_QUOTED_STRING) {
1853 if (!pairparsevalue(vp, pair->value)) {
1857 vp->flags.do_xlat = 0;
1859 vp->flags.do_xlat = 1;
1866 * Return the first label of a CONF_SECTION
1869 const char *cf_section_name1(const CONF_SECTION *cs)
1871 return (cs ? cs->name1 : NULL);
1875 * Return the second label of a CONF_SECTION
1878 const char *cf_section_name2(const CONF_SECTION *cs)
1880 return (cs ? cs->name2 : NULL);
1884 * Find a value in a CONF_SECTION
1886 const char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1890 cp = cf_pair_find(cs, attr);
1892 return (cp ? cp->value : NULL);
1896 * Return the next pair after a CONF_PAIR
1897 * with a certain name (char *attr) If the requested
1898 * attr is NULL, any attr matches.
1901 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1902 CONF_PAIR *pair, const char *attr)
1907 * If pair is NULL this must be a first time run
1908 * Find the pair with correct name
1912 return cf_pair_find(cs, attr);
1915 ci = cf_pairtoitem(pair)->next;
1917 for (; ci; ci = ci->next) {
1918 if (ci->type != CONF_ITEM_PAIR)
1920 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1924 return cf_itemtopair(ci);
1928 * Find a CONF_SECTION, or return the root if name is NULL
1931 CONF_SECTION *cf_section_find(const char *name)
1934 return cf_section_sub_find(mainconfig.config, name);
1936 return mainconfig.config;
1940 * Find a sub-section in a section
1943 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1947 if (!name) return NULL; /* can't find an un-named section */
1950 * Do the fast lookup if possible.
1952 if (cs->section_tree) {
1957 return rbtree_finddata(cs->section_tree, &mycs);
1960 for (ci = cs->children; ci; ci = ci->next) {
1961 if (ci->type != CONF_ITEM_SECTION)
1963 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1967 return cf_itemtosection(ci);
1973 * Find a CONF_SECTION with both names.
1975 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1976 const char *name1, const char *name2)
1980 if (!cs) cs = mainconfig.config;
1982 if (name1 && (cs->section_tree)) {
1983 CONF_SECTION mycs, *master_cs;
1988 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1990 return rbtree_finddata(master_cs->name2_tree, &mycs);
1995 * Else do it the old-fashioned way.
1997 for (ci = cs->children; ci; ci = ci->next) {
1998 CONF_SECTION *subcs;
2000 if (ci->type != CONF_ITEM_SECTION)
2003 subcs = cf_itemtosection(ci);
2005 if (!subcs->name2) {
2006 if (strcmp(subcs->name1, name2) == 0) break;
2008 if (strcmp(subcs->name2, name2) == 0) break;
2010 continue; /* don't do the string comparisons below */
2013 if ((strcmp(subcs->name1, name1) == 0) &&
2014 (subcs->name2 != NULL) &&
2015 (strcmp(subcs->name2, name2) == 0))
2019 return cf_itemtosection(ci);
2023 * Return the next subsection after a CONF_SECTION
2024 * with a certain name1 (char *name1). If the requested
2025 * name1 is NULL, any name1 matches.
2028 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
2029 CONF_SECTION *subsection,
2035 * If subsection is NULL this must be a first time run
2036 * Find the subsection with correct name
2039 if (subsection == NULL){
2040 ci = section->children;
2042 ci = cf_sectiontoitem(subsection)->next;
2045 for (; ci; ci = ci->next) {
2046 if (ci->type != CONF_ITEM_SECTION)
2048 if ((name1 == NULL) ||
2049 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
2053 return cf_itemtosection(ci);
2058 * Return the next section after a CONF_SECTION
2059 * with a certain name1 (char *name1). If the requested
2060 * name1 is NULL, any name1 matches.
2063 CONF_SECTION *cf_section_find_next(CONF_SECTION *section,
2064 CONF_SECTION *subsection,
2067 if (!section->item.parent) return NULL;
2069 return cf_subsection_find_next(section->item.parent, subsection, name1);
2073 * Return the next item after a CONF_ITEM.
2076 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
2079 * If item is NULL this must be a first time run
2080 * Return the first item
2084 return section->children;
2090 int cf_section_lineno(CONF_SECTION *section)
2092 return cf_sectiontoitem(section)->lineno;
2095 const char *cf_pair_filename(CONF_PAIR *pair)
2097 return cf_pairtoitem(pair)->filename;
2100 const char *cf_section_filename(CONF_SECTION *section)
2102 return cf_sectiontoitem(section)->filename;
2105 int cf_pair_lineno(CONF_PAIR *pair)
2107 return cf_pairtoitem(pair)->lineno;
2110 int cf_item_is_section(CONF_ITEM *item)
2112 return item->type == CONF_ITEM_SECTION;
2114 int cf_item_is_pair(CONF_ITEM *item)
2116 return item->type == CONF_ITEM_PAIR;
2120 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
2121 void *data, void (*data_free)(void *))
2127 name_len = strlen(name) + 1;
2129 p = rad_malloc(sizeof(*cd) + name_len);
2130 cd = (CONF_DATA *) p;
2131 memset(cd, 0, sizeof(*cd));
2133 cd->item.type = CONF_ITEM_DATA;
2134 cd->item.parent = parent;
2136 cd->free = data_free;
2139 memcpy(p, name, name_len);
2145 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
2148 if (!cs || !name) return NULL;
2151 * Find the name in the tree, for speed.
2153 if (cs->data_tree) {
2158 return rbtree_finddata(cs->data_tree, &mycd);
2165 * Find data from a particular section.
2167 void *cf_data_find(CONF_SECTION *cs, const char *name)
2169 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
2171 if (cd) return cd->data;
2177 * Add named data to a configuration section.
2179 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
2180 void *data, void (*data_free)(void *),
2185 if (!cs || !name) return -1;
2188 * Already exists. Can't add it.
2190 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
2192 cd = cf_data_alloc(cs, name, data, data_free);
2196 cf_item_add(cs, cf_datatoitem(cd));
2202 * Add named data to a configuration section.
2204 int cf_data_add(CONF_SECTION *cs, const char *name,
2205 void *data, void (*data_free)(void *))
2207 return cf_data_add_internal(cs, name, data, data_free, 0);
2212 * Copy CONF_DATA from src to dst
2214 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
2217 CONF_ITEM *cd, *next, **last;
2220 * Don't check if s->data_tree is NULL. It's child
2221 * sections may have data, even if this section doesn't.
2224 rad_assert(d->data_tree == NULL);
2225 d->data_tree = s->data_tree;
2226 s->data_tree = NULL;
2229 * Walk through src, moving CONF_ITEM_DATA
2232 last = &(s->children);
2233 for (cd = s->children; cd != NULL; cd = next) {
2237 * Recursively copy data from child sections.
2239 if (cd->type == CONF_ITEM_SECTION) {
2240 CONF_SECTION *s1, *d1;
2242 s1 = cf_itemtosection(cd);
2243 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
2245 cf_section_copy_data(s1, d1);
2252 * Not conf data, remember last ptr.
2254 if (cd->type != CONF_ITEM_DATA) {
2260 * Remove it from the src list
2266 * Add it to the dst list
2269 rad_assert(d->tail == NULL);
2272 rad_assert(d->tail != NULL);
2280 * For a CONF_DATA element, stat the filename, if necessary.
2282 static int filename_stat(void *context, void *data)
2285 CONF_DATA *cd = data;
2287 context = context; /* -Wunused */
2289 if (cd->flag != PW_TYPE_FILENAME) return 0;
2291 if (stat(cd->name, &buf) < 0) return -1;
2293 if (buf.st_mtime != *(time_t *) cd->data) return -1;
2300 * Compare two CONF_SECTIONS. The items MUST be in the same
2303 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
2305 CONF_ITEM *ca = a->children;
2306 CONF_ITEM *cb = b->children;
2314 if (!ca && !cb) break;
2319 if (ca && ca->type == CONF_ITEM_DATA) {
2323 if (cb && cb->type == CONF_ITEM_DATA) {
2329 * One is smaller than the other. Exit.
2331 if (!ca || !cb) return 0;
2333 if (ca->type != cb->type) return 0;
2336 * Deal with subsections.
2338 if (ca->type == CONF_ITEM_SECTION) {
2339 CONF_SECTION *sa = cf_itemtosection(ca);
2340 CONF_SECTION *sb = cf_itemtosection(cb);
2342 if (!cf_section_cmp(sa, sb)) return 0;
2346 rad_assert(ca->type == CONF_ITEM_PAIR);
2348 pa = cf_itemtopair(ca);
2349 pb = cf_itemtopair(cb);
2352 * Different attr and/or value, Exit.
2354 if ((strcmp(pa->attr, pb->attr) != 0) ||
2355 (strcmp(pa->value, pb->value) != 0)) return 0;
2359 * And go to the next element.
2367 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
2370 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
2375 * They must be the same, say so.
2382 * Migrate CONF_DATA from one section to another.
2384 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
2387 CONF_SECTION *s, *d;
2389 for (ci = src->children; ci != NULL; ci = ci->next) {
2390 if (ci->type != CONF_ITEM_SECTION)
2393 s = cf_itemtosection(ci);
2394 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
2396 if (!d) continue; /* not in new one, don't migrate it */
2399 * A section of the same name is in BOTH src & dst,
2400 * compare the CONF_PAIR's. If they're all the same,
2401 * then copy the CONF_DATA from one to the other.
2403 if (cf_section_cmp(s, d)) {
2404 cf_section_copy_data(s, d);
2408 return 1; /* rcode means anything? */
2412 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
2414 if (!cs || !template || cs->template || template->template) return -1;
2416 cs->template = template;
2423 * This is here to make the rest of the code easier to read. It
2424 * ties conffile.c to log.c, but it means we don't have to
2425 * pollute every other function with the knowledge of the
2426 * configuration internals.
2428 void cf_log_err(CONF_ITEM *ci, const char *fmt, ...)
2434 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2437 radlog(L_ERR, "%s[%d]: %s", ci->filename, ci->lineno, buffer);
2441 void cf_log_info(CONF_SECTION *cs, const char *fmt, ...)
2446 if (debug_flag > 1 && cf_log_config && cs) vradlog(L_DBG, fmt, ap);
2451 * Wrapper to simplify the code.
2453 void cf_log_module(CONF_SECTION *cs, const char *fmt, ...)
2459 if (debug_flag > 1 && cf_log_modules && cs) {
2460 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2462 radlog(L_DBG, " Module: %s", buffer);
2467 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
2469 if (!cs) return NULL;
2471 return cs->variables;
2476 * JMG dump_config tries to dump the config structure in a readable format
2480 static int dump_config_section(CONF_SECTION *cs, int indent)
2486 /* The DEBUG macro doesn't let me
2487 * for(i=0;i<indent;++i) debugputchar('\t');
2488 * so I had to get creative. --Pac. */
2490 for (ci = cs->children; ci; ci = ci->next) {
2492 case CONF_ITEM_PAIR:
2493 cp=cf_itemtopair(ci);
2494 DEBUG("%.*s%s = %s",
2495 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2496 cp->attr, cp->value);
2499 case CONF_ITEM_SECTION:
2500 scs=cf_itemtosection(ci);
2501 DEBUG("%.*s%s %s%s{",
2502 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2504 scs->name2 ? scs->name2 : "",
2505 scs->name2 ? " " : "");
2506 dump_config_section(scs, indent+1);
2508 indent, "\t\t\t\t\t\t\t\t\t\t\t");
2511 default: /* FIXME: Do more! */
2519 int dump_config(CONF_SECTION *cs)
2521 return dump_config_section(cs, 0);
2525 static const char *cf_pair_print_value(const CONF_PAIR *cp,
2526 char *buffer, size_t buflen)
2530 if (!cp->value) return "";
2532 switch (cp->value_type) {
2535 snprintf(buffer, buflen, "%s", cp->value);
2538 case T_SINGLE_QUOTED_STRING:
2539 snprintf(buffer, buflen, "'%s'", cp->value);
2542 case T_DOUBLE_QUOTED_STRING:
2544 fr_print_string(cp->value, strlen(cp->value),
2545 buffer + 1, buflen - 3);
2546 p = buffer + strlen(buffer); /* yuck... */
2556 int cf_pair2xml(FILE *fp, CONF_PAIR *cp)
2558 fprintf(fp, "<%s>", cp->attr);
2563 const char *q = cp->value;
2565 while (*q && (p < (buffer + sizeof(buffer)))) {
2567 memcpy(p, "&", 4);
2570 } else if (q[0] == '<') {
2571 memcpy(p, "<", 4);
2574 } else if (q[0] == '>') {
2575 memcpy(p, ">", 4);
2585 fprintf(fp, "%s", buffer);
2588 fprintf(fp, "</%s>\n", cp->attr);
2593 int cf_section2xml(FILE *fp, CONF_SECTION *cs)
2595 CONF_ITEM *ci, *next;
2600 fprintf(fp, "<%s>\n", cs->name1);
2602 fprintf(fp, "<_name2>%s</_name2>\n", cs->name2);
2606 * Loop over contents.
2608 for (ci = cs->children; ci; ci = next) {
2612 case CONF_ITEM_PAIR:
2613 if (!cf_pair2xml(fp, (CONF_PAIR *) ci)) return 0;
2616 case CONF_ITEM_SECTION:
2617 if (!cf_section2xml(fp, (CONF_SECTION *) ci)) return 0;
2620 default: /* should really be an error. */
2626 fprintf(fp, "</%s>\n", cs->name1);
2628 return 1; /* success */
2631 int cf_pair2file(FILE *fp, CONF_PAIR *cp)
2635 fprintf(fp, "\t%s = %s\n", cp->attr,
2636 cf_pair_print_value(cp, buffer, sizeof(buffer)));
2641 int cf_section2file(FILE *fp, CONF_SECTION *cs)
2643 CONF_ITEM *ci, *next;
2649 fprintf(fp, "%s {\n", cs->name1);
2651 fprintf(fp, "%s %s {\n",
2652 cs->name1, cs->name2);
2656 * Loop over contents.
2658 for (ci = cs->children; ci; ci = next) {
2662 case CONF_ITEM_PAIR:
2663 if (!cf_pair2file(fp, (CONF_PAIR *) ci)) return 0;
2666 case CONF_ITEM_SECTION:
2667 if (!cf_section2file(fp, (CONF_SECTION *) ci)) return 0;
2670 default: /* should really be an error. */
2678 return 1; /* success */