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