Use macro for terminating CONF_PARSER arrays
[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
1386  *      - 1 if default value was used.
1387  *      - 0 on success.
1388  *      - -1 on error.
1389  *      - -2 if deprecated.
1390  */
1391 int cf_item_parse(CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt)
1392 {
1393         int rcode;
1394         bool deprecated, required, attribute, secret, file_input, cant_be_empty, tmpl, multi;
1395         char **q;
1396         char const *value;
1397         CONF_PAIR *cp = NULL;
1398         fr_ipaddr_t *ipaddr;
1399         char buffer[8192];
1400         CONF_ITEM *c_item = &cs->item;
1401
1402         if (!cs) return -1;
1403
1404         deprecated = (type & PW_TYPE_DEPRECATED);
1405         required = (type & PW_TYPE_REQUIRED);
1406         attribute = (type & PW_TYPE_ATTRIBUTE);
1407         secret = (type & PW_TYPE_SECRET);
1408         file_input = (type == PW_TYPE_FILE_INPUT);      /* check, not and */
1409         cant_be_empty = (type & PW_TYPE_NOT_EMPTY);
1410         tmpl = (type & PW_TYPE_TMPL);
1411         multi = (type & PW_TYPE_MULTI);
1412
1413         if (attribute) required = true;
1414         if (required) cant_be_empty = true;     /* May want to review this in the future... */
1415
1416         /*
1417          *      Everything except templates must have a base type.
1418          */
1419         if (!(type & 0xff) && !tmpl) {
1420                 cf_log_err(c_item, "Configuration item \"%s\" must have a data type", name);
1421                 return -1;
1422         }
1423
1424         type &= 0xff;                           /* normal types are small */
1425
1426         rcode = 0;
1427
1428         cp = cf_pair_find(cs, name);
1429
1430         /*
1431          *      No pairs match the configuration item name in the current
1432          *      section, use the default value.
1433          */
1434         if (!cp) {
1435                 if (deprecated) return 0;       /* Don't set the default value */
1436
1437                 rcode = 1;
1438                 value = dflt;
1439         /*
1440          *      Something matched, used the CONF_PAIR value.
1441          */
1442         } else {
1443                 CONF_PAIR *next = cp;
1444                 value = cp->value;
1445                 cp->parsed = true;
1446                 c_item = &cp->item;
1447
1448                 /*
1449                  *      @fixme We should actually validate
1450                  *      the value of the pairs too
1451                  */
1452                 if (multi) while ((next = cf_pair_find_next(cs, next, name))) {
1453                         next->parsed = true;
1454                 }
1455
1456                 if (deprecated) {
1457                         cf_log_err(c_item, "Configuration item \"%s\" is deprecated", name);
1458
1459                         return -2;
1460                 }
1461         }
1462
1463         if (!value) {
1464                 if (required) {
1465                 is_required:
1466                         cf_log_err(c_item, "Configuration item \"%s\" must have a value", name);
1467
1468                         return -1;
1469                 }
1470                 return rcode;
1471         }
1472
1473         if ((value[0] == '\0') && cant_be_empty) {
1474         cant_be_empty:
1475                 cf_log_err(c_item, "Configuration item \"%s\" must not be empty (zero length)", name);
1476                 if (!required) cf_log_err(c_item, "Comment item to silence this message");
1477
1478                 return -1;
1479         }
1480
1481
1482         /*
1483          *      Process a value as a LITERAL template.  Once all of
1484          *      the attrs and xlats are defined, the pass2 code
1485          *      converts it to the appropriate type.
1486          */
1487         if (tmpl) {
1488                 vp_tmpl_t *vpt;
1489
1490                 if (!value) {
1491                         *(vp_tmpl_t **)data = NULL;
1492                         return 0;
1493                 }
1494
1495                 rad_assert(!attribute);
1496                 vpt = tmpl_alloc(cs, TMPL_TYPE_LITERAL, value, strlen(value));
1497                 *(vp_tmpl_t **)data = vpt;
1498
1499                 return 0;
1500         }
1501
1502         switch (type) {
1503         case PW_TYPE_BOOLEAN:
1504                 /*
1505                  *      Allow yes/no, true/false, and on/off
1506                  */
1507                 if ((strcasecmp(value, "yes") == 0) ||
1508                     (strcasecmp(value, "true") == 0) ||
1509                     (strcasecmp(value, "on") == 0)) {
1510                         *(bool *)data = true;
1511                 } else if ((strcasecmp(value, "no") == 0) ||
1512                            (strcasecmp(value, "false") == 0) ||
1513                            (strcasecmp(value, "off") == 0)) {
1514                         *(bool *)data = false;
1515                 } else {
1516                         *(bool *)data = false;
1517                         cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
1518                                "variable %s", value, name);
1519                         return -1;
1520                 }
1521                 cf_log_info(cs, "%.*s\t%s = %s",
1522                             cs->depth, parse_spaces, name, value);
1523                 break;
1524
1525         case PW_TYPE_INTEGER:
1526         {
1527                 unsigned long v = strtoul(value, 0, 0);
1528
1529                 /*
1530                  *      Restrict integer values to 0-INT32_MAX, this means
1531                  *      it will always be safe to cast them to a signed type
1532                  *      for comparisons, and imposes the same range limit as
1533                  *      before we switched to using an unsigned type to
1534                  *      represent config item integers.
1535                  */
1536                 if (v > INT32_MAX) {
1537                         cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1538                                    name, INT32_MAX);
1539                         return -1;
1540                 }
1541
1542                 *(uint32_t *)data = v;
1543                 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint32_t *)data);
1544         }
1545                 break;
1546
1547         case PW_TYPE_BYTE:
1548         {
1549                 unsigned long v = strtoul(value, 0, 0);
1550
1551                 if (v > UINT8_MAX) {
1552                         cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1553                                    name, UINT8_MAX);
1554                         return -1;
1555                 }
1556                 *(uint8_t *)data = (uint8_t) v;
1557                 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint8_t *)data);
1558         }
1559                 break;
1560
1561         case PW_TYPE_SHORT:
1562         {
1563                 unsigned long v = strtoul(value, 0, 0);
1564
1565                 if (v > UINT16_MAX) {
1566                         cf_log_err(&(cs->item), "Invalid value \"%s\" for variable %s, must be between 0-%u", value,
1567                                    name, UINT16_MAX);
1568                         return -1;
1569                 }
1570                 *(uint16_t *)data = (uint16_t) v;
1571                 cf_log_info(cs, "%.*s\t%s = %u", cs->depth, parse_spaces, name, *(uint16_t *)data);
1572         }
1573                 break;
1574
1575         case PW_TYPE_INTEGER64:
1576                 *(uint64_t *)data = strtoull(value, 0, 0);
1577                 cf_log_info(cs, "%.*s\t%s = %" PRIu64, cs->depth, parse_spaces, name, *(uint64_t *)data);
1578                 break;
1579
1580         case PW_TYPE_SIGNED:
1581                 *(int32_t *)data = strtol(value, 0, 0);
1582                 cf_log_info(cs, "%.*s\t%s = %d", cs->depth, parse_spaces, name, *(int32_t *)data);
1583                 break;
1584
1585         case PW_TYPE_STRING:
1586                 q = (char **) data;
1587                 if (*q != NULL) {
1588                         talloc_free(*q);
1589                 }
1590
1591                 /*
1592                  *      Expand variables which haven't already been
1593                  *      expanded automagically when the configuration
1594                  *      file was read.
1595                  */
1596                 if (value == dflt) {
1597                         int lineno = 0;
1598
1599                         lineno = cs->item.lineno;
1600
1601                         value = cf_expand_variables("<internal>",
1602                                                     &lineno,
1603                                                     cs, buffer, sizeof(buffer),
1604                                                     value, NULL);
1605                         if (!value) {
1606                                 cf_log_err(&(cs->item),"Failed expanding variable %s", name);
1607                                 return -1;
1608                         }
1609                 }
1610
1611                 if (required && !value) goto is_required;
1612                 if (cant_be_empty && (value[0] == '\0')) goto cant_be_empty;
1613
1614                 if (attribute) {
1615                         if (!dict_attrbyname(value)) {
1616                                 if (!cp) {
1617                                         cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
1618                                                    value, name);
1619                                 } else {
1620                                         cf_log_err(&(cp->item), "No such attribute '%s'", value);
1621                                 }
1622                                 return -1;
1623                         }
1624                 }
1625
1626                 /*
1627                  *      Hide secrets when using "radiusd -X".
1628                  */
1629                 if (secret && (rad_debug_lvl <= 2)) {
1630                         cf_log_info(cs, "%.*s\t%s = <<< secret >>>",
1631                                     cs->depth, parse_spaces, name);
1632                 } else {
1633                         cf_log_info(cs, "%.*s\t%s = \"%s\"",
1634                                     cs->depth, parse_spaces, name, value ? value : "(null)");
1635                 }
1636                 *q = value ? talloc_typed_strdup(cs, value) : NULL;
1637
1638                 /*
1639                  *      If there's data AND it's an input file, check
1640                  *      that we can read it.  This check allows errors
1641                  *      to be caught as early as possible, during
1642                  *      server startup.
1643                  */
1644                 if (*q && file_input && !cf_file_input(cs, *q)) {
1645                         return -1;
1646                 }
1647                 break;
1648
1649         case PW_TYPE_IPV4_ADDR:
1650         case PW_TYPE_IPV4_PREFIX:
1651                 ipaddr = data;
1652
1653                 if (fr_pton4(ipaddr, value, -1, true, false) < 0) {
1654                         ERROR("%s", fr_strerror());
1655                         return -1;
1656                 }
1657                 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1658                 break;
1659
1660         case PW_TYPE_IPV6_ADDR:
1661         case PW_TYPE_IPV6_PREFIX:
1662                 ipaddr = data;
1663
1664                 if (fr_pton6(ipaddr, value, -1, true, false) < 0) {
1665                         ERROR("%s", fr_strerror());
1666                         return -1;
1667                 }
1668                 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1669                 break;
1670
1671         case PW_TYPE_COMBO_IP_ADDR:
1672         case PW_TYPE_COMBO_IP_PREFIX:
1673                 ipaddr = data;
1674
1675                 if (fr_pton(ipaddr, value, -1, AF_UNSPEC, true) < 0) {
1676                         ERROR("%s", fr_strerror());
1677                         return -1;
1678                 }
1679                 if (fr_item_validate_ipaddr(cs, name, type, value, ipaddr) < 0) return -1;
1680                 break;
1681
1682         case PW_TYPE_TIMEVAL: {
1683                 int sec;
1684                 char *end;
1685                 struct timeval tv;
1686
1687                 sec = strtoul(value, &end, 10);
1688                 tv.tv_sec = sec;
1689                 tv.tv_usec = 0;
1690                 if (*end == '.') {
1691                         size_t len;
1692
1693                         len = strlen(end + 1);
1694
1695                         if (len > 6) {
1696                                 ERROR("Too much precision for timeval");
1697                                 return -1;
1698                         }
1699
1700                         /*
1701                          *      If they write "0.1", that means
1702                          *      "10000" microseconds.
1703                          */
1704                         sec = strtoul(end + 1, NULL, 10);
1705                         while (len < 6) {
1706                                 sec *= 10;
1707                                 len++;
1708                         }
1709
1710                         tv.tv_usec = sec;
1711                 }
1712                 cf_log_info(cs, "%.*s\t%s = %d.%06d",
1713                             cs->depth, parse_spaces, name, (int) tv.tv_sec, (int) tv.tv_usec);
1714                 memcpy(data, &tv, sizeof(tv));
1715                 }
1716                 break;
1717
1718         default:
1719                 /*
1720                  *      If we get here, it's a sanity check error.
1721                  *      It's not an error parsing the configuration
1722                  *      file.
1723                  */
1724                 rad_assert(type > PW_TYPE_INVALID);
1725                 rad_assert(type < PW_TYPE_MAX);
1726
1727                 ERROR("type '%s' is not supported in the configuration files",
1728                        fr_int2str(dict_attr_types, type, "?Unknown?"));
1729                 return -1;
1730         } /* switch over variable type */
1731
1732         if (!cp) {
1733                 CONF_PAIR *cpn;
1734
1735                 cpn = cf_pair_alloc(cs, name, value, T_OP_SET, T_BARE_WORD, T_BARE_WORD);
1736                 if (!cpn) return -1;
1737                 cpn->parsed = true;
1738                 cpn->item.filename = "<internal>";
1739                 cpn->item.lineno = 0;
1740                 cf_item_add(cs, &(cpn->item));
1741         }
1742
1743         return rcode;
1744 }
1745
1746
1747 /*
1748  *      A copy of cf_section_parse that initializes pointers before
1749  *      parsing them.
1750  */
1751 static void cf_section_parse_init(CONF_SECTION *cs, void *base,
1752                                   CONF_PARSER const *variables)
1753 {
1754         int i;
1755
1756         for (i = 0; variables[i].name != NULL; i++) {
1757                 if (variables[i].type == PW_TYPE_SUBSECTION) {
1758                         CONF_SECTION *subcs;
1759
1760                         if (!variables[i].dflt) continue;
1761
1762                         subcs = cf_section_sub_find(cs, variables[i].name);
1763
1764                         /*
1765                          *      If there's no subsection in the
1766                          *      config, BUT the CONF_PARSER wants one,
1767                          *      then create an empty one.  This is so
1768                          *      that we can track the strings,
1769                          *      etc. allocated in the subsection.
1770                          */
1771                         if (!subcs) {
1772                                 subcs = cf_section_alloc(cs, variables[i].name, NULL);
1773                                 if (!subcs) return;
1774
1775                                 subcs->item.filename = cs->item.filename;
1776                                 subcs->item.lineno = cs->item.lineno;
1777                                 cf_item_add(cs, &(subcs->item));
1778                         }
1779
1780                         cf_section_parse_init(subcs, (uint8_t *)base + variables[i].offset,
1781                                               (CONF_PARSER const *) variables[i].dflt);
1782                         continue;
1783                 }
1784
1785                 if ((variables[i].type != PW_TYPE_STRING) &&
1786                     (variables[i].type != PW_TYPE_FILE_INPUT) &&
1787                     (variables[i].type != PW_TYPE_FILE_OUTPUT)) {
1788                         continue;
1789                 }
1790
1791                 if (variables[i].data) {
1792                         *(char **) variables[i].data = NULL;
1793                 } else if (base) {
1794                         *(char **) (((char *)base) + variables[i].offset) = NULL;
1795                 } else {
1796                         continue;
1797                 }
1798         } /* for all variables in the configuration section */
1799 }
1800
1801
1802 static void cf_section_parse_warn(CONF_SECTION *cs)
1803 {
1804         CONF_ITEM *ci;
1805
1806         for (ci = cs->children; ci; ci = ci->next) {
1807                 /*
1808                  *      Don't recurse on sections. We can only safely
1809                  *      check conf pairs at the same level as the
1810                  *      section that was just parsed.
1811                  */
1812                 if (ci->type == CONF_ITEM_SECTION) continue;
1813                 if (ci->type == CONF_ITEM_PAIR) {
1814                         CONF_PAIR *cp;
1815
1816                         cp = cf_item_to_pair(ci);
1817                         if (cp->parsed) continue;
1818
1819                         WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
1820                              cp->item.filename ? cp->item.filename : "unknown",
1821                              cp->item.lineno ? cp->item.lineno : 0,
1822                                 cp->attr);
1823                 }
1824
1825                 /*
1826                  *      Skip everything else.
1827                  */
1828         }
1829 }
1830
1831 /** Parse a configuration section into user-supplied variables
1832  *
1833  * @param cs to parse.
1834  * @param base pointer to a struct to fill with data.  Any buffers will also be talloced
1835  *      using this parent as a pointer.
1836  * @param variables mappings between struct fields and #CONF_ITEM s.
1837  * @return
1838  *      - 0 on success.
1839  *      - -1 on general error.
1840  *      - -2 if a deprecated #CONF_ITEM was found.
1841  */
1842 int cf_section_parse(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1843 {
1844         int ret = 0;
1845         int i;
1846         void *data;
1847
1848         cs->variables = variables; /* this doesn't hurt anything */
1849
1850         if (!cs->name2) {
1851                 cf_log_info(cs, "%.*s%s {", cs->depth, parse_spaces, cs->name1);
1852         } else {
1853                 cf_log_info(cs, "%.*s%s %s {", cs->depth, parse_spaces, cs->name1, cs->name2);
1854         }
1855
1856         cf_section_parse_init(cs, base, variables);
1857
1858         /*
1859          *      Handle the known configuration parameters.
1860          */
1861         for (i = 0; variables[i].name != NULL; i++) {
1862                 /*
1863                  *      Handle subsections specially
1864                  */
1865                 if (variables[i].type == PW_TYPE_SUBSECTION) {
1866                         CONF_SECTION *subcs;
1867
1868                         subcs = cf_section_sub_find(cs, variables[i].name);
1869                         /*
1870                          *      Default in this case is overloaded to mean a pointer
1871                          *      to the CONF_PARSER struct for the subsection.
1872                          */
1873                         if (!variables[i].dflt || !subcs) {
1874                                 ERROR("Internal sanity check 1 failed in cf_section_parse %s", variables[i].name);
1875                                 ret = -1;
1876                                 goto finish;
1877                         }
1878
1879                         ret = cf_section_parse(subcs, (uint8_t *)base + variables[i].offset,
1880                                                (CONF_PARSER const *) variables[i].dflt);
1881                         if (ret < 0) goto finish;
1882                         continue;
1883                 } /* else it's a CONF_PAIR */
1884
1885                 if (variables[i].data) {
1886                         data = variables[i].data; /* prefer this. */
1887                 } else if (base) {
1888                         data = ((char *)base) + variables[i].offset;
1889                 } else {
1890                         ERROR("Internal sanity check 2 failed in cf_section_parse");
1891                         ret = -1;
1892                         goto finish;
1893                 }
1894
1895                 /*
1896                  *      Parse the pair we found, or a default value.
1897                  */
1898                 ret = cf_item_parse(cs, variables[i].name, variables[i].type, data, variables[i].dflt);
1899                 switch (ret) {
1900                 case 1:         /* Used default */
1901                         ret = 0;
1902                         break;
1903
1904                 case 0:         /* OK */
1905                         break;
1906
1907                 case -1:        /* Parse error */
1908                         goto finish;
1909
1910                 case -2:        /* Deprecated CONF ITEM */
1911                         if ((variables[i + 1].offset == variables[i].offset) &&
1912                             (variables[i + 1].data == variables[i].data)) {
1913                                 cf_log_err(&(cs->item), "Replace \"%s\" with \"%s\"", variables[i].name,
1914                                            variables[i + 1].name);
1915                         }
1916                         goto finish;
1917                 }
1918         } /* for all variables in the configuration section */
1919
1920         /*
1921          *      Ensure we have a proper terminator, type so we catch
1922          *      missing terminators reliably
1923          */
1924         rad_assert(variables[i].type == -1);
1925
1926         /*
1927          *      Warn about items in the configuration which weren't
1928          *      checked during parsing.
1929          */
1930         if (rad_debug_lvl >= 3) cf_section_parse_warn(cs);
1931
1932         cs->base = base;
1933
1934         cf_log_info(cs, "%.*s}", cs->depth, parse_spaces);
1935
1936 finish:
1937         return ret;
1938 }
1939
1940
1941 /*
1942  *      Check XLAT things in pass 2.  But don't cache the xlat stuff anywhere.
1943  */
1944 int cf_section_parse_pass2(CONF_SECTION *cs, void *base, CONF_PARSER const *variables)
1945 {
1946         int i;
1947         ssize_t slen;
1948         char const *error;
1949         char *value = NULL;
1950         xlat_exp_t *xlat;
1951
1952         /*
1953          *      Handle the known configuration parameters.
1954          */
1955         for (i = 0; variables[i].name != NULL; i++) {
1956                 CONF_PAIR *cp;
1957                 void *data;
1958
1959                 /*
1960                  *      Handle subsections specially
1961                  */
1962                 if (variables[i].type == PW_TYPE_SUBSECTION) {
1963                         CONF_SECTION *subcs;
1964                         subcs = cf_section_sub_find(cs, variables[i].name);
1965
1966                         if (cf_section_parse_pass2(subcs, (uint8_t *)base + variables[i].offset,
1967                                                    (CONF_PARSER const *) variables[i].dflt) < 0) {
1968                                 return -1;
1969                         }
1970                         continue;
1971                 } /* else it's a CONF_PAIR */
1972
1973                 /*
1974                  *      Figure out which data we need to fix.
1975                  */
1976                 if (variables[i].data) {
1977                         data = variables[i].data; /* prefer this. */
1978                 } else if (base) {
1979                         data = ((char *)base) + variables[i].offset;
1980                 } else {
1981                         data = NULL;
1982                 }
1983
1984                 cp = cf_pair_find(cs, variables[i].name);
1985                 xlat = NULL;
1986
1987         redo:
1988                 if (!cp || !cp->value || !data) continue;
1989
1990                 if ((cp->rhs_type != T_DOUBLE_QUOTED_STRING) &&
1991                     (cp->rhs_type != T_BARE_WORD)) continue;
1992
1993                 /*
1994                  *      Non-xlat expansions shouldn't have xlat!
1995                  */
1996                 if (((variables[i].type & PW_TYPE_XLAT) == 0) &&
1997                     ((variables[i].type & PW_TYPE_TMPL) == 0)) {
1998                         /*
1999                          *      Ignore %{... in shared secrets.
2000                          *      They're never dynamically expanded.
2001                          */
2002                         if ((variables[i].type & PW_TYPE_SECRET) != 0) continue;
2003
2004                         if (strstr(cp->value, "%{") != NULL) {
2005                                 WARN("%s[%d]: Found dynamic expansion in string which will not be dynamically expanded",
2006                                      cp->item.filename ? cp->item.filename : "unknown",
2007                                      cp->item.lineno ? cp->item.lineno : 0);
2008                         }
2009                         continue;
2010                 }
2011
2012                 /*
2013                  *      Parse (and throw away) the xlat string.
2014                  *
2015                  *      FIXME: All of these should be converted from PW_TYPE_XLAT
2016                  *      to PW_TYPE_TMPL.
2017                  */
2018                 if ((variables[i].type & PW_TYPE_XLAT) != 0) {
2019                         /*
2020                          *      xlat expansions should be parseable.
2021                          */
2022                         value = talloc_strdup(cs, cp->value); /* modified by xlat_tokenize */
2023                         xlat = NULL;
2024
2025                         slen = xlat_tokenize(cs, value, &xlat, &error);
2026                         if (slen < 0) {
2027                                 char *spaces, *text;
2028
2029                         error:
2030                                 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
2031
2032                                 cf_log_err(&cp->item, "Failed parsing expanded string:");
2033                                 cf_log_err(&cp->item, "%s", text);
2034                                 cf_log_err(&cp->item, "%s^ %s", spaces, error);
2035
2036                                 talloc_free(spaces);
2037                                 talloc_free(text);
2038                                 talloc_free(value);
2039                                 talloc_free(xlat);
2040                                 return -1;
2041                         }
2042
2043                         talloc_free(value);
2044                         talloc_free(xlat);
2045                 }
2046
2047                 /*
2048                  *      Convert the LITERAL template to the actual
2049                  *      type.
2050                  */
2051                 if ((variables[i].type & PW_TYPE_TMPL) != 0) {
2052                         vp_tmpl_t *vpt;
2053
2054                         slen = tmpl_afrom_str(cs, &vpt, cp->value, talloc_array_length(cp->value) - 1,
2055                                               cp->rhs_type,
2056                                               REQUEST_CURRENT, PAIR_LIST_REQUEST, true);
2057                         if (slen < 0) {
2058                                 error = fr_strerror();
2059                                 goto error;
2060                         }
2061
2062                         /*
2063                          *      Sanity check
2064                          *
2065                          *      Don't add default - update with new types.
2066                          */
2067                         switch (vpt->type) {
2068                         /*
2069                          *      All attributes should have been defined by this point.
2070                          */
2071                         case TMPL_TYPE_ATTR_UNDEFINED:
2072                                 cf_log_err(&cp->item, "Unknown attribute '%s'", vpt->tmpl_unknown_name);
2073                                 return -1;
2074
2075                         case TMPL_TYPE_LITERAL:
2076                         case TMPL_TYPE_ATTR:
2077                         case TMPL_TYPE_LIST:
2078                         case TMPL_TYPE_DATA:
2079                         case TMPL_TYPE_EXEC:
2080                         case TMPL_TYPE_XLAT:
2081                         case TMPL_TYPE_XLAT_STRUCT:
2082                                 break;
2083
2084                         case TMPL_TYPE_UNKNOWN:
2085                         case TMPL_TYPE_REGEX:
2086                         case TMPL_TYPE_REGEX_STRUCT:
2087                         case TMPL_TYPE_NULL:
2088                                 rad_assert(0);
2089                         }
2090
2091                         talloc_free(*(vp_tmpl_t **)data);
2092                         *(vp_tmpl_t **)data = vpt;
2093                 }
2094
2095                 /*
2096                  *      If the "multi" flag is set, check all of them.
2097                  */
2098                 if ((variables[i].type & PW_TYPE_MULTI) != 0) {
2099                         cp = cf_pair_find_next(cs, cp, cp->attr);
2100                         goto redo;
2101                 }
2102         } /* for all variables in the configuration section */
2103
2104         return 0;
2105 }
2106
2107 /*
2108  *      Merge the template so everyting else "just works".
2109  */
2110 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
2111 {
2112         CONF_ITEM *ci;
2113
2114         if (!cs || !template) return true;
2115
2116         cs->template = NULL;
2117
2118         /*
2119          *      Walk over the template, adding its' entries to the
2120          *      current section.  But only if the entries don't
2121          *      already exist in the current section.
2122          */
2123         for (ci = template->children; ci; ci = ci->next) {
2124                 if (ci->type == CONF_ITEM_PAIR) {
2125                         CONF_PAIR *cp1, *cp2;
2126
2127                         /*
2128                          *      It exists, don't over-write it.
2129                          */
2130                         cp1 = cf_item_to_pair(ci);
2131                         if (cf_pair_find(cs, cp1->attr)) {
2132                                 continue;
2133                         }
2134
2135                         /*
2136                          *      Create a new pair with all of the data
2137                          *      of the old one.
2138                          */
2139                         cp2 = cf_pair_dup(cs, cp1);
2140                         if (!cp2) return false;
2141
2142                         cp2->item.filename = cp1->item.filename;
2143                         cp2->item.lineno = cp1->item.lineno;
2144
2145                         cf_item_add(cs, &(cp2->item));
2146                         continue;
2147                 }
2148
2149                 if (ci->type == CONF_ITEM_SECTION) {
2150                         CONF_SECTION *subcs1, *subcs2;
2151
2152                         subcs1 = cf_item_to_section(ci);
2153                         rad_assert(subcs1 != NULL);
2154
2155                         subcs2 = cf_section_sub_find_name2(cs, subcs1->name1, subcs1->name2);
2156                         if (subcs2) {
2157                                 /*
2158                                  *      sub-sections get merged.
2159                                  */
2160                                 if (!cf_template_merge(subcs2, subcs1)) {
2161                                         return false;
2162                                 }
2163                                 continue;
2164                         }
2165
2166                         /*
2167                          *      Our section doesn't have a matching
2168                          *      sub-section.  Copy it verbatim from
2169                          *      the template.
2170                          */
2171                         subcs2 = cf_section_dup(cs, subcs1,
2172                                                 cf_section_name1(subcs1), cf_section_name2(subcs1),
2173                                                 false);
2174                         if (!subcs2) return false;
2175
2176                         subcs2->item.filename = subcs1->item.filename;
2177                         subcs2->item.lineno = subcs1->item.lineno;
2178
2179                         cf_item_add(cs, &(subcs2->item));
2180                         continue;
2181                 }
2182
2183                 /* ignore everything else */
2184         }
2185
2186         return true;
2187 }
2188
2189 static char const *cf_local_file(char const *base, char const *filename,
2190                                  char *buffer, size_t bufsize)
2191 {
2192         size_t dirsize;
2193         char *p;
2194
2195         strlcpy(buffer, base, bufsize);
2196
2197         p = strrchr(buffer, FR_DIR_SEP);
2198         if (!p) return filename;
2199         if (p[1]) {             /* ./foo */
2200                 p[1] = '\0';
2201         }
2202
2203         dirsize = (p - buffer) + 1;
2204
2205         if ((dirsize + strlen(filename)) >= bufsize) {
2206                 return NULL;
2207         }
2208
2209         strlcpy(p + 1, filename, bufsize - dirsize);
2210
2211         return buffer;
2212 }
2213
2214
2215 /*
2216  *      Read a part of the config file.
2217  */
2218 static int cf_section_read(char const *filename, int *lineno, FILE *fp,
2219                            CONF_SECTION *current)
2220
2221 {
2222         CONF_SECTION *this, *css;
2223         CONF_PAIR *cpn;
2224         char const *ptr;
2225         char const *value;
2226         char buf[8192];
2227         char buf1[8192];
2228         char buf2[8192];
2229         char buf3[8192];
2230         char buf4[8192];
2231         FR_TOKEN t1 = T_INVALID, t2, t3;
2232         bool has_spaces = false;
2233         bool pass2;
2234         char *cbuf = buf;
2235         size_t len;
2236
2237         this = current;         /* add items here */
2238
2239         /*
2240          *      Read, checking for line continuations ('\\' at EOL)
2241          */
2242         for (;;) {
2243                 int at_eof;
2244                 css = NULL;
2245
2246                 /*
2247                  *      Get data, and remember if we are at EOF.
2248                  */
2249                 at_eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
2250                 (*lineno)++;
2251
2252                 /*
2253                  *      We read the entire 8k worth of data: complain.
2254                  *      Note that we don't care if the last character
2255                  *      is \n: it's still forbidden.  This means that
2256                  *      the maximum allowed length of text is 8k-1, which
2257                  *      should be plenty.
2258                  */
2259                 len = strlen(cbuf);
2260                 if ((cbuf + len + 1) >= (buf + sizeof(buf))) {
2261                         ERROR("%s[%d]: Line too long",
2262                                filename, *lineno);
2263                         return -1;
2264                 }
2265
2266                 if (has_spaces) {
2267                         ptr = cbuf;
2268                         while (isspace((int) *ptr)) ptr++;
2269
2270                         if (ptr > cbuf) {
2271                                 memmove(cbuf, ptr, len - (ptr - cbuf));
2272                                 len -= (ptr - cbuf);
2273                         }
2274                 }
2275
2276                 /*
2277                  *      Not doing continuations: check for edge
2278                  *      conditions.
2279                  */
2280                 if (cbuf == buf) {
2281                         if (at_eof) break;
2282
2283                         ptr = buf;
2284                         while (*ptr && isspace((int) *ptr)) ptr++;
2285
2286                         if (!*ptr || (*ptr == '#')) continue;
2287
2288                 } else if (at_eof || (len == 0)) {
2289                         ERROR("%s[%d]: Continuation at EOF is illegal",
2290                                filename, *lineno);
2291                         return -1;
2292                 }
2293
2294                 /*
2295                  *      See if there's a continuation.
2296                  */
2297                 while ((len > 0) &&
2298                        ((cbuf[len - 1] == '\n') || (cbuf[len - 1] == '\r'))) {
2299                         len--;
2300                         cbuf[len] = '\0';
2301                 }
2302
2303                 if ((len > 0) && (cbuf[len - 1] == '\\')) {
2304                         /*
2305                          *      Check for "suppress spaces" magic.
2306                          */
2307                         if (!has_spaces && (len > 2) && (cbuf[len - 2] == '"')) {
2308                                 has_spaces = true;
2309                         }
2310
2311                         cbuf[len - 1] = '\0';
2312                         cbuf += len - 1;
2313                         continue;
2314                 }
2315
2316                 ptr = cbuf = buf;
2317                 has_spaces = false;
2318
2319         get_more:
2320                 pass2 = false;
2321
2322                 /*
2323                  *      The parser is getting to be evil.
2324                  */
2325                 while ((*ptr == ' ') || (*ptr == '\t')) ptr++;
2326
2327                 if (((ptr[0] == '%') && (ptr[1] == '{')) ||
2328                     (ptr[0] == '`')) {
2329                         int hack;
2330
2331                         if (ptr[0] == '%') {
2332                                 hack = rad_copy_variable(buf1, ptr);
2333                         } else {
2334                                 hack = rad_copy_string(buf1, ptr);
2335                         }
2336                         if (hack < 0) {
2337                                 ERROR("%s[%d]: Invalid expansion: %s",
2338                                        filename, *lineno, ptr);
2339                                 return -1;
2340                         }
2341
2342                         ptr += hack;
2343
2344                         t2 = gettoken(&ptr, buf2, sizeof(buf2), true);
2345                         switch (t2) {
2346                         case T_EOL:
2347                         case T_HASH:
2348                                 goto do_bare_word;
2349
2350                         default:
2351                                 ERROR("%s[%d]: Invalid expansion: %s",
2352                                        filename, *lineno, ptr);
2353                                 return -1;
2354                         }
2355                 } else {
2356                         t1 = gettoken(&ptr, buf1, sizeof(buf1), true);
2357                 }
2358
2359                 /*
2360                  *      The caller eats "name1 name2 {", and calls us
2361                  *      for the data inside of the section.  So if we
2362                  *      receive a closing brace, then it must mean the
2363                  *      end of the section.
2364                  */
2365                if (t1 == T_RCBRACE) {
2366                        if (this == current) {
2367                                ERROR("%s[%d]: Too many closing braces",
2368                                       filename, *lineno);
2369                                return -1;
2370                        }
2371
2372                        /*
2373                         *       Merge the template into the existing
2374                         *       section.  This uses more memory, but
2375                         *       means that templates now work with
2376                         *       sub-sections, etc.
2377                         */
2378                        if (!cf_template_merge(this, this->template)) {
2379                                return -1;
2380                        }
2381
2382                        this = this->item.parent;
2383                        goto check_for_more;
2384                }
2385
2386                if (t1 != T_BARE_WORD) goto skip_keywords;
2387
2388                 /*
2389                  *      Allow for $INCLUDE files
2390                  *
2391                  *      This *SHOULD* work for any level include.
2392                  *      I really really really hate this file.  -cparker
2393                  */
2394                if ((strcasecmp(buf1, "$INCLUDE") == 0) ||
2395                    (strcasecmp(buf1, "$-INCLUDE") == 0)) {
2396                         bool relative = true;
2397
2398                         t2 = getword(&ptr, buf2, sizeof(buf2), true);
2399                         if (t2 != T_EOL) {
2400                                ERROR("%s[%d]: Unexpected text after $INCLUDE",
2401                                      filename, *lineno);
2402                                return -1;
2403                         }
2404
2405                         if (buf2[0] == '$') relative = false;
2406
2407                         value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf2, NULL);
2408                         if (!value) return -1;
2409
2410                         if (!FR_DIR_IS_RELATIVE(value)) relative = false;
2411
2412                         if (relative) {
2413                                 value = cf_local_file(filename, value, buf3,
2414                                                       sizeof(buf3));
2415                                 if (!value) {
2416                                         ERROR("%s[%d]: Directories too deep.",
2417                                                filename, *lineno);
2418                                         return -1;
2419                                 }
2420                         }
2421
2422
2423 #ifdef HAVE_DIRENT_H
2424                         /*
2425                          *      $INCLUDE foo/
2426                          *
2427                          *      Include ALL non-"dot" files in the directory.
2428                          *      careful!
2429                          */
2430                         if (value[strlen(value) - 1] == '/') {
2431                                 DIR             *dir;
2432                                 struct dirent   *dp;
2433                                 struct stat stat_buf;
2434
2435                                 DEBUG2("including files in directory %s", value );
2436 #ifdef S_IWOTH
2437                                 /*
2438                                  *      Security checks.
2439                                  */
2440                                 if (stat(value, &stat_buf) < 0) {
2441                                         ERROR("%s[%d]: Failed reading directory %s: %s",
2442                                                filename, *lineno,
2443                                                value, fr_syserror(errno));
2444                                         return -1;
2445                                 }
2446
2447                                 if ((stat_buf.st_mode & S_IWOTH) != 0) {
2448                                         ERROR("%s[%d]: Directory %s is globally writable.  Refusing to start due to "
2449                                               "insecure configuration", filename, *lineno, value);
2450                                         return -1;
2451                                 }
2452 #endif
2453                                 dir = opendir(value);
2454                                 if (!dir) {
2455                                         ERROR("%s[%d]: Error reading directory %s: %s",
2456                                                filename, *lineno, value,
2457                                                fr_syserror(errno));
2458                                         return -1;
2459                                 }
2460
2461                                 /*
2462                                  *      Read the directory, ignoring "." files.
2463                                  */
2464                                 while ((dp = readdir(dir)) != NULL) {
2465                                         char const *p;
2466
2467                                         if (dp->d_name[0] == '.') continue;
2468
2469                                         /*
2470                                          *      Check for valid characters
2471                                          */
2472                                         for (p = dp->d_name; *p != '\0'; p++) {
2473                                                 if (isalpha((int)*p) ||
2474                                                     isdigit((int)*p) ||
2475                                                     (*p == '-') ||
2476                                                     (*p == '_') ||
2477                                                     (*p == '.')) continue;
2478                                                 break;
2479                                         }
2480                                         if (*p != '\0') continue;
2481
2482                                         snprintf(buf2, sizeof(buf2), "%s%s",
2483                                                  value, dp->d_name);
2484                                         if ((stat(buf2, &stat_buf) != 0) ||
2485                                             S_ISDIR(stat_buf.st_mode)) continue;
2486                                         /*
2487                                          *      Read the file into the current
2488                                          *      configuration section.
2489                                          */
2490                                         if (cf_file_include(this, buf2) < 0) {
2491                                                 closedir(dir);
2492                                                 return -1;
2493                                         }
2494                                 }
2495                                 closedir(dir);
2496                         }  else
2497 #endif
2498                         { /* it was a normal file */
2499                                 if (buf1[1] == '-') {
2500                                         struct stat statbuf;
2501
2502                                         if (stat(value, &statbuf) < 0) {
2503                                                 WARN("Not including file %s: %s", value, fr_syserror(errno));
2504                                                 continue;
2505                                         }
2506                                 }
2507
2508                                 if (cf_file_include(this, value) < 0) {
2509                                         return -1;
2510                                 }
2511                         }
2512                         continue;
2513                 } /* we were in an include */
2514
2515                if (strcasecmp(buf1, "$template") == 0) {
2516                        CONF_ITEM *ci;
2517                        CONF_SECTION *parentcs, *templatecs;
2518                        t2 = getword(&ptr, buf2, sizeof(buf2), true);
2519
2520                        if (t2 != T_EOL) {
2521                                ERROR("%s[%d]: Unexpected text after $TEMPLATE", filename, *lineno);
2522                                return -1;
2523                        }
2524
2525                        parentcs = cf_top_section(current);
2526
2527                        templatecs = cf_section_sub_find(parentcs, "templates");
2528                        if (!templatecs) {
2529                                 ERROR("%s[%d]: No \"templates\" section for reference \"%s\"", filename, *lineno, buf2);
2530                                 return -1;
2531                        }
2532
2533                        ci = cf_reference_item(parentcs, templatecs, buf2);
2534                        if (!ci || (ci->type != CONF_ITEM_SECTION)) {
2535                                 ERROR("%s[%d]: Reference \"%s\" not found", filename, *lineno, buf2);
2536                                 return -1;
2537                        }
2538
2539                        if (!this) {
2540                                 ERROR("%s[%d]: Internal sanity check error in template reference", filename, *lineno);
2541                                 return -1;
2542                        }
2543
2544                        if (this->template) {
2545                                 ERROR("%s[%d]: Section already has a template", filename, *lineno);
2546                                 return -1;
2547                        }
2548
2549                        this->template = cf_item_to_section(ci);
2550                        continue;
2551                }
2552
2553                 /*
2554                  *      Ensure that the user can't add CONF_PAIRs
2555                  *      with 'internal' names;
2556                  */
2557                 if (buf1[0] == '_') {
2558                         ERROR("%s[%d]: Illegal configuration pair name \"%s\"", filename, *lineno, buf1);
2559                         return -1;
2560                 }
2561
2562                 /*
2563                  *      Handle if/elsif specially.
2564                  */
2565                 if ((strcmp(buf1, "if") == 0) || (strcmp(buf1, "elsif") == 0)) {
2566                         ssize_t slen;
2567                         char const *error = NULL;
2568                         char *p;
2569                         CONF_SECTION *server;
2570                         fr_cond_t *cond = NULL;
2571
2572                         /*
2573                          *      if / elsif MUST be inside of a
2574                          *      processing section, which MUST in turn
2575                          *      be inside of a "server" directive.
2576                          */
2577                         if (!this->item.parent) {
2578                         invalid_location:
2579                                 ERROR("%s[%d]: Invalid location for '%s'",
2580                                        filename, *lineno, buf1);
2581                                 return -1;
2582                         }
2583
2584                         /*
2585                          *      Can only have "if" in 3 named sections.
2586                          */
2587                         server = this->item.parent;
2588                         while (server &&
2589                                (strcmp(server->name1, "server") != 0) &&
2590                                (strcmp(server->name1, "policy") != 0) &&
2591                                (strcmp(server->name1, "instantiate") != 0)) {
2592                                 server = server->item.parent;
2593                                 if (!server) goto invalid_location;
2594                         }
2595
2596                         /*
2597                          *      Skip (...) to find the {
2598                          */
2599                         slen = fr_condition_tokenize(this, cf_section_to_item(this), ptr, &cond,
2600                                                      &error, FR_COND_TWO_PASS);
2601                         memcpy(&p, &ptr, sizeof(p));
2602
2603                         if (slen < 0) {
2604                                 if (p[-slen] != '{') goto cond_error;
2605                                 slen = -slen;
2606                         }
2607                         TALLOC_FREE(cond);
2608
2609                         /*
2610                          *      This hack is so that the NEXT stage
2611                          *      doesn't go "too far" in expanding the
2612                          *      variable.  We can parse the conditions
2613                          *      without expanding the ${...} stuff.
2614                          *      BUT we don't want to expand all of the
2615                          *      stuff AFTER the condition.  So we do
2616                          *      two passes.
2617                          *
2618                          *      The first pass is to discover the end
2619                          *      of the condition.  We then expand THAT
2620                          *      string, and do a second pass parsing
2621                          *      the expanded condition.
2622                          */
2623                         p += slen;
2624                         *p = '\0';
2625
2626                         /*
2627                          *      If there's a ${...}.  If so, expand it.
2628                          */
2629                         if (strchr(ptr, '$') != NULL) {
2630                                 ptr = cf_expand_variables(filename, lineno,
2631                                                           this,
2632                                                           buf3, sizeof(buf3),
2633                                                           ptr, NULL);
2634                                 if (!ptr) {
2635                                         ERROR("%s[%d]: Parse error expanding ${...} in condition",
2636                                               filename, *lineno);
2637                                         return -1;
2638                                 }
2639                         } /* else leave it alone */
2640
2641                         css = cf_section_alloc(this, buf1, ptr);
2642                         if (!css) {
2643                                 ERROR("%s[%d]: Failed allocating memory for section",
2644                                       filename, *lineno);
2645                                 return -1;
2646                         }
2647                         css->item.filename = filename;
2648                         css->item.lineno = *lineno;
2649
2650                         slen = fr_condition_tokenize(css, cf_section_to_item(css), ptr, &cond,
2651                                                      &error, FR_COND_TWO_PASS);
2652                         *p = '{'; /* put it back */
2653
2654                 cond_error:
2655                         if (slen < 0) {
2656                                 char *spaces, *text;
2657
2658                                 fr_canonicalize_error(this, &spaces, &text, slen, ptr);
2659
2660                                 ERROR("%s[%d]: Parse error in condition",
2661                                       filename, *lineno);
2662                                 ERROR("%s[%d]: %s", filename, *lineno, text);
2663                                 ERROR("%s[%d]: %s^ %s", filename, *lineno, spaces, error);
2664
2665                                 talloc_free(spaces);
2666                                 talloc_free(text);
2667                                 talloc_free(css);
2668                                 return -1;
2669                         }
2670
2671                         if ((size_t) slen >= (sizeof(buf2) - 1)) {
2672                                 talloc_free(css);
2673                                 ERROR("%s[%d]: Condition is too large after \"%s\"",
2674                                        filename, *lineno, buf1);
2675                                 return -1;
2676                         }
2677
2678                         /*
2679                          *      Copy the expanded and parsed condition
2680                          *      into buf2.  Then, parse the text after
2681                          *      the condition, which now MUST be a '{.
2682                          *
2683                          *      If it wasn't '{' it would have been
2684                          *      caught in the first pass of
2685                          *      conditional parsing, above.
2686                          */
2687                         memcpy(buf2, ptr, slen);
2688                         buf2[slen] = '\0';
2689                         ptr = p;
2690
2691                         if ((t3 = gettoken(&ptr, buf3, sizeof(buf3), true)) != T_LCBRACE) {
2692                                 talloc_free(css);
2693                                 ERROR("%s[%d]: Expected '{' %d",
2694                                       filename, *lineno, t3);
2695                                 return -1;
2696                         }
2697
2698                         /*
2699                          *      Swap the condition with trailing stuff for
2700                          *      the final condition.
2701                          */
2702                         memcpy(&p, &css->name2, sizeof(css->name2));
2703                         talloc_free(p);
2704                         css->name2 = talloc_typed_strdup(css, buf2);
2705
2706                         cf_item_add(this, &(css->item));
2707                         cf_data_add_internal(css, "if", cond, NULL, false);
2708
2709                         /*
2710                          *      The current section is now the child section.
2711                          */
2712                         this = css;
2713                         css = NULL;
2714                         goto check_for_more;
2715                 }
2716
2717         skip_keywords:
2718                 /*
2719                  *      Grab the next token.
2720                  */
2721                 t2 = gettoken(&ptr, buf2, sizeof(buf2), !cf_new_escape);
2722                 switch (t2) {
2723                 case T_EOL:
2724                 case T_HASH:
2725                 case T_COMMA:
2726                 do_bare_word:
2727                         t3 = t2;
2728                         t2 = T_OP_EQ;
2729                         value = NULL;
2730                         goto do_set;
2731
2732                 case T_OP_INCRM:
2733                 case T_OP_ADD:
2734                 case T_OP_CMP_EQ:
2735                 case T_OP_SUB:
2736                 case T_OP_LE:
2737                 case T_OP_GE:
2738                 case T_OP_CMP_FALSE:
2739                         if (!this || (strcmp(this->name1, "update") != 0)) {
2740                                 ERROR("%s[%d]: Invalid operator in assignment",
2741                                        filename, *lineno);
2742                                 return -1;
2743                         }
2744                         /* FALL-THROUGH */
2745
2746                 case T_OP_EQ:
2747                 case T_OP_SET:
2748                         while (isspace((int) *ptr)) ptr++;
2749
2750                         /*
2751                          *      New parser: non-quoted strings are
2752                          *      bare words, and we parse everything
2753                          *      until the next newline, or the next
2754                          *      comma.  If they have { or } in a bare
2755                          *      word, well... too bad.
2756                          */
2757                         if (cf_new_escape && (*ptr != '"') && (*ptr != '\'')
2758                             && (*ptr != '`') && (*ptr != '/')) {
2759                                 const char *q = ptr;
2760
2761                                 t3 = T_BARE_WORD;
2762                                 while (*q && (*q >= ' ') && (*q != ',') &&
2763                                        !isspace(*q)) q++;
2764
2765                                 if ((size_t) (q - ptr) >= sizeof(buf3)) {
2766                                         ERROR("%s[%d]: Parse error: value too long",
2767                                               filename, *lineno);
2768                                         return -1;
2769                                 }
2770
2771                                 memcpy(buf3, ptr, (q - ptr));
2772                                 buf3[q - ptr] = '\0';
2773                                 ptr = q;
2774
2775                         } else {
2776                                 t3 = getstring(&ptr, buf3, sizeof(buf3), !cf_new_escape);
2777                         }
2778
2779                         if (t3 == T_INVALID) {
2780                                 ERROR("%s[%d]: Parse error: %s",
2781                                        filename, *lineno,
2782                                        fr_strerror());
2783                                 return -1;
2784                         }
2785
2786                         /*
2787                          *      Allow "foo" by itself, or "foo = bar"
2788                          */
2789                         switch (t3) {
2790                                 bool soft_fail;
2791
2792                         case T_BARE_WORD:
2793                         case T_DOUBLE_QUOTED_STRING:
2794                         case T_BACK_QUOTED_STRING:
2795                                 value = cf_expand_variables(filename, lineno, this, buf4, sizeof(buf4), buf3, &soft_fail);
2796                                 if (!value) {
2797                                         if (!soft_fail) return -1;
2798
2799                                         /*
2800                                          *      References an item which doesn't exist,
2801                                          *      or which is already marked up as being
2802                                          *      expanded in pass2.  Wait for pass2 to
2803                                          *      do the expansions.
2804                                          */
2805                                         pass2 = true;
2806                                         value = buf3;
2807                                 }
2808                                 break;
2809
2810                         case T_EOL:
2811                         case T_HASH:
2812                                 value = NULL;
2813                                 break;
2814
2815                         default:
2816                                 value = buf3;
2817                                 break;
2818                         }
2819
2820                         /*
2821                          *      Add this CONF_PAIR to our CONF_SECTION
2822                          */
2823                 do_set:
2824                         cpn = cf_pair_alloc(this, buf1, value, t2, t1, t3);
2825                         if (!cpn) return -1;
2826                         cpn->item.filename = filename;
2827                         cpn->item.lineno = *lineno;
2828                         cpn->pass2 = pass2;
2829                         cf_item_add(this, &(cpn->item));
2830
2831                         /*
2832                          *      Hacks for escaping
2833                          */
2834                         if (!cf_new_escape && !this->item.parent && value &&
2835                             (strcmp(buf1, "correct_escapes") == 0) &&
2836                             ((strcmp(value, "true") == 0) ||
2837                              (strcmp(value, "yes") == 0) ||
2838                              (strcmp(value, "1") == 0))) {
2839                                 cf_new_escape = true;
2840                         }
2841
2842                         /*
2843                          *      Require a comma, unless there's a comment.
2844                          */
2845                         while (isspace(*ptr)) ptr++;
2846
2847                         if (*ptr == ',') {
2848                                 ptr++;
2849                                 break;
2850                         }
2851
2852                         /*
2853                          *      module # stuff!
2854                          *      foo = bar # other stuff
2855                          */
2856                         if ((t3 == T_HASH) || (t3 == T_COMMA) || (t3 == T_EOL) || (*ptr == '#')) continue;
2857
2858                         if (!*ptr || (*ptr == '}')) break;
2859
2860                         ERROR("%s[%d]: Syntax error: Expected comma after '%s': %s",
2861                               filename, *lineno, value, ptr);
2862                         return -1;
2863
2864                         /*
2865                          *      No '=', must be a section or sub-section.
2866                          */
2867                 case T_BARE_WORD:
2868                 case T_DOUBLE_QUOTED_STRING:
2869                 case T_SINGLE_QUOTED_STRING:
2870                         t3 = gettoken(&ptr, buf3, sizeof(buf3), true);
2871                         if (t3 != T_LCBRACE) {
2872                                 ERROR("%s[%d]: Expecting section start brace '{' after \"%s %s\"",
2873                                        filename, *lineno, buf1, buf2);
2874                                 return -1;
2875                         }
2876                         /* FALL-THROUGH */
2877
2878                 case T_LCBRACE:
2879                         css = cf_section_alloc(this, buf1,
2880                                                t2 == T_LCBRACE ? NULL : buf2);
2881                         if (!css) {
2882                                 ERROR("%s[%d]: Failed allocating memory for section",
2883                                       filename, *lineno);
2884                                 return -1;
2885                         }
2886
2887                         css->item.filename = filename;
2888                         css->item.lineno = *lineno;
2889                         cf_item_add(this, &(css->item));
2890
2891                         /*
2892                          *      There may not be a name2
2893                          */
2894                         css->name2_type = (t2 == T_LCBRACE) ? T_INVALID : t2;
2895
2896                         /*
2897                          *      The current section is now the child section.
2898                          */
2899                         this = css;
2900                         break;
2901
2902                 case T_INVALID:
2903                         ERROR("%s[%d]: Syntax error in '%s': %s", filename, *lineno, ptr, fr_strerror());
2904
2905                         return -1;
2906
2907                 default:
2908                         ERROR("%s[%d]: Parse error after \"%s\": unexpected token \"%s\"",
2909                               filename, *lineno, buf1, fr_int2str(fr_tokens, t2, "<INVALID>"));
2910
2911                         return -1;
2912                 }
2913
2914         check_for_more:
2915                 /*
2916                  *      Done parsing one thing.  Skip to EOL if possible.
2917                  */
2918                 while (isspace(*ptr)) ptr++;
2919
2920                 if (*ptr == '#') continue;
2921
2922                 if (*ptr) {
2923                         goto get_more;
2924                 }
2925
2926         }
2927
2928         /*
2929          *      See if EOF was unexpected ..
2930          */
2931         if (feof(fp) && (this != current)) {
2932                 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
2933                       filename, *lineno, cf_section_name1(this), cf_section_lineno(this));
2934                 return -1;
2935         }
2936
2937         return 0;
2938 }
2939
2940 /*
2941  *      Include one config file in another.
2942  */
2943 static int cf_file_include(CONF_SECTION *cs, char const *filename_in)
2944 {
2945         FILE            *fp;
2946         int             lineno = 0;
2947         char const      *filename;
2948
2949         /*
2950          *      So we only need to do this once.
2951          */
2952         filename = talloc_strdup(cs, filename_in);
2953
2954         DEBUG2("including configuration file %s", filename);
2955
2956         fp = cf_file_open(cs, filename);
2957         if (!fp) return -1;
2958
2959         if (!cs->item.filename) cs->item.filename = filename;
2960
2961         /*
2962          *      Read the section.  It's OK to have EOF without a
2963          *      matching close brace.
2964          */
2965         if (cf_section_read(filename, &lineno, fp, cs) < 0) {
2966                 fclose(fp);
2967                 return -1;
2968         }
2969
2970         fclose(fp);
2971         return 0;
2972 }
2973
2974
2975 /*
2976  *      Do variable expansion in pass2.
2977  *
2978  *      This is a breadth-first expansion.  "deep
2979  */
2980 static int cf_section_pass2(CONF_SECTION *cs)
2981 {
2982         CONF_ITEM *ci;
2983
2984         for (ci = cs->children; ci; ci = ci->next) {
2985                 char const *value;
2986                 CONF_PAIR *cp;
2987                 char buffer[8192];
2988
2989                 if (ci->type != CONF_ITEM_PAIR) continue;
2990
2991                 cp = cf_item_to_pair(ci);
2992                 if (!cp->value || !cp->pass2) continue;
2993
2994                 rad_assert((cp->rhs_type == T_BARE_WORD) ||
2995                            (cp->rhs_type == T_DOUBLE_QUOTED_STRING) ||
2996                            (cp->rhs_type == T_BACK_QUOTED_STRING));
2997
2998                 value = cf_expand_variables(ci->filename, &ci->lineno, cs, buffer, sizeof(buffer), cp->value, NULL);
2999                 if (!value) return -1;
3000
3001                 rad_const_free(cp->value);
3002                 cp->value = talloc_typed_strdup(cp, value);
3003         }
3004
3005         for (ci = cs->children; ci; ci = ci->next) {
3006                 if (ci->type != CONF_ITEM_SECTION) continue;
3007
3008                 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
3009         }
3010
3011         return 0;
3012 }
3013
3014
3015 /*
3016  *      Bootstrap a config file.
3017  */
3018 int cf_file_read(CONF_SECTION *cs, char const *filename)
3019 {
3020         char *p;
3021         CONF_PAIR *cp;
3022         rbtree_t *tree;
3023
3024         cp = cf_pair_alloc(cs, "confdir", filename, T_OP_SET, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3025         if (!cp) return -1;
3026
3027         p = strrchr(cp->value, FR_DIR_SEP);
3028         if (p) *p = '\0';
3029
3030         cp->item.filename = "<internal>";
3031         cp->item.lineno = -1;
3032         cf_item_add(cs, &(cp->item));
3033
3034         tree = rbtree_create(cs, filename_cmp, NULL, 0);
3035         if (!tree) return -1;
3036
3037         cf_data_add_internal(cs, "filename", tree, NULL, 0);
3038
3039         if (cf_file_include(cs, filename) < 0) return -1;
3040
3041         /*
3042          *      Now that we've read the file, go back through it and
3043          *      expand the variables.
3044          */
3045         if (cf_section_pass2(cs) < 0) return -1;
3046
3047         return 0;
3048 }
3049
3050
3051 void cf_file_free(CONF_SECTION *cs)
3052 {
3053         talloc_free(cs);
3054 }
3055
3056
3057 /*
3058  * Return a CONF_PAIR within a CONF_SECTION.
3059  */
3060 CONF_PAIR *cf_pair_find(CONF_SECTION const *cs, char const *name)
3061 {
3062         CONF_PAIR *cp, mycp;
3063
3064         if (!cs || !name) return NULL;
3065
3066         mycp.attr = name;
3067         cp = rbtree_finddata(cs->pair_tree, &mycp);
3068         if (cp) return cp;
3069
3070         if (!cs->template) return NULL;
3071
3072         return rbtree_finddata(cs->template->pair_tree, &mycp);
3073 }
3074
3075 /*
3076  * Return the attr of a CONF_PAIR
3077  */
3078
3079 char const *cf_pair_attr(CONF_PAIR const *pair)
3080 {
3081         return (pair ? pair->attr : NULL);
3082 }
3083
3084 /*
3085  * Return the value of a CONF_PAIR
3086  */
3087
3088 char const *cf_pair_value(CONF_PAIR const *pair)
3089 {
3090         return (pair ? pair->value : NULL);
3091 }
3092
3093 FR_TOKEN cf_pair_operator(CONF_PAIR const *pair)
3094 {
3095         return (pair ? pair->op : T_INVALID);
3096 }
3097
3098 /** Return the value (lhs) type
3099  *
3100  * @param pair to extract value type from.
3101  * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3102  *      T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3103  */
3104 FR_TOKEN cf_pair_attr_type(CONF_PAIR const *pair)
3105 {
3106         return (pair ? pair->lhs_type : T_INVALID);
3107 }
3108
3109 /** Return the value (rhs) type
3110  *
3111  * @param pair to extract value type from.
3112  * @return one of T_BARE_WORD, T_SINGLE_QUOTED_STRING, T_BACK_QUOTED_STRING
3113  *      T_DOUBLE_QUOTED_STRING or T_INVALID if the pair is NULL.
3114  */
3115 FR_TOKEN cf_pair_value_type(CONF_PAIR const *pair)
3116 {
3117         return (pair ? pair->rhs_type : T_INVALID);
3118 }
3119
3120 /*
3121  * Turn a CONF_PAIR into a VALUE_PAIR
3122  * For now, ignore the "value_type" field...
3123  */
3124 VALUE_PAIR *cf_pairtovp(CONF_PAIR *pair)
3125 {
3126         if (!pair) {
3127                 fr_strerror_printf("Internal error");
3128                 return NULL;
3129         }
3130
3131         if (!pair->value) {
3132                 fr_strerror_printf("No value given for attribute %s", pair->attr);
3133                 return NULL;
3134         }
3135
3136         /*
3137          *      false comparisons never match.  BUT if it's a "string"
3138          *      or `string`, then remember to expand it later.
3139          */
3140         if ((pair->op != T_OP_CMP_FALSE) &&
3141             ((pair->rhs_type == T_DOUBLE_QUOTED_STRING) ||
3142              (pair->rhs_type == T_BACK_QUOTED_STRING))) {
3143                 VALUE_PAIR *vp;
3144
3145                 vp = fr_pair_make(pair, NULL, pair->attr, NULL, pair->op);
3146                 if (!vp) {
3147                         return NULL;
3148                 }
3149
3150                 if (fr_pair_mark_xlat(vp, pair->value) < 0) {
3151                         talloc_free(vp);
3152
3153                         return NULL;
3154                 }
3155
3156                 return vp;
3157         }
3158
3159         return fr_pair_make(pair, NULL, pair->attr, pair->value, pair->op);
3160 }
3161
3162 /*
3163  * Return the first label of a CONF_SECTION
3164  */
3165
3166 char const *cf_section_name1(CONF_SECTION const *cs)
3167 {
3168         return (cs ? cs->name1 : NULL);
3169 }
3170
3171 /*
3172  * Return the second label of a CONF_SECTION
3173  */
3174
3175 char const *cf_section_name2(CONF_SECTION const *cs)
3176 {
3177         return (cs ? cs->name2 : NULL);
3178 }
3179
3180 /** Return name2 if set, else name1
3181  *
3182  */
3183 char const *cf_section_name(CONF_SECTION const *cs)
3184 {
3185         char const *name;
3186
3187         name = cf_section_name2(cs);
3188         if (name) return name;
3189
3190         return cf_section_name1(cs);
3191 }
3192
3193 /*
3194  * Find a value in a CONF_SECTION
3195  */
3196 char const *cf_section_value_find(CONF_SECTION const *cs, char const *attr)
3197 {
3198         CONF_PAIR       *cp;
3199
3200         cp = cf_pair_find(cs, attr);
3201
3202         return (cp ? cp->value : NULL);
3203 }
3204
3205
3206 CONF_SECTION *cf_section_find_name2(CONF_SECTION const *cs,
3207                                     char const *name1, char const *name2)
3208 {
3209         char const      *their2;
3210         CONF_ITEM const *ci;
3211
3212         if (!cs || !name1) return NULL;
3213
3214         for (ci = &(cs->item); ci; ci = ci->next) {
3215                 if (ci->type != CONF_ITEM_SECTION)
3216                         continue;
3217
3218                 if (strcmp(cf_item_to_section(ci)->name1, name1) != 0) {
3219                         continue;
3220                 }
3221
3222                 their2 = cf_item_to_section(ci)->name2;
3223
3224                 if ((!name2 && !their2) ||
3225                     (name2 && their2 && (strcmp(name2, their2) == 0))) {
3226                         return cf_item_to_section(ci);
3227                 }
3228         }
3229
3230         return NULL;
3231 }
3232
3233 /** Find a pair with a name matching attr, after specified pair.
3234  *
3235  * @param cs to search in.
3236  * @param pair to search from (may be NULL).
3237  * @param attr to find (may be NULL in which case any attribute matches).
3238  * @return the next matching CONF_PAIR or NULL if none matched.
3239  */
3240 CONF_PAIR *cf_pair_find_next(CONF_SECTION const *cs,
3241                              CONF_PAIR const *pair, char const *attr)
3242 {
3243         CONF_ITEM       *ci;
3244
3245         if (!cs) return NULL;
3246
3247         /*
3248          *      If pair is NULL and we're trying to find a specific
3249          *      attribute this must be a first time run.
3250          *
3251          *      Find the pair with correct name.
3252          */
3253         if (!pair && attr) return cf_pair_find(cs, attr);
3254
3255         /*
3256          *      Start searching from the next child, or from the head
3257          *      of the list of children (if no pair was provided).
3258          */
3259         for (ci = pair ? pair->item.next : cs->children;
3260              ci;
3261              ci = ci->next) {
3262                 if (ci->type != CONF_ITEM_PAIR) continue;
3263
3264                 if (!attr || strcmp(cf_item_to_pair(ci)->attr, attr) == 0) break;
3265         }
3266
3267         return cf_item_to_pair(ci);
3268 }
3269
3270 /*
3271  * Find a CONF_SECTION, or return the root if name is NULL
3272  */
3273
3274 CONF_SECTION *cf_section_find(char const *name)
3275 {
3276         if (name)
3277                 return cf_section_sub_find(root_config, name);
3278         else
3279                 return root_config;
3280 }
3281
3282 /** Find a sub-section in a section
3283  *
3284  *      This finds ANY section having the same first name.
3285  *      The second name is ignored.
3286  */
3287 CONF_SECTION *cf_section_sub_find(CONF_SECTION const *cs, char const *name)
3288 {
3289         CONF_SECTION mycs;
3290
3291         if (!cs || !name) return NULL;  /* can't find an un-named section */
3292
3293         /*
3294          *      No sub-sections have been defined, so none exist.
3295          */
3296         if (!cs->section_tree) return NULL;
3297
3298         mycs.name1 = name;
3299         mycs.name2 = NULL;
3300         return rbtree_finddata(cs->section_tree, &mycs);
3301 }
3302
3303
3304 /** Find a CONF_SECTION with both names.
3305  *
3306  */
3307 CONF_SECTION *cf_section_sub_find_name2(CONF_SECTION const *cs,
3308                                         char const *name1, char const *name2)
3309 {
3310         CONF_ITEM    *ci;
3311
3312         if (!cs) cs = root_config;
3313         if (!cs) return NULL;
3314
3315         if (name1) {
3316                 CONF_SECTION mycs, *master_cs;
3317
3318                 if (!cs->section_tree) return NULL;
3319
3320                 mycs.name1 = name1;
3321                 mycs.name2 = name2;
3322
3323                 master_cs = rbtree_finddata(cs->section_tree, &mycs);
3324                 if (!master_cs) return NULL;
3325
3326                 /*
3327                  *      Look it up in the name2 tree.  If it's there,
3328                  *      return it.
3329                  */
3330                 if (master_cs->name2_tree) {
3331                         CONF_SECTION *subcs;
3332
3333                         subcs = rbtree_finddata(master_cs->name2_tree, &mycs);
3334                         if (subcs) return subcs;
3335                 }
3336
3337                 /*
3338                  *      We don't insert ourselves into the name2 tree.
3339                  *      So if there's nothing in the name2 tree, maybe
3340                  *      *we* are the answer.
3341                  */
3342                 if (!master_cs->name2 && name2) return NULL;
3343                 if (master_cs->name2 && !name2) return NULL;
3344                 if (!master_cs->name2 && !name2) return master_cs;
3345
3346                 if (strcmp(master_cs->name2, name2) == 0) {
3347                         return master_cs;
3348                 }
3349
3350                 return NULL;
3351         }
3352
3353         /*
3354          *      Else do it the old-fashioned way.
3355          */
3356         for (ci = cs->children; ci; ci = ci->next) {
3357                 CONF_SECTION *subcs;
3358
3359                 if (ci->type != CONF_ITEM_SECTION)
3360                         continue;
3361
3362                 subcs = cf_item_to_section(ci);
3363                 if (!subcs->name2) {
3364                         if (strcmp(subcs->name1, name2) == 0) break;
3365                 } else {
3366                         if (strcmp(subcs->name2, name2) == 0) break;
3367                 }
3368         }
3369
3370         return cf_item_to_section(ci);
3371 }
3372
3373 /*
3374  * Return the next subsection after a CONF_SECTION
3375  * with a certain name1 (char *name1). If the requested
3376  * name1 is NULL, any name1 matches.
3377  */
3378
3379 CONF_SECTION *cf_subsection_find_next(CONF_SECTION const *section,
3380                                       CONF_SECTION const *subsection,
3381                                       char const *name1)
3382 {
3383         CONF_ITEM       *ci;
3384
3385         if (!section) return NULL;
3386
3387         /*
3388          * If subsection is NULL this must be a first time run
3389          * Find the subsection with correct name
3390          */
3391
3392         if (!subsection) {
3393                 ci = section->children;
3394         } else {
3395                 ci = subsection->item.next;
3396         }
3397
3398         for (; ci; ci = ci->next) {
3399                 if (ci->type != CONF_ITEM_SECTION)
3400                         continue;
3401                 if ((name1 == NULL) ||
3402                     (strcmp(cf_item_to_section(ci)->name1, name1) == 0))
3403                         break;
3404         }
3405
3406         return cf_item_to_section(ci);
3407 }
3408
3409
3410 /*
3411  * Return the next section after a CONF_SECTION
3412  * with a certain name1 (char *name1). If the requested
3413  * name1 is NULL, any name1 matches.
3414  */
3415
3416 CONF_SECTION *cf_section_find_next(CONF_SECTION const *section,
3417                                    CONF_SECTION const *subsection,
3418                                    char const *name1)
3419 {
3420         if (!section) return NULL;
3421
3422         if (!section->item.parent) return NULL;
3423
3424         return cf_subsection_find_next(section->item.parent, subsection, name1);
3425 }
3426
3427 /** Return the next item after a CONF_ITEM.
3428  *
3429  */
3430 CONF_ITEM *cf_item_find_next(CONF_SECTION const *section, CONF_ITEM const *item)
3431 {
3432         if (!section) return NULL;
3433
3434         /*
3435          *      If item is NULL this must be a first time run
3436          *      Return the first item
3437          */
3438         if (item == NULL) {
3439                 return section->children;
3440         } else {
3441                 return item->next;
3442         }
3443 }
3444
3445 static void _pair_count(int *count, CONF_SECTION const *cs)
3446 {
3447         CONF_ITEM const *ci;
3448
3449         for (ci = cf_item_find_next(cs, NULL);
3450              ci != NULL;
3451              ci = cf_item_find_next(cs, ci)) {
3452
3453                 if (cf_item_is_section(ci)) {
3454                         _pair_count(count, cf_item_to_section(ci));
3455                         continue;
3456                 }
3457
3458                 (*count)++;
3459         }
3460 }
3461
3462 /** Count the number of conf pairs beneath a section
3463  *
3464  * @param[in] cs to search for items in.
3465  * @return number of pairs nested within section.
3466  */
3467 int cf_pair_count(CONF_SECTION const *cs)
3468 {
3469         int count = 0;
3470
3471         _pair_count(&count, cs);
3472
3473         return count;
3474 }
3475
3476 CONF_SECTION *cf_item_parent(CONF_ITEM const *ci)
3477 {
3478         if (!ci) return NULL;
3479
3480         return ci->parent;
3481 }
3482
3483 int cf_section_lineno(CONF_SECTION const *section)
3484 {
3485         return section->item.lineno;
3486 }
3487
3488 char const *cf_pair_filename(CONF_PAIR const *pair)
3489 {
3490         return pair->item.filename;
3491 }
3492
3493 char const *cf_section_filename(CONF_SECTION const *section)
3494 {
3495         return section->item.filename;
3496 }
3497
3498 int cf_pair_lineno(CONF_PAIR const *pair)
3499 {
3500         return pair->item.lineno;
3501 }
3502
3503 bool cf_item_is_section(CONF_ITEM const *item)
3504 {
3505         return item->type == CONF_ITEM_SECTION;
3506 }
3507
3508 bool cf_item_is_pair(CONF_ITEM const *item)
3509 {
3510         return item->type == CONF_ITEM_PAIR;
3511 }
3512
3513
3514 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, char const *name,
3515                                 void *data, void (*data_free)(void *))
3516 {
3517         CONF_DATA *cd;
3518
3519         cd = talloc_zero(parent, CONF_DATA);
3520         if (!cd) return NULL;
3521
3522         cd->item.type = CONF_ITEM_DATA;
3523         cd->item.parent = parent;
3524         cd->name = talloc_typed_strdup(cd, name);
3525         if (!cd->name) {
3526                 talloc_free(cd);
3527                 return NULL;
3528         }
3529
3530         cd->data = data;
3531         cd->free = data_free;
3532
3533         if (cd->free) {
3534                 talloc_set_destructor(cd, _cf_data_free);
3535         }
3536
3537         return cd;
3538 }
3539
3540 static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name, int flag)
3541 {
3542         if (!cs || !name) return NULL;
3543
3544         /*
3545          *      Find the name in the tree, for speed.
3546          */
3547         if (cs->data_tree) {
3548                 CONF_DATA mycd;
3549
3550                 mycd.name = name;
3551                 mycd.flag = flag;
3552                 return rbtree_finddata(cs->data_tree, &mycd);
3553         }
3554
3555         return NULL;
3556 }
3557
3558 /*
3559  *      Find data from a particular section.
3560  */
3561 void *cf_data_find(CONF_SECTION const *cs, char const *name)
3562 {
3563         CONF_DATA *cd = cf_data_find_internal(cs, name, 0);
3564
3565         if (cd) return cd->data;
3566         return NULL;
3567 }
3568
3569
3570 /*
3571  *      Add named data to a configuration section.
3572  */
3573 static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
3574                                 void *data, void (*data_free)(void *),
3575                                 int flag)
3576 {
3577         CONF_DATA *cd;
3578
3579         if (!cs || !name) return -1;
3580
3581         /*
3582          *      Already exists.  Can't add it.
3583          */
3584         if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
3585
3586         cd = cf_data_alloc(cs, name, data, data_free);
3587         if (!cd) return -1;
3588         cd->flag = flag;
3589
3590         cf_item_add(cs, cf_data_to_item(cd));
3591
3592         return 0;
3593 }
3594
3595 /*
3596  *      Add named data to a configuration section.
3597  */
3598 int cf_data_add(CONF_SECTION *cs, char const *name,
3599                 void *data, void (*data_free)(void *))
3600 {
3601         return cf_data_add_internal(cs, name, data, data_free, 0);
3602 }
3603
3604 /** Remove named data from a configuration section
3605  *
3606  */
3607 void *cf_data_remove(CONF_SECTION *cs, char const *name)
3608 {
3609         CONF_DATA mycd;
3610         CONF_DATA *cd;
3611         void *data;
3612
3613         if (!cs || !name) return NULL;
3614         if (!cs->data_tree) return NULL;
3615
3616         /*
3617          *      Find the name in the tree, for speed.
3618          */
3619         mycd.name = name;
3620         mycd.flag = 0;
3621         cd = rbtree_finddata(cs->data_tree, &mycd);
3622         if (!cd) return NULL;
3623
3624         talloc_set_destructor(cd, NULL);        /* Disarm the destructor */
3625         rbtree_deletebydata(cs->data_tree, &mycd);
3626
3627         data = cd->data;
3628         talloc_free(cd);
3629
3630         return data;
3631 }
3632
3633 /*
3634  *      This is here to make the rest of the code easier to read.  It
3635  *      ties conffile.c to log.c, but it means we don't have to
3636  *      pollute every other function with the knowledge of the
3637  *      configuration internals.
3638  */
3639 void cf_log_err(CONF_ITEM const *ci, char const *fmt, ...)
3640 {
3641         va_list ap;
3642         char buffer[256];
3643
3644         va_start(ap, fmt);
3645         vsnprintf(buffer, sizeof(buffer), fmt, ap);
3646         va_end(ap);
3647
3648         if (ci) {
3649                 ERROR("%s[%d]: %s",
3650                        ci->filename ? ci->filename : "unknown",
3651                        ci->lineno ? ci->lineno : 0,
3652                        buffer);
3653         } else {
3654                 ERROR("<unknown>[*]: %s", buffer);
3655         }
3656 }
3657
3658 void cf_log_err_cs(CONF_SECTION const *cs, char const *fmt, ...)
3659 {
3660         va_list ap;
3661         char buffer[256];
3662
3663         va_start(ap, fmt);
3664         vsnprintf(buffer, sizeof(buffer), fmt, ap);
3665         va_end(ap);
3666
3667         rad_assert(cs != NULL);
3668
3669         ERROR("%s[%d]: %s",
3670                cs->item.filename ? cs->item.filename : "unknown",
3671                cs->item.lineno ? cs->item.lineno : 0,
3672                buffer);
3673 }
3674
3675 void cf_log_err_cp(CONF_PAIR const *cp, char const *fmt, ...)
3676 {
3677         va_list ap;
3678         char buffer[256];
3679
3680         va_start(ap, fmt);
3681         vsnprintf(buffer, sizeof(buffer), fmt, ap);
3682         va_end(ap);
3683
3684         rad_assert(cp != NULL);
3685
3686         ERROR("%s[%d]: %s",
3687                cp->item.filename ? cp->item.filename : "unknown",
3688                cp->item.lineno ? cp->item.lineno : 0,
3689                buffer);
3690 }
3691
3692 void cf_log_info(CONF_SECTION const *cs, char const *fmt, ...)
3693 {
3694         va_list ap;
3695
3696         va_start(ap, fmt);
3697         if ((rad_debug_lvl > 1) && cs) vradlog(L_DBG, fmt, ap);
3698         va_end(ap);
3699 }
3700
3701 /*
3702  *      Wrapper to simplify the code.
3703  */
3704 void cf_log_module(CONF_SECTION const *cs, char const *fmt, ...)
3705 {
3706         va_list ap;
3707         char buffer[256];
3708
3709         va_start(ap, fmt);
3710         if (rad_debug_lvl > 1 && cs) {
3711                 vsnprintf(buffer, sizeof(buffer), fmt, ap);
3712
3713                 DEBUG("%.*s# %s", cs->depth, parse_spaces, buffer);
3714         }
3715         va_end(ap);
3716 }
3717
3718 const CONF_PARSER *cf_section_parse_table(CONF_SECTION *cs)
3719 {
3720         if (!cs) return NULL;
3721
3722         return cs->variables;
3723 }
3724
3725 /*
3726  *      For "switch" and "case" statements.
3727  */
3728 FR_TOKEN cf_section_name2_type(CONF_SECTION const *cs)
3729 {
3730         if (!cs) return T_INVALID;
3731
3732         return cs->name2_type;
3733 }