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: 4fcc59abbadebce6169aaa24fcff4368e3d729e7 $
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: 4fcc59abbadebce6169aaa24fcff4368e3d729e7 $")
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) tmpl_dcursor_nested_t *tmpl_cursor_nested_pop(tmpl_dcursor_ctx_t *cc)
70{
72
73 if (ns != &cc->leaf) talloc_free(ns);
74
75 return ns;
76}
77
78/** Initialise the evaluation context for traversing a group attribute
79 *
80 */
81static inline CC_HINT(always_inline)
82void _tmpl_cursor_pair_init(TALLOC_CTX *list_ctx, fr_pair_list_t *list, tmpl_attr_t const *ar, tmpl_dcursor_ctx_t *cc)
83{
85
86 if (tmpl_attr_list_next(&cc->vpt->data.attribute.ar, ar)) {
88 MEM(ns = talloc(cc->pool, tmpl_dcursor_nested_t));
89 } else {
90 ns = &cc->leaf;
91 }
92
94 .ar = ar,
95 .list_ctx = list_ctx
96 };
97
98 /*
99 * Iterates over attributes of a specific type
100 */
101 if (ar_is_normal(ar)) {
103 /*
104 * Iterates over all attributes at this level
105 */
106 } else if (ar_is_unspecified(ar)) {
108 } else {
109 fr_assert_msg(0, "Invalid attr reference type");
110 }
112}
113
114static inline CC_HINT(always_inline) void tmpl_cursor_reset(tmpl_dcursor_ctx_t *cc)
115{
116 while (tmpl_cursor_nested_pop(cc)); /* Pop all the nested cursors */
117
118 /*
119 * Reinitialise the lowest frame in the cursor stack
120 */
121 _tmpl_cursor_pair_init(cc->rel_list_ctx, cc->rel_list, tmpl_attr_list_head(&cc->vpt->data.attribute.ar), cc);
122}
123
124/** Evaluates, then, sometimes, pops evaluation contexts from the tmpl stack
125 *
126 * To pop or not to pop is determined by whether evaluating the context again
127 * would/should/could produce another fr_pair_t.
128 *
129 * @param[in] curr The pair to evaluate.
130 * @param[in] cc Tracks state between cursor calls.
131 * @return the vp evaluated.
132 */
133static inline CC_HINT(always_inline)
135{
136 tmpl_attr_t const *ar;
138 fr_pair_t *iter = curr, *vp;
139 bool pop = false;
140 int16_t num = NUM_ALL;
141
142 ns = fr_dlist_tail(&cc->nested);
143 ar = ns->ar;
145
146 if (!ar) goto all_inst;
147
148 /*
149 * Array indexes can be attribute references. In which case they must be castable to a uint8_t.
150 *
151 * i.e. there's likly no point in allowing the array ref to specify "none", or "any", or "count".
152 *
153 * Arguably it's useful to specify "all", but why? The main utility of the array reference is to
154 * index a particular attribute when looping over a list of attributes.
155 */
156 if (ar_filter_is_tmpl(ar)) {
157 uint8_t ref;
158
160 fr_assert(tmpl_is_attr(ar->ar_tmpl));
161
162 /*
163 * Can't cast it, we're done.
164 */
165 if (tmpl_expand(&ref, NULL, 0, cc->request, ar->ar_tmpl) < 0) {
166 vp = NULL;
167 pop = true;
168 goto done;
169 }
170
171 num = ref;
172 goto find_num;
173 }
174
175 /*
176 * @todo - add dynamic evaluation of conditions. But that would work _only_ if the conditions
177 * aren't blocking, AND we somehow have a way for the conditions to reference a "self" attribute.
178 */
179
180 /*
181 * No filter means "first one", unless the "foreach" code called tmpl_attr_rewrite_leaf_num(),
182 * which rewrites are_
183 */
184 if (ar_filter_is_none(ar)) {
185 num = 0;
186
187 } else if (ar_filter_is_expr(ar)) {
188 fr_value_box_t box;
189 request_t *request = cc->request;
190
191 if (unlang_xlat_eval_type(request, &box, FR_TYPE_UINT8, NULL, request, ar->ar_expr) < 0) {
192 RPEDEBUG("Failed evaluating expression");
193 vp = NULL;
194 pop = true;
195 goto done;
196 }
197
198 num = box.vb_uint8;
199
200 } else if (ar_filter_is_cond(ar)) {
201 request_t *request = cc->request;
202
203 RDEBUG("Conditions in array references are unsupported");
204 vp = NULL;
205 pop = true;
206 goto done;
207
208 } else {
210
211 num = ar->ar_num;
212 }
213
214 switch (num) {
215 /*
216 * Get the first instance
217 */
218 case NUM_UNSPEC:
219 pop = true;
220 break;
221
222 /*
223 * Get all instances
224 */
225 case NUM_ALL:
226 case NUM_COUNT:
227 all_inst:
228 /*
229 * @todo - arguably we shouldn't try building things here.
230 */
231 if (!vp) {
232 pop = true; /* pop only when we're done */
233
234 } else if (num != NUM_COUNT) {
235 ns->num++;
236 }
238
239 break;
240
241 /*
242 * Get the last instance
243 */
244 case NUM_LAST:
245 while ((iter = fr_dcursor_next(&ns->cursor))) {
246 vp = iter;
247 }
248 pop = true;
249 break;
250
251 /*
252 * Get the n'th instance
253 */
254 default:
255 find_num:
256 {
257 int16_t i = 0;
258
259 while ((i++ < num) && vp) vp = fr_dcursor_next(&ns->cursor);
260 pop = true;
261 }
262 break;
263 }
264
265 /*
266 * If no pair was found and there is a fill
267 * callback, call that, depending on the suffix
268 */
269 if (!vp && cc->build && ar) switch (num) {
270 case NUM_UNSPEC:
271 case NUM_LAST:
272 case 0:
273 vp = cc->build(ns->list_ctx, &ns->cursor, ar->da, cc->uctx);
274 break;
275
276 default:
277 break;
278 }
279
280done:
281 if (pop) tmpl_cursor_nested_pop(cc);
282
283 return vp;
284}
285
286static void *_tmpl_cursor_next(UNUSED fr_dcursor_t *cursor, void *curr, void *uctx)
287{
288 tmpl_dcursor_ctx_t *cc = uctx;
289 tmpl_t const *vpt = cc->vpt;
290
291 fr_pair_t *vp;
292
293 /*
294 * No curr means reset back to the initial state
295 * i.e. we're at the end of the cursor, so next
296 * means start from the beginning.
297 */
298 if (!curr) tmpl_cursor_reset(cc);
299
300 switch (vpt->type) {
301 case TMPL_TYPE_ATTR:
302 {
303 tmpl_attr_t const *ar = NULL;
304 tmpl_dcursor_nested_t *ns = NULL;
305
306 /*
307 * - Continue until there are no evaluation contexts
308 * - Push a evaluation context if evaluating the head of the
309 * stack yields a VP and we're not at the deepest attribute
310 * reference.
311 * - Return if we have a VP and there are no more attribute
312 * references to push, i.e. we're at the deepest attribute
313 * reference.
314 */
315 while ((ns = fr_dlist_tail(&cc->nested))) {
316 ar = ns->ar;
317 vp = _tmpl_cursor_eval(curr, cc);
318 if (!vp) continue;
319
320 ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar);
321 if (ar) {
322 fr_pair_list_t *list_head;
323
324 list_head = &vp->vp_group;
325 _tmpl_cursor_pair_init(vp, list_head, ar, cc);
326 curr = fr_pair_list_head(list_head);
327 continue;
328 }
329
330 return vp;
331 }
332
333 return NULL;
334 }
335
336 default:
337 fr_assert(0);
338 }
339
340 return NULL;
341}
342
343/** Initialise a #fr_dcursor_t at the specified point in a pair tree
344 *
345 * This makes iterating over the one or more #fr_pair_t specified by a #tmpl_t
346 * significantly easier.
347 *
348 * @param[out] err May be NULL if no error code is required.
349 * Will be set to:
350 * - 0 on success.
351 * - -1 if no matching #fr_pair_t could be found.
352 * - -2 if list could not be found (doesn't exist in current #request_t).
353 * - -3 if context could not be found (no parent #request_t available).
354 * @param[in] ctx to make temporary allocations under.
355 * @param[in] cc to initialise. Tracks evaluation state.
356 * Must be explicitly cleared with tmpl_cursor_state_clear
357 * otherwise we will leak memory.
358 * @param[in] cursor to store iterator position.
359 * @param[in] request the current request.
360 * @param[in] list a nested list to start evaluating from.
361 * May be the child list of a pair in the request's pair tree.
362 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
363 * @param[in] build Callback to build missing pairs.
364 * @param[in] uctx to pass to build.
365 * @return
366 * - First #fr_pair_t specified by the #tmpl_t.
367 * - NULL if no matching #fr_pair_t found, and NULL on error.
368 *
369 * @see tmpl_cursor_next
370 */
372 fr_dcursor_t *cursor,
373 request_t *request, fr_pair_t *list, tmpl_t const *vpt,
374 tmpl_dcursor_build_t build, void *uctx)
375{
376 fr_pair_t *vp = NULL;
377
379
380 /*
381 * Initialise the temporary cursor context
382 */
383 *cc = (tmpl_dcursor_ctx_t){
384 .vpt = vpt,
385 .ctx = ctx,
386 .request = request,
387 .rel_list_ctx = list,
388 .rel_list = &list->vp_group,
389 .build = build,
390 .uctx = uctx
391 };
393
394 /*
395 * Prime the stack!
396 */
397 switch (vpt->type) {
398 case TMPL_TYPE_ATTR:
399 _tmpl_cursor_pair_init(cc->rel_list_ctx, cc->rel_list, tmpl_attr_list_head(&vpt->data.attribute.ar), cc);
400 break;
401
402 default:
403 fr_assert(0);
404 break;
405 }
406
407 /*
408 * Get the first entry from the tmpl
409 */
411 if (!vp) {
412 if (err) {
413 *err = -1;
414 if (tmpl_is_list(vpt)) {
415 fr_strerror_printf("List \"%s\" is empty", vpt->name);
416 } else {
417 fr_strerror_printf("No matching \"%s\" pairs found", tmpl_attr_tail_da(vpt)->name);
418 }
419 }
420 return NULL;
421 }
422
423 return vp;
424}
425
426/** Initialise a #fr_dcursor_t to the #fr_pair_t specified by a #tmpl_t
427 *
428 * This makes iterating over the one or more #fr_pair_t specified by a #tmpl_t
429 * significantly easier.
430 *
431 * @param[out] err May be NULL if no error code is required.
432 * Will be set to:
433 * - 0 on success.
434 * - -1 if no matching #fr_pair_t could be found.
435 * - -2 if list could not be found (doesn't exist in current #request_t).
436 * - -3 if context could not be found (no parent #request_t available).
437 * @param[in] ctx to make temporary allocations under.
438 * @param[in] cc to initialise. Tracks evaluation state.
439 * Must be explicitly cleared with tmpl_cursor_state_clear
440 * otherwise we will leak memory.
441 * @param[in] cursor to store iterator position.
442 * @param[in] request The current #request_t.
443 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
444 * @param[in] build Callback to build missing pairs.
445 * @param[in] uctx for building new pairs.
446 * @return
447 * - First #fr_pair_t specified by the #tmpl_t.
448 * - NULL if no matching #fr_pair_t found, and NULL on error.
449 *
450 * @see tmpl_cursor_next
451 */
453 fr_dcursor_t *cursor, request_t *request, tmpl_t const *vpt,
454 tmpl_dcursor_build_t build, void *uctx)
455{
456 fr_pair_t *list;
457
459
460 if (err) *err = 0;
461
462 /*
463 * Navigate to the correct request context (parent, outer, current, etc...)
464 */
465 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) {
466 if (err) *err = -3;
467 memset(cc, 0, sizeof(*cc)); /* so tmpl_dcursor_clear doesn't explode */
468 return NULL;
469 }
470 list = request->pair_root; /* Start navigating from the root of that request */
471
472 return tmpl_dcursor_init_relative(err, ctx, cc, cursor, request, list, vpt, build, uctx);
473}
474
475/** Clear any temporary state allocations
476 *
477 */
479{
480 /*
481 * If the pool wasn't created, nothing was talloc'd which
482 * needs freeing.
483 */
484 if (!cc->pool) return;
485
486 fr_dlist_remove(&cc->nested, &cc->leaf); /* Noop if leaf isn't inserted */
488
489 TALLOC_FREE(cc->pool);
490}
491
492/** Initialize a #tmpl_dcursor_t into a #fr_value_box_t
493 *
494 * The #tmpl_dcursor_ctx_t and #tmpl_dcursor_t are associated with the
495 * input value-box, and will be freed when the value-box is freed.
496 *
497 * @param[out] err May be NULL if no error code is required.
498 * Will be set to:
499 * - 0 on success.
500 * - -1 if no matching #fr_pair_t could be found.
501 * - -2 if list could not be found (doesn't exist in current #request_t).
502 * - -3 if context could not be found (no parent #request_t available).
503 * @param[in] ctx Where the cursor will be allocated from
504 * @param[in] vb Where the #tmpl_dursor_t is stored.
505 * @param[in] request the current request.
506 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
507 * @return
508 * - First #fr_pair_t specified by the #tmpl_t.
509 * - NULL if no matching #fr_pair_t found, and NULL on error.
510 *
511 * @see tmpl_cursor_next
512 */
513fr_pair_t *tmpl_dcursor_value_box_init(int *err, TALLOC_CTX *ctx, fr_value_box_t *vb, request_t *request, tmpl_t const *vpt)
514{
516 fr_dcursor_t *cursor;
517 fr_pair_t *vp;
518
519 MEM(cc = talloc(ctx, tmpl_dcursor_ctx_t));
520 MEM(cursor = talloc(ctx, fr_dcursor_t));
521
522 /*
523 * The cursor can return something, nothing (-1), or no list (-2) or no context (-3). Of
524 * these, only the last two are actually errors.
525 *
526 * "no matching pair" is a valid answer, and can be passed to the function.
527 */
528 vp = tmpl_dcursor_init(err, cc, cc, cursor, request, vpt);
529 if (!vp) {
530 if (!err) return NULL;
531
532 if (*err == -1) {
533 RWDEBUG("Cursor %s returned no attributes", vpt->name);
534 goto set_cursor;
535 } else {
536 RPEDEBUG("Failed initializing cursor");
537 }
538
539 return NULL;
540 }
541
542set_cursor:
544 return vp;
545}
546
547
548/** Simple pair building callback for use with tmpl_dcursors
549 *
550 * Which always appends the new pair to the tail of the list
551 * since it is only called when no matching pairs were found when
552 * walking the list.
553 *
554 * @param[in] parent to allocate new pair within.
555 * @param[in,out] cursor to append new pair to.
556 * @param[in] da of new pair.
557 * @param[in] uctx unused.
558 * @return
559 * - newly allocated #fr_pair_t.
560 * - NULL on error.
561 */
563{
564 fr_pair_t *vp;
566 if (vp) {
568 fr_dcursor_append(cursor, vp);
569 }
570 return vp;
571}
572
573#define EXTENT_ADD(_out, _ar, _list_ctx, _list) \
574 do { \
575 tmpl_attr_extent_t *_extent; \
576 MEM(_extent = talloc(ctx, tmpl_attr_extent_t)); \
577 *_extent = (tmpl_attr_extent_t){ \
578 .ar = _ar, \
579 .list_ctx = _list_ctx, \
580 .list = _list \
581 }; \
582 fr_dlist_insert_tail(_out, _extent); \
583 } while (0)
584
585/** Determines points where the reference list extends beyond the current pair tree
586 *
587 * If a particular branch in the VP hierarchy is incomplete, i.e. the chain of attribute
588 * refers to nodes deeper than the nodes currently in the tree, then we return the
589 * deepest point node in the tree which matched, and the ar that we failed to evaluate.
590 *
591 * If the reference list resolves to one or more structural pairs, return those as well.
592 *
593 * This function can be used for a number of different operations, but it's most useful
594 * for determining insertion points for new attributes, or determining which attributes
595 * need to be updated.
596 *
597 * @param[in] ctx to allocate. It's recommended to pass a pool with space
598 * for at least five extent structures.
599 * @param[out] existing List of extents we discovered by evaluating all
600 * attribute references. May be NULL.
601 * @param[out] to_build List of extents that need building out, i.e. references
602 * extend beyond pairs. May be NULL.
603 * @param[in] request The current #request_t.
604 * @param[in] vpt specifying the #fr_pair_t type to retrieve or create.
605 * Must be #TMPL_TYPE_ATTR.
606 * @return
607 * - 0 on success a pair was found.
608 * - -2 if list could not be found (doesn't exist in current #request_t).
609 * - -3 if context could not be found (no parent #request_t available).
610 */
611int tmpl_extents_find(TALLOC_CTX *ctx,
612 fr_dlist_head_t *existing, fr_dlist_head_t *to_build,
613 request_t *request, tmpl_t const *vpt)
614{
615 fr_pair_t *curr = NULL;
616 fr_pair_list_t *list_head;
617
618 TALLOC_CTX *list_ctx = NULL;
619
621 tmpl_dcursor_nested_t *ns = NULL;
622
623 tmpl_attr_t const *ar = NULL;
624
626
628
629 /*
630 * Navigate to the correct request context
631 */
632 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) return -3;
633
634 list_head = &request->pair_root->vp_group;
635 list_ctx = request->pair_root;
636
637 /*
638 * If it's a leaf skip all the expensive
639 * initialisation and just return the list
640 * it's part of.
641 *
642 * This is only needed because lists are
643 * treated specially. Once lists are groups
644 * this can be removed.
645 */
646 ar = tmpl_attr_list_head(&vpt->data.attribute.ar);
647 switch (ar->ar_da->type) {
649 break;
650
651 default:
652 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
653 return 0;
654 }
655
656 /*
657 * Initialise the temporary cursor context
658 */
659 cc = (tmpl_dcursor_ctx_t){
660 .vpt = vpt,
661 .ctx = ctx,
662 .request = request,
663 .rel_list = list_head
664 };
666
667 /*
668 * Prime the stack!
669 */
670 _tmpl_cursor_pair_init(list_ctx, cc.rel_list, tmpl_attr_list_head(&vpt->data.attribute.ar), &cc);
671
672 /*
673 * - Continue until there are no evaluation contexts
674 * - Push a evaluation context if evaluating the head of the
675 * stack yields a VP and we're not at the deepest attribute
676 * reference.
677 * - Return if we have a VP and there are no more attribute
678 * references to push, i.e. we're at the deepest attribute
679 * reference.
680 */
681 curr = fr_pair_list_head(list_head);
682 while ((ns = fr_dlist_tail(&cc.nested))) {
683 tmpl_attr_t const *n_ar;
684
685 list_ctx = ns->list_ctx;
686 ar = ns->ar;
687 curr = _tmpl_cursor_eval(curr, &cc);
688 if (!curr) {
689 /*
690 * References extend beyond current
691 * pair tree.
692 */
693 if (!ar->resolve_only && to_build) EXTENT_ADD(to_build, ar, list_ctx, list_head);
694 continue; /* Rely on _tmpl_cursor_eval popping the stack */
695 }
696
697 /*
698 * Evaluate the next reference
699 */
700 n_ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar);
701 if (n_ar) {
702 ar = n_ar;
703 list_head = &curr->vp_group;
704 list_ctx = curr; /* Allocations are under the group */
705 _tmpl_cursor_pair_init(list_ctx, list_head, ar, &cc);
706 curr = fr_pair_list_head(list_head);
707 continue;
708 }
709
710 /*
711 * Only reached when we can't find an exiting
712 * part of the pair_root to keep walking.
713 *
714 * VP tree may extend beyond the reference.
715 * If the reference was structural, record this
716 * as an extent.
717 */
718 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
719
720 break;
721 }
722
723 return 0;
724}
725
726/** Allocate interior pairs
727 *
728 * Builds out the pair tree to the point where leaf attributes can be added
729 *
730 * @param[out] existing List to add built out attributes to.
731 * @param[in] to_build List to remove attributes from.
732 * @param[in] vpt We are evaluating.
733 * @return
734 * - 0 on success.
735 * - -1 on failure.
736 */
738{
739 tmpl_attr_extent_t *extent = NULL;
740
741 while ((extent = fr_dlist_head(to_build))) {
742 fr_pair_list_t *list;
743 TALLOC_CTX *list_ctx;
744 fr_pair_t *vp;
745 tmpl_attr_t const *ar;
746
747 fr_assert(extent->ar); /* Interior extents MUST contain an ar */
748
749 /*
750 * Try and allocate VPs for the
751 * rest of the attribute references.
752 */
753 for (ar = extent->ar, list = extent->list, list_ctx = extent->list_ctx;
754 ar;
755 ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar)) {
756 switch (ar->type) {
759 /*
760 * Don't build leaf attributes
761 */
762 if (!fr_type_is_structural(ar->ar_da->type)) continue;
763
764 MEM(vp = fr_pair_afrom_da(list_ctx, ar->ar_da)); /* Copies unknowns */
766 fr_pair_append(list, vp);
767 list = &vp->vp_group;
768 list_ctx = vp; /* New allocations occur under the VP */
769 break;
770
771 default:
772 fr_assert_fail("references of this type should have been resolved");
773 return -1;
774 }
775 }
776
777 fr_dlist_remove(to_build, extent); /* Do this *before* zeroing the dlist headers */
778 *extent = (tmpl_attr_extent_t){
779 .list = list,
780 .list_ctx = list_ctx
781 };
782 fr_dlist_insert_tail(existing, extent); /* move between in and out */
783 }
784
785 return 0;
786}
787
789{
790 tmpl_attr_extent_t const *extent = NULL;
791 fr_pair_t *vp = NULL;
792
793 for (extent = fr_dlist_head(head);
794 extent;
795 extent = fr_dlist_next(head, extent)) {
796 tmpl_attr_t const *ar = extent->ar;
797 char const *ctx_name;
798
799 if (ar) {
800 fprintf(fp, "extent-interior-attr\n");
801 tmpl_attr_ref_debug(fp, extent->ar, 0);
802 } else {
803 fprintf(fp, "extent-leaf\n");
804 }
805
806 ctx_name = talloc_get_name(extent->list_ctx);
807 if (strcmp(ctx_name, "fr_pair_t") == 0) {
808 fprintf(fp, "list_ctx : %p (%s, %s)\n", extent->list_ctx, ctx_name,
809 ((fr_pair_t *)extent->list_ctx)->da->name);
810 } else {
811 fprintf(fp, "list_ctx : %p (%s)\n", extent->list_ctx, ctx_name);
812 }
813 fprintf(fp, "list : %p", extent->list);
814 if (fr_pair_list_empty(extent->list)) {
815 fprintf(fp, "list (first) : none (%p)\n", extent->list);
816 } else {
817 vp = fr_pair_list_head(extent->list);
818 fprintf(fp, "list (first) : %s (%p)\n", vp->da->name, extent->list);
819 }
820 }
821
822}
823
825{
827 tmpl_request_t *rr = NULL;
828 tmpl_attr_t *ar = NULL;
829 fr_sbuff_t our_out = FR_SBUFF(out);
830
831 /*
832 * Print all the request references
833 */
834 while ((rr = tmpl_request_list_next(&cc->vpt->data.attribute.rr, rr))) {
836 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
837 }
838
839 ns = fr_dlist_head(&cc->nested);
840
841 /*
842 * This also prints out the things we're looping over in nested?
843 */
844 while ((ar = tmpl_attr_list_next(tmpl_attr(cc->vpt), ar))) {
845 if (ns->ar == ar) break;
846
847 if (ar->ar_da == request_attr_local) continue;
848
849 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ar->da->name);
850 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
851 }
852
853 /*
854 * Subtract one from the number, because ???
855 *
856 * @todo - for foo.[*], print out the actual da being used, which involves tracking the current
857 * vp, too. Except that we would then have to track _all_ instances of _all_ vps in a list,
858 * which is bad. Perhaps just forbid the use of foo.[*] instead.
859 */
860 while (true) {
861 fr_assert(ns->num > 0);
862
863 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ns->ar->da->name);
864 FR_SBUFF_IN_CHAR_RETURN(&our_out, '[');
865 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%zd", ns->num - 1);
866 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
867
868 ns = fr_dlist_next(&cc->nested, ns);
869 if (!ns) break;
870
871 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
872 }
873
874 FR_SBUFF_SET_RETURN(out, &our_out);
875}
#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:202
#define fr_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error.
Definition debug.h:208
#define MEM(x)
Definition debug.h:36
static fr_slen_t err
Definition dict.h:870
static int8_t fr_dict_attr_cmp(fr_dict_attr_t const *a, fr_dict_attr_t const *b)
Definition dict.h:642
#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:1348
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:289
#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:963
#define ar_is_unspecified(_ar)
Definition tmpl.h:508
#define ar_filter_is_tmpl(_ar)
Definition tmpl.h:522
void tmpl_attr_ref_debug(FILE *fp, const tmpl_attr_t *ar, int idx)
#define NUM_LAST
Definition tmpl.h:395
TALLOC_CTX * list_ctx
Where to allocate new attributes if building out from the current extents of the tree.
Definition tmpl.h:612
fr_table_num_sorted_t const tmpl_request_ref_table[]
Map keywords to tmpl_request_ref_t values.
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
#define NUM_ALL
Definition tmpl.h:393
#define ar_filter_is_num(_ar)
Definition tmpl.h:520
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition tmpl.h:142
#define NUM_COUNT
Definition tmpl.h:394
#define ar_filter_is_cond(_ar)
Definition tmpl.h:521
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:922
static fr_slen_t vpt
Definition tmpl.h:1271
#define ar_filter_is_expr(_ar)
Definition tmpl.h:523
#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:1054
#define NUM_UNSPEC
Definition tmpl.h:392
#define tmpl_attr(_tmpl)
Definition tmpl.h:656
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:614
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:803
#define ar_is_normal(_ar)
Definition tmpl.h:507
@ TMPL_ATTR_TYPE_NORMAL
Normal, resolved, attribute ref.
Definition tmpl.h:379
@ 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:382
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:606
#define ar_filter_is_none(_ar)
Definition tmpl.h:519
Describes the current extents of a pair tree in relation to the tree described by a tmpl_t.
Definition tmpl.h:603
fr_pair_t * vp
An element in a list of nested attribute references.
Definition tmpl.h:432
unsigned int _CONST resolve_only
This reference and those before it.
Definition tmpl.h:455
fr_dict_attr_t const *_CONST da
Resolved dictionary attribute.
Definition tmpl.h:436
tmpl_attr_type_t _CONST type
is a raw reference
Definition tmpl.h:460
Define manipulation functions for the attribute reference list.
Definition tmpl.h:473
tmpl_request_ref_t _CONST request
Definition tmpl.h:477
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_reset(tmpl_dcursor_ctx_t *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)
void tmpl_extents_debug(FILE *fp, fr_dlist_head_t *head)
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.
#define EXTENT_ADD(_out, _ar, _list_ctx, _list)
static tmpl_dcursor_nested_t * tmpl_cursor_nested_pop(tmpl_dcursor_ctx_t *cc)
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.
TALLOC_CTX * rel_list_ctx
When we need to allocate pairs in our the rel_list, we use this as the ctx.
tmpl_dcursor_nested_t leaf
Pre-allocated leaf state.
tmpl_dcursor_build_t build
Callback to build missing pairs.
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
fr_pair_list_t * rel_list
List we're starting at.
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:742
static fr_slen_t head
Definition xlat.h:420
#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:582
fr_dlist_head_t * fr_pair_list_to_dlist(fr_pair_list_t const *list)
Get the dlist head from a pair list.
#define PAIR_ALLOCED(_x)
Definition pair.h:209
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:854
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_type_is_structural(_x)
Definition types.h:393
@ FR_TYPE_PAIR_CURSOR
cursor over a fr_pair_t
Definition types.h:91
#define FR_TYPE_STRUCTURAL
Definition types.h:317
void fr_value_box_set_cursor(fr_value_box_t *dst, fr_type_t type, void *cursor, char const *name)
Definition value.c:4967
static size_t char ** out
Definition value.h:1023