Fix return code. Clarify code for parsing configuration files
[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 #include <freeradius-devel/ident.h>
30 RCSID("$Id$")
31
32 #include <freeradius-devel/radiusd.h>
33 #include <freeradius-devel/rad_assert.h>
34
35 #ifdef HAVE_DIRENT_H
36 #include <dirent.h>
37
38 #ifdef HAVE_SYS_STAT_H
39 #include <sys/stat.h>
40 #endif
41 #endif
42
43 #include <ctype.h>
44
45 typedef enum conf_type {
46         CONF_ITEM_INVALID = 0,
47         CONF_ITEM_PAIR,
48         CONF_ITEM_SECTION,
49         CONF_ITEM_DATA
50 } CONF_ITEM_TYPE;
51
52 struct conf_item {
53         struct conf_item *next;
54         struct conf_part *parent;
55         int lineno;
56         CONF_ITEM_TYPE type;
57 };
58 struct conf_pair {
59         CONF_ITEM item;
60         char *attr;
61         char *value;
62         LRAD_TOKEN operator;
63 };
64 struct conf_part {
65         CONF_ITEM item;
66         const char *name1;
67         const char *name2;
68         struct conf_item *children;
69         struct conf_item *tail; /* for speed */
70         CONF_SECTION    *template;
71         rbtree_t        *pair_tree; /* and a partridge.. */
72         rbtree_t        *section_tree; /* no jokes here */
73         rbtree_t        *name2_tree; /* for sections of the same name2 */
74         rbtree_t        *data_tree;
75         void            *base;
76         int depth;
77         const CONF_PARSER *variables;
78 };
79
80
81 /*
82  *      Internal data that is associated with a configuration section,
83  *      so that we don't have to track it separately.
84  */
85 struct conf_data {
86         CONF_ITEM  item;
87         const char *name;
88         int        flag;
89         void       *data;       /* user data */
90         void       (*free)(void *); /* free user data function */
91 };
92
93
94 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
95                                 void *data, void (*data_free)(void *),
96                                 int flag);
97 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
98                                    int flag);
99
100 /*
101  *      Isolate the scary casts in these tiny provably-safe functions
102  */
103 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
104 {
105         if (ci == NULL)
106                 return NULL;
107         rad_assert(ci->type == CONF_ITEM_PAIR);
108         return (CONF_PAIR *)ci;
109 }
110 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
111 {
112         if (ci == NULL)
113                 return NULL;
114         rad_assert(ci->type == CONF_ITEM_SECTION);
115         return (CONF_SECTION *)ci;
116 }
117 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
118 {
119         if (cp == NULL)
120                 return NULL;
121         return (CONF_ITEM *)cp;
122 }
123 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
124 {
125         if (cs == NULL)
126                 return NULL;
127         return (CONF_ITEM *)cs;
128 }
129
130 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
131 {
132         if (ci == NULL)
133                 return NULL;
134         rad_assert(ci->type == CONF_ITEM_DATA);
135         return (CONF_DATA *)ci;
136 }
137 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
138 {
139         if (cd == NULL)
140                 return NULL;
141         return (CONF_ITEM *)cd;
142 }
143
144 /*
145  *      Create a new CONF_PAIR
146  */
147 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
148                                 LRAD_TOKEN operator, CONF_SECTION *parent)
149 {
150         CONF_PAIR *cp;
151
152         cp = rad_malloc(sizeof(*cp));
153         memset(cp, 0, sizeof(*cp));
154         cp->item.type = CONF_ITEM_PAIR;
155         cp->item.parent = parent;
156         cp->attr = strdup(attr);
157         cp->value = strdup(value);
158         cp->operator = operator;
159
160         return cp;
161 }
162
163 /*
164  *      Free a CONF_PAIR
165  */
166 void cf_pair_free(CONF_PAIR **cp)
167 {
168         if (!cp || !*cp) return;
169
170         if ((*cp)->attr)
171                 free((*cp)->attr);
172         if ((*cp)->value)
173                 free((*cp)->value);
174
175 #ifndef NDEBUG
176         memset(*cp, 0, sizeof(*cp));
177 #endif
178         free(*cp);
179
180         *cp = NULL;
181 }
182
183
184 static void cf_data_free(CONF_DATA **cd)
185 {
186         if (!cd || !*cd) return;
187
188         if ((*cd)->flag != 0) free((*cd)->name);
189         if (!(*cd)->free) {
190                 free((*cd)->data);
191         } else {
192                 ((*cd)->free)((*cd)->data);
193         }
194 #ifndef NDEBUG
195         memset(*cd, 0, sizeof(*cd));
196 #endif
197         free(*cd);
198         *cd = NULL;
199 }
200
201 /*
202  *      rbtree callback function
203  */
204 static int pair_cmp(const void *a, const void *b)
205 {
206         const CONF_PAIR *one = a;
207         const CONF_PAIR *two = b;
208
209         return strcmp(one->attr, two->attr);
210 }
211
212
213 /*
214  *      rbtree callback function
215  */
216 static int section_cmp(const void *a, const void *b)
217 {
218         const CONF_SECTION *one = a;
219         const CONF_SECTION *two = b;
220
221         return strcmp(one->name1, two->name1);
222 }
223
224
225 /*
226  *      rbtree callback function
227  */
228 static int name2_cmp(const void *a, const void *b)
229 {
230         const CONF_SECTION *one = a;
231         const CONF_SECTION *two = b;
232
233         rad_assert(strcmp(one->name1, two->name1) == 0);
234
235         if (!one->name2 && !two->name2) return 0;
236         if (!one->name2) return -1;
237         if (!two->name2) return +1;
238
239         return strcmp(one->name2, two->name2);
240 }
241
242
243 /*
244  *      rbtree callback function
245  */
246 static int data_cmp(const void *a, const void *b)
247 {
248         int rcode;
249
250         const CONF_DATA *one = a;
251         const CONF_DATA *two = b;
252
253         rcode = one->flag - two->flag;
254         if (rcode != 0) return rcode;
255
256         return strcmp(one->name, two->name);
257 }
258
259
260 /*
261  *      Free strings we've parsed into data structures.
262  */
263 static void cf_section_parse_free(void *base, const CONF_PARSER *variables)
264 {
265         int i;
266
267         /*
268          *      Don't automatically free the strings if we're being
269          *      called from a module.  This is also for clients.c,
270          *      where client_free() expects to be able to free the
271          *      client structure.  If we moved everything to key off
272          *      of the config files, we might solve some problems...
273          */
274         if (!variables) return;
275
276         /*
277          *      Free up dynamically allocated string pointers.
278          */
279         for (i = 0; variables[i].name != NULL; i++) {
280                 char **p;
281
282                 if ((variables[i].type != PW_TYPE_STRING_PTR) &&
283                     (variables[i].type != PW_TYPE_FILENAME)) {
284                         continue;
285                 }
286
287                 /*
288                  *      No base struct offset, data must be the pointer.
289                  *      If data doesn't exist, ignore the entry, there
290                  *      must be something wrong.
291                  */
292                 if (!base) {
293                         if (!variables[i].data) {
294                                 continue;
295                         }
296
297                         p = (char **) variables[i].data;;
298
299                 } else if (variables[i].data) {
300                         p = (char **) variables[i].data;;
301
302                 } else {
303                         p = (char **) (((char *)base) + variables[i].offset);
304                 }
305
306                 free(*p);
307                 *p = NULL;
308         }
309 }
310
311
312 /*
313  *      Free a CONF_SECTION
314  */
315 void cf_section_free(CONF_SECTION **cs)
316 {
317         CONF_ITEM       *ci, *next;
318
319         if (!cs || !*cs) return;
320
321         if ((*cs)->variables) {
322                 cf_section_parse_free((*cs)->base, (*cs)->variables);
323         }
324
325         for (ci = (*cs)->children; ci; ci = next) {
326                 next = ci->next;
327
328                 switch (ci->type) {
329                 case CONF_ITEM_PAIR: {
330                                 CONF_PAIR *pair = cf_itemtopair(ci);
331                                 cf_pair_free(&pair);
332                         }
333                         break;
334
335                 case CONF_ITEM_SECTION: {
336
337                                 CONF_SECTION *section = cf_itemtosection(ci);
338                                 cf_section_free(&section);
339                         }
340                         break;
341
342                 case CONF_ITEM_DATA: {
343                                 CONF_DATA *data = cf_itemtodata(ci);
344                                 cf_data_free(&data);
345                         }
346                         break;
347
348                 default:        /* should really be an error. */
349                         break;
350                 }
351         }
352
353         if ((*cs)->name1)
354                 free((*cs)->name1);
355         if ((*cs)->name2)
356                 free((*cs)->name2);
357         if ((*cs)->pair_tree)
358                 rbtree_free((*cs)->pair_tree);
359         if ((*cs)->section_tree)
360                 rbtree_free((*cs)->section_tree);
361         if ((*cs)->name2_tree)
362                 rbtree_free((*cs)->name2_tree);
363         if ((*cs)->data_tree)
364                 rbtree_free((*cs)->data_tree);
365
366         /*
367          * And free the section
368          */
369 #ifndef NDEBUG
370         memset(*cs, 0, sizeof(*cs));
371 #endif
372         free(*cs);
373
374         *cs = NULL;
375 }
376
377
378 /*
379  *      Allocate a CONF_SECTION
380  */
381 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
382                                       CONF_SECTION *parent)
383 {
384         CONF_SECTION    *cs;
385
386         if (!name1) return NULL;
387
388         cs = rad_malloc(sizeof(*cs));
389         memset(cs, 0, sizeof(*cs));
390         cs->item.type = CONF_ITEM_SECTION;
391         cs->item.parent = parent;
392         cs->name1 = strdup(name1);
393         if (!cs->name1) {
394                 cf_section_free(&cs);
395                 return NULL;
396         }
397
398         if (name2 && *name2) {
399                 cs->name2 = strdup(name2);
400                 if (!cs->name2) {
401                         cf_section_free(&cs);
402                         return NULL;
403                 }
404         }
405         cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
406         if (!cs->pair_tree) {
407                 cf_section_free(&cs);
408                 return NULL;
409         }
410
411         /*
412          *      Don't create a data tree, it may not be needed.
413          */
414
415         /*
416          *      Don't create the section tree here, it may not
417          *      be needed.
418          */
419
420         if (parent) cs->depth = parent->depth + 1;
421
422         return cs;
423 }
424
425
426 /*
427  *      Add an item to a configuration section.
428  */
429 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
430 {
431         if (!cs->children) {
432                 rad_assert(cs->tail == NULL);
433                 cs->children = ci;
434         } else {
435                 rad_assert(cs->tail != NULL);
436                 cs->tail->next = ci;
437         }
438
439         /*
440          *      Update the trees (and tail) for each item added.
441          */
442         for (/* nothing */; ci != NULL; ci = ci->next) {
443                 cs->tail = ci;
444
445                 /*
446                  *      For fast lookups, pair's and sections get
447                  *      added to rbtree's.
448                  */
449                 switch (ci->type) {
450                         case CONF_ITEM_PAIR:
451                                 rbtree_insert(cs->pair_tree, ci);
452                                 break;
453
454                         case CONF_ITEM_SECTION: {
455                                 const CONF_SECTION *cs_new = cf_itemtosection(ci);
456
457                                 if (!cs->section_tree) {
458                                         cs->section_tree = rbtree_create(section_cmp, NULL, 0);
459                                         /* ignore any errors */
460                                 }
461
462                                 if (cs->section_tree) {
463                                         rbtree_insert(cs->section_tree, cs_new);                                }
464
465                                 /*
466                                  *      Two names: find the named instance.
467                                  */
468                                 if (cs_new->name2) {
469                                         CONF_SECTION *old_cs;
470
471                                         /*
472                                          *      Find the FIRST
473                                          *      CONF_SECTION having
474                                          *      the given name1, and
475                                          *      create a new tree
476                                          *      under it.
477                                          */
478                                         old_cs = rbtree_finddata(cs->section_tree, cs_new);
479                                         if (!old_cs) return; /* this is a bad error! */
480
481                                         if (!old_cs->name2_tree) {
482                                                 old_cs->name2_tree = rbtree_create(name2_cmp,
483                                                                                    NULL, 0);
484                                         }
485                                         if (old_cs->name2_tree) {
486                                                 rbtree_insert(old_cs->name2_tree, cs_new);
487                                         }
488                                 } /* had a name2 */
489                                 break;
490                         } /* was a section */
491
492                         case CONF_ITEM_DATA:
493                                 if (!cs->data_tree) {
494                                         cs->data_tree = rbtree_create(data_cmp, NULL, 0);
495                                 }
496                                 if (cs->data_tree) {
497                                         rbtree_insert(cs->data_tree, ci);
498                                 }
499                                 break;
500
501                         default: /* FIXME: assert & error! */
502                                 break;
503
504                 } /* switch over conf types */
505         } /* loop over ci */
506 }
507
508 /*
509  *      Expand the variables in an input string.
510  */
511 static const char *cf_expand_variables(const char *cf, int *lineno,
512                                        const CONF_SECTION *outercs,
513                                        char *output, const char *input)
514 {
515         char *p;
516         const char *end, *ptr;
517         char name[8192];
518         const CONF_SECTION *parentcs;
519
520         /*
521          *      Find the master parent conf section.
522          *      We can't use mainconfig.config, because we're in the
523          *      process of re-building it, and it isn't set up yet...
524          */
525         for (parentcs = outercs;
526              parentcs->item.parent != NULL;
527              parentcs = parentcs->item.parent) {
528                 /* do nothing */
529         }
530
531         p = output;
532         ptr = input;
533         while (*ptr) {
534                 /*
535                  *      Ignore anything other than "${"
536                  */
537                 if ((*ptr == '$') && (ptr[1] == '{')) {
538                         int up;
539                         CONF_PAIR *cp;
540                         const CONF_SECTION *cs;
541
542                         /*
543                          *      FIXME: Add support for ${foo:-bar},
544                          *      like in xlat.c
545                          */
546
547                         /*
548                          *      Look for trailing '}', and log a
549                          *      warning for anything that doesn't match,
550                          *      and exit with a fatal error.
551                          */
552                         end = strchr(ptr, '}');
553                         if (end == NULL) {
554                                 *p = '\0';
555                                 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
556                                        cf, *lineno);
557                                 return NULL;
558                         }
559
560                         ptr += 2;
561
562                         cp = NULL;
563                         up = 0;
564
565                         /*
566                          *      ${.foo} means "foo from the current section"
567                          */
568                         if (*ptr == '.') {
569                                 up = 1;
570                                 cs = outercs;
571                                 ptr++;
572
573                                 /*
574                                  *      ${..foo} means "foo from the section
575                                  *      enclosing this section" (etc.)
576                                  */
577                                 while (*ptr == '.') {
578                                         if (cs->item.parent)
579                                                 cs = cs->item.parent;
580                                         ptr++;
581                                 }
582
583                         } else {
584                                 const char *q;
585                                 /*
586                                  *      ${foo} is local, with
587                                  *      main as lower priority
588                                  */
589                                 cs = outercs;
590
591                                 /*
592                                  *      ${foo.bar.baz} is always rooted
593                                  *      from the top.
594                                  */
595                                 for (q = ptr; *q && q != end; q++) {
596                                         if (*q == '.') {
597                                                 cs = parentcs;
598                                                 up = 1;
599                                                 break;
600                                         }
601                                 }
602                         }
603
604                         while (cp == NULL) {
605                                 char *q;
606                                 /*
607                                  *      Find the next section.
608                                  */
609                                 for (q = name;
610                                      (*ptr != 0) && (*ptr != '.') &&
611                                              (ptr != end);
612                                      q++, ptr++) {
613                                         *q = *ptr;
614                                 }
615                                 *q = '\0';
616
617                                 /*
618                                  *      The character is a '.', find a
619                                  *      section (as the user has given
620                                  *      us a subsection to find)
621                                  */
622                                 if (*ptr == '.') {
623                                         CONF_SECTION *next;
624
625                                         ptr++;  /* skip the period */
626
627                                         /*
628                                          *      Find the sub-section.
629                                          */
630                                         next = cf_section_sub_find(cs, name);
631                                         if (next == NULL) {
632                                                 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
633                                                 return NULL;
634                                         }
635                                         cs = next;
636
637                                 } else { /* no period, must be a conf-part */
638                                         /*
639                                          *      Find in the current referenced
640                                          *      section.
641                                          */
642                                         cp = cf_pair_find(cs, name);
643                                         if (cp == NULL) {
644                                                 /*
645                                                  *      It it was NOT ${..foo}
646                                                  *      then look in the
647                                                  *      top-level config items.
648                                                  */
649                                                 if (!up) cp = cf_pair_find(parentcs, name);
650                                         }
651                                         if (cp == NULL) {
652                                                 radlog(L_ERR, "config: No such configuration item %s in section %s when expanding string \"%s\"", name,
653                                                        cf_section_name1(cs),
654                                                        input);
655                                                 return NULL;
656                                         }
657                                 }
658                         } /* until cp is non-NULL */
659
660                         /*
661                          *  Substitute the value of the variable.
662                          */
663                         strcpy(p, cp->value);
664                         p += strlen(p);
665                         ptr = end + 1;
666
667                 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
668                         char *env;
669
670                         ptr += 5;
671
672                         /*
673                          *      Look for trailing '}', and log a
674                          *      warning for anything that doesn't match,
675                          *      and exit with a fatal error.
676                          */
677                         end = strchr(ptr, '}');
678                         if (end == NULL) {
679                                 *p = '\0';
680                                 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
681                                        cf, *lineno);
682                                 return NULL;
683                         }
684
685                         memcpy(name, ptr, end - ptr);
686                         name[end - ptr] = '\0';
687
688                         /*
689                          *      Get the environment variable.
690                          *      If none exists, then make it an empty string.
691                          */
692                         env = getenv(name);
693                         if (env == NULL) {
694                                 *name = '\0';
695                                 env = name;
696                         }
697
698                         strcpy(p, env);
699                         p += strlen(p);
700                         ptr = end + 1;
701
702                 } else {
703                         /*
704                          *      Copy it over verbatim.
705                          */
706                         *(p++) = *(ptr++);
707                 }
708         } /* loop over all of the input string. */
709
710         *p = '\0';
711
712         return output;
713 }
714
715
716 /*
717  *      Parses an item (not a CONF_ITEM) into the specified format,
718  *      with a default value.
719  *
720  *      Returns -1 on error, 0 for correctly parsed, and 1 if the
721  *      default value was used.  Note that the default value will be
722  *      used ONLY if the CONF_PAIR is NULL.
723  */
724 int cf_item_parse(CONF_SECTION *cs, const char *name,
725                   int type, void *data, const char *dflt)
726 {
727         int rcode = 0;
728         char **q;
729         const char *value;
730         lrad_ipaddr_t ipaddr;
731         const CONF_PAIR *cp;
732         char ipbuf[128];
733
734         cp = cf_pair_find(cs, name);
735         if (cp) {
736                 value = cp->value;
737
738         } else if (!dflt) {
739                 return 1;       /* nothing to parse, return default value */
740
741         } else {
742                 rcode = 1;
743                 value = dflt;
744         }
745
746         switch (type) {
747         case PW_TYPE_BOOLEAN:
748                 /*
749                  *      Allow yes/no and on/off
750                  */
751                 if ((strcasecmp(value, "yes") == 0) ||
752                     (strcasecmp(value, "on") == 0)) {
753                         *(int *)data = 1;
754                 } else if ((strcasecmp(value, "no") == 0) ||
755                            (strcasecmp(value, "off") == 0)) {
756                         *(int *)data = 0;
757                 } else {
758                         *(int *)data = 0;
759                         radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, name);
760                         return -1;
761                 }
762                 DEBUG2("\t%s = %s", name, value);
763                 break;
764
765         case PW_TYPE_INTEGER:
766                 *(int *)data = strtol(value, 0, 0);
767                 DEBUG2("\t%s = %d", name, *(int *)data);
768                 break;
769
770         case PW_TYPE_STRING_PTR:
771                 q = (char **) data;
772                 if (*q != NULL) {
773                         free(*q);
774                 }
775
776                 /*
777                  *      Expand variables which haven't already been
778                  *      expanded automagically when the configuration
779                  *      file was read.
780                  */
781                 if (value == dflt) {
782                         char buffer[8192];
783
784                         int lineno = cs->item.lineno;
785
786                         /*
787                          *      FIXME: sizeof(buffer)?
788                          */
789                         value = cf_expand_variables("?",
790                                                     &lineno,
791                                                     cs, buffer, value);
792                         if (!value) return -1;
793                 }
794
795                 DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
796                 *q = value ? strdup(value) : NULL;
797                 break;
798
799                 /*
800                  *      This is the same as PW_TYPE_STRING_PTR,
801                  *      except that we also "stat" the file, and
802                  *      cache the result.
803                  */
804         case PW_TYPE_FILENAME:
805                 q = (char **) data;
806                 if (*q != NULL) {
807                         free(*q);
808                 }
809
810                 /*
811                  *      Expand variables which haven't already been
812                  *      expanded automagically when the configuration
813                  *      file was read.
814                  */
815                 if (value == dflt) {
816                         char buffer[8192];
817
818                         int lineno = cs->item.lineno;
819
820                         /*
821                          *      FIXME: sizeof(buffer)?
822                          */
823                         value = cf_expand_variables("?",
824                                                     &lineno,
825                                                     cs, buffer, value);
826                         if (!value) return -1;
827                 }
828
829                 DEBUG2("\t%s = \"%s\"", name, value ? value : "(null)");
830                 *q = value ? strdup(value) : NULL;
831
832                 /*
833                  *      And now we "stat" the file.
834                  */
835                 if (*q) {
836                         struct stat buf;
837
838                         if (stat(*q, &buf) == 0) {
839                                 time_t *mtime;
840
841                                 mtime = rad_malloc(sizeof(*mtime));
842                                 *mtime = buf.st_mtime;
843                                 /* FIXME: error? */
844                                 cf_data_add_internal(cs, *q, mtime, free,
845                                                      PW_TYPE_FILENAME);
846                         }
847                 }
848                 break;
849
850         case PW_TYPE_IPADDR:
851                 /*
852                  *      Allow '*' as any address
853                  */
854                 if (strcmp(value, "*") == 0) {
855                         *(uint32_t *) data = htonl(INADDR_ANY);
856                         DEBUG2("\t%s = *", name);
857                         break;
858                 }
859                 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
860                         radlog(L_ERR, "Can't find IP address for host %s", value);
861                         return -1;
862                 }
863                 DEBUG2("\t%s = %s IP address [%s]", name, value,
864                                ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
865                 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
866                 break;
867
868         case PW_TYPE_IPV6ADDR:
869                 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
870                         radlog(L_ERR, "Can't find IPv6 address for host %s", value);
871                         return -1;
872                 }
873                 DEBUG2("\t%s = %s IPv6 address [%s]", name, value,
874                                ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
875                 memcpy(data, &ipaddr.ipaddr.ip6addr,
876                        sizeof(ipaddr.ipaddr.ip6addr));
877                 break;
878
879         default:
880                 radlog(L_ERR, "type %d not supported yet", type);
881                 return -1;
882                 break;
883         } /* switch over variable type */
884
885         return rcode;
886 }
887
888 static const char *parse_spaces = "                                                                                                                                                                                                                                                                ";
889
890 /*
891  *      Parse a configuration section into user-supplied variables.
892  */
893 int cf_section_parse(CONF_SECTION *cs, void *base,
894                      const CONF_PARSER *variables)
895 {
896         int i;
897         void *data;
898
899         if (!cs->name2) {
900                 DEBUG2("%.*s%s {", cs->depth, parse_spaces,
901                        cs->name1);
902         } else {
903                 DEBUG2("%.*s%s %s {", cs->depth, parse_spaces,
904                        cs->name1, cs->name2);
905         }
906
907         /*
908          *      Handle the known configuration parameters.
909          */
910         for (i = 0; variables[i].name != NULL; i++) {
911                 /*
912                  *      Handle subsections specially
913                  */
914                 if (variables[i].type == PW_TYPE_SUBSECTION) {
915                         const CONF_SECTION *subcs;
916                         subcs = cf_section_sub_find(cs, variables[i].name);
917
918                         /*
919                          *      If the configuration section is NOT there,
920                          *      then ignore it.
921                          *
922                          *      FIXME! This is probably wrong... we should
923                          *      probably set the items to their default values.
924                          */
925                         if (!subcs) continue;
926
927                         if (!variables[i].dflt) {
928                                 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
929                                 goto error;
930                         }
931
932                         if (cf_section_parse(subcs, base,
933                                              (const CONF_PARSER *) variables[i].dflt) < 0) {
934                                 goto error;
935                         }
936                         continue;
937                 } /* else it's a CONF_PAIR */
938
939                 if (variables[i].data) {
940                         data = variables[i].data; /* prefer this. */
941                 } else if (base) {
942                         data = ((char *)base) + variables[i].offset;
943                 } else {
944                         DEBUG2("Internal sanity check 2 failed in cf_section_parse");
945                         goto error;
946                 }
947
948                 /*
949                  *      Parse the pair we found, or a default value.
950                  */
951                 if (cf_item_parse(cs, variables[i].name, variables[i].type,
952                                   data, variables[i].dflt) < 0) {
953                         goto error;
954                 }
955         } /* for all variables in the configuration section */
956
957         DEBUG2("%.*s}", cs->depth, parse_spaces);
958
959         cs->base = base;
960         cs->variables = variables;
961
962         return 0;
963
964  error:
965         DEBUG2("%.*s}", cs->depth, parse_spaces);
966         cf_section_parse_free(base, variables);
967         return -1;
968 }
969
970
971 /*
972  *      Read a part of the config file.
973  */
974 static int cf_section_read(const char *file, int *lineno, FILE *fp,
975                            CONF_SECTION *current)
976
977 {
978         CONF_SECTION *this, *css;
979         CONF_PAIR *cpn;
980         char *ptr;
981         const char *value;
982         char buf[8192];
983         char buf1[8192];
984         char buf2[8192];
985         char buf3[8192];
986         int t1, t2, t3;
987         char *cbuf = buf;
988         int len;
989
990         this = current;         /* add items here */
991
992         /*
993          *      Read, checking for line continuations ('\\' at EOL)
994          */
995         for (;;) {
996                 int eof;
997
998                 /*
999                  *      Get data, and remember if we are at EOF.
1000                  */
1001                 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
1002                 (*lineno)++;
1003
1004                 len = strlen(cbuf);
1005
1006                 /*
1007                  *      We've filled the buffer, and there isn't
1008                  *      a CR in it.  Die!
1009                  */
1010                 if ((cbuf[len - 1] != '\n') && !feof(fp)) {
1011                         radlog(L_ERR, "%s[%d]: Line too long",
1012                                file, *lineno);
1013                         return -1;
1014                 }
1015
1016                 /*
1017                  *  Check for continuations.
1018                  */
1019                 if (cbuf[len - 1] == '\n') len--;
1020
1021                 /*
1022                  *      Last character is '\\'.  Over-write it,
1023                  *      and read another line.
1024                  */
1025                 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1026                         if (len >= (sizeof(buf) - 5)) {
1027                                 radlog(L_ERR, "%s[%d]: Line too long",
1028                                        file, *lineno);
1029                                 return -1;
1030                         }
1031
1032                         cbuf[len - 1] = '\0';
1033                         cbuf += len - 1;
1034                         continue;
1035                 }
1036
1037                 /*
1038                  *  We're at EOF, and haven't read anything.  Stop.
1039                  */
1040                 if (eof && (cbuf == buf)) {
1041                         break;
1042                 }
1043
1044                 ptr = cbuf = buf;
1045                 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1046
1047                 if ((*buf1 == '#') || (*buf1 == '\0')) {
1048                         continue;
1049                 }
1050
1051                 /*
1052                  *      The caller eats "name1 name2 {", and calls us
1053                  *      for the data inside of the section.  So if we
1054                  *      receive a closing brace, then it must mean the
1055                  *      end of the section.
1056                  */
1057                if (t1 == T_RCBRACE) {
1058                        if (this == current) {
1059                                radlog(L_ERR, "%s[%d]: Too many closing braces",
1060                                       file, *lineno);
1061                                return -1;
1062
1063                        }
1064                        this = this->item.parent;
1065                        continue;
1066                 }
1067
1068                 /*
1069                  *      Allow for $INCLUDE files
1070                  *
1071                  *      This *SHOULD* work for any level include.
1072                  *      I really really really hate this file.  -cparker
1073                  */
1074                 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1075                         t2 = getword(&ptr, buf2, sizeof(buf2));
1076
1077                         value = cf_expand_variables(file, lineno, this, buf, buf2);
1078                         if (!value) return -1;
1079
1080 #ifdef HAVE_DIRENT_H
1081                         /*
1082                          *      $INCLUDE foo/
1083                          *
1084                          *      Include ALL non-"dot" files in the directory.
1085                          *      careful!
1086                          */
1087                         if (value[strlen(value) - 1] == '/') {
1088                                 DIR             *dir;
1089                                 struct dirent   *dp;
1090                                 struct stat stat_buf;
1091
1092                                 DEBUG2( "Config:   including files in directory: %s", value );
1093                                 dir = opendir(value);
1094                                 if (!dir) {
1095                                         radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1096                                                file, *lineno, value,
1097                                                strerror(errno));
1098                                         return -1;
1099                                 }
1100
1101                                 /*
1102                                  *      Read the directory, ignoring "." files.
1103                                  */
1104                                 while ((dp = readdir(dir)) != NULL) {
1105                                         const char *p;
1106
1107                                         if (dp->d_name[0] == '.') continue;
1108
1109                                         /*
1110                                          *      Check for valid characters
1111                                          */
1112                                         for (p = dp->d_name; *p != '\0'; p++) {
1113                                                 if (isalpha((int)*p) ||
1114                                                     isdigit((int)*p) ||
1115                                                     (*p == '_') ||
1116                                                     (*p == '.')) continue;
1117                                                 break;
1118                                         }
1119                                         if (*p != '\0') continue;
1120
1121                                         snprintf(buf2, sizeof(buf2), "%s%s",
1122                                                  value, dp->d_name);
1123                                         if ((stat(buf2, &stat_buf) != 0) ||
1124                                             S_ISDIR(stat_buf.st_mode)) continue;
1125                                         /*
1126                                          *      Read the file into the current
1127                                          *      configuration sectoin.
1128                                          */
1129                                         if (cf_file_include(buf2, this) < 0) {
1130                                                 closedir(dir);
1131                                                 return -1;
1132                                         }
1133                                 }
1134                                 closedir(dir);
1135                         }  else
1136 #endif
1137                         { /* it was a normal file */
1138                                 if (cf_file_include(value, this) < 0) {
1139                                         return -1;
1140                                 }
1141                         }
1142                         continue;
1143                 } /* we were in an include */
1144
1145                 /*
1146                  *      Ensure that the user can't add CONF_PAIRs
1147                  *      with 'internal' names;
1148                  */
1149                 if (buf1[0] == '_') {
1150                         radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1151                                         file, *lineno, buf1);
1152                         return -1;
1153                 }
1154
1155                 /*
1156                  *      Grab the next token.
1157                  */
1158                 t2 = gettoken(&ptr, buf2, sizeof(buf2));
1159                 switch (t2) {
1160                 case T_EOL:
1161                 case T_HASH:
1162                 case T_OP_EQ:
1163                 case T_OP_SET:
1164                         t3 = getword(&ptr, buf3, sizeof(buf3));
1165                         t2 = T_OP_EQ;
1166
1167                         /*
1168                          *      Handle variable substitution via ${foo}
1169                          */
1170                         value = cf_expand_variables(file, lineno, this,
1171                                                     buf, buf3);
1172                         if (!value) return -1;
1173                         
1174                         
1175                         /*
1176                          *      Add this CONF_PAIR to our CONF_SECTION
1177                          */
1178                         cpn = cf_pair_alloc(buf1, value, t2, this);
1179                         cpn->item.lineno = *lineno;
1180                         cf_item_add(this, cf_pairtoitem(cpn));
1181                         continue;
1182
1183                         /*
1184                          *      No '=', must be a section or sub-section.
1185                          */
1186                 case T_BARE_WORD:
1187                 case T_DOUBLE_QUOTED_STRING:
1188                 case T_SINGLE_QUOTED_STRING:
1189                         t3 = gettoken(&ptr, buf3, sizeof(buf3));
1190                         if (t3 != T_LCBRACE) {
1191                                 radlog(L_ERR, "%s[%d]: Expecting section start brace '{' after \"%s %s\"",
1192                                        file, *lineno, buf1, buf2);
1193                                 return -1;
1194                         }
1195
1196                 case T_LCBRACE:
1197                         css = cf_section_alloc(buf1,
1198                                                t2 == T_LCBRACE ? NULL : buf2,
1199                                                this);
1200                         if (!css) {
1201                                 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1202                                                 file, *lineno);
1203                                 return -1;
1204                         }
1205                         cf_item_add(this, cf_sectiontoitem(css));
1206                         css->item.lineno = *lineno;
1207
1208                         /*
1209                          *      The current section is now the child section.
1210                          */
1211                         this = css;
1212                         continue;
1213
1214                 default:
1215                         radlog(L_ERR, "%s[%d]: Parse error after \"%s\"",
1216                                file, *lineno, buf1);
1217                         return -1;
1218                 }
1219         }
1220
1221         /*
1222          *      See if EOF was unexpected ..
1223          */
1224         if (feof(fp) && (this != current)) {
1225                 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1226                        file, *lineno,
1227                        cf_section_name1(this), cf_section_lineno(this));
1228                 return -1;
1229         }
1230
1231         return 0;
1232 }
1233
1234 /*
1235  *      Include one config file in another.
1236  */
1237 int cf_file_include(const char *file, CONF_SECTION *cs)
1238 {
1239         FILE            *fp;
1240         int             lineno = 0;
1241         struct stat     statbuf;
1242         time_t          *mtime;
1243
1244         DEBUG2( "Config:   including file: %s", file);
1245
1246         if (stat(file, &statbuf) == 0) {
1247 #ifdef S_IWOTH
1248                 if ((statbuf.st_mode & S_IWOTH) != 0) {
1249                         radlog(L_ERR|L_CONS, "Configuration file %s is globally writable.  Refusing to start due to insecure configuration.",
1250                                file);
1251                         return -1;
1252                 }
1253 #endif
1254
1255 #ifdef S_IROTH
1256                 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1257                         radlog(L_ERR|L_CONS, "Configuration file %s is globally readable.  Refusing to start due to insecure configuration.",
1258                                file);
1259                         return -1;
1260                 }
1261 #endif
1262         }
1263
1264         fp = fopen(file, "r");
1265         if (!fp) {
1266                 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1267                        file, strerror(errno));
1268                 return -1;
1269         }
1270
1271         /*
1272          *      Read the section.  It's OK to have EOF without a
1273          *      matching close brace.
1274          */
1275         if (cf_section_read(file, &lineno, fp, cs) < 0) {
1276                 fclose(fp);
1277                 return -1;
1278         }
1279
1280         /*
1281          *      Add the filename to the section
1282          */
1283         mtime = rad_malloc(sizeof(*mtime));
1284         *mtime = statbuf.st_mtime;
1285         /* FIXME: error? */
1286         cf_data_add_internal(cs, file, mtime, free,
1287                              PW_TYPE_FILENAME);
1288
1289         fclose(fp);
1290         return 0;
1291 }
1292
1293 /*
1294  *      Bootstrap a config file.
1295  */
1296 CONF_SECTION *cf_file_read(const char *file)
1297 {
1298         CONF_SECTION *cs;
1299
1300         cs = cf_section_alloc("main", NULL, NULL);
1301         if (!cs) return NULL;
1302
1303         if (cf_file_include(file, cs) < 0) {
1304                 cf_section_free(&cs);
1305                 return NULL;
1306         }
1307
1308         return cs;
1309 }
1310
1311 /*
1312  * Return a CONF_PAIR within a CONF_SECTION.
1313  */
1314 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1315 {
1316         CONF_ITEM       *ci;
1317         CONF_PAIR       *cp = NULL;
1318
1319         if (!cs) cs = mainconfig.config;
1320
1321         /*
1322          *      Find the name in the tree, for speed.
1323          */
1324         if (name) {
1325                 CONF_PAIR mycp;
1326
1327                 mycp.attr = name;
1328                 cp = rbtree_finddata(cs->pair_tree, &mycp);
1329         } else {
1330                 /*
1331                  *      Else find the first one that matches
1332                  */
1333                 for (ci = cs->children; ci; ci = ci->next) {
1334                         if (ci->type == CONF_ITEM_PAIR) {
1335                                 return cf_itemtopair(ci);
1336                         }
1337                 }
1338         }
1339
1340         if (cp || !cs->template) return cp;
1341
1342         return cf_pair_find(cs->template, name);
1343 }
1344
1345 /*
1346  * Return the attr of a CONF_PAIR
1347  */
1348
1349 char *cf_pair_attr(CONF_PAIR *pair)
1350 {
1351         return (pair ? pair->attr : NULL);
1352 }
1353
1354 /*
1355  * Return the value of a CONF_PAIR
1356  */
1357
1358 char *cf_pair_value(CONF_PAIR *pair)
1359 {
1360         return (pair ? pair->value : NULL);
1361 }
1362
1363 /*
1364  * Return the first label of a CONF_SECTION
1365  */
1366
1367 const char *cf_section_name1(const CONF_SECTION *cs)
1368 {
1369         return (cs ? cs->name1 : NULL);
1370 }
1371
1372 /*
1373  * Return the second label of a CONF_SECTION
1374  */
1375
1376 const char *cf_section_name2(const CONF_SECTION *cs)
1377 {
1378         return (cs ? cs->name2 : NULL);
1379 }
1380
1381 /*
1382  * Find a value in a CONF_SECTION
1383  */
1384 char *cf_section_value_find(const CONF_SECTION *cs, const char *attr)
1385 {
1386         CONF_PAIR       *cp;
1387
1388         cp = cf_pair_find(cs, attr);
1389
1390         return (cp ? cp->value : NULL);
1391 }
1392
1393 /*
1394  * Return the next pair after a CONF_PAIR
1395  * with a certain name (char *attr) If the requested
1396  * attr is NULL, any attr matches.
1397  */
1398
1399 CONF_PAIR *cf_pair_find_next(const CONF_SECTION *cs,
1400                              const CONF_PAIR *pair, const char *attr)
1401 {
1402         CONF_ITEM       *ci;
1403
1404         /*
1405          * If pair is NULL this must be a first time run
1406          * Find the pair with correct name
1407          */
1408
1409         if (pair == NULL){
1410                 return cf_pair_find(cs, attr);
1411         }
1412
1413         ci = cf_pairtoitem(pair)->next;
1414
1415         for (; ci; ci = ci->next) {
1416                 if (ci->type != CONF_ITEM_PAIR)
1417                         continue;
1418                 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1419                         break;
1420         }
1421
1422         return cf_itemtopair(ci);
1423 }
1424
1425 /*
1426  * Find a CONF_SECTION, or return the root if name is NULL
1427  */
1428
1429 CONF_SECTION *cf_section_find(const char *name)
1430 {
1431         if (name)
1432                 return cf_section_sub_find(mainconfig.config, name);
1433         else
1434                 return mainconfig.config;
1435 }
1436
1437 /*
1438  * Find a sub-section in a section
1439  */
1440
1441 CONF_SECTION *cf_section_sub_find(const CONF_SECTION *cs, const char *name)
1442 {
1443         CONF_ITEM *ci;
1444
1445         /*
1446          *      Do the fast lookup if possible.
1447          */
1448         if (name && cs->section_tree) {
1449                 CONF_SECTION mycs;
1450
1451                 mycs.name1 = name;
1452                 mycs.name2 = NULL;
1453                 return rbtree_finddata(cs->section_tree, &mycs);
1454         }
1455
1456         for (ci = cs->children; ci; ci = ci->next) {
1457                 if (ci->type != CONF_ITEM_SECTION)
1458                         continue;
1459                 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1460                         break;
1461         }
1462
1463         return cf_itemtosection(ci);
1464
1465 }
1466
1467
1468 /*
1469  *      Find a CONF_SECTION with both names.
1470  */
1471 CONF_SECTION *cf_section_sub_find_name2(const CONF_SECTION *cs,
1472                                         const char *name1, const char *name2)
1473 {
1474         CONF_ITEM    *ci;
1475
1476         if (!name2) return cf_section_sub_find(cs, name1);
1477
1478         if (!cs) cs = mainconfig.config;
1479
1480         if (name1 && (cs->section_tree)) {
1481                 CONF_SECTION mycs, *master_cs;
1482
1483                 mycs.name1 = name1;
1484                 mycs.name2 = name2;
1485
1486                 master_cs = rbtree_finddata(cs->section_tree, &mycs);
1487                 if (master_cs) {
1488                         return rbtree_finddata(master_cs->name2_tree, &mycs);
1489                 }
1490         }
1491
1492         /*
1493          *      Else do it the old-fashioned way.
1494          */
1495         for (ci = cs->children; ci; ci = ci->next) {
1496                 CONF_SECTION *subcs;
1497
1498                 if (ci->type != CONF_ITEM_SECTION)
1499                         continue;
1500
1501                 subcs = cf_itemtosection(ci);
1502                 if (!name1) {
1503                         if (!subcs->name2) {
1504                                 if (strcmp(subcs->name1, name2) == 0) break;
1505                         } else {
1506                                 if (strcmp(subcs->name2, name2) == 0) break;
1507                         }
1508                         continue; /* don't do the string comparisons below */
1509                 }
1510
1511                 if ((strcmp(subcs->name1, name1) == 0) &&
1512                     (subcs->name2 != NULL) &&
1513                     (strcmp(subcs->name2, name2) == 0))
1514                         break;
1515         }
1516
1517         return cf_itemtosection(ci);
1518 }
1519
1520 /*
1521  * Return the next subsection after a CONF_SECTION
1522  * with a certain name1 (char *name1). If the requested
1523  * name1 is NULL, any name1 matches.
1524  */
1525
1526 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1527                                       CONF_SECTION *subsection,
1528                                       const char *name1)
1529 {
1530         CONF_ITEM       *ci;
1531
1532         /*
1533          * If subsection is NULL this must be a first time run
1534          * Find the subsection with correct name
1535          */
1536
1537         if (subsection == NULL){
1538                 ci = section->children;
1539         } else {
1540                 ci = cf_sectiontoitem(subsection)->next;
1541         }
1542
1543         for (; ci; ci = ci->next) {
1544                 if (ci->type != CONF_ITEM_SECTION)
1545                         continue;
1546                 if ((name1 == NULL) ||
1547                     (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1548                         break;
1549         }
1550
1551         return cf_itemtosection(ci);
1552 }
1553
1554 /*
1555  * Return the next item after a CONF_ITEM.
1556  */
1557
1558 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1559 {
1560         /*
1561          * If item is NULL this must be a first time run
1562          * Return the first item
1563          */
1564
1565         if (item == NULL) {
1566                 return section->children;
1567         } else {
1568                 return item->next;
1569         }
1570 }
1571
1572 int cf_section_lineno(CONF_SECTION *section)
1573 {
1574         return cf_sectiontoitem(section)->lineno;
1575 }
1576
1577 int cf_pair_lineno(CONF_PAIR *pair)
1578 {
1579         return cf_pairtoitem(pair)->lineno;
1580 }
1581
1582 int cf_item_is_section(CONF_ITEM *item)
1583 {
1584         return item->type == CONF_ITEM_SECTION;
1585 }
1586 int cf_item_is_pair(CONF_ITEM *item)
1587 {
1588         return item->type == CONF_ITEM_PAIR;
1589 }
1590
1591
1592 static CONF_DATA *cf_data_alloc(CONF_SECTION *parent, const char *name,
1593                                 void *data, void (*data_free)(void *))
1594 {
1595         CONF_DATA *cd;
1596
1597         cd = rad_malloc(sizeof(*cd));
1598         memset(cd, 0, sizeof(*cd));
1599
1600         cd->item.type = CONF_ITEM_DATA;
1601         cd->item.parent = parent;
1602         cd->name = strdup(name);
1603         cd->data = data;
1604         cd->free = data_free;
1605
1606         return cd;
1607 }
1608
1609
1610 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
1611                                    int flag)
1612 {
1613         if (!cs || !name) return NULL;
1614
1615         /*
1616          *      Find the name in the tree, for speed.
1617          */
1618         if (cs->data_tree) {
1619                 CONF_DATA mycd, *cd;
1620
1621                 mycd.name = name;
1622                 mycd.flag = flag;
1623                 cd = rbtree_finddata(cs->data_tree, &mycd);
1624                 if (cd) return cd->data;
1625         }
1626
1627         return NULL;
1628 }
1629
1630 /*
1631  *      Find data from a particular section.
1632  */
1633 void *cf_data_find(CONF_SECTION *cs, const char *name)
1634 {
1635         return cf_data_find_internal(cs, name, 0);
1636 }
1637
1638
1639 /*
1640  *      Add named data to a configuration section.
1641  */
1642 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
1643                                 void *data, void (*data_free)(void *),
1644                                 int flag)
1645 {
1646         CONF_DATA *cd;
1647
1648         if (!cs || !name) return -1;
1649
1650         /*
1651          *      Already exists.  Can't add it.
1652          */
1653         if (cf_data_find_internal(cs, name, flag) != NULL) return -1;
1654
1655         cd = cf_data_alloc(cs, name, data, data_free);
1656         if (!cd) return -1;
1657         cd->flag = flag;
1658
1659         cf_item_add(cs, cf_datatoitem(cd));
1660
1661         return 0;
1662 }
1663
1664 /*
1665  *      Add named data to a configuration section.
1666  */
1667 int cf_data_add(CONF_SECTION *cs, const char *name,
1668                 void *data, void (*data_free)(void *))
1669 {
1670         return cf_data_add_internal(cs, name, data, data_free, 0);
1671 }
1672
1673
1674 /*
1675  *      Copy CONF_DATA from src to dst
1676  */
1677 static void cf_section_copy_data(CONF_SECTION *s, CONF_SECTION *d)
1678 {
1679
1680         CONF_ITEM *cd, *next, **last;
1681
1682         /*
1683          *      Don't check if s->data_tree is NULL.  It's child
1684          *      sections may have data, even if this section doesn't.
1685          */
1686
1687         rad_assert(d->data_tree == NULL);
1688         d->data_tree = s->data_tree;
1689         s->data_tree = NULL;
1690
1691         /*
1692          *      Walk through src, moving CONF_ITEM_DATA
1693          *      to dst, by hand.
1694          */
1695         last = &(s->children);
1696         for (cd = s->children; cd != NULL; cd = next) {
1697                 next = cd->next;
1698
1699                 /*
1700                  *      Recursively copy data from child sections.
1701                  */
1702                 if (cd->type == CONF_ITEM_SECTION) {
1703                         CONF_SECTION *s1, *d1;
1704
1705                         s1 = cf_itemtosection(cd);
1706                         d1 = cf_section_sub_find_name2(d, s1->name1, s1->name2);
1707                         if (d1) {
1708                                 cf_section_copy_data(s1, d1);
1709                         }
1710                         last = &(cd->next);
1711                         continue;
1712                 }
1713
1714                 /*
1715                  *      Not conf data, remember last ptr.
1716                  */
1717                 if (cd->type != CONF_ITEM_DATA) {
1718                         last = &(cd->next);
1719                         continue;
1720                 }
1721
1722                 /*
1723                  *      Remove it from the src list
1724                  */
1725                 *last = cd->next;
1726                 cd->next = NULL;
1727
1728                 /*
1729                  *      Add it to the dst list
1730                  */
1731                 if (!d->children) {
1732                         rad_assert(d->tail == NULL);
1733                         d->children = cd;
1734                 } else {
1735                         rad_assert(d->tail != NULL);
1736                         d->tail->next = cd;
1737                 }
1738                 d->tail = cd;
1739         }
1740 }
1741
1742 /*
1743  *      For a CONF_DATA element, stat the filename, if necessary.
1744  */
1745 static int filename_stat(void *context, void *data)
1746 {
1747         struct stat buf;
1748         CONF_DATA *cd = data;
1749
1750         context = context;      /* -Wunused */
1751
1752         if (cd->flag != PW_TYPE_FILENAME) return 0;
1753
1754         if (stat(cd->name, &buf) < 0) return -1;
1755
1756         if (buf.st_mtime != *(time_t *) cd->data) return -1;
1757
1758         return 0;
1759 }
1760
1761
1762 /*
1763  *      Compare two CONF_SECTIONS.  The items MUST be in the same
1764  *      order.
1765  */
1766 static int cf_section_cmp(CONF_SECTION *a, CONF_SECTION *b)
1767 {
1768         CONF_ITEM *ca = a->children;
1769         CONF_ITEM *cb = b->children;
1770
1771         while (1) {
1772                 CONF_PAIR *pa, *pb;
1773
1774                 /*
1775                  *      Done.  Stop.
1776                  */
1777                 if (!ca && !cb) break;
1778
1779                 /*
1780                  *      Skip CONF_DATA.
1781                  */
1782                 if (ca && ca->type == CONF_ITEM_DATA) {
1783                         ca = ca->next;
1784                         continue;
1785                 }
1786                 if (cb && cb->type == CONF_ITEM_DATA) {
1787                         cb = cb->next;
1788                         continue;
1789                 }
1790
1791                 /*
1792                  *      One is smaller than the other.  Exit.
1793                  */
1794                 if (!ca || !cb) return 0;
1795
1796                 if (ca->type != cb->type) return 0;
1797
1798                 /*
1799                  *      Deal with subsections.
1800                  */
1801                 if (ca->type == CONF_ITEM_SECTION) {
1802                         CONF_SECTION *sa = cf_itemtosection(ca);
1803                         CONF_SECTION *sb = cf_itemtosection(cb);
1804
1805                         if (!cf_section_cmp(sa, sb)) return 0;
1806                         goto next;
1807                 }
1808
1809                 rad_assert(ca->type == CONF_ITEM_PAIR);
1810
1811                 pa = cf_itemtopair(ca);
1812                 pb = cf_itemtopair(cb);
1813
1814                 /*
1815                  *      Different attr and/or value, Exit.
1816                  */
1817                 if ((strcmp(pa->attr, pb->attr) != 0) ||
1818                     (strcmp(pa->value, pb->value) != 0)) return 0;
1819
1820
1821                 /*
1822                  *      And go to the next element.
1823                  */
1824         next:
1825                 ca = ca->next;
1826                 cb = cb->next;
1827         }
1828
1829         /*
1830          *      Walk over the CONF_DATA, stat'ing PW_TYPE_FILENAME.
1831          */
1832         if (a->data_tree &&
1833             (rbtree_walk(a->data_tree, InOrder, filename_stat, NULL) != 0)) {
1834                 return 0;
1835         }
1836
1837         /*
1838          *      They must be the same, say so.
1839          */
1840         return 1;
1841 }
1842
1843
1844 /*
1845  *      Migrate CONF_DATA from one section to another.
1846  */
1847 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1848 {
1849         CONF_ITEM *ci;
1850         CONF_SECTION *s, *d;
1851
1852         for (ci = src->children; ci != NULL; ci = ci->next) {
1853                 if (ci->type != CONF_ITEM_SECTION)
1854                         continue;
1855
1856                 s = cf_itemtosection(ci);
1857                 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1858
1859                 if (!d) continue; /* not in new one, don't migrate it */
1860
1861                 /*
1862                  *      A section of the same name is in BOTH src & dst,
1863                  *      compare the CONF_PAIR's.  If they're all the same,
1864                  *      then copy the CONF_DATA from one to the other.
1865                  */
1866                 if (cf_section_cmp(s, d)) {
1867                         cf_section_copy_data(s, d);
1868                 }
1869         }
1870
1871         return 1;               /* rcode means anything? */
1872 }
1873
1874 int cf_section_template(CONF_SECTION *cs, CONF_SECTION *template)
1875 {
1876         if (!cs || !template || cs->template || template->template) return -1;
1877
1878         cs->template = template;
1879
1880         return 0;
1881 }
1882
1883 #if 0
1884 /*
1885  * JMG dump_config tries to dump the config structure in a readable format
1886  *
1887 */
1888
1889 static int dump_config_section(CONF_SECTION *cs, int indent)
1890 {
1891         CONF_SECTION    *scs;
1892         CONF_PAIR       *cp;
1893         CONF_ITEM       *ci;
1894
1895         /* The DEBUG macro doesn't let me
1896          *   for(i=0;i<indent;++i) debugputchar('\t');
1897          * so I had to get creative. --Pac. */
1898
1899         for (ci = cs->children; ci; ci = ci->next) {
1900                 switch (ci->type) {
1901                 case CONF_ITEM_PAIR:
1902                         cp=cf_itemtopair(ci);
1903                         DEBUG("%.*s%s = %s",
1904                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1905                                 cp->attr, cp->value);
1906                         break;
1907
1908                 case CONF_ITEM_SECTION:
1909                         scs=cf_itemtosection(ci);
1910                         DEBUG("%.*s%s %s%s{",
1911                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1912                                 scs->name1,
1913                                 scs->name2 ? scs->name2 : "",
1914                                 scs->name2 ?  " " : "");
1915                         dump_config_section(scs, indent+1);
1916                         DEBUG("%.*s}",
1917                                 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1918                         break;
1919
1920                 default:        /* FIXME: Do more! */
1921                         break;
1922                 }
1923         }
1924
1925         return 0;
1926 }
1927
1928 int dump_config(void)
1929 {
1930         return dump_config_section(mainconfig.config, 0);
1931 }
1932 #endif