pairparsevalue should return 0 or -1 like pretty much every other function int the...
[freeradius.git] / src / lib / valuepair.c
1 /*
2  * valuepair.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
27 #include <ctype.h>
28
29 #ifdef HAVE_PCREPOSIX_H
30 #  define WITH_REGEX
31 #  include <pcreposix.h>
32 #elif defined(HAVE_REGEX_H)
33 #  include <regex.h>
34 #  define WITH_REGEX
35
36 /*
37  *  For POSIX Regular expressions.
38  *  (0) Means no extended regular expressions.
39  *  REG_EXTENDED means use extended regular expressions.
40  */
41 #  ifndef REG_EXTENDED
42 #    define REG_EXTENDED (0)
43 #  endif
44
45 #  ifndef REG_NOSUB
46 #    define REG_NOSUB (0)
47 #  endif
48 #endif
49
50 #define attribute_eq(_x, _y) ((_x && _y) && (_x->da == _y->da) && (_x->tag == _y->tag))
51
52 /** Free a VALUE_PAIR
53  *
54  * @note Do not call directly, use talloc_free instead.
55  *
56  * @param vp to free.
57  * @return 0
58  */
59 static int _pairfree(VALUE_PAIR *vp) {
60         /*
61          *      The lack of DA means something has gone wrong
62          */
63         if (!vp->da) {
64                 fr_strerror_printf("VALUE_PAIR has NULL DICT_ATTR pointer (probably already freed)");
65         /*
66          *      Only free the DICT_ATTR if it was dynamically allocated
67          *      and was marked for free when the VALUE_PAIR is freed.
68          *
69          *      @fixme This is an awful hack and needs to be removed once DICT_ATTRs are allocated by talloc.
70          */
71         } else if (vp->da->flags.vp_free) {
72                 dict_attr_free(&(vp->da));
73         }
74
75 #ifndef NDEBUG
76         vp->vp_integer = 0xf4eef4ee;
77 #endif
78
79 #ifdef TALLOC_DEBUG
80         talloc_report_depth_cb(NULL, 0, -1, fr_talloc_verify_cb, NULL);
81 #endif
82         return 0;
83 }
84
85 /** Dynamically allocate a new attribute
86  *
87  * Allocates a new attribute and a new dictionary attr if no DA is provided.
88  *
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.
92  */
93 VALUE_PAIR *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da)
94 {
95         VALUE_PAIR *vp;
96
97         /*
98          *      Caller must specify a da else we don't know what the attribute type is.
99          */
100         if (!da) {
101                 fr_strerror_printf("Invalid arguments");
102                 return NULL;
103         }
104
105         vp = talloc_zero(ctx, VALUE_PAIR);
106         if (!vp) {
107                 fr_strerror_printf("Out of memory");
108                 return NULL;
109         }
110
111         vp->da = da;
112         vp->op = T_OP_EQ;
113         vp->tag = TAG_ANY;
114         vp->type = VT_NONE;
115
116         vp->length = da->flags.length;
117
118         talloc_set_destructor(vp, _pairfree);
119
120         return vp;
121 }
122
123 /** Create a new valuepair
124  *
125  * If attr and vendor match a dictionary entry then a VP with that DICT_ATTR
126  * will be returned.
127  *
128  * If attr or vendor are uknown will call dict_attruknown to create a dynamic
129  * DICT_ATTR of PW_TYPE_OCTETS.
130  *
131  * Which type of DICT_ATTR the VALUE_PAIR was created with can be determined by
132  * checking @verbatim vp->da->flags.is_unknown @endverbatim.
133  *
134  * @param[in] ctx for allocated memory, usually a pointer to a RADIUS_PACKET
135  * @param[in] attr number.
136  * @param[in] vendor number.
137  * @return the new valuepair or NULL on error.
138  */
139 VALUE_PAIR *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
140 {
141         DICT_ATTR const *da;
142
143         da = dict_attrbyvalue(attr, vendor);
144         if (!da) {
145                 da = dict_attrunknown(attr, vendor, true);
146                 if (!da) {
147                         return NULL;
148                 }
149         }
150
151         return pairalloc(ctx, da);
152 }
153
154 /** Free memory used by a valuepair list.
155  *
156  * @todo TLV: needs to free all dependents of each VP freed.
157  */
158 void pairfree(VALUE_PAIR **vps)
159 {
160         VALUE_PAIR      *vp;
161         vp_cursor_t     cursor;
162
163         if (!vps || !*vps) {
164                 return;
165         }
166
167         for (vp = fr_cursor_init(&cursor, vps);
168              vp;
169              vp = fr_cursor_next(&cursor)) {
170                 VERIFY_VP(vp);
171                 talloc_free(vp);
172         }
173
174         *vps = NULL;
175 }
176
177 /** Mark malformed or unrecognised attributed as unknown
178  *
179  * @param vp to change DICT_ATTR of.
180  * @return 0 on success (or if already unknown) else -1 on error.
181  */
182 int pair2unknown(VALUE_PAIR *vp)
183 {
184         DICT_ATTR const *da;
185
186         VERIFY_VP(vp);
187         if (vp->da->flags.is_unknown) {
188                 return 0;
189         }
190
191         da = dict_attrunknown(vp->da->attr, vp->da->vendor, true);
192         if (!da) {
193                 return -1;
194         }
195
196         vp->da = da;
197
198         return 0;
199 }
200
201 /** Find the pair with the matching DAs
202  *
203  */
204 VALUE_PAIR *pairfind_da(VALUE_PAIR *vp, DICT_ATTR const *da, int8_t tag)
205 {
206         vp_cursor_t     cursor;
207         VALUE_PAIR      *i;
208
209         if(!fr_assert(da)) {
210                  return NULL;
211         }
212
213         for (i = fr_cursor_init(&cursor, &vp);
214              i;
215              i = fr_cursor_next(&cursor)) {
216                 VERIFY_VP(i);
217                 if ((i->da == da) && (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
218                         return i;
219                 }
220         }
221
222         return NULL;
223 }
224
225
226 /** Find the pair with the matching attribute
227  *
228  * @todo should take DAs and do a pointer comparison.
229  */
230 VALUE_PAIR *pairfind(VALUE_PAIR *vp, unsigned int attr, unsigned int vendor, int8_t tag)
231 {
232         vp_cursor_t     cursor;
233         VALUE_PAIR      *i;
234
235         VERIFY_LIST(vp);
236
237         for (i = fr_cursor_init(&cursor, &vp);
238              i;
239              i = fr_cursor_next(&cursor)) {
240                 if ((i->da->attr == attr) && (i->da->vendor == vendor) && \
241                     (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
242                         return i;
243                 }
244         }
245
246         return NULL;
247 }
248
249 /** Delete matching pairs
250  *
251  * Delete matching pairs from the attribute list.
252  *
253  * @param[in,out] first VP in list.
254  * @param[in] attr to match.
255  * @param[in] vendor to match.
256  * @param[in] tag to match. TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
257  *
258  * @todo should take DAs and do a point comparison.
259  */
260 void pairdelete(VALUE_PAIR **first, unsigned int attr, unsigned int vendor,
261                 int8_t tag)
262 {
263         VALUE_PAIR *i, *next;
264         VALUE_PAIR **last = first;
265
266         for(i = *first; i; i = next) {
267                 VERIFY_VP(i);
268                 next = i->next;
269                 if ((i->da->attr == attr) && (i->da->vendor == vendor) &&
270                     (!i->da->flags.has_tag || TAG_EQ(tag, i->tag))) {
271                         *last = next;
272                         talloc_free(i);
273                 } else {
274                         last = &i->next;
275                 }
276         }
277 }
278
279 /** Add a VP to the end of the list.
280  *
281  * Locates the end of 'first', and links an additional VP 'add' at the end.
282  *
283  * @param[in] first VP in linked list. Will add new VP to the end of this list.
284  * @param[in] add VP to add to list.
285  */
286 void pairadd(VALUE_PAIR **first, VALUE_PAIR *add)
287 {
288         VALUE_PAIR *i;
289
290         if (!add) return;
291
292         VERIFY_VP(add);
293
294         if (*first == NULL) {
295                 *first = add;
296                 return;
297         }
298         for(i = *first; i->next; i = i->next)
299                 VERIFY_VP(i);
300         i->next = add;
301 }
302
303 /** Replace all matching VPs
304  *
305  * Walks over 'first', and replaces the first VP that matches 'replace'.
306  *
307  * @note Memory used by the VP being replaced will be freed.
308  * @note Will not work with unknown attributes.
309  *
310  * @param[in,out] first VP in linked list. Will search and replace in this list.
311  * @param[in] replace VP to replace.
312  */
313 void pairreplace(VALUE_PAIR **first, VALUE_PAIR *replace)
314 {
315         VALUE_PAIR *i, *next;
316         VALUE_PAIR **prev = first;
317
318         VERIFY_VP(replace);
319
320         if (*first == NULL) {
321                 *first = replace;
322                 return;
323         }
324
325         /*
326          *      Not an empty list, so find item if it is there, and
327          *      replace it. Note, we always replace the first one, and
328          *      we ignore any others that might exist.
329          */
330         for(i = *first; i; i = next) {
331                 VERIFY_VP(i);
332                 next = i->next;
333
334                 /*
335                  *      Found the first attribute, replace it,
336                  *      and return.
337                  */
338                 if ((i->da == replace->da) && (!i->da->flags.has_tag || TAG_EQ(replace->tag, i->tag))) {
339                         *prev = replace;
340
341                         /*
342                          *      Should really assert that replace->next == NULL
343                          */
344                         replace->next = next;
345                         talloc_free(i);
346                         return;
347                 }
348
349                 /*
350                  *      Point to where the attribute should go.
351                  */
352                 prev = &i->next;
353         }
354
355         /*
356          *      If we got here, we didn't find anything to replace, so
357          *      stopped at the last item, which we just append to.
358          */
359         *prev = replace;
360 }
361
362 int8_t attrtagcmp(void const *a, void const *b)
363 {
364         VALUE_PAIR const *my_a = a;
365         VALUE_PAIR const *my_b = b;
366
367         VERIFY_VP(my_a);
368         VERIFY_VP(my_b);
369
370         uint8_t cmp;
371
372         cmp = fr_pointer_cmp(my_a->da, my_b->da);
373
374         if (cmp != 0) return cmp;
375
376         if (my_a->tag < my_b->tag) {
377                 return -1;
378         }
379
380         if (my_a->tag > my_b->tag) {
381                 return 1;
382         }
383
384         return 0;
385 }
386
387 static void pairsort_split(VALUE_PAIR *source, VALUE_PAIR **front, VALUE_PAIR **back)
388 {
389         VALUE_PAIR *fast;
390         VALUE_PAIR *slow;
391
392         /*
393          *      Stopping condition - no more elements left to split
394          */
395         if (!source || !source->next) {
396                 *front = source;
397                 *back = NULL;
398
399                 return;
400         }
401
402         /*
403          *      Fast advances twice as fast as slow, so when it gets to the end,
404          *      slow will point to the middle of the linked list.
405          */
406         slow = source;
407         fast = source->next;
408
409         while (fast) {
410                 fast = fast->next;
411                 if (fast) {
412                         slow = slow->next;
413                         fast = fast->next;
414                 }
415         }
416
417         *front = source;
418         *back = slow->next;
419         slow->next = NULL;
420 }
421
422 static VALUE_PAIR *pairsort_merge(VALUE_PAIR *a, VALUE_PAIR *b, fr_cmp_t cmp)
423 {
424         VALUE_PAIR *result = NULL;
425
426         if (!a) return b;
427         if (!b) return a;
428
429         /*
430          *      Compare the DICT_ATTRs and tags
431          */
432         if (cmp(a, b) <= 0) {
433                 result = a;
434                 result->next = pairsort_merge(a->next, b, cmp);
435         } else {
436                 result = b;
437                 result->next = pairsort_merge(a, b->next, cmp);
438         }
439
440         return result;
441 }
442
443 /** Sort a linked list of VALUE_PAIRs using merge sort
444  *
445  * @param[in,out] vps List of VALUE_PAIRs to sort.
446  * @param[in] cmp to sort with
447  */
448 void pairsort(VALUE_PAIR **vps, fr_cmp_t cmp)
449 {
450         VALUE_PAIR *head = *vps;
451         VALUE_PAIR *a;
452         VALUE_PAIR *b;
453
454         /*
455          *      If there's 0-1 elements it must already be sorted.
456          */
457         if (!head || !head->next) {
458                 return;
459         }
460
461         pairsort_split(head, &a, &b);   /* Split into sublists */
462         pairsort(&a, cmp);              /* Traverse left */
463         pairsort(&b, cmp);              /* Traverse right */
464
465         /*
466          *      merge the two sorted lists together
467          */
468         *vps = pairsort_merge(a, b, cmp);
469 }
470
471 /** Write an error to the library errorbuff detailing the mismatch
472  *
473  * Retrieve output with fr_strerror();
474  *
475  * @todo add thread specific talloc contexts.
476  *
477  * @param ctx a hack until we have thread specific talloc contexts.
478  * @param failed pair of attributes which didn't match.
479  */
480 void pairvalidate_debug(TALLOC_CTX *ctx, VALUE_PAIR const *failed[2])
481 {
482         VALUE_PAIR const *filter = failed[0];
483         VALUE_PAIR const *list = failed[1];
484
485         char *value, *pair;
486
487         (void) fr_strerror();   /* Clear any existing messages */
488
489         if (!fr_assert(!(!filter && !list))) return;
490
491         if (!list) {
492                 if (!filter) return;
493                 fr_strerror_printf("Attribute \"%s\" not found in list", filter->da->name);
494                 return;
495         }
496
497         if (!filter || (filter->da != list->da)) {
498                 fr_strerror_printf("Attribute \"%s\" not found in filter", list->da->name);
499                 return;
500         }
501
502         if (TAG_EQ(filter->tag, list->tag)) {
503                 fr_strerror_printf("Attribute \"%s\" tag \"%i\" didn't match filter tag \"%i\"",
504                                    list->da->name, list->tag, filter->tag);
505                 return;
506         }
507
508         pair = vp_aprint(ctx, filter);
509         value = vp_aprint_value(ctx, list);
510
511         fr_strerror_printf("Attribute value \"%s\" didn't match filter \"%s\"", value, pair);
512
513         talloc_free(pair);
514         talloc_free(value);
515
516         return;
517 }
518
519 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
520  *
521  * @note will sort both filter and list in place.
522  *
523  * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
524  *        May be NULL.
525  * @param filter attributes to check list against.
526  * @param list attributes, probably a request or reply
527  */
528 bool pairvalidate(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
529 {
530         vp_cursor_t filter_cursor;
531         vp_cursor_t list_cursor;
532
533         VALUE_PAIR *check, *match;
534
535         if (!filter && !list) {
536                 return true;
537         }
538
539         /*
540          *      This allows us to verify the sets of validate and reply are equal
541          *      i.e. we have a validate rule which matches every reply attribute.
542          *
543          *      @todo this should be removed one we have sets and lists
544          */
545         pairsort(&filter, attrtagcmp);
546         pairsort(&list, attrtagcmp);
547
548         check = fr_cursor_init(&filter_cursor, &filter);
549         match = fr_cursor_init(&list_cursor, &list);
550
551         while (true) {
552                 if (!match && !check) goto mismatch;
553
554                 /*
555                  *      The lists are sorted, so if the first
556                  *      attributes aren't of the same type, then we're
557                  *      done.
558                  */
559                 if (!attribute_eq(check, match)) goto mismatch;
560
561                 /*
562                  *      They're of the same type, but don't have the
563                  *      same values.  This is a problem.
564                  *
565                  *      Note that the RFCs say that for attributes of
566                  *      the same type, order is important.
567                  */
568                 if (!paircmp(check, match)) goto mismatch;
569
570                 check = fr_cursor_next(&filter_cursor);
571                 match = fr_cursor_next(&list_cursor);
572
573                 /*
574                  *      One list ended earlier than the others, they
575                  *      didn't match.
576                  */
577                 if (!match || !check) break;
578         }
579
580         return true;
581
582 mismatch:
583         if (failed) {
584                 failed[0] = check;
585                 failed[1] = match;
586         }
587         return false;
588 }
589
590 /** Uses paircmp to verify all VALUE_PAIRs in list match the filter defined by check
591  *
592  * @note will sort both filter and list in place.
593  *
594  * @param failed pointer to an array to write the pointers of the filter/list attributes that didn't match.
595  *        May be NULL.
596  * @param filter attributes to check list against.
597  * @param list attributes, probably a request or reply
598  */
599 bool pairvalidate_relaxed(VALUE_PAIR const *failed[2], VALUE_PAIR *filter, VALUE_PAIR *list)
600 {
601         vp_cursor_t filter_cursor;
602         vp_cursor_t list_cursor;
603
604         VALUE_PAIR *check, *last_check = NULL, *match = NULL;
605
606         if (!filter && !list) {
607                 return true;
608         }
609
610         /*
611          *      This allows us to verify the sets of validate and reply are equal
612          *      i.e. we have a validate rule which matches every reply attribute.
613          *
614          *      @todo this should be removed one we have sets and lists
615          */
616         pairsort(&filter, attrtagcmp);
617         pairsort(&list, attrtagcmp);
618
619         fr_cursor_init(&list_cursor, &list);
620         for (check = fr_cursor_init(&filter_cursor, &filter);
621              check;
622              check = fr_cursor_next(&filter_cursor)) {
623                 /*
624                  *      Were processing check attributes of a new type.
625                  */
626                 if (!attribute_eq(last_check, check)) {
627                         /*
628                          *      Record the start of the matching attributes in the pair list
629                          *      For every other operator we require the match to be present
630                          */
631                         match = fr_cursor_next_by_da(&list_cursor, check->da, check->tag);
632                         if (!match) {
633                                 if (check->op == T_OP_CMP_FALSE) continue;
634                                 goto mismatch;
635                         }
636
637                         fr_cursor_init(&list_cursor, &match);
638                         last_check = check;
639                 }
640
641                 /*
642                  *      Now iterate over all attributes of the same type.
643                  */
644                 for (match = fr_cursor_first(&list_cursor);
645                      attribute_eq(match, check);
646                      match = fr_cursor_next(&list_cursor)) {
647                         /*
648                          *      This attribute passed the filter
649                          */
650                         if (!paircmp(check, match)) goto mismatch;
651                 }
652         }
653
654         return true;
655
656 mismatch:
657         if (failed) {
658                 failed[0] = check;
659                 failed[1] = match;
660         }
661         return false;
662 }
663
664 /** Copy a single valuepair
665  *
666  * Allocate a new valuepair and copy the da from the old vp.
667  *
668  * @param[in] ctx for talloc
669  * @param[in] vp to copy.
670  * @return a copy of the input VP or NULL on error.
671  */
672 VALUE_PAIR *paircopyvp(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
673 {
674         VALUE_PAIR *n;
675
676         if (!vp) return NULL;
677
678         VERIFY_VP(vp);
679
680         n = pairalloc(ctx, vp->da);
681         if (!n) return NULL;
682
683         memcpy(n, vp, sizeof(*n));
684
685         /*
686          *      Now copy the value
687          */
688         if (vp->type == VT_XLAT) {
689                 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
690         }
691
692         n->da = dict_attr_copy(vp->da, true);
693         if (!n->da) {
694                 talloc_free(n);
695                 return NULL;
696         }
697
698         n->next = NULL;
699
700         switch (vp->da->type) {
701         case PW_TYPE_TLV:
702         case PW_TYPE_OCTETS:
703                 n->vp_octets = NULL;    /* else pairmemcpy will free vp's value */
704                 pairmemcpy(n, vp->vp_octets, n->length);
705                 break;
706
707         case PW_TYPE_STRING:
708                 n->vp_strvalue = NULL;  /* else pairstrnpy will free vp's value */
709                 pairstrncpy(n, vp->vp_strvalue, n->length);
710                 break;
711
712         default:
713                 break;
714         }
715
716         return n;
717 }
718
719 /** Copy data from one VP to another
720  *
721  * Allocate a new pair using da, and copy over the value from the specified
722  * vp.
723  *
724  * @todo Should be able to do type conversions.
725  *
726  * @param[in] ctx for talloc
727  * @param[in] da of new attribute to alloc.
728  * @param[in] vp to copy data from.
729  * @return the new valuepair.
730  */
731 VALUE_PAIR *paircopyvpdata(TALLOC_CTX *ctx, DICT_ATTR const *da, VALUE_PAIR const *vp)
732 {
733         VALUE_PAIR *n;
734
735         if (!vp) return NULL;
736
737         VERIFY_VP(vp);
738
739         /*
740          *      The types have to be identical, OR the "from" VP has
741          *      to be octets.
742          */
743         if (da->type != vp->da->type) {
744                 int length;
745                 uint8_t *p;
746                 VALUE_PAIR const **pvp;
747
748                 if (vp->da->type == PW_TYPE_OCTETS) {
749                         /*
750                          *      Decode the data.  It may be wrong!
751                          */
752                         if (rad_data2vp(da->attr, da->vendor, vp->vp_octets, vp->length, &n) < 0) {
753                                 return NULL;
754                         }
755
756                         n->type = VT_DATA;
757                         return n;
758                 }
759
760                 /*
761                  *      Else the destination type is octets
762                  */
763                 switch (vp->da->type) {
764                 default:
765                         return NULL; /* can't do it */
766
767                 case PW_TYPE_INTEGER:
768                 case PW_TYPE_IPADDR:
769                 case PW_TYPE_DATE:
770                 case PW_TYPE_IFID:
771                 case PW_TYPE_IPV6ADDR:
772                 case PW_TYPE_IPV6PREFIX:
773                 case PW_TYPE_BYTE:
774                 case PW_TYPE_SHORT:
775                 case PW_TYPE_ETHERNET:
776                 case PW_TYPE_SIGNED:
777                 case PW_TYPE_INTEGER64:
778                 case PW_TYPE_IPV4PREFIX:
779                         break;
780                 }
781
782                 n = pairalloc(ctx, da);
783                 if (!n) return NULL;
784
785                 p = talloc_array(n, uint8_t, dict_attr_sizes[vp->da->type][1] + 2);
786
787                 pvp = &vp;
788                 length = rad_vp2attr(NULL, NULL, NULL, pvp, p, dict_attr_sizes[vp->da->type][1]);
789                 if (length < 0) {
790                         pairfree(&n);
791                         return NULL;
792                 }
793
794                 pairmemcpy(n, p + 2, length - 2);
795                 talloc_free(p);
796                 return n;
797         }
798
799         n = pairalloc(ctx, da);
800         if (!n) return NULL;
801
802         memcpy(n, vp, sizeof(*n));
803         n->da = da;
804
805         if (n->type == VT_XLAT) {
806                 n->value.xlat = talloc_typed_strdup(n, n->value.xlat);
807         }
808
809         if (n->data.ptr) switch (n->da->type) {
810         case PW_TYPE_TLV:
811         case PW_TYPE_OCTETS:
812                 n->vp_octets = talloc_memdup(n, vp->vp_octets, n->length);
813                 talloc_set_type(n->vp_octets, uint8_t);
814                 break;
815
816         case PW_TYPE_STRING:
817                 n->vp_strvalue = talloc_memdup(n, vp->vp_strvalue, n->length + 1);      /* NULL byte */
818                 talloc_set_type(n->vp_strvalue, char);
819                 break;
820
821         default:
822                 break;
823         }
824
825         n->next = NULL;
826
827         return n;
828 }
829
830
831 /** Copy a pairlist.
832  *
833  * Copy all pairs from 'from' regardless of tag, attribute or vendor.
834  *
835  * @param[in] ctx for new VALUE_PAIRs to be allocated in.
836  * @param[in] from whence to copy VALUE_PAIRs.
837  * @return the head of the new VALUE_PAIR list or NULL on error.
838  */
839 VALUE_PAIR *paircopy(TALLOC_CTX *ctx, VALUE_PAIR *from)
840 {
841         vp_cursor_t src, dst;
842
843         VALUE_PAIR *out = NULL, *vp;
844
845         fr_cursor_init(&dst, &out);
846         for (vp = fr_cursor_init(&src, &from);
847              vp;
848              vp = fr_cursor_next(&src)) {
849                 VERIFY_VP(vp);
850                 vp = paircopyvp(ctx, vp);
851                 if (!vp) {
852                         pairfree(&out);
853                         return NULL;
854                 }
855                 fr_cursor_insert(&dst, vp); /* paircopy sets next pointer to NULL */
856         }
857
858         return out;
859 }
860
861 /** Copy matching pairs
862  *
863  * Copy pairs of a matching attribute number, vendor number and tag from the
864  * the input list to a new list, and returns the head of this list.
865  *
866  * @param[in] ctx for talloc
867  * @param[in] from whence to copy VALUE_PAIRs.
868  * @param[in] attr to match, if 0 input list will not be filtered by attr.
869  * @param[in] vendor to match.
870  * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
871  * @return the head of the new VALUE_PAIR list or NULL on error.
872  */
873 VALUE_PAIR *paircopy2(TALLOC_CTX *ctx, VALUE_PAIR *from,
874                       unsigned int attr, unsigned int vendor, int8_t tag)
875 {
876         vp_cursor_t src, dst;
877
878         VALUE_PAIR *out = NULL, *vp;
879
880         fr_cursor_init(&dst, &out);
881         for (vp = fr_cursor_init(&src, &from);
882              vp;
883              vp = fr_cursor_next(&src)) {
884                 VERIFY_VP(vp);
885
886                 if ((vp->da->attr != attr) || (vp->da->vendor != vendor)) {
887                         continue;
888                 }
889
890                 if (vp->da->flags.has_tag && TAG_EQ(tag, vp->tag)) {
891                         continue;
892                 }
893
894                 vp = paircopyvp(ctx, vp);
895                 if (!vp) {
896                         pairfree(&out);
897                         return NULL;
898                 }
899                 fr_cursor_insert(&dst, vp);
900         }
901
902         return out;
903 }
904
905 /** Steal all members of a VALUE_PAIR list
906  *
907  * @param[in] ctx to move VALUE_PAIRs into
908  * @param[in] from VALUE_PAIRs to move into the new context.
909  */
910 VALUE_PAIR *pairsteal(TALLOC_CTX *ctx, VALUE_PAIR *from)
911 {
912         vp_cursor_t cursor;
913         VALUE_PAIR *vp;
914
915         for (vp = fr_cursor_init(&cursor, &from);
916              vp;
917              vp = fr_cursor_next(&cursor)) {
918                 (void) talloc_steal(ctx, vp);
919         }
920
921         return from;
922 }
923
924 /** Move pairs from source list to destination list respecting operator
925  *
926  * @note This function does some additional magic that's probably not needed
927  *       in most places. Consider using radius_pairmove in server code.
928  *
929  * @note pairfree should be called on the head of the source list to free
930  *       unmoved attributes (if they're no longer needed).
931  *
932  * @note Does not respect tags when matching.
933  *
934  * @param[in] ctx for talloc
935  * @param[in,out] to destination list.
936  * @param[in,out] from source list.
937  *
938  * @see radius_pairmove
939  */
940 void pairmove(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from)
941 {
942         VALUE_PAIR *i, *found;
943         VALUE_PAIR *head_new, **tail_new;
944         VALUE_PAIR **tail_from;
945
946         if (!to || !from || !*from) return;
947
948         /*
949          *      We're editing the "to" list while we're adding new
950          *      attributes to it.  We don't want the new attributes to
951          *      be edited, so we create an intermediate list to hold
952          *      them during the editing process.
953          */
954         head_new = NULL;
955         tail_new = &head_new;
956
957         /*
958          *      We're looping over the "from" list, moving some
959          *      attributes out, but leaving others in place.
960          */
961         tail_from = from;
962         while ((i = *tail_from) != NULL) {
963                 VERIFY_VP(i);
964
965                 /*
966                  *      We never move Fall-Through.
967                  */
968                 if (!i->da->vendor && i->da->attr == PW_FALL_THROUGH) {
969                         tail_from = &(i->next);
970                         continue;
971                 }
972
973                 /*
974                  *      Unlike previous versions, we treat all other
975                  *      attributes as normal.  i.e. there's no special
976                  *      treatment for passwords or Hint.
977                  */
978
979                 switch (i->op) {
980                         /*
981                          *      Anything else are operators which
982                          *      shouldn't occur.  We ignore them, and
983                          *      leave them in place.
984                          */
985                         default:
986                                 tail_from = &(i->next);
987                                 continue;
988
989                         /*
990                          *      Add it to the "to" list, but only if
991                          *      it doesn't already exist.
992                          */
993                         case T_OP_EQ:
994                                 found = pairfind(*to, i->da->attr, i->da->vendor, TAG_ANY);
995                                 if (!found) goto do_add;
996
997                                 tail_from = &(i->next);
998                                 continue;
999
1000                         /*
1001                          *      Add it to the "to" list, and delete any attribute
1002                          *      of the same vendor/attr which already exists.
1003                          */
1004                         case T_OP_SET:
1005                                 found = pairfind(*to, i->da->attr, i->da->vendor, TAG_ANY);
1006                                 if (!found) goto do_add;
1007
1008                                 /*
1009                                  *      Do NOT call pairdelete() here,
1010                                  *      due to issues with re-writing
1011                                  *      "request->username".
1012                                  *
1013                                  *      Everybody calls pairmove, and
1014                                  *      expects it to work.  We can't
1015                                  *      update request->username here,
1016                                  *      so instead we over-write the
1017                                  *      vp that it's pointing to.
1018                                  */
1019                                 switch (found->da->type) {
1020                                         VALUE_PAIR *j;
1021
1022                                         default:
1023                                                 j = found->next;
1024                                                 memcpy(found, i, sizeof(*found));
1025                                                 found->next = j;
1026                                                 break;
1027
1028                                         case PW_TYPE_TLV:
1029                                                 pairmemsteal(found, i->vp_tlv);
1030                                                 i->vp_tlv = NULL;
1031                                                 break;
1032
1033                                         case PW_TYPE_OCTETS:
1034                                                 pairmemsteal(found, i->vp_octets);
1035                                                 i->vp_octets = NULL;
1036                                                 break;
1037
1038                                         case PW_TYPE_STRING:
1039                                                 pairstrsteal(found, i->vp_strvalue);
1040                                                 i->vp_strvalue = NULL;
1041                                                 found->tag = i->tag;
1042                                                 break;
1043                                 }
1044
1045                                 /*
1046                                  *      Delete *all* of the attributes
1047                                  *      of the same number.
1048                                  */
1049                                 pairdelete(&found->next,
1050                                            found->da->attr,
1051                                            found->da->vendor, TAG_ANY);
1052
1053                                 /*
1054                                  *      Remove this attribute from the
1055                                  *      "from" list.
1056                                  */
1057                                 *tail_from = i->next;
1058                                 i->next = NULL;
1059                                 pairfree(&i);
1060                                 continue;
1061
1062                         /*
1063                          *      Move it from the old list and add it
1064                          *      to the new list.
1065                          */
1066                         case T_OP_ADD:
1067                 do_add:
1068                                 *tail_from = i->next;
1069                                 i->next = NULL;
1070                                 *tail_new = talloc_steal(ctx, i);
1071                                 tail_new = &(i->next);
1072                                 continue;
1073                 }
1074         } /* loop over the "from" list. */
1075
1076         /*
1077          *      Take the "new" list, and append it to the "to" list.
1078          */
1079         pairadd(to, head_new);
1080 }
1081
1082 /** Move matching pairs between VALUE_PAIR lists
1083  *
1084  * Move pairs of a matching attribute number, vendor number and tag from the
1085  * the input list to the output list.
1086  *
1087  * @note pairfree should be called on the head of the old list to free unmoved
1088          attributes (if they're no longer needed).
1089  *
1090  * @param[in] ctx for talloc
1091  * @param[in,out] to destination list.
1092  * @param[in,out] from source list.
1093  * @param[in] attr to match, if PW_VENDOR_SPECIFIC and vendor 0, only VSAs will
1094  *            be copied.  If 0 and 0, all attributes will match
1095  * @param[in] vendor to match.
1096  * @param[in] tag to match, TAG_ANY matches any tag, TAG_NONE matches tagless VPs.
1097  */
1098 void pairfilter(TALLOC_CTX *ctx, VALUE_PAIR **to, VALUE_PAIR **from, unsigned int attr, unsigned int vendor, int8_t tag)
1099 {
1100         VALUE_PAIR *to_tail, *i, *next;
1101         VALUE_PAIR *iprev = NULL;
1102
1103         /*
1104          *      Find the last pair in the "to" list and put it in "to_tail".
1105          *
1106          *      @todo: replace the "if" with "VALUE_PAIR **tail"
1107          */
1108         if (*to != NULL) {
1109                 to_tail = *to;
1110                 for(i = *to; i; i = i->next) {
1111                         VERIFY_VP(i);
1112                         to_tail = i;
1113                 }
1114         } else
1115                 to_tail = NULL;
1116
1117         /*
1118          *      Attr/vendor of 0 means "move them all".
1119          *      It's better than "pairadd(foo,bar);bar=NULL"
1120          */
1121         if ((vendor == 0) && (attr == 0)) {
1122                 if (*to) {
1123                         to_tail->next = *from;
1124                 } else {
1125                         *to = *from;
1126                 }
1127
1128                 for (i = *from; i; i = i->next) {
1129                         (void) talloc_steal(ctx, i);
1130                 }
1131
1132                 *from = NULL;
1133                 return;
1134         }
1135
1136         for(i = *from; i; i = next) {
1137                 VERIFY_VP(i);
1138                 next = i->next;
1139
1140                 if (i->da->flags.has_tag && TAG_EQ(tag, i->tag)) {
1141                         continue;
1142                 }
1143
1144                 /*
1145                  *      vendor=0, attr = PW_VENDOR_SPECIFIC means
1146                  *      "match any vendor attribute".
1147                  */
1148                 if ((vendor == 0) && (attr == PW_VENDOR_SPECIFIC)) {
1149                         /*
1150                          *      It's a VSA: move it over.
1151                          */
1152                         if (i->da->vendor != 0) goto move;
1153
1154                         /*
1155                          *      It's Vendor-Specific: move it over.
1156                          */
1157                         if (i->da->attr == attr) goto move;
1158
1159                         /*
1160                          *      It's not a VSA: ignore it.
1161                          */
1162                         iprev = i;
1163                         continue;
1164                 }
1165
1166                 /*
1167                  *      If it isn't an exact match, ignore it.
1168                  */
1169                 if (!((i->da->vendor == vendor) && (i->da->attr == attr))) {
1170                         iprev = i;
1171                         continue;
1172                 }
1173
1174         move:
1175                 /*
1176                  *      Remove the attribute from the "from" list.
1177                  */
1178                 if (iprev)
1179                         iprev->next = next;
1180                 else
1181                         *from = next;
1182
1183                 /*
1184                  *      Add the attribute to the "to" list.
1185                  */
1186                 if (to_tail)
1187                         to_tail->next = i;
1188                 else
1189                         *to = i;
1190                 to_tail = i;
1191                 i->next = NULL;
1192                 (void) talloc_steal(ctx, i);
1193         }
1194 }
1195
1196 static char const *hextab = "0123456789abcdef";
1197
1198 /** Convert string value to native attribute value
1199  *
1200  * @param vp to assign value to.
1201  * @param value string to convert. Binary safe for variable length values if len is provided.
1202  * @param inlen may be 0 in which case strlen(len) is used to determine length, else inline
1203  *        should be the length of the string or sub string to parse.
1204  * @return true on success, else false.
1205  */
1206 int pairparsevalue(VALUE_PAIR *vp, char const *value, size_t inlen)
1207 {
1208         char const      *cs;
1209         DICT_VALUE      *dval;
1210         size_t          len;
1211         char            buffer[256];
1212
1213         if (!value) return -1;
1214         VERIFY_VP(vp);
1215
1216         /*
1217          *      It's a comparison, not a real VALUE_PAIR, copy the string over verbatim
1218          */
1219         if ((vp->op == T_OP_REG_EQ) || (vp->op == T_OP_REG_NE)) {
1220                 pairstrcpy(vp, value);  /* Icky hacky ewww */
1221                 goto finish;
1222         }
1223
1224         len = (inlen == 0) ? strlen(value) : inlen;
1225
1226         /*
1227          *      It's a variable length type so we just alloc a new buffer
1228          *      of size len and copy.
1229          */
1230         switch(vp->da->type) {
1231         case PW_TYPE_STRING:
1232         {
1233                 size_t          vp_len;
1234                 char const      *cp;
1235                 char            *p;
1236                 int             x;
1237
1238                 /*
1239                  *      Do escaping here
1240                  */
1241                 vp->vp_strvalue = p = talloc_memdup(vp, value, len + 1);
1242                 p[len] = '\0';
1243                 talloc_set_type(p, char);
1244
1245                 cp = value;
1246                 vp_len = 0;
1247                 while (*cp) {
1248                         char c = *cp++;
1249
1250                         if (c == '\\') switch (*cp) {
1251                         case 'r':
1252                                 c = '\r';
1253                                 cp++;
1254                                 break;
1255                         case 'n':
1256                                 c = '\n';
1257                                 cp++;
1258                                 break;
1259                         case 't':
1260                                 c = '\t';
1261                                 cp++;
1262                                 break;
1263                         case '"':
1264                                 c = '"';
1265                                 cp++;
1266                                 break;
1267                         case '\'':
1268                                 c = '\'';
1269                                 cp++;
1270                                 break;
1271                         case '\\':
1272                                 c = '\\';
1273                                 cp++;
1274                                 break;
1275                         case '`':
1276                                 c = '`';
1277                                 cp++;
1278                                 break;
1279                         case '\0':
1280                                 c = '\\'; /* no cp++ */
1281                                 break;
1282                         default:
1283                                 if ((cp[0] >= '0') &&
1284                                     (cp[0] <= '9') &&
1285                                     (cp[1] >= '0') &&
1286                                     (cp[1] <= '9') &&
1287                                     (cp[2] >= '0') &&
1288                                     (cp[2] <= '9') &&
1289                                     (sscanf(cp, "%3o", &x) == 1)) {
1290                                         c = x;
1291                                         cp += 3;
1292
1293                                 } else if (cp[0]) {
1294                                         /*
1295                                          *      \p --> p
1296                                          */
1297                                         c = *cp++;
1298                                 } /* else at EOL \ --> \ */
1299                         }
1300                         *p++ = c;
1301                         vp_len++;
1302                 }
1303                 *p = '\0';
1304                 vp->length = vp_len;
1305         }
1306                 goto finish;
1307
1308         /* raw octets: 0x01020304... */
1309         case PW_TYPE_VSA:
1310                 if (strcmp(value, "ANY") == 0) {
1311                         vp->length = 0;
1312                         goto finish;
1313                 } /* else it's hex */
1314
1315         case PW_TYPE_OCTETS:
1316         {
1317                 uint8_t *p;
1318
1319                 /*
1320                  *      No 0x prefix, just copy verbatim.
1321                  */
1322                 if ((len < 2) || (strncasecmp(value, "0x", 2) != 0)) {
1323                         pairmemcpy(vp, (uint8_t const *) value, len);
1324                         goto finish;
1325                 }
1326
1327
1328 #ifdef WITH_ASCEND_BINARY
1329         do_octets:
1330 #endif
1331                 len -= 2;
1332
1333                 /*
1334                  *      Invalid.
1335                  */
1336                 if ((len & 0x01) != 0) {
1337                         fr_strerror_printf("Length of Hex String is not even, got %zu bytes", vp->length);
1338                         return -1;
1339                 }
1340
1341                 vp->length = len >> 1;
1342                 p = talloc_array(vp, uint8_t, vp->length);
1343                 if (fr_hex2bin(p, value + 2, len) != vp->length) {
1344                         talloc_free(p);
1345                         fr_strerror_printf("Invalid hex data");
1346                         return -1;
1347                 }
1348
1349                 vp->vp_octets = p;
1350         }
1351                 goto finish;
1352
1353         case PW_TYPE_ABINARY:
1354 #ifdef WITH_ASCEND_BINARY
1355                 if ((len > 1) && (strncasecmp(value, "0x", 2) == 0)) goto do_octets;
1356
1357                 if (ascend_parse_filter(vp, value, len) < 0 ) {
1358                         /* Allow ascend_parse_filter's strerror to bubble up */
1359                         return -1;
1360                 }
1361                 goto finish;
1362 #else
1363                 /*
1364                  *      If Ascend binary is NOT defined,
1365                  *      then fall through to raw octets, so that
1366                  *      the user can at least make them by hand...
1367                  */
1368                 goto do_octets;
1369 #endif
1370
1371         /* don't use this! */
1372         case PW_TYPE_TLV:
1373         {
1374                 uint8_t *p;
1375
1376                 if ((len < 2) || (len & 0x01) || (strncasecmp(value, "0x", 2) != 0)) {
1377                         fr_strerror_printf("Invalid TLV specification");
1378                         return -1;
1379                 }
1380                 len -= 2;
1381
1382                 vp->length = len >> 1;
1383                 p = talloc_array(vp, uint8_t, vp->length);
1384                 if (!p) {
1385                         fr_strerror_printf("No memory");
1386                         return -1;
1387                 }
1388                 if (fr_hex2bin(p, value + 2, len) != vp->length) {
1389                         fr_strerror_printf("Invalid hex data in TLV");
1390                         return -1;
1391                 }
1392
1393                 vp->vp_tlv = p;
1394         }
1395                 goto finish;
1396
1397         default:
1398                 break;
1399         }
1400
1401         /*
1402          *      It's a fixed size type, copy to a temporary buffer and
1403          *      \0 terminate if insize >= 0.
1404          */
1405         if (inlen > 0) {
1406                 if (len >= sizeof(buffer)) {
1407                         fr_strerror_printf("Temporary buffer too small");
1408                         return -1;
1409                 }
1410
1411                 memcpy(buffer, value, inlen);
1412                 buffer[inlen] = '\0';
1413                 value = buffer;
1414         }
1415
1416         switch(vp->da->type) {
1417         case PW_TYPE_IPADDR:
1418         {
1419                 char const *p;
1420                 fr_ipaddr_t ipaddr;
1421                 char ipv4[16];
1422                 /*
1423                  *      FIXME: complain if hostname
1424                  *      cannot be resolved, or resolve later!
1425                  */
1426                 p = NULL;
1427
1428                 /*
1429                  *      Convert things which are obviously integers to IP addresses
1430                  *
1431                  *      We assume the number is the bigendian representation of the
1432                  *      IP address.
1433                  */
1434                 if (is_integer(value)) {
1435                         vp->vp_ipaddr = htonl(atol(value));
1436                         break;
1437                 }
1438
1439                 /*
1440                  *      Certain applications/databases print IPv4 addresses with a
1441                  *      /32 suffix. Strip it off if the mask is 32, else error out.
1442                  */
1443                 p = strchr(value, '/');
1444                 if (p) {
1445                         if ((p[1] != '3') || (p[2] != '2') || (p[3] != '\0')) {
1446                                 fr_strerror_printf("Invalid IP address suffix \"%s\".  Only '/32' permitted "
1447                                                    "for non-prefix types", p);
1448                                 return -1;
1449                         }
1450
1451                         strlcpy(ipv4, value, sizeof(ipv4));
1452                         ipv4[p - value] = '\0';
1453                         cs = ipv4;
1454                 } else {
1455                         cs = value;
1456                 }
1457
1458                 if (ip_hton(cs, AF_INET, &ipaddr) < 0) {
1459                         fr_strerror_printf("Failed to find IP address for %s", cs);
1460                         return -1;
1461                 }
1462
1463                 vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1464                 vp->length = 4;
1465         }
1466                 break;
1467
1468         case PW_TYPE_BYTE:
1469         {
1470                 char *p;
1471                 vp->length = 1;
1472
1473                 /*
1474                  *      Note that ALL integers are unsigned!
1475                  */
1476                 vp->vp_integer = fr_strtoul(value, &p);
1477                 if (!*p) {
1478                         if (vp->vp_integer > 255) {
1479                                 fr_strerror_printf("Byte value \"%s\" is larger than 255", value);
1480                                 return -1;
1481                         }
1482                         break;
1483                 }
1484                 if (is_whitespace(p)) break;
1485         }
1486                 goto check_for_value;
1487
1488         case PW_TYPE_SHORT:
1489         {
1490                 char *p;
1491
1492                 /*
1493                  *      Note that ALL integers are unsigned!
1494                  */
1495                 vp->vp_integer = fr_strtoul(value, &p);
1496                 vp->length = 2;
1497                 if (!*p) {
1498                         if (vp->vp_integer > 65535) {
1499                                 fr_strerror_printf("Byte value \"%s\" is larger than 65535", value);
1500                                 return -1;
1501                         }
1502                         break;
1503                 }
1504                 if (is_whitespace(p)) break;
1505         }
1506                 goto check_for_value;
1507
1508         case PW_TYPE_INTEGER:
1509         {
1510                 char *p;
1511
1512                 /*
1513                  *      Note that ALL integers are unsigned!
1514                  */
1515                 vp->vp_integer = fr_strtoul(value, &p);
1516                 vp->length = 4;
1517                 if (!*p) break;
1518                 if (is_whitespace(p)) break;
1519
1520         check_for_value:
1521                 /*
1522                  *      Look for the named value for the given
1523                  *      attribute.
1524                  */
1525                 if ((dval = dict_valbyname(vp->da->attr, vp->da->vendor, value)) == NULL) {
1526                         fr_strerror_printf("Unknown value '%s' for attribute '%s'", value, vp->da->name);
1527                         return -1;
1528                 }
1529                 vp->vp_integer = dval->value;
1530         }
1531                 break;
1532
1533         case PW_TYPE_INTEGER64:
1534         {
1535                 uint64_t y;
1536
1537                 /*
1538                  *      Note that ALL integers are unsigned!
1539                  */
1540                 if (sscanf(value, "%" PRIu64, &y) != 1) {
1541                         fr_strerror_printf("Invalid value '%s' for attribute '%s'",
1542                                            value, vp->da->name);
1543                         return -1;
1544                 }
1545                 vp->vp_integer64 = y;
1546                 vp->length = 8;
1547         }
1548                 break;
1549
1550         case PW_TYPE_DATE:
1551         {
1552                 /*
1553                  *      time_t may be 64 bits, whule vp_date
1554                  *      MUST be 32-bits.  We need an
1555                  *      intermediary variable to handle
1556                  *      the conversions.
1557                  */
1558                 time_t date;
1559
1560                 if (fr_get_time(value, &date) < 0) {
1561                         fr_strerror_printf("failed to parse time string "
1562                                    "\"%s\"", value);
1563                         return -1;
1564                 }
1565
1566                 vp->vp_date = date;
1567                 vp->length = 4;
1568         }
1569
1570                 break;
1571
1572         case PW_TYPE_IFID:
1573                 if (ifid_aton(value, (void *) &vp->vp_ifid) == NULL) {
1574                         fr_strerror_printf("Failed to parse interface-id string \"%s\"", value);
1575                         return -1;
1576                 }
1577                 vp->length = 8;
1578                 break;
1579
1580         case PW_TYPE_IPV6ADDR:
1581         {
1582                 fr_ipaddr_t ipaddr;
1583
1584                 if (ip_hton(value, AF_INET6, &ipaddr) < 0) {
1585                         fr_strerror_printf("failed to parse IPv6 address "
1586                                            "string \"%s\": %s", value, fr_strerror());
1587                         return -1;
1588                 }
1589                 vp->vp_ipv6addr = ipaddr.ipaddr.ip6addr;
1590                 vp->length = 16; /* length of IPv6 address */
1591         }
1592                 break;
1593
1594         case PW_TYPE_IPV6PREFIX:
1595         {
1596                 char const *p;
1597                 unsigned int prefix;
1598                 char *eptr;
1599
1600                 p = strchr(value, '/');
1601                 if (!p || ((p - value) >= 256)) {
1602                         fr_strerror_printf("invalid IPv6 prefix string \"%s\"", value);
1603                         return -1;
1604                 }
1605
1606                 /*
1607                  *      Copy string to temporary buffer if we didn't do it earlier
1608                  */
1609                 if (inlen == 0) memcpy(buffer, value, p - value);
1610                 buffer[p - value] = '\0';
1611
1612                 if (inet_pton(AF_INET6, buffer, vp->vp_ipv6prefix + 2) <= 0) {
1613                         fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1614                         return -1;
1615                 }
1616
1617                 prefix = strtoul(p + 1, &eptr, 10);
1618                 if ((prefix > 128) || *eptr) {
1619                         fr_strerror_printf("failed to parse IPv6 address string \"%s\"", value);
1620                         return -1;
1621                 }
1622                 vp->vp_ipv6prefix[1] = prefix;
1623
1624                 if (prefix < 128) {
1625                         struct in6_addr addr;
1626
1627                         addr = fr_ipaddr_mask6((struct in6_addr *)(&vp->vp_ipv6prefix[2]), prefix);
1628                         memcpy(vp->vp_ipv6prefix + 2, &addr, sizeof(addr));
1629                 }
1630                 vp->length = 16 + 2;
1631         }
1632                 break;
1633
1634         case PW_TYPE_IPV4PREFIX:
1635         {
1636                 char *p;
1637                 unsigned int prefix;
1638                 char *eptr;
1639
1640                 p = strchr(value, '/');
1641
1642                 /*
1643                  *      192.0.2.2 is parsed as if it was /32
1644                  */
1645                 if (!p) {
1646                         vp->vp_ipv4prefix[1] = 32;
1647
1648                         if (inet_pton(AF_INET, value, vp->vp_ipv4prefix + 2) <= 0) {
1649                                 fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1650                                 return -1;
1651                         }
1652                         vp->length = sizeof(vp->vp_ipv4prefix);
1653                         break;
1654                 }
1655
1656                 /*
1657                  *      Otherwise parse the prefix
1658                  */
1659                 if ((size_t)(p - value) >= sizeof(buffer)) {
1660                         fr_strerror_printf("invalid IPv4 prefix string \"%s\"", value);
1661                         return -1;
1662                 }
1663
1664                 /*
1665                  *      Copy string to temporary buffer if we didn't do it earlier
1666                  */
1667                 if (inlen == 0) memcpy(buffer, value, p - value);
1668                 buffer[p - value] = '\0';
1669
1670                 if (inet_pton(AF_INET, buffer, vp->vp_ipv4prefix + 2) <= 0) {
1671                         fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1672                         return -1;
1673                 }
1674
1675                 prefix = strtoul(p + 1, &eptr, 10);
1676                 if ((prefix > 32) || *eptr) {
1677                         fr_strerror_printf("failed to parse IPv4 address string \"%s\"", value);
1678                         return -1;
1679                 }
1680                 vp->vp_ipv4prefix[1] = prefix;
1681
1682                 if (prefix < 32) {
1683                         struct in_addr addr;
1684
1685                         addr = fr_ipaddr_mask((struct in_addr *)(&vp->vp_ipv4prefix[2]), prefix);
1686                         memcpy(vp->vp_ipv4prefix + 2, &addr, sizeof(addr));
1687                 }
1688
1689                 vp->length = sizeof(vp->vp_ipv4prefix);
1690         }
1691                 break;
1692
1693         case PW_TYPE_ETHERNET:
1694         {
1695                 char const *c1, *c2, *cp;
1696                 size_t vp_len = 0;
1697
1698                 /*
1699                  *      Convert things which are obviously integers to Ethernet addresses
1700                  *
1701                  *      We assume the number is the bigendian representation of the
1702                  *      ethernet address.
1703                  */
1704                 if (is_integer(value)) {
1705                         uint64_t integer = htonll(atoll(value));
1706
1707                         memcpy(&vp->vp_ether, &integer, sizeof(vp->vp_ether));
1708                         break;
1709                 }
1710
1711                 cp = value;
1712                 while (*cp) {
1713                         if (cp[1] == ':') {
1714                                 c1 = hextab;
1715                                 c2 = memchr(hextab, tolower((int) cp[0]), 16);
1716                                 cp += 2;
1717                         } else if ((cp[1] != '\0') && ((cp[2] == ':') || (cp[2] == '\0'))) {
1718                                 c1 = memchr(hextab, tolower((int) cp[0]), 16);
1719                                 c2 = memchr(hextab, tolower((int) cp[1]), 16);
1720                                 cp += 2;
1721                                 if (*cp == ':') cp++;
1722                         } else {
1723                                 c1 = c2 = NULL;
1724                         }
1725                         if (!c1 || !c2 || (vp_len >= sizeof(vp->vp_ether))) {
1726                                 fr_strerror_printf("failed to parse Ethernet address \"%s\"", value);
1727                                 return -1;
1728                         }
1729                         vp->vp_ether[vp_len] = ((c1-hextab)<<4) + (c2-hextab);
1730                         vp_len++;
1731                 }
1732
1733                 vp->length = 6;
1734         }
1735                 break;
1736
1737         /*
1738          *      Crazy polymorphic (IPv4/IPv6) attribute type for WiMAX.
1739          *
1740          *      We try and make is saner by replacing the original
1741          *      da, with either an IPv4 or IPv6 da type.
1742          *
1743          *      These are not dynamic da, and will have the same vendor
1744          *      and attribute as the original.
1745          */
1746         case PW_TYPE_COMBO_IP:
1747         {
1748                 DICT_ATTR const *da;
1749
1750                 if (inet_pton(AF_INET6, value, &vp->vp_ipv6addr) > 0) {
1751                         da = dict_attrbytype(vp->da->attr, vp->da->vendor, PW_TYPE_IPV6ADDR);
1752                         if (!da) {
1753                                 fr_strerror_printf("Cannot find ipv6addr for %s", vp->da->name);
1754                                 return -1;
1755                         }
1756
1757                         vp->length = 16; /* length of IPv6 address */
1758                 } else {
1759                         fr_ipaddr_t ipaddr;
1760
1761                         da = dict_attrbytype(vp->da->attr, vp->da->vendor,
1762                                              PW_TYPE_IPADDR);
1763                         if (!da) {
1764                                 fr_strerror_printf("Cannot find ipaddr for %s", vp->da->name);
1765                                 return -1;
1766                         }
1767
1768                         if (ip_hton(value, AF_INET, &ipaddr) < 0) {
1769                                 fr_strerror_printf("Failed to find IPv4 address for %s", value);
1770                                 return -1;
1771                         }
1772
1773                         vp->vp_ipaddr = ipaddr.ipaddr.ip4addr.s_addr;
1774                         vp->length = 4;
1775                 }
1776
1777                 vp->da = da;
1778         }
1779                 break;
1780
1781         case PW_TYPE_SIGNED:
1782                 /* Damned code for 1 WiMAX attribute */
1783                 vp->vp_signed = (int32_t) strtol(value, NULL, 10);
1784                 vp->length = 4;
1785                 break;
1786
1787                 /*
1788                  *  Anything else.
1789                  */
1790         default:
1791                 fr_strerror_printf("unknown attribute type %d", vp->da->type);
1792                 return -1;
1793         }
1794
1795 finish:
1796         vp->type = VT_DATA;
1797         return 0;
1798 }
1799
1800 /** Use simple heuristics to create an VALUE_PAIR from an unknown address string
1801  *
1802  * If a DICT_ATTR is not provided for the address type, parsing will fail with
1803  * and error.
1804  *
1805  * @param ctx to allocate VP in.
1806  * @param value IPv4/IPv6 address/prefix string.
1807  * @param ipv4 dictionary attribute to use for an IPv4 address.
1808  * @param ipv6 dictionary attribute to use for an IPv6 address.
1809  * @param ipv4_prefix dictionary attribute to use for an IPv4 prefix.
1810  * @param ipv6_prefix dictionary attribute to use for an IPv6 prefix.
1811  * @return NULL on error, or new VALUE_PAIR.
1812  */
1813 VALUE_PAIR *pairmake_ip(TALLOC_CTX *ctx, char const *value, DICT_ATTR *ipv4, DICT_ATTR *ipv6,
1814                         DICT_ATTR *ipv4_prefix, DICT_ATTR *ipv6_prefix)
1815 {
1816         VALUE_PAIR *vp;
1817         DICT_ATTR *da = NULL;
1818
1819         if (!fr_assert(ipv4 || ipv6 || ipv4_prefix || ipv6_prefix)) {
1820                 return NULL;
1821         }
1822
1823         /* No point in repeating the work of pairparsevalue */
1824         if (strchr(value, ':')) {
1825                 if (strchr(value, '/')) {
1826                         da = ipv6_prefix;
1827                         goto finish;
1828                 }
1829
1830                 da = ipv6;
1831                 goto finish;
1832         }
1833
1834         if (strchr(value, '/')) {
1835                 da = ipv4_prefix;
1836                 goto finish;
1837         }
1838
1839         if (ipv4) {
1840                 da = ipv4;
1841                 goto finish;
1842         }
1843
1844         fr_strerror_printf("Invalid IP value specified, allowed types are %s%s%s%s",
1845                            ipv4 ? "ipaddr " : "", ipv6 ? "ipv6addr " : "",
1846                            ipv4_prefix ? "ipv4prefix " : "", ipv6_prefix ? "ipv6prefix" : "");
1847
1848 finish:
1849         vp = pairalloc(ctx, da);
1850         if (!vp) return NULL;
1851         if (pairparsevalue(vp, value, 0) < 0) {
1852                 talloc_free(vp);
1853                 return NULL;
1854         }
1855
1856         return vp;
1857 }
1858
1859
1860 /** Create a valuepair from an ASCII attribute and value
1861  *
1862  * Where the attribute name is in the form:
1863  *  - Attr-%d
1864  *  - Attr-%d.%d.%d...
1865  *  - Vendor-%d-Attr-%d
1866  *  - VendorName-Attr-%d
1867  *
1868  * @param ctx for talloc
1869  * @param attribute name to parse.
1870  * @param value to parse (must be a hex string).
1871  * @param op to assign to new valuepair.
1872  * @return new valuepair or NULL on error.
1873  */
1874 static VALUE_PAIR *pairmake_any(TALLOC_CTX *ctx,
1875                                 char const *attribute, char const *value,
1876                                 FR_TOKEN op)
1877 {
1878         VALUE_PAIR      *vp;
1879         DICT_ATTR const *da;
1880
1881         uint8_t         *data;
1882         size_t          size;
1883
1884         da = dict_attrunknownbyname(attribute, true);
1885         if (!da) return NULL;
1886
1887         /*
1888          *      Unknown attributes MUST be of type 'octets'
1889          */
1890         if (value && (strncasecmp(value, "0x", 2) != 0)) {
1891                 fr_strerror_printf("Unknown attribute \"%s\" requires a hex "
1892                                    "string, not \"%s\"", attribute, value);
1893
1894                 dict_attr_free(&da);
1895                 return NULL;
1896         }
1897
1898         /*
1899          *      We've now parsed the attribute properly, Let's create
1900          *      it.  This next stop also looks the attribute up in the
1901          *      dictionary, and creates the appropriate type for it.
1902          */
1903         vp = pairalloc(ctx, da);
1904         if (!vp) {
1905                 dict_attr_free(&da);
1906                 return NULL;
1907         }
1908
1909         vp->op = (op == 0) ? T_OP_EQ : op;
1910
1911         if (!value) return vp;
1912
1913         size = strlen(value + 2);
1914         vp->length = size >> 1;
1915         data = talloc_array(vp, uint8_t, vp->length);
1916
1917         if (fr_hex2bin(data, value + 2, size) != vp->length) {
1918                 fr_strerror_printf("Invalid hex string");
1919                 talloc_free(vp);
1920                 return NULL;
1921         }
1922
1923         vp->vp_octets = data;
1924         vp->type = VT_DATA;
1925         return vp;
1926 }
1927
1928
1929 /** Create a VALUE_PAIR from ASCII strings
1930  *
1931  * Converts an attribute string identifier (with an optional tag qualifier)
1932  * and value string into a VALUE_PAIR.
1933  *
1934  * The string value is parsed according to the type of VALUE_PAIR being created.
1935  *
1936  * @param[in] ctx for talloc
1937  * @param[in] vps list where the attribute will be added (optional)
1938  * @param[in] attribute name.
1939  * @param[in] value attribute value (may be NULL if value will be set later).
1940  * @param[in] op to assign to new VALUE_PAIR.
1941  * @return a new VALUE_PAIR.
1942  */
1943 VALUE_PAIR *pairmake(TALLOC_CTX *ctx, VALUE_PAIR **vps,
1944                      char const *attribute, char const *value, FR_TOKEN op)
1945 {
1946         DICT_ATTR const *da;
1947         VALUE_PAIR      *vp;
1948         char            *tc, *ts;
1949         int8_t          tag;
1950         bool            found_tag;
1951         char            buffer[256];
1952         char const      *attrname = attribute;
1953
1954         /*
1955          *    Check for tags in 'Attribute:Tag' format.
1956          */
1957         found_tag = false;
1958         tag = TAG_ANY;
1959
1960         ts = strrchr(attribute, ':');
1961         if (ts && !ts[1]) {
1962                 fr_strerror_printf("Invalid tag for attribute %s", attribute);
1963                 return NULL;
1964         }
1965
1966         if (ts && ts[1]) {
1967                 strlcpy(buffer, attribute, sizeof(buffer));
1968                 attrname = buffer;
1969                 ts = strrchr(attrname, ':');
1970                 if (!ts) return NULL;
1971
1972                  /* Colon found with something behind it */
1973                  if (ts[1] == '*' && ts[2] == 0) {
1974                          /* Wildcard tag for check items */
1975                          tag = TAG_ANY;
1976                          *ts = '\0';
1977                  } else if ((ts[1] >= '0') && (ts[1] <= '9')) {
1978                          /* It's not a wild card tag */
1979                          tag = strtol(ts + 1, &tc, 0);
1980                          if (tc && !*tc && TAG_VALID_ZERO(tag))
1981                                  *ts = '\0';
1982                          else tag = TAG_ANY;
1983                  } else {
1984                          fr_strerror_printf("Invalid tag for attribute %s", attribute);
1985                          return NULL;
1986                  }
1987                  found_tag = true;
1988         }
1989
1990         /*
1991          *      It's not found in the dictionary, so we use
1992          *      another method to create the attribute.
1993          */
1994         da = dict_attrbyname(attrname);
1995         if (!da) {
1996                 vp = pairmake_any(ctx, attrname, value, op);
1997                 if (vp && vps) pairadd(vps, vp);
1998                 return vp;
1999         }
2000
2001         /*      Check for a tag in the 'Merit' format of:
2002          *      :Tag:Value.  Print an error if we already found
2003          *      a tag in the Attribute.
2004          */
2005
2006         if (value && (*value == ':' && da->flags.has_tag)) {
2007                 /* If we already found a tag, this is invalid */
2008                 if(found_tag) {
2009                         fr_strerror_printf("Duplicate tag %s for attribute %s",
2010                                    value, da->name);
2011                         DEBUG("Duplicate tag %s for attribute %s\n",
2012                                    value, da->name);
2013                         return NULL;
2014                 }
2015                 /* Colon found and attribute allows a tag */
2016                 if (value[1] == '*' && value[2] == ':') {
2017                        /* Wildcard tag for check items */
2018                        tag = TAG_ANY;
2019                        value += 3;
2020                 } else {
2021                        /* Real tag */
2022                        tag = strtol(value + 1, &tc, 0);
2023                        if (tc && *tc==':' && TAG_VALID_ZERO(tag))
2024                             value = tc + 1;
2025                        else tag = 0;
2026                 }
2027         }
2028
2029         vp = pairalloc(ctx, da);
2030         if (!vp) return NULL;
2031         vp->op = (op == 0) ? T_OP_EQ : op;
2032         vp->tag = tag;
2033
2034         switch (vp->op) {
2035         case T_OP_CMP_TRUE:
2036         case T_OP_CMP_FALSE:
2037                 vp->vp_strvalue = NULL;
2038                 vp->length = 0;
2039                 value = NULL;   /* ignore it! */
2040                 break;
2041
2042                 /*
2043                  *      Regular expression comparison of integer attributes
2044                  *      does a STRING comparison of the names of their
2045                  *      integer attributes.
2046                  */
2047         case T_OP_REG_EQ:       /* =~ */
2048         case T_OP_REG_NE:       /* !~ */
2049         {
2050
2051                 int compare;
2052                 regex_t reg;
2053 #ifndef WITH_REGEX
2054                 fr_strerror_printf("Regular expressions are not supported");
2055                 return NULL;
2056
2057 #else
2058
2059                 /*
2060                  *      Someone else will fill in the value.
2061                  */
2062                 if (!value) break;
2063
2064                 talloc_free(vp);
2065
2066                 compare = regcomp(&reg, value, REG_EXTENDED);
2067                 if (compare != 0) {
2068                         regerror(compare, &reg, buffer, sizeof(buffer));
2069                         fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
2070                                            attribute, buffer);
2071                         return NULL;
2072                 }
2073                 regfree(&reg);
2074
2075                 vp = pairmake(ctx, NULL, attribute, NULL, op);
2076                 if (!vp) return NULL;
2077
2078                 if (pairmark_xlat(vp, value) < 0) {
2079                         talloc_free(vp);
2080                         return NULL;
2081                 }
2082
2083                 value = NULL;   /* ignore it */
2084                 break;
2085 #endif
2086         }
2087         default:
2088                 break;
2089         }
2090
2091         /*
2092          *      FIXME: if (strcasecmp(attribute, vp->da->name) != 0)
2093          *      then the user MAY have typed in the attribute name
2094          *      as Vendor-%d-Attr-%d, and the value MAY be octets.
2095          *
2096          *      We probably want to fix pairparsevalue to accept
2097          *      octets as values for any attribute.
2098          */
2099         if (value && (pairparsevalue(vp, value, 0) < 0)) {
2100                 talloc_free(vp);
2101                 return NULL;
2102         }
2103
2104         if (vps) pairadd(vps, vp);
2105         return vp;
2106 }
2107
2108 /** Mark a valuepair for xlat expansion
2109  *
2110  * Copies xlat source (unprocessed) string to valuepair value,
2111  * and sets value type.
2112  *
2113  * @param vp to mark for expansion.
2114  * @param value to expand.
2115  * @return 0 if marking succeeded or -1 if vp already had a value, or OOM.
2116  */
2117 int pairmark_xlat(VALUE_PAIR *vp, char const *value)
2118 {
2119         char *raw;
2120
2121         /*
2122          *      valuepair should not already have a value.
2123          */
2124         if (vp->type != VT_NONE) {
2125                 return -1;
2126         }
2127
2128         raw = talloc_typed_strdup(vp, value);
2129         if (!raw) {
2130                 return -1;
2131         }
2132
2133         vp->type = VT_XLAT;
2134         vp->value.xlat = raw;
2135         vp->length = 0;
2136
2137         return 0;
2138 }
2139
2140 /** Read a single valuepair from a buffer, and advance the pointer
2141  *
2142  * Sets *eol to T_EOL if end of line was encountered.
2143  *
2144  * @param[in,out] ptr to read from and update.
2145  * @param[out] raw The struct to write the raw VALUE_PAIR to.
2146  * @return the last token read.
2147  */
2148 FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw)
2149 {
2150         char const      *p;
2151         char *q;
2152         FR_TOKEN        ret = T_OP_INVALID, next, quote;
2153         char            buf[8];
2154
2155         if (!ptr || !*ptr || !raw) {
2156                 fr_strerror_printf("Invalid arguments");
2157                 return T_OP_INVALID;
2158         }
2159
2160         /*
2161          *      Skip leading spaces
2162          */
2163         p = *ptr;
2164         while ((*p == ' ') || (*p == '\t')) p++;
2165
2166         if (!*p) {
2167                 fr_strerror_printf("No token read where we expected "
2168                                    "an attribute name");
2169                 return T_OP_INVALID;
2170         }
2171
2172         if (*p == '#') {
2173                 fr_strerror_printf("Read a comment instead of a token");
2174
2175                 return T_HASH;
2176         }
2177
2178         /*
2179          *      Try to get the attribute name.
2180          */
2181         q = raw->l_opand;
2182         *q = '\0';
2183         while (*p) {
2184                 uint8_t const *t = (uint8_t const *) p;
2185
2186                 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2187                 too_long:
2188                         fr_strerror_printf("Attribute name too long");
2189                         return T_OP_INVALID;
2190                 }
2191
2192                 /*
2193                  *      Only ASCII is allowed, and only a subset of that.
2194                  */
2195                 if ((*t < 32) || (*t >= 128)) {
2196                 invalid:
2197                         fr_strerror_printf("Invalid attribute name");
2198                         return T_OP_INVALID;
2199                 }
2200
2201                 /*
2202                  *      This is arguably easier than trying to figure
2203                  *      out which operators come after the attribute
2204                  *      name.  Yes, our "lexer" is bad.
2205                  */
2206                 if (!dict_attr_allowed_chars[(int) *t]) {
2207                         break;
2208                 }
2209
2210                 *(q++) = *(p++);
2211         }
2212
2213         /*
2214          *      ASCII, but not a valid attribute name.
2215          */
2216         if (!*raw->l_opand) goto invalid;
2217
2218         /*
2219          *      Look for tag (:#).  This is different from :=, which
2220          *      is an operator.
2221          */
2222         if ((*p == ':') && (isdigit((int) p[1]))) {
2223                 if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2224                         goto too_long;
2225                 }
2226                 *(q++) = *(p++);
2227
2228                 while (isdigit((int) *p)) {
2229                         if (q >= (raw->l_opand + sizeof(raw->l_opand))) {
2230                                 goto too_long;
2231                         }
2232                         *(q++) = *(p++);
2233                 }
2234         }
2235
2236         *q = '\0';
2237         *ptr = p;
2238
2239         /* Now we should have an operator here. */
2240         raw->op = gettoken(ptr, buf, sizeof(buf), false);
2241         if (raw->op  < T_EQSTART || raw->op  > T_EQEND) {
2242                 fr_strerror_printf("Expecting operator");
2243
2244                 return T_OP_INVALID;
2245         }
2246
2247         /*
2248          *      Read value.  Note that empty string values are allowed
2249          */
2250         quote = gettoken(ptr, raw->r_opand, sizeof(raw->r_opand), false);
2251         if (quote == T_EOL) {
2252                 fr_strerror_printf("Failed to get value");
2253
2254                 return T_OP_INVALID;
2255         }
2256
2257         /*
2258          *      Peek at the next token. Must be T_EOL, T_COMMA, or T_HASH
2259          */
2260         p = *ptr;
2261
2262         next = gettoken(&p, buf, sizeof(buf), false);
2263         switch (next) {
2264         case T_EOL:
2265         case T_HASH:
2266                 break;
2267
2268         case T_COMMA:
2269                 *ptr = p;
2270                 break;
2271
2272         default:
2273                 fr_strerror_printf("Expected end of line or comma");
2274                 return T_OP_INVALID;
2275         }
2276         ret = next;
2277
2278         switch (quote) {
2279         /*
2280          *      Perhaps do xlat's
2281          */
2282         case T_DOUBLE_QUOTED_STRING:
2283                 /*
2284                  *      Only report as double quoted if it contained valid
2285                  *      a valid xlat expansion.
2286                  */
2287                 p = strchr(raw->r_opand, '%');
2288                 if (p && (p[1] == '{')) {
2289                         raw->quote = quote;
2290                 } else {
2291                         raw->quote = T_SINGLE_QUOTED_STRING;
2292                 }
2293
2294                 break;
2295         default:
2296                 raw->quote = quote;
2297
2298                 break;
2299         }
2300
2301         return ret;
2302 }
2303
2304 /** Read one line of attribute/value pairs into a list.
2305  *
2306  * The line may specify multiple attributes separated by commas.
2307  *
2308  * @note If the function returns T_OP_INVALID, an error has occurred and
2309  * @note the valuepair list should probably be freed.
2310  *
2311  * @param ctx for talloc
2312  * @param buffer to read valuepairs from.
2313  * @param list where the parsed VALUE_PAIRs will be appended.
2314  * @return the last token parsed, or T_OP_INVALID
2315  */
2316 FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **list)
2317 {
2318         VALUE_PAIR      *vp, *head, **tail;
2319         char const      *p;
2320         FR_TOKEN        last_token = T_OP_INVALID;
2321         FR_TOKEN        previous_token;
2322         VALUE_PAIR_RAW  raw;
2323
2324         /*
2325          *      We allow an empty line.
2326          */
2327         if (buffer[0] == 0) {
2328                 return T_EOL;
2329         }
2330
2331         head = NULL;
2332         tail = &head;
2333
2334         p = buffer;
2335         do {
2336                 raw.l_opand[0] = '\0';
2337                 raw.r_opand[0] = '\0';
2338
2339                 previous_token = last_token;
2340
2341                 last_token = pairread(&p, &raw);
2342                 if (last_token == T_OP_INVALID) break;
2343
2344                 if (raw.quote == T_DOUBLE_QUOTED_STRING) {
2345                         vp = pairmake(ctx, NULL, raw.l_opand, NULL, raw.op);
2346                         if (!vp) {
2347                                 last_token = T_OP_INVALID;
2348                                 break;
2349                         }
2350                         if (pairmark_xlat(vp, raw.r_opand) < 0) {
2351                                 talloc_free(vp);
2352                                 last_token = T_OP_INVALID;
2353                                 break;
2354                         }
2355                 } else {
2356                         vp = pairmake(ctx, NULL, raw.l_opand, raw.r_opand, raw.op);
2357                         if (!vp) {
2358                                 last_token = T_OP_INVALID;
2359                                 break;
2360                         }
2361                 }
2362
2363                 *tail = vp;
2364                 tail = &((*tail)->next);
2365         } while (*p && (last_token == T_COMMA));
2366
2367         /*
2368          *      Don't tell the caller that there was a comment.
2369          */
2370         if (last_token == T_HASH) {
2371                 last_token = previous_token;
2372         }
2373
2374         if (last_token == T_OP_INVALID) {
2375                 pairfree(&head);
2376         } else {
2377                 pairadd(list, head);
2378         }
2379
2380         /*
2381          *      And return the last token which we read.
2382          */
2383         return last_token;
2384 }
2385
2386 /*
2387  *      Read valuepairs from the fp up to End-Of-File.
2388  */
2389 int readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone)
2390 {
2391         char buf[8192];
2392         FR_TOKEN last_token = T_EOL;
2393
2394         vp_cursor_t cursor;
2395
2396         VALUE_PAIR *vp = NULL;
2397
2398         fr_cursor_init(&cursor, out);
2399
2400         while (fgets(buf, sizeof(buf), fp) != NULL) {
2401                 /*
2402                  *      If we get a '\n' by itself, we assume that's
2403                  *      the end of that VP
2404                  */
2405                 if (buf[0] == '\n') {
2406                         if (vp) return 0;
2407                         continue;
2408                 }
2409
2410                 /*
2411                  *      Comments get ignored
2412                  */
2413                 if (buf[0] == '#') continue;
2414
2415                 /*
2416                  *      Read all of the attributes on the current line.
2417                  */
2418                 vp = NULL;
2419                 last_token = userparse(ctx, buf, &vp);
2420                 if (!vp) {
2421                         if (last_token != T_EOL) goto error;
2422                         break;
2423                 }
2424
2425                 fr_cursor_insert(&cursor, vp);
2426                 buf[0] = '\0';
2427         }
2428
2429         *pfiledone = true;
2430
2431         return 0;
2432
2433 error:
2434         vp = fr_cursor_first(&cursor);
2435         if (vp) pairfree(&vp);
2436
2437         return -1;
2438 }
2439
2440 /** Compare two attribute values
2441  *
2442  * @param[in] one the first attribute.
2443  * @param[in] two the second attribute.
2444  * @return -1 if one is less than two, 0 if both are equal, 1 if one is more than two, < -1 on error.
2445  */
2446 int8_t paircmp_value(VALUE_PAIR const *one, VALUE_PAIR const *two)
2447 {
2448         int64_t compare = 0;
2449
2450         VERIFY_VP(one);
2451         VERIFY_VP(two);
2452
2453         if (one->da->type != two->da->type) {
2454                 fr_strerror_printf("Can't compare attribute values of different types");
2455                 return -2;
2456         }
2457
2458         /*
2459          *      After doing the previous check for special comparisons,
2460          *      do the per-type comparison here.
2461          */
2462         switch (one->da->type) {
2463         case PW_TYPE_ABINARY:
2464         case PW_TYPE_OCTETS:
2465         {
2466                 size_t length;
2467
2468                 if (one->length > two->length) {
2469                         length = one->length;
2470                 } else {
2471                         length = two->length;
2472                 }
2473
2474                 if (length) {
2475                         compare = memcmp(one->vp_octets, two->vp_octets, length);
2476                         if (compare != 0) break;
2477                 }
2478
2479                 /*
2480                  *      Contents are the same.  The return code
2481                  *      is therefore the difference in lengths.
2482                  *
2483                  *      i.e. "0x00" is smaller than "0x0000"
2484                  */
2485                 compare = one->length - two->length;
2486         }
2487                 break;
2488
2489         case PW_TYPE_STRING:
2490                 fr_assert(one->vp_strvalue);
2491                 fr_assert(two->vp_strvalue);
2492                 compare = strcmp(one->vp_strvalue, two->vp_strvalue);
2493                 break;
2494
2495         case PW_TYPE_BOOLEAN:
2496         case PW_TYPE_BYTE:
2497         case PW_TYPE_SHORT:
2498         case PW_TYPE_INTEGER:
2499         case PW_TYPE_DATE:
2500                 compare = (int64_t) one->vp_integer - (int64_t) two->vp_integer;
2501                 break;
2502
2503         case PW_TYPE_SIGNED:
2504                 compare = one->vp_signed - two->vp_signed;
2505                 break;
2506
2507         case PW_TYPE_INTEGER64:
2508                 /*
2509                  *      Don't want integer overflow!
2510                  */
2511                 if (one->vp_integer64 < two->vp_integer64) {
2512                         compare = -1;
2513                 } else if (one->vp_integer64 > two->vp_integer64) {
2514                         compare = 1;
2515                 }
2516                 break;
2517
2518         case PW_TYPE_ETHERNET:
2519                 compare = memcmp(&one->vp_ether, &two->vp_ether, sizeof(one->vp_ether));
2520                 break;
2521
2522         case PW_TYPE_IPADDR:
2523                 compare = (int64_t) ntohl(one->vp_ipaddr) - (int64_t) ntohl(two->vp_ipaddr);
2524                 break;
2525
2526         case PW_TYPE_IPV6ADDR:
2527                 compare = memcmp(&one->vp_ipv6addr, &two->vp_ipv6addr, sizeof(one->vp_ipv6addr));
2528                 break;
2529
2530         case PW_TYPE_IPV6PREFIX:
2531                 compare = memcmp(&one->vp_ipv6prefix, &two->vp_ipv6prefix, sizeof(one->vp_ipv6prefix));
2532                 break;
2533
2534         case PW_TYPE_IPV4PREFIX:
2535                 compare = memcmp(&one->vp_ipv4prefix, &two->vp_ipv4prefix, sizeof(one->vp_ipv4prefix));
2536                 break;
2537
2538         case PW_TYPE_IFID:
2539                 compare = memcmp(&one->vp_ifid, &two->vp_ifid, sizeof(one->vp_ifid));
2540                 break;
2541
2542         /*
2543          *      None of the types below should be in the REQUEST
2544          */
2545         case PW_TYPE_INVALID:           /* We should never see these */
2546         case PW_TYPE_COMBO_IP:          /* This should of been converted into IPADDR/IPV6ADDR */
2547         case PW_TYPE_TLV:
2548         case PW_TYPE_EXTENDED:
2549         case PW_TYPE_LONG_EXTENDED:
2550         case PW_TYPE_EVS:
2551         case PW_TYPE_VSA:
2552         case PW_TYPE_TIMEVAL:
2553         case PW_TYPE_MAX:
2554                 fr_assert(0);   /* unknown type */
2555                 return -2;
2556
2557         /*
2558          *      Do NOT add a default here, as new types are added
2559          *      static analysis will warn us they're not handled
2560          */
2561         }
2562
2563         if (compare > 0) {
2564                 return 1;
2565         } else if (compare < 0) {
2566                 return -1;
2567         }
2568         return 0;
2569 }
2570
2571 /*
2572  *      We leverage the fact that IPv4 and IPv6 prefixes both
2573  *      have the same format:
2574  *
2575  *      reserved, prefix-len, data...
2576  */
2577 static int paircmp_op_cidr(FR_TOKEN op, int bytes,
2578                            uint8_t one_net, uint8_t const *one,
2579                            uint8_t two_net, uint8_t const *two)
2580 {
2581         int i, common;
2582         uint32_t mask;
2583
2584         /*
2585          *      Handle the case of netmasks being identical.
2586          */
2587         if (one_net == two_net) {
2588                 int compare;
2589
2590                 compare = memcmp(one, two, bytes);
2591
2592                 /*
2593                  *      If they're identical return true for
2594                  *      identical.
2595                  */
2596                 if ((compare == 0) &&
2597                     ((op == T_OP_CMP_EQ) ||
2598                      (op == T_OP_LE) ||
2599                      (op == T_OP_GE))) {
2600                         return true;
2601                 }
2602
2603                 /*
2604                  *      Everything else returns false.
2605                  *
2606                  *      10/8 == 24/8  --> false
2607                  *      10/8 <= 24/8  --> false
2608                  *      10/8 >= 24/8  --> false
2609                  */
2610                 return false;
2611         }
2612
2613         /*
2614          *      Netmasks are different.  That limits the
2615          *      possible results, based on the operator.
2616          */
2617         switch (op) {
2618         case T_OP_CMP_EQ:
2619                 return false;
2620
2621         case T_OP_NE:
2622                 return true;
2623
2624         case T_OP_LE:
2625         case T_OP_LT:   /* 192/8 < 192.168/16 --> false */
2626                 if (one_net < two_net) {
2627                         return false;
2628                 }
2629                 break;
2630
2631         case T_OP_GE:
2632         case T_OP_GT:   /* 192/16 > 192.168/8 --> false */
2633                 if (one_net > two_net) {
2634                         return false;
2635                 }
2636                 break;
2637
2638         default:
2639                 return false;
2640         }
2641
2642         if (one_net < two_net) {
2643                 common = one_net;
2644         } else {
2645                 common = two_net;
2646         }
2647
2648         /*
2649          *      Do the check byte by byte.  If the bytes are
2650          *      identical, it MAY be a match.  If they're different,
2651          *      it is NOT a match.
2652          */
2653         i = 0;
2654         while (i < bytes) {
2655                 /*
2656                  *      All leading bytes are identical.
2657                  */
2658                 if (common == 0) return true;
2659
2660                 /*
2661                  *      Doing bitmasks takes more work.
2662                  */
2663                 if (common < 8) break;
2664
2665                 if (one[i] != two[i]) return false;
2666
2667                 common -= 8;
2668                 i++;
2669                 continue;
2670         }
2671
2672         mask = 1;
2673         mask <<= (8 - common);
2674         mask--;
2675         mask = ~mask;
2676
2677         if ((one[i] & mask) == ((two[i] & mask))) {
2678                 return true;
2679         }
2680
2681         return false;
2682 }
2683
2684 /** Compare two attributes using an operator
2685  *
2686  * @param[in] a the first attribute
2687  * @param[in] op the operator for comparison.
2688  * @param[in] b the second attribute
2689  * @return 1 if true, 0 if false, -1 on error.
2690  */
2691 int8_t paircmp_op(VALUE_PAIR const *a, FR_TOKEN op, VALUE_PAIR const *b)
2692 {
2693         int compare;
2694
2695         if (!a || !b) return -1;
2696
2697         switch (a->da->type) {
2698         case PW_TYPE_IPADDR:
2699                 switch (b->da->type) {
2700                 case PW_TYPE_IPADDR:            /* IPv4 and IPv4 */
2701                         goto cmp;
2702
2703                 case PW_TYPE_IPV4PREFIX:        /* IPv4 and IPv4 Prefix */
2704                         return paircmp_op_cidr(op, 4, 32, (uint8_t const *) &a->vp_ipaddr,
2705                                                b->vp_ipv4prefix[1], (uint8_t const *) &b->vp_ipv4prefix + 2);
2706
2707                 default:
2708                         fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
2709                         return -1;
2710                 }
2711                 break;
2712
2713         case PW_TYPE_IPV4PREFIX:                /* IPv4 and IPv4 Prefix */
2714                 switch (b->da->type) {
2715                 case PW_TYPE_IPADDR:
2716                         return paircmp_op_cidr(op, 4, a->vp_ipv4prefix[1],
2717                                                (uint8_t const *) &a->vp_ipv4prefix + 2,
2718                                                32, (uint8_t const *) &b->vp_ipaddr);
2719
2720                 case PW_TYPE_IPV4PREFIX:        /* IPv4 Prefix and IPv4 Prefix */
2721                         return paircmp_op_cidr(op, 4, a->vp_ipv4prefix[1],
2722                                                (uint8_t const *) &a->vp_ipv4prefix + 2,
2723                                                b->vp_ipv4prefix[1], (uint8_t const *) &b->vp_ipv4prefix + 2);
2724
2725                 default:
2726                         fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
2727                         return -1;
2728                 }
2729                 break;
2730
2731         case PW_TYPE_IPV6ADDR:
2732                 switch (b->da->type) {
2733                 case PW_TYPE_IPV6ADDR:          /* IPv6 and IPv6 */
2734                         goto cmp;
2735
2736                 case PW_TYPE_IPV6PREFIX:        /* IPv6 and IPv6 Preifx */
2737                         return paircmp_op_cidr(op, 16, 128, (uint8_t const *) &a->vp_ipv6addr,
2738                                                b->vp_ipv6prefix[1], (uint8_t const *) &b->vp_ipv6prefix + 2);
2739                         break;
2740
2741                 default:
2742                         fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
2743                         return -1;
2744                 }
2745                 break;
2746
2747         case PW_TYPE_IPV6PREFIX:
2748                 switch (b->da->type) {
2749                 case PW_TYPE_IPV6ADDR:          /* IPv6 Prefix and IPv6 */
2750                         return paircmp_op_cidr(op, 16, a->vp_ipv6prefix[1],
2751                                                (uint8_t const *) &a->vp_ipv6prefix + 2,
2752                                                128, (uint8_t const *) &b->vp_ipv6addr);
2753
2754                 case PW_TYPE_IPV6PREFIX:        /* IPv6 Prefix and IPv6 */
2755                         return paircmp_op_cidr(op, 16, a->vp_ipv6prefix[1],
2756                                                (uint8_t const *) &a->vp_ipv6prefix + 2,
2757                                                b->vp_ipv6prefix[1], (uint8_t const *) &b->vp_ipv6prefix + 2);
2758
2759                 default:
2760                         fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
2761                         return -1;
2762                 }
2763                 break;
2764
2765         default:
2766         cmp:
2767                 compare = paircmp_value(a, b);
2768                 if (compare < -1) {     /* comparison error */
2769                         return -1;
2770                 }
2771         }
2772
2773         /*
2774          *      Now do the operator comparison.
2775          */
2776         switch (op) {
2777         case T_OP_CMP_EQ:
2778                 return (compare == 0);
2779
2780         case T_OP_NE:
2781                 return (compare != 0);
2782
2783         case T_OP_LT:
2784                 return (compare < 0);
2785
2786         case T_OP_GT:
2787                 return (compare > 0);
2788
2789         case T_OP_LE:
2790                 return (compare <= 0);
2791
2792         case T_OP_GE:
2793                 return (compare >= 0);
2794
2795         default:
2796                 return 0;
2797         }
2798 }
2799
2800 /** Compare two pairs, using the operator from "a"
2801  *
2802  *      i.e. given two attributes, it does:
2803  *
2804  *      (b->data) (a->operator) (a->data)
2805  *
2806  *      e.g. "foo" != "bar"
2807  *
2808  * @param[in] a the first attribute
2809  * @param[in] b the second attribute
2810  * @return 1 if true, 0 if false, -1 on error.
2811  */
2812 int8_t paircmp(VALUE_PAIR *a, VALUE_PAIR *b)
2813 {
2814         if (!a) return -1;
2815
2816         VERIFY_VP(a);
2817         if (b) VERIFY_VP(b);
2818
2819         switch (a->op) {
2820         case T_OP_CMP_TRUE:
2821                 return (b != NULL);
2822
2823         case T_OP_CMP_FALSE:
2824                 return (b == NULL);
2825
2826                 /*
2827                  *      a is a regex, compile it, print b to a string,
2828                  *      and then do string comparisons.
2829                  */
2830         case T_OP_REG_EQ:
2831         case T_OP_REG_NE:
2832 #ifndef WITH_REGEX
2833                 return -1;
2834 #else
2835                 {
2836                         int compare;
2837                         regex_t reg;
2838                         char buffer[MAX_STRING_LEN * 4 + 1];
2839
2840                         compare = regcomp(&reg, a->vp_strvalue, REG_EXTENDED);
2841                         if (compare != 0) {
2842                                 regerror(compare, &reg, buffer, sizeof(buffer));
2843                                 fr_strerror_printf("Illegal regular expression in attribute: %s: %s",
2844                                                    a->da->name, buffer);
2845                                 return -1;
2846                         }
2847
2848                         if (!b) {
2849                                 regfree(&reg);
2850                                 return -1;
2851                         }
2852
2853                         vp_prints_value(buffer, sizeof(buffer), b, 0);
2854
2855                         /*
2856                          *      Don't care about substring matches,
2857                          *      oh well...
2858                          */
2859                         compare = regexec(&reg, buffer, 0, NULL, 0);
2860
2861                         regfree(&reg);
2862                         if (a->op == T_OP_REG_EQ) {
2863                                 return (compare == 0);
2864                         }
2865
2866                         return (compare != 0);
2867                 }
2868 #endif
2869
2870         default:                /* we're OK */
2871                 break;
2872         }
2873
2874         return paircmp_op(b, a->op, a);
2875 }
2876
2877 /** Determine equality of two lists
2878  *
2879  * This is useful for comparing lists of attributes inserted into a binary tree.
2880  *
2881  * @param a first list of VALUE_PAIRs.
2882  * @param b second list of VALUE_PAIRs.
2883  * @return -1 if a < b, 0 if the two lists are equal, 1 if a > b, -2 on error.
2884  */
2885 int8_t pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b)
2886 {
2887         vp_cursor_t a_cursor, b_cursor;
2888         VALUE_PAIR *a_p, *b_p;
2889         int ret;
2890
2891         for (a_p = fr_cursor_init(&a_cursor, &a), b_p = fr_cursor_init(&b_cursor, &b);
2892              a_p && b_p;
2893              a_p = fr_cursor_next(&a_cursor), b_p = fr_cursor_next(&b_cursor)) {
2894                 /* Same VP, no point doing expensive checks */
2895                 if (a_p == b_p) {
2896                         continue;
2897                 }
2898
2899                 if (a_p->da < b_p->da) {
2900                         return -1;
2901                 }
2902                 if (a_p->da > b_p->da) {
2903                         return 1;
2904                 }
2905
2906                 if (a_p->tag < b_p->tag) {
2907                         return -1;
2908                 }
2909                 if (a_p->tag > b_p->tag) {
2910                         return 1;
2911                 }
2912
2913                 ret = paircmp_value(a_p, b_p);
2914                 if (ret != 0) {
2915                         fr_assert(ret >= -1);   /* Comparison error */
2916                         return ret;
2917                 }
2918         }
2919
2920         if (!a_p && !b_p) {
2921                 return 0;
2922         }
2923
2924         if (!a_p) {
2925                 return -1;
2926         }
2927
2928         /* if(!b_p) */
2929         return 1;
2930 }
2931
2932 /** Set the type of the VALUE_PAIR value buffer to match it's DICT_ATTR
2933  *
2934  * @param vp to fixup.
2935  */
2936 static void pairtypeset(VALUE_PAIR *vp)
2937 {
2938         if (!vp->data.ptr) return;
2939
2940         switch(vp->da->type) {
2941         case PW_TYPE_OCTETS:
2942         case PW_TYPE_TLV:
2943                 talloc_set_type(vp->data.ptr, uint8_t);
2944                 return;
2945
2946         case PW_TYPE_STRING:
2947                 talloc_set_type(vp->data.ptr, char);
2948                 return;
2949
2950         default:
2951                 return;
2952         }
2953 }
2954
2955 /** Copy data into an "octets" data type.
2956  *
2957  * @param[in,out] vp to update
2958  * @param[in] src data to copy
2959  * @param[in] size of the data, may be 0 in which case previous value will be freed.
2960  */
2961 void pairmemcpy(VALUE_PAIR *vp, uint8_t const *src, size_t size)
2962 {
2963         uint8_t *p = NULL, *q;
2964
2965         VERIFY_VP(vp);
2966
2967         if (size > 0) {
2968                 p = talloc_memdup(vp, src, size);
2969                 if (!p) return;
2970                 talloc_set_type(p, uint8_t);
2971         }
2972
2973         memcpy(&q, &vp->vp_octets, sizeof(q));
2974         TALLOC_FREE(q);
2975
2976         vp->vp_octets = p;
2977         vp->length = size;
2978
2979         if (size > 0) pairtypeset(vp);
2980 }
2981
2982 /** Reparent an allocated octet buffer to a VALUE_PAIR
2983  *
2984  * @param[in,out] vp to update
2985  * @param[in] src buffer to steal.
2986  */
2987 void pairmemsteal(VALUE_PAIR *vp, uint8_t const *src)
2988 {
2989         uint8_t *q;
2990
2991         VERIFY_VP(vp);
2992
2993         memcpy(&q, &vp->vp_octets, sizeof(q));
2994         talloc_free(q);
2995
2996         vp->vp_octets = talloc_steal(vp, src);
2997         vp->type = VT_DATA;
2998         vp->length = talloc_array_length(vp->vp_strvalue);
2999         pairtypeset(vp);
3000 }
3001
3002 /** Reparent an allocated char buffer to a VALUE_PAIR
3003  *
3004  * @param[in,out] vp to update
3005  * @param[in] src buffer to steal.
3006  */
3007 void pairstrsteal(VALUE_PAIR *vp, char const *src)
3008 {
3009         uint8_t *q;
3010
3011         VERIFY_VP(vp);
3012
3013         memcpy(&q, &vp->vp_octets, sizeof(q));
3014         talloc_free(q);
3015
3016         vp->vp_strvalue = talloc_steal(vp, src);
3017         vp->type = VT_DATA;
3018         vp->length = talloc_array_length(vp->vp_strvalue) - 1;
3019         pairtypeset(vp);
3020 }
3021
3022 /** Copy data into an "string" data type.
3023  *
3024  * @param[in,out] vp to update
3025  * @param[in] src data to copy
3026  */
3027 void pairstrcpy(VALUE_PAIR *vp, char const *src)
3028 {
3029         char *p, *q;
3030
3031         VERIFY_VP(vp);
3032
3033         p = talloc_strdup(vp, src);
3034
3035         if (!p) return;
3036
3037         memcpy(&q, &vp->vp_strvalue, sizeof(q));
3038         talloc_free(q);
3039
3040         vp->vp_strvalue = p;
3041         vp->type = VT_DATA;
3042         vp->length = talloc_array_length(vp->vp_strvalue) - 1;
3043         pairtypeset(vp);
3044 }
3045
3046 /** Copy data into an "string" data type.
3047  *
3048  * @param[in,out] vp to update.
3049  * @param[in] src data to copy.
3050  * @param[in] len of data to copy.
3051  */
3052 void pairstrncpy(VALUE_PAIR *vp, char const *src, size_t len)
3053 {
3054         char *p, *q;
3055
3056         VERIFY_VP(vp);
3057
3058         p = talloc_array(vp, char, len + 1);
3059         if (!p) return;
3060
3061         memcpy(p, src, len);    /* embdedded \0 safe */
3062         p[len] = '\0';
3063
3064         memcpy(&q, &vp->vp_strvalue, sizeof(q));
3065         talloc_free(q);
3066
3067         vp->vp_strvalue = p;
3068         vp->type = VT_DATA;
3069         vp->length = len;
3070         pairtypeset(vp);
3071 }
3072
3073 /** Print data into an "string" data type.
3074  *
3075  * @param[in,out] vp to update
3076  * @param[in] fmt the format string
3077  */
3078 void pairsprintf(VALUE_PAIR *vp, char const *fmt, ...)
3079 {
3080         va_list ap;
3081         char *p, *q;
3082
3083         VERIFY_VP(vp);
3084
3085         va_start(ap, fmt);
3086         p = talloc_vasprintf(vp, fmt, ap);
3087         va_end(ap);
3088
3089         if (!p) return;
3090
3091         memcpy(&q, &vp->vp_strvalue, sizeof(q));
3092         talloc_free(q);
3093
3094         vp->vp_strvalue = p;
3095         vp->type = VT_DATA;
3096
3097         vp->length = talloc_array_length(vp->vp_strvalue) - 1;
3098         pairtypeset(vp);
3099 }
3100