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>
40 #include "rad_assert.h"
45 static const char rcsid[] =
48 #define xstrdup strdup
50 typedef enum conf_type {
56 struct conf_item *next;
57 struct conf_part *parent;
71 struct conf_item *children;
75 * Isolate the scary casts in these tiny provably-safe functions
77 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
81 rad_assert(ci->type == CONF_ITEM_PAIR);
82 return (CONF_PAIR *)ci;
84 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
88 rad_assert(ci->type == CONF_ITEM_SECTION);
89 return (CONF_SECTION *)ci;
91 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
95 return (CONF_ITEM *)cp;
97 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
101 return (CONF_ITEM *)cs;
105 * Create a new CONF_PAIR
107 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
108 LRAD_TOKEN operator, CONF_SECTION *parent)
112 cp = (CONF_PAIR *)rad_malloc(sizeof(CONF_PAIR));
113 memset(cp, 0, sizeof(CONF_PAIR));
114 cp->item.type = CONF_ITEM_PAIR;
115 cp->item.parent = parent;
116 cp->attr = xstrdup(attr);
117 cp->value = xstrdup(value);
118 cp->operator = operator;
126 void cf_pair_free(CONF_PAIR **cp)
128 if (!cp || !*cp) return;
136 memset(*cp, 0, sizeof(*cp));
144 * Allocate a CONF_SECTION
146 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
147 CONF_SECTION *parent)
151 if (name1 == NULL || !name1[0])
154 cs = (CONF_SECTION *)rad_malloc(sizeof(CONF_SECTION));
155 memset(cs, 0, sizeof(CONF_SECTION));
156 cs->item.type = CONF_ITEM_SECTION;
157 cs->item.parent = parent;
158 cs->name1 = strdup(name1);
159 cs->name2 = (name2 && *name2) ? xstrdup(name2) : NULL;
165 * Free a CONF_SECTION
167 void cf_section_free(CONF_SECTION **cs)
169 CONF_ITEM *ci, *next;
171 if (!cs || !*cs) return;
173 for (ci = (*cs)->children; ci; ci = next) {
175 if (ci->type==CONF_ITEM_PAIR) {
176 CONF_PAIR *pair = cf_itemtopair(ci);
179 CONF_SECTION *section = cf_itemtosection(ci);
180 cf_section_free(§ion);
190 * And free the section
193 memset(*cs, 0, sizeof(*cs));
201 * Add an item to a configuration section.
203 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci_new)
207 for (ci = cs->children; ci && ci->next; ci = ci->next)
211 cs->children = ci_new;
217 * Expand the variables in an input string.
219 static const char *cf_expand_variables(const char *cf, int *lineno,
220 CONF_SECTION *outercs,
221 char *output, const char *input)
224 const char *end, *ptr;
226 CONF_SECTION *parentcs;
229 * Find the master parent conf section.
230 * We can't use mainconfig.config, because we're in the
231 * process of re-building it, and it isn't set up yet...
233 for (parentcs = outercs;
234 parentcs->item.parent != NULL;
235 parentcs = parentcs->item.parent) {
243 * Ignore anything other than "${"
245 if ((*ptr == '$') && (ptr[1] == '{')) {
251 * Look for trailing '}', and log a
252 * warning for anything that doesn't match,
253 * and exit with a fatal error.
255 end = strchr(ptr, '}');
258 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
269 * ${.foo} means "foo from the current section"
277 * ${..foo} means "foo from the section
278 * enclosing this section" (etc.)
280 while (*ptr == '.') {
282 cs = cs->item.parent;
289 * ${foo} is local, with
290 * main as lower priority
295 * ${foo.bar.baz} is always rooted
298 for (q = ptr; *q && q != end; q++) {
310 * Find the next section.
313 (*ptr != 0) && (*ptr != '.') &&
321 * The character is a '.', find a
322 * section (as the user has given
323 * us a subsection to find)
328 ptr++; /* skip the period */
331 * Find the sub-section.
333 next = cf_section_sub_find(cs, name);
335 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
340 } else { /* no period, must be a conf-part */
342 * Find in the current referenced
345 cp = cf_pair_find(cs, name);
348 * It it was NOT ${..foo}
350 * top-level config items.
352 if (!up) cp = cf_pair_find(parentcs, name);
355 radlog(L_ERR, "config: No such entry %s for string %s", name, input);
359 } /* until cp is non-NULL */
362 * Substitute the value of the variable.
364 strcpy(p, cp->value);
368 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
374 * Look for trailing '}', and log a
375 * warning for anything that doesn't match,
376 * and exit with a fatal error.
378 end = strchr(ptr, '}');
381 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
386 memcpy(name, ptr, end - ptr);
387 name[end - ptr] = '\0';
390 * Get the environment variable.
391 * If none exists, then make it an empty string.
405 * Copy it over verbatim.
409 } /* loop over all of the input string. */
417 * Parse a configuration section into user-supplied variables.
419 int cf_section_parse(CONF_SECTION *cs, void *base,
420 const CONF_PARSER *variables)
426 CONF_SECTION *subsection;
433 * Handle the user-supplied variables.
435 for (i = 0; variables[i].name != NULL; i++) {
436 value = variables[i].dflt;
437 if (variables[i].data) {
438 data = variables[i].data; /* prefer this. */
440 data = ((char *)base) + variables[i].offset;
442 data = variables[i].data;
445 cp = cf_pair_find(cs, variables[i].name);
450 switch (variables[i].type)
452 case PW_TYPE_SUBSECTION:
453 subsection = cf_section_sub_find(cs,variables[i].name);
456 * If the configuration section is NOT there,
459 * FIXME! This is probably wrong... we should
460 * probably set the items to their default values.
462 if (subsection == NULL) {
466 rcode = cf_section_parse(subsection, base,
467 (CONF_PARSER *) data);
473 case PW_TYPE_BOOLEAN:
475 * Allow yes/no and on/off
477 if ((strcasecmp(value, "yes") == 0) ||
478 (strcasecmp(value, "on") == 0)) {
480 } else if ((strcasecmp(value, "no") == 0) ||
481 (strcasecmp(value, "off") == 0)) {
485 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, variables[i].name);
488 DEBUG2(" %s: %s = %s",
494 case PW_TYPE_INTEGER:
495 *(int *)data = strtol(value, 0, 0);
496 DEBUG2(" %s: %s = %d",
502 case PW_TYPE_STRING_PTR:
509 * Expand variables while parsing,
510 * but ONLY expand ones which haven't already
513 if (value && (value == variables[i].dflt)) {
514 value = cf_expand_variables("?",
522 DEBUG2(" %s: %s = \"%s\"",
525 value ? value : "(null)");
526 *q = value ? strdup(value) : NULL;
531 * Allow '*' as any address
533 if (strcmp(value, "*") == 0) {
534 *(uint32_t *) data = 0;
537 ipaddr = ip_getaddr(value);
539 radlog(L_ERR, "Can't find IP address for host %s", value);
542 DEBUG2(" %s: %s = %s IP address [%s]",
545 value, ip_ntoa(buffer, ipaddr));
546 *(uint32_t *) data = ipaddr;
550 radlog(L_ERR, "type %d not supported yet", variables[i].type);
553 } /* switch over variable type */
554 } /* for all variables in the configuration section */
560 * Read a part of the config file.
562 static CONF_SECTION *cf_section_read(const char *cf, int *lineno, FILE *fp,
563 const char *name1, const char *name2,
564 CONF_SECTION *parent)
566 CONF_SECTION *cs, *css;
579 * Ensure that the user can't add CONF_SECTIONs
580 * with 'internal' names;
582 if ((name1 != NULL) && (name1[0] == '_')) {
583 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
589 * Allocate new section.
591 cs = cf_section_alloc(name1, name2, parent);
592 cs->item.lineno = *lineno;
595 * Read, checking for line continuations ('\\' at EOL)
601 * Get data, and remember if we are at EOF.
603 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
609 * We've filled the buffer, and there isn't
612 if ((len == sizeof(buf)) &&
613 (cbuf[len - 1] != '\n')) {
614 radlog(L_ERR, "%s[%d]: Line too long",
616 cf_section_free(&cs);
621 * Check for continuations.
623 if (cbuf[len - 1] == '\n') len--;
626 * Last character is '\\'. Over-write it,
627 * and read another line.
629 if ((len > 0) && (cbuf[len - 1] == '\\')) {
630 cbuf[len - 1] = '\0';
636 * We're at EOF, and haven't read anything. Stop.
638 if (eof && (cbuf == buf)) {
643 t1 = gettoken(&ptr, buf1, sizeof(buf1));
646 * Skip comments and blank lines immediately.
648 if ((*buf1 == '#') || (*buf1 == '\0')) {
653 * Allow for $INCLUDE files
655 * This *SHOULD* work for any level include.
656 * I really really really hate this file. -cparker
658 if (strcasecmp(buf1, "$INCLUDE") == 0) {
662 t2 = getword(&ptr, buf2, sizeof(buf2));
664 value = cf_expand_variables(cf, lineno, cs, buf, buf2);
666 cf_section_free(&cs);
670 DEBUG2( "Config: including file: %s", value );
672 if ((is = conf_read(cf, *lineno, value, cs)) == NULL) {
673 cf_section_free(&cs);
678 * Add the included conf to our CONF_SECTION
681 if (is->children != NULL) {
685 * Re-write the parent of the
686 * moved children to be the
687 * upper-layer section.
689 for (ci = is->children; ci; ci = ci->next) {
694 * If there are children, then
695 * move them up a layer.
698 cf_item_add(cs, is->children);
703 * Always free the section for the
706 cf_section_free(&is);
713 * No '=': must be a section or sub-section.
715 if (strchr(ptr, '=') == NULL) {
716 t2 = gettoken(&ptr, buf2, sizeof(buf2));
717 t3 = gettoken(&ptr, buf3, sizeof(buf3));
719 t2 = gettoken(&ptr, buf2, sizeof(buf2));
720 t3 = getword(&ptr, buf3, sizeof(buf3));
724 * See if it's the end of a section.
726 if (t1 == T_RCBRACE) {
727 if (name1 == NULL || buf2[0]) {
728 radlog(L_ERR, "%s[%d]: Unexpected end of section",
730 cf_section_free(&cs);
737 * Perhaps a subsection.
739 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
740 css = cf_section_read(cf, lineno, fp, buf1,
741 t2==T_LCBRACE ? NULL : buf2, cs);
743 cf_section_free(&cs);
746 cf_item_add(cs, cf_sectiontoitem(css));
752 * Ignore semi-colons.
758 * Must be a normal attr = value line.
760 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
762 } else if (buf1[0] == 0 || buf2[0] == 0 ||
763 (t2 < T_EQSTART || t2 > T_EQEND)) {
764 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
766 cf_section_free(&cs);
771 * Ensure that the user can't add CONF_PAIRs
772 * with 'internal' names;
774 if (buf1[0] == '_') {
775 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
777 cf_section_free(&cs);
782 * Handle variable substitution via ${foo}
784 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
786 cf_section_free(&cs);
792 * Add this CONF_PAIR to our CONF_SECTION
794 cpn = cf_pair_alloc(buf1, value, t2, parent);
795 cpn->item.lineno = *lineno;
796 cf_item_add(cs, cf_pairtoitem(cpn));
800 * See if EOF was unexpected ..
803 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
804 cf_section_free(&cs);
812 * Read the config file.
814 CONF_SECTION *conf_read(const char *fromfile, int fromline,
815 const char *conffile, CONF_SECTION *parent)
821 if ((fp = fopen(conffile, "r")) == NULL) {
823 radlog(L_ERR|L_CONS, "%s[%d]: Unable to open file \"%s\": %s",
824 fromfile, fromline, conffile, strerror(errno));
826 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
827 conffile, strerror(errno));
833 cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
835 cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, NULL);
845 * Return a CONF_PAIR within a CONF_SECTION.
847 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
851 if (section == NULL) {
852 section = mainconfig.config;
855 for (ci = section->children; ci; ci = ci->next) {
856 if (ci->type != CONF_ITEM_PAIR)
858 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
862 return cf_itemtopair(ci);
866 * Return the attr of a CONF_PAIR
869 char *cf_pair_attr(CONF_PAIR *pair)
871 return (pair ? pair->attr : NULL);
875 * Return the value of a CONF_PAIR
878 char *cf_pair_value(CONF_PAIR *pair)
880 return (pair ? pair->value : NULL);
884 * Return the first label of a CONF_SECTION
887 char *cf_section_name1(CONF_SECTION *section)
889 return (section ? section->name1 : NULL);
893 * Return the second label of a CONF_SECTION
896 char *cf_section_name2(CONF_SECTION *section)
898 return (section ? section->name2 : NULL);
902 * Find a value in a CONF_SECTION
904 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
908 cp = cf_pair_find(section, attr);
910 return (cp ? cp->value : NULL);
914 * Return the next pair after a CONF_PAIR
915 * with a certain name (char *attr) If the requested
916 * attr is NULL, any attr matches.
919 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
924 * If pair is NULL this must be a first time run
925 * Find the pair with correct name
929 return cf_pair_find(section, attr);
932 ci = cf_pairtoitem(pair)->next;
934 for (; ci; ci = ci->next) {
935 if (ci->type != CONF_ITEM_PAIR)
937 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
941 return cf_itemtopair(ci);
945 * Find a CONF_SECTION, or return the root if name is NULL
948 CONF_SECTION *cf_section_find(const char *name)
951 return cf_section_sub_find(mainconfig.config, name);
953 return mainconfig.config;
957 * Find a sub-section in a section
960 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
964 for (ci = section->children; ci; ci = ci->next) {
965 if (ci->type != CONF_ITEM_SECTION)
967 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
971 return cf_itemtosection(ci);
976 * Return the next subsection after a CONF_SECTION
977 * with a certain name1 (char *name1). If the requested
978 * name1 is NULL, any name1 matches.
981 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
982 CONF_SECTION *subsection,
988 * If subsection is NULL this must be a first time run
989 * Find the subsection with correct name
992 if (subsection == NULL){
993 ci = section->children;
995 ci = cf_sectiontoitem(subsection)->next;
998 for (; ci; ci = ci->next) {
999 if (ci->type != CONF_ITEM_SECTION)
1001 if ((name1 == NULL) ||
1002 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1006 return cf_itemtosection(ci);
1010 * Return the next item after a CONF_ITEM.
1013 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1016 * If item is NULL this must be a first time run
1017 * Return the first item
1021 return section->children;
1027 int cf_section_lineno(CONF_SECTION *section)
1029 return cf_sectiontoitem(section)->lineno;
1032 int cf_pair_lineno(CONF_PAIR *pair)
1034 return cf_pairtoitem(pair)->lineno;
1037 int cf_item_is_section(CONF_ITEM *item)
1039 return item->type == CONF_ITEM_SECTION;
1041 int cf_item_is_pair(CONF_ITEM *item)
1043 return item->type == CONF_ITEM_PAIR;
1049 * JMG dump_config tries to dump the config structure in a readable format
1053 static int dump_config_section(CONF_SECTION *cs, int indent)
1059 /* The DEBUG macro doesn't let me
1060 * for(i=0;i<indent;++i) debugputchar('\t');
1061 * so I had to get creative. --Pac. */
1063 for (ci = cs->children; ci; ci = ci->next) {
1064 if (ci->type == CONF_ITEM_PAIR) {
1065 cp=cf_itemtopair(ci);
1066 DEBUG("%.*s%s = %s",
1067 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1068 cp->attr, cp->value);
1070 scs=cf_itemtosection(ci);
1071 DEBUG("%.*s%s %s%s{",
1072 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1074 scs->name2 ? scs->name2 : "",
1075 scs->name2 ? " " : "");
1076 dump_config_section(scs, indent+1);
1078 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1085 int dump_config(void)
1087 return dump_config_section(mainconfig.config, 0);