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