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: 3a8baa19cae357ec04d6d724871c037f2d29e23c $
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: 3a8baa19cae357ec04d6d724871c037f2d29e23c $")
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 fr_cmp_ret_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 fr_rb_find((void **)&file, 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) != 0) 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) != 0) 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 fr_cmp_ret_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 bool do_glob = false;
1283 char const *value;
1284 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1285
1286 /*
1287 * Can't do this inside of update / map.
1288 */
1289 if (parent->unlang == CF_UNLANG_ASSIGNMENT) {
1290 ERROR("%s[%d]: Parse error: Invalid location for $INCLUDE",
1291 frame->filename, frame->lineno);
1292 return -1;
1293 }
1294
1295 fr_skip_whitespace(ptr);
1296
1297 /*
1298 * Grab all of the non-whitespace text.
1299 */
1300 value = ptr;
1301 while (*ptr && !isspace((uint8_t) *ptr)) {
1302 /*
1303 * Disallow control codes as filenames. There's no reason to allow weird filenames.
1304 *
1305 * This check is mainly for the fuzzers, which do weird things.
1306 */
1307 if (*ptr < ' ') {
1308 ERROR("%s[%d]: Invalid character in filename for $INCLUDE", frame->filename, frame->lineno);
1309 return -1;
1310 }
1311
1312 do_glob |= (*ptr == '*');
1313
1314 ptr++;
1315 }
1316
1317 /*
1318 * Disallow long filenames.
1319 *
1320 * This check is mainly for the fuzzers, which do weird things.
1321 */
1322 if ((size_t) (ptr - value) > 1024) {
1323 ERROR("%s[%d]: Filename too long for $INCLUDE", frame->filename, frame->lineno);
1324 return -1;
1325 }
1326
1327 /*
1328 * We're OK with whitespace after the filename.
1329 */
1330 fr_skip_whitespace(ptr);
1331
1332 /*
1333 * But anything else after the filename is wrong.
1334 */
1335 if (*ptr) {
1336 ERROR("%s[%d]: Unexpected text after $INCLUDE", frame->filename, frame->lineno);
1337 return -1;
1338 }
1339
1340 /*
1341 * Hack for ${confdir}/foo
1342 */
1343 if (*value == '$') relative = false;
1344
1345 value = cf_expand_variables(frame->filename, frame->lineno, parent, stack->buff[1], stack->bufsize,
1346 value, ptr - value, NULL, false);
1347 if (!value) return -1;
1348
1349 if (!FR_DIR_IS_RELATIVE(value)) relative = false;
1350
1351 if (relative) {
1352 value = cf_local_file(frame->filename, value, stack->buff[2], stack->bufsize);
1353 if (!value) {
1354 ERROR("%s[%d]: Directories too deep", frame->filename, frame->lineno);
1355 return -1;
1356 }
1357 }
1358
1359 if (do_glob) {
1360#ifndef HAVE_GLOB_H
1361 ERROR("%s[%d]: Filename globbing is not supported.", frame->filename, frame->lineno);
1362 return -1;
1363#else
1364 stack->depth++;
1365 frame = &stack->frame[stack->depth];
1366 memset(frame, 0, sizeof(*frame));
1367
1368 frame->type = CF_STACK_GLOB;
1369 frame->required = required;
1370 frame->parent = parent;
1371 frame->current = parent;
1372
1373 /*
1374 * For better debugging.
1375 */
1376 frame->filename = frame[-1].filename;
1377 frame->lineno = frame[-1].lineno;
1378
1379 if (glob(value, GLOB_ERR | GLOB_NOESCAPE, NULL, &frame->glob) < 0) {
1380 stack->depth--;
1381 ERROR("%s[%d]: Failed expanding '%s' - %s", frame->filename, frame->lineno,
1382 value, fr_syserror(errno));
1383 return -1;
1384 }
1385
1386 /*
1387 * If nothing matches, that may be an error.
1388 */
1389 if (frame->glob.gl_pathc == 0) {
1390 if (!required) {
1391 stack->depth--;
1392 return 0;
1393 }
1394
1395 ERROR("%s[%d]: Failed expanding '%s' - No matching files", frame->filename, frame->lineno,
1396 value);
1397 return -1;
1398 }
1399
1400 return 1;
1401#endif
1402 }
1403
1404 /*
1405 * Allow $-INCLUDE for directories, too.
1406 */
1407 if (!required) {
1408 struct stat statbuf;
1409
1410 if (stat(value, &statbuf) < 0) {
1411 WARN("Not including file %s: %s", value, fr_syserror(errno));
1412 return 0;
1413 }
1414 }
1415
1416 /*
1417 * The filename doesn't end in '/', so it must be a file.
1418 */
1419 if (value[strlen(value) - 1] != '/') {
1420 if ((stack->depth + 1) >= MAX_STACK) {
1421 ERROR("%s[%d]: Directories too deep", frame->filename, frame->lineno);
1422 return -1;
1423 }
1424
1425 stack->depth++;
1426 frame = &stack->frame[stack->depth];
1427 memset(frame, 0, sizeof(*frame));
1428
1429 frame->type = CF_STACK_FILE;
1430 frame->fp = NULL;
1431 frame->parent = parent;
1432 frame->current = parent;
1433 frame->filename = talloc_strdup(frame->parent, value);
1434 return 1;
1435 }
1436
1437#ifdef HAVE_DIRENT_H
1438 /*
1439 * $INCLUDE foo/
1440 *
1441 * Include ALL non-"dot" files in the directory.
1442 * careful!
1443 */
1444 {
1445 char *directory;
1446 DIR *dir = NULL;
1447 struct dirent *dp;
1448 struct stat stat_buf;
1449 cf_file_heap_t *h;
1450#ifdef S_IWOTH
1451 int my_fd;
1452#endif
1453
1454 /*
1455 * We need to keep a copy of parent while the
1456 * included files mangle our buff[] array.
1457 */
1458 directory = talloc_strdup(parent, value);
1459
1460 if (DEBUG_ENABLED2) cf_log_debug(parent, "Including files in directory \"%s\"", directory);
1461
1462 dir = opendir(directory);
1463 if (!dir) {
1464 ERROR("%s[%d]: Error reading directory %s: %s",
1465 frame->filename, frame->lineno, value,
1466 fr_syserror(errno));
1467 error:
1468 if (dir) closedir(dir);
1469 talloc_free(directory);
1470 return -1;
1471 }
1472
1473#ifdef S_IWOTH
1474 my_fd = dirfd(dir);
1475 fr_assert(my_fd >= 0);
1476
1477 /*
1478 * Security checks.
1479 */
1480 if (fstat(my_fd, &stat_buf) < 0) {
1481 ERROR("%s[%d]: Failed reading directory %s: %s", frame->filename, frame->lineno,
1482 directory, fr_syserror(errno));
1483 goto error;
1484 }
1485
1486 if ((stat_buf.st_mode & S_IWOTH) != 0) {
1487 ERROR("%s[%d]: Directory %s is globally writable. Refusing to start due to "
1488 "insecure configuration", frame->filename, frame->lineno, directory);
1489 goto error;
1490 }
1491#endif
1492
1493 /*
1494 * Directory plus next filename.
1495 */
1496 if ((stack->depth + 2) >= MAX_STACK) {
1497 ERROR("%s[%d]: Directories too deep", frame->filename, frame->lineno);
1498 goto error;
1499 }
1500
1501 stack->depth++;
1502 frame = &stack->frame[stack->depth];
1503 *frame = (cf_stack_frame_t){
1504 .type = CF_STACK_DIR,
1505 .directory = directory,
1506 .parent = parent,
1507 .current = parent,
1508 .from_dir = true
1509 };
1510
1511 MEM(frame->heap = fr_heap_alloc(frame->directory, filename_cmp, cf_file_heap_t, heap_id, 0));
1512
1513 /*
1514 * Read the whole directory before loading any
1515 * individual file. We stat() files to ensure
1516 * that they're readable. We ignore
1517 * subdirectories and files with odd filenames.
1518 */
1519 while ((dp = readdir(dir)) != NULL) {
1520 char const *p, *ext = NULL;
1521
1522 if (dp->d_name[0] == '.') continue;
1523
1524 /*
1525 * Check for valid characters
1526 */
1527 for (p = dp->d_name; *p != '\0'; p++) {
1528 if (isalpha((uint8_t) *p) ||
1529 isdigit((uint8_t) *p) ||
1530 (*p == '-') ||
1531 (*p == '_')) continue;
1532
1533 if (*p == '.') {
1534 ext = p;
1535 continue;
1536 }
1537 break;
1538 }
1539 if (*p != '\0') continue;
1540
1541 /*
1542 * Ignore config files generated by deb / rpm packaging updates, and
1543 * documentation files.
1544 */
1545 if (ext) switch (fr_table_value_by_str(cf_file_extensions, ext + 1, CF_EXTENSION_READ)) {
1546 case CF_EXTENSION_READ:
1547 break;
1548
1549 case CF_EXTENSION_PKG:
1550 WARN("Ignoring packaging system produced file %s%s", frame->directory, dp->d_name);
1551 continue;
1552
1553 case CF_EXTENSION_TXT:
1554 continue;
1555 }
1556
1557 snprintf(stack->buff[1], stack->bufsize, "%s%s",
1558 frame->directory, dp->d_name);
1559
1560 if (stat(stack->buff[1], &stat_buf) != 0) {
1561 ERROR("%s[%d]: Failed checking file %s: %s",
1562 (frame - 1)->filename, (frame - 1)->lineno,
1563 stack->buff[1], fr_syserror(errno));
1564 continue;
1565 }
1566
1567 if (S_ISDIR(stat_buf.st_mode)) {
1568 WARN("%s[%d]: Ignoring directory %s",
1569 (frame - 1)->filename, (frame - 1)->lineno,
1570 stack->buff[1]);
1571 continue;
1572 }
1573
1574 MEM(h = talloc_zero(frame->heap, cf_file_heap_t));
1575 MEM(h->filename = talloc_strdup(h, stack->buff[1]));
1576 h->heap_id = FR_HEAP_INDEX_INVALID;
1577 (void) fr_heap_insert(&frame->heap, h);
1578 }
1579
1580 closedir(dir);
1581 return 1;
1582 }
1583#else
1584 ERROR("%s[%d]: Error including %s: No support for directories!",
1585 frame->filename, frame->lineno, value);
1586 return -1;
1587#endif
1588}
1589
1590
1592{
1593 CONF_ITEM *ci;
1594 CONF_SECTION *parent_cs, *templatecs;
1595 fr_token_t token;
1596 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1597 CONF_SECTION *parent = frame->current;
1598
1599 token = getword(&stack->ptr, stack->buff[2], stack->bufsize, true);
1600 if (token != T_EOL) {
1601 ERROR("%s[%d]: Unexpected text after $TEMPLATE", frame->filename, frame->lineno);
1602 return -1;
1603 }
1604
1605 if (!parent) {
1606 ERROR("%s[%d]: Internal sanity check error in template reference", frame->filename, frame->lineno);
1607 return -1;
1608 }
1609
1610 if (parent->template) {
1611 ERROR("%s[%d]: Section already has a template", frame->filename, frame->lineno);
1612 return -1;
1613 }
1614
1615 /*
1616 * Allow in-line templates.
1617 */
1618 templatecs = cf_section_find(cf_item_to_section(cf_parent(parent)), "template", stack->buff[2]);
1619 if (templatecs) {
1620 parent->template = templatecs;
1621 return 0;
1622 }
1623
1624 parent_cs = cf_root(parent);
1625
1626 templatecs = cf_section_find(parent_cs, "templates", NULL);
1627 if (!templatecs) {
1628 ERROR("%s[%d]: Cannot find template \"%s\", as no 'templates' section exists.",
1629 frame->filename, frame->lineno, stack->buff[2]);
1630 return -1;
1631 }
1632
1633 ci = cf_reference_item(parent_cs, templatecs, stack->buff[2]);
1634 if (!ci || (ci->type != CONF_ITEM_SECTION)) {
1635 PERROR("%s[%d]: Failed finding item \"%s\" in the 'templates' section.",
1636 frame->filename, frame->lineno, stack->buff[2]);
1637 return -1;
1638 }
1639
1640 parent->template = cf_item_to_section(ci);
1641 return 0;
1642}
1643
1644
1645static int cf_file_fill(cf_stack_t *stack);
1646
1647
1649 ['{'] = true,
1650};
1651
1653 [0] = true,
1654
1655 ['\r'] = true,
1656 ['\n'] = true,
1657
1658 ['#'] = true,
1659 [','] = true,
1660 [';'] = true,
1661 ['}'] = true,
1662};
1663
1665{
1666 ssize_t slen = 0;
1667 CONF_SECTION *cs;
1668 uint8_t const *p;
1669 char const *ptr = stack->ptr;
1670 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1671 CONF_SECTION *parent = frame->current;
1672 char *buff[4];
1673
1674 /*
1675 * Short names are nicer.
1676 */
1677 buff[1] = stack->buff[1];
1678 buff[2] = stack->buff[2];
1679 buff[3] = stack->buff[3];
1680
1681 /*
1682 * Create the CONF_SECTION. We don't pass a name2, as it
1683 * hasn't yet been parsed.
1684 */
1685 cs = cf_section_alloc(parent, parent, buff[1], NULL);
1686 if (!cs) {
1687 cf_log_err(parent, "Failed allocating memory for section");
1688 return NULL;
1689 }
1690 cf_filename_set(cs, frame->filename);
1691 cf_lineno_set(cs, frame->lineno);
1692
1693 /*
1694 * Keep "parsing" the condition until we hit EOL.
1695 *
1696 *
1697 */
1698 while (true) {
1699 int rcode;
1700 bool eol;
1701
1702 /*
1703 * Try to parse the condition. We can have a parse success, or a parse failure.
1704 */
1705 slen = fr_skip_condition(ptr, NULL, terminal_end_section, &eol);
1706
1707 /*
1708 * Parse success means we stop reading more data.
1709 */
1710 if (slen > 0) break;
1711
1712 /*
1713 * Parse failures not at EOL are real errors.
1714 */
1715 if (!eol) {
1716 slen = 0;
1717 fr_strerror_const("Unexpected EOF");
1718 error:
1719 cf_canonicalize_error(cs, slen, "Parse error in condition", ptr);
1720 talloc_free(cs);
1721 return NULL;
1722 }
1723
1724 /*
1725 * Parse failures at EOL means that we read more data.
1726 */
1727 p = (uint8_t const *) ptr + (-slen);
1728
1729 /*
1730 * Auto-continue across CR LF until we reach the
1731 * end of the string. We mash everything into one line.
1732 */
1733 if (*p && (*p < ' ')) {
1734 while ((*p == '\r') || (*p == '\n')) {
1735 char *q;
1736
1737 q = UNCONST(char *, p);
1738 *q = ' ';
1739 p++;
1740 continue;
1741 }
1742
1743 /*
1744 * Hopefully the next line is already in
1745 * the buffer, and we don't have to read
1746 * more data.
1747 */
1748 continue;
1749 }
1750
1751 /*
1752 * Anything other than EOL is a problem at this point.
1753 */
1754 if (*p) {
1755 fr_strerror_const("Unexpected text after condition");
1756 goto error;
1757 }
1758
1759 /*
1760 * We hit EOL, so the parse error is really "read more data".
1761 */
1762 stack->fill = UNCONST(char *, p);
1763 rcode = cf_file_fill(stack);
1764 if (rcode < 0) {
1765 cf_log_err(cs, "Failed parsing condition");
1766 return NULL;
1767 }
1768 }
1769
1770 fr_assert((size_t) slen < (stack->bufsize - 1));
1771
1772 ptr += slen;
1773 fr_skip_whitespace(ptr);
1774
1775 if (*ptr != '{') {
1776 cf_log_err(cs, "Expected '{' instead of %s", ptr);
1777 talloc_free(cs);
1778 return NULL;
1779 }
1780 ptr++;
1781
1782 /*
1783 * Save the parsed condition (minus trailing whitespace)
1784 * into a buffer.
1785 */
1786 memcpy(buff[2], stack->ptr, slen);
1787 buff[2][slen] = '\0';
1788
1789 while (slen > 0) {
1790 if (!isspace((uint8_t) buff[2][slen - 1])) break;
1791
1792 buff[2][slen - 1] = '\0';
1793 slen--;
1794 }
1795
1796 /*
1797 * Expand the variables in the pre-parsed condition.
1798 */
1799 if (!cf_expand_variables(frame->filename, frame->lineno, parent,
1800 buff[3], stack->bufsize, buff[2], slen, NULL, true)) {
1801 fr_strerror_const("Failed expanding configuration variable");
1802 return NULL;
1803 }
1804
1805 MEM(cs->name2 = talloc_strdup(cs, buff[3]));
1807
1808 stack->ptr = ptr;
1809
1810 cs->allow_locals = true;
1811 cs->unlang = CF_UNLANG_ALLOW;
1812 return cf_section_to_item(cs);
1813}
1814
1816{
1817 char const *mod;
1818 char const *value = NULL;
1819 CONF_SECTION *css;
1820 fr_token_t token;
1821 char const *ptr = stack->ptr;
1822 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1823 CONF_SECTION *parent = frame->current;
1824 char *buff[4];
1825
1826 /*
1827 * Short names are nicer.
1828 */
1829 buff[1] = stack->buff[1];
1830 buff[2] = stack->buff[2];
1831
1832 if (cf_get_token(parent, &ptr, &token, buff[1], stack->bufsize,
1833 frame->filename, frame->lineno) < 0) {
1834 return NULL;
1835 }
1836
1837 if (token != T_BARE_WORD) {
1838 ERROR("%s[%d]: Invalid syntax for 'map' - module name must not be a quoted string",
1839 frame->filename, frame->lineno);
1840 return NULL;
1841 }
1842 mod = buff[1];
1843
1844 /*
1845 * Maps without an expansion string are allowed, though
1846 * it's not clear why.
1847 */
1848 if (*ptr == '{') {
1849 ptr++;
1850 goto alloc_section;
1851 }
1852
1853 /*
1854 * Now get the expansion string.
1855 */
1856 if (cf_get_token(parent, &ptr, &token, buff[2], stack->bufsize,
1857 frame->filename, frame->lineno) < 0) {
1858 return NULL;
1859 }
1860 if (!fr_str_tok[token]) {
1861 ERROR("%s[%d]: Expecting string expansions in 'map' definition",
1862 frame->filename, frame->lineno);
1863 return NULL;
1864 }
1865
1866 if (*ptr != '{') {
1867 ERROR("%s[%d]: Expecting section start brace '{' in 'map' definition",
1868 frame->filename, frame->lineno);
1869 return NULL;
1870 }
1871 ptr++;
1872 value = buff[2];
1873
1874 /*
1875 * Allocate the section
1876 */
1877alloc_section:
1878 css = cf_section_alloc(parent, parent, "map", mod);
1879 if (!css) {
1880 ERROR("%s[%d]: Failed allocating memory for section",
1881 frame->filename, frame->lineno);
1882 return NULL;
1883 }
1884 cf_filename_set(css, frame->filename);
1885 cf_lineno_set(css, frame->lineno);
1886 css->name2_quote = T_BARE_WORD;
1887
1888 css->argc = 0;
1889 if (value) {
1890 css->argv = talloc_array(css, char const *, 1);
1891 css->argv[0] = talloc_strdup(css->argv, value);
1892 css->argv_quote = talloc_array(css, fr_token_t, 1);
1893 css->argv_quote[0] = token;
1894 css->argc++;
1895 }
1896 stack->ptr = ptr;
1898
1899 return cf_section_to_item(css);
1900}
1901
1902
1904{
1905 char const *mod = NULL;
1906 CONF_SECTION *css;
1907 fr_token_t token;
1908 char const *ptr = stack->ptr;
1909 cf_stack_frame_t *frame = &stack->frame[stack->depth];
1910 CONF_SECTION *parent = frame->current;
1911 char *buff[4];
1912 int values = 0;
1913
1914 /*
1915 * Short names are nicer.
1916 */
1917 buff[1] = stack->buff[1];
1918 buff[2] = stack->buff[2];
1919 buff[3] = stack->buff[3];
1920
1921 /*
1922 * subrequest { ... } is allowed.
1923 */
1924 fr_skip_whitespace(ptr);
1925 if (*ptr == '{') {
1926 ptr++;
1927 goto alloc_section;
1928 }
1929
1930 /*
1931 * Get the name of the Packet-Type.
1932 */
1933 if (cf_get_token(parent, &ptr, &token, buff[1], stack->bufsize,
1934 frame->filename, frame->lineno) < 0) {
1935 return NULL;
1936 }
1937
1938 mod = buff[1];
1939
1940 /*
1941 * subrequest Access-Request { ... } is allowed.
1942 */
1943 if (*ptr == '{') {
1944 ptr++;
1945 goto alloc_section;
1946 }
1947
1948 /*
1949 * subrequest Access-Request &foo { ... }
1950 */
1951 if (cf_get_token(parent, &ptr, &token, buff[2], stack->bufsize,
1952 frame->filename, frame->lineno) < 0) {
1953 return NULL;
1954 }
1955
1956 if (token != T_BARE_WORD) {
1957 ERROR("%s[%d]: The second argument to 'subrequest' must be an attribute reference",
1958 frame->filename, frame->lineno);
1959 return NULL;
1960 }
1961 values++;
1962
1963 if (*ptr == '{') {
1964 ptr++;
1965 goto alloc_section;
1966 }
1967
1968 /*
1969 * subrequest Access-Request &foo &bar { ... }
1970 */
1971 if (cf_get_token(parent, &ptr, &token, buff[3], stack->bufsize,
1972 frame->filename, frame->lineno) < 0) {
1973 return NULL;
1974 }
1975
1976 if (token != T_BARE_WORD) {
1977 ERROR("%s[%d]: The third argument to 'subrequest' must be an attribute reference",
1978 frame->filename, frame->lineno);
1979 return NULL;
1980 }
1981 values++;
1982
1983 if (*ptr != '{') {
1984 ERROR("%s[%d]: Expecting section start brace '{' in 'subrequest' definition",
1985 frame->filename, frame->lineno);
1986 return NULL;
1987 }
1988 ptr++;
1989
1990 /*
1991 * Allocate the section
1992 */
1993alloc_section:
1994 css = cf_section_alloc(parent, parent, "subrequest", mod);
1995 if (!css) {
1996 ERROR("%s[%d]: Failed allocating memory for section",
1997 frame->filename, frame->lineno);
1998 return NULL;
1999 }
2000 cf_filename_set(css, frame->filename);
2001 cf_lineno_set(css, frame->lineno);
2002 if (mod) css->name2_quote = T_BARE_WORD;
2003
2004 css->argc = values;
2005 if (values) {
2006 int i;
2007
2008 css->argv = talloc_array(css, char const *, values);
2009 css->argv_quote = talloc_array(css, fr_token_t, values);
2010
2011 for (i = 0; i < values; i++) {
2012 css->argv[i] = talloc_strdup(css->argv, buff[2 + i]);
2013 css->argv_quote[i] = T_BARE_WORD;
2014 }
2015 }
2016
2017 stack->ptr = ptr;
2018
2019 css->allow_locals = true;
2020 css->unlang = CF_UNLANG_ALLOW;
2021 return cf_section_to_item(css);
2022}
2023
2025{
2026 CONF_SECTION *css;
2027 int argc = 0;
2028 char const *ptr = stack->ptr;
2029 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2030 CONF_SECTION *parent = frame->current;
2031 char *name2 = NULL;
2032 char *argv[RLM_MODULE_NUMCODES];
2033
2034 while (true) {
2035 char const *p;
2036 size_t len;
2037
2038 fr_skip_whitespace(ptr);
2039
2040 /*
2041 * We have an open bracket, it's the end of the "catch" statement.
2042 */
2043 if (*ptr == '{') {
2044 ptr++;
2045 break;
2046 }
2047
2048 /*
2049 * The arguments have to be short, unquoted words.
2050 */
2051 p = ptr;
2052 while (isalpha((uint8_t) *ptr)) ptr++;
2053
2054 len = ptr - p;
2055 if (len > 16) {
2056 ERROR("%s[%d]: Invalid syntax for 'catch' - unknown rcode '%s'",
2057 frame->filename, frame->lineno, p);
2058 error:
2059 talloc_free(name2);
2060 return NULL;
2061 }
2062
2063 if ((*ptr != '{') && !isspace((uint8_t) *ptr)) {
2064 ERROR("%s[%d]: Invalid syntax for 'catch' - unexpected text at '%s'",
2065 frame->filename, frame->lineno, ptr);
2066 goto error;
2067 }
2068
2069 if (!name2) {
2070 name2 = talloc_strndup(NULL, p, len);
2071 continue;
2072 }
2073
2074 if (argc >= RLM_MODULE_NUMCODES) {
2075 ERROR("%s[%d]: Invalid syntax for 'catch' - too many arguments at'%s'",
2076 frame->filename, frame->lineno, ptr);
2077 goto error;
2078 }
2079
2080 argv[argc++] = talloc_strndup(name2, p, len);
2081 }
2082
2083 css = cf_section_alloc(parent, parent, "catch", name2);
2084 if (!css) {
2085 ERROR("%s[%d]: Failed allocating memory for section",
2086 frame->filename, frame->lineno);
2087 goto error;
2088 }
2089 cf_filename_set(css, frame->filename);
2090 cf_lineno_set(css, frame->lineno);
2091 css->name2_quote = T_BARE_WORD;
2092 css->unlang = CF_UNLANG_ALLOW;
2093
2094 css->argc = argc;
2095 if (argc) {
2096 int i;
2097
2098 css->argv = talloc_array(css, char const *, argc + 1);
2099 css->argv_quote = talloc_array(css, fr_token_t, argc);
2100 css->argc = argc;
2101
2102 for (i = 0; i < argc; i++) {
2103 css->argv[i] = talloc_strdup(css->argv, argv[i]);
2104 css->argv_quote[i] = T_BARE_WORD;
2105 }
2106
2107 css->argv[argc] = NULL;
2108 }
2109 talloc_free(name2);
2110
2111 stack->ptr = ptr;
2112
2113 return cf_section_to_item(css);
2114}
2115
2116static int parse_error(cf_stack_t *stack, char const *ptr, char const *message)
2117{
2118 char *spaces, *text;
2119 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2120
2121 if (!ptr) ptr = stack->ptr;
2122
2123 /*
2124 * We must pass a _negative_ offset to this function.
2125 */
2126 fr_canonicalize_error(NULL, &spaces, &text, stack->ptr - ptr, stack->ptr);
2127
2128 ERROR("%s[%d]: %s", frame->filename, frame->lineno, text);
2129 ERROR("%s[%d]: %s^ - %s", frame->filename, frame->lineno, spaces, message);
2130
2132 talloc_free(text);
2133 return -1;
2134}
2135
2136static int parse_type_name(cf_stack_t *stack, char const **ptr_p, char const *type_ptr, fr_type_t *type_p)
2137{
2139 fr_token_t token;
2140 char const *ptr = *ptr_p;
2141 char const *ptr2;
2142
2143 /*
2144 * Parse an explicit type.
2145 */
2147 switch (type) {
2148 default:
2149 break;
2150
2151 case FR_TYPE_NULL:
2152 case FR_TYPE_VOID:
2153 case FR_TYPE_VALUE_BOX:
2154 case FR_TYPE_MAX:
2155 (void) parse_error(stack, type_ptr, "Unknown or invalid variable type in 'foreach'");
2156 return -1;
2157 }
2158
2159 fr_skip_whitespace(ptr);
2160 ptr2 = ptr;
2161
2162 /*
2163 * Parse the variable name. @todo - allow '-' in names.
2164 */
2165 token = gettoken(&ptr, stack->buff[2], stack->bufsize, false);
2166 if (token != T_BARE_WORD) {
2167 (void) parse_error(stack, ptr2, "Invalid variable name for key in 'foreach'");
2168 return -1;
2169 }
2170 fr_skip_whitespace(ptr);
2171
2172 *ptr_p = ptr;
2173 *type_p = type;
2174
2175 return 0;
2176}
2177
2178/*
2179 * foreach &User-Name { - old and deprecated
2180 *
2181 * foreach value (...) { - automatically define variable
2182 *
2183 * foreach string value ( ...) { - data type for variable
2184 *
2185 * foreach string key, type value (..) { - key is "string", value is as above
2186 */
2188{
2189 fr_token_t token;
2191 CONF_SECTION *css;
2192 char const *ptr = stack->ptr, *ptr2, *type_ptr;
2193 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2194 CONF_SECTION *parent = frame->current;
2195
2196 css = cf_section_alloc(parent, parent, "foreach", NULL);
2197 if (!css) {
2198 ERROR("%s[%d]: Failed allocating memory for section",
2199 frame->filename, frame->lineno);
2200 return NULL;
2201 }
2202
2203 cf_filename_set(css, frame->filename);
2204 cf_lineno_set(css, frame->lineno);
2205 css->name2_quote = T_BARE_WORD;
2206 css->unlang = CF_UNLANG_ALLOW;
2207 css->allow_locals = true;
2208
2209 /*
2210 * Get the first argument to "foreach". For backwards
2211 * compatibility, it could be an attribute reference.
2212 */
2213 type_ptr = ptr;
2214 if (cf_get_token(parent, &ptr, &token, stack->buff[1], stack->bufsize,
2215 frame->filename, frame->lineno) < 0) {
2216 return NULL;
2217 }
2218
2219 if (token != T_BARE_WORD) {
2220 invalid_argument:
2221 (void) parse_error(stack, type_ptr, "Unexpected argument to 'foreach'");
2222 return NULL;
2223 }
2224
2225 fr_skip_whitespace(ptr);
2226
2227 /*
2228 * foreach foo { ...
2229 *
2230 * Deprecated and don't use.
2231 */
2232 if (*ptr == '{') {
2233 css->name2 = talloc_strdup(css, stack->buff[1]);
2234
2235 ptr++;
2236 stack->ptr = ptr;
2237
2238 cf_log_warn(css, "Using deprecated syntax. Please use the new 'foreach' syntax.");
2239 return cf_section_to_item(css);
2240 }
2241
2242 fr_skip_whitespace(ptr);
2243
2244 /*
2245 * foreach value (...) {
2246 */
2247 if (*ptr == '(') {
2249 strcpy(stack->buff[2], stack->buff[1]); /* so that we can parse expression in buff[1] */
2250 goto alloc_argc_2;
2251 }
2252
2253 /*
2254 * on input, type name is in stack->buff[1]
2255 * on output, variable name is in stack->buff[2]
2256 */
2257 if (parse_type_name(stack, &ptr, type_ptr, &type) < 0) return NULL;
2258
2259 /*
2260 * if we now have an expression block, then just have variable type / name.
2261 */
2262 if (*ptr == '(') goto alloc_argc_2;
2263
2264 /*
2265 * There's a comma. the first "type name" is for the key. We skip the comma, and parse the
2266 * second "type name" as being for the value.
2267 *
2268 * foreach type key, type value (...)
2269 */
2270 if (*ptr == ',') {
2271 /*
2272 * We have 4 arguments, [var-type, var-name, key-type, key-name]
2273 *
2274 * We don't really care about key-type, but we might care later.
2275 */
2276 css->argc = 4;
2277 css->argv = talloc_array(css, char const *, css->argc);
2278 css->argv_quote = talloc_array(css, fr_token_t, css->argc);
2279
2280 css->argv[2] = fr_type_to_str(type);
2281 css->argv_quote[2] = T_BARE_WORD;
2282
2283 css->argv[3] = talloc_strdup(css->argv, stack->buff[2]);
2284 css->argv_quote[3] = T_BARE_WORD;
2285
2286 ptr++;
2287 fr_skip_whitespace(ptr);
2288 type_ptr = ptr;
2289
2290 /*
2291 * Now parse "type value"
2292 */
2293 token = gettoken(&ptr, stack->buff[1], stack->bufsize, false);
2294 if (token != T_BARE_WORD) goto invalid_argument;
2295
2296 if (parse_type_name(stack, &ptr, type_ptr, &type) < 0) return NULL;
2297
2298 if (!fr_type_is_leaf(type)) {
2299 (void) parse_error(stack, type_ptr, "Invalid data type for 'key' variable");
2300 return NULL;
2301 }
2302 }
2303
2304 /*
2305 * The thing to loop over must now be in an expression block.
2306 */
2307 if (*ptr != '(') {
2308 (void) parse_error(stack, ptr, "Expected (...) after 'foreach' variable definition");
2309 return NULL;
2310 }
2311
2312 goto parse_expression;
2313
2314alloc_argc_2:
2315 css->argc = 2;
2316 css->argv = talloc_array(css, char const *, css->argc);
2317 css->argv_quote = talloc_array(css, fr_token_t, css->argc);
2318
2319
2320parse_expression:
2321 /*
2322 * "(" whitespace EXPRESSION whitespace ")"
2323 */
2324 ptr++;
2325 fr_skip_whitespace(ptr);
2326 ptr2 = ptr;
2327
2328 if (cf_get_token(parent, &ptr, &token, stack->buff[1], stack->bufsize,
2329 frame->filename, frame->lineno) < 0) {
2330 return NULL;
2331 }
2332
2333 /*
2334 * We can do &foo[*] or %func(...), but not "...".
2335 */
2336 if (token != T_BARE_WORD) {
2337 (void) parse_error(stack, ptr2, "Invalid reference in 'foreach'");
2338 return NULL;
2339 }
2340
2341 fr_skip_whitespace(ptr);
2342 if (*ptr != ')') {
2343 (void) parse_error(stack, ptr, "Missing ')' in 'foreach'");
2344 return NULL;
2345 }
2346 ptr++;
2347 fr_skip_whitespace(ptr);
2348
2349 if (*ptr != '{') {
2350 (void) parse_error(stack, ptr, "Expected '{' in 'foreach'");
2351 return NULL;
2352 }
2353
2354 css->name2 = talloc_strdup(css, stack->buff[1]);
2355
2356 /*
2357 * Add in the extra arguments
2358 */
2359 css->argv[0] = fr_type_to_str(type);
2360 css->argv_quote[0] = T_BARE_WORD;
2361
2362 css->argv[1] = talloc_strdup(css->argv, stack->buff[2]);
2363 css->argv_quote[1] = T_BARE_WORD;
2364
2365 ptr++;
2366 stack->ptr = ptr;
2367
2368 return cf_section_to_item(css);
2369}
2370
2371
2372static int add_pair(CONF_SECTION *parent, char const *attr, char const *value,
2373 fr_token_t name1_token, fr_token_t op_token, fr_token_t value_token,
2374 char *buff, char const *filename, int lineno)
2375{
2376 CONF_DATA const *cd;
2377 conf_parser_t *rule;
2378 CONF_PAIR *cp;
2379 bool pass2 = false;
2380
2381 /*
2382 * If we have the value, expand any configuration
2383 * variables in it.
2384 */
2385 if (value && *value) {
2386 bool soft_fail;
2387 char const *expanded;
2388
2389 expanded = cf_expand_variables(filename, lineno, parent, buff, talloc_array_length(buff), value, -1, &soft_fail,
2390 (value_token != T_BARE_WORD));
2391 if (expanded) {
2392 value = expanded;
2393
2394 } else if (!soft_fail) {
2395 return -1;
2396
2397 } else {
2398 /*
2399 * References an item which doesn't exist,
2400 * or which is already marked up as being
2401 * expanded in pass2. Wait for pass2 to
2402 * do the expansions.
2403 *
2404 * Leave the input value alone.
2405 */
2406 pass2 = true;
2407 }
2408 }
2409
2410 cp = cf_pair_alloc(parent, attr, value, op_token, name1_token, value_token);
2411 if (!cp) return -1;
2412 cf_filename_set(cp, filename);
2413 cf_lineno_set(cp, lineno);
2414 cp->pass2 = pass2;
2415
2417 if (!cd) return 0;
2418
2419 rule = cf_data_value(cd);
2420 if (!rule->on_read) return 0;
2421
2422 /*
2423 * Do the on_read callback after adding the value.
2424 */
2425 return rule->on_read(parent, NULL, NULL, cf_pair_to_item(cp), rule);
2426}
2427
2428/*
2429 * switch (cast) foo {
2430 */
2432{
2433 size_t match_len;
2435 fr_token_t name2_quote = T_BARE_WORD;
2436 CONF_SECTION *css;
2437 char const *ptr = stack->ptr;
2438 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2439 CONF_SECTION *parent = frame->current;
2440
2441 fr_skip_whitespace(ptr);
2442 if (*ptr == '(') {
2443 char const *start;
2444
2445 ptr++;
2446 start = ptr;
2447
2448 while (isalpha(*ptr)) ptr++;
2449
2450 if (*ptr != ')') {
2451 ERROR("%s[%d]: Missing ')' in cast",
2452 frame->filename, frame->lineno);
2453 return NULL;
2454 }
2455
2457 start, ptr - start, FR_TYPE_MAX);
2458 if (type == FR_TYPE_MAX) {
2459 ERROR("%s[%d]: Unknown data type '%.*s' in cast",
2460 frame->filename, frame->lineno, (int) (ptr - start), start);
2461 return NULL;
2462 }
2463
2464 if (!fr_type_is_leaf(type)) {
2465 ERROR("%s[%d]: Invalid data type '%.*s' in cast",
2466 frame->filename, frame->lineno, (int) (ptr - start), start);
2467 return NULL;
2468 }
2469
2470 ptr++;
2471 fr_skip_whitespace(ptr);
2472 }
2473
2474 /*
2475 * Get the argument to the switch statement
2476 */
2477 if (cf_get_token(parent, &ptr, &name2_quote, stack->buff[1], stack->bufsize,
2478 frame->filename, frame->lineno) < 0) {
2479 return NULL;
2480 }
2481
2482 css = cf_section_alloc(parent, parent, "switch", NULL);
2483 if (!css) {
2484 ERROR("%s[%d]: Failed allocating memory for section",
2485 frame->filename, frame->lineno);
2486 return NULL;
2487 }
2488
2489 cf_filename_set(css, frame->filename);
2490 cf_lineno_set(css, frame->lineno);
2491 css->name2_quote = name2_quote;
2492 css->unlang = CF_UNLANG_ALLOW;
2493 css->allow_locals = true;
2494
2495 fr_skip_whitespace(ptr);
2496
2497 if (*ptr != '{') {
2498 (void) parse_error(stack, ptr, "Expected '{' in 'switch'");
2499 return NULL;
2500 }
2501
2502 css->name2 = talloc_strdup(css, stack->buff[1]);
2503
2504 /*
2505 * Add in the extra argument.
2506 */
2507 if (type != FR_TYPE_NULL) {
2508 css->argc = 1;
2509 css->argv = talloc_array(css, char const *, css->argc);
2510 css->argv_quote = talloc_array(css, fr_token_t, css->argc);
2511
2512 css->argv[0] = fr_type_to_str(type);
2513 css->argv_quote[0] = T_BARE_WORD;
2514 }
2515
2516 ptr++;
2517 stack->ptr = ptr;
2518
2519 return cf_section_to_item(css);
2520}
2521
2522
2524 { L("catch"), (void *) process_catch },
2525 { L("elsif"), (void *) process_if },
2526 { L("foreach"), (void *) process_foreach },
2527 { L("if"), (void *) process_if },
2528 { L("map"), (void *) process_map },
2529 { L("subrequest"), (void *) process_subrequest },
2530 { L("switch"), (void *) process_switch }
2531};
2533
2534typedef CONF_ITEM *(*cf_process_func_t)(cf_stack_t *);
2535
2536/*
2537 * This is fine. Don't complain.
2538 */
2539#ifdef __clang__
2540#pragma clang diagnostic ignored "-Wgnu-designator"
2541#endif
2542
2543/** Convert tokens back to a quoting character
2544 *
2545 * Non-string types convert to '?' to screw ups can be identified easily
2546 */
2547static const bool cf_name_char1[SBUFF_CHAR_CLASS] = {
2548 [ '0' ... '9' ] = true,
2549 [ 'A' ... 'Z' ] = true,
2550 [ 'a' ... 'z' ] = true,
2551 [ '%' ] = true, // %function() in unlang
2552 [ '@' ] = true, // @policy
2553 [ '-' ] = true, // -sql, but probably only in 'unlang'
2554 [ '&' ] = true, // legacy stuff.
2555};
2556
2557
2559{
2560 fr_token_t name1_token, name2_token, value_token, op_token;
2561 char const *value;
2562 CONF_SECTION *css;
2563 char const *ptr = stack->ptr;
2564 char const *name1_ptr, *name2_ptr, *op_ptr;
2565 cf_stack_frame_t *frame = &stack->frame[stack->depth];
2566 CONF_SECTION *parent = frame->current;
2567 char *buff[4];
2568 cf_process_func_t process;
2569
2570 /*
2571 * Short names are nicer.
2572 */
2573 buff[0] = stack->buff[0];
2574 buff[1] = stack->buff[1];
2575 buff[2] = stack->buff[2];
2576 buff[3] = stack->buff[3];
2577
2578 fr_assert(parent != NULL);
2579
2580 /*
2581 * Catch end of a subsection.
2582 *
2583 * frame->current is the new thing we just created.
2584 * frame->parent is the parent of the current frame
2585 * frame->at_reference is the original frame->current, before the @reference
2586 * parent is the parent we started with when we started this section.
2587 */
2588 if (*ptr == '}') {
2589 /*
2590 * No pushed braces means that we're already in
2591 * the parent section which loaded this file. We
2592 * cannot go back up another level.
2593 *
2594 * This limitation means that we cannot put half
2595 * of a CONF_SECTION in one file, and then the
2596 * second half in another file. That's fine.
2597 */
2598 if (frame->braces == 0) {
2599 return parse_error(stack, ptr, "Too many closing braces");
2600 }
2601
2602 /*
2603 * Reset the current and parent to the original
2604 * section, before we were parsing the
2605 * @reference.
2606 */
2607 if (frame->at_reference && (frame->braces == frame->at_reference_braces + 1)) {
2608 frame->current = frame->parent = frame->at_reference;
2609 frame->at_reference = NULL;
2610
2611 } else {
2612 /*
2613 * Go back up one section, because we can.
2614 */
2615 frame->current = frame->parent = cf_item_to_section(frame->current->item.parent);
2616 }
2617
2618 fr_assert(frame->braces > 0);
2619 frame->braces--;
2620
2621 /*
2622 * Merge the template into the existing
2623 * section. parent uses more memory, but
2624 * means that templates now work with
2625 * sub-sections, etc.
2626 */
2627 if (!cf_template_merge(parent, parent->template)) return -1;
2628
2629 ptr++;
2630 stack->ptr = ptr;
2631 return 1;
2632 }
2633
2634 /*
2635 * Found nothing to get excited over. It MUST be
2636 * a key word.
2637 */
2638 name1_ptr = ptr;
2639 switch (parent->unlang) {
2640 default:
2641 /*
2642 * The LHS is a bare word / keyword in normal configuration file syntax.
2643 */
2644 name1_token = gettoken(&ptr, buff[1], stack->bufsize, false);
2645 if (name1_token == T_EOL) return 0;
2646
2647 if (name1_token == T_INVALID) {
2648 return parse_error(stack, name1_ptr, fr_strerror());
2649 }
2650
2651 if (name1_token != T_BARE_WORD) {
2652 return parse_error(stack, name1_ptr, "Invalid location for quoted string");
2653 }
2654
2655 if (*buff[1] == '%') {
2656 return parse_error(stack, name1_ptr, "Cannot use functions outside of a processing section");
2657 }
2658
2659 if (*buff[1] == '&') {
2660 return parse_error(stack, name1_ptr, "Cannot reference attributes outside of a processing section");
2661 }
2662
2663 fr_skip_whitespace(ptr);
2664 break;
2665
2666 case CF_UNLANG_ALLOW:
2667 case CF_UNLANG_EDIT:
2669 /*
2670 * The LHS can be an xlat expansion, attribute reference, etc.
2671 */
2672 if (cf_get_token(parent, &ptr, &name1_token, buff[1], stack->bufsize,
2673 frame->filename, frame->lineno) < 0) {
2674 return -1;
2675 }
2676
2677 /*
2678 * Complain about old syntax.
2679 */
2680 if (check_config && (*buff[1] == '&')) {
2681 WARN("%s[%d]: Using '&' is no longer necessary when referencing attributes. Please delete it.", frame->filename, frame->lineno);
2682 }
2683 break;
2684 }
2685
2686 /*
2687 * Check for bad names. A section named ".foo" will absolutely break the path hierarchy.
2688 */
2689 if (name1_token == T_BARE_WORD) {
2690 if (!cf_name_char1[(uint8_t) *buff[1]]) {
2691 return parse_error(stack, name1_ptr, "Invalid name");
2692 }
2693 }
2694
2695 /*
2696 * Check if the thing we just parsed is an unlang keyword.
2697 */
2698 if ((name1_token == T_BARE_WORD) && isalpha((uint8_t) *buff[1])) {
2700 if (process) {
2701 CONF_ITEM *ci;
2702
2703 /*
2704 * Disallow keywords outside of unlang sections.
2705 *
2706 * We don't strictly need to do this with the more state-oriented parser, but
2707 * people keep putting unlang into random places in the configuration files,
2708 * which is wrong.
2709 */
2710 if (parent->unlang != CF_UNLANG_ALLOW) {
2711 return parse_error(stack, name1_ptr, "Invalid location for unlang keyword");
2712 }
2713
2714 stack->ptr = ptr;
2715 ci = process(stack);
2716 if (!ci) return -1;
2717
2718 ptr = stack->ptr;
2719 if (cf_item_is_section(ci)) {
2720 parent->allow_locals = false;
2721 css = cf_item_to_section(ci);
2722 goto add_section;
2723 }
2724
2725 /*
2726 * Else the item is a pair, and the call to process() it already added it to the
2727 * current section.
2728 */
2729 goto added_pair;
2730 }
2731
2732 /*
2733 * The next token isn't text, so we ignore it.
2734 */
2735 if (!isalnum((int) *ptr)) goto check_for_eol;
2736 }
2737
2738 /*
2739 * See if this thing is a variable definition.
2740 */
2741 if ((name1_token == T_BARE_WORD) && parent->allow_locals) {
2743 char const *ptr3;
2744
2746 if (type == FR_TYPE_NULL) {
2747 parent->allow_locals = false;
2748 goto check_for_eol;
2749 }
2750
2751 if (type == FR_TYPE_TLV) goto parse_name2;
2752
2753 /*
2754 * group {
2755 *
2756 * is a section.
2757 */
2758 if (type == FR_TYPE_GROUP) {
2759 fr_skip_whitespace(ptr);
2760 if (*ptr == '{') {
2761 ptr++;
2762 value = NULL;
2763 name2_token = T_BARE_WORD;
2764 goto alloc_section;
2765 }
2766
2767 } else if (!fr_type_is_leaf(type)) {
2768 /*
2769 * Other structural types are allowed.
2770 */
2771 return parse_error(stack, name1_ptr, "Invalid data type for local variable. Must be 'tlv' or else a non-structural type");
2772 }
2773
2774 /*
2775 * We don't have an operator, so set it to a magic value.
2776 */
2778
2779 /*
2780 * Parse the name of the local variable, and use it as the "value" for the CONF_PAIR.
2781 */
2782 ptr3 = ptr;
2783 if (cf_get_token(parent, &ptr, &value_token, buff[2], stack->bufsize,
2784 frame->filename, frame->lineno) < 0) {
2785 return -1;
2786 }
2787
2788 if (value_token != T_BARE_WORD) {
2789 return parse_error(stack, ptr3, "Invalid name");
2790 }
2791
2792 value = buff[2];
2793
2794 /*
2795 * Non-structural things must be variable definitions.
2796 */
2797 if (fr_type_is_leaf(type)) goto alloc_pair;
2798
2799 /*
2800 * Parse: group foo
2801 * vs group foo { ...
2802 */
2803 fr_skip_whitespace(ptr);
2804
2805 if (*ptr != '{') goto alloc_pair;
2806
2807 ptr++;
2808 name2_token = T_BARE_WORD;
2809 goto alloc_section;
2810 }
2811
2812 /*
2813 * We've parsed the LHS thing. The RHS might be empty, or an operator, or another word, or an
2814 * open bracket.
2815 */
2816check_for_eol:
2817 if (!*ptr || (*ptr == '#') || (*ptr == ',') || (*ptr == ';') || (*ptr == '}')) {
2818 /*
2819 * Only unlang sections can have module references.
2820 *
2821 * We also allow bare words in edit lists, where the RHS is a list of values.
2822 *
2823 * @todo - detail "suppress" requires bare words :(
2824 */
2825 parent->allow_locals = false;
2826 value_token = T_INVALID;
2827 op_token = T_OP_EQ;
2828 value = NULL;
2829 goto alloc_pair;
2830 }
2831
2832 /*
2833 * A common pattern is: name { ...}
2834 * Check for it and skip ahead.
2835 */
2836 if (*ptr == '{') {
2837 ptr++;
2838 name2_token = T_INVALID;
2839 value = NULL;
2840 goto alloc_section;
2841 }
2842
2843 /*
2844 * Parse the thing after the first word. It can be an operator, or the second name for a section.
2845 */
2846 name2_ptr = ptr;
2847 switch (parent->unlang) {
2848 default:
2849 /*
2850 * Configuration sections can only have '=' after the
2851 * first word, OR a second word which is the second name
2852 * of a configuration section.
2853 */
2854 if (*ptr == '=') goto operator;
2855
2856 /*
2857 * Section name2 can only be alphanumeric or UTF-8.
2858 */
2859 parse_name2:
2860 name2_ptr = ptr;
2861
2862 if (!(isalpha((uint8_t) *ptr) || isdigit((uint8_t) *ptr) || (*(uint8_t const *) ptr >= 0x80))) {
2863 /*
2864 * Maybe they missed a closing brace somewhere?
2865 */
2866 name2_token = gettoken(&ptr, buff[2], stack->bufsize, false); /* can't be EOL */
2867 if (fr_assignment_op[name2_token]) {
2868 return parse_error(stack, name2_ptr, "Unexpected operator, was expecting a configuration section. Is there a missing '}' somewhere?");
2869 }
2870
2871 return parse_error(stack, name2_ptr, "Invalid second name for configuration section");
2872 }
2873
2874 name2_token = gettoken(&ptr, buff[2], stack->bufsize, false); /* can't be EOL */
2875 if (name2_token == T_INVALID) {
2876 return parse_error(stack, name2_ptr, fr_strerror());
2877 }
2878
2879 if (name2_token != T_BARE_WORD) {
2880 return parse_error(stack, name2_ptr, "Unexpected quoted string after section name");
2881 }
2882
2883 fr_skip_whitespace(ptr);
2884
2885 /*
2886 * load-balance and redundant-load-balance MUST have a static module name, and MAY have
2887 * an additional load-balance keyword.
2888 */
2889 if ((parent->unlang == CF_UNLANG_MODULES) && (*ptr != '{') &&
2890 ((strcmp(buff[1], "load-balance") == 0) ||
2891 (strcmp(buff[1], "redundant-load-balance") == 0))) {
2892 /*
2893 * The third name could be an attribute name, xlat expansion, etc.
2894 */
2895 if (cf_get_token(parent, &ptr, &value_token, buff[3], stack->bufsize,
2896 frame->filename, frame->lineno) < 0) {
2897 return -1;
2898 }
2899
2900 fr_skip_whitespace(ptr);
2901
2902 if (*ptr != '{') {
2903 return parse_error(stack, ptr, "Expected '{'");
2904 }
2905
2906 ptr++;
2907
2908 css = cf_section_alloc(parent, parent, buff[1], buff[2]);
2909 if (!css) goto oom;
2910
2911 css->argc = 1;
2912 css->argv = talloc_array(css, char const *, 1);
2913 css->argv[0] = talloc_strdup(css->argv, buff[3]);
2914 css->argv_quote = talloc_array(css, fr_token_t, 1);
2915 css->argv_quote[0] = value_token;
2916 goto setup_section;
2917 }
2918
2919 if (*ptr != '{') {
2920 return parse_error(stack, ptr, "Missing '{' for configuration section");
2921 }
2922
2923 ptr++;
2924 value = buff[2];
2925 goto alloc_section;
2926
2928 /*
2929 * The next thing MUST be an operator. We don't support nested attributes in "update" or
2930 * "map" sections.
2931 */
2932 goto operator;
2933
2934 case CF_UNLANG_EDIT:
2935 /*
2936 * The next thing MUST be an operator. Edit sections always do operations, even on
2937 * lists. i.e. there is no second name section when editing a list.
2938 */
2939 goto operator;
2940
2941 case CF_UNLANG_ALLOW:
2942 /*
2943 * 'case ::foo' is allowed. For generality, we just expect that the second argument to
2944 * 'case' is not an operator.
2945 *
2946 * These keywords must have a second name. Or at least, if the character after the
2947 * keyword isn't a '{', then they must have a second name, which is the argument to the
2948 * keyword.
2949 */
2950 if ((strcmp(buff[1], "case") == 0) ||
2951 (strcmp(buff[1], "limit") == 0) ||
2952 (strcmp(buff[1], "load-balance") == 0) ||
2953 (strcmp(buff[1], "redundant-load-balance") == 0) ||
2954 (strcmp(buff[1], "timeout") == 0)) {
2955 break;
2956 }
2957
2958 /*
2959 * These keywords are NOT allowed to have an argument.
2960 */
2961 if ((strcmp(buff[1], "default") == 0) ||
2962 (strcmp(buff[1], "group") == 0) ||
2963 (strcmp(buff[1], "transaction") == 0) ||
2964 (strcmp(buff[1], "try") == 0) ||
2965 (strcmp(buff[1], "redundant") == 0)) {
2966 return parse_error(stack, ptr, "Expected '{' after keyword");
2967 }
2968
2969 /*
2970 * It's not a string, bare word, or attribute reference. It must be an operator.
2971 */
2972 if (!((*ptr == '"') || (*ptr == '`') || (*ptr == '\'') || ((*ptr == '&') && (ptr[1] != '=')) ||
2973 ((*((uint8_t const *) ptr) & 0x80) != 0) || isalpha((uint8_t) *ptr) || isdigit((uint8_t) *ptr))) {
2974 goto operator;
2975 }
2976 break;
2977 }
2978
2979 /*
2980 * The second name could be a bare word, xlat expansion, string etc.
2981 */
2982 if (cf_get_token(parent, &ptr, &name2_token, buff[2], stack->bufsize,
2983 frame->filename, frame->lineno) < 0) {
2984 return -1;
2985 }
2986
2987 if (*ptr != '{') {
2989
2990 /*
2991 * Not the name of a data type.
2992 */
2993 if (type == FR_TYPE_NULL) {
2994 return parse_error(stack, ptr, "Expected '{' after section names");
2995 }
2996
2997 if (parent->unlang == CF_UNLANG_EDIT) {
2998 return parse_error(stack, name1_ptr, "Invalid location for local variable - definitions cannot be inside of an assignment");
2999 }
3000
3001 if (parent->unlang == CF_UNLANG_ALLOW) {
3002 return parse_error(stack, name1_ptr, "Invalid location for local variable - definitions must go at the start of a section");
3003 }
3004
3005 return parse_error(stack, name1_ptr, "Invalid location for local variable - definitions can only go in a processing section.");
3006 }
3007 ptr++;
3008 value = buff[2];
3009
3010alloc_section:
3011 parent->allow_locals = false;
3012
3013 /*
3014 * @policy foo { ...}
3015 *
3016 * Means "add foo to the policy section". And if
3017 * policy{} doesn't exist, create it, and then mark up
3018 * policy{} with a flag "we need to merge it", so that
3019 * when we read the actual policy{}, we merge the
3020 * contents together, instead of creating a second
3021 * policy{}.
3022 *
3023 * @todo - allow for '.' in @.ref Or at least test it. :(
3024 *
3025 * @todo - allow for two section names @ref foo bar {...}
3026 *
3027 * @todo - maybe we can use this to overload things in
3028 * virtual servers, and in modules?
3029 */
3030 if (buff[1][0] == '@') {
3031 CONF_ITEM *ci;
3032 CONF_SECTION *root;
3033 char const *name = &buff[1][1];
3034
3035 if (!value) {
3036 ERROR("%s[%d]: Missing section name for reference", frame->filename, frame->lineno);
3037 return -1;
3038 }
3039
3040 root = cf_root(parent);
3041
3042 ci = cf_reference_item(root, parent, name);
3043 if (!ci) {
3044 if (name[1] == '.') {
3045 PERROR("%s[%d]: Failed finding reference \"%s\"", frame->filename, frame->lineno, name);
3046 return -1;
3047 }
3048
3049 css = cf_section_alloc(root, root, name, NULL);
3050 if (!css) goto oom;
3051
3052 cf_filename_set(css, frame->filename);
3053 cf_lineno_set(css, frame->lineno);
3054 css->name2_quote = name2_token;
3055 css->unlang = CF_UNLANG_NONE;
3056 css->allow_locals = false;
3057 css->at_reference = true;
3058 parent = css;
3059
3060 /*
3061 * Copy this code from below. :(
3062 */
3063 if (cf_item_to_section(parent->item.parent) == root) {
3064 if (strcmp(css->name1, "server") == 0) css->unlang = CF_UNLANG_SERVER;
3065 if (strcmp(css->name1, "policy") == 0) css->unlang = CF_UNLANG_POLICY;
3066 if (strcmp(css->name1, "modules") == 0) css->unlang = CF_UNLANG_MODULES;
3067 if (strcmp(css->name1, "templates") == 0) css->unlang = CF_UNLANG_CAN_HAVE_UPDATE;
3068 }
3069
3070 } else {
3071 if (!cf_item_is_section(ci)) {
3072 ERROR("%s[%d]: Reference \"%s\" is not a section", frame->filename, frame->lineno, name);
3073 return -1;
3074 }
3075
3076 /*
3077 * Set the new parent and ensure we're
3078 * not creating a duplicate section.
3079 */
3081 css = cf_section_find(parent, value, NULL);
3082 if (css) {
3083 ERROR("%s[%d]: Reference \"%s\" already contains a \"%s\" section at %s[%d]",
3084 frame->filename, frame->lineno, name, value,
3085 css->item.filename, css->item.lineno);
3086 return -1;
3087 }
3088 }
3089
3090 /*
3091 * We're processing a section. The @reference is
3092 * OUTSIDE of this section.
3093 *
3094 * '@reference' is only valid at the top level of a
3095 * section: once any enclosing section has been opened,
3096 * frame->current diverges from frame->parent, and the
3097 * reference has no well-defined target.
3098 */
3099 if (frame->current != frame->parent) {
3100 ERROR("%s[%d]: '@%s' references can only appear at the top level of a section",
3101 frame->filename, frame->lineno, name);
3102 return -1;
3103 }
3104 frame->at_reference = frame->parent;
3105 frame->at_reference_braces = frame->braces;
3106 name2_token = T_BARE_WORD;
3107
3108 css = cf_section_alloc(parent, parent, value, NULL);
3109 } else {
3110 /*
3111 * Check if there's already an auto-created
3112 * section of this name. If so, just use that
3113 * section instead of allocating a new one.
3114 */
3115 css = cf_section_find(parent, buff[1], value);
3116 if (css && css->at_reference) {
3117 css->at_reference = false;
3118 } else {
3120 }
3121 }
3122
3123 if (!css) {
3124 oom:
3125 ERROR("%s[%d]: Failed allocating memory for section",
3126 frame->filename, frame->lineno);
3127 return -1;
3128 }
3129
3130setup_section:
3131 if (css->depth >= CONF_SECTION_MAX_DEPTH) {
3132 ERROR("%s[%d]: Failed creating section - too many nested sections (%d)",
3133 frame->filename, frame->lineno, css->depth);
3134 return -1;
3135 }
3136
3137 cf_filename_set(css, frame->filename);
3138 cf_lineno_set(css, frame->lineno);
3139 css->name2_quote = name2_token;
3140 css->unlang = CF_UNLANG_NONE;
3141 css->allow_locals = false;
3142
3143 /*
3144 * Only a few top-level sections allow "unlang"
3145 * statements. And for those, "unlang"
3146 * statements are only allowed in child
3147 * subsection.
3148 */
3149 switch (parent->unlang) {
3150 case CF_UNLANG_NONE:
3151 if (!parent->item.parent) {
3152 if (strcmp(css->name1, "server") == 0) css->unlang = CF_UNLANG_SERVER;
3153 if (strcmp(css->name1, "policy") == 0) css->unlang = CF_UNLANG_POLICY;
3154 if (strcmp(css->name1, "modules") == 0) css->unlang = CF_UNLANG_MODULES;
3155 if (strcmp(css->name1, "templates") == 0) css->unlang = CF_UNLANG_CAN_HAVE_UPDATE;
3156
3157 } else if ((cf_item_to_section(parent->item.parent)->unlang == CF_UNLANG_MODULES) &&
3158 (strcmp(css->name1, "update") == 0)) {
3159 /*
3160 * Module configuration can contain "update" statements.
3161 */
3163 css->allow_locals = false;
3164 }
3165 break;
3166
3167 /*
3168 * It's a policy section - allow unlang inside of child sections.
3169 */
3170 case CF_UNLANG_POLICY:
3171 css->unlang = CF_UNLANG_ALLOW;
3172 css->allow_locals = true;
3173 break;
3174
3175 /*
3176 * A virtual server has processing sections, but only a limited number of them.
3177 * Rather than trying to autoload them and glue the interpreter into the conf
3178 * file parser, we just hack it.
3179 */
3180 case CF_UNLANG_SERVER:
3181 // git grep SECTION_NAME src/process/ src/lib/server/process.h | sed 's/.*SECTION_NAME("//;s/",.*//' | sort -u
3183 css->unlang = CF_UNLANG_ALLOW;
3184 css->allow_locals = true;
3185 break;
3186 }
3187
3188 /*
3189 * Allow local variables, but no unlang statements.
3190 */
3191 if (strcmp(css->name1, "dictionary") == 0) {
3193 css->allow_locals = true;
3194 break;
3195 }
3196
3197 /*
3198 * ldap sync has "update" a few levels down.
3199 */
3200 if (strcmp(css->name1, "listen") == 0) {
3202 }
3203 break;
3204
3205 /*
3206 * Virtual modules in the "modules" section can have unlang.
3207 */
3208 case CF_UNLANG_MODULES:
3209 if ((strcmp(css->name1, "group") == 0) ||
3210 (strcmp(css->name1, "load-balance") == 0) ||
3211 (strcmp(css->name1, "redundant") == 0) ||
3212 (strcmp(css->name1, "redundant-load-balance") == 0)) {
3213 css->unlang = CF_UNLANG_ALLOW;
3214 css->allow_locals = true;
3215 } else {
3217 }
3218 break;
3219
3220 case CF_UNLANG_EDIT:
3221 /*
3222 * Edit sections can only have children which are edit sections.
3223 */
3224 css->unlang = CF_UNLANG_EDIT;
3225 break;
3226
3227 case CF_UNLANG_ALLOW:
3228 /*
3229 * If we are doing list assignment, then don't allow local variables. The children are
3230 * also then all edit sections, and not unlang statements.
3231 *
3232 * If we're not doing list assignment, then name2 has to be a bare word, string, etc.
3233 */
3234 css->allow_locals = !fr_list_assignment_op[name2_token];
3235 if (css->allow_locals) {
3236 /*
3237 * @todo - tighten this up for "actions" sections, and module rcode
3238 * over-rides.
3239 *
3240 * Perhaps the best way to do that is to change the syntax for module
3241 * over-rides, so that the parser doesn't have to guess. :(
3242 */
3243 css->unlang = CF_UNLANG_ALLOW;
3244 } else {
3245 css->unlang = CF_UNLANG_EDIT;
3246 }
3247 break;
3248
3249 /*
3250 * We can (maybe?) do nested assignments inside of an old-style "update" or "map" section
3251 */
3254 break;
3255
3258 css->allow_locals = true;
3259 break;
3260
3262 if (strcmp(css->name1, "update") == 0) {
3264 } else {
3266 }
3267 break;
3268 }
3269
3270add_section:
3271 /*
3272 * The current section is now the child section.
3273 */
3274 frame->current = css;
3275 frame->braces++;
3276 css = NULL;
3277 stack->ptr = ptr;
3278 return 1;
3279
3280
3281 /*
3282 * If we're not parsing a section, then the next
3283 * token MUST be an operator.
3284 */
3285operator:
3286 op_ptr = ptr;
3287 name2_token = gettoken(&ptr, buff[2], stack->bufsize, false);
3288 switch (name2_token) {
3289 case T_OP_ADD_EQ:
3290 case T_OP_SUB_EQ:
3291 case T_OP_MUL_EQ:
3292 case T_OP_DIV_EQ:
3293 case T_OP_AND_EQ:
3294 case T_OP_OR_EQ:
3295 case T_OP_NE:
3296 case T_OP_RSHIFT_EQ:
3297 case T_OP_GE:
3298 case T_OP_GT:
3299 case T_OP_LSHIFT_EQ:
3300 case T_OP_LE:
3301 case T_OP_LT:
3302 case T_OP_CMP_EQ:
3303 case T_OP_CMP_FALSE:
3304 case T_OP_SET:
3305 case T_OP_PREPEND:
3306 /*
3307 * Allow more operators in unlang statements, edit sections, and old-style "update" sections.
3308 */
3309 if ((parent->unlang != CF_UNLANG_ALLOW) && (parent->unlang != CF_UNLANG_EDIT) && (parent->unlang != CF_UNLANG_ASSIGNMENT)) {
3310 return parse_error(stack, op_ptr, "Invalid operator for assignment");
3311 }
3313
3314 case T_OP_EQ:
3315 /*
3316 * Configuration variables can only use =
3317 */
3318 fr_skip_whitespace(ptr);
3319 op_token = name2_token;
3320 break;
3321
3322 default:
3323 return parse_error(stack, op_ptr, "Syntax error, the input should be an assignment operator");
3324 }
3325
3326 /*
3327 * MUST have something after the operator.
3328 */
3329 if (!*ptr || (*ptr == '#') || (*ptr == ',') || (*ptr == ';')) {
3330 return parse_error(stack, ptr, "Missing value after operator");
3331 }
3332
3333 /*
3334 * foo = { ... } for nested groups.
3335 *
3336 * As a special case, we allow sub-sections after '=', etc.
3337 *
3338 * This syntax is only for inside of "update"
3339 * sections, and for attributes of type "group".
3340 * But the parser isn't (yet) smart enough to
3341 * know about that context. So we just silently
3342 * allow it everywhere.
3343 */
3344 if (*ptr == '{') {
3345 if ((parent->unlang != CF_UNLANG_ALLOW) && (parent->unlang != CF_UNLANG_EDIT)) {
3346 return parse_error(stack, ptr, "Invalid location for nested attribute assignment");
3347 }
3348
3349 if (!fr_list_assignment_op[name2_token]) {
3350 return parse_error(stack, ptr, "Invalid assignment operator for list");
3351 }
3352
3353 /*
3354 * Now that we've peeked ahead to
3355 * see the open brace, parse it
3356 * for real.
3357 */
3358 ptr++;
3359
3360 /*
3361 * Leave name2_token as the
3362 * operator (as a hack). But
3363 * note that there's no actual
3364 * name2. We'll deal with that
3365 * situation later.
3366 */
3367 value = NULL;
3368 goto alloc_section;
3369 }
3370
3371 fr_skip_whitespace(ptr);
3372
3373 /*
3374 * Parse the value for a CONF_PAIR.
3375 *
3376 * If it's unlang or an edit section, the RHS can be an expression.
3377 */
3378 if ((parent->unlang == CF_UNLANG_ALLOW) || (parent->unlang == CF_UNLANG_EDIT)) {
3379 bool eol;
3380 ssize_t slen;
3381
3382 name2_ptr = ptr;
3383
3384 /*
3385 * If the RHS is an expression (foo) or function %foo(), then mark it up as an expression.
3386 */
3387 if ((*ptr == '(') || (*ptr == '%')) {
3388 /* nothing */
3389
3390 } else if (cf_get_token(parent, &name2_ptr, &value_token, buff[2], stack->bufsize,
3391 frame->filename, frame->lineno) == 0) {
3392 /*
3393 * We have one token (bare word), followed by EOL. It's just a token.
3394 */
3395 fr_skip_whitespace(name2_ptr);
3396 if (terminal_end_line[(uint8_t) *name2_ptr]) {
3397 parent->allow_locals = false;
3398 ptr = name2_ptr;
3399 value = buff[2];
3400 goto alloc_pair;
3401 }
3402 } /* else it looks like an expression */
3403
3404 /*
3405 * Parse the text as an expression.
3406 *
3407 * Note that unlike conditions, expressions MUST use \ at the EOL for continuation.
3408 * If we automatically read past EOL, as with:
3409 *
3410 * &foo := (bar -
3411 * baz)
3412 *
3413 * That works, mostly. Until the user forgets to put the trailing ')', and then
3414 * the parse is bad enough that it tries to read to EOF, or to some other random
3415 * parse error.
3416 *
3417 * So the simplest way to avoid utter craziness is to simply require a signal which
3418 * says "yes, I intended to put this over multiple lines".
3419 */
3420 slen = fr_skip_condition(ptr, NULL, terminal_end_line, &eol);
3421 if (slen < 0) {
3422 return parse_error(stack, ptr + (-slen), fr_strerror());
3423 }
3424
3425 /*
3426 * We parsed until the end of the string, but the condition still needs more data.
3427 */
3428 if (eol) {
3429 return parse_error(stack, ptr + slen, "Expression is unfinished at end of line");
3430 }
3431
3432 /*
3433 * Keep a copy of the entire RHS.
3434 */
3435 memcpy(buff[2], ptr, slen);
3436 buff[2][slen] = '\0';
3437
3438 value = buff[2];
3439
3440 /*
3441 * Mark it up as an expression
3442 *
3443 * @todo - we should really just call cf_data_add() to add a flag, but this is good for
3444 * now. See map_afrom_cp()
3445 */
3446 value_token = T_HASH;
3447
3448 /*
3449 * Skip terminal characters
3450 */
3451 ptr += slen;
3452 if ((*ptr == ',') || (*ptr == ';')) ptr++;
3453
3454#if 0
3455 } else if ((parent->unlang != CF_UNLANG_ASSIGNMENT) &&
3456 ((*ptr == '`') || (*ptr == '%') || (*ptr == '('))) {
3457 /*
3458 * Config sections can't use backticks, xlat expansions, or expressions.
3459 *
3460 * Except module configurations can have key = %{...}
3461 */
3462 return parse_error(stack, ptr, "Invalid value for assignment in configuration file");
3463#endif
3464
3465 } else {
3466 if (cf_get_token(parent, &ptr, &value_token, buff[2], stack->bufsize,
3467 frame->filename, frame->lineno) < 0) {
3468 return -1;
3469 }
3470 value = buff[2];
3471 }
3472
3473 /*
3474 * We have an attribute assignment, which means that we no longer allow local variables to be
3475 * defined.
3476 */
3477 parent->allow_locals = false;
3478
3479alloc_pair:
3480 if (add_pair(parent, buff[1], value, name1_token, op_token, value_token, buff[3], frame->filename, frame->lineno) < 0) return -1;
3481
3482added_pair:
3483 fr_skip_whitespace(ptr);
3484
3485 /*
3486 * Skip semicolon if we see it after a
3487 * CONF_PAIR. Also allow comma for
3488 * backwards compatibility with secret
3489 * things in v3.
3490 */
3491 if ((*ptr == ';') || (*ptr == ',')) {
3492 ptr++;
3493 stack->ptr = ptr;
3494 return 1;
3495 }
3496
3497 /*
3498 * Closing brace is allowed after a CONF_PAIR
3499 * definition.
3500 */
3501 if (*ptr == '}') {
3502 stack->ptr = ptr;
3503 return 1;
3504 }
3505
3506 /*
3507 * Anything OTHER than EOL or comment is a syntax
3508 * error.
3509 */
3510 if (*ptr && (*ptr != '#')) {
3511 return parse_error(stack, ptr, "Unexpected text after configuration item");
3512 }
3513
3514 /*
3515 * Since we're at EOL or comment, just drop the
3516 * text, and go read another line of text.
3517 */
3518 return 0;
3519}
3520
3521
3523{
3524 cf_stack_frame_t *frame = &stack->frame[stack->depth];
3525 CONF_SECTION *parent = frame->current;
3526 cf_file_heap_t *h;
3527
3528 fr_heap_pop((void **)&h, &frame->heap);
3529 if (!h) {
3530 /*
3531 * Done reading the directory entry. Close it, and go
3532 * back up a stack frame.
3533 */
3534 talloc_free(frame->directory);
3535 stack->depth--;
3536 return 0;
3537 }
3538
3539 /*
3540 * Push the next filename onto the stack.
3541 */
3542 stack->depth++;
3543 frame = &stack->frame[stack->depth];
3544 memset(frame, 0, sizeof(*frame));
3545
3546 frame->type = CF_STACK_FILE;
3547 frame->fp = NULL;
3548 frame->parent = parent;
3549 frame->current = parent;
3550 frame->filename = h->filename;
3551 frame->lineno = 0;
3552 frame->from_dir = true;
3553 return 1;
3554}
3555
3556
3558
3559void cf_md5_init(void)
3560{
3562}
3563
3564
3565static void cf_md5_update(char const *p)
3566{
3567 if (!cf_md5_ctx) return;
3568
3569 fr_md5_update(cf_md5_ctx, (uint8_t const *)p, strlen(p));
3570}
3571
3573{
3574 if (!cf_md5_ctx) {
3575 memset(digest, 0, MD5_DIGEST_LENGTH);
3576 return;
3577 }
3578
3579 fr_md5_final(digest, cf_md5_ctx);
3581 cf_md5_ctx = NULL;
3582}
3583
3585{
3586 bool at_eof, has_spaces;
3587 size_t len;
3588 char const *ptr;
3589 cf_stack_frame_t *frame = &stack->frame[stack->depth];
3590
3591read_more:
3592 has_spaces = false;
3593
3594read_continuation:
3595 /*
3596 * Get data, and remember if we are at EOF.
3597 */
3598 at_eof = (fgets(stack->fill, stack->bufsize - (stack->fill - stack->buff[0]), frame->fp) == NULL);
3599 cf_md5_update(stack->fill);
3600 frame->lineno++;
3601
3602 /*
3603 * We read the entire 8k worth of data: complain.
3604 * Note that we don't care if the last character
3605 * is \n: it's still forbidden. This means that
3606 * the maximum allowed length of text is 8k-1, which
3607 * should be plenty.
3608 */
3609 len = strlen(stack->fill);
3610 if ((stack->fill + len + 1) >= (stack->buff[0] + stack->bufsize)) {
3611 ERROR("%s[%d]: Line too long", frame->filename, frame->lineno);
3612 return -1;
3613 }
3614
3615 /*
3616 * Suppress leading whitespace after a
3617 * continuation line.
3618 */
3619 if (has_spaces) {
3620 ptr = stack->fill;
3621 fr_skip_whitespace(ptr);
3622
3623 if (ptr > stack->fill) {
3624 memmove(stack->fill, ptr, len - (ptr - stack->fill));
3625 len -= (ptr - stack->fill);
3626 }
3627 }
3628
3629 /*
3630 * Skip blank lines when we're at the start of
3631 * the read buffer.
3632 */
3633 if (stack->fill == stack->buff[0]) {
3634 if (at_eof) return 0;
3635
3636 ptr = stack->buff[0];
3637 fr_skip_whitespace(ptr);
3638
3639 if (!*ptr || (*ptr == '#')) goto read_more;
3640
3641 } else if (at_eof || (len == 0)) {
3642 ERROR("%s[%d]: Continuation at EOF is illegal", frame->filename, frame->lineno);
3643 return -1;
3644 }
3645
3646 /*
3647 * See if there's a continuation.
3648 */
3649 while ((len > 0) &&
3650 ((stack->fill[len - 1] == '\n') || (stack->fill[len - 1] == '\r'))) {
3651 len--;
3652 stack->fill[len] = '\0';
3653 }
3654
3655 if ((len > 0) && (stack->fill[len - 1] == '\\')) {
3656 /*
3657 * Check for "suppress spaces" magic.
3658 */
3659 if (!has_spaces && (len > 2) && (stack->fill[len - 2] == '"')) {
3660 has_spaces = true;
3661 }
3662
3663 stack->fill[len - 1] = '\0';
3664 stack->fill += len - 1;
3665 goto read_continuation;
3666 }
3667
3668 ptr = stack->fill;
3669
3670 /*
3671 * We now have one full line of text in the input
3672 * buffer, without continuations.
3673 */
3674 fr_skip_whitespace(ptr);
3675
3676 /*
3677 * Nothing left, or just a comment. Go read
3678 * another line of text.
3679 *
3680 * If the caller asked us to preserve comments (utilities like
3681 * radconf2json round-trip them as CONF_COMMENT items), capture
3682 * the comment text on the current section before continuing.
3683 */
3684 if (!*ptr || (*ptr == '#')) {
3685 if (_cf_preserve_comments() && frame->current) {
3686 char const *text;
3687 CONF_COMMENT *c;
3688 char *p;
3689
3690 /*
3691 * `#` line: keep everything after the marker
3692 * verbatim (whitespace included; operators use
3693 * leading indent to format banners). Empty
3694 * line: emit a blank marker (NULL text) so the
3695 * writer round-trips the visual separator, and
3696 * downstream tooling can tell two comment blocks
3697 * separated by a blank line apart from one long
3698 * block.
3699 */
3700 text = (*ptr == '#') ? ptr + 1 : NULL;
3701 c = cf_comment_alloc(frame->current, text);
3702 if (c) {
3703 /* Trim trailing CR/LF off the talloc'd copy */
3704 p = UNCONST(char *, c->text);
3705 if (p) {
3706 size_t l = strlen(p);
3707 while (l > 0 && (p[l - 1] == '\n' || p[l - 1] == '\r')) {
3708 p[--l] = '\0';
3709 }
3710 }
3711 cf_filename_set(c, frame->filename);
3712 cf_lineno_set(c, frame->lineno);
3713 }
3714 }
3715 goto read_more;
3716 }
3717
3718 return 1;
3719}
3720
3721
3722/*
3723 * Read a configuration file or files.
3724 */
3726{
3728 char const *ptr;
3729
3730 cf_stack_frame_t *frame;
3731 int rcode;
3732
3733do_frame:
3734 frame = &stack->frame[stack->depth];
3735 parent = frame->current; /* add items here */
3736
3737 switch (frame->type) {
3738#ifdef HAVE_GLOB_H
3739 case CF_STACK_GLOB:
3740 if (frame->gl_current == frame->glob.gl_pathc) {
3741 globfree(&frame->glob);
3742 goto pop_stack;
3743 }
3744
3745 /*
3746 * Process the filename as an include.
3747 */
3748 if (process_include(stack, parent, frame->glob.gl_pathv[frame->gl_current++], frame->required, false) < 0) return -1;
3749
3750 /*
3751 * Run the correct frame. If the file is NOT
3752 * required, then the call to process_include()
3753 * may return 0, and we just process the next
3754 * glob. Otherwise, the call to
3755 * process_include() may return a directory or a
3756 * filename. Go handle that.
3757 */
3758 goto do_frame;
3759#endif
3760
3761#ifdef HAVE_DIRENT_H
3762 case CF_STACK_DIR:
3763 rcode = frame_readdir(stack);
3764 if (rcode == 0) goto do_frame;
3765 if (rcode < 0) return -1;
3766
3767 /*
3768 * Reset which frame we're looking at.
3769 */
3770 frame = &stack->frame[stack->depth];
3771 fr_assert(frame->type == CF_STACK_FILE);
3772 break;
3773#endif
3774
3775 case CF_STACK_FILE:
3776 break;
3777 }
3778
3779#ifndef NDEBUG
3780 /*
3781 * One last sanity check.
3782 */
3783 if (frame->type != CF_STACK_FILE) {
3784 cf_log_err(frame->current, "%s: Internal sanity check failed", __FUNCTION__);
3785 goto pop_stack;
3786 }
3787#endif
3788
3789 /*
3790 * Open the new file if necessary. It either came from
3791 * the first call to the function, or was pushed onto the
3792 * stack by another function.
3793 */
3794 if (!frame->fp) {
3795 rcode = cf_file_open(frame->parent, frame->filename, frame->from_dir, &frame->fp);
3796 if (rcode < 0) return -1;
3797
3798 /*
3799 * Ignore this file
3800 */
3801 if (rcode == 1) {
3802 cf_log_warn(frame->current, "Ignoring file %s - it was already read",
3803 frame->filename);
3804 goto pop_stack;
3805 }
3806 }
3807
3808 /*
3809 * Read, checking for line continuations ('\\' at EOL)
3810 */
3811 for (;;) {
3812 /*
3813 * Fill the buffers with data.
3814 */
3815 stack->fill = stack->buff[0];
3816 rcode = cf_file_fill(stack);
3817 if (rcode < 0) return -1;
3818 if (rcode == 0) break;
3819
3820 /*
3821 * The text here MUST be at the start of a line,
3822 * OR have only whitespace in front of it.
3823 */
3824 ptr = stack->buff[0];
3825 fr_skip_whitespace(ptr);
3826
3827 if (*ptr == '$') {
3828 /*
3829 * Allow for $INCLUDE files
3830 */
3831 if (strncasecmp(ptr, "$INCLUDE", 8) == 0) {
3832 ptr += 8;
3833
3834 stack->ptr = ptr;
3835 if (process_include(stack, parent, ptr, true, true) < 0) return -1;
3836 goto do_frame;
3837 }
3838
3839 if (strncasecmp(ptr, "$-INCLUDE", 9) == 0) {
3840 ptr += 9;
3841
3842 stack->ptr = ptr;
3843 rcode = process_include(stack, parent, ptr, false, true);
3844 if (rcode < 0) return -1;
3845 if (rcode == 0) continue;
3846 goto do_frame;
3847 }
3848
3849 /*
3850 * Allow for $TEMPLATE things
3851 */
3852 if (strncasecmp(ptr, "$TEMPLATE", 9) == 0) {
3853 ptr += 9;
3854 fr_skip_whitespace(ptr);
3855
3856 stack->ptr = ptr;
3857 if (process_template(stack) < 0) return -1;
3858 continue;
3859 }
3860
3861 stack->ptr = ptr;
3862 return parse_error(stack, ptr, "Unknown $... keyword");
3863 }
3864
3865 /*
3866 * All of the file handling code is done. Parse the input.
3867 */
3868 do {
3869 fr_skip_whitespace(ptr);
3870 if (!*ptr || (*ptr == '#')) break;
3871
3872 stack->ptr = ptr;
3873 rcode = parse_input(stack);
3874 ptr = stack->ptr;
3875
3876 if (rcode < 0) return -1;
3877 parent = frame->current;
3878 } while (rcode == 1);
3879 }
3880
3881 fr_assert(frame->fp != NULL);
3882
3883 /*
3884 * See if EOF was unexpected.
3885 */
3886 if (feof(frame->fp) && (parent != frame->parent)) {
3887 ERROR("%s[%d]: EOF reached without closing brace for section %s starting at line %d",
3889 return -1;
3890 }
3891
3892 fclose(frame->fp);
3893 frame->fp = NULL;
3894
3895pop_stack:
3896 /*
3897 * More things to read, go read them.
3898 */
3899 if (stack->depth > 0) {
3900 stack->depth--;
3901 goto do_frame;
3902 }
3903
3904 return 0;
3905}
3906
3908{
3909 cf_stack_frame_t *frame = &stack->frame[stack->depth];
3910
3911 while (stack->depth >= 0) {
3912 switch (frame->type) {
3913 case CF_STACK_FILE:
3914 if (frame->fp) fclose(frame->fp);
3915 frame->fp = NULL;
3916 break;
3917
3918#ifdef HAVE_DIRENT_H
3919 case CF_STACK_DIR:
3920 talloc_free(frame->directory);
3921 break;
3922#endif
3923
3924#ifdef HAVE_GLOB_H
3925 case CF_STACK_GLOB:
3926 globfree(&frame->glob);
3927 break;
3928#endif
3929 }
3930
3931 frame--;
3932 stack->depth--;
3933 }
3934
3935 talloc_free(stack->buff);
3936}
3937
3938/*
3939 * Bootstrap a config file.
3940 */
3941int cf_file_read(CONF_SECTION *cs, char const *filename, bool root)
3942{
3943 int i;
3944 fr_rb_tree_t *tree;
3946 cf_stack_frame_t *frame;
3947
3948 /*
3949 * Only add the default config directory if we're loading a top-level configuration file.
3950 */
3951 if (root) {
3952 char const *p;
3953 char *confdir = NULL;
3954 CONF_PAIR *cp;
3955
3956 /*
3957 * For compatibility, this goes first. And we don't care if there are too many '/'.
3958 */
3959 cp = cf_pair_alloc(cs, "raddbdir", filename, T_OP_EQ, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3960 if (!cp) return -1;
3961
3962 /*
3963 * This goes second, and we try to be nice about too many '/'.
3964 *
3965 * "confdir" is the directory portion of filename, so trim everything
3966 * from the final path separator onwards before storing it.
3967 */
3968 p = strrchr(filename, FR_DIR_SEP);
3969 if (p) {
3970 confdir = talloc_bstrndup(cs, filename, p - filename);
3971 cp = cf_pair_alloc(cs, "confdir", confdir, T_OP_EQ, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3972 talloc_free(confdir);
3973 } else {
3974 cp = cf_pair_alloc(cs, "confdir", filename, T_OP_EQ, T_BARE_WORD, T_SINGLE_QUOTED_STRING);
3975 }
3976 if (!cp) return -1;
3977 }
3978
3979 MEM(tree = fr_rb_inline_talloc_alloc(cs, cf_file_t, node, _inode_cmp, NULL));
3980
3981 cf_data_add(cs, tree, "filename", false);
3982
3983#ifndef NDEBUG
3984 memset(&stack, 0, sizeof(stack));
3985#endif
3986
3987 /*
3988 * Allocate temporary buffers on the heap (so we don't use *all* the stack space)
3989 */
3990 stack.buff = talloc_array(cs, char *, 4);
3991 for (i = 0; i < 4; i++) MEM(stack.buff[i] = talloc_array(stack.buff, char, 8192));
3992
3993 stack.depth = 0;
3994 stack.bufsize = 8192;
3995 frame = &stack.frame[stack.depth];
3996
3997 memset(frame, 0, sizeof(*frame));
3998 frame->parent = frame->current = cs;
3999
4000 frame->type = CF_STACK_FILE;
4001 frame->filename = talloc_strdup(frame->parent, filename);
4002 cs->item.filename = frame->filename;
4003
4004 if (cf_file_include(&stack) < 0) {
4006 return -1;
4007 }
4008
4009 talloc_free(stack.buff);
4010
4011 /*
4012 * Now that we've read the file, go back through it and
4013 * expand the variables.
4014 */
4015 if (cf_section_pass2(cs) < 0) {
4016 cf_log_err(cs, "Parsing config items failed");
4017 return -1;
4018 }
4019
4020 return 0;
4021}
4022
4023/** Bootstrap a configuration section from an in-memory buffer.
4024 *
4025 * Mirrors cf_file_read(), but reads the configuration text from a buffer via fmemopen() instead of from a
4026 * file on disk. The buffer is treated as if it were the contents of "filename", which is used in error
4027 * messages and stored as the section's source filename.
4028 *
4029 * $INCLUDE / $-INCLUDEs are taken relative to the directory portion of "filename", as with cf_file_read().
4030 *
4031 * Unlike cf_file_read(), this function never sets the "raddbdir" / "confdir" CONF_PAIRs (the buffer has no
4032 * canonical install path) and never inserts "filename" into the dedup tree.
4033 *
4034 * @param[in] cs Top-level configuration section to populate.
4035 * @param[in] buffer The configuration text. The caller retains ownership
4036 * and may free the buffer after this call returns.
4037 * @param[in] buflen Length of @p buffer in bytes (excluding any trailing NUL).
4038 * @param[in] filename Virtual filename for error reporting. May be a
4039 * placeholder like "<inline>" if there is no real file.
4040 * @return 0 on success, -1 on failure.
4041 */
4042int cf_file_read_buffer(CONF_SECTION *cs, char const *buffer, size_t buflen, char const *filename)
4043{
4044 int i;
4045 FILE *fp;
4046 fr_rb_tree_t *tree;
4048 cf_stack_frame_t *frame;
4049
4050 fp = fmemopen(UNCONST(char *, buffer), buflen, "r");
4051 if (!fp) {
4052 ERROR("Failed opening in-memory buffer for parsing: %s", fr_syserror(errno));
4053 return -1;
4054 }
4055
4056 /*
4057 * The dedup-by-inode filename tree may already exist if the caller
4058 * is reading more than one buffer / file into the same root section.
4059 */
4060 tree = cf_data_value(cf_data_find(cs, fr_rb_tree_t, "filename"));
4061 if (!tree) {
4062 MEM(tree = fr_rb_inline_talloc_alloc(cs, cf_file_t, node, _inode_cmp, NULL));
4063 cf_data_add(cs, tree, "filename", false);
4064 }
4065
4066#ifndef NDEBUG
4067 memset(&stack, 0, sizeof(stack));
4068#endif
4069
4070 /*
4071 * Allocate temporary buffers on the heap (so we don't use *all* the stack space)
4072 */
4073 stack.buff = talloc_array(cs, char *, 4);
4074 for (i = 0; i < 4; i++) MEM(stack.buff[i] = talloc_array(stack.buff, char, 8192));
4075
4076 stack.depth = 0;
4077 stack.bufsize = 8192;
4078 frame = &stack.frame[stack.depth];
4079
4080 memset(frame, 0, sizeof(*frame));
4081 frame->parent = frame->current = cs;
4082
4083 frame->type = CF_STACK_FILE;
4084 frame->filename = talloc_strdup(frame->parent, filename);
4085 cs->item.filename = frame->filename;
4086
4087 /*
4088 * Pre-set frame->fp so cf_file_include() skips its own cf_file_open()
4089 * call (which would try to fopen() a real path). cf_file_include()
4090 * fclose()s frame->fp on the way out, which for an fmemopen() handle
4091 * does not free the underlying buffer.
4092 */
4093 frame->fp = fp;
4094
4095 if (cf_file_include(&stack) < 0) {
4097 return -1;
4098 }
4099
4100 talloc_free(stack.buff);
4101
4102 /*
4103 * Now that we've read the buffer, go back through it and
4104 * expand the variables.
4105 */
4106 if (cf_section_pass2(cs) < 0) {
4107 cf_log_err(cs, "Parsing config items failed");
4108 return -1;
4109 }
4110
4111 return 0;
4112}
4113
4115{
4116 talloc_free(cs);
4117}
4118
4119static char const parse_tabs[] = " ";
4120
4121static ssize_t cf_string_write(FILE *fp, char const *string, size_t len, fr_token_t t)
4122{
4123 size_t outlen;
4124 char c;
4125 char buffer[2048];
4126
4127 switch (t) {
4128 default:
4129 c = '\0';
4130 break;
4131
4133 c = '"';
4134 break;
4135
4137 c = '\'';
4138 break;
4139
4141 c = '`';
4142 break;
4143 }
4144
4145 if (c) fprintf(fp, "%c", c);
4146
4147 outlen = fr_snprint(buffer, sizeof(buffer), string, len, c);
4148 fwrite(buffer, outlen, 1, fp);
4149
4150 if (c) fprintf(fp, "%c", c);
4151 return 1;
4152}
4153
4154int cf_pair_write(FILE *fp, CONF_PAIR *cp)
4155{
4156 if (!cp->value) {
4157 fprintf(fp, "%s\n", cp->attr);
4158 return 0;
4159 }
4160
4161 cf_string_write(fp, cp->attr, strlen(cp->attr), cp->lhs_quote);
4162 fprintf(fp, " %s ", fr_table_str_by_value(fr_tokens_table, cp->op, "<INVALID>"));
4163 cf_string_write(fp, cp->value, strlen(cp->value), cp->rhs_quote);
4164 fprintf(fp, "\n");
4165
4166 return 1; /* FIXME */
4167}
4168
4169
4170int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth)
4171{
4172 if (!fp || !cs) return -1;
4173
4174 /*
4175 * Print the section name1, etc.
4176 */
4177 fwrite(parse_tabs, depth, 1, fp);
4178 cf_string_write(fp, cs->name1, strlen(cs->name1), T_BARE_WORD);
4179
4180 /*
4181 * FIXME: check for "if" or "elsif". And if so, print
4182 * out the parsed condition, instead of the input text
4183 *
4184 * cf_data_find(cs, CF_DATA_TYPE_UNLANG, "if");
4185 */
4186 if (cs->name2) {
4187 fputs(" ", fp);
4188
4189#if 0
4190 c = cf_data_value(cf_data_find(cs, fr_cond_t, NULL));
4191 if (c) {
4192 char buffer[1024];
4193
4194 cond_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), c);
4195 fprintf(fp, "(%s)", buffer);
4196 } else
4197#endif
4198 cf_string_write(fp, cs->name2, strlen(cs->name2), cs->name2_quote);
4199 }
4200
4201 fputs(" {\n", fp);
4202
4203 cf_section_write_children(fp, cs, depth + 1);
4204
4205 fwrite(parse_tabs, depth, 1, fp);
4206 fputs("}\n\n", fp);
4207
4208 return 1;
4209}
4210
4211/** Emit the children of a section at `depth` without an enclosing `{ ... }`.
4212 *
4213 * cf_section_write wraps a section in `name { ... }`; this helper writes only
4214 * the children at the requested indent, which is what tools like
4215 * `radjson2conf -r` need: rendering a synthetic-root section as a file-scope
4216 * fragment ready to be `$INCLUDE`d at any depth.
4217 *
4218 * Blank lines in the source come back through as NULL-text CONF_ITEM_COMMENT
4219 * markers, so the writer doesn't have to synthesise its own separators - just
4220 * emit what's there. Consecutive blank markers collapse to a single blank
4221 * line on output so artifacts from upstream tooling (deletes that left their
4222 * preceding blank behind, splits that introduced extra spacers) don't
4223 * accumulate as visible whitespace.
4224 */
4226{
4227 bool prev_was_blank = false;
4228
4229 if (!fp || !cs) return -1;
4230
4231 cf_item_foreach(&cs->item, ci) {
4232 switch (ci->type) {
4233 case CONF_ITEM_SECTION:
4235 /*
4236 * Sections close with `}\n\n` so the trailing
4237 * blank line is already in the stream; flag it
4238 * so a NULL-text comment immediately after
4239 * doesn't double it up.
4240 */
4241 prev_was_blank = true;
4242 break;
4243
4244 case CONF_ITEM_PAIR:
4245 /*
4246 * Ignore internal things.
4247 */
4248 if (!ci->filename || (ci->filename[0] == '<')) break;
4249
4250 fwrite(parse_tabs, depth, 1, fp);
4252 prev_was_blank = false;
4253 break;
4254
4255 case CONF_ITEM_COMMENT: {
4256 CONF_COMMENT const *c = cf_item_to_comment(ci);
4257
4258 /*
4259 * NULL text == blank line. Collapse
4260 * runs of blanks to a single one.
4261 */
4262 if (c->text == NULL) {
4263 if (!prev_was_blank) {
4264 fputc('\n', fp);
4265 prev_was_blank = true;
4266 }
4267 break;
4268 }
4269
4270 fwrite(parse_tabs, depth, 1, fp);
4271 fputc('#', fp);
4272 fputs(c->text, fp);
4273 fputc('\n', fp);
4274 prev_was_blank = false;
4275 } break;
4276
4277 default:
4278 break;
4279 }
4280 }
4281
4282 return 1;
4283}
4284
4285
4287 CONF_SECTION const *outer_cs,
4288 char const *ptr)
4289{
4290 CONF_PAIR *cp;
4291 CONF_SECTION *next;
4292 CONF_SECTION const *cs = outer_cs;
4293 char name[8192], *p;
4294 char const *name2;
4295
4296 if (!ptr || (!parent_cs && !outer_cs)) {
4297 fr_strerror_const("Invalid argument");
4298 return NULL;
4299 }
4300
4301 if (!*ptr) {
4302 fr_strerror_const("Empty string is invalid");
4303 return NULL;
4304 }
4305
4306 strlcpy(name, ptr, sizeof(name));
4307
4308 p = name;
4309
4310 /*
4311 * ".foo" means "foo from the current section"
4312 */
4313 if (*p == '.') {
4314 p++;
4315
4316 /*
4317 * Just '.' means the current section
4318 */
4319 if (*p == '\0') return cf_section_to_item(cs); /* const issues */
4320
4321 /*
4322 * ..foo means "foo from the section
4323 * enclosing this section" (etc.)
4324 */
4325 while (*p == '.') {
4326 if (cs->item.parent) cs = cf_item_to_section(cs->item.parent);
4327
4328 /*
4329 * .. means the section
4330 * enclosing this section
4331 */
4332 if (!*++p) return cf_section_to_item(cs); /* const issues */
4333 }
4334
4335 /*
4336 * "foo.bar.baz" means "from the given root"
4337 */
4338 } else if (strchr(p, '.') != NULL) {
4339 if (!parent_cs) {
4340 missing_parent:
4341 fr_strerror_const("Missing parent configuration section");
4342 return NULL;
4343 }
4344 cs = parent_cs;
4345
4346 /*
4347 * "foo" could be from the current section, either as a
4348 * section or as a pair.
4349 *
4350 * If that isn't found, search from the given root.
4351 */
4352 } else {
4353 next = cf_section_find(cs, p, NULL);
4354 if (!next && cs->template) next = cf_section_find(cs->template, p, NULL);
4355 if (next) return &(next->item);
4356
4357 cp = cf_pair_find(cs, p);
4358 if (!cp && cs->template) cp = cf_pair_find(cs->template, p);
4359 if (cp) return &(cp->item);
4360
4361 if (!parent_cs) goto missing_parent;
4362 cs = parent_cs;
4363 }
4364
4365 /*
4366 * Chop the string into pieces, and look up the pieces.
4367 */
4368 while (*p) {
4369 char *n1, *n2, *q;
4370
4371 n1 = p;
4372 n2 = NULL;
4373 q = p;
4374
4375 fr_assert(*q);
4376
4377 /*
4378 * Look for a terminating '.' or '[', to get name1 and possibly name2.
4379 */
4380 while (*q != '\0') {
4381 /*
4382 * foo.bar -> return "foo"
4383 */
4384 if (*q == '.') {
4385 *q++ = '\0'; /* leave 'q' after the '.' */
4386 break;
4387 }
4388
4389 /*
4390 * name1 is anything up to '[' or EOS.
4391 */
4392 if (*q != '[') {
4393 q++;
4394 continue;
4395 }
4396
4397 /*
4398 * Found "name1[", look for "name2]" or "name2]."
4399 */
4400 *q++ = '\0';
4401 n2 = q;
4402
4403 while (*q != '\0') {
4404 if (*q == '[') {
4405 fr_strerror_const("Invalid reference, '[' cannot be used inside of a '[...]' block");
4406 return NULL;
4407 }
4408
4409 if (*q != ']') {
4410 q++;
4411 continue;
4412 }
4413
4414 /*
4415 * We've found the trailing ']'
4416 */
4417 *q++ = '\0';
4418
4419 /*
4420 * "name2]"
4421 */
4422 if (!*q) break;
4423
4424 /*
4425 * Must be "name2]."
4426 */
4427 if (*q++ == '.') break;
4428
4429 fr_strerror_const("Invalid reference, ']' is not followed by '.'");
4430 return NULL;
4431 }
4432
4433 /*
4434 * "name1[name2", but not "name1[name2]"
4435 *
4436 * The inner loop exited because it found *q == '\0',
4437 * meaning that the closing ']' was never found.
4438 */
4439 if (!*q) {
4440 fr_strerror_printf("Invalid reference after '%s', missing close ']'", n1);
4441 return NULL;
4442 }
4443
4444 break;
4445 }
4446 p = q; /* get it ready for the next round */
4447
4448 /*
4449 * End of the string. The thing could be a section with
4450 * two names, a section with one name, or a pair.
4451 *
4452 * And if we don't find the thing we're looking for here,
4453 * check the template section.
4454 */
4455 if (!*p) {
4456 /*
4457 * Two names, must be a section.
4458 */
4459 if (n2) {
4460 next = cf_section_find(cs, n1, n2);
4461 if (!next && cs->template) next = cf_section_find(cs->template, n1, n2);
4462 if (next) return &(next->item);
4463
4464 fail:
4465 name2 = cf_section_name2(cs);
4466 fr_strerror_printf("Parent section %s%s%s { ... } does not contain a %s %s { ... } configuration section",
4467 cf_section_name1(cs),
4468 name2 ? " " : "", name2 ? name2 : "",
4469 n1, n2);
4470 return NULL;
4471 }
4472
4473 /*
4474 * One name, the final thing can be a section or a pair.
4475 */
4476 next = cf_section_find(cs, n1, NULL);
4477 if (!next && cs->template) next = cf_section_find(cs->template, n1, NULL);
4478
4479 if (next) return &(next->item);
4480
4481 cp = cf_pair_find(cs, n1);
4482 if (!cp && cs->template) cp = cf_pair_find(cs->template, n1);
4483 if (cp) return &(cp->item);
4484
4485 name2 = cf_section_name2(cs);
4486 fr_strerror_printf("Parent section %s%s%s { ... } does not contain a %s configuration item",
4487 cf_section_name1(cs),
4488 name2 ? " " : "", name2 ? name2 : "",
4489 n1);
4490 return NULL;
4491 }
4492
4493 /*
4494 * There's more to the string. The thing we're looking
4495 * for MUST be a configuration section.
4496 */
4497 next = cf_section_find(cs, n1, n2);
4498 if (!next && cs->template) next = cf_section_find(cs->template, n1, n2);
4499 if (next) {
4500 cs = next;
4501 continue;
4502 }
4503
4504 if (n2) goto fail;
4505
4506 name2 = cf_section_name2(cs);
4507 fr_strerror_printf("Parent section %s%s%s { ... } does not contain a %s { ... } configuration section",
4508 cf_section_name1(cs),
4509 name2 ? " " : "", name2 ? name2 : "",
4510 n1);
4511 return NULL;
4512 }
4513
4514 /*
4515 * We've fallen off of the end of the string. This should not have happened!
4516 */
4517 fr_strerror_const("Cannot parse reference");
4518 return NULL;
4519}
4520
4521/*
4522 * Only for unit_test_map
4523 */
4525{
4527 fr_assert(!cs->item.parent);
4528
4529 cs->unlang = CF_UNLANG_ALLOW;
4530}
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:560
#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:391
#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:384
#define NUM_ELEMENTS(_t)
Definition build.h:406
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:2558
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:4225
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:4524
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:2532
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:4042
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:3522
static CONF_ITEM * process_map(cf_stack_t *stack)
Definition cf_file.c:1815
static CONF_ITEM * process_foreach(cf_stack_t *stack)
Definition cf_file.c:2187
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:2372
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:3559
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:2136
static fr_cmp_ret_t filename_cmp(void const *one, void const *two)
Definition cf_file.c:1253
static const bool cf_name_char1[SBUFF_CHAR_CLASS]
Convert tokens back to a quoting character.
Definition cf_file.c:2547
static void cf_stack_cleanup(cf_stack_t *stack)
Definition cf_file.c:3907
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:1652
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:3941
static fr_md5_ctx_t * cf_md5_ctx
Definition cf_file.c:3557
int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth)
Definition cf_file.c:4170
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:1648
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:4121
static char const parse_tabs[]
Definition cf_file.c:4119
static fr_cmp_ret_t _inode_cmp(void const *one, void const *two)
Definition cf_file.c:704
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:3725
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:2534
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
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:3584
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:4286
char const * filename
Definition cf_file.c:1249
static CONF_ITEM * process_subrequest(cf_stack_t *stack)
Definition cf_file.c:1903
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:2523
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:1591
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:3572
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:1664
static void cf_md5_update(char const *p)
Definition cf_file.c:3565
void cf_file_free(CONF_SECTION *cs)
Definition cf_file.c:4114
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:2024
int cf_pair_write(FILE *fp, CONF_PAIR *cp)
Definition cf_file.c:4154
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:2431
static int parse_error(cf_stack_t *stack, char const *ptr, char const *message)
Definition cf_file.c:2116
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:840
bool _cf_preserve_comments(void)
Definition cf_util.c:820
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:1486
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1362
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
Definition cf_util.c:1916
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:749
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:1444
char const * cf_section_name1(CONF_SECTION const *cs)
Return the first identifier of a CONF_SECTION.
Definition cf_util.c:1348
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:1204
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:695
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:1597
CONF_COMMENT * cf_comment_alloc(CONF_SECTION *parent, char const *text)
Allocate a new comment item attached to parent.
Definition cf_util.c:845
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:782
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
Definition cf_util.c:629
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:1035
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:675
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1756
CONF_ITEM * cf_pair_to_item(CONF_PAIR const *cp)
Cast a CONF_PAIR to a CONF_ITEM.
Definition cf_util.c:733
#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:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
Test enumeration values.
Definition dict_test.h:92
Definition dwarf.c:424
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
Definition heap.c:149
int fr_heap_pop(void **out, fr_heap_t **hp)
Remove a node from the heap.
Definition heap.c:359
unsigned int fr_heap_index_t
Definition heap.h:82
#define fr_heap_alloc(_ctx, _cmp, _type, _field, _init)
Creates a heap that can be used with non-talloced elements.
Definition heap.h:102
#define FR_HEAP_INDEX_INVALID
Definition heap.h:85
The main heap structure.
Definition heap.h:68
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
fr_cmp_ret_t
Result of an ordering comparison.
Definition misc.h:50
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
#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)
int fr_rb_find(void **found, fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:586
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:637
#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 char * base(const char *p)
Definition testlib.c:55
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
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
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