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