The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
map.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: f915a577be9e5461d3fcac8a3825a2679b860e75 $
19 *
20 * @brief map / template functions
21 * @file src/lib/server/map.c
22 *
23 * @ingroup AVP
24 *
25 * @copyright 2013 The FreeRADIUS server project
26 * @copyright 2013 Alan DeKok (aland@freeradius.org)
27 */
28
29RCSID("$Id: f915a577be9e5461d3fcac8a3825a2679b860e75 $")
30
31#include <freeradius-devel/server/exec.h>
32#include <freeradius-devel/server/exec_legacy.h>
33#include <freeradius-devel/server/map.h>
34#include <freeradius-devel/server/paircmp.h>
35#include <freeradius-devel/server/tmpl_dcursor.h>
36
37#include <freeradius-devel/unlang/interpret.h>
38
39#include <freeradius-devel/util/base16.h>
40#include <freeradius-devel/util/pair_legacy.h>
41
42#include <freeradius-devel/protocol/radius/rfc2865.h>
43#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
44
45
47 { L("\""), T_DOUBLE_QUOTED_STRING }, /* Don't re-order, backslash throws off ordering */
48 { L("'"), T_SINGLE_QUOTED_STRING },
49 { L("/"), T_SOLIDUS_QUOTED_STRING },
50 { L("`"), T_BACK_QUOTED_STRING }
51};
53
54
55#ifdef DEBUG_MAP
56static void map_dump(request_t *request, map_t const *map)
57{
58 if (map->rhs) {
59 RDEBUG2(">>> MAP NAMES %s %s", map->lhs->name, map->rhs->name);
60
61 RDEBUG2(">>> MAP TYPES LHS: %s, RHS: %s",
62 tmpl_type_to_str(map->lhs->type),
63 tmpl_type_to_str(map->rhs->type));
64 } else {
65 RDEBUG2(">>> MAP NAMES %s", map->lhs->name);
66
67 RDEBUG2(">>> MAP TYPES LHS: %s",
68 tmpl_type_to_str(map->lhs->type));
69 }
70}
71#endif
72
73static inline map_t *map_alloc(TALLOC_CTX *ctx, map_t *parent)
74{
75 map_t *map;
76
77 if (parent) {
78 map = talloc_zero(parent, map_t);
79 } else {
80 map = talloc_zero(ctx, map_t);
81 }
82 map->parent = parent;
83
84 map_list_init(&map->child);
85 return map;
86}
87
88/** Convert CONFIG_PAIR (which may contain refs) to map_t.
89 *
90 * Treats the left operand as an attribute reference
91 * @verbatim<request>.<list>.<attribute>@endverbatim
92 *
93 * Treatment of left operand depends on quotation, barewords are treated as
94 * attribute references, double quoted values are treated as expandable strings,
95 * single quoted values are treated as literal strings.
96 *
97 * Return must be freed with talloc_free
98 *
99 * @param[in] ctx for talloc.
100 * @param[in] out Where to write the pointer to the new #map_t.
101 * @param[in] parent the parent map
102 * @param[in] cp to convert to map.
103 * @param[in] lhs_rules rules for parsing LHS attribute references.
104 * @param[in] input_rhs_rules rules for parsing RHS attribute references.
105 * @param[in] edit treat the map as an edit
106 * @return
107 * - #map_t if successful.
108 * - NULL on error.
109 */
110int map_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp,
111 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *input_rhs_rules, bool edit)
112{
113 map_t *map;
114 char const *attr, *value, *marker_subject;
115 char *unescaped_value = NULL;
116 fr_sbuff_parse_rules_t const *p_rules;
117 ssize_t slen;
118 fr_token_t quote;
119 fr_dict_attr_t const *da;
120 tmpl_rules_t my_rhs_rules = {};
121 tmpl_rules_t const *rhs_rules = input_rhs_rules;
122 TALLOC_CTX *child_ctx = NULL;
123
124 *out = NULL;
125
126 if (!cp) return -1;
127
128 MEM(map = map_alloc(ctx, parent));
129 map->op = cf_pair_operator(cp);
130 map->ci = cf_pair_to_item(cp);
131
132 attr = cf_pair_attr(cp);
133 value = cf_pair_value(cp);
134 if (!value) {
135 cf_log_err(cp, "Missing attribute value");
136 goto error;
137 }
138
139 /*
140 * Allow for the RHS rules to be taken from the LHS rules.
141 */
142 if (!rhs_rules) rhs_rules = lhs_rules;
143
144 MEM(child_ctx = talloc(map, uint8_t));
145
146 /*
147 * LHS may be an expansion (that expands to an attribute reference)
148 * or an attribute reference. Quoting determines which it is.
149 */
150 quote = cf_pair_attr_quote(cp);
151 switch (quote) {
154 slen = tmpl_afrom_substr(ctx, &map->lhs,
155 &FR_SBUFF_IN(attr, talloc_strlen(attr)),
156 quote,
157 value_parse_rules_unquoted[quote], /* We're not searching for quotes */
158 lhs_rules);
159 if (slen <= 0) {
160 char *spaces, *text;
161
162 marker_subject = attr;
163 marker:
164 fr_canonicalize_error(ctx, &spaces, &text, slen, marker_subject);
165 cf_log_err(cp, "%s", text);
166 cf_log_perr(cp, "%s^", spaces);
167
169 talloc_free(text);
170 goto error;
171 }
172 break;
173
174 default:
175 slen = tmpl_afrom_attr_str(ctx, NULL, &map->lhs, attr, lhs_rules);
176 if (slen <= 0) {
177 cf_log_err(cp, "Failed parsing attribute reference %s - %s", attr, fr_strerror());
178 marker_subject = attr;
179 goto marker;
180 }
181
183 cf_log_perr(cp, "Failed creating attribute %s", map->lhs->name);
184 goto error;
185 }
186
188
189 /*
190 * The caller wants the RHS attributes to be
191 * parsed in the context of the LHS, but only if
192 * the LHS attribute was a group / structural attribute.
193 */
194 if (!input_rhs_rules) {
195 tmpl_rules_child_init(child_ctx, &my_rhs_rules, lhs_rules, map->lhs);
196 } else {
197 my_rhs_rules = *input_rhs_rules;
198 }
199 rhs_rules = &my_rhs_rules;
200
201 if (edit) {
202 if ((map->op != T_OP_RSHIFT_EQ) && (map->op != T_OP_LSHIFT_EQ)) {
203 my_rhs_rules.enumv = tmpl_attr_tail_da(map->lhs);
204 }
205 } else {
206 /*
207 * If we parsed an attribute on the LHS, and the RHS looks like an enumerated
208 * value, then set the enumv.
209 */
210 if (!rhs_rules->enumv && tmpl_is_attr(map->lhs) &&
211 (value[0] == ':') && value[1] == ':') {
212 my_rhs_rules.enumv = tmpl_attr_tail_da(map->lhs);
213 }
214 }
215 break;
216 }
217
218 /*
219 * RHS might be an attribute reference.
220 */
221 quote = cf_pair_value_quote(cp);
222 p_rules = value_parse_rules_unquoted[quote]; /* We're not searching for quotes */
223 if (quote == T_DOUBLE_QUOTED_STRING || quote == T_BACK_QUOTED_STRING) {
224 slen = fr_sbuff_out_aunescape_until(child_ctx, &unescaped_value,
225 &FR_SBUFF_IN(value, talloc_strlen(value)), SIZE_MAX, p_rules->terminals, p_rules->escapes);
226 if (slen < 0) {
227 marker_subject = value;
228 goto marker;
229 }
230 value = unescaped_value;
231 p_rules = NULL;
232
233 } else if (edit && (quote == T_HASH)) {
234 fr_slen_t slent;
236
237 /*
238 * Not a bare word, it's marked up as a complex expression.
239 *
240 * See cf_file.c, parse_input() and the use of T_HASH when doing alloc_pair,
241 * in order to see what magic is going on here.
242 */
243 slen = talloc_strlen(value);
244
245 MEM(map->rhs = tmpl_alloc(map, TMPL_TYPE_XLAT, T_BARE_WORD, value, slen));
246
247 /*
248 * Tokenize the expression.
249 */
250 slent = xlat_tokenize_expression(map->rhs, &head, &FR_SBUFF_IN(value, slen), p_rules, rhs_rules);
251 if (slent <= 0) {
252 slen = slent + 1;
253 marker_subject = value;
254 goto marker;
255 }
256
257 tmpl_set_xlat(map->rhs, head);
258 goto verify;
259
260 } else if ((map->op == T_OP_CMP_TRUE) || (map->op == T_OP_CMP_FALSE)) {
261 /*
262 * These operators require a hard-coded string on the RHS.
263 */
264 if (strcmp(value, "ANY") != 0) {
265 fr_strerror_printf("Invalid value for %s", fr_tokens[map->op]);
266 goto error;
267 }
268
269 (void) tmpl_afrom_value_box(map, &map->rhs, fr_box_strvalue("ANY"), false);
270 goto verify;
271
272 } else {
273 slen = talloc_strlen(value);
274 }
275
276 /*
277 * If we're assigning to a structural attribute, AND the
278 * RHS side is a string, THEN don't parse the RHS as a
279 * "group". The edit code will take the string, create
280 * pairs, and work on that.
281 */
282 if (edit && (rhs_rules == &my_rhs_rules) && my_rhs_rules.enumv && fr_type_is_structural(my_rhs_rules.enumv->type) &&
283 ((quote == T_DOUBLE_QUOTED_STRING) || (quote == T_BACK_QUOTED_STRING) || (quote == T_SINGLE_QUOTED_STRING))) {
284 my_rhs_rules.enumv = NULL;
285 }
286
287 slen = tmpl_afrom_substr(map, &map->rhs,
288 &FR_SBUFF_IN(value, slen),
289 quote,
290 p_rules,
291 rhs_rules);
292 if (slen < 0) {
293 marker_subject = value;
294 goto marker;
295 }
296
297 if (!map->rhs) {
298 cf_log_perr(cp, "Failed parsing RHS");
299 goto error;
300 }
301
302 if (tmpl_is_attr(map->rhs) && (tmpl_attr_unknown_add(map->rhs) < 0)) {
303 cf_log_perr(cp, "Failed creating attribute %s", map->rhs->name);
304 goto error;
305 }
306
307 /*
308 * We cannot assign a count to an attribute. That must
309 * be done in an xlat.
310 */
311 if (tmpl_is_attr(map->rhs) &&
312 (tmpl_attr_tail_num(map->rhs) == NUM_COUNT)) {
313 cf_log_err(cp, "Cannot assign from a count");
314 goto error;
315 }
316
317 /*
318 * If we can resolve the RHS in the context of the LHS,
319 * and are allowed to do so, then do that now.
320 *
321 * The "map" keyword uses the RHS not as the value which
322 * is assigned to the LHS. Instead, it is a pointer to
323 * the value. As such, we cannot immediately resolve the
324 * RHS in the context of the LHS.
325 *
326 * The edit code allows string assignments to lists, and
327 * will interpret the string as a sequence of edit
328 * operations. So we skip casting strings to lists.
329 *
330 * @todo - that could arguably be done immediately here,
331 * and the RHS could be parsed and created as a child
332 * map. That would allow for more compile-time sanity
333 * checks.
334 */
335 if (tmpl_is_attr(map->lhs) && tmpl_is_data(map->rhs) &&
336 (!input_rhs_rules || !input_rhs_rules->attr.disallow_rhs_resolve) &&
338 fr_type_t cast_type;
339
340 if ((map->op != T_OP_RSHIFT_EQ) && (map->op != T_OP_LSHIFT_EQ)) {
341 da = tmpl_attr_tail_da(map->lhs);
342 cast_type = da->type;
343 } else {
344 da = NULL;
345 cast_type = FR_TYPE_UINT32;
346 }
347
348 if (tmpl_cast_in_place(map->rhs, cast_type, da) < 0) {
349 cf_log_err(cp, "Invalid assignment - %s", fr_strerror());
350 goto error;
351 }
352 }
353
354verify:
355 MAP_VERIFY(map);
356 TALLOC_FREE(child_ctx);
357
358 *out = map;
359
360 return 0;
361
362error:
363 TALLOC_FREE(child_ctx);
364 talloc_free(map);
365 return -1;
366}
367
369 { L("!*"), T_OP_CMP_FALSE },
370 { L("!="), T_OP_NE },
371 { L("!~"), T_OP_REG_NE },
372 { L("+="), T_OP_ADD_EQ },
373 { L("-="), T_OP_SUB_EQ },
374 { L(":="), T_OP_SET },
375 { L("<"), T_OP_LT },
376 { L("<="), T_OP_LE },
377 { L("="), T_OP_EQ },
378 { L("=*"), T_OP_CMP_TRUE },
379 { L("=="), T_OP_CMP_EQ },
380 { L("=~"), T_OP_REG_EQ },
381 { L(">"), T_OP_GT },
382 { L(">="), T_OP_GE }
383};
385
386fr_sbuff_parse_rules_t const map_parse_rules_bareword_quoted = {
387 .escapes = &(fr_sbuff_unescape_rules_t){
388 .chr = '\\',
389 /*
390 * Allow barewords to contain whitespace
391 * if they're escaped.
392 */
393 .subs = {
394 ['\t'] = '\t',
395 ['\n'] = '\n',
396 [' '] = ' '
397 },
398 .do_hex = false,
399 .do_oct = false
400 },
401
402 /*
403 * We want to stop on _any_ terminal character, even if
404 * the token itself isn't valid here. Doing so means
405 * that we don't have the parser accept things like:
406 *
407 * User-Name,,,,=bob===
408 */
409 .terminals = &FR_SBUFF_TERMS(
410 L("\t"),
411 L("\n"),
412 L(" "),
413 L("!*"),
414 L("!="),
415 L("!~"),
416 L("+="),
417 L(","),
418 L("-="),
419 L(":="),
420 L("<"),
421 L("<="),
422 L("=*"),
423 L("=="),
424 L("=~"),
425 L(">"),
426 L(">="),
427 )
428};
429
437
438/** Parse sbuff into (which may contain refs) to map_t.
439 *
440 * This function uses the legacy operator meanings. The maps created here
441 * should only be used with radius_legacy_map_cmp() and
442 * radius_legacy_map_apply()
443 *
444 * The left operand MUST be an attribute reference
445 * @verbatim<request>.<list>.<attribute>@endverbatim
446 *
447 * The op_table should be #cond_cmp_op_table for check items, and
448 * #map_assignment_op_table for reply items.
449 *
450 * Return must be freed with talloc_free
451 *
452 * @param[in] ctx for talloc.
453 * @param[in] out Where to write the pointer to the new #map_t.
454 * @param[in,out] parent_p the parent map, updated for relative maps
455 * @param[in] in the data to parse for creating the map.
456 * @param[in] op_table for lhs OP rhs
457 * @param[in] op_table_len length of op_table
458 * @param[in] lhs_rules rules for parsing LHS attribute references.
459 * @param[in] rhs_rules rules for parsing RHS attribute references.
460 * @param[in] p_rules a list of terminals which will stop string
461 * parsing.
462 * @return
463 * - >0 on success.
464 * - <=0 on error.
465 */
466ssize_t map_afrom_substr(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, fr_sbuff_t *in,
467 fr_table_num_sorted_t const *op_table, size_t op_table_len,
468 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
469 fr_sbuff_parse_rules_t const *p_rules)
470{
471 ssize_t slen;
472 fr_token_t token;
473 map_t *map;
474 fr_sbuff_t our_in = FR_SBUFF(in);
475 fr_sbuff_marker_t m_lhs, m_rhs, m_op;
476 fr_sbuff_term_t const *tt = p_rules ? p_rules->terminals : NULL;
477 map_t *parent;
478 fr_dict_attr_t const *enumv;
479 tmpl_rules_t our_rhs_rules;
480
481 if (parent_p) {
482 parent = *parent_p;
483 } else {
484 parent = NULL;
485 }
486
487 *out = NULL;
488 MEM(map = map_alloc(ctx, NULL));
489
490 (void)fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, tt);
491
492 fr_sbuff_marker(&m_lhs, &our_in);
494 switch (token) {
495 default:
496 fr_strerror_const("Expected attribute reference, not string");
497 goto error;
498
499 case T_BARE_WORD:
500 {
501 tmpl_rules_t our_lhs_rules;
502
503 our_lhs_rules = *lhs_rules;
504
505 /*
506 * Bare words on the LHS are always attributes.
507 *
508 * It doesn't make sense to allow "5 = 4". Or, "5 = NAS-Port".
509 *
510 * The conditions do allow "5 == NAS-Port", but
511 * they use a different parser for that.
512 */
513
514 /*
515 * Absolute references are from the root.
516 */
517 if (!fr_sbuff_next_if_char(&our_in, '.')) {
518 parent = NULL;
519
520 lhs_root:
521 slen = tmpl_afrom_attr_substr(map, NULL, &map->lhs, &our_in,
522 &map_parse_rules_bareword_quoted, &our_lhs_rules);
523 break;
524 }
525
526 /*
527 * Allow for ".foo" to refer to the current
528 * parents list. Allow for "..foo" to refer to
529 * the grandparent list.
530 */
531
532 /*
533 * Relative references must have a parent.
534 */
535 if (!parent) {
536 fr_strerror_const("Unexpected location for relative attribute - no parent attribute exists");
537 goto error;
538 }
539
540 /*
541 * Multiple '.' means "go to our parents parent".
542 */
543 while (fr_sbuff_next_if_char(&our_in, '.')) {
544 if (!parent) {
545 fr_strerror_const("Too many '.' in relative reference");
546 goto error;
547 }
548 parent = parent->parent;
549 }
550
551 if (!parent) goto lhs_root;
552
553 /*
554 * Start looking in the correct parent, not in whatever we were handed.
555 */
557 our_lhs_rules.attr.namespace = tmpl_attr_tail_da(parent->lhs);
558
559 slen = tmpl_afrom_attr_substr(map, NULL, &map->lhs, &our_in,
560 &map_parse_rules_bareword_quoted, &our_lhs_rules);
561 break;
562 }
563 }
564
565 if (!map->lhs) {
566 error:
567 slen = 0;
568
569 error_adj:
570 talloc_free(map);
571 FR_SBUFF_ERROR_RETURN(&our_in);
572 }
574
575 (void)fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, tt);
576 fr_sbuff_marker(&m_op, &our_in);
577
578 /*
579 * Parse operator.
580 */
581 fr_sbuff_out_by_longest_prefix(&slen, &map->op, op_table, &our_in, T_INVALID);
582 if (map->op == T_INVALID) {
583 fr_strerror_const("Invalid operator");
584 goto error_adj;
585 }
586
587 /*
588 * Validate operators for check items.
589 *
590 * We can have comparison operators for reply items, as the rlm_attr_filter module
591 * uses that.
592 *
593 * However, we can't do comparisons on structural entries, except for existence checks.
594 */
595 if (!parent_p && tmpl_attr_tail_da_is_structural(map->lhs)) {
596 if (fr_comparison_op[map->op] && (map->op != T_OP_CMP_TRUE) && (map->op != T_OP_CMP_FALSE)) {
597 fr_sbuff_set(&our_in, &m_op);
598 fr_strerror_const("Comparison operators cannot be used inside of structural data types");
599 goto error;
600 }
601 }
602
603 (void)fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, tt);
604
605 /*
606 * Copy LHS code above, except parsing in RHS, with some
607 * minor modifications.
608 */
609 fr_sbuff_marker(&m_rhs, &our_in);
610
611 /*
612 * If the LHS is a structural attribute, then (for now),
613 * the RHS can only be {}. This limitation should only
614 * be temporary, as we should really recurse to create
615 * child maps. And then the child maps should not have
616 * '.' as prefixes, and should require that the LHS can
617 * only be an attribute, etc. Not trivial, so we'll just
618 * skip all that for now.
619 */
620 switch (tmpl_attr_tail_da(map->lhs)->type) {
622 if ((map->op == T_OP_REG_EQ) || (map->op == T_OP_REG_NE)) {
623 fr_sbuff_set(&our_in, &m_op);
624 fr_assert(0);
625 fr_strerror_const("Regular expressions cannot be used for structural attributes");
626 goto error;
627 }
628
629 /*
630 * To match Vendor-Specific =* ANY
631 *
632 * Which is a damned hack.
633 */
634 if ((map->op == T_OP_CMP_TRUE) || (map->op == T_OP_CMP_FALSE)) goto parse_rhs;
635
636 if (fr_comparison_op[map->op]) {
637 fr_sbuff_set(&our_in, &m_op);
638 fr_strerror_const("Comparison operators cannot be used for structural attributes");
639 goto error;
640 }
641
642 /*
643 * radius_legacy_map_cmp() and radius_legacy_map_apply() both support structural
644 * attributes with RHS strings. And this function is only called from
645 * users_file.c. The consumers of the users file only call the radius legacy map
646 * functions.
647 */
648 if (!fr_sbuff_next_if_char(&our_in, '{')) {
649 goto parse_rhs;
650 }
651
652 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, tt);
653
654 /*
655 * Peek at the next character. If it's '}', stop.
656 */
657 if (!fr_sbuff_next_if_char(&our_in, '}')) {
658 fr_sbuff_set(&our_in, &m_rhs);
659 fr_strerror_const("Unexpected text before '}'");
660 goto error;
661 }
662
663 /*
664 * @todo - call ourselves recursively, with a special flag saying '.' is no
665 * longer necessary. And that we need to stop on '}'
666 */
667
668 /*
669 * Create an empty RHS.
670 */
672 (void) fr_value_box_strdup(map->rhs, tmpl_value(map->rhs), NULL, "", false);
673 goto check_for_child;
674
675 default:
676 break;
677 }
678
679parse_rhs:
680 /*
681 * These operators require a hard-coded string on the
682 * RHS. And we don't care about enums.
683 */
684 if ((map->op == T_OP_CMP_TRUE) || (map->op == T_OP_CMP_FALSE)) {
685 if (fr_sbuff_adv_past_str_literal(&our_in, "ANY") <= 0) {
686 fr_strerror_printf("Invalid value for %s", fr_tokens[map->op]);
687 goto error;
688 }
689
690 (void) tmpl_afrom_value_box(map, &map->rhs, fr_box_strvalue("ANY"), false);
691
692 goto check_for_child;
693 }
694
695 enumv = tmpl_attr_tail_da(map->lhs);
696
697 /*
698 * LHS is a structural type. The RHS is either empty (create empty LHS), or it's a string
699 * containing a list of attributes to create.
700 */
701 if (fr_type_is_leaf(enumv->type)) {
702 our_rhs_rules = *rhs_rules;
703 our_rhs_rules.enumv = enumv;
704 rhs_rules = &our_rhs_rules;
705 }
706
708 switch (token) {
713 slen = tmpl_afrom_substr(map, &map->rhs, &our_in, token,
714 value_parse_rules_quoted[token], rhs_rules);
715 break;
716
717 default:
718 if (rhs_rules->attr.bare_word_enum && rhs_rules->enumv) {
719 fr_value_box_t *vb;
720
721 MEM(vb = fr_value_box_alloc(map, rhs_rules->enumv->type, rhs_rules->enumv));
722
723 if (!p_rules) p_rules = &value_parse_rules_bareword_quoted;
724
725 /*
726 * It MUST be the given data type, and it MAY be an enum name.
727 */
728 slen = fr_value_box_from_substr(map, vb, rhs_rules->enumv->type, rhs_rules->enumv,
729 &our_in, p_rules);
730 if (slen < 0) goto error;
731
732 if (tmpl_afrom_value_box(map, &map->rhs, vb, true) < 0) {
733 goto error;
734 }
735
736 } else {
737 if (!p_rules) p_rules = &value_parse_rules_bareword_quoted;
738
739 /*
740 * Use the RHS termination rules ONLY for bare
741 * words. For quoted strings we already know how
742 * to terminate the input string.
743 */
744 slen = tmpl_afrom_substr(map, &map->rhs, &our_in, token, p_rules, rhs_rules);
745 }
746 break;
747 }
748 if (!map->rhs) goto error_adj;
749
750 /*
751 * Check for, and skip, the trailing quote if we had a leading quote.
752 */
753 if (token != T_BARE_WORD) {
754 if (!fr_sbuff_next_if_char(&our_in, fr_token_quote[token])) {
755 fr_strerror_const("Unexpected end of quoted string");
756 goto error;
757 }
758
759 /*
760 * The tmpl code does NOT return tmpl_type_data
761 * for string data without xlat. Instead, it
762 * creates TMPL_TYPE_DATA_UNRESOLVED.
763 */
764 if (tmpl_resolve(map->rhs, NULL) < 0) {
765 fr_sbuff_set(&our_in, &m_rhs); /* Marker points to RHS */
766 goto error;
767 }
768 } else if (tmpl_is_attr(map->lhs) && (tmpl_is_data_unresolved(map->rhs) || tmpl_is_data(map->rhs))) {
769 /*
770 * If the operator is "true" or "false", just
771 * cast the RHS to string, as no one will care
772 * about it.
773 */
774 if ((map->op != T_OP_CMP_TRUE) && (map->op != T_OP_CMP_FALSE)) {
775 fr_dict_attr_t const *da = tmpl_attr_tail_da(map->lhs);
776
777 if (tmpl_cast_in_place(map->rhs, da->type, da) < 0) {
778 fr_sbuff_set(&our_in, &m_rhs); /* Marker points to RHS */
779 goto error;
780 }
781 } else {
782 if (tmpl_cast_in_place(map->rhs, FR_TYPE_STRING, NULL) < 0) {
783 fr_sbuff_set(&our_in, &m_rhs); /* Marker points to RHS */
784 goto error;
785 }
786 }
787 }
788
789 if (tmpl_contains_regex(map->lhs)) {
790 fr_sbuff_set(&our_in, &m_lhs);
791 fr_strerror_const("Unexpected regular expression");
792 goto error;
793 }
794
795 if ((map->op == T_OP_REG_EQ) || (map->op == T_OP_REG_NE)) {
796 if (!tmpl_contains_regex(map->rhs)) {
797 fr_sbuff_set(&our_in, &m_rhs);
798 fr_strerror_const("Expected regular expression after regex operator");
799 goto error;
800 }
801 } else {
802 if (tmpl_contains_regex(map->rhs)) {
803 fr_sbuff_set(&our_in, &m_rhs);
804 fr_strerror_const("Unexpected regular expression");
805 goto error;
806 }
807 }
808
809check_for_child:
810 /*
811 * Add this map to to the parents list. Note that the caller
812 * will have to check for this, but checking if map->parent
813 * exists.
814 */
815 if (parent) {
816 (void) talloc_steal(parent, map);
817 map->parent = parent;
818 map_list_insert_tail(&parent->child, map);
819 }
820
821 if (parent_p) {
823 *parent_p = map;
824 } else {
825 *parent_p = parent;
826 }
827 }
828
829 /*
830 * Xlat expansions are cast to strings for structural data types.
831 */
834 }
835
836 MAP_VERIFY(map);
837 *out = map;
838
839 FR_SBUFF_SET_RETURN(in, &our_in);
840}
841
842static int _map_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, map_t *parent, CONF_SECTION *cs,
843 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
844 map_validate_t validate, void *uctx,
845 unsigned int max, bool update, bool edit)
846{
847 CONF_ITEM *ci;
848 CONF_PAIR *cp;
849
850 unsigned int total = 0;
851 map_t *map;
852 TALLOC_CTX *parent_ctx;
853
854 tmpl_rules_t our_lhs_rules = *lhs_rules; /* Mutable copy of the destination */
855 tmpl_rules_t child_rhs_rules = *rhs_rules;
856 tmpl_rules_t const *our_rhs_rules;
857
858 /*
859 * The first map has ctx as the parent context.
860 * The rest have the previous map as the parent context.
861 */
862 parent_ctx = ctx;
863
864 ci = cf_section_to_item(cs);
865
866 /*
867 * Check the "update" section for destination lists.
868 */
869 if (update) {
870 char const *name2 = cf_section_name2(cs);
871
872 if (name2) {
873 fr_dict_attr_t const *list_def = NULL;
874 fr_sbuff_t sbuff = FR_SBUFF_IN(name2, strlen(name2));
875
876 /*
877 * There's a name, but it's not one we recognize. Or, it's one we recognize but
878 * there are things after it, we forbid it.
879 *
880 * This code is ONLY used for "update" sections in modules. None of those
881 * examples use a destination list. If we allow any destination list (including
882 * parent, outer, etc.) then we allow the possibility of changing protocols,
883 * which is bad.
884 *
885 * This limitation also means that the module "update" lists can't automatically
886 * edit other structural attributes, such as "reply.foo". But that seems a small
887 * price to pay.
888 *
889 * The admin can still specify outer / parent / etc. on the individual entries in
890 * an "update" section, but we'll let that go for now.
891 *
892 * @todo - We should arguably forbid parent/outer list references in an "update"
893 * section.
894 */
895 if ((tmpl_attr_list_from_substr(&list_def, &sbuff) <= 0) ||
896 !fr_sbuff_eof(&sbuff)) {
897 cf_log_err(ci, "Invalid destination list specifier for 'update' - must be one of 'request', 'reply', 'control', or 'state'");
898 return -1;
899 }
900
901 our_lhs_rules.attr.list_def = list_def;
902 }
903 }
904
905 for (ci = cf_item_next(cs, NULL);
906 ci != NULL;
907 ci = cf_item_next(cs, ci)) {
908 tmpl_rules_t child_lhs_rules = our_lhs_rules;
909
910 /*
911 * Disallow list references on the LHS of child lists for edit sections.
912 */
913 if (edit) child_lhs_rules.attr.list_presence = TMPL_ATTR_LIST_FORBID;
914
915 if (total++ == max) {
916 cf_log_err(ci, "Map size exceeded");
917 error:
918 /*
919 * Free in reverse as successive entries have their
920 * prececessors as talloc parent contexts
921 */
922 map_list_talloc_reverse_free(out);
923 return -1;
924 }
925
926 /*
927 * If we have a subsection, AND the name2 is an
928 * assignment operator, THEN we allow sub-maps.
929 */
930 if (cf_item_is_section(ci)) {
931 CONF_SECTION *subcs;
932 fr_token_t token;
933 ssize_t slen;
934 map_list_t child_list;
935
936 map_list_init(&child_list);
937 subcs = cf_item_to_section(ci);
938 token = cf_section_name2_quote(subcs);
939
940 if (token == T_INVALID) {
941 cf_log_err(ci, "Section '%s { ... }' is missing the '=' operator", cf_section_name1(subcs));
942 goto error;
943 }
944
945 if (!fr_assignment_op[token]) {
946 cf_log_err(ci, "Invalid operator '%s'", fr_tokens[token]);
947 goto error;
948 }
949
950 MEM(map = map_alloc(parent_ctx, parent));
951 map->op = token;
952 map->ci = ci;
953
954 /*
955 * The LHS MUST be an attribute name.
956 * map_afrom_cp() allows for dynamic
957 * names, but for simplicity we forbid
958 * them for now. Once the functionality
959 * is tested and used, we can allow that.
960 */
961 slen = tmpl_afrom_attr_str(map, NULL, &map->lhs, cf_section_name1(subcs), &our_lhs_rules);
962 if (slen <= 0) {
963 cf_log_err(ci, "Failed parsing attribute reference for list %s - %s",
964 cf_section_name1(subcs), fr_strerror());
965 talloc_free(map);
966 goto error; /* re-do "goto marker" stuff to print out spaces ? */
967 }
968
969 /*
970 * The LHS MUST be an attribute reference
971 * for now.
972 */
973 if (!tmpl_is_attr(map->lhs)) {
974 cf_log_err(ci, "Left side of group '%s' is NOT an attribute reference",
975 map->lhs->name);
976 talloc_free(map);
977 goto error; /* re-do "goto marker" stuff to print out spaces ? */
978 }
979
980 if (tmpl_attr_tail_da(map->lhs)->flags.is_unknown) {
981 cf_log_err(ci, "Unknown attribute '%s'", map->lhs->name);
982 talloc_free(map);
983 goto error; /* re-do "goto marker" stuff to print out spaces ? */
984 }
985
986 /*
987 * The leaf reference of the outer section
988 * is used as the parsing context of the
989 * inner section.
990 */
991 child_lhs_rules.attr.namespace = tmpl_attr_tail_da(map->lhs);
992
993 /*
994 * Groups MAY change dictionaries. If so, then swap the dictionary and the parent.
995 */
996 if (child_lhs_rules.attr.namespace->type == FR_TYPE_GROUP) {
997 fr_dict_attr_t const *ref;
998 fr_dict_t const *dict, *internal;
999
1000 ref = fr_dict_attr_ref(child_lhs_rules.attr.namespace);
1001 dict = fr_dict_by_da(ref);
1002 internal = fr_dict_internal();
1003
1004 if (dict != internal) {
1005 if (dict != child_lhs_rules.attr.dict_def) {
1006 child_lhs_rules.attr.dict_def = dict;
1007 child_lhs_rules.attr.namespace = ref;
1008 }
1009 } else {
1010 /*
1011 * We're internal: don't use it, and instead rely on dict_def.
1012 */
1013 child_lhs_rules.attr.namespace = NULL;
1014 }
1015 }
1016
1017 if (fr_type_is_structural(tmpl_attr_tail_da(map->lhs)->type)) {
1018 /*
1019 * This prints out any relevant error
1020 * messages. We MAY want to print out
1021 * additional ones, but that might get
1022 * complex and confusing.
1023 *
1024 * We call out internal _map_afrom_cs()
1025 * function, in order to pass in the
1026 * correct parent map.
1027 */
1028 if (_map_afrom_cs(map, &child_list, map, cf_item_to_section(ci),
1029 &child_lhs_rules, rhs_rules, validate, uctx, max, false, edit) < 0) {
1030 fail:
1031 map_list_talloc_free(&child_list);
1032 talloc_free(map);
1033 goto error;
1034 }
1035 map_list_move(&map->child, &child_list);
1036 } else {
1038
1039 /*
1040 * foo := { a, b, c }
1041 *
1042 * @todo - maybe lhs_rules? But definitely not child_lhs_rules.
1043 *
1044 * @todo - this code is taken from compile_edit_section(), we might want
1045 * to just move that code here, for fewer special cases.
1046 */
1047 if (map_list_afrom_cs(map, &child_list, cf_item_to_section(ci), rhs_rules, NULL, NULL, max) < 0) {
1048 goto fail;
1049 }
1050
1051 if ((map->op != T_OP_SET) && !map_list_num_elements(&child_list)) {
1052 cf_log_err(cs, "Cannot use operator '%s' for assigning empty list to '%s' data type.",
1053 fr_tokens[map->op], fr_type_to_str(tmpl_attr_tail_da(map->lhs)->type));
1054 goto fail;
1055 }
1056
1057 map_list_move(&map->child, &child_list);
1058 }
1059
1060 MAP_VERIFY(map);
1061 goto next;
1062 }
1063
1064 if (!cf_item_is_pair(ci)) {
1065 cf_log_err(ci, "Entry is not in \"attribute = value\" format");
1066 goto error;
1067 }
1068
1069 cp = cf_item_to_pair(ci);
1070 fr_assert(cp != NULL);
1071
1072 /*
1073 * Over-ride RHS rules for
1074 *
1075 * reply += {
1076 * User-Name = User-Name
1077 * }
1078 *
1079 * Which looks stupid. Instead we require
1080 *
1081 * reply += {
1082 * User-Name = request.User-Name
1083 * }
1084 *
1085 * On the other hand, any xlats on the RHS don't use the full path. :( And we still need
1086 * to allow relative attribute references via ".foo", when updating structures.
1087 */
1088 our_rhs_rules = rhs_rules;
1089 if (edit && (rhs_rules->attr.list_def != child_lhs_rules.attr.list_def)) {
1090 char const *value = cf_pair_value(cp);
1091
1092 if (value && (*value == '&')) {
1093 child_rhs_rules.attr.list_presence = TMPL_ATTR_LIST_REQUIRE;
1094 our_rhs_rules = &child_rhs_rules;
1095 }
1096 }
1097
1098 if (map_afrom_cp(parent_ctx, &map, parent, cp, &child_lhs_rules, our_rhs_rules, edit) < 0) {
1099 cf_log_err(ci, "Failed creating map from '%s = %s'",
1100 cf_pair_attr(cp), cf_pair_value(cp));
1101 goto error;
1102 }
1103
1104 MAP_VERIFY(map);
1105
1106 /*
1107 * Check the types in the map are valid
1108 */
1109 if (validate && (validate(map, uctx) < 0)) goto error;
1110
1111 next:
1112 parent_ctx = map;
1113 map_list_insert_tail(out, map);
1114 }
1115
1116 return 0;
1117
1118}
1119
1120/** Convert a config section into an attribute map.
1121 *
1122 * For "update" sections, Uses 'name2' of section to set default request and lists.
1123 *
1124 * @param[in] ctx for talloc.
1125 * @param[out] out Where to store the allocated map.
1126 * @param[in] cs the update section
1127 * @param[in] lhs_rules rules for parsing LHS attribute references.
1128 * @param[in] rhs_rules rules for parsing RHS attribute references.
1129 * @param[in] validate map using this callback (may be NULL).
1130 * @param[in] uctx to pass to callback.
1131 * @param[in] max number of mappings to process.
1132 * @return
1133 * - 0 on success.
1134 * - -1 on failure.
1135 */
1136int map_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
1137 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
1138 map_validate_t validate, void *uctx,
1139 unsigned int max)
1140{
1141 bool update;
1142 char const *name2;
1143 map_t *parent = NULL;
1144
1145 name2 = cf_section_name1(cs);
1146 update = (name2 && (strcmp(name2, "update") == 0));
1147
1148 if (ctx) parent = talloc_get_type(ctx, map_t);
1149
1150 return _map_afrom_cs(ctx, out, parent, cs, lhs_rules, rhs_rules, validate, uctx, max, update, false);
1151}
1152
1153/** Convert a config section into an attribute map for editing
1154 *
1155 * @param[in] ctx for talloc.
1156 * @param[out] out Where to store the allocated map.
1157 * @param[in] cs the update section
1158 * @param[in] lhs_rules rules for parsing LHS attribute references.
1159 * @param[in] rhs_rules rules for parsing RHS attribute references.
1160 * @param[in] validate map using this callback (may be NULL).
1161 * @param[in] uctx to pass to callback.
1162 * @param[in] max number of mappings to process.
1163 * @return
1164 * - 0 on success.
1165 * - -1 on failure.
1166 */
1167int map_afrom_cs_edit(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
1168 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
1169 map_validate_t validate, void *uctx,
1170 unsigned int max)
1171{
1172 map_t *parent = NULL;
1173
1174 if (ctx) parent = talloc_get_type(ctx, map_t);
1175
1176 return _map_afrom_cs(ctx, out, parent, cs, lhs_rules, rhs_rules, validate, uctx, max, false, true);
1177}
1178
1179/** Convert CONFIG_PAIR (which may contain refs) to map_t.
1180 *
1181 * Treats the CONFIG_PAIR name as a value.
1182 *
1183 * Treatment of left operand depends on quotation, barewords are treated as
1184 * attribute , double quoted values are treated as expandable strings,
1185 * single quoted values are treated as literal strings.
1186 *
1187 * Return must be freed with talloc_free
1188 *
1189 * @param[in] ctx for talloc.
1190 * @param[in] out Where to write the pointer to the new #map_t.
1191 * @param[in] parent the parent map
1192 * @param[in] cp to convert to map.
1193 * @param[in] t_rules rules for parsing name
1194 * @return
1195 * - #map_t if successful.
1196 * - NULL on error.
1197 */
1198static int map_value_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp,
1199 tmpl_rules_t const *t_rules)
1200{
1201 map_t *map;
1202 char const *attr, *marker_subject;
1203 ssize_t slen;
1205
1206 *out = NULL;
1207
1208 if (!cp) return -1;
1209
1210 MEM(map = map_alloc(ctx, parent));
1211 map->op = T_OP_EQ; /* @todo - should probably be T_INVALID */
1212 map->ci = cf_pair_to_item(cp);
1213
1214 attr = cf_pair_attr(cp);
1215
1216 /*
1217 * LHS may be an expansion (that expands to an attribute reference)
1218 * or an attribute reference. Quoting determines which it is.
1219 */
1221 switch (type) {
1225 slen = tmpl_afrom_substr(ctx, &map->lhs,
1226 &FR_SBUFF_IN(attr, talloc_strlen(attr)),
1227 type,
1228 value_parse_rules_unquoted[type], /* We're not searching for quotes */
1229 t_rules);
1230 if (slen <= 0) {
1231 char *spaces, *text;
1232
1233 marker:
1234 marker_subject = attr;
1235 fr_canonicalize_error(ctx, &spaces, &text, slen, marker_subject);
1236 cf_log_err(cp, "%s", text);
1237 cf_log_perr(cp, "%s^", spaces);
1238
1240 talloc_free(text);
1241 goto error;
1242 }
1243 break;
1244
1246 fr_strerror_const("Invalid location for regular expression");
1247 slen = 0;
1248 goto marker;
1249
1250 default:
1251 /*
1252 * Let the tmpl code determine if it's an attribute reference or else is a raw value.
1253 */
1254 slen = tmpl_afrom_substr(ctx, &map->lhs, &FR_SBUFF_IN(attr, talloc_strlen(attr)), T_BARE_WORD, NULL, t_rules);
1255 if (slen <= 0) goto marker;
1256
1257 if (tmpl_is_attr(map->lhs) && (tmpl_attr_unknown_add(map->lhs) < 0)) {
1258 fr_strerror_printf("Failed defining attribute %s", map->lhs->name);
1259 goto error;
1260 }
1261 break;
1262 }
1263
1264 MAP_VERIFY(map);
1265
1266 *out = map;
1267
1268 return 0;
1269
1270error:
1271 talloc_free(map);
1272 return -1;
1273}
1274
1275/*
1276 * Where the RHS are all values.
1277 */
1278static int _map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, map_t *parent, CONF_SECTION *cs,
1279 tmpl_rules_t const *t_rules,
1280 map_validate_t validate, void *uctx,
1281 unsigned int max)
1282{
1283 CONF_ITEM *ci;
1284 CONF_PAIR *cp;
1285
1286 unsigned int total = 0;
1287 map_t *map;
1288 TALLOC_CTX *parent_ctx;
1289
1290 /*
1291 * The first map has ctx as the parent context.
1292 * The rest have the previous map as the parent context.
1293 */
1294 parent_ctx = ctx;
1295
1296 for (ci = cf_item_next(cs, NULL);
1297 ci != NULL;
1298 ci = cf_item_next(cs, ci)) {
1299 if (total++ == max) {
1300 cf_log_err(ci, "Map size exceeded");
1301 error:
1302 /*
1303 * Free in reverse as successive entries have their
1304 * prececessors as talloc parent contexts
1305 */
1306 map_list_talloc_reverse_free(out);
1307 return -1;
1308 }
1309
1310 if (cf_item_is_section(ci)) {
1311 cf_log_err(ci, "Cannot create sub-lists");
1312 goto error;
1313 }
1314
1315 cp = cf_item_to_pair(ci);
1316 fr_assert(cp != NULL);
1317
1318 if (map_value_afrom_cp(parent_ctx, &map, parent, cp, t_rules) < 0) {
1319 fr_assert(0);
1320 cf_log_err(ci, "Failed creating map from '%s'", cf_pair_attr(cp));
1321 goto error;
1322 }
1323
1324 MAP_VERIFY(map);
1325
1326 /*
1327 * Check the types in the map are valid
1328 */
1329 if (validate && (validate(map, uctx) < 0)) goto error;
1330
1331 parent_ctx = map;
1332 map_list_insert_tail(out, map);
1333 }
1334
1335 return 0;
1336
1337}
1338
1339/** Convert a config section into a list of { a, b, c, d, ... }
1340 *
1341 * @param[in] ctx for talloc.
1342 * @param[out] out Where to store the allocated map.
1343 * @param[in] cs the update section
1344 * @param[in] t_rules rules for parsing the data.
1345 * @param[in] validate map using this callback (may be NULL).
1346 * @param[in] uctx to pass to callback.
1347 * @param[in] max number of mappings to process.
1348 * @return
1349 * - 0 on success.
1350 * - -1 on failure.
1351 */
1352int map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
1353 tmpl_rules_t const *t_rules,
1354 map_validate_t validate, void *uctx,
1355 unsigned int max)
1356{
1357 map_t *parent = NULL;
1358
1359 if (ctx) parent = talloc_get_type(ctx, map_t);
1360
1361 return _map_list_afrom_cs(ctx, out, parent, cs, t_rules, validate, uctx, max);
1362}
1363
1364/** Convert a value box to a map
1365 *
1366 * This is mainly used in IO modules, where another function is used to convert
1367 * between the foreign value type and internal values, and the destination
1368 * attribute is provided as a string.
1369 *
1370 * @param[in] ctx for talloc
1371 * @param[out] out Where to store the head of the map.
1372 * @param[in] lhs of the operation
1373 * @param[in] lhs_quote type of the LHS string
1374 * @param[in] lhs_rules rules that control parsing of the LHS string.
1375 * @param[in] op the operation to perform
1376 * @param[in] rhs of the operation
1377 * @param[in] steal_rhs_buffs Whether we attempt to save allocs by stealing the buffers
1378 * from the rhs #fr_value_box_t.
1379 * @return
1380 * - #map_t if successful.
1381 * - NULL on error.
1382 */
1383int map_afrom_value_box(TALLOC_CTX *ctx, map_t **out,
1384 char const *lhs, fr_token_t lhs_quote, tmpl_rules_t const *lhs_rules,
1385 fr_token_t op,
1386 fr_value_box_t *rhs, bool steal_rhs_buffs)
1387{
1388 ssize_t slen;
1389 map_t *map;
1390
1391 map = map_alloc(ctx, NULL);
1392
1393 slen = tmpl_afrom_substr(map, &map->lhs,
1394 &FR_SBUFF_IN_STR(lhs),
1395 lhs_quote,
1396 NULL,
1397 lhs_rules);
1398 if (slen < 0) {
1399 error:
1400 talloc_free(map);
1401 return -1;
1402 }
1403
1404 map->op = op;
1405
1406 if (tmpl_afrom_value_box(map, &map->rhs, rhs, steal_rhs_buffs) < 0) goto error;
1407
1408 MAP_VERIFY(map);
1409 *out = map;
1410
1411 return 0;
1412}
1413
1414/** Convert a value pair string to valuepair map
1415 *
1416 * Takes a valuepair string with list and request qualifiers and converts it into a
1417 * #map_t.
1418 *
1419 * Attribute string is in the format (where @verbatim <qu> @endverbatim is a quotation char ['"]):
1420 @verbatim
1421 [<list>.][<qu>]<attribute>[<qu>] <op> [<qu>]<value>[<qu>]
1422 @endverbatim
1423 *
1424 * @param[in] ctx where to allocate the map.
1425 * @param[out] out Where to write the new map.
1426 * @param[in] vp_str string to parse.
1427 * @param[in] lhs_rules rules for parsing LHS attribute references.
1428 * @param[in] rhs_rules rules for parsing RHS attribute references.
1429 * @return
1430 * - 0 on success.
1431 * - < 0 on error.
1432 */
1433int map_afrom_attr_str(TALLOC_CTX *ctx, map_t **out, char const *vp_str,
1434 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules)
1435{
1436 fr_sbuff_t sbuff = FR_SBUFF_IN(vp_str, strlen(vp_str));
1437
1439 lhs_rules, rhs_rules, NULL) < 0) {
1440 return -1;
1441 }
1442
1443 if (!fr_cond_assert(*out != NULL)) return -1;
1444
1445 if (!tmpl_is_attr((*out)->lhs)) {
1446 TALLOC_FREE(*out);
1447 fr_strerror_const("Left operand must be an attribute");
1448 return -1;
1449 }
1450
1451 return 0;
1452}
1453
1454/** Convert a fr_pair_t into a map
1455 *
1456 * @param[in] ctx where to allocate the map.
1457 * @param[out] out Where to write the new map (must be freed with talloc_free()).
1458 * @param[in] vp to convert.
1459 * @param[in] rules to insert attributes into.
1460 * @return
1461 * - 0 on success.
1462 * - -1 on failure.
1463 */
1464int map_afrom_vp(TALLOC_CTX *ctx, map_t **out, fr_pair_t *vp, tmpl_rules_t const *rules)
1465{
1466 char buffer[256];
1467 fr_sbuff_t buffer_sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1468
1469 map_t *map;
1470
1471 map = map_alloc(ctx, NULL);
1472 if (!map) {
1473 oom:
1474 fr_strerror_const("Out of memory");
1475 return -1;
1476 }
1477
1478 /*
1479 * Allocate the LHS
1480 */
1481 map->lhs = tmpl_alloc(map, TMPL_TYPE_ATTR, T_BARE_WORD, NULL, 0);
1482 if (!map->lhs) {
1483 talloc_free(map);
1484 goto oom;
1485 }
1486
1488
1489 tmpl_attr_set_request_ref(map->lhs, rules->attr.request_def);
1490 tmpl_attr_set_list(map->lhs, rules->attr.list_def);
1491
1492 tmpl_print(&buffer_sbuff, map->lhs, NULL);
1493 tmpl_set_name(map->lhs, T_BARE_WORD, fr_sbuff_start(&buffer_sbuff), -1);
1494
1495 /*
1496 * Allocate the RHS
1497 */
1498 map->rhs = tmpl_alloc(map, TMPL_TYPE_DATA, T_BARE_WORD, NULL, -1);
1499 if (!map->rhs) {
1500 talloc_free(map);
1501 goto oom;
1502 }
1503
1504 switch (vp->vp_type) {
1505 case FR_TYPE_QUOTED:
1506 tmpl_set_name_printf(map->rhs, T_DOUBLE_QUOTED_STRING, "%pV", &vp->data);
1507 break;
1508
1509 default:
1510 tmpl_set_name_printf(map->rhs, T_BARE_WORD, "%pV", &vp->data);
1511 break;
1512 }
1513
1514 if (unlikely(fr_value_box_copy(map->rhs, tmpl_value(map->rhs), &vp->data) < 0)) {
1515 talloc_free(map);
1516 return -1;
1517 }
1518
1519 *out = map;
1520
1521 return 0;
1522}
1523
1524/** Process map which has exec as a src
1525 *
1526 * Evaluate maps which specify exec as a src. This may be used by various sorts of update sections, and so
1527 * has been broken out into it's own function.
1528 *
1529 * @param[in,out] ctx to allocate new #fr_pair_t (s) in.
1530 * @param[out] out Where to write the #fr_pair_t (s).
1531 * @param[in] request structure (used only for talloc).
1532 * @param[in] map the map. The LHS (dst) must be #TMPL_TYPE_ATTR.
1533 * The RHS (src) must be #TMPL_TYPE_EXEC.
1534 * @return
1535 * - 0 on success.
1536 * - -1 on failure.
1537 */
1538static int map_exec_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map)
1539{
1540 int result;
1541 fr_pair_t *vp;
1542 fr_pair_list_t *input_pairs = NULL;
1543 char answer[1024];
1544
1546
1547 MAP_VERIFY(map);
1548
1549 fr_assert(map->rhs); /* Quite clang scan */
1550 fr_assert(tmpl_is_exec(map->rhs));
1551 fr_assert(tmpl_is_attr(map->lhs));
1552
1553 if (fr_type_is_structural(tmpl_attr_tail_da(map->lhs)->type)) {
1554 REDEBUG("Cannot assign `exec` output to structural attribute %s", map->lhs->name);
1555 return -1;
1556 }
1557
1558 /*
1559 * We always put the request pairs into the environment
1560 */
1561 input_pairs = tmpl_list_head(request, request_attr_request);
1562
1563 /*
1564 * Automagically switch output type depending on our destination
1565 * @todo - If dst is a list, then we create attributes from the output of the program
1566 * if dst is an attribute, then we create an attribute of that type and then
1567 * call fr_pair_value_from_str on the output of the script.
1568 */
1569 result = radius_exec_program_legacy(answer, sizeof(answer),
1570 request, map->rhs->name, input_pairs ? input_pairs : NULL,
1572 if (result != 0) {
1573 REDEBUG("Exec failed with code (%i)", result);
1574 return -1;
1575 }
1576
1577 /*
1578 * @todo - we completely ignore the operator here :( Arguably the caller should be calling ONLY
1579 * the legacy pair move functions with the results of this function.
1580 */
1582 vp->op = map->op;
1583 if (fr_pair_value_from_str(vp, answer, strlen(answer), &fr_value_unescape_single, false) < 0) {
1584 RPEDEBUG("Failed parsing exec output");
1585 talloc_free(vp);
1586 return -2;
1587 }
1589
1590 return 0;
1591}
1592
1593/** Convert a map to a #fr_pair_t
1594 *
1595 * @param[in,out] ctx to allocate #fr_pair_t (s) in.
1596 * @param[out] out Where to write the #fr_pair_t (s), which may be NULL if not found
1597 * @param[in] request The current request.
1598 * @param[in] map the map. The LHS (dst) has to be #TMPL_TYPE_ATTR.
1599 * @param[in] uctx unused.
1600 * @return
1601 * - 0 on success.
1602 * - -1 on failure.
1603 */
1604int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, UNUSED void *uctx)
1605{
1606 int rcode = 0;
1607 fr_pair_t *n = NULL;
1608 fr_pair_list_t found;
1609 request_t *context = request;
1610 ssize_t slen;
1611 char *str;
1612
1613 fr_pair_list_init(&found);
1615
1616 MAP_VERIFY(map);
1617 if (!fr_cond_assert(map->lhs != NULL)) return -1;
1618
1619 fr_assert(tmpl_is_attr(map->lhs));
1620
1621 /*
1622 * If there's no RHS, then it MUST be an attribute, and
1623 * it MUST be structural. And it MAY have children.
1624 */
1625 if (!map->rhs) {
1626 map_t *child;
1627
1628 if (!tmpl_is_attr(map->lhs)) return -1;
1629
1630 switch (tmpl_attr_tail_da(map->lhs)->type) {
1631 case FR_TYPE_STRUCTURAL:
1632 break;
1633
1634 default:
1635 return -1;
1636 }
1637
1638 /*
1639 * Create the parent attribute, and
1640 * recurse to generate the children into
1641 * vp->vp_group
1642 */
1644 n->op = map->op;
1645
1646 for (child = map_list_next(&map->child, NULL);
1647 child != NULL;
1648 child = map_list_next(&map->child, child)) {
1649 fr_pair_list_t list;
1650
1651 /*
1652 * map_to_vp() frees "out", so we need to
1653 * work around that by creating a
1654 * temporary list.
1655 */
1656 fr_pair_list_init(&list);
1657 if (map_to_vp(n, &list, request, child, NULL) < 0) {
1658 talloc_free(n);
1659 return -1;
1660 }
1661
1662 fr_pair_list_append(&n->vp_group, &list);
1663 }
1664
1666 return 0;
1667 }
1668
1669 /*
1670 * List to list found, this is a special case because we don't need
1671 * to allocate any attributes, just finding the current list, and change
1672 * the op.
1673 */
1676 fr_pair_list_t *from = NULL;
1677
1678 if (tmpl_request_ptr(&context, tmpl_request(map->rhs)) == 0) {
1679 from = tmpl_list_head(context, tmpl_list(map->rhs));
1680 }
1681 if (!from) return 0;
1682
1683 if (fr_pair_list_copy(ctx, &found, from) < 0) return -1;
1684
1685 /*
1686 * List to list copy is empty if the src list has no attributes.
1687 */
1688 if (fr_pair_list_empty(&found)) return 0;
1689
1690 fr_pair_list_foreach(&found, vp) {
1691 vp->op = T_OP_ADD_EQ;
1692 }
1693
1694 fr_pair_list_append(out, &found);
1695
1696 return 0;
1697 }
1698
1699 /*
1700 * And parse the RHS
1701 */
1702 switch (map->rhs->type) {
1703 case TMPL_TYPE_XLAT:
1704 fr_assert(tmpl_is_attr(map->lhs));
1705 fr_assert(tmpl_attr_tail_da(map->lhs)); /* We need to know which attribute to create */
1706 fr_assert(tmpl_xlat(map->rhs) != NULL);
1707
1709
1710 /*
1711 * We do the debug printing because xlat_aeval_compiled
1712 * doesn't have access to the original string. It's been
1713 * mangled during the parsing to an internal data structure
1714 */
1715 RDEBUG2("EXPAND %s", map->rhs->name);
1716 RINDENT();
1717
1718 str = NULL;
1719 slen = xlat_aeval_compiled(request, &str, request, tmpl_xlat(map->rhs), NULL, NULL);
1720 REXDENT();
1721
1722 if (slen < 0) {
1723 rcode = slen;
1724 goto error;
1725 }
1726
1727 RDEBUG2("--> %s", str);
1728
1729 rcode = fr_pair_value_from_str(n, str, strlen(str), NULL, false);
1730 talloc_free(str);
1731 if (rcode < 0) {
1732 goto error;
1733 }
1734
1735 n->op = map->op;
1737 break;
1738
1740 fr_assert(tmpl_is_attr(map->lhs));
1741 fr_assert(tmpl_attr_tail_da(map->lhs)); /* We need to know which attribute to create */
1742
1744
1745 if (fr_pair_value_from_str(n, map->rhs->name, strlen(map->rhs->name), NULL, false) < 0) {
1746 rcode = -1;
1747 goto error;
1748 }
1749 n->op = map->op;
1751 break;
1752
1753 case TMPL_TYPE_ATTR:
1754 {
1755 fr_pair_t *vp;
1756 fr_dcursor_t from;
1757
1759
1760 /*
1761 * @todo should log error, and return -1 for v3.1 (causes update to fail)
1762 */
1763 if (tmpl_copy_pairs(ctx, &found, request, map->rhs) < 0) return 0;
1764
1765 vp = fr_pair_dcursor_init(&from, &found);
1766
1767 /*
1768 * Src/Dst attributes don't match, convert src attributes
1769 * to match dst.
1770 */
1771 if (tmpl_is_attr(map->lhs) && tmpl_attr_tail_da_is_leaf(map->lhs) &&
1772 (tmpl_attr_tail_da(map->rhs)->type != tmpl_attr_tail_da(map->lhs)->type)) {
1773 for (; vp; vp = fr_dcursor_current(&from)) {
1775
1776 if (fr_value_box_cast(n, &n->data,
1777 tmpl_attr_tail_da(map->lhs)->type, tmpl_attr_tail_da(map->lhs), &vp->data) < 0) {
1778 RPEDEBUG("Attribute conversion failed");
1779 fr_pair_list_free(&found);
1780 talloc_free(n);
1781 return -1;
1782 }
1783 vp = fr_dcursor_remove(&from); /* advances cursor */
1784 talloc_free(vp);
1785
1786 fr_assert((n->vp_type != FR_TYPE_STRING) || (n->vp_strvalue != NULL));
1787
1788 n->op = map->op;
1790 }
1791
1792 return 0;
1793 }
1794
1795 /*
1796 * Otherwise we just need to fixup the attribute types
1797 * and operators
1798 */
1799 for (; vp; vp = fr_dcursor_next(&from)) {
1801 vp->op = map->op;
1802 }
1803 fr_pair_list_append(out, &found);
1804 }
1805 break;
1806
1807 case TMPL_TYPE_DATA:
1809 fr_assert(tmpl_is_attr(map->lhs));
1810
1812
1813 if (tmpl_attr_tail_da(map->lhs)->type == tmpl_value_type(map->rhs)) {
1814 if (unlikely(fr_value_box_copy(n, &n->data, tmpl_value(map->rhs)) < 0)) {
1815 rcode = -1;
1816 talloc_free(n);
1817 goto error;
1818 }
1819
1820 } else if (fr_value_box_cast(n, &n->data, n->vp_type, n->da, tmpl_value(map->rhs)) < 0) {
1821 RPEDEBUG("Implicit cast failed");
1822 rcode = -1;
1823 goto error;
1824 }
1825 n->op = map->op;
1827
1828 MAP_VERIFY(map);
1829 break;
1830
1831 /*
1832 * This essentially does the same as rlm_exec xlat, except it's non-configurable.
1833 * It's only really here as a convenience for people who expect the contents of
1834 * backticks to be executed in a shell.
1835 *
1836 * exec string is xlat expanded and arguments are shell escaped.
1837 */
1838 case TMPL_TYPE_EXEC:
1839 return map_exec_to_vp(ctx, out, request, map);
1840
1841 default:
1842 fr_strerror_const("Internal sanity check failure");
1843 rcode = -1;
1844 error:
1845 talloc_free(n);
1846 return rcode;
1847 }
1848
1849 return 0;
1850}
1851
1852#define DEBUG_OVERWRITE(_old, _new) \
1853do {\
1854 if (RDEBUG_ENABLED3) {\
1855 char *our_old; \
1856 char *our_new; \
1857 fr_pair_aprint_value_quoted(request, &our_old, _old, T_DOUBLE_QUOTED_STRING); \
1858 fr_pair_aprint_value_quoted(request, &our_new, _new, T_DOUBLE_QUOTED_STRING); \
1859 RINDENT(); \
1860 RDEBUG3("--> overwriting %s with %s", our_old, our_new); \
1861 REXDENT(); \
1862 talloc_free(our_old); \
1863 talloc_free(our_new); \
1864 } \
1865} while (0)
1866
1867/** Convert #map_t to #fr_pair_t (s) and add them to a #request_t.
1868 *
1869 * Takes a single #map_t, resolves request and list identifiers
1870 * to pointers in the current request, then attempts to retrieve module
1871 * specific value(s) using callback, and adds the resulting values to the
1872 * correct request/list.
1873 *
1874 * @param request The current request.
1875 * @param map specifying destination attribute and location and src identifier.
1876 * @param func to retrieve module specific values and convert them to
1877 * #fr_pair_t.
1878 * @param ctx to be passed to func.
1879 * @return
1880 * - -1 if the operation failed.
1881 * - -2 in the source attribute wasn't valid.
1882 * - 0 on success.
1883 */
1884int map_to_request(request_t *request, map_t const *map, radius_map_getvalue_t func, void *ctx)
1885{
1886 int rcode = 0;
1887 fr_pair_t *dst;
1888 fr_pair_list_t *list, src_list;
1889 request_t *context, *tmp_ctx = NULL;
1890 TALLOC_CTX *parent;
1891 fr_dcursor_t dst_list;
1892
1893 map_t exp_map;
1894 tmpl_t *exp_lhs;
1895 fr_dict_attr_t const *list_ref;
1896
1897 tmpl_dcursor_ctx_t cc = {};
1898
1899 fr_pair_list_init(&src_list);
1900 MAP_VERIFY(map);
1901 fr_assert(map->lhs != NULL);
1902 fr_assert(map->rhs != NULL);
1903
1904 /*
1905 * This function is called only when creating attributes. It cannot be called for conditions.
1906 */
1907 if (fr_comparison_op[map->op]) {
1908 REDEBUG("Invalid operator in %s %s ...", map->lhs->name, fr_tokens[map->op]);
1909 return -1;
1910 }
1911
1912 tmp_ctx = talloc_pool(request, 1024);
1913
1914 /*
1915 * Preprocessing of the LHS of the map.
1916 */
1917 switch (map->lhs->type) {
1918 /*
1919 * Already in the correct form.
1920 */
1921 case TMPL_TYPE_ATTR:
1922 break;
1923
1924 /*
1925 * Everything else gets expanded, then re-parsed as an attribute reference.
1926 *
1927 * This allows the syntax like:
1928 * - "Attr-%{number}" := "value"
1929 */
1930 case TMPL_TYPE_XLAT:
1931 case TMPL_TYPE_EXEC:
1932 {
1933 char *attr_str;
1934 ssize_t slen;
1935
1936 slen = tmpl_aexpand(request, &attr_str, request, map->lhs, NULL, NULL);
1937 if (slen <= 0) {
1938 RPEDEBUG("Left side expansion failed");
1939 fr_assert(!attr_str);
1940 rcode = -1;
1941 goto finish;
1942 }
1943
1944 slen = tmpl_afrom_attr_str(tmp_ctx, NULL, &exp_lhs, attr_str,
1945 &(tmpl_rules_t){
1946 .attr = {
1947 .dict_def = request->local_dict,
1948 .list_def = request_attr_request,
1949 }
1950 });
1951 if (slen <= 0) {
1952 RPEDEBUG("Left side expansion result \"%s\" is not an attribute reference", attr_str);
1953 talloc_free(attr_str);
1954 rcode = -1;
1955 goto finish;
1956 }
1957 talloc_free(attr_str);
1958 fr_assert(tmpl_is_attr(exp_lhs));
1959
1960 memcpy(&exp_map, map, sizeof(exp_map));
1961 exp_map.lhs = exp_lhs;
1962 map = &exp_map;
1963 }
1964 break;
1965
1966 default:
1967 fr_assert(0);
1968 break;
1969 }
1970
1971
1972 /*
1973 * Sanity check inputs. We can have a list or attribute
1974 * as a destination.
1975 */
1976 if (!tmpl_is_attr(map->lhs)) {
1977 REDEBUG("Left side \"%.*s\" of map should be an attr or list but is an %s",
1978 (int)map->lhs->len, map->lhs->name,
1979 tmpl_type_to_str(map->lhs->type));
1980 rcode = -2;
1981 goto finish;
1982 }
1983
1984 context = request;
1985 if (tmpl_request_ptr(&context, tmpl_request(map->lhs)) < 0) {
1986 RPEDEBUG("Mapping \"%.*s\" -> \"%.*s\" cannot be performed due to error in left side of map",
1987 (int)map->rhs->len, map->rhs->name, (int)map->lhs->len, map->lhs->name);
1988 rcode = -2;
1989 goto finish;
1990 }
1991
1992 list_ref = tmpl_list(map->lhs);
1993 list = tmpl_list_head(context, list_ref);
1994 if (!list) {
1995 REDEBUG("Mapping \"%.*s\" -> \"%.*s\" cannot be performed due to to invalid list qualifier \"%s\" in left side of map",
1996 (int)map->rhs->len, map->rhs->name, (int)map->lhs->len, map->lhs->name,
1997 tmpl_list_name(list_ref, "<INVALID>"));
1998 rcode = -2;
1999 goto finish;
2000 }
2001
2004
2005 /*
2006 * The callback should either return -1 to signify operations error,
2007 * -2 when it can't find the attribute or list being referenced, or
2008 * 0 to signify success. It may return "success", but still have no
2009 * VPs to work with.
2010 */
2011 rcode = func(parent, &src_list, request, map, ctx);
2012 if (rcode < 0) {
2013 fr_assert(fr_pair_list_empty(&src_list));
2014 goto finish;
2015 }
2016 if (fr_pair_list_empty(&src_list)) {
2017 RDEBUG2("%.*s skipped: No values available", (int)map->lhs->len, map->lhs->name);
2018 goto finish;
2019 }
2020
2021 /*
2022 * Print the VPs
2023 */
2024#ifndef WITH_VERIFY_PTR
2025 if (RDEBUG_ENABLED)
2026#endif
2027 {
2028 fr_pair_list_foreach(&src_list, vp) {
2029 PAIR_VERIFY(vp);
2030
2031 if (RDEBUG_ENABLED) map_debug_log(request, map, vp);
2032 }
2033 }
2034
2035 /*
2036 * The destination is a list (which is a completely different set of operations)
2037 */
2038 if (tmpl_is_list(map->lhs)) {
2039 switch (map->op) {
2040 case T_OP_CMP_FALSE:
2041 /* We don't need the src VPs (should just be 'ANY') */
2042 fr_assert(fr_pair_list_empty(&src_list));
2043
2044 /* Clear the entire dst list */
2045 fr_pair_list_free(list);
2046 goto finish;
2047
2048 case T_OP_SET:
2049 if (tmpl_is_list(map->rhs)) {
2050 fr_pair_list_free(list);
2051 fr_pair_list_append(list, &src_list);
2052 fr_pair_list_init(&src_list);
2053 } else {
2055
2056 case T_OP_EQ:
2057 fr_assert(tmpl_is_exec(map->rhs));
2059
2060 case T_OP_ADD_EQ:
2061 fr_pair_list_move_op(list, &src_list, T_OP_ADD_EQ);
2062 }
2063 goto finish;
2064
2065 case T_OP_PREPEND:
2066 fr_pair_list_move_op(list, &src_list, T_OP_PREPEND);
2067 goto finish;
2068
2069 default:
2070 rcode = -1;
2071 goto finish;
2072 }
2073 }
2074
2075 /*
2076 * Find the destination attribute. We leave with either
2077 * the dst_list and vp pointing to the attribute or the VP
2078 * being NULL (no attribute at that index).
2079 */
2080 dst = tmpl_dcursor_init(NULL, tmp_ctx, &cc, &dst_list, request, map->lhs);
2081
2082 /*
2083 * The destination is an attribute
2084 */
2085 switch (map->op) {
2086 default:
2087 break;
2088 /*
2089 * !* - Remove all attributes which match dst in the specified list.
2090 * This doesn't use attributes returned by the func(), and immediately frees them.
2091 */
2092 case T_OP_CMP_FALSE:
2093 /* We don't need the src VPs (should just be 'ANY') */
2094 fr_assert(fr_pair_list_empty(&src_list));
2095 if (!dst) goto finish;
2096
2097 /*
2098 * Wildcard: delete all of the matching ones
2099 */
2100 if (tmpl_attr_tail_num(map->lhs) == NUM_UNSPEC) {
2101 fr_pair_delete_by_child_num(list, tmpl_attr_tail_da(map->lhs)->parent, tmpl_attr_tail_da(map->lhs)->attr);
2102 dst = NULL;
2103 /*
2104 * We've found the Nth one. Delete it, and only it.
2105 */
2106 } else {
2107 dst = fr_dcursor_remove(&dst_list);
2108 talloc_free(dst);
2109 }
2110
2111 /*
2112 * Check that the User-Name and User-Password
2113 * caches point to the correct attribute.
2114 */
2115 goto finish;
2116
2117 /*
2118 * -= - Delete attributes in the dst list which match any of the
2119 * src_list attributes.
2120 *
2121 * This operation has two modes:
2122 * - If tmpl_attr_tail_num(map->lhs) > 0, we check each of the src_list attributes against
2123 * the dst attribute, to see if any of their values match.
2124 * - If tmpl_attr_tail_num(map->lhs) == NUM_UNSPEC, we compare all instances of the dst attribute
2125 * against each of the src_list attributes.
2126 */
2127 case T_OP_SUB_EQ:
2128 /* We didn't find any attributes earlier */
2129 if (!dst) {
2130 goto finish;
2131 } else {
2132 fr_pair_list_t free_list;
2133
2134 fr_pair_list_init(&free_list);
2135
2136 /*
2137 * Instance specific[n] delete
2138 */
2139 if (tmpl_attr_tail_num(map->lhs) != NUM_UNSPEC) {
2140 fr_pair_list_foreach(&src_list, vp) {
2141 vp->op = T_OP_CMP_EQ;
2142 rcode = paircmp_pairs(request, vp, dst);
2143 if (rcode == 0) {
2144 dst = fr_dcursor_remove(&dst_list);
2145 fr_pair_append(&free_list, dst);
2146 break;
2147 }
2148 }
2149
2150 goto done_subeq;
2151 }
2152
2153 /*
2154 * All instances[*] delete
2155 */
2156 for (dst = fr_dcursor_current(&dst_list);
2157 dst;
2159 fr_pair_list_foreach(&src_list, vp) {
2160 vp->op = T_OP_CMP_EQ;
2161 rcode = paircmp_pairs(request, vp, dst);
2162 if (rcode == 0) {
2163 dst = fr_dcursor_remove(&dst_list);
2164 fr_pair_append(&free_list, dst);
2165 }
2166 }
2167 }
2168
2169 done_subeq:
2170 fr_pair_list_free(&free_list);
2171 }
2172
2173 rcode = 0;
2174 goto finish;
2175 }
2176
2177 switch (map->op) {
2178 /*
2179 * = - Set only if not already set
2180 */
2181 case T_OP_EQ:
2182 {
2183 tmpl_attr_extent_t *extent = NULL;
2184 fr_dlist_head_t leaf;
2185 fr_dlist_head_t interior;
2186 fr_pair_t *src_vp;
2187
2188 if (dst) {
2189 RDEBUG3("Refusing to overwrite (use :=)");
2190 goto finish;
2191 }
2192
2194 fr_dlist_talloc_init(&interior, tmpl_attr_extent_t, entry);
2195
2196 /*
2197 * Find out what we need to build and build it
2198 */
2199 if ((tmpl_extents_find(tmp_ctx, &leaf, &interior, request, map->lhs) < 0) ||
2200 (tmpl_extents_build_to_leaf_parent(&leaf, &interior, map->lhs) < 0)) {
2201 fr_dlist_talloc_free(&leaf);
2202 fr_dlist_talloc_free(&interior);
2203 rcode = -1;
2204 goto finish;
2205 }
2206
2207 /*
2208 * Need to copy src to all dsts
2209 */
2210 src_vp = fr_pair_list_head(&src_list);
2211 if (!src_vp) {
2212 fr_dlist_talloc_free(&leaf);
2213 rcode = -1;
2214 goto finish;
2215 }
2216
2217 if (fr_dlist_num_elements(&leaf) > 1) {
2218 while ((extent = fr_dlist_tail(&leaf))) {
2219 fr_pair_append(extent->list, fr_pair_copy(extent->list_ctx, src_vp));
2221 }
2222 } else {
2223 extent = fr_dlist_head(&leaf);
2224 fr_pair_append(extent->list, fr_pair_copy(extent->list_ctx, src_vp));
2226 }
2227
2228 /* Free any we didn't insert */
2229 fr_assert(fr_dlist_num_elements(&interior) == 0);
2230 fr_assert(fr_dlist_num_elements(&leaf) == 0);
2231 }
2232 break;
2233
2234 /*
2235 * := - Overwrite existing attribute with last src_list attribute
2236 */
2237 case T_OP_SET:
2238 {
2239 tmpl_attr_extent_t *extent = NULL;
2240 fr_dlist_head_t leaf;
2241 fr_dlist_head_t interior;
2242 fr_pair_t *src_vp;
2243
2244 src_vp = fr_pair_list_tail(&src_list);
2245
2246 if (dst) {
2247 DEBUG_OVERWRITE(dst, src_vp);
2248
2249 fr_pair_reinit_from_da(NULL, dst, src_vp->da);
2250 fr_pair_value_copy(dst, src_vp);
2251 break;
2252 }
2253
2255 fr_dlist_talloc_init(&interior, tmpl_attr_extent_t, entry);
2256
2257 /*
2258 * Find out what we need to build and build it
2259 */
2260 if ((tmpl_extents_find(tmp_ctx, &leaf, &interior, request, map->lhs) < 0) ||
2261 (tmpl_extents_build_to_leaf_parent(&leaf, &interior, map->lhs) < 0)) {
2262 op_set_error:
2263 fr_dlist_talloc_free(&leaf);
2264 fr_dlist_talloc_free(&interior);
2265 rcode = -1;
2266 goto finish;
2267 }
2268
2269 if (fr_dlist_num_elements(&leaf) > 1) {
2270 ERROR("Not yet supported");
2271 goto op_set_error;
2272 }
2273
2274 extent = fr_dlist_head(&leaf);
2275 fr_pair_append(extent->list, fr_pair_copy(extent->list_ctx, src_vp));
2276
2277 fr_assert(fr_dlist_num_elements(&interior) == 0);
2278 fr_dlist_talloc_free(&leaf);
2279 }
2280 break;
2281
2282 /*
2283 * ^= - Prepend src_list attributes to the destination
2284 */
2285 case T_OP_PREPEND:
2286 fr_pair_list_prepend(list, &src_list);
2287 break;
2288
2289 /*
2290 * += - Add all src_list attributes to the destination
2291 */
2292 case T_OP_ADD_EQ:
2293 {
2294 tmpl_attr_extent_t *extent = NULL;
2295 fr_dlist_head_t leaf;
2296 fr_dlist_head_t interior;
2297
2299 fr_dlist_talloc_init(&interior, tmpl_attr_extent_t, entry);
2300
2301 /*
2302 * Find out what we need to build and build it
2303 */
2304 if ((tmpl_extents_find(tmp_ctx, &leaf, &interior, request, map->lhs) < 0) ||
2305 (tmpl_extents_build_to_leaf_parent(&leaf, &interior, map->lhs) < 0)) {
2306 fr_dlist_talloc_free(&leaf);
2307 fr_dlist_talloc_free(&interior);
2308 rcode = -1;
2309 goto finish;
2310 }
2311
2312 if (fr_dlist_num_elements(&leaf) > 1) {
2313 while ((extent = fr_dlist_tail(&leaf))) {
2314 (void) fr_pair_list_copy(extent->list_ctx, extent->list, &src_list);
2316 }
2317 } else {
2318 extent = fr_dlist_head(&leaf);
2319 (void) fr_pair_list_copy(extent->list_ctx, extent->list, &src_list);
2321 }
2322
2323 fr_assert(fr_dlist_num_elements(&interior) == 0);
2324 fr_assert(fr_dlist_num_elements(&leaf) == 0);
2325 }
2326 break;
2327
2328 /*
2329 * Filter operators
2330 */
2331 case T_OP_NE:
2332 case T_OP_CMP_EQ:
2333 case T_OP_GE:
2334 case T_OP_GT:
2335 case T_OP_LE:
2336 case T_OP_LT:
2337 {
2338 fr_pair_t *a;
2339
2342
2343 fr_dcursor_head(&dst_list);
2344
2345 fr_pair_list_foreach(&src_list, b) {
2346 for (a = fr_dcursor_current(&dst_list);
2347 a;
2348 a = fr_dcursor_next(&dst_list)) {
2349 int8_t cmp;
2350
2351 cmp = fr_pair_cmp_by_da(a, b); /* attribute and tag match */
2352 if (cmp > 0) break;
2353 else if (cmp < 0) continue;
2354
2355 cmp = (fr_value_box_cmp_op(map->op, &a->data, &b->data) == 0);
2356 if (cmp != 0) {
2357 a = fr_dcursor_remove(&dst_list);
2358 talloc_free(a);
2359 }
2360 }
2361 if (!a) break; /* end of the list */
2362 }
2363 }
2364 break;
2365
2366 default:
2367 fr_assert(0); /* Should have been caught be the caller */
2368 rcode = -1;
2369 break;
2370 }
2371
2372finish:
2373 fr_pair_list_free(&src_list);
2374 tmpl_dcursor_clear(&cc);
2375 talloc_free(tmp_ctx);
2376 return rcode;
2377}
2378
2379/** Print a map to a string
2380 *
2381 * @param[out] out Buffer to write string to.
2382 * @param[in] map to print.
2383 * @return
2384 * - The number of bytes written to the out buffer.
2385 * - A number >= outlen if truncation has occurred.
2386 */
2388{
2389 fr_sbuff_t our_out = FR_SBUFF(out);
2390
2391 MAP_VERIFY(map);
2392
2393 /*
2394 * Print the lhs
2395 */
2396 if (tmpl_rules_cast(map->lhs)) {
2397 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "<%s>",
2399 }
2400 FR_SBUFF_RETURN(tmpl_print_quoted, &our_out, map->lhs);
2401
2402 /*
2403 * Print separators and operator
2404 */
2405 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
2407 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
2408
2409 /*
2410 * The RHS doesn't matter for many operators
2411 */
2412 if ((map->op == T_OP_CMP_TRUE) || (map->op == T_OP_CMP_FALSE)) {
2413 FR_SBUFF_IN_STRCPY_RETURN(&our_out, "ANY");
2414 FR_SBUFF_SET_RETURN(out, &our_out);
2415 }
2416
2417 /*
2418 * If there's no RHS but no children, then the map was
2419 * invalid.
2420 */
2421 if (!map->rhs) {
2422 fr_assert(!map_list_empty(&map->child));
2423 fr_sbuff_terminate(out);
2424 return 0;
2425 }
2426
2427 if (tmpl_rules_cast(map->rhs)) {
2428 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "<%s>",
2430 }
2431
2432 /*
2433 * Print the RHS.
2434 */
2435 FR_SBUFF_RETURN(tmpl_print_quoted, &our_out, map->rhs);
2436
2437 FR_SBUFF_SET_RETURN(out, &our_out);
2438}
2439
2440/*
2441 * Debug print a map / VP
2442 */
2443void map_debug_log(request_t *request, map_t const *map, fr_pair_t const *vp)
2444{
2445 char *rhs = NULL, *value = NULL;
2446 char buffer[256];
2447
2448 MAP_VERIFY(map);
2449 if (!fr_cond_assert(map->lhs != NULL)) return;
2450 if (!fr_cond_assert(map->rhs != NULL)) return;
2451
2452 fr_assert(vp);
2453
2454 switch (map->rhs->type) {
2455 /*
2456 * Just print the value being assigned
2457 */
2458 default:
2460 fr_pair_aprint_value_quoted(request, &rhs, vp, map->rhs->quote);
2461 break;
2462
2463 case TMPL_TYPE_XLAT:
2464 fr_pair_aprint_value_quoted(request, &rhs, vp, map->rhs->quote);
2465 break;
2466
2467 case TMPL_TYPE_DATA:
2468 fr_pair_aprint_value_quoted(request, &rhs, vp, map->rhs->quote);
2469 break;
2470
2471 case TMPL_TYPE_ATTR:
2472 {
2473 fr_token_t quote;
2474
2475 switch (vp->vp_type) {
2476 case FR_TYPE_QUOTED:
2477 quote = T_DOUBLE_QUOTED_STRING;
2478 break;
2479 default:
2480 quote = T_BARE_WORD;
2481 break;
2482 }
2483
2484 /*
2485 * Not appropriate to use map->rhs->quote here, as that's the quoting
2486 * around the attr ref. The attribute value has no quoting, so we choose
2487 * the quoting based on the data type.
2488 */
2489 fr_pair_aprint_value_quoted(request, &value, vp, quote);
2490 tmpl_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), map->rhs, NULL);
2491 rhs = talloc_typed_asprintf(request, "%s -> %s", buffer, value);
2492 }
2493 break;
2494 }
2495
2496 switch (map->lhs->type) {
2497 case TMPL_TYPE_ATTR:
2498 tmpl_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), map->lhs, NULL);
2499 RDEBUG2("%s %s %s", buffer, fr_table_str_by_value(fr_tokens_table, vp ? vp->op : map->op, "<INVALID>"), rhs);
2500 break;
2501
2502 default:
2503 break;
2504 }
2505
2506 /*
2507 * Must be LIFO free order so we don't leak pool memory
2508 */
2509 talloc_free(rhs);
2511}
2512
2513/** Convert a fr_pair_t into a map
2514 *
2515 * @param[in] ctx where to allocate the map.
2516 * @param[out] out Where to write the new map (must be freed with talloc_free()).
2517 * @param[in,out] parent_p the parent map, updated for relative maps
2518 * @param[in] request the request
2519 * @param[in] lhs of map
2520 * @param[in] op_str operator for map
2521 * @param[in] rhs of map
2522 * @param[in] lhs_rules for parsing the LHS
2523 * @param[in] rhs_rules for parsing the RHS
2524 * @param[in] bare_word_only RHS is bare words, and nothing else.
2525 * @return
2526 * - 0 on success.
2527 * - -1 on failure.
2528 */
2529int map_afrom_fields(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, request_t *request,
2530 char const *lhs, char const *op_str, char const *rhs,
2531 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, bool bare_word_only)
2532{
2533 ssize_t slen;
2534 fr_token_t quote, op;
2535 map_t *map;
2536 map_t *parent = *parent_p;
2537 tmpl_rules_t my_lhs_rules, my_rhs_rules;
2538
2540 if (op == T_INVALID) {
2541 fr_strerror_printf("Invalid operator '%s'", op_str);
2542 return -1;
2543 }
2544
2545 /*
2546 * Reply items can be expanded. Check items cannot be,
2547 * unless the operator is a comparison operator.
2548 */
2549 if (!bare_word_only) {
2550 if (!fr_assignment_op[op]) {
2551 fr_strerror_printf("Invalid operator '%s' for assignment in reply item", op_str);
2552 return -1;
2553 }
2554
2555 } else if (!fr_assignment_op[op] && !fr_comparison_op[op]) {
2556 fr_strerror_printf("Invalid operator '%s' for check item", op_str);
2557 return -1;
2558 }
2559
2560 my_lhs_rules = *lhs_rules;
2561 lhs_rules = &my_lhs_rules;
2562
2563 /*
2564 * We're only called from SQL. If the default list is request, then we only use that for
2565 * comparisons. We rewrite assignments to use the control list.
2566 *
2567 * @todo - as we expand the use of this function, perhaps add another argument which controls
2568 * this flag. But this function already has parameter overload :(
2569 */
2570 if (fr_assignment_op[op] && (lhs_rules->attr.list_def == request_attr_request)) {
2571 my_lhs_rules.attr.list_def = request_attr_control;
2572 }
2573
2574 /*
2575 * One '.' means "the current parent".
2576 */
2577 if (*lhs == '.') {
2578 if (!parent) {
2579 no_parent:
2580 fr_strerror_const("Unexpected location for relative attribute - no parent attribute exists");
2581 return -1;
2582 }
2583 lhs++;
2584
2585 /*
2586 * Multiple '.' means "go to our parents parent".
2587 */
2588 while (*lhs == '.') {
2589 if (!parent) goto no_parent;
2590
2591 parent = parent->parent;
2592 lhs++;
2593 }
2594
2595 /*
2596 * Child elements can only be "=".
2597 */
2598 if (parent) {
2599 if (fr_comparison_op[op]) {
2600 fr_strerror_const("Comparison operators cannot be used inside of structural data types");
2601 return -1;
2602 }
2603
2604 if (op != T_OP_EQ) {
2605 fr_strerror_const("Invalid operator inside of structural data type - must be '='");
2606 return -1;
2607 }
2608 }
2609 }
2610
2611 MEM(map = map_alloc(ctx, parent));
2612 map->op = op;
2613
2614 /*
2615 * Start looking in the correct parent, not in whatever we were handed.
2616 */
2617 if (parent) {
2619 my_lhs_rules.attr.namespace = tmpl_attr_tail_da(parent->lhs);
2620
2621 slen = tmpl_afrom_attr_substr(map, NULL, &map->lhs, &FR_SBUFF_IN_STR(lhs),
2623 } else {
2624 /*
2625 * There's no '.', so this
2626 * attribute MUST come from the
2627 * root of the dictionary tree.
2628 */
2629 parent = NULL;
2630
2631 /*
2632 * Allocate the LHS, which must be an attribute.
2633 *
2634 * @todo - track relative attributes, which begin with a '.'
2635 */
2636 slen = tmpl_afrom_attr_str(map, NULL, &map->lhs, lhs, lhs_rules);
2637 }
2638 if (slen <= 0) {
2639 error:
2640 talloc_free(map);
2641 return -1;
2642 }
2643
2644 if (tmpl_attr_tail_is_unknown(map->lhs) && tmpl_attr_unknown_add(map->lhs) < 0) {
2645 fr_strerror_printf("Failed creating attribute %s", map->lhs->name);
2646 goto error;
2647 }
2648
2649 my_rhs_rules = *rhs_rules;
2650 my_rhs_rules.at_runtime = true;
2651 my_rhs_rules.xlat.runtime_el = unlang_interpret_event_list(request);
2652 my_rhs_rules.enumv = tmpl_attr_tail_da(map->lhs);
2653
2654 /*
2655 * LHS is a structureal type. The RHS is either empty (create empty LHS), or it's a string
2656 * containing a list of attributes to create.
2657 */
2658 if (!fr_type_is_leaf(my_rhs_rules.enumv->type)) {
2659 my_rhs_rules.enumv = NULL;
2660 }
2661
2662 /*
2663 * Try to figure out what we should do with the RHS.
2664 */
2665 if ((map->op == T_OP_CMP_TRUE) || (map->op == T_OP_CMP_FALSE)) {
2666 /*
2667 * These operators require a hard-coded string on the RHS.
2668 */
2669 if (strcmp(rhs, "ANY") != 0) {
2670 fr_strerror_printf("Invalid value %s for operator %s", rhs, fr_tokens[map->op]);
2671 goto error;
2672 }
2673
2674 if (tmpl_afrom_value_box(map, &map->rhs, fr_box_strvalue("ANY"), false) < 0) goto error;
2675
2676 } else if (bare_word_only) {
2677 fr_value_box_t *vb;
2678
2679 /*
2680 * No value, or no enum, parse it as a bare-word string.
2681 */
2682 if (!rhs[0] || !my_rhs_rules.enumv) goto do_bare_word;
2683
2684 MEM(vb = fr_value_box_alloc(map, my_rhs_rules.enumv->type, my_rhs_rules.enumv));
2685
2686 /*
2687 * It MUST be the given data type.
2688 */
2689 slen = fr_value_box_from_str(map, vb, my_rhs_rules.enumv->type, my_rhs_rules.enumv,
2690 rhs, strlen(rhs), NULL);
2691 if (slen <= 0) goto error;
2692
2693 if (tmpl_afrom_value_box(map, &map->rhs, vb, true) < 0) {
2694 goto error;
2695 }
2696
2697 } else if (rhs[0] == '"') {
2698 /*
2699 * We've been asked to expand the RHS. Passwords like
2700 *
2701 * "%{Calling-Station-ID}"
2702 *
2703 * might not do what you want.
2704 */
2705 quote = T_DOUBLE_QUOTED_STRING;
2706 goto parse_quoted;
2707
2708 } else if (rhs[0] == '\'') {
2709 size_t len;
2710
2711 quote = T_SINGLE_QUOTED_STRING;
2712
2713 parse_quoted:
2714 len = strlen(rhs + 1);
2715 if (len == 1) {
2716 if (rhs[1] != rhs[0]) {
2717 fr_strerror_const("Invalid string on right side");
2718 goto error;
2719 }
2720
2721 rhs = "";
2722 goto alloc_empty;
2723 }
2724
2725 slen = tmpl_afrom_substr(map, &map->rhs, &FR_SBUFF_IN(rhs + 1, len),
2726 quote, value_parse_rules_quoted[quote], &my_rhs_rules);
2727 if (slen < 0) {
2728 REDEBUG3("Failed parsing right-hand side as quoted string.");
2729 fail_rhs:
2730 fr_strerror_printf("Failed parsing right-hand side, %s", fr_strerror());
2731 goto error;
2732 }
2733
2734 fr_assert((size_t) slen <= (len + 1));
2735
2736 if (rhs[slen + 1] != rhs[0]) {
2737 fr_strerror_printf("Failed parsing right-hand side, missing end quote in ... %s", rhs);
2738 goto error;
2739 }
2740
2741 if (rhs[slen + 2]) {
2742 fr_strerror_const("Failed parsing right-hand side, unexpected data after end quote");
2743 goto error;
2744 }
2745
2746 if (slen == 0) {
2747 rhs = "";
2748 goto alloc_empty;
2749 }
2750
2751 } else if (rhs[0] == '&') {
2752 /*
2753 * No enums here.
2754 */
2756
2757 parse_as_attr:
2758 my_rhs_rules.enumv = NULL;
2759
2760 slen = tmpl_afrom_attr_str(map, NULL, &map->rhs, rhs, rhs_rules);
2761 if (slen <= 0) {
2762 REDEBUG3("Failed parsing right-hand side as attribute.");
2763 goto fail_rhs;
2764 }
2765
2766 } else if (!rhs[0] || !my_rhs_rules.enumv || (my_rhs_rules.enumv->type == FR_TYPE_STRING)) {
2767 do_bare_word:
2768 quote = T_BARE_WORD;
2769
2770 if (tmpl_attr_tail_da_is_structural(map->lhs) && !*rhs) goto done;
2771
2772 alloc_empty:
2773 MEM(map->rhs = tmpl_alloc(map, TMPL_TYPE_DATA, quote, rhs, strlen(rhs)));
2774
2775 /*
2776 * Create it when we have
2777 *
2778 * my-struct = ""
2779 */
2780 (void) fr_value_box_strdup(map->rhs, tmpl_value(map->rhs), NULL, rhs, false);
2781
2782 } else {
2783 /*
2784 * Parse it as the given data type.
2785 */
2786 slen = tmpl_afrom_substr(map, &map->rhs, &FR_SBUFF_IN_STR(rhs),
2788 if (slen <= 0) {
2789 goto parse_as_attr;
2790 }
2791
2792 /*
2793 * Xlat expansions are cast to strings for structural data types.
2794 */
2795 if (tmpl_attr_tail_da_is_structural(map->lhs) && (tmpl_is_xlat(map->rhs))) {
2797 }
2798 }
2799
2800 if (tmpl_needs_resolving(map->rhs)) {
2801 tmpl_res_rules_t tr_rules = (tmpl_res_rules_t) {
2802 .dict_def = lhs_rules->attr.dict_def,
2803 .enumv = tmpl_attr_tail_da(map->lhs)
2804 };
2805
2807
2808 if (tmpl_resolve(map->rhs, &tr_rules) < 0) {
2809 REDEBUG3("Failed resolving right-hand side.");
2810 goto fail_rhs;
2811 }
2812 }
2813
2814 /*
2815 * @todo - check that the entire string was parsed.
2816 */
2817
2818done:
2819 /*
2820 * If the tail is a leaf, we don't change parent.
2821 * Otherwise the structural attribute is the new parent.
2822 */
2823 if (tmpl_attr_tail_da_is_leaf(map->lhs)) {
2824 *parent_p = parent;
2825 } else {
2826 *parent_p = map;
2827 }
2828
2829 MAP_VERIFY(map);
2830
2831 if (parent) map_list_insert_tail(&parent->child, map);
2832 *out = map;
2833
2834 return 0;
2835}
static int const char char buffer[256]
Definition acutest.h:578
int n
Definition acutest.h:579
static int context
Definition radmin.c:71
#define RCSID(id)
Definition build.h:487
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
Common header for all CONF_* types.
Definition cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:72
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
Definition cf_util.c:631
fr_token_t cf_pair_attr_quote(CONF_PAIR const *pair)
Return the value (lhs) quoting of a pair.
Definition cf_util.c:1603
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1170
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:683
fr_token_t cf_pair_operator(CONF_PAIR const *pair)
Return the operator of a pair.
Definition cf_util.c:1588
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
Definition cf_util.c:1618
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
Definition cf_util.c:617
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:663
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
Definition cf_util.c:1229
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1574
CONF_ITEM * cf_pair_to_item(CONF_PAIR const *cp)
Cast a CONF_PAIR to a CONF_ITEM.
Definition cf_util.c:721
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1558
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:288
#define cf_item_next(_parent, _curr)
Definition cf_util.h:92
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:295
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:290
static void * fr_dcursor_filter_next(fr_dcursor_t *cursor, fr_dcursor_eval_t eval, void const *uctx)
Return the next item, skipping the current item, that satisfies an evaluation function.
Definition dcursor.h:547
static void * fr_dcursor_remove(fr_dcursor_t *cursor)
Remove the current item.
Definition dcursor.h:482
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:339
static void * fr_dcursor_head(fr_dcursor_t *cursor)
Rewind cursor to the start of the list.
Definition dcursor.h:234
#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
#define ERROR(fmt,...)
Definition dhcpclient.c:41
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition dict_util.c:2872
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4929
static fr_slen_t in
Definition dict.h:884
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:148
Test enumeration values.
Definition dict_test.h:92
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static int fr_dlist_talloc_free_head(fr_dlist_head_t *list_head)
Free the first item in the list.
Definition dlist.h:837
static void fr_dlist_talloc_free(fr_dlist_head_t *head)
Free all items in a doubly linked list (with talloc)
Definition dlist.h:892
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
static void * fr_dlist_tail(fr_dlist_head_t const *list_head)
Return the TAIL item of a list or NULL if the list is empty.
Definition dlist.h:513
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:257
static int fr_dlist_talloc_free_tail(fr_dlist_head_t *list_head)
Free the last item in the list.
Definition dlist.h:847
Head of a doubly linked list.
Definition dlist.h:51
#define EXEC_TIMEOUT
Default wait time for exec calls (in seconds).
Definition exec.h:32
int radius_exec_program_legacy(char *out, size_t outlen, request_t *request, char const *cmd, fr_pair_list_t *input_pairs, bool exec_wait, bool shell_escape, fr_time_delta_t timeout)
Execute a program.
talloc_free(hp)
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2055
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:455
#define RDEBUG3(fmt,...)
Definition log.h:355
#define REDEBUG3(fmt,...)
Definition log.h:385
#define RPEDEBUG(fmt,...)
Definition log.h:388
#define RINDENT()
Indent R* messages by one level.
Definition log.h:442
static int _map_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, map_t *parent, CONF_SECTION *cs, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, map_validate_t validate, void *uctx, unsigned int max, bool update, bool edit)
Definition map.c:842
static int map_value_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp, tmpl_rules_t const *t_rules)
Convert CONFIG_PAIR (which may contain refs) to map_t.
Definition map.c:1198
int map_afrom_fields(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, request_t *request, char const *lhs, char const *op_str, char const *rhs, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, bool bare_word_only)
Convert a fr_pair_t into a map.
Definition map.c:2529
#define DEBUG_OVERWRITE(_old, _new)
Definition map.c:1852
int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, UNUSED void *uctx)
Convert a map to a fr_pair_t.
Definition map.c:1604
int map_afrom_cs_edit(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, map_validate_t validate, void *uctx, unsigned int max)
Convert a config section into an attribute map for editing.
Definition map.c:1167
fr_table_num_sorted_t const map_assignment_op_table[]
Definition map.c:368
fr_sbuff_parse_rules_t const map_parse_rules_bareword_quoted
Definition map.c:386
ssize_t map_afrom_substr(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, fr_sbuff_t *in, fr_table_num_sorted_t const *op_table, size_t op_table_len, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, fr_sbuff_parse_rules_t const *p_rules)
Parse sbuff into (which may contain refs) to map_t.
Definition map.c:466
static map_t * map_alloc(TALLOC_CTX *ctx, map_t *parent)
Definition map.c:73
static int _map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, map_t *parent, CONF_SECTION *cs, tmpl_rules_t const *t_rules, map_validate_t validate, void *uctx, unsigned int max)
Definition map.c:1278
int map_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *input_rhs_rules, bool edit)
Convert CONFIG_PAIR (which may contain refs) to map_t.
Definition map.c:110
static fr_table_num_sorted_t const cond_quote_table[]
Definition map.c:46
int map_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, map_validate_t validate, void *uctx, unsigned int max)
Convert a config section into an attribute map.
Definition map.c:1136
int map_afrom_value_box(TALLOC_CTX *ctx, map_t **out, char const *lhs, fr_token_t lhs_quote, tmpl_rules_t const *lhs_rules, fr_token_t op, fr_value_box_t *rhs, bool steal_rhs_buffs)
Convert a value box to a map.
Definition map.c:1383
int map_afrom_vp(TALLOC_CTX *ctx, map_t **out, fr_pair_t *vp, tmpl_rules_t const *rules)
Convert a fr_pair_t into a map.
Definition map.c:1464
int map_to_request(request_t *request, map_t const *map, radius_map_getvalue_t func, void *ctx)
Convert map_t to fr_pair_t (s) and add them to a request_t.
Definition map.c:1884
static int map_exec_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map)
Process map which has exec as a src.
Definition map.c:1538
static size_t cond_quote_table_len
Definition map.c:52
int map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs, tmpl_rules_t const *t_rules, map_validate_t validate, void *uctx, unsigned int max)
Convert a config section into a list of { a, b, c, d, ... }.
Definition map.c:1352
int map_afrom_attr_str(TALLOC_CTX *ctx, map_t **out, char const *vp_str, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules)
Convert a value pair string to valuepair map.
Definition map.c:1433
ssize_t map_print(fr_sbuff_t *out, map_t const *map)
Print a map to a string.
Definition map.c:2387
size_t map_assignment_op_table_len
Definition map.c:384
fr_sbuff_parse_rules_t const * map_parse_rules_quoted[T_TOKEN_LAST]
Definition map.c:430
void map_debug_log(request_t *request, map_t const *map, fr_pair_t const *vp)
Definition map.c:2443
void fr_canonicalize_error(TALLOC_CTX *ctx, char **sp, char **text, ssize_t slen, char const *fmt)
Canonicalize error strings, removing tabs, and generate spaces for error marker.
Definition log.c:87
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
fr_slen_t tmpl_print(fr_sbuff_t *out, tmpl_t const *vpt, fr_sbuff_escape_rules_t const *e_rules)
bool fr_pair_matches_da(void const *item, void const *uctx)
Evaluation function for matching if vp matches a given da.
Definition pair.c:3455
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
Definition pair.c:2330
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
Convert string value to native attribute value.
Definition pair.c:2621
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1352
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:290
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
int fr_pair_delete_by_child_num(fr_pair_list_t *list, fr_dict_attr_t const *parent, unsigned int attr)
Delete matching pairs from the specified list.
Definition pair.c:1815
int fr_pair_value_copy(fr_pair_t *dst, fr_pair_t *src)
Copy the value from one pair to another.
Definition pair.c:2595
int fr_pair_reinit_from_da(fr_pair_list_t *list, fr_pair_t *vp, fr_dict_attr_t const *da)
Re-initialise an attribute with a different da.
Definition pair.c:327
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:503
int8_t fr_pair_cmp_by_da(void const *a, void const *b)
Order attributes by their da, and tag.
Definition pair.c:1851
void fr_pair_list_move_op(fr_pair_list_t *to, fr_pair_list_t *from, fr_token_t op)
Move pairs from source list to destination list respecting operator.
int paircmp_pairs(UNUSED request_t *request, fr_pair_t const *check, fr_pair_t *vp)
Compares check and vp by value.
Definition paircmp.c:53
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
#define RDEBUG2(fmt,...)
#define RDEBUG_ENABLED()
static bool done
Definition radclient.c:83
static const char * spaces
Definition radict.c:178
fr_dict_attr_t const * request_attr_request
Definition request.c:43
fr_dict_attr_t const * request_attr_control
Definition request.c:45
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
Definition sbuff.c:2129
#define fr_sbuff_start(_sbuff_or_marker)
#define fr_sbuff_out_by_longest_prefix(_match_len, _out, _table, _sbuff, _def)
#define fr_sbuff_adv_past_str_literal(_sbuff, _needle)
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_adv_past_whitespace(_sbuff, _len, _tt)
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition sbuff.h:193
#define FR_SBUFF_RETURN(_func, _sbuff,...)
#define fr_sbuff_eof(_x)
#define FR_SBUFF_ERROR_RETURN(_sbuff_or_marker)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define FR_SBUFF_IN_SPRINTF_RETURN(...)
#define FR_SBUFF_IN_STR(_start)
#define FR_SBUFF(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
#define FR_SBUFF_IN_STRCPY_RETURN(...)
Set of terminal elements.
Set of parsing rules for *unescape_until functions.
#define MAP_VERIFY(_x)
Definition map.h:108
int(* radius_map_getvalue_t)(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, void *uctx)
Definition map.h:117
int(* map_validate_t)(map_t *map, void *ctx)
Definition map.h:116
static int16_t tmpl_attr_tail_num(tmpl_t const *vpt)
Return the last attribute reference's attribute number.
Definition tmpl.h:885
void tmpl_attr_set_list(tmpl_t *vpt, fr_dict_attr_t const *list)
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_is_xlat(vpt)
Definition tmpl.h:210
void tmpl_set_name_printf(tmpl_t *vpt, fr_token_t quote, char const *fmt,...))
Set the name on a pre-initialised tmpl.
void tmpl_set_xlat(tmpl_t *vpt, xlat_exp_head_t *xlat)
Change the default dictionary in the tmpl's resolution rules.
void tmpl_attr_set_request_ref(tmpl_t *vpt, FR_DLIST_HEAD(tmpl_request_list) const *request_def)
Set the request for an attribute ref.
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_value(_tmpl)
Definition tmpl.h:937
TALLOC_CTX * list_ctx
Where to allocate new attributes if building out from the current extents of the tree.
Definition tmpl.h:614
int tmpl_afrom_value_box(TALLOC_CTX *ctx, tmpl_t **out, fr_value_box_t *data, bool steal)
Create a tmpl_t from a fr_value_box_t.
tmpl_t * tmpl_alloc(TALLOC_CTX *ctx, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len)
Create a new heap allocated tmpl_t.
int tmpl_attr_unknown_add(tmpl_t *vpt)
Add an unknown fr_dict_attr_t specified by a tmpl_t to the main dictionary.
static bool tmpl_attr_tail_is_unknown(tmpl_t const *vpt)
Return true if the last attribute reference is "unknown".
Definition tmpl.h:742
#define tmpl_contains_regex(vpt)
Definition tmpl.h:226
int tmpl_extents_build_to_leaf_parent(fr_dlist_head_t *leaf, fr_dlist_head_t *interior, tmpl_t const *vpt)
Allocate interior pairs.
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
#define tmpl_is_exec(vpt)
Definition tmpl.h:211
fr_dict_attr_t const * enumv
Enumeration attribute used to resolve enum values.
Definition tmpl.h:342
void tmpl_rules_child_init(TALLOC_CTX *ctx, tmpl_rules_t *out, tmpl_rules_t const *parent, tmpl_t *vpt)
Initialize a set of rules from a parent set of rules, and a parsed tmpl_t.
#define tmpl_xlat(_tmpl)
Definition tmpl.h:930
static fr_dict_attr_t const * tmpl_list(tmpl_t const *vpt)
Definition tmpl.h:904
int tmpl_extents_find(TALLOC_CTX *ctx, fr_dlist_head_t *leaf, fr_dlist_head_t *interior, request_t *request, tmpl_t const *vpt))
Determines points where the reference list extends beyond the current pair tree.
#define tmpl_rules_cast(_tmpl)
Definition tmpl.h:942
ssize_t tmpl_afrom_attr_str(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t **out, char const *name, tmpl_rules_t const *rules))
Parse a string into a TMPL_TYPE_ATTR_* type tmpl_t.
void tmpl_set_name(tmpl_t *vpt, fr_token_t quote, char const *name, ssize_t len)
Set the name on a pre-initialised tmpl.
@ 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_DATA
Value in native boxed format.
Definition tmpl.h:138
@ TMPL_TYPE_DATA_UNRESOLVED
Unparsed literal string.
Definition tmpl.h:179
static bool tmpl_attr_tail_da_is_leaf(tmpl_t const *vpt)
Return true if the the last attribute reference is a leaf attribute.
Definition tmpl.h:817
#define NUM_COUNT
Definition tmpl.h:396
#define tmpl_contains_attr(vpt)
Definition tmpl.h:225
static bool tmpl_attr_tail_da_is_structural(tmpl_t const *vpt)
Return true if the the last attribute reference is a structural attribute.
Definition tmpl.h:835
int tmpl_copy_pairs(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, tmpl_t const *vpt))
Copy pairs matching a tmpl_t in the current request_t.
Definition tmpl_eval.c:685
fr_pair_list_t * tmpl_list_head(request_t *request, fr_dict_attr_t const *list)
Resolve attribute fr_pair_list_t value to an attribute list.
Definition tmpl_eval.c:70
TALLOC_CTX * tmpl_list_ctx(request_t *request, fr_dict_attr_t const *list)
Return the correct TALLOC_CTX to alloc fr_pair_t in, for a list.
Definition tmpl_eval.c:110
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 fr_slen_t e_rules fr_slen_t tmpl_print_quoted(fr_sbuff_t *out, tmpl_t const *vpt)
Print a tmpl_t to a string with quotes.
tmpl_xlat_rules_t xlat
Rules/data for parsing xlats.
Definition tmpl.h:340
bool at_runtime
Produce an ephemeral/runtime tmpl.
Definition tmpl.h:348
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:920
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
fr_dict_t const * dict_def
Alternative default dictionary to use if vpt->rules->dict_def is NULL.
Definition tmpl.h:369
#define NUM_UNSPEC
Definition tmpl.h:394
#define tmpl_value_type(_tmpl)
Definition tmpl.h:939
int tmpl_request_ptr(request_t **request, FR_DLIST_HEAD(tmpl_request_list) const *rql)
Resolve a tmpl_request_ref_t to a request_t.
Definition tmpl_eval.c:163
#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
fr_pair_list_t * list
List that we tried to evaluate ar in and failed.
Definition tmpl.h:616
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:801
@ TMPL_ATTR_LIST_REQUIRE
Attribute refs are required to have a list.
Definition tmpl.h:264
@ TMPL_ATTR_LIST_FORBID
Attribute refs are forbidden from having a list.
Definition tmpl.h:263
int tmpl_attr_set_leaf_da(tmpl_t *vpt, fr_dict_attr_t const *da)
Replace the leaf attribute only.
struct tmpl_res_rules_s tmpl_res_rules_t
Definition tmpl.h:237
static char const * tmpl_list_name(fr_dict_attr_t const *list, char const *def)
Return the name of a tmpl list or def if list not provided.
Definition tmpl.h:915
#define tmpl_aexpand(_ctx, _out, _request, _vpt, _escape, _escape_ctx)
Expand a tmpl to a C type, allocing a new buffer to hold the string.
Definition tmpl.h:1064
fr_slen_t tmpl_attr_list_from_substr(fr_dict_attr_t const **da_p, fr_sbuff_t *in)
Parse one a single list reference.
fr_event_list_t * runtime_el
The eventlist to use for runtime instantiation of xlats.
Definition tmpl.h:328
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:223
int tmpl_cast_set(tmpl_t *vpt, fr_type_t type)
Set a cast for a tmpl.
Describes the current extents of a pair tree in relation to the tree described by a tmpl_t.
Definition tmpl.h:605
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
fr_aka_sim_id_type_t type
fr_pair_t * vp
Value pair map.
Definition map.h:77
fr_token_t op
The operator that controls insertion of the dst attribute.
Definition map.h:82
tmpl_t * lhs
Typically describes the attribute to add, modify or compare.
Definition map.h:78
map_list_t child
parent map, for nested ones
Definition map.h:89
map_t * parent
Definition map.h:88
tmpl_t * rhs
Typically describes a literal value or a src attribute to copy or compare.
Definition map.h:79
CONF_ITEM * ci
Config item that the map was created from.
Definition map.h:85
tmpl_attr_list_presence_t list_presence
Whether the attribute reference can have a list, forbid it, or require it.
Definition tmpl.h:298
fr_dict_attr_t const * list_def
Default list to use with unqualified attribute reference.
Definition tmpl.h:295
unsigned int disallow_rhs_resolve
map RHS is NOT immediately resolved in the context of the LHS.
Definition tmpl.h:324
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:273
unsigned int bare_word_enum
for v3 compatibility.
Definition tmpl.h:322
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:545
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:139
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
void tmpl_dcursor_clear(tmpl_dcursor_ctx_t *cc)
Clear any temporary state allocations.
#define tmpl_dcursor_init(_err, _ctx, _cc, _cursor, _request, _vpt)
Maintains state between cursor calls.
const bool fr_assignment_op[T_TOKEN_LAST]
Definition token.c:171
char const * fr_token_name(int token)
Definition token.c:514
fr_table_num_ordered_t const fr_tokens_table[]
Definition token.c:34
const char fr_token_quote[T_TOKEN_LAST]
Convert tokens back to a quoting character.
Definition token.c:159
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:81
const bool fr_comparison_op[T_TOKEN_LAST]
Definition token.c:201
enum fr_token fr_token_t
@ T_OP_SUB_EQ
Definition token.h:70
@ T_INVALID
Definition token.h:39
@ T_SINGLE_QUOTED_STRING
Definition token.h:122
@ T_OP_CMP_TRUE
Definition token.h:104
@ T_BARE_WORD
Definition token.h:120
@ T_OP_EQ
Definition token.h:83
@ T_BACK_QUOTED_STRING
Definition token.h:123
@ T_HASH
Definition token.h:119
@ T_OP_SET
Definition token.h:84
@ T_OP_NE
Definition token.h:97
@ T_OP_ADD_EQ
Definition token.h:69
@ T_OP_CMP_FALSE
Definition token.h:105
@ T_OP_LSHIFT_EQ
Definition token.h:77
@ T_OP_RSHIFT_EQ
Definition token.h:76
@ T_OP_REG_EQ
Definition token.h:102
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
@ T_OP_CMP_EQ
Definition token.h:106
@ T_OP_LE
Definition token.h:100
@ T_OP_GE
Definition token.h:98
@ T_OP_GT
Definition token.h:99
@ T_SOLIDUS_QUOTED_STRING
Definition token.h:124
@ T_OP_LT
Definition token.h:101
@ T_OP_REG_NE
Definition token.h:103
@ T_OP_PREPEND
Definition token.h:85
#define T_TOKEN_LAST
Definition token.h:129
static fr_slen_t head
Definition xlat.h:420
ssize_t xlat_aeval_compiled(TALLOC_CTX *ctx, char **out, request_t *request, xlat_exp_head_t const *head, xlat_escape_legacy_t escape, void const *escape_ctx))
Definition xlat_eval.c:1884
fr_slen_t xlat_tokenize_expression(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))
Definition xlat_expr.c:3165
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define PAIR_VERIFY(_x)
Definition pair.h:204
void fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp)
Sort a doubly linked list of fr_pair_ts using merge sort.
fr_pair_t * fr_pair_list_tail(fr_pair_list_t const *list)
Get the tail of a valuepair list.
Definition pair_inline.c:55
#define fr_pair_list_foreach(_list_head, _iter)
Iterate over the contents of a fr_pair_list_t.
Definition pair.h:279
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src)
Appends a list of fr_pair_t from a temporary list to a destination list.
void fr_pair_list_prepend(fr_pair_list_t *dst, fr_pair_list_t *src)
Move a list of fr_pair_t from a temporary list to the head of a destination list.
static fr_slen_t fr_pair_aprint_value_quoted(TALLOC_CTX *ctx, char **out, fr_pair_t const *vp, fr_token_t quote) 1(fr_pair_print_value_quoted
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:604
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:42
static fr_slen_t parent
Definition pair.h:858
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define FR_TYPE_QUOTED
Definition types.h:313
#define fr_type_is_structural(_x)
Definition types.h:393
#define FR_TYPE_STRUCTURAL
Definition types.h:317
#define fr_type_is_leaf(_x)
Definition types.h:394
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:455
fr_sbuff_unescape_rules_t const fr_value_unescape_single
Definition value.c:291
fr_sbuff_parse_rules_t const value_parse_rules_solidus_quoted
Definition value.c:565
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:3961
fr_sbuff_parse_rules_t const * value_parse_rules_quoted[T_TOKEN_LAST]
Parse rules for quoted strings.
Definition value.c:612
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:4409
int fr_value_box_cmp_op(fr_token_t op, fr_value_box_t const *a, fr_value_box_t const *b)
Compare two attributes using an operator.
Definition value.c:1023
fr_sbuff_parse_rules_t const value_parse_rules_single_quoted
Definition value.c:559
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:6079
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4634
fr_sbuff_parse_rules_t const value_parse_rules_bareword_quoted
Definition value.c:530
fr_sbuff_parse_rules_t const value_parse_rules_backtick_quoted
Definition value.c:571
fr_sbuff_parse_rules_t const * value_parse_rules_unquoted[T_TOKEN_LAST]
Parse rules for non-quoted strings.
Definition value.c:514
fr_sbuff_parse_rules_t const value_parse_rules_double_quoted
Definition value.c:553
ssize_t fr_value_box_from_substr(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *rules)
Convert string value to a fr_value_box_t type.
Definition value.c:5424
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:644
#define fr_box_strvalue(_val)
Definition value.h:308
static size_t char ** out
Definition value.h:1030