Parse hex Ascend-Data-Filter correctly
[freeradius.git] / src / lib / value.c
1 /*
2  * value.c      Functions to handle value_data_t
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 2014 The FreeRADIUS server project
21  */
22
23 RCSID("$Id$")
24
25 #include <freeradius-devel/libradius.h>
26 #include <ctype.h>
27
28 /** Compare two values
29  *
30  * @param[in] a_type of data to compare.
31  * @param[in] a_len of data to compare.
32  * @param[in] a Value to compare.
33  * @param[in] b_type of data to compare.
34  * @param[in] b_len of data to compare.
35  * @param[in] b Value to compare.
36  * @return -1 if a is less than b, 0 if both are equal, 1 if a is more than b, < -1 on error.
37  */
38 int value_data_cmp(PW_TYPE a_type, value_data_t const *a, size_t a_len,
39                    PW_TYPE b_type, value_data_t const *b, size_t b_len)
40 {
41         int compare = 0;
42
43         if (a_type != b_type) {
44                 fr_strerror_printf("Can't compare values of different types");
45                 return -2;
46         }
47
48         /*
49          *      After doing the previous check for special comparisons,
50          *      do the per-type comparison here.
51          */
52         switch (a_type) {
53         case PW_TYPE_ABINARY:
54         case PW_TYPE_OCTETS:
55         case PW_TYPE_STRING:    /* We use memcmp to be \0 safe */
56         {
57                 size_t length;
58
59                 if (a_len > b_len) {
60                         length = a_len;
61                 } else {
62                         length = b_len;
63                 }
64
65                 if (length) {
66                         compare = memcmp(a->octets, b->octets, length);
67                         if (compare != 0) break;
68                 }
69
70                 /*
71                  *      Contents are the same.  The return code
72                  *      is therefore the difference in lengths.
73                  *
74                  *      i.e. "0x00" is smaller than "0x0000"
75                  */
76                 compare = a_len - b_len;
77         }
78                 break;
79
80                 /*
81                  *      Short-hand for simplicity.
82                  */
83 #define CHECK(_type) if (a->_type < b->_type)   { compare = -1; \
84                 } else if (a->_type > b->_type) { compare = +1; }
85
86         case PW_TYPE_BOOLEAN:   /* this isn't a RADIUS type, and shouldn't really ever be used */
87         case PW_TYPE_BYTE:
88                 CHECK(byte);
89                 break;
90
91
92         case PW_TYPE_SHORT:
93                 CHECK(ushort);
94                 break;
95
96         case PW_TYPE_DATE:
97                 CHECK(date);
98                 break;
99
100         case PW_TYPE_INTEGER:
101                 CHECK(integer);
102                 break;
103
104         case PW_TYPE_SIGNED:
105                 CHECK(sinteger);
106                 break;
107
108         case PW_TYPE_INTEGER64:
109                 CHECK(integer64);
110                 break;
111
112         case PW_TYPE_ETHERNET:
113                 compare = memcmp(a->ether, b->ether, sizeof(a->ether));
114                 break;
115
116         case PW_TYPE_IPV4_ADDR: {
117                         uint32_t a_int, b_int;
118
119                         a_int = ntohl(a->ipaddr.s_addr);
120                         b_int = ntohl(b->ipaddr.s_addr);
121                         if (a_int < b_int) {
122                                 compare = -1;
123                         } else if (a_int > b_int) {
124                                 compare = +1;
125                         }
126                 }
127                 break;
128
129         case PW_TYPE_IPV6_ADDR:
130                 compare = memcmp(&a->ipv6addr, &b->ipv6addr, sizeof(a->ipv6addr));
131                 break;
132
133         case PW_TYPE_IPV6_PREFIX:
134                 compare = memcmp(a->ipv6prefix, b->ipv6prefix, sizeof(a->ipv6prefix));
135                 break;
136
137         case PW_TYPE_IPV4_PREFIX:
138                 compare = memcmp(a->ipv4prefix, b->ipv4prefix, sizeof(a->ipv4prefix));
139                 break;
140
141         case PW_TYPE_IFID:
142                 compare = memcmp(a->ifid, b->ifid, sizeof(a->ifid));
143                 break;
144
145         /*
146          *      Na of the types below should be in the REQUEST
147          */
148         case PW_TYPE_INVALID:           /* We should never see these */
149         case PW_TYPE_COMBO_IP_ADDR:             /* This should have been converted into IPADDR/IPV6ADDR */
150         case PW_TYPE_COMBO_IP_PREFIX:           /* This should have been converted into IPADDR/IPV6ADDR */
151         case PW_TYPE_TLV:
152         case PW_TYPE_EXTENDED:
153         case PW_TYPE_LONG_EXTENDED:
154         case PW_TYPE_EVS:
155         case PW_TYPE_VSA:
156         case PW_TYPE_TIMEVAL:
157         case PW_TYPE_MAX:
158                 fr_assert(0);   /* unknown type */
159                 return -2;
160
161         /*
162          *      Do NOT add a default here, as new types are added
163          *      static analysis will warn us they're not handled
164          */
165         }
166
167         if (compare > 0) {
168                 return 1;
169         } else if (compare < 0) {
170                 return -1;
171         }
172         return 0;
173 }
174
175 /*
176  *      We leverage the fact that IPv4 and IPv6 prefixes both
177  *      have the same format:
178  *
179  *      reserved, prefix-len, data...
180  */
181 static int value_data_cidr_cmp_op(FR_TOKEN op, int bytes,
182                                   uint8_t a_net, uint8_t const *a,
183                                   uint8_t b_net, uint8_t const *b)
184 {
185         int i, common;
186         uint32_t mask;
187
188         /*
189          *      Handle the case of netmasks being identical.
190          */
191         if (a_net == b_net) {
192                 int compare;
193
194                 compare = memcmp(a, b, bytes);
195
196                 /*
197                  *      If they're identical return true for
198                  *      identical.
199                  */
200                 if ((compare == 0) &&
201                     ((op == T_OP_CMP_EQ) ||
202                      (op == T_OP_LE) ||
203                      (op == T_OP_GE))) {
204                         return true;
205                 }
206
207                 /*
208                  *      Everything else returns false.
209                  *
210                  *      10/8 == 24/8  --> false
211                  *      10/8 <= 24/8  --> false
212                  *      10/8 >= 24/8  --> false
213                  */
214                 return false;
215         }
216
217         /*
218          *      Netmasks are different.  That limits the
219          *      possible results, based on the operator.
220          */
221         switch (op) {
222         case T_OP_CMP_EQ:
223                 return false;
224
225         case T_OP_NE:
226                 return true;
227
228         case T_OP_LE:
229         case T_OP_LT:   /* 192/8 < 192.168/16 --> false */
230                 if (a_net < b_net) {
231                         return false;
232                 }
233                 break;
234
235         case T_OP_GE:
236         case T_OP_GT:   /* 192/16 > 192.168/8 --> false */
237                 if (a_net > b_net) {
238                         return false;
239                 }
240                 break;
241
242         default:
243                 return false;
244         }
245
246         if (a_net < b_net) {
247                 common = a_net;
248         } else {
249                 common = b_net;
250         }
251
252         /*
253          *      Do the check byte by byte.  If the bytes are
254          *      identical, it MAY be a match.  If they're different,
255          *      it is NOT a match.
256          */
257         i = 0;
258         while (i < bytes) {
259                 /*
260                  *      All leading bytes are identical.
261                  */
262                 if (common == 0) return true;
263
264                 /*
265                  *      Doing bitmasks takes more work.
266                  */
267                 if (common < 8) break;
268
269                 if (a[i] != b[i]) return false;
270
271                 common -= 8;
272                 i++;
273                 continue;
274         }
275
276         mask = 1;
277         mask <<= (8 - common);
278         mask--;
279         mask = ~mask;
280
281         if ((a[i] & mask) == ((b[i] & mask))) {
282                 return true;
283         }
284
285         return false;
286 }
287
288 /** Compare two attributes using an operator
289  *
290  * @param[in] op to use in comparison.
291  * @param[in] a_type of data to compare.
292  * @param[in] a_len of data to compare.
293  * @param[in] a Value to compare.
294  * @param[in] b_type of data to compare.
295  * @param[in] b_len of data to compare.
296  * @param[in] b Value to compare.
297  * @return 1 if true, 0 if false, -1 on error.
298  */
299 int value_data_cmp_op(FR_TOKEN op,
300                       PW_TYPE a_type, value_data_t const *a, size_t a_len,
301                       PW_TYPE b_type, value_data_t const *b, size_t b_len)
302 {
303         int compare = 0;
304
305         if (!a || !b) return -1;
306
307         switch (a_type) {
308         case PW_TYPE_IPV4_ADDR:
309                 switch (b_type) {
310                 case PW_TYPE_IPV4_ADDR:         /* IPv4 and IPv4 */
311                         goto cmp;
312
313                 case PW_TYPE_IPV4_PREFIX:       /* IPv4 and IPv4 Prefix */
314                         return value_data_cidr_cmp_op(op, 4, 32, (uint8_t const *) &a->ipaddr,
315                                                       b->ipv4prefix[1], (uint8_t const *) &b->ipv4prefix[2]);
316
317                 default:
318                         fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
319                         return -1;
320                 }
321
322         case PW_TYPE_IPV4_PREFIX:               /* IPv4 and IPv4 Prefix */
323                 switch (b_type) {
324                 case PW_TYPE_IPV4_ADDR:
325                         return value_data_cidr_cmp_op(op, 4, a->ipv4prefix[1],
326                                                     (uint8_t const *) &a->ipv4prefix[2],
327                                                     32, (uint8_t const *) &b->ipaddr);
328
329                 case PW_TYPE_IPV4_PREFIX:       /* IPv4 Prefix and IPv4 Prefix */
330                         return value_data_cidr_cmp_op(op, 4, a->ipv4prefix[1],
331                                                     (uint8_t const *) &a->ipv4prefix[2],
332                                                     b->ipv4prefix[1], (uint8_t const *) &b->ipv4prefix[2]);
333
334                 default:
335                         fr_strerror_printf("Cannot compare IPv4 with IPv6 address");
336                         return -1;
337                 }
338
339         case PW_TYPE_IPV6_ADDR:
340                 switch (b_type) {
341                 case PW_TYPE_IPV6_ADDR:         /* IPv6 and IPv6 */
342                         goto cmp;
343
344                 case PW_TYPE_IPV6_PREFIX:       /* IPv6 and IPv6 Preifx */
345                         return value_data_cidr_cmp_op(op, 16, 128, (uint8_t const *) &a->ipv6addr,
346                                                       b->ipv6prefix[1], (uint8_t const *) &b->ipv6prefix[2]);
347
348                 default:
349                         fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
350                         return -1;
351                 }
352
353         case PW_TYPE_IPV6_PREFIX:
354                 switch (b_type) {
355                 case PW_TYPE_IPV6_ADDR:         /* IPv6 Prefix and IPv6 */
356                         return value_data_cidr_cmp_op(op, 16, a->ipv6prefix[1],
357                                                       (uint8_t const *) &a->ipv6prefix[2],
358                                                       128, (uint8_t const *) &b->ipv6addr);
359
360                 case PW_TYPE_IPV6_PREFIX:       /* IPv6 Prefix and IPv6 */
361                         return value_data_cidr_cmp_op(op, 16, a->ipv6prefix[1],
362                                                       (uint8_t const *) &a->ipv6prefix[2],
363                                                       b->ipv6prefix[1], (uint8_t const *) &b->ipv6prefix[2]);
364
365                 default:
366                         fr_strerror_printf("Cannot compare IPv6 with IPv4 address");
367                         return -1;
368                 }
369
370         default:
371         cmp:
372                 compare = value_data_cmp(a_type, a, a_len,
373                                          b_type, b, b_len);
374                 if (compare < -1) {     /* comparison error */
375                         return -1;
376                 }
377         }
378
379         /*
380          *      Now do the operator comparison.
381          */
382         switch (op) {
383         case T_OP_CMP_EQ:
384                 return (compare == 0);
385
386         case T_OP_NE:
387                 return (compare != 0);
388
389         case T_OP_LT:
390                 return (compare < 0);
391
392         case T_OP_GT:
393                 return (compare > 0);
394
395         case T_OP_LE:
396                 return (compare <= 0);
397
398         case T_OP_GE:
399                 return (compare >= 0);
400
401         default:
402                 return 0;
403         }
404 }
405
406 static char const hextab[] = "0123456789abcdef";
407
408 /** Convert string value to a value_data_t type
409  *
410  * @param[in] ctx to alloc strings in.
411  * @param[out] dst where to write parsed value.
412  * @param[in,out] src_type of value data to create/type of value created.
413  * @param[in] src_enumv DICT_ATTR with string aliases for integer values.
414  * @param[in] src String to convert. Binary safe for variable length values if len is provided.
415  * @param[in] src_len may be < 0 in which case strlen(len) is used to determine length, else src_len
416  *        should be the length of the string or sub string to parse.
417  * @param[in] quote quotation character used to drive de-escaping
418  * @return length of data written to out or -1 on parse error.
419  */
420 ssize_t value_data_from_str(TALLOC_CTX *ctx, value_data_t *dst,
421                             PW_TYPE *src_type, DICT_ATTR const *src_enumv,
422                             char const *src, ssize_t src_len, char quote)
423 {
424         DICT_VALUE      *dval;
425         size_t          len;
426         ssize_t         ret;
427         char            buffer[256];
428
429         if (!src) return -1;
430
431         len = (src_len < 0) ? strlen(src) : (size_t)src_len;
432
433         /*
434          *      Set size for all fixed length attributes.
435          */
436         ret = dict_attr_sizes[*src_type][1];    /* Max length */
437
438         /*
439          *      It's a variable ret src_type so we just alloc a new buffer
440          *      of size len and copy.
441          */
442         switch (*src_type) {
443         case PW_TYPE_STRING:
444         {
445                 char            *p, *buff;
446                 char const      *q;
447                 int             x;
448
449                 buff = p = talloc_bstrndup(ctx, src, len);
450
451                 /*
452                  *      No de-quoting.  Just copy the string.
453                  */
454                 if (!quote) {
455                         ret = len;
456                         dst->strvalue = buff;
457                         goto finish;
458                 }
459
460                 /*
461                  *      Do escaping for single quoted strings.  Only
462                  *      single quotes get escaped.  Everything else is
463                  *      left as-is.
464                  */
465                 if (quote == '\'') {
466                         q = p;
467
468                         while (q < (buff + len)) {
469                                 /*
470                                  *      The quotation character is escaped.
471                                  */
472                                 if ((q[0] == '\\') &&
473                                     (q[1] == quote)) {
474                                         *(p++) = quote;
475                                         q += 2;
476                                         continue;
477                                 }
478
479                                 /*
480                                  *      Two backslashes get mangled to one.
481                                  */
482                                 if ((q[0] == '\\') &&
483                                     (q[1] == '\\')) {
484                                         *(p++) = '\\';
485                                         q += 2;
486                                         continue;
487                                 }
488
489                                 /*
490                                  *      Not escaped, just copy it over.
491                                  */
492                                 *(p++) = *(q++);
493                         }
494
495                         *p = '\0';
496                         ret = p - buff;
497
498                         /* Shrink the buffer to the correct size */
499                         dst->strvalue = talloc_realloc(ctx, buff, char, ret + 1);
500                         goto finish;
501                 }
502
503                 /*
504                  *      It's "string" or `string`, do all standard
505                  *      escaping.
506                  */
507                 q = p;
508                 while (q < (buff + len)) {
509                         char c = *q++;
510
511                         if ((c == '\\') && (q >= (buff + len))) {
512                                 fr_strerror_printf("Invalid escape at end of string");
513                                 talloc_free(buff);
514                                 return -1;
515                         }
516
517                         /*
518                          *      Fix up \X -> ... the binary form of it.
519                          */
520                         if (c == '\\') {
521                                 switch (*q) {
522                                 case 'r':
523                                         c = '\r';
524                                         q++;
525                                         break;
526
527                                 case 'n':
528                                         c = '\n';
529                                         q++;
530                                         break;
531
532                                 case 't':
533                                         c = '\t';
534                                         q++;
535                                         break;
536
537                                 case '\\':
538                                         c = '\\';
539                                         q++;
540                                         break;
541
542                                 default:
543                                         /*
544                                          *      \" --> ", but only inside of double quoted strings, etc.
545                                          */
546                                         if (*q == quote) {
547                                                 c = quote;
548                                                 q++;
549                                                 break;
550                                         }
551
552                                         /*
553                                          *      \000 --> binary zero character
554                                          */
555                                         if ((q[0] >= '0') &&
556                                             (q[0] <= '9') &&
557                                             (q[1] >= '0') &&
558                                             (q[1] <= '9') &&
559                                             (q[2] >= '0') &&
560                                             (q[2] <= '9') &&
561                                             (sscanf(q, "%3o", &x) == 1)) {
562                                                 c = x;
563                                                 q += 3;
564                                         }
565
566                                         /*
567                                          *      Else It's not a recognised escape sequence DON'T
568                                          *      consume the backslash. This is identical
569                                          *      behaviour to bash and most other things that
570                                          *      use backslash escaping.
571                                          */
572                                 }
573                         }
574
575                         *p++ = c;
576                 }
577
578                 *p = '\0';
579                 ret = p - buff;
580                 dst->strvalue = talloc_realloc(ctx, buff, char, ret + 1);
581         }
582                 goto finish;
583
584         case PW_TYPE_VSA:
585                 fr_strerror_printf("Must use 'Attr-26 = ...' instead of 'Vendor-Specific = ...'");
586                 return -1;
587
588         /* raw octets: 0x01020304... */
589         case PW_TYPE_OCTETS:
590         {
591                 uint8_t *p;
592
593                 /*
594                  *      No 0x prefix, just copy verbatim.
595                  */
596                 if ((len < 2) || (strncasecmp(src, "0x", 2) != 0)) {
597                         dst->octets = talloc_memdup(ctx, (uint8_t const *)src, len);
598                         talloc_set_type(dst->octets, uint8_t);
599                         ret = len;
600                         goto finish;
601                 }
602
603                 len -= 2;
604
605                 /*
606                  *      Invalid.
607                  */
608                 if ((len & 0x01) != 0) {
609                         fr_strerror_printf("Length of Hex String is not even, got %zu bytes", len);
610                         return -1;
611                 }
612
613                 ret = len >> 1;
614                 p = talloc_array(ctx, uint8_t, ret);
615                 if (fr_hex2bin(p, ret, src + 2, len) != (size_t)ret) {
616                         talloc_free(p);
617                         fr_strerror_printf("Invalid hex data");
618                         return -1;
619                 }
620
621                 dst->octets = p;
622         }
623                 goto finish;
624
625         case PW_TYPE_ABINARY:
626 #ifdef WITH_ASCEND_BINARY
627                 if ((len > 1) && (strncasecmp(src, "0x", 2) == 0)) {
628                         ssize_t bin;
629
630                         if (len > ((sizeof(dst->filter) + 1) * 2)) {
631                                 fr_strerror_printf("Hex data is too large for ascend filter");
632                                 return -1;
633                         }
634
635                         bin = fr_hex2bin((uint8_t *) &dst->filter, ret, src + 2, len);
636                         if (bin < ret) {
637                                 memset(((uint8_t *) &dst->filter) + bin, 0, ret - bin);
638                         }
639                 } else {
640                         if (ascend_parse_filter(dst, src, len) < 0 ) {
641                                 /* Allow ascend_parse_filter's strerror to bubble up */
642                                 return -1;
643                         }
644                 }
645
646                 ret = sizeof(dst->filter);
647                 goto finish;
648 #else
649                 /*
650                  *      If Ascend binary is NOT defined,
651                  *      then fall through to raw octets, so that
652                  *      the user can at least make them by hand...
653                  */
654                 goto do_octets;
655 #endif
656
657         /* don't use this! */
658         case PW_TYPE_TLV:
659                 fr_strerror_printf("Cannot parse TLV");
660                 return -1;
661
662         case PW_TYPE_IPV4_ADDR:
663         {
664                 fr_ipaddr_t addr;
665
666                 if (fr_pton4(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
667
668                 /*
669                  *      We allow v4 addresses to have a /32 suffix as some databases (PostgreSQL)
670                  *      print them this way.
671                  */
672                 if (addr.prefix != 32) {
673                         fr_strerror_printf("Invalid IPv4 mask length \"/%i\".  Only \"/32\" permitted "
674                                            "for non-prefix types", addr.prefix);
675                         return -1;
676                 }
677
678                 dst->ipaddr.s_addr = addr.ipaddr.ip4addr.s_addr;
679         }
680                 goto finish;
681
682         case PW_TYPE_IPV4_PREFIX:
683         {
684                 fr_ipaddr_t addr;
685
686                 if (fr_pton4(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
687
688                 dst->ipv4prefix[1] = addr.prefix;
689                 memcpy(&dst->ipv4prefix[2], &addr.ipaddr.ip4addr.s_addr, sizeof(dst->ipv4prefix) - 2);
690         }
691                 goto finish;
692
693         case PW_TYPE_IPV6_ADDR:
694         {
695                 fr_ipaddr_t addr;
696
697                 if (fr_pton6(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
698
699                 /*
700                  *      We allow v6 addresses to have a /128 suffix as some databases (PostgreSQL)
701                  *      print them this way.
702                  */
703                 if (addr.prefix != 128) {
704                         fr_strerror_printf("Invalid IPv6 mask length \"/%i\".  Only \"/128\" permitted "
705                                            "for non-prefix types", addr.prefix);
706                         return -1;
707                 }
708
709                 memcpy(&dst->ipv6addr, addr.ipaddr.ip6addr.s6_addr, sizeof(dst->ipv6addr));
710         }
711                 goto finish;
712
713         case PW_TYPE_IPV6_PREFIX:
714         {
715                 fr_ipaddr_t addr;
716
717                 if (fr_pton6(&addr, src, src_len, fr_hostname_lookups, false) < 0) return -1;
718
719                 dst->ipv6prefix[1] = addr.prefix;
720                 memcpy(&dst->ipv6prefix[2], addr.ipaddr.ip6addr.s6_addr, sizeof(dst->ipv6prefix) - 2);
721         }
722                 goto finish;
723
724         default:
725                 break;
726         }
727
728         /*
729          *      It's a fixed size src_type, copy to a temporary buffer and
730          *      \0 terminate if insize >= 0.
731          */
732         if (src_len > 0) {
733                 if (len >= sizeof(buffer)) {
734                         fr_strerror_printf("Temporary buffer too small");
735                         return -1;
736                 }
737
738                 memcpy(buffer, src, src_len);
739                 buffer[src_len] = '\0';
740                 src = buffer;
741         }
742
743         switch (*src_type) {
744         case PW_TYPE_BYTE:
745         {
746                 char *p;
747                 unsigned int i;
748
749                 /*
750                  *      Note that ALL integers are unsigned!
751                  */
752                 i = fr_strtoul(src, &p);
753
754                 /*
755                  *      Look for the named src for the given
756                  *      attribute.
757                  */
758                 if (src_enumv && *p && !is_whitespace(p)) {
759                         if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
760                                 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
761                                                    src, src_enumv->name);
762                                 return -1;
763                         }
764
765                         dst->byte = dval->value;
766                 } else {
767                         if (i > 255) {
768                                 fr_strerror_printf("Byte value \"%s\" is larger than 255", src);
769                                 return -1;
770                         }
771
772                         dst->byte = i;
773                 }
774                 break;
775         }
776
777         case PW_TYPE_SHORT:
778         {
779                 char *p;
780                 unsigned int i;
781
782                 /*
783                  *      Note that ALL integers are unsigned!
784                  */
785                 i = fr_strtoul(src, &p);
786
787                 /*
788                  *      Look for the named src for the given
789                  *      attribute.
790                  */
791                 if (src_enumv && *p && !is_whitespace(p)) {
792                         if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
793                                 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
794                                                    src, src_enumv->name);
795                                 return -1;
796                         }
797
798                         dst->ushort = dval->value;
799                 } else {
800                         if (i > 65535) {
801                                 fr_strerror_printf("Short value \"%s\" is larger than 65535", src);
802                                 return -1;
803                         }
804
805                         dst->ushort = i;
806                 }
807                 break;
808         }
809
810         case PW_TYPE_INTEGER:
811         {
812                 char *p;
813                 unsigned int i;
814
815                 /*
816                  *      Note that ALL integers are unsigned!
817                  */
818                 i = fr_strtoul(src, &p);
819
820                 /*
821                  *      Look for the named src for the given
822                  *      attribute.
823                  */
824                 if (src_enumv && *p && !is_whitespace(p)) {
825                         if ((dval = dict_valbyname(src_enumv->attr, src_enumv->vendor, src)) == NULL) {
826                                 fr_strerror_printf("Unknown or invalid value \"%s\" for attribute %s",
827                                                    src, src_enumv->name);
828                                 return -1;
829                         }
830
831                         dst->integer = dval->value;
832                 } else {
833                         /*
834                          *      Value is always within the limits
835                          */
836                         dst->integer = i;
837                 }
838         }
839                 break;
840
841         case PW_TYPE_INTEGER64:
842         {
843                 uint64_t i;
844
845                 /*
846                  *      Note that ALL integers are unsigned!
847                  */
848                 if (sscanf(src, "%" PRIu64, &i) != 1) {
849                         fr_strerror_printf("Failed parsing \"%s\" as unsigned 64bit integer", src);
850                         return -1;
851                 }
852                 dst->integer64 = i;
853         }
854                 break;
855
856         case PW_TYPE_DATE:
857         {
858                 /*
859                  *      time_t may be 64 bits, whule vp_date MUST be 32-bits.  We need an
860                  *      intermediary variable to handle the conversions.
861                  */
862                 time_t date;
863
864                 if (fr_get_time(src, &date) < 0) {
865                         fr_strerror_printf("failed to parse time string \"%s\"", src);
866                         return -1;
867                 }
868
869                 dst->date = date;
870         }
871
872                 break;
873
874         case PW_TYPE_IFID:
875                 if (ifid_aton(src, (void *) dst->ifid) == NULL) {
876                         fr_strerror_printf("Failed to parse interface-id string \"%s\"", src);
877                         return -1;
878                 }
879                 break;
880
881         case PW_TYPE_ETHERNET:
882         {
883                 char const *c1, *c2, *cp;
884                 size_t p_len = 0;
885
886                 /*
887                  *      Convert things which are obviously integers to Ethernet addresses
888                  *
889                  *      We assume the number is the bigendian representation of the
890                  *      ethernet address.
891                  */
892                 if (is_integer(src)) {
893                         uint64_t integer = htonll(atoll(src));
894
895                         memcpy(dst->ether, &integer, sizeof(dst->ether));
896                         break;
897                 }
898
899                 cp = src;
900                 while (*cp) {
901                         if (cp[1] == ':') {
902                                 c1 = hextab;
903                                 c2 = memchr(hextab, tolower((int) cp[0]), 16);
904                                 cp += 2;
905                         } else if ((cp[1] != '\0') && ((cp[2] == ':') || (cp[2] == '\0'))) {
906                                 c1 = memchr(hextab, tolower((int) cp[0]), 16);
907                                 c2 = memchr(hextab, tolower((int) cp[1]), 16);
908                                 cp += 2;
909                                 if (*cp == ':') cp++;
910                         } else {
911                                 c1 = c2 = NULL;
912                         }
913                         if (!c1 || !c2 || (p_len >= sizeof(dst->ether))) {
914                                 fr_strerror_printf("failed to parse Ethernet address \"%s\"", src);
915                                 return -1;
916                         }
917                         dst->ether[p_len] = ((c1-hextab)<<4) + (c2-hextab);
918                         p_len++;
919                 }
920         }
921                 break;
922
923         /*
924          *      Crazy polymorphic (IPv4/IPv6) attribute src_type for WiMAX.
925          *
926          *      We try and make is saner by replacing the original
927          *      da, with either an IPv4 or IPv6 da src_type.
928          *
929          *      These are not dynamic da, and will have the same vendor
930          *      and attribute as the original.
931          */
932         case PW_TYPE_COMBO_IP_ADDR:
933         {
934                 if (inet_pton(AF_INET6, src, &dst->ipv6addr) > 0) {
935                         *src_type = PW_TYPE_IPV6_ADDR;
936                         ret = dict_attr_sizes[PW_TYPE_COMBO_IP_ADDR][1]; /* size of IPv6 address */
937                 } else {
938                         fr_ipaddr_t ipaddr;
939
940                         if (ip_hton(&ipaddr, AF_INET, src, false) < 0) {
941                                 fr_strerror_printf("Failed to find IPv4 address for %s", src);
942                                 return -1;
943                         }
944
945                         *src_type = PW_TYPE_IPV4_ADDR;
946                         dst->ipaddr.s_addr = ipaddr.ipaddr.ip4addr.s_addr;
947                         ret = dict_attr_sizes[PW_TYPE_COMBO_IP_ADDR][0]; /* size of IPv4 address */
948                 }
949         }
950                 break;
951
952         case PW_TYPE_SIGNED:
953                 /* Damned code for 1 WiMAX attribute */
954                 dst->sinteger = (int32_t)strtol(src, NULL, 10);
955                 break;
956
957         /*
958          *  Anything else.
959          */
960         default:
961                 fr_strerror_printf("Unknown attribute type %d", *src_type);
962                 return -1;
963         }
964
965 finish:
966         return ret;
967 }
968
969 /** Performs byte order reversal for types that need it
970  *
971  */
972 static void value_data_hton(value_data_t *dst, PW_TYPE type, void const *src, size_t src_len)
973 {
974         /* 8 byte integers */
975         switch (type) {
976         case PW_TYPE_INTEGER64:
977                 dst->integer64 = htonll(*(uint64_t const *)src);
978                 break;
979
980         /* 4 byte integers */
981         case PW_TYPE_INTEGER:
982         case PW_TYPE_DATE:
983         case PW_TYPE_SIGNED:
984                 dst->integer = htonl(*(uint32_t const *)src);
985                 break;
986
987         /* 2 byte integers */
988         case PW_TYPE_SHORT:
989                 dst->ushort = htons(*(uint16_t const *)src);
990                 break;
991
992         case PW_TYPE_OCTETS:
993         case PW_TYPE_STRING:
994                 fr_assert(0);
995                 return;         /* shouldn't happen */
996
997         default:
998                 memcpy(dst, src, src_len);
999         }
1000 }
1001
1002 /** Convert one type of value_data_t to another
1003  *
1004  * @note This should be the canonical function used to convert between data types.
1005  *
1006  * @param ctx to allocate buffers in (usually the same as dst)
1007  * @param dst Where to write result of casting.
1008  * @param dst_type to cast to.
1009  * @param dst_enumv Enumerated values used to converts strings to integers.
1010  * @param src_type to cast from.
1011  * @param src_enumv Enumerated values used to convert integers to strings.
1012  * @param src Input data.
1013  * @param src_len Input data len.
1014  * @return the length of data in the dst or -1 on error.
1015  */
1016 ssize_t value_data_cast(TALLOC_CTX *ctx, value_data_t *dst,
1017                         PW_TYPE dst_type, DICT_ATTR const *dst_enumv,
1018                         PW_TYPE src_type, DICT_ATTR const *src_enumv,
1019                         value_data_t const *src, size_t src_len)
1020 {
1021         if (!fr_assert(dst_type != src_type)) return -1;
1022
1023         /*
1024          *      Deserialise a value_data_t
1025          */
1026         if (src_type == PW_TYPE_STRING) {
1027                 return value_data_from_str(ctx, dst, &dst_type, dst_enumv, src->strvalue, src_len, '\0');
1028         }
1029
1030         /*
1031          *      Converts the src data to octets with no processing.
1032          */
1033         if (dst_type == PW_TYPE_OCTETS) {
1034                 value_data_hton(dst, src_type, src, src_len);
1035                 dst->octets = talloc_memdup(ctx, dst, src_len);
1036                 talloc_set_type(dst->octets, uint8_t);
1037                 return talloc_array_length(dst->strvalue);
1038         }
1039
1040         /*
1041          *      Serialise a value_data_t
1042          */
1043         if (dst_type == PW_TYPE_STRING) {
1044                 dst->strvalue = value_data_aprints(ctx, src_type, src_enumv, src, src_len, '\0');
1045                 return talloc_array_length(dst->strvalue) - 1;
1046         }
1047
1048         if ((src_type == PW_TYPE_IFID) &&
1049             (dst_type == PW_TYPE_INTEGER64)) {
1050                 memcpy(&dst->integer64, src->ifid, sizeof(src->ifid));
1051                 dst->integer64 = htonll(dst->integer64);
1052         fixed_length:
1053                 return dict_attr_sizes[dst_type][0];
1054         }
1055
1056         if ((src_type == PW_TYPE_INTEGER64) &&
1057             (dst_type == PW_TYPE_ETHERNET)) {
1058                 uint8_t array[8];
1059                 uint64_t i;
1060
1061                 i = htonll(src->integer64);
1062                 memcpy(array, &i, 8);
1063
1064                 /*
1065                  *      For OUIs in the DB.
1066                  */
1067                 if ((array[0] != 0) || (array[1] != 0)) return -1;
1068
1069                 memcpy(dst->ether, &array[2], 6);
1070                 goto fixed_length;
1071         }
1072
1073         /*
1074          *      For integers, we allow the casting of a SMALL type to
1075          *      a larger type, but not vice-versa.
1076          */
1077         if (dst_type == PW_TYPE_INTEGER64) {
1078                 switch (src_type) {
1079                 case PW_TYPE_BYTE:
1080                         dst->integer64 = src->byte;
1081                         break;
1082
1083                 case PW_TYPE_SHORT:
1084                         dst->integer64 = src->ushort;
1085                         break;
1086
1087                 case PW_TYPE_INTEGER:
1088                         dst->integer64 = src->integer;
1089                         break;
1090
1091                 case PW_TYPE_DATE:
1092                         dst->integer64 = src->date;
1093                         break;
1094
1095                 case PW_TYPE_OCTETS:
1096                         goto do_octets;
1097
1098                 default:
1099                 invalid_cast:
1100                         fr_strerror_printf("Invalid cast from %s to %s",
1101                                            fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1102                                            fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1103                         return -1;
1104
1105                 }
1106                 goto fixed_length;
1107         }
1108
1109         /*
1110          *      We can cast LONG integers to SHORTER ones, so long
1111          *      as the long one is on the LHS.
1112          */
1113         if (dst_type == PW_TYPE_INTEGER) {
1114                 switch (src_type) {
1115                 case PW_TYPE_BYTE:
1116                         dst->integer = src->byte;
1117                         break;
1118
1119                 case PW_TYPE_SHORT:
1120                         dst->integer = src->ushort;
1121                         break;
1122
1123                 case PW_TYPE_OCTETS:
1124                         goto do_octets;
1125
1126                 default:
1127                         goto invalid_cast;
1128                 }
1129                 goto fixed_length;
1130         }
1131
1132         if (dst_type == PW_TYPE_SHORT) {
1133                 switch (src_type) {
1134                 case PW_TYPE_BYTE:
1135                         dst->ushort = src->byte;
1136                         break;
1137
1138                 case PW_TYPE_OCTETS:
1139                         goto do_octets;
1140
1141                 default:
1142                         goto invalid_cast;
1143                 }
1144                 goto fixed_length;
1145         }
1146
1147         /*
1148          *      We can cast integers less that < INT_MAX to signed
1149          */
1150         if (dst_type == PW_TYPE_SIGNED) {
1151                 switch (src_type) {
1152                 case PW_TYPE_BYTE:
1153                         dst->sinteger = src->byte;
1154                         break;
1155
1156                 case PW_TYPE_SHORT:
1157                         dst->sinteger = src->ushort;
1158                         break;
1159
1160                 case PW_TYPE_INTEGER:
1161                         if (src->integer > INT_MAX) {
1162                                 fr_strerror_printf("Invalid cast: From integer to signed.  integer value %u is larger "
1163                                                    "than max signed int and would overflow", src->integer);
1164                                 return -1;
1165                         }
1166                         dst->sinteger = (int)src->integer;
1167                         break;
1168
1169                 case PW_TYPE_INTEGER64:
1170                         if (src->integer > INT_MAX) {
1171                                 fr_strerror_printf("Invalid cast: From integer64 to signed.  integer64 value %" PRIu64
1172                                                    " is larger than max signed int and would overflow", src->integer64);
1173                                 return -1;
1174                         }
1175                         dst->sinteger = (int)src->integer64;
1176                         break;
1177
1178                 case PW_TYPE_OCTETS:
1179                         goto do_octets;
1180
1181                 default:
1182                         goto invalid_cast;
1183                 }
1184                 goto fixed_length;
1185         }
1186         /*
1187          *      Conversions between IPv4 addresses, IPv6 addresses, IPv4 prefixes and IPv6 prefixes
1188          *
1189          *      For prefix to ipaddress conversions, we assume that the host portion has already
1190          *      been zeroed out.
1191          *
1192          *      We allow casts from v6 to v4 if the v6 address has the correct mapping prefix.
1193          *
1194          *      We only allow casts from prefixes to addresses if the prefix is the the length of
1195          *      the address, e.g. 32 for ipv4 128 for ipv6.
1196          */
1197         {
1198                 /*
1199                  *      10 bytes of 0x00 2 bytes of 0xff
1200                  */
1201                 static uint8_t const v4_v6_map[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1202                                                      0x00, 0x00, 0x00, 0x00, 0xff, 0xff };
1203
1204                 switch (dst_type) {
1205                 case PW_TYPE_IPV4_ADDR:
1206                         switch (src_type) {
1207                         case PW_TYPE_IPV6_ADDR:
1208                                 if (memcmp(src->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) {
1209                                 bad_v6_prefix_map:
1210                                         fr_strerror_printf("Invalid cast from %s to %s.  No IPv4-IPv6 mapping prefix",
1211                                                            fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1212                                                            fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1213                                         return -1;
1214                                 }
1215
1216                                 memcpy(&dst->ipaddr, &src->ipv6addr.s6_addr[sizeof(v4_v6_map)],
1217                                        sizeof(dst->ipaddr));
1218                                 goto fixed_length;
1219
1220                         case PW_TYPE_IPV4_PREFIX:
1221                                 if (src->ipv4prefix[1] != 32) {
1222                                 bad_v4_prefix_len:
1223                                         fr_strerror_printf("Invalid cast from %s to %s.  Only /32 prefixes may be "
1224                                                            "cast to IP address types",
1225                                                            fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1226                                                            fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1227                                         return -1;
1228                                 }
1229
1230                                 memcpy(&dst->ipaddr, &src->ipv4prefix[2], sizeof(dst->ipaddr));
1231                                 goto fixed_length;
1232
1233                         case PW_TYPE_IPV6_PREFIX:
1234                                 if (src->ipv6prefix[1] != 128) {
1235                                 bad_v6_prefix_len:
1236                                         fr_strerror_printf("Invalid cast from %s to %s.  Only /128 prefixes may be "
1237                                                            "cast to IP address types",
1238                                                            fr_int2str(dict_attr_types, src_type, "<INVALID>"),
1239                                                            fr_int2str(dict_attr_types, dst_type, "<INVALID>"));
1240                                         return -1;
1241                                 }
1242                                 if (memcmp(&src->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map)) != 0) {
1243                                         goto bad_v6_prefix_map;
1244                                 }
1245                                 memcpy(&dst->ipaddr, &src->ipv6prefix[2 + sizeof(v4_v6_map)],
1246                                        sizeof(dst->ipaddr));
1247                                 goto fixed_length;
1248
1249                         default:
1250                                 break;
1251                         }
1252                         break;
1253
1254                 case PW_TYPE_IPV6_ADDR:
1255                         switch (src_type) {
1256                         case PW_TYPE_IPV4_ADDR:
1257                                 /* Add the v4/v6 mapping prefix */
1258                                 memcpy(dst->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map));
1259                                 memcpy(&dst->ipv6addr.s6_addr[sizeof(v4_v6_map)], &src->ipaddr,
1260                                        sizeof(dst->ipv6addr.s6_addr) - sizeof(v4_v6_map));
1261
1262                                 goto fixed_length;
1263
1264                         case PW_TYPE_IPV4_PREFIX:
1265                                 if (src->ipv4prefix[1] != 32) goto bad_v4_prefix_len;
1266
1267                                 /* Add the v4/v6 mapping prefix */
1268                                 memcpy(dst->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map));
1269                                 memcpy(&dst->ipv6addr.s6_addr[sizeof(v4_v6_map)], &src->ipv4prefix[2],
1270                                        sizeof(dst->ipv6addr.s6_addr) - sizeof(v4_v6_map));
1271                                 goto fixed_length;
1272
1273                         case PW_TYPE_IPV6_PREFIX:
1274                                 if (src->ipv4prefix[1] != 128) goto bad_v6_prefix_len;
1275
1276                                 memcpy(dst->ipv6addr.s6_addr, &src->ipv6prefix[2], sizeof(dst->ipv6addr.s6_addr));
1277                                 goto fixed_length;
1278
1279                         default:
1280                                 break;
1281                         }
1282                         break;
1283
1284                 case PW_TYPE_IPV4_PREFIX:
1285                         switch (src_type) {
1286                         case PW_TYPE_IPV4_ADDR:
1287                                 memcpy(&dst->ipv4prefix[2], &src->ipaddr, sizeof(dst->ipv4prefix) - 2);
1288                                 dst->ipv4prefix[0] = 0;
1289                                 dst->ipv4prefix[1] = 32;
1290                                 goto fixed_length;
1291
1292                         case PW_TYPE_IPV6_ADDR:
1293                                 if (memcmp(src->ipv6addr.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) {
1294                                         goto bad_v6_prefix_map;
1295                                 }
1296                                 memcpy(&dst->ipv4prefix[2], &src->ipv6addr.s6_addr[sizeof(v4_v6_map)],
1297                                        sizeof(dst->ipv4prefix) - 2);
1298                                 dst->ipv4prefix[0] = 0;
1299                                 dst->ipv4prefix[1] = 32;
1300                                 goto fixed_length;
1301
1302                         case PW_TYPE_IPV6_PREFIX:
1303                                 if (memcmp(&src->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map)) != 0) {
1304                                         goto bad_v6_prefix_map;
1305                                 }
1306
1307                                 /*
1308                                  *      Prefix must be >= 96 bits. If it's < 96 bytes and the
1309                                  *      above check passed, the v6 address wasn't masked
1310                                  *      correctly when it was packet into a value_data_t.
1311                                  */
1312                                 if (!fr_assert(src->ipv6prefix[1] >= (sizeof(v4_v6_map) * 8))) return -1;
1313
1314                                 memcpy(&dst->ipv4prefix[2], &src->ipv6prefix[2 + sizeof(v4_v6_map)],
1315                                        sizeof(dst->ipv4prefix) - 2);
1316                                 dst->ipv4prefix[0] = 0;
1317                                 dst->ipv4prefix[1] = src->ipv6prefix[1] - (sizeof(v4_v6_map) * 8);
1318                                 goto fixed_length;
1319
1320                         default:
1321                                 break;
1322                         }
1323                         break;
1324
1325                 case PW_TYPE_IPV6_PREFIX:
1326                         switch (src_type) {
1327                         case PW_TYPE_IPV4_ADDR:
1328                                 /* Add the v4/v6 mapping prefix */
1329                                 memcpy(&dst->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map));
1330                                 memcpy(&dst->ipv6prefix[2 + sizeof(v4_v6_map)], &src->ipaddr,
1331                                        (sizeof(dst->ipv6prefix) - 2) - sizeof(v4_v6_map));
1332                                 dst->ipv6prefix[0] = 0;
1333                                 dst->ipv6prefix[1] = 128;
1334                                 goto fixed_length;
1335
1336                         case PW_TYPE_IPV4_PREFIX:
1337                                 /* Add the v4/v6 mapping prefix */
1338                                 memcpy(&dst->ipv6prefix[2], v4_v6_map, sizeof(v4_v6_map));
1339                                 memcpy(&dst->ipv6prefix[2 + sizeof(v4_v6_map)], &src->ipv4prefix[2],
1340                                        (sizeof(dst->ipv6prefix) - 2) - sizeof(v4_v6_map));
1341                                 dst->ipv6prefix[0] = 0;
1342                                 dst->ipv6prefix[1] = (sizeof(v4_v6_map) * 8) + src->ipv4prefix[1];
1343                                 goto fixed_length;
1344
1345                         case PW_TYPE_IPV6_ADDR:
1346                                 memcpy(&dst->ipv6prefix[2], &src->ipv6addr, sizeof(dst->ipv6prefix) - 2);
1347                                 dst->ipv6prefix[0] = 0;
1348                                 dst->ipv6prefix[1] = 128;
1349                                 goto fixed_length;
1350
1351                         default:
1352                                 break;
1353                         }
1354
1355                         break;
1356
1357                 default:
1358                         break;
1359                 }
1360         }
1361
1362         /*
1363          *      The attribute we've found has to have a size which is
1364          *      compatible with the type of the destination cast.
1365          */
1366         if ((src_len < dict_attr_sizes[dst_type][0]) ||
1367             (src_len > dict_attr_sizes[dst_type][1])) {
1368                 char const *src_type_name;
1369
1370                 src_type_name =  fr_int2str(dict_attr_types, src_type, "<INVALID>");
1371                 fr_strerror_printf("Invalid cast from %s to %s. Length should be between %zu and %zu but is %zu",
1372                                    src_type_name,
1373                                    fr_int2str(dict_attr_types, dst_type, "<INVALID>"),
1374                                    dict_attr_sizes[dst_type][0], dict_attr_sizes[dst_type][1],
1375                                    src_len);
1376                 return -1;
1377         }
1378
1379         if (src_type == PW_TYPE_OCTETS) {
1380         do_octets:
1381                 value_data_hton(dst, dst_type, src->octets, src_len);
1382                 return src_len;
1383         }
1384
1385         /*
1386          *      Convert host order to network byte order.
1387          */
1388         if ((dst_type == PW_TYPE_IPV4_ADDR) &&
1389             ((src_type == PW_TYPE_INTEGER) ||
1390              (src_type == PW_TYPE_DATE) ||
1391              (src_type == PW_TYPE_SIGNED))) {
1392                 dst->ipaddr.s_addr = htonl(src->integer);
1393
1394         } else if ((src_type == PW_TYPE_IPV4_ADDR) &&
1395                    ((dst_type == PW_TYPE_INTEGER) ||
1396                     (dst_type == PW_TYPE_DATE) ||
1397                     (dst_type == PW_TYPE_SIGNED))) {
1398                 dst->integer = htonl(src->ipaddr.s_addr);
1399
1400         } else {                /* they're of the same byte order */
1401                 memcpy(&dst, &src, src_len);
1402         }
1403
1404         return src_len;
1405 }
1406
1407 /** Copy value data verbatim duplicating any buffers
1408  *
1409  * @param ctx To allocate buffers in.
1410  * @param dst Where to copy value_data to.
1411  * @param src_type Type of src.
1412  * @param src Where to copy value_data from.
1413  * @param src_len Where
1414  */
1415 ssize_t value_data_copy(TALLOC_CTX *ctx, value_data_t *dst, PW_TYPE src_type,
1416                         const value_data_t *src, size_t src_len)
1417 {
1418         switch (src_type) {
1419         default:
1420                 memcpy(dst, src, sizeof(*src));
1421                 break;
1422
1423         case PW_TYPE_STRING:
1424                 dst->strvalue = talloc_bstrndup(ctx, src->strvalue, src_len);
1425                 if (!dst->strvalue) return -1;
1426                 break;
1427
1428         case PW_TYPE_OCTETS:
1429                 dst->octets = talloc_memdup(ctx, src->octets, src_len);
1430                 talloc_set_type(dst->strvalue, uint8_t);
1431                 if (!dst->octets) return -1;
1432                 break;
1433         }
1434
1435         return src_len;
1436 }
1437
1438
1439
1440 /** Print one attribute value to a string
1441  *
1442  */
1443 char *value_data_aprints(TALLOC_CTX *ctx,
1444                          PW_TYPE type, DICT_ATTR const *enumv, value_data_t const *data,
1445                          size_t inlen, char quote)
1446 {
1447         char *p = NULL;
1448         unsigned int i;
1449
1450         switch (type) {
1451         case PW_TYPE_STRING:
1452         {
1453                 size_t len, ret;
1454
1455                 if (!quote) {
1456                         p = talloc_bstrndup(ctx, data->strvalue, inlen);
1457                         if (!p) return NULL;
1458                         talloc_set_type(p, char);
1459                         return p;
1460                 }
1461
1462                 /* Gets us the size of the buffer we need to alloc */
1463                 len = fr_prints_len(data->strvalue, inlen, quote);
1464                 p = talloc_array(ctx, char, len);
1465                 if (!p) return NULL;
1466
1467                 ret = fr_prints(p, len, data->strvalue, inlen, quote);
1468                 if (!fr_assert(ret == (len - 1))) {
1469                         talloc_free(p);
1470                         return NULL;
1471                 }
1472                 break;
1473         }
1474
1475         case PW_TYPE_INTEGER:
1476                 i = data->integer;
1477                 goto print_int;
1478
1479         case PW_TYPE_SHORT:
1480                 i = data->ushort;
1481                 goto print_int;
1482
1483         case PW_TYPE_BYTE:
1484                 i = data->byte;
1485
1486         print_int:
1487         {
1488                 DICT_VALUE const *dv;
1489
1490                 if (enumv && (dv = dict_valbyattr(enumv->attr, enumv->vendor, i))) {
1491                         p = talloc_typed_strdup(ctx, dv->name);
1492                 } else {
1493                         p = talloc_typed_asprintf(ctx, "%u", i);
1494                 }
1495         }
1496                 break;
1497
1498         case PW_TYPE_SIGNED:
1499                 p = talloc_typed_asprintf(ctx, "%d", data->sinteger);
1500                 break;
1501
1502         case PW_TYPE_INTEGER64:
1503                 p = talloc_typed_asprintf(ctx, "%" PRIu64 , data->integer64);
1504                 break;
1505
1506         case PW_TYPE_ETHERNET:
1507                 p = talloc_typed_asprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1508                                           data->ether[0], data->ether[1],
1509                                           data->ether[2], data->ether[3],
1510                                           data->ether[4], data->ether[5]);
1511                 break;
1512
1513         case PW_TYPE_ABINARY:
1514 #ifdef WITH_ASCEND_BINARY
1515                 p = talloc_array(ctx, char, 128);
1516                 if (!p) return NULL;
1517                 print_abinary(p, 128, (uint8_t *) &data->filter, inlen, 0);
1518                 break;
1519 #else
1520                   /* FALL THROUGH */
1521 #endif
1522
1523         case PW_TYPE_OCTETS:
1524                 p = talloc_array(ctx, char, 2 + 1 + inlen * 2);
1525                 if (!p) return NULL;
1526                 p[0] = '0';
1527                 p[1] = 'x';
1528
1529                 fr_bin2hex(p + 2, data->octets, inlen);
1530                 break;
1531
1532         case PW_TYPE_DATE:
1533         {
1534                 time_t t;
1535                 struct tm s_tm;
1536
1537                 t = data->date;
1538
1539                 p = talloc_array(ctx, char, 64);
1540                 strftime(p, 64, "%b %e %Y %H:%M:%S %Z",
1541                          localtime_r(&t, &s_tm));
1542                 break;
1543         }
1544
1545         /*
1546          *      We need to use the proper inet_ntop functions for IP
1547          *      addresses, else the output might not match output of
1548          *      other functions, which makes testing difficult.
1549          *
1550          *      An example is tunnelled ipv4 in ipv6 addresses.
1551          */
1552         case PW_TYPE_IPV4_ADDR:
1553         case PW_TYPE_IPV4_PREFIX:
1554         {
1555                 char buff[INET_ADDRSTRLEN  + 4]; // + /prefix
1556
1557                 buff[0] = '\0';
1558                 value_data_prints(buff, sizeof(buff), type, enumv, data, inlen, '\0');
1559
1560                 p = talloc_typed_strdup(ctx, buff);
1561         }
1562         break;
1563
1564         case PW_TYPE_IPV6_ADDR:
1565         case PW_TYPE_IPV6_PREFIX:
1566         {
1567                 char buff[INET6_ADDRSTRLEN + 4]; // + /prefix
1568
1569                 buff[0] = '\0';
1570                 value_data_prints(buff, sizeof(buff), type, enumv, data, inlen, '\0');
1571
1572                 p = talloc_typed_strdup(ctx, buff);
1573         }
1574         break;
1575
1576         case PW_TYPE_IFID:
1577                 p = talloc_typed_asprintf(ctx, "%x:%x:%x:%x",
1578                                           (data->ifid[0] << 8) | data->ifid[1],
1579                                           (data->ifid[2] << 8) | data->ifid[3],
1580                                           (data->ifid[4] << 8) | data->ifid[5],
1581                                           (data->ifid[6] << 8) | data->ifid[7]);
1582                 break;
1583
1584         case PW_TYPE_BOOLEAN:
1585                 p = talloc_typed_strdup(ctx, data->byte ? "yes" : "no");
1586                 break;
1587
1588         /*
1589          *      Don't add default here
1590          */
1591         case PW_TYPE_INVALID:
1592         case PW_TYPE_COMBO_IP_ADDR:
1593         case PW_TYPE_COMBO_IP_PREFIX:
1594         case PW_TYPE_TLV:
1595         case PW_TYPE_EXTENDED:
1596         case PW_TYPE_LONG_EXTENDED:
1597         case PW_TYPE_EVS:
1598         case PW_TYPE_VSA:
1599         case PW_TYPE_TIMEVAL:
1600         case PW_TYPE_MAX:
1601                 fr_assert(0);
1602                 return NULL;
1603         }
1604
1605         return p;
1606 }
1607
1608
1609 /** Print the value of an attribute to a string
1610  *
1611  * @note return value should be checked with is_truncated.
1612  * @note Will always \0 terminate unless outlen == 0.
1613  *
1614  * @param out Where to write the printed version of the attribute value.
1615  * @param outlen Length of the output buffer.
1616  * @param type of data being printed.
1617  * @param enumv Enumerated string values for integer types.
1618  * @param data to print.
1619  * @param inlen Length of data.
1620  * @param quote char to escape in string output.
1621  * @return  the number of bytes written to the out buffer, or a number >= outlen if truncation has occurred.
1622  */
1623 size_t value_data_prints(char *out, size_t outlen,
1624                          PW_TYPE type, DICT_ATTR const *enumv, value_data_t const *data,
1625                          ssize_t inlen, char quote)
1626 {
1627         DICT_VALUE      *v;
1628         char            buf[1024];      /* Interim buffer to use with poorly behaved printing functions */
1629         char const      *a = NULL;
1630         char            *p = out;
1631         time_t          t;
1632         struct tm       s_tm;
1633         unsigned int    i;
1634
1635         size_t          len = 0, freespace = outlen;
1636
1637         if (!data) return 0;
1638         if (outlen == 0) return inlen;
1639
1640         *out = '\0';
1641
1642         p = out;
1643
1644         switch (type) {
1645         case PW_TYPE_STRING:
1646
1647                 /*
1648                  *      Ensure that WE add the quotation marks around the string.
1649                  */
1650                 if (quote) {
1651                         if (freespace < 3) return inlen + 2;
1652
1653                         *p++ = quote;
1654                         freespace--;
1655
1656                         len = fr_prints(p, freespace, data->strvalue, inlen, quote);
1657                         /* always terminate the quoted string with another quote */
1658                         if (len >= (freespace - 1)) {
1659                                 /* Use out not p as we're operating on the entire buffer */
1660                                 out[outlen - 2] = (char) quote;
1661                                 out[outlen - 1] = '\0';
1662                                 return len + 2;
1663                         }
1664                         p += len;
1665                         freespace -= len;
1666
1667                         *p++ = (char) quote;
1668                         freespace--;
1669                         *p = '\0';
1670
1671                         return len + 2;
1672                 }
1673
1674                 return fr_prints(out, outlen, data->strvalue, inlen, quote);
1675
1676         case PW_TYPE_INTEGER:
1677                 i = data->integer;
1678                 goto print_int;
1679
1680         case PW_TYPE_SHORT:
1681                 i = data->ushort;
1682                 goto print_int;
1683
1684         case PW_TYPE_BYTE:
1685                 i = data->byte;
1686
1687 print_int:
1688                 /* Normal, non-tagged attribute */
1689                 if (enumv && (v = dict_valbyattr(enumv->attr, enumv->vendor, i)) != NULL) {
1690                         a = v->name;
1691                         len = strlen(a);
1692                 } else {
1693                         /* should never be truncated */
1694                         len = snprintf(buf, sizeof(buf), "%u", i);
1695                         a = buf;
1696                 }
1697                 break;
1698
1699         case PW_TYPE_INTEGER64:
1700                 return snprintf(out, outlen, "%" PRIu64, data->integer64);
1701
1702         case PW_TYPE_DATE:
1703                 t = data->date;
1704                 if (quote > 0) {
1705                         len = strftime(buf, sizeof(buf) - 1, "%%%b %e %Y %H:%M:%S %Z%%", localtime_r(&t, &s_tm));
1706                         buf[0] = (char) quote;
1707                         buf[len - 1] = (char) quote;
1708                         buf[len] = '\0';
1709                 } else {
1710                         len = strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z", localtime_r(&t, &s_tm));
1711                 }
1712                 a = buf;
1713                 break;
1714
1715         case PW_TYPE_SIGNED: /* Damned code for 1 WiMAX attribute */
1716                 len = snprintf(buf, sizeof(buf), "%d", data->sinteger);
1717                 a = buf;
1718                 break;
1719
1720         case PW_TYPE_IPV4_ADDR:
1721                 a = inet_ntop(AF_INET, &(data->ipaddr), buf, sizeof(buf));
1722                 len = strlen(buf);
1723                 break;
1724
1725         case PW_TYPE_ABINARY:
1726 #ifdef WITH_ASCEND_BINARY
1727                 print_abinary(buf, sizeof(buf), (uint8_t const *) data->filter, inlen, quote);
1728                 a = buf;
1729                 len = strlen(buf);
1730                 break;
1731 #else
1732         /* FALL THROUGH */
1733 #endif
1734         case PW_TYPE_OCTETS:
1735         case PW_TYPE_TLV:
1736         {
1737                 size_t max;
1738
1739                 /* Return the number of bytes we would have written */
1740                 len = (inlen * 2) + 2;
1741                 if (freespace <= 1) {
1742                         return len;
1743                 }
1744
1745                 *out++ = '0';
1746                 freespace--;
1747
1748                 if (freespace <= 1) {
1749                         *out = '\0';
1750                         return len;
1751                 }
1752                 *out++ = 'x';
1753                 freespace--;
1754
1755                 if (freespace <= 2) {
1756                         *out = '\0';
1757                         return len;
1758                 }
1759
1760                 /* Get maximum number of bytes we can encode given freespace */
1761                 max = ((freespace % 2) ? freespace - 1 : freespace - 2) / 2;
1762                 fr_bin2hex(out, data->octets, ((size_t)inlen > max) ? max : (size_t)inlen);
1763         }
1764                 return len;
1765
1766         case PW_TYPE_IFID:
1767                 a = ifid_ntoa(buf, sizeof(buf), data->ifid);
1768                 len = strlen(buf);
1769                 break;
1770
1771         case PW_TYPE_IPV6_ADDR:
1772                 a = inet_ntop(AF_INET6, &data->ipv6addr, buf, sizeof(buf));
1773                 len = strlen(buf);
1774                 break;
1775
1776         case PW_TYPE_IPV6_PREFIX:
1777         {
1778                 struct in6_addr addr;
1779
1780                 /*
1781                  *      Alignment issues.
1782                  */
1783                 memcpy(&addr, &(data->ipv6prefix[2]), sizeof(addr));
1784
1785                 a = inet_ntop(AF_INET6, &addr, buf, sizeof(buf));
1786                 if (a) {
1787                         p = buf;
1788
1789                         len = strlen(buf);
1790                         p += len;
1791                         len += snprintf(p, sizeof(buf) - len, "/%u", (unsigned int) data->ipv6prefix[1]);
1792                 }
1793         }
1794                 break;
1795
1796         case PW_TYPE_IPV4_PREFIX:
1797         {
1798                 struct in_addr addr;
1799
1800                 /*
1801                  *      Alignment issues.
1802                  */
1803                 memcpy(&addr, &(data->ipv4prefix[2]), sizeof(addr));
1804
1805                 a = inet_ntop(AF_INET, &addr, buf, sizeof(buf));
1806                 if (a) {
1807                         p = buf;
1808
1809                         len = strlen(buf);
1810                         p += len;
1811                         len += snprintf(p, sizeof(buf) - len, "/%u", (unsigned int) (data->ipv4prefix[1] & 0x3f));
1812                 }
1813         }
1814                 break;
1815
1816         case PW_TYPE_ETHERNET:
1817                 return snprintf(out, outlen, "%02x:%02x:%02x:%02x:%02x:%02x",
1818                                 data->ether[0], data->ether[1],
1819                                 data->ether[2], data->ether[3],
1820                                 data->ether[4], data->ether[5]);
1821
1822         /*
1823          *      Don't add default here
1824          */
1825         case PW_TYPE_INVALID:
1826         case PW_TYPE_COMBO_IP_ADDR:
1827         case PW_TYPE_COMBO_IP_PREFIX:
1828         case PW_TYPE_EXTENDED:
1829         case PW_TYPE_LONG_EXTENDED:
1830         case PW_TYPE_EVS:
1831         case PW_TYPE_VSA:
1832         case PW_TYPE_TIMEVAL:
1833         case PW_TYPE_BOOLEAN:
1834         case PW_TYPE_MAX:
1835                 fr_assert(0);
1836                 *out = '\0';
1837                 return 0;
1838         }
1839
1840         if (a) strlcpy(out, a, outlen);
1841
1842         return len;     /* Return the number of bytes we would of written (for truncation detection) */
1843 }
1844