The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
cf_file.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: b0b3049af3cdca377680d64cfc971cf4d42287ba $
19 * @file cf_file.c
20 * @brief Read the radiusd.conf file.
21 *
22 * @note Yep I should learn to use lex & yacc, or at least
23 * write a decent parser. I know how to do that, really :)
24 * miquels@cistron.nl
25 *
26 * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
27 * @copyright 2000,2006 The FreeRADIUS server project
28 * @copyright 2000 Miquel van Smoorenburg (miquels@cistron.nl)
29 * @copyright 2000 Alan DeKok (aland@freeradius.org)
30 */
31RCSID("$Id: b0b3049af3cdca377680d64cfc971cf4d42287ba $")
32
33#include <sys/errno.h>
34
35#include <freeradius-devel/server/cf_file.h>
36#include <freeradius-devel/server/cf_priv.h>
37#include <freeradius-devel/server/log.h>
38#include <freeradius-devel/server/tmpl.h>
39#include <freeradius-devel/server/util.h>
40#include <freeradius-devel/server/virtual_servers.h>
41#include <freeradius-devel/util/debug.h>
42#include <freeradius-devel/util/file.h>
43#include <freeradius-devel/util/misc.h>
44#include <freeradius-devel/util/perm.h>
45#include <freeradius-devel/util/skip.h>
46#include <freeradius-devel/util/md5.h>
47
48#ifdef HAVE_DIRENT_H
49#endif
50
51#ifdef HAVE_GLOB_H
52#endif
53
54#ifdef HAVE_SYS_STAT_H
55#endif
56
57#include <fcntl.h>
58
59#include <freeradius-devel/server/main_config.h>
60
61bool check_config = false;
62static uid_t conf_check_uid = (uid_t)-1;
63static gid_t conf_check_gid = (gid_t)-1;
64
70
72 { L("instance"), CONF_PROPERTY_INSTANCE },
73 { L("name"), CONF_PROPERTY_NAME }
74};
76
78 { L("accounting"), true },
79 { L("add"), true },
80 { L("authenticate"), true },
81 { L("clear"), true },
82 { L("deny"), true },
83 { L("error"), true },
84 { L("establish"), true },
85 { L("finally"), true },
86 { L("load"), true },
87 { L("new"), true },
88 { L("recv"), true },
89 { L("send"), true },
90 { L("store"), true },
91 { L("verify"), true },
92};
94
95typedef enum {
97#ifdef HAVE_DIRENT_H
98 CF_STACK_DIR,
99#endif
100#ifdef HAVE_GLOB_H
101 CF_STACK_GLOB
102#endif
104
105#define MAX_STACK (32)
106typedef struct {
108
109 char const *filename; //!< filename we're reading
110 int lineno; //!< line in that filename
111
112 union {
113 struct {
114 FILE *fp; //!< FP we're reading
115 };
116
117#ifdef HAVE_DIRENT_H
118 struct {
119 fr_heap_t *heap; //!< sorted heap of files
120 char *directory; //!< directory name we're reading
121 };
122#endif
123
124#ifdef HAVE_GLOB_H
125 struct {
126 size_t gl_current;
127 glob_t glob; //! reading glob()
128 bool required;
129 };
130#endif
131 };
132
133 CONF_SECTION *parent; //!< which started this file
134 CONF_SECTION *current; //!< sub-section we're reading
135 CONF_SECTION *at_reference; //!< was this thing an @foo ?
136 int at_reference_braces; //!< braces when we found this thing
137
139 bool from_dir; //!< this file was read from $include foo/
141
142/*
143 * buff[0] is the data we read from the file
144 * buff[1] is name
145 * buff[2] is name2 OR value for pair
146 * buff[3] is a temporary buffer
147 */
148typedef struct {
149 char **buff; //!< buffers for reading / parsing
150 size_t bufsize; //!< size of the buffers
151 int depth; //!< stack depth
152 char const *ptr; //!< current parse pointer
153 char *fill; //!< where we start filling the buffer from
154 cf_stack_frame_t frame[MAX_STACK]; //!< stack frames
155} cf_stack_t;
156
157/*
158 * Open and read a file.
159 */
160static int cf_expand_file(char const *cf, int lineno, char name[static PATH_MAX],
161 char **p_p, char const **ptr_p, char *output, size_t outsize,
162 bool raw)
163{
164 int fd;
165 size_t room;
166 ssize_t len;
167 char *p, *next;
168 char const *ptr;
169
170 /*
171 * Note that we do NOT recursively expand the value. It has to be a hard-coded string.
172 *
173 * We do NOT do any sanity checks on the value. i.e. filenames beginning with '/' are allowed,
174 * as are files with "../../". The value here comes from the configuration files, and only the
175 * administrator has write access to them.
176 */
177 strlcpy(name, cf, PATH_MAX);
178 p = strrchr(name, '/');
179 if (p) {
180 p++;
181 } else {
182 p = name;
183 }
184
185 ptr = *ptr_p;
186
187 /*
188 * Look for trailing '}', and log a
189 * warning for anything that doesn't match,
190 * and exit with a fatal error.
191 */
192 next = strchr(ptr, '}');
193 if (next == NULL) {
194 *p = '\0';
195 ERROR("%s[%d]: File expansion missing }",
196 cf, lineno);
197 return -1;
198 }
199
200 /*
201 * Can't really happen because input lines are
202 * capped at 8k, which is sizeof(name)
203 */
204 if ((next - ptr) >= (name + PATH_MAX - p)) {
205 ERROR("%s[%d]: File name is too large",
206 cf, lineno);
207 return -1;
208 }
209
210 memcpy(p, ptr, next - ptr);
211 p[next - ptr] = '\0';
212
213 fd = open(name, O_RDONLY);
214 if (fd < 0) {
215 ERROR("%s[%d]: Failed opening %s: %s",
216 cf, lineno, name, strerror(errno));
217 return -1;
218 }
219
220 p = *p_p;
221 room = (output + outsize) - p;
222 fr_assert(room > 0);
223
224 /*
225 * Read the raw data.
226 */
227 len = read(fd, p, room);
228 if (len < 0) {
229 ERROR("%s[%d]: Failed reading %s: %s",
230 cf, lineno, name, strerror(errno));
231 close(fd);
232 return -1;
233 }
234 close(fd);
235
236 if (!len) {
237 ERROR("%s[%d]: Failed reading %s: the file is empty",
238 cf, lineno, name);
239 return -1;
240 }
241
242 /*
243 * We don't know whether or not it was
244 * truncated, so we just error out.
245 */
246 if ((size_t) len >= room) {
247 ERROR("%s[%d]: Too much data in %s: did not read the entire file",
248 cf, lineno, name);
249 return -1;
250 }
251
252 /*
253 * If we're not reading the raw file, return only the first line.
254 */
255 if (!raw) {
256 char *q, *end = p + len;
257
258 while (p < end) {
259 if (*p >= ' ') {
260 p++;
261 continue;
262 }
263
264 break;
265 }
266
267 /*
268 * Strip trailing CR/LF.
269 */
270 for (q = p; q < end; q++) {
271 if (*q >= ' ') break;
272
273 *q = '\0';
274 }
275
276 if (q != end) {
277 ERROR("%s[%d]: Too much data in %s: expected one line of text, found multiple lines in the file",
278 cf, lineno, name);
279 return -1;
280 }
281 } else {
282 p += len;
283 }
284
285 *ptr_p = next + 1;
286 *p_p = p;
287
288 return 0;
289}
290
291/*
292 * Expand the variables in an input string.
293 *
294 * Input and output should be two different buffers, as the
295 * output may be longer than the input.
296 */
297char const *cf_expand_variables(char const *cf, int lineno,
298 CONF_SECTION *outer_cs,
299 char *output, size_t outsize,
300 char const *input, ssize_t inlen, bool *soft_fail, bool soft_fail_env)
301{
302 char *p;
303 char const *end, *next, *ptr;
304 CONF_SECTION const *parent_cs;
305 char name[PATH_MAX];
306
307 if (soft_fail) *soft_fail = false;
308
309 /*
310 * Utilities (radjson2conf rebuilding a fragment from JSON)
311 * can opt out of variable resolution so values containing
312 * `${...}` round-trip verbatim instead of failing against
313 * an incomplete tree.
314 */
315 if (!_cf_expand_variables()) {
316 size_t want = inlen >= 0 ? (size_t)inlen : strlen(input);
317 if (want >= outsize) want = outsize - 1;
318 memcpy(output, input, want);
319 output[want] = '\0';
320 return output;
321 }
322
323 /*
324 * Find the master parent conf section.
325 * We can't use main_config->root_cs, because we're in the
326 * process of re-building it, and it isn't set up yet...
327 */
328 parent_cs = cf_root(outer_cs);
329
330 p = output;
331 ptr = input;
332
333 if (inlen < 0) {
334 end = NULL;
335 } else {
336 end = input + inlen;
337 }
338
339 /*
340 * Note that this CAN go over "end" if the input string
341 * is malformed. e.g. pass "${foo.bar}", and pass
342 * "inlen=5". Well, too bad.
343 */
344 while (*ptr && (!end || (ptr < end))) {
345 /*
346 * Ignore anything other than "${"
347 */
348 if ((*ptr == '$') && (ptr[1] == '{')) {
349 CONF_ITEM *ci;
350 CONF_PAIR *cp;
351 char *q;
352 ssize_t len;
353
354 len = fr_skip_xlat(ptr, end);
355 if (len <= 0) {
356 ERROR("%s[%d]: Failed parsing variable expansion '%s''",
357 cf, lineno, input);
358 return NULL;
359 }
360
361 next = ptr + len;
362 ptr += 2;
363
364 /*
365 * Can't really happen because input lines are
366 * capped at 8k, which is sizeof(name)
367 */
368 if ((size_t) len >= sizeof(name)) {
369 ERROR("%s[%d]: Reference string is too large",
370 cf, lineno);
371 return NULL;
372 }
373
374 memcpy(name, ptr, len - 3);
375 name[len - 3] = '\0';
376
377 /*
378 * Read configuration value from a file.
379 *
380 * Note that this is "read binary data", and the contents aren't stripped of
381 * CRLF.
382 */
383 if (name[0] == '/') {
384 int fd = open(name, O_RDONLY);
385 struct stat buf;
386
387 if (fd < 0) {
388 ERROR("%s[%d]: Reference \"${%s}\" failed opening file - %s", cf, lineno, name, fr_syserror(errno));
389 return NULL;
390 }
391
392 if (fstat(fd, &buf) < 0) {
393 fail_fd:
394 close(fd);
395 ERROR("%s[%d]: Reference \"${%s}\" failed reading file - %s", cf, lineno, name, fr_syserror(errno));
396 return NULL;
397 }
398
399 if (buf.st_size >= ((output + outsize) - p)) {
400 close(fd);
401 ERROR("%s[%d]: Reference \"${%s}\" file is too large (%zu >= %zu)", cf, lineno, name,
402 (size_t) buf.st_size, (size_t) ((output + outsize) - p));
403 return NULL;
404 }
405
406 len = read(fd, p, (output + outsize) - p - 1);
407 if (len < 0) goto fail_fd;
408
409 close(fd);
410 p += len;
411 *p = '\0';
412 ptr = next;
413 goto check_eos;
414 }
415
416 q = strchr(name, ':');
417 if (q) {
418 *(q++) = '\0';
419 }
420
421 ci = cf_reference_item(parent_cs, outer_cs, name);
422 if (!ci) {
423 if (soft_fail) *soft_fail = true;
424 PERROR("%s[%d]: Failed finding reference \"${%s}\"", cf, lineno, name);
425 return NULL;
426 }
427
428 /*
429 * The expansion doesn't refer to another item or section
430 * it's the property of a section.
431 */
432 if (q) {
433 CONF_SECTION *find;
434 char const *f;
435 size_t flen;
436
437 if (ci->type != CONF_ITEM_SECTION) {
438 ERROR("%s[%d]: Can only reference properties of sections", cf, lineno);
439 return NULL;
440 }
441
442 find = cf_item_to_section(ci);
445 f = find->name1;
446 break;
447
449 f = find->name2 ? find->name2 : find->name1;
450 break;
451
452 default:
453 ERROR("%s[%d]: Invalid property '%s'", cf, lineno, q);
454 return NULL;
455 }
456
457 flen = talloc_strlen(f);
458 if ((p + flen) >= (output + outsize)) goto too_long;
459
460 memcpy(p, f, flen);
461 p += flen;
462 ptr = next;
463
464 } else if (ci->type == CONF_ITEM_PAIR) {
465 /*
466 * Substitute the value of the variable.
467 */
468 cp = cf_item_to_pair(ci);
469
470 /*
471 * If the thing we reference is
472 * marked up as being expanded in
473 * pass2, don't expand it now.
474 * Let it be expanded in pass2.
475 */
476 if (cp->pass2) {
477 if (soft_fail) *soft_fail = true;
478
479 ERROR("%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
480 cf, lineno, input);
481 return NULL;
482 }
483
484 if (!cp->value) {
485 ERROR("%s[%d]: Reference \"%s\" has no value",
486 cf, lineno, input);
487 return NULL;
488 }
489
490 if (p + strlen(cp->value) >= output + outsize) {
491 ERROR("%s[%d]: Reference \"%s\" is too long",
492 cf, lineno, input);
493 return NULL;
494 }
495
496 strcpy(p, cp->value);
497 cp->item.referenced = true;
498 p += strlen(p);
499 ptr = next;
500
501 } else if (ci->type == CONF_ITEM_SECTION) {
502 CONF_SECTION *subcs;
503 CONF_ITEM *ci_p;
504
505 /*
506 * We can't refer to any parent, otherwise we have an infinite loop.
507 */
508 for (ci_p = &outer_cs->item; ci_p != NULL; ci_p = ci_p->parent) {
509 if (ci_p == ci) {
510 ERROR("%s[%d]: Cannot reference different item in same section", cf, lineno);
511 return NULL;
512 }
513 }
514
515 /*
516 * Copy the section instead of
517 * referencing it.
518 */
519 subcs = cf_item_to_section(ci);
520 subcs = cf_section_dup(outer_cs, outer_cs, subcs,
521 cf_section_name1(subcs), cf_section_name2(subcs),
522 false);
523 if (!subcs) {
524 ERROR("%s[%d]: Failed copying reference %s", cf, lineno, name);
525 return NULL;
526 }
527
528 cf_filename_set(subcs, ci->filename);
529 cf_lineno_set(subcs, ci->lineno);
530
531 ptr = next;
532
533 } else {
534 ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, lineno, input);
535 return NULL;
536 }
537 } else if (strncmp(ptr, "$ENV{", 5) == 0) {
538 char *env;
539
540 ptr += 5;
541
542 /*
543 * Look for trailing '}', and log a
544 * warning for anything that doesn't match,
545 * and exit with a fatal error.
546 */
547 next = strchr(ptr, '}');
548 if (next == NULL) {
549 *p = '\0';
550 ERROR("%s[%d]: Environment variable expansion missing }",
551 cf, lineno);
552 return NULL;
553 }
554
555 /*
556 * Can't really happen because input lines are
557 * capped at 8k, which is sizeof(name)
558 */
559 if ((size_t) (next - ptr) >= sizeof(name)) {
560 ERROR("%s[%d]: Environment variable name is too large",
561 cf, lineno);
562 return NULL;
563 }
564
565 memcpy(name, ptr, next - ptr);
566 name[next - ptr] = '\0';
567
568 /*
569 * Get the environment variable.
570 * If none exists, then make it an empty string.
571 */
572 env = getenv(name);
573 if (!env) {
574 if (!soft_fail_env) {
575 ERROR("%s[%d]: Invalid reference to missing environment variable $ENV{%s}",
576 cf, lineno, name);
577 return NULL;
578 }
579
580 *name = '\0';
581 env = name;
582 }
583
584 if (p + strlen(env) >= output + outsize) {
585 ERROR("%s[%d]: Reference \"%s\" is too long",
586 cf, lineno, input);
587 return NULL;
588 }
589
590 strcpy(p, env);
591 p += strlen(p);
592 ptr = next + 1;
593
594 } else if (strncmp(ptr, "$VALUE{", 7) == 0) {
595 ptr += 7;
596
597 if (cf_expand_file(cf, lineno, name, &p, &ptr, output, outsize, false) < 0) return NULL;
598
599 } else if (strncmp(ptr, "$FILE{", 6) == 0) {
600 ptr += 6;
601
602 if (cf_expand_file(cf, lineno, name, &p, &ptr, output, outsize, true) < 0) return NULL;
603
604 } else {
605 /*
606 * Copy it over verbatim.
607 */
608 *(p++) = *(ptr++);
609 }
610
611 check_eos:
612 if (p >= (output + outsize)) {
613 too_long:
614 ERROR("%s[%d]: Reference \"%s\" is too long",
615 cf, lineno, input);
616 return NULL;
617 }
618 } /* loop over all of the input string. */
619
620 *p = '\0';
621
622 return output;
623}
624
625/*
626 * Merge the template so everything else "just works".
627 */
628static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
629{
630 if (!cs || !template) return true;
631
632 cs->template = NULL;
633
634 /*
635 * Walk over the template, adding its' entries to the
636 * current section. But only if the entries don't
637 * already exist in the current section.
638 */
639 cf_item_foreach(&template->item, ci) {
640 if (ci->type == CONF_ITEM_PAIR) {
641 CONF_PAIR *cp1, *cp2;
642
643 /*
644 * It exists, don't over-write it.
645 */
646 cp1 = cf_item_to_pair(ci);
647 if (cf_pair_find(cs, cp1->attr)) {
648 continue;
649 }
650
651 /*
652 * Create a new pair with all of the data
653 * of the old one.
654 */
655 cp2 = cf_pair_dup(cs, cp1, true);
656 if (!cp2) return false;
657
658 cf_filename_set(cp2, cp1->item.filename);
659 cf_lineno_set(cp2, cp1->item.lineno);
660 continue;
661 }
662
663 if (ci->type == CONF_ITEM_SECTION) {
664 CONF_SECTION *subcs1, *subcs2;
665
666 subcs1 = cf_item_to_section(ci);
667 fr_assert(subcs1 != NULL);
668
669 subcs2 = cf_section_find(cs, subcs1->name1, subcs1->name2);
670 if (subcs2) {
671 /*
672 * sub-sections get merged.
673 */
674 if (!cf_template_merge(subcs2, subcs1)) {
675 return false;
676 }
677 continue;
678 }
679
680 /*
681 * Our section doesn't have a matching
682 * sub-section. Copy it verbatim from
683 * the template.
684 */
685 subcs2 = cf_section_dup(cs, cs, subcs1,
686 cf_section_name1(subcs1), cf_section_name2(subcs1),
687 false);
688 if (!subcs2) return false;
689
690 cf_filename_set(subcs2, subcs1->item.filename);
691 cf_lineno_set(subcs2, subcs1->item.lineno);
692 continue;
693 }
694
695 /* ignore everything else */
696 }
697
698 return true;
699}
700
701/*
702 * Functions for tracking files by inode
703 */
704static int8_t _inode_cmp(void const *one, void const *two)
705{
706 cf_file_t const *a = one, *b = two;
707
708 CMP_RETURN(a, b, buf.st_dev);
709
710 return CMP(a->buf.st_ino, b->buf.st_ino);
711}
712
713static int cf_file_open(CONF_SECTION *cs, char const *filename, bool from_dir, FILE **fp_p)
714{
716 CONF_SECTION *top;
717 fr_rb_tree_t *tree;
718 FILE *fp;
719
720 top = cf_root(cs);
721 tree = cf_data_value(cf_data_find(top, fr_rb_tree_t, "filename"));
722 fr_assert(tree);
723
724 /*
725 * If we're including a wildcard directory, then ignore
726 * any files the users has already explicitly loaded in
727 * that directory.
728 */
729 if (from_dir) {
730 cf_file_t my_file;
731 char const *r;
732 int fd, my_fd;
733
734 my_file.cs = cs;
735 my_file.filename = filename;
736
737 /*
738 * Find and open the directory containing filename so we can use
739 * the "at"functions to avoid time of check/time of use insecurities.
740 */
741 if (fr_dirfd(&my_fd, &r, filename) < 0) {
742 ERROR("Failed to open directory containing %s", filename);
743 return -1;
744 }
745
746 if (fstatat(my_fd, r, &my_file.buf, 0) < 0) {
747 if (my_fd != AT_FDCWD) close(my_fd);
748 goto error;
749 }
750
751 file = fr_rb_find(tree, &my_file);
752
753 /*
754 * The file was previously read by including it
755 * explicitly. After it was read, we have a
756 * $INCLUDE of the directory it is in. In that
757 * case, we ignore the file.
758 *
759 * However, if the file WAS read from a wildcard
760 * $INCLUDE directory, then we read it again.
761 */
762 if (file && !file->from_dir) {
763 if (my_fd != AT_FDCWD) close(my_fd);
764 return 1;
765 }
766
767 fd = openat(my_fd, r, O_RDONLY, 0);
768 if (my_fd != AT_FDCWD) close(my_fd);
769 if (fd < 0) goto error;
770 fp = fdopen(fd, "r");
771 } else {
772 fp = fopen(filename, "r");
773 }
774
775 if (DEBUG_ENABLED2) cf_log_debug(cs, "including configuration file %s", filename);
776
777 if (!fp) {
778 error:
779 ERROR("Unable to open file \"%s\": %s", filename, fr_syserror(errno));
780 return -1;
781 }
782
783 MEM(file = talloc(tree, cf_file_t));
784
785 file->filename = talloc_strdup(file, filename); /* The rest of the code expects this to be a talloced buffer */
786 file->cs = cs;
787 file->from_dir = from_dir;
788
789 if (fstat(fileno(fp), &file->buf) == 0) {
790#ifdef S_IWOTH
791 if ((file->buf.st_mode & S_IWOTH) != 0) {
792 ERROR("Configuration file %s is globally writable. "
793 "Refusing to start due to insecure configuration.", filename);
794
795 fclose(fp);
797 return -1;
798 }
799#endif
800 }
801
802 /*
803 * We can include the same file twice. e.g. when it
804 * contains common definitions, such as for SQL.
805 *
806 * Though the admin should really use templates for that.
807 */
808 if (!fr_rb_insert(tree, file)) talloc_free(file);
809
810 *fp_p = fp;
811 return 0;
812}
813
814/** Set the euid/egid used when performing file checks
815 *
816 * Sets the euid, and egid used when cf_file_check is called to check
817 * permissions on conf items of type #CONF_FLAG_FILE_READABLE
818 *
819 * @note This is probably only useful for the freeradius daemon itself.
820 *
821 * @param uid to set, (uid_t)-1 to use current euid.
822 * @param gid to set, (gid_t)-1 to use current egid.
823 */
824void cf_file_check_set_uid_gid(uid_t uid, gid_t gid)
825{
826 if (uid != (uid_t) -1) conf_check_uid = uid;
827 if (gid != (gid_t) -1) conf_check_gid = gid;
828}
829
830/** Perform an operation with the effect/group set to conf_check_gid and conf_check_uid
831 *
832 * @param filename CONF_PAIR for the file being checked
833 * @param cb callback function to perform the check
834 * @param uctx user context for the callback
835 * @return
836 * - CF_FILE_OTHER_ERROR if there was a problem modifying permissions
837 * - The return value from the callback
838 */
840 cf_file_check_err_t (*cb)(char const *filename, void *uctx), void *uctx)
841{
842 int ret;
843
844 uid_t euid = (uid_t) -1;
845 gid_t egid = (gid_t) -1;
846
847 if ((conf_check_gid != (gid_t) -1) && ((egid = getegid()) != conf_check_gid)) {
848 if (setegid(conf_check_gid) < 0) {
849 fr_strerror_printf("Failed setting effective group ID (%d) for file check: %s",
850 (int) conf_check_gid, fr_syserror(errno));
851 return CF_FILE_OTHER_ERROR;
852 }
853 }
854 if ((conf_check_uid != (uid_t) -1) && ((euid = geteuid()) != conf_check_uid)) {
855 if (seteuid(conf_check_uid) < 0) {
856 fr_strerror_printf("Failed setting effective user ID (%d) for file check: %s",
857 (int) conf_check_uid, fr_syserror(errno));
858
859 restore_gid:
860 if ((conf_check_gid != egid) &&
861 (setegid(conf_check_gid) < 0)) {
862 fr_strerror_printf_push("Failed resetting effective group ID (%d) for file check: %s",
863 (int) conf_check_gid, fr_syserror(errno));
864 }
865
866 return CF_FILE_OTHER_ERROR;
867 }
868 }
869 ret = cb(filename, uctx);
870 if (conf_check_uid != euid) {
871 if (seteuid(euid) < 0) {
872 fr_strerror_printf("Failed restoring effective user ID (%d) after file check: %s",
873 (int) euid, fr_syserror(errno));
874 goto restore_gid;
875 }
876 }
877 if (conf_check_gid != egid) {
878 if (setegid(egid) < 0) {
879 fr_strerror_printf("Failed restoring effective group ID (%d) after file check: %s",
880 (int) egid, fr_syserror(errno));
881 return CF_FILE_OTHER_ERROR;
882 }
883 }
884
885 return ret;
886}
887
888/** Check if we can connect to a unix socket
889 *
890 * @param[in] filename CONF_PAIR for the unix socket path
891 * @param[in] uctx user context, not used
892 * @return
893 * - CF_FILE_OK if the socket exists and is a socket.
894 * - CF_FILE_NO_EXIST if the file doesn't exist.
895 * - CF_FILE_NO_PERMISSION if the file exists but is not accessible.
896 * - CF_FILE_NO_UNIX_SOCKET if the file exists but is not a socket.
897 * - CF_FILE_OTHER_ERROR any other error.
898 */
900{
901 int fd;
903
904 struct sockaddr_un addr = { .sun_family = AF_UNIX };
905
907
908 if (talloc_strlen(filename) >= sizeof(addr.sun_path)) {
909 fr_strerror_printf("Socket path \"%s\" too long", filename);
910 return CF_FILE_OTHER_ERROR;
911 }
912
913 strlcpy(addr.sun_path, filename, sizeof(addr.sun_path));
914
915 fd = socket(AF_UNIX, SOCK_STREAM, 0);
916 if (fd < 0) {
917 fr_strerror_printf("Failed checking permissions for \"%s\": %s",
918 filename, fr_syserror(errno));
919 return CF_FILE_OTHER_ERROR;
920 }
921 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
922 fr_strerror_printf("Failed setting non-blocking mode for socket %s: %s",
923 filename, fr_syserror(errno));
924 close(fd);
925 return CF_FILE_OTHER_ERROR;
926 }
927
928 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
929 switch (errno) {
930 case EINPROGRESS: /* This is fine */
931 break;
932
933 case ENOENT:
934 fr_strerror_printf("Socket path \"%s\" does not exist", filename);
935 ret = CF_FILE_NO_EXIST;
936 break;
937
938 case EACCES:
939 fr_perm_file_error(errno);
940 fr_strerror_printf_push("Socket path \"%s\" exists but is not accessible", filename);
942 break;
943
944 case ENOTSOCK:
945 fr_strerror_printf("File \"%s\" is not a socket", filename);
947 break;
948
949 default:
950 fr_strerror_printf("Failed connecting to socket %s: %s", filename, fr_syserror(errno));
952 break;
953 }
954 }
955
956 close(fd);
957
958 return ret;
959}
960
961/** Check if file exists, and is a socket
962 *
963 * @param[in] filename CONF_PAIR for the unix socket path
964 * @param[in] uctx user context, not used
965 * @return
966 * - CF_FILE_OK if the socket exists and is a socket.
967 * - CF_FILE_NO_EXIST if the file doesn't exist.
968 * - CF_FILE_NO_PERMISSION if the file exists but is not accessible.
969 * - CF_FILE_NO_UNIX_SOCKET if the file exists but is not a socket.
970 * - CF_FILE_OTHER_ERROR any other error.
971 */
972cf_file_check_err_t cf_file_check_unix_perm(char const *filename, UNUSED void *uctx)
973{
974 struct stat buf;
975
977
978 if (stat(filename, &buf) < 0) {
979 switch (errno) {
980 case ENOENT:
981 fr_strerror_printf("Socket path \"%s\" does not exist", filename);
982 return CF_FILE_NO_EXIST;
983
984 case EPERM:
985 case EACCES:
986 fr_perm_file_error(errno);
987 fr_strerror_printf_push("Socket path \"%s\" exists but is not accessible: %s",
988 filename, fr_syserror(errno));
990
991 default:
992 fr_strerror_printf("Unable to stat socket \"%s\": %s", filename, fr_syserror(errno));
993 return CF_FILE_OTHER_ERROR;
994 }
995 }
996
997 if (!S_ISSOCK(buf.st_mode)) {
998 fr_strerror_printf("File \"%s\" is not a socket", filename);
1000 }
1001
1002 return CF_FILE_OK;
1003}
1004
1005/** Callback for cf_file_check to open a file and check permissions.
1006 *
1007 * This is used to check if a file exists, and is readable by the
1008 * unprivileged user/group.
1009 *
1010 * @param filename currently being processed.
1011 * @param uctx user context, which is a pointer to cf_file_t
1012 * @return
1013 * - CF_FILE_OK if the file exists and is readable.
1014 * - CF_FILE_NO_EXIST if the file does not exist.
1015 * - CF_FILE_NO_PERMISSION if the file exists but is not accessible.
1016 * - CF_FILE_OTHER_ERROR if there was any other error.
1017 */
1018cf_file_check_err_t cf_file_check_open_read(char const *filename, void *uctx)
1019{
1020 int fd;
1021 cf_file_t *file = uctx;
1022
1024
1025 fd = open(filename, O_RDONLY);
1026 if (fd < 0) {
1027 error:
1028 if (fd >= 0) close(fd);
1029
1030 switch (errno) {
1031 case ENOENT:
1032 fr_strerror_printf("File \"%s\" does not exist", filename);
1033 return CF_FILE_NO_EXIST;
1034
1035 case EPERM:
1036 case EACCES:
1037 fr_perm_file_error(errno);
1038 fr_strerror_printf_push("File \"%s\" exists but is not accessible: %s",
1039 filename, fr_syserror(errno));
1040 return CF_FILE_NO_PERMISSION;
1041
1042 default:
1043 fr_strerror_printf("Unable to open file \"%s\": %s", filename, fr_syserror(errno));
1044 return CF_FILE_OTHER_ERROR;
1045
1046 }
1047 }
1048
1049 if (file && fstat(fd, &file->buf) < 0) goto error;
1050
1051 close(fd);
1052 return CF_FILE_OK;
1053}
1054
1055/** Do some checks on the file as an "input" file. i.e. one read by a module.
1056 *
1057 * @note Must be called with super user privileges.
1058 *
1059 * @param cp currently being processed.
1060 * @param check_perms If true - will return error if file is world readable,
1061 * or not readable by the unprivileged user/group.
1062 * @return
1063 * - CF_FILE_OK if the socket exists and is a socket.
1064 * - CF_FILE_NO_EXIST if the file doesn't exist.
1065 * - CF_FILE_NO_PERMISSION if the file exists but is not accessible.
1066 * - CF_FILE_OTHER_ERROR any other error.
1067 */
1069{
1070 cf_file_t *file;
1071 CONF_SECTION *top;
1072 fr_rb_tree_t *tree;
1073 char const *filename = cf_pair_value(cp);
1075
1076 top = cf_root(cp);
1077 tree = cf_data_value(cf_data_find(top, fr_rb_tree_t, "filename"));
1078 if (!tree) return CF_FILE_OTHER_ERROR;
1079
1080 file = talloc(tree, cf_file_t);
1081 if (!file) return CF_FILE_OTHER_ERROR;
1082
1083 file->filename = talloc_strdup(file, filename); /* The rest of the code expects this to be talloced */
1085
1086 if (!check_perms) {
1087 if (stat(filename, &file->buf) < 0) {
1088 fr_perm_file_error(errno); /* Write error and euid/egid to error buff */
1089 cf_log_perr(cp, "Unable to open file \"%s\"", filename);
1090 error:
1092 return CF_FILE_OTHER_ERROR;
1093 }
1095 return CF_FILE_OK;
1096 }
1097
1098 /*
1099 * This really does seem to be the simplest way
1100 * to check that the file can be read with the
1101 * euid/egid.
1102 */
1104 if (ret < 0) {
1105 cf_log_perr(cp, "Permissions check failed");
1106 goto error;
1107 }
1108#ifdef S_IWOTH
1109 if ((file->buf.st_mode & S_IWOTH) != 0) {
1110 cf_log_perr(cp, "Configuration file %s is globally writable. "
1111 "Refusing to start due to insecure configuration.", filename);
1113 return CF_FILE_OTHER_ERROR;
1114 }
1115#endif
1116
1117 /*
1118 * It's OK to include the same file twice...
1119 */
1120 if (!fr_rb_insert(tree, file)) talloc_free(file);
1121
1122 return CF_FILE_OK;
1123}
1124
1125/*
1126 * Do variable expansion in pass2.
1127 *
1128 * This is a breadth-first expansion. "deep
1129 */
1131{
1132 cf_item_foreach(&cs->item, ci) {
1133 char const *value;
1134 CONF_PAIR *cp;
1135 char buffer[8192];
1136
1137 if (ci->type != CONF_ITEM_PAIR) continue;
1138
1139 cp = cf_item_to_pair(ci);
1140 if (!cp->value || !cp->pass2) continue;
1141
1142 fr_assert((cp->rhs_quote == T_BARE_WORD) ||
1143 (cp->rhs_quote == T_HASH) ||
1146
1147 value = cf_expand_variables(ci->filename, ci->lineno, cs, buffer, sizeof(buffer), cp->value, -1, NULL,
1148 (cp->rhs_quote != T_BARE_WORD));
1149 if (!value) return -1;
1150
1151 if (!*value) {
1152 ERROR("%s[%d]: Empty filename in $INCLUDE", ci->filename, ci->lineno);
1153 return -1;
1154 }
1155
1157 cp->value = talloc_strdup(cp, value);
1158 }
1159
1160 cf_item_foreach(&cs->item, ci) {
1161 if (ci->type != CONF_ITEM_SECTION) continue;
1162
1163 if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
1164 }
1165
1166 return 0;
1167}
1168
1169
1170static char const *cf_local_file(char const *base, char const *filename,
1171 char *buffer, size_t bufsize)
1172{
1173 size_t dirsize;
1174 char *p;
1175
1176 strlcpy(buffer, base, bufsize);
1177
1178 p = strrchr(buffer, FR_DIR_SEP);
1179 if (!p) return filename;
1180 if (p[1]) { /* ./foo */
1181 p[1] = '\0';
1182 }
1183
1184 dirsize = (p - buffer) + 1;
1185
1186 if ((dirsize + strlen(filename)) >= bufsize) {
1187 return NULL;
1188 }
1189
1190 strlcpy(p + 1, filename, bufsize - dirsize);
1191
1192 return buffer;
1193}
1194
1195/*
1196 * Like gettoken(), but uses the new API which seems better for a
1197 * host of reasons.
1198 */
1199static int cf_get_token(CONF_SECTION *parent, char const **ptr_p, fr_token_t *token, char *buffer, size_t buflen,
1200 char const *filename, int lineno)
1201{
1202 char const *ptr = *ptr_p;
1203 ssize_t slen;
1204 char const *out;
1205 size_t outlen;
1206
1207 /*
1208 * Discover the string content, returning what kind of
1209 * string it is.
1210 *
1211 * Don't allow casts or regexes. But do allow bare
1212 * %{...} expansions.
1213 */
1214 slen = tmpl_preparse(&out, &outlen, ptr, strlen(ptr), token);
1215 if (slen <= 0) {
1216 char *spaces, *text;
1217
1218 fr_canonicalize_error(parent, &spaces, &text, slen, ptr);
1219
1220 ERROR("%s[%d]: %s", filename, lineno, text);
1221 ERROR("%s[%d]: %s^ - %s", filename, lineno, spaces, fr_strerror());
1222
1224 talloc_free(text);
1225 return -1;
1226 }
1227
1228 if ((size_t) slen >= buflen) {
1229 ERROR("%s[%d]: Name is too long", filename, lineno);
1230 return -1;
1231 }
1232
1233 /*
1234 * Unescape it or copy it verbatim as necessary.
1235 */
1236 if (!cf_expand_variables(filename, lineno, parent, buffer, buflen,
1237 out, outlen, NULL, (*token != T_BARE_WORD))) {
1238 return -1;
1239 }
1240
1241 ptr += slen;
1242 fr_skip_whitespace(ptr);
1243
1244 *ptr_p = ptr;
1245 return 0;
1246}
1247
1252
1253static int8_t filename_cmp(void const *one, void const *two)
1254{
1255 int ret;
1256 cf_file_heap_t const *a = one;
1257 cf_file_heap_t const *b = two;
1258
1259 ret = strcmp(a->filename, b->filename);
1260 return CMP(ret, 0);
1261}
1262
1268
1270 { L("adoc"), CF_EXTENSION_TXT },
1271 { L("dpkg-dist"), CF_EXTENSION_PKG },
1272 { L("dpkg-old"), CF_EXTENSION_PKG },
1273 { L("md"), CF_EXTENSION_TXT },
1274 { L("rpmnew"), CF_EXTENSION_PKG },
1275 { L("rpmsave"), CF_EXTENSION_PKG },
1276};
1278
1279
1280static int process_include(cf_stack_t *stack, CONF_SECTION *parent, char const *ptr, bool required, bool relative)
1281{
1282 char const *value;
1283 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1284
1285 /*
1286 * Can't do this inside of update / map.
1287 */
1288 if (parent->unlang == CF_UNLANG_ASSIGNMENT) {
1289 ERROR("%s[%d]: Parse error: Invalid location for $INCLUDE",
1290 frame->filename, frame->lineno);
1291 return -1;
1292 }
1293
1294 fr_skip_whitespace(ptr);
1295
1296 /*
1297 * Grab all of the non-whitespace text.
1298 */
1299 value = ptr;
1300 while (*ptr && !isspace((uint8_t) *ptr)) ptr++;
1301
1302 /*
1303 * We're OK with whitespace after the filename.
1304 */
1305 fr_skip_whitespace(ptr);
1306
1307 /*
1308 * But anything else after the filename is wrong.
1309 */
1310 if (*ptr) {
1311 ERROR("%s[%d]: Unexpected text after $INCLUDE", frame->filename, frame->lineno);
1312 return -1;
1313 }
1314
1315 /*
1316 * Hack for ${confdir}/foo
1317 */
1318 if (*value == '$') relative = false;
1319
1320 value = cf_expand_variables(frame->filename, frame->lineno, parent, stack->buff[1], stack->bufsize,
1321 value, ptr - value, NULL, false);
1322 if (!value) return -1;
1323
1324 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
1325
1326 if (relative) {
1327 value = cf_local_file(frame->filename, value, stack->buff[2], stack->bufsize);
1328 if (!value) {
1329 ERROR("%s[%d]: Directories too deep", frame->filename, frame->lineno);
1330 return -1;
1331 }
1332 }
1333
1334 if (strchr(value, '*') != 0) {
1335#ifndef HAVE_GLOB_H
1336 ERROR("%s[%d]: Filename globbing is not supported.", frame->filename, frame->lineno);
1337 return -1;
1338#else
1339 stack->depth++;
1340 frame = &stack->frame[stack->depth];
1341 memset(frame, 0, sizeof(*frame));
1342
1343 frame->type = CF_STACK_GLOB;
1344 frame->required = required;
1345 frame->parent = parent;
1346 frame->current = parent;
1347
1348 /*
1349 * For better debugging.
1350 */
1351 frame->filename = frame[-1].filename;
1352 frame->lineno = frame[-1].lineno;
1353
1354 if (glob(value, GLOB_ERR | GLOB_NOESCAPE, NULL, &frame->glob) < 0) {
1355 stack->depth--;
1356 ERROR("%s[%d]: Failed expanding '%s' - %s", frame->filename, frame->lineno,
1357 value, fr_syserror(errno));
1358 return -1;
1359 }
1360
1361 /*
1362 * If nothing matches, that may be an error.
1363 */
1364 if (frame->glob.gl_pathc == 0) {
1365 if (!required) {
1366 stack->depth--;
1367 return 0;
1368 }
1369
1370 ERROR("%s[%d]: Failed expanding '%s' - No matching files", frame->filename, frame->lineno,
1371 value);
1372 return -1;
1373 }
1374
1375 return 1;
1376#endif
1377 }
1378
1379 /*
1380 * Allow $-INCLUDE for directories, too.
1381 */
1382 if (!required) {
1383 struct stat statbuf;
1384
1385 if (stat(value, &statbuf) < 0) {
1386 WARN("Not including file %s: %s", value, fr_syserror(errno));
1387 return 0;
1388 }
1389 }
1390
1391 /*
1392 * The filename doesn't end in '/', so it must be a file.
1393 */
1394 if (value[strlen(value) - 1] != '/') {
1395 if ((stack->depth + 1) >= MAX_STACK) {
1396 ERROR("%s[%d]: Directories too deep", frame->filename, frame->lineno);
1397 return -1;
1398 }
1399
1400 stack->depth++;
1401 frame = &stack->frame[stack->depth];
1402 memset(frame, 0, sizeof(*frame));
1403
1404 frame->type = CF_STACK_FILE;
1405 frame->fp = NULL;
1406 frame->parent = parent;
1407 frame->current = parent;
1408 frame->filename = talloc_strdup(frame->parent, value);
1409 return 1;
1410 }
1411
1412#ifdef HAVE_DIRENT_H
1413 /*
1414 * $INCLUDE foo/
1415 *
1416 * Include ALL non-"dot" files in the directory.
1417 * careful!
1418 */
1419 {
1420 char *directory;
1421 DIR *dir = NULL;
1422 struct dirent *dp;
1423 struct stat stat_buf;
1424 cf_file_heap_t *h;
1425#ifdef S_IWOTH
1426 int my_fd;
1427#endif
1428
1429 /*
1430 * We need to keep a copy of parent while the
1431 * included files mangle our buff[] array.
1432 */
1433 directory = talloc_strdup(parent, value);
1434
1435 if (DEBUG_ENABLED2) cf_log_debug(parent, "Including files in directory \"%s\"", directory);
1436
1437 dir = opendir(directory);
1438 if (!dir) {
1439 ERROR("%s[%d]: Error reading directory %s: %s",
1440 frame->filename, frame->lineno, value,
1441 fr_syserror(errno));
1442 error:
1443 if (dir) closedir(dir);
1444 talloc_free(directory);
1445 return -1;
1446 }
1447
1448#ifdef S_IWOTH
1449 my_fd = dirfd(dir);
1450 fr_assert(my_fd >= 0);
1451
1452 /*
1453 * Security checks.
1454 */
1455 if (fstat(my_fd, &stat_buf) < 0) {
1456 ERROR("%s[%d]: Failed reading directory %s: %s", frame->filename, frame->lineno,
1457 directory, fr_syserror(errno));
1458 goto error;
1459 }
1460
1461 if ((stat_buf.st_mode & S_IWOTH) != 0) {
1462 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
1463 "insecure configuration", frame->filename, frame->lineno, directory);
1464 goto error;
1465 }
1466#endif
1467
1468 /*
1469 * Directory plus next filename.
1470 */
1471 if ((stack->depth + 2) >= MAX_STACK) {
1472 ERROR("%s[%d]: Directories too deep", frame->filename, frame->lineno);
1473 goto error;
1474 }
1475
1476 stack->depth++;
1477 frame = &stack->frame[stack->depth];
1478 *frame = (cf_stack_frame_t){
1479 .type = CF_STACK_DIR,
1480 .directory = directory,
1481 .parent = parent,
1482 .current = parent,
1483 .from_dir = true
1484 };
1485
1486 MEM(frame->heap = fr_heap_alloc(frame->directory, filename_cmp, cf_file_heap_t, heap_id, 0));
1487
1488 /*
1489 * Read the whole directory before loading any
1490 * individual file. We stat() files to ensure
1491 * that they're readable. We ignore
1492 * subdirectories and files with odd filenames.
1493 */
1494 while ((dp = readdir(dir)) != NULL) {
1495 char const *p, *ext = NULL;
1496
1497 if (dp->d_name[0] == '.') continue;
1498
1499 /*
1500 * Check for valid characters
1501 */
1502 for (p = dp->d_name; *p != '\0'; p++) {
1503 if (isalpha((uint8_t) *p) ||
1504 isdigit((uint8_t) *p) ||
1505 (*p == '-') ||
1506 (*p == '_')) continue;
1507
1508 if (*p == '.') {
1509 ext = p;
1510 continue;
1511 }
1512 break;
1513 }
1514 if (*p != '\0') continue;
1515
1516 /*
1517 * Ignore config files generated by deb / rpm packaging updates, and
1518 * documentation files.
1519 */
1520 if (ext) switch (fr_table_value_by_str(cf_file_extensions, ext + 1, CF_EXTENSION_READ)) {
1521 case CF_EXTENSION_READ:
1522 break;
1523
1524 case CF_EXTENSION_PKG:
1525 WARN("Ignoring packaging system produced file %s%s", frame->directory, dp->d_name);
1526 continue;
1527
1528 case CF_EXTENSION_TXT:
1529 continue;
1530 }
1531
1532 snprintf(stack->buff[1], stack->bufsize, "%s%s",
1533 frame->directory, dp->d_name);
1534
1535 if (stat(stack->buff[1], &stat_buf) != 0) {
1536 ERROR("%s[%d]: Failed checking file %s: %s",
1537 (frame - 1)->filename, (frame - 1)->lineno,
1538 stack->buff[1], fr_syserror(errno));
1539 continue;
1540 }
1541
1542 if (S_ISDIR(stat_buf.st_mode)) {
1543 WARN("%s[%d]: Ignoring directory %s",
1544 (frame - 1)->filename, (frame - 1)->lineno,
1545 stack->buff[1]);
1546 continue;
1547 }
1548
1549 MEM(h = talloc_zero(frame->heap, cf_file_heap_t));
1550 MEM(h->filename = talloc_strdup(h, stack->buff[1]));
1551 h->heap_id = FR_HEAP_INDEX_INVALID;
1552 (void) fr_heap_insert(&frame->heap, h);
1553 }
1554
1555 closedir(dir);
1556 return 1;
1557 }
1558#else
1559 ERROR("%s[%d]: Error including %s: No support for directories!",
1560 frame->filename, frame->lineno, value);
1561 return -1;
1562#endif
1563}
1564
1565
1567{
1568 CONF_ITEM *ci;
1569 CONF_SECTION *parent_cs, *templatecs;
1570 fr_token_t token;
1571 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1572 CONF_SECTION *parent = frame->current;
1573
1574 token = getword(&stack->ptr, stack->buff[2], stack->bufsize, true);
1575 if (token != T_EOL) {
1576 ERROR("%s[%d]: Unexpected text after $TEMPLATE", frame->filename, frame->lineno);
1577 return -1;
1578 }
1579
1580 if (!parent) {
1581 ERROR("%s[%d]: Internal sanity check error in template reference", frame->filename, frame->lineno);
1582 return -1;
1583 }
1584
1585 if (parent->template) {
1586 ERROR("%s[%d]: Section already has a template", frame->filename, frame->lineno);
1587 return -1;
1588 }
1589
1590 /*
1591 * Allow in-line templates.
1592 */
1593 templatecs = cf_section_find(cf_item_to_section(cf_parent(parent)), "template", stack->buff[2]);
1594 if (templatecs) {
1595 parent->template = templatecs;
1596 return 0;
1597 }
1598
1599 parent_cs = cf_root(parent);
1600
1601 templatecs = cf_section_find(parent_cs, "templates", NULL);
1602 if (!templatecs) {
1603 ERROR("%s[%d]: Cannot find template \"%s\", as no 'templates' section exists.",
1604 frame->filename, frame->lineno, stack->buff[2]);
1605 return -1;
1606 }
1607
1608 ci = cf_reference_item(parent_cs, templatecs, stack->buff[2]);
1609 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1610 PERROR("%s[%d]: Failed finding item \"%s\" in the 'templates' section.",
1611 frame->filename, frame->lineno, stack->buff[2]);
1612 return -1;
1613 }
1614
1615 parent->template = cf_item_to_section(ci);
1616 return 0;
1617}
1618
1619
1620static int cf_file_fill(cf_stack_t *stack);
1621
1622
1624 ['{'] = true,
1625};
1626
1628 [0] = true,
1629
1630 ['\r'] = true,
1631 ['\n'] = true,
1632
1633 ['#'] = true,
1634 [','] = true,
1635 [';'] = true,
1636 ['}'] = true,
1637};
1638
1640{
1641 ssize_t slen = 0;
1642 CONF_SECTION *cs;
1643 uint8_t const *p;
1644 char const *ptr = stack->ptr;
1645 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1646 CONF_SECTION *parent = frame->current;
1647 char *buff[4];
1648
1649 /*
1650 * Short names are nicer.
1651 */
1652 buff[1] = stack->buff[1];
1653 buff[2] = stack->buff[2];
1654 buff[3] = stack->buff[3];
1655
1656 /*
1657 * Create the CONF_SECTION. We don't pass a name2, as it
1658 * hasn't yet been parsed.
1659 */
1660 cs = cf_section_alloc(parent, parent, buff[1], NULL);
1661 if (!cs) {
1662 cf_log_err(parent, "Failed allocating memory for section");
1663 return NULL;
1664 }
1665 cf_filename_set(cs, frame->filename);
1666 cf_lineno_set(cs, frame->lineno);
1667
1668 /*
1669 * Keep "parsing" the condition until we hit EOL.
1670 *
1671 *
1672 */
1673 while (true) {
1674 int rcode;
1675 bool eol;
1676
1677 /*
1678 * Try to parse the condition. We can have a parse success, or a parse failure.
1679 */
1680 slen = fr_skip_condition(ptr, NULL, terminal_end_section, &eol);
1681
1682 /*
1683 * Parse success means we stop reading more data.
1684 */
1685 if (slen > 0) break;
1686
1687 /*
1688 * Parse failures not at EOL are real errors.
1689 */
1690 if (!eol) {
1691 slen = 0;
1692 fr_strerror_const("Unexpected EOF");
1693 error:
1694 cf_canonicalize_error(cs, slen, "Parse error in condition", ptr);
1695 talloc_free(cs);
1696 return NULL;
1697 }
1698
1699 /*
1700 * Parse failures at EOL means that we read more data.
1701 */
1702 p = (uint8_t const *) ptr + (-slen);
1703
1704 /*
1705 * Auto-continue across CR LF until we reach the
1706 * end of the string. We mash everything into one line.
1707 */
1708 if (*p && (*p < ' ')) {
1709 while ((*p == '\r') || (*p == '\n')) {
1710 char *q;
1711
1712 q = UNCONST(char *, p);
1713 *q = ' ';
1714 p++;
1715 continue;
1716 }
1717
1718 /*
1719 * Hopefully the next line is already in
1720 * the buffer, and we don't have to read
1721 * more data.
1722 */
1723 continue;
1724 }
1725
1726 /*
1727 * Anything other than EOL is a problem at this point.
1728 */
1729 if (*p) {
1730 fr_strerror_const("Unexpected text after condition");
1731 goto error;
1732 }
1733
1734 /*
1735 * We hit EOL, so the parse error is really "read more data".
1736 */
1737 stack->fill = UNCONST(char *, p);
1738 rcode = cf_file_fill(stack);
1739 if (rcode < 0) {
1740 cf_log_err(cs, "Failed parsing condition");
1741 return NULL;
1742 }
1743 }
1744
1745 fr_assert((size_t) slen < (stack->bufsize - 1));
1746
1747 ptr += slen;
1748 fr_skip_whitespace(ptr);
1749
1750 if (*ptr != '{') {
1751 cf_log_err(cs, "Expected '{' instead of %s", ptr);
1752 talloc_free(cs);
1753 return NULL;
1754 }
1755 ptr++;
1756
1757 /*
1758 * Save the parsed condition (minus trailing whitespace)
1759 * into a buffer.
1760 */
1761 memcpy(buff[2], stack->ptr, slen);
1762 buff[2][slen] = '\0';
1763
1764 while (slen > 0) {
1765 if (!isspace((uint8_t) buff[2][slen - 1])) break;
1766
1767 buff[2][slen - 1] = '\0';
1768 slen--;
1769 }
1770
1771 /*
1772 * Expand the variables in the pre-parsed condition.
1773 */
1774 if (!cf_expand_variables(frame->filename, frame->lineno, parent,
1775 buff[3], stack->bufsize, buff[2], slen, NULL, true)) {
1776 fr_strerror_const("Failed expanding configuration variable");
1777 return NULL;
1778 }
1779
1780 MEM(cs->name2 = talloc_strdup(cs, buff[3]));
1782
1783 stack->ptr = ptr;
1784
1785 cs->allow_locals = true;
1786 cs->unlang = CF_UNLANG_ALLOW;
1787 return cf_section_to_item(cs);
1788}
1789
1791{
1792 char const *mod;
1793 char const *value = NULL;
1794 CONF_SECTION *css;
1795 fr_token_t token;
1796 char const *ptr = stack->ptr;
1797 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1798 CONF_SECTION *parent = frame->current;
1799 char *buff[4];
1800
1801 /*
1802 * Short names are nicer.
1803 */
1804 buff[1] = stack->buff[1];
1805 buff[2] = stack->buff[2];
1806
1807 if (cf_get_token(parent, &ptr, &token, buff[1], stack->bufsize,
1808 frame->filename, frame->lineno) < 0) {
1809 return NULL;
1810 }
1811
1812 if (token != T_BARE_WORD) {
1813 ERROR("%s[%d]: Invalid syntax for 'map' - module name must not be a quoted string",
1814 frame->filename, frame->lineno);
1815 return NULL;
1816 }
1817 mod = buff[1];
1818
1819 /*
1820 * Maps without an expansion string are allowed, though
1821 * it's not clear why.
1822 */
1823 if (*ptr == '{') {
1824 ptr++;
1825 goto alloc_section;
1826 }
1827
1828 /*
1829 * Now get the expansion string.
1830 */
1831 if (cf_get_token(parent, &ptr, &token, buff[2], stack->bufsize,
1832 frame->filename, frame->lineno) < 0) {
1833 return NULL;
1834 }
1835 if (!fr_str_tok[token]) {
1836 ERROR("%s[%d]: Expecting string expansions in 'map' definition",
1837 frame->filename, frame->lineno);
1838 return NULL;
1839 }
1840
1841 if (*ptr != '{') {
1842 ERROR("%s[%d]: Expecting section start brace '{' in 'map' definition",
1843 frame->filename, frame->lineno);
1844 return NULL;
1845 }
1846 ptr++;
1847 value = buff[2];
1848
1849 /*
1850 * Allocate the section
1851 */
1852alloc_section:
1853 css = cf_section_alloc(parent, parent, "map", mod);
1854 if (!css) {
1855 ERROR("%s[%d]: Failed allocating memory for section",
1856 frame->filename, frame->lineno);
1857 return NULL;
1858 }
1859 cf_filename_set(css, frame->filename);
1860 cf_lineno_set(css, frame->lineno);
1861 css->name2_quote = T_BARE_WORD;
1862
1863 css->argc = 0;
1864 if (value) {
1865 css->argv = talloc_array(css, char const *, 1);
1866 css->argv[0] = talloc_strdup(css->argv, value);
1867 css->argv_quote = talloc_array(css, fr_token_t, 1);
1868 css->argv_quote[0] = token;
1869 css->argc++;
1870 }
1871 stack->ptr = ptr;
1873
1874 return cf_section_to_item(css);
1875}
1876
1877
1879{
1880 char const *mod = NULL;
1881 CONF_SECTION *css;
1882 fr_token_t token;
1883 char const *ptr = stack->ptr;
1884 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1885 CONF_SECTION *parent = frame->current;
1886 char *buff[4];
1887 int values = 0;
1888
1889 /*
1890 * Short names are nicer.
1891 */
1892 buff[1] = stack->buff[1];
1893 buff[2] = stack->buff[2];
1894 buff[3] = stack->buff[3];
1895
1896 /*
1897 * subrequest { ... } is allowed.
1898 */
1899 fr_skip_whitespace(ptr);
1900 if (*ptr == '{') {
1901 ptr++;
1902 goto alloc_section;
1903 }
1904
1905 /*
1906 * Get the name of the Packet-Type.
1907 */
1908 if (cf_get_token(parent, &ptr, &token, buff[1], stack->bufsize,
1909 frame->filename, frame->lineno) < 0) {
1910 return NULL;
1911 }
1912
1913 mod = buff[1];
1914
1915 /*
1916 * subrequest Access-Request { ... } is allowed.
1917 */
1918 if (*ptr == '{') {
1919 ptr++;
1920 goto alloc_section;
1921 }
1922
1923 /*
1924 * subrequest Access-Request &foo { ... }
1925 */
1926 if (cf_get_token(parent, &ptr, &token, buff[2], stack->bufsize,
1927 frame->filename, frame->lineno) < 0) {
1928 return NULL;
1929 }
1930
1931 if (token != T_BARE_WORD) {
1932 ERROR("%s[%d]: The second argument to 'subrequest' must be an attribute reference",
1933 frame->filename, frame->lineno);
1934 return NULL;
1935 }
1936 values++;
1937
1938 if (*ptr == '{') {
1939 ptr++;
1940 goto alloc_section;
1941 }
1942
1943 /*
1944 * subrequest Access-Request &foo &bar { ... }
1945 */
1946 if (cf_get_token(parent, &ptr, &token, buff[3], stack->bufsize,
1947 frame->filename, frame->lineno) < 0) {
1948 return NULL;
1949 }
1950
1951 if (token != T_BARE_WORD) {
1952 ERROR("%s[%d]: The third argument to 'subrequest' must be an attribute reference",
1953 frame->filename, frame->lineno);
1954 return NULL;
1955 }
1956 values++;
1957
1958 if (*ptr != '{') {
1959 ERROR("%s[%d]: Expecting section start brace '{' in 'subrequest' definition",
1960 frame->filename, frame->lineno);
1961 return NULL;
1962 }
1963 ptr++;
1964
1965 /*
1966 * Allocate the section
1967 */
1968alloc_section:
1969 css = cf_section_alloc(parent, parent, "subrequest", mod);
1970 if (!css) {
1971 ERROR("%s[%d]: Failed allocating memory for section",
1972 frame->filename, frame->lineno);
1973 return NULL;
1974 }
1975 cf_filename_set(css, frame->filename);
1976 cf_lineno_set(css, frame->lineno);
1977 if (mod) css->name2_quote = T_BARE_WORD;
1978
1979 css->argc = values;
1980 if (values) {
1981 int i;
1982
1983 css->argv = talloc_array(css, char const *, values);
1984 css->argv_quote = talloc_array(css, fr_token_t, values);
1985
1986 for (i = 0; i < values; i++) {
1987 css->argv[i] = talloc_strdup(css->argv, buff[2 + i]);
1988 css->argv_quote[i] = T_BARE_WORD;
1989 }
1990 }
1991
1992 stack->ptr = ptr;
1993
1994 css->allow_locals = true;
1995 css->unlang = CF_UNLANG_ALLOW;
1996 return cf_section_to_item(css);
1997}
1998
2000{
2001 CONF_SECTION *css;
2002 int argc = 0;
2003 char const *ptr = stack->ptr;
2004 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2005 CONF_SECTION *parent = frame->current;
2006 char *name2 = NULL;
2007 char *argv[RLM_MODULE_NUMCODES];
2008
2009 while (true) {
2010 char const *p;
2011 size_t len;
2012
2013 fr_skip_whitespace(ptr);
2014
2015 /*
2016 * We have an open bracket, it's the end of the "catch" statement.
2017 */
2018 if (*ptr == '{') {
2019 ptr++;
2020 break;
2021 }
2022
2023 /*
2024 * The arguments have to be short, unquoted words.
2025 */
2026 p = ptr;
2027 while (isalpha((uint8_t) *ptr)) ptr++;
2028
2029 len = ptr - p;
2030 if (len > 16) {
2031 ERROR("%s[%d]: Invalid syntax for 'catch' - unknown rcode '%s'",
2032 frame->filename, frame->lineno, p);
2033 return NULL;
2034 }
2035
2036 if ((*ptr != '{') && !isspace((uint8_t) *ptr)) {
2037 ERROR("%s[%d]: Invalid syntax for 'catch' - unexpected text at '%s'",
2038 frame->filename, frame->lineno, ptr);
2039 return NULL;
2040 }
2041
2042 if (!name2) {
2043 name2 = talloc_strndup(NULL, p, len);
2044 continue;
2045 }
2046
2047 if (argc >= RLM_MODULE_NUMCODES) {
2048 ERROR("%s[%d]: Invalid syntax for 'catch' - too many arguments at'%s'",
2049 frame->filename, frame->lineno, ptr);
2050 talloc_free(name2);
2051 return NULL;
2052 }
2053
2054 argv[argc++] = talloc_strndup(name2, p, len);
2055 }
2056
2057 css = cf_section_alloc(parent, parent, "catch", name2);
2058 if (!css) {
2059 talloc_free(name2);
2060 ERROR("%s[%d]: Failed allocating memory for section",
2061 frame->filename, frame->lineno);
2062 return NULL;
2063 }
2064 cf_filename_set(css, frame->filename);
2065 cf_lineno_set(css, frame->lineno);
2066 css->name2_quote = T_BARE_WORD;
2067 css->unlang = CF_UNLANG_ALLOW;
2068
2069 css->argc = argc;
2070 if (argc) {
2071 int i;
2072
2073 css->argv = talloc_array(css, char const *, argc + 1);
2074 css->argv_quote = talloc_array(css, fr_token_t, argc);
2075 css->argc = argc;
2076
2077 for (i = 0; i < argc; i++) {
2078 css->argv[i] = talloc_strdup(css->argv, argv[i]);
2079 css->argv_quote[i] = T_BARE_WORD;
2080 }
2081
2082 css->argv[argc] = NULL;
2083 }
2084 talloc_free(name2);
2085
2086 stack->ptr = ptr;
2087
2088 return cf_section_to_item(css);
2089}
2090
2091static int parse_error(cf_stack_t *stack, char const *ptr, char const *message)
2092{
2093 char *spaces, *text;
2094 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2095
2096 if (!ptr) ptr = stack->ptr;
2097
2098 /*
2099 * We must pass a _negative_ offset to this function.
2100 */
2101 fr_canonicalize_error(NULL, &spaces, &text, stack->ptr - ptr, stack->ptr);
2102
2103 ERROR("%s[%d]: %s", frame->filename, frame->lineno, text);
2104 ERROR("%s[%d]: %s^ - %s", frame->filename, frame->lineno, spaces, message);
2105
2107 talloc_free(text);
2108 return -1;
2109}
2110
2111static int parse_type_name(cf_stack_t *stack, char const **ptr_p, char const *type_ptr, fr_type_t *type_p)
2112{
2114 fr_token_t token;
2115 char const *ptr = *ptr_p;
2116 char const *ptr2;
2117
2118 /*
2119 * Parse an explicit type.
2120 */
2122 switch (type) {
2123 default:
2124 break;
2125
2126 case FR_TYPE_NULL:
2127 case FR_TYPE_VOID:
2128 case FR_TYPE_VALUE_BOX:
2129 case FR_TYPE_MAX:
2130 (void) parse_error(stack, type_ptr, "Unknown or invalid variable type in 'foreach'");
2131 return -1;
2132 }
2133
2134 fr_skip_whitespace(ptr);
2135 ptr2 = ptr;
2136
2137 /*
2138 * Parse the variable name. @todo - allow '-' in names.
2139 */
2140 token = gettoken(&ptr, stack->buff[2], stack->bufsize, false);
2141 if (token != T_BARE_WORD) {
2142 (void) parse_error(stack, ptr2, "Invalid variable name for key in 'foreach'");
2143 return -1;
2144 }
2145 fr_skip_whitespace(ptr);
2146
2147 *ptr_p = ptr;
2148 *type_p = type;
2149
2150 return 0;
2151}
2152
2153/*
2154 * foreach &User-Name { - old and deprecated
2155 *
2156 * foreach value (...) { - automatically define variable
2157 *
2158 * foreach string value ( ...) { - data type for variable
2159 *
2160 * foreach string key, type value (..) { - key is "string", value is as above
2161 */
2163{
2164 fr_token_t token;
2166 CONF_SECTION *css;
2167 char const *ptr = stack->ptr, *ptr2, *type_ptr;
2168 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2169 CONF_SECTION *parent = frame->current;
2170
2171 css = cf_section_alloc(parent, parent, "foreach", NULL);
2172 if (!css) {
2173 ERROR("%s[%d]: Failed allocating memory for section",
2174 frame->filename, frame->lineno);
2175 return NULL;
2176 }
2177
2178 cf_filename_set(css, frame->filename);
2179 cf_lineno_set(css, frame->lineno);
2180 css->name2_quote = T_BARE_WORD;
2181 css->unlang = CF_UNLANG_ALLOW;
2182 css->allow_locals = true;
2183
2184 /*
2185 * Get the first argument to "foreach". For backwards
2186 * compatibility, it could be an attribute reference.
2187 */
2188 type_ptr = ptr;
2189 if (cf_get_token(parent, &ptr, &token, stack->buff[1], stack->bufsize,
2190 frame->filename, frame->lineno) < 0) {
2191 return NULL;
2192 }
2193
2194 if (token != T_BARE_WORD) {
2195 invalid_argument:
2196 (void) parse_error(stack, type_ptr, "Unexpected argument to 'foreach'");
2197 return NULL;
2198 }
2199
2200 fr_skip_whitespace(ptr);
2201
2202 /*
2203 * foreach foo { ...
2204 *
2205 * Deprecated and don't use.
2206 */
2207 if (*ptr == '{') {
2208 css->name2 = talloc_strdup(css, stack->buff[1]);
2209
2210 ptr++;
2211 stack->ptr = ptr;
2212
2213 cf_log_warn(css, "Using deprecated syntax. Please use the new 'foreach' syntax.");
2214 return cf_section_to_item(css);
2215 }
2216
2217 fr_skip_whitespace(ptr);
2218
2219 /*
2220 * foreach value (...) {
2221 */
2222 if (*ptr == '(') {
2224 strcpy(stack->buff[2], stack->buff[1]); /* so that we can parse expression in buff[1] */
2225 goto alloc_argc_2;
2226 }
2227
2228 /*
2229 * on input, type name is in stack->buff[1]
2230 * on output, variable name is in stack->buff[2]
2231 */
2232 if (parse_type_name(stack, &ptr, type_ptr, &type) < 0) return NULL;
2233
2234 /*
2235 * if we now have an expression block, then just have variable type / name.
2236 */
2237 if (*ptr == '(') goto alloc_argc_2;
2238
2239 /*
2240 * There's a comma. the first "type name" is for the key. We skip the comma, and parse the
2241 * second "type name" as being for the value.
2242 *
2243 * foreach type key, type value (...)
2244 */
2245 if (*ptr == ',') {
2246 /*
2247 * We have 4 arguments, [var-type, var-name, key-type, key-name]
2248 *
2249 * We don't really care about key-type, but we might care later.
2250 */
2251 css->argc = 4;
2252 css->argv = talloc_array(css, char const *, css->argc);
2253 css->argv_quote = talloc_array(css, fr_token_t, css->argc);
2254
2255 css->argv[2] = fr_type_to_str(type);
2256 css->argv_quote[2] = T_BARE_WORD;
2257
2258 css->argv[3] = talloc_strdup(css->argv, stack->buff[2]);
2259 css->argv_quote[3] = T_BARE_WORD;
2260
2261 ptr++;
2262 fr_skip_whitespace(ptr);
2263 type_ptr = ptr;
2264
2265 /*
2266 * Now parse "type value"
2267 */
2268 token = gettoken(&ptr, stack->buff[1], stack->bufsize, false);
2269 if (token != T_BARE_WORD) goto invalid_argument;
2270
2271 if (parse_type_name(stack, &ptr, type_ptr, &type) < 0) return NULL;
2272
2273 if (!fr_type_is_leaf(type)) {
2274 (void) parse_error(stack, type_ptr, "Invalid data type for 'key' variable");
2275 return NULL;
2276 }
2277 }
2278
2279 /*
2280 * The thing to loop over must now be in an expression block.
2281 */
2282 if (*ptr != '(') {
2283 (void) parse_error(stack, ptr, "Expected (...) after 'foreach' variable definition");
2284 return NULL;
2285 }
2286
2287 goto parse_expression;
2288
2289alloc_argc_2:
2290 css->argc = 2;
2291 css->argv = talloc_array(css, char const *, css->argc);
2292 css->argv_quote = talloc_array(css, fr_token_t, css->argc);
2293
2294
2295parse_expression:
2296 /*
2297 * "(" whitespace EXPRESSION whitespace ")"
2298 */
2299 ptr++;
2300 fr_skip_whitespace(ptr);
2301 ptr2 = ptr;
2302
2303 if (cf_get_token(parent, &ptr, &token, stack->buff[1], stack->bufsize,
2304 frame->filename, frame->lineno) < 0) {
2305 return NULL;
2306 }
2307
2308 /*
2309 * We can do &foo[*] or %func(...), but not "...".
2310 */
2311 if (token != T_BARE_WORD) {
2312 (void) parse_error(stack, ptr2, "Invalid reference in 'foreach'");
2313 return NULL;
2314 }
2315
2316 fr_skip_whitespace(ptr);
2317 if (*ptr != ')') {
2318 (void) parse_error(stack, ptr, "Missing ')' in 'foreach'");
2319 return NULL;
2320 }
2321 ptr++;
2322 fr_skip_whitespace(ptr);
2323
2324 if (*ptr != '{') {
2325 (void) parse_error(stack, ptr, "Expected '{' in 'foreach'");
2326 return NULL;
2327 }
2328
2329 css->name2 = talloc_strdup(css, stack->buff[1]);
2330
2331 /*
2332 * Add in the extra arguments
2333 */
2334 css->argv[0] = fr_type_to_str(type);
2335 css->argv_quote[0] = T_BARE_WORD;
2336
2337 css->argv[1] = talloc_strdup(css->argv, stack->buff[2]);
2338 css->argv_quote[1] = T_BARE_WORD;
2339
2340 ptr++;
2341 stack->ptr = ptr;
2342
2343 return cf_section_to_item(css);
2344}
2345
2346
2347static int add_pair(CONF_SECTION *parent, char const *attr, char const *value,
2348 fr_token_t name1_token, fr_token_t op_token, fr_token_t value_token,
2349 char *buff, char const *filename, int lineno)
2350{
2351 CONF_DATA const *cd;
2352 conf_parser_t *rule;
2353 CONF_PAIR *cp;
2354 bool pass2 = false;
2355
2356 /*
2357 * If we have the value, expand any configuration
2358 * variables in it.
2359 */
2360 if (value && *value) {
2361 bool soft_fail;
2362 char const *expanded;
2363
2364 expanded = cf_expand_variables(filename, lineno, parent, buff, talloc_array_length(buff), value, -1, &soft_fail,
2365 (value_token != T_BARE_WORD));
2366 if (expanded) {
2367 value = expanded;
2368
2369 } else if (!soft_fail) {
2370 return -1;
2371
2372 } else {
2373 /*
2374 * References an item which doesn't exist,
2375 * or which is already marked up as being
2376 * expanded in pass2. Wait for pass2 to
2377 * do the expansions.
2378 *
2379 * Leave the input value alone.
2380 */
2381 pass2 = true;
2382 }
2383 }
2384
2385 cp = cf_pair_alloc(parent, attr, value, op_token, name1_token, value_token);
2386 if (!cp) return -1;
2387 cf_filename_set(cp, filename);
2388 cf_lineno_set(cp, lineno);
2389 cp->pass2 = pass2;
2390
2392 if (!cd) return 0;
2393
2394 rule = cf_data_value(cd);
2395 if (!rule->on_read) return 0;
2396
2397 /*
2398 * Do the on_read callback after adding the value.
2399 */
2400 return rule->on_read(parent, NULL, NULL, cf_pair_to_item(cp), rule);
2401}
2402
2403/*
2404 * switch (cast) foo {
2405 */
2407{
2408 size_t match_len;
2410 fr_token_t name2_quote = T_BARE_WORD;
2411 CONF_SECTION *css;
2412 char const *ptr = stack->ptr;
2413 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2414 CONF_SECTION *parent = frame->current;
2415
2416 fr_skip_whitespace(ptr);
2417 if (*ptr == '(') {
2418 char const *start;
2419
2420 ptr++;
2421 start = ptr;
2422
2423 while (isalpha(*ptr)) ptr++;
2424
2425 if (*ptr != ')') {
2426 ERROR("%s[%d]: Missing ')' in cast",
2427 frame->filename, frame->lineno);
2428 return NULL;
2429 }
2430
2432 start, ptr - start, FR_TYPE_MAX);
2433 if (type == FR_TYPE_MAX) {
2434 ERROR("%s[%d]: Unknown data type '%.*s' in cast",
2435 frame->filename, frame->lineno, (int) (ptr - start), start);
2436 return NULL;
2437 }
2438
2439 if (!fr_type_is_leaf(type)) {
2440 ERROR("%s[%d]: Invalid data type '%.*s' in cast",
2441 frame->filename, frame->lineno, (int) (ptr - start), start);
2442 return NULL;
2443 }
2444
2445 ptr++;
2446 fr_skip_whitespace(ptr);
2447 }
2448
2449 /*
2450 * Get the argument to the switch statement
2451 */
2452 if (cf_get_token(parent, &ptr, &name2_quote, stack->buff[1], stack->bufsize,
2453 frame->filename, frame->lineno) < 0) {
2454 return NULL;
2455 }
2456
2457 css = cf_section_alloc(parent, parent, "switch", NULL);
2458 if (!css) {
2459 ERROR("%s[%d]: Failed allocating memory for section",
2460 frame->filename, frame->lineno);
2461 return NULL;
2462 }
2463
2464 cf_filename_set(css, frame->filename);
2465 cf_lineno_set(css, frame->lineno);
2466 css->name2_quote = name2_quote;
2467 css->unlang = CF_UNLANG_ALLOW;
2468 css->allow_locals = true;
2469
2470 fr_skip_whitespace(ptr);
2471
2472 if (*ptr != '{') {
2473 (void) parse_error(stack, ptr, "Expected '{' in 'switch'");
2474 return NULL;
2475 }
2476
2477 css->name2 = talloc_strdup(css, stack->buff[1]);
2478
2479 /*
2480 * Add in the extra argument.
2481 */
2482 if (type != FR_TYPE_NULL) {
2483 css->argc = 1;
2484 css->argv = talloc_array(css, char const *, css->argc);
2485 css->argv_quote = talloc_array(css, fr_token_t, css->argc);
2486
2487 css->argv[0] = fr_type_to_str(type);
2488 css->argv_quote[0] = T_BARE_WORD;
2489 }
2490
2491 ptr++;
2492 stack->ptr = ptr;
2493
2494 return cf_section_to_item(css);
2495}
2496
2497
2499 { L("catch"), (void *) process_catch },
2500 { L("elsif"), (void *) process_if },
2501 { L("foreach"), (void *) process_foreach },
2502 { L("if"), (void *) process_if },
2503 { L("map"), (void *) process_map },
2504 { L("subrequest"), (void *) process_subrequest },
2505 { L("switch"), (void *) process_switch }
2506};
2508
2509typedef CONF_ITEM *(*cf_process_func_t)(cf_stack_t *);
2510
2511/*
2512 * This is fine. Don't complain.
2513 */
2514#ifdef __clang__
2515#pragma clang diagnostic ignored "-Wgnu-designator"
2516#endif
2517
2518/** Convert tokens back to a quoting character
2519 *
2520 * Non-string types convert to '?' to screw ups can be identified easily
2521 */
2522static const bool cf_name_char1[SBUFF_CHAR_CLASS] = {
2523 [ '0' ... '9' ] = true,
2524 [ 'A' ... 'Z' ] = true,
2525 [ 'a' ... 'z' ] = true,
2526 [ '%' ] = true, // %function() in unlang
2527 [ '@' ] = true, // @policy
2528 [ '-' ] = true, // -sql, but probably only in 'unlang'
2529 [ '&' ] = true, // legacy stuff.
2530};
2531
2532
2534{
2535 fr_token_t name1_token, name2_token, value_token, op_token;
2536 char const *value;
2537 CONF_SECTION *css;
2538 char const *ptr = stack->ptr;
2539 char const *name1_ptr, *name2_ptr, *op_ptr;
2540 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2541 CONF_SECTION *parent = frame->current;
2542 char *buff[4];
2543 cf_process_func_t process;
2544
2545 /*
2546 * Short names are nicer.
2547 */
2548 buff[0] = stack->buff[0];
2549 buff[1] = stack->buff[1];
2550 buff[2] = stack->buff[2];
2551 buff[3] = stack->buff[3];
2552
2553 fr_assert(parent != NULL);
2554
2555 /*
2556 * Catch end of a subsection.
2557 *
2558 * frame->current is the new thing we just created.
2559 * frame->parent is the parent of the current frame
2560 * frame->at_reference is the original frame->current, before the @reference
2561 * parent is the parent we started with when we started this section.
2562 */
2563 if (*ptr == '}') {
2564 /*
2565 * No pushed braces means that we're already in
2566 * the parent section which loaded this file. We
2567 * cannot go back up another level.
2568 *
2569 * This limitation means that we cannot put half
2570 * of a CONF_SECTION in one file, and then the
2571 * second half in another file. That's fine.
2572 */
2573 if (frame->braces == 0) {
2574 return parse_error(stack, ptr, "Too many closing braces");
2575 }
2576
2577 /*
2578 * Reset the current and parent to the original
2579 * section, before we were parsing the
2580 * @reference.
2581 */
2582 if (frame->at_reference && (frame->braces == frame->at_reference_braces + 1)) {
2583 frame->current = frame->parent = frame->at_reference;
2584 frame->at_reference = NULL;
2585
2586 } else {
2587 /*
2588 * Go back up one section, because we can.
2589 */
2590 frame->current = frame->parent = cf_item_to_section(frame->current->item.parent);
2591 }
2592
2593 fr_assert(frame->braces > 0);
2594 frame->braces--;
2595
2596 /*
2597 * Merge the template into the existing
2598 * section. parent uses more memory, but
2599 * means that templates now work with
2600 * sub-sections, etc.
2601 */
2602 if (!cf_template_merge(parent, parent->template)) return -1;
2603
2604 ptr++;
2605 stack->ptr = ptr;
2606 return 1;
2607 }
2608
2609 /*
2610 * Found nothing to get excited over. It MUST be
2611 * a key word.
2612 */
2613 name1_ptr = ptr;
2614 switch (parent->unlang) {
2615 default:
2616 /*
2617 * The LHS is a bare word / keyword in normal configuration file syntax.
2618 */
2619 name1_token = gettoken(&ptr, buff[1], stack->bufsize, false);
2620 if (name1_token == T_EOL) return 0;
2621
2622 if (name1_token == T_INVALID) {
2623 return parse_error(stack, name1_ptr, fr_strerror());
2624 }
2625
2626 if (name1_token != T_BARE_WORD) {
2627 return parse_error(stack, name1_ptr, "Invalid location for quoted string");
2628 }
2629
2630 if (*buff[1] == '%') {
2631 return parse_error(stack, name1_ptr, "Cannot use functions outside of a processing section");
2632 }
2633
2634 if (*buff[1] == '&') {
2635 return parse_error(stack, name1_ptr, "Cannot reference attributes outside of a processing section");
2636 }
2637
2638 fr_skip_whitespace(ptr);
2639 break;
2640
2641 case CF_UNLANG_ALLOW:
2642 case CF_UNLANG_EDIT:
2644 /*
2645 * The LHS can be an xlat expansion, attribute reference, etc.
2646 */
2647 if (cf_get_token(parent, &ptr, &name1_token, buff[1], stack->bufsize,
2648 frame->filename, frame->lineno) < 0) {
2649 return -1;
2650 }
2651
2652 /*
2653 * Complain about old syntax.
2654 */
2655 if (check_config && (*buff[1] == '&')) {
2656 WARN("%s[%d]: Using '&' is no longer necessary when referencing attributes. Please delete it.", frame->filename, frame->lineno);
2657 }
2658 break;
2659 }
2660
2661 /*
2662 * Check for bad names. A section named ".foo" will absolutely break the path hierarchy.
2663 */
2664 if (name1_token == T_BARE_WORD) {
2665 if (!cf_name_char1[(uint8_t) *buff[1]]) {
2666 return parse_error(stack, name1_ptr, "Invalid name");
2667 }
2668 }
2669
2670 /*
2671 * Check if the thing we just parsed is an unlang keyword.
2672 */
2673 if ((name1_token == T_BARE_WORD) && isalpha((uint8_t) *buff[1])) {
2675 if (process) {
2676 CONF_ITEM *ci;
2677
2678 /*
2679 * Disallow keywords outside of unlang sections.
2680 *
2681 * We don't strictly need to do this with the more state-oriented parser, but
2682 * people keep putting unlang into random places in the configuration files,
2683 * which is wrong.
2684 */
2685 if (parent->unlang != CF_UNLANG_ALLOW) {
2686 return parse_error(stack, name1_ptr, "Invalid location for unlang keyword");
2687 }
2688
2689 stack->ptr = ptr;
2690 ci = process(stack);
2691 if (!ci) return -1;
2692
2693 ptr = stack->ptr;
2694 if (cf_item_is_section(ci)) {
2695 parent->allow_locals = false;
2696 css = cf_item_to_section(ci);
2697 goto add_section;
2698 }
2699
2700 /*
2701 * Else the item is a pair, and the call to process() it already added it to the
2702 * current section.
2703 */
2704 goto added_pair;
2705 }
2706
2707 /*
2708 * The next token isn't text, so we ignore it.
2709 */
2710 if (!isalnum((int) *ptr)) goto check_for_eol;
2711 }
2712
2713 /*
2714 * See if this thing is a variable definition.
2715 */
2716 if ((name1_token == T_BARE_WORD) && parent->allow_locals) {
2718 char const *ptr3;
2719
2721 if (type == FR_TYPE_NULL) {
2722 parent->allow_locals = false;
2723 goto check_for_eol;
2724 }
2725
2726 if (type == FR_TYPE_TLV) goto parse_name2;
2727
2728 /*
2729 * group {
2730 *
2731 * is a section.
2732 */
2733 if (type == FR_TYPE_GROUP) {
2734 fr_skip_whitespace(ptr);
2735 if (*ptr == '{') {
2736 ptr++;
2737 value = NULL;
2738 name2_token = T_BARE_WORD;
2739 goto alloc_section;
2740 }
2741
2742 } else if (!fr_type_is_leaf(type)) {
2743 /*
2744 * Other structural types are allowed.
2745 */
2746 return parse_error(stack, name1_ptr, "Invalid data type for local variable. Must be 'tlv' or else a non-structural type");
2747 }
2748
2749 /*
2750 * We don't have an operator, so set it to a magic value.
2751 */
2753
2754 /*
2755 * Parse the name of the local variable, and use it as the "value" for the CONF_PAIR.
2756 */
2757 ptr3 = ptr;
2758 if (cf_get_token(parent, &ptr, &value_token, buff[2], stack->bufsize,
2759 frame->filename, frame->lineno) < 0) {
2760 return -1;
2761 }
2762
2763 if (value_token != T_BARE_WORD) {
2764 return parse_error(stack, ptr3, "Invalid name");
2765 }
2766
2767 value = buff[2];
2768
2769 /*
2770 * Non-structural things must be variable definitions.
2771 */
2772 if (fr_type_is_leaf(type)) goto alloc_pair;
2773
2774 /*
2775 * Parse: group foo
2776 * vs group foo { ...
2777 */
2778 fr_skip_whitespace(ptr);
2779
2780 if (*ptr != '{') goto alloc_pair;
2781
2782 ptr++;
2783 name2_token = T_BARE_WORD;
2784 goto alloc_section;
2785 }
2786
2787 /*
2788 * We've parsed the LHS thing. The RHS might be empty, or an operator, or another word, or an
2789 * open bracket.
2790 */
2791check_for_eol:
2792 if (!*ptr || (*ptr == '#') || (*ptr == ',') || (*ptr == ';') || (*ptr == '}')) {
2793 /*
2794 * Only unlang sections can have module references.
2795 *
2796 * We also allow bare words in edit lists, where the RHS is a list of values.
2797 *
2798 * @todo - detail "suppress" requires bare words :(
2799 */
2800 parent->allow_locals = false;
2801 value_token = T_INVALID;
2802 op_token = T_OP_EQ;
2803 value = NULL;
2804 goto alloc_pair;
2805 }
2806
2807 /*
2808 * A common pattern is: name { ...}
2809 * Check for it and skip ahead.
2810 */
2811 if (*ptr == '{') {
2812 ptr++;
2813 name2_token = T_INVALID;
2814 value = NULL;
2815 goto alloc_section;
2816 }
2817
2818 /*
2819 * Parse the thing after the first word. It can be an operator, or the second name for a section.
2820 */
2821 name2_ptr = ptr;
2822 switch (parent->unlang) {
2823 default:
2824 /*
2825 * Configuration sections can only have '=' after the
2826 * first word, OR a second word which is the second name
2827 * of a configuration section.
2828 */
2829 if (*ptr == '=') goto operator;
2830
2831 /*
2832 * Section name2 can only be alphanumeric or UTF-8.
2833 */
2834 parse_name2:
2835 name2_ptr = ptr;
2836
2837 if (!(isalpha((uint8_t) *ptr) || isdigit((uint8_t) *ptr) || (*(uint8_t const *) ptr >= 0x80))) {
2838 /*
2839 * Maybe they missed a closing brace somewhere?
2840 */
2841 name2_token = gettoken(&ptr, buff[2], stack->bufsize, false); /* can't be EOL */
2842 if (fr_assignment_op[name2_token]) {
2843 return parse_error(stack, name2_ptr, "Unexpected operator, was expecting a configuration section. Is there a missing '}' somewhere?");
2844 }
2845
2846 return parse_error(stack, name2_ptr, "Invalid second name for configuration section");
2847 }
2848
2849 name2_token = gettoken(&ptr, buff[2], stack->bufsize, false); /* can't be EOL */
2850 if (name2_token == T_INVALID) {
2851 return parse_error(stack, name2_ptr, fr_strerror());
2852 }
2853
2854 if (name2_token != T_BARE_WORD) {
2855 return parse_error(stack, name2_ptr, "Unexpected quoted string after section name");
2856 }
2857
2858 fr_skip_whitespace(ptr);
2859
2860 /*
2861 * load-balance and redundant-load-balance MUST have a static module name, and MAY have
2862 * an additional load-balance keyword.
2863 */
2864 if ((parent->unlang == CF_UNLANG_MODULES) && (*ptr != '{') &&
2865 ((strcmp(buff[1], "load-balance") == 0) ||
2866 (strcmp(buff[1], "redundant-load-balance") == 0))) {
2867 /*
2868 * The third name could be an attribute name, xlat expansion, etc.
2869 */
2870 if (cf_get_token(parent, &ptr, &value_token, buff[3], stack->bufsize,
2871 frame->filename, frame->lineno) < 0) {
2872 return -1;
2873 }
2874
2875 fr_skip_whitespace(ptr);
2876
2877 if (*ptr != '{') {
2878 return parse_error(stack, ptr, "Expected '{'");
2879 }
2880
2881 ptr++;
2882
2883 css = cf_section_alloc(parent, parent, buff[1], buff[2]);
2884 if (!css) goto oom;
2885
2886 css->argc = 1;
2887 css->argv = talloc_array(css, char const *, 1);
2888 css->argv[0] = talloc_strdup(css->argv, buff[3]);
2889 css->argv_quote = talloc_array(css, fr_token_t, 1);
2890 css->argv_quote[0] = value_token;
2891 goto setup_section;
2892 }
2893
2894 if (*ptr != '{') {
2895 return parse_error(stack, ptr, "Missing '{' for configuration section");
2896 }
2897
2898 ptr++;
2899 value = buff[2];
2900 goto alloc_section;
2901
2903 /*
2904 * The next thing MUST be an operator. We don't support nested attributes in "update" or
2905 * "map" sections.
2906 */
2907 goto operator;
2908
2909 case CF_UNLANG_EDIT:
2910 /*
2911 * The next thing MUST be an operator. Edit sections always do operations, even on
2912 * lists. i.e. there is no second name section when editing a list.
2913 */
2914 goto operator;
2915
2916 case CF_UNLANG_ALLOW:
2917 /*
2918 * 'case ::foo' is allowed. For generality, we just expect that the second argument to
2919 * 'case' is not an operator.
2920 */
2921 if ((strcmp(buff[1], "case") == 0) ||
2922 (strcmp(buff[1], "limit") == 0) ||
2923 (strcmp(buff[1], "timeout") == 0)) {
2924 break;
2925 }
2926
2927 /*
2928 * It's not a string, bare word, or attribute reference. It must be an operator.
2929 */
2930 if (!((*ptr == '"') || (*ptr == '`') || (*ptr == '\'') || ((*ptr == '&') && (ptr[1] != '=')) ||
2931 ((*((uint8_t const *) ptr) & 0x80) != 0) || isalpha((uint8_t) *ptr) || isdigit((uint8_t) *ptr))) {
2932 goto operator;
2933 }
2934 break;
2935 }
2936
2937 /*
2938 * The second name could be a bare word, xlat expansion, string etc.
2939 */
2940 if (cf_get_token(parent, &ptr, &name2_token, buff[2], stack->bufsize,
2941 frame->filename, frame->lineno) < 0) {
2942 return -1;
2943 }
2944
2945 if (*ptr != '{') {
2947
2948 /*
2949 * Not the name of a data type.
2950 */
2951 if (type == FR_TYPE_NULL) {
2952 return parse_error(stack, ptr, "Expected '{' after section names");
2953 }
2954
2955 if (parent->unlang == CF_UNLANG_EDIT) {
2956 return parse_error(stack, name1_ptr, "Invalid location for local variable - definitions cannot be inside of an assignment");
2957 }
2958
2959 if (parent->unlang == CF_UNLANG_ALLOW) {
2960 return parse_error(stack, name1_ptr, "Invalid location for local variable - definitions must go at the start of a section");
2961 }
2962
2963 return parse_error(stack, name1_ptr, "Invalid location for local variable - definitions can only go in a processing section.");
2964 }
2965 ptr++;
2966 value = buff[2];
2967
2968alloc_section:
2969 parent->allow_locals = false;
2970
2971 /*
2972 * @policy foo { ...}
2973 *
2974 * Means "add foo to the policy section". And if
2975 * policy{} doesn't exist, create it, and then mark up
2976 * policy{} with a flag "we need to merge it", so that
2977 * when we read the actual policy{}, we merge the
2978 * contents together, instead of creating a second
2979 * policy{}.
2980 *
2981 * @todo - allow for '.' in @.ref Or at least test it. :(
2982 *
2983 * @todo - allow for two section names @ref foo bar {...}
2984 *
2985 * @todo - maybe we can use this to overload things in
2986 * virtual servers, and in modules?
2987 */
2988 if (buff[1][0] == '@') {
2989 CONF_ITEM *ci;
2990 CONF_SECTION *root;
2991 char const *name = &buff[1][1];
2992
2993 if (!value) {
2994 ERROR("%s[%d]: Missing section name for reference", frame->filename, frame->lineno);
2995 return -1;
2996 }
2997
2998 root = cf_root(parent);
2999
3000 ci = cf_reference_item(root, parent, name);
3001 if (!ci) {
3002 if (name[1] == '.') {
3003 PERROR("%s[%d]: Failed finding reference \"%s\"", frame->filename, frame->lineno, name);
3004 return -1;
3005 }
3006
3007 css = cf_section_alloc(root, root, name, NULL);
3008 if (!css) goto oom;
3009
3010 cf_filename_set(css, frame->filename);
3011 cf_lineno_set(css, frame->lineno);
3012 css->name2_quote = name2_token;
3013 css->unlang = CF_UNLANG_NONE;
3014 css->allow_locals = false;
3015 css->at_reference = true;
3016 parent = css;
3017
3018 /*
3019 * Copy this code from below. :(
3020 */
3021 if (cf_item_to_section(parent->item.parent) == root) {
3022 if (strcmp(css->name1, "server") == 0) css->unlang = CF_UNLANG_SERVER;
3023 if (strcmp(css->name1, "policy") == 0) css->unlang = CF_UNLANG_POLICY;
3024 if (strcmp(css->name1, "modules") == 0) css->unlang = CF_UNLANG_MODULES;
3025 if (strcmp(css->name1, "templates") == 0) css->unlang = CF_UNLANG_CAN_HAVE_UPDATE;
3026 }
3027
3028 } else {
3029 if (!cf_item_is_section(ci)) {
3030 ERROR("%s[%d]: Reference \"%s\" is not a section", frame->filename, frame->lineno, name);
3031 return -1;
3032 }
3033
3034 /*
3035 * Set the new parent and ensure we're
3036 * not creating a duplicate section.
3037 */
3039 css = cf_section_find(parent, value, NULL);
3040 if (css) {
3041 ERROR("%s[%d]: Reference \"%s\" already contains a \"%s\" section at %s[%d]",
3042 frame->filename, frame->lineno, name, value,
3043 css->item.filename, css->item.lineno);
3044 return -1;
3045 }
3046 }
3047
3048 /*
3049 * We're processing a section. The @reference is
3050 * OUTSIDE of this section.
3051 *
3052 * '@reference' is only valid at the top level of a
3053 * section: once any enclosing section has been opened,
3054 * frame->current diverges from frame->parent, and the
3055 * reference has no well-defined target.
3056 */
3057 if (frame->current != frame->parent) {
3058 ERROR("%s[%d]: '@%s' references can only appear at the top level of a section",
3059 frame->filename, frame->lineno, name);
3060 return -1;
3061 }
3062 frame->at_reference = frame->parent;
3063 frame->at_reference_braces = frame->braces;
3064 name2_token = T_BARE_WORD;
3065
3066 css = cf_section_alloc(parent, parent, value, NULL);
3067 } else {
3068 /*
3069 * Check if there's already an auto-created
3070 * section of this name. If so, just use that
3071 * section instead of allocating a new one.
3072 */
3073 css = cf_section_find(parent, buff[1], value);
3074 if (css && css->at_reference) {
3075 css->at_reference = false;
3076 } else {
3078 }
3079 }
3080
3081 if (!css) {
3082 oom:
3083 ERROR("%s[%d]: Failed allocating memory for section",
3084 frame->filename, frame->lineno);
3085 return -1;
3086 }
3087
3088setup_section:
3089 if (css->depth >= CONF_SECTION_MAX_DEPTH) {
3090 ERROR("%s[%d]: Failed creating section - too many nested sections (%d)",
3091 frame->filename, frame->lineno, css->depth);
3092 return -1;
3093 }
3094
3095 cf_filename_set(css, frame->filename);
3096 cf_lineno_set(css, frame->lineno);
3097 css->name2_quote = name2_token;
3098 css->unlang = CF_UNLANG_NONE;
3099 css->allow_locals = false;
3100
3101 /*
3102 * Only a few top-level sections allow "unlang"
3103 * statements. And for those, "unlang"
3104 * statements are only allowed in child
3105 * subsection.
3106 */
3107 switch (parent->unlang) {
3108 case CF_UNLANG_NONE:
3109 if (!parent->item.parent) {
3110 if (strcmp(css->name1, "server") == 0) css->unlang = CF_UNLANG_SERVER;
3111 if (strcmp(css->name1, "policy") == 0) css->unlang = CF_UNLANG_POLICY;
3112 if (strcmp(css->name1, "modules") == 0) css->unlang = CF_UNLANG_MODULES;
3113 if (strcmp(css->name1, "templates") == 0) css->unlang = CF_UNLANG_CAN_HAVE_UPDATE;
3114
3115 } else if ((cf_item_to_section(parent->item.parent)->unlang == CF_UNLANG_MODULES) &&
3116 (strcmp(css->name1, "update") == 0)) {
3117 /*
3118 * Module configuration can contain "update" statements.
3119 */
3121 css->allow_locals = false;
3122 }
3123 break;
3124
3125 /*
3126 * It's a policy section - allow unlang inside of child sections.
3127 */
3128 case CF_UNLANG_POLICY:
3129 css->unlang = CF_UNLANG_ALLOW;
3130 css->allow_locals = true;
3131 break;
3132
3133 /*
3134 * A virtual server has processing sections, but only a limited number of them.
3135 * Rather than trying to autoload them and glue the interpreter into the conf
3136 * file parser, we just hack it.
3137 */
3138 case CF_UNLANG_SERVER:
3139 // git grep SECTION_NAME src/process/ src/lib/server/process.h | sed 's/.*SECTION_NAME("//;s/",.*//' | sort -u
3141 css->unlang = CF_UNLANG_ALLOW;
3142 css->allow_locals = true;
3143 break;
3144 }
3145
3146 /*
3147 * Allow local variables, but no unlang statements.
3148 */
3149 if (strcmp(css->name1, "dictionary") == 0) {
3151 css->allow_locals = true;
3152 break;
3153 }
3154
3155 /*
3156 * ldap sync has "update" a few levels down.
3157 */
3158 if (strcmp(css->name1, "listen") == 0) {
3160 }
3161 break;
3162
3163 /*
3164 * Virtual modules in the "modules" section can have unlang.
3165 */
3166 case CF_UNLANG_MODULES:
3167 if ((strcmp(css->name1, "group") == 0) ||
3168 (strcmp(css->name1, "load-balance") == 0) ||
3169 (strcmp(css->name1, "redundant") == 0) ||
3170 (strcmp(css->name1, "redundant-load-balance") == 0)) {
3171 css->unlang = CF_UNLANG_ALLOW;
3172 css->allow_locals = true;
3173 } else {
3175 }
3176 break;
3177
3178 case CF_UNLANG_EDIT:
3179 /*
3180 * Edit sections can only have children which are edit sections.
3181 */
3182 css->unlang = CF_UNLANG_EDIT;
3183 break;
3184
3185 case CF_UNLANG_ALLOW:
3186 /*
3187 * If we are doing list assignment, then don't allow local variables. The children are
3188 * also then all edit sections, and not unlang statements.
3189 *
3190 * If we're not doing list assignment, then name2 has to be a bare word, string, etc.
3191 */
3192 css->allow_locals = !fr_list_assignment_op[name2_token];
3193 if (css->allow_locals) {
3194 /*
3195 * @todo - tighten this up for "actions" sections, and module rcode
3196 * over-rides.
3197 *
3198 * Perhaps the best way to do that is to change the syntax for module
3199 * over-rides, so that the parser doesn't have to guess. :(
3200 */
3201 css->unlang = CF_UNLANG_ALLOW;
3202 } else {
3203 css->unlang = CF_UNLANG_EDIT;
3204 }
3205 break;
3206
3207 /*
3208 * We can (maybe?) do nested assignments inside of an old-style "update" or "map" section
3209 */
3212 break;
3213
3216 css->allow_locals = true;
3217 break;
3218
3220 if (strcmp(css->name1, "update") == 0) {
3222 } else {
3224 }
3225 break;
3226 }
3227
3228add_section:
3229 /*
3230 * The current section is now the child section.
3231 */
3232 frame->current = css;
3233 frame->braces++;
3234 css = NULL;
3235 stack->ptr = ptr;
3236 return 1;
3237
3238
3239 /*
3240 * If we're not parsing a section, then the next
3241 * token MUST be an operator.
3242 */
3243operator:
3244 op_ptr = ptr;
3245 name2_token = gettoken(&ptr, buff[2], stack->bufsize, false);
3246 switch (name2_token) {
3247 case T_OP_ADD_EQ:
3248 case T_OP_SUB_EQ:
3249 case T_OP_MUL_EQ:
3250 case T_OP_DIV_EQ:
3251 case T_OP_AND_EQ:
3252 case T_OP_OR_EQ:
3253 case T_OP_NE:
3254 case T_OP_RSHIFT_EQ:
3255 case T_OP_GE:
3256 case T_OP_GT:
3257 case T_OP_LSHIFT_EQ:
3258 case T_OP_LE:
3259 case T_OP_LT:
3260 case T_OP_CMP_EQ:
3261 case T_OP_CMP_FALSE:
3262 case T_OP_SET:
3263 case T_OP_PREPEND:
3264 /*
3265 * Allow more operators in unlang statements, edit sections, and old-style "update" sections.
3266 */
3267 if ((parent->unlang != CF_UNLANG_ALLOW) && (parent->unlang != CF_UNLANG_EDIT) && (parent->unlang != CF_UNLANG_ASSIGNMENT)) {
3268 return parse_error(stack, op_ptr, "Invalid operator for assignment");
3269 }
3271
3272 case T_OP_EQ:
3273 /*
3274 * Configuration variables can only use =
3275 */
3276 fr_skip_whitespace(ptr);
3277 op_token = name2_token;
3278 break;
3279
3280 default:
3281 return parse_error(stack, op_ptr, "Syntax error, the input should be an assignment operator");
3282 }
3283
3284 /*
3285 * MUST have something after the operator.
3286 */
3287 if (!*ptr || (*ptr == '#') || (*ptr == ',') || (*ptr == ';')) {
3288 return parse_error(stack, ptr, "Missing value after operator");
3289 }
3290
3291 /*
3292 * foo = { ... } for nested groups.
3293 *
3294 * As a special case, we allow sub-sections after '=', etc.
3295 *
3296 * This syntax is only for inside of "update"
3297 * sections, and for attributes of type "group".
3298 * But the parser isn't (yet) smart enough to
3299 * know about that context. So we just silently
3300 * allow it everywhere.
3301 */
3302 if (*ptr == '{') {
3303 if ((parent->unlang != CF_UNLANG_ALLOW) && (parent->unlang != CF_UNLANG_EDIT)) {
3304 return parse_error(stack, ptr, "Invalid location for nested attribute assignment");
3305 }
3306
3307 if (!fr_list_assignment_op[name2_token]) {
3308 return parse_error(stack, ptr, "Invalid assignment operator for list");
3309 }
3310
3311 /*
3312 * Now that we've peeked ahead to
3313 * see the open brace, parse it
3314 * for real.
3315 */
3316 ptr++;
3317
3318 /*
3319 * Leave name2_token as the
3320 * operator (as a hack). But
3321 * note that there's no actual
3322 * name2. We'll deal with that
3323 * situation later.
3324 */
3325 value = NULL;
3326 goto alloc_section;
3327 }
3328
3329 fr_skip_whitespace(ptr);
3330
3331 /*
3332 * Parse the value for a CONF_PAIR.
3333 *
3334 * If it's unlang or an edit section, the RHS can be an expression.
3335 */
3336 if ((parent->unlang == CF_UNLANG_ALLOW) || (parent->unlang == CF_UNLANG_EDIT)) {
3337 bool eol;
3338 ssize_t slen;
3339
3340 name2_ptr = ptr;
3341
3342 /*
3343 * If the RHS is an expression (foo) or function %foo(), then mark it up as an expression.
3344 */
3345 if ((*ptr == '(') || (*ptr == '%')) {
3346 /* nothing */
3347
3348 } else if (cf_get_token(parent, &name2_ptr, &value_token, buff[2], stack->bufsize,
3349 frame->filename, frame->lineno) == 0) {
3350 /*
3351 * We have one token (bare word), followed by EOL. It's just a token.
3352 */
3353 fr_skip_whitespace(name2_ptr);
3354 if (terminal_end_line[(uint8_t) *name2_ptr]) {
3355 parent->allow_locals = false;
3356 ptr = name2_ptr;
3357 value = buff[2];
3358 goto alloc_pair;
3359 }
3360 } /* else it looks like an expression */
3361
3362 /*
3363 * Parse the text as an expression.
3364 *
3365 * Note that unlike conditions, expressions MUST use \ at the EOL for continuation.
3366 * If we automatically read past EOL, as with:
3367 *
3368 * &foo := (bar -
3369 * baz)
3370 *
3371 * That works, mostly. Until the user forgets to put the trailing ')', and then
3372 * the parse is bad enough that it tries to read to EOF, or to some other random
3373 * parse error.
3374 *
3375 * So the simplest way to avoid utter craziness is to simply require a signal which
3376 * says "yes, I intended to put this over multiple lines".
3377 */
3378 slen = fr_skip_condition(ptr, NULL, terminal_end_line, &eol);
3379 if (slen < 0) {
3380 return parse_error(stack, ptr + (-slen), fr_strerror());
3381 }
3382
3383 /*
3384 * We parsed until the end of the string, but the condition still needs more data.
3385 */
3386 if (eol) {
3387 return parse_error(stack, ptr + slen, "Expression is unfinished at end of line");
3388 }
3389
3390 /*
3391 * Keep a copy of the entire RHS.
3392 */
3393 memcpy(buff[2], ptr, slen);
3394 buff[2][slen] = '\0';
3395
3396 value = buff[2];
3397
3398 /*
3399 * Mark it up as an expression
3400 *
3401 * @todo - we should really just call cf_data_add() to add a flag, but this is good for
3402 * now. See map_afrom_cp()
3403 */
3404 value_token = T_HASH;
3405
3406 /*
3407 * Skip terminal characters
3408 */
3409 ptr += slen;
3410 if ((*ptr == ',') || (*ptr == ';')) ptr++;
3411
3412#if 0
3413 } else if ((parent->unlang != CF_UNLANG_ASSIGNMENT) &&
3414 ((*ptr == '`') || (*ptr == '%') || (*ptr == '('))) {
3415 /*
3416 * Config sections can't use backticks, xlat expansions, or expressions.
3417 *
3418 * Except module configurations can have key = %{...}
3419 */
3420 return parse_error(stack, ptr, "Invalid value for assignment in configuration file");
3421#endif
3422
3423 } else {
3424 if (cf_get_token(parent, &ptr, &value_token, buff[2], stack->bufsize,
3425 frame->filename, frame->lineno) < 0) {
3426 return -1;
3427 }
3428 value = buff[2];
3429 }
3430
3431 /*
3432 * We have an attribute assignment, which means that we no longer allow local variables to be
3433 * defined.
3434 */
3435 parent->allow_locals = false;
3436
3437alloc_pair:
3438 if (add_pair(parent, buff[1], value, name1_token, op_token, value_token, buff[3], frame->filename, frame->lineno) < 0) return -1;
3439
3440added_pair:
3441 fr_skip_whitespace(ptr);
3442
3443 /*
3444 * Skip semicolon if we see it after a
3445 * CONF_PAIR. Also allow comma for
3446 * backwards compatibility with secret
3447 * things in v3.
3448 */
3449 if ((*ptr == ';') || (*ptr == ',')) {
3450 ptr++;
3451 stack->ptr = ptr;
3452 return 1;
3453 }
3454
3455 /*
3456 * Closing brace is allowed after a CONF_PAIR
3457 * definition.
3458 */
3459 if (*ptr == '}') {
3460 stack->ptr = ptr;
3461 return 1;
3462 }
3463
3464 /*
3465 * Anything OTHER than EOL or comment is a syntax
3466 * error.
3467 */
3468 if (*ptr && (*ptr != '#')) {
3469 return parse_error(stack, ptr, "Unexpected text after configuration item");
3470 }
3471
3472 /*
3473 * Since we're at EOL or comment, just drop the
3474 * text, and go read another line of text.
3475 */
3476 return 0;
3477}
3478
3479
3481{
3482 cf_stack_frame_t *frame = &stack->frame[stack->depth];
3483 CONF_SECTION *parent = frame->current;
3484 cf_file_heap_t *h;
3485
3486 h = fr_heap_pop(&frame->heap);
3487 if (!h) {
3488 /*
3489 * Done reading the directory entry. Close it, and go
3490 * back up a stack frame.
3491 */
3492 talloc_free(frame->directory);
3493 stack->depth--;
3494 return 0;
3495 }
3496
3497 /*
3498 * Push the next filename onto the stack.
3499 */
3500 stack->depth++;
3501 frame = &stack->frame[stack->depth];
3502 memset(frame, 0, sizeof(*frame));
3503
3504 frame->type = CF_STACK_FILE;
3505 frame->fp = NULL;
3506 frame->parent = parent;
3507 frame->current = parent;
3508 frame->filename = h->filename;
3509 frame->lineno = 0;
3510 frame->from_dir = true;
3511 return 1;
3512}
3513
3514
3516
3517void cf_md5_init(void)
3518{
3520}
3521
3522
3523static void cf_md5_update(char const *p)
3524{
3525 if (!cf_md5_ctx) return;
3526
3527 fr_md5_update(cf_md5_ctx, (uint8_t const *)p, strlen(p));
3528}
3529
3531{
3532 if (!cf_md5_ctx) {
3533 memset(digest, 0, MD5_DIGEST_LENGTH);
3534 return;
3535 }
3536
3537 fr_md5_final(digest, cf_md5_ctx);
3539 cf_md5_ctx = NULL;
3540}
3541
3543{
3544 bool at_eof, has_spaces;
3545 size_t len;
3546 char const *ptr;
3547 cf_stack_frame_t *frame = &stack->frame[stack->depth];
3548
3549read_more:
3550 has_spaces = false;
3551
3552read_continuation:
3553 /*
3554 * Get data, and remember if we are at EOF.
3555 */
3556 at_eof = (fgets(stack->fill, stack->bufsize - (stack->fill - stack->buff[0]), frame->fp) == NULL);
3557 cf_md5_update(stack->fill);
3558 frame->lineno++;
3559
3560 /*
3561 * We read the entire 8k worth of data: complain.
3562 * Note that we don't care if the last character
3563 * is \n: it's still forbidden. This means that
3564 * the maximum allowed length of text is 8k-1, which
3565 * should be plenty.
3566 */
3567 len = strlen(stack->fill);
3568 if ((stack->fill + len + 1) >= (stack->buff[0] + stack->bufsize)) {
3569 ERROR("%s[%d]: Line too long", frame->filename, frame->lineno);
3570 return -1;
3571 }
3572
3573 /*
3574 * Suppress leading whitespace after a
3575 * continuation line.
3576 */
3577 if (has_spaces) {
3578 ptr = stack->fill;
3579 fr_skip_whitespace(ptr);
3580
3581 if (ptr > stack->fill) {
3582 memmove(stack->fill, ptr, len - (ptr - stack->fill));
3583 len -= (ptr - stack->fill);
3584 }
3585 }
3586
3587 /*
3588 * Skip blank lines when we're at the start of
3589 * the read buffer.
3590 */
3591 if (stack->fill == stack->buff[0]) {
3592 if (at_eof) return 0;
3593
3594 ptr = stack->buff[0];
3595 fr_skip_whitespace(ptr);
3596
3597 if (!*ptr || (*ptr == '#')) goto read_more;
3598
3599 } else if (at_eof || (len == 0)) {
3600 ERROR("%s[%d]: Continuation at EOF is illegal", frame->filename, frame->lineno);
3601 return -1;
3602 }
3603
3604 /*
3605 * See if there's a continuation.
3606 */
3607 while ((len > 0) &&
3608 ((stack->fill[len - 1] == '\n') || (stack->fill[len - 1] == '\r'))) {
3609 len--;
3610 stack->fill[len] = '\0';
3611 }
3612
3613 if ((len > 0) && (stack->fill[len - 1] == '\\')) {
3614 /*
3615 * Check for "suppress spaces" magic.
3616 */
3617 if (!has_spaces && (len > 2) && (stack->fill[len - 2] == '"')) {
3618 has_spaces = true;
3619 }
3620
3621 stack->fill[len - 1] = '\0';
3622 stack->fill += len - 1;
3623 goto read_continuation;
3624 }
3625
3626 ptr = stack->fill;
3627
3628 /*
3629 * We now have one full line of text in the input
3630 * buffer, without continuations.
3631 */
3632 fr_skip_whitespace(ptr);
3633
3634 /*
3635 * Nothing left, or just a comment. Go read
3636 * another line of text.
3637 *
3638 * If the caller asked us to preserve comments (utilities like
3639 * radconf2json round-trip them as CONF_COMMENT items), capture
3640 * the comment text on the current section before continuing.
3641 */
3642 if (!*ptr || (*ptr == '#')) {
3643 if (_cf_preserve_comments() && frame->current) {
3644 char const *text;
3645 CONF_COMMENT *c;
3646 char *p;
3647
3648 /*
3649 * `#` line: keep everything after the marker
3650 * verbatim (whitespace included; operators use
3651 * leading indent to format banners). Empty
3652 * line: emit a blank marker (NULL text) so the
3653 * writer round-trips the visual separator, and
3654 * downstream tooling can tell two comment blocks
3655 * separated by a blank line apart from one long
3656 * block.
3657 */
3658 text = (*ptr == '#') ? ptr + 1 : NULL;
3659 c = cf_comment_alloc(frame->current, text);
3660 if (c) {
3661 /* Trim trailing CR/LF off the talloc'd copy */
3662 p = UNCONST(char *, c->text);
3663 if (p) {
3664 size_t l = strlen(p);
3665 while (l > 0 && (p[l - 1] == '\n' || p[l - 1] == '\r')) {
3666 p[--l] = '\0';
3667 }
3668 }
3669 cf_filename_set(c, frame->filename);
3670 cf_lineno_set(c, frame->lineno);
3671 }
3672 }
3673 goto read_more;
3674 }
3675
3676 return 1;
3677}
3678
3679
3680/*
3681 * Read a configuration file or files.
3682 */
3684{
3686 char const *ptr;
3687
3688 cf_stack_frame_t *frame;
3689 int rcode;
3690
3691do_frame:
3692 frame = &stack->frame[stack->depth];
3693 parent = frame->current; /* add items here */
3694
3695 switch (frame->type) {
3696#ifdef HAVE_GLOB_H
3697 case CF_STACK_GLOB:
3698 if (frame->gl_current == frame->glob.gl_pathc) {
3699 globfree(&frame->glob);
3700 goto pop_stack;
3701 }
3702
3703 /*
3704 * Process the filename as an include.
3705 */
3706 if (process_include(stack, parent, frame->glob.gl_pathv[frame->gl_current++], frame->required, false) < 0) return -1;
3707
3708 /*
3709 * Run the correct frame. If the file is NOT
3710 * required, then the call to process_include()
3711 * may return 0, and we just process the next
3712 * glob. Otherwise, the call to
3713 * process_include() may return a directory or a
3714 * filename. Go handle that.
3715 */
3716 goto do_frame;
3717#endif
3718
3719#ifdef HAVE_DIRENT_H
3720 case CF_STACK_DIR:
3721 rcode = frame_readdir(stack);
3722 if (rcode == 0) goto do_frame;
3723 if (rcode < 0) return -1;
3724
3725 /*
3726 * Reset which frame we're looking at.
3727 */
3728 frame = &stack->frame[stack->depth];
3729 fr_assert(frame->type == CF_STACK_FILE);
3730 break;
3731#endif
3732
3733 case CF_STACK_FILE:
3734 break;
3735 }
3736
3737#ifndef NDEBUG
3738 /*
3739 * One last sanity check.
3740 */
3741 if (frame->type != CF_STACK_FILE) {
3742 cf_log_err(frame->current, "%s: Internal sanity check failed", __FUNCTION__);
3743 goto pop_stack;
3744 }
3745#endif
3746
3747 /*
3748 * Open the new file if necessary. It either came from
3749 * the first call to the function, or was pushed onto the
3750 * stack by another function.
3751 */
3752 if (!frame->fp) {
3753 rcode = cf_file_open(frame->parent, frame->filename, frame->from_dir, &frame->fp);
3754 if (rcode < 0) return -1;
3755
3756 /*
3757 * Ignore this file
3758 */
3759 if (rcode == 1) {
3760 cf_log_warn(frame->current, "Ignoring file %s - it was already read",
3761 frame->filename);
3762 goto pop_stack;
3763 }
3764 }
3765
3766 /*
3767 * Read, checking for line continuations ('\\' at EOL)
3768 */
3769 for (;;) {
3770 /*
3771 * Fill the buffers with data.
3772 */
3773 stack->fill = stack->buff[0];
3774 rcode = cf_file_fill(stack);
3775 if (rcode < 0) return -1;
3776 if (rcode == 0) break;
3777
3778 /*
3779 * The text here MUST be at the start of a line,
3780 * OR have only whitespace in front of it.
3781 */
3782 ptr = stack->buff[0];
3783 fr_skip_whitespace(ptr);
3784
3785 if (*ptr == '$') {
3786 /*
3787 * Allow for $INCLUDE files
3788 */
3789 if (strncasecmp(ptr, "$INCLUDE", 8) == 0) {
3790 ptr += 8;
3791
3792 stack->ptr = ptr;
3793 if (process_include(stack, parent, ptr, true, true) < 0) return -1;
3794 goto do_frame;
3795 }
3796
3797 if (strncasecmp(ptr, "$-INCLUDE", 9) == 0) {
3798 ptr += 9;
3799
3800 stack->ptr = ptr;
3801 rcode = process_include(stack, parent, ptr, false, true);
3802 if (rcode < 0) return -1;
3803 if (rcode == 0) continue;
3804 goto do_frame;
3805 }
3806
3807 /*
3808 * Allow for $TEMPLATE things
3809 */
3810 if (strncasecmp(ptr, "$TEMPLATE", 9) == 0) {
3811 ptr += 9;
3812 fr_skip_whitespace(ptr);
3813
3814 stack->ptr = ptr;
3815 if (process_template(stack) < 0) return -1;
3816 continue;
3817 }
3818
3819 stack->ptr = ptr;
3820 return parse_error(stack, ptr, "Unknown $... keyword");
3821 }
3822
3823 /*
3824 * All of the file handling code is done. Parse the input.
3825 */
3826 do {
3827 fr_skip_whitespace(ptr);
3828 if (!*ptr || (*ptr == '#')) break;
3829
3830 stack->ptr = ptr;
3831 rcode = parse_input(stack);
3832 ptr = stack->ptr;
3833
3834 if (rcode < 0) return -1;
3835 parent = frame->current;
3836 } while (rcode == 1);
3837 }
3838
3839 fr_assert(frame->fp != NULL);
3840
3841 /*
3842 * See if EOF was unexpected.
3843 */
3844 if (feof(frame->fp) && (parent != frame->parent)) {
3845 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
3847 return -1;
3848 }
3849
3850 fclose(frame->fp);
3851 frame->fp = NULL;
3852
3853pop_stack:
3854 /*
3855 * More things to read, go read them.
3856 */
3857 if (stack->depth > 0) {
3858 stack->depth--;
3859 goto do_frame;
3860 }
3861
3862 return 0;
3863}
3864
3866{
3867 cf_stack_frame_t *frame = &stack->frame[stack->depth];
3868
3869 while (stack->depth >= 0) {
3870 switch (frame->type) {
3871 case CF_STACK_FILE:
3872 if (frame->fp) fclose(frame->fp);
3873 frame->fp = NULL;
3874 break;
3875
3876#ifdef HAVE_DIRENT_H
3877 case CF_STACK_DIR:
3878 talloc_free(frame->directory);
3879 break;
3880#endif
3881
3882#ifdef HAVE_GLOB_H
3883 case CF_STACK_GLOB:
3884 globfree(&frame->glob);
3885 break;
3886#endif
3887 }
3888
3889 frame--;
3890 stack->depth--;
3891 }
3892
3893 talloc_free(stack->buff);
3894}
3895
3896/*
3897 * Bootstrap a config file.
3898 */
3899int cf_file_read(CONF_SECTION *cs, char const *filename, bool root)
3900{
3901 int i;
3902 fr_rb_tree_t *tree;
3904 cf_stack_frame_t *frame;
3905
3906 /*
3907 * Only add the default config directory if we're loading a top-level configuration file.
3908 */
3909 if (root) {
3910 char const *p;
3911 char *confdir = NULL;
3912 CONF_PAIR *cp;
3913
3914 /*
3915 * For compatibility, this goes first. And we don't care if there are too many '/'.
3916 */
3917 cp = cf_pair_alloc(cs, "raddbdir", filename, T_OP_EQ, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3918 if (!cp) return -1;
3919
3920 /*
3921 * This goes second, and we try to be nice about too many '/'.
3922 *
3923 * "confdir" is the directory portion of filename, so trim everything
3924 * from the final path separator onwards before storing it.
3925 */
3926 p = strrchr(filename, FR_DIR_SEP);
3927 if (p) {
3928 confdir = talloc_bstrndup(cs, filename, p - filename);
3929 cp = cf_pair_alloc(cs, "confdir", confdir, T_OP_EQ, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3930 talloc_free(confdir);
3931 } else {
3932 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_EQ, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3933 }
3934 if (!cp) return -1;
3935 }
3936
3937 MEM(tree = fr_rb_inline_talloc_alloc(cs, cf_file_t, node, _inode_cmp, NULL));
3938
3939 cf_data_add(cs, tree, "filename", false);
3940
3941#ifndef NDEBUG
3942 memset(&stack, 0, sizeof(stack));
3943#endif
3944
3945 /*
3946 * Allocate temporary buffers on the heap (so we don't use *all* the stack space)
3947 */
3948 stack.buff = talloc_array(cs, char *, 4);
3949 for (i = 0; i < 4; i++) MEM(stack.buff[i] = talloc_array(stack.buff, char, 8192));
3950
3951 stack.depth = 0;
3952 stack.bufsize = 8192;
3953 frame = &stack.frame[stack.depth];
3954
3955 memset(frame, 0, sizeof(*frame));
3956 frame->parent = frame->current = cs;
3957
3958 frame->type = CF_STACK_FILE;
3959 frame->filename = talloc_strdup(frame->parent, filename);
3960 cs->item.filename = frame->filename;
3961
3962 if (cf_file_include(&stack) < 0) {
3964 return -1;
3965 }
3966
3967 talloc_free(stack.buff);
3968
3969 /*
3970 * Now that we've read the file, go back through it and
3971 * expand the variables.
3972 */
3973 if (cf_section_pass2(cs) < 0) {
3974 cf_log_err(cs, "Parsing config items failed");
3975 return -1;
3976 }
3977
3978 return 0;
3979}
3980
3981/** Bootstrap a configuration section from an in-memory buffer.
3982 *
3983 * Mirrors cf_file_read(), but reads the configuration text from a buffer via fmemopen() instead of from a
3984 * file on disk. The buffer is treated as if it were the contents of "filename", which is used in error
3985 * messages and stored as the section's source filename.
3986 *
3987 * $INCLUDE / $-INCLUDEs are taken relative to the directory portion of "filename", as with cf_file_read().
3988 *
3989 * Unlike cf_file_read(), this function never sets the "raddbdir" / "confdir" CONF_PAIRs (the buffer has no
3990 * canonical install path) and never inserts "filename" into the dedup tree.
3991 *
3992 * @param[in] cs Top-level configuration section to populate.
3993 * @param[in] buffer The configuration text. The caller retains ownership
3994 * and may free the buffer after this call returns.
3995 * @param[in] buflen Length of @p buffer in bytes (excluding any trailing NUL).
3996 * @param[in] filename Virtual filename for error reporting. May be a
3997 * placeholder like "<inline>" if there is no real file.
3998 * @return 0 on success, -1 on failure.
3999 */
4000int cf_file_read_buffer(CONF_SECTION *cs, char const *buffer, size_t buflen, char const *filename)
4001{
4002 int i;
4003 FILE *fp;
4004 fr_rb_tree_t *tree;
4006 cf_stack_frame_t *frame;
4007
4008 fp = fmemopen(UNCONST(char *, buffer), buflen, "r");
4009 if (!fp) {
4010 ERROR("Failed opening in-memory buffer for parsing: %s", fr_syserror(errno));
4011 return -1;
4012 }
4013
4014 /*
4015 * The dedup-by-inode filename tree may already exist if the caller
4016 * is reading more than one buffer / file into the same root section.
4017 */
4018 tree = cf_data_value(cf_data_find(cs, fr_rb_tree_t, "filename"));
4019 if (!tree) {
4020 MEM(tree = fr_rb_inline_talloc_alloc(cs, cf_file_t, node, _inode_cmp, NULL));
4021 cf_data_add(cs, tree, "filename", false);
4022 }
4023
4024#ifndef NDEBUG
4025 memset(&stack, 0, sizeof(stack));
4026#endif
4027
4028 /*
4029 * Allocate temporary buffers on the heap (so we don't use *all* the stack space)
4030 */
4031 stack.buff = talloc_array(cs, char *, 4);
4032 for (i = 0; i < 4; i++) MEM(stack.buff[i] = talloc_array(stack.buff, char, 8192));
4033
4034 stack.depth = 0;
4035 stack.bufsize = 8192;
4036 frame = &stack.frame[stack.depth];
4037
4038 memset(frame, 0, sizeof(*frame));
4039 frame->parent = frame->current = cs;
4040
4041 frame->type = CF_STACK_FILE;
4042 frame->filename = talloc_strdup(frame->parent, filename);
4043 cs->item.filename = frame->filename;
4044
4045 /*
4046 * Pre-set frame->fp so cf_file_include() skips its own cf_file_open()
4047 * call (which would try to fopen() a real path). cf_file_include()
4048 * fclose()s frame->fp on the way out, which for an fmemopen() handle
4049 * does not free the underlying buffer.
4050 */
4051 frame->fp = fp;
4052
4053 if (cf_file_include(&stack) < 0) {
4055 return -1;
4056 }
4057
4058 talloc_free(stack.buff);
4059
4060 /*
4061 * Now that we've read the buffer, go back through it and
4062 * expand the variables.
4063 */
4064 if (cf_section_pass2(cs) < 0) {
4065 cf_log_err(cs, "Parsing config items failed");
4066 return -1;
4067 }
4068
4069 return 0;
4070}
4071
4073{
4074 talloc_free(cs);
4075}
4076
4077static char const parse_tabs[] = " ";
4078
4079static ssize_t cf_string_write(FILE *fp, char const *string, size_t len, fr_token_t t)
4080{
4081 size_t outlen;
4082 char c;
4083 char buffer[2048];
4084
4085 switch (t) {
4086 default:
4087 c = '\0';
4088 break;
4089
4091 c = '"';
4092 break;
4093
4095 c = '\'';
4096 break;
4097
4099 c = '`';
4100 break;
4101 }
4102
4103 if (c) fprintf(fp, "%c", c);
4104
4105 outlen = fr_snprint(buffer, sizeof(buffer), string, len, c);
4106 fwrite(buffer, outlen, 1, fp);
4107
4108 if (c) fprintf(fp, "%c", c);
4109 return 1;
4110}
4111
4112int cf_pair_write(FILE *fp, CONF_PAIR *cp)
4113{
4114 if (!cp->value) {
4115 fprintf(fp, "%s\n", cp->attr);
4116 return 0;
4117 }
4118
4119 cf_string_write(fp, cp->attr, strlen(cp->attr), cp->lhs_quote);
4120 fprintf(fp, " %s ", fr_table_str_by_value(fr_tokens_table, cp->op, "<INVALID>"));
4121 cf_string_write(fp, cp->value, strlen(cp->value), cp->rhs_quote);
4122 fprintf(fp, "\n");
4123
4124 return 1; /* FIXME */
4125}
4126
4127
4128int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth)
4129{
4130 if (!fp || !cs) return -1;
4131
4132 /*
4133 * Print the section name1, etc.
4134 */
4135 fwrite(parse_tabs, depth, 1, fp);
4136 cf_string_write(fp, cs->name1, strlen(cs->name1), T_BARE_WORD);
4137
4138 /*
4139 * FIXME: check for "if" or "elsif". And if so, print
4140 * out the parsed condition, instead of the input text
4141 *
4142 * cf_data_find(cs, CF_DATA_TYPE_UNLANG, "if");
4143 */
4144 if (cs->name2) {
4145 fputs(" ", fp);
4146
4147#if 0
4148 c = cf_data_value(cf_data_find(cs, fr_cond_t, NULL));
4149 if (c) {
4150 char buffer[1024];
4151
4152 cond_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), c);
4153 fprintf(fp, "(%s)", buffer);
4154 } else
4155#endif
4156 cf_string_write(fp, cs->name2, strlen(cs->name2), cs->name2_quote);
4157 }
4158
4159 fputs(" {\n", fp);
4160
4161 cf_section_write_children(fp, cs, depth + 1);
4162
4163 fwrite(parse_tabs, depth, 1, fp);
4164 fputs("}\n\n", fp);
4165
4166 return 1;
4167}
4168
4169/** Emit the children of a section at `depth` without an enclosing `{ ... }`.
4170 *
4171 * cf_section_write wraps a section in `name { ... }`; this helper writes only
4172 * the children at the requested indent, which is what tools like
4173 * `radjson2conf -r` need: rendering a synthetic-root section as a file-scope
4174 * fragment ready to be `$INCLUDE`d at any depth.
4175 *
4176 * Blank lines in the source come back through as NULL-text CONF_ITEM_COMMENT
4177 * markers, so the writer doesn't have to synthesise its own separators - just
4178 * emit what's there. Consecutive blank markers collapse to a single blank
4179 * line on output so artifacts from upstream tooling (deletes that left their
4180 * preceding blank behind, splits that introduced extra spacers) don't
4181 * accumulate as visible whitespace.
4182 */
4184{
4185 bool prev_was_blank = false;
4186
4187 if (!fp || !cs) return -1;
4188
4189 cf_item_foreach(&cs->item, ci) {
4190 switch (ci->type) {
4191 case CONF_ITEM_SECTION:
4193 /*
4194 * Sections close with `}\n\n` so the trailing
4195 * blank line is already in the stream; flag it
4196 * so a NULL-text comment immediately after
4197 * doesn't double it up.
4198 */
4199 prev_was_blank = true;
4200 break;
4201
4202 case CONF_ITEM_PAIR:
4203 /*
4204 * Ignore internal things.
4205 */
4206 if (!ci->filename || (ci->filename[0] == '<')) break;
4207
4208 fwrite(parse_tabs, depth, 1, fp);
4210 prev_was_blank = false;
4211 break;
4212
4213 case CONF_ITEM_COMMENT: {
4214 CONF_COMMENT const *c = cf_item_to_comment(ci);
4215
4216 /*
4217 * NULL text == blank line. Collapse
4218 * runs of blanks to a single one.
4219 */
4220 if (c->text == NULL) {
4221 if (!prev_was_blank) {
4222 fputc('\n', fp);
4223 prev_was_blank = true;
4224 }
4225 break;
4226 }
4227
4228 fwrite(parse_tabs, depth, 1, fp);
4229 fputc('#', fp);
4230 fputs(c->text, fp);
4231 fputc('\n', fp);
4232 prev_was_blank = false;
4233 } break;
4234
4235 default:
4236 break;
4237 }
4238 }
4239
4240 return 1;
4241}
4242
4243
4245 CONF_SECTION const *outer_cs,
4246 char const *ptr)
4247{
4248 CONF_PAIR *cp;
4249 CONF_SECTION *next;
4250 CONF_SECTION const *cs = outer_cs;
4251 char name[8192], *p;
4252 char const *name2;
4253
4254 if (!ptr || (!parent_cs && !outer_cs)) {
4255 fr_strerror_const("Invalid argument");
4256 return NULL;
4257 }
4258
4259 if (!*ptr) {
4260 fr_strerror_const("Empty string is invalid");
4261 return NULL;
4262 }
4263
4264 strlcpy(name, ptr, sizeof(name));
4265
4266 p = name;
4267
4268 /*
4269 * ".foo" means "foo from the current section"
4270 */
4271 if (*p == '.') {
4272 p++;
4273
4274 /*
4275 * Just '.' means the current section
4276 */
4277 if (*p == '\0') return cf_section_to_item(cs); /* const issues */
4278
4279 /*
4280 * ..foo means "foo from the section
4281 * enclosing this section" (etc.)
4282 */
4283 while (*p == '.') {
4284 if (cs->item.parent) cs = cf_item_to_section(cs->item.parent);
4285
4286 /*
4287 * .. means the section
4288 * enclosing this section
4289 */
4290 if (!*++p) return cf_section_to_item(cs); /* const issues */
4291 }
4292
4293 /*
4294 * "foo.bar.baz" means "from the given root"
4295 */
4296 } else if (strchr(p, '.') != NULL) {
4297 if (!parent_cs) {
4298 missing_parent:
4299 fr_strerror_const("Missing parent configuration section");
4300 return NULL;
4301 }
4302 cs = parent_cs;
4303
4304 /*
4305 * "foo" could be from the current section, either as a
4306 * section or as a pair.
4307 *
4308 * If that isn't found, search from the given root.
4309 */
4310 } else {
4311 next = cf_section_find(cs, p, NULL);
4312 if (!next && cs->template) next = cf_section_find(cs->template, p, NULL);
4313 if (next) return &(next->item);
4314
4315 cp = cf_pair_find(cs, p);
4316 if (!cp && cs->template) cp = cf_pair_find(cs->template, p);
4317 if (cp) return &(cp->item);
4318
4319 if (!parent_cs) goto missing_parent;
4320 cs = parent_cs;
4321 }
4322
4323 /*
4324 * Chop the string into pieces, and look up the pieces.
4325 */
4326 while (*p) {
4327 char *n1, *n2, *q;
4328
4329 n1 = p;
4330 n2 = NULL;
4331 q = p;
4332
4333 fr_assert(*q);
4334
4335 /*
4336 * Look for a terminating '.' or '[', to get name1 and possibly name2.
4337 */
4338 while (*q != '\0') {
4339 /*
4340 * foo.bar -> return "foo"
4341 */
4342 if (*q == '.') {
4343 *q++ = '\0'; /* leave 'q' after the '.' */
4344 break;
4345 }
4346
4347 /*
4348 * name1 is anything up to '[' or EOS.
4349 */
4350 if (*q != '[') {
4351 q++;
4352 continue;
4353 }
4354
4355 /*
4356 * Found "name1[", look for "name2]" or "name2]."
4357 */
4358 *q++ = '\0';
4359 n2 = q;
4360
4361 while (*q != '\0') {
4362 if (*q == '[') {
4363 fr_strerror_const("Invalid reference, '[' cannot be used inside of a '[...]' block");
4364 return NULL;
4365 }
4366
4367 if (*q != ']') {
4368 q++;
4369 continue;
4370 }
4371
4372 /*
4373 * We've found the trailing ']'
4374 */
4375 *q++ = '\0';
4376
4377 /*
4378 * "name2]"
4379 */
4380 if (!*q) break;
4381
4382 /*
4383 * Must be "name2]."
4384 */
4385 if (*q++ == '.') break;
4386
4387 fr_strerror_const("Invalid reference, ']' is not followed by '.'");
4388 return NULL;
4389 }
4390
4391 /*
4392 * "name1[name2", but not "name1[name2]"
4393 *
4394 * The inner loop exited because it found *q == '\0',
4395 * meaning that the closing ']' was never found.
4396 */
4397 if (!*q) {
4398 fr_strerror_printf("Invalid reference after '%s', missing close ']'", n1);
4399 return NULL;
4400 }
4401
4402 break;
4403 }
4404 p = q; /* get it ready for the next round */
4405
4406 /*
4407 * End of the string. The thing could be a section with
4408 * two names, a section with one name, or a pair.
4409 *
4410 * And if we don't find the thing we're looking for here,
4411 * check the template section.
4412 */
4413 if (!*p) {
4414 /*
4415 * Two names, must be a section.
4416 */
4417 if (n2) {
4418 next = cf_section_find(cs, n1, n2);
4419 if (!next && cs->template) next = cf_section_find(cs->template, n1, n2);
4420 if (next) return &(next->item);
4421
4422 fail:
4423 name2 = cf_section_name2(cs);
4424 fr_strerror_printf("Parent section %s%s%s { ... } does not contain a %s %s { ... } configuration section",
4425 cf_section_name1(cs),
4426 name2 ? " " : "", name2 ? name2 : "",
4427 n1, n2);
4428 return NULL;
4429 }
4430
4431 /*
4432 * One name, the final thing can be a section or a pair.
4433 */
4434 next = cf_section_find(cs, n1, NULL);
4435 if (!next && cs->template) next = cf_section_find(cs->template, n1, NULL);
4436
4437 if (next) return &(next->item);
4438
4439 cp = cf_pair_find(cs, n1);
4440 if (!cp && cs->template) cp = cf_pair_find(cs->template, n1);
4441 if (cp) return &(cp->item);
4442
4443 name2 = cf_section_name2(cs);
4444 fr_strerror_printf("Parent section %s%s%s { ... } does not contain a %s configuration item",
4445 cf_section_name1(cs),
4446 name2 ? " " : "", name2 ? name2 : "",
4447 n1);
4448 return NULL;
4449 }
4450
4451 /*
4452 * There's more to the string. The thing we're looking
4453 * for MUST be a configuration section.
4454 */
4455 next = cf_section_find(cs, n1, n2);
4456 if (!next && cs->template) next = cf_section_find(cs->template, n1, n2);
4457 if (next) {
4458 cs = next;
4459 continue;
4460 }
4461
4462 if (n2) goto fail;
4463
4464 name2 = cf_section_name2(cs);
4465 fr_strerror_printf("Parent section %s%s%s { ... } does not contain a %s { ... } configuration section",
4466 cf_section_name1(cs),
4467 name2 ? " " : "", name2 ? name2 : "",
4468 n1);
4469 return NULL;
4470 }
4471
4472 /*
4473 * We've fallen off of the end of the string. This should not have happened!
4474 */
4475 fr_strerror_const("Cannot parse reference");
4476 return NULL;
4477}
4478
4479/*
4480 * Only for unit_test_map
4481 */
4483{
4485 fr_assert(!cs->item.parent);
4486
4487 cs->unlang = CF_UNLANG_ALLOW;
4488}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
strcpy(log_entry->msg, buffer)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define RCSID(id)
Definition build.h:512
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:228
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:343
#define CMP_RETURN(_a, _b, _field)
Return if the comparison is not 0 (is unequal)
Definition build.h:122
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:113
#define UNUSED
Definition build.h:336
#define NUM_ELEMENTS(_t)
Definition build.h:358
int at_reference_braces
braces when we found this thing
Definition cf_file.c:136
CONF_SECTION * current
sub-section we're reading
Definition cf_file.c:134
cf_stack_file_t type
Definition cf_file.c:107
static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
Definition cf_file.c:628
static int parse_input(cf_stack_t *stack)
Definition cf_file.c:2533
int cf_section_write_children(FILE *fp, CONF_SECTION *cs, int depth)
Emit the children of a section at depth without an enclosing { ... }.
Definition cf_file.c:4183
char * fill
where we start filling the buffer from
Definition cf_file.c:153
void cf_section_set_unlang(CONF_SECTION *cs)
Definition cf_file.c:4482
conf_property
Definition cf_file.c:65
@ CONF_PROPERTY_NAME
Definition cf_file.c:67
@ CONF_PROPERTY_INSTANCE
Definition cf_file.c:68
@ CONF_PROPERTY_INVALID
Definition cf_file.c:66
static int unlang_keywords_len
Definition cf_file.c:2507
cf_file_extension_t
Definition cf_file.c:1263
@ CF_EXTENSION_PKG
Definition cf_file.c:1265
@ CF_EXTENSION_READ
Definition cf_file.c:1264
@ CF_EXTENSION_TXT
Definition cf_file.c:1266
static fr_table_num_sorted_t const server_unlang_section[]
Definition cf_file.c:77
int cf_file_read_buffer(CONF_SECTION *cs, char const *buffer, size_t buflen, char const *filename)
Bootstrap a configuration section from an in-memory buffer.
Definition cf_file.c:4000
bool check_config
Definition cf_file.c:61
fr_heap_index_t heap_id
Definition cf_file.c:1250
static int frame_readdir(cf_stack_t *stack)
Definition cf_file.c:3480
static CONF_ITEM * process_map(cf_stack_t *stack)
Definition cf_file.c:1790
static CONF_ITEM * process_foreach(cf_stack_t *stack)
Definition cf_file.c:2162
static int add_pair(CONF_SECTION *parent, char const *attr, char const *value, fr_token_t name1_token, fr_token_t op_token, fr_token_t value_token, char *buff, char const *filename, int lineno)
Definition cf_file.c:2347
static int cf_expand_file(char const *cf, int lineno, char name[static PATH_MAX], char **p_p, char const **ptr_p, char *output, size_t outsize, bool raw)
Definition cf_file.c:160
void cf_md5_init(void)
Definition cf_file.c:3517
char const * cf_expand_variables(char const *cf, int lineno, CONF_SECTION *outer_cs, char *output, size_t outsize, char const *input, ssize_t inlen, bool *soft_fail, bool soft_fail_env)
Definition cf_file.c:297
static int parse_type_name(cf_stack_t *stack, char const **ptr_p, char const *type_ptr, fr_type_t *type_p)
Definition cf_file.c:2111
static const bool cf_name_char1[SBUFF_CHAR_CLASS]
Convert tokens back to a quoting character.
Definition cf_file.c:2522
static void cf_stack_cleanup(cf_stack_t *stack)
Definition cf_file.c:3865
int depth
stack depth
Definition cf_file.c:151
cf_file_check_err_t cf_file_check_unix_perm(char const *filename, UNUSED void *uctx)
Check if file exists, and is a socket.
Definition cf_file.c:972
static const bool terminal_end_line[SBUFF_CHAR_CLASS]
Definition cf_file.c:1627
static int cf_file_open(CONF_SECTION *cs, char const *filename, bool from_dir, FILE **fp_p)
Definition cf_file.c:713
int cf_file_read(CONF_SECTION *cs, char const *filename, bool root)
Definition cf_file.c:3899
static fr_md5_ctx_t * cf_md5_ctx
Definition cf_file.c:3515
static int8_t filename_cmp(void const *one, void const *two)
Definition cf_file.c:1253
int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth)
Definition cf_file.c:4128
int lineno
line in that filename
Definition cf_file.c:110
cf_stack_file_t
Definition cf_file.c:95
@ CF_STACK_FILE
Definition cf_file.c:96
static const bool terminal_end_section[SBUFF_CHAR_CLASS]
Definition cf_file.c:1623
static size_t conf_property_name_len
Definition cf_file.c:75
static fr_table_num_sorted_t const conf_property_name[]
Definition cf_file.c:71
int cf_section_pass2(CONF_SECTION *cs)
Definition cf_file.c:1130
bool from_dir
this file was read from $include foo/
Definition cf_file.c:139
static ssize_t cf_string_write(FILE *fp, char const *string, size_t len, fr_token_t t)
Definition cf_file.c:4079
static char const parse_tabs[]
Definition cf_file.c:4077
cf_file_check_err_t cf_file_check(CONF_PAIR *cp, bool check_perms)
Do some checks on the file as an "input" file.
Definition cf_file.c:1068
static int cf_file_include(cf_stack_t *stack)
Definition cf_file.c:3683
static uid_t conf_check_uid
Definition cf_file.c:62
static gid_t conf_check_gid
Definition cf_file.c:63
CONF_ITEM *(* cf_process_func_t)(cf_stack_t *)
Definition cf_file.c:2509
static int cf_get_token(CONF_SECTION *parent, char const **ptr_p, fr_token_t *token, char *buffer, size_t buflen, char const *filename, int lineno)
Definition cf_file.c:1199
static int8_t _inode_cmp(void const *one, void const *two)
Definition cf_file.c:704
CONF_SECTION * at_reference
was this thing an @foo ?
Definition cf_file.c:135
cf_file_check_err_t cf_file_check_open_read(char const *filename, void *uctx)
Callback for cf_file_check to open a file and check permissions.
Definition cf_file.c:1018
static int cf_file_fill(cf_stack_t *stack)
Definition cf_file.c:3542
static const size_t cf_file_extensions_len
Definition cf_file.c:1277
char const * ptr
current parse pointer
Definition cf_file.c:152
cf_file_check_err_t cf_file_check_effective(char const *filename, cf_file_check_err_t(*cb)(char const *filename, void *uctx), void *uctx)
Perform an operation with the effect/group set to conf_check_gid and conf_check_uid.
Definition cf_file.c:839
CONF_ITEM * cf_reference_item(CONF_SECTION const *parent_cs, CONF_SECTION const *outer_cs, char const *ptr)
Definition cf_file.c:4244
char const * filename
Definition cf_file.c:1249
static CONF_ITEM * process_subrequest(cf_stack_t *stack)
Definition cf_file.c:1878
void cf_file_check_set_uid_gid(uid_t uid, gid_t gid)
Set the euid/egid used when performing file checks.
Definition cf_file.c:824
enum conf_property CONF_PROPERTY
static fr_table_ptr_sorted_t unlang_keywords[]
Definition cf_file.c:2498
char ** buff
buffers for reading / parsing
Definition cf_file.c:149
CONF_SECTION * parent
which started this file
Definition cf_file.c:133
static int process_template(cf_stack_t *stack)
Definition cf_file.c:1566
size_t bufsize
size of the buffers
Definition cf_file.c:150
static size_t server_unlang_section_len
Definition cf_file.c:93
void cf_md5_final(uint8_t *digest)
Definition cf_file.c:3530
static int process_include(cf_stack_t *stack, CONF_SECTION *parent, char const *ptr, bool required, bool relative)
Definition cf_file.c:1280
static CONF_ITEM * process_if(cf_stack_t *stack)
Definition cf_file.c:1639
static void cf_md5_update(char const *p)
Definition cf_file.c:3523
void cf_file_free(CONF_SECTION *cs)
Definition cf_file.c:4072
static char const * cf_local_file(char const *base, char const *filename, char *buffer, size_t bufsize)
Definition cf_file.c:1170
#define MAX_STACK
Definition cf_file.c:105
static CONF_ITEM * process_catch(cf_stack_t *stack)
Definition cf_file.c:1999
int cf_pair_write(FILE *fp, CONF_PAIR *cp)
Definition cf_file.c:4112
cf_file_check_err_t cf_file_check_unix_connect(char const *filename, UNUSED void *uctx)
Check if we can connect to a unix socket.
Definition cf_file.c:899
char const * filename
filename we're reading
Definition cf_file.c:109
static CONF_ITEM * process_switch(cf_stack_t *stack)
Definition cf_file.c:2406
static int parse_error(cf_stack_t *stack, char const *ptr, char const *message)
Definition cf_file.c:2091
static fr_table_num_sorted_t const cf_file_extensions[]
Definition cf_file.c:1269
cf_file_check_err_t
Results of file checks.
Definition cf_file.h:45
@ CF_FILE_OK
File checks passed.
Definition cf_file.h:46
@ CF_FILE_NO_PERMISSION
Requested permissions not set.
Definition cf_file.h:47
@ CF_FILE_NO_UNIX_SOCKET
File is not a unix socket.
Definition cf_file.h:49
@ CF_FILE_OTHER_ERROR
Other error occurred checking permissions.
Definition cf_file.h:50
@ CF_FILE_NO_EXIST
File does not exist.
Definition cf_file.h:48
cf_parse_t on_read
Function to call as the item is being read, just after it has been allocated and initialized.
Definition cf_parse.h:626
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:606
CONF_SECTION * cs
CONF_SECTION associated with the file.
Definition cf_priv.h:169
CONF_ITEM item
Common set of fields.
Definition cf_priv.h:107
CONF_ITEM * parent
Parent.
Definition cf_priv.h:61
fr_token_t name2_quote
The type of quoting around name2.
Definition cf_priv.h:112
char const * name2
Second name token. Given foo bar {} would be bar.
Definition cf_priv.h:110
bool allow_locals
allow local variables
Definition cf_priv.h:121
int argc
number of additional arguments
Definition cf_priv.h:114
char const * attr
Attribute name.
Definition cf_priv.h:80
#define CONF_SECTION_MAX_DEPTH
Definition cf_priv.h:126
bool referenced
Was this item referenced in the config?
Definition cf_priv.h:69
fr_token_t rhs_quote
Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
Definition cf_priv.h:85
int depth
Definition cf_priv.h:119
char const * value
Attribute value.
Definition cf_priv.h:81
cf_unlang_t unlang
Definition cf_priv.h:120
char const * name1
First name token. Given foo bar {} would be foo.
Definition cf_priv.h:109
@ CF_UNLANG_MODULES
this section is in "modules", allow unlang 2 down
Definition cf_priv.h:96
@ CF_UNLANG_NONE
no unlang
Definition cf_priv.h:92
@ CF_UNLANG_CAN_HAVE_UPDATE
can have "update"
Definition cf_priv.h:100
@ CF_UNLANG_ALLOW
allow unlang in this section
Definition cf_priv.h:93
@ CF_UNLANG_POLICY
this section is a policy, allow unlang 2 down
Definition cf_priv.h:95
@ CF_UNLANG_ASSIGNMENT
only assignments inside of map / update
Definition cf_priv.h:98
@ CF_UNLANG_EDIT
only edit commands
Definition cf_priv.h:97
@ CF_UNLANG_DICTIONARY
only local variable definitions
Definition cf_priv.h:99
@ CF_UNLANG_SERVER
this section is a virtual server, allow unlang 2 down
Definition cf_priv.h:94
fr_token_t * argv_quote
Definition cf_priv.h:116
fr_token_t op
Operator e.g. =, :=.
Definition cf_priv.h:83
CONF_ITEM item
Common set of fields.
Definition cf_priv.h:78
bool pass2
do expansion in pass2.
Definition cf_priv.h:87
char const * filename
The file the config item was parsed from.
Definition cf_priv.h:71
struct stat buf
stat about the file
Definition cf_priv.h:170
bool at_reference
this thing was created from an ...
Definition cf_priv.h:122
bool _cf_expand_variables(void)
Definition cf_util.c:837
bool _cf_preserve_comments(void)
Definition cf_util.c:817
CONF_SECTION * template
Definition cf_priv.h:124
char const * filename
name of the file
Definition cf_priv.h:168
char const * text
Comment text, with the leading # and any surrounding whitespace stripped.
Definition cf_priv.h:149
@ CONF_ITEM_PAIR
Definition cf_priv.h:41
@ CONF_ITEM_SECTION
Definition cf_priv.h:42
@ CONF_ITEM_COMMENT
A # ... line preserved verbatim from the input - only created when the parser is asked to keep commen...
Definition cf_priv.h:44
char const ** argv
additional arguments
Definition cf_priv.h:115
fr_token_t lhs_quote
Name quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
Definition cf_priv.h:84
int lineno
The line number the config item began on.
Definition cf_priv.h:70
CONF_ITEM_TYPE type
Whether the config item is a config_pair, conf_section or cf_data.
Definition cf_priv.h:66
A # ... comment line preserved verbatim from the input.
Definition cf_priv.h:146
Internal data that is associated with a configuration section.
Definition cf_priv.h:156
Common header for all CONF_* types.
Definition cf_priv.h:54
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:77
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
CONF_PAIR * cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp, bool copy_meta)
Duplicate a CONF_PAIR.
Definition cf_util.c:1483
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1359
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
Definition cf_util.c:1913
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:746
CONF_PAIR * cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value, fr_token_t op, fr_token_t lhs_quote, fr_token_t rhs_quote)
Allocate a CONF_PAIR.
Definition cf_util.c:1441
char const * cf_section_name1(CONF_SECTION const *cs)
Return the first identifier of a CONF_SECTION.
Definition cf_util.c:1345
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1201
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:692
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
Definition cf_util.c:1594
CONF_COMMENT * cf_comment_alloc(CONF_SECTION *parent, char const *text)
Allocate a new comment item attached to parent.
Definition cf_util.c:842
CONF_COMMENT * cf_item_to_comment(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_COMMENT (asserts on type mismatch).
Definition cf_util.c:779
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
Definition cf_util.c:626
CONF_SECTION * cf_section_dup(TALLOC_CTX *ctx, CONF_SECTION *parent, CONF_SECTION const *cs, char const *name1, char const *name2, bool copy_meta)
Duplicate a configuration section.
Definition cf_util.c:1032
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:672
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1753
CONF_ITEM * cf_pair_to_item(CONF_PAIR const *cp)
Cast a CONF_PAIR to a CONF_ITEM.
Definition cf_util.c:730
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:345
#define cf_lineno(_cf)
Definition cf_util.h:121
#define cf_data_add(_cf, _data, _name, _free)
Definition cf_util.h:311
#define cf_data_find(_cf, _type, _name)
Definition cf_util.h:300
#define cf_lineno_set(_ci, _lineno)
Definition cf_util.h:186
#define cf_root(_cf)
Definition cf_util.h:115
#define cf_parent(_cf)
Definition cf_util.h:118
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
Definition cf_util.h:423
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:352
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:201
#define CF_TO_ITEM(_cf)
Auto cast from the input type to CONF_ITEM (which is the base type)
Definition cf_util.h:65
#define cf_item_foreach(_parent, _iter)
Iterate over every child item of a CONF_SECTION (or any CONF_ITEM that has children).
Definition cf_util.h:109
#define cf_filename_set(_ci, _filename)
Definition cf_util.h:183
#define cf_log_warn(_cf, _fmt,...)
Definition cf_util.h:346
#define cf_log_debug(_cf, _fmt,...)
Definition cf_util.h:348
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:40
Test enumeration values.
Definition dict_test.h:92
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
Definition heap.c:146
void * fr_heap_pop(fr_heap_t **hp)
Remove a node from the heap.
Definition heap.c:325
unsigned int fr_heap_index_t
Definition heap.h:80
#define fr_heap_alloc(_ctx, _cmp, _type, _field, _init)
Creates a heap that can be used with non-talloced elements.
Definition heap.h:100
#define FR_HEAP_INDEX_INVALID
Definition heap.h:83
The main heap structure.
Definition heap.h:66
talloc_free(hp)
#define PERROR(_fmt,...)
Definition log.h:233
#define DEBUG_ENABLED2
True if global debug level 1-2 messages are enabled.
Definition log.h:263
int fr_dirfd(int *dirfd, char const **filename, char const *pathname)
From a pathname, return fd and filename needed for *at() functions.
Definition file.c:411
void fr_canonicalize_error(TALLOC_CTX *ctx, char **sp, char **text, ssize_t slen, char const *fmt)
Canonicalize error strings, removing tabs, and generate spaces for error marker.
Definition log.c:105
static char * stack[MAX_STACK]
Definition radmin.c:158
#define fr_md5_final(_out, _ctx)
Finalise the ctx, producing the digest.
Definition md5.h:90
#define fr_md5_ctx_alloc()
Allocation function for MD5 digest context.
Definition md5.h:68
void fr_md5_ctx_t
Definition md5.h:25
#define fr_md5_update(_ctx, _in, _inlen)
Ingest plaintext into the digest.
Definition md5.h:83
#define fr_md5_ctx_free(_ctx)
Free function for MD5 digest ctx.
Definition md5.h:75
#define MD5_DIGEST_LENGTH
fr_type_t
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_MAX
Number of defined data types.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_VALUE_BOX
A boxed value.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
int strncasecmp(char *s1, char *s2, int n)
Definition missing.c:35
void fr_perm_file_error(int num)
Write a file access error to the fr_strerror buffer, including euid/egid.
Definition perm.c:533
size_t fr_snprint(char *out, size_t outlen, char const *in, ssize_t inlen, char quote)
Escape any non printable or non-UTF8 characters in the input string.
Definition print.c:237
#define fr_assert(_expr)
Definition rad_assert.h:37
#define WARN(fmt,...)
static const char * spaces
Definition radict.c:177
static fr_token_t op_token(char const *s)
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:244
The main red black tree structure.
Definition rb.h:71
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:57
static char const * name
#define SBUFF_CHAR_CLASS
Definition sbuff.h:203
#define FR_SBUFF_OUT(_start, _len_or_end)
ssize_t tmpl_preparse(char const **out, size_t *outlen, char const *in, size_t inlen, fr_token_t *type))
Preparse a string in preparation for passing it to tmpl_afrom_substr()
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:37
ssize_t fr_skip_condition(char const *start, char const *end, bool const terminal[static SBUFF_CHAR_CLASS], bool *eol)
Skip a conditional expression.
Definition skip.c:317
ssize_t fr_skip_xlat(char const *start, char const *end)
Skip an xlat expression.
Definition skip.c:296
#define fr_skip_whitespace(_p)
Skip whitespace ('\t', '\n', '\v', '\f', '\r', ' ')
Definition skip.h:36
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
fr_aka_sim_id_type_t type
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition strlcpy.c:34
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_value_by_longest_prefix(_match_len, _table, _name, _name_len, _def)
Find the longest string match using a sorted or ordered table.
Definition table.h:764
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:685
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
An element in a lexicographically sorted array of name to ptr mappings.
Definition table.h:65
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:618
#define talloc_strndup(_ctx, _str, _len)
Definition talloc.h:150
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:288
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
const bool fr_assignment_op[T_TOKEN_LAST]
Definition token.c:236
const bool fr_list_assignment_op[T_TOKEN_LAST]
Definition token.c:253
fr_token_t gettoken(char const **ptr, char *buf, int buflen, bool unescape)
Definition token.c:536
fr_table_num_ordered_t const fr_tokens_table[]
Definition token.c:33
const bool fr_str_tok[T_TOKEN_LAST]
Definition token.c:299
int getword(char const **ptr, char *buf, int buflen, bool unescape)
Definition token.c:527
enum fr_token fr_token_t
@ T_OP_SUB_EQ
Definition token.h:68
@ T_INVALID
Definition token.h:37
@ T_OP_DIV_EQ
Definition token.h:70
@ T_EOL
Definition token.h:38
@ T_SINGLE_QUOTED_STRING
Definition token.h:120
@ T_OP_AND_EQ
Definition token.h:72
@ T_OP_CMP_TRUE
Definition token.h:102
@ T_BARE_WORD
Definition token.h:118
@ T_OP_EQ
Definition token.h:81
@ T_OP_MUL_EQ
Definition token.h:69
@ T_BACK_QUOTED_STRING
Definition token.h:121
@ T_HASH
Definition token.h:117
@ T_OP_SET
Definition token.h:82
@ T_OP_NE
Definition token.h:95
@ T_OP_ADD_EQ
Definition token.h:67
@ T_OP_CMP_FALSE
Definition token.h:103
@ T_OP_LSHIFT_EQ
Definition token.h:75
@ T_OP_RSHIFT_EQ
Definition token.h:74
@ T_DOUBLE_QUOTED_STRING
Definition token.h:119
@ T_OP_CMP_EQ
Definition token.h:104
@ T_OP_LE
Definition token.h:98
@ T_OP_GE
Definition token.h:96
@ T_OP_GT
Definition token.h:97
@ T_OP_OR_EQ
Definition token.h:71
@ T_OP_LT
Definition token.h:99
@ T_OP_PREPEND
Definition token.h:83
static fr_slen_t parent
Definition pair.h:858
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:581
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84
#define fr_strerror_const(_msg)
Definition strerror.h:223
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
#define fr_type_is_leaf(_x)
Definition types.h:393
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
static fr_type_t fr_type_from_str(char const *type)
Return the constant value representing a type.
Definition types.h:479
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1030
static size_t char ** out
Definition value.h:1030