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