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: eb7295f5f8172bf0e088aea49a6d49add40792c8 $
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: eb7295f5f8172bf0e088aea49a6d49add40792c8 $")
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) fr_dcursor_append(cursor, vp);
567 return vp;
568}
569
570#define EXTENT_ADD(_out, _ar, _list_ctx, _list) \
571 do { \
572 tmpl_attr_extent_t *_extent; \
573 MEM(_extent = talloc(ctx, tmpl_attr_extent_t)); \
574 *_extent = (tmpl_attr_extent_t){ \
575 .ar = _ar, \
576 .list_ctx = _list_ctx, \
577 .list = _list \
578 }; \
579 fr_dlist_insert_tail(_out, _extent); \
580 } while (0)
581
582/** Determines points where the reference list extends beyond the current pair tree
583 *
584 * If a particular branch in the VP hierarchy is incomplete, i.e. the chain of attribute
585 * refers to nodes deeper than the nodes currently in the tree, then we return the
586 * deepest point node in the tree which matched, and the ar that we failed to evaluate.
587 *
588 * If the reference list resolves to one or more structural pairs, return those as well.
589 *
590 * This function can be used for a number of different operations, but it's most useful
591 * for determining insertion points for new attributes, or determining which attributes
592 * need to be updated.
593 *
594 * @param[in] ctx to allocate. It's recommended to pass a pool with space
595 * for at least five extent structures.
596 * @param[out] existing List of extents we discovered by evaluating all
597 * attribute references. May be NULL.
598 * @param[out] to_build List of extents that need building out, i.e. references
599 * extend beyond pairs. May be NULL.
600 * @param[in] request The current #request_t.
601 * @param[in] vpt specifying the #fr_pair_t type to retrieve or create.
602 * Must be #TMPL_TYPE_ATTR.
603 * @return
604 * - 0 on success a pair was found.
605 * - -2 if list could not be found (doesn't exist in current #request_t).
606 * - -3 if context could not be found (no parent #request_t available).
607 */
608int tmpl_extents_find(TALLOC_CTX *ctx,
609 fr_dlist_head_t *existing, fr_dlist_head_t *to_build,
610 request_t *request, tmpl_t const *vpt)
611{
612 fr_pair_t *curr = NULL;
613 fr_pair_list_t *list_head;
614
615 TALLOC_CTX *list_ctx = NULL;
616
618 tmpl_dcursor_nested_t *ns = NULL;
619
620 tmpl_attr_t const *ar = NULL;
621
623
625
626 /*
627 * Navigate to the correct request context
628 */
629 if (tmpl_request_ptr(&request, tmpl_request(vpt)) < 0) return -3;
630
631 list_head = &request->pair_root->vp_group;
632 list_ctx = request->pair_root;
633
634 /*
635 * If it's a leaf skip all the expensive
636 * initialisation and just return the list
637 * it's part of.
638 *
639 * This is only needed because lists are
640 * treated specially. Once lists are groups
641 * this can be removed.
642 */
643 ar = tmpl_attr_list_head(&vpt->data.attribute.ar);
644 switch (ar->ar_da->type) {
646 break;
647
648 default:
649 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
650 return 0;
651 }
652
653 /*
654 * Initialise the temporary cursor context
655 */
656 cc = (tmpl_dcursor_ctx_t){
657 .vpt = vpt,
658 .ctx = ctx,
659 .request = request,
660 .rel_list = list_head
661 };
663
664 /*
665 * Prime the stack!
666 */
667 _tmpl_cursor_pair_init(list_ctx, cc.rel_list, tmpl_attr_list_head(&vpt->data.attribute.ar), &cc);
668
669 /*
670 * - Continue until there are no evaluation contexts
671 * - Push a evaluation context if evaluating the head of the
672 * stack yields a VP and we're not at the deepest attribute
673 * reference.
674 * - Return if we have a VP and there are no more attribute
675 * references to push, i.e. we're at the deepest attribute
676 * reference.
677 */
678 curr = fr_pair_list_head(list_head);
679 while ((ns = fr_dlist_tail(&cc.nested))) {
680 tmpl_attr_t const *n_ar;
681
682 list_ctx = ns->list_ctx;
683 ar = ns->ar;
684 curr = _tmpl_cursor_eval(curr, &cc);
685 if (!curr) {
686 /*
687 * References extend beyond current
688 * pair tree.
689 */
690 if (!ar->resolve_only && to_build) EXTENT_ADD(to_build, ar, list_ctx, list_head);
691 continue; /* Rely on _tmpl_cursor_eval popping the stack */
692 }
693
694 /*
695 * Evaluate the next reference
696 */
697 n_ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar);
698 if (n_ar) {
699 ar = n_ar;
700 list_head = &curr->vp_group;
701 list_ctx = curr; /* Allocations are under the group */
702 _tmpl_cursor_pair_init(list_ctx, list_head, ar, &cc);
703 curr = fr_pair_list_head(list_head);
704 continue;
705 }
706
707 /*
708 * Only reached when we can't find an exiting
709 * part of the pair_root to keep walking.
710 *
711 * VP tree may extend beyond the reference.
712 * If the reference was structural, record this
713 * as an extent.
714 */
715 if (existing) EXTENT_ADD(existing, NULL, list_ctx, list_head);
716
717 break;
718 }
719
720 return 0;
721}
722
723/** Allocate interior pairs
724 *
725 * Builds out the pair tree to the point where leaf attributes can be added
726 *
727 * @param[out] existing List to add built out attributes to.
728 * @param[in] to_build List to remove attributes from.
729 * @param[in] vpt We are evaluating.
730 * @return
731 * - 0 on success.
732 * - -1 on failure.
733 */
735{
736 tmpl_attr_extent_t *extent = NULL;
737
738 while ((extent = fr_dlist_head(to_build))) {
739 fr_pair_list_t *list;
740 TALLOC_CTX *list_ctx;
741 fr_pair_t *vp;
742 tmpl_attr_t const *ar;
743
744 fr_assert(extent->ar); /* Interior extents MUST contain an ar */
745
746 /*
747 * Try and allocate VPs for the
748 * rest of the attribute references.
749 */
750 for (ar = extent->ar, list = extent->list, list_ctx = extent->list_ctx;
751 ar;
752 ar = tmpl_attr_list_next(&vpt->data.attribute.ar, ar)) {
753 switch (ar->type) {
756 /*
757 * Don't build leaf attributes
758 */
759 if (!fr_type_is_structural(ar->ar_da->type)) continue;
760
761 MEM(vp = fr_pair_afrom_da(list_ctx, ar->ar_da)); /* Copies unknowns */
762 fr_pair_append(list, vp);
763 list = &vp->vp_group;
764 list_ctx = vp; /* New allocations occur under the VP */
765 break;
766
767 default:
768 fr_assert_fail("references of this type should have been resolved");
769 return -1;
770 }
771 }
772
773 fr_dlist_remove(to_build, extent); /* Do this *before* zeroing the dlist headers */
774 *extent = (tmpl_attr_extent_t){
775 .list = list,
776 .list_ctx = list_ctx
777 };
778 fr_dlist_insert_tail(existing, extent); /* move between in and out */
779 }
780
781 return 0;
782}
783
785{
786 tmpl_attr_extent_t const *extent = NULL;
787 fr_pair_t *vp = NULL;
788
789 for (extent = fr_dlist_head(head);
790 extent;
791 extent = fr_dlist_next(head, extent)) {
792 tmpl_attr_t const *ar = extent->ar;
793 char const *ctx_name;
794
795 if (ar) {
796 fprintf(fp, "extent-interior-attr\n");
797 tmpl_attr_ref_debug(fp, extent->ar, 0);
798 } else {
799 fprintf(fp, "extent-leaf\n");
800 }
801
802 ctx_name = talloc_get_name(extent->list_ctx);
803 if (strcmp(ctx_name, "fr_pair_t") == 0) {
804 fprintf(fp, "list_ctx : %p (%s, %s)\n", extent->list_ctx, ctx_name,
805 ((fr_pair_t *)extent->list_ctx)->da->name);
806 } else {
807 fprintf(fp, "list_ctx : %p (%s)\n", extent->list_ctx, ctx_name);
808 }
809 fprintf(fp, "list : %p", extent->list);
810 if (fr_pair_list_empty(extent->list)) {
811 fprintf(fp, "list (first) : none (%p)\n", extent->list);
812 } else {
813 vp = fr_pair_list_head(extent->list);
814 fprintf(fp, "list (first) : %s (%p)\n", vp->da->name, extent->list);
815 }
816 }
817
818}
819
821{
823 tmpl_request_t *rr = NULL;
824 tmpl_attr_t *ar = NULL;
825 fr_sbuff_t our_out = FR_SBUFF(out);
826
827 /*
828 * Print all the request references
829 */
830 while ((rr = tmpl_request_list_next(&cc->vpt->data.attribute.rr, rr))) {
832 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
833 }
834
835 ns = fr_dlist_head(&cc->nested);
836
837 /*
838 * This also prints out the things we're looping over in nested?
839 */
840 while ((ar = tmpl_attr_list_next(tmpl_attr(cc->vpt), ar))) {
841 if (ns->ar == ar) break;
842
843 if (ar->ar_da == request_attr_local) continue;
844
845 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ar->da->name);
846 FR_SBUFF_IN_CHAR_RETURN(&our_out, '.');
847 }
848
849 /*
850 * Subtract one from the number, because ???
851 *
852 * @todo - for foo.[*], print out the actual da being used, which involves tracking the current
853 * vp, too. Except that we would then have to track _all_ instances of _all_ vps in a list,
854 * which is bad. Perhaps just forbid the use of foo.[*] instead.
855 */
856 while (true) {
857 fr_assert(ns->num > 0);
858
859 FR_SBUFF_IN_STRCPY_RETURN(&our_out, ns->ar->da->name);
860 FR_SBUFF_IN_CHAR_RETURN(&our_out, '[');
861 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%zd", ns->num - 1);
862 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
863
864 ns = fr_dlist_next(&cc->nested, ns);
865 if (!ns) break;
866
867 FR_SBUFF_IN_CHAR_RETURN(&our_out, ']');
868 }
869
870 FR_SBUFF_SET_RETURN(out, &our_out);
871}
#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:849
static int8_t fr_dict_attr_cmp(fr_dict_attr_t const *a, fr_dict_attr_t const *b)
Definition dict.h:621
#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:1343
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
void tmpl_attr_ref_debug(FILE *fp, const tmpl_attr_t *ar, int idx)
#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.
#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_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:746
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: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:841
#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:4955
static size_t char ** out
Definition value.h:1023