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: f2ffb4aa0edfd82fbaa35e150407404c5f94a314 $
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: f2ffb4aa0edfd82fbaa35e150407404c5f94a314 $")
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_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_dlist_head_t *list, 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#ifdef TMPL_DCURSOR_MOD
325static int tmpl_dcursor_insert(UNUSED fr_dlist_head_t *list, void *to_insert, void *uctx)
326{
327 tmpl_dcursor_ctx_t *cc = uctx;
329
330 if (!ns) return 0;
331
332 fr_dcursor_insert(&ns->cursor, to_insert);
333 return 0;
334}
335
336static int tmpl_dcursor_remove(UNUSED fr_dlist_head_t *list, void *to_remove, void *uctx)
337{
338 tmpl_dcursor_ctx_t *cc = uctx;
340 void *current;
341
342 if (!ns) return 0;
343
345 if (current == to_remove) {
347 } else {
348 fr_dcursor_set_current(&ns->cursor, to_remove);
351 }
352 return 0;
353}
354#endif
355
356/** Initialise a #fr_dcursor_t at the specified point in a pair tree
357 *
358 * This makes iterating over the one or more #fr_pair_t specified by a #tmpl_t
359 * significantly easier.
360 *
361 * @param[out] err May be NULL if no error code is required.
362 * Will be set to:
363 * - 0 on success.
364 * - -1 if no matching #fr_pair_t could be found.
365 * - -2 if list could not be found (doesn't exist in current #request_t).
366 * - -3 if context could not be found (no parent #request_t available).
367 * @param[in] ctx to make temporary allocations under.
368 * @param[in] cc to initialise. Tracks evaluation state.
369 * Must be explicitly cleared with tmpl_cursor_state_clear
370 * otherwise we will leak memory.
371 * @param[in] cursor to store iterator position.
372 * @param[in] request the current request.
373 * @param[in] list a nested list to start evaluating from.
374 * May be the child list of a pair in the request's pair tree.
375 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
376 * @param[in] build Callback to build missing pairs.
377 * @param[in] uctx to pass to build.
378 * @return
379 * - First #fr_pair_t specified by the #tmpl_t.
380 * - NULL if no matching #fr_pair_t found, and NULL on error.
381 *
382 * @see tmpl_cursor_next
383 */
385 fr_dcursor_t *cursor,
386 request_t *request, fr_pair_t *list, tmpl_t const *vpt,
387 tmpl_dcursor_build_t build, void *uctx)
388{
389 fr_pair_t *vp = NULL;
390
392
393 /*
394 * Initialise the temporary cursor context
395 */
396 *cc = (tmpl_dcursor_ctx_t){
397 .vpt = vpt,
398 .ctx = ctx,
399 .request = request,
400 .list = &list->vp_group,
401 .build = build,
402 .uctx = uctx
403 };
405
406 /*
407 * Prime the stack!
408 */
409 switch (vpt->type) {
410 case TMPL_TYPE_ATTR:
411 _tmpl_cursor_pair_init(list, cc->list, tmpl_attr_list_head(&vpt->data.attribute.ar), cc);
412 break;
413
414 default:
415 fr_assert(0);
416 break;
417 }
418
419 /*
420 * Get the first entry from the tmpl
421 */
422#ifndef TMPL_DCURSOR_MOD
424#else
425 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);
426#endif
427 if (!vp) {
428 if (err) {
429 *err = -1;
430 if (tmpl_is_list(vpt)) {
431 fr_strerror_printf("List \"%s\" is empty", vpt->name);
432 } else {
433 fr_strerror_printf("No matching \"%s\" pairs found", tmpl_attr_tail_da(vpt)->name);
434 }
435 }
436 return NULL;
437 }
438
439 return vp;
440}
441
442/** Initialise a #fr_dcursor_t to the #fr_pair_t specified by a #tmpl_t
443 *
444 * This makes iterating over the one or more #fr_pair_t specified by a #tmpl_t
445 * significantly easier.
446 *
447 * @param[out] err May be NULL if no error code is required.
448 * Will be set to:
449 * - 0 on success.
450 * - -1 if no matching #fr_pair_t could be found.
451 * - -2 if list could not be found (doesn't exist in current #request_t).
452 * - -3 if context could not be found (no parent #request_t available).
453 * @param[in] ctx to make temporary allocations under.
454 * @param[in] cc to initialise. Tracks evaluation state.
455 * Must be explicitly cleared with tmpl_cursor_state_clear
456 * otherwise we will leak memory.
457 * @param[in] cursor to store iterator position.
458 * @param[in] request The current #request_t.
459 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
460 * @param[in] build Callback to build missing pairs.
461 * @param[in] uctx for building new pairs.
462 * @return
463 * - First #fr_pair_t specified by the #tmpl_t.
464 * - NULL if no matching #fr_pair_t found, and NULL on error.
465 *
466 * @see tmpl_cursor_next
467 */
469 fr_dcursor_t *cursor, request_t *request, tmpl_t const *vpt,
470 tmpl_dcursor_build_t build, void *uctx)
471{
472 fr_pair_t *list;
473
475
476 if (err) *err = 0;
477
478 /*
479 * Navigate to the correct request context (parent, outer, current, etc...)
480 */
481 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) {
482 if (err) *err = -3;
483 memset(cc, 0, sizeof(*cc)); /* so tmpl_dcursor_clear doesn't explode */
484 return NULL;
485 }
486 list = request->pair_root; /* Start navigating from the root of that request */
487
488 return tmpl_dcursor_init_relative(err, ctx, cc, cursor, request, list, vpt, build, uctx);
489}
490
491/** Clear any temporary state allocations
492 *
493 */
495{
496 /*
497 * If the pool wasn't created, nothing was talloc'd which
498 * needs freeing.
499 */
500 if (!cc->pool) return;
501
502 fr_dlist_remove(&cc->nested, &cc->leaf); /* Noop if leaf isn't inserted */
504
505 TALLOC_FREE(cc->pool);
506}
507
508/** Initialize a #tmpl_dcursor_t into a #fr_value_box_t
509 *
510 * The #tmpl_dcursor_ctx_t and #tmpl_dcursor_t are associated with the
511 * input value-box, and will be freed when the value-box is freed.
512 *
513 * @param[out] err May be NULL if no error code is required.
514 * Will be set to:
515 * - 0 on success.
516 * - -1 if no matching #fr_pair_t could be found.
517 * - -2 if list could not be found (doesn't exist in current #request_t).
518 * - -3 if context could not be found (no parent #request_t available).
519 * @param[in] ctx Where the cursor will be allocated from
520 * @param[in] vb Where the #tmpl_dursor_t is stored.
521 * @param[in] request the current request.
522 * @param[in] vpt specifying the #fr_pair_t type or list to iterate over.
523 * @return
524 * - First #fr_pair_t specified by the #tmpl_t.
525 * - NULL if no matching #fr_pair_t found, and NULL on error.
526 *
527 * @see tmpl_cursor_next
528 */
529fr_pair_t *tmpl_dcursor_value_box_init(int *err, TALLOC_CTX *ctx, fr_value_box_t *vb, request_t *request, tmpl_t const *vpt)
530{
532 fr_dcursor_t *cursor;
533 fr_pair_t *vp;
534
535 MEM(cc = talloc(ctx, tmpl_dcursor_ctx_t));
536 MEM(cursor = talloc(ctx, fr_dcursor_t));
537
538 /*
539 * The cursor can return something, nothing (-1), or no list (-2) or no context (-3). Of
540 * these, only the last two are actually errors.
541 *
542 * "no matching pair" is a valid answer, and can be passed to the function.
543 */
544 vp = tmpl_dcursor_init(err, cc, cc, cursor, request, vpt);
545 if (!vp) {
546 if (!err) return NULL;
547
548 if (*err == -1) {
549 RWDEBUG("Cursor %s returned no attributes", vpt->name);
550 } else {
551 RPEDEBUG("Failed initializing cursor");
552 }
553
554 return NULL;
555 }
556
558 return vp;
559}
560
561
562/** Simple pair building callback for use with tmpl_dcursors
563 *
564 * Which always appends the new pair to the tail of the list
565 * since it is only called when no matching pairs were found when
566 * walking the list.
567 *
568 * @param[in] parent to allocate new pair within.
569 * @param[in,out] cursor to append new pair to.
570 * @param[in] da of new pair.
571 * @param[in] uctx unused.
572 * @return
573 * - newly allocated #fr_pair_t.
574 * - NULL on error.
575 */
577{
578 fr_pair_t *vp;
580 if (vp) fr_dcursor_append(cursor, vp);
581 return vp;
582}
583
584#define EXTENT_ADD(_out, _ar, _list_ctx, _list) \
585 do { \
586 tmpl_attr_extent_t *_extent; \
587 MEM(_extent = talloc(ctx, tmpl_attr_extent_t)); \
588 *_extent = (tmpl_attr_extent_t){ \
589 .ar = _ar, \
590 .list_ctx = _list_ctx, \
591 .list = _list \
592 }; \
593 fr_dlist_insert_tail(_out, _extent); \
594 } while (0)
595
596/** Determines points where the reference list extends beyond the current pair tree
597 *
598 * If a particular branch in the VP hierarchy is incomplete, i.e. the chain of attribute
599 * refers to nodes deeper than the nodes currently in the tree, then we return the
600 * deepest point node in the tree which matched, and the ar that we failed to evaluate.
601 *
602 * If the reference list resolves to one or more structural pairs, return those as well.
603 *
604 * This function can be used for a number of different operations, but it's most useful
605 * for determining insertion points for new attributes, or determining which attributes
606 * need to be updated.
607 *
608 * @param[in] ctx to allocate. It's recommended to pass a pool with space
609 * for at least five extent structures.
610 * @param[out] existing List of extents we discovered by evaluating all
611 * attribute references. May be NULL.
612 * @param[out] to_build List of extents that need building out, i.e. references
613 * extend beyond pairs. May be NULL.
614 * @param[in] request The current #request_t.
615 * @param[in] vpt specifying the #fr_pair_t type to retrieve or create.
616 * Must be #TMPL_TYPE_ATTR.
617 * @return
618 * - 0 on success a pair was found.
619 * - -2 if list could not be found (doesn't exist in current #request_t).
620 * - -3 if context could not be found (no parent #request_t available).
621 */
622int tmpl_extents_find(TALLOC_CTX *ctx,
623 fr_dlist_head_t *existing, fr_dlist_head_t *to_build,
624 request_t *request, tmpl_t const *vpt)
625{
626 fr_pair_t *curr = NULL;
627 fr_pair_list_t *list_head;
628
629 TALLOC_CTX *list_ctx = NULL;
630
632 tmpl_dcursor_nested_t *ns = NULL;
633
634 tmpl_attr_t const *ar = NULL;
635
637
639
640 /*
641 * Navigate to the correct request context
642 */
643 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) return -3;
644
645 list_head = &request->pair_root->vp_group;
646 list_ctx = request->pair_root;
647
648 /*
649 * If it's a leaf skip all the expensive
650 * initialisation and just return the list
651 * it's part of.
652 *
653 * This is only needed because lists are
654 * treated specially. Once lists are groups
655 * this can be removed.
656 */
657 ar = tmpl_attr_list_head(&vpt->data.attribute.ar);
658 switch (ar->ar_da->type) {
660 break;
661
662 default:
663 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
664 return 0;
665 }
666
667 /*
668 * Initialise the temporary cursor context
669 */
670 cc = (tmpl_dcursor_ctx_t){
671 .vpt = vpt,
672 .ctx = ctx,
673 .request = request,
674 .list = list_head
675 };
677
678 /*
679 * Prime the stack!
680 */
681 _tmpl_cursor_pair_init(list_ctx, cc.list, tmpl_attr_list_head(&vpt->data.attribute.ar), &cc);
682
683 /*
684 * - Continue until there are no evaluation contexts
685 * - Push a evaluation context if evaluating the head of the
686 * stack yields a VP and we're not at the deepest attribute
687 * reference.
688 * - Return if we have a VP and there are no more attribute
689 * references to push, i.e. we're at the deepest attribute
690 * reference.
691 */
692 curr = fr_pair_list_head(list_head);
693 while ((ns = fr_dlist_tail(&cc.nested))) {
694 tmpl_attr_t const *n_ar;
695
696 list_ctx = ns->list_ctx;
697 ar = ns->ar;
698 curr = _tmpl_cursor_eval(curr, &cc);
699 if (!curr) {
700 /*
701 * References extend beyond current
702 * pair tree.
703 */
704 if (!ar->resolve_only && to_build) EXTENT_ADD(to_build, ar, list_ctx, list_head);
705 continue; /* Rely on _tmpl_cursor_eval popping the stack */
706 }
707
708 /*
709 * Evaluate the next reference
710 */
711 n_ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar);
712 if (n_ar) {
713 ar = n_ar;
714 list_head = &curr->vp_group;
715 list_ctx = curr; /* Allocations are under the group */
716 _tmpl_cursor_pair_init(list_ctx, list_head, ar, &cc);
717 curr = fr_pair_list_head(list_head);
718 continue;
719 }
720
721 /*
722 * Only reached when we can't find an exiting
723 * part of the pair_root to keep walking.
724 *
725 * VP tree may extend beyond the reference.
726 * If the reference was structural, record this
727 * as an extent.
728 */
729 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
730
731 break;
732 }
733
734 return 0;
735}
736
737/** Allocate interior pairs
738 *
739 * Builds out the pair tree to the point where leaf attributes can be added
740 *
741 * @param[out] existing List to add built out attributes to.
742 * @param[in] to_build List to remove attributes from.
743 * @param[in] vpt We are evaluating.
744 * @return
745 * - 0 on success.
746 * - -1 on failure.
747 */
749{
750 tmpl_attr_extent_t *extent = NULL;
751
752 while ((extent = fr_dlist_head(to_build))) {
753 fr_pair_list_t *list;
754 TALLOC_CTX *list_ctx;
755 fr_pair_t *vp;
756 tmpl_attr_t const *ar;
757
758 fr_assert(extent->ar); /* Interior extents MUST contain an ar */
759
760 /*
761 * Try and allocate VPs for the
762 * rest of the attribute references.
763 */
764 for (ar = extent->ar, list = extent->list, list_ctx = extent->list_ctx;
765 ar;
766 ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar)) {
767 switch (ar->type) {
770 /*
771 * Don't build leaf attributes
772 */
773 if (!fr_type_is_structural(ar->ar_da->type)) continue;
774
775 MEM(vp = fr_pair_afrom_da(list_ctx, ar->ar_da)); /* Copies unknowns */
776 fr_pair_append(list, vp);
777 list = &vp->vp_group;
778 list_ctx = vp; /* New allocations occur under the VP */
779 break;
780
781 default:
782 fr_assert_fail("references of this type should have been resolved");
783 return -1;
784 }
785 }
786
787 fr_dlist_remove(to_build, extent); /* Do this *before* zeroing the dlist headers */
788 *extent = (tmpl_attr_extent_t){
789 .list = list,
790 .list_ctx = list_ctx
791 };
792 fr_dlist_insert_tail(existing, extent); /* move between in and out */
793 }
794
795 return 0;
796}
797
799{
800 tmpl_attr_extent_t const *extent = NULL;
801 fr_pair_t *vp = NULL;
802
803 for (extent = fr_dlist_head(head);
804 extent;
805 extent = fr_dlist_next(head, extent)) {
806 tmpl_attr_t const *ar = extent->ar;
807 char const *ctx_name;
808
809 if (ar) {
810 FR_FAULT_LOG("extent-interior-attr");
811 tmpl_attr_ref_debug(extent->ar, 0);
812 } else {
813 FR_FAULT_LOG("extent-leaf");
814 }
815
816 ctx_name = talloc_get_name(extent->list_ctx);
817 if (strcmp(ctx_name, "fr_pair_t") == 0) {
818 FR_FAULT_LOG("list_ctx : %p (%s, %s)", extent->list_ctx, ctx_name,
819 ((fr_pair_t *)extent->list_ctx)->da->name);
820 } else {
821 FR_FAULT_LOG("list_ctx : %p (%s)", extent->list_ctx, ctx_name);
822 }
823 FR_FAULT_LOG("list : %p", extent->list);
824 if (fr_pair_list_empty(extent->list)) {
825 FR_FAULT_LOG("list (first) : none (%p)", extent->list);
826 } else {
827 vp = fr_pair_list_head(extent->list);
828 FR_FAULT_LOG("list (first) : %s (%p)", vp->da->name, extent->list);
829 }
830 }
831
832}
833
835{
837 tmpl_request_t *rr = NULL;
838 tmpl_attr_t *ar = NULL;
839 fr_sbuff_t our_out = FR_SBUFF(out);
840
841 /*
842 * Print all the request references
843 */
844 while ((rr = tmpl_request_list_next(&cc->vpt->data.attribute.rr, rr))) {
846 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
847 }
848
849 ns = fr_dlist_head(&cc->nested);
850
851 /*
852 * This also prints out the things we're looping over in nested?
853 */
854 while ((ar = tmpl_attr_list_next(tmpl_attr(cc->vpt), ar))) {
855 if (ns->ar == ar) break;
856
857 if (ar->ar_da == request_attr_local) continue;
858
859 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ar->da->name);
860 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
861 }
862
863 /*
864 * Subtract one from the number, because ???
865 *
866 * @todo - for foo.[*], print out the actual da being used, which involves tracking the current
867 * vp, too. Except that we would then have to track _all_ instances of _all_ vps in a list,
868 * which is bad. Perhaps just forbid the use of foo.[*] instead.
869 */
870 while (true) {
871 fr_assert(ns->num > 0);
872
873 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ns->ar->da->name);
874 FR_SBUFF_IN_CHAR_RETURN(&our_out, '[');
875 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%zd", ns->num - 1);
876 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
877
878 ns = fr_dlist_next(&cc->nested, ns);
879 if (!ns) break;
880
881 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
882 }
883
884 FR_SBUFF_SET_RETURN(out, &our_out);
885}
#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
static int fr_dcursor_insert(fr_dcursor_t *cursor, void *v)
Insert directly after the current item.
Definition dcursor.h:437
#define fr_dcursor_init(_cursor, _head)
Initialise a cursor.
Definition dcursor.h:736
static void * fr_dcursor_set_current(fr_dcursor_t *cursor, void *item)
Set the cursor to a specified item.
Definition dcursor.h:355
static void * fr_dcursor_remove(fr_dcursor_t *cursor)
Remove the current item.
Definition dcursor.h:482
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:339
#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:694
#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:1351
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 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
#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 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.
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: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: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