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)
492 if (!cs || !ci) return;
495 rad_assert(cs->tail == NULL);
498 rad_assert(cs->tail != NULL);
503 * Update the trees (and tail) for each item added.
505 for (/* nothing */; ci != NULL; ci = ci->next) {
509 * For fast lookups, pair's and sections get
514 rbtree_insert(cs->pair_tree, ci);
517 case CONF_ITEM_SECTION: {
518 CONF_SECTION *cs_new = cf_itemtosection(ci);
520 if (!cs->section_tree) {
521 cs->section_tree = rbtree_create(section_cmp, NULL, 0);
522 if (!cs->section_tree) {
523 radlog(L_ERR, "Out of memory");
528 rbtree_insert(cs->section_tree, cs_new);
531 * Two names: find the named instance.
534 CONF_SECTION *old_cs;
538 * CONF_SECTION having
539 * the given name1, and
543 old_cs = rbtree_finddata(cs->section_tree, cs_new);
544 if (!old_cs) return; /* this is a bad error! */
546 if (!old_cs->name2_tree) {
547 old_cs->name2_tree = rbtree_create(name2_cmp,
550 if (old_cs->name2_tree) {
551 rbtree_insert(old_cs->name2_tree, cs_new);
555 } /* was a section */
558 if (!cs->data_tree) {
559 cs->data_tree = rbtree_create(data_cmp, NULL, 0);
562 rbtree_insert(cs->data_tree, ci);
566 default: /* FIXME: assert & error! */
569 } /* switch over conf types */
574 CONF_ITEM *cf_reference_item(const CONF_SECTION *parentcs,
575 CONF_SECTION *outercs,
580 const CONF_SECTION *cs = outercs;
584 strlcpy(name, ptr, sizeof(name));
588 * ".foo" means "foo from the current section"
594 * ..foo means "foo from the section
595 * enclosing this section" (etc.)
599 cs = cs->item.parent;
604 * "foo.bar.baz" means "from the root"
606 } else if (strchr(p, '.') != NULL) {
607 if (!parentcs) goto no_such_item;
619 if (r && q > r) q = NULL;
620 if (q && q < r) r = NULL;
626 q = strchr(r + 1, ']');
627 if (!q) return NULL; /* parse error */
630 * Points to foo[bar]xx: parse error,
631 * it should be foo[bar] or foo[bar].baz
633 if (q[1] && q[1] != '.') goto no_such_item;
637 next = cf_section_sub_find_name2(cs, p, r + 1);
642 * Points to a named instance of a section.
645 if (!next) goto no_such_item;
646 return cf_sectiontoitem(next);
649 q++; /* ensure we skip the ']' and '.' */
653 next = cf_section_sub_find(cs, p);
657 if (!next) break; /* it MAY be a pair in this section! */
663 if (!*p) goto no_such_item;
667 * Find it in the current referenced
670 cp = cf_pair_find(cs, p);
671 if (cp) return cf_pairtoitem(cp);
673 next = cf_section_sub_find(cs, p);
674 if (next) return cf_sectiontoitem(next);
677 * "foo" is "in the current section, OR in main".
679 if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
685 DEBUG2("WARNING: No such configuration item %s", ptr);
690 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
692 if (!cs) return NULL;
694 while (cs->item.parent != NULL) {
695 cs = cs->item.parent;
703 * Expand the variables in an input string.
705 static const char *cf_expand_variables(const char *cf, int *lineno,
706 CONF_SECTION *outercs,
707 char *output, size_t outsize,
711 const char *end, *ptr;
712 const CONF_SECTION *parentcs;
716 * Find the master parent conf section.
717 * We can't use mainconfig.config, because we're in the
718 * process of re-building it, and it isn't set up yet...
720 parentcs = cf_top_section(outercs);
726 * Ignore anything other than "${"
728 if ((*ptr == '$') && (ptr[1] == '{')) {
733 * FIXME: Add support for ${foo:-bar},
738 * Look for trailing '}', and log a
739 * warning for anything that doesn't match,
740 * and exit with a fatal error.
742 end = strchr(ptr, '}');
745 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
753 * Can't really happen because input lines are
754 * capped at 8k, which is sizeof(name)
756 if ((size_t) (end - ptr) >= sizeof(name)) {
757 radlog(L_ERR, "%s[%d]: Reference string is too large",
762 memcpy(name, ptr, end - ptr);
763 name[end - ptr] = '\0';
765 ci = cf_reference_item(parentcs, outercs, name);
766 if (!ci || (ci->type != CONF_ITEM_PAIR)) {
767 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
773 * Substitute the value of the variable.
775 cp = cf_itemtopair(ci);
777 radlog(L_ERR, "%s[%d]: Reference \"%s\" has no value",
782 if (p + strlen(cp->value) >= output + outsize) {
783 radlog(L_ERR, "%s[%d]: Reference \"%s\" is too long",
788 strcpy(p, cp->value);
792 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
798 * Look for trailing '}', and log a
799 * warning for anything that doesn't match,
800 * and exit with a fatal error.
802 end = strchr(ptr, '}');
805 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
811 * Can't really happen because input lines are
812 * capped at 8k, which is sizeof(name)
814 if ((size_t) (end - ptr) >= sizeof(name)) {
815 radlog(L_ERR, "%s[%d]: Environment variable name is too large",
820 memcpy(name, ptr, end - ptr);
821 name[end - ptr] = '\0';
824 * Get the environment variable.
825 * If none exists, then make it an empty string.
833 if (p + strlen(env) >= output + outsize) {
834 radlog(L_ERR, "%s[%d]: Reference \"%s\" is too long",
845 * Copy it over verbatim.
850 if (p >= (output + outsize)) {
851 radlog(L_ERR, "%s[%d]: Reference \"%s\" is too long",
855 } /* loop over all of the input string. */
864 * Parses an item (not a CONF_ITEM) into the specified format,
865 * with a default value.
867 * Returns -1 on error, 0 for correctly parsed, and 1 if the
868 * default value was used. Note that the default value will be
869 * used ONLY if the CONF_PAIR is NULL.
871 int cf_item_parse(CONF_SECTION *cs, const char *name,
872 int type, void *data, const char *dflt)
878 const CONF_PAIR *cp = NULL;
881 if (cs) cp = cf_pair_find(cs, name);
886 return 1; /* nothing to parse, return default value */
898 case PW_TYPE_BOOLEAN:
900 * Allow yes/no and on/off
902 if ((strcasecmp(value, "yes") == 0) ||
903 (strcasecmp(value, "on") == 0)) {
905 } else if ((strcasecmp(value, "no") == 0) ||
906 (strcasecmp(value, "off") == 0)) {
910 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
913 cf_log_info(cs, "\t%s = %s", name, value);
916 case PW_TYPE_INTEGER:
917 *(int *)data = strtol(value, 0, 0);
918 cf_log_info(cs, "\t%s = %d", name, *(int *)data);
921 case PW_TYPE_STRING_PTR:
928 * Expand variables which haven't already been
929 * expanded automagically when the configuration
937 if (cs) lineno = cs->item.lineno;
940 * FIXME: sizeof(buffer)?
942 value = cf_expand_variables("<internal>",
944 cs, buffer, sizeof(buffer),
947 cf_log_err(cf_sectiontoitem(cs),"Failed expanding variable %s", name);
952 cf_log_info(cs, "\t%s = \"%s\"", name, value ? value : "(null)");
953 *q = value ? strdup(value) : NULL;
957 * This is the same as PW_TYPE_STRING_PTR,
958 * except that we also "stat" the file, and
961 case PW_TYPE_FILENAME:
968 * Expand variables which haven't already been
969 * expanded automagically when the configuration
977 if (cs) lineno = cs->item.lineno;
980 * FIXME: sizeof(buffer)?
982 value = cf_expand_variables("?",
984 cs, buffer, sizeof(buffer),
986 if (!value) return -1;
989 cf_log_info(cs, "\t%s = \"%s\"", name, value ? value : "(null)");
990 *q = value ? strdup(value) : NULL;
993 * And now we "stat" the file.
995 * FIXME: This appears to leak memory on exit,
996 * and we don't use this information. So it's
997 * commented out for now.
1002 if (stat(*q, &buf) == 0) {
1005 mtime = rad_malloc(sizeof(*mtime));
1006 *mtime = buf.st_mtime;
1008 cf_data_add_internal(cs, *q, mtime, free,
1014 case PW_TYPE_IPADDR:
1016 * Allow '*' as any address
1018 if (strcmp(value, "*") == 0) {
1019 *(uint32_t *) data = htonl(INADDR_ANY);
1020 cf_log_info(cs, "\t%s = *", name);
1023 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
1024 radlog(L_ERR, "Can't find IP address for host %s", value);
1028 if (strspn(value, "0123456789.") == strlen(value)) {
1029 cf_log_info(cs, "\t%s = %s", name, value);
1031 cf_log_info(cs, "\t%s = %s IP address [%s]", name, value,
1032 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
1034 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
1037 case PW_TYPE_IPV6ADDR:
1038 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
1039 radlog(L_ERR, "Can't find IPv6 address for host %s", value);
1042 cf_log_info(cs, "\t%s = %s IPv6 address [%s]", name, value,
1043 ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
1044 memcpy(data, &ipaddr.ipaddr.ip6addr,
1045 sizeof(ipaddr.ipaddr.ip6addr));
1049 radlog(L_ERR, "type %d not supported yet", type);
1051 } /* switch over variable type */
1056 cpn = cf_pair_alloc(name, value, T_OP_SET, T_BARE_WORD, cs);
1057 if (!cpn) return -1;
1058 cpn->item.filename = "<internal>";
1059 cpn->item.lineno = 0;
1060 cf_item_add(cs, cf_pairtoitem(cpn));
1066 static const char *parse_spaces = " ";
1069 * A copy of cf_section_parse that initializes pointers before
1072 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1073 const CONF_PARSER *variables)
1078 for (i = 0; variables[i].name != NULL; i++) {
1079 if (variables[i].type == PW_TYPE_SUBSECTION) {
1080 CONF_SECTION *subcs;
1081 subcs = cf_section_sub_find(cs, variables[i].name);
1082 if (!subcs) continue;
1084 if (!variables[i].dflt) continue;
1086 cf_section_parse_init(subcs, base,
1087 (const CONF_PARSER *) variables[i].dflt);
1091 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
1092 (variables[i].type != PW_TYPE_FILENAME)) {
1096 if (variables[i].data) {
1097 data = variables[i].data; /* prefer this. */
1099 data = ((char *)base) + variables[i].offset;
1104 *(char **) data = NULL;
1105 } /* for all variables in the configuration section */
1109 * Parse a configuration section into user-supplied variables.
1111 int cf_section_parse(CONF_SECTION *cs, void *base,
1112 const CONF_PARSER *variables)
1117 cs->variables = variables; /* this doesn't hurt anything */
1120 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1123 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1124 cs->name1, cs->name2);
1127 cf_section_parse_init(cs, base, variables);
1130 * Handle the known configuration parameters.
1132 for (i = 0; variables[i].name != NULL; i++) {
1134 * Handle subsections specially
1136 if (variables[i].type == PW_TYPE_SUBSECTION) {
1137 CONF_SECTION *subcs;
1138 subcs = cf_section_sub_find(cs, variables[i].name);
1141 * If the configuration section is NOT there,
1144 * FIXME! This is probably wrong... we should
1145 * probably set the items to their default values.
1147 if (!subcs) continue;
1149 if (!variables[i].dflt) {
1150 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
1154 if (cf_section_parse(subcs, base,
1155 (const CONF_PARSER *) variables[i].dflt) < 0) {
1159 } /* else it's a CONF_PAIR */
1161 if (variables[i].data) {
1162 data = variables[i].data; /* prefer this. */
1164 data = ((char *)base) + variables[i].offset;
1166 DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1171 * Parse the pair we found, or a default value.
1173 if (cf_item_parse(cs, variables[i].name, variables[i].type,
1174 data, variables[i].dflt) < 0) {
1177 } /* for all variables in the configuration section */
1179 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1186 cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1187 cf_section_parse_free(cs, base);
1193 * Sanity check the "if" or "elsif", presuming that the first '('
1194 * has already been eaten.
1196 * We're not really parsing it here, just checking if it's mostly
1199 static int condition_looks_ok(const char **ptr)
1203 const char *p = *ptr;
1215 return 0; /* no trailing slash */
1227 return 0; /* no trailing slash */
1238 if (num_braces == 1) {
1239 const char *q = p + 1;
1242 * Validate that there isn't much
1243 * else after the closing brace.
1245 while ((*q == ' ') || (*q == '\t')) q++;
1251 DEBUG2("Expected open brace '{' after condition at %s", p);
1255 *ptr = p + 1; /* include the trailing ')' */
1275 DEBUG3("Unexpected error");
1279 int cf_exclude_file(const char *filename)
1283 const char *p = filename;
1286 * FIXME: Maybe later make this a globally set configuration
1287 * variable. But that's low priority.
1289 static const char *excluded[] = {
1290 "rpmsave", "rpmnew", "dpkg-new", "dpkg-dist", "dpkg-old",
1294 if (!p || !*p) return TRUE; /* coding error */
1296 if (*p == '.') return TRUE; /* ".", "..", ".foo", ... */
1298 if (*p == '#') return TRUE; /* #foo# */
1301 if (p[len - 1] == '~') return TRUE; /* foo~ */
1303 p = strrchr(p, '.');
1304 if (!p) return FALSE; /* just "foo", it's OK */
1307 for (i = 0; excluded[i] != NULL; i++) {
1308 if (strcmp(p, excluded[i]) == 0) return TRUE;
1315 static const char *cf_local_file(CONF_SECTION *cs, const char *local,
1316 char *buffer, size_t bufsize)
1320 CONF_SECTION *parentcs = cf_top_section(cs);
1322 p = strrchr(parentcs->item.filename, FR_DIR_SEP);
1323 if (!p) return local;
1325 dirsize = (p - parentcs->item.filename) + 1;
1327 if ((dirsize + strlen(local)) >= bufsize) {
1331 memcpy(buffer, parentcs->item.filename, dirsize);
1332 strlcpy(buffer + dirsize, local, bufsize - dirsize);
1337 static int seen_too_much(const char *filename, int lineno, const char *ptr)
1340 if (isspace(*ptr)) {
1345 if (*ptr == '#') return FALSE;
1351 radlog(L_ERR, "%s[%d] Unexpected text %s. See \"man unlang\"",
1352 filename, lineno, ptr);
1361 * Read a part of the config file.
1363 static int cf_section_read(const char *filename, int *lineno, FILE *fp,
1364 CONF_SECTION *current)
1367 CONF_SECTION *this, *css;
1379 this = current; /* add items here */
1382 * Read, checking for line continuations ('\\' at EOL)
1388 * Get data, and remember if we are at EOF.
1390 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1394 * We read the entire 8k worth of data: complain.
1395 * Note that we don't care if the last character
1396 * is \n: it's still forbidden. This means that
1397 * the maximum allowed length of text is 8k-1, which
1401 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
1402 radlog(L_ERR, "%s[%d]: Line too long",
1408 * Not doing continuations: check for edge
1415 while (*ptr && isspace((int) *ptr)) ptr++;
1417 if (!*ptr || (*ptr == '#')) continue;
1419 } else if (at_eof || (len == 0)) {
1420 radlog(L_ERR, "%s[%d]: Continuation at EOF is illegal",
1426 * See if there's a continuation.
1429 ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
1434 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1435 cbuf[len - 1] = '\0';
1443 * The parser is getting to be evil.
1445 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
1447 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
1451 if (ptr[0] == '%') {
1452 hack = rad_copy_variable(buf1, ptr);
1454 hack = rad_copy_string(buf1, ptr);
1457 radlog(L_ERR, "%s[%d]: Invalid expansion: %s",
1458 filename, *lineno, ptr);
1465 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1472 radlog(L_ERR, "%s[%d]: Invalid expansion: %s",
1473 filename, *lineno, ptr);
1477 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1481 * The caller eats "name1 name2 {", and calls us
1482 * for the data inside of the section. So if we
1483 * receive a closing brace, then it must mean the
1484 * end of the section.
1486 if (t1 == T_RCBRACE) {
1487 if (this == current) {
1488 radlog(L_ERR, "%s[%d]: Too many closing braces",
1493 this = this->item.parent;
1494 if (seen_too_much(filename, *lineno, ptr)) return -1;
1499 * Allow for $INCLUDE files
1501 * This *SHOULD* work for any level include.
1502 * I really really really hate this file. -cparker
1504 if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
1505 (strcasecmp(buf1, "$-INCLUDE") == 0)) {
1508 t2 = getword(&ptr, buf2, sizeof(buf2));
1510 if (buf2[0] == '$') relative = 0;
1512 value = cf_expand_variables(filename, lineno, this, buf, sizeof(buf), buf2);
1513 if (!value) return -1;
1515 if (!FR_DIR_IS_RELATIVE(value)) relative = 0;
1518 value = cf_local_file(current, value, buf3,
1521 radlog(L_ERR, "%s[%d]: Directories too deep.",
1528 #ifdef HAVE_DIRENT_H
1532 * Include ALL non-"dot" files in the directory.
1535 if (value[strlen(value) - 1] == '/') {
1538 struct stat stat_buf;
1540 DEBUG2("including files in directory %s", value );
1545 if (stat(value, &stat_buf) < 0) {
1546 radlog(L_ERR, "%s[%d]: Failed reading directory %s: %s",
1548 value, strerror(errno));
1552 if ((stat_buf.st_mode & S_IWOTH) != 0) {
1553 radlog(L_ERR|L_CONS, "%s[%d]: Directory %s is globally writable. Refusing to start due to insecure configuration.",
1554 filename, *lineno, value);
1558 dir = opendir(value);
1560 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1561 filename, *lineno, value,
1567 * Read the directory, ignoring some files.
1569 while ((dp = readdir(dir)) != NULL) {
1570 if (cf_exclude_file(dp->d_name))
1573 snprintf(buf2, sizeof(buf2), "%s%s",
1575 if ((stat(buf2, &stat_buf) != 0) ||
1576 S_ISDIR(stat_buf.st_mode)) continue;
1578 * Read the file into the current
1579 * configuration sectoin.
1581 if (cf_file_include(buf2, this) < 0) {
1589 { /* it was a normal file */
1590 if (buf1[1] == '-') {
1591 struct stat statbuf;
1593 if (stat(value, &statbuf) < 0) {
1594 DEBUG("WARNING: Not including file %s: %s", value, strerror(errno));
1599 if (cf_file_include(value, this) < 0) {
1604 } /* we were in an include */
1606 if (strcasecmp(buf1, "$template") == 0) {
1608 CONF_SECTION *parentcs, *templatecs;
1609 t2 = getword(&ptr, buf2, sizeof(buf2));
1611 parentcs = cf_top_section(current);
1613 templatecs = cf_section_sub_find(parentcs, "templates");
1615 radlog(L_ERR, "%s[%d]: No \"templates\" section for reference \"%s\"",
1616 filename, *lineno, buf2);
1620 ci = cf_reference_item(parentcs, templatecs, buf2);
1621 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1622 radlog(L_ERR, "%s[%d]: Reference \"%s\" not found",
1623 filename, *lineno, buf2);
1627 if (this->template) {
1628 radlog(L_ERR, "%s[%d]: Section already has a template",
1633 this->template = cf_itemtosection(ci);
1638 * Ensure that the user can't add CONF_PAIRs
1639 * with 'internal' names;
1641 if (buf1[0] == '_') {
1642 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1643 filename, *lineno, buf1);
1648 * Grab the next token.
1650 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1665 case T_OP_CMP_FALSE:
1666 if (!this || (strcmp(this->name1, "update") != 0)) {
1667 radlog(L_ERR, "%s[%d]: Invalid operator in assignment",
1675 t3 = getstring(&ptr, buf3, sizeof(buf3));
1676 if (t3 == T_OP_INVALID) {
1677 radlog(L_ERR, "%s[%d]: Parse error: %s",
1684 * These are not allowed. Print a
1685 * helpful error message.
1687 if ((t3 == T_BACK_QUOTED_STRING) &&
1688 (!this || (strcmp(this->name1, "update") != 0))) {
1689 radlog(L_ERR, "%s[%d]: Syntax error: Invalid string `...` in assignment",
1695 * Handle variable substitution via ${foo}
1697 if ((t3 == T_BARE_WORD) ||
1698 (t3 == T_DOUBLE_QUOTED_STRING)) {
1699 value = cf_expand_variables(filename, lineno, this,
1700 buf, sizeof(buf), buf3);
1701 if (!value) return -1;
1702 } else if ((t3 == T_EOL) ||
1710 * Add this CONF_PAIR to our CONF_SECTION
1713 cpn = cf_pair_alloc(buf1, value, t2, t3, this);
1714 cpn->item.filename = filename;
1715 cpn->item.lineno = *lineno;
1716 cf_item_add(this, cf_pairtoitem(cpn));
1720 * This horrible code is here to support
1721 * if/then/else failover in the
1722 * authorize, etc. sections. It makes no
1723 * sense anywhere else.
1726 if ((strcmp(buf1, "if") == 0) ||
1727 (strcmp(buf1, "elsif") == 0)) {
1728 const char *end = ptr;
1729 CONF_SECTION *server;
1731 if (!condition_looks_ok(&end)) {
1732 radlog(L_ERR, "%s[%d]: Parse error in condition at: %s",
1733 filename, *lineno, ptr);
1737 if ((size_t) (end - ptr) >= (sizeof(buf2) - 1)) {
1738 radlog(L_ERR, "%s[%d]: Statement too complicated after \"%s\"",
1739 filename, *lineno, buf1);
1744 * More sanity checking. This is
1745 * getting to be a horrible hack.
1749 if (strcmp(server->name1, "server") == 0) break;
1750 server = server->item.parent;
1754 radlog(L_ERR, "%s[%d]: Processing directives such as \"%s\" cannot be used here.",
1755 filename, *lineno, buf1);
1760 memcpy(buf2 + 1, ptr, end - ptr);
1761 buf2[end - ptr + 1] = '\0';
1765 if (gettoken(&ptr, buf3, sizeof(buf3)) != T_LCBRACE) {
1766 radlog(L_ERR, "%s[%d]: Expected '{'",
1773 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1774 filename, *lineno, buf1);
1781 * No '=', must be a section or sub-section.
1784 case T_DOUBLE_QUOTED_STRING:
1785 case T_SINGLE_QUOTED_STRING:
1786 t3 = gettoken(&ptr, buf3, sizeof(buf3));
1787 if (t3 != T_LCBRACE) {
1788 radlog(L_ERR, "%s[%d]: Expecting section start brace '{' after \"%s %s\"",
1789 filename, *lineno, buf1, buf2);
1796 if (seen_too_much(filename, *lineno, ptr)) return -1;
1798 css = cf_section_alloc(buf1,
1799 t2 == T_LCBRACE ? NULL : buf2,
1802 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1806 cf_item_add(this, cf_sectiontoitem(css));
1807 css->item.filename = filename;
1808 css->item.lineno = *lineno;
1811 * The current section is now the child section.
1817 radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1818 filename, *lineno, buf1);
1824 * See if EOF was unexpected ..
1826 if (feof(fp) && (this != current)) {
1827 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1829 cf_section_name1(this), cf_section_lineno(this));
1837 * Include one config file in another.
1839 int cf_file_include(const char *filename, CONF_SECTION *cs)
1843 struct stat statbuf;
1847 DEBUG2( "including configuration file %s", filename);
1849 if (stat(filename, &statbuf) == 0) {
1851 if ((statbuf.st_mode & S_IWOTH) != 0) {
1852 radlog(L_ERR|L_CONS, "Configuration file %s is globally writable. Refusing to start due to insecure configuration.",
1859 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1860 radlog(L_ERR|L_CONS, "Configuration file %s is globally readable. Refusing to start due to insecure configuration.",
1867 fp = fopen(filename, "r");
1869 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1870 filename, strerror(errno));
1874 if (cf_data_find_internal(cs, filename, PW_TYPE_FILENAME)) {
1876 radlog(L_ERR, "Cannot include the same file twice: \"%s\"",
1882 * Add the filename to the section
1884 mtime = rad_malloc(sizeof(*mtime));
1885 *mtime = statbuf.st_mtime;
1887 if (cf_data_add_internal(cs, filename, mtime, free,
1888 PW_TYPE_FILENAME) < 0) {
1890 radlog(L_ERR|L_CONS, "Internal error opening file \"%s\"",
1895 cd = cf_data_find_internal(cs, filename, PW_TYPE_FILENAME);
1898 radlog(L_ERR|L_CONS, "Internal error opening file \"%s\"",
1903 if (!cs->item.filename) cs->item.filename = filename;
1906 * Read the section. It's OK to have EOF without a
1907 * matching close brace.
1909 if (cf_section_read(cd->name, &lineno, fp, cs) < 0) {
1919 * Bootstrap a config file.
1921 CONF_SECTION *cf_file_read(const char *filename)
1927 cs = cf_section_alloc("main", NULL, NULL);
1928 if (!cs) return NULL;
1930 cp = cf_pair_alloc("confdir", filename, T_OP_SET, T_BARE_WORD, cs);
1931 if (!cp) return NULL;
1933 p = strrchr(cp->value, FR_DIR_SEP);
1936 cp->item.filename = "internal";
1937 cp->item.lineno = 0;
1938 cf_item_add(cs, cf_pairtoitem(cp));
1940 if (cf_file_include(filename, cs) < 0) {
1941 cf_section_free(&cs);
1949 * Return a CONF_PAIR within a CONF_SECTION.
1951 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1954 CONF_PAIR *cp = NULL;
1956 if (!cs) return NULL;
1959 * Find the name in the tree, for speed.
1965 cp = rbtree_finddata(cs->pair_tree, &mycp);
1968 * Else find the first one that matches
1970 for (ci = cs->children; ci; ci = ci->next) {
1971 if (ci->type == CONF_ITEM_PAIR) {
1972 return cf_itemtopair(ci);
1977 if (cp || !cs->template) return cp;
1979 return cf_pair_find(cs->template, name);
1983 * Return the attr of a CONF_PAIR
1986 const char *cf_pair_attr(CONF_PAIR *pair)
1988 return (pair ? pair->attr : NULL);
1992 * Return the value of a CONF_PAIR
1995 const char *cf_pair_value(CONF_PAIR *pair)
1997 return (pair ? pair->value : NULL);
2000 FR_TOKEN cf_pair_operator(CONF_PAIR *pair)
2002 return (pair ? pair->operator : T_OP_INVALID);
2006 * Copied here for error reporting.
2008 extern void fr_strerror_printf(const char *, ...);
2011 * Turn a CONF_PAIR into a VALUE_PAIR
2012 * For now, ignore the "value_type" field...
2014 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
2019 fr_strerror_printf("Internal error");
2024 fr_strerror_printf("No value given for attribute %s", pair->attr);
2029 * pairmake handles tags. pairalloc() doesn't.
2031 vp = pairmake(pair->attr, NULL, pair->operator);
2037 * Ignore the value if it's a false comparison.
2039 if (pair->operator == T_OP_CMP_FALSE) return vp;
2041 if (pair->value_type == T_BARE_WORD) {
2042 if ((vp->type == PW_TYPE_STRING) &&
2043 (pair->value[0] == '0') && (pair->value[1] == 'x')) {
2044 vp->type = PW_TYPE_OCTETS;
2046 if (!pairparsevalue(vp, pair->value)) {
2050 vp->flags.do_xlat = 0;
2052 } else if (pair->value_type == T_SINGLE_QUOTED_STRING) {
2053 if (!pairparsevalue(vp, pair->value)) {
2057 vp->flags.do_xlat = 0;
2059 vp->flags.do_xlat = 1;
2066 * Return the first label of a CONF_SECTION
2069 const char *cf_section_name1(const CONF_SECTION *cs)
2071 return (cs ? cs->name1 : NULL);
2075 * Return the second label of a CONF_SECTION
2078 const char *cf_section_name2(const CONF_SECTION *cs)
2080 return (cs ? cs->name2 : NULL);
2084 * Find a value in a CONF_SECTION
2086 const char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
2090 cp = cf_pair_find(cs, attr);
2092 return (cp ? cp->value : NULL);
2096 CONF_SECTION *cf_section_find_name2(const CONF_SECTION *section,
2097 const char *name1, const char *name2)
2102 if (!section || !name1) return NULL;
2104 for (ci = cf_sectiontoitem(section); ci; ci = ci->next) {
2105 if (ci->type != CONF_ITEM_SECTION)
2108 if (strcmp(cf_itemtosection(ci)->name1, name1) != 0)
2111 their2 = cf_itemtosection(ci)->name2;
2113 if ((!name2 && !their2) ||
2114 (name2 && their2 && (strcmp(name2, their2) == 0))) {
2115 return cf_itemtosection(ci);
2123 * Return the next pair after a CONF_PAIR
2124 * with a certain name (char *attr) If the requested
2125 * attr is NULL, any attr matches.
2128 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
2129 CONF_PAIR *pair, const char *attr)
2133 if (!cs) return NULL;
2136 * If pair is NULL this must be a first time run
2137 * Find the pair with correct name
2141 return cf_pair_find(cs, attr);
2144 ci = cf_pairtoitem(pair)->next;
2146 for (; ci; ci = ci->next) {
2147 if (ci->type != CONF_ITEM_PAIR)
2149 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
2153 return cf_itemtopair(ci);
2157 * Find a CONF_SECTION, or return the root if name is NULL
2160 CONF_SECTION *cf_section_find(const char *name)
2163 return cf_section_sub_find(mainconfig.config, name);
2165 return mainconfig.config;
2169 * Find a sub-section in a section
2172 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
2176 if (!name) return NULL; /* can't find an un-named section */
2179 * Do the fast lookup if possible.
2181 if (cs->section_tree) {
2186 return rbtree_finddata(cs->section_tree, &mycs);
2189 for (ci = cs->children; ci; ci = ci->next) {
2190 if (ci->type != CONF_ITEM_SECTION)
2192 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
2196 return cf_itemtosection(ci);
2202 * Find a CONF_SECTION with both names.
2204 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
2205 const char *name1, const char *name2)
2209 if (!cs) cs = mainconfig.config;
2211 if (name1 && (cs->section_tree)) {
2212 CONF_SECTION mycs, *master_cs;
2217 master_cs = rbtree_finddata(cs->section_tree, &mycs);
2219 return rbtree_finddata(master_cs->name2_tree, &mycs);
2224 * Else do it the old-fashioned way.
2226 for (ci = cs->children; ci; ci = ci->next) {
2227 CONF_SECTION *subcs;
2229 if (ci->type != CONF_ITEM_SECTION)
2232 subcs = cf_itemtosection(ci);
2234 if (!subcs->name2) {
2235 if (strcmp(subcs->name1, name2) == 0) break;
2237 if (strcmp(subcs->name2, name2) == 0) break;
2239 continue; /* don't do the string comparisons below */
2242 if ((strcmp(subcs->name1, name1) == 0) &&
2243 (subcs->name2 != NULL) &&
2244 (strcmp(subcs->name2, name2) == 0))
2248 return cf_itemtosection(ci);
2252 * Return the next subsection after a CONF_SECTION
2253 * with a certain name1 (char *name1). If the requested
2254 * name1 is NULL, any name1 matches.
2257 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
2258 CONF_SECTION *subsection,
2263 if (!section) return NULL;
2266 * If subsection is NULL this must be a first time run
2267 * Find the subsection with correct name
2270 if (subsection == NULL){
2271 ci = section->children;
2273 ci = cf_sectiontoitem(subsection)->next;
2276 for (; ci; ci = ci->next) {
2277 if (ci->type != CONF_ITEM_SECTION)
2279 if ((name1 == NULL) ||
2280 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
2284 return cf_itemtosection(ci);
2289 * Return the next section after a CONF_SECTION
2290 * with a certain name1 (char *name1). If the requested
2291 * name1 is NULL, any name1 matches.
2294 CONF_SECTION *cf_section_find_next(CONF_SECTION *section,
2295 CONF_SECTION *subsection,
2298 if (!section) return NULL;
2300 if (!section->item.parent) return NULL;
2302 return cf_subsection_find_next(section->item.parent, subsection, name1);
2306 * Return the next item after a CONF_ITEM.
2309 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
2311 if (!section) return NULL;
2314 * If item is NULL this must be a first time run
2315 * Return the first item
2319 return section->children;
2325 CONF_SECTION *cf_item_parent(CONF_ITEM *ci)
2327 if (!ci) return NULL;
2332 int cf_section_lineno(CONF_SECTION *section)
2334 return cf_sectiontoitem(section)->lineno;
2337 const char *cf_pair_filename(CONF_PAIR *pair)
2339 return cf_pairtoitem(pair)->filename;
2342 const char *cf_section_filename(CONF_SECTION *section)
2344 return cf_sectiontoitem(section)->filename;
2347 int cf_pair_lineno(CONF_PAIR *pair)
2349 return cf_pairtoitem(pair)->lineno;
2352 int cf_item_is_section(CONF_ITEM *item)
2354 return item->type == CONF_ITEM_SECTION;
2356 int cf_item_is_pair(CONF_ITEM *item)
2358 return item->type == CONF_ITEM_PAIR;
2362 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
2363 void *data, void (*data_free)(void *))
2369 name_len = strlen(name) + 1;
2371 p = rad_malloc(sizeof(*cd) + name_len);
2372 cd = (CONF_DATA *) p;
2373 memset(cd, 0, sizeof(*cd));
2375 cd->item.type = CONF_ITEM_DATA;
2376 cd->item.parent = parent;
2378 cd->free = data_free;
2381 memcpy(p, name, name_len);
2387 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
2390 if (!cs || !name) return NULL;
2393 * Find the name in the tree, for speed.
2395 if (cs->data_tree) {
2400 return rbtree_finddata(cs->data_tree, &mycd);
2407 * Find data from a particular section.
2409 void *cf_data_find(CONF_SECTION *cs, const char *name)
2411 CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
2413 if (cd) return cd->data;
2419 * Add named data to a configuration section.
2421 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
2422 void *data, void (*data_free)(void *),
2427 if (!cs || !name) return -1;
2430 * Already exists. Can't add it.
2432 if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
2434 cd = cf_data_alloc(cs, name, data, data_free);
2438 cf_item_add(cs, cf_datatoitem(cd));
2444 * Add named data to a configuration section.
2446 int cf_data_add(CONF_SECTION *cs, const char *name,
2447 void *data, void (*data_free)(void *))
2449 return cf_data_add_internal(cs, name, data, data_free, 0);
2454 * Copy CONF_DATA from src to dst
2456 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
2459 CONF_ITEM *cd, *next, **last;
2462 * Don't check if s->data_tree is NULL. It's child
2463 * sections may have data, even if this section doesn't.
2466 rad_assert(d->data_tree == NULL);
2467 d->data_tree = s->data_tree;
2468 s->data_tree = NULL;
2471 * Walk through src, moving CONF_ITEM_DATA
2474 last = &(s->children);
2475 for (cd = s->children; cd != NULL; cd = next) {
2479 * Recursively copy data from child sections.
2481 if (cd->type == CONF_ITEM_SECTION) {
2482 CONF_SECTION *s1, *d1;
2484 s1 = cf_itemtosection(cd);
2485 d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
2487 cf_section_copy_data(s1, d1);
2494 * Not conf data, remember last ptr.
2496 if (cd->type != CONF_ITEM_DATA) {
2502 * Remove it from the src list
2508 * Add it to the dst list
2511 rad_assert(d->tail == NULL);
2514 rad_assert(d->tail != NULL);
2522 * For a CONF_DATA element, stat the filename, if necessary.
2524 static int filename_stat(void *context, void *data)
2527 CONF_DATA *cd = data;
2529 context = context; /* -Wunused */
2531 if (cd->flag != PW_TYPE_FILENAME) return 0;
2533 if (stat(cd->name, &buf) < 0) return -1;
2535 if (buf.st_mtime != *(time_t *) cd->data) return -1;
2542 * Compare two CONF_SECTIONS. The items MUST be in the same
2545 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
2547 CONF_ITEM *ca = a->children;
2548 CONF_ITEM *cb = b->children;
2556 if (!ca && !cb) break;
2561 if (ca && ca->type == CONF_ITEM_DATA) {
2565 if (cb && cb->type == CONF_ITEM_DATA) {
2571 * One is smaller than the other. Exit.
2573 if (!ca || !cb) return 0;
2575 if (ca->type != cb->type) return 0;
2578 * Deal with subsections.
2580 if (ca->type == CONF_ITEM_SECTION) {
2581 CONF_SECTION *sa = cf_itemtosection(ca);
2582 CONF_SECTION *sb = cf_itemtosection(cb);
2584 if (!cf_section_cmp(sa, sb)) return 0;
2588 rad_assert(ca->type == CONF_ITEM_PAIR);
2590 pa = cf_itemtopair(ca);
2591 pb = cf_itemtopair(cb);
2594 * Different attr and/or value, Exit.
2596 if ((strcmp(pa->attr, pb->attr) != 0) ||
2597 (strcmp(pa->value, pb->value) != 0)) return 0;
2601 * And go to the next element.
2609 * Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
2612 (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
2617 * They must be the same, say so.
2624 * Migrate CONF_DATA from one section to another.
2626 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
2629 CONF_SECTION *s, *d;
2631 for (ci = src->children; ci != NULL; ci = ci->next) {
2632 if (ci->type != CONF_ITEM_SECTION)
2635 s = cf_itemtosection(ci);
2636 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
2638 if (!d) continue; /* not in new one, don't migrate it */
2641 * A section of the same name is in BOTH src & dst,
2642 * compare the CONF_PAIR's. If they're all the same,
2643 * then copy the CONF_DATA from one to the other.
2645 if (cf_section_cmp(s, d)) {
2646 cf_section_copy_data(s, d);
2650 return 1; /* rcode means anything? */
2654 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
2656 if (!cs || !template || cs->template || template->template) return -1;
2658 cs->template = template;
2665 * This is here to make the rest of the code easier to read. It
2666 * ties conffile.c to log.c, but it means we don't have to
2667 * pollute every other function with the knowledge of the
2668 * configuration internals.
2670 void cf_log_err(CONF_ITEM *ci, const char *fmt, ...)
2676 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2679 radlog(L_ERR, "%s[%d]: %s", ci->filename, ci->lineno, buffer);
2683 void cf_log_info(CONF_SECTION *cs, const char *fmt, ...)
2688 if (debug_flag > 1 && cf_log_config && cs) vradlog(L_DBG, fmt, ap);
2693 * Wrapper to simplify the code.
2695 void cf_log_module(CONF_SECTION *cs, const char *fmt, ...)
2701 if (debug_flag > 1 && cf_log_modules && cs) {
2702 vsnprintf(buffer, sizeof(buffer), fmt, ap);
2704 radlog(L_DBG, " Module: %s", buffer);
2709 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
2711 if (!cs) return NULL;
2713 return cs->variables;
2718 * JMG dump_config tries to dump the config structure in a readable format
2722 static int dump_config_section(CONF_SECTION *cs, int indent)
2728 /* The DEBUG macro doesn't let me
2729 * for(i=0;i<indent;++i) debugputchar('\t');
2730 * so I had to get creative. --Pac. */
2732 for (ci = cs->children; ci; ci = ci->next) {
2734 case CONF_ITEM_PAIR:
2735 cp=cf_itemtopair(ci);
2736 DEBUG("%.*s%s = %s",
2737 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2738 cp->attr, cp->value);
2741 case CONF_ITEM_SECTION:
2742 scs=cf_itemtosection(ci);
2743 DEBUG("%.*s%s %s%s{",
2744 indent, "\t\t\t\t\t\t\t\t\t\t\t",
2746 scs->name2 ? scs->name2 : "",
2747 scs->name2 ? " " : "");
2748 dump_config_section(scs, indent+1);
2750 indent, "\t\t\t\t\t\t\t\t\t\t\t");
2753 default: /* FIXME: Do more! */
2761 int dump_config(CONF_SECTION *cs)
2763 return dump_config_section(cs, 0);
2767 static const char *cf_pair_print_value(const CONF_PAIR *cp,
2768 char *buffer, size_t buflen)
2772 if (!cp->value) return "";
2774 switch (cp->value_type) {
2777 snprintf(buffer, buflen, "%s", cp->value);
2780 case T_SINGLE_QUOTED_STRING:
2781 snprintf(buffer, buflen, "'%s'", cp->value);
2784 case T_DOUBLE_QUOTED_STRING:
2786 fr_print_string(cp->value, strlen(cp->value),
2787 buffer + 1, buflen - 3);
2788 p = buffer + strlen(buffer); /* yuck... */
2798 int cf_pair2xml(FILE *fp, const CONF_PAIR *cp)
2800 fprintf(fp, "<%s>", cp->attr);
2805 const char *q = cp->value;
2807 while (*q && (p < (buffer + sizeof(buffer) - 1))) {
2809 memcpy(p, "&", 4);
2812 } else if (q[0] == '<') {
2813 memcpy(p, "<", 4);
2816 } else if (q[0] == '>') {
2817 memcpy(p, ">", 4);
2827 fprintf(fp, "%s", buffer);
2830 fprintf(fp, "</%s>\n", cp->attr);
2835 int cf_section2xml(FILE *fp, const CONF_SECTION *cs)
2837 CONF_ITEM *ci, *next;
2842 fprintf(fp, "<%s>\n", cs->name1);
2844 fprintf(fp, "<_name2>%s</_name2>\n", cs->name2);
2848 * Loop over contents.
2850 for (ci = cs->children; ci; ci = next) {
2854 case CONF_ITEM_PAIR:
2855 if (!cf_pair2xml(fp, (CONF_PAIR *) ci)) return 0;
2858 case CONF_ITEM_SECTION:
2859 if (!cf_section2xml(fp, (CONF_SECTION *) ci)) return 0;
2862 default: /* should really be an error. */
2868 fprintf(fp, "</%s>\n", cs->name1);
2870 return 1; /* success */
2873 int cf_pair2file(FILE *fp, const CONF_PAIR *cp)
2877 fprintf(fp, "\t%s = %s\n", cp->attr,
2878 cf_pair_print_value(cp, buffer, sizeof(buffer)));
2883 int cf_section2file(FILE *fp, const CONF_SECTION *cs)
2885 const CONF_ITEM *ci, *next;
2891 fprintf(fp, "%s {\n", cs->name1);
2893 fprintf(fp, "%s %s {\n",
2894 cs->name1, cs->name2);
2898 * Loop over contents.
2900 for (ci = cs->children; ci; ci = next) {
2904 case CONF_ITEM_PAIR:
2905 if (!cf_pair2file(fp, (const CONF_PAIR *) ci)) return 0;
2908 case CONF_ITEM_SECTION:
2909 if (!cf_section2file(fp, (const CONF_SECTION *) ci)) return 0;
2912 default: /* should really be an error. */
2920 return 1; /* success */