The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
tmpl_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: 4a29349bffd62d75ff5495c9da8ff5987e45831d $
19 *
20 * @brief #fr_pair_t template functions
21 * @file src/lib/server/tmpl_tokenize.c
22 *
23 * @ingroup AVP
24 *
25 * @copyright 2014-2020 The FreeRADIUS server project
26 */
27RCSID("$Id: 4a29349bffd62d75ff5495c9da8ff5987e45831d $")
28
29#define _TMPL_PRIVATE 1
30
31#include <freeradius-devel/server/tmpl.h>
32#include <freeradius-devel/server/base.h>
33#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
34
35#include <freeradius-devel/util/base16.h>
36#include <freeradius-devel/util/skip.h>
37
38
39/*
40 * For xlat_exp_head_alloc(), because xlat_copy() doesn't create an output head.
41 */
42#include <freeradius-devel/unlang/xlat_priv.h>
43
44/** Define a global variable for specifying a default request reference
45 *
46 * @param[in] _name what the global variable should be called.
47 * @param[in] _ref one of the values of tmpl_request_ref_t
48 * - REQUEST_CURRENT
49 * - REQUEST_OUTER,
50 * - REQUEST_PARENT,
51 * - REQUEST_UNKNOWN
52 */
53#define TMPL_REQUEST_REF_DEF(_name, _ref) \
54static tmpl_request_t _name ## _entry = { \
55 .entry = { \
56 .entry = { \
57 .next = &_name.head.entry, \
58 .prev = &_name.head.entry \
59 } \
60 }, \
61 .request = _ref \
62}; \
63FR_DLIST_HEAD(tmpl_request_list) _name = { \
64 .head = { \
65 .offset = offsetof(tmpl_request_t, entry), \
66 .entry = { \
67 .next = &_name ## _entry.entry.entry, \
68 .prev = &_name ## _entry.entry.entry, \
69 }, \
70 .num_elements = 1, \
71 } \
72}
73
74/** Use the current request as the default
75 *
76 * Used as .attr.request_def = \&tmpl_request_def_current;
77 */
78TMPL_REQUEST_REF_DEF(tmpl_request_def_current, REQUEST_CURRENT);
79
80/** Use the outer request as the default
81 *
82 * Used as .attr.request_def = \&tmpl_request_def_outer;
83 */
84TMPL_REQUEST_REF_DEF(tmpl_request_def_outer, REQUEST_OUTER);
85
86/** Use the parent request as the default
87 *
88 * Used as .attr.request_def = \&tmpl_request_def_parent;
89 */
90TMPL_REQUEST_REF_DEF(tmpl_request_def_parent, REQUEST_PARENT);
91
92/** Default parser rules
93 *
94 * Because this is getting to be a ridiculous number of parsing rules
95 * to pass in via arguments.
96 *
97 * Defaults are used if a NULL rules pointer is passed to the parsing function.
98 */
99#define DEFAULT_RULES tmpl_rules_t default_rules = { .attr = { .list_def = request_attr_request }}
100
101#define CHECK_T_RULES do { \
102 if (!t_rules) { \
103 t_rules = &default_rules; \
104 } \
105 } while (0)
106
107
108/* clang-format off */
109/** Map #tmpl_type_t values to descriptive strings
110 */
112 { L("uninitialised"), TMPL_TYPE_UNINITIALISED },
113
114 { L("data"), TMPL_TYPE_DATA },
115
116 { L("attr"), TMPL_TYPE_ATTR },
117
118 { L("exec"), TMPL_TYPE_EXEC },
119 { L("xlat"), TMPL_TYPE_XLAT },
120
121 { L("regex"), TMPL_TYPE_REGEX },
122 { L("regex-uncompiled"), TMPL_TYPE_REGEX_UNCOMPILED },
123 { L("regex-xlat"), TMPL_TYPE_REGEX_XLAT },
124
125 { L("data-unresolved"), TMPL_TYPE_DATA_UNRESOLVED },
126 { L("attr-unresolved"), TMPL_TYPE_ATTR_UNRESOLVED },
127 { L("exec-unresolved"), TMPL_TYPE_EXEC_UNRESOLVED },
128 { L("xlat-unresolved"), TMPL_TYPE_XLAT_UNRESOLVED },
129 { L("regex-unresolved"), TMPL_TYPE_REGEX_XLAT_UNRESOLVED }
130};
132
133/** Attr ref types
134 */
136 { L("normal"), TMPL_ATTR_TYPE_NORMAL },
137 { L("unspecified"), TMPL_ATTR_TYPE_UNSPEC },
138 { L("unknown"), TMPL_ATTR_TYPE_UNKNOWN },
139 { L("unresolved"), TMPL_ATTR_TYPE_UNRESOLVED }
140};
142
143/** Map keywords to #tmpl_request_ref_t values
144 */
146 { L("current"), REQUEST_CURRENT },
147 { L("outer"), REQUEST_OUTER },
148 { L("parent"), REQUEST_PARENT },
149};
151
152
153/** Special attribute reference indexes
154 */
156 { L("*"), NUM_ALL },
157 { L("#"), NUM_COUNT },
158 { L("u"), NUM_UNSPEC },
159 { L("n"), NUM_LAST }
160};
162/* clang-format on */
163
164static void attr_to_raw(tmpl_t *vpt, tmpl_attr_t *ref);
165
166/*
167 * Can't use |= or ^= else we get out of range errors
168 */
169#define UNRESOLVED_SET(_flags) (*(_flags) = (*(_flags) | TMPL_FLAG_UNRESOLVED))
170#define RESOLVED_SET(_flags) (*(_flags) = (*(_flags) & ~TMPL_FLAG_UNRESOLVED))
171
172/** Verify, after skipping whitespace, that a substring ends in a terminal char, or ends without further chars
173 *
174 * @param[in] in the sbuff to check.
175 * @param[in] p_rules to use terminals from.
176 * @return
177 * - true if substr is terminated correctly.
178 * - false if subst is not terminated correctly.
179 */
180static inline bool CC_HINT(always_inline) tmpl_substr_terminal_check(fr_sbuff_t *in,
181 fr_sbuff_parse_rules_t const *p_rules)
182{
184 bool ret;
185
186 if (!fr_sbuff_extend(in)) return true; /* we're at the end of the string */
187 if (!p_rules || !p_rules->terminals) return false; /* more stuff to parse but don't have a terminal set */
188
189 fr_sbuff_marker(&m, in);
190 ret = fr_sbuff_is_terminal(in, p_rules->terminals);
191 fr_sbuff_set(in, &m);
192 fr_sbuff_marker_release(&m);
193 return ret;
194}
195
196void tmpl_attr_ref_debug(const tmpl_attr_t *ar, int i)
197{
198 char buffer[sizeof(STRINGIFY(INT16_MAX)) + 1];
199
200 snprintf(buffer, sizeof(buffer), "%i", ar->ar_num);
201
202 switch (ar->type) {
206 if (!ar->da) {
207 FR_FAULT_LOG("\t[%u] %s null%s%s%s",
208 i,
209 fr_table_str_by_value(attr_table, ar->type, "<INVALID>"),
210 ar->ar_num != NUM_UNSPEC ? "[" : "",
211 ar->ar_num != NUM_UNSPEC ? fr_table_str_by_value(attr_num_table, ar->ar_num, buffer) : "",
212 ar->ar_num != NUM_UNSPEC ? "]" : "");
213 return;
214 }
215
216 FR_FAULT_LOG("\t[%u] %s %s %s%s%s%s (%p) attr %u",
217 i,
218 fr_table_str_by_value(attr_table, ar->type, "<INVALID>"),
219 fr_type_to_str(ar->da->type),
220 ar->da->name,
221 ar->ar_num != NUM_UNSPEC ? "[" : "",
222 ar->ar_num != NUM_UNSPEC ? fr_table_str_by_value(attr_num_table, ar->ar_num, buffer) : "",
223 ar->ar_num != NUM_UNSPEC ? "]" : "",
224 ar->da,
225 ar->da->attr
226 );
227 FR_FAULT_LOG("\t is_raw : %s", ar_is_raw(ar) ? "yes" : "no");
228 FR_FAULT_LOG("\t is_unknown : %s", ar_is_unknown(ar) ? "yes" : "no");
229 if (ar->ar_parent) FR_FAULT_LOG("\t parent : %s (%p)", ar->ar_parent->name, ar->ar_parent);
230 break;
231
232
234 /*
235 * Type reveals unresolved status
236 * so we don't need to add it explicitly
237 */
238 FR_FAULT_LOG("\t[%u] %s %s%s%s%s",
239 i,
240 fr_table_str_by_value(attr_table, ar->type, "<INVALID>"),
241 ar->ar_unresolved,
242 ar->ar_num != NUM_UNSPEC ? "[" : "",
243 ar->ar_num != NUM_UNSPEC ? fr_table_str_by_value(attr_num_table, ar->ar_num, buffer) : "",
244 ar->ar_num != NUM_UNSPEC ? "]" : "");
245 if (ar->ar_parent) FR_FAULT_LOG("\t parent : %s", ar->ar_parent->name);
246 if (ar->ar_unresolved_namespace) FR_FAULT_LOG("\t namespace : %s", ar->ar_unresolved_namespace->name);
247 break;
248
249 default:
250 FR_FAULT_LOG("\t[%u] Bad type %s(%u)",
251 i, fr_table_str_by_value(attr_table, ar->type, "<INVALID>"), ar->type);
252 break;
253 }
254}
255
256void tmpl_attr_ref_list_debug(FR_DLIST_HEAD(tmpl_attr_list) const *ar_head)
257{
258 tmpl_attr_t *ar = NULL;
259 unsigned int i = 0;
260
261 FR_FAULT_LOG("attribute references:");
262 /*
263 * Print all the attribute references
264 */
265 while ((ar = tmpl_attr_list_next(ar_head, ar))) {
266 tmpl_attr_ref_debug(ar, i);
267 i++;
268 }
269}
270
272{
273 tmpl_request_t *rr = NULL;
274 unsigned int i = 0;
275
276 switch (vpt->type) {
277 case TMPL_TYPE_ATTR:
279 break;
280
281 default:
282 FR_FAULT_LOG("%s can't print tmpls of type %s", __FUNCTION__,
283 tmpl_type_to_str(vpt->type));
284 return;
285 }
286
287 FR_FAULT_LOG("tmpl_t %s (%.8x) \"%pV\" (%p)",
288 tmpl_type_to_str(vpt->type),
289 vpt->type,
290 fr_box_strvalue_len(vpt->name, vpt->len), vpt);
291
293 FR_FAULT_LOG("\tquote : %s", fr_table_str_by_value(fr_token_quotes_table, vpt->quote, "<INVALID>"));
294
295 FR_FAULT_LOG("request references:");
296
297 /*
298 * Print all the request references
299 */
300 while ((rr = tmpl_request_list_next(&vpt->data.attribute.rr, rr))) {
301 FR_FAULT_LOG("\t[%u] %s (%u)", i,
303 i++;
304 }
305
306 FR_FAULT_LOG("list: %s", tmpl_list_name(tmpl_list(vpt), "<INVALID>"));
308}
309
311{
312 switch (vpt->type) {
313 case TMPL_TYPE_ATTR:
316 return;
317
318 default:
319 break;
320 }
321
322 FR_FAULT_LOG("tmpl_t %s (%.8x) \"%s\" (%p)",
323 tmpl_type_to_str(vpt->type),
324 vpt->type,
325 vpt->name, vpt);
326
328 FR_FAULT_LOG("\tquote : %s", fr_table_str_by_value(fr_token_quotes_table, vpt->quote, "<INVALID>"));
329 switch (vpt->type) {
330 case TMPL_TYPE_DATA:
332 FR_FAULT_LOG("\tlen : %zu", tmpl_value_length(vpt));
333 FR_FAULT_LOG("\tvalue : %pV", tmpl_value(vpt));
334
335 if (tmpl_value_enumv(vpt)) FR_FAULT_LOG("\tenumv : %s (%p)",
337 return;
338
339 case TMPL_TYPE_XLAT:
340 case TMPL_TYPE_EXEC:
342 {
343 char *str;
344
345 xlat_aprint(NULL, &str, tmpl_xlat(vpt), NULL);
346
347 FR_FAULT_LOG("\texpansion : %s", str);
348
349 talloc_free(str);
350 }
351 break;
352
353 case TMPL_TYPE_REGEX:
354 {
355 FR_FAULT_LOG("\tpattern : %s", vpt->name);
356 }
357 break;
358
359 default:
362 FR_FAULT_LOG("\tunescaped : %s", vpt->data.unescaped);
363 FR_FAULT_LOG("\tlen : %zu", talloc_array_length(vpt->data.unescaped) - 1);
364 } else {
365 FR_FAULT_LOG("\tunresolved : %s", vpt->name);
366 FR_FAULT_LOG("\tlen : %zu", vpt->len);
367 }
368 } else {
369 FR_FAULT_LOG("debug nyi");
370 }
371 break;
372 }
373}
374
375/** @name Parse list and request qualifiers to #fr_pair_list_t and #tmpl_request_ref_t values
376 *
377 * These functions also resolve #fr_pair_list_t and #tmpl_request_ref_t values to #request_t
378 * structs and the head of #fr_pair_t lists in those structs.
379 *
380 * For adding new #fr_pair_t to the lists, the #tmpl_list_ctx function can be used
381 * to obtain the appropriate TALLOC_CTX pointer.
382 *
383 * @note These don't really have much to do with #tmpl_t. They're in the same
384 * file as they're used almost exclusively by the tmpl_* functions.
385 * @{
386 */
387
388/** Parse one a single list reference
389 *
390 * @param[out] da_p attribute representing a list.
391 * @param[in] in Sbuff to read request references from.
392 * @return
393 * - > 0 the number of bytes parsed.
394 * - 0 no list qualifier found.
395 */
397{
398 fr_dict_attr_t const *da;
399 fr_sbuff_t our_in = FR_SBUFF(in);
400
401 if (((fr_sbuff_adv_past_strcase(&our_in, request_attr_request->name, request_attr_request->name_len)) &&
402 (da = request_attr_request)) ||
404 (da = request_attr_reply)) ||
406 (da = request_attr_control)) ||
408 (da = request_attr_state))) {
409 /* note: no local variables */
410 *da_p = da;
411 FR_SBUFF_SET_RETURN(in, &our_in);
412 }
413
414 return 0;
415}
416
417 /** Allocate a new request reference and add it to the end of the attribute reference list
418 *
419 */
420static inline CC_HINT(always_inline) CC_HINT(nonnull(2,3))
421void tmpl_request_ref_list_copy(TALLOC_CTX *ctx,
422 FR_DLIST_HEAD(tmpl_request_list) *out, FR_DLIST_HEAD(tmpl_request_list) const *in)
423{
424 tmpl_request_t *rr = NULL;
425 tmpl_request_t *n_rr = NULL;
426
427 /*
428 * Duplicate the complete default list
429 */
430 while ((rr = tmpl_request_list_next(in, rr))) {
431 MEM(n_rr = talloc(ctx, tmpl_request_t));
432 *n_rr = (tmpl_request_t){
433 .request = rr->request
434 };
435 tmpl_request_list_insert_tail(out, n_rr);
436 ctx = n_rr; /* Chain the contexts */
437 }
438}
439
440 /** Allocate a new request reference list and copy request references into it
441 *
442 */
443static inline CC_HINT(always_inline) CC_HINT(nonnull(2,3))
444void tmpl_request_ref_list_acopy(TALLOC_CTX *ctx,
445 FR_DLIST_HEAD(tmpl_request_list) **out, FR_DLIST_HEAD(tmpl_request_list) const *in)
446{
447 FR_DLIST_HEAD(tmpl_request_list) *rql;
448
449 MEM(rql = talloc_zero(ctx, FR_DLIST_HEAD(tmpl_request_list)));
450 tmpl_request_list_talloc_init(rql);
451
453
454 *out = rql;
455}
456
457/** Dump a request list to stderr
458 *
459 */
460void tmpl_request_ref_list_debug(FR_DLIST_HEAD(tmpl_request_list) const *rql)
461{
462 tmpl_request_t *rr = NULL;
463
464 while ((rr = tmpl_request_list_next(rql, rr))) {
465 FR_FAULT_LOG("request - %s (%u)",
467 rr->request);
468 }
469}
470
471/** Compare a list of request qualifiers
472 *
473 * @param[in] a first list. If NULL tmpl_request_def_current will be used.
474 * @param[in] b second list. If NULL tmpl_request_def_current will be used.
475 * @return
476 * - >0 a > b
477 * - 0 a == b
478 * - <0 a < b
479 */
480int8_t tmpl_request_ref_list_cmp(FR_DLIST_HEAD(tmpl_request_list) const *a, FR_DLIST_HEAD(tmpl_request_list) const *b)
481{
482 tmpl_request_t *a_rr = NULL, *b_rr = NULL;
483
484 /*
485 * NULL, uninit, empty are all equivalent
486 * to tmpl_request_def_current.
487 *
488 * We need all these equivalent checks to
489 * deal with uninitialised tmpl rules.
490 */
491 if (!a || !tmpl_request_list_initialised(a) || tmpl_request_list_empty(a)) a = &tmpl_request_def_current;
492 if (!b || !tmpl_request_list_initialised(b) || tmpl_request_list_empty(b)) b = &tmpl_request_def_current;
493
494 /*
495 * Fast path...
496 */
497 if (a == b) return 0;
498
499 for (;;) {
500 a_rr = tmpl_request_list_next(a, a_rr);
501 b_rr = tmpl_request_list_next(b, b_rr);
502
503 if (!a_rr || !b_rr) return CMP(tmpl_request_list_num_elements(a), tmpl_request_list_num_elements(b));
504
505 CMP_RETURN(a_rr, b_rr, request);
506 }
507}
508
509/** Parse one or more request references, writing the list to out
510 *
511 * @param[in] ctx to allocate request refs in.
512 * @param[out] err If !NULL where to write the parsing error.
513 * @param[in] out The list to write to.
514 * @param[in] in Sbuff to read request references from.
515 * @param[in] p_rules Parse rules.
516 * @param[in] t_rules Default list and other rules.
517 * @param[out] namespace the namespace to use
518 * @return
519 * - >= 0 the number of bytes parsed.
520 * - <0 negative offset for where the error occurred
521 */
523 FR_DLIST_HEAD(tmpl_request_list) *out,
524 fr_sbuff_t *in,
525 fr_sbuff_parse_rules_t const *p_rules,
526 tmpl_rules_t const *t_rules,
527 fr_dict_attr_t const **namespace)
528{
530 tmpl_request_t *rr;
531 size_t ref_len;
532 fr_sbuff_t our_in = FR_SBUFF(in);
533 tmpl_request_t *tail = tmpl_request_list_tail(out);
534 unsigned int depth = 0;
536 tmpl_attr_rules_t const *at_rules;
538
540 at_rules = &t_rules->attr;
541
542 /*
543 * The caller wants to know the default namespace for
544 * resolving the attribute.
545 *
546 * @todo - why not use dict_def if it's set? Tho TBH we
547 * should probably just remove dict_def, and always use "namespace".
548 */
549 if (namespace) {
550 if (at_rules->namespace) {
551 *namespace = at_rules->namespace;
552 } else {
553 *namespace = NULL;
554 }
555 }
556
557 /*
558 * We could make the caller do this but as this
559 * function is intended to help populate tmpl rules,
560 * just be nice...
561 */
562 if (!tmpl_request_list_initialised(out)) tmpl_request_list_talloc_init(out);
563
564 fr_sbuff_marker(&m, &our_in);
566 bool end;
567
568 /*
569 * Search for a known request reference like
570 * 'current', or 'parent'.
571 */
573
574 /*
575 * No match
576 */
577 if (ref_len == 0) {
578 /*
579 * If depth == 0, we're at the start
580 * so just use the default request
581 * reference.
582 */
583 default_ref:
584 if ((depth == 0) && at_rules->request_def) {
585 tmpl_request_ref_list_copy(ctx, out, at_rules->request_def);
586 }
587 break;
588 }
589
590 /*
591 * We don't want to misidentify the list
592 * as being part of an attribute.
593 */
594 if (!fr_sbuff_is_char(&our_in, '.') && (fr_sbuff_is_in_charset(&our_in, fr_dict_attr_allowed_chars) || !tmpl_substr_terminal_check(&our_in, p_rules))) {
595 goto default_ref;
596 }
597
598 if (depth == 0) {
599 if (at_rules->namespace || (at_rules->list_presence == TMPL_ATTR_LIST_FORBID)) {
600 fr_strerror_const("List qualifiers are not allowed here");
602
603 fr_sbuff_set(&our_in, in); /* Marker at the start */
604 error:
605 tmpl_request_list_talloc_free_to_tail(out, tail);
606 FR_SBUFF_ERROR_RETURN(&our_in);
607 }
608 }
609
610 /*
611 * If the caller is asking for a namespace, then walk back up the tmpl_rules_t to find a parent namespace.
612 */
613 if (namespace && t_rules && t_rules->parent) {
614 t_rules = t_rules->parent;
615
616 switch (ref) {
617 case REQUEST_OUTER:
618 while (t_rules->parent) t_rules = t_rules->parent; /* Walk back to the root */
620
621 case REQUEST_PARENT:
622 if (t_rules->attr.namespace) {
623 *namespace = t_rules->attr.namespace;
624 } else if (t_rules->attr.dict_def) {
625 *namespace = fr_dict_root(t_rules->attr.dict_def);
626 } else {
627 *namespace = NULL;
628 }
629 break;
630
631 default:
632 break;
633 }
634 }
635
636 /*
637 * Add a new entry to the dlist
638 */
639 MEM(rr = talloc(ctx, tmpl_request_t));
640 *rr = (tmpl_request_t){
641 .request = ref
642 };
643 tmpl_request_list_insert_tail(out, rr);
644
645 /*
646 * Advance past the separator (if there is one)
647 */
648 end = !fr_sbuff_next_if_char(&our_in, '.');
649
650 /*
651 * Update to the last successfully parsed component
652 *
653 * This makes it easy to backtrack from refs like
654 *
655 * parent.outer-realm-name
656 */
657 fr_sbuff_set(&m, &our_in);
658
659 if (end) break;
660 }
661
662 /*
663 * Nesting level too deep
664 */
666 fr_strerror_const("Request ref nesting too deep");
668 goto error; /* Leave marker at the end */
669 }
670
672}
673
674/** Parse one or more request references, allocing a new list and adding the references to it
675 *
676 * This can be used to create request ref lists for rules and for tmpls.
677 *
678 * @param[in] ctx to allocate request refs in.
679 * @param[out] err If !NULL where to write the parsing error.
680 * @param[out] out The new list.
681 * @param[in] in Sbuff to read request references from.
682 * @return
683 * - >= 0 the number of bytes parsed.
684 * - <0 negative offset for where the error occurred
685 */
687 FR_DLIST_HEAD(tmpl_request_list) **out,
688 fr_sbuff_t *in)
689{
690 fr_slen_t slen;
691
692 FR_DLIST_HEAD(tmpl_request_list) *rql;
693
694 MEM(rql = talloc_zero(ctx, FR_DLIST_HEAD(tmpl_request_list)));
695 tmpl_request_list_talloc_init(rql);
696
697 slen = tmpl_request_ref_list_from_substr(rql, err, rql, in, NULL, NULL, NULL);
698 if (slen < 0) {
699 talloc_free(rql);
700 return slen;
701 }
702
703 *out = rql;
704
705 return slen;
706}
707/** @} */
708
709/** @name Alloc or initialise #tmpl_t
710 *
711 * @note Should not usually be called outside of tmpl_* functions, use one of
712 * the tmpl_*from_* functions instead.
713 * @{
714 */
715
716/** Initialise fields inside a tmpl depending on its type
717 *
718 */
719static inline CC_HINT(always_inline) void tmpl_type_init(tmpl_t *vpt, tmpl_type_t type)
720{
721
722 switch (type) {
723#ifndef HAVE_REGEX
724 case TMPL_TYPE_REGEX:
728 fr_assert(0);
729 return;
730#endif
731
732 case TMPL_TYPE_ATTR:
734 tmpl_attr_list_talloc_init(tmpl_attr(vpt));
735 tmpl_request_list_talloc_init(&vpt->data.attribute.rr);
736 break;
737
738 default:
739 break;
740 }
741 vpt->type = type;
742 }
743
744/** Set the name on a pre-initialised tmpl
745 *
746 * @param[in] vpt to set the name for.
747 * @param[in] quote Original quoting around the name.
748 * @param[in] fmt string.
749 * @param[in] ... format arguments.
750 */
751void tmpl_set_name_printf(tmpl_t *vpt, fr_token_t quote, char const *fmt, ...)
752{
753 va_list ap;
754 char const *old = NULL;
755
756 if (vpt->type != TMPL_TYPE_UNINITIALISED) old = vpt->name;
757
758 va_start(ap, fmt);
759 vpt->name = fr_vasprintf(vpt, fmt, ap);
760 vpt->quote = quote;
761 vpt->len = talloc_array_length(vpt->name) - 1;
762 va_end(ap);
763
764 talloc_const_free(old); /* Free name last so it can be used in the format string */
765}
766
767/** Set the name on a pre-initialised tmpl
768 *
769 * @param[in] vpt to set the name for.
770 * @param[in] quote Original quoting around the name.
771 * @param[in] name of the #tmpl_t.
772 * @param[in] len The length of the buffer (or a substring of the buffer) pointed to by name.
773 * If < 0 strlen will be used to determine the length.
774 */
775void tmpl_set_name_shallow(tmpl_t *vpt, fr_token_t quote, char const *name, ssize_t len)
776{
778
779 vpt->name = name;
780 vpt->len = len < 0 ? strlen(name) : (size_t)len;
781 vpt->quote = quote;
782}
783
784/** Set the name on a pre-initialised tmpl
785 *
786 * @param[in] vpt to set the name for.
787 * @param[in] quote Original quoting around the name.
788 * @param[in] name of the #tmpl_t.
789 * @param[in] len The length of the buffer (or a substring of the buffer) pointed to by name.
790 * If < 0 strlen will be used to determine the length.
791 */
792void tmpl_set_name(tmpl_t *vpt, fr_token_t quote, char const *name, ssize_t len)
793{
795
796 talloc_const_free(vpt->name);
797
798 vpt->name = talloc_bstrndup(vpt, name, len < 0 ? strlen(name) : (size_t)len);
799 vpt->len = talloc_array_length(vpt->name) - 1;
800 vpt->quote = quote;
801}
802
803/** Change the default dictionary in the tmpl's resolution rules
804 *
805 * @param[in] vpt to alter.
806 * @param[in] dict to set.
807 */
809{
810 vpt->rules.attr.dict_def = dict;
811}
812
813/** Set escape parameters for the tmpl output
814 *
815 * @param[in] vpt to alter.
816 * @param[in] escape to set.
817 */
819{
820 vpt->rules.escape = *escape;
821}
822
823/** Change the default dictionary in the tmpl's resolution rules
824 *
825 * @param[in] vpt to alter.
826 * @param[in] xlat to set.
827 */
829{
830 fr_assert(vpt->type == TMPL_TYPE_XLAT);
831
832 tmpl_xlat(vpt) = xlat;
833}
834
835
836/** Initialise a tmpl using a format string to create the name
837 *
838 * @param[in] vpt to initialise.
839 * @param[in] type of tmpl to initialise.
840 * @param[in] quote Original quoting around the name.
841 * @param[in] fmt string.
842 * @param[in] ... format arguments.
843 * @return A pointer to the newly initialised tmpl.
844 */
846{
847 va_list ap;
848
849 memset(vpt, 0, sizeof(*vpt));
851
852 va_start(ap, fmt);
853 vpt->name = fr_vasprintf(vpt, fmt, ap);
854 vpt->len = talloc_array_length(vpt->name) - 1;
855 vpt->quote = quote;
856 va_end(ap);
857
858 return vpt;
859}
860
861/** Initialise a tmpl without copying the input name string
862 *
863 * @note Name is not talloc_strdup'd or memcpy'd so must be available, and must not change
864 * for the lifetime of the #tmpl_t.
865 *
866 * @param[out] vpt to initialise.
867 * @param[in] type to set in the #tmpl_t.
868 * @param[in] quote The type of quoting around the template name.
869 * @param[in] name of the #tmpl_t.
870 * @param[in] len The length of the buffer (or a substring of the buffer) pointed to by name.
871 * If < 0 strlen will be used to determine the length.
872 * @param[in] t_rules used during parsing.
873 * @return a pointer to the initialised #tmpl_t. The same value as vpt.
874 */
876 char const *name, ssize_t len, tmpl_rules_t const *t_rules)
877{
878 memset(vpt, 0, sizeof(*vpt));
880 tmpl_set_name_shallow(vpt, quote, name, len);
881 if (t_rules) vpt->rules = *t_rules;
882
883 return vpt;
884}
885
886/** Initialise a tmpl using a literal string to create the name
887 *
888 * @param[in] vpt to initialise.
889 * @param[in] type of tmpl to initialise.
890 * @param[in] quote Original quoting around the name.
891 * @param[in] name to set for the tmpl.
892 * @param[in] len Name length. If < 0 strlen will be used
893 * to determine the name.
894 * @param[in] t_rules used during parsing.
895 * @return A pointer to the newly initialised tmpl.
896 */
898 char const *name, ssize_t len, tmpl_rules_t const *t_rules)
899{
900 memset(vpt, 0, sizeof(*vpt));
902 tmpl_set_name(vpt, quote, name, len);
903 if (t_rules) vpt->rules = *t_rules;
904
905 return vpt;
906}
907
908/** Create a new heap allocated #tmpl_t
909 *
910 * Must be later initialised with a tmpl_init_* function.
911 *
912 * This function is provided to allow tmpls to be pre-allocated for talloc purposes before
913 * their name is known.
914 */
915static inline CC_HINT(always_inline) tmpl_t *tmpl_alloc_null(TALLOC_CTX *ctx)
916{
917 tmpl_t *vpt;
918
919 /*
920 * Allocate enough memory to hold at least
921 * one attribute reference and one request
922 * reference.
923 */
924 MEM(vpt = talloc_pooled_object(ctx, tmpl_t, 2, sizeof(tmpl_request_t) + sizeof(tmpl_attr_t)));
926
927 return vpt;
928}
929
930/** Create a new heap allocated #tmpl_t
931 *
932 * @param[in,out] ctx to allocate in.
933 * @param[in] type to set in the #tmpl_t.
934 * @param[in] name of the #tmpl_t (will be copied to a new talloc buffer parented
935 * by the #tmpl_t).
936 * @param[in] len The length of the buffer (or a substring of the buffer) pointed to by name.
937 * If < 0 strlen will be used to determine the length.
938 * @param[in] quote The type of quoting around the template name.
939 * @return the newly allocated #tmpl_t.
940 */
941tmpl_t *tmpl_alloc(TALLOC_CTX *ctx, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len)
942{
943 tmpl_t *vpt;
944
945 vpt = tmpl_alloc_null(ctx);
946 memset(vpt, 0, sizeof(*vpt));
947
949 if (name) tmpl_set_name(vpt, quote, name, len);
950
951 return vpt;
952}
953/** @} */
954
955/** @name Create new #tmpl_t from a string
956 *
957 * @{
958 */
959
960/** Allocate a new attribute reference and add it to the end of the attribute reference list
961 *
962 */
964{
965 tmpl_attr_t *ar;
966 TALLOC_CTX *ctx;
967
968 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) == 0) {
969 ctx = vpt;
970 } else {
971 ctx = tmpl_attr_list_tail(tmpl_attr(vpt));
972 }
973
974 MEM(ar = talloc(ctx, tmpl_attr_t));
975 *ar = (tmpl_attr_t){
976 .type = type,
977 .filter = {
979 .num = NUM_UNSPEC
980 }
981 };
982 tmpl_attr_list_insert_tail(tmpl_attr(vpt), ar);
983
984 return ar;
985}
986
987/** Create a #tmpl_t from a #fr_value_box_t
988 *
989 * @param[in,out] ctx to allocate #tmpl_t in.
990 * @param[out] out Where to write pointer to new #tmpl_t.
991 * @param[in] data to convert.
992 * @param[in] steal If true, any buffers are moved to the new
993 * ctx instead of being duplicated.
994 * @return
995 * - 0 on success.
996 * - -1 on failure.
997 */
998int tmpl_afrom_value_box(TALLOC_CTX *ctx, tmpl_t **out, fr_value_box_t *data, bool steal)
999{
1000 char *name;
1001 fr_slen_t slen;
1002 tmpl_t *vpt;
1004
1005 MEM(vpt = talloc(ctx, tmpl_t));
1007 if (slen < 0) {
1008 error:
1010 return -1;
1011 }
1012
1013 tmpl_init_shallow(vpt, TMPL_TYPE_DATA, quote, name, slen, NULL);
1014
1015 if (steal) {
1016 if (fr_value_box_steal(vpt, tmpl_value(vpt), data) < 0) goto error;
1017 } else {
1018 if (fr_value_box_copy(vpt, tmpl_value(vpt), data) < 0) goto error;
1019 }
1020 *out = vpt;
1021
1022 return 0;
1023}
1024
1025/** Copy a list of attribute and request references from one tmpl to another
1026 *
1027 */
1028int tmpl_attr_copy(tmpl_t *dst, tmpl_t const *src)
1029{
1030 tmpl_attr_t *src_ar = NULL, *dst_ar;
1031
1032 /*
1033 * Clear any existing attribute references
1034 */
1035 if (tmpl_attr_list_num_elements(tmpl_attr(dst)) > 0) tmpl_attr_list_talloc_reverse_free(tmpl_attr(dst));
1036
1037 while ((src_ar = tmpl_attr_list_next(tmpl_attr(src), src_ar))) {
1038 dst_ar = tmpl_attr_add(dst, src_ar->type);
1039
1040 switch (src_ar->type) {
1042 dst_ar->ar_da = src_ar->ar_da;
1043 break;
1044
1045 case TMPL_ATTR_TYPE_UNSPEC: /* Nothing to copy */
1046 break;
1047
1049 dst_ar->ar_unknown = fr_dict_attr_unknown_copy(dst_ar, src_ar->ar_unknown);
1050 break;
1051
1053 dst_ar->ar_unresolved = talloc_bstrdup(dst_ar, src_ar->ar_unresolved);
1054 break;
1055
1056 default:
1057 if (!fr_cond_assert(0)) return -1;
1058 }
1059 dst_ar->ar_num = src_ar->ar_num;
1060 dst_ar->ar_filter_type = src_ar->ar_filter_type;
1061 dst_ar->parent = src_ar->parent;
1062 }
1063
1064 /*
1065 * Clear any existing request references
1066 * and copy the ones from the source.
1067 */
1068 tmpl_request_list_talloc_reverse_free(&dst->data.attribute.rr);
1069 tmpl_request_ref_list_copy(dst, &dst->data.attribute.rr, &src->data.attribute.rr);
1070
1071 /*
1072 * Ensure that we copy over any parsing rules, defaults, etc.
1073 */
1074 dst->rules = src->rules;
1075
1076 TMPL_ATTR_VERIFY(dst);
1077
1078 return 0;
1079}
1080
1081/** Replace the current attribute reference
1082 *
1083 */
1085{
1086 tmpl_attr_t *ref;
1087
1089
1090 /*
1091 * Clear any existing references
1092 */
1093 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) > 0) {
1094 tmpl_attr_list_talloc_reverse_free(tmpl_attr(vpt));
1095 }
1096
1097 /*
1098 * Unknown attributes get copied
1099 */
1100 if (da->flags.is_unknown) {
1102 ref->da = ref->ar_unknown = fr_dict_attr_unknown_copy(vpt, da);
1103 } else {
1105 ref->da = da;
1106 }
1107 ref->ar_parent = fr_dict_root(fr_dict_by_da(da)); /* Parent is the root of the dictionary */
1108
1110
1111 return 0;
1112}
1113
1114/** Replace the leaf attribute only
1115 *
1116 */
1118{
1119 tmpl_attr_t *ref, *parent = NULL;
1120
1123
1124 /*
1125 * Clear any existing references
1126 */
1127 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) > 0) {
1128 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) > 1) {
1129 ref = tmpl_attr_list_tail(tmpl_attr(vpt));
1130 parent = tmpl_attr_list_prev(tmpl_attr(vpt), ref);
1131
1132 if (!fr_dict_attr_common_parent(parent->ar_da, da, true)) {
1133 fr_strerror_const("New leaf da and old leaf da do not share the same ancestor");
1134 return -1;
1135 }
1136 } else {
1137 ref = tmpl_attr_list_tail(tmpl_attr(vpt));
1138 }
1139
1140 /*
1141 * Free old unknown and unresolved attributes...
1142 */
1143 talloc_free_children(ref);
1144
1145 /*
1146 *
1147 */
1148 ref->ar_filter_type = TMPL_ATTR_FILTER_TYPE_NONE;
1149 ref->ar_num = NUM_UNSPEC;
1150
1151 } else {
1152 ref = tmpl_attr_add(vpt, da->flags.is_unknown ? TMPL_ATTR_TYPE_UNKNOWN : TMPL_ATTR_TYPE_NORMAL);
1153 }
1154
1155
1156 /*
1157 * Unknown attributes get copied
1158 */
1159 if (da->flags.is_unknown) {
1160 ref->da = ref->ar_unknown = fr_dict_attr_unknown_copy(vpt, da);
1161 } else {
1162 ref->da = da;
1163 }
1164
1165 /*
1166 * FIXME - Should be calculated from existing ar
1167 */
1168 ref->ar_parent = fr_dict_root(fr_dict_by_da(da)); /* Parent is the root of the dictionary */
1169
1171
1172 return 0;
1173}
1174
1175/** Rewrite the leaf's instance number
1176 *
1177 * This function is _only_ called from the compiler, for "update" and "foreach" keywords. In those cases,
1178 * the user historically did "foo-bar", but really meant "foo-bar[*]". We silently update that for
1179 * "update" sections, and complain about it in "foreach" sections.
1180 *
1181 * As the server now supports multiple types of leaf references, we do the rewrite _only_ from "none" (no
1182 * filter), OR where it's a numerical index, AND the index hasn't been specified.
1183 */
1185{
1186 tmpl_attr_t *ref = NULL;
1187
1189
1190 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) == 0) return;
1191
1192 ref = tmpl_attr_list_tail(tmpl_attr(vpt));
1193
1194 if (ref->ar_filter_type == TMPL_ATTR_FILTER_TYPE_NONE) {
1195 ref->ar_filter_type = TMPL_ATTR_FILTER_TYPE_INDEX;
1196 ref->ar_num = to;
1197
1198 } else if (ref->ar_filter_type != TMPL_ATTR_FILTER_TYPE_INDEX) {
1199 return;
1200
1201 } else if (ref->ar_num == NUM_UNSPEC) {
1202 ref->ar_num = to;
1203 }
1204
1206}
1207
1208/** Set the request for an attribute ref
1209 *
1210 */
1211void tmpl_attr_set_request_ref(tmpl_t *vpt, FR_DLIST_HEAD(tmpl_request_list) const *request_def)
1212{
1213 fr_assert_msg(tmpl_is_attr(vpt), "Expected tmpl type 'attr', got '%s'",
1214 tmpl_type_to_str(vpt->type));
1215
1216 /*
1217 * Clear any existing request references
1218 */
1219 tmpl_request_list_talloc_reverse_free(&vpt->data.attribute.rr);
1220 tmpl_request_ref_list_copy(vpt, &vpt->data.attribute.rr, request_def);
1221
1223}
1224
1226{
1227 tmpl_attr_t *ref = tmpl_attr_list_head(tmpl_attr(vpt));
1228 if (tmpl_attr_is_list_attr(ref)) ref->da = list;
1229
1231}
1232
1233/** Create a new tmpl from a list tmpl and a da
1234 *
1235 */
1236int tmpl_attr_afrom_list(TALLOC_CTX *ctx, tmpl_t **out, tmpl_t const *list, fr_dict_attr_t const *da)
1237{
1238 tmpl_t *vpt;
1239 tmpl_attr_t *ar;
1240
1241 char attr[256];
1242 ssize_t slen;
1243
1244 MEM(vpt = tmpl_alloc(ctx, TMPL_TYPE_ATTR, T_BARE_WORD, NULL, 0));
1245
1246 /*
1247 * Copies request refs and the list ref
1248 */
1249 tmpl_attr_copy(vpt, list);
1251
1252 if (da->flags.is_unknown) {
1254 ar->da = ar->ar_unknown = fr_dict_attr_unknown_copy(vpt, da);
1255 } else {
1257 ar->ar_da = da;
1258 }
1259
1260 ar->ar_parent = fr_dict_root(fr_dict_by_da(da));
1261
1262 /*
1263 * We need to rebuild the attribute name, to be the
1264 * one we copied from the source list.
1265 */
1266 slen = tmpl_print(&FR_SBUFF_OUT(attr, sizeof(attr)), vpt,
1267 fr_value_escape_by_quote[list->quote]);
1268 if (slen < 0) {
1269 fr_strerror_printf("Serialized attribute too long. Must be < "
1270 STRINGIFY(sizeof(attr)) " bytes, got %zu bytes", (size_t)-slen);
1272 return -1;
1273 }
1274
1275 vpt->len = (size_t)slen;
1276 vpt->name = talloc_typed_strdup(vpt, attr);
1277 vpt->quote = T_BARE_WORD;
1278
1280
1281 *out = vpt;
1282
1283 return 0;
1284}
1285/** @} */
1286
1287/** Insert an attribute reference into a tmpl
1288 *
1289 * Not all attribute references can be used to create new attributes,
1290 * for example those accessing instance > 0 or those that resolve
1291 * to special indexes.
1292 *
1293 * We mark up these references and their parents as resolve only
1294 * meaning that if any code needs to use a reference chain to build
1295 * out a pair tree, it bails out early.
1296 *
1297 * @param[in] vpt containing the reference list.
1298 * @param[in] ar to insert and check.
1299 */
1300static inline CC_HINT(always_inline) void tmpl_attr_insert(tmpl_t *vpt, tmpl_attr_t *ar)
1301{
1302 /*
1303 * Insert the reference into the list.
1304 */
1305 tmpl_attr_list_insert_tail(tmpl_attr(vpt), ar);
1306
1307 switch (ar->ar_num) {
1308 case 0:
1309 case NUM_UNSPEC:
1310 break;
1311
1312 default:
1313 ar->resolve_only = true;
1314 while ((ar = tmpl_attr_list_prev(tmpl_attr(vpt), ar))) ar->resolve_only = true;
1315 break;
1316 }
1317}
1318
1319/** Parse array subscript and in future other filters
1320 *
1321 * @param[out] err Parse error code.
1322 * @param[in] ar to populate filter for.
1323 * @param[in] name containing more attribute ref data.
1324 * @param[in] at_rules see tmpl_attr_afrom_attr_substr.
1325 * @return
1326 * - >0 if a filter was parsed.
1327 * - 0 if no filter was available.
1328 * - <0 on filter parse error.
1329 */
1331 fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
1332{
1333 fr_sbuff_t our_name = FR_SBUFF(name);
1334
1335 /*
1336 * Parse array subscript (and eventually complex filters)
1337 */
1338 if (!fr_sbuff_next_if_char(&our_name, '[')) return 0;
1339
1340 if (at_rules->disallow_filters || tmpl_attr_is_list_attr(ar)) {
1341 fr_strerror_const("Filters not allowed here");
1343 fr_sbuff_set_to_start(&our_name);
1344 FR_SBUFF_ERROR_RETURN(&our_name);
1345 }
1346
1347 ar->ar_filter_type = TMPL_ATTR_FILTER_TYPE_INDEX;
1348 fr_sbuff_switch(&our_name, '\0') {
1349 case '#':
1350 ar->ar_num = NUM_COUNT;
1351 fr_sbuff_next(&our_name);
1352 break;
1353
1354 case '*':
1355 ar->ar_num = NUM_ALL;
1356 fr_sbuff_next(&our_name);
1357 break;
1358
1359 case '0':
1360 case '1':
1361 case '2':
1362 case '3':
1363 case '4':
1364 case '5':
1365 case '6':
1366 case '7':
1367 case '8':
1368 case '9':
1369 {
1370 ssize_t rcode;
1372 fr_sbuff_t tmp = FR_SBUFF(&our_name);
1373
1374 /*
1375 * All digits (not hex).
1376 */
1377 rcode = fr_sbuff_out(&sberr, &ar->ar_num, &tmp);
1378 if ((rcode < 0) || !fr_sbuff_is_char(&tmp, ']')) goto parse_tmpl;
1379
1380 if ((ar->ar_num > 1000) || (ar->ar_num < 0)) {
1381 fr_strerror_printf("Invalid array index '%hi' (should be between 0-1000)", ar->ar_num);
1382 ar->ar_num = 0;
1383 goto error;
1384 }
1385
1386 fr_sbuff_set(&our_name, &tmp); /* Advance name _AFTER_ doing checks */
1387 break;
1388 }
1389
1390 case '"':
1391 case '\'':
1392 case '`':
1393 case '/':
1394 fr_strerror_const("Invalid data type for array index");
1395 goto error;
1396
1397 /* Used as EOB here */
1398 missing_closing:
1399 case '\0':
1400 fr_strerror_const("No closing ']' for array index");
1401 error:
1403 FR_SBUFF_ERROR_RETURN(&our_name);
1404
1405 case '(': /* (...) expression */
1406 {
1407 fr_sbuff_t tmp = FR_SBUFF(&our_name);
1408 fr_slen_t slen;
1409 tmpl_rules_t t_rules;
1410 fr_sbuff_parse_rules_t p_rules;
1411 fr_sbuff_term_t const filter_terminals = FR_SBUFF_TERMS(L("]"));
1412
1413
1414 tmp = FR_SBUFF(&our_name);
1415 t_rules = (tmpl_rules_t) {};
1416 t_rules.attr = *at_rules;
1417
1418 /*
1419 * Unspecified child, we can create a filter starting from the children.
1420 *
1421 * @todo - When parsing the condition, we need to ensure that the condition contains a
1422 * reference to the current cursor, and we need to decide what that syntax is.
1423 */
1424 if (ar->type == TMPL_ATTR_TYPE_UNSPEC) {
1425 t_rules.attr.namespace = fr_dict_root(at_rules->dict_def);
1426
1427 } else {
1428 if (!ar->ar_da || !fr_type_is_structural(ar->ar_da->type)) {
1429 fr_strerror_printf("Invalid filter - cannot use filter on leaf attributes");
1430 ar->ar_num = 0;
1431 goto error;
1432 }
1433 t_rules.attr.namespace = ar->ar_da;
1434 }
1435
1436 p_rules = (fr_sbuff_parse_rules_t) {
1437 .terminals = &filter_terminals,
1438 .escapes = NULL
1439 };
1440
1441 /*
1442 * Check if it's a condition.
1443 */
1444 slen = xlat_tokenize_condition(ar, &ar->ar_cond, &tmp, &p_rules, &t_rules);
1445 if (slen < 0) goto error;
1446
1447 if (xlat_impure_func(ar->ar_cond)) {
1448 fr_strerror_const("Condition in attribute index cannot depend on functions which call external databases");
1449 goto error;
1450 }
1451
1452 ar->ar_filter_type = TMPL_ATTR_FILTER_TYPE_CONDITION;
1453 fr_sbuff_set(&our_name, &tmp); /* Advance name _AFTER_ doing checks */
1454 break;
1455 }
1456
1457 case '%': /* ${...} expansion */
1458 {
1459 fr_sbuff_t tmp = FR_SBUFF(&our_name);
1460 fr_slen_t slen;
1461 tmpl_rules_t t_rules;
1462 fr_sbuff_parse_rules_t p_rules;
1463 fr_sbuff_term_t const filter_terminals = FR_SBUFF_TERMS(L("]"));
1464
1465 if (!fr_sbuff_is_str(&our_name, "%{", 2)) {
1466 fr_strerror_const("Invalid expression in attribute index");
1467 goto error;
1468 }
1469
1470 tmp = FR_SBUFF(&our_name);
1471 t_rules = (tmpl_rules_t) {};
1472 t_rules.attr = *at_rules;
1473
1474 p_rules = (fr_sbuff_parse_rules_t) {
1475 .terminals = &filter_terminals,
1476 .escapes = NULL
1477 };
1478
1479 /*
1480 * Check if it's an expression.
1481 */
1482 slen = xlat_tokenize_expression(ar, &ar->ar_expr, &tmp, &p_rules, &t_rules);
1483 if (slen < 0) goto error;
1484
1485 if (xlat_impure_func(ar->ar_expr)) {
1486 fr_strerror_const("Expression in attribute index cannot depend on functions which call external databases");
1487 goto error;
1488 }
1489
1490 ar->ar_filter_type = TMPL_ATTR_FILTER_TYPE_EXPR;
1491
1492 fr_sbuff_set(&our_name, &tmp); /* Advance name _AFTER_ doing checks */
1493 break;
1494 }
1495
1496 case 'n':
1497 /*
1498 * [n] is the last one
1499 *
1500 * [nope] is a reference to "nope".
1501 */
1502 if (fr_sbuff_is_str(&our_name, "n]", 2)) {
1503 ar->ar_num = NUM_LAST;
1504 fr_sbuff_next(&our_name);
1505 break;
1506 }
1508
1509 default:
1510 parse_tmpl:
1511 {
1512 fr_sbuff_t tmp = FR_SBUFF(&our_name);
1513 ssize_t slen;
1514 tmpl_rules_t t_rules;
1515 fr_sbuff_parse_rules_t p_rules;
1516 fr_sbuff_term_t const filter_terminals = FR_SBUFF_TERMS(L("]"));
1517
1518 tmp = FR_SBUFF(&our_name);
1519 t_rules = (tmpl_rules_t) {};
1520 t_rules.attr = *at_rules;
1521
1522 /*
1523 * Don't reset namespace, we always want to start searching from the top level of the
1524 * dictionaries.
1525 */
1526
1527 p_rules = (fr_sbuff_parse_rules_t) {
1528 .terminals = &filter_terminals,
1529 .escapes = NULL
1530 };
1531
1532 /*
1533 * @todo - for some reason, the tokenize_condition code allows for internal
1534 * vs protocol vs local attributes, whereas the tmpl function only accepts
1535 * internal ones.
1536 */
1537 slen = tmpl_afrom_substr(ar, &ar->ar_tmpl, &tmp, T_BARE_WORD, &p_rules, &t_rules);
1538 if (slen <= 0) goto error;
1539
1540 if (!tmpl_is_attr(ar->ar_tmpl)) {
1541 fr_strerror_printf("Invalid array index '%s'", ar->ar_tmpl->name);
1542 goto error;
1543 }
1544
1545 /*
1546 * Arguably we _could_ say &User-Name["foo"] matches all user-name with value "foo",
1547 * but that would confuse the issue for &Integer-Thing[4].
1548 *
1549 * For matching therefore, we really need to have a way to define "self".
1550 */
1551 if (!fr_type_numeric[tmpl_attr_tail_da(ar->ar_tmpl)->type]) {
1552 fr_strerror_const("Invalid data type for array index (must be numeric)");
1553 goto error;
1554 }
1555
1556 ar->ar_filter_type = TMPL_ATTR_FILTER_TYPE_TMPL;
1557 fr_sbuff_set(&our_name, &tmp); /* Advance name _AFTER_ doing checks */
1558 break;
1559 }
1560 }
1561
1562 /*
1563 * Always advance here, so the error
1564 * marker points to the bad char.
1565 */
1566 if (!fr_sbuff_next_if_char(&our_name, ']')) goto missing_closing;
1567
1568 FR_SBUFF_SET_RETURN(name, &our_name);
1569}
1570
1571extern fr_dict_attr_t const *tmpl_attr_unspec;
1572
1573static inline CC_HINT(nonnull(3,4))
1575 tmpl_t *vpt,
1576 fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
1577{
1578 fr_slen_t slen;
1579
1580 *ar = (tmpl_attr_t){
1581 .ar_num = NUM_UNSPEC, /* May be changed by tmpl_attr_parse_filter */
1582 .ar_type = TMPL_ATTR_TYPE_UNSPEC,
1583 .ar_da = tmpl_attr_unspec,
1584 };
1585
1586 slen = tmpl_attr_parse_filter(err, ar, name, at_rules);
1587 if (slen < 0) {
1588 return slen;
1589
1590 /*
1591 * No filters and no previous elements is the equivalent of '&'
1592 * which is not allowed.
1593 *
1594 * &[<filter>] is allowed as this lets us perform filtering operations
1595 * at the root.
1596 */
1597 } else if ((slen == 0) && (tmpl_attr_num_elements(vpt) == 0)) {
1598 fr_strerror_const("Invalid attribute name");
1600 return -1;
1601 }
1602
1603 tmpl_attr_insert(vpt, ar);
1604
1605 return slen;
1606}
1607
1608/** Parse an unresolved attribute, i.e. one which can't be found in the current dictionary
1609 *
1610 * This function calls itself recursively to process additional OID
1611 * components once we've failed to resolve one component.
1612 *
1613 * @note Do not call directly.
1614 *
1615 * @param[in] ctx to allocate new attribute reference in.
1616 * @param[out] err Parse error.
1617 * @param[in,out] vpt to append this reference to.
1618 * @param[in] parent Last known parent.
1619 * @param[in] namespace in which the attribute will be resolved.
1620 * @param[in] name to parse.
1621 * @param[in] at_rules see tmpl_attr_afrom_attr_substr.
1622 * @return
1623 * - <0 on error.
1624 * - 0 on success.
1625 */
1626static inline CC_HINT(nonnull(3,6))
1628 tmpl_t *vpt,
1629 fr_dict_attr_t const *parent, fr_dict_attr_t const *namespace,
1630 fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
1631{
1632 tmpl_attr_t *ar = NULL, *ar_curr;
1633 fr_sbuff_t our_name = FR_SBUFF(name);
1634 fr_slen_t slen;
1635 char *unresolved;
1636
1637 /*
1638 * Point we free from if something goes wrong.
1639 */
1640 ar_curr = tmpl_attr_list_tail(tmpl_attr(vpt));
1641 for (;;) {
1642 MEM(ar = talloc(ctx, tmpl_attr_t));
1643 /*
1644 * Copy out a string of allowed dictionary chars to form
1645 * the unresolved attribute name.
1646 *
1647 * This will be resolved later (outside of this function).
1648 */
1649 slen = fr_sbuff_out_abstrncpy_allowed(ar, &unresolved,
1650 &our_name, FR_DICT_ATTR_MAX_NAME_LEN + 1,
1652 if (slen == 0) {
1653 slen = tmpl_attr_ref_from_unspecified_substr(ar, err, vpt, &our_name, at_rules);
1654 if (slen < 0) {
1655 fr_sbuff_advance(&our_name, +slen);
1656 error:
1657 talloc_free(ar);
1658 tmpl_attr_list_talloc_free_to_tail(tmpl_attr(vpt), ar_curr);
1659 return -1;
1660 }
1661 return fr_sbuff_set(name, &our_name);
1662 } else if (slen > FR_DICT_ATTR_MAX_NAME_LEN) {
1663 fr_strerror_const("Attribute name is too long");
1665 goto error;
1666 }
1667
1668 *ar = (tmpl_attr_t){
1669 .ar_num = NUM_UNSPEC,
1670 .ar_type = TMPL_ATTR_TYPE_UNRESOLVED,
1671 .ar_unresolved = unresolved,
1672 .ar_unresolved_namespace = namespace,
1673 .ar_parent = parent
1674 };
1675
1676 if (tmpl_attr_parse_filter(err, ar, &our_name, at_rules) < 0) goto error;
1677
1678 /*
1679 * Insert the ar into the list of attribute references
1680 */
1681 tmpl_attr_insert(vpt, ar);
1682
1683 /*
1684 * Once one OID component is created as unresolved all
1685 * future OID components are also unresolved.
1686 */
1687 if (!fr_sbuff_next_if_char(&our_name, '.')) break;
1688 }
1689
1690 /*
1691 * Mark the tmpl up as an unresolved attribute reference
1692 * the attribute reference will be resolved later.
1693 */
1695
1696 return fr_sbuff_set(name, &our_name);
1697}
1698
1699/*
1700 * Add attr_ref when we've parsed an intermediate dictionary name
1701 * which is itself a ref.
1702 */
1703static void tmpl_attr_ref_fixup(TALLOC_CTX *ctx, tmpl_t *vpt, fr_dict_attr_t const *da, fr_dict_attr_t const *parent)
1704{
1705 tmpl_attr_t *ar;
1706
1707 if (tmpl_attr_tail_da(vpt) == da) return;
1708
1709 if (da->parent != parent) tmpl_attr_ref_fixup(ctx, vpt, da->parent, parent);
1710
1711 MEM(ar = talloc(ctx, tmpl_attr_t));
1712 *ar = (tmpl_attr_t) {
1713 .ar_num = NUM_UNSPEC,
1714 .ar_type = TMPL_ATTR_TYPE_NORMAL,
1715 .ar_da = da,
1716 .ar_parent = da->parent,
1717 };
1718
1719 tmpl_attr_insert(vpt, ar);
1720}
1721
1722/** Parse an attribute reference, either an OID or attribute name
1723 *
1724 * @note Do not call directly.
1725 *
1726 * @param[in] ctx to allocate new attribute reference in.
1727 * @param[out] err Parse error.
1728 * @param[in,out] vpt to append this reference to.
1729 * @param[in] parent Parent where the attribute will be placed (group, struct, tlv, etc).
1730 * @param[in] namespace Where the child attribute will be parsed from (dict root, struct member, TLV child, etc)
1731 * @param[in] name to parse.
1732 * @param[in] p_rules Formatting rules used to check for trailing garbage.
1733 * @param[in] at_rules which places constraints on attribute reference parsing.
1734 * Rules interpreted by this function is:
1735 * - allow_unknown - If false unknown OID components
1736 * result in a parse error.
1737 * - allow_unresolved - If false unknown attribute names
1738 * result in a parse error.
1739 * - allow_foreign - If an attribute resolves in a dictionary
1740 * that does not match the parent
1741 * (exception being FR_TYPE_GROUP) then that results
1742 * in a parse error.
1743 * @param[in] depth How deep we are. Used to check for maximum nesting level.
1744 * @return
1745 * - <0 on error.
1746 * - 0 on success.
1747 */
1748static inline int tmpl_attr_afrom_attr_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err,
1749 tmpl_t *vpt,
1750 fr_dict_attr_t const *parent, fr_dict_attr_t const *namespace,
1752 fr_sbuff_parse_rules_t const *p_rules, tmpl_attr_rules_t const *at_rules,
1753 unsigned int depth)
1754{
1755 uint32_t oid = 0;
1756 tmpl_attr_t *ar = NULL;
1757 fr_dict_attr_t const *da;
1759 fr_dict_attr_err_t dict_err;
1760 fr_dict_attr_t const *our_parent = parent;
1761
1762 fr_sbuff_marker(&m_s, name);
1763
1765 fr_strerror_const("Attribute nesting too deep");
1767 error:
1768 talloc_free(ar);
1769 fr_sbuff_marker_release(&m_s);
1771 }
1772
1773 /*
1774 * Input too short
1775 */
1776 if (!fr_sbuff_extend(name)) {
1777 fr_strerror_const("Missing attribute reference");
1779 goto error;
1780 }
1781
1782 /*
1783 * Maybe there's no child namespace (struct member, tlv child, etc). In which case we must
1784 * search from the default dictionary root.
1785 *
1786 * This search is probably wrong in some cases. See the comments below around FR_TYPE_GROUP.
1787 *
1788 * If we change out the dictionaries, we should arguably also change dict_def in the
1789 * tmpl_attr_rules_t. On top of that, the "dict_attr_search" functions take a #fr_dict_t
1790 * pointer, and not a pointer to the dict root. So we can't pass them a namespace.
1791 */
1792 if (!namespace) {
1793 fr_assert(parent == NULL);
1794
1796 at_rules->dict_def,
1797 name, p_rules ? p_rules->terminals : NULL,
1798 true,
1799 at_rules->allow_foreign);
1800 /*
1801 * The attribute was found either in the dict_def root, OR in the internal root, OR if
1802 * !dict_def && allow_foreign, in some other dictionary root.
1803 *
1804 * Otherwise we're still not sure what the attribute is. It may end up being an
1805 * unresolved one.
1806 */
1807 if (da) {
1808 our_parent = da->parent;
1809
1810 if (!our_parent->flags.is_root) {
1811 tmpl_attr_ref_fixup(ctx, vpt, our_parent, fr_dict_root(da->dict));
1812 }
1813 }
1814 } else {
1815 fr_assert(parent != NULL);
1816
1817 /*
1818 * Otherwise we're resolving the next piece in the context of where-ever we ended up from
1819 * parsing the last bit.
1820 *
1821 * The "parent" could be the same as "namespace", if both are at a dictionary root, OR
1822 * both are from a struct / tlv attribute.
1823
1824 * Or, "parent" could be a grouping attribute (e.g. request), and "namespace" could be
1825 * the dictionary root.
1826 */
1827 (void)fr_dict_attr_by_name_substr(&dict_err,
1828 &da,
1829 namespace,
1830 name,
1831 p_rules ? p_rules->terminals : NULL);
1832
1833 /*
1834 * Allow fallback to internal attributes
1835 * if the parent was a group, and we're
1836 * allowing internal resolution.
1837 *
1838 * Discard any errors here... It's more
1839 * useful to have the original.
1840 */
1841 if (!da) {
1842 ar = tmpl_attr_list_tail(&vpt->data.attribute.ar);
1843 if (!ar || ((ar->type == TMPL_ATTR_TYPE_NORMAL) && (ar->ar_da->type == FR_TYPE_GROUP))) {
1844 fr_dict_attr_t const *internal_root = fr_dict_root(fr_dict_internal());
1845
1846 (void)fr_dict_attr_by_name_substr(NULL,
1847 &da, internal_root,
1848 name,
1849 p_rules ? p_rules->terminals : NULL);
1850 if (da) {
1851 dict_err = FR_DICT_ATTR_OK;
1852 our_parent = internal_root;
1853 }
1854 }
1855 ar = NULL;
1856
1857 } else {
1858 /*
1859 * If we searched in a local dictionary, but found a real attribute
1860 * switch the namespace.
1861 */
1862 if (!da->flags.local && namespace->flags.local) namespace = our_parent = fr_dict_root(da->dict);
1863
1864 /*
1865 * We had an alias in the same namespace,
1866 * go add more things in.
1867 */
1868 if (da->parent != our_parent) {
1869 fr_assert(namespace == our_parent);
1870 tmpl_attr_ref_fixup(ctx, vpt, da->parent, our_parent);
1871 }
1872 }
1873 }
1874
1875 /*
1876 * Fatal errors related to nesting...
1877 */
1878 switch (dict_err) {
1880 fr_assert(our_parent != NULL);
1881 if (our_parent->flags.is_unknown) break;
1882 goto error;
1883
1885 goto error;
1886
1887 default:
1888 if (!da) break;
1889
1890 /*
1891 * The named component was a known attribute
1892 * so record it as a normal attribute
1893 * reference.
1894 */
1895 fr_assert(our_parent != NULL);
1896 goto alloc_ar;
1897 }
1898
1899 /*
1900 * At this point we haven't found a known attribute. What remains MUST be an OID component, OR an
1901 * unresolved attribute.
1902 *
1903 * The default is to parse the OIDs in the current namespace. If there is none, then we parse
1904 * the OIDs and unresolved attributes in the dict_def. And if that doesn't exist, in the
1905 * internal dictionaries.
1906 *
1907 * Note that we do NOT allow unknown attributes in the internal dictionary. Those attributes are
1908 * generally just DEFINEs, and their numbers have no meaning.
1909 */
1910 if (!namespace) {
1911 if (at_rules->dict_def) {
1912 our_parent = namespace = fr_dict_root(at_rules->dict_def);
1913 } else {
1914 our_parent = namespace = fr_dict_root(fr_dict_internal());
1915 }
1916 }
1917
1918 fr_assert(our_parent != NULL);
1919 fr_assert(namespace != NULL);
1920
1921 /*
1922 * See if the ref begins with an unsigned integer
1923 * if it does it's probably an OID component
1924 *
1925 * .<oid>
1926 */
1927 if (fr_sbuff_out(NULL, &oid, name) > 0) {
1928 namespace = fr_dict_unlocal(namespace);
1929
1930 fr_assert(ar == NULL);
1931
1932 fr_strerror_clear(); /* Clear out any existing errors */
1933
1934 if (fr_dict_by_da(namespace) == fr_dict_internal()) goto disallow_unknown;
1935
1936 /*
1937 * The OID component was a known attribute
1938 * so record it as a normal attribute
1939 * reference.
1940 */
1941 da = fr_dict_attr_child_by_num(namespace, oid);
1942 if (da) goto alloc_ar;
1943
1944 if (!at_rules->allow_unknown) {
1945 disallow_unknown:
1946 fr_strerror_const("Unknown attributes not allowed here");
1948 fr_sbuff_set(name, &m_s);
1949 goto error;
1950 }
1951
1952 /*
1953 * If it's numeric and not a known attribute
1954 * then we create an unknown attribute with
1955 * the specified attribute number.
1956 */
1957 MEM(ar = talloc(ctx, tmpl_attr_t));
1958
1959 /*
1960 * VSAs have VENDORs as children. All others are just normal things.
1961 */
1962 switch (namespace->type) {
1963 case FR_TYPE_VSA:
1964 da = fr_dict_attr_unknown_vendor_afrom_num(ar, namespace, oid);
1965 break;
1966
1967 default:
1968 da = fr_dict_attr_unknown_raw_afrom_num(ar, namespace, oid);
1969 break;
1970 }
1971
1972 if (!da) {
1973 if (err) *err = TMPL_ATTR_ERROR_UNKNOWN_NOT_ALLOWED; /* strerror set by dict function */
1974 goto error;
1975 }
1976
1977 *ar = (tmpl_attr_t){
1978 .ar_num = NUM_UNSPEC,
1979 .ar_type = TMPL_ATTR_TYPE_UNKNOWN,
1980 .ar_unknown = UNCONST(fr_dict_attr_t *, da),
1981 .ar_da = da,
1982 .ar_parent = our_parent,
1983 };
1984 goto do_suffix;
1985 }
1986
1987 /*
1988 * Can't parse it as an attribute, might be a literal string
1989 * let the caller decide.
1990 *
1991 * Don't alter the fr_strerror buffer, may contain useful
1992 * errors from the dictionary code.
1993 */
1994 if (!at_rules->allow_unresolved && !(at_rules->allow_wildcard && fr_sbuff_is_char(name, '['))) {
1995 fr_strerror_const_push("Unresolved attributes are not allowed here");
1997 fr_sbuff_set(name, &m_s);
1998 goto error;
1999 }
2000
2001 fr_sbuff_marker_release(&m_s);
2002
2003 /*
2004 * Once we hit one unresolved attribute we have to treat
2005 * the rest of the components are unresolved as well.
2006 */
2007 return tmpl_attr_ref_afrom_unresolved_substr(ctx, err, vpt, our_parent, namespace, name, at_rules);
2008
2009alloc_ar:
2010 /*
2011 * We have a da, remove any of the errors recorded from failed
2012 * searches to find the attribute to avoid misleading messages
2013 * if something else fails.
2014 */
2016
2017 MEM(ar = talloc(ctx, tmpl_attr_t));
2018 *ar = (tmpl_attr_t) {
2019 .ar_num = NUM_UNSPEC,
2020 .ar_type = TMPL_ATTR_TYPE_NORMAL,
2021 .ar_da = da,
2022 .ar_parent = our_parent,
2023 };
2024
2025do_suffix:
2026 /*
2027 * Parse the attribute reference filter
2028 *
2029 * Error out immediately if the filter is bad
2030 * otherwise determine whether to keep the
2031 * attribute reference or omit it based on:
2032 *
2033 * - Whether there was a filter present.
2034 * - The type of attribute.
2035 * - If this is the leaf attribute reference.
2036 */
2037 if (tmpl_attr_parse_filter(err, ar, name, at_rules) < 0) goto error;
2038
2039 /*
2040 * Local variables are always unitary.
2041 *
2042 * [0] is allowed, as is [n], [*], and [#]. But [1], etc. aren't allowed.
2043 */
2044 if (da->flags.local && (ar->ar_num > 0)) {
2045 fr_strerror_printf("Invalid array reference for local variable");
2047 fr_sbuff_set(name, &m_s);
2048 goto error;
2049 }
2050
2051 /*
2052 * At the end of the attribute reference. If there's a
2053 * trailing '.' then there's another attribute reference
2054 * we need to parse, otherwise we're done.
2055 */
2056 fr_sbuff_marker_release(&m_s);
2057 fr_sbuff_marker(&m_s, name);
2058
2059 if (fr_sbuff_next_if_char(name, '.')) {
2060 fr_dict_attr_t const *ref;
2061
2062 switch (da->type) {
2063 case FR_TYPE_GROUP:
2064 ref = fr_dict_attr_ref(da);
2065
2066 /*
2067 * If the ref is outside of the internal namespace, then we use it.
2068 *
2069 * If the ref is inside of the internal namespace (e.g. "request"), then we do
2070 * something else.
2071 *
2072 * If we were given a root dictionary on input, use that. We have to follow this
2073 * dictionary because this function calls itself recursively, WITHOUT updating
2074 * "dict_def" in the attr_rules. So the dict-def there is whatever got passed
2075 * into tmpl_afrom_attr_substr(), BEFORE the "parent.parent.parent..." parsing.
2076 * Which means that in many cases, the "dict_def" is completely irrelevant.
2077 *
2078 * If there is no parent on input, then we just use dict_def.
2079 *
2080 * Otherwise we search through all of the dictionaries.
2081 *
2082 * Note that we cannot put random protocol attributes into an internal attribute
2083 * of type "group".
2084 */
2085 if (ref != fr_dict_root(fr_dict_internal())) {
2086 our_parent = namespace = ref;
2087
2088 } else if (parent && parent->flags.is_root) {
2089 our_parent = namespace = parent;
2090
2091 } else if (at_rules->dict_def) {
2092 our_parent = namespace = fr_dict_root(at_rules->dict_def);
2093
2094 } else {
2095 our_parent = namespace = NULL;
2096 }
2097 break;
2098
2099 case FR_TYPE_STRUCT:
2100 case FR_TYPE_TLV:
2101 case FR_TYPE_VENDOR:
2102 case FR_TYPE_VSA:
2103 is_union:
2104 /*
2105 * Structural types are parented and namespaced from their parent da.
2106 */
2107 namespace = our_parent = da;
2108 break;
2109
2110 default:
2111 /*
2112 * Key fields can have children, because we really don't know how else to
2113 * represent the child structures.
2114 */
2115 if (fr_dict_attr_is_key_field(da)) goto is_union;
2116
2117 fr_strerror_printf("Parent type of nested attribute %s must be of type "
2118 "\"struct\", \"tlv\", \"vendor\", \"vsa\" or \"group\", got \"%s\"",
2119 da->name,
2120 fr_type_to_str(da->type));
2121 fr_sbuff_set(name, &m_s);
2122 goto error;
2123 }
2124
2125 if (ar) tmpl_attr_insert(vpt, ar);
2126
2127 if (tmpl_attr_afrom_attr_substr(ctx, err, vpt, our_parent, namespace, name, p_rules, at_rules, depth + 1) < 0) {
2128 if (ar) {
2129 tmpl_attr_list_talloc_free_tail(&vpt->data.attribute.ar); /* Remove and free ar */
2130 ar = NULL;
2131 }
2132 goto error;
2133 }
2134
2135 /*
2136 * If it's a leaf we always insert the attribute
2137 * reference into the list, even if it's a
2138 * nesting attribute.
2139 *
2140 * This is useful for nested edit sections
2141 * where the tmpl might be the name of a new
2142 * subsection.
2143 */
2144 } else {
2145 tmpl_attr_insert(vpt, ar);
2146 }
2147
2148 /*
2149 * Remove unnecessary casts.
2150 */
2153
2155
2156 fr_sbuff_marker_release(&m_s);
2157 return 0;
2158}
2159
2160/** Parse a string into a TMPL_TYPE_ATTR_* type #tmpl_t
2161 *
2162 * @param[in,out] ctx to allocate #tmpl_t in.
2163 * @param[out] err May be NULL. Provides the exact error that the parser hit
2164 * when processing the attribute ref.
2165 * @param[out] out Where to write pointer to new #tmpl_t.
2166 * @param[in] name of attribute including #tmpl_request_ref_t and #fr_pair_list_t qualifiers.
2167 * @param[in] p_rules Formatting rules used to check for trailing garbage.
2168 * @param[in] t_rules Rules which control parsing:
2169 * - dict_def The default dictionary to use if attributes
2170 * are unqualified.
2171 * - request_def The default #request_t to set if no
2172 * #tmpl_request_ref_t qualifiers are found in name.
2173 * - list_def The default list to set if no #fr_pair_list_t
2174 * qualifiers are found in the name.
2175 * - allow_unknown If true attributes in the format accepted by
2176 * #fr_dict_attr_unknown_afrom_oid_substr will be allowed,
2177 * even if they're not in the main dictionaries.
2178 * If an unknown attribute is found a #TMPL_TYPE_ATTR
2179 * #tmpl_t will be produced.
2180 * If #tmpl_afrom_attr_substr is being called on
2181 * startup, the #tmpl_t may be passed to
2182 * #tmpl_attr_unknown_add to
2183 * add the unknown attribute to the main dictionary.
2184 * If the unknown attribute is not added to
2185 * the main dictionary the #tmpl_t cannot be used
2186 * to search for a #fr_pair_t in a #request_t.
2187 * - allow_unresolved If true, we don't generate a parse error on
2188 * unknown attributes. If an unknown attribute is
2189 * found a #TMPL_TYPE_ATTR_UNRESOLVED
2190 * #tmpl_t will be produced.
2191 * - allow_foreign If true, allow attribute names to be qualified
2192 * with a protocol outside of the passed dict_def.
2193 * - disallow_filters
2194 *
2195 * @see REMARKER to produce pretty error markers from the return value.
2196 *
2197 * @return
2198 * - <= 0 on error (offset as negative integer)
2199 * - > 0 on success (number of bytes parsed).
2200 */
2203 fr_sbuff_parse_rules_t const *p_rules,
2204 tmpl_rules_t const *t_rules)
2205{
2206 int ret;
2207 tmpl_t *vpt;
2208 fr_sbuff_t our_name = FR_SBUFF(name); /* Take a local copy in case we need to back track */
2209 bool is_raw = false;
2210 tmpl_attr_rules_t const *at_rules;
2212 fr_dict_attr_t const *namespace;
2214
2216
2217 at_rules = &t_rules->attr;
2218
2220
2221 if (!fr_sbuff_extend(&our_name)) {
2222 fr_strerror_const("Empty attribute reference");
2224 FR_SBUFF_ERROR_RETURN(&our_name);
2225 }
2226
2227 /*
2228 * '&' prefix is ignored.
2229 */
2230 (void) fr_sbuff_next_if_char(&our_name, '&');
2231
2232 /*
2233 * We parsed the tmpl as User-Name, but NOT %{User-Name}.
2234 */
2235 MEM(vpt = tmpl_alloc(ctx, TMPL_TYPE_ATTR, T_BARE_WORD, NULL, 0));
2236
2237 /*
2238 * The "raw." prefix marks up the leaf attribute
2239 * as unknown if it wasn't already which allows
2240 * users to stick whatever they want in there as
2241 * a value.
2242 */
2243 if (fr_sbuff_adv_past_strcase_literal(&our_name, "raw.")) is_raw = true;
2244
2245 /*
2246 * Parse one or more request references
2247 */
2249 &vpt->data.attribute.rr,
2250 &our_name,
2251 p_rules,
2252 t_rules,
2253 &namespace);
2254 if (ret < 0) {
2255 error:
2256 *out = NULL;
2258 FR_SBUFF_ERROR_RETURN(&our_name);
2259 }
2260
2261 fr_sbuff_marker(&m_l, &our_name);
2262
2263 /*
2264 * Parse the list and / or attribute reference
2265 */
2267 vpt,
2268 namespace, namespace,
2269 &our_name, p_rules, at_rules, 0);
2270 if (ret < 0) goto error;
2271
2272 /*
2273 * Check to see if the user wants the leaf
2274 * attribute to be raw.
2275 *
2276 * We can only do the conversion now _if_
2277 * the complete hierarchy has been resolved
2278 * otherwise we'll need to do the conversion
2279 * later.
2280 */
2281 if (tmpl_is_attr(vpt) && is_raw) tmpl_attr_to_raw(vpt);
2282
2283 /*
2284 * Local variables cannot be given a list modifier.
2285 */
2287 tmpl_attr_t *ar = tmpl_attr_list_head(tmpl_attr(vpt));
2288 bool is_local = ar->ar_da->flags.local;
2289
2290 for (; ar != NULL;
2291 ar = tmpl_attr_list_next(tmpl_attr(vpt), ar)) {
2292 if (!ar->ar_da->flags.local ||
2293 (ar->ar_da->flags.local && is_local)) continue;
2294
2295 fr_strerror_printf("Local attributes cannot be used in any list");
2297 fr_sbuff_set(&our_name, &m_l);
2298 goto error;
2299 }
2300
2301 /*
2302 * That being said, local variables are named "foo", but are always put into the local list.
2303 */
2304 if (is_local && (at_rules->list_presence != TMPL_ATTR_LIST_FORBID)) {
2305 MEM(ar = talloc(vpt, tmpl_attr_t));
2306 *ar = (tmpl_attr_t){
2307 .ar_type = TMPL_ATTR_TYPE_NORMAL,
2308 .ar_da = request_attr_local,
2309 .ar_parent = fr_dict_root(fr_dict_internal())
2310 };
2311
2312 /*
2313 * Prepend the local list ref so it gets evaluated
2314 * first.
2315 */
2316 tmpl_attr_list_insert_head(tmpl_attr(vpt), ar);
2317 }
2318 }
2319
2320 /*
2321 * Check whether the tmpl has a list qualifier.
2322 */
2323 switch (at_rules->list_presence) {
2325 break;
2326
2328 if (tmpl_attr_is_list_attr(tmpl_attr_list_head(tmpl_attr(vpt)))) {
2329 fr_strerror_const("List qualifiers are not allowed here.");
2331 goto error;
2332 }
2333 break;
2334
2336 if (!tmpl_attr_is_list_attr(tmpl_attr_list_head(tmpl_attr(vpt)))) {
2337 fr_strerror_const("List qualifier is required, but no list was found.");
2339 goto error;
2340 }
2341 break;
2342 }
2343
2344 /*
2345 * If we're using lists, ensure that the default list is specified.
2346 */
2347 if (!tmpl_attr_is_list_attr(tmpl_attr_list_head(tmpl_attr(vpt)))) {
2348 tmpl_attr_t *ar;
2349
2350 MEM(ar = talloc(vpt, tmpl_attr_t));
2351 *ar = (tmpl_attr_t){
2352 .ar_type = TMPL_ATTR_TYPE_NORMAL,
2353 .ar_parent = fr_dict_root(fr_dict_internal())
2354 };
2355
2356 fr_assert(at_rules->list_def);
2357 ar->ar_da = at_rules->list_def;
2358
2359 /*
2360 * Prepend the list ref so it gets evaluated
2361 * first.
2362 */
2363 tmpl_attr_list_insert_head(tmpl_attr(vpt), ar);
2364 }
2365
2366 tmpl_set_name(vpt, T_BARE_WORD, fr_sbuff_start(&our_name), fr_sbuff_used(&our_name));
2367 vpt->rules = *t_rules; /* Record the rules */
2368
2369 /*
2370 * If there are actual requests, duplicate them
2371 * and move them into the list.
2372 *
2373 * A NULL request_def pointer is equivalent to the
2374 * current request.
2375 */
2376 if (t_rules->attr.request_def) {
2377 tmpl_request_ref_list_acopy(vpt, &vpt->rules.attr.request_def, t_rules->attr.request_def);
2378 }
2379
2380 if (tmpl_is_attr(vpt)) {
2381 tmpl_attr_t *ar;
2382
2383 /*
2384 * Suppress useless casts.
2385 */
2387 vpt->rules.cast = FR_TYPE_NULL;
2388 }
2389
2390 /*
2391 * Ensure that the list is set correctly, so that
2392 * the returned vpt just doesn't just match the
2393 * input rules, it is also internally consistent.
2394 */
2395 ar = tmpl_attr_list_head(tmpl_attr(vpt));
2396 fr_assert(ar != NULL);
2397
2398 if (tmpl_attr_is_list_attr(ar)) vpt->rules.attr.list_def = ar->ar_da;
2399 }
2400
2401 if (!tmpl_substr_terminal_check(&our_name, p_rules)) {
2402 fr_strerror_const("Unexpected text after attribute reference");
2404 goto error;
2405 }
2406
2407 /*
2408 * If everything was resolved correctly
2409 * we now need to check the cast type.
2410 */
2411 if (!tmpl_needs_resolving(vpt) && !fr_type_is_null(t_rules->cast) &&
2412 !fr_type_cast(t_rules->cast, tmpl_attr_tail_da(vpt)->type)) {
2413 fr_strerror_printf("Cannot cast type '%s' to '%s'",
2416 fr_sbuff_set_to_start(&our_name);
2417 goto error;
2418 }
2419
2420 TMPL_VERIFY(vpt); /* Because we want to ensure we produced something sane */
2421
2422 *out = vpt;
2423 FR_SBUFF_SET_RETURN(name, &our_name);
2424}
2425
2426/** Parse a string into a TMPL_TYPE_ATTR_* type #tmpl_t
2427 *
2428 * @param[in,out] ctx to allocate #tmpl_t in.
2429 * @param[out] err May be NULL. Provides the exact error that the parser hit
2430 * when processing the attribute ref.
2431 * @param[out] out Where to write pointer to new #tmpl_t.
2432 * @param[in] name of attribute including #tmpl_request_ref_t and #fr_pair_list_t qualifiers.
2433 * @param[in] t_rules Rules which control parsing. See tmpl_afrom_attr_substr() for details.
2434 *
2435 * @note Unlike #tmpl_afrom_attr_substr this function will error out if the entire
2436 * name string isn't parsed.
2437 */
2439 tmpl_t **out, char const *name, tmpl_rules_t const *t_rules)
2440{
2441 ssize_t slen, name_len;
2443
2445
2446 name_len = strlen(name);
2447 slen = tmpl_afrom_attr_substr(ctx, err, out, &FR_SBUFF_IN(name, name_len), NULL, t_rules);
2448 if (slen <= 0) return slen;
2449
2450 if (!fr_cond_assert(*out)) return -1;
2451
2452 if (slen != name_len) {
2453 /* This looks wrong, but it produces meaningful errors for unknown attrs */
2454 fr_strerror_printf("Unexpected text after %s",
2455 tmpl_type_to_str((*out)->type));
2456 return -slen;
2457 }
2458
2459 TMPL_VERIFY(*out);
2460
2461 return slen;
2462}
2463
2464/** Create TMPL_TYPE_DATA from a string
2465 *
2466 * @param[in] ctx to allocate tmpl to.
2467 * @param[out] out where to write tmpl.
2468 * @param[in] in sbuff to parse.
2469 * @param[in] quote surrounding the operand to parse.
2470 * @param[in] t_rules specifying the cast and any enumeration values.
2471 * @param[in] allow_enum Whether parsing the value as an enum should be allowed.
2472 * @param[in] p_rules formatting rules.
2473 * @return
2474 * - <0 on error
2475 * - >=0 on success.
2476 */
2478 fr_token_t quote,
2479 tmpl_rules_t const *t_rules, bool allow_enum,
2480 fr_sbuff_parse_rules_t const *p_rules)
2481{
2482 fr_sbuff_t our_in = FR_SBUFF(in);
2483 fr_value_box_t tmp;
2484 tmpl_t *vpt;
2486
2487 if (!fr_type_is_null(t_rules->cast)) cast = t_rules->cast;
2488
2489 if (!fr_type_is_leaf(cast)) {
2490 fr_strerror_printf("%s is not a valid cast type",
2491 fr_type_to_str(cast));
2492 FR_SBUFF_ERROR_RETURN(&our_in);
2493 }
2494
2495 vpt = tmpl_alloc_null(ctx);
2496 if (fr_value_box_from_substr(vpt, &tmp,
2497 cast, allow_enum ? t_rules->enumv : NULL,
2498 &our_in, p_rules) < 0) {
2500 FR_SBUFF_ERROR_RETURN(&our_in);
2501 }
2503
2504 tmpl_init(vpt, TMPL_TYPE_DATA, quote, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in), t_rules);
2505
2507
2508 *out = vpt;
2509
2510 if (cast == tmpl_value_type(vpt)) vpt->rules.cast = FR_TYPE_NULL;
2511
2513
2514 FR_SBUFF_SET_RETURN(in, &our_in);
2515}
2516
2517/** Parse a truth value
2518 *
2519 * @param[in] ctx to allocate tmpl to.
2520 * @param[out] out where to write tmpl.
2521 * @param[in] in sbuff to parse.
2522 * @param[in] p_rules formatting rules.
2523 * @return
2524 * - < 0 sbuff does not contain a boolean value.
2525 * - > 0 how many bytes were parsed.
2526 */
2528 fr_sbuff_parse_rules_t const *p_rules)
2529{
2530 fr_sbuff_t our_in = FR_SBUFF(in);
2531 bool a_bool;
2532 tmpl_t *vpt;
2533
2534 if (fr_sbuff_out(NULL, &a_bool, &our_in) < 0) {
2535 fr_strerror_const("Not a boolean value");
2536 return 0;
2537 }
2538
2539 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2540 fr_strerror_const("Unexpected text after bool");
2542 }
2543
2545
2546 fr_value_box_init(&vpt->data.literal, FR_TYPE_BOOL, NULL, false);
2547 vpt->data.literal.vb_bool = a_bool;
2548
2549 *out = vpt;
2550
2551 FR_SBUFF_SET_RETURN(in, &our_in);
2552}
2553
2554/** Parse bareword as an octet string
2555 *
2556 * @param[in] ctx to allocate tmpl to.
2557 * @param[out] out where to write tmpl.
2558 * @param[in] in sbuff to parse.
2559 * @param[in] p_rules formatting rules.
2560 * @return
2561 * - < 0 negative offset where parse error occurred.
2562 * - 0 sbuff does not contain a hex string.
2563 * - > 0 how many bytes were parsed.
2564 */
2566 fr_sbuff_parse_rules_t const *p_rules)
2567{
2568 fr_sbuff_t our_in = FR_SBUFF(in);
2569 tmpl_t *vpt;
2570 char *hex;
2571 size_t binlen, len;
2572 uint8_t *bin;
2573
2574 if (!fr_sbuff_adv_past_strcase_literal(&our_in, "0x")) return 0;
2575
2576 MEM(vpt = tmpl_alloc(ctx, TMPL_TYPE_DATA, T_BARE_WORD, NULL, 0));
2577
2578 /*
2579 * This allows stream parsing to work correctly
2580 * we could be less lazy and copy hex data in
2581 * chunks, but never mind...
2582 */
2583 len = fr_sbuff_out_abstrncpy_allowed(vpt, &hex, &our_in, SIZE_MAX, sbuff_char_class_hex);
2584 if (len & 0x01) {
2585 fr_strerror_const("Hex string not even length");
2586 error:
2588 FR_SBUFF_ERROR_RETURN(&our_in);
2589 }
2590 if (len == 0) {
2591 fr_strerror_const("Zero length hex string is invalid");
2592 goto error;
2593 }
2594
2595 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2596 fr_strerror_const("Unexpected text after hex string");
2597 goto error;
2598 }
2599
2600 bin = (uint8_t *)hex;
2601 binlen = len / 2;
2602
2604
2605 (void)fr_base16_decode(NULL, &FR_DBUFF_TMP(bin, binlen), &FR_SBUFF_IN(hex, len), false);
2606 MEM(bin = talloc_realloc_size(vpt, bin, binlen)); /* Realloc to the correct length */
2607 (void)fr_value_box_memdup_shallow(&vpt->data.literal, NULL, bin, binlen, false);
2608
2609 *out = vpt;
2610
2611 FR_SBUFF_SET_RETURN(in, &our_in);
2612}
2613
2614/** Parse bareword as an IPv4 address or prefix
2615 *
2616 * @param[in] ctx to allocate tmpl to.
2617 * @param[out] out where to write tmpl.
2618 * @param[in] in sbuff to parse.
2619 * @param[in] p_rules formatting rules.
2620 * @return
2621 * - < 0 sbuff does not contain an IPv4 address or prefix.
2622 * - > 0 how many bytes were parsed.
2623 */
2625 fr_sbuff_parse_rules_t const *p_rules)
2626{
2627 tmpl_t *vpt;
2628 fr_sbuff_t our_in = FR_SBUFF(in);
2630 int count;
2631 uint32_t ipaddr;
2632 uint8_t addr[4] = {}, prefix = 32;
2633
2634 for (count = 0; count < 4; count++) {
2635 if (!fr_sbuff_out(NULL, &addr[count], &our_in)) FR_SBUFF_ERROR_RETURN(&our_in);
2636
2637 if (count == 3) break;
2638
2639 if (fr_sbuff_next_if_char(&our_in, '.')) continue;
2640
2641 if (!fr_sbuff_is_char(&our_in, '/')) FR_SBUFF_ERROR_RETURN(&our_in);
2642 }
2643
2644 /*
2645 * If it has a trailing '/' then it's an IP prefix.
2646 */
2647 if (fr_sbuff_next_if_char(&our_in, '/')) {
2648 if (fr_sbuff_out(NULL, &prefix, &our_in) < 0) {
2649 fr_strerror_const("IPv4 CIDR mask malformed");
2650 FR_SBUFF_ERROR_RETURN(&our_in);
2651 }
2652
2653 if (prefix > 32) {
2654 fr_strerror_const("IPv4 CIDR mask too high");
2655 FR_SBUFF_ERROR_RETURN(&our_in);
2656 }
2657
2659 } else {
2661 }
2662
2663 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2664 fr_strerror_const("Unexpected text after IPv4 string or prefix");
2665 FR_SBUFF_ERROR_RETURN(&our_in);
2666 }
2667
2669 fr_value_box_init(&vpt->data.literal, type, NULL, false);
2670 vpt->data.literal.vb_ip.af = AF_INET;
2671 vpt->data.literal.vb_ip.prefix = prefix;
2672
2673 /*
2674 * Zero out lower bits
2675 */
2676 ipaddr = (((uint32_t) addr[0]) << 24) | (((uint32_t) addr[1]) << 16) | (((uint32_t) addr[2]) << 8) | addr[3];
2677 if (prefix < 32) {
2678 ipaddr &= ~((uint32_t) 0) << (32 - prefix);
2679 }
2680 vpt->data.literal.vb_ip.addr.v4.s_addr = htonl(ipaddr);
2681
2682 *out = vpt;
2683
2684 FR_SBUFF_SET_RETURN(in, &our_in);
2685}
2686
2687/** Parse bareword as an IPv6 address or prefix
2688 *
2689 * @param[in] ctx to allocate tmpl to.
2690 * @param[out] out where to write tmpl.
2691 * @param[in] in sbuff to parse.
2692 * @param[in] p_rules formatting rules.
2693 * @return
2694 * - < 0 sbuff does not contain an IPv4 address or prefix.
2695 * - > 0 how many bytes were parsed.
2696 */
2698 fr_sbuff_parse_rules_t const *p_rules)
2699{
2700 tmpl_t *vpt;
2701 fr_sbuff_t our_in = FR_SBUFF(in);
2704 size_t len;
2705 char *sep_a, *sep_b;
2706
2707 static bool ipv6_chars[UINT8_MAX + 1] = {
2708 ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true,
2709 ['5'] = true, ['6'] = true, ['7'] = true, ['8'] = true, ['9'] = true,
2710 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true,
2711 ['f'] = true,
2712 ['A'] = true, ['B'] = true, ['C'] = true, ['D'] = true, ['E'] = true,
2713 ['F'] = true,
2714 [':'] = true, ['.'] = true
2715 };
2716
2717 /*
2718 * Drop a marker to pin the start of the
2719 * address in the buffer.
2720 */
2721 fr_sbuff_marker(&m, &our_in);
2722
2723 /*
2724 * Check for something looking like an IPv6 address
2725 *
2726 * Minimum string is '::'
2727 */
2728 len = fr_sbuff_adv_past_allowed(&our_in, FR_IPADDR_STRLEN + 1, ipv6_chars, NULL);
2729 if ((len < 2) || (len > FR_IPADDR_STRLEN)) {
2730 error:
2731 FR_SBUFF_ERROR_RETURN(&our_in);
2732 }
2733
2734 /*
2735 * Got ':' after '.', this isn't allowed.
2736 *
2737 * We need this check else IPv4 gets parsed
2738 * as blank IPv6 address.
2739 */
2740 sep_a = memchr(fr_sbuff_current(&m), '.', len);
2741 if (sep_a && (!(sep_b = memchr(fr_sbuff_current(&m), ':', len)) || (sep_b > sep_a))) {
2742 fr_strerror_const("First IPv6 component separator was a '.'");
2743 goto error;
2744 }
2745
2746 /*
2747 * The v6 parse function will happily turn
2748 * integers into v6 addresses *sigh*.
2749 */
2750 sep_a = memchr(fr_sbuff_current(&m), ':', len);
2751 if (!sep_a) {
2752 fr_strerror_const("No IPv6 component separator");
2753 goto error;
2754 }
2755
2756 /*
2757 * Handle scope
2758 */
2759 if (fr_sbuff_next_if_char(&our_in, '%')) {
2760 len = fr_sbuff_adv_until(&our_in, IFNAMSIZ + 1, p_rules->terminals, '\0');
2761 if ((len < 1) || (len > IFNAMSIZ)) {
2762 fr_strerror_const("IPv6 scope too long");
2763 goto error;
2764 }
2765 }
2766
2767 /*
2768 * ...and finally the prefix.
2769 */
2770 if (fr_sbuff_next_if_char(&our_in, '/')) {
2771 uint8_t mask;
2772
2773 if (fr_sbuff_out(NULL, &mask, &our_in) < 0) {
2774 fr_strerror_const("IPv6 CIDR mask malformed");
2775 goto error;
2776 }
2777 if (mask > 128) {
2778 fr_strerror_const("IPv6 CIDR mask too high");
2779 goto error;
2780 }
2781
2783 } else {
2785 }
2786
2787 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2788 fr_strerror_const("Unexpected text after IPv6 string or prefix");
2789 goto error;
2790 }
2791
2793 if (fr_value_box_from_substr(vpt, &vpt->data.literal, type, NULL,
2794 &FR_SBUFF_REPARSE(&our_in),
2795 NULL) < 0) {
2797 goto error;
2798 }
2799 *out = vpt;
2800
2801 FR_SBUFF_SET_RETURN(in, &our_in);
2802}
2803
2804
2805/** Try and parse signed or unsigned integers
2806 *
2807 * @param[in] ctx to allocate tmpl to.
2808 * @param[out] out where to write tmpl.
2809 * @param[in] in sbuff to parse.
2810 * @param[in] p_rules formatting rules.
2811 * @return
2812 * - < 0 sbuff does not contain a mac address.
2813 * - > 0 how many bytes were parsed.
2814 */
2816 fr_sbuff_parse_rules_t const *p_rules)
2817{
2818 tmpl_t *vpt;
2819 fr_sbuff_t our_in = FR_SBUFF(in);
2820 uint8_t buff[6] = {};
2821 fr_dbuff_t dbuff;
2822 fr_value_box_t *vb;
2824
2825 fr_dbuff_init(&dbuff, buff, sizeof(buff));
2826
2827 fr_base16_decode(&err, &dbuff, &our_in, true);
2828 if (err != FR_SBUFF_PARSE_OK) return 0;
2829
2830 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
2831
2832 fr_base16_decode(&err, &dbuff, &our_in, true);
2833 if (err != FR_SBUFF_PARSE_OK) return 0;
2834
2835 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
2836
2837 fr_base16_decode(&err, &dbuff, &our_in, true);
2838 if (err != FR_SBUFF_PARSE_OK) return 0;
2839
2840 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
2841
2842 fr_base16_decode(&err, &dbuff, &our_in, true);
2843 if (err != FR_SBUFF_PARSE_OK) return 0;
2844
2845 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
2846
2847 fr_base16_decode(&err, &dbuff, &our_in, true);
2848 if (err != FR_SBUFF_PARSE_OK) return 0;
2849
2850 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
2851
2852 fr_base16_decode(&err, &dbuff, &our_in, true);
2853 if (err != FR_SBUFF_PARSE_OK) return 0;
2854
2855 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2856 fr_strerror_const("Unexpected text after mac address");
2857 return 0;
2858 }
2859
2861 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
2862 vb = tmpl_value(vpt);
2863
2864 fr_value_box_init(vb, FR_TYPE_ETHERNET, NULL, false);
2865 memcpy(vb->vb_ether, buff, sizeof(vb->vb_ether));
2866
2867 *out = vpt;
2868
2869 FR_SBUFF_SET_RETURN(in, &our_in);
2870}
2871
2872/** Try and parse signed or unsigned integers
2873 *
2874 * @param[in] ctx to allocate tmpl to.
2875 * @param[out] out where to write tmpl.
2876 * @param[in] in sbuff to parse.
2877 * @param[in] p_rules formatting rules.
2878 * @return
2879 * - < 0 sbuff does not contain an integer.
2880 * - > 0 how many bytes were parsed.
2881 */
2883 fr_sbuff_parse_rules_t const *p_rules)
2884{
2885 tmpl_t *vpt;
2886 fr_sbuff_t our_in = FR_SBUFF(in);
2887 ssize_t slen;
2888 fr_value_box_t *vb;
2889
2890 /*
2891 * Pick the narrowest signed type
2892 */
2893 if (fr_sbuff_is_char(&our_in, '-')) {
2894 int64_t a_int;
2895
2896 slen = fr_sbuff_out(NULL, &a_int, &our_in);
2897 if (slen <= 0) return 0;
2898
2899 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2900 fr_strerror_const("Unexpected text after signed integer");
2901 error:
2902 FR_SBUFF_ERROR_RETURN(&our_in);
2903 }
2904
2906 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
2907 vb = tmpl_value(vpt);
2908 if (a_int >= INT8_MIN) {
2909 fr_value_box_init(vb, FR_TYPE_INT8, NULL, false);
2910 vb->vb_int8 = (int8_t)a_int;
2911 } else if (a_int >= INT16_MIN) {
2912 fr_value_box_init(vb, FR_TYPE_INT16, NULL, false);
2913 vb->vb_int16 = (int16_t)a_int;
2914 } else if (a_int >= INT32_MIN) {
2915 fr_value_box_init(vb, FR_TYPE_INT32, NULL, false);
2916 vb->vb_int32 = (int32_t)a_int;
2917 } else {
2918 fr_value_box_init(vb, FR_TYPE_INT64, NULL, false);
2919 vb->vb_int64 = (int64_t)a_int;
2920 }
2921 /*
2922 * Pick the narrowest unsigned type
2923 */
2924 } else {
2925 uint64_t a_uint;
2926
2927 slen = fr_sbuff_out(NULL, &a_uint, &our_in);
2928 if (slen <= 0) return slen;
2929
2930 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2931 fr_strerror_const("Unexpected text after unsigned integer");
2932 goto error;
2933 }
2934
2936 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
2937 vb = tmpl_value(vpt);
2938 if (a_uint <= UINT8_MAX) {
2939 fr_value_box_init(vb, FR_TYPE_UINT8, NULL, false);
2940 vb->vb_uint8 = (uint8_t)a_uint;
2941 } else if (a_uint <= UINT16_MAX) {
2942 fr_value_box_init(vb, FR_TYPE_UINT16, NULL, false);
2943 vb->vb_uint16 = (uint16_t)a_uint;
2944 } else if (a_uint <= UINT32_MAX) {
2945 fr_value_box_init(vb, FR_TYPE_UINT32, NULL, false);
2946 vb->vb_uint32 = (uint32_t)a_uint;
2947 } else {
2948 fr_value_box_init(vb, FR_TYPE_UINT64, NULL, false);
2949 vb->vb_uint64 = (uint64_t)a_uint;
2950 }
2951 }
2952
2953 *out = vpt;
2954
2955 FR_SBUFF_SET_RETURN(in, &our_in);
2956}
2957
2959 fr_sbuff_parse_rules_t const *p_rules)
2960{
2961 tmpl_t *vpt;
2962 fr_sbuff_t our_in = FR_SBUFF(in);
2963 double a_float;
2964 ssize_t slen;
2965 fr_value_box_t *vb;
2966
2967 slen = fr_sbuff_out(NULL, &a_float, &our_in);
2968 if (slen <= 0) return 0;
2969
2970 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2971 fr_strerror_const("Unexpected text after float");
2972 FR_SBUFF_ERROR_RETURN(&our_in);
2973 }
2974
2976 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
2977 vb = tmpl_value(vpt);
2978 fr_value_box_init(vb, FR_TYPE_FLOAT64, NULL, false);
2979 vb->vb_float64 = a_float;
2980
2981 *out = vpt;
2982
2983 FR_SBUFF_SET_RETURN(in, &our_in);
2984}
2985
2987 fr_sbuff_parse_rules_t const *p_rules)
2988{
2989 tmpl_t *vpt;
2990 fr_sbuff_t our_in = FR_SBUFF(in);
2991 fr_time_delta_t a_delta;
2992 fr_slen_t slen;
2993 fr_value_box_t *vb;
2994
2995 slen = fr_time_delta_from_substr(&a_delta, &our_in, FR_TIME_RES_SEC, true, p_rules ? p_rules->terminals : NULL);
2996 if (slen <= 0) return 0;
2997
2999 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
3000 vb = tmpl_value(vpt);
3001 fr_value_box_init(vb, FR_TYPE_TIME_DELTA, NULL, false);
3002 vb->vb_time_delta = a_delta;
3003
3004 *out = vpt;
3005
3006 FR_SBUFF_SET_RETURN(in, &our_in);
3007}
3008
3009/*
3010 * ::value
3011 *
3012 * Treated as enum name. Note that this check MUST be done after the test for IPv6, as
3013 * "::1" is an allowed IPv6 address.
3014 *
3015 * @todo - Mark this up as an enum name? Or do we really care? Maybe we want to allow
3016 *
3017 * Service-Type == 'Framed-User'
3018 *
3019 * or
3020 *
3021 * Service-Type == "Framed-User'
3022 *
3023 * as the second one allows for xlat expansions of enum names.
3024 *
3025 * We probably do want to forbid the single-quoted form of enums,
3026 * as that doesn't seem to make sense.
3027 *
3028 * We also need to distinguish unresolved bare words as enums
3029 * (with :: prefix) from unresolved attributes without an & prefix.
3030 */
3031static ssize_t tmpl_afrom_enum(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in,
3032 fr_sbuff_parse_rules_t const *p_rules,
3033 tmpl_rules_t const *t_rules)
3034{
3035 tmpl_t *vpt;
3036 char *str;
3038 fr_sbuff_t our_in = FR_SBUFF(in);
3039
3040 /*
3041 * If there isn't a "::" prefix, then check for migration flags, and enum.
3042 *
3043 * If we require an enum prefix, then the input can't be an enum, and we don't do any more
3044 * parsing.
3045 *
3046 * Otherwise if there's no prefix and no enumv, we know this input can't be an enum name.
3047 */
3048 if (!fr_sbuff_adv_past_str_literal(&our_in, "::")) {
3049 return 0;
3050
3051 } else if (t_rules->enumv &&
3052 ((t_rules->enumv->type == FR_TYPE_IPV6_ADDR) ||
3053 ((t_rules->enumv->type == FR_TYPE_IPV6_PREFIX)))) {
3054
3055 /*
3056 * We can't have enumerated names for IPv6 addresses.
3057 *
3058 * @todo - allow them ONLY if the RHS string is a valid enum name.
3059 */
3060 return 0;
3061 }
3062
3063 vpt = tmpl_alloc_null(ctx);
3064
3065 /*
3066 * If it doesn't match any other type of bareword, parse it as an enum name.
3067 *
3068 * Note that we don't actually try to resolve the enum name. The caller is responsible
3069 * for doing that.
3070 */
3071 if (fr_dict_enum_name_afrom_substr(vpt, &str, &sberr, &our_in, p_rules ? p_rules->terminals : NULL) < 0) {
3072 /*
3073 * Produce our own errors which make
3074 * more sense in the context of tmpls
3075 */
3076 switch (sberr) {
3078 fr_strerror_const("No operand found. Expected &ref, literal, "
3079 "'quoted literal', \"%{expansion}\", or enum value");
3080 break;
3081
3083 fr_strerror_const("enum values must contain at least one alpha character");
3084 break;
3085
3086 default:
3087 fr_strerror_const("Unexpected text after enum value.");
3088 break;
3089 }
3090
3092 FR_SBUFF_ERROR_RETURN(&our_in);
3093 }
3094
3095 /*
3096 * If there's a valid enum name, then we use it. Otherwise we leave name resolution to run time.
3097 */
3098 if (t_rules->enumv) {
3099 fr_dict_enum_value_t const *dv;
3100
3101 dv = fr_dict_enum_by_name(t_rules->enumv, str, -1);
3102 if (dv) {
3104 fr_sbuff_start(&our_in), fr_sbuff_used(&our_in), t_rules);
3105 (void) fr_value_box_copy(vpt, &vpt->data.literal, dv->value);
3106 vpt->data.literal.enumv = t_rules->enumv;
3107
3108 *out = vpt;
3109 FR_SBUFF_SET_RETURN(in, &our_in);
3110 }
3111 }
3112
3113 /*
3114 * Either there's no enum, or the enum name didn't match one of the listed ones. There's no
3115 * point in waiting for an enum which might be declared later. That's not possible, so we fall
3116 * back to parsing the various data types.
3117 */
3118 if (t_rules->at_runtime) return 0;
3119
3121 fr_sbuff_start(&our_in), fr_sbuff_used(&our_in), t_rules);
3122 vpt->data.unescaped = str;
3123 *out = vpt;
3124
3125 FR_SBUFF_SET_RETURN(in, &our_in);
3126}
3127
3128/** Convert an arbitrary string into a #tmpl_t
3129 *
3130 * @note Unlike #tmpl_afrom_attr_str return code 0 doesn't necessarily indicate failure,
3131 * may just mean a 0 length string was parsed. Check to see if the function emitted
3132 * a #tmpl_t in *out.
3133 *
3134 * @note xlats and regexes are left uncompiled. This is to support the two pass parsing
3135 * done by the modcall code. Compilation on pass1 of that code could fail, as
3136 * attributes or xlat functions registered by modules may not be available (yet).
3137 *
3138 * @note For details of attribute parsing see #tmpl_afrom_attr_substr.
3139 *
3140 * @param[in,out] ctx To allocate #tmpl_t in.
3141 * @param[out] out Where to write the pointer to the new #tmpl_t.
3142 * @param[in] in String to parse.
3143 * @param[in] quote Quoting around the tmpl. Determines what we
3144 * attempt to parse the string as.
3145 * @param[in] p_rules Formatting rules for the tmpl.
3146 * @param[in] t_rules Validation rules for attribute references.
3147 * @return
3148 * - < 0 on error (offset as negative integer)
3149 * - >= 0 on success (number of bytes parsed).
3150 *
3151 * @see REMARKER to produce pretty error markers from the return value.
3152 *
3153 * @see tmpl_afrom_attr_substr
3154 */
3156 fr_sbuff_t *in, fr_token_t quote,
3157 fr_sbuff_parse_rules_t const *p_rules,
3158 tmpl_rules_t const *t_rules)
3159{
3160 fr_sbuff_t our_in = FR_SBUFF(in);
3161
3162 fr_slen_t slen;
3164 char *str;
3165
3166 tmpl_t *vpt = NULL;
3168
3170
3171 *out = NULL;
3172
3173 switch (quote) {
3174 case T_BARE_WORD:
3175 /*
3176 * Skip other bareword types if
3177 * we find a '&' prefix.
3178 */
3179 if (fr_sbuff_is_char(&our_in, '&')) return tmpl_afrom_attr_substr(ctx, NULL, out, in,
3180 p_rules, t_rules);
3181
3182 /*
3183 * Allow bareword xlats if we
3184 * find a '%' prefix.
3185 */
3186 if (fr_sbuff_is_char(&our_in, '%')) {
3188 xlat_exp_head_t *head = NULL;
3189
3190 vpt = tmpl_alloc_null(ctx);
3191 slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
3192 if (slen <= 0) FR_SBUFF_ERROR_RETURN(&our_in);
3193
3196 goto set_tmpl;
3197
3198 } else if (fr_dlist_num_elements(&head->dlist) == 1) {
3199 xlat_exp_t *node = xlat_exp_head(head);
3200 tmpl_t *hoisted;
3201
3202 if (node->type != XLAT_TMPL) goto set_tmpl;
3203
3204 /*
3205 * We were asked to parse a tmpl. But it turned out to be an xlat %{...}
3206 *
3207 * If that xlat is identically a tmpl such as %{User-Name}, then we just
3208 * hoist the tmpl to this node. Otherwise at run time, we will have an
3209 * extra bounce through the xlat code, for no real reason.
3210 */
3211 hoisted = node->vpt;
3212
3213 (void) talloc_steal(ctx, hoisted);
3215 vpt = hoisted;
3216
3217 } else {
3218 set_tmpl:
3219 tmpl_init(vpt, type, quote, fr_sbuff_start(&our_in), slen, t_rules);
3220 vpt->data.xlat.ex = head;
3221 }
3222
3223 *out = vpt;
3224
3226
3227 FR_SBUFF_SET_RETURN(in, &our_in);
3228 }
3229
3230 /*
3231 * Deal with explicit casts...
3232 */
3233 if (!fr_type_is_null(t_rules->cast)) return tmpl_afrom_value_substr(ctx, out, in, quote,
3234 t_rules, true, p_rules);
3235
3236 /*
3237 * We're at runtime and have a data type. Just parse it as that data type, without doing
3238 * endless "maybe it's this thing" attempts.
3239 */
3240 if (t_rules->at_runtime && t_rules->enumv) {
3241 tmpl_rules_t my_t_rules = *t_rules;
3242
3243 fr_assert(fr_type_is_leaf(t_rules->enumv->type));
3244
3245 my_t_rules.cast = my_t_rules.enumv->type;
3246
3247 return tmpl_afrom_value_substr(ctx, out, in, quote, &my_t_rules, true, p_rules);
3248 }
3249
3250 /*
3251 * Prefer enum names to IPv6 addresses.
3252 */
3253 if (t_rules->enumv && fr_sbuff_is_str_literal(&our_in, "::")) {
3254 slen = tmpl_afrom_enum(ctx, out, &our_in, p_rules, t_rules);
3255 if (slen > 0) goto done_bareword;
3256 fr_assert(!*out);
3257 }
3258
3259 /*
3260 * See if it's a boolean value
3261 */
3262 slen = tmpl_afrom_bool_substr(ctx, out, &our_in, p_rules);
3263 if (slen > 0) {
3264 done_bareword:
3265 TMPL_VERIFY(*out);
3266
3267 FR_SBUFF_SET_RETURN(in, &our_in);
3268 }
3269 fr_assert(!*out);
3270
3271 /*
3272 * See if it's an octets string
3273 */
3274 slen = tmpl_afrom_octets_substr(ctx, out, &our_in, p_rules);
3275 if (slen > 0) goto done_bareword;
3276 fr_assert(!*out);
3277
3278 /*
3279 * See if it's a mac address
3280 *
3281 * Needs to be before IPv6 as the pton functions
3282 * are too greedy, and on macOS will happily
3283 * convert a mac address to an IPv6 address.
3284 */
3285 slen = tmpl_afrom_ether_substr(ctx, out, &our_in, p_rules);
3286 if (slen > 0) goto done_bareword;
3287 fr_assert(!*out);
3288
3289 /*
3290 * See if it's an IPv4 address or prefix
3291 */
3292 slen = tmpl_afrom_ipv4_substr(ctx, out, &our_in, p_rules);
3293 if (slen > 0) goto done_bareword;
3294 fr_assert(!*out);
3295
3296 /*
3297 * See if it's an IPv6 address or prefix
3298 */
3299 slen = tmpl_afrom_ipv6_substr(ctx, out, &our_in, p_rules);
3300 if (slen > 0) goto done_bareword;
3301 fr_assert(!*out);
3302
3303 slen = tmpl_afrom_enum(ctx, out, &our_in, p_rules, t_rules);
3304 if (slen > 0) goto done_bareword;
3305 fr_assert(!*out);
3306
3307 /*
3308 * See if it's a integer
3309 */
3310 slen = tmpl_afrom_integer_substr(ctx, out, &our_in, p_rules);
3311 if (slen > 0) goto done_bareword;
3312 fr_assert(!*out);
3313
3314 /*
3315 * See if it's a float
3316 */
3317 slen = tmpl_afrom_float_substr(ctx, out, &our_in, p_rules);
3318 if (slen > 0) goto done_bareword;
3319 fr_assert(!*out);
3320
3321 /*
3322 * See if it's a time delta
3323 *
3324 * We do this after floats and integers so that
3325 * they get parsed as integer and float types
3326 * and not time deltas.
3327 */
3328 slen = tmpl_afrom_time_delta(ctx, out, &our_in, p_rules);
3329 if (slen > 0) goto done_bareword;
3330 fr_assert(!*out);
3331
3332 /*
3333 * See if it's an attribute reference
3334 * without the prefix.
3335 */
3336 slen = tmpl_afrom_attr_substr(ctx, NULL, out, &our_in, p_rules, t_rules);
3337 if (slen > 0) goto done_bareword;
3338 fr_assert(!*out);
3339
3340 /*
3341 * We can't parse it as anything, that's an error.
3342 *
3343 * But it may be an enumeration value for an
3344 * attribute which is loaded later. In which
3345 * case we allow parsing the enumeration.
3346 */
3347 if (!fr_sbuff_is_str_literal(&our_in, "::")) {
3348 /*
3349 * Return the error string from parsing the attribute!
3350 */
3351 FR_SBUFF_ERROR_RETURN(&our_in);
3352 }
3353
3354 /*
3355 * Attempt to resolve enumeration values
3356 */
3357 vpt = tmpl_alloc_null(ctx);
3358
3359 /*
3360 * If it doesn't match any other type of bareword, parse it as an enum name.
3361 *
3362 * Note that we don't actually try to resolve the enum name. The caller is responsible
3363 * for doing that.
3364 */
3365 if (fr_dict_enum_name_afrom_substr(vpt, &str, &sberr, &our_in, p_rules ? p_rules->terminals : NULL) < 0) {
3366 /*
3367 * Produce our own errors which make
3368 * more sense in the context of tmpls
3369 */
3370 switch (sberr) {
3372 fr_strerror_const("No operand found. Expected &ref, literal, "
3373 "'quoted literal', \"%{expansion}\", or enum value");
3374 break;
3375
3377 fr_strerror_const("enum values must contain at least one alpha character");
3378 break;
3379
3380 default:
3381 fr_strerror_const("Unexpected text after enum value.");
3382 break;
3383 }
3384
3386 FR_SBUFF_ERROR_RETURN(&our_in);
3387 }
3388
3390 fr_sbuff_start(&our_in), fr_sbuff_used(&our_in), t_rules);
3391 vpt->data.unescaped = str;
3392 *out = vpt;
3393
3394 FR_SBUFF_SET_RETURN(in, &our_in);
3395
3397 /*
3398 * Single quoted strings can be cast
3399 * to a specific data type immediately
3400 * as they cannot contain expansions.
3401 */
3402 if (!fr_type_is_null(t_rules->cast)) return tmpl_afrom_value_substr(ctx, out, in, quote,
3403 t_rules, false,
3404 p_rules);
3405 vpt = tmpl_alloc_null(ctx);
3406 slen = fr_sbuff_out_aunescape_until(vpt, &str, &our_in, SIZE_MAX,
3407 p_rules ? p_rules->terminals : NULL,
3408 p_rules ? p_rules->escapes : NULL);
3409 tmpl_init(vpt, TMPL_TYPE_DATA_UNRESOLVED, quote, fr_sbuff_start(&our_in), slen, t_rules);
3410 vpt->data.unescaped = str;
3411 break;
3412
3414 {
3415 xlat_exp_head_t *head = NULL;
3417
3418 vpt = tmpl_alloc_null(ctx);
3419
3420 slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
3421 if (slen < 0) FR_SBUFF_ERROR_RETURN(&our_in);
3422
3423 /*
3424 * If the string doesn't contain an xlat,
3425 * and we want to cast it as a specific
3426 * type, then do the conversion now.
3427 */
3428 if (xlat_is_literal(head)) {
3429 if (!fr_type_is_null(t_rules->cast)) {
3430 talloc_free(vpt); /* Also frees any nodes */
3431
3432 return tmpl_afrom_value_substr(ctx, out,
3433 in, quote,
3434 t_rules, false, p_rules);
3435 }
3436
3437 /*
3438 * If the string doesn't contain an xlat
3439 * and there's no cast, we just store
3440 * the string for conversion later.
3441 */
3442 if (xlat_to_string(vpt, &str, &head)) {
3443 TALLOC_FREE(head);
3444
3446 fr_sbuff_start(&our_in), slen, t_rules);
3447 vpt->data.unescaped = str; /* Store the unescaped string for parsing later */
3448 break;
3449 }
3450 }
3451
3452 /*
3453 * If the string actually contains an xlat
3454 * store the compiled xlat.
3455 */
3457
3458 tmpl_init(vpt, type, quote, fr_sbuff_start(&our_in), slen, t_rules);
3459 vpt->data.xlat.ex = head;
3460 }
3461 break;
3462
3464 {
3466 xlat_exp_head_t *head = NULL;
3467
3468 vpt = tmpl_alloc_null(ctx);
3469
3470 /*
3471 * Ensure that we pre-parse the exec string.
3472 * This allows us to catch parse errors as early
3473 * as possible.
3474 *
3475 * FIXME - We need an ephemeral version of this
3476 * too.
3477 */
3478 slen = xlat_tokenize_argv(vpt, &head, &our_in, NULL, p_rules, t_rules, true);
3479 if ((slen <= 0) || !head) {
3481 FR_SBUFF_ERROR_RETURN(&our_in);
3482 }
3483
3484 /*
3485 * Ensure any xlats produced are bootstrapped
3486 * so that their instance data will be created.
3487 */
3488 if (xlat_finalize(head, t_rules->xlat.runtime_el) < 0) {
3489 fr_strerror_const("Failed to bootstrap xlat");
3490 FR_SBUFF_ERROR_RETURN(&our_in);
3491 }
3492
3494
3495 tmpl_init(vpt, type, quote, fr_sbuff_start(&our_in), slen, t_rules);
3496 vpt->data.xlat.ex = head;
3497 }
3498 break;
3499
3501 {
3502 xlat_exp_head_t *head = NULL;
3504 tmpl_rules_t arg_t_rules = *t_rules;
3505
3506 arg_t_rules.literals_safe_for = FR_REGEX_SAFE_FOR;
3507
3508 if (!fr_type_is_null(t_rules->cast)) {
3509 fr_strerror_const("Casts cannot be used with regular expressions");
3510 fr_sbuff_set_to_start(&our_in); /* Point to the cast */
3511 FR_SBUFF_ERROR_RETURN(&our_in);
3512 }
3513
3514 vpt = tmpl_alloc_null(ctx);
3515
3516 slen = xlat_tokenize(vpt, &head, &our_in, p_rules, &arg_t_rules);
3517 if (slen < 0) FR_SBUFF_ERROR_RETURN(&our_in);
3518
3519 /*
3520 * Check if the string actually contains an xlat
3521 * if it doesn't, we unfortunately still
3522 * can't compile the regex here, as we don't know if
3523 * it should be ephemeral or what flags should be used
3524 * during the compilation.
3525 *
3526 * The caller will need to do the compilation after we
3527 * return.
3528 */
3529 if (xlat_to_string(vpt, &str, &head)) {
3531 fr_sbuff_start(&our_in), slen, t_rules);
3532 vpt->data.unescaped = str; /* Store the unescaped string for compilation later */
3533 break;
3534 }
3535 /*
3536 * Mark the regex up as a regex-xlat which
3537 * will need expanding before evaluation, and can never
3538 * be pre-compiled.
3539 */
3541
3542 tmpl_init(vpt, type, quote, fr_sbuff_start(&our_in), slen, t_rules);
3543 vpt->data.xlat.ex = head;
3544 }
3545 break;
3546
3547 default:
3548 fr_assert_msg(0, "Unknown quote type %i", quote);
3549 FR_SBUFF_ERROR_RETURN(&our_in);
3550 }
3551
3553 *out = vpt;
3554
3555 FR_SBUFF_SET_RETURN(in, &our_in);
3556}
3557
3558/** Copy a tmpl
3559 *
3560 * Fully duplicates the contents of a tmpl including any nested attribute
3561 * references.
3562 *
3563 * @param[in] ctx to perform allocations under.
3564 * @param[in] in tmpl to duplicate.
3565 * @return
3566 * - NULL on error.
3567 * - A new tmpl on success.
3568 */
3569tmpl_t *tmpl_copy(TALLOC_CTX *ctx, tmpl_t const *in)
3570{
3571 tmpl_t *vpt;
3572
3573 MEM(vpt = tmpl_alloc(ctx, in->type, in->quote, in->name, in->len));
3574 vpt->rules = in->rules;
3575
3576 /*
3577 * Copy over the unescaped data
3578 */
3580 if (unlikely(!(vpt->data.unescaped = talloc_bstrdup(vpt, in->data.unescaped)))) {
3581 error:
3583 return NULL;
3584 }
3585 }
3586
3587 /*
3588 * Copy attribute references
3589 */
3590 else if (tmpl_contains_attr(vpt)) {
3591 if (unlikely(tmpl_attr_copy(vpt, in) < 0)) goto error;
3592
3593 /*
3594 * Copy flags for all regex flavours (and possibly recompile the regex)
3595 */
3596 } else if (tmpl_contains_regex(vpt)) {
3597 vpt->data.reg_flags = in->data.reg_flags;
3598
3599 /*
3600 * If the tmpl contains a _compiled_ regex
3601 * then convert it back to an uncompiled
3602 * regex and recompile.
3603 *
3604 * Most of the regex libraries don't allow
3605 * copying compiled expressions.
3606 */
3607 if (tmpl_is_regex(vpt)) {
3609 if (unlikely(!(vpt->data.unescaped = talloc_bstrdup(vpt, in->data.reg.src)))) goto error;
3610 if (unlikely(tmpl_regex_compile(vpt, vpt->data.reg.subcaptures) < 0)) goto error;
3611 return vpt;
3612 }
3613
3614 /*
3615 * Copy the xlat component.
3616 *
3617 * @todo - in general we can't copy an xlat, as the instances need resolving!
3618 *
3619 * We add an assertion here because nothing allocates the head, and we need it.
3620 */
3621 } else if (tmpl_contains_xlat(vpt)) {
3622 fr_assert(in->data.xlat.ex != NULL);
3623
3624 vpt->data.xlat.ex = xlat_exp_head_alloc(vpt);
3625 if (!vpt->data.xlat.ex) goto error;
3626
3627 if (unlikely(xlat_copy(vpt, vpt->data.xlat.ex, in->data.xlat.ex) < 0)) goto error;
3628
3629 } else if (tmpl_is_data(vpt)) {
3630 if (unlikely(fr_value_box_copy(vpt, &vpt->data.literal, &in->data.literal) < 0)) goto error;
3631
3632 } else {
3633 fr_assert(0); /* copy of this type is unimplemented */
3634 }
3635
3637
3638 return vpt;
3639}
3640
3641/** Parse a cast specifier
3642 *
3643 * Note that casts are
3644 *
3645 * (foo)
3646 *
3647 * and NOT
3648 *
3649 * ( foo )
3650 *
3651 * Not for any particular reason, but to emphasize a bit that they're
3652 * not mathematical expressions.
3653 *
3654 * @param[out] rules to set the cast type in.
3655 * @param[in] in String containing the cast marker.
3656 * @return
3657 * - 0 no cast specifier found.
3658 * - >0 the number of bytes parsed.
3659 * - <0 offset of parse error.
3660 */
3662{
3663 char close = '\0';
3664 fr_sbuff_t our_in = FR_SBUFF(in);
3666 fr_type_t cast;
3667 ssize_t slen;
3668
3669 if (fr_sbuff_next_if_char(&our_in, '(')) {
3670 close = ')';
3671
3672 } else {
3673 if (rules) rules->cast = FR_TYPE_NULL;
3674 return 0;
3675 }
3676
3677 fr_sbuff_marker(&m, &our_in);
3679 if (fr_type_is_null(cast)) {
3680 fr_strerror_const("Unknown data type");
3681 FR_SBUFF_ERROR_RETURN(&our_in);
3682 }
3683 if (fr_type_is_non_leaf(cast)) {
3684 fr_strerror_printf("Forbidden data type '%s' in cast", fr_type_to_str(cast));
3686 }
3687
3688 if (!fr_sbuff_next_if_char(&our_in, close)) {
3689 fr_strerror_const("Unterminated cast");
3690 FR_SBUFF_ERROR_RETURN(&our_in);
3691 }
3692 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
3693
3694 if (rules) rules->cast = cast;
3695
3696 FR_SBUFF_SET_RETURN(in, &our_in);
3697}
3698
3699/** Set a cast for a tmpl
3700 *
3701 * @param[in,out] vpt to set cast for.
3702 * @param[in] dst_type to set.
3703 * @return
3704 * - 0 on success.
3705 * - -1 on failure.
3706 */
3708{
3709 fr_type_t src_type;
3710
3711 switch (dst_type) {
3712 default:
3713 fr_strerror_printf("Forbidden data type '%s' in cast",
3714 fr_type_to_str(dst_type));
3715 return -1;
3716
3717 /*
3718 * We can always remove a cast.
3719 */
3720 case FR_TYPE_NULL:
3721 goto done;
3722
3723 /*
3724 * Only "base" data types are allowed. Structural types
3725 * and horrid WiMAX crap is forbidden.
3726 */
3727 case FR_TYPE_LEAF:
3728 break;
3729 }
3730
3731 switch (vpt->type) {
3732 /*
3733 * This should have been fixed before we got here.
3734 */
3736
3737 /*
3738 * By default, tmpl types cannot be cast to anything.
3739 */
3740 default:
3741 fr_strerror_const("Cannot use cast here.");
3742 return -1;
3743
3744 /*
3745 * These tmpl types are effectively of data type
3746 * "string", so they can be cast to anything.
3747 */
3748 case TMPL_TYPE_XLAT:
3749 case TMPL_TYPE_EXEC:
3753 break;
3754
3755 case TMPL_TYPE_DATA:
3756 src_type = tmpl_value_type(vpt);
3757 goto check_types;
3758
3759 case TMPL_TYPE_ATTR:
3760 {
3762
3763 /*
3764 * If the attribute has an enum, then the cast means "use the raw value, and not
3765 * the enum name".
3766 */
3767 if (da->type == dst_type) {
3768 if (da->flags.has_value) goto done;
3769 return 0;
3770 }
3771 src_type = da->type;
3772 }
3773
3774 /*
3775 * Suppress casts where they are duplicate, unless there's an enumv. In which case the
3776 * cast means "don't print the enumv value, just print the raw data".
3777 */
3778 check_types:
3779 if (src_type == dst_type) {
3780 /*
3781 * Cast with enumv means "use the raw value, and not the enum name".
3782 */
3783 if (tmpl_rules_enumv(vpt)) {
3784 tmpl_rules_enumv(vpt) = NULL;
3785 goto done;
3786 }
3787 return 0;
3788 }
3789
3790 if (!fr_type_cast(dst_type, src_type)) {
3791 fr_strerror_printf("Cannot cast type '%s' to '%s'",
3792 fr_type_to_str(src_type),
3793 fr_type_to_str(dst_type));
3794 return -1;
3795 }
3796 break;
3797 }
3798
3799done:
3800 vpt->rules.cast = dst_type;
3801 return 0;
3802}
3803
3804#ifdef HAVE_REGEX
3805/** Parse a set of regular expression flags
3806 *
3807 * @param[out] vpt Write the flags to the regex flags field in this #tmpl_t.
3808 * @param[in] in Where to parse the flag string from.
3809 * @param[in] terminals That mark the end of the regex flag string.
3810 * @return
3811 * - 0 no flags found.
3812 * - >0 the number of bytes of flags parsed.
3813 * - <0 offset of parse error.
3814 */
3815ssize_t tmpl_regex_flags_substr(tmpl_t *vpt, fr_sbuff_t *in, fr_sbuff_term_t const *terminals)
3816{
3817 fr_slen_t slen;
3818 int err = 0;
3819
3821
3822 slen = regex_flags_parse(&err, &vpt->data.reg_flags, in, terminals, true);
3823 switch (err) {
3824 case 0:
3825 break;
3826
3827 case -1: /* Non-flag and non-terminal */
3828 case -2: /* Duplicate flag */
3829 return slen;
3830 }
3831
3832 return slen;
3833}
3834#endif
3835
3836/** @name Change a #tmpl_t type, usually by casting or resolving a reference
3837 *
3838 * #tmpl_cast_in_place can be used to convert #TMPL_TYPE_DATA_UNRESOLVED to a #TMPL_TYPE_DATA of a
3839 * specified #fr_type_t.
3840 *
3841 * #tmpl_attr_unknown_add converts a #TMPL_TYPE_ATTR with an unknown #fr_dict_attr_t to a
3842 * #TMPL_TYPE_ATTR with a known #fr_dict_attr_t, by adding the unknown #fr_dict_attr_t to the main
3843 * dictionary, and updating the ``tmpl_attr_tail_da`` pointer.
3844 * @{
3845 */
3846
3847/** Determine the correct quoting after a cast
3848 *
3849 * @param[in] existing_quote Exiting quotation type.
3850 * @param[in] type Cast type.
3851 * @param[in] enumv Enumeration values.
3852 * @param[in] unescaped The unescaped value of an enumeration.
3853 * @param[in] unescaped_len Length of unescaped.
3854 */
3855static inline CC_HINT(always_inline)
3857 fr_type_t type, fr_dict_attr_t const *enumv,
3858 char const *unescaped, size_t unescaped_len)
3859{
3860 if (!fr_type_is_string(type)) return T_BARE_WORD;
3861
3862 if (enumv && fr_dict_enum_by_name(enumv, unescaped, unescaped_len)) return T_BARE_WORD;
3863
3864 /*
3865 * Leave the original quoting if it's
3866 * single or double, else default to
3867 * single quoting.
3868 */
3869 switch (existing_quote) {
3872 return existing_quote;
3873
3874 default:
3876 }
3877}
3878
3879
3880/** Convert #tmpl_t of type #TMPL_TYPE_DATA_UNRESOLVED or #TMPL_TYPE_DATA to #TMPL_TYPE_DATA of type specified
3881 *
3882 * @note Conversion is done in place.
3883 * @note Irrespective of whether the #tmpl_t was #TMPL_TYPE_DATA_UNRESOLVED or #TMPL_TYPE_DATA,
3884 * on successful cast it will be #TMPL_TYPE_DATA.
3885 *
3886 * @param[in,out] vpt The template to modify. Must be of type #TMPL_TYPE_DATA_UNRESOLVED
3887 * or #TMPL_TYPE_DATA.
3888 * @param[in] type to cast to.
3889 * @param[in] enumv Enumerated dictionary values associated with a #fr_dict_attr_t.
3890 * @return
3891 * - 0 on success.
3892 * - -1 on failure.
3893 */
3895{
3897
3899
3900 switch (vpt->type) {
3902 {
3903 char *unescaped = vpt->data.unescaped;
3904
3905 /*
3906 * We're trying to convert an unresolved (bareword)
3907 * tmpl to octets.
3908 *
3909 * tmpl_afrom_substr uses the 0x prefix as type
3910 * inference, so if it was a hex string the tmpl
3911 * type would not have fallen through to
3912 * unresolved.
3913 *
3914 * That means if we're trying to resolve it here
3915 * it's really a printable string, not a sequence
3916 * of hexits, so we just want the binary
3917 * representation of that string, and not the hex
3918 * to bin conversion.
3919 */
3920 if (fr_type_is_octets(type)) {
3921 if (fr_value_box_memdup(vpt, &vpt->data.literal, enumv,
3922 (uint8_t const *)unescaped, talloc_array_length(unescaped) - 1,
3923 false) < 0) return -1;
3924 } else {
3925 if (fr_value_box_from_str(vpt, &vpt->data.literal, type,
3926 enumv,
3927 unescaped, talloc_array_length(unescaped) - 1,
3928 NULL) < 0) return -1;
3929 }
3930 vpt->type = TMPL_TYPE_DATA;
3931 vpt->quote = tmpl_cast_quote(vpt->quote, type, enumv,
3932 unescaped, talloc_array_length(unescaped) - 1);
3933 talloc_free(unescaped);
3934 fr_value_box_mark_safe_for(&vpt->data.literal, vpt->rules.literals_safe_for);
3935
3936 /*
3937 * The data is now of the correct type, so we don't need to keep a cast.
3938 */
3939 vpt->rules.cast = FR_TYPE_NULL;
3940 }
3941 break;
3942
3943 case TMPL_TYPE_DATA:
3944 {
3945 if (type == tmpl_value_type(vpt)) return 0; /* noop */
3946
3947 /*
3948 * Enumerations aren't used when casting between
3949 * data types. They're only used when processing
3950 * unresolved tmpls.
3951 *
3952 * i.e. TMPL_TYPE_DATA_UNRESOLVED != TMPL_TYPE_DATA(FR_TYPE_STRING)
3953 */
3954 if (fr_value_box_cast_in_place(vpt, &vpt->data.literal, type, NULL) < 0) return -1;
3955// fr_value_box_mark_safe_for(&vpt->data.literal, vpt->rules.literals_safe_for); ??? is this necessary?
3956
3957 /*
3958 * Strings get quoted, everything else is a bare
3959 * word...
3960 */
3961 if (fr_type_is_string(type)) {
3962 vpt->quote = T_SINGLE_QUOTED_STRING;
3963 } else {
3964 vpt->quote = T_BARE_WORD;
3965 }
3966
3967 /*
3968 * The data is now of the correct type, so we don't need to keep a cast.
3969 */
3970 vpt->rules.cast = FR_TYPE_NULL;
3971 }
3972 break;
3973
3974 case TMPL_TYPE_ATTR:
3975 /*
3976 * Suppress casts to the same type.
3977 */
3978 if (tmpl_attr_tail_da(vpt)->type == type) {
3979 vpt->rules.cast = FR_TYPE_NULL;
3980 break;
3981 }
3983
3985 vpt->rules.cast = type;
3986 break;
3987
3988 default:
3989 fr_assert(0);
3990 }
3992
3993 return 0;
3994}
3995
3996/** Resolve an unresolved attribute
3997 *
3998 * Multi-pass parsing fixups for attribute references.
3999 *
4000 * @param[in] vpt to resolve.
4001 * @param[in] tr_rules Combined with the original parse rules for
4002 * additional resolution passes.
4003 * @return
4004 * - 0 if all references were resolved.
4005 * - -1 if there are unknown attributes which need
4006 * adding to the global dictionary first.
4007 * - -2 if there are attributes we couldn't resolve.
4008 */
4009static inline CC_HINT(always_inline) int tmpl_attr_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules)
4010{
4011 tmpl_attr_t *ar = NULL, *next, *prev;
4012 fr_dict_attr_t const *da, *namespace;
4013 fr_dict_t const *dict_def;
4014
4016
4018
4019 dict_def = vpt->rules.attr.dict_def;
4020 if (!dict_def || tr_rules->force_dict_def) dict_def = tr_rules->dict_def;
4021
4022 /*
4023 * First component is special because we may need
4024 * to search for it in multiple dictionaries.
4025 *
4026 * This emulates what's done in the initial
4027 * tokenizer function.
4028 */
4029 ar = tmpl_attr_list_head(tmpl_attr(vpt));
4030 if (ar->type == TMPL_ATTR_TYPE_UNRESOLVED) {
4032 &da,
4033 dict_def,
4034 &FR_SBUFF_IN(ar->ar_unresolved,
4035 talloc_array_length(ar->ar_unresolved) - 1),
4036 NULL,
4037 true,
4038 vpt->rules.attr.allow_foreign);
4039 if (!da) return -2; /* Can't resolve, maybe the caller can resolve later */
4040
4041 ar->ar_type = TMPL_ATTR_TYPE_NORMAL;
4042 ar->ar_da = da;
4043 ar->ar_parent = fr_dict_root(fr_dict_by_da(da));
4044
4045 /*
4046 * Record the dictionary that was
4047 * successfully used for resolution.
4048 */
4049 vpt->rules.attr.dict_def = tr_rules->dict_def;
4050
4051 /*
4052 * Reach into the next reference
4053 * and correct its parent and
4054 * namespace.
4055 */
4056 next = tmpl_attr_list_next(tmpl_attr(vpt), ar);
4057 if (next) {
4058 next->ar_parent = da;
4059 next->ar_unresolved_namespace = da;
4060 }
4061 }
4062
4063 /*
4064 * Loop, resolving each unresolved attribute in turn
4065 */
4066 while ((ar = tmpl_attr_list_next(tmpl_attr(vpt), ar))) {
4067 switch (ar->type) {
4070 continue; /* Don't need to resolve */
4071
4073 return -1; /* Unknown attributes must be resolved first */
4074
4075 default:
4076 break;
4077 }
4078
4079 prev = tmpl_attr_list_prev(tmpl_attr(vpt), ar);
4080
4081 /*
4082 * If the parent is a list AR, then use the default dictionary for the namespace
4083 */
4084 namespace = (prev && dict_def && tmpl_attr_is_list_attr(prev)) ? fr_dict_root(dict_def) : ar->ar_unresolved_namespace;
4085
4086 (void)fr_dict_attr_by_name_substr(NULL,
4087 &da,
4088 namespace,
4089 &FR_SBUFF_IN(ar->ar_unresolved,
4090 talloc_array_length(ar->ar_unresolved) - 1),
4091 NULL);
4092 /*
4093 * Still can't resolve, check to see if
4094 * the last attribute reference was a
4095 * group.
4096 *
4097 * If it was, then we may be able to
4098 * fall back to resolving the attribute
4099 * in the internal dictionary.
4100 */
4101 if (!da) {
4102 if (prev && (prev->ar_da->type == FR_TYPE_GROUP)) {
4103 (void)fr_dict_attr_by_name_substr(NULL,
4104 &da,
4106 &FR_SBUFF_IN(ar->ar_unresolved,
4107 talloc_array_length(ar->ar_unresolved) - 1),
4108 NULL);
4109 }
4110 if (!da) return -2;
4111 }
4112
4113 /*
4114 * Known attribute, just rewrite.
4115 */
4116 ar->ar_type = TMPL_ATTR_TYPE_NORMAL;
4117 ar->ar_da = da;
4118
4119 /*
4120 * Parent should have been corrected in
4121 * the previous loop iteration.
4122 */
4123 fr_assert(ar->ar_parent && !ar->ar_parent->flags.is_unknown);
4124
4125 /*
4126 * Reach into the next reference
4127 * and correct its parent.
4128 */
4129 next = tmpl_attr_list_next(tmpl_attr(vpt), ar);
4130 if (next) {
4131 next->ar_parent = da;
4132 next->ar_unresolved_namespace = da;
4133 }
4134
4135 /*
4136 * Remove redundant attributes
4137 *
4138 * If it's not a group or does not specify
4139 * an index, the ar is redundant and should
4140 * be removed.
4141 */
4142 prev = tmpl_attr_list_prev(tmpl_attr(vpt), ar);
4143 if (prev && (prev->ar_da->type != FR_TYPE_GROUP) && (prev->ar_num == NUM_UNSPEC)) {
4144 tmpl_attr_list_remove(tmpl_attr(vpt), prev);
4145 ar->ar_parent = prev->ar_parent;
4146 talloc_free(prev);
4147 }
4148 }
4149
4150 RESOLVED_SET(&vpt->type);
4152
4153 return 0;
4154}
4155
4156/** Resolve an unresolved xlat, i.e. one containing unresolved attribute references or xlat functions
4157 *
4158 * Multi-pass parsing fixups for attribute references.
4159 *
4160 * Works for base types:
4161 * - TMPL_TYPE_XLAT
4162 * - TMPL_TYPE_EXEC
4163 * - TMPL_TYPE_REGEX_XLAT
4164 *
4165 * @param[in] vpt Containing the xlat expansion to resolve.
4166 * @param[in] tr_rules Combined with the original parse rules for
4167 * additional resolution passes.
4168 * @return
4169 * - 0 on success.
4170 * - -1 on failure.
4171 */
4172static inline CC_HINT(always_inline)
4174{
4175 if (xlat_resolve(vpt->data.xlat.ex,
4177 .tr_rules = tr_rules,
4178 .allow_unresolved = false
4179 }) < 0) return -1;
4180
4181 fr_assert(!xlat_needs_resolving(vpt->data.xlat.ex));
4182
4183 RESOLVED_SET(&vpt->type);
4185
4186 return 0;
4187}
4188
4189/** Attempt to resolve functions and attributes in xlats and attribute references
4190 *
4191 * @note If resolution is successful, the rules->attr.dict_def field will be modified to
4192 * reflect the dictionary resolution was successful in.
4193 *
4194 * @param[in,out] vpt to resolve. Should be of type TMPL_TYPE_XLAT_UNRESOLVED
4195 * or TMPL_TYPE_ATTR_UNRESOLVED. All other types will be
4196 * noops.
4197 * @param[in] tr_rules Combined with the original parse rules for
4198 * additional resolution passes.
4199 * @return
4200 * - 0 on success.
4201 * - -1 on failure.
4202 */
4204{
4205 static tmpl_res_rules_t const default_tr_rules;
4206
4207 int ret = 0;
4208
4209 if (!tmpl_needs_resolving(vpt)) return 0; /* Nothing to do */
4210
4211 if (!tr_rules) tr_rules = &default_tr_rules;
4212
4213 /*
4214 * Sanity check. There shouldn't be conflicting
4215 * enumvs between the original rules and resolution
4216 * rules.
4217 *
4218 * Either the enumv was available during parsing
4219 * and shouldn't have changed during subsequent
4220 * resolution passes, or it wasn't available at
4221 * parse-time, but now is.
4222 */
4223 if (tr_rules->enumv && tmpl_rules_enumv(vpt) && !tmpl_rules_enumv(vpt)->flags.is_unknown &&
4224 (tr_rules->enumv != tmpl_rules_enumv(vpt))) {
4225 fr_strerror_printf("mismatch between parse-time enumv '%s' and resolution-time enumv '%s'",
4226 tmpl_rules_enumv(vpt)->name, tr_rules->enumv->name);
4227
4228 return -1;
4229 }
4230
4231 /*
4232 * The xlat component of the #tmpl_t needs resolving.
4233 *
4234 * This includes exec tmpls, which are largely xlats
4235 * "under the hood".
4236 */
4237 if (tmpl_contains_xlat(vpt)) {
4238 ret = tmpl_xlat_resolve(vpt, tr_rules);
4239
4240 /*
4241 * The attribute reference needs resolving.
4242 */
4243 } else if (tmpl_contains_attr(vpt)) {
4244 fr_type_t dst_type = tmpl_rules_cast(vpt);
4245
4246 fr_assert(vpt->quote == T_BARE_WORD); /* 'User-Name' or "User-Name" is not allowed. */
4247
4248 ret = tmpl_attr_resolve(vpt, tr_rules);
4249 if (ret < 0) return ret;
4250
4251 if (dst_type == tmpl_attr_tail_da(vpt)->type) {
4252 vpt->rules.cast = FR_TYPE_NULL;
4253 }
4254
4255 /*
4256 * Convert unresolved tmpls into enumvs, or failing that, string values.
4257 *
4258 * Unresolved tmpls are by definition TMPL_TYPE_DATA.
4259 */
4260 } else if (tmpl_is_data_unresolved(vpt)) {
4261 fr_type_t dst_type = tmpl_rules_cast(vpt);
4262 fr_dict_attr_t const *enumv = tmpl_rules_enumv(vpt);
4263
4264 /*
4265 * If there wasn't an enumv set in the
4266 * original rules, and we now have one
4267 * (possibly because the other side of a
4268 * binary expression has been resolved),
4269 * then use the new enumv.
4270 */
4271 if (!enumv) enumv = tr_rules->enumv;
4272
4273 /*
4274 * We don't have an explicit output type. Try to
4275 * interpret the data os the enumv data type, OR
4276 * if all else fails, it's a string.
4277 */
4278 if (fr_type_is_null(dst_type)) {
4279 /*
4280 * Infer the cast from the enumv type.
4281 */
4282 if (enumv) {
4283 dst_type = enumv->type;
4284
4285 } else if (vpt->quote != T_BARE_WORD) {
4286 dst_type = FR_TYPE_STRING; /* quoted strings are strings */
4287
4288 } else if (strncmp(vpt->data.unescaped, "::", 2) != 0) {
4289 /*
4290 * The rest of the code should have errored out before this.
4291 */
4292 fr_strerror_printf("Failed resolving data '%s' - it is not an attribute name or a quoted string", vpt->data.unescaped);
4293 return -1;
4294
4295 } else {
4296 /*
4297 * It's a valid enum ::NAME which was added _after_ the dictionaries were
4298 * loaded. That's fine. fr_value_box_from_substr() will skip over the
4299 * "::", and parse the enum name.
4300 */
4301 }
4302 }
4303
4304 /*
4305 * tmpl_cast_in_place first resolves using
4306 * the enumv, _then_ casts using the type.
4307 */
4308 if (tmpl_cast_in_place(vpt, dst_type, enumv) < 0) return -1;
4309
4311 /*
4312 * Catch any other cases of unresolved things
4313 * we need to address. We put the assert here
4314 * so we don't end up running inappropriate
4315 * code for non-debug builds.
4316 */
4317 } else {
4318 fr_assert(0);
4319 }
4320
4322
4323 return ret;
4324}
4325
4326/** Reset the tmpl, leaving only the name in place
4327 *
4328 * After calling this function, the tmpl type will revert to TMPL_TYPE_DATA_UNRESOLVED
4329 * and only the name and quoting will be preserved.
4330 *
4331 * @param[in] vpt to reset.
4332 */
4334{
4335 tmpl_t tmp = {
4337 .name = vpt->name,
4338 .len = vpt->len,
4339 .quote = vpt->quote
4340 };
4341
4342 switch (vpt->type) {
4344 case TMPL_TYPE_MAX:
4345 fr_assert(0);
4346 break;
4347
4350 break;
4351
4352 case TMPL_TYPE_DATA:
4353 fr_value_box_clear(&vpt->data.literal);
4354 break;
4355
4356 /*
4357 * These types contain dynamically allocated
4358 * attribute and request references.
4359 */
4360 case TMPL_TYPE_ATTR:
4362 tmpl_attr_list_talloc_free(tmpl_attr(vpt));
4363 tmpl_request_list_talloc_free(&vpt->data.attribute.rr);
4364 break;
4365
4366 /*
4367 * These all store an xlat expansion
4368 */
4369 case TMPL_TYPE_EXEC:
4370 case TMPL_TYPE_XLAT:
4375 TALLOC_FREE(vpt->data.xlat.ex);
4376 break;
4377
4378 case TMPL_TYPE_REGEX:
4379 talloc_free(vpt->data.reg.ex);
4380 break;
4381
4382 }
4383
4384 memcpy(vpt, &tmp, sizeof(*vpt));
4385
4387}
4388
4390{
4391 if (!ref) return;
4392
4393 switch (ref->type) {
4395 {
4396 ref->da = ref->ar_unknown = fr_dict_attr_unknown_afrom_da(vpt, ref->da);
4397 ref->ar_unknown->type = FR_TYPE_OCTETS;
4398 ref->is_raw = 1;
4399 ref->ar_unknown->flags.is_unknown = 1;
4401 }
4402 break;
4403 case TMPL_ATTR_TYPE_UNSPEC: /* noop */
4404 break;
4405
4407 ref->ar_unknown->type = FR_TYPE_OCTETS;
4408 ref->is_raw = 1;
4409 break;
4410
4412 ref->is_raw = true;
4413 break;
4414 }
4415
4417}
4418
4419/** Convert the leaf attribute of a tmpl to a unknown/raw type
4420 *
4421 */
4423{
4424 attr_to_raw(vpt, tmpl_attr_list_tail(tmpl_attr(vpt)));
4425}
4426
4427/** Add an unknown #fr_dict_attr_t specified by a #tmpl_t to the main dictionary
4428 *
4429 * @param vpt to add. ``tmpl_attr_tail_da`` pointer will be updated to point to the
4430 * #fr_dict_attr_t inserted into the dictionary.
4431 * @return
4432 * - 1 noop (did nothing) - Not possible to convert tmpl.
4433 * - 0 on success.
4434 * - -1 on failure.
4435 */
4437{
4438 tmpl_attr_t *ar = NULL, *next = NULL;
4439
4440 if (!vpt) return 1;
4441
4442 /*
4443 * Can't do this for expressions parsed at runtime
4444 */
4445 if (vpt->rules.at_runtime) return 1;
4446
4448
4450
4451 if (!tmpl_attr_tail_is_unknown(vpt)) return 1; /* Ensure at least the leaf is unknown */
4452
4453 while ((ar = tmpl_attr_list_next(tmpl_attr(vpt), ar))) {
4454 fr_dict_attr_t const *unknown, *known;
4455
4456 switch (ar->type) {
4457 case TMPL_ATTR_TYPE_NORMAL: /* Skip */
4459 continue;
4460
4461 case TMPL_ATTR_TYPE_UNRESOLVED: /* Shouldn't have been called */
4462 fr_strerror_const("Remaining attributes are unresolved");
4463 return -1;
4464
4466 break;
4467 }
4468
4469 unknown = ar->ar_unknown;
4470 known = fr_dict_attr_unknown_add(fr_dict_unconst(fr_dict_by_da(unknown)), unknown);
4471 if (!known) return -1;
4472
4473 /*
4474 * Fixup the parent of the next unknown
4475 * now it's known.
4476 */
4477 next = tmpl_attr_list_next(tmpl_attr(vpt), ar);
4478 if (next && (next->type == TMPL_ATTR_TYPE_UNKNOWN) &&
4479 (next->ar_da->parent == unknown)) {
4481 known) < 0) return -1;
4482 next->ar_parent = known;
4483 }
4484
4485 /*
4486 * Convert the ref to a normal type.
4487 * At runtime there should be no
4488 * "unknown" references as they should
4489 * have all been added to a
4490 * dictionary.
4491 */
4493
4494 /*
4495 * If the attribute is *NOT* raw then
4496 * swap the canonical unknown with the
4497 * one that was previously associated
4498 * with the tmpl.
4499 *
4500 * This establishes the unknown attribute
4501 * in the dictionary if it was really
4502 * unknown whilst not mucking up the
4503 * types for raw attributes.
4504 */
4505 if (!ar_is_raw(ar)) {
4506 fr_dict_attr_unknown_free(&ar->ar_da);
4507 ar->ar_da = known;
4508 } else if (!fr_cond_assert(!next)) {
4509 fr_strerror_const("Only the leaf may be raw");
4510 return -1;
4511 }
4512 }
4513
4514 return 0;
4515}
4516
4517/** Add an unresolved #fr_dict_attr_t specified by a #tmpl_t to the main dictionary
4518 *
4519 * @note fr_dict_attr_add will not return an error if the attribute already exists
4520 * meaning that multiple #tmpl_t specifying the same attribute can be
4521 * passed to this function to be fixed up, so long as the type and flags
4522 * are identical.
4523 *
4524 * @param[in] dict_def Default dictionary to use if none is
4525 * specified by the tmpl_attr_tail_unresolved.
4526 * @param[in] vpt specifying unresolved attribute to add.
4527 * ``tmpl_attr_tail_da`` pointer will be updated to
4528 * point to the #fr_dict_attr_t inserted
4529 * into the dictionary. Lists and requests
4530 * will be preserved.
4531 * @param[in] type to define unresolved attribute as.
4532 * @param[in] flags to define unresolved attribute with.
4533 * @return
4534 * - 1 noop (did nothing) - Not possible to convert tmpl.
4535 * - 0 on success.
4536 * - -1 on failure.
4537 */
4539 fr_type_t type, fr_dict_attr_flags_t const *flags)
4540{
4541 fr_dict_attr_t const *da;
4542 fr_dict_attr_flags_t our_flags = *flags;
4543
4544 our_flags.name_only = true;
4545
4546 if (!vpt) return -1;
4547
4549
4550 if (!tmpl_is_attr_unresolved(vpt)) return 1;
4551
4552 if (fr_dict_attr_add(dict_def,
4554 return -1;
4555 }
4557 if (!da) return -1;
4558
4559 if (type != da->type) {
4560 fr_strerror_printf("Attribute %s of type %s already defined with type %s",
4561 da->name, fr_type_to_str(type),
4562 fr_type_to_str(da->type));
4563 return -1;
4564 }
4565
4566 if (memcmp(flags, &da->flags, sizeof(*flags)) != 0) {
4567 fr_strerror_printf("Attribute %s already defined with different flags", da->name);
4568 return -1;
4569 }
4570
4571 tmpl_attr_set_da(vpt, da);
4572 vpt->type = TMPL_TYPE_ATTR;
4573
4574 return 0;
4575}
4576
4577#ifdef HAVE_REGEX
4578/** Convert a TMPL_TYPE_REGEX_UNCOMPILED into a TMPL_TYPE_REGEX
4579 *
4580 * Other regex types become noops.
4581 */
4582ssize_t tmpl_regex_compile(tmpl_t *vpt, bool subcaptures)
4583{
4584 ssize_t slen;
4585 char *unescaped = vpt->data.unescaped;
4586
4587 if (tmpl_is_regex_xlat(vpt) || tmpl_is_regex(vpt)) return 0; /* Don't need compiling */
4588
4590
4591 slen = regex_compile(vpt, &vpt->data.reg.ex,
4592 unescaped, talloc_array_length(unescaped) - 1,
4593 &vpt->data.reg_flags, subcaptures, vpt->rules.at_runtime);
4594 if (slen <= 0) return vpt->quote != T_BARE_WORD ? slen - 1 : slen; /* Account for the quoting */
4595
4596 vpt->type = TMPL_TYPE_REGEX;
4597 vpt->data.reg.src = unescaped; /* Keep this around for debugging and copying */
4598 vpt->data.reg.subcaptures = subcaptures;
4599
4601
4602 return slen;
4603}
4604#endif
4605/** @} */
4606
4607/** @name Print the contents of a #tmpl_t
4608 * @{
4609 */
4611{
4612 fr_sbuff_t our_out = FR_SBUFF(out);
4613 tmpl_request_t *rr = tmpl_request_list_head(rql);
4614
4615 /*
4616 * Print request references
4617 */
4618 while (rr) {
4619 FR_SBUFF_IN_TABLE_STR_RETURN(&our_out, tmpl_request_ref_table, rr->request, "<INVALID>");
4620 rr = tmpl_request_list_next(rql, rr);
4621 if (rr) FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4622 }
4623
4624 FR_SBUFF_SET_RETURN(out, &our_out);
4625}
4626
4627/** Print an attribute or list #tmpl_t to a string
4628 *
4629 * This function is the direct counterpart to #tmpl_afrom_attr_substr.
4630 *
4631 * @param[in] out Where to write the presentation format #tmpl_t string.
4632 * @param[in] vpt to print.
4633 * @return
4634 * - >0 the number of bytes written to the out buffer.
4635 * - 0 invalid argument.
4636 * - <0 the number of bytes we would have needed to complete the print.
4637 */
4639{
4640 tmpl_attr_t *ar = NULL;
4642 fr_sbuff_t our_out = FR_SBUFF(out);
4643 fr_slen_t slen;
4644
4646
4647 /*
4648 * Only print things we can print...
4649 */
4650 switch (vpt->type) {
4652 case TMPL_TYPE_ATTR:
4653 break;
4654
4655 default:
4656 fr_assert(0);
4657 return 0;
4658 }
4659
4660 /*
4661 * Print request references
4662 */
4663 slen = tmpl_request_ref_list_print(&our_out, &vpt->data.attribute.rr);
4664 if (slen > 0) FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4665 if (slen < 0) return slen;
4666
4667 /*
4668 *
4669 * If the leaf attribute is unknown and raw we
4670 * add the raw. prefix.
4671 *
4672 * If the leaf attribute is unknown and not raw
4673 * we add the .unknown prefix.
4674 *
4675 */
4677
4678 /*
4679 * Print attribute identifiers
4680 */
4681 ar = NULL;
4682 while ((ar = tmpl_attr_list_next(tmpl_attr(vpt), ar))) {
4683 switch(ar->type) {
4685 break;
4686
4689 {
4690 int i, depth = 0;
4691
4692 fr_assert(ar->ar_parent); /* All normal and unknown attributes must have parents */
4693
4694 fr_proto_da_stack_build_partial(&stack, ar->ar_parent, ar->ar_da);
4695
4696 /*
4697 * First component in the list has everything built
4698 */
4699 if (ar == tmpl_attr_list_head(tmpl_attr(vpt))) {
4700 depth = ar->ar_parent->depth - 1; /* Adjust for array index */
4701 /*
4702 * Everything else skips the first component
4703 */
4704 } else {
4705 depth = ar->ar_parent->depth;
4706 }
4707
4708 /*
4709 * Root attributes will be skipped by the build
4710 * function, so da[0] contains the attribute
4711 * we're looking for.
4712 */
4713 if (depth < 0) depth = 0;
4714
4715 /*
4716 * Print from our parent depth to the AR we're processing
4717 *
4718 * For refs we skip the attribute pointed to be the ref
4719 * and just print its children.
4720 *
4721 * In addition skip printing "request." in most cases.
4722 */
4723 if ((stack.da[depth] == request_attr_request) && tmpl_attr_list_next(tmpl_attr(vpt), ar) &&
4724 (ar->filter.type == TMPL_ATTR_FILTER_TYPE_NONE)) continue;
4725
4726 for (i = depth; (unsigned int)i < ar->ar_da->depth; i++) {
4727 FR_SBUFF_IN_STRCPY_RETURN(&our_out, stack.da[i]->name);
4728
4729 /*
4730 * Print intermediary separators
4731 * if necessary.
4732 */
4733 if (((unsigned int)i + 1) < ar->ar_da->depth) FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4734 }
4735 }
4736 break;
4737
4738 /*
4739 * For unresolved attribute we print the raw identifier we
4740 * got when parsing the tmpl.
4741 */
4743 {
4744 unsigned int i, depth;
4745
4746 /*
4747 * This is the first unresolved component in a potential
4748 * chain of unresolved components. Print the path up to
4749 * the last known parent.
4750 */
4751 if (ar->ar_parent && !ar->ar_parent->flags.is_root) {
4752 fr_proto_da_stack_build_partial(&stack, ar->ar_parent, ar->ar_parent);
4753 if (ar->ar_parent->flags.is_root) {
4754 depth = 0;
4755 } else {
4756 depth = ar->ar_parent->depth - 1;
4757 }
4758
4759 for (i = depth; i < ar->ar_parent->depth; i++) {
4760 FR_SBUFF_IN_STRCPY_RETURN(&our_out, stack.da[i]->name);
4761 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4762 }
4763 }
4764 /*
4765 * Then print the unresolved component
4766 */
4767 FR_SBUFF_IN_BSTRCPY_BUFFER_RETURN(&our_out, ar->ar_unresolved);
4768 break;
4769 }
4770 }
4771
4772 if (ar_filter_is_none(ar)) {
4773 /* do nothing */
4774
4775 } else if (ar_filter_is_num(ar)) {
4776 switch (ar->ar_num) {
4777 case NUM_UNSPEC:
4778 break;
4779
4780 case NUM_ALL:
4781 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "[*]");
4782 break;
4783
4784 case NUM_COUNT:
4785 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "[#]");
4786 break;
4787
4788 case NUM_LAST:
4789 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "[n]");
4790 break;
4791
4792 default:
4793 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "[%i]", ar->ar_num);
4794 break;
4795 }
4796
4797 } else if (ar_filter_is_cond(ar)) {
4798 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "[");
4799 (void) xlat_print(&our_out, ar->ar_cond, NULL);
4800 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "]");
4801
4802 } else {
4803 fr_assert(0);
4804 }
4805
4806 if (tmpl_attr_list_next(tmpl_attr(vpt), ar)) FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4807 }
4808 FR_SBUFF_SET_RETURN(out, &our_out);
4809}
4810
4811/** Print a #tmpl_t to a string
4812 *
4813 * This function should primarily be used for regenerating vpt->name when the contents
4814 * of the #tmpl_t is changed programmatically, or when the #tmpl_t is being serialized
4815 * in some non-standard way, i.e. as a value for a field in a database.
4816 *
4817 * This function is the direct counterpart to #tmpl_afrom_substr.
4818 *
4819 * @note Does not print flags for regular expressions, as the quoting char is needed
4820 * to separate the elements of the expression.
4821 * Call regex_flags_print to write the flags values to the output buffer.
4822 *
4823 * @param[out] out Where to write the presentation format #tmpl_t string.
4824 * @param[in] vpt to print.
4825 * @param[in] e_rules Escaping rules used to print strings.
4826 * @return
4827 * - >0 the number of bytes written to the out buffer.
4828 * - 0 invalid argument.
4829 * - <0 the number of bytes we would have needed to complete the print.
4830 */
4832 fr_sbuff_escape_rules_t const *e_rules)
4833{
4834 fr_sbuff_t our_out = FR_SBUFF(out);
4835
4837
4838 switch (vpt->type) {
4840 case TMPL_TYPE_ATTR:
4842 break;
4843
4844 case TMPL_TYPE_DATA:
4845 FR_SBUFF_RETURN(fr_value_box_print, &our_out, tmpl_value(vpt), e_rules);
4846 break;
4847
4848 case TMPL_TYPE_REGEX:
4849 FR_SBUFF_IN_BSTRNCPY_RETURN(&our_out, vpt->name, vpt->len); /* Fixme - double escapes */
4850 break;
4851
4853 FR_SBUFF_IN_ESCAPE_BUFFER_RETURN(&our_out, vpt->data.unescaped, e_rules);
4854 break;
4855
4857 case TMPL_TYPE_MAX:
4858 fr_sbuff_terminate(out);
4859 break;
4860
4861 /*
4862 * The remaining types will either
4863 * be xlat expansions, or need
4864 * resolving, in which case the
4865 * unescaped string is available
4866 * in vpt->unescaped.
4867 */
4868 default:
4869 if (tmpl_contains_xlat(vpt)) {
4870 FR_SBUFF_RETURN(xlat_print, &our_out, tmpl_xlat(vpt), e_rules);
4871 break;
4872 }
4873
4875 FR_SBUFF_IN_ESCAPE_BUFFER_RETURN(&our_out, vpt->data.unescaped, e_rules);
4876 break;
4877 }
4878
4879 fr_assert_fail("Can't print invalid tmpl type %s", tmpl_type_to_str(vpt->type));
4880
4881 /*
4882 * Ensure we do something sane for non-debug builds
4883 */
4884 fr_sbuff_terminate(out);
4885 return 0;
4886 }
4887
4888 FR_SBUFF_SET_RETURN(out, &our_out);
4889}
4890
4891/** Print a #tmpl_t to a string with quotes
4892 *
4893 * This function should be used when the tmpl is embedded in some other construct
4894 * in the server's configuration.
4895 *
4896 * It adds standard quoting around tmpl's used as operands in expressions and applies
4897 * the correct escaping rules.
4898 *
4899 * @param[out] out Where to write the presentation format #tmpl_t string.
4900 * @param[in] vpt to print.
4901 * @return
4902 * - >0 the number of bytes written to the out buffer.
4903 * - 0 invalid argument.
4904 * - <0 the number of bytes we would have needed to complete the print.
4905 */
4907{
4908 fr_sbuff_t our_out = FR_SBUFF(out);
4909
4910 char quote = fr_token_quote[vpt->quote];
4911
4912 if (quote != '\0') FR_SBUFF_IN_CHAR_RETURN(&our_out, quote);
4913 FR_SBUFF_RETURN(tmpl_print, &our_out, vpt,
4915 if (quote != '\0') FR_SBUFF_IN_CHAR_RETURN(&our_out, quote);
4916
4917 /*
4918 * Optionally print the flags
4919 */
4920 if (vpt->type & TMPL_FLAG_REGEX) FR_SBUFF_RETURN(regex_flags_print, &our_out, tmpl_regex_flags(vpt));
4921
4922 FR_SBUFF_SET_RETURN(out, &our_out);
4923}
4924/** @} */
4925
4926
4927#ifdef WITH_VERIFY_PTR
4928/** Used to check whether areas of a tmpl_t are zeroed out
4929 *
4930 * @param ptr Offset to begin checking at.
4931 * @param len How many bytes to check.
4932 * @return
4933 * - Pointer to the first non-zero byte.
4934 * - NULL if all bytes were zero.
4935 */
4936static uint8_t const *is_zeroed(uint8_t const *ptr, size_t len)
4937{
4938 size_t i;
4939
4940 for (i = 0; i < len; i++) {
4941 if (ptr[i] != 0x00) return ptr + i;
4942 }
4943
4944 return NULL;
4945}
4946
4947/** Verify that unused regions of the struct are zeroed out
4948 *
4949 */
4950#define CHECK_ZEROED(_vpt, _field) is_zeroed(((uint8_t const *)&(_vpt)->data) + sizeof((_vpt)->data._field), sizeof((_vpt)->data) - sizeof((_vpt)->data._field))
4951
4952
4953/** Print hex data
4954 *
4955 */
4956#define PRINT_NON_ZEROED(_vpt, _field, _nz_ptr) \
4957do { \
4958 DEBUG("Expected live portion %p-%p (0-%zu)", \
4959 _vpt, \
4960 (uint8_t const *)&(_vpt)->data + sizeof((_vpt)->data._field), \
4961 sizeof((_vpt)->data._field)); \
4962 DEBUG("Expected zero portion %p-%p (%zu-%zu)", \
4963 (uint8_t const *)&(_vpt)->data + sizeof((_vpt)->data._field), \
4964 (uint8_t const *)&(_vpt)->data + sizeof((_vpt)->data), \
4965 sizeof((_vpt)->data._field), sizeof((_vpt)->data)); \
4966 HEX_MARKER1((uint8_t const *)&vpt->data, sizeof(vpt->data), nz - (uint8_t const *)&vpt->data, "non-zero memory", ""); \
4967} while (0)
4968
4969
4970/** Verify the attribute reference in a tmpl_t make sense
4971 *
4972 * @note If the attribute reference is is invalid, causes the server to exit.
4973 *
4974 * @param file obtained with __FILE__.
4975 * @param line obtained with __LINE__.
4976 * @param vpt to check.
4977 */
4978void tmpl_attr_verify(char const *file, int line, tmpl_t const *vpt)
4979{
4980 tmpl_attr_t *ar = NULL;
4981 tmpl_attr_t *slow = NULL, *fast = NULL;
4982 tmpl_attr_t *seen_unknown = NULL;
4983 tmpl_attr_t *seen_unresolved = NULL;
4984
4986
4987 /*
4988 * Loop detection
4989 */
4990 while ((slow = tmpl_attr_list_next(tmpl_attr(vpt), slow)) &&
4991 (fast = tmpl_attr_list_next(tmpl_attr(vpt), fast))) {
4992
4993 /*
4994 * Advances twice as fast as slow...
4995 */
4996 fast = tmpl_attr_list_next(tmpl_attr(vpt), fast);
4997 fr_fatal_assert_msg(fast != slow,
4998 "CONSISTENCY CHECK FAILED %s[%u]: Looping reference list found. "
4999 "Fast pointer hit slow pointer at \"%s\"",
5000 file, line,
5001 slow->type == TMPL_ATTR_TYPE_UNRESOLVED ? slow->ar_unresolved :
5002 slow->da ? slow->da->name : "(null-attr)");
5003 }
5004
5005 /*
5006 * Lineage type check
5007 *
5008 * Known attribute cannot come after unresolved or unknown attributes
5009 * Unknown attributes cannot come after unresolved attributes
5010 */
5011 if (!tmpl_is_list(vpt)) while ((ar = tmpl_attr_list_next(tmpl_attr(vpt), ar))) {
5012 switch (ar->type) {
5014 if (seen_unknown) {
5016 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5017 "TMPL_TYPE_ATTR known attribute \"%s\" "
5018 "occurred after unknown attribute %s "
5019 "in attr ref list",
5020 file, line,
5021 ar->da->name,
5022 ar->unknown.da->name);
5023 }
5024 if (seen_unresolved) {
5026 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5027 "TMPL_TYPE_ATTR known attribute \"%s\" "
5028 "occurred after unresolved attribute \"%s\""
5029 "in attr ref list",
5030 file, line,
5031 ar->da->name,
5032 ar->ar_unresolved);
5033 }
5034 fr_fatal_assert_msg(ar->ar_parent,
5035 "CONSISTENCY CHECK FAILED %s[%u]: attr ref missing parent",
5036 file, line);
5037 break;
5038
5040 if (seen_unknown) {
5042 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5043 "TMPL_TYPE_ATTR unspecified attribute "
5044 "occurred after unknown attribute %s "
5045 "in attr ref list",
5046 file, line,
5047 ar->unknown.da->name);
5048 }
5049 if (seen_unresolved) {
5051 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5052 "TMPL_TYPE_ATTR unspecified attribute "
5053 "occurred after unresolved attribute \"%s\""
5054 "in attr ref list",
5055 file, line,
5056 ar->ar_unresolved);
5057 }
5058 break;
5059
5061 seen_unresolved = ar;
5062 fr_fatal_assert_msg(ar->ar_unresolved_namespace,
5063 "CONSISTENCY CHECK FAILED %s[%u]: unresolved attr ref missing namespace",
5064 file, line);
5065 break;
5066
5068 seen_unknown = ar;
5069 if (seen_unresolved) {
5071 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5072 "TMPL_TYPE_ATTR unknown attribute \"%s\" "
5073 "occurred after unresolved attribute %s "
5074 "in attr ref list",
5075 file, line, ar->da->name,
5076 ar->ar_unresolved);
5077 }
5078 break;
5079 }
5080 }
5081}
5082
5083/** Verify fields of a tmpl_t make sense
5084 *
5085 * @note If the #tmpl_t is invalid, causes the server to exit.
5086 *
5087 * @param file obtained with __FILE__.
5088 * @param line obtained with __LINE__.
5089 * @param vpt to check.
5090 */
5091void tmpl_verify(char const *file, int line, tmpl_t const *vpt)
5092{
5093 uint8_t const *nz;
5094
5095 fr_assert(vpt);
5096
5098 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: tmpl_t type was "
5099 "TMPL_TYPE_UNINITIALISED (uninitialised)", file, line);
5100 }
5101
5102 if (vpt->type >= TMPL_TYPE_MAX) {
5103 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: tmpl_t type was %i "
5104 "(outside range of tmpl_type_table)", file, line, vpt->type);
5105 }
5106
5107 if (!vpt->name && (vpt->quote != T_INVALID)) {
5108 char quote = vpt->quote >= T_TOKEN_LAST ? '?' : fr_token_quote[vpt->quote];
5109
5110 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: Quote type '%c' (%i) was set for NULL name",
5111 file, line, quote, vpt->quote);
5112 }
5113
5114 if (vpt->name && (vpt->quote == T_INVALID)) {
5115 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: No quoting type was set for name \"%.*s\"",
5116 file, line, (int)vpt->len, vpt->name);
5117 }
5118
5119 /*
5120 * Do a memcmp of the bytes after where the space allocated for
5121 * the union member should have ended and the end of the union.
5122 * These should always be zero if the union has been initialised
5123 * properly.
5124 *
5125 * If they're still all zero, do TMPL_TYPE specific checks.
5126 */
5127 switch (vpt->type) {
5129 if (!vpt->data.unescaped) {
5130 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA_UNRESOLVED "
5131 "unescaped field is NULL", file, line);
5132 }
5133 break;
5134
5136 if (!vpt->data.xlat.ex) {
5137 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT "
5138 "has a NULL xlat.ex field", file, line);
5139
5140 }
5141
5142 if (!xlat_needs_resolving(vpt->data.xlat.ex)) {
5143 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT_UNRESOLVED "
5144 "does not have 'needs resolving' flag set", file, line);
5145 }
5146 break;
5147
5148 case TMPL_TYPE_XLAT:
5149 if (!vpt->data.xlat.ex) {
5150 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT "
5151 "has a NULL xlat.ex field", file, line);
5152
5153 }
5154 break;
5155
5156/* @todo When regexes get converted to xlat the flags field of the regex union is used
5157 case TMPL_TYPE_XLAT_UNRESOLVED:
5158 if (is_zeroed((uint8_t const *)&vpt->data, sizeof(vpt->data))) {
5159 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT_UNRESOLVED "
5160 "has non-zero bytes in its data union", file, line);
5161 }
5162 break;
5163
5164 case TMPL_TYPE_XLAT:
5165 if (CHECK_ZEROED(vpt, xlat)) {
5166 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT "
5167 "has non-zero bytes after the data.xlat pointer in the union", file, line);
5168 }
5169 break;
5170*/
5171
5172 case TMPL_TYPE_EXEC:
5174 /* tmpl_xlat(vpt) can be initialized */
5175 break;
5176
5178 if ((tmpl_attr_list_num_elements(tmpl_attr(vpt)) > 0) &&
5179 ((tmpl_attr_t *)tmpl_attr_list_tail(tmpl_attr(vpt)))->da) {
5180#ifndef NDEBUG
5182#endif
5183 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR_UNRESOLVED contains %u "
5184 "references", file, line, tmpl_attr_list_num_elements(tmpl_attr(vpt)));
5185 }
5186 break;
5187
5188 case TMPL_TYPE_ATTR:
5189 if ((nz = CHECK_ZEROED(vpt, attribute))) {
5190 PRINT_NON_ZEROED(vpt, attribute, nz);
5191 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR "
5192 "has non-zero bytes after the data.attribute struct in the union",
5193 file, line);
5194 }
5195
5197 fr_assert(vpt->rules.cast == FR_TYPE_NULL);
5198 break;
5199 }
5200
5203 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR "
5204 "da is marked as unknown, but address is not equal to the template's "
5205 "unknown da pointer", file, line);
5206 }
5207 /*
5208 * Raw attributes may not have been added to the dictionary yet
5209 */
5210 } else {
5211 fr_dict_attr_t const *da;
5212 fr_dict_t const *dict;
5213
5214 /*
5215 * Attribute may be present with multiple names
5216 */
5218 if (!dict) {
5219 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR "
5220 "attribute \"%s\" (%s) not rooted in a dictionary",
5223 }
5224
5225 da = tmpl_attr_tail_da(vpt);
5226 if (!tmpl_attr_tail_is_raw(vpt) && (da != tmpl_attr_tail_da(vpt))) {
5227 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR "
5228 "dictionary pointer %p \"%s\" (%s) "
5229 "and global dictionary pointer %p \"%s\" (%s) differ",
5230 file, line,
5233 da, da->name,
5234 fr_type_to_str(da->type));
5235 }
5236
5237 tmpl_attr_verify(file, line, vpt);
5238 }
5239 break;
5240
5241 case TMPL_TYPE_DATA:
5242 if ((nz = CHECK_ZEROED(vpt, literal))) {
5243 PRINT_NON_ZEROED(vpt, literal, nz);
5244 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA "
5245 "has non-zero bytes after the data.literal struct in the union",
5246 file, line);
5247 }
5248
5250 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA type was "
5251 "FR_TYPE_NULL (uninitialised)", file, line);
5252 }
5253
5255 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA type was "
5256 "%i (outside the range of fr_type_ts)", file, line, tmpl_value_type(vpt));
5257 }
5258 /*
5259 * Unlike fr_pair_ts we can't guarantee that fr_pair_t_TMPL buffers will
5260 * be talloced. They may be allocated on the stack or in global variables.
5261 */
5262 switch (tmpl_value_type(vpt)) {
5263 case FR_TYPE_STRING:
5265 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA char buffer not \\0 "
5266 "terminated", file, line);
5267 }
5268 break;
5269
5270 case FR_TYPE_STRUCTURAL:
5271 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA is of type TLV",
5272 file, line);
5273
5274 default:
5275 break;
5276 }
5277
5278 break;
5279
5283#ifndef HAVE_REGEX
5284 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_REGEX_XLAT_UNRESOLVED - No regex support",
5285 file, line);
5286#endif
5287 break;
5288
5289 case TMPL_TYPE_REGEX:
5290#ifdef HAVE_REGEX
5291 if (tmpl_regex(vpt) == NULL) {
5292 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_REGEX "
5293 "reg.ex field was NULL", file, line);
5294 }
5295#else
5296 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_REGEX - No regex support",
5297 file, line);
5298#endif
5299 break;
5300
5302 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_UNINITIALISED", file, line);
5303
5304 case TMPL_TYPE_MAX:
5305 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_MAX", file, line);
5306 }
5307}
5308#endif
5309
5310static const bool array_terminal[UINT8_MAX + 1] = {
5311 [ ']' ] = true,
5312};
5313
5314#define return_P(_x) fr_strerror_const(_x);goto return_p
5315
5316/** Preparse a string in preparation for passing it to tmpl_afrom_substr()
5317 *
5318 * Note that the input string is not modified, which means that the
5319 * tmpl_afrom_substr() function MUST un-escape it.
5320 *
5321 * The caller should pass 'out' and 'outlen' to tmpl_afrom_substr()
5322 * as 'in' and 'inlen'. The caller should also pass 'type'.
5323 * The caller should also pass do_unescape=true.
5324 *
5325 * @param[out] out start of the string to parse
5326 * @param[out] outlen length of the string to parse
5327 * @param in where we start looking for the string
5328 * @param inlen length of the input string
5329 * @param[out] type token type of the string.
5330 * @return
5331 * - > 0, amount of parsed string to skip, to get to the next token
5332 * - <=0, -offset in 'start' where the parse error was located
5333 */
5334ssize_t tmpl_preparse(char const **out, size_t *outlen, char const *in, size_t inlen,
5336{
5337 char const *p = in, *end = in + inlen;
5338 char quote;
5339 char close;
5340 int depth;
5341 bool triple;
5342
5343 *type = T_INVALID;
5344
5345 while (isspace((uint8_t) *p) && (p < end)) p++;
5346 if (p >= end) return p - in;
5347
5348 switch (*p) {
5349 /*
5350 * Allow bare xlat's
5351 */
5352 case '%':
5353 if (p[1] != '{') {
5354 char const *q;
5355
5356 q = p + 1;
5357
5358 /*
5359 * Function syntax: %foo(...)
5360 */
5361 while ((q < end) && (isalnum((int) *q) || (*q == '.') || (*q == '_') || (*q == '-'))) {
5362 q++;
5363 }
5364
5365 if (*q != '(') {
5366 p++;
5367 fr_strerror_const("Invalid character after '%'");
5368 return_p:
5369 return -(p - in);
5370 }
5371
5372 /*
5373 * Return the whole %foo(...) string.
5374 */
5375 *out = p;
5376 if (*type == T_INVALID) *type = T_BARE_WORD;
5377 close = ')';
5378
5379 p = q + 1;
5380 depth = 1;
5381 goto loop;
5382 }
5383
5384 /*
5385 * For now, %{...} is treated as a double-quoted
5386 * string. Once we clean other things up, the
5387 * xlats will be treated as strongly typed values
5388 * / lists on their own.
5389 */
5390 if (*type == T_INVALID) *type = T_BARE_WORD;
5391 depth = 0;
5392 close = '}';
5393
5394 /*
5395 * Xlat's are quoted by %{...} / %(...) nesting, not by
5396 * escapes, so we need to do special escaping.
5397 */
5398 *out = p;
5399 loop:
5400 while (*p) {
5401 /*
5402 * End of expansion. Return the entire
5403 * expansion, including the enclosing %{}
5404 * characters.
5405 */
5406 if ((*p == '}') || (*p == ')')) {
5407 bool match = (*p == close);
5408
5409 p++;
5410 depth--;
5411
5412 if (depth == 0) {
5413 if (!match) break;
5414
5415 *outlen = p - (*out);
5416 return p - in;
5417 }
5418 continue;
5419 }
5420
5421 if (*p == '\\') {
5422 p++;
5423 if (!p[1]) {
5424 return_P("End of string after escape");
5425 }
5426
5427 p++;
5428 continue;
5429 }
5430
5431 if ((p[0] == '%') && ((p[1] == '{') || (p[1] == '('))) {
5432 if (!p[2]) {
5433 return_P("End of string after expansion");
5434 }
5435
5436 p += 2;
5437 depth++;
5438 continue;
5439 }
5440
5441 /*
5442 * Allow (...) and {...}
5443 */
5444 if ((*p == '{') || (*p == '(')) {
5445 p++;
5446 depth++;
5447 continue;
5448 }
5449
5450 p++;
5451 }
5452
5453 /*
5454 * End of input without end of string.
5455 * Point the error to the start of the string.
5456 */
5457 p = *out;
5458 return_P("Unterminated expansion");
5459
5460 case '/':
5461 goto bare_word;
5462
5463 case '\'':
5464 quote = *(p++);
5466 goto skip_string;
5467
5468 case '`':
5469 quote = *(p++);
5471 goto skip_string;
5472
5473 case '"':
5474 quote = *(p++);
5476
5477 /*
5478 * We're not trying to do a *correct* parsing of
5479 * every string here. We're trying to do a
5480 * simple parse that isn't wrong. We therefore
5481 * accept most anything that's vaguely well
5482 * formed, and rely on the next stage to do a
5483 * more rigorous check.
5484 */
5485 skip_string:
5486 if ((inlen > 3) && (p[0] == quote) && (p[1] == quote)) {
5487 triple = true;
5488 p += 2;
5489 } else {
5490 triple = false;
5491 }
5492 *out = p;
5493
5494 while (*p) {
5495 if (p >= end) goto unterminated;
5496
5497 /*
5498 * End of string. Tell the caller the
5499 * length of the data inside of the
5500 * string, and return the number of
5501 * characters to skip.
5502 */
5503 if (*p == quote) {
5504 if (!triple) {
5505 *outlen = p - (*out);
5506 p++;
5507 return p - in;
5508
5509 }
5510
5511 if (((end - p) >= 3) && (p[1] == quote) && (p[2] == quote)) {
5512 *outlen = p - (*out);
5513 p += 3;
5514 return p - in;
5515 }
5516
5517 p++;
5518 continue;
5519 }
5520
5521 if (*p == '\\') {
5522 p++;
5523 if (!p[1]) {
5524 return_P("End of string after escape");
5525 }
5526 }
5527 p++;
5528 }
5529
5530 /*
5531 * End of input without end of string.
5532 * Point the error to the start of the string.
5533 */
5534 unterminated:
5535 p = *out;
5536 return_P("Unterminated string");
5537
5538 case '&':
5539 *out = p; /* the output string starts with '&' */
5540 p++;
5541 quote = '[';
5542 goto skip_word;
5543
5544 default:
5545 bare_word:
5546 *out = p;
5547 quote = '['; /* foo[1] is OK */
5548
5549 skip_word:
5550 *type = T_BARE_WORD;
5551 depth = 0;
5552
5553 /*
5554 * Allow *most* things. But stop on spaces and special characters.
5555 */
5556 while (*p) {
5557 if (isspace((uint8_t) *p)) {
5558 break;
5559 }
5560
5561 if (*p == '$') {
5562 if (p[1] == '{') {
5563 p += 2;
5564 depth++;
5565 continue;
5566
5567 } else if ((p[1] == 'E') &&
5568 (p[2] == 'N') &&
5569 (p[3] == 'V') &&
5570 (p[4] == '{')) {
5571 p += 5;
5572 depth++;
5573 continue;
5574
5575 } else {
5576 /*
5577 * Bare '$' is wrong...
5578 */
5579 break;
5580 }
5581 }
5582
5583 if (*p == '%') {
5584 if (p[1] == '{') {
5585 p += 2;
5586 depth++;
5587 continue;
5588 }
5589
5590 p++;
5591 continue;
5592 }
5593
5594 /*
5595 * If we're inside of a ${...} expansion,
5596 * then allow everything until the
5597 * closing '}'. This means that we can
5598 * do ${foo[bar].baz}, among other
5599 * thingds.
5600 */
5601 if (depth > 0) {
5602 if (*p == '}') {
5603 depth--;
5604 }
5605
5606 p++;
5607 continue;
5608 }
5609
5610 /*
5611 * '-' is special. We allow it for
5612 * attribute names, BUT it's a
5613 * terminating token if the NEXT
5614 * character is '='.
5615 *
5616 * We have the same criteria for IPv6
5617 * addresses and tagged attributes. ':'
5618 * is allowed, but ':=' is a breaking
5619 * token.
5620 */
5621 if ((*p == '-') || (*p == ':')) {
5622 if (p[1] == '=') break;
5623 p++;
5624 continue;
5625 }
5626
5627 /*
5628 * Allowed in attribute names, and/or
5629 * host names and IP addresses, and IPv6 addresses.
5630 */
5631 if ((*p == '.') || (*p == '/') || (*p == '_') || (*p == '*') ||
5632 (*p == ']') || (*p == '@')) {
5633 p++;
5634 continue;
5635 }
5636
5637 /*
5638 * [...] is an IPv6 address.
5639 */
5640 if ((p == in) && (*p == '[')) {
5641 p++;
5642 continue;
5643 }
5644
5645 /*
5646 * Allow letters and numbers
5647 */
5648 if (((*p >= 'a') && (*p <= 'z')) ||
5649 ((*p >= 'A') && (*p <= 'Z')) ||
5650 ((*p >= '0') && (*p <= '9'))) {
5651 p++;
5652 continue;
5653 }
5654
5655 /*
5656 * Allow UTF-8 sequences.
5657 */
5658 if (*(uint8_t const *)p > 0x80) {
5659 p++;
5660 continue;
5661 }
5662
5663 /*
5664 * If it's an attribute reference, allow
5665 * a few more things inside of a "[...]"
5666 * block.
5667 */
5668 if (*p == '[') {
5669 if (quote != '[') {
5670 return_P("Invalid location for '['");
5671 }
5672
5673 p++;
5674
5675 /*
5676 * Allow [#], etc. But stop
5677 * immediately after the ']'.
5678 */
5679 if ((*p == '#') || (*p == '*') || (*p == 'n')) {
5680 p++;
5681
5682 } else if (*p == '(') {
5683 ssize_t slen;
5684 bool eol = false;
5685
5686 slen = fr_skip_condition(p, end, array_terminal, &eol);
5687 if (slen < 0) {
5688 p += -slen;
5689 return -(p - in);
5690 }
5691 p += slen;
5692 continue;
5693
5694 } else {
5695 /*
5696 * Allow numbers as array indexes
5697 */
5698 while ((*p >= '0') && (*p <= '9')) {
5699 p++;
5700 }
5701
5702 if (*p != ']') {
5703 return_P("Array index is not an integer");
5704 }
5705 }
5706
5707 if (*p == ']') {
5708 p++;
5709 continue;
5710 }
5711 }
5712
5713 /*
5714 * Everything else is a breaking token
5715 */
5716 break;
5717 }
5718
5719 /*
5720 * Give some slightly better error messages.
5721 */
5722 if (*p == '\\') {
5723 return_P("Unexpected escape");
5724 }
5725
5726 if ((*p == '"') || (*p == '\'') || (*p == '`')) {
5727 return_P("Unexpected start of string");
5728 }
5729
5730 if (p == *out) {
5731 return_P("Empty string is invalid");
5732 }
5733
5734 *outlen = p - (*out);
5735 break;
5736 }
5737
5738 return p - in;
5739}
5740
5741/** Return whether or not async is required for this tmpl.
5742 *
5743 * If the tmpl is needs_async, then it is async
5744 * If the tmpl is not needs_async, then it will not yield
5745 *
5746 * If the tmpl yields, then async is required.
5747 */
5749{
5750 switch (vpt->type) {
5751 case TMPL_TYPE_EXEC: /* we don't have "exec no-wait" here */
5752 case TMPL_TYPE_XLAT_UNRESOLVED: /* we have no idea, so be safe */
5753#ifndef HAVE_REGEX
5755#endif
5756 return true;
5757
5758#ifndef HAVE_REGEX
5760#endif
5761 case TMPL_TYPE_XLAT: /* synchronous xlats use unlang_interpret_synchronous() */
5762 default:
5763 return false;
5764 }
5765}
5766
5767/** Initialize a set of rules from a parent set of rules, and a parsed tmpl_t
5768 *
5769 */
5771{
5772 fr_dict_attr_t const *da;
5773 fr_dict_attr_t const *ref;
5774 fr_dict_t const *dict, *internal;
5775
5776 *out = *parent;
5777 /* don't set ->parent=parent, that is only for switching subrequest, etc. */
5778
5779 if (!tmpl_is_attr(vpt)) return;
5780
5781 da = tmpl_attr_tail_da(vpt);
5782
5783 /*
5784 * The input tmpl is a leaf. We must parse the child as
5785 * a normal attribute reference (as with the parent tmpl).
5786 */
5787 if (!fr_type_structural[da->type]) {
5788 return;
5789 }
5790
5791 if (vpt->rules.attr.request_def) {
5792 tmpl_request_ref_list_acopy(ctx, &out->attr.request_def, vpt->rules.attr.request_def);
5793 }
5794 out->attr.list_def = tmpl_list(vpt);
5795
5796 /*
5797 * Parse the child attributes in the context of the parent struct / tlv / whatever.
5798 */
5799 if (da->type != FR_TYPE_GROUP) {
5800 out->attr.dict_def = fr_dict_by_da(da);
5801 out->attr.namespace = da;
5802 return;
5803 }
5804
5805 ref = fr_dict_attr_ref(da);
5806 dict = fr_dict_by_da(ref);
5807 internal = fr_dict_internal();
5808
5809 /*
5810 * Groups MAY change dictionaries. If so, then swap the dictionary and the parent.
5811 */
5812 if ((dict != internal) && (dict != out->attr.dict_def)) {
5813 out->attr.dict_def = dict;
5814 out->attr.namespace = ref;
5815 }
5816
5817 /*
5818 * Otherwise the reference is swapping FROM a protocol
5819 * dictionary TO the internal dictionary, and TO an
5820 * internal group. We fall back to leaving well enough
5821 * alone, and leave things as-is. This allows internal
5822 * grouping attributes to appear anywhere.
5823 */
5824}
5825
5826static void tmpl_attr_rules_debug(tmpl_attr_rules_t const *at_rules)
5827{
5828 FR_FAULT_LOG("\tdict_def = %s", at_rules->dict_def ? fr_dict_root(at_rules->dict_def)->name : "");
5829 FR_FAULT_LOG("\tnamespace = %s", at_rules->namespace ? at_rules->namespace->name : "");
5830
5831 FR_FAULT_LOG("\tlist_def = %s", at_rules->list_def ? at_rules->list_def->name : "");
5832
5833 FR_FAULT_LOG("\tallow_unknown = %u", at_rules->allow_unknown);
5834 FR_FAULT_LOG("\tallow_unresolved = %u", at_rules->allow_unresolved);
5835 FR_FAULT_LOG("\tallow_wildcard = %u", at_rules->allow_wildcard);
5836 FR_FAULT_LOG("\tallow_foreign = %u", at_rules->allow_foreign);
5837 FR_FAULT_LOG("\tdisallow_filters = %u", at_rules->disallow_filters);
5838}
5839
5840
5842{
5843 FR_FAULT_LOG("\tparent = %p", rules->parent);
5844 FR_FAULT_LOG(" attr {");
5845 tmpl_attr_rules_debug(&rules->attr);
5846 FR_FAULT_LOG(" }");
5847 FR_FAULT_LOG("\tenumv = %s", rules->enumv ? rules->enumv->name : "");
5848 FR_FAULT_LOG("\tcast = %s", fr_type_to_str(rules->cast));
5849 FR_FAULT_LOG("\tat_runtime = %u", rules->at_runtime);
5850 FR_FAULT_LOG("\tliterals_safe_for = %lx", rules->literals_safe_for);
5851
5852}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
va_end(args)
static int const char * fmt
Definition acutest.h:573
int const char int line
Definition acutest.h:702
va_start(args, fmt)
#define fr_base16_decode(_err, _out, _in, _no_trailing)
Definition base16.h:95
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define STRINGIFY(x)
Definition build.h:197
#define CMP_RETURN(_a, _b, _field)
Return if the comparison is not 0 (is unequal)
Definition build.h:121
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:112
#define unlikely(_x)
Definition build.h:383
#define NUM_ELEMENTS(_t)
Definition build.h:339
static char const * skip_word(char const *text)
Definition command.c:1880
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition dbuff.h:354
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:514
#define fr_fatal_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:191
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:210
#define fr_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error.
Definition debug.h:216
#define FR_FAULT_LOG(_fmt,...)
Definition debug.h:49
#define fr_fatal_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:184
#define MEM(x)
Definition debug.h:36
fr_slen_t fr_dict_attr_by_name_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_attr_t const *parent, fr_sbuff_t *name, fr_sbuff_term_t const *tt))
unsigned int name_only
this attribute should always be referred to by name.
Definition dict.h:100
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition dict_util.c:4629
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition dict_util.c:2609
fr_dict_attr_t const * fr_dict_attr_common_parent(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition dict_util.c:2040
static fr_slen_t err
Definition dict.h:840
static fr_dict_attr_t * fr_dict_attr_unknown_vendor_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int vendor)
Definition dict.h:579
bool const fr_dict_attr_allowed_chars[UINT8_MAX+1]
Characters that are allowed in dictionary attribute names.
Definition dict_util.c:47
static fr_dict_attr_t * fr_dict_attr_unknown_copy(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Definition dict.h:559
fr_dict_attr_t const * fr_dict_attr_unknown_add(fr_dict_t *dict, fr_dict_attr_t const *old)
Converts an unknown to a known by adding it to the internal dictionaries.
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3266
fr_dict_attr_t * fr_dict_attr_unconst(fr_dict_attr_t const *da)
Coerce to non-const.
Definition dict_util.c:4641
fr_dict_attr_t * fr_dict_attr_unknown_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da))
Copy a known or unknown attribute to produce an unknown attribute with the specified name.
static fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Definition dict.h:586
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2403
int fr_dict_attr_unknown_parent_to_known(fr_dict_attr_t *da, fr_dict_attr_t const *parent)
Fixup the parent of an unknown attribute using an equivalent known attribute.
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:236
void fr_dict_attr_unknown_free(fr_dict_attr_t const **da)
Free dynamically allocated (unknown attributes)
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4654
fr_slen_t fr_dict_attr_search_by_qualified_name_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_t const *dict_def, fr_sbuff_t *name, fr_sbuff_term_t const *tt, bool internal, bool foreign))
Locate a qualified fr_dict_attr_t by its name and a dictionary qualifier.
Definition dict_util.c:2978
fr_slen_t fr_dict_attr_search_by_name_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_t const *dict_def, fr_sbuff_t *name, fr_sbuff_term_t const *tt, bool internal, bool foreign))
Locate a fr_dict_attr_t by its name in the top level namespace of a dictionary.
Definition dict_util.c:3007
static fr_slen_t fr_dict_enum_name_afrom_substr(TALLOC_CTX *ctx, char **out, fr_sbuff_parse_error_t *err, fr_sbuff_t *in, fr_sbuff_term_t const *tt) 1(fr_dict_enum_name_from_substr
#define FR_DICT_MAX_TLV_STACK
Maximum TLV stack size.
Definition dict.h:499
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition dict.h:295
@ FR_DICT_ATTR_OK
No error.
Definition dict.h:296
@ FR_DICT_ATTR_NOT_DESCENDENT
Attribute is not a descendent of the parent attribute.
Definition dict.h:302
@ FR_DICT_ATTR_NO_CHILDREN
Child lookup in attribute with no children.
Definition dict.h:306
int fr_dict_attr_add(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, unsigned int attr, fr_type_t type, fr_dict_attr_flags_t const *flags))
Add an attribute to the dictionary.
Definition dict_util.c:1715
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition dict_util.c:3331
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:158
fr_dict_enum_value_t const * fr_dict_enum_by_name(fr_dict_attr_t const *da, char const *name, ssize_t len)
Definition dict_util.c:3439
static fr_slen_t in
Definition dict.h:840
#define FR_DICT_ATTR_MAX_NAME_LEN
Maximum length of a attribute name.
Definition dict.h:479
Values of the encryption flags.
Value of an enumerated attribute.
Definition dict.h:232
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:184
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
#define FR_DLIST_HEAD(_name)
Expands to the type name used for the head wrapper structure.
Definition dlist.h:1122
#define FR_IPADDR_STRLEN
Like INET6_ADDRSTRLEN but includes space for the textual Zone ID.
Definition inet.h:89
talloc_free(reap)
static char * stack[MAX_STACK]
Definition radmin.c:159
unsigned short uint16_t
fr_type_t
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_INT8
8 Bit signed integer.
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_ETHERNET
48 Bit Mac-Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_MAX
Number of defined data types.
@ 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_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_INT32
32 Bit signed integer.
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
@ FR_TYPE_FLOAT64
Double precision floating point.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
unsigned long int size_t
#define UINT8_MAX
fr_sbuff_parse_error_t
@ FR_SBUFF_PARSE_ERROR_NOT_FOUND
String does not contain a token matching the output type.
@ FR_SBUFF_PARSE_ERROR_FORMAT
Format of data was invalid.
@ FR_SBUFF_PARSE_OK
No error.
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
char * fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
Definition print.c:851
void fr_proto_da_stack_build_partial(fr_da_stack_t *stack, fr_dict_attr_t const *parent, fr_dict_attr_t const *da)
Complete the DA stack for a child attribute.
Definition proto.c:159
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:81
static uint32_t mask
Definition rbmonkey.c:39
fr_dict_attr_t const * request_attr_request
Definition request.c:43
fr_dict_attr_t const * request_attr_control
Definition request.c:45
fr_dict_attr_t const * request_attr_local
Definition request.c:47
fr_dict_attr_t const * request_attr_state
Definition request.c:46
fr_dict_attr_t const * request_attr_reply
Definition request.c:44
static char const * name
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static UINT8_MAX+1], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
Definition sbuff.c:1779
bool const sbuff_char_class_hex[UINT8_MAX+1]
Definition sbuff.c:94
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
Definition sbuff.c:2154
size_t fr_sbuff_adv_past_strcase(fr_sbuff_t *sbuff, char const *needle, size_t needle_len)
Return true and advance past the end of the needle if needle occurs next in the sbuff.
Definition sbuff.c:1745
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:1854
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:2090
#define fr_sbuff_start(_sbuff_or_marker)
#define fr_sbuff_out_by_longest_prefix(_match_len, _out, _table, _sbuff, _def)
#define FR_SBUFF_IN_TABLE_STR_RETURN(_sbuff, _table, _number, _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 FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_adv_past_whitespace(_sbuff, _len, _tt)
#define fr_sbuff_adv_past_strcase_literal(_sbuff, _needle)
#define fr_sbuff_current(_sbuff_or_marker)
#define FR_SBUFF_REPARSE(_sbuff_or_marker)
#define FR_SBUFF_IN_ESCAPE_BUFFER_RETURN(...)
#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_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_IN_SPRINTF_RETURN(...)
#define FR_SBUFF(_sbuff_or_marker)
#define FR_SBUFF_IN_BSTRNCPY_RETURN(...)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define fr_sbuff_out(_err, _out, _in)
#define fr_sbuff_switch(_sbuff_or_marker, _eob)
#define FR_SBUFF_OUT(_start, _len_or_end)
#define fr_sbuff_used(_sbuff_or_marker)
#define FR_SBUFF_IN_STRCPY_RETURN(...)
#define FR_SBUFF_IN_BSTRCPY_BUFFER_RETURN(...)
Set of terminal elements.
#define tmpl_contains_xlat(vpt)
Definition tmpl.h:227
#define TMPL_VERIFY(_vpt)
Definition tmpl.h:961
#define tmpl_value_length(_tmpl)
Definition tmpl.h:938
static char const * tmpl_type_to_str(tmpl_type_t type)
Return a static string containing the type name.
Definition tmpl.h:634
#define tmpl_is_uninitialised(vpt)
Helpers to verify the type of tmpl_t.
Definition tmpl.h:204
#define tmpl_is_attr_unresolved(vpt)
Definition tmpl.h:219
enum requests_ref_e tmpl_request_ref_t
static bool tmpl_attr_tail_is_unspecified(tmpl_t const *vpt)
Return true if the last attribute reference is "unspecified".
Definition tmpl.h:726
#define NUM_LAST
Definition tmpl.h:393
#define tmpl_rules_enumv(_tmpl)
Definition tmpl.h:943
static bool tmpl_attr_tail_is_normal(tmpl_t const *vpt)
Return true if the last attribute reference is "normal".
Definition tmpl.h:710
#define tmpl_value(_tmpl)
Definition tmpl.h:937
#define ar_is_unknown(_ar)
Definition tmpl.h:507
static bool tmpl_attr_tail_is_unknown(tmpl_t const *vpt)
Return true if the last attribute reference is "unknown".
Definition tmpl.h:742
#define tmpl_contains_regex(vpt)
Definition tmpl.h:226
fr_value_box_safe_for_t literals_safe_for
safe_for value assigned to literal values in xlats, execs, and data.
Definition tmpl.h:347
#define ar_is_raw(_ar)
Definition tmpl.h:509
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
#define NUM_ALL
Definition tmpl.h:391
fr_dict_attr_t const * enumv
Enumeration attribute used to resolve enum values.
Definition tmpl.h:338
tmpl_rules_t const * parent
for parent / child relationships
Definition tmpl.h:333
#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
static bool tmpl_attr_is_list_attr(tmpl_attr_t const *ar)
Return true if the tmpl_attr is one of the list types.
Definition tmpl.h:679
#define ar_filter_is_num(_ar)
Definition tmpl.h:518
#define tmpl_rules_cast(_tmpl)
Definition tmpl.h:942
@ TMPL_TYPE_REGEX_UNCOMPILED
Regex where compilation is possible but hasn't been performed yet.
Definition tmpl.h:158
@ TMPL_TYPE_MAX
Marker for the last tmpl type.
Definition tmpl.h:199
@ TMPL_TYPE_ATTR_UNRESOLVED
An attribute reference that we couldn't resolve but looked valid.
Definition tmpl.h:185
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition tmpl.h:142
@ TMPL_TYPE_XLAT
Pre-parsed xlat expansion.
Definition tmpl.h:146
@ TMPL_TYPE_EXEC
Callout to an external script or program.
Definition tmpl.h:150
@ TMPL_TYPE_REGEX_XLAT_UNRESOLVED
A regular expression with unresolved xlat functions or attribute references.
Definition tmpl.h:197
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition tmpl.h:138
@ TMPL_TYPE_REGEX
Compiled (and possibly JIT'd) regular expression.
Definition tmpl.h:154
@ TMPL_TYPE_DATA_UNRESOLVED
Unparsed literal string.
Definition tmpl.h:179
@ TMPL_TYPE_XLAT_UNRESOLVED
A xlat expansion with unresolved xlat functions or attribute references.
Definition tmpl.h:193
@ TMPL_TYPE_REGEX_XLAT
A regex containing xlat expansions.
Definition tmpl.h:162
@ TMPL_TYPE_EXEC_UNRESOLVED
An exec with unresolved xlat function or attribute references.
Definition tmpl.h:189
@ TMPL_TYPE_UNINITIALISED
Uninitialised.
Definition tmpl.h:134
#define tmpl_is_regex_xlat(vpt)
Definition tmpl.h:215
#define NUM_COUNT
Definition tmpl.h:392
#define tmpl_assert_type(_cond)
Convenience macro for printing a meaningful assert message when we get a bad tmpl type.
Definition tmpl.h:620
#define tmpl_contains_attr(vpt)
Definition tmpl.h:225
#define ar_da
Definition tmpl.h:499
#define TMPL_FLAG_REGEX
Is a type of regular expression.
Definition tmpl.h:116
#define ar_filter_is_cond(_ar)
Definition tmpl.h:519
tmpl_xlat_rules_t xlat
Rules/data for parsing xlats.
Definition tmpl.h:336
static char const * tmpl_attr_tail_unresolved(tmpl_t const *vpt)
Return the last attribute reference unresolved da.
Definition tmpl.h:869
bool at_runtime
Produce an ephemeral/runtime tmpl.
Definition tmpl.h:344
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:920
#define TMPL_ATTR_VERIFY(_vpt)
Definition tmpl.h:960
bool force_dict_def
Use supplied dict_def even if original vpt->rules->dict_def was not NULL.
Definition tmpl.h:370
#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:365
#define NUM_UNSPEC
Definition tmpl.h:390
static size_t tmpl_attr_num_elements(tmpl_t const *vpt)
The number of attribute references contained within a tmpl.
Definition tmpl.h:896
#define tmpl_value_type(_tmpl)
Definition tmpl.h:939
#define tmpl_attr(_tmpl)
Definition tmpl.h:654
tmpl_attr_error_t
Definition tmpl.h:1004
@ TMPL_ATTR_ERROR_INVALID_ARRAY_INDEX
Invalid array index.
Definition tmpl.h:1023
@ TMPL_ATTR_ERROR_LIST_NOT_ALLOWED
List qualifier is not allowed here.
Definition tmpl.h:1008
@ TMPL_ATTR_ERROR_UNRESOLVED_NOT_ALLOWED
Attribute couldn't be found in the dictionaries.
Definition tmpl.h:1014
@ TMPL_ATTR_ERROR_BAD_CAST
Specified cast was invalid.
Definition tmpl.h:1027
@ TMPL_ATTR_ERROR_INVALID_NAME
Attribute ref length is zero, or longer than the maximum.
Definition tmpl.h:1016
@ TMPL_ATTR_ERROR_MISSING_TERMINATOR
Unexpected text found after attribute reference.
Definition tmpl.h:1026
@ TMPL_ATTR_ERROR_LIST_MISSING
List qualifier is required, but missing.
Definition tmpl.h:1009
@ TMPL_ATTR_ERROR_NONE
No error.
Definition tmpl.h:1005
@ TMPL_ATTR_ERROR_FOREIGN_NOT_ALLOWED
Attribute resolved in a dictionary different to the one specified.
Definition tmpl.h:1020
@ TMPL_ATTR_ERROR_UNKNOWN_NOT_ALLOWED
Attribute specified as OID, could not be found in the dictionaries, and is disallowed because 'disall...
Definition tmpl.h:1010
@ TMPL_ATTR_ERROR_FILTER_NOT_ALLOWED
Filters disallowed by rules.
Definition tmpl.h:1022
@ TMPL_ATTR_ERROR_EMPTY
Attribute ref contains no data.
Definition tmpl.h:1006
@ TMPL_ATTR_ERROR_NESTING_TOO_DEEP
Too many levels of nesting.
Definition tmpl.h:1025
#define tmpl_is_data_unresolved(vpt)
Definition tmpl.h:217
fr_type_t cast
Whether there was an explicit cast.
Definition tmpl.h:340
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:335
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:801
@ TMPL_ATTR_LIST_REQUIRE
Attribute refs are required to have a list.
Definition tmpl.h:264
@ TMPL_ATTR_LIST_ALLOW
Attribute refs are allowed to have a list.
Definition tmpl.h:262
@ TMPL_ATTR_LIST_FORBID
Attribute refs are forbidden from having a list.
Definition tmpl.h:263
enum tmpl_type_e tmpl_type_t
Types of tmpl_t.
static fr_dict_attr_t const * tmpl_attr_tail_unknown(tmpl_t const *vpt)
Return the last attribute reference unknown da.
Definition tmpl.h:853
#define tmpl_is_regex(vpt)
Definition tmpl.h:213
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
static bool tmpl_attr_tail_is_raw(tmpl_t const *vpt)
Return true if the last attribute reference is "raw".
Definition tmpl.h:774
@ REQUEST_OUTER
request_t containing the outer layer of the EAP conversation.
Definition tmpl.h:92
@ REQUEST_PARENT
Parent (whatever it is).
Definition tmpl.h:96
@ REQUEST_UNKNOWN
Unknown request.
Definition tmpl.h:97
@ REQUEST_CURRENT
The current request (default).
Definition tmpl.h:91
struct tmpl_rules_s tmpl_rules_t
Definition tmpl.h:233
#define tmpl_is_regex_xlat_unresolved(vpt)
Definition tmpl.h:221
#define tmpl_is_regex_uncompiled(vpt)
Definition tmpl.h:214
fr_dict_attr_t const * enumv
for resolving T_BARE_WORD
Definition tmpl.h:373
#define TMPL_MAX_REQUEST_REF_NESTING
The maximum number of request references allowed.
Definition tmpl.h:85
tmpl_attr_type_t
Definition tmpl.h:376
@ TMPL_ATTR_TYPE_UNSPEC
No attribute was specified as this level only a filter.
Definition tmpl.h:378
@ TMPL_ATTR_TYPE_NORMAL
Normal, resolved, attribute ref.
Definition tmpl.h:377
@ TMPL_ATTR_TYPE_UNKNOWN
We have an attribute number but it doesn't match anything in the dictionary, or isn't a child of the ...
Definition tmpl.h:380
@ TMPL_ATTR_TYPE_UNRESOLVED
We have a name, but nothing else to identify the attribute.
Definition tmpl.h:385
@ TMPL_ATTR_FILTER_TYPE_TMPL
Filter is a tmpl.
Definition tmpl.h:407
@ TMPL_ATTR_FILTER_TYPE_INDEX
Filter is an index type.
Definition tmpl.h:405
@ TMPL_ATTR_FILTER_TYPE_CONDITION
Filter is a condition.
Definition tmpl.h:406
@ TMPL_ATTR_FILTER_TYPE_NONE
No filter present.
Definition tmpl.h:404
@ TMPL_ATTR_FILTER_TYPE_EXPR
Filter is an expression.
Definition tmpl.h:408
fr_event_list_t * runtime_el
The eventlist to use for runtime instantiation of xlats.
Definition tmpl.h:324
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:223
tmpl_attr_filter_type_t _CONST type
Type of filter this is.
Definition tmpl.h:412
#define ar_filter_is_none(_ar)
Definition tmpl.h:517
Similar to tmpl_rules_t, but used to specify parameters that may change during subsequent resolution ...
Definition tmpl.h:364
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:332
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
ssize_t fr_skip_condition(char const *start, char const *end, bool const terminal[static UINT8_MAX+1], bool *eol)
Skip a conditional expression.
Definition skip.c:264
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
return count
Definition module.c:155
fr_aka_sim_id_type_t type
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
uint8_t disallow_filters
disallow filters.
Definition tmpl.h:314
uint8_t allow_unresolved
Allow attributes that look valid but were not found in the dictionaries.
Definition tmpl.h:304
uint8_t allow_foreign
Allow arguments not found in dict_def.
Definition tmpl.h:312
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:273
uint8_t allow_wildcard
Allow the special case of .
Definition tmpl.h:309
uint8_t allow_unknown
Allow unknown attributes i.e.
Definition tmpl.h:301
An element in a list of nested attribute references.
Definition tmpl.h:430
unsigned int _CONST resolve_only
This reference and those before it.
Definition tmpl.h:453
unsigned int _CONST is_raw
Definition tmpl.h:456
fr_dict_attr_t const *_CONST da
Resolved dictionary attribute.
Definition tmpl.h:434
fr_dict_attr_t const *_CONST parent
The parent we used when trying to resolve the attribute originally.
Definition tmpl.h:448
tmpl_attr_filter_t _CONST filter
Filter associated with the attribute reference.
Definition tmpl.h:460
tmpl_attr_type_t _CONST type
is a raw reference
Definition tmpl.h:458
Define manipulation functions for the attribute reference list.
Definition tmpl.h:471
tmpl_request_ref_t _CONST request
Definition tmpl.h:475
#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
char * talloc_bstrdup(TALLOC_CTX *ctx, char const *in)
Binary safe strdup function.
Definition talloc.c:558
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:586
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:467
#define talloc_get_type_abort_const
Definition talloc.h:287
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:229
#define talloc_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
Definition talloc.h:180
fr_slen_t fr_time_delta_from_substr(fr_time_delta_t *out, fr_sbuff_t *in, fr_time_res_t hint, bool no_trailing, fr_sbuff_term_t const *tt)
Create fr_time_delta_t from a string.
Definition time.c:214
@ FR_TIME_RES_SEC
Definition time.h:50
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
Escaping rules for tmpls.
Definition tmpl_escape.h:80
int8_t tmpl_request_ref_list_cmp(FR_DLIST_HEAD(tmpl_request_list) const *a, FR_DLIST_HEAD(tmpl_request_list) const *b)
Compare a list of request qualifiers.
static ssize_t tmpl_afrom_time_delta(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
void tmpl_request_ref_list_debug(FR_DLIST_HEAD(tmpl_request_list) const *rql)
Dump a request list to stderr.
fr_slen_t tmpl_request_ref_list_print(fr_sbuff_t *out, FR_DLIST_HEAD(tmpl_request_list) const *rql)
void tmpl_attr_set_list(tmpl_t *vpt, fr_dict_attr_t const *list)
tmpl_t * tmpl_init_shallow(tmpl_t *vpt, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len, tmpl_rules_t const *t_rules)
Initialise a tmpl without copying the input name string.
fr_slen_t tmpl_request_ref_list_afrom_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err, FR_DLIST_HEAD(tmpl_request_list) **out, fr_sbuff_t *in)
Parse one or more request references, allocing a new list and adding the references to it.
static fr_table_num_sorted_t const attr_num_table[]
Special attribute reference indexes.
void tmpl_unresolve(tmpl_t *vpt)
Reset the tmpl, leaving only the name in place.
void tmpl_set_xlat(tmpl_t *vpt, xlat_exp_head_t *xlat)
Change the default dictionary in the tmpl's resolution rules.
static void tmpl_type_init(tmpl_t *vpt, tmpl_type_t type)
Initialise fields inside a tmpl depending on its type.
#define RESOLVED_SET(_flags)
static fr_slen_t tmpl_attr_ref_afrom_unresolved_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t *vpt, fr_dict_attr_t const *parent, fr_dict_attr_t const *namespace, fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
Parse an unresolved attribute, i.e.
void tmpl_attr_set_request_ref(tmpl_t *vpt, FR_DLIST_HEAD(tmpl_request_list) const *request_def)
Set the request for an attribute ref.
void tmpl_set_name_printf(tmpl_t *vpt, fr_token_t quote, char const *fmt,...)
Set the name on a pre-initialised tmpl.
int tmpl_attr_set_da(tmpl_t *vpt, fr_dict_attr_t const *da)
Replace the current attribute reference.
static fr_table_num_ordered_t const attr_table[]
Attr ref types.
static int tmpl_xlat_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules)
Resolve an unresolved xlat, i.e.
fr_table_num_sorted_t const tmpl_request_ref_table[]
Map keywords to tmpl_request_ref_t values.
int tmpl_afrom_value_box(TALLOC_CTX *ctx, tmpl_t **out, fr_value_box_t *data, bool steal)
Create a tmpl_t from a fr_value_box_t.
tmpl_t * tmpl_alloc(TALLOC_CTX *ctx, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len)
Create a new heap allocated tmpl_t.
int tmpl_attr_unknown_add(tmpl_t *vpt)
Add an unknown fr_dict_attr_t specified by a tmpl_t to the main dictionary.
fr_slen_t tmpl_attr_print(fr_sbuff_t *out, tmpl_t const *vpt)
Print an attribute or list tmpl_t to a string.
void tmpl_set_escape(tmpl_t *vpt, tmpl_escape_t const *escape)
Set escape parameters for the tmpl output.
bool tmpl_async_required(tmpl_t const *vpt)
Return whether or not async is required for this tmpl.
void tmpl_rules_child_init(TALLOC_CTX *ctx, tmpl_rules_t *out, tmpl_rules_t const *parent, tmpl_t *vpt)
Initialize a set of rules from a parent set of rules, and a parsed tmpl_t.
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.
static fr_slen_t tmpl_attr_ref_from_unspecified_substr(tmpl_attr_t *ar, tmpl_attr_error_t *err, tmpl_t *vpt, fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
static void attr_to_raw(tmpl_t *vpt, tmpl_attr_t *ref)
tmpl_t * tmpl_init_printf(tmpl_t *vpt, tmpl_type_t type, fr_token_t quote, char const *fmt,...)
Initialise a tmpl using a format string to create the name.
static void tmpl_attr_insert(tmpl_t *vpt, tmpl_attr_t *ar)
Insert an attribute reference into a tmpl.
int tmpl_cast_set(tmpl_t *vpt, fr_type_t dst_type)
Set a cast for a tmpl.
static fr_slen_t tmpl_afrom_ipv4_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
Parse bareword as an IPv4 address or prefix.
void tmpl_set_name(tmpl_t *vpt, fr_token_t quote, char const *name, ssize_t len)
Set the name on a pre-initialised tmpl.
int tmpl_attr_afrom_list(TALLOC_CTX *ctx, tmpl_t **out, tmpl_t const *list, fr_dict_attr_t const *da)
Create a new tmpl from a list tmpl and a da.
void tmpl_attr_to_raw(tmpl_t *vpt)
Convert the leaf attribute of a tmpl to a unknown/raw type.
tmpl_t * tmpl_copy(TALLOC_CTX *ctx, tmpl_t const *in)
Copy a tmpl.
void tmpl_attr_ref_debug(const tmpl_attr_t *ar, int i)
void tmpl_attr_rewrite_leaf_num(tmpl_t *vpt, int16_t to)
Rewrite the leaf's instance number.
static bool tmpl_substr_terminal_check(fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
Verify, after skipping whitespace, that a substring ends in a terminal char, or ends without further ...
static const bool array_terminal[UINT8_MAX+1]
static void tmpl_request_ref_list_copy(TALLOC_CTX *ctx, FR_DLIST_HEAD(tmpl_request_list) *out, FR_DLIST_HEAD(tmpl_request_list) const *in)
Allocate a new request reference and add it to the end of the attribute reference list.
static void tmpl_attr_rules_debug(tmpl_attr_rules_t const *at_rules)
ssize_t tmpl_preparse(char const **out, size_t *outlen, char const *in, size_t inlen, fr_token_t *type)
Preparse a string in preparation for passing it to tmpl_afrom_substr()
#define TMPL_REQUEST_REF_DEF(_name, _ref)
Define a global variable for specifying a default request reference.
#define UNRESOLVED_SET(_flags)
void tmpl_debug(tmpl_t const *vpt)
void tmpl_set_name_shallow(tmpl_t *vpt, fr_token_t quote, char const *name, ssize_t len)
Set the name on a pre-initialised tmpl.
static int tmpl_attr_afrom_attr_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t *vpt, fr_dict_attr_t const *parent, fr_dict_attr_t const *namespace, fr_sbuff_t *name, fr_sbuff_parse_rules_t const *p_rules, tmpl_attr_rules_t const *at_rules, unsigned int depth)
Parse an attribute reference, either an OID or attribute name.
static fr_slen_t tmpl_afrom_ipv6_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
Parse bareword as an IPv6 address or prefix.
static fr_slen_t tmpl_afrom_bool_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
Parse a truth value.
static tmpl_attr_t * tmpl_attr_add(tmpl_t *vpt, tmpl_attr_type_t type)
Allocate a new attribute reference and add it to the end of the attribute reference list.
static size_t attr_num_table_len
#define return_P(_x)
ssize_t tmpl_afrom_attr_str(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t **out, char const *name, tmpl_rules_t const *t_rules)
Parse a string into a TMPL_TYPE_ATTR_* type tmpl_t.
#define CHECK_T_RULES
void tmpl_attr_ref_list_debug(FR_DLIST_HEAD(tmpl_attr_list) const *ar_head)
ssize_t tmpl_cast_from_substr(tmpl_rules_t *rules, fr_sbuff_t *in)
Parse a cast specifier.
static tmpl_t * tmpl_alloc_null(TALLOC_CTX *ctx)
Create a new heap allocated tmpl_t.
static fr_slen_t tmpl_request_ref_list_from_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err, FR_DLIST_HEAD(tmpl_request_list) *out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, fr_dict_attr_t const **namespace)
Parse one or more request references, writing the list to out.
void tmpl_rules_debug(tmpl_rules_t const *rules)
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 ssize_t tmpl_afrom_ether_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
Try and parse signed or unsigned integers.
static ssize_t tmpl_afrom_float_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
static fr_slen_t tmpl_afrom_octets_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
Parse bareword as an octet string.
size_t tmpl_type_table_len
static int tmpl_attr_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules)
Resolve an unresolved attribute.
static ssize_t tmpl_afrom_enum(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
int tmpl_attr_set_leaf_da(tmpl_t *vpt, fr_dict_attr_t const *da)
Replace the leaf attribute only.
fr_slen_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
Convert an arbitrary string into a tmpl_t.
static fr_token_t tmpl_cast_quote(fr_token_t existing_quote, fr_type_t type, fr_dict_attr_t const *enumv, char const *unescaped, size_t unescaped_len)
Determine the correct quoting after a cast.
fr_dict_attr_t const * tmpl_attr_unspec
Placeholder attribute for uses of unspecified attribute references.
Definition tmpl_eval.c:55
static fr_slen_t tmpl_afrom_value_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, tmpl_rules_t const *t_rules, bool allow_enum, fr_sbuff_parse_rules_t const *p_rules)
Create TMPL_TYPE_DATA from a string.
int tmpl_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules)
Attempt to resolve functions and attributes in xlats and attribute references.
void tmpl_set_dict_def(tmpl_t *vpt, fr_dict_t const *dict)
Change the default dictionary in the tmpl's resolution rules.
tmpl_t * tmpl_init(tmpl_t *vpt, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len, tmpl_rules_t const *t_rules)
Initialise a tmpl using a literal string to create the name.
static void tmpl_request_ref_list_acopy(TALLOC_CTX *ctx, FR_DLIST_HEAD(tmpl_request_list) **out, FR_DLIST_HEAD(tmpl_request_list) const *in)
Allocate a new request reference list and copy request references into it.
fr_slen_t tmpl_print(fr_sbuff_t *out, tmpl_t const *vpt, fr_sbuff_escape_rules_t const *e_rules)
Print a tmpl_t to a string.
static fr_slen_t tmpl_afrom_integer_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules)
Try and parse signed or unsigned integers.
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...
fr_slen_t tmpl_print_quoted(fr_sbuff_t *out, tmpl_t const *vpt)
Print a tmpl_t to a string with quotes.
static void tmpl_attr_ref_fixup(TALLOC_CTX *ctx, tmpl_t *vpt, fr_dict_attr_t const *da, fr_dict_attr_t const *parent)
static fr_slen_t tmpl_attr_parse_filter(tmpl_attr_error_t *err, tmpl_attr_t *ar, fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
Parse array subscript and in future other filters.
fr_slen_t tmpl_attr_list_from_substr(fr_dict_attr_t const **da_p, fr_sbuff_t *in)
Parse one a single list reference.
#define DEFAULT_RULES
Default parser rules.
fr_table_num_ordered_t const tmpl_type_table[]
Map tmpl_type_t values to descriptive strings.
void tmpl_attr_debug(tmpl_t const *vpt)
static size_t attr_table_len
int tmpl_attr_tail_unresolved_add(fr_dict_t *dict_def, tmpl_t *vpt, fr_type_t type, fr_dict_attr_flags_t const *flags)
Add an unresolved fr_dict_attr_t specified by a tmpl_t to the main dictionary.
size_t tmpl_request_ref_table_len
fr_table_num_sorted_t const fr_token_quotes_table[]
Definition token.c:67
const char fr_token_quote[T_TOKEN_LAST]
Convert tokens back to a quoting character.
Definition token.c:157
enum fr_token fr_token_t
@ T_INVALID
Definition token.h:39
@ T_SINGLE_QUOTED_STRING
Definition token.h:122
@ T_BARE_WORD
Definition token.h:120
@ T_BACK_QUOTED_STRING
Definition token.h:123
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
@ T_SOLIDUS_QUOTED_STRING
Definition token.h:124
#define T_TOKEN_LAST
Definition token.h:129
close(uq->fd)
bool xlat_needs_resolving(xlat_exp_head_t const *head)
Check to see if the expansion needs resolving.
bool xlat_is_literal(xlat_exp_head_t const *head)
Check to see if the expansion consists entirely of value-box elements.
fr_slen_t xlat_tokenize_condition(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:3148
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 xlat_impure_func(xlat_exp_head_t const *head)
fr_slen_t xlat_tokenize(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)
Tokenize an xlat expansion.
static fr_slen_t head
Definition xlat.h:419
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 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.
fr_slen_t xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in, xlat_t const *xlat, 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.
#define xlat_copy(_ctx, _out, _in)
Definition xlat.h:457
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.
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:693
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:3120
static fr_slen_t parent
Definition pair.h:845
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:54
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#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
bool fr_type_cast(fr_type_t dst, fr_type_t src)
Return if we're allowed to cast the types.
Definition types.c:289
bool const fr_type_numeric[FR_TYPE_MAX+1]
Definition types.c:179
bool const fr_type_structural[FR_TYPE_MAX+1]
Definition types.c:189
#define fr_type_is_non_leaf(_x)
Definition types.h:390
#define fr_type_is_octets(_x)
Definition types.h:345
#define fr_type_is_structural(_x)
Definition types.h:388
#define fr_type_is_string(_x)
Definition types.h:344
#define FR_TYPE_STRUCTURAL
Definition types.h:312
#define fr_type_is_null(_x)
Definition types.h:343
#define fr_type_is_leaf(_x)
Definition types.h:389
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:450
#define FR_TYPE_LEAF
Definition types.h:313
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:5496
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:3962
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:3790
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:4700
void fr_value_box_copy_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *src)
Perform a shallow copy of a value_box.
Definition value.c:4065
ssize_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:5459
fr_sbuff_escape_rules_t * fr_value_escape_by_quote[T_TOKEN_LAST]
Definition value.c:440
int fr_value_box_steal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t *src)
Copy value data verbatim moving any buffers to the specified context.
Definition value.c:4089
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:3945
ssize_t fr_value_box_from_substr(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *rules)
Convert string value to a fr_value_box_t type.
Definition value.c:4899
int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Copy a buffer to a fr_value_box_t.
Definition value.c:4620
#define fr_value_box_mark_safe_for(_box, _safe_for)
Definition value.h:1071
static fr_slen_t fr_value_box_aprint(TALLOC_CTX *ctx, char **out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules) 1(fr_value_box_print
static fr_slen_t data
Definition value.h:1288
#define fr_box_strvalue_len(_val, _len)
Definition value.h:305
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1020
#define vb_strvalue
Definition value.h:257
int nonnull(2, 5))
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:606
static size_t char ** out
Definition value.h:1020
#define xlat_exp_head_alloc(_ctx)
Definition xlat_priv.h:273
@ XLAT_TMPL
xlat attribute
Definition xlat_priv.h:112
xlat_type_t _CONST type
type of this expansion.
Definition xlat_priv.h:155
static xlat_exp_t * xlat_exp_head(xlat_exp_head_t const *head)
Definition xlat_priv.h:209
An xlat expansion node.
Definition xlat_priv.h:148