The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
xlat_expr.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: 3e86bea42c69733947f6311ec47e4af7f2932485 $
19 *
20 * @file xlat_expr.c
21 * @brief Tokenizers and support functions for xlat expressions
22 *
23 * @copyright 2021 The FreeRADIUS server project
24 * @copyright 2021 Network RADIUS SAS (legal@networkradius.com)
25 */
26
27RCSID("$Id: 3e86bea42c69733947f6311ec47e4af7f2932485 $")
28
29#include <freeradius-devel/server/base.h>
30#include <freeradius-devel/unlang/xlat_priv.h>
31#include <freeradius-devel/util/calc.h>
32#include <freeradius-devel/server/tmpl_dcursor.h>
33#include <freeradius-devel/unlang/xlat_func.h>
34
35#undef XLAT_DEBUG
36#ifdef DEBUG_XLAT
37# define XLAT_DEBUG(_fmt, ...) DEBUG3("%s[%i] "_fmt, __FILE__, __LINE__, ##__VA_ARGS__)
38#else
39# define XLAT_DEBUG(...)
40#endif
41
42extern bool tmpl_require_enum_prefix;
43
44/*
45 * The new tokenizer accepts most things which are accepted by the old one. Many of the errors will be
46 * different, though.
47 *
48 * @todo - add a "output" fr_type_t to xlat_t, which is mainly used by the comparison functions. Right
49 * now it will happily parse things like:
50 *
51 * (1 < 2) < 3
52 *
53 * though the result of (1 < 2) is a boolean, so the result is always true. We probably want to have
54 * that as a compile-time error / check. This can probably just be done with xlat_purify() ? which
55 * doesn't need to interpret the LHS, but just knows its limits. We perhaps want a "range compare"
56 * function, which just checks ranges on one side against values on the right.
57 *
58 * Even worse, when we do "((bool) 1) < 3", the RHS is cast to the type of the LHS by
59 * tmpl_afrom_substr(). This is because we pass the LHS data type recursively down, which works most of
60 * the time, but not all of the time. There are currently hacks in the "upcast" code here to fix this,
61 * but it's a hack.
62 *
63 * @todo - add instantiation routines for assignment operations. This lets us do things
64 * like:
65 * if ((&foo += 4) > 6) ...
66 *
67 * However, this would also require us adding an edit list pointer to the xlat evaluation functions,
68 * which is not trivial. Or, maybe we attach it to the request somehow?
69 */
70
71static xlat_exp_t *xlat_exists_alloc(TALLOC_CTX *ctx, xlat_exp_t *child);
72
73static void xlat_func_append_arg(xlat_exp_t *head, xlat_exp_t *node, bool exists)
74{
75 xlat_exp_t *group;
76
77 fr_assert(head->type == XLAT_FUNC);
78
79 if (node->type == XLAT_GROUP) {
80 xlat_exp_insert_tail(head->call.args, node);
81 xlat_flags_merge(&head->flags, &head->call.args->flags);
82 return;
83 }
84
85 /*
86 * Wrap existence checks for attribute reference.
87 */
88 if (exists && (node->type == XLAT_TMPL) && tmpl_contains_attr(node->vpt)) {
89 node = xlat_exists_alloc(head, node);
90 }
91
92 group = xlat_exp_alloc(head->call.args, XLAT_GROUP, NULL, 0);
93 group->quote = T_BARE_WORD;
94
95 xlat_exp_set_name_buffer_shallow(group, node->fmt); /* not entirely correct, but good enough for now */
96 group->flags = node->flags;
97
98 talloc_steal(group->group, node);
99 xlat_exp_insert_tail(group->group, node);
100
101 xlat_exp_insert_tail(head->call.args, group);
102
103 xlat_flags_merge(&head->flags, &head->call.args->flags);
104}
105
106
107/** Allocate a specific cast node.
108 *
109 * With the first argument being a UINT8 of the data type.
110 * See xlat_func_cast() for the implementation.
111 *
112 */
113static xlat_exp_t *xlat_exists_alloc(TALLOC_CTX *ctx, xlat_exp_t *child)
114{
115 xlat_exp_t *node;
116
117 /*
118 * Create an "exists" node.
119 */
120 MEM(node = xlat_exp_alloc(ctx, XLAT_FUNC, "exists", 6));
121 MEM(node->call.func = xlat_func_find("exists", 6));
122 fr_assert(node->call.func != NULL);
123 node->flags = node->call.func->flags;
124
125 fr_assert(child->type == XLAT_TMPL);
126 fr_assert(tmpl_contains_attr(child->vpt));
127 xlat_exp_set_name_buffer_shallow(node, child->vpt->name);
128
129 xlat_func_append_arg(node, child, false);
130
131 return node;
132}
133
134
136{
137 size_t at_in = fr_sbuff_used_total(out);
138
139 FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]);
140 xlat_print_node(out, node->call.args, xlat_exp_head(node->call.args), e_rules, 0);
141
142 return fr_sbuff_used_total(out) - at_in;
143}
144
146{
147 size_t at_in = fr_sbuff_used_total(out);
148 xlat_exp_t *child = xlat_exp_head(node->call.args);
149
150 fr_assert(child != NULL);
151
153 xlat_print_node(out, node->call.args, child, e_rules, 0); /* prints a space after the first argument */
154
155 FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]);
157
158 child = xlat_exp_next(node->call.args, child);
159 fr_assert(child != NULL);
160
161 xlat_print_node(out, node->call.args, child, e_rules, 0);
162
164
165 return fr_sbuff_used_total(out) - at_in;
166}
167
168static int xlat_expr_resolve_binary(xlat_exp_t *node, UNUSED void *inst, xlat_res_rules_t const *xr_rules)
169{
170 xlat_exp_t *arg1, *arg2;
171 xlat_exp_t *a, *b;
172 tmpl_res_rules_t my_tr_rules;
173
174 XLAT_DEBUG("RESOLVE %s\n", node->fmt);
175
176 arg1 = xlat_exp_head(node->call.args);
177 fr_assert(arg1);
178 fr_assert(arg1->type == XLAT_GROUP);
179
180 arg2 = xlat_exp_next(node->call.args, arg1);
181 fr_assert(arg2);
182 fr_assert(arg2->type == XLAT_GROUP);
183
184 a = xlat_exp_head(arg1->group);
185 b = xlat_exp_head(arg2->group);
186
187 /*
188 * We have many things here, just call resolve recursively.
189 */
190 if (xlat_exp_next(arg1->group, a) || (xlat_exp_next(arg2->group, b))) goto resolve;
191
192 /*
193 * Anything else must get resolved at run time.
194 */
195 if ((a->type != XLAT_TMPL) || (b->type != XLAT_TMPL)) goto resolve;
196
197 /*
198 * The tr_rules should always contain dict_def
199 */
200 fr_assert(xr_rules); /* always set by xlat_resolve() */
201 if (xr_rules->tr_rules) {
202 my_tr_rules = *xr_rules->tr_rules;
203 } else {
204 my_tr_rules = (tmpl_res_rules_t) { };
205 }
206
207 /*
208 * The LHS attribute dictates the enumv for the RHS one.
209 */
210 if (tmpl_contains_attr(a->vpt)) {
211 XLAT_DEBUG("\ta - %s %s\n", a->fmt, b->fmt);
212
213 if (a->flags.needs_resolving) {
214 XLAT_DEBUG("\tresolve attr a\n");
215 if (tmpl_resolve(a->vpt, &my_tr_rules) < 0) return -1;
216 a->flags.needs_resolving = false;
217 }
218
219 my_tr_rules.enumv = tmpl_attr_tail_da(a->vpt);
220
221 XLAT_DEBUG("\tresolve other b\n");
222 if (tmpl_resolve(b->vpt, &my_tr_rules) < 0) return -1;
223
224 b->flags.needs_resolving = false;
225 b->flags.pure = tmpl_is_data(b->vpt);
226 b->flags.constant = b->flags.pure;
227 goto flags;
228 }
229
230 if (tmpl_contains_attr(b->vpt)) {
231 XLAT_DEBUG("\tb - %s %s\n", a->fmt, b->fmt);
232
233 if (b->flags.needs_resolving) {
234 XLAT_DEBUG("\tresolve attr b\n");
235 if (tmpl_resolve(b->vpt, &my_tr_rules) < 0) return -1;
236
237 b->flags.needs_resolving = false;
238 }
239
240 my_tr_rules.enumv = tmpl_attr_tail_da(b->vpt);
241
242 XLAT_DEBUG("\tresolve other a\n");
243 if (tmpl_resolve(a->vpt, &my_tr_rules) < 0) return -1;
244
245 a->flags.needs_resolving = false;
246 a->flags.pure = tmpl_is_data(a->vpt);
247 a->flags.constant = a->flags.pure;
248 goto flags;
249 }
250
251resolve:
252 /*
253 * This call will fix everything recursively.
254 */
255 return xlat_resolve(node->call.args, xr_rules);
256
257flags:
258 arg1->flags = arg1->group->flags = a->flags;
259 arg2->flags = arg2->group->flags = b->flags;
260 xlat_flags_merge(&node->call.args->flags, &arg2->flags);
261
264
267
268 node->call.args->flags.needs_resolving = false;
269
270 return 0;
271}
272
274{
275 switch (type) {
276 case FR_TYPE_STRING:
277 fr_value_box_strdup_shallow(vb, NULL, "", false);
278 break;
279
280 case FR_TYPE_OCTETS:
281 fr_value_box_memdup_shallow(vb, NULL, (void const *) "", 0, false);
282 break;
283
284 default:
285 fr_value_box_init(vb, type, NULL, false);
286 break;
287 }
288}
289
291 { .required = false, .type = FR_TYPE_VOID },
292 { .required = false, .type = FR_TYPE_VOID },
294};
295
297 UNUSED xlat_ctx_t const *xctx,
298 request_t *request, fr_value_box_list_t *in,
299 fr_token_t op,
300 fr_type_t default_type, fr_dict_attr_t const *enumv)
301{
302 int rcode;
303 fr_value_box_t *dst, *a, *b;
304 fr_value_box_t one, two;
305
306 MEM(dst = fr_value_box_alloc_null(ctx));
307
308 /*
309 * Each argument is a FR_TYPE_GROUP, with one or more elements in a list.
310 */
311 a = fr_value_box_list_head(in);
312 b = fr_value_box_list_next(in, a);
313
314 if (!a && !b) return XLAT_ACTION_FAIL;
315
316 fr_assert(!a || (a->type == FR_TYPE_GROUP));
317 fr_assert(!b || (b->type == FR_TYPE_GROUP));
318
320
321 if (fr_value_box_list_num_elements(&a->vb_group) > 1) {
322 REDEBUG("Expected one value as the first argument, got %u",
323 fr_value_box_list_num_elements(&a->vb_group));
324 return XLAT_ACTION_FAIL;
325 }
326 a = fr_value_box_list_head(&a->vb_group);
327
328 if (fr_value_box_list_num_elements(&b->vb_group) > 1) {
329 REDEBUG("Expected one value as the second argument, got %u",
330 fr_value_box_list_num_elements(&b->vb_group));
331 return XLAT_ACTION_FAIL;
332 }
333 b = fr_value_box_list_head(&b->vb_group);
334
335 if (!a) {
336 a = &one;
337 fr_value_box_init_zero(a, b->type);
338 }
339
340 if (!b) {
341 b = &two;
342 fr_value_box_init_zero(b, a->type);
343 }
344
345 rcode = fr_value_calc_binary_op(dst, dst, default_type, a, op, b);
346 if (rcode < 0) {
347 talloc_free(dst);
348 return XLAT_ACTION_FAIL;
349 }
350
351 /*
352 * Over-write, but only if it's present. Otherwise leave
353 * any existing enum alone.
354 */
355 if (enumv) dst->enumv = enumv;
356
358 VALUE_BOX_LIST_VERIFY((fr_value_box_list_t *)out->dlist);
359 return XLAT_ACTION_DONE;
360}
361
362#define XLAT_BINARY_FUNC(_name, _op) \
363static xlat_action_t xlat_func_ ## _name(TALLOC_CTX *ctx, fr_dcursor_t *out, \
364 xlat_ctx_t const *xctx, \
365 request_t *request, fr_value_box_list_t *in) \
366{ \
367 return xlat_binary_op(ctx, out, xctx, request, in, _op, FR_TYPE_NULL, NULL); \
368}
369
370XLAT_BINARY_FUNC(op_add, T_ADD)
371XLAT_BINARY_FUNC(op_sub, T_SUB)
372XLAT_BINARY_FUNC(op_mul, T_MUL)
373XLAT_BINARY_FUNC(op_div, T_DIV)
374XLAT_BINARY_FUNC(op_mod, T_MOD)
375XLAT_BINARY_FUNC(op_and, T_AND)
377XLAT_BINARY_FUNC(op_xor, T_XOR)
378XLAT_BINARY_FUNC(op_rshift, T_RSHIFT)
379XLAT_BINARY_FUNC(op_lshift, T_LSHIFT)
380
382 { .required = false, .type = FR_TYPE_VOID },
383 { .required = false, .type = FR_TYPE_VOID },
385};
386
387static xlat_action_t xlat_cmp_op(TALLOC_CTX *ctx, fr_dcursor_t *out,
388 UNUSED xlat_ctx_t const *xctx,
389 UNUSED request_t *request, fr_value_box_list_t *in,
390 fr_token_t op)
391{
392 int rcode;
393 fr_value_box_t *dst, *a, *b;
394
395 /*
396 * Each argument is a FR_TYPE_GROUP, with one or more elements in a list.
397 */
398 a = fr_value_box_list_head(in);
399 b = fr_value_box_list_next(in, a);
400
401 if (!a || !b) return XLAT_ACTION_FAIL;
402
403 fr_assert(a->type == FR_TYPE_GROUP);
404 fr_assert(b->type == FR_TYPE_GROUP);
405
407
409
410 rcode = fr_value_calc_list_cmp(dst, dst, &a->vb_group, op, &b->vb_group);
411 if (rcode < 0) {
412 talloc_free(dst);
413 return XLAT_ACTION_FAIL;
414 }
415
416 fr_assert(dst->type == FR_TYPE_BOOL);
417 dst->enumv = attr_expr_bool_enum;
418
420 VALUE_BOX_LIST_VERIFY((fr_value_box_list_t *)out->dlist);
421 return XLAT_ACTION_DONE;
422}
423
424
425#define XLAT_CMP_FUNC(_name, _op) \
426static xlat_action_t xlat_func_ ## _name(TALLOC_CTX *ctx, fr_dcursor_t *out, \
427 xlat_ctx_t const *xctx, \
428 request_t *request, fr_value_box_list_t *in) \
429{ \
430 return xlat_cmp_op(ctx, out, xctx, request, in, _op); \
431}
432
434XLAT_CMP_FUNC(cmp_ne, T_OP_NE)
435XLAT_CMP_FUNC(cmp_lt, T_OP_LT)
436XLAT_CMP_FUNC(cmp_le, T_OP_LE)
437XLAT_CMP_FUNC(cmp_gt, T_OP_GT)
438XLAT_CMP_FUNC(cmp_ge, T_OP_GE)
441
442typedef struct {
444 regex_t *regex; //!< precompiled regex
445 xlat_exp_t *xlat; //!< to expand
446 fr_regex_flags_t *regex_flags;
448
449typedef struct {
451 fr_value_box_list_t list;
453
454static fr_slen_t xlat_expr_print_regex(fr_sbuff_t *out, xlat_exp_t const *node, void *instance, fr_sbuff_escape_rules_t const *e_rules)
455{
456 size_t at_in = fr_sbuff_used_total(out);
457 xlat_exp_t *child = xlat_exp_head(node->call.args);
458 xlat_regex_inst_t *inst = instance;
459
460 fr_assert(child != NULL);
461
463 xlat_print_node(out, node->call.args, child, e_rules, 0);
464
465 /*
466 * A space is printed after the first argument only if
467 * there's a second one. So add one if we "ate" the second argument.
468 */
470
471 FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]);
473
475
476 if (inst->xlat->quote == T_SINGLE_QUOTED_STRING) FR_SBUFF_IN_CHAR_RETURN(out, 'm');
478 FR_SBUFF_IN_STRCPY_RETURN(out, inst->xlat->vpt->name);
480
481 FR_SBUFF_RETURN(regex_flags_print, out, inst->regex_flags);
482
484
485 return fr_sbuff_used_total(out) - at_in;
486}
487
488
489/*
490 * Each argument is it's own head, because we do NOT always want
491 * to go to the next argument.
492 */
494{
495 xlat_regex_inst_t *inst = talloc_get_type_abort(xctx->inst, xlat_regex_inst_t);
496 xlat_exp_t *lhs, *rhs, *regex;
497
498 lhs = xlat_exp_head(xctx->ex->call.args);
499 rhs = xlat_exp_next(xctx->ex->call.args, lhs);
500
501 (void) fr_dlist_remove(&xctx->ex->call.args->dlist, rhs);
502
503 fr_assert(rhs);
504 fr_assert(rhs->type == XLAT_GROUP);
505 regex = xlat_exp_head(rhs->group);
506 fr_assert(tmpl_contains_regex(regex->vpt));
507
508 inst->op = xctx->ex->call.func->token;
509 inst->regex_flags = tmpl_regex_flags(regex->vpt);
510
511 inst->xlat = talloc_steal(inst, regex);
512 talloc_free(rhs); /* group wrapper is no longer needed */
513
514 /*
515 * The RHS is more then just one regex node, it has to be dynamically expanded.
516 */
517 if (tmpl_contains_xlat(regex->vpt)) {
518 return 0;
519 }
520
521 if (tmpl_is_data_unresolved(regex->vpt)) {
522 fr_strerror_const("Regex must be resolved before instantiation");
523 return -1;
524 }
525
526 /*
527 * Must have been caught in the parse phase.
528 */
529 fr_assert(tmpl_is_regex(regex->vpt));
530
531 inst->regex = tmpl_regex(regex->vpt);
532
533 return 0;
534}
535
536
538 .name = "regex",
539 .chr = '\\',
540 .subs = {
541 ['$'] = '$',
542 ['('] = '(',
543 ['*'] = '*',
544 ['+'] = '+',
545 ['.'] = '.',
546 ['/'] = '/',
547 ['?'] = '?',
548 ['['] = '[',
549 ['\\'] = '\\',
550 ['^'] = '^',
551 ['`'] = '`',
552 ['|'] = '|',
553 ['\a'] = 'a',
554 ['\b'] = 'b',
555 ['\n'] = 'n',
556 ['\r'] = 'r',
557 ['\t'] = 't',
558 ['\v'] = 'v'
559 },
560 .esc = {
563 },
564 .do_utf8 = true,
565 .do_oct = true
566};
567
569 { .required = true, .type = FR_TYPE_STRING },
570 { .concat = true, .type = FR_TYPE_STRING },
572};
573
574
575/** Perform a regular expressions comparison between two operands
576 *
577 * @param[in] ctx to allocate resulting box in.
578 * @param[in] request The current request.
579 * @param[in] in list of item or items
580 * @param[in,out] preg Pointer to pre-compiled or runtime-compiled
581 * regular expression. In the case of runtime-compiled
582 * the pattern may be stolen by the `regex_sub_to_request`
583 * function as the original pattern is needed to resolve
584 * capture groups.
585 * The caller should only free the `regex_t *` if it
586 * compiled it, and the pointer has not been set to NULL
587 * when this function returns.
588 * @param[out] out Where result is written.
589 * @param[in] op the operation to perform.
590 * @return
591 * - -1 on failure.
592 * - 0 for "no match".
593 * - 1 for "match".
594 */
595static xlat_action_t xlat_regex_match(TALLOC_CTX *ctx, request_t *request, fr_value_box_list_t *in, regex_t **preg,
597{
598 uint32_t subcaptures;
599 int ret = 0;
600
601 fr_regmatch_t *regmatch;
602 fr_value_box_t *dst;
603 fr_value_box_t *arg, *vb;
604 fr_sbuff_t *agg;
605 char const *subject;
606 size_t len;
607
608 FR_SBUFF_TALLOC_THREAD_LOCAL(&agg, 256, 8192);
609
610 arg = fr_value_box_list_head(in);
611 fr_assert(arg != NULL);
612 fr_assert(arg->type == FR_TYPE_GROUP);
613
614 subcaptures = regex_subcapture_count(*preg);
615 if (!subcaptures) subcaptures = REQUEST_MAX_REGEX + 1; /* +1 for %{0} (whole match) capture group */
616 MEM(regmatch = regex_match_data_alloc(NULL, subcaptures));
617
618 while ((vb = fr_value_box_list_pop_head(&arg->vb_group)) != NULL) {
619 if (vb->type == FR_TYPE_STRING) {
620 subject = vb->vb_strvalue;
621 len = vb->vb_length;
622
623 } else {
624 fr_value_box_list_t list;
625
626 fr_value_box_list_init(&list);
627 fr_value_box_list_insert_head(&list, vb);
628 vb = NULL;
629
630 /*
631 * Concatenate everything, and escape untrusted inputs.
632 */
633 if (fr_value_box_list_concat_as_string(NULL, NULL, agg, &list, NULL, 0, &regex_escape_rules,
634 FR_VALUE_BOX_LIST_FREE_BOX, FR_REGEX_SAFE_FOR, true) < 0) {
635 RPEDEBUG("Failed concatenating regular expression string");
636 talloc_free(regmatch);
637 return XLAT_ACTION_FAIL;
638 }
639
640 subject = fr_sbuff_start(agg);
641 len = fr_sbuff_used(agg);
642 }
643
644 /*
645 * Evaluate the expression
646 */
647 ret = regex_exec(*preg, subject, len, regmatch);
648 switch (ret) {
649 default:
650 RPEDEBUG("REGEX failed");
651 talloc_free(vb);
652 talloc_free(regmatch);
653 return XLAT_ACTION_FAIL;
654
655 case 0:
656 regex_sub_to_request(request, NULL, NULL); /* clear out old entries */
657 continue;
658
659 case 1:
660 regex_sub_to_request(request, preg, &regmatch);
661 talloc_free(vb);
662 goto done;
663
664 }
665
666 talloc_free(vb);
667 }
668
669done:
670 talloc_free(regmatch); /* free if not consumed */
671
673 dst->vb_bool = (ret == (op == T_OP_REG_EQ));
674
676
677 return XLAT_ACTION_DONE;
678}
679
681 xlat_ctx_t const *xctx,
682 request_t *request, fr_value_box_list_t *in)
683{
685 xlat_regex_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, xlat_regex_rctx_t);
686 ssize_t slen;
687 regex_t *preg = NULL;
688 fr_sbuff_t *agg;
689
690 FR_SBUFF_TALLOC_THREAD_LOCAL(&agg, 256, 8192);
691
692 /*
693 * If the expansions fails, then we fail the entire thing.
694 */
695 if (!rctx->last_success) {
696 talloc_free(rctx);
697 return XLAT_ACTION_FAIL;
698 }
699
700 /*
701 * Because we expanded the RHS ourselves, the "concat"
702 * flag to the RHS argument is ignored. So we just
703 * concatenate it here. We escape the various untrusted inputs.
704 */
705 if (fr_value_box_list_concat_as_string(NULL, NULL, agg, &rctx->list, NULL, 0, &regex_escape_rules,
706 FR_VALUE_BOX_LIST_FREE_BOX, FR_REGEX_SAFE_FOR, true) < 0) {
707 RPEDEBUG("Failed concatenating regular expression string");
708 return XLAT_ACTION_FAIL;
709 }
710
711 fr_assert(inst->regex == NULL);
712
713 slen = regex_compile(rctx, &preg, fr_sbuff_start(agg), fr_sbuff_used(agg),
714 tmpl_regex_flags(inst->xlat->vpt), true, true); /* flags, allow subcaptures, at runtime */
715 if (slen <= 0) return XLAT_ACTION_FAIL;
716
717 return xlat_regex_match(ctx, request, in, &preg, out, inst->op);
718}
719
721 xlat_ctx_t const *xctx,
722 request_t *request, fr_value_box_list_t *in,
723 fr_token_t op)
724{
726 xlat_regex_rctx_t *rctx;
727 regex_t *preg;
728
729 /*
730 * Just run precompiled regexes.
731 */
732 if (inst->regex) {
733 preg = tmpl_regex(inst->xlat->vpt);
734
735 return xlat_regex_match(ctx, request, in, &preg, out, op);
736 }
737
738 MEM(rctx = talloc_zero(unlang_interpret_frame_talloc_ctx(request), xlat_regex_rctx_t));
739 fr_value_box_list_init(&rctx->list);
740
741 if (unlang_xlat_yield(request, xlat_regex_resume, NULL, 0, rctx) != XLAT_ACTION_YIELD) {
742 fail:
743 talloc_free(rctx);
744 return XLAT_ACTION_FAIL;
745 }
746
747 if (unlang_xlat_push(ctx, &rctx->last_success, &rctx->list,
748 request, tmpl_xlat(inst->xlat->vpt), UNLANG_SUB_FRAME) < 0) goto fail;
749
751}
752
753#define XLAT_REGEX_FUNC(_name, _op) \
754static xlat_action_t xlat_func_ ## _name(TALLOC_CTX *ctx, fr_dcursor_t *out, \
755 xlat_ctx_t const *xctx, \
756 request_t *request, fr_value_box_list_t *in) \
757{ \
758 return xlat_regex_op(ctx, out, xctx, request, in, _op); \
759}
760
763
770
771typedef struct {
772 TALLOC_CTX *ctx;
774 fr_value_box_t *box; //!< output value-box
776 fr_value_box_list_t list;
778
779static fr_slen_t xlat_expr_print_nary(fr_sbuff_t *out, xlat_exp_t const *node, void *instance, fr_sbuff_escape_rules_t const *e_rules)
780{
781 size_t at_in = fr_sbuff_used_total(out);
782 xlat_logical_inst_t *inst = instance;
784
786
787 /*
788 * We might get called before the node is instantiated.
789 */
790 if (!inst->argv) {
791 head = node->call.args;
792
793 fr_assert(head != NULL);
794
795 xlat_exp_foreach(head, child) {
796 xlat_print_node(out, head, child, e_rules, 0);
797
798 if (!xlat_exp_next(head, child)) break;
799
800 FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]);
802 }
803 } else {
804 int i;
805
806 for (i = 0; i < inst->argc; i++) {
807 xlat_print(out, inst->argv[i], e_rules);
808 if (i == (inst->argc - 1)) break;
809
811 FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]);
812 if ((i + 1) < inst->argc) FR_SBUFF_IN_CHAR_RETURN(out, ' ');
813 }
814 }
815
817
818 return fr_sbuff_used_total(out) - at_in;
819}
820
821/*
822 * This returns "false" for "ignore this argument"
823 *
824 * result is "false" for "delete this argument"
825 * result is "true" for "return this argument".
826 */
827static bool xlat_node_matches_bool(bool *result, xlat_exp_t *parent, xlat_exp_head_t *head, bool sense)
828{
829 fr_value_box_t *box;
830 xlat_exp_t *node;
831
832 if (!head->flags.pure) return false;
833
834 node = xlat_exp_head(head);
835 if (!node || xlat_exp_next(head, node)) {
836 return false;
837 }
838
839 if (node->type == XLAT_BOX) {
840 box = &node->data;
841 goto check;
842 }
843
844 if (node->type != XLAT_TMPL) {
845 return false;
846 }
847
848 if (!tmpl_is_data(node->vpt)) {
849 return false;
850 }
851
852 box = tmpl_value(node->vpt);
853
854check:
855 /*
856 * On "true", replace the entire logical operation with the value-box.
857 *
858 * On "false", omit this argument, and go to the next one.
859 */
860 *result = (fr_value_box_is_truthy(box) == sense);
861
862 if (!*result) return true;
863
865
867 fr_value_box_copy(parent, &parent->data, box);
868 parent->flags = (xlat_flags_t) { .pure = true, .constant = true, };
869
870 talloc_free_children(parent);
871
872 return true;
873}
874
875/** Undo work which shouldn't have been done. :(
876 *
877 */
879{
880 xlat_exp_t *group, *node;
881
882 group = xlat_exp_head(head);
883 if (!group || xlat_exp_next(head, group)) return;
884
885 if (group->type != XLAT_GROUP) return;
886
887 node = xlat_exp_head(group->group);
888 if (!node || xlat_exp_next(group->group, node)) return;
889
890 (void) fr_dlist_remove(&head->dlist, group);
891 (void) fr_dlist_remove(&group->group->dlist, node);
892 (void) talloc_steal(head, node);
893
894 talloc_free(group);
895
896 fr_dlist_insert_tail(&head->dlist, node);
897 head->flags = node->flags;
898}
899
900/** If any argument resolves to inst->stop_on_match, the entire thing is a bool of inst->stop_on_match.
901 *
902 * If any argument resolves to !inst->stop_on_match, it is removed.
903 */
904static int xlat_expr_logical_purify(xlat_exp_t *node, void *instance, request_t *request)
905{
906 int i, j;
907 int deleted = 0;
908 bool result;
909 xlat_logical_inst_t *inst = talloc_get_type_abort(instance, xlat_logical_inst_t);
910 xlat_exp_head_t *group;
911
912 fr_assert(node->type == XLAT_FUNC);
913
914 /*
915 * Don't check the last argument. If everything else gets deleted,
916 * then we just return the last argument.
917 */
918 for (i = 0; i < inst->argc; i++) {
919 /*
920 * The argument is pure, so we purify it before
921 * doing any other checks.
922 */
923 if (inst->argv[i]->flags.can_purify) {
924 if (xlat_purify_list(inst->argv[i], request) < 0) return -1;
925
926 /*
927 * xlat_purify_list expects that its outputs will be arguments to functions, so
928 * they're grouped. We con't need that, so we ungroup them here.
929 */
930 xlat_ungroup(inst->argv[i]);
931 }
932
933 /*
934 * This returns "false" for "ignore".
935 *
936 * result is "false" for "delete this argument"
937 * result is "true" for "return this argument".
938 */
939 if (!xlat_node_matches_bool(&result, node, inst->argv[i], inst->stop_on_match)) continue;
940
941 /*
942 * 0 && EXPR --> 0.
943 * 1 || EXPR --> 1
944 *
945 * Parent is now an XLAT_BOX, so we're done.
946 */
947 if (result) return 0;
948
949 /*
950 * We're at the last argument. If we've deleted everything else, then just leave the
951 * last argument alone. Otherwise some arguments remain, so we can delete the last one.
952 */
953 if (((i + 1) == inst->argc) && (deleted == i)) break;
954
955 TALLOC_FREE(inst->argv[i]);
956 deleted++;
957 }
958
959 if (!deleted) return 0;
960
961 /*
962 * Pack the array. We insert at i, and read from j. We don't need to read the deleted entries,
963 * as they all MUST be NULL.
964 */
965 i = 0;
966 j = -1;
967 while (i < (inst->argc - deleted)) {
968 if (inst->argv[i]) {
969 i++;
970 continue;
971 }
972
973 /*
974 * Start searching from the next entry, OR start searching from where we left off before.
975 */
976 if (j < 0) j = i + 1;
977
978 /*
979 * Find the first non-NULL entry, and insert it in argv[i]. We search here until the end
980 * of the array, because we may have deleted entries from the start of the array.
981 */
982 while (j < inst->argc) {
983 if (inst->argv[j]) break;
984 j++;
985 }
986
987 /*
988 * Move the entry down, and clear out the tail end of the array.
989 */
990 inst->argv[i++] = inst->argv[j];
991 inst->argv[j++] = NULL;
992 }
993
994 inst->argc -= deleted;
995
996 if (inst->argc > 1) return 0;
997
998 /*
999 * Only one argument left/ We can hoist the child into ourselves, and omit the logical operation.
1000 */
1001 group = inst->argv[0];
1002 fr_assert(group != NULL);
1003 talloc_steal(node, group);
1004
1007
1008 /* re-print, with purified nodes removed */
1009 {
1010 char *name;
1011
1012 MEM(xlat_aprint(node, &name, group, NULL) >= 0);
1014 }
1015
1016 node->group = group;
1017 node->flags = group->flags;
1018
1019 return 0;
1020}
1021
1022/** Process one argument of a logical operation.
1023 *
1024 * If we see a list in a truthy context, then we DON'T expand the list. Instead, we return a bool which
1025 * indicates if the list was empty (or not). This prevents us from returning a whole mess of value-boxes
1026 * when the user just wanted to see if the list existed.
1027 *
1028 * Otherwise, we expand the xlat, and continue.
1029 */
1031 xlat_ctx_t const *xctx,
1032 request_t *request, UNUSED fr_value_box_list_t *in)
1033{
1035 xlat_logical_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, xlat_logical_rctx_t);
1036
1037 /*
1038 * Push the xlat onto the stack for expansion.
1039 */
1040 if (unlang_xlat_yield(request, inst->callback, NULL, 0, rctx) != XLAT_ACTION_YIELD) {
1041 fail:
1042 talloc_free(rctx->box);
1043 talloc_free(rctx);
1044 return XLAT_ACTION_FAIL;
1045 }
1046
1047 if (unlang_xlat_push(rctx, &rctx->last_success, &rctx->list,
1048 request, inst->argv[rctx->current], UNLANG_SUB_FRAME) < 0) goto fail;
1049
1051}
1052
1053/** See if the input is truthy or not.
1054 *
1055 * @param[in] rctx our ctx
1056 * @param[in] in list of value-boxes to check
1057 * @return
1058 * - false if there are no truthy values. The last box is copied to the rctx.
1059 * This is to allow us to return default values which may not be truthy,
1060 * e.g. %{&Counter || 0} or %{&Framed-IP-Address || 0.0.0.0}.
1061 * If we don't copy the last box to the rctx, the expression just returns NULL
1062 * which is never useful...
1063 * - true if we find a truthy value. The first truthy box is copied to the rctx.
1064 *
1065 * Empty lists are not truthy.
1066 */
1067static bool xlat_logical_or(xlat_logical_rctx_t *rctx, fr_value_box_list_t const *in)
1068{
1069 fr_value_box_t *last = NULL;
1070 bool ret = false;
1071
1072 /*
1073 * Empty lists are !truthy.
1074 */
1075 if (!fr_value_box_list_num_elements(in)) return false;
1076
1077 /*
1078 * Loop over the input list. If the box is a group, then do this recursively.
1079 */
1081 if (fr_box_is_group(box)) {
1082 if (!xlat_logical_or(rctx, &box->vb_group)) return false;
1083 continue;
1084 }
1085
1086 last = box;
1087
1088 /*
1089 * Remember the last box we found.
1090 *
1091 * If it's truthy, then we stop immediately.
1092 */
1093 if (fr_value_box_is_truthy(box)) {
1094 ret = true;
1095 break;
1096 }
1097 }
1098
1099 if (!rctx->box) {
1100 MEM(rctx->box = fr_value_box_alloc_null(rctx->ctx));
1101 } else {
1102 fr_value_box_clear(rctx->box);
1103 }
1104 if (last) fr_value_box_copy(rctx->box, rctx->box, last);
1105
1106 return ret;
1107}
1108
1109/*
1110 * We've evaluated an expression. Let's see if we need to continue with ||
1111 */
1113 xlat_ctx_t const *xctx,
1114 request_t *request, fr_value_box_list_t *in)
1115{
1117 xlat_logical_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, xlat_logical_rctx_t);
1118 bool match;
1119
1120 /*
1121 * If one of the expansions fails, then we fail the
1122 * entire thing.
1123 */
1124 if (!rctx->last_success) {
1125 talloc_free(rctx->box);
1126 talloc_free(rctx);
1127 return XLAT_ACTION_FAIL;
1128 }
1129
1130 /*
1131 * Recursively check groups. i.e. we effectively flatten each list.
1132 *
1133 * (a, b, c) || (d, e, f) == a || b || c || d || e || f
1134 */
1135 match = xlat_logical_or(rctx, &rctx->list);
1136 if (match) goto done;
1137
1138 fr_value_box_list_talloc_free(&rctx->list);
1139
1140 rctx->current++;
1141
1142 /*
1143 * Nothing to expand, return the final value we saw.
1144 */
1145 if (rctx->current >= inst->argc) {
1146 done:
1147 /*
1148 * Otherwise we stop on failure, with the boolean
1149 * we just updated.
1150 */
1151 if (rctx->box) fr_dcursor_append(out, rctx->box);
1152
1153 talloc_free(rctx);
1154 return XLAT_ACTION_DONE;
1155 }
1156
1157 return xlat_logical_process_arg(ctx, out, xctx, request, in);
1158}
1159
1160/** See if the input is truthy or not.
1161 *
1162 * @param[in] rctx our ctx
1163 * @param[in] in list of value-boxes to check
1164 * @return
1165 * - false on failure
1166 * - true for match, with dst updated to contain the relevant box.
1167 *
1168 * Empty lists are not truthy.
1169 */
1170static bool xlat_logical_and(xlat_logical_rctx_t *rctx, fr_value_box_list_t const *in)
1171{
1172 fr_value_box_t *found = NULL;
1173
1174 /*
1175 * Empty lists are !truthy.
1176 */
1177 if (!fr_value_box_list_num_elements(in)) return false;
1178
1179 /*
1180 * Loop over the input list. If the box is a group, then do this recursively.
1181 */
1183 if (fr_box_is_group(box)) {
1184 if (!xlat_logical_and(rctx, &box->vb_group)) return false;
1185 continue;
1186 }
1187
1188 /*
1189 * Remember the last box we found.
1190 *
1191 * If it's truthy, then we keep going either
1192 * until the end, or until we get a "false".
1193 */
1194 if (fr_value_box_is_truthy(box)) {
1195 found = box;
1196 continue;
1197 }
1198
1199 /*
1200 * Stop on the first "false"
1201 */
1202 return false;
1203 }
1204
1205 if (!found) return false;
1206
1207 if (!rctx->box) {
1208 MEM(rctx->box = fr_value_box_alloc_null(rctx));
1209 } else {
1210 fr_value_box_clear(rctx->box);
1211 }
1212 fr_value_box_copy(rctx->box, rctx->box, found);
1213
1214 return true;
1215}
1216
1217/*
1218 * We've evaluated an expression. Let's see if we need to continue with &&
1219 */
1221 xlat_ctx_t const *xctx,
1222 request_t *request, fr_value_box_list_t *in)
1223{
1225 xlat_logical_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, xlat_logical_rctx_t);
1226 bool match;
1227
1228 /*
1229 * If one of the expansions fails, then we fail the
1230 * entire thing.
1231 */
1232 if (!rctx->last_success) {
1233 talloc_free(rctx->box);
1234 talloc_free(rctx);
1235 return XLAT_ACTION_FAIL;
1236 }
1237
1238 /*
1239 * Recursively check groups. i.e. we effectively flatten each list.
1240 *
1241 * (a, b, c) && (d, e, f) == a && b && c && d && e && f
1242 */
1243 match = xlat_logical_and(rctx, &rctx->list);
1244 if (!match) return XLAT_ACTION_DONE;
1245
1246 fr_value_box_list_talloc_free(&rctx->list);
1247
1248 rctx->current++;
1249
1250 /*
1251 * Nothing to expand, return the final value we saw.
1252 */
1253 if (rctx->current >= inst->argc) {
1254 /*
1255 * Otherwise we stop on failure, with the boolean
1256 * we just updated.
1257 */
1258 fr_assert(rctx->box != NULL);
1259 fr_dcursor_append(out, rctx->box);
1260
1261 talloc_free(rctx);
1262 return XLAT_ACTION_DONE;
1263 }
1264
1265 return xlat_logical_process_arg(ctx, out, xctx, request, in);
1266}
1267
1268/*
1269 * Each argument is it's own head, because we do NOT always want
1270 * to go to the next argument.
1271 */
1273{
1274 xlat_logical_inst_t *inst = talloc_get_type_abort(xctx->inst, xlat_logical_inst_t);
1275
1276 inst->argc = xlat_flatten_compiled_argv(inst, &inst->argv, xctx->ex->call.args);
1277 if (xctx->ex->call.func->token == T_LOR) {
1278 inst->callback = xlat_logical_or_resume;
1279 inst->stop_on_match = true;
1280 } else {
1281 inst->callback = xlat_logical_and_resume;
1282 inst->stop_on_match = false;
1283 }
1284
1285 return 0;
1286}
1287
1288
1289/** Process logical &&, ||
1290 *
1291 */
1293 xlat_ctx_t const *xctx,
1294 request_t *request, fr_value_box_list_t *in)
1295{
1296 xlat_logical_rctx_t *rctx;
1298
1299 MEM(rctx = talloc_zero(unlang_interpret_frame_talloc_ctx(request), xlat_logical_rctx_t));
1300 rctx->ctx = ctx;
1301 rctx->current = 0;
1302
1303 if (inst->stop_on_match) {
1304 rctx->box = NULL;
1305 } else {
1307 rctx->box->vb_bool = true;
1308 }
1309 fr_value_box_list_init(&rctx->list);
1310
1311 (UNCONST(xlat_ctx_t *, xctx))->rctx = rctx; /* ensure it's there before a resume! */
1312
1313 return xlat_logical_process_arg(ctx, out, xctx, request, in);
1314}
1315
1316
1318 { .required = true, .single = true, .concat = true },
1320};
1321
1323 UNUSED xlat_ctx_t const *xctx,
1324 request_t *request, fr_value_box_list_t *in, fr_token_t op)
1325{
1326 int rcode;
1327 fr_value_box_t *dst, *group, *vb;
1328
1329 /*
1330 * We do some basic type checks here.
1331 */
1332 group = fr_value_box_list_head(in);
1333 vb = fr_value_box_list_head(&group->vb_group);
1334
1335 /*
1336 * -NULL is an error
1337 * ~NULL is an error
1338 * !NULL is handled by xlat_func_unary_not
1339 */
1340 if (!vb) {
1341 fr_strerror_printf("Input is empty");
1342 return XLAT_ACTION_FAIL;
1343 }
1344
1345 if (!fr_type_is_leaf(vb->type) || fr_type_is_variable_size(vb->type)) {
1346 REDEBUG("Cannot perform operation on data type %s", fr_type_to_str(vb->type));
1347 return XLAT_ACTION_FAIL;
1348 }
1349
1350 MEM(dst = fr_value_box_alloc_null(ctx));
1351
1352 /*
1353 * We rely on this function to do the remainder of the type checking.
1354 */
1355 rcode = fr_value_calc_unary_op(dst, dst, op, vb);
1356 if ((rcode < 0) || fr_type_is_null(dst->type)) {
1357 talloc_free(dst);
1358 return XLAT_ACTION_FAIL;
1359 }
1360
1361 fr_dcursor_append(out, dst);
1362 return XLAT_ACTION_DONE;
1363}
1364
1365
1367 UNUSED xlat_ctx_t const *xctx,
1368 UNUSED request_t *request, fr_value_box_list_t *in)
1369{
1370 fr_value_box_t *dst, *group, *vb;
1371
1372 group = fr_value_box_list_head(in);
1373 vb = fr_value_box_list_head(&group->vb_group);
1374
1375 /*
1376 * Don't call calc_unary_op(), because we want the enum names.
1377 */
1379
1380 /*
1381 * !NULL = true
1382 */
1383 if (!vb) {
1384 dst->vb_bool = true;
1385 } else {
1386 dst->vb_bool = !fr_value_box_is_truthy(vb);
1387 }
1388
1389 fr_dcursor_append(out, dst);
1390 return XLAT_ACTION_DONE;
1391}
1392
1394 xlat_ctx_t const *xctx,
1395 request_t *request, fr_value_box_list_t *in)
1396{
1397 return xlat_func_unary_op(ctx, out, xctx, request, in, T_SUB);
1398}
1399
1401 xlat_ctx_t const *xctx,
1402 request_t *request, fr_value_box_list_t *in)
1403{
1404 return xlat_func_unary_op(ctx, out, xctx, request, in, T_COMPLEMENT);
1405}
1406
1407/** Convert XLAT_BOX arguments to XLAT_TMPL
1408 *
1409 * xlat_tokenize() just makes all unknown arguments into XLAT_BOX, of data type FR_TYPE_STRING. Whereas
1410 * xlat_tokenize_expr() calls tmpl_afrom_substr(), which tries hard to create a particular data type.
1411 *
1412 * This function fixes up calls of the form %op_add(3, 4), which normally passes 2 arguments of "3" and "4",
1413 * so that the arguments are instead passed as integers 3 and 4.
1414 *
1415 * This fixup isn't *strictly* necessary, but it's good to have no surprises in the code, if the user creates
1416 * an expression manually.
1417 */
1419{
1420 xlat_exp_foreach(xctx->ex->call.args, arg) {
1421 ssize_t slen;
1422 xlat_exp_t *node;
1423 tmpl_t *vpt;
1424
1425 fr_assert(arg->type == XLAT_GROUP);
1426
1427 node = xlat_exp_head(arg->group);
1428 if (!node) continue;
1429 if (node->type != XLAT_BOX) continue;
1430 if (node->data.type != FR_TYPE_STRING) continue;
1431
1432 /*
1433 * Try to parse it. If we can't, leave it for a run-time error.
1434 */
1435 slen = tmpl_afrom_substr(node, &vpt, &FR_SBUFF_IN(node->data.vb_strvalue, node->data.vb_length),
1436 node->quote, NULL, NULL);
1437 if (slen <= 0) continue;
1438 if ((size_t) slen < node->data.vb_length) continue;
1439
1440 /*
1441 * Leave it as XLAT_BOX, but with the (guessed) new data type.
1442 */
1443 fr_value_box_clear(&node->data);
1444 fr_value_box_copy(node, &node->data, tmpl_value(vpt));
1446 }
1447
1448 return 0;
1449}
1450
1452 { .concat = true, .type = FR_TYPE_STRING },
1454};
1455
1456/** Holds the result of pre-parsing the rcode on startup
1457 */
1458typedef struct {
1459 rlm_rcode_t rcode; //!< The preparsed rcode.
1461
1462/** Convert static expr_rcode arguments into rcodes
1463 *
1464 * This saves doing the lookup at runtime, which given how frequently this xlat is used
1465 * could get quite expensive.
1466 */
1468{
1469 xlat_rcode_inst_t *inst = talloc_get_type_abort(xctx->inst, xlat_rcode_inst_t);
1470 xlat_exp_t *arg;
1471 xlat_exp_t *rcode_arg;
1472 fr_value_box_t *rcode;
1473
1474 /*
1475 * If it's literal data, then we can pre-resolve it to
1476 * a rcode now, and skip that at runtime.
1477 */
1478 arg = xlat_exp_head(xctx->ex->call.args);
1479 fr_assert(arg->type == XLAT_GROUP);
1480
1481 /*
1482 * We can only pre-parse if this if the value is
1483 * in a single box...
1484 */
1485 if (fr_dlist_num_elements(&arg->group->dlist) != 1) return 0;
1486 rcode_arg = xlat_exp_head(arg->group);
1487
1488 /*
1489 * We can only pre-parse is this is a static value.
1490 */
1491 if (rcode_arg->type != XLAT_BOX) return 0;
1492
1493 rcode = &rcode_arg->data;
1494
1495 switch (rcode->type) {
1496 case FR_TYPE_STRING:
1497 inst->rcode = fr_table_value_by_str(rcode_table, rcode->vb_strvalue, RLM_MODULE_NOT_SET);
1498 if (inst->rcode == RLM_MODULE_NOT_SET) {
1499 unknown:
1500 ERROR("Unknown rcode '%pV'", rcode);
1501 return -1;
1502 }
1503 break;
1504
1505 case FR_TYPE_INT8:
1506 case FR_TYPE_INT16:
1507 case FR_TYPE_INT32:
1508 case FR_TYPE_INT64:
1509 case FR_TYPE_UINT16:
1510 case FR_TYPE_UINT32:
1511 case FR_TYPE_UINT64:
1512 case FR_TYPE_SIZE:
1513 if (fr_value_box_cast_in_place(rcode_arg, rcode, FR_TYPE_UINT8, NULL) < 0) {
1514 invalid:
1515 ERROR("Invalid value for rcode '%pV'", rcode);
1516 return -1;
1517 }
1519
1520 case FR_TYPE_UINT8:
1521 if (rcode->vb_uint8 >= RLM_MODULE_NUMCODES) goto invalid;
1522 inst->rcode = rcode->vb_uint8;
1523 break;
1524
1525 default:
1526 goto unknown;
1527 }
1528
1529 /*
1530 * No point in creating useless boxes at runtime,
1531 * nuke the argument now.
1532 */
1533 (void) fr_dlist_remove(&xctx->ex->call.args->dlist, arg);
1534 talloc_free(arg);
1535
1536 return 0;
1537}
1538
1539static fr_slen_t xlat_expr_print_rcode(fr_sbuff_t *out, xlat_exp_t const *node, void *instance, UNUSED fr_sbuff_escape_rules_t const *e_rules)
1540{
1541 size_t at_in = fr_sbuff_used_total(out);
1542 xlat_rcode_inst_t *inst = instance;
1543
1544 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(out, "%expr.rcode('");
1545 if (xlat_exp_head(node->call.args)) {
1546 ssize_t slen;
1547
1548 xlat_exp_foreach(node->call.args, child) {
1549 slen = xlat_print_node(out, node->call.args, child, NULL, 0);
1550 if (slen < 0) return slen;
1551 }
1552 } else {
1554 }
1556
1557 return fr_sbuff_used_total(out) - at_in;
1558}
1559
1560/** Match the passed rcode against request->rcode
1561 *
1562 * Example:
1563@verbatim
1564%expr.rcode('handled') == true
1565
1566# ...or how it's used normally used
1567if (handled) {
1568 ...
1569}
1570@endverbatim
1571 *
1572 * @ingroup xlat_functions
1573 */
1575 xlat_ctx_t const *xctx,
1576 request_t *request, fr_value_box_list_t *args)
1577{
1579 fr_value_box_t *arg_rcode;
1580 rlm_rcode_t rcode;
1581 fr_value_box_t *vb;
1582
1583 /*
1584 * If we have zero args, it's because the instantiation
1585 * function consumed them. om nom nom.
1586 */
1587 if (fr_value_box_list_num_elements(args) == 0) {
1589 rcode = inst->rcode;
1590 } else {
1591 XLAT_ARGS(args, &arg_rcode);
1592 rcode = fr_table_value_by_str(rcode_table, arg_rcode->vb_strvalue, RLM_MODULE_NOT_SET);
1593 if (rcode == RLM_MODULE_NOT_SET) {
1594 REDEBUG("Invalid rcode '%pV'", arg_rcode);
1595 return XLAT_ACTION_FAIL;
1596 }
1597 }
1598
1599 RDEBUG3("Request rcode is '%s'",
1600 fr_table_str_by_value(rcode_table, request->rcode, "<INVALID>"));
1601
1604 vb->vb_bool = (request->rcode == rcode);
1605
1606 return XLAT_ACTION_DONE;
1607}
1608
1609/** Takes no arguments
1610 */
1612 XLAT_ARG_PARSER_TERMINATOR, /* Coverity gets tripped up by only having a single entry here */
1614};
1615
1616/** Return the current rcode as a string
1617 *
1618 * Example:
1619@verbatim
1620"%rcode()" == "handled"
1621@endverbatim
1622 *
1623 * @ingroup xlat_functions
1624 */
1626 UNUSED xlat_ctx_t const *xctx,
1627 request_t *request, UNUSED fr_value_box_list_t *args)
1628{
1629 fr_value_box_t *vb;
1630
1631 /*
1632 * FIXME - This should really be an enum
1633 */
1634 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL));
1635 if (fr_value_box_strdup(vb, vb, NULL, fr_table_str_by_value(rcode_table, request->rcode, "<INVALID>"), false) < 0) {
1636 talloc_free(vb);
1637 return XLAT_ACTION_FAIL;
1638 }
1640
1641 return XLAT_ACTION_DONE;
1642}
1643
1644typedef struct {
1645 tmpl_t const *vpt; //!< the attribute reference
1646 xlat_exp_head_t *xlat; //!< the xlat which needs expanding
1648
1649typedef struct {
1651 fr_value_box_list_t list;
1653
1655 { .concat = true, .type = FR_TYPE_STRING },
1657};
1658
1659/*
1660 * We just print the xlat as-is.
1661 */
1662static fr_slen_t xlat_expr_print_exists(fr_sbuff_t *out, xlat_exp_t const *node, void *instance, fr_sbuff_escape_rules_t const *e_rules)
1663{
1664 size_t at_in = fr_sbuff_used_total(out);
1665 xlat_exists_inst_t *inst = instance;
1666
1667 if (inst->xlat) {
1668 xlat_print(out, inst->xlat, e_rules);
1669 } else {
1670 xlat_print_node(out, node->call.args, xlat_exp_head(node->call.args), e_rules, 0);
1671 }
1672
1673 return fr_sbuff_used_total(out) - at_in;
1674}
1675
1676/*
1677 * Don't expand the argument if it's already an attribute reference.
1678 */
1680{
1681 xlat_exists_inst_t *inst = talloc_get_type_abort(xctx->inst, xlat_exists_inst_t);
1682 xlat_exp_t *arg, *xlat;
1683
1684 arg = xlat_exp_head(xctx->ex->call.args);
1685 (void) fr_dlist_remove(&xctx->ex->call.args->dlist, arg);
1686
1687 fr_assert(arg->type == XLAT_GROUP);
1688 xlat = xlat_exp_head(arg->group);
1689
1690 inst->xlat = talloc_steal(inst, arg->group);
1691 talloc_free(arg);
1692
1693 /*
1694 * If it's an attribute, we can cache a reference to it.
1695 */
1696 if ((xlat->type == XLAT_TMPL) && (tmpl_contains_attr(xlat->vpt))) {
1697 inst->vpt = xlat->vpt;
1698 }
1699
1700 return 0;
1701}
1702
1704 request_t *request, tmpl_t const *vpt, bool do_free)
1705{
1706 fr_pair_t *vp;
1707 fr_value_box_t *dst;
1708 fr_dcursor_t cursor;
1710
1712
1713 vp = tmpl_dcursor_init(NULL, NULL, &cc, &cursor, request, vpt);
1714 dst->vb_bool = (vp != NULL);
1715
1716 if (do_free) talloc_const_free(vpt);
1717 tmpl_dcursor_clear(&cc);
1718 fr_dcursor_append(out, dst);
1719 return XLAT_ACTION_DONE;
1720}
1721
1723 xlat_ctx_t const *xctx,
1724 request_t *request, UNUSED fr_value_box_list_t *in)
1725{
1726 xlat_exists_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, xlat_exists_rctx_t);
1727 ssize_t slen;
1728 tmpl_t *vpt;
1729 fr_value_box_t *vb;
1730 fr_sbuff_t *agg;
1731
1732 FR_SBUFF_TALLOC_THREAD_LOCAL(&agg, 256, 8192);
1733
1734 /*
1735 * If the expansions fails, then we fail the entire thing.
1736 */
1737 if (!rctx->last_success) {
1738 fail:
1739 talloc_free(rctx);
1740 return XLAT_ACTION_FAIL;
1741 }
1742
1743 /*
1744 * Because we expanded the RHS ourselves, the "concat"
1745 * flag to the RHS argument is ignored. So we just
1746 * concatenate it here. We escape the various untrusted inputs.
1747 */
1748 if (fr_value_box_list_concat_as_string(NULL, NULL, agg, &rctx->list, NULL, 0, NULL,
1749 FR_VALUE_BOX_LIST_FREE_BOX, 0, true) < 0) {
1750 RPEDEBUG("Failed concatenating attribute name string");
1751 return XLAT_ACTION_FAIL;
1752 }
1753
1754 vb = fr_value_box_list_head(&rctx->list);
1755
1756 slen = tmpl_afrom_attr_str(ctx, NULL, &vpt, vb->vb_strvalue,
1757 &(tmpl_rules_t) {
1758 .attr = {
1759 .dict_def = request->dict,
1760 .request_def = &tmpl_request_def_current,
1761 .list_def = request_attr_request,
1762 .prefix = TMPL_ATTR_REF_PREFIX_AUTO,
1763 .allow_unknown = false,
1764 .allow_unresolved = false,
1765 },
1766 });
1767 if (slen <= 0) goto fail;
1768
1769 talloc_free(rctx); /* no longer needed */
1770 return xlat_attr_exists(ctx, out, request, vpt, true);
1771}
1772
1773/** See if a named attribute exists
1774 *
1775 * Example:
1776@verbatim
1777"%{exists:&Foo}" == true
1778@endverbatim
1779 *
1780 * @ingroup xlat_functions
1781 */
1783 xlat_ctx_t const *xctx,
1784 request_t *request, UNUSED fr_value_box_list_t *in)
1785{
1787 xlat_exists_rctx_t *rctx;
1788
1789 /*
1790 * We return "true" if the attribute exists. Otherwise we return "false".
1791 *
1792 * Except for virtual attributes. If we're testing for
1793 * their existence, we always return "true".
1794 */
1795 if (inst->vpt) {
1796 return xlat_attr_exists(ctx, out, request, inst->vpt, false);
1797 }
1798
1799 /*
1800 * Expand the xlat into a string.
1801 */
1802 MEM(rctx = talloc_zero(unlang_interpret_frame_talloc_ctx(request), xlat_exists_rctx_t));
1803 fr_value_box_list_init(&rctx->list);
1804
1805 if (unlang_xlat_yield(request, xlat_exists_resume, NULL, 0, rctx) != XLAT_ACTION_YIELD) {
1806 fail:
1807 talloc_free(rctx);
1808 return XLAT_ACTION_FAIL;
1809 }
1810
1811 if (unlang_xlat_push(ctx, &rctx->last_success, &rctx->list,
1812 request, inst->xlat, UNLANG_SUB_FRAME) < 0) goto fail;
1813
1815}
1816
1817#undef XLAT_REGISTER_BINARY_OP
1818#define XLAT_REGISTER_BINARY_OP(_op, _name) \
1819do { \
1820 if (unlikely((xlat = xlat_func_register(NULL, "op_" STRINGIFY(_name), xlat_func_op_ ## _name, FR_TYPE_VOID)) == NULL)) return -1; \
1821 xlat_func_args_set(xlat, binary_op_xlat_args); \
1822 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); \
1823 xlat_func_print_set(xlat, xlat_expr_print_binary); \
1824 xlat_func_instantiate_set(xlat, xlat_function_args_to_tmpl, NULL, NULL, NULL); \
1825 xlat->token = _op; \
1826} while (0)
1827
1828#undef XLAT_REGISTER_BINARY_CMP
1829#define XLAT_REGISTER_BINARY_CMP(_op, _name) \
1830do { \
1831 if (unlikely((xlat = xlat_func_register(NULL, "cmp_" STRINGIFY(_name), xlat_func_cmp_ ## _name, FR_TYPE_BOOL)) == NULL)) return -1; \
1832 xlat_func_args_set(xlat, binary_cmp_xlat_args); \
1833 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); \
1834 xlat_func_print_set(xlat, xlat_expr_print_binary); \
1835 xlat_func_resolve_set(xlat, xlat_expr_resolve_binary); \
1836 xlat->token = _op; \
1837} while (0)
1838
1839#undef XLAT_REGISTER_NARY_OP
1840#define XLAT_REGISTER_NARY_OP(_op, _name, _func_name) \
1841do { \
1842 if (unlikely((xlat = xlat_func_register(NULL, STRINGIFY(_name), xlat_func_ ## _func_name, FR_TYPE_VOID)) == NULL)) return -1; \
1843 xlat_func_instantiate_set(xlat, xlat_instantiate_ ## _func_name, xlat_ ## _func_name ## _inst_t, NULL, NULL); \
1844 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); \
1845 xlat_func_print_set(xlat, xlat_expr_print_nary); \
1846 xlat_purify_func_set(xlat, xlat_expr_logical_purify); \
1847 xlat->token = _op; \
1848} while (0)
1849
1850#undef XLAT_REGISTER_REGEX_OP
1851#define XLAT_REGISTER_REGEX_OP(_op, _name) \
1852do { \
1853 if (unlikely((xlat = xlat_func_register(NULL, STRINGIFY(_name), xlat_func_ ## _name, FR_TYPE_VOID)) == NULL)) return -1; \
1854 xlat_func_args_set(xlat, regex_op_xlat_args); \
1855 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); \
1856 xlat_func_instantiate_set(xlat, xlat_instantiate_regex, xlat_regex_inst_t, NULL, NULL); \
1857 xlat_func_print_set(xlat, xlat_expr_print_regex); \
1858 xlat->token = _op; \
1859} while (0)
1860
1861#define XLAT_REGISTER_BOOL(_xlat, _func, _arg, _ret_type) \
1862do { \
1863 if (unlikely((xlat = xlat_func_register(NULL, _xlat, _func, _ret_type)) == NULL)) return -1; \
1864 xlat_func_args_set(xlat, _arg); \
1865 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_INTERNAL); \
1866} while (0)
1867
1868#define XLAT_REGISTER_UNARY(_op, _xlat, _func) \
1869do { \
1870 if (unlikely((xlat = xlat_func_register(NULL, _xlat, _func, FR_TYPE_VOID)) == NULL)) return -1; \
1871 xlat_func_args_set(xlat, unary_op_xlat_args); \
1872 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); \
1873 xlat_func_print_set(xlat, xlat_expr_print_unary); \
1874 xlat->token = _op; \
1875} while (0)
1876
1878{
1879 xlat_t *xlat;
1880
1891
1900
1903
1904 /*
1905 * &&, ||
1906 *
1907 * @todo - remove tmpl_resolve() from tokenize_field(), and add xlat_resolve_logical_or() / xlat_resolve_logical_and()
1908 * functions which do partial resolution.
1909 */
1910 XLAT_REGISTER_NARY_OP(T_LAND, logical_and, logical);
1911 XLAT_REGISTER_NARY_OP(T_LOR, logical_or, logical);
1912
1916
1920
1921 if (unlikely((xlat = xlat_func_register(NULL, "rcode", xlat_func_rcode, FR_TYPE_STRING)) == NULL)) return -1;
1924
1925 /*
1926 * -EXPR
1927 * ~EXPR
1928 * !EXPR
1929 */
1933
1934 return 0;
1935}
1936
1937/*
1938 * Must use the same names as above.
1939 */
1941 [ T_ADD ] = L("op_add"),
1942 [ T_SUB ] = L("op_sub"),
1943 [ T_MUL ] = L("op_mul"),
1944 [ T_DIV ] = L("op_div"),
1945 [ T_MOD ] = L("op_mod"),
1946 [ T_AND ] = L("op_and"),
1947 [ T_OR ] = L("op_or"),
1948 [ T_XOR ] = L("op_xor"),
1949 [ T_RSHIFT ] = L("op_rshift"),
1950 [ T_LSHIFT ] = L("op_lshift"),
1951
1952 [ T_LAND ] = L("logical_and"),
1953 [ T_LOR ] = L("logical_or"),
1954
1955 [ T_OP_CMP_EQ ] = L("cmp_eq"),
1956 [ T_OP_NE ] = L("cmp_ne"),
1957 [ T_OP_LT ] = L("cmp_lt"),
1958 [ T_OP_LE ] = L("cmp_le"),
1959 [ T_OP_GT ] = L("cmp_gt"),
1960 [ T_OP_GE ] = L("cmp_ge"),
1961
1962 [ T_OP_CMP_EQ_TYPE ] = L("cmp_eq_type"),
1963 [ T_OP_CMP_NE_TYPE ] = L("cmp_ne_type"),
1964
1965 [ T_OP_REG_EQ ] = L("reg_eq"),
1966 [ T_OP_REG_NE ] = L("reg_ne"),
1967};
1968
1969/*
1970 * Which are logical operations
1971 */
1972static const bool logical_ops[T_TOKEN_LAST] = {
1973 [T_LAND] = true,
1974 [T_LOR] = true,
1975};
1976
1977/*
1978 * These operators can take multiple arguments.
1979 */
1980static const bool multivalue_ops[T_TOKEN_LAST] = {
1981 [T_LAND] = true,
1982 [T_LOR] = true,
1983};
1984
1985/*
1986 * Allow for BEDMAS ordering. Gross ordering is first number,
1987 * fine ordering is second number. Unused operators are assigned as zero.
1988 *
1989 * Larger numbers are higher precedence.
1990 */
1991#define P(_x, _y) (((_x) << 4) | (_y))
1992
1993static const int precedence[T_TOKEN_LAST] = {
1994 [T_INVALID] = 0,
1995
1996 /*
1997 * Assignment operators go here as P(1,n)
1998 *
1999 * += -= *= /= %= <<= >>= &= ^= |=
2000 *
2001 * We want the output of the assignment operators to be the result of the assignment. This means
2002 * that the assignments can really only be done for simple attributes, and not tmpls with filters
2003 * which select multiple attributes.
2004 *
2005 * Which (for now) means that we likely want to disallow assignments in expressions. That's
2006 * fine, as this isn't C, and we're not sure that it makes sense to do something like:
2007 *
2008 * if ((&foo += 5) > 60) ...
2009 *
2010 * Or maybe it does. Who knows?
2011 */
2012
2013 [T_LOR] = P(2,0),
2014 [T_LAND] = P(2,1),
2015
2016 [T_OR] = P(3,0),
2017 [T_XOR] = P(3,1),
2018 [T_AND] = P(3,2),
2019
2020 [T_OP_REG_EQ] = P(4,0),
2021 [T_OP_REG_NE] = P(4,0),
2022
2023 [T_OP_CMP_EQ] = P(4,1),
2024 [T_OP_NE] = P(4,1),
2025
2026 [T_OP_CMP_EQ_TYPE] = P(4,1),
2027 [T_OP_CMP_NE_TYPE] = P(4,1),
2028
2029 [T_OP_LT] = P(5,0),
2030 [T_OP_LE] = P(5,0),
2031 [T_OP_GT] = P(5,0),
2032 [T_OP_GE] = P(5,0),
2033
2034 [T_RSHIFT] = P(6,0),
2035 [T_LSHIFT] = P(6,0),
2036
2037 [T_SUB] = P(7,0),
2038 [T_ADD] = P(7,1),
2039
2040 [T_MOD] = P(8,0),
2041 [T_MUL] = P(8,1),
2042 [T_DIV] = P(8,2),
2043
2044 [T_LBRACE] = P(10,0),
2045};
2046
2047#define fr_sbuff_skip_whitespace(_x) \
2048 do { \
2049 while (isspace((uint8_t) fr_sbuff_char(_x, '\0'))) fr_sbuff_advance(_x, 1); \
2050 } while (0)
2051
2053 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules,
2054 fr_token_t prev, fr_sbuff_parse_rules_t const *bracket_rules,
2055 fr_sbuff_parse_rules_t const *input_rules, bool cond);
2056
2058 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules,
2059 fr_sbuff_parse_rules_t const *bracket_rules, char *out_c, bool cond);
2060
2062 { L("\""), T_DOUBLE_QUOTED_STRING }, /* Don't re-order, backslash throws off ordering */
2063 { L("'"), T_SINGLE_QUOTED_STRING },
2064 { L("/"), T_SOLIDUS_QUOTED_STRING },
2065 { L("`"), T_BACK_QUOTED_STRING }
2066};
2068
2069
2070/*
2071 * Look for prefix operators
2072 *
2073 * + = ignore
2074 * - = unary_minus(next)
2075 * ! = unary_not(next)
2076 * ~ = unary_xor(0, next)
2077 * (expr) = recurse, and parse expr
2078 *
2079 * as a special case, <type> is a cast. Which lets us know how
2080 * to parse the next thing we get. Otherwise, parse the thing as
2081 * int64_t.
2082 */
2084 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules,
2085 fr_sbuff_parse_rules_t const *bracket_rules, char *out_c, bool cond)
2086{
2087 xlat_exp_t *node = NULL, *unary = NULL;
2088 xlat_t *func = NULL;
2089 fr_sbuff_t our_in = FR_SBUFF(in);
2090 char c = '\0';
2091
2093
2094 /*
2095 * Handle !-~ by adding a unary function to the xlat
2096 * node, with the first argument being the _next_ thing
2097 * we allocate.
2098 */
2099 if (fr_sbuff_next_if_char(&our_in, '!')) { /* unary not */
2100 func = xlat_func_find("unary_not", 9);
2101 fr_assert(func != NULL);
2102 c = '!';
2103 goto check_for_double;
2104
2105 }
2106 else if (fr_sbuff_next_if_char(&our_in, '-')) { /* unary minus */
2107 fr_sbuff_skip_whitespace(&our_in);
2108
2109 /*
2110 * -4 is a number, not minus(4).
2111 */
2112 if (fr_sbuff_is_digit(&our_in)) goto field;
2113
2114 func = xlat_func_find("unary_minus", 11);
2115 fr_assert(func != NULL);
2116 c = '-';
2117 goto check_for_double;
2118
2119 }
2120 else if (fr_sbuff_next_if_char(&our_in, '~')) { /* unary complement */
2121 func = xlat_func_find("unary_complement", 16);
2122 fr_assert(func != NULL);
2123 c = '~';
2124 goto check_for_double;
2125
2126 }
2127 else if (fr_sbuff_next_if_char(&our_in, '+')) { /* ignore unary + */
2128 c = '+';
2129
2130 check_for_double:
2131 fr_sbuff_skip_whitespace(&our_in);
2132 fr_sbuff_skip_whitespace(&our_in);
2133 if (fr_sbuff_is_char(&our_in, c)) {
2134 fr_strerror_const("Double operator is invalid");
2135 FR_SBUFF_ERROR_RETURN(&our_in);
2136 }
2137 }
2138
2139 /*
2140 * Maybe we have a unary not / etc. If so, make sure
2141 * that we return that, and not the child node
2142 */
2143 if (!func) {
2144 field:
2145 return tokenize_field(head, out, in, p_rules, t_rules, bracket_rules, out_c, cond);
2146 }
2147
2148 /*
2149 * Tokenize_field may reset this if the operation is wrapped inside of another expression.
2150 */
2151 *out_c = c;
2152
2153 MEM(unary = xlat_exp_alloc(head, XLAT_FUNC, fr_tokens[func->token], strlen(fr_tokens[func->token])));
2154 unary->call.func = func;
2155 unary->call.dict = t_rules->attr.dict_def;
2156 unary->flags = func->flags;
2157 unary->flags.impure_func = !func->flags.pure;
2158
2159 if (tokenize_field(unary->call.args, &node, &our_in, p_rules, t_rules, bracket_rules, out_c, (c == '!')) < 0) {
2160 talloc_free(unary);
2161 FR_SBUFF_ERROR_RETURN(&our_in);
2162 }
2163
2164 if (!node) {
2165 fr_strerror_const("Empty expression is invalid");
2166 FR_SBUFF_ERROR_RETURN(&our_in);
2167 }
2168
2169 xlat_func_append_arg(unary, node, (c == '!'));
2170 unary->flags.can_purify = (unary->call.func->flags.pure && unary->call.args->flags.pure) | unary->call.args->flags.can_purify;
2171
2172 /*
2173 * Don't add it to head->flags, that will be done when it's actually inserted.
2174 */
2175
2176 *out = unary;
2177
2178 FR_SBUFF_SET_RETURN(in, &our_in);
2179}
2180
2181/** Allocate a specific cast node.
2182 *
2183 * With the first argument being a UINT8 of the data type.
2184 * See xlat_func_cast() for the implementation.
2185 *
2186 */
2187static xlat_exp_t *expr_cast_alloc(TALLOC_CTX *ctx, fr_type_t type)
2188{
2189 xlat_exp_t *cast, *node;
2190
2191 /*
2192 * Create a "cast" node. The first argument is a UINT8 value-box of the cast type. The RHS is
2193 * whatever "node" comes next.
2194 */
2195 MEM(cast = xlat_exp_alloc(ctx, XLAT_FUNC, "cast", 4));
2196 MEM(cast->call.func = xlat_func_find("cast", 4));
2197 // no need to set dict here
2198 fr_assert(cast->call.func != NULL);
2199 cast->flags = cast->call.func->flags;
2200
2201 /*
2202 * Create argv[0] UINT8, with "Cast-Base" as
2203 * the "da". This allows the printing routines
2204 * to print the name of the type, and not the
2205 * number.
2206 */
2207 MEM(node = xlat_exp_alloc(cast, XLAT_BOX, NULL, 0));
2208 node->flags.constant = true;
2209 {
2210 char const *type_name = fr_table_str_by_value(fr_type_table, type, "<INVALID>");
2211 xlat_exp_set_name(node, type_name, strlen(type_name));
2212 }
2213
2214 fr_value_box_init(&node->data, FR_TYPE_UINT8, attr_cast_base, false);
2215 node->data.vb_uint8 = type;
2216
2217 xlat_func_append_arg(cast, node, false);
2218
2219 return cast;
2220}
2221
2223{
2224 fr_sbuff_t our_in = FR_SBUFF(in);
2226 ssize_t slen;
2227
2228 if (!fr_sbuff_next_if_char(&our_in, '(')) {
2229 no_cast:
2230 *cast = FR_TYPE_NULL;
2231 return 0;
2232 }
2233
2234 fr_sbuff_marker(&m, &our_in);
2236
2237 /*
2238 * We didn't read anything, there's no cast.
2239 */
2240 if (fr_sbuff_diff(&our_in, &m) == 0) goto no_cast;
2241
2242 if (!fr_sbuff_next_if_char(&our_in, ')')) goto no_cast;
2243
2244 if (fr_type_is_null(*cast)) {
2245 fr_strerror_printf("Invalid data type in cast");
2247 }
2248
2249 if (!fr_type_is_leaf(*cast)) {
2250 fr_strerror_printf("Invalid data type '%s' in cast", fr_type_to_str(*cast));
2251 FR_SBUFF_ERROR_RETURN(&our_in);
2252 }
2253
2254 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
2255
2256 FR_SBUFF_SET_RETURN(in, &our_in);
2257}
2258
2259/*
2260 * Tokenize the RHS of a regular expression.
2261 */
2263 tmpl_rules_t const *t_rules,
2264 fr_sbuff_parse_rules_t const *bracket_rules)
2265{
2266 ssize_t slen;
2267 xlat_exp_t *node = NULL;
2268 fr_sbuff_t our_in = FR_SBUFF(in);
2269 fr_sbuff_marker_t opand_m, flag;
2270 tmpl_t *vpt;
2272
2274
2275 fr_sbuff_skip_whitespace(&our_in);
2276
2277 /*
2278 * Record where the operand begins for better error offsets later
2279 */
2280 fr_sbuff_marker(&opand_m, &our_in);
2281
2282 /*
2283 * Regexes cannot have casts or sub-expressions.
2284 */
2285 if (!fr_sbuff_next_if_char(&our_in, '/')) {
2286 /*
2287 * Allow for m'...' ala Perl
2288 */
2289 if (!fr_sbuff_is_str(&our_in, "m'", 2)) {
2290 fr_strerror_const("Expected regular expression");
2291 goto error;
2292 }
2293
2294 fr_sbuff_advance(&our_in, 2);
2295 quote = T_SINGLE_QUOTED_STRING;
2296 }
2297
2298 /*
2299 * Allocate the xlat node now so the talloc hierarchy is correct
2300 */
2301 MEM(node = xlat_exp_alloc(head, XLAT_TMPL, NULL, 0));
2302
2303 /*
2304 * tmpl_afrom_substr does pretty much all the work of parsing the operand. Note that we pass '/'
2305 * as the quote, so that the tmpl gets parsed as a regex.
2306 */
2307 (void) tmpl_afrom_substr(node, &vpt, &our_in, T_SOLIDUS_QUOTED_STRING, value_parse_rules_quoted[quote], t_rules);
2308 if (!vpt) {
2309 error:
2310 talloc_free(node);
2311 FR_SBUFF_ERROR_RETURN(&our_in);
2312 }
2313
2314 /*
2315 * @todo - allow for the RHS to be an attribute, too?
2316 */
2317
2318 /*
2319 * It would be nice if tmpl_afrom_substr() did this :(
2320 */
2321 if (!fr_sbuff_next_if_char(&our_in, fr_token_quote[quote])) {
2322 fr_strerror_const("Unterminated regular expression");
2323 goto error;
2324 }
2325
2326 /*
2327 * Remember where the flags start
2328 */
2329 fr_sbuff_marker(&flag, &our_in);
2330 if (tmpl_regex_flags_substr(vpt, &our_in, bracket_rules->terminals) < 0) {
2331 talloc_free(node);
2332 FR_SBUFF_ERROR_RETURN(&our_in);
2333 }
2334
2335 fr_sbuff_skip_whitespace(&our_in);
2336
2337 /*
2338 * Try to compile regular expressions, but only if
2339 * they're not being dynamically expanded.
2340 */
2341 if (!tmpl_contains_xlat(vpt)) {
2342 slen = tmpl_regex_compile(vpt, true);
2343 if (slen <= 0) goto error;
2344 }
2345
2346 node->vpt = vpt;
2347 node->quote = quote;
2349
2350 node->flags.pure = !tmpl_contains_xlat(node->vpt);
2351 node->flags.needs_resolving = tmpl_needs_resolving(node->vpt);
2352
2353 *out = node;
2354
2355 FR_SBUFF_SET_RETURN(in, &our_in);
2356}
2357
2358
2360{
2361 rlm_rcode_t rcode;
2362 ssize_t slen;
2363 xlat_t *func;
2364 xlat_exp_t *node, *arg;
2365 fr_sbuff_t our_in = FR_SBUFF(in);
2366
2367 fr_sbuff_out_by_longest_prefix(&slen, &rcode, rcode_table, &our_in, T_BARE_WORD);
2368 if (slen <= 0) return 0;
2369
2370 /*
2371 * @todo - allow for attributes to have the name "ok-foo" ???
2372 */
2373 func = xlat_func_find("expr.rcode", -1);
2374 fr_assert(func != NULL);
2375
2376 MEM(node = xlat_exp_alloc(head, XLAT_FUNC, fr_sbuff_start(&our_in), slen));
2377 node->call.func = func;
2378 // no need to set dict here
2379 node->flags = func->flags; /* rcode is impure, but can be calculated statically */
2380
2381 MEM(arg = xlat_exp_alloc(node, XLAT_BOX, fr_sbuff_start(&our_in), slen));
2382
2383 /*
2384 * Doesn't need resolving, isn't pure, doesn't need anything else.
2385 */
2386 arg->flags = (xlat_flags_t) { };
2387
2388 /*
2389 * We need a string for unit tests, but this should really be just a number.
2390 */
2391 fr_value_box_init(&arg->data, FR_TYPE_STRING, NULL, false);
2392 (void) fr_value_box_bstrndup(arg, &arg->data, NULL, fr_sbuff_start(&our_in), slen, false);
2393
2394 xlat_func_append_arg(node, arg, false);
2395
2396 *out = node;
2397
2398 FR_SBUFF_SET_RETURN(in, &our_in);
2399}
2400
2401
2402/*
2403 * Tokenize a field without unary operators.
2404 */
2406 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules,
2407 fr_sbuff_parse_rules_t const *bracket_rules, char *out_c, bool cond)
2408{
2409 ssize_t slen;
2410 xlat_exp_t *node = NULL;
2411 fr_sbuff_t our_in = FR_SBUFF(in);
2412 fr_sbuff_marker_t opand_m;
2413 tmpl_rules_t our_t_rules = *t_rules;
2414 tmpl_t *vpt = NULL;
2415 fr_token_t quote;
2416 int triple = 1;
2417 fr_type_t cast_type = FR_TYPE_NULL;
2418 xlat_exp_t *cast = NULL;
2419
2421
2422 /*
2423 * Allow for explicit casts. Non-leaf types are forbidden.
2424 */
2425 if (expr_cast_from_substr(&cast_type, &our_in) < 0) return -1;
2426
2427 /*
2428 * If we still have '(', then recurse for other expressions
2429 *
2430 * Tokenize the sub-expression, ensuring that we stop at ')'.
2431 *
2432 * Note that if we have a sub-expression, then we don't use the hinting for "type".
2433 * That's because we're parsing a complete expression here (EXPR). So the intermediate
2434 * nodes in the expression can be almost anything. And we only cast it to the final
2435 * value when we get the output of the expression.
2436 */
2437 if (fr_sbuff_next_if_char(&our_in, '(')) {
2438 our_t_rules.cast = FR_TYPE_NULL;
2439 our_t_rules.enumv = NULL;
2440
2441 /*
2442 * No input rules means "ignore external terminal sequences, as we're expecting a ')' as
2443 * our terminal sequence.
2444 */
2445 if (tokenize_expression(head, &node, &our_in, bracket_rules, &our_t_rules, T_INVALID, bracket_rules, NULL, cond) < 0) {
2446 FR_SBUFF_ERROR_RETURN(&our_in);
2447 }
2448
2449 if (!fr_sbuff_next_if_char(&our_in, ')')) {
2450 fr_strerror_printf("Failed to find trailing ')'");
2451 FR_SBUFF_ERROR_RETURN(&our_in);
2452 }
2453
2454 /*
2455 * We've parsed one "thing", so we stop. The
2456 * next thing should be an operator, not another
2457 * value.
2458 */
2459 *out_c = '\0';
2460 goto done;
2461 }
2462
2463 /*
2464 * If there is a cast, try to pass it recursively to the parser. This allows us to set default
2465 * data types, etc.
2466 *
2467 * We may end up removing the cast later, if for example the tmpl is an attribute whose data type
2468 * matches the cast.
2469 *
2470 * This ifdef isn't defined anywhere, and is just a reminder to check this code when we move to
2471 * tmpl_require_enum_prefix=yes. This cast has to be deleted.
2472 */
2473 if (!tmpl_require_enum_prefix && (cast_type != FR_TYPE_NULL)) {
2474 our_t_rules.cast = cast_type;
2475 our_t_rules.enumv = NULL;
2476 }
2477
2478 /*
2479 * Record where the operand begins for better error offsets later
2480 */
2481 fr_sbuff_marker(&opand_m, &our_in);
2482
2484
2485 switch (quote) {
2486 default:
2487 case T_BARE_WORD:
2488 p_rules = bracket_rules;
2489
2490 /*
2491 * Peek for rcodes.
2492 */
2493 slen = tokenize_rcode(head, &node, &our_in);
2494 if (slen > 0) {
2495 *out = node;
2496 FR_SBUFF_SET_RETURN(in, &our_in);
2497 }
2498 break;
2499
2501 fr_strerror_const("Unexpected regular expression");
2502 fr_sbuff_set(&our_in, &opand_m); /* Error points to the quoting char at the start of the string */
2503 goto error;
2504
2507 /*
2508 * We want to force the output to be a string.
2509 */
2510 if (cast_type == FR_TYPE_NULL) cast_type = FR_TYPE_STRING;
2512
2514 /*
2515 * Don't put the cast in the tmpl, but put it instead in the expression.
2516 */
2517 if (cast_type != FR_TYPE_NULL) our_t_rules.cast = FR_TYPE_NULL;
2518
2519 p_rules = value_parse_rules_quoted[quote];
2520
2521 /*
2522 * Triple-quoted strings have different terminal conditions.
2523 */
2524 if (fr_sbuff_remaining(&our_in) >= 2) {
2525 char const *p = fr_sbuff_current(&our_in);
2526 char c = fr_token_quote[quote];
2527
2528 /*
2529 * """foo "quote" and end"""
2530 */
2531 if ((p[0] == c) && (p[1] == c)) {
2532 triple = 3;
2533 (void) fr_sbuff_advance(&our_in, 2);
2534 p_rules = value_parse_rules_3quoted[quote];
2535 }
2536 }
2537
2538 break;
2539 }
2540
2541 /*
2542 * Allocate the xlat node now so the talloc hierarchy is correct
2543 */
2544 MEM(node = xlat_exp_alloc(head, XLAT_TMPL, NULL, 0));
2545
2546 /*
2547 * tmpl_afrom_substr does pretty much all the work of
2548 * parsing the operand. It pays attention to the cast on
2549 * our_t_rules, and will try to parse any data there as
2550 * of the correct type.
2551 */
2552 if (tmpl_afrom_substr(node, &vpt, &our_in, quote, p_rules, &our_t_rules) < 0) {
2553 error:
2554 FR_SBUFF_ERROR_RETURN(&our_in);
2555 }
2556
2557 /*
2558 * Add in unknown attributes, by defining them in the local dictionary.
2559 */
2560 if (tmpl_is_attr(vpt) && (tmpl_attr_unknown_add(vpt) < 0)) {
2561 fr_strerror_printf("Failed defining attribute %s", tmpl_attr_tail_da(vpt)->name);
2562 fr_sbuff_set(&our_in, &opand_m);
2563 goto error;
2564 }
2565
2566 if (quote != T_BARE_WORD) {
2567 /*
2568 * Ensure that the string ends with the correct number of quotes.
2569 */
2570 do {
2571 if (!fr_sbuff_is_char(&our_in, fr_token_quote[quote])) {
2572 fr_strerror_const("Unterminated string");
2573 fr_sbuff_set(&our_in, &opand_m);
2574 goto error;
2575 }
2576
2577 fr_sbuff_advance(&our_in, 1);
2578 } while (--triple > 0);
2579
2580 /*
2581 * Quoted strings just get resolved now.
2582 *
2583 * @todo - this means that things like
2584 *
2585 * &Session-Timeout == '10'
2586 *
2587 * are run-time errors, instead of load-time parse errors.
2588 *
2589 * On the other hand, if people assign static strings to non-string
2590 * attributes... they sort of deserve what they get.
2591 */
2592 if (tmpl_is_data_unresolved(vpt) && (tmpl_resolve(vpt, NULL) < 0)) goto error;
2593 } else {
2594 /*
2595 * Catch the old case of alternation :(
2596 */
2597 char const *p;
2598
2599 fr_assert(talloc_array_length(vpt->name) > 1);
2600
2601 p = vpt->name + talloc_array_length(vpt->name) - 2;
2602 if ((*p == ':') && fr_sbuff_is_char(&our_in, '-')) {
2603 fr_sbuff_set(&our_in, fr_sbuff_current(&our_in) - 2);
2604 fr_strerror_const("Alternation is no longer supported. Use '%{a || b}' instead of '%{a:-b}'");
2605 goto error;
2606 }
2607 }
2608
2609 fr_sbuff_skip_whitespace(&our_in);
2610
2611 /*
2612 * The tmpl has a cast, and it's the same as the explicit cast we were given, we can discard the
2613 * explicit cast.
2614 *
2615 * Also do this for tmpls which are attributes, and which have the same data type as the cast.
2616 *
2617 * This work isn't _strictly_ necessary, but it avoids duplicate casts at run-time.
2618 */
2619 if (cast_type != FR_TYPE_NULL) {
2620 if (tmpl_rules_cast(vpt) == cast_type) {
2621 cast_type = FR_TYPE_NULL;
2622
2623 } else if (tmpl_is_attr(vpt)) {
2624 fr_dict_attr_t const *da;
2625
2627
2628 da = tmpl_attr_tail_da(vpt); /* could be a list! */
2629
2630 /*
2631 * Omit the cast if the da type matches our cast. BUT don't do this for enums! In that
2632 * case, the cast will convert the value-box to one _without_ an enumv entry, which means
2633 * that the value will get printed as its underlying data type, and not as the enum name.
2634 */
2635 if (da && (da->type == cast_type)) {
2636 tmpl_cast_set(vpt, cast_type);
2637 cast_type = FR_TYPE_NULL;
2638 }
2639
2640 } else if (tmpl_is_data(vpt)) {
2642
2643 /*
2644 * Omit our cast type if the data is already of the right type.
2645 *
2646 * Otherwise if we have a cast, then convert the data now, and then reset the
2647 * cast_type to nothing. This work allows for better errors at startup, and
2648 * minimizes run-time work.
2649 */
2650 if (tmpl_value_type(vpt) == cast_type) {
2651 cast_type = FR_TYPE_NULL;
2652
2653 } else {
2654 /*
2655 * Cast it now, and remove the cast type.
2656 */
2657 if (tmpl_cast_in_place(vpt, cast_type, NULL) < 0) {
2658 fr_sbuff_set(&our_in, &opand_m);
2659 goto error;
2660 }
2661
2662 cast_type = FR_TYPE_NULL;
2663 }
2664 }
2665
2666 /*
2667 * Push the cast down.
2668 *
2669 * But if we're casting to string, and the RHS is already a string, we don't need to cast
2670 * it. We can just discard the cast.
2671 */
2672 if ((cast_type != FR_TYPE_NULL) && (tmpl_rules_cast(vpt) == FR_TYPE_NULL)) {
2673 if ((cast_type != FR_TYPE_STRING) || (vpt->quote == T_BARE_WORD)) {
2674 tmpl_cast_set(vpt, cast_type);
2675 }
2676 cast_type = FR_TYPE_NULL;
2677 }
2678 }
2679
2680 /*
2681 * Else we're not hoisting, set the node to the VPT
2682 */
2683 node->vpt = vpt;
2684 node->quote = quote;
2686
2687 if (tmpl_is_data(node->vpt)) {
2688 /*
2689 * Print "true" and "false" instead of "yes" and "no".
2690 */
2693 }
2694
2695 node->flags.constant = true;
2696 node->flags.pure = true;
2697
2698 } else if (tmpl_contains_xlat(node->vpt)) {
2699 node->flags = tmpl_xlat(vpt)->flags;
2700
2701 } else if (tmpl_is_attr(node->vpt)) {
2702 node->flags.pure = false;
2703
2704#ifndef NDEBUG
2705 if (vpt->name[0] == '%') {
2706 fr_assert(vpt->rules.attr.prefix == TMPL_ATTR_REF_PREFIX_NO);
2707 } else {
2708 fr_assert(vpt->rules.attr.prefix == TMPL_ATTR_REF_PREFIX_YES);
2709 }
2710#endif
2711
2712 } else {
2713 node->flags.pure = false;
2714 }
2715
2716 node->flags.constant = node->flags.pure;
2717 node->flags.needs_resolving = tmpl_needs_resolving(node->vpt);
2718
2720
2721done:
2722 /*
2723 * If there is a cast, then reparent the node with a cast wrapper.
2724 */
2725 if (cast_type != FR_TYPE_NULL) {
2726 MEM(cast = expr_cast_alloc(head, cast_type));
2727 xlat_func_append_arg(cast, node, false);
2728 node = cast;
2729 }
2730
2731 *out = node;
2732
2733 FR_SBUFF_SET_RETURN(in, &our_in);
2734}
2735
2736/*
2737 * A mapping of operators to tokens.
2738 */
2740 { L("!="), T_OP_NE },
2741 { L("!=="), T_OP_CMP_NE_TYPE },
2742
2743 { L("&"), T_AND },
2744 { L("&&"), T_LAND },
2745 { L("*"), T_MUL },
2746 { L("+"), T_ADD },
2747 { L("-"), T_SUB },
2748 { L("/"), T_DIV },
2749 { L("%"), T_MOD },
2750 { L("^"), T_XOR },
2751
2752 { L("|"), T_OR },
2753 { L("||"), T_LOR },
2754
2755 { L("<"), T_OP_LT },
2756 { L("<<"), T_LSHIFT },
2757 { L("<="), T_OP_LE },
2758
2759 { L("="), T_OP_EQ },
2760 { L("=="), T_OP_CMP_EQ },
2761 { L("==="), T_OP_CMP_EQ_TYPE },
2762
2763 { L("=~"), T_OP_REG_EQ },
2764 { L("!~"), T_OP_REG_NE },
2765
2766 { L(">"), T_OP_GT },
2767 { L(">="), T_OP_GE },
2768 { L(">>"), T_RSHIFT },
2769
2770};
2772
2773static bool valid_type(xlat_exp_t *node)
2774{
2775 fr_dict_attr_t const *da;
2776
2777#ifdef STATIC_ANALYZER
2778 if (!node) return false;
2779#endif
2780
2781 if (node->type != XLAT_TMPL) return true;
2782
2783 if (tmpl_is_list(node->vpt)) {
2784 list:
2785 fr_strerror_const("Cannot use list references in condition");
2786 return false;
2787 }
2788
2789 if (!tmpl_is_attr(node->vpt)) return true;
2790
2791 da = tmpl_attr_tail_da(node->vpt);
2792 if (fr_type_is_structural(da->type)) {
2793 if (da->dict == fr_dict_internal()) goto list;
2794
2795 fr_strerror_const("Cannot use structural types in condition");
2796 return false;
2797 }
2798
2799 return true;
2800}
2801
2802
2803/** Tokenize a mathematical operation.
2804 *
2805 * (EXPR)
2806 * !EXPR
2807 * A OP B
2808 *
2809 * If "out" is NULL then the expression is added to "head".
2810 * Otherwise, it's returned to the caller.
2811 */
2813 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules,
2814 fr_token_t prev, fr_sbuff_parse_rules_t const *bracket_rules,
2815 fr_sbuff_parse_rules_t const *input_rules, bool cond)
2816{
2817 xlat_exp_t *lhs = NULL, *rhs, *node;
2818 xlat_t *func = NULL;
2819 fr_token_t op;
2820 ssize_t slen;
2821 fr_sbuff_marker_t m_lhs, m_op, m_rhs;
2822 fr_sbuff_t our_in = FR_SBUFF(in);
2823 char c = '\0';
2824
2826
2827 fr_sbuff_skip_whitespace(&our_in);
2828
2829 fr_sbuff_marker(&m_lhs, &our_in);
2830
2831 /*
2832 * Get the LHS of the operation.
2833 */
2834 slen = tokenize_unary(head, &lhs, &our_in, p_rules, t_rules, bracket_rules, &c, cond);
2835 if (slen < 0) FR_SBUFF_ERROR_RETURN(&our_in);
2836
2837 if (slen == 0) {
2838 fr_assert(lhs == NULL);
2839 *out = NULL;
2840 FR_SBUFF_SET_RETURN(in, &our_in);
2841 }
2842
2843redo:
2844 rhs = NULL;
2845
2846#ifdef STATIC_ANALYZER
2847 if (!lhs) return 0; /* shut up stupid analyzer */
2848#else
2849 fr_assert(lhs != NULL);
2850#endif
2851
2852 fr_sbuff_skip_whitespace(&our_in);
2853
2854 /*
2855 * No more input, we're done.
2856 */
2857 if (fr_sbuff_extend(&our_in) == 0) {
2858 done:
2859 *out = lhs;
2860 FR_SBUFF_SET_RETURN(in, &our_in);
2861 }
2862
2863 /*
2864 * ')' is a terminal, even if we didn't expect it.
2865 * Because if we didn't expect it, then it's an error.
2866 *
2867 * If we did expect it, then we return whatever we found,
2868 * and let the caller eat the ')'.
2869 */
2870 if (fr_sbuff_is_char(&our_in, ')')) {
2871 if (!bracket_rules) {
2872 fr_strerror_printf("Unexpected ')'");
2873 FR_SBUFF_ERROR_RETURN(&our_in);
2874 }
2875
2876 goto done;
2877 }
2878 fr_sbuff_skip_whitespace(&our_in);
2879
2880 /*
2881 * We hit a terminal sequence, stop.
2882 */
2883 if (input_rules && fr_sbuff_is_terminal(&our_in, input_rules->terminals)) goto done;
2884
2885 /*
2886 * Remember where we were after parsing the LHS.
2887 */
2888 fr_sbuff_marker(&m_op, &our_in);
2889
2890 /*
2891 * Get the operator.
2892 */
2893 XLAT_DEBUG(" operator <-- %pV", fr_box_strvalue_len(fr_sbuff_current(&our_in), fr_sbuff_remaining(&our_in)));
2895 if ((op == T_INVALID) || !binary_ops[op].str) {
2896 fr_strerror_const("Invalid operator");
2897 fr_sbuff_set(&our_in, &m_op);
2898 talloc_free(lhs);
2899 FR_SBUFF_ERROR_RETURN(&our_in);
2900 }
2901
2902 /*
2903 * We can't (yet) do &list1 = &list2 + &list3
2904 */
2905 if (fr_binary_op[op] && t_rules->enumv && fr_type_is_structural(t_rules->enumv->type)) {
2906 fr_strerror_const("Invalid operator for structural attribute");
2907 fr_sbuff_set(&our_in, &m_op);
2908 talloc_free(lhs);
2909 FR_SBUFF_ERROR_RETURN(&our_in);
2910 }
2911
2912 fr_assert(precedence[op] != 0);
2913
2914 /*
2915 * a * b + c ... = (a * b) + c ...
2916 *
2917 * Feed the current expression to the caller, who will
2918 * take care of continuing.
2919 */
2920 if (precedence[op] <= precedence[prev]) {
2921 fr_sbuff_set(&our_in, &m_op);
2922 goto done;
2923 }
2924
2925 /*
2926 * &Foo and !&Foo are permitted as the LHS of || and &&
2927 */
2928 if (((c == '!') || (c == '~')) && (op != T_LAND) && (op != T_LOR)) {
2929 fr_strerror_printf("Operator '%c' is only applied to the left hand side of the '%s' operation, add (..) to evaluate the operation first", c, fr_tokens[op]);
2930 fail_lhs:
2931 fr_sbuff_set(&our_in, &m_lhs);
2932 FR_SBUFF_ERROR_RETURN(&our_in);
2933 }
2934
2935 fr_sbuff_skip_whitespace(&our_in);
2936 fr_sbuff_marker(&m_rhs, &our_in);
2937
2938#if 0
2939 /*
2940 * If LHS is attr && structural, allow only == and !=, then check that the RHS is {}.
2941 *
2942 * However, we don't want the LHS evaluated, so just re-write it as an "exists" xlat?
2943 *
2944 * @todo - check lists for equality?
2945 */
2946 if ((lhs->type == XLAT_TMPL) && tmpl_is_attr(lhs->vpt) && fr_type_is_structural(tmpl_attr_tail_da(lhs->vpt)->type)) {
2947 if ((op != T_OP_CMP_EQ) && (op != T_OP_NE)) {
2948 fr_strerror_printf("Invalid operator '%s' for left hand side structural attribute", fr_tokens[op]);
2949 fr_sbuff_set(&our_in, &m_op);
2950 FR_SBUFF_ERROR_RETURN(&our_in);
2951 }
2952
2953 fr_assert(0);
2954 }
2955#endif
2956
2957 /*
2958 * We now parse the RHS, allowing a (perhaps different) cast on the RHS.
2959 */
2960 XLAT_DEBUG(" recurse RHS <-- %pV", fr_box_strvalue_len(fr_sbuff_current(&our_in), fr_sbuff_remaining(&our_in)));
2961 if ((op == T_OP_REG_EQ) || (op == T_OP_REG_NE)) {
2962 slen = tokenize_regex_rhs(head, &rhs, &our_in, t_rules, bracket_rules);
2963 } else {
2964 slen = tokenize_expression(head, &rhs, &our_in, p_rules, t_rules, op, bracket_rules, input_rules, cond);
2965 }
2966 if (slen < 0) {
2967 talloc_free(lhs);
2968 FR_SBUFF_ERROR_RETURN(&our_in);
2969 }
2970
2971#ifdef STATIC_ANALYZER
2972 if (!rhs) return -1;
2973#endif
2974
2975 func = xlat_func_find(binary_ops[op].str, binary_ops[op].len);
2976 fr_assert(func != NULL);
2977
2978 if (multivalue_ops[op]) {
2979 if ((lhs->type == XLAT_FUNC) && (lhs->call.func->token == op)) {
2980 xlat_func_append_arg(lhs, rhs, cond);
2981
2982 lhs->call.args->flags.can_purify |= rhs->flags.can_purify | rhs->flags.pure;
2983 lhs->flags.can_purify = lhs->call.args->flags.can_purify;
2984 goto redo;
2985 }
2986 goto purify;
2987 }
2988
2989 /*
2990 * Complain on comparisons between invalid data types.
2991 *
2992 * @todo - allow
2993 *
2994 * &structural == {}
2995 * &structural != {}
2996 *
2997 * as special cases, so we can check lists for emptiness.
2998 */
2999 if (fr_comparison_op[op]) {
3000 if (!valid_type(lhs)) goto fail_lhs;
3001 if (!valid_type(rhs)) {
3002 fr_sbuff_set(&our_in, &m_rhs);
3003 FR_SBUFF_ERROR_RETURN(&our_in);
3004 }
3005
3006 /*
3007 * Peephole optimization. If both LHS
3008 * and RHS are static values, then just call the
3009 * relevant condition code to get the result.
3010 */
3011 if (cond) {
3012 int rcode;
3013
3014 purify:
3015 rcode = xlat_purify_op(head, &node, lhs, op, rhs);
3016 if (rcode < 0) goto fail_lhs;
3017
3018 if (rcode) {
3019 lhs = node;
3020 goto redo;
3021 }
3022 }
3023 }
3024
3025 /*
3026 * Create the function node, with the LHS / RHS arguments.
3027 */
3028 MEM(node = xlat_exp_alloc(head, XLAT_FUNC, fr_tokens[op], strlen(fr_tokens[op])));
3029 node->call.func = func;
3030 node->call.dict = t_rules->attr.dict_def;
3031 node->flags = func->flags;
3032 node->flags.impure_func = !func->flags.pure;
3033
3034 xlat_func_append_arg(node, lhs, logical_ops[op] && cond);
3035 xlat_func_append_arg(node, rhs, logical_ops[op] && cond);
3036
3037 fr_assert(xlat_exp_head(node->call.args) != NULL);
3038
3039 /*
3040 * Logical operations can be purified if ANY of their arguments can be purified.
3041 */
3042 if (logical_ops[op]) {
3043 xlat_exp_foreach(node->call.args, arg) {
3044 node->call.args->flags.can_purify |= arg->flags.can_purify | arg->flags.pure;
3045 if (node->call.args->flags.can_purify) break;
3046 }
3047 node->flags.can_purify = node->call.args->flags.can_purify;
3048
3049 } else {
3050 node->flags.can_purify = (node->call.func->flags.pure && node->call.args->flags.pure) | node->call.args->flags.can_purify;
3051 }
3052
3053 lhs = node;
3054 goto redo;
3055}
3056
3058 L(""),
3059 L(")"),
3060);
3061
3063 L("\t"),
3064 L("\n"),
3065 L("\r"),
3066 L(" "),
3067 L("!"),
3068 L("%"),
3069 L("&"),
3070 L("*"),
3071 L("+"),
3072 L("-"),
3073 L("/"),
3074 L("<"),
3075 L("="),
3076 L(">"),
3077 L("^"),
3078 L("|"),
3079 L("~"),
3080);
3081
3083 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool cond)
3084{
3085 ssize_t slen;
3086 fr_sbuff_parse_rules_t *bracket_rules = NULL;
3087 fr_sbuff_parse_rules_t *terminal_rules = NULL;
3088 tmpl_rules_t my_rules = { };
3090 xlat_exp_t *node = NULL;
3091
3092 /*
3093 * Whatever the caller passes, ensure that we have a
3094 * terminal rule which ends on operators, and a terminal
3095 * rule which ends on ')'.
3096 */
3097 MEM(bracket_rules = talloc_zero(ctx, fr_sbuff_parse_rules_t));
3098 MEM(terminal_rules = talloc_zero(ctx, fr_sbuff_parse_rules_t));
3099 if (p_rules) {
3100 *bracket_rules = *p_rules;
3101 *terminal_rules = *p_rules;
3102
3103 if (p_rules->terminals) {
3104 MEM(terminal_rules->terminals = fr_sbuff_terminals_amerge(terminal_rules,
3105 p_rules->terminals,
3106 &operator_terms));
3107 } else {
3108 terminal_rules->terminals = &operator_terms;
3109 }
3110 } else {
3111 terminal_rules->terminals = &operator_terms;
3112 }
3113 MEM(bracket_rules->terminals = fr_sbuff_terminals_amerge(bracket_rules,
3114 terminal_rules->terminals,
3115 &bracket_terms));
3116
3118 if (!t_rules) t_rules = &my_rules;
3119
3120 slen = tokenize_expression(head, &node, in, terminal_rules, t_rules, T_INVALID, bracket_rules, p_rules, cond);
3121 talloc_free(bracket_rules);
3122 talloc_free(terminal_rules);
3123
3124 if (slen < 0) {
3127 }
3128
3129 if (!node) {
3130 *out = head;
3131 return slen;
3132 }
3133
3134 /*
3135 * If the tmpl is not resolved, then it refers to an attribute which doesn't exist. That's an
3136 * error.
3137 */
3138 if ((node->type == XLAT_TMPL) && tmpl_is_data_unresolved(node->vpt)) {
3139 fr_strerror_const("Unexpected text - attribute names must prefixed with '&'");
3140 return -1;
3141 }
3142
3143 /*
3144 * Convert raw existence checks to existence functions.
3145 */
3146 if (cond && (node->type == XLAT_TMPL) && tmpl_contains_attr(node->vpt)) {
3147 node = xlat_exists_alloc(head, node);
3148 }
3149
3151
3152 /*
3153 * Add nodes that need to be bootstrapped to
3154 * the registry.
3155 */
3156 if (xlat_finalize(head, t_rules->xlat.runtime_el) < 0) {
3158 return -1;
3159 }
3160
3161 *out = head;
3162 return slen;
3163}
3164
3166 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
3167{
3168 return xlat_tokenize_expression_internal(ctx, out, in, p_rules, t_rules, false);
3169}
3170
3172 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
3173{
3174 return xlat_tokenize_expression_internal(ctx, out, in, p_rules, t_rules, true);
3175}
3176
3177/** Allow callers to see if an xlat is truthy
3178 *
3179 * So the caller can cache it, and needs to check fewer things at run
3180 * time.
3181 *
3182 * @param[in] head of the xlat to check
3183 * @param[out] out truthiness of the box
3184 * @return
3185 * - false - xlat is not truthy, *out is unchanged.
3186 * - true - xlat is truthy, *out is the result of fr_value_box_is_truthy()
3187 */
3189{
3190 xlat_exp_t const *node;
3191 fr_value_box_t const *box;
3192
3193 /*
3194 * Only pure / constant things can be truthy.
3195 */
3196 if (!head->flags.pure) goto return_false;
3197
3198 node = xlat_exp_head(head);
3199 if (!node) {
3200 *out = false;
3201 return true;
3202 }
3203
3204 if (xlat_exp_next(head, node)) goto return_false;
3205
3206 if (node->type == XLAT_BOX) {
3207 box = &node->data;
3208
3209 } else if ((node->type == XLAT_TMPL) && tmpl_is_data(node->vpt)) {
3210 box = tmpl_value(node->vpt);
3211
3212 } else {
3213 return_false:
3214 *out = false;
3215 return false;
3216 }
3217
3219 return true;
3220}
va_list args
Definition acutest.h:770
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:483
#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:322
#define unlikely(_x)
Definition build.h:381
#define UNUSED
Definition build.h:315
#define NUM_ELEMENTS(_t)
Definition build.h:337
int fr_value_calc_list_cmp(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_list_t const *list1, fr_token_t op, fr_value_box_list_t const *list2)
Definition calc.c:2688
int fr_value_calc_binary_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t hint, fr_value_box_t const *a, fr_token_t op, fr_value_box_t const *b)
Calculate DST = A OP B.
Definition calc.c:1894
int fr_value_calc_unary_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_token_t op, fr_value_box_t const *src)
Calculate unary operations.
Definition calc.c:2460
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:41
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4610
static fr_slen_t in
Definition dict.h:824
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:638
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 int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:378
static xlat_action_t xlat_func_rcode(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *args)
Return the current rcode as a string.
Definition xlat_expr.c:1625
static xlat_action_t xlat_func_exists(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
See if a named attribute exists.
Definition xlat_expr.c:1782
static xlat_action_t xlat_func_expr_rcode(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Match the passed rcode against request->rcode.
Definition xlat_expr.c:1574
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:1403
#define UNLANG_SUB_FRAME
Definition interpret.h:36
#define RDEBUG3(fmt,...)
Definition log.h:343
#define RPEDEBUG(fmt,...)
Definition log.h:376
talloc_free(reap)
@ TMPL_ATTR_REF_PREFIX_NO
Attribute refs have no '&' prefix.
@ TMPL_ATTR_REF_PREFIX_YES
Attribute refs must have '&' prefix.
fr_type_t
@ FR_TYPE_INT8
8 Bit signed integer.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_INT16
16 Bit signed integer.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_INT32
32 Bit signed integer.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_SIZE
Unsigned integer capable of representing any memory address on the local system.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
unsigned int uint32_t
long int ssize_t
ssize_t fr_slen_t
#define check(_handle, _len_p)
Definition bio.c:44
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:80
#define REDEBUG(fmt,...)
Definition radclient.h:52
fr_table_num_sorted_t const rcode_table[]
Definition rcode.c:35
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:51
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:50
static char const * name
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
Definition sbuff.c:2152
fr_sbuff_term_t * fr_sbuff_terminals_amerge(TALLOC_CTX *ctx, fr_sbuff_term_t const *a, fr_sbuff_term_t const *b)
Merge two sets of terminal strings.
Definition sbuff.c:645
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:2088
#define fr_sbuff_start(_sbuff_or_marker)
#define fr_sbuff_out_by_longest_prefix(_match_len, _out, _table, _sbuff, _def)
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_diff(_a, _b)
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_adv_past_whitespace(_sbuff, _len, _tt)
#define fr_sbuff_current(_sbuff_or_marker)
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition sbuff.h:192
#define FR_SBUFF_IN_STRCPY_LITERAL_RETURN(_sbuff, _str)
#define fr_sbuff_extend(_sbuff_or_marker)
#define fr_sbuff_used_total(_sbuff_or_marker)
#define FR_SBUFF_RETURN(_func, _sbuff,...)
#define fr_sbuff_is_char(_sbuff_or_marker, _c)
#define FR_SBUFF_ERROR_RETURN(_sbuff_or_marker)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define fr_sbuff_is_digit(_sbuff_or_marker)
#define SBUFF_CHAR_UNPRINTABLES_EXTENDED
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define fr_sbuff_remaining(_sbuff_or_marker)
#define SBUFF_CHAR_UNPRINTABLES_LOW
#define fr_sbuff_used(_sbuff_or_marker)
#define FR_SBUFF_IN_STRCPY_RETURN(...)
#define FR_SBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
Terminal element with pre-calculated lengths.
Definition sbuff.h:161
Set of terminal elements.
#define tmpl_contains_xlat(vpt)
Definition tmpl.h:231
#define tmpl_is_attr_unresolved(vpt)
Definition tmpl.h:224
static fr_slen_t vpt
Definition tmpl.h:1272
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:948
int tmpl_attr_unknown_add(tmpl_t *vpt)
Add an unknown fr_dict_attr_t specified by a tmpl_t to the main dictionary.
#define tmpl_contains_regex(vpt)
Definition tmpl.h:230
#define tmpl_is_attr(vpt)
Definition tmpl.h:213
fr_dict_attr_t const * enumv
Enumeration attribute used to resolve enum values.
Definition tmpl.h:347
#define tmpl_value_enumv(_tmpl)
Definition tmpl.h:951
#define tmpl_xlat(_tmpl)
Definition tmpl.h:941
#define tmpl_rules_cast(_tmpl)
Definition tmpl.h:953
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.
#define tmpl_contains_attr(vpt)
Definition tmpl.h:229
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.
tmpl_xlat_rules_t xlat
Rules/data for parsing xlats.
Definition tmpl.h:345
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:931
int tmpl_cast_in_place(tmpl_t *vpt, fr_type_t type, fr_dict_attr_t const *enumv))
Convert tmpl_t of type TMPL_TYPE_DATA_UNRESOLVED or TMPL_TYPE_DATA to TMPL_TYPE_DATA of type specifie...
#define tmpl_is_data(vpt)
Definition tmpl.h:211
#define tmpl_value_type(_tmpl)
Definition tmpl.h:950
#define tmpl_is_data_unresolved(vpt)
Definition tmpl.h:222
fr_type_t cast
Whether there was an explicit cast.
Definition tmpl.h:349
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:344
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:812
struct tmpl_res_rules_s tmpl_res_rules_t
Definition tmpl.h:240
#define tmpl_is_regex(vpt)
Definition tmpl.h:218
fr_dict_attr_t const * enumv
for resolving T_BARE_WORD
Definition tmpl.h:382
fr_event_list_t * runtime_el
The eventlist to use for runtime instantiation of xlats.
Definition tmpl.h:333
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:228
int tmpl_cast_set(tmpl_t *vpt, fr_type_t type)
Set a cast for a tmpl.
Similar to tmpl_rules_t, but used to specify parameters that may change during subsequent resolution ...
Definition tmpl.h:373
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:341
static void xor(char *out, char *in1, char *in2, int n)
Definition smbdes.c:183
static void lshift(char *d, int count, int n)
Definition smbdes.c:165
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
fr_pair_t * vp
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:285
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
#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 an arbitrarily ordered array of name to num mappings.
Definition table.h:57
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
#define talloc_get_type_abort_const
Definition talloc.h:282
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:224
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 char fr_token_quote[T_TOKEN_LAST]
Convert tokens back to a quoting character.
Definition token.c:156
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:78
const bool fr_comparison_op[T_TOKEN_LAST]
Definition token.c:198
const bool fr_binary_op[T_TOKEN_LAST]
Definition token.c:216
enum fr_token fr_token_t
@ T_AND
Definition token.h:55
@ T_INVALID
Definition token.h:39
@ T_SUB
Definition token.h:52
@ T_RSHIFT
Definition token.h:62
@ T_NOT
Definition token.h:57
@ T_XOR
Definition token.h:58
@ T_DIV
Definition token.h:54
@ T_SINGLE_QUOTED_STRING
Definition token.h:122
@ T_MOD
Definition token.h:60
@ T_BARE_WORD
Definition token.h:120
@ T_OP_EQ
Definition token.h:83
@ T_LAND
Definition token.h:91
@ T_COMPLEMENT
Definition token.h:59
@ T_ADD
Definition token.h:51
@ T_BACK_QUOTED_STRING
Definition token.h:123
@ T_OP_NE
Definition token.h:97
@ T_LOR
Definition token.h:92
@ T_LSHIFT
Definition token.h:63
@ T_OP_REG_EQ
Definition token.h:102
@ T_OP_CMP_EQ_TYPE
Definition token.h:107
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
@ T_OP_CMP_EQ
Definition token.h:106
@ T_LBRACE
Definition token.h:43
@ T_MUL
Definition token.h:53
@ T_OP_LE
Definition token.h:100
@ T_OP_CMP_NE_TYPE
Definition token.h:108
@ 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_OR
Definition token.h:56
#define T_TOKEN_LAST
Definition token.h:129
xlat_action_t unlang_xlat_yield(request_t *request, xlat_func_t resume, xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition xlat.c:573
int unlang_xlat_push(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat, bool top_frame)
Push a pre-compiled xlat onto the stack for evaluation.
Definition xlat.c:286
tmpl_res_rules_t const * tr_rules
tmpl resolution rules.
Definition xlat.h:163
fr_slen_t xlat_print(fr_sbuff_t *in, xlat_exp_head_t const *node, fr_sbuff_escape_rules_t const *e_rules)
Reconstitute an xlat expression from its constituent nodes.
bool required
Argument must be present, and non-empty.
Definition xlat.h:148
static fr_slen_t head
Definition xlat.h:422
int xlat_flatten_compiled_argv(TALLOC_CTX *ctx, xlat_exp_head_t ***argv, xlat_exp_head_t *head)
Turn xlat_tokenize_argv() into an argv[] array, and nuke the input list.
Definition xlat_eval.c:1622
bool concat
Concat boxes together.
Definition xlat.h:149
static fr_slen_t xlat_aprint(TALLOC_CTX *ctx, char **out, xlat_exp_head_t const *head, fr_sbuff_escape_rules_t const *e_rules) 1(xlat_print
bool can_purify
if the xlat has a pure function with pure arguments.
Definition xlat.h:116
int xlat_purify_op(TALLOC_CTX *ctx, xlat_exp_t **out, xlat_exp_t *lhs, fr_token_t op, xlat_exp_t *rhs)
xlat_action_t(* xlat_func_t)(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
xlat callback function
Definition xlat.h:230
#define XLAT_ARGS(_list,...)
Populate local variables with value boxes from the input list.
Definition xlat.h:381
bool pure
has no external side effects, true for BOX, LITERAL, and some functions
Definition xlat.h:114
bool needs_resolving
Needs pass2 resolution.
Definition xlat.h:113
int xlat_resolve(xlat_exp_head_t *head, xlat_res_rules_t const *xr_rules)
Walk over an xlat tree recursively, resolving any unresolved functions or references.
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:168
int xlat_finalize(xlat_exp_head_t *head, fr_event_list_t *runtime_el)
Bootstrap static xlats, or instantiate ephemeral ones.
Definition xlat_inst.c:695
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_YIELD
An xlat function pushed a resume frame onto the stack.
Definition xlat.h:42
@ XLAT_ACTION_PUSH_UNLANG
An xlat function pushed an unlang frame onto the unlang stack.
Definition xlat.h:39
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
bool constant
xlat is just tmpl_attr_tail_data, or XLAT_BOX
Definition xlat.h:118
bool impure_func
xlat contains an impure function
Definition xlat.h:115
int xlat_instance_unregister_func(xlat_exp_t *node)
Remove a node from the list of xlat instance data.
Definition xlat_inst.c:550
Definition for a single argument consumend by an xlat function.
Definition xlat.h:147
Flags that control resolution and evaluation.
Definition xlat.h:112
static fr_slen_t parent
Definition pair.h:851
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
#define fr_type_is_variable_size(_x)
Definition types.h:367
#define fr_type_is_structural(_x)
Definition types.h:371
#define fr_type_is_null(_x)
Definition types.h:326
#define fr_type_is_leaf(_x)
Definition types.h:372
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:433
fr_sbuff_parse_rules_t const * value_parse_rules_quoted[T_TOKEN_LAST]
Parse rules for quoted strings.
Definition value.c:606
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:3740
bool fr_value_box_is_truthy(fr_value_box_t const *in)
Check truthiness of values.
Definition value.c:6326
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:3572
void fr_value_box_memdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Assign a buffer to a box, but don't copy it.
Definition value.c:4548
fr_sbuff_parse_rules_t const * value_parse_rules_3quoted[T_TOKEN_LAST]
Definition value.c:622
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:3927
void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4036
ssize_t fr_value_box_list_concat_as_string(bool *tainted, bool *secret, fr_sbuff_t *sbuff, fr_value_box_list_t *list, char const *sep, size_t sep_len, fr_sbuff_escape_rules_t const *e_rules, fr_value_box_list_action_t proc_action, fr_value_box_safe_for_t safe_for, bool flatten)
Concatenate a list of value boxes together.
Definition value.c:5584
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:3723
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4148
@ FR_VALUE_BOX_LIST_FREE_BOX
Free each processed box.
Definition value.h:218
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:621
static fr_slen_t data
Definition value.h:1265
#define fr_box_strvalue_len(_val, _len)
Definition value.h:286
#define fr_box_is_group(_x)
Definition value.h:430
#define VALUE_BOX_LIST_VERIFY(_x)
Definition value.h:1296
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:632
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:587
#define fr_value_box_list_foreach(_list_head, _iter)
Definition value.h:206
static size_t char ** out
Definition value.h:997
void xlat_exp_set_name(xlat_exp_t *node, char const *fmt, size_t len)
Set the format string for an xlat node.
Definition xlat_alloc.c:191
void xlat_exp_set_name_buffer_shallow(xlat_exp_t *node, char const *fmt)
Set the format string for an xlat node from a pre-existing buffer.
Definition xlat_alloc.c:213
void * rctx
Resume context.
Definition xlat_ctx.h:54
xlat_exp_t * ex
Tokenized expression to use in expansion.
Definition xlat_ctx.h:63
void const * inst
xlat instance data.
Definition xlat_ctx.h:50
void * inst
xlat instance data to populate.
Definition xlat_ctx.h:62
An xlat calling ctx.
Definition xlat_ctx.h:49
An xlat instantiation ctx.
Definition xlat_ctx.h:61
fr_dict_attr_t const * attr_expr_bool_enum
Definition xlat_eval.c:49
fr_dict_attr_t const * attr_cast_base
Definition xlat_eval.c:50
static const fr_sbuff_escape_rules_t regex_escape_rules
Definition xlat_expr.c:537
tmpl_t const * vpt
the attribute reference
Definition xlat_expr.c:1645
fr_value_box_list_t list
Definition xlat_expr.c:1651
#define fr_sbuff_skip_whitespace(_x)
Definition xlat_expr.c:2047
static size_t const expr_assignment_op_table_len
Definition xlat_expr.c:2771
static fr_slen_t xlat_expr_print_binary(fr_sbuff_t *out, xlat_exp_t const *node, UNUSED void *inst, fr_sbuff_escape_rules_t const *e_rules)
Definition xlat_expr.c:145
#define XLAT_REGISTER_BOOL(_xlat, _func, _arg, _ret_type)
Definition xlat_expr.c:1861
static fr_slen_t xlat_expr_print_rcode(fr_sbuff_t *out, xlat_exp_t const *node, void *instance, UNUSED fr_sbuff_escape_rules_t const *e_rules)
Definition xlat_expr.c:1539
static xlat_action_t xlat_regex_match(TALLOC_CTX *ctx, request_t *request, fr_value_box_list_t *in, regex_t **preg, fr_dcursor_t *out, fr_token_t op)
Perform a regular expressions comparison between two operands.
Definition xlat_expr.c:595
static void xlat_func_append_arg(xlat_exp_t *head, xlat_exp_t *node, bool exists)
Definition xlat_expr.c:73
bool xlat_is_truthy(xlat_exp_head_t const *head, bool *out)
Allow callers to see if an xlat is truthy.
Definition xlat_expr.c:3188
static xlat_action_t xlat_func_logical(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Process logical &&, ||.
Definition xlat_expr.c:1292
xlat_exp_t * xlat
to expand
Definition xlat_expr.c:445
#define XLAT_BINARY_FUNC(_name, _op)
Definition xlat_expr.c:362
static const fr_sbuff_term_t bracket_terms
Definition xlat_expr.c:3057
static fr_slen_t tokenize_unary(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, fr_sbuff_parse_rules_t const *bracket_rules, char *out_c, bool cond)
Definition xlat_expr.c:2083
bool tmpl_require_enum_prefix
static size_t expr_quote_table_len
Definition xlat_expr.c:2067
static xlat_action_t xlat_exists_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Definition xlat_expr.c:1722
static int xlat_instantiate_exists(xlat_inst_ctx_t const *xctx)
Definition xlat_expr.c:1679
static fr_slen_t xlat_expr_print_nary(fr_sbuff_t *out, xlat_exp_t const *node, void *instance, fr_sbuff_escape_rules_t const *e_rules)
Definition xlat_expr.c:779
int xlat_register_expressions(void)
Definition xlat_expr.c:1877
xlat_exp_head_t * xlat
the xlat which needs expanding
Definition xlat_expr.c:1646
static xlat_arg_parser_t const xlat_func_exists_arg[]
Definition xlat_expr.c:1654
static xlat_action_t xlat_logical_process_arg(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *in)
Process one argument of a logical operation.
Definition xlat_expr.c:1030
static fr_slen_t xlat_tokenize_expression_internal(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool cond)
Definition xlat_expr.c:3082
static int xlat_function_args_to_tmpl(xlat_inst_ctx_t const *xctx)
Convert XLAT_BOX arguments to XLAT_TMPL.
Definition xlat_expr.c:1418
static fr_slen_t xlat_expr_print_unary(fr_sbuff_t *out, xlat_exp_t const *node, UNUSED void *inst, fr_sbuff_escape_rules_t const *e_rules)
Definition xlat_expr.c:135
#define P(_x, _y)
Definition xlat_expr.c:1991
static xlat_arg_parser_t const regex_op_xlat_args[]
Definition xlat_expr.c:568
static void xlat_ungroup(xlat_exp_head_t *head)
Undo work which shouldn't have been done.
Definition xlat_expr.c:878
static xlat_action_t xlat_func_unary_complement(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Definition xlat_expr.c:1400
static xlat_action_t xlat_cmp_op(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in, fr_token_t op)
Definition xlat_expr.c:387
static const int precedence[T_TOKEN_LAST]
Definition xlat_expr.c:1993
static xlat_action_t xlat_func_unary_minus(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Definition xlat_expr.c:1393
static xlat_action_t xlat_logical_or_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Definition xlat_expr.c:1112
static fr_table_num_sorted_t const expr_quote_table[]
Definition xlat_expr.c:2061
#define XLAT_REGISTER_BINARY_OP(_op, _name)
Definition xlat_expr.c:1818
#define XLAT_REGEX_FUNC(_name, _op)
Definition xlat_expr.c:753
static fr_slen_t tokenize_rcode(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuff_t *in)
Definition xlat_expr.c:2359
static xlat_action_t xlat_binary_op(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in, fr_token_t op, fr_type_t default_type, fr_dict_attr_t const *enumv)
Definition xlat_expr.c:296
static bool valid_type(xlat_exp_t *node)
Definition xlat_expr.c:2773
static xlat_exp_t * expr_cast_alloc(TALLOC_CTX *ctx, fr_type_t type)
Allocate a specific cast node.
Definition xlat_expr.c:2187
static bool xlat_logical_and(xlat_logical_rctx_t *rctx, fr_value_box_list_t const *in)
See if the input is truthy or not.
Definition xlat_expr.c:1170
fr_value_box_t * box
output value-box
Definition xlat_expr.c:774
#define XLAT_DEBUG(...)
Definition xlat_expr.c:39
fr_value_box_list_t list
Definition xlat_expr.c:451
static xlat_action_t xlat_regex_op(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in, fr_token_t op)
Definition xlat_expr.c:720
static xlat_action_t xlat_func_unary_not(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
Definition xlat_expr.c:1366
fr_regex_flags_t * regex_flags
Definition xlat_expr.c:446
static bool xlat_node_matches_bool(bool *result, xlat_exp_t *parent, xlat_exp_head_t *head, bool sense)
Definition xlat_expr.c:827
static int xlat_instantiate_regex(xlat_inst_ctx_t const *xctx)
Definition xlat_expr.c:493
static xlat_action_t xlat_func_unary_op(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in, fr_token_t op)
Definition xlat_expr.c:1322
static int xlat_expr_resolve_binary(xlat_exp_t *node, UNUSED void *inst, xlat_res_rules_t const *xr_rules)
Definition xlat_expr.c:168
static const bool logical_ops[T_TOKEN_LAST]
Definition xlat_expr.c:1972
xlat_func_t callback
Definition xlat_expr.c:766
static xlat_arg_parser_t const binary_op_xlat_args[]
Definition xlat_expr.c:290
static xlat_action_t xlat_attr_exists(TALLOC_CTX *ctx, fr_dcursor_t *out, request_t *request, tmpl_t const *vpt, bool do_free)
Definition xlat_expr.c:1703
static xlat_arg_parser_t const binary_cmp_xlat_args[]
Definition xlat_expr.c:381
static const bool multivalue_ops[T_TOKEN_LAST]
Definition xlat_expr.c:1980
static const fr_sbuff_term_elem_t binary_ops[T_TOKEN_LAST]
Definition xlat_expr.c:1940
static bool xlat_logical_or(xlat_logical_rctx_t *rctx, fr_value_box_list_t const *in)
See if the input is truthy or not.
Definition xlat_expr.c:1067
fr_token_t op
Definition xlat_expr.c:443
TALLOC_CTX * ctx
Definition xlat_expr.c:772
regex_t * regex
precompiled regex
Definition xlat_expr.c:444
#define XLAT_REGISTER_NARY_OP(_op, _name, _func_name)
Definition xlat_expr.c:1840
static int xlat_instantiate_expr_rcode(xlat_inst_ctx_t const *xctx)
Convert static expr_rcode arguments into rcodes.
Definition xlat_expr.c:1467
static void fr_value_box_init_zero(fr_value_box_t *vb, fr_type_t type)
Definition xlat_expr.c:273
static xlat_action_t xlat_regex_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Definition xlat_expr.c:680
static fr_table_num_ordered_t const expr_assignment_op_table[]
Definition xlat_expr.c:2739
static xlat_exp_t * xlat_exists_alloc(TALLOC_CTX *ctx, xlat_exp_t *child)
Allocate a specific cast node.
Definition xlat_expr.c:113
static fr_slen_t expr_cast_from_substr(fr_type_t *cast, fr_sbuff_t *in)
Definition xlat_expr.c:2222
#define XLAT_REGISTER_BINARY_CMP(_op, _name)
Definition xlat_expr.c:1829
static xlat_arg_parser_t const xlat_func_rcode_arg[]
Takes no arguments.
Definition xlat_expr.c:1611
static int xlat_expr_logical_purify(xlat_exp_t *node, void *instance, request_t *request)
If any argument resolves to inst->stop_on_match, the entire thing is a bool of inst->stop_on_match.
Definition xlat_expr.c:904
fr_value_box_list_t list
Definition xlat_expr.c:776
rlm_rcode_t rcode
The preparsed rcode.
Definition xlat_expr.c:1459
xlat_exp_head_t ** argv
Definition xlat_expr.c:768
static fr_slen_t xlat_expr_print_exists(fr_sbuff_t *out, xlat_exp_t const *node, void *instance, fr_sbuff_escape_rules_t const *e_rules)
Definition xlat_expr.c:1662
static int xlat_instantiate_logical(xlat_inst_ctx_t const *xctx)
Definition xlat_expr.c:1272
#define XLAT_REGISTER_UNARY(_op, _xlat, _func)
Definition xlat_expr.c:1868
static xlat_arg_parser_t const unary_op_xlat_args[]
Definition xlat_expr.c:1317
fr_slen_t xlat_tokenize_expression(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
Definition xlat_expr.c:3165
static xlat_arg_parser_t const xlat_func_expr_rcode_arg[]
Definition xlat_expr.c:1451
static fr_slen_t tokenize_regex_rhs(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuff_t *in, tmpl_rules_t const *t_rules, fr_sbuff_parse_rules_t const *bracket_rules)
Definition xlat_expr.c:2262
static fr_slen_t xlat_expr_print_regex(fr_sbuff_t *out, xlat_exp_t const *node, void *instance, fr_sbuff_escape_rules_t const *e_rules)
Definition xlat_expr.c:454
static const fr_sbuff_term_t operator_terms
Definition xlat_expr.c:3062
#define XLAT_REGISTER_REGEX_OP(_op, _name)
Definition xlat_expr.c:1851
static xlat_action_t xlat_logical_and_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Definition xlat_expr.c:1220
static ssize_t tokenize_field(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, fr_sbuff_parse_rules_t const *bracket_rules, char *out_c, bool cond)
Definition xlat_expr.c:2405
static ssize_t tokenize_expression(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, fr_token_t prev, fr_sbuff_parse_rules_t const *bracket_rules, fr_sbuff_parse_rules_t const *input_rules, bool cond)
Tokenize a mathematical operation.
Definition xlat_expr.c:2812
#define XLAT_CMP_FUNC(_name, _op)
Definition xlat_expr.c:425
fr_slen_t xlat_tokenize_condition(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
Definition xlat_expr.c:3171
Holds the result of pre-parsing the rcode on startup.
Definition xlat_expr.c:1458
void xlat_func_flags_set(xlat_t *x, xlat_func_flags_t flags)
Specify flags that alter the xlat's behaviour.
Definition xlat_func.c:402
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:365
xlat_t * xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type)
Register an xlat function.
Definition xlat_func.c:218
void xlat_func_print_set(xlat_t *xlat, xlat_print_t func)
Set a print routine for an xlat function.
Definition xlat_func.c:414
xlat_t * xlat_func_find(char const *in, ssize_t inlen)
Definition xlat_func.c:79
#define xlat_func_instantiate_set(_xlat, _instantiate, _inst_struct, _detach, _uctx)
Set a callback for global instantiation of xlat functions.
Definition xlat_func.h:93
@ XLAT_FUNC_FLAG_INTERNAL
Definition xlat_func.h:39
#define xlat_exp_head_alloc(_ctx)
Definition xlat_priv.h:271
xlat_flags_t flags
Flags that control resolution and evaluation.
Definition xlat_priv.h:157
static xlat_exp_t * xlat_exp_next(xlat_exp_head_t const *head, xlat_exp_t const *node)
Definition xlat_priv.h:244
xlat_flags_t flags
Flags that control resolution and evaluation.
Definition xlat_priv.h:190
fr_token_t quote
Type of quoting around XLAT_GROUP types.
Definition xlat_priv.h:155
@ XLAT_BOX
fr_value_box_t
Definition xlat_priv.h:107
@ XLAT_TMPL
xlat attribute
Definition xlat_priv.h:113
@ XLAT_FUNC
xlat module
Definition xlat_priv.h:109
@ XLAT_GROUP
encapsulated string of xlats
Definition xlat_priv.h:117
static void xlat_flags_merge(xlat_flags_t *parent, xlat_flags_t const *child)
Merge flags from child to parent.
Definition xlat_priv.h:227
#define xlat_exp_set_type(_node, _type)
Definition xlat_priv.h:274
fr_token_t token
for expressions
Definition xlat_priv.h:68
char const *_CONST fmt
The original format string (a talloced buffer).
Definition xlat_priv.h:154
ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_head_t const *head, xlat_exp_t const *node, fr_sbuff_escape_rules_t const *e_rules, char c)
int xlat_purify_list(xlat_exp_head_t *head, request_t *request)
Definition xlat_purify.c:60
xlat_type_t _CONST type
type of this expansion.
Definition xlat_priv.h:158
#define xlat_exp_alloc(_ctx, _type, _in, _inlen)
Definition xlat_priv.h:280
xlat_flags_t flags
various flags
Definition xlat_priv.h:90
#define xlat_exp_foreach(_list_head, _iter)
Iterate over the contents of a list, only one level.
Definition xlat_priv.h:220
static int xlat_exp_insert_tail(xlat_exp_head_t *head, xlat_exp_t *node)
Definition xlat_priv.h:236
static xlat_exp_t * xlat_exp_head(xlat_exp_head_t const *head)
Definition xlat_priv.h:207
An xlat expansion node.
Definition xlat_priv.h:151