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