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: d0ffbc55b3e71dfd41622e9840b820235c72ca00 $
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: d0ffbc55b3e71dfd41622e9840b820235c72ca00 $")
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] cursor 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_dcursor_t *cursor, 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(cursor->dlist, 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_cond(ar)) {
189 request_t *request = cc->request;
190
191 RDEBUG("Conditions in array references are unsupported");
192 vp = NULL;
193 pop = true;
194 goto done;
195
196 } else {
198
199 num = ar->ar_num;
200 }
201
202 switch (num) {
203 /*
204 * Get the first instance
205 */
206 case NUM_UNSPEC:
207 pop = true;
208 break;
209
210 /*
211 * Get all instances
212 */
213 case NUM_ALL:
214 case NUM_COUNT:
215 all_inst:
216 /*
217 * @todo - arguably we shouldn't try building things here.
218 */
219 if (!vp) {
220 pop = true; /* pop only when we're done */
221
222 } else if (num != NUM_COUNT) {
223 ns->num++;
224 }
226
227 break;
228
229 /*
230 * Get the last instance
231 */
232 case NUM_LAST:
233 while ((iter = fr_dcursor_next(&ns->cursor))) {
234 vp = iter;
235 }
236 pop = true;
237 break;
238
239 /*
240 * Get the n'th instance
241 */
242 default:
243 find_num:
244 {
245 int16_t i = 0;
246
247 while ((i++ < num) && vp) vp = fr_dcursor_next(&ns->cursor);
248 pop = true;
249 }
250 break;
251 }
252
253 /*
254 * If no pair was found and there is a fill
255 * callback, call that, depending on the suffix
256 */
257 if (!vp && cc->build && ar) switch (num) {
258 case NUM_UNSPEC:
259 case NUM_LAST:
260 case 0:
261 vp = cc->build(ns->list_ctx, &ns->cursor, ar->da, cc->uctx);
262 break;
263
264 default:
265 break;
266 }
267
268done:
269 if (pop) tmpl_cursor_nested_pop(cc);
270
271 return vp;
272}
273
274static void *_tmpl_cursor_next(UNUSED fr_dcursor_t *cursor, void *curr, void *uctx)
275{
276 tmpl_dcursor_ctx_t *cc = uctx;
277 tmpl_t const *vpt = cc->vpt;
278
279 fr_pair_t *vp;
280
281 switch (vpt->type) {
282 case TMPL_TYPE_ATTR:
283 {
284 tmpl_attr_t const *ar = NULL;
285 tmpl_dcursor_nested_t *ns = NULL;
286
287 /*
288 * - Continue until there are no evaluation contexts
289 * - Push a evaluation context if evaluating the head of the
290 * stack yields a VP and we're not at the deepest attribute
291 * reference.
292 * - Return if we have a VP and there are no more attribute
293 * references to push, i.e. we're at the deepest attribute
294 * reference.
295 */
296 while ((ns = fr_dlist_tail(&cc->nested))) {
297 ar = ns->ar;
298 vp = _tmpl_cursor_eval(curr, cc);
299 if (!vp) continue;
300
301 ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar);
302 if (ar) {
303 fr_pair_list_t *list_head;
304
305 list_head = &vp->vp_group;
306 _tmpl_cursor_pair_init(vp, list_head, ar, cc);
307 curr = fr_pair_list_head(list_head);
308 continue;
309 }
310
311 return vp;
312 }
313
314 return NULL;
315 }
316
317 default:
318 fr_assert(0);
319 }
320
321 return NULL;
322}
323
324/** Initialise a #fr_dcursor_t at the specified point in a pair tree
325 *
326 * This makes iterating over the one or more #fr_pair_t specified by a #tmpl_t
327 * significantly easier.
328 *
329 * @param[out] err May be NULL if no error code is required.
330 * Will be set to:
331 * - 0 on success.
332 * - -1 if no matching #fr_pair_t could be found.
333 * - -2 if list could not be found (doesn't exist in current #request_t).
334 * - -3 if context could not be found (no parent #request_t available).
335 * @param[in] ctx to make temporary allocations under.
336 * @param[in] cc to initialise. Tracks evaluation state.
337 * Must be explicitly cleared with tmpl_cursor_state_clear
338 * otherwise we will leak memory.
339 * @param[in] cursor to store iterator position.
340 * @param[in] request the current request.
341 * @param[in] list a nested list to start evaluating from.
342 * May be the child list of a pair in the request's pair tree.
343 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
344 * @param[in] build Callback to build missing pairs.
345 * @param[in] uctx to pass to build.
346 * @return
347 * - First #fr_pair_t specified by the #tmpl_t.
348 * - NULL if no matching #fr_pair_t found, and NULL on error.
349 *
350 * @see tmpl_cursor_next
351 */
353 fr_dcursor_t *cursor,
354 request_t *request, fr_pair_t *list, tmpl_t const *vpt,
355 tmpl_dcursor_build_t build, void *uctx)
356{
357 fr_pair_t *vp = NULL;
358
360
361 /*
362 * Initialise the temporary cursor context
363 */
364 *cc = (tmpl_dcursor_ctx_t){
365 .vpt = vpt,
366 .ctx = ctx,
367 .request = request,
368 .list = &list->vp_group,
369 .build = build,
370 .uctx = uctx
371 };
373
374 /*
375 * Prime the stack!
376 */
377 switch (vpt->type) {
378 case TMPL_TYPE_ATTR:
379 _tmpl_cursor_pair_init(list, cc->list, tmpl_attr_list_head(&vpt->data.attribute.ar), cc);
380 break;
381
382 default:
383 fr_assert(0);
384 break;
385 }
386
387 /*
388 * Get the first entry from the tmpl
389 */
391 if (!vp) {
392 if (err) {
393 *err = -1;
394 if (tmpl_is_list(vpt)) {
395 fr_strerror_printf("List \"%s\" is empty", vpt->name);
396 } else {
397 fr_strerror_printf("No matching \"%s\" pairs found", tmpl_attr_tail_da(vpt)->name);
398 }
399 }
400 return NULL;
401 }
402
403 return vp;
404}
405
406/** Initialise a #fr_dcursor_t to the #fr_pair_t specified by a #tmpl_t
407 *
408 * This makes iterating over the one or more #fr_pair_t specified by a #tmpl_t
409 * significantly easier.
410 *
411 * @param[out] err May be NULL if no error code is required.
412 * Will be set to:
413 * - 0 on success.
414 * - -1 if no matching #fr_pair_t could be found.
415 * - -2 if list could not be found (doesn't exist in current #request_t).
416 * - -3 if context could not be found (no parent #request_t available).
417 * @param[in] ctx to make temporary allocations under.
418 * @param[in] cc to initialise. Tracks evaluation state.
419 * Must be explicitly cleared with tmpl_cursor_state_clear
420 * otherwise we will leak memory.
421 * @param[in] cursor to store iterator position.
422 * @param[in] request The current #request_t.
423 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
424 * @param[in] build Callback to build missing pairs.
425 * @param[in] uctx for building new pairs.
426 * @return
427 * - First #fr_pair_t specified by the #tmpl_t.
428 * - NULL if no matching #fr_pair_t found, and NULL on error.
429 *
430 * @see tmpl_cursor_next
431 */
433 fr_dcursor_t *cursor, request_t *request, tmpl_t const *vpt,
434 tmpl_dcursor_build_t build, void *uctx)
435{
436 fr_pair_t *list;
437
439
440 if (err) *err = 0;
441
442 /*
443 * Navigate to the correct request context (parent, outer, current, etc...)
444 */
445 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) {
446 if (err) *err = -3;
447 memset(cc, 0, sizeof(*cc)); /* so tmpl_dcursor_clear doesn't explode */
448 return NULL;
449 }
450 list = request->pair_root; /* Start navigating from the root of that request */
451
452 return tmpl_dcursor_init_relative(err, ctx, cc, cursor, request, list, vpt, build, uctx);
453}
454
455/** Clear any temporary state allocations
456 *
457 */
459{
460 /*
461 * If the pool wasn't created, nothing was talloc'd which
462 * needs freeing.
463 */
464 if (!cc->pool) return;
465
466 fr_dlist_remove(&cc->nested, &cc->leaf); /* Noop if leaf isn't inserted */
468
469 TALLOC_FREE(cc->pool);
470}
471
472/** Initialize a #tmpl_dcursor_t into a #fr_value_box_t
473 *
474 * The #tmpl_dcursor_ctx_t and #tmpl_dcursor_t are associated with the
475 * input value-box, and will be freed when the value-box is freed.
476 *
477 * @param[out] err May be NULL if no error code is required.
478 * Will be set to:
479 * - 0 on success.
480 * - -1 if no matching #fr_pair_t could be found.
481 * - -2 if list could not be found (doesn't exist in current #request_t).
482 * - -3 if context could not be found (no parent #request_t available).
483 * @param[in] ctx Where the cursor will be allocated from
484 * @param[in] vb Where the #tmpl_dursor_t is stored.
485 * @param[in] request the current request.
486 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
487 * @return
488 * - First #fr_pair_t specified by the #tmpl_t.
489 * - NULL if no matching #fr_pair_t found, and NULL on error.
490 *
491 * @see tmpl_cursor_next
492 */
493fr_pair_t *tmpl_dcursor_value_box_init(int *err, TALLOC_CTX *ctx, fr_value_box_t *vb, request_t *request, tmpl_t const *vpt)
494{
496 fr_dcursor_t *cursor;
497 fr_pair_t *vp;
498
499 MEM(cc = talloc(ctx, tmpl_dcursor_ctx_t));
500 MEM(cursor = talloc(ctx, fr_dcursor_t));
501
502 /*
503 * The cursor can return something, nothing (-1), or no list (-2) or no context (-3). Of
504 * these, only the last two are actually errors.
505 *
506 * "no matching pair" is a valid answer, and can be passed to the function.
507 */
508 vp = tmpl_dcursor_init(err, cc, cc, cursor, request, vpt);
509 if (!vp) {
510 if (!err) return NULL;
511
512 if (*err == -1) {
513 RWDEBUG("Cursor %s returned no attributes", vpt->name);
514 } else {
515 RPEDEBUG("Failed initializing cursor");
516 }
517
518 return NULL;
519 }
520
522 return vp;
523}
524
525
526/** Simple pair building callback for use with tmpl_dcursors
527 *
528 * Which always appends the new pair to the tail of the list
529 * since it is only called when no matching pairs were found when
530 * walking the list.
531 *
532 * @param[in] parent to allocate new pair within.
533 * @param[in,out] cursor to append new pair to.
534 * @param[in] da of new pair.
535 * @param[in] uctx unused.
536 * @return
537 * - newly allocated #fr_pair_t.
538 * - NULL on error.
539 */
541{
542 fr_pair_t *vp;
544 if (vp) fr_dcursor_append(cursor, vp);
545 return vp;
546}
547
548#define EXTENT_ADD(_out, _ar, _list_ctx, _list) \
549 do { \
550 tmpl_attr_extent_t *_extent; \
551 MEM(_extent = talloc(ctx, tmpl_attr_extent_t)); \
552 *_extent = (tmpl_attr_extent_t){ \
553 .ar = _ar, \
554 .list_ctx = _list_ctx, \
555 .list = _list \
556 }; \
557 fr_dlist_insert_tail(_out, _extent); \
558 } while (0)
559
560/** Determines points where the reference list extends beyond the current pair tree
561 *
562 * If a particular branch in the VP hierarchy is incomplete, i.e. the chain of attribute
563 * refers to nodes deeper than the nodes currently in the tree, then we return the
564 * deepest point node in the tree which matched, and the ar that we failed to evaluate.
565 *
566 * If the reference list resolves to one or more structural pairs, return those as well.
567 *
568 * This function can be used for a number of different operations, but it's most useful
569 * for determining insertion points for new attributes, or determining which attributes
570 * need to be updated.
571 *
572 * @param[in] ctx to allocate. It's recommended to pass a pool with space
573 * for at least five extent structures.
574 * @param[out] existing List of extents we discovered by evaluating all
575 * attribute references. May be NULL.
576 * @param[out] to_build List of extents that need building out, i.e. references
577 * extend beyond pairs. May be NULL.
578 * @param[in] request The current #request_t.
579 * @param[in] vpt specifying the #fr_pair_t type to retrieve or create.
580 * Must be #TMPL_TYPE_ATTR.
581 * @return
582 * - 0 on success a pair was found.
583 * - -2 if list could not be found (doesn't exist in current #request_t).
584 * - -3 if context could not be found (no parent #request_t available).
585 */
586int tmpl_extents_find(TALLOC_CTX *ctx,
587 fr_dlist_head_t *existing, fr_dlist_head_t *to_build,
588 request_t *request, tmpl_t const *vpt)
589{
590 fr_pair_t *curr = NULL;
591 fr_pair_list_t *list_head;
592
593 TALLOC_CTX *list_ctx = NULL;
594
596 tmpl_dcursor_nested_t *ns = NULL;
597
598 tmpl_attr_t const *ar = NULL;
599
601
603
604 /*
605 * Navigate to the correct request context
606 */
607 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) return -3;
608
609 list_head = &request->pair_root->vp_group;
610 list_ctx = request->pair_root;
611
612 /*
613 * If it's a leaf skip all the expensive
614 * initialisation and just return the list
615 * it's part of.
616 *
617 * This is only needed because lists are
618 * treated specially. Once lists are groups
619 * this can be removed.
620 */
621 ar = tmpl_attr_list_head(&vpt->data.attribute.ar);
622 switch (ar->ar_da->type) {
624 break;
625
626 default:
627 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
628 return 0;
629 }
630
631 /*
632 * Initialise the temporary cursor context
633 */
634 cc = (tmpl_dcursor_ctx_t){
635 .vpt = vpt,
636 .ctx = ctx,
637 .request = request,
638 .list = list_head
639 };
641
642 /*
643 * Prime the stack!
644 */
645 _tmpl_cursor_pair_init(list_ctx, cc.list, tmpl_attr_list_head(&vpt->data.attribute.ar), &cc);
646
647 /*
648 * - Continue until there are no evaluation contexts
649 * - Push a evaluation context if evaluating the head of the
650 * stack yields a VP and we're not at the deepest attribute
651 * reference.
652 * - Return if we have a VP and there are no more attribute
653 * references to push, i.e. we're at the deepest attribute
654 * reference.
655 */
656 curr = fr_pair_list_head(list_head);
657 while ((ns = fr_dlist_tail(&cc.nested))) {
658 tmpl_attr_t const *n_ar;
659
660 list_ctx = ns->list_ctx;
661 ar = ns->ar;
662 curr = _tmpl_cursor_eval(curr, &cc);
663 if (!curr) {
664 /*
665 * References extend beyond current
666 * pair tree.
667 */
668 if (!ar->resolve_only && to_build) EXTENT_ADD(to_build, ar, list_ctx, list_head);
669 continue; /* Rely on _tmpl_cursor_eval popping the stack */
670 }
671
672 /*
673 * Evaluate the next reference
674 */
675 n_ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar);
676 if (n_ar) {
677 ar = n_ar;
678 list_head = &curr->vp_group;
679 list_ctx = curr; /* Allocations are under the group */
680 _tmpl_cursor_pair_init(list_ctx, list_head, ar, &cc);
681 curr = fr_pair_list_head(list_head);
682 continue;
683 }
684
685 /*
686 * Only reached when we can't find an exiting
687 * part of the pair_root to keep walking.
688 *
689 * VP tree may extend beyond the reference.
690 * If the reference was structural, record this
691 * as an extent.
692 */
693 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
694
695 break;
696 }
697
698 return 0;
699}
700
701/** Allocate interior pairs
702 *
703 * Builds out the pair tree to the point where leaf attributes can be added
704 *
705 * @param[out] existing List to add built out attributes to.
706 * @param[in] to_build List to remove attributes from.
707 * @param[in] vpt We are evaluating.
708 * @return
709 * - 0 on success.
710 * - -1 on failure.
711 */
713{
714 tmpl_attr_extent_t *extent = NULL;
715
716 while ((extent = fr_dlist_head(to_build))) {
717 fr_pair_list_t *list;
718 TALLOC_CTX *list_ctx;
719 fr_pair_t *vp;
720 tmpl_attr_t const *ar;
721
722 fr_assert(extent->ar); /* Interior extents MUST contain an ar */
723
724 /*
725 * Try and allocate VPs for the
726 * rest of the attribute references.
727 */
728 for (ar = extent->ar, list = extent->list, list_ctx = extent->list_ctx;
729 ar;
730 ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar)) {
731 switch (ar->type) {
734 /*
735 * Don't build leaf attributes
736 */
737 if (!fr_type_is_structural(ar->ar_da->type)) continue;
738
739 MEM(vp = fr_pair_afrom_da(list_ctx, ar->ar_da)); /* Copies unknowns */
740 fr_pair_append(list, vp);
741 list = &vp->vp_group;
742 list_ctx = vp; /* New allocations occur under the VP */
743 break;
744
745 default:
746 fr_assert_fail("references of this type should have been resolved");
747 return -1;
748 }
749 }
750
751 fr_dlist_remove(to_build, extent); /* Do this *before* zeroing the dlist headers */
752 *extent = (tmpl_attr_extent_t){
753 .list = list,
754 .list_ctx = list_ctx
755 };
756 fr_dlist_insert_tail(existing, extent); /* move between in and out */
757 }
758
759 return 0;
760}
761
763{
764 tmpl_attr_extent_t const *extent = NULL;
765 fr_pair_t *vp = NULL;
766
767 for (extent = fr_dlist_head(head);
768 extent;
769 extent = fr_dlist_next(head, extent)) {
770 tmpl_attr_t const *ar = extent->ar;
771 char const *ctx_name;
772
773 if (ar) {
774 FR_FAULT_LOG("extent-interior-attr");
775 tmpl_attr_ref_debug(extent->ar, 0);
776 } else {
777 FR_FAULT_LOG("extent-leaf");
778 }
779
780 ctx_name = talloc_get_name(extent->list_ctx);
781 if (strcmp(ctx_name, "fr_pair_t") == 0) {
782 FR_FAULT_LOG("list_ctx : %p (%s, %s)", extent->list_ctx, ctx_name,
783 ((fr_pair_t *)extent->list_ctx)->da->name);
784 } else {
785 FR_FAULT_LOG("list_ctx : %p (%s)", extent->list_ctx, ctx_name);
786 }
787 FR_FAULT_LOG("list : %p", extent->list);
788 if (fr_pair_list_empty(extent->list)) {
789 FR_FAULT_LOG("list (first) : none (%p)", extent->list);
790 } else {
791 vp = fr_pair_list_head(extent->list);
792 FR_FAULT_LOG("list (first) : %s (%p)", vp->da->name, extent->list);
793 }
794 }
795
796}
797
799{
801 tmpl_request_t *rr = NULL;
802 tmpl_attr_t *ar = NULL;
803 fr_sbuff_t our_out = FR_SBUFF(out);
804
805 /*
806 * Print all the request references
807 */
808 while ((rr = tmpl_request_list_next(&cc->vpt->data.attribute.rr, rr))) {
810 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
811 }
812
813 ns = fr_dlist_head(&cc->nested);
814
815 /*
816 * This also prints out the things we're looping over in nested?
817 */
818 while ((ar = tmpl_attr_list_next(tmpl_attr(cc->vpt), ar))) {
819 if (ns->ar == ar) break;
820
821 if (ar->ar_da == request_attr_local) continue;
822
823 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ar->da->name);
824 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
825 }
826
827 /*
828 * Subtract one from the number, because ???
829 *
830 * @todo - for foo.[*], print out the actual da being used, which involves tracking the current
831 * vp, too. Except that we would then have to track _all_ instances of _all_ vps in a list,
832 * which is bad. Perhaps just forbid the use of foo.[*] instead.
833 */
834 while (true) {
835 fr_assert(ns->num > 0);
836
837 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ns->ar->da->name);
838 FR_SBUFF_IN_CHAR_RETURN(&our_out, '[');
839 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%zd", ns->num - 1);
840 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
841
842 ns = fr_dlist_next(&cc->nested, ns);
843 if (!ns) break;
844
845 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
846 }
847
848 FR_SBUFF_SET_RETURN(out, &our_out);
849}
#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:290
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:408
#define fr_dcursor_init(_cursor, _head)
Initialise a cursor.
Definition dcursor.h:710
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:339
fr_dlist_head_t * dlist
Head of the doubly linked list being iterated over.
Definition dcursor.h:94
#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:840
static int8_t fr_dict_attr_cmp(fr_dict_attr_t const *a, fr_dict_attr_t const *b)
Definition dict.h:612
#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 RWDEBUG(fmt,...)
Definition log.h:361
#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:1342
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:287
#define fr_assert(_expr)
Definition rad_assert.h:38
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
#define ar_filter_is_cond(_ar)
Definition tmpl.h:519
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 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.
static void * _tmpl_cursor_child_next(fr_dcursor_t *cursor, void *curr, void *uctx)
Traverse a list of attributes.
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_next(UNUSED fr_dcursor_t *cursor, void *curr, void *uctx)
fr_pair_t * tmpl_dcursor_value_box_init(int *err, TALLOC_CTX *ctx, fr_value_box_t *vb, request_t *request, tmpl_t const *vpt)
Initialize a #tmpl_dcursor_t into a fr_value_box_t.
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.
#define tmpl_dcursor_init(_err, _ctx, _cc, _cursor, _request, _vpt)
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:419
#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:567
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:839
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_type_is_structural(_x)
Definition types.h:388
@ FR_TYPE_PAIR_CURSOR
cursor over a fr_pair_t
Definition types.h:89
#define FR_TYPE_STRUCTURAL
Definition types.h:312
void fr_value_box_set_cursor(fr_value_box_t *dst, fr_type_t type, void *cursor, char const *name)
Definition value.c:4731
static size_t char ** out
Definition value.h:1020