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