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.
100 if (!da) return NULL;
102 vp = talloc_zero(ctx, VALUE_PAIR);
104 fr_strerror_printf("Out of memory");
112 vp->length = da->flags.length;
114 talloc_set_destructor(vp, _pairfree);
119 /** Create a new valuepair
121 * If attr and vendor match a dictionary entry then a VP with that DICT_ATTR
124 * If attr or vendor are uknown will call dict_attruknown to create a dynamic
125 * DICT_ATTR of PW_TYPE_OCTETS.
127 * Which type of DICT_ATTR the VALUE_PAIR was created with can be determined by
128 * checking @verbatim vp->da->flags.is_unknown @endverbatim.
130 * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
131 * @param[in] attr number.
132 * @param[in] vendor number.
133 * @return the new valuepair or NULL on error.
135 VALUE_PAIR *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
139 da = dict_attrbyvalue(attr, vendor);
141 da = dict_attrunknown(attr, vendor, true);
147 return pairalloc(ctx, da);
150 /** Free memory used by a valuepair list.
152 * @todo TLV: needs to free all dependents of each VP freed.
154 void pairfree(VALUE_PAIR **vps)
163 for (vp = paircursor(&cursor, vps);
165 vp = pairnext(&cursor)) {
173 /** Mark malformed or unrecognised attributed as unknown
175 * @param vp to change DICT_ATTR of.
176 * @return 0 on success (or if already unknown) else -1 on error.
178 int pair2unknown(VALUE_PAIR *vp)
183 if (vp->da->flags.is_unknown) {
187 da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
197 /** Find the pair with the matching attribute
199 * @todo should take DAs and do a pointer comparison.
201 VALUE_PAIR *pairfind(VALUE_PAIR *vp, unsigned int attr, unsigned int vendor,
207 for (i = paircursor(&cursor, &vp);
209 i = pairnext(&cursor)) {
211 if ((i->da->attr == attr) && (i->da->vendor == vendor)
212 && ((tag == TAG_ANY) || (i->da->flags.has_tag &&
221 /** Setup a cursor to iterate over attribute pairs
223 * @param cursor Where to initialise the cursor (uses existing structure).
224 * @param node to start from.
226 VALUE_PAIR *paircursorc(vp_cursor_t *cursor, VALUE_PAIR const * const *node)
228 memset(cursor, 0, sizeof(*cursor));
230 if (!node || !cursor) {
234 memcpy(&cursor->first, &node, sizeof(cursor->first));
235 cursor->current = *cursor->first;
237 if (cursor->current) {
238 VERIFY_VP(cursor->current);
239 cursor->next = cursor->current->next;
242 return cursor->current;
245 VALUE_PAIR *pairfirst(vp_cursor_t *cursor)
247 cursor->current = *cursor->first;
249 if (cursor->current) {
250 VERIFY_VP(cursor->current);
251 cursor->next = cursor->current->next;
252 if (cursor->next) VERIFY_VP(cursor->next);
253 cursor->found = NULL;
256 return cursor->current;
259 /** Iterate over attributes of a given type in the pairlist
263 VALUE_PAIR *pairfindnext(vp_cursor_t *cursor, unsigned int attr, unsigned int vendor, int8_t tag)
267 i = pairfind(!cursor->found ? cursor->current : cursor->found->next, attr, vendor, tag);
270 cursor->current = NULL;
275 cursor->next = i->next;
282 /** Retrieve the next VALUE_PAIR
286 VALUE_PAIR *pairnext(vp_cursor_t *cursor)
288 cursor->current = cursor->next;
289 if (cursor->current) {
290 VERIFY_VP(cursor->current);
293 * Set this now in case 'current' gets freed before
294 * pairnext is called again.
296 cursor->next = cursor->current->next;
299 * Next call to pairfindnext will start from the current
300 * position in the list, not the last found instance.
302 cursor->found = NULL;
305 return cursor->current;
308 VALUE_PAIR *paircurrent(vp_cursor_t *cursor)
310 if (cursor->current) {
311 VERIFY_VP(cursor->current);
314 return cursor->current;
319 * @todo don't use with pairdelete
321 void pairinsert(vp_cursor_t *cursor, VALUE_PAIR *add)
332 * Cursor was initialised with a pointer to a NULL value_pair
334 if (!*cursor->first) {
335 *cursor->first = add;
336 cursor->current = add;
337 cursor->next = cursor->current->next;
343 * We don't yet know where the last VALUE_PAIR is
345 * Assume current is closer to the end of the list and use that if available.
348 cursor->last = cursor->current ? cursor->current : *cursor->first;
351 VERIFY_VP(cursor->last);
354 * Something outside of the cursor added another VALUE_PAIR
356 if (cursor->last->next) {
357 for (i = cursor->last; i; i = i->next) {
364 * Either current was never set, or something iterated to the end of the
367 if (!cursor->current) {
368 cursor->current = add;
371 cursor->last->next = add;
374 /** Remove the current pair
376 * @todo this is really inefficient and should be fixed...
378 * @param cursor to remove the current pair from.
379 * @return NULL on error, else the VALUE_PAIR we just removed.
381 VALUE_PAIR *pairremove(vp_cursor_t *cursor)
383 VALUE_PAIR *vp, **last;
385 vp = paircurrent(cursor);
390 last = cursor->first;
391 while (*last != vp) {
392 last = &(*last)->next;
395 pairnext(cursor); /* Advance the cursor past the one were about to delete */
403 /** Delete matching pairs
405 * Delete matching pairs from the attribute list.
407 * @param[in,out] first VP in list.
408 * @param[in] attr to match.
409 * @param[in] vendor to match.
410 * @param[in] tag to match. TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
412 * @todo should take DAs and do a point comparison.
414 void pairdelete(VALUE_PAIR **first, unsigned int attr, unsigned int vendor,
417 VALUE_PAIR *i, *next;
418 VALUE_PAIR **last = first;
420 for(i = *first; i; i = next) {
423 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
425 (i->da->flags.has_tag && (i->tag == tag)))) {
434 /** Add a VP to the end of the list.
436 * Locates the end of 'first', and links an additional VP 'add' at the end.
438 * @param[in] first VP in linked list. Will add new VP to the end of this list.
439 * @param[in] add VP to add to list.
441 void pairadd(VALUE_PAIR **first, VALUE_PAIR *add)
449 if (*first == NULL) {
453 for(i = *first; i->next; i = i->next)
458 /** Replace all matching VPs
460 * Walks over 'first', and replaces the first VP that matches 'replace'.
462 * @note Memory used by the VP being replaced will be freed.
463 * @note Will not work with unknown attributes.
465 * @param[in,out] first VP in linked list. Will search and replace in this list.
466 * @param[in] replace VP to replace.
468 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *replace)
470 VALUE_PAIR *i, *next;
471 VALUE_PAIR **prev = first;
475 if (*first == NULL) {
481 * Not an empty list, so find item if it is there, and
482 * replace it. Note, we always replace the first one, and
483 * we ignore any others that might exist.
485 for(i = *first; i; i = next) {
490 * Found the first attribute, replace it,
493 if ((i->da == replace->da) &&
494 (!i->da->flags.has_tag || (i->tag == replace->tag))
499 * Should really assert that replace->next == NULL
501 replace->next = next;
507 * Point to where the attribute should go.
513 * If we got here, we didn't find anything to replace, so
514 * stopped at the last item, which we just append to.
519 static void pairsort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
525 * Stopping condition - no more elements left to split
527 if (!source || !source->next) {
535 * Fast advances twice as fast as slow, so when it gets to the end,
536 * slow will point to the middle of the linked list.
554 static VALUE_PAIR *pairsort_merge(VALUE_PAIR *a, VALUE_PAIR *b, bool with_tag)
556 VALUE_PAIR *result = NULL;
562 * Compare the DICT_ATTRs and tags
564 if ((with_tag && (a->tag < b->tag)) || (a->da <= b->da)) {
566 result->next = pairsort_merge(a->next, b, with_tag);
569 result->next = pairsort_merge(a, b->next, with_tag);
575 /** Sort a linked list of VALUE_PAIRs using merge sort
577 * @param[in,out] vps List of VALUE_PAIRs to sort.
578 * @param[in] with_tag sort by tag then by DICT_ATTR
580 void pairsort(VALUE_PAIR **vps, bool with_tag)
582 VALUE_PAIR *head = *vps;
587 * If there's 0-1 elements it must already be sorted.
589 if (!head || !head->next) {
593 pairsort_split(head, &a, &b); /* Split into sublists */
594 pairsort(&a, with_tag); /* Traverse left */
595 pairsort(&b, with_tag); /* Traverse right */
598 * merge the two sorted lists together
600 *vps = pairsort_merge(a, b, with_tag);
603 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
605 * @param filter attributes to check list against.
606 * @param list attributes, probably a request or reply
608 bool pairvalidate(VALUE_PAIR *filter, VALUE_PAIR *list)
610 vp_cursor_t filter_cursor;
611 vp_cursor_t list_cursor;
613 VALUE_PAIR *check, *match;
615 if (!filter && !list) {
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, true);
629 pairsort(&list, true);
631 match = paircursor(&list_cursor, &list);
632 check = paircursor(&filter_cursor, &filter);
636 * The lists are sorted, so if the first
637 * attributes aren't of the same type, then we're
640 if (!attribute_eq(check, match)) {
645 * They're of the same type, but don't have the
646 * same values. This is a problem.
648 * Note that the RFCs say that for attributes of
649 * the same type, order is important.
651 if (!paircmp(check, match)) {
655 match = pairnext(&list_cursor);
656 check = pairnext(&filter_cursor);
658 if (!match && !check) break;
661 * One list ended earlier than the others, they
664 if (!match || !check) {
672 /** Copy a single valuepair
674 * Allocate a new valuepair and copy the da from the old vp.
676 * @param[in] ctx for talloc
677 * @param[in] vp to copy.
678 * @return a copy of the input VP or NULL on error.
680 VALUE_PAIR *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
684 if (!vp) return NULL;
688 n = pairalloc(ctx, vp->da);
690 fr_strerror_printf("out of memory");
694 memcpy(n, vp, sizeof(*n));
699 if (vp->type == VT_XLAT) {
700 n->value.xlat = talloc_strdup(n, n->value.xlat);
703 n->da = dict_attr_copy(vp->da, true);
711 if ((n->da->type == PW_TYPE_TLV) ||
712 (n->da->type == PW_TYPE_OCTETS)) {
713 if (n->vp_octets != NULL) {
714 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
717 } else if (n->da->type == PW_TYPE_STRING) {
718 if (n->vp_strvalue != NULL) {
720 * Equivalent to, and faster than strdup.
722 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1);
729 /** Copy data from one VP to another
731 * Allocate a new pair using da, and copy over the value from the specified
734 * @todo Should be able to do type conversions.
736 * @param[in] ctx for talloc
737 * @param[in] da of new attribute to alloc.
738 * @param[in] vp to copy data from.
739 * @return the new valuepair.
741 VALUE_PAIR *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp)
745 if (!vp) return NULL;
750 * The types have to be identical, OR the "from" VP has
753 if (da->type != vp->da->type) {
756 VALUE_PAIR const **pvp;
758 if (vp->da->type == PW_TYPE_OCTETS) {
760 * Decode the data. It may be wrong!
762 if (rad_data2vp(da->attr, da->vendor, vp->vp_octets, vp->length, &n) < 0) {
771 * Else the destination type is octets
773 switch (vp->da->type) {
775 return NULL; /* can't do it */
777 case PW_TYPE_INTEGER:
781 case PW_TYPE_IPV6ADDR:
782 case PW_TYPE_IPV6PREFIX:
785 case PW_TYPE_ETHERNET:
787 case PW_TYPE_INTEGER64:
788 case PW_TYPE_IPV4PREFIX:
792 n = pairalloc(ctx, da);
795 p = talloc_array(n, uint8_t, dict_attr_sizes[vp->da->type][1] + 2);
798 length = rad_vp2attr(NULL, NULL, NULL, pvp, p, dict_attr_sizes[vp->da->type][1]);
804 pairmemcpy(n, p + 2, length - 2);
809 n = pairalloc(ctx, da);
814 memcpy(n, vp, sizeof(*n));
817 if (n->type == VT_XLAT) {
818 n->value.xlat = talloc_strdup(n, n->value.xlat);
821 switch (n->da->type) {
824 if (n->vp_octets != NULL) {
825 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
830 if (n->vp_strvalue != NULL) {
831 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1); /* NULL byte */
844 /** Copy matching pairs
846 * Copy pairs of a matching attribute number, vendor number and tag from the
847 * the input list to a new list, and returns the head of this list.
849 * @param[in] ctx for talloc
850 * @param[in] from whence to copy VALUE_PAIRs.
851 * @param[in] attr to match, if 0 input list will not be filtered by attr.
852 * @param[in] vendor to match.
853 * @param[in] tag to match, TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
854 * @return the head of the new VALUE_PAIR list or NULL on error.
856 VALUE_PAIR *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from,
857 unsigned int attr, unsigned int vendor, int8_t tag)
859 vp_cursor_t src, dst;
861 VALUE_PAIR *out = NULL, *vp;
863 paircursor(&dst, &out);
864 for (vp = paircursor(&src, &from);
866 vp = pairnext(&src)) {
869 if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
873 if ((tag != TAG_ANY) && vp->da->flags.has_tag && (vp->tag != tag)) {
877 vp = paircopyvp(ctx, vp);
882 pairinsert(&dst, vp);
891 * Copy all pairs from 'from' regardless of tag, attribute or vendor.
893 * @param[in] ctx for new VALUE_PAIRs to be allocated in.
894 * @param[in] from whence to copy VALUE_PAIRs.
895 * @return the head of the new VALUE_PAIR list or NULL on error.
897 VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from)
899 vp_cursor_t src, dst;
901 VALUE_PAIR *out = NULL, *vp;
903 paircursor(&dst, &out);
904 for (vp = paircursor(&src, &from);
906 vp = pairnext(&src)) {
908 vp = paircopyvp(ctx, vp);
913 pairinsert(&dst, vp); /* paircopy sets next pointer to NULL */
919 /** Move pairs from source list to destination list respecting operator
921 * @note This function does some additional magic that's probably not needed
922 * in most places. Consider using radius_pairmove in server code.
924 * @note pairfree should be called on the head of the source list to free
925 * unmoved attributes (if they're no longer needed).
927 * @note Does not respect tags when matching.
929 * @param[in] ctx for talloc
930 * @param[in,out] to destination list.
931 * @param[in,out] from source list.
933 * @see radius_pairmove
935 void pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from)
937 VALUE_PAIR *i, *j, *found;
938 VALUE_PAIR *head_new, **tail_new;
939 VALUE_PAIR **tail_from;
941 if (!to || !from || !*from) return;
944 * We're editing the "to" list while we're adding new
945 * attributes to it. We don't want the new attributes to
946 * be edited, so we create an intermediate list to hold
947 * them during the editing process.
950 tail_new = &head_new;
953 * We're looping over the "from" list, moving some
954 * attributes out, but leaving others in place.
957 while ((i = *tail_from) != NULL) {
961 * We never move Fall-Through.
963 if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
964 tail_from = &(i->next);
969 * Unlike previous versions, we treat all other
970 * attributes as normal. i.e. there's no special
971 * treatment for passwords or Hint.
976 * These are COMPARISON attributes
977 * from a check list, and are not
978 * supposed to be moved.
990 tail_from = &(i->next);
994 * Add it to the "to" list, but only if
995 * it doesn't already exist.
998 found = pairfind(*to, i->da->attr, i->da->vendor,
1000 if (!found) goto do_add;
1002 tail_from = &(i->next);
1006 * Add it to the "to" list, and delete any attribute
1007 * of the same vendor/attr which already exists.
1010 found = pairfind(*to, i->da->attr, i->da->vendor,
1012 if (!found) goto do_add;
1018 * Do NOT call pairdelete() here,
1019 * due to issues with re-writing
1020 * "request->username".
1022 * Everybody calls pairmove, and
1023 * expects it to work. We can't
1024 * update request->username here,
1025 * so instead we over-write the
1026 * vp that it's pointing to.
1028 switch (found->da->type) {
1030 memcpy(found, i, sizeof(*found));
1034 pairmemsteal(found, i->vp_tlv);
1038 case PW_TYPE_OCTETS:
1039 pairmemsteal(found, i->vp_octets);
1040 i->vp_octets = NULL;
1043 case PW_TYPE_STRING:
1044 pairstrsteal(found, i->vp_strvalue);
1045 i->vp_strvalue = NULL;
1051 * Delete *all* of the attributes
1052 * of the same number.
1054 pairdelete(&found->next,
1056 found->da->vendor, TAG_ANY);
1059 * Remove this attribute from the
1062 *tail_from = i->next;
1068 * Move it from the old list and add it
1073 *tail_from = i->next;
1075 *tail_new = talloc_steal(ctx, i);
1076 tail_new = &(i->next);
1082 } /* loop over the "from" list. */
1085 * Take the "new" list, and append it to the "to" list.
1087 pairadd(to, head_new);
1090 /** Move matching pairs between VALUE_PAIR lists
1092 * Move pairs of a matching attribute number, vendor number and tag from the
1093 * the input list to the output list.
1095 * @note pairfree should be called on the head of the old list to free unmoved
1096 attributes (if they're no longer needed).
1098 * @param[in] ctx for talloc
1099 * @param[in,out] to destination list.
1100 * @param[in,out] from source list.
1101 * @param[in] attr to match, if PW_VENDOR_SPECIFIC and vendor 0, only VSAs will
1102 * be copied. If 0 and 0, all attributes will match
1103 * @param[in] vendor to match.
1104 * @param[in] tag to match, TAG_ANY matches any tag, TAG_UNUSED matches tagless VPs.
1106 void pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from, unsigned int attr, unsigned int vendor, int8_t tag)
1108 VALUE_PAIR *to_tail, *i, *next;
1109 VALUE_PAIR *iprev = NULL;
1112 * Find the last pair in the "to" list and put it in "to_tail".
1114 * @todo: replace the "if" with "VALUE_PAIR **tail"
1118 for(i = *to; i; i = i->next) {
1126 * Attr/vendor of 0 means "move them all".
1127 * It's better than "pairadd(foo,bar);bar=NULL"
1129 if ((vendor == 0) && (attr == 0)) {
1131 to_tail->next = *from;
1136 for (i = *from; i; i = i->next) {
1137 (void) talloc_steal(ctx, i);
1144 for(i = *from; i; i = next) {
1148 if ((tag != TAG_ANY) && i->da->flags.has_tag &&
1154 * vendor=0, attr = PW_VENDOR_SPECIFIC means
1155 * "match any vendor attribute".
1157 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
1159 * It's a VSA: move it over.
1161 if (i->da->vendor != 0) goto move;
1164 * It's Vendor-Specific: move it over.
1166 if (i->da->attr == attr) goto move;
1169 * It's not a VSA: ignore it.
1176 * If it isn't an exact match, ignore it.
1178 if (!((i->da->vendor == vendor) && (i->da->attr == attr))) {
1185 * Remove the attribute from the "from" list.
1193 * Add the attribute to the "to" list.
1201 (void) talloc_steal(ctx, i);
1205 static char const *hextab = "0123456789abcdef";
1207 bool pairparsevalue(VALUE_PAIR *vp, char const *value)
1210 char const *cp, *cs;
1216 if (!value) return false;
1220 * It's a comparison, not a real VALUE_PAIR, copy the string over verbatim
1222 if ((vp->op == T_OP_REG_EQ) || (vp->op == T_OP_REG_NE)) {
1223 pairstrcpy(vp, value); /* Icky hacky ewww */
1227 switch(vp->da->type) {
1228 case PW_TYPE_STRING:
1232 p = talloc_strdup(vp, value);
1233 vp->vp_strvalue = p;
1271 c = '\\'; /* no cp++ */
1274 if ((cp[0] >= '0') &&
1280 (sscanf(cp, "%3o", &x) == 1)) {
1283 } /* else just do '\\' */
1290 vp->length = length;
1293 case PW_TYPE_IPADDR:
1295 * FIXME: complain if hostname
1296 * cannot be resolved, or resolve later!
1305 * Convert things which are obviously integers to IP addresses
1307 * We assume the number is the bigendian representation of the
1310 if (fr_integer_check(value)) {
1311 vp->vp_ipaddr = htonl(atol(value));
1315 if (ip_hton(cs, AF_INET, &ipaddr) < 0) {
1316 fr_strerror_printf("Failed to find IP address for %s", cs);
1320 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1329 * Note that ALL integers are unsigned!
1331 vp->vp_integer = fr_strtoul(value, &p);
1333 if (vp->vp_integer > 255) {
1334 fr_strerror_printf("Byte value \"%s\" is larger than 255", value);
1339 if (fr_whitespace_check(p)) break;
1340 goto check_for_value;
1344 * Note that ALL integers are unsigned!
1346 vp->vp_integer = fr_strtoul(value, &p);
1349 if (vp->vp_integer > 65535) {
1350 fr_strerror_printf("Byte value \"%s\" is larger than 65535", value);
1355 if (fr_whitespace_check(p)) break;
1356 goto check_for_value;
1358 case PW_TYPE_INTEGER:
1360 * Note that ALL integers are unsigned!
1362 vp->vp_integer = fr_strtoul(value, &p);
1365 if (fr_whitespace_check(p)) break;
1369 * Look for the named value for the given
1372 if ((dval = dict_valbyname(vp->da->attr, vp->da->vendor, value)) == NULL) {
1373 fr_strerror_printf("Unknown value '%s' for attribute '%s'", value, vp->da->name);
1376 vp->vp_integer = dval->value;
1379 case PW_TYPE_INTEGER64:
1381 * Note that ALL integers are unsigned!
1383 if (sscanf(value, "%" PRIu64, &y) != 1) {
1384 fr_strerror_printf("Invalid value '%s' for attribute '%s'",
1385 value, vp->da->name);
1388 vp->vp_integer64 = y;
1390 length = strspn(value, "0123456789");
1391 if (fr_whitespace_check(value + length)) break;
1397 * time_t may be 64 bits, whule vp_date
1398 * MUST be 32-bits. We need an
1399 * intermediary variable to handle
1404 if (fr_get_time(value, &date) < 0) {
1405 fr_strerror_printf("failed to parse time string "
1415 case PW_TYPE_ABINARY:
1416 #ifdef WITH_ASCEND_BINARY
1417 if (strncasecmp(value, "0x", 2) == 0) {
1421 if (ascend_parse_filter(vp, value) < 0 ) {
1422 /* Allow ascend_parse_filter's strerror to bubble up */
1428 * If Ascend binary is NOT defined,
1429 * then fall through to raw octets, so that
1430 * the user can at least make them by hand...
1433 /* raw octets: 0x01020304... */
1435 if (strcmp(value, "ANY") == 0) {
1438 } /* else it's hex */
1440 case PW_TYPE_OCTETS:
1441 if (strncasecmp(value, "0x", 2) == 0) {
1445 #ifdef WITH_ASCEND_BINARY
1450 vp->length = size >> 1;
1451 us = talloc_array(vp, uint8_t, vp->length);
1456 if ((size & 0x01) != 0) {
1457 fr_strerror_printf("Hex string is not an even length string");
1461 if (fr_hex2bin(us, cp, vp->length) != vp->length) {
1462 fr_strerror_printf("Invalid hex data");
1467 pairstrcpy(vp, value);
1472 if (ifid_aton(value, (void *) &vp->vp_ifid) == NULL) {
1473 fr_strerror_printf("Failed to parse interface-id string \"%s\"", value);
1479 case PW_TYPE_IPV6ADDR:
1483 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
1486 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1488 fr_strerror_printf("failed to parse IPv6 address "
1489 "string \"%s\": %s", value, buffer);
1492 vp->vp_ipv6addr = ipaddr.ipaddr.ip6addr;
1493 vp->length = 16; /* length of IPv6 address */
1497 case PW_TYPE_IPV6PREFIX:
1498 p = strchr(value, '/');
1499 if (!p || ((p - value) >= 256)) {
1500 fr_strerror_printf("invalid IPv6 prefix string \"%s\"", value);
1503 unsigned int prefix;
1504 char buffer[256], *eptr;
1506 memcpy(buffer, value, p - value);
1507 buffer[p - value] = '\0';
1509 if (inet_pton(AF_INET6, buffer, vp->vp_ipv6prefix + 2) <= 0) {
1510 fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1514 prefix = strtoul(p + 1, &eptr, 10);
1515 if ((prefix > 128) || *eptr) {
1516 fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1519 vp->vp_ipv6prefix[1] = prefix;
1521 vp->length = 16 + 2;
1524 case PW_TYPE_IPV4PREFIX:
1525 p = strchr(value, '/');
1528 * 192.0.2.2 is parsed as if it was /32
1531 vp->vp_ipv4prefix[1] = 32;
1533 if (inet_pton(AF_INET, value, vp->vp_ipv4prefix + 2) <= 0) {
1534 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1537 vp->length = sizeof(vp->vp_ipv4prefix);
1542 * Otherwise parse the prefix
1544 if ((p - value) >= 256) {
1545 fr_strerror_printf("invalid IPv4 prefix string \"%s\"", value);
1548 unsigned int prefix;
1549 char buffer[256], *eptr;
1551 memcpy(buffer, value, p - value);
1552 buffer[p - value] = '\0';
1554 if (inet_pton(AF_INET, buffer, vp->vp_ipv4prefix + 2) <= 0) {
1555 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1559 prefix = strtoul(p + 1, &eptr, 10);
1560 if ((prefix > 32) || *eptr) {
1561 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1564 vp->vp_ipv4prefix[1] = prefix;
1567 uint32_t addr, mask;
1569 memcpy(&addr, vp->vp_ipv4prefix + 2, sizeof(addr));
1571 mask <<= (32 - prefix);
1576 memcpy(vp->vp_ipv4prefix + 2, &addr, sizeof(addr));
1579 vp->length = sizeof(vp->vp_ipv4prefix);
1582 case PW_TYPE_ETHERNET:
1584 char const *c1, *c2;
1587 * Convert things which are obviously integers to Ethernet addresses
1589 * We assume the number is the bigendian representation of the
1592 if (fr_integer_check(value)) {
1593 uint64_t integer = htonll(atoll(value));
1595 memcpy(&vp->vp_ether, &integer, sizeof(vp->vp_ether));
1604 c2 = memchr(hextab, tolower((int) cp[0]), 16);
1606 } else if ((cp[1] != '\0') &&
1609 c1 = memchr(hextab, tolower((int) cp[0]), 16);
1610 c2 = memchr(hextab, tolower((int) cp[1]), 16);
1612 if (*cp == ':') cp++;
1616 if (!c1 || !c2 || (length >= sizeof(vp->vp_ether))) {
1617 fr_strerror_printf("failed to parse Ethernet address \"%s\"", value);
1620 vp->vp_ether[length] = ((c1-hextab)<<4) + (c2-hextab);
1628 * Crazy polymorphic (IPv4/IPv6) attribute type for WiMAX.
1630 * We try and make is saner by replacing the original
1631 * da, with either an IPv4 or IPv6 da type.
1633 * These are not dynamic da, and will have the same vendor
1634 * and attribute as the original.
1636 case PW_TYPE_COMBO_IP:
1638 DICT_ATTR const *da;
1640 if (inet_pton(AF_INET6, value, &vp->vp_ipv6addr) > 0) {
1641 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1644 fr_strerror_printf("Cannot find ipv6addr for %s", vp->da->name);
1648 vp->length = 16; /* length of IPv6 address */
1652 da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1655 fr_strerror_printf("Cannot find ipaddr for %s", vp->da->name);
1659 if (ip_hton(value, AF_INET, &ipaddr) < 0) {
1660 fr_strerror_printf("Failed to find IPv4 address for %s", value);
1664 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1672 case PW_TYPE_SIGNED: /* Damned code for 1 WiMAX attribute */
1673 vp->vp_signed = (int32_t) strtol(value, &p, 10);
1677 case PW_TYPE_TLV: /* don't use this! */
1678 if (strncasecmp(value, "0x", 2) != 0) {
1679 fr_strerror_printf("Invalid TLV specification");
1682 length = strlen(value + 2) / 2;
1683 if (vp->length < length) {
1684 TALLOC_FREE(vp->vp_tlv);
1686 vp->vp_tlv = talloc_array(vp, uint8_t, length);
1688 fr_strerror_printf("No memory");
1691 if (fr_hex2bin(vp->vp_tlv, value + 2, length) != length) {
1692 fr_strerror_printf("Invalid hex data in TLV");
1695 vp->length = length;
1702 fr_strerror_printf("unknown attribute type %d", vp->da->type);
1711 /** Create a valuepair from an ASCII attribute and value
1713 * Where the attribute name is in the form:
1715 * - Attr-%d.%d.%d...
1716 * - Vendor-%d-Attr-%d
1717 * - VendorName-Attr-%d
1719 * @param ctx for talloc
1720 * @param attribute name to parse.
1721 * @param value to parse (must be a hex string).
1722 * @param op to assign to new valuepair.
1723 * @return new valuepair or NULL on error.
1725 static VALUE_PAIR *pairmake_any(TALLOC_CTX *ctx,
1726 char const *attribute, char const *value,
1730 DICT_ATTR const *da;
1735 da = dict_attrunknownbyname(attribute, true);
1736 if (!da) return NULL;
1739 * Unknown attributes MUST be of type 'octets'
1741 if (value && (strncasecmp(value, "0x", 2) != 0)) {
1742 fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
1743 "string, not \"%s\"", attribute, value);
1745 dict_attr_free(&da);
1750 * We've now parsed the attribute properly, Let's create
1751 * it. This next stop also looks the attribute up in the
1752 * dictionary, and creates the appropriate type for it.
1754 vp = pairalloc(ctx, da);
1756 dict_attr_free(&da);
1760 vp->op = (op == 0) ? T_OP_EQ : op;
1762 if (!value) return vp;
1764 size = strlen(value + 2);
1765 vp->length = size >> 1;
1766 data = talloc_array(vp, uint8_t, vp->length);
1768 if (fr_hex2bin(data, value + 2, size) != vp->length) {
1769 fr_strerror_printf("Invalid hex string");
1774 vp->vp_octets = data;
1780 /** Create a VALUE_PAIR from ASCII strings
1782 * Converts an attribute string identifier (with an optional tag qualifier)
1783 * and value string into a VALUE_PAIR.
1785 * The string value is parsed according to the type of VALUE_PAIR being created.
1787 * @param[in] ctx for talloc
1788 * @param[in] vps list where the attribute will be added (optional)
1789 * @param[in] attribute name.
1790 * @param[in] value attribute value (may be NULL if value will be set later).
1791 * @param[in] op to assign to new VALUE_PAIR.
1792 * @return a new VALUE_PAIR.
1794 VALUE_PAIR *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps,
1795 char const *attribute, char const *value, FR_TOKEN op)
1797 DICT_ATTR const *da;
1803 char const *attrname = attribute;
1806 * Check for tags in 'Attribute:Tag' format.
1811 ts = strrchr(attribute, ':');
1813 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1818 strlcpy(buffer, attribute, sizeof(buffer));
1820 ts = strrchr(attrname, ':');
1821 if (!ts) return NULL;
1823 /* Colon found with something behind it */
1824 if (ts[1] == '*' && ts[2] == 0) {
1825 /* Wildcard tag for check items */
1828 } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1829 /* It's not a wild card tag */
1830 tag = strtol(ts + 1, &tc, 0);
1831 if (tc && !*tc && TAG_VALID_ZERO(tag))
1835 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1842 * It's not found in the dictionary, so we use
1843 * another method to create the attribute.
1845 da = dict_attrbyname(attrname);
1847 vp = pairmake_any(ctx, attrname, value, op);
1848 if (vp && vps) pairadd(vps, vp);
1852 /* Check for a tag in the 'Merit' format of:
1853 * :Tag:Value. Print an error if we already found
1854 * a tag in the Attribute.
1857 if (value && (*value == ':' && da->flags.has_tag)) {
1858 /* If we already found a tag, this is invalid */
1860 fr_strerror_printf("Duplicate tag %s for attribute %s",
1862 DEBUG("Duplicate tag %s for attribute %s\n",
1866 /* Colon found and attribute allows a tag */
1867 if (value[1] == '*' && value[2] == ':') {
1868 /* Wildcard tag for check items */
1873 tag = strtol(value + 1, &tc, 0);
1874 if (tc && *tc==':' && TAG_VALID_ZERO(tag))
1880 vp = pairalloc(ctx, da);
1885 vp->op = (op == 0) ? T_OP_EQ : op;
1893 case T_OP_CMP_FALSE:
1894 vp->vp_strvalue = NULL;
1896 value = NULL; /* ignore it! */
1900 * Regular expression comparison of integer attributes
1901 * does a STRING comparison of the names of their
1902 * integer attributes.
1904 case T_OP_REG_EQ: /* =~ */
1905 case T_OP_REG_NE: /* !~ */
1907 fr_strerror_printf("Regular expressions are not supported");
1913 * Someone else will fill in the value.
1923 compare = regcomp(®, value, REG_EXTENDED);
1925 regerror(compare, ®, buffer, sizeof(buffer));
1926 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
1932 vp = pairmake(ctx, NULL, attribute, NULL, op);
1933 if (!vp) return NULL;
1935 if (pairmark_xlat(vp, value) < 0) {
1940 value = NULL; /* ignore it */
1946 * FIXME: if (strcasecmp(attribute, vp->da->name) != 0)
1947 * then the user MAY have typed in the attribute name
1948 * as Vendor-%d-Attr-%d, and the value MAY be octets.
1950 * We probably want to fix pairparsevalue to accept
1951 * octets as values for any attribute.
1953 if (value && !pairparsevalue(vp, value)) {
1958 if (vps) pairadd(vps, vp);
1962 /** Mark a valuepair for xlat expansion
1964 * Copies xlat source (unprocessed) string to valuepair value,
1965 * and sets value type.
1967 * @param vp to mark for expansion.
1968 * @param value to expand.
1969 * @return 0 if marking succeeded or -1 if vp already had a value, or OOM.
1971 int pairmark_xlat(VALUE_PAIR *vp, char const *value)
1976 * valuepair should not already have a value.
1978 if (vp->type != VT_NONE) {
1982 raw = talloc_strdup(vp, value);
1988 vp->value.xlat = raw;
1994 /** Read a single valuepair from a buffer, and advance the pointer
1996 * Sets *eol to T_EOL if end of line was encountered.
1998 * @param[in,out] ptr to read from and update.
1999 * @param[out] raw The struct to write the raw VALUE_PAIR to.
2000 * @return the last token read.
2002 FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw)
2006 FR_TOKEN ret = T_OP_INVALID, next, quote;
2009 if (!ptr || !*ptr || !raw) {
2010 fr_strerror_printf("Invalid arguments");
2011 return T_OP_INVALID;
2015 * Skip leading spaces
2018 while ((*p == ' ') || (*p == '\t')) p++;
2021 fr_strerror_printf("No token read where we expected "
2022 "an attribute name");
2023 return T_OP_INVALID;
2027 fr_strerror_printf("Read a comment instead of a token");
2033 * Try to get the attribute name.
2038 uint8_t const *t = (uint8_t const *) p;
2040 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2042 fr_strerror_printf("Attribute name too long");
2043 return T_OP_INVALID;
2047 * Only ASCII is allowed, and only a subset of that.
2049 if ((*t < 32) || (*t >= 128)) {
2051 fr_strerror_printf("Invalid attribute name");
2052 return T_OP_INVALID;
2056 * This is arguably easier than trying to figure
2057 * out which operators come after the attribute
2058 * name. Yes, our "lexer" is bad.
2060 if (!dict_attr_allowed_chars[(int) *t]) {
2068 * ASCII, but not a valid attribute name.
2070 if (!*raw->l_opand) goto invalid;
2073 * Look for tag (:#). This is different from :=, which
2076 if ((*p == ':') && (isdigit((int) p[1]))) {
2077 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2082 while (isdigit((int) *p)) {
2083 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2093 /* Now we should have an operator here. */
2094 raw->op = gettoken(ptr, buf, sizeof(buf));
2095 if (raw->op < T_EQSTART || raw->op > T_EQEND) {
2096 fr_strerror_printf("Expecting operator");
2098 return T_OP_INVALID;
2102 * Read value. Note that empty string values are allowed
2104 quote = gettoken(ptr, raw->r_opand, sizeof(raw->r_opand));
2105 if (quote == T_EOL) {
2106 fr_strerror_printf("Failed to get value");
2108 return T_OP_INVALID;
2112 * Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
2116 next = gettoken(&p, buf, sizeof(buf));
2127 fr_strerror_printf("Expected end of line or comma");
2128 return T_OP_INVALID;
2136 case T_DOUBLE_QUOTED_STRING:
2138 * Only report as double quoted if it contained valid
2139 * a valid xlat expansion.
2141 p = strchr(raw->r_opand, '%');
2142 if (p && (p[1] == '{')) {
2145 raw->quote = T_SINGLE_QUOTED_STRING;
2158 /** Read one line of attribute/value pairs into a list.
2160 * The line may specify multiple attributes separated by commas.
2162 * @note If the function returns T_OP_INVALID, an error has occurred and
2163 * @note the valuepair list should probably be freed.
2165 * @param ctx for talloc
2166 * @param buffer to read valuepairs from.
2167 * @param list where the parsed VALUE_PAIRs will be appended.
2168 * @return the last token parsed, or T_OP_INVALID
2170 FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **list)
2172 VALUE_PAIR *vp, *head, **tail;
2174 FR_TOKEN last_token = T_OP_INVALID;
2175 FR_TOKEN previous_token;
2179 * We allow an empty line.
2181 if (buffer[0] == 0) {
2190 raw.l_opand[0] = '\0';
2191 raw.r_opand[0] = '\0';
2193 previous_token = last_token;
2195 last_token = pairread(&p, &raw);
2196 if (last_token == T_OP_INVALID) break;
2198 if (raw.quote == T_DOUBLE_QUOTED_STRING) {
2199 vp = pairmake(ctx, NULL, raw.l_opand, NULL, raw.op);
2201 last_token = T_OP_INVALID;
2204 if (pairmark_xlat(vp, raw.r_opand) < 0) {
2206 last_token = T_OP_INVALID;
2210 vp = pairmake(ctx, NULL, raw.l_opand, raw.r_opand, raw.op);
2212 last_token = T_OP_INVALID;
2218 tail = &((*tail)->next);
2219 } while (*p && (last_token == T_COMMA));
2222 * Don't tell the caller that there was a comment.
2224 if (last_token == T_HASH) {
2225 last_token = previous_token;
2228 if (last_token == T_OP_INVALID) {
2231 pairadd(list, head);
2235 * And return the last token which we read.
2241 * Read valuepairs from the fp up to End-Of-File.
2243 * Hmm... this function is only used by radclient..
2245 VALUE_PAIR *readvp2(TALLOC_CTX *ctx, FILE *fp, int *pfiledone, char const *errprefix)
2248 FR_TOKEN last_token = T_EOL;
2255 while (!error && fgets(buf, sizeof(buf), fp) != NULL) {
2257 * If we get a '\n' by itself, we assume that's
2258 * the end of that VP
2260 if ((buf[0] == '\n') && (list)) {
2263 if ((buf[0] == '\n') && (!list)) {
2268 * Comments get ignored
2270 if (buf[0] == '#') continue;
2273 * Read all of the attributes on the current line.
2276 last_token = userparse(ctx, buf, &vp);
2278 if (last_token != T_EOL) {
2279 fr_perror("%s", errprefix);
2290 if (error) pairfree(&list);
2294 return error ? NULL: list;
2298 * We leverage the fact that IPv4 and IPv6 prefixes both
2299 * have the same format:
2301 * reserved, prefix-len, data...
2303 static int paircmp_cidr(FR_TOKEN op, int bytes, uint8_t const *one, uint8_t const *two)
2309 * Handle the case of netmasks being identical.
2311 if (one[1] == two[1]) {
2314 compare = memcmp(one + 2, two + 2, bytes);
2317 * If they're identical return true for
2320 if ((compare == 0) &&
2321 ((op == T_OP_CMP_EQ) ||
2328 * Everything else returns false.
2330 * 10/8 == 24/8 --> false
2331 * 10/8 <= 24/8 --> false
2332 * 10/8 >= 24/8 --> false
2338 * Netmasks are different. That limits the
2339 * possible results, based on the operator.
2349 case T_OP_LT: /* 192/8 < 192.168/16 --> false */
2350 if (one[1] < two[1]) {
2356 case T_OP_GT: /* 192/16 > 192.168/8 --> false */
2357 if (one[1] > two[1]) {
2366 if (one[1] < two[1]) {
2373 * Do the check byte by byte. If the bytes are
2374 * identical, it MAY be a match. If they're different,
2375 * it is NOT a match.
2378 while (i < (2 + bytes)) {
2380 * All leading bytes are identical.
2382 if (common == 0) return true;
2385 * Doing bitmasks takes more work.
2387 if (common < 8) break;
2389 if (one[i] != two[i]) return false;
2397 mask <<= (8 - common);
2401 if ((one[i] & mask) == ((two[i] & mask))) {
2409 * Compare two pairs, using the operator from "one".
2411 * i.e. given two attributes, it does:
2413 * (two->data) (one->operator) (one->data)
2415 * e.g. "foo" != "bar"
2417 * Returns true (comparison is true), or false (comparison is not true);
2419 int paircmp(VALUE_PAIR *one, VALUE_PAIR *two)
2428 return (two != NULL);
2430 case T_OP_CMP_FALSE:
2431 return (two == NULL);
2434 * One is a regex, compile it, print two to a string,
2435 * and then do string comparisons.
2444 char buffer[MAX_STRING_LEN * 4 + 1];
2446 compare = regcomp(®, one->vp_strvalue, REG_EXTENDED);
2448 regerror(compare, ®, buffer, sizeof(buffer));
2449 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
2450 one->da->name, buffer);
2454 vp_prints_value(buffer, sizeof(buffer), two, 0);
2457 * Don't care about substring matches,
2460 compare = regexec(®, buffer, 0, NULL, 0);
2463 if (one->op == T_OP_REG_EQ) return (compare == 0);
2464 return (compare != 0);
2468 default: /* we're OK */
2472 return paircmp_op(two, one->op, one);
2475 /* Compare two attributes
2477 * @param[in] one the first attribute
2478 * @param[in] op the operator for comparison
2479 * @param[in] two the second attribute
2480 * @return < 0 if one is less than two, 0 if both are equal, > 0 if one is more than two.
2482 int paircmp_op(VALUE_PAIR const *one, FR_TOKEN op, VALUE_PAIR const *two)
2490 * Can't compare two attributes of differing types
2492 * FIXME: maybe do checks for IP OP IP/mask ??
2494 if (one->da->type != two->da->type) {
2495 return one->da->type - two->da->type;
2499 * After doing the previous check for special comparisons,
2500 * do the per-type comparison here.
2502 switch (one->da->type) {
2503 case PW_TYPE_ABINARY:
2504 case PW_TYPE_OCTETS:
2508 if (one->length > two->length) {
2509 length = one->length;
2511 length = two->length;
2515 compare = memcmp(one->vp_octets, two->vp_octets,
2517 if (compare != 0) break;
2521 * Contents are the same. The return code
2522 * is therefore the difference in lengths.
2524 * i.e. "0x00" is smaller than "0x0000"
2526 compare = one->length - two->length;
2530 case PW_TYPE_STRING:
2531 compare = strcmp(one->vp_strvalue, two->vp_strvalue);
2536 case PW_TYPE_INTEGER:
2538 if (one->vp_integer < two->vp_integer) {
2540 } else if (one->vp_integer == two->vp_integer) {
2547 case PW_TYPE_SIGNED:
2548 if (one->vp_signed < two->vp_signed) {
2550 } else if (one->vp_signed == two->vp_signed) {
2557 case PW_TYPE_INTEGER64:
2559 * Don't want integer overflow!
2561 if (one->vp_integer64 < two->vp_integer64) {
2563 } else if (one->vp_integer64 > two->vp_integer64) {
2570 case PW_TYPE_ETHERNET:
2571 compare = memcmp(&one->vp_ether, &two->vp_ether,
2572 sizeof(one->vp_ether));
2575 case PW_TYPE_IPADDR:
2576 if (ntohl(one->vp_ipaddr) < ntohl(two->vp_ipaddr)) {
2578 } else if (one->vp_ipaddr == two->vp_ipaddr) {
2585 case PW_TYPE_IPV6ADDR:
2586 compare = memcmp(&one->vp_ipv6addr, &two->vp_ipv6addr,
2587 sizeof(one->vp_ipv6addr));
2590 case PW_TYPE_IPV6PREFIX:
2591 return paircmp_cidr(op, 16,
2592 (uint8_t const *) &one->vp_ipv6prefix,
2593 (uint8_t const *) &two->vp_ipv6prefix);
2595 case PW_TYPE_IPV4PREFIX:
2596 return paircmp_cidr(op, 4,
2597 (uint8_t const *) &one->vp_ipv4prefix,
2598 (uint8_t const *) &two->vp_ipv4prefix);
2601 compare = memcmp(&one->vp_ifid, &two->vp_ifid, sizeof(one->vp_ifid));
2605 * None of the types below should be in the REQUEST
2607 case PW_TYPE_COMBO_IP: /* This should of been converted into IPADDR/IPV6ADDR */
2609 case PW_TYPE_EXTENDED:
2610 case PW_TYPE_LONG_EXTENDED:
2613 case PW_TYPE_INVALID: /* We should never see these */
2615 fr_assert(0); /* unknown type */
2619 * Do NOT add a default here, as new types are added
2620 * static analysis will warn us they're not handled
2625 * Now do the operator comparison.
2629 return (compare == 0);
2632 return (compare != 0);
2635 return (compare < 0);
2638 return (compare > 0);
2641 return (compare <= 0);
2644 return (compare >= 0);
2651 /** Copy data into an "octets" data type.
2653 * @param[in,out] vp to update
2654 * @param[in] src data to copy
2655 * @param[in] size of the data
2657 void pairmemcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
2663 p = talloc_memdup(vp, src, size);
2666 memcpy(&q, &vp->vp_octets, sizeof(q));
2673 /** Reparent an allocated octet buffer to a VALUE_PAIR
2675 * @param[in,out] vp to update
2676 * @param[in] src buffer to steal.
2678 void pairmemsteal(VALUE_PAIR *vp, uint8_t const *src)
2683 memcpy(&q, &vp->vp_octets, sizeof(q));
2686 vp->vp_octets = talloc_steal(vp, src);
2688 vp->length = talloc_array_length(vp->vp_octets);
2691 /** Reparent an allocated char buffer to a VALUE_PAIR
2693 * @param[in,out] vp to update
2694 * @param[in] src buffer to steal.
2696 void pairstrsteal(VALUE_PAIR *vp, char const *src)
2701 memcpy(&q, &vp->vp_octets, sizeof(q));
2704 vp->vp_strvalue = talloc_steal(vp, src);
2706 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2709 /** Copy data into an "string" data type.
2711 * @param[in,out] vp to update
2712 * @param[in] src data to copy
2714 void pairstrcpy(VALUE_PAIR *vp, char const *src)
2720 p = talloc_strdup(vp, src);
2723 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2726 vp->vp_strvalue = p;
2728 vp->length = talloc_array_length(vp->vp_strvalue) - 1;
2732 /** Print data into an "string" data type.
2734 * @param[in,out] vp to update
2735 * @param[in] fmt the format string
2737 void pairsprintf(VALUE_PAIR *vp, char const *fmt, ...)
2745 p = talloc_vasprintf(vp, fmt, ap);
2750 memcpy(&q, &vp->vp_strvalue, sizeof(q));
2753 vp->vp_strvalue = p;
2756 vp->length = talloc_array_length(vp->vp_strvalue) - 1;