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