31RCSID(
"$Id: 024dd68b013e511162487ccf31ed9ca14a93e569 $")
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>
59#include <freeradius-devel/server/main_config.h>
78 {
L(
"accounting"),
true },
80 {
L(
"authenticate"),
true },
84 {
L(
"establish"),
true },
85 {
L(
"finally"),
true },
91 {
L(
"verify"),
true },
105#define MAX_STACK (32)
161 char **p_p,
char const **ptr_p,
char *output,
size_t outsize,
178 p = strrchr(
name,
'/');
192 next = strchr(ptr,
'}');
195 ERROR(
"%s[%d]: File expansion missing }",
204 if ((next - ptr) >= (
name + PATH_MAX - p)) {
205 ERROR(
"%s[%d]: File name is too large",
210 memcpy(p, ptr, next - ptr);
211 p[next - ptr] =
'\0';
213 fd = open(
name, O_RDONLY);
215 ERROR(
"%s[%d]: Failed opening %s: %s",
216 cf, lineno,
name, strerror(errno));
221 room = (output + outsize) - p;
227 len = read(fd, p, room);
229 ERROR(
"%s[%d]: Failed reading %s: %s",
230 cf, lineno,
name, strerror(errno));
237 ERROR(
"%s[%d]: Failed reading %s: the file is empty",
246 if ((
size_t) len >= room) {
247 ERROR(
"%s[%d]: Too much data in %s: did not read the entire file",
256 char *q, *end = p + len;
270 for (q = p; q < end; q++) {
271 if (*q >=
' ')
break;
277 ERROR(
"%s[%d]: Too much data in %s: expected one line of text, found multiple lines in the file",
299 char *output,
size_t outsize,
300 char const *input,
ssize_t inlen,
bool *soft_fail,
bool soft_fail_env)
303 char const *end, *next, *ptr;
307 if (soft_fail) *soft_fail =
false;
317 if (want >= outsize) want = outsize - 1;
318 memcpy(output, input, want);
344 while (*ptr && (!end || (ptr < end))) {
348 if ((*ptr ==
'$') && (ptr[1] ==
'{')) {
356 ERROR(
"%s[%d]: Failed parsing variable expansion '%s''",
368 if ((
size_t) len >=
sizeof(
name)) {
369 ERROR(
"%s[%d]: Reference string is too large",
374 memcpy(
name, ptr, len - 3);
375 name[len - 3] =
'\0';
383 if (
name[0] ==
'/') {
384 int fd = open(
name, O_RDONLY);
388 ERROR(
"%s[%d]: Reference \"${%s}\" failed opening file - %s", cf, lineno,
name,
fr_syserror(errno));
392 if (fstat(fd, &buf) < 0) {
395 ERROR(
"%s[%d]: Reference \"${%s}\" failed reading file - %s", cf, lineno,
name,
fr_syserror(errno));
399 if (buf.st_size >= ((output + outsize) - p)) {
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));
406 len = read(fd, p, (output + outsize) - p);
407 if (len < 0)
goto fail_fd;
416 q = strchr(
name,
':');
423 if (soft_fail) *soft_fail =
true;
424 PERROR(
"%s[%d]: Failed finding reference \"${%s}\"", cf, lineno,
name);
438 ERROR(
"%s[%d]: Can only reference properties of sections", cf, lineno);
453 ERROR(
"%s[%d]: Invalid property '%s'", cf, lineno, q);
458 if ((p + flen) >= (output + outsize))
goto too_long;
477 if (soft_fail) *soft_fail =
true;
479 ERROR(
"%s[%d]: Reference \"%s\" points to a variable which has not been expanded.",
485 ERROR(
"%s[%d]: Reference \"%s\" has no value",
490 if (p + strlen(cp->
value) >= output + outsize) {
491 ERROR(
"%s[%d]: Reference \"%s\" is too long",
508 for (ci_p = &outer_cs->
item; ci_p != NULL; ci_p = ci_p->
parent) {
510 ERROR(
"%s[%d]: Cannot reference different item in same section", cf, lineno);
524 ERROR(
"%s[%d]: Failed copying reference %s", cf, lineno,
name);
534 ERROR(
"%s[%d]: Reference \"%s\" type is invalid", cf, lineno, input);
537 }
else if (strncmp(ptr,
"$ENV{", 5) == 0) {
547 next = strchr(ptr,
'}');
550 ERROR(
"%s[%d]: Environment variable expansion missing }",
559 if ((
size_t) (next - ptr) >=
sizeof(
name)) {
560 ERROR(
"%s[%d]: Environment variable name is too large",
565 memcpy(
name, ptr, next - ptr);
566 name[next - ptr] =
'\0';
574 if (!soft_fail_env) {
575 ERROR(
"%s[%d]: Invalid reference to missing environment variable $ENV{%s}",
584 if (p + strlen(env) >= output + outsize) {
585 ERROR(
"%s[%d]: Reference \"%s\" is too long",
594 }
else if (strncmp(ptr,
"$VALUE{", 7) == 0) {
597 if (
cf_expand_file(cf, lineno,
name, &p, &ptr, output, outsize,
false) < 0)
return NULL;
599 }
else if (strncmp(ptr,
"$FILE{", 6) == 0) {
602 if (
cf_expand_file(cf, lineno,
name, &p, &ptr, output, outsize,
true) < 0)
return NULL;
612 if (p >= (output + outsize)) {
614 ERROR(
"%s[%d]: Reference \"%s\" is too long",
630 if (!cs || !
template)
return true;
656 if (!cp2)
return false;
688 if (!subcs2)
return false;
710 return CMP(a->
buf.st_ino, b->buf.st_ino);
741 if (
fr_dirfd(&my_fd, &r, filename) < 0) {
742 ERROR(
"Failed to open directory containing %s", filename);
746 if (fstatat(my_fd, r, &my_file.
buf, 0) < 0) {
747 if (my_fd != AT_FDCWD) close(my_fd);
763 if (my_fd != AT_FDCWD) close(my_fd);
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");
772 fp = fopen(filename,
"r");
787 file->from_dir = from_dir;
789 if (fstat(fileno(fp), &
file->buf) == 0) {
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);
844 uid_t euid = (uid_t) -1;
845 gid_t egid = (gid_t) -1;
869 ret = cb(filename, uctx);
871 if (seteuid(euid) < 0) {
878 if (setegid(egid) < 0) {
904 struct sockaddr_un addr = { .sun_family = AF_UNIX };
913 strlcpy(addr.sun_path, filename,
sizeof(addr.sun_path));
915 fd = socket(AF_UNIX, SOCK_STREAM, 0);
921 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
928 if (connect(fd, (
struct sockaddr *)&addr,
sizeof(addr)) < 0) {
978 if (stat(filename, &buf) < 0) {
997 if (!S_ISSOCK(buf.st_mode)) {
1025 fd = open(filename, O_RDONLY);
1028 if (fd >= 0) close(fd);
1049 if (
file && fstat(fd, &
file->buf) < 0)
goto error;
1087 if (stat(filename, &
file->buf) < 0) {
1089 cf_log_perr(cp,
"Unable to open file \"%s\"", filename);
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);
1149 if (!
value)
return -1;
1166 char *
buffer,
size_t bufsize)
1173 p = strrchr(
buffer, FR_DIR_SEP);
1174 if (!p)
return filename;
1179 dirsize = (p -
buffer) + 1;
1181 if ((dirsize + strlen(filename)) >= bufsize) {
1185 strlcpy(p + 1, filename, bufsize - dirsize);
1195 char const *filename,
int lineno)
1197 char const *ptr = *ptr_p;
1215 ERROR(
"%s[%d]: %s", filename, lineno, text);
1223 if ((
size_t) slen >= buflen) {
1224 ERROR(
"%s[%d]: Name is too long", filename, lineno);
1268 ERROR(
"%s[%d]: Parse error: Invalid location for $INCLUDE",
1279 while (*ptr && !isspace((
uint8_t) *ptr)) ptr++;
1297 if (*
value ==
'$') relative =
false;
1301 if (!
value)
return -1;
1303 if (!FR_DIR_IS_RELATIVE(
value)) relative =
false;
1313 if (strchr(
value,
'*') != 0) {
1320 memset(frame, 0,
sizeof(*frame));
1322 frame->
type = CF_STACK_GLOB;
1323 frame->required = required;
1333 if (glob(
value, GLOB_ERR | GLOB_NOESCAPE, NULL, &frame->glob) < 0) {
1343 if (frame->glob.gl_pathc == 0) {
1349 ERROR(
"%s[%d]: Failed expanding '%s' - No matching files", frame->
filename, frame->
lineno,
1362 struct stat statbuf;
1364 if (stat(
value, &statbuf) < 0) {
1381 memset(frame, 0,
sizeof(*frame));
1402 struct stat stat_buf;
1416 dir = opendir(directory);
1418 ERROR(
"%s[%d]: Error reading directory %s: %s",
1422 if (dir) closedir(dir);
1434 if (fstat(my_fd, &stat_buf) < 0) {
1440 if ((stat_buf.st_mode & S_IWOTH) != 0) {
1441 ERROR(
"%s[%d]: Directory %s is globally writable. Refusing to start due to "
1442 "insecure configuration", frame->
filename, frame->
lineno, directory);
1458 .
type = CF_STACK_DIR,
1459 .directory = directory,
1473 while ((dp = readdir(dir)) != NULL) {
1477 if (dp->d_name[0] ==
'.')
continue;
1482 for (p = dp->d_name; *p !=
'\0'; p++) {
1487 (*p ==
'.'))
continue;
1490 if (*p !=
'\0')
continue;
1495 len = strlen(dp->d_name);
1496 if ((len > 10) && (strncmp(&dp->d_name[len - 10],
".dpkg-dist", 10) == 0)) {
1498 WARN(
"Ignoring packaging system produced file %s%s", frame->directory, dp->d_name);
1501 if ((len > 9) && (strncmp(&dp->d_name[len - 9],
".dpkg-old", 9) == 0))
goto pkg_file;
1502 if ((len > 7) && (strncmp(&dp->d_name[len - 7],
".rpmnew", 7) == 0))
goto pkg_file;
1503 if ((len > 8) && (strncmp(&dp->d_name[len - 8],
".rpmsave", 8) == 0))
goto pkg_file;
1506 frame->directory, dp->d_name);
1508 if (stat(
stack->buff[1], &stat_buf) != 0) {
1509 ERROR(
"%s[%d]: Failed checking file %s: %s",
1510 (frame - 1)->filename, (frame - 1)->lineno,
1515 if (S_ISDIR(stat_buf.st_mode)) {
1516 WARN(
"%s[%d]: Ignoring directory %s",
1517 (frame - 1)->filename, (frame - 1)->lineno,
1532 ERROR(
"%s[%d]: Error including %s: No support for directories!",
1548 if (token !=
T_EOL) {
1554 ERROR(
"%s[%d]: Internal sanity check error in template reference", frame->
filename, frame->
lineno);
1568 parent->template = templatecs;
1576 ERROR(
"%s[%d]: Cannot find template \"%s\", as no 'templates' section exists.",
1583 PERROR(
"%s[%d]: Failed finding item \"%s\" in the 'templates' section.",
1617 char const *ptr =
stack->ptr;
1658 if (slen > 0)
break;
1675 p = (
uint8_t const *) ptr + (-slen);
1681 if (*p && (*p <
' ')) {
1682 while ((*p ==
'\r') || (*p ==
'\n')) {
1724 cf_log_err(cs,
"Expected '{' instead of %s", ptr);
1735 buff[2][slen] =
'\0';
1740 buff[2][slen - 1] =
'\0';
1766 char const *
value = NULL;
1769 char const *ptr =
stack->ptr;
1786 ERROR(
"%s[%d]: Invalid syntax for 'map' - module name must not be a quoted string",
1809 ERROR(
"%s[%d]: Expecting string expansions in 'map' definition",
1815 ERROR(
"%s[%d]: Expecting section start brace '{' in 'map' definition",
1828 ERROR(
"%s[%d]: Failed allocating memory for section",
1838 css->
argv = talloc_array(css,
char const *, 1);
1853 char const *mod = NULL;
1856 char const *ptr =
stack->ptr;
1905 ERROR(
"%s[%d]: The second argument to 'subrequest' must be an attribute reference",
1925 ERROR(
"%s[%d]: The third argument to 'subrequest' must be an attribute reference",
1932 ERROR(
"%s[%d]: Expecting section start brace '{' in 'subrequest' definition",
1944 ERROR(
"%s[%d]: Failed allocating memory for section",
1956 css->
argv = talloc_array(css,
char const *, values);
1959 for (i = 0; i < values; i++) {
1976 char const *ptr =
stack->ptr;
2000 while (isalpha((
uint8_t) *ptr)) ptr++;
2004 ERROR(
"%s[%d]: Invalid syntax for 'catch' - unknown rcode '%s'",
2009 if ((*ptr !=
'{') && !isspace((
uint8_t) *ptr)) {
2010 ERROR(
"%s[%d]: Invalid syntax for 'catch' - unexpected text at '%s'",
2021 ERROR(
"%s[%d]: Invalid syntax for 'catch' - too many arguments at'%s'",
2033 ERROR(
"%s[%d]: Failed allocating memory for section",
2046 css->
argv = talloc_array(css,
char const *, argc + 1);
2050 for (i = 0; i < argc; i++) {
2055 css->
argv[argc] = NULL;
2069 if (!ptr) ptr =
stack->ptr;
2088 char const *ptr = *ptr_p;
2103 (void)
parse_error(
stack, type_ptr,
"Unknown or invalid variable type in 'foreach'");
2115 (void)
parse_error(
stack, ptr2,
"Invalid variable name for key in 'foreach'");
2140 char const *ptr =
stack->ptr, *ptr2, *type_ptr;
2146 ERROR(
"%s[%d]: Failed allocating memory for section",
2186 cf_log_warn(css,
"Using deprecated syntax. Please use the new 'foreach' syntax.");
2210 if (*ptr ==
'(')
goto alloc_argc_2;
2225 css->
argv = talloc_array(css,
char const *, css->
argc);
2247 (void)
parse_error(
stack, type_ptr,
"Invalid data type for 'key' variable");
2256 (void)
parse_error(
stack, ptr,
"Expected (...) after 'foreach' variable definition");
2260 goto parse_expression;
2264 css->
argv = talloc_array(css,
char const *, css->
argc);
2322 char *
buff,
char const *filename,
int lineno)
2335 char const *expanded;
2342 }
else if (!soft_fail) {
2385 char const *ptr =
stack->ptr;
2396 while (isalpha(*ptr)) ptr++;
2399 ERROR(
"%s[%d]: Missing ')' in cast",
2407 ERROR(
"%s[%d]: Unknown data type '%.*s' in cast",
2413 ERROR(
"%s[%d]: Invalid data type '%.*s' in cast",
2432 ERROR(
"%s[%d]: Failed allocating memory for section",
2457 css->
argv = talloc_array(css,
char const *, css->
argc);
2488#pragma clang diagnostic ignored "-Wgnu-designator"
2496 [
'0' ...
'9' ] =
true,
2497 [
'A' ...
'Z' ] =
true,
2498 [
'a' ...
'z' ] =
true,
2511 char const *ptr =
stack->ptr;
2512 char const *name1_ptr, *name2_ptr, *op_ptr;
2546 if (frame->
braces == 0) {
2587 switch (
parent->unlang) {
2593 if (name1_token ==
T_EOL)
return 0;
2600 return parse_error(
stack, name1_ptr,
"Invalid location for quoted string");
2603 if (*
buff[1] ==
'%') {
2604 return parse_error(
stack, name1_ptr,
"Cannot use functions outside of a processing section");
2607 if (*
buff[1] ==
'&') {
2608 return parse_error(
stack, name1_ptr,
"Cannot reference attributes outside of a processing section");
2629 WARN(
"%s[%d]: Using '&' is no longer necessary when referencing attributes. Please delete it.", frame->
filename, frame->
lineno);
2659 return parse_error(
stack, name1_ptr,
"Invalid location for unlang keyword");
2663 ci = process(
stack);
2668 parent->allow_locals =
false;
2683 if (!isalnum((
int) *ptr))
goto check_for_eol;
2695 parent->allow_locals =
false;
2719 return parse_error(
stack, name1_ptr,
"Invalid data type for local variable. Must be 'tlv' or else a non-structural type");
2753 if (*ptr !=
'{')
goto alloc_pair;
2765 if (!*ptr || (*ptr ==
'#') || (*ptr ==
',') || (*ptr ==
';') || (*ptr ==
'}')) {
2773 parent->allow_locals =
false;
2795 switch (
parent->unlang) {
2802 if (*ptr ==
'=')
goto operator;
2816 return parse_error(
stack, name2_ptr,
"Unexpected operator, was expecting a configuration section. Is there a missing '}' somewhere?");
2819 return parse_error(
stack, name2_ptr,
"Invalid second name for configuration section");
2828 return parse_error(
stack, name2_ptr,
"Unexpected quoted string after section name");
2838 ((strcmp(
buff[1],
"load-balance") == 0) ||
2839 (strcmp(
buff[1],
"redundant-load-balance") == 0))) {
2860 css->
argv = talloc_array(css,
char const *, 1);
2894 if ((strcmp(
buff[1],
"case") == 0) ||
2895 (strcmp(
buff[1],
"limit") == 0) ||
2896 (strcmp(
buff[1],
"timeout") == 0)) {
2903 if (!((*ptr ==
'"') || (*ptr ==
'`') || (*ptr ==
'\'') || ((*ptr ==
'&') && (ptr[1] !=
'=')) ||
2929 return parse_error(
stack, name1_ptr,
"Invalid location for local variable - definitions cannot be inside of an assignment");
2933 return parse_error(
stack, name1_ptr,
"Invalid location for local variable - definitions must go at the start of a section");
2936 return parse_error(
stack, name1_ptr,
"Invalid location for local variable - definitions can only go in a processing section.");
2942 parent->allow_locals =
false;
2961 if (
buff[1][0] ==
'@') {
2975 if (
name[1] ==
'.') {
3014 ERROR(
"%s[%d]: Reference \"%s\" already contains a \"%s\" section at %s[%d]",
3031 ERROR(
"%s[%d]: '@%s' references can only appear at the top level of a section",
3056 ERROR(
"%s[%d]: Failed allocating memory for section",
3074 switch (
parent->unlang) {
3076 if (!
parent->item.parent) {
3083 (strcmp(css->
name1,
"update") == 0)) {
3116 if (strcmp(css->
name1,
"dictionary") == 0) {
3125 if (strcmp(css->
name1,
"listen") == 0) {
3134 if ((strcmp(css->
name1,
"group") == 0) ||
3135 (strcmp(css->
name1,
"load-balance") == 0) ||
3136 (strcmp(css->
name1,
"redundant") == 0) ||
3137 (strcmp(css->
name1,
"redundant-load-balance") == 0)) {
3187 if (strcmp(css->
name1,
"update") == 0) {
3213 switch (name2_token) {
3248 return parse_error(
stack, op_ptr,
"Syntax error, the input should be an assignment operator");
3254 if (!*ptr || (*ptr ==
'#') || (*ptr ==
',') || (*ptr ==
';')) {
3271 return parse_error(
stack, ptr,
"Invalid location for nested attribute assignment");
3312 if ((*ptr ==
'(') || (*ptr ==
'%')) {
3322 parent->allow_locals =
false;
3354 return parse_error(
stack, ptr + slen,
"Expression is unfinished at end of line");
3360 memcpy(
buff[2], ptr, slen);
3361 buff[2][slen] =
'\0';
3377 if ((*ptr ==
',') || (*ptr ==
';')) ptr++;
3381 ((*ptr ==
'`') || (*ptr ==
'%') || (*ptr ==
'('))) {
3387 return parse_error(
stack, ptr,
"Invalid value for assignment in configuration file");
3402 parent->allow_locals =
false;
3416 if ((*ptr ==
';') || (*ptr ==
',')) {
3435 if (*ptr && (*ptr !=
'#')) {
3436 return parse_error(
stack, ptr,
"Unexpected text after configuration item");
3469 memset(frame, 0,
sizeof(*frame));
3511 bool at_eof, has_spaces;
3523 at_eof = (fgets(
stack->fill,
stack->bufsize - (
stack->fill -
stack->buff[0]), frame->fp) == NULL);
3534 len = strlen(
stack->fill);
3548 if (ptr >
stack->fill) {
3549 memmove(
stack->fill, ptr, len - (ptr -
stack->fill));
3550 len -= (ptr -
stack->fill);
3559 if (at_eof)
return 0;
3561 ptr =
stack->buff[0];
3564 if (!*ptr || (*ptr ==
'#'))
goto read_more;
3566 }
else if (at_eof || (len == 0)) {
3575 ((
stack->fill[len - 1] ==
'\n') || (
stack->fill[len - 1] ==
'\r'))) {
3577 stack->fill[len] =
'\0';
3580 if ((len > 0) && (
stack->fill[len - 1] ==
'\\')) {
3584 if (!has_spaces && (len > 2) && (
stack->fill[len - 2] ==
'"')) {
3588 stack->fill[len - 1] =
'\0';
3589 stack->fill += len - 1;
3590 goto read_continuation;
3609 if (!*ptr || (*ptr ==
'#')) {
3625 text = (*ptr ==
'#') ? ptr + 1 : NULL;
3631 size_t l = strlen(p);
3632 while (l > 0 && (p[l - 1] ==
'\n' || p[l - 1] ==
'\r')) {
3662 switch (frame->
type) {
3665 if (frame->gl_current == frame->glob.gl_pathc) {
3666 globfree(&frame->glob);
3689 if (rcode == 0)
goto do_frame;
3690 if (rcode < 0)
return -1;
3721 if (rcode < 0)
return -1;
3742 if (rcode < 0)
return -1;
3743 if (rcode == 0)
break;
3749 ptr =
stack->buff[0];
3769 if (rcode < 0)
return -1;
3770 if (rcode == 0)
continue;
3795 if (!*ptr || (*ptr ==
'#'))
break;
3801 if (rcode < 0)
return -1;
3803 }
while (rcode == 1);
3812 ERROR(
"%s[%d]: EOF reached without closing brace for section %s starting at line %d",
3824 if (
stack->depth > 0) {
3836 while (
stack->depth >= 0) {
3837 switch (frame->
type) {
3839 if (frame->fp) fclose(frame->fp);
3851 globfree(&frame->glob);
3878 char *confdir = NULL;
3893 p = strrchr(filename, FR_DIR_SEP);
3915 stack.buff = talloc_array(cs,
char *, 4);
3916 for (i = 0; i < 4; i++)
MEM(
stack.buff[i] = talloc_array(
stack.buff,
char, 8192));
3919 stack.bufsize = 8192;
3922 memset(frame, 0,
sizeof(*frame));
3941 cf_log_err(cs,
"Parsing config items failed");
3977 ERROR(
"Failed opening in-memory buffer for parsing: %s",
fr_syserror(errno));
3998 stack.buff = talloc_array(cs,
char *, 4);
3999 for (i = 0; i < 4; i++)
MEM(
stack.buff[i] = talloc_array(
stack.buff,
char, 8192));
4002 stack.bufsize = 8192;
4005 memset(frame, 0,
sizeof(*frame));
4032 cf_log_err(cs,
"Parsing config items failed");
4070 if (c) fprintf(fp,
"%c", c);
4073 fwrite(
buffer, outlen, 1, fp);
4075 if (c) fprintf(fp,
"%c", c);
4082 fprintf(fp,
"%s\n", cp->
attr);
4097 if (!fp || !cs)
return -1;
4120 fprintf(fp,
"(%s)",
buffer);
4152 bool prev_was_blank =
false;
4154 if (!fp || !cs)
return -1;
4166 prev_was_blank =
true;
4173 if (!ci->filename || (ci->filename[0] ==
'<'))
break;
4177 prev_was_blank =
false;
4187 if (c->
text == NULL) {
4188 if (!prev_was_blank) {
4190 prev_was_blank =
true;
4199 prev_was_blank =
false;
4218 char name[8192], *p;
4221 if (!ptr || (!parent_cs && !outer_cs)) {
4263 }
else if (strchr(p,
'.') != NULL) {
4280 if (next)
return &(next->
item);
4284 if (cp)
return &(cp->
item);
4286 if (!parent_cs)
goto missing_parent;
4305 while (*q !=
'\0') {
4328 while (*q !=
'\0') {
4330 fr_strerror_const(
"Invalid reference, '[' cannot be used inside of a '[...]' block");
4352 if (*q++ ==
'.')
break;
4387 if (next)
return &(next->
item);
4391 fr_strerror_printf(
"Parent section %s%s%s { ... } does not contain a %s %s { ... } configuration section",
4393 name2 ?
" " :
"", name2 ? name2 :
"",
4404 if (next)
return &(next->
item);
4408 if (cp)
return &(cp->
item);
4411 fr_strerror_printf(
"Parent section %s%s%s { ... } does not contain a %s configuration item",
4413 name2 ?
" " :
"", name2 ? name2 :
"",
4432 fr_strerror_printf(
"Parent section %s%s%s { ... } does not contain a %s { ... } configuration section",
4434 name2 ?
" " :
"", name2 ? name2 :
"",
static int const char char buffer[256]
strcpy(log_entry->msg, buffer)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define L(_str)
Helper for initialising arrays of string literals.
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
#define CMP_RETURN(_a, _b, _field)
Return if the comparison is not 0 (is unequal)
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
int at_reference_braces
braces when we found this thing
CONF_SECTION * current
sub-section we're reading
static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
static int parse_input(cf_stack_t *stack)
int cf_section_write_children(FILE *fp, CONF_SECTION *cs, int depth)
Emit the children of a section at depth without an enclosing { ... }.
char * fill
where we start filling the buffer from
void cf_section_set_unlang(CONF_SECTION *cs)
static int unlang_keywords_len
static fr_table_num_sorted_t const server_unlang_section[]
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.
static int frame_readdir(cf_stack_t *stack)
static CONF_ITEM * process_map(cf_stack_t *stack)
static CONF_ITEM * process_foreach(cf_stack_t *stack)
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)
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)
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)
static int parse_type_name(cf_stack_t *stack, char const **ptr_p, char const *type_ptr, fr_type_t *type_p)
static const bool cf_name_char1[SBUFF_CHAR_CLASS]
Convert tokens back to a quoting character.
static void cf_stack_cleanup(cf_stack_t *stack)
cf_file_check_err_t cf_file_check_unix_perm(char const *filename, UNUSED void *uctx)
Check if file exists, and is a socket.
static const bool terminal_end_line[SBUFF_CHAR_CLASS]
static int cf_file_open(CONF_SECTION *cs, char const *filename, bool from_dir, FILE **fp_p)
int cf_file_read(CONF_SECTION *cs, char const *filename, bool root)
static fr_md5_ctx_t * cf_md5_ctx
static int8_t filename_cmp(void const *one, void const *two)
int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth)
int lineno
line in that filename
static const bool terminal_end_section[SBUFF_CHAR_CLASS]
static size_t conf_property_name_len
static fr_table_num_sorted_t const conf_property_name[]
int cf_section_pass2(CONF_SECTION *cs)
bool from_dir
this file was read from $include foo/
static ssize_t cf_string_write(FILE *fp, char const *string, size_t len, fr_token_t t)
static char const parse_tabs[]
cf_file_check_err_t cf_file_check(CONF_PAIR *cp, bool check_perms)
Do some checks on the file as an "input" file.
static int cf_file_include(cf_stack_t *stack)
static uid_t conf_check_uid
static gid_t conf_check_gid
CONF_ITEM *(* cf_process_func_t)(cf_stack_t *)
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)
static int8_t _inode_cmp(void const *one, void const *two)
CONF_SECTION * at_reference
was this thing an @foo ?
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.
static int cf_file_fill(cf_stack_t *stack)
char const * ptr
current parse pointer
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.
CONF_ITEM * cf_reference_item(CONF_SECTION const *parent_cs, CONF_SECTION const *outer_cs, char const *ptr)
static CONF_ITEM * process_subrequest(cf_stack_t *stack)
void cf_file_check_set_uid_gid(uid_t uid, gid_t gid)
Set the euid/egid used when performing file checks.
enum conf_property CONF_PROPERTY
static fr_table_ptr_sorted_t unlang_keywords[]
char ** buff
buffers for reading / parsing
CONF_SECTION * parent
which started this file
static int process_template(cf_stack_t *stack)
size_t bufsize
size of the buffers
static size_t server_unlang_section_len
void cf_md5_final(uint8_t *digest)
static int process_include(cf_stack_t *stack, CONF_SECTION *parent, char const *ptr, bool required, bool relative)
static CONF_ITEM * process_if(cf_stack_t *stack)
static void cf_md5_update(char const *p)
void cf_file_free(CONF_SECTION *cs)
static char const * cf_local_file(char const *base, char const *filename, char *buffer, size_t bufsize)
static CONF_ITEM * process_catch(cf_stack_t *stack)
int cf_pair_write(FILE *fp, CONF_PAIR *cp)
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.
char const * filename
filename we're reading
static CONF_ITEM * process_switch(cf_stack_t *stack)
static int parse_error(cf_stack_t *stack, char const *ptr, char const *message)
cf_file_check_err_t
Results of file checks.
@ CF_FILE_OK
File checks passed.
@ CF_FILE_NO_PERMISSION
Requested permissions not set.
@ CF_FILE_NO_UNIX_SOCKET
File is not a unix socket.
@ CF_FILE_OTHER_ERROR
Other error occurred checking permissions.
@ CF_FILE_NO_EXIST
File does not exist.
cf_parse_t on_read
Function to call as the item is being read, just after it has been allocated and initialized.
Defines a CONF_PAIR to C data type mapping.
CONF_SECTION * cs
CONF_SECTION associated with the file.
CONF_ITEM item
Common set of fields.
CONF_ITEM * parent
Parent.
fr_token_t name2_quote
The type of quoting around name2.
char const * name2
Second name token. Given foo bar {} would be bar.
bool allow_locals
allow local variables
int argc
number of additional arguments
char const * attr
Attribute name.
bool referenced
Was this item referenced in the config?
fr_token_t rhs_quote
Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
char const * value
Attribute value.
char const * name1
First name token. Given foo bar {} would be foo.
@ CF_UNLANG_MODULES
this section is in "modules", allow unlang 2 down
@ CF_UNLANG_NONE
no unlang
@ CF_UNLANG_CAN_HAVE_UPDATE
can have "update"
@ CF_UNLANG_ALLOW
allow unlang in this section
@ CF_UNLANG_POLICY
this section is a policy, allow unlang 2 down
@ CF_UNLANG_ASSIGNMENT
only assignments inside of map / update
@ CF_UNLANG_EDIT
only edit commands
@ CF_UNLANG_DICTIONARY
only local variable definitions
@ CF_UNLANG_SERVER
this section is a virtual server, allow unlang 2 down
fr_token_t op
Operator e.g. =, :=.
CONF_ITEM item
Common set of fields.
bool pass2
do expansion in pass2.
char const * filename
The file the config item was parsed from.
struct stat buf
stat about the file
bool at_reference
this thing was created from an ...
bool _cf_expand_variables(void)
bool _cf_preserve_comments(void)
char const * filename
name of the file
char const * text
Comment text, with the leading # and any surrounding whitespace stripped.
@ CONF_ITEM_COMMENT
A # ... line preserved verbatim from the input - only created when the parser is asked to keep commen...
char const ** argv
additional arguments
fr_token_t lhs_quote
Name quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
int lineno
The line number the config item began on.
CONF_ITEM_TYPE type
Whether the config item is a config_pair, conf_section or cf_data.
Internal data that is associated with a configuration section.
Common header for all CONF_* types.
Configuration AVP similar to a fr_pair_t.
A section grouping multiple CONF_PAIR.
CONF_PAIR * cf_pair_dup(CONF_SECTION *parent, CONF_PAIR *cp, bool copy_meta)
Duplicate a CONF_PAIR.
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
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.
char const * cf_section_name1(CONF_SECTION const *cs)
Return the first identifier of a CONF_SECTION.
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
CONF_COMMENT * cf_comment_alloc(CONF_SECTION *parent, char const *text)
Allocate a new comment item attached to parent.
CONF_COMMENT * cf_item_to_comment(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_COMMENT (asserts on type mismatch).
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
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.
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
CONF_ITEM * cf_pair_to_item(CONF_PAIR const *cp)
Cast a CONF_PAIR to a CONF_ITEM.
#define cf_log_err(_cf, _fmt,...)
#define cf_data_add(_cf, _data, _name, _free)
#define cf_data_find(_cf, _type, _name)
#define cf_lineno_set(_ci, _lineno)
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
#define cf_log_perr(_cf, _fmt,...)
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
#define CF_TO_ITEM(_cf)
Auto cast from the input type to CONF_ITEM (which is the base type)
#define cf_item_foreach(_parent, _iter)
Iterate over every child item of a CONF_SECTION (or any CONF_ITEM that has children).
#define cf_filename_set(_ci, _filename)
#define cf_log_warn(_cf, _fmt,...)
#define cf_log_debug(_cf, _fmt,...)
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
void * fr_heap_pop(fr_heap_t **hp)
Remove a node from the heap.
unsigned int fr_heap_index_t
#define fr_heap_alloc(_ctx, _cmp, _type, _field, _init)
Creates a heap that can be used with non-talloced elements.
#define FR_HEAP_INDEX_INVALID
#define DEBUG_ENABLED2
True if global debug level 1-2 messages are enabled.
int fr_dirfd(int *dirfd, char const **filename, char const *pathname)
From a pathname, return fd and filename needed for *at() functions.
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.
static char * stack[MAX_STACK]
#define fr_md5_final(_out, _ctx)
Finalise the ctx, producing the digest.
#define fr_md5_ctx_alloc()
Allocation function for MD5 digest context.
#define fr_md5_update(_ctx, _in, _inlen)
Ingest plaintext into the digest.
#define fr_md5_ctx_free(_ctx)
Free function for MD5 digest ctx.
#define MD5_DIGEST_LENGTH
@ 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_GROUP
A grouping of other attributes.
static uint8_t depth(fr_minmax_heap_index_t i)
int strncasecmp(char *s1, char *s2, int n)
void fr_perm_file_error(int num)
Write a file access error to the fr_strerror buffer, including euid/egid.
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.
static const char * spaces
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.
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
#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.
The main red black tree structure.
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
#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]
ssize_t fr_skip_condition(char const *start, char const *end, bool const terminal[static SBUFF_CHAR_CLASS], bool *eol)
Skip a conditional expression.
ssize_t fr_skip_xlat(char const *start, char const *end)
Skip an xlat expression.
#define fr_skip_whitespace(_p)
Skip whitespace ('\t', '\n', '\v', '\f', '\r', ' ')
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
fr_aka_sim_id_type_t type
size_t strlcpy(char *dst, char const *src, size_t siz)
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
#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.
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
An element in a lexicographically sorted array of name to num mappings.
An element in a lexicographically sorted array of name to ptr mappings.
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
#define talloc_strndup(_ctx, _str, _len)
static int talloc_const_free(void const *ptr)
Free const'd memory.
#define talloc_strdup(_ctx, _str)
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
const bool fr_assignment_op[T_TOKEN_LAST]
const bool fr_list_assignment_op[T_TOKEN_LAST]
fr_token_t gettoken(char const **ptr, char *buf, int buflen, bool unescape)
fr_table_num_ordered_t const fr_tokens_table[]
const bool fr_str_tok[T_TOKEN_LAST]
int getword(char const **ptr, char *buf, int buflen, bool unescape)
char const * fr_strerror(void)
Get the last library error.
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
#define fr_strerror_const(_msg)
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
#define fr_type_is_leaf(_x)
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
static fr_type_t fr_type_from_str(char const *type)
Return the constant value representing a type.
static size_t char fr_sbuff_t size_t inlen
static size_t char ** out