Rename functions in pair.c to be consistent with the established naming scheme
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  *
24  * Copyright 2000,2006  The FreeRADIUS server project
25  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
26  * Copyright 2000  Alan DeKok <aland@ox.org>
27  */
28
29 RCSID("$Id$")
30
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/parser.h>
33 #include <freeradius-devel/rad_assert.h>
34
35 #ifdef HAVE_DIRENT_H
36 #include <dirent.h>
37 #endif
38
39 #ifdef HAVE_SYS_STAT_H
40 #include <sys/stat.h>
41 #endif
42
43 #include <ctype.h>
44
45 bool check_config = false;
46
47 typedef enum conf_property {
48         CONF_PROPERTY_INVALID = 0,
49         CONF_PROPERTY_NAME,
50         CONF_PROPERTY_INSTANCE,
51 } CONF_PROPERTY;
52
53 static const FR_NAME_NUMBER conf_property_name[] = {
54         { "name",       CONF_PROPERTY_NAME},
55         { "instance",   CONF_PROPERTY_INSTANCE},
56
57         {  NULL , -1 }
58 };
59
60 typedef enum conf_type {
61         CONF_ITEM_INVALID = 0,
62         CONF_ITEM_PAIR,
63         CONF_ITEM_SECTION,
64         CONF_ITEM_DATA
65 } CONF_ITEM_TYPE;
66
67 struct conf_item {
68         struct conf_item *next;         //!< Sibling.
69         struct conf_part *parent;       //!< Parent.
70         int lineno;                     //!< The line number the config item began on.
71         char const *filename;           //!< The file the config item was parsed from.
72         CONF_ITEM_TYPE type;            //!< Whether the config item is a config_pair, conf_section or conf_data.
73 };
74
75 /** Configuration AVP similar to a VALUE_PAIR
76  *
77  */
78 struct conf_pair {
79         CONF_ITEM       item;
80         char const      *attr;          //!< Attribute name
81         char const      *value;         //!< Attribute value
82         FR_TOKEN        op;             //!< Operator e.g. =, :=
83         FR_TOKEN        lhs_type;       //!< Name quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
84         FR_TOKEN        rhs_type;       //!< Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
85         bool            pass2;          //!< do expansion in pass2.
86         bool            parsed;         //!< Was this item used during parsing?
87 };
88
89 /** Internal data that is associated with a configuration section
90  *
91  */
92 struct conf_data {
93         CONF_ITEM       item;
94         char const      *name;
95         int             flag;
96         void            *data;          //!< User data
97         void            (*free)(void *);        //!< Free user data function
98 };
99
100 struct conf_part {
101         CONF_ITEM       item;
102         char const      *name1;         //!< First name token.  Given ``foo bar {}`` would be ``foo``.
103         char const      *name2;         //!< Second name token. Given ``foo bar {}`` would be ``bar``.
104
105         FR_TOKEN        name2_type;     //!< The type of quoting around name2.
106
107         CONF_ITEM       *children;
108         CONF_ITEM       *tail;          //!< For speed.
109         CONF_SECTION    *template;
110
111         rbtree_t        *pair_tree;     //!< and a partridge..
112         rbtree_t        *section_tree;  //!< no jokes here.
113         rbtree_t        *name2_tree;    //!< for sections of the same name2
114         rbtree_t        *data_tree;
115
116         void            *base;
117         int             depth;
118
119         CONF_PARSER const *variables;
120 };
121
122 typedef struct cf_file_t {
123         char const      *filename;
124         CONF_SECTION    *cs;
125         bool            input;
126         struct stat     buf;
127 } cf_file_t;
128
129 CONF_SECTION *root_config = NULL;
130 bool cf_new_escape = false;
131
132
133 static int              cf_data_add_internal(CONF_SECTION *cs, char const *name, void *data,
134                                              void (*data_free)(void *), int flag);
135
136 static void             *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag);
137
138 static char const       *cf_expand_variables(char const *cf, int *lineno,
139                                              CONF_SECTION *outercs,
140                                              char *output, size_t outsize,
141                                              char const *input, bool *soft_fail);
142
143 static int cf_file_include(CONF_SECTION *cs, char const *filename_in);
144
145
146
147 /*
148  *      Isolate the scary casts in these tiny provably-safe functions
149  */
150
151 /** Cast a CONF_ITEM to a CONF_PAIR
152  *
153  */
154 CONF_PAIR *cf_item_to_pair(CONF_ITEM const *ci)
155 {
156         CONF_PAIR *out;
157
158         if (ci == NULL) return NULL;
159
160         rad_assert(ci->type == CONF_ITEM_PAIR);
161
162         memcpy(&out, &ci, sizeof(out));
163         return out;
164 }
165
166 /** Cast a CONF_ITEM to a CONF_SECTION
167  *
168  */
169 CONF_SECTION *cf_item_to_section(CONF_ITEM const *ci)
170 {
171         CONF_SECTION *out;
172
173         if (ci == NULL) return NULL;
174
175         rad_assert(ci->type == CONF_ITEM_SECTION);
176
177         memcpy(&out, &ci, sizeof(out));
178         return out;
179 }
180
181 /** Cast a CONF_PAIR to a CONF_ITEM
182  *
183  */
184 CONF_ITEM *cf_pair_to_item(CONF_PAIR const *cp)
185 {
186         CONF_ITEM *out;
187
188         if (cp == NULL) return NULL;
189
190         memcpy(&out, &cp, sizeof(out));
191         return out;
192 }
193
194 /** Cast a CONF_SECTION to a CONF_ITEM
195  *
196  */
197 CONF_ITEM *cf_section_to_item(CONF_SECTION const *cs)
198 {
199         CONF_ITEM *out;
200
201         if (cs == NULL) return NULL;
202
203         memcpy(&out, &cs, sizeof(out));
204         return out;
205 }
206
207 /** Cast CONF_DATA to a CONF_ITEM
208  *
209  */
210 static CONF_ITEM *cf_data_to_item(CONF_DATA const *cd)
211 {
212         CONF_ITEM *out;
213
214         if (cd == NULL) {
215                 return NULL;
216         }
217
218         memcpy(&out, &cd, sizeof(out));
219         return out;
220 }
221
222 static int _cf_data_free(CONF_DATA *cd)
223 {
224         if (cd->free) cd->free(cd->data);
225
226         return 0;
227 }
228
229 /*
230  *      rbtree callback function
231  */
232 static int pair_cmp(void const *a, void const *b)
233 {
234         CONF_PAIR const *one = a;
235         CONF_PAIR const *two = b;
236
237         return strcmp(one->attr, two->attr);
238 }
239
240
241 /*
242  *      rbtree callback function
243  */
244 static int section_cmp(void const *a, void const *b)
245 {
246         CONF_SECTION const *one = a;
247         CONF_SECTION const *two = b;
248
249         return strcmp(one->name1, two->name1);
250 }
251
252
253 /*
254  *      rbtree callback function
255  */
256 static int name2_cmp(void const *a, void const *b)
257 {
258         CONF_SECTION const *one = a;
259         CONF_SECTION const *two = b;
260
261         rad_assert(strcmp(one->name1, two->name1) == 0);
262
263         if (!one->name2 && !two->name2) return 0;
264         if (one->name2 && !two->name2) return -1;
265         if (!one->name2 && two->name2) return +1;
266
267         return strcmp(one->name2, two->name2);
268 }
269
270
271 /*
272  *      rbtree callback function
273  */
274 static int data_cmp(void const *a, void const *b)
275 {
276         int rcode;
277
278         CONF_DATA const *one = a;
279         CONF_DATA const *two = b;
280
281         rcode = one->flag - two->flag;
282         if (rcode != 0) return rcode;
283
284         return strcmp(one->name, two->name);
285 }
286
287 /*
288  *      Functions for tracking filenames.
289  */
290 static int filename_cmp(void const *a, void const *b)
291 {
292         cf_file_t const *one = a;
293         cf_file_t const *two = b;
294
295         if (one->buf.st_dev < two->buf.st_dev) return -1;
296         if (one->buf.st_dev > two->buf.st_dev) return +1;
297
298         if (one->buf.st_ino < two->buf.st_ino) return -1;
299         if (one->buf.st_ino > two->buf.st_ino) return +1;
300
301         return 0;
302 }
303
304 static FILE *cf_file_open(CONF_SECTION *cs, char const *filename)
305 {
306         cf_file_t *file;
307         CONF_DATA *cd;
308         CONF_SECTION *top;
309         rbtree_t *tree;
310         int fd;
311         FILE *fp;
312
313         top = cf_top_section(cs);
314         cd = cf_data_find_internal(top, "filename", 0);
315         if (!cd) return NULL;
316
317         tree = cd->data;
318
319         fp = fopen(filename, "r");
320         if (!fp) {
321                 ERROR("Unable to open file \"%s\": %s",
322                       filename, fr_syserror(errno));
323                 return NULL;
324         }
325
326         fd = fileno(fp);
327
328         file = talloc(tree, cf_file_t);
329         if (!file) {
330                 fclose(fp);
331                 return NULL;
332         }
333
334         file->filename = filename;
335         file->cs = cs;
336         file->input = true;
337
338         if (fstat(fd, &file->buf) == 0) {
339 #ifdef S_IWOTH
340                 if ((file->buf.st_mode & S_IWOTH) != 0) {
341                         ERROR("Configuration file %s is globally writable.  "
342                               "Refusing to start due to insecure configuration.", filename);
343
344                         fclose(fp);
345                         talloc_free(file);
346                         return NULL;
347                 }
348 #endif
349         }
350
351         /*
352          *      We can include the same file twice.  e.g. when it
353          *      contains common definitions, such as for SQL.
354          *
355          *      Though the admin should really use templates for that.
356          */
357         if (!rbtree_insert(tree, file)) {
358                 talloc_free(file);
359         }
360
361         return fp;
362 }
363
364 /*
365  *      Do some checks on the file as an "input" file.  i.e. one read
366  *      by a module.
367  */
368 static bool cf_file_input(CONF_SECTION *cs, char const *filename)
369 {
370         cf_file_t *file;
371         CONF_DATA *cd;
372         CONF_SECTION *top;
373         rbtree_t *tree;
374
375         top = cf_top_section(cs);
376         cd = cf_data_find_internal(top, "filename", 0);
377         if (!cd) return false;
378
379         tree = cd->data;
380
381         file = talloc(tree, cf_file_t);
382         if (!file) return false;
383
384         file->filename = filename;
385         file->cs = cs;
386         file->input = true;
387
388         if (stat(filename, &file->buf) < 0) {
389                 ERROR("Unable to open file \"%s\": %s", filename, fr_syserror(errno));
390                 talloc_free(file);
391                 return false;
392         }
393
394 #ifdef S_IWOTH
395         if ((file->buf.st_mode & S_IWOTH) != 0) {
396                 ERROR("Configuration file %s is globally writable.  "
397                       "Refusing to start due to insecure configuration.", filename);
398                 talloc_free(file);
399                 return false;
400         }
401 #endif
402
403         /*
404          *      It's OK to include the same file twice...
405          */
406         if (!rbtree_insert(tree, file)) {
407                 talloc_free(file);
408         }
409
410         return true;
411
412 }
413
414
415 /*
416  *      Return 0 for keep going, 1 for stop.
417  */
418 static int file_callback(void *ctx, void *data)
419 {
420         int *rcode = ctx;
421         struct stat buf;
422         cf_file_t *file = data;
423
424         /*
425          *      The file doesn't exist or we can no longer read it.
426          */
427         if (stat(file->filename, &buf) < 0) {
428                 *rcode = CF_FILE_ERROR;
429                 return 1;
430         }
431
432         /*
433          *      The file changed, we'll need to re-read it.
434          */
435         if (buf.st_mtime != file->buf.st_mtime) {
436                 /*
437                  *      Set none -> whatever
438                  */
439                 if (*rcode == CF_FILE_NONE) {
440                         if (!file->input) {
441                                 *rcode = CF_FILE_CONFIG;
442                                 return 1;
443                         }
444
445                         *rcode = CF_FILE_MODULE;
446                         return 0;
447
448                 }
449
450                 /*
451                  *      A module WAS changed, but now we discover that
452                  *      a main config file has changed.  We might as
453                  *      well re-load everything.
454                  */
455                 if ((*rcode == CF_FILE_MODULE) && !file->input) {
456                         *rcode = CF_FILE_CONFIG;
457                         return 1;
458                 }
459         }
460
461         return 0;
462 }
463
464
465 /*
466  *      See if any of the files have changed.
467  */
468 int cf_file_changed(CONF_SECTION *cs)
469 {
470         int rcode;
471         CONF_DATA *cd;
472         CONF_SECTION *top;
473         rbtree_t *tree;
474
475         top = cf_top_section(cs);
476         cd = cf_data_find_internal(top, "filename", 0);
477         if (!cd) return true;
478
479         tree = cd->data;
480
481         rcode = CF_FILE_NONE;
482         (void) rbtree_walk(tree, RBTREE_IN_ORDER, file_callback, &rcode);
483
484         return rcode;
485 }
486
487 static int _cf_section_free(CONF_SECTION *cs)
488 {
489         /*
490          *      Name1 and name2 are allocated contiguous with
491          *      cs.
492          */
493         if (cs->pair_tree) {
494                 rbtree_free(cs->pair_tree);
495                 cs->pair_tree = NULL;
496         }
497         if (cs->section_tree) {
498                 rbtree_free(cs->section_tree);
499                 cs->section_tree = NULL;
500         }
501         if (cs->name2_tree) {
502                 rbtree_free(cs->name2_tree);
503                 cs->name2_tree = NULL;
504         }
505         if (cs->data_tree) {
506                 rbtree_free(cs->data_tree);
507                 cs->data_tree = NULL;
508         }
509
510         return 0;
511 }
512
513 /** Allocate a CONF_PAIR
514  *
515  * @param parent CONF_SECTION to hang this CONF_PAIR off of.
516  * @param attr name.
517  * @param value of CONF_PAIR.
518  * @param op T_OP_EQ, T_OP_SET etc.
519  * @param lhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
520  * @param rhs_type T_BARE_WORD, T_DOUBLE_QUOTED_STRING, T_BACK_QUOTED_STRING
521  * @return NULL on error, else a new CONF_SECTION parented by parent.
522  */
523 CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value,
524                          FR_TOKEN op, FR_TOKEN lhs_type, FR_TOKEN rhs_type)
525 {
526         CONF_PAIR *cp;
527
528         rad_assert(fr_equality_op[op] || fr_assignment_op[op]);
529         if (!attr) return NULL;
530
531         cp = talloc_zero(parent, CONF_PAIR);
532         if (!cp) return NULL;
533
534         cp->item.type = CONF_ITEM_PAIR;
535         cp->item.parent = parent;
536         cp->lhs_type = lhs_type;
537         cp->rhs_type = rhs_type;
538         cp->op = op;
539
540         cp->attr = talloc_typed_strdup(cp, attr);
541         if (!cp->attr) {
542         error:
543                 talloc_free(cp);
544                 return NULL;
545         }
546
547         if (value) {
548                 cp->value = talloc_typed_strdup(cp, value);
549                 if (!cp->value) goto error;
550         }
551
552         return cp;
553 }
554
555 /** Duplicate a CONF_PAIR
556  *
557  * @param parent to allocate new pair in.
558  * @param cp to duplicate.
559  * @return NULL on error, else a duplicate of the input pair.
560  */
561 CONF_PAIR *cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp)
562 {
563         CONF_PAIR *new;
564
565         rad_assert(parent);
566         rad_assert(cp);
567
568         new = cf_pair_alloc(parent, cp->attr, cf_pair_value(cp),
569                             cp->op, cp->lhs_type, cp->rhs_type);
570         if (!new) return NULL;
571
572         new->parsed = cp->parsed;
573         new->item.lineno = cp->item.lineno;
574
575         /*
576          *      Avoid mallocs if possible.
577          */
578         if (!cp->item.filename || (strcmp(parent->item.filename, cp->item.filename) == 0)) {
579                 new->item.filename = parent->item.filename;
580         } else {
581                 new->item.filename = talloc_strdup(new, cp->item.filename);
582         }
583
584         return new;
585 }
586
587 /** Add a configuration pair to a section
588  *
589  * @param parent section to add pair to.
590  * @param cp to add.
591  */
592 void cf_pair_add(CONF_SECTION *parent, CONF_PAIR *cp)
593 {
594         cf_item_add(parent, cf_pair_to_item(cp));
595 }
596
597 /** Allocate a CONF_SECTION
598  *
599  * @param parent CONF_SECTION to hang this CONF_SECTION off of.
600  * @param name1 Primary name.
601  * @param name2 Secondary name.
602  * @return NULL on error, else a new CONF_SECTION parented by parent.
603  */
604 CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1, char const *name2)
605 {
606         CONF_SECTION *cs;
607         char buffer[1024];
608
609         if (!name1) return NULL;
610
611         if (name2 && parent) {
612                 if (strchr(name2, '$')) {
613                         name2 = cf_expand_variables(parent->item.filename,
614                                                     &parent->item.lineno,
615                                                     parent,
616                                                     buffer, sizeof(buffer), name2, NULL);
617                         if (!name2) {
618                                 ERROR("Failed expanding section name");
619                                 return NULL;
620                         }
621                 }
622         }
623
624         cs = talloc_zero(parent, CONF_SECTION);
625         if (!cs) return NULL;
626
627         cs->item.type = CONF_ITEM_SECTION;
628         cs->item.parent = parent;
629
630         cs->name1 = talloc_typed_strdup(cs, name1);
631         if (!cs->name1) {
632         error:
633                 talloc_free(cs);
634                 return NULL;
635         }
636
637         if (name2) {
638                 cs->name2 = talloc_typed_strdup(cs, name2);
639                 if (!cs->name2) goto error;
640         }
641
642         cs->pair_tree = rbtree_create(cs, pair_cmp, NULL, 0);
643         if (!cs->pair_tree) goto error;
644
645         talloc_set_destructor(cs, _cf_section_free);
646
647         /*
648          *      Don't create a data tree, it may not be needed.
649          */
650
651         /*
652          *      Don't create the section tree here, it may not
653          *      be needed.
654          */
655
656         if (parent) cs->depth = parent->depth + 1;
657
658         return cs;
659 }
660
661 /** Duplicate a configuration section
662  *
663  * @note recursively duplicates any child sections.
664  * @note does not duplicate any data associated with a section, or its child sections.
665  *
666  * @param parent section (may be NULL).
667  * @param cs to duplicate.
668  * @param name1 of new section.
669  * @param name2 of new section.
670  * @param copy_meta Copy additional meta data for a section (like template, base, depth and variables).
671  * @return a duplicate of the existing section, or NULL on error.
672  */
673 CONF_SECTION *cf_section_dup(CONF_SECTION *parent, CONF_SECTION const *cs,
674                              char const *name1, char const *name2, bool copy_meta)
675 {
676         CONF_SECTION *new, *subcs;
677         CONF_PAIR *cp;
678         CONF_ITEM *ci;
679
680         new = cf_section_alloc(parent, name1, name2);
681
682         if (copy_meta) {
683                 new->template = cs->template;
684                 new->base = cs->base;
685                 new->depth = cs->depth;
686                 new->variables = cs->variables;
687         }
688
689         new->item.lineno = cs->item.lineno;
690
691         if (!cs->item.filename || (parent && (strcmp(parent->item.filename, cs->item.filename) == 0))) {
692                 new->item.filename = parent->item.filename;
693         } else {
694                 new->item.filename = talloc_strdup(new, cs->item.filename);
695         }
696
697         for (ci = cs->children; ci; ci = ci->next) {
698                 switch (ci->type) {
699                 case CONF_ITEM_SECTION:
700                         subcs = cf_item_to_section(ci);
701                         subcs = cf_section_dup(new, subcs,
702                                                cf_section_name1(subcs), cf_section_name2(subcs),
703                                                copy_meta);
704                         if (!subcs) {
705                                 talloc_free(new);
706                                 return NULL;
707                         }
708                         cf_section_add(new, subcs);
709                         break;
710
711                 case CONF_ITEM_PAIR:
712                         cp = cf_pair_dup(new, cf_item_to_pair(ci));
713                         if (!cp) {
714                                 talloc_free(new);
715                                 return NULL;
716                         }
717                         cf_pair_add(new, cp);
718                         break;
719
720                 case CONF_ITEM_DATA: /* Skip data */
721                         break;
722
723                 case CONF_ITEM_INVALID:
724                         rad_assert(0);
725                 }
726         }
727
728         return new;
729 }
730
731 void cf_section_add(CONF_SECTION *parent, CONF_SECTION *cs)
732 {
733         cf_item_add(parent, &(cs->item));
734 }
735
736 /** Replace pair in a given section with a new pair, of the given value.
737  *
738  * @param cs to replace pair in.
739  * @param cp to replace.
740  * @param value New value to assign to cp.
741  * @return 0 on success, -1 on failure.
742  */
743 int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
744 {
745         CONF_PAIR *newp;
746         CONF_ITEM *ci, *cn, **last;
747
748         newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->lhs_type, cp->rhs_type);
749         if (!newp) return -1;
750
751         ci = &(cp->item);
752         cn = &(newp->item);
753
754         /*
755          *      Find the old one from the linked list, and replace it
756          *      with the new one.
757          */
758         for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
759                 if (*last == ci) {
760                         cn->next = (*last)->next;
761                         *last = cn;
762                         ci->next = NULL;
763                         break;
764                 }
765         }
766
767         rbtree_deletebydata(cs->pair_tree, ci);
768
769         rbtree_insert(cs->pair_tree, cn);
770
771         return 0;
772 }
773
774
775 /*
776  *      Add an item to a configuration section.
777  */
778 void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
779 {
780 #ifndef NDEBUG
781         CONF_ITEM *first = ci;
782 #endif
783
784         rad_assert((void *)cs != (void *)ci);
785
786         if (!cs || !ci) return;
787
788         if (!cs->children) {
789                 rad_assert(cs->tail == NULL);
790                 cs->children = ci;
791         } else {
792                 rad_assert(cs->tail != NULL);
793                 cs->tail->next = ci;
794         }
795
796         /*
797          *      Update the trees (and tail) for each item added.
798          */
799         for (/* nothing */; ci != NULL; ci = ci->next) {
800                 rad_assert(ci->next != first);  /* simple cycle detection */
801
802                 cs->tail = ci;
803
804                 /*
805                  *      For fast lookups, pairs and sections get
806                  *      added to rbtree's.
807                  */
808                 switch (ci->type) {
809                 case CONF_ITEM_PAIR:
810                         if (!rbtree_insert(cs->pair_tree, ci)) {
811                                 CONF_PAIR *cp = cf_item_to_pair(ci);
812
813                                 if (strcmp(cp->attr, "confdir") == 0) break;
814                                 if (!cp->value) break; /* module name, "ok", etc. */
815                         }
816                         break;
817
818                 case CONF_ITEM_SECTION: {
819                         CONF_SECTION *cs_new = cf_item_to_section(ci);
820                         CONF_SECTION *name1_cs;
821
822                         if (!cs->section_tree) {
823                                 cs->section_tree = rbtree_create(cs, section_cmp, NULL, 0);
824                                 if (!cs->section_tree) {
825                                         ERROR("Out of memory");
826                                         fr_exit_now(1);
827                                 }
828                         }
829
830                         name1_cs = rbtree_finddata(cs->section_tree, cs_new);
831                         if (!name1_cs) {
832                                 if (!rbtree_insert(cs->section_tree, cs_new)) {
833                                         ERROR("Failed inserting section into tree");
834                                         fr_exit_now(1);
835                                 }
836                                 break;
837                         }
838
839                         /*
840                          *      We already have a section of
841                          *      this "name1".  Add a new
842                          *      sub-section based on name2.
843                          */
844                         if (!name1_cs->name2_tree) {
845                                 name1_cs->name2_tree = rbtree_create(name1_cs, name2_cmp, NULL, 0);
846                                 if (!name1_cs->name2_tree) {
847                                         ERROR("Out of memory");
848                                         fr_exit_now(1);
849                                 }
850                         }
851
852                         /*
853                          *      We don't care if this fails.
854                          *      If the user tries to create
855                          *      two sections of the same
856                          *      name1/name2, the duplicate
857                          *      section is just silently
858                          *      ignored.
859                          */
860                         rbtree_insert(name1_cs->name2_tree, cs_new);
861                         break;
862                 } /* was a section */
863
864                 case CONF_ITEM_DATA:
865                         if (!cs->data_tree) {
866                                 cs->data_tree = rbtree_create(cs, data_cmp, NULL, 0);
867                         }
868                         if (cs->data_tree) {
869                                 rbtree_insert(cs->data_tree, ci);
870                         }
871                         break;
872
873                 default: /* FIXME: assert & error! */
874                         break;
875
876                 } /* switch over conf types */
877         } /* loop over ci */
878 }
879
880
881 CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
882                              CONF_SECTION *outercs,
883                              char const *ptr)
884 {
885         CONF_PAIR *cp;
886         CONF_SECTION *next;
887         CONF_SECTION const *cs = outercs;
888         char name[8192];
889         char *p;
890
891         if (!cs) goto no_such_item;
892
893         strlcpy(name, ptr, sizeof(name));
894         p = name;
895
896         /*
897          *      ".foo" means "foo from the current section"
898          */
899         if (*p == '.') {
900                 p++;
901
902                 /*
903                  *      Just '.' means the current section
904                  */
905                 if (*p == '\0') {
906                         return cf_section_to_item(cs);
907                 }
908
909                 /*
910                  *      ..foo means "foo from the section
911                  *      enclosing this section" (etc.)
912                  */
913                 while (*p == '.') {
914                         if (cs->item.parent) {
915                                 cs = cs->item.parent;
916                         }
917
918                         /*
919                          *      .. means the section
920                          *      enclosing this section
921                          */
922                         if (!*++p) {
923                                 return cf_section_to_item(cs);
924                         }
925                 }
926
927                 /*
928                  *      "foo.bar.baz" means "from the root"
929                  */
930         } else if (strchr(p, '.') != NULL) {
931                 if (!parentcs) goto no_such_item;
932
933                 cs = parentcs;
934         }
935
936         while (*p) {
937                 char *q, *r;
938
939                 r = strchr(p, '[');
940                 q = strchr(p, '.');
941                 if (!r && !q) break;
942
943                 if (r && q > r) q = NULL;
944                 if (q && q < r) r = NULL;
945
946                 /*
947                  *      Split off name2.
948                  */
949                 if (r) {
950                         q = strchr(r + 1, ']');
951                         if (!q) return NULL; /* parse error */
952
953                         /*
954                          *      Points to foo[bar]xx: parse error,
955                          *      it should be foo[bar] or foo[bar].baz
956                          */
957                         if (q[1] && q[1] != '.') goto no_such_item;
958
959                         *r = '\0';
960                         *q = '\0';
961                         next = cf_section_sub_find_name2(cs, p, r + 1);
962                         *r = '[';
963                         *q = ']';
964
965                         /*
966                          *      Points to a named instance of a section.
967                          */
968                         if (!q[1]) {
969                                 if (!next) goto no_such_item;
970                                 return &(next->item);
971                         }
972
973                         q++;    /* ensure we skip the ']' and '.' */
974
975                 } else {
976                         *q = '\0';
977                         next = cf_section_sub_find(cs, p);
978                         *q = '.';
979                 }
980
981                 if (!next) break; /* it MAY be a pair in this section! */
982
983                 cs = next;
984                 p = q + 1;
985         }
986
987         if (!*p) goto no_such_item;
988
989  retry:
990         /*
991          *      Find it in the current referenced
992          *      section.
993          */
994         cp = cf_pair_find(cs, p);
995         if (cp) {
996                 cp->parsed = true;      /* conf pairs which are referenced count as parsed */
997                 return &(cp->item);
998         }
999
1000         next = cf_section_sub_find(cs, p);
1001         if (next) return &(next->item);
1002
1003         /*
1004          *      "foo" is "in the current section, OR in main".
1005          */
1006         if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
1007                 cs = parentcs;
1008                 goto retry;
1009         }
1010
1011 no_such_item:
1012         return NULL;
1013 }
1014
1015
1016 CONF_SECTION *cf_top_section(CONF_SECTION *cs)
1017 {
1018         if (!cs) return NULL;
1019
1020         while (cs->item.parent != NULL) {
1021                 cs = cs->item.parent;
1022         }
1023
1024         return cs;
1025 }
1026
1027
1028 /*
1029  *      Expand the variables in an input string.
1030  */
1031 static char const *cf_expand_variables(char const *cf, int *lineno,
1032                                        CONF_SECTION *outercs,
1033                                        char *output, size_t outsize,
1034                                        char const *input, bool *soft_fail)
1035 {
1036         char *p;
1037         char const *end, *ptr;
1038         CONF_SECTION const *parentcs;
1039         char name[8192];
1040
1041         if (soft_fail) *soft_fail = false;
1042
1043         /*
1044          *      Find the master parent conf section.
1045          *      We can't use main_config.config, because we're in the
1046          *      process of re-building it, and it isn't set up yet...
1047          */
1048         parentcs = cf_top_section(outercs);
1049
1050         p = output;
1051         ptr = input;
1052         while (*ptr) {
1053                 /*
1054                  *      Ignore anything other than "${"
1055                  */
1056                 if ((*ptr == '$') && (ptr[1] == '{')) {
1057                         CONF_ITEM *ci;
1058                         CONF_PAIR *cp;
1059                         char *q;
1060
1061                         /*
1062                          *      FIXME: Add support for ${foo:-bar},
1063                          *      like in xlat.c
1064                          */
1065
1066                         /*
1067                          *      Look for trailing '}', and log a
1068                          *      warning for anything that doesn't match,
1069                          *      and exit with a fatal error.
1070                          */
1071                         end = strchr(ptr, '}');
1072                         if (end == NULL) {
1073                                 *p = '\0';
1074                                 INFO("%s[%d]: Variable expansion missing }",
1075                                        cf, *lineno);
1076                                 return NULL;
1077                         }
1078
1079                         ptr += 2;
1080
1081                         /*
1082                          *      Can't really happen because input lines are
1083                          *      capped at 8k, which is sizeof(name)
1084                          */
1085                         if ((size_t) (end - ptr) >= sizeof(name)) {
1086                                 ERROR("%s[%d]: Reference string is too large",
1087                                       cf, *lineno);
1088                                 return NULL;
1089                         }
1090
1091                         memcpy(name, ptr, end - ptr);
1092                         name[end - ptr] = '\0';
1093
1094                         q = strchr(name, ':');
1095                         if (q) {
1096                                 *(q++) = '\0';
1097                         }
1098
1099                         ci = cf_reference_item(parentcs, outercs, name);
1100                         if (!ci) {
1101                                 if (soft_fail) *soft_fail = true;
1102                                 ERROR("%s[%d]: Reference \"${%s}\" not found", cf, *lineno, name);
1103                                 return NULL;
1104                         }
1105
1106                         /*
1107                          *      The expansion doesn't refer to another item or section
1108                          *      it's the property of a section.
1109                          */
1110                         if (q) {
1111                                 CONF_SECTION *mycs = cf_item_to_section(ci);
1112
1113                                 if (ci->type != CONF_ITEM_SECTION) {
1114                                         ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
1115                                         return NULL;
1116                                 }
1117
1118                                 switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
1119                                 case CONF_PROPERTY_NAME:
1120                                         strcpy(p, mycs->name1);
1121                                         break;
1122
1123                                 case CONF_PROPERTY_INSTANCE:
1124                                         strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
1125                                         break;
1126
1127                                 default:
1128                                         ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
1129                                         return NULL;
1130                                 }
1131                                 p += strlen(p);
1132                                 ptr = end + 1;
1133
1134                         } else if (ci->type == CONF_ITEM_PAIR) {
1135                                 /*
1136                                  *  Substitute the value of the variable.
1137                                  */
1138                                 cp = cf_item_to_pair(ci);
1139
1140                                 /*
1141                                  *      If the thing we reference is
1142                                  *      marked up as being expanded in
1143                                  *      pass2, don't expand it now.
1144                                  *      Let it be expanded in pass2.
1145                                  */
1146                                 if (cp->pass2) {
1147                                         if (soft_fail) *soft_fail = true;
1148
1149                                         ERROR("%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
1150                                               cf, *lineno, input);
1151                                         return NULL;
1152                                 }
1153
1154                                 if (!cp->value) {
1155                                         ERROR("%s[%d]: Reference \"%s\" has no value",
1156                                                cf, *lineno, input);
1157                                         return NULL;
1158                                 }
1159
1160                                 if (p + strlen(cp->value) >= output + outsize) {
1161                                         ERROR("%s[%d]: Reference \"%s\" is too long",
1162                                                cf, *lineno, input);
1163                                         return NULL;
1164                                 }
1165
1166                                 strcpy(p, cp->value);
1167                                 p += strlen(p);
1168                                 ptr = end + 1;
1169
1170                         } else if (ci->type == CONF_ITEM_SECTION) {
1171                                 CONF_SECTION *subcs;
1172
1173                                 /*
1174                                  *      Adding an entry again to a
1175                                  *      section is wrong.  We don't
1176                                  *      want an infinite loop.
1177                                  */
1178                                 if (ci->parent == outercs) {
1179                                         ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
1180                                         return NULL;
1181                                 }
1182
1183                                 /*
1184                                  *      Copy the section instead of
1185                                  *      referencing it.
1186                                  */
1187                                 subcs = cf_item_to_section(ci);
1188                                 subcs = cf_section_dup(outercs, subcs,
1189                                                        cf_section_name1(subcs), cf_section_name2(subcs),
1190                                                        false);
1191                                 if (!subcs) {
1192                                         ERROR("%s[%d]: Failed copying reference %s", cf, *lineno, name);
1193                                         return NULL;
1194                                 }
1195
1196                                 subcs->item.filename = ci->filename;
1197                                 subcs->item.lineno = ci->lineno;
1198                                 cf_item_add(outercs, &(subcs->item));
1199
1200                                 ptr = end + 1;
1201
1202                         } else {
1203                                 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
1204                                 return NULL;
1205                         }
1206                 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
1207                         char *env;
1208
1209                         ptr += 5;
1210
1211                         /*
1212                          *      Look for trailing '}', and log a
1213                          *      warning for anything that doesn't match,
1214                          *      and exit with a fatal error.
1215                          */
1216                         end = strchr(ptr, '}');
1217                         if (end == NULL) {
1218                                 *p = '\0';
1219                                 INFO("%s[%d]: Environment variable expansion missing }",
1220                                        cf, *lineno);
1221                                 return NULL;
1222                         }
1223
1224                         /*
1225                          *      Can't really happen because input lines are
1226                          *      capped at 8k, which is sizeof(name)
1227                          */
1228                         if ((size_t) (end - ptr) >= sizeof(name)) {
1229                                 ERROR("%s[%d]: Environment variable name is too large",
1230                                        cf, *lineno);
1231                                 return NULL;
1232                         }
1233
1234                         memcpy(name, ptr, end - ptr);
1235                         name[end - ptr] = '\0';
1236
1237                         /*
1238                          *      Get the environment variable.
1239                          *      If none exists, then make it an empty string.
1240                          */
1241                         env = getenv(name);
1242                         if (env == NULL) {
1243                                 *name = '\0';
1244                                 env = name;
1245                         }
1246
1247                         if (p + strlen(env) >= output + outsize) {
1248                                 ERROR("%s[%d]: Reference \"%s\" is too long",
1249                                        cf, *lineno, input);
1250                                 return NULL;
1251                         }
1252
1253                         strcpy(p, env);
1254                         p += strlen(p);
1255                         ptr = end + 1;
1256
1257                 } else {
1258                         /*
1259                          *      Copy it over verbatim.
1260                          */
1261                         *(p++) = *(ptr++);
1262                 }
1263
1264
1265                 if (p >= (output + outsize)) {
1266                         ERROR("%s[%d]: Reference \"%s\" is too long",
1267                                cf, *lineno, input);
1268                         return NULL;
1269                 }
1270         } /* loop over all of the input string. */
1271
1272         *p = '\0';
1273
1274         return output;
1275 }
1276
1277 static char const parse_spaces[] = "                                                                                                                                                                                                                                                                ";
1278
1279 /** Validation function for ipaddr conffile types
1280  *
1281  */
1282 static inline int fr_item_validate_ipaddr(CONF_SECTION *cs, char const *name, PW_TYPE type, char const *value,
1283                                           fr_ipaddr_t *ipaddr)
1284 {
1285         char ipbuf[128];
1286
1287         if (strcmp(value, "*") == 0) {
1288                 cf_log_info(cs, "%.*s\t%s = *", cs->depth, parse_spaces, name);
1289         } else if (strspn(value, ".0123456789abdefABCDEF:%[]/") == strlen(value)) {
1290                 cf_log_info(cs, "%.*s\t%s = %s", cs->depth, parse_spaces, name, value);
1291         } else {
1292                 cf_log_info(cs, "%.*s\t%s = %s IPv%s address [%s]", cs->depth, parse_spaces, name, value,
1293                             (ipaddr->af == AF_INET ? "4" : " 6"), ip_ntoh(ipaddr, ipbuf, sizeof(ipbuf)));
1294         }
1295
1296         switch (type) {
1297         case PW_TYPE_IPV4_ADDR:
1298         case PW_TYPE_IPV6_ADDR:
1299         case PW_TYPE_COMBO_IP_ADDR:
1300                 switch (ipaddr->af) {
1301                 case AF_INET:
1302                 if (ipaddr->prefix != 32) {
1303                         ERROR("Invalid IPv4 mask length \"/%i\".  Only \"/32\" permitted for non-prefix types",
1304                               ipaddr->prefix);
1305
1306                         return -1;
1307                 }
1308                         break;
1309
1310                 case AF_INET6:
1311                 if (ipaddr->prefix != 128) {
1312                         ERROR("Invalid IPv6 mask length \"/%i\".  Only \"/128\" permitted for non-prefix types",
1313                               ipaddr->prefix);
1314
1315                         return -1;
1316                 }
1317                         break;
1318
1319                 default:
1320                         return -1;
1321                 }
1322         default:
1323                 return 0;
1324         }
1325 }
1326
1327 /** Parses a #CONF_PAIR into a C data type, with a default value.
1328  *
1329  * Takes fields from a #CONF_PARSER struct and uses them to parse the string value
1330  * of a #CONF_PAIR into a C data type matching the type argument.
1331  *
1332  * The format of the types are the same as #value_data_t types.
1333  *
1334  * @note The dflt value will only be used if no matching #CONF_PAIR is found. Empty strings will not
1335  *       result in the dflt value being used.
1336  *
1337  * **PW_TYPE to data type mappings**
1338  * | PW_TYPE                 | Data type          | Dynamically allocated  |
1339  * | ----------------------- | ------------------ | ---------------------- |
1340  * | PW_TYPE_TMPL            | ``vp_tmpl_t``      | Yes                    |
1341  * | PW_TYPE_BOOLEAN         | ``bool``           | No                     |
1342  * | PW_TYPE_INTEGER         | ``uint32_t``       | No                     |
1343  * | PW_TYPE_SHORT           | ``uint16_t``       | No                     |
1344  * | PW_TYPE_INTEGER64       | ``uint64_t``       | No                     |
1345  * | PW_TYPE_SIGNED          | ``int32_t``        | No                     |
1346  * | PW_TYPE_STRING          | ``char const *``   | Yes                    |
1347  * | PW_TYPE_IPV4_ADDR       | ``fr_ipaddr_t``    | No                     |
1348  * | PW_TYPE_IPV4_PREFIX     | ``fr_ipaddr_t``    | No                     |
1349  * | PW_TYPE_IPV6_ADDR       | ``fr_ipaddr_t``    | No                     |
1350  * | PW_TYPE_IPV6_PREFIX     | ``fr_ipaddr_t``    | No                     |
1351  * | PW_TYPE_COMBO_IP_ADDR   | ``fr_ipaddr_t``    | No                     |
1352  * | PW_TYPE_COMBO_IP_PREFIX | ``fr_ipaddr_t``    | No                     |
1353  * | PW_TYPE_TIMEVAL         | ``struct timeval`` | No                     |
1354  *
1355  * @param cs to search for matching #CONF_PAIR in.
1356  * @param name of #CONF_PAIR to search for.
1357  * @param type Data type to parse #CONF_PAIR value as.
1358  *      Should be one of the following ``data`` types, and one or more of the following ``flag`` types or'd together:
1359  *      - ``data`` #PW_TYPE_TMPL                - @copybrief PW_TYPE_TMPL
1360                                                   Feeds the value into #tmpl_afrom_str. Value can be
1361  *                                                obtained when processing requests, with #tmpl_expand or #tmpl_aexpand.
1362  *      - ``data`` #PW_TYPE_BOOLEAN             - @copybrief PW_TYPE_BOOLEAN
1363  *      - ``data`` #PW_TYPE_INTEGER             - @copybrief PW_TYPE_INTEGER
1364  *      - ``data`` #PW_TYPE_SHORT               - @copybrief PW_TYPE_SHORT
1365  *      - ``data`` #PW_TYPE_INTEGER64           - @copybrief PW_TYPE_INTEGER64
1366  *      - ``data`` #PW_TYPE_SIGNED              - @copybrief PW_TYPE_SIGNED
1367  *      - ``data`` #PW_TYPE_STRING              - @copybrief PW_TYPE_STRING
1368  *      - ``data`` #PW_TYPE_IPV4_ADDR           - @copybrief PW_TYPE_IPV4_ADDR (IPv4 address with prefix 32).
1369  *      - ``data`` #PW_TYPE_IPV4_PREFIX         - @copybrief PW_TYPE_IPV4_PREFIX (IPv4 address with variable prefix).
1370  *      - ``data`` #PW_TYPE_IPV6_ADDR           - @copybrief PW_TYPE_IPV6_ADDR (IPv6 address with prefix 128).
1371  *      - ``data`` #PW_TYPE_IPV6_PREFIX         - @copybrief PW_TYPE_IPV6_PREFIX (IPv6 address with variable prefix).
1372  *      - ``data`` #PW_TYPE_COMBO_IP_ADDR       - @copybrief PW_TYPE_COMBO_IP_ADDR (IPv4/IPv6 address with
1373                                                   prefix 32/128).
1374  *      - ``data`` #PW_TYPE_COMBO_IP_PREFIX     - @copybrief PW_TYPE_COMBO_IP_PREFIX (IPv4/IPv6 address with
1375  *                                                variable prefix).
1376  *      - ``data`` #PW_TYPE_TIMEVAL             - @copybrief PW_TYPE_TIMEVAL
1377  *      - ``flag`` #PW_TYPE_DEPRECATED          - @copybrief PW_TYPE_DEPRECATED
1378  *      - ``flag`` #PW_TYPE_REQUIRED            - @copybrief PW_TYPE_REQUIRED
1379  *      - ``flag`` #PW_TYPE_ATTRIBUTE           - @copybrief PW_TYPE_ATTRIBUTE
1380  *      - ``flag`` #PW_TYPE_SECRET              - @copybrief PW_TYPE_SECRET
1381  *      - ``flag`` #PW_TYPE_FILE_INPUT          - @copybrief PW_TYPE_FILE_INPUT
1382  *      - ``flag`` #PW_TYPE_NOT_EMPTY           - @copybrief PW_TYPE_NOT_EMPTY
1383  * @param data Pointer to a global variable, or pointer to a field in the struct being populated with values.
1384  * @param dflt value to use, if no #CONF_PAIR is found.
1385  * @return -1 on error, -2 if deprecated, 0 on success (correctly parsed), 1 if default value was used.
1386  */
1387 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1388 {
1389         int rcode;
1390         bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, multi;
1391         char **q;
1392         char const *value;
1393         CONF_PAIR *cp = NULL;
1394         fr_ipaddr_t *ipaddr;
1395         char buffer[8192];
1396         CONF_ITEM *c_item = &cs->item;
1397
1398         if (!cs) return -1;
1399
1400         deprecated = (type & PW_TYPE_DEPRECATED);
1401         required = (type & PW_TYPE_REQUIRED);
1402         attribute = (type & PW_TYPE_ATTRIBUTE);
1403         secret = (type & PW_TYPE_SECRET);
1404         file_input = (type == PW_TYPE_FILE_INPUT);      /* check, not and */
1405         cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1406         tmpl = (type & PW_TYPE_TMPL);
1407         multi = (type & PW_TYPE_MULTI);
1408
1409         if (attribute) required = true;
1410         if (required) cant_be_empty = true;     /* May want to review this in the future... */
1411
1412         /*
1413          *      Everything except templates must have a base type.
1414          */
1415         if (!(type & 0xff) && !tmpl) {
1416                 cf_log_err(c_item, "Configuration item '%s' must have a data type", name);
1417                 return -1;
1418         }
1419
1420         type &= 0xff;                           /* normal types are small */
1421
1422         rcode = 0;
1423
1424         cp = cf_pair_find(cs, name);
1425
1426         /*
1427          *      No pairs match the configuration item name in the current
1428          *      section, use the default value.
1429          */
1430         if (!cp) {
1431                 rcode = 1;
1432                 value = dflt;
1433         /*
1434          *      Something matched, used the CONF_PAIR value.
1435          */
1436         } else {
1437                 CONF_PAIR *next = cp;
1438                 value = cp->value;
1439                 cp->parsed = true;
1440                 c_item = &cp->item;
1441
1442                 /*
1443                  *      @fixme We should actually validate
1444                  *      the value of the pairs too
1445                  */
1446                 if (multi) while ((next = cf_pair_find_next(cs, next, name))) {
1447                         next->parsed = true;
1448                 }
1449         }
1450
1451         if (!value) {
1452                 if (required) {
1453                 is_required:
1454                         cf_log_err(c_item, "Configuration item '%s' must have a value", name);
1455
1456                         return -1;
1457                 }
1458                 return rcode;
1459         }
1460
1461         if ((value[0] == '\0') && cant_be_empty) {
1462         cant_be_empty:
1463                 cf_log_err(c_item, "Configuration item '%s' must not be empty (zero length)", name);
1464
1465                 if (!required)
1466                         cf_log_err(c_item, "Comment item to silence this message");
1467
1468                 return -1;
1469         }
1470
1471         if (deprecated) {
1472                 cf_log_err(c_item, "Configuration item \"%s\" is deprecated", name);
1473
1474                 return -2;
1475         }
1476
1477         /*
1478          *      Process a value as a LITERAL template.  Once all of
1479          *      the attrs and xlats are defined, the pass2 code
1480          *      converts it to the appropriate type.
1481          */
1482         if (tmpl) {
1483                 vp_tmpl_t *vpt;
1484
1485                 if (!value) {
1486                         *(vp_tmpl_t **)data = NULL;
1487                         return 0;
1488                 }
1489
1490                 rad_assert(!attribute);
1491                 vpt = tmpl_alloc(cs, TMPL_TYPE_LITERAL, value, strlen(value));
1492                 *(vp_tmpl_t **)data = vpt;
1493
1494                 return 0;
1495         }
1496
1497         switch (type) {
1498         case PW_TYPE_BOOLEAN:
1499                 /*
1500                  *      Allow yes/no, true/false, and on/off
1501                  */
1502                 if ((strcasecmp(value, "yes") == 0) ||
1503                     (strcasecmp(value, "true") == 0) ||
1504                     (strcasecmp(value, "on") == 0)) {
1505                         *(bool *)data = true;
1506                 } else if ((strcasecmp(value, "no") == 0) ||
1507                            (strcasecmp(value, "false") == 0) ||
1508                            (strcasecmp(value, "off") == 0)) {
1509                         *(bool *)data = false;
1510                 } else {
1511                         *(bool *)data = false;
1512                         cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1513                                "variable %s", value, name);
1514                         return -1;
1515                 }
1516                 cf_log_info(cs, "%.*s\t%s = %s",
1517                             cs->depth, parse_spaces, name, value);
1518                 break;
1519
1520         case PW_TYPE_INTEGER:
1521         {
1522                 unsigned long v = strtoul(value, 0, 0);
1523
1524                 /*
1525                  *      Restrict integer values to 0-INT32_MAX, this means
1526                  *      it will always be safe to cast them to a signed type
1527                  *      for comparisons, and imposes the same range limit as
1528                  *      before we switched to using an unsigned type to
1529                  *      represent config item integers.
1530                  */
1531                 if (v > INT32_MAX) {
1532                         cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1533                                    name, INT32_MAX);
1534                         return -1;
1535                 }
1536
1537                 *(uint32_t *)data = v;
1538                 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1539         }
1540                 break;
1541
1542         case PW_TYPE_BYTE:
1543         {
1544                 unsigned long v = strtoul(value, 0, 0);
1545
1546                 if (v > UINT8_MAX) {
1547                         cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1548                                    name, UINT8_MAX);
1549                         return -1;
1550                 }
1551                 *(uint8_t *)data = (uint8_t) v;
1552                 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint8_t *)data);
1553         }
1554                 break;
1555
1556         case PW_TYPE_SHORT:
1557         {
1558                 unsigned long v = strtoul(value, 0, 0);
1559
1560                 if (v > UINT16_MAX) {
1561                         cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1562                                    name, UINT16_MAX);
1563                         return -1;
1564                 }
1565                 *(uint16_t *)data = (uint16_t) v;
1566                 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1567         }
1568                 break;
1569
1570         case PW_TYPE_INTEGER64:
1571                 *(uint64_t *)data = strtoull(value, 0, 0);
1572                 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1573                 break;
1574
1575         case PW_TYPE_SIGNED:
1576                 *(int32_t *)data = strtol(value, 0, 0);
1577                 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1578                 break;
1579
1580         case PW_TYPE_STRING:
1581                 q = (char **) data;
1582                 if (*q != NULL) {
1583                         talloc_free(*q);
1584                 }
1585
1586                 /*
1587                  *      Expand variables which haven't already been
1588                  *      expanded automagically when the configuration
1589                  *      file was read.
1590                  */
1591                 if (value == dflt) {
1592                         int lineno = 0;
1593
1594                         lineno = cs->item.lineno;
1595
1596                         value = cf_expand_variables("<internal>",
1597                                                     &lineno,
1598                                                     cs, buffer, sizeof(buffer),
1599                                                     value, NULL);
1600                         if (!value) {
1601                                 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1602                                 return -1;
1603                         }
1604                 }
1605
1606                 if (required && !value) goto is_required;
1607                 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1608
1609                 if (attribute) {
1610                         if (!dict_attrbyname(value)) {
1611                                 if (!cp) {
1612                                         cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1613                                                    value, name);
1614                                 } else {
1615                                         cf_log_err(&(cp->item), "No such attribute '%s'", value);
1616                                 }
1617                                 return -1;
1618                         }
1619                 }
1620
1621                 /*
1622                  *      Hide secrets when using "radiusd -X".
1623                  */
1624                 if (secret && (rad_debug_lvl <= 2)) {
1625                         cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1626                                     cs->depth, parse_spaces, name);
1627                 } else {
1628                         cf_log_info(cs, "%.*s\t%s = \"%s\"",
1629                                     cs->depth, parse_spaces, name, value ? value : "(null)");
1630                 }
1631                 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1632
1633                 /*
1634                  *      If there's data AND it's an input file, check
1635                  *      that we can read it.  This check allows errors
1636                  *      to be caught as early as possible, during
1637                  *      server startup.
1638                  */
1639                 if (*q && file_input && !cf_file_input(cs, *q)) {
1640                         return -1;
1641                 }
1642                 break;
1643
1644         case PW_TYPE_IPV4_ADDR:
1645         case PW_TYPE_IPV4_PREFIX:
1646                 ipaddr = data;
1647
1648                 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1649                         ERROR("%s", fr_strerror());
1650                         return -1;
1651                 }
1652                 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1653                 break;
1654
1655         case PW_TYPE_IPV6_ADDR:
1656         case PW_TYPE_IPV6_PREFIX:
1657                 ipaddr = data;
1658
1659                 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1660                         ERROR("%s", fr_strerror());
1661                         return -1;
1662                 }
1663                 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1664                 break;
1665
1666         case PW_TYPE_COMBO_IP_ADDR:
1667         case PW_TYPE_COMBO_IP_PREFIX:
1668                 ipaddr = data;
1669
1670                 if (fr_pton(ipaddr, value, -1, true) < 0) {
1671                         ERROR("%s", fr_strerror());
1672                         return -1;
1673                 }
1674                 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1675                 break;
1676
1677         case PW_TYPE_TIMEVAL: {
1678                 int sec;
1679                 char *end;
1680                 struct timeval tv;
1681
1682                 sec = strtoul(value, &end, 10);
1683                 tv.tv_sec = sec;
1684                 tv.tv_usec = 0;
1685                 if (*end == '.') {
1686                         size_t len;
1687
1688                         len = strlen(end + 1);
1689
1690                         if (len > 6) {
1691                                 ERROR("Too much precision for timeval");
1692                                 return -1;
1693                         }
1694
1695                         /*
1696                          *      If they write "0.1", that means
1697                          *      "10000" microseconds.
1698                          */
1699                         sec = strtoul(end + 1, NULL, 10);
1700                         while (len < 6) {
1701                                 sec *= 10;
1702                                 len++;
1703                         }
1704
1705                         tv.tv_usec = sec;
1706                 }
1707                 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1708                             cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1709                 memcpy(data, &tv, sizeof(tv));
1710                 }
1711                 break;
1712
1713         default:
1714                 /*
1715                  *      If we get here, it's a sanity check error.
1716                  *      It's not an error parsing the configuration
1717                  *      file.
1718                  */
1719                 rad_assert(type > PW_TYPE_INVALID);
1720                 rad_assert(type < PW_TYPE_MAX);
1721
1722                 ERROR("type '%s' is not supported in the configuration files",
1723                        fr_int2str(dict_attr_types, type, "?Unknown?"));
1724                 return -1;
1725         } /* switch over variable type */
1726
1727         if (!cp) {
1728                 CONF_PAIR *cpn;
1729
1730                 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1731                 if (!cpn) return -1;
1732                 cpn->parsed = true;
1733                 cpn->item.filename = "<internal>";
1734                 cpn->item.lineno = 0;
1735                 cf_item_add(cs, &(cpn->item));
1736         }
1737
1738         return rcode;
1739 }
1740
1741
1742 /*
1743  *      A copy of cf_section_parse that initializes pointers before
1744  *      parsing them.
1745  */
1746 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1747                                   CONF_PARSER const *variables)
1748 {
1749         int i;
1750
1751         for (i = 0; variables[i].name != NULL; i++) {
1752                 if (variables[i].type == PW_TYPE_SUBSECTION) {
1753                         CONF_SECTION *subcs;
1754
1755                         if (!variables[i].dflt) continue;
1756
1757                         subcs = cf_section_sub_find(cs, variables[i].name);
1758
1759                         /*
1760                          *      If there's no subsection in the
1761                          *      config, BUT the CONF_PARSER wants one,
1762                          *      then create an empty one.  This is so
1763                          *      that we can track the strings,
1764                          *      etc. allocated in the subsection.
1765                          */
1766                         if (!subcs) {
1767                                 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1768                                 if (!subcs) return;
1769
1770                                 subcs->item.filename = cs->item.filename;
1771                                 subcs->item.lineno = cs->item.lineno;
1772                                 cf_item_add(cs, &(subcs->item));
1773                         }
1774
1775                         cf_section_parse_init(subcs, (uint8_t *)base + variables[i].offset,
1776                                               (CONF_PARSER const *) variables[i].dflt);
1777                         continue;
1778                 }
1779
1780                 if ((variables[i].type != PW_TYPE_STRING) &&
1781                     (variables[i].type != PW_TYPE_FILE_INPUT) &&
1782                     (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1783                         continue;
1784                 }
1785
1786                 if (variables[i].data) {
1787                         *(char **) variables[i].data = NULL;
1788                 } else if (base) {
1789                         *(char **) (((char *)base) + variables[i].offset) = NULL;
1790                 } else {
1791                         continue;
1792                 }
1793         } /* for all variables in the configuration section */
1794 }
1795
1796
1797 static void cf_section_parse_warn(CONF_SECTION *cs)
1798 {
1799         CONF_ITEM *ci;
1800
1801         for (ci = cs->children; ci; ci = ci->next) {
1802                 /*
1803                  *      Don't recurse on sections. We can only safely
1804                  *      check conf pairs at the same level as the
1805                  *      section that was just parsed.
1806                  */
1807                 if (ci->type == CONF_ITEM_SECTION) continue;
1808                 if (ci->type == CONF_ITEM_PAIR) {
1809                         CONF_PAIR *cp;
1810
1811                         cp = cf_item_to_pair(ci);
1812                         if (cp->parsed) continue;
1813
1814                         WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
1815                              cp->item.filename ? cp->item.filename : "unknown",
1816                              cp->item.lineno ? cp->item.lineno : 0,
1817                                 cp->attr);
1818                 }
1819
1820                 /*
1821                  *      Skip everything else.
1822                  */
1823         }
1824 }
1825
1826 /*
1827  *      Parse a configuration section into user-supplied variables.
1828  */
1829 int cf_section_parse(CONF_SECTION *cs, void *base,
1830                      CONF_PARSER const *variables)
1831 {
1832         int ret;
1833         int i;
1834         void *data;
1835
1836         cs->variables = variables; /* this doesn't hurt anything */
1837
1838         if (!cs->name2) {
1839                 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces,
1840                        cs->name1);
1841         } else {
1842                 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces,
1843                        cs->name1, cs->name2);
1844         }
1845
1846         cf_section_parse_init(cs, base, variables);
1847
1848         /*
1849          *      Handle the known configuration parameters.
1850          */
1851         for (i = 0; variables[i].name != NULL; i++) {
1852                 /*
1853                  *      Handle subsections specially
1854                  */
1855                 if (variables[i].type == PW_TYPE_SUBSECTION) {
1856                         CONF_SECTION *subcs;
1857
1858                         subcs = cf_section_sub_find(cs, variables[i].name);
1859                         /*
1860                          *      Default in this case is overloaded to mean a pointer
1861                          *      to the CONF_PARSER struct for the subsection.
1862                          */
1863                         if (!variables[i].dflt || !subcs) {
1864                                 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1865                                 goto error;
1866                         }
1867
1868                         if (cf_section_parse(subcs, (uint8_t *)base + variables[i].offset,
1869                                              (CONF_PARSER const *) variables[i].dflt) < 0) goto error;
1870                         continue;
1871                 } /* else it's a CONF_PAIR */
1872
1873                 if (variables[i].data) {
1874                         data = variables[i].data; /* prefer this. */
1875                 } else if (base) {
1876                         data = ((char *)base) + variables[i].offset;
1877                 } else {
1878                         DEBUG2("Internal sanity check 2 failed in cf_section_parse");
1879                         goto error;
1880                 }
1881
1882                 /*
1883                  *      Parse the pair we found, or a default value.
1884                  */
1885                 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1886                 if (ret < 0) {
1887                         /*
1888                          *      Be nice, and print the name of the new config item.
1889                          */
1890                         if ((ret == -2) && (variables[i + 1].offset == variables[i].offset) &&
1891                             (variables[i + 1].data == variables[i].data)) {
1892                                 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1893                                            variables[i + 1].name);
1894                         }
1895
1896                         if ((ret == -2) && check_config) continue;
1897
1898                         goto error;
1899                 }
1900         } /* for all variables in the configuration section */
1901
1902         /*
1903          *      Warn about items in the configuration which weren't
1904          *      checked during parsing.
1905          */
1906         if (rad_debug_lvl >= 3) cf_section_parse_warn(cs);
1907
1908         cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1909
1910         cs->base = base;
1911
1912         return 0;
1913
1914  error:
1915         cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1916         return -1;
1917 }
1918
1919
1920 /*
1921  *      Check XLAT things in pass 2.  But don't cache the xlat stuff anywhere.
1922  */
1923 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1924 {
1925         int i;
1926         ssize_t slen;
1927         char const *error;
1928         char *value = NULL;
1929         xlat_exp_t *xlat;
1930
1931         /*
1932          *      Handle the known configuration parameters.
1933          */
1934         for (i = 0; variables[i].name != NULL; i++) {
1935                 CONF_PAIR *cp;
1936                 void *data;
1937
1938                 /*
1939                  *      Handle subsections specially
1940                  */
1941                 if (variables[i].type == PW_TYPE_SUBSECTION) {
1942                         CONF_SECTION *subcs;
1943                         subcs = cf_section_sub_find(cs, variables[i].name);
1944
1945                         if (cf_section_parse_pass2(subcs, (uint8_t *)base + variables[i].offset,
1946                                                    (CONF_PARSER const *) variables[i].dflt) < 0) {
1947                                 return -1;
1948                         }
1949                         continue;
1950                 } /* else it's a CONF_PAIR */
1951
1952                 /*
1953                  *      Figure out which data we need to fix.
1954                  */
1955                 if (variables[i].data) {
1956                         data = variables[i].data; /* prefer this. */
1957                 } else if (base) {
1958                         data = ((char *)base) + variables[i].offset;
1959                 } else {
1960                         data = NULL;
1961                 }
1962
1963                 cp = cf_pair_find(cs, variables[i].name);
1964                 xlat = NULL;
1965
1966         redo:
1967                 if (!cp || !cp->value || !data) continue;
1968
1969                 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1970                     (cp->rhs_type != T_BARE_WORD)) continue;
1971
1972                 /*
1973                  *      Non-xlat expansions shouldn't have xlat!
1974                  */
1975                 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1976                     ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1977                         /*
1978                          *      Ignore %{... in shared secrets.
1979                          *      They're never dynamically expanded.
1980                          */
1981                         if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
1982
1983                         if (strstr(cp->value, "%{") != NULL) {
1984                                 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
1985                                      cp->item.filename ? cp->item.filename : "unknown",
1986                                      cp->item.lineno ? cp->item.lineno : 0);
1987                         }
1988                         continue;
1989                 }
1990
1991                 /*
1992                  *      Parse (and throw away) the xlat string.
1993                  *
1994                  *      FIXME: All of these should be converted from PW_TYPE_XLAT
1995                  *      to PW_TYPE_TMPL.
1996                  */
1997                 if ((variables[i].type & PW_TYPE_XLAT) != 0) {
1998                         /*
1999                          *      xlat expansions should be parseable.
2000                          */
2001                         value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
2002                         xlat = NULL;
2003
2004                         slen = xlat_tokenize(cs, value, &xlat, &error);
2005                         if (slen < 0) {
2006                                 char *spaces, *text;
2007
2008                         error:
2009                                 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
2010
2011                                 cf_log_err(&cp->item, "Failed parsing expanded string:");
2012                                 cf_log_err(&cp->item, "%s", text);
2013                                 cf_log_err(&cp->item, "%s^ %s", spaces, error);
2014
2015                                 talloc_free(spaces);
2016                                 talloc_free(text);
2017                                 talloc_free(value);
2018                                 talloc_free(xlat);
2019                                 return -1;
2020                         }
2021
2022                         talloc_free(value);
2023                         talloc_free(xlat);
2024                 }
2025
2026                 /*
2027                  *      Convert the LITERAL template to the actual
2028                  *      type.
2029                  */
2030                 if ((variables[i].type & PW_TYPE_TMPL) != 0) {
2031                         vp_tmpl_t *vpt;
2032
2033                         slen = tmpl_afrom_str(cs, &vpt, cp->value, talloc_array_length(cp->value) - 1,
2034                                               cp->rhs_type,
2035                                               REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
2036                         if (slen < 0) {
2037                                 error = fr_strerror();
2038                                 goto error;
2039                         }
2040
2041                         /*
2042                          *      Sanity check
2043                          *
2044                          *      Don't add default - update with new types.
2045                          */
2046                         switch (vpt->type) {
2047                         /*
2048                          *      All attributes should have been defined by this point.
2049                          */
2050                         case TMPL_TYPE_ATTR_UNDEFINED:
2051                                 cf_log_err(&cp->item, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
2052                                 return -1;
2053
2054                         case TMPL_TYPE_LITERAL:
2055                         case TMPL_TYPE_ATTR:
2056                         case TMPL_TYPE_LIST:
2057                         case TMPL_TYPE_DATA:
2058                         case TMPL_TYPE_EXEC:
2059                         case TMPL_TYPE_XLAT:
2060                         case TMPL_TYPE_XLAT_STRUCT:
2061                                 break;
2062
2063                         case TMPL_TYPE_UNKNOWN:
2064                         case TMPL_TYPE_REGEX:
2065                         case TMPL_TYPE_REGEX_STRUCT:
2066                         case TMPL_TYPE_NULL:
2067                                 rad_assert(0);
2068                         }
2069
2070                         talloc_free(*(vp_tmpl_t **)data);
2071                         *(vp_tmpl_t **)data = vpt;
2072                 }
2073
2074                 /*
2075                  *      If the "multi" flag is set, check all of them.
2076                  */
2077                 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
2078                         cp = cf_pair_find_next(cs, cp, cp->attr);
2079                         goto redo;
2080                 }
2081         } /* for all variables in the configuration section */
2082
2083         return 0;
2084 }
2085
2086 /*
2087  *      Merge the template so everyting else "just works".
2088  */
2089 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
2090 {
2091         CONF_ITEM *ci;
2092
2093         if (!cs || !template) return true;
2094
2095         cs->template = NULL;
2096
2097         /*
2098          *      Walk over the template, adding its' entries to the
2099          *      current section.  But only if the entries don't
2100          *      already exist in the current section.
2101          */
2102         for (ci = template->children; ci; ci = ci->next) {
2103                 if (ci->type == CONF_ITEM_PAIR) {
2104                         CONF_PAIR *cp1, *cp2;
2105
2106                         /*
2107                          *      It exists, don't over-write it.
2108                          */
2109                         cp1 = cf_item_to_pair(ci);
2110                         if (cf_pair_find(cs, cp1->attr)) {
2111                                 continue;
2112                         }
2113
2114                         /*
2115                          *      Create a new pair with all of the data
2116                          *      of the old one.
2117                          */
2118                         cp2 = cf_pair_dup(cs, cp1);
2119                         if (!cp2) return false;
2120
2121                         cp2->item.filename = cp1->item.filename;
2122                         cp2->item.lineno = cp1->item.lineno;
2123
2124                         cf_item_add(cs, &(cp2->item));
2125                         continue;
2126                 }
2127
2128                 if (ci->type == CONF_ITEM_SECTION) {
2129                         CONF_SECTION *subcs1, *subcs2;
2130
2131                         subcs1 = cf_item_to_section(ci);
2132                         rad_assert(subcs1 != NULL);
2133
2134                         subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
2135                         if (subcs2) {
2136                                 /*
2137                                  *      sub-sections get merged.
2138                                  */
2139                                 if (!cf_template_merge(subcs2, subcs1)) {
2140                                         return false;
2141                                 }
2142                                 continue;
2143                         }
2144
2145                         /*
2146                          *      Our section doesn't have a matching
2147                          *      sub-section.  Copy it verbatim from
2148                          *      the template.
2149                          */
2150                         subcs2 = cf_section_dup(cs, subcs1,
2151                                                 cf_section_name1(subcs1), cf_section_name2(subcs1),
2152                                                 false);
2153                         if (!subcs2) return false;
2154
2155                         subcs2->item.filename = subcs1->item.filename;
2156                         subcs2->item.lineno = subcs1->item.lineno;
2157
2158                         cf_item_add(cs, &(subcs2->item));
2159                         continue;
2160                 }
2161
2162                 /* ignore everything else */
2163         }
2164
2165         return true;
2166 }
2167
2168 static char const *cf_local_file(char const *base, char const *filename,
2169                                  char *buffer, size_t bufsize)
2170 {
2171         size_t dirsize;
2172         char *p;
2173
2174         strlcpy(buffer, base, bufsize);
2175
2176         p = strrchr(buffer, FR_DIR_SEP);
2177         if (!p) return filename;
2178         if (p[1]) {             /* ./foo */
2179                 p[1] = '\0';
2180         }
2181
2182         dirsize = (p - buffer) + 1;
2183
2184         if ((dirsize + strlen(filename)) >= bufsize) {
2185                 return NULL;
2186         }
2187
2188         strlcpy(p + 1, filename, bufsize - dirsize);
2189
2190         return buffer;
2191 }
2192
2193
2194 /*
2195  *      Read a part of the config file.
2196  */
2197 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
2198                            CONF_SECTION *current)
2199
2200 {
2201         CONF_SECTION *this, *css;
2202         CONF_PAIR *cpn;
2203         char const *ptr;
2204         char const *value;
2205         char buf[8192];
2206         char buf1[8192];
2207         char buf2[8192];
2208         char buf3[8192];
2209         char buf4[8192];
2210         FR_TOKEN t1 = T_INVALID, t2, t3;
2211         bool has_spaces = false;
2212         bool pass2;
2213         char *cbuf = buf;
2214         size_t len;
2215
2216         this = current;         /* add items here */
2217
2218         /*
2219          *      Read, checking for line continuations ('\\' at EOL)
2220          */
2221         for (;;) {
2222                 int at_eof;
2223                 css = NULL;
2224
2225                 /*
2226                  *      Get data, and remember if we are at EOF.
2227                  */
2228                 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
2229                 (*lineno)++;
2230
2231                 /*
2232                  *      We read the entire 8k worth of data: complain.
2233                  *      Note that we don't care if the last character
2234                  *      is \n: it's still forbidden.  This means that
2235                  *      the maximum allowed length of text is 8k-1, which
2236                  *      should be plenty.
2237                  */
2238                 len = strlen(cbuf);
2239                 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
2240                         ERROR("%s[%d]: Line too long",
2241                                filename, *lineno);
2242                         return -1;
2243                 }
2244
2245                 if (has_spaces) {
2246                         ptr = cbuf;
2247                         while (isspace((int) *ptr)) ptr++;
2248
2249                         if (ptr > cbuf) {
2250                                 memmove(cbuf, ptr, len - (ptr - cbuf));
2251                                 len -= (ptr - cbuf);
2252                         }
2253                 }
2254
2255                 /*
2256                  *      Not doing continuations: check for edge
2257                  *      conditions.
2258                  */
2259                 if (cbuf == buf) {
2260                         if (at_eof) break;
2261
2262                         ptr = buf;
2263                         while (*ptr && isspace((int) *ptr)) ptr++;
2264
2265                         if (!*ptr || (*ptr == '#')) continue;
2266
2267                 } else if (at_eof || (len == 0)) {
2268                         ERROR("%s[%d]: Continuation at EOF is illegal",
2269                                filename, *lineno);
2270                         return -1;
2271                 }
2272
2273                 /*
2274                  *      See if there's a continuation.
2275                  */
2276                 while ((len > 0) &&
2277                        ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
2278                         len--;
2279                         cbuf[len] = '\0';
2280                 }
2281
2282                 if ((len > 0) && (cbuf[len - 1] == '\\')) {
2283                         /*
2284                          *      Check for "suppress spaces" magic.
2285                          */
2286                         if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
2287                                 has_spaces = true;
2288                         }
2289
2290                         cbuf[len - 1] = '\0';
2291                         cbuf += len - 1;
2292                         continue;
2293                 }
2294
2295                 ptr = cbuf = buf;
2296                 has_spaces = false;
2297
2298         get_more:
2299                 pass2 = false;
2300
2301                 /*
2302                  *      The parser is getting to be evil.
2303                  */
2304                 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
2305
2306                 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
2307                     (ptr[0] == '`')) {
2308                         int hack;
2309
2310                         if (ptr[0] == '%') {
2311                                 hack = rad_copy_variable(buf1, ptr);
2312                         } else {
2313                                 hack = rad_copy_string(buf1, ptr);
2314                         }
2315                         if (hack < 0) {
2316                                 ERROR("%s[%d]: Invalid expansion: %s",
2317                                        filename, *lineno, ptr);
2318                                 return -1;
2319                         }
2320
2321                         ptr += hack;
2322
2323                         t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2324                         switch (t2) {
2325                         case T_EOL:
2326                         case T_HASH:
2327                                 goto do_bare_word;
2328
2329                         default:
2330                                 ERROR("%s[%d]: Invalid expansion: %s",
2331                                        filename, *lineno, ptr);
2332                                 return -1;
2333                         }
2334                 } else {
2335                         t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
2336                 }
2337
2338                 /*
2339                  *      The caller eats "name1 name2 {", and calls us
2340                  *      for the data inside of the section.  So if we
2341                  *      receive a closing brace, then it must mean the
2342                  *      end of the section.
2343                  */
2344                if (t1 == T_RCBRACE) {
2345                        if (this == current) {
2346                                ERROR("%s[%d]: Too many closing braces",
2347                                       filename, *lineno);
2348                                return -1;
2349                        }
2350
2351                        /*
2352                         *       Merge the template into the existing
2353                         *       section.  This uses more memory, but
2354                         *       means that templates now work with
2355                         *       sub-sections, etc.
2356                         */
2357                        if (!cf_template_merge(this, this->template)) {
2358                                return -1;
2359                        }
2360
2361                        this = this->item.parent;
2362                        goto check_for_more;
2363                }
2364
2365                if (t1 != T_BARE_WORD) goto skip_keywords;
2366
2367                 /*
2368                  *      Allow for $INCLUDE files
2369                  *
2370                  *      This *SHOULD* work for any level include.
2371                  *      I really really really hate this file.  -cparker
2372                  */
2373                if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2374                    (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2375                         bool relative = true;
2376
2377                         t2 = getword(&ptr, buf2, sizeof(buf2), true);
2378                         if (t2 != T_EOL) {
2379                                ERROR("%s[%d]: Unexpected text after $INCLUDE",
2380                                      filename, *lineno);
2381                                return -1;
2382                         }
2383
2384                         if (buf2[0] == '$') relative = false;
2385
2386                         value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2, NULL);
2387                         if (!value) return -1;
2388
2389                         if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2390
2391                         if (relative) {
2392                                 value = cf_local_file(filename, value, buf3,
2393                                                       sizeof(buf3));
2394                                 if (!value) {
2395                                         ERROR("%s[%d]: Directories too deep.",
2396                                                filename, *lineno);
2397                                         return -1;
2398                                 }
2399                         }
2400
2401
2402 #ifdef HAVE_DIRENT_H
2403                         /*
2404                          *      $INCLUDE foo/
2405                          *
2406                          *      Include ALL non-"dot" files in the directory.
2407                          *      careful!
2408                          */
2409                         if (value[strlen(value) - 1] == '/') {
2410                                 DIR             *dir;
2411                                 struct dirent   *dp;
2412                                 struct stat stat_buf;
2413
2414                                 DEBUG2("including files in directory %s", value );
2415 #ifdef S_IWOTH
2416                                 /*
2417                                  *      Security checks.
2418                                  */
2419                                 if (stat(value, &stat_buf) < 0) {
2420                                         ERROR("%s[%d]: Failed reading directory %s: %s",
2421                                                filename, *lineno,
2422                                                value, fr_syserror(errno));
2423                                         return -1;
2424                                 }
2425
2426                                 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2427                                         ERROR("%s[%d]: Directory %s is globally writable.  Refusing to start due to "
2428                                               "insecure configuration", filename, *lineno, value);
2429                                         return -1;
2430                                 }
2431 #endif
2432                                 dir = opendir(value);
2433                                 if (!dir) {
2434                                         ERROR("%s[%d]: Error reading directory %s: %s",
2435                                                filename, *lineno, value,
2436                                                fr_syserror(errno));
2437                                         return -1;
2438                                 }
2439
2440                                 /*
2441                                  *      Read the directory, ignoring "." files.
2442                                  */
2443                                 while ((dp = readdir(dir)) != NULL) {
2444                                         char const *p;
2445
2446                                         if (dp->d_name[0] == '.') continue;
2447
2448                                         /*
2449                                          *      Check for valid characters
2450                                          */
2451                                         for (p = dp->d_name; *p != '\0'; p++) {
2452                                                 if (isalpha((int)*p) ||
2453                                                     isdigit((int)*p) ||
2454                                                     (*p == '-') ||
2455                                                     (*p == '_') ||
2456                                                     (*p == '.')) continue;
2457                                                 break;
2458                                         }
2459                                         if (*p != '\0') continue;
2460
2461                                         snprintf(buf2, sizeof(buf2), "%s%s",
2462                                                  value, dp->d_name);
2463                                         if ((stat(buf2, &stat_buf) != 0) ||
2464                                             S_ISDIR(stat_buf.st_mode)) continue;
2465                                         /*
2466                                          *      Read the file into the current
2467                                          *      configuration section.
2468                                          */
2469                                         if (cf_file_include(this, buf2) < 0) {
2470                                                 closedir(dir);
2471                                                 return -1;
2472                                         }
2473                                 }
2474                                 closedir(dir);
2475                         }  else
2476 #endif
2477                         { /* it was a normal file */
2478                                 if (buf1[1] == '-') {
2479                                         struct stat statbuf;
2480
2481                                         if (stat(value, &statbuf) < 0) {
2482                                                 WARN("Not including file %s: %s", value, fr_syserror(errno));
2483                                                 continue;
2484                                         }
2485                                 }
2486
2487                                 if (cf_file_include(this, value) < 0) {
2488                                         return -1;
2489                                 }
2490                         }
2491                         continue;
2492                 } /* we were in an include */
2493
2494                if (strcasecmp(buf1, "$template") == 0) {
2495                        CONF_ITEM *ci;
2496                        CONF_SECTION *parentcs, *templatecs;
2497                        t2 = getword(&ptr, buf2, sizeof(buf2), true);
2498
2499                        if (t2 != T_EOL) {
2500                                ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2501                                return -1;
2502                        }
2503
2504                        parentcs = cf_top_section(current);
2505
2506                        templatecs = cf_section_sub_find(parentcs, "templates");
2507                        if (!templatecs) {
2508                                 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2509                                 return -1;
2510                        }
2511
2512                        ci = cf_reference_item(parentcs, templatecs, buf2);
2513                        if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2514                                 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2515                                 return -1;
2516                        }
2517
2518                        if (!this) {
2519                                 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2520                                 return -1;
2521                        }
2522
2523                        if (this->template) {
2524                                 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2525                                 return -1;
2526                        }
2527
2528                        this->template = cf_item_to_section(ci);
2529                        continue;
2530                }
2531
2532                 /*
2533                  *      Ensure that the user can't add CONF_PAIRs
2534                  *      with 'internal' names;
2535                  */
2536                 if (buf1[0] == '_') {
2537                         ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2538                         return -1;
2539                 }
2540
2541                 /*
2542                  *      Handle if/elsif specially.
2543                  */
2544                 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2545                         ssize_t slen;
2546                         char const *error = NULL;
2547                         char *p;
2548                         CONF_SECTION *server;
2549                         fr_cond_t *cond = NULL;
2550
2551                         /*
2552                          *      if / elsif MUST be inside of a
2553                          *      processing section, which MUST in turn
2554                          *      be inside of a "server" directive.
2555                          */
2556                         if (!this->item.parent) {
2557                         invalid_location:
2558                                 ERROR("%s[%d]: Invalid location for '%s'",
2559                                        filename, *lineno, buf1);
2560                                 return -1;
2561                         }
2562
2563                         /*
2564                          *      Can only have "if" in 3 named sections.
2565                          */
2566                         server = this->item.parent;
2567                         while (server &&
2568                                (strcmp(server->name1, "server") != 0) &&
2569                                (strcmp(server->name1, "policy") != 0) &&
2570                                (strcmp(server->name1, "instantiate") != 0)) {
2571                                 server = server->item.parent;
2572                                 if (!server) goto invalid_location;
2573                         }
2574
2575                         /*
2576                          *      Skip (...) to find the {
2577                          */
2578                         slen = fr_condition_tokenize(this, cf_section_to_item(this), ptr, &cond,
2579                                                      &error, FR_COND_TWO_PASS);
2580                         memcpy(&p, &ptr, sizeof(p));
2581
2582                         if (slen < 0) {
2583                                 if (p[-slen] != '{') goto cond_error;
2584                                 slen = -slen;
2585                         }
2586                         TALLOC_FREE(cond);
2587
2588                         /*
2589                          *      This hack is so that the NEXT stage
2590                          *      doesn't go "too far" in expanding the
2591                          *      variable.  We can parse the conditions
2592                          *      without expanding the ${...} stuff.
2593                          *      BUT we don't want to expand all of the
2594                          *      stuff AFTER the condition.  So we do
2595                          *      two passes.
2596                          *
2597                          *      The first pass is to discover the end
2598                          *      of the condition.  We then expand THAT
2599                          *      string, and do a second pass parsing
2600                          *      the expanded condition.
2601                          */
2602                         p += slen;
2603                         *p = '\0';
2604
2605                         /*
2606                          *      If there's a ${...}.  If so, expand it.
2607                          */
2608                         if (strchr(ptr, '$') != NULL) {
2609                                 ptr = cf_expand_variables(filename, lineno,
2610                                                           this,
2611                                                           buf3, sizeof(buf3),
2612                                                           ptr, NULL);
2613                                 if (!ptr) {
2614                                         ERROR("%s[%d]: Parse error expanding ${...} in condition",
2615                                               filename, *lineno);
2616                                         return -1;
2617                                 }
2618                         } /* else leave it alone */
2619
2620                         css = cf_section_alloc(this, buf1, ptr);
2621                         if (!css) {
2622                                 ERROR("%s[%d]: Failed allocating memory for section",
2623                                       filename, *lineno);
2624                                 return -1;
2625                         }
2626                         css->item.filename = filename;
2627                         css->item.lineno = *lineno;
2628
2629                         slen = fr_condition_tokenize(css, cf_section_to_item(css), ptr, &cond,
2630                                                      &error, FR_COND_TWO_PASS);
2631                         *p = '{'; /* put it back */
2632
2633                 cond_error:
2634                         if (slen < 0) {
2635                                 char *spaces, *text;
2636
2637                                 fr_canonicalize_error(this, &spaces, &text, slen, ptr);
2638
2639                                 ERROR("%s[%d]: Parse error in condition",
2640                                       filename, *lineno);
2641                                 ERROR("%s[%d]: %s", filename, *lineno, text);
2642                                 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2643
2644                                 talloc_free(spaces);
2645                                 talloc_free(text);
2646                                 talloc_free(css);
2647                                 return -1;
2648                         }
2649
2650                         if ((size_t) slen >= (sizeof(buf2) - 1)) {
2651                                 talloc_free(css);
2652                                 ERROR("%s[%d]: Condition is too large after \"%s\"",
2653                                        filename, *lineno, buf1);
2654                                 return -1;
2655                         }
2656
2657                         /*
2658                          *      Copy the expanded and parsed condition
2659                          *      into buf2.  Then, parse the text after
2660                          *      the condition, which now MUST be a '{.
2661                          *
2662                          *      If it wasn't '{' it would have been
2663                          *      caught in the first pass of
2664                          *      conditional parsing, above.
2665                          */
2666                         memcpy(buf2, ptr, slen);
2667                         buf2[slen] = '\0';
2668                         ptr = p;
2669
2670                         if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2671                                 talloc_free(css);
2672                                 ERROR("%s[%d]: Expected '{' %d",
2673                                       filename, *lineno, t3);
2674                                 return -1;
2675                         }
2676
2677                         /*
2678                          *      Swap the condition with trailing stuff for
2679                          *      the final condition.
2680                          */
2681                         memcpy(&p, &css->name2, sizeof(css->name2));
2682                         talloc_free(p);
2683                         css->name2 = talloc_typed_strdup(css, buf2);
2684
2685                         cf_item_add(this, &(css->item));
2686                         cf_data_add_internal(css, "if", cond, NULL, false);
2687
2688                         /*
2689                          *      The current section is now the child section.
2690                          */
2691                         this = css;
2692                         css = NULL;
2693                         goto check_for_more;
2694                 }
2695
2696         skip_keywords:
2697                 /*
2698                  *      Grab the next token.
2699                  */
2700                 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2701                 switch (t2) {
2702                 case T_EOL:
2703                 case T_HASH:
2704                 case T_COMMA:
2705                 do_bare_word:
2706                         t3 = t2;
2707                         t2 = T_OP_EQ;
2708                         value = NULL;
2709                         goto do_set;
2710
2711                 case T_OP_INCRM:
2712                 case T_OP_ADD:
2713                 case T_OP_CMP_EQ:
2714                 case T_OP_SUB:
2715                 case T_OP_LE:
2716                 case T_OP_GE:
2717                 case T_OP_CMP_FALSE:
2718                         if (!this || (strcmp(this->name1, "update") != 0)) {
2719                                 ERROR("%s[%d]: Invalid operator in assignment",
2720                                        filename, *lineno);
2721                                 return -1;
2722                         }
2723                         /* FALL-THROUGH */
2724
2725                 case T_OP_EQ:
2726                 case T_OP_SET:
2727                         while (isspace((int) *ptr)) ptr++;
2728
2729                         /*
2730                          *      New parser: non-quoted strings are
2731                          *      bare words, and we parse everything
2732                          *      until the next newline, or the next
2733                          *      comma.  If they have { or } in a bare
2734                          *      word, well... too bad.
2735                          */
2736                         if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2737                             && (*ptr != '`') && (*ptr != '/')) {
2738                                 const char *q = ptr;
2739
2740                                 t3 = T_BARE_WORD;
2741                                 while (*q && (*q >= ' ') && (*q != ',') &&
2742                                        !isspace(*q)) q++;
2743
2744                                 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2745                                         ERROR("%s[%d]: Parse error: value too long",
2746                                               filename, *lineno);
2747                                         return -1;
2748                                 }
2749
2750                                 memcpy(buf3, ptr, (q - ptr));
2751                                 buf3[q - ptr] = '\0';
2752                                 ptr = q;
2753
2754                         } else {
2755                                 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2756                         }
2757
2758                         if (t3 == T_INVALID) {
2759                                 ERROR("%s[%d]: Parse error: %s",
2760                                        filename, *lineno,
2761                                        fr_strerror());
2762                                 return -1;
2763                         }
2764
2765                         /*
2766                          *      Allow "foo" by itself, or "foo = bar"
2767                          */
2768                         switch (t3) {
2769                                 bool soft_fail;
2770
2771                         case T_BARE_WORD:
2772                         case T_DOUBLE_QUOTED_STRING:
2773                         case T_BACK_QUOTED_STRING:
2774                                 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3, &soft_fail);
2775                                 if (!value) {
2776                                         if (!soft_fail) return -1;
2777
2778                                         /*
2779                                          *      References an item which doesn't exist,
2780                                          *      or which is already marked up as being
2781                                          *      expanded in pass2.  Wait for pass2 to
2782                                          *      do the expansions.
2783                                          */
2784                                         pass2 = true;
2785                                         value = buf3;
2786                                 }
2787                                 break;
2788
2789                         case T_EOL:
2790                         case T_HASH:
2791                                 value = NULL;
2792                                 break;
2793
2794                         default:
2795                                 value = buf3;
2796                                 break;
2797                         }
2798
2799                         /*
2800                          *      Add this CONF_PAIR to our CONF_SECTION
2801                          */
2802                 do_set:
2803                         cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2804                         if (!cpn) return -1;
2805                         cpn->item.filename = filename;
2806                         cpn->item.lineno = *lineno;
2807                         cpn->pass2 = pass2;
2808                         cf_item_add(this, &(cpn->item));
2809
2810                         /*
2811                          *      Hacks for escaping
2812                          */
2813                         if (!cf_new_escape && !this->item.parent && value &&
2814                             (strcmp(buf1, "correct_escapes") == 0) &&
2815                             ((strcmp(value, "true") == 0) ||
2816                              (strcmp(value, "yes") == 0) ||
2817                              (strcmp(value, "1") == 0))) {
2818                                 cf_new_escape = true;
2819                         }
2820
2821                         /*
2822                          *      Require a comma, unless there's a comment.
2823                          */
2824                         while (isspace(*ptr)) ptr++;
2825
2826                         if (*ptr == ',') {
2827                                 ptr++;
2828                                 break;
2829                         }
2830
2831                         /*
2832                          *      module # stuff!
2833                          *      foo = bar # other stuff
2834                          */
2835                         if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2836
2837                         if (!*ptr || (*ptr == '}')) break;
2838
2839                         ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2840                               filename, *lineno, value, ptr);
2841                         return -1;
2842
2843                         /*
2844                          *      No '=', must be a section or sub-section.
2845                          */
2846                 case T_BARE_WORD:
2847                 case T_DOUBLE_QUOTED_STRING:
2848                 case T_SINGLE_QUOTED_STRING:
2849                         t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2850                         if (t3 != T_LCBRACE) {
2851                                 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2852                                        filename, *lineno, buf1, buf2);
2853                                 return -1;
2854                         }
2855                         /* FALL-THROUGH */
2856
2857                 case T_LCBRACE:
2858                         css = cf_section_alloc(this, buf1,
2859                                                t2 == T_LCBRACE ? NULL : buf2);
2860                         if (!css) {
2861                                 ERROR("%s[%d]: Failed allocating memory for section",
2862                                       filename, *lineno);
2863                                 return -1;
2864                         }
2865
2866                         css->item.filename = filename;
2867                         css->item.lineno = *lineno;
2868                         cf_item_add(this, &(css->item));
2869
2870                         /*
2871                          *      There may not be a name2
2872                          */
2873                         css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2874
2875                         /*
2876                          *      The current section is now the child section.
2877                          */
2878                         this = css;
2879                         break;
2880
2881                 case T_INVALID:
2882                         ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2883
2884                         return -1;
2885
2886                 default:
2887                         ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2888                               filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2889
2890                         return -1;
2891                 }
2892
2893         check_for_more:
2894                 /*
2895                  *      Done parsing one thing.  Skip to EOL if possible.
2896                  */
2897                 while (isspace(*ptr)) ptr++;
2898
2899                 if (*ptr == '#') continue;
2900
2901                 if (*ptr) {
2902                         goto get_more;
2903                 }
2904
2905         }
2906
2907         /*
2908          *      See if EOF was unexpected ..
2909          */
2910         if (feof(fp) && (this != current)) {
2911                 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2912                       filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2913                 return -1;
2914         }
2915
2916         return 0;
2917 }
2918
2919 /*
2920  *      Include one config file in another.
2921  */
2922 static int cf_file_include(CONF_SECTION *cs, char const *filename_in)
2923 {
2924         FILE            *fp;
2925         int             lineno = 0;
2926         char const      *filename;
2927
2928         /*
2929          *      So we only need to do this once.
2930          */
2931         filename = talloc_strdup(cs, filename_in);
2932
2933         DEBUG2("including configuration file %s", filename);
2934
2935         fp = cf_file_open(cs, filename);
2936         if (!fp) return -1;
2937
2938         if (!cs->item.filename) cs->item.filename = filename;
2939
2940         /*
2941          *      Read the section.  It's OK to have EOF without a
2942          *      matching close brace.
2943          */
2944         if (cf_section_read(filename, &lineno, fp, cs) < 0) {
2945                 fclose(fp);
2946                 return -1;
2947         }
2948
2949         fclose(fp);
2950         return 0;
2951 }
2952
2953
2954 /*
2955  *      Do variable expansion in pass2.
2956  *
2957  *      This is a breadth-first expansion.  "deep
2958  */
2959 static int cf_section_pass2(CONF_SECTION *cs)
2960 {
2961         CONF_ITEM *ci;
2962
2963         for (ci = cs->children; ci; ci = ci->next) {
2964                 char const *value;
2965                 CONF_PAIR *cp;
2966                 char buffer[8192];
2967
2968                 if (ci->type != CONF_ITEM_PAIR) continue;
2969
2970                 cp = cf_item_to_pair(ci);
2971                 if (!cp->value || !cp->pass2) continue;
2972
2973                 rad_assert((cp->rhs_type == T_BARE_WORD) ||
2974                            (cp->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2975                            (cp->rhs_type == T_BACK_QUOTED_STRING));
2976
2977                 value = cf_expand_variables(ci->filename, &ci->lineno, cs, buffer, sizeof(buffer), cp->value, NULL);
2978                 if (!value) return -1;
2979
2980                 rad_const_free(cp->value);
2981                 cp->value = talloc_typed_strdup(cp, value);
2982         }
2983
2984         for (ci = cs->children; ci; ci = ci->next) {
2985                 if (ci->type != CONF_ITEM_SECTION) continue;
2986
2987                 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
2988         }
2989
2990         return 0;
2991 }
2992
2993
2994 /*
2995  *      Bootstrap a config file.
2996  */
2997 int cf_file_read(CONF_SECTION *cs, char const *filename)
2998 {
2999         char *p;
3000         CONF_PAIR *cp;
3001         rbtree_t *tree;
3002
3003         cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3004         if (!cp) return -1;
3005
3006         p = strrchr(cp->value, FR_DIR_SEP);
3007         if (p) *p = '\0';
3008
3009         cp->item.filename = "<internal>";
3010         cp->item.lineno = -1;
3011         cf_item_add(cs, &(cp->item));
3012
3013         tree = rbtree_create(cs, filename_cmp, NULL, 0);
3014         if (!tree) return -1;
3015
3016         cf_data_add_internal(cs, "filename", tree, NULL, 0);
3017
3018         if (cf_file_include(cs, filename) < 0) return -1;
3019
3020         /*
3021          *      Now that we've read the file, go back through it and
3022          *      expand the variables.
3023          */
3024         if (cf_section_pass2(cs) < 0) return -1;
3025
3026         return 0;
3027 }
3028
3029
3030 void cf_file_free(CONF_SECTION *cs)
3031 {
3032         talloc_free(cs);
3033 }
3034
3035
3036 /*
3037  * Return a CONF_PAIR within a CONF_SECTION.
3038  */
3039 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
3040 {
3041         CONF_PAIR *cp, mycp;
3042
3043         if (!cs || !name) return NULL;
3044
3045         mycp.attr = name;
3046         cp = rbtree_finddata(cs->pair_tree, &mycp);
3047         if (cp) return cp;
3048
3049         if (!cs->template) return NULL;
3050
3051         return rbtree_finddata(cs->template->pair_tree, &mycp);
3052 }
3053
3054 /*
3055  * Return the attr of a CONF_PAIR
3056  */
3057
3058 char const *cf_pair_attr(CONF_PAIR const *pair)
3059 {
3060         return (pair ? pair->attr : NULL);
3061 }
3062
3063 /*
3064  * Return the value of a CONF_PAIR
3065  */
3066
3067 char const *cf_pair_value(CONF_PAIR const *pair)
3068 {
3069         return (pair ? pair->value : NULL);
3070 }
3071
3072 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
3073 {
3074         return (pair ? pair->op : T_INVALID);
3075 }
3076
3077 /** Return the value (lhs) type
3078  *
3079  * @param pair to extract value type from.
3080  * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3081  *      T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3082  */
3083 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
3084 {
3085         return (pair ? pair->lhs_type : T_INVALID);
3086 }
3087
3088 /** Return the value (rhs) type
3089  *
3090  * @param pair to extract value type from.
3091  * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3092  *      T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3093  */
3094 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
3095 {
3096         return (pair ? pair->rhs_type : T_INVALID);
3097 }
3098
3099 /*
3100  * Turn a CONF_PAIR into a VALUE_PAIR
3101  * For now, ignore the "value_type" field...
3102  */
3103 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
3104 {
3105         if (!pair) {
3106                 fr_strerror_printf("Internal error");
3107                 return NULL;
3108         }
3109
3110         if (!pair->value) {
3111                 fr_strerror_printf("No value given for attribute %s", pair->attr);
3112                 return NULL;
3113         }
3114
3115         /*
3116          *      false comparisons never match.  BUT if it's a "string"
3117          *      or `string`, then remember to expand it later.
3118          */
3119         if ((pair->op != T_OP_CMP_FALSE) &&
3120             ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
3121              (pair->rhs_type == T_BACK_QUOTED_STRING))) {
3122                 VALUE_PAIR *vp;
3123
3124                 vp = fr_pair_make(pair, NULL, pair->attr, NULL, pair->op);
3125                 if (!vp) {
3126                         return NULL;
3127                 }
3128
3129                 if (fr_pair_mark_xlat(vp, pair->value) < 0) {
3130                         talloc_free(vp);
3131
3132                         return NULL;
3133                 }
3134
3135                 return vp;
3136         }
3137
3138         return fr_pair_make(pair, NULL, pair->attr, pair->value, pair->op);
3139 }
3140
3141 /*
3142  * Return the first label of a CONF_SECTION
3143  */
3144
3145 char const *cf_section_name1(CONF_SECTION const *cs)
3146 {
3147         return (cs ? cs->name1 : NULL);
3148 }
3149
3150 /*
3151  * Return the second label of a CONF_SECTION
3152  */
3153
3154 char const *cf_section_name2(CONF_SECTION const *cs)
3155 {
3156         return (cs ? cs->name2 : NULL);
3157 }
3158
3159 /** Return name2 if set, else name1
3160  *
3161  */
3162 char const *cf_section_name(CONF_SECTION const *cs)
3163 {
3164         char const *name;
3165
3166         name = cf_section_name2(cs);
3167         if (name) return name;
3168
3169         return cf_section_name1(cs);
3170 }
3171
3172 /*
3173  * Find a value in a CONF_SECTION
3174  */
3175 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
3176 {
3177         CONF_PAIR       *cp;
3178
3179         cp = cf_pair_find(cs, attr);
3180
3181         return (cp ? cp->value : NULL);
3182 }
3183
3184
3185 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
3186                                     char const *name1, char const *name2)
3187 {
3188         char const      *their2;
3189         CONF_ITEM const *ci;
3190
3191         if (!cs || !name1) return NULL;
3192
3193         for (ci = &(cs->item); ci; ci = ci->next) {
3194                 if (ci->type != CONF_ITEM_SECTION)
3195                         continue;
3196
3197                 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
3198                         continue;
3199                 }
3200
3201                 their2 = cf_item_to_section(ci)->name2;
3202
3203                 if ((!name2 && !their2) ||
3204                     (name2 && their2 && (strcmp(name2, their2) == 0))) {
3205                         return cf_item_to_section(ci);
3206                 }
3207         }
3208
3209         return NULL;
3210 }
3211
3212 /** Find a pair with a name matching attr, after specified pair.
3213  *
3214  * @param cs to search in.
3215  * @param pair to search from (may be NULL).
3216  * @param attr to find (may be NULL in which case any attribute matches).
3217  * @return the next matching CONF_PAIR or NULL if none matched.
3218  */
3219 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
3220                              CONF_PAIR const *pair, char const *attr)
3221 {
3222         CONF_ITEM       *ci;
3223
3224         if (!cs) return NULL;
3225
3226         /*
3227          *      If pair is NULL and we're trying to find a specific
3228          *      attribute this must be a first time run.
3229          *
3230          *      Find the pair with correct name.
3231          */
3232         if (!pair && attr) return cf_pair_find(cs, attr);
3233
3234         /*
3235          *      Start searching from the next child, or from the head
3236          *      of the list of children (if no pair was provided).
3237          */
3238         for (ci = pair ? pair->item.next : cs->children;
3239              ci;
3240              ci = ci->next) {
3241                 if (ci->type != CONF_ITEM_PAIR) continue;
3242
3243                 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
3244         }
3245
3246         return cf_item_to_pair(ci);
3247 }
3248
3249 /*
3250  * Find a CONF_SECTION, or return the root if name is NULL
3251  */
3252
3253 CONF_SECTION *cf_section_find(char const *name)
3254 {
3255         if (name)
3256                 return cf_section_sub_find(root_config, name);
3257         else
3258                 return root_config;
3259 }
3260
3261 /** Find a sub-section in a section
3262  *
3263  *      This finds ANY section having the same first name.
3264  *      The second name is ignored.
3265  */
3266 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
3267 {
3268         CONF_SECTION mycs;
3269
3270         if (!cs || !name) return NULL;  /* can't find an un-named section */
3271
3272         /*
3273          *      No sub-sections have been defined, so none exist.
3274          */
3275         if (!cs->section_tree) return NULL;
3276
3277         mycs.name1 = name;
3278         mycs.name2 = NULL;
3279         return rbtree_finddata(cs->section_tree, &mycs);
3280 }
3281
3282
3283 /** Find a CONF_SECTION with both names.
3284  *
3285  */
3286 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
3287                                         char const *name1, char const *name2)
3288 {
3289         CONF_ITEM    *ci;
3290
3291         if (!cs) cs = root_config;
3292         if (!cs) return NULL;
3293
3294         if (name1) {
3295                 CONF_SECTION mycs, *master_cs;
3296
3297                 if (!cs->section_tree) return NULL;
3298
3299                 mycs.name1 = name1;
3300                 mycs.name2 = name2;
3301
3302                 master_cs = rbtree_finddata(cs->section_tree, &mycs);
3303                 if (!master_cs) return NULL;
3304
3305                 /*
3306                  *      Look it up in the name2 tree.  If it's there,
3307                  *      return it.
3308                  */
3309                 if (master_cs->name2_tree) {
3310                         CONF_SECTION *subcs;
3311
3312                         subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
3313                         if (subcs) return subcs;
3314                 }
3315
3316                 /*
3317                  *      We don't insert ourselves into the name2 tree.
3318                  *      So if there's nothing in the name2 tree, maybe
3319                  *      *we* are the answer.
3320                  */
3321                 if (!master_cs->name2 && name2) return NULL;
3322                 if (master_cs->name2 && !name2) return NULL;
3323                 if (!master_cs->name2 && !name2) return master_cs;
3324
3325                 if (strcmp(master_cs->name2, name2) == 0) {
3326                         return master_cs;
3327                 }
3328
3329                 return NULL;
3330         }
3331
3332         /*
3333          *      Else do it the old-fashioned way.
3334          */
3335         for (ci = cs->children; ci; ci = ci->next) {
3336                 CONF_SECTION *subcs;
3337
3338                 if (ci->type != CONF_ITEM_SECTION)
3339                         continue;
3340
3341                 subcs = cf_item_to_section(ci);
3342                 if (!subcs->name2) {
3343                         if (strcmp(subcs->name1, name2) == 0) break;
3344                 } else {
3345                         if (strcmp(subcs->name2, name2) == 0) break;
3346                 }
3347         }
3348
3349         return cf_item_to_section(ci);
3350 }
3351
3352 /*
3353  * Return the next subsection after a CONF_SECTION
3354  * with a certain name1 (char *name1). If the requested
3355  * name1 is NULL, any name1 matches.
3356  */
3357
3358 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
3359                                       CONF_SECTION const *subsection,
3360                                       char const *name1)
3361 {
3362         CONF_ITEM       *ci;
3363
3364         if (!section) return NULL;
3365
3366         /*
3367          * If subsection is NULL this must be a first time run
3368          * Find the subsection with correct name
3369          */
3370
3371         if (!subsection) {
3372                 ci = section->children;
3373         } else {
3374                 ci = subsection->item.next;
3375         }
3376
3377         for (; ci; ci = ci->next) {
3378                 if (ci->type != CONF_ITEM_SECTION)
3379                         continue;
3380                 if ((name1 == NULL) ||
3381                     (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3382                         break;
3383         }
3384
3385         return cf_item_to_section(ci);
3386 }
3387
3388
3389 /*
3390  * Return the next section after a CONF_SECTION
3391  * with a certain name1 (char *name1). If the requested
3392  * name1 is NULL, any name1 matches.
3393  */
3394
3395 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3396                                    CONF_SECTION const *subsection,
3397                                    char const *name1)
3398 {
3399         if (!section) return NULL;
3400
3401         if (!section->item.parent) return NULL;
3402
3403         return cf_subsection_find_next(section->item.parent, subsection, name1);
3404 }
3405
3406 /** Return the next item after a CONF_ITEM.
3407  *
3408  */
3409 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3410 {
3411         if (!section) return NULL;
3412
3413         /*
3414          *      If item is NULL this must be a first time run
3415          *      Return the first item
3416          */
3417         if (item == NULL) {
3418                 return section->children;
3419         } else {
3420                 return item->next;
3421         }
3422 }
3423
3424 static void _pair_count(int *count, CONF_SECTION const *cs)
3425 {
3426         CONF_ITEM const *ci;
3427
3428         for (ci = cf_item_find_next(cs, NULL);
3429              ci != NULL;
3430              ci = cf_item_find_next(cs, ci)) {
3431
3432                 if (cf_item_is_section(ci)) {
3433                         _pair_count(count, cf_item_to_section(ci));
3434                         continue;
3435                 }
3436
3437                 (*count)++;
3438         }
3439 }
3440
3441 /** Count the number of conf pairs beneath a section
3442  *
3443  * @param[in] cs to search for items in.
3444  * @return number of pairs nested within section.
3445  */
3446 int cf_pair_count(CONF_SECTION const *cs)
3447 {
3448         int count = 0;
3449
3450         _pair_count(&count, cs);
3451
3452         return count;
3453 }
3454
3455 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3456 {
3457         if (!ci) return NULL;
3458
3459         return ci->parent;
3460 }
3461
3462 int cf_section_lineno(CONF_SECTION const *section)
3463 {
3464         return section->item.lineno;
3465 }
3466
3467 char const *cf_pair_filename(CONF_PAIR const *pair)
3468 {
3469         return pair->item.filename;
3470 }
3471
3472 char const *cf_section_filename(CONF_SECTION const *section)
3473 {
3474         return section->item.filename;
3475 }
3476
3477 int cf_pair_lineno(CONF_PAIR const *pair)
3478 {
3479         return pair->item.lineno;
3480 }
3481
3482 bool cf_item_is_section(CONF_ITEM const *item)
3483 {
3484         return item->type == CONF_ITEM_SECTION;
3485 }
3486
3487 bool cf_item_is_pair(CONF_ITEM const *item)
3488 {
3489         return item->type == CONF_ITEM_PAIR;
3490 }
3491
3492
3493 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3494                                 void *data, void (*data_free)(void *))
3495 {
3496         CONF_DATA *cd;
3497
3498         cd = talloc_zero(parent, CONF_DATA);
3499         if (!cd) return NULL;
3500
3501         cd->item.type = CONF_ITEM_DATA;
3502         cd->item.parent = parent;
3503         cd->name = talloc_typed_strdup(cd, name);
3504         if (!cd->name) {
3505                 talloc_free(cd);
3506                 return NULL;
3507         }
3508
3509         cd->data = data;
3510         cd->free = data_free;
3511
3512         if (cd->free) {
3513                 talloc_set_destructor(cd, _cf_data_free);
3514         }
3515
3516         return cd;
3517 }
3518
3519 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3520 {
3521         if (!cs || !name) return NULL;
3522
3523         /*
3524          *      Find the name in the tree, for speed.
3525          */
3526         if (cs->data_tree) {
3527                 CONF_DATA mycd;
3528
3529                 mycd.name = name;
3530                 mycd.flag = flag;
3531                 return rbtree_finddata(cs->data_tree, &mycd);
3532         }
3533
3534         return NULL;
3535 }
3536
3537 /*
3538  *      Find data from a particular section.
3539  */
3540 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3541 {
3542         CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3543
3544         if (cd) return cd->data;
3545         return NULL;
3546 }
3547
3548
3549 /*
3550  *      Add named data to a configuration section.
3551  */
3552 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3553                                 void *data, void (*data_free)(void *),
3554                                 int flag)
3555 {
3556         CONF_DATA *cd;
3557
3558         if (!cs || !name) return -1;
3559
3560         /*
3561          *      Already exists.  Can't add it.
3562          */
3563         if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3564
3565         cd = cf_data_alloc(cs, name, data, data_free);
3566         if (!cd) return -1;
3567         cd->flag = flag;
3568
3569         cf_item_add(cs, cf_data_to_item(cd));
3570
3571         return 0;
3572 }
3573
3574 /*
3575  *      Add named data to a configuration section.
3576  */
3577 int cf_data_add(CONF_SECTION *cs, char const *name,
3578                 void *data, void (*data_free)(void *))
3579 {
3580         return cf_data_add_internal(cs, name, data, data_free, 0);
3581 }
3582
3583 /** Remove named data from a configuration section
3584  *
3585  */
3586 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3587 {
3588         CONF_DATA mycd;
3589         CONF_DATA *cd;
3590         void *data;
3591
3592         if (!cs || !name) return NULL;
3593         if (!cs->data_tree) return NULL;
3594
3595         /*
3596          *      Find the name in the tree, for speed.
3597          */
3598         mycd.name = name;
3599         mycd.flag = 0;
3600         cd = rbtree_finddata(cs->data_tree, &mycd);
3601         if (!cd) return NULL;
3602
3603         talloc_set_destructor(cd, NULL);        /* Disarm the destructor */
3604         rbtree_deletebydata(cs->data_tree, &mycd);
3605
3606         data = cd->data;
3607         talloc_free(cd);
3608
3609         return data;
3610 }
3611
3612 /*
3613  *      This is here to make the rest of the code easier to read.  It
3614  *      ties conffile.c to log.c, but it means we don't have to
3615  *      pollute every other function with the knowledge of the
3616  *      configuration internals.
3617  */
3618 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3619 {
3620         va_list ap;
3621         char buffer[256];
3622
3623         va_start(ap, fmt);
3624         vsnprintf(buffer, sizeof(buffer), fmt, ap);
3625         va_end(ap);
3626
3627         if (ci) {
3628                 ERROR("%s[%d]: %s",
3629                        ci->filename ? ci->filename : "unknown",
3630                        ci->lineno ? ci->lineno : 0,
3631                        buffer);
3632         } else {
3633                 ERROR("<unknown>[*]: %s", buffer);
3634         }
3635 }
3636
3637 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3638 {
3639         va_list ap;
3640         char buffer[256];
3641
3642         va_start(ap, fmt);
3643         vsnprintf(buffer, sizeof(buffer), fmt, ap);
3644         va_end(ap);
3645
3646         rad_assert(cs != NULL);
3647
3648         ERROR("%s[%d]: %s",
3649                cs->item.filename ? cs->item.filename : "unknown",
3650                cs->item.lineno ? cs->item.lineno : 0,
3651                buffer);
3652 }
3653
3654 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3655 {
3656         va_list ap;
3657         char buffer[256];
3658
3659         va_start(ap, fmt);
3660         vsnprintf(buffer, sizeof(buffer), fmt, ap);
3661         va_end(ap);
3662
3663         rad_assert(cp != NULL);
3664
3665         ERROR("%s[%d]: %s",
3666                cp->item.filename ? cp->item.filename : "unknown",
3667                cp->item.lineno ? cp->item.lineno : 0,
3668                buffer);
3669 }
3670
3671 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3672 {
3673         va_list ap;
3674
3675         va_start(ap, fmt);
3676         if ((rad_debug_lvl > 1) && cs) vradlog(L_DBG, fmt, ap);
3677         va_end(ap);
3678 }
3679
3680 /*
3681  *      Wrapper to simplify the code.
3682  */
3683 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3684 {
3685         va_list ap;
3686         char buffer[256];
3687
3688         va_start(ap, fmt);
3689         if (rad_debug_lvl > 1 && cs) {
3690                 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3691
3692                 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3693         }
3694         va_end(ap);
3695 }
3696
3697 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3698 {
3699         if (!cs) return NULL;
3700
3701         return cs->variables;
3702 }
3703
3704 /*
3705  *      For "switch" and "case" statements.
3706  */
3707 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3708 {
3709         if (!cs) return T_INVALID;
3710
3711         return cs->name2_type;
3712 }