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 = FREE_MAGIC;
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");
115 vp->length = da->flags.length;
117 talloc_set_destructor(vp, _pairfree);
122 /** Create a new valuepair
124 * If attr and vendor match a dictionary entry then a VP with that DICT_ATTR
127 * If attr or vendor are uknown will call dict_attruknown to create a dynamic
128 * DICT_ATTR of PW_TYPE_OCTETS.
130 * Which type of DICT_ATTR the VALUE_PAIR was created with can be determined by
131 * checking @verbatim vp->da->flags.is_unknown @endverbatim.
133 * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
134 * @param[in] attr number.
135 * @param[in] vendor number.
136 * @return the new valuepair or NULL on error.
138 VALUE_PAIR *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
142 da = dict_attrbyvalue(attr, vendor);
144 da = dict_attrunknown(attr, vendor, true);
150 return pairalloc(ctx, da);
153 /** Free memory used by a valuepair list.
155 * @todo TLV: needs to free all dependents of each VP freed.
157 void pairfree(VALUE_PAIR **vps)
166 for (vp = fr_cursor_init(&cursor, vps);
168 vp = fr_cursor_next(&cursor)) {
176 /** Mark malformed or unrecognised attributed as unknown
178 * @param vp to change DICT_ATTR of.
179 * @return 0 on success (or if already unknown) else -1 on error.
181 int pair2unknown(VALUE_PAIR *vp)
186 if (vp->da->flags.is_unknown) {
190 da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
199 /** Find the pair with the matching DAs
202 VALUE_PAIR *pairfind_da(VALUE_PAIR *vp, DICT_ATTR const *da, int8_t tag)
211 for (i = fr_cursor_init(&cursor, &vp);
213 i = fr_cursor_next(&cursor)) {
215 if ((i->da == da) && (!i->da->flags.has_tag || (tag == TAG_ANY) || (i->tag == tag))) {
224 /** Find the pair with the matching attribute
226 * @todo should take DAs and do a pointer comparison.
228 VALUE_PAIR *pairfind(VALUE_PAIR *vp, unsigned int attr, unsigned int vendor, int8_t tag)
235 for (i = fr_cursor_init(&cursor, &vp);
237 i = fr_cursor_next(&cursor)) {
238 if ((i->da->attr == attr) && (i->da->vendor == vendor) && \
239 (!i->da->flags.has_tag || (tag == TAG_ANY) || (i->tag == tag))) {
247 /** Delete matching pairs
249 * Delete matching pairs from the attribute list.
251 * @param[in,out] first VP in list.
252 * @param[in] attr to match.
253 * @param[in] vendor to match.
254 * @param[in] tag to match. TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
256 * @todo should take DAs and do a point comparison.
258 void pairdelete(VALUE_PAIR **first, unsigned int attr, unsigned int vendor,
261 VALUE_PAIR *i, *next;
262 VALUE_PAIR **last = first;
264 for(i = *first; i; i = next) {
267 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
269 (i->da->flags.has_tag && (i->tag == tag)))) {
278 /** Add a VP to the end of the list.
280 * Locates the end of 'first', and links an additional VP 'add' at the end.
282 * @param[in] first VP in linked list. Will add new VP to the end of this list.
283 * @param[in] add VP to add to list.
285 void pairadd(VALUE_PAIR **first, VALUE_PAIR *add)
293 if (*first == NULL) {
297 for(i = *first; i->next; i = i->next)
302 /** Replace all matching VPs
304 * Walks over 'first', and replaces the first VP that matches 'replace'.
306 * @note Memory used by the VP being replaced will be freed.
307 * @note Will not work with unknown attributes.
309 * @param[in,out] first VP in linked list. Will search and replace in this list.
310 * @param[in] replace VP to replace.
312 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *replace)
314 VALUE_PAIR *i, *next;
315 VALUE_PAIR **prev = first;
319 if (*first == NULL) {
325 * Not an empty list, so find item if it is there, and
326 * replace it. Note, we always replace the first one, and
327 * we ignore any others that might exist.
329 for(i = *first; i; i = next) {
334 * Found the first attribute, replace it,
337 if ((i->da == replace->da) &&
338 (!i->da->flags.has_tag || (i->tag == replace->tag))
343 * Should really assert that replace->next == NULL
345 replace->next = next;
351 * Point to where the attribute should go.
357 * If we got here, we didn't find anything to replace, so
358 * stopped at the last item, which we just append to.
363 int8_t attrcmp(VALUE_PAIR const *a, VALUE_PAIR const *b)
372 if (a->da == b->da) {
379 int8_t attrtagcmp(VALUE_PAIR const *a, VALUE_PAIR const *b)
388 if (cmp != 0) return cmp;
390 if (a->tag < b->tag) {
394 if (a->tag > b->tag) {
401 static void pairsort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
407 * Stopping condition - no more elements left to split
409 if (!source || !source->next) {
417 * Fast advances twice as fast as slow, so when it gets to the end,
418 * slow will point to the middle of the linked list.
436 static VALUE_PAIR *pairsort_merge(VALUE_PAIR *a, VALUE_PAIR *b, fr_pair_cmp_t cmp)
438 VALUE_PAIR *result = NULL;
444 * Compare the DICT_ATTRs and tags
446 if (cmp(a, b) <= 0) {
448 result->next = pairsort_merge(a->next, b, cmp);
451 result->next = pairsort_merge(a, b->next, cmp);
457 /** Sort a linked list of VALUE_PAIRs using merge sort
459 * @param[in,out] vps List of VALUE_PAIRs to sort.
460 * @param[in] cmp to sort with
462 void pairsort(VALUE_PAIR **vps, fr_pair_cmp_t cmp)
464 VALUE_PAIR *head = *vps;
469 * If there's 0-1 elements it must already be sorted.
471 if (!head || !head->next) {
475 pairsort_split(head, &a, &b); /* Split into sublists */
476 pairsort(&a, cmp); /* Traverse left */
477 pairsort(&b, cmp); /* Traverse right */
480 * merge the two sorted lists together
482 *vps = pairsort_merge(a, b, cmp);
485 /** Write an error to the library errorbuff detailing the mismatch
487 * Retrieve output with fr_strerror();
489 * @todo add thread specific talloc contexts.
491 * @param ctx a hack until we have thread specific talloc contexts.
492 * @param failed pair of attributes which didn't match.
494 void pairvalidate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2])
496 VALUE_PAIR const *filter = failed[0];
497 VALUE_PAIR const *list = failed[1];
501 (void) fr_strerror(); /* Clear any existing messages */
503 if (!filter && !list)
505 if (!fr_assert(!(!filter && !list))) return;
508 fr_strerror_printf("Attribute \"%s\" not found in list", filter->da->name);
512 if (!filter || (filter->da != list->da)) {
513 fr_strerror_printf("Attribute \"%s\" not found in filter", list->da->name);
517 if (filter->tag != list->tag) {
518 fr_strerror_printf("Attribute \"%s\" tag \"%i\" didn't match filter tag \"%i\"",
519 list->da->name, list->tag, list->tag);
523 pair = vp_aprint(ctx, filter);
524 value = vp_aprints(ctx, list);
526 fr_strerror_printf("Attribute value \"%s\" didn't match filter \"%s\"", value, pair);
534 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
536 * @note will sort both filter and list in place.
538 * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
540 * @param filter attributes to check list against.
541 * @param list attributes, probably a request or reply
543 bool pairvalidate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
545 vp_cursor_t filter_cursor;
546 vp_cursor_t list_cursor;
548 VALUE_PAIR *check, *match;
550 if (!filter && !list) {
555 * This allows us to verify the sets of validate and reply are equal
556 * i.e. we have a validate rule which matches every reply attribute.
558 * @todo this should be removed one we have sets and lists
560 pairsort(&filter, attrtagcmp);
561 pairsort(&list, attrtagcmp);
563 check = fr_cursor_init(&filter_cursor, &filter);
564 match = fr_cursor_init(&list_cursor, &list);
568 * The lists are sorted, so if the first
569 * attributes aren't of the same type, then we're
572 if (!attribute_eq(check, match)) goto mismatch;
575 * They're of the same type, but don't have the
576 * same values. This is a problem.
578 * Note that the RFCs say that for attributes of
579 * the same type, order is important.
581 if (!paircmp(check, match)) goto mismatch;
583 check = fr_cursor_next(&filter_cursor);
584 match = fr_cursor_next(&list_cursor);
585 if (!match && !check) goto mismatch;
588 * One list ended earlier than the others, they
591 if (!match || !check) goto mismatch;
604 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
606 * @note will sort both filter and list in place.
608 * @param filter attributes to check list against.
609 * @param list attributes, probably a request or reply
611 bool pairvalidate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
613 vp_cursor_t filter_cursor;
614 vp_cursor_t list_cursor;
616 VALUE_PAIR *check, *match, *last_check = NULL, *last_match;
618 if (!filter && !list) {
623 * This allows us to verify the sets of validate and reply are equal
624 * i.e. we have a validate rule which matches every reply attribute.
626 * @todo this should be removed one we have sets and lists
628 pairsort(&filter, attrtagcmp);
629 pairsort(&list, attrtagcmp);
631 fr_cursor_init(&list_cursor, &list);
632 for (check = fr_cursor_init(&filter_cursor, &filter);
634 check = fr_cursor_next(&filter_cursor)) {
636 * Were processing check attributes of a new type.
638 if (!attribute_eq(last_check, check)) {
640 * Record the start of the matching attributes in the pair list
641 * For every other operator we require the match to be present
643 last_match = fr_cursor_next_by_da(&list_cursor, check->da, check->tag);
645 if (check->op == T_OP_CMP_FALSE) continue;
649 fr_cursor_init(&list_cursor, &last_match);
654 * Now iterate over all attributes of the same type.
656 for (match = fr_cursor_first(&list_cursor);
657 attribute_eq(match, check);
658 match = fr_cursor_next(&list_cursor)) {
660 * This attribute passed the filter
662 if (!paircmp(check, match)) goto mismatch;
676 /** Copy a single valuepair
678 * Allocate a new valuepair and copy the da from the old vp.
680 * @param[in] ctx for talloc
681 * @param[in] vp to copy.
682 * @return a copy of the input VP or NULL on error.
684 VALUE_PAIR *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
688 if (!vp) return NULL;
692 n = pairalloc(ctx, vp->da);
695 memcpy(n, vp, sizeof(*n));
700 if (vp->type == VT_XLAT) {
701 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
704 n->da = dict_attr_copy(vp->da, true);
712 if (n->data.ptr) switch (n->da->type) {
715 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
716 talloc_set_type(n->vp_octets, uint8_t);
720 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1); /* NULL byte */
721 talloc_set_type(n->vp_strvalue, char);
731 /** Copy data from one VP to another
733 * Allocate a new pair using da, and copy over the value from the specified
736 * @todo Should be able to do type conversions.
738 * @param[in] ctx for talloc
739 * @param[in] da of new attribute to alloc.
740 * @param[in] vp to copy data from.
741 * @return the new valuepair.
743 VALUE_PAIR *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp)
747 if (!vp) return NULL;
752 * The types have to be identical, OR the "from" VP has
755 if (da->type != vp->da->type) {
758 VALUE_PAIR const **pvp;
760 if (vp->da->type == PW_TYPE_OCTETS) {
762 * Decode the data. It may be wrong!
764 if (rad_data2vp(da->attr, da->vendor, vp->vp_octets, vp->length, &n) < 0) {
773 * Else the destination type is octets
775 switch (vp->da->type) {
777 return NULL; /* can't do it */
779 case PW_TYPE_INTEGER:
783 case PW_TYPE_IPV6ADDR:
784 case PW_TYPE_IPV6PREFIX:
787 case PW_TYPE_ETHERNET:
789 case PW_TYPE_INTEGER64:
790 case PW_TYPE_IPV4PREFIX:
794 n = pairalloc(ctx, da);
797 p = talloc_array(n, uint8_t, dict_attr_sizes[vp->da->type][1] + 2);
800 length = rad_vp2attr(NULL, NULL, NULL, pvp, p, dict_attr_sizes[vp->da->type][1]);
806 pairmemcpy(n, p + 2, length - 2);
811 n = pairalloc(ctx, da);
814 memcpy(n, vp, sizeof(*n));
817 if (n->type == VT_XLAT) {
818 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
821 if (n->data.ptr) switch (n->da->type) {
824 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
825 talloc_set_type(n->vp_octets, uint8_t);
829 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1); /* NULL byte */
830 talloc_set_type(n->vp_strvalue, char);
845 * Copy all pairs from 'from' regardless of tag, attribute or vendor.
847 * @param[in] ctx for new VALUE_PAIRs to be allocated in.
848 * @param[in] from whence to copy VALUE_PAIRs.
849 * @return the head of the new VALUE_PAIR list or NULL on error.
851 VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from)
853 vp_cursor_t src, dst;
855 VALUE_PAIR *out = NULL, *vp;
857 fr_cursor_init(&dst, &out);
858 for (vp = fr_cursor_init(&src, &from);
860 vp = fr_cursor_next(&src)) {
862 vp = paircopyvp(ctx, vp);
867 fr_cursor_insert(&dst, vp); /* paircopy sets next pointer to NULL */
873 /** Copy matching pairs
875 * Copy pairs of a matching attribute number, vendor number and tag from the
876 * the input list to a new list, and returns the head of this list.
878 * @param[in] ctx for talloc
879 * @param[in] from whence to copy VALUE_PAIRs.
880 * @param[in] attr to match, if 0 input list will not be filtered by attr.
881 * @param[in] vendor to match.
882 * @param[in] tag to match, TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
883 * @return the head of the new VALUE_PAIR list or NULL on error.
885 VALUE_PAIR *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from,
886 unsigned int attr, unsigned int vendor, int8_t tag)
888 vp_cursor_t src, dst;
890 VALUE_PAIR *out = NULL, *vp;
892 fr_cursor_init(&dst, &out);
893 for (vp = fr_cursor_init(&src, &from);
895 vp = fr_cursor_next(&src)) {
898 if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
902 if ((tag != TAG_ANY) && vp->da->flags.has_tag && (vp->tag != tag)) {
906 vp = paircopyvp(ctx, vp);
911 fr_cursor_insert(&dst, vp);
917 /** Steal all members of a VALUE_PAIR list
919 * @param[in] ctx to move VALUE_PAIRs into
920 * @param[in] from VALUE_PAIRs to move into the new context.
922 VALUE_PAIR *pairsteal(TALLOC_CTX *ctx, VALUE_PAIR *from)
927 for (vp = fr_cursor_init(&cursor, &from);
929 vp = fr_cursor_next(&cursor)) {
930 (void) talloc_steal(ctx, vp);
936 /** Move pairs from source list to destination list respecting operator
938 * @note This function does some additional magic that's probably not needed
939 * in most places. Consider using radius_pairmove in server code.
941 * @note pairfree should be called on the head of the source list to free
942 * unmoved attributes (if they're no longer needed).
944 * @note Does not respect tags when matching.
946 * @param[in] ctx for talloc
947 * @param[in,out] to destination list.
948 * @param[in,out] from source list.
950 * @see radius_pairmove
952 void pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from)
954 VALUE_PAIR *i, *found;
955 VALUE_PAIR *head_new, **tail_new;
956 VALUE_PAIR **tail_from;
958 if (!to || !from || !*from) return;
961 * We're editing the "to" list while we're adding new
962 * attributes to it. We don't want the new attributes to
963 * be edited, so we create an intermediate list to hold
964 * them during the editing process.
967 tail_new = &head_new;
970 * We're looping over the "from" list, moving some
971 * attributes out, but leaving others in place.
974 while ((i = *tail_from) != NULL) {
978 * We never move Fall-Through.
980 if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
981 tail_from = &(i->next);
986 * Unlike previous versions, we treat all other
987 * attributes as normal. i.e. there's no special
988 * treatment for passwords or Hint.
993 * Anything else are operators which
994 * shouldn't occur. We ignore them, and
995 * leave them in place.
998 tail_from = &(i->next);
1002 * Add it to the "to" list, but only if
1003 * it doesn't already exist.
1006 found = pairfind(*to, i->da->attr, i->da->vendor,
1008 if (!found) goto do_add;
1010 tail_from = &(i->next);
1014 * Add it to the "to" list, and delete any attribute
1015 * of the same vendor/attr which already exists.
1018 found = pairfind(*to, i->da->attr, i->da->vendor,
1020 if (!found) goto do_add;
1023 * Do NOT call pairdelete() here,
1024 * due to issues with re-writing
1025 * "request->username".
1027 * Everybody calls pairmove, and
1028 * expects it to work. We can't
1029 * update request->username here,
1030 * so instead we over-write the
1031 * vp that it's pointing to.
1033 switch (found->da->type) {
1038 memcpy(found, i, sizeof(*found));
1043 pairmemsteal(found, i->vp_tlv);
1047 case PW_TYPE_OCTETS:
1048 pairmemsteal(found, i->vp_octets);
1049 i->vp_octets = NULL;
1052 case PW_TYPE_STRING:
1053 pairstrsteal(found, i->vp_strvalue);
1054 i->vp_strvalue = NULL;
1055 found->tag = i->tag;
1060 * Delete *all* of the attributes
1061 * of the same number.
1063 pairdelete(&found->next,
1065 found->da->vendor, TAG_ANY);
1068 * Remove this attribute from the
1071 *tail_from = i->next;
1077 * Move it from the old list and add it
1082 *tail_from = i->next;
1084 *tail_new = talloc_steal(ctx, i);
1085 tail_new = &(i->next);
1088 } /* loop over the "from" list. */
1091 * Take the "new" list, and append it to the "to" list.
1093 pairadd(to, head_new);
1096 /** Move matching pairs between VALUE_PAIR lists
1098 * Move pairs of a matching attribute number, vendor number and tag from the
1099 * the input list to the output list.
1101 * @note pairfree should be called on the head of the old list to free unmoved
1102 attributes (if they're no longer needed).
1104 * @param[in] ctx for talloc
1105 * @param[in,out] to destination list.
1106 * @param[in,out] from source list.
1107 * @param[in] attr to match, if PW_VENDOR_SPECIFIC and vendor 0, only VSAs will
1108 * be copied. If 0 and 0, all attributes will match
1109 * @param[in] vendor to match.
1110 * @param[in] tag to match, TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
1112 void pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from, unsigned int attr, unsigned int vendor, int8_t tag)
1114 VALUE_PAIR *to_tail, *i, *next;
1115 VALUE_PAIR *iprev = NULL;
1118 * Find the last pair in the "to" list and put it in "to_tail".
1120 * @todo: replace the "if" with "VALUE_PAIR **tail"
1124 for(i = *to; i; i = i->next) {
1132 * Attr/vendor of 0 means "move them all".
1133 * It's better than "pairadd(foo,bar);bar=NULL"
1135 if ((vendor == 0) && (attr == 0)) {
1137 to_tail->next = *from;
1142 for (i = *from; i; i = i->next) {
1143 (void) talloc_steal(ctx, i);
1150 for(i = *from; i; i = next) {
1154 if ((tag != TAG_ANY) && i->da->flags.has_tag &&
1160 * vendor=0, attr = PW_VENDOR_SPECIFIC means
1161 * "match any vendor attribute".
1163 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
1165 * It's a VSA: move it over.
1167 if (i->da->vendor != 0) goto move;
1170 * It's Vendor-Specific: move it over.
1172 if (i->da->attr == attr) goto move;
1175 * It's not a VSA: ignore it.
1182 * If it isn't an exact match, ignore it.
1184 if (!((i->da->vendor == vendor) && (i->da->attr == attr))) {
1191 * Remove the attribute from the "from" list.
1199 * Add the attribute to the "to" list.
1207 (void) talloc_steal(ctx, i);
1211 static char const *hextab = "0123456789abcdef";
1213 bool pairparsevalue(VALUE_PAIR *vp, char const *value)
1216 char const *cp, *cs;
1222 if (!value) return false;
1226 * It's a comparison, not a real VALUE_PAIR, copy the string over verbatim
1228 if ((vp->op == T_OP_REG_EQ) || (vp->op == T_OP_REG_NE)) {
1229 pairstrcpy(vp, value); /* Icky hacky ewww */
1233 switch(vp->da->type) {
1234 case PW_TYPE_STRING:
1238 p = talloc_typed_strdup(vp, value);
1239 vp->vp_strvalue = p;
1277 c = '\\'; /* no cp++ */
1280 if ((cp[0] >= '0') &&
1286 (sscanf(cp, "%3o", &x) == 1)) {
1295 } /* else at EOL \ --> \ */
1302 vp->length = length;
1305 case PW_TYPE_IPADDR:
1307 * FIXME: complain if hostname
1308 * cannot be resolved, or resolve later!
1316 * Convert things which are obviously integers to IP addresses
1318 * We assume the number is the bigendian representation of the
1321 if (fr_integer_check(value)) {
1322 vp->vp_ipaddr = htonl(atol(value));
1327 * Certain applications/databases print IPv4 addresses with a
1328 * /32 suffix. Strip it off if the mask is 32, else error out.
1330 p = strchr(value, '/');
1332 if ((p[1] != '3') || (p[2] != '2') || (p[3] != '\0')) {
1333 fr_strerror_printf("Invalid IP address suffix \"%s\". Only '/32' permitted "
1334 "for non-prefix types", p);
1338 strlcpy(ipv4, value, sizeof(ipv4));
1339 ipv4[p - value] = '\0';
1345 if (ip_hton(cs, AF_INET, &ipaddr) < 0) {
1346 fr_strerror_printf("Failed to find IP address for %s", cs);
1350 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1359 * Note that ALL integers are unsigned!
1361 vp->vp_integer = fr_strtoul(value, &p);
1363 if (vp->vp_integer > 255) {
1364 fr_strerror_printf("Byte value \"%s\" is larger than 255", value);
1369 if (fr_whitespace_check(p)) break;
1370 goto check_for_value;
1374 * Note that ALL integers are unsigned!
1376 vp->vp_integer = fr_strtoul(value, &p);
1379 if (vp->vp_integer > 65535) {
1380 fr_strerror_printf("Byte value \"%s\" is larger than 65535", value);
1385 if (fr_whitespace_check(p)) break;
1386 goto check_for_value;
1388 case PW_TYPE_INTEGER:
1390 * Note that ALL integers are unsigned!
1392 vp->vp_integer = fr_strtoul(value, &p);
1395 if (fr_whitespace_check(p)) break;
1399 * Look for the named value for the given
1402 if ((dval = dict_valbyname(vp->da->attr, vp->da->vendor, value)) == NULL) {
1403 fr_strerror_printf("Unknown value '%s' for attribute '%s'", value, vp->da->name);
1406 vp->vp_integer = dval->value;
1409 case PW_TYPE_INTEGER64:
1411 * Note that ALL integers are unsigned!
1413 if (sscanf(value, "%" PRIu64, &y) != 1) {
1414 fr_strerror_printf("Invalid value '%s' for attribute '%s'",
1415 value, vp->da->name);
1418 vp->vp_integer64 = y;
1420 length = strspn(value, "0123456789");
1421 if (fr_whitespace_check(value + length)) break;
1427 * time_t may be 64 bits, whule vp_date
1428 * MUST be 32-bits. We need an
1429 * intermediary variable to handle
1434 if (fr_get_time(value, &date) < 0) {
1435 fr_strerror_printf("failed to parse time string "
1445 case PW_TYPE_ABINARY:
1446 #ifdef WITH_ASCEND_BINARY
1447 if (strncasecmp(value, "0x", 2) == 0) {
1451 if (ascend_parse_filter(vp, value) < 0 ) {
1452 /* Allow ascend_parse_filter's strerror to bubble up */
1458 * If Ascend binary is NOT defined,
1459 * then fall through to raw octets, so that
1460 * the user can at least make them by hand...
1463 /* raw octets: 0x01020304... */
1465 if (strcmp(value, "ANY") == 0) {
1468 } /* else it's hex */
1470 case PW_TYPE_OCTETS:
1471 if (strncasecmp(value, "0x", 2) == 0) {
1475 #ifdef WITH_ASCEND_BINARY
1480 vp->length = size >> 1;
1481 us = talloc_array(vp, uint8_t, vp->length);
1486 if ((size & 0x01) != 0) {
1487 fr_strerror_printf("Hex string is not an even length string");
1491 if (fr_hex2bin(us, cp, vp->length) != vp->length) {
1492 fr_strerror_printf("Invalid hex data");
1497 pairmemcpy(vp, (const uint8_t *) value, strlen(value));
1502 if (ifid_aton(value, (void *) &vp->vp_ifid) == NULL) {
1503 fr_strerror_printf("Failed to parse interface-id string \"%s\"", value);
1509 case PW_TYPE_IPV6ADDR:
1513 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
1516 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1518 fr_strerror_printf("failed to parse IPv6 address "
1519 "string \"%s\": %s", value, buffer);
1522 vp->vp_ipv6addr = ipaddr.ipaddr.ip6addr;
1523 vp->length = 16; /* length of IPv6 address */
1527 case PW_TYPE_IPV6PREFIX:
1528 p = strchr(value, '/');
1529 if (!p || ((p - value) >= 256)) {
1530 fr_strerror_printf("invalid IPv6 prefix string \"%s\"", value);
1533 unsigned int prefix;
1534 char buffer[256], *eptr;
1536 memcpy(buffer, value, p - value);
1537 buffer[p - value] = '\0';
1539 if (inet_pton(AF_INET6, buffer, vp->vp_ipv6prefix + 2) <= 0) {
1540 fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1544 prefix = strtoul(p + 1, &eptr, 10);
1545 if ((prefix > 128) || *eptr) {
1546 fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1549 vp->vp_ipv6prefix[1] = prefix;
1552 struct in6_addr addr;
1554 addr = fr_ipaddr_mask6((struct in6_addr *)(&vp->vp_ipv6prefix[2]), prefix);
1555 memcpy(vp->vp_ipv6prefix + 2, &addr, sizeof(addr));
1558 vp->length = 16 + 2;
1561 case PW_TYPE_IPV4PREFIX:
1562 p = strchr(value, '/');
1565 * 192.0.2.2 is parsed as if it was /32
1568 vp->vp_ipv4prefix[1] = 32;
1570 if (inet_pton(AF_INET, value, vp->vp_ipv4prefix + 2) <= 0) {
1571 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1574 vp->length = sizeof(vp->vp_ipv4prefix);
1579 * Otherwise parse the prefix
1581 if ((p - value) >= 256) {
1582 fr_strerror_printf("invalid IPv4 prefix string \"%s\"", value);
1585 unsigned int prefix;
1586 char buffer[256], *eptr;
1588 memcpy(buffer, value, p - value);
1589 buffer[p - value] = '\0';
1591 if (inet_pton(AF_INET, buffer, vp->vp_ipv4prefix + 2) <= 0) {
1592 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1596 prefix = strtoul(p + 1, &eptr, 10);
1597 if ((prefix > 32) || *eptr) {
1598 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1601 vp->vp_ipv4prefix[1] = prefix;
1604 struct in_addr addr;
1606 addr = fr_ipaddr_mask((struct in_addr *)(&vp->vp_ipv4prefix[2]), prefix);
1607 memcpy(vp->vp_ipv4prefix + 2, &addr, sizeof(addr));
1610 vp->length = sizeof(vp->vp_ipv4prefix);
1613 case PW_TYPE_ETHERNET:
1615 char const *c1, *c2;
1618 * Convert things which are obviously integers to Ethernet addresses
1620 * We assume the number is the bigendian representation of the
1623 if (fr_integer_check(value)) {
1624 uint64_t integer = htonll(atoll(value));
1626 memcpy(&vp->vp_ether, &integer, sizeof(vp->vp_ether));
1635 c2 = memchr(hextab, tolower((int) cp[0]), 16);
1637 } else if ((cp[1] != '\0') &&
1640 c1 = memchr(hextab, tolower((int) cp[0]), 16);
1641 c2 = memchr(hextab, tolower((int) cp[1]), 16);
1643 if (*cp == ':') cp++;
1647 if (!c1 || !c2 || (length >= sizeof(vp->vp_ether))) {
1648 fr_strerror_printf("failed to parse Ethernet address \"%s\"", value);
1651 vp->vp_ether[length] = ((c1-hextab)<<4) + (c2-hextab);
1659 * Crazy polymorphic (IPv4/IPv6) attribute type for WiMAX.
1661 * We try and make is saner by replacing the original
1662 * da, with either an IPv4 or IPv6 da type.
1664 * These are not dynamic da, and will have the same vendor
1665 * and attribute as the original.
1667 case PW_TYPE_COMBO_IP:
1669 DICT_ATTR const *da;
1671 if (inet_pton(AF_INET6, value, &vp->vp_ipv6addr) > 0) {
1672 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1675 fr_strerror_printf("Cannot find ipv6addr for %s", vp->da->name);
1679 vp->length = 16; /* length of IPv6 address */
1683 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1686 fr_strerror_printf("Cannot find ipaddr for %s", vp->da->name);
1690 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
1691 fr_strerror_printf("Failed to find IPv4 address for %s", value);
1695 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1703 case PW_TYPE_SIGNED: /* Damned code for 1 WiMAX attribute */
1704 vp->vp_signed = (int32_t) strtol(value, &p, 10);
1708 case PW_TYPE_TLV: /* don't use this! */
1709 if (strncasecmp(value, "0x", 2) != 0) {
1710 fr_strerror_printf("Invalid TLV specification");
1713 length = strlen(value + 2) / 2;
1714 if (vp->length < length) {
1715 TALLOC_FREE(vp->vp_tlv);
1717 vp->vp_tlv = talloc_array(vp, uint8_t, length);
1719 fr_strerror_printf("No memory");
1722 if (fr_hex2bin(vp->vp_tlv, value + 2, length) != length) {
1723 fr_strerror_printf("Invalid hex data in TLV");
1726 vp->length = length;
1733 fr_strerror_printf("unknown attribute type %d", vp->da->type);
1742 /** Use simple heuristics to create an VALUE_PAIR from an unknown address string
1744 * If a DICT_ATTR is not provided for the address type, parsing will fail with
1747 * @param ctx to allocate VP in.
1748 * @param value IPv4/IPv6 address/prefix string.
1749 * @param ipv4 dictionary attribute to use for an IPv4 address.
1750 * @param ipv6 dictionary attribute to use for an IPv6 address.
1751 * @param ipv4_prefix dictionary attribute to use for an IPv4 prefix.
1752 * @param ipv6_prefix dictionary attribute to use for an IPv6 prefix.
1753 * @return NULL on error, or new VALUE_PAIR.
1755 VALUE_PAIR *pairmake_ip(TALLOC_CTX *ctx, char const *value, DICT_ATTR *ipv4, DICT_ATTR *ipv6,
1756 DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix)
1759 DICT_ATTR *da = NULL;
1761 if (!fr_assert(ipv4 || ipv6 || ipv4_prefix || ipv6_prefix)) {
1765 /* No point in repeating the work of pairparsevalue */
1766 if (strchr(value, ':')) {
1767 if (strchr(value, '/')) {
1776 if (strchr(value, '/')) {
1786 fr_strerror_printf("Invalid IP value specified, allowed types are %s%s%s%s",
1787 ipv4 ? "ipaddr " : "", ipv6 ? "ipv6addr " : "",
1788 ipv4_prefix ? "ipv4prefix " : "", ipv6_prefix ? "ipv6prefix" : "");
1791 vp = pairalloc(ctx, da);
1792 if (!vp) return NULL;
1793 if (!pairparsevalue(vp, value)) {
1802 /** Create a valuepair from an ASCII attribute and value
1804 * Where the attribute name is in the form:
1806 * - Attr-%d.%d.%d...
1807 * - Vendor-%d-Attr-%d
1808 * - VendorName-Attr-%d
1810 * @param ctx for talloc
1811 * @param attribute name to parse.
1812 * @param value to parse (must be a hex string).
1813 * @param op to assign to new valuepair.
1814 * @return new valuepair or NULL on error.
1816 static VALUE_PAIR *pairmake_any(TALLOC_CTX *ctx,
1817 char const *attribute, char const *value,
1821 DICT_ATTR const *da;
1826 da = dict_attrunknownbyname(attribute, true);
1827 if (!da) return NULL;
1830 * Unknown attributes MUST be of type 'octets'
1832 if (value && (strncasecmp(value, "0x", 2) != 0)) {
1833 fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
1834 "string, not \"%s\"", attribute, value);
1836 dict_attr_free(&da);
1841 * We've now parsed the attribute properly, Let's create
1842 * it. This next stop also looks the attribute up in the
1843 * dictionary, and creates the appropriate type for it.
1845 vp = pairalloc(ctx, da);
1847 dict_attr_free(&da);
1851 vp->op = (op == 0) ? T_OP_EQ : op;
1853 if (!value) return vp;
1855 size = strlen(value + 2);
1856 vp->length = size >> 1;
1857 data = talloc_array(vp, uint8_t, vp->length);
1859 if (fr_hex2bin(data, value + 2, size) != vp->length) {
1860 fr_strerror_printf("Invalid hex string");
1865 vp->vp_octets = data;
1871 /** Create a VALUE_PAIR from ASCII strings
1873 * Converts an attribute string identifier (with an optional tag qualifier)
1874 * and value string into a VALUE_PAIR.
1876 * The string value is parsed according to the type of VALUE_PAIR being created.
1878 * @param[in] ctx for talloc
1879 * @param[in] vps list where the attribute will be added (optional)
1880 * @param[in] attribute name.
1881 * @param[in] value attribute value (may be NULL if value will be set later).
1882 * @param[in] op to assign to new VALUE_PAIR.
1883 * @return a new VALUE_PAIR.
1885 VALUE_PAIR *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps,
1886 char const *attribute, char const *value, FR_TOKEN op)
1888 DICT_ATTR const *da;
1894 char const *attrname = attribute;
1897 * Check for tags in 'Attribute:Tag' format.
1902 ts = strrchr(attribute, ':');
1904 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1909 strlcpy(buffer, attribute, sizeof(buffer));
1911 ts = strrchr(attrname, ':');
1912 if (!ts) return NULL;
1914 /* Colon found with something behind it */
1915 if (ts[1] == '*' && ts[2] == 0) {
1916 /* Wildcard tag for check items */
1919 } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1920 /* It's not a wild card tag */
1921 tag = strtol(ts + 1, &tc, 0);
1922 if (tc && !*tc && TAG_VALID_ZERO(tag))
1926 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1933 * It's not found in the dictionary, so we use
1934 * another method to create the attribute.
1936 da = dict_attrbyname(attrname);
1938 vp = pairmake_any(ctx, attrname, value, op);
1939 if (vp && vps) pairadd(vps, vp);
1943 /* Check for a tag in the 'Merit' format of:
1944 * :Tag:Value. Print an error if we already found
1945 * a tag in the Attribute.
1948 if (value && (*value == ':' && da->flags.has_tag)) {
1949 /* If we already found a tag, this is invalid */
1951 fr_strerror_printf("Duplicate tag %s for attribute %s",
1953 DEBUG("Duplicate tag %s for attribute %s\n",
1957 /* Colon found and attribute allows a tag */
1958 if (value[1] == '*' && value[2] == ':') {
1959 /* Wildcard tag for check items */
1964 tag = strtol(value + 1, &tc, 0);
1965 if (tc && *tc==':' && TAG_VALID_ZERO(tag))
1971 vp = pairalloc(ctx, da);
1972 if (!vp) return NULL;
1973 vp->op = (op == 0) ? T_OP_EQ : op;
1981 case T_OP_CMP_FALSE:
1982 vp->vp_strvalue = NULL;
1984 value = NULL; /* ignore it! */
1988 * Regular expression comparison of integer attributes
1989 * does a STRING comparison of the names of their
1990 * integer attributes.
1992 case T_OP_REG_EQ: /* =~ */
1993 case T_OP_REG_NE: /* !~ */
1995 fr_strerror_printf("Regular expressions are not supported");
2001 * Someone else will fill in the value.
2011 compare = regcomp(®, value, REG_EXTENDED);
2013 regerror(compare, ®, buffer, sizeof(buffer));
2014 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
2020 vp = pairmake(ctx, NULL, attribute, NULL, op);
2021 if (!vp) return NULL;
2023 if (pairmark_xlat(vp, value) < 0) {
2028 value = NULL; /* ignore it */
2034 * FIXME: if (strcasecmp(attribute, vp->da->name) != 0)
2035 * then the user MAY have typed in the attribute name
2036 * as Vendor-%d-Attr-%d, and the value MAY be octets.
2038 * We probably want to fix pairparsevalue to accept
2039 * octets as values for any attribute.
2041 if (value && !pairparsevalue(vp, value)) {
2046 if (vps) pairadd(vps, vp);
2050 /** Mark a valuepair for xlat expansion
2052 * Copies xlat source (unprocessed) string to valuepair value,
2053 * and sets value type.
2055 * @param vp to mark for expansion.
2056 * @param value to expand.
2057 * @return 0 if marking succeeded or -1 if vp already had a value, or OOM.
2059 int pairmark_xlat(VALUE_PAIR *vp, char const *value)
2064 * valuepair should not already have a value.
2066 if (vp->type != VT_NONE) {
2070 raw = talloc_typed_strdup(vp, value);
2076 vp->value.xlat = raw;
2082 /** Read a single valuepair from a buffer, and advance the pointer
2084 * Sets *eol to T_EOL if end of line was encountered.
2086 * @param[in,out] ptr to read from and update.
2087 * @param[out] raw The struct to write the raw VALUE_PAIR to.
2088 * @return the last token read.
2090 FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw)
2094 FR_TOKEN ret = T_OP_INVALID, next, quote;
2097 if (!ptr || !*ptr || !raw) {
2098 fr_strerror_printf("Invalid arguments");
2099 return T_OP_INVALID;
2103 * Skip leading spaces
2106 while ((*p == ' ') || (*p == '\t')) p++;
2109 fr_strerror_printf("No token read where we expected "
2110 "an attribute name");
2111 return T_OP_INVALID;
2115 fr_strerror_printf("Read a comment instead of a token");
2121 * Try to get the attribute name.
2126 uint8_t const *t = (uint8_t const *) p;
2128 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2130 fr_strerror_printf("Attribute name too long");
2131 return T_OP_INVALID;
2135 * Only ASCII is allowed, and only a subset of that.
2137 if ((*t < 32) || (*t >= 128)) {
2139 fr_strerror_printf("Invalid attribute name");
2140 return T_OP_INVALID;
2144 * This is arguably easier than trying to figure
2145 * out which operators come after the attribute
2146 * name. Yes, our "lexer" is bad.
2148 if (!dict_attr_allowed_chars[(int) *t]) {
2156 * ASCII, but not a valid attribute name.
2158 if (!*raw->l_opand) goto invalid;
2161 * Look for tag (:#). This is different from :=, which
2164 if ((*p == ':') && (isdigit((int) p[1]))) {
2165 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2170 while (isdigit((int) *p)) {
2171 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2181 /* Now we should have an operator here. */
2182 raw->op = gettoken(ptr, buf, sizeof(buf));
2183 if (raw->op < T_EQSTART || raw->op > T_EQEND) {
2184 fr_strerror_printf("Expecting operator");
2186 return T_OP_INVALID;
2190 * Read value. Note that empty string values are allowed
2192 quote = gettoken(ptr, raw->r_opand, sizeof(raw->r_opand));
2193 if (quote == T_EOL) {
2194 fr_strerror_printf("Failed to get value");
2196 return T_OP_INVALID;
2200 * Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
2204 next = gettoken(&p, buf, sizeof(buf));
2215 fr_strerror_printf("Expected end of line or comma");
2216 return T_OP_INVALID;
2224 case T_DOUBLE_QUOTED_STRING:
2226 * Only report as double quoted if it contained valid
2227 * a valid xlat expansion.
2229 p = strchr(raw->r_opand, '%');
2230 if (p && (p[1] == '{')) {
2233 raw->quote = T_SINGLE_QUOTED_STRING;
2246 /** Read one line of attribute/value pairs into a list.
2248 * The line may specify multiple attributes separated by commas.
2250 * @note If the function returns T_OP_INVALID, an error has occurred and
2251 * @note the valuepair list should probably be freed.
2253 * @param ctx for talloc
2254 * @param buffer to read valuepairs from.
2255 * @param list where the parsed VALUE_PAIRs will be appended.
2256 * @return the last token parsed, or T_OP_INVALID
2258 FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **list)
2260 VALUE_PAIR *vp, *head, **tail;
2262 FR_TOKEN last_token = T_OP_INVALID;
2263 FR_TOKEN previous_token;
2267 * We allow an empty line.
2269 if (buffer[0] == 0) {
2278 raw.l_opand[0] = '\0';
2279 raw.r_opand[0] = '\0';
2281 previous_token = last_token;
2283 last_token = pairread(&p, &raw);
2284 if (last_token == T_OP_INVALID) break;
2286 if (raw.quote == T_DOUBLE_QUOTED_STRING) {
2287 vp = pairmake(ctx, NULL, raw.l_opand, NULL, raw.op);
2289 last_token = T_OP_INVALID;
2292 if (pairmark_xlat(vp, raw.r_opand) < 0) {
2294 last_token = T_OP_INVALID;
2298 vp = pairmake(ctx, NULL, raw.l_opand, raw.r_opand, raw.op);
2300 last_token = T_OP_INVALID;
2306 tail = &((*tail)->next);
2307 } while (*p && (last_token == T_COMMA));
2310 * Don't tell the caller that there was a comment.
2312 if (last_token == T_HASH) {
2313 last_token = previous_token;
2316 if (last_token == T_OP_INVALID) {
2319 pairadd(list, head);
2323 * And return the last token which we read.
2329 * Read valuepairs from the fp up to End-Of-File.
2331 int readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone)
2334 FR_TOKEN last_token = T_EOL;
2338 VALUE_PAIR *vp = NULL;
2340 fr_cursor_init(&cursor, out);
2342 while (fgets(buf, sizeof(buf), fp) != NULL) {
2344 * If we get a '\n' by itself, we assume that's
2345 * the end of that VP
2347 if (buf[0] == '\n') {
2353 * Comments get ignored
2355 if (buf[0] == '#') continue;
2358 * Read all of the attributes on the current line.
2361 last_token = userparse(ctx, buf, &vp);
2363 if (last_token != T_EOL) goto error;
2367 fr_cursor_insert(&cursor, vp);
2376 vp = fr_cursor_first(&cursor);
2377 if (vp) pairfree(&vp);
2382 /** Compare two attribute values
2384 * @param[in] one the first attribute.
2385 * @param[in] two the second attribute.
2386 * @return -1 if one is less than two, 0 if both are equal, 1 if one is more than two, < -1 on error.
2388 int8_t paircmp_value(VALUE_PAIR const *one, VALUE_PAIR const *two)
2390 int64_t compare = 0;
2395 if (one->da->type != two->da->type) {
2396 fr_strerror_printf("Can't compare attribute values of different types");
2401 * After doing the previous check for special comparisons,
2402 * do the per-type comparison here.
2404 switch (one->da->type) {
2405 case PW_TYPE_ABINARY:
2406 case PW_TYPE_OCTETS:
2410 if (one->length > two->length) {
2411 length = one->length;
2413 length = two->length;
2417 compare = memcmp(one->vp_octets, two->vp_octets, length);
2418 if (compare != 0) break;
2422 * Contents are the same. The return code
2423 * is therefore the difference in lengths.
2425 * i.e. "0x00" is smaller than "0x0000"
2427 compare = one->length - two->length;
2431 case PW_TYPE_STRING:
2432 fr_assert(one->vp_strvalue);
2433 fr_assert(two->vp_strvalue);
2434 compare = strcmp(one->vp_strvalue, two->vp_strvalue);
2439 case PW_TYPE_INTEGER:
2441 compare = (int64_t) one->vp_integer - (int64_t) two->vp_integer;
2444 case PW_TYPE_SIGNED:
2445 compare = one->vp_signed - two->vp_signed;
2448 case PW_TYPE_INTEGER64:
2450 * Don't want integer overflow!
2452 if (one->vp_integer64 < two->vp_integer64) {
2454 } else if (one->vp_integer64 > two->vp_integer64) {
2459 case PW_TYPE_ETHERNET:
2460 compare = memcmp(&one->vp_ether, &two->vp_ether, sizeof(one->vp_ether));
2463 case PW_TYPE_IPADDR:
2464 compare = (int64_t) ntohl(one->vp_ipaddr) - (int64_t) ntohl(two->vp_ipaddr);
2467 case PW_TYPE_IPV6ADDR:
2468 compare = memcmp(&one->vp_ipv6addr, &two->vp_ipv6addr, sizeof(one->vp_ipv6addr));
2471 case PW_TYPE_IPV6PREFIX:
2472 compare = memcmp(&one->vp_ipv6prefix, &two->vp_ipv6prefix, sizeof(one->vp_ipv6prefix));
2475 case PW_TYPE_IPV4PREFIX:
2476 compare = memcmp(&one->vp_ipv4prefix, &two->vp_ipv4prefix, sizeof(one->vp_ipv4prefix));
2480 compare = memcmp(&one->vp_ifid, &two->vp_ifid, sizeof(one->vp_ifid));
2484 * None of the types below should be in the REQUEST
2486 case PW_TYPE_COMBO_IP: /* This should of been converted into IPADDR/IPV6ADDR */
2488 case PW_TYPE_EXTENDED:
2489 case PW_TYPE_LONG_EXTENDED:
2492 case PW_TYPE_INVALID: /* We should never see these */
2494 fr_assert(0); /* unknown type */
2498 * Do NOT add a default here, as new types are added
2499 * static analysis will warn us they're not handled
2505 } else if (compare < 0) {
2512 * We leverage the fact that IPv4 and IPv6 prefixes both
2513 * have the same format:
2515 * reserved, prefix-len, data...
2517 static int paircmp_op_cidr(FR_TOKEN op, int bytes,
2518 uint8_t one_net, uint8_t const *one,
2519 uint8_t two_net, uint8_t const *two)
2525 * Handle the case of netmasks being identical.
2527 if (one_net == two_net) {
2530 compare = memcmp(one, two, bytes);
2533 * If they're identical return true for
2536 if ((compare == 0) &&
2537 ((op == T_OP_CMP_EQ) ||
2544 * Everything else returns false.
2546 * 10/8 == 24/8 --> false
2547 * 10/8 <= 24/8 --> false
2548 * 10/8 >= 24/8 --> false
2554 * Netmasks are different. That limits the
2555 * possible results, based on the operator.
2565 case T_OP_LT: /* 192/8 < 192.168/16 --> false */
2566 if (one_net < two_net) {
2572 case T_OP_GT: /* 192/16 > 192.168/8 --> false */
2573 if (one_net > two_net) {
2582 if (one_net < two_net) {
2589 * Do the check byte by byte. If the bytes are
2590 * identical, it MAY be a match. If they're different,
2591 * it is NOT a match.
2596 * All leading bytes are identical.
2598 if (common == 0) return true;
2601 * Doing bitmasks takes more work.
2603 if (common < 8) break;
2605 if (one[i] != two[i]) return false;
2613 mask <<= (8 - common);
2617 if ((one[i] & mask) == ((two[i] & mask))) {
2624 /** Compare two attributes using an operator
2626 * @param[in] a the first attribute
2627 * @param[in] op the operator for comparison.
2628 * @param[in] b the second attribute
2629 * @return 1 if true, 0 if false, -1 on error.
2631 int8_t paircmp_op(VALUE_PAIR const *a, FR_TOKEN op, VALUE_PAIR const *b)
2635 if (!a || !b) return -1;
2637 switch (a->da->type) {
2638 case PW_TYPE_IPADDR:
2639 switch (b->da->type) {
2640 case PW_TYPE_IPADDR: /* IPv4 and IPv4 */
2643 case PW_TYPE_IPV4PREFIX: /* IPv4 and IPv4 Prefix */
2644 return paircmp_op_cidr(op, 4, 32, (uint8_t const *) &a->vp_ipaddr,
2645 b->vp_ipv4prefix[1], (uint8_t const *) &b->vp_ipv4prefix + 2);
2648 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
2653 case PW_TYPE_IPV4PREFIX: /* IPv4 and IPv4 Prefix */
2654 switch (b->da->type) {
2655 case PW_TYPE_IPADDR:
2656 return paircmp_op_cidr(op, 4, a->vp_ipv4prefix[1],
2657 (uint8_t const *) &a->vp_ipv4prefix + 2,
2658 32, (uint8_t const *) &b->vp_ipaddr);
2660 case PW_TYPE_IPV4PREFIX: /* IPv4 Prefix and IPv4 Prefix */
2661 return paircmp_op_cidr(op, 4, a->vp_ipv4prefix[1],
2662 (uint8_t const *) &a->vp_ipv4prefix + 2,
2663 b->vp_ipv4prefix[1], (uint8_t const *) &b->vp_ipv4prefix + 2);
2666 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
2671 case PW_TYPE_IPV6ADDR:
2672 switch (b->da->type) {
2673 case PW_TYPE_IPV6ADDR: /* IPv6 and IPv6 */
2676 case PW_TYPE_IPV6PREFIX: /* IPv6 and IPv6 Preifx */
2677 return paircmp_op_cidr(op, 16, 128, (uint8_t const *) &a->vp_ipv6addr,
2678 b->vp_ipv6prefix[1], (uint8_t const *) &b->vp_ipv6prefix + 2);
2682 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
2687 case PW_TYPE_IPV6PREFIX:
2688 switch (b->da->type) {
2689 case PW_TYPE_IPV6ADDR: /* IPv6 Prefix and IPv6 */
2690 return paircmp_op_cidr(op, 16, a->vp_ipv6prefix[1],
2691 (uint8_t const *) &a->vp_ipv6prefix + 2,
2692 128, (uint8_t const *) &b->vp_ipv6addr);
2694 case PW_TYPE_IPV6PREFIX: /* IPv6 Prefix and IPv6 */
2695 return paircmp_op_cidr(op, 16, a->vp_ipv6prefix[1],
2696 (uint8_t const *) &a->vp_ipv6prefix + 2,
2697 b->vp_ipv6prefix[1], (uint8_t const *) &b->vp_ipv6prefix + 2);
2700 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
2707 compare = paircmp_value(a, b);
2708 if (compare < -1) { /* comparison error */
2714 * Now do the operator comparison.
2718 return (compare == 0);
2721 return (compare != 0);
2724 return (compare < 0);
2727 return (compare > 0);
2730 return (compare <= 0);
2733 return (compare >= 0);
2740 /** Compare two pairs, using the operator from "a"
2742 * i.e. given two attributes, it does:
2744 * (b->data) (a->operator) (a->data)
2746 * e.g. "foo" != "bar"
2748 * @param[in] a the first attribute
2749 * @param[in] b the second attribute
2750 * @return 1 if true, 0 if false, -1 on error.
2752 int8_t paircmp(VALUE_PAIR *a, VALUE_PAIR *b)
2757 if (b) VERIFY_VP(b);
2763 case T_OP_CMP_FALSE:
2767 * a is a regex, compile it, print b to a string,
2768 * and then do string comparisons.
2778 char buffer[MAX_STRING_LEN * 4 + 1];
2780 compare = regcomp(®, a->vp_strvalue, REG_EXTENDED);
2782 regerror(compare, ®, buffer, sizeof(buffer));
2783 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
2784 a->da->name, buffer);
2790 vp_prints_value(buffer, sizeof(buffer), b, 0);
2793 * Don't care about substring matches,
2796 compare = regexec(®, buffer, 0, NULL, 0);
2799 if (a->op == T_OP_REG_EQ) {
2800 return (compare == 0);
2803 return (compare != 0);
2807 default: /* we're OK */
2811 return paircmp_op(b, a->op, a);
2814 /** Determine equality of two lists
2816 * This is useful for comparing lists of attributes inserted into a binary tree.
2818 * @param a first list of VALUE_PAIRs.
2819 * @param b second list of VALUE_PAIRs.
2820 * @return -1 if a < b, 0 if the two lists are equal, 1 if a > b, -2 on error.
2822 int8_t pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b)
2824 vp_cursor_t a_cursor, b_cursor;
2825 VALUE_PAIR *a_p, *b_p;
2828 for (a_p = fr_cursor_init(&a_cursor, &a), b_p = fr_cursor_init(&b_cursor, &b);
2830 a_p = fr_cursor_next(&a_cursor), b_p = fr_cursor_next(&b_cursor)) {
2831 /* Same VP, no point doing expensive checks */
2836 if (a_p->da < b_p->da) {
2839 if (a_p->da > b_p->da) {
2843 if (a_p->tag < b_p->tag) {
2846 if (a_p->tag > b_p->tag) {
2850 ret = paircmp_value(a_p, b_p);
2852 fr_assert(ret >= -1); /* Comparison error */
2869 /** Set the type of the VALUE_PAIR value buffer to match it's DICT_ATTR
2871 * @param vp to fixup.
2873 static void pairtypeset(VALUE_PAIR *vp)
2875 if (!vp->data.ptr) return;
2877 switch(vp->da->type) {
2878 case PW_TYPE_OCTETS:
2880 talloc_set_type(vp->data.ptr, uint8_t);
2883 case PW_TYPE_STRING:
2884 talloc_set_type(vp->data.ptr, char);
2892 /** Copy data into an "octets" data type.
2894 * @param[in,out] vp to update
2895 * @param[in] src data to copy
2896 * @param[in] size of the data
2898 void pairmemcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
2904 p = talloc_memdup(vp, src, size);
2906 talloc_set_type(p, uint8_t);
2908 memcpy(&q, &vp->vp_octets, sizeof(q));
2916 /** Reparent an allocated octet buffer to a VALUE_PAIR
2918 * @param[in,out] vp to update
2919 * @param[in] src buffer to steal.
2921 void pairmemsteal(VALUE_PAIR *vp, uint8_t const *src)
2927 memcpy(&q, &vp->vp_octets, sizeof(q));
2930 vp->vp_octets = talloc_steal(vp, src);
2932 vp->length = talloc_array_length(vp->vp_strvalue);
2936 /** Reparent an allocated char buffer to a VALUE_PAIR
2938 * @param[in,out] vp to update
2939 * @param[in] src buffer to steal.
2941 void pairstrsteal(VALUE_PAIR *vp, char const *src)
2947 memcpy(&q, &vp->vp_octets, sizeof(q));
2950 vp->vp_strvalue = talloc_steal(vp, src);
2952 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2956 /** Copy data into an "string" data type.
2958 * @param[in,out] vp to update
2959 * @param[in] src data to copy
2961 void pairstrcpy(VALUE_PAIR *vp, char const *src)
2967 p = talloc_strdup(vp, src);
2971 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2974 vp->vp_strvalue = p;
2976 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2981 /** Print data into an "string" data type.
2983 * @param[in,out] vp to update
2984 * @param[in] fmt the format string
2986 void pairsprintf(VALUE_PAIR *vp, char const *fmt, ...)
2994 p = talloc_vasprintf(vp, fmt, ap);
2999 memcpy(&q, &vp->vp_strvalue, sizeof(q));
3002 vp->vp_strvalue = p;
3005 vp->length = talloc_array_length(vp->vp_strvalue) - 1;