Don't start if the config files are globally readable or writable.
[freeradius.git] / src / main / conffile.c
1 /*
2  * conffile.c   Read the radiusd.conf file.
3  *
4  *              Yep I should learn to use lex & yacc, or at least
5  *              write a decent parser. I know how to do that, really :)
6  *              miquels@cistron.nl
7  *
8  * Version:     $Id$
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  * Copyright 2000  The FreeRADIUS server project
25  * Copyright 2000  Miquel van Smoorenburg <miquels@cistron.nl>
26  * Copyright 2000  Alan DeKok <aland@ox.org>
27  */
28
29 #include "autoconf.h"
30 #include "libradius.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #ifdef HAVE_NETINET_IN_H
36 #       include <netinet/in.h>
37 #endif
38
39 #ifdef HAVE_SYS_STAT_H
40 #include        <sys/stat.h>
41 #endif
42
43 #include "radiusd.h"
44 #include "rad_assert.h"
45 #include "conffile.h"
46 #include "token.h"
47 #include "modules.h"
48
49 static const char rcsid[] =
50 "$Id$";
51
52 #define xstrdup strdup
53
54 typedef enum conf_type {
55         CONF_ITEM_PAIR,
56         CONF_ITEM_SECTION
57 } CONF_ITEM_TYPE;
58
59 struct conf_item {
60         struct conf_item *next;
61         struct conf_part *parent;
62         int lineno;
63         CONF_ITEM_TYPE type;
64 };
65 struct conf_pair {
66         CONF_ITEM item;
67         char *attr;
68         char *value;
69         LRAD_TOKEN operator;
70 };
71 struct conf_part {
72         CONF_ITEM item;
73         char *name1;
74         char *name2;
75         struct conf_item *children;
76 };
77
78 /*
79  *      Isolate the scary casts in these tiny provably-safe functions
80  */
81 CONF_PAIR *cf_itemtopair(CONF_ITEM *ci)
82 {
83         if (ci == NULL)
84                 return NULL;
85         rad_assert(ci->type == CONF_ITEM_PAIR);
86         return (CONF_PAIR *)ci;
87 }
88 CONF_SECTION *cf_itemtosection(CONF_ITEM *ci)
89 {
90         if (ci == NULL)
91                 return NULL;
92         rad_assert(ci->type == CONF_ITEM_SECTION);
93         return (CONF_SECTION *)ci;
94 }
95 CONF_ITEM *cf_pairtoitem(CONF_PAIR *cp)
96 {
97         if (cp == NULL)
98                 return NULL;
99         return (CONF_ITEM *)cp;
100 }
101 CONF_ITEM *cf_sectiontoitem(CONF_SECTION *cs)
102 {
103         if (cs == NULL)
104                 return NULL;
105         return (CONF_ITEM *)cs;
106 }
107
108 /*
109  *      Create a new CONF_PAIR
110  */
111 static CONF_PAIR *cf_pair_alloc(const char *attr, const char *value,
112                 LRAD_TOKEN operator, CONF_SECTION *parent)
113 {
114         CONF_PAIR *cp;
115
116         cp = (CONF_PAIR *)rad_malloc(sizeof(CONF_PAIR));
117         memset(cp, 0, sizeof(CONF_PAIR));
118         cp->item.type = CONF_ITEM_PAIR;
119         cp->item.parent = parent;
120         cp->attr = xstrdup(attr);
121         cp->value = xstrdup(value);
122         cp->operator = operator;
123
124         return cp;
125 }
126
127 /*
128  *      Free a CONF_PAIR
129  */
130 void cf_pair_free(CONF_PAIR **cp)
131 {
132         if (!cp || !*cp) return;
133
134         if ((*cp)->attr)
135                 free((*cp)->attr);
136         if ((*cp)->value)
137                 free((*cp)->value);
138
139 #ifndef NDEBUG
140         memset(*cp, 0, sizeof(*cp));
141 #endif
142         free(*cp);
143
144         *cp = NULL;
145 }
146
147 /*
148  *      Allocate a CONF_SECTION
149  */
150 static CONF_SECTION *cf_section_alloc(const char *name1, const char *name2,
151                                       CONF_SECTION *parent, const char *cf)
152 {
153         CONF_SECTION    *cs;
154
155         if (name1 == NULL || !name1[0])
156                 name1 = "main";
157
158         cs = (CONF_SECTION *)rad_malloc(sizeof(CONF_SECTION));
159         memset(cs, 0, sizeof(CONF_SECTION));
160         cs->item.type = CONF_ITEM_SECTION;
161         cs->item.parent = parent;
162         cs->name1 = strdup(name1);
163         cs->name2 = (name2 && *name2) ? xstrdup(name2) : NULL;
164
165         return cs;
166 }
167
168 /*
169  *      Free a CONF_SECTION
170  */
171 void cf_section_free(CONF_SECTION **cs)
172 {
173         CONF_ITEM       *ci, *next;
174
175         if (!cs || !*cs) return;
176
177         for (ci = (*cs)->children; ci; ci = next) {
178                 next = ci->next;
179                 if (ci->type==CONF_ITEM_PAIR) {
180                         CONF_PAIR *pair = cf_itemtopair(ci);
181                         cf_pair_free(&pair);
182                 } else {
183                         CONF_SECTION *section = cf_itemtosection(ci);
184                         cf_section_free(&section);
185                 }
186         }
187
188         if ((*cs)->name1)
189                 free((*cs)->name1);
190         if ((*cs)->name2)
191                 free((*cs)->name2);
192
193         /*
194          * And free the section
195          */
196 #ifndef NDEBUG
197         memset(*cs, 0, sizeof(*cs));
198 #endif
199         free(*cs);
200
201         *cs = NULL;
202 }
203
204 /*
205  *      Add an item to a configuration section.
206  */
207 static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci_new)
208 {
209         CONF_ITEM *ci;
210
211         for (ci = cs->children; ci && ci->next; ci = ci->next)
212                 ;
213
214         if (ci == NULL)
215                 cs->children = ci_new;
216         else
217                 ci->next = ci_new;
218 }
219
220 /*
221  *      Expand the variables in an input string.
222  */
223 static const char *cf_expand_variables(const char *cf, int *lineno,
224                                        CONF_SECTION *outercs,
225                                        char *output, const char *input)
226 {
227         char *p;
228         const char *end, *ptr;
229         char name[8192];
230         CONF_SECTION *parentcs;
231
232         /*
233          *      Find the master parent conf section.
234          *      We can't use mainconfig.config, because we're in the
235          *      process of re-building it, and it isn't set up yet...
236          */
237         for (parentcs = outercs;
238              parentcs->item.parent != NULL;
239              parentcs = parentcs->item.parent) {
240                 /* do nothing */
241         }
242
243         p = output;
244         ptr = input;
245         while (*ptr) {
246                 /*
247                  *      Ignore anything other than "${"
248                  */
249                 if ((*ptr == '$') && (ptr[1] == '{')) {
250                         int up;
251                         CONF_PAIR *cp;
252                         CONF_SECTION *cs;
253
254                         /*
255                          *      Look for trailing '}', and log a
256                          *      warning for anything that doesn't match,
257                          *      and exit with a fatal error.
258                          */
259                         end = strchr(ptr, '}');
260                         if (end == NULL) {
261                                 *p = '\0';
262                                 radlog(L_INFO, "%s[%d]: Variable expansion missing }",
263                                        cf, *lineno);
264                                 return NULL;
265                         }
266
267                         ptr += 2;
268
269                         cp = NULL;
270                         up = 0;
271
272                         /*
273                          *      ${.foo} means "foo from the current section"
274                          */
275                         if (*ptr == '.') {
276                                 up = 1;
277                                 cs = outercs;
278                                 ptr++;
279
280                                 /*
281                                  *      ${..foo} means "foo from the section
282                                  *      enclosing this section" (etc.)
283                                  */
284                                 while (*ptr == '.') {
285                                         if (cs->item.parent)
286                                                 cs = cs->item.parent;
287                                         ptr++;
288                                 }
289
290                         } else {
291                                 const char *q;
292                                 /*
293                                  *      ${foo} is local, with
294                                  *      main as lower priority
295                                  */
296                                 cs = outercs;
297
298                                 /*
299                                  *      ${foo.bar.baz} is always rooted
300                                  *      from the top.
301                                  */
302                                 for (q = ptr; *q && q != end; q++) {
303                                         if (*q == '.') {
304                                                 cs = parentcs;
305                                                 up = 1;
306                                                 break;
307                                         }
308                                 }
309                         }
310
311                         while (cp == NULL) {
312                                 char *q;
313                                 /*
314                                  *      Find the next section.
315                                  */
316                                 for (q = name;
317                                      (*ptr != 0) && (*ptr != '.') &&
318                                              (ptr != end);
319                                      q++, ptr++) {
320                                         *q = *ptr;
321                                 }
322                                 *q = '\0';
323
324                                 /*
325                                  *      The character is a '.', find a
326                                  *      section (as the user has given
327                                  *      us a subsection to find)
328                                  */
329                                 if (*ptr == '.') {
330                                         CONF_SECTION *next;
331
332                                         ptr++;  /* skip the period */
333
334                                         /*
335                                          *      Find the sub-section.
336                                          */
337                                         next = cf_section_sub_find(cs, name);
338                                         if (next == NULL) {
339                                                 radlog(L_ERR, "config: No such section %s in variable %s", name, input);
340                                                 return NULL;
341                                         }
342                                         cs = next;
343
344                                 } else { /* no period, must be a conf-part */
345                                         /*
346                                          *      Find in the current referenced
347                                          *      section.
348                                          */
349                                         cp = cf_pair_find(cs, name);
350                                         if (cp == NULL) {
351                                                 /*
352                                                  *      It it was NOT ${..foo}
353                                                  *      then look in the
354                                                  *      top-level config items.
355                                                  */
356                                                 if (!up) cp = cf_pair_find(parentcs, name);
357                                         }
358                                         if (cp == NULL) {
359                                                 radlog(L_ERR, "config: No such entry %s for string %s", name, input);
360                                                 return NULL;
361                                         }
362                                 }
363                         } /* until cp is non-NULL */
364
365                         /*
366                          *  Substitute the value of the variable.
367                          */
368                         strcpy(p, cp->value);
369                         p += strlen(p);
370                         ptr = end + 1;
371
372                 } else if (memcmp(ptr, "$ENV{", 5) == 0) {
373                         char *env;
374
375                         ptr += 5;
376
377                         /*
378                          *      Look for trailing '}', and log a
379                          *      warning for anything that doesn't match,
380                          *      and exit with a fatal error.
381                          */
382                         end = strchr(ptr, '}');
383                         if (end == NULL) {
384                                 *p = '\0';
385                                 radlog(L_INFO, "%s[%d]: Environment variable expansion missing }",
386                                        cf, *lineno);
387                                 return NULL;
388                         }
389
390                         memcpy(name, ptr, end - ptr);
391                         name[end - ptr] = '\0';
392
393                         /*
394                          *      Get the environment variable.
395                          *      If none exists, then make it an empty string.
396                          */
397                         env = getenv(name);
398                         if (env == NULL) {
399                                 *name = '\0';
400                                 env = name;
401                         }
402
403                         strcpy(p, env);
404                         p += strlen(p);
405                         ptr = end + 1;
406
407                 } else {
408                         /*
409                          *      Copy it over verbatim.
410                          */
411                         *(p++) = *(ptr++);
412                 }
413         } /* loop over all of the input string. */
414
415         *p = '\0';
416
417         return output;
418 }
419
420 /*
421  *      Parse a configuration section into user-supplied variables.
422  */
423 int cf_section_parse(CONF_SECTION *cs, void *base,
424                      const CONF_PARSER *variables)
425 {
426         int i;
427         int rcode;
428         char **q;
429         CONF_PAIR *cp;
430         CONF_SECTION *subsection;
431         uint32_t ipaddr;
432         char buffer[8192];
433         const char *value;
434         void *data;
435
436         /*
437          *      Handle the user-supplied variables.
438          */
439         for (i = 0; variables[i].name != NULL; i++) {
440                 value = variables[i].dflt;
441                 if (variables[i].data) {
442                         data = variables[i].data; /* prefer this. */
443                 } else if (base) {
444                         data = ((char *)base) + variables[i].offset;
445                 } else {
446                         data = variables[i].data;
447                 }
448
449                 cp = cf_pair_find(cs, variables[i].name);
450                 if (cp) {
451                         value = cp->value;
452                 }
453
454                 switch (variables[i].type)
455                 {
456                 case PW_TYPE_SUBSECTION:
457                         subsection = cf_section_sub_find(cs,variables[i].name);
458
459                         /*
460                          *      If the configuration section is NOT there,
461                          *      then ignore it.
462                          *
463                          *      FIXME! This is probably wrong... we should
464                          *      probably set the items to their default values.
465                          */
466                         if (subsection == NULL) {
467                                 break;
468                         }
469
470                         rcode = cf_section_parse(subsection, base,
471                                         (CONF_PARSER *) data);
472                         if (rcode < 0) {
473                                 return -1;
474                         }
475                         break;
476
477                 case PW_TYPE_BOOLEAN:
478                         /*
479                          *      Allow yes/no and on/off
480                          */
481                         if ((strcasecmp(value, "yes") == 0) ||
482                                         (strcasecmp(value, "on") == 0)) {
483                                 *(int *)data = 1;
484                         } else if ((strcasecmp(value, "no") == 0) ||
485                                                 (strcasecmp(value, "off") == 0)) {
486                                 *(int *)data = 0;
487                         } else {
488                                 *(int *)data = 0;
489                                 radlog(L_ERR, "Bad value \"%s\" for boolean variable %s", value, variables[i].name);
490                                 return -1;
491                         }
492                         DEBUG2(" %s: %s = %s",
493                                         cs->name1,
494                                         variables[i].name,
495                                         value);
496                         break;
497
498                 case PW_TYPE_INTEGER:
499                         *(int *)data = strtol(value, 0, 0);
500                         DEBUG2(" %s: %s = %d",
501                                         cs->name1,
502                                         variables[i].name,
503                                         *(int *)data);
504                         break;
505
506                 case PW_TYPE_STRING_PTR:
507                         q = (char **) data;
508                         if (*q != NULL) {
509                                 free(*q);
510                         }
511
512                         /*
513                          *      Expand variables while parsing,
514                          *      but ONLY expand ones which haven't already
515                          *      been expanded.
516                          */
517                         if (value && (value == variables[i].dflt)) {
518                                 value = cf_expand_variables("?",
519                                                             &cs->item.lineno,
520                                                             cs, buffer, value);
521                                 if (!value) {
522                                         return -1;
523                                 }
524                         }
525
526                         DEBUG2(" %s: %s = \"%s\"",
527                                         cs->name1,
528                                         variables[i].name,
529                                         value ? value : "(null)");
530                         *q = value ? strdup(value) : NULL;
531                         break;
532
533                 case PW_TYPE_IPADDR:
534                         /*
535                          *      Allow '*' as any address
536                          */
537                         if (strcmp(value, "*") == 0) {
538                                 *(uint32_t *) data = 0;
539                                 break;
540                         }
541                         ipaddr = ip_getaddr(value);
542                         if (ipaddr == 0) {
543                                 radlog(L_ERR, "Can't find IP address for host %s", value);
544                                 return -1;
545                         }
546                         DEBUG2(" %s: %s = %s IP address [%s]",
547                                         cs->name1,
548                                         variables[i].name,
549                                         value, ip_ntoa(buffer, ipaddr));
550                         *(uint32_t *) data = ipaddr;
551                         break;
552
553                 default:
554                         radlog(L_ERR, "type %d not supported yet", variables[i].type);
555                         return -1;
556                         break;
557                 } /* switch over variable type */
558         } /* for all variables in the configuration section */
559
560         return 0;
561 }
562
563 /*
564  *      Read a part of the config file.
565  */
566 static CONF_SECTION *cf_section_read(const char *cf, int *lineno, FILE *fp,
567                                      const char *name1, const char *name2,
568                                      CONF_SECTION *parent)
569 {
570         CONF_SECTION *cs, *css;
571         CONF_PAIR *cpn;
572         char *ptr;
573         const char *value;
574         char buf[8192];
575         char buf1[8192];
576         char buf2[8192];
577         char buf3[8192];
578         int t1, t2, t3;
579         char *cbuf = buf;
580         int len;
581
582         /*
583          *      Ensure that the user can't add CONF_SECTIONs
584          *      with 'internal' names;
585          */
586         if ((name1 != NULL) && (name1[0] == '_')) {
587                 radlog(L_ERR, "%s[%d]: Illegal configuration section name",
588                         cf, *lineno);
589                 return NULL;
590         }
591
592         /*
593          *      Allocate new section.
594          */
595         cs = cf_section_alloc(name1, name2, parent, cf);
596         cs->item.lineno = *lineno;
597
598         /*
599          *      Read, checking for line continuations ('\\' at EOL)
600          */
601         for (;;) {
602                 int eof;
603
604                 /*
605                  *      Get data, and remember if we are at EOF.
606                  */
607                 eof = (fgets(cbuf, sizeof(buf) - (cbuf - buf), fp) == NULL);
608                 (*lineno)++;
609
610                 len = strlen(cbuf);
611
612                 /*
613                  *      We've filled the buffer, and there isn't
614                  *      a CR in it.  Die!
615                  */
616                 if ((len == sizeof(buf)) &&
617                     (cbuf[len - 1] != '\n')) {
618                         radlog(L_ERR, "%s[%d]: Line too long",
619                                cf, *lineno);
620                         cf_section_free(&cs);
621                         return NULL;
622                 }
623
624                 /*
625                  *  Check for continuations.
626                  */
627                 if (cbuf[len - 1] == '\n') len--;
628
629                 /*
630                  *      Last character is '\\'.  Over-write it,
631                  *      and read another line.
632                  */
633                 if ((len > 0) && (cbuf[len - 1] == '\\')) {
634                         cbuf[len - 1] = '\0';
635                         cbuf += len - 1;
636                         continue;
637                 }
638
639                 /*
640                  *  We're at EOF, and haven't read anything.  Stop.
641                  */
642                 if (eof && (cbuf == buf)) {
643                         break;
644                 }
645
646                 ptr = cbuf = buf;
647                 t1 = gettoken(&ptr, buf1, sizeof(buf1));
648
649                 /*
650                  *      Skip comments and blank lines immediately.
651                  */
652                 if ((*buf1 == '#') || (*buf1 == '\0')) {
653                         continue;
654                 }
655
656                 /*
657                  *      Allow for $INCLUDE files
658                  *
659                  *      This *SHOULD* work for any level include.
660                  *      I really really really hate this file.  -cparker
661                  */
662                 if (strcasecmp(buf1, "$INCLUDE") == 0) {
663
664                         CONF_SECTION      *is;
665
666                         t2 = getword(&ptr, buf2, sizeof(buf2));
667
668                         value = cf_expand_variables(cf, lineno, cs, buf, buf2);
669                         if (value == NULL) {
670                                 cf_section_free(&cs);
671                                 return NULL;
672                         }
673
674                         DEBUG2( "Config:   including file: %s", value );
675
676                         if ((is = conf_read(cf, *lineno, value, cs)) == NULL) {
677                                 cf_section_free(&cs);
678                                 return NULL;
679                         }
680
681                         /*
682                          *      Add the included conf to our CONF_SECTION
683                          */
684                         if (is != NULL) {
685                                 if (is->children != NULL) {
686                                         CONF_ITEM *ci;
687
688                                         /*
689                                          *      Re-write the parent of the
690                                          *      moved children to be the
691                                          *      upper-layer section.
692                                          */
693                                         for (ci = is->children; ci; ci = ci->next) {
694                                                 ci->parent = cs;
695                                         }
696
697                                         /*
698                                          *      If there are children, then
699                                          *      move them up a layer.
700                                          */
701                                         if (is->children) {
702                                                 cf_item_add(cs, is->children);
703                                         }
704                                         is->children = NULL;
705                                 }
706                                 /*
707                                  *      Always free the section for the
708                                  *      $INCLUDEd file.
709                                  */
710                                 cf_section_free(&is);
711                         }
712
713                         continue;
714                 }
715
716                 /*
717                  *      No '=': must be a section or sub-section.
718                  */
719                 if (strchr(ptr, '=') == NULL) {
720                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
721                         t3 = gettoken(&ptr, buf3, sizeof(buf3));
722                 } else {
723                         t2 = gettoken(&ptr, buf2, sizeof(buf2));
724                         t3 = getword(&ptr, buf3, sizeof(buf3));
725                 }
726
727                 /*
728                  *      See if it's the end of a section.
729                  */
730                 if (t1 == T_RCBRACE) {
731                         if (name1 == NULL || buf2[0]) {
732                                 radlog(L_ERR, "%s[%d]: Unexpected end of section",
733                                                 cf, *lineno);
734                                 cf_section_free(&cs);
735                                 return NULL;
736                         }
737                         return cs;
738                 }
739
740                 /*
741                  * Perhaps a subsection.
742                  */
743                 if (t2 == T_LCBRACE || t3 == T_LCBRACE) {
744                         css = cf_section_read(cf, lineno, fp, buf1,
745                                               t2==T_LCBRACE ? NULL : buf2, cs);
746                         if (css == NULL) {
747                                 cf_section_free(&cs);
748                                 return NULL;
749                         }
750                         cf_item_add(cs, cf_sectiontoitem(css));
751
752                         continue;
753                 }
754
755                 /*
756                  *      Ignore semi-colons.
757                  */
758                 if (*buf2 == ';')
759                         *buf2 = '\0';
760
761                 /*
762                  *      Must be a normal attr = value line.
763                  */
764                 if (buf1[0] != 0 && buf2[0] == 0 && buf3[0] == 0) {
765                         t2 = T_OP_EQ;
766                 } else if (buf1[0] == 0 || buf2[0] == 0 ||
767                            (t2 < T_EQSTART || t2 > T_EQEND)) {
768                         radlog(L_ERR, "%s[%d]: Line is not in 'attribute = value' format",
769                                         cf, *lineno);
770                         cf_section_free(&cs);
771                         return NULL;
772                 }
773
774                 /*
775                  *      Ensure that the user can't add CONF_PAIRs
776                  *      with 'internal' names;
777                  */
778                 if (buf1[0] == '_') {
779                         radlog(L_ERR, "%s[%d]: Illegal configuration pair name \"%s\"",
780                                         cf, *lineno, buf1);
781                         cf_section_free(&cs);
782                         return NULL;
783                 }
784
785                 /*
786                  *      Handle variable substitution via ${foo}
787                  */
788                 value = cf_expand_variables(cf, lineno, cs, buf, buf3);
789                 if (!value) {
790                         cf_section_free(&cs);
791                         return NULL;
792                 }
793
794
795                 /*
796                  *      Add this CONF_PAIR to our CONF_SECTION
797                  */
798                 cpn = cf_pair_alloc(buf1, value, t2, parent);
799                 cpn->item.lineno = *lineno;
800                 cf_item_add(cs, cf_pairtoitem(cpn));
801         }
802
803         /*
804          *      See if EOF was unexpected ..
805          */
806         if (name1 != NULL) {
807                 radlog(L_ERR, "%s[%d]: Unexpected end of file", cf, *lineno);
808                 cf_section_free(&cs);
809                 return NULL;
810         }
811
812         return cs;
813 }
814
815 /*
816  *      Read the config file.
817  */
818 CONF_SECTION *conf_read(const char *fromfile, int fromline,
819                         const char *conffile, CONF_SECTION *parent)
820 {
821         FILE            *fp;
822         int             lineno = 0;
823         CONF_SECTION    *cs;
824         struct stat     statbuf;
825         char            buf[8192];
826
827         buf[0] = '\0';
828         if (fromfile) {
829                 snprintf(buf, sizeof(buf), "%s[%d]: ", fromfile, fromline);
830         }
831
832         if (stat(conffile, &statbuf) == 0) {
833                 if ((statbuf.st_mode & S_IWOTH) != 0) {
834                         radlog(L_ERR|L_CONS, "%sConfiguration file %s is globally writable.  Refusing to start due to insecure configuration.",
835                                buf[0] ? buf : "", conffile);
836                         return NULL;
837                 }
838
839                 if ((statbuf.st_mode & S_IROTH) != 0) {
840                         radlog(L_ERR|L_CONS, "%sConfiguration file %s is globally readable.  Refusing to start due to insecure configuration.",
841                                buf[0] ? buf : "", conffile);
842                         return NULL;
843                 }
844         }
845
846         if ((fp = fopen(conffile, "r")) == NULL) {
847                 radlog(L_ERR|L_CONS, "%sUnable to open file \"%s\": %s",
848                        buf[0] ? buf : "", conffile, strerror(errno));
849                 return NULL;
850         }
851
852         cs = cf_section_read(conffile, &lineno, fp, NULL, NULL, parent);
853
854         fclose(fp);
855
856         return cs;
857 }
858
859
860 /*
861  * Return a CONF_PAIR within a CONF_SECTION.
862  */
863 CONF_PAIR *cf_pair_find(CONF_SECTION *section, const char *name)
864 {
865         CONF_ITEM       *ci;
866
867         if (section == NULL) {
868                 section = mainconfig.config;
869         }
870
871         for (ci = section->children; ci; ci = ci->next) {
872                 if (ci->type != CONF_ITEM_PAIR)
873                         continue;
874                 if (name == NULL || strcmp(cf_itemtopair(ci)->attr, name) == 0)
875                         break;
876         }
877
878         return cf_itemtopair(ci);
879 }
880
881 /*
882  * Return the attr of a CONF_PAIR
883  */
884
885 char *cf_pair_attr(CONF_PAIR *pair)
886 {
887         return (pair ? pair->attr : NULL);
888 }
889
890 /*
891  * Return the value of a CONF_PAIR
892  */
893
894 char *cf_pair_value(CONF_PAIR *pair)
895 {
896         return (pair ? pair->value : NULL);
897 }
898
899 /*
900  * Return the first label of a CONF_SECTION
901  */
902
903 char *cf_section_name1(CONF_SECTION *section)
904 {
905         return (section ? section->name1 : NULL);
906 }
907
908 /*
909  * Return the second label of a CONF_SECTION
910  */
911
912 char *cf_section_name2(CONF_SECTION *section)
913 {
914         return (section ? section->name2 : NULL);
915 }
916
917 /*
918  * Find a value in a CONF_SECTION
919  */
920 char *cf_section_value_find(CONF_SECTION *section, const char *attr)
921 {
922         CONF_PAIR       *cp;
923
924         cp = cf_pair_find(section, attr);
925
926         return (cp ? cp->value : NULL);
927 }
928
929 /*
930  * Return the next pair after a CONF_PAIR
931  * with a certain name (char *attr) If the requested
932  * attr is NULL, any attr matches.
933  */
934
935 CONF_PAIR *cf_pair_find_next(CONF_SECTION *section, CONF_PAIR *pair, const char *attr)
936 {
937         CONF_ITEM       *ci;
938
939         /*
940          * If pair is NULL this must be a first time run
941          * Find the pair with correct name
942          */
943
944         if (pair == NULL){
945                 return cf_pair_find(section, attr);
946         }
947
948         ci = cf_pairtoitem(pair)->next;
949
950         for (; ci; ci = ci->next) {
951                 if (ci->type != CONF_ITEM_PAIR)
952                         continue;
953                 if (attr == NULL || strcmp(cf_itemtopair(ci)->attr, attr) == 0)
954                         break;
955         }
956
957         return cf_itemtopair(ci);
958 }
959
960 /*
961  * Find a CONF_SECTION, or return the root if name is NULL
962  */
963
964 CONF_SECTION *cf_section_find(const char *name)
965 {
966         if (name)
967                 return cf_section_sub_find(mainconfig.config, name);
968         else
969                 return mainconfig.config;
970 }
971
972 /*
973  * Find a sub-section in a section
974  */
975
976 CONF_SECTION *cf_section_sub_find(CONF_SECTION *section, const char *name)
977 {
978         CONF_ITEM *ci;
979
980         for (ci = section->children; ci; ci = ci->next) {
981                 if (ci->type != CONF_ITEM_SECTION)
982                         continue;
983                 if (strcmp(cf_itemtosection(ci)->name1, name) == 0)
984                         break;
985         }
986
987         return cf_itemtosection(ci);
988
989 }
990
991 /*
992  * Return the next subsection after a CONF_SECTION
993  * with a certain name1 (char *name1). If the requested
994  * name1 is NULL, any name1 matches.
995  */
996
997 CONF_SECTION *cf_subsection_find_next(CONF_SECTION *section,
998                 CONF_SECTION *subsection,
999                 const char *name1)
1000 {
1001         CONF_ITEM       *ci;
1002
1003         /*
1004          * If subsection is NULL this must be a first time run
1005          * Find the subsection with correct name
1006          */
1007
1008         if (subsection == NULL){
1009                 ci = section->children;
1010         } else {
1011                 ci = cf_sectiontoitem(subsection)->next;
1012         }
1013
1014         for (; ci; ci = ci->next) {
1015                 if (ci->type != CONF_ITEM_SECTION)
1016                         continue;
1017                 if ((name1 == NULL) ||
1018                                 (strcmp(cf_itemtosection(ci)->name1, name1) == 0))
1019                         break;
1020         }
1021
1022         return cf_itemtosection(ci);
1023 }
1024
1025 /*
1026  * Return the next item after a CONF_ITEM.
1027  */
1028
1029 CONF_ITEM *cf_item_find_next(CONF_SECTION *section, CONF_ITEM *item)
1030 {
1031         /*
1032          * If item is NULL this must be a first time run
1033          * Return the first item
1034          */
1035
1036         if (item == NULL) {
1037                 return section->children;
1038         } else {
1039                 return item->next;
1040         }
1041 }
1042
1043 int cf_section_lineno(CONF_SECTION *section)
1044 {
1045         return cf_sectiontoitem(section)->lineno;
1046 }
1047
1048 int cf_pair_lineno(CONF_PAIR *pair)
1049 {
1050         return cf_pairtoitem(pair)->lineno;
1051 }
1052
1053 int cf_item_is_section(CONF_ITEM *item)
1054 {
1055         return item->type == CONF_ITEM_SECTION;
1056 }
1057 int cf_item_is_pair(CONF_ITEM *item)
1058 {
1059         return item->type == CONF_ITEM_PAIR;
1060 }
1061
1062
1063 #if 0
1064 /*
1065  * JMG dump_config tries to dump the config structure in a readable format
1066  *
1067 */
1068
1069 static int dump_config_section(CONF_SECTION *cs, int indent)
1070 {
1071         CONF_SECTION    *scs;
1072         CONF_PAIR       *cp;
1073         CONF_ITEM       *ci;
1074
1075         /* The DEBUG macro doesn't let me
1076          *   for(i=0;i<indent;++i) debugputchar('\t');
1077          * so I had to get creative. --Pac. */
1078
1079         for (ci = cs->children; ci; ci = ci->next) {
1080                 if (ci->type == CONF_ITEM_PAIR) {
1081                         cp=cf_itemtopair(ci);
1082                         DEBUG("%.*s%s = %s",
1083                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1084                                 cp->attr, cp->value);
1085                 } else {
1086                         scs=cf_itemtosection(ci);
1087                         DEBUG("%.*s%s %s%s{",
1088                                 indent, "\t\t\t\t\t\t\t\t\t\t\t",
1089                                 scs->name1,
1090                                 scs->name2 ? scs->name2 : "",
1091                                 scs->name2 ?  " " : "");
1092                         dump_config_section(scs, indent+1);
1093                         DEBUG("%.*s}",
1094                                 indent, "\t\t\t\t\t\t\t\t\t\t\t");
1095                 }
1096         }
1097
1098         return 0;
1099 }
1100
1101 int dump_config(void)
1102 {
1103         return dump_config_section(mainconfig.config, 0);
1104 }
1105 #endif