Merge tag 'release_3_0_12' into branch moonshot-fr-3.0.12-upgrade.
[freeradius.git] / src / lib / pair.c
1 /*
2  * pair.c       Functions to handle VALUE_PAIRs
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2000,2006  The FreeRADIUS server project
21  */
22
23 RCSID("$Id$")
24
25 #include <freeradius-devel/libradius.h>
26 #include <freeradius-devel/regex.h>
27
28 #include <ctype.h>
29
30 /** Free a VALUE_PAIR
31  *
32  * @note Do not call directly, use talloc_free instead.
33  *
34  * @param vp to free.
35  * @return 0
36  */
37 static int _fr_pair_free(VALUE_PAIR *vp) {
38 #ifndef NDEBUG
39         vp->vp_integer = 0xf4eef4ee;
40 #endif
41
42 #ifdef TALLOC_DEBUG
43         talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL);
44 #endif
45         return 0;
46 }
47
48 static VALUE_PAIR *fr_pair_alloc(TALLOC_CTX *ctx)
49 {
50         VALUE_PAIR *vp;
51
52         vp = talloc_zero(ctx, VALUE_PAIR);
53         if (!vp) {
54                 fr_strerror_printf("Out of memory");
55                 return NULL;
56         }
57
58         vp->op = T_OP_EQ;
59         vp->tag = TAG_ANY;
60         vp->type = VT_NONE;
61
62         talloc_set_destructor(vp, _fr_pair_free);
63
64         return vp;
65 }
66
67
68 /** Dynamically allocate a new attribute
69  *
70  * Allocates a new attribute and a new dictionary attr if no DA is provided.
71  *
72  * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
73  * @param[in] da Specifies the dictionary attribute to build the VP from.
74  * @return a new value pair or NULL if an error occurred.
75  */
76 VALUE_PAIR *fr_pair_afrom_da(TALLOC_CTX *ctx, DICT_ATTR const *da)
77 {
78         VALUE_PAIR *vp;
79
80         /*
81          *      Caller must specify a da else we don't know what the attribute type is.
82          */
83         if (!da) {
84                 fr_strerror_printf("Invalid arguments");
85                 return NULL;
86         }
87
88         vp = fr_pair_alloc(ctx);
89         if (!vp) {
90                 fr_strerror_printf("Out of memory");
91                 return NULL;
92         }
93
94         /*
95          *      Use the 'da' to initialize more fields.
96          */
97         vp->da = da;
98         vp->vp_length = da->flags.length;
99
100         return vp;
101 }
102
103 /** Create a new valuepair
104  *
105  * If attr and vendor match a dictionary entry then a VP with that DICT_ATTR
106  * will be returned.
107  *
108  * If attr or vendor are uknown will call dict_attruknown to create a dynamic
109  * DICT_ATTR of PW_TYPE_OCTETS.
110  *
111  * Which type of DICT_ATTR the VALUE_PAIR was created with can be determined by
112  * checking @verbatim vp->da->flags.is_unknown @endverbatim.
113  *
114  * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
115  * @param[in] attr number.
116  * @param[in] vendor number.
117  * @return the new valuepair or NULL on error.
118  */
119 VALUE_PAIR *fr_pair_afrom_num(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
120 {
121         DICT_ATTR const *da;
122
123         da = dict_attrbyvalue(attr, vendor);
124         if (!da) {
125                 VALUE_PAIR *vp;
126
127                 vp = fr_pair_alloc(ctx);
128                 if (!vp) return NULL;
129
130                 /*
131                  *      Ensure that the DA is parented by the VP.
132                  */
133                 da = dict_unknown_afrom_fields(vp, attr, vendor);
134                 if (!da) {
135                         talloc_free(vp);
136                         return NULL;
137                 }
138
139                 vp->da = da;
140                 return vp;
141         }
142
143         return fr_pair_afrom_da(ctx, da);
144 }
145
146 /** Free memory used by a valuepair list.
147  *
148  * @todo TLV: needs to free all dependents of each VP freed.
149  */
150 void fr_pair_list_free(VALUE_PAIR **vps)
151 {
152         VALUE_PAIR      *vp;
153         vp_cursor_t     cursor;
154
155         if (!vps || !*vps) {
156                 return;
157         }
158
159         for (vp = fr_cursor_init(&cursor, vps);
160              vp;
161              vp = fr_cursor_next(&cursor)) {
162                 VERIFY_VP(vp);
163                 talloc_free(vp);
164         }
165
166         *vps = NULL;
167 }
168
169 /** Mark malformed or unrecognised attributed as unknown
170  *
171  * @param vp to change DICT_ATTR of.
172  * @return 0 on success (or if already unknown) else -1 on error.
173  */
174 int fr_pair_to_unknown(VALUE_PAIR *vp)
175 {
176         DICT_ATTR const *da;
177
178         VERIFY_VP(vp);
179         if (vp->da->flags.is_unknown) {
180                 return 0;
181         }
182
183         da = dict_unknown_afrom_fields(vp, vp->da->attr, vp->da->vendor);
184         if (!da) {
185                 return -1;
186         }
187
188         vp->da = da;
189
190         return 0;
191 }
192
193 /** Find the pair with the matching DAs
194  *
195  */
196 VALUE_PAIR *fr_pair_find_by_da(VALUE_PAIR *vp, DICT_ATTR const *da, int8_t tag)
197 {
198         vp_cursor_t     cursor;
199
200         if(!fr_assert(da)) {
201                  return NULL;
202         }
203
204         (void) fr_cursor_init(&cursor, &vp);
205         return fr_cursor_next_by_da(&cursor, da, tag);
206 }
207
208
209 /** Find the pair with the matching attribute
210  *
211  * @todo should take DAs and do a pointer comparison.
212  */
213 VALUE_PAIR *fr_pair_find_by_num(VALUE_PAIR *vp, unsigned int attr, unsigned int vendor, int8_t tag)
214 {
215         vp_cursor_t     cursor;
216
217         /* List head may be NULL if it contains no VPs */
218         if (!vp) return NULL;
219
220         VERIFY_LIST(vp);
221
222         (void) fr_cursor_init(&cursor, &vp);
223         return fr_cursor_next_by_num(&cursor, attr, vendor, tag);
224 }
225
226 /** Delete matching pairs
227  *
228  * Delete matching pairs from the attribute list.
229  *
230  * @param[in,out] first VP in list.
231  * @param[in] attr to match.
232  * @param[in] vendor to match.
233  * @param[in] tag to match. TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
234  *
235  * @todo should take DAs and do a point comparison.
236  */
237 void fr_pair_delete_by_num(VALUE_PAIR **first, unsigned int attr, unsigned int vendor, int8_t tag)
238 {
239         VALUE_PAIR *i, *next;
240         VALUE_PAIR **last = first;
241
242         for(i = *first; i; i = next) {
243                 VERIFY_VP(i);
244                 next = i->next;
245                 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
246                     (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
247                         *last = next;
248                         talloc_free(i);
249                 } else {
250                         last = &i->next;
251                 }
252         }
253 }
254
255 /** Add a VP to the end of the list.
256  *
257  * Locates the end of 'first', and links an additional VP 'add' at the end.
258  *
259  * @param[in] first VP in linked list. Will add new VP to the end of this list.
260  * @param[in] add VP to add to list.
261  */
262 void fr_pair_add(VALUE_PAIR **first, VALUE_PAIR *add)
263 {
264         VALUE_PAIR *i;
265
266         if (!add) return;
267
268         VERIFY_VP(add);
269
270         if (*first == NULL) {
271                 *first = add;
272                 return;
273         }
274
275         for (i = *first; i->next; i = i->next) {
276 #ifdef WITH_VERIFY_PTR
277                 VERIFY_VP(i);
278                 /*
279                  *      The same VP should never by added multiple times
280                  *      to the same list.
281                  */
282                 fr_assert(i != add);
283 #endif
284         }
285
286         i->next = add;
287 }
288
289 /** Replace all matching VPs
290  *
291  * Walks over 'first', and replaces the first VP that matches 'replace'.
292  *
293  * @note Memory used by the VP being replaced will be freed.
294  * @note Will not work with unknown attributes.
295  *
296  * @param[in,out] first VP in linked list. Will search and replace in this list.
297  * @param[in] replace VP to replace.
298  */
299 void fr_pair_replace(VALUE_PAIR **first, VALUE_PAIR *replace)
300 {
301         VALUE_PAIR *i, *next;
302         VALUE_PAIR **prev = first;
303
304         VERIFY_VP(replace);
305
306         if (*first == NULL) {
307                 *first = replace;
308                 return;
309         }
310
311         /*
312          *      Not an empty list, so find item if it is there, and
313          *      replace it. Note, we always replace the first one, and
314          *      we ignore any others that might exist.
315          */
316         for(i = *first; i; i = next) {
317                 VERIFY_VP(i);
318                 next = i->next;
319
320                 /*
321                  *      Found the first attribute, replace it,
322                  *      and return.
323                  */
324                 if ((i->da == replace->da) && (!i->da->flags.has_tag || TAG_EQ(replace->tag, i->tag))) {
325                         *prev = replace;
326
327                         /*
328                          *      Should really assert that replace->next == NULL
329                          */
330                         replace->next = next;
331                         talloc_free(i);
332                         return;
333                 }
334
335                 /*
336                  *      Point to where the attribute should go.
337                  */
338                 prev = &i->next;
339         }
340
341         /*
342          *      If we got here, we didn't find anything to replace, so
343          *      stopped at the last item, which we just append to.
344          */
345         *prev = replace;
346 }
347
348 int8_t fr_pair_cmp_by_da_tag(void const *a, void const *b)
349 {
350         VALUE_PAIR const *my_a = a;
351         VALUE_PAIR const *my_b = b;
352
353         VERIFY_VP(my_a);
354         VERIFY_VP(my_b);
355
356         uint8_t cmp;
357
358         cmp = fr_pointer_cmp(my_a->da, my_b->da);
359         if (cmp != 0) return cmp;
360
361         if (my_a->tag < my_b->tag) return -1;
362
363         if (my_a->tag > my_b->tag) return 1;
364
365         return 0;
366 }
367
368 static void fr_pair_list_sort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
369 {
370         VALUE_PAIR *fast;
371         VALUE_PAIR *slow;
372
373         /*
374          *      Stopping condition - no more elements left to split
375          */
376         if (!source || !source->next) {
377                 *front = source;
378                 *back = NULL;
379
380                 return;
381         }
382
383         /*
384          *      Fast advances twice as fast as slow, so when it gets to the end,
385          *      slow will point to the middle of the linked list.
386          */
387         slow = source;
388         fast = source->next;
389
390         while (fast) {
391                 fast = fast->next;
392                 if (fast) {
393                         slow = slow->next;
394                         fast = fast->next;
395                 }
396         }
397
398         *front = source;
399         *back = slow->next;
400         slow->next = NULL;
401 }
402
403 static VALUE_PAIR *fr_pair_list_sort_merge(VALUE_PAIR *a, VALUE_PAIR *b, fr_cmp_t cmp)
404 {
405         VALUE_PAIR *result = NULL;
406
407         if (!a) return b;
408         if (!b) return a;
409
410         /*
411          *      Compare the DICT_ATTRs and tags
412          */
413         if (cmp(a, b) <= 0) {
414                 result = a;
415                 result->next = fr_pair_list_sort_merge(a->next, b, cmp);
416         } else {
417                 result = b;
418                 result->next = fr_pair_list_sort_merge(a, b->next, cmp);
419         }
420
421         return result;
422 }
423
424 /** Sort a linked list of VALUE_PAIRs using merge sort
425  *
426  * @param[in,out] vps List of VALUE_PAIRs to sort.
427  * @param[in] cmp to sort with
428  */
429 void fr_pair_list_sort(VALUE_PAIR **vps, fr_cmp_t cmp)
430 {
431         VALUE_PAIR *head = *vps;
432         VALUE_PAIR *a;
433         VALUE_PAIR *b;
434
435         /*
436          *      If there's 0-1 elements it must already be sorted.
437          */
438         if (!head || !head->next) {
439                 return;
440         }
441
442         fr_pair_list_sort_split(head, &a, &b);  /* Split into sublists */
443         fr_pair_list_sort(&a, cmp);             /* Traverse left */
444         fr_pair_list_sort(&b, cmp);             /* Traverse right */
445
446         /*
447          *      merge the two sorted lists together
448          */
449         *vps = fr_pair_list_sort_merge(a, b, cmp);
450 }
451
452 /** Write an error to the library errorbuff detailing the mismatch
453  *
454  * Retrieve output with fr_strerror();
455  *
456  * @todo add thread specific talloc contexts.
457  *
458  * @param ctx a hack until we have thread specific talloc contexts.
459  * @param failed pair of attributes which didn't match.
460  */
461 void fr_pair_validate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2])
462 {
463         VALUE_PAIR const *filter = failed[0];
464         VALUE_PAIR const *list = failed[1];
465
466         char *value, *str;
467
468         (void) fr_strerror();   /* Clear any existing messages */
469
470         if (!fr_assert(!(!filter && !list))) return;
471
472         if (!list) {
473                 if (!filter) return;
474                 fr_strerror_printf("Attribute \"%s\" not found in list", filter->da->name);
475                 return;
476         }
477
478         if (!filter || (filter->da != list->da)) {
479                 fr_strerror_printf("Attribute \"%s\" not found in filter", list->da->name);
480                 return;
481         }
482
483         if (!TAG_EQ(filter->tag, list->tag)) {
484                 fr_strerror_printf("Attribute \"%s\" tag \"%i\" didn't match filter tag \"%i\"",
485                                    list->da->name, list->tag, filter->tag);
486                 return;
487         }
488
489
490         value = vp_aprints_value(ctx, list, '"');
491         str = vp_aprints(ctx, filter, '"');
492
493         fr_strerror_printf("Attribute value \"%s\" didn't match filter: %s", value, str);
494
495         talloc_free(str);
496         talloc_free(value);
497
498         return;
499 }
500
501 /** Uses fr_pair_cmp to verify all VALUE_PAIRs in list match the filter defined by check
502  *
503  * @note will sort both filter and list in place.
504  *
505  * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
506  *        May be NULL.
507  * @param filter attributes to check list against.
508  * @param list attributes, probably a request or reply
509  */
510 bool fr_pair_validate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
511 {
512         vp_cursor_t filter_cursor;
513         vp_cursor_t list_cursor;
514
515         VALUE_PAIR *check, *match;
516
517         if (!filter && !list) {
518                 return true;
519         }
520
521         /*
522          *      This allows us to verify the sets of validate and reply are equal
523          *      i.e. we have a validate rule which matches every reply attribute.
524          *
525          *      @todo this should be removed one we have sets and lists
526          */
527         fr_pair_list_sort(&filter, fr_pair_cmp_by_da_tag);
528         fr_pair_list_sort(&list, fr_pair_cmp_by_da_tag);
529
530         check = fr_cursor_init(&filter_cursor, &filter);
531         match = fr_cursor_init(&list_cursor, &list);
532         while (match || check) {
533                 /*
534                  *      Lists are of different lengths
535                  */
536                 if (!match || !check) goto mismatch;
537
538                 /*
539                  *      The lists are sorted, so if the first
540                  *      attributes aren't of the same type, then we're
541                  *      done.
542                  */
543                 if (!ATTRIBUTE_EQ(check, match)) goto mismatch;
544
545                 /*
546                  *      They're of the same type, but don't have the
547                  *      same values.  This is a problem.
548                  *
549                  *      Note that the RFCs say that for attributes of
550                  *      the same type, order is important.
551                  */
552                 if (fr_pair_cmp(check, match) != 1) goto mismatch;
553
554                 check = fr_cursor_next(&filter_cursor);
555                 match = fr_cursor_next(&list_cursor);
556         }
557
558         return true;
559
560 mismatch:
561         if (failed) {
562                 failed[0] = check;
563                 failed[1] = match;
564         }
565         return false;
566 }
567
568 /** Uses fr_pair_cmp to verify all VALUE_PAIRs in list match the filter defined by check
569  *
570  * @note will sort both filter and list in place.
571  *
572  * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
573  *        May be NULL.
574  * @param filter attributes to check list against.
575  * @param list attributes, probably a request or reply
576  */
577 bool fr_pair_validate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
578 {
579         vp_cursor_t filter_cursor;
580         vp_cursor_t list_cursor;
581
582         VALUE_PAIR *check, *last_check = NULL, *match = NULL;
583
584         if (!filter && !list) {
585                 return true;
586         }
587
588         /*
589          *      This allows us to verify the sets of validate and reply are equal
590          *      i.e. we have a validate rule which matches every reply attribute.
591          *
592          *      @todo this should be removed one we have sets and lists
593          */
594         fr_pair_list_sort(&filter, fr_pair_cmp_by_da_tag);
595         fr_pair_list_sort(&list, fr_pair_cmp_by_da_tag);
596
597         fr_cursor_init(&list_cursor, &list);
598         for (check = fr_cursor_init(&filter_cursor, &filter);
599              check;
600              check = fr_cursor_next(&filter_cursor)) {
601                 /*
602                  *      Were processing check attributes of a new type.
603                  */
604                 if (!ATTRIBUTE_EQ(last_check, check)) {
605                         /*
606                          *      Record the start of the matching attributes in the pair list
607                          *      For every other operator we require the match to be present
608                          */
609                         match = fr_cursor_next_by_da(&list_cursor, check->da, check->tag);
610                         if (!match) {
611                                 if (check->op == T_OP_CMP_FALSE) continue;
612                                 goto mismatch;
613                         }
614
615                         fr_cursor_init(&list_cursor, &match);
616                         last_check = check;
617                 }
618
619                 /*
620                  *      Now iterate over all attributes of the same type.
621                  */
622                 for (match = fr_cursor_first(&list_cursor);
623                      ATTRIBUTE_EQ(match, check);
624                      match = fr_cursor_next(&list_cursor)) {
625                         /*
626                          *      This attribute passed the filter
627                          */
628                         if (!fr_pair_cmp(check, match)) goto mismatch;
629                 }
630         }
631
632         return true;
633
634 mismatch:
635         if (failed) {
636                 failed[0] = check;
637                 failed[1] = match;
638         }
639         return false;
640 }
641
642 /** Copy a single valuepair
643  *
644  * Allocate a new valuepair and copy the da from the old vp.
645  *
646  * @param[in] ctx for talloc
647  * @param[in] vp to copy.
648  * @return a copy of the input VP or NULL on error.
649  */
650 VALUE_PAIR *fr_pair_copy(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
651 {
652         VALUE_PAIR *n;
653
654         if (!vp) return NULL;
655
656         VERIFY_VP(vp);
657
658         n = fr_pair_afrom_da(ctx, vp->da);
659         if (!n) return NULL;
660
661         memcpy(n, vp, sizeof(*n));
662
663         /*
664          *      If the DA is unknown, steal "n" to "ctx".  This does
665          *      nothing for "n", but will also copy the unknown "da".
666          */
667         if (n->da->flags.is_unknown) {
668                 fr_pair_steal(ctx, n);
669         }
670
671         n->next = NULL;
672
673         /*
674          *      If it's an xlat, copy the raw string and return early,
675          *      so we don't pre-expand or otherwise mangle the VALUE_PAIR.
676          */
677         if (vp->type == VT_XLAT) {
678                 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
679                 return n;
680         }
681
682         switch (vp->da->type) {
683         case PW_TYPE_OCTETS:
684                 n->vp_octets = NULL;    /* else fr_pair_value_memcpy will free vp's value */
685                 fr_pair_value_memcpy(n, vp->vp_octets, n->vp_length);
686                 break;
687
688         case PW_TYPE_STRING:
689                 n->vp_strvalue = NULL;  /* else pairstrnpy will free vp's value */
690                 fr_pair_value_bstrncpy(n, vp->vp_strvalue, n->vp_length);
691                 break;
692
693         default:
694                 break;
695         }
696
697         return n;
698 }
699
700 /** Copy a pairlist.
701  *
702  * Copy all pairs from 'from' regardless of tag, attribute or vendor.
703  *
704  * @param[in] ctx for new VALUE_PAIRs to be allocated in.
705  * @param[in] from whence to copy VALUE_PAIRs.
706  * @return the head of the new VALUE_PAIR list or NULL on error.
707  */
708 VALUE_PAIR *fr_pair_list_copy(TALLOC_CTX *ctx, VALUE_PAIR *from)
709 {
710         vp_cursor_t src, dst;
711
712         VALUE_PAIR *out = NULL, *vp;
713
714         fr_cursor_init(&dst, &out);
715         for (vp = fr_cursor_init(&src, &from);
716              vp;
717              vp = fr_cursor_next(&src)) {
718                 VERIFY_VP(vp);
719                 vp = fr_pair_copy(ctx, vp);
720                 if (!vp) {
721                         fr_pair_list_free(&out);
722                         return NULL;
723                 }
724                 fr_cursor_insert(&dst, vp); /* fr_pair_list_copy sets next pointer to NULL */
725         }
726
727         return out;
728 }
729
730 /** Copy matching pairs
731  *
732  * Copy pairs of a matching attribute number, vendor number and tag from the
733  * the input list to a new list, and returns the head of this list.
734  *
735  * @param[in] ctx for talloc
736  * @param[in] from whence to copy VALUE_PAIRs.
737  * @param[in] attr to match. If attribute PW_VENDOR_SPECIFIC and vendor 0,
738  *      will match (and therefore copy) only VSAs.
739  *      If attribute 0 and vendor 0  will match (and therefore copy) all
740  *      attributes.
741  * @param[in] vendor to match.
742  * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
743  * @return the head of the new VALUE_PAIR list or NULL on error.
744  */
745 VALUE_PAIR *fr_pair_list_copy_by_num(TALLOC_CTX *ctx, VALUE_PAIR *from,
746                                      unsigned int attr, unsigned int vendor, int8_t tag)
747 {
748         vp_cursor_t src, dst;
749
750         VALUE_PAIR *out = NULL, *vp;
751
752         fr_cursor_init(&dst, &out);
753         for (vp = fr_cursor_init(&src, &from);
754              vp;
755              vp = fr_cursor_next(&src)) {
756                 VERIFY_VP(vp);
757
758                 if (vp->da->flags.has_tag && !TAG_EQ(tag, vp->tag)) {
759                         continue;
760                 }
761
762                 /*
763                  *      Attr/vendor of 0 means "move them all".
764                  *      It's better than "fr_pair_copy(foo,bar);bar=NULL"
765                  */
766                 if ((attr == 0) && (vendor == 0)) {
767                         goto do_copy;
768                 }
769
770                 /*
771                  *      vendor=0, attr = PW_VENDOR_SPECIFIC means
772                  *      "match any vendor attribute".
773                  */
774                 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
775                         /*
776                          *      It's a VSA: copy it over.
777                          */
778                         if (vp->da->vendor != 0) goto do_copy;
779
780                         /*
781                          *      It's Vendor-Specific: copy it over.
782                          */
783                         if (vp->da->attr == attr) goto do_copy;
784
785                         /*
786                          *      It's not a VSA: ignore it.
787                          */
788                         continue;
789                 }
790
791                 if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
792                         continue;
793                 }
794
795         do_copy:
796                 vp = fr_pair_copy(ctx, vp);
797                 if (!vp) {
798                         fr_pair_list_free(&out);
799                         return NULL;
800                 }
801                 fr_cursor_insert(&dst, vp);
802         }
803
804         return out;
805 }
806
807 /** Steal one VP
808  *
809  * @param[in] ctx to move VALUE_PAIR into
810  * @param[in] vp VALUE_PAIR to move into the new context.
811  */
812 void fr_pair_steal(TALLOC_CTX *ctx, VALUE_PAIR *vp)
813 {
814         (void) talloc_steal(ctx, vp);
815
816         /*
817          *      The DA may be unknown.  If we're stealing the VPs to a
818          *      different context, copy the unknown DA.  We use the VP
819          *      as a context here instead of "ctx", so that when the
820          *      VP is freed, so is the DA.
821          *
822          *      Since we have no introspection into OTHER VPs using
823          *      the same DA, we can't have multiple VPs use the same
824          *      DA.  So we might as well tie it to this VP.
825          */
826         if (vp->da->flags.is_unknown) {
827                 DICT_ATTR *da;
828                 char *p;
829                 size_t size;
830
831                 size = talloc_get_size(vp->da);
832
833                 p = talloc_zero_array(vp, char, size);
834                 da = (DICT_ATTR *) p;
835                 talloc_set_type(p, DICT_ATTR);
836                 memcpy(da, vp->da, size);
837                 vp->da = da;
838         }
839 }
840
841 /** Move pairs from source list to destination list respecting operator
842  *
843  * @note This function does some additional magic that's probably not needed
844  *       in most places. Consider using radius_pairmove in server code.
845  *
846  * @note fr_pair_list_free should be called on the head of the source list to free
847  *       unmoved attributes (if they're no longer needed).
848  *
849  * @note Does not respect tags when matching.
850  *
851  * @param[in] ctx for talloc
852  * @param[in,out] to destination list.
853  * @param[in,out] from source list.
854  *
855  * @see radius_pairmove
856  */
857 void fr_pair_list_move(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from)
858 {
859         VALUE_PAIR *i, *found;
860         VALUE_PAIR *head_new, **tail_new;
861         VALUE_PAIR **tail_from;
862
863         if (!to || !from || !*from) return;
864
865         /*
866          *      We're editing the "to" list while we're adding new
867          *      attributes to it.  We don't want the new attributes to
868          *      be edited, so we create an intermediate list to hold
869          *      them during the editing process.
870          */
871         head_new = NULL;
872         tail_new = &head_new;
873
874         /*
875          *      We're looping over the "from" list, moving some
876          *      attributes out, but leaving others in place.
877          */
878         tail_from = from;
879         while ((i = *tail_from) != NULL) {
880                 VALUE_PAIR *j;
881
882                 VERIFY_VP(i);
883
884                 /*
885                  *      We never move Fall-Through.
886                  */
887                 if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
888                         tail_from = &(i->next);
889                         continue;
890                 }
891
892                 /*
893                  *      Unlike previous versions, we treat all other
894                  *      attributes as normal.  i.e. there's no special
895                  *      treatment for passwords or Hint.
896                  */
897
898                 switch (i->op) {
899                 /*
900                  *      Anything else are operators which
901                  *      shouldn't occur.  We ignore them, and
902                  *      leave them in place.
903                  */
904                 default:
905                         tail_from = &(i->next);
906                         continue;
907
908                 /*
909                  *      Add it to the "to" list, but only if
910                  *      it doesn't already exist.
911                  */
912                 case T_OP_EQ:
913                         found = fr_pair_find_by_da(*to, i->da, TAG_ANY);
914                         if (!found) goto do_add;
915
916                         tail_from = &(i->next);
917                         continue;
918
919                 /*
920                  *      Add it to the "to" list, and delete any attribute
921                  *      of the same vendor/attr which already exists.
922                  */
923                 case T_OP_SET:
924                         found = fr_pair_find_by_da(*to, i->da, TAG_ANY);
925                         if (!found) goto do_add;
926
927                         /*
928                          *      Do NOT call fr_pair_delete_by_num() here,
929                          *      due to issues with re-writing
930                          *      "request->username".
931                          *
932                          *      Everybody calls fr_pair_move, and
933                          *      expects it to work.  We can't
934                          *      update request->username here,
935                          *      so instead we over-write the
936                          *      vp that it's pointing to.
937                          */
938                         switch (found->da->type) {
939                         default:
940                                 j = found->next;
941                                 memcpy(found, i, sizeof(*found));
942                                 found->next = j;
943                                 break;
944
945                         case PW_TYPE_OCTETS:
946                                 fr_pair_value_memsteal(found, i->vp_octets);
947                                 i->vp_octets = NULL;
948                                 break;
949
950                         case PW_TYPE_STRING:
951                                 fr_pair_value_strsteal(found, i->vp_strvalue);
952                                 i->vp_strvalue = NULL;
953                                 found->tag = i->tag;
954                                 break;
955                         }
956
957                         /*
958                          *      Delete *all* of the attributes
959                          *      of the same number.
960                          */
961                         fr_pair_delete_by_num(&found->next,
962                                    found->da->attr,
963                                    found->da->vendor, TAG_ANY);
964
965                         /*
966                          *      Remove this attribute from the
967                          *      "from" list.
968                          */
969                         *tail_from = i->next;
970                         i->next = NULL;
971                         fr_pair_list_free(&i);
972                         continue;
973
974                 /*
975                  *      Move it from the old list and add it
976                  *      to the new list.
977                  */
978                 case T_OP_ADD:
979         do_add:
980                         *tail_from = i->next;
981                         i->next = NULL;
982                         *tail_new = i;
983                         fr_pair_steal(ctx, i);
984                         tail_new = &(i->next);
985                         continue;
986                 }
987         } /* loop over the "from" list. */
988
989         /*
990          *      Take the "new" list, and append it to the "to" list.
991          */
992         fr_pair_add(to, head_new);
993 }
994
995 /** Move matching pairs between VALUE_PAIR lists
996  *
997  * Move pairs of a matching attribute number, vendor number and tag from the
998  * the input list to the output list.
999  *
1000  * @note fr_pair_list_free should be called on the head of the old list to free unmoved
1001          attributes (if they're no longer needed).
1002  *
1003  * @param[in] ctx for talloc
1004  * @param[in,out] to destination list.
1005  * @param[in,out] from source list.
1006  * @param[in] attr to match. If attribute PW_VENDOR_SPECIFIC and vendor 0,
1007  *      will match (and therefore copy) only VSAs.
1008  *      If attribute 0 and vendor 0  will match (and therefore copy) all
1009  *      attributes.
1010  * @param[in] vendor to match.
1011  * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
1012  * @param[in] move if set to "true", VPs are moved.  If set to "false", VPs are copied, and the old one deleted.
1013  */
1014 static void fr_pair_list_move_by_num_internal(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
1015                                               unsigned int attr, unsigned int vendor, int8_t tag,
1016                                               bool move)
1017 {
1018         VALUE_PAIR *to_tail, *i, *next, *this;
1019         VALUE_PAIR *iprev = NULL;
1020
1021         /*
1022          *      Find the last pair in the "to" list and put it in "to_tail".
1023          *
1024          *      @todo: replace the "if" with "VALUE_PAIR **tail"
1025          */
1026         if (*to != NULL) {
1027                 to_tail = *to;
1028                 for(i = *to; i; i = i->next) {
1029                         VERIFY_VP(i);
1030                         to_tail = i;
1031                 }
1032         } else
1033                 to_tail = NULL;
1034
1035         /*
1036          *      Attr/vendor of 0 means "move them all".
1037          *      It's better than "fr_pair_add(foo,bar);bar=NULL"
1038          */
1039         if ((vendor == 0) && (attr == 0)) {
1040                 if (*to) {
1041                         to_tail->next = *from;
1042                 } else {
1043                         *to = *from;
1044                 }
1045
1046                 for (i = *from; i; i = i->next) {
1047                         fr_pair_steal(ctx, i);
1048                 }
1049
1050                 *from = NULL;
1051                 return;
1052         }
1053
1054         for(i = *from; i; i = next) {
1055                 VERIFY_VP(i);
1056                 next = i->next;
1057
1058                 if (i->da->flags.has_tag && !TAG_EQ(tag, i->tag)) {
1059                         iprev = i;
1060                         continue;
1061                 }
1062
1063                 /*
1064                  *      vendor=0, attr = PW_VENDOR_SPECIFIC means
1065                  *      "match any vendor attribute".
1066                  */
1067                 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
1068                         /*
1069                          *      It's a VSA: move it over.
1070                          */
1071                         if (i->da->vendor != 0) goto move;
1072
1073                         /*
1074                          *      It's Vendor-Specific: move it over.
1075                          */
1076                         if (i->da->attr == attr) goto move;
1077
1078                         /*
1079                          *      It's not a VSA: ignore it.
1080                          */
1081                         iprev = i;
1082                         continue;
1083                 }
1084
1085                 /*
1086                  *      If it isn't an exact match, ignore it.
1087                  */
1088                 if (!((i->da->vendor == vendor) && (i->da->attr == attr))) {
1089                         iprev = i;
1090                         continue;
1091                 }
1092
1093         move:
1094                 /*
1095                  *      Remove the attribute from the "from" list.
1096                  */
1097                 if (iprev)
1098                         iprev->next = next;
1099                 else
1100                         *from = next;
1101
1102                 if (move) {
1103                         this = i;
1104                 } else {
1105                         this = fr_pair_copy(ctx, i);
1106                 }
1107
1108                 /*
1109                  *      Add the attribute to the "to" list.
1110                  */
1111                 if (to_tail)
1112                         to_tail->next = this;
1113                 else
1114                         *to = this;
1115                 to_tail = this;
1116                 this->next = NULL;
1117
1118                 if (move) {
1119                         fr_pair_steal(ctx, i);
1120                 } else {
1121                         talloc_free(i);
1122                 }
1123         }
1124 }
1125
1126
1127 /** Move matching pairs between VALUE_PAIR lists
1128  *
1129  * Move pairs of a matching attribute number, vendor number and tag from the
1130  * the input list to the output list.
1131  *
1132  * @note pairs which are moved have their parent changed to ctx.
1133  *
1134  * @note fr_pair_list_free should be called on the head of the old list to free unmoved
1135          attributes (if they're no longer needed).
1136  *
1137  * @param[in] ctx for talloc
1138  * @param[in,out] to destination list.
1139  * @param[in,out] from source list.
1140  * @param[in] attr to match. If attribute PW_VENDOR_SPECIFIC and vendor 0,
1141  *      will match (and therefore copy) only VSAs.
1142  *      If attribute 0 and vendor 0  will match (and therefore copy) all
1143  *      attributes.
1144  * @param[in] vendor to match.
1145  * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
1146  */
1147 void fr_pair_list_move_by_num(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
1148                               unsigned int attr, unsigned int vendor, int8_t tag)
1149 {
1150         fr_pair_list_move_by_num_internal(ctx, to, from, attr, vendor, tag, true);
1151 }
1152
1153
1154 /** Copy / delete matching pairs between VALUE_PAIR lists
1155  *
1156  * Move pairs of a matching attribute number, vendor number and tag from the
1157  * the input list to the output list.  Like fr_pair_list_move_by_num(), but
1158  * instead does copy / delete.
1159  *
1160  * @note The pair is NOT reparented.  It is copied and deleted.
1161  *
1162  * @note fr_pair_list_free should be called on the head of the old list to free unmoved
1163          attributes (if they're no longer needed).
1164  *
1165  * @param[in] ctx for talloc
1166  * @param[in,out] to destination list.
1167  * @param[in,out] from source list.
1168  * @param[in] attr to match. If attribute PW_VENDOR_SPECIFIC and vendor 0,
1169  *      will match (and therefore copy) only VSAs.
1170  *      If attribute 0 and vendor 0  will match (and therefore copy) all
1171  *      attributes.
1172  * @param[in] vendor to match.
1173  * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
1174  */
1175 void fr_pair_list_mcopy_by_num(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from,
1176                               unsigned int attr, unsigned int vendor, int8_t tag)
1177 {
1178         fr_pair_list_move_by_num_internal(ctx, to, from, attr, vendor, tag, false);
1179 }
1180
1181
1182 /** Convert string value to native attribute value
1183  *
1184  * @param vp to assign value to.
1185  * @param value string to convert. Binary safe for variable length values if len is provided.
1186  * @param inlen may be < 0 in which case strlen(len) is used to determine length, else inline
1187  *        should be the length of the string or sub string to parse.
1188  * @return 0 on success -1 on error.
1189  */
1190 int fr_pair_value_from_str(VALUE_PAIR *vp, char const *value, size_t inlen)
1191 {
1192         ssize_t ret;
1193         PW_TYPE type;
1194         VERIFY_VP(vp);
1195
1196         if (!value) return -1;
1197
1198         type = vp->da->type;
1199
1200         /*
1201          *      We presume that the input data is from a double quoted
1202          *      string, and needs escaping
1203          */
1204         ret = value_data_from_str(vp, &vp->data, &type, vp->da, value, inlen, '"');
1205         if (ret < 0) return -1;
1206
1207         /*
1208          *      If we parsed to a different type than the DA associated with
1209          *      the VALUE_PAIR we now need to fixup the DA.
1210          */
1211         if (type != vp->da->type) {
1212                 DICT_ATTR const *da;
1213
1214                 da = dict_attrbytype(vp->da->attr, vp->da->vendor, type);
1215                 if (!da) {
1216                         fr_strerror_printf("Cannot find %s variant of attribute \"%s\"",
1217                                            fr_int2str(dict_attr_types, type, "<INVALID>"), vp->da->name);
1218                         return -1;
1219                 }
1220                 vp->da = da;
1221         }
1222
1223         vp->vp_length = ret;
1224         vp->type = VT_DATA;
1225
1226         VERIFY_VP(vp);
1227
1228         return 0;
1229 }
1230
1231 /** Use simple heuristics to create an VALUE_PAIR from an unknown address string
1232  *
1233  * If a DICT_ATTR is not provided for the address type, parsing will fail with
1234  * and error.
1235  *
1236  * @param ctx to allocate VP in.
1237  * @param value IPv4/IPv6 address/prefix string.
1238  * @param ipv4 dictionary attribute to use for an IPv4 address.
1239  * @param ipv6 dictionary attribute to use for an IPv6 address.
1240  * @param ipv4_prefix dictionary attribute to use for an IPv4 prefix.
1241  * @param ipv6_prefix dictionary attribute to use for an IPv6 prefix.
1242  * @return NULL on error, or new VALUE_PAIR.
1243  */
1244 VALUE_PAIR *fr_pair_afrom_ip_str(TALLOC_CTX *ctx, char const *value, DICT_ATTR *ipv4, DICT_ATTR *ipv6,
1245                                  DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix)
1246 {
1247         VALUE_PAIR *vp;
1248         DICT_ATTR *da = NULL;
1249
1250         if (!fr_assert(ipv4 || ipv6 || ipv4_prefix || ipv6_prefix)) {
1251                 return NULL;
1252         }
1253
1254         /* No point in repeating the work of fr_pair_value_from_str */
1255         if (strchr(value, ':')) {
1256                 if (strchr(value, '/')) {
1257                         da = ipv6_prefix;
1258                         goto finish;
1259                 }
1260
1261                 da = ipv6;
1262                 goto finish;
1263         }
1264
1265         if (strchr(value, '/')) {
1266                 da = ipv4_prefix;
1267                 goto finish;
1268         }
1269
1270         if (ipv4) {
1271                 da = ipv4;
1272                 goto finish;
1273         }
1274
1275         fr_strerror_printf("Invalid IP value specified, allowed types are %s%s%s%s",
1276                            ipv4 ? "ipaddr " : "", ipv6 ? "ipv6addr " : "",
1277                            ipv4_prefix ? "ipv4prefix " : "", ipv6_prefix ? "ipv6prefix" : "");
1278
1279 finish:
1280         vp = fr_pair_afrom_da(ctx, da);
1281         if (!vp) return NULL;
1282         if (fr_pair_value_from_str(vp, value, -1) < 0) {
1283                 talloc_free(vp);
1284                 return NULL;
1285         }
1286
1287         return vp;
1288 }
1289
1290
1291 /** Create a valuepair from an ASCII attribute and value
1292  *
1293  * Where the attribute name is in the form:
1294  *  - Attr-%d
1295  *  - Attr-%d.%d.%d...
1296  *  - Vendor-%d-Attr-%d
1297  *  - VendorName-Attr-%d
1298  *
1299  * @param ctx for talloc
1300  * @param attribute name to parse.
1301  * @param value to parse (must be a hex string).
1302  * @param op to assign to new valuepair.
1303  * @return new valuepair or NULL on error.
1304  */
1305 static VALUE_PAIR *fr_pair_make_unknown(TALLOC_CTX *ctx,
1306                                         char const *attribute, char const *value,
1307                                         FR_TOKEN op)
1308 {
1309         VALUE_PAIR      *vp, *vp2;
1310         DICT_ATTR const *da;
1311
1312         uint8_t         *data;
1313         size_t          size;
1314         ssize_t         len;
1315
1316         vp = fr_pair_alloc(ctx);
1317         if (!vp) return NULL;
1318
1319         vp->da = dict_unknown_afrom_str(vp, attribute);
1320         if (!vp->da) {
1321                 talloc_free(vp);
1322                 return NULL;
1323         }
1324
1325         /*
1326          *      No value.  Nothing more to do.
1327          */
1328         if (!value) return vp;
1329
1330         /*
1331          *      Unknown attributes MUST be of type 'octets'
1332          */
1333         if (strncasecmp(value, "0x", 2) != 0) {
1334                 fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
1335                                    "string, not \"%s\"", attribute, value);
1336                 talloc_free(vp);
1337                 return NULL;
1338         }
1339
1340         /*
1341          *      Convert the hex data to binary.
1342          */
1343         size = strlen(value + 2);
1344
1345         vp->vp_length = size >> 1;
1346         vp->vp_octets = data = talloc_array(vp, uint8_t, vp->vp_length);
1347         vp->type = VT_DATA;
1348         vp->op = (op == 0) ? T_OP_EQ : op;
1349
1350         if (fr_hex2bin(data, vp->vp_length, value + 2, size) != vp->vp_length) {
1351                 fr_strerror_printf("Invalid hex string");
1352                 talloc_free(vp);
1353                 return NULL;
1354         }
1355
1356         /*
1357          *      It's still unknown, return it as-is.
1358          */
1359         da = dict_attrbyvalue(vp->da->attr, vp->da->vendor);
1360         if (!da) return vp;
1361
1362         /*
1363          *      It MIGHT be known.  See if we can decode the raw data
1364          *      into a valid attribute.
1365          */
1366         len = data2vp(ctx, NULL, NULL, NULL, da,
1367                       vp->vp_octets, vp->vp_length, vp->vp_length,
1368                       &vp2);
1369         if (len <= 0) return vp;
1370
1371         /*
1372          *      It's still unknown.  Return the original VP.
1373          */
1374         if (vp2->da->flags.is_unknown) {
1375                 fr_pair_list_free(&vp2);
1376                 return vp;
1377         }
1378
1379         /*
1380          *      Didn't parse all of it.  Return the "unknown" one.
1381          *
1382          *      FIXME: it COULD have parsed 2 attributes and
1383          *      then not the third, so returning 2 "knowns"
1384          *      and 1 "unknown" is likely preferable.
1385          */
1386         if ((size_t) len < vp->vp_length) {
1387                 fr_pair_list_free(&vp2);
1388                 return vp;
1389         }
1390
1391         fr_pair_list_free(&vp);
1392         return vp2;
1393 }
1394
1395
1396 /** Create a VALUE_PAIR from ASCII strings
1397  *
1398  * Converts an attribute string identifier (with an optional tag qualifier)
1399  * and value string into a VALUE_PAIR.
1400  *
1401  * The string value is parsed according to the type of VALUE_PAIR being created.
1402  *
1403  * @param[in] ctx for talloc
1404  * @param[in] vps list where the attribute will be added (optional)
1405  * @param[in] attribute name.
1406  * @param[in] value attribute value (may be NULL if value will be set later).
1407  * @param[in] op to assign to new VALUE_PAIR.
1408  * @return a new VALUE_PAIR.
1409  */
1410 VALUE_PAIR *fr_pair_make(TALLOC_CTX *ctx, VALUE_PAIR **vps,
1411                         char const *attribute, char const *value, FR_TOKEN op)
1412 {
1413         DICT_ATTR const *da;
1414         VALUE_PAIR      *vp;
1415         char            *tc, *ts;
1416         int8_t          tag;
1417         bool            found_tag;
1418         char            buffer[256];
1419         char const      *attrname = attribute;
1420
1421         /*
1422          *    Check for tags in 'Attribute:Tag' format.
1423          */
1424         found_tag = false;
1425         tag = TAG_ANY;
1426
1427         ts = strrchr(attribute, ':');
1428         if (ts && !ts[1]) {
1429                 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1430                 return NULL;
1431         }
1432
1433         if (ts && ts[1]) {
1434                 strlcpy(buffer, attribute, sizeof(buffer));
1435                 attrname = buffer;
1436                 ts = strrchr(attrname, ':');
1437                 if (!ts) return NULL;
1438
1439                  /* Colon found with something behind it */
1440                  if (ts[1] == '*' && ts[2] == 0) {
1441                          /* Wildcard tag for check items */
1442                          tag = TAG_ANY;
1443                          *ts = '\0';
1444                  } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1445                          /* It's not a wild card tag */
1446                          tag = strtol(ts + 1, &tc, 0);
1447                          if (tc && !*tc && TAG_VALID_ZERO(tag))
1448                                  *ts = '\0';
1449                          else tag = TAG_ANY;
1450                  } else {
1451                          fr_strerror_printf("Invalid tag for attribute %s", attribute);
1452                          return NULL;
1453                  }
1454                  found_tag = true;
1455         }
1456
1457         /*
1458          *      It's not found in the dictionary, so we use
1459          *      another method to create the attribute.
1460          */
1461         da = dict_attrbyname(attrname);
1462         if (!da) {
1463                 vp = fr_pair_make_unknown(ctx, attrname, value, op);
1464                 if (vp && vps) fr_pair_add(vps, vp);
1465                 return vp;
1466         }
1467
1468         /*      Check for a tag in the 'Merit' format of:
1469          *      :Tag:Value.  Print an error if we already found
1470          *      a tag in the Attribute.
1471          */
1472
1473         if (value && (*value == ':' && da->flags.has_tag)) {
1474                 /* If we already found a tag, this is invalid */
1475                 if(found_tag) {
1476                         fr_strerror_printf("Duplicate tag %s for attribute %s",
1477                                    value, da->name);
1478                         DEBUG("Duplicate tag %s for attribute %s\n",
1479                                    value, da->name);
1480                         return NULL;
1481                 }
1482                 /* Colon found and attribute allows a tag */
1483                 if (value[1] == '*' && value[2] == ':') {
1484                        /* Wildcard tag for check items */
1485                        tag = TAG_ANY;
1486                        value += 3;
1487                 } else {
1488                        /* Real tag */
1489                        tag = strtol(value + 1, &tc, 0);
1490                        if (tc && *tc==':' && TAG_VALID_ZERO(tag))
1491                             value = tc + 1;
1492                        else tag = 0;
1493                 }
1494         }
1495
1496         vp = fr_pair_afrom_da(ctx, da);
1497         if (!vp) return NULL;
1498         vp->op = (op == 0) ? T_OP_EQ : op;
1499         vp->tag = tag;
1500
1501         switch (vp->op) {
1502         case T_OP_CMP_TRUE:
1503         case T_OP_CMP_FALSE:
1504                 vp->vp_strvalue = NULL;
1505                 vp->vp_length = 0;
1506                 value = NULL;   /* ignore it! */
1507                 break;
1508
1509                 /*
1510                  *      Regular expression comparison of integer attributes
1511                  *      does a STRING comparison of the names of their
1512                  *      integer attributes.
1513                  */
1514         case T_OP_REG_EQ:       /* =~ */
1515         case T_OP_REG_NE:       /* !~ */
1516         {
1517 #ifndef HAVE_REGEX
1518                 fr_strerror_printf("Regular expressions are not supported");
1519                 return NULL;
1520 #else
1521                 ssize_t slen;
1522                 regex_t *preg;
1523
1524                 /*
1525                  *      Someone else will fill in the value.
1526                  */
1527                 if (!value) break;
1528
1529                 talloc_free(vp);
1530
1531                 slen = regex_compile(ctx, &preg, value, strlen(value), false, false, false, true);
1532                 if (slen <= 0) {
1533                         fr_strerror_printf("Error at offset %zu compiling regex for %s: %s", -slen,
1534                                            attribute, fr_strerror());
1535                         return NULL;
1536                 }
1537                 talloc_free(preg);
1538
1539                 vp = fr_pair_make(ctx, NULL, attribute, NULL, op);
1540                 if (!vp) return NULL;
1541
1542                 if (fr_pair_mark_xlat(vp, value) < 0) {
1543                         talloc_free(vp);
1544                         return NULL;
1545                 }
1546
1547                 value = NULL;   /* ignore it */
1548                 break;
1549 #endif
1550         }
1551         default:
1552                 break;
1553         }
1554
1555         /*
1556          *      We allow this for stupidity, but it's really a bad idea.
1557          */
1558         if (vp->da->type == PW_TYPE_TLV) {
1559                 ssize_t len;
1560                 DICT_ATTR const *unknown;
1561                 VALUE_PAIR *head = NULL;
1562                 VALUE_PAIR **tail = &head;
1563
1564                 if (!value) {
1565                         talloc_free(vp);
1566                         return NULL;
1567                 }
1568
1569                 unknown = dict_unknown_afrom_fields(vp, vp->da->attr, vp->da->vendor);
1570                 if (!unknown) {
1571                         talloc_free(vp);
1572                         return NULL;
1573                 }
1574
1575                 vp->da = unknown;
1576
1577                 /*
1578                  *      Parse it as an unknown type, i.e. octets.
1579                  */
1580                 if (fr_pair_value_from_str(vp, value, -1) < 0) {
1581                         talloc_free(vp);
1582                         return NULL;
1583                 }
1584
1585                 /*
1586                  *      It's badly formatted.  Treat it as unknown.
1587                  */
1588                 if (rad_tlv_ok(vp->vp_octets, vp->vp_length, 1, 1) < 0) {
1589                         goto do_add;
1590                 }
1591
1592                 /*
1593                  *      Decode the TLVs
1594                  */
1595                 len = rad_data2vp_tlvs(ctx, NULL, NULL, NULL, da, vp->vp_octets,
1596                                        vp->vp_length, tail);
1597                 if (len < 0) {
1598                         goto do_add;
1599                 }
1600
1601                 talloc_free(vp);
1602                 vp = head;
1603                 goto do_add;
1604         }
1605
1606         /*
1607          *      FIXME: if (strcasecmp(attribute, vp->da->name) != 0)
1608          *      then the user MAY have typed in the attribute name
1609          *      as Vendor-%d-Attr-%d, and the value MAY be octets.
1610          *
1611          *      We probably want to fix fr_pair_value_from_str to accept
1612          *      octets as values for any attribute.
1613          */
1614         if (value && (fr_pair_value_from_str(vp, value, -1) < 0)) {
1615                 talloc_free(vp);
1616                 return NULL;
1617         }
1618
1619 do_add:
1620         if (vps) fr_pair_add(vps, vp);
1621         return vp;
1622 }
1623
1624 /** Mark a valuepair for xlat expansion
1625  *
1626  * Copies xlat source (unprocessed) string to valuepair value,
1627  * and sets value type.
1628  *
1629  * @param vp to mark for expansion.
1630  * @param value to expand.
1631  * @return 0 if marking succeeded or -1 if vp already had a value, or OOM.
1632  */
1633 int fr_pair_mark_xlat(VALUE_PAIR *vp, char const *value)
1634 {
1635         char *raw;
1636
1637         /*
1638          *      valuepair should not already have a value.
1639          */
1640         if (vp->type != VT_NONE) {
1641                 fr_strerror_printf("Pair already has a value");
1642                 return -1;
1643         }
1644
1645         raw = talloc_typed_strdup(vp, value);
1646         if (!raw) {
1647                 fr_strerror_printf("Out of memory");
1648                 return -1;
1649         }
1650
1651         vp->type = VT_XLAT;
1652         vp->value.xlat = raw;
1653         vp->vp_length = 0;
1654
1655         return 0;
1656 }
1657
1658
1659 /** Read a single valuepair from a buffer, and advance the pointer
1660  *
1661  *  Returns T_EOL if end of line was encountered.
1662  *
1663  * @param[in,out] ptr to read from and update.
1664  * @param[out] raw The struct to write the raw VALUE_PAIR to.
1665  * @return the last token read.
1666  */
1667 FR_TOKEN fr_pair_raw_from_str(char const **ptr, VALUE_PAIR_RAW *raw)
1668 {
1669         char const      *p;
1670         char *q;
1671         FR_TOKEN        ret = T_INVALID, next, quote;
1672         char            buf[8];
1673
1674         if (!ptr || !*ptr || !raw) {
1675                 fr_strerror_printf("Invalid arguments");
1676                 return T_INVALID;
1677         }
1678
1679         /*
1680          *      Skip leading spaces
1681          */
1682         p = *ptr;
1683         while ((*p == ' ') || (*p == '\t')) p++;
1684
1685         if (!*p) {
1686                 fr_strerror_printf("No token read where we expected "
1687                                    "an attribute name");
1688                 return T_INVALID;
1689         }
1690
1691         if (*p == '#') return T_HASH;
1692
1693         /*
1694          *      Try to get the attribute name.
1695          */
1696         q = raw->l_opand;
1697         *q = '\0';
1698         while (*p) {
1699                 uint8_t const *t = (uint8_t const *) p;
1700
1701                 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1702                 too_long:
1703                         fr_strerror_printf("Attribute name too long");
1704                         return T_INVALID;
1705                 }
1706
1707                 /*
1708                  *      This is arguably easier than trying to figure
1709                  *      out which operators come after the attribute
1710                  *      name.  Yes, our "lexer" is bad.
1711                  */
1712                 if (!dict_attr_allowed_chars[(unsigned int) *t]) {
1713                         break;
1714                 }
1715
1716                 /*
1717                  *      Attribute:=value is NOT
1718                  *
1719                  *      Attribute:
1720                  *      =
1721                  *      value
1722                  */
1723                 if ((*p == ':') && (!isdigit((int) p[1]))) {
1724                         break;
1725                 }
1726
1727                 *(q++) = *(p++);
1728         }
1729
1730         /*
1731          *      Haven't found any valid characters in the name.
1732          */
1733         if (!*raw->l_opand) {
1734                 fr_strerror_printf("Invalid attribute name");
1735                 return T_INVALID;
1736         }
1737
1738         /*
1739          *      Look for tag (:#).  This is different from :=, which
1740          *      is an operator.
1741          */
1742         if ((*p == ':') && (isdigit((int) p[1]))) {
1743                 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1744                         goto too_long;
1745                 }
1746                 *(q++) = *(p++);
1747
1748                 while (isdigit((int) *p)) {
1749                         if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
1750                                 goto too_long;
1751                         }
1752                         *(q++) = *(p++);
1753                 }
1754         }
1755
1756         *q = '\0';
1757         *ptr = p;
1758
1759         /* Now we should have an operator here. */
1760         raw->op = gettoken(ptr, buf, sizeof(buf), false);
1761         if (raw->op  < T_EQSTART || raw->op  > T_EQEND) {
1762                 fr_strerror_printf("Expecting operator");
1763
1764                 return T_INVALID;
1765         }
1766
1767         /*
1768          *      Read value.  Note that empty string values are allowed
1769          */
1770         quote = gettoken(ptr, raw->r_opand, sizeof(raw->r_opand), false);
1771         if (quote == T_EOL) {
1772                 fr_strerror_printf("Failed to get value");
1773
1774                 return T_INVALID;
1775         }
1776
1777         /*
1778          *      Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
1779          */
1780         p = *ptr;
1781
1782         next = gettoken(&p, buf, sizeof(buf), false);
1783         switch (next) {
1784         case T_HASH:
1785                 next = T_EOL;
1786                 break;
1787
1788         case T_EOL:
1789                 break;
1790
1791         case T_COMMA:
1792                 *ptr = p;
1793                 break;
1794
1795         default:
1796                 fr_strerror_printf("Expected end of line or comma");
1797                 return T_INVALID;
1798         }
1799         ret = next;
1800
1801         switch (quote) {
1802         /*
1803          *      Perhaps do xlat's
1804          */
1805         case T_DOUBLE_QUOTED_STRING:
1806                 /*
1807                  *      Only report as double quoted if it contained valid
1808                  *      a valid xlat expansion.
1809                  */
1810                 p = strchr(raw->r_opand, '%');
1811                 if (p && (p[1] == '{')) {
1812                         raw->quote = quote;
1813                 } else {
1814                         raw->quote = T_SINGLE_QUOTED_STRING;
1815                 }
1816
1817                 break;
1818
1819         case T_SINGLE_QUOTED_STRING:
1820         case T_BACK_QUOTED_STRING:
1821         case T_BARE_WORD:
1822                 raw->quote = quote;
1823                 break;
1824
1825         default:
1826                 fr_strerror_printf("Failed to find expected value on right hand side");
1827                 return T_INVALID;
1828         }
1829
1830         return ret;
1831 }
1832
1833 /** Read one line of attribute/value pairs into a list.
1834  *
1835  * The line may specify multiple attributes separated by commas.
1836  *
1837  * @note If the function returns T_INVALID, an error has occurred and
1838  * @note the valuepair list should probably be freed.
1839  *
1840  * @param ctx for talloc
1841  * @param buffer to read valuepairs from.
1842  * @param list where the parsed VALUE_PAIRs will be appended.
1843  * @return the last token parsed, or T_INVALID
1844  */
1845 FR_TOKEN fr_pair_list_afrom_str(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **list)
1846 {
1847         VALUE_PAIR      *vp, *head, **tail;
1848         char const      *p;
1849         FR_TOKEN        last_token = T_INVALID;
1850         VALUE_PAIR_RAW  raw;
1851
1852         /*
1853          *      We allow an empty line.
1854          */
1855         if (buffer[0] == 0) {
1856                 return T_EOL;
1857         }
1858
1859         head = NULL;
1860         tail = &head;
1861
1862         p = buffer;
1863         do {
1864                 raw.l_opand[0] = '\0';
1865                 raw.r_opand[0] = '\0';
1866
1867                 last_token = fr_pair_raw_from_str(&p, &raw);
1868
1869                 /*
1870                  *      JUST a hash.  Don't try to create a VP.
1871                  *      Let the caller determine if an empty list is OK.
1872                  */
1873                 if (last_token == T_HASH) {
1874                         last_token = T_EOL;
1875                         break;
1876                 }
1877                 if (last_token == T_INVALID) break;
1878
1879                 if (raw.quote == T_DOUBLE_QUOTED_STRING) {
1880                         vp = fr_pair_make(ctx, NULL, raw.l_opand, NULL, raw.op);
1881                         if (!vp) {
1882                                 last_token = T_INVALID;
1883                                 break;
1884                         }
1885                         if (fr_pair_mark_xlat(vp, raw.r_opand) < 0) {
1886                                 talloc_free(vp);
1887                                 last_token = T_INVALID;
1888                                 break;
1889                         }
1890                 } else {
1891                         vp = fr_pair_make(ctx, NULL, raw.l_opand, raw.r_opand, raw.op);
1892                         if (!vp) {
1893                                 last_token = T_INVALID;
1894                                 break;
1895                         }
1896                 }
1897
1898                 *tail = vp;
1899                 tail = &((*tail)->next);
1900         } while (*p && (last_token == T_COMMA));
1901
1902         if (last_token == T_INVALID) {
1903                 fr_pair_list_free(&head);
1904         } else {
1905                 fr_pair_add(list, head);
1906         }
1907
1908         /*
1909          *      And return the last token which we read.
1910          */
1911         return last_token;
1912 }
1913
1914 /*
1915  *      Read valuepairs from the fp up to End-Of-File.
1916  */
1917 int fr_pair_list_afrom_file(TALLOC_CTX *ctx, VALUE_PAIR **out, FILE *fp, bool *pfiledone)
1918 {
1919         char buf[8192];
1920         FR_TOKEN last_token = T_EOL;
1921
1922         vp_cursor_t cursor;
1923
1924         VALUE_PAIR *vp = NULL;
1925         fr_cursor_init(&cursor, out);
1926
1927         while (fgets(buf, sizeof(buf), fp) != NULL) {
1928                 /*
1929                  *      If we get a '\n' by itself, we assume that's
1930                  *      the end of that VP
1931                  */
1932                 if (buf[0] == '\n') {
1933                         if (vp) {
1934                                 *pfiledone = false;
1935                                 return 0;
1936                         }
1937                         continue;
1938                 }
1939
1940                 /*
1941                  *      Comments get ignored
1942                  */
1943                 if (buf[0] == '#') continue;
1944
1945                 /*
1946                  *      Read all of the attributes on the current line.
1947                  */
1948                 vp = NULL;
1949                 last_token = fr_pair_list_afrom_str(ctx, buf, &vp);
1950                 if (!vp) {
1951                         if (last_token != T_EOL) goto error;
1952                         break;
1953                 }
1954
1955                 fr_cursor_merge(&cursor, vp);
1956                 buf[0] = '\0';
1957         }
1958         *pfiledone = true;
1959
1960         return 0;
1961
1962 error:
1963         *pfiledone = false;
1964         vp = fr_cursor_first(&cursor);
1965         if (vp) fr_pair_list_free(&vp);
1966
1967         return -1;
1968 }
1969
1970 /** Compare two pairs, using the operator from "a"
1971  *
1972  *      i.e. given two attributes, it does:
1973  *
1974  *      (b->data) (a->operator) (a->data)
1975  *
1976  *      e.g. "foo" != "bar"
1977  *
1978  * @param[in] a the first attribute
1979  * @param[in] b the second attribute
1980  * @return 1 if true, 0 if false, -1 on error.
1981  */
1982 int fr_pair_cmp(VALUE_PAIR *a, VALUE_PAIR *b)
1983 {
1984         if (!a) return -1;
1985
1986         VERIFY_VP(a);
1987         if (b) VERIFY_VP(b);
1988
1989         switch (a->op) {
1990         case T_OP_CMP_TRUE:
1991                 return (b != NULL);
1992
1993         case T_OP_CMP_FALSE:
1994                 return (b == NULL);
1995
1996                 /*
1997                  *      a is a regex, compile it, print b to a string,
1998                  *      and then do string comparisons.
1999                  */
2000         case T_OP_REG_EQ:
2001         case T_OP_REG_NE:
2002 #ifndef HAVE_REGEX
2003                 return -1;
2004 #else
2005                 if (!b) return false;
2006
2007                 {
2008                         ssize_t slen;
2009                         regex_t *preg;
2010                         char    *value;
2011
2012                         if (!fr_assert(a->da->type == PW_TYPE_STRING)) return -1;
2013
2014                         slen = regex_compile(NULL, &preg, a->value.xlat, talloc_array_length(a->value.xlat) - 1, false, false, false, true);
2015                         if (slen <= 0) {
2016                                 fr_strerror_printf("Error at offset %zu compiling regex for %s: %s",
2017                                                    -slen, a->da->name, fr_strerror());
2018                                 return -1;
2019                         }
2020                         value = vp_aprints_value(NULL, b, '\0');
2021                         if (!value) {
2022                                 talloc_free(preg);
2023                                 return -1;
2024                         }
2025
2026                         /*
2027                          *      Don't care about substring matches, oh well...
2028                          */
2029                         slen = regex_exec(preg, value, talloc_array_length(value) - 1, NULL, NULL);
2030                         talloc_free(preg);
2031                         talloc_free(value);
2032
2033                         if (slen < 0) return -1;
2034                         if (a->op == T_OP_REG_EQ) return (int)slen;
2035                         return !slen;
2036                 }
2037 #endif
2038
2039         default:                /* we're OK */
2040                 if (!b) return false;
2041                 break;
2042         }
2043
2044         return fr_pair_cmp_op(a->op, b, a);
2045 }
2046
2047 /** Determine equality of two lists
2048  *
2049  * This is useful for comparing lists of attributes inserted into a binary tree.
2050  *
2051  * @param a first list of VALUE_PAIRs.
2052  * @param b second list of VALUE_PAIRs.
2053  * @return -1 if a < b, 0 if the two lists are equal, 1 if a > b, -2 on error.
2054  */
2055 int fr_pair_list_cmp(VALUE_PAIR *a, VALUE_PAIR *b)
2056 {
2057         vp_cursor_t a_cursor, b_cursor;
2058         VALUE_PAIR *a_p, *b_p;
2059         int ret;
2060
2061         for (a_p = fr_cursor_init(&a_cursor, &a), b_p = fr_cursor_init(&b_cursor, &b);
2062              a_p && b_p;
2063              a_p = fr_cursor_next(&a_cursor), b_p = fr_cursor_next(&b_cursor)) {
2064                 /* Same VP, no point doing expensive checks */
2065                 if (a_p == b_p) {
2066                         continue;
2067                 }
2068
2069                 if (a_p->da < b_p->da) {
2070                         return -1;
2071                 }
2072                 if (a_p->da > b_p->da) {
2073                         return 1;
2074                 }
2075
2076                 if (a_p->tag < b_p->tag) {
2077                         return -1;
2078                 }
2079                 if (a_p->tag > b_p->tag) {
2080                         return 1;
2081                 }
2082
2083                 ret = value_data_cmp(a_p->da->type, &a_p->data, a_p->vp_length,
2084                                      b_p->da->type, &b_p->data, b_p->vp_length);
2085                 if (ret != 0) {
2086                         fr_assert(ret >= -1);   /* Comparison error */
2087                         return ret;
2088                 }
2089         }
2090
2091         if (!a_p && !b_p) {
2092                 return 0;
2093         }
2094
2095         if (!a_p) {
2096                 return -1;
2097         }
2098
2099         /* if(!b_p) */
2100         return 1;
2101 }
2102
2103 /** Set the type of the VALUE_PAIR value buffer to match it's DICT_ATTR
2104  *
2105  * @param vp to fixup.
2106  */
2107 static void fr_pair_value_set_type(VALUE_PAIR *vp)
2108 {
2109         if (!vp->data.ptr) return;
2110
2111         switch (vp->da->type) {
2112         case PW_TYPE_OCTETS:
2113                 talloc_set_type(vp->data.ptr, uint8_t);
2114                 return;
2115
2116         case PW_TYPE_STRING:
2117                 talloc_set_type(vp->data.ptr, char);
2118                 return;
2119
2120         default:
2121                 return;
2122         }
2123 }
2124
2125 /** Copy data into an "octets" data type.
2126  *
2127  * @param[in,out] vp to update
2128  * @param[in] src data to copy
2129  * @param[in] size of the data, may be 0 in which case previous value will be freed.
2130  */
2131 void fr_pair_value_memcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
2132 {
2133         uint8_t *p = NULL, *q;
2134
2135         VERIFY_VP(vp);
2136
2137         if (size > 0) {
2138                 p = talloc_memdup(vp, src, size);
2139                 if (!p) return;
2140                 talloc_set_type(p, uint8_t);
2141         }
2142
2143         memcpy(&q, &vp->vp_octets, sizeof(q));
2144         TALLOC_FREE(q);
2145
2146         vp->vp_octets = p;
2147         vp->vp_length = size;
2148
2149         if (size > 0) fr_pair_value_set_type(vp);
2150 }
2151
2152 /** Reparent an allocated octet buffer to a VALUE_PAIR
2153  *
2154  * @param[in,out] vp to update
2155  * @param[in] src buffer to steal.
2156  */
2157 void fr_pair_value_memsteal(VALUE_PAIR *vp, uint8_t const *src)
2158 {
2159         uint8_t *q;
2160
2161         VERIFY_VP(vp);
2162
2163         memcpy(&q, &vp->vp_octets, sizeof(q));
2164         talloc_free(q);
2165
2166         vp->vp_octets = talloc_steal(vp, src);
2167         vp->type = VT_DATA;
2168         vp->vp_length = talloc_array_length(vp->vp_strvalue);
2169         fr_pair_value_set_type(vp);
2170 }
2171
2172 /** Reparent an allocated char buffer to a VALUE_PAIR
2173  *
2174  * @param[in,out] vp to update
2175  * @param[in] src buffer to steal.
2176  */
2177 void fr_pair_value_strsteal(VALUE_PAIR *vp, char const *src)
2178 {
2179         uint8_t *q;
2180
2181         VERIFY_VP(vp);
2182
2183         memcpy(&q, &vp->vp_octets, sizeof(q));
2184         talloc_free(q);
2185
2186         vp->vp_strvalue = talloc_steal(vp, src);
2187         vp->type = VT_DATA;
2188         vp->vp_length = talloc_array_length(vp->vp_strvalue) - 1;
2189         fr_pair_value_set_type(vp);
2190 }
2191
2192 /** Copy data into an "string" data type.
2193  *
2194  * @param[in,out] vp to update
2195  * @param[in] src data to copy
2196  */
2197 void fr_pair_value_strcpy(VALUE_PAIR *vp, char const *src)
2198 {
2199         char *p, *q;
2200
2201         VERIFY_VP(vp);
2202
2203         p = talloc_strdup(vp, src);
2204
2205         if (!p) return;
2206
2207         memcpy(&q, &vp->vp_strvalue, sizeof(q));
2208         talloc_free(q);
2209
2210         vp->vp_strvalue = p;
2211         vp->type = VT_DATA;
2212         vp->vp_length = talloc_array_length(vp->vp_strvalue) - 1;
2213         fr_pair_value_set_type(vp);
2214 }
2215
2216 /** Copy data into an "string" data type.
2217  *
2218  * @note unlike the original strncpy, this function does not stop
2219  *      if it finds \0 bytes embedded in the string.
2220  *
2221  * @param[in,out] vp to update.
2222  * @param[in] src data to copy.
2223  * @param[in] len of data to copy.
2224  */
2225 void fr_pair_value_bstrncpy(VALUE_PAIR *vp, void const *src, size_t len)
2226 {
2227         char *p, *q;
2228
2229         VERIFY_VP(vp);
2230
2231         p = talloc_array(vp, char, len + 1);
2232         if (!p) return;
2233
2234         memcpy(p, src, len);    /* embdedded \0 safe */
2235         p[len] = '\0';
2236
2237         memcpy(&q, &vp->vp_strvalue, sizeof(q));
2238         talloc_free(q);
2239
2240         vp->vp_strvalue = p;
2241         vp->type = VT_DATA;
2242         vp->vp_length = len;
2243         fr_pair_value_set_type(vp);
2244 }
2245
2246 /** Print data into an "string" data type.
2247  *
2248  * @param[in,out] vp to update
2249  * @param[in] fmt the format string
2250  */
2251 void fr_pair_value_sprintf(VALUE_PAIR *vp, char const *fmt, ...)
2252 {
2253         va_list ap;
2254         char *p, *q;
2255
2256         VERIFY_VP(vp);
2257
2258         va_start(ap, fmt);
2259         p = talloc_vasprintf(vp, fmt, ap);
2260         va_end(ap);
2261
2262         if (!p) return;
2263
2264         memcpy(&q, &vp->vp_strvalue, sizeof(q));
2265         talloc_free(q);
2266
2267         vp->vp_strvalue = p;
2268         vp->type = VT_DATA;
2269
2270         vp->vp_length = talloc_array_length(vp->vp_strvalue) - 1;
2271         fr_pair_value_set_type(vp);
2272 }
2273
2274 #ifdef WITH_VERIFY_PTR
2275 /*
2276  *      Verify a VALUE_PAIR
2277  */
2278 inline void fr_pair_verify(char const *file, int line, VALUE_PAIR const *vp)
2279 {
2280         if (!vp) {
2281                 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR pointer was NULL", file, line);
2282                 fr_assert(0);
2283                 fr_exit_now(1);
2284         }
2285
2286         (void) talloc_get_type_abort(vp, VALUE_PAIR);
2287
2288         if (!vp->da) {
2289                 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR da pointer was NULL", file, line);
2290                 fr_assert(0);
2291                 fr_exit_now(1);
2292         }
2293
2294         if (vp->data.ptr) switch (vp->da->type) {
2295         case PW_TYPE_OCTETS:
2296         {
2297                 size_t len;
2298                 TALLOC_CTX *parent;
2299
2300                 if (!talloc_get_type(vp->data.ptr, uint8_t)) {
2301                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
2302                                      "uint8_t but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
2303                         (void) talloc_get_type_abort(vp->data.ptr, uint8_t);
2304                 }
2305
2306                 len = talloc_array_length(vp->vp_octets);
2307                 if (vp->vp_length > len) {
2308                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
2309                                      "uint8_t data buffer length %zu\n", file, line, vp->da->name, vp->vp_length, len);
2310                         fr_assert(0);
2311                         fr_exit_now(1);
2312                 }
2313
2314                 parent = talloc_parent(vp->data.ptr);
2315                 if (parent != vp) {
2316                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not "
2317                                      "parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
2318                                      file, line, vp->da->name,
2319                                      vp, parent, parent ? talloc_get_name(parent) : "NULL");
2320                         fr_assert(0);
2321                         fr_exit_now(1);
2322                 }
2323         }
2324                 break;
2325
2326         case PW_TYPE_STRING:
2327         {
2328                 size_t len;
2329                 TALLOC_CTX *parent;
2330
2331                 if (!talloc_get_type(vp->data.ptr, char)) {
2332                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
2333                                      "char but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
2334                         (void) talloc_get_type_abort(vp->data.ptr, char);
2335                 }
2336
2337                 len = (talloc_array_length(vp->vp_strvalue) - 1);
2338                 if (vp->vp_length > len) {
2339                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
2340                                      "char buffer length %zu\n", file, line, vp->da->name, vp->vp_length, len);
2341                         fr_assert(0);
2342                         fr_exit_now(1);
2343                 }
2344
2345                 if (vp->vp_strvalue[vp->vp_length] != '\0') {
2346                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer not \\0 "
2347                                      "terminated\n", file, line, vp->da->name);
2348                         fr_assert(0);
2349                         fr_exit_now(1);
2350                 }
2351
2352                 parent = talloc_parent(vp->data.ptr);
2353                 if (parent != vp) {
2354                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not "
2355                                      "parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
2356                                      file, line, vp->da->name,
2357                                      vp, parent, parent ? talloc_get_name(parent) : "NULL");
2358                         fr_assert(0);
2359                         fr_exit_now(1);
2360                 }
2361         }
2362                 break;
2363
2364         default:
2365                 break;
2366         }
2367
2368         if (vp->da->flags.is_unknown) {
2369                 (void) talloc_get_type_abort(vp->da, DICT_ATTR);
2370         } else {
2371                 DICT_ATTR const *da;
2372
2373                 /*
2374                  *      Attribute may be present with multiple names
2375                  */
2376                 da = dict_attrbyname(vp->da->name);
2377                 if (!da) {
2378                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR attribute %p \"%s\" (%s) "
2379                                      "not found in global dictionary",
2380                                      file, line, vp->da, vp->da->name,
2381                                      fr_int2str(dict_attr_types, vp->da->type, "<INVALID>"));
2382                         fr_assert(0);
2383                         fr_exit_now(1);
2384                 }
2385
2386                 if (da->type == PW_TYPE_COMBO_IP_ADDR) {
2387                         da = dict_attrbytype(vp->da->attr, vp->da->vendor, vp->da->type);
2388                         if (!da) {
2389                                 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR attribute %p \"%s\" "
2390                                              "variant (%s) not found in global dictionary",
2391                                              file, line, vp->da, vp->da->name,
2392                                              fr_int2str(dict_attr_types, vp->da->type, "<INVALID>"));
2393                                 fr_assert(0);
2394                                 fr_exit_now(1);
2395                         }
2396                 }
2397
2398
2399                 if (da != vp->da) {
2400                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR "
2401                                      "dictionary pointer %p \"%s\" (%s) "
2402                                      "and global dictionary pointer %p \"%s\" (%s) differ",
2403                                      file, line, vp->da, vp->da->name,
2404                                      fr_int2str(dict_attr_types, vp->da->type, "<INVALID>"),
2405                                      da, da->name, fr_int2str(dict_attr_types, da->type, "<INVALID>"));
2406                         fr_assert(0);
2407                         fr_exit_now(1);
2408                 }
2409         }
2410 }
2411
2412 /*
2413  *      Verify a pair list
2414  */
2415 void fr_pair_list_verify(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR *vps)
2416 {
2417         vp_cursor_t cursor;
2418         VALUE_PAIR *vp;
2419         TALLOC_CTX *parent;
2420
2421         for (vp = fr_cursor_init(&cursor, &vps);
2422              vp;
2423              vp = fr_cursor_next(&cursor)) {
2424                 VERIFY_VP(vp);
2425
2426                 parent = talloc_parent(vp);
2427                 if (expected && (parent != expected)) {
2428                         FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: Expected VALUE_PAIR \"%s\" to be parented "
2429                                      "by %p (%s), instead parented by %p (%s)\n",
2430                                      file, line, vp->da->name,
2431                                      expected, talloc_get_name(expected),
2432                                      parent, parent ? talloc_get_name(parent) : "NULL");
2433
2434                         fr_log_talloc_report(expected);
2435                         if (parent) fr_log_talloc_report(parent);
2436
2437                         fr_assert(0);
2438                         fr_exit_now(1);
2439                 }
2440
2441         }
2442 }
2443 #endif