The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
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: c39be37d34acc755e02b5531f87ead3b186a0c14 $
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: c39be37d34acc755e02b5531f87ead3b186a0c14 $")
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
831}
832
833/** Set escape parameters for the tmpl output
834 *
835 * @param[in] vpt to alter.
836 * @param[in] escape to set.
837 */
839{
840 vpt->rules.escape = *escape;
841
843}
844
845/** Change the default dictionary in the tmpl's resolution rules
846 *
847 * @param[in] vpt to alter.
848 * @param[in] xlat to set.
849 */
851{
852 fr_assert((vpt->type == TMPL_TYPE_XLAT) || (vpt->type == TMPL_TYPE_EXEC));
853
854 tmpl_xlat(vpt) = xlat;
855
857}
858
859
860/** Initialise a tmpl using a format string to create the name
861 *
862 * @param[in] vpt to initialise.
863 * @param[in] type of tmpl to initialise.
864 * @param[in] quote Original quoting around the name.
865 * @param[in] fmt string.
866 * @param[in] ... format arguments.
867 * @return A pointer to the newly initialised tmpl.
868 */
870{
871 va_list ap;
872
873 memset(vpt, 0, sizeof(*vpt));
875
876 va_start(ap, fmt);
877 vpt->name = fr_vasprintf(vpt, fmt, ap);
878 vpt->len = talloc_strlen(vpt->name);
879 vpt->quote = quote;
880 va_end(ap);
881
882 return vpt;
883}
884
885/** Initialise a tmpl without copying the input name string
886 *
887 * @note Name is not talloc_strdup'd or memcpy'd so must be available, and must not change
888 * for the lifetime of the #tmpl_t.
889 *
890 * @param[out] vpt to initialise.
891 * @param[in] type to set in the #tmpl_t.
892 * @param[in] quote The type of quoting around the template name.
893 * @param[in] name of the #tmpl_t.
894 * @param[in] len The length of the buffer (or a substring of the buffer) pointed to by name.
895 * If < 0 strlen will be used to determine the length.
896 * @param[in] t_rules used during parsing.
897 * @return a pointer to the initialised #tmpl_t. The same value as vpt.
898 */
900 char const *name, ssize_t len, tmpl_rules_t const *t_rules)
901{
902 memset(vpt, 0, sizeof(*vpt));
904 tmpl_set_name_shallow(vpt, quote, name, len);
905 if (t_rules) vpt->rules = *t_rules;
906
907 return vpt;
908}
909
910/** Initialise a tmpl using a literal string to create the name
911 *
912 * @param[in] vpt to initialise.
913 * @param[in] type of tmpl to initialise.
914 * @param[in] quote Original quoting around the name.
915 * @param[in] name to set for the tmpl.
916 * @param[in] len Name length. If < 0 strlen will be used
917 * to determine the name.
918 * @param[in] t_rules used during parsing.
919 * @return A pointer to the newly initialised tmpl.
920 */
922 char const *name, ssize_t len, tmpl_rules_t const *t_rules)
923{
924 memset(vpt, 0, sizeof(*vpt));
926 tmpl_set_name(vpt, quote, name, len);
927 if (t_rules) vpt->rules = *t_rules;
928
929 return vpt;
930}
931
932/** Create a new heap allocated #tmpl_t
933 *
934 * Must be later initialised with a tmpl_init_* function.
935 *
936 * This function is provided to allow tmpls to be pre-allocated for talloc purposes before
937 * their name is known.
938 */
939static inline CC_HINT(always_inline) tmpl_t *tmpl_alloc_null(TALLOC_CTX *ctx)
940{
941 tmpl_t *vpt;
942
943 /*
944 * Allocate enough memory to hold at least
945 * one attribute reference and one request
946 * reference.
947 */
948 MEM(vpt = talloc_pooled_object(ctx, tmpl_t, 2, sizeof(tmpl_request_t) + sizeof(tmpl_attr_t)));
950
951 return vpt;
952}
953
954/** Create a new heap allocated #tmpl_t
955 *
956 * @param[in,out] ctx to allocate in.
957 * @param[in] type to set in the #tmpl_t.
958 * @param[in] name of the #tmpl_t (will be copied to a new talloc buffer parented
959 * by the #tmpl_t).
960 * @param[in] len The length of the buffer (or a substring of the buffer) pointed to by name.
961 * If < 0 strlen will be used to determine the length.
962 * @param[in] quote The type of quoting around the template name.
963 * @return the newly allocated #tmpl_t.
964 */
965tmpl_t *tmpl_alloc(TALLOC_CTX *ctx, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len)
966{
967 tmpl_t *vpt;
968
969 vpt = tmpl_alloc_null(ctx);
970 memset(vpt, 0, sizeof(*vpt));
971
973 if (name) tmpl_set_name(vpt, quote, name, len);
974
975 return vpt;
976}
977/** @} */
978
979/** @name Create new #tmpl_t from a string
980 *
981 * @{
982 */
983
984/** Allocate a new attribute reference and add it to the end of the attribute reference list
985 *
986 */
988{
989 tmpl_attr_t *ar;
990 TALLOC_CTX *ctx;
991
992 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) == 0) {
993 ctx = vpt;
994 } else {
995 ctx = tmpl_attr_list_tail(tmpl_attr(vpt));
996 }
997
998 MEM(ar = talloc(ctx, tmpl_attr_t));
999 *ar = (tmpl_attr_t){
1000 .type = type,
1001 .filter = {
1003 .num = NUM_UNSPEC
1004 }
1005 };
1006 tmpl_attr_list_insert_tail(tmpl_attr(vpt), ar);
1007
1008 return ar;
1009}
1010
1011/** Create a #tmpl_t from a #fr_value_box_t
1012 *
1013 * @param[in,out] ctx to allocate #tmpl_t in.
1014 * @param[out] out Where to write pointer to new #tmpl_t.
1015 * @param[in] data to convert.
1016 * @param[in] steal If true, any buffers are moved to the new
1017 * ctx instead of being duplicated.
1018 * @return
1019 * - 0 on success.
1020 * - -1 on failure.
1021 */
1022int tmpl_afrom_value_box(TALLOC_CTX *ctx, tmpl_t **out, fr_value_box_t *data, bool steal)
1023{
1024 char *name;
1025 fr_slen_t slen;
1026 tmpl_t *vpt;
1028
1029 MEM(vpt = talloc(ctx, tmpl_t));
1031 if (slen < 0) {
1032 error:
1034 return -1;
1035 }
1036
1037 tmpl_init_shallow(vpt, TMPL_TYPE_DATA, quote, name, slen, NULL);
1038
1039 if (steal) {
1040 if (fr_value_box_steal(vpt, tmpl_value(vpt), data) < 0) goto error;
1041 } else {
1042 if (unlikely(fr_value_box_copy(vpt, tmpl_value(vpt), data) < 0)) goto error;
1043 }
1044
1046 *out = vpt;
1047
1048 return 0;
1049}
1050
1051/** Copy a list of attribute and request references from one tmpl to another
1052 *
1053 */
1054int tmpl_attr_copy(tmpl_t *dst, tmpl_t const *src)
1055{
1056 tmpl_attr_t *src_ar = NULL, *dst_ar;
1057
1058 /*
1059 * Clear any existing attribute references
1060 */
1061 if (tmpl_attr_list_num_elements(tmpl_attr(dst)) > 0) tmpl_attr_list_talloc_reverse_free(tmpl_attr(dst));
1062
1063 while ((src_ar = tmpl_attr_list_next(tmpl_attr(src), src_ar))) {
1064 dst_ar = tmpl_attr_add(dst, src_ar->type);
1065
1066 switch (src_ar->type) {
1068 dst_ar->ar_da = src_ar->ar_da;
1069 break;
1070
1071 case TMPL_ATTR_TYPE_UNSPEC: /* Nothing to copy */
1072 break;
1073
1075 dst_ar->ar_unknown = fr_dict_attr_unknown_copy(dst_ar, src_ar->ar_unknown);
1076 break;
1077
1079 dst_ar->ar_unresolved = talloc_bstrdup(dst_ar, src_ar->ar_unresolved);
1080 break;
1081
1082 default:
1083 if (!fr_cond_assert(0)) return -1;
1084 }
1085 dst_ar->ar_num = src_ar->ar_num;
1086 dst_ar->ar_filter_type = src_ar->ar_filter_type;
1087 dst_ar->parent = src_ar->parent;
1088 }
1089
1090 /*
1091 * Clear any existing request references
1092 * and copy the ones from the source.
1093 */
1094 tmpl_request_list_talloc_reverse_free(&dst->data.attribute.rr);
1095 tmpl_request_ref_list_copy(dst, &dst->data.attribute.rr, &src->data.attribute.rr);
1096
1097 /*
1098 * Ensure that we copy over any parsing rules, defaults, etc.
1099 */
1100 dst->rules = src->rules;
1101
1102 TMPL_VERIFY(dst);
1103
1104 return 0;
1105}
1106
1107/** Replace the current attribute reference
1108 *
1109 */
1111{
1112 tmpl_attr_t *ref;
1113
1115
1116 /*
1117 * Clear any existing references
1118 */
1119 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) > 0) {
1120 tmpl_attr_list_talloc_reverse_free(tmpl_attr(vpt));
1121 }
1122
1123 /*
1124 * Unknown attributes get copied
1125 */
1126 if (da->flags.is_unknown) {
1128 ref->da = ref->ar_unknown = fr_dict_attr_unknown_copy(vpt, da);
1129 } else {
1131 ref->da = da;
1132 }
1133 ref->ar_parent = fr_dict_root(fr_dict_by_da(da)); /* Parent is the root of the dictionary */
1134
1136
1137 return 0;
1138}
1139
1140/** Replace the leaf attribute only
1141 *
1142 */
1144{
1145 tmpl_attr_t *ref, *parent = NULL;
1146
1149
1150 /*
1151 * Clear any existing references
1152 */
1153 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) > 0) {
1154 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) > 1) {
1155 ref = tmpl_attr_list_tail(tmpl_attr(vpt));
1156 parent = tmpl_attr_list_prev(tmpl_attr(vpt), ref);
1157
1158 if (!fr_dict_attr_common_parent(parent->ar_da, da, true)) {
1159 fr_strerror_const("New leaf da and old leaf da do not share the same ancestor");
1160 return -1;
1161 }
1162 } else {
1163 ref = tmpl_attr_list_tail(tmpl_attr(vpt));
1164 }
1165
1166 /*
1167 * Free old unknown and unresolved attributes...
1168 */
1169 talloc_free_children(ref);
1170
1171 /*
1172 *
1173 */
1174 ref->ar_filter_type = TMPL_ATTR_FILTER_TYPE_NONE;
1175 ref->ar_num = NUM_UNSPEC;
1176
1177 } else {
1178 ref = tmpl_attr_add(vpt, da->flags.is_unknown ? TMPL_ATTR_TYPE_UNKNOWN : TMPL_ATTR_TYPE_NORMAL);
1179 }
1180
1181
1182 /*
1183 * Unknown attributes get copied
1184 */
1185 if (da->flags.is_unknown) {
1186 ref->da = ref->ar_unknown = fr_dict_attr_unknown_copy(vpt, da);
1187 } else {
1188 ref->da = da;
1189 }
1190
1191 /*
1192 * The parent of the reference is the parent of the attribute, not
1193 * the root of the dictionary. tmpl_attr_verify() asserts that the
1194 * two agree, so using the root here fails verification for any
1195 * attribute which is not a child of the root.
1196 */
1197 ref->ar_parent = da->parent;
1198
1200
1201 return 0;
1202}
1203
1204/** Rewrite the leaf's instance number
1205 *
1206 * This function is _only_ called from the compiler, for "update" and "foreach" keywords. In those cases,
1207 * the user historically did "foo-bar", but really meant "foo-bar[*]". We silently update that for
1208 * "update" sections, and complain about it in "foreach" sections.
1209 *
1210 * As the server now supports multiple types of leaf references, we do the rewrite _only_ from "none" (no
1211 * filter), OR where it's a numerical index, AND the index hasn't been specified.
1212 */
1214{
1215 tmpl_attr_t *ref = NULL;
1216
1218
1219 if (tmpl_attr_list_num_elements(tmpl_attr(vpt)) == 0) return;
1220
1221 ref = tmpl_attr_list_tail(tmpl_attr(vpt));
1222
1223 if (ref->ar_filter_type == TMPL_ATTR_FILTER_TYPE_NONE) {
1224 ref->ar_filter_type = TMPL_ATTR_FILTER_TYPE_INDEX;
1225 ref->ar_num = to;
1226
1227 } else if (ref->ar_filter_type != TMPL_ATTR_FILTER_TYPE_INDEX) {
1228 return;
1229
1230 } else if (ref->ar_num == NUM_UNSPEC) {
1231 ref->ar_num = to;
1232 }
1233
1235}
1236
1238{
1239 tmpl_attr_t *ref = tmpl_attr_list_head(tmpl_attr(vpt));
1240 if (tmpl_attr_is_list_attr(ref)) ref->da = list;
1241
1243}
1244
1245/** Create a new tmpl from a list tmpl and a da
1246 *
1247 */
1248int tmpl_attr_afrom_list(TALLOC_CTX *ctx, tmpl_t **out, tmpl_t const *list, fr_dict_attr_t const *da)
1249{
1250 tmpl_t *vpt;
1251 tmpl_attr_t *ar;
1252
1253 char attr[256];
1254 ssize_t slen;
1255
1256 MEM(vpt = tmpl_alloc(ctx, TMPL_TYPE_ATTR, T_BARE_WORD, NULL, 0));
1257
1258 /*
1259 * Copies request refs and the list ref
1260 */
1261 tmpl_attr_copy(vpt, list);
1263
1264 if (da->flags.is_unknown) {
1266 ar->da = ar->ar_unknown = fr_dict_attr_unknown_copy(vpt, da);
1267 } else {
1269 ar->ar_da = da;
1270 }
1271
1272 /*
1273 * As above: the reference's parent is the attribute's parent, not
1274 * the root of the dictionary.
1275 */
1276 ar->ar_parent = da->parent;
1277
1278 /*
1279 * We need to rebuild the attribute name, to be the
1280 * one we copied from the source list.
1281 */
1282 slen = tmpl_print(&FR_SBUFF_OUT(attr, sizeof(attr)), vpt,
1283 fr_value_escape_by_quote[list->quote]);
1284 if (slen < 0) {
1285 fr_strerror_printf("Serialized attribute too long. Must be < "
1286 STRINGIFY(sizeof(attr)) " bytes, got %zu bytes", (size_t)-slen);
1288 return -1;
1289 }
1290
1291 vpt->len = (size_t)slen;
1292 vpt->name = talloc_strdup(vpt, attr);
1293 vpt->quote = T_BARE_WORD;
1294
1296 *out = vpt;
1297
1298 return 0;
1299}
1300/** @} */
1301
1302/** Insert an attribute reference into a tmpl
1303 *
1304 * Not all attribute references can be used to create new attributes,
1305 * for example those accessing instance > 0 or those that resolve
1306 * to special indexes.
1307 *
1308 * We mark up these references and their parents as resolve only
1309 * meaning that if any code needs to use a reference chain to build
1310 * out a pair tree, it bails out early.
1311 *
1312 * @param[in] vpt containing the reference list.
1313 * @param[in] ar to insert and check.
1314 */
1315static inline CC_HINT(always_inline) void tmpl_attr_insert(tmpl_t *vpt, tmpl_attr_t *ar)
1316{
1317 /*
1318 * Insert the reference into the list.
1319 */
1320 tmpl_attr_list_insert_tail(tmpl_attr(vpt), ar);
1321
1322 switch (ar->ar_num) {
1323 case 0:
1324 case NUM_UNSPEC:
1325 break;
1326
1327 default:
1328 ar->resolve_only = true;
1329 while ((ar = tmpl_attr_list_prev(tmpl_attr(vpt), ar))) ar->resolve_only = true;
1330 break;
1331 }
1332}
1333
1334/** Parse array subscript and in future other filters
1335 *
1336 * @param[out] err Parse error code.
1337 * @param[in] ar to populate filter for.
1338 * @param[in] name containing more attribute ref data.
1339 * @param[in] at_rules see tmpl_attr_afrom_attr_substr.
1340 * @return
1341 * - >0 if a filter was parsed.
1342 * - 0 if no filter was available.
1343 * - <0 on filter parse error.
1344 */
1346 fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
1347{
1348 fr_sbuff_t our_name = FR_SBUFF(name);
1349
1350 /*
1351 * Parse array subscript (and eventually complex filters)
1352 */
1353 if (!fr_sbuff_next_if_char(&our_name, '[')) return 0;
1354
1355 if (at_rules->disallow_filters || tmpl_attr_is_list_attr(ar)) {
1356 fr_strerror_const("Filters not allowed here");
1358 fr_sbuff_set_to_start(&our_name);
1359 FR_SBUFF_ERROR_RETURN(&our_name);
1360 }
1361
1362 ar->ar_filter_type = TMPL_ATTR_FILTER_TYPE_INDEX;
1363 fr_sbuff_switch(&our_name, '\0') {
1364 case '#':
1365 ar->ar_num = NUM_COUNT;
1366 fr_sbuff_next(&our_name);
1367 break;
1368
1369 case '*':
1370 ar->ar_num = NUM_ALL;
1371 fr_sbuff_next(&our_name);
1372 break;
1373
1374 case '0':
1375 case '1':
1376 case '2':
1377 case '3':
1378 case '4':
1379 case '5':
1380 case '6':
1381 case '7':
1382 case '8':
1383 case '9':
1384 {
1385 ssize_t rcode;
1387 fr_sbuff_t tmp = FR_SBUFF(&our_name);
1388
1389 /*
1390 * All digits (not hex).
1391 */
1392 rcode = fr_sbuff_out(&sberr, &ar->ar_num, &tmp);
1393 if ((rcode < 0) || !fr_sbuff_is_char(&tmp, ']')) goto parse_tmpl;
1394
1395 if ((ar->ar_num > 1000) || (ar->ar_num < 0)) {
1396 fr_strerror_printf("Invalid array index '%hi' (should be between 0-1000)", ar->ar_num);
1397 ar->ar_num = 0;
1398 goto error;
1399 }
1400
1401 fr_sbuff_set(&our_name, &tmp); /* Advance name _AFTER_ doing checks */
1402 break;
1403 }
1404
1405 case '"':
1406 case '\'':
1407 case '`':
1408 case '/':
1409 fr_strerror_const("Invalid data type for array index");
1410 goto error;
1411
1412 /* Used as EOB here */
1413 missing_closing:
1414 case '\0':
1415 fr_strerror_const("No closing ']' for array index");
1416 error:
1418 FR_SBUFF_ERROR_RETURN(&our_name);
1419
1420 case '(': /* (...) expression */
1421 {
1422 fr_sbuff_t tmp = FR_SBUFF(&our_name);
1423 fr_slen_t slen;
1424 tmpl_rules_t t_rules;
1425 fr_sbuff_parse_rules_t p_rules;
1426 fr_sbuff_term_t const filter_terminals = FR_SBUFF_TERMS(L("]"));
1427
1428
1429 tmp = FR_SBUFF(&our_name);
1430 t_rules = (tmpl_rules_t) {};
1431 t_rules.attr = *at_rules;
1432
1433 /*
1434 * Unspecified child, we can create a filter starting from the children.
1435 *
1436 * @todo - When parsing the condition, we need to ensure that the condition contains a
1437 * reference to the current cursor, and we need to decide what that syntax is.
1438 */
1439 if (ar->type == TMPL_ATTR_TYPE_UNSPEC) {
1440 if (at_rules->dict_def) t_rules.attr.namespace = fr_dict_root(at_rules->dict_def);
1441
1442 } else {
1443 if (!ar->ar_da || !fr_type_is_structural(ar->ar_da->type)) {
1444 fr_strerror_printf("Invalid filter - cannot use filter on leaf attributes");
1445 ar->ar_num = 0;
1446 goto error;
1447 }
1448 t_rules.attr.namespace = ar->ar_da;
1449 }
1450
1451 p_rules = (fr_sbuff_parse_rules_t) {
1452 .terminals = &filter_terminals,
1453 .escapes = NULL
1454 };
1455
1456 /*
1457 * Check if it's a condition.
1458 */
1459 slen = xlat_tokenize_condition(ar, &ar->ar_cond, &tmp, &p_rules, &t_rules);
1460 if (slen < 0) goto error;
1461
1462 if (xlat_impure_func(ar->ar_cond)) {
1463 fr_strerror_const("Condition in attribute index cannot depend on functions which call external databases");
1464 goto error;
1465 }
1466
1467 ar->ar_filter_type = TMPL_ATTR_FILTER_TYPE_CONDITION;
1468 fr_sbuff_set(&our_name, &tmp); /* Advance name _AFTER_ doing checks */
1469 break;
1470 }
1471
1472 case '%': /* ${...} expansion */
1473 {
1474 fr_sbuff_t tmp = FR_SBUFF(&our_name);
1475 fr_slen_t slen;
1476 tmpl_rules_t t_rules;
1477 fr_sbuff_parse_rules_t p_rules;
1478 fr_sbuff_term_t const filter_terminals = FR_SBUFF_TERMS(L("]"));
1479
1480 if (!fr_sbuff_is_str(&our_name, "%{", 2)) {
1481 fr_strerror_const("Invalid expression in attribute index");
1482 goto error;
1483 }
1484
1485 tmp = FR_SBUFF(&our_name);
1486 t_rules = (tmpl_rules_t) {};
1487 t_rules.attr = *at_rules;
1488
1489 p_rules = (fr_sbuff_parse_rules_t) {
1490 .terminals = &filter_terminals,
1491 .escapes = NULL
1492 };
1493
1494 /*
1495 * Check if it's an expression.
1496 */
1497 slen = xlat_tokenize_expression(ar, &ar->ar_expr, &tmp, &p_rules, &t_rules);
1498 if (slen < 0) goto error;
1499
1500 if (xlat_impure_func(ar->ar_expr)) {
1501 fr_strerror_const("Expression in attribute index cannot depend on functions which call external databases");
1502 goto error;
1503 }
1504
1505 ar->ar_filter_type = TMPL_ATTR_FILTER_TYPE_EXPR;
1506
1507 fr_sbuff_set(&our_name, &tmp); /* Advance name _AFTER_ doing checks */
1508 break;
1509 }
1510
1511 case 'n':
1512 /*
1513 * [n] is the last one
1514 *
1515 * [nope] is a reference to "nope".
1516 */
1517 if (fr_sbuff_is_str(&our_name, "n]", 2)) {
1518 ar->ar_num = NUM_LAST;
1519 fr_sbuff_next(&our_name);
1520 break;
1521 }
1523
1524 default:
1525 parse_tmpl:
1526 {
1527 fr_sbuff_t tmp = FR_SBUFF(&our_name);
1528 ssize_t slen;
1529 tmpl_rules_t t_rules;
1530 fr_sbuff_parse_rules_t p_rules;
1531 fr_sbuff_term_t const filter_terminals = FR_SBUFF_TERMS(L("]"));
1532
1533 tmp = FR_SBUFF(&our_name);
1534 t_rules = (tmpl_rules_t) {};
1535 t_rules.attr = *at_rules;
1536
1537 /*
1538 * Don't reset namespace, we always want to start searching from the top level of the
1539 * dictionaries.
1540 */
1541
1542 p_rules = (fr_sbuff_parse_rules_t) {
1543 .terminals = &filter_terminals,
1544 .escapes = NULL
1545 };
1546
1547 /*
1548 * @todo - for some reason, the tokenize_condition code allows for internal
1549 * vs protocol vs local attributes, whereas the tmpl function only accepts
1550 * internal ones.
1551 */
1552 slen = tmpl_afrom_substr(ar, &ar->ar_tmpl, &tmp, T_BARE_WORD, &p_rules, &t_rules);
1553 if (slen <= 0) goto error;
1554
1555 if (!tmpl_is_attr(ar->ar_tmpl)) {
1556 fr_strerror_printf("Invalid array index '%s'", ar->ar_tmpl->name);
1557 goto error;
1558 }
1559
1560 /*
1561 * Arguably we _could_ say &User-Name["foo"] matches all user-name with value "foo",
1562 * but that would confuse the issue for &Integer-Thing[4].
1563 *
1564 * For matching therefore, we really need to have a way to define "self".
1565 */
1566 if (!fr_type_numeric[tmpl_attr_tail_da(ar->ar_tmpl)->type]) {
1567 fr_strerror_const("Invalid data type for array index (must be numeric)");
1568 goto error;
1569 }
1570
1571 ar->ar_filter_type = TMPL_ATTR_FILTER_TYPE_TMPL;
1572 fr_sbuff_set(&our_name, &tmp); /* Advance name _AFTER_ doing checks */
1573 break;
1574 }
1575 }
1576
1577 /*
1578 * Always advance here, so the error
1579 * marker points to the bad char.
1580 */
1581 if (!fr_sbuff_next_if_char(&our_name, ']')) goto missing_closing;
1582
1583 FR_SBUFF_SET_RETURN(name, &our_name);
1584}
1585
1586extern fr_dict_attr_t const *tmpl_attr_unspec;
1587
1588static inline CC_HINT(nonnull(3,4))
1590 tmpl_t *vpt,
1591 fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
1592{
1593 fr_slen_t slen;
1594
1595 *ar = (tmpl_attr_t){
1596 .ar_num = NUM_UNSPEC, /* May be changed by tmpl_attr_parse_filter */
1597 .ar_type = TMPL_ATTR_TYPE_UNSPEC,
1598 .ar_da = tmpl_attr_unspec,
1599 };
1600
1601 slen = tmpl_attr_parse_filter(err, ar, name, at_rules);
1602 if (slen < 0) {
1603 return slen;
1604
1605 /*
1606 * No filters and no previous elements is the equivalent of '&'
1607 * which is not allowed.
1608 *
1609 * &[<filter>] is allowed as this lets us perform filtering operations
1610 * at the root.
1611 */
1612 } else if (tmpl_attr_num_elements(vpt) == 0) {
1613 fr_strerror_const("Invalid attribute name");
1615 return -1;
1616 }
1617
1618 tmpl_attr_insert(vpt, ar);
1619
1620 return slen;
1621}
1622
1623/** Parse an unresolved attribute, i.e. one which can't be found in the current dictionary
1624 *
1625 * This function calls itself recursively to process additional OID
1626 * components once we've failed to resolve one component.
1627 *
1628 * @note Do not call directly.
1629 *
1630 * @param[in] ctx to allocate new attribute reference in.
1631 * @param[out] err Parse error.
1632 * @param[in,out] vpt to append this reference to.
1633 * @param[in] parent Last known parent.
1634 * @param[in] namespace in which the attribute will be resolved.
1635 * @param[in] name to parse.
1636 * @param[in] at_rules see tmpl_attr_afrom_attr_substr.
1637 * @return
1638 * - <0 on error.
1639 * - 0 on success.
1640 */
1641static inline CC_HINT(nonnull(3,6))
1643 tmpl_t *vpt,
1644 fr_dict_attr_t const *parent, fr_dict_attr_t const *namespace,
1645 fr_sbuff_t *name, tmpl_attr_rules_t const *at_rules)
1646{
1647 tmpl_attr_t *ar = NULL, *ar_curr;
1648 fr_sbuff_t our_name = FR_SBUFF(name);
1649 fr_slen_t slen;
1650 char *unresolved;
1651
1652 /*
1653 * Point we free from if something goes wrong.
1654 */
1655 ar_curr = tmpl_attr_list_tail(tmpl_attr(vpt));
1656 for (;;) {
1657 MEM(ar = talloc(ctx, tmpl_attr_t));
1658 /*
1659 * Copy out a string of allowed dictionary chars to form
1660 * the unresolved attribute name.
1661 *
1662 * This will be resolved later (outside of this function).
1663 */
1664 slen = fr_sbuff_out_abstrncpy_allowed(ar, &unresolved,
1665 &our_name, FR_DICT_ATTR_MAX_NAME_LEN + 1,
1667 if (slen < 0) return -1;
1668
1669 if (slen == 0) {
1670 slen = tmpl_attr_ref_from_unspecified_substr(ar, err, vpt, &our_name, at_rules);
1671 if (slen < 0) {
1672 fr_sbuff_advance(&our_name, +slen);
1673 error:
1674 talloc_free(ar);
1675 tmpl_attr_list_talloc_free_to_tail(tmpl_attr(vpt), ar_curr);
1676 return -1;
1677 }
1678 return fr_sbuff_set(name, &our_name);
1679 } else if (slen > FR_DICT_ATTR_MAX_NAME_LEN) {
1680 fr_strerror_const("Attribute name is too long");
1682 goto error;
1683 }
1684
1685 *ar = (tmpl_attr_t){
1686 .ar_num = NUM_UNSPEC,
1687 .ar_type = TMPL_ATTR_TYPE_UNRESOLVED,
1688 .ar_unresolved = unresolved,
1689 .ar_unresolved_namespace = namespace,
1690 .ar_parent = parent
1691 };
1692
1693 if (tmpl_attr_parse_filter(err, ar, &our_name, at_rules) < 0) goto error;
1694
1695 /*
1696 * Insert the ar into the list of attribute references
1697 *
1698 * The tmpl is no longer a pure TMPL_TYPE_ATTR. We have to convert it to an unresolved
1699 * one. If we don't do this, then tmpl_afrom_attr_substr() would walk the ar list
1700 * expecting every ar->ar_da to be non-NULL, and would crash on the UNRESOLVED entry.
1701 */
1702 tmpl_attr_insert(vpt, ar);
1704
1705 /*
1706 * Once one OID component is created as unresolved all
1707 * future OID components are also unresolved.
1708 */
1709 if (!fr_sbuff_next_if_char(&our_name, '.')) break;
1710 }
1711
1712 /*
1713 * Mark the tmpl up as an unresolved attribute reference
1714 * the attribute reference will be resolved later.
1715 */
1717
1718 return fr_sbuff_set(name, &our_name);
1719}
1720
1721/*
1722 * Add attr_ref when we've parsed an intermediate dictionary name
1723 * which is itself a ref.
1724 */
1725static void tmpl_attr_ref_fixup(TALLOC_CTX *ctx, tmpl_t *vpt, fr_dict_attr_t const *da, fr_dict_attr_t const *parent)
1726{
1727 tmpl_attr_t *ar;
1728
1729 if (tmpl_attr_tail_da(vpt) == da) return;
1730
1731 if (da->parent != parent) tmpl_attr_ref_fixup(ctx, vpt, da->parent, parent);
1732
1733 MEM(ar = talloc(ctx, tmpl_attr_t));
1734 *ar = (tmpl_attr_t) {
1735 .ar_num = NUM_UNSPEC,
1736 .ar_type = TMPL_ATTR_TYPE_NORMAL,
1737 .ar_da = da,
1738 .ar_parent = da->parent,
1739 };
1740
1741 tmpl_attr_insert(vpt, ar);
1742}
1743
1744/** Parse an attribute reference, either an OID or attribute name
1745 *
1746 * @note Do not call directly.
1747 *
1748 * @param[in] ctx to allocate new attribute reference in.
1749 * @param[out] err Parse error.
1750 * @param[in,out] vpt to append this reference to.
1751 * @param[in] parent Parent where the attribute will be placed (group, struct, tlv, etc).
1752 * @param[in] namespace Where the child attribute will be parsed from (dict root, struct member, TLV child, etc)
1753 * @param[in] name to parse.
1754 * @param[in] p_rules Formatting rules used to check for trailing garbage.
1755 * @param[in] at_rules which places constraints on attribute reference parsing.
1756 * Rules interpreted by this function is:
1757 * - allow_unknown - If false unknown OID components
1758 * result in a parse error.
1759 * - allow_unresolved - If false unknown attribute names
1760 * result in a parse error.
1761 * - allow_foreign - If an attribute resolves in a dictionary
1762 * that does not match the parent
1763 * (exception being FR_TYPE_GROUP) then that results
1764 * in a parse error.
1765 * @param[in] depth How deep we are. Used to check for maximum nesting level.
1766 * @return
1767 * - <0 on error.
1768 * - 0 on success.
1769 */
1771 tmpl_t *vpt,
1772 fr_dict_attr_t const *parent, fr_dict_attr_t const *namespace,
1774 fr_sbuff_parse_rules_t const *p_rules, tmpl_attr_rules_t const *at_rules,
1775 unsigned int depth)
1776{
1777 uint32_t oid = 0;
1778 tmpl_attr_t *ar = NULL;
1779 fr_dict_attr_t const *da;
1781 fr_dict_attr_err_t dict_err;
1782 fr_dict_attr_t const *our_parent = parent;
1783
1784 fr_sbuff_marker(&m_s, name);
1785
1787 fr_strerror_const("Attribute nesting too deep");
1789 error:
1790 talloc_free(ar);
1791 fr_sbuff_marker_release(&m_s);
1793 }
1794
1795 /*
1796 * Input too short
1797 */
1798 if (!fr_sbuff_extend(name)) {
1799 fr_strerror_const("Unexpected end of input when trying to read an attribute name");
1801 goto error;
1802 }
1803
1804 /*
1805 * This cannot possibly be an attribute name, so we just bypass all kinds of work.
1806 */
1808 fr_strerror_printf("Unexpected input '%c' when trying to read an attribute name",
1809 fr_sbuff_char(name, '\0'));
1812 }
1813
1814 /*
1815 * Maybe there's no child namespace (struct member, tlv child, etc). In which case we must
1816 * search from the default dictionary root.
1817 *
1818 * This search is probably wrong in some cases. See the comments below around FR_TYPE_GROUP.
1819 *
1820 * If we change out the dictionaries, we should arguably also change dict_def in the
1821 * tmpl_attr_rules_t. On top of that, the "dict_attr_search" functions take a #fr_dict_t
1822 * pointer, and not a pointer to the dict root. So we can't pass them a namespace.
1823 */
1824 if (!namespace) {
1825 fr_assert(parent == NULL);
1826
1828 at_rules->dict_def,
1829 name, p_rules ? p_rules->terminals : NULL,
1830 true,
1831 at_rules->allow_foreign);
1832 /*
1833 * The attribute was found either in the dict_def root, OR in the internal root, OR if
1834 * !dict_def && allow_foreign, in some other dictionary root.
1835 *
1836 * Otherwise we're still not sure what the attribute is. It may end up being an
1837 * unresolved one.
1838 */
1839 if (da) {
1840 our_parent = da->parent;
1841
1842 if (!our_parent->flags.is_root) {
1843 tmpl_attr_ref_fixup(ctx, vpt, our_parent, fr_dict_root(da->dict));
1844 }
1845 }
1846 } else {
1847 fr_assert(parent != NULL);
1848
1849 /*
1850 * Otherwise we're resolving the next piece in the context of where-ever we ended up from
1851 * parsing the last bit.
1852 *
1853 * The "parent" could be the same as "namespace", if both are at a dictionary root, OR
1854 * both are from a struct / tlv attribute.
1855
1856 * Or, "parent" could be a grouping attribute (e.g. request), and "namespace" could be
1857 * the dictionary root.
1858 */
1859 (void)fr_dict_attr_by_name_substr(&dict_err,
1860 &da,
1861 namespace,
1862 name,
1863 p_rules ? p_rules->terminals : NULL);
1864
1865 /*
1866 * Allow fallback to internal attributes
1867 * if the parent was a group, and we're
1868 * allowing internal resolution.
1869 *
1870 * Discard any errors here... It's more
1871 * useful to have the original.
1872 */
1873 if (!da) {
1874 ar = tmpl_attr_list_tail(&vpt->data.attribute.ar);
1875 if (!ar || ((ar->type == TMPL_ATTR_TYPE_NORMAL) && (ar->ar_da->type == FR_TYPE_GROUP))) {
1876 fr_dict_attr_t const *internal_root = fr_dict_root(fr_dict_internal());
1877
1878 (void)fr_dict_attr_by_name_substr(NULL,
1879 &da, internal_root,
1880 name,
1881 p_rules ? p_rules->terminals : NULL);
1882 if (da) {
1883 dict_err = FR_DICT_ATTR_OK;
1884 our_parent = internal_root;
1885 }
1886 }
1887 ar = NULL;
1888
1889 } else {
1890 /*
1891 * If we searched in a local dictionary, but found a real attribute
1892 * switch the namespace.
1893 */
1894 if (!da->flags.local && namespace->flags.local) namespace = our_parent = fr_dict_root(da->dict);
1895 }
1896 }
1897
1898 /*
1899 * Fatal errors related to nesting...
1900 */
1901 switch (dict_err) {
1903 fr_assert(our_parent != NULL);
1904 if (our_parent->flags.is_unknown) break;
1905 goto error;
1906
1908 goto error;
1909
1910 default:
1911 if (!da) break;
1912
1913 /*
1914 * The named component was a known attribute
1915 * so record it as a normal attribute
1916 * reference.
1917 */
1918 fr_assert(our_parent != NULL);
1919
1920 /*
1921 * We had an alias in the same namespace,
1922 * go add more things in.
1923 */
1924 if (da->parent != our_parent) {
1925 fr_assert(namespace == our_parent);
1926 tmpl_attr_ref_fixup(ctx, vpt, da->parent, our_parent);
1927 }
1928
1929 goto alloc_ar;
1930 }
1931
1932 /*
1933 * At this point we haven't found a known attribute. What remains MUST be an OID component, OR an
1934 * unresolved attribute.
1935 *
1936 * The default is to parse the OIDs in the current namespace. If there is none, then we parse
1937 * the OIDs and unresolved attributes in the dict_def. And if that doesn't exist, in the
1938 * internal dictionaries.
1939 *
1940 * Note that we do NOT allow unknown attributes in the internal dictionary. Those attributes are
1941 * generally just DEFINEs, and their numbers have no meaning.
1942 */
1943 if (!namespace) {
1944 if (at_rules->dict_def) {
1945 our_parent = namespace = fr_dict_root(at_rules->dict_def);
1946 } else {
1947 our_parent = namespace = fr_dict_root(fr_dict_internal());
1948 }
1949 }
1950
1951 fr_assert(our_parent != NULL);
1952 fr_assert(namespace != NULL);
1953
1954 /*
1955 * See if the ref begins with an unsigned integer
1956 * if it does it's probably an OID component
1957 *
1958 * .<oid>
1959 */
1960 if (fr_sbuff_out(NULL, &oid, name) > 0) {
1961 if (!at_rules->allow_oid) {
1962 uint8_t c = fr_sbuff_uint8(name, '\0');
1963
1964 /*
1965 * This extra test is to give the user better errors. The string "3G" is parsed
1966 * as "3", and then an error of "what the heck do you mean by G?"
1967 *
1968 * In contrast, the string "3." is parsed as "3", and then "nope, that's not an attribute reference".
1969 */
1970 if (c != '.') {
1971 fr_strerror_const("Unexpected text after attribute reference");
1973 } else {
1974 fr_strerror_const("Numerical attribute references are not allowed here");
1976
1977 fr_sbuff_set(name, &m_s);
1978 }
1979 goto error;
1980 }
1981
1982 our_parent = namespace = fr_dict_unlocal(namespace);
1983
1984 fr_assert(ar == NULL);
1985
1986 fr_strerror_clear(); /* Clear out any existing errors */
1987
1988 if (fr_dict_by_da(namespace) == fr_dict_internal()) goto disallow_unknown;
1989
1990 /*
1991 * The OID component was a known attribute
1992 * so record it as a normal attribute
1993 * reference.
1994 */
1995 da = fr_dict_attr_child_by_num(namespace, oid);
1996 if (da) {
1997 fr_assert(da->parent == our_parent);
1998 goto alloc_ar;
1999 }
2000
2001 if (!at_rules->allow_unknown) {
2002 disallow_unknown:
2003 fr_strerror_const("Unknown attributes not allowed here");
2005 fr_sbuff_set(name, &m_s);
2006 goto error;
2007 }
2008
2009 /*
2010 * If it's numeric and not a known attribute
2011 * then we create an unknown attribute with
2012 * the specified attribute number.
2013 */
2014 MEM(ar = talloc(ctx, tmpl_attr_t));
2015
2016 /*
2017 * VSAs have VENDORs as children. All others are just normal things.
2018 */
2019 switch (namespace->type) {
2020 case FR_TYPE_VSA:
2021 da = fr_dict_attr_unknown_vendor_afrom_num(ar, namespace, oid);
2022 break;
2023
2024 default:
2025 da = fr_dict_attr_unknown_raw_afrom_num(ar, namespace, oid);
2026 break;
2027 }
2028
2029 if (!da) {
2030 if (err) *err = TMPL_ATTR_ERROR_UNKNOWN_NOT_ALLOWED; /* strerror set by dict function */
2031 goto error;
2032 }
2033
2034 *ar = (tmpl_attr_t){
2035 .ar_num = NUM_UNSPEC,
2036 .ar_type = TMPL_ATTR_TYPE_UNKNOWN,
2037 .ar_unknown = UNCONST(fr_dict_attr_t *, da),
2038 .ar_da = da,
2039 .ar_parent = our_parent,
2040 };
2041 goto do_suffix;
2042 }
2043
2044 /*
2045 * Can't parse it as an attribute, might be a literal string
2046 * let the caller decide.
2047 *
2048 * Don't alter the fr_strerror buffer, may contain useful
2049 * errors from the dictionary code.
2050 */
2051 if (!at_rules->allow_unresolved && !(at_rules->allow_wildcard && fr_sbuff_is_char(name, '['))) {
2052 fr_strerror_const_push("Unresolved attributes are not allowed here");
2054 fr_sbuff_set(name, &m_s);
2055 goto error;
2056 }
2057
2058 fr_sbuff_marker_release(&m_s);
2059
2060 /*
2061 * Once we hit one unresolved attribute we have to treat
2062 * the rest of the components are unresolved as well.
2063 */
2064 return tmpl_attr_ref_afrom_unresolved_substr(ctx, err, vpt, our_parent, namespace, name, at_rules);
2065
2066alloc_ar:
2067 /*
2068 * We have a da, remove any of the errors recorded from failed
2069 * searches to find the attribute to avoid misleading messages
2070 * if something else fails.
2071 */
2073
2074 MEM(ar = talloc(ctx, tmpl_attr_t));
2075 *ar = (tmpl_attr_t) {
2076 .ar_num = NUM_UNSPEC,
2077 .ar_type = TMPL_ATTR_TYPE_NORMAL,
2078 .ar_da = da,
2079 .ar_parent = da->parent,
2080 };
2081
2082do_suffix:
2083 /*
2084 * Parse the attribute reference filter
2085 *
2086 * Error out immediately if the filter is bad
2087 * otherwise determine whether to keep the
2088 * attribute reference or omit it based on:
2089 *
2090 * - Whether there was a filter present.
2091 * - The type of attribute.
2092 * - If this is the leaf attribute reference.
2093 */
2094 if (tmpl_attr_parse_filter(err, ar, name, at_rules) < 0) goto error;
2095
2096 /*
2097 * Local variables are always unitary.
2098 *
2099 * [0] is allowed, as is [n], [*], and [#]. But [1], etc. aren't allowed.
2100 */
2101 if (da->flags.local && (ar->ar_num > 0)) {
2102 fr_strerror_printf("Invalid array reference for local variable");
2104 fr_sbuff_set(name, &m_s);
2105 goto error;
2106 }
2107
2108 /*
2109 * At the end of the attribute reference. If there's a
2110 * trailing '.' then there's another attribute reference
2111 * we need to parse, otherwise we're done.
2112 */
2113 fr_sbuff_marker_release(&m_s);
2114 fr_sbuff_marker(&m_s, name);
2115
2116 if (fr_sbuff_next_if_char(name, '.')) {
2117 fr_dict_attr_t const *ref;
2118
2119 switch (da->type) {
2120 case FR_TYPE_GROUP:
2121 ref = fr_dict_attr_ref(da);
2122
2123 /*
2124 * If the ref is outside of the internal namespace, then we use it.
2125 *
2126 * If the ref is inside of the internal namespace (e.g. "request"), then we do
2127 * something else.
2128 *
2129 * If we were given a root dictionary on input, use that. We have to follow this
2130 * dictionary because this function calls itself recursively, WITHOUT updating
2131 * "dict_def" in the attr_rules. So the dict-def there is whatever got passed
2132 * into tmpl_afrom_attr_substr(), BEFORE the "parent.parent.parent..." parsing.
2133 * Which means that in many cases, the "dict_def" is completely irrelevant.
2134 *
2135 * If there is no parent on input, then we just use dict_def.
2136 *
2137 * Otherwise we search through all of the dictionaries.
2138 *
2139 * Note that we cannot put random protocol attributes into an internal attribute
2140 * of type "group".
2141 */
2142 if (ref != fr_dict_root(fr_dict_internal())) {
2143 our_parent = namespace = ref;
2144
2145 } else if (parent && parent->flags.is_root) {
2146 our_parent = namespace = parent;
2147
2148 } else if (request_attr_is_list(da)) {
2149 our_parent = namespace = NULL;
2150
2151 } else if (at_rules->dict_def) {
2152 our_parent = namespace = fr_dict_root(at_rules->dict_def);
2153
2154 } else {
2155 our_parent = namespace = NULL;
2156 }
2157 break;
2158
2160 /*
2161 * Structural types are parented and namespaced from their parent da.
2162 */
2163 namespace = our_parent = da;
2164 break;
2165
2166 default:
2167 fr_strerror_printf("Attribute %s of data type '%s' cannot have child attributes", da->name, fr_type_to_str(da->type));
2168 fr_sbuff_set(name, &m_s);
2169 goto error;
2170 }
2171
2172 if (ar) tmpl_attr_insert(vpt, ar);
2173
2174 if (tmpl_attr_afrom_attr_substr(ctx, err, vpt, our_parent, namespace, name, p_rules, at_rules, depth + 1) < 0) {
2175 if (ar) {
2176 tmpl_attr_list_talloc_free_tail(&vpt->data.attribute.ar); /* Remove and free ar */
2177 ar = NULL;
2178 }
2179 goto error;
2180 }
2181
2182 /*
2183 * If it's a leaf we always insert the attribute
2184 * reference into the list, even if it's a
2185 * nesting attribute.
2186 *
2187 * This is useful for nested edit sections
2188 * where the tmpl might be the name of a new
2189 * subsection.
2190 */
2191 } else {
2192 tmpl_attr_insert(vpt, ar);
2193 }
2194
2195 /*
2196 * Remove unnecessary casts.
2197 */
2200
2202
2203 fr_sbuff_marker_release(&m_s);
2204 return 0;
2205}
2206
2208{
2209 switch (ref->type) {
2211 {
2212 ref->da = ref->ar_unknown = fr_dict_attr_unknown_afrom_da(vpt, ref->da);
2213 if (!ref->da) return -1;
2214
2215 ref->ar_unknown->type = FR_TYPE_OCTETS;
2216 ref->is_raw = 1;
2218 }
2219 break;
2220 case TMPL_ATTR_TYPE_UNSPEC: /* noop */
2221 break;
2222
2224 ref->ar_unknown->type = FR_TYPE_OCTETS;
2225 ref->is_raw = 1;
2226 break;
2227
2229 ref->is_raw = true;
2230 break;
2231 }
2232
2234
2235 return 0;
2236}
2237
2238/** Parse a string into a TMPL_TYPE_ATTR_* type #tmpl_t
2239 *
2240 * @param[in,out] ctx to allocate #tmpl_t in.
2241 * @param[out] err May be NULL. Provides the exact error that the parser hit
2242 * when processing the attribute ref.
2243 * @param[out] out Where to write pointer to new #tmpl_t.
2244 * @param[in] name of attribute including #tmpl_request_ref_t and #fr_pair_list_t qualifiers.
2245 * @param[in] p_rules Formatting rules used to check for trailing garbage.
2246 * @param[in] t_rules Rules which control parsing:
2247 * - dict_def The default dictionary to use if attributes
2248 * are unqualified.
2249 * - request_def The default #request_t to set if no
2250 * #tmpl_request_ref_t qualifiers are found in name.
2251 * - list_def The default list to set if no #fr_pair_list_t
2252 * qualifiers are found in the name.
2253 * - allow_unknown If true, numerical attributes will be allowed,
2254 * even if they're not in the main dictionaries.
2255 * If an unknown attribute is found a #TMPL_TYPE_ATTR
2256 * #tmpl_t will be produced.
2257 * If #tmpl_afrom_attr_substr is being called on
2258 * startup, the #tmpl_t may be passed to
2259 * #tmpl_attr_unknown_add to
2260 * add the unknown attribute to the main dictionary.
2261 * If the unknown attribute is not added to
2262 * the main dictionary the #tmpl_t cannot be used
2263 * to search for a #fr_pair_t in a #request_t.
2264 * - allow_unresolved If true, we don't generate a parse error on
2265 * unknown attributes. If an unknown attribute is
2266 * found a #TMPL_TYPE_ATTR_UNRESOLVED
2267 * #tmpl_t will be produced.
2268 * - allow_foreign If true, allow attribute names to be qualified
2269 * with a protocol outside of the passed dict_def.
2270 * - disallow_filters
2271 *
2272 * @see REMARKER to produce pretty error markers from the return value.
2273 *
2274 * @return
2275 * - <= 0 on error (offset as negative integer)
2276 * - > 0 on success (number of bytes parsed).
2277 */
2280 fr_sbuff_parse_rules_t const *p_rules,
2281 tmpl_rules_t const *t_rules)
2282{
2283 int ret;
2284 tmpl_t *vpt;
2285 fr_sbuff_t our_name = FR_SBUFF(name); /* Take a local copy in case we need to back track */
2286 bool is_raw = false;
2287 tmpl_attr_rules_t const *at_rules;
2288 tmpl_attr_rules_t my_attr_rules;
2290 fr_dict_attr_t const *namespace;
2292
2294
2295 at_rules = &t_rules->attr;
2296
2298
2299 if (!fr_sbuff_extend(&our_name)) {
2300 fr_strerror_const("Empty attribute reference");
2302 FR_SBUFF_ERROR_RETURN(&our_name);
2303 }
2304
2305 /*
2306 * '&' prefix is ignored.
2307 */
2308 if (fr_sbuff_next_if_char(&our_name, '&') && check_config && at_rules->ci) {
2309 cf_log_warn(at_rules->ci, "Using '&' is no longer necessary when referencing attributes. Please delete it.");
2310 }
2311
2312 if (fr_sbuff_is_char(name, '[')) {
2313 fr_strerror_const("Missing attribute name");
2315 FR_SBUFF_ERROR_RETURN(&our_name);
2316 }
2317
2318 /*
2319 * We parsed the tmpl as User-Name, but NOT %{User-Name}.
2320 */
2321 MEM(vpt = tmpl_alloc(ctx, TMPL_TYPE_ATTR, T_BARE_WORD, NULL, 0));
2322
2323 /*
2324 * The "raw." prefix marks up the leaf attribute
2325 * as unknown if it wasn't already which allows
2326 * users to stick whatever they want in there as
2327 * a value.
2328 */
2329 if (fr_sbuff_adv_past_strcase_literal(&our_name, "raw.")) {
2330 my_attr_rules = *at_rules;
2331 my_attr_rules.allow_oid = true;
2332 at_rules = &my_attr_rules;
2333
2334 is_raw = true;
2335 }
2336
2337 /*
2338 * Parse one or more request references
2339 */
2341 &vpt->data.attribute.rr,
2342 &our_name,
2343 t_rules,
2344 &namespace);
2345 if (ret < 0) {
2346 error:
2347 *out = NULL;
2349 FR_SBUFF_ERROR_RETURN(&our_name);
2350 }
2351
2352 fr_sbuff_marker(&m_l, &our_name);
2353
2354 /*
2355 * Parse the list and / or attribute reference
2356 */
2358 vpt,
2359 namespace, namespace,
2360 &our_name, p_rules, at_rules, 0);
2361 if (ret < 0) goto error;
2362
2363 if (!tmpl_substr_terminal_check(&our_name, p_rules)) {
2364 fr_strerror_const("Unexpected text after attribute reference");
2366 goto error;
2367 }
2368
2369 /*
2370 * Check whether the tmpl has a list qualifier.
2371 */
2372 switch (at_rules->list_presence) {
2374 break;
2375
2377 if (tmpl_attr_is_list_attr(tmpl_attr_list_head(tmpl_attr(vpt)))) {
2378 fr_strerror_const("List qualifiers are not allowed here.");
2380 goto error;
2381 }
2382 break;
2383
2385 if (!tmpl_attr_is_list_attr(tmpl_attr_list_head(tmpl_attr(vpt)))) {
2386 fr_strerror_const("List qualifier is required, but no list was found.");
2388 goto error;
2389 }
2390 break;
2391 }
2392
2393 tmpl_set_name(vpt, T_BARE_WORD, fr_sbuff_start(&our_name), fr_sbuff_used(&our_name));
2394 vpt->rules = *t_rules; /* Record the rules */
2395
2396 /*
2397 * Check to see if the user wants the leaf
2398 * attribute to be raw.
2399 *
2400 * We can only do the conversion now _if_
2401 * the complete hierarchy has been resolved
2402 * otherwise we'll need to do the conversion
2403 * later.
2404 */
2405 if (tmpl_is_attr(vpt)) {
2406 tmpl_attr_t *ar = tmpl_attr_list_head(tmpl_attr(vpt));
2407 bool is_local = ar->ar_da->flags.local;
2408 bool allow_local = is_local;
2409
2410 /*
2411 * Convert known attributes to raw ones if requested.
2412 */
2413 if (is_raw) {
2414 /*
2415 * Local variables cannot be raw.
2416 */
2417 if (is_local) {
2418 fr_strerror_printf("Local attributes cannot be 'raw'");
2420 fr_sbuff_set(&our_name, &m_l);
2421 goto error;
2422 }
2423 ret = attr_to_raw(vpt, tmpl_attr_list_tail(tmpl_attr(vpt)));
2424 if (ret < 0) goto error;
2425 }
2426
2427 /*
2428 * We can transition from local to non-local, but not the other way around.
2429 */
2430 for (;
2431 ar != NULL;
2432 ar = tmpl_attr_list_next(tmpl_attr(vpt), ar)) {
2433 if (ar->ar_da->flags.local == allow_local) continue;
2434
2435 if (!ar->ar_da->flags.local && allow_local) {
2436 allow_local = false;
2437 continue;
2438 }
2439
2440 if (ar->ar_da->flags.local) {
2441 fr_strerror_printf("Local attributes cannot be used in any list");
2443 fr_sbuff_set(&our_name, &m_l);
2444 goto error;
2445 }
2446 }
2447
2448 /*
2449 * Local variables are named "foo", but are always put into the local list.
2450 *
2451 * We add the list after checking for non-local -> local transition, as
2452 * request_attr_local isn't a local attribute.
2453 *
2454 * When the list is forbidden, we're creating a local attribute inside of a local
2455 * TLV.
2456 */
2457 if (is_local && (at_rules->list_presence != TMPL_ATTR_LIST_FORBID)) {
2458 MEM(ar = talloc(vpt, tmpl_attr_t));
2459 *ar = (tmpl_attr_t){
2460 .ar_type = TMPL_ATTR_TYPE_NORMAL,
2461 .ar_da = request_attr_local,
2462 .ar_parent = fr_dict_root(fr_dict_internal())
2463 };
2464
2465 /*
2466 * Prepend the local list ref so it gets evaluated
2467 * first.
2468 */
2469 tmpl_attr_list_insert_head(tmpl_attr(vpt), ar);
2470 }
2471 }
2472
2473 /*
2474 * If a list wasn't already specified, then add one now.
2475 */
2476 if (!tmpl_attr_is_list_attr(tmpl_attr_list_head(tmpl_attr(vpt)))) {
2477 tmpl_attr_t *ar;
2478
2479 MEM(ar = talloc(vpt, tmpl_attr_t));
2480 *ar = (tmpl_attr_t){
2481 .ar_type = TMPL_ATTR_TYPE_NORMAL,
2482 .ar_parent = fr_dict_root(fr_dict_internal())
2483 };
2484
2485 fr_assert(at_rules->list_def);
2486 ar->ar_da = at_rules->list_def;
2487
2488 /*
2489 * Prepend the list ref so it gets evaluated
2490 * first.
2491 */
2492 tmpl_attr_list_insert_head(tmpl_attr(vpt), ar);
2493 }
2494
2495 /*
2496 * If there is a default request (parent, outer, etc.), add it to the ar list.
2497 *
2498 * A NULL request_def pointer is equivalent to the current request.
2499 */
2500 if (t_rules->attr.request_def) {
2501 tmpl_request_ref_list_acopy(vpt, &vpt->rules.attr.request_def, t_rules->attr.request_def);
2502 }
2503
2504 /*
2505 * Now that all of the lists are set correctly, do some final validation and updates on the
2506 * attribute.
2507 */
2508 if (tmpl_is_attr(vpt)) {
2509 tmpl_attr_t *ar;
2510
2511 /*
2512 * Ensure that the list is set correctly, so that the returned vpt doesn't just
2513 * match the input rules, it is also internally consistent.
2514 */
2515 ar = tmpl_attr_list_head(tmpl_attr(vpt));
2516 fr_assert(ar != NULL);
2517
2518 if (tmpl_attr_is_list_attr(ar)) vpt->rules.attr.list_def = ar->ar_da;
2519
2521 /*
2522 * Suppress useless casts.
2523 */
2525 vpt->rules.cast = FR_TYPE_NULL;
2526 }
2527
2528 /*
2529 * Check if the cast is allowed. This lets us give better errors at compile time.
2530 */
2531 if ((tmpl_rules_cast(vpt)!= FR_TYPE_NULL) &&
2533 fr_strerror_printf("Cannot cast type '%s' to '%s'",
2536 fr_sbuff_set_to_start(&our_name);
2537 goto error;
2538 }
2539 }
2540 }
2541
2543 *out = vpt;
2544
2545 FR_SBUFF_SET_RETURN(name, &our_name);
2546}
2547
2548/** Parse a string into a TMPL_TYPE_ATTR_* type #tmpl_t
2549 *
2550 * @param[in,out] ctx to allocate #tmpl_t in.
2551 * @param[out] err May be NULL. Provides the exact error that the parser hit
2552 * when processing the attribute ref.
2553 * @param[out] out Where to write pointer to new #tmpl_t.
2554 * @param[in] name of attribute including #tmpl_request_ref_t and #fr_pair_list_t qualifiers.
2555 * @param[in] t_rules Rules which control parsing. See tmpl_afrom_attr_substr() for details.
2556 *
2557 * @note Unlike #tmpl_afrom_attr_substr this function will error out if the entire
2558 * name string isn't parsed.
2559 */
2561 tmpl_t **out, char const *name, tmpl_rules_t const *t_rules)
2562{
2563 ssize_t slen, name_len;
2565
2567
2568 name_len = strlen(name);
2569 slen = tmpl_afrom_attr_substr(ctx, err, out, &FR_SBUFF_IN(name, name_len), NULL, t_rules);
2570 if (slen <= 0) return slen;
2571
2572 if (!fr_cond_assert(*out)) return -1;
2573
2574 if (slen != name_len) {
2575 /* This looks wrong, but it produces meaningful errors for unknown attrs */
2576 fr_strerror_printf("Unexpected text after %s",
2577 tmpl_type_to_str((*out)->type));
2578 return -slen;
2579 }
2580
2581 TMPL_VERIFY(*out);
2582
2583 return slen;
2584}
2585
2586/** Create TMPL_TYPE_DATA from a string
2587 *
2588 * @param[in] ctx to allocate tmpl to.
2589 * @param[out] out where to write tmpl.
2590 * @param[in] in sbuff to parse.
2591 * @param[in] quote surrounding the operand to parse.
2592 * @param[in] t_rules specifying the cast and any enumeration values.
2593 * @param[in] allow_enum Whether parsing the value as an enum should be allowed.
2594 * @param[in] p_rules formatting rules.
2595 * @return
2596 * - <0 on error
2597 * - >=0 on success.
2598 */
2600 fr_token_t quote,
2601 tmpl_rules_t const *t_rules, bool allow_enum,
2602 fr_sbuff_parse_rules_t const *p_rules)
2603{
2604 fr_sbuff_t our_in = FR_SBUFF(in);
2605 fr_value_box_t tmp;
2606 tmpl_t *vpt;
2608
2609 if (!fr_type_is_null(t_rules->cast)) cast = t_rules->cast;
2610
2611 if (!fr_type_is_leaf(cast)) {
2612 fr_strerror_printf("%s is not a valid cast type",
2613 fr_type_to_str(cast));
2614 FR_SBUFF_ERROR_RETURN(&our_in);
2615 }
2616
2617 vpt = tmpl_alloc_null(ctx);
2618 if (fr_value_box_from_substr(vpt, &tmp,
2619 cast, allow_enum ? t_rules->enumv : NULL,
2620 &our_in, p_rules) < 0) {
2622 FR_SBUFF_ERROR_RETURN(&our_in);
2623 }
2625
2626 tmpl_init(vpt, TMPL_TYPE_DATA, quote, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in), t_rules);
2627
2629
2630 if (cast == tmpl_value_type(vpt)) vpt->rules.cast = FR_TYPE_NULL;
2631
2633 *out = vpt;
2634
2635 FR_SBUFF_SET_RETURN(in, &our_in);
2636}
2637
2638/** Match the bareword `null` and return a TMPL_TYPE_DATA carrying an FR_TYPE_NULL box
2639 *
2640 * Used as an explicit "no value" placeholder by callers that want the
2641 * argument slot to remain present (so positional xlat arguments line
2642 * up) without carrying any bytes. Downstream code distinguishes an
2643 * intentional null from an uninitialised one by checking
2644 * `fr_type_is_null(vb->type)` after the box has made it into an arg
2645 * list - if it reaches the xlat body, the author put it there.
2646 *
2647 * @param[in] ctx to allocate tmpl to.
2648 * @param[out] out where to write tmpl.
2649 * @param[in] in sbuff to parse.
2650 * @param[in] p_rules formatting rules.
2651 * @return
2652 * - 0 sbuff does not contain the `null` keyword.
2653 * - > 0 how many bytes were parsed.
2654 */
2656 fr_sbuff_parse_rules_t const *p_rules)
2657{
2658 fr_sbuff_t our_in = FR_SBUFF(in);
2659 tmpl_t *vpt;
2660
2661 if (!fr_sbuff_adv_past_strcase_literal(&our_in, "null")) return 0;
2662 if (!tmpl_substr_terminal_check(&our_in, p_rules)) return 0;
2663
2665 fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
2666 fr_value_box_init(&vpt->data.literal, FR_TYPE_NULL, NULL, false);
2667
2669 *out = vpt;
2670
2671 FR_SBUFF_SET_RETURN(in, &our_in);
2672}
2673
2674/** Parse a truth value
2675 *
2676 * @param[in] ctx to allocate tmpl to.
2677 * @param[out] out where to write tmpl.
2678 * @param[in] in sbuff to parse.
2679 * @param[in] p_rules formatting rules.
2680 * @return
2681 * - < 0 sbuff does not contain a boolean value.
2682 * - > 0 how many bytes were parsed.
2683 */
2685 fr_sbuff_parse_rules_t const *p_rules)
2686{
2687 fr_sbuff_t our_in = FR_SBUFF(in);
2688 bool a_bool;
2689 tmpl_t *vpt;
2690
2691 if (fr_sbuff_out(NULL, &a_bool, &our_in) < 0) {
2692 fr_strerror_const("Not a boolean value");
2693 return 0;
2694 }
2695
2696 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2697 fr_strerror_const("Unexpected text after bool");
2698 FR_SBUFF_ERROR_RETURN(&our_in);
2699 }
2700
2702
2703 fr_value_box_init(&vpt->data.literal, FR_TYPE_BOOL, NULL, false);
2704 vpt->data.literal.vb_bool = a_bool;
2705
2707 *out = vpt;
2708
2709 FR_SBUFF_SET_RETURN(in, &our_in);
2710}
2711
2712/** Parse bareword as an octet string
2713 *
2714 * @param[in] ctx to allocate tmpl to.
2715 * @param[out] out where to write tmpl.
2716 * @param[in] in sbuff to parse.
2717 * @param[in] p_rules formatting rules.
2718 * @return
2719 * - < 0 negative offset where parse error occurred.
2720 * - 0 sbuff does not contain a hex string.
2721 * - > 0 how many bytes were parsed.
2722 */
2724 fr_sbuff_parse_rules_t const *p_rules)
2725{
2726 fr_sbuff_t our_in = FR_SBUFF(in);
2727 tmpl_t *vpt;
2728 char *hex;
2729 size_t binlen, len;
2730 uint8_t *bin;
2731
2732 if (!fr_sbuff_adv_past_strcase_literal(&our_in, "0x")) return 0;
2733
2734 MEM(vpt = tmpl_alloc(ctx, TMPL_TYPE_DATA, T_BARE_WORD, NULL, 0));
2735
2736 /*
2737 * This allows stream parsing to work correctly
2738 * we could be less lazy and copy hex data in
2739 * chunks, but never mind...
2740 */
2741 len = fr_sbuff_out_abstrncpy_allowed(vpt, &hex, &our_in, SIZE_MAX, sbuff_char_class_hex);
2742 if (len & 0x01) {
2743 fr_strerror_const("Hex string not even length");
2744 error:
2746 FR_SBUFF_ERROR_RETURN(&our_in);
2747 }
2748 if (len == 0) {
2749 fr_strerror_const("Zero length hex string is invalid");
2750 goto error;
2751 }
2752
2753 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2754 fr_strerror_const("Unexpected text after hex string");
2755 goto error;
2756 }
2757
2758 bin = (uint8_t *)hex;
2759 binlen = len / 2;
2760
2762
2763 (void)fr_base16_decode(NULL, &FR_DBUFF_TMP(bin, binlen), &FR_SBUFF_IN(hex, len), false);
2764 MEM(bin = talloc_realloc_size(vpt, bin, binlen)); /* Realloc to the correct length */
2765 (void)fr_value_box_memdup_shallow(&vpt->data.literal, NULL, bin, binlen, false);
2766
2768 *out = vpt;
2769
2770 FR_SBUFF_SET_RETURN(in, &our_in);
2771}
2772
2773/** Parse bareword as an IPv4 address or prefix
2774 *
2775 * @param[in] ctx to allocate tmpl to.
2776 * @param[out] out where to write tmpl.
2777 * @param[in] in sbuff to parse.
2778 * @param[in] p_rules formatting rules.
2779 * @return
2780 * - < 0 sbuff does not contain an IPv4 address or prefix.
2781 * - > 0 how many bytes were parsed.
2782 */
2784 fr_sbuff_parse_rules_t const *p_rules)
2785{
2786 tmpl_t *vpt;
2787 fr_sbuff_t our_in = FR_SBUFF(in);
2789 int count;
2790 uint32_t ipaddr;
2791 uint8_t addr[4] = {}, prefix = 32;
2792
2793 for (count = 0; count < 4; count++) {
2794 if (!fr_sbuff_out(NULL, &addr[count], &our_in)) FR_SBUFF_ERROR_RETURN(&our_in);
2795
2796 if (count == 3) break;
2797
2798 if (fr_sbuff_next_if_char(&our_in, '.')) continue;
2799
2800 if (!fr_sbuff_is_char(&our_in, '/')) FR_SBUFF_ERROR_RETURN(&our_in);
2801 }
2802
2803 /*
2804 * If it has a trailing '/' then it's an IP prefix.
2805 */
2806 if (fr_sbuff_next_if_char(&our_in, '/')) {
2807 if (fr_sbuff_out(NULL, &prefix, &our_in) < 0) {
2808 fr_strerror_const("IPv4 CIDR mask malformed");
2809 FR_SBUFF_ERROR_RETURN(&our_in);
2810 }
2811
2812 if (prefix > 32) {
2813 fr_strerror_const("IPv4 CIDR mask too high");
2814 FR_SBUFF_ERROR_RETURN(&our_in);
2815 }
2816
2818 } else {
2820 }
2821
2822 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2823 fr_strerror_const("Unexpected text after IPv4 string or prefix");
2824 FR_SBUFF_ERROR_RETURN(&our_in);
2825 }
2826
2828 fr_value_box_init(&vpt->data.literal, type, NULL, false);
2829 vpt->data.literal.vb_ip.af = AF_INET;
2830 vpt->data.literal.vb_ip.prefix = prefix;
2831
2832 /*
2833 * Zero out lower bits
2834 */
2835 ipaddr = (((uint32_t) addr[0]) << 24) | (((uint32_t) addr[1]) << 16) | (((uint32_t) addr[2]) << 8) | addr[3];
2836 if (prefix == 0) {
2837 ipaddr = 0;
2838
2839 } else if (prefix < 32) {
2840 ipaddr &= ~((uint32_t) 0) << (32 - prefix);
2841 }
2842 vpt->data.literal.vb_ipv4addr = htonl(ipaddr);
2843
2845 *out = vpt;
2846
2847 FR_SBUFF_SET_RETURN(in, &our_in);
2848}
2849
2850/** Parse bareword as an IPv6 address or prefix
2851 *
2852 * @param[in] ctx to allocate tmpl to.
2853 * @param[out] out where to write tmpl.
2854 * @param[in] in sbuff to parse.
2855 * @param[in] p_rules formatting rules.
2856 * @return
2857 * - < 0 sbuff does not contain an IPv6 address or prefix.
2858 * - > 0 how many bytes were parsed.
2859 */
2861 fr_sbuff_parse_rules_t const *p_rules)
2862{
2863 tmpl_t *vpt;
2864 fr_sbuff_t our_in = FR_SBUFF(in);
2867 size_t len;
2868 char *sep_a, *sep_b;
2869
2870 static const bool ipv6_chars[SBUFF_CHAR_CLASS] = {
2871 ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true,
2872 ['5'] = true, ['6'] = true, ['7'] = true, ['8'] = true, ['9'] = true,
2873 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true,
2874 ['f'] = true,
2875 ['A'] = true, ['B'] = true, ['C'] = true, ['D'] = true, ['E'] = true,
2876 ['F'] = true,
2877 [':'] = true, ['.'] = true
2878 };
2879
2880 /*
2881 * Drop a marker to pin the start of the
2882 * address in the buffer.
2883 */
2884 fr_sbuff_marker(&m, &our_in);
2885
2886 /*
2887 * Check for something looking like an IPv6 address
2888 *
2889 * Minimum string is '::'
2890 */
2891 len = fr_sbuff_adv_past_allowed(&our_in, FR_IPADDR_STRLEN + 1, ipv6_chars, NULL);
2892 if ((len < 2) || (len > FR_IPADDR_STRLEN)) {
2893 error:
2894 FR_SBUFF_ERROR_RETURN(&our_in);
2895 }
2896
2897 /*
2898 * Got ':' after '.', this isn't allowed.
2899 *
2900 * We need this check else IPv4 gets parsed
2901 * as blank IPv6 address.
2902 */
2903 sep_a = memchr(fr_sbuff_current(&m), '.', len);
2904 if (sep_a && (!(sep_b = memchr(fr_sbuff_current(&m), ':', len)) || (sep_b > sep_a))) {
2905 fr_strerror_const("First IPv6 component separator was a '.'");
2906 goto error;
2907 }
2908
2909 /*
2910 * The v6 parse function will happily turn
2911 * integers into v6 addresses *sigh*.
2912 */
2913 sep_a = memchr(fr_sbuff_current(&m), ':', len);
2914 if (!sep_a) {
2915 fr_strerror_const("No IPv6 component separator");
2916 goto error;
2917 }
2918
2919 /*
2920 * Handle scope
2921 */
2922 if (fr_sbuff_next_if_char(&our_in, '%')) {
2923 len = fr_sbuff_adv_until(&our_in, IFNAMSIZ + 1, p_rules ? p_rules->terminals : NULL, '\0');
2924 if ((len < 1) || (len > IFNAMSIZ)) {
2925 fr_strerror_const("IPv6 scope too long");
2926 goto error;
2927 }
2928 }
2929
2930 /*
2931 * ...and finally the prefix.
2932 */
2933 if (fr_sbuff_next_if_char(&our_in, '/')) {
2934 uint8_t mask;
2935
2936 if (fr_sbuff_out(NULL, &mask, &our_in) < 0) {
2937 fr_strerror_const("IPv6 CIDR mask malformed");
2938 goto error;
2939 }
2940 if (mask > 128) {
2941 fr_strerror_const("IPv6 CIDR mask too high");
2942 goto error;
2943 }
2944
2946 } else {
2948 }
2949
2950 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
2951 fr_strerror_const("Unexpected text after IPv6 string or prefix");
2952 goto error;
2953 }
2954
2956 if (fr_value_box_from_substr(vpt, &vpt->data.literal, type, NULL,
2957 &FR_SBUFF_REPARSE(&our_in),
2958 NULL) < 0) {
2960 goto error;
2961 }
2962
2964 *out = vpt;
2965
2966 FR_SBUFF_SET_RETURN(in, &our_in);
2967}
2968
2969
2970/** Try and parse signed or unsigned integers
2971 *
2972 * @param[in] ctx to allocate tmpl to.
2973 * @param[out] out where to write tmpl.
2974 * @param[in] in sbuff to parse.
2975 * @param[in] p_rules formatting rules.
2976 * @return
2977 * - < 0 sbuff does not contain a mac address.
2978 * - > 0 how many bytes were parsed.
2979 */
2981 fr_sbuff_parse_rules_t const *p_rules)
2982{
2983 tmpl_t *vpt;
2984 fr_sbuff_t our_in = FR_SBUFF(in);
2985 uint8_t buff[6] = {};
2986 fr_dbuff_t dbuff;
2987 fr_value_box_t *vb;
2989
2990 fr_dbuff_init(&dbuff, buff, sizeof(buff));
2991
2992 fr_base16_decode(&err, &dbuff, &our_in, true);
2993 if (err != FR_SBUFF_PARSE_OK) return 0;
2994
2995 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
2996
2997 fr_base16_decode(&err, &dbuff, &our_in, true);
2998 if (err != FR_SBUFF_PARSE_OK) return 0;
2999
3000 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
3001
3002 fr_base16_decode(&err, &dbuff, &our_in, true);
3003 if (err != FR_SBUFF_PARSE_OK) return 0;
3004
3005 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
3006
3007 fr_base16_decode(&err, &dbuff, &our_in, true);
3008 if (err != FR_SBUFF_PARSE_OK) return 0;
3009
3010 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
3011
3012 fr_base16_decode(&err, &dbuff, &our_in, true);
3013 if (err != FR_SBUFF_PARSE_OK) return 0;
3014
3015 if (!fr_sbuff_next_if_char(&our_in, ':')) return 0;
3016
3017 fr_base16_decode(&err, &dbuff, &our_in, true);
3018 if (err != FR_SBUFF_PARSE_OK) return 0;
3019
3020 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
3021 fr_strerror_const("Unexpected text after mac address");
3022 return 0;
3023 }
3024
3026 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
3027 vb = tmpl_value(vpt);
3028
3029 fr_value_box_init(vb, FR_TYPE_ETHERNET, NULL, false);
3030 memcpy(vb->vb_ether, buff, sizeof(vb->vb_ether));
3031
3033 *out = vpt;
3034
3035 FR_SBUFF_SET_RETURN(in, &our_in);
3036}
3037
3038/** Try and parse signed or unsigned integers
3039 *
3040 * @param[in] ctx to allocate tmpl to.
3041 * @param[out] out where to write tmpl.
3042 * @param[in] in sbuff to parse.
3043 * @param[in] p_rules formatting rules.
3044 * @return
3045 * - < 0 sbuff does not contain an integer.
3046 * - > 0 how many bytes were parsed.
3047 */
3049 fr_sbuff_parse_rules_t const *p_rules)
3050{
3051 tmpl_t *vpt;
3052 fr_sbuff_t our_in = FR_SBUFF(in);
3053 ssize_t slen;
3054 fr_value_box_t *vb;
3055
3056 /*
3057 * Pick the narrowest signed type
3058 */
3059 if (fr_sbuff_is_char(&our_in, '-')) {
3060 int64_t a_int;
3061
3062 slen = fr_sbuff_out(NULL, &a_int, &our_in);
3063 if (slen <= 0) return 0;
3064
3065 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
3066 fr_strerror_const("Unexpected text after signed integer");
3067 error:
3068 FR_SBUFF_ERROR_RETURN(&our_in);
3069 }
3070
3072 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
3073 vb = tmpl_value(vpt);
3074 if (a_int >= INT8_MIN) {
3075 fr_value_box_init(vb, FR_TYPE_INT8, NULL, false);
3076 vb->vb_int8 = (int8_t)a_int;
3077 } else if (a_int >= INT16_MIN) {
3078 fr_value_box_init(vb, FR_TYPE_INT16, NULL, false);
3079 vb->vb_int16 = (int16_t)a_int;
3080 } else if (a_int >= INT32_MIN) {
3081 fr_value_box_init(vb, FR_TYPE_INT32, NULL, false);
3082 vb->vb_int32 = (int32_t)a_int;
3083 } else {
3084 fr_value_box_init(vb, FR_TYPE_INT64, NULL, false);
3085 vb->vb_int64 = (int64_t)a_int;
3086 }
3087 /*
3088 * Pick the narrowest unsigned type
3089 */
3090 } else {
3091 uint64_t a_uint;
3092
3093 slen = fr_sbuff_out(NULL, &a_uint, &our_in);
3094 if (slen <= 0) return slen;
3095
3096 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
3097 fr_strerror_const("Unexpected text after unsigned integer");
3098 goto error;
3099 }
3100
3102 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
3103 vb = tmpl_value(vpt);
3104 if (a_uint <= UINT8_MAX) {
3105 fr_value_box_init(vb, FR_TYPE_UINT8, NULL, false);
3106 vb->vb_uint8 = (uint8_t)a_uint;
3107 } else if (a_uint <= UINT16_MAX) {
3108 fr_value_box_init(vb, FR_TYPE_UINT16, NULL, false);
3109 vb->vb_uint16 = (uint16_t)a_uint;
3110 } else if (a_uint <= UINT32_MAX) {
3111 fr_value_box_init(vb, FR_TYPE_UINT32, NULL, false);
3112 vb->vb_uint32 = (uint32_t)a_uint;
3113 } else {
3114 fr_value_box_init(vb, FR_TYPE_UINT64, NULL, false);
3115 vb->vb_uint64 = (uint64_t)a_uint;
3116 }
3117 }
3118
3120 *out = vpt;
3121
3122 FR_SBUFF_SET_RETURN(in, &our_in);
3123}
3124
3126 fr_sbuff_parse_rules_t const *p_rules)
3127{
3128 tmpl_t *vpt;
3129 fr_sbuff_t our_in = FR_SBUFF(in);
3130 double a_float;
3131 ssize_t slen;
3132 fr_value_box_t *vb;
3133
3134 slen = fr_sbuff_out(NULL, &a_float, &our_in);
3135 if (slen <= 0) return 0;
3136
3137 if (!tmpl_substr_terminal_check(&our_in, p_rules)) {
3138 fr_strerror_const("Unexpected text after float");
3139 FR_SBUFF_ERROR_RETURN(&our_in);
3140 }
3141
3143 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
3144 vb = tmpl_value(vpt);
3145 fr_value_box_init(vb, FR_TYPE_FLOAT64, NULL, false);
3146 vb->vb_float64 = a_float;
3147
3149 *out = vpt;
3150
3151 FR_SBUFF_SET_RETURN(in, &our_in);
3152}
3153
3155 fr_sbuff_parse_rules_t const *p_rules)
3156{
3157 tmpl_t *vpt;
3158 fr_sbuff_t our_in = FR_SBUFF(in);
3159 fr_time_delta_t a_delta;
3160 fr_slen_t slen;
3161 fr_value_box_t *vb;
3162
3163 slen = fr_time_delta_from_substr(&a_delta, &our_in, FR_TIME_RES_SEC, true, p_rules ? p_rules->terminals : NULL);
3164 if (slen <= 0) return 0;
3165
3167 T_BARE_WORD, fr_sbuff_start(&our_in), fr_sbuff_used(&our_in)));
3168 vb = tmpl_value(vpt);
3169 fr_value_box_init(vb, FR_TYPE_TIME_DELTA, NULL, false);
3170 vb->vb_time_delta = a_delta;
3171
3173 *out = vpt;
3174
3175 FR_SBUFF_SET_RETURN(in, &our_in);
3176}
3177
3178/*
3179 * ::value
3180 *
3181 * Treated as enum name. Note that this check MUST be done after the test for IPv6, as
3182 * "::1" is an allowed IPv6 address.
3183 *
3184 * @todo - Mark this up as an enum name? Or do we really care? Maybe we want to allow
3185 *
3186 * Service-Type == 'Framed-User'
3187 *
3188 * or
3189 *
3190 * Service-Type == "Framed-User'
3191 *
3192 * as the second one allows for xlat expansions of enum names.
3193 *
3194 * We probably do want to forbid the single-quoted form of enums,
3195 * as that doesn't seem to make sense.
3196 *
3197 * We also need to distinguish unresolved bare words as enums
3198 * (with :: prefix) from unresolved attributes without an & prefix.
3199 */
3200static ssize_t tmpl_afrom_enum(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in,
3201 fr_sbuff_parse_rules_t const *p_rules,
3202 tmpl_rules_t const *t_rules)
3203{
3204 tmpl_t *vpt;
3206 fr_sbuff_t our_in = FR_SBUFF(in);
3207 fr_sbuff_t *enum_buff;
3208
3209 FR_SBUFF_TALLOC_THREAD_LOCAL(&enum_buff, 1024, SIZE_MAX);
3210
3211 /*
3212 * If there isn't a "::" prefix, then check for migration flags, and enum.
3213 *
3214 * If we require an enum prefix, then the input can't be an enum, and we don't do any more
3215 * parsing.
3216 *
3217 * Otherwise if there's no prefix and no enumv, we know this input can't be an enum name.
3218 */
3219 if (!fr_sbuff_adv_past_str_literal(&our_in, "::")) {
3220 return 0;
3221
3222 } else if (t_rules->enumv &&
3223 ((t_rules->enumv->type == FR_TYPE_IPV6_ADDR) ||
3224 ((t_rules->enumv->type == FR_TYPE_IPV6_PREFIX)))) {
3225
3226 /*
3227 * We can't have enumerated names for IPv6 addresses.
3228 *
3229 * @todo - allow them ONLY if the RHS string is a valid enum name.
3230 */
3231 return 0;
3232 }
3233
3234 /*
3235 * Need to store the value with the prefix, because the value box functions
3236 * expect it to be there...
3237 */
3238 fr_sbuff_in_strcpy_literal(enum_buff, "::");
3239
3240 vpt = tmpl_alloc_null(ctx);
3241
3242 /*
3243 * If it doesn't match any other type of bareword, parse it as an enum name.
3244 *
3245 * Note that we don't actually try to resolve the enum name. The caller is responsible
3246 * for doing that.
3247 */
3248 if (fr_dict_enum_name_from_substr(enum_buff, &sberr, &our_in, p_rules ? p_rules->terminals : NULL) < 0) {
3249 /*
3250 * Produce our own errors which make
3251 * more sense in the context of tmpls
3252 */
3253 switch (sberr) {
3255 fr_strerror_const("No operand found. Expected &ref, literal, "
3256 "'quoted literal', \"%{expansion}\", or enum value");
3257 break;
3258
3260 fr_strerror_const("enum values must contain at least one alpha character");
3261 break;
3262
3263 default:
3264 fr_strerror_const("Unexpected text after enum value.");
3265 break;
3266 }
3267
3269 FR_SBUFF_ERROR_RETURN(&our_in);
3270 }
3271
3272 /*
3273 * If there's a valid enum name, then we use it. Otherwise we leave name resolution to run time.
3274 */
3275 if (t_rules->enumv) {
3276 fr_dict_enum_value_t const *dv;
3277
3278 dv = fr_dict_enum_by_name(t_rules->enumv, fr_sbuff_start(enum_buff), fr_sbuff_used(enum_buff));
3279 if (dv) {
3281 fr_sbuff_start(&our_in), fr_sbuff_used(&our_in), t_rules);
3282 if (unlikely(fr_value_box_copy(vpt, &vpt->data.literal, dv->value) < 0)) {
3284 return -1;
3285 }
3286 vpt->data.literal.enumv = t_rules->enumv;
3287
3289 *out = vpt;
3290
3291 FR_SBUFF_SET_RETURN(in, &our_in);
3292 }
3293 }
3294
3295 /*
3296 * Either there's no enum, or the enum name didn't match one of the listed ones. There's no
3297 * point in waiting for an enum which might be declared later. That's not possible, so we fall
3298 * back to parsing the various data types.
3299 */
3300 if (t_rules->at_runtime) return 0;
3301
3303 fr_sbuff_start(&our_in), fr_sbuff_used(&our_in), t_rules);
3304 MEM(vpt->data.unescaped = talloc_bstrndup(vpt, fr_sbuff_start(enum_buff), fr_sbuff_used(enum_buff)));
3305
3307 *out = vpt;
3308
3309 FR_SBUFF_SET_RETURN(in, &our_in);
3310}
3311
3312/** Convert an arbitrary string into a #tmpl_t
3313 *
3314 * @note Unlike #tmpl_afrom_attr_str return code 0 doesn't necessarily indicate failure,
3315 * may just mean a 0 length string was parsed. Check to see if the function emitted
3316 * a #tmpl_t in *out.
3317 *
3318 * @note xlats and regexes are left uncompiled. This is to support the two pass parsing
3319 * done by the modcall code. Compilation on pass1 of that code could fail, as
3320 * attributes or xlat functions registered by modules may not be available (yet).
3321 *
3322 * @note For details of attribute parsing see #tmpl_afrom_attr_substr.
3323 *
3324 * @param[in,out] ctx To allocate #tmpl_t in.
3325 * @param[out] out Where to write the pointer to the new #tmpl_t.
3326 * @param[in] in String to parse.
3327 * @param[in] quote Quoting around the tmpl. Determines what we
3328 * attempt to parse the string as.
3329 * @param[in] p_rules Formatting rules for the tmpl.
3330 * @param[in] t_rules Validation rules for attribute references.
3331 * @return
3332 * - < 0 on error (offset as negative integer)
3333 * - >= 0 on success (number of bytes parsed).
3334 *
3335 * @see REMARKER to produce pretty error markers from the return value.
3336 *
3337 * @see tmpl_afrom_attr_substr
3338 */
3340 fr_sbuff_t *in, fr_token_t quote,
3341 fr_sbuff_parse_rules_t const *p_rules,
3342 tmpl_rules_t const *t_rules)
3343{
3344 fr_sbuff_t our_in = FR_SBUFF(in);
3345
3346 fr_slen_t slen;
3348 char *str;
3349
3350 tmpl_t *vpt = NULL;
3352
3354
3355 *out = NULL;
3356
3357 switch (quote) {
3358 case T_BARE_WORD:
3359 /*
3360 * Skip other bareword types if
3361 * we find a '&' prefix.
3362 */
3363 if (fr_sbuff_is_char(&our_in, '&')) return tmpl_afrom_attr_substr(ctx, NULL, out, in,
3364 p_rules, t_rules);
3365
3366 /*
3367 * Allow bareword xlats if we
3368 * find a '%' prefix.
3369 */
3370 if (fr_sbuff_is_char(&our_in, '%')) {
3372 xlat_exp_head_t *head = NULL;
3373
3374 vpt = tmpl_alloc_null(ctx);
3375 slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
3376 if (slen <= 0) FR_SBUFF_ERROR_RETURN(&our_in);
3377
3380 goto set_tmpl;
3381
3382 } else if (fr_dlist_num_elements(&head->dlist) == 1) {
3383 xlat_exp_t *node = xlat_exp_head(head);
3384 tmpl_t *hoisted;
3385
3386 if (node->type != XLAT_TMPL) goto set_tmpl;
3387
3388 /*
3389 * We were asked to parse a tmpl. But it turned out to be an xlat %{...}
3390 *
3391 * If that xlat is identically a tmpl such as %{User-Name}, then we just
3392 * hoist the tmpl to this node. Otherwise at run time, we will have an
3393 * extra bounce through the xlat code, for no real reason.
3394 */
3395 hoisted = node->vpt;
3396
3397 (void) talloc_steal(ctx, hoisted);
3399 vpt = hoisted;
3400
3401 } else {
3402 set_tmpl:
3403 tmpl_init(vpt, type, quote, fr_sbuff_start(&our_in), slen, t_rules);
3404 vpt->data.xlat.ex = head;
3405 }
3406
3408 *out = vpt;
3409
3410 FR_SBUFF_SET_RETURN(in, &our_in);
3411 }
3412
3413 /*
3414 * Deal with explicit casts...
3415 */
3416 if (!fr_type_is_null(t_rules->cast)) {
3417 slen = tmpl_afrom_value_substr(ctx, out, in, quote, t_rules, true, p_rules);
3418
3419 /*
3420 * If the string doesn't cast to the destination type
3421 * parse it as an attribute.
3422 */
3423 if (slen < 0) return tmpl_afrom_attr_substr(ctx, NULL, out, in, p_rules, t_rules);
3424 return slen;
3425 }
3426
3427 /*
3428 * We're at runtime and have a data type. Just parse it as that data type, without doing
3429 * endless "maybe it's this thing" attempts.
3430 */
3431 if (t_rules->at_runtime && t_rules->enumv) {
3432 tmpl_rules_t my_t_rules = *t_rules;
3433
3434 fr_assert(fr_type_is_leaf(t_rules->enumv->type));
3435
3436 my_t_rules.cast = my_t_rules.enumv->type;
3437
3438 return tmpl_afrom_value_substr(ctx, out, in, quote, &my_t_rules, true, p_rules);
3439 }
3440
3441 /*
3442 * Prefer enum names to IPv6 addresses.
3443 */
3444 if (t_rules->enumv && fr_sbuff_is_str_literal(&our_in, "::")) {
3445 slen = tmpl_afrom_enum(ctx, out, &our_in, p_rules, t_rules);
3446 if (slen > 0) goto done_bareword;
3447 fr_assert(!*out);
3448 }
3449
3450 /*
3451 * See if it's the `null` keyword. Matched before the
3452 * numeric / address / enum branches so it isn't
3453 * shadowed by a dictionary attribute literally named
3454 * "null".
3455 */
3456 slen = tmpl_afrom_null_substr(ctx, out, &our_in, p_rules);
3457 if (slen > 0) goto done_bareword;
3458 fr_assert(!*out);
3459
3460 /*
3461 * See if it's a boolean value
3462 */
3463 slen = tmpl_afrom_bool_substr(ctx, out, &our_in, p_rules);
3464 if (slen > 0) {
3465 done_bareword:
3466 TMPL_VERIFY(*out);
3467
3468 FR_SBUFF_SET_RETURN(in, &our_in);
3469 }
3470 fr_assert(!*out);
3471
3472 /*
3473 * See if it's an octets string
3474 */
3475 slen = tmpl_afrom_octets_substr(ctx, out, &our_in, p_rules);
3476 if (slen > 0) goto done_bareword;
3477 fr_assert(!*out);
3478
3479 /*
3480 * See if it's a mac address
3481 *
3482 * Needs to be before IPv6 as the pton functions
3483 * are too greedy, and on macOS will happily
3484 * convert a mac address to an IPv6 address.
3485 */
3486 slen = tmpl_afrom_ether_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 an IPv4 address or prefix
3492 */
3493 slen = tmpl_afrom_ipv4_substr(ctx, out, &our_in, p_rules);
3494 if (slen > 0) goto done_bareword;
3495 fr_assert(!*out);
3496
3497 /*
3498 * See if it's an IPv6 address or prefix
3499 */
3500 slen = tmpl_afrom_ipv6_substr(ctx, out, &our_in, p_rules);
3501 if (slen > 0) goto done_bareword;
3502 fr_assert(!*out);
3503
3504 slen = tmpl_afrom_enum(ctx, out, &our_in, p_rules, t_rules);
3505 if (slen > 0) goto done_bareword;
3506 fr_assert(!*out);
3507
3508 /*
3509 * See if it's a integer
3510 */
3511 slen = tmpl_afrom_integer_substr(ctx, out, &our_in, p_rules);
3512 if (slen > 0) goto done_bareword;
3513 fr_assert(!*out);
3514
3515 /*
3516 * See if it's a float
3517 */
3518 slen = tmpl_afrom_float_substr(ctx, out, &our_in, p_rules);
3519 if (slen > 0) goto done_bareword;
3520 fr_assert(!*out);
3521
3522 /*
3523 * See if it's a time delta
3524 *
3525 * We do this after floats and integers so that
3526 * they get parsed as integer and float types
3527 * and not time deltas.
3528 */
3529 slen = tmpl_afrom_time_delta(ctx, out, &our_in, p_rules);
3530 if (slen > 0) goto done_bareword;
3531 fr_assert(!*out);
3532
3533 /*
3534 * See if it's an attribute reference
3535 * without the prefix.
3536 */
3537 slen = tmpl_afrom_attr_substr(ctx, NULL, out, &our_in, p_rules, t_rules);
3538 if (slen > 0) goto done_bareword;
3539 fr_assert(!*out);
3540
3541 /*
3542 * We can't parse it as anything, that's an error.
3543 *
3544 * But it may be an enumeration value for an
3545 * attribute which is loaded later. In which
3546 * case we allow parsing the enumeration.
3547 */
3548 if (!fr_sbuff_is_str_literal(&our_in, "::")) {
3549 /*
3550 * Return the error string from parsing the attribute!
3551 */
3552 FR_SBUFF_ERROR_RETURN(&our_in);
3553 }
3554
3555 /*
3556 * Attempt to resolve enumeration values
3557 */
3558 vpt = tmpl_alloc_null(ctx);
3559
3560 /*
3561 * If it doesn't match any other type of bareword, parse it as an enum name.
3562 *
3563 * Note that we don't actually try to resolve the enum name. The caller is responsible
3564 * for doing that.
3565 */
3566 if (fr_dict_enum_name_afrom_substr(vpt, &str, &sberr, &our_in, p_rules ? p_rules->terminals : NULL) < 0) {
3567 /*
3568 * Produce our own errors which make
3569 * more sense in the context of tmpls
3570 */
3571 switch (sberr) {
3573 fr_strerror_const("No operand found. Expected &ref, literal, "
3574 "'quoted literal', \"%{expansion}\", or enum value");
3575 break;
3576
3578 fr_strerror_const("enum values must contain at least one alpha character");
3579 break;
3580
3581 default:
3582 fr_strerror_const("Unexpected text after enum value.");
3583 break;
3584 }
3585
3587 FR_SBUFF_ERROR_RETURN(&our_in);
3588 }
3589
3591 fr_sbuff_start(&our_in), fr_sbuff_used(&our_in), t_rules);
3592 vpt->data.unescaped = str;
3593
3595 *out = vpt;
3596
3597 FR_SBUFF_SET_RETURN(in, &our_in);
3598
3600 /*
3601 * Single quoted strings can be cast
3602 * to a specific data type immediately
3603 * as they cannot contain expansions.
3604 */
3605 if (!fr_type_is_null(t_rules->cast)) return tmpl_afrom_value_substr(ctx, out, in, quote,
3606 t_rules, false,
3607 p_rules);
3608 vpt = tmpl_alloc_null(ctx);
3609 slen = fr_sbuff_out_aunescape_until(vpt, &str, &our_in, SIZE_MAX,
3610 p_rules ? p_rules->terminals : NULL,
3611 p_rules ? p_rules->escapes : NULL);
3612 tmpl_init(vpt, TMPL_TYPE_DATA_UNRESOLVED, quote, fr_sbuff_start(&our_in), slen, t_rules);
3613 vpt->data.unescaped = str;
3614 break;
3615
3617 {
3618 xlat_exp_head_t *head = NULL;
3620
3621 vpt = tmpl_alloc_null(ctx);
3622
3623 slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
3624 if (slen < 0) FR_SBUFF_ERROR_RETURN(&our_in);
3625
3626 /*
3627 * If the string doesn't contain an xlat,
3628 * and we want to cast it as a specific
3629 * type, then do the conversion now.
3630 */
3631 if (xlat_is_literal(head)) {
3632 if (!fr_type_is_null(t_rules->cast)) {
3633 talloc_free(vpt); /* Also frees any nodes */
3634
3635 return tmpl_afrom_value_substr(ctx, out,
3636 in, quote,
3637 t_rules, false, p_rules);
3638 }
3639
3640 /*
3641 * If the string doesn't contain an xlat
3642 * and there's no cast, we just store
3643 * the string for conversion later.
3644 */
3645 if (xlat_to_string(vpt, &str, &head)) {
3646 TALLOC_FREE(head);
3647
3649 fr_sbuff_start(&our_in), slen, t_rules);
3650 vpt->data.unescaped = str; /* Store the unescaped string for parsing later */
3651 break;
3652 }
3653 }
3654
3655 /*
3656 * If the string actually contains an xlat
3657 * store the compiled xlat.
3658 */
3660
3661 tmpl_init(vpt, type, quote, fr_sbuff_start(&our_in), slen, t_rules);
3662 vpt->data.xlat.ex = head;
3663 }
3664 break;
3665
3667 {
3669 xlat_exp_head_t *head = NULL;
3670
3671 vpt = tmpl_alloc_null(ctx);
3672
3673 /*
3674 * Ensure that we pre-parse the exec string.
3675 * This allows us to catch parse errors as early
3676 * as possible.
3677 *
3678 * FIXME - We need an ephemeral version of this
3679 * too.
3680 */
3681 slen = xlat_tokenize_argv(vpt, &head, &our_in, NULL, p_rules, t_rules, true);
3682 if ((slen <= 0) || !head) {
3684 FR_SBUFF_ERROR_RETURN(&our_in);
3685 }
3686
3687 /*
3688 * Ensure any xlats produced are bootstrapped
3689 * so that their instance data will be created.
3690 */
3691 if (xlat_finalize(head, t_rules->xlat.runtime_el) < 0) {
3693 fr_strerror_const("Failed to bootstrap xlat");
3694 FR_SBUFF_ERROR_RETURN(&our_in);
3695 }
3696
3698
3699 tmpl_init(vpt, type, quote, fr_sbuff_start(&our_in), slen, t_rules);
3700 vpt->data.xlat.ex = head;
3701 }
3702 break;
3703
3705 {
3706 xlat_exp_head_t *head = NULL;
3708 tmpl_rules_t arg_t_rules = *t_rules;
3709
3710 arg_t_rules.literals_safe_for = FR_REGEX_SAFE_FOR;
3711
3712 if (!fr_type_is_null(t_rules->cast)) {
3713 fr_strerror_const("Casts cannot be used with regular expressions");
3714 fr_sbuff_set_to_start(&our_in); /* Point to the cast */
3715 FR_SBUFF_ERROR_RETURN(&our_in);
3716 }
3717
3718 vpt = tmpl_alloc_null(ctx);
3719
3720 slen = xlat_tokenize(vpt, &head, &our_in, p_rules, &arg_t_rules);
3721 if (slen < 0) {
3723 FR_SBUFF_ERROR_RETURN(&our_in);
3724 }
3725
3726 /*
3727 * Check if the string actually contains an xlat
3728 * if it doesn't, we unfortunately still
3729 * can't compile the regex here, as we don't know if
3730 * it should be ephemeral or what flags should be used
3731 * during the compilation.
3732 *
3733 * The caller will need to do the compilation after we
3734 * return.
3735 */
3736 if (xlat_to_string(vpt, &str, &head)) {
3738 fr_sbuff_start(&our_in), slen, t_rules);
3739 vpt->data.unescaped = str; /* Store the unescaped string for compilation later */
3740 break;
3741 }
3742 /*
3743 * Mark the regex up as a regex-xlat which
3744 * will need expanding before evaluation, and can never
3745 * be pre-compiled.
3746 */
3748
3749 tmpl_init(vpt, type, quote, fr_sbuff_start(&our_in), slen, t_rules);
3750 vpt->data.xlat.ex = head;
3751 }
3752 break;
3753
3754 default:
3755 fr_assert_msg(0, "Unknown quote type %i", quote);
3756 FR_SBUFF_ERROR_RETURN(&our_in);
3757 }
3758
3760 *out = vpt;
3761
3762 FR_SBUFF_SET_RETURN(in, &our_in);
3763}
3764
3765/** Copy a tmpl
3766 *
3767 * Fully duplicates the contents of a tmpl including any nested attribute
3768 * references.
3769 *
3770 * @param[in] ctx to perform allocations under.
3771 * @param[in] in tmpl to duplicate.
3772 * @return
3773 * - NULL on error.
3774 * - A new tmpl on success.
3775 */
3776tmpl_t *tmpl_copy(TALLOC_CTX *ctx, tmpl_t const *in)
3777{
3778 tmpl_t *vpt;
3779
3780 MEM(vpt = tmpl_alloc(ctx, in->type, in->quote, in->name, in->len));
3781 vpt->rules = in->rules;
3782
3783 /*
3784 * Copy over the unescaped data
3785 */
3787 if (unlikely(!(vpt->data.unescaped = talloc_bstrdup(vpt, in->data.unescaped)))) {
3788 error:
3790 return NULL;
3791 }
3792 }
3793
3794 /*
3795 * Copy attribute references
3796 */
3797 else if (tmpl_contains_attr(vpt)) {
3798 if (unlikely(tmpl_attr_copy(vpt, in) < 0)) goto error;
3799
3800 /*
3801 * Copy flags for all regex flavours (and possibly recompile the regex)
3802 */
3803 } else if (tmpl_contains_regex(vpt)) {
3804 vpt->data.reg_flags = in->data.reg_flags;
3805
3806 /*
3807 * If the tmpl contains a _compiled_ regex
3808 * then convert it back to an uncompiled
3809 * regex and recompile.
3810 *
3811 * Most of the regex libraries don't allow
3812 * copying compiled expressions.
3813 */
3814 if (tmpl_is_regex(vpt)) {
3816 if (unlikely(!(vpt->data.unescaped = talloc_bstrdup(vpt, in->data.reg.src)))) goto error;
3817 if (unlikely(tmpl_regex_compile(vpt, in->data.reg.subcaptures) < 0)) goto error;
3818 return vpt;
3819 }
3820
3821 /*
3822 * The regex could also be an xlat.
3823 */
3825
3826 goto copy_xlat;
3827
3828 /*
3829 * Copy the xlat component.
3830 *
3831 * @todo - in general we can't copy an xlat, as the instances need resolving!
3832 *
3833 * We add an assertion here because nothing allocates the head, and we need it.
3834 */
3835 } else if (tmpl_contains_xlat(vpt)) {
3836 copy_xlat:
3837 fr_assert(in->data.xlat.ex != NULL);
3838
3839 vpt->data.xlat.ex = xlat_exp_head_alloc(vpt);
3840 if (!vpt->data.xlat.ex) goto error;
3841
3842 if (unlikely(xlat_copy(vpt, vpt->data.xlat.ex, in->data.xlat.ex) < 0)) goto error;
3843
3844 } else if (tmpl_is_data(vpt)) {
3845 if (unlikely(fr_value_box_copy(vpt, &vpt->data.literal, &in->data.literal) < 0)) goto error;
3846
3847 } else {
3848 fr_assert(0); /* copy of this type is unimplemented */
3849 }
3850
3852
3853 return vpt;
3854}
3855
3856/** Parse a cast specifier
3857 *
3858 * Note that casts are
3859 *
3860 * (foo)
3861 *
3862 * and NOT
3863 *
3864 * ( foo )
3865 *
3866 * Not for any particular reason, but to emphasize a bit that they're
3867 * not mathematical expressions.
3868 *
3869 * @param[out] rules to set the cast type in.
3870 * @param[in] in String containing the cast marker.
3871 * @return
3872 * - 0 no cast specifier found.
3873 * - >0 the number of bytes parsed.
3874 * - <0 offset of parse error.
3875 */
3877{
3878 char close = '\0';
3879 fr_sbuff_t our_in = FR_SBUFF(in);
3881 fr_type_t cast;
3882 ssize_t slen;
3883
3884 if (fr_sbuff_next_if_char(&our_in, '(')) {
3885 close = ')';
3886
3887 } else {
3888 if (rules) rules->cast = FR_TYPE_NULL;
3889 return 0;
3890 }
3891
3892 fr_sbuff_marker(&m, &our_in);
3894 if (fr_type_is_null(cast)) {
3895 fr_strerror_const("Unknown data type");
3896 FR_SBUFF_ERROR_RETURN(&our_in);
3897 }
3898 if (fr_type_is_non_leaf(cast)) {
3899 fr_strerror_printf("Forbidden data type '%s' in cast", fr_type_to_str(cast));
3901 }
3902
3903 if (!fr_sbuff_next_if_char(&our_in, close)) {
3904 fr_strerror_const("Unterminated cast");
3905 FR_SBUFF_ERROR_RETURN(&our_in);
3906 }
3907 fr_sbuff_adv_past_whitespace(&our_in, SIZE_MAX, NULL);
3908
3909 if (rules) rules->cast = cast;
3910
3911 FR_SBUFF_SET_RETURN(in, &our_in);
3912}
3913
3914/** Set a cast for a tmpl
3915 *
3916 * @param[in,out] vpt to set cast for.
3917 * @param[in] dst_type to set.
3918 * @return
3919 * - 0 on success.
3920 * - -1 on failure.
3921 */
3923{
3924 fr_type_t src_type;
3925
3926 switch (dst_type) {
3927 default:
3928 fr_strerror_printf("Forbidden data type '%s' in cast",
3929 fr_type_to_str(dst_type));
3930 return -1;
3931
3932 /*
3933 * We can always remove a cast.
3934 */
3935 case FR_TYPE_NULL:
3936 goto done;
3937
3938 /*
3939 * Only "base" data types are allowed. Structural types
3940 * and horrid WiMAX crap is forbidden.
3941 */
3942 case FR_TYPE_LEAF:
3943 break;
3944 }
3945
3946 switch (vpt->type) {
3947 /*
3948 * This should have been fixed before we got here.
3949 */
3951
3952 /*
3953 * By default, tmpl types cannot be cast to anything.
3954 */
3955 default:
3956 fr_strerror_const("Cannot use cast here.");
3957 return -1;
3958
3959 /*
3960 * These tmpl types are effectively of data type
3961 * "string", so they can be cast to anything.
3962 */
3963 case TMPL_TYPE_XLAT:
3964 case TMPL_TYPE_EXEC:
3968 break;
3969
3970 case TMPL_TYPE_DATA:
3971 src_type = tmpl_value_type(vpt);
3972 goto check_types;
3973
3974 case TMPL_TYPE_ATTR:
3975 {
3977
3978 /*
3979 * If the attribute has an enum, then the cast means "use the raw value, and not
3980 * the enum name".
3981 */
3982 if (da->type == dst_type) {
3983 if (da->flags.has_value) goto done;
3984 return 0;
3985 }
3986 src_type = da->type;
3987 }
3988
3989 /*
3990 * Suppress casts where they are duplicate, unless there's an enumv. In which case the
3991 * cast means "don't print the enumv value, just print the raw data".
3992 */
3993 check_types:
3994 if (src_type == dst_type) {
3995 /*
3996 * Cast with enumv means "use the raw value, and not the enum name".
3997 */
3998 if (tmpl_rules_enumv(vpt)) {
3999 tmpl_rules_enumv(vpt) = NULL;
4000 goto done;
4001 }
4002 return 0;
4003 }
4004
4005 if (!fr_type_cast(dst_type, src_type)) {
4006 fr_strerror_printf("Cannot cast type '%s' to '%s'",
4007 fr_type_to_str(src_type),
4008 fr_type_to_str(dst_type));
4009 return -1;
4010 }
4011 break;
4012 }
4013
4014done:
4015 vpt->rules.cast = dst_type;
4016
4018
4019 return 0;
4020}
4021
4022#ifdef HAVE_REGEX
4023/** Parse a set of regular expression flags
4024 *
4025 * @param[out] vpt Write the flags to the regex flags field in this #tmpl_t.
4026 * @param[in] in Where to parse the flag string from.
4027 * @param[in] terminals That mark the end of the regex flag string.
4028 * @return
4029 * - 0 no flags found.
4030 * - >0 the number of bytes of flags parsed.
4031 * - <0 offset of parse error.
4032 */
4033ssize_t tmpl_regex_flags_substr(tmpl_t *vpt, fr_sbuff_t *in, fr_sbuff_term_t const *terminals)
4034{
4035 fr_slen_t slen;
4036 int err = 0;
4037
4039
4040 slen = regex_flags_parse(&err, &vpt->data.reg_flags, in, terminals, true);
4041 switch (err) {
4042 case 0:
4043 break;
4044
4045 case -1: /* Non-flag and non-terminal */
4046 case -2: /* Duplicate flag */
4047 return slen;
4048 }
4049
4051
4052 return slen;
4053}
4054#endif
4055
4056/** @name Change a #tmpl_t type, usually by casting or resolving a reference
4057 *
4058 * #tmpl_cast_in_place can be used to convert #TMPL_TYPE_DATA_UNRESOLVED to a #TMPL_TYPE_DATA of a
4059 * specified #fr_type_t.
4060 *
4061 * #tmpl_attr_unknown_add converts a #TMPL_TYPE_ATTR with an unknown #fr_dict_attr_t to a
4062 * #TMPL_TYPE_ATTR with a known #fr_dict_attr_t, by adding the unknown #fr_dict_attr_t to the main
4063 * dictionary, and updating the ``tmpl_attr_tail_da`` pointer.
4064 * @{
4065 */
4066
4067/** Determine the correct quoting after a cast
4068 *
4069 * @param[in] existing_quote Exiting quotation type.
4070 * @param[in] type Cast type.
4071 * @param[in] enumv Enumeration values.
4072 * @param[in] unescaped The unescaped value of an enumeration.
4073 * @param[in] unescaped_len Length of unescaped.
4074 */
4075static inline CC_HINT(always_inline)
4077 fr_type_t type, fr_dict_attr_t const *enumv,
4078 char const *unescaped, size_t unescaped_len)
4079{
4080 if (!fr_type_is_string(type)) return T_BARE_WORD;
4081
4082 if (enumv && fr_dict_enum_by_name(enumv, unescaped, unescaped_len)) return T_BARE_WORD;
4083
4084 /*
4085 * Leave the original quoting if it's
4086 * single or double, else default to
4087 * single quoting.
4088 */
4089 switch (existing_quote) {
4092 return existing_quote;
4093
4094 default:
4096 }
4097}
4098
4099
4100/** Convert #tmpl_t of type #TMPL_TYPE_DATA_UNRESOLVED or #TMPL_TYPE_DATA to #TMPL_TYPE_DATA of type specified
4101 *
4102 * @note Conversion is done in place.
4103 * @note For #TMPL_TYPE_DATA_UNRESOLVED, the type will be updated to #TMPL_TYPE_DATA
4104 *
4105 * @param[in,out] vpt The template to modify. Must be of type #TMPL_TYPE_DATA_UNRESOLVED
4106 * or #TMPL_TYPE_DATA, #TMPL_TYPE_ATTR_UNRESOLVED, or #TMPL_TYPE_ATTR
4107 * @param[in] type to cast to.
4108 * @param[in] enumv Enumerated dictionary values associated with a #fr_dict_attr_t.
4109 * @return
4110 * - 0 on success.
4111 * - -1 on failure.
4112 */
4114{
4116
4119
4120 switch (vpt->type) {
4122 {
4123 char *unescaped = vpt->data.unescaped;
4124
4125 /*
4126 * We're trying to convert an unresolved (bareword)
4127 * tmpl to octets.
4128 *
4129 * tmpl_afrom_substr uses the 0x prefix as type
4130 * inference, so if it was a hex string the tmpl
4131 * type would not have fallen through to
4132 * unresolved.
4133 *
4134 * That means if we're trying to resolve it here
4135 * it's really a printable string, not a sequence
4136 * of hexits, so we just want the binary
4137 * representation of that string, and not the hex
4138 * to bin conversion.
4139 */
4140 if (fr_type_is_octets(type)) {
4141 if (fr_value_box_memdup(vpt, &vpt->data.literal, enumv,
4142 (uint8_t const *)unescaped, talloc_strlen(unescaped),
4143 false) < 0) return -1;
4144 } else {
4145 if (fr_value_box_from_str(vpt, &vpt->data.literal, type,
4146 enumv,
4147 unescaped, talloc_strlen(unescaped),
4148 NULL) < 0) return -1;
4149 }
4150 vpt->type = TMPL_TYPE_DATA;
4151 vpt->quote = tmpl_cast_quote(vpt->quote, type, enumv,
4152 unescaped, talloc_strlen(unescaped));
4153 talloc_free(unescaped);
4154 fr_value_box_mark_safe_for(&vpt->data.literal, vpt->rules.literals_safe_for);
4155
4156 /*
4157 * The data is now of the correct type, so we don't need to keep a cast.
4158 */
4159 vpt->rules.cast = FR_TYPE_NULL;
4160 }
4161 break;
4162
4163 case TMPL_TYPE_DATA:
4164 {
4165 if (type == tmpl_value_type(vpt)) return 0; /* noop */
4166
4167 /*
4168 * Enumerations aren't used when casting between
4169 * data types. They're only used when processing
4170 * unresolved tmpls.
4171 *
4172 * i.e. TMPL_TYPE_DATA_UNRESOLVED != TMPL_TYPE_DATA(FR_TYPE_STRING)
4173 */
4174 if (fr_value_box_cast_in_place(vpt, &vpt->data.literal, type, NULL) < 0) return -1;
4175// fr_value_box_mark_safe_for(&vpt->data.literal, vpt->rules.literals_safe_for); ??? is this necessary?
4176
4177 /*
4178 * Strings get quoted, everything else is a bare
4179 * word...
4180 */
4181 if (fr_type_is_string(type)) {
4182 vpt->quote = T_SINGLE_QUOTED_STRING;
4183 } else {
4184 vpt->quote = T_BARE_WORD;
4185 }
4186
4187 /*
4188 * The data is now of the correct type, so we don't need to keep a cast.
4189 */
4190 vpt->rules.cast = FR_TYPE_NULL;
4191 }
4192 break;
4193
4194 case TMPL_TYPE_ATTR:
4195 /*
4196 * Suppress casts to the same type.
4197 */
4198 if (tmpl_attr_tail_da(vpt)->type == type) {
4199 vpt->rules.cast = FR_TYPE_NULL;
4200 break;
4201 }
4203
4205 vpt->rules.cast = type;
4206 break;
4207
4208 default:
4209 fr_assert(0);
4210 }
4212
4213 return 0;
4214}
4215
4216/** Resolve an unresolved attribute
4217 *
4218 * Multi-pass parsing fixups for attribute references.
4219 *
4220 * @param[in] vpt to resolve.
4221 * @param[in] tr_rules Combined with the original parse rules for
4222 * additional resolution passes.
4223 * @return
4224 * - 0 if all references were resolved.
4225 * - -1 if there are unknown attributes which need
4226 * adding to the global dictionary first.
4227 * - -2 if there are attributes we couldn't resolve.
4228 */
4229static inline CC_HINT(always_inline) int tmpl_attr_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules)
4230{
4231 tmpl_attr_t *ar = NULL, *next, *prev;
4232 fr_dict_attr_t const *da, *namespace;
4233 fr_dict_t const *dict_def;
4234
4236
4238
4239 dict_def = vpt->rules.attr.dict_def;
4240 if (!dict_def || tr_rules->force_dict_def) dict_def = tr_rules->dict_def;
4241
4242 /*
4243 * First component is special because we may need
4244 * to search for it in multiple dictionaries.
4245 *
4246 * This emulates what's done in the initial
4247 * tokenizer function.
4248 */
4249 ar = tmpl_attr_list_head(tmpl_attr(vpt));
4250 if (ar->type == TMPL_ATTR_TYPE_UNRESOLVED) {
4252 &da,
4253 dict_def,
4254 &FR_SBUFF_IN(ar->ar_unresolved,
4255 talloc_strlen(ar->ar_unresolved)),
4256 NULL,
4257 true,
4258 vpt->rules.attr.allow_foreign);
4259 if (!da) return -2; /* Can't resolve, maybe the caller can resolve later */
4260
4261 ar->ar_type = TMPL_ATTR_TYPE_NORMAL;
4262 ar->ar_da = da;
4263 ar->ar_parent = fr_dict_root(fr_dict_by_da(da));
4264
4265 /*
4266 * Record the dictionary that was
4267 * successfully used for resolution.
4268 */
4269 vpt->rules.attr.dict_def = tr_rules->dict_def;
4270
4271 /*
4272 * Reach into the next reference
4273 * and correct its parent and
4274 * namespace.
4275 */
4276 next = tmpl_attr_list_next(tmpl_attr(vpt), ar);
4277 if (next) {
4278 next->ar_parent = da;
4279 next->ar_unresolved_namespace = da;
4280 }
4281 }
4282
4283 /*
4284 * Loop, resolving each unresolved attribute in turn
4285 */
4286 while ((ar = tmpl_attr_list_next(tmpl_attr(vpt), ar))) {
4287 switch (ar->type) {
4290 continue; /* Don't need to resolve */
4291
4293 return -1; /* Unknown attributes must be resolved first */
4294
4295 default:
4296 break;
4297 }
4298
4299 prev = tmpl_attr_list_prev(tmpl_attr(vpt), ar);
4300
4301 /*
4302 * If the parent is a list AR, then use the default dictionary for the namespace
4303 */
4304 namespace = (prev && dict_def && tmpl_attr_is_list_attr(prev)) ? fr_dict_root(dict_def) : ar->ar_unresolved_namespace;
4305
4306 (void)fr_dict_attr_by_name_substr(NULL,
4307 &da,
4308 namespace,
4309 &FR_SBUFF_IN(ar->ar_unresolved,
4310 talloc_strlen(ar->ar_unresolved)),
4311 NULL);
4312 /*
4313 * Still can't resolve, check to see if
4314 * the last attribute reference was a
4315 * group.
4316 *
4317 * If it was, then we may be able to
4318 * fall back to resolving the attribute
4319 * in the internal dictionary.
4320 */
4321 if (!da) {
4322 if (prev && (prev->ar_da->type == FR_TYPE_GROUP)) {
4323 (void)fr_dict_attr_by_name_substr(NULL,
4324 &da,
4326 &FR_SBUFF_IN(ar->ar_unresolved,
4327 talloc_strlen(ar->ar_unresolved)),
4328 NULL);
4329 }
4330 if (!da) return -2;
4331 }
4332
4333 /*
4334 * Known attribute, just rewrite.
4335 */
4336 ar->ar_type = TMPL_ATTR_TYPE_NORMAL;
4337 ar->ar_da = da;
4338
4339 /*
4340 * Parent should have been corrected in
4341 * the previous loop iteration.
4342 */
4343 fr_assert(ar->ar_parent && !ar->ar_parent->flags.is_unknown);
4344
4345 /*
4346 * Reach into the next reference
4347 * and correct its parent.
4348 */
4349 next = tmpl_attr_list_next(tmpl_attr(vpt), ar);
4350 if (next) {
4351 next->ar_parent = da;
4352 next->ar_unresolved_namespace = da;
4353 }
4354
4355 /*
4356 * Remove redundant attributes
4357 *
4358 * If it's not a group or does not specify
4359 * an index, the ar is redundant and should
4360 * be removed.
4361 */
4362 prev = tmpl_attr_list_prev(tmpl_attr(vpt), ar);
4363 if (prev && (prev->ar_da->type != FR_TYPE_GROUP) && (prev->ar_num == NUM_UNSPEC)) {
4364 tmpl_attr_list_remove(tmpl_attr(vpt), prev);
4365 ar->ar_parent = prev->ar_parent;
4366 talloc_free(prev);
4367 }
4368 }
4369
4370 RESOLVED_SET(&vpt->type);
4372
4373 return 0;
4374}
4375
4376/** Resolve an unresolved xlat, i.e. one containing unresolved attribute references or xlat functions
4377 *
4378 * Multi-pass parsing fixups for attribute references.
4379 *
4380 * Works for base types:
4381 * - TMPL_TYPE_XLAT
4382 * - TMPL_TYPE_EXEC
4383 * - TMPL_TYPE_REGEX_XLAT
4384 *
4385 * @param[in] vpt Containing the xlat expansion to resolve.
4386 * @param[in] tr_rules Combined with the original parse rules for
4387 * additional resolution passes.
4388 * @return
4389 * - 0 on success.
4390 * - -1 on failure.
4391 */
4392static inline CC_HINT(always_inline)
4394{
4395 if (xlat_resolve(vpt->data.xlat.ex,
4397 .tr_rules = tr_rules,
4398 .allow_unresolved = false
4399 }) < 0) return -1;
4400
4401 fr_assert(!xlat_needs_resolving(vpt->data.xlat.ex));
4402
4403 RESOLVED_SET(&vpt->type);
4405
4406 return 0;
4407}
4408
4409/** Attempt to resolve functions and attributes in xlats and attribute references
4410 *
4411 * @note If resolution is successful, the rules->attr.dict_def field will be modified to
4412 * reflect the dictionary resolution was successful in.
4413 *
4414 * @param[in,out] vpt to resolve. Should be of type TMPL_TYPE_XLAT_UNRESOLVED
4415 * or TMPL_TYPE_ATTR_UNRESOLVED. All other types will be
4416 * noops.
4417 * @param[in] tr_rules Combined with the original parse rules for
4418 * additional resolution passes.
4419 * @return
4420 * - 0 on success.
4421 * - -1 on failure.
4422 */
4424{
4425 static tmpl_res_rules_t const default_tr_rules = {};
4426
4428
4429 if (!tmpl_needs_resolving(vpt)) return 0; /* Nothing to do */
4430
4431 if (!tr_rules) tr_rules = &default_tr_rules;
4432
4433 /*
4434 * Sanity check. There shouldn't be conflicting
4435 * enumvs between the original rules and resolution
4436 * rules.
4437 *
4438 * Either the enumv was available during parsing
4439 * and shouldn't have changed during subsequent
4440 * resolution passes, or it wasn't available at
4441 * parse-time, but now is.
4442 */
4443 if (tr_rules->enumv && tmpl_rules_enumv(vpt) && !tmpl_rules_enumv(vpt)->flags.is_unknown &&
4445 fr_strerror_printf("mismatch between parse-time enumv '%s' and resolution-time enumv '%s'",
4447 return -1;
4448 }
4449
4450 /*
4451 * The xlat component of the #tmpl_t needs resolving.
4452 *
4453 * This includes exec tmpls, which are largely xlats
4454 * "under the hood".
4455 */
4456 if (tmpl_contains_xlat(vpt)) {
4457 if (tmpl_xlat_resolve(vpt, tr_rules) < 0) return -1;
4458
4459 /*
4460 * The attribute reference needs resolving.
4461 */
4462 } else if (tmpl_contains_attr(vpt)) {
4463 fr_type_t dst_type = tmpl_rules_cast(vpt);
4464
4465 fr_assert(vpt->quote == T_BARE_WORD); /* 'User-Name' or "User-Name" is not allowed. */
4466
4467 if (tmpl_attr_resolve(vpt, tr_rules) < 0) return -1;
4468
4469 if (dst_type == tmpl_attr_tail_da(vpt)->type) {
4470 vpt->rules.cast = FR_TYPE_NULL;
4471 }
4472
4473 /*
4474 * Convert unresolved tmpls into enumvs, or failing that, string values.
4475 *
4476 * Unresolved tmpls are by definition TMPL_TYPE_DATA.
4477 */
4478 } else if (tmpl_is_data_unresolved(vpt)) {
4479 fr_type_t dst_type = tmpl_rules_cast(vpt);
4480 fr_dict_attr_t const *enumv = tmpl_rules_enumv(vpt);
4481
4482 /*
4483 * If there wasn't an enumv set in the
4484 * original rules, and we now have one
4485 * (possibly because the other side of a
4486 * binary expression has been resolved),
4487 * then use the new enumv.
4488 */
4489 if (!enumv) enumv = tr_rules->enumv;
4490
4491 /*
4492 * We don't have an explicit output type. Try to
4493 * interpret the data os the enumv data type, OR
4494 * if all else fails, it's a string.
4495 */
4496 if (fr_type_is_null(dst_type)) {
4497 /*
4498 * Infer the cast from the enumv type.
4499 */
4500 if (enumv) {
4501 dst_type = enumv->type;
4502
4503 } else if (vpt->quote != T_BARE_WORD) {
4504 dst_type = FR_TYPE_STRING; /* quoted strings are strings */
4505
4506 } else if (strncmp(vpt->data.unescaped, "::", 2) != 0) {
4507 /*
4508 * The rest of the code should have errored out before this.
4509 */
4510 fr_strerror_printf("Failed resolving data '%s' - it is not an attribute name or a quoted string", vpt->data.unescaped);
4511 return -1;
4512
4513 } else {
4514 /*
4515 * It's a valid enum ::NAME which was added _after_ the dictionaries were
4516 * loaded. That's fine. fr_value_box_from_substr() will skip over the
4517 * "::", and parse the enum name.
4518 */
4519 }
4520 }
4521
4522 /*
4523 * tmpl_cast_in_place first resolves using
4524 * the enumv, _then_ casts using the type.
4525 */
4526 if (tmpl_cast_in_place(vpt, dst_type, enumv) < 0) return -1;
4527
4529 /*
4530 * Catch any other cases of unresolved things
4531 * we need to address. We put the assert here
4532 * so we don't end up running inappropriate
4533 * code for non-debug builds.
4534 */
4535 } else {
4536#ifndef NDEBUG
4537 fr_assert(0);
4538#else
4539 return -1;
4540#endif
4541 }
4542
4544
4545 return 0;
4546}
4547
4548/** Reset the tmpl, leaving only the name in place
4549 *
4550 * After calling this function, the tmpl type will revert to TMPL_TYPE_DATA_UNRESOLVED
4551 * and only the name and quoting will be preserved.
4552 *
4553 * @param[in] vpt to reset.
4554 */
4556{
4557 tmpl_t tmp = {
4559 .name = vpt->name,
4560 .len = vpt->len,
4561 .quote = vpt->quote
4562 };
4563
4564 switch (vpt->type) {
4566 case TMPL_TYPE_MAX:
4567 fr_assert(0);
4568 break;
4569
4572 break;
4573
4574 case TMPL_TYPE_DATA:
4575 fr_value_box_clear(&vpt->data.literal);
4576 break;
4577
4578 /*
4579 * These types contain dynamically allocated
4580 * attribute and request references.
4581 */
4582 case TMPL_TYPE_ATTR:
4584 tmpl_attr_list_talloc_free(tmpl_attr(vpt));
4585 tmpl_request_list_talloc_free(&vpt->data.attribute.rr);
4586 break;
4587
4588 /*
4589 * These all store an xlat expansion
4590 */
4591 case TMPL_TYPE_EXEC:
4592 case TMPL_TYPE_XLAT:
4597 TALLOC_FREE(vpt->data.xlat.ex);
4598 break;
4599
4600 case TMPL_TYPE_REGEX:
4601 talloc_free(vpt->data.reg.ex);
4602 break;
4603
4604 }
4605
4606 memcpy(vpt, &tmp, sizeof(*vpt));
4607 vpt->data.unescaped = talloc_bstrdup(vpt, vpt->name);
4609}
4610
4611/** Add an unknown #fr_dict_attr_t specified by a #tmpl_t to the main dictionary
4612 *
4613 * @param vpt to add. ``tmpl_attr_tail_da`` pointer will be updated to point to the
4614 * #fr_dict_attr_t inserted into the dictionary.
4615 * @return
4616 * - 1 noop (did nothing) - Not possible to convert tmpl.
4617 * - 0 on success.
4618 * - -1 on failure.
4619 */
4621{
4622 tmpl_attr_t *ar = NULL, *next = NULL;
4623
4624 if (!vpt) return 1;
4625
4626 /*
4627 * Can't do this for expressions parsed at runtime
4628 */
4629 if (vpt->rules.at_runtime) return 1;
4630
4632
4634
4635 if (!tmpl_attr_tail_is_unknown(vpt)) return 1; /* Ensure at least the leaf is unknown */
4636
4637 while ((ar = tmpl_attr_list_next(tmpl_attr(vpt), ar))) {
4638 fr_dict_attr_t const *unknown, *known;
4639
4640 switch (ar->type) {
4641 case TMPL_ATTR_TYPE_NORMAL: /* Skip */
4643 continue;
4644
4645 case TMPL_ATTR_TYPE_UNRESOLVED: /* Shouldn't have been called */
4646 fr_strerror_const("Remaining attributes are unresolved");
4647 return -1;
4648
4650 break;
4651 }
4652
4653 unknown = ar->ar_unknown;
4654 known = fr_dict_attr_unknown_add(fr_dict_unconst(fr_dict_by_da(unknown)), unknown);
4655 if (!known) return -1;
4656
4657 /*
4658 * Fixup the parent of the next unknown
4659 * now it's known.
4660 */
4661 next = tmpl_attr_list_next(tmpl_attr(vpt), ar);
4662 if (next && (next->type == TMPL_ATTR_TYPE_UNKNOWN) &&
4663 (next->ar_da->parent == unknown)) {
4665 known) < 0) return -1;
4666 next->ar_parent = known;
4667 }
4668
4669 /*
4670 * Convert the ref to a normal type.
4671 * At runtime there should be no
4672 * "unknown" references as they should
4673 * have all been added to a
4674 * dictionary.
4675 */
4677
4678 /*
4679 * If the attribute is *NOT* raw then
4680 * swap the canonical unknown with the
4681 * one that was previously associated
4682 * with the tmpl.
4683 *
4684 * This establishes the unknown attribute
4685 * in the dictionary if it was really
4686 * unknown whilst not mucking up the
4687 * types for raw attributes.
4688 */
4689 if (!ar_is_raw(ar)) {
4690 fr_dict_attr_unknown_free(&ar->ar_da);
4691 ar->ar_da = known;
4692 } else if (!fr_cond_assert(!next)) {
4693 fr_strerror_const("Only the leaf may be raw");
4694 return -1;
4695 }
4696 }
4697
4698 return 0;
4699}
4700
4701/** Add an unresolved #fr_dict_attr_t specified by a #tmpl_t to the main dictionary
4702 *
4703 * @note fr_dict_attr_add will not return an error if the attribute already exists
4704 * meaning that multiple #tmpl_t specifying the same attribute can be
4705 * passed to this function to be fixed up, so long as the type and flags
4706 * are identical.
4707 *
4708 * @param[in] dict_def Default dictionary to use if none is
4709 * specified by the tmpl_attr_tail_unresolved.
4710 * @param[in] vpt specifying unresolved attribute to add.
4711 * ``tmpl_attr_tail_da`` pointer will be updated to
4712 * point to the #fr_dict_attr_t inserted
4713 * into the dictionary. Lists and requests
4714 * will be preserved.
4715 * @param[in] type to define unresolved attribute as.
4716 * @param[in] flags to define unresolved attribute with.
4717 * @return
4718 * - 1 noop (did nothing) - Not possible to convert tmpl.
4719 * - 0 on success.
4720 * - -1 on failure.
4721 */
4723 fr_type_t type, fr_dict_attr_flags_t const *flags)
4724{
4725 fr_dict_attr_t const *da;
4726 fr_dict_attr_flags_t our_flags = *flags;
4727
4728 our_flags.name_only = true;
4729
4730 if (!vpt) return -1;
4731
4733
4734 if (!tmpl_is_attr_unresolved(vpt)) return 1;
4735
4736 if (fr_dict_attr_add(dict_def,
4738 return -1;
4739 }
4741 if (!da) return -1;
4742
4743 if (type != da->type) {
4744 fr_strerror_printf("Attribute %s of type %s already defined with type %s",
4745 da->name, fr_type_to_str(type),
4746 fr_type_to_str(da->type));
4747 return -1;
4748 }
4749
4750 if (memcmp(flags, &da->flags, sizeof(*flags)) != 0) {
4751 fr_strerror_printf("Attribute %s already defined with different flags", da->name);
4752 return -1;
4753 }
4754
4755 tmpl_attr_set_da(vpt, da);
4756 vpt->type = TMPL_TYPE_ATTR;
4757
4758 return 0;
4759}
4760
4761#ifdef HAVE_REGEX
4762/** Convert a TMPL_TYPE_REGEX_UNCOMPILED into a TMPL_TYPE_REGEX
4763 *
4764 * Other regex types become noops.
4765 */
4766ssize_t tmpl_regex_compile(tmpl_t *vpt, bool subcaptures)
4767{
4768 ssize_t slen;
4769 char *unescaped = vpt->data.unescaped;
4770
4771 if (tmpl_is_regex_xlat(vpt) || tmpl_is_regex(vpt)) return 0; /* Don't need compiling */
4772
4774
4775 slen = regex_compile(vpt, &vpt->data.reg.ex,
4776 unescaped, talloc_strlen(unescaped),
4777 &vpt->data.reg_flags, subcaptures, vpt->rules.at_runtime);
4778 if (slen <= 0) return vpt->quote != T_BARE_WORD ? slen - 1 : slen; /* Account for the quoting */
4779
4780 vpt->type = TMPL_TYPE_REGEX;
4781 vpt->data.reg.src = unescaped; /* Keep this around for debugging and copying */
4782 vpt->data.reg.subcaptures = subcaptures;
4783
4785
4786 return slen;
4787}
4788#endif
4789/** @} */
4790
4791/** @name Print the contents of a #tmpl_t
4792 * @{
4793 */
4795{
4796 fr_sbuff_t our_out = FR_SBUFF(out);
4797 tmpl_request_t *rr = tmpl_request_list_head(rql);
4798
4799 /*
4800 * Print request references
4801 */
4802 while (rr) {
4804 rr = tmpl_request_list_next(rql, rr);
4805 if (rr) FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4806 }
4807
4808 FR_SBUFF_SET_RETURN(out, &our_out);
4809}
4810
4811/** Print an attribute or list #tmpl_t to a string
4812 *
4813 * This function is the direct counterpart to #tmpl_afrom_attr_substr.
4814 *
4815 * @param[in] out Where to write the presentation format #tmpl_t string.
4816 * @param[in] vpt to print.
4817 * @return
4818 * - >0 the number of bytes written to the out buffer.
4819 * - 0 invalid argument.
4820 * - <0 the number of bytes we would have needed to complete the print.
4821 */
4823{
4824 tmpl_attr_t *ar = NULL;
4826 fr_sbuff_t our_out = FR_SBUFF(out);
4827 fr_slen_t slen;
4828
4830
4831 /*
4832 * Only print things we can print...
4833 */
4834 switch (vpt->type) {
4836 case TMPL_TYPE_ATTR:
4837 break;
4838
4839 default:
4840 fr_assert(0);
4841 return 0;
4842 }
4843
4844 /*
4845 * Print request references
4846 */
4847 slen = tmpl_request_ref_list_print(&our_out, &vpt->data.attribute.rr);
4848 if (slen > 0) FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4849 if (slen < 0) return slen;
4850
4851 /*
4852 *
4853 * If the leaf attribute is unknown and raw we
4854 * add the raw. prefix.
4855 *
4856 * If the leaf attribute is unknown and not raw
4857 * we add the .unknown prefix.
4858 *
4859 */
4861
4862 /*
4863 * Print attribute identifiers
4864 */
4865 ar = NULL;
4866 while ((ar = tmpl_attr_list_next(tmpl_attr(vpt), ar))) {
4867 switch(ar->type) {
4869 break;
4870
4873 {
4874 int i, depth = 0;
4875
4876 fr_assert(ar->ar_parent); /* All normal and unknown attributes must have parents */
4877
4878 fr_proto_da_stack_build_partial(&stack, ar->ar_parent, ar->ar_da);
4879
4880 /*
4881 * First component in the list has everything built
4882 */
4883 if (ar == tmpl_attr_list_head(tmpl_attr(vpt))) {
4884 depth = ar->ar_parent->depth - 1; /* Adjust for array index */
4885 /*
4886 * Everything else skips the first component
4887 */
4888 } else {
4889 depth = ar->ar_parent->depth;
4890 }
4891
4892 /*
4893 * Root attributes will be skipped by the build
4894 * function, so da[0] contains the attribute
4895 * we're looking for.
4896 */
4897 if (depth < 0) depth = 0;
4898
4899 /*
4900 * Print from our parent depth to the AR we're processing
4901 *
4902 * For refs we skip the attribute pointed to be the ref
4903 * and just print its children.
4904 *
4905 * In addition skip printing "request." in most cases.
4906 */
4907 if ((stack.da[depth] == request_attr_request) && tmpl_attr_list_next(tmpl_attr(vpt), ar) &&
4908 (ar->filter.type == TMPL_ATTR_FILTER_TYPE_NONE)) continue;
4909
4910 for (i = depth; (unsigned int)i < ar->ar_da->depth; i++) {
4911 FR_SBUFF_IN_STRCPY_RETURN(&our_out, stack.da[i]->name);
4912
4913 /*
4914 * Print intermediary separators
4915 * if necessary.
4916 */
4917 if (((unsigned int)i + 1) < ar->ar_da->depth) FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4918 }
4919 }
4920 break;
4921
4922 /*
4923 * For unresolved attribute we print the raw identifier we
4924 * got when parsing the tmpl.
4925 */
4927 {
4928 unsigned int i, depth;
4929
4930 /*
4931 * This is the first unresolved component in a potential
4932 * chain of unresolved components. Print the path up to
4933 * the last known parent.
4934 */
4935 if (ar->ar_parent && !ar->ar_parent->flags.is_root) {
4936 fr_proto_da_stack_build_partial(&stack, ar->ar_parent, ar->ar_parent);
4937 if (ar->ar_parent->flags.is_root) {
4938 depth = 0;
4939 } else {
4940 depth = ar->ar_parent->depth - 1;
4941 }
4942
4943 for (i = depth; i < ar->ar_parent->depth; i++) {
4944 FR_SBUFF_IN_STRCPY_RETURN(&our_out, stack.da[i]->name);
4945 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4946 }
4947 }
4948 /*
4949 * Then print the unresolved component
4950 */
4951 FR_SBUFF_IN_BSTRCPY_BUFFER_RETURN(&our_out, ar->ar_unresolved);
4952 break;
4953 }
4954 }
4955
4956 if (ar_filter_is_none(ar)) {
4957 /* do nothing */
4958
4959 } else if (ar_filter_is_num(ar)) {
4960 switch (ar->ar_num) {
4961 case NUM_UNSPEC:
4962 break;
4963
4964 case NUM_ALL:
4965 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "[*]");
4966 break;
4967
4968 case NUM_COUNT:
4969 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "[#]");
4970 break;
4971
4972 case NUM_LAST:
4973 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "[n]");
4974 break;
4975
4976 default:
4977 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "[%i]", ar->ar_num);
4978 break;
4979 }
4980
4981 } else if (ar_filter_is_cond(ar)) {
4982 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "[");
4983 (void) xlat_print(&our_out, ar->ar_cond, NULL);
4984 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "]");
4985
4986 } else {
4987 fr_assert(0);
4988 }
4989
4990 if (tmpl_attr_list_next(tmpl_attr(vpt), ar)) FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
4991 }
4992 FR_SBUFF_SET_RETURN(out, &our_out);
4993}
4994
4995/** Print a #tmpl_t to a string
4996 *
4997 * This function should primarily be used for regenerating vpt->name when the contents
4998 * of the #tmpl_t is changed programmatically, or when the #tmpl_t is being serialized
4999 * in some non-standard way, i.e. as a value for a field in a database.
5000 *
5001 * This function is the direct counterpart to #tmpl_afrom_substr.
5002 *
5003 * @note Does not print flags for regular expressions, as the quoting char is needed
5004 * to separate the elements of the expression.
5005 * Call regex_flags_print to write the flags values to the output buffer.
5006 *
5007 * @param[out] out Where to write the presentation format #tmpl_t string.
5008 * @param[in] vpt to print.
5009 * @param[in] e_rules Escaping rules used to print strings.
5010 * @return
5011 * - >0 the number of bytes written to the out buffer.
5012 * - 0 invalid argument.
5013 * - <0 the number of bytes we would have needed to complete the print.
5014 */
5016 fr_sbuff_escape_rules_t const *e_rules)
5017{
5018 fr_sbuff_t our_out = FR_SBUFF(out);
5019
5021
5022 switch (vpt->type) {
5024 case TMPL_TYPE_ATTR:
5026 break;
5027
5028 case TMPL_TYPE_DATA:
5029 FR_SBUFF_RETURN(fr_value_box_print, &our_out, tmpl_value(vpt), e_rules);
5030 break;
5031
5032 case TMPL_TYPE_REGEX:
5033 FR_SBUFF_IN_BSTRNCPY_RETURN(&our_out, vpt->name, vpt->len); /* Fixme - double escapes */
5034 break;
5035
5037 FR_SBUFF_IN_ESCAPE_BUFFER_RETURN(&our_out, vpt->data.unescaped, e_rules);
5038 break;
5039
5041 case TMPL_TYPE_MAX:
5042 fr_sbuff_terminate(out);
5043 break;
5044
5045 /*
5046 * The remaining types will either
5047 * be xlat expansions, or need
5048 * resolving, in which case the
5049 * unescaped string is available
5050 * in vpt->unescaped.
5051 */
5052 default:
5053 if (tmpl_contains_xlat(vpt)) {
5054 FR_SBUFF_RETURN(xlat_print, &our_out, tmpl_xlat(vpt), e_rules);
5055 break;
5056 }
5057
5059 FR_SBUFF_IN_ESCAPE_BUFFER_RETURN(&our_out, vpt->data.unescaped, e_rules);
5060 break;
5061 }
5062
5063 fr_assert_fail("Can't print invalid tmpl type %s", tmpl_type_to_str(vpt->type));
5064
5065 /*
5066 * Ensure we do something sane for non-debug builds
5067 */
5068 fr_sbuff_terminate(out);
5069 return 0;
5070 }
5071
5072 FR_SBUFF_SET_RETURN(out, &our_out);
5073}
5074
5075/** Print a #tmpl_t to a string with quotes
5076 *
5077 * This function should be used when the tmpl is embedded in some other construct
5078 * in the server's configuration.
5079 *
5080 * It adds standard quoting around tmpl's used as operands in expressions and applies
5081 * the correct escaping rules.
5082 *
5083 * @param[out] out Where to write the presentation format #tmpl_t string.
5084 * @param[in] vpt to print.
5085 * @return
5086 * - >0 the number of bytes written to the out buffer.
5087 * - 0 invalid argument.
5088 * - <0 the number of bytes we would have needed to complete the print.
5089 */
5091{
5092 fr_sbuff_t our_out = FR_SBUFF(out);
5093
5094 char quote = fr_token_quote[vpt->quote];
5095
5096 if (quote != '\0') FR_SBUFF_IN_CHAR_RETURN(&our_out, quote);
5097 FR_SBUFF_RETURN(tmpl_print, &our_out, vpt,
5099 if (quote != '\0') FR_SBUFF_IN_CHAR_RETURN(&our_out, quote);
5100
5101 /*
5102 * Optionally print the flags
5103 */
5104 if (vpt->type & TMPL_FLAG_REGEX) FR_SBUFF_RETURN(regex_flags_print, &our_out, tmpl_regex_flags(vpt));
5105
5106 FR_SBUFF_SET_RETURN(out, &our_out);
5107}
5108/** @} */
5109
5110
5111#ifdef WITH_VERIFY_PTR
5112/** Used to check whether areas of a tmpl_t are zeroed out
5113 *
5114 * @param ptr Offset to begin checking at.
5115 * @param len How many bytes to check.
5116 * @return
5117 * - Pointer to the first non-zero byte.
5118 * - NULL if all bytes were zero.
5119 */
5120static uint8_t const *is_zeroed(uint8_t const *ptr, size_t len)
5121{
5122 size_t i;
5123
5124 for (i = 0; i < len; i++) {
5125 if (ptr[i] != 0x00) return ptr + i;
5126 }
5127
5128 return NULL;
5129}
5130
5131/** Verify that unused regions of the struct are zeroed out
5132 *
5133 */
5134#define CHECK_ZEROED(_vpt, _field) is_zeroed(((uint8_t const *)&(_vpt)->data) + sizeof((_vpt)->data._field), sizeof((_vpt)->data) - sizeof((_vpt)->data._field))
5135
5136
5137/** Print hex data
5138 *
5139 */
5140#define PRINT_NON_ZEROED(_vpt, _field, _nz_ptr) \
5141do { \
5142 DEBUG("Expected live portion %p-%p (0-%zu)", \
5143 _vpt, \
5144 (uint8_t const *)&(_vpt)->data + sizeof((_vpt)->data._field), \
5145 sizeof((_vpt)->data._field)); \
5146 DEBUG("Expected zero portion %p-%p (%zu-%zu)", \
5147 (uint8_t const *)&(_vpt)->data + sizeof((_vpt)->data._field), \
5148 (uint8_t const *)&(_vpt)->data + sizeof((_vpt)->data), \
5149 sizeof((_vpt)->data._field), sizeof((_vpt)->data)); \
5150 HEX_MARKER1((uint8_t const *)&vpt->data, sizeof(vpt->data), nz - (uint8_t const *)&vpt->data, "non-zero memory", ""); \
5151} while (0)
5152
5153
5154/** Verify the attribute reference in a tmpl_t make sense
5155 *
5156 * @note If the attribute reference is invalid, causes the server to exit.
5157 *
5158 * @param file obtained with __FILE__.
5159 * @param line obtained with __LINE__.
5160 * @param vpt to check.
5161 */
5162void tmpl_attr_verify(char const *file, int line, tmpl_t const *vpt)
5163{
5164 tmpl_attr_t *ar = NULL;
5165 tmpl_attr_t *slow = NULL, *fast = NULL;
5166 tmpl_attr_t *seen_unknown = NULL;
5167 tmpl_attr_t *seen_unresolved = NULL;
5168
5170
5171 /*
5172 * Loop detection
5173 */
5174 while ((slow = tmpl_attr_list_next(tmpl_attr(vpt), slow)) &&
5175 (fast = tmpl_attr_list_next(tmpl_attr(vpt), fast))) {
5176
5177 /*
5178 * Advances twice as fast as slow...
5179 */
5180 fast = tmpl_attr_list_next(tmpl_attr(vpt), fast);
5181 fr_fatal_assert_msg(fast != slow,
5182 "CONSISTENCY CHECK FAILED %s[%u]: Looping reference list found. "
5183 "Fast pointer hit slow pointer at \"%s\"",
5184 file, line,
5185 slow->type == TMPL_ATTR_TYPE_UNRESOLVED ? slow->ar_unresolved :
5186 slow->da ? slow->da->name : "(null-attr)");
5187 }
5188
5189 /*
5190 * Lineage type check
5191 *
5192 * Known attribute cannot come after unresolved or unknown attributes
5193 * Unknown attributes cannot come after unresolved attributes
5194 */
5195 if (!tmpl_is_list(vpt)) while ((ar = tmpl_attr_list_next(tmpl_attr(vpt), ar))) {
5196 switch (ar->type) {
5198 if (seen_unknown) {
5199 tmpl_attr_debug(stderr, vpt);
5200 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5201 "TMPL_TYPE_ATTR known attribute \"%s\" "
5202 "occurred after unknown attribute %s "
5203 "in attr ref list",
5204 file, line,
5205 ar->da->name,
5206 ar->unknown.da->name);
5207 }
5208 if (seen_unresolved) {
5209 tmpl_attr_debug(stderr, vpt);
5210 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5211 "TMPL_TYPE_ATTR known attribute \"%s\" "
5212 "occurred after unresolved attribute \"%s\""
5213 "in attr ref list",
5214 file, line,
5215 ar->da->name,
5216 ar->ar_unresolved);
5217 }
5218 fr_fatal_assert_msg(ar->ar_parent,
5219 "CONSISTENCY CHECK FAILED %s[%u]: attr ref missing parent",
5220 file, line);
5221
5222 if (ar->ar_parent->type != FR_TYPE_GROUP) {
5223 fr_fatal_assert_msg(ar->ar_parent == ar->ar_da->parent,
5224 "CONSISTENCY CHECK FAILED %s[%u]: attr ref has wrong parent: "
5225 "Expected %s, got %s",
5226 file, line,
5227 ar->ar_da->parent->name,
5228 ar->ar_parent->name);
5229
5230 }
5231 break;
5232
5234 if (seen_unknown) {
5235 tmpl_attr_debug(stderr, vpt);
5236 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5237 "TMPL_TYPE_ATTR unspecified attribute "
5238 "occurred after unknown attribute %s "
5239 "in attr ref list",
5240 file, line,
5241 ar->unknown.da->name);
5242 }
5243 if (seen_unresolved) {
5244 tmpl_attr_debug(stderr, vpt);
5245 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5246 "TMPL_TYPE_ATTR unspecified attribute "
5247 "occurred after unresolved attribute \"%s\""
5248 "in attr ref list",
5249 file, line,
5250 ar->ar_unresolved);
5251 }
5252 break;
5253
5255 seen_unresolved = ar;
5256 fr_fatal_assert_msg(ar->ar_unresolved_namespace,
5257 "CONSISTENCY CHECK FAILED %s[%u]: unresolved attr ref missing namespace",
5258 file, line);
5259 break;
5260
5262 seen_unknown = ar;
5263 if (seen_unresolved) {
5264 tmpl_attr_debug(stderr, vpt);
5265 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: "
5266 "TMPL_TYPE_ATTR unknown attribute \"%s\" "
5267 "occurred after unresolved attribute %s "
5268 "in attr ref list",
5269 file, line, ar->da->name,
5270 ar->ar_unresolved);
5271 }
5272 break;
5273 }
5274 }
5275}
5276
5277/** Verify fields of a tmpl_t make sense
5278 *
5279 * @note If the #tmpl_t is invalid, causes the server to exit.
5280 *
5281 * @param file obtained with __FILE__.
5282 * @param line obtained with __LINE__.
5283 * @param vpt to check.
5284 */
5285void tmpl_verify(char const *file, int line, tmpl_t const *vpt)
5286{
5287 uint8_t const *nz;
5288
5289 fr_assert(vpt);
5290
5292 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: tmpl_t type was "
5293 "TMPL_TYPE_UNINITIALISED (uninitialised)", file, line);
5294 }
5295
5296 if (vpt->type >= TMPL_TYPE_MAX) {
5297 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: tmpl_t type was %i "
5298 "(outside range of tmpl_type_table)", file, line, vpt->type);
5299 }
5300
5301 if (!vpt->name && (vpt->quote != T_INVALID)) {
5302 char quote = vpt->quote >= T_TOKEN_LAST ? '?' : fr_token_quote[vpt->quote];
5303
5304 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: Quote type '%c' (%i) was set for NULL name",
5305 file, line, quote, vpt->quote);
5306 }
5307
5308 if (vpt->name && (vpt->quote == T_INVALID)) {
5309 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: No quoting type was set for name \"%.*s\"",
5310 file, line, (int)vpt->len, vpt->name);
5311 }
5312
5313 /*
5314 * Do a memcmp of the bytes after where the space allocated for
5315 * the union member should have ended and the end of the union.
5316 * These should always be zero if the union has been initialised
5317 * properly.
5318 *
5319 * If they're still all zero, do TMPL_TYPE specific checks.
5320 */
5321 switch (vpt->type) {
5323 if (!vpt->data.unescaped) {
5324 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA_UNRESOLVED "
5325 "unescaped field is NULL", file, line);
5326 }
5327 break;
5328
5330 if (!vpt->data.xlat.ex) {
5331 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT "
5332 "has a NULL xlat.ex field", file, line);
5333
5334 }
5335
5336 if (!xlat_needs_resolving(vpt->data.xlat.ex)) {
5337 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT_UNRESOLVED "
5338 "does not have 'needs resolving' flag set", file, line);
5339 }
5340 break;
5341
5342 case TMPL_TYPE_XLAT:
5343 if (!vpt->data.xlat.ex) {
5344 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT "
5345 "has a NULL xlat.ex field", file, line);
5346
5347 }
5349 break;
5350
5351/* @todo When regexes get converted to xlat the flags field of the regex union is used
5352 case TMPL_TYPE_XLAT_UNRESOLVED:
5353 if (is_zeroed((uint8_t const *)&vpt->data, sizeof(vpt->data))) {
5354 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT_UNRESOLVED "
5355 "has non-zero bytes in its data union", file, line);
5356 }
5357 break;
5358
5359 case TMPL_TYPE_XLAT:
5360 if (CHECK_ZEROED(vpt, xlat)) {
5361 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_XLAT "
5362 "has non-zero bytes after the data.xlat pointer in the union", file, line);
5363 }
5364 break;
5365*/
5366
5367 case TMPL_TYPE_EXEC:
5369 break;
5370
5372 /* tmpl_xlat(vpt) can be initialized */
5373 break;
5374
5376 if ((tmpl_attr_list_num_elements(tmpl_attr(vpt)) > 0) &&
5377 ((tmpl_attr_t *)tmpl_attr_list_tail(tmpl_attr(vpt)))->da) {
5378#ifndef NDEBUG
5379 tmpl_attr_debug(stderr, vpt);
5380#endif
5381 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR_UNRESOLVED contains %u "
5382 "references", file, line, tmpl_attr_list_num_elements(tmpl_attr(vpt)));
5383 }
5384 break;
5385
5386 case TMPL_TYPE_ATTR:
5387 if ((nz = CHECK_ZEROED(vpt, attribute))) {
5388 PRINT_NON_ZEROED(vpt, attribute, nz);
5389 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR "
5390 "has non-zero bytes after the data.attribute struct in the union",
5391 file, line);
5392 }
5393
5395 fr_assert(vpt->rules.cast == FR_TYPE_NULL);
5396 break;
5397 }
5398
5401 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR "
5402 "da is marked as unknown, but address is not equal to the template's "
5403 "unknown da pointer", file, line);
5404 }
5405 /*
5406 * Raw attributes may not have been added to the dictionary yet
5407 */
5408 } else {
5409 fr_dict_attr_t const *da;
5410 fr_dict_t const *dict;
5411
5412 /*
5413 * Attribute may be present with multiple names
5414 */
5416 if (!dict) {
5417 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR "
5418 "attribute \"%s\" (%s) not rooted in a dictionary",
5419 file, line, tmpl_attr_tail_da(vpt)->name,
5421 }
5422
5423 da = tmpl_attr_tail_da(vpt);
5424 if (!tmpl_attr_tail_is_raw(vpt) && (da != tmpl_attr_tail_da(vpt))) {
5425 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_ATTR "
5426 "dictionary pointer %p \"%s\" (%s) "
5427 "and global dictionary pointer %p \"%s\" (%s) differ",
5428 file, line,
5431 da, da->name,
5432 fr_type_to_str(da->type));
5433 }
5434
5435 tmpl_attr_verify(file, line, vpt);
5436 }
5437 break;
5438
5439 case TMPL_TYPE_DATA:
5440 if ((nz = CHECK_ZEROED(vpt, literal))) {
5441 PRINT_NON_ZEROED(vpt, literal, nz);
5442 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA "
5443 "has non-zero bytes after the data.literal struct in the union",
5444 file, line);
5445 }
5446
5447 /*
5448 * An FR_TYPE_NULL box inside a TMPL_TYPE_DATA used to
5449 * fire here as the "you forgot to init the box" signal,
5450 * but the `null` keyword (see tmpl_afrom_null_substr)
5451 * deliberately constructs one. Accept it.
5452 */
5454 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA type was "
5455 "%i (outside the range of fr_type_ts)", file, line, tmpl_value_type(vpt));
5456 }
5457 /*
5458 * Unlike fr_pair_ts we can't guarantee that fr_pair_t_TMPL buffers will
5459 * be talloced. They may be allocated on the stack or in global variables.
5460 */
5461 switch (tmpl_value_type(vpt)) {
5462 case FR_TYPE_STRING:
5464 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA char buffer not \\0 "
5465 "terminated", file, line);
5466 }
5467 break;
5468
5469 case FR_TYPE_STRUCTURAL:
5470 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_DATA is of type TLV",
5471 file, line);
5472
5473 default:
5474 break;
5475 }
5476
5478 break;
5479
5481#ifdef HAVE_REGEX
5483 break;
5484#endif
5485
5486
5489#ifndef HAVE_REGEX
5490 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_REGEX_XLAT_UNRESOLVED - No regex support",
5491 file, line);
5492#endif
5493 break;
5494
5495 case TMPL_TYPE_REGEX:
5496#ifdef HAVE_REGEX
5497 if (tmpl_regex(vpt) == NULL) {
5498 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_REGEX "
5499 "reg.ex field was NULL", file, line);
5500 }
5501#else
5502 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_REGEX - No regex support",
5503 file, line);
5504#endif
5505 break;
5506
5508 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_UNINITIALISED", file, line);
5509
5510 case TMPL_TYPE_MAX:
5511 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%u]: TMPL_TYPE_MAX", file, line);
5512 }
5513}
5514#endif
5515
5516static const bool array_terminal[SBUFF_CHAR_CLASS] = {
5517 [ ']' ] = true,
5518};
5519
5520#define return_P(_x) fr_strerror_const(_x);goto return_p
5521
5522#define is_char(_offset, _x) (((p + _offset) < end) && (p[_offset] == _x))
5523
5524/** Preparse a string in preparation for passing it to tmpl_afrom_substr()
5525 *
5526 * Note that the input string is not modified, which means that the
5527 * tmpl_afrom_substr() function MUST un-escape it.
5528 *
5529 * The caller should pass 'out' and 'outlen' to tmpl_afrom_substr()
5530 * as 'in' and 'inlen'. The caller should also pass 'type'.
5531 * The caller should also pass do_unescape=true.
5532 *
5533 * @param[out] out start of the string to parse
5534 * @param[out] outlen length of the string to parse
5535 * @param in where we start looking for the string
5536 * @param inlen length of the input string
5537 * @param[out] type token type of the string.
5538 * @return
5539 * - > 0, amount of parsed string to skip, to get to the next token
5540 * - <=0, -offset in 'start' where the parse error was located
5541 */
5542ssize_t tmpl_preparse(char const **out, size_t *outlen, char const *in, size_t inlen,
5544{
5545 char const *p = in, *end = in + inlen;
5546 char quote;
5547 char close;
5548 int depth;
5549 bool triple;
5550
5551 *type = T_INVALID;
5552
5553 while (isspace((uint8_t) *p) && (p < end)) p++;
5554 if (p >= end) return p - in;
5555
5556 switch (*p) {
5557 /*
5558 * Allow bare xlat's
5559 */
5560 case '%':
5561 if (p[1] != '{') {
5562 char const *q;
5563
5564 q = p + 1;
5565
5566 /*
5567 * Function syntax: %foo(...)
5568 */
5569 while ((q < end) && (isalnum((int) *q) || (*q == '.') || (*q == '_') || (*q == '-'))) {
5570 q++;
5571 }
5572
5573 if (*q != '(') {
5574 p++;
5575 fr_strerror_const("Invalid character after '%'");
5576 return_p:
5577 return -(p - in);
5578 }
5579
5580 /*
5581 * Return the whole %foo(...) string.
5582 */
5583 *out = p;
5584 if (*type == T_INVALID) *type = T_BARE_WORD;
5585 close = ')';
5586
5587 p = q + 1;
5588 depth = 1;
5589 goto loop;
5590 }
5591
5592 /*
5593 * For now, %{...} is treated as a double-quoted
5594 * string. Once we clean other things up, the
5595 * xlats will be treated as strongly typed values
5596 * / lists on their own.
5597 */
5598 if (*type == T_INVALID) *type = T_BARE_WORD;
5599 depth = 0;
5600 close = '}';
5601
5602 /*
5603 * Xlat's are quoted by %{...} / %(...) nesting, not by
5604 * escapes, so we need to do special escaping.
5605 */
5606 *out = p;
5607 loop:
5608 while (*p) {
5609 /*
5610 * End of expansion. Return the entire
5611 * expansion, including the enclosing %{}
5612 * characters.
5613 */
5614 if ((*p == '}') || (*p == ')')) {
5615 bool match = (*p == close);
5616
5617 p++;
5618 depth--;
5619
5620 if (depth == 0) {
5621 if (!match) break;
5622
5623 *outlen = p - (*out);
5624 return p - in;
5625 }
5626 continue;
5627 }
5628
5629 if (*p == '\\') {
5630 if (is_char(1, '\0')) {
5631 return_P("End of string after escape");
5632 }
5633 p += 2;
5634 continue;
5635 }
5636
5637 if ((p[0] == '%') && (is_char(1, '{') || is_char(1, '('))) {
5638 if (is_char(2, '\0')) {
5639 return_P("End of string after expansion");
5640 }
5641
5642 p += 2;
5643 depth++;
5644 continue;
5645 }
5646
5647 /*
5648 * Allow (...) and {...}
5649 */
5650 if ((*p == '{') || (*p == '(')) {
5651 p++;
5652 depth++;
5653 continue;
5654 }
5655
5656 p++;
5657 }
5658
5659 /*
5660 * End of input without end of string.
5661 * Point the error to the start of the string.
5662 */
5663 p = *out;
5664 return_P("Unterminated expansion");
5665
5666 case '/':
5667 goto bare_word;
5668
5669 case '\'':
5670 quote = *(p++);
5672 goto skip_string;
5673
5674 case '`':
5675 quote = *(p++);
5677 goto skip_string;
5678
5679 case '"':
5680 quote = *(p++);
5682
5683 /*
5684 * We're not trying to do a *correct* parsing of
5685 * every string here. We're trying to do a
5686 * simple parse that isn't wrong. We therefore
5687 * accept most anything that's vaguely well
5688 * formed, and rely on the next stage to do a
5689 * more rigorous check.
5690 */
5691 skip_string:
5692 if (is_char(0, quote) && is_char(1, quote)) {
5693 triple = true;
5694 p += 2;
5695 } else {
5696 triple = false;
5697 }
5698 *out = p;
5699
5700 while (*p) {
5701 if (p >= end) goto unterminated;
5702
5703 /*
5704 * End of string. Tell the caller the
5705 * length of the data inside of the
5706 * string, and return the number of
5707 * characters to skip.
5708 */
5709 if (*p == quote) {
5710 if (!triple) {
5711 *outlen = p - (*out);
5712 p++;
5713 return p - in;
5714
5715 }
5716
5717
5718 if (is_char(1, quote) && is_char(2, quote)) {
5719 *outlen = p - (*out);
5720 p += 3;
5721 return p - in;
5722 }
5723
5724 p++;
5725 continue;
5726 }
5727
5728 if (*p == '\\') {
5729 if (is_char(1, '\0')) {
5730 return_P("End of string after escape");
5731 }
5732 p++;
5733 }
5734 p++;
5735 }
5736
5737 /*
5738 * End of input without end of string.
5739 * Point the error to the start of the string.
5740 */
5741 unterminated:
5742 p = *out;
5743 return_P("Unterminated string");
5744
5745 case '&':
5746 *out = p; /* the output string starts with '&' */
5747 p++;
5748 quote = '[';
5749 goto skip_word;
5750
5751 default:
5752 bare_word:
5753 *out = p;
5754 quote = '['; /* foo[1] is OK */
5755
5756 skip_word:
5757 *type = T_BARE_WORD;
5758 depth = 0;
5759
5760 /*
5761 * Allow *most* things. But stop on spaces and special characters.
5762 */
5763 while (*p) {
5764 if (isspace((uint8_t) *p)) {
5765 break;
5766 }
5767
5768 if (*p == '$') {
5769 if (p[1] == '{') {
5770 p += 2;
5771 depth++;
5772 continue;
5773
5774 } else if ((p[1] >= 'A') && (p[1] <= 'Z')) {
5775 p++;
5776 while ((*p >= 'A') && (*p <= 'Z')) {
5777 p++;
5778 }
5779
5780 if (*p != '{') {
5781 return_P("Missing '{'");
5782 }
5783
5784 depth++;
5785 continue;
5786
5787 } else {
5788 /*
5789 * Bare '$' is wrong...
5790 */
5791 break;
5792 }
5793 }
5794
5795 if (*p == '%') {
5796 if (p[1] == '{') {
5797 p += 2;
5798 depth++;
5799 continue;
5800 }
5801
5802 p++;
5803 continue;
5804 }
5805
5806 /*
5807 * If we're inside of a ${...} expansion,
5808 * then allow everything until the
5809 * closing '}'. This means that we can
5810 * do ${foo[bar].baz}, among other
5811 * thingds.
5812 */
5813 if (depth > 0) {
5814 if (*p == '}') {
5815 depth--;
5816 }
5817
5818 p++;
5819 continue;
5820 }
5821
5822 /*
5823 * '-' is special. We allow it for
5824 * attribute names, BUT it's a
5825 * terminating token if the NEXT
5826 * character is '='.
5827 *
5828 * We have the same criteria for IPv6
5829 * addresses and tagged attributes. ':'
5830 * is allowed, but ':=' is a breaking
5831 * token.
5832 */
5833 if ((*p == '-') || (*p == ':')) {
5834 if (p[1] == '=') break;
5835 p++;
5836 continue;
5837 }
5838
5839 /*
5840 * Allowed in attribute names, and/or
5841 * host names and IP addresses, and IPv6 addresses.
5842 */
5843 if ((*p == '.') || (*p == '/') || (*p == '_') || (*p == '*') ||
5844 (*p == ']') || (*p == '@')) {
5845 p++;
5846 continue;
5847 }
5848
5849 /*
5850 * [...] is an IPv6 address.
5851 */
5852 if ((p == in) && (*p == '[')) {
5853 p++;
5854 continue;
5855 }
5856
5857 /*
5858 * Allow letters and numbers
5859 */
5860 if (((*p >= 'a') && (*p <= 'z')) ||
5861 ((*p >= 'A') && (*p <= 'Z')) ||
5862 ((*p >= '0') && (*p <= '9'))) {
5863 p++;
5864 continue;
5865 }
5866
5867 /*
5868 * Allow UTF-8 sequences.
5869 */
5870 if (*(uint8_t const *)p > 0x80) {
5871 p++;
5872 continue;
5873 }
5874
5875 /*
5876 * If it's an attribute reference, allow
5877 * a few more things inside of a "[...]"
5878 * block.
5879 */
5880 if (*p == '[') {
5881 if (quote != '[') {
5882 return_P("Invalid location for '['");
5883 }
5884
5885 p++;
5886
5887 /*
5888 * Allow [#], etc. But stop
5889 * immediately after the ']'.
5890 */
5891 if ((*p == '#') || (*p == '*') || (*p == 'n')) {
5892 p++;
5893
5894 } else {
5895 ssize_t slen;
5896 bool eol = false;
5897
5898 slen = fr_skip_condition(p, end, array_terminal, &eol);
5899 if (slen < 0) {
5900 p += -slen;
5901 return -(p - in);
5902 }
5903 p += slen;
5904 continue;
5905 }
5906
5907 if (*p == ']') {
5908 p++;
5909 continue;
5910 }
5911 }
5912
5913 /*
5914 * Everything else is a breaking token
5915 */
5916 break;
5917 }
5918
5919 /*
5920 * Give some slightly better error messages.
5921 */
5922 if (*p == '\\') {
5923 return_P("Unexpected escape");
5924 }
5925
5926 if ((*p == '"') || (*p == '\'') || (*p == '`')) {
5927 return_P("Unexpected start of string");
5928 }
5929
5930 if (p == *out) {
5931 return_P("Empty string is invalid");
5932 }
5933
5934 *outlen = p - (*out);
5935 break;
5936 }
5937
5938 return p - in;
5939}
5940
5941/** Return whether or not async is required for this tmpl.
5942 *
5943 * If the tmpl is needs_async, then it is async
5944 * If the tmpl is not needs_async, then it will not yield
5945 *
5946 * If the tmpl yields, then async is required.
5947 */
5949{
5950 switch (vpt->type) {
5951 case TMPL_TYPE_EXEC: /* we don't have "exec no-wait" here */
5952 case TMPL_TYPE_XLAT_UNRESOLVED: /* we have no idea, so be safe */
5953#ifndef HAVE_REGEX
5955#endif
5956 return true;
5957
5958#ifndef HAVE_REGEX
5960#endif
5961 case TMPL_TYPE_XLAT: /* synchronous xlats use unlang_interpret_synchronous() */
5962 default:
5963 return false;
5964 }
5965}
5966
5967/** Initialize a set of rules from a parent set of rules, and a parsed tmpl_t
5968 *
5969 */
5971{
5972 fr_dict_attr_t const *da;
5973 fr_dict_attr_t const *ref;
5974 fr_dict_t const *dict, *internal;
5975
5976 *out = *parent;
5977 /* don't set ->parent=parent, that is only for switching subrequest, etc. */
5978
5979 if (!tmpl_is_attr(vpt)) return;
5980
5981 da = tmpl_attr_tail_da(vpt);
5982
5983 /*
5984 * The input tmpl is a leaf. We must parse the child as
5985 * a normal attribute reference (as with the parent tmpl).
5986 */
5987 if (!fr_type_structural[da->type]) {
5988 return;
5989 }
5990
5991 if (vpt->rules.attr.request_def) {
5992 tmpl_request_ref_list_acopy(ctx, &out->attr.request_def, vpt->rules.attr.request_def);
5993 }
5994 out->attr.list_def = tmpl_list(vpt);
5995
5996 /*
5997 * Parse the child attributes in the context of the parent struct / tlv / whatever.
5998 */
5999 if (da->type != FR_TYPE_GROUP) {
6000 out->attr.dict_def = fr_dict_by_da(da);
6001 out->attr.namespace = da;
6002 return;
6003 }
6004
6005 ref = fr_dict_attr_ref(da);
6006 dict = fr_dict_by_da(ref);
6007 internal = fr_dict_internal();
6008
6009 /*
6010 * Groups MAY change dictionaries. If so, then swap the dictionary and the parent.
6011 */
6012 if ((dict != internal) && (dict != out->attr.dict_def)) {
6013 out->attr.dict_def = dict;
6014 out->attr.namespace = ref;
6015 }
6016
6017 /*
6018 * Otherwise the reference is swapping FROM a protocol
6019 * dictionary TO the internal dictionary, and TO an
6020 * internal group. We fall back to leaving well enough
6021 * alone, and leave things as-is. This allows internal
6022 * grouping attributes to appear anywhere.
6023 */
6024}
6025
6026static void tmpl_attr_rules_debug(tmpl_attr_rules_t const *at_rules)
6027{
6028 FR_FAULT_LOG("\tdict_def = %s", at_rules->dict_def ? fr_dict_root(at_rules->dict_def)->name : "");
6029 FR_FAULT_LOG("\tnamespace = %s", at_rules->namespace ? at_rules->namespace->name : "");
6030
6031 FR_FAULT_LOG("\tlist_def = %s", at_rules->list_def ? at_rules->list_def->name : "");
6032
6033 FR_FAULT_LOG("\tallow_unknown = %u", at_rules->allow_unknown);
6034 FR_FAULT_LOG("\tallow_unresolved = %u", at_rules->allow_unresolved);
6035 FR_FAULT_LOG("\tallow_wildcard = %u", at_rules->allow_wildcard);
6036 FR_FAULT_LOG("\tallow_foreign = %u", at_rules->allow_foreign);
6037 FR_FAULT_LOG("\tdisallow_filters = %u", at_rules->disallow_filters);
6038}
6039
6040
6042{
6043 FR_FAULT_LOG("\tparent = %p", rules->parent);
6044 FR_FAULT_LOG(" attr {");
6045 tmpl_attr_rules_debug(&rules->attr);
6046 FR_FAULT_LOG(" }");
6047 FR_FAULT_LOG("\tenumv = %s", rules->enumv ? rules->enumv->name : "");
6048 FR_FAULT_LOG("\tcast = %s", fr_type_to_str(rules->cast));
6049 FR_FAULT_LOG("\tat_runtime = %u", rules->at_runtime);
6050 FR_FAULT_LOG("\tliterals_safe_for = %lx", rules->literals_safe_for);
6051
6052}
static int const char char buffer[256]
Definition acutest.h:576
va_end(args)
static int const char * fmt
Definition acutest.h:573
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:560
#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:391
#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:455
#define NUM_ELEMENTS(_t)
Definition build.h:406
bool check_config
Definition cf_file.c:61
#define cf_log_warn(_cf, _fmt,...)
Definition cf_util.h:346
static char const * skip_word(char const *text)
Definition command.c:1880
fr_dict_t * dict
Definition common.c:31
#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:224
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:172
#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:243
#define fr_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error.
Definition debug.h:249
#define FR_FAULT_LOG(_fmt,...)
Definition debug.h:52
#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:217
#define MEM(x)
Definition debug.h:38
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:3819
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition dict_util.c:4901
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:2854
bool const fr_dict_attr_nested_allowed_chars[SBUFF_CHAR_CLASS]
Characters allowed in a nested dictionary attribute name.
Definition dict_util.c:63
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:2285
bool const fr_dict_attr_allowed_chars[SBUFF_CHAR_CLASS]
Characters allowed in a single dictionary attribute name.
Definition dict_util.c:56
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:3518
fr_dict_attr_t * fr_dict_attr_unconst(fr_dict_attr_t const *da)
Coerce to non-const.
Definition dict_util.c:4913
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:2637
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:4926
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:3230
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:3259
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:1940
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:3585
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:3696
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:1139
Definition dwarf.c:424
Definition dwarf.c:563
static tmpl_res_rules_t tr_rules
Definition fuzzer_tmpl.c:53
talloc_free(hp)
#define FR_IPADDR_STRLEN
Like INET6_ADDRSTRLEN but includes space for the textual Zone ID.
Definition inet.h:89
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
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:363
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:1867
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:2242
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:1833
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:1942
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:2178
#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_uint8(_sbuff_or_marker, _eob)
#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
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.
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:1267
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 'allow_...
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:37
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:317
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
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:804
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:590
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:618
#define talloc_get_type_abort_const
Definition talloc.h:117
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:288
#define talloc_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
Definition talloc.h:211
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
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:224
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
static unsigned count
Definition unittest.c:47
bool xlat_needs_resolving(xlat_exp_head_t const *head)
Check to see if the expansion needs resolving.
#define XLAT_HEAD_VERIFY(_head)
Definition xlat.h:465
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:3206
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:421
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:456
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:696
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:3178
static fr_slen_t parent
Definition pair.h:858
char * fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
Definition print.c:860
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:581
#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:352
bool const fr_type_numeric[FR_TYPE_MAX+1]
Definition types.c:242
bool const fr_type_structural[FR_TYPE_MAX+1]
Definition types.c:252
#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:6131
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:4416
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:4218
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:5187
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:4540
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:6094
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:4574
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:4399
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:5432
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:5103
#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
#define VALUE_BOX_VERIFY(_x)
Definition value.h:1370
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