The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
pair.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library 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 GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** AVP manipulation and search API
18 *
19 * @file src/lib/util/pair.c
20 *
21 * @copyright 2021 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22 * @copyright 2000,2006,2015,2020 The FreeRADIUS server project
23 */
24RCSID("$Id: 58870063b67fac29c44720d05e2859ae524cf94c $")
25
26#define _PAIR_PRIVATE 1
27#define _PAIR_INLINE 1
28
29#include <freeradius-devel/util/debug.h>
30#include <freeradius-devel/util/misc.h>
31#include <freeradius-devel/util/pair.h>
32#include <freeradius-devel/util/pair_legacy.h>
33#include <freeradius-devel/util/proto.h>
34#include <freeradius-devel/util/regex.h>
35
36FR_TLIST_FUNCS(fr_pair_order_list, fr_pair_t, order_entry)
37FR_TLIST_PARENT_FUNCS(fr_pair_order_list, fr_pair_t, fr_pair_list_t, order)
38
39#include <freeradius-devel/util/pair_inline.c>
40
41/** Initialise a pair list header
42 *
43 * @param[in,out] list to initialise
44 *
45 * @hidecallergraph
46 */
48{
49 /*
50 * Initialises the order list. This
51 * maintains the overall order of attributes
52 * in the list and allows us to iterate over
53 * all of them.
54 */
55 fr_pair_order_list_talloc_init(&list->order);
56
57#ifdef WITH_VERIFY_PTR
58 list->verified = true;
59#endif
60 list->is_child = false;
61}
62
63/** Free a fr_pair_t
64 *
65 * @note Do not call directly, use talloc_free instead.
66 *
67 * @param vp to free.
68 * @return 0
69 */
71{
72#ifdef TALLOC_DEBUG
73 talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL);
74#endif
75
76#if 0
77 /*
78 * We would like to enforce that a VP must be removed from a list before it's freed. However, we
79 * free pair_lists via talloc_free(). And the talloc code just frees things in (essentially) a
80 * random order. So this guarantee can't be enforced.
81 */
82 fr_assert(fr_pair_order_list_parent(vp) == NULL);
83#endif
84
85 /*
86 * Pairs with children have the children
87 * freed explicitly.
88 */
89 if (likely(vp->da != NULL)) switch (vp->vp_type) {
91 fr_pair_list_free(&vp->vp_group);
92 break;
93
94 case FR_TYPE_STRING:
95 case FR_TYPE_OCTETS:
96 fr_assert(!vp->vp_edit);
97 if (fr_value_box_is_secret(&vp->data)) memset_explicit(vp->vp_ptr, 0, vp->vp_length);
98 break;
99
100 default:
101 fr_assert(!vp->vp_edit);
102 if (fr_value_box_is_secret(&vp->data)) memset_explicit(&vp->data, 0, sizeof(vp->data));
103 break;
104 }
105
106#ifndef NDEBUG
107 memset(vp, 0, sizeof(*vp));
108#endif
109
110 return 0;
111}
112
113/** Allocate a new pair list on the heap
114 *
115 * @param[in] ctx to allocate the pair list in.
116 * @return
117 * - A new #fr_pair_list_t.
118 * - NULL if an error occurred.
119 */
121{
122 fr_pair_list_t *pl;
123
124 pl = talloc(ctx, fr_pair_list_t);
125 if (unlikely(!pl)) return NULL;
126
128
129 return pl;
130}
131
132/** Initialise fields in an fr_pair_t without assigning a da
133 *
134 * @note Internal use by the allocation functions only.
135 */
136static inline CC_HINT(always_inline) void pair_init_null(fr_pair_t *vp)
137{
138 fr_pair_order_list_entry_init(vp);
139
140 /*
141 * Legacy cruft
142 */
143 vp->op = T_OP_EQ;
144}
145
146/** Initialise fields in an fr_pair_t without assigning a da
147 *
148 * Used only for temporary value-pairs which are not placed in any list.
149 */
151{
152 memset(vp, 0, sizeof(*vp));
153
155}
156
157/** Dynamically allocate a new attribute with no #fr_dict_attr_t assigned
158 *
159 * This is not the function you're looking for (unless you're binding
160 * unknown attributes to pairs, and need to pre-allocate the memory).
161 * You probably want #fr_pair_afrom_da instead.
162 *
163 * @note You must assign a #fr_dict_attr_t before freeing this #fr_pair_t.
164 *
165 * @param[in] ctx to allocate the pair list in.
166 * @return
167 * - A new #fr_pair_t.
168 * - NULL if an error occurred.
169 */
171{
172 fr_pair_t *vp;
173
174 vp = talloc_zero(ctx, fr_pair_t);
175 if (!vp) {
176 fr_strerror_printf("Out of memory");
177 return NULL;
178 }
179 talloc_set_destructor(vp, _fr_pair_free);
180
183
184 return vp;
185}
186
187/** Continue initialising an fr_pair_t assigning a da
188 *
189 * @note Internal use by the pair allocation functions only.
190 */
191static inline CC_HINT(always_inline) void pair_init_from_da(fr_pair_t *vp, fr_dict_attr_t const *da)
192{
193 /*
194 * Use the 'da' to initialize more fields.
195 */
196 vp->da = da;
197
198 if (likely(fr_type_is_leaf(da->type))) {
199 fr_value_box_init(&vp->data, da->type, da, false);
200 } else {
201#ifndef NDEBUG
202 /*
203 * Make it very obvious if we failed
204 * to initialise something.
205 */
206 memset(&vp->data, 0xff, sizeof(vp->data));
207#endif
208
210
211 /*
212 * Make sure that the pad field is initialized.
213 */
214 if (sizeof(vp->pad)) memset(vp->pad, 0, sizeof(vp->pad));
215
216 /*
217 * Hack around const issues...
218 */
219 memcpy(UNCONST(fr_type_t *, &vp->vp_type), &da->type, sizeof(vp->vp_type));
220 fr_pair_list_init(&vp->vp_group);
221 vp->vp_group.is_child = true;
222 fr_pair_order_list_talloc_init_children(vp, &vp->vp_group.order);
223 }
224}
225
226/** A special allocation function which disables child autofree
227 *
228 * This is intended to allocate root attributes for requests.
229 * These roots are special in that they do not necessarily own
230 * the child attributes and _MUST NOT_ free them when they
231 * themselves are freed. The children are allocated in special
232 * ctxs which may be moved between session state entries and
233 * requests, or may belong to a parent request.
234 *
235 * @param[in] ctx to allocate the pair root in.
236 * @param[in] da The root attribute.
237 * @return
238 * - A new root pair on success.
239 * - NULL on failure.
240 * @hidecallergraph
241 */
243{
244 fr_pair_t *vp;
245
246#ifndef NDEBUG
247 if (da->type != FR_TYPE_GROUP) {
248 fr_strerror_const("Root must be a group type");
249 return NULL;
250 }
251#endif
252
253 vp = talloc_zero(ctx, fr_pair_t);
254 if (unlikely(!vp)) {
255 fr_strerror_const("Out of memory");
256 return NULL;
257 }
258
259 if (unlikely(da->flags.is_unknown)) {
260 fr_strerror_const("Root attribute cannot be unknown");
262 return NULL;
263 }
264
267
268 return vp;
269}
270
271/** Dynamically allocate a new attribute and assign a #fr_dict_attr_t
272 *
273 * @note Will duplicate any unknown attributes passed as the da.
274 *
275 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t
276 * @param[in] da Specifies the dictionary attribute to build the #fr_pair_t from.
277 * If unknown, will be duplicated, with the memory being bound to
278 * the pair.
279 * @return
280 * - A new #fr_pair_t.
281 * - NULL if an error occurred.
282 * @hidecallergraph
283 */
284fr_pair_t *fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
285{
286 fr_pair_t *vp;
287
288 vp = fr_pair_alloc_null(ctx);
289 if (!vp) {
290 oom:
291 fr_strerror_const("Out of memory");
292 return NULL;
293 }
294
295 /*
296 * If we get passed an unknown da, we need to ensure that
297 * it's parented by "vp".
298 */
299 if (da->flags.is_unknown) {
300 fr_dict_attr_t const *unknown;
301
302 unknown = fr_dict_attr_unknown_copy(vp, da);
303 if (!unknown) {
305 goto oom;
306 }
307 da = unknown;
308 }
309
312
313 return vp;
314}
315
316/** Re-initialise an attribute with a different da
317 *
318 * If the new da has a different type to the old da, we'll attempt to cast
319 * the current value in place.
320 */
322{
323 fr_dict_attr_t const *to_free;
324
325 /*
326 * vp may be created from fr_pair_alloc_null(), in which case it has no da.
327 */
328 if (vp->da && !vp->da->flags.is_raw) {
329 if (vp->da == da) return 0;
330
331 if (!fr_type_is_leaf(vp->vp_type)) return -1;
332
333 if ((da->type != vp->vp_type) && (fr_value_box_cast_in_place(vp, &vp->data, da->type, da) < 0)) return -1;
334 } else {
335 fr_assert(fr_type_is_leaf(vp->vp_type) || (fr_type_is_structural(vp->vp_type) && (fr_pair_list_num_elements(&vp->vp_group) == 0)));
336
337 fr_value_box_init(&vp->data, da->type, da, false);
338 }
339
340 to_free = vp->da;
341
342 /*
343 * If the new da is unknown the destination needs its own copy
344 * to ensure lifetime consistency.
345 */
346 if (unlikely(da->flags.is_unknown)) {
348 } else {
349 vp->da = da;
350 }
351
352 /*
353 * Only frees unknown fr_dict_attr_t's
354 */
356
357 /*
358 * Ensure we update the attribute index in the parent.
359 */
360 if (list) {
361 fr_pair_remove(list, vp);
362
363 fr_pair_append(list, vp);
364 }
365
366#ifdef WITH_VERIFY_PTR
367 vp->verified = false;
368#endif
369 return 0;
370}
371
372/** Create a new valuepair
373 *
374 * If attr and vendor match a dictionary entry then a VP with that #fr_dict_attr_t
375 * will be returned.
376 *
377 * If attr or vendor are unknown will call dict_attruknown to create a dynamic
378 * #fr_dict_attr_t of #FR_TYPE_OCTETS.
379 *
380 * Which type of #fr_dict_attr_t the #fr_pair_t was created with can be determined by
381 * checking @verbatim vp->da->flags.is_unknown @endverbatim.
382 *
383 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
384 * @param[in] parent of the attribute being allocated (usually a dictionary or vendor).
385 * @param[in] attr number.
386 * @return
387 * - A new #fr_pair_t.
388 * - NULL on error.
389 */
390fr_pair_t *fr_pair_afrom_child_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
391{
392 fr_dict_attr_t const *da;
393 fr_pair_t *vp;
394
395 vp = fr_pair_alloc_null(ctx);
396 if (unlikely(!vp)) return NULL;
397
399 if (!da) {
400 fr_dict_attr_t *unknown;
401
403 if (!unknown) {
405 return NULL;
406 }
407 da = unknown;
408 }
409
412
413 return vp;
414}
415
416/** Create a pair (and all intermediate parents), and append it to the list
417 *
418 * Unlike fr_pair_afrom_da_nested(), this function starts off at an intermediate ctx and list.
419 *
420 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
421 * @param[out] list where the created pair is supposed to go.
422 * @param[in] da the da for the pair to create
423 * @param[in] start the starting depth. If start != 0, we must have ctx==vp at that depth, and list==&vp->vp_group
424 * @return
425 * - A new #fr_pair_t.
426 * - NULL on error.
427 */
428fr_pair_t *fr_pair_afrom_da_depth_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da, unsigned int start)
429{
430 fr_pair_t *vp;
431 unsigned int i;
432 TALLOC_CTX *cur_ctx;
433 fr_dict_attr_t const *find; /* DA currently being looked for */
434 fr_pair_list_t *cur_list; /* Current list being searched */
435 fr_da_stack_t da_stack;
436
437 /*
438 * Short-circuit the common case.
439 */
440 if (da->depth == (start + 1)) {
441 if (fr_pair_append_by_da(ctx, &vp, list, da) < 0) return NULL;
443 return vp;
444 }
445
446 fr_proto_da_stack_build(&da_stack, da);
447 cur_list = list;
448 cur_ctx = ctx;
449
450 for (i = start; i <= da->depth; i++) {
451 find = da_stack.da[i];
452
453 vp = fr_pair_find_by_da(cur_list, NULL, find);
454 if (!vp || (vp->da == da)) {
455 if (fr_pair_append_by_da(cur_ctx, &vp, cur_list, find) < 0) return NULL;
457 }
458
459 if (find == da) return vp;
460
462
463 cur_ctx = vp;
464 cur_list = &vp->vp_group;
465 }
466
467 fr_assert(0);
468
469 return NULL;
470}
471
472/** Create a pair (and all intermediate parents), and append it to the list
473 *
474 * If the relevant leaf pair already exists, then a new one is created.
475 *
476 * This function is similar to fr_pair_update_by_da_parent(), except that function requires
477 * a parent pair, and this one takes a separate talloc ctx and pair list.
478 *
479 * @param[in] ctx for allocated memory, usually a pointer to a #fr_packet_t.
480 * @param[out] list where the created pair is supposed to go.
481 * @param[in] da the da for the pair to create
482 * @return
483 * - A new #fr_pair_t.
484 * - NULL on error.
485 */
487{
488 if (da->depth <= 1) {
489 fr_pair_t *vp;
490
491 if (fr_pair_append_by_da(ctx, &vp, list, da) < 0) return NULL;
493 return vp;
494 }
495
496 return fr_pair_afrom_da_depth_nested(ctx, list, da, 0);
497}
498
499/** Copy a single valuepair
500 *
501 * Allocate a new valuepair and copy the da from the old vp.
502 *
503 * @param[in] ctx for talloc
504 * @param[in] vp to copy.
505 * @return
506 * - A copy of the input VP.
507 * - NULL on error.
508 */
509fr_pair_t *fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
510{
511 fr_pair_t *n;
512
514
515 n = fr_pair_afrom_da(ctx, vp->da);
516 if (!n) return NULL;
517
518 n->op = vp->op;
519
520 /*
521 * Groups are special.
522 */
523 if (fr_type_is_structural(n->vp_type)) {
524 if (fr_pair_list_copy(n, &n->vp_group, &vp->vp_group) < 0) {
525 error:
526 talloc_free(n);
527 return NULL;
528 }
529
530 } else {
531 if (unlikely(fr_value_box_copy(n, &n->data, &vp->data) < 0)) goto error;
532 }
534
535 return n;
536}
537
538/** Steal one VP
539 *
540 * @param[in] ctx to move fr_pair_t into
541 * @param[in] vp fr_pair_t to move into the new context.
542 */
543int fr_pair_steal(TALLOC_CTX *ctx, fr_pair_t *vp)
544{
545 fr_pair_t *nvp;
546
547 nvp = talloc_steal(ctx, vp);
548 if (unlikely(!nvp)) {
549 fr_strerror_printf("Failed moving pair %pV to new ctx", vp);
550 return -1;
551 }
552
553#ifdef WITH_VERIFY_PTR
554 vp->verified = false;
555#endif
556 return 0;
557}
558
559#define IN_A_LIST_MSG "Pair %pV is already in a list, and cannot be moved"
560#define NOT_IN_THIS_LIST_MSG "Pair %pV is not in the given list"
561
562/** Change a vp's talloc ctx and insert it into a new list
563 *
564 * @param[in] list_ctx to move vp into.
565 * @param[out] list to add vp to.
566 * @param[in] vp to move.
567 * @return
568 * - 0 on success.
569 * - -1 on failure (already in list).
570 */
571int fr_pair_steal_append(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
572{
573 if (fr_pair_order_list_in_a_list(vp)) {
575 return -1;
576 }
577
578 if (unlikely(fr_pair_steal(list_ctx, vp) < 0)) return -1;
579
580 if (unlikely(fr_pair_append(list, vp) < 0)) return -1;
581
582 return 0;
583}
584
585/** Change a vp's talloc ctx and insert it into a new list
586 *
587 * @param[in] list_ctx to move vp into.
588 * @param[out] list to add vp to.
589 * @param[in] vp to move.
590 * @return
591 * - 0 on success.
592 * - -1 on failure (already in list).
593 */
594int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
595{
596 if (fr_pair_order_list_in_a_list(vp)) {
598 return -1;
599 }
600
601 if (unlikely(fr_pair_steal(list_ctx, vp) < 0)) return -1;
602
603 if (unlikely(fr_pair_prepend(list, vp) < 0)) return -1;
604
605 return 0;
606}
607
608/** Mark malformed attribute as raw
609 *
610 * @param[in] vp to mark as raw.
611 * @param[in] data to parse.
612 * @param[in] data_len of data to parse.
613 *
614 * @return
615 * - 0 on success
616 * - -1 on failure.
617 */
618int fr_pair_raw_afrom_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len)
619{
620 fr_dict_attr_t *unknown;
621
623
624 if (!fr_cond_assert(vp->da->flags.is_unknown == false)) return -1;
625
626 if (!fr_cond_assert(vp->da->parent != NULL)) return -1;
627
629 if (!unknown) return -1;
630
631 fr_value_box_clear(&vp->data);
632
633 vp->da = unknown;
634 fr_assert(vp->da->type == FR_TYPE_OCTETS);
635
636 return fr_value_box_memdup(vp, &vp->data, NULL, data, data_len, true);
637}
638
639/** Iterate over pairs with a specified da
640 *
641 * @param[in] cursor to iterate over
642 * @param[in] current The fr_pair_t cursor->current. Will be advanced and checked to
643 * see if it matches the specified fr_dict_attr_t.
644 * @param[in] uctx The fr_dict_attr_t to search for.
645 * @return
646 * - Next matching fr_pair_t.
647 * - NULL if not more matching fr_pair_ts could be found.
648 */
649static void *fr_pair_iter_next_by_da(fr_dcursor_t *cursor, void *current, void *uctx)
650{
651 fr_pair_t *c = current;
652 fr_dict_attr_t *da = uctx;
653
654 while ((c = fr_dcursor_list_next(cursor, c))) {
655 PAIR_VERIFY(c);
656 if (c->da == da) break;
657 }
658
659 return c;
660}
661
662/** Iterate over pairs which are decedents of the specified da
663 *
664 * @param[in] cursor to iterate over.
665 * @param[in] current The fr_pair_t cursor->current. Will be advanced and checked to
666 * see if it matches the specified fr_dict_attr_t.
667 * @param[in] uctx The fr_dict_attr_t to search for.
668 * @return
669 * - Next matching fr_pair_t.
670 * - NULL if not more matching fr_pair_ts could be found.
671 */
672static void *fr_pair_iter_next_by_ancestor(fr_dcursor_t *cursor, void *current, void *uctx)
673{
674 fr_pair_t *c = current;
675 fr_dict_attr_t *da = uctx;
676
677 while ((c = fr_dcursor_list_next(cursor, c))) {
678 PAIR_VERIFY(c);
679 if (fr_dict_attr_common_parent(da, c->da, true)) break;
680 }
681
682 return c;
683}
684
685/** Return the number of instances of a given da in the specified list
686 *
687 * @param[in] list to search in.
688 * @param[in] da to look for in the list.
689 * @return
690 * - 0 if no instances exist.
691 * - >0 the number of instance of a given attribute.
692 */
693unsigned int fr_pair_count_by_da(fr_pair_list_t const *list, fr_dict_attr_t const *da)
694{
695 fr_pair_t *vp = NULL;
696 unsigned int count = 0;
697
698 if (fr_pair_list_empty(list)) return 0;
699
700 while ((vp = fr_pair_list_next(list, vp))) if (da == vp->da) count++;
701
702 return count;
703}
704
705/** Find the first pair with a matching da
706 *
707 * @param[in] list to search in.
708 * @param[in] prev the previous attribute in the list.
709 * @param[in] da the next da to find.
710 * @return
711 * - first matching fr_pair_t.
712 * - NULL if no fr_pair_ts match.
713 *
714 * @hidecallergraph
715 */
717{
718 fr_pair_t *vp = UNCONST(fr_pair_t *, prev);
719
720 if (fr_pair_list_empty(list)) return NULL;
721
722 PAIR_LIST_VERIFY(list);
723
724 while ((vp = fr_pair_list_next(list, vp))) if (da == vp->da) return vp;
725
726 return NULL;
727}
728
729/** Find the last pair with a matching da
730 *
731 * @param[in] list to search in.
732 * @param[in] da the da to find
733 * @return
734 * - first matching #fr_pair_t.
735 * - NULL if no #fr_pair_t's match.
736 *
737 * @hidecallergraph
738 */
740{
741 fr_pair_t *vp = NULL;
742
743 if (fr_pair_list_empty(list)) return NULL;
744
745 PAIR_LIST_VERIFY(list);
746
747 while ((vp = fr_pair_list_prev(list, vp))) if (da == vp->da) return vp;
748
749 return NULL;
750}
751
752/** Find a pair with a matching da at a given index
753 *
754 * @param[in] list to search in.
755 * @param[in] da to look for in the list.
756 * @param[in] idx Instance of the attribute to return.
757 * @return
758 * - first matching fr_pair_t.
759 * - NULL if no fr_pair_ts match.
760 *
761 * @hidecallergraph
762 */
763fr_pair_t *fr_pair_find_by_da_idx(fr_pair_list_t const *list, fr_dict_attr_t const *da, unsigned int idx)
764{
765 fr_pair_t *vp = NULL;
766
767 if (fr_pair_list_empty(list)) return NULL;
768
769 PAIR_LIST_VERIFY(list);
770
771 while ((vp = fr_pair_list_next(list, vp))) {
772 if (da != vp->da) continue;
773
774 if (idx == 0) return vp;
775
776 idx--;
777 }
778 return NULL;
779}
780
781/** Find a pair with a matching fr_dict_attr_t, by walking the nested fr_dict_attr_t tree
782 *
783 * The list should be the one containing the top level attributes.
784 *
785 * @param[in] list to search in.
786 * @param[in] da the next da to find.
787 * @return
788 * - first matching fr_pair_t.
789 * - NULL if no fr_pair_ts match.
790 */
792{
793 fr_pair_t *vp;
794 fr_dict_attr_t const **find; /* DA currently being looked for */
795 fr_pair_list_t const *cur_list; /* Current list being searched */
796 fr_da_stack_t da_stack;
797
798 if (fr_pair_list_empty(list)) return NULL;
799
800 /*
801 * In the common case, we're looking for attributes in
802 * the root (at level 1), so we just skip to a special
803 * function for that
804 */
805 if (da->depth <= 1) return fr_pair_find_by_da(list, NULL, da);
806
807 fr_proto_da_stack_build(&da_stack, da);
808
809 /*
810 * Start at the top of the list, and at the top of the nesting.
811 */
812 cur_list = list;
813 find = &da_stack.da[0];
814 vp = NULL;
815
816 /*
817 * Loop over the list at each level until we find a matching da.
818 */
819 while (true) {
820 fr_pair_t *next;
821
822 fr_assert((*find)->depth <= da->depth);
823
824 /*
825 * Find a vp which matches a given da. If found,
826 * recurse into the child list to find the child
827 * attribute.
828 *
829 */
830 next = fr_pair_find_by_da(cur_list, vp, *find);
831 if (next) {
832 /*
833 * We've found a match for the requested
834 * da - return it.
835 */
836 if ((*find) == da) return next;
837
838 /*
839 * Prepare to search the next level.
840 */
841 cur_list = &next->vp_group;
842 find++;
843 vp = NULL;
844 continue;
845 }
846
847 /*
848 * We hit the end of the top-level list. Therefore we found nothing.
849 */
850 if (cur_list == list) break;
851
852 /*
853 * We hit the end of *A* list. Go to the parent
854 * VP, and then find its list.
855 *
856 * We still then have to go to the next attribute
857 * in the parent list, as we've checked all of the
858 * children of this VP.
859 */
860 find--;
861 vp = fr_pair_list_parent(cur_list);
862 cur_list = fr_pair_parent_list(vp);
863 }
864
865 /*
866 * We have the right list, go find the da.
867 */
868 return fr_pair_find_by_da(list, NULL, da);
869}
870
871/** Find the pair with the matching child attribute at a given index
872 *
873 * @param[in] list in which to search.
874 * @param[in] parent attribute in which to lookup child.
875 * @param[in] attr id of child.
876 * @param[in] idx Instance of the attribute to return.
877 * @return
878 * - first matching value pair.
879 * - NULL if no pair found.
880 */
882 fr_dict_attr_t const *parent, unsigned int attr, unsigned int idx)
883{
884 fr_dict_attr_t const *da;
885
886 /* List head may be NULL if it contains no VPs */
887 if (fr_pair_list_empty(list)) return NULL;
888
889 PAIR_LIST_VERIFY(list);
890
892 if (!da) return NULL;
893
894 return fr_pair_find_by_da_idx(list, da, idx);
895}
896
897/** Get the child list of a group
898 *
899 * @param[in] vp which MUST be of a type
900 * that can contain children.
901 * @return
902 * - NULL on error
903 * - pointer to head of the child list.
904 */
906{
907 if (!fr_type_is_structural(vp->vp_type)) return NULL;
908
909 return &vp->vp_group;
910}
911
912/** Return a pointer to the parent pair list
913 *
914 */
916{
917 return fr_pair_order_list_parent_list(vp);
918}
919
920/** Return a pointer to the parent pair.
921 *
922 */
924{
926
927 if (!list) return NULL;
928
929 if (!list->is_child) return NULL;
930
931 return (fr_pair_t *) (UNCONST(uint8_t *, list) - offsetof(fr_pair_t, vp_group));
932}
933
934/** Return a pointer to the parent pair which contains this list.
935 *
936 */
938{
939 if (!list) return NULL;
940
941 if (!list->is_child) return NULL;
942
943 return (fr_pair_t *) (UNCONST(uint8_t *, list) - offsetof(fr_pair_t, vp_group));
944}
945
946/** Keep attr tree and sublists synced on cursor insert
947 *
948 * @param[in] cursor the cursor being modified
949 * @param[in] to_insert fr_pair_t being inserted.
950 * @param[in] uctx fr_pair_list_t containing the order list.
951 * @return
952 * - 0 on success.
953 */
954static int _pair_list_dcursor_insert(fr_dcursor_t *cursor, void *to_insert, UNUSED void *uctx)
955{
956 fr_pair_t *vp = to_insert;
957
958 /*
959 * Mark the pair as inserted into the list.
960 */
961 fr_pair_order_list_set_head_from_dlist(fr_dcursor_list(cursor), vp);
962
964
965 return 0;
966}
967
968/** Keep attr tree and sublists synced on cursor removal
969 *
970 * @param[in] cursor the cursor being modified
971 * @param[in] to_remove fr_pair_t being removed.
972 * @param[in] uctx fr_pair_list_t containing the order list.
973 * @return
974 * - 0 on success.
975 */
976static int _pair_list_dcursor_remove(fr_dcursor_t *cursor, void *to_remove, UNUSED void *uctx)
977{
978 fr_pair_t *vp = to_remove;
980
981 /*
982 * Mark the pair as removed from the list.
983 */
984 fr_pair_order_list_set_head(NULL, vp);
985
987
988 /*
989 * If the VP is in the cursor, then the cursor code will
990 * take care of removing it.
991 */
992 if (fr_pair_order_list_dlist_head(&parent->order) == fr_dcursor_list(cursor)) return 0;
993
995 return 1;
996}
997
998/** Iterates over the leaves of a list
999 *
1000 * @param[in] list to iterate over.
1001 * @param[in] vp the current CVP
1002 * @return
1003 * - NULL when done
1004 * - vp - a leaf pair
1005 */
1007{
1008 fr_pair_t *next, *parent;
1009 fr_pair_list_t *parent_list;
1010
1011 /*
1012 * Start: return the head of the top-level list.
1013 */
1014 if (!vp) {
1015 vp = fr_pair_list_head(list);
1016 if (!vp) goto next_parent_sibling;
1017
1018 next_sibling:
1019 if (fr_type_is_leaf(vp->vp_type)) return vp;
1020
1022
1023 vp = fr_pair_list_iter_leaf(&vp->vp_group, NULL);
1024 if (vp) return vp;
1025
1026 /*
1027 * vp is NULL, so we've processed all of its children.
1028 */
1029 }
1030
1031 /*
1032 * Go to the next sibling in the parent list of vp.
1033 */
1034next_parent_sibling:
1035 parent_list = fr_pair_parent_list(vp);
1036 if (!parent_list) return NULL;
1037
1038 next = fr_pair_list_next(parent_list, vp);
1039 if (!next) {
1040 /*
1041 * We're done the top-level list.
1042 */
1043 if (parent_list == list) return NULL;
1044
1046 fr_assert(&parent->vp_group == parent_list);
1047 vp = parent;
1048 goto next_parent_sibling;
1049 }
1050
1051 /*
1052 * We do have a "next" attribute. Go check if we can return it.
1053 */
1054 vp = next;
1055 goto next_sibling;
1056}
1057
1058/** Initialises a special dcursor with callbacks that will maintain the attr sublists correctly
1059 *
1060 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1061 *
1062 * @param[out] cursor to initialise.
1063 * @param[in] list to iterate over.
1064 * @param[in] iter Iterator to use when filtering pairs.
1065 * @param[in] uctx To pass to iterator.
1066 * @param[in] is_const whether the fr_pair_list_t is const.
1067 * @return
1068 * - NULL if src does not point to any items.
1069 * - The first pair in the list.
1070 */
1072 fr_dcursor_iter_t iter, void const *uctx,
1073 bool is_const)
1074{
1075 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1076 iter, NULL, uctx,
1078}
1079
1080/** Initialises a special dcursor with callbacks that will maintain the attr sublists correctly
1081 *
1082 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1083 *
1084 * @param[out] cursor to initialise.
1085 * @param[in] list to iterate over.
1086 * @param[in] is_const whether the fr_pair_list_t is const.
1087 * @return
1088 * - NULL if src does not point to any items.
1089 * - The first pair in the list.
1090 */
1092 bool is_const)
1093{
1094 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1095 NULL, NULL, NULL,
1097}
1098
1099/** Initialise a cursor that will return only attributes matching the specified #fr_dict_attr_t
1100 *
1101 * @param[in] cursor to initialise.
1102 * @param[in] list to iterate over.
1103 * @param[in] da to search for.
1104 * @param[in] is_const whether the fr_pair_list_t is const.
1105 * @return
1106 * - The first matching pair.
1107 * - NULL if no pairs match.
1108 */
1110 fr_pair_list_t const *list, fr_dict_attr_t const *da,
1111 bool is_const)
1112{
1113 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1114 fr_pair_iter_next_by_da, NULL, da,
1116}
1117
1118/** Initialise a cursor that will return only attributes descended from the specified #fr_dict_attr_t
1119 *
1120 * @param[in] cursor to initialise.
1121 * @param[in] list to iterate over.
1122 * @param[in] da who's descendants to search for.
1123 * @param[in] is_const whether the fr_pair_list_t is const.
1124 * @return
1125 * - The first matching pair.
1126 * - NULL if no pairs match.
1127 */
1129 fr_pair_list_t const *list, fr_dict_attr_t const *da,
1130 bool is_const)
1131{
1132 fr_pair_t *vp;
1133
1135
1136 /*
1137 * This function is only used by snmp.c and password.c. Once we've fully moved to
1138 * nested attributes, it should be removed.
1139 */
1140 fr_assert(da->parent->flags.is_root);
1141
1142 vp = fr_pair_find_by_da(list, NULL, da);
1143 if (vp) {
1144 list = &vp->vp_group;
1145
1146 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1147 NULL, NULL, NULL,
1149 }
1150
1151 return _fr_dcursor_init(cursor, fr_pair_order_list_dlist_head(&list->order),
1154}
1155
1156/** Iterate over pairs
1157 *
1158 * @param[in] cursor to iterate over.
1159 * @param[in] current The fr_value_box_t cursor->current. Will be advanced and checked to
1160 * see if it matches the specified fr_dict_attr_t.
1161 * @param[in] uctx unused
1162 * @return
1163 * - Next matching fr_pair_t.
1164 * - NULL if not more matching fr_pair_ts could be found.
1165 */
1166static void *_fr_pair_iter_next_value(fr_dcursor_t *cursor, void *current, UNUSED void *uctx)
1167{
1168 fr_pair_t *vp;
1169
1170 if (!current) {
1171 vp = NULL;
1172 } else {
1173 vp = (fr_pair_t *) ((uint8_t *) current - offsetof(fr_pair_t, data));
1174 PAIR_VERIFY(vp);
1175 }
1176
1177 while ((vp = fr_dcursor_list_next(cursor, vp))) {
1178 PAIR_VERIFY(vp);
1179 if (fr_type_is_leaf(vp->vp_type)) return &vp->data;
1180 }
1181
1182 return NULL;
1183}
1184
1185/*
1186 * The value dcursor just runs the iterator, and never uses the dlist. Inserts and deletes are forbidden.
1187 *
1188 * However, the underlying dcursor code needs a dlist, so we create a fake one to pass it. In debug
1189 * builds, the dcursor code will do things like try to check talloc types. So we need to pass it an
1190 * empty dlist with no talloc types.
1191 */
1193 .offset = offsetof(fr_dlist_head_t, entry),
1194 .type = NULL,
1195 .num_elements = 0,
1196 .entry = {
1197 .prev = &value_dlist.entry,
1199 },
1200};
1201
1202/** Initialises a special dcursor over a #fr_pair_list_t, but which returns #fr_value_box_t
1203 *
1204 * @note This is the only way to use a dcursor in non-const mode with fr_pair_list_t.
1205 * @note - the list cannot be modified, and structural attributes are not returned.
1206 *
1207 * @param[out] cursor to initialise.
1208 * @return
1209 * - NULL if src does not point to any items.
1210 * - The first pair in the list.
1211 */
1213{
1214 return _fr_dcursor_init(cursor, &value_dlist,
1215 _fr_pair_iter_next_value, NULL, NULL, NULL, NULL, NULL, true);
1216}
1217
1218/** Iterate over pairs
1219 *
1220 * @param[in] cursor to iterate over.
1221 * @param[in] current The fr_value_box_t cursor->current. Will be advanced and checked to
1222 * see if it matches the specified fr_dict_attr_t.
1223 * @param[in] uctx The parent dcursor
1224 * @return
1225 * - Next matching fr_pair_t.
1226 * - NULL if not more matching fr_pair_ts could be found.
1227 */
1228static void *_fr_pair_iter_next_dcursor_value(UNUSED fr_dcursor_t *cursor, void *current, void *uctx)
1229{
1230 fr_pair_t *vp;
1231 fr_dcursor_t *parent = uctx;
1232
1233 if (!current) {
1235 if (!vp) return NULL;
1236 goto check;
1237 }
1238
1239 while ((vp = fr_dcursor_next(parent))) {
1240 check:
1241 PAIR_VERIFY(vp);
1242
1243 if (fr_type_is_leaf(vp->vp_type)) return &vp->data;
1244 }
1245
1246 return NULL;
1247}
1248
1249/** Initialises a special dcursor over another cursor which returns #fr_pair_t, but we return #fr_value_box_t
1250 *
1251 * @note - the list cannot be modified, and structural attributes are not returned.
1252 *
1253 * @param[out] cursor to initialise.
1254 * @param[in] parent to iterate over
1255 * @return
1256 * - NULL if src does not point to any items.
1257 * - The first pair in the list.
1258 */
1264
1265/** Add a VP to the start of the list.
1266 *
1267 * Links an additional VP 'add' at the beginning a list.
1268 *
1269 * @param[in] list VP in linked list. Will add new VP to this list.
1270 * @param[in] to_add VP to add to list.
1271 * @return
1272 * - 0 on success.
1273 * - -1 on failure (pair already in list).
1274 */
1276{
1277 PAIR_VERIFY(to_add);
1278
1279#ifdef WITH_VERIFY_PTR
1280 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1281 list->verified = false;
1282#endif
1283
1284 if (fr_pair_order_list_in_a_list(to_add)) {
1286 return -1;
1287 }
1288
1289 fr_pair_order_list_insert_head(&list->order, to_add);
1290
1291 return 0;
1292}
1293
1294/** Add a VP to the end of the list.
1295 *
1296 * Links an additional VP 'to_add' at the end of a list.
1297 *
1298 * @param[in] list VP in linked list. Will add new VP to this list.
1299 * @param[in] to_add VP to add to list.
1300 * @return
1301 * - 0 on success.
1302 * - -1 on failure (pair already in list).
1303 *
1304 * @hidecallergraph
1305 */
1307{
1308#ifdef WITH_VERIFY_PTR
1309 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1310 list->verified = false;
1311#endif
1312
1313 if (fr_pair_order_list_in_a_list(to_add)) {
1315 return -1;
1316 }
1317
1318 fr_pair_order_list_insert_tail(&list->order, to_add);
1319
1320 return 0;
1321}
1322
1323/** Add a VP after another VP.
1324 *
1325 * @param[in] list VP in linked list. Will add new VP to this list.
1326 * @param[in] pos to insert pair after.
1327 * @param[in] to_add VP to add to list.
1328 * @return
1329 * - 0 on success.
1330 * - -1 on failure (pair already in list).
1331 */
1333{
1334 PAIR_VERIFY(to_add);
1335
1336#ifdef WITH_VERIFY_PTR
1337 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1338 list->verified = false;
1339#endif
1340
1341 if (fr_pair_order_list_in_a_list(to_add)) {
1343 return -1;
1344 }
1345
1346 if (pos && !fr_pair_order_list_in_list(&list->order, pos)) {
1348 return -1;
1349 }
1350
1351 fr_pair_order_list_insert_after(&list->order, pos, to_add);
1352
1353 return 0;
1354}
1355
1356/** Add a VP before another VP.
1357 *
1358 * @param[in] list VP in linked list. Will add new VP to this list.
1359 * @param[in] pos to insert pair after.
1360 * @param[in] to_add VP to add to list.
1361 * @return
1362 * - 0 on success.
1363 * - -1 on failure (pair already in list).
1364 */
1366{
1367 PAIR_VERIFY(to_add);
1368
1369#ifdef WITH_VERIFY_PTR
1370 fr_assert(!fr_pair_order_list_in_a_list(to_add));
1371 fr_assert(!pos || fr_pair_order_list_in_a_list(pos));
1372 list->verified = false;
1373#endif
1374
1375 if (fr_pair_order_list_in_a_list(to_add)) {
1377 return -1;
1378 }
1379
1380 if (pos && !fr_pair_order_list_in_list(&list->order, pos)) {
1382 return -1;
1383 }
1384
1385 fr_pair_order_list_insert_before(&list->order, pos, to_add);
1386
1387 return 0;
1388}
1389
1390/** Replace a given VP
1391 *
1392 * @note Memory used by the VP being replaced will be freed.
1393 *
1394 * @param[in,out] list pair list
1395 * @param[in] to_replace pair to replace and free, on list
1396 * @param[in] vp New pair to insert.
1397 */
1399{
1400 PAIR_VERIFY_WITH_LIST(list, to_replace);
1401 PAIR_VERIFY(vp);
1402
1403#ifdef WITH_VERIFY_PTR
1404 fr_assert(!fr_pair_order_list_in_a_list(vp));
1405 fr_assert(fr_pair_order_list_in_a_list(to_replace));
1406 list->verified = false;
1407#endif
1408
1409 fr_pair_insert_after(list, to_replace, vp);
1410 fr_pair_remove(list, to_replace);
1411 talloc_free(to_replace);
1412}
1413
1414/** Alloc a new fr_pair_t (and append)
1415 *
1416 * @param[in] ctx to allocate new #fr_pair_t in.
1417 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1418 * care about manipulating the fr_pair_t.
1419 * @param[in,out] list in which to append the pair.
1420 * @param[in] da of attribute to create.
1421 * @return
1422 * - 0 on success.
1423 * - -1 on failure.
1424 */
1425int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
1426{
1427 fr_pair_t *vp;
1428
1429 vp = fr_pair_afrom_da(ctx, da);
1430 if (unlikely(!vp)) {
1431 if (out) *out = NULL;
1432 return -1;
1433 }
1434
1435 fr_pair_append(list, vp);
1436 if (out) *out = vp;
1437
1438 return 0;
1439}
1440
1441/** Alloc a new fr_pair_t (and prepend)
1442 *
1443 * @param[in] ctx to allocate new #fr_pair_t in.
1444 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1445 * care about manipulating the fr_pair_t.
1446 * @param[in,out] list in which to prepend the pair.
1447 * @param[in] da of attribute to create.
1448 * @return
1449 * - 0 on success.
1450 * - -1 on failure.
1451 */
1452int fr_pair_prepend_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
1453{
1454 fr_pair_t *vp;
1455
1456 vp = fr_pair_afrom_da(ctx, da);
1457 if (unlikely(!vp)) {
1458 if (out) *out = NULL;
1459 return -1;
1460 }
1461
1462 fr_pair_prepend(list, vp);
1463 if (out) *out = vp;
1464
1465 return 0;
1466}
1467
1468/** Alloc a new fr_pair_t, adding the parent attributes if required
1469 *
1470 * A child pair will be added to the first available matching parent
1471 * found.
1472 *
1473 * @param[in] ctx to allocate new #fr_pair_t in
1474 * @param[out] out Pair we allocated. May be NULL if the caller doesn't
1475 * care about manipulating the fr_pair_t.
1476 * @param[in] list in which to insert the pair.
1477 * @param[in] da of the attribute to create.
1478 * @return
1479 * - 0 on success.
1480 * - -1 on failure.
1481 */
1483{
1484 fr_pair_t *vp = NULL;
1485 fr_da_stack_t da_stack;
1486 fr_dict_attr_t const **find;
1487 TALLOC_CTX *pair_ctx = ctx;
1488
1489 /*
1490 * Fast path for non-nested attributes
1491 */
1492 if (da->depth <= 1) return fr_pair_append_by_da(ctx, out, list, da);
1493
1494 fr_proto_da_stack_build(&da_stack, da);
1495 find = &da_stack.da[0];
1496
1497 /*
1498 * Walk down the da stack looking for candidate parent
1499 * attributes and then allocating the leaf.
1500 */
1501 while (true) {
1502 fr_assert((*find)->depth <= da->depth);
1503
1504 /*
1505 * We're not at the leaf, look for a potential parent
1506 */
1507 if ((*find) != da) vp = fr_pair_find_by_da(list, NULL, *find);
1508
1509 /*
1510 * Nothing found, create the pair
1511 */
1512 if (!vp) {
1513 if (fr_pair_append_by_da(pair_ctx, &vp, list, *find) < 0) {
1514 if (out) *out = NULL;
1515 return -1;
1516 }
1518 }
1519
1520 /*
1521 * We're at the leaf, return
1522 */
1523 if ((*find) == da) {
1524 if(out) *out = vp;
1525 return 0;
1526 }
1527
1528 /*
1529 * Prepare for next level
1530 */
1531 list = &vp->vp_group;
1532 pair_ctx = vp;
1533 vp = NULL;
1534 find++;
1535 }
1536}
1537
1538/** Return the first fr_pair_t matching the #fr_dict_attr_t or alloc a new fr_pair_t and its subtree (and append)
1539 *
1540 * @param[in] parent If parent->da is an ancestor of the specified
1541 * da, we continue building out the nested structure
1542 * from the parent.
1543 * If parent is NOT an ancestor, then it must be a group
1544 * attribute, and we will append the shallowest member
1545 * of the struct or TLV as a child, and build out everything
1546 * to the specified da.
1547 * @param[out] out Pair we allocated or found. May be NULL if the caller doesn't
1548 * care about manipulating the fr_pair_t.
1549 * @param[in] da of attribute to locate or alloc.
1550 * @return
1551 * - 1 if attribute already existed.
1552 * - 0 if we allocated a new attribute.
1553 * - -1 on memory allocation failure.
1554 * - -2 if the parent is not a group attribute.
1555 */
1557 fr_dict_attr_t const *da)
1558{
1559 fr_pair_t *vp = NULL;
1560 fr_da_stack_t da_stack;
1561 fr_dict_attr_t const **find; /* ** to allow us to iterate */
1562 TALLOC_CTX *pair_ctx = parent;
1563 fr_pair_list_t *list = &parent->vp_group;
1564
1565 /*
1566 * Fast path for non-nested attributes
1567 */
1568 if (da->depth <= 1) {
1569 vp = fr_pair_find_by_da(list, NULL, da);
1570 if (vp) {
1571 if (out) *out = vp;
1572 return 1;
1573 }
1574
1575 return fr_pair_append_by_da(parent, out, list, da);
1576 }
1577
1578 fr_proto_da_stack_build(&da_stack, da);
1579 /*
1580 * Is parent an ancestor of the attribute we're trying
1581 * to build? If so, we resume from the deepest pairs
1582 * already created.
1583 *
1584 * da stack excludes the root.
1585 */
1586 if ((parent->da->depth < da->depth) && (da_stack.da[parent->da->depth - 1] == parent->da)) {
1587 /*
1588 * Start our search from the parent's children
1589 */
1590 list = &parent->vp_group;
1591 find = &da_stack.da[parent->da->depth]; /* Next deepest attr than parent */
1592 /*
1593 * Disallow building one TLV tree into another
1594 */
1595 } else if (!fr_type_is_group(parent->da->type)) {
1596 fr_strerror_printf("Expected parent \"%s\" to be an ancestor of \"%s\" or a group. "
1597 "But it is not an ancestor and is of type %s", parent->da->name, da->name,
1598 fr_type_to_str(parent->da->type));
1599 return -2;
1600 } else {
1601 find = &da_stack.da[0];
1602 }
1603
1604 /*
1605 * Walk down the da stack looking for candidate parent
1606 * attributes and then allocating the leaf, and any
1607 * attributes between the leaf and parent.
1608 */
1609 while (true) {
1610 fr_assert((*find)->depth <= da->depth);
1611
1612 vp = fr_pair_find_by_da(list, NULL, *find);
1613 /*
1614 * Nothing found at this level, create the pair
1615 */
1616 if (!vp) {
1617 if (fr_pair_append_by_da(pair_ctx, &vp, list, *find) < 0) {
1618 if (out) *out = NULL;
1619 return -1;
1620 }
1622 }
1623
1624 /*
1625 * We're at the leaf, return
1626 */
1627 if ((*find) == da) {
1628 if (out) *out = vp;
1629 return 0;
1630 }
1631
1632 /*
1633 * Prepare for next level
1634 */
1635 list = &vp->vp_group;
1636 pair_ctx = vp;
1637 vp = NULL;
1638 find++;
1639 }
1640}
1641
1642/** Delete matching pairs from the specified list
1643 *
1644 * @param[in,out] list to search for attributes in or delete attributes from.
1645 * @param[in] da to match.
1646 * @return
1647 * - >0 the number of pairs deleted.
1648 * - 0 if no pairs were deleted.
1649 */
1651{
1652 int cnt = 0;
1653
1654 fr_pair_list_foreach(list, vp) {
1655 if (da == vp->da) {
1656 if (fr_pair_immutable(vp)) continue;
1657
1658 cnt++;
1659 fr_pair_delete(list, vp);
1660 }
1661 }
1662
1663 return cnt;
1664}
1665
1666/** Delete matching pairs from the specified list, and prune any empty branches
1667 *
1668 * @param[in,out] list to search for attributes in or delete attributes from.
1669 * @param[in] da to match.
1670 * @return
1671 * - >0 the number of pairs deleted.
1672 * - 0 if no pairs were deleted.
1673 */
1675{
1676 int cnt = 0;
1677 fr_pair_t *vp;
1678 fr_dict_attr_t const **find; /* DA currently being looked for */
1679 fr_pair_list_t *cur_list; /* Current list being searched */
1680 fr_da_stack_t da_stack;
1681
1682 /*
1683 * Fast path for non-nested attributes
1684 */
1685 if (da->depth <= 1) return fr_pair_delete_by_da(list, da);
1686
1687 /*
1688 * No pairs, fast path!
1689 */
1690 if (fr_pair_list_empty(list)) return 0;
1691
1692 /*
1693 * Similar to fr_pair_find_by_da_nested()
1694 */
1695 fr_proto_da_stack_build(&da_stack, da);
1696 cur_list = list;
1697 find = &da_stack.da[0];
1698 vp = NULL;
1699
1700 /*
1701 * Loop over the list at each level until we find a matching da.
1702 */
1703 while (true) {
1704 fr_pair_t *next;
1705
1706 fr_assert((*find)->depth <= da->depth);
1707
1708 /*
1709 * Find a vp which matches a given da. If found,
1710 * recurse into the child list to find the child
1711 * attribute.
1712 *
1713 */
1714 next = fr_pair_find_by_da(cur_list, vp, *find);
1715 if (next) {
1716 /*
1717 * We've found a match for the requested
1718 * da - delete it
1719 */
1720 if ((*find) == da) {
1721 do {
1722 fr_pair_delete(cur_list, next);
1723 cnt++;
1724 } while ((next = fr_pair_find_by_da(cur_list, vp, *find)) != NULL);
1725
1726 return cnt;
1727 }
1728
1729 /*
1730 * Prepare to search the next level.
1731 */
1732 cur_list = &next->vp_group;
1733 find++;
1734 vp = NULL;
1735 continue;
1736 }
1737
1738 /*
1739 * We hit the end of the top-level list. Therefore we found nothing.
1740 */
1741 if (cur_list == list) break;
1742
1743 /*
1744 * We hit the end of *A* list. Go to the parent
1745 * VP, and then find its list.
1746 *
1747 * We still then have to go to the next attribute
1748 * in the parent list, as we've checked all of the
1749 * children of this VP.
1750 */
1751 find--;
1752 vp = fr_pair_list_parent(cur_list);
1753 cur_list = fr_pair_parent_list(vp);
1754 }
1755
1756 return fr_pair_delete_by_da(list, da);
1757}
1758
1759/** Delete matching pairs from the specified list
1760 *
1761 * @param[in] list to delete attributes from.
1762 * @param[in] parent to match.
1763 * @param[in] attr to match.
1764 * @return
1765 * - >0 the number of pairs deleted.
1766 * - 0 if no pairs were delete.
1767 * - -1 if we couldn't resolve the attribute number.
1768 */
1770{
1771 fr_dict_attr_t const *da;
1772
1774 if (!da) return -1;
1775
1776 return fr_pair_delete_by_da(list, da);
1777}
1778
1779/** Remove fr_pair_t from a list and free
1780 *
1781 * @param[in] list of value pairs to remove VP from.
1782 * @param[in] vp to remove
1783 * @return
1784 * - <0 on error: pair wasn't deleted
1785 * - 0 on success
1786 */
1788{
1789 fr_pair_remove(list, vp);
1790 return talloc_free(vp);
1791}
1792
1793/** Order attributes by their da
1794 *
1795 * Useful where attributes need to be aggregated, but not necessarily
1796 * ordered by attribute number.
1797 *
1798 * @param[in] a first #fr_pair_t
1799 * @param[in] b second #fr_pair_t
1800 * @return
1801 * - +1 if a > b
1802 * - 0 if a == b
1803 * - -1 if a < b
1804 */
1805fr_cmp_ret_t fr_pair_cmp_by_da(void const *a, void const *b)
1806{
1807 fr_pair_t const *my_a = a;
1808 fr_pair_t const *my_b = b;
1809
1810 PAIR_VERIFY(my_a);
1811 PAIR_VERIFY(my_b);
1812
1813 return CMP(my_a->da, my_b->da);
1814}
1815
1816#ifdef WITH_VERIFY_PTR
1817/** Order attributes by their da, with minimal checks.
1818 *
1819 * This is a variant of #fr_pair_cmp_by_da, which does NOT call
1820 * talloc_get_type_abort(), or PAIR_VERIFY(). If the caller puts a
1821 * #fr_pair_t onto the stack, then it doesn't have a talloc type, so
1822 * we can't call talloc_get_type_abort().
1823 *
1824 * Alternatively, if the list is being modified, PAIR_VERIFY() walks
1825 * the list being modified, which can end up with problems. As a
1826 * result, this code takes more chances than fr_pair_cmp_by_da().
1827 *
1828 * @param[in] a first #fr_pair_t
1829 * @param[in] b second #fr_pair_t
1830 * @return
1831 * - +1 if a > b
1832 * - 0 if a == b
1833 * - -1 if a < b
1834 */
1835fr_cmp_ret_t fr_pair_cmp_by_da_mutable(void const *a, void const *b)
1836{
1837 fr_pair_t const *my_a = a;
1838 fr_pair_t const *my_b = b;
1839
1840 return CMP(my_a->da, my_b->da);
1841}
1842#endif
1843
1844/** Order attributes by their attribute number, and tag
1845 *
1846 * @param[in] a first dict_attr_t.
1847 * @param[in] b second dict_attr_t.
1848 * @return
1849 * - +1 if a > b
1850 * - 0 if a == b
1851 * - -1 if a < b
1852 */
1853static inline fr_cmp_ret_t pair_cmp_by_num(void const *a, void const *b)
1854{
1855 fr_cmp_ret_t ret;
1856 unsigned int i, min;
1857 fr_pair_t const *my_a = a;
1858 fr_pair_t const *my_b = b;
1859 fr_da_stack_t da_stack_a, da_stack_b;
1860
1861 PAIR_VERIFY(my_a);
1862 PAIR_VERIFY(my_b);
1863
1864 fr_proto_da_stack_build(&da_stack_a, my_a->da);
1865 fr_proto_da_stack_build(&da_stack_b, my_b->da);
1866
1867 if (da_stack_a.depth <= da_stack_b.depth) {
1868 min = da_stack_a.depth;
1869 } else {
1870 min = da_stack_b.depth;
1871 }
1872
1873 for (i = 0; i < min; i++) {
1874 ret = CMP(da_stack_a.da[i]->attr, da_stack_b.da[i]->attr);
1875 if (ret != 0) return ret;
1876 }
1877
1878 /*
1879 * Sort attributes of similar depth together.
1880 *
1881 * What we really want to do is to sort by entire parent da_stack.
1882 */
1883 ret = CMP(my_a->da->depth, my_b->da->depth);
1884 if (ret != 0) return ret;
1885
1886 /*
1887 * Attributes of the same depth get sorted by their parents.
1888 */
1889 ret = CMP(my_a->da->parent->attr, my_b->da->parent->attr);
1890 if (ret != 0) return ret;
1891
1892 /*
1893 * If the attributes have the same parent, they get sorted by number.
1894 */
1895 return CMP(my_a->da->attr, my_b->da->attr);
1896}
1897
1898/** Order attributes by their parent(s), attribute number, and tag
1899 *
1900 * Useful for some protocols where attributes of the same number should by aggregated
1901 * within a packet or container TLV.
1902 *
1903 * @param[in] a first dict_attr_t.
1904 * @param[in] b second dict_attr_t.
1905 * @return
1906 * - +1 if a > b
1907 * - 0 if a == b
1908 * - -1 if a < b
1909 */
1910fr_cmp_ret_t fr_pair_cmp_by_parent_num(void const *a, void const *b)
1911{
1912 fr_pair_t const *vp_a = a;
1913 fr_pair_t const *vp_b = b;
1914 fr_dict_attr_t const *da_a = vp_a->da;
1915 fr_dict_attr_t const *da_b = vp_b->da;
1916 fr_da_stack_t da_stack_a;
1917 fr_da_stack_t da_stack_b;
1918 fr_cmp_ret_t cmp;
1919 int i;
1920
1921 /*
1922 * Fast path (assuming attributes
1923 * are in the same dictionary).
1924 */
1925 if ((da_a->parent->flags.is_root) && (da_b->parent->flags.is_root)) return pair_cmp_by_num(vp_a, vp_b);
1926
1927 fr_proto_da_stack_build(&da_stack_a, da_a);
1928 fr_proto_da_stack_build(&da_stack_b, da_b);
1929
1930 for (i = 0; (da_a = da_stack_a.da[i]) && (da_b = da_stack_b.da[i]); i++) {
1931 cmp = CMP(da_a->attr, da_b->attr);
1932 if (cmp != 0) return cmp;
1933 }
1934
1935 /*
1936 * If a has a shallower attribute
1937 * hierarchy than b, it should come
1938 * before b.
1939 */
1940 return (fr_cmp_ret_t)((da_a && !da_b) - (!da_a && da_b));
1941}
1942
1943/** Compare two pairs, using the operator from "a"
1944 *
1945 * i.e. given two attributes, it does:
1946 *
1947 * (b->data) (a->operator) (a->data)
1948 *
1949 * e.g. "foo" != "bar"
1950 *
1951 * @param[in] a the head attribute
1952 * @param[in] b the second attribute
1953 * @return
1954 * - 1 if true.
1955 * - 0 if false.
1956 * - -1 on failure.
1957 */
1958int fr_pair_cmp(fr_pair_t const *a, fr_pair_t const *b)
1959{
1960 if (!a) return -1;
1961
1962 PAIR_VERIFY(a);
1963 if (b) PAIR_VERIFY(b);
1964
1965 switch (a->op) {
1966 case T_OP_CMP_TRUE:
1967 return (b != NULL);
1968
1969 case T_OP_CMP_FALSE:
1970 return (b == NULL);
1971
1972 /*
1973 * a is a regex, compile it, print b to a string,
1974 * and then do string comparisons.
1975 */
1976 case T_OP_REG_EQ:
1977 case T_OP_REG_NE:
1978#ifndef HAVE_REGEX
1979 return -1;
1980#else
1981 if (!b) return false;
1982
1983 {
1984 ssize_t slen;
1985 regex_t *preg;
1986 char *value;
1987
1988 if (!fr_cond_assert(a->vp_type == FR_TYPE_STRING)) return -1;
1989
1990 slen = regex_compile(NULL, &preg, a->vp_strvalue, talloc_strlen(a->vp_strvalue),
1991 NULL, false, true);
1992 if (slen <= 0) {
1993 fr_strerror_printf_push("Error at offset %zd compiling regex for %s", -slen,
1994 a->da->name);
1995 return -1;
1996 }
1997 fr_pair_aprint(NULL, &value, NULL, b);
1998 if (!value) {
1999 talloc_free(preg);
2000 return -1;
2001 }
2002
2003 /*
2004 * Don't care about substring matches, oh well...
2005 */
2006 slen = regex_exec(preg, value, talloc_strlen(value), NULL);
2007 talloc_free(preg);
2009
2010 if (slen < 0) return -1;
2011 if (a->op == T_OP_REG_EQ) return (int)slen;
2012 return !slen;
2013 }
2014#endif
2015
2016 default: /* we're OK */
2017 if (!b) return false;
2018 break;
2019 }
2020
2021 return fr_pair_cmp_op(a->op, b, a);
2022}
2023
2024/** Determine equality of two lists
2025 *
2026 * This is useful for comparing lists of attributes inserted into a binary tree.
2027 *
2028 * @param a head list of #fr_pair_t.
2029 * @param b second list of #fr_pair_t.
2030 * @return
2031 * - CMP_LT if a < b.
2032 * - CMP_EQ if the two lists are equal.
2033 * - CMP_GT if a > b.
2034 * - CMP_ERR if the lists are not comparable, retrieve the error with fr_strerror.
2035 */
2037{
2038 fr_pair_t *a_p, *b_p;
2039
2040 for (a_p = fr_pair_list_head(a), b_p = fr_pair_list_head(b);
2041 a_p && b_p;
2042 a_p = fr_pair_list_next(a, a_p), b_p = fr_pair_list_next(b, b_p)) {
2043 fr_cmp_ret_t ret;
2044
2045 /* Same VP, no point doing expensive checks */
2046 if (a_p == b_p) continue;
2047
2048 ret = CMP(a_p->da, b_p->da);
2049 if (ret != 0) return ret;
2050
2051 switch (a_p->vp_type) {
2052 case FR_TYPE_STRUCTURAL:
2053 ret = fr_pair_list_cmp(&a_p->vp_group, &b_p->vp_group);
2054 if (ret != 0) return ret;
2055 break;
2056
2057 default:
2058 ret = fr_value_box_cmp(&a_p->data, &b_p->data);
2059 if (ret != 0) return ret;
2060 }
2061
2062 }
2063
2064 /*
2065 * If we've run off of the end of one of the lists.
2066 */
2067 return CMP(a_p, b_p);
2068}
2069
2070/** Write an error to the library errorbuff detailing the mismatch
2071 *
2072 * Retrieve output with fr_strerror();
2073 *
2074 * @todo add thread specific talloc contexts.
2075 *
2076 * @param failed pair of attributes which didn't match.
2077 */
2078void fr_pair_validate_debug(fr_pair_t const *failed[2])
2079{
2080 fr_pair_t const *filter = failed[0];
2081 fr_pair_t const *list = failed[1];
2082
2083 fr_strerror_clear(); /* Clear any existing messages */
2084
2085 if (!list) {
2086 if (!filter) {
2087 (void) fr_cond_assert(filter != NULL);
2088 return;
2089 }
2090 fr_strerror_printf("Attribute \"%s\" not found in list", filter->da->name);
2091 return;
2092 }
2093
2094 if (!filter || (filter->da != list->da)) {
2095 fr_strerror_printf("Attribute \"%s\" not found in filter", list->da->name);
2096 return;
2097 }
2098
2099 fr_strerror_printf("Attribute value: %pP didn't match filter: %pP", list, filter);
2100
2101 return;
2102}
2103
2104/** Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check
2105 *
2106 * @note will sort both filter and list in place.
2107 *
2108 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
2109 * May be NULL.
2110 * @param filter attributes to check list against.
2111 * @param list attributes, probably a request or reply
2112 */
2113bool fr_pair_validate(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
2114{
2115 fr_pair_t *check, *match;
2116
2117 if (fr_pair_list_empty(filter) && fr_pair_list_empty(list)) return true;
2118
2119 /*
2120 * This allows us to verify the sets of validate and reply are equal
2121 * i.e. we have a validate rule which matches every reply attribute.
2122 *
2123 * @todo this should be removed one we have sets and lists
2124 */
2127
2128 check = fr_pair_list_head(filter);
2129 match = fr_pair_list_head(list);
2130 while (match || check) {
2131 /*
2132 * Lists are of different lengths
2133 */
2134 if (!match || !check) goto mismatch;
2135
2136 /*
2137 * The lists are sorted, so if the head
2138 * attributes aren't of the same type, then we're
2139 * done.
2140 */
2141 if (!ATTRIBUTE_EQ(check, match)) goto mismatch;
2142
2143 /*
2144 * They're of the same type, but don't have the
2145 * same values. This is a problem.
2146 *
2147 * Note that the RFCs say that for attributes of
2148 * the same type, order is important.
2149 */
2150 switch (check->vp_type) {
2151 case FR_TYPE_STRUCTURAL:
2152 /*
2153 * Return from here on failure, so that the nested mismatch
2154 * information is preserved.
2155 */
2156 if (!fr_pair_validate(failed, &check->vp_group, &match->vp_group)) return false;
2157 break;
2158
2159 default:
2160 /*
2161 * This attribute passed the filter
2162 */
2163 if (!fr_pair_cmp(check, match)) goto mismatch;
2164 break;
2165 }
2166
2167 check = fr_pair_list_next(filter, check);
2168 match = fr_pair_list_next(list, match);
2169 }
2170
2171 return true;
2172
2173mismatch:
2174 if (failed) {
2175 failed[0] = check;
2176 failed[1] = match;
2177 }
2178 return false;
2179}
2180
2181/** Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check
2182 *
2183 * @note will sort both filter and list in place.
2184 *
2185 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
2186 * May be NULL.
2187 * @param filter attributes to check list against.
2188 * @param list attributes, probably a request or reply
2189 */
2191{
2192 fr_pair_t *last_check = NULL, *match = NULL;
2193
2194 if (fr_pair_list_empty(filter) && fr_pair_list_empty(list)) return true;
2195
2196 /*
2197 * This allows us to verify the sets of validate and reply are equal
2198 * i.e. we have a validate rule which matches every reply attribute.
2199 *
2200 * @todo this should be removed one we have sets and lists
2201 */
2204
2205 fr_pair_list_foreach(filter, check) {
2206 /*
2207 * Were processing check attributes of a new type.
2208 */
2209 if (!ATTRIBUTE_EQ(last_check, check)) {
2210 /*
2211 * Record the start of the matching attributes in the pair list
2212 * For every other operator we require the match to be present
2213 */
2214 while ((match = fr_pair_list_next(list, match))) {
2215 if (match->da == check->da) break;
2216 }
2217 if (!match) {
2218 if (check->op == T_OP_CMP_FALSE) continue;
2219 goto mismatch;
2220 }
2221
2222 last_check = check;
2223 } else {
2224 match = fr_pair_list_head(list);
2225 }
2226
2227 /*
2228 * Now iterate over all attributes of the same type.
2229 */
2230 for (;
2231 ATTRIBUTE_EQ(match, check);
2232 match = fr_pair_list_next(list, match)) {
2233 switch (check->vp_type) {
2234 case FR_TYPE_STRUCTURAL:
2235 if (!fr_pair_validate_relaxed(failed, &check->vp_group, &match->vp_group)) goto mismatch;
2236 break;
2237
2238 default:
2239 /*
2240 * This attribute passed the filter
2241 */
2242 if (!fr_pair_cmp(check, match)) {
2243 mismatch:
2244 if (failed) {
2245 failed[0] = check;
2246 failed[1] = match;
2247 }
2248 return false;
2249 }
2250 break;
2251 }
2252 }
2253 }
2254
2255 return true;
2256}
2257
2258/**
2259 *
2260 * @param[in] vp the pair to check
2261 * @return
2262 * - true the pair is immutable, or has an immutable child
2263 * - false the pair is not immutable, or has no immutable children.
2264 */
2266{
2267 if (fr_type_is_leaf(vp->vp_type)) return vp->vp_immutable;
2268
2270
2271 fr_pair_list_foreach(&vp->vp_group, child) {
2272 if (fr_type_is_leaf(child->vp_type)) {
2273 if (child->vp_immutable) return true;
2274
2275 continue;
2276 }
2277
2278 fr_assert(fr_type_is_structural(child->vp_type));
2279
2280 if (fr_pair_immutable(child)) return true;
2281 }
2282
2283 return false;
2284}
2285
2286/** Steal a list of pairs to a new context
2287 *
2288 */
2289void fr_pair_list_steal(TALLOC_CTX *ctx, fr_pair_list_t *list)
2290{
2291 fr_pair_list_foreach(list, vp) {
2292 (void) fr_pair_steal(ctx, vp);
2293 }
2294
2295#ifdef WITH_VERIFY_PTR
2296 list->verified = false;
2297#endif
2298}
2299
2300/** Duplicate a list of pairs
2301 *
2302 * Copy all pairs from 'from' regardless of tag, attribute or vendor.
2303 *
2304 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2305 * @param[in] to where to copy attributes to.
2306 * @param[in] from whence to copy #fr_pair_t (s).
2307 * @return
2308 * - >0 the number of attributes copied.
2309 * - 0 if no attributes copied.
2310 * - -1 on error.
2311 */
2312int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
2313{
2314 fr_pair_t *new_vp, *first_added = NULL;
2315 int cnt = 0;
2316
2317 fr_pair_list_foreach(from, vp) {
2318 cnt++;
2320
2321 new_vp = fr_pair_copy(ctx, vp);
2322 if (!new_vp) {
2323 fr_pair_order_list_talloc_free_to_tail(&to->order, first_added);
2324 return -1;
2325 }
2326
2327 if (!first_added) first_added = new_vp;
2328 fr_pair_append(to, new_vp);
2329 }
2330
2331 return cnt;
2332}
2333
2334
2335/** Copy the contents of a pair list to a set of value-boxes
2336 *
2337 * This function should be removed when the xlats use dcursors
2338 * of copying all of the boxes.
2339 *
2340 * @param[in] dst where boxes will be created
2341 * @param[in] from whence to copy #fr_pair_t (s).
2342 * @return
2343 * - >0 the number of boxes copied.
2344 * - 0 if no boxes copied.
2345 * - -1 on error.
2346 */
2348{
2349 int cnt = 0;
2350 fr_value_box_t *value, *first_added = NULL;
2351
2352 fr_assert(dst->type == FR_TYPE_GROUP);
2353
2354 fr_pair_list_foreach(from, vp) {
2355 cnt++;
2357
2358 if (fr_type_is_structural(vp->vp_type)) {
2360 if (!value) goto fail;
2361
2362 if (fr_pair_list_copy_to_box(value, &vp->vp_group) < 0) {
2364 goto fail;
2365 }
2366
2367 } else {
2368 value = fr_value_box_alloc(dst, vp->vp_type, vp->da);
2369 if (!value) {
2370 fail:
2371 fr_value_box_list_talloc_free_to_tail(&dst->vb_group, first_added);
2372 return -1;
2373 }
2374 if (unlikely(fr_value_box_copy(value, value, &vp->data) < 0)) {
2376 goto fail;
2377 }
2378 }
2379
2380 if (!first_added) first_added = value;
2381 fr_value_box_list_insert_tail(&dst->vb_group, value);
2382 }
2383
2384 return cnt;
2385}
2386
2387/** Duplicate pairs in a list matching the specified da
2388 *
2389 * Copy all pairs from 'from' matching the specified da.
2390 *
2391 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2392 * @param[in] to where to copy attributes to.
2393 * @param[in] from whence to copy #fr_pair_t (s).
2394 * @param[in] da to match.
2395 * @return
2396 * - >0 the number of attributes copied.
2397 * - 0 if no attributes copied.
2398 * - -1 on error.
2399 */
2401 fr_pair_list_t const *from, fr_dict_attr_t const *da)
2402{
2403 fr_pair_t *new_vp;
2404 unsigned int cnt = 0;
2405 fr_pair_list_t new_list;
2406
2407 fr_pair_list_init(&new_list);
2408
2409 fr_pair_list_foreach(from, vp) {
2411
2412 if (vp->da != da) continue;
2413
2414 cnt++;
2415 new_vp = fr_pair_copy(ctx, vp);
2416 if (!new_vp) {
2417 fr_pair_list_free(&new_list);
2418 return -1;
2419 }
2420
2421 fr_pair_append(&new_list, new_vp);
2422 }
2423
2424 fr_pair_list_append(to, &new_list);
2425
2426 return cnt;
2427}
2428
2429/** Duplicate pairs in a list where the da is a descendant of parent_da
2430 *
2431 * Copy all pairs from 'from' which are descendants of the specified 'parent_da'.
2432 * This is particularly useful for copying attributes of a particular vendor, where the vendor
2433 * da is passed as parent_da.
2434 *
2435 * @param[in] ctx for new #fr_pair_t (s) to be allocated in.
2436 * @param[in] to where to copy attributes to.
2437 * @param[in] from whence to copy #fr_pair_t (s).
2438 * @param[in] parent_da to match.
2439 * @return
2440 * - >0 one or more attributes were copied
2441 * - 0 if no attributes copied.
2442 * - -1 on error.
2443 */
2445 fr_pair_list_t const *from, fr_dict_attr_t const *parent_da)
2446{
2447 fr_pair_t *tlv;
2448 bool found = false;
2449 fr_pair_list_t list;
2450
2451 if (!fr_type_is_structural(parent_da->type)) return -1;
2452
2453 /*
2454 * Allow for nested attributes.
2455 */
2456 tlv = fr_pair_find_by_da(from, NULL, parent_da);
2457 if (tlv) {
2458 fr_pair_t *vp;
2459
2460 vp = fr_pair_copy(ctx, tlv);
2461 if (!vp) return -1;
2462
2463 fr_pair_append(to, vp);
2464
2465 return 1;
2466 }
2467
2468 fr_pair_list_init(&list);
2469
2470 fr_pair_list_foreach(from, vp) {
2471 fr_pair_t *new_vp;
2472
2473 if (!fr_dict_attr_common_parent(parent_da, vp->da, true)) continue;
2474
2475 new_vp = fr_pair_copy(ctx, vp);
2476 if (unlikely(!new_vp)) {
2477 fr_pair_list_free(&list);
2478 return -1;
2479 }
2480
2481 fr_pair_append(&list, new_vp);
2482 found = true;
2483 }
2484
2485 fr_pair_list_append(to, &list);
2486
2487 return found;
2488}
2489
2490/** Free/zero out value (or children) of a given VP
2491 *
2492 * @param[in] vp to clear value from.
2493 */
2495{
2496 fr_pair_t *child;
2497
2498 switch (vp->vp_type) {
2499 default:
2501 break;
2502
2503 case FR_TYPE_STRUCTURAL:
2504 if (fr_pair_list_empty(&vp->vp_group)) return;
2505
2506 while ((child = fr_pair_order_list_pop_tail(&vp->vp_group.order))) {
2507 fr_pair_value_clear(child);
2508 talloc_free(child);
2509 }
2510 break;
2511 }
2512}
2513
2514/** Copy the value from one pair to another
2515 *
2516 * @param[out] dst where to copy the value to.
2517 * will clear assigned value.
2518 * @param[in] src where to copy the value from
2519 * Must have an assigned value.
2520 * @return
2521 * - 0 on success.
2522 * - -1 on failure.
2523 */
2525{
2526#ifdef WITH_VERIFY_PTR
2527 char const *file = dst->data.file; /* copying the value-box also copies these fields */
2528 int line = dst->data.line;
2529#endif
2530
2531 if (!fr_cond_assert(src->data.type != FR_TYPE_NULL)) return -1;
2532
2533 fr_value_box_clear_value(&dst->data);
2534 if (unlikely(fr_value_box_copy(dst, &dst->data, &src->data) < 0)) return -1;
2535
2536 /*
2537 * If either source or destination is secret, then this value is secret.
2538 */
2539 if (src->da->flags.secret || dst->da->flags.secret || fr_value_box_is_secret(&src->data)) {
2540 fr_value_box_set_secret(&dst->data, true);
2541 }
2542#ifdef WITH_VERIFY_PTR
2543 dst->data.file = file;
2544 dst->data.line = line;
2545#endif
2546 return 0;
2547}
2548
2549/** Convert string value to native attribute value
2550 *
2551 * @param[in] vp to assign value to.
2552 * @param[in] value string to convert. Binary safe for variable
2553 * length values if len is provided.
2554 * @param[in] inlen The length of the input string.
2555 * @param[in] uerules used to perform unescaping.
2556 * @param[in] tainted Whether the value came from a trusted source.
2557 * @return
2558 * - 0 on success.
2559 * - -1 on failure.
2560 */
2561int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen,
2562 fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
2563{
2564 /*
2565 * This is not yet supported because the rest of the APIs
2566 * to parse pair names, etc. don't yet enforce "inlen".
2567 * This is likely not a problem in practice, but we
2568 * haven't yet audited the uses of this function for that
2569 * behavior.
2570 */
2571 switch (vp->vp_type) {
2572 case FR_TYPE_STRUCTURAL:
2573 fr_strerror_printf("Attributes of type '%s' are not yet supported",
2574 fr_type_to_str(vp->vp_type));
2575 return -1;
2576
2577 default:
2578 break;
2579 }
2580
2581 /*
2582 * We presume that the input data is from a double quoted
2583 * string, and needs unescaping
2584 */
2585 if (fr_value_box_from_str(vp, &vp->data, vp->vp_type, vp->da,
2586 value, inlen,
2587 uerules) < 0) return -1;
2588
2590
2591 PAIR_VERIFY(vp);
2592
2593 return 0;
2594}
2595
2596/** Copy data into an "string" data type.
2597 *
2598 * @note vp->da must be of type FR_TYPE_STRING.
2599 *
2600 * @param[in,out] vp to update
2601 * @param[in] src data to copy
2602 * @param[in] tainted Whether the value came from a trusted source.
2603 * @return
2604 * - 0 on success.
2605 * - -1 on failure.
2606 */
2607int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
2608{
2609 int ret;
2610
2611 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2612
2613 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2614 ret = fr_value_box_strdup(vp, &vp->data, vp->da, src, tainted);
2615 if (ret == 0) {
2616 PAIR_VERIFY(vp);
2617 }
2618
2619 return ret;
2620}
2621
2622/** Assign a buffer containing a nul terminated string to a vp, but don't copy it
2623 *
2624 * @param[in] vp to assign string to.
2625 * @param[in] src to copy string from.
2626 * @param[in] tainted Whether the value came from a trusted source.
2627 * @return
2628 * - 0 on success.
2629 * - -1 on failure.
2630 */
2631int fr_pair_value_strdup_shallow(fr_pair_t *vp, char const *src, bool tainted)
2632{
2633 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2634
2635 fr_value_box_clear(&vp->data);
2636 fr_value_box_strdup_shallow(&vp->data, vp->da, src, tainted);
2637
2638 PAIR_VERIFY(vp);
2639
2640 return 0;
2641}
2642
2643/** Trim the length of the string buffer to match the length of the C string
2644 *
2645 * @param[in,out] vp to trim.
2646 * @return
2647 * - 0 on success.
2648 * - -1 on failure.
2649 */
2651{
2652 int ret;
2653
2654 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2655
2656 ret = fr_value_box_strtrim(vp, &vp->data);
2657 if (ret == 0) {
2658 PAIR_VERIFY(vp);
2659 }
2660
2661 return ret;
2662}
2663
2664/** Print data into an "string" data type.
2665 *
2666 * @note vp->da must be of type FR_TYPE_STRING.
2667 *
2668 * @param[in,out] vp to update
2669 * @param[in] fmt the format string
2670 */
2671int fr_pair_value_aprintf(fr_pair_t *vp, char const *fmt, ...)
2672{
2673 int ret;
2674 va_list ap;
2675
2676 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2677
2678 fr_value_box_clear(&vp->data);
2679 va_start(ap, fmt);
2680 ret = fr_value_box_vasprintf(vp, &vp->data, vp->da, false, fmt, ap);
2681 va_end(ap);
2682
2683 if (ret == 0) {
2684 PAIR_VERIFY(vp);
2685 }
2686
2687 return ret;
2688}
2689
2690/** Pre-allocate a memory buffer for a "string" type value pair
2691 *
2692 * @note Will clear existing values (including buffers).
2693 *
2694 * @param[in,out] vp to update
2695 * @param[out] out If non-null will be filled with a pointer to the
2696 * new buffer.
2697 * @param[in] size of the data.
2698 * @param[in] tainted Whether the value came from a trusted source.
2699 * @return
2700 * - 0 on success.
2701 * - -1 on failure.
2702 */
2703int fr_pair_value_bstr_alloc(fr_pair_t *vp, char **out, size_t size, bool tainted)
2704{
2705 int ret;
2706
2707 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2708
2709 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2710 ret = fr_value_box_bstr_alloc(vp, out, &vp->data, vp->da, size, tainted);
2711 if (ret == 0) {
2712 PAIR_VERIFY(vp);
2713 }
2714
2715 return ret;
2716}
2717
2718/** Change the length of a buffer for a "string" type value pair
2719 *
2720 * @param[in,out] vp to update
2721 * @param[out] out If non-null will be filled with a pointer to the
2722 * new buffer.
2723 * @param[in] size of the data.
2724 * @return
2725 * - 0 on success.
2726 * - -1 on failure.
2727 */
2728int fr_pair_value_bstr_realloc(fr_pair_t *vp, char **out, size_t size)
2729{
2730 int ret;
2731
2732 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2733
2734 ret = fr_value_box_bstr_realloc(vp, out, &vp->data, size);
2735 if (ret == 0) {
2736 PAIR_VERIFY(vp);
2737 }
2738
2739 return ret;
2740}
2741
2742/** Copy data into a "string" type value pair
2743 *
2744 * @note This API will copy binary data, including embedded '\0'
2745 *
2746 * @note vp->da must be of type FR_TYPE_STRING.
2747 *
2748 * @param[in,out] vp to update.
2749 * @param[in] src data to copy.
2750 * @param[in] len of data to copy.
2751 * @param[in] tainted Whether the value came from a trusted source.
2752 * @return
2753 * - 0 on success.
2754 * - -1 on failure.
2755 */
2756int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
2757{
2758 int ret;
2759
2760 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2761
2762 fr_value_box_clear(&vp->data);
2763 ret = fr_value_box_bstrndup(vp, &vp->data, vp->da, src, len, tainted);
2764 if (ret == 0) {
2765 PAIR_VERIFY(vp);
2766 }
2767
2768 return ret;
2769}
2770
2771/** Copy a nul terminated talloced buffer a "string" type value pair
2772 *
2773 * The buffer must be \0 terminated, or an error will be returned.
2774 *
2775 * @param[in,out] vp to update.
2776 * @param[in] src a talloced nul terminated buffer.
2777 * @param[in] tainted Whether the value came from a trusted source.
2778 * @return
2779 * - 0 on success.
2780 * - -1 on failure.
2781 */
2782int fr_pair_value_bstrdup_buffer(fr_pair_t *vp, char const *src, bool tainted)
2783{
2784 int ret;
2785
2786 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2787
2788 fr_value_box_clear(&vp->data);
2789 ret = fr_value_box_bstrdup_buffer(vp, &vp->data, vp->da, src, tainted);
2790 if (ret == 0) {
2791 PAIR_VERIFY(vp);
2792 }
2793
2794 return ret;
2795}
2796
2797/** Assign a string to a "string" type value pair
2798 *
2799 * @param[in] vp to assign new buffer to.
2800 * @param[in] src a string.
2801 * @param[in] len of src.
2802 * @param[in] tainted Whether the value came from a trusted source.
2803 * @return
2804 * - 0 on success.
2805 * - -1 on failure.
2806 */
2807int fr_pair_value_bstrndup_shallow(fr_pair_t *vp, char const *src, size_t len, bool tainted)
2808{
2809 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2810
2811 fr_value_box_clear(&vp->data);
2812 fr_value_box_bstrndup_shallow(&vp->data, vp->da, src, len, tainted);
2813 PAIR_VERIFY(vp);
2814
2815 return 0;
2816}
2817
2818/** Assign a string to a "string" type value pair
2819 *
2820 * @param[in] vp to assign new buffer to.
2821 * @param[in] src a string.
2822 * @param[in] tainted Whether the value came from a trusted source.
2823 * @return
2824 * - 0 on success.
2825 * - -1 on failure.
2826 */
2827int fr_pair_value_bstrdup_buffer_shallow(fr_pair_t *vp, char const *src, bool tainted)
2828{
2829 int ret;
2830
2831 if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
2832
2833 fr_value_box_clear(&vp->data);
2834 ret = fr_value_box_bstrdup_buffer_shallow(NULL, &vp->data, vp->da, src, tainted);
2835 if (ret == 0) {
2836 PAIR_VERIFY(vp);
2837 }
2838
2839 return ret;
2840}
2841
2842/** Pre-allocate a memory buffer for a "octets" type value pair
2843 *
2844 * @note Will clear existing values (including buffers).
2845 *
2846 * @param[in,out] vp to update
2847 * @param[out] out If non-null will be filled with a pointer to the
2848 * new buffer.
2849 * @param[in] size of the data.
2850 * @param[in] tainted Whether the value came from a trusted source.
2851 * @return
2852 * - 0 on success.
2853 * - -1 on failure.
2854 */
2855int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
2856{
2857 int ret;
2858
2859 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2860
2861 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2862 ret = fr_value_box_mem_alloc(vp, out, &vp->data, vp->da, size, tainted);
2863 if (ret == 0) {
2864 PAIR_VERIFY(vp);
2865 }
2866
2867 return ret;
2868}
2869
2870/** Change the length of a buffer for a "octets" type value pair
2871 *
2872 * @param[in,out] vp to update
2873 * @param[out] out If non-null will be filled with a pointer to the
2874 * new buffer.
2875 * @param[in] size of the data.
2876 * @return
2877 * - 0 on success.
2878 * - -1 on failure.
2879 */
2881{
2882 int ret;
2883
2884 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2885
2886 ret = fr_value_box_mem_realloc(vp, out, &vp->data, size);
2887 if (ret == 0) {
2888 PAIR_VERIFY(vp);
2889 }
2890
2891 return ret;
2892}
2893
2894/** Copy data into an "octets" data type.
2895 *
2896 * @note Will clear existing values (including buffers).
2897 *
2898 * @param[in,out] vp to update
2899 * @param[in] src data to copy
2900 * @param[in] len of the data.
2901 * @param[in] tainted Whether the value came from a trusted source.
2902 * @return
2903 * - 0 on success.
2904 * - -1 on failure.
2905 */
2906int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
2907{
2908 int ret;
2909
2910 if (unlikely((len > 0) && !src)) {
2911 fr_strerror_printf("Invalid arguments to %s. Len > 0 (%zu) but src was NULL",
2912 __FUNCTION__, len);
2913 return -1;
2914 }
2915
2916 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2917
2918 fr_value_box_clear_value(&vp->data); /* Free any existing buffers */
2919 ret = fr_value_box_memdup(vp, &vp->data, vp->da, src, len, tainted);
2920 if (ret == 0) PAIR_VERIFY(vp);
2921
2922 return ret;
2923}
2924
2925/** Copy data from a talloced buffer into an "octets" data type.
2926 *
2927 * @note Will clear existing values (including buffers).
2928 *
2929 * @param[in,out] vp to update
2930 * @param[in] src data to copy
2931 * @param[in] tainted Whether the value came from a trusted source.
2932 * @return
2933 * - 0 on success.
2934 * - -1 on failure.
2935 */
2936int fr_pair_value_memdup_buffer(fr_pair_t *vp, uint8_t const *src, bool tainted)
2937{
2938 int ret;
2939
2940 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2941
2942 fr_value_box_clear(&vp->data); /* Free any existing buffers */
2943 ret = fr_value_box_memdup_buffer(vp, &vp->data, vp->da, src, tainted);
2944 if (ret == 0) {
2945 PAIR_VERIFY(vp);
2946 }
2947
2948 return ret;
2949}
2950
2951/** Assign a buffer to a "octets" type value pair
2952 *
2953 * @param[in] vp to assign new buffer to.
2954 * @param[in] src data to copy.
2955 * @param[in] len of src.
2956 * @param[in] tainted Whether the value came from a trusted source.
2957 * @return
2958 * - 0 on success.
2959 * - -1 on failure.
2960 */
2961int fr_pair_value_memdup_shallow(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
2962{
2963 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2964
2965 fr_value_box_clear(&vp->data);
2966 fr_value_box_memdup_shallow(&vp->data, vp->da, src, len, tainted);
2967 PAIR_VERIFY(vp);
2968
2969 return 0;
2970}
2971
2972/** Assign a talloced buffer to a "octets" type value pair
2973 *
2974 * @param[in] vp to assign new buffer to.
2975 * @param[in] src data to copy.
2976 * @param[in] tainted Whether the value came from a trusted source.
2977 * @return
2978 * - 0 on success.
2979 * - -1 on failure.
2980 */
2982{
2983 if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
2984
2985 fr_value_box_clear(&vp->data);
2986 fr_value_box_memdup_buffer_shallow(NULL, &vp->data, vp->da, src, tainted);
2987 PAIR_VERIFY(vp);
2988
2989 return 0;
2990}
2991
2992
2993/** Return a const buffer for an enum type attribute
2994 *
2995 * Where the vp type is numeric but does not have any enumv, or its value
2996 * does not map to an enumv, the integer value of the pair will be printed
2997 * to buff, and a pointer to buff will be returned.
2998 *
2999 * @param[in] vp to print.
3000 * @param[in] buff to print integer value to.
3001 * @return a talloced buffer.
3002 */
3003char const *fr_pair_value_enum(fr_pair_t const *vp, char buff[20])
3004{
3005 fr_dict_enum_value_t const *enumv;
3006
3007 if (!fr_box_is_numeric(&vp->data)) {
3008 fr_strerror_printf("Pair %s is not numeric", vp->da->name);
3009 return NULL;
3010 }
3011
3012 if (vp->da->flags.has_value) switch (vp->vp_type) {
3013 case FR_TYPE_BOOL:
3014 return vp->vp_bool ? "yes" : "no";
3015
3016 default:
3017 enumv = fr_dict_enum_by_value(vp->da, &vp->data);
3018 if (enumv) return enumv->name;
3019 break;
3020 }
3021
3023 return buff;
3024}
3025
3026/** Get value box of a VP, optionally prefer enum value.
3027 *
3028 * Get the data value box of the given VP. If 'e' is set to 1 and the VP has an
3029 * enum value, this will be returned instead. Otherwise it will be set to the
3030 * value box of the VP itself.
3031 *
3032 * @param[out] out pointer to a value box.
3033 * @param[in] vp to print.
3034 * @return 1 if the enum value has been used, 0 otherwise, -1 on error.
3035 */
3037{
3038 fr_dict_enum_value_t const *dv;
3039
3040 if (vp->da && vp->da->flags.has_value &&
3041 (dv = fr_dict_enum_by_value(vp->da, &vp->data))) {
3042 *out = dv->value;
3043 return 1;
3044 }
3045
3046 *out = &vp->data;
3047 return 0;
3048}
3049
3050#ifdef WITH_VERIFY_PTR
3051#define PAIR_NAME "fr_pair_t %s (from %s:%d)"
3052#define PAIR_NAME_LOCATION(_x) (_x)->da->name, (_x)->data.file ? (_x)->data.file : "", (_x)->data.line
3053
3054/*
3055 * Verify a fr_pair_t
3056 */
3057void fr_pair_verify(char const *file, int line, fr_dict_attr_t const *parent_da,
3058 fr_pair_list_t const *list, fr_pair_t const *vp, bool verify_values)
3059{
3061
3062 if (vp->verified) {
3063 if (fr_type_is_structural(vp->vp_type)) {
3064 if (vp->vp_group.verified) return;
3065 } else {
3066 if (vp->data.verified) return;
3067 }
3068
3069 UNCONST(fr_pair_t *, vp)->verified = false;
3070 }
3071
3072 if (!vp->da) {
3073 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t da pointer was NULL", file, line);
3074 }
3075
3076 fr_dict_attr_verify(file, line, vp->da);
3077
3078 /*
3079 * Enforce correct parentage. If the parent exists, AND it's not a group (because groups break
3080 * the strict hierarchy), then check parentage.
3081 *
3082 * We also ignore parentage if either the expected parent or the vp is raw / unknown. We may
3083 * want to tighten that a little bit, as there are cases where we create raw / unknown
3084 * attributes, and the parent is also raw / unknown. In which case the parent_da _should_ be the
3085 * same as vp->da->parent.
3086 */
3087 if (parent_da && (parent_da->type != FR_TYPE_GROUP) &&
3088 !parent_da->flags.is_raw && !parent_da->flags.is_unknown &&
3089 !vp->da->flags.is_raw && !vp->da->flags.is_unknown) {
3090 fr_fatal_assert_msg(vp->da->parent == parent_da,
3091 "CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " does not have the correct parentage - "
3092 "expected parent da %s, found different parent da %s",
3093 file, line,
3094 PAIR_NAME_LOCATION(vp), parent_da->name, vp->da->parent->name);
3095 }
3096
3097 if (list) {
3098 fr_fatal_assert_msg(fr_pair_order_list_parent(vp) == &list->order,
3099 "CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " does not have the correct parentage",
3100 file, line,
3101 PAIR_NAME_LOCATION(vp));
3102 }
3103
3104 /*
3105 * This field is only valid for non-structural pairs
3106 */
3107 if (!fr_type_is_structural(vp->vp_type)) {
3109
3110 if (vp->data.enumv) fr_dict_attr_verify(file, line, vp->data.enumv);
3111
3112 if (parent && !fr_dict_attr_can_contain(parent->da, vp->da)) {
3113 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " should be parented by da %s, but is parented by da %s",
3114 file, line,
3115 PAIR_NAME_LOCATION(vp), vp->da->parent->name, parent->da->name);
3116 }
3117
3118 /*
3119 * The data types have to agree, except for comb-ip and combo-ipaddr.
3120 */
3121 if (vp->vp_type != vp->da->type) switch (vp->da->type) {
3123 if ((vp->vp_type == FR_TYPE_IPV4_ADDR) ||
3124 (vp->vp_type == FR_TYPE_IPV6_ADDR)) {
3125 break;
3126 }
3127 goto failed_type;
3128
3130 if ((vp->vp_type == FR_TYPE_IPV4_PREFIX) ||
3131 (vp->vp_type == FR_TYPE_IPV6_PREFIX)) {
3132 break;
3133 }
3135
3136 default:
3137 failed_type:
3138 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " has value of data type '%s', which disagrees with the dictionary data type '%s'",
3139 file, line,
3140 PAIR_NAME_LOCATION(vp), fr_type_to_str(vp->vp_type), fr_type_to_str(vp->da->type));
3141 }
3142
3143 /*
3144 * We would like to enable this, but there's a
3145 * lot of code like fr_pair_append_by_da() which
3146 * creates the #fr_pair_t with no value.
3147 */
3148 if (verify_values) fr_value_box_verify(file, line, &vp->data);
3149
3150 } else {
3152
3153 if (parent && (parent->vp_type != FR_TYPE_GROUP) && (parent->da == vp->da)) {
3154 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " structural (non-group) type contains itself",
3155 file, line,
3156 PAIR_NAME_LOCATION(vp));
3157 }
3158
3159 fr_pair_list_verify(file, line, vp, &vp->vp_group, verify_values);
3160 }
3161
3162 switch (vp->vp_type) {
3163 case FR_TYPE_OCTETS:
3164 {
3165 size_t len;
3166 TALLOC_CTX *parent;
3167
3168 if (!vp->vp_octets) break; /* We might be in the middle of initialisation */
3169
3170 if (!talloc_get_type(vp->vp_ptr, uint8_t)) {
3171 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " data buffer type should be "
3172 "uint8_t but is %s",
3173 file, line,
3174 PAIR_NAME_LOCATION(vp), talloc_get_name(vp->vp_ptr));
3175 }
3176
3177 len = talloc_array_length(vp->vp_octets);
3178 if (vp->vp_length > len) {
3179 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " length %zu is greater than "
3180 "uint8_t data buffer length %zu",
3181 file, line,
3182 PAIR_NAME_LOCATION(vp), vp->vp_length, len);
3183 }
3184
3185#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3186 parent = talloc_parent(vp->vp_ptr);
3187 if (parent != vp) {
3188 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " char buffer is not "
3189 "parented by fr_pair_t %p, instead parented by %p (%s)",
3190 file, line,
3191 PAIR_NAME_LOCATION(vp), vp,
3192 parent, parent ? talloc_get_name(parent) : "NULL");
3193 }
3194#endif
3195 }
3196 break;
3197
3198 case FR_TYPE_STRING:
3199 {
3200 size_t len;
3201 TALLOC_CTX *parent;
3202
3203 if (!vp->vp_octets) break; /* We might be in the middle of initialisation */
3204
3205 if (!talloc_get_type(vp->vp_ptr, char)) {
3206 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " data buffer type should be "
3207 "char but is %s",
3208 file, line,
3209 PAIR_NAME_LOCATION(vp), talloc_get_name(vp->vp_ptr));
3210 }
3211
3212 len = (talloc_strlen(vp->vp_strvalue));
3213 if (vp->vp_length > len) {
3214 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " length %zu is greater than "
3215 "char buffer length %zu",
3216 file, line,
3217 PAIR_NAME_LOCATION(vp), vp->vp_length, len);
3218 }
3219
3220 if (vp->vp_strvalue[vp->vp_length] != '\0') {
3221 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " char buffer not \\0 "
3222 "terminated",
3223 file, line,
3224 PAIR_NAME_LOCATION(vp));
3225 }
3226
3227#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3228 parent = talloc_parent(vp->vp_ptr);
3229 if (parent != vp) {
3230 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " char buffer is not "
3231 "parented by fr_pair_t %p, instead parented by %p (%s)",
3232 file, line,
3233 PAIR_NAME_LOCATION(vp), vp,
3234 parent, parent ? talloc_get_name(parent) : "NULL");
3235 }
3236#endif
3237 }
3238 break;
3239
3240 case FR_TYPE_IPV4_ADDR:
3241 if (vp->vp_ip.af != AF_INET) {
3242 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " address family is not "
3243 "set correctly for IPv4 address. Expected %i got %i",
3244 file, line,
3245 PAIR_NAME_LOCATION(vp),
3246 AF_INET, vp->vp_ip.af);
3247 }
3248 if (vp->vp_ip.prefix != 32) {
3249 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " address prefix "
3250 "not set correctly for IPv4 address. Expected %i got %i",
3251 file, line,
3252 PAIR_NAME_LOCATION(vp),
3253 32, vp->vp_ip.prefix);
3254 }
3255 break;
3256
3257 case FR_TYPE_IPV6_ADDR:
3258 if (vp->vp_ip.af != AF_INET6) {
3259 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " address family is not "
3260 "set correctly for IPv6 address. Expected %i got %i",
3261 file, line,
3262 PAIR_NAME_LOCATION(vp),
3263 AF_INET6, vp->vp_ip.af);
3264 }
3265 if (vp->vp_ip.prefix != 128) {
3266 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " address prefix "
3267 "set correctly for IPv6 address. Expected %i got %i",
3268 file, line,
3269 PAIR_NAME_LOCATION(vp),
3270 128, vp->vp_ip.prefix);
3271 }
3272 break;
3273
3274 case FR_TYPE_ATTR:
3275 if (!vp->vp_attr) {
3276 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " attribute pointer is NULL",
3277 file, line,
3278 PAIR_NAME_LOCATION(vp));
3279 }
3280 break;
3281
3282 case FR_TYPE_STRUCTURAL:
3283 {
3284 if (vp->vp_group.verified) break;
3285
3286 fr_pair_list_foreach(&vp->vp_group, child) {
3287#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3288 TALLOC_CTX *parent = talloc_parent(child);
3289
3291 "CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " should be parented "
3292 "by " PAIR_NAME ". Expected talloc parent %p (%s) got %p (%s)",
3293 file, line,
3294 PAIR_NAME_LOCATION(child), PAIR_NAME_LOCATION(vp),
3295 vp, talloc_get_name(vp),
3296 parent, talloc_get_name(parent));
3297#endif
3298
3299 /*
3300 * Check if the child can be in the parent.
3301 */
3303 "CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " should be parented "
3304 "by da %s, but it is instead parented by da %s",
3305 file, line,
3306 PAIR_NAME_LOCATION(child), child->da->parent->name, vp->da->name);
3307
3308 fr_pair_verify(file, line, vp->da, &vp->vp_group, child, verify_values);
3309 }
3310
3311 UNCONST(fr_pair_t *, vp)->vp_group.verified = true;
3312 }
3313 break;
3314
3315 default:
3316 break;
3317 }
3318
3319 if (vp->da->flags.is_unknown || vp->vp_raw) {
3321
3322 /*
3323 * Raw or unknown attributes can have specific data types. See DER and CBOR.
3324 */
3325
3326 } else if (fr_type_is_leaf(vp->vp_type) && (vp->vp_type != vp->da->type) &&
3327 !((vp->da->type == FR_TYPE_COMBO_IP_ADDR) && ((vp->vp_type == FR_TYPE_IPV4_ADDR) || (vp->vp_type == FR_TYPE_IPV6_ADDR))) &&
3328 !((vp->da->type == FR_TYPE_COMBO_IP_PREFIX) && ((vp->vp_type == FR_TYPE_IPV4_PREFIX) || (vp->vp_type == FR_TYPE_IPV6_PREFIX)))) {
3329 char data_type_int[10], da_type_int[10];
3330
3331 snprintf(data_type_int, sizeof(data_type_int), "%u", vp->vp_type);
3332 snprintf(da_type_int, sizeof(da_type_int), "%u", vp->da->type);
3333
3334 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: " PAIR_NAME " attribute "
3335 "data type (%s) does not match da type (%s)",
3336 file, line,
3337 PAIR_NAME_LOCATION(vp),
3338 fr_table_str_by_value(fr_type_table, vp->vp_type, data_type_int),
3339 fr_table_str_by_value(fr_type_table, vp->da->type, da_type_int));
3340 }
3341
3342 UNCONST(fr_pair_t *, vp)->verified = true;
3343 /* leave vp->data.verified alone */
3344}
3345
3346/** Verify a pair list
3347 *
3348 * @param[in] file from which the verification is called
3349 * @param[in] line number in file
3350 * @param[in] expected talloc ctx pairs should have been allocated in
3351 * @param[in] list of fr_pair_ts to verify
3352 * @param[in] verify_values whether we verify the values, too.
3353 */
3354void fr_pair_list_verify(char const *file, int line, TALLOC_CTX const *expected, fr_pair_list_t const *list, bool verify_values)
3355{
3356 fr_pair_t *slow, *fast;
3357#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3358 TALLOC_CTX *parent;
3359#endif
3360
3361 if (fr_pair_list_empty(list)) return; /* Fast path */
3362
3363 /*
3364 * Only verify the list if it has been modified.
3365 */
3366 if (list->verified) return;
3367
3368 for (slow = fr_pair_list_head(list), fast = fr_pair_list_head(list);
3369 slow && fast;
3370 slow = fr_pair_list_next(list, slow), fast = fr_pair_list_next(list, fast)) {
3371 if (!slow->da) {
3372 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_pair_t da pointer was NULL", file, line);
3373 }
3374
3375 /*
3376 * Advances twice as fast as slow...
3377 */
3378 fast = fr_pair_list_next(list, fast);
3379 fr_fatal_assert_msg(fast != slow,
3380 "CONSISTENCY CHECK FAILED %s[%d]: Looping list found. Fast pointer hit "
3381 "slow pointer at \"%s\"",
3382 file, line, slow->da->name);
3383
3384 if (slow->verified) continue;
3385
3386 fr_pair_verify(__FILE__, __LINE__, NULL, list, slow, verify_values);
3387
3388#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3389 parent = talloc_parent(slow);
3390 if (expected && (parent != expected)) {
3391 bad_parent:
3392 fr_log_talloc_report(expected);
3394
3395 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: Expected " PAIR_NAME " to be parented "
3396 "by %p (%s), instead parented by %p (%s)\n",
3397 file, line,
3398 PAIR_NAME_LOCATION(slow),
3399 expected, talloc_get_name(expected),
3400 parent, parent ? talloc_get_name(parent) : "NULL");
3401 }
3402#endif
3403 }
3404
3405 /*
3406 * Check the remaining pairs
3407 */
3408 for (; slow; slow = fr_pair_list_next(list, slow)) {
3409 if (slow->verified) continue;
3410
3411 fr_pair_verify(__FILE__, __LINE__, NULL, list, slow, verify_values);
3412
3413#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3414 parent = talloc_parent(slow);
3415 if (expected && (parent != expected)) goto bad_parent;
3416#endif
3417 }
3418
3419 UNCONST(fr_pair_list_t *, list)->verified = true;
3420}
3421#endif
3422
3423/** Mark up a list of VPs as tainted.
3424 *
3425 */
3427{
3428 if (fr_pair_list_empty(list)) return;
3429
3430 fr_pair_list_foreach(list, vp) {
3432
3433 switch (vp->vp_type) {
3434 case FR_TYPE_STRUCTURAL:
3435 fr_pair_list_tainted(&vp->vp_group);
3436 break;
3437
3438 default:
3439 break;
3440 }
3441
3442 vp->vp_tainted = true;
3443 }
3444}
3445
3446/** Evaluation function for matching if vp matches a given da
3447 *
3448 * @param item pointer to a fr_pair_t
3449 * @param uctx da to match
3450 *
3451 * @return true if the pair matches the da
3452 */
3453bool fr_pair_matches_da(void const *item, void const *uctx)
3454{
3455 fr_pair_t const *vp = item;
3456 fr_dict_attr_t const *da = uctx;
3457 return da == vp->da;
3458}
3459
3460/** Find or allocate a parent attribute.
3461 *
3462 * The input da is somewhere down inside of the da hierarchy. We
3463 * need to recursively find or create parent VPs which match the
3464 * given da.
3465 *
3466 * We find (or add) the VP into the "in" list. Any newly created VP
3467 * is inserted before "next". Or if "next==NULL", at the tail of
3468 * "in".
3469 *
3470 * @param[in] in the parent vp to look in
3471 * @param[in] item if we create a new vp, insert it before this item
3472 * @param[in] da look for vps in the parent which match this da
3473 * @return
3474 * - NULL on OOM
3475 * - parent vp we've found or allocated.
3476 */
3478{
3479 fr_pair_t *parent, *vp;
3480
3482
3483 /*
3484 * We're looking for a parent in the root of the
3485 * dictionary. Find the relevant VP in the current
3486 * container.
3487 *
3488 * If it's not found, allocate it, and insert it into the
3489 * list. Note that we insert it before the given "item"
3490 * vp so that we don't loop over the newly created pair
3491 * as we're processing the list.
3492 */
3493 if (da->flags.is_root || (da->parent == in->da)) {
3494 return in;
3495 }
3496
3497 /*
3498 * We're not at the root. Go find (or create) the parent
3499 * of this da.
3500 */
3501 parent = pair_alloc_parent(in, item, da->parent);
3502 if (!parent) return NULL;
3503
3504 /*
3505 * We have the parent attribute, maybe it already
3506 * contains the da we're looking for?
3507 */
3508 vp = fr_pair_find_by_da(&parent->vp_group, NULL, da);
3509 if (vp) return vp;
3510
3511 /*
3512 * Now that the entire set of parents has been created,
3513 * create the final VP. Make sure it's in the parent,
3514 * and return it.
3515 */
3516 vp = fr_pair_afrom_da(parent, da);
3517 if (!vp) return NULL;
3518
3519 /*
3520 * If we are at the root, and have been provided with
3521 * an entry to insert before, then do that.
3522 */
3523 if (item && da->parent->flags.is_root) {
3524 fr_pair_insert_before(&parent->vp_group, item, vp);
3525 } else {
3526 fr_pair_append(&parent->vp_group, vp);
3527 }
3528 return vp;
3529}
3530
3531/** Parse a list of VPs from a value box.
3532 *
3533 * @param[in] ctx to allocate new VPs in
3534 * @param[out] out list to add new pairs to
3535 * @param[in] dict to use in parsing
3536 * @param[in] box whose value is to be parsed
3537 */
3538void fr_pair_list_afrom_box(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_t const *dict, fr_value_box_t *box)
3539{
3540 fr_pair_parse_t root, relative;
3541
3542 fr_assert(box->type == FR_TYPE_STRING);
3543
3544 root = (fr_pair_parse_t) {
3545 .ctx = ctx,
3546 .da = fr_dict_root(dict),
3547 .list = out,
3548 .dict = dict,
3549 .internal = fr_dict_internal(),
3550 .allow_crlf = true,
3551 .tainted = box->tainted,
3552 };
3553 relative = (fr_pair_parse_t) { };
3554
3555 if (fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(box->vb_strvalue, box->vb_length)) < 0) {
3556 return;
3557 }
3558}
int const char * file
Definition acutest.h:855
va_end(args)
int n
Definition acutest.h:642
static int const char * fmt
Definition acutest.h:638
void int line
Definition acutest.h:810
va_start(args, fmt)
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define RCSID(id)
Definition build.h:603
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:130
#define unlikely(_x)
Definition build.h:486
#define UNUSED
Definition build.h:415
fr_cmp_ret_t
Result of an ordering comparison.
Definition build.h:113
fr_dict_t * dict
Definition common.c:31
static size_t min(size_t x, size_t y)
Definition dbuff.c:66
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
void *(* fr_dcursor_iter_t)(fr_dcursor_t *cursor, void *to_eval, void *uctx)
Callback for implementing custom iterators.
Definition dcursor.h:49
#define fr_dcursor_list_next(_cursor, _ptr)
Definition dcursor.h:810
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
#define fr_dcursor_list(_cursor)
Definition dcursor.h:812
static void * _fr_dcursor_init(fr_dcursor_t *cursor, fr_dlist_head_t const *head, fr_dcursor_iter_t iter, fr_dcursor_iter_t peek, void const *iter_uctx, fr_dcursor_insert_t insert, fr_dcursor_remove_t remove, void const *mod_uctx, bool is_const)
Setup a cursor to iterate over attribute items in dlists.
Definition dcursor.h:735
int fr_log_talloc_report(TALLOC_CTX const *ctx)
Generate a talloc memory report for a context and print to stderr/stdout.
Definition debug.c:972
#define fr_fatal_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:229
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:177
#define fr_fatal_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:222
fr_dict_attr_t const * fr_dict_attr_common_parent(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition dict_util.c:2365
static fr_dict_attr_t * fr_dict_attr_unknown_copy(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Definition dict.h:608
fr_dict_attr_t * fr_dict_attr_unknown_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da))
Copy a known or unknown attribute to produce an unknown attribute with the specified name.
static fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Definition dict.h:635
void fr_dict_attr_verify(char const *file, int line, fr_dict_attr_t const *da)
Definition dict_util.c:5236
bool fr_dict_attr_can_contain(fr_dict_attr_t const *parent, fr_dict_attr_t const *child)
See if a structural da is allowed to contain another da.
Definition dict_util.c:5326
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2717
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:281
void fr_dict_attr_unknown_free(fr_dict_attr_t const **da)
Free dynamically allocated (unknown attributes)
fr_dict_enum_value_t const * fr_dict_enum_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the structure representing an enum value in a fr_dict_attr_t.
Definition dict_util.c:3729
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:5065
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition dict_util.c:3667
char const * name
Enum name.
Definition dict.h:278
Value of an enumerated attribute.
Definition dict.h:277
Test enumeration values.
Definition dict_test.h:92
unsigned int offset
Positive offset from start of structure to fr_dlist_t.
Definition dlist.h:55
fr_dlist_t * next
Definition dlist.h:43
fr_dlist_t entry
Struct holding the head and tail of the list.
Definition dlist.h:52
Head of a doubly linked list.
Definition dlist.h:51
talloc_free(hp)
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
Definition lst.c:121
fr_type_t
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_COMBO_IP_PREFIX
IPv4 or IPv6 address prefix depending on length.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
unsigned char uint8_t
void * memset_explicit(void *ptr, int ch, size_t len)
Definition missing.c:708
bool fr_pair_matches_da(void const *item, void const *uctx)
Evaluation function for matching if vp matches a given da.
Definition pair.c:3453
int fr_pair_insert_before(fr_pair_list_t *list, fr_pair_t *pos, fr_pair_t *to_add)
Add a VP before another VP.
Definition pair.c:1365
fr_pair_t * fr_pair_list_parent(fr_pair_list_t const *list)
Return a pointer to the parent pair which contains this list.
Definition pair.c:937
static void pair_init_from_da(fr_pair_t *vp, fr_dict_attr_t const *da)
Continue initialising an fr_pair_t assigning a da.
Definition pair.c:191
int fr_pair_value_memdup_buffer(fr_pair_t *vp, uint8_t const *src, bool tainted)
Copy data from a talloced buffer into an "octets" data type.
Definition pair.c:2936
fr_pair_t * fr_pair_afrom_da_depth_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da, unsigned int start)
Create a pair (and all intermediate parents), and append it to the list.
Definition pair.c:428
unsigned int fr_pair_count_by_da(fr_pair_list_t const *list, fr_dict_attr_t const *da)
Return the number of instances of a given da in the specified list.
Definition pair.c:693
void fr_pair_list_tainted(fr_pair_list_t *list)
Mark up a list of VPs as tainted.
Definition pair.c:3426
#define NOT_IN_THIS_LIST_MSG
Definition pair.c:560
int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and append)
Definition pair.c:1425
int fr_pair_value_enum_box(fr_value_box_t const **out, fr_pair_t *vp)
Get value box of a VP, optionally prefer enum value.
Definition pair.c:3036
int fr_pair_value_aprintf(fr_pair_t *vp, char const *fmt,...)
Print data into an "string" data type.
Definition pair.c:2671
int fr_pair_delete_by_da_nested(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list, and prune any empty branches.
Definition pair.c:1674
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
Definition pair.c:2312
int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
Change a vp's talloc ctx and insert it into a new list.
Definition pair.c:594
fr_pair_t * fr_pair_root_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
A special allocation function which disables child autofree.
Definition pair.c:242
int fr_pair_value_memdup(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Copy data into an "octets" data type.
Definition pair.c:2906
static int _pair_list_dcursor_remove(fr_dcursor_t *cursor, void *to_remove, UNUSED void *uctx)
Keep attr tree and sublists synced on cursor removal.
Definition pair.c:976
static void * _fr_pair_iter_next_value(fr_dcursor_t *cursor, void *current, UNUSED void *uctx)
Iterate over pairs.
Definition pair.c:1166
static void * fr_pair_iter_next_by_da(fr_dcursor_t *cursor, void *current, void *uctx)
Iterate over pairs with a specified da.
Definition pair.c:649
fr_pair_t * fr_pair_find_by_da_nested(fr_pair_list_t const *list, fr_dict_attr_t const *da)
Find a pair with a matching fr_dict_attr_t, by walking the nested fr_dict_attr_t tree.
Definition pair.c:791
fr_cmp_ret_t fr_pair_list_cmp(fr_pair_list_t const *a, fr_pair_list_t const *b)
Determine equality of two lists.
Definition pair.c:2036
void fr_pair_validate_debug(fr_pair_t const *failed[2])
Write an error to the library errorbuff detailing the mismatch.
Definition pair.c:2078
int fr_pair_value_strdup(fr_pair_t *vp, char const *src, bool tainted)
Copy data into an "string" data type.
Definition pair.c:2607
int fr_pair_value_bstrdup_buffer_shallow(fr_pair_t *vp, char const *src, bool tainted)
Assign a string to a "string" type value pair.
Definition pair.c:2827
void fr_pair_init_null(fr_pair_t *vp)
Initialise fields in an fr_pair_t without assigning a da.
Definition pair.c:150
int fr_pair_value_from_str(fr_pair_t *vp, char const *value, size_t inlen, fr_sbuff_unescape_rules_t const *uerules, UNUSED bool tainted)
Convert string value to native attribute value.
Definition pair.c:2561
fr_pair_list_t * fr_pair_parent_list(fr_pair_t const *vp)
Return a pointer to the parent pair list.
Definition pair.c:915
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:716
fr_pair_t * fr_pair_find_by_child_num_idx(fr_pair_list_t const *list, fr_dict_attr_t const *parent, unsigned int attr, unsigned int idx)
Find the pair with the matching child attribute at a given index.
Definition pair.c:881
int fr_pair_cmp(fr_pair_t const *a, fr_pair_t const *b)
Compare two pairs, using the operator from "a".
Definition pair.c:1958
fr_pair_list_t * fr_pair_list_alloc(TALLOC_CTX *ctx)
Allocate a new pair list on the heap.
Definition pair.c:120
int fr_pair_value_bstrndup_shallow(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Assign a string to a "string" type value pair.
Definition pair.c:2807
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:1306
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
Definition pair.c:1650
fr_pair_t * fr_pair_parent(fr_pair_t const *vp)
Return a pointer to the parent pair.
Definition pair.c:923
fr_cmp_ret_t fr_pair_cmp_by_da(void const *a, void const *b)
Order attributes by their da.
Definition pair.c:1805
fr_pair_t * _fr_pair_dcursor_by_ancestor_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dict_attr_t const *da, bool is_const)
Initialise a cursor that will return only attributes descended from the specified fr_dict_attr_t.
Definition pair.c:1128
static void pair_init_null(fr_pair_t *vp)
Initialise fields in an fr_pair_t without assigning a da.
Definition pair.c:136
void fr_pair_replace(fr_pair_list_t *list, fr_pair_t *to_replace, fr_pair_t *vp)
Replace a given VP.
Definition pair.c:1398
fr_pair_t * fr_pair_find_by_da_idx(fr_pair_list_t const *list, fr_dict_attr_t const *da, unsigned int idx)
Find a pair with a matching da at a given index.
Definition pair.c:763
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:284
static fr_cmp_ret_t pair_cmp_by_num(void const *a, void const *b)
Order attributes by their attribute number, and tag.
Definition pair.c:1853
static int _pair_list_dcursor_insert(fr_dcursor_t *cursor, void *to_insert, UNUSED void *uctx)
Keep attr tree and sublists synced on cursor insert.
Definition pair.c:954
int fr_pair_value_bstrdup_buffer(fr_pair_t *vp, char const *src, bool tainted)
Copy a nul terminated talloced buffer a "string" type value pair.
Definition pair.c:2782
int fr_pair_update_by_da_parent(fr_pair_t *parent, fr_pair_t **out, fr_dict_attr_t const *da)
Return the first fr_pair_t matching the fr_dict_attr_t or alloc a new fr_pair_t and its subtree (and ...
Definition pair.c:1556
int fr_pair_list_copy_by_ancestor(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_dict_attr_t const *parent_da)
Duplicate pairs in a list where the da is a descendant of parent_da.
Definition pair.c:2444
static int _fr_pair_free(fr_pair_t *vp)
Free a fr_pair_t.
Definition pair.c:70
bool fr_pair_validate(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check.
Definition pair.c:2113
int fr_pair_steal_append(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp)
Change a vp's talloc ctx and insert it into a new list.
Definition pair.c:571
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:47
static fr_pair_t * pair_alloc_parent(fr_pair_t *in, fr_pair_t *item, fr_dict_attr_t const *da)
Find or allocate a parent attribute.
Definition pair.c:3477
int fr_pair_value_mem_realloc(fr_pair_t *vp, uint8_t **out, size_t size)
Change the length of a buffer for a "octets" type value pair.
Definition pair.c:2880
fr_pair_t * fr_pair_find_last_by_da(fr_pair_list_t const *list, fr_dict_attr_t const *da)
Find the last pair with a matching da.
Definition pair.c:739
char const * fr_pair_value_enum(fr_pair_t const *vp, char buff[20])
Return a const buffer for an enum type attribute.
Definition pair.c:3003
int fr_pair_value_bstrndup(fr_pair_t *vp, char const *src, size_t len, bool tainted)
Copy data into a "string" type value pair.
Definition pair.c:2756
int fr_pair_value_bstr_alloc(fr_pair_t *vp, char **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "string" type value pair.
Definition pair.c:2703
fr_pair_t * fr_pair_alloc_null(TALLOC_CTX *ctx)
Dynamically allocate a new attribute with no fr_dict_attr_t assigned.
Definition pair.c:170
int fr_pair_value_bstr_realloc(fr_pair_t *vp, char **out, size_t size)
Change the length of a buffer for a "string" type value pair.
Definition pair.c:2728
static void * fr_pair_iter_next_by_ancestor(fr_dcursor_t *cursor, void *current, void *uctx)
Iterate over pairs which are decedents of the specified da.
Definition pair.c:672
fr_cmp_ret_t fr_pair_cmp_by_parent_num(void const *a, void const *b)
Order attributes by their parent(s), attribute number, and tag.
Definition pair.c:1910
bool fr_pair_immutable(fr_pair_t const *vp)
Definition pair.c:2265
void fr_pair_value_clear(fr_pair_t *vp)
Free/zero out value (or children) of a given VP.
Definition pair.c:2494
int fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list and free.
Definition pair.c:1787
int fr_pair_append_by_da_parent(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t, adding the parent attributes if required.
Definition pair.c:1482
static fr_dlist_head_t value_dlist
Definition pair.c:1192
int fr_pair_delete_by_child_num(fr_pair_list_t *list, fr_dict_attr_t const *parent, unsigned int attr)
Delete matching pairs from the specified list.
Definition pair.c:1769
int fr_pair_value_copy(fr_pair_t *dst, fr_pair_t *src)
Copy the value from one pair to another.
Definition pair.c:2524
fr_pair_t * _fr_pair_dcursor_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, bool is_const)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.c:1091
int fr_pair_reinit_from_da(fr_pair_list_t *list, fr_pair_t *vp, fr_dict_attr_t const *da)
Re-initialise an attribute with a different da.
Definition pair.c:321
int fr_pair_steal(TALLOC_CTX *ctx, fr_pair_t *vp)
Steal one VP.
Definition pair.c:543
int fr_pair_value_memdup_shallow(fr_pair_t *vp, uint8_t const *src, size_t len, bool tainted)
Assign a buffer to a "octets" type value pair.
Definition pair.c:2961
bool fr_pair_validate_relaxed(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check.
Definition pair.c:2190
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:509
fr_value_box_t * fr_pair_dcursor_nested_init(fr_dcursor_t *cursor, fr_dcursor_t *parent)
Initialises a special dcursor over another cursor which returns fr_pair_t, but we return fr_value_box...
Definition pair.c:1259
#define IN_A_LIST_MSG
Definition pair.c:559
fr_pair_t * _fr_pair_dcursor_by_da_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dict_attr_t const *da, bool is_const)
Initialise a cursor that will return only attributes matching the specified fr_dict_attr_t.
Definition pair.c:1109
static void * _fr_pair_iter_next_dcursor_value(UNUSED fr_dcursor_t *cursor, void *current, void *uctx)
Iterate over pairs.
Definition pair.c:1228
int fr_pair_value_mem_alloc(fr_pair_t *vp, uint8_t **out, size_t size, bool tainted)
Pre-allocate a memory buffer for a "octets" type value pair.
Definition pair.c:2855
int fr_pair_value_strtrim(fr_pair_t *vp)
Trim the length of the string buffer to match the length of the C string.
Definition pair.c:2650
int fr_pair_insert_after(fr_pair_list_t *list, fr_pair_t *pos, fr_pair_t *to_add)
Add a VP after another VP.
Definition pair.c:1332
fr_pair_t * fr_pair_afrom_da_nested(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_dict_attr_t const *da)
Create a pair (and all intermediate parents), and append it to the list.
Definition pair.c:486
fr_pair_list_t * fr_pair_children(fr_pair_t *vp)
Get the child list of a group.
Definition pair.c:905
int fr_pair_prepend(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the start of the list.
Definition pair.c:1275
fr_value_box_t * fr_pair_dcursor_value_init(fr_dcursor_t *cursor)
Initialises a special dcursor over a fr_pair_list_t, but which returns fr_value_box_t.
Definition pair.c:1212
void fr_pair_list_afrom_box(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_t const *dict, fr_value_box_t *box)
Parse a list of VPs from a value box.
Definition pair.c:3538
fr_pair_t * fr_pair_list_iter_leaf(fr_pair_list_t *list, fr_pair_t *vp)
Iterates over the leaves of a list.
Definition pair.c:1006
void fr_pair_list_steal(TALLOC_CTX *ctx, fr_pair_list_t *list)
Steal a list of pairs to a new context.
Definition pair.c:2289
int fr_pair_list_copy_by_da(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from, fr_dict_attr_t const *da)
Duplicate pairs in a list matching the specified da.
Definition pair.c:2400
int fr_pair_raw_afrom_pair(fr_pair_t *vp, uint8_t const *data, size_t data_len)
Mark malformed attribute as raw.
Definition pair.c:618
int fr_pair_value_memdup_buffer_shallow(fr_pair_t *vp, uint8_t const *src, bool tainted)
Assign a talloced buffer to a "octets" type value pair.
Definition pair.c:2981
int fr_pair_value_strdup_shallow(fr_pair_t *vp, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a vp, but don't copy it.
Definition pair.c:2631
int fr_pair_prepend_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and prepend)
Definition pair.c:1452
fr_pair_t * _fr_pair_dcursor_iter_init(fr_dcursor_t *cursor, fr_pair_list_t const *list, fr_dcursor_iter_t iter, void const *uctx, bool is_const)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.c:1071
int fr_pair_list_copy_to_box(fr_value_box_t *dst, fr_pair_list_t *from)
Copy the contents of a pair list to a set of value-boxes.
Definition pair.c:2347
fr_pair_t * fr_pair_afrom_child_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Create a new valuepair.
Definition pair.c:390
fr_slen_t fr_pair_list_afrom_substr(fr_pair_parse_t const *root, fr_pair_parse_t *relative, fr_sbuff_t *in)
Parse a fr_pair_list_t from a substring.
TALLOC_CTX * ctx
Definition pair_legacy.h:43
void fr_proto_da_stack_build(fr_da_stack_t *stack, fr_dict_attr_t const *da)
Build a complete DA stack from the da back to the root.
Definition proto.c:118
#define fr_assert(_expr)
Definition rad_assert.h:37
#define FR_SBUFF_IN(_start, _len_or_end)
#define FR_SBUFF_OUT(_start, _len_or_end)
Set of parsing rules for *unescape_until functions.
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:37
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
return count
Definition module.c:155
fr_pair_t * vp
bool _CONST is_child
is a child of a VP
Definition pair.h:55
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:804
#define talloc_get_type_abort_const
Definition talloc.h:117
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
#define FR_TLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for tlists.
Definition tlist.h:800
#define FR_TLIST_PARENT_FUNCS(_name, _element_type, _list_type, _list_field)
Define a type specific function to find the structure which contains a tlist head.
Definition tlist.h:940
@ T_OP_CMP_TRUE
Definition token.h:102
@ T_BARE_WORD
Definition token.h:118
@ T_OP_EQ
Definition token.h:81
@ T_OP_CMP_FALSE
Definition token.h:103
@ T_OP_REG_EQ
Definition token.h:100
@ T_OP_REG_NE
Definition token.h:101
#define ATTRIBUTE_EQ(_x, _y)
Definition pair.h:154
#define fr_pair_cmp_op(_op, _a, _b)
Compare two attributes using and operator.
Definition pair.h:678
#define PAIR_ALLOCED(_x)
Definition pair.h:217
static fr_slen_t fr_pair_aprint(TALLOC_CTX *ctx, char **out, fr_dict_attr_t const *parent, fr_pair_t const *vp) 1(fr_pair_print
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define PAIR_VERIFY(_x)
Definition pair.h:209
#define fr_pair_cmp_by_da_mutable
Definition pair.h:691
fr_pair_t * fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition pair_inline.c:69
#define vp_group
Definition pair.h:143
fr_pair_t * fr_pair_remove(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list without freeing.
Definition pair_inline.c:93
#define fr_pair_list_foreach(_list_head, _iter)
Iterate over the contents of a fr_pair_list_t.
Definition pair.h:285
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src)
Appends a list of fr_pair_t from a temporary list to a destination list.
#define PAIR_VERIFY_WITH_LIST(_l, _x)
Definition pair.h:210
int fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp)
Sort a doubly linked list of fr_pair_ts using merge sort.
ssize_t fr_pair_print_value_quoted(fr_sbuff_t *out, fr_pair_t const *vp, fr_token_t quote)
Print the value of an attribute to a string.
Definition pair_print.c:59
#define PAIR_LIST_VERIFY(_x)
Definition pair.h:212
fr_pair_t * fr_pair_list_prev(fr_pair_list_t const *list, fr_pair_t const *item))
Get the previous item in a valuepair list before a specific entry.
Definition pair_inline.c:82
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
size_t fr_pair_list_num_elements(fr_pair_list_t const *list)
Get the length of a list of fr_pair_t.
static fr_slen_t parent
Definition pair.h:864
uint8_t depth
Deepest attribute in the stack.
Definition proto.h:56
fr_dict_attr_t const * da[FR_DICT_MAX_TLV_STACK+1]
The stack.
Definition proto.h:57
Structure for holding the stack of dictionary attributes being encoded.
Definition proto.h:55
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:581
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84
#define fr_strerror_const(_msg)
Definition strerror.h:223
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:83
#define fr_type_is_group(_x)
Definition types.h:376
#define fr_type_is_structural(_x)
Definition types.h:392
#define FR_TYPE_STRUCTURAL
Definition types.h:316
#define fr_type_is_leaf(_x)
Definition types.h:393
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
fr_cmp_ret_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:770
void fr_value_box_memdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, bool tainted)
Assign a talloced buffer to a box, but don't copy it.
Definition value.c:5357
fr_slen_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:6256
int fr_value_box_vasprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted, char const *fmt, va_list ap)
Print a formatted string using our internal printf wrapper and assign it to a value box.
Definition value.c:4820
int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb)
Trim the length of the string buffer to match the length of the C string.
Definition value.c:4789
int fr_value_box_mem_alloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, size_t len, bool tainted)
Pre-allocate an octets buffer for filling by the caller.
Definition value.c:5161
int fr_value_box_memdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, bool tainted)
Copy a talloced buffer to a fr_value_box_t.
Definition value.c:5317
int fr_value_box_bstrdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated talloced buffer to a fr_value_box_t.
Definition value.c:5077
int fr_value_box_mem_realloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:5194
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:4526
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:4326
void fr_value_box_memdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Assign a buffer to a box, but don't copy it.
Definition value.c:5339
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:4461
void fr_value_box_verify(char const *file, int line, fr_value_box_t const *vb)
Validation function to check that a fr_value_box_t is correctly initialised.
Definition value.c:7404
void fr_value_box_set_secret(fr_value_box_t *box, bool secret)
Mark a box as holding a secret, or not.
Definition value.c:7638
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4763
void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4873
int fr_value_box_bstr_alloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, size_t len, bool tainted)
Alloc and assign an empty \0 terminated string to a fr_value_box_t.
Definition value.c:4940
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:4509
int fr_value_box_bstr_realloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:4973
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:5014
int fr_value_box_bstrdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a talloced buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:5122
void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Assign a string to to a fr_value_box_t.
Definition value.c:5101
int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Copy a buffer to a fr_value_box_t.
Definition value.c:5255
#define fr_box_is_numeric(_x)
Definition value.h:490
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:668
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static bool fr_value_box_is_secret(fr_value_box_t const *box)
Definition value.h:1143
static fr_slen_t data
Definition value.h:1366
#define fr_value_box_is_safe_for_only(_box, _safe_for)
Definition value.h:1132
static fr_sbuff_err_t char ** out
Definition value.h:1061
static fr_sbuff_err_t char size_t * len
Definition value.h:1061
#define FR_VALUE_BOX_SAFE_FOR_NONE
Definition value.h:177
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:634