2 * pair.c Functions to handle VALUE_PAIRs
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
25 #include <freeradius-devel/libradius.h>
26 #include <freeradius-devel/regex.h>
32 * @note Do not call directly, use talloc_free instead.
37 static int _fr_pair_free(VALUE_PAIR *vp) {
39 vp->vp_integer = 0xf4eef4ee;
43 talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL);
48 /** Dynamically allocate a new attribute
50 * Allocates a new attribute and a new dictionary attr if no DA is provided.
52 * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
53 * @param[in] da Specifies the dictionary attribute to build the VP from.
54 * @return a new value pair or NULL if an error occurred.
56 VALUE_PAIR *fr_pair_afrom_da(TALLOC_CTX *ctx, DICT_ATTR const *da)
61 * Caller must specify a da else we don't know what the attribute type is.
64 fr_strerror_printf("Invalid arguments");
68 vp = talloc_zero(ctx, VALUE_PAIR);
70 fr_strerror_printf("Out of memory");
79 vp->vp_length = da->flags.length;
81 talloc_set_destructor(vp, _fr_pair_free);
86 /** Create a new valuepair
88 * If attr and vendor match a dictionary entry then a VP with that DICT_ATTR
91 * If attr or vendor are uknown will call dict_attruknown to create a dynamic
92 * DICT_ATTR of PW_TYPE_OCTETS.
94 * Which type of DICT_ATTR the VALUE_PAIR was created with can be determined by
95 * checking @verbatim vp->da->flags.is_unknown @endverbatim.
97 * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
98 * @param[in] attr number.
99 * @param[in] vendor number.
100 * @return the new valuepair or NULL on error.
102 VALUE_PAIR *fr_pair_afrom_num(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
106 da = dict_attrbyvalue(attr, vendor);
108 da = dict_unknown_afrom_fields(ctx, attr, vendor);
114 return fr_pair_afrom_da(ctx, da);
117 /** Free memory used by a valuepair list.
119 * @todo TLV: needs to free all dependents of each VP freed.
121 void fr_pair_list_free(VALUE_PAIR **vps)
130 for (vp = fr_cursor_init(&cursor, vps);
132 vp = fr_cursor_next(&cursor)) {
140 /** Mark malformed or unrecognised attributed as unknown
142 * @param vp to change DICT_ATTR of.
143 * @return 0 on success (or if already unknown) else -1 on error.
145 int fr_pair_to_unknown(VALUE_PAIR *vp)
150 if (vp->da->flags.is_unknown) {
154 da = dict_unknown_afrom_fields(vp, vp->da->attr, vp->da->vendor);
164 /** Find the pair with the matching DAs
167 VALUE_PAIR *fr_pair_find_by_da(VALUE_PAIR *vp, DICT_ATTR const *da, int8_t tag)
175 (void) fr_cursor_init(&cursor, &vp);
176 return fr_cursor_next_by_da(&cursor, da, tag);
180 /** Find the pair with the matching attribute
182 * @todo should take DAs and do a pointer comparison.
184 VALUE_PAIR *fr_pair_find_by_num(VALUE_PAIR *vp, unsigned int attr, unsigned int vendor, int8_t tag)
188 /* List head may be NULL if it contains no VPs */
189 if (!vp) return NULL;
193 (void) fr_cursor_init(&cursor, &vp);
194 return fr_cursor_next_by_num(&cursor, attr, vendor, tag);
197 /** Delete matching pairs
199 * Delete matching pairs from the attribute list.
201 * @param[in,out] first VP in list.
202 * @param[in] attr to match.
203 * @param[in] vendor to match.
204 * @param[in] tag to match. TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
206 * @todo should take DAs and do a point comparison.
208 void fr_pair_delete_by_num(VALUE_PAIR **first, unsigned int attr, unsigned int vendor, int8_t tag)
210 VALUE_PAIR *i, *next;
211 VALUE_PAIR **last = first;
213 for(i = *first; i; i = next) {
216 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
217 (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
226 /** Add a VP to the end of the list.
228 * Locates the end of 'first', and links an additional VP 'add' at the end.
230 * @param[in] first VP in linked list. Will add new VP to the end of this list.
231 * @param[in] add VP to add to list.
233 void fr_pair_add(VALUE_PAIR **first, VALUE_PAIR *add)
241 if (*first == NULL) {
246 for (i = *first; i->next; i = i->next) {
247 #ifdef WITH_VERIFY_PTR
250 * The same VP should never by added multiple times
260 /** Replace all matching VPs
262 * Walks over 'first', and replaces the first VP that matches 'replace'.
264 * @note Memory used by the VP being replaced will be freed.
265 * @note Will not work with unknown attributes.
267 * @param[in,out] first VP in linked list. Will search and replace in this list.
268 * @param[in] replace VP to replace.
270 void fr_pair_replace(VALUE_PAIR **first, VALUE_PAIR *replace)
272 VALUE_PAIR *i, *next;
273 VALUE_PAIR **prev = first;
277 if (*first == NULL) {
283 * Not an empty list, so find item if it is there, and
284 * replace it. Note, we always replace the first one, and
285 * we ignore any others that might exist.
287 for(i = *first; i; i = next) {
292 * Found the first attribute, replace it,
295 if ((i->da == replace->da) && (!i->da->flags.has_tag || TAG_EQ(replace->tag, i->tag))) {
299 * Should really assert that replace->next == NULL
301 replace->next = next;
307 * Point to where the attribute should go.
313 * If we got here, we didn't find anything to replace, so
314 * stopped at the last item, which we just append to.
319 int8_t fr_pair_cmp_by_da_tag(void const *a, void const *b)
321 VALUE_PAIR const *my_a = a;
322 VALUE_PAIR const *my_b = b;
329 cmp = fr_pointer_cmp(my_a->da, my_b->da);
330 if (cmp != 0) return cmp;
332 if (my_a->tag < my_b->tag) return -1;
334 if (my_a->tag > my_b->tag) return 1;
339 static void fr_pair_list_sort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
345 * Stopping condition - no more elements left to split
347 if (!source || !source->next) {
355 * Fast advances twice as fast as slow, so when it gets to the end,
356 * slow will point to the middle of the linked list.
374 static VALUE_PAIR *fr_pair_list_sort_merge(VALUE_PAIR *a, VALUE_PAIR *b, fr_cmp_t cmp)
376 VALUE_PAIR *result = NULL;
382 * Compare the DICT_ATTRs and tags
384 if (cmp(a, b) <= 0) {
386 result->next = fr_pair_list_sort_merge(a->next, b, cmp);
389 result->next = fr_pair_list_sort_merge(a, b->next, cmp);
395 /** Sort a linked list of VALUE_PAIRs using merge sort
397 * @param[in,out] vps List of VALUE_PAIRs to sort.
398 * @param[in] cmp to sort with
400 void fr_pair_list_sort(VALUE_PAIR **vps, fr_cmp_t cmp)
402 VALUE_PAIR *head = *vps;
407 * If there's 0-1 elements it must already be sorted.
409 if (!head || !head->next) {
413 fr_pair_list_sort_split(head, &a, &b); /* Split into sublists */
414 fr_pair_list_sort(&a, cmp); /* Traverse left */
415 fr_pair_list_sort(&b, cmp); /* Traverse right */
418 * merge the two sorted lists together
420 *vps = fr_pair_list_sort_merge(a, b, cmp);
423 /** Write an error to the library errorbuff detailing the mismatch
425 * Retrieve output with fr_strerror();
427 * @todo add thread specific talloc contexts.
429 * @param ctx a hack until we have thread specific talloc contexts.
430 * @param failed pair of attributes which didn't match.
432 void fr_pair_validate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2])
434 VALUE_PAIR const *filter = failed[0];
435 VALUE_PAIR const *list = failed[1];
439 (void) fr_strerror(); /* Clear any existing messages */
441 if (!fr_assert(!(!filter && !list))) return;
445 fr_strerror_printf("Attribute \"%s\" not found in list", filter->da->name);
449 if (!filter || (filter->da != list->da)) {
450 fr_strerror_printf("Attribute \"%s\" not found in filter", list->da->name);
454 if (!TAG_EQ(filter->tag, list->tag)) {
455 fr_strerror_printf("Attribute \"%s\" tag \"%i\" didn't match filter tag \"%i\"",
456 list->da->name, list->tag, filter->tag);
461 value = vp_aprints_value(ctx, list, '"');
462 str = vp_aprints(ctx, filter, '"');
464 fr_strerror_printf("Attribute value \"%s\" didn't match filter: %s", value, str);
472 /** Uses fr_pair_cmp to verify all VALUE_PAIRs in list match the filter defined by check
474 * @note will sort both filter and list in place.
476 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
478 * @param filter attributes to check list against.
479 * @param list attributes, probably a request or reply
481 bool fr_pair_validate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
483 vp_cursor_t filter_cursor;
484 vp_cursor_t list_cursor;
486 VALUE_PAIR *check, *match;
488 if (!filter && !list) {
493 * This allows us to verify the sets of validate and reply are equal
494 * i.e. we have a validate rule which matches every reply attribute.
496 * @todo this should be removed one we have sets and lists
498 fr_pair_list_sort(&filter, fr_pair_cmp_by_da_tag);
499 fr_pair_list_sort(&list, fr_pair_cmp_by_da_tag);
501 check = fr_cursor_init(&filter_cursor, &filter);
502 match = fr_cursor_init(&list_cursor, &list);
503 while (match || check) {
505 * Lists are of different lengths
507 if (!match || !check) goto mismatch;
510 * The lists are sorted, so if the first
511 * attributes aren't of the same type, then we're
514 if (!ATTRIBUTE_EQ(check, match)) goto mismatch;
517 * They're of the same type, but don't have the
518 * same values. This is a problem.
520 * Note that the RFCs say that for attributes of
521 * the same type, order is important.
523 if (fr_pair_cmp(check, match) != 1) goto mismatch;
525 check = fr_cursor_next(&filter_cursor);
526 match = fr_cursor_next(&list_cursor);
539 /** Uses fr_pair_cmp to verify all VALUE_PAIRs in list match the filter defined by check
541 * @note will sort both filter and list in place.
543 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
545 * @param filter attributes to check list against.
546 * @param list attributes, probably a request or reply
548 bool fr_pair_validate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
550 vp_cursor_t filter_cursor;
551 vp_cursor_t list_cursor;
553 VALUE_PAIR *check, *last_check = NULL, *match = NULL;
555 if (!filter && !list) {
560 * This allows us to verify the sets of validate and reply are equal
561 * i.e. we have a validate rule which matches every reply attribute.
563 * @todo this should be removed one we have sets and lists
565 fr_pair_list_sort(&filter, fr_pair_cmp_by_da_tag);
566 fr_pair_list_sort(&list, fr_pair_cmp_by_da_tag);
568 fr_cursor_init(&list_cursor, &list);
569 for (check = fr_cursor_init(&filter_cursor, &filter);
571 check = fr_cursor_next(&filter_cursor)) {
573 * Were processing check attributes of a new type.
575 if (!ATTRIBUTE_EQ(last_check, check)) {
577 * Record the start of the matching attributes in the pair list
578 * For every other operator we require the match to be present
580 match = fr_cursor_next_by_da(&list_cursor, check->da, check->tag);
582 if (check->op == T_OP_CMP_FALSE) continue;
586 fr_cursor_init(&list_cursor, &match);
591 * Now iterate over all attributes of the same type.
593 for (match = fr_cursor_first(&list_cursor);
594 ATTRIBUTE_EQ(match, check);
595 match = fr_cursor_next(&list_cursor)) {
597 * This attribute passed the filter
599 if (!fr_pair_cmp(check, match)) goto mismatch;
613 /** Copy a single valuepair
615 * Allocate a new valuepair and copy the da from the old vp.
617 * @param[in] ctx for talloc
618 * @param[in] vp to copy.
619 * @return a copy of the input VP or NULL on error.
621 VALUE_PAIR *fr_pair_copy(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
625 if (!vp) return NULL;
629 n = fr_pair_afrom_da(ctx, vp->da);
632 memcpy(n, vp, sizeof(*n));
635 * If the DA is unknown, steal "n" to "ctx". This does
636 * nothing for "n", but will also copy the unknown "da".
638 if (n->da->flags.is_unknown) {
639 fr_pair_steal(ctx, n);
645 * If it's an xlat, copy the raw string and return early,
646 * so we don't pre-expand or otherwise mangle the VALUE_PAIR.
648 if (vp->type == VT_XLAT) {
649 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
653 switch (vp->da->type) {
655 n->vp_octets = NULL; /* else fr_pair_value_memcpy will free vp's value */
656 fr_pair_value_memcpy(n, vp->vp_octets, n->vp_length);
660 n->vp_strvalue = NULL; /* else pairstrnpy will free vp's value */
661 fr_pair_value_bstrncpy(n, vp->vp_strvalue, n->vp_length);
673 * Copy all pairs from 'from' regardless of tag, attribute or vendor.
675 * @param[in] ctx for new VALUE_PAIRs to be allocated in.
676 * @param[in] from whence to copy VALUE_PAIRs.
677 * @return the head of the new VALUE_PAIR list or NULL on error.
679 VALUE_PAIR *fr_pair_list_copy(TALLOC_CTX *ctx, VALUE_PAIR *from)
681 vp_cursor_t src, dst;
683 VALUE_PAIR *out = NULL, *vp;
685 fr_cursor_init(&dst, &out);
686 for (vp = fr_cursor_init(&src, &from);
688 vp = fr_cursor_next(&src)) {
690 vp = fr_pair_copy(ctx, vp);
692 fr_pair_list_free(&out);
695 fr_cursor_insert(&dst, vp); /* fr_pair_list_copy sets next pointer to NULL */
701 /** Copy matching pairs
703 * Copy pairs of a matching attribute number, vendor number and tag from the
704 * the input list to a new list, and returns the head of this list.
706 * @param[in] ctx for talloc
707 * @param[in] from whence to copy VALUE_PAIRs.
708 * @param[in] attr to match, if 0 input list will not be filtered by attr.
709 * @param[in] vendor to match.
710 * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
711 * @return the head of the new VALUE_PAIR list or NULL on error.
713 VALUE_PAIR *fr_pair_list_copy_by_num(TALLOC_CTX *ctx, VALUE_PAIR *from,
714 unsigned int attr, unsigned int vendor, int8_t tag)
716 vp_cursor_t src, dst;
718 VALUE_PAIR *out = NULL, *vp;
720 fr_cursor_init(&dst, &out);
721 for (vp = fr_cursor_init(&src, &from);
723 vp = fr_cursor_next(&src)) {
726 if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
730 if (vp->da->flags.has_tag && !TAG_EQ(tag, vp->tag)) {
734 vp = fr_pair_copy(ctx, vp);
736 fr_pair_list_free(&out);
739 fr_cursor_insert(&dst, vp);
747 * @param[in] ctx to move VALUE_PAIR into
748 * @param[in] vp VALUE_PAIR to move into the new context.
750 void fr_pair_steal(TALLOC_CTX *ctx, VALUE_PAIR *vp)
752 (void) talloc_steal(ctx, vp);
755 * The DA may be unknown. If we're stealing the VPs to a
756 * different context, copy the unknown DA. We use the VP
757 * as a context here instead of "ctx", so that when the
758 * VP is freed, so is the DA.
760 * Since we have no introspection into OTHER VPs using
761 * the same DA, we can't have multiple VPs use the same
762 * DA. So we might as well tie it to this VP.
764 if (vp->da->flags.is_unknown) {
769 size = talloc_get_size(vp->da);
771 p = talloc_zero_array(vp, char, size);
772 da = (DICT_ATTR *) p;
773 talloc_set_type(p, DICT_ATTR);
774 memcpy(da, vp->da, size);
779 /** Move pairs from source list to destination list respecting operator
781 * @note This function does some additional magic that's probably not needed
782 * in most places. Consider using radius_pairmove in server code.
784 * @note fr_pair_list_free should be called on the head of the source list to free
785 * unmoved attributes (if they're no longer needed).
787 * @note Does not respect tags when matching.
789 * @param[in] ctx for talloc
790 * @param[in,out] to destination list.
791 * @param[in,out] from source list.
793 * @see radius_pairmove
795 void fr_pair_list_move(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from)
797 VALUE_PAIR *i, *found;
798 VALUE_PAIR *head_new, **tail_new;
799 VALUE_PAIR **tail_from;
801 if (!to || !from || !*from) return;
804 * We're editing the "to" list while we're adding new
805 * attributes to it. We don't want the new attributes to
806 * be edited, so we create an intermediate list to hold
807 * them during the editing process.
810 tail_new = &head_new;
813 * We're looping over the "from" list, moving some
814 * attributes out, but leaving others in place.
817 while ((i = *tail_from) != NULL) {
823 * We never move Fall-Through.
825 if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
826 tail_from = &(i->next);
831 * Unlike previous versions, we treat all other
832 * attributes as normal. i.e. there's no special
833 * treatment for passwords or Hint.
838 * Anything else are operators which
839 * shouldn't occur. We ignore them, and
840 * leave them in place.
843 tail_from = &(i->next);
847 * Add it to the "to" list, but only if
848 * it doesn't already exist.
851 found = fr_pair_find_by_da(*to, i->da, TAG_ANY);
852 if (!found) goto do_add;
854 tail_from = &(i->next);
858 * Add it to the "to" list, and delete any attribute
859 * of the same vendor/attr which already exists.
862 found = fr_pair_find_by_da(*to, i->da, TAG_ANY);
863 if (!found) goto do_add;
866 * Do NOT call fr_pair_delete_by_num() here,
867 * due to issues with re-writing
868 * "request->username".
870 * Everybody calls fr_pair_move, and
871 * expects it to work. We can't
872 * update request->username here,
873 * so instead we over-write the
874 * vp that it's pointing to.
876 switch (found->da->type) {
879 memcpy(found, i, sizeof(*found));
884 fr_pair_value_memsteal(found, i->vp_octets);
889 fr_pair_value_strsteal(found, i->vp_strvalue);
890 i->vp_strvalue = NULL;
896 * Delete *all* of the attributes
897 * of the same number.
899 fr_pair_delete_by_num(&found->next,
901 found->da->vendor, TAG_ANY);
904 * Remove this attribute from the
907 *tail_from = i->next;
909 fr_pair_list_free(&i);
913 * Move it from the old list and add it
918 *tail_from = i->next;
921 fr_pair_steal(ctx, i);
922 tail_new = &(i->next);
925 } /* loop over the "from" list. */
928 * Take the "new" list, and append it to the "to" list.
930 fr_pair_add(to, head_new);
933 /** Move matching pairs between VALUE_PAIR lists
935 * Move pairs of a matching attribute number, vendor number and tag from the
936 * the input list to the output list.
938 * @note fr_pair_list_free should be called on the head of the old list to free unmoved
939 attributes (if they're no longer needed).
941 * @param[in] ctx for talloc
942 * @param[in,out] to destination list.
943 * @param[in,out] from source list.
944 * @param[in] attr to match. If attribute PW_VENDOR_SPECIFIC and vendor 0,
945 * will match (and therefore copy) only VSAs.
946 * If attribute 0 and vendor 0 will match (and therefore copy) all
948 * @param[in] vendor to match.
949 * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
950 * @param[in] move if set to "true", VPs are moved. If set to "false", VPs are copied, and the old one deleted.
952 static void fr_pair_list_move_by_num_internal(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
953 unsigned int attr, unsigned int vendor, int8_t tag,
956 VALUE_PAIR *to_tail, *i, *next, *this;
957 VALUE_PAIR *iprev = NULL;
960 * Find the last pair in the "to" list and put it in "to_tail".
962 * @todo: replace the "if" with "VALUE_PAIR **tail"
966 for(i = *to; i; i = i->next) {
974 * Attr/vendor of 0 means "move them all".
975 * It's better than "fr_pair_add(foo,bar);bar=NULL"
977 if ((vendor == 0) && (attr == 0)) {
979 to_tail->next = *from;
984 for (i = *from; i; i = i->next) {
985 fr_pair_steal(ctx, i);
992 for(i = *from; i; i = next) {
996 if (i->da->flags.has_tag && !TAG_EQ(tag, i->tag)) {
1002 * vendor=0, attr = PW_VENDOR_SPECIFIC means
1003 * "match any vendor attribute".
1005 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
1007 * It's a VSA: move it over.
1009 if (i->da->vendor != 0) goto move;
1012 * It's Vendor-Specific: move it over.
1014 if (i->da->attr == attr) goto move;
1017 * It's not a VSA: ignore it.
1024 * If it isn't an exact match, ignore it.
1026 if (!((i->da->vendor == vendor) && (i->da->attr == attr))) {
1033 * Remove the attribute from the "from" list.
1043 this = fr_pair_copy(ctx, i);
1047 * Add the attribute to the "to" list.
1050 to_tail->next = this;
1057 fr_pair_steal(ctx, i);
1065 /** Move matching pairs between VALUE_PAIR lists
1067 * Move pairs of a matching attribute number, vendor number and tag from the
1068 * the input list to the output list.
1070 * @note pairs which are moved have their parent changed to ctx.
1072 * @note fr_pair_list_free should be called on the head of the old list to free unmoved
1073 attributes (if they're no longer needed).
1075 * @param[in] ctx for talloc
1076 * @param[in,out] to destination list.
1077 * @param[in,out] from source list.
1078 * @param[in] attr to match. If attribute PW_VENDOR_SPECIFIC and vendor 0,
1079 * will match (and therefore copy) only VSAs.
1080 * If attribute 0 and vendor 0 will match (and therefore copy) all
1082 * @param[in] vendor to match.
1083 * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
1085 void fr_pair_list_move_by_num(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
1086 unsigned int attr, unsigned int vendor, int8_t tag)
1088 fr_pair_list_move_by_num_internal(ctx, to, from, attr, vendor, tag, true);
1092 /** Copy / delete matching pairs between VALUE_PAIR lists
1094 * Move pairs of a matching attribute number, vendor number and tag from the
1095 * the input list to the output list. Like fr_pair_list_move_by_num(), but
1096 * instead does copy / delete.
1098 * @note The pair is NOT reparented. It is copied and deleted.
1100 * @note fr_pair_list_free should be called on the head of the old list to free unmoved
1101 attributes (if they're no longer needed).
1103 * @param[in] ctx for talloc
1104 * @param[in,out] to destination list.
1105 * @param[in,out] from source list.
1106 * @param[in] attr to match. If attribute PW_VENDOR_SPECIFIC and vendor 0,
1107 * will match (and therefore copy) only VSAs.
1108 * If attribute 0 and vendor 0 will match (and therefore copy) all
1110 * @param[in] vendor to match.
1111 * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
1113 void fr_pair_list_mcopy_by_num(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
1114 unsigned int attr, unsigned int vendor, int8_t tag)
1116 fr_pair_list_move_by_num_internal(ctx, to, from, attr, vendor, tag, false);
1120 /** Convert string value to native attribute value
1122 * @param vp to assign value to.
1123 * @param value string to convert. Binary safe for variable length values if len is provided.
1124 * @param inlen may be < 0 in which case strlen(len) is used to determine length, else inline
1125 * should be the length of the string or sub string to parse.
1126 * @return 0 on success -1 on error.
1128 int fr_pair_value_from_str(VALUE_PAIR *vp, char const *value, size_t inlen)
1134 if (!value) return -1;
1136 type = vp->da->type;
1139 * We presume that the input data is from a double quoted
1140 * string, and needs escaping
1142 ret = value_data_from_str(vp, &vp->data, &type, vp->da, value, inlen, '"');
1143 if (ret < 0) return -1;
1146 * If we parsed to a different type than the DA associated with
1147 * the VALUE_PAIR we now need to fixup the DA.
1149 if (type != vp->da->type) {
1150 DICT_ATTR const *da;
1152 da = dict_attrbytype(vp->da->attr, vp->da->vendor, type);
1154 fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1155 fr_int2str(dict_attr_types, type, "<INVALID>"), vp->da->name);
1161 vp->vp_length = ret;
1169 /** Use simple heuristics to create an VALUE_PAIR from an unknown address string
1171 * If a DICT_ATTR is not provided for the address type, parsing will fail with
1174 * @param ctx to allocate VP in.
1175 * @param value IPv4/IPv6 address/prefix string.
1176 * @param ipv4 dictionary attribute to use for an IPv4 address.
1177 * @param ipv6 dictionary attribute to use for an IPv6 address.
1178 * @param ipv4_prefix dictionary attribute to use for an IPv4 prefix.
1179 * @param ipv6_prefix dictionary attribute to use for an IPv6 prefix.
1180 * @return NULL on error, or new VALUE_PAIR.
1182 VALUE_PAIR *fr_pair_afrom_ip_str(TALLOC_CTX *ctx, char const *value, DICT_ATTR *ipv4, DICT_ATTR *ipv6,
1183 DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix)
1186 DICT_ATTR *da = NULL;
1188 if (!fr_assert(ipv4 || ipv6 || ipv4_prefix || ipv6_prefix)) {
1192 /* No point in repeating the work of fr_pair_value_from_str */
1193 if (strchr(value, ':')) {
1194 if (strchr(value, '/')) {
1203 if (strchr(value, '/')) {
1213 fr_strerror_printf("Invalid IP value specified, allowed types are %s%s%s%s",
1214 ipv4 ? "ipaddr " : "", ipv6 ? "ipv6addr " : "",
1215 ipv4_prefix ? "ipv4prefix " : "", ipv6_prefix ? "ipv6prefix" : "");
1218 vp = fr_pair_afrom_da(ctx, da);
1219 if (!vp) return NULL;
1220 if (fr_pair_value_from_str(vp, value, -1) < 0) {
1229 static VALUE_PAIR *fr_pair_from_unkown(VALUE_PAIR *vp, DICT_ATTR const *da)
1234 len = data2vp(NULL, NULL, NULL, NULL, da,
1235 vp->vp_octets, vp->vp_length, vp->vp_length,
1237 if (len < 0) return vp; /* it's really unknown */
1239 if (vp2->da->flags.is_unknown) {
1240 fr_pair_list_free(&vp2);
1245 * Didn't parse all of it. Return the "unknown" one.
1247 * FIXME: it COULD have parsed 2 attributes and
1248 * then not the third, so returning 2 "knowns"
1249 * and 1 "unknown" is likely preferable.
1251 if ((size_t) len < vp->vp_length) {
1252 fr_pair_list_free(&vp2);
1256 fr_pair_steal(talloc_parent(vp), vp2);
1257 fr_pair_list_free(&vp);
1261 /** Create a valuepair from an ASCII attribute and value
1263 * Where the attribute name is in the form:
1265 * - Attr-%d.%d.%d...
1266 * - Vendor-%d-Attr-%d
1267 * - VendorName-Attr-%d
1269 * @param ctx for talloc
1270 * @param attribute name to parse.
1271 * @param value to parse (must be a hex string).
1272 * @param op to assign to new valuepair.
1273 * @return new valuepair or NULL on error.
1275 static VALUE_PAIR *fr_pair_make_unknown(TALLOC_CTX *ctx,
1276 char const *attribute, char const *value,
1280 DICT_ATTR const *da;
1285 da = dict_unknown_afrom_str(ctx, attribute);
1286 if (!da) return NULL;
1289 * Unknown attributes MUST be of type 'octets'
1291 if (value && (strncasecmp(value, "0x", 2) != 0)) {
1292 fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
1293 "string, not \"%s\"", attribute, value);
1295 dict_attr_free(&da);
1300 * We've now parsed the attribute properly, Let's create
1301 * it. This next stop also looks the attribute up in the
1302 * dictionary, and creates the appropriate type for it.
1304 vp = fr_pair_afrom_da(ctx, da);
1306 dict_attr_free(&da);
1310 vp->op = (op == 0) ? T_OP_EQ : op;
1312 if (!value) return vp;
1314 size = strlen(value + 2);
1315 vp->vp_length = size >> 1;
1316 data = talloc_array(vp, uint8_t, vp->vp_length);
1318 if (fr_hex2bin(data, vp->vp_length, value + 2, size) != vp->vp_length) {
1319 fr_strerror_printf("Invalid hex string");
1324 vp->vp_octets = data;
1328 * Convert unknowns to knowns
1330 da = dict_attrbyvalue(vp->da->attr, vp->da->vendor);
1332 return fr_pair_from_unkown(vp, da);
1339 /** Create a VALUE_PAIR from ASCII strings
1341 * Converts an attribute string identifier (with an optional tag qualifier)
1342 * and value string into a VALUE_PAIR.
1344 * The string value is parsed according to the type of VALUE_PAIR being created.
1346 * @param[in] ctx for talloc
1347 * @param[in] vps list where the attribute will be added (optional)
1348 * @param[in] attribute name.
1349 * @param[in] value attribute value (may be NULL if value will be set later).
1350 * @param[in] op to assign to new VALUE_PAIR.
1351 * @return a new VALUE_PAIR.
1353 VALUE_PAIR *fr_pair_make(TALLOC_CTX *ctx, VALUE_PAIR **vps,
1354 char const *attribute, char const *value, FR_TOKEN op)
1356 DICT_ATTR const *da;
1362 char const *attrname = attribute;
1365 * Check for tags in 'Attribute:Tag' format.
1370 ts = strrchr(attribute, ':');
1372 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1377 strlcpy(buffer, attribute, sizeof(buffer));
1379 ts = strrchr(attrname, ':');
1380 if (!ts) return NULL;
1382 /* Colon found with something behind it */
1383 if (ts[1] == '*' && ts[2] == 0) {
1384 /* Wildcard tag for check items */
1387 } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1388 /* It's not a wild card tag */
1389 tag = strtol(ts + 1, &tc, 0);
1390 if (tc && !*tc && TAG_VALID_ZERO(tag))
1394 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1401 * It's not found in the dictionary, so we use
1402 * another method to create the attribute.
1404 da = dict_attrbyname(attrname);
1406 vp = fr_pair_make_unknown(ctx, attrname, value, op);
1407 if (vp && vps) fr_pair_add(vps, vp);
1411 /* Check for a tag in the 'Merit' format of:
1412 * :Tag:Value. Print an error if we already found
1413 * a tag in the Attribute.
1416 if (value && (*value == ':' && da->flags.has_tag)) {
1417 /* If we already found a tag, this is invalid */
1419 fr_strerror_printf("Duplicate tag %s for attribute %s",
1421 DEBUG("Duplicate tag %s for attribute %s\n",
1425 /* Colon found and attribute allows a tag */
1426 if (value[1] == '*' && value[2] == ':') {
1427 /* Wildcard tag for check items */
1432 tag = strtol(value + 1, &tc, 0);
1433 if (tc && *tc==':' && TAG_VALID_ZERO(tag))
1439 vp = fr_pair_afrom_da(ctx, da);
1440 if (!vp) return NULL;
1441 vp->op = (op == 0) ? T_OP_EQ : op;
1446 case T_OP_CMP_FALSE:
1447 vp->vp_strvalue = NULL;
1449 value = NULL; /* ignore it! */
1453 * Regular expression comparison of integer attributes
1454 * does a STRING comparison of the names of their
1455 * integer attributes.
1457 case T_OP_REG_EQ: /* =~ */
1458 case T_OP_REG_NE: /* !~ */
1461 fr_strerror_printf("Regular expressions are not supported");
1468 * Someone else will fill in the value.
1474 slen = regex_compile(ctx, &preg, value, strlen(value), false, false, false, true);
1476 fr_strerror_printf("Error at offset %zu compiling regex for %s: %s", -slen,
1477 attribute, fr_strerror());
1482 vp = fr_pair_make(ctx, NULL, attribute, NULL, op);
1483 if (!vp) return NULL;
1485 if (fr_pair_mark_xlat(vp, value) < 0) {
1490 value = NULL; /* ignore it */
1499 * We allow this for stupidity, but it's really a bad idea.
1501 if (vp->da->type == PW_TYPE_TLV) {
1503 DICT_ATTR const *unknown;
1504 VALUE_PAIR *head = NULL;
1505 VALUE_PAIR **tail = &head;
1512 unknown = dict_unknown_afrom_fields(vp, vp->da->attr, vp->da->vendor);
1521 * Parse it as an unknown type, i.e. octets.
1523 if (fr_pair_value_from_str(vp, value, -1) < 0) {
1529 * It's badly formatted. Treat it as unknown.
1531 if (rad_tlv_ok(vp->vp_octets, vp->vp_length, 1, 1) < 0) {
1538 len = rad_data2vp_tlvs(ctx, NULL, NULL, NULL, da, vp->vp_octets,
1539 vp->vp_length, tail);
1550 * FIXME: if (strcasecmp(attribute, vp->da->name) != 0)
1551 * then the user MAY have typed in the attribute name
1552 * as Vendor-%d-Attr-%d, and the value MAY be octets.
1554 * We probably want to fix fr_pair_value_from_str to accept
1555 * octets as values for any attribute.
1557 if (value && (fr_pair_value_from_str(vp, value, -1) < 0)) {
1563 if (vps) fr_pair_add(vps, vp);
1567 /** Mark a valuepair for xlat expansion
1569 * Copies xlat source (unprocessed) string to valuepair value,
1570 * and sets value type.
1572 * @param vp to mark for expansion.
1573 * @param value to expand.
1574 * @return 0 if marking succeeded or -1 if vp already had a value, or OOM.
1576 int fr_pair_mark_xlat(VALUE_PAIR *vp, char const *value)
1581 * valuepair should not already have a value.
1583 if (vp->type != VT_NONE) {
1587 raw = talloc_typed_strdup(vp, value);
1593 vp->value.xlat = raw;
1600 /** Read a single valuepair from a buffer, and advance the pointer
1602 * Returns T_EOL if end of line was encountered.
1604 * @param[in,out] ptr to read from and update.
1605 * @param[out] raw The struct to write the raw VALUE_PAIR to.
1606 * @return the last token read.
1608 FR_TOKEN fr_pair_raw_from_str(char const **ptr, VALUE_PAIR_RAW *raw)
1612 FR_TOKEN ret = T_INVALID, next, quote;
1615 if (!ptr || !*ptr || !raw) {
1616 fr_strerror_printf("Invalid arguments");
1621 * Skip leading spaces
1624 while ((*p == ' ') || (*p == '\t')) p++;
1627 fr_strerror_printf("No token read where we expected "
1628 "an attribute name");
1632 if (*p == '#') return T_HASH;
1635 * Try to get the attribute name.
1640 uint8_t const *t = (uint8_t const *) p;
1642 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1644 fr_strerror_printf("Attribute name too long");
1649 * This is arguably easier than trying to figure
1650 * out which operators come after the attribute
1651 * name. Yes, our "lexer" is bad.
1653 if (!dict_attr_allowed_chars[(unsigned int) *t]) {
1658 * Attribute:=value is NOT
1664 if ((*p == ':') && (!isdigit((int) p[1]))) {
1672 * Haven't found any valid characters in the name.
1674 if (!*raw->l_opand) {
1675 fr_strerror_printf("Invalid attribute name");
1680 * Look for tag (:#). This is different from :=, which
1683 if ((*p == ':') && (isdigit((int) p[1]))) {
1684 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1689 while (isdigit((int) *p)) {
1690 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1700 /* Now we should have an operator here. */
1701 raw->op = gettoken(ptr, buf, sizeof(buf), false);
1702 if (raw->op < T_EQSTART || raw->op > T_EQEND) {
1703 fr_strerror_printf("Expecting operator");
1709 * Read value. Note that empty string values are allowed
1711 quote = gettoken(ptr, raw->r_opand, sizeof(raw->r_opand), false);
1712 if (quote == T_EOL) {
1713 fr_strerror_printf("Failed to get value");
1719 * Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
1723 next = gettoken(&p, buf, sizeof(buf), false);
1737 fr_strerror_printf("Expected end of line or comma");
1746 case T_DOUBLE_QUOTED_STRING:
1748 * Only report as double quoted if it contained valid
1749 * a valid xlat expansion.
1751 p = strchr(raw->r_opand, '%');
1752 if (p && (p[1] == '{')) {
1755 raw->quote = T_SINGLE_QUOTED_STRING;
1760 case T_SINGLE_QUOTED_STRING:
1761 case T_BACK_QUOTED_STRING:
1767 fr_strerror_printf("Failed to find expected value on right hand side");
1774 /** Read one line of attribute/value pairs into a list.
1776 * The line may specify multiple attributes separated by commas.
1778 * @note If the function returns T_INVALID, an error has occurred and
1779 * @note the valuepair list should probably be freed.
1781 * @param ctx for talloc
1782 * @param buffer to read valuepairs from.
1783 * @param list where the parsed VALUE_PAIRs will be appended.
1784 * @return the last token parsed, or T_INVALID
1786 FR_TOKEN fr_pair_list_afrom_str(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **list)
1788 VALUE_PAIR *vp, *head, **tail;
1790 FR_TOKEN last_token = T_INVALID;
1794 * We allow an empty line.
1796 if (buffer[0] == 0) {
1805 raw.l_opand[0] = '\0';
1806 raw.r_opand[0] = '\0';
1808 last_token = fr_pair_raw_from_str(&p, &raw);
1811 * JUST a hash. Don't try to create a VP.
1812 * Let the caller determine if an empty list is OK.
1814 if (last_token == T_HASH) {
1818 if (last_token == T_INVALID) break;
1820 if (raw.quote == T_DOUBLE_QUOTED_STRING) {
1821 vp = fr_pair_make(ctx, NULL, raw.l_opand, NULL, raw.op);
1823 last_token = T_INVALID;
1826 if (fr_pair_mark_xlat(vp, raw.r_opand) < 0) {
1828 last_token = T_INVALID;
1832 vp = fr_pair_make(ctx, NULL, raw.l_opand, raw.r_opand, raw.op);
1834 last_token = T_INVALID;
1840 tail = &((*tail)->next);
1841 } while (*p && (last_token == T_COMMA));
1843 if (last_token == T_INVALID) {
1844 fr_pair_list_free(&head);
1846 fr_pair_add(list, head);
1850 * And return the last token which we read.
1856 * Read valuepairs from the fp up to End-Of-File.
1858 int fr_pair_list_afrom_file(TALLOC_CTX *ctx, VALUE_PAIR **out, FILE *fp, bool *pfiledone)
1861 FR_TOKEN last_token = T_EOL;
1865 VALUE_PAIR *vp = NULL;
1866 fr_cursor_init(&cursor, out);
1868 while (fgets(buf, sizeof(buf), fp) != NULL) {
1870 * If we get a '\n' by itself, we assume that's
1871 * the end of that VP
1873 if (buf[0] == '\n') {
1882 * Comments get ignored
1884 if (buf[0] == '#') continue;
1887 * Read all of the attributes on the current line.
1890 last_token = fr_pair_list_afrom_str(ctx, buf, &vp);
1892 if (last_token != T_EOL) goto error;
1896 fr_cursor_merge(&cursor, vp);
1905 vp = fr_cursor_first(&cursor);
1906 if (vp) fr_pair_list_free(&vp);
1911 /** Compare two pairs, using the operator from "a"
1913 * i.e. given two attributes, it does:
1915 * (b->data) (a->operator) (a->data)
1917 * e.g. "foo" != "bar"
1919 * @param[in] a the first attribute
1920 * @param[in] b the second attribute
1921 * @return 1 if true, 0 if false, -1 on error.
1923 int fr_pair_cmp(VALUE_PAIR *a, VALUE_PAIR *b)
1928 if (b) VERIFY_VP(b);
1934 case T_OP_CMP_FALSE:
1938 * a is a regex, compile it, print b to a string,
1939 * and then do string comparisons.
1946 if (!b) return false;
1953 if (!fr_assert(a->da->type == PW_TYPE_STRING)) return -1;
1955 slen = regex_compile(NULL, &preg, a->vp_strvalue, a->vp_length, false, false, false, true);
1957 fr_strerror_printf("Error at offset %zu compiling regex for %s: %s",
1958 -slen, a->da->name, fr_strerror());
1961 value = vp_aprints_value(NULL, b, '\0');
1968 * Don't care about substring matches, oh well...
1970 slen = regex_exec(preg, value, talloc_array_length(value) - 1, NULL, NULL);
1974 if (slen < 0) return -1;
1975 if (a->op == T_OP_REG_EQ) return (int)slen;
1980 default: /* we're OK */
1981 if (!b) return false;
1985 return fr_pair_cmp_op(a->op, b, a);
1988 /** Determine equality of two lists
1990 * This is useful for comparing lists of attributes inserted into a binary tree.
1992 * @param a first list of VALUE_PAIRs.
1993 * @param b second list of VALUE_PAIRs.
1994 * @return -1 if a < b, 0 if the two lists are equal, 1 if a > b, -2 on error.
1996 int fr_pair_list_cmp(VALUE_PAIR *a, VALUE_PAIR *b)
1998 vp_cursor_t a_cursor, b_cursor;
1999 VALUE_PAIR *a_p, *b_p;
2002 for (a_p = fr_cursor_init(&a_cursor, &a), b_p = fr_cursor_init(&b_cursor, &b);
2004 a_p = fr_cursor_next(&a_cursor), b_p = fr_cursor_next(&b_cursor)) {
2005 /* Same VP, no point doing expensive checks */
2010 if (a_p->da < b_p->da) {
2013 if (a_p->da > b_p->da) {
2017 if (a_p->tag < b_p->tag) {
2020 if (a_p->tag > b_p->tag) {
2024 ret = value_data_cmp(a_p->da->type, &a_p->data, a_p->vp_length,
2025 b_p->da->type, &b_p->data, b_p->vp_length);
2027 fr_assert(ret >= -1); /* Comparison error */
2044 /** Set the type of the VALUE_PAIR value buffer to match it's DICT_ATTR
2046 * @param vp to fixup.
2048 static void fr_pair_value_set_type(VALUE_PAIR *vp)
2050 if (!vp->data.ptr) return;
2052 switch (vp->da->type) {
2053 case PW_TYPE_OCTETS:
2054 talloc_set_type(vp->data.ptr, uint8_t);
2057 case PW_TYPE_STRING:
2058 talloc_set_type(vp->data.ptr, char);
2066 /** Copy data into an "octets" data type.
2068 * @param[in,out] vp to update
2069 * @param[in] src data to copy
2070 * @param[in] size of the data, may be 0 in which case previous value will be freed.
2072 void fr_pair_value_memcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
2074 uint8_t *p = NULL, *q;
2079 p = talloc_memdup(vp, src, size);
2081 talloc_set_type(p, uint8_t);
2084 memcpy(&q, &vp->vp_octets, sizeof(q));
2088 vp->vp_length = size;
2090 if (size > 0) fr_pair_value_set_type(vp);
2093 /** Reparent an allocated octet buffer to a VALUE_PAIR
2095 * @param[in,out] vp to update
2096 * @param[in] src buffer to steal.
2098 void fr_pair_value_memsteal(VALUE_PAIR *vp, uint8_t const *src)
2104 memcpy(&q, &vp->vp_octets, sizeof(q));
2107 vp->vp_octets = talloc_steal(vp, src);
2109 vp->vp_length = talloc_array_length(vp->vp_strvalue);
2110 fr_pair_value_set_type(vp);
2113 /** Reparent an allocated char buffer to a VALUE_PAIR
2115 * @param[in,out] vp to update
2116 * @param[in] src buffer to steal.
2118 void fr_pair_value_strsteal(VALUE_PAIR *vp, char const *src)
2124 memcpy(&q, &vp->vp_octets, sizeof(q));
2127 vp->vp_strvalue = talloc_steal(vp, src);
2129 vp->vp_length = talloc_array_length(vp->vp_strvalue) - 1;
2130 fr_pair_value_set_type(vp);
2133 /** Copy data into an "string" data type.
2135 * @param[in,out] vp to update
2136 * @param[in] src data to copy
2138 void fr_pair_value_strcpy(VALUE_PAIR *vp, char const *src)
2144 p = talloc_strdup(vp, src);
2148 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2151 vp->vp_strvalue = p;
2153 vp->vp_length = talloc_array_length(vp->vp_strvalue) - 1;
2154 fr_pair_value_set_type(vp);
2157 /** Copy data into an "string" data type.
2159 * @note unlike the original strncpy, this function does not stop
2160 * if it finds \0 bytes embedded in the string.
2162 * @param[in,out] vp to update.
2163 * @param[in] src data to copy.
2164 * @param[in] len of data to copy.
2166 void fr_pair_value_bstrncpy(VALUE_PAIR *vp, void const *src, size_t len)
2172 p = talloc_array(vp, char, len + 1);
2175 memcpy(p, src, len); /* embdedded \0 safe */
2178 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2181 vp->vp_strvalue = p;
2183 vp->vp_length = len;
2184 fr_pair_value_set_type(vp);
2187 /** Print data into an "string" data type.
2189 * @param[in,out] vp to update
2190 * @param[in] fmt the format string
2192 void fr_pair_value_sprintf(VALUE_PAIR *vp, char const *fmt, ...)
2200 p = talloc_vasprintf(vp, fmt, ap);
2205 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2208 vp->vp_strvalue = p;
2211 vp->vp_length = talloc_array_length(vp->vp_strvalue) - 1;
2212 fr_pair_value_set_type(vp);
2215 #ifdef WITH_VERIFY_PTR
2217 * Verify a VALUE_PAIR
2219 inline void fr_pair_verify(char const *file, int line, VALUE_PAIR const *vp)
2222 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR pointer was NULL", file, line);
2227 (void) talloc_get_type_abort(vp, VALUE_PAIR);
2230 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR da pointer was NULL", file, line);
2235 if (vp->data.ptr) switch (vp->da->type) {
2236 case PW_TYPE_OCTETS:
2241 if (!talloc_get_type(vp->data.ptr, uint8_t)) {
2242 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
2243 "uint8_t but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
2244 (void) talloc_get_type_abort(vp->data.ptr, uint8_t);
2247 len = talloc_array_length(vp->vp_octets);
2248 if (vp->vp_length > len) {
2249 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
2250 "uint8_t data buffer length %zu\n", file, line, vp->da->name, vp->vp_length, len);
2255 parent = talloc_parent(vp->data.ptr);
2257 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not "
2258 "parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
2259 file, line, vp->da->name,
2260 vp, parent, parent ? talloc_get_name(parent) : "NULL");
2267 case PW_TYPE_STRING:
2272 if (!talloc_get_type(vp->data.ptr, char)) {
2273 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
2274 "char but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
2275 (void) talloc_get_type_abort(vp->data.ptr, char);
2278 len = (talloc_array_length(vp->vp_strvalue) - 1);
2279 if (vp->vp_length > len) {
2280 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
2281 "char buffer length %zu\n", file, line, vp->da->name, vp->vp_length, len);
2286 if (vp->vp_strvalue[vp->vp_length] != '\0') {
2287 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer not \\0 "
2288 "terminated\n", file, line, vp->da->name);
2293 parent = talloc_parent(vp->data.ptr);
2295 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not "
2296 "parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
2297 file, line, vp->da->name,
2298 vp, parent, parent ? talloc_get_name(parent) : "NULL");
2309 if (vp->da->flags.is_unknown) {
2310 (void) talloc_get_type_abort(vp->da, DICT_ATTR);
2312 DICT_ATTR const *da;
2315 * Attribute may be present with multiple names
2317 da = dict_attrbyname(vp->da->name);
2319 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR attribute %p \"%s\" (%s) "
2320 "not found in global dictionary",
2321 file, line, vp->da, vp->da->name,
2322 fr_int2str(dict_attr_types, vp->da->type, "<INVALID>"));
2327 if (da->type == PW_TYPE_COMBO_IP_ADDR) {
2328 da = dict_attrbytype(vp->da->attr, vp->da->vendor, vp->da->type);
2330 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR attribute %p \"%s\" "
2331 "variant (%s) not found in global dictionary",
2332 file, line, vp->da, vp->da->name,
2333 fr_int2str(dict_attr_types, vp->da->type, "<INVALID>"));
2341 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR "
2342 "dictionary pointer %p \"%s\" (%s) "
2343 "and global dictionary pointer %p \"%s\" (%s) differ",
2344 file, line, vp->da, vp->da->name,
2345 fr_int2str(dict_attr_types, vp->da->type, "<INVALID>"),
2346 da, da->name, fr_int2str(dict_attr_types, da->type, "<INVALID>"));
2354 * Verify a pair list
2356 void fr_pair_list_verify(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR *vps)
2362 for (vp = fr_cursor_init(&cursor, &vps);
2364 vp = fr_cursor_next(&cursor)) {
2367 parent = talloc_parent(vp);
2368 if (expected && (parent != expected)) {
2369 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: Expected VALUE_PAIR \"%s\" to be parented "
2370 "by %p (%s), instead parented by %p (%s)\n",
2371 file, line, vp->da->name,
2372 expected, talloc_get_name(expected),
2373 parent, parent ? talloc_get_name(parent) : "NULL");
2375 fr_log_talloc_report(expected);
2376 if (parent) fr_log_talloc_report(parent);