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;
76 const CONF_PARSER *variables;
77 struct conf_item *children;
81 * Isolate the scary casts in these tiny provably-safe functions
83 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
87 rad_assert(ci->type == CONF_ITEM_PAIR);
88 return (CONF_PAIR *)ci;
90 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
94 rad_assert(ci->type == CONF_ITEM_SECTION);
95 return (CONF_SECTION *)ci;
97 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
101 return (CONF_ITEM *)cp;
103 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
107 return (CONF_ITEM *)cs;
111 * Create a new CONF_PAIR
113 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
114 LRAD_TOKEN operator, CONF_SECTION *parent)
118 cp = (CONF_PAIR *)rad_malloc(sizeof(CONF_PAIR));
119 memset(cp, 0, sizeof(CONF_PAIR));
120 cp->item.type = CONF_ITEM_PAIR;
121 cp->item.parent = parent;
122 cp->attr = xstrdup(attr);
123 cp->value = xstrdup(value);
124 cp->operator = operator;
132 void cf_pair_free(CONF_PAIR **cp)
134 if (!cp || !*cp) return;
142 memset(*cp, 0, sizeof(*cp));
150 * Free strings we've parsed into data structures.
152 static void cf_section_parse_free(void *base, const CONF_PARSER *variables)
157 * Don't automatically free the strings if we're being
158 * called from a module.
160 if (base || !variables) return;
163 * Free up dynamically allocated string pointers.
165 for (i = 0; variables[i].name != NULL; i++) {
168 if (variables[i].type != PW_TYPE_STRING_PTR) {
173 * Prefer the data, if it's there.
174 * Else use the base + offset.
176 if (!variables[i].data) {
183 p = (char **) (variables[i].data);
193 * Allocate a CONF_SECTION
195 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
196 CONF_SECTION *parent, const char *cf)
200 if (name1 == NULL || !name1[0])
203 cs = (CONF_SECTION *)rad_malloc(sizeof(CONF_SECTION));
204 memset(cs, 0, sizeof(CONF_SECTION));
205 cs->item.type = CONF_ITEM_SECTION;
206 cs->item.parent = parent;
207 cs->name1 = strdup(name1);
208 cs->name2 = (name2 && *name2) ? xstrdup(name2) : NULL;
214 * Free a CONF_SECTION
216 void cf_section_free(CONF_SECTION **cs)
218 CONF_ITEM *ci, *next;
220 if (!cs || !*cs) return;
222 if ((*cs)->variables) {
223 cf_section_parse_free((*cs)->base, (*cs)->variables);
226 for (ci = (*cs)->children; ci; ci = next) {
228 if (ci->type==CONF_ITEM_PAIR) {
229 CONF_PAIR *pair = cf_itemtopair(ci);
232 CONF_SECTION *section = cf_itemtosection(ci);
233 cf_section_free(§ion);
243 * And free the section
246 memset(*cs, 0, sizeof(*cs));
254 * Add an item to a configuration section.
256 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci_new)
260 for (ci = cs->children; ci && ci->next; ci = ci->next)
264 cs->children = ci_new;
270 * Expand the variables in an input string.
272 static const char *cf_expand_variables(const char *cf, int *lineno,
273 CONF_SECTION *outercs,
274 char *output, const char *input)
277 const char *end, *ptr;
279 CONF_SECTION *parentcs;
282 * Find the master parent conf section.
283 * We can't use mainconfig.config, because we're in the
284 * process of re-building it, and it isn't set up yet...
286 for (parentcs = outercs;
287 parentcs->item.parent != NULL;
288 parentcs = parentcs->item.parent) {
296 * Ignore anything other than "${"
298 if ((*ptr == '$') && (ptr[1] == '{')) {
304 * Look for trailing '}', and log a
305 * warning for anything that doesn't match,
306 * and exit with a fatal error.
308 end = strchr(ptr, '}');
311 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
322 * ${.foo} means "foo from the current section"
330 * ${..foo} means "foo from the section
331 * enclosing this section" (etc.)
333 while (*ptr == '.') {
335 cs = cs->item.parent;
342 * ${foo} is local, with
343 * main as lower priority
348 * ${foo.bar.baz} is always rooted
351 for (q = ptr; *q && q != end; q++) {
363 * Find the next section.
366 (*ptr != 0) && (*ptr != '.') &&
374 * The character is a '.', find a
375 * section (as the user has given
376 * us a subsection to find)
381 ptr++; /* skip the period */
384 * Find the sub-section.
386 next = cf_section_sub_find(cs, name);
388 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
393 } else { /* no period, must be a conf-part */
395 * Find in the current referenced
398 cp = cf_pair_find(cs, name);
401 * It it was NOT ${..foo}
403 * top-level config items.
405 if (!up) cp = cf_pair_find(parentcs, name);
408 radlog(L_ERR, "config: No such entry %s for string %s", name, input);
412 } /* until cp is non-NULL */
415 * Substitute the value of the variable.
417 strcpy(p, cp->value);
421 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
427 * Look for trailing '}', and log a
428 * warning for anything that doesn't match,
429 * and exit with a fatal error.
431 end = strchr(ptr, '}');
434 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
439 memcpy(name, ptr, end - ptr);
440 name[end - ptr] = '\0';
443 * Get the environment variable.
444 * If none exists, then make it an empty string.
458 * Copy it over verbatim.
462 } /* loop over all of the input string. */
471 * Parse a configuration section into user-supplied variables.
473 int cf_section_parse(CONF_SECTION *cs, void *base,
474 const CONF_PARSER *variables)
480 CONF_SECTION *subsection;
487 * Handle the user-supplied variables.
489 for (i = 0; variables[i].name != NULL; i++) {
490 value = variables[i].dflt;
491 if (variables[i].data) {
492 data = variables[i].data; /* prefer this. */
494 data = ((char *)base) + variables[i].offset;
496 data = variables[i].data;
499 cp = cf_pair_find(cs, variables[i].name);
504 switch (variables[i].type)
506 case PW_TYPE_SUBSECTION:
507 subsection = cf_section_sub_find(cs,variables[i].name);
510 * If the configuration section is NOT there,
513 * FIXME! This is probably wrong... we should
514 * probably set the items to their default values.
516 if (subsection == NULL) {
520 rcode = cf_section_parse(subsection, base,
521 (CONF_PARSER *) data);
523 cf_section_parse_free(base, variables);
528 case PW_TYPE_BOOLEAN:
530 * Allow yes/no and on/off
532 if ((strcasecmp(value, "yes") == 0) ||
533 (strcasecmp(value, "on") == 0)) {
535 } else if ((strcasecmp(value, "no") == 0) ||
536 (strcasecmp(value, "off") == 0)) {
540 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, variables[i].name);
541 cf_section_parse_free(base, variables);
544 DEBUG2(" %s: %s = %s",
550 case PW_TYPE_INTEGER:
551 *(int *)data = strtol(value, 0, 0);
552 DEBUG2(" %s: %s = %d",
558 case PW_TYPE_STRING_PTR:
565 * Expand variables while parsing,
566 * but ONLY expand ones which haven't already
569 if (value && (value == variables[i].dflt)) {
570 value = cf_expand_variables("?",
574 cf_section_parse_free(base, variables);
579 DEBUG2(" %s: %s = \"%s\"",
582 value ? value : "(null)");
583 *q = value ? strdup(value) : NULL;
588 * Allow '*' as any address
590 if (strcmp(value, "*") == 0) {
591 *(uint32_t *) data = 0;
594 ipaddr = ip_getaddr(value);
596 radlog(L_ERR, "Can't find IP address for host %s", value);
597 cf_section_parse_free(base, variables);
600 DEBUG2(" %s: %s = %s IP address [%s]",
603 value, ip_ntoa(buffer, ipaddr));
604 *(uint32_t *) data = ipaddr;
608 radlog(L_ERR, "type %d not supported yet", variables[i].type);
609 cf_section_parse_free(base, variables);
612 } /* switch over variable type */
613 } /* for all variables in the configuration section */
616 cs->variables = variables;
623 * Read a part of the config file.
625 static CONF_SECTION *cf_section_read(const char *cf, int *lineno, FILE *fp,
626 const char *name1, const char *name2,
627 CONF_SECTION *parent)
629 CONF_SECTION *cs, *css;
642 * Ensure that the user can't add CONF_SECTIONs
643 * with 'internal' names;
645 if ((name1 != NULL) && (name1[0] == '_')) {
646 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
652 * Allocate new section.
654 cs = cf_section_alloc(name1, name2, parent, cf);
655 cs->item.lineno = *lineno;
658 * Read, checking for line continuations ('\\' at EOL)
664 * Get data, and remember if we are at EOF.
666 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
672 * We've filled the buffer, and there isn't
675 if ((len >= (sizeof(buf) - 1)) &&
676 (((cbuf[len - 1] != '\n')) ||
677 (cbuf[len - 1] == '\\'))) {
678 radlog(L_ERR, "%s[%d]: Line too long",
680 cf_section_free(&cs);
685 * Check for continuations.
687 if (cbuf[len - 1] == '\n') len--;
690 * Last character is '\\'. Over-write it,
691 * and read another line.
693 if ((len > 0) && (cbuf[len - 1] == '\\')) {
694 cbuf[len - 1] = '\0';
700 * We're at EOF, and haven't read anything. Stop.
702 if (eof && (cbuf == buf)) {
707 t1 = gettoken(&ptr, buf1, sizeof(buf1));
710 * Skip comments and blank lines immediately.
712 if ((*buf1 == '#') || (*buf1 == '\0')) {
717 * Allow for $INCLUDE files
719 * This *SHOULD* work for any level include.
720 * I really really really hate this file. -cparker
722 if (strcasecmp(buf1, "$INCLUDE") == 0) {
726 t2 = getword(&ptr, buf2, sizeof(buf2));
728 value = cf_expand_variables(cf, lineno, cs, buf, buf2);
730 cf_section_free(&cs);
734 DEBUG2( "Config: including file: %s", value );
736 if ((is = conf_read(cf, *lineno, value, cs)) == NULL) {
737 cf_section_free(&cs);
742 * Add the included conf to our CONF_SECTION
745 if (is->children != NULL) {
749 * Re-write the parent of the
750 * moved children to be the
751 * upper-layer section.
753 for (ci = is->children; ci; ci = ci->next) {
758 * If there are children, then
759 * move them up a layer.
762 cf_item_add(cs, is->children);
767 * Always free the section for the
770 cf_section_free(&is);
777 * No '=': must be a section or sub-section.
779 if (strchr(ptr, '=') == NULL) {
780 t2 = gettoken(&ptr, buf2, sizeof(buf2));
781 t3 = gettoken(&ptr, buf3, sizeof(buf3));
783 t2 = gettoken(&ptr, buf2, sizeof(buf2));
784 t3 = getword(&ptr, buf3, sizeof(buf3));
788 * See if it's the end of a section.
790 if (t1 == T_RCBRACE) {
791 if (name1 == NULL || buf2[0]) {
792 radlog(L_ERR, "%s[%d]: Unexpected end of section",
794 cf_section_free(&cs);
801 * Perhaps a subsection.
803 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
804 css = cf_section_read(cf, lineno, fp, buf1,
805 t2==T_LCBRACE ? NULL : buf2, cs);
807 cf_section_free(&cs);
810 cf_item_add(cs, cf_sectiontoitem(css));
816 * Ignore semi-colons.
822 * Must be a normal attr = value line.
824 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
826 } else if (buf1[0] == 0 || buf2[0] == 0 ||
827 (t2 < T_EQSTART || t2 > T_EQEND)) {
828 radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
830 cf_section_free(&cs);
835 * Ensure that the user can't add CONF_PAIRs
836 * with 'internal' names;
838 if (buf1[0] == '_') {
839 radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
841 cf_section_free(&cs);
846 * Handle variable substitution via ${foo}
848 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
850 cf_section_free(&cs);
856 * Add this CONF_PAIR to our CONF_SECTION
858 cpn = cf_pair_alloc(buf1, value, t2, parent);
859 cpn->item.lineno = *lineno;
860 cf_item_add(cs, cf_pairtoitem(cpn));
864 * See if EOF was unexpected ..
867 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
868 cf_section_free(&cs);
876 * Read the config file.
878 CONF_SECTION *conf_read(const char *fromfile, int fromline,
879 const char *conffile, CONF_SECTION *parent)
889 snprintf(buf, sizeof(buf), "%s[%d]: ", fromfile, fromline);
892 if (stat(conffile, &statbuf) == 0) {
893 if ((statbuf.st_mode & S_IWOTH) != 0) {
894 radlog(L_ERR|L_CONS, "%sConfiguration file %s is globally writable. Refusing to start due to insecure configuration.",
895 buf[0] ? buf : "", conffile);
899 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
900 radlog(L_ERR|L_CONS, "%sConfiguration file %s is globally readable. Refusing to start due to insecure configuration.",
901 buf[0] ? buf : "", conffile);
906 if ((fp = fopen(conffile, "r")) == NULL) {
907 radlog(L_ERR|L_CONS, "%sUnable to open file \"%s\": %s",
908 buf[0] ? buf : "", conffile, strerror(errno));
912 cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
921 * Return a CONF_PAIR within a CONF_SECTION.
923 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
927 if (section == NULL) {
928 section = mainconfig.config;
931 for (ci = section->children; ci; ci = ci->next) {
932 if (ci->type != CONF_ITEM_PAIR)
934 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
938 return cf_itemtopair(ci);
942 * Return the attr of a CONF_PAIR
945 char *cf_pair_attr(CONF_PAIR *pair)
947 return (pair ? pair->attr : NULL);
951 * Return the value of a CONF_PAIR
954 char *cf_pair_value(CONF_PAIR *pair)
956 return (pair ? pair->value : NULL);
960 * Return the first label of a CONF_SECTION
963 char *cf_section_name1(CONF_SECTION *section)
965 return (section ? section->name1 : NULL);
969 * Return the second label of a CONF_SECTION
972 char *cf_section_name2(CONF_SECTION *section)
974 return (section ? section->name2 : NULL);
978 * Find a value in a CONF_SECTION
980 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
984 cp = cf_pair_find(section, attr);
986 return (cp ? cp->value : NULL);
990 * Return the next pair after a CONF_PAIR
991 * with a certain name (char *attr) If the requested
992 * attr is NULL, any attr matches.
995 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
1000 * If pair is NULL this must be a first time run
1001 * Find the pair with correct name
1005 return cf_pair_find(section, attr);
1008 ci = cf_pairtoitem(pair)->next;
1010 for (; ci; ci = ci->next) {
1011 if (ci->type != CONF_ITEM_PAIR)
1013 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1017 return cf_itemtopair(ci);
1021 * Find a CONF_SECTION, or return the root if name is NULL
1024 CONF_SECTION *cf_section_find(const char *name)
1027 return cf_section_sub_find(mainconfig.config, name);
1029 return mainconfig.config;
1033 * Find a sub-section in a section
1036 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
1040 for (ci = section->children; ci; ci = ci->next) {
1041 if (ci->type != CONF_ITEM_SECTION)
1043 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1047 return cf_itemtosection(ci);
1052 * Return the next subsection after a CONF_SECTION
1053 * with a certain name1 (char *name1). If the requested
1054 * name1 is NULL, any name1 matches.
1057 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1058 CONF_SECTION *subsection,
1064 * If subsection is NULL this must be a first time run
1065 * Find the subsection with correct name
1068 if (subsection == NULL){
1069 ci = section->children;
1071 ci = cf_sectiontoitem(subsection)->next;
1074 for (; ci; ci = ci->next) {
1075 if (ci->type != CONF_ITEM_SECTION)
1077 if ((name1 == NULL) ||
1078 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1082 return cf_itemtosection(ci);
1086 * Return the next item after a CONF_ITEM.
1089 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1092 * If item is NULL this must be a first time run
1093 * Return the first item
1097 return section->children;
1103 int cf_section_lineno(CONF_SECTION *section)
1105 return cf_sectiontoitem(section)->lineno;
1108 int cf_pair_lineno(CONF_PAIR *pair)
1110 return cf_pairtoitem(pair)->lineno;
1113 int cf_item_is_section(CONF_ITEM *item)
1115 return item->type == CONF_ITEM_SECTION;
1117 int cf_item_is_pair(CONF_ITEM *item)
1119 return item->type == CONF_ITEM_PAIR;
1125 * JMG dump_config tries to dump the config structure in a readable format
1129 static int dump_config_section(CONF_SECTION *cs, int indent)
1135 /* The DEBUG macro doesn't let me
1136 * for(i=0;i<indent;++i) debugputchar('\t');
1137 * so I had to get creative. --Pac. */
1139 for (ci = cs->children; ci; ci = ci->next) {
1140 if (ci->type == CONF_ITEM_PAIR) {
1141 cp=cf_itemtopair(ci);
1142 DEBUG("%.*s%s = %s",
1143 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1144 cp->attr, cp->value);
1146 scs=cf_itemtosection(ci);
1147 DEBUG("%.*s%s %s%s{",
1148 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1150 scs->name2 ? scs->name2 : "",
1151 scs->name2 ? " " : "");
1152 dump_config_section(scs, indent+1);
1154 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1161 int dump_config(void)
1163 return dump_config_section(mainconfig.config, 0);