Added 'base' and 'variables' to CONF_SECTION, so that the
[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/autoconf.h>
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #ifdef HAVE_NETINET_IN_H
38 #       include <netinet/in.h>
39 #endif
40
41 #ifdef HAVE_DIRENT_H
42 #include <dirent.h>
43
44 #ifdef HAVE_SYS_STAT_H
45 #include <sys/stat.h>
46 #endif
47 #endif
48
49 #include <ctype.h>
50
51 #include <freeradius-devel/radiusd.h>
52 #include <freeradius-devel/rad_assert.h>
53 #include <freeradius-devel/modules.h>
54
55 typedef enum conf_type {
56         CONF_ITEM_INVALID = 0,
57         CONF_ITEM_PAIR,
58         CONF_ITEM_SECTION,
59         CONF_ITEM_DATA
60 } CONF_ITEM_TYPE;
61
62 struct conf_item {
63         struct conf_item *next;
64         struct conf_part *parent;
65         int lineno;
66         CONF_ITEM_TYPE type;
67 };
68 struct conf_pair {
69         CONF_ITEM item;
70         char *attr;
71         char *value;
72         LRAD_TOKEN operator;
73 };
74 struct conf_part {
75         CONF_ITEM item;
76         const char *name1;
77         const char *name2;
78         struct conf_item *children;
79         struct conf_item *tail; /* for speed */
80         rbtree_t        *pair_tree; /* and a partridge.. */
81         rbtree_t        *section_tree; /* no jokes here */
82         rbtree_t        *name2_tree; /* for sections of the same name2 */
83         rbtree_t        *data_tree;
84         void *base;
85         const CONF_PARSER *variables;
86 };
87
88
89 /*
90  *      Internal data that is associated with a configuration section,
91  *      so that we don't have to track it separately.
92  */
93 struct conf_data {
94         CONF_ITEM  item;
95         const char *name;
96         int        flag;
97         void       *data;       /* user data */
98         void       (*free)(void *); /* free user data function */
99 };
100
101
102 static int cf_data_add_internal(CONF_SECTION *cs, const char *name,
103                                 void *data, void (*data_free)(void *),
104                                 int flag);
105 static void *cf_data_find_internal(CONF_SECTION *cs, const char *name,
106                                    int flag);
107
108 /*
109  *      Isolate the scary casts in these tiny provably-safe functions
110  */
111 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
112 {
113         if (ci == NULL)
114                 return NULL;
115         rad_assert(ci->type == CONF_ITEM_PAIR);
116         return (CONF_PAIR *)ci;
117 }
118 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
119 {
120         if (ci == NULL)
121                 return NULL;
122         rad_assert(ci->type == CONF_ITEM_SECTION);
123         return (CONF_SECTION *)ci;
124 }
125 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
126 {
127         if (cp == NULL)
128                 return NULL;
129         return (CONF_ITEM *)cp;
130 }
131 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
132 {
133         if (cs == NULL)
134                 return NULL;
135         return (CONF_ITEM *)cs;
136 }
137
138 static CONF_DATA *cf_itemtodata(CONF_ITEM *ci)
139 {
140         if (ci == NULL)
141                 return NULL;
142         rad_assert(ci->type == CONF_ITEM_DATA);
143         return (CONF_DATA *)ci;
144 }
145 static CONF_ITEM *cf_datatoitem(CONF_DATA *cd)
146 {
147         if (cd == NULL)
148                 return NULL;
149         return (CONF_ITEM *)cd;
150 }
151
152 /*
153  *      Create a new CONF_PAIR
154  */
155 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
156                                 LRAD_TOKEN operator, CONF_SECTION *parent)
157 {
158         CONF_PAIR *cp;
159
160         cp = rad_malloc(sizeof(*cp));
161         memset(cp, 0, sizeof(*cp));
162         cp->item.type = CONF_ITEM_PAIR;
163         cp->item.parent = parent;
164         cp->attr = strdup(attr);
165         cp->value = strdup(value);
166         cp->operator = operator;
167
168         return cp;
169 }
170
171 /*
172  *      Free a CONF_PAIR
173  */
174 void cf_pair_free(CONF_PAIR **cp)
175 {
176         if (!cp || !*cp) return;
177
178         if ((*cp)->attr)
179                 free((*cp)->attr);
180         if ((*cp)->value)
181                 free((*cp)->value);
182
183 #ifndef NDEBUG
184         memset(*cp, 0, sizeof(*cp));
185 #endif
186         free(*cp);
187
188         *cp = NULL;
189 }
190
191
192 static void cf_data_free(CONF_DATA **cd)
193 {
194         if (!cd || !*cd) return;
195
196         free((*cd)->name);
197         if (!(*cd)->free) {
198                 free((*cd)->data);
199         } else {
200                 ((*cd)->free)((*cd)->data);
201         }
202 #ifndef NDEBUG
203         memset(*cd, 0, sizeof(*cd));
204 #endif
205         free(*cd);
206         *cd = NULL;
207 }
208
209 /*
210  *      rbtree callback function
211  */
212 static int pair_cmp(const void *a, const void *b)
213 {
214         const CONF_PAIR *one = a;
215         const CONF_PAIR *two = b;
216
217         return strcmp(one->attr, two->attr);
218 }
219
220
221 /*
222  *      rbtree callback function
223  */
224 static int section_cmp(const void *a, const void *b)
225 {
226         const CONF_SECTION *one = a;
227         const CONF_SECTION *two = b;
228
229         return strcmp(one->name1, two->name1);
230 }
231
232
233 /*
234  *      rbtree callback function
235  */
236 static int name2_cmp(const void *a, const void *b)
237 {
238         const CONF_SECTION *one = a;
239         const CONF_SECTION *two = b;
240
241         rad_assert(strcmp(one->name1, two->name1) == 0);
242
243         if (!one->name2 && !two->name2) return 0;
244         if (!one->name2) return -1;
245         if (!two->name2) return +1;
246
247         return strcmp(one->name2, two->name2);
248 }
249
250
251 /*
252  *      rbtree callback function
253  */
254 static int data_cmp(const void *a, const void *b)
255 {
256         int rcode;
257
258         const CONF_DATA *one = a;
259         const CONF_DATA *two = b;
260
261         rcode = one->flag - two->flag;
262         if (rcode != 0) return rcode;
263
264         return strcmp(one->name, two->name);
265 }
266
267
268 /*
269  *      Free strings we've parsed into data structures.
270  */
271 static void cf_section_parse_free(void *base, const CONF_PARSER *variables)
272 {
273         int i;
274
275         /*
276          *      Don't automatically free the strings if we're being
277          *      called from a module.
278          */
279         if (base || !variables) return;
280         
281         /*
282          *      Free up dynamically allocated string pointers.
283          */
284         for (i = 0; variables[i].name != NULL; i++) {
285                 char **p;
286
287                 if (variables[i].type != PW_TYPE_STRING_PTR) {
288                         continue;
289                 }
290
291                 /*
292                  *      Prefer the data, if it's there.
293                  *      Else use the base + offset.
294                  */
295                 if (!variables[i].data) {
296                         continue;
297                 }
298
299                 /*
300                  *      FIXME: Add base
301                  */
302                 p = (char **) (variables[i].data);
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(" %s: %s = %s", cs->name1, name, value);
759                 break;
760                 
761         case PW_TYPE_INTEGER:
762                 *(int *)data = strtol(value, 0, 0);
763                 DEBUG2(" %s: %s = %d",
764                        cs->name1, name,
765                        *(int *)data);
766                 break;
767                 
768         case PW_TYPE_STRING_PTR:
769                 q = (char **) data;
770                 if (*q != NULL) {
771                         free(*q);
772                 }
773                 
774                 /*
775                  *      Expand variables which haven't already been
776                  *      expanded automagically when the configuration
777                  *      file was read.
778                  */
779                 if (value == dflt) {
780                         char buffer[8192];
781
782                         int lineno = cs->item.lineno;
783
784                         /*
785                          *      FIXME: sizeof(buffer)?
786                          */
787                         value = cf_expand_variables("?",
788                                                     &lineno,
789                                                     cs, buffer, value);
790                         if (!value) return -1;
791                 }
792                 
793                 DEBUG2(" %s: %s = \"%s\"",
794                        cs->name1, name,
795                        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(" %s: %s = \"%s\"",
830                        cs->name1, name,
831                        value ? value : "(null)");
832                 *q = value ? strdup(value) : NULL;
833
834                 /*
835                  *      And now we "stat" the file.
836                  */
837                 if (*q) {
838                         struct stat buf;
839
840                         if (stat(*q, &buf) == 0) {
841                                 time_t *mtime;
842
843                                 mtime = rad_malloc(sizeof(*mtime));
844                                 *mtime = buf.st_mtime;
845                                 /* FIXME: error? */
846                                 cf_data_add_internal(cs, *q, mtime, free,
847                                                      PW_TYPE_FILENAME);
848                         }
849                 }
850                 break;
851
852         case PW_TYPE_IPADDR:
853                 /*
854                  *      Allow '*' as any address
855                  */
856                 if (strcmp(value, "*") == 0) {
857                         *(uint32_t *) data = htonl(INADDR_ANY);
858                         DEBUG2(" %s: %s = *", cs->name1, name);
859                         break;
860                 }
861                 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
862                         radlog(L_ERR, "Can't find IP address for host %s", value);
863                         return -1;
864                 }
865                 DEBUG2(" %s: %s = %s IP address [%s]",
866                        cs->name1, name, value,
867                        ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
868                 *(uint32_t *) data = ipaddr.ipaddr.ip4addr.s_addr;
869                 break;
870                 
871         case PW_TYPE_IPV6ADDR:
872                 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
873                         radlog(L_ERR, "Can't find IPv6 address for host %s", value);
874                         return -1;
875                 }
876                 DEBUG2(" %s: %s = %s IPv6 address [%s]",
877                        cs->name1, name, value,
878                        ip_ntoh(&ipaddr, ipbuf, sizeof(ipbuf)));
879                 memcpy(data, &ipaddr.ipaddr.ip6addr,
880                        sizeof(ipaddr.ipaddr.ip6addr));
881                 break;
882                 
883         default:
884                 radlog(L_ERR, "type %d not supported yet", type);
885                 return -1;
886                 break;
887         } /* switch over variable type */
888         
889         return rcode;
890 }
891
892 /*
893  *      Parse a configuration section into user-supplied variables.
894  */
895 int cf_section_parse(CONF_SECTION *cs, void *base,
896                      const CONF_PARSER *variables)
897 {
898         int i;
899         void *data;
900
901         /*
902          *      Handle the known configuration parameters.
903          */
904         for (i = 0; variables[i].name != NULL; i++) {
905                 /*
906                  *      Handle subsections specially
907                  */
908                 if (variables[i].type == PW_TYPE_SUBSECTION) {
909                         const CONF_SECTION *subcs;
910                         subcs = cf_section_sub_find(cs, variables[i].name);
911                         
912                         /*
913                          *      If the configuration section is NOT there,
914                          *      then ignore it.
915                          *
916                          *      FIXME! This is probably wrong... we should
917                          *      probably set the items to their default values.
918                          */
919                         if (!subcs) continue;
920
921                         if (!variables[i].dflt) {
922                                 DEBUG2("Internal sanity check 1 failed in cf_section_parse");
923                                 cf_section_parse_free(base, variables);
924                                 return -1;
925                         }
926                         
927                         if (cf_section_parse(subcs, base,
928                                              (const CONF_PARSER *) variables[i].dflt) < 0) {
929                                 cf_section_parse_free(base, variables);
930                                 return -1;
931                         }
932                         continue;
933                 } /* else it's a CONF_PAIR */
934                 
935                 if (variables[i].data) {
936                         data = variables[i].data; /* prefer this. */
937                 } else if (base) {
938                         data = ((char *)base) + variables[i].offset;
939                 } else {
940                         DEBUG2("Internal sanity check 2 failed in cf_section_parse");
941                         cf_section_parse_free(base, variables);
942                         return -1;
943                 }
944
945                 /*
946                  *      Parse the pair we found, or a default value.
947                  */
948                 if (cf_item_parse(cs, variables[i].name, variables[i].type,
949                                   data, variables[i].dflt) < 0) {
950                         cf_section_parse_free(base, variables);
951                         return -1;
952                 }
953         } /* for all variables in the configuration section */
954
955         cs->base = base;
956         cs->variables = variables;
957
958         return 0;
959 }
960
961
962 /*
963  *      Read a part of the config file.
964  */
965 static int cf_section_read(const char *file, int *lineno, FILE *fp,
966                            CONF_SECTION *current)
967
968 {
969         CONF_SECTION *this, *css;
970         CONF_PAIR *cpn;
971         char *ptr;
972         const char *value;
973         char buf[8192];
974         char buf1[8192];
975         char buf2[8192];
976         char buf3[8192];
977         int t1, t2, t3;
978         char *cbuf = buf;
979         int len;
980
981         this = current;         /* add items here */
982
983         /*
984          *      Read, checking for line continuations ('\\' at EOL)
985          */
986         for (;;) {
987                 int eof;
988
989                 /*
990                  *      Get data, and remember if we are at EOF.
991                  */
992                 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
993                 (*lineno)++;
994
995                 len = strlen(cbuf);
996
997                 /*
998                  *      We've filled the buffer, and there isn't
999                  *      a CR in it.  Die!
1000                  */
1001                 if ((len == (sizeof(buf) - 1)) &&
1002                     (cbuf[len - 1] != '\n')) {
1003                         radlog(L_ERR, "%s[%d]: Line too long",
1004                                file, *lineno);
1005                         return -1;
1006                 }
1007
1008                 /*
1009                  *  Check for continuations.
1010                  */
1011                 if (cbuf[len - 1] == '\n') len--;
1012
1013                 /*
1014                  *      Last character is '\\'.  Over-write it,
1015                  *      and read another line.
1016                  */
1017                 if ((len > 0) && (cbuf[len - 1] == '\\')) {
1018                         cbuf[len - 1] = '\0';
1019                         cbuf += len - 1;
1020                         continue;
1021                 }
1022
1023                 /*
1024                  *  We're at EOF, and haven't read anything.  Stop.
1025                  */
1026                 if (eof && (cbuf == buf)) {
1027                         break;
1028                 }
1029
1030                 ptr = cbuf = buf;
1031                 t1 = gettoken(&ptr, buf1, sizeof(buf1));
1032
1033                if ((*buf1 == '#') || (*buf1 == '\0')) {
1034                        continue;
1035                }
1036
1037                 /*
1038                  *      The caller eats "name1 name2 {", and calls us
1039                  *      for the data inside of the section.  So if we
1040                  *      receive a closing brace, then it must mean the
1041                  *      end of the section.
1042                  */
1043                if (t1 == T_RCBRACE) {
1044                        if (this == current) {
1045                                radlog(L_ERR, "%s[%d]: Too many closing braces",
1046                                       file, *lineno);
1047                                return -1;
1048                                
1049                        }
1050                        this = this->item.parent;
1051                        continue;
1052                 }
1053
1054                 /*
1055                  *      Allow for $INCLUDE files
1056                  *
1057                  *      This *SHOULD* work for any level include.
1058                  *      I really really really hate this file.  -cparker
1059                  */
1060                 if (strcasecmp(buf1, "$INCLUDE") == 0) {
1061                         t2 = getword(&ptr, buf2, sizeof(buf2));
1062
1063                         value = cf_expand_variables(file, lineno, this, buf, buf2);
1064                         if (!value) return -1;
1065
1066 #ifdef HAVE_DIRENT_H
1067                         /*
1068                          *      $INCLUDE foo/
1069                          *
1070                          *      Include ALL non-"dot" files in the directory.
1071                          *      careful!
1072                          */
1073                         if (value[strlen(value) - 1] == '/') {
1074                                 DIR             *dir;
1075                                 struct dirent   *dp;
1076                                 struct stat stat_buf;
1077
1078                                 DEBUG2( "Config:   including files in directory: %s", value );
1079                                 dir = opendir(value);
1080                                 if (!dir) {
1081                                         radlog(L_ERR, "%s[%d]: Error reading directory %s: %s",
1082                                                file, *lineno, value,
1083                                                strerror(errno));
1084                                         return -1;
1085                                 }
1086
1087                                 /*
1088                                  *      Read the directory, ignoring "." files.
1089                                  */
1090                                 while ((dp = readdir(dir)) != NULL) {
1091                                         const char *p;
1092
1093                                         if (dp->d_name[0] == '.') continue;
1094
1095                                         /*
1096                                          *      Check for valid characters
1097                                          */
1098                                         for (p = dp->d_name; *p != '\0'; p++) {
1099                                                 if (isalpha((int)*p) ||
1100                                                     isdigit((int)*p) ||
1101                                                     (*p == '_') ||
1102                                                     (*p == '.')) continue;
1103                                                 break;
1104                                         }
1105                                         if (*p != '\0') continue;
1106
1107                                         snprintf(buf2, sizeof(buf2), "%s%s",
1108                                                  value, dp->d_name);
1109                                         if ((stat(buf2, &stat_buf) != 0) ||
1110                                             S_ISDIR(stat_buf.st_mode)) continue;
1111                                         /*
1112                                          *      Read the file into the current
1113                                          *      configuration sectoin.
1114                                          */
1115                                         if (cf_file_include(buf2, this) < 0) {
1116                                                 closedir(dir);
1117                                                 return -1;
1118                                         }
1119                                 }
1120                                 closedir(dir);
1121                         }  else
1122 #endif
1123                         { /* it was a normal file */
1124                                 if (cf_file_include(value, this) < 0) {
1125                                         return -1;
1126                                 }
1127                         }
1128                         continue;
1129                 } /* we were in an include */
1130
1131                 /*
1132                  *      No '=': must be a section or sub-section.
1133                  */
1134                 if (strchr(ptr, '=') == NULL) {
1135                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
1136                         t3 = gettoken(&ptr, buf3, sizeof(buf3));
1137                 } else {
1138                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
1139                         t3 = getword(&ptr, buf3, sizeof(buf3));
1140                 }
1141
1142                 /*
1143                  * Perhaps a subsection.
1144                  */
1145                 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
1146                         css = cf_section_alloc(buf1,
1147                                                t2 == T_LCBRACE ? NULL : buf2,
1148                                                this);
1149                         if (!css) {
1150                                 radlog(L_ERR, "%s[%d]: Failed allocating memory for section",
1151                                                 file, *lineno);
1152                         }
1153                         cf_item_add(this, cf_sectiontoitem(css));
1154                         css->item.lineno = *lineno;
1155
1156                         /*
1157                          *      The current section is now the child section.
1158                          */
1159                         this = css;
1160                         continue;
1161                 }
1162
1163                 /*
1164                  *      Ignore semi-colons.
1165                  */
1166                 if (*buf2 == ';')
1167                         *buf2 = '\0';
1168
1169                 /*
1170                  *      Must be a normal attr = value line.
1171                  */
1172                 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
1173                         t2 = T_OP_EQ;
1174                 } else if (buf1[0] == 0 || buf2[0] == 0 ||
1175                            (t2 < T_EQSTART || t2 > T_EQEND)) {
1176                         radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
1177                                         file, *lineno);
1178                         return -1;
1179                 }
1180
1181                 /*
1182                  *      Ensure that the user can't add CONF_PAIRs
1183                  *      with 'internal' names;
1184                  */
1185                 if (buf1[0] == '_') {
1186                         radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
1187                                         file, *lineno, buf1);
1188                         return -1;
1189                 }
1190
1191                 /*
1192                  *      Handle variable substitution via ${foo}
1193                  */
1194                 value = cf_expand_variables(file, lineno, this, buf, buf3);
1195                 if (!value) return -1;
1196
1197
1198                 /*
1199                  *      Add this CONF_PAIR to our CONF_SECTION
1200                  */
1201                 cpn = cf_pair_alloc(buf1, value, t2, this);
1202                 cpn->item.lineno = *lineno;
1203                 cf_item_add(this, cf_pairtoitem(cpn));
1204         }
1205
1206         /*
1207          *      See if EOF was unexpected ..
1208          */
1209         if (feof(fp) && (this != current)) {
1210                 radlog(L_ERR, "%s[%d]: EOF reached without closing brace for section %s starting at line %d",
1211                        file, *lineno,
1212                        cf_section_name1(this), cf_section_lineno(this));
1213                 return -1;
1214         }
1215
1216         return 0;
1217 }
1218
1219 /*
1220  *      Include one config file in another.
1221  */
1222 int cf_file_include(const char *file, CONF_SECTION *cs)
1223 {
1224         FILE            *fp;
1225         int             lineno = 0;
1226         struct stat     statbuf;
1227         time_t          *mtime;
1228
1229         DEBUG2( "Config:   including file: %s", file);
1230
1231         if (stat(file, &statbuf) == 0) {
1232                 if ((statbuf.st_mode & S_IWOTH) != 0) {
1233                         radlog(L_ERR|L_CONS, "Configuration file %s is globally writable.  Refusing to start due to insecure configuration.",
1234                                file);
1235                         return -1;
1236                 }
1237
1238                 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
1239                         radlog(L_ERR|L_CONS, "Configuration file %s is globally readable.  Refusing to start due to insecure configuration.",
1240                                file);
1241                         return -1;
1242                 }
1243         }
1244
1245         fp = fopen(file, "r");
1246         if (!fp) {
1247                 radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
1248                        file, strerror(errno));
1249                 return -1;
1250         }
1251
1252         /*
1253          *      Read the section.  It's OK to have EOF without a
1254          *      matching close brace.
1255          */
1256         if (cf_section_read(file, &lineno, fp, cs) < 0) {
1257                 fclose(fp);
1258                 return -1;
1259         }
1260
1261         /*
1262          *      Add the filename to the section
1263          */
1264         mtime = rad_malloc(sizeof(*mtime));
1265         *mtime = statbuf.st_mtime;
1266         /* FIXME: error? */
1267         cf_data_add_internal(cs, file, mtime, free,
1268                              PW_TYPE_FILENAME);
1269
1270         fclose(fp);
1271         return 0;
1272 }
1273
1274 /*
1275  *      Bootstrap a config file.
1276  */
1277 CONF_SECTION *cf_file_read(const char *file)
1278 {
1279         CONF_SECTION *cs;
1280
1281         cs = cf_section_alloc("main", NULL, NULL);
1282         if (!cs) return NULL;
1283
1284         if (cf_file_include(file, cs) < 0) {
1285                 cf_section_free(&cs);
1286                 return NULL;
1287         }
1288
1289         return cs;
1290 }
1291
1292 /*
1293  * Return a CONF_PAIR within a CONF_SECTION.
1294  */
1295 CONF_PAIR *cf_pair_find(const CONF_SECTION *cs, const char *name)
1296 {
1297         CONF_ITEM       *ci;
1298
1299         if (!cs) cs = mainconfig.config;
1300
1301         /*
1302          *      Find the name in the tree, for speed.
1303          */
1304         if (name) {
1305                 CONF_PAIR mycp;
1306
1307                 mycp.attr = name;
1308                 return rbtree_finddata(cs->pair_tree, &mycp);
1309         }
1310
1311         /*
1312          *      Else find the first one
1313          */
1314         for (ci = cs->children; ci; ci = ci->next) {
1315                 if (ci->type == CONF_ITEM_PAIR)
1316                         return cf_itemtopair(ci);
1317         }
1318         
1319         return NULL;
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
1823 /*
1824  *      Migrate CONF_DATA from one section to another.
1825  */
1826 int cf_section_migrate(CONF_SECTION *dst, CONF_SECTION *src)
1827 {
1828         CONF_ITEM *ci;
1829         CONF_SECTION *s, *d;
1830
1831         for (ci = src->children; ci != NULL; ci = ci->next) {
1832                 if (ci->type != CONF_ITEM_SECTION)
1833                         continue;
1834
1835                 s = cf_itemtosection(ci);
1836                 d = cf_section_sub_find_name2(dst, s->name1, s->name2);
1837
1838                 if (!d) continue; /* not in new one, don't migrate it */
1839
1840                 /*
1841                  *      A section of the same name is in BOTH src & dst,
1842                  *      compare the CONF_PAIR's.  If they're all the same,
1843                  *      then copy the CONF_DATA from one to the other.
1844                  */
1845                 if (cf_section_cmp(s, d)) {
1846                         cf_section_copy_data(s, d);
1847                 }
1848         }
1849
1850         return 1;               /* rcode means anything? */
1851 }
1852
1853
1854 #if 0
1855 /*
1856  * JMG dump_config tries to dump the config structure in a readable format
1857  *
1858 */
1859
1860 static int dump_config_section(CONF_SECTION *cs, int indent)
1861 {
1862         CONF_SECTION    *scs;
1863         CONF_PAIR       *cp;
1864         CONF_ITEM       *ci;
1865
1866         /* The DEBUG macro doesn't let me
1867          *   for(i=0;i<indent;++i) debugputchar('\t');
1868          * so I had to get creative. --Pac. */
1869
1870         for (ci = cs->children; ci; ci = ci->next) {
1871                 switch (ci->type) {
1872                 case CONF_ITEM_PAIR:
1873                         cp=cf_itemtopair(ci);
1874                         DEBUG("%.*s%s = %s",
1875                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1876                                 cp->attr, cp->value);
1877                         break;
1878
1879                 case CONF_ITEM_SECTION:
1880                         scs=cf_itemtosection(ci);
1881                         DEBUG("%.*s%s %s%s{",
1882                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1883                                 scs->name1,
1884                                 scs->name2 ? scs->name2 : "",
1885                                 scs->name2 ?  " " : "");
1886                         dump_config_section(scs, indent+1);
1887                         DEBUG("%.*s}",
1888                                 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1889                         break;
1890
1891                 default:        /* FIXME: Do more! */
1892                         break;
1893                 }
1894         }
1895
1896         return 0;
1897 }
1898
1899 int dump_config(void)
1900 {
1901         return dump_config_section(mainconfig.config, 0);
1902 }
1903 #endif