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)
233 for (i = fr_cursor_init(&cursor, &vp);
235 i = fr_cursor_next(&cursor)) {
237 if ((i->da->attr == attr) && (i->da->vendor == vendor) && \
238 (!i->da->flags.has_tag || (tag == TAG_ANY) || (i->tag == tag))) {
246 /** Delete matching pairs
248 * Delete matching pairs from the attribute list.
250 * @param[in,out] first VP in list.
251 * @param[in] attr to match.
252 * @param[in] vendor to match.
253 * @param[in] tag to match. TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
255 * @todo should take DAs and do a point comparison.
257 void pairdelete(VALUE_PAIR **first, unsigned int attr, unsigned int vendor,
260 VALUE_PAIR *i, *next;
261 VALUE_PAIR **last = first;
263 for(i = *first; i; i = next) {
266 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
268 (i->da->flags.has_tag && (i->tag == tag)))) {
277 /** Add a VP to the end of the list.
279 * Locates the end of 'first', and links an additional VP 'add' at the end.
281 * @param[in] first VP in linked list. Will add new VP to the end of this list.
282 * @param[in] add VP to add to list.
284 void pairadd(VALUE_PAIR **first, VALUE_PAIR *add)
292 if (*first == NULL) {
296 for(i = *first; i->next; i = i->next)
301 /** Replace all matching VPs
303 * Walks over 'first', and replaces the first VP that matches 'replace'.
305 * @note Memory used by the VP being replaced will be freed.
306 * @note Will not work with unknown attributes.
308 * @param[in,out] first VP in linked list. Will search and replace in this list.
309 * @param[in] replace VP to replace.
311 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *replace)
313 VALUE_PAIR *i, *next;
314 VALUE_PAIR **prev = first;
318 if (*first == NULL) {
324 * Not an empty list, so find item if it is there, and
325 * replace it. Note, we always replace the first one, and
326 * we ignore any others that might exist.
328 for(i = *first; i; i = next) {
333 * Found the first attribute, replace it,
336 if ((i->da == replace->da) &&
337 (!i->da->flags.has_tag || (i->tag == replace->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 attrcmp(VALUE_PAIR const *a, VALUE_PAIR const *b)
371 if (a->da == b->da) {
378 int8_t attrtagcmp(VALUE_PAIR const *a, VALUE_PAIR const *b)
387 if (cmp != 0) return cmp;
389 if (a->tag < b->tag) {
393 if (a->tag > b->tag) {
400 static void pairsort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
406 * Stopping condition - no more elements left to split
408 if (!source || !source->next) {
416 * Fast advances twice as fast as slow, so when it gets to the end,
417 * slow will point to the middle of the linked list.
435 static VALUE_PAIR *pairsort_merge(VALUE_PAIR *a, VALUE_PAIR *b, fr_pair_cmp_t cmp)
437 VALUE_PAIR *result = NULL;
443 * Compare the DICT_ATTRs and tags
445 if (cmp(a, b) <= 0) {
447 result->next = pairsort_merge(a->next, b, cmp);
450 result->next = pairsort_merge(a, b->next, cmp);
456 /** Sort a linked list of VALUE_PAIRs using merge sort
458 * @param[in,out] vps List of VALUE_PAIRs to sort.
459 * @param[in] cmp to sort with
461 void pairsort(VALUE_PAIR **vps, fr_pair_cmp_t cmp)
463 VALUE_PAIR *head = *vps;
468 * If there's 0-1 elements it must already be sorted.
470 if (!head || !head->next) {
474 pairsort_split(head, &a, &b); /* Split into sublists */
475 pairsort(&a, cmp); /* Traverse left */
476 pairsort(&b, cmp); /* Traverse right */
479 * merge the two sorted lists together
481 *vps = pairsort_merge(a, b, cmp);
484 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
486 * @param filter attributes to check list against.
487 * @param list attributes, probably a request or reply
489 bool pairvalidate(VALUE_PAIR *filter, VALUE_PAIR *list)
491 vp_cursor_t filter_cursor;
492 vp_cursor_t list_cursor;
494 VALUE_PAIR *check, *match;
496 if (!filter && !list) {
501 * This allows us to verify the sets of validate and reply are equal
502 * i.e. we have a validate rule which matches every reply attribute.
504 * @todo this should be removed one we have sets and lists
506 pairsort(&filter, attrtagcmp);
507 pairsort(&list, attrtagcmp);
509 match = fr_cursor_init(&list_cursor, &list);
510 check = fr_cursor_init(&filter_cursor, &filter);
514 * The lists are sorted, so if the first
515 * attributes aren't of the same type, then we're
518 if (!attribute_eq(check, match)) {
523 * They're of the same type, but don't have the
524 * same values. This is a problem.
526 * Note that the RFCs say that for attributes of
527 * the same type, order is important.
529 if (!paircmp(check, match)) {
533 match = fr_cursor_next(&list_cursor);
534 check = fr_cursor_next(&filter_cursor);
536 if (!match && !check) break;
539 * One list ended earlier than the others, they
542 if (!match || !check) {
550 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
552 * @param filter attributes to check list against.
553 * @param list attributes, probably a request or reply
555 bool pairvalidate_relaxed(VALUE_PAIR *filter, VALUE_PAIR *list)
557 vp_cursor_t filter_cursor;
558 vp_cursor_t list_cursor;
560 VALUE_PAIR *check, *match, *last_check = NULL, *last_match;
562 if (!filter && !list) {
567 * This allows us to verify the sets of validate and reply are equal
568 * i.e. we have a validate rule which matches every reply attribute.
570 * @todo this should be removed one we have sets and lists
572 pairsort(&filter, attrtagcmp);
573 pairsort(&list, attrtagcmp);
575 fr_cursor_init(&list_cursor, &list);
576 for (check = fr_cursor_init(&filter_cursor, &filter);
578 check = fr_cursor_next(&filter_cursor)) {
580 * Were processing check attributes of a new type.
582 if (!attribute_eq(last_check, check)) {
584 * Record the start of the matching attributes in the pair list
585 * For every other operator we require the match to be present
587 last_match = fr_cursor_next_by_da(&list_cursor, check->da, check->tag);
589 if (check->op == T_OP_CMP_FALSE) {
595 fr_cursor_init(&list_cursor, &last_match);
600 * Now iterate over all attributes of the same type.
602 for (match = fr_cursor_first(&list_cursor);
603 attribute_eq(match, check);
604 match = fr_cursor_next(&list_cursor)) {
606 * This attribute passed the filter
608 if (!paircmp(check, match)) {
617 /** Copy a single valuepair
619 * Allocate a new valuepair and copy the da from the old vp.
621 * @param[in] ctx for talloc
622 * @param[in] vp to copy.
623 * @return a copy of the input VP or NULL on error.
625 VALUE_PAIR *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
629 if (!vp) return NULL;
633 n = pairalloc(ctx, vp->da);
636 memcpy(n, vp, sizeof(*n));
641 if (vp->type == VT_XLAT) {
642 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
645 n->da = dict_attr_copy(vp->da, true);
653 if (n->data.ptr) switch (n->da->type) {
656 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
657 talloc_set_type(n->vp_octets, uint8_t);
661 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1); /* NULL byte */
662 talloc_set_type(n->vp_strvalue, char);
672 /** Copy data from one VP to another
674 * Allocate a new pair using da, and copy over the value from the specified
677 * @todo Should be able to do type conversions.
679 * @param[in] ctx for talloc
680 * @param[in] da of new attribute to alloc.
681 * @param[in] vp to copy data from.
682 * @return the new valuepair.
684 VALUE_PAIR *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp)
688 if (!vp) return NULL;
693 * The types have to be identical, OR the "from" VP has
696 if (da->type != vp->da->type) {
699 VALUE_PAIR const **pvp;
701 if (vp->da->type == PW_TYPE_OCTETS) {
703 * Decode the data. It may be wrong!
705 if (rad_data2vp(da->attr, da->vendor, vp->vp_octets, vp->length, &n) < 0) {
714 * Else the destination type is octets
716 switch (vp->da->type) {
718 return NULL; /* can't do it */
720 case PW_TYPE_INTEGER:
724 case PW_TYPE_IPV6ADDR:
725 case PW_TYPE_IPV6PREFIX:
728 case PW_TYPE_ETHERNET:
730 case PW_TYPE_INTEGER64:
731 case PW_TYPE_IPV4PREFIX:
735 n = pairalloc(ctx, da);
738 p = talloc_array(n, uint8_t, dict_attr_sizes[vp->da->type][1] + 2);
741 length = rad_vp2attr(NULL, NULL, NULL, pvp, p, dict_attr_sizes[vp->da->type][1]);
747 pairmemcpy(n, p + 2, length - 2);
752 n = pairalloc(ctx, da);
755 memcpy(n, vp, sizeof(*n));
758 if (n->type == VT_XLAT) {
759 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
762 if (n->data.ptr) switch (n->da->type) {
765 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
766 talloc_set_type(n->vp_octets, uint8_t);
770 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1); /* NULL byte */
771 talloc_set_type(n->vp_strvalue, char);
786 * Copy all pairs from 'from' regardless of tag, attribute or vendor.
788 * @param[in] ctx for new VALUE_PAIRs to be allocated in.
789 * @param[in] from whence to copy VALUE_PAIRs.
790 * @return the head of the new VALUE_PAIR list or NULL on error.
792 VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from)
794 vp_cursor_t src, dst;
796 VALUE_PAIR *out = NULL, *vp;
798 fr_cursor_init(&dst, &out);
799 for (vp = fr_cursor_init(&src, &from);
801 vp = fr_cursor_next(&src)) {
803 vp = paircopyvp(ctx, vp);
808 fr_cursor_insert(&dst, vp); /* paircopy sets next pointer to NULL */
814 /** Copy matching pairs
816 * Copy pairs of a matching attribute number, vendor number and tag from the
817 * the input list to a new list, and returns the head of this list.
819 * @param[in] ctx for talloc
820 * @param[in] from whence to copy VALUE_PAIRs.
821 * @param[in] attr to match, if 0 input list will not be filtered by attr.
822 * @param[in] vendor to match.
823 * @param[in] tag to match, TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
824 * @return the head of the new VALUE_PAIR list or NULL on error.
826 VALUE_PAIR *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from,
827 unsigned int attr, unsigned int vendor, int8_t tag)
829 vp_cursor_t src, dst;
831 VALUE_PAIR *out = NULL, *vp;
833 fr_cursor_init(&dst, &out);
834 for (vp = fr_cursor_init(&src, &from);
836 vp = fr_cursor_next(&src)) {
839 if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
843 if ((tag != TAG_ANY) && vp->da->flags.has_tag && (vp->tag != tag)) {
847 vp = paircopyvp(ctx, vp);
852 fr_cursor_insert(&dst, vp);
858 /** Steal all members of a VALUE_PAIR list
860 * @param[in] ctx to move VALUE_PAIRs into
861 * @param[in] from VALUE_PAIRs to move into the new context.
863 VALUE_PAIR *pairsteal(TALLOC_CTX *ctx, VALUE_PAIR *from)
868 for (vp = fr_cursor_init(&cursor, &from);
870 vp = fr_cursor_next(&cursor)) {
871 (void) talloc_steal(ctx, vp);
877 /** Move pairs from source list to destination list respecting operator
879 * @note This function does some additional magic that's probably not needed
880 * in most places. Consider using radius_pairmove in server code.
882 * @note pairfree should be called on the head of the source list to free
883 * unmoved attributes (if they're no longer needed).
885 * @note Does not respect tags when matching.
887 * @param[in] ctx for talloc
888 * @param[in,out] to destination list.
889 * @param[in,out] from source list.
891 * @see radius_pairmove
893 void pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from)
895 VALUE_PAIR *i, *found;
896 VALUE_PAIR *head_new, **tail_new;
897 VALUE_PAIR **tail_from;
899 if (!to || !from || !*from) return;
902 * We're editing the "to" list while we're adding new
903 * attributes to it. We don't want the new attributes to
904 * be edited, so we create an intermediate list to hold
905 * them during the editing process.
908 tail_new = &head_new;
911 * We're looping over the "from" list, moving some
912 * attributes out, but leaving others in place.
915 while ((i = *tail_from) != NULL) {
919 * We never move Fall-Through.
921 if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
922 tail_from = &(i->next);
927 * Unlike previous versions, we treat all other
928 * attributes as normal. i.e. there's no special
929 * treatment for passwords or Hint.
934 * Anything else are operators which
935 * shouldn't occur. We ignore them, and
936 * leave them in place.
939 tail_from = &(i->next);
943 * Add it to the "to" list, but only if
944 * it doesn't already exist.
947 found = pairfind(*to, i->da->attr, i->da->vendor,
949 if (!found) goto do_add;
951 tail_from = &(i->next);
955 * Add it to the "to" list, and delete any attribute
956 * of the same vendor/attr which already exists.
959 found = pairfind(*to, i->da->attr, i->da->vendor,
961 if (!found) goto do_add;
964 * Do NOT call pairdelete() here,
965 * due to issues with re-writing
966 * "request->username".
968 * Everybody calls pairmove, and
969 * expects it to work. We can't
970 * update request->username here,
971 * so instead we over-write the
972 * vp that it's pointing to.
974 switch (found->da->type) {
979 memcpy(found, i, sizeof(*found));
984 pairmemsteal(found, i->vp_tlv);
989 pairmemsteal(found, i->vp_octets);
994 pairstrsteal(found, i->vp_strvalue);
995 i->vp_strvalue = NULL;
1001 * Delete *all* of the attributes
1002 * of the same number.
1004 pairdelete(&found->next,
1006 found->da->vendor, TAG_ANY);
1009 * Remove this attribute from the
1012 *tail_from = i->next;
1018 * Move it from the old list and add it
1023 *tail_from = i->next;
1025 *tail_new = talloc_steal(ctx, i);
1026 tail_new = &(i->next);
1029 } /* loop over the "from" list. */
1032 * Take the "new" list, and append it to the "to" list.
1034 pairadd(to, head_new);
1037 /** Move matching pairs between VALUE_PAIR lists
1039 * Move pairs of a matching attribute number, vendor number and tag from the
1040 * the input list to the output list.
1042 * @note pairfree should be called on the head of the old list to free unmoved
1043 attributes (if they're no longer needed).
1045 * @param[in] ctx for talloc
1046 * @param[in,out] to destination list.
1047 * @param[in,out] from source list.
1048 * @param[in] attr to match, if PW_VENDOR_SPECIFIC and vendor 0, only VSAs will
1049 * be copied. If 0 and 0, all attributes will match
1050 * @param[in] vendor to match.
1051 * @param[in] tag to match, TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
1053 void pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from, unsigned int attr, unsigned int vendor, int8_t tag)
1055 VALUE_PAIR *to_tail, *i, *next;
1056 VALUE_PAIR *iprev = NULL;
1059 * Find the last pair in the "to" list and put it in "to_tail".
1061 * @todo: replace the "if" with "VALUE_PAIR **tail"
1065 for(i = *to; i; i = i->next) {
1073 * Attr/vendor of 0 means "move them all".
1074 * It's better than "pairadd(foo,bar);bar=NULL"
1076 if ((vendor == 0) && (attr == 0)) {
1078 to_tail->next = *from;
1083 for (i = *from; i; i = i->next) {
1084 (void) talloc_steal(ctx, i);
1091 for(i = *from; i; i = next) {
1095 if ((tag != TAG_ANY) && i->da->flags.has_tag &&
1101 * vendor=0, attr = PW_VENDOR_SPECIFIC means
1102 * "match any vendor attribute".
1104 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
1106 * It's a VSA: move it over.
1108 if (i->da->vendor != 0) goto move;
1111 * It's Vendor-Specific: move it over.
1113 if (i->da->attr == attr) goto move;
1116 * It's not a VSA: ignore it.
1123 * If it isn't an exact match, ignore it.
1125 if (!((i->da->vendor == vendor) && (i->da->attr == attr))) {
1132 * Remove the attribute from the "from" list.
1140 * Add the attribute to the "to" list.
1148 (void) talloc_steal(ctx, i);
1152 static char const *hextab = "0123456789abcdef";
1154 bool pairparsevalue(VALUE_PAIR *vp, char const *value)
1157 char const *cp, *cs;
1163 if (!value) return false;
1167 * It's a comparison, not a real VALUE_PAIR, copy the string over verbatim
1169 if ((vp->op == T_OP_REG_EQ) || (vp->op == T_OP_REG_NE)) {
1170 pairstrcpy(vp, value); /* Icky hacky ewww */
1174 switch(vp->da->type) {
1175 case PW_TYPE_STRING:
1179 p = talloc_typed_strdup(vp, value);
1180 vp->vp_strvalue = p;
1218 c = '\\'; /* no cp++ */
1221 if ((cp[0] >= '0') &&
1227 (sscanf(cp, "%3o", &x) == 1)) {
1236 } /* else at EOL \ --> \ */
1243 vp->length = length;
1246 case PW_TYPE_IPADDR:
1248 * FIXME: complain if hostname
1249 * cannot be resolved, or resolve later!
1257 * Convert things which are obviously integers to IP addresses
1259 * We assume the number is the bigendian representation of the
1262 if (fr_integer_check(value)) {
1263 vp->vp_ipaddr = htonl(atol(value));
1268 * Certain applications/databases print IPv4 addresses with a
1269 * /32 suffix. Strip it off if the mask is 32, else error out.
1271 p = strchr(value, '/');
1273 if ((p[1] != '3') || (p[2] != '2') || (p[3] != '\0')) {
1274 fr_strerror_printf("Invalid IP address suffix \"%s\". Only '/32' permitted "
1275 "for non-prefix types", p);
1279 strlcpy(ipv4, value, sizeof(ipv4));
1280 ipv4[p - value] = '\0';
1286 if (ip_hton(cs, AF_INET, &ipaddr) < 0) {
1287 fr_strerror_printf("Failed to find IP address for %s", cs);
1291 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1300 * Note that ALL integers are unsigned!
1302 vp->vp_integer = fr_strtoul(value, &p);
1304 if (vp->vp_integer > 255) {
1305 fr_strerror_printf("Byte value \"%s\" is larger than 255", value);
1310 if (fr_whitespace_check(p)) break;
1311 goto check_for_value;
1315 * Note that ALL integers are unsigned!
1317 vp->vp_integer = fr_strtoul(value, &p);
1320 if (vp->vp_integer > 65535) {
1321 fr_strerror_printf("Byte value \"%s\" is larger than 65535", value);
1326 if (fr_whitespace_check(p)) break;
1327 goto check_for_value;
1329 case PW_TYPE_INTEGER:
1331 * Note that ALL integers are unsigned!
1333 vp->vp_integer = fr_strtoul(value, &p);
1336 if (fr_whitespace_check(p)) break;
1340 * Look for the named value for the given
1343 if ((dval = dict_valbyname(vp->da->attr, vp->da->vendor, value)) == NULL) {
1344 fr_strerror_printf("Unknown value '%s' for attribute '%s'", value, vp->da->name);
1347 vp->vp_integer = dval->value;
1350 case PW_TYPE_INTEGER64:
1352 * Note that ALL integers are unsigned!
1354 if (sscanf(value, "%" PRIu64, &y) != 1) {
1355 fr_strerror_printf("Invalid value '%s' for attribute '%s'",
1356 value, vp->da->name);
1359 vp->vp_integer64 = y;
1361 length = strspn(value, "0123456789");
1362 if (fr_whitespace_check(value + length)) break;
1368 * time_t may be 64 bits, whule vp_date
1369 * MUST be 32-bits. We need an
1370 * intermediary variable to handle
1375 if (fr_get_time(value, &date) < 0) {
1376 fr_strerror_printf("failed to parse time string "
1386 case PW_TYPE_ABINARY:
1387 #ifdef WITH_ASCEND_BINARY
1388 if (strncasecmp(value, "0x", 2) == 0) {
1392 if (ascend_parse_filter(vp, value) < 0 ) {
1393 /* Allow ascend_parse_filter's strerror to bubble up */
1399 * If Ascend binary is NOT defined,
1400 * then fall through to raw octets, so that
1401 * the user can at least make them by hand...
1404 /* raw octets: 0x01020304... */
1406 if (strcmp(value, "ANY") == 0) {
1409 } /* else it's hex */
1411 case PW_TYPE_OCTETS:
1412 if (strncasecmp(value, "0x", 2) == 0) {
1416 #ifdef WITH_ASCEND_BINARY
1421 vp->length = size >> 1;
1422 us = talloc_array(vp, uint8_t, vp->length);
1427 if ((size & 0x01) != 0) {
1428 fr_strerror_printf("Hex string is not an even length string");
1432 if (fr_hex2bin(us, cp, vp->length) != vp->length) {
1433 fr_strerror_printf("Invalid hex data");
1438 pairmemcpy(vp, (const uint8_t *) value, strlen(value));
1443 if (ifid_aton(value, (void *) &vp->vp_ifid) == NULL) {
1444 fr_strerror_printf("Failed to parse interface-id string \"%s\"", value);
1450 case PW_TYPE_IPV6ADDR:
1454 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
1457 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1459 fr_strerror_printf("failed to parse IPv6 address "
1460 "string \"%s\": %s", value, buffer);
1463 vp->vp_ipv6addr = ipaddr.ipaddr.ip6addr;
1464 vp->length = 16; /* length of IPv6 address */
1468 case PW_TYPE_IPV6PREFIX:
1469 p = strchr(value, '/');
1470 if (!p || ((p - value) >= 256)) {
1471 fr_strerror_printf("invalid IPv6 prefix string \"%s\"", value);
1474 unsigned int prefix;
1475 char buffer[256], *eptr;
1477 memcpy(buffer, value, p - value);
1478 buffer[p - value] = '\0';
1480 if (inet_pton(AF_INET6, buffer, vp->vp_ipv6prefix + 2) <= 0) {
1481 fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1485 prefix = strtoul(p + 1, &eptr, 10);
1486 if ((prefix > 128) || *eptr) {
1487 fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1490 vp->vp_ipv6prefix[1] = prefix;
1493 struct in6_addr addr;
1495 addr = fr_ipaddr_mask6((struct in6_addr *)(&vp->vp_ipv6prefix[2]), prefix);
1496 memcpy(vp->vp_ipv6prefix + 2, &addr, sizeof(addr));
1499 vp->length = 16 + 2;
1502 case PW_TYPE_IPV4PREFIX:
1503 p = strchr(value, '/');
1506 * 192.0.2.2 is parsed as if it was /32
1509 vp->vp_ipv4prefix[1] = 32;
1511 if (inet_pton(AF_INET, value, vp->vp_ipv4prefix + 2) <= 0) {
1512 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1515 vp->length = sizeof(vp->vp_ipv4prefix);
1520 * Otherwise parse the prefix
1522 if ((p - value) >= 256) {
1523 fr_strerror_printf("invalid IPv4 prefix string \"%s\"", value);
1526 unsigned int prefix;
1527 char buffer[256], *eptr;
1529 memcpy(buffer, value, p - value);
1530 buffer[p - value] = '\0';
1532 if (inet_pton(AF_INET, buffer, vp->vp_ipv4prefix + 2) <= 0) {
1533 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1537 prefix = strtoul(p + 1, &eptr, 10);
1538 if ((prefix > 32) || *eptr) {
1539 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1542 vp->vp_ipv4prefix[1] = prefix;
1545 struct in_addr addr;
1547 addr = fr_ipaddr_mask((struct in_addr *)(&vp->vp_ipv4prefix[2]), prefix);
1548 memcpy(vp->vp_ipv4prefix + 2, &addr, sizeof(addr));
1551 vp->length = sizeof(vp->vp_ipv4prefix);
1554 case PW_TYPE_ETHERNET:
1556 char const *c1, *c2;
1559 * Convert things which are obviously integers to Ethernet addresses
1561 * We assume the number is the bigendian representation of the
1564 if (fr_integer_check(value)) {
1565 uint64_t integer = htonll(atoll(value));
1567 memcpy(&vp->vp_ether, &integer, sizeof(vp->vp_ether));
1576 c2 = memchr(hextab, tolower((int) cp[0]), 16);
1578 } else if ((cp[1] != '\0') &&
1581 c1 = memchr(hextab, tolower((int) cp[0]), 16);
1582 c2 = memchr(hextab, tolower((int) cp[1]), 16);
1584 if (*cp == ':') cp++;
1588 if (!c1 || !c2 || (length >= sizeof(vp->vp_ether))) {
1589 fr_strerror_printf("failed to parse Ethernet address \"%s\"", value);
1592 vp->vp_ether[length] = ((c1-hextab)<<4) + (c2-hextab);
1600 * Crazy polymorphic (IPv4/IPv6) attribute type for WiMAX.
1602 * We try and make is saner by replacing the original
1603 * da, with either an IPv4 or IPv6 da type.
1605 * These are not dynamic da, and will have the same vendor
1606 * and attribute as the original.
1608 case PW_TYPE_COMBO_IP:
1610 DICT_ATTR const *da;
1612 if (inet_pton(AF_INET6, value, &vp->vp_ipv6addr) > 0) {
1613 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1616 fr_strerror_printf("Cannot find ipv6addr for %s", vp->da->name);
1620 vp->length = 16; /* length of IPv6 address */
1624 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1627 fr_strerror_printf("Cannot find ipaddr for %s", vp->da->name);
1631 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
1632 fr_strerror_printf("Failed to find IPv4 address for %s", value);
1636 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1644 case PW_TYPE_SIGNED: /* Damned code for 1 WiMAX attribute */
1645 vp->vp_signed = (int32_t) strtol(value, &p, 10);
1649 case PW_TYPE_TLV: /* don't use this! */
1650 if (strncasecmp(value, "0x", 2) != 0) {
1651 fr_strerror_printf("Invalid TLV specification");
1654 length = strlen(value + 2) / 2;
1655 if (vp->length < length) {
1656 TALLOC_FREE(vp->vp_tlv);
1658 vp->vp_tlv = talloc_array(vp, uint8_t, length);
1660 fr_strerror_printf("No memory");
1663 if (fr_hex2bin(vp->vp_tlv, value + 2, length) != length) {
1664 fr_strerror_printf("Invalid hex data in TLV");
1667 vp->length = length;
1674 fr_strerror_printf("unknown attribute type %d", vp->da->type);
1683 /** Use simple heuristics to create an VALUE_PAIR from an unknown address string
1685 * If a DICT_ATTR is not provided for the address type, parsing will fail with
1688 * @param ctx to allocate VP in.
1689 * @param value IPv4/IPv6 address/prefix string.
1690 * @param ipv4 dictionary attribute to use for an IPv4 address.
1691 * @param ipv6 dictionary attribute to use for an IPv6 address.
1692 * @param ipv4_prefix dictionary attribute to use for an IPv4 prefix.
1693 * @param ipv6_prefix dictionary attribute to use for an IPv6 prefix.
1694 * @return NULL on error, or new VALUE_PAIR.
1696 VALUE_PAIR *pairmake_ip(TALLOC_CTX *ctx, char const *value, DICT_ATTR *ipv4, DICT_ATTR *ipv6,
1697 DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix)
1700 DICT_ATTR *da = NULL;
1702 if (!fr_assert(ipv4 || ipv6 || ipv4_prefix || ipv6_prefix)) {
1706 /* No point in repeating the work of pairparsevalue */
1707 if (strchr(value, ':')) {
1708 if (strchr(value, '/')) {
1717 if (strchr(value, '/')) {
1727 fr_strerror_printf("Invalid IP value specified, allowed types are %s%s%s%s",
1728 ipv4 ? "ipaddr " : "", ipv6 ? "ipv6addr " : "",
1729 ipv4_prefix ? "ipv4prefix " : "", ipv6_prefix ? "ipv6prefix" : "");
1732 vp = pairalloc(ctx, da);
1733 if (!vp) return NULL;
1734 if (!pairparsevalue(vp, value)) {
1743 /** Create a valuepair from an ASCII attribute and value
1745 * Where the attribute name is in the form:
1747 * - Attr-%d.%d.%d...
1748 * - Vendor-%d-Attr-%d
1749 * - VendorName-Attr-%d
1751 * @param ctx for talloc
1752 * @param attribute name to parse.
1753 * @param value to parse (must be a hex string).
1754 * @param op to assign to new valuepair.
1755 * @return new valuepair or NULL on error.
1757 static VALUE_PAIR *pairmake_any(TALLOC_CTX *ctx,
1758 char const *attribute, char const *value,
1762 DICT_ATTR const *da;
1767 da = dict_attrunknownbyname(attribute, true);
1768 if (!da) return NULL;
1771 * Unknown attributes MUST be of type 'octets'
1773 if (value && (strncasecmp(value, "0x", 2) != 0)) {
1774 fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
1775 "string, not \"%s\"", attribute, value);
1777 dict_attr_free(&da);
1782 * We've now parsed the attribute properly, Let's create
1783 * it. This next stop also looks the attribute up in the
1784 * dictionary, and creates the appropriate type for it.
1786 vp = pairalloc(ctx, da);
1788 dict_attr_free(&da);
1792 vp->op = (op == 0) ? T_OP_EQ : op;
1794 if (!value) return vp;
1796 size = strlen(value + 2);
1797 vp->length = size >> 1;
1798 data = talloc_array(vp, uint8_t, vp->length);
1800 if (fr_hex2bin(data, value + 2, size) != vp->length) {
1801 fr_strerror_printf("Invalid hex string");
1806 vp->vp_octets = data;
1812 /** Create a VALUE_PAIR from ASCII strings
1814 * Converts an attribute string identifier (with an optional tag qualifier)
1815 * and value string into a VALUE_PAIR.
1817 * The string value is parsed according to the type of VALUE_PAIR being created.
1819 * @param[in] ctx for talloc
1820 * @param[in] vps list where the attribute will be added (optional)
1821 * @param[in] attribute name.
1822 * @param[in] value attribute value (may be NULL if value will be set later).
1823 * @param[in] op to assign to new VALUE_PAIR.
1824 * @return a new VALUE_PAIR.
1826 VALUE_PAIR *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps,
1827 char const *attribute, char const *value, FR_TOKEN op)
1829 DICT_ATTR const *da;
1835 char const *attrname = attribute;
1838 * Check for tags in 'Attribute:Tag' format.
1843 ts = strrchr(attribute, ':');
1845 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1850 strlcpy(buffer, attribute, sizeof(buffer));
1852 ts = strrchr(attrname, ':');
1853 if (!ts) return NULL;
1855 /* Colon found with something behind it */
1856 if (ts[1] == '*' && ts[2] == 0) {
1857 /* Wildcard tag for check items */
1860 } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1861 /* It's not a wild card tag */
1862 tag = strtol(ts + 1, &tc, 0);
1863 if (tc && !*tc && TAG_VALID_ZERO(tag))
1867 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1874 * It's not found in the dictionary, so we use
1875 * another method to create the attribute.
1877 da = dict_attrbyname(attrname);
1879 vp = pairmake_any(ctx, attrname, value, op);
1880 if (vp && vps) pairadd(vps, vp);
1884 /* Check for a tag in the 'Merit' format of:
1885 * :Tag:Value. Print an error if we already found
1886 * a tag in the Attribute.
1889 if (value && (*value == ':' && da->flags.has_tag)) {
1890 /* If we already found a tag, this is invalid */
1892 fr_strerror_printf("Duplicate tag %s for attribute %s",
1894 DEBUG("Duplicate tag %s for attribute %s\n",
1898 /* Colon found and attribute allows a tag */
1899 if (value[1] == '*' && value[2] == ':') {
1900 /* Wildcard tag for check items */
1905 tag = strtol(value + 1, &tc, 0);
1906 if (tc && *tc==':' && TAG_VALID_ZERO(tag))
1912 vp = pairalloc(ctx, da);
1913 if (!vp) return NULL;
1914 vp->op = (op == 0) ? T_OP_EQ : op;
1922 case T_OP_CMP_FALSE:
1923 vp->vp_strvalue = NULL;
1925 value = NULL; /* ignore it! */
1929 * Regular expression comparison of integer attributes
1930 * does a STRING comparison of the names of their
1931 * integer attributes.
1933 case T_OP_REG_EQ: /* =~ */
1934 case T_OP_REG_NE: /* !~ */
1936 fr_strerror_printf("Regular expressions are not supported");
1942 * Someone else will fill in the value.
1952 compare = regcomp(®, value, REG_EXTENDED);
1954 regerror(compare, ®, buffer, sizeof(buffer));
1955 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
1961 vp = pairmake(ctx, NULL, attribute, NULL, op);
1962 if (!vp) return NULL;
1964 if (pairmark_xlat(vp, value) < 0) {
1969 value = NULL; /* ignore it */
1975 * FIXME: if (strcasecmp(attribute, vp->da->name) != 0)
1976 * then the user MAY have typed in the attribute name
1977 * as Vendor-%d-Attr-%d, and the value MAY be octets.
1979 * We probably want to fix pairparsevalue to accept
1980 * octets as values for any attribute.
1982 if (value && !pairparsevalue(vp, value)) {
1987 if (vps) pairadd(vps, vp);
1991 /** Mark a valuepair for xlat expansion
1993 * Copies xlat source (unprocessed) string to valuepair value,
1994 * and sets value type.
1996 * @param vp to mark for expansion.
1997 * @param value to expand.
1998 * @return 0 if marking succeeded or -1 if vp already had a value, or OOM.
2000 int pairmark_xlat(VALUE_PAIR *vp, char const *value)
2005 * valuepair should not already have a value.
2007 if (vp->type != VT_NONE) {
2011 raw = talloc_typed_strdup(vp, value);
2017 vp->value.xlat = raw;
2023 /** Read a single valuepair from a buffer, and advance the pointer
2025 * Sets *eol to T_EOL if end of line was encountered.
2027 * @param[in,out] ptr to read from and update.
2028 * @param[out] raw The struct to write the raw VALUE_PAIR to.
2029 * @return the last token read.
2031 FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw)
2035 FR_TOKEN ret = T_OP_INVALID, next, quote;
2038 if (!ptr || !*ptr || !raw) {
2039 fr_strerror_printf("Invalid arguments");
2040 return T_OP_INVALID;
2044 * Skip leading spaces
2047 while ((*p == ' ') || (*p == '\t')) p++;
2050 fr_strerror_printf("No token read where we expected "
2051 "an attribute name");
2052 return T_OP_INVALID;
2056 fr_strerror_printf("Read a comment instead of a token");
2062 * Try to get the attribute name.
2067 uint8_t const *t = (uint8_t const *) p;
2069 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2071 fr_strerror_printf("Attribute name too long");
2072 return T_OP_INVALID;
2076 * Only ASCII is allowed, and only a subset of that.
2078 if ((*t < 32) || (*t >= 128)) {
2080 fr_strerror_printf("Invalid attribute name");
2081 return T_OP_INVALID;
2085 * This is arguably easier than trying to figure
2086 * out which operators come after the attribute
2087 * name. Yes, our "lexer" is bad.
2089 if (!dict_attr_allowed_chars[(int) *t]) {
2097 * ASCII, but not a valid attribute name.
2099 if (!*raw->l_opand) goto invalid;
2102 * Look for tag (:#). This is different from :=, which
2105 if ((*p == ':') && (isdigit((int) p[1]))) {
2106 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2111 while (isdigit((int) *p)) {
2112 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2122 /* Now we should have an operator here. */
2123 raw->op = gettoken(ptr, buf, sizeof(buf));
2124 if (raw->op < T_EQSTART || raw->op > T_EQEND) {
2125 fr_strerror_printf("Expecting operator");
2127 return T_OP_INVALID;
2131 * Read value. Note that empty string values are allowed
2133 quote = gettoken(ptr, raw->r_opand, sizeof(raw->r_opand));
2134 if (quote == T_EOL) {
2135 fr_strerror_printf("Failed to get value");
2137 return T_OP_INVALID;
2141 * Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
2145 next = gettoken(&p, buf, sizeof(buf));
2156 fr_strerror_printf("Expected end of line or comma");
2157 return T_OP_INVALID;
2165 case T_DOUBLE_QUOTED_STRING:
2167 * Only report as double quoted if it contained valid
2168 * a valid xlat expansion.
2170 p = strchr(raw->r_opand, '%');
2171 if (p && (p[1] == '{')) {
2174 raw->quote = T_SINGLE_QUOTED_STRING;
2187 /** Read one line of attribute/value pairs into a list.
2189 * The line may specify multiple attributes separated by commas.
2191 * @note If the function returns T_OP_INVALID, an error has occurred and
2192 * @note the valuepair list should probably be freed.
2194 * @param ctx for talloc
2195 * @param buffer to read valuepairs from.
2196 * @param list where the parsed VALUE_PAIRs will be appended.
2197 * @return the last token parsed, or T_OP_INVALID
2199 FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **list)
2201 VALUE_PAIR *vp, *head, **tail;
2203 FR_TOKEN last_token = T_OP_INVALID;
2204 FR_TOKEN previous_token;
2208 * We allow an empty line.
2210 if (buffer[0] == 0) {
2219 raw.l_opand[0] = '\0';
2220 raw.r_opand[0] = '\0';
2222 previous_token = last_token;
2224 last_token = pairread(&p, &raw);
2225 if (last_token == T_OP_INVALID) break;
2227 if (raw.quote == T_DOUBLE_QUOTED_STRING) {
2228 vp = pairmake(ctx, NULL, raw.l_opand, NULL, raw.op);
2230 last_token = T_OP_INVALID;
2233 if (pairmark_xlat(vp, raw.r_opand) < 0) {
2235 last_token = T_OP_INVALID;
2239 vp = pairmake(ctx, NULL, raw.l_opand, raw.r_opand, raw.op);
2241 last_token = T_OP_INVALID;
2247 tail = &((*tail)->next);
2248 } while (*p && (last_token == T_COMMA));
2251 * Don't tell the caller that there was a comment.
2253 if (last_token == T_HASH) {
2254 last_token = previous_token;
2257 if (last_token == T_OP_INVALID) {
2260 pairadd(list, head);
2264 * And return the last token which we read.
2270 * Read valuepairs from the fp up to End-Of-File.
2272 int readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone)
2275 FR_TOKEN last_token = T_EOL;
2279 VALUE_PAIR *vp = NULL;
2281 fr_cursor_init(&cursor, out);
2283 while (fgets(buf, sizeof(buf), fp) != NULL) {
2285 * If we get a '\n' by itself, we assume that's
2286 * the end of that VP
2288 if (buf[0] == '\n') {
2294 * Comments get ignored
2296 if (buf[0] == '#') continue;
2299 * Read all of the attributes on the current line.
2302 last_token = userparse(ctx, buf, &vp);
2304 if (last_token != T_EOL) goto error;
2308 fr_cursor_insert(&cursor, vp);
2317 vp = fr_cursor_first(&cursor);
2318 if (vp) pairfree(&vp);
2323 /** Compare two attribute values
2325 * @param[in] one the first attribute.
2326 * @param[in] two the second attribute.
2327 * @return -1 if one is less than two, 0 if both are equal, 1 if one is more than two, < -1 on error.
2329 int8_t paircmp_value(VALUE_PAIR const *one, VALUE_PAIR const *two)
2331 int64_t compare = 0;
2336 if (one->da->type != two->da->type) {
2337 fr_strerror_printf("Can't compare attribute values of different types");
2342 * After doing the previous check for special comparisons,
2343 * do the per-type comparison here.
2345 switch (one->da->type) {
2346 case PW_TYPE_ABINARY:
2347 case PW_TYPE_OCTETS:
2351 if (one->length > two->length) {
2352 length = one->length;
2354 length = two->length;
2358 compare = memcmp(one->vp_octets, two->vp_octets, length);
2359 if (compare != 0) break;
2363 * Contents are the same. The return code
2364 * is therefore the difference in lengths.
2366 * i.e. "0x00" is smaller than "0x0000"
2368 compare = one->length - two->length;
2372 case PW_TYPE_STRING:
2373 fr_assert(one->vp_strvalue);
2374 fr_assert(two->vp_strvalue);
2375 compare = strcmp(one->vp_strvalue, two->vp_strvalue);
2380 case PW_TYPE_INTEGER:
2382 compare = (int64_t) one->vp_integer - (int64_t) two->vp_integer;
2385 case PW_TYPE_SIGNED:
2386 compare = one->vp_signed - two->vp_signed;
2389 case PW_TYPE_INTEGER64:
2391 * Don't want integer overflow!
2393 if (one->vp_integer64 < two->vp_integer64) {
2395 } else if (one->vp_integer64 > two->vp_integer64) {
2400 case PW_TYPE_ETHERNET:
2401 compare = memcmp(&one->vp_ether, &two->vp_ether, sizeof(one->vp_ether));
2404 case PW_TYPE_IPADDR:
2405 compare = (int64_t) ntohl(one->vp_ipaddr) - (int64_t) ntohl(two->vp_ipaddr);
2408 case PW_TYPE_IPV6ADDR:
2409 compare = memcmp(&one->vp_ipv6addr, &two->vp_ipv6addr, sizeof(one->vp_ipv6addr));
2412 case PW_TYPE_IPV6PREFIX:
2413 compare = memcmp(&one->vp_ipv6prefix, &two->vp_ipv6prefix, sizeof(one->vp_ipv6prefix));
2416 case PW_TYPE_IPV4PREFIX:
2417 compare = memcmp(&one->vp_ipv4prefix, &two->vp_ipv4prefix, sizeof(one->vp_ipv4prefix));
2421 compare = memcmp(&one->vp_ifid, &two->vp_ifid, sizeof(one->vp_ifid));
2425 * None of the types below should be in the REQUEST
2427 case PW_TYPE_COMBO_IP: /* This should of been converted into IPADDR/IPV6ADDR */
2429 case PW_TYPE_EXTENDED:
2430 case PW_TYPE_LONG_EXTENDED:
2433 case PW_TYPE_INVALID: /* We should never see these */
2435 fr_assert(0); /* unknown type */
2439 * Do NOT add a default here, as new types are added
2440 * static analysis will warn us they're not handled
2446 } else if (compare < 0) {
2453 * We leverage the fact that IPv4 and IPv6 prefixes both
2454 * have the same format:
2456 * reserved, prefix-len, data...
2458 static int paircmp_op_cidr(FR_TOKEN op, int bytes,
2459 uint8_t one_net, uint8_t const *one,
2460 uint8_t two_net, uint8_t const *two)
2466 * Handle the case of netmasks being identical.
2468 if (one_net == two_net) {
2471 compare = memcmp(one, two, bytes);
2474 * If they're identical return true for
2477 if ((compare == 0) &&
2478 ((op == T_OP_CMP_EQ) ||
2485 * Everything else returns false.
2487 * 10/8 == 24/8 --> false
2488 * 10/8 <= 24/8 --> false
2489 * 10/8 >= 24/8 --> false
2495 * Netmasks are different. That limits the
2496 * possible results, based on the operator.
2506 case T_OP_LT: /* 192/8 < 192.168/16 --> false */
2507 if (one_net < two_net) {
2513 case T_OP_GT: /* 192/16 > 192.168/8 --> false */
2514 if (one_net > two_net) {
2523 if (one_net < two_net) {
2530 * Do the check byte by byte. If the bytes are
2531 * identical, it MAY be a match. If they're different,
2532 * it is NOT a match.
2537 * All leading bytes are identical.
2539 if (common == 0) return true;
2542 * Doing bitmasks takes more work.
2544 if (common < 8) break;
2546 if (one[i] != two[i]) return false;
2554 mask <<= (8 - common);
2558 if ((one[i] & mask) == ((two[i] & mask))) {
2565 /** Compare two attributes using an operator
2567 * @param[in] a the first attribute
2568 * @param[in] op the operator for comparison.
2569 * @param[in] b the second attribute
2570 * @return 1 if true, 0 if false, -1 on error.
2572 int8_t paircmp_op(VALUE_PAIR const *a, FR_TOKEN op, VALUE_PAIR const *b)
2576 if (!a || !b) return -1;
2578 switch (a->da->type) {
2579 case PW_TYPE_IPADDR:
2580 switch (b->da->type) {
2581 case PW_TYPE_IPADDR: /* IPv4 and IPv4 */
2584 case PW_TYPE_IPV4PREFIX: /* IPv4 and IPv4 Prefix */
2585 return paircmp_op_cidr(op, 4, 32, (uint8_t const *) &a->vp_ipaddr,
2586 b->vp_ipv4prefix[1], (uint8_t const *) &b->vp_ipv4prefix + 2);
2589 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
2594 case PW_TYPE_IPV4PREFIX: /* IPv4 and IPv4 Prefix */
2595 switch (b->da->type) {
2596 case PW_TYPE_IPADDR:
2597 return paircmp_op_cidr(op, 4, a->vp_ipv4prefix[1],
2598 (uint8_t const *) &a->vp_ipv4prefix + 2,
2599 32, (uint8_t const *) &b->vp_ipaddr);
2601 case PW_TYPE_IPV4PREFIX: /* IPv4 Prefix and IPv4 Prefix */
2602 return paircmp_op_cidr(op, 4, a->vp_ipv4prefix[1],
2603 (uint8_t const *) &a->vp_ipv4prefix + 2,
2604 b->vp_ipv4prefix[1], (uint8_t const *) &b->vp_ipv4prefix + 2);
2607 fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
2612 case PW_TYPE_IPV6ADDR:
2613 switch (b->da->type) {
2614 case PW_TYPE_IPV6ADDR: /* IPv6 and IPv6 */
2617 case PW_TYPE_IPV6PREFIX: /* IPv6 and IPv6 Preifx */
2618 return paircmp_op_cidr(op, 16, 128, (uint8_t const *) &a->vp_ipv6addr,
2619 b->vp_ipv6prefix[1], (uint8_t const *) &b->vp_ipv6prefix + 2);
2623 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
2628 case PW_TYPE_IPV6PREFIX:
2629 switch (b->da->type) {
2630 case PW_TYPE_IPV6ADDR: /* IPv6 Prefix and IPv6 */
2631 return paircmp_op_cidr(op, 16, a->vp_ipv6prefix[1],
2632 (uint8_t const *) &a->vp_ipv6prefix + 2,
2633 128, (uint8_t const *) &b->vp_ipv6addr);
2635 case PW_TYPE_IPV6PREFIX: /* IPv6 Prefix and IPv6 */
2636 return paircmp_op_cidr(op, 16, a->vp_ipv6prefix[1],
2637 (uint8_t const *) &a->vp_ipv6prefix + 2,
2638 b->vp_ipv6prefix[1], (uint8_t const *) &b->vp_ipv6prefix + 2);
2641 fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
2648 compare = paircmp_value(a, b);
2649 if (compare < -1) { /* comparison error */
2655 * Now do the operator comparison.
2659 return (compare == 0);
2662 return (compare != 0);
2665 return (compare < 0);
2668 return (compare > 0);
2671 return (compare <= 0);
2674 return (compare >= 0);
2681 /** Compare two pairs, using the operator from "a"
2683 * i.e. given two attributes, it does:
2685 * (b->data) (a->operator) (a->data)
2687 * e.g. "foo" != "bar"
2689 * @param[in] a the first attribute
2690 * @param[in] b the second attribute
2691 * @return 1 if true, 0 if false, -1 on error.
2693 int8_t paircmp(VALUE_PAIR *a, VALUE_PAIR *b)
2698 if (b) VERIFY_VP(b);
2704 case T_OP_CMP_FALSE:
2708 * a is a regex, compile it, print b to a string,
2709 * and then do string comparisons.
2719 char buffer[MAX_STRING_LEN * 4 + 1];
2721 compare = regcomp(®, a->vp_strvalue, REG_EXTENDED);
2723 regerror(compare, ®, buffer, sizeof(buffer));
2724 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
2725 a->da->name, buffer);
2731 vp_prints_value(buffer, sizeof(buffer), b, 0);
2734 * Don't care about substring matches,
2737 compare = regexec(®, buffer, 0, NULL, 0);
2740 if (a->op == T_OP_REG_EQ) {
2741 return (compare == 0);
2744 return (compare != 0);
2748 default: /* we're OK */
2752 return paircmp_op(b, a->op, a);
2755 /** Determine equality of two lists
2757 * This is useful for comparing lists of attributes inserted into a binary tree.
2759 * @param a first list of VALUE_PAIRs.
2760 * @param b second list of VALUE_PAIRs.
2761 * @return -1 if a < b, 0 if the two lists are equal, 1 if a > b, -2 on error.
2763 int8_t pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b)
2765 vp_cursor_t a_cursor, b_cursor;
2766 VALUE_PAIR *a_p, *b_p;
2769 for (a_p = fr_cursor_init(&a_cursor, &a), b_p = fr_cursor_init(&b_cursor, &b);
2771 a_p = fr_cursor_next(&a_cursor), b_p = fr_cursor_next(&b_cursor)) {
2772 /* Same VP, no point doing expensive checks */
2777 if (a_p->da < b_p->da) {
2780 if (a_p->da > b_p->da) {
2784 if (a_p->tag < b_p->tag) {
2787 if (a_p->tag > b_p->tag) {
2791 ret = paircmp_value(a_p, b_p);
2793 fr_assert(ret >= -1); /* Comparison error */
2810 /** Set the type of the VALUE_PAIR value buffer to match it's DICT_ATTR
2812 * @param vp to fixup.
2814 static void pairtypeset(VALUE_PAIR *vp)
2816 if (!vp->data.ptr) return;
2818 switch(vp->da->type) {
2819 case PW_TYPE_OCTETS:
2821 talloc_set_type(vp->data.ptr, uint8_t);
2824 case PW_TYPE_STRING:
2825 talloc_set_type(vp->data.ptr, char);
2833 /** Copy data into an "octets" data type.
2835 * @param[in,out] vp to update
2836 * @param[in] src data to copy
2837 * @param[in] size of the data
2839 void pairmemcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
2845 p = talloc_memdup(vp, src, size);
2847 talloc_set_type(p, uint8_t);
2849 memcpy(&q, &vp->vp_octets, sizeof(q));
2857 /** Reparent an allocated octet buffer to a VALUE_PAIR
2859 * @param[in,out] vp to update
2860 * @param[in] src buffer to steal.
2862 void pairmemsteal(VALUE_PAIR *vp, uint8_t const *src)
2868 memcpy(&q, &vp->vp_octets, sizeof(q));
2871 vp->vp_octets = talloc_steal(vp, src);
2873 vp->length = talloc_array_length(vp->vp_strvalue);
2877 /** Reparent an allocated char buffer to a VALUE_PAIR
2879 * @param[in,out] vp to update
2880 * @param[in] src buffer to steal.
2882 void pairstrsteal(VALUE_PAIR *vp, char const *src)
2888 memcpy(&q, &vp->vp_octets, sizeof(q));
2891 vp->vp_strvalue = talloc_steal(vp, src);
2893 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2897 /** Copy data into an "string" data type.
2899 * @param[in,out] vp to update
2900 * @param[in] src data to copy
2902 void pairstrcpy(VALUE_PAIR *vp, char const *src)
2908 p = talloc_strdup(vp, src);
2912 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2915 vp->vp_strvalue = p;
2917 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2922 /** Print data into an "string" data type.
2924 * @param[in,out] vp to update
2925 * @param[in] fmt the format string
2927 void pairsprintf(VALUE_PAIR *vp, char const *fmt, ...)
2935 p = talloc_vasprintf(vp, fmt, ap);
2940 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2943 vp->vp_strvalue = p;
2946 vp->length = talloc_array_length(vp->vp_strvalue) - 1;