cf_data_add calls cf_item_add.
[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
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef HAVE_NETINET_IN_H
35 #       include <netinet/in.h>
36 #endif
37
38 #ifdef HAVE_DIRENT_H
39 #include <dirent.h>
40
41 #ifdef HAVE_SYS_STAT_H
42 #include <sys/stat.h>
43 #endif
44 #endif
45
46 #include <ctype.h>
47
48 #include "radiusd.h"
49 #include "rad_assert.h"
50 #include "conffile.h"
51 #include "token.h"
52 #include "modules.h"
53
54 static const char rcsid[] =
55 "$Id$";
56
57 typedef enum conf_type {
58         CONF_ITEM_INVALID = 0,
59         CONF_ITEM_PAIR,
60         CONF_ITEM_SECTION,
61         CONF_ITEM_DATA
62 } CONF_ITEM_TYPE;
63
64 struct conf_item {
65         struct conf_item *next;
66         struct conf_part *parent;
67         int lineno;
68         CONF_ITEM_TYPE type;
69 };
70 struct conf_pair {
71         CONF_ITEM item;
72         char *attr;
73         char *value;
74         LRAD_TOKEN operator;
75 };
76 struct conf_part {
77         CONF_ITEM item;
78         const char *name1;
79         const char *name2;
80         struct conf_item *children;
81         rbtree_t        *pair_tree; /* and a partridge.. */
82         rbtree_t        *section_tree; /* no jokes here */
83         rbtree_t        *name2_tree; /* for sections of the same name2 */
84 };
85
86
87 /*
88  *      Internal data that is associated with a configuration section,
89  *      so that we don't have to track it separately.
90  */
91 struct conf_data {
92         CONF_ITEM  item;
93         const char *name;
94         void       *data;       /* user data */
95         void       (*free)(void *); /* free user data function */
96 };
97
98 /*
99  *      Isolate the scary casts in these tiny provably-safe functions
100  */
101 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
102 {
103         if (ci == NULL)
104                 return NULL;
105         rad_assert(ci->type == CONF_ITEM_PAIR);
106         return (CONF_PAIR *)ci;
107 }
108 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
109 {
110         if (ci == NULL)
111                 return NULL;
112         rad_assert(ci->type == CONF_ITEM_SECTION);
113         return (CONF_SECTION *)ci;
114 }
115 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
116 {
117         if (cp == NULL)
118                 return NULL;
119         return (CONF_ITEM *)cp;
120 }
121 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
122 {
123         if (cs == NULL)
124                 return NULL;
125         return (CONF_ITEM *)cs;
126 }
127
128 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
129 {
130         if (ci == NULL)
131                 return NULL;
132         rad_assert(ci->type == CONF_ITEM_DATA);
133         return (CONF_DATA *)ci;
134 }
135 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
136 {
137         if (cd == NULL)
138                 return NULL;
139         return (CONF_ITEM *)cd;
140 }
141
142 /*
143  *      Create a new CONF_PAIR
144  */
145 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
146                                 LRAD_TOKEN operator, CONF_SECTION *parent)
147 {
148         CONF_PAIR *cp;
149
150         cp = rad_malloc(sizeof(*cp));
151         memset(cp, 0, sizeof(*cp));
152         cp->item.type = CONF_ITEM_PAIR;
153         cp->item.parent = parent;
154         cp->attr = strdup(attr);
155         cp->value = strdup(value);
156         cp->operator = operator;
157
158         return cp;
159 }
160
161 /*
162  *      Free a CONF_PAIR
163  */
164 void cf_pair_free(CONF_PAIR **cp)
165 {
166         if (!cp || !*cp) return;
167
168         if ((*cp)->attr)
169                 free((*cp)->attr);
170         if ((*cp)->value)
171                 free((*cp)->value);
172
173 #ifndef NDEBUG
174         memset(*cp, 0, sizeof(*cp));
175 #endif
176         free(*cp);
177
178         *cp = NULL;
179 }
180
181
182 static void cf_data_free(CONF_DATA **cd)
183 {
184         if (!cd || !*cd) return;
185
186         free((*cd)->name);
187         if (!(*cd)->free) {
188                 free((*cd)->data);
189         } else {
190                 ((*cd)->free)((*cd)->data);
191         }
192
193         *cd = NULL;
194 }
195
196 /*
197  *      rbtree callback function
198  */
199 static int pair_cmp(const void *a, const void *b)
200 {
201         const CONF_PAIR *one = a;
202         const CONF_PAIR *two = b;
203
204         return strcmp(one->attr, two->attr);
205 }
206
207
208 /*
209  *      rbtree callback function
210  */
211 static int section_cmp(const void *a, const void *b)
212 {
213         const CONF_SECTION *one = a;
214         const CONF_SECTION *two = b;
215
216         return strcmp(one->name1, two->name1);
217 }
218
219
220 /*
221  *      rbtree callback function
222  */
223 static int name2_cmp(const void *a, const void *b)
224 {
225         const CONF_SECTION *one = a;
226         const CONF_SECTION *two = b;
227
228         rad_assert(strcmp(one->name1, two->name1) == 0);
229
230         if (!one->name2 && !two->name2) return 0;
231         if (!one->name2) return -1;
232         if (!two->name2) return +1;
233
234         return strcmp(one->name2, two->name2);
235 }
236
237
238 /*
239  *      Free a CONF_SECTION
240  */
241 void cf_section_free(CONF_SECTION **cs)
242 {
243         CONF_ITEM       *ci, *next;
244
245         if (!cs || !*cs) return;
246
247         for (ci = (*cs)->children; ci; ci = next) {
248                 next = ci->next;
249
250                 switch (ci->type) {
251                 case CONF_ITEM_PAIR: {
252                                 CONF_PAIR *pair = cf_itemtopair(ci);
253                                 cf_pair_free(&pair);
254                         }
255                         break;
256
257                 case CONF_ITEM_SECTION: {
258                                 
259                                 CONF_SECTION *section = cf_itemtosection(ci);
260                                 cf_section_free(&section);
261                         }
262                         break;
263
264                 case CONF_ITEM_DATA: {
265                                 CONF_DATA *data = cf_itemtodata(ci);
266                                 cf_data_free(&data);
267                         }
268                         break;
269
270                 default:        /* should really be an error. */
271                         break;
272                 }
273         }
274
275         if ((*cs)->name1)
276                 free((*cs)->name1);
277         if ((*cs)->name2)
278                 free((*cs)->name2);
279         if ((*cs)->pair_tree)
280                 rbtree_free((*cs)->pair_tree);
281         if ((*cs)->section_tree)
282                 rbtree_free((*cs)->section_tree);
283         if ((*cs)->name2_tree)
284                 rbtree_free((*cs)->name2_tree);
285
286         /*
287          * And free the section
288          */
289 #ifndef NDEBUG
290         memset(*cs, 0, sizeof(*cs));
291 #endif
292         free(*cs);
293
294         *cs = NULL;
295 }
296
297
298 /*
299  *      Allocate a CONF_SECTION
300  */
301 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
302                                       CONF_SECTION *parent)
303 {
304         CONF_SECTION    *cs;
305
306         if (name1 == NULL || !name1[0])
307                 name1 = "main";
308
309         cs = rad_malloc(sizeof(*cs));
310         memset(cs, 0, sizeof(*cs));
311         cs->item.type = CONF_ITEM_SECTION;
312         cs->item.parent = parent;
313         cs->name1 = strdup(name1);
314         if (!cs->name1) {
315                 cf_section_free(&cs);
316                 return NULL;
317         }
318         
319         if (name2 && *name2) {
320                 cs->name2 = strdup(name2);
321                 if (!cs->name2) {
322                         cf_section_free(&cs);
323                         return NULL;
324                 }
325         }
326         cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
327         if (!cs->pair_tree) {
328                 cf_section_free(&cs);
329                 return NULL;
330         }
331
332         /*
333          *      Don't create the section tree here, it may not
334          *      be needed.
335          */
336         return cs;
337 }
338
339
340 /*
341  *      Add an item to a configuration section.
342  */
343 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
344 {
345         CONF_ITEM **last;
346
347         /*
348          *      New entries are added at the bottom of the list.
349          */
350         for (last = &(cs->children);
351              (*last) != NULL;
352              last = &((*last)->next)) {
353                 /* nothing */
354         }
355         *last = ci;
356
357         /*
358          *      We may be adding a list, rather than just one element.
359          *      If so, loop over all entries.
360          */
361         for (*last = ci; ci != NULL; ci = ci->next) {
362                 /*
363                  *      For fast lookups, pair's and sections get
364                  *      added to rbtree's.
365                  */
366                 switch (ci->type) {
367                         case CONF_ITEM_PAIR:
368                                 rbtree_insert(cs->pair_tree, ci);
369                                 break;
370                                 
371                         case CONF_ITEM_SECTION: {
372                                 const CONF_SECTION *cs_new = cf_itemtosection(ci);
373                                 
374                                 if (!cs->section_tree) {
375                                         cs->section_tree = rbtree_create(section_cmp, NULL, 0);
376                                         /* ignore any errors */
377                                 }
378                                 
379                                 if (cs->section_tree) {
380                                         rbtree_insert(cs->section_tree, cs_new);
381                                 }
382                                 
383                                 /*
384                                  *      Two names: find the named instance.
385                                  */
386                                 if (cs_new->name2) {
387                                         CONF_SECTION *old_cs;
388                                         
389                                         /*
390                                          *      Find the FIRST
391                                          *      CONF_SECTION having
392                                          *      the given name1, and
393                                          *      create a new tree
394                                          *      under it.
395                                          */
396                                         old_cs = rbtree_finddata(cs->section_tree, cs_new);
397                                         if (!old_cs) return; /* this is a bad error! */
398                                         
399                                         if (!old_cs->name2_tree) {
400                                                 old_cs->name2_tree = rbtree_create(name2_cmp,
401                                                                                    NULL, 0);
402                                         }
403                                         if (old_cs->name2_tree) {
404                                                 rbtree_insert(old_cs->name2_tree, cs_new);
405                                         }
406                                 } /* had a name2 */
407                                 break;
408                         } /* was a section */
409
410                         case CONF_ITEM_DATA:
411                                 /*
412                                  *      Don't do anything special.
413                                  */
414                                 break;
415
416                         default: /* FIXME: assert & error! */
417                                 break;
418
419                 } /* switch over conf types */
420         } /* loop over ci */
421 }
422
423 /*
424  *      Expand the variables in an input string.
425  */
426 static const char *cf_expand_variables(const char *cf, int *lineno,
427                                        const CONF_SECTION *outercs,
428                                        char *output, const char *input)
429 {
430         char *p;
431         const char *end, *ptr;
432         char name[8192];
433         const CONF_SECTION *parentcs;
434
435         /*
436          *      Find the master parent conf section.
437          *      We can't use mainconfig.config, because we're in the
438          *      process of re-building it, and it isn't set up yet...
439          */
440         for (parentcs = outercs;
441              parentcs->item.parent != NULL;
442              parentcs = parentcs->item.parent) {
443                 /* do nothing */
444         }
445
446         p = output;
447         ptr = input;
448         while (*ptr) {
449                 /*
450                  *      Ignore anything other than "${"
451                  */
452                 if ((*ptr == '$') && (ptr[1] == '{')) {
453                         int up;
454                         CONF_PAIR *cp;
455                         const CONF_SECTION *cs;
456
457                         /*
458                          *      FIXME: Add support for ${foo:-bar},
459                          *      like in xlat.c
460                          */
461
462                         /*
463                          *      Look for trailing '}', and log a
464                          *      warning for anything that doesn't match,
465                          *      and exit with a fatal error.
466                          */
467                         end = strchr(ptr, '}');
468                         if (end == NULL) {
469                                 *p = '\0';
470                                 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
471                                        cf, *lineno);
472                                 return NULL;
473                         }
474
475                         ptr += 2;
476
477                         cp = NULL;
478                         up = 0;
479
480                         /*
481                          *      ${.foo} means "foo from the current section"
482                          */
483                         if (*ptr == '.') {
484                                 up = 1;
485                                 cs = outercs;
486                                 ptr++;
487
488                                 /*
489                                  *      ${..foo} means "foo from the section
490                                  *      enclosing this section" (etc.)
491                                  */
492                                 while (*ptr == '.') {
493                                         if (cs->item.parent)
494                                                 cs = cs->item.parent;
495                                         ptr++;
496                                 }
497
498                         } else {
499                                 const char *q;
500                                 /*
501                                  *      ${foo} is local, with
502                                  *      main as lower priority
503                                  */
504                                 cs = outercs;
505
506                                 /*
507                                  *      ${foo.bar.baz} is always rooted
508                                  *      from the top.
509                                  */
510                                 for (q = ptr; *q && q != end; q++) {
511                                         if (*q == '.') {
512                                                 cs = parentcs;
513                                                 up = 1;
514                                                 break;
515                                         }
516                                 }
517                         }
518
519                         while (cp == NULL) {
520                                 char *q;
521                                 /*
522                                  *      Find the next section.
523                                  */
524                                 for (q = name;
525                                      (*ptr != 0) && (*ptr != '.') &&
526                                              (ptr != end);
527                                      q++, ptr++) {
528                                         *q = *ptr;
529                                 }
530                                 *q = '\0';
531
532                                 /*
533                                  *      The character is a '.', find a
534                                  *      section (as the user has given
535                                  *      us a subsection to find)
536                                  */
537                                 if (*ptr == '.') {
538                                         CONF_SECTION *next;
539
540                                         ptr++;  /* skip the period */
541
542                                         /*
543                                          *      Find the sub-section.
544                                          */
545                                         next = cf_section_sub_find(cs, name);
546                                         if (next == NULL) {
547                                                 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
548                                                 return NULL;
549                                         }
550                                         cs = next;
551
552                                 } else { /* no period, must be a conf-part */
553                                         /*
554                                          *      Find in the current referenced
555                                          *      section.
556                                          */
557                                         cp = cf_pair_find(cs, name);
558                                         if (cp == NULL) {
559                                                 /*
560                                                  *      It it was NOT ${..foo}
561                                                  *      then look in the
562                                                  *      top-level config items.
563                                                  */
564                                                 if (!up) cp = cf_pair_find(parentcs, name);
565                                         }
566                                         if (cp == NULL) {
567                                                 radlog(L_ERR, "config: No such entry %s for string %s", name, input);
568                                                 return NULL;
569                                         }
570                                 }
571                         } /* until cp is non-NULL */
572
573                         /*
574                          *  Substitute the value of the variable.
575                          */
576                         strcpy(p, cp->value);
577                         p += strlen(p);
578                         ptr = end + 1;
579
580                 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
581                         char *env;
582
583                         ptr += 5;
584
585                         /*
586                          *      Look for trailing '}', and log a
587                          *      warning for anything that doesn't match,
588                          *      and exit with a fatal error.
589                          */
590                         end = strchr(ptr, '}');
591                         if (end == NULL) {
592                                 *p = '\0';
593                                 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
594                                        cf, *lineno);
595                                 return NULL;
596                         }
597
598                         memcpy(name, ptr, end - ptr);
599                         name[end - ptr] = '\0';
600
601                         /*
602                          *      Get the environment variable.
603                          *      If none exists, then make it an empty string.
604                          */
605                         env = getenv(name);
606                         if (env == NULL) {
607                                 *name = '\0';
608                                 env = name;
609                         }
610
611                         strcpy(p, env);
612                         p += strlen(p);
613                         ptr = end + 1;
614
615                 } else {
616                         /*
617                          *      Copy it over verbatim.
618                          */
619                         *(p++) = *(ptr++);
620                 }
621         } /* loop over all of the input string. */
622
623         *p = '\0';
624
625         return output;
626 }
627
628
629 /*
630  *      Parses an item (not a CONF_ITEM) into the specified format,
631  *      with a default value.
632  *
633  *      Returns -1 on error, 0 for correctly parsed, and 1 if the
634  *      default value was used.  Note that the default value will be
635  *      used ONLY if the CONF_PAIR is NULL.
636  */
637 int cf_item_parse(const CONF_SECTION *cs, const char *name,
638                   int type, void *data, const char *dflt)
639 {
640         int rcode = 0;
641         char **q;
642         const char *value;
643         lrad_ipaddr_t ipaddr;
644         const CONF_PAIR *cp;
645         char ipbuf[128];
646
647         cp = cf_pair_find(cs, name);
648         if (cp) {
649                 value = cp->value;
650
651         } else if (!dflt) {
652                 return 1;       /* nothing to parse, return default value */
653
654         } else {
655                 rcode = 1;
656                 value = dflt;
657         }
658
659         switch (type) {
660         case PW_TYPE_BOOLEAN:
661                 /*
662                  *      Allow yes/no and on/off
663                  */
664                 if ((strcasecmp(value, "yes") == 0) ||
665                     (strcasecmp(value, "on") == 0)) {
666                         *(int *)data = 1;
667                 } else if ((strcasecmp(value, "no") == 0) ||
668                            (strcasecmp(value, "off") == 0)) {
669                         *(int *)data = 0;
670                 } else {
671                         *(int *)data = 0;
672                         radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
673                         return -1;
674                 }
675                 DEBUG2(" %s: %s = %s", cs->name1, name, value);
676                 break;
677                 
678         case PW_TYPE_INTEGER:
679                 *(int *)data = strtol(value, 0, 0);
680                 DEBUG2(" %s: %s = %d",
681                        cs->name1, name,
682                        *(int *)data);
683                 break;
684                 
685         case PW_TYPE_STRING_PTR:
686                 q = (char **) data;
687                 if (*q != NULL) {
688                         free(*q);
689                 }
690                 
691                 /*
692                  *      Expand variables which haven't already been
693                  *      expanded automagically when the configuration
694                  *      file was read.
695                  */
696                 if (value == dflt) {
697                         char buffer[8192];
698
699                         int lineno = cs->item.lineno;
700
701                         /*
702                          *      FIXME: sizeof(buffer)?
703                          */
704                         value = cf_expand_variables("?",
705                                                     &lineno,
706                                                     cs, buffer, value);
707                         if (!value) return -1;
708                 }
709                 
710                 DEBUG2(" %s: %s = \"%s\"",
711                        cs->name1, name,
712                        value ? value : "(null)");
713                 *q = value ? strdup(value) : NULL;
714                 break;
715                 
716         case PW_TYPE_IPADDR:
717                 /*
718                  *      Allow '*' as any address
719                  */
720                 if (strcmp(value, "*") == 0) {
721                         *(uint32_t *) data = htonl(INADDR_ANY);
722                         DEBUG2(" %s: %s = *", cs->name1, name);
723                         break;
724                 }
725                 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
726                         radlog(L_ERR, "Can't find IP address for host %s", value);
727                         return -1;
728                 }
729                 DEBUG2(" %s: %s = %s IP address [%s]",
730                        cs->name1, name, value,
731                        ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
732                 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
733                 break;
734                 
735         case PW_TYPE_IPV6ADDR:
736                 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
737                         radlog(L_ERR, "Can't find IPv6 address for host %s", value);
738                         return -1;
739                 }
740                 DEBUG2(" %s: %s = %s IPv6 address [%s]",
741                        cs->name1, name, value,
742                        ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
743                 memcpy(data, &ipaddr.ipaddr.ip6addr,
744                        sizeof(ipaddr.ipaddr.ip6addr));
745                 break;
746                 
747         default:
748                 radlog(L_ERR, "type %d not supported yet", type);
749                 return -1;
750                 break;
751         } /* switch over variable type */
752         
753         return rcode;
754 }
755
756 /*
757  *      Parse a configuration section into user-supplied variables.
758  */
759 int cf_section_parse(const CONF_SECTION *cs, void *base,
760                      const CONF_PARSER *variables)
761 {
762         int i;
763         void *data;
764
765         /*
766          *      Handle the known configuration parameters.
767          */
768         for (i = 0; variables[i].name != NULL; i++) {
769                 /*
770                  *      Handle subsections specially
771                  */
772                 if (variables[i].type == PW_TYPE_SUBSECTION) {
773                         const CONF_SECTION *subcs;
774                         subcs = cf_section_sub_find(cs, variables[i].name);
775                         
776                         /*
777                          *      If the configuration section is NOT there,
778                          *      then ignore it.
779                          *
780                          *      FIXME! This is probably wrong... we should
781                          *      probably set the items to their default values.
782                          */
783                         if (!subcs) continue;
784
785                         if (!variables[i].data) {
786                                 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
787                                 return -1;
788                         }
789                         
790                         if (cf_section_parse(subcs, base,
791                                              (CONF_PARSER *) variables[i].data) < 0) {
792                                 return -1;
793                         }
794                         continue;
795                 } /* else it's a CONF_PAIR */
796                 
797                 if (variables[i].data) {
798                         data = variables[i].data; /* prefer this. */
799                 } else if (base) {
800                         data = ((char *)base) + variables[i].offset;
801                 } else {
802                         DEBUG2("Internal sanity check 2 failed in cf_section_parse");
803                         return -1;
804                 }
805
806                 /*
807                  *      Parse the pair we found, or a default value.
808                  */
809                 if (cf_item_parse(cs, variables[i].name, variables[i].type,
810                                   data, variables[i].dflt) < 0) {
811                         return -1;
812                 }
813         } /* for all variables in the configuration section */
814
815         return 0;
816 }
817
818 /*
819  *      Used in a few places, so in one function for clarity.
820  */
821 static void cf_fixup_children(CONF_SECTION *cs, CONF_SECTION *is)
822 {
823         /*
824          *      Add the included conf
825          *      to our CONF_SECTION
826          */
827         if (is->children != NULL) {
828                 CONF_ITEM *ci;
829                 
830                 /*
831                  *      Re-write the parent of the
832                  *      moved children to be the
833                  *      upper-layer section.
834                  */
835                 for (ci = is->children; ci; ci = ci->next) {
836                         ci->parent = cs;
837                 }
838                 
839                 /*
840                  *      If there are children, then
841                  *      move them up a layer.
842                  */
843                 if (is->children) {
844                         cf_item_add(cs, is->children);
845                 }
846                 is->children = NULL;
847         }
848         /*
849          *      Always free the section for the
850          *      $INCLUDEd file.
851          */
852         cf_section_free(&is);
853 }
854
855
856 /*
857  *      Read a part of the config file.
858  */
859 static CONF_SECTION *cf_section_read(const char *cf, int *lineno, FILE *fp,
860                                      const char *name1, const char *name2,
861                                      CONF_SECTION *parent)
862 {
863         CONF_SECTION *cs, *css;
864         CONF_PAIR *cpn;
865         char *ptr;
866         const char *value;
867         char buf[8192];
868         char buf1[8192];
869         char buf2[8192];
870         char buf3[8192];
871         int t1, t2, t3;
872         char *cbuf = buf;
873         int len;
874
875         /*
876          *      Ensure that the user can't add CONF_SECTIONs
877          *      with 'internal' names;
878          */
879         if ((name1 != NULL) && (name1[0] == '_')) {
880                 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
881                         cf, *lineno);
882                 return NULL;
883         }
884
885         /*
886          *      Allocate new section.
887          */
888         cs = cf_section_alloc(name1, name2, parent);
889         cs->item.lineno = *lineno;
890
891         /*
892          *      Read, checking for line continuations ('\\' at EOL)
893          */
894         for (;;) {
895                 int eof;
896
897                 /*
898                  *      Get data, and remember if we are at EOF.
899                  */
900                 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
901                 (*lineno)++;
902
903                 len = strlen(cbuf);
904
905                 /*
906                  *      We've filled the buffer, and there isn't
907                  *      a CR in it.  Die!
908                  */
909                 if ((len == sizeof(buf)) &&
910                     (cbuf[len - 1] != '\n')) {
911                         radlog(L_ERR, "%s[%d]: Line too long",
912                                cf, *lineno);
913                         cf_section_free(&cs);
914                         return NULL;
915                 }
916
917                 /*
918                  *  Check for continuations.
919                  */
920                 if (cbuf[len - 1] == '\n') len--;
921
922                 /*
923                  *      Last character is '\\'.  Over-write it,
924                  *      and read another line.
925                  */
926                 if ((len > 0) && (cbuf[len - 1] == '\\')) {
927                         cbuf[len - 1] = '\0';
928                         cbuf += len - 1;
929                         continue;
930                 }
931
932                 /*
933                  *  We're at EOF, and haven't read anything.  Stop.
934                  */
935                 if (eof && (cbuf == buf)) {
936                         break;
937                 }
938
939                 ptr = cbuf = buf;
940                 t1 = gettoken(&ptr, buf1, sizeof(buf1));
941
942                 /*
943                  *      Skip comments and blank lines immediately.
944                  */
945                 if ((*buf1 == '#') || (*buf1 == '\0')) {
946                         continue;
947                 }
948
949                 /*
950                  *      Allow for $INCLUDE files
951                  *
952                  *      This *SHOULD* work for any level include.
953                  *      I really really really hate this file.  -cparker
954                  */
955                 if (strcasecmp(buf1, "$INCLUDE") == 0) {
956                         CONF_SECTION    *is;
957
958                         t2 = getword(&ptr, buf2, sizeof(buf2));
959
960                         value = cf_expand_variables(cf, lineno, cs, buf, buf2);
961                         if (value == NULL) {
962                                 cf_section_free(&cs);
963                                 return NULL;
964                         }
965
966 #ifdef HAVE_DIRENT_H
967                         /*
968                          *      $INCLUDE foo/
969                          *
970                          *      Include ALL non-"dot" files in the directory.
971                          *      careful!
972                          */
973                         if (value[strlen(value) - 1] == '/') {
974                                 DIR             *dir;
975                                 struct dirent   *dp;
976                                 struct stat stat_buf;
977
978                                 DEBUG2( "Config:   including files in directory: %s", value );
979                                 dir = opendir(value);
980                                 if (!dir) {
981                                         radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
982                                                cf, *lineno, value,
983                                                strerror(errno));
984                                         cf_section_free(&cs);
985                                         return NULL;
986                                 }
987
988                                 /*
989                                  *      Read the directory, ignoring "." files.
990                                  */
991                                 while ((dp = readdir(dir)) != NULL) {
992                                         const char *p;
993
994                                         if (dp->d_name[0] == '.') continue;
995
996                                         /*
997                                          *      Check for valid characters
998                                          */
999                                         for (p = dp->d_name; *p != '\0'; p++) {
1000                                                 if (isalpha((int)*p) ||
1001                                                     isdigit((int)*p) ||
1002                                                     (*p == '_') ||
1003                                                     (*p == '.')) continue;
1004                                                 break;
1005                                         }
1006                                         if (*p != '\0') continue;
1007
1008                                         snprintf(buf2, sizeof(buf2), "%s%s",
1009                                                  value, dp->d_name);
1010                                         if ((stat(buf2, &stat_buf) != 0) ||
1011                                             S_ISDIR(stat_buf.st_mode)) continue;
1012                                         if ((is = conf_read(cf, *lineno, buf2, parent)) == NULL) {
1013                                                 closedir(dir);
1014                                                 cf_section_free(&cs);
1015                                                 return NULL;
1016                                         }
1017                                         
1018                                         cf_fixup_children(cs, is);
1019                                 }
1020                                 closedir(dir);
1021                         }  else
1022 #endif
1023                         { /* it was a normal file */
1024                                 DEBUG2( "Config:   including file: %s", value );
1025                                 if ((is = conf_read(cf, *lineno, value, parent)) == NULL) {
1026                                         cf_section_free(&cs);
1027                                         return NULL;
1028                                 }
1029                                 cf_fixup_children(cs, is);
1030                         }
1031                         continue;
1032                 } /* we were in an include */
1033
1034                 /*
1035                  *      No '=': must be a section or sub-section.
1036                  */
1037                 if (strchr(ptr, '=') == NULL) {
1038                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
1039                         t3 = gettoken(&ptr, buf3, sizeof(buf3));
1040                 } else {
1041                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
1042                         t3 = getword(&ptr, buf3, sizeof(buf3));
1043                 }
1044
1045                 /*
1046                  *      See if it's the end of a section.
1047                  */
1048                 if (t1 == T_RCBRACE) {
1049                         if (name1 == NULL || buf2[0]) {
1050                                 radlog(L_ERR, "%s[%d]: Unexpected end of section",
1051                                                 cf, *lineno);
1052                                 cf_section_free(&cs);
1053                                 return NULL;
1054                         }
1055                         return cs;
1056                 }
1057
1058                 /*
1059                  * Perhaps a subsection.
1060                  */
1061                 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1062                         css = cf_section_read(cf, lineno, fp, buf1,
1063                                               t2==T_LCBRACE ? NULL : buf2, cs);
1064                         if (css == NULL) {
1065                                 cf_section_free(&cs);
1066                                 return NULL;
1067                         }
1068                         cf_item_add(cs, cf_sectiontoitem(css));
1069
1070                         continue;
1071                 }
1072
1073                 /*
1074                  *      Ignore semi-colons.
1075                  */
1076                 if (*buf2 == ';')
1077                         *buf2 = '\0';
1078
1079                 /*
1080                  *      Must be a normal attr = value line.
1081                  */
1082                 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1083                         t2 = T_OP_EQ;
1084                 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1085                            (t2 < T_EQSTART || t2 > T_EQEND)) {
1086                         radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1087                                         cf, *lineno);
1088                         cf_section_free(&cs);
1089                         return NULL;
1090                 }
1091
1092                 /*
1093                  *      Ensure that the user can't add CONF_PAIRs
1094                  *      with 'internal' names;
1095                  */
1096                 if (buf1[0] == '_') {
1097                         radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1098                                         cf, *lineno, buf1);
1099                         cf_section_free(&cs);
1100                         return NULL;
1101                 }
1102
1103                 /*
1104                  *      Handle variable substitution via ${foo}
1105                  */
1106                 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
1107                 if (!value) {
1108                         cf_section_free(&cs);
1109                         return NULL;
1110                 }
1111
1112
1113                 /*
1114                  *      Add this CONF_PAIR to our CONF_SECTION
1115                  */
1116                 cpn = cf_pair_alloc(buf1, value, t2, parent);
1117                 cpn->item.lineno = *lineno;
1118                 cf_item_add(cs, cf_pairtoitem(cpn));
1119         }
1120
1121         /*
1122          *      See if EOF was unexpected ..
1123          */
1124         if (name1 != NULL) {
1125                 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
1126                 cf_section_free(&cs);
1127                 return NULL;
1128         }
1129
1130         return cs;
1131 }
1132
1133 /*
1134  *      Read the config file.
1135  */
1136 CONF_SECTION *conf_read(const char *fromfile, int fromline,
1137                         const char *conffile, CONF_SECTION *parent)
1138 {
1139         FILE            *fp;
1140         int             lineno = 0;
1141         CONF_SECTION    *cs;
1142
1143         if ((fp = fopen(conffile, "r")) == NULL) {
1144                 if (fromfile) {
1145                         radlog(L_ERR|L_CONS, "%s[%d]: Unable to open file \"%s\": %s",
1146                                         fromfile, fromline, conffile, strerror(errno));
1147                 } else {
1148                         radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1149                                         conffile, strerror(errno));
1150                 }
1151                 return NULL;
1152         }
1153
1154         cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
1155
1156         fclose(fp);
1157
1158         return cs;
1159 }
1160
1161
1162 /*
1163  * Return a CONF_PAIR within a CONF_SECTION.
1164  */
1165 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1166 {
1167         CONF_ITEM       *ci;
1168
1169         if (!cs) cs = mainconfig.config;
1170
1171         /*
1172          *      Find the name in the tree, for speed.
1173          */
1174         if (name) {
1175                 CONF_PAIR mycp;
1176
1177                 mycp.attr = name;
1178                 return rbtree_finddata(cs->pair_tree, &mycp);
1179         }
1180
1181         /*
1182          *      Else find the first one
1183          */
1184         for (ci = cs->children; ci; ci = ci->next) {
1185                 if (ci->type == CONF_ITEM_PAIR)
1186                         return cf_itemtopair(ci);
1187         }
1188         
1189         return NULL;
1190 }
1191
1192 /*
1193  * Return the attr of a CONF_PAIR
1194  */
1195
1196 char *cf_pair_attr(CONF_PAIR *pair)
1197 {
1198         return (pair ? pair->attr : NULL);
1199 }
1200
1201 /*
1202  * Return the value of a CONF_PAIR
1203  */
1204
1205 char *cf_pair_value(CONF_PAIR *pair)
1206 {
1207         return (pair ? pair->value : NULL);
1208 }
1209
1210 /*
1211  * Return the first label of a CONF_SECTION
1212  */
1213
1214 const char *cf_section_name1(const CONF_SECTION *cs)
1215 {
1216         return (cs ? cs->name1 : NULL);
1217 }
1218
1219 /*
1220  * Return the second label of a CONF_SECTION
1221  */
1222
1223 const char *cf_section_name2(const CONF_SECTION *cs)
1224 {
1225         return (cs ? cs->name2 : NULL);
1226 }
1227
1228 /*
1229  * Find a value in a CONF_SECTION
1230  */
1231 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1232 {
1233         CONF_PAIR       *cp;
1234
1235         cp = cf_pair_find(cs, attr);
1236
1237         return (cp ? cp->value : NULL);
1238 }
1239
1240 /*
1241  * Return the next pair after a CONF_PAIR
1242  * with a certain name (char *attr) If the requested
1243  * attr is NULL, any attr matches.
1244  */
1245
1246 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1247                              const CONF_PAIR *pair, const char *attr)
1248 {
1249         CONF_ITEM       *ci;
1250
1251         /*
1252          * If pair is NULL this must be a first time run
1253          * Find the pair with correct name
1254          */
1255
1256         if (pair == NULL){
1257                 return cf_pair_find(cs, attr);
1258         }
1259
1260         ci = cf_pairtoitem(pair)->next;
1261
1262         for (; ci; ci = ci->next) {
1263                 if (ci->type != CONF_ITEM_PAIR)
1264                         continue;
1265                 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1266                         break;
1267         }
1268
1269         return cf_itemtopair(ci);
1270 }
1271
1272 /*
1273  * Find a CONF_SECTION, or return the root if name is NULL
1274  */
1275
1276 CONF_SECTION *cf_section_find(const char *name)
1277 {
1278         if (name)
1279                 return cf_section_sub_find(mainconfig.config, name);
1280         else
1281                 return mainconfig.config;
1282 }
1283
1284 /*
1285  * Find a sub-section in a section
1286  */
1287
1288 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1289 {
1290         CONF_ITEM *ci;
1291
1292         /*
1293          *      Do the fast lookup if possible.
1294          */
1295         if (name && cs->section_tree) {
1296                 CONF_SECTION mycs;
1297
1298                 mycs.name1 = name;
1299                 mycs.name2 = NULL;
1300                 return rbtree_finddata(cs->section_tree, &mycs);
1301         }
1302
1303         for (ci = cs->children; ci; ci = ci->next) {
1304                 if (ci->type != CONF_ITEM_SECTION)
1305                         continue;
1306                 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1307                         break;
1308         }
1309
1310         return cf_itemtosection(ci);
1311
1312 }
1313
1314
1315 /*
1316  *      Find a CONF_SECTION with both names.
1317  */
1318 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1319                                         const char *name1, const char *name2)
1320 {
1321         CONF_ITEM    *ci;
1322
1323         if (!name2) return cf_section_sub_find(cs, name1);
1324
1325         if (!cs) cs = mainconfig.config;
1326
1327         if (name1 && (cs->section_tree)) {
1328                 CONF_SECTION mycs, *master_cs;
1329                 
1330                 mycs.name1 = name1;
1331                 mycs.name2 = name2;
1332                 
1333                 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1334                 if (master_cs) {
1335                         return rbtree_finddata(master_cs->name2_tree, &mycs);
1336                 }
1337         }
1338
1339         /*
1340          *      Else do it the old-fashioned way.
1341          */
1342         for (ci = cs->children; ci; ci = ci->next) {
1343                 CONF_SECTION *subcs;
1344
1345                 if (ci->type != CONF_ITEM_SECTION)
1346                         continue;
1347
1348                 subcs = cf_itemtosection(ci);
1349                 if (!name1) {
1350                         if (!subcs->name2) {
1351                                 if (strcmp(subcs->name1, name2) == 0) break;
1352                         } else {
1353                                 if (strcmp(subcs->name2, name2) == 0) break;
1354                         }
1355                         continue; /* don't do the string comparisons below */
1356                 }
1357
1358                 if ((strcmp(subcs->name1, name1) == 0) &&
1359                     (subcs->name2 != NULL) &&
1360                     (strcmp(subcs->name2, name2) == 0))
1361                         break;
1362         }
1363
1364         return cf_itemtosection(ci);
1365 }
1366
1367 /*
1368  * Return the next subsection after a CONF_SECTION
1369  * with a certain name1 (char *name1). If the requested
1370  * name1 is NULL, any name1 matches.
1371  */
1372
1373 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1374                                       CONF_SECTION *subsection,
1375                                       const char *name1)
1376 {
1377         CONF_ITEM       *ci;
1378
1379         /*
1380          * If subsection is NULL this must be a first time run
1381          * Find the subsection with correct name
1382          */
1383
1384         if (subsection == NULL){
1385                 ci = section->children;
1386         } else {
1387                 ci = cf_sectiontoitem(subsection)->next;
1388         }
1389
1390         for (; ci; ci = ci->next) {
1391                 if (ci->type != CONF_ITEM_SECTION)
1392                         continue;
1393                 if ((name1 == NULL) ||
1394                     (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1395                         break;
1396         }
1397
1398         return cf_itemtosection(ci);
1399 }
1400
1401 /*
1402  * Return the next item after a CONF_ITEM.
1403  */
1404
1405 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1406 {
1407         /*
1408          * If item is NULL this must be a first time run
1409          * Return the first item
1410          */
1411
1412         if (item == NULL) {
1413                 return section->children;
1414         } else {
1415                 return item->next;
1416         }
1417 }
1418
1419 int cf_section_lineno(CONF_SECTION *section)
1420 {
1421         return cf_sectiontoitem(section)->lineno;
1422 }
1423
1424 int cf_pair_lineno(CONF_PAIR *pair)
1425 {
1426         return cf_pairtoitem(pair)->lineno;
1427 }
1428
1429 int cf_item_is_section(CONF_ITEM *item)
1430 {
1431         return item->type == CONF_ITEM_SECTION;
1432 }
1433
1434
1435 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1436                                 void *data, void (*data_free)(void *))
1437 {
1438         CONF_DATA *cd;
1439
1440         cd = rad_malloc(sizeof(*cd));
1441         memset(cd, 0, sizeof(*cd));
1442
1443         cd->item.type = CONF_ITEM_DATA;
1444         cd->item.parent = parent;
1445         cd->name = strdup(name);
1446         cd->data = data;
1447         cd->free = data_free;
1448
1449         return cd;
1450 }
1451
1452
1453 /*
1454  *      Find data from a particular section.
1455  */
1456 void *cf_data_find(CONF_SECTION *cs, const char *name)
1457 {
1458         CONF_ITEM *ci;
1459
1460         if (!cs || !name) return NULL;
1461
1462         for (ci = cs->children; ci != NULL; ci = ci->next) {
1463                 CONF_DATA *cd;
1464
1465                 /*
1466                  *      Data is always inserted at the front of the
1467                  *      list.
1468                  */
1469                 if (ci->type != CONF_ITEM_DATA) return NULL;
1470
1471                 cd = cf_itemtodata(ci);
1472                 if (strcmp(name, cd->name) == 0) return cd->data;
1473         }
1474
1475         return NULL;
1476 }
1477
1478
1479 /*
1480  *      Add named data to a configuration section.
1481  */
1482 int cf_data_add(CONF_SECTION *cs, const char *name,
1483                 void *data, void (*data_free)(void *))
1484 {
1485         CONF_DATA *cd;
1486
1487         if (!cs || !name || !data) return -1;
1488
1489         /*
1490          *      Already exists.  Can't add it.
1491          */
1492         if (cf_data_find(cs, name) != NULL) return -1;
1493
1494         cd = cf_data_alloc(cs, name, data, data_free);
1495         if (!cd) return -1;
1496
1497         cf_item_add(cs, cf_datatoitem(cd));
1498
1499         return 0;
1500 }
1501
1502
1503 #if 0
1504 /*
1505  * JMG dump_config tries to dump the config structure in a readable format
1506  *
1507 */
1508
1509 static int dump_config_section(CONF_SECTION *cs, int indent)
1510 {
1511         CONF_SECTION    *scs;
1512         CONF_PAIR       *cp;
1513         CONF_ITEM       *ci;
1514
1515         /* The DEBUG macro doesn't let me
1516          *   for(i=0;i<indent;++i) debugputchar('\t');
1517          * so I had to get creative. --Pac. */
1518
1519         for (ci = cs->children; ci; ci = ci->next) {
1520                 switch (ci->type) {
1521                 case CONF_ITEM_PAIR:
1522                         cp=cf_itemtopair(ci);
1523                         DEBUG("%.*s%s = %s",
1524                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1525                                 cp->attr, cp->value);
1526                         break;
1527
1528                 case CONF_ITEM_SECTION:
1529                         scs=cf_itemtosection(ci);
1530                         DEBUG("%.*s%s %s%s{",
1531                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1532                                 scs->name1,
1533                                 scs->name2 ? scs->name2 : "",
1534                                 scs->name2 ?  " " : "");
1535                         dump_config_section(scs, indent+1);
1536                         DEBUG("%.*s}",
1537                                 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1538                         break;
1539
1540                 default:        /* FIXME: Do more! */
1541                         break;
1542                 }
1543         }
1544
1545         return 0;
1546 }
1547
1548 int dump_config(void)
1549 {
1550         return dump_config_section(mainconfig.config, 0);
1551 }
1552 #endif