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