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: ce8b71320168ff92c9bc3810bcca259c8f546660 $
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: ce8b71320168ff92c9bc3810bcca259c8f546660 $")
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 fr_slen_t slen;
867 char const *p;
868
869 p = cf_section_name2(cs);
870 if (!p) goto do_children;
871
872 if (our_lhs_rules.attr.list_presence == TMPL_ATTR_LIST_FORBID) {
873 cf_log_err(ci, "Invalid \"update\" section - list references are not allowed here");
874 return -1;
875 }
876
877 MEM(tmp_ctx = talloc_init_const("tmp"));
878
879 slen = tmpl_request_ref_list_afrom_substr(ctx, NULL, &our_lhs_rules.attr.request_def,
880 &FR_SBUFF_IN_STR(p));
881 if (slen < 0) {
882 cf_log_err(ci, "Invalid reference - %s", fr_strerror());
883 talloc_free(tmp_ctx);
884 return -1;
885 }
886 p += slen;
887
888 slen = tmpl_attr_list_from_substr(&our_lhs_rules.attr.list_def, &FR_SBUFF_IN_STR(p));
889 if (slen == 0) {
890 cf_log_err(ci, "Unknown list reference \"%s\"", p);
891 talloc_free(tmp_ctx);
892 return -1;
893 }
894 }
895
896do_children:
897 for (ci = cf_item_next(cs, NULL);
898 ci != NULL;
899 ci = cf_item_next(cs, ci)) {
900 tmpl_rules_t child_lhs_rules = our_lhs_rules;
901
902 /*
903 * Disallow list references on the LHS of child lists for edit sections.
904 */
905 if (edit) child_lhs_rules.attr.list_presence = TMPL_ATTR_LIST_FORBID;
906
907 if (total++ == max) {
908 cf_log_err(ci, "Map size exceeded");
909 error:
910 /*
911 * Free in reverse as successive entries have their
912 * prececessors as talloc parent contexts
913 */
914 talloc_free(tmp_ctx);
915 map_list_talloc_reverse_free(out);
916 return -1;
917 }
918
919 /*
920 * If we have a subsection, AND the name2 is an
921 * assignment operator, THEN we allow sub-maps.
922 */
923 if (cf_item_is_section(ci)) {
924 CONF_SECTION *subcs;
925 fr_token_t token;
926 ssize_t slen;
927 map_list_t child_list;
928
929 map_list_init(&child_list);
930 subcs = cf_item_to_section(ci);
931 token = cf_section_name2_quote(subcs);
932
933 if (token == T_INVALID) {
934 cf_log_err(ci, "Section '%s { ... }' is missing the '=' operator", cf_section_name1(subcs));
935 goto error;
936 }
937
938 if (!fr_assignment_op[token]) {
939 cf_log_err(ci, "Invalid operator '%s'", fr_tokens[token]);
940 goto error;
941 }
942
943 MEM(map = map_alloc(parent_ctx, parent));
944 map->op = token;
945 map->ci = ci;
946
947 /*
948 * The LHS MUST be an attribute name.
949 * map_afrom_cp() allows for dynamic
950 * names, but for simplicity we forbid
951 * them for now. Once the functionality
952 * is tested and used, we can allow that.
953 */
954 slen = tmpl_afrom_attr_str(ctx, NULL, &map->lhs, cf_section_name1(subcs), &our_lhs_rules);
955 if (slen <= 0) {
956 cf_log_err(ci, "Failed parsing attribute reference for list %s - %s",
957 cf_section_name1(subcs), fr_strerror());
958 talloc_free(map);
959 goto error; /* re-do "goto marker" stuff to print out spaces ? */
960 }
961
962 /*
963 * The LHS MUST be an attribute reference
964 * for now.
965 */
966 if (!tmpl_is_attr(map->lhs)) {
967 cf_log_err(ci, "Left side of group '%s' is NOT an attribute reference",
968 map->lhs->name);
969 talloc_free(map);
970 goto error; /* re-do "goto marker" stuff to print out spaces ? */
971 }
972
973 if (tmpl_attr_tail_da(map->lhs)->flags.is_unknown) {
974 cf_log_err(ci, "Unknown attribute '%s'", map->lhs->name);
975 talloc_free(map);
976 goto error; /* re-do "goto marker" stuff to print out spaces ? */
977 }
978
979 /*
980 * The leaf reference of the outer section
981 * is used as the parsing context of the
982 * inner section.
983 */
984 child_lhs_rules.attr.namespace = tmpl_attr_tail_da(map->lhs);
985
986 /*
987 * Groups MAY change dictionaries. If so, then swap the dictionary and the parent.
988 */
989 if (child_lhs_rules.attr.namespace->type == FR_TYPE_GROUP) {
990 fr_dict_attr_t const *ref;
991 fr_dict_t const *dict, *internal;
992
993 ref = fr_dict_attr_ref(child_lhs_rules.attr.namespace);
994 dict = fr_dict_by_da(ref);
995 internal = fr_dict_internal();
996
997 if (dict != internal) {
998 if (dict != child_lhs_rules.attr.dict_def) {
999 child_lhs_rules.attr.dict_def = dict;
1000 child_lhs_rules.attr.namespace = ref;
1001 }
1002 } else {
1003 /*
1004 * We're internal: don't use it, and instead rely on dict_def.
1005 */
1006 child_lhs_rules.attr.namespace = NULL;
1007 }
1008 }
1009
1010 if (fr_type_is_structural(tmpl_attr_tail_da(map->lhs)->type)) {
1011 /*
1012 * This prints out any relevant error
1013 * messages. We MAY want to print out
1014 * additional ones, but that might get
1015 * complex and confusing.
1016 *
1017 * We call out internal _map_afrom_cs()
1018 * function, in order to pass in the
1019 * correct parent map.
1020 */
1021 if (_map_afrom_cs(map, &child_list, map, cf_item_to_section(ci),
1022 &child_lhs_rules, rhs_rules, validate, uctx, max, false, edit) < 0) {
1023 fail:
1024 map_list_talloc_free(&child_list);
1025 talloc_free(map);
1026 goto error;
1027 }
1028 map_list_move(&map->child, &child_list);
1029 } else {
1031
1032 /*
1033 * foo := { a, b, c }
1034 *
1035 * @todo - maybe lhs_rules? But definitely not child_lhs_rules.
1036 *
1037 * @todo - this code is taken from compile_edit_section(), we might want
1038 * to just move that code here, for fewer special cases.
1039 */
1040 if (map_list_afrom_cs(map, &child_list, cf_item_to_section(ci), rhs_rules, NULL, NULL, max) < 0) {
1041 goto fail;
1042 }
1043
1044 if ((map->op != T_OP_SET) && !map_list_num_elements(&child_list)) {
1045 cf_log_err(cs, "Cannot use operator '%s' for assigning empty list to '%s' data type.",
1046 fr_tokens[map->op], fr_type_to_str(tmpl_attr_tail_da(map->lhs)->type));
1047 goto fail;
1048 }
1049
1050 map_list_move(&map->child, &child_list);
1051 }
1052
1053 MAP_VERIFY(map);
1054 goto next;
1055 }
1056
1057 if (!cf_item_is_pair(ci)) {
1058 cf_log_err(ci, "Entry is not in \"attribute = value\" format");
1059 goto error;
1060 }
1061
1062 cp = cf_item_to_pair(ci);
1063 fr_assert(cp != NULL);
1064
1065 /*
1066 * Over-ride RHS rules for
1067 *
1068 * reply += {
1069 * User-Name = User-Name
1070 * }
1071 *
1072 * Which looks stupid. Instead we require
1073 *
1074 * reply += {
1075 * User-Name = request.User-Name
1076 * }
1077 *
1078 * On the other hand, any xlats on the RHS don't use the full path. :( And we still need
1079 * to allow relative attribute references via ".foo", when updating structures.
1080 */
1081 our_rhs_rules = rhs_rules;
1082 if (edit && (rhs_rules->attr.list_def != child_lhs_rules.attr.list_def)) {
1083 char const *value = cf_pair_value(cp);
1084
1085 if (value && (*value == '&')) {
1086 child_rhs_rules.attr.list_presence = TMPL_ATTR_LIST_REQUIRE;
1087 our_rhs_rules = &child_rhs_rules;
1088 }
1089 }
1090
1091 if (map_afrom_cp(parent_ctx, &map, parent, cp, &child_lhs_rules, our_rhs_rules, edit) < 0) {
1092 cf_log_err(ci, "Failed creating map from '%s = %s'",
1093 cf_pair_attr(cp), cf_pair_value(cp));
1094 goto error;
1095 }
1096
1097 MAP_VERIFY(map);
1098
1099 /*
1100 * Check the types in the map are valid
1101 */
1102 if (validate && (validate(map, uctx) < 0)) goto error;
1103
1104 next:
1105 parent_ctx = map;
1106 map_list_insert_tail(out, map);
1107 }
1108
1109 talloc_free(tmp_ctx);
1110 return 0;
1111
1112}
1113
1114/** Convert a config section into an attribute map.
1115 *
1116 * For "update" sections, Uses 'name2' of section to set default request and lists.
1117 *
1118 * @param[in] ctx for talloc.
1119 * @param[out] out Where to store the allocated map.
1120 * @param[in] cs the update section
1121 * @param[in] lhs_rules rules for parsing LHS attribute references.
1122 * @param[in] rhs_rules rules for parsing RHS attribute references.
1123 * @param[in] validate map using this callback (may be NULL).
1124 * @param[in] uctx to pass to callback.
1125 * @param[in] max number of mappings to process.
1126 * @return
1127 * - 0 on success.
1128 * - -1 on failure.
1129 */
1130int map_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
1131 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
1132 map_validate_t validate, void *uctx,
1133 unsigned int max)
1134{
1135 bool update;
1136 char const *name2;
1137 map_t *parent = NULL;
1138
1139 name2 = cf_section_name1(cs);
1140 update = (name2 && (strcmp(name2, "update") == 0));
1141
1142 if (ctx) parent = talloc_get_type(ctx, map_t);
1143
1144 return _map_afrom_cs(ctx, out, parent, cs, lhs_rules, rhs_rules, validate, uctx, max, update, false);
1145}
1146
1147/** Convert a config section into an attribute map for editing
1148 *
1149 * @param[in] ctx for talloc.
1150 * @param[out] out Where to store the allocated map.
1151 * @param[in] cs the update section
1152 * @param[in] lhs_rules rules for parsing LHS attribute references.
1153 * @param[in] rhs_rules rules for parsing RHS attribute references.
1154 * @param[in] validate map using this callback (may be NULL).
1155 * @param[in] uctx to pass to callback.
1156 * @param[in] max number of mappings to process.
1157 * @return
1158 * - 0 on success.
1159 * - -1 on failure.
1160 */
1161int map_afrom_cs_edit(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
1162 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules,
1163 map_validate_t validate, void *uctx,
1164 unsigned int max)
1165{
1166 map_t *parent = NULL;
1167
1168 if (ctx) parent = talloc_get_type(ctx, map_t);
1169
1170 return _map_afrom_cs(ctx, out, parent, cs, lhs_rules, rhs_rules, validate, uctx, max, false, true);
1171}
1172
1173/** Convert CONFIG_PAIR (which may contain refs) to map_t.
1174 *
1175 * Treats the CONFIG_PAIR name as a value.
1176 *
1177 * Treatment of left operand depends on quotation, barewords are treated as
1178 * attribute , double quoted values are treated as expandable strings,
1179 * single quoted values are treated as literal strings.
1180 *
1181 * Return must be freed with talloc_free
1182 *
1183 * @param[in] ctx for talloc.
1184 * @param[in] out Where to write the pointer to the new #map_t.
1185 * @param[in] parent the parent map
1186 * @param[in] cp to convert to map.
1187 * @param[in] t_rules rules for parsing name
1188 * @return
1189 * - #map_t if successful.
1190 * - NULL on error.
1191 */
1192static int map_value_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp,
1193 tmpl_rules_t const *t_rules)
1194{
1195 map_t *map;
1196 char const *attr, *marker_subject;
1197 ssize_t slen;
1199
1200 *out = NULL;
1201
1202 if (!cp) return -1;
1203
1204 MEM(map = map_alloc(ctx, parent));
1205 map->op = T_OP_EQ; /* @todo - should probably be T_INVALID */
1206 map->ci = cf_pair_to_item(cp);
1207
1208 attr = cf_pair_attr(cp);
1209
1210 /*
1211 * LHS may be an expansion (that expands to an attribute reference)
1212 * or an attribute reference. Quoting determines which it is.
1213 */
1215 switch (type) {
1219 slen = tmpl_afrom_substr(ctx, &map->lhs,
1220 &FR_SBUFF_IN(attr, talloc_array_length(attr) - 1),
1221 type,
1222 value_parse_rules_unquoted[type], /* We're not searching for quotes */
1223 t_rules);
1224 if (slen <= 0) {
1225 char *spaces, *text;
1226
1227 marker:
1228 marker_subject = attr;
1229 fr_canonicalize_error(ctx, &spaces, &text, slen, marker_subject);
1230 cf_log_err(cp, "%s", text);
1231 cf_log_perr(cp, "%s^", spaces);
1232
1234 talloc_free(text);
1235 goto error;
1236 }
1237 break;
1238
1240 fr_strerror_const("Invalid location for regular expression");
1241 slen = 0;
1242 goto marker;
1243
1244 default:
1245 /*
1246 * Let the tmpl code determine if it's an attribute reference or else is a raw value.
1247 */
1248 slen = tmpl_afrom_substr(ctx, &map->lhs, &FR_SBUFF_IN(attr, talloc_array_length(attr) - 1), T_BARE_WORD, NULL, t_rules);
1249 if (slen <= 0) goto marker;
1250
1251 if (tmpl_is_attr(map->lhs) && (tmpl_attr_unknown_add(map->lhs) < 0)) {
1252 fr_strerror_printf("Failed defining attribute %s", map->lhs->name);
1253 goto error;
1254 }
1255 break;
1256 }
1257
1258 MAP_VERIFY(map);
1259
1260 *out = map;
1261
1262 return 0;
1263
1264error:
1265 talloc_free(map);
1266 return -1;
1267}
1268
1269/*
1270 * Where the RHS are all values.
1271 */
1272static int _map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, map_t *parent, CONF_SECTION *cs,
1273 tmpl_rules_t const *t_rules,
1274 map_validate_t validate, void *uctx,
1275 unsigned int max)
1276{
1277 CONF_ITEM *ci;
1278 CONF_PAIR *cp;
1279
1280 unsigned int total = 0;
1281 map_t *map;
1282 TALLOC_CTX *parent_ctx;
1283
1284 /*
1285 * The first map has ctx as the parent context.
1286 * The rest have the previous map as the parent context.
1287 */
1288 parent_ctx = ctx;
1289
1290 for (ci = cf_item_next(cs, NULL);
1291 ci != NULL;
1292 ci = cf_item_next(cs, ci)) {
1293 if (total++ == max) {
1294 cf_log_err(ci, "Map size exceeded");
1295 error:
1296 /*
1297 * Free in reverse as successive entries have their
1298 * prececessors as talloc parent contexts
1299 */
1300 map_list_talloc_reverse_free(out);
1301 return -1;
1302 }
1303
1304 if (cf_item_is_section(ci)) {
1305 cf_log_err(ci, "Cannot create sub-lists");
1306 goto error;
1307 }
1308
1309 cp = cf_item_to_pair(ci);
1310 fr_assert(cp != NULL);
1311
1312 if (map_value_afrom_cp(parent_ctx, &map, parent, cp, t_rules) < 0) {
1313 fr_assert(0);
1314 cf_log_err(ci, "Failed creating map from '%s'", cf_pair_attr(cp));
1315 goto error;
1316 }
1317
1318 MAP_VERIFY(map);
1319
1320 /*
1321 * Check the types in the map are valid
1322 */
1323 if (validate && (validate(map, uctx) < 0)) goto error;
1324
1325 parent_ctx = map;
1326 map_list_insert_tail(out, map);
1327 }
1328
1329 return 0;
1330
1331}
1332
1333/** Convert a config section into a list of { a, b, c, d, ... }
1334 *
1335 * @param[in] ctx for talloc.
1336 * @param[out] out Where to store the allocated map.
1337 * @param[in] cs the update section
1338 * @param[in] t_rules rules for parsing the data.
1339 * @param[in] validate map using this callback (may be NULL).
1340 * @param[in] uctx to pass to callback.
1341 * @param[in] max number of mappings to process.
1342 * @return
1343 * - 0 on success.
1344 * - -1 on failure.
1345 */
1346int map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs,
1347 tmpl_rules_t const *t_rules,
1348 map_validate_t validate, void *uctx,
1349 unsigned int max)
1350{
1351 map_t *parent = NULL;
1352
1353 if (ctx) parent = talloc_get_type(ctx, map_t);
1354
1355 return _map_list_afrom_cs(ctx, out, parent, cs, t_rules, validate, uctx, max);
1356}
1357
1358/** Convert a value box to a map
1359 *
1360 * This is mainly used in IO modules, where another function is used to convert
1361 * between the foreign value type and internal values, and the destination
1362 * attribute is provided as a string.
1363 *
1364 * @param[in] ctx for talloc
1365 * @param[out] out Where to store the head of the map.
1366 * @param[in] lhs of the operation
1367 * @param[in] lhs_quote type of the LHS string
1368 * @param[in] lhs_rules rules that control parsing of the LHS string.
1369 * @param[in] op the operation to perform
1370 * @param[in] rhs of the operation
1371 * @param[in] steal_rhs_buffs Whether we attempt to save allocs by stealing the buffers
1372 * from the rhs #fr_value_box_t.
1373 * @return
1374 * - #map_t if successful.
1375 * - NULL on error.
1376 */
1377int map_afrom_value_box(TALLOC_CTX *ctx, map_t **out,
1378 char const *lhs, fr_token_t lhs_quote, tmpl_rules_t const *lhs_rules,
1379 fr_token_t op,
1380 fr_value_box_t *rhs, bool steal_rhs_buffs)
1381{
1382 ssize_t slen;
1383 map_t *map;
1384
1385 map = map_alloc(ctx, NULL);
1386
1387 slen = tmpl_afrom_substr(map, &map->lhs,
1388 &FR_SBUFF_IN_STR(lhs),
1389 lhs_quote,
1390 NULL,
1391 lhs_rules);
1392 if (slen < 0) {
1393 error:
1394 talloc_free(map);
1395 return -1;
1396 }
1397
1398 map->op = op;
1399
1400 if (tmpl_afrom_value_box(map, &map->rhs, rhs, steal_rhs_buffs) < 0) goto error;
1401
1402 MAP_VERIFY(map);
1403 *out = map;
1404
1405 return 0;
1406}
1407
1408/** Convert a value pair string to valuepair map
1409 *
1410 * Takes a valuepair string with list and request qualifiers and converts it into a
1411 * #map_t.
1412 *
1413 * Attribute string is in the format (where @verbatim <qu> @endverbatim is a quotation char ['"]):
1414 @verbatim
1415 [<list>.][<qu>]<attribute>[<qu>] <op> [<qu>]<value>[<qu>]
1416 @endverbatim
1417 *
1418 * @param[in] ctx where to allocate the map.
1419 * @param[out] out Where to write the new map.
1420 * @param[in] vp_str string to parse.
1421 * @param[in] lhs_rules rules for parsing LHS attribute references.
1422 * @param[in] rhs_rules rules for parsing RHS attribute references.
1423 * @return
1424 * - 0 on success.
1425 * - < 0 on error.
1426 */
1427int map_afrom_attr_str(TALLOC_CTX *ctx, map_t **out, char const *vp_str,
1428 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules)
1429{
1430 fr_sbuff_t sbuff = FR_SBUFF_IN(vp_str, strlen(vp_str));
1431
1433 lhs_rules, rhs_rules, NULL) < 0) {
1434 return -1;
1435 }
1436
1437 if (!fr_cond_assert(*out != NULL)) return -1;
1438
1439 if (!tmpl_is_attr((*out)->lhs)) {
1440 TALLOC_FREE(*out);
1441 fr_strerror_const("Left operand must be an attribute");
1442 return -1;
1443 }
1444
1445 return 0;
1446}
1447
1448/** Convert a fr_pair_t into a map
1449 *
1450 * @param[in] ctx where to allocate the map.
1451 * @param[out] out Where to write the new map (must be freed with talloc_free()).
1452 * @param[in] vp to convert.
1453 * @param[in] rules to insert attributes into.
1454 * @return
1455 * - 0 on success.
1456 * - -1 on failure.
1457 */
1458int map_afrom_vp(TALLOC_CTX *ctx, map_t **out, fr_pair_t *vp, tmpl_rules_t const *rules)
1459{
1460 char buffer[256];
1461 fr_sbuff_t buffer_sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
1462
1463 map_t *map;
1464
1465 map = map_alloc(ctx, NULL);
1466 if (!map) {
1467 oom:
1468 fr_strerror_const("Out of memory");
1469 return -1;
1470 }
1471
1472 /*
1473 * Allocate the LHS
1474 */
1475 map->lhs = tmpl_alloc(map, TMPL_TYPE_ATTR, T_BARE_WORD, NULL, 0);
1476 if (!map->lhs) goto oom;
1477
1479
1480 tmpl_attr_set_request_ref(map->lhs, rules->attr.request_def);
1481 tmpl_attr_set_list(map->lhs, rules->attr.list_def);
1482
1483 tmpl_print(&buffer_sbuff, map->lhs, NULL);
1484 tmpl_set_name(map->lhs, T_BARE_WORD, fr_sbuff_start(&buffer_sbuff), -1);
1485
1486 /*
1487 * Allocate the RHS
1488 */
1489 map->rhs = tmpl_alloc(map, TMPL_TYPE_DATA, T_BARE_WORD, NULL, -1);
1490 if (!map->lhs) goto oom;
1491
1492 switch (vp->vp_type) {
1493 case FR_TYPE_QUOTED:
1494 tmpl_set_name_printf(map->rhs, T_DOUBLE_QUOTED_STRING, "%pV", &vp->data);
1495 break;
1496
1497 default:
1498 tmpl_set_name_printf(map->rhs, T_BARE_WORD, "%pV", &vp->data);
1499 break;
1500 }
1501
1502 if (unlikely(fr_value_box_copy(map->rhs, tmpl_value(map->rhs), &vp->data) < 0)) {
1503 talloc_free(map);
1504 return -1;
1505 }
1506
1507 *out = map;
1508
1509 return 0;
1510}
1511
1512/** Process map which has exec as a src
1513 *
1514 * Evaluate maps which specify exec as a src. This may be used by various sorts of update sections, and so
1515 * has been broken out into it's own function.
1516 *
1517 * @param[in,out] ctx to allocate new #fr_pair_t (s) in.
1518 * @param[out] out Where to write the #fr_pair_t (s).
1519 * @param[in] request structure (used only for talloc).
1520 * @param[in] map the map. The LHS (dst) must be #TMPL_TYPE_ATTR.
1521 * The RHS (src) must be #TMPL_TYPE_EXEC.
1522 * @return
1523 * - 0 on success.
1524 * - -1 on failure.
1525 */
1526static int map_exec_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map)
1527{
1528 int result;
1529 fr_pair_t *vp;
1530 fr_pair_list_t *input_pairs = NULL;
1531 char answer[1024];
1532
1534
1535 MAP_VERIFY(map);
1536
1537 fr_assert(map->rhs); /* Quite clang scan */
1538 fr_assert(tmpl_is_exec(map->rhs));
1539 fr_assert(tmpl_is_attr(map->lhs));
1540
1541 if (fr_type_is_structural(tmpl_attr_tail_da(map->lhs)->type)) {
1542 REDEBUG("Cannot assign `exec` output to structural attribute %s", map->lhs->name);
1543 return -1;
1544 }
1545
1546 /*
1547 * We always put the request pairs into the environment
1548 */
1549 input_pairs = tmpl_list_head(request, request_attr_request);
1550
1551 /*
1552 * Automagically switch output type depending on our destination
1553 * @todo - If dst is a list, then we create attributes from the output of the program
1554 * if dst is an attribute, then we create an attribute of that type and then
1555 * call fr_pair_value_from_str on the output of the script.
1556 */
1557 result = radius_exec_program_legacy(answer, sizeof(answer),
1558 request, map->rhs->name, input_pairs ? input_pairs : NULL,
1560 if (result != 0) {
1561 REDEBUG("Exec failed with code (%i)", result);
1562 return -1;
1563 }
1564
1565 /*
1566 * @todo - we completely ignore the operator here :( Arguably the caller should be calling ONLY
1567 * the legacy pair move functions with the results of this function.
1568 */
1570 vp->op = map->op;
1571 if (fr_pair_value_from_str(vp, answer, strlen(answer), &fr_value_unescape_single, false) < 0) {
1572 RPEDEBUG("Failed parsing exec output");
1573 talloc_free(&vp);
1574 return -2;
1575 }
1577
1578 return 0;
1579}
1580
1581/** Convert a map to a #fr_pair_t
1582 *
1583 * @param[in,out] ctx to allocate #fr_pair_t (s) in.
1584 * @param[out] out Where to write the #fr_pair_t (s), which may be NULL if not found
1585 * @param[in] request The current request.
1586 * @param[in] map the map. The LHS (dst) has to be #TMPL_TYPE_ATTR.
1587 * @param[in] uctx unused.
1588 * @return
1589 * - 0 on success.
1590 * - -1 on failure.
1591 */
1592int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, UNUSED void *uctx)
1593{
1594 int rcode = 0;
1595 fr_pair_t *n = NULL;
1596 fr_pair_list_t found;
1597 request_t *context = request;
1598 ssize_t slen;
1599 char *str;
1600
1601 fr_pair_list_init(&found);
1603
1604 MAP_VERIFY(map);
1605 if (!fr_cond_assert(map->lhs != NULL)) return -1;
1606
1607 fr_assert(tmpl_is_attr(map->lhs));
1608
1609 /*
1610 * If there's no RHS, then it MUST be an attribute, and
1611 * it MUST be structural. And it MAY have children.
1612 */
1613 if (!map->rhs) {
1614 map_t *child;
1615
1616 if (!tmpl_is_attr(map->lhs)) return -1;
1617
1618 switch (tmpl_attr_tail_da(map->lhs)->type) {
1619 case FR_TYPE_STRUCTURAL:
1620 break;
1621
1622 default:
1623 return -1;
1624 }
1625
1626 /*
1627 * Create the parent attribute, and
1628 * recurse to generate the children into
1629 * vp->vp_group
1630 */
1632 n->op = map->op;
1633
1634 for (child = map_list_next(&map->child, NULL);
1635 child != NULL;
1636 child = map_list_next(&map->child, child)) {
1637 fr_pair_list_t list;
1638
1639 /*
1640 * map_to_vp() frees "out", so we need to
1641 * work around that by creating a
1642 * temporary list.
1643 */
1644 fr_pair_list_init(&list);
1645 if (map_to_vp(n, &list, request, child, NULL) < 0) {
1646 talloc_free(n);
1647 return -1;
1648 }
1649
1650 fr_pair_list_append(&n->vp_group, &list);
1651 }
1652
1654 return 0;
1655 }
1656
1657 /*
1658 * List to list found, this is a special case because we don't need
1659 * to allocate any attributes, just finding the current list, and change
1660 * the op.
1661 */
1664 fr_pair_list_t *from = NULL;
1665
1666 if (tmpl_request_ptr(&context, tmpl_request(map->rhs)) == 0) {
1667 from = tmpl_list_head(context, tmpl_list(map->rhs));
1668 }
1669 if (!from) return 0;
1670
1671 if (fr_pair_list_copy(ctx, &found, from) < 0) return -1;
1672
1673 /*
1674 * List to list copy is empty if the src list has no attributes.
1675 */
1676 if (fr_pair_list_empty(&found)) return 0;
1677
1678 fr_pair_list_foreach(&found, vp) {
1679 vp->op = T_OP_ADD_EQ;
1680 }
1681
1682 fr_pair_list_append(out, &found);
1683
1684 return 0;
1685 }
1686
1687 /*
1688 * And parse the RHS
1689 */
1690 switch (map->rhs->type) {
1691 case TMPL_TYPE_XLAT:
1692 fr_assert(tmpl_is_attr(map->lhs));
1693 fr_assert(tmpl_attr_tail_da(map->lhs)); /* We need to know which attribute to create */
1694 fr_assert(tmpl_xlat(map->rhs) != NULL);
1695
1697
1698 /*
1699 * We do the debug printing because xlat_aeval_compiled
1700 * doesn't have access to the original string. It's been
1701 * mangled during the parsing to an internal data structure
1702 */
1703 RDEBUG2("EXPAND %s", map->rhs->name);
1704 RINDENT();
1705
1706 str = NULL;
1707 slen = xlat_aeval_compiled(request, &str, request, tmpl_xlat(map->rhs), NULL, NULL);
1708 REXDENT();
1709
1710 if (slen < 0) {
1711 rcode = slen;
1712 goto error;
1713 }
1714
1715 RDEBUG2("--> %s", str);
1716
1717 rcode = fr_pair_value_from_str(n, str, strlen(str), NULL, false);
1718 talloc_free(str);
1719 if (rcode < 0) {
1720 goto error;
1721 }
1722
1723 n->op = map->op;
1725 break;
1726
1728 fr_assert(tmpl_is_attr(map->lhs));
1729 fr_assert(tmpl_attr_tail_da(map->lhs)); /* We need to know which attribute to create */
1730
1732
1733 if (fr_pair_value_from_str(n, map->rhs->name, strlen(map->rhs->name), NULL, false) < 0) {
1734 rcode = 0;
1735 goto error;
1736 }
1737 n->op = map->op;
1739 break;
1740
1741 case TMPL_TYPE_ATTR:
1742 {
1743 fr_pair_t *vp;
1744 fr_dcursor_t from;
1745
1747
1748 /*
1749 * @todo should log error, and return -1 for v3.1 (causes update to fail)
1750 */
1751 if (tmpl_copy_pairs(ctx, &found, request, map->rhs) < 0) return 0;
1752
1753 vp = fr_pair_dcursor_init(&from, &found);
1754
1755 /*
1756 * Src/Dst attributes don't match, convert src attributes
1757 * to match dst.
1758 */
1759 if (tmpl_is_attr(map->lhs) && tmpl_attr_tail_da_is_leaf(map->lhs) &&
1760 (tmpl_attr_tail_da(map->rhs)->type != tmpl_attr_tail_da(map->lhs)->type)) {
1761 for (; vp; vp = fr_dcursor_current(&from)) {
1763
1764 if (fr_value_box_cast(n, &n->data,
1765 tmpl_attr_tail_da(map->lhs)->type, tmpl_attr_tail_da(map->lhs), &vp->data) < 0) {
1766 RPEDEBUG("Attribute conversion failed");
1767 fr_pair_list_free(&found);
1768 talloc_free(n);
1769 return -1;
1770 }
1771 vp = fr_dcursor_remove(&from); /* advances cursor */
1772 talloc_free(vp);
1773
1774 fr_assert((n->vp_type != FR_TYPE_STRING) || (n->vp_strvalue != NULL));
1775
1776 n->op = map->op;
1778 }
1779
1780 return 0;
1781 }
1782
1783 /*
1784 * Otherwise we just need to fixup the attribute types
1785 * and operators
1786 */
1787 for (; vp; vp = fr_dcursor_next(&from)) {
1789 vp->op = map->op;
1790 }
1791 fr_pair_list_append(out, &found);
1792 }
1793 break;
1794
1795 case TMPL_TYPE_DATA:
1797 fr_assert(tmpl_is_attr(map->lhs));
1798
1800
1801 if (tmpl_attr_tail_da(map->lhs)->type == tmpl_value_type(map->rhs)) {
1802 if (unlikely(fr_value_box_copy(n, &n->data, tmpl_value(map->rhs)) < 0)) {
1803 rcode = -1;
1804 talloc_free(n);
1805 goto error;
1806 }
1807
1808 } else if (fr_value_box_cast(n, &n->data, n->vp_type, n->da, tmpl_value(map->rhs)) < 0) {
1809 RPEDEBUG("Implicit cast failed");
1810 rcode = -1;
1811 goto error;
1812 }
1813 n->op = map->op;
1815
1816 MAP_VERIFY(map);
1817 break;
1818
1819 /*
1820 * This essentially does the same as rlm_exec xlat, except it's non-configurable.
1821 * It's only really here as a convenience for people who expect the contents of
1822 * backticks to be executed in a shell.
1823 *
1824 * exec string is xlat expanded and arguments are shell escaped.
1825 */
1826 case TMPL_TYPE_EXEC:
1827 return map_exec_to_vp(ctx, out, request, map);
1828
1829 default:
1830 fr_assert(0); /* Should have been caught at parse time */
1831
1832 error:
1833 talloc_free(n);
1834 return rcode;
1835 }
1836
1837 return 0;
1838}
1839
1840#define DEBUG_OVERWRITE(_old, _new) \
1841do {\
1842 if (RDEBUG_ENABLED3) {\
1843 char *our_old; \
1844 char *our_new; \
1845 fr_pair_aprint_value_quoted(request, &our_old, _old, T_DOUBLE_QUOTED_STRING); \
1846 fr_pair_aprint_value_quoted(request, &our_new, _new, T_DOUBLE_QUOTED_STRING); \
1847 RINDENT(); \
1848 RDEBUG3("--> overwriting %s with %s", our_old, our_new); \
1849 REXDENT(); \
1850 talloc_free(our_old); \
1851 talloc_free(our_new); \
1852 } \
1853} while (0)
1854
1855/** Convert #map_t to #fr_pair_t (s) and add them to a #request_t.
1856 *
1857 * Takes a single #map_t, resolves request and list identifiers
1858 * to pointers in the current request, then attempts to retrieve module
1859 * specific value(s) using callback, and adds the resulting values to the
1860 * correct request/list.
1861 *
1862 * @param request The current request.
1863 * @param map specifying destination attribute and location and src identifier.
1864 * @param func to retrieve module specific values and convert them to
1865 * #fr_pair_t.
1866 * @param ctx to be passed to func.
1867 * @return
1868 * - -1 if the operation failed.
1869 * - -2 in the source attribute wasn't valid.
1870 * - 0 on success.
1871 */
1872int map_to_request(request_t *request, map_t const *map, radius_map_getvalue_t func, void *ctx)
1873{
1874 int rcode = 0;
1875 fr_pair_t *dst;
1876 fr_pair_list_t *list, src_list;
1877 request_t *context, *tmp_ctx = NULL;
1878 TALLOC_CTX *parent;
1879 fr_dcursor_t dst_list;
1880
1881 bool found = false;
1882
1883 map_t exp_map;
1884 tmpl_t *exp_lhs;
1885 fr_dict_attr_t const *list_ref;
1886
1887 tmpl_dcursor_ctx_t cc = {};
1888
1889 fr_pair_list_init(&src_list);
1890 MAP_VERIFY(map);
1891 fr_assert(map->lhs != NULL);
1892 fr_assert(map->rhs != NULL);
1893
1894 /*
1895 * This function is called only when creating attributes. It cannot be called for conditions.
1896 */
1897 if (fr_comparison_op[map->op]) {
1898 REDEBUG("Invalid operator in %s %s ...", map->lhs->name, fr_tokens[map->op]);
1899 return -1;
1900 }
1901
1902 tmp_ctx = talloc_pool(request, 1024);
1903
1904 /*
1905 * Preprocessing of the LHS of the map.
1906 */
1907 switch (map->lhs->type) {
1908 /*
1909 * Already in the correct form.
1910 */
1911 case TMPL_TYPE_ATTR:
1912 break;
1913
1914 /*
1915 * Everything else gets expanded, then re-parsed as an attribute reference.
1916 *
1917 * This allows the syntax like:
1918 * - "Attr-%{number}" := "value"
1919 */
1920 case TMPL_TYPE_XLAT:
1921 case TMPL_TYPE_EXEC:
1922 {
1923 char *attr_str;
1924 ssize_t slen;
1925
1926 slen = tmpl_aexpand(request, &attr_str, request, map->lhs, NULL, NULL);
1927 if (slen <= 0) {
1928 RPEDEBUG("Left side expansion failed");
1929 fr_assert(!attr_str);
1930 rcode = -1;
1931 goto finish;
1932 }
1933
1934 slen = tmpl_afrom_attr_str(tmp_ctx, NULL, &exp_lhs, attr_str,
1935 &(tmpl_rules_t){
1936 .attr = {
1937 .dict_def = request->local_dict,
1938 .list_def = request_attr_request,
1939 }
1940 });
1941 if (slen <= 0) {
1942 RPEDEBUG("Left side expansion result \"%s\" is not an attribute reference", attr_str);
1943 talloc_free(attr_str);
1944 rcode = -1;
1945 goto finish;
1946 }
1947 fr_assert(tmpl_is_attr(exp_lhs));
1948
1949 memcpy(&exp_map, map, sizeof(exp_map));
1950 exp_map.lhs = exp_lhs;
1951 map = &exp_map;
1952 }
1953 break;
1954
1955 default:
1956 fr_assert(0);
1957 break;
1958 }
1959
1960
1961 /*
1962 * Sanity check inputs. We can have a list or attribute
1963 * as a destination.
1964 */
1965 if (!tmpl_is_attr(map->lhs)) {
1966 REDEBUG("Left side \"%.*s\" of map should be an attr or list but is an %s",
1967 (int)map->lhs->len, map->lhs->name,
1968 tmpl_type_to_str(map->lhs->type));
1969 rcode = -2;
1970 goto finish;
1971 }
1972
1973 context = request;
1974 if (tmpl_request_ptr(&context, tmpl_request(map->lhs)) < 0) {
1975 RPEDEBUG("Mapping \"%.*s\" -> \"%.*s\" cannot be performed due to error in left side of map",
1976 (int)map->rhs->len, map->rhs->name, (int)map->lhs->len, map->lhs->name);
1977 rcode = -2;
1978 goto finish;
1979 }
1980
1981 list_ref = tmpl_list(map->lhs);
1982 list = tmpl_list_head(context, list_ref);
1983 if (!list) {
1984 REDEBUG("Mapping \"%.*s\" -> \"%.*s\" cannot be performed due to to invalid list qualifier \"%s\" in left side of map",
1985 (int)map->rhs->len, map->rhs->name, (int)map->lhs->len, map->lhs->name,
1986 tmpl_list_name(list_ref, "<INVALID>"));
1987 rcode = -2;
1988 goto finish;
1989 }
1990
1993
1994 /*
1995 * The callback should either return -1 to signify operations error,
1996 * -2 when it can't find the attribute or list being referenced, or
1997 * 0 to signify success. It may return "success", but still have no
1998 * VPs to work with.
1999 */
2000 rcode = func(parent, &src_list, request, map, ctx);
2001 if (rcode < 0) {
2002 fr_assert(fr_pair_list_empty(&src_list));
2003 goto finish;
2004 }
2005 if (fr_pair_list_empty(&src_list)) {
2006 RDEBUG2("%.*s skipped: No values available", (int)map->lhs->len, map->lhs->name);
2007 goto finish;
2008 }
2009
2010 /*
2011 * Print the VPs
2012 */
2013#ifndef WITH_VERIFY_PTR
2014 if (RDEBUG_ENABLED)
2015#endif
2016 {
2017 fr_pair_list_foreach(&src_list, vp) {
2018 PAIR_VERIFY(vp);
2019
2020 if (RDEBUG_ENABLED) map_debug_log(request, map, vp);
2021 }
2022 }
2023
2024 /*
2025 * The destination is a list (which is a completely different set of operations)
2026 */
2027 if (tmpl_is_list(map->lhs)) {
2028 switch (map->op) {
2029 case T_OP_CMP_FALSE:
2030 /* We don't need the src VPs (should just be 'ANY') */
2031 fr_assert(fr_pair_list_empty(&src_list));
2032
2033 /* Clear the entire dst list */
2034 fr_pair_list_free(list);
2035 goto finish;
2036
2037 case T_OP_SET:
2038 if (tmpl_is_list(map->rhs)) {
2039 fr_pair_list_free(list);
2040 fr_pair_list_append(list, &src_list);
2041 fr_pair_list_init(&src_list);
2042 } else {
2044
2045 case T_OP_EQ:
2046 fr_assert(tmpl_is_exec(map->rhs));
2048
2049 case T_OP_ADD_EQ:
2050 fr_pair_list_move_op(list, &src_list, T_OP_ADD_EQ);
2051 }
2052 goto update;
2053
2054 case T_OP_PREPEND:
2055 fr_pair_list_move_op(list, &src_list, T_OP_PREPEND);
2056 goto update;
2057
2058 default:
2059 fr_pair_list_free(&src_list);
2060 rcode = -1;
2061 goto finish;
2062 }
2063 }
2064
2065 /*
2066 * Find the destination attribute. We leave with either
2067 * the dst_list and vp pointing to the attribute or the VP
2068 * being NULL (no attribute at that index).
2069 */
2070 dst = tmpl_dcursor_init(NULL, tmp_ctx, &cc, &dst_list, request, map->lhs);
2071 /*
2072 * The destination is an attribute
2073 */
2074 switch (map->op) {
2075 default:
2076 break;
2077 /*
2078 * !* - Remove all attributes which match dst in the specified list.
2079 * This doesn't use attributes returned by the func(), and immediately frees them.
2080 */
2081 case T_OP_CMP_FALSE:
2082 /* We don't need the src VPs (should just be 'ANY') */
2083 fr_assert(fr_pair_list_empty(&src_list));
2084 if (!dst) goto finish;
2085
2086 /*
2087 * Wildcard: delete all of the matching ones
2088 */
2089 if (tmpl_attr_tail_num(map->lhs) == NUM_UNSPEC) {
2090 fr_pair_delete_by_child_num(list, tmpl_attr_tail_da(map->lhs)->parent, tmpl_attr_tail_da(map->lhs)->attr);
2091 dst = NULL;
2092 /*
2093 * We've found the Nth one. Delete it, and only it.
2094 */
2095 } else {
2096 dst = fr_dcursor_remove(&dst_list);
2097 talloc_free(dst);
2098 }
2099
2100 /*
2101 * Check that the User-Name and User-Password
2102 * caches point to the correct attribute.
2103 */
2104 goto update;
2105
2106 /*
2107 * -= - Delete attributes in the dst list which match any of the
2108 * src_list attributes.
2109 *
2110 * This operation has two modes:
2111 * - If tmpl_attr_tail_num(map->lhs) > 0, we check each of the src_list attributes against
2112 * the dst attribute, to see if any of their values match.
2113 * - If tmpl_attr_tail_num(map->lhs) == NUM_UNSPEC, we compare all instances of the dst attribute
2114 * against each of the src_list attributes.
2115 */
2116 case T_OP_SUB_EQ:
2117 /* We didn't find any attributes earlier */
2118 if (!dst) {
2119 fr_pair_list_free(&src_list);
2120 goto finish;
2121 }
2122
2123 /*
2124 * Instance specific[n] delete
2125 */
2126 if (tmpl_attr_tail_num(map->lhs) != NUM_UNSPEC) {
2127 fr_pair_list_foreach(&src_list, vp) {
2128 vp->op = T_OP_CMP_EQ;
2129 rcode = paircmp_pairs(request, vp, dst);
2130 if (rcode == 0) {
2131 dst = fr_dcursor_remove(&dst_list);
2132 talloc_free(dst);
2133 found = true;
2134 }
2135 }
2136 rcode = 0;
2137 fr_pair_list_free(&src_list);
2138 if (!found) goto finish;
2139 goto update;
2140 }
2141
2142 /*
2143 * All instances[*] delete
2144 */
2145 for (dst = fr_dcursor_current(&dst_list);
2146 dst;
2148 fr_pair_list_foreach(&src_list, vp) {
2149 vp->op = T_OP_CMP_EQ;
2150 rcode = paircmp_pairs(request, vp, dst);
2151 if (rcode == 0) {
2152 dst = fr_dcursor_remove(&dst_list);
2153 talloc_free(dst);
2154 found = true;
2155 }
2156 }
2157 }
2158 rcode = 0;
2159 fr_pair_list_free(&src_list);
2160 if (!found) goto finish;
2161 goto update;
2162 }
2163
2164 switch (map->op) {
2165 /*
2166 * = - Set only if not already set
2167 */
2168 case T_OP_EQ:
2169 {
2170 tmpl_attr_extent_t *extent = NULL;
2171 fr_dlist_head_t leaf;
2172 fr_dlist_head_t interior;
2173 fr_pair_t *src_vp;
2174
2175 if (dst) {
2176 RDEBUG3("Refusing to overwrite (use :=)");
2177 fr_pair_list_free(&src_list);
2178 goto finish;
2179 }
2180
2182 fr_dlist_talloc_init(&interior, tmpl_attr_extent_t, entry);
2183
2184 /*
2185 * Find out what we need to build and build it
2186 */
2187 if ((tmpl_extents_find(tmp_ctx, &leaf, &interior, request, map->lhs) < 0) ||
2188 (tmpl_extents_build_to_leaf_parent(&leaf, &interior, map->lhs) < 0)) {
2189 fr_dlist_talloc_free(&leaf);
2190 fr_dlist_talloc_free(&interior);
2191 rcode = -1;
2192 goto finish;
2193 }
2194
2195 /*
2196 * Need to copy src to all dsts
2197 */
2198 src_vp = fr_pair_list_head(&src_list);
2199 if (!src_vp) {
2200 fr_dlist_talloc_free(&leaf);
2201 rcode = -1;
2202 goto finish;
2203 }
2204
2205 if (fr_dlist_num_elements(&leaf) > 1) {
2206 while ((extent = fr_dlist_tail(&leaf))) {
2207 fr_pair_append(extent->list, fr_pair_copy(extent->list_ctx, src_vp));
2209 }
2210 } else {
2211 extent = fr_dlist_head(&leaf);
2212 fr_pair_append(extent->list, fr_pair_copy(extent->list_ctx, src_vp));
2214 }
2215
2216 /* Free any we didn't insert */
2217 fr_pair_list_free(&src_list);
2218 fr_assert(fr_dlist_num_elements(&interior) == 0);
2219 fr_assert(fr_dlist_num_elements(&leaf) == 0);
2220 }
2221 break;
2222
2223 /*
2224 * := - Overwrite existing attribute with last src_list attribute
2225 */
2226 case T_OP_SET:
2227 {
2228 tmpl_attr_extent_t *extent = NULL;
2229 fr_dlist_head_t leaf;
2230 fr_dlist_head_t interior;
2231 fr_pair_t *src_vp;
2232
2233 src_vp = fr_pair_list_tail(&src_list);
2234
2235 if (dst) {
2236 DEBUG_OVERWRITE(dst, src_vp);
2237
2238 fr_pair_reinit_from_da(NULL, dst, src_vp->da);
2239 fr_pair_value_copy(dst, src_vp);
2240
2241 goto op_set_done;
2242 }
2243
2245 fr_dlist_talloc_init(&interior, tmpl_attr_extent_t, entry);
2246
2247 /*
2248 * Find out what we need to build and build it
2249 */
2250 if ((tmpl_extents_find(tmp_ctx, &leaf, &interior, request, map->lhs) < 0) ||
2251 (tmpl_extents_build_to_leaf_parent(&leaf, &interior, map->lhs) < 0)) {
2252 op_set_error:
2253 fr_dlist_talloc_free(&leaf);
2254 fr_dlist_talloc_free(&interior);
2255 rcode = -1;
2256 goto finish;
2257 }
2258
2259 if (fr_dlist_num_elements(&leaf) > 1) {
2260 ERROR("Not yet supported");
2261
2262 goto op_set_error;
2263 } else {
2264 extent = fr_dlist_head(&leaf);
2265 fr_pair_append(extent->list, fr_pair_copy(extent->list_ctx, src_vp));
2266 }
2267
2268 fr_assert(fr_dlist_num_elements(&interior) == 0);
2269 fr_dlist_talloc_free(&leaf);
2270
2271 op_set_done:
2272 /* Free any we didn't insert */
2273 fr_pair_list_free(&src_list);
2274 }
2275 break;
2276
2277 /*
2278 * ^= - Prepend src_list attributes to the destination
2279 */
2280 case T_OP_PREPEND:
2281 fr_pair_list_prepend(list, &src_list);
2282 fr_pair_list_free(&src_list);
2283 break;
2284
2285 /*
2286 * += - Add all src_list attributes to the destination
2287 */
2288 case T_OP_ADD_EQ:
2289 {
2290 tmpl_attr_extent_t *extent = NULL;
2291 fr_dlist_head_t leaf;
2292 fr_dlist_head_t interior;
2293
2295 fr_dlist_talloc_init(&interior, tmpl_attr_extent_t, entry);
2296
2297 /*
2298 * Find out what we need to build and build it
2299 */
2300 if ((tmpl_extents_find(tmp_ctx, &leaf, &interior, request, map->lhs) < 0) ||
2301 (tmpl_extents_build_to_leaf_parent(&leaf, &interior, map->lhs) < 0)) {
2302 fr_dlist_talloc_free(&leaf);
2303 fr_dlist_talloc_free(&interior);
2304 rcode = -1;
2305 goto finish;
2306 }
2307
2308 if (fr_dlist_num_elements(&leaf) > 1) {
2309 while ((extent = fr_dlist_tail(&leaf))) {
2310 (void) fr_pair_list_copy(extent->list_ctx, extent->list, &src_list);
2312 }
2313 /* Free all the src vps */
2314 fr_pair_list_free(&src_list);
2315 } else {
2316 extent = fr_dlist_head(&leaf);
2317 (void) fr_pair_list_copy(extent->list_ctx, extent->list, &src_list);
2319 }
2320
2321 fr_pair_list_free(&src_list);
2322 fr_assert(fr_dlist_num_elements(&interior) == 0);
2323 fr_assert(fr_dlist_num_elements(&leaf) == 0);
2324 }
2325 break;
2326
2327 /*
2328 * Filter operators
2329 */
2330 case T_OP_NE:
2331 case T_OP_CMP_EQ:
2332 case T_OP_GE:
2333 case T_OP_GT:
2334 case T_OP_LE:
2335 case T_OP_LT:
2336 {
2337 fr_pair_t *a;
2338
2341
2342 fr_dcursor_head(&dst_list);
2343
2344 fr_pair_list_foreach(&src_list, b) {
2345 for (a = fr_dcursor_current(&dst_list);
2346 a;
2347 a = fr_dcursor_next(&dst_list)) {
2348 int8_t cmp;
2349
2350 cmp = fr_pair_cmp_by_da(a, b); /* attribute and tag match */
2351 if (cmp > 0) break;
2352 else if (cmp < 0) continue;
2353
2354 cmp = (fr_value_box_cmp_op(map->op, &a->data, &b->data) == 0);
2355 if (cmp != 0) {
2356 a = fr_dcursor_remove(&dst_list);
2357 talloc_free(a);
2358 }
2359 }
2360 if (!a) break; /* end of the list */
2361 }
2362 fr_pair_list_free(&src_list);
2363 }
2364 break;
2365
2366 default:
2367 fr_assert(0); /* Should have been caught be the caller */
2368 rcode = -1;
2369 goto finish;
2370 }
2371
2372update:
2373 fr_assert(fr_pair_list_empty(&src_list));
2374
2375finish:
2376 tmpl_dcursor_clear(&cc);
2377 talloc_free(tmp_ctx);
2378 return rcode;
2379}
2380
2381/** Print a map to a string
2382 *
2383 * @param[out] out Buffer to write string to.
2384 * @param[in] map to print.
2385 * @return
2386 * - The number of bytes written to the out buffer.
2387 * - A number >= outlen if truncation has occurred.
2388 */
2390{
2391 fr_sbuff_t our_out = FR_SBUFF(out);
2392
2393 MAP_VERIFY(map);
2394
2395 /*
2396 * Print the lhs
2397 */
2398 if (tmpl_rules_cast(map->lhs)) {
2399 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "<%s>",
2401 }
2402 FR_SBUFF_RETURN(tmpl_print_quoted, &our_out, map->lhs);
2403
2404 /*
2405 * Print separators and operator
2406 */
2407 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
2409 FR_SBUFF_IN_CHAR_RETURN(&our_out, ' ');
2410
2411 /*
2412 * The RHS doesn't matter for many operators
2413 */
2414 if ((map->op == T_OP_CMP_TRUE) || (map->op == T_OP_CMP_FALSE)) {
2415 FR_SBUFF_IN_STRCPY_RETURN(&our_out, "ANY");
2416 FR_SBUFF_SET_RETURN(out, &our_out);
2417 }
2418
2419 /*
2420 * If there's no child and no RHS then the
2421 * map was invalid.
2422 */
2423 if (map_list_empty(&map->child) && !fr_cond_assert(map->rhs != NULL)) {
2424 fr_sbuff_terminate(out);
2425 return 0;
2426 }
2427
2428 if (tmpl_rules_cast(map->rhs)) {
2429 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "<%s>",
2431 }
2432
2433 /*
2434 * Print the RHS.
2435 */
2436 FR_SBUFF_RETURN(tmpl_print_quoted, &our_out, map->rhs);
2437
2438 FR_SBUFF_SET_RETURN(out, &our_out);
2439}
2440
2441/*
2442 * Debug print a map / VP
2443 */
2444void map_debug_log(request_t *request, map_t const *map, fr_pair_t const *vp)
2445{
2446 char *rhs = NULL, *value = NULL;
2447 char buffer[256];
2448
2449 MAP_VERIFY(map);
2450 if (!fr_cond_assert(map->lhs != NULL)) return;
2451 if (!fr_cond_assert(map->rhs != NULL)) return;
2452
2453 fr_assert(vp);
2454
2455 switch (map->rhs->type) {
2456 /*
2457 * Just print the value being assigned
2458 */
2459 default:
2461 fr_pair_aprint_value_quoted(request, &rhs, vp, map->rhs->quote);
2462 break;
2463
2464 case TMPL_TYPE_XLAT:
2465 fr_pair_aprint_value_quoted(request, &rhs, vp, map->rhs->quote);
2466 break;
2467
2468 case TMPL_TYPE_DATA:
2469 fr_pair_aprint_value_quoted(request, &rhs, vp, map->rhs->quote);
2470 break;
2471
2472 case TMPL_TYPE_ATTR:
2473 {
2474 fr_token_t quote;
2475
2476 switch (vp->vp_type) {
2477 case FR_TYPE_QUOTED:
2478 quote = T_DOUBLE_QUOTED_STRING;
2479 break;
2480 default:
2481 quote = T_BARE_WORD;
2482 break;
2483 }
2484
2485 /*
2486 * Not appropriate to use map->rhs->quote here, as that's the quoting
2487 * around the attr ref. The attribute value has no quoting, so we choose
2488 * the quoting based on the data type.
2489 */
2490 fr_pair_aprint_value_quoted(request, &value, vp, quote);
2491 tmpl_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), map->rhs, NULL);
2492 rhs = talloc_typed_asprintf(request, "%s -> %s", buffer, value);
2493 }
2494 break;
2495 }
2496
2497 switch (map->lhs->type) {
2498 case TMPL_TYPE_ATTR:
2499 tmpl_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), map->lhs, NULL);
2500 RDEBUG2("%s %s %s", buffer, fr_table_str_by_value(fr_tokens_table, vp ? vp->op : map->op, "<INVALID>"), rhs);
2501 break;
2502
2503 default:
2504 break;
2505 }
2506
2507 /*
2508 * Must be LIFO free order so we don't leak pool memory
2509 */
2510 talloc_free(rhs);
2512}
2513
2514/** Convert a fr_pair_t into a map
2515 *
2516 * @param[in] ctx where to allocate the map.
2517 * @param[out] out Where to write the new map (must be freed with talloc_free()).
2518 * @param[in,out] parent_p the parent map, updated for relative maps
2519 * @param[in] request the request
2520 * @param[in] lhs of map
2521 * @param[in] op_str operator for map
2522 * @param[in] rhs of map
2523 * @param[in] lhs_rules for parsing the LHS
2524 * @param[in] rhs_rules for parsing the RHS
2525 * @param[in] bare_word_only RHS is bare words, and nothing else.
2526 * @return
2527 * - 0 on success.
2528 * - -1 on failure.
2529 */
2530int map_afrom_fields(TALLOC_CTX *ctx, map_t **out, map_t **parent_p, request_t *request,
2531 char const *lhs, char const *op_str, char const *rhs,
2532 tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, bool bare_word_only)
2533{
2534 ssize_t slen;
2535 fr_token_t quote, op;
2536 map_t *map;
2537 map_t *parent = *parent_p;
2538 tmpl_rules_t my_rules;
2539
2541 if (op == T_INVALID) {
2542 fr_strerror_printf("Invalid operator '%s'", op_str);
2543 return -1;
2544 }
2545
2546 /*
2547 * Reply items can be expanded. Check items cannot be,
2548 * unless the operator is a comparison operator.
2549 */
2550 if (!bare_word_only) {
2551 if (!fr_assignment_op[op]) {
2552 fr_assert(0);
2553 fr_strerror_printf("Invalid operator '%s' for assignment in reply item", op_str);
2554 return -1;
2555 }
2556
2557 } else if (!fr_assignment_op[op] && !fr_comparison_op[op]) {
2558 if (!fr_assignment_op[op]) {
2559 fr_strerror_printf("Invalid operator '%s' for check item", op_str);
2560 return -1;
2561 }
2562 }
2563
2564 my_rules = *lhs_rules;
2565 lhs_rules = &my_rules;
2566
2567 /*
2568 * We're only called from SQL. If the default list is request, then we only use that for
2569 * comparisons. We rewrite assignments to use the control list.
2570 *
2571 * @todo - as we expand the use of this function, perhaps add another argument which controls
2572 * this flag. But this function already has parameter overload :(
2573 */
2574 if (fr_assignment_op[op] && (lhs_rules->attr.list_def == request_attr_request)) {
2576 }
2577
2578 /*
2579 * One '.' means "the current parent".
2580 */
2581 if (*lhs == '.') {
2582 if (!parent) {
2583 no_parent:
2584 fr_strerror_const("Unexpected location for relative attribute - no parent attribute exists");
2585 return -1;
2586 }
2587 lhs++;
2588
2589 /*
2590 * Multiple '.' means "go to our parents parent".
2591 */
2592 while (*lhs == '.') {
2593 if (!parent) goto no_parent;
2594
2595 parent = parent->parent;
2596 lhs++;
2597 }
2598
2599 /*
2600 * Child elements can only be "=".
2601 */
2602 if (parent) {
2603 if (fr_comparison_op[op]) {
2604 fr_strerror_const("Comparison operators cannot be used inside of structural data types");
2605 return -1;
2606 }
2607
2608 if (op != T_OP_EQ) {
2609 fr_strerror_const("Invalid operator inside of structural data type - must be '='");
2610 return -1;
2611 }
2612 }
2613 }
2614
2615 MEM(map = map_alloc(ctx, parent));
2616 map->op = op;
2617
2618 /*
2619 * Start looking in the correct parent, not in whatever we were handed.
2620 */
2621 if (parent) {
2623 my_rules.attr.namespace = tmpl_attr_tail_da(parent->lhs);
2624
2625 slen = tmpl_afrom_attr_substr(map, NULL, &map->lhs, &FR_SBUFF_IN_STR(lhs),
2627 } else {
2628 /*
2629 * There's no '.', so this
2630 * attribute MUST come from the
2631 * root of the dictionary tree.
2632 */
2633 parent = NULL;
2634
2635 /*
2636 * Allocate the LHS, which must be an attribute.
2637 *
2638 * @todo - track relative attributes, which begin with a '.'
2639 */
2640 slen = tmpl_afrom_attr_str(ctx, NULL, &map->lhs, lhs, lhs_rules);
2641 }
2642 if (slen <= 0) {
2643 error:
2644 talloc_free(map);
2645 return -1;
2646 }
2647
2648 if (tmpl_attr_tail_is_unknown(map->lhs) && tmpl_attr_unknown_add(map->lhs) < 0) {
2649 fr_strerror_printf("Failed creating attribute %s", map->lhs->name);
2650 goto error;
2651 }
2652
2653 my_rules = *rhs_rules;
2654 my_rules.at_runtime = true;
2655 my_rules.xlat.runtime_el = unlang_interpret_event_list(request);
2656 my_rules.enumv = tmpl_attr_tail_da(map->lhs);
2657
2658 /*
2659 * LHS is a structureal type. The RHS is either empty (create empty LHS), or it's a string
2660 * containing a list of attributes to create.
2661 */
2662 if (!fr_type_is_leaf(my_rules.enumv->type)) {
2663 my_rules.enumv = NULL;
2664 }
2665
2666 /*
2667 * Try to figure out what we should do with the RHS.
2668 */
2669 if ((map->op == T_OP_CMP_TRUE) || (map->op == T_OP_CMP_FALSE)) {
2670 /*
2671 * These operators require a hard-coded string on the RHS.
2672 */
2673 if (strcmp(rhs, "ANY") != 0) {
2674 fr_strerror_printf("Invalid value %s for operator %s", rhs, fr_tokens[map->op]);
2675 goto error;
2676 }
2677
2678 if (tmpl_afrom_value_box(map, &map->rhs, fr_box_strvalue("ANY"), false) < 0) goto error;
2679
2680 } else if (bare_word_only) {
2681 fr_value_box_t *vb;
2682
2683 /*
2684 * No value, or no enum, parse it as a bare-word string.
2685 */
2686 if (!rhs[0] || !my_rules.enumv) goto do_bare_word;
2687
2688 MEM(vb = fr_value_box_alloc(map, my_rules.enumv->type, my_rules.enumv));
2689
2690 /*
2691 * It MUST be the given data type.
2692 */
2693 slen = fr_value_box_from_str(map, vb, my_rules.enumv->type, my_rules.enumv,
2694 rhs, strlen(rhs), NULL);
2695 if (slen <= 0) goto error;
2696
2697 if (tmpl_afrom_value_box(map, &map->rhs, vb, true) < 0) {
2698 goto error;
2699 }
2700
2701 } else if (rhs[0] == '"') {
2702 /*
2703 * We've been asked to expand the RHS. Passwords like
2704 *
2705 * "%{Calling-Station-ID}"
2706 *
2707 * might not do what you want.
2708 */
2709 quote = T_DOUBLE_QUOTED_STRING;
2710 goto parse_quoted;
2711
2712 } else if (rhs[0] == '\'') {
2713 size_t len;
2714
2715 quote = T_SINGLE_QUOTED_STRING;
2716
2717 parse_quoted:
2718 len = strlen(rhs + 1);
2719 if (len == 1) {
2720 if (rhs[1] != rhs[0]) {
2721 fr_strerror_const("Invalid string on right side");
2722 return -1;
2723 }
2724
2725 rhs = "";
2726 goto alloc_empty;
2727 }
2728
2729 slen = tmpl_afrom_substr(map, &map->rhs, &FR_SBUFF_IN(rhs + 1, len - 1),
2730 quote, value_parse_rules_quoted[quote], &my_rules);
2731 if (slen < 0) {
2732 REDEBUG3("Failed parsing right-hand side as quoted string.");
2733 fail_rhs:
2734 fr_strerror_printf("Failed parsing right-hand side: %s", fr_strerror());
2735 goto error;
2736 }
2737
2738 if (slen == 0) {
2739 rhs = "";
2740 goto alloc_empty;
2741 }
2742
2743 /*
2744 * Ignore any extra data after the string.
2745 *
2746 * @todo - this should likely be a parse error: we didn't parse the entire string!
2747 */
2748
2749 } else if (rhs[0] == '&') {
2750 /*
2751 * No enums here.
2752 */
2754
2755 parse_as_attr:
2756 my_rules.enumv = NULL;
2757
2758 slen = tmpl_afrom_attr_str(map, NULL, &map->rhs, rhs, &my_rules);
2759 if (slen <= 0) {
2760 REDEBUG3("Failed parsing right-hand side as attribute.");
2761 goto fail_rhs;
2762 }
2763
2764 } else if (!rhs[0] || !my_rules.enumv || (my_rules.enumv->type == FR_TYPE_STRING)) {
2765 do_bare_word:
2766 quote = T_BARE_WORD;
2767
2768 if (tmpl_attr_tail_da_is_structural(map->lhs) && !*rhs) goto done;
2769
2770 alloc_empty:
2771 MEM(map->rhs = tmpl_alloc(map, TMPL_TYPE_DATA, quote, rhs, strlen(rhs)));
2772
2773 /*
2774 * Create it when we have
2775 *
2776 * my-struct = ""
2777 */
2778 (void) fr_value_box_strdup(map->rhs, tmpl_value(map->rhs), NULL, rhs, false);
2779
2780 } else {
2781 /*
2782 * Parse it as the given data type.
2783 */
2784 slen = tmpl_afrom_substr(map, &map->rhs, &FR_SBUFF_IN_STR(rhs),
2786 if (slen <= 0) {
2787 goto parse_as_attr;
2788 }
2789
2790 /*
2791 * Xlat expansions are cast to strings for structural data types.
2792 */
2793 if (tmpl_attr_tail_da_is_structural(map->lhs) && (tmpl_is_xlat(map->rhs))) {
2795 }
2796 }
2797
2798 if (tmpl_needs_resolving(map->rhs)) {
2799 tmpl_res_rules_t tr_rules = (tmpl_res_rules_t) {
2800 .dict_def = lhs_rules->attr.dict_def,
2801 .enumv = tmpl_attr_tail_da(map->lhs)
2802 };
2803
2805
2806 if (tmpl_resolve(map->rhs, &tr_rules) < 0) {
2807 REDEBUG3("Failed resolving right-hand side.");
2808 goto fail_rhs;
2809 }
2810 }
2811
2812 /*
2813 * @todo - check that the entire string was parsed.
2814 */
2815
2816done:
2817 /*
2818 * If the tail is a leaf, we don't change parent.
2819 * Otherwise the structural attribute is the new parent.
2820 */
2821 if (tmpl_attr_tail_da_is_leaf(map->lhs)) {
2822 *parent_p = parent;
2823 } else {
2824 *parent_p = map;
2825 }
2826
2827 MAP_VERIFY(map);
2828
2829 if (parent) map_list_insert_tail(&parent->child, map);
2830 *out = map;
2831
2832 return 0;
2833}
static int const char char buffer[256]
Definition acutest.h:576
int n
Definition acutest.h:577
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
static char const * spaces
Definition dependency.c:360
#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:2702
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4749
static fr_slen_t in
Definition dict.h:861
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:185
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:2009
#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:1192
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:2530
#define DEBUG_OVERWRITE(_old, _new)
Definition map.c:1840
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:1592
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:1161
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:1272
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:1130
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:1377
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:1458
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:1872
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:1526
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:1346
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:1427
ssize_t map_print(fr_sbuff_t *out, map_t const *map)
Print a map to a string.
Definition map.c:2389
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:2444
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:3402
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:2321
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:2594
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:1345
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:287
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:1806
int fr_pair_value_copy(fr_pair_t *dst, fr_pair_t *src)
Copy the value from one pair to another.
Definition pair.c:2568
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:318
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:495
int8_t fr_pair_cmp_by_da(void const *a, void const *b)
Order attributes by their da, and tag.
Definition pair.c:1842
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:81
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG2(fmt,...)
Definition radclient.h:54
#define RDEBUG_ENABLED()
Definition radclient.h:49
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:192
#define FR_SBUFF_RETURN(_func, _sbuff,...)
#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:634
#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:610
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:338
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:392
#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:336
bool at_runtime
Produce an ephemeral/runtime tmpl.
Definition tmpl.h:344
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:365
#define NUM_UNSPEC
Definition tmpl.h:390
fr_slen_t tmpl_request_ref_list_afrom_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err, FR_DLIST_HEAD(tmpl_request_list) _CONST **out, fr_sbuff_t *in)
#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:335
fr_pair_list_t * list
List that we tried to evaluate ar in and failed.
Definition tmpl.h:612
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:1062
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:324
#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:601
Similar to tmpl_rules_t, but used to specify parameters that may change during subsequent resolution ...
Definition tmpl.h:364
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:332
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:320
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:318
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 TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:112
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:1836
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:3135
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define PAIR_VERIFY(_x)
Definition pair.h:193
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:263
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:589
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:841
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:3730
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:4148
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:5754
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:4368
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:5117
#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