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>
30 #include "libradius.h"
35 #ifdef HAVE_NETINET_IN_H
36 # include <netinet/in.h>
44 #include "rad_assert.h"
49 static const char rcsid[] =
52 #define xstrdup strdup
54 typedef enum conf_type {
60 struct conf_item *next;
61 struct conf_part *parent;
75 struct conf_item *children;
79 * Isolate the scary casts in these tiny provably-safe functions
81 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
85 rad_assert(ci->type == CONF_ITEM_PAIR);
86 return (CONF_PAIR *)ci;
88 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
92 rad_assert(ci->type == CONF_ITEM_SECTION);
93 return (CONF_SECTION *)ci;
95 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
99 return (CONF_ITEM *)cp;
101 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
105 return (CONF_ITEM *)cs;
109 * Create a new CONF_PAIR
111 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
112 LRAD_TOKEN operator, CONF_SECTION *parent)
116 cp = (CONF_PAIR *)rad_malloc(sizeof(CONF_PAIR));
117 memset(cp, 0, sizeof(CONF_PAIR));
118 cp->item.type = CONF_ITEM_PAIR;
119 cp->item.parent = parent;
120 cp->attr = xstrdup(attr);
121 cp->value = xstrdup(value);
122 cp->operator = operator;
130 void cf_pair_free(CONF_PAIR **cp)
132 if (!cp || !*cp) return;
140 memset(*cp, 0, sizeof(*cp));
148 * Allocate a CONF_SECTION
150 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
151 CONF_SECTION *parent)
155 if (name1 == NULL || !name1[0])
158 cs = (CONF_SECTION *)rad_malloc(sizeof(CONF_SECTION));
159 memset(cs, 0, sizeof(CONF_SECTION));
160 cs->item.type = CONF_ITEM_SECTION;
161 cs->item.parent = parent;
162 cs->name1 = strdup(name1);
163 cs->name2 = (name2 && *name2) ? xstrdup(name2) : NULL;
169 * Free a CONF_SECTION
171 void cf_section_free(CONF_SECTION **cs)
173 CONF_ITEM *ci, *next;
175 if (!cs || !*cs) return;
177 for (ci = (*cs)->children; ci; ci = next) {
179 if (ci->type==CONF_ITEM_PAIR) {
180 CONF_PAIR *pair = cf_itemtopair(ci);
183 CONF_SECTION *section = cf_itemtosection(ci);
184 cf_section_free(§ion);
194 * And free the section
197 memset(*cs, 0, sizeof(*cs));
205 * Add an item to a configuration section.
207 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci_new)
211 for (ci = cs->children; ci && ci->next; ci = ci->next)
215 cs->children = ci_new;
221 * Expand the variables in an input string.
223 static const char *cf_expand_variables(const char *cf, int *lineno,
224 CONF_SECTION *outercs,
225 char *output, const char *input)
228 const char *end, *ptr;
230 CONF_SECTION *parentcs;
233 * Find the master parent conf section.
234 * We can't use mainconfig.config, because we're in the
235 * process of re-building it, and it isn't set up yet...
237 for (parentcs = outercs;
238 parentcs->item.parent != NULL;
239 parentcs = parentcs->item.parent) {
247 * Ignore anything other than "${"
249 if ((*ptr == '$') && (ptr[1] == '{')) {
255 * FIXME: Add support for ${foo:-bar},
260 * Look for trailing '}', and log a
261 * warning for anything that doesn't match,
262 * and exit with a fatal error.
264 end = strchr(ptr, '}');
267 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
278 * ${.foo} means "foo from the current section"
286 * ${..foo} means "foo from the section
287 * enclosing this section" (etc.)
289 while (*ptr == '.') {
291 cs = cs->item.parent;
298 * ${foo} is local, with
299 * main as lower priority
304 * ${foo.bar.baz} is always rooted
307 for (q = ptr; *q && q != end; q++) {
319 * Find the next section.
322 (*ptr != 0) && (*ptr != '.') &&
330 * The character is a '.', find a
331 * section (as the user has given
332 * us a subsection to find)
337 ptr++; /* skip the period */
340 * Find the sub-section.
342 next = cf_section_sub_find(cs, name);
344 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
349 } else { /* no period, must be a conf-part */
351 * Find in the current referenced
354 cp = cf_pair_find(cs, name);
357 * It it was NOT ${..foo}
359 * top-level config items.
361 if (!up) cp = cf_pair_find(parentcs, name);
364 radlog(L_ERR, "config: No such entry %s for string %s", name, input);
368 } /* until cp is non-NULL */
371 * Substitute the value of the variable.
373 strcpy(p, cp->value);
377 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
383 * Look for trailing '}', and log a
384 * warning for anything that doesn't match,
385 * and exit with a fatal error.
387 end = strchr(ptr, '}');
390 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
395 memcpy(name, ptr, end - ptr);
396 name[end - ptr] = '\0';
399 * Get the environment variable.
400 * If none exists, then make it an empty string.
414 * Copy it over verbatim.
418 } /* loop over all of the input string. */
426 * Parse a configuration section into user-supplied variables.
428 int cf_section_parse(CONF_SECTION *cs, void *base,
429 const CONF_PARSER *variables)
435 CONF_SECTION *subsection;
442 * Handle the user-supplied variables.
444 for (i = 0; variables[i].name != NULL; i++) {
445 value = variables[i].dflt;
446 if (variables[i].data) {
447 data = variables[i].data; /* prefer this. */
449 data = ((char *)base) + variables[i].offset;
451 data = variables[i].data;
454 cp = cf_pair_find(cs, variables[i].name);
459 switch (variables[i].type)
461 case PW_TYPE_SUBSECTION:
462 subsection = cf_section_sub_find(cs,variables[i].name);
465 * If the configuration section is NOT there,
468 * FIXME! This is probably wrong... we should
469 * probably set the items to their default values.
471 if (subsection == NULL) {
475 rcode = cf_section_parse(subsection, base,
476 (CONF_PARSER *) data);
482 case PW_TYPE_BOOLEAN:
484 * Allow yes/no and on/off
486 if ((strcasecmp(value, "yes") == 0) ||
487 (strcasecmp(value, "on") == 0)) {
489 } else if ((strcasecmp(value, "no") == 0) ||
490 (strcasecmp(value, "off") == 0)) {
494 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, variables[i].name);
497 DEBUG2(" %s: %s = %s",
503 case PW_TYPE_INTEGER:
504 *(int *)data = strtol(value, 0, 0);
505 DEBUG2(" %s: %s = %d",
511 case PW_TYPE_STRING_PTR:
518 * Expand variables while parsing,
519 * but ONLY expand ones which haven't already
522 if (value && (value == variables[i].dflt)) {
523 value = cf_expand_variables("?",
531 DEBUG2(" %s: %s = \"%s\"",
534 value ? value : "(null)");
535 *q = value ? strdup(value) : NULL;
540 * Allow '*' as any address
542 if (strcmp(value, "*") == 0) {
543 *(uint32_t *) data = 0;
546 ipaddr = ip_getaddr(value);
548 radlog(L_ERR, "Can't find IP address for host %s", value);
551 DEBUG2(" %s: %s = %s IP address [%s]",
554 value, ip_ntoa(buffer, ipaddr));
555 *(uint32_t *) data = ipaddr;
559 radlog(L_ERR, "type %d not supported yet", variables[i].type);
562 } /* switch over variable type */
563 } /* for all variables in the configuration section */
569 * Used in a few places, so in one function for clarity.
571 static void cf_fixup_children(CONF_SECTION *cs, CONF_SECTION *is)
574 * Add the included conf
575 * to our CONF_SECTION
577 if (is->children != NULL) {
581 * Re-write the parent of the
582 * moved children to be the
583 * upper-layer section.
585 for (ci = is->children; ci; ci = ci->next) {
590 * If there are children, then
591 * move them up a layer.
594 cf_item_add(cs, is->children);
599 * Always free the section for the
602 cf_section_free(&is);
607 * Read a part of the config file.
609 static CONF_SECTION *cf_section_read(const char *cf, int *lineno, FILE *fp,
610 const char *name1, const char *name2,
611 CONF_SECTION *parent)
613 CONF_SECTION *cs, *css;
626 * Ensure that the user can't add CONF_SECTIONs
627 * with 'internal' names;
629 if ((name1 != NULL) && (name1[0] == '_')) {
630 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
636 * Allocate new section.
638 cs = cf_section_alloc(name1, name2, parent);
639 cs->item.lineno = *lineno;
642 * Read, checking for line continuations ('\\' at EOL)
648 * Get data, and remember if we are at EOF.
650 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
656 * We've filled the buffer, and there isn't
659 if ((len == sizeof(buf)) &&
660 (cbuf[len - 1] != '\n')) {
661 radlog(L_ERR, "%s[%d]: Line too long",
663 cf_section_free(&cs);
668 * Check for continuations.
670 if (cbuf[len - 1] == '\n') len--;
673 * Last character is '\\'. Over-write it,
674 * and read another line.
676 if ((len > 0) && (cbuf[len - 1] == '\\')) {
677 cbuf[len - 1] = '\0';
683 * We're at EOF, and haven't read anything. Stop.
685 if (eof && (cbuf == buf)) {
690 t1 = gettoken(&ptr, buf1, sizeof(buf1));
693 * Skip comments and blank lines immediately.
695 if ((*buf1 == '#') || (*buf1 == '\0')) {
700 * Allow for $INCLUDE files
702 * This *SHOULD* work for any level include.
703 * I really really really hate this file. -cparker
705 if (strcasecmp(buf1, "$INCLUDE") == 0) {
708 t2 = getword(&ptr, buf2, sizeof(buf2));
710 value = cf_expand_variables(cf, lineno, cs, buf, buf2);
712 cf_section_free(&cs);
720 * Include ALL non-"dot" files in the directory.
723 if (value[strlen(value) - 1] == '/') {
727 DEBUG2( "Config: including files in directory: %s", value );
728 dir = opendir(value);
730 radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
733 cf_section_free(&cs);
738 * Read the directory, ignoring "." files.
740 while ((dp = readdir(dir)) != NULL) {
741 if (dp->d_name[0] == '.') continue;
742 if (strchr(dp->d_name, '~') != NULL) continue;
744 snprintf(buf2, sizeof(buf2), "%s%s",
746 if ((is = conf_read(cf, *lineno, buf2, parent)) == NULL) {
748 cf_section_free(&cs);
752 cf_fixup_children(cs, is);
757 { /* it was a normal file */
758 DEBUG2( "Config: including file: %s", value );
759 if ((is = conf_read(cf, *lineno, value, parent)) == NULL) {
760 cf_section_free(&cs);
763 cf_fixup_children(cs, is);
766 } /* we were in an include */
769 * No '=': must be a section or sub-section.
771 if (strchr(ptr, '=') == NULL) {
772 t2 = gettoken(&ptr, buf2, sizeof(buf2));
773 t3 = gettoken(&ptr, buf3, sizeof(buf3));
775 t2 = gettoken(&ptr, buf2, sizeof(buf2));
776 t3 = getword(&ptr, buf3, sizeof(buf3));
780 * See if it's the end of a section.
782 if (t1 == T_RCBRACE) {
783 if (name1 == NULL || buf2[0]) {
784 radlog(L_ERR, "%s[%d]: Unexpected end of section",
786 cf_section_free(&cs);
793 * Perhaps a subsection.
795 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
796 css = cf_section_read(cf, lineno, fp, buf1,
797 t2==T_LCBRACE ? NULL : buf2, cs);
799 cf_section_free(&cs);
802 cf_item_add(cs, cf_sectiontoitem(css));
808 * Ignore semi-colons.
814 * Must be a normal attr = value line.
816 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
818 } else if (buf1[0] == 0 || buf2[0] == 0 ||
819 (t2 < T_EQSTART || t2 > T_EQEND)) {
820 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
822 cf_section_free(&cs);
827 * Ensure that the user can't add CONF_PAIRs
828 * with 'internal' names;
830 if (buf1[0] == '_') {
831 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
833 cf_section_free(&cs);
838 * Handle variable substitution via ${foo}
840 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
842 cf_section_free(&cs);
848 * Add this CONF_PAIR to our CONF_SECTION
850 cpn = cf_pair_alloc(buf1, value, t2, parent);
851 cpn->item.lineno = *lineno;
852 cf_item_add(cs, cf_pairtoitem(cpn));
856 * See if EOF was unexpected ..
859 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
860 cf_section_free(&cs);
868 * Read the config file.
870 CONF_SECTION *conf_read(const char *fromfile, int fromline,
871 const char *conffile, CONF_SECTION *parent)
877 if ((fp = fopen(conffile, "r")) == NULL) {
879 radlog(L_ERR|L_CONS, "%s[%d]: Unable to open file \"%s\": %s",
880 fromfile, fromline, conffile, strerror(errno));
882 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
883 conffile, strerror(errno));
888 cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
897 * Return a CONF_PAIR within a CONF_SECTION.
899 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
903 if (section == NULL) {
904 section = mainconfig.config;
907 for (ci = section->children; ci; ci = ci->next) {
908 if (ci->type != CONF_ITEM_PAIR)
910 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
914 return cf_itemtopair(ci);
918 * Return the attr of a CONF_PAIR
921 char *cf_pair_attr(CONF_PAIR *pair)
923 return (pair ? pair->attr : NULL);
927 * Return the value of a CONF_PAIR
930 char *cf_pair_value(CONF_PAIR *pair)
932 return (pair ? pair->value : NULL);
936 * Return the first label of a CONF_SECTION
939 char *cf_section_name1(CONF_SECTION *section)
941 return (section ? section->name1 : NULL);
945 * Return the second label of a CONF_SECTION
948 char *cf_section_name2(CONF_SECTION *section)
950 return (section ? section->name2 : NULL);
954 * Find a value in a CONF_SECTION
956 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
960 cp = cf_pair_find(section, attr);
962 return (cp ? cp->value : NULL);
966 * Return the next pair after a CONF_PAIR
967 * with a certain name (char *attr) If the requested
968 * attr is NULL, any attr matches.
971 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
976 * If pair is NULL this must be a first time run
977 * Find the pair with correct name
981 return cf_pair_find(section, attr);
984 ci = cf_pairtoitem(pair)->next;
986 for (; ci; ci = ci->next) {
987 if (ci->type != CONF_ITEM_PAIR)
989 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
993 return cf_itemtopair(ci);
997 * Find a CONF_SECTION, or return the root if name is NULL
1000 CONF_SECTION *cf_section_find(const char *name)
1003 return cf_section_sub_find(mainconfig.config, name);
1005 return mainconfig.config;
1009 * Find a sub-section in a section
1012 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
1016 for (ci = section->children; ci; ci = ci->next) {
1017 if (ci->type != CONF_ITEM_SECTION)
1019 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1023 return cf_itemtosection(ci);
1028 * Return the next subsection after a CONF_SECTION
1029 * with a certain name1 (char *name1). If the requested
1030 * name1 is NULL, any name1 matches.
1033 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1034 CONF_SECTION *subsection,
1040 * If subsection is NULL this must be a first time run
1041 * Find the subsection with correct name
1044 if (subsection == NULL){
1045 ci = section->children;
1047 ci = cf_sectiontoitem(subsection)->next;
1050 for (; ci; ci = ci->next) {
1051 if (ci->type != CONF_ITEM_SECTION)
1053 if ((name1 == NULL) ||
1054 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1058 return cf_itemtosection(ci);
1062 * Return the next item after a CONF_ITEM.
1065 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1068 * If item is NULL this must be a first time run
1069 * Return the first item
1073 return section->children;
1079 int cf_section_lineno(CONF_SECTION *section)
1081 return cf_sectiontoitem(section)->lineno;
1084 int cf_pair_lineno(CONF_PAIR *pair)
1086 return cf_pairtoitem(pair)->lineno;
1089 int cf_item_is_section(CONF_ITEM *item)
1091 return item->type == CONF_ITEM_SECTION;
1097 * JMG dump_config tries to dump the config structure in a readable format
1101 static int dump_config_section(CONF_SECTION *cs, int indent)
1107 /* The DEBUG macro doesn't let me
1108 * for(i=0;i<indent;++i) debugputchar('\t');
1109 * so I had to get creative. --Pac. */
1111 for (ci = cs->children; ci; ci = ci->next) {
1112 if (ci->type == CONF_ITEM_PAIR) {
1113 cp=cf_itemtopair(ci);
1114 DEBUG("%.*s%s = %s",
1115 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1116 cp->attr, cp->value);
1118 scs=cf_itemtosection(ci);
1119 DEBUG("%.*s%s %s%s{",
1120 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1122 scs->name2 ? scs->name2 : "",
1123 scs->name2 ? " " : "");
1124 dump_config_section(scs, indent+1);
1126 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1133 int dump_config(void)
1135 return dump_config_section(mainconfig.config, 0);