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