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