Don't leave DIR's open on error.
[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
743                                         snprintf(buf2, sizeof(buf2), "%s%s",
744                                                  value, dp->d_name);
745                                         if ((is = conf_read(cf, *lineno, buf2, parent)) == NULL) {
746                                                 closedir(dir);
747                                                 cf_section_free(&cs);
748                                                 return NULL;
749                                         }
750                                         
751                                         cf_fixup_children(cs, is);
752                                 }
753                                 closedir(dir);
754                         }  else
755 #endif
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);
760                                         return NULL;
761                                 }
762                                 cf_fixup_children(cs, is);
763                         }
764                         continue;
765                 } /* we were in an include */
766
767                 /*
768                  *      No '=': must be a section or sub-section.
769                  */
770                 if (strchr(ptr, '=') == NULL) {
771                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
772                         t3 = gettoken(&ptr, buf3, sizeof(buf3));
773                 } else {
774                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
775                         t3 = getword(&ptr, buf3, sizeof(buf3));
776                 }
777
778                 /*
779                  *      See if it's the end of a section.
780                  */
781                 if (t1 == T_RCBRACE) {
782                         if (name1 == NULL || buf2[0]) {
783                                 radlog(L_ERR, "%s[%d]: Unexpected end of section",
784                                                 cf, *lineno);
785                                 cf_section_free(&cs);
786                                 return NULL;
787                         }
788                         return cs;
789                 }
790
791                 /*
792                  * Perhaps a subsection.
793                  */
794                 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
795                         css = cf_section_read(cf, lineno, fp, buf1,
796                                               t2==T_LCBRACE ? NULL : buf2, cs);
797                         if (css == NULL) {
798                                 cf_section_free(&cs);
799                                 return NULL;
800                         }
801                         cf_item_add(cs, cf_sectiontoitem(css));
802
803                         continue;
804                 }
805
806                 /*
807                  *      Ignore semi-colons.
808                  */
809                 if (*buf2 == ';')
810                         *buf2 = '\0';
811
812                 /*
813                  *      Must be a normal attr = value line.
814                  */
815                 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
816                         t2 = T_OP_EQ;
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",
820                                         cf, *lineno);
821                         cf_section_free(&cs);
822                         return NULL;
823                 }
824
825                 /*
826                  *      Ensure that the user can't add CONF_PAIRs
827                  *      with 'internal' names;
828                  */
829                 if (buf1[0] == '_') {
830                         radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
831                                         cf, *lineno, buf1);
832                         cf_section_free(&cs);
833                         return NULL;
834                 }
835
836                 /*
837                  *      Handle variable substitution via ${foo}
838                  */
839                 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
840                 if (!value) {
841                         cf_section_free(&cs);
842                         return NULL;
843                 }
844
845
846                 /*
847                  *      Add this CONF_PAIR to our CONF_SECTION
848                  */
849                 cpn = cf_pair_alloc(buf1, value, t2, parent);
850                 cpn->item.lineno = *lineno;
851                 cf_item_add(cs, cf_pairtoitem(cpn));
852         }
853
854         /*
855          *      See if EOF was unexpected ..
856          */
857         if (name1 != NULL) {
858                 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
859                 cf_section_free(&cs);
860                 return NULL;
861         }
862
863         return cs;
864 }
865
866 /*
867  *      Read the config file.
868  */
869 CONF_SECTION *conf_read(const char *fromfile, int fromline,
870                         const char *conffile, CONF_SECTION *parent)
871 {
872         FILE            *fp;
873         int             lineno = 0;
874         CONF_SECTION    *cs;
875
876         if ((fp = fopen(conffile, "r")) == NULL) {
877                 if (fromfile) {
878                         radlog(L_ERR|L_CONS, "%s[%d]: Unable to open file \"%s\": %s",
879                                         fromfile, fromline, conffile, strerror(errno));
880                 } else {
881                         radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
882                                         conffile, strerror(errno));
883                 }
884                 return NULL;
885         }
886
887         cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
888
889         fclose(fp);
890
891         return cs;
892 }
893
894
895 /*
896  * Return a CONF_PAIR within a CONF_SECTION.
897  */
898 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
899 {
900         CONF_ITEM       *ci;
901
902         if (section == NULL) {
903                 section = mainconfig.config;
904         }
905
906         for (ci = section->children; ci; ci = ci->next) {
907                 if (ci->type != CONF_ITEM_PAIR)
908                         continue;
909                 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
910                         break;
911         }
912
913         return cf_itemtopair(ci);
914 }
915
916 /*
917  * Return the attr of a CONF_PAIR
918  */
919
920 char *cf_pair_attr(CONF_PAIR *pair)
921 {
922         return (pair ? pair->attr : NULL);
923 }
924
925 /*
926  * Return the value of a CONF_PAIR
927  */
928
929 char *cf_pair_value(CONF_PAIR *pair)
930 {
931         return (pair ? pair->value : NULL);
932 }
933
934 /*
935  * Return the first label of a CONF_SECTION
936  */
937
938 char *cf_section_name1(CONF_SECTION *section)
939 {
940         return (section ? section->name1 : NULL);
941 }
942
943 /*
944  * Return the second label of a CONF_SECTION
945  */
946
947 char *cf_section_name2(CONF_SECTION *section)
948 {
949         return (section ? section->name2 : NULL);
950 }
951
952 /*
953  * Find a value in a CONF_SECTION
954  */
955 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
956 {
957         CONF_PAIR       *cp;
958
959         cp = cf_pair_find(section, attr);
960
961         return (cp ? cp->value : NULL);
962 }
963
964 /*
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.
968  */
969
970 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
971 {
972         CONF_ITEM       *ci;
973
974         /*
975          * If pair is NULL this must be a first time run
976          * Find the pair with correct name
977          */
978
979         if (pair == NULL){
980                 return cf_pair_find(section, attr);
981         }
982
983         ci = cf_pairtoitem(pair)->next;
984
985         for (; ci; ci = ci->next) {
986                 if (ci->type != CONF_ITEM_PAIR)
987                         continue;
988                 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
989                         break;
990         }
991
992         return cf_itemtopair(ci);
993 }
994
995 /*
996  * Find a CONF_SECTION, or return the root if name is NULL
997  */
998
999 CONF_SECTION *cf_section_find(const char *name)
1000 {
1001         if (name)
1002                 return cf_section_sub_find(mainconfig.config, name);
1003         else
1004                 return mainconfig.config;
1005 }
1006
1007 /*
1008  * Find a sub-section in a section
1009  */
1010
1011 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
1012 {
1013         CONF_ITEM *ci;
1014
1015         for (ci = section->children; ci; ci = ci->next) {
1016                 if (ci->type != CONF_ITEM_SECTION)
1017                         continue;
1018                 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1019                         break;
1020         }
1021
1022         return cf_itemtosection(ci);
1023
1024 }
1025
1026 /*
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.
1030  */
1031
1032 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1033                 CONF_SECTION *subsection,
1034                 const char *name1)
1035 {
1036         CONF_ITEM       *ci;
1037
1038         /*
1039          * If subsection is NULL this must be a first time run
1040          * Find the subsection with correct name
1041          */
1042
1043         if (subsection == NULL){
1044                 ci = section->children;
1045         } else {
1046                 ci = cf_sectiontoitem(subsection)->next;
1047         }
1048
1049         for (; ci; ci = ci->next) {
1050                 if (ci->type != CONF_ITEM_SECTION)
1051                         continue;
1052                 if ((name1 == NULL) ||
1053                                 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1054                         break;
1055         }
1056
1057         return cf_itemtosection(ci);
1058 }
1059
1060 /*
1061  * Return the next item after a CONF_ITEM.
1062  */
1063
1064 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1065 {
1066         /*
1067          * If item is NULL this must be a first time run
1068          * Return the first item
1069          */
1070
1071         if (item == NULL) {
1072                 return section->children;
1073         } else {
1074                 return item->next;
1075         }
1076 }
1077
1078 int cf_section_lineno(CONF_SECTION *section)
1079 {
1080         return cf_sectiontoitem(section)->lineno;
1081 }
1082
1083 int cf_pair_lineno(CONF_PAIR *pair)
1084 {
1085         return cf_pairtoitem(pair)->lineno;
1086 }
1087
1088 int cf_item_is_section(CONF_ITEM *item)
1089 {
1090         return item->type == CONF_ITEM_SECTION;
1091 }
1092
1093
1094 #if 0
1095 /*
1096  * JMG dump_config tries to dump the config structure in a readable format
1097  *
1098 */
1099
1100 static int dump_config_section(CONF_SECTION *cs, int indent)
1101 {
1102         CONF_SECTION    *scs;
1103         CONF_PAIR       *cp;
1104         CONF_ITEM       *ci;
1105
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. */
1109
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);
1116                 } else {
1117                         scs=cf_itemtosection(ci);
1118                         DEBUG("%.*s%s %s%s{",
1119                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1120                                 scs->name1,
1121                                 scs->name2 ? scs->name2 : "",
1122                                 scs->name2 ?  " " : "");
1123                         dump_config_section(scs, indent+1);
1124                         DEBUG("%.*s}",
1125                                 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1126                 }
1127         }
1128
1129         return 0;
1130 }
1131
1132 int dump_config(void)
1133 {
1134         return dump_config_section(mainconfig.config, 0);
1135 }
1136 #endif