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