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