Fix a memory leak. If a file is $INCLUDEd, we have a CONF_SECTION
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  * Copyright 2000  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 "autoconf.h"
30 #include "libradius.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35
36 #if HAVE_NETINET_IN_H
37 #       include <netinet/in.h>
38 #endif
39
40 #include "radiusd.h"
41 #include "conffile.h"
42 #include "token.h"
43 #include "modules.h"
44
45 static const char rcsid[] =
46 "$Id$";
47
48 #define xstrdup strdup
49
50 typedef enum conf_type {
51         CONF_ITEM_PAIR,
52         CONF_ITEM_SECTION
53 } CONF_ITEM_TYPE;
54
55 struct conf_item {
56         struct conf_item        *next;
57         struct conf_part        *parent;
58         int                     lineno;
59         CONF_ITEM_TYPE          type;
60 };
61 struct conf_pair {
62         CONF_ITEM       item;
63         char            *attr;
64         char            *value;
65         int             operator;
66 };
67 struct conf_part {
68         CONF_ITEM               item;
69         char                    *name1;
70         char                    *name2;
71         struct conf_item        *children;
72 };
73
74 CONF_SECTION    *config = NULL;
75
76 /*
77  *      Yucky hacks.
78  */
79 extern RADCLIENT *clients;
80 extern REALM     *realms;
81 extern int read_realms_file(const char *file);
82
83 static int generate_realms(const char *filename);
84 static int generate_clients(const char *filename);
85 static CONF_SECTION *conf_read(const char *fromfile, int fromline, const char *conffile);
86
87 #ifndef RADIUS_CONFIG
88 #define RADIUS_CONFIG "radiusd.conf"
89 #endif
90
91 /*
92  *      Isolate the scary casts in these tiny provably-safe functions
93  */
94 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
95 {
96         if (ci == NULL)
97                 return NULL;
98         assert(ci->type == CONF_ITEM_PAIR);
99         return (CONF_PAIR *)ci;
100 }
101 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
102 {
103         if (ci == NULL)
104                 return NULL;
105         assert(ci->type == CONF_ITEM_SECTION);
106         return (CONF_SECTION *)ci;
107 }
108 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
109 {
110         if (cp == NULL)
111                 return NULL;
112         return (CONF_ITEM *)cp;
113 }
114 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
115 {
116         if (cs == NULL)
117                 return NULL;
118         return (CONF_ITEM *)cs;
119 }
120
121 /*
122  *      Create a new CONF_PAIR
123  */
124 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
125                 int operator, CONF_SECTION *parent)
126 {
127         CONF_PAIR       *cp;
128
129         cp = (CONF_PAIR *)rad_malloc(sizeof(CONF_PAIR));
130         memset(cp, 0, sizeof(CONF_PAIR));
131         cp->item.type = CONF_ITEM_PAIR;
132         cp->item.parent = parent;
133         cp->attr = xstrdup(attr);
134         cp->value = xstrdup(value);
135         cp->operator = operator;
136
137         return cp;
138 }
139
140 /*
141  *      Free a CONF_PAIR
142  */
143 void cf_pair_free(CONF_PAIR **cp)
144 {
145         if (!cp || !*cp) return;
146
147         if ((*cp)->attr)
148                 free((*cp)->attr);
149         if ((*cp)->value)
150                 free((*cp)->value);
151
152 #ifndef NDEBUG
153         memset(*cp, 0, sizeof(*cp));
154 #endif
155         free(*cp);
156
157         *cp = NULL;
158 }
159
160 /*
161  *      Allocate a CONF_SECTION
162  */
163 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
164                 CONF_SECTION *parent)
165 {
166         CONF_SECTION    *cs;
167
168         if (name1 == NULL || !name1[0]) 
169                 name1 = "main";
170
171         cs = (CONF_SECTION *)rad_malloc(sizeof(CONF_SECTION));
172         memset(cs, 0, sizeof(CONF_SECTION));
173         cs->item.type = CONF_ITEM_SECTION;
174         cs->item.parent = parent;
175         cs->name1 = xstrdup(name1);
176         cs->name2 = (name2 && *name2) ? xstrdup(name2) : NULL;
177
178         return cs;
179 }
180
181 /*
182  *      Free a CONF_SECTION
183  */
184 void cf_section_free(CONF_SECTION **cs)
185 {
186         CONF_ITEM       *ci, *next;
187
188         if (!cs || !*cs) return;
189
190         for (ci = (*cs)->children; ci; ci = next) {
191                 next = ci->next;
192                 if (ci->type==CONF_ITEM_PAIR) {
193                         CONF_PAIR *pair = cf_itemtopair(ci);
194                         cf_pair_free(&pair);
195                 } else {
196                         CONF_SECTION *section = cf_itemtosection(ci);
197                         cf_section_free(&section);
198                 }
199         }
200
201         if ((*cs)->name1) free((*cs)->name1);
202         if ((*cs)->name2) free((*cs)->name2);
203
204         /*
205          * And free the section
206          */
207 #ifndef NDEBUG
208         memset(*cs, 0, sizeof(*cs));
209 #endif
210         free(*cs);
211
212         *cs = NULL;
213 }
214
215 /*
216  *      Add an item to a configuration section.
217  */
218 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci_new)
219 {
220         CONF_ITEM *ci;
221         
222         for (ci = cs->children; ci && ci->next; ci = ci->next)
223                 ;
224
225         if (ci == NULL)
226                 cs->children = ci_new;
227         else
228                 ci->next = ci_new;
229 }
230
231 /*
232  *      Expand the variables in an input string.
233  */
234 static const char *cf_expand_variables(const char *cf, int *lineno,
235                                         CONF_SECTION *cs,
236                                         char *output, const char *input)
237 {
238         char            *p;
239         const char      *end, *ptr;
240         char            name[1024];
241         CONF_PAIR       *cpn;
242         CONF_SECTION    *outercs;
243
244         p = output;
245         ptr = input;
246         while (*ptr >= ' ') {
247                 /*
248                  *      Ignore anything other than "${"
249                  */
250                 if ((*ptr != '$') || (ptr[1] != '{')) {
251                         *(p++) = *(ptr++);
252                         continue;
253                 }
254                 
255                 /*
256                  *      Look for trailing '}', and log a
257                  *      warning for anything that doesn't match,
258                  *      and exit with a fatal error.
259                  */
260                 end = strchr(ptr, '}');
261                 if (end == NULL) {
262                         *(p++) = *(ptr++);
263                         radlog(L_INFO, "%s[%d]: Variable expansion missing }",
264                                         cf, *lineno);
265                         return NULL;
266                 }
267                 
268                 ptr += 2;
269
270                 memcpy(name, ptr, end - ptr);
271                 name[end - ptr] = '\0';
272
273                 cpn = cf_pair_find(cs, name);
274                 
275                 /*
276                  *      Also look recursively up the section tree,
277                  *      so things like ${confdir} can be defined
278                  *      there and used inside the module config
279                  *      sections.
280                  */
281                 for (outercs=cs->item.parent; 
282                                 (cpn == NULL) && (outercs != NULL);
283                                 outercs=outercs->item.parent) {
284                         cpn = cf_pair_find(outercs, name);
285                 }
286                 if (!cpn) {
287                         radlog(L_ERR, "%s[%d]: Unknown variable \"%s\"",
288                                         cf, *lineno, name);
289                         return NULL;
290                 }
291                 
292                 /*
293                  *  Substitute the value of the variable.
294                  */
295                 strcpy(p, cpn->value);
296                 p += strlen(p);
297                 ptr = end + 1;
298         } /* loop over all of the input string. */
299
300         *p = '\0';
301
302         return output;
303 }
304
305 /*
306  *      Parse a configuration section into user-supplied variables.
307  */
308 int cf_section_parse(CONF_SECTION *cs, const CONF_PARSER *variables)
309 {
310         int             i;
311         int             rcode;
312         char            **q;
313         CONF_PAIR       *cp;
314         CONF_SECTION    *subsection;
315         uint32_t        ipaddr;
316         char            buffer[1024];
317         const char      *value;
318
319         /*
320          *      Handle the user-supplied variables.
321          */
322         for (i = 0; variables[i].name != NULL; i++) {
323                 value = variables[i].dflt;
324
325                 cp = cf_pair_find(cs, variables[i].name);
326                 if (cp) {
327                         value = cp->value;
328                 }
329                 
330                 switch (variables[i].type)
331                 {
332                 case PW_TYPE_SUBSECTION:
333                         subsection = cf_section_sub_find(cs,variables[i].name);
334
335                         /*
336                          *      If the configuration section is NOT there,
337                          *      then ignore it.
338                          *
339                          *      FIXME! This is probably wrong... we should
340                          *      probably set the items to their default values.
341                          */
342                         if (!subsection) {
343                                 break;
344                         }
345
346                         rcode = cf_section_parse(subsection,
347                                                  (CONF_PARSER *) variables[i].data);
348                         if (rcode < 0) {
349                                 return -1;
350                         }
351                         break;
352
353                 case PW_TYPE_BOOLEAN:
354                         /*
355                          *      Allow yes/no and on/off
356                          */
357                         if ((strcasecmp(value, "yes") == 0) ||
358                                         (strcasecmp(value, "on") == 0)) {
359                                 *(int *)variables[i].data = 1;
360                         } else if ((strcasecmp(value, "no") == 0) ||
361                                                 (strcasecmp(value, "off") == 0)) {
362                                 *(int *)variables[i].data = 0;
363                         } else {
364                                 *(int *)variables[i].data = 0;
365                                 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, variables[i].name);
366                                 return -1;
367                         }
368                         DEBUG2(" %s: %s = %s",
369                                 cs->name1,
370                                 variables[i].name,
371                                 value);
372                         break;
373
374                 case PW_TYPE_INTEGER:
375                         *(int *)variables[i].data = strtol(value, 0, 0);
376                         DEBUG2(" %s: %s = %d",
377                                 cs->name1,
378                                 variables[i].name,
379                                 *(int *)variables[i].data);
380                         break;
381                         
382                 case PW_TYPE_STRING_PTR:
383                         q = (char **) variables[i].data;
384                         if (*q != NULL) {
385                                 free(*q);
386                         }
387
388                         /*
389                          *      Expand variables while parsing,
390                          *      but ONLY expand ones which haven't already
391                          *      been expanded.
392                          */
393                         if (value && (value == variables[i].dflt)) {
394                                 value = cf_expand_variables(NULL, 0, cs, buffer,value);
395                                 if (!value) {
396                                         return -1;
397                                 }
398                         }
399
400                         DEBUG2(" %s: %s = \"%s\"",
401                                 cs->name1,
402                                 variables[i].name,
403                                 value ? value : "(null)");
404                         *q = value ? strdup(value) : NULL;
405                         break;
406
407                 case PW_TYPE_IPADDR:
408                         /*
409                          *      Allow '*' as any address
410                          */
411                         if (strcmp(value, "*") == 0) {
412                                 *(uint32_t *) variables[i].data = 0;
413                                 break;
414                         }
415                         ipaddr = ip_getaddr(value);
416                         if (ipaddr == 0) {
417                                 radlog(L_ERR, "Can't find IP address for host %s", value);
418                                 return -1;
419                         }
420                         DEBUG2(" %s: %s = %s IP address [%s]",
421                                 cs->name1,
422                                 variables[i].name,
423                                 value, ip_ntoa(buffer, ipaddr));
424                         *(uint32_t *) variables[i].data = ipaddr;
425                         break;
426                         
427                 default:
428                         radlog(L_ERR, "type %d not supported yet", variables[i].type);
429                         return -1;
430                         break;
431                 } /* switch over variable type */
432         } /* for all variables in the configuration section */
433         
434         return 0;
435 }
436
437 /*
438  *      Read a part of the config file.
439  */
440 static CONF_SECTION *cf_section_read(const char *cf, int *lineno, FILE *fp,
441                                         const char *name1, const char *name2,
442                                         CONF_SECTION *parent)
443 {
444         CONF_SECTION    *cs, *css;
445         CONF_PAIR       *cpn;
446         char            *ptr;
447         const char      *value;
448         char            buf[8192];
449         char            buf1[1024];
450         char            buf2[1024];
451         char            buf3[1024];
452         int             t1, t2, t3;
453         
454         /*
455          *      Ensure that the user can't add CONF_SECTIONs
456          *      with 'internal' names;
457          */
458         if ((name1 != NULL) && (name1[0] == '_')) {
459                 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
460                         cf, *lineno);
461                 return NULL;
462         }
463
464         /*
465          *      Allocate new section.
466          */
467         cs = cf_section_alloc(name1, name2, parent);
468         cs->item.lineno = *lineno;
469
470         /*
471          *      Read.
472          */
473         while (fgets(buf, sizeof(buf), fp) != NULL) {
474                 (*lineno)++;
475                 ptr = buf;
476
477                 t1 = gettoken(&ptr, buf1, sizeof(buf1));
478
479                 /*
480                  *      Skip comments and blank lines immediately.
481                  */
482                 if ((*buf1 == '#') || (*buf1 == '\0')) {
483                         continue;
484                 }
485
486                 /*
487                  *      Allow for $INCLUDE files
488                  *
489                  *      Currently this allows for includes only at the top 
490                  *      level of config.  IE you cannot have an $INCLUDE nested
491                  *      inside section.  -cparker
492                  */
493                 if ((strcasecmp(buf1, "$INCLUDE") == 0) &&
494                         (name1 == NULL) && (name2 == NULL)) {
495
496                         CONF_SECTION      *is;
497
498                         t2 = getword(&ptr, buf2, sizeof(buf2));
499
500                         value = cf_expand_variables(cf, lineno, cs, buf, buf2);
501                         if (!value) {
502                                 cf_section_free(&cs);
503                                 return NULL;
504                         }
505
506                         DEBUG2( "Config:   including file: %s", value );
507
508                         if ((is = conf_read(cf, *lineno, value)) == NULL) {
509                                 cf_section_free(&cs);
510                                 return NULL;
511                         }
512
513                         /*
514                          *      Add the included conf to our CONF_SECTION
515                          */
516                         if (is != NULL) {
517                                 if (is->children != NULL) {
518                                         CONF_ITEM *ci;
519                          
520                                         /*
521                                          *      Re-write the parent of the
522                                          *      moved children to be the
523                                          *      upper-layer section.
524                                          */
525                                         for (ci = is->children; ci; ci = ci->next) {
526                                                 ci->parent = cs;
527                                         }
528
529                                         /*
530                                          *      If there are children, then
531                                          *      move them up a layer.
532                                          */
533                                         if (is->children) {
534                                                 cf_item_add(cs, is->children);
535                                         }
536                                         is->children = NULL;
537                                 }
538                                 /*
539                                  *      Always free the section for the
540                                  *      $INCLUDEd file.
541                                  */
542                                 cf_section_free(&is);
543                         }
544
545                         continue;
546                 }
547
548                 /*
549                  *      No '=': must be a section or sub-section.
550                  */
551                 if (strchr(ptr, '=') == NULL) {
552                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
553                         t3 = gettoken(&ptr, buf3, sizeof(buf3));
554                 } else {
555                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
556                         t3 = getword(&ptr, buf3, sizeof(buf3));
557                 }
558
559                 /*
560                  *      See if it's the end of a section.
561                  */
562                 if (t1 == T_RCBRACE) {
563                         if (name1 == NULL || buf2[0]) {
564                                 radlog(L_ERR, "%s[%d]: Unexpected end of section",
565                                                 cf, *lineno);
566                                 cf_section_free(&cs);
567                                 return NULL;
568                         }
569                         return cs;
570                 }
571
572                 /*
573                  * Perhaps a subsection.
574                  */
575                 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
576                         css = cf_section_read(cf, lineno, fp, buf1,
577                                         t2==T_LCBRACE ? NULL : buf2, cs);
578                         if (css == NULL) {
579                                 cf_section_free(&cs);
580                                 return NULL;
581                         }
582                         cf_item_add(cs, cf_sectiontoitem(css));
583
584                         continue;               
585                 }
586
587                 /*
588                  *      Ignore semi-colons.
589                  */
590                 if (*buf2 == ';') *buf2 = '\0';
591
592                 /*
593                  *      Must be a normal attr = value line.
594                  */
595                 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
596                         t2 = T_OP_EQ;
597                 } else if (buf1[0] == 0 || buf2[0] == 0 || buf3[0] == 0 ||
598                                 (t2 < T_EQSTART || t2 > T_EQEND)) {
599                         radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
600                                 cf, *lineno);
601                         cf_section_free(&cs);
602                         return NULL;
603                 }
604
605                 /*
606                  *      Ensure that the user can't add CONF_PAIRs
607                  *      with 'internal' names;
608                  */
609                 if (buf1[0] == '_') {
610                         radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
611                                 cf, *lineno, buf1);
612                         cf_section_free(&cs);
613                         return NULL;
614                 }
615                 
616                 /*
617                  *      Handle variable substitution via ${foo}
618                  */
619                 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
620                 if (!value) {
621                         cf_section_free(&cs);
622                         return NULL;
623                 }
624
625
626                 /*
627                  *      Add this CONF_PAIR to our CONF_SECTION
628                  */
629                 cpn = cf_pair_alloc(buf1, value, t2, parent);
630                 cpn->item.lineno = *lineno;
631                 cf_item_add(cs, cf_pairtoitem(cpn));
632         }
633
634         /*
635          *      See if EOF was unexpected ..
636          */
637         if (name1 != NULL) {
638                 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
639                 cf_section_free(&cs);
640                 return NULL;
641         }
642
643         return cs;
644 }
645
646 /*
647  *      Read the config file.
648  */
649 static CONF_SECTION *conf_read(const char *fromfile, int fromline, const char *conffile)
650 {
651         FILE            *fp;
652         int             lineno = 0;
653         CONF_SECTION    *cs;
654         
655         if ((fp = fopen(conffile, "r")) == NULL) {
656                 if (fromfile) {
657                         radlog(L_ERR|L_CONS, "%s[%d]: Unable to open file \"%s\": %s",
658                                 fromfile, fromline, conffile, strerror(errno));
659                 } else {
660                         radlog(L_ERR|L_CONS, "Unable to open file \"%s\": %s",
661                                 conffile, strerror(errno));
662                 }
663                 return NULL;
664         }
665
666         cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, NULL);
667         fclose(fp);
668
669         return cs;
670 }
671
672 /*
673  *      These are not used anywhere else..
674  */
675 static const char *localstatedir = NULL;
676 static const char *prefix = NULL;
677
678 static CONF_PARSER directory_config[] = {
679   /*
680    *    FIXME: 'prefix' is the ONLY one which should be configured
681    *    at compile time.  Hard-coding it here is bad.  It will be cleaned
682    *    up once we clean up the hard-coded defines for the locations of
683    *    the various files.
684    */
685   {  "prefix",            PW_TYPE_STRING_PTR, &prefix,            "/usr/local"},
686   { "localstatedir",      PW_TYPE_STRING_PTR, &localstatedir,     "${prefix}/var"}, 
687   { "logdir",             PW_TYPE_STRING_PTR, &radlog_dir,        "${localstatedir}/log"},
688   { "libdir",             PW_TYPE_STRING_PTR, &radlib_dir,        "${prefix}/lib"},
689   { "radacctdir",         PW_TYPE_STRING_PTR, &radacct_dir,       "${logdir}/radacct" },
690   { "hostname_lookups",   PW_TYPE_BOOLEAN,    &librad_dodns,      "no" },
691
692   /*
693    *    We don't allow re-defining this, as doing so will cause
694    *    all sorts of confusion.
695    */
696 #if 0
697   { "confdir",            PW_TYPE_STRING_PTR, &radius_dir,        RADIUS_DIR },
698 #endif
699   { NULL, -1, NULL, NULL }
700 };
701
702
703 /* JLN
704  * Read the configuration and library
705  * This uses the new kind of configuration file as defined by
706  * Miquel at http://www.miquels.cistron.nl/radius/
707  */
708
709 int read_radius_conf_file(void)
710 {
711         char buffer[256];
712         CONF_SECTION *cs;
713
714         /* Lets go look for the new configuration files */
715         sprintf(buffer, "%.200s/%.50s", radius_dir, RADIUS_CONFIG);
716         if ((cs = conf_read(NULL, 0, buffer)) == NULL) {
717                 return -1;
718         }
719
720         /*
721          *      Free the old configuration data, and replace it
722          *      with the new one.
723          */
724         cf_section_free(&config);
725         config = cs;
726         
727         /*
728          *      And parse the directory configuration values.
729          */
730         cs = cf_section_find(NULL);
731         if (!cs)
732                 return -1;
733
734         /*
735          *      This allows us to figure out where, relative to
736          *      radiusd.conf, the other configuration files exist.
737          */
738         cf_section_parse(cs, directory_config);
739
740         /* Initialize the dictionary */
741         DEBUG2("read_config_files:  reading dictionary");
742         if (dict_init(radius_dir, RADIUS_DICTIONARY) != 0) {
743                 radlog(L_ERR|L_CONS, "Errors reading dictionary: %s",
744                         librad_errstr);
745                 return -1;
746         }
747
748         /* old-style clients file */
749         sprintf(buffer, "%.200s/%.50s", radius_dir, RADIUS_CLIENTS);
750         DEBUG2("read_config_files:  reading clients");
751         if (read_clients_file(buffer) < 0) {
752                 radlog(L_ERR|L_CONS, "Errors reading clients");
753                 return -1;
754         }
755
756         /*
757          *      Add to that, the *new* list of clients.
758          */
759         sprintf(buffer, "%.200s/%.50s", radius_dir, RADIUS_CONFIG);
760         if (generate_clients(buffer) < 0) {
761                 return -1;
762         }
763
764         /* old-style realms file */
765         sprintf(buffer, "%.200s/%.50s", radius_dir, RADIUS_REALMS);
766         DEBUG2("read_config_files:  reading realms");
767         if (read_realms_file(buffer) < 0) {
768                 radlog(L_ERR|L_CONS, "Errors reading realms");
769                 return -1;
770         }
771
772         /*
773          *      If there isn't any realms it isn't fatal..
774          */
775         sprintf(buffer, "%.200s/%.50s", radius_dir, RADIUS_CONFIG);
776         if (generate_realms(buffer) < 0) {
777                 return -1;
778         }
779
780         /* old-style naslist file */
781         sprintf(buffer, "%.200s/%.50s", radius_dir, RADIUS_NASLIST);
782         DEBUG2("read_config_files:  reading naslist");
783         if (read_naslist_file(buffer) < 0) {
784                 radlog(L_ERR|L_CONS, "Errors reading naslist");
785                 return -1;
786         }
787
788         return 0;       
789 }
790
791 /* JLN
792  * Create the linked list of realms from the new configuration type
793  * This way we don't have to change to much in the other source-files
794  */
795
796 static int generate_realms(const char *filename)
797 {
798         CONF_SECTION    *cs;
799         REALM           *c;
800         char            *s, *authhost, *accthost;
801
802         for (cs = cf_subsection_find_next(config, NULL, "realm"); cs != NULL;
803                         cs = cf_subsection_find_next(config, cs, "realm")) {
804                 if (!cs->name2) {
805                         radlog(L_CONS|L_ERR, "%s[%d]: Missing realm name", filename, cs->item.lineno);
806                         return -1;
807                 }
808                 /*
809                  * We've found a realm, allocate space for it
810                  */
811                 c = rad_malloc(sizeof(REALM));
812                 memset(c, 0, sizeof(REALM));
813                 /*
814                  * An authhost must exist in the configuration
815                  */
816                 if ((authhost = cf_section_value_find(cs, "authhost")) == NULL) {
817                         radlog(L_CONS|L_ERR, 
818                                 "%s[%d]: No authhost entry in realm", 
819                                 filename, cs->item.lineno);
820                         return -1;
821                 }
822                 if ((s = strchr(authhost, ':')) != NULL) {
823                         *s++ = 0;
824                         c->auth_port = atoi(s);
825                 } else {
826                         c->auth_port = auth_port;
827                 }
828                 accthost = cf_section_value_find(cs, "accthost");
829                 if ((s =strchr(accthost, ':')) != NULL) {
830                         *s++ = 0;
831                         c->acct_port = atoi(s); 
832                 } else {
833                         c->acct_port = acct_port;
834                 }
835                 if (strcmp(authhost, "LOCAL") != 0)
836                         c->ipaddr = ip_getaddr(authhost);
837
838                 /* 
839                  * Double check length, just to be sure!
840                  */
841                 if (strlen(authhost) >= sizeof(c->server)) {
842                         radlog(L_ERR, "%s[%d]: Server name of length %d is greater that allowed: %d",
843                                         filename, cs->item.lineno,
844                                         strlen(authhost), sizeof(c->server) - 1);
845                         return -1;
846                 }
847                 if (strlen(cs->name2) >= sizeof(c->realm)) {
848                         radlog(L_ERR, "%s[%d]: Realm name of length %d is greater than allowed %d",
849                                         filename, cs->item.lineno,
850                                         strlen(cs->name2), sizeof(c->server) - 1);
851                         return -1;
852                 }
853                 
854                 strcpy(c->realm, cs->name2);
855                 strcpy(c->server, authhost);    
856
857                 s = cf_section_value_find(cs, "secret");
858                 if (s == NULL) {
859                         radlog(L_ERR, "%s[%d]: No shared secret supplied for realm",
860                                         filename, cs->item.lineno);
861                         return -1;
862                 }
863
864                 if (strlen(s) >= sizeof(c->secret)) {
865                         radlog(L_ERR, "%s[%d]: Secret of length %d is greater than the allowed maximum of %d.",
866                                         filename, cs->item.lineno,
867                                         strlen(s), sizeof(c->secret) - 1);
868                         return -1;
869                 }
870                 strNcpy((char *)c->secret, s, sizeof(c->secret));
871
872                 c->striprealm = 1;
873                 
874                 if ((cf_section_value_find(cs, "nostrip")) != NULL)
875                         c->striprealm = 0;
876                 if ((cf_section_value_find(cs, "noacct")) != NULL)
877                         c->acct_port = 0;
878                 if ((cf_section_value_find(cs, "trusted")) != NULL)
879                         c->trusted = 1;
880                 if ((cf_section_value_find(cs, "notrealm")) != NULL)
881                         c->notrealm = 1;
882                 if ((cf_section_value_find(cs, "notsuffix")) != NULL)
883                         c->notrealm = 1;
884
885
886                 c->next = realms;
887                 realms = c;
888
889         }
890
891         return 0;
892 }
893
894 /* JLN
895  * Create the linked list of realms from the new configuration type
896  * This way we don't have to change to much in the other source-files
897  */
898 static int generate_clients(const char *filename)
899 {
900         CONF_SECTION    *cs;
901         RADCLIENT       *c;
902         char            *hostnm, *secret, *shortnm, *netmask;
903
904         for (cs = cf_subsection_find_next(config, NULL, "client"); cs != NULL; 
905                         cs = cf_subsection_find_next(config, cs, "client")) {
906                 if (!cs->name2) {
907                         radlog(L_CONS|L_ERR, "%s[%d]: Missing client name", filename, cs->item.lineno);
908                         return -1;
909                 }
910                 /*
911                  * Check the lengths, we don't want any core dumps
912                  */
913                 hostnm = cs->name2;
914                 secret = cf_section_value_find(cs, "secret");
915                 shortnm = cf_section_value_find(cs, "shortname");
916                 netmask = strchr(hostnm, '/');
917
918                 if (strlen(secret) >= sizeof(c->secret)) {
919                         radlog(L_ERR, "%s[%d]: Secret of length %d is greater than the allowed maximum of %d.",
920                                 filename, cs->item.lineno,
921                                 strlen(secret), sizeof(c->secret) - 1);
922                         return -1;
923                 }
924                 if (strlen(shortnm) > sizeof(c->shortname)) {
925                         radlog(L_ERR, "%s[%d]: Client short name of length %d is greater than the allowed maximum of %d.",
926                                         filename, cs->item.lineno,
927                                         strlen(shortnm), sizeof(c->shortname) - 1);
928                         return -1;
929                 }
930                 /*
931                  * The size is fine.. Let's create the buffer
932                  */
933                 c = rad_malloc(sizeof(RADCLIENT));
934
935                 /*
936                  *      Look for netmasks.
937                  */
938                 c->netmask = ~0;
939                 if (netmask) {
940                         int i, mask_length;
941
942                         mask_length = atoi(netmask + 1);
943                         if ((mask_length <= 0) || (mask_length > 32)) {
944                                 radlog(L_ERR, "%s[%d]: Invalid value '%s' for IP network mask.",
945                                                 filename, cs->item.lineno, netmask + 1);
946                                 return -1;
947                         }
948                         
949                         c->netmask = (1 << 31);
950                         for (i = 1; i < mask_length; i++) {
951                                 c->netmask |= (c->netmask >> 1);
952                         }
953
954                         *netmask = '\0';
955                         c->netmask = htonl(c->netmask);
956                 }
957
958                 c->ipaddr = ip_getaddr(hostnm);
959                 if (c->ipaddr == INADDR_NONE) {
960                         radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s",
961                                         filename, cs->item.lineno, hostnm);
962                         return -1;
963                 }
964
965                 /*
966                  *      Update the client name again...
967                  */
968                 if (netmask) {
969                         *netmask = '/';
970                         c->ipaddr &= c->netmask;
971                         strcpy(c->longname, hostnm);
972                 } else {
973                         ip_hostname(c->longname, sizeof(c->longname),
974                                         c->ipaddr);
975                 }
976
977                 strcpy((char *)c->secret, secret);
978                 strcpy(c->shortname, shortnm);
979
980                 c->next = clients;
981                 clients = c;
982         }
983
984         return 0;
985 }
986
987 /* 
988  * Return a CONF_PAIR within a CONF_SECTION.
989  */
990
991 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
992 {
993         CONF_ITEM       *ci;
994
995         if (section == NULL) {
996           section = config;
997         }
998
999         for (ci = section->children; ci; ci = ci->next) {
1000                 if (ci->type != CONF_ITEM_PAIR)
1001                         continue;
1002                 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
1003                         break;
1004         }
1005
1006         return cf_itemtopair(ci);
1007 }
1008
1009 /*
1010  * Return the attr of a CONF_PAIR
1011  */
1012
1013 char *cf_pair_attr(CONF_PAIR *pair)
1014 {
1015         return (pair ? pair->attr : NULL);
1016 }
1017
1018 /*
1019  * Return the value of a CONF_PAIR
1020  */
1021
1022 char *cf_pair_value(CONF_PAIR *pair)
1023 {
1024         return (pair ? pair->value : NULL);
1025 }
1026
1027 /*
1028  * Return the first label of a CONF_SECTION
1029  */
1030
1031 char *cf_section_name1(CONF_SECTION *section)
1032 {
1033         return (section ? section->name1 : NULL);
1034 }
1035
1036 /*
1037  * Return the second label of a CONF_SECTION
1038  */
1039
1040 char *cf_section_name2(CONF_SECTION *section)
1041 {
1042         return (section ? section->name2 : NULL);
1043 }
1044
1045 /* 
1046  * Find a value in a CONF_SECTION
1047  */
1048 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
1049 {
1050         CONF_PAIR       *cp;
1051
1052         cp = cf_pair_find(section, attr);
1053
1054         return (cp ? cp->value : NULL);
1055 }
1056
1057 /*
1058  * Return the next pair after a CONF_PAIR
1059  * with a certain name (char *attr) If the requested
1060  * attr is NULL, any attr matches.
1061  */
1062
1063 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
1064 {
1065         CONF_ITEM       *ci;
1066
1067         /*
1068          * If pair is NULL this must be a first time run
1069          * Find the pair with correct name
1070          */
1071
1072         if (pair == NULL){
1073                 return cf_pair_find(section, attr);
1074         }
1075
1076         ci = cf_pairtoitem(pair)->next;
1077
1078         for (; ci; ci = ci->next) {
1079                 if (ci->type != CONF_ITEM_PAIR)
1080                         continue;
1081                 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
1082                         break;
1083         }
1084
1085         return cf_itemtopair(ci);
1086 }
1087
1088 /*
1089  * Find a CONF_SECTION, or return the root if name is NULL
1090  */
1091
1092 CONF_SECTION *cf_section_find(const char *name)
1093 {
1094         if (name)
1095                 return cf_section_sub_find(config, name);
1096         else
1097                 return config;
1098 }
1099
1100 /*
1101  * Find a sub-section in a section
1102  */
1103
1104 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
1105 {
1106         CONF_ITEM *ci;
1107
1108         for (ci = section->children; ci; ci = ci->next) {
1109                 if (ci->type != CONF_ITEM_SECTION)
1110                         continue;
1111                 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
1112                         break;
1113         }
1114
1115         return cf_itemtosection(ci);
1116
1117 }
1118
1119 /*
1120  * Return the next subsection after a CONF_SECTION
1121  * with a certain name1 (char *name1). If the requested
1122  * name1 is NULL, any name1 matches.
1123  */
1124
1125 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
1126                 CONF_SECTION *subsection,
1127                 const char *name1)
1128 {
1129         CONF_ITEM       *ci;
1130
1131         /*
1132          * If subsection is NULL this must be a first time run
1133          * Find the subsection with correct name
1134          */
1135
1136         if (subsection == NULL){
1137                 ci = section->children;
1138         } else {
1139                 ci = cf_sectiontoitem(subsection)->next;
1140         }
1141
1142         for (; ci; ci = ci->next) {
1143                 if (ci->type != CONF_ITEM_SECTION)
1144                         continue;
1145                 if ((name1 == NULL) || 
1146                                 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1147                         break;
1148         }
1149
1150         return cf_itemtosection(ci);
1151 }
1152
1153 /*
1154  * Return the next item after a CONF_ITEM.
1155  */
1156
1157 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1158 {
1159         /*
1160          * If item is NULL this must be a first time run
1161          * Return the first item
1162          */
1163
1164         if (item == NULL) {
1165                 return section->children;
1166         } else {
1167                 return item->next;
1168         }
1169 }
1170
1171 int cf_section_lineno(CONF_SECTION *section)
1172 {
1173         return cf_sectiontoitem(section)->lineno;
1174 }
1175
1176 int cf_pair_lineno(CONF_PAIR *pair)
1177 {
1178         return cf_pairtoitem(pair)->lineno;
1179 }
1180
1181 int cf_item_is_section(CONF_ITEM *item)
1182 {
1183         return item->type == CONF_ITEM_SECTION;
1184 }
1185
1186
1187 /* 
1188  * JMG dump_config tries to dump the config structure in a readable format
1189  * 
1190 */
1191
1192 static int dump_config_section(CONF_SECTION *cs, int indent)
1193 {
1194         CONF_SECTION    *scs;
1195         CONF_PAIR       *cp;
1196         CONF_ITEM       *ci;
1197
1198         /* The DEBUG macro doesn't let me
1199          *   for(i=0;i<indent;++i) debugputchar('\t');
1200          * so I had to get creative. --Pac. */
1201
1202         for (ci = cs->children; ci; ci = ci->next) {
1203                 if (ci->type == CONF_ITEM_PAIR) {
1204                         cp=cf_itemtopair(ci);
1205                         DEBUG("%.*s%s = %s",
1206                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1207                                 cp->attr, cp->value);
1208                 } else {
1209                         scs=cf_itemtosection(ci);
1210                         DEBUG("%.*s%s %s%s{",
1211                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1212                                 scs->name1,
1213                                 scs->name2 ? scs->name2 : "",
1214                                 scs->name2 ?  " " : "");
1215                         dump_config_section(scs, indent+1);
1216                         DEBUG("%.*s}",
1217                                 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1218                 }
1219         }
1220
1221         return 0;
1222 }
1223
1224 int dump_config(void)
1225 {
1226         return dump_config_section(config, 0);
1227 }