The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
tmpl_dcursor.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: ac7ddbfeed73d0c266b7b174d87c49f7145838ca $
19 *
20 * @brief #fr_pair_t template functions
21 * @file src/lib/server/tmpl_dcursor.c
22 *
23 * @ingroup AVP
24 *
25 * @copyright 2020-2021 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
26 */
27RCSID("$Id: ac7ddbfeed73d0c266b7b174d87c49f7145838ca $")
28
29#include <freeradius-devel/server/exec.h>
30#include <freeradius-devel/server/exec_legacy.h>
31#include <freeradius-devel/server/tmpl.h>
32#include <freeradius-devel/server/tmpl_dcursor.h>
33#include <freeradius-devel/util/edit.h>
34
35static inline CC_HINT(always_inline)
37{
38 if (!cc->pool) MEM(cc->pool = talloc_pool(cc->ctx, sizeof(tmpl_dcursor_nested_t) * 5));
39}
40
41/** Traverse a list of attributes
42 *
43 * A dcursor iterator function for matching attributes
44 *
45 * @param[in] list being traversed.
46 * @param[in] curr item in the list to start tests from.
47 * @param[in] uctx Context for evaluation - in this instance a #tmpl_dcursor_nested_t
48 * @return
49 * - the next matching attribute
50 * - NULL if none found
51 */
52static void *_tmpl_cursor_child_next(fr_dlist_head_t *list, void *curr, void *uctx)
53{
54 tmpl_dcursor_nested_t *ns = uctx;
55 fr_pair_t *vp = curr;
56
57 while ((vp = fr_dlist_next(list, vp))) {
58 if (fr_dict_attr_cmp(ns->ar->ar_da, vp->da) == 0) break;
59 }
60
61 return vp;
62}
63
64static inline CC_HINT(always_inline) void tmpl_cursor_nested_push(tmpl_dcursor_ctx_t *cc, tmpl_dcursor_nested_t *ns)
65{
67}
68
69static inline CC_HINT(always_inline) void tmpl_cursor_nested_pop(tmpl_dcursor_ctx_t *cc)
70{
72
73 if (ns != &cc->leaf) talloc_free(ns);
74}
75
76/** Initialise the evaluation context for traversing a group attribute
77 *
78 */
79static inline CC_HINT(always_inline)
80void _tmpl_cursor_pair_init(TALLOC_CTX *list_ctx, fr_pair_list_t *list, tmpl_attr_t const *ar, tmpl_dcursor_ctx_t *cc)
81{
83
84 if (tmpl_attr_list_next(&cc->vpt->data.attribute.ar, ar)) {
86 MEM(ns = talloc(cc->pool, tmpl_dcursor_nested_t));
87 } else {
88 ns = &cc->leaf;
89 }
90
92 .ar = ar,
93 .list_ctx = list_ctx
94 };
95
96 /*
97 * Iterates over attributes of a specific type
98 */
99 if (ar_is_normal(ar)) {
101 /*
102 * Iterates over all attributes at this level
103 */
104 } else if (ar_is_unspecified(ar)) {
106 } else {
107 fr_assert_msg(0, "Invalid attr reference type");
108 }
110}
111
112/** Evaluates, then, sometimes, pops evaluation contexts from the tmpl stack
113 *
114 * To pop or not to pop is determined by whether evaluating the context again
115 * would/should/could produce another fr_pair_t.
116 *
117 * @param[in] curr The pair to evaluate.
118 * @param[in] cc Tracks state between cursor calls.
119 * @return the vp evaluated.
120 */
121static inline CC_HINT(always_inline)
123{
124 tmpl_attr_t const *ar;
126 fr_pair_t *iter = curr, *vp;
127 bool pop = false;
128 int16_t num = NUM_ALL;
129
130 ns = fr_dlist_tail(&cc->nested);
131 ar = ns->ar;
133
134 if (!ar) goto all_inst;
135
136 /*
137 * Array indexes can be attribute references. In which case they must be castable to a uint8_t.
138 *
139 * i.e. there's likly no point in allowing the array ref to specify "none", or "any", or "count".
140 *
141 * Arguably it's useful to specify "all", but why? The main utility of the array reference is to
142 * index a particular attribute when looping over a list of attributes.
143 */
144 if (ar_filter_is_tmpl(ar)) {
145 uint8_t ref;
146
148 fr_assert(tmpl_is_attr(ar->ar_tmpl));
149
150 /*
151 * Can't cast it, we're done.
152 */
153 if (tmpl_expand(&ref, NULL, 0, cc->request, ar->ar_tmpl) < 0) {
154 vp = NULL;
155 pop = true;
156 goto done;
157 }
158
159 num = ref;
160 goto find_num;
161 }
162
163 /*
164 * @todo - add dynamic evaluation of conditions. But that would work _only_ if the conditions
165 * aren't blocking, AND we somehow have a way for the conditions to reference a "self" attribute.
166 */
167
168 /*
169 * No filter means "first one", unless the "foreach" code called tmpl_attr_rewrite_leaf_num(),
170 * which rewrites are_
171 */
172 if (ar_filter_is_none(ar)) {
173 num = 0;
174
175 } else if (ar_filter_is_expr(ar)) {
176 fr_value_box_t box;
177 request_t *request = cc->request;
178
179 if (unlang_xlat_eval_type(request, &box, FR_TYPE_UINT8, NULL, request, ar->ar_expr) < 0) {
180 RPEDEBUG("Failed evaluating expression");
181 vp = NULL;
182 pop = true;
183 goto done;
184 }
185
186 num = box.vb_uint8;
187
188 } else if (!ar_filter_is_num(ar)) {
189 request_t *request = cc->request;
190
191 RDEBUG("Attribute filter is unsupported");
192 vp = NULL;
193 pop = true;
194 goto done;
195
196 } else {
197 num = ar->ar_num;
198 }
199
200 switch (num) {
201 /*
202 * Get the first instance
203 */
204 case NUM_UNSPEC:
205 pop = true;
206 break;
207
208 /*
209 * Get all instances
210 */
211 case NUM_ALL:
212 case NUM_COUNT:
213 all_inst:
214 /*
215 * @todo - arguably we shouldn't try building things here.
216 */
217 if (!vp) {
218 pop = true; /* pop only when we're done */
219
220 } else if (num != NUM_COUNT) {
221 ns->num++;
222 }
224
225 break;
226
227 /*
228 * Get the last instance
229 */
230 case NUM_LAST:
231 while ((iter = fr_dcursor_next(&ns->cursor))) {
232 vp = iter;
233 }
234 pop = true;
235 break;
236
237 /*
238 * Get the n'th instance
239 */
240 default:
241 find_num:
242 {
243 int16_t i = 0;
244
245 while ((i++ < num) && vp) vp = fr_dcursor_next(&ns->cursor);
246 pop = true;
247 }
248 break;
249 }
250
251 /*
252 * If no pair was found and there is a fill
253 * callback, call that, depending on the suffix
254 */
255 if (!vp && cc->build && ar) switch (num) {
256 case NUM_UNSPEC:
257 case NUM_LAST:
258 case 0:
259 vp = cc->build(ns->list_ctx, &ns->cursor, ar->da, cc->uctx);
260 break;
261
262 default:
263 break;
264 }
265
266done:
267 if (pop) tmpl_cursor_nested_pop(cc);
268
269 return vp;
270}
271
272static void *_tmpl_cursor_next(UNUSED fr_dlist_head_t *list, void *curr, void *uctx)
273{
274 tmpl_dcursor_ctx_t *cc = uctx;
275 tmpl_t const *vpt = cc->vpt;
276
277 fr_pair_t *vp;
278
279 switch (vpt->type) {
280 case TMPL_TYPE_ATTR:
281 {
282 tmpl_attr_t const *ar = NULL;
283 tmpl_dcursor_nested_t *ns = NULL;
284
285 /*
286 * - Continue until there are no evaluation contexts
287 * - Push a evaluation context if evaluating the head of the
288 * stack yields a VP and we're not at the deepest attribute
289 * reference.
290 * - Return if we have a VP and there are no more attribute
291 * references to push, i.e. we're at the deepest attribute
292 * reference.
293 */
294 while ((ns = fr_dlist_tail(&cc->nested))) {
295 ar = ns->ar;
296 vp = _tmpl_cursor_eval(curr, cc);
297 if (!vp) continue;
298
299 ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar);
300 if (ar) {
301 fr_pair_list_t *list_head;
302
303 list_head = &vp->vp_group;
304 _tmpl_cursor_pair_init(vp, list_head, ar, cc);
305 curr = fr_pair_list_head(list_head);
306 continue;
307 }
308
309 return vp;
310 }
311
312 return NULL;
313 }
314
315 default:
316 fr_assert(0);
317 }
318
319 return NULL;
320}
321
322#ifdef TMPL_DCURSOR_MOD
323static int tmpl_dcursor_insert(UNUSED fr_dlist_head_t *list, void *to_insert, void *uctx)
324{
325 tmpl_dcursor_ctx_t *cc = uctx;
327
328 if (!ns) return 0;
329
330 fr_dcursor_insert(&ns->cursor, to_insert);
331 return 0;
332}
333
334static int tmpl_dcursor_remove(UNUSED fr_dlist_head_t *list, void *to_remove, void *uctx)
335{
336 tmpl_dcursor_ctx_t *cc = uctx;
338 void *current;
339
340 if (!ns) return 0;
341
343 if (current == to_remove) {
345 } else {
346 fr_dcursor_set_current(&ns->cursor, to_remove);
349 }
350 return 0;
351}
352#endif
353
354/** Initialise a #fr_dcursor_t at the specified point in a pair tree
355 *
356 * This makes iterating over the one or more #fr_pair_t specified by a #tmpl_t
357 * significantly easier.
358 *
359 * @param[out] err May be NULL if no error code is required.
360 * Will be set to:
361 * - 0 on success.
362 * - -1 if no matching #fr_pair_t could be found.
363 * - -2 if list could not be found (doesn't exist in current #request_t).
364 * - -3 if context could not be found (no parent #request_t available).
365 * @param[in] ctx to make temporary allocations under.
366 * @param[in] cc to initialise. Tracks evaluation state.
367 * Must be explicitly cleared with tmpl_cursor_state_clear
368 * otherwise we will leak memory.
369 * @param[in] cursor to store iterator position.
370 * @param[in] request the current request.
371 * @param[in] list a nested list to start evaluating from.
372 * May be the child list of a pair in the request's pair tree.
373 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
374 * @param[in] build Callback to build missing pairs.
375 * @param[in] uctx to pass to build.
376 * @return
377 * - First #fr_pair_t specified by the #tmpl_t.
378 * - NULL if no matching #fr_pair_t found, and NULL on error.
379 *
380 * @see tmpl_cursor_next
381 */
383 fr_dcursor_t *cursor,
384 request_t *request, fr_pair_t *list, tmpl_t const *vpt,
385 tmpl_dcursor_build_t build, void *uctx)
386{
387 fr_pair_t *vp = NULL;
388
390
391 /*
392 * Initialise the temporary cursor context
393 */
394 *cc = (tmpl_dcursor_ctx_t){
395 .vpt = vpt,
396 .ctx = ctx,
397 .request = request,
398 .list = &list->vp_group,
399 .build = build,
400 .uctx = uctx
401 };
403
404 /*
405 * Prime the stack!
406 */
407 switch (vpt->type) {
408 case TMPL_TYPE_ATTR:
409 _tmpl_cursor_pair_init(list, cc->list, tmpl_attr_list_head(&vpt->data.attribute.ar), cc);
410 break;
411
412 default:
413 fr_assert(0);
414 break;
415 }
416
417 /*
418 * Get the first entry from the tmpl
419 */
420#ifndef TMPL_DCURSOR_MOD
422#else
423 vp = fr_dcursor_iter_mod_init(cursor, fr_pair_list_to_dlist(cc->list), _tmpl_cursor_next, NULL, cc, tmpl_dcursor_insert, tmpl_dcursor_remove, cc);
424#endif
425 if (!vp) {
426 if (err) {
427 *err = -1;
428 if (tmpl_is_list(vpt)) {
429 fr_strerror_printf("List \"%s\" is empty", vpt->name);
430 } else {
431 fr_strerror_printf("No matching \"%s\" pairs found", tmpl_attr_tail_da(vpt)->name);
432 }
433 }
434 return NULL;
435 }
436
437 return vp;
438}
439
440/** Initialise a #fr_dcursor_t to the #fr_pair_t specified by a #tmpl_t
441 *
442 * This makes iterating over the one or more #fr_pair_t specified by a #tmpl_t
443 * significantly easier.
444 *
445 * @param[out] err May be NULL if no error code is required.
446 * Will be set to:
447 * - 0 on success.
448 * - -1 if no matching #fr_pair_t could be found.
449 * - -2 if list could not be found (doesn't exist in current #request_t).
450 * - -3 if context could not be found (no parent #request_t available).
451 * @param[in] ctx to make temporary allocations under.
452 * @param[in] cc to initialise. Tracks evaluation state.
453 * Must be explicitly cleared with tmpl_cursor_state_clear
454 * otherwise we will leak memory.
455 * @param[in] cursor to store iterator position.
456 * @param[in] request The current #request_t.
457 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
458 * @param[in] build Callback to build missing pairs.
459 * @param[in] uctx for building new pairs.
460 * @return
461 * - First #fr_pair_t specified by the #tmpl_t.
462 * - NULL if no matching #fr_pair_t found, and NULL on error.
463 *
464 * @see tmpl_cursor_next
465 */
467 fr_dcursor_t *cursor, request_t *request, tmpl_t const *vpt,
468 tmpl_dcursor_build_t build, void *uctx)
469{
470 fr_pair_t *list;
471
473
474 if (err) *err = 0;
475
476 /*
477 * Navigate to the correct request context (parent, outer, current, etc...)
478 */
479 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) {
480 if (err) *err = -3;
481 memset(cc, 0, sizeof(*cc)); /* so tmpl_dcursor_clear doesn't explode */
482 return NULL;
483 }
484 list = request->pair_root; /* Start navigating from the root of that request */
485
486 return tmpl_dcursor_init_relative(err, ctx, cc, cursor, request, list, vpt, build, uctx);
487}
488
489/** Clear any temporary state allocations
490 *
491 */
493{
494 /*
495 * If the pool wasn't created, nothing was talloc'd which
496 * needs freeing.
497 */
498 if (!cc->pool) return;
499
500 fr_dlist_remove(&cc->nested, &cc->leaf); /* Noop if leaf isn't inserted */
502
503 TALLOC_FREE(cc->pool);
504}
505
506/** Simple pair building callback for use with tmpl_dcursors
507 *
508 * Which always appends the new pair to the tail of the list
509 * since it is only called when no matching pairs were found when
510 * walking the list.
511 *
512 * @param[in] parent to allocate new pair within.
513 * @param[in,out] cursor to append new pair to.
514 * @param[in] da of new pair.
515 * @param[in] uctx unused.
516 * @return
517 * - newly allocated #fr_pair_t.
518 * - NULL on error.
519 */
521{
522 fr_pair_t *vp;
524 if (vp) fr_dcursor_append(cursor, vp);
525 return vp;
526}
527
528#define EXTENT_ADD(_out, _ar, _list_ctx, _list) \
529 do { \
530 tmpl_attr_extent_t *_extent; \
531 MEM(_extent = talloc(ctx, tmpl_attr_extent_t)); \
532 *_extent = (tmpl_attr_extent_t){ \
533 .ar = _ar, \
534 .list_ctx = _list_ctx, \
535 .list = _list \
536 }; \
537 fr_dlist_insert_tail(_out, _extent); \
538 } while (0)
539
540/** Determines points where the reference list extends beyond the current pair tree
541 *
542 * If a particular branch in the VP hierarchy is incomplete, i.e. the chain of attribute
543 * refers to nodes deeper than the nodes currently in the tree, then we return the
544 * deepest point node in the tree which matched, and the ar that we failed to evaluate.
545 *
546 * If the reference list resolves to one or more structural pairs, return those as well.
547 *
548 * This function can be used for a number of different operations, but it's most useful
549 * for determining insertion points for new attributes, or determining which attributes
550 * need to be updated.
551 *
552 * @param[in] ctx to allocate. It's recommended to pass a pool with space
553 * for at least five extent structures.
554 * @param[out] existing List of extents we discovered by evaluating all
555 * attribute references. May be NULL.
556 * @param[out] to_build List of extents that need building out, i.e. references
557 * extend beyond pairs. May be NULL.
558 * @param[in] request The current #request_t.
559 * @param[in] vpt specifying the #fr_pair_t type to retrieve or create.
560 * Must be #TMPL_TYPE_ATTR.
561 * @return
562 * - 0 on success a pair was found.
563 * - -2 if list could not be found (doesn't exist in current #request_t).
564 * - -3 if context could not be found (no parent #request_t available).
565 */
566int tmpl_extents_find(TALLOC_CTX *ctx,
567 fr_dlist_head_t *existing, fr_dlist_head_t *to_build,
568 request_t *request, tmpl_t const *vpt)
569{
570 fr_pair_t *curr = NULL;
571 fr_pair_list_t *list_head;
572
573 TALLOC_CTX *list_ctx = NULL;
574
576 tmpl_dcursor_nested_t *ns = NULL;
577
578 tmpl_attr_t const *ar = NULL;
579
581
583
584 /*
585 * Navigate to the correct request context
586 */
587 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) return -3;
588
589 list_head = &request->pair_root->vp_group;
590 list_ctx = request->pair_root;
591
592 /*
593 * If it's a leaf skip all the expensive
594 * initialisation and just return the list
595 * it's part of.
596 *
597 * This is only needed because lists are
598 * treated specially. Once lists are groups
599 * this can be removed.
600 */
601 ar = tmpl_attr_list_head(&vpt->data.attribute.ar);
602 switch (ar->ar_da->type) {
604 break;
605
606 default:
607 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
608 return 0;
609 }
610
611 /*
612 * Initialise the temporary cursor context
613 */
614 cc = (tmpl_dcursor_ctx_t){
615 .vpt = vpt,
616 .ctx = ctx,
617 .request = request,
618 .list = list_head
619 };
621
622 /*
623 * Prime the stack!
624 */
625 _tmpl_cursor_pair_init(list_ctx, cc.list, tmpl_attr_list_head(&vpt->data.attribute.ar), &cc);
626
627 /*
628 * - Continue until there are no evaluation contexts
629 * - Push a evaluation context if evaluating the head of the
630 * stack yields a VP and we're not at the deepest attribute
631 * reference.
632 * - Return if we have a VP and there are no more attribute
633 * references to push, i.e. we're at the deepest attribute
634 * reference.
635 */
636 curr = fr_pair_list_head(list_head);
637 while ((ns = fr_dlist_tail(&cc.nested))) {
638 tmpl_attr_t const *n_ar;
639
640 list_ctx = ns->list_ctx;
641 ar = ns->ar;
642 curr = _tmpl_cursor_eval(curr, &cc);
643 if (!curr) {
644 /*
645 * References extend beyond current
646 * pair tree.
647 */
648 if (!ar->resolve_only && to_build) EXTENT_ADD(to_build, ar, list_ctx, list_head);
649 continue; /* Rely on _tmpl_cursor_eval popping the stack */
650 }
651
652 /*
653 * Evaluate the next reference
654 */
655 n_ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar);
656 if (n_ar) {
657 ar = n_ar;
658 list_head = &curr->vp_group;
659 list_ctx = curr; /* Allocations are under the group */
660 _tmpl_cursor_pair_init(list_ctx, list_head, ar, &cc);
661 curr = fr_pair_list_head(list_head);
662 continue;
663 }
664
665 /*
666 * Only reached when we can't find an exiting
667 * part of the pair_root to keep walking.
668 *
669 * VP tree may extend beyond the reference.
670 * If the reference was structural, record this
671 * as an extent.
672 */
673 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
674
675 break;
676 }
677
678 return 0;
679}
680
681/** Allocate interior pairs
682 *
683 * Builds out the pair tree to the point where leaf attributes can be added
684 *
685 * @param[out] existing List to add built out attributes to.
686 * @param[in] to_build List to remove attributes from.
687 * @param[in] vpt We are evaluating.
688 * @return
689 * - 0 on success.
690 * - -1 on failure.
691 */
693{
694 tmpl_attr_extent_t *extent = NULL;
695
696 while ((extent = fr_dlist_head(to_build))) {
697 fr_pair_list_t *list;
698 TALLOC_CTX *list_ctx;
699 fr_pair_t *vp;
700 tmpl_attr_t const *ar;
701
702 fr_assert(extent->ar); /* Interior extents MUST contain an ar */
703
704 /*
705 * Try and allocate VPs for the
706 * rest of the attribute references.
707 */
708 for (ar = extent->ar, list = extent->list, list_ctx = extent->list_ctx;
709 ar;
710 ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar)) {
711 switch (ar->type) {
714 /*
715 * Don't build leaf attributes
716 */
717 if (!fr_type_is_structural(ar->ar_da->type)) continue;
718
719 MEM(vp = fr_pair_afrom_da(list_ctx, ar->ar_da)); /* Copies unknowns */
720 fr_pair_append(list, vp);
721 list = &vp->vp_group;
722 list_ctx = vp; /* New allocations occur under the VP */
723 break;
724
725 default:
726 fr_assert_fail("references of this type should have been resolved");
727 return -1;
728 }
729 }
730
731 fr_dlist_remove(to_build, extent); /* Do this *before* zeroing the dlist headers */
732 *extent = (tmpl_attr_extent_t){
733 .list = list,
734 .list_ctx = list_ctx
735 };
736 fr_dlist_insert_tail(existing, extent); /* move between in and out */
737 }
738
739 return 0;
740}
741
743{
744 tmpl_attr_extent_t const *extent = NULL;
745 fr_pair_t *vp = NULL;
746
747 for (extent = fr_dlist_head(head);
748 extent;
749 extent = fr_dlist_next(head, extent)) {
750 tmpl_attr_t const *ar = extent->ar;
751 char const *ctx_name;
752
753 if (ar) {
754 FR_FAULT_LOG("extent-interior-attr");
755 tmpl_attr_ref_debug(extent->ar, 0);
756 } else {
757 FR_FAULT_LOG("extent-leaf");
758 }
759
760 ctx_name = talloc_get_name(extent->list_ctx);
761 if (strcmp(ctx_name, "fr_pair_t") == 0) {
762 FR_FAULT_LOG("list_ctx : %p (%s, %s)", extent->list_ctx, ctx_name,
763 ((fr_pair_t *)extent->list_ctx)->da->name);
764 } else {
765 FR_FAULT_LOG("list_ctx : %p (%s)", extent->list_ctx, ctx_name);
766 }
767 FR_FAULT_LOG("list : %p", extent->list);
768 if (fr_pair_list_empty(extent->list)) {
769 FR_FAULT_LOG("list (first) : none (%p)", extent->list);
770 } else {
771 vp = fr_pair_list_head(extent->list);
772 FR_FAULT_LOG("list (first) : %s (%p)", vp->da->name, extent->list);
773 }
774 }
775
776}
777
779{
781 tmpl_request_t *rr = NULL;
782 tmpl_attr_t *ar = NULL;
783 fr_sbuff_t our_out = FR_SBUFF(out);
784
785 /*
786 * Print all the request references
787 */
788 while ((rr = tmpl_request_list_next(&cc->vpt->data.attribute.rr, rr))) {
790 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
791 }
792
793 ns = fr_dlist_head(&cc->nested);
794
795 /*
796 * This also prints out the things we're looping over in nested?
797 */
798 while ((ar = tmpl_attr_list_next(tmpl_attr(cc->vpt), ar))) {
799 if (ns->ar == ar) break;
800
801 if (ar->ar_da == request_attr_local) continue;
802
803 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ar->da->name);
804 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
805 }
806
807 /*
808 * Subtract one from the number, because ???
809 *
810 * @todo - for foo.[*], print out the actual da being used, which involves tracking the current
811 * vp, too. Except that we would then have to track _all_ instances of _all_ vps in a list,
812 * which is bad. Perhaps just forbid the use of foo.[*] instead.
813 */
814 while (true) {
815 fr_assert(ns->num > 0);
816
817 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ns->ar->da->name);
818 FR_SBUFF_IN_CHAR_RETURN(&our_out, '[');
819 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%zd", ns->num - 1);
820 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
821
822 ns = fr_dlist_next(&cc->nested, ns);
823 if (!ns) break;
824
825 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
826 }
827
828 FR_SBUFF_SET_RETURN(out, &our_out);
829}
#define RCSID(id)
Definition build.h:485
#define UNUSED
Definition build.h:317
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
static int fr_dcursor_insert(fr_dcursor_t *cursor, void *v)
Insert directly after the current item.
Definition dcursor.h:435
#define fr_dcursor_init(_cursor, _head)
Initialise a cursor.
Definition dcursor.h:732
static void * fr_dcursor_set_current(fr_dcursor_t *cursor, void *item)
Set the cursor to a specified item.
Definition dcursor.h:353
static void * fr_dcursor_remove(fr_dcursor_t *cursor)
Remove the current item.
Definition dcursor.h:480
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
#define fr_dcursor_iter_mod_init(_cursor, _list, _iter, _peek, _iter_uctx, _insert, _remove, _mod_uctx)
Initialise a cursor with a custom iterator.
Definition dcursor.h:690
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:210
#define fr_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error.
Definition debug.h:216
#define FR_FAULT_LOG(_fmt,...)
Definition debug.h:49
#define MEM(x)
Definition debug.h:36
static fr_slen_t err
Definition dict.h:833
static int8_t fr_dict_attr_cmp(fr_dict_attr_t const *a, fr_dict_attr_t const *b)
Definition dict.h:611
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:260
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:486
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:638
static void fr_dlist_talloc_free(fr_dlist_head_t *head)
Free all items in a doubly linked list (with talloc)
Definition dlist.h:908
static void * fr_dlist_pop_tail(fr_dlist_head_t *list_head)
Remove the tail item in a list.
Definition dlist.h:688
static void * fr_dlist_tail(fr_dlist_head_t const *list_head)
Return the TAIL item of a list or NULL if the list is empty.
Definition dlist.h:531
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:378
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:555
Head of a doubly linked list.
Definition dlist.h:51
#define RPEDEBUG(fmt,...)
Definition log.h:376
talloc_free(reap)
@ FR_TYPE_UINT8
8 Bit unsigned integer.
long int ssize_t
unsigned char uint8_t
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1347
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:285
#define fr_assert(_expr)
Definition rad_assert.h:38
static rc_request_t * current
static bool done
Definition radclient.c:81
#define RDEBUG(fmt,...)
Definition radclient.h:53
fr_dict_attr_t const * request_attr_local
Definition request.c:47
static char const * name
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define FR_SBUFF_IN_SPRINTF_RETURN(...)
#define FR_SBUFF(_sbuff_or_marker)
#define FR_SBUFF_IN_STRCPY_RETURN(...)
#define TMPL_VERIFY(_vpt)
Definition tmpl.h:961
#define ar_is_unspecified(_ar)
Definition tmpl.h:506
#define ar_filter_is_tmpl(_ar)
Definition tmpl.h:520
#define NUM_LAST
Definition tmpl.h:393
TALLOC_CTX * list_ctx
Where to allocate new attributes if building out from the current extents of the tree.
Definition tmpl.h:610
fr_table_num_sorted_t const tmpl_request_ref_table[]
Map keywords to tmpl_request_ref_t values.
void tmpl_attr_ref_debug(const tmpl_attr_t *ar, int idx)
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
#define NUM_ALL
Definition tmpl.h:391
#define ar_filter_is_num(_ar)
Definition tmpl.h:518
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition tmpl.h:142
#define NUM_COUNT
Definition tmpl.h:392
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:920
static fr_slen_t vpt
Definition tmpl.h:1269
#define ar_filter_is_expr(_ar)
Definition tmpl.h:521
#define tmpl_expand(_out, _buff, _buff_len, _request, _vpt)
Expand a tmpl to a C type, using existing storage to hold variably sized types.
Definition tmpl.h:1052
#define NUM_UNSPEC
Definition tmpl.h:390
#define tmpl_attr(_tmpl)
Definition tmpl.h:654
int tmpl_request_ptr(request_t **request, FR_DLIST_HEAD(tmpl_request_list) const *rql)
Resolve a tmpl_request_ref_t to a request_t.
Definition tmpl_eval.c:163
fr_pair_list_t * list
List that we tried to evaluate ar in and failed.
Definition tmpl.h:612
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:801
#define ar_is_normal(_ar)
Definition tmpl.h:505
@ TMPL_ATTR_TYPE_NORMAL
Normal, resolved, attribute ref.
Definition tmpl.h:377
@ TMPL_ATTR_TYPE_UNKNOWN
We have an attribute number but it doesn't match anything in the dictionary, or isn't a child of the ...
Definition tmpl.h:380
tmpl_attr_t const * ar
Attribute representing the ar after the deepest node that was found in the existing pair tree when ev...
Definition tmpl.h:604
#define ar_filter_is_none(_ar)
Definition tmpl.h:517
Describes the current extents of a pair tree in relation to the tree described by a tmpl_t.
Definition tmpl.h:601
fr_pair_t * vp
An element in a list of nested attribute references.
Definition tmpl.h:430
unsigned int _CONST resolve_only
This reference and those before it.
Definition tmpl.h:453
fr_dict_attr_t const *_CONST da
Resolved dictionary attribute.
Definition tmpl.h:434
tmpl_attr_type_t _CONST type
is a raw reference
Definition tmpl.h:458
Define manipulation functions for the attribute reference list.
Definition tmpl.h:471
tmpl_request_ref_t _CONST request
Definition tmpl.h:475
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
static void * _tmpl_cursor_next(UNUSED fr_dlist_head_t *list, void *curr, void *uctx)
static fr_pair_t * _tmpl_cursor_eval(fr_pair_t *curr, tmpl_dcursor_ctx_t *cc)
Evaluates, then, sometimes, pops evaluation contexts from the tmpl stack.
ssize_t tmpl_dcursor_print(fr_sbuff_t *out, tmpl_dcursor_ctx_t const *cc)
static void _tmpl_cursor_pair_init(TALLOC_CTX *list_ctx, fr_pair_list_t *list, tmpl_attr_t const *ar, tmpl_dcursor_ctx_t *cc)
Initialise the evaluation context for traversing a group attribute.
fr_pair_t * tmpl_dcursor_pair_build(fr_pair_t *parent, fr_dcursor_t *cursor, fr_dict_attr_t const *da, UNUSED void *uctx)
Simple pair building callback for use with tmpl_dcursors.
int tmpl_extents_build_to_leaf_parent(fr_dlist_head_t *existing, fr_dlist_head_t *to_build, tmpl_t const *vpt)
Allocate interior pairs.
static void * _tmpl_cursor_child_next(fr_dlist_head_t *list, void *curr, void *uctx)
Traverse a list of attributes.
int tmpl_extents_find(TALLOC_CTX *ctx, fr_dlist_head_t *existing, fr_dlist_head_t *to_build, request_t *request, tmpl_t const *vpt)
Determines points where the reference list extends beyond the current pair tree.
static void tmpl_cursor_nested_push(tmpl_dcursor_ctx_t *cc, tmpl_dcursor_nested_t *ns)
static void tmpl_cursor_nested_pop(tmpl_dcursor_ctx_t *cc)
static void _tmpl_cursor_pool_init(tmpl_dcursor_ctx_t *cc)
void tmpl_dcursor_clear(tmpl_dcursor_ctx_t *cc)
Clear any temporary state allocations.
fr_pair_t * tmpl_dcursor_init_relative(int *err, TALLOC_CTX *ctx, tmpl_dcursor_ctx_t *cc, fr_dcursor_t *cursor, request_t *request, fr_pair_t *list, tmpl_t const *vpt, tmpl_dcursor_build_t build, void *uctx)
Initialise a fr_dcursor_t at the specified point in a pair tree.
void tmpl_extents_debug(fr_dlist_head_t *head)
#define EXTENT_ADD(_out, _ar, _list_ctx, _list)
fr_pair_t * _tmpl_dcursor_init(int *err, TALLOC_CTX *ctx, tmpl_dcursor_ctx_t *cc, fr_dcursor_t *cursor, request_t *request, tmpl_t const *vpt, tmpl_dcursor_build_t build, void *uctx)
Initialise a fr_dcursor_t to the fr_pair_t specified by a tmpl_t.
TALLOC_CTX * ctx
Temporary allocations go here.
tmpl_dcursor_nested_t leaf
Pre-allocated leaf state.
tmpl_dcursor_build_t build
Callback to build missing pairs.
fr_pair_list_t * list
List within the request.
fr_dcursor_t cursor
Cursor to track where we are in the list in case we're doing counts.
struct tmpl_dcursor_ctx_s tmpl_dcursor_ctx_t
size_t num
which attribute number we are looking at
fr_dlist_head_t nested
Nested state.
tmpl_t const * vpt
tmpl we're evaluating.
request_t * request
Result of following the request references.
struct tmpl_dcursor_nested_s tmpl_dcursor_nested_t
TALLOC_CTX * pool
Temporary pool.
void * uctx
Context for building new pairs.
TALLOC_CTX * list_ctx
Track where we should be allocating attributes.
fr_pair_t *(* tmpl_dcursor_build_t)(fr_pair_t *parent, fr_dcursor_t *cursor, fr_dict_attr_t const *da, void *uctx)
Callback function for populating missing pair.
tmpl_attr_t const * ar
Attribute reference this state entry is associated with.
Maintains state between cursor calls.
State for traversing an attribute reference.
int unlang_xlat_eval_type(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t type, fr_dict_attr_t const *enumv, request_t *request, xlat_exp_head_t const *xlat)
Evaluate a "pure" (or not impure) xlat.
Definition xlat.c:765
static fr_slen_t head
Definition xlat.h:416
#define fr_pair_dcursor_iter_init(_cursor, _list, _iter, _uctx)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:569
fr_dlist_head_t * fr_pair_list_to_dlist(fr_pair_list_t const *list)
Get the dlist head from a pair list.
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
fr_pair_t * fr_pair_list_head(fr_pair_list_t const *list)
Get the head of a valuepair list.
Definition pair_inline.c:42
static fr_slen_t parent
Definition pair.h:845
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_type_is_structural(_x)
Definition types.h:371
#define FR_TYPE_STRUCTURAL
Definition types.h:296
static size_t char ** out
Definition value.h:1012