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;
743 snprintf(buf2, sizeof(buf2), "%s%s",
745 if ((is = conf_read(cf, *lineno, buf2, parent)) == NULL) {
747 cf_section_free(&cs);
751 cf_fixup_children(cs, is);
756 { /* it was a normal file */
757 DEBUG2( "Config: including file: %s", value );
758 if ((is = conf_read(cf, *lineno, value, parent)) == NULL) {
759 cf_section_free(&cs);
762 cf_fixup_children(cs, is);
765 } /* we were in an include */
768 * No '=': must be a section or sub-section.
770 if (strchr(ptr, '=') == NULL) {
771 t2 = gettoken(&ptr, buf2, sizeof(buf2));
772 t3 = gettoken(&ptr, buf3, sizeof(buf3));
774 t2 = gettoken(&ptr, buf2, sizeof(buf2));
775 t3 = getword(&ptr, buf3, sizeof(buf3));
779 * See if it's the end of a section.
781 if (t1 == T_RCBRACE) {
782 if (name1 == NULL || buf2[0]) {
783 radlog(L_ERR, "%s[%d]: Unexpected end of section",
785 cf_section_free(&cs);
792 * Perhaps a subsection.
794 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
795 css = cf_section_read(cf, lineno, fp, buf1,
796 t2==T_LCBRACE ? NULL : buf2, cs);
798 cf_section_free(&cs);
801 cf_item_add(cs, cf_sectiontoitem(css));
807 * Ignore semi-colons.
813 * Must be a normal attr = value line.
815 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
817 } else if (buf1[0] == 0 || buf2[0] == 0 ||
818 (t2 < T_EQSTART || t2 > T_EQEND)) {
819 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
821 cf_section_free(&cs);
826 * Ensure that the user can't add CONF_PAIRs
827 * with 'internal' names;
829 if (buf1[0] == '_') {
830 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
832 cf_section_free(&cs);
837 * Handle variable substitution via ${foo}
839 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
841 cf_section_free(&cs);
847 * Add this CONF_PAIR to our CONF_SECTION
849 cpn = cf_pair_alloc(buf1, value, t2, parent);
850 cpn->item.lineno = *lineno;
851 cf_item_add(cs, cf_pairtoitem(cpn));
855 * See if EOF was unexpected ..
858 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
859 cf_section_free(&cs);
867 * Read the config file.
869 CONF_SECTION *conf_read(const char *fromfile, int fromline,
870 const char *conffile, CONF_SECTION *parent)
876 if ((fp = fopen(conffile, "r")) == NULL) {
878 radlog(L_ERR|L_CONS, "%s[%d]: Unable to open file \"%s\": %s",
879 fromfile, fromline, conffile, strerror(errno));
881 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
882 conffile, strerror(errno));
887 cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
896 * Return a CONF_PAIR within a CONF_SECTION.
898 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
902 if (section == NULL) {
903 section = mainconfig.config;
906 for (ci = section->children; ci; ci = ci->next) {
907 if (ci->type != CONF_ITEM_PAIR)
909 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
913 return cf_itemtopair(ci);
917 * Return the attr of a CONF_PAIR
920 char *cf_pair_attr(CONF_PAIR *pair)
922 return (pair ? pair->attr : NULL);
926 * Return the value of a CONF_PAIR
929 char *cf_pair_value(CONF_PAIR *pair)
931 return (pair ? pair->value : NULL);
935 * Return the first label of a CONF_SECTION
938 char *cf_section_name1(CONF_SECTION *section)
940 return (section ? section->name1 : NULL);
944 * Return the second label of a CONF_SECTION
947 char *cf_section_name2(CONF_SECTION *section)
949 return (section ? section->name2 : NULL);
953 * Find a value in a CONF_SECTION
955 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
959 cp = cf_pair_find(section, attr);
961 return (cp ? cp->value : NULL);
965 * Return the next pair after a CONF_PAIR
966 * with a certain name (char *attr) If the requested
967 * attr is NULL, any attr matches.
970 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
975 * If pair is NULL this must be a first time run
976 * Find the pair with correct name
980 return cf_pair_find(section, attr);
983 ci = cf_pairtoitem(pair)->next;
985 for (; ci; ci = ci->next) {
986 if (ci->type != CONF_ITEM_PAIR)
988 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
992 return cf_itemtopair(ci);
996 * Find a CONF_SECTION, or return the root if name is NULL
999 CONF_SECTION *cf_section_find(const char *name)
1002 return cf_section_sub_find(mainconfig.config, name);
1004 return mainconfig.config;
1008 * Find a sub-section in a section
1011 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
1015 for (ci = section->children; ci; ci = ci->next) {
1016 if (ci->type != CONF_ITEM_SECTION)
1018 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1022 return cf_itemtosection(ci);
1027 * Return the next subsection after a CONF_SECTION
1028 * with a certain name1 (char *name1). If the requested
1029 * name1 is NULL, any name1 matches.
1032 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1033 CONF_SECTION *subsection,
1039 * If subsection is NULL this must be a first time run
1040 * Find the subsection with correct name
1043 if (subsection == NULL){
1044 ci = section->children;
1046 ci = cf_sectiontoitem(subsection)->next;
1049 for (; ci; ci = ci->next) {
1050 if (ci->type != CONF_ITEM_SECTION)
1052 if ((name1 == NULL) ||
1053 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1057 return cf_itemtosection(ci);
1061 * Return the next item after a CONF_ITEM.
1064 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1067 * If item is NULL this must be a first time run
1068 * Return the first item
1072 return section->children;
1078 int cf_section_lineno(CONF_SECTION *section)
1080 return cf_sectiontoitem(section)->lineno;
1083 int cf_pair_lineno(CONF_PAIR *pair)
1085 return cf_pairtoitem(pair)->lineno;
1088 int cf_item_is_section(CONF_ITEM *item)
1090 return item->type == CONF_ITEM_SECTION;
1096 * JMG dump_config tries to dump the config structure in a readable format
1100 static int dump_config_section(CONF_SECTION *cs, int indent)
1106 /* The DEBUG macro doesn't let me
1107 * for(i=0;i<indent;++i) debugputchar('\t');
1108 * so I had to get creative. --Pac. */
1110 for (ci = cs->children; ci; ci = ci->next) {
1111 if (ci->type == CONF_ITEM_PAIR) {
1112 cp=cf_itemtopair(ci);
1113 DEBUG("%.*s%s = %s",
1114 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1115 cp->attr, cp->value);
1117 scs=cf_itemtosection(ci);
1118 DEBUG("%.*s%s %s%s{",
1119 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1121 scs->name2 ? scs->name2 : "",
1122 scs->name2 ? " " : "");
1123 dump_config_section(scs, indent+1);
1125 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1132 int dump_config(void)
1134 return dump_config_section(mainconfig.config, 0);