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>
39 #ifdef HAVE_SYS_STAT_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, const char *cf)
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 * Look for trailing '}', and log a
256 * warning for anything that doesn't match,
257 * and exit with a fatal error.
259 end = strchr(ptr, '}');
262 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
273 * ${.foo} means "foo from the current section"
281 * ${..foo} means "foo from the section
282 * enclosing this section" (etc.)
284 while (*ptr == '.') {
286 cs = cs->item.parent;
293 * ${foo} is local, with
294 * main as lower priority
299 * ${foo.bar.baz} is always rooted
302 for (q = ptr; *q && q != end; q++) {
314 * Find the next section.
317 (*ptr != 0) && (*ptr != '.') &&
325 * The character is a '.', find a
326 * section (as the user has given
327 * us a subsection to find)
332 ptr++; /* skip the period */
335 * Find the sub-section.
337 next = cf_section_sub_find(cs, name);
339 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
344 } else { /* no period, must be a conf-part */
346 * Find in the current referenced
349 cp = cf_pair_find(cs, name);
352 * It it was NOT ${..foo}
354 * top-level config items.
356 if (!up) cp = cf_pair_find(parentcs, name);
359 radlog(L_ERR, "config: No such entry %s for string %s", name, input);
363 } /* until cp is non-NULL */
366 * Substitute the value of the variable.
368 strcpy(p, cp->value);
372 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
378 * Look for trailing '}', and log a
379 * warning for anything that doesn't match,
380 * and exit with a fatal error.
382 end = strchr(ptr, '}');
385 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
390 memcpy(name, ptr, end - ptr);
391 name[end - ptr] = '\0';
394 * Get the environment variable.
395 * If none exists, then make it an empty string.
409 * Copy it over verbatim.
413 } /* loop over all of the input string. */
421 * Parse a configuration section into user-supplied variables.
423 int cf_section_parse(CONF_SECTION *cs, void *base,
424 const CONF_PARSER *variables)
430 CONF_SECTION *subsection;
437 * Handle the user-supplied variables.
439 for (i = 0; variables[i].name != NULL; i++) {
440 value = variables[i].dflt;
441 if (variables[i].data) {
442 data = variables[i].data; /* prefer this. */
444 data = ((char *)base) + variables[i].offset;
446 data = variables[i].data;
449 cp = cf_pair_find(cs, variables[i].name);
454 switch (variables[i].type)
456 case PW_TYPE_SUBSECTION:
457 subsection = cf_section_sub_find(cs,variables[i].name);
460 * If the configuration section is NOT there,
463 * FIXME! This is probably wrong... we should
464 * probably set the items to their default values.
466 if (subsection == NULL) {
470 rcode = cf_section_parse(subsection, base,
471 (CONF_PARSER *) data);
477 case PW_TYPE_BOOLEAN:
479 * Allow yes/no and on/off
481 if ((strcasecmp(value, "yes") == 0) ||
482 (strcasecmp(value, "on") == 0)) {
484 } else if ((strcasecmp(value, "no") == 0) ||
485 (strcasecmp(value, "off") == 0)) {
489 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, variables[i].name);
492 DEBUG2(" %s: %s = %s",
498 case PW_TYPE_INTEGER:
499 *(int *)data = strtol(value, 0, 0);
500 DEBUG2(" %s: %s = %d",
506 case PW_TYPE_STRING_PTR:
513 * Expand variables while parsing,
514 * but ONLY expand ones which haven't already
517 if (value && (value == variables[i].dflt)) {
518 value = cf_expand_variables("?",
526 DEBUG2(" %s: %s = \"%s\"",
529 value ? value : "(null)");
530 *q = value ? strdup(value) : NULL;
535 * Allow '*' as any address
537 if (strcmp(value, "*") == 0) {
538 *(uint32_t *) data = 0;
541 ipaddr = ip_getaddr(value);
543 radlog(L_ERR, "Can't find IP address for host %s", value);
546 DEBUG2(" %s: %s = %s IP address [%s]",
549 value, ip_ntoa(buffer, ipaddr));
550 *(uint32_t *) data = ipaddr;
554 radlog(L_ERR, "type %d not supported yet", variables[i].type);
557 } /* switch over variable type */
558 } /* for all variables in the configuration section */
564 * Read a part of the config file.
566 static CONF_SECTION *cf_section_read(const char *cf, int *lineno, FILE *fp,
567 const char *name1, const char *name2,
568 CONF_SECTION *parent)
570 CONF_SECTION *cs, *css;
583 * Ensure that the user can't add CONF_SECTIONs
584 * with 'internal' names;
586 if ((name1 != NULL) && (name1[0] == '_')) {
587 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
593 * Allocate new section.
595 cs = cf_section_alloc(name1, name2, parent, cf);
596 cs->item.lineno = *lineno;
599 * Read, checking for line continuations ('\\' at EOL)
605 * Get data, and remember if we are at EOF.
607 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
613 * We've filled the buffer, and there isn't
616 if ((len == sizeof(buf)) &&
617 (cbuf[len - 1] != '\n')) {
618 radlog(L_ERR, "%s[%d]: Line too long",
620 cf_section_free(&cs);
625 * Check for continuations.
627 if (cbuf[len - 1] == '\n') len--;
630 * Last character is '\\'. Over-write it,
631 * and read another line.
633 if ((len > 0) && (cbuf[len - 1] == '\\')) {
634 cbuf[len - 1] = '\0';
640 * We're at EOF, and haven't read anything. Stop.
642 if (eof && (cbuf == buf)) {
647 t1 = gettoken(&ptr, buf1, sizeof(buf1));
650 * Skip comments and blank lines immediately.
652 if ((*buf1 == '#') || (*buf1 == '\0')) {
657 * Allow for $INCLUDE files
659 * This *SHOULD* work for any level include.
660 * I really really really hate this file. -cparker
662 if (strcasecmp(buf1, "$INCLUDE") == 0) {
666 t2 = getword(&ptr, buf2, sizeof(buf2));
668 value = cf_expand_variables(cf, lineno, cs, buf, buf2);
670 cf_section_free(&cs);
674 DEBUG2( "Config: including file: %s", value );
676 if ((is = conf_read(cf, *lineno, value, cs)) == NULL) {
677 cf_section_free(&cs);
682 * Add the included conf to our CONF_SECTION
685 if (is->children != NULL) {
689 * Re-write the parent of the
690 * moved children to be the
691 * upper-layer section.
693 for (ci = is->children; ci; ci = ci->next) {
698 * If there are children, then
699 * move them up a layer.
702 cf_item_add(cs, is->children);
707 * Always free the section for the
710 cf_section_free(&is);
717 * No '=': must be a section or sub-section.
719 if (strchr(ptr, '=') == NULL) {
720 t2 = gettoken(&ptr, buf2, sizeof(buf2));
721 t3 = gettoken(&ptr, buf3, sizeof(buf3));
723 t2 = gettoken(&ptr, buf2, sizeof(buf2));
724 t3 = getword(&ptr, buf3, sizeof(buf3));
728 * See if it's the end of a section.
730 if (t1 == T_RCBRACE) {
731 if (name1 == NULL || buf2[0]) {
732 radlog(L_ERR, "%s[%d]: Unexpected end of section",
734 cf_section_free(&cs);
741 * Perhaps a subsection.
743 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
744 css = cf_section_read(cf, lineno, fp, buf1,
745 t2==T_LCBRACE ? NULL : buf2, cs);
747 cf_section_free(&cs);
750 cf_item_add(cs, cf_sectiontoitem(css));
756 * Ignore semi-colons.
762 * Must be a normal attr = value line.
764 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
766 } else if (buf1[0] == 0 || buf2[0] == 0 ||
767 (t2 < T_EQSTART || t2 > T_EQEND)) {
768 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
770 cf_section_free(&cs);
775 * Ensure that the user can't add CONF_PAIRs
776 * with 'internal' names;
778 if (buf1[0] == '_') {
779 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
781 cf_section_free(&cs);
786 * Handle variable substitution via ${foo}
788 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
790 cf_section_free(&cs);
796 * Add this CONF_PAIR to our CONF_SECTION
798 cpn = cf_pair_alloc(buf1, value, t2, parent);
799 cpn->item.lineno = *lineno;
800 cf_item_add(cs, cf_pairtoitem(cpn));
804 * See if EOF was unexpected ..
807 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
808 cf_section_free(&cs);
816 * Read the config file.
818 CONF_SECTION *conf_read(const char *fromfile, int fromline,
819 const char *conffile, CONF_SECTION *parent)
829 snprintf(buf, sizeof(buf), "%s[%d]: ", fromfile, fromline);
832 if (stat(conffile, &statbuf) == 0) {
833 if ((statbuf.st_mode & S_IWOTH) != 0) {
834 radlog(L_ERR|L_CONS, "%sConfiguration file %s is globally writable. Refusing to start due to insecure configuration.",
835 buf[0] ? buf : "", conffile);
839 if ((statbuf.st_mode & S_IROTH) != 0) {
840 radlog(L_ERR|L_CONS, "%sConfiguration file %s is globally readable. Refusing to start due to insecure configuration.",
841 buf[0] ? buf : "", conffile);
846 if ((fp = fopen(conffile, "r")) == NULL) {
847 radlog(L_ERR|L_CONS, "%sUnable to open file \"%s\": %s",
848 buf[0] ? buf : "", conffile, strerror(errno));
852 cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
861 * Return a CONF_PAIR within a CONF_SECTION.
863 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
867 if (section == NULL) {
868 section = mainconfig.config;
871 for (ci = section->children; ci; ci = ci->next) {
872 if (ci->type != CONF_ITEM_PAIR)
874 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
878 return cf_itemtopair(ci);
882 * Return the attr of a CONF_PAIR
885 char *cf_pair_attr(CONF_PAIR *pair)
887 return (pair ? pair->attr : NULL);
891 * Return the value of a CONF_PAIR
894 char *cf_pair_value(CONF_PAIR *pair)
896 return (pair ? pair->value : NULL);
900 * Return the first label of a CONF_SECTION
903 char *cf_section_name1(CONF_SECTION *section)
905 return (section ? section->name1 : NULL);
909 * Return the second label of a CONF_SECTION
912 char *cf_section_name2(CONF_SECTION *section)
914 return (section ? section->name2 : NULL);
918 * Find a value in a CONF_SECTION
920 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
924 cp = cf_pair_find(section, attr);
926 return (cp ? cp->value : NULL);
930 * Return the next pair after a CONF_PAIR
931 * with a certain name (char *attr) If the requested
932 * attr is NULL, any attr matches.
935 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
940 * If pair is NULL this must be a first time run
941 * Find the pair with correct name
945 return cf_pair_find(section, attr);
948 ci = cf_pairtoitem(pair)->next;
950 for (; ci; ci = ci->next) {
951 if (ci->type != CONF_ITEM_PAIR)
953 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
957 return cf_itemtopair(ci);
961 * Find a CONF_SECTION, or return the root if name is NULL
964 CONF_SECTION *cf_section_find(const char *name)
967 return cf_section_sub_find(mainconfig.config, name);
969 return mainconfig.config;
973 * Find a sub-section in a section
976 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
980 for (ci = section->children; ci; ci = ci->next) {
981 if (ci->type != CONF_ITEM_SECTION)
983 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
987 return cf_itemtosection(ci);
992 * Return the next subsection after a CONF_SECTION
993 * with a certain name1 (char *name1). If the requested
994 * name1 is NULL, any name1 matches.
997 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
998 CONF_SECTION *subsection,
1004 * If subsection is NULL this must be a first time run
1005 * Find the subsection with correct name
1008 if (subsection == NULL){
1009 ci = section->children;
1011 ci = cf_sectiontoitem(subsection)->next;
1014 for (; ci; ci = ci->next) {
1015 if (ci->type != CONF_ITEM_SECTION)
1017 if ((name1 == NULL) ||
1018 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1022 return cf_itemtosection(ci);
1026 * Return the next item after a CONF_ITEM.
1029 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1032 * If item is NULL this must be a first time run
1033 * Return the first item
1037 return section->children;
1043 int cf_section_lineno(CONF_SECTION *section)
1045 return cf_sectiontoitem(section)->lineno;
1048 int cf_pair_lineno(CONF_PAIR *pair)
1050 return cf_pairtoitem(pair)->lineno;
1053 int cf_item_is_section(CONF_ITEM *item)
1055 return item->type == CONF_ITEM_SECTION;
1057 int cf_item_is_pair(CONF_ITEM *item)
1059 return item->type == CONF_ITEM_PAIR;
1065 * JMG dump_config tries to dump the config structure in a readable format
1069 static int dump_config_section(CONF_SECTION *cs, int indent)
1075 /* The DEBUG macro doesn't let me
1076 * for(i=0;i<indent;++i) debugputchar('\t');
1077 * so I had to get creative. --Pac. */
1079 for (ci = cs->children; ci; ci = ci->next) {
1080 if (ci->type == CONF_ITEM_PAIR) {
1081 cp=cf_itemtopair(ci);
1082 DEBUG("%.*s%s = %s",
1083 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1084 cp->attr, cp->value);
1086 scs=cf_itemtosection(ci);
1087 DEBUG("%.*s%s %s%s{",
1088 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1090 scs->name2 ? scs->name2 : "",
1091 scs->name2 ? " " : "");
1092 dump_config_section(scs, indent+1);
1094 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1101 int dump_config(void)
1103 return dump_config_section(mainconfig.config, 0);