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