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