The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
xlat_tokenize.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: f6ad9077e8c8a5cb08e97c642d62a5207b191122 $
19 *
20 * @file xlat_tokenize.c
21 * @brief String expansion ("translation"). Tokenizes xlat expansion strings.
22 *
23 * @copyright 2017-2021 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 * @copyright 2000 Alan DeKok (aland@freeradius.org)
25 * @copyright 2000,2006 The FreeRADIUS server project
26 */
27
28
29RCSID("$Id: f6ad9077e8c8a5cb08e97c642d62a5207b191122 $")
30
31#include <freeradius-devel/util/debug.h>
32#include <freeradius-devel/util/event.h>
33#include <freeradius-devel/util/value.h>
34#include <freeradius-devel/server/regex.h>
35#include <freeradius-devel/unlang/xlat_priv.h>
36
37#undef XLAT_DEBUG
38#undef XLAT_HEXDUMP
39#ifdef DEBUG_XLAT
40# define XLAT_DEBUG(_fmt, ...) DEBUG3("%s[%i] "_fmt, __FILE__, __LINE__, ##__VA_ARGS__)
41# define XLAT_HEXDUMP(_data, _len, _fmt, ...) HEXDUMP3(_data, _len, "%s[%i] "_fmt, __FILE__, __LINE__, ##__VA_ARGS__)
42#else
43# define XLAT_DEBUG(...)
44# define XLAT_HEXDUMP(...)
45#endif
46
47/** These rules apply to literal values and function arguments inside of an expansion
48 *
49 */
51 .name = "xlat",
52 .chr = '\\',
53 .subs = {
54 ['a'] = '\a',
55 ['b'] = '\b',
56 ['e'] = '\\', /* escape character, not \e */
57 ['n'] = '\n',
58 ['r'] = '\r',
59 ['t'] = '\t',
60 ['v'] = '\v',
61 ['\\'] = '\\',
62 ['%'] = '%', /* Expansion begin */
63 ['}'] = '}' /* Expansion end */
64 },
65 .do_hex = true,
66 .do_oct = true
67};
68
69/** These rules apply to literal values and function arguments inside of an expansion
70 *
71 */
73 .name = "xlat",
74 .chr = '\\',
75 .subs = {
76 ['\a'] = 'a',
77 ['\b'] = 'b',
78 ['\n'] = 'n',
79 ['\r'] = 'r',
80 ['\t'] = 't',
81 ['\v'] = 'v',
82 ['\\'] = '\\',
83 ['%'] = '%', /* Expansion begin */
84 ['}'] = '}' /* Expansion end */
85 },
86 .esc = {
89 },
90 .do_utf8 = true,
91 .do_oct = true
92};
93
94/** Parse rules for literal values inside of an expansion
95 *
96 * These rules are used to parse literals as arguments to functions.
97 *
98 * The caller sets the literal parse rules for outside of expansions when they
99 * call xlat_tokenize.
100 */
101static fr_sbuff_parse_rules_t const xlat_function_arg_rules = {
102 .escapes = &xlat_unescape,
103 .terminals = &FR_SBUFF_TERMS( /* These get merged with other literal terminals */
104 L(")"),
105 L(","),
106 ),
107};
108
109#ifdef HAVE_REGEX
110/** Parse an xlat reference
111 *
112 * Allows access to a subcapture groups
113 * @verbatim %{<num>} @endverbatim
114 */
116{
117 uint8_t num;
118 xlat_exp_t *node;
120
121 XLAT_DEBUG("REGEX <-- %.*s", (int) fr_sbuff_remaining(in), fr_sbuff_current(in));
122
123 /*
124 * Not a number, ignore it.
125 */
126 (void) fr_sbuff_out(&err, &num, in);
127 if (err != FR_SBUFF_PARSE_OK) return 0;
128
129 /*
130 * Not %{\d+}, ignore it.
131 */
132 if (!fr_sbuff_is_char(in, '}')) return 0;
133
134 /*
135 * It is a regex ref, but it has to be a valid one.
136 */
137 if (num > REQUEST_MAX_REGEX) {
138 fr_strerror_printf("Invalid regex reference. Must be in range 0-%d", REQUEST_MAX_REGEX);
139 fr_sbuff_set(in, m_s);
140 return -1;
141 }
142
143 MEM(node = xlat_exp_alloc(head, XLAT_REGEX, fr_sbuff_current(m_s), fr_sbuff_behind(m_s)));
144 node->regex_index = num;
145
146 *out = node;
147
148 (void) fr_sbuff_advance(in, 1); /* must be '}' */
149 return 1;
150}
151#endif
152
155 ['.'] = true, ['-'] = true, ['_'] = true,
156};
157
158
159/** Normalize an xlat which contains a tmpl.
160 *
161 * Constant data is turned into XLAT_BOX, and some other thingies are done.
162 */
164{
165 tmpl_t *vpt = node->vpt;
166
167 /*
168 * Any casting, etc. has to be taken care of in the xlat expression parser, and not here.
169 */
171
172 if (tmpl_is_attr_unresolved(node->vpt)) {
173 return 0;
174 }
175
176 /*
177 * Add in unknown attributes, by defining them in the local dictionary.
178 */
179 if (tmpl_is_attr(vpt)) {
180 if (tmpl_attr_unknown_add(vpt) < 0) {
181 fr_strerror_printf("Failed defining attribute %s", tmpl_attr_tail_da(vpt)->name);
182 return -1;
183 }
184
185 return 0;
186 }
187
188 if (!tmpl_contains_data(vpt)) {
190 return 0;
191 }
192
193 if (tmpl_is_data_unresolved(vpt) && (tmpl_resolve(vpt, NULL) < 0)) return -1;
194
195 /*
196 * Hoist data to an XLAT_BOX instead of an XLAT_TMPL
197 */
199
200 /*
201 * Print "true" and "false" instead of "yes" and "no".
202 */
205 }
206
207 /*
208 * Convert the XLAT_TMPL to XLAT_BOX
209 */
211
212 return 0;
213}
214
215/** Validate and sanity check function arguments.
216 *
217 */
218static int xlat_validate_function_arg(xlat_arg_parser_t const *arg_p, xlat_exp_t *arg, int argc)
219{
220 xlat_exp_t *node;
221
222 fr_assert(arg->type == XLAT_GROUP);
223
224 /*
225 * "is_argv" does dual duty. One, it causes xlat_print() to print spaces in between arguments.
226 *
227 * Two, it is checked by xlat_frame_eval_repeat(), which then does NOT concatenate strings in
228 * place. Instead, it just passes the strings though to xlat_process_arg_list(). Which calls
229 * xlat_arg_stringify(), and that does the escaping and final concatenation.
230 */
231 arg->group->is_argv = (arg_p->func != NULL) | arg_p->will_escape;
232
233 node = xlat_exp_head(arg->group);
234
235 if (!node) {
236 if (!arg_p->required) return 0;
237
238 fr_strerror_const("Missing argument");
239 return -1;
240 }
241
242 /*
243 * The caller doesn't care about the type, we don't do any validation.
244 */
245 if (arg_p->type == FR_TYPE_VOID) return 0;
246
247 /*
248 * A cursor should be (for now) a named string.
249 */
250 if (arg_p->type == FR_TYPE_PAIR_CURSOR) {
251 if (node->type == XLAT_BOX) {
252 check_box:
253 if (node->data.type != FR_TYPE_STRING) {
254 fr_strerror_printf("Cursor must be a string attribute reference, not %s",
255 fr_type_to_str(node->data.type));
256 return -1;
257 }
258
259 return 0;
260 }
261
262 /*
263 * The expression parser should not allow anything else here.
264 */
265 fr_assert((node->type == XLAT_TMPL) || (node->type == XLAT_GROUP));
266
267 /*
268 * Func, etc.
269 */
270 if (node->type != XLAT_TMPL) return 0;
271
272 if (tmpl_rules_cast(node->vpt) != FR_TYPE_NULL) {
273 fr_strerror_const("Cursor cannot have cast");
274 return -1;
275 }
276
277 if (xlat_tmpl_normalize(node) < 0) return -1;
278
279 if (node->type == XLAT_BOX) goto check_box;
280
281 if (!tmpl_is_attr(node->vpt)) {
282 fr_strerror_printf("Invalid argument - expected attribute reference");
283 return -1;
284 }
285
286 /*
287 * Bare attribute references are allowed, but are marked up as "return a cursor to this
288 * thing, don't return a value".
289 */
290 arg->group->cursor = true;
291 return 0;
292 }
293
294 /*
295 * An attribute argument results in an FR_TYPE_ATTR box, rather than the value of the attribute
296 */
297 if (arg_p->type == FR_TYPE_ATTR) {
298 if (node->type != XLAT_TMPL) {
299 fr_strerror_printf("Attribute must be a bare word");
300 return -1;
301 }
302
303 if (xlat_tmpl_normalize(node) < 0) return -1;
304
305 if (!tmpl_is_attr(node->vpt)) {
306 fr_strerror_printf("Invalid argument - expected attribute reference");
307 return -1;
308 }
309
310 arg->group->is_attr = true;
311 return 0;
312 }
313
314 /*
315 * The argument is either ONE tmpl / value-box, OR is an
316 * xlat group which contains a double-quoted string.
317 */
318 fr_assert(fr_dlist_num_elements(&arg->group->dlist) == 1);
319
320 /*
321 * Do at least somewhat of a pass of normalizing the nodes, even if there are more than one.
322 */
323 if (node->type == XLAT_TMPL) {
324 return xlat_tmpl_normalize(node);
325 }
326
327 /*
328 * @todo - probably move the double-quoted string "node->flags.constant" check here, to more
329 * clearly separate parsing from normalization.
330 */
331
332 if (node->type != XLAT_BOX) {
333 return 0;
334 }
335
336 /*
337 * If it's the correct data type, then we don't need to do anything.
338 */
339 if (arg_p->type == node->data.type) {
340 return 0;
341 }
342
343 /*
344 * An explicit `null` literal is preserved unchanged - the xlat
345 * body receives an FR_TYPE_NULL box in this arg slot and can
346 * decide what to do with it. Casting would collapse it into a
347 * zero-length value of the declared type and hide the intent.
348 *
349 * Callers that marked the arg `required = true` are asking for
350 * a concrete value, not a placeholder - reject the literal up
351 * front so the config error surfaces at startup rather than on
352 * the first request that hits this node.
353 */
354 if (fr_type_is_null(node->data.type)) {
355 if (arg_p->required) {
356 fr_strerror_printf("Invalid argument %d - `null` is not allowed for a required argument", argc);
357 return -1;
358 }
359 return 0;
360 }
361
362 /*
363 * Cast (or parse) the input data to the expected argument data type.
364 */
365 if (fr_value_box_cast_in_place(node, &node->data, arg_p->type, NULL) < 0) {
366 fr_strerror_printf("Invalid argument %d - %s", argc, fr_strerror());
367 return -1;
368 }
369
370 return 0;
371}
372
374{
375 xlat_arg_parser_t const *arg_p;
376 xlat_exp_t *arg = xlat_exp_head(node->call.args);
377 int i = 1;
378
379 fr_assert(node->type == XLAT_FUNC);
380
381 /*
382 * Check the function definition against what the user passed in.
383 */
384 if (!node->call.func->args) {
385 if (node->call.args) {
386 fr_strerror_const("Too many arguments to function call, expected 0");
387 return -1;
388 }
389
390 /*
391 * Function takes no arguments, and none were passed in. There's nothing to verify.
392 */
393 return 0;
394 }
395
396 if (!node->call.args) {
397 fr_strerror_const("Too few arguments to function call");
398 return -1;
399 }
400
401 /*
402 * The function both has arguments defined, and the user has supplied them.
403 */
404 for (arg_p = node->call.func->args, i = 0; arg_p->type != FR_TYPE_NULL; arg_p++) {
405 if (!arg) {
406 if (arg_p->required) {
407 fr_strerror_printf("Missing required argument %u",
408 (unsigned int)(arg_p - node->call.func->args) + 1);
409 return -1;
410 }
411
412 /*
413 * No arg and not required, we can stop.
414 *
415 * If there is an arg, we validate it, even if it isn't required.
416 */
417 break;
418 }
419
420 /*
421 * All arguments MUST be put into a group, even
422 * if they're just one element.
423 */
424 fr_assert(arg->type == XLAT_GROUP);
425
426 if (xlat_validate_function_arg(arg_p, arg, i) < 0) return -1;
427
428 arg = xlat_exp_next(node->call.args, arg);
429 i++;
430 }
431
432 /*
433 * @todo - check if there is a trailing argument. But for functions which take no arguments, the
434 * "arg" is an empty group.
435 */
436
437 return 0;
438}
439
440/** Parse an xlat function and its child argument
441 *
442 * Parses a function call string in the format
443 * @verbatim %<func>(<argument>) @endverbatim
444 *
445 * @return
446 * - 0 if the string was parsed into a function.
447 * - <0 on parse error.
448 */
450{
451 char c;
452 xlat_exp_t *node;
453 xlat_t *func;
455 tmpl_rules_t my_t_rules;
456
457 fr_sbuff_marker(&m_s, in);
458
460
461 /*
462 * The caller ensures that the first character after the percent exists, and is alphanumeric.
463 */
464 c = fr_sbuff_char(in, '\0');
465
466 /*
467 * Even if it is alphanumeric, only a limited set of characters are one-letter expansions.
468 *
469 * And even then only if the character after them is a terminal character.
470 */
471 if (strchr("cCdDeGHIlmMnSstTY", c) != NULL) {
472 uint8_t n;
473
474 fr_sbuff_next(in);
475
476 /*
477 * End of buffer == one letter expansion.
478 */
479 n = fr_sbuff_uint8(in, '\0');
480 if (!n) goto one_letter;
481
482 /*
483 * %Y() is the new format.
484 */
485 if (n == '(') {
486 fr_sbuff_next(in);
487
488 if (!fr_sbuff_next_if_char(in, ')')) {
489 fr_strerror_const("Missing closing brace ')'");
490 return -1;
491 }
492
493 goto one_letter;
494 }
495
496 /*
497 * %M. or %Y- is a one-letter expansion followed by the other character.
498 */
499 if (!sbuff_char_alpha_num[n]) {
500 one_letter:
501 XLAT_DEBUG("ONE-LETTER <-- %c", c);
503
504 xlat_exp_set_name(node, fr_sbuff_current(&m_s), 1);
505 xlat_exp_set_type(node, XLAT_ONE_LETTER); /* needs node->fmt to be set */
506
507 fr_sbuff_marker_release(&m_s);
508
509#ifdef STATIC_ANALYZER
510 if (!node->fmt) return -1;
511#endif
512
514 return 0;
515 }
516
517 /*
518 * Anything else, it must be a full function name.
519 */
520 fr_sbuff_set(in, &m_s);
521 }
522
524
526
527 if (!fr_sbuff_is_char(in, '(')) {
528 fr_strerror_printf("Missing '('");
529 return -1;
530 }
531
532 /*
533 * Check for failure.
534 */
535 if (!func && (!t_rules->attr.allow_unresolved|| t_rules->at_runtime)) {
536 fr_strerror_const("Unresolved expansion functions are not allowed here");
537 fr_sbuff_set(in, &m_s); /* backtrack */
538 fr_sbuff_marker_release(&m_s);
539 return -1;
540 }
541
542 /*
543 * Allocate a node to hold the function
544 */
546 if (!func) {
548
549 } else {
550 xlat_exp_set_func(node, func, t_rules->attr.dict_def);
551 }
552
553 fr_sbuff_marker_release(&m_s);
554
555 (void) fr_sbuff_next(in); /* skip the '(' */
556
557 /*
558 * The caller might want the _output_ cast to something. But that doesn't mean we cast each
559 * _argument_ to the xlat function.
560 */
561 if (t_rules->cast != FR_TYPE_NULL) {
562 my_t_rules = *t_rules;
563 my_t_rules.cast = FR_TYPE_NULL;
564 t_rules = &my_t_rules;
565 }
566
567 /*
568 * Now parse the child nodes that form the
569 * function's arguments.
570 */
571 if (xlat_tokenize_argv(node, &node->call.args, in, func ? func->args : NULL,
572 &xlat_function_arg_rules, t_rules, false) < 0) {
573 error:
574 talloc_free(node);
575 return -1;
576 }
577
578 if (!fr_sbuff_next_if_char(in, ')')) {
579 fr_strerror_const("Missing closing brace ')'");
580 goto error;
581 }
582
584
586 return 0;
587}
588
589/** Parse an attribute ref or a virtual attribute
590 *
591 */
593 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
594{
596 tmpl_t *vpt = NULL;
597 xlat_exp_t *node;
598
600 tmpl_rules_t our_t_rules;
601 fr_sbuff_t our_in = FR_SBUFF(in);
602
603 XLAT_DEBUG("ATTRIBUTE <-- %.*s", (int) fr_sbuff_remaining(in), fr_sbuff_current(in));
604
605 /*
606 * We are called from %{foo}. So we don't use attribute prefixes.
607 */
608 our_t_rules = *t_rules;
609 our_t_rules.attr.allow_wildcard = true;
610
611 fr_sbuff_marker(&m_s, in);
612
614 if (tmpl_afrom_attr_substr(node, &err, &vpt, &our_in, p_rules, &our_t_rules) < 0) {
615 /*
616 * If the parse error occurred before a terminator,
617 * then the error is changed to 'Unknown module',
618 * as it was more likely to be a bad module name,
619 * than a request qualifier.
620 */
622 error:
623 fr_sbuff_marker_release(&m_s);
624 talloc_free(node);
625 FR_SBUFF_ERROR_RETURN(&our_in);
626 }
627
628 /*
629 * Deal with unresolved attributes.
630 */
632 if (!t_rules->attr.allow_unresolved) {
634
635 fr_strerror_const("Unresolved attributes not allowed in expansions here");
636 fr_sbuff_set(&our_in, &m_s); /* Error at the start of the attribute */
637 goto error;
638 }
639 }
640
641 /*
642 * Deal with normal attribute (or list)
643 */
645 xlat_exp_set_vpt(node, vpt);
646
647 /*
648 * Remember that it was %{User-Name}
649 *
650 * This is a temporary hack until all of the unit tests
651 * pass without '&'.
652 */
653 UNCONST(tmpl_attr_rules_t *, &vpt->rules.attr)->xlat = true;
654
656
657 fr_sbuff_marker_release(&m_s);
658 return fr_sbuff_set(in, &our_in);
659}
660
663 ['-'] = true, ['/'] = true, ['_'] = true, // fr_dict_attr_allowed_chars
664 ['.'] = true, ['*'] = true, ['#'] = true,
665 ['['] = true, [']'] = true, // tmpls and attribute arrays
666};
667
669 tmpl_rules_t const *t_rules)
670{
671 size_t len;
672 int ret;
674 char hint;
675 fr_sbuff_term_t hint_tokens = FR_SBUFF_TERMS(
676 L(" "), /* First special token is a ' ' - Likely a syntax error */
677 L("["), /* First special token is a '[' i.e. '%{attr[<idx>]}' */
678 L("}") /* First special token is a '}' i.e. '%{<attrref>}' */
679 );
680
681 fr_sbuff_parse_rules_t attr_p_rules = {
682 .escapes = &xlat_unescape,
683 .terminals = &FR_SBUFF_TERM("}")
684 };
685#ifdef HAVE_REGEX
686 xlat_exp_t *node;
687#endif
688
689 XLAT_DEBUG("EXPANSION <-- %.*s", (int) fr_sbuff_remaining(in), fr_sbuff_current(in));
690
691 fr_sbuff_marker(&m_s, in);
692
693#ifdef HAVE_REGEX
694 ret = xlat_tokenize_regex(head, &node, in, &m_s);
695 if (ret < 0) return ret;
696
697 if (ret == 1) {
698 fr_assert(node != NULL);
700 return 0;
701 }
702
703 fr_sbuff_set(in, &m_s); /* backtrack to the start of the expression */
704#endif /* HAVE_REGEX */
705
706 /*
707 * See if it's an attribute reference, with possible array stuff.
708 */
710 if (fr_sbuff_is_char(in, '}')) {
711 if (!len) goto empty_disallowed;
712 goto check_for_attr;
713 }
714
715 if (!fr_sbuff_extend(in)) {
716 fr_strerror_const("Missing closing brace '}'");
717 fr_sbuff_marker_release(&m_s);
718 return -1;
719 }
720
721 /*
722 * It must be an expression.
723 *
724 * We wrap the xlat in a group, and then mark the group to be hoisted.
725 */
726 {
727 tmpl_rules_t my_rules;
728
729 fr_sbuff_set(in, &m_s); /* backtrack to the start of the expression */
730
731 MEM(node = xlat_exp_alloc(head, XLAT_GROUP, NULL, 0));
732
733 if (t_rules) {
734 my_rules = *t_rules;
735 my_rules.enumv = NULL;
736 my_rules.cast = FR_TYPE_NULL;
737 t_rules = &my_rules;
738 }
739
740 ret = xlat_tokenize_expression(node, &node->group, in, &attr_p_rules, t_rules);
741 if (ret <= 0) {
742 talloc_free(node);
743 return ret;
744 }
745
746 if (!fr_sbuff_is_char(in, '}')) {
747 talloc_free(node);
748 fr_strerror_const("Missing closing brace '}'");
749 return -1;
750 }
751
753 node->flags = node->group->flags;
754
755 /*
756 * Print it as %{...}. Then when we're evaluating a string, hoist the results.
757 */
758 node->flags.xlat = true;
759 node->hoist = true;
760
762
763 (void) fr_sbuff_next(in); /* skip '}' */
764 return ret;
765 }
766
767check_for_attr:
768 fr_sbuff_set(in, &m_s); /* backtrack */
769
770 /*
771 * %{Attr-Name}
772 * %{Attr-Name[#]}
773 * %{request.Attr-Name}
774 */
775
776 /*
777 * Check for empty expressions %{} %{: %{[
778 */
779 fr_sbuff_marker(&m_s, in);
780 len = fr_sbuff_adv_until(in, SIZE_MAX, &hint_tokens, '\0');
781
782 /*
783 * This means the end of a string not containing any of the other
784 * tokens was reached.
785 *
786 * e.g. '%{myfirstxlat'
787 */
788 if (!fr_sbuff_extend(in)) {
789 fr_strerror_const("Missing closing brace '}'");
790 fr_sbuff_marker_release(&m_s);
791 return -1;
792 }
793
794 hint = fr_sbuff_char(in, '\0');
795
796 XLAT_DEBUG("EXPANSION HINT TOKEN '%c'", hint);
797 if (len == 0) {
798 switch (hint) {
799 case '}':
800 empty_disallowed:
801 fr_strerror_const("Empty expressions are invalid");
802 return -1;
803
804 case '[':
805 fr_strerror_const("Missing attribute name");
806 return -1;
807
808 default:
809 break;
810 }
811 }
812
813 switch (hint) {
814 /*
815 * Hint token is a:
816 * - '[' - Which is an attribute index, so it must be an attribute.
817 * - '}' - The end of the expansion, which means it was a bareword.
818 */
819 case '.':
820 case '}':
821 case '[':
822 fr_sbuff_set(in, &m_s); /* backtrack */
823 fr_sbuff_marker_release(&m_s);
824
825 if (xlat_tokenize_attribute(head, in, &attr_p_rules, t_rules) < 0) return -1;
826
827 if (!fr_sbuff_next_if_char(in, '}')) {
828 fr_strerror_const("Missing closing brace '}'");
829 return -1;
830 }
831
832 return 0;
833
834 /*
835 * Hint token was whitespace
836 *
837 * e.g. '%{my '
838 */
839 default:
840 break;
841 }
842
843 /*
844 * Box print is so we get \t \n etc..
845 */
846 fr_strerror_printf("Invalid char '%pV' in expression", fr_box_strvalue_len(fr_sbuff_current(in), 1));
847 return -1;
848}
849
850/** Parse an xlat string i.e. a non-expansion or non-function
851 *
852 * When this function is being used outside of an xlat expansion, i.e. on a string
853 * which contains one or more xlat expansions, it uses the terminal grammar and
854 * escaping rules of that string type.
855 *
856 * Which this function is being used inside of an xlat expansion, it uses xlat specific
857 * terminal grammar and escaping rules.
858 *
859 * This allows us to be smart about processing quotes within the expansions themselves.
860 *
861 * @param[out] head to allocate nodes in, and where to write the first
862 * child, and where the flags are stored.
863 * @param[in] in sbuff to parse.
864 * @param[in] p_rules that control parsing.
865 * @param[in] t_rules that control attribute reference and xlat function parsing.
866 * @return
867 * - <0 on failure
868 * - >=0 for number of bytes parsed
869 */
871 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
872{
873 xlat_exp_t *node = NULL;
874 xlat_exp_t *prev = NULL;
875 fr_slen_t slen;
877 L("%"),
878 );
879 fr_sbuff_term_t *tokens;
881 fr_sbuff_t our_in = FR_SBUFF(in);
882
883 XLAT_DEBUG("STRING <-- %.*s", (int) fr_sbuff_remaining(in), fr_sbuff_current(in));
884
885 escapes = p_rules ? p_rules->escapes : NULL;
886 tokens = p_rules && p_rules->terminals ?
887 fr_sbuff_terminals_amerge(NULL, p_rules->terminals, &terminals) : &terminals;
888
889 for (;;) {
890 char *str;
892
893 /*
894 * pre-allocate the node so we don't have to steal it later.
895 */
896 node = xlat_exp_alloc(head, XLAT_BOX, NULL, 0);
897
898 /*
899 * Find the next token
900 */
901 skip_alloc:
902 fr_sbuff_marker(&m_s, &our_in);
903 slen = fr_sbuff_out_aunescape_until(node, &str, &our_in, SIZE_MAX, tokens, escapes);
904
905 if (slen < 0) {
906 error:
907 talloc_free(node);
908
909 /*
910 * Free our temporary array of terminals
911 */
912 if (tokens != &terminals) talloc_free(tokens);
913 fr_sbuff_marker_release(&m_s);
914 FR_SBUFF_ERROR_RETURN(&our_in);
915 }
916
917 /*
918 * It's a value box, create an appropriate node
919 */
920 if (slen > 0) {
921 do_value_box:
922 /*
923 * If the previous node was also a constant value-box, we can merge the new
924 * string into it instead of inserting a fresh node. This merge ensures that we
925 * only have one constant value-box produced, instead of many.
926 */
927 if (prev && (prev->type == XLAT_BOX) && (prev->data.type == FR_TYPE_STRING)) {
928 size_t prev_len = prev->data.vb_length;
929 size_t add_len = talloc_strlen(str);
930 size_t total = prev_len + add_len;
931 char *merged;
932
933 MEM(fr_value_box_bstr_realloc(prev, &merged, &prev->data, total) == 0);
934 memcpy(merged + prev_len, str, add_len + 1);
935
936 xlat_exp_set_name(prev, merged, total);
937 talloc_free(str);
938
939 XLAT_DEBUG("VALUE-BOX merged --> %s", prev->fmt);
940
941 fr_sbuff_marker_release(&m_s);
942
943 /*
944 * Keep "node", as we haven't used it.
945 */
946 goto skip_alloc;
947 }
948
949 xlat_exp_set_name_shallow(node, str);
950 fr_value_box_bstrndup(node, &node->data, NULL, str, talloc_strlen(str), false);
951 fr_value_box_mark_safe_for(&node->data, t_rules->literals_safe_for);
952
953 if (!escapes) {
954 XLAT_DEBUG("VALUE-BOX %s <-- %.*s", str,
955 (int) fr_sbuff_behind(&m_s), fr_sbuff_current(&m_s));
956 } else {
957 XLAT_DEBUG("VALUE-BOX (%s) %s <-- %.*s", escapes->name, str,
958 (int) fr_sbuff_behind(&m_s), fr_sbuff_current(&m_s));
959 }
960 XLAT_HEXDUMP((uint8_t const *)str, talloc_strlen(str), " VALUE-BOX ");
961
963
964 prev = node;
965 node = NULL;
966 fr_sbuff_marker_release(&m_s);
967 continue;
968 }
969
970 /*
971 * We have parsed as much as we can as unescaped
972 * input. Either some text (and added the node
973 * to the list), or zero text. We now try to
974 * parse '%' expansions.
975 */
976
977 /*
978 * Attribute, function call, or other expansion.
979 */
980 if (fr_sbuff_adv_past_str_literal(&our_in, "%{")) {
981 TALLOC_FREE(node); /* nope, couldn't use it */
982
983 if (xlat_tokenize_expansion(head, &our_in, t_rules) < 0) goto error;
984
985 if (fr_sbuff_is_str_literal(&our_in, ":-")) {
986 fr_strerror_const("Old style alternation of %{...:-...} is no longer supported");
987 goto error;
988 }
989
990 prev = NULL; /* non-value-box inserted; subsequent text must not merge */
991
992 next:
993 fr_sbuff_marker_release(&m_s);
994 continue;
995 }
996
997 /*
998 * More migration hacks: allow %foo(...)
999 */
1000 if (fr_sbuff_next_if_char(&our_in, '%')) {
1001 /*
1002 * % non-alphanumeric, create a value-box for just the "%" character.
1003 */
1004 if (!fr_sbuff_is_alnum(&our_in)) {
1005 if (fr_sbuff_next_if_char(&our_in, '%')) { /* nothing */ }
1006
1007 str = talloc_strdup(node, "%");
1008 goto do_value_box;
1009 }
1010
1011 TALLOC_FREE(node); /* nope, couldn't use it */
1012
1013 /*
1014 * Tokenize the function arguments using the new method.
1015 */
1016 if (xlat_tokenize_function_args(head, &our_in, t_rules) < 0) goto error;
1017 prev = NULL; /* non-value-box inserted; subsequent text must not merge */
1018 goto next;
1019 }
1020
1021 /*
1022 * Nothing we recognize. Just return nothing.
1023 */
1024 TALLOC_FREE(node);
1025 XLAT_DEBUG("VALUE-BOX <-- (empty)");
1026 fr_sbuff_marker_release(&m_s);
1027 break;
1028 }
1029
1030 /*
1031 * Free our temporary array of terminals
1032 */
1033 if (tokens != &terminals) talloc_free(tokens);
1034
1035 return fr_sbuff_set(in, &our_in);
1036}
1037
1039 { L("\""), T_DOUBLE_QUOTED_STRING }, /* Don't re-order, backslash throws off ordering */
1040 { L("'"), T_SINGLE_QUOTED_STRING },
1041 { L("`"), T_BACK_QUOTED_STRING }
1042};
1044
1045#define INFO_INDENT(_fmt, ...) INFO("%*s"_fmt, depth * 2, " ", ## __VA_ARGS__)
1046
1047static void _xlat_debug_head(xlat_exp_head_t const *head, int depth);
1048static void _xlat_debug_node(xlat_exp_t const *node, int depth, bool print_flags)
1049{
1050 INFO_INDENT("{ -- %s", node->fmt);
1051#ifndef NDEBUG
1052// INFO_INDENT(" %s:%d", node->file, node->line);
1053#endif
1054
1055 if (print_flags) {
1056 INFO_INDENT("flags = %s %s %s %s %s",
1057 node->flags.needs_resolving ? "need_resolving" : "",
1058 node->flags.pure ? "pure" : "",
1059 node->flags.can_purify ? "can_purify" : "",
1060 node->flags.constant ? "constant" : "",
1061 node->flags.xlat ? "xlat" : "");
1062 }
1063
1064 depth++;
1065
1066 if (node->quote != T_BARE_WORD) INFO_INDENT("quote = %c", fr_token_quote[node->quote]);
1067
1068 switch (node->type) {
1069 case XLAT_BOX:
1070 INFO_INDENT("value %s --> %pV", fr_type_to_str(node->data.type), &node->data);
1071 break;
1072
1073 case XLAT_GROUP:
1074 INFO_INDENT("group");
1075 INFO_INDENT("{");
1076 _xlat_debug_head(node->group, depth + 1);
1077 INFO_INDENT("}");
1078 break;
1079
1080 case XLAT_ONE_LETTER:
1081 INFO_INDENT("percent (%c)", node->fmt[0]);
1082 break;
1083
1084 case XLAT_TMPL:
1085 {
1086 if (tmpl_cast_get(node->vpt) != FR_TYPE_NULL) {
1087 INFO_INDENT("cast (%s)", fr_type_to_str(tmpl_cast_get(node->vpt)));
1088 }
1089
1090 if (tmpl_is_attr(node->vpt)) {
1091 fr_assert(!node->flags.pure);
1092 if (tmpl_attr_tail_da(node->vpt)) INFO_INDENT("tmpl attribute (%s)", tmpl_attr_tail_da(node->vpt)->name);
1093 if (tmpl_attr_tail_num(node->vpt) != NUM_UNSPEC) {
1094 FR_DLIST_HEAD(tmpl_request_list) const *list;
1095 tmpl_request_t *rr = NULL;
1096
1097 INFO_INDENT("{");
1098
1099 /*
1100 * Loop over the request references
1101 */
1102 list = tmpl_request(node->vpt);
1103 while ((rr = tmpl_request_list_next(list, rr))) {
1104 INFO_INDENT("ref %u", rr->request);
1105 }
1106 INFO_INDENT("list %s", tmpl_list_name(tmpl_list(node->vpt), "<INVALID>"));
1107 if (tmpl_attr_tail_num(node->vpt) != NUM_UNSPEC) {
1108 if (tmpl_attr_tail_num(node->vpt) == NUM_COUNT) {
1109 INFO_INDENT("[#]");
1110 } else if (tmpl_attr_tail_num(node->vpt) == NUM_ALL) {
1111 INFO_INDENT("[*]");
1112 } else {
1113 INFO_INDENT("[%d]", tmpl_attr_tail_num(node->vpt));
1114 }
1115 }
1116 INFO_INDENT("}");
1117 }
1118 } else if (tmpl_is_data(node->vpt)) {
1119 INFO_INDENT("tmpl (%s) type %s", node->fmt, fr_type_to_str(tmpl_value_type(node->vpt)));
1120
1121 } else if (tmpl_is_xlat(node->vpt)) {
1122 INFO_INDENT("tmpl xlat (%s)", node->fmt);
1123 _xlat_debug_head(tmpl_xlat(node->vpt), depth + 1);
1124
1125 } else {
1126 INFO_INDENT("tmpl (%s)", node->fmt);
1127 }
1128 }
1129 break;
1130
1131 case XLAT_FUNC:
1132 fr_assert(node->call.func != NULL);
1133 INFO_INDENT("func (%s)", node->call.func->name);
1134 if (xlat_exp_head(node->call.args)) {
1135 INFO_INDENT("{");
1136 _xlat_debug_head(node->call.args, depth + 1);
1137 INFO_INDENT("}");
1138 }
1139 break;
1140
1142 INFO_INDENT("func-unresolved (%s)", node->fmt);
1143 if (xlat_exp_head(node->call.args)) {
1144 INFO_INDENT("{");
1145 _xlat_debug_head(node->call.args, depth + 1);
1146 INFO_INDENT("}");
1147 }
1148 break;
1149
1150#ifdef HAVE_REGEX
1151 case XLAT_REGEX:
1152 INFO_INDENT("regex-var -- %d", node->regex_index);
1153 break;
1154#endif
1155
1156 case XLAT_INVALID:
1157 DEBUG("XLAT-INVALID");
1158 break;
1159 }
1160
1161 depth--;
1162 INFO_INDENT("}");
1163}
1164
1165void xlat_debug(xlat_exp_t const *node)
1166{
1167 _xlat_debug_node(node, 0, true);
1168}
1169
1171{
1172 int i = 0;
1173
1174 fr_assert(head != NULL);
1175
1176 INFO_INDENT("head flags = %s %s %s %s %s",
1177 head->flags.needs_resolving ? "need_resolving," : "",
1178 head->flags.pure ? "pure" : "",
1179 head->flags.can_purify ? "can_purify" : "",
1180 head->flags.constant ? "constant" : "",
1181 head->flags.xlat ? "xlat" : "");
1182
1183 depth++;
1184
1185 xlat_exp_foreach(head, node) {
1186 INFO_INDENT("[%d] flags = %s %s %s %s %s", i++,
1187 node->flags.needs_resolving ? "need_resolving" : "",
1188 node->flags.pure ? "pure" : "",
1189 node->flags.can_purify ? "can_purify" : "",
1190 node->flags.constant ? "constant" : "",
1191 node->flags.xlat ? "xlat" : "");
1192
1193 _xlat_debug_node(node, depth, false);
1194 }
1195}
1196
1198{
1200}
1201
1203 fr_sbuff_escape_rules_t const *e_rules, char c)
1204{
1205 ssize_t slen;
1206 size_t at_in = fr_sbuff_used_total(out);
1207 char close;
1208
1209 if (!node) return 0;
1210
1211 if (node->flags.xlat) FR_SBUFF_IN_CHAR_RETURN(out, '%', '{');
1212
1213 switch (node->type) {
1214 case XLAT_GROUP:
1216 xlat_print(out, node->group, fr_value_escape_by_quote[node->quote]);
1218
1219 if (xlat_exp_next(head, node)) {
1220 if (c) FR_SBUFF_IN_CHAR_RETURN(out, c);
1221
1222 if (head->is_argv) FR_SBUFF_IN_CHAR_RETURN(out, ' '); /* Add ' ' between args */
1223 }
1224 goto done;
1225
1226 case XLAT_BOX:
1227 /*
1228 * @todo - respect node->quote here, too. Which also means updating the parser.
1229 */
1230 if (node->quote == T_BARE_WORD) {
1231 if (node->data.enumv &&
1232 (strncmp(node->fmt, "::", 2) == 0)) {
1234 }
1235
1236 FR_SBUFF_RETURN(fr_value_box_print, out, &node->data, e_rules);
1237 } else {
1238 FR_SBUFF_RETURN(fr_value_box_print_quoted, out, &node->data, node->quote);
1239 }
1240 goto done;
1241
1242 case XLAT_TMPL:
1243 if (node->vpt->rules.cast != FR_TYPE_NULL) {
1245 FR_SBUFF_IN_STRCPY_RETURN(out, fr_type_to_str(node->vpt->rules.cast));
1247 }
1248
1249 if (tmpl_is_data(node->vpt)) {
1250 /*
1251 * Manually add enum prefix when printing.
1252 */
1253 if (node->vpt->data.literal.enumv &&
1254 ((node->vpt->data.literal.type != FR_TYPE_BOOL) || da_is_bit_field(node->vpt->data.literal.enumv)) &&
1255 (strncmp(node->fmt, "::", 2) == 0)) {
1256 FR_SBUFF_IN_CHAR_RETURN(out, ':', ':');
1257 }
1259 goto done;
1260 }
1261 if (tmpl_needs_resolving(node->vpt)) {
1262 if (node->vpt->quote != T_BARE_WORD) {
1264 }
1265 FR_SBUFF_IN_STRCPY_RETURN(out, node->vpt->name); /* @todo - escape it? */
1266 if (node->vpt->quote != T_BARE_WORD) {
1268 }
1269 goto done;
1270 }
1271
1272 if (tmpl_contains_xlat(node->vpt)) { /* xlat and exec */
1273 if (node->vpt->quote == T_BARE_WORD) {
1274 xlat_print(out, tmpl_xlat(node->vpt), NULL);
1275 } else {
1279 }
1280 goto done;
1281 }
1282
1283 /*
1284 * Regexes need their own print routine, as they need to print the flags, too.
1285 *
1286 * Regexes should also "eat" their arguments into their instance data, so that we should
1287 * never try to print a regex.
1288 */
1289 fr_assert(!tmpl_contains_regex(node->vpt));
1290
1291 // attr or list
1292 fr_assert(tmpl_is_attr(node->vpt));
1293 fr_assert(talloc_parent(node->vpt) == node);
1294 fr_assert(!node->flags.pure);
1295
1296 /*
1297 * No '&', print the name, BUT without any attribute prefix.
1298 */
1299 if (!node->vpt->rules.attr.xlat) {
1300 char const *p = node->fmt;
1301
1302 if (*p == '&') p++;
1303
1305 goto done;
1306 }
1307 break;
1308
1309 case XLAT_ONE_LETTER:
1310 FR_SBUFF_IN_CHAR_RETURN(out, '%', node->fmt[0]);
1311 goto done;
1312
1313 case XLAT_FUNC:
1314 /*
1315 * We have a callback for printing this node, go
1316 * call it.
1317 */
1318 if (node->call.func->print) {
1319 slen = node->call.func->print(out, node, node->call.inst ? node->call.inst->data : NULL, e_rules);
1320 if (slen < 0) return slen;
1321 goto done;
1322 }
1323 break;
1324
1325 default:
1326 break;
1327 }
1328
1329 /*
1330 * Now print %(...) or %{...}
1331 */
1332 if ((node->type == XLAT_FUNC) || (node->type == XLAT_FUNC_UNRESOLVED)) {
1333 FR_SBUFF_IN_CHAR_RETURN(out, '%'); /* then the name */
1334 close = ')';
1335 } else {
1337 close = '}';
1338 }
1339
1340 switch (node->type) {
1341 case XLAT_TMPL:
1342 slen = tmpl_attr_print(out, node->vpt);
1343 if (slen < 0) return slen;
1344 break;
1345
1346#ifdef HAVE_REGEX
1347 case XLAT_REGEX:
1348 FR_SBUFF_IN_SPRINTF_RETURN(out, "%i", node->regex_index);
1349 break;
1350#endif
1351
1352 case XLAT_FUNC:
1353 FR_SBUFF_IN_BSTRCPY_BUFFER_RETURN(out, node->call.func->name);
1355
1356 goto print_args;
1357
1361
1362 print_args:
1363 if (xlat_exp_head(node->call.args)) {
1364 xlat_exp_foreach(node->call.args, child) {
1365 slen = xlat_print_node(out, node->call.args, child, &xlat_escape, ',');
1366 if (slen < 0) return slen;
1367 }
1368 }
1369 break;
1370
1371 case XLAT_INVALID:
1372 case XLAT_BOX:
1373 case XLAT_ONE_LETTER:
1374 case XLAT_GROUP:
1375 fr_assert_fail(NULL);
1376 break;
1377 }
1379
1380done:
1381 if (node->flags.xlat) FR_SBUFF_IN_CHAR_RETURN(out, '}');
1382
1383 return fr_sbuff_used_total(out) - at_in;
1384}
1385
1386/** Reconstitute an xlat expression from its constituent nodes
1387 *
1388 * @param[in] out Where to write the output string.
1389 * @param[in] head First node to print.
1390 * @param[in] e_rules Specifying how to escape literal values.
1391 */
1393{
1394 ssize_t slen;
1395 size_t at_in = fr_sbuff_used_total(out);
1396
1397 xlat_exp_foreach(head, node) {
1398 slen = xlat_print_node(out, head, node, e_rules, 0);
1399 if (slen < 0) {
1400 /* coverity[return_overflow] */
1401 return slen - (fr_sbuff_used_total(out) - at_in);
1402 }
1403 }
1404
1405 return fr_sbuff_used_total(out) - at_in;
1406}
1407
1408#if 0
1409static void xlat_safe_for(xlat_exp_head_t *head, fr_value_box_safe_for_t safe_for)
1410{
1411 xlat_exp_foreach(head, node) {
1412 switch (node->type) {
1413 case XLAT_BOX:
1414 if (node->data.safe_for != safe_for) {
1415 ERROR("FAILED %lx %lx - %s", node->data.safe_for, safe_for, node->fmt);
1416 }
1417 fr_assert(node->data.safe_for == safe_for);
1418 break;
1419
1420 case XLAT_GROUP:
1421 xlat_safe_for(node->group, safe_for);
1422 break;
1423
1424 case XLAT_TMPL:
1425 if (!tmpl_is_xlat(node->vpt)) break;
1426
1427 xlat_safe_for(tmpl_xlat(node->vpt), safe_for);
1428 break;
1429
1430 default:
1431 break;
1432 }
1433 }
1434}
1435#endif
1436
1437
1439 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
1440{
1441 int triple = 1;
1442 ssize_t slen;
1443 fr_sbuff_t our_in = FR_SBUFF(in);
1444 xlat_exp_t *node;
1446
1447 /*
1448 * Triple-quoted strings have different terminal conditions.
1449 */
1450 switch (quote) {
1452 fr_strerror_const("Unexpected regular expression");
1453 fr_sbuff_advance(in, -1); /* to the actual '/' */
1454 our_in = FR_SBUFF(in);
1455 FR_SBUFF_ERROR_RETURN(&our_in);
1456
1457 default:
1458 fr_assert(0);
1459 FR_SBUFF_ERROR_RETURN(&our_in);
1460
1461 case T_BARE_WORD:
1462#ifdef HAVE_REGEX
1463 fr_sbuff_marker(&m, &our_in);
1464
1465 /*
1466 * Regular expression expansions are %{...}
1467 */
1468 if (fr_sbuff_adv_past_str_literal(&our_in, "%{")) {
1469 int ret;
1471
1472 fr_sbuff_marker(&m_s, &our_in);
1473
1474 ret = xlat_tokenize_regex(ctx, &node, &our_in, &m_s);
1475 if (ret < 0) FR_SBUFF_ERROR_RETURN(&our_in);
1476
1477 if (ret == 1) goto done;
1478
1479 fr_sbuff_set(&our_in, &m);
1480 }
1481#endif /* HAVE_REGEX */
1482
1483#if 0
1484 /*
1485 * Avoid a bounce through tmpls for %{...} and %func()
1486 *
1487 * @todo %{...} --> tokenize expression
1488 * %foo(..) --> tokenize_function_args (and have that function look for ()
1489 * %Y or %Y() --> one letter
1490 */
1491 if (fr_sbuff_is_char(&our_in, '%')) {
1492 xlat_exp_head_t *head = NULL;
1493
1495
1496 slen = xlat_tokenize_input(head, &our_in, p_rules, t_rules);
1497 if (slen <= 0) {
1499 FR_SBUFF_ERROR_RETURN(&our_in);
1500 }
1501
1502 fr_assert(fr_dlist_num_elements(&head->dlist) == 1);
1503
1504 node = fr_dlist_pop_head(&head->dlist);
1505 fr_assert(node != NULL);
1506 (void) talloc_steal(ctx, node);
1508 goto done;
1509 }
1510#endif
1511 break;
1512
1516 p_rules = value_parse_rules_quoted[quote];
1517
1518 if (fr_sbuff_remaining(&our_in) >= 2) {
1519 char const *p = fr_sbuff_current(&our_in);
1520 char c = fr_token_quote[quote];
1521
1522 /*
1523 * """foo "quote" and end"""
1524 */
1525 if ((p[0] == c) && (p[1] == c)) {
1526 triple = 3;
1527 (void) fr_sbuff_advance(&our_in, 2);
1528 p_rules = value_parse_rules_3quoted[quote];
1529 }
1530 }
1531 break;
1532 }
1533
1534 switch (quote) {
1535 /*
1536 * `foo` is a tmpl, and is NOT a group.
1537 */
1539 case T_BARE_WORD:
1540 MEM(node = xlat_exp_alloc(ctx, XLAT_TMPL, NULL, 0));
1541 node->quote = quote;
1542
1543 /*
1544 * tmpl_afrom_substr does pretty much all the work of
1545 * parsing the operand. It pays attention to the cast on
1546 * our_t_rules, and will try to parse any data there as
1547 * of the correct type.
1548 */
1549 slen = tmpl_afrom_substr(node, &node->vpt, &our_in, quote, p_rules, t_rules);
1550 if (slen <= 0) {
1551 fr_sbuff_advance(&our_in, -slen - 1); /* point to the correct offset */
1552
1553 error:
1554 talloc_free(node);
1555 FR_SBUFF_ERROR_RETURN(&our_in);
1556 }
1557 xlat_exp_set_vpt(node, node->vpt); /* sets flags */
1558
1559 if (xlat_tmpl_normalize(node) < 0) goto error;
1560
1561 if (quote == T_BARE_WORD) goto done;
1562
1563 break; /* exec - look for closing quote */
1564
1565 /*
1566 * "Double quoted strings may contain %{expansions}"
1567 */
1569 MEM(node = xlat_exp_alloc(ctx, XLAT_GROUP, NULL, 0));
1570 node->quote = quote;
1571
1572 fr_sbuff_marker(&m, &our_in);
1573 XLAT_DEBUG("ARGV double quotes <-- %.*s", (int) fr_sbuff_remaining(&our_in), fr_sbuff_current(&our_in));
1574
1575 if (xlat_tokenize_input(node->group, &our_in, p_rules, t_rules) < 0) goto error;
1576
1577 node->flags = node->group->flags;
1578 node->hoist = true;
1580
1581 /*
1582 * There's no expansion in the string. Hoist the value-box where we can.
1583 */
1584 if (node->flags.constant) {
1585 size_t num = fr_dlist_num_elements(&node->group->dlist);
1586
1587 /*
1588 * Empty string: convert the wrapper to an empty XLAT_BOX.
1589 *
1590 * Exactly one child of type XLAT_BOX: hoist the child up in place of the
1591 * wrapper.
1592 *
1593 * Anything else (multiple constant children, or a single non-box child like
1594 * a hoisted %{(const)} expression result) is left wrapped in an XLAT_GROUP.
1595 * The hoist flag on the GROUP makes the runtime concatenate the children's
1596 * stringified values at eval time, which is the correct semantics for cases
1597 * like "x%{(1)}y". The integer 1 still needs to be stringified before it
1598 * can be merged into the surrounding literal text.
1599 */
1600 if (num == 0) {
1602
1603 fr_value_box_init(&node->data, FR_TYPE_STRING, NULL, false);
1604 fr_value_box_strdup(node, &node->data, NULL, "", false);
1605
1606 fr_assert(node->type == XLAT_BOX);
1607 node->quote = quote;
1608
1609 } else if (num == 1) {
1610 xlat_exp_t *child = xlat_exp_head(node->group);
1611
1612 if (child->type == XLAT_BOX) {
1613 (void) talloc_steal(ctx, child);
1614 talloc_free(node);
1615 node = child;
1616 node->quote = quote; /* not the same node! */
1617 } /* else there are single non-box constant child, leave the wrapper alone */
1618 } /* else there are multiple constant children, leave the wrapper alone */
1619 }
1620 break;
1621
1622 /*
1623 * 'Single quoted strings get parsed as literal strings'
1624 */
1626 {
1627 char *str;
1628
1629 XLAT_DEBUG("ARGV single quotes <-- %.*s", (int) fr_sbuff_remaining(&our_in), fr_sbuff_current(&our_in));
1630
1631 node = xlat_exp_alloc(ctx, XLAT_BOX, NULL, 0);
1632 node->quote = quote;
1633
1634 slen = fr_sbuff_out_aunescape_until(node, &str, &our_in, SIZE_MAX, p_rules->terminals, p_rules->escapes);
1635 if (slen < 0) goto error;
1636
1637 xlat_exp_set_name_shallow(node, str);
1638 fr_value_box_strdup(node, &node->data, NULL, str, false);
1639 fr_value_box_mark_safe_for(&node->data, t_rules->literals_safe_for); /* Literal values are treated as implicitly safe */
1640 }
1641 break;
1642
1643 default:
1644 fr_strerror_const("Internal sanity check failed in tokenizing expansion word");
1645 FR_SBUFF_ERROR_RETURN(&our_in);
1646 }
1647
1648 /*
1649 * Ensure that the string ends with the correct number of quotes.
1650 */
1651 do {
1652 if (!fr_sbuff_is_char(&our_in, fr_token_quote[quote])) {
1653 fr_strerror_const("Unterminated string");
1654 fr_sbuff_set_to_start(&our_in);
1655 goto error;
1656 }
1657
1658 fr_sbuff_advance(&our_in, 1);
1659 } while (--triple > 0);
1660
1661done:
1662 *out = node;
1663
1664 FR_SBUFF_SET_RETURN(in, &our_in);
1665}
1666
1667/** Tokenize an xlat expansion into a series of XLAT_TYPE_CHILD arguments
1668 *
1669 * @param[in] ctx to allocate nodes in. Note: All nodes will be
1670 * allocated in the same ctx. This is to allow
1671 * manipulation by xlat instantiation functions
1672 * later.
1673 * @param[out] out the head of the xlat list / tree structure.
1674 * @param[in] in the format string to expand.
1675 * @param[in] xlat_args the arguments
1676 * @param[in] p_rules controlling how to parse the string outside of
1677 * any expansions.
1678 * @param[in] t_rules controlling how attribute references are parsed.
1679 * @param[in] spaces whether the arguments are delimited by spaces
1680 * @return
1681 * - < 0 on error.
1682 * - >0 on success which is the number of characters parsed.
1683 */
1685 xlat_arg_parser_t const *xlat_args,
1686 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool spaces)
1687{
1688 int argc;
1689 fr_sbuff_t our_in = FR_SBUFF(in);
1690 ssize_t slen;
1692 fr_sbuff_parse_rules_t const *our_p_rules; /* Bareword parse rules */
1693 fr_sbuff_parse_rules_t tmp_p_rules;
1695 xlat_arg_parser_t const *arg = NULL, *arg_start;
1696 tmpl_rules_t arg_t_rules;
1697
1698 if (xlat_args) {
1699 arg_start = arg = xlat_args; /* Track the arguments as we parse */
1700 } else {
1701 static xlat_arg_parser_t const default_arg[] = { { .variadic = XLAT_ARG_VARIADIC_EMPTY_SQUASH, .type = FR_TYPE_VOID },
1703 arg_start = arg = &default_arg[0];
1704 }
1705 arg_t_rules = *t_rules;
1706
1707 if (unlikely(spaces)) {
1709 if (p_rules) { /* only for tmpl_tokenize, and back-ticks */
1710 fr_assert(p_rules->terminals);
1711
1712 tmp_p_rules = (fr_sbuff_parse_rules_t){ /* Stack allocated due to CL scope */
1713 .terminals = fr_sbuff_terminals_amerge(NULL, p_rules->terminals,
1715 .escapes = (p_rules->escapes ? p_rules->escapes : value_parse_rules_bareword_quoted.escapes)
1716 };
1717 our_p_rules = &tmp_p_rules;
1718 } else {
1719 our_p_rules = &value_parse_rules_bareword_quoted;
1720 }
1721
1722 } else {
1723 if (!p_rules) {
1724 p_rules = &xlat_function_arg_rules;
1725 } else {
1727 }
1728 fr_assert(p_rules->terminals);
1729
1730 our_p_rules = p_rules;
1731
1732 /*
1733 * The arguments to a function are NOT the output data type of the function.
1734 *
1735 * We do NOT check for quotation characters. We DO update t_rules to strip any casts. The
1736 * OUTPUT of the function is cast to the relevant data type, but each ARGUMENT is just an
1737 * expression with no given data type. Parsing the expression is NOT done with the cast of
1738 * arg->type, as that means each individual piece of the expression is parsed as the type. We
1739 * have to cast on the final _output_ of the expression, and we allow the _input_ pieces of the
1740 * expression to be just about anything.
1741 */
1742 arg_t_rules.enumv = NULL;
1743 arg_t_rules.cast = FR_TYPE_NULL;
1744 arg_t_rules.attr.namespace = NULL;
1745 arg_t_rules.attr.request_def = NULL;
1746 arg_t_rules.attr.list_def = request_attr_request;
1748 }
1749
1751
1752 /*
1753 * skip spaces at the beginning as we don't want them to become a whitespace literal.
1754 */
1755 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
1756 fr_sbuff_marker(&m, &our_in);
1757 argc = 1;
1758
1759 while (fr_sbuff_extend(&our_in)) {
1760 xlat_exp_t *node = NULL;
1761 fr_token_t quote;
1762 size_t len;
1763
1764 arg_t_rules.literals_safe_for = arg->safe_for;
1765
1766 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
1767 fr_sbuff_set(&m, &our_in); /* Record start of argument */
1768
1769 MEM(node = xlat_exp_alloc(ctx, XLAT_GROUP, NULL, 0)); /* quote = T_BARE_WORD */
1770
1771 if (likely(!spaces)) {
1772 /*
1773 * We've reached the end of the arguments, don't try to tokenize anything else.
1774 */
1775 if (fr_sbuff_is_char(&our_in, ')')) {
1776 slen = 0;
1777
1778 } else {
1779 /*
1780 * Parse a full expression as an argv, all the way to a terminal character.
1781 * We use the input parse rules here.
1782 */
1783 slen = xlat_tokenize_expression(node, &node->group, &our_in, our_p_rules, &arg_t_rules);
1784 }
1785 } else {
1787
1788 node->quote = quote;
1789
1790 if (quote == T_BARE_WORD) {
1791 /*
1792 * Each argument is a bare word all by itself, OR an xlat thing all by itself.
1793 */
1794 slen = xlat_tokenize_input(node->group, &our_in, our_p_rules, &arg_t_rules);
1795
1796 } else {
1797 xlat_exp_t *child = NULL;
1798
1799 slen = xlat_tokenize_word(node->group, &child, &our_in, quote, our_p_rules, &arg_t_rules);
1800 if (child) {
1801 fr_assert(slen > 0);
1802
1803 xlat_exp_insert_tail(node->group, child);
1804 }
1805 }
1806 }
1807
1808 if (slen < 0) {
1809 error:
1810 if (our_p_rules == &tmp_p_rules) talloc_const_free(our_p_rules->terminals);
1812
1813 FR_SBUFF_ERROR_RETURN(&our_in); /* error */
1814 }
1815 fr_assert(node != NULL);
1816
1817 /*
1818 * No data, but the argument was required. Complain.
1819 */
1820 if (!slen && arg->required) {
1821 fr_strerror_printf("Missing required arg %u", argc);
1822 goto error;
1823 }
1824
1825 fr_assert(node->type == XLAT_GROUP);
1826 node->flags = node->group->flags;
1827
1828 /*
1829 * Check number of arguments.
1830 */
1831 if (arg->type == FR_TYPE_NULL) {
1832 fr_strerror_printf("Too many arguments, expected %zu, got %d",
1833 (size_t) (arg - arg_start), argc);
1834 fr_sbuff_set(&our_in, &m);
1835 goto error;
1836 }
1837
1838 if (!node->fmt) xlat_exp_set_name(node, fr_sbuff_current(&m), fr_sbuff_behind(&m));
1839
1840 /*
1841 * Ensure that the function args are correct.
1842 */
1843 if (xlat_validate_function_arg(arg, node, argc) < 0) {
1844 fr_sbuff_set(&our_in, &m);
1845 goto error;
1846 }
1847
1849
1850 /*
1851 * If we're not and the end of the string
1852 * and there's no whitespace between tokens
1853 * then error.
1854 */
1855 fr_sbuff_set(&m, &our_in);
1856 len = fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
1857
1858 /*
1859 * Commas are in the list of terminals, but we skip over them, and keep parsing more
1860 * arguments.
1861 */
1862 if (!spaces) {
1863 fr_assert(p_rules && p_rules->terminals);
1864
1865 if (fr_sbuff_next_if_char(&our_in, ',')) goto next;
1866
1867 if (fr_sbuff_is_char(&our_in, ')')) break;
1868
1869 if (fr_sbuff_eof(&our_in)) {
1870 fr_strerror_printf("Missing ')' after argument %d", argc);
1871 goto error;
1872 }
1873
1874 fr_strerror_printf("Unexpected text after argument %d", argc);
1875 goto error;
1876 }
1877
1878 /*
1879 * Check to see if we have a terminal char, which at this point has to be '``.
1880 */
1881 if (our_p_rules->terminals) {
1882 if (fr_sbuff_is_terminal(&our_in, our_p_rules->terminals)) break;
1883
1884 if (fr_sbuff_eof(&our_in)) {
1885 fr_strerror_printf("Unexpected end of input string after argument %d", argc);
1886 goto error;
1887 }
1888 }
1889
1890 /*
1891 * Otherwise, if we can extend, and found
1892 * no additional whitespace, it means two
1893 * arguments were smushed together.
1894 */
1895 if (fr_sbuff_extend(&our_in) && (len == 0)) {
1896 fr_strerror_const("Unexpected text after argument");
1897 goto error;
1898 }
1899 next:
1900 if (!arg->variadic) {
1901 arg++;
1902 argc++;
1903
1904 if (arg->type == FR_TYPE_NULL) {
1905 fr_strerror_printf("Too many arguments, expected %zu, got %d",
1906 (size_t) (arg - arg_start), argc);
1907 goto error;
1908 }
1909 }
1910 }
1911
1912 if (our_p_rules == &tmp_p_rules) talloc_const_free(our_p_rules->terminals);
1913
1915 *out = head;
1916
1917 FR_SBUFF_SET_RETURN(in, &our_in);
1918}
1919
1920/** Tokenize an xlat expansion
1921 *
1922 * @param[in] ctx to allocate dynamic buffers in.
1923 * @param[out] out the head of the xlat list / tree structure.
1924 * @param[in] in the format string to expand.
1925 * @param[in] p_rules controlling how the string containing the xlat
1926 * expansions should be parsed.
1927 * @param[in] t_rules controlling how attribute references are parsed.
1928 * @return
1929 * - >0 on success.
1930 * - 0 and *head == NULL - Parse failure on first char.
1931 * - 0 and *head != NULL - Zero length expansion
1932 * - < 0 the negative offset of the parse failure.
1933 */
1935 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
1936{
1937 fr_sbuff_t our_in = FR_SBUFF(in);
1939
1940 fr_assert(!t_rules || !t_rules->at_runtime || (t_rules->xlat.runtime_el != NULL));
1941
1943 fr_strerror_clear(); /* Clear error buffer */
1944
1945 if (xlat_tokenize_input(head, &our_in, p_rules, t_rules) < 0) {
1947 FR_SBUFF_ERROR_RETURN(&our_in);
1948 }
1949
1950 /*
1951 * Add nodes that need to be bootstrapped to
1952 * the registry.
1953 */
1954 if (xlat_finalize(head, t_rules->xlat.runtime_el) < 0) {
1956 return 0;
1957 }
1958
1960 *out = head;
1961
1962 FR_SBUFF_SET_RETURN(in, &our_in);
1963}
1964
1965/** Check to see if the expansion consists entirely of value-box elements
1966 *
1967 * @param[in] head to check.
1968 * @return
1969 * - true if expansion contains only literal elements.
1970 * - false if expansion contains expandable elements.
1971 */
1973{
1974 xlat_exp_foreach(head, node) {
1975 if (node->type != XLAT_BOX) return false;
1976 }
1977
1978 return true;
1979}
1980
1981/** Check to see if the expansion needs resolving
1982 *
1983 * @param[in] head to check.
1984 * @return
1985 * - true if expansion needs resolving
1986 * - false otherwise
1987 */
1989{
1990 return head->flags.needs_resolving;
1991}
1992
1993/** Convert an xlat node to an unescaped literal string and free the original node
1994 *
1995 * This is really "unparse the xlat nodes, and convert back to their original string".
1996 *
1997 * @param[in] ctx to allocate the new string in.
1998 * @param[out] str a duplicate of the node's fmt string.
1999 * @param[in,out] head to convert.
2000 * @return
2001 * - true the tree consists of a single value node which was converted.
2002 * - false the tree was more complex than a single literal, op was a noop.
2003 */
2004bool xlat_to_string(TALLOC_CTX *ctx, char **str, xlat_exp_head_t **head)
2005{
2008 size_t len = 0;
2009
2010 if (!*head) return false;
2011
2012 /*
2013 * Instantiation functions may chop
2014 * up the node list into multiple
2015 * literals, so we need to walk the
2016 * list until we find a non-literal.
2017 */
2018 xlat_exp_foreach(*head, node) {
2019 if (node->type != XLAT_BOX) return false;
2020 len += talloc_strlen(node->fmt);
2021 }
2022
2023 fr_sbuff_init_talloc(ctx, &out, &tctx, len, SIZE_MAX);
2024
2025 xlat_exp_foreach(*head, node) {
2027 }
2028
2029 *str = fr_sbuff_buff(&out); /* No need to trim, should be the correct length */
2030
2031 return true;
2032}
2033
2034/** Walk over an xlat tree recursively, resolving any unresolved functions or references
2035 *
2036 * @param[in,out] head of xlat tree to resolve.
2037 * @param[in] xr_rules Specifies rules to use for resolution passes after initial
2038 * tokenization.
2039 * @return
2040 * - 0 on success.
2041 * - -1 on failure.
2042 */
2044{
2045 static xlat_res_rules_t xr_default;
2046 xlat_flags_t our_flags;
2047 xlat_t *func;
2048
2049 if (!head->flags.needs_resolving) return 0; /* Already done */
2050
2051 if (!xr_rules) xr_rules = &xr_default;
2052
2053 our_flags = XLAT_FLAGS_INIT;
2054
2055 xlat_exp_foreach(head, node) {
2056 /*
2057 * This node and none of its children need resolving
2058 */
2059 if (!node->flags.needs_resolving) {
2060 xlat_flags_merge(&our_flags, &node->flags);
2061 continue;
2062 }
2063
2064 switch (node->type) {
2065 case XLAT_GROUP:
2066 if (xlat_resolve(node->group, xr_rules) < 0) return -1;
2067 node->flags = node->group->flags;
2068 break;
2069
2070 /*
2071 * An unresolved function.
2072 */
2074 /*
2075 * Try to find the function
2076 */
2077 func = xlat_func_find(node->fmt, talloc_strlen(node->fmt));
2078 if (!func) {
2079 /*
2080 * FIXME - Produce proper error with marker
2081 */
2082 if (!xr_rules->allow_unresolved) {
2083 fr_strerror_printf("Failed resolving function %pV",
2085 return -1;
2086 }
2087 break;
2088 }
2089
2091 xlat_exp_set_func(node, func, xr_rules->tr_rules->dict_def);
2092
2093 /*
2094 * Check input arguments of our freshly resolved function
2095 */
2096 if (xlat_validate_function_args(node) < 0) return -1;
2097
2098 /*
2099 * Add the freshly resolved function
2100 * to the bootstrap tree.
2101 */
2102 if (xlat_instance_register_func(node) < 0) return -1;
2103
2104 /*
2105 * The function is now resolved, so we go through the normal process of resolving
2106 * its arguments, etc.
2107 */
2109
2110 /*
2111 * A resolved function with unresolved args. We re-initialize the flags from the
2112 * function definition, resolve the arguments, and update the flags.
2113 */
2114 case XLAT_FUNC:
2115 node->flags = node->call.func->flags;
2116
2117 if (node->call.func->resolve) {
2118 void *inst = node->call.inst ? node->call.inst->data : NULL;
2119
2120 if (node->call.func->resolve(node, inst, xr_rules) < 0) return -1;
2121
2122 } else if (node->call.args) {
2123 if (xlat_resolve(node->call.args, xr_rules) < 0) return -1;
2124
2125 } /* else the function takes no arguments */
2126
2127 node->flags.needs_resolving = false;
2129 break;
2130
2131 case XLAT_TMPL:
2132 /*
2133 * Resolve any nested xlats in regexes, exec, or xlats.
2134 */
2135 if (tmpl_resolve(node->vpt, xr_rules->tr_rules) < 0) return -1;
2136
2137 fr_assert(!tmpl_needs_resolving(node->vpt));
2138 node->flags.needs_resolving = false;
2139
2140 if (xlat_tmpl_normalize(node) < 0) return -1;
2141 break;
2142
2143 default:
2144 fr_assert(0); /* boxes, one letter, etc. should not have been marked as unresolved */
2145 return -1;
2146 }
2147
2148 xlat_flags_merge(&our_flags, &node->flags);
2149 }
2150
2151 head->flags = our_flags;
2152
2153 fr_assert(!head->flags.needs_resolving);
2154
2155 return 0;
2156}
2157
2158
2159/** Try to convert an xlat to a tmpl for efficiency
2160 *
2161 * @param ctx to allocate new tmpl_t in.
2162 * @param head to convert.
2163 * @return
2164 * - NULL if unable to convert (not necessarily error).
2165 * - A new #tmpl_t.
2166 */
2168{
2169 tmpl_t *vpt;
2170 xlat_exp_t *node = xlat_exp_head(head);
2171
2172 if (!node || (node->type != XLAT_TMPL) || !tmpl_is_attr(node->vpt)) return NULL;
2173
2174 /*
2175 * Concat means something completely different as an attribute reference
2176 * Count isn't implemented.
2177 */
2178 if ((tmpl_attr_tail_num(node->vpt) == NUM_COUNT) || (tmpl_attr_tail_num(node->vpt) == NUM_ALL)) return NULL;
2179
2181 if (!vpt) return NULL;
2182
2183 tmpl_attr_copy(vpt, node->vpt);
2184
2186
2187 return vpt;
2188}
2189
2191{
2192 return head->flags.impure_func;
2193}
2194
2195/*
2196 * Try to determine the output data type of an expansion.
2197 *
2198 * This is only a best guess for now.
2199 */
2201{
2202 xlat_exp_t *node;
2203
2204 node = xlat_exp_head(head);
2205 fr_assert(node);
2206
2207 if (xlat_exp_next(head, node)) return FR_TYPE_NULL;
2208
2209 if (node->quote != T_BARE_WORD) return FR_TYPE_STRING;
2210
2211 if (node->type == XLAT_FUNC) {
2212 return node->call.func->return_type;
2213 }
2214
2215 if (node->type == XLAT_TMPL) {
2216 return tmpl_data_type(node->vpt);
2217 }
2218
2219 return FR_TYPE_NULL;
2220}
int n
Definition acutest.h:577
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define RCSID(id)
Definition build.h:512
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:228
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:343
#define unlikely(_x)
Definition build.h:407
#define NUM_ELEMENTS(_t)
Definition build.h:358
#define fr_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error.
Definition debug.h:208
#define MEM(x)
Definition debug.h:36
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
static fr_slen_t err
Definition dict.h:882
#define da_is_bit_field(_da)
Definition dict.h:171
static fr_slen_t in
Definition dict.h:882
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
static void * fr_dlist_pop_head(fr_dlist_head_t *list_head)
Remove the head item in a list.
Definition dlist.h:654
#define FR_DLIST_HEAD(_name)
Expands to the type name used for the head wrapper structure.
Definition dlist.h:1104
talloc_free(hp)
static const bool escapes[SBUFF_CHAR_CLASS]
Definition util.c:40
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_BOOL
A truth value.
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
fr_sbuff_parse_error_t
@ FR_SBUFF_PARSE_OK
No error.
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
static void print_args(fr_log_t const *log, char const *file, int line, size_t arg_cnt, uint8_t const *argv, uint8_t const *start, uint8_t const *end)
Definition base.c:356
#define fr_assert(_expr)
Definition rad_assert.h:37
static bool done
Definition radclient.c:80
static const char * spaces
Definition radict.c:177
fr_dict_attr_t const * request_attr_request
Definition request.c:43
static char const * name
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static SBUFF_CHAR_CLASS], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
Definition sbuff.c:1822
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
Definition sbuff.c:2197
bool const sbuff_char_alpha_num[SBUFF_CHAR_CLASS]
Definition sbuff.c:99
size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr)
Wind position until we hit a character in the terminal set.
Definition sbuff.c:1897
ssize_t fr_sbuff_in_bstrcpy_buffer(fr_sbuff_t *sbuff, char const *str)
Copy bytes into the sbuff up to the first \0.
Definition sbuff.c:1513
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:655
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:2133
#define fr_sbuff_out_by_longest_prefix(_match_len, _out, _table, _sbuff, _def)
#define fr_sbuff_adv_past_str_literal(_sbuff, _needle)
#define fr_sbuff_is_str_literal(_sbuff, _str)
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define SBUFF_CHAR_CLASS
Definition sbuff.h:203
#define fr_sbuff_is_alnum(_sbuff_or_marker)
#define fr_sbuff_adv_past_whitespace(_sbuff, _len, _tt)
#define fr_sbuff_current(_sbuff_or_marker)
#define fr_sbuff_char(_sbuff_or_marker, _eob)
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition sbuff.h:190
char const * name
Name for rule set to aid we debugging.
Definition sbuff.h:209
#define FR_SBUFF_IN_STRCPY_LITERAL_RETURN(_sbuff, _str)
#define fr_sbuff_extend(_sbuff_or_marker)
#define fr_sbuff_buff(_sbuff_or_marker)
#define fr_sbuff_used_total(_sbuff_or_marker)
#define SBUFF_CHAR_CLASS_ALPHA_NUM
#define FR_SBUFF_RETURN(_func, _sbuff,...)
#define fr_sbuff_is_char(_sbuff_or_marker, _c)
#define fr_sbuff_eof(_x)
#define FR_SBUFF_ERROR_RETURN(_sbuff_or_marker)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define FR_SBUFF_IN_SPRINTF_RETURN(...)
#define fr_sbuff_uint8(_sbuff_or_marker, _eob)
#define SBUFF_CHAR_UNPRINTABLES_EXTENDED
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define fr_sbuff_out(_err, _out, _in)
#define fr_sbuff_remaining(_sbuff_or_marker)
#define SBUFF_CHAR_UNPRINTABLES_LOW
#define fr_sbuff_behind(_sbuff_or_marker)
#define FR_SBUFF_TERM(_str)
Initialise a terminal structure with a single string.
Definition sbuff.h:178
#define FR_SBUFF_IN_STRCPY_RETURN(...)
#define FR_SBUFF_IN_BSTRCPY_BUFFER_RETURN(...)
Set of terminal elements.
Talloc sbuff extension structure.
Definition sbuff.h:137
Set of parsing rules for *unescape_until functions.
static int16_t tmpl_attr_tail_num(tmpl_t const *vpt)
Return the last attribute reference's attribute number.
Definition tmpl.h:885
#define tmpl_contains_xlat(vpt)
Definition tmpl.h:227
#define TMPL_VERIFY(_vpt)
Definition tmpl.h:961
#define tmpl_is_xlat(vpt)
Definition tmpl.h:210
#define tmpl_is_attr_unresolved(vpt)
Definition tmpl.h:219
#define tmpl_contains_data(vpt)
Definition tmpl.h:224
int tmpl_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules))
Attempt to resolve functions and attributes in xlats and attribute references.
#define tmpl_value(_tmpl)
Definition tmpl.h:937
static fr_slen_t rql fr_slen_t tmpl_attr_print(fr_sbuff_t *out, tmpl_t const *vpt)
Print an attribute or list tmpl_t to a string.
tmpl_t * tmpl_alloc(TALLOC_CTX *ctx, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len)
Create a new heap allocated tmpl_t.
int tmpl_attr_unknown_add(tmpl_t *vpt)
Add an unknown fr_dict_attr_t specified by a tmpl_t to the main dictionary.
#define tmpl_contains_regex(vpt)
Definition tmpl.h:226
fr_value_box_safe_for_t literals_safe_for
safe_for value assigned to literal values in xlats, execs, and data.
Definition tmpl.h:351
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
#define NUM_ALL
Definition tmpl.h:395
fr_dict_attr_t const * enumv
Enumeration attribute used to resolve enum values.
Definition tmpl.h:342
#define tmpl_value_enumv(_tmpl)
Definition tmpl.h:940
#define tmpl_xlat(_tmpl)
Definition tmpl.h:930
static fr_dict_attr_t const * tmpl_list(tmpl_t const *vpt)
Definition tmpl.h:904
#define tmpl_rules_cast(_tmpl)
Definition tmpl.h:942
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition tmpl.h:142
#define NUM_COUNT
Definition tmpl.h:396
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.
fr_type_t tmpl_data_type(tmpl_t const *vpt)
Definition tmpl_eval.c:1342
tmpl_xlat_rules_t xlat
Rules/data for parsing xlats.
Definition tmpl.h:340
bool at_runtime
Produce an ephemeral/runtime tmpl.
Definition tmpl.h:348
ssize_t tmpl_afrom_attr_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t **out, fr_sbuff_t *name, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Parse a string into a TMPL_TYPE_ATTR_* type tmpl_t.
#define tmpl_is_data(vpt)
Definition tmpl.h:206
static fr_slen_t vpt
Definition tmpl.h:1269
fr_dict_t const * dict_def
Alternative default dictionary to use if vpt->rules->dict_def is NULL.
Definition tmpl.h:369
#define NUM_UNSPEC
Definition tmpl.h:394
#define tmpl_value_type(_tmpl)
Definition tmpl.h:939
static fr_type_t tmpl_cast_get(tmpl_t *vpt)
Definition tmpl.h:1220
tmpl_attr_error_t
Definition tmpl.h:1004
@ TMPL_ATTR_ERROR_MISSING_TERMINATOR
Unexpected text found after attribute reference.
Definition tmpl.h:1027
#define tmpl_is_data_unresolved(vpt)
Definition tmpl.h:217
fr_type_t cast
Whether there was an explicit cast.
Definition tmpl.h:344
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:339
int tmpl_attr_copy(tmpl_t *dst, tmpl_t const *src)
Copy a list of attribute and request references from one tmpl to another.
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:801
@ TMPL_ATTR_LIST_ALLOW
Attribute refs are allowed to have a list.
Definition tmpl.h:262
static char const * tmpl_list_name(fr_dict_attr_t const *list, char const *def)
Return the name of a tmpl list or def if list not provided.
Definition tmpl.h:915
fr_event_list_t * runtime_el
The eventlist to use for runtime instantiation of xlats.
Definition tmpl.h:328
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:223
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:336
eap_aka_sim_process_conf_t * inst
Define entry and head types for tmpl request references.
Definition tmpl.h:272
tmpl_attr_list_presence_t list_presence
Whether the attribute reference can have a list, forbid it, or require it.
Definition tmpl.h:298
fr_dict_attr_t const * list_def
Default list to use with unqualified attribute reference.
Definition tmpl.h:295
unsigned int allow_wildcard
Allow the special case of .
Definition tmpl.h:311
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:273
unsigned int allow_unresolved
Allow attributes that look valid but were not found in the dictionaries.
Definition tmpl.h:306
Define manipulation functions for the attribute reference list.
Definition tmpl.h:475
tmpl_request_ref_t _CONST request
Definition tmpl.h:479
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:254
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
const char fr_token_quote[T_TOKEN_LAST]
Convert tokens back to a quoting character.
Definition token.c:224
enum fr_token fr_token_t
@ T_SINGLE_QUOTED_STRING
Definition token.h:120
@ T_BARE_WORD
Definition token.h:118
@ T_BACK_QUOTED_STRING
Definition token.h:121
@ T_DOUBLE_QUOTED_STRING
Definition token.h:119
@ T_SOLIDUS_QUOTED_STRING
Definition token.h:122
tmpl_res_rules_t const * tr_rules
tmpl resolution rules.
Definition xlat.h:165
fr_type_t type
Type to cast argument to.
Definition xlat.h:155
#define XLAT_HEAD_VERIFY(_head)
Definition xlat.h:464
#define XLAT_FLAGS_INIT
Definition xlat.h:118
unsigned int pure
has no external side effects, true for BOX, LITERAL, and some functions
Definition xlat.h:110
int xlat_instance_register_func(xlat_exp_t *node)
Callback for creating "permanent" instance data for a xlat_exp_t.
Definition xlat_inst.c:592
unsigned int xlat
it's an xlat wrapper
Definition xlat.h:115
xlat_escape_func_t func
Function to handle tainted values.
Definition xlat.h:156
bool allow_unresolved
If false, all resolution steps must be completed.
Definition xlat.h:166
@ XLAT_ARG_VARIADIC_EMPTY_SQUASH
Empty argument groups are removed.
Definition xlat.h:136
static fr_slen_t head
Definition xlat.h:420
xlat_arg_parser_variadic_t variadic
All additional boxes should be processed using this definition.
Definition xlat.h:153
fr_value_box_safe_for_t safe_for
Escaped value to set for boxes processed by this escape function.
Definition xlat.h:157
unsigned int required
Argument must be present, and non-empty.
Definition xlat.h:146
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
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:692
unsigned int can_purify
if the xlat has a pure function with pure arguments.
Definition xlat.h:112
fr_slen_t xlat_tokenize_expression(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Definition xlat_expr.c:3163
unsigned int will_escape
the function will do escaping and concatenation.
Definition xlat.h:150
unsigned int constant
xlat is just tmpl_attr_tail_data, or XLAT_BOX
Definition xlat.h:114
unsigned int needs_resolving
Needs pass2 resolution.
Definition xlat.h:109
Definition for a single argument consumed by an xlat function.
Definition xlat.h:145
Flags that control resolution and evaluation.
Definition xlat.h:108
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:581
#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_TYPE_ATTR
A contains an attribute reference.
Definition types.h:83
@ FR_TYPE_PAIR_CURSOR
cursor over a fr_pair_t
Definition types.h:90
#define fr_type_is_null(_x)
Definition types.h:347
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
ssize_t fr_value_box_print(fr_sbuff_t *out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules)
Print one boxed value to a string.
Definition value.c:6105
fr_sbuff_parse_rules_t const * value_parse_rules_quoted[T_TOKEN_LAST]
Parse rules for quoted strings.
Definition value.c:611
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:4196
fr_sbuff_parse_rules_t const * value_parse_rules_3quoted[T_TOKEN_LAST]
Definition value.c:627
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:4619
ssize_t fr_value_box_print_quoted(fr_sbuff_t *out, fr_value_box_t const *data, fr_token_t quote)
Print one boxed value to a string with quotes (where needed)
Definition value.c:6345
fr_sbuff_parse_rules_t const value_parse_rules_bareword_quoted
Definition value.c:529
int fr_value_box_bstr_realloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:4797
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:4838
fr_sbuff_escape_rules_t const * fr_value_escape_by_quote[T_TOKEN_LAST]
Definition value.c:446
#define fr_value_box_mark_safe_for(_box, _safe_for)
Definition value.h:1093
#define fr_box_strvalue_buffer(_val)
Definition value.h:312
#define fr_box_strvalue_len(_val, _len)
Definition value.h:309
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition value.h:162
int nonnull(2, 5))
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:610
static size_t char ** out
Definition value.h:1030
void xlat_exp_finalize_func(xlat_exp_t *node)
Definition xlat_alloc.c:284
void xlat_exp_set_vpt(xlat_exp_t *node, tmpl_t *vpt)
Set the tmpl for a node, along with flags and the name.
Definition xlat_alloc.c:252
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:308
void xlat_exp_set_func(xlat_exp_t *node, xlat_t const *func, fr_dict_t const *dict)
Set the function for a node.
Definition xlat_alloc.c:274
void xlat_exp_set_name_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:338
fr_dict_attr_t const * attr_expr_bool_enum
Definition xlat_eval.c:42
xlat_t * xlat_func_find(char const *in, ssize_t inlen)
Definition xlat_func.c:77
#define xlat_exp_head_alloc(_ctx)
Definition xlat_priv.h:274
xlat_flags_t flags
Flags that control resolution and evaluation.
Definition xlat_priv.h:154
#define xlat_exp_alloc_null(_ctx)
Definition xlat_priv.h:280
static xlat_exp_t * xlat_exp_next(xlat_exp_head_t const *head, xlat_exp_t const *node)
Definition xlat_priv.h:247
int xlat_tokenize_regex(xlat_exp_head_t *head, xlat_exp_t **out, fr_sbuff_t *in, fr_sbuff_marker_t *m_s)
fr_token_t quote
Type of quoting around XLAT_GROUP types.
Definition xlat_priv.h:152
@ XLAT_ONE_LETTER
Special "one-letter" expansion.
Definition xlat_priv.h:109
@ XLAT_BOX
fr_value_box_t
Definition xlat_priv.h:108
@ XLAT_TMPL
xlat attribute
Definition xlat_priv.h:112
@ XLAT_FUNC
xlat module
Definition xlat_priv.h:110
@ XLAT_GROUP
encapsulated string of xlats
Definition xlat_priv.h:116
@ XLAT_FUNC_UNRESOLVED
func needs resolution during pass2.
Definition xlat_priv.h:111
@ XLAT_INVALID
Bad expansion.
Definition xlat_priv.h:107
xlat_arg_parser_t const * args
Definition of args consumed.
Definition xlat_priv.h:94
static void xlat_flags_merge(xlat_flags_t *parent, xlat_flags_t const *child)
Merge flags from child to parent.
Definition xlat_priv.h:230
#define xlat_exp_set_type(_node, _type)
Definition xlat_priv.h:277
char const *_CONST fmt
The original format string (a talloced buffer).
Definition xlat_priv.h:151
xlat_type_t _CONST type
type of this expansion.
Definition xlat_priv.h:155
#define xlat_exp_alloc(_ctx, _type, _in, _inlen)
Definition xlat_priv.h:283
#define xlat_exp_foreach(_list_head, _iter)
Iterate over the contents of a list, only one level.
Definition xlat_priv.h:223
static int xlat_exp_insert_tail(xlat_exp_head_t *head, xlat_exp_t *node)
Definition xlat_priv.h:239
static xlat_exp_t * xlat_exp_head(xlat_exp_head_t const *head)
Definition xlat_priv.h:210
An xlat expansion node.
Definition xlat_priv.h:148
bool xlat_needs_resolving(xlat_exp_head_t const *head)
Check to see if the expansion needs resolving.
#define INFO_INDENT(_fmt,...)
static bool const tmpl_attr_allowed_chars[SBUFF_CHAR_CLASS]
fr_slen_t xlat_tokenize(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)
Tokenize an xlat expansion.
bool xlat_is_literal(xlat_exp_head_t const *head)
Check to see if the expansion consists entirely of value-box elements.
static int xlat_validate_function_arg(xlat_arg_parser_t const *arg_p, xlat_exp_t *arg, int argc)
Validate and sanity check function arguments.
int xlat_validate_function_args(xlat_exp_t *node)
static fr_table_num_sorted_t const xlat_quote_table[]
void xlat_debug_head(xlat_exp_head_t const *head)
static void _xlat_debug_head(xlat_exp_head_t const *head, int depth)
bool xlat_impure_func(xlat_exp_head_t const *head)
fr_slen_t xlat_tokenize_word(TALLOC_CTX *ctx, xlat_exp_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
static size_t xlat_quote_table_len
tmpl_t * xlat_to_tmpl_attr(TALLOC_CTX *ctx, xlat_exp_head_t *head)
Try to convert an xlat to a tmpl for efficiency.
ssize_t xlat_print(fr_sbuff_t *out, xlat_exp_head_t const *head, fr_sbuff_escape_rules_t const *e_rules)
Reconstitute an xlat expression from its constituent nodes.
#define XLAT_HEXDUMP(...)
static fr_sbuff_parse_rules_t const xlat_function_arg_rules
Parse rules for literal values inside of an expansion.
#define XLAT_DEBUG(...)
bool const xlat_func_chars[SBUFF_CHAR_CLASS]
bool xlat_to_string(TALLOC_CTX *ctx, char **str, xlat_exp_head_t **head)
Convert an xlat node to an unescaped literal string and free the original node.
static void _xlat_debug_node(xlat_exp_t const *node, int depth, bool print_flags)
static int xlat_tmpl_normalize(xlat_exp_t *node)
Normalize an xlat which contains a tmpl.
static fr_sbuff_unescape_rules_t const xlat_unescape
These rules apply to literal values and function arguments inside of an expansion.
static ssize_t xlat_tokenize_input(xlat_exp_head_t *head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
Parse an xlat string i.e.
void xlat_debug(xlat_exp_t const *node)
fr_slen_t xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in, xlat_arg_parser_t const *xlat_args, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool spaces)
Tokenize an xlat expansion into a series of XLAT_TYPE_CHILD arguments.
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)
static fr_sbuff_escape_rules_t const xlat_escape
These rules apply to literal values and function arguments inside of an expansion.
static int xlat_tokenize_expansion(xlat_exp_head_t *head, fr_sbuff_t *in, tmpl_rules_t const *t_rules)
static ssize_t xlat_tokenize_attribute(xlat_exp_head_t *head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
Parse an attribute ref or a virtual attribute.
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.
static int xlat_tokenize_function_args(xlat_exp_head_t *head, fr_sbuff_t *in, tmpl_rules_t const *t_rules)
Parse an xlat function and its child argument.
fr_type_t xlat_data_type(xlat_exp_head_t const *head)