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