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