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