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