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