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