2 * valuepair.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>
29 #ifdef HAVE_PCREPOSIX_H
31 # include <pcreposix.h>
32 #elif defined(HAVE_REGEX_H)
37 * For POSIX Regular expressions.
38 * (0) Means no extended regular expressions.
39 * REG_EXTENDED means use extended regular expressions.
42 # define REG_EXTENDED (0)
46 # define REG_NOSUB (0)
50 #define attribute_eq(_x, _y) ((_x && _y) && (_x->da == _y->da) && (_x->tag == _y->tag))
54 * @note Do not call directly, use talloc_free instead.
59 static int _pairfree(VALUE_PAIR *vp) {
61 * The lack of DA means something has gone wrong
64 fr_strerror_printf("VALUE_PAIR has NULL DICT_ATTR pointer (probably already freed)");
66 * Only free the DICT_ATTR if it was dynamically allocated
67 * and was marked for free when the VALUE_PAIR is freed.
69 * @fixme This is an awful hack and needs to be removed once DICT_ATTRs are allocated by talloc.
71 } else if (vp->da->flags.vp_free) {
72 dict_attr_free(&(vp->da));
76 vp->vp_integer = 0xf4eef4ee;
80 talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL);
85 /** Dynamically allocate a new attribute
87 * Allocates a new attribute and a new dictionary attr if no DA is provided.
89 * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
90 * @param[in] da Specifies the dictionary attribute to build the VP from.
91 * @return a new value pair or NULL if an error occurred.
93 VALUE_PAIR *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da)
98 * Caller must specify a da else we don't know what the attribute type is.
101 fr_strerror_printf("Invalid arguments");
105 vp = talloc_zero(ctx, VALUE_PAIR);
107 fr_strerror_printf("Out of memory");
116 vp->length = da->flags.length;
118 talloc_set_destructor(vp, _pairfree);
123 /** Create a new valuepair
125 * If attr and vendor match a dictionary entry then a VP with that DICT_ATTR
128 * If attr or vendor are uknown will call dict_attruknown to create a dynamic
129 * DICT_ATTR of PW_TYPE_OCTETS.
131 * Which type of DICT_ATTR the VALUE_PAIR was created with can be determined by
132 * checking @verbatim vp->da->flags.is_unknown @endverbatim.
134 * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
135 * @param[in] attr number.
136 * @param[in] vendor number.
137 * @return the new valuepair or NULL on error.
139 VALUE_PAIR *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
143 da = dict_attrbyvalue(attr, vendor);
145 da = dict_attrunknown(attr, vendor, true);
151 return pairalloc(ctx, da);
154 /** Free memory used by a valuepair list.
156 * @todo TLV: needs to free all dependents of each VP freed.
158 void pairfree(VALUE_PAIR **vps)
167 for (vp = fr_cursor_init(&cursor, vps);
169 vp = fr_cursor_next(&cursor)) {
177 /** Mark malformed or unrecognised attributed as unknown
179 * @param vp to change DICT_ATTR of.
180 * @return 0 on success (or if already unknown) else -1 on error.
182 int pair2unknown(VALUE_PAIR *vp)
187 if (vp->da->flags.is_unknown) {
191 da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
201 /** Find the pair with the matching DAs
204 VALUE_PAIR *pairfind_da(VALUE_PAIR *vp, DICT_ATTR const *da, int8_t tag)
213 for (i = fr_cursor_init(&cursor, &vp);
215 i = fr_cursor_next(&cursor)) {
217 if ((i->da == da) && (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
226 /** Find the pair with the matching attribute
228 * @todo should take DAs and do a pointer comparison.
230 VALUE_PAIR *pairfind(VALUE_PAIR *vp, unsigned int attr, unsigned int vendor, int8_t tag)
237 for (i = fr_cursor_init(&cursor, &vp);
239 i = fr_cursor_next(&cursor)) {
240 if ((i->da->attr == attr) && (i->da->vendor == vendor) && \
241 (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
249 /** Delete matching pairs
251 * Delete matching pairs from the attribute list.
253 * @param[in,out] first VP in list.
254 * @param[in] attr to match.
255 * @param[in] vendor to match.
256 * @param[in] tag to match. TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
258 * @todo should take DAs and do a point comparison.
260 void pairdelete(VALUE_PAIR **first, unsigned int attr, unsigned int vendor,
263 VALUE_PAIR *i, *next;
264 VALUE_PAIR **last = first;
266 for(i = *first; i; i = next) {
269 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
270 (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
279 /** Add a VP to the end of the list.
281 * Locates the end of 'first', and links an additional VP 'add' at the end.
283 * @param[in] first VP in linked list. Will add new VP to the end of this list.
284 * @param[in] add VP to add to list.
286 void pairadd(VALUE_PAIR **first, VALUE_PAIR *add)
294 if (*first == NULL) {
298 for(i = *first; i->next; i = i->next)
303 /** Replace all matching VPs
305 * Walks over 'first', and replaces the first VP that matches 'replace'.
307 * @note Memory used by the VP being replaced will be freed.
308 * @note Will not work with unknown attributes.
310 * @param[in,out] first VP in linked list. Will search and replace in this list.
311 * @param[in] replace VP to replace.
313 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *replace)
315 VALUE_PAIR *i, *next;
316 VALUE_PAIR **prev = first;
320 if (*first == NULL) {
326 * Not an empty list, so find item if it is there, and
327 * replace it. Note, we always replace the first one, and
328 * we ignore any others that might exist.
330 for(i = *first; i; i = next) {
335 * Found the first attribute, replace it,
338 if ((i->da == replace->da) && (!i->da->flags.has_tag || TAG_EQ(replace->tag, i->tag))) {
342 * Should really assert that replace->next == NULL
344 replace->next = next;
350 * Point to where the attribute should go.
356 * If we got here, we didn't find anything to replace, so
357 * stopped at the last item, which we just append to.
362 int8_t attrtagcmp(void const *a, void const *b)
364 VALUE_PAIR const *my_a = a;
365 VALUE_PAIR const *my_b = b;
372 cmp = fr_pointer_cmp(my_a->da, my_b->da);
374 if (cmp != 0) return cmp;
376 if (my_a->tag < my_b->tag) {
380 if (my_a->tag > my_b->tag) {
387 static void pairsort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
393 * Stopping condition - no more elements left to split
395 if (!source || !source->next) {
403 * Fast advances twice as fast as slow, so when it gets to the end,
404 * slow will point to the middle of the linked list.
422 static VALUE_PAIR *pairsort_merge(VALUE_PAIR *a, VALUE_PAIR *b, fr_cmp_t cmp)
424 VALUE_PAIR *result = NULL;
430 * Compare the DICT_ATTRs and tags
432 if (cmp(a, b) <= 0) {
434 result->next = pairsort_merge(a->next, b, cmp);
437 result->next = pairsort_merge(a, b->next, cmp);
443 /** Sort a linked list of VALUE_PAIRs using merge sort
445 * @param[in,out] vps List of VALUE_PAIRs to sort.
446 * @param[in] cmp to sort with
448 void pairsort(VALUE_PAIR **vps, fr_cmp_t cmp)
450 VALUE_PAIR *head = *vps;
455 * If there's 0-1 elements it must already be sorted.
457 if (!head || !head->next) {
461 pairsort_split(head, &a, &b); /* Split into sublists */
462 pairsort(&a, cmp); /* Traverse left */
463 pairsort(&b, cmp); /* Traverse right */
466 * merge the two sorted lists together
468 *vps = pairsort_merge(a, b, cmp);
471 /** Write an error to the library errorbuff detailing the mismatch
473 * Retrieve output with fr_strerror();
475 * @todo add thread specific talloc contexts.
477 * @param ctx a hack until we have thread specific talloc contexts.
478 * @param failed pair of attributes which didn't match.
480 void pairvalidate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2])
482 VALUE_PAIR const *filter = failed[0];
483 VALUE_PAIR const *list = failed[1];
487 (void) fr_strerror(); /* Clear any existing messages */
489 if (!fr_assert(!(!filter && !list))) return;
493 fr_strerror_printf("Attribute \"%s\" not found in list", filter->da->name);
497 if (!filter || (filter->da != list->da)) {
498 fr_strerror_printf("Attribute \"%s\" not found in filter", list->da->name);
502 if (TAG_EQ(filter->tag, list->tag)) {
503 fr_strerror_printf("Attribute \"%s\" tag \"%i\" didn't match filter tag \"%i\"",
504 list->da->name, list->tag, filter->tag);
508 pair = vp_aprint(ctx, filter);
509 value = vp_aprint_value(ctx, list);
511 fr_strerror_printf("Attribute value \"%s\" didn't match filter \"%s\"", value, pair);
519 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
521 * @note will sort both filter and list in place.
523 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
525 * @param filter attributes to check list against.
526 * @param list attributes, probably a request or reply
528 bool pairvalidate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
530 vp_cursor_t filter_cursor;
531 vp_cursor_t list_cursor;
533 VALUE_PAIR *check, *match;
535 if (!filter && !list) {
540 * This allows us to verify the sets of validate and reply are equal
541 * i.e. we have a validate rule which matches every reply attribute.
543 * @todo this should be removed one we have sets and lists
545 pairsort(&filter, attrtagcmp);
546 pairsort(&list, attrtagcmp);
548 check = fr_cursor_init(&filter_cursor, &filter);
549 match = fr_cursor_init(&list_cursor, &list);
552 if (!match && !check) goto mismatch;
555 * The lists are sorted, so if the first
556 * attributes aren't of the same type, then we're
559 if (!attribute_eq(check, match)) goto mismatch;
562 * They're of the same type, but don't have the
563 * same values. This is a problem.
565 * Note that the RFCs say that for attributes of
566 * the same type, order is important.
568 if (!paircmp(check, match)) goto mismatch;
570 check = fr_cursor_next(&filter_cursor);
571 match = fr_cursor_next(&list_cursor);
574 * One list ended earlier than the others, they
577 if (!match || !check) break;
590 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
592 * @note will sort both filter and list in place.
594 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
596 * @param filter attributes to check list against.
597 * @param list attributes, probably a request or reply
599 bool pairvalidate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
601 vp_cursor_t filter_cursor;
602 vp_cursor_t list_cursor;
604 VALUE_PAIR *check, *last_check = NULL, *match = NULL;
606 if (!filter && !list) {
611 * This allows us to verify the sets of validate and reply are equal
612 * i.e. we have a validate rule which matches every reply attribute.
614 * @todo this should be removed one we have sets and lists
616 pairsort(&filter, attrtagcmp);
617 pairsort(&list, attrtagcmp);
619 fr_cursor_init(&list_cursor, &list);
620 for (check = fr_cursor_init(&filter_cursor, &filter);
622 check = fr_cursor_next(&filter_cursor)) {
624 * Were processing check attributes of a new type.
626 if (!attribute_eq(last_check, check)) {
628 * Record the start of the matching attributes in the pair list
629 * For every other operator we require the match to be present
631 match = fr_cursor_next_by_da(&list_cursor, check->da, check->tag);
633 if (check->op == T_OP_CMP_FALSE) continue;
637 fr_cursor_init(&list_cursor, &match);
642 * Now iterate over all attributes of the same type.
644 for (match = fr_cursor_first(&list_cursor);
645 attribute_eq(match, check);
646 match = fr_cursor_next(&list_cursor)) {
648 * This attribute passed the filter
650 if (!paircmp(check, match)) goto mismatch;
664 /** Copy a single valuepair
666 * Allocate a new valuepair and copy the da from the old vp.
668 * @param[in] ctx for talloc
669 * @param[in] vp to copy.
670 * @return a copy of the input VP or NULL on error.
672 VALUE_PAIR *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
676 if (!vp) return NULL;
680 n = pairalloc(ctx, vp->da);
683 memcpy(n, vp, sizeof(*n));
688 if (vp->type == VT_XLAT) {
689 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
692 n->da = dict_attr_copy(vp->da, true);
700 switch (vp->da->type) {
703 n->vp_octets = NULL; /* else pairmemcpy will free vp's value */
704 pairmemcpy(n, vp->vp_octets, n->length);
708 n->vp_strvalue = NULL; /* else pairstrnpy will free vp's value */
709 pairstrncpy(n, vp->vp_strvalue, n->length);
719 /** Copy data from one VP to another
721 * Allocate a new pair using da, and copy over the value from the specified
724 * @todo Should be able to do type conversions.
726 * @param[in] ctx for talloc
727 * @param[in] da of new attribute to alloc.
728 * @param[in] vp to copy data from.
729 * @return the new valuepair.
731 VALUE_PAIR *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp)
735 if (!vp) return NULL;
740 * The types have to be identical, OR the "from" VP has
743 if (da->type != vp->da->type) {
746 VALUE_PAIR const **pvp;
748 if (vp->da->type == PW_TYPE_OCTETS) {
750 * Decode the data. It may be wrong!
752 if (rad_data2vp(da->attr, da->vendor, vp->vp_octets, vp->length, &n) < 0) {
761 * Else the destination type is octets
763 switch (vp->da->type) {
765 return NULL; /* can't do it */
767 case PW_TYPE_INTEGER:
771 case PW_TYPE_IPV6ADDR:
772 case PW_TYPE_IPV6PREFIX:
775 case PW_TYPE_ETHERNET:
777 case PW_TYPE_INTEGER64:
778 case PW_TYPE_IPV4PREFIX:
782 n = pairalloc(ctx, da);
785 p = talloc_array(n, uint8_t, dict_attr_sizes[vp->da->type][1] + 2);
788 length = rad_vp2attr(NULL, NULL, NULL, pvp, p, dict_attr_sizes[vp->da->type][1]);
794 pairmemcpy(n, p + 2, length - 2);
799 n = pairalloc(ctx, da);
802 memcpy(n, vp, sizeof(*n));
805 if (n->type == VT_XLAT) {
806 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
809 if (n->data.ptr) switch (n->da->type) {
812 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
813 talloc_set_type(n->vp_octets, uint8_t);
817 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1); /* NULL byte */
818 talloc_set_type(n->vp_strvalue, char);
833 * Copy all pairs from 'from' regardless of tag, attribute or vendor.
835 * @param[in] ctx for new VALUE_PAIRs to be allocated in.
836 * @param[in] from whence to copy VALUE_PAIRs.
837 * @return the head of the new VALUE_PAIR list or NULL on error.
839 VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from)
841 vp_cursor_t src, dst;
843 VALUE_PAIR *out = NULL, *vp;
845 fr_cursor_init(&dst, &out);
846 for (vp = fr_cursor_init(&src, &from);
848 vp = fr_cursor_next(&src)) {
850 vp = paircopyvp(ctx, vp);
855 fr_cursor_insert(&dst, vp); /* paircopy sets next pointer to NULL */
861 /** Copy matching pairs
863 * Copy pairs of a matching attribute number, vendor number and tag from the
864 * the input list to a new list, and returns the head of this list.
866 * @param[in] ctx for talloc
867 * @param[in] from whence to copy VALUE_PAIRs.
868 * @param[in] attr to match, if 0 input list will not be filtered by attr.
869 * @param[in] vendor to match.
870 * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
871 * @return the head of the new VALUE_PAIR list or NULL on error.
873 VALUE_PAIR *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from,
874 unsigned int attr, unsigned int vendor, int8_t tag)
876 vp_cursor_t src, dst;
878 VALUE_PAIR *out = NULL, *vp;
880 fr_cursor_init(&dst, &out);
881 for (vp = fr_cursor_init(&src, &from);
883 vp = fr_cursor_next(&src)) {
886 if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
890 if (vp->da->flags.has_tag && TAG_EQ(tag, vp->tag)) {
894 vp = paircopyvp(ctx, vp);
899 fr_cursor_insert(&dst, vp);
905 /** Steal all members of a VALUE_PAIR list
907 * @param[in] ctx to move VALUE_PAIRs into
908 * @param[in] from VALUE_PAIRs to move into the new context.
910 VALUE_PAIR *pairsteal(TALLOC_CTX *ctx, VALUE_PAIR *from)
915 for (vp = fr_cursor_init(&cursor, &from);
917 vp = fr_cursor_next(&cursor)) {
918 (void) talloc_steal(ctx, vp);
924 /** Move pairs from source list to destination list respecting operator
926 * @note This function does some additional magic that's probably not needed
927 * in most places. Consider using radius_pairmove in server code.
929 * @note pairfree should be called on the head of the source list to free
930 * unmoved attributes (if they're no longer needed).
932 * @note Does not respect tags when matching.
934 * @param[in] ctx for talloc
935 * @param[in,out] to destination list.
936 * @param[in,out] from source list.
938 * @see radius_pairmove
940 void pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from)
942 VALUE_PAIR *i, *found;
943 VALUE_PAIR *head_new, **tail_new;
944 VALUE_PAIR **tail_from;
946 if (!to || !from || !*from) return;
949 * We're editing the "to" list while we're adding new
950 * attributes to it. We don't want the new attributes to
951 * be edited, so we create an intermediate list to hold
952 * them during the editing process.
955 tail_new = &head_new;
958 * We're looping over the "from" list, moving some
959 * attributes out, but leaving others in place.
962 while ((i = *tail_from) != NULL) {
966 * We never move Fall-Through.
968 if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
969 tail_from = &(i->next);
974 * Unlike previous versions, we treat all other
975 * attributes as normal. i.e. there's no special
976 * treatment for passwords or Hint.
981 * Anything else are operators which
982 * shouldn't occur. We ignore them, and
983 * leave them in place.
986 tail_from = &(i->next);
990 * Add it to the "to" list, but only if
991 * it doesn't already exist.
994 found = pairfind(*to, i->da->attr, i->da->vendor, TAG_ANY);
995 if (!found) goto do_add;
997 tail_from = &(i->next);
1001 * Add it to the "to" list, and delete any attribute
1002 * of the same vendor/attr which already exists.
1005 found = pairfind(*to, i->da->attr, i->da->vendor, TAG_ANY);
1006 if (!found) goto do_add;
1009 * Do NOT call pairdelete() here,
1010 * due to issues with re-writing
1011 * "request->username".
1013 * Everybody calls pairmove, and
1014 * expects it to work. We can't
1015 * update request->username here,
1016 * so instead we over-write the
1017 * vp that it's pointing to.
1019 switch (found->da->type) {
1024 memcpy(found, i, sizeof(*found));
1029 pairmemsteal(found, i->vp_tlv);
1033 case PW_TYPE_OCTETS:
1034 pairmemsteal(found, i->vp_octets);
1035 i->vp_octets = NULL;
1038 case PW_TYPE_STRING:
1039 pairstrsteal(found, i->vp_strvalue);
1040 i->vp_strvalue = NULL;
1041 found->tag = i->tag;
1046 * Delete *all* of the attributes
1047 * of the same number.
1049 pairdelete(&found->next,
1051 found->da->vendor, TAG_ANY);
1054 * Remove this attribute from the
1057 *tail_from = i->next;
1063 * Move it from the old list and add it
1068 *tail_from = i->next;
1070 *tail_new = talloc_steal(ctx, i);
1071 tail_new = &(i->next);
1074 } /* loop over the "from" list. */
1077 * Take the "new" list, and append it to the "to" list.
1079 pairadd(to, head_new);
1082 /** Move matching pairs between VALUE_PAIR lists
1084 * Move pairs of a matching attribute number, vendor number and tag from the
1085 * the input list to the output list.
1087 * @note pairfree should be called on the head of the old list to free unmoved
1088 attributes (if they're no longer needed).
1090 * @param[in] ctx for talloc
1091 * @param[in,out] to destination list.
1092 * @param[in,out] from source list.
1093 * @param[in] attr to match, if PW_VENDOR_SPECIFIC and vendor 0, only VSAs will
1094 * be copied. If 0 and 0, all attributes will match
1095 * @param[in] vendor to match.
1096 * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
1098 void pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from, unsigned int attr, unsigned int vendor, int8_t tag)
1100 VALUE_PAIR *to_tail, *i, *next;
1101 VALUE_PAIR *iprev = NULL;
1104 * Find the last pair in the "to" list and put it in "to_tail".
1106 * @todo: replace the "if" with "VALUE_PAIR **tail"
1110 for(i = *to; i; i = i->next) {
1118 * Attr/vendor of 0 means "move them all".
1119 * It's better than "pairadd(foo,bar);bar=NULL"
1121 if ((vendor == 0) && (attr == 0)) {
1123 to_tail->next = *from;
1128 for (i = *from; i; i = i->next) {
1129 (void) talloc_steal(ctx, i);
1136 for(i = *from; i; i = next) {
1140 if (i->da->flags.has_tag && TAG_EQ(tag, i->tag)) {
1145 * vendor=0, attr = PW_VENDOR_SPECIFIC means
1146 * "match any vendor attribute".
1148 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
1150 * It's a VSA: move it over.
1152 if (i->da->vendor != 0) goto move;
1155 * It's Vendor-Specific: move it over.
1157 if (i->da->attr == attr) goto move;
1160 * It's not a VSA: ignore it.
1167 * If it isn't an exact match, ignore it.
1169 if (!((i->da->vendor == vendor) && (i->da->attr == attr))) {
1176 * Remove the attribute from the "from" list.
1184 * Add the attribute to the "to" list.
1192 (void) talloc_steal(ctx, i);
1196 static char const *hextab = "0123456789abcdef";
1198 /** Convert string value to native attribute value
1200 * @param vp to assign value to.
1201 * @param value string to convert. Binary safe for variable length values if len is provided.
1202 * @param inlen may be 0 in which case strlen(len) is used to determine length, else inline
1203 * should be the length of the string or sub string to parse.
1204 * @return true on success, else false.
1206 int pairparsevalue(VALUE_PAIR *vp, char const *value, size_t inlen)
1212 if (!value) return -1;
1216 * It's a comparison, not a real VALUE_PAIR, copy the string over verbatim
1218 if ((vp->op == T_OP_REG_EQ) || (vp->op == T_OP_REG_NE)) {
1219 pairstrcpy(vp, value); /* Icky hacky ewww */
1223 len = (inlen == 0) ? strlen(value) : inlen;
1226 * It's a variable length type so we just alloc a new buffer
1227 * of size len and copy.
1229 switch(vp->da->type) {
1230 case PW_TYPE_STRING:
1240 vp->vp_strvalue = p = talloc_memdup(vp, value, len + 1);
1242 talloc_set_type(p, char);
1249 if (c == '\\') switch (*cp) {
1279 c = '\\'; /* no cp++ */
1282 if ((cp[0] >= '0') &&
1288 (sscanf(cp, "%3o", &x) == 1)) {
1297 } /* else at EOL \ --> \ */
1303 vp->length = vp_len;
1307 /* raw octets: 0x01020304... */
1309 if (strcmp(value, "ANY") == 0) {
1312 } /* else it's hex */
1314 case PW_TYPE_OCTETS:
1319 * No 0x prefix, just copy verbatim.
1321 if ((len < 2) || (strncasecmp(value, "0x", 2) != 0)) {
1322 pairmemcpy(vp, (uint8_t const *) value, len);
1327 #ifdef WITH_ASCEND_BINARY
1335 if ((len & 0x01) != 0) {
1336 fr_strerror_printf("Length of Hex String is not even, got %zu bytes", vp->length);
1340 vp->length = len >> 1;
1341 p = talloc_array(vp, uint8_t, vp->length);
1342 if (fr_hex2bin(p, value + 2, len) != vp->length) {
1344 fr_strerror_printf("Invalid hex data");
1352 case PW_TYPE_ABINARY:
1353 #ifdef WITH_ASCEND_BINARY
1354 if ((len > 1) && (strncasecmp(value, "0x", 2) == 0)) goto do_octets;
1356 if (ascend_parse_filter(vp, value, len) < 0 ) {
1357 /* Allow ascend_parse_filter's strerror to bubble up */
1363 * If Ascend binary is NOT defined,
1364 * then fall through to raw octets, so that
1365 * the user can at least make them by hand...
1370 /* don't use this! */
1375 if ((len < 2) || (len & 0x01) || (strncasecmp(value, "0x", 2) != 0)) {
1376 fr_strerror_printf("Invalid TLV specification");
1381 vp->length = len >> 1;
1382 p = talloc_array(vp, uint8_t, vp->length);
1384 fr_strerror_printf("No memory");
1387 if (fr_hex2bin(p, value + 2, len) != vp->length) {
1388 fr_strerror_printf("Invalid hex data in TLV");
1396 case PW_TYPE_IPADDR:
1400 if (fr_pton4(&addr, value, inlen, false, false) < 0) return -1;
1403 * We allow v4 addresses to have a /32 suffix as some databases (PostgreSQL)
1404 * print them this way.
1406 if (addr.prefix != 32) {
1407 fr_strerror_printf("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted "
1408 "for non-prefix types", addr.prefix);
1412 vp->vp_ipaddr = addr.ipaddr.ip4addr.s_addr;
1413 vp->length = sizeof(vp->vp_ipaddr);
1417 case PW_TYPE_IPV4PREFIX:
1421 if (fr_pton4(&addr, value, inlen, false, false) < 0) return -1;
1423 vp->vp_ipv4prefix[1] = addr.prefix;
1424 memcpy(vp->vp_ipv4prefix + 2, &addr.ipaddr.ip4addr.s_addr, sizeof(vp->vp_ipv4prefix) - 2);
1425 vp->length = sizeof(vp->vp_ipv4prefix);
1429 case PW_TYPE_IPV6ADDR:
1433 if (fr_pton6(&addr, value, inlen, false, false) < 0) return -1;
1436 * We allow v6 addresses to have a /128 suffix as some databases (PostgreSQL)
1437 * print them this way.
1439 if (addr.prefix != 128) {
1440 fr_strerror_printf("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted "
1441 "for non-prefix types", addr.prefix);
1445 memcpy(&vp->vp_ipv6addr, &addr.ipaddr.ip6addr.s6_addr, sizeof(vp->vp_ipv6addr));
1446 vp->length = sizeof(vp->vp_ipv6addr);
1450 case PW_TYPE_IPV6PREFIX:
1454 if (fr_pton6(&addr, value, inlen, false, false) < 0) return -1;
1456 vp->vp_ipv6prefix[1] = addr.prefix;
1457 memcpy(vp->vp_ipv6prefix + 2, &addr.ipaddr.ip6addr.s6_addr, sizeof(vp->vp_ipv6prefix) - 2);
1458 vp->length = sizeof(vp->vp_ipv6prefix);
1467 * It's a fixed size type, copy to a temporary buffer and
1468 * \0 terminate if insize >= 0.
1471 if (len >= sizeof(buffer)) {
1472 fr_strerror_printf("Temporary buffer too small");
1476 memcpy(buffer, value, inlen);
1477 buffer[inlen] = '\0';
1481 switch(vp->da->type) {
1488 * Note that ALL integers are unsigned!
1490 vp->vp_integer = fr_strtoul(value, &p);
1492 if (vp->vp_integer > 255) {
1493 fr_strerror_printf("Byte value \"%s\" is larger than 255", value);
1498 if (is_whitespace(p)) break;
1500 goto check_for_value;
1507 * Note that ALL integers are unsigned!
1509 vp->vp_integer = fr_strtoul(value, &p);
1512 if (vp->vp_integer > 65535) {
1513 fr_strerror_printf("Byte value \"%s\" is larger than 65535", value);
1518 if (is_whitespace(p)) break;
1520 goto check_for_value;
1522 case PW_TYPE_INTEGER:
1527 * Note that ALL integers are unsigned!
1529 vp->vp_integer = fr_strtoul(value, &p);
1532 if (is_whitespace(p)) break;
1536 * Look for the named value for the given
1539 if ((dval = dict_valbyname(vp->da->attr, vp->da->vendor, value)) == NULL) {
1540 fr_strerror_printf("Unknown value '%s' for attribute '%s'", value, vp->da->name);
1543 vp->vp_integer = dval->value;
1547 case PW_TYPE_INTEGER64:
1552 * Note that ALL integers are unsigned!
1554 if (sscanf(value, "%" PRIu64, &y) != 1) {
1555 fr_strerror_printf("Invalid value '%s' for attribute '%s'",
1556 value, vp->da->name);
1559 vp->vp_integer64 = y;
1567 * time_t may be 64 bits, whule vp_date
1568 * MUST be 32-bits. We need an
1569 * intermediary variable to handle
1574 if (fr_get_time(value, &date) < 0) {
1575 fr_strerror_printf("failed to parse time string "
1587 if (ifid_aton(value, (void *) &vp->vp_ifid) == NULL) {
1588 fr_strerror_printf("Failed to parse interface-id string \"%s\"", value);
1594 case PW_TYPE_ETHERNET:
1596 char const *c1, *c2, *cp;
1600 * Convert things which are obviously integers to Ethernet addresses
1602 * We assume the number is the bigendian representation of the
1605 if (is_integer(value)) {
1606 uint64_t integer = htonll(atoll(value));
1608 memcpy(&vp->vp_ether, &integer, sizeof(vp->vp_ether));
1616 c2 = memchr(hextab, tolower((int) cp[0]), 16);
1618 } else if ((cp[1] != '\0') && ((cp[2] == ':') || (cp[2] == '\0'))) {
1619 c1 = memchr(hextab, tolower((int) cp[0]), 16);
1620 c2 = memchr(hextab, tolower((int) cp[1]), 16);
1622 if (*cp == ':') cp++;
1626 if (!c1 || !c2 || (vp_len >= sizeof(vp->vp_ether))) {
1627 fr_strerror_printf("failed to parse Ethernet address \"%s\"", value);
1630 vp->vp_ether[vp_len] = ((c1-hextab)<<4) + (c2-hextab);
1639 * Crazy polymorphic (IPv4/IPv6) attribute type for WiMAX.
1641 * We try and make is saner by replacing the original
1642 * da, with either an IPv4 or IPv6 da type.
1644 * These are not dynamic da, and will have the same vendor
1645 * and attribute as the original.
1647 case PW_TYPE_COMBO_IP:
1649 DICT_ATTR const *da;
1651 if (inet_pton(AF_INET6, value, &vp->vp_ipv6addr) > 0) {
1652 da = dict_attrbytype(vp->da->attr, vp->da->vendor, PW_TYPE_IPV6ADDR);
1654 fr_strerror_printf("Cannot find ipv6addr for %s", vp->da->name);
1658 vp->length = 16; /* length of IPv6 address */
1662 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1665 fr_strerror_printf("Cannot find ipaddr for %s", vp->da->name);
1669 if (ip_hton(&ipaddr, AF_INET, value, false) < 0) {
1670 fr_strerror_printf("Failed to find IPv4 address for %s", value);
1674 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1682 case PW_TYPE_SIGNED:
1683 /* Damned code for 1 WiMAX attribute */
1684 vp->vp_signed = (int32_t) strtol(value, NULL, 10);
1692 fr_strerror_printf("unknown attribute type %d", vp->da->type);
1701 /** Use simple heuristics to create an VALUE_PAIR from an unknown address string
1703 * If a DICT_ATTR is not provided for the address type, parsing will fail with
1706 * @param ctx to allocate VP in.
1707 * @param value IPv4/IPv6 address/prefix string.
1708 * @param ipv4 dictionary attribute to use for an IPv4 address.
1709 * @param ipv6 dictionary attribute to use for an IPv6 address.
1710 * @param ipv4_prefix dictionary attribute to use for an IPv4 prefix.
1711 * @param ipv6_prefix dictionary attribute to use for an IPv6 prefix.
1712 * @return NULL on error, or new VALUE_PAIR.
1714 VALUE_PAIR *pairmake_ip(TALLOC_CTX *ctx, char const *value, DICT_ATTR *ipv4, DICT_ATTR *ipv6,
1715 DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix)
1718 DICT_ATTR *da = NULL;
1720 if (!fr_assert(ipv4 || ipv6 || ipv4_prefix || ipv6_prefix)) {
1724 /* No point in repeating the work of pairparsevalue */
1725 if (strchr(value, ':')) {
1726 if (strchr(value, '/')) {
1735 if (strchr(value, '/')) {
1745 fr_strerror_printf("Invalid IP value specified, allowed types are %s%s%s%s",
1746 ipv4 ? "ipaddr " : "", ipv6 ? "ipv6addr " : "",
1747 ipv4_prefix ? "ipv4prefix " : "", ipv6_prefix ? "ipv6prefix" : "");
1750 vp = pairalloc(ctx, da);
1751 if (!vp) return NULL;
1752 if (pairparsevalue(vp, value, 0) < 0) {
1761 /** Create a valuepair from an ASCII attribute and value
1763 * Where the attribute name is in the form:
1765 * - Attr-%d.%d.%d...
1766 * - Vendor-%d-Attr-%d
1767 * - VendorName-Attr-%d
1769 * @param ctx for talloc
1770 * @param attribute name to parse.
1771 * @param value to parse (must be a hex string).
1772 * @param op to assign to new valuepair.
1773 * @return new valuepair or NULL on error.
1775 static VALUE_PAIR *pairmake_any(TALLOC_CTX *ctx,
1776 char const *attribute, char const *value,
1780 DICT_ATTR const *da;
1785 da = dict_attrunknownbyname(attribute, true);
1786 if (!da) return NULL;
1789 * Unknown attributes MUST be of type 'octets'
1791 if (value && (strncasecmp(value, "0x", 2) != 0)) {
1792 fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
1793 "string, not \"%s\"", attribute, value);
1795 dict_attr_free(&da);
1800 * We've now parsed the attribute properly, Let's create
1801 * it. This next stop also looks the attribute up in the
1802 * dictionary, and creates the appropriate type for it.
1804 vp = pairalloc(ctx, da);
1806 dict_attr_free(&da);
1810 vp->op = (op == 0) ? T_OP_EQ : op;
1812 if (!value) return vp;
1814 size = strlen(value + 2);
1815 vp->length = size >> 1;
1816 data = talloc_array(vp, uint8_t, vp->length);
1818 if (fr_hex2bin(data, value + 2, size) != vp->length) {
1819 fr_strerror_printf("Invalid hex string");
1824 vp->vp_octets = data;
1830 /** Create a VALUE_PAIR from ASCII strings
1832 * Converts an attribute string identifier (with an optional tag qualifier)
1833 * and value string into a VALUE_PAIR.
1835 * The string value is parsed according to the type of VALUE_PAIR being created.
1837 * @param[in] ctx for talloc
1838 * @param[in] vps list where the attribute will be added (optional)
1839 * @param[in] attribute name.
1840 * @param[in] value attribute value (may be NULL if value will be set later).
1841 * @param[in] op to assign to new VALUE_PAIR.
1842 * @return a new VALUE_PAIR.
1844 VALUE_PAIR *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps,
1845 char const *attribute, char const *value, FR_TOKEN op)
1847 DICT_ATTR const *da;
1853 char const *attrname = attribute;
1856 * Check for tags in 'Attribute:Tag' format.
1861 ts = strrchr(attribute, ':');
1863 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1868 strlcpy(buffer, attribute, sizeof(buffer));
1870 ts = strrchr(attrname, ':');
1871 if (!ts) return NULL;
1873 /* Colon found with something behind it */
1874 if (ts[1] == '*' && ts[2] == 0) {
1875 /* Wildcard tag for check items */
1878 } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1879 /* It's not a wild card tag */
1880 tag = strtol(ts + 1, &tc, 0);
1881 if (tc && !*tc && TAG_VALID_ZERO(tag))
1885 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1892 * It's not found in the dictionary, so we use
1893 * another method to create the attribute.
1895 da = dict_attrbyname(attrname);
1897 vp = pairmake_any(ctx, attrname, value, op);
1898 if (vp && vps) pairadd(vps, vp);
1902 /* Check for a tag in the 'Merit' format of:
1903 * :Tag:Value. Print an error if we already found
1904 * a tag in the Attribute.
1907 if (value && (*value == ':' && da->flags.has_tag)) {
1908 /* If we already found a tag, this is invalid */
1910 fr_strerror_printf("Duplicate tag %s for attribute %s",
1912 DEBUG("Duplicate tag %s for attribute %s\n",
1916 /* Colon found and attribute allows a tag */
1917 if (value[1] == '*' && value[2] == ':') {
1918 /* Wildcard tag for check items */
1923 tag = strtol(value + 1, &tc, 0);
1924 if (tc && *tc==':' && TAG_VALID_ZERO(tag))
1930 vp = pairalloc(ctx, da);
1931 if (!vp) return NULL;
1932 vp->op = (op == 0) ? T_OP_EQ : op;
1937 case T_OP_CMP_FALSE:
1938 vp->vp_strvalue = NULL;
1940 value = NULL; /* ignore it! */
1944 * Regular expression comparison of integer attributes
1945 * does a STRING comparison of the names of their
1946 * integer attributes.
1948 case T_OP_REG_EQ: /* =~ */
1949 case T_OP_REG_NE: /* !~ */
1955 fr_strerror_printf("Regular expressions are not supported");
1961 * Someone else will fill in the value.
1967 compare = regcomp(®, value, REG_EXTENDED);
1969 regerror(compare, ®, buffer, sizeof(buffer));
1970 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
1976 vp = pairmake(ctx, NULL, attribute, NULL, op);
1977 if (!vp) return NULL;
1979 if (pairmark_xlat(vp, value) < 0) {
1984 value = NULL; /* ignore it */
1993 * FIXME: if (strcasecmp(attribute, vp->da->name) != 0)
1994 * then the user MAY have typed in the attribute name
1995 * as Vendor-%d-Attr-%d, and the value MAY be octets.
1997 * We probably want to fix pairparsevalue to accept
1998 * octets as values for any attribute.
2000 if (value && (pairparsevalue(vp, value, 0) < 0)) {
2005 if (vps) pairadd(vps, vp);
2009 /** Mark a valuepair for xlat expansion
2011 * Copies xlat source (unprocessed) string to valuepair value,
2012 * and sets value type.
2014 * @param vp to mark for expansion.
2015 * @param value to expand.
2016 * @return 0 if marking succeeded or -1 if vp already had a value, or OOM.
2018 int pairmark_xlat(VALUE_PAIR *vp, char const *value)
2023 * valuepair should not already have a value.
2025 if (vp->type != VT_NONE) {
2029 raw = talloc_typed_strdup(vp, value);
2035 vp->value.xlat = raw;
2041 /** Read a single valuepair from a buffer, and advance the pointer
2043 * Sets *eol to T_EOL if end of line was encountered.
2045 * @param[in,out] ptr to read from and update.
2046 * @param[out] raw The struct to write the raw VALUE_PAIR to.
2047 * @return the last token read.
2049 FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw)
2053 FR_TOKEN ret = T_OP_INVALID, next, quote;
2056 if (!ptr || !*ptr || !raw) {
2057 fr_strerror_printf("Invalid arguments");
2058 return T_OP_INVALID;
2062 * Skip leading spaces
2065 while ((*p == ' ') || (*p == '\t')) p++;
2068 fr_strerror_printf("No token read where we expected "
2069 "an attribute name");
2070 return T_OP_INVALID;
2074 fr_strerror_printf("Read a comment instead of a token");
2080 * Try to get the attribute name.
2085 uint8_t const *t = (uint8_t const *) p;
2087 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2089 fr_strerror_printf("Attribute name too long");
2090 return T_OP_INVALID;
2094 * Only ASCII is allowed, and only a subset of that.
2096 if ((*t < 32) || (*t >= 128)) {
2098 fr_strerror_printf("Invalid attribute name");
2099 return T_OP_INVALID;
2103 * This is arguably easier than trying to figure
2104 * out which operators come after the attribute
2105 * name. Yes, our "lexer" is bad.
2107 if (!dict_attr_allowed_chars[(int) *t]) {
2115 * ASCII, but not a valid attribute name.
2117 if (!*raw->l_opand) goto invalid;
2120 * Look for tag (:#). This is different from :=, which
2123 if ((*p == ':') && (isdigit((int) p[1]))) {
2124 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2129 while (isdigit((int) *p)) {
2130 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2140 /* Now we should have an operator here. */
2141 raw->op = gettoken(ptr, buf, sizeof(buf), false);
2142 if (raw->op < T_EQSTART || raw->op > T_EQEND) {
2143 fr_strerror_printf("Expecting operator");
2145 return T_OP_INVALID;
2149 * Read value. Note that empty string values are allowed
2151 quote = gettoken(ptr, raw->r_opand, sizeof(raw->r_opand), false);
2152 if (quote == T_EOL) {
2153 fr_strerror_printf("Failed to get value");
2155 return T_OP_INVALID;
2159 * Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
2163 next = gettoken(&p, buf, sizeof(buf), false);
2174 fr_strerror_printf("Expected end of line or comma");
2175 return T_OP_INVALID;
2183 case T_DOUBLE_QUOTED_STRING:
2185 * Only report as double quoted if it contained valid
2186 * a valid xlat expansion.
2188 p = strchr(raw->r_opand, '%');
2189 if (p && (p[1] == '{')) {
2192 raw->quote = T_SINGLE_QUOTED_STRING;
2205 /** Read one line of attribute/value pairs into a list.
2207 * The line may specify multiple attributes separated by commas.
2209 * @note If the function returns T_OP_INVALID, an error has occurred and
2210 * @note the valuepair list should probably be freed.
2212 * @param ctx for talloc
2213 * @param buffer to read valuepairs from.
2214 * @param list where the parsed VALUE_PAIRs will be appended.
2215 * @return the last token parsed, or T_OP_INVALID
2217 FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **list)
2219 VALUE_PAIR *vp, *head, **tail;
2221 FR_TOKEN last_token = T_OP_INVALID;
2222 FR_TOKEN previous_token;
2226 * We allow an empty line.
2228 if (buffer[0] == 0) {
2237 raw.l_opand[0] = '\0';
2238 raw.r_opand[0] = '\0';
2240 previous_token = last_token;
2242 last_token = pairread(&p, &raw);
2243 if (last_token == T_OP_INVALID) break;
2245 if (raw.quote == T_DOUBLE_QUOTED_STRING) {
2246 vp = pairmake(ctx, NULL, raw.l_opand, NULL, raw.op);
2248 last_token = T_OP_INVALID;
2251 if (pairmark_xlat(vp, raw.r_opand) < 0) {
2253 last_token = T_OP_INVALID;
2257 vp = pairmake(ctx, NULL, raw.l_opand, raw.r_opand, raw.op);
2259 last_token = T_OP_INVALID;
2265 tail = &((*tail)->next);
2266 } while (*p && (last_token == T_COMMA));
2269 * Don't tell the caller that there was a comment.
2271 if (last_token == T_HASH) {
2272 last_token = previous_token;
2275 if (last_token == T_OP_INVALID) {
2278 pairadd(list, head);
2282 * And return the last token which we read.
2288 * Read valuepairs from the fp up to End-Of-File.
2290 int readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone)
2293 FR_TOKEN last_token = T_EOL;
2297 VALUE_PAIR *vp = NULL;
2299 fr_cursor_init(&cursor, out);
2301 while (fgets(buf, sizeof(buf), fp) != NULL) {
2303 * If we get a '\n' by itself, we assume that's
2304 * the end of that VP
2306 if (buf[0] == '\n') {
2312 * Comments get ignored
2314 if (buf[0] == '#') continue;
2317 * Read all of the attributes on the current line.
2320 last_token = userparse(ctx, buf, &vp);
2322 if (last_token != T_EOL) goto error;
2326 fr_cursor_insert(&cursor, vp);
2335 vp = fr_cursor_first(&cursor);
2336 if (vp) pairfree(&vp);
2341 /** Compare two attribute values
2343 * @param[in] one the first attribute.
2344 * @param[in] two the second attribute.
2345 * @return -1 if one is less than two, 0 if both are equal, 1 if one is more than two, < -1 on error.
2347 int8_t paircmp_value(VALUE_PAIR const *one, VALUE_PAIR const *two)
2349 int64_t compare = 0;
2354 if (one->da->type != two->da->type) {
2355 fr_strerror_printf("Can't compare attribute values of different types");
2360 * After doing the previous check for special comparisons,
2361 * do the per-type comparison here.
2363 switch (one->da->type) {
2364 case PW_TYPE_ABINARY:
2365 case PW_TYPE_OCTETS:
2369 if (one->length > two->length) {
2370 length = one->length;
2372 length = two->length;
2376 compare = memcmp(one->vp_octets, two->vp_octets, length);
2377 if (compare != 0) break;
2381 * Contents are the same. The return code
2382 * is therefore the difference in lengths.
2384 * i.e. "0x00" is smaller than "0x0000"
2386 compare = one->length - two->length;
2390 case PW_TYPE_STRING:
2391 fr_assert(one->vp_strvalue);
2392 fr_assert(two->vp_strvalue);
2393 compare = strcmp(one->vp_strvalue, two->vp_strvalue);
2396 case PW_TYPE_BOOLEAN:
2399 case PW_TYPE_INTEGER:
2401 compare = (int64_t) one->vp_integer - (int64_t) two->vp_integer;
2404 case PW_TYPE_SIGNED:
2405 compare = one->vp_signed - two->vp_signed;
2408 case PW_TYPE_INTEGER64:
2410 * Don't want integer overflow!
2412 if (one->vp_integer64 < two->vp_integer64) {
2414 } else if (one->vp_integer64 > two->vp_integer64) {
2419 case PW_TYPE_ETHERNET:
2420 compare = memcmp(&one->vp_ether, &two->vp_ether, sizeof(one->vp_ether));
2423 case PW_TYPE_IPADDR:
2424 compare = (int64_t) ntohl(one->vp_ipaddr) - (int64_t) ntohl(two->vp_ipaddr);
2427 case PW_TYPE_IPV6ADDR:
2428 compare = memcmp(&one->vp_ipv6addr, &two->vp_ipv6addr, sizeof(one->vp_ipv6addr));
2431 case PW_TYPE_IPV6PREFIX:
2432 compare = memcmp(&one->vp_ipv6prefix, &two->vp_ipv6prefix, sizeof(one->vp_ipv6prefix));
2435 case PW_TYPE_IPV4PREFIX:
2436 compare = memcmp(&one->vp_ipv4prefix, &two->vp_ipv4prefix, sizeof(one->vp_ipv4prefix));
2440 compare = memcmp(&one->vp_ifid, &two->vp_ifid, sizeof(one->vp_ifid));
2444 * None of the types below should be in the REQUEST
2446 case PW_TYPE_INVALID: /* We should never see these */
2447 case PW_TYPE_COMBO_IP: /* This should of been converted into IPADDR/IPV6ADDR */
2448 case PW_TYPE_COMBO_IPPREFIX: /* This should of been converted into IPADDR/IPV6ADDR */
2450 case PW_TYPE_EXTENDED:
2451 case PW_TYPE_LONG_EXTENDED:
2454 case PW_TYPE_TIMEVAL:
2456 fr_assert(0); /* unknown type */
2460 * Do NOT add a default here, as new types are added
2461 * static analysis will warn us they're not handled
2467 } else if (compare < 0) {
2474 * We leverage the fact that IPv4 and IPv6 prefixes both
2475 * have the same format:
2477 * reserved, prefix-len, data...
2479 static int paircmp_op_cidr(FR_TOKEN op, int bytes,
2480 uint8_t one_net, uint8_t const *one,
2481 uint8_t two_net, uint8_t const *two)
2487 * Handle the case of netmasks being identical.
2489 if (one_net == two_net) {
2492 compare = memcmp(one, two, bytes);
2495 * If they're identical return true for
2498 if ((compare == 0) &&
2499 ((op == T_OP_CMP_EQ) ||
2506 * Everything else returns false.
2508 * 10/8 == 24/8 --> false
2509 * 10/8 <= 24/8 --> false
2510 * 10/8 >= 24/8 --> false
2516 * Netmasks are different. That limits the
2517 * possible results, based on the operator.
2527 case T_OP_LT: /* 192/8 < 192.168/16 --> false */
2528 if (one_net < two_net) {
2534 case T_OP_GT: /* 192/16 > 192.168/8 --> false */
2535 if (one_net > two_net) {
2544 if (one_net < two_net) {
2551 * Do the check byte by byte. If the bytes are
2552 * identical, it MAY be a match. If they're different,
2553 * it is NOT a match.
2558 * All leading bytes are identical.
2560 if (common == 0) return true;
2563 * Doing bitmasks takes more work.
2565 if (common < 8) break;
2567 if (one[i] != two[i]) return false;
2575 mask <<= (8 - common);
2579 if ((one[i] & mask) == ((two[i] & mask))) {
2586 /** Compare two attributes using an operator
2588 * @param[in] a the first attribute
2589 * @param[in] op the operator for comparison.
2590 * @param[in] b the second attribute
2591 * @return 1 if true, 0 if false, -1 on error.
2593 int8_t paircmp_op(VALUE_PAIR const *a, FR_TOKEN op, VALUE_PAIR const *b)
2597 if (!a || !b) return -1;
2599 switch (a->da->type) {
2600 case PW_TYPE_IPADDR:
2601 switch (b->da->type) {
2602 case PW_TYPE_IPADDR: /* IPv4 and IPv4 */
2605 case PW_TYPE_IPV4PREFIX: /* IPv4 and IPv4 Prefix */
2606 return paircmp_op_cidr(op, 4, 32, (uint8_t const *) &a->vp_ipaddr,
2607 b->vp_ipv4prefix[1], (uint8_t const *) &b->vp_ipv4prefix + 2);
2610 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
2615 case PW_TYPE_IPV4PREFIX: /* IPv4 and IPv4 Prefix */
2616 switch (b->da->type) {
2617 case PW_TYPE_IPADDR:
2618 return paircmp_op_cidr(op, 4, a->vp_ipv4prefix[1],
2619 (uint8_t const *) &a->vp_ipv4prefix + 2,
2620 32, (uint8_t const *) &b->vp_ipaddr);
2622 case PW_TYPE_IPV4PREFIX: /* IPv4 Prefix and IPv4 Prefix */
2623 return paircmp_op_cidr(op, 4, a->vp_ipv4prefix[1],
2624 (uint8_t const *) &a->vp_ipv4prefix + 2,
2625 b->vp_ipv4prefix[1], (uint8_t const *) &b->vp_ipv4prefix + 2);
2628 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
2633 case PW_TYPE_IPV6ADDR:
2634 switch (b->da->type) {
2635 case PW_TYPE_IPV6ADDR: /* IPv6 and IPv6 */
2638 case PW_TYPE_IPV6PREFIX: /* IPv6 and IPv6 Preifx */
2639 return paircmp_op_cidr(op, 16, 128, (uint8_t const *) &a->vp_ipv6addr,
2640 b->vp_ipv6prefix[1], (uint8_t const *) &b->vp_ipv6prefix + 2);
2644 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
2649 case PW_TYPE_IPV6PREFIX:
2650 switch (b->da->type) {
2651 case PW_TYPE_IPV6ADDR: /* IPv6 Prefix and IPv6 */
2652 return paircmp_op_cidr(op, 16, a->vp_ipv6prefix[1],
2653 (uint8_t const *) &a->vp_ipv6prefix + 2,
2654 128, (uint8_t const *) &b->vp_ipv6addr);
2656 case PW_TYPE_IPV6PREFIX: /* IPv6 Prefix and IPv6 */
2657 return paircmp_op_cidr(op, 16, a->vp_ipv6prefix[1],
2658 (uint8_t const *) &a->vp_ipv6prefix + 2,
2659 b->vp_ipv6prefix[1], (uint8_t const *) &b->vp_ipv6prefix + 2);
2662 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
2669 compare = paircmp_value(a, b);
2670 if (compare < -1) { /* comparison error */
2676 * Now do the operator comparison.
2680 return (compare == 0);
2683 return (compare != 0);
2686 return (compare < 0);
2689 return (compare > 0);
2692 return (compare <= 0);
2695 return (compare >= 0);
2702 /** Compare two pairs, using the operator from "a"
2704 * i.e. given two attributes, it does:
2706 * (b->data) (a->operator) (a->data)
2708 * e.g. "foo" != "bar"
2710 * @param[in] a the first attribute
2711 * @param[in] b the second attribute
2712 * @return 1 if true, 0 if false, -1 on error.
2714 int8_t paircmp(VALUE_PAIR *a, VALUE_PAIR *b)
2719 if (b) VERIFY_VP(b);
2725 case T_OP_CMP_FALSE:
2729 * a is a regex, compile it, print b to a string,
2730 * and then do string comparisons.
2740 char buffer[MAX_STRING_LEN * 4 + 1];
2742 compare = regcomp(®, a->vp_strvalue, REG_EXTENDED);
2744 regerror(compare, ®, buffer, sizeof(buffer));
2745 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
2746 a->da->name, buffer);
2755 vp_prints_value(buffer, sizeof(buffer), b, 0);
2758 * Don't care about substring matches,
2761 compare = regexec(®, buffer, 0, NULL, 0);
2764 if (a->op == T_OP_REG_EQ) {
2765 return (compare == 0);
2768 return (compare != 0);
2772 default: /* we're OK */
2776 return paircmp_op(b, a->op, a);
2779 /** Determine equality of two lists
2781 * This is useful for comparing lists of attributes inserted into a binary tree.
2783 * @param a first list of VALUE_PAIRs.
2784 * @param b second list of VALUE_PAIRs.
2785 * @return -1 if a < b, 0 if the two lists are equal, 1 if a > b, -2 on error.
2787 int8_t pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b)
2789 vp_cursor_t a_cursor, b_cursor;
2790 VALUE_PAIR *a_p, *b_p;
2793 for (a_p = fr_cursor_init(&a_cursor, &a), b_p = fr_cursor_init(&b_cursor, &b);
2795 a_p = fr_cursor_next(&a_cursor), b_p = fr_cursor_next(&b_cursor)) {
2796 /* Same VP, no point doing expensive checks */
2801 if (a_p->da < b_p->da) {
2804 if (a_p->da > b_p->da) {
2808 if (a_p->tag < b_p->tag) {
2811 if (a_p->tag > b_p->tag) {
2815 ret = paircmp_value(a_p, b_p);
2817 fr_assert(ret >= -1); /* Comparison error */
2834 /** Set the type of the VALUE_PAIR value buffer to match it's DICT_ATTR
2836 * @param vp to fixup.
2838 static void pairtypeset(VALUE_PAIR *vp)
2840 if (!vp->data.ptr) return;
2842 switch(vp->da->type) {
2843 case PW_TYPE_OCTETS:
2845 talloc_set_type(vp->data.ptr, uint8_t);
2848 case PW_TYPE_STRING:
2849 talloc_set_type(vp->data.ptr, char);
2857 /** Copy data into an "octets" data type.
2859 * @param[in,out] vp to update
2860 * @param[in] src data to copy
2861 * @param[in] size of the data, may be 0 in which case previous value will be freed.
2863 void pairmemcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
2865 uint8_t *p = NULL, *q;
2870 p = talloc_memdup(vp, src, size);
2872 talloc_set_type(p, uint8_t);
2875 memcpy(&q, &vp->vp_octets, sizeof(q));
2881 if (size > 0) pairtypeset(vp);
2884 /** Reparent an allocated octet buffer to a VALUE_PAIR
2886 * @param[in,out] vp to update
2887 * @param[in] src buffer to steal.
2889 void pairmemsteal(VALUE_PAIR *vp, uint8_t const *src)
2895 memcpy(&q, &vp->vp_octets, sizeof(q));
2898 vp->vp_octets = talloc_steal(vp, src);
2900 vp->length = talloc_array_length(vp->vp_strvalue);
2904 /** Reparent an allocated char buffer to a VALUE_PAIR
2906 * @param[in,out] vp to update
2907 * @param[in] src buffer to steal.
2909 void pairstrsteal(VALUE_PAIR *vp, char const *src)
2915 memcpy(&q, &vp->vp_octets, sizeof(q));
2918 vp->vp_strvalue = talloc_steal(vp, src);
2920 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2924 /** Copy data into an "string" data type.
2926 * @param[in,out] vp to update
2927 * @param[in] src data to copy
2929 void pairstrcpy(VALUE_PAIR *vp, char const *src)
2935 p = talloc_strdup(vp, src);
2939 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2942 vp->vp_strvalue = p;
2944 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2948 /** Copy data into an "string" data type.
2950 * @param[in,out] vp to update.
2951 * @param[in] src data to copy.
2952 * @param[in] len of data to copy.
2954 void pairstrncpy(VALUE_PAIR *vp, char const *src, size_t len)
2960 p = talloc_array(vp, char, len + 1);
2963 memcpy(p, src, len); /* embdedded \0 safe */
2966 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2969 vp->vp_strvalue = p;
2975 /** Print data into an "string" data type.
2977 * @param[in,out] vp to update
2978 * @param[in] fmt the format string
2980 void pairsprintf(VALUE_PAIR *vp, char const *fmt, ...)
2988 p = talloc_vasprintf(vp, fmt, ap);
2993 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2996 vp->vp_strvalue = p;
2999 vp->length = talloc_array_length(vp->vp_strvalue) - 1;