The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
cf_parse.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: ca0a1dc7502eac50d48b49c8c1c60e2c9611eba0 $
19 * @file cf_parse.c
20 * @brief Convert internal format configuration values into native C types.
21 *
22 * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 * @copyright 2000,2006 The FreeRADIUS server project
24 * @copyright 2000 Miquel van Smoorenburg (miquels@cistron.nl)
25 * @copyright 2000 Alan DeKok (aland@freeradius.org)
26 */
27RCSID("$Id: ca0a1dc7502eac50d48b49c8c1c60e2c9611eba0 $")
28
29#include <string.h>
30
31#include <freeradius-devel/server/cf_file.h>
32#include <freeradius-devel/server/cf_parse.h>
33#include <freeradius-devel/server/cf_priv.h>
34#include <freeradius-devel/server/log.h>
35#include <freeradius-devel/server/tmpl.h>
36#include <freeradius-devel/server/virtual_servers.h>
37#include <freeradius-devel/server/main_config.h>
38#include <freeradius-devel/util/debug.h>
39#include <freeradius-devel/util/inet.h>
40#include <freeradius-devel/util/misc.h>
41#include <freeradius-devel/util/perm.h>
42
44static char const parse_spaces[] = " ";
45
46#define PAIR_SPACE(_cs) ((_cs->depth + 1) * 2)
47#define SECTION_SPACE(_cs) (_cs->depth * 2)
48
49void cf_pair_debug_log(CONF_SECTION const *cs, CONF_PAIR *cp, conf_parser_t const *rule)
50{
51 char const *value;
52 char *tmp = NULL;
53 char const *quote = "";
54 bool secret = (rule && (rule->flags & CONF_FLAG_SECRET));
56
57 if (cp->printed) return;
58
59 /*
60 * tmpls are special, they just need to get printed as string
61 */
62 if (!rule || (rule->flags & CONF_FLAG_TMPL)) {
64 } else {
65 type = rule->type;
66 }
67
68 if (secret && (fr_debug_lvl < L_DBG_LVL_3)) {
69 cf_log_debug(cs, "%.*s%s = <<< secret >>>", PAIR_SPACE(cs), parse_spaces, cp->attr);
70 return;
71 }
72
73 /*
74 * Print the strings with the correct quotation character and escaping.
75 */
77 value = tmp = fr_asprint(NULL, cp->value, talloc_array_length(cp->value) - 1, fr_token_quote[cp->rhs_quote]);
78
79 } else {
80 value = cf_pair_value(cp);
81 }
82
84 switch (cf_pair_value_quote(cp)) {
85 default:
86 break;
87
89 quote = "\"";
90 break;
91
93 quote = "'";
94 break;
95
97 quote = "`";
98 break;
99
101 quote = "/";
102 break;
103 }
104 }
105
106 cf_log_debug(cs, "%.*s%s = %s%s%s", PAIR_SPACE(cs), parse_spaces, cp->attr, quote, value, quote);
107
108 talloc_free(tmp);
109
110 cp->printed = true;
111}
112
113/** Parses a #CONF_PAIR into a boxed value
114 *
115 * @copybrief cf_pair_value
116 * @see cf_pair_value
117 *
118 * @param[in] ctx to allocate any dynamic buffers in.
119 * @param[out] out Where to write the parsed value.
120 * @param[in] cp to parse.
121 * @param[in] rule to parse to. May contain flags.
122 * @return
123 * - 0 on success.
124 * - -1 on failure.
125 */
126int cf_pair_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *out, CONF_PAIR *cp, conf_parser_t const *rule)
127{
128 if (fr_value_box_from_str(ctx, out, rule->type, NULL, cp->value, talloc_array_length(cp->value) - 1, NULL) < 0) {
129 cf_log_perr(cp, "Invalid value \"%s\" for config item %s",
130 cp->value, cp->attr);
131
132 return -1;
133 }
134
135 /*
136 * Strings can be file paths...
137 */
138 if (fr_type_is_string(rule->type)) {
139 /*
140 * If there's out AND it's an input file, check
141 * that we can read it. This check allows errors
142 * to be caught as early as possible, during
143 * server startup.
144 */
145 if (fr_rule_file_input(rule) && !cf_file_check(cp, true)) {
146 error:
148 return -1;
149 }
150 if (fr_rule_file_exists(rule) && !cf_file_check(cp, false)) goto error;
151 }
152
154
155 return 0;
156}
157
158/** Parses a #CONF_PAIR into a C data type
159 *
160 * @copybrief cf_pair_value
161 * @see cf_pair_value
162 *
163 * @param[in] ctx to allocate any dynamic buffers in.
164 * @param[out] out Where to write the parsed value.
165 * @param[in] base address of the structure out points into.
166 * May be NULL in the case of manual parsing.
167 * @param[in] ci to parse.
168 * @param[in] rule to parse to. May contain flags.
169 * @return
170 * - 0 on success.
171 * - -1 on failure.
172 */
173int cf_pair_parse_value(TALLOC_CTX *ctx, void *out, UNUSED void *base, CONF_ITEM *ci, conf_parser_t const *rule)
174{
175 int ret = 0;
176 bool cant_be_empty, tmpl;
177
178 ssize_t slen;
179
180 CONF_PAIR *cp = cf_item_to_pair(ci);
181
182 cant_be_empty = fr_rule_not_empty(rule);
183 tmpl = fr_rule_is_tmpl(rule);
184
185 fr_assert(cp);
186 fr_assert(!fr_rule_is_attribute(rule) || tmpl); /* Attribute flag only valid for templates */
187
188 if (fr_rule_required(rule)) cant_be_empty = true; /* May want to review this in the future... */
189
190 /*
191 * Everything except templates must have a base type.
192 */
193 if (!rule->type && !tmpl) {
194 cf_log_err(cp, "Configuration pair \"%s\" must have a data type", cp->attr);
195 return -1;
196 }
197
198 /*
199 * Catch crazy errors.
200 */
201 if (!cp->value) {
202 cf_log_err(cp, "Configuration pair \"%s\" must have a value", cp->attr);
203 return -1;
204 }
205
206 /*
207 * Check for zero length strings
208 */
209 if ((cp->value[0] == '\0') && cant_be_empty) {
210 cf_log_err(cp, "Configuration pair \"%s\" must not be empty (zero length)", cp->attr);
211 if (!fr_rule_required(rule)) cf_log_err(cp, "Comment item to silence this message");
212 error:
213 ret = -1;
214 return ret;
215 }
216
217 if (tmpl) {
218 tmpl_t *vpt;
219 static tmpl_rules_t rules = {
220 .attr = {
221 .allow_unknown = true,
222 .allow_unresolved = true,
223 .allow_foreign = true,
224 },
225 .literals_safe_for = FR_VALUE_BOX_SAFE_FOR_ANY,
226 };
227 fr_sbuff_t sbuff = FR_SBUFF_IN(cp->value, strlen(cp->value));
228
230
231 /*
232 * Bare words are magical sometimes.
233 */
234 if (cp->rhs_quote == T_BARE_WORD) {
235 /*
236 * Attributes are parsed as attributes.
237 */
238 if (fr_rule_is_attribute(rule)) {
239 slen = tmpl_afrom_attr_substr(cp, NULL, &vpt, &sbuff, NULL, &rules);
240 if (slen < 0) goto tmpl_error;
241
242 fr_assert(vpt);
243
244 *(tmpl_t **)out = vpt;
245 goto finish;
246 }
247
248 /*
249 * @todo - otherwise bare words are NOT parsed as attributes, they're parsed as
250 * bare words, ala v3.
251 */
252
253 } else if (fr_rule_is_attribute(rule)) {
254 cf_log_err(cp, "Unexpected quoted string. An attribute name is required here.");
255 goto error;
256 }
257
258 slen = tmpl_afrom_substr(cp, &vpt, &sbuff, cp->rhs_quote,
260 &rules);
261 if (slen < 0) {
262 tmpl_error:
263 cf_canonicalize_error(cp, slen, fr_strerror(), cp->value);
264 goto error;
265 }
266 fr_assert(vpt);
267
268 /*
269 * The caller told us what data type was expected. If we do have data, then try to cast
270 * it to the requested type.
271 */
272 if ((rule->type != FR_TYPE_VOID) && tmpl_contains_data(vpt)) {
273 slen = 0; // for errors
274
276 tmpl_cast_set(vpt, rule->type);
277
278 if (tmpl_resolve(vpt, NULL) < 0) goto tmpl_error;
279
280 } else if (rule->type != tmpl_value_type(vpt)) {
282
283 if (tmpl_cast_in_place(vpt, rule->type, NULL) < 0) goto tmpl_error;
284 }
285 }
286
287 *(tmpl_t **)out = vpt;
288 goto finish;
289 }
290
291 /*
292 * Parse as a boxed value out of sheer laziness...
293 *
294 * Then we get all the internal types for free, and only need to add
295 * one set of printing and parsing functions for new types...
296 */
297 {
299
300 if (cf_pair_to_value_box(ctx, &vb, cf_item_to_pair(ci), rule) < 0) goto error;
301
302 if (fr_value_box_memcpy_out(out, &vb) < 0) {
303 cf_log_perr(cp, "Failed unboxing parsed configuration item value");
305 goto error;
306 }
307 }
308
309finish:
310
311 return ret;
312}
313
314/** Allocate a pair using the dflt value and quotation
315 *
316 * The pair created by this function should fed to #cf_pair_parse for parsing.
317 *
318 * @param[out] out Where to write the CONF_PAIR we created with the default value.
319 * @param[in] parent being populated.
320 * @param[in] cs to parent the CONF_PAIR from.
321 * @param[in] rule to use to create the default.
322 * @return
323 * - 0 on success.
324 * - -1 on failure.
325 */
326static int cf_pair_default(CONF_PAIR **out, void *parent, CONF_SECTION *cs, conf_parser_t const *rule)
327
328{
329 int lineno = 0;
330 char const *expanded;
331 CONF_PAIR *cp;
332 char buffer[8192];
333 fr_token_t dflt_quote = rule->quote;
334
335 fr_assert(rule->dflt || rule->dflt_func);
336
337 if (fr_rule_required(rule)) {
338 cf_log_err(cs, "Configuration pair \"%s\" must have a value", rule->name1);
339 return -1;
340 }
341
342 /*
343 * If no default quote was set, determine it from the type
344 */
345 if (dflt_quote == T_INVALID) {
346 if (fr_type_is_quoted(rule->type)) {
347 dflt_quote = T_DOUBLE_QUOTED_STRING;
348 } else {
349 dflt_quote = T_BARE_WORD;
350 }
351 }
352
353 /*
354 * Use the dynamic default function if set
355 */
356 if (rule->dflt_func) {
357 if (rule->dflt_func(out, parent, cs, dflt_quote, rule) < 0) {
358 cf_log_perr(cs, "Failed producing default for \"%s\"", rule->name1);
359 return -1;
360 }
361
362 return 0;
363 }
364
365 expanded = cf_expand_variables("<internal>", lineno, cs, buffer, sizeof(buffer), rule->dflt, -1, NULL);
366 if (!expanded) {
367 cf_log_err(cs, "Failed expanding variable %s", rule->name1);
368 return -1;
369 }
370
371 cp = cf_pair_alloc(cs, rule->name1, expanded, T_OP_EQ, T_BARE_WORD, dflt_quote);
372 if (!cp) return -1;
373
374 /*
375 * Set the ret to indicate we used a default value
376 */
377 *out = cp;
378
379 return 1;
380}
381
382static int cf_pair_unescape(CONF_PAIR *cp, conf_parser_t const *rule)
383{
384 char const *p;
385 char *str, *unescaped, *q;
386
387 if (!cp->value) return 0;
388
389 if (cp->rhs_quote != T_DOUBLE_QUOTED_STRING) return 0;
390
391 if (!(rule->flags & CONF_FLAG_TMPL)) {
392 if (rule->type != FR_TYPE_STRING) return 0;
393 }
394
395 if (strchr(cp->value, '\\') == NULL) return 0;
396
397 str = talloc_strdup(cp, cp->value);
398 if (!str) return -1;
399
400 p = cp->value;
401 q = str;
402 while (*p) {
403 unsigned int x;
404
405 if (*p != '\\') {
406 *(q++) = *(p++);
407 continue;
408 }
409
410 p++;
411 switch (*p) {
412 case 'r':
413 *q++ = '\r';
414 break;
415 case 'n':
416 *q++ = '\n';
417 break;
418 case 't':
419 *q++ = '\t';
420 break;
421
422 default:
423 if (*p >= '0' && *p <= '9' &&
424 sscanf(p, "%3o", &x) == 1) {
425 if (!x) {
426 cf_log_err(cp, "Cannot have embedded zeros in value for %s", cp->attr);
427 return -1;
428 }
429
430 *q++ = x;
431 p += 2;
432 } else {
433 *q++ = *p;
434 }
435 break;
436 }
437 p++;
438 }
439 *q = '\0';
440
441 unescaped = talloc_typed_strdup(cp, str); /* no embedded NUL */
442 if (!unescaped) return -1;
443
444 talloc_free(str);
445
446 /*
447 * Replace the old value with the new one.
448 */
450 cp->value = unescaped;
451
452 return 0;
453}
454
455/** Parses a #CONF_PAIR into a C data type, with a default value.
456 *
457 * @param[in] ctx To allocate arrays and values in.
458 * @param[out] out Where to write the result.
459 * Must not be NULL unless rule->runc is provided.
460 * @param[in] base address of the structure out points into.
461 * May be NULL in the case of manual parsing.
462 * @param[in] cs to search for matching #CONF_PAIR in.
463 * @param[in] rule to parse #CONF_PAIR with.
464 * @return
465 * - 1 if default value was used, or if there was no CONF_PAIR or dflt.
466 * - 0 on success.
467 * - -1 on error.
468 * - -2 if deprecated.
469 */
470static int CC_HINT(nonnull(4,5)) cf_pair_parse_internal(TALLOC_CTX *ctx, void *out, void *base,
471 CONF_SECTION *cs, conf_parser_t const *rule)
472{
473 bool required, deprecated;
474 size_t count = 0;
475 CONF_PAIR *cp = NULL, *dflt_cp = NULL;
476
477#ifndef NDEBUG
478 char const *dflt = rule->dflt;
479 fr_token_t dflt_quote = rule->quote;
480#endif
481 cf_parse_t func = rule->func ? rule->func : cf_pair_parse_value;
482
483 fr_assert(!fr_rule_is_tmpl(rule) || !dflt || (dflt_quote != T_INVALID)); /* We ALWAYS need a quoting type for templates */
484
485 /*
486 * Functions don't necessarily *need* to write
487 * anywhere, so their data pointer can be NULL.
488 */
489 if (!out) {
490 if (!rule->func) {
491 cf_log_err(cs, "Rule doesn't specify output destination");
492 return -1;
493 }
494 }
495
496 required = fr_rule_required(rule);
497 deprecated = fr_rule_deprecated(rule);
498
499 /*
500 * If the item is multi-valued we allocate an array
501 * to hold the multiple values.
502 */
503 if (fr_rule_multi(rule)) {
504 void **array;
505 size_t i = 0;
506
507 /*
508 * Easier than re-allocing
509 */
510 count = cf_pair_count(cs, rule->name1);
511
512 /*
513 * Multivalued, but there's no value, create a
514 * default pair.
515 */
516 if (!count) {
517 if (deprecated) return 0;
518
519 if (!fr_rule_dflt(rule)) {
520 if (required) {
521 need_value:
522 cf_log_err(cs, "Configuration item \"%s\" must have a value", rule->name1);
523 return -1;
524 }
525 return 1;
526 }
527
528 if (cf_pair_default(&dflt_cp, base, cs, rule) < 0) return -1;
529 count = cf_pair_count(cs, rule->name1); /* Dynamic functions can add multiple defaults */
530 if (!count) {
531 if (fr_rule_not_empty(rule)) {
532 cf_log_err(cs, "Configuration item \"%s\" cannot be empty", rule->name1);
533 return -1;
534 }
535 return 0;
536 }
537 }
538
539 if (deprecated) {
540 /*
541 * Emit the deprecated warning in the
542 * context of the first pair.
543 */
544 cp = cf_pair_find(cs, rule->name1);
545 fr_assert(cp);
546
547 deprecated:
548 cf_log_err(cp, "Configuration pair \"%s\" is deprecated", cp->attr);
549 return -2;
550 }
551
552 /*
553 * No output, so don't bother allocing the array
554 */
555 if (!out) {
556 array = NULL;
557
558 /*
559 * Tmpl is outside normal range
560 */
561 } else if (fr_rule_is_tmpl(rule)) {
562 MEM(array = (void **)talloc_zero_array(ctx, tmpl_t *, count));
563
564 /*
565 * Allocate an array of values.
566 *
567 * We don't NULL terminate. Consumer must use
568 * talloc_array_length().
569 */
570 } else {
571 array = fr_type_array_alloc(ctx, rule->type, count);
572 if (unlikely(array == NULL)) {
573 cf_log_perr(cp, "Failed allocating value array");
574 return -1;
575 }
576 }
577
578 while ((cp = cf_pair_find_next(cs, cp, rule->name1))) {
579 int ret;
580 void *entry;
581 TALLOC_CTX *value_ctx = array;
582
583 /*
584 * Figure out where to write the output
585 */
586 if (!array) {
587 entry = NULL;
588 } else if ((rule->type == FR_TYPE_VOID) || (rule->flags & CONF_FLAG_TMPL)) {
589 entry = &array[i++];
590 } else {
591 entry = ((uint8_t *) array) + (i++ * fr_value_box_field_sizes[rule->type]);
592 }
593
594 if (cf_pair_unescape(cp, rule) < 0) return -1;
595
596 /*
597 * Switch between custom parsing function
598 * and the standard value parsing function.
599 */
600 cf_pair_debug_log(cs, cp, rule);
601
602 if (cf_pair_is_parsed(cp)) continue;
603 ret = func(value_ctx, entry, base, cf_pair_to_item(cp), rule);
604 if (ret < 0) {
605 talloc_free(array);
606 return -1;
607 }
609 }
610 if (array) *(void **)out = array;
611 /*
612 * Single valued config item gets written to
613 * the data pointer directly.
614 */
615 } else {
616 CONF_PAIR *next;
617 int ret;
618
619 cp = cf_pair_find(cs, rule->name1);
620 if (!cp) {
621 if (deprecated) return 0;
622
623 if (!fr_rule_dflt(rule)) {
624 if (required) goto need_value;
625 return 1;
626 }
627
628 if (cf_pair_default(&dflt_cp, base, cs, rule) < 0) return -1;
629 cp = dflt_cp;
630 if (!cp) {
631 if (fr_rule_not_empty(rule)) {
632 cf_log_err(cs, "Configuration item \"%s\" cannot be empty", rule->name1);
633 return -1;
634 }
635
636 return 0;
637 }
638 } else {
639 if (cf_pair_unescape(cp, rule) < 0) return -1;
640 }
641
642 next = cf_pair_find_next(cs, cp, rule->name1);
643 if (next) {
644 cf_log_err(cf_pair_to_item(next), "Invalid duplicate configuration item '%s'", rule->name1);
645 return -1;
646 }
647 if (deprecated) goto deprecated;
648
649 cf_pair_debug_log(cs, cp, rule);
650
651 if (cf_pair_is_parsed(cp)) return 0;
652 ret = func(ctx, out, base, cf_pair_to_item(cp), rule);
653 if (ret < 0) return -1;
655 }
656
657 return 0;
658}
659
660/** Parses a #CONF_PAIR into a C data type, with a default value.
661 *
662 * Takes fields from a #conf_parser_t struct and uses them to parse the string value
663 * of a #CONF_PAIR into a C data type matching the type argument.
664 *
665 * The format of the types are the same as #fr_value_box_t types.
666 *
667 * @note The dflt value will only be used if no matching #CONF_PAIR is found. Empty strings will not
668 * result in the dflt value being used.
669 *
670 * **fr_type_t to data type mappings**
671 * | fr_type_t | Data type | Dynamically allocated |
672 * | ----------------------- | ------------------ | ---------------------- |
673 * | FR_TYPE_BOOL | ``bool`` | No |
674 * | FR_TYPE_UINT32 | ``uint32_t`` | No |
675 * | FR_TYPE_UINT16 | ``uint16_t`` | No |
676 * | FR_TYPE_UINT64 | ``uint64_t`` | No |
677 * | FR_TYPE_INT32 | ``int32_t`` | No |
678 * | FR_TYPE_STRING | ``char const *`` | Yes |
679 * | FR_TYPE_IPV4_ADDR | ``fr_ipaddr_t`` | No |
680 * | FR_TYPE_IPV4_PREFIX | ``fr_ipaddr_t`` | No |
681 * | FR_TYPE_IPV6_ADDR | ``fr_ipaddr_t`` | No |
682 * | FR_TYPE_IPV6_PREFIX | ``fr_ipaddr_t`` | No |
683 * | FR_TYPE_COMBO_IP_ADDR | ``fr_ipaddr_t`` | No |
684 * | FR_TYPE_COMBO_IP_PREFIX | ``fr_ipaddr_t`` | No |
685 * | FR_TYPE_TIME_DELTA | ``fr_time_delta_t``| No |
686 *
687 * @param[in] ctx To allocate arrays and values in.
688 * @param[in] cs to search for matching #CONF_PAIR in.
689 * @param[in] name of #CONF_PAIR to search for.
690 * @param[in] type Data type to parse #CONF_PAIR value as.
691 * Should be one of the following ``data`` types,
692 * and one or more of the following ``flag`` types or'd together:
693
694 * - ``data`` #FR_TYPE_BOOL - @copybrief FR_TYPE_BOOL
695 * - ``data`` #FR_TYPE_UINT32 - @copybrief FR_TYPE_UINT32
696 * - ``data`` #FR_TYPE_UINT16 - @copybrief FR_TYPE_UINT16
697 * - ``data`` #FR_TYPE_UINT64 - @copybrief FR_TYPE_UINT64
698 * - ``data`` #FR_TYPE_INT32 - @copybrief FR_TYPE_INT32
699 * - ``data`` #FR_TYPE_STRING - @copybrief FR_TYPE_STRING
700 * - ``data`` #FR_TYPE_IPV4_ADDR - @copybrief FR_TYPE_IPV4_ADDR (IPv4 address with prefix 32).
701 * - ``data`` #FR_TYPE_IPV4_PREFIX - @copybrief FR_TYPE_IPV4_PREFIX (IPv4 address with variable prefix).
702 * - ``data`` #FR_TYPE_IPV6_ADDR - @copybrief FR_TYPE_IPV6_ADDR (IPv6 address with prefix 128).
703 * - ``data`` #FR_TYPE_IPV6_PREFIX - @copybrief FR_TYPE_IPV6_PREFIX (IPv6 address with variable prefix).
704 * - ``data`` #FR_TYPE_COMBO_IP_ADDR - @copybrief FR_TYPE_COMBO_IP_ADDR (IPv4/IPv6 address with
705 * prefix 32/128).
706 * - ``data`` #FR_TYPE_COMBO_IP_PREFIX - @copybrief FR_TYPE_COMBO_IP_PREFIX (IPv4/IPv6 address with
707 * variable prefix).
708 * - ``data`` #FR_TYPE_TIME_DELTA - @copybrief FR_TYPE_TIME_DELTA
709 * - ``flag`` #CONF_FLAG_TMPL - @copybrief CONF_FLAG_TMPL
710 * Feeds the value into #tmpl_afrom_substr. Value can be
711 * obtained when processing requests, with #tmpl_expand or #tmpl_aexpand.
712 * - ``flag`` #FR_TYPE_DEPRECATED - @copybrief FR_TYPE_DEPRECATED
713 * - ``flag`` #CONF_FLAG_REQUIRED - @copybrief CONF_FLAG_REQUIRED
714 * - ``flag`` #CONF_FLAG_ATTRIBUTE - @copybrief CONF_FLAG_ATTRIBUTE
715 * - ``flag`` #CONF_FLAG_SECRET - @copybrief CONF_FLAG_SECRET
716 * - ``flag`` #CONF_FLAG_FILE_INPUT - @copybrief CONF_FLAG_FILE_INPUT
717 * - ``flag`` #CONF_FLAG_FILE_OUTPUT - @copybrief CONF_FLAG_FILE_OUTPUT
718 * - ``flag`` #CONF_FLAG_NOT_EMPTY - @copybrief CONF_FLAG_NOT_EMPTY
719 * - ``flag`` #CONF_FLAG_MULTI - @copybrief CONF_FLAG_MULTI
720 * - ``flag`` #CONF_FLAG_IS_SET - @copybrief CONF_FLAG_IS_SET
721 * @param[out] data Pointer to a global variable, or pointer to a field in the struct being populated with values.
722 * @param[in] dflt value to use, if no #CONF_PAIR is found.
723 * @param[in] dflt_quote around the dflt value.
724 * @return
725 * - 1 if default value was used, or if there was no CONF_PAIR or dflt.
726 * - 0 on success.
727 * - -1 on error.
728 * - -2 if deprecated.
729 */
730int cf_pair_parse(TALLOC_CTX *ctx, CONF_SECTION *cs, char const *name,
731 unsigned int type, void *data, char const *dflt, fr_token_t dflt_quote)
732{
733 conf_parser_t rule = {
734 .name1 = name,
735 .type = type,
736 .dflt = dflt,
737 .quote = dflt_quote
738 };
739
740 return cf_pair_parse_internal(ctx, data, NULL, cs, &rule);
741}
742
743/** Pre-allocate a config section structure to allow defaults to be set
744 *
745 * @param cs The parent subsection.
746 * @param base pointer or variable.
747 * @param rule that may have defaults in this config section.
748 * @return
749 * - 0 on success.
750 * - -1 on failure.
751 */
752static int cf_section_parse_init(CONF_SECTION *cs, void *base, conf_parser_t const *rule)
753{
754 CONF_PAIR *cp;
755
756 /*
757 * This rule refers to a named subsection
758 */
759 if ((rule->flags & CONF_FLAG_SUBSECTION)) {
760 char const *name2 = NULL;
761 CONF_SECTION *subcs;
762
763 /*
764 * Optional MUST be listed before required ones
765 */
766 if ((rule->flags & CONF_FLAG_OPTIONAL) != 0) {
767 return 0;
768 }
769
770 subcs = cf_section_find(cs, rule->name1, rule->name2);
771
772 /*
773 * Set the is_set field for the subsection.
774 */
775 if (rule->flags & CONF_FLAG_IS_SET) {
776 bool *is_set;
777
778 is_set = rule->data ? rule->is_set_ptr : ((uint8_t *)base) + rule->is_set_offset;
779 if (is_set) *is_set = (subcs != NULL);
780 }
781
782 /*
783 * It exists, we don't have to do anything else.
784 */
785 if (subcs) return 0;
786
787 /*
788 * If there is no subsection, either complain,
789 * allow it, or create it with default values.
790 */
791 if (rule->flags & CONF_FLAG_REQUIRED) {
792 cf_log_err(cs, "Missing %s {} subsection", rule->name1);
793 return -1;
794 }
795
796 /*
797 * It's OK for this to be missing. Don't
798 * initialize it.
799 */
800 if ((rule->flags & CONF_FLAG_OK_MISSING) != 0) return 0;
801
802 /*
803 * If there's no subsection in the
804 * config, BUT the conf_parser_t wants one,
805 * then create an empty one. This is so
806 * that we can track the strings,
807 * etc. allocated in the subsection.
808 */
809 if (DEBUG_ENABLED4) cf_log_debug(cs, "Allocating fake section \"%s\"", rule->name1);
810
811 /*
812 * If name1 is CF_IDENT_ANY, then don't
813 * alloc the section as we have no idea
814 * what it should be called.
815 */
816 if (rule->name1 == CF_IDENT_ANY) return 0;
817
818 /*
819 * Don't specify name2 if it's CF_IDENT_ANY
820 */
821 if (rule->name2 != CF_IDENT_ANY) name2 = rule->name2;
822 subcs = cf_section_alloc(cs, cs, rule->name1, name2);
823 if (!subcs) return -1;
824
825 return 0;
826 }
827
828 /*
829 * This rule refers to another conf_parse_t which is included in-line in
830 * this section.
831 */
832 if ((rule->flags & CONF_FLAG_REF) != 0) {
833 conf_parser_t const *rule_p;
834 uint8_t *sub_base = base;
835
836 fr_assert(rule->subcs != NULL);
837
838 sub_base += rule->offset;
839
840 for (rule_p = rule->subcs; rule_p->name1; rule_p++) {
841 int ret = cf_section_parse_init(cs, sub_base, rule_p);
842 if (ret < 0) return ret;
843 }
844 return 0;
845 }
846
847 /*
848 * Don't re-initialize data which was already parsed.
849 */
850 cp = cf_pair_find(cs, rule->name1);
851 if (cp && cp->parsed) return 0;
852
853 if ((rule->type != FR_TYPE_STRING) &&
854 (!(rule->flags & CONF_FLAG_FILE_INPUT)) &&
855 (!(rule->flags & CONF_FLAG_FILE_OUTPUT))) {
856 return 0;
857 }
858
859 if (rule->data) {
860 *(char **) rule->data = NULL;
861 } else if (base) {
862 *(char **) (((char *)base) + rule->offset) = NULL;
863 } else {
864 return 0;
865 }
866
867 return 0;
868}
869
871{
872 cf_item_foreach(&cs->item, ci) {
873 /*
874 * Don't recurse on sections. We can only safely
875 * check conf pairs at the same level as the
876 * section that was just parsed.
877 */
878 if (ci->type == CONF_ITEM_SECTION) continue;
879 if (ci->type == CONF_ITEM_PAIR) {
880 CONF_PAIR *cp;
881
882 cp = cf_item_to_pair(ci);
883 if (cp->parsed || cp->referenced || (ci->lineno < 0)) continue;
884
885 WARN("%s[%d]: The item '%s' is defined, but is unused by the configuration",
886 ci->filename, ci->lineno,
887 cp->attr);
888 }
889
890 /*
891 * Skip everything else.
892 */
893 }
894}
895
896/** Parse a subsection
897 *
898 * @note Turns out using nested structures (instead of pointers) for subsections, was actually
899 * a pretty bad design decision, and will need to be fixed at some future point.
900 * For now we have a horrible hack where only multi-subsections get an array of structures
901 * of the appropriate size.
902 *
903 * @param[in] ctx to allocate any additional structures under.
904 * @param[out] out pointer to a struct/pointer to fill with data.
905 * @param[in] base address of the structure out points into.
906 * May be NULL in the case of manual parsing.
907 * @param[in] cs to parse.
908 * @param[in] rule to parse the subcs with.
909 * @return
910 * - 0 on success.
911 * - -1 on general error.
912 * - -2 if a deprecated #CONF_ITEM was found.
913 */
914static int cf_subsection_parse(TALLOC_CTX *ctx, void *out, void *base, CONF_SECTION *cs, conf_parser_t const *rule)
915{
916 CONF_SECTION *subcs = NULL;
917 int count = 0, i = 0, ret;
918
919 size_t subcs_size = rule->subcs_size;
920 conf_parser_t const *rules = rule->subcs;
921
922 uint8_t **array = NULL;
923
925
926 subcs = cf_section_find(cs, rule->name1, rule->name2);
927 if (!subcs) return 0;
928
929 /*
930 * Handle the single subsection case (which is simple)
931 */
932 if (!(rule->flags & CONF_FLAG_MULTI)) {
933 uint8_t *buff = NULL;
934
935 if (DEBUG_ENABLED4) cf_log_debug(cs, "Evaluating rules for %s section. Output %p",
936 cf_section_name1(subcs), out);
937
938 /*
939 * Add any rules, so the func can just call cf_section_parse
940 * if it wants to continue after doing its stuff.
941 */
942 if (cf_section_rules_push(subcs, rules) < 0) return -1;
943 if (rule->func) return rule->func(ctx, out, base, cf_section_to_item(subcs), rule);
944
945 /*
946 * FIXME: We shouldn't allow nested structures like this.
947 * Each subsection struct should be allocated separately so
948 * we have a clean talloc hierarchy.
949 */
950 if (!subcs_size) return cf_section_parse(ctx, out, subcs);
951
952 if (out) {
953 MEM(buff = talloc_zero_array(ctx, uint8_t, subcs_size));
954 if (rule->subcs_type) talloc_set_name_const(buff, rule->subcs_type);
955 }
956
957 ret = cf_section_parse(buff, buff, subcs);
958 if (ret < 0) {
960 return ret;
961 }
962
963 if (out) *((uint8_t **)out) = buff;
964
965 return 0;
966 }
967
968 fr_assert(subcs_size);
969
970 /*
971 * Handle the multi subsection case (which is harder)
972 */
973 subcs = NULL;
974 while ((subcs = cf_section_find_next(cs, subcs, rule->name1, rule->name2))) count++;
975
976 /*
977 * Allocate an array to hold the subsections
978 */
979 if (out) {
980 MEM(array = talloc_zero_array(ctx, uint8_t *, count));
981 if (rule->subcs_type) talloc_set_name(array, "%s *", rule->subcs_type);
982 }
983 /*
984 * Start parsing...
985 *
986 * Note, we allocate each subsection structure individually
987 * so that they can be used as talloc contexts and we can
988 * keep the talloc hierarchy clean.
989 */
990 subcs = NULL;
991 while ((subcs = cf_section_find_next(cs, subcs, rule->name1, rule->name2))) {
992 uint8_t *buff = NULL;
993
994 if (DEBUG_ENABLED4) cf_log_debug(cs, "Evaluating rules for %s[%i] section. Output %p",
995 cf_section_name1(subcs),
996 i, out);
997
998 if (array) {
999 MEM(buff = talloc_zero_array(array, uint8_t, subcs_size));
1000 if (rule->subcs_type) talloc_set_name_const(buff, rule->subcs_type);
1001 array[i++] = buff;
1002 }
1003
1004 /*
1005 * Add any rules, so the func can just call cf_section_parse
1006 * if it wants to continue after doing its stuff.
1007 */
1008 if (cf_section_rules_push(subcs, rules) < 0) {
1009 talloc_free(array);
1010 return -1;
1011 }
1012 if (rule->func) {
1013 ret = rule->func(ctx, buff, base, cf_section_to_item(subcs), rule);
1014 if (ret < 0) {
1015 talloc_free(array);
1016 return ret;
1017 }
1018 continue;
1019 }
1020
1021 ret = cf_section_parse(buff, buff, subcs);
1022 if (ret < 0) {
1023 talloc_free(array);
1024 return ret;
1025 }
1026 }
1027
1028 if (out) *((uint8_t ***)out) = array;
1029
1030 return 0;
1031}
1032
1033static int cf_section_parse_rule(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs, conf_parser_t const *rule)
1034{
1035 int ret;
1036 bool *is_set = NULL;
1037 void *data = NULL;
1038
1039 /*
1040 * Ignore ON_READ parse rules if there's no subsequent
1041 * parse functions.
1042 */
1043 if (!rule->func && rule->on_read) return 0;
1044
1045 /*
1046 * Pre-allocate the config structure to hold default values
1047 */
1048 if (cf_section_parse_init(cs, base, rule) < 0) return -1;
1049
1050 if (rule->data) {
1051 data = rule->data; /* prefer this. */
1052 } else if (base) {
1053 data = ((uint8_t *)base) + rule->offset;
1054 }
1055
1056 /*
1057 * Handle subsections specially
1058 */
1059 if (rule->flags & CONF_FLAG_SUBSECTION) {
1060 return cf_subsection_parse(ctx, data, base, cs, rule);
1061 }
1062
1063 /*
1064 * Ignore this rule if it's a reference, as the
1065 * rules it points to have been pushed by the
1066 * above function.
1067 */
1068 if ((rule->flags & CONF_FLAG_REF) != 0) {
1069 conf_parser_t const *rule_p;
1070 uint8_t *sub_base = base;
1071
1072 fr_assert(rule->subcs != NULL);
1073
1074 sub_base += rule->offset;
1075
1076 for (rule_p = rule->subcs; rule_p->name1; rule_p++) {
1077 if (rule_p->flags & CONF_FLAG_DEPRECATED) continue; /* Skip deprecated */
1078
1079 ret = cf_section_parse_rule(ctx, sub_base, cs, rule_p);
1080 if (ret < 0) return ret;
1081 }
1082
1083 /*
1084 * Ensure we have a proper terminator, type so we catch
1085 * missing terminators reliably
1086 */
1087 fr_cond_assert(rule_p->type == conf_term.type);
1088
1089 return 0;
1090 }
1091
1092 /*
1093 * Else it's a CONF_PAIR
1094 */
1095
1096 /*
1097 * Pair either needs an output destination or
1098 * there needs to be a function associated with
1099 * it.
1100 */
1101 if (!data && !rule->func) {
1102 cf_log_err(cs, "Rule doesn't specify output destination");
1103 return -1;
1104 }
1105
1106 /*
1107 * Get pointer to where we need to write out
1108 * whether the pointer was set.
1109 */
1110 if (rule->flags & CONF_FLAG_IS_SET) {
1111 is_set = rule->data ? rule->is_set_ptr : ((uint8_t *)base) + rule->is_set_offset;
1112 }
1113
1114 /*
1115 * Parse the pair we found, or a default value.
1116 */
1117 ret = cf_pair_parse_internal(ctx, data, base, cs, rule);
1118 switch (ret) {
1119 case 1: /* Used default (or not present) */
1120 if (is_set) *is_set = false;
1121 ret = 0;
1122 break;
1123
1124 case 0: /* OK */
1125 if (is_set) *is_set = true;
1126 break;
1127
1128 case -1: /* Parse error */
1129 break;
1130
1131 case -2: /* Deprecated CONF ITEM */
1132 if (((rule + 1)->offset && ((rule + 1)->offset == rule->offset)) ||
1133 ((rule + 1)->data && ((rule + 1)->data == rule->data))) {
1134 cf_log_err(cs, "Replace \"%s\" with \"%s\"", rule->name1,
1135 (rule + 1)->name1);
1136 }
1137 break;
1138 }
1139
1140 return ret;
1141}
1142
1143/** Parse a configuration section into user-supplied variables
1144 *
1145 * @param[in] ctx to allocate any strings, or additional structures in.
1146 * Usually the same as base, unless base is a nested struct.
1147 * @param[out] base pointer to a struct to fill with data.
1148 * @param[in] cs to parse.
1149 * @return
1150 * - 0 on success.
1151 * - -1 on general error.
1152 * - -2 if a deprecated #CONF_ITEM was found.
1153 */
1154int cf_section_parse(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs)
1155{
1156 CONF_DATA const *rule_cd = NULL;
1157
1158 if (!cs->name2) {
1159 cf_log_debug(cs, "%.*s%s {", SECTION_SPACE(cs), parse_spaces, cs->name1);
1160 } else {
1161 cf_log_debug(cs, "%.*s%s %s {", SECTION_SPACE(cs), parse_spaces, cs->name1, cs->name2);
1162 }
1163
1164 /*
1165 * Loop over all the child rules of the section
1166 */
1167 while ((rule_cd = cf_data_find_next(cs, rule_cd, conf_parser_t, CF_IDENT_ANY))) {
1168 int ret;
1169 conf_parser_t *rule;
1170
1171 rule = cf_data_value(rule_cd);
1172
1173 ret = cf_section_parse_rule(ctx, base, cs, rule);
1174 if (ret < 0) return ret;
1175 }
1176
1177 cs->base = base;
1178
1179 /*
1180 * Warn about items in the configuration which weren't
1181 * checked during parsing.
1182 */
1184
1185 cf_log_debug(cs, "%.*s}", SECTION_SPACE(cs), parse_spaces);
1186
1187 return 0;
1188}
1189
1190/*
1191 * Pass2 fixups on tmpl_t
1192 *
1193 * We don't have (or need yet) cf_pair_parse_pass2(), so we just
1194 * do it for tmpls.
1195 */
1197 bool attribute, fr_dict_t const *dict_def)
1198{
1199 tmpl_t *vpt = *out;
1200
1201 fr_assert(vpt); /* We need something to resolve */
1202
1203 if (tmpl_resolve(vpt, &(tmpl_res_rules_t){ .dict_def = dict_def, .force_dict_def = (dict_def != NULL)}) < 0) {
1204 cf_log_perr(cp, "Failed processing configuration item '%s'", cp->attr);
1205 return -1;
1206 }
1207
1208 if (attribute) {
1209 if (!tmpl_is_attr(vpt)) {
1210 cf_log_err(cp, "Expected attr got %s",
1211 tmpl_type_to_str(vpt->type));
1212 return -1;
1213 }
1214 }
1215
1216 switch (vpt->type) {
1217 /*
1218 * All attributes should have been defined by this point.
1219 */
1221 cf_log_err(cp, "Unknown attribute '%s'", tmpl_attr_tail_unresolved(vpt));
1222 return -1;
1223
1225 /*
1226 * Try to realize the underlying type, if at all possible.
1227 */
1228 if (!attribute && type && (tmpl_cast_in_place(vpt, type, NULL) < 0)) {
1229 cf_log_perr(cp, "Failed processing configuration item '%s'", cp->attr);
1230 return -1;
1231 }
1232 break;
1233
1234 case TMPL_TYPE_ATTR:
1235 if (!check_config) break;
1236
1237 if (vpt->name[0] != '&') break;
1238
1239 if (main_config_migrate_option_get("call_env_forbid_ampersand")) {
1240 cf_log_err(cp, "Please remove '&' from the attribute name");
1241 return -1;
1242 }
1243 cf_log_warn(cp, "Please remove '&' from the attribute name");
1244 break;
1245
1246 case TMPL_TYPE_DATA:
1247 case TMPL_TYPE_EXEC:
1249 case TMPL_TYPE_XLAT:
1251 break;
1252
1254 case TMPL_TYPE_REGEX:
1258 case TMPL_TYPE_MAX:
1259 fr_assert(0);
1260 /* Don't add default */
1261 }
1262
1263 return 0;
1264}
1265
1266/** Fixup xlat expansions and attributes
1267 *
1268 * @param[out] base start of structure to write #tmpl_t s to.
1269 * @param[in] cs CONF_SECTION to fixup.
1270 * @return
1271 * - 0 on success.
1272 * - -1 on failure (parse errors etc...).
1273 */
1275{
1276 CONF_DATA const *rule_cd = NULL;
1277
1278 while ((rule_cd = cf_data_find_next(cs, rule_cd, conf_parser_t, CF_IDENT_ANY))) {
1279 bool attribute, multi, is_tmpl, is_xlat;
1280 CONF_PAIR *cp;
1281 conf_parser_t *rule = cf_data_value(rule_cd);
1282 void *data;
1283 fr_type_t type = rule->type;
1284 conf_parser_flags_t flags = rule->flags;
1285 fr_dict_t const *dict = NULL;
1286
1287 is_tmpl = (flags & CONF_FLAG_TMPL);
1288 is_xlat = (flags & CONF_FLAG_XLAT);
1289 attribute = (flags & CONF_FLAG_ATTRIBUTE);
1290 multi = (flags & CONF_FLAG_MULTI);
1291
1292 /*
1293 * It's a section, recurse!
1294 */
1295 if (flags & CONF_FLAG_SUBSECTION) {
1296 uint8_t *subcs_base;
1297 CONF_SECTION *subcs = cf_section_find(cs, rule->name1, rule->name2);
1298
1299 /*
1300 * Select base by whether this is a nested struct,
1301 * or a pointer to another struct.
1302 */
1303 if (!base) {
1304 subcs_base = NULL;
1305 } else if (multi) {
1306 size_t j, len;
1307 uint8_t **array;
1308
1309 array = *(uint8_t ***)(((uint8_t *)base) + rule->offset);
1310 len = talloc_array_length(array);
1311
1312 for (j = 0; j < len; j++) if (cf_section_parse_pass2(array[j], subcs) < 0) return -1;
1313 continue;
1314 } else {
1315 subcs_base = (uint8_t *)base + rule->offset;
1316 }
1317
1318 if (cf_section_parse_pass2(subcs_base, subcs) < 0) return -1;
1319
1320 continue;
1321 }
1322
1323 /*
1324 * Find the CONF_PAIR, may still not exist if there was
1325 * no default set for the conf_parser_t.
1326 */
1327 cp = cf_pair_find(cs, rule->name1);
1328 if (!cp) continue;
1329
1330 /*
1331 * Figure out which data we need to fix.
1332 */
1333 data = rule->data; /* prefer this. */
1334 if (!data && base) data = ((char *)base) + rule->offset;
1335 if (!data) continue;
1336
1337 /*
1338 * Non-xlat expansions shouldn't have xlat!
1339 *
1340 * Except other libraries like libkafka may be the ones
1341 * doing the actual expansion, so we don't _know_
1342 * if the xlatlike value is destined for use in FreeRADIUS
1343 * or not, so we can't definitely determine if this is an
1344 * error.
1345 *
1346 * Code left in place to warn other people off re-adding
1347 * this check in future.
1348 */
1349#if 0
1350 if (!is_xlat && !is_tmpl) {
1351 /*
1352 * Ignore %{... in shared secrets.
1353 * They're never dynamically expanded.
1354 */
1355 if ((rule->flags & CONF_FLAG_SECRET) != 0) continue;
1356
1357 if (strstr(cp->value, "%{") != NULL) {
1358 cf_log_err(cp, "Found dynamic expansion in string which "
1359 "will not be dynamically expanded");
1360 return -1;
1361 }
1362 continue;
1363 }
1364#endif
1365
1366 /*
1367 * Search for dictionary data somewhere in the virtual
1368 * server.
1369 */
1371
1372 /*
1373 * Parse (and throw away) the xlat string (for validation).
1374 *
1375 * FIXME: All of these should be converted from CONF_FLAG_XLAT
1376 * to CONF_FLAG_TMPL.
1377 */
1378 if (is_xlat) {
1379 ssize_t slen;
1380 xlat_exp_head_t *xlat;
1381
1382 redo:
1383 xlat = NULL;
1384
1385 /*
1386 * xlat expansions should be parseable.
1387 */
1388 slen = xlat_tokenize(cs, &xlat,
1389 &FR_SBUFF_IN(cp->value, talloc_array_length(cp->value) - 1), NULL,
1390 &(tmpl_rules_t) {
1391 .attr = {
1392 .dict_def = dict,
1393 .list_def = request_attr_request,
1394 .allow_unknown = false,
1395 .allow_unresolved = false,
1396 .allow_foreign = (dict == NULL)
1397 },
1398 });
1399 if (slen < 0) {
1400 char *spaces, *text;
1401
1402 fr_canonicalize_error(cs, &spaces, &text, slen, cp->value);
1403
1404 cf_log_err(cp, "Failed parsing expansion string:");
1405 cf_log_err(cp, "%s", text);
1406 cf_log_perr(cp, "%s^", spaces);
1407
1409 talloc_free(text);
1410 talloc_free(xlat);
1411 return -1;
1412 }
1413
1414 talloc_free(xlat);
1415
1416 /*
1417 * If the "multi" flag is set, check all of them.
1418 */
1419 if (multi) {
1420 cp = cf_pair_find_next(cs, cp, cp->attr);
1421 if (cp) goto redo;
1422 }
1423 continue;
1424
1425 /*
1426 * Parse the pair into a template
1427 */
1428 } else if (is_tmpl && !multi) {
1429 if (cf_parse_tmpl_pass2(cs, (tmpl_t **)data, cp, type, attribute, dict) < 0) {
1430 return -1;
1431 }
1432
1433 } else if (is_tmpl) {
1434 size_t i;
1435 char const *name = cp->attr;
1436 tmpl_t **array = *(tmpl_t ***) data;
1437
1438 for (i = 0; i < talloc_array_length(array); i++, cp = cf_pair_find_next(cs, cp, name)) {
1439 if (!cp) break;
1440
1441 if (cf_parse_tmpl_pass2(cs, &array[i], cp, type, attribute, dict) < 0) {
1442 return -1;
1443 }
1444 }
1445 }
1446 }
1447
1448 return 0;
1449}
1450
1451
1452/** Add a single rule to a #CONF_SECTION
1453 *
1454 * @param[in] cs to add rules to.
1455 * @param[in] rule to add.
1456 * @param[in] filename where the rule was pushed.
1457 * @param[in] lineno where the rule was pushed.
1458 * @return
1459 * - 0 on success.
1460 * - -1 if the rules added conflict.
1461 */
1462int _cf_section_rule_push(CONF_SECTION *cs, conf_parser_t const *rule, char const *filename, int lineno)
1463{
1464 char const *name1, *name2;
1465
1466 if (!cs || !rule) return 0;
1467
1468 name1 = rule->name1 == CF_IDENT_ANY ? "__any__" : rule->name1;
1469 name2 = rule->name2 == CF_IDENT_ANY ? "__any__" : rule->name2;
1470
1471 if (DEBUG_ENABLED4) {
1472 cf_log_debug(cs, "Pushed parse rule to %s section: %s %s",
1473 cf_section_name1(cs),
1474 name1, rule->flags & CONF_FLAG_SUBSECTION ? "{}": "");
1475 }
1476
1477 /*
1478 * Qualifying with name prevents duplicate rules being added
1479 *
1480 * Fixme maybe?.. Can't have a section and pair with the same name.
1481 */
1482 if (!_cf_data_add_static(CF_TO_ITEM(cs), rule, "conf_parser_t", name1, filename, lineno)) {
1483 CONF_DATA const *cd;
1484 conf_parser_t *old;
1485
1486 cd = cf_data_find(CF_TO_ITEM(cs), conf_parser_t, name1);
1487 old = cf_data_value(cd);
1488 fr_assert(old != NULL);
1489
1490 /*
1491 * Shut up about duplicates.
1492 */
1493 if (memcmp(rule, old, sizeof(*rule)) == 0) {
1494 return 0;
1495 }
1496
1497 /*
1498 * Remove any ON_READ callbacks, and add the new
1499 * rule in its place.
1500 */
1501 if (old->on_read) {
1502 CONF_DATA *cd1;
1503
1504 /*
1505 * Over-write the rule in place.
1506 *
1507 * We'd like to call cf_item_remove(), but
1508 * that apparently doesn't work for
1509 * CONF_DATA. We don't need to
1510 * free/alloc one, so re-using this is
1511 * fine.
1512 */
1513 memcpy(&cd1, &cd, sizeof(cd1));
1514 cd1->data = rule;
1515 cd1->item.filename = filename;
1516 cd1->item.lineno = lineno;
1517 return 0;
1518 }
1519
1520 /*
1521 * If we have a duplicate sub-section, just
1522 * recurse and add the new sub-rules to the
1523 * existing sub-section.
1524 */
1525 if (rule->flags & CONF_FLAG_SUBSECTION) {
1526 CONF_SECTION *subcs;
1527
1528 subcs = cf_section_find(cs, name1, name2);
1529 if (!subcs) {
1530 cf_log_err(cs, "Failed finding '%s' subsection", name1);
1531 cf_item_debug(cs);
1532 return -1;
1533 }
1534
1535 /*
1536 * The old rules were delayed until we pushed a matching subsection which is actually used.
1537 */
1538 if ((old->flags & CONF_FLAG_OPTIONAL) != 0) {
1539 if (cf_section_rules_push(subcs, old->subcs) < 0) return -1;
1540 }
1541
1542 return cf_section_rules_push(subcs, rule->subcs);
1543 }
1544
1545 cf_log_err(cs, "Data of type %s with name \"%s\" already exists. "
1546 "Existing data added %s[%i]", "conf_parser_t",
1547 name1, cd->item.filename, cd->item.lineno);
1548
1549 cf_item_debug(cs);
1550 return -1;
1551 }
1552
1553 return 0;
1554}
1555
1556/** Add an array of parse rules to a #CONF_SECTION
1557 *
1558 * @param[in] cs to add rules to.
1559 * @param[in] rules to add. Last element should have NULL name field.
1560 * @param[in] filename where the rule was pushed.
1561 * @param[in] lineno where the rule was pushed.
1562 * @return
1563 * - 0 on success.
1564 * - -1 on failure.
1565 */
1566int _cf_section_rules_push(CONF_SECTION *cs, conf_parser_t const *rules, char const *filename, int lineno)
1567{
1568 conf_parser_t const *rule_p;
1569
1570 if (!cs || !rules) return 0;
1571
1572 for (rule_p = rules; rule_p->name1; rule_p++) {
1573 if (rule_p->flags & CONF_FLAG_DEPRECATED) continue; /* Skip deprecated */
1574 if (_cf_section_rule_push(cs, rule_p, filename, lineno) < 0) return -1;
1575 }
1576
1577 /*
1578 * Ensure we have a proper terminator, type so we catch
1579 * missing terminators reliably
1580 */
1581 fr_cond_assert(rule_p->type == conf_term.type);
1582
1583 return 0;
1584}
1585
1586/** Generic function for parsing conf pair values as int
1587 *
1588 * @note This should be used for enum types as c99 6.4.4.3 states that the enumeration
1589 * constants are of type int.
1590 *
1591 */
1592int cf_table_parse_int(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent,
1593 CONF_ITEM *ci, conf_parser_t const *rule)
1594{
1595 int num;
1596 cf_table_parse_ctx_t const *parse_ctx = rule->uctx;
1597
1598 if (cf_pair_in_table(&num, parse_ctx->table, *parse_ctx->len, cf_item_to_pair(ci)) < 0) return -1;
1599
1600 *((int *)out) = num;
1601
1602 return 0;
1603}
1604
1605/** Generic function for parsing conf pair values as int32_t (FR_TYPE_INT32)
1606 *
1607 */
1608int cf_table_parse_int32(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent,
1609 CONF_ITEM *ci, conf_parser_t const *rule)
1610{
1611 int32_t num;
1612 cf_table_parse_ctx_t const *parse_ctx = rule->uctx;
1613
1614 if (cf_pair_in_table(&num, parse_ctx->table, *parse_ctx->len, cf_item_to_pair(ci)) < 0) return -1;
1615
1616 *((int32_t *)out) = num;
1617
1618 return 0;
1619}
1620
1621/** Generic function for parsing conf pair values as int32_t (FR_TYPE_UINT32)
1622 *
1623 */
1624int cf_table_parse_uint32(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent,
1625 CONF_ITEM *ci, conf_parser_t const *rule)
1626{
1627 int32_t num;
1628 cf_table_parse_ctx_t const *parse_ctx = rule->uctx;
1629
1630 if (cf_pair_in_table(&num, parse_ctx->table, *parse_ctx->len, cf_item_to_pair(ci)) < 0) return -1;
1631 if (num < 0) {
1632 cf_log_err(ci, "Resolved value must be a positive integer, got %i", num);
1633 return -1;
1634 }
1635 *((uint32_t *)out) = (uint32_t)num;
1636
1637 return 0;
1638}
1639
1640/** Generic function for resolving UID strings to uid_t values
1641 *
1642 * Type should be FR_TYPE_VOID, struct field should be a uid_t.
1643 */
1644int cf_parse_uid(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
1645 CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
1646{
1647 if (fr_perm_uid_from_str(ctx, (uid_t *)out, cf_pair_value(cf_item_to_pair(ci))) < 0) {
1648 cf_log_perr(ci, "Failed resolving UID");
1649 return -1;
1650 }
1651
1652 return 0;
1653}
1654
1655/** Generic function for resolving GID strings to uid_t values
1656 *
1657 * Type should be FR_TYPE_VOID, struct field should be a gid_t.
1658 */
1659int cf_parse_gid(TALLOC_CTX *ctx, void *out, UNUSED void *parent,
1660 CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
1661{
1662 if (fr_perm_gid_from_str(ctx, (gid_t *)out, cf_pair_value(cf_item_to_pair(ci))) < 0) {
1663 cf_log_perr(ci, "Failed resolving GID");
1664 return -1;
1665 }
1666
1667 return 0;
1668}
1669
1670/** Generic function for resolving permissions to a mode-t
1671 *
1672 * Type should be FR_TYPE_VOID, struct field should be a gid_t.
1673 */
1674int cf_parse_permissions(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent,
1675 CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
1676{
1677 mode_t mode;
1678 char const *name = cf_pair_value(cf_item_to_pair(ci));
1679
1680 if (fr_perm_mode_from_str(&mode, name) < 0) {
1681 cf_log_perr(ci, "Invalid permissions string");
1682 return -1;
1683 }
1684
1685 *(mode_t *) out = mode;
1686
1687 return 0;
1688}
1689
1690/** NULL callback for sections
1691 *
1692 * This callback exists only as a place-holder to ensure that the
1693 * nested on_read functions are called. The conf file routines won't
1694 * recurse into every conf_parser_t section to check if there's an
1695 * "on_read" callback. So this place-holder is a signal to do that.
1696 *
1697 * @param[in] ctx to allocate data in.
1698 * @param[out] out Unused
1699 * @param[in] parent Base structure address.
1700 * @param[in] ci #CONF_SECTION containing the current section.
1701 * @param[in] rule unused.
1702 * @return
1703 * - 0 on success.
1704 * - -1 on failure.
1705 */
1706int cf_null_on_read(UNUSED TALLOC_CTX *ctx, UNUSED void *out, UNUSED void *parent,
1707 UNUSED CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
1708{
1709 return 0;
1710}
static int const char char buffer[256]
Definition acutest.h:576
#define RCSID(id)
Definition build.h:485
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
bool check_config
Definition cf_file.c:62
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)
Definition cf_file.c:167
bool cf_file_check(CONF_PAIR *cp, bool check_perms)
Do some checks on the file as an "input" file.
Definition cf_file.c:651
int cf_table_parse_uint32(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int32_t (FR_TYPE_UINT32)
Definition cf_parse.c:1624
int cf_section_parse(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs)
Parse a configuration section into user-supplied variables.
Definition cf_parse.c:1154
int cf_table_parse_int32(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int32_t (FR_TYPE_INT32)
Definition cf_parse.c:1608
static int cf_pair_default(CONF_PAIR **out, void *parent, CONF_SECTION *cs, conf_parser_t const *rule)
Allocate a pair using the dflt value and quotation.
Definition cf_parse.c:326
#define PAIR_SPACE(_cs)
Definition cf_parse.c:46
static char const parse_spaces[]
Definition cf_parse.c:44
int cf_table_parse_int(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int.
Definition cf_parse.c:1592
int cf_pair_parse_value(TALLOC_CTX *ctx, void *out, UNUSED void *base, CONF_ITEM *ci, conf_parser_t const *rule)
Parses a CONF_PAIR into a C data type.
Definition cf_parse.c:173
static int cf_pair_unescape(CONF_PAIR *cp, conf_parser_t const *rule)
Definition cf_parse.c:382
static void cf_section_parse_warn(CONF_SECTION *cs)
Definition cf_parse.c:870
static conf_parser_t conf_term
Definition cf_parse.c:43
int cf_parse_gid(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
Generic function for resolving GID strings to uid_t values.
Definition cf_parse.c:1659
int cf_pair_parse(TALLOC_CTX *ctx, CONF_SECTION *cs, char const *name, unsigned int type, void *data, char const *dflt, fr_token_t dflt_quote)
Parses a CONF_PAIR into a C data type, with a default value.
Definition cf_parse.c:730
void cf_pair_debug_log(CONF_SECTION const *cs, CONF_PAIR *cp, conf_parser_t const *rule)
Definition cf_parse.c:49
int cf_null_on_read(UNUSED TALLOC_CTX *ctx, UNUSED void *out, UNUSED void *parent, UNUSED CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
NULL callback for sections.
Definition cf_parse.c:1706
static int cf_pair_parse_internal(TALLOC_CTX *ctx, void *out, void *base, CONF_SECTION *cs, conf_parser_t const *rule)
Parses a CONF_PAIR into a C data type, with a default value.
Definition cf_parse.c:470
int cf_parse_permissions(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
Generic function for resolving permissions to a mode-t.
Definition cf_parse.c:1674
int cf_section_parse_pass2(void *base, CONF_SECTION *cs)
Fixup xlat expansions and attributes.
Definition cf_parse.c:1274
static int cf_section_parse_init(CONF_SECTION *cs, void *base, conf_parser_t const *rule)
Pre-allocate a config section structure to allow defaults to be set.
Definition cf_parse.c:752
int _cf_section_rule_push(CONF_SECTION *cs, conf_parser_t const *rule, char const *filename, int lineno)
Add a single rule to a CONF_SECTION.
Definition cf_parse.c:1462
#define SECTION_SPACE(_cs)
Definition cf_parse.c:47
int _cf_section_rules_push(CONF_SECTION *cs, conf_parser_t const *rules, char const *filename, int lineno)
Add an array of parse rules to a CONF_SECTION.
Definition cf_parse.c:1566
int cf_pair_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *out, CONF_PAIR *cp, conf_parser_t const *rule)
Parses a CONF_PAIR into a boxed value.
Definition cf_parse.c:126
static int cf_subsection_parse(TALLOC_CTX *ctx, void *out, void *base, CONF_SECTION *cs, conf_parser_t const *rule)
Parse a subsection.
Definition cf_parse.c:914
int cf_parse_uid(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
Generic function for resolving UID strings to uid_t values.
Definition cf_parse.c:1644
static int cf_parse_tmpl_pass2(UNUSED CONF_SECTION *cs, tmpl_t **out, CONF_PAIR *cp, fr_type_t type, bool attribute, fr_dict_t const *dict_def)
Definition cf_parse.c:1196
static int cf_section_parse_rule(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs, conf_parser_t const *rule)
Definition cf_parse.c:1033
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:658
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:612
void const * uctx
User data accessible by the cf_parse_t func.
Definition cf_parse.h:618
void * data
Pointer to a static variable to write the parsed value to.
Definition cf_parse.h:609
#define fr_rule_dflt(_rule)
Definition cf_parse.h:491
conf_parser_flags_t flags
Flags which control parsing behaviour.
Definition cf_parse.h:601
#define fr_rule_not_empty(_rule)
Definition cf_parse.h:483
fr_type_t type
An fr_type_t value, controls the output type.
Definition cf_parse.h:599
size_t offset
Relative offset of field or structure to write the parsed value to.
Definition cf_parse.h:603
#define fr_rule_multi(_rule)
Definition cf_parse.h:481
char const * name2
Second identifier for CONF_SECTION.
Definition cf_parse.h:597
fr_token_t quote
Quoting around the default value. Only used for templates.
Definition cf_parse.h:650
fr_table_num_sorted_t const * table
Definition cf_parse.h:654
#define fr_rule_file_exists(_rule)
Definition cf_parse.h:489
#define fr_rule_deprecated(_rule)
Definition cf_parse.h:470
int(* cf_parse_t)(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Callback for performing custom parsing of a CONF_SECTION or CONF_PAIR.
Definition cf_parse.h:552
#define fr_rule_file_input(_rule)
Definition cf_parse.h:476
#define cf_section_rules_push(_cs, _rule)
Definition cf_parse.h:690
#define fr_rule_is_tmpl(_rule)
Definition cf_parse.h:497
char const * name1
Name of the CONF_ITEM to parse.
Definition cf_parse.h:596
#define fr_rule_is_attribute(_rule)
Definition cf_parse.h:493
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:615
#define fr_rule_required(_rule)
Definition cf_parse.h:472
conf_parser_flags_t
Definition cf_parse.h:426
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition cf_parse.h:434
@ CONF_FLAG_FILE_OUTPUT
File matching value must exist, and must be writable.
Definition cf_parse.h:442
@ CONF_FLAG_MULTI
CONF_PAIR can have multiple copies.
Definition cf_parse.h:448
@ CONF_FLAG_REF
reference another conf_parser_t inline in this one
Definition cf_parse.h:459
@ CONF_FLAG_SECRET
Only print value if debug level >= 3.
Definition cf_parse.h:438
@ CONF_FLAG_IS_SET
Write whether this config item was left as the default to is_set_offset or is_set_ptr.
Definition cf_parse.h:453
@ CONF_FLAG_ATTRIBUTE
Value must resolve to attribute in dict (deprecated, use CONF_FLAG_TMPL).
Definition cf_parse.h:436
@ CONF_FLAG_FILE_INPUT
File matching value must exist, and must be readable.
Definition cf_parse.h:440
@ CONF_FLAG_DEPRECATED
If a matching CONF_PAIR is found, error out with a deprecated message.
Definition cf_parse.h:432
@ CONF_FLAG_XLAT
string will be dynamically expanded.
Definition cf_parse.h:445
@ CONF_FLAG_OPTIONAL
subsection is pushed only if a non-optional matching one is pushed
Definition cf_parse.h:460
@ CONF_FLAG_OK_MISSING
OK if it's missing.
Definition cf_parse.h:456
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:428
@ CONF_FLAG_TMPL
CONF_PAIR should be parsed as a template.
Definition cf_parse.h:446
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:595
bool printed
Was this item printed already in debug mode?
Definition cf_priv.h:82
CONF_ITEM item
Common set of fields.
Definition cf_priv.h:102
void * base
Definition cf_priv.h:113
char const * name2
Second name token. Given foo bar {} would be bar.
Definition cf_priv.h:105
char const * attr
Attribute name.
Definition cf_priv.h:73
fr_token_t rhs_quote
Value Quoting style T_(DOUBLE|SINGLE|BACK)_QUOTE_STRING or T_BARE_WORD.
Definition cf_priv.h:78
char const * value
Attribute value.
Definition cf_priv.h:74
#define cf_item_foreach(_ci, _iter)
Iterate over the contents of a list.
Definition cf_priv.h:149
char const * name1
First name token. Given foo bar {} would be foo.
Definition cf_priv.h:104
void const * data
User data.
Definition cf_priv.h:130
char const * filename
The file the config item was parsed from.
Definition cf_priv.h:64
@ CONF_ITEM_PAIR
Definition cf_priv.h:41
@ CONF_ITEM_SECTION
Definition cf_priv.h:42
bool referenced
Was this item referenced in the config?
Definition cf_priv.h:83
CONF_ITEM item
Common set of fields.
Definition cf_priv.h:125
bool parsed
Was this item used during parsing?
Definition cf_priv.h:81
int lineno
The line number the config item began on.
Definition cf_priv.h:63
Internal data that is associated with a configuration section.
Definition cf_priv.h:124
Common header for all CONF_* types.
Definition cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
CONF_PAIR * cf_pair_find_next(CONF_SECTION const *cs, CONF_PAIR const *prev, char const *attr)
Find a pair with a name matching attr, after specified pair.
Definition cf_util.c:1452
unsigned int cf_pair_count(CONF_SECTION const *cs, char const *attr)
Count the number of times an attribute occurs in a parent section.
Definition cf_util.c:1519
int cf_pair_in_table(int32_t *out, fr_table_num_sorted_t const *table, size_t table_len, CONF_PAIR *cp)
Check to see if the CONF_PAIR value is present in the specified table.
Definition cf_util.c:1965
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
Definition cf_util.c:1762
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
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:1278
void cf_pair_mark_parsed(CONF_PAIR *cp)
Mark a pair as parsed.
Definition cf_util.c:1375
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1170
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:1027
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:1438
CONF_DATA const * _cf_data_add_static(CONF_ITEM *ci, void const *data, char const *type, char const *name, char const *filename, int lineno)
Add non-talloced user data to a config section.
Definition cf_util.c:1827
bool cf_pair_is_parsed(CONF_PAIR *cp)
Return whether a pair has already been parsed.
Definition cf_util.c:1387
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
Definition cf_util.c:1637
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:663
CONF_SECTION * cf_section_find_next(CONF_SECTION const *cs, CONF_SECTION const *prev, char const *name1, char const *name2)
Return the next matching section.
Definition cf_util.c:1048
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1593
CONF_ITEM * cf_pair_to_item(CONF_PAIR const *cp)
Cast a CONF_PAIR to a CONF_ITEM.
Definition cf_util.c:721
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#define cf_data_find(_cf, _type, _name)
Definition cf_util.h:244
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
Definition cf_util.h:367
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:296
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:140
#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_log_warn(_cf, _fmt,...)
Definition cf_util.h:290
#define cf_log_debug(_cf, _fmt,...)
Definition cf_util.h:292
#define cf_data_find_next(_cf, _prev, _type, _name)
Definition cf_util.h:247
#define cf_item_debug(_cf)
Definition cf_util.h:361
#define CF_IDENT_ANY
Definition cf_util.h:78
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
#define MEM(x)
Definition debug.h:36
static char const * spaces
Definition dependency.c:370
Test enumeration values.
Definition dict_test.h:92
#define DEBUG_ENABLED4
True if global debug level 1-3 messages are enabled.
Definition log.h:260
talloc_free(reap)
int fr_debug_lvl
Definition log.c:40
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:87
@ L_DBG_LVL_3
3rd highest priority debug messages (-xxx | -Xx).
Definition log.h:72
bool main_config_migrate_option_get(char const *name)
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_VOID
User data.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
unsigned int mode_t
int fr_perm_mode_from_str(mode_t *out, char const *str)
Definition perm.c:62
int fr_perm_uid_from_str(TALLOC_CTX *ctx, uid_t *out, char const *name)
Resolve a user name to a GID.
Definition perm.c:451
int fr_perm_gid_from_str(TALLOC_CTX *ctx, gid_t *out, char const *name)
Resolve a group name to a GID.
Definition perm.c:473
char * fr_asprint(TALLOC_CTX *ctx, char const *in, ssize_t inlen, char quote)
Escape string that may contain binary data, and write it to a new buffer.
Definition print.c:428
#define fr_assert(_expr)
Definition rad_assert.h:38
static char * secret
#define WARN(fmt,...)
Definition radclient.h:47
fr_dict_attr_t const * request_attr_request
Definition request.c:43
static char const * name
#define FR_SBUFF_IN(_start, _len_or_end)
static char const * tmpl_type_to_str(tmpl_type_t type)
Return a static string containing the type name.
Definition tmpl.h:634
#define tmpl_contains_data(vpt)
Definition tmpl.h:224
int tmpl_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules))
Attempt to resolve functions and attributes in xlats and attribute references.
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
@ TMPL_TYPE_REGEX_UNCOMPILED
Regex where compilation is possible but hasn't been performed yet.
Definition tmpl.h:158
@ TMPL_TYPE_MAX
Marker for the last tmpl type.
Definition tmpl.h:199
@ TMPL_TYPE_ATTR_UNRESOLVED
An attribute reference that we couldn't resolve but looked valid.
Definition tmpl.h:185
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition tmpl.h:142
@ TMPL_TYPE_XLAT
Pre-parsed xlat expansion.
Definition tmpl.h:146
@ TMPL_TYPE_EXEC
Callout to an external script or program.
Definition tmpl.h:150
@ TMPL_TYPE_REGEX_XLAT_UNRESOLVED
A regular expression with unresolved xlat functions or attribute references.
Definition tmpl.h:197
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition tmpl.h:138
@ TMPL_TYPE_REGEX
Compiled (and possibly JIT'd) regular expression.
Definition tmpl.h:154
@ TMPL_TYPE_DATA_UNRESOLVED
Unparsed literal string.
Definition tmpl.h:179
@ TMPL_TYPE_XLAT_UNRESOLVED
A xlat expansion with unresolved xlat functions or attribute references.
Definition tmpl.h:193
@ TMPL_TYPE_REGEX_XLAT
A regex containing xlat expansions.
Definition tmpl.h:162
@ TMPL_TYPE_EXEC_UNRESOLVED
An exec with unresolved xlat function or attribute references.
Definition tmpl.h:189
@ TMPL_TYPE_UNINITIALISED
Uninitialised.
Definition tmpl.h:134
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
static char const * tmpl_attr_tail_unresolved(tmpl_t const *vpt)
Return the last attribute reference unresolved da.
Definition tmpl.h:869
ssize_t tmpl_afrom_attr_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t **out, fr_sbuff_t *name, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Parse a string into a TMPL_TYPE_ATTR_* type tmpl_t.
int tmpl_cast_in_place(tmpl_t *vpt, fr_type_t type, fr_dict_attr_t const *enumv))
Convert tmpl_t of type TMPL_TYPE_DATA_UNRESOLVED or TMPL_TYPE_DATA to TMPL_TYPE_DATA of type specifie...
#define tmpl_is_data(vpt)
Definition tmpl.h:206
static fr_slen_t vpt
Definition tmpl.h:1269
#define tmpl_value_type(_tmpl)
Definition tmpl.h:939
#define tmpl_is_data_unresolved(vpt)
Definition tmpl.h:217
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:335
int tmpl_cast_set(tmpl_t *vpt, fr_type_t type)
Set a cast for a tmpl.
Similar to tmpl_rules_t, but used to specify parameters that may change during subsequent resolution ...
Definition tmpl.h:364
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:332
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
return count
Definition module.c:155
fr_aka_sim_id_type_t type
fr_dict_attr_t const * list_def
Default list to use with unqualified attribute reference.
Definition tmpl.h:295
uint8_t allow_unknown
Allow unknown attributes i.e.
Definition tmpl.h:301
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:441
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:224
const char fr_token_quote[T_TOKEN_LAST]
Convert tokens back to a quoting character.
Definition token.c:155
enum fr_token fr_token_t
@ T_INVALID
Definition token.h:39
@ T_SINGLE_QUOTED_STRING
Definition token.h:122
@ T_BARE_WORD
Definition token.h:120
@ T_OP_EQ
Definition token.h:83
@ T_BACK_QUOTED_STRING
Definition token.h:123
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
@ T_SOLIDUS_QUOTED_STRING
Definition token.h:124
fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
Tokenize an xlat expansion.
static fr_slen_t parent
Definition pair.h:845
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
void ** fr_type_array_alloc(TALLOC_CTX *ctx, fr_type_t type, size_t count)
Allocate an array of a given type.
Definition types.c:631
#define fr_type_is_string(_x)
Definition types.h:327
#define fr_type_is_quoted(_x)
Definition types.h:368
size_t const fr_value_box_field_sizes[]
How many bytes wide each of the value data fields are.
Definition value.c:148
ssize_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:5450
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:3904
fr_sbuff_parse_rules_t const * value_parse_rules_unquoted[T_TOKEN_LAST]
Parse rules for non-quoted strings.
Definition value.c:507
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:3946
static int fr_value_box_memcpy_out(void *out, fr_value_box_t const *vb)
Copy the value of a value box to a field in a C struct.
Definition value.h:779
#define fr_value_box_mark_safe_for(_box, _safe_for)
Definition value.h:1063
static fr_slen_t data
Definition value.h:1274
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1012
#define FR_VALUE_BOX_SAFE_FOR_ANY
Definition value.h:166
fr_dict_t const * virtual_server_dict_by_child_ci(CONF_ITEM const *ci)
Return the namespace for a given virtual server specified by a CONF_ITEM within the virtual server.