Don't read "foo~" files.
[freeradius.git] / src / main / conffile.c
1 /*
2  * conffile.c   Read the radiusd.conf file.
3  *
4  *              Yep I should learn to use lex & yacc, or at least
5  *              write a decent parser. I know how to do that, really :)
6  *              miquels@cistron.nl
7  *
8  * Version:     $Id$
9  *
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.
14  *
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.
19  *
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
23  *
24  * Copyright 2000  The FreeRADIUS server project
25  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
26  * Copyright 2000  Alan DeKok <aland@ox.org>
27  */
28
29 #include "autoconf.h"
30 #include "libradius.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #ifdef HAVE_NETINET_IN_H
36 #       include <netinet/in.h>
37 #endif
38
39 #ifdef HAVE_DIRENT_H
40 #include <dirent.h>
41 #endif
42
43 #include "radiusd.h"
44 #include "rad_assert.h"
45 #include "conffile.h"
46 #include "token.h"
47 #include "modules.h"
48
49 static const char rcsid[] =
50 "$Id$";
51
52 #define xstrdup strdup
53
54 typedef enum conf_type {
55         CONF_ITEM_PAIR,
56         CONF_ITEM_SECTION
57 } CONF_ITEM_TYPE;
58
59 struct conf_item {
60         struct conf_item *next;
61         struct conf_part *parent;
62         int lineno;
63         CONF_ITEM_TYPE type;
64 };
65 struct conf_pair {
66         CONF_ITEM item;
67         char *attr;
68         char *value;
69         LRAD_TOKEN operator;
70 };
71 struct conf_part {
72         CONF_ITEM item;
73         char *name1;
74         char *name2;
75         struct conf_item *children;
76 };
77
78 /*
79  *      Isolate the scary casts in these tiny provably-safe functions
80  */
81 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
82 {
83         if (ci == NULL)
84                 return NULL;
85         rad_assert(ci->type == CONF_ITEM_PAIR);
86         return (CONF_PAIR *)ci;
87 }
88 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
89 {
90         if (ci == NULL)
91                 return NULL;
92         rad_assert(ci->type == CONF_ITEM_SECTION);
93         return (CONF_SECTION *)ci;
94 }
95 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
96 {
97         if (cp == NULL)
98                 return NULL;
99         return (CONF_ITEM *)cp;
100 }
101 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
102 {
103         if (cs == NULL)
104                 return NULL;
105         return (CONF_ITEM *)cs;
106 }
107
108 /*
109  *      Create a new CONF_PAIR
110  */
111 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
112                 LRAD_TOKEN operator, CONF_SECTION *parent)
113 {
114         CONF_PAIR *cp;
115
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;
123
124         return cp;
125 }
126
127 /*
128  *      Free a CONF_PAIR
129  */
130 void cf_pair_free(CONF_PAIR **cp)
131 {
132         if (!cp || !*cp) return;
133
134         if ((*cp)->attr)
135                 free((*cp)->attr);
136         if ((*cp)->value)
137                 free((*cp)->value);
138
139 #ifndef NDEBUG
140         memset(*cp, 0, sizeof(*cp));
141 #endif
142         free(*cp);
143
144         *cp = NULL;
145 }
146
147 /*
148  *      Allocate a CONF_SECTION
149  */
150 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
151                 CONF_SECTION *parent)
152 {
153         CONF_SECTION    *cs;
154
155         if (name1 == NULL || !name1[0])
156                 name1 = "main";
157
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;
164
165         return cs;
166 }
167
168 /*
169  *      Free a CONF_SECTION
170  */
171 void cf_section_free(CONF_SECTION **cs)
172 {
173         CONF_ITEM       *ci, *next;
174
175         if (!cs || !*cs) return;
176
177         for (ci = (*cs)->children; ci; ci = next) {
178                 next = ci->next;
179                 if (ci->type==CONF_ITEM_PAIR) {
180                         CONF_PAIR *pair = cf_itemtopair(ci);
181                         cf_pair_free(&pair);
182                 } else {
183                         CONF_SECTION *section = cf_itemtosection(ci);
184                         cf_section_free(&section);
185                 }
186         }
187
188         if ((*cs)->name1)
189                 free((*cs)->name1);
190         if ((*cs)->name2)
191                 free((*cs)->name2);
192
193         /*
194          * And free the section
195          */
196 #ifndef NDEBUG
197         memset(*cs, 0, sizeof(*cs));
198 #endif
199         free(*cs);
200
201         *cs = NULL;
202 }
203
204 /*
205  *      Add an item to a configuration section.
206  */
207 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci_new)
208 {
209         CONF_ITEM *ci;
210
211         for (ci = cs->children; ci && ci->next; ci = ci->next)
212                 ;
213
214         if (ci == NULL)
215                 cs->children = ci_new;
216         else
217                 ci->next = ci_new;
218 }
219
220 /*
221  *      Expand the variables in an input string.
222  */
223 static const char *cf_expand_variables(const char *cf, int *lineno,
224                                        CONF_SECTION *outercs,
225                                        char *output, const char *input)
226 {
227         char *p;
228         const char *end, *ptr;
229         char name[8192];
230         CONF_SECTION *parentcs;
231
232         /*
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...
236          */
237         for (parentcs = outercs;
238              parentcs->item.parent != NULL;
239              parentcs = parentcs->item.parent) {
240                 /* do nothing */
241         }
242
243         p = output;
244         ptr = input;
245         while (*ptr) {
246                 /*
247                  *      Ignore anything other than "${"
248                  */
249                 if ((*ptr == '$') && (ptr[1] == '{')) {
250                         int up;
251                         CONF_PAIR *cp;
252                         CONF_SECTION *cs;
253
254                         /*
255                          *      FIXME: Add support for ${foo:-bar},
256                          *      like in xlat.c
257                          */
258
259                         /*
260                          *      Look for trailing '}', and log a
261                          *      warning for anything that doesn't match,
262                          *      and exit with a fatal error.
263                          */
264                         end = strchr(ptr, '}');
265                         if (end == NULL) {
266                                 *p = '\0';
267                                 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
268                                        cf, *lineno);
269                                 return NULL;
270                         }
271
272                         ptr += 2;
273
274                         cp = NULL;
275                         up = 0;
276
277                         /*
278                          *      ${.foo} means "foo from the current section"
279                          */
280                         if (*ptr == '.') {
281                                 up = 1;
282                                 cs = outercs;
283                                 ptr++;
284
285                                 /*
286                                  *      ${..foo} means "foo from the section
287                                  *      enclosing this section" (etc.)
288                                  */
289                                 while (*ptr == '.') {
290                                         if (cs->item.parent)
291                                                 cs = cs->item.parent;
292                                         ptr++;
293                                 }
294
295                         } else {
296                                 const char *q;
297                                 /*
298                                  *      ${foo} is local, with
299                                  *      main as lower priority
300                                  */
301                                 cs = outercs;
302
303                                 /*
304                                  *      ${foo.bar.baz} is always rooted
305                                  *      from the top.
306                                  */
307                                 for (q = ptr; *q && q != end; q++) {
308                                         if (*q == '.') {
309                                                 cs = parentcs;
310                                                 up = 1;
311                                                 break;
312                                         }
313                                 }
314                         }
315
316                         while (cp == NULL) {
317                                 char *q;
318                                 /*
319                                  *      Find the next section.
320                                  */
321                                 for (q = name;
322                                      (*ptr != 0) && (*ptr != '.') &&
323                                              (ptr != end);
324                                      q++, ptr++) {
325                                         *q = *ptr;
326                                 }
327                                 *q = '\0';
328
329                                 /*
330                                  *      The character is a '.', find a
331                                  *      section (as the user has given
332                                  *      us a subsection to find)
333                                  */
334                                 if (*ptr == '.') {
335                                         CONF_SECTION *next;
336
337                                         ptr++;  /* skip the period */
338
339                                         /*
340                                          *      Find the sub-section.
341                                          */
342                                         next = cf_section_sub_find(cs, name);
343                                         if (next == NULL) {
344                                                 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
345                                                 return NULL;
346                                         }
347                                         cs = next;
348
349                                 } else { /* no period, must be a conf-part */
350                                         /*
351                                          *      Find in the current referenced
352                                          *      section.
353                                          */
354                                         cp = cf_pair_find(cs, name);
355                                         if (cp == NULL) {
356                                                 /*
357                                                  *      It it was NOT ${..foo}
358                                                  *      then look in the
359                                                  *      top-level config items.
360                                                  */
361                                                 if (!up) cp = cf_pair_find(parentcs, name);
362                                         }
363                                         if (cp == NULL) {
364                                                 radlog(L_ERR, "config: No such entry %s for string %s", name, input);
365                                                 return NULL;
366                                         }
367                                 }
368                         } /* until cp is non-NULL */
369
370                         /*
371                          *  Substitute the value of the variable.
372                          */
373                         strcpy(p, cp->value);
374                         p += strlen(p);
375                         ptr = end + 1;
376
377                 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
378                         char *env;
379
380                         ptr += 5;
381
382                         /*
383                          *      Look for trailing '}', and log a
384                          *      warning for anything that doesn't match,
385                          *      and exit with a fatal error.
386                          */
387                         end = strchr(ptr, '}');
388                         if (end == NULL) {
389                                 *p = '\0';
390                                 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
391                                        cf, *lineno);
392                                 return NULL;
393                         }
394
395                         memcpy(name, ptr, end - ptr);
396                         name[end - ptr] = '\0';
397
398                         /*
399                          *      Get the environment variable.
400                          *      If none exists, then make it an empty string.
401                          */
402                         env = getenv(name);
403                         if (env == NULL) {
404                                 *name = '\0';
405                                 env = name;
406                         }
407
408                         strcpy(p, env);
409                         p += strlen(p);
410                         ptr = end + 1;
411
412                 } else {
413                         /*
414                          *      Copy it over verbatim.
415                          */
416                         *(p++) = *(ptr++);
417                 }
418         } /* loop over all of the input string. */
419
420         *p = '\0';
421
422         return output;
423 }
424
425 /*
426  *      Parse a configuration section into user-supplied variables.
427  */
428 int cf_section_parse(CONF_SECTION *cs, void *base,
429                      const CONF_PARSER *variables)
430 {
431         int i;
432         int rcode;
433         char **q;
434         CONF_PAIR *cp;
435         CONF_SECTION *subsection;
436         uint32_t ipaddr;
437         char buffer[8192];
438         const char *value;
439         void *data;
440
441         /*
442          *      Handle the user-supplied variables.
443          */
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. */
448                 } else if (base) {
449                         data = ((char *)base) + variables[i].offset;
450                 } else {
451                         data = variables[i].data;
452                 }
453
454                 cp = cf_pair_find(cs, variables[i].name);
455                 if (cp) {
456                         value = cp->value;
457                 }
458
459                 switch (variables[i].type)
460                 {
461                 case PW_TYPE_SUBSECTION:
462                         subsection = cf_section_sub_find(cs,variables[i].name);
463
464                         /*
465                          *      If the configuration section is NOT there,
466                          *      then ignore it.
467                          *
468                          *      FIXME! This is probably wrong... we should
469                          *      probably set the items to their default values.
470                          */
471                         if (subsection == NULL) {
472                                 break;
473                         }
474
475                         rcode = cf_section_parse(subsection, base,
476                                         (CONF_PARSER *) data);
477                         if (rcode < 0) {
478                                 return -1;
479                         }
480                         break;
481
482                 case PW_TYPE_BOOLEAN:
483                         /*
484                          *      Allow yes/no and on/off
485                          */
486                         if ((strcasecmp(value, "yes") == 0) ||
487                                         (strcasecmp(value, "on") == 0)) {
488                                 *(int *)data = 1;
489                         } else if ((strcasecmp(value, "no") == 0) ||
490                                                 (strcasecmp(value, "off") == 0)) {
491                                 *(int *)data = 0;
492                         } else {
493                                 *(int *)data = 0;
494                                 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, variables[i].name);
495                                 return -1;
496                         }
497                         DEBUG2(" %s: %s = %s",
498                                         cs->name1,
499                                         variables[i].name,
500                                         value);
501                         break;
502
503                 case PW_TYPE_INTEGER:
504                         *(int *)data = strtol(value, 0, 0);
505                         DEBUG2(" %s: %s = %d",
506                                         cs->name1,
507                                         variables[i].name,
508                                         *(int *)data);
509                         break;
510
511                 case PW_TYPE_STRING_PTR:
512                         q = (char **) data;
513                         if (*q != NULL) {
514                                 free(*q);
515                         }
516
517                         /*
518                          *      Expand variables while parsing,
519                          *      but ONLY expand ones which haven't already
520                          *      been expanded.
521                          */
522                         if (value && (value == variables[i].dflt)) {
523                                 value = cf_expand_variables("?",
524                                                             &cs->item.lineno,
525                                                             cs, buffer, value);
526                                 if (!value) {
527                                         return -1;
528                                 }
529                         }
530
531                         DEBUG2(" %s: %s = \"%s\"",
532                                         cs->name1,
533                                         variables[i].name,
534                                         value ? value : "(null)");
535                         *q = value ? strdup(value) : NULL;
536                         break;
537
538                 case PW_TYPE_IPADDR:
539                         /*
540                          *      Allow '*' as any address
541                          */
542                         if (strcmp(value, "*") == 0) {
543                                 *(uint32_t *) data = 0;
544                                 break;
545                         }
546                         ipaddr = ip_getaddr(value);
547                         if (ipaddr == 0) {
548                                 radlog(L_ERR, "Can't find IP address for host %s", value);
549                                 return -1;
550                         }
551                         DEBUG2(" %s: %s = %s IP address [%s]",
552                                         cs->name1,
553                                         variables[i].name,
554                                         value, ip_ntoa(buffer, ipaddr));
555                         *(uint32_t *) data = ipaddr;
556                         break;
557
558                 default:
559                         radlog(L_ERR, "type %d not supported yet", variables[i].type);
560                         return -1;
561                         break;
562                 } /* switch over variable type */
563         } /* for all variables in the configuration section */
564
565         return 0;
566 }
567
568 /*
569  *      Used in a few places, so in one function for clarity.
570  */
571 static void cf_fixup_children(CONF_SECTION *cs, CONF_SECTION *is)
572 {
573         /*
574          *      Add the included conf
575          *      to our CONF_SECTION
576          */
577         if (is->children != NULL) {
578                 CONF_ITEM *ci;
579                 
580                 /*
581                  *      Re-write the parent of the
582                  *      moved children to be the
583                  *      upper-layer section.
584                  */
585                 for (ci = is->children; ci; ci = ci->next) {
586                         ci->parent = cs;
587                 }
588                 
589                 /*
590                  *      If there are children, then
591                  *      move them up a layer.
592                  */
593                 if (is->children) {
594                         cf_item_add(cs, is->children);
595                 }
596                 is->children = NULL;
597         }
598         /*
599          *      Always free the section for the
600          *      $INCLUDEd file.
601          */
602         cf_section_free(&is);
603 }
604
605
606 /*
607  *      Read a part of the config file.
608  */
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)
612 {
613         CONF_SECTION *cs, *css;
614         CONF_PAIR *cpn;
615         char *ptr;
616         const char *value;
617         char buf[8192];
618         char buf1[8192];
619         char buf2[8192];
620         char buf3[8192];
621         int t1, t2, t3;
622         char *cbuf = buf;
623         int len;
624
625         /*
626          *      Ensure that the user can't add CONF_SECTIONs
627          *      with 'internal' names;
628          */
629         if ((name1 != NULL) && (name1[0] == '_')) {
630                 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
631                         cf, *lineno);
632                 return NULL;
633         }
634
635         /*
636          *      Allocate new section.
637          */
638         cs = cf_section_alloc(name1, name2, parent);
639         cs->item.lineno = *lineno;
640
641         /*
642          *      Read, checking for line continuations ('\\' at EOL)
643          */
644         for (;;) {
645                 int eof;
646
647                 /*
648                  *      Get data, and remember if we are at EOF.
649                  */
650                 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
651                 (*lineno)++;
652
653                 len = strlen(cbuf);
654
655                 /*
656                  *      We've filled the buffer, and there isn't
657                  *      a CR in it.  Die!
658                  */
659                 if ((len == sizeof(buf)) &&
660                     (cbuf[len - 1] != '\n')) {
661                         radlog(L_ERR, "%s[%d]: Line too long",
662                                cf, *lineno);
663                         cf_section_free(&cs);
664                         return NULL;
665                 }
666
667                 /*
668                  *  Check for continuations.
669                  */
670                 if (cbuf[len - 1] == '\n') len--;
671
672                 /*
673                  *      Last character is '\\'.  Over-write it,
674                  *      and read another line.
675                  */
676                 if ((len > 0) && (cbuf[len - 1] == '\\')) {
677                         cbuf[len - 1] = '\0';
678                         cbuf += len - 1;
679                         continue;
680                 }
681
682                 /*
683                  *  We're at EOF, and haven't read anything.  Stop.
684                  */
685                 if (eof && (cbuf == buf)) {
686                         break;
687                 }
688
689                 ptr = cbuf = buf;
690                 t1 = gettoken(&ptr, buf1, sizeof(buf1));
691
692                 /*
693                  *      Skip comments and blank lines immediately.
694                  */
695                 if ((*buf1 == '#') || (*buf1 == '\0')) {
696                         continue;
697                 }
698
699                 /*
700                  *      Allow for $INCLUDE files
701                  *
702                  *      This *SHOULD* work for any level include.
703                  *      I really really really hate this file.  -cparker
704                  */
705                 if (strcasecmp(buf1, "$INCLUDE") == 0) {
706                         CONF_SECTION    *is;
707
708                         t2 = getword(&ptr, buf2, sizeof(buf2));
709
710                         value = cf_expand_variables(cf, lineno, cs, buf, buf2);
711                         if (value == NULL) {
712                                 cf_section_free(&cs);
713                                 return NULL;
714                         }
715
716 #ifdef HAVE_DIRENT_H
717                         /*
718                          *      $INCLUDE foo/
719                          *
720                          *      Include ALL non-"dot" files in the directory.
721                          *      careful!
722                          */
723                         if (value[strlen(value) - 1] == '/') {
724                                 DIR             *dir;
725                                 struct dirent   *dp;
726
727                                 DEBUG2( "Config:   including files in directory: %s", value );
728                                 dir = opendir(value);
729                                 if (!dir) {
730                                         radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
731                                                cf, *lineno, value,
732                                                strerror(errno));
733                                         cf_section_free(&cs);
734                                         return NULL;
735                                 }
736
737                                 /*
738                                  *      Read the directory, ignoring "." files.
739                                  */
740                                 while ((dp = readdir(dir)) != NULL) {
741                                         if (dp->d_name[0] == '.') continue;
742                                         if (strchr(dp->d_name, '~') != NULL) continue;
743
744                                         snprintf(buf2, sizeof(buf2), "%s%s",
745                                                  value, dp->d_name);
746                                         if ((is = conf_read(cf, *lineno, buf2, parent)) == NULL) {
747                                                 closedir(dir);
748                                                 cf_section_free(&cs);
749                                                 return NULL;
750                                         }
751                                         
752                                         cf_fixup_children(cs, is);
753                                 }
754                                 closedir(dir);
755                         }  else
756 #endif
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);
761                                         return NULL;
762                                 }
763                                 cf_fixup_children(cs, is);
764                         }
765                         continue;
766                 } /* we were in an include */
767
768                 /*
769                  *      No '=': must be a section or sub-section.
770                  */
771                 if (strchr(ptr, '=') == NULL) {
772                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
773                         t3 = gettoken(&ptr, buf3, sizeof(buf3));
774                 } else {
775                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
776                         t3 = getword(&ptr, buf3, sizeof(buf3));
777                 }
778
779                 /*
780                  *      See if it's the end of a section.
781                  */
782                 if (t1 == T_RCBRACE) {
783                         if (name1 == NULL || buf2[0]) {
784                                 radlog(L_ERR, "%s[%d]: Unexpected end of section",
785                                                 cf, *lineno);
786                                 cf_section_free(&cs);
787                                 return NULL;
788                         }
789                         return cs;
790                 }
791
792                 /*
793                  * Perhaps a subsection.
794                  */
795                 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
796                         css = cf_section_read(cf, lineno, fp, buf1,
797                                               t2==T_LCBRACE ? NULL : buf2, cs);
798                         if (css == NULL) {
799                                 cf_section_free(&cs);
800                                 return NULL;
801                         }
802                         cf_item_add(cs, cf_sectiontoitem(css));
803
804                         continue;
805                 }
806
807                 /*
808                  *      Ignore semi-colons.
809                  */
810                 if (*buf2 == ';')
811                         *buf2 = '\0';
812
813                 /*
814                  *      Must be a normal attr = value line.
815                  */
816                 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
817                         t2 = T_OP_EQ;
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",
821                                         cf, *lineno);
822                         cf_section_free(&cs);
823                         return NULL;
824                 }
825
826                 /*
827                  *      Ensure that the user can't add CONF_PAIRs
828                  *      with 'internal' names;
829                  */
830                 if (buf1[0] == '_') {
831                         radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
832                                         cf, *lineno, buf1);
833                         cf_section_free(&cs);
834                         return NULL;
835                 }
836
837                 /*
838                  *      Handle variable substitution via ${foo}
839                  */
840                 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
841                 if (!value) {
842                         cf_section_free(&cs);
843                         return NULL;
844                 }
845
846
847                 /*
848                  *      Add this CONF_PAIR to our CONF_SECTION
849                  */
850                 cpn = cf_pair_alloc(buf1, value, t2, parent);
851                 cpn->item.lineno = *lineno;
852                 cf_item_add(cs, cf_pairtoitem(cpn));
853         }
854
855         /*
856          *      See if EOF was unexpected ..
857          */
858         if (name1 != NULL) {
859                 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
860                 cf_section_free(&cs);
861                 return NULL;
862         }
863
864         return cs;
865 }
866
867 /*
868  *      Read the config file.
869  */
870 CONF_SECTION *conf_read(const char *fromfile, int fromline,
871                         const char *conffile, CONF_SECTION *parent)
872 {
873         FILE            *fp;
874         int             lineno = 0;
875         CONF_SECTION    *cs;
876
877         if ((fp = fopen(conffile, "r")) == NULL) {
878                 if (fromfile) {
879                         radlog(L_ERR|L_CONS, "%s[%d]: Unable to open file \"%s\": %s",
880                                         fromfile, fromline, conffile, strerror(errno));
881                 } else {
882                         radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
883                                         conffile, strerror(errno));
884                 }
885                 return NULL;
886         }
887
888         cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
889
890         fclose(fp);
891
892         return cs;
893 }
894
895
896 /*
897  * Return a CONF_PAIR within a CONF_SECTION.
898  */
899 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
900 {
901         CONF_ITEM       *ci;
902
903         if (section == NULL) {
904                 section = mainconfig.config;
905         }
906
907         for (ci = section->children; ci; ci = ci->next) {
908                 if (ci->type != CONF_ITEM_PAIR)
909                         continue;
910                 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
911                         break;
912         }
913
914         return cf_itemtopair(ci);
915 }
916
917 /*
918  * Return the attr of a CONF_PAIR
919  */
920
921 char *cf_pair_attr(CONF_PAIR *pair)
922 {
923         return (pair ? pair->attr : NULL);
924 }
925
926 /*
927  * Return the value of a CONF_PAIR
928  */
929
930 char *cf_pair_value(CONF_PAIR *pair)
931 {
932         return (pair ? pair->value : NULL);
933 }
934
935 /*
936  * Return the first label of a CONF_SECTION
937  */
938
939 char *cf_section_name1(CONF_SECTION *section)
940 {
941         return (section ? section->name1 : NULL);
942 }
943
944 /*
945  * Return the second label of a CONF_SECTION
946  */
947
948 char *cf_section_name2(CONF_SECTION *section)
949 {
950         return (section ? section->name2 : NULL);
951 }
952
953 /*
954  * Find a value in a CONF_SECTION
955  */
956 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
957 {
958         CONF_PAIR       *cp;
959
960         cp = cf_pair_find(section, attr);
961
962         return (cp ? cp->value : NULL);
963 }
964
965 /*
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.
969  */
970
971 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
972 {
973         CONF_ITEM       *ci;
974
975         /*
976          * If pair is NULL this must be a first time run
977          * Find the pair with correct name
978          */
979
980         if (pair == NULL){
981                 return cf_pair_find(section, attr);
982         }
983
984         ci = cf_pairtoitem(pair)->next;
985
986         for (; ci; ci = ci->next) {
987                 if (ci->type != CONF_ITEM_PAIR)
988                         continue;
989                 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
990                         break;
991         }
992
993         return cf_itemtopair(ci);
994 }
995
996 /*
997  * Find a CONF_SECTION, or return the root if name is NULL
998  */
999
1000 CONF_SECTION *cf_section_find(const char *name)
1001 {
1002         if (name)
1003                 return cf_section_sub_find(mainconfig.config, name);
1004         else
1005                 return mainconfig.config;
1006 }
1007
1008 /*
1009  * Find a sub-section in a section
1010  */
1011
1012 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
1013 {
1014         CONF_ITEM *ci;
1015
1016         for (ci = section->children; ci; ci = ci->next) {
1017                 if (ci->type != CONF_ITEM_SECTION)
1018                         continue;
1019                 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1020                         break;
1021         }
1022
1023         return cf_itemtosection(ci);
1024
1025 }
1026
1027 /*
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.
1031  */
1032
1033 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1034                 CONF_SECTION *subsection,
1035                 const char *name1)
1036 {
1037         CONF_ITEM       *ci;
1038
1039         /*
1040          * If subsection is NULL this must be a first time run
1041          * Find the subsection with correct name
1042          */
1043
1044         if (subsection == NULL){
1045                 ci = section->children;
1046         } else {
1047                 ci = cf_sectiontoitem(subsection)->next;
1048         }
1049
1050         for (; ci; ci = ci->next) {
1051                 if (ci->type != CONF_ITEM_SECTION)
1052                         continue;
1053                 if ((name1 == NULL) ||
1054                                 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1055                         break;
1056         }
1057
1058         return cf_itemtosection(ci);
1059 }
1060
1061 /*
1062  * Return the next item after a CONF_ITEM.
1063  */
1064
1065 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1066 {
1067         /*
1068          * If item is NULL this must be a first time run
1069          * Return the first item
1070          */
1071
1072         if (item == NULL) {
1073                 return section->children;
1074         } else {
1075                 return item->next;
1076         }
1077 }
1078
1079 int cf_section_lineno(CONF_SECTION *section)
1080 {
1081         return cf_sectiontoitem(section)->lineno;
1082 }
1083
1084 int cf_pair_lineno(CONF_PAIR *pair)
1085 {
1086         return cf_pairtoitem(pair)->lineno;
1087 }
1088
1089 int cf_item_is_section(CONF_ITEM *item)
1090 {
1091         return item->type == CONF_ITEM_SECTION;
1092 }
1093
1094
1095 #if 0
1096 /*
1097  * JMG dump_config tries to dump the config structure in a readable format
1098  *
1099 */
1100
1101 static int dump_config_section(CONF_SECTION *cs, int indent)
1102 {
1103         CONF_SECTION    *scs;
1104         CONF_PAIR       *cp;
1105         CONF_ITEM       *ci;
1106
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. */
1110
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);
1117                 } else {
1118                         scs=cf_itemtosection(ci);
1119                         DEBUG("%.*s%s %s%s{",
1120                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1121                                 scs->name1,
1122                                 scs->name2 ? scs->name2 : "",
1123                                 scs->name2 ?  " " : "");
1124                         dump_config_section(scs, indent+1);
1125                         DEBUG("%.*s}",
1126                                 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1127                 }
1128         }
1129
1130         return 0;
1131 }
1132
1133 int dump_config(void)
1134 {
1135         return dump_config_section(mainconfig.config, 0);
1136 }
1137 #endif