The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
value.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Boxed value structures and functions to manipulate them
18 *
19 * @file src/lib/util/value.c
20 *
21 * There are three notional data formats used in the server:
22 *
23 * - #fr_value_box_t are the INTERNAL format. This is usually close to the in-memory representation
24 * of the data, though uint32s and IPs are always converted to/from octets with BIG ENDIAN
25 * uint8 ordering for consistency.
26 * - #fr_value_box_cast is used to convert (cast) #fr_value_box_t between INTERNAL formats.
27 * - #fr_value_box_strdup* is used to ingest nul terminated strings into the INTERNAL format.
28 * - #fr_value_box_memdup* is used to ingest binary data into the INTERNAL format.
29 *
30 * - NETWORK format is the format we send/receive on the wire. It is not a perfect representation
31 * of data packing for all protocols, so you will likely need to overload conversion for some types.
32 * - fr_value_box_to_network is used to convert INTERNAL format data to generic NETWORK format data.
33 * For uint32s, IP addresses etc... This means BIG ENDIAN uint8 ordering.
34 * - fr_value_box_from_network is used to convert packet buffer fragments in NETWORK format to
35 * INTERNAL format.
36 *
37 * - PRESENTATION format is what we print to the screen, and what we get from the user, databases
38 * and configuration files.
39 * - #fr_value_box_aprint is used to convert from INTERNAL to PRESENTATION format.
40 * - #fr_value_box_from_substr is used to convert from PRESENTATION to INTERNAL format.
41 *
42 * @copyright 2014-2017 The FreeRADIUS server project
43 * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
44 */
45RCSID("$Id: 48a7411335e7d813f1de7faa36d7d0121c631577 $")
46
47#define _VALUE_PRIVATE
48#include <freeradius-devel/util/value.h>
49#undef _VALUE_PRIVATE
50
51#include <freeradius-devel/util/atexit.h>
52#include <freeradius-devel/util/base16.h>
53#include <freeradius-devel/util/size.h>
54
55#include <math.h>
56#include <float.h>
57
58/** Sanity checks
59 *
60 * There should never be an instance where these fail.
61 */
62static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_ip.addr.v4.s_addr) == 4,
63 "in_addr.s_addr has unexpected length");
64static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_ip.addr.v6.s6_addr) == 16,
65 "in6_addr.s6_addr has unexpected length");
66static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_ifid) == 8,
67 "vb_ifid has unexpected length");
68static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_ether) == 6,
69 "vb_ether has unexpected length");
70
71static_assert(SIZEOF_MEMBER(fr_value_box_t, datum.boolean) == 1,
72 "datum.boolean has unexpected length");
73static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_uint8) == 1,
74 "vb_uint8 has unexpected length");
75static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_uint16) == 2,
76 "vb_uint16 has unexpected length");
77static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_uint32) == 4,
78 "vb_uint32 has unexpected length");
79static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_uint64) == 8,
80 "vb_uint64 has unexpected length");
81
82static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_int8) == 1,
83 "vb_int16 has unexpected length");
84static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_int16) == 2,
85 "vb_int16 has unexpected length");
86static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_int32) == 4,
87 "vb_int32 has unexpected length");
88static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_int64) == 8,
89 "vb_int64 has unexpected length");
90
91static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_float32) == 4,
92 "vb_float32 has unexpected length");
93static_assert(SIZEOF_MEMBER(fr_value_box_t, vb_float64) == 8,
94 "vb_float64 has unexpected length");
95
96/** How many bytes on-the-wire would a #fr_value_box_t value consume
97 *
98 * This is for the generic NETWORK format. For field sizes in the in-memory
99 * structure use #fr_value_box_field_sizes.
100 *
101 * @note Don't use this array directly when determining the length
102 * that would be consumed by the on-the-wire representation.
103 * Use #fr_value_box_network_length instead, as that deals with variable
104 * length attributes too.
105 */
106#define network_min_size(_x) (fr_value_box_network_sizes[_x][0])
107#define network_max_size(_x) (fr_value_box_network_sizes[_x][1])
108static size_t const fr_value_box_network_sizes[FR_TYPE_MAX + 1][2] = {
109 [FR_TYPE_NULL] = {~0, 0},
110
111 [FR_TYPE_STRING] = {0, ~0},
112 [FR_TYPE_OCTETS] = {0, ~0},
113
114 [FR_TYPE_IPV4_ADDR] = {4, 4},
115 [FR_TYPE_IPV4_PREFIX] = {5, 5},
116 [FR_TYPE_IPV6_ADDR] = {16, 17},
117 [FR_TYPE_IPV6_PREFIX] = {17, 18},
118 [FR_TYPE_COMBO_IP_ADDR] = {4, 17},
119 [FR_TYPE_COMBO_IP_PREFIX] = {16, 18},
120 [FR_TYPE_IFID] = {8, 8},
121 [FR_TYPE_ETHERNET] = {6, 6},
122
123 [FR_TYPE_BOOL] = {1, 1},
124 [FR_TYPE_UINT8] = {1, 1},
125 [FR_TYPE_UINT16] = {2, 2},
126 [FR_TYPE_UINT32] = {4, 4},
127 [FR_TYPE_UINT64] = {8, 8},
128
129 [FR_TYPE_INT8] = {1, 1},
130 [FR_TYPE_INT16] = {2, 2},
131 [FR_TYPE_INT32] = {4, 4},
132 [FR_TYPE_INT64] = {8, 8},
133
134 [FR_TYPE_FLOAT32] = {4, 4},
135 [FR_TYPE_FLOAT64] = {8, 8},
136
137 [FR_TYPE_DATE] = {2, 8}, //!< 2, 4, or 8 only
138 [FR_TYPE_TIME_DELTA] = {2, 8}, //!< 2, 4, or 8 only
139
140 [FR_TYPE_MAX] = {~0, 0} //!< Ensure array covers all types.
141};
142
143/** How many bytes wide each of the value data fields are
144 *
145 * This is useful when copying a value from a fr_value_box_t to a memory
146 * location passed as a void *.
147 */
148size_t const fr_value_box_field_sizes[] = {
151
160
161 [FR_TYPE_BOOL] = SIZEOF_MEMBER(fr_value_box_t, datum.boolean),
166
171
174
176
177 [FR_TYPE_TIME_DELTA] = SIZEOF_MEMBER(fr_value_box_t, datum.time_delta),
179
181
182 [FR_TYPE_MAX] = 0 //!< Ensure array covers all types.
183};
184
185/** Where the value starts in the #fr_value_box_t
186 *
187 */
188size_t const fr_value_box_offsets[] = {
191
198 [FR_TYPE_IFID] = offsetof(fr_value_box_t, vb_ifid),
200
201 [FR_TYPE_BOOL] = offsetof(fr_value_box_t, vb_bool),
206
207 [FR_TYPE_INT8] = offsetof(fr_value_box_t, vb_int8),
211
214
215 [FR_TYPE_DATE] = offsetof(fr_value_box_t, vb_date),
216
218 [FR_TYPE_SIZE] = offsetof(fr_value_box_t, vb_size),
219
220 [FR_TYPE_VALUE_BOX] = 0,
221
222 [FR_TYPE_MAX] = 0 //!< Ensure array covers all types.
223};
224
225static uint64_t const fr_value_box_integer_max[] = {
226 [FR_TYPE_BOOL] = true,
228 [FR_TYPE_UINT16] = UINT16_MAX,
229 [FR_TYPE_UINT32] = UINT32_MAX,
230 [FR_TYPE_UINT64] = UINT64_MAX,
231
232 [FR_TYPE_INT8] = INT8_MAX,
233 [FR_TYPE_INT16] = INT16_MAX,
234 [FR_TYPE_INT32] = INT32_MAX,
235 [FR_TYPE_INT64] = INT64_MAX,
236
237 [FR_TYPE_DATE] = UINT64_MAX,
238 [FR_TYPE_TIME_DELTA] = INT64_MAX,
239
240 [FR_TYPE_SIZE] = SIZE_MAX,
241
242 [FR_TYPE_MAX] = 0 //!< Ensure array covers all types.
243};
244
245static int64_t const fr_value_box_integer_min[] = {
246 [FR_TYPE_BOOL] = false,
247 [FR_TYPE_UINT8] = 0,
248 [FR_TYPE_UINT16] = 0,
249 [FR_TYPE_UINT32] = 0,
250 [FR_TYPE_UINT64] = 0,
251
252 [FR_TYPE_INT8] = INT8_MIN,
253 [FR_TYPE_INT16] = INT16_MIN,
254 [FR_TYPE_INT32] = INT32_MIN,
255 [FR_TYPE_INT64] = INT64_MIN,
256
257 [FR_TYPE_DATE] = 0,
258 [FR_TYPE_TIME_DELTA] = INT64_MIN,
259
260 [FR_TYPE_SIZE] = 0,
261
262 [FR_TYPE_MAX] = 0 //!< Ensure array covers all types.
263};
264
266 .name = "double",
267 .chr = '\\',
268 .subs = {
269 ['"'] = '"', /* Quoting char */
270 ['%'] = '%', /* xlat expansions */
271 ['\\'] = '\\',
272 ['a'] = '\a',
273 ['b'] = '\b',
274 ['e'] = '\\',
275 ['n'] = '\n',
276 ['r'] = '\r',
277 ['t'] = '\t',
278 ['v'] = '\v'
279 },
280 .do_hex = true,
281 .do_oct = true
282};
283
285 .name = "single",
286 .chr = '\\',
287 .subs = {
288 ['\''] = '\'', /* Quoting char */
289 ['\\'] = '\\'
290 },
291 .do_hex = false,
292 .do_oct = false
293};
294
296 .name = "solidus",
297 .chr = '\\',
298 .subs = {
299 ['%'] = '%', /* xlat expansions */
300 ['/'] = '/', /* Quoting char */
301 ['a'] = '\a',
302 ['b'] = '\b',
303 ['e'] = '\\',
304 ['n'] = '\n',
305 ['r'] = '\r',
306 ['t'] = '\t',
307 ['v'] = '\v'
308 },
309 .skip = {
310 ['\\'] = '\\' /* Leave this for the regex library */
311 },
312 .do_hex = true,
313 .do_oct = true
314};
315
317 .name = "backtick",
318 .chr = '\\',
319 .subs = {
320 ['%'] = '%', /* xlat expansions */
321 ['\\'] = '\\',
322 ['`'] = '`', /* Quoting char */
323 ['a'] = '\a',
324 ['b'] = '\b',
325 ['e'] = '\\',
326 ['n'] = '\n',
327 ['r'] = '\r',
328 ['t'] = '\t',
329 ['v'] = '\v'
330 },
331 .do_hex = true,
332 .do_oct = true
333};
334
341
348
350 .name = "double",
351 .chr = '\\',
352 .subs = {
353 ['"'] = '"', /* Quoting char */
354 ['%'] = '%', /* xlat expansions */
355 ['\\'] = '\\',
356 ['\a'] = 'a',
357 ['\b'] = 'b',
358 ['\n'] = 'n',
359 ['\r'] = 'r',
360 ['\t'] = 't',
361 ['\v'] = 'v'
362 },
363 .esc = {
366 },
367 .do_utf8 = true,
368 .do_oct = true
369};
370
371#ifdef __clang__
372#pragma clang diagnostic ignored "-Wgnu-designator"
373#endif
374
375/** Escape secret fields by simply mashing all data to '.'
376 *
377 * The length of the secret still leaks, but that is likely fine. Fixing that is more work.
378 *
379 */
381 .name = "secret",
382 .subs = {
383 [ 0 ... 255 ] = '.',
384 },
385};
386
388 .name = "single",
389 .chr = '\\',
390 .subs = {
391 ['\''] = '\'', /* Quoting char */
392 ['\\'] = '\\'
393 },
394 .do_utf8 = true,
395};
396
398 .name = "solidus",
399 .chr = '\\',
400 .subs = {
401 ['%'] = '%', /* xlat expansions */
402 ['/'] = '/', /* Quoting char */
403 ['\a'] = 'a',
404 ['\b'] = 'b',
405 ['\n'] = 'n',
406 ['\r'] = 'r',
407 ['\t'] = 't',
408 ['\v'] = 'v'
409 },
410 .esc = {
413 },
414 .do_utf8 = true,
415 .do_oct = true
416};
417
419 .name = "backtick",
420 .chr = '\\',
421 .subs = {
422 ['%'] = '%', /* xlat expansions */
423 ['\\'] = '\\',
424 ['`'] = '`', /* Quoting char */
425 ['\a'] = 'a',
426 ['\b'] = 'b',
427 ['\n'] = 'n',
428 ['\r'] = 'r',
429 ['\t'] = 't',
430 ['\v'] = 'v'
431 },
432 .esc = {
435 },
436 .do_utf8 = true,
437 .do_oct = true
438};
439
446
453
455 .name = "unprintables",
456 .chr = '\\',
457 .subs = {
458 ['\\'] = '\\',
459 },
460 .esc = {
463 },
464 .do_utf8 = true,
465 .do_oct = true
466};
467
468
469/** @name Produce a #tmpl_t from a string or substring
470 *
471 * @{
472 */
473
474/* clang-format off */
475/** Default formatting rules
476 *
477 * Control token termination, escaping and how the tmpl is printed.
478 */
479fr_sbuff_parse_rules_t const value_parse_rules_bareword_unquoted = {
480
481};
482
483fr_sbuff_parse_rules_t const value_parse_rules_double_unquoted = {
484 .escapes = &fr_value_unescape_double
485};
486
487fr_sbuff_parse_rules_t const value_parse_rules_single_unquoted = {
488 .escapes = &fr_value_unescape_single
489};
490
491fr_sbuff_parse_rules_t const value_parse_rules_solidus_unquoted = {
492 .escapes = &fr_value_unescape_solidus
493};
494
495fr_sbuff_parse_rules_t const value_parse_rules_backtick_unquoted = {
497};
498
499/** Parse rules for non-quoted strings
500 *
501 * These parse rules should be used for processing escape sequences in
502 * data from external data sources like SQL databases and REST APIs.
503 *
504 * They do not include terminals to stop parsing as it assumes the values
505 * are discrete, and not wrapped in quotes.
506 */
514
522
523fr_sbuff_parse_rules_t const value_parse_rules_bareword_quoted = {
524 .escapes = &(fr_sbuff_unescape_rules_t){
525 .chr = '\\',
526 /*
527 * Allow barewords to contain whitespace
528 * if they're escaped.
529 */
530 .subs = {
531 ['\t'] = '\t',
532 ['\n'] = '\n',
533 [' '] = ' '
534 },
535 .do_hex = false,
536 .do_oct = false
537 },
538 .terminals = &FR_SBUFF_TERMS(
539 L(""),
540 L("\t"),
541 L("\n"),
542 L(" ")
543 )
544};
545
546fr_sbuff_parse_rules_t const value_parse_rules_double_quoted = {
547 .escapes = &fr_value_unescape_double,
548 .terminals = &FR_SBUFF_TERMS(
549 L(""), L("\n"), L("\r"), L("\""))
550};
551
552fr_sbuff_parse_rules_t const value_parse_rules_single_quoted = {
553 .escapes = &fr_value_unescape_single,
554 .terminals = &FR_SBUFF_TERMS(
555 L(""), L("\n"), L("\r"), L("'"))
556};
557
558fr_sbuff_parse_rules_t const value_parse_rules_solidus_quoted = {
559 .escapes = &fr_value_unescape_solidus,
560 .terminals = &FR_SBUFF_TERMS(
561 L(""), L("\n"), L("\r"), L("/"))
562};
563
564fr_sbuff_parse_rules_t const value_parse_rules_backtick_quoted = {
565 .escapes = &fr_value_unescape_backtick,
566 .terminals = &FR_SBUFF_TERMS(
567 L(""), L("\n"), L("\r"), L("`"))
568};
569
570/*
571 * And triple-quoted versions of the above.
572 */
573fr_sbuff_parse_rules_t const value_parse_rules_double_3quoted = {
574 .escapes = &fr_value_unescape_double,
575 .terminals = &FR_SBUFF_TERMS(
576 L(""), L("\n"), L("\r"), L("\"\"\""))
577};
578
579fr_sbuff_parse_rules_t const value_parse_rules_single_3quoted = {
580 .escapes = &fr_value_unescape_single,
581 .terminals = &FR_SBUFF_TERMS(
582 L(""), L("\n"), L("\r"), L("'''"))
583};
584
585fr_sbuff_parse_rules_t const value_parse_rules_solidus_3quoted = {
586 .escapes = &fr_value_unescape_solidus,
587 .terminals = &FR_SBUFF_TERMS(
588 L(""), L("\n"), L("\r"), L("///"))
589};
590
591fr_sbuff_parse_rules_t const value_parse_rules_backtick_3quoted = {
592 .escapes = &fr_value_unescape_backtick,
593 .terminals = &FR_SBUFF_TERMS(
594 L(""), L("\n"), L("\r"), L("```"))
595};
596
597/** Parse rules for quoted strings
598 *
599 * These parse rules should be used for internal parsing functions that
600 * are working with configuration files.
601 *
602 * They include appropriate quote terminals to force functions parsing
603 * quoted strings to return when they reach a quote character.
604 */
612
620
628
629/* clang-format on */
630/** @} */
631
632/** Copy flags and type data from one value box to another
633 *
634 * @param[in] dst to copy flags to
635 * @param[in] src of data.
636 */
637static inline void fr_value_box_copy_meta(fr_value_box_t *dst, fr_value_box_t const *src)
638{
639 switch (src->type) {
641 dst->vb_length = src->vb_length;
642 break;
643 /*
644 * Not 100% sure this should be done here
645 * but if the intent is to make a null
646 * box usable, then we need to do this
647 * somewhere.
648 */
649 case FR_TYPE_GROUP:
650 fr_value_box_list_init(&dst->vb_group);
651 break;
652
653 default:
654 break;
655 }
656
657 dst->enumv = src->enumv;
658 dst->type = src->type;
659 dst->tainted = src->tainted;
660 dst->safe_for = src->safe_for;
661 dst->secret = src->secret;
662 fr_value_box_list_entry_init(dst);
663}
664
665/** Compare two floating point numbers for equality.
666 *
667 * We're not _quite_ supposed to use DBL_EPSILON here, and are instead supposed to choose our own epsilon.
668 * But this is good enough for most purposed.
669 */
670static int8_t float_cmp(double a, double b)
671{
672 double sum, diff;
673
674 /*
675 * Handles the best cast scenario.
676 */
677 if (a == b) return 0;
678
679 diff = fabs(a - b);
680
681 /*
682 * One of the numbers is zero. The other might be close to zero, in which case it might as well
683 * be zero.
684 *
685 * Otherwise, the non-zero number is far from zero, and we can just compare them.
686 */
687 if ((fpclassify(a) == FP_ZERO) || (fpclassify(b) == FP_ZERO)) {
688 check:
689 if (diff < DBL_EPSILON) return 0;
690
691 return CMP(a, b);
692 }
693
694 /*
695 * Get the rough scale of the two numbers.
696 */
697 sum = fabs(a) + fabs(b);
698
699 /*
700 * The two numbers are not zero, but both are close to it.
701 */
702 if (sum < DBL_MIN) goto check;
703
704 /*
705 * Get the relative differences. This check also handles overflow of sum.
706 */
707 if ((diff / fmin(sum, DBL_MAX)) < DBL_EPSILON) return 0;
708
709 return CMP(a, b);
710}
711
712/** Compare two values
713 *
714 * @param[in] a Value to compare.
715 * @param[in] b Value to compare.
716 * @return
717 * - -1 if a is less than b.
718 * - 0 if both are equal.
719 * - 1 if a is more than b.
720 * - < -1 on failure.
721 */
723{
724 if (!fr_cond_assert(a->type != FR_TYPE_NULL)) return -1;
725 if (!fr_cond_assert(b->type != FR_TYPE_NULL)) return -1;
726
727 if (a->type != b->type) {
728 fr_strerror_printf("%s: Can't compare values of different types", __FUNCTION__);
729 return -2;
730 }
731
732 /*
733 * After doing the previous check for special comparisons,
734 * do the per-type comparison here.
735 */
736 switch (a->type) {
738 {
739 size_t length;
740
741 if (a->vb_length < b->vb_length) {
742 length = a->vb_length;
743 } else {
744 length = b->vb_length;
745 }
746
747 if (length) {
748 int cmp;
749
750 /*
751 * Use constant-time comparisons for secret values.
752 */
753 if (a->secret || b->secret) {
754 cmp = fr_digest_cmp(a->datum.ptr, b->datum.ptr, length);
755 } else {
756 cmp = memcmp(a->datum.ptr, b->datum.ptr, length);
757 }
758 if (cmp != 0) return CMP(cmp, 0);
759 }
760
761 /*
762 * Contents are the same. The return code
763 * is therefore the difference in lengths.
764 *
765 * i.e. "0x00" is smaller than "0x0000"
766 */
767 return CMP(a->vb_length, b->vb_length);
768 }
769
770 /*
771 * Short-hand for simplicity.
772 */
773#define RETURN(_type) return CMP(a->datum._type, b->datum._type)
774#define COMPARE(_type) return CMP(memcmp(&a->datum._type, &b->datum._type, sizeof(a->datum._type)), 0)
775
776 case FR_TYPE_BOOL:
777 RETURN(boolean);
778
779 case FR_TYPE_DATE:
780 return fr_unix_time_cmp(a->datum.date, b->datum.date);
781
782 case FR_TYPE_UINT8:
783 RETURN(uint8);
784
785 case FR_TYPE_UINT16:
786 RETURN(uint16);
787
788 case FR_TYPE_UINT32:
789 RETURN(uint32);
790
791 case FR_TYPE_UINT64:
792 RETURN(uint64);
793
794 case FR_TYPE_INT8:
795 RETURN(int8);
796
797 case FR_TYPE_INT16:
798 RETURN(int16);
799
800 case FR_TYPE_INT32:
801 RETURN(int32);
802
803 case FR_TYPE_INT64:
804 RETURN(int64);
805
806 case FR_TYPE_SIZE:
807 RETURN(size);
808
810 return fr_time_delta_cmp(a->datum.time_delta, b->datum.time_delta);
811
812 case FR_TYPE_FLOAT32:
813 return float_cmp(a->vb_float32, b->vb_float32);
814
815 case FR_TYPE_FLOAT64:
816 return float_cmp(a->vb_float64, b->vb_float64);
817
818 case FR_TYPE_ETHERNET:
819 COMPARE(ether);
820
827 return fr_ipaddr_cmp(&a->vb_ip, &b->vb_ip);
828
829 case FR_TYPE_IFID:
830 COMPARE(ifid);
831
832 /*
833 * These should be handled at some point
834 */
835 case FR_TYPE_NON_LEAF:
836 (void)fr_cond_assert(0); /* unknown type */
837 return -2;
838
839 /*
840 * Do NOT add a default here, as new types are added
841 * static analysis will warn us they're not handled
842 */
843 }
844 return 0;
845}
846
847/*
848 * We leverage the fact that IPv4 and IPv6 prefixes both
849 * have the same format:
850 *
851 * reserved, prefix-len, data...
852 */
853static int fr_value_box_cidr_cmp_op(fr_token_t op, int bytes,
854 uint8_t a_net, uint8_t const *a,
855 uint8_t b_net, uint8_t const *b)
856{
857 int i, common;
859
860 /*
861 * Handle the case of netmasks being identical.
862 */
863 if (a_net == b_net) {
864 int compare;
865
866 compare = memcmp(a, b, bytes);
867
868 /*
869 * If they're identical return true for
870 * identical.
871 */
872 if ((compare == 0) &&
873 ((op == T_OP_CMP_EQ) ||
874 (op == T_OP_LE) ||
875 (op == T_OP_GE))) {
876 return true;
877 }
878
879 /*
880 * Everything else returns false.
881 *
882 * 10/8 == 24/8 --> false
883 * 10/8 <= 24/8 --> false
884 * 10/8 >= 24/8 --> false
885 */
886 return false;
887 }
888
889 /*
890 * Netmasks are different. That limits the
891 * possible results, based on the operator.
892 */
893 switch (op) {
894 case T_OP_CMP_EQ:
895 return false;
896
897 case T_OP_NE:
898 return true;
899
900 case T_OP_LE:
901 case T_OP_LT: /* 192/8 < 192.168/16 --> false */
902 if (a_net < b_net) {
903 return false;
904 }
905 break;
906
907 case T_OP_GE:
908 case T_OP_GT: /* 192/16 > 192.168/8 --> false */
909 if (a_net > b_net) {
910 return false;
911 }
912 break;
913
914 default:
915 return false;
916 }
917
918 if (a_net < b_net) {
919 common = a_net;
920 } else {
921 common = b_net;
922 }
923
924 /*
925 * Do the check uint8 by uint8. If the bytes are
926 * identical, it MAY be a match. If they're different,
927 * it is NOT a match.
928 */
929 i = 0;
930 while (i < bytes) {
931 /*
932 * All leading bytes are identical.
933 */
934 if (common == 0) return true;
935
936 /*
937 * Doing bitmasks takes more work.
938 */
939 if (common < 8) break;
940
941 if (a[i] != b[i]) return false;
942
943 common -= 8;
944 i++;
945 continue;
946 }
947
948 mask = 1;
949 mask <<= (8 - common);
950 mask--;
951 mask = ~mask;
952
953 if ((a[i] & mask) == ((b[i] & mask))) {
954 return true;
955 }
956
957 return false;
958}
959
960/*
961 * So we don't have to include <util/regex.h> in a recursive fashion.
962 */
963extern int fr_regex_cmp_op(fr_token_t op, fr_value_box_t const *a, fr_value_box_t const *b);
964
965/** Compare two attributes using an operator
966 *
967 * @param[in] op to use in comparison.
968 * @param[in] a Value to compare.
969 * @param[in] b Value to compare.
970 * @return
971 * - 1 if true
972 * - 0 if false
973 * - -1 on failure.
974 */
976{
977 int compare = 0;
978
979 if (!fr_cond_assert(a->type != FR_TYPE_NULL)) return -1;
980 if (!fr_cond_assert(b->type != FR_TYPE_NULL)) return -1;
981
982 if (unlikely((op == T_OP_REG_EQ) || (op == T_OP_REG_NE))) return fr_regex_cmp_op(op, a, b);
983
984 switch (a->type) {
986 switch (b->type) {
988 if (b->vb_ip.af != AF_INET) goto fail_cmp_v4;
990
991 case FR_TYPE_IPV4_ADDR: /* IPv4 and IPv4 */
992 goto cmp;
993
995 if (b->vb_ip.af != AF_INET) goto fail_cmp_v4;
997
998 case FR_TYPE_IPV4_PREFIX: /* IPv4 and IPv4 Prefix */
999 return fr_value_box_cidr_cmp_op(op, 4, 32, (uint8_t const *) &a->vb_ip.addr.v4.s_addr,
1000 b->vb_ip.prefix, (uint8_t const *) &b->vb_ip.addr.v4.s_addr);
1001
1002 default:
1003 fail_cmp_v4:
1004 fr_strerror_const("Cannot compare IPv4 with IPv6 address");
1005 return -1;
1006 }
1007
1008 case FR_TYPE_IPV4_PREFIX: /* IPv4 and IPv4 Prefix */
1009 cmp_prefix_v4:
1010 switch (b->type) {
1012 if (b->vb_ip.af != AF_INET) goto fail_cmp_v4;
1014
1015 case FR_TYPE_IPV4_ADDR:
1016 return fr_value_box_cidr_cmp_op(op, 4, a->vb_ip.prefix,
1017 (uint8_t const *) &a->vb_ip.addr.v4.s_addr,
1018 32, (uint8_t const *) &b->vb_ip.addr.v4);
1019
1021 if (b->vb_ip.af != AF_INET) goto fail_cmp_v4;
1023
1024 case FR_TYPE_IPV4_PREFIX: /* IPv4 Prefix and IPv4 Prefix */
1025 return fr_value_box_cidr_cmp_op(op, 4, a->vb_ip.prefix,
1026 (uint8_t const *) &a->vb_ip.addr.v4.s_addr,
1027 b->vb_ip.prefix, (uint8_t const *) &b->vb_ip.addr.v4.s_addr);
1028
1029 default:
1030 fr_strerror_const("Cannot compare IPv4 with IPv6 address");
1031 return -1;
1032 }
1033
1034 case FR_TYPE_IPV6_ADDR:
1035 switch (b->type) {
1037 if (b->vb_ip.af != AF_INET6) goto fail_cmp_v6;
1039
1040 case FR_TYPE_IPV6_ADDR: /* IPv6 and IPv6 */
1041 goto cmp;
1042
1044 if (b->vb_ip.af != AF_INET6) goto fail_cmp_v6;
1046
1047 case FR_TYPE_IPV6_PREFIX: /* IPv6 and IPv6 Preifx */
1048 return fr_value_box_cidr_cmp_op(op, 16, 128, (uint8_t const *) &a->vb_ip.addr.v6,
1049 b->vb_ip.prefix, (uint8_t const *) &b->vb_ip.addr.v6);
1050
1051 default:
1052 fail_cmp_v6:
1053 fr_strerror_const("Cannot compare IPv6 with IPv4 address");
1054 return -1;
1055 }
1056
1058 cmp_prefix_v6:
1059 switch (b->type) {
1061 if (b->vb_ip.af != AF_INET6) goto fail_cmp_v6;
1063
1064 case FR_TYPE_IPV6_ADDR: /* IPv6 Prefix and IPv6 */
1065 return fr_value_box_cidr_cmp_op(op, 16, a->vb_ip.prefix,
1066 (uint8_t const *) &a->vb_ip.addr.v6,
1067 128, (uint8_t const *) &b->vb_ip.addr.v6);
1068
1070 if (b->vb_ip.af != AF_INET6) goto fail_cmp_v6;
1072
1073 case FR_TYPE_IPV6_PREFIX: /* IPv6 Prefix and IPv6 */
1074 return fr_value_box_cidr_cmp_op(op, 16, a->vb_ip.prefix,
1075 (uint8_t const *) &a->vb_ip.addr.v6,
1076 b->vb_ip.prefix, (uint8_t const *) &b->vb_ip.addr.v6);
1077
1078 default:
1079 fr_strerror_const("Cannot compare IPv6 with IPv4 address");
1080 return -1;
1081 }
1082
1084 if (a->vb_ip.af != b->vb_ip.af) goto fail_cmp_v4; /* as good as any */
1085
1086 goto cmp;
1087
1089 if (a->vb_ip.af != b->vb_ip.af) goto fail_cmp_v4; /* as good as any */
1090
1091 if (a->vb_ip.af == AF_INET) goto cmp_prefix_v4;
1092
1093 goto cmp_prefix_v6;
1094
1095 default:
1096 cmp:
1097 compare = fr_value_box_cmp(a, b);
1098 if (compare < -1) { /* comparison error */
1099 return -1;
1100 }
1101 }
1102
1103 /*
1104 * Now do the operator comparison.
1105 */
1106 switch (op) {
1107 case T_OP_CMP_EQ:
1108 return (compare == 0);
1109
1110 case T_OP_NE:
1111 return (compare != 0);
1112
1113 case T_OP_LT:
1114 return (compare < 0);
1115
1116 case T_OP_GT:
1117 return (compare > 0);
1118
1119 case T_OP_LE:
1120 return (compare <= 0);
1121
1122 case T_OP_GE:
1123 return (compare >= 0);
1124
1125 default:
1126 return 0;
1127 }
1128}
1129
1130/** Convert a string value with escape sequences into its binary form
1131 *
1132 * The quote character determines the escape sequences recognised.
1133 *
1134 * - Literal mode ("'" quote char) will unescape:
1135 @verbatim
1136 - \\ - Literal backslash.
1137 - <quote> - The quotation char.
1138 @endverbatim
1139 * - Expanded mode ('"' quote char) will also unescape:
1140 @verbatim
1141 - \a - Alert.
1142 - \b - Backspace.
1143 - \e - Escape character i.e. (\‍)
1144 - \r - Carriage return.
1145 - \n - Newline.
1146 - \t - Tab.
1147 - \v - Vertical tab
1148 - <oct> - An octal escape sequence.
1149 - \x<hex> - A hex escape sequence.
1150 @endverbatim
1151 * - Backtick mode ('`' quote char) identical to expanded mode.
1152 * - Regex mode ('/') identical to expanded mode but two successive
1153 * backslashes will be interpreted as an escape sequence, but not
1154 * unescaped, so that they will be passed to the underlying regex
1155 * library.
1156 * - Verbatim mode ('\0' quote char) copies in to out verbatim.
1157 *
1158 * @note The resulting output may contain embedded \0s.
1159 * @note Unrecognised escape sequences will be copied verbatim.
1160 * @note In and out may point to the same underlying buffer.
1161 * @note Copying will stop early if an unescaped instance of the
1162 * quoting char is found in the input buffer.
1163 *
1164 * @param[out] out Where to write the unescaped string.
1165 * @param[in] in The string to unescape.
1166 * @param[in] inlen Length of input string. Pass SIZE_MAX to copy all data
1167 * in the input buffer.
1168 * @param[in] quote Character around the string, determines unescaping mode.
1169 *
1170 * @return
1171 * - 0 if input string was empty.
1172 * - >0 the number of bytes written to out.
1173 */
1175{
1176 switch (quote) {
1177 default:
1178 break;
1179
1180 case '"':
1181 {
1183 }
1184 case '\'':
1185 {
1187 }
1188
1189 case '`':
1190 {
1192 }
1193
1194 case '/':
1195 {
1197 }
1198 }
1199
1201}
1202
1203/** Convert a string value with escape sequences into its binary form
1204 *
1205 * The quote character determines the escape sequences recognised.
1206 *
1207 * - Literal mode ("'" quote char) will unescape:
1208 @verbatim
1209 - \\ - Literal backslash.
1210 - <quote> - The quotation char.
1211 @endverbatim
1212 * - Expanded mode ('"' quote char) will also unescape:
1213 @verbatim
1214 - \a - Alert.
1215 - \b - Backspace.
1216 - \e - Escape character i.e. (\‍)
1217 - \r - Carriage return.
1218 - \n - Newline.
1219 - \t - Tab.
1220 - \v - Vertical tab
1221 - <oct> - An octal escape sequence.
1222 - \x<hex> - A hex escape sequence.
1223 @endverbatim
1224 * - Backtick mode ('`' quote char) identical to expanded mode.
1225 * - Regex mode ('/') identical to expanded mode but two successive
1226 * backslashes will be interpreted as an escape sequence, but not
1227 * unescaped, so that they will be passed to the underlying regex
1228 * library.
1229 * - Verbatim mode ('\0' quote char) copies in to out verbatim.
1230 *
1231 * @note The resulting output may contain embedded \0s.
1232 * @note Unrecognised escape sequences will be copied verbatim.
1233 * @note In and out may point to the same underlying buffer.
1234 * @note Copying will stop early if an unescaped instance of the
1235 * quoting char is found in the input buffer.
1236 *
1237 * @param[out] out Where to write the unescaped string.
1238 * @param[in] in The string to unescape.
1239 * @param[in] inlen Length of input string. Pass SIZE_MAX to copy all data
1240 * in the input buffer.
1241 * @param[in] quote Character around the string, determines unescaping mode.
1242 *
1243 * @return
1244 * - 0 if input string was empty.
1245 * - >0 the number of bytes written to out.
1246 */
1248{
1249 switch (quote) {
1250 default:
1251 break;
1252
1253 case '"':
1255
1256 case '\'':
1258
1259 case '`':
1261
1262 case '/':
1264 }
1265
1267}
1268
1269/** Performs byte order reversal for types that need it
1270 *
1271 * @param[in] dst Where to write the result. May be the same as src.
1272 * @param[in] src #fr_value_box_t containing an uint32 value.
1273 * @return
1274 * - 0 on success.
1275 * - -1 on failure.
1276 */
1278{
1279 if (!fr_cond_assert(src->type != FR_TYPE_NULL)) return -1;
1280
1281 switch (src->type) {
1282 default:
1283 break;
1284
1285 case FR_TYPE_BOOL:
1286 case FR_TYPE_UINT8:
1287 case FR_TYPE_INT8:
1288 case FR_TYPE_IPV4_ADDR:
1290 case FR_TYPE_IPV6_ADDR:
1294 case FR_TYPE_IFID:
1295 case FR_TYPE_ETHERNET:
1296 case FR_TYPE_SIZE:
1297 fr_value_box_copy(NULL, dst, src);
1298 return 0;
1299
1300 case FR_TYPE_OCTETS:
1301 case FR_TYPE_STRING:
1302 case FR_TYPE_NON_LEAF:
1303 fr_assert_fail(NULL);
1304 return -1; /* shouldn't happen */
1305 }
1306
1307 /*
1308 * If we're not just flipping in place
1309 * initialise the destination box
1310 * with similar meta data as the src.
1311 *
1312 * Don't use the copy meta data function
1313 * here as that doesn't initialise the
1314 * destination box.
1315 */
1316 if (dst != src) fr_value_box_init(dst, src->type, src->enumv, src->tainted);
1317
1318 switch (src->type) {
1319 case FR_TYPE_UINT16:
1320 dst->vb_uint16 = htons(src->vb_uint16);
1321 break;
1322
1323 case FR_TYPE_UINT32:
1324 dst->vb_uint32 = htonl(src->vb_uint32);
1325 break;
1326
1327 case FR_TYPE_UINT64:
1328 dst->vb_uint64 = htonll(src->vb_uint64);
1329 break;
1330
1331 case FR_TYPE_INT16:
1332 dst->vb_int16 = htons(src->vb_int16);
1333 break;
1334
1335 case FR_TYPE_INT32:
1336 dst->vb_int32 = htonl(src->vb_int32);
1337 break;
1338
1339 case FR_TYPE_INT64:
1340 dst->vb_int64 = htonll(src->vb_int64);
1341 break;
1342
1343 case FR_TYPE_DATE:
1344 dst->vb_date = fr_unix_time_wrap(htonll(fr_unix_time_unwrap(src->vb_date)));
1345 break;
1346
1347 case FR_TYPE_TIME_DELTA:
1348 dst->vb_time_delta = fr_time_delta_wrap(htonll(fr_time_delta_unwrap(src->vb_time_delta)));
1349 break;
1350
1351 case FR_TYPE_FLOAT32:
1352 dst->vb_float32 = htonl((uint32_t)src->vb_float32);
1353 break;
1354
1355 case FR_TYPE_FLOAT64:
1356 dst->vb_float64 = htonll((uint64_t)src->vb_float64);
1357 break;
1358
1359 default:
1360 fr_assert_fail(NULL);
1361 return -1; /* shouldn't happen */
1362 }
1363
1364 return 0;
1365}
1366
1367/** Get the size of the value held by the fr_value_box_t
1368 *
1369 * This is the length of the NETWORK presentation
1370 */
1372{
1373 switch (value->type) {
1375 if (value->enumv) {
1376 /*
1377 * Fixed-width fields.
1378 */
1379 if (value->enumv->flags.length) {
1380 return value->enumv->flags.length;
1381 }
1382
1383 /*
1384 * Clamp length at maximum we're allowed to encode.
1385 */
1386 if (da_is_length_field(value->enumv)) {
1387 if (value->enumv->flags.subtype == FLAG_LENGTH_UINT8) {
1388 if (value->vb_length > 255) return 255;
1389
1390 } else if (value->enumv->flags.subtype == FLAG_LENGTH_UINT16) {
1391 if (value->vb_length > 65535) return 65535;
1392 }
1393 }
1394 }
1395 return value->vb_length;
1396
1397 /*
1398 * These can have different encodings, depending on the underlying protocol.
1399 */
1400 case FR_TYPE_DATE:
1401 case FR_TYPE_TIME_DELTA:
1402 if (value->enumv) return value->enumv->flags.length;
1404
1405 default:
1406 return network_min_size(value->type);
1407 }
1408}
1409
1410/** Encode a single value box, serializing its contents in generic network format
1411 *
1412 * The serialized form of #fr_value_box_t may not match the requirements of your protocol
1413 * completely. In cases where they do not, you should overload specific types in the
1414 * function calling #fr_value_box_to_network.
1415 *
1416 * The general serialization rules are:
1417 *
1418 * - Octets are encoded in binary form (not hex).
1419 * - Strings are encoded without the trailing \0 byte.
1420 * - Integers are encoded big-endian.
1421 * - Bools are encoded using one byte, with value 0x00 (false) or 0x01 (true).
1422 * - Signed integers are encoded two's complement, with the MSB as the sign bit.
1423 * Byte order is big-endian.
1424 * - Network addresses are encoded big-endian.
1425 * - IPv4 prefixes are encoded with 1 byte for the prefix, then 4 bytes of address.
1426 * - IPv6 prefixes are encoded with 1 byte for the scope_id, 1 byte for the prefix,
1427 * and 16 bytes of address.
1428 * - Floats are encoded in IEEE-754 format with a big-endian byte order. We rely
1429 * on the fact that the C standards require floats to be represented in IEEE-754
1430 * format in memory.
1431 * - Dates are encoded as 16/32/64-bit unsigned UNIX timestamps.
1432 * - time_deltas are encoded as 16/32/64-bit signed integers.
1433 *
1434 * #FR_TYPE_SIZE is not encodable, as it is system specific.
1435 *
1436 * This function will not encode structural types (TLVs, VSAs etc...). These are usually
1437 * specific to the protocol anyway.
1438 *
1439 * All of the dictionary rules are respected. string/octets can have
1440 * a fixed length (which is zero-padded if necessary), or can have an
1441 * 8/16-bit "length" prefix.
1442 *
1443 * @param[out] dbuff Where to write serialized data.
1444 * @param[in] value to encode.
1445 * @return
1446 * - 0 no bytes were written.
1447 * - >0 the number of bytes written to out.
1448 * - <0 the number of bytes we'd need in dbuff to complete the operation.
1449 */
1451{
1452 size_t min, max;
1453 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
1454
1455 /*
1456 * We cannot encode structural types here.
1457 */
1458 if (!fr_type_is_leaf(value->type)) {
1459 unsupported:
1460 fr_strerror_printf("%s: Cannot encode type \"%s\"",
1461 __FUNCTION__,
1462 fr_type_to_str(value->type));
1464 }
1465
1466 /*
1467 * Variable length types
1468 */
1469 switch (value->type) {
1470 case FR_TYPE_OCTETS:
1471 case FR_TYPE_STRING:
1472 max = value->vb_length;
1473
1474 /*
1475 * Sometimes variable length *inside* the server
1476 * has maximum length on the wire.
1477 */
1478 if (value->enumv) {
1479 if (value->enumv->flags.length) {
1480 /*
1481 * The field is fixed size, and the data is smaller than that, We zero-pad the field.
1482 */
1483 if (max < value->enumv->flags.length) {
1484 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, (uint8_t const *)value->datum.ptr, max);
1485 FR_DBUFF_MEMSET_RETURN(&work_dbuff, 0, value->enumv->flags.length - max);
1486 return fr_dbuff_set(dbuff, &work_dbuff);
1487
1488 } else if (max > value->enumv->flags.length) {
1489 /*
1490 * Truncate the input to the maximum allowed length.
1491 */
1492 max = value->enumv->flags.length;
1493 }
1494
1495 } else if (da_is_length_field(value->enumv)) {
1496 /*
1497 * Truncate the output to the max allowed for this field and encode the length.
1498 */
1499 if (value->enumv->flags.subtype == FLAG_LENGTH_UINT8) {
1500 if (max > 255) max = 255;
1501 FR_DBUFF_IN_RETURN(&work_dbuff, (uint8_t) max);
1502
1503 } else if (value->enumv->flags.subtype == FLAG_LENGTH_UINT16) {
1504 if (max > 65536) max = 65535;
1505 FR_DBUFF_IN_RETURN(&work_dbuff, (uint16_t) max);
1506
1507 } else {
1508 return -1;
1509 }
1510 }
1511 }
1512
1513 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, (uint8_t const *)value->datum.ptr, max);
1514 return fr_dbuff_set(dbuff, &work_dbuff);
1515
1516 /*
1517 * The data can be encoded in a variety of widths.
1518 */
1519 case FR_TYPE_DATE:
1520 case FR_TYPE_TIME_DELTA:
1521 if (value->enumv) {
1522 min = value->enumv->flags.length;
1523 } else {
1524 min = 4;
1525 }
1526 break;
1527
1528 default:
1529 min = network_min_size(value->type);
1530 break;
1531 }
1532
1533 /*
1534 * We have to encode actual data here.
1535 */
1536 fr_assert(min > 0);
1537
1538 switch (value->type) {
1539 case FR_TYPE_IPV4_ADDR:
1540 ipv4addr:
1541 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff,
1542 (uint8_t const *)&value->vb_ip.addr.v4.s_addr,
1543 sizeof(value->vb_ip.addr.v4.s_addr));
1544 break;
1545 /*
1546 * Needs special mangling
1547 */
1549 ipv4prefix:
1550 FR_DBUFF_IN_RETURN(&work_dbuff, value->vb_ip.prefix);
1551 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff,
1552 (uint8_t const *)&value->vb_ip.addr.v4.s_addr,
1553 sizeof(value->vb_ip.addr.v4.s_addr));
1554 break;
1555
1556 case FR_TYPE_IPV6_ADDR:
1557 ipv6addr:
1558 if (value->vb_ip.scope_id > 0) FR_DBUFF_IN_RETURN(&work_dbuff, value->vb_ip.scope_id);
1559 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, value->vb_ip.addr.v6.s6_addr, sizeof(value->vb_ip.addr.v6.s6_addr));
1560 break;
1561
1563 ipv6prefix:
1564 if (value->vb_ip.scope_id > 0) FR_DBUFF_IN_RETURN(&work_dbuff, value->vb_ip.scope_id);
1565 FR_DBUFF_IN_RETURN(&work_dbuff, value->vb_ip.prefix);
1566 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, value->vb_ip.addr.v6.s6_addr, sizeof(value->vb_ip.addr.v6.s6_addr));
1567 break;
1568
1569 case FR_TYPE_BOOL:
1570 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, value->datum.boolean);
1571 break;
1572
1574 switch (value->vb_ip.af) {
1575 case AF_INET:
1576 goto ipv4addr;
1577
1578 case AF_INET6:
1579 goto ipv6addr;
1580
1581 default:
1582 break;
1583 }
1584
1585 fr_strerror_const("Combo IP value missing af");
1586 return 0;
1587
1589 switch (value->vb_ip.af) {
1590 case AF_INET:
1591 goto ipv4prefix;
1592
1593 case AF_INET6:
1594 goto ipv6prefix;
1595
1596 default:
1597 break;
1598 }
1599
1600 fr_strerror_const("Combo IP value missing af");
1601 return 0;
1602
1603 /*
1604 * Already in network byte-order
1605 */
1606 case FR_TYPE_IFID:
1607 case FR_TYPE_ETHERNET:
1608 case FR_TYPE_UINT8:
1609 case FR_TYPE_INT8:
1611 break;
1612
1613 /*
1614 * Needs a bytesex operation
1615 */
1616 case FR_TYPE_UINT16:
1617 case FR_TYPE_UINT32:
1618 case FR_TYPE_UINT64:
1619 case FR_TYPE_INT16:
1620 case FR_TYPE_INT32:
1621 case FR_TYPE_INT64:
1622 case FR_TYPE_FLOAT32:
1623 case FR_TYPE_FLOAT64:
1624 {
1625 fr_value_box_t tmp;
1626
1627 fr_value_box_hton(&tmp, value);
1628
1629 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, fr_value_box_raw(&tmp, value->type), min);
1630 }
1631 break;
1632
1633 /*
1634 * Dates and deltas are stored internally as
1635 * 64-bit nanoseconds. We have to convert to the
1636 * network format. First by resolution (ns, us,
1637 * ms, s), and then by size (16/32/64-bit).
1638 */
1639 case FR_TYPE_DATE:
1640 {
1641 uint64_t date = 0;
1642 fr_time_res_t res;
1643
1644 if (!value->enumv) {
1645 res = FR_TIME_RES_SEC;
1646 } else {
1647 res = value->enumv->flags.flag_time_res;
1648 }
1649 date = fr_unix_time_to_integer(value->vb_date, res);
1650
1651 if (!value->enumv) {
1652 goto date_size4;
1653
1654 } else switch (value->enumv->flags.length) {
1655 case 2:
1656 if (date > UINT16_MAX) date = UINT16_MAX;
1657 FR_DBUFF_IN_RETURN(&work_dbuff, (int16_t) date);
1658 break;
1659
1660 date_size4:
1661 case 4:
1662 if (date > UINT32_MAX) date = UINT32_MAX;
1663 FR_DBUFF_IN_RETURN(&work_dbuff, (int32_t) date);
1664 break;
1665
1666 case 8:
1667 FR_DBUFF_IN_RETURN(&work_dbuff, date);
1668 break;
1669
1670 default:
1671 goto unsupported;
1672 }
1673
1674 }
1675 break;
1676
1677 case FR_TYPE_TIME_DELTA:
1678 {
1679 int64_t date = 0; /* may be negative */
1681 if (value->enumv) res = value->enumv->flags.flag_time_res;
1682
1683 date = fr_time_delta_to_integer(value->vb_time_delta, res);
1684
1685 if (!value->enumv) {
1686 goto delta_size4;
1687
1688 } else if (!value->enumv->flags.is_unsigned) {
1689 switch (value->enumv->flags.length) {
1690 case 2:
1691 if (date < INT16_MIN) {
1692 date = INT16_MIN;
1693 } else if (date > INT16_MAX) {
1694 date = INT16_MAX;
1695 }
1696 FR_DBUFF_IN_RETURN(&work_dbuff, (int16_t)date);
1697 break;
1698
1699 delta_size4:
1700 case 4:
1701 if (date < INT32_MIN) {
1702 date = INT32_MIN;
1703 } else if (date > INT32_MAX) {
1704 date = INT32_MAX;
1705 }
1706 FR_DBUFF_IN_RETURN(&work_dbuff, (int32_t)date);
1707 break;
1708
1709 case 8:
1710 FR_DBUFF_IN_RETURN(&work_dbuff, (int64_t)date);
1711 break;
1712
1713 default:
1714 goto unsupported;
1715 }
1716 } else { /* time delta is unsigned! */
1717 switch (value->enumv->flags.length) {
1718 case 2:
1719 if (date < 0) {
1720 date = 0;
1721 } else if (date > UINT16_MAX) {
1722 date = UINT16_MAX;
1723 }
1724 FR_DBUFF_IN_RETURN(&work_dbuff, (uint16_t)date);
1725 break;
1726
1727 case 4:
1728 if (date < 0) {
1729 date = 0;
1730 } else if (date > UINT32_MAX) {
1731 date = UINT32_MAX;
1732 }
1733 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t)date);
1734 break;
1735
1736 case 8:
1737 FR_DBUFF_IN_RETURN(&work_dbuff, (uint64_t)date);
1738 break;
1739
1740 default:
1741 goto unsupported;
1742 }
1743 }
1744 }
1745 break;
1746
1747 case FR_TYPE_OCTETS:
1748 case FR_TYPE_STRING:
1749 case FR_TYPE_SIZE:
1750 case FR_TYPE_NON_LEAF:
1751 goto unsupported;
1752 }
1753
1754 return fr_dbuff_set(dbuff, &work_dbuff);
1755}
1756
1757/** Decode a #fr_value_box_t from serialized binary data
1758 *
1759 * The general deserialization rules are:
1760 *
1761 * - Octets are decoded in binary form (not hex).
1762 * - Strings are decoded without the trailing \0 byte. Strings must consist only of valid UTF8 chars.
1763 * - Integers are decoded big-endian.
1764 * - Bools are decoded using one byte, with value 0x00 (false) or 0x01 (true).
1765 * - Signed integers are decoded two's complement, with the MSB as the sign bit.
1766 * Byte order is big-endian.
1767 * - Network addresses are decoded big-endian.
1768 * - IPv4 prefixes are decoded with 1 byte for the prefix, then 4 bytes of address.
1769 * - IPv6 prefixes are decoded with 1 byte for the scope_id, 1 byte for the prefix,
1770 * and 16 bytes of address.
1771 * - Floats are decoded in IEEE-754 format with a big-endian byte order. We rely
1772 * on the fact that the C standards require floats to be represented in IEEE-754
1773 * format in memory.
1774 * - Dates are decoded as 32bit unsigned UNIX timestamps.
1775 *
1776 * All of the dictionary rules are respected. string/octets can have
1777 * a fixed length, or can have an 8/16-bit "length" prefix. If the
1778 * enumv is not an array, then the input # len MUST be the correct size
1779 * (not too large or small), otherwise an error is returned.
1780 *
1781 * If the enumv is an array, then the input must have the minimum
1782 * length, and the number of bytes decoded is capped at the maximum
1783 * length allowed to be decoded. This behavior allows the caller to
1784 * decode an array of values simply by calling this function in a
1785 * loop.
1786 *
1787 * @param[in] ctx Where to allocate any talloc buffers required.
1788 * @param[out] dst value_box to write the result to.
1789 * @param[in] type to decode data to.
1790 * @param[in] enumv Aliases for values.
1791 * @param[in] dbuff Binary data to decode.
1792 * @param[in] len Length of data to decode. For fixed length types we only
1793 * decode complete values.
1794 * @param[in] tainted Whether the value came from a trusted source.
1795 * @return
1796 * - >= 0 The number of bytes consumed.
1797 * - <0 - The negative offset where the error occurred.
1798 * - FR_VALUE_BOX_NET_OOM (negative value) - Out of memory.
1799 */
1801 fr_value_box_t *dst, fr_type_t type, fr_dict_attr_t const *enumv,
1802 fr_dbuff_t *dbuff, size_t len,
1803 bool tainted)
1804{
1805 size_t min, max;
1806 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
1807
1809 max = network_max_size(type);
1810
1811 if (len < min) {
1812 fr_strerror_printf("Got truncated value parsing type \"%s\". "
1813 "Expected length >= %zu bytes, got %zu bytes",
1815 min, len);
1816 return -(len);
1817 }
1818
1819 /*
1820 * For array entries, we only decode one value at a time.
1821 */
1822 if (len > max) {
1823 if (enumv && !enumv->flags.array) {
1824 fr_strerror_printf("Found trailing garbage parsing type \"%s\". "
1825 "Expected length <= %zu bytes, got %zu bytes",
1827 max, len);
1828 return -(max);
1829 }
1830
1831 len = max;
1832 }
1833
1834 /*
1835 * String / octets are special.
1836 */
1838 size_t newlen = len;
1839 size_t offset = 0;
1840
1841 /*
1842 * Decode fixed-width fields.
1843 */
1844 if (enumv) {
1845 if (enumv->flags.length) {
1846 newlen = enumv->flags.length;
1847
1848 } else if (da_is_length_field(enumv)) {
1849 /*
1850 * Or fields with a length prefix.
1851 */
1852 if (enumv->flags.subtype == FLAG_LENGTH_UINT8) {
1853 uint8_t num = 0;
1854
1855 FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
1856 newlen = num;
1857 offset = 1;
1858
1859 } else if (enumv->flags.subtype == FLAG_LENGTH_UINT16) {
1860 uint16_t num = 0;
1861
1862 FR_DBUFF_OUT_RETURN(&num, &work_dbuff);
1863 newlen = num;
1864 offset = 2;
1865
1866 } else {
1867 return -1;
1868 }
1869 }
1870 }
1871
1872 /*
1873 * If we need more data than exists, that's an error.
1874 *
1875 * Otherwise, bound the decoding to the count we found.
1876 */
1877 if (newlen > len) return -(newlen + offset);
1878 len = newlen;
1879
1880 switch (type) {
1881 case FR_TYPE_STRING:
1882 if (fr_value_box_bstrndup_dbuff(ctx, dst, enumv, &work_dbuff, len, tainted) < 0) {
1883 return FR_VALUE_BOX_NET_OOM;
1884 }
1885 return fr_dbuff_set(dbuff, &work_dbuff);
1886
1887 case FR_TYPE_OCTETS:
1888 if (fr_value_box_memdup_dbuff(ctx, dst, enumv, &work_dbuff, len, tainted) < 0) {
1889 return FR_VALUE_BOX_NET_OOM;
1890 }
1891 return fr_dbuff_set(dbuff, &work_dbuff);
1892
1893 default:
1894 return -1;
1895 }
1896 }
1897
1898 /*
1899 * Pre-Initialise box for non-variable types
1900 */
1901 fr_value_box_init(dst, type, enumv, tainted);
1902 switch (type) {
1903 /*
1904 * Already in network byte order
1905 */
1906 case FR_TYPE_IPV4_ADDR:
1907 ipv4addr:
1908 dst->vb_ip = (fr_ipaddr_t){
1909 .af = AF_INET,
1910 .prefix = 32,
1911 };
1912 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *)&dst->vb_ip.addr.v4, &work_dbuff, len);
1913 break;
1914
1916 ipv4prefix:
1917 dst->vb_ip = (fr_ipaddr_t){
1918 .af = AF_INET,
1919 };
1920 FR_DBUFF_OUT_RETURN(&dst->vb_ip.prefix, &work_dbuff);
1921 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *)&dst->vb_ip.addr.v4, &work_dbuff, len - 1);
1922 break;
1923
1924 case FR_TYPE_IPV6_ADDR:
1925 ipv6addr:
1926 dst->vb_ip = (fr_ipaddr_t){
1927 .af = AF_INET6,
1928 .scope_id = 0,
1929 .prefix = 128
1930 };
1931 if (len == max) {
1932 uint8_t scope_id = 0;
1933
1934 FR_DBUFF_OUT_RETURN(&scope_id, &work_dbuff);
1935 dst->vb_ip.scope_id = scope_id;
1936 len--;
1937 }
1938 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *)&dst->vb_ip.addr.v6, &work_dbuff, len);
1939 break;
1940
1942 ipv6prefix:
1943 dst->vb_ip = (fr_ipaddr_t){
1944 .af = AF_INET6,
1945 .scope_id = 0,
1946 };
1947 if (len == max) {
1948 uint8_t scope_id = 0;
1949
1950 FR_DBUFF_OUT_RETURN(&scope_id, &work_dbuff);
1951 dst->vb_ip.scope_id = scope_id;
1952 len--;
1953 }
1954 FR_DBUFF_OUT_RETURN(&dst->vb_ip.prefix, &work_dbuff);
1955 FR_DBUFF_OUT_MEMCPY_RETURN((uint8_t *)&dst->vb_ip.addr.v6, &work_dbuff, len - 1);
1956 break;
1957
1959 if ((len >= network_min_size(FR_TYPE_IPV6_ADDR)) &&
1960 (len <= network_max_size(FR_TYPE_IPV6_ADDR))) goto ipv6addr; /* scope is optional */
1961 else if ((len >= network_min_size(FR_TYPE_IPV4_ADDR)) &&
1962 (len <= network_max_size(FR_TYPE_IPV4_ADDR))) goto ipv4addr;
1963 fr_strerror_const("Invalid combo ip address value");
1964 return 0;
1965
1967 if ((len >= network_min_size(FR_TYPE_IPV6_PREFIX)) &&
1968 (len <= network_max_size(FR_TYPE_IPV6_PREFIX))) goto ipv6prefix; /* scope is optional */
1969 else if ((len >= network_min_size(FR_TYPE_IPV4_PREFIX)) &&
1970 (len <= network_max_size(FR_TYPE_IPV4_PREFIX))) goto ipv4prefix;
1971 fr_strerror_const("Invalid combo ip prefix value");
1972 return 0;
1973
1974 case FR_TYPE_BOOL:
1975 {
1976 uint8_t val = 0;
1977
1978 FR_DBUFF_OUT_RETURN(&val, &work_dbuff);
1979 dst->datum.boolean = (val != 0);
1980 }
1981 break;
1982
1983 case FR_TYPE_IFID:
1984 case FR_TYPE_ETHERNET:
1985 FR_DBUFF_OUT_MEMCPY_RETURN(fr_value_box_raw(dst, type), &work_dbuff, len);
1986 break;
1987
1988 case FR_TYPE_UINT8:
1989 FR_DBUFF_OUT_RETURN(&dst->vb_uint8, &work_dbuff);
1990 break;
1991
1992 case FR_TYPE_UINT16:
1993 FR_DBUFF_OUT_RETURN(&dst->vb_uint16, &work_dbuff);
1994 break;
1995
1996 case FR_TYPE_UINT32:
1997 FR_DBUFF_OUT_RETURN(&dst->vb_uint32, &work_dbuff);
1998 break;
1999
2000 case FR_TYPE_UINT64:
2001 FR_DBUFF_OUT_RETURN(&dst->vb_uint64, &work_dbuff);
2002 break;
2003
2004 case FR_TYPE_INT8:
2005 FR_DBUFF_OUT_RETURN(&dst->vb_int8, &work_dbuff);
2006 break;
2007
2008 case FR_TYPE_INT16:
2009 FR_DBUFF_OUT_RETURN(&dst->vb_int16, &work_dbuff);
2010 break;
2011
2012 case FR_TYPE_INT32:
2013 FR_DBUFF_OUT_RETURN(&dst->vb_int32, &work_dbuff);
2014 break;
2015
2016 case FR_TYPE_INT64:
2017 FR_DBUFF_OUT_RETURN(&dst->vb_int64, &work_dbuff);
2018 break;
2019
2020 case FR_TYPE_FLOAT32:
2021 FR_DBUFF_OUT_RETURN(&dst->vb_float32, &work_dbuff);
2022 break;
2023
2024 case FR_TYPE_FLOAT64:
2025 FR_DBUFF_OUT_RETURN(&dst->vb_float64, &work_dbuff);
2026 break;
2027
2028 /*
2029 * Dates and deltas are stored internally as
2030 * 64-bit nanoseconds. We have to convert from
2031 * the network format. First by size
2032 * (16/32/64-bit), and then by resolution (ns,
2033 * us, ms, s).
2034 */
2035 case FR_TYPE_DATE:
2036 {
2037 size_t length = 4;
2038 fr_time_res_t precision = FR_TIME_RES_SEC;
2039 uint64_t date;
2040
2041 if (enumv) {
2042 length = enumv->flags.length;
2043 precision = (fr_time_res_t)enumv->flags.flag_time_res;
2044 }
2045
2046 /*
2047 * Input data doesn't match what we were told we
2048 * need.
2049 */
2050 if (len > length) return -(length);
2051
2052 dst->enumv = enumv;
2053
2054 FR_DBUFF_OUT_UINT64V_RETURN(&date, &work_dbuff, length);
2055
2056 if (!fr_multiply(&date, date, fr_time_multiplier_by_res[precision])) {
2057 fr_strerror_const("date would overflow");
2058 return 0;
2059 }
2060
2061 dst->vb_date = fr_unix_time_wrap(date);
2062 }
2063 break;
2064
2065 case FR_TYPE_TIME_DELTA:
2066 {
2067 size_t length = 4;
2068 fr_time_res_t precision = FR_TIME_RES_SEC;
2069 int64_t date;
2070
2071 if (enumv) {
2072 length = enumv->flags.length;
2073 precision = (fr_time_res_t)enumv->flags.flag_time_res;
2074 }
2075
2076 /*
2077 * Input data doesn't match what we were told we
2078 * need.
2079 */
2080 if (len > length) return -(length);
2081
2082 dst->enumv = enumv;
2083
2084 if (!enumv || !enumv->flags.is_unsigned) {
2085 FR_DBUFF_OUT_INT64V_RETURN(&date, &work_dbuff, length);
2086 } else {
2087 uint64_t tmp;
2088
2089 /*
2090 * Else it's an unsigned time delta, but
2091 * we do have to clamp it at the max
2092 * value for a signed 64-bit integer.
2093 */
2094 FR_DBUFF_OUT_UINT64V_RETURN(&tmp, &work_dbuff, length);
2095
2096 if (tmp > INT64_MAX) tmp = INT64_MAX;
2097
2098 date = tmp;
2099 }
2100
2101 dst->vb_time_delta = fr_time_delta_wrap(fr_time_scale(date, precision));
2102 }
2103 break;
2104
2105 case FR_TYPE_STRING:
2106 case FR_TYPE_OCTETS:
2107 break; /* Already dealt with */
2108
2109 case FR_TYPE_SIZE:
2110 case FR_TYPE_NON_LEAF:
2111 fr_strerror_printf("Cannot decode type \"%s\" - Is not a value",
2113 break;
2114 }
2115
2116 return fr_dbuff_set(dbuff, &work_dbuff);
2117}
2118
2119/** Get a key from a value box
2120 *
2121 * @param[in,out] out - set to a small buffer on input. If the callback has more data
2122 * than is available here, the callback can update "out" to point elsewhere
2123 * @param[in,out] outlen The number of bits available in the initial buffer. On output,
2124 * the number of bits available in the key
2125 * @param[in] value the value box which contains the key
2126 * @return
2127 * - <0 on error
2128 * - 0 on success
2129 */
2131{
2132 ssize_t slen;
2133 fr_dbuff_t dbuff;
2134
2135 switch (value->type) {
2136 case FR_TYPE_BOOL:
2137 if (*outlen < 8) return -1;
2138
2139 *out[0] = (value->vb_bool) << 7;
2140 *outlen = 1;
2141 break;
2142
2144 if (*outlen < (fr_value_box_network_sizes[value->type][1] * 8)) return -1;
2145
2146 /*
2147 * Integers are put into network byte order.
2148 */
2149 fr_dbuff_init(&dbuff, *out, *outlen >> 3);
2150
2151 slen = fr_value_box_to_network(&dbuff, value);
2152 if (slen < 0) return -1;
2153 *outlen = slen * 8; /* bits not bytes */
2154 break;
2155
2156 case FR_TYPE_IP:
2157 /*
2158 * IPs are already in network byte order.
2159 */
2160 *out = UNCONST(uint8_t *, &value->vb_ip.addr);
2161 *outlen = value->vb_ip.prefix;
2162 break;
2163
2164 case FR_TYPE_STRING:
2165 case FR_TYPE_OCTETS:
2166 *out = value->datum.ptr;
2167 *outlen = value->vb_length * 8;
2168 break;
2169
2170 case FR_TYPE_ETHERNET:
2171 *out = UNCONST(uint8_t *, &value->vb_ether[0]);
2172 *outlen = sizeof(value->vb_ether) * 8;
2173 break;
2174
2175 default:
2176 fr_strerror_printf("Invalid data type '%s' for getting key",
2177 fr_type_to_str(value->type));
2178 return -1;
2179 }
2180
2181 return 0;
2182}
2183
2184/** Convert octets to a fixed size value box value
2185 *
2186 * All fixed size types are allowed.
2187 *
2188 * @param dst Where to write result of casting.
2189 * @param dst_type to cast to.
2190 * @param dst_enumv enumeration values.
2191 * @param src Input data.
2192 */
2194 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2195 fr_value_box_t const *src)
2196{
2197 uint8_t *ptr;
2198
2199 if (!fr_type_is_fixed_size(dst_type)) if (!fr_cond_assert(false)) return -1;
2200
2201 if (src->vb_length > network_max_size(dst_type)) {
2202 fr_strerror_printf("Invalid cast from %s to %s. Source length %zu is greater than "
2203 "destination type size %zu",
2204 fr_type_to_str(src->type),
2205 fr_type_to_str(dst_type),
2206 src->vb_length,
2207 network_max_size(dst_type));
2208 return -1;
2209 }
2210
2211 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
2212
2213 /*
2214 * No data to copy means just reset it to zero.
2215 */
2216 if (!src->vb_length) return 0;
2217
2218 ptr = (uint8_t *) &dst->datum;
2219
2220 /*
2221 * If the source is too small, just left-fill with zeroes.
2222 */
2223 if (src->vb_length < network_min_size(dst_type)) {
2224 ptr += network_min_size(dst_type) - src->vb_length;
2225 }
2226
2227 /*
2228 * Copy the raw octets into the datum of a value_box
2229 * inverting bytesex for uint32s (if LE).
2230 */
2231 memcpy(ptr, src->vb_octets, src->vb_length);
2232 fr_value_box_hton(dst, dst);
2233
2234 return 0;
2235}
2236
2237/** v4 to v6 mapping prefix
2238 *
2239 * Part of the IPv6 range is allocated to represent IPv4 addresses.
2240 */
2241static uint8_t const v4_v6_map[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2242 0x00, 0x00, 0x00, 0x00, 0xff, 0xff };
2243
2244
2245/** Convert any supported type to a string
2246 *
2247 * All non-structural types are allowed.
2248 *
2249 * @param ctx unused.
2250 * @param dst Where to write result of casting.
2251 * @param dst_type to cast to.
2252 * @param dst_enumv enumeration values.
2253 * @param src Input data.
2254 */
2255static inline int fr_value_box_cast_to_strvalue(TALLOC_CTX *ctx, fr_value_box_t *dst,
2256 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2257 fr_value_box_t const *src)
2258{
2259 if (!fr_cond_assert(dst_type == FR_TYPE_STRING)) return -1;
2260
2261 fr_value_box_init(dst, FR_TYPE_STRING, dst_enumv, false);
2262
2263 switch (src->type) {
2264 /*
2265 * The presentation format of octets is hex
2266 * What we actually want here is the raw string
2267 */
2268 case FR_TYPE_OCTETS:
2269 fr_value_box_safety_copy(dst, src);
2270 return fr_value_box_bstrndup(ctx, dst, dst_enumv,
2271 (char const *)src->vb_octets, src->vb_length, src->tainted);
2272
2273 case FR_TYPE_GROUP:
2275 dst, UNCONST(fr_value_box_list_t *, &src->vb_group),
2278 SIZE_MAX);
2279
2280 /*
2281 * Get the presentation format
2282 */
2283 default:
2284 {
2285 char *str;
2286
2287 fr_value_box_aprint(ctx, &str, src, NULL);
2288 if (unlikely(!str)) return -1;
2289
2291 return fr_value_box_bstrdup_buffer_shallow(NULL, dst, dst_enumv, str, src->tainted);
2292 }
2293 }
2294}
2295
2296/** Convert any supported type to octets
2297 *
2298 * All non-structural types are allowed.
2299 *
2300 * @param ctx unused.
2301 * @param dst Where to write result of casting.
2302 * @param dst_type to cast to.
2303 * @param dst_enumv enumeration values.
2304 * @param src Input data.
2305 */
2306static inline int fr_value_box_cast_to_octets(TALLOC_CTX *ctx, fr_value_box_t *dst,
2307 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2308 fr_value_box_t const *src)
2309{
2310 if (!fr_cond_assert(dst_type == FR_TYPE_OCTETS)) return -1;
2311
2312 fr_value_box_init(dst, FR_TYPE_OCTETS, dst_enumv, false);
2314
2315 switch (src->type) {
2316 /*
2317 * <string> (excluding terminating \0)
2318 */
2319 case FR_TYPE_STRING:
2320 fr_value_box_safety_copy(dst, src);
2321 return fr_value_box_memdup(ctx, dst, dst_enumv,
2322 (uint8_t const *)src->vb_strvalue, src->vb_length, src->tainted);
2323
2324 case FR_TYPE_GROUP:
2326 dst, UNCONST(fr_value_box_list_t *, &src->vb_group),
2329 SIZE_MAX);
2330 /*
2331 * <4 bytes address>
2332 */
2333 case FR_TYPE_IPV4_ADDR:
2334 return fr_value_box_memdup(ctx, dst, dst_enumv,
2335 (uint8_t const *)&src->vb_ip.addr.v4.s_addr,
2336 sizeof(src->vb_ip.addr.v4.s_addr), src->tainted);
2337
2338 /*
2339 * <1 uint8 prefix> + <4 bytes address>
2340 */
2342 {
2343 uint8_t *bin;
2344
2345 if (fr_value_box_mem_alloc(ctx, &bin, dst, dst_enumv,
2346 sizeof(src->vb_ip.addr.v4.s_addr) + 1, src->tainted) < 0) return -1;
2347
2348 bin[0] = src->vb_ip.prefix;
2349 memcpy(&bin[1], (uint8_t const *)&src->vb_ip.addr.v4.s_addr, sizeof(src->vb_ip.addr.v4.s_addr));
2350 }
2351 return 0;
2352
2353 /*
2354 * <16 bytes address>
2355 */
2356 case FR_TYPE_IPV6_ADDR:
2357 return fr_value_box_memdup(ctx, dst, dst_enumv,
2358 (uint8_t const *)src->vb_ip.addr.v6.s6_addr,
2359 sizeof(src->vb_ip.addr.v6.s6_addr), src->tainted);
2360
2361 /*
2362 * <1 uint8 prefix> + <1 uint8 scope> + <16 bytes address>
2363 */
2365 {
2366 uint8_t *bin;
2367
2368 if (fr_value_box_mem_alloc(ctx, &bin, dst, dst_enumv,
2369 sizeof(src->vb_ip.addr.v6.s6_addr) + 2, src->tainted) < 0) return -1;
2370 bin[0] = src->vb_ip.scope_id;
2371 bin[1] = src->vb_ip.prefix;
2372 memcpy(&bin[2], src->vb_ip.addr.v6.s6_addr, sizeof(src->vb_ip.addr.v6.s6_addr));
2373 }
2374 return 0;
2375
2376 /*
2377 * Get the raw binary in memory representation
2378 */
2379 case FR_TYPE_NUMERIC:
2380 {
2381 fr_value_box_t tmp;
2382
2383 fr_value_box_hton(&tmp, src); /* Flip any numeric representations */
2384 return fr_value_box_memdup(ctx, dst, dst_enumv,
2385 fr_value_box_raw(&tmp, src->type),
2386 fr_value_box_field_sizes[src->type], src->tainted);
2387 }
2388
2389 default:
2390 /* Not the same talloc_memdup call as above. The above memdup reads data from the dst */
2391 return fr_value_box_memdup(ctx, dst, dst_enumv,
2392 fr_value_box_raw(src, src->type),
2393 fr_value_box_field_sizes[src->type], src->tainted);
2394 }
2395}
2396
2397#define CAST_IP_FIX_COMBO \
2398 case FR_TYPE_COMBO_IP_ADDR: \
2399 if (src->vb_ip.af == AF_INET) { \
2400 src_type = FR_TYPE_IPV4_ADDR; \
2401 } else if (src->vb_ip.af == AF_INET6) { \
2402 src_type = FR_TYPE_IPV6_ADDR; \
2403 } \
2404 break; \
2405 case FR_TYPE_COMBO_IP_PREFIX: \
2406 if (src->vb_ip.af == AF_INET) { \
2407 src_type = FR_TYPE_IPV4_PREFIX; \
2408 } else if (src->vb_ip.af == AF_INET6) { \
2409 src_type = FR_TYPE_IPV6_PREFIX; \
2410 } \
2411 break
2412
2413
2414/** Convert any supported type to an IPv4 address
2415 *
2416 * Allowed input types are:
2417 * - FR_TYPE_IPV6_ADDR (with v4 prefix).
2418 * - FR_TYPE_IPV4_PREFIX (with 32bit mask).
2419 * - FR_TYPE_IPV6_PREFIX (with v4 prefix and 128bit mask).
2420 * - FR_TYPE_OCTETS (of length 4).
2421 * - FR_TYPE_UINT32
2422 *
2423 * @param ctx unused.
2424 * @param dst Where to write result of casting.
2425 * @param dst_type to cast to.
2426 * @param dst_enumv enumeration values.
2427 * @param src Input data.
2428 */
2429static inline int fr_value_box_cast_to_ipv4addr(TALLOC_CTX *ctx, fr_value_box_t *dst,
2430 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2431 fr_value_box_t const *src)
2432{
2433 fr_type_t src_type = src->type;
2434
2435 fr_assert(dst_type == FR_TYPE_IPV4_ADDR);
2437
2438 switch (src_type) {
2439 case FR_TYPE_STRING:
2440 return fr_value_box_from_str(ctx, dst, dst_type, dst_enumv,
2441 src->vb_strvalue, src->vb_length,
2442 NULL);
2443
2445
2446 default:
2447 break;
2448 }
2449
2450 /*
2451 * Pre-initialise box for non-variable types
2452 */
2453 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
2454 dst->vb_ip.af = AF_INET;
2455 dst->vb_ip.prefix = 32;
2456 dst->vb_ip.scope_id = 0;
2457
2458 switch (src_type) {
2459 case FR_TYPE_IPV6_ADDR:
2460 if (memcmp(src->vb_ip.addr.v6.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) {
2461 bad_v6_prefix_map:
2462 fr_strerror_printf("Invalid cast from %s to %s. No IPv4-IPv6 mapping prefix",
2463 fr_type_to_str(src->type),
2464 fr_type_to_str(dst_type));
2465 return -1;
2466 }
2467
2468 memcpy(&dst->vb_ip.addr.v4, &src->vb_ip.addr.v6.s6_addr[sizeof(v4_v6_map)],
2469 sizeof(dst->vb_ip.addr.v4));
2470
2471 break;
2472
2474 if (src->vb_ip.prefix != 32) {
2475 fr_strerror_printf("Invalid cast from %s to %s. Only /32 (not %i/) prefixes may be "
2476 "cast to IP address types",
2477 fr_type_to_str(src->type),
2478 fr_type_to_str(dst_type),
2479 src->vb_ip.prefix);
2480 return -1;
2481 }
2483
2484 case FR_TYPE_IPV4_ADDR: /* Needed for handling combo addresses */
2485 memcpy(&dst->vb_ip.addr.v4, &src->vb_ip.addr.v4, sizeof(dst->vb_ip.addr.v4));
2486 break;
2487
2489 if (src->vb_ip.prefix != 128) {
2490 fr_strerror_printf("Invalid cast from %s to %s. Only /128 (not /%i) prefixes may be "
2491 "cast to IP address types",
2492 fr_type_to_str(src->type),
2493 fr_type_to_str(dst_type),
2494 src->vb_ip.prefix);
2495 return -1;
2496 }
2497 if (memcmp(&src->vb_ip.addr.v6.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) goto bad_v6_prefix_map;
2498 memcpy(&dst->vb_ip.addr.v4, &src->vb_ip.addr.v6.s6_addr[sizeof(v4_v6_map)],
2499 sizeof(dst->vb_ip.addr.v4));
2500 break;
2501
2502 case FR_TYPE_OCTETS:
2503 if (src->vb_length != sizeof(dst->vb_ip.addr.v4.s_addr)) {
2504 fr_strerror_printf("Invalid cast from %s to %s. Needed octet string of length %zu, got %zu",
2505 fr_type_to_str(src->type),
2506 fr_type_to_str(dst_type),
2507 sizeof(dst->vb_ip.addr.v4.s_addr), src->vb_length);
2508 return -1;
2509 }
2510 memcpy(&dst->vb_ip.addr.v4, src->vb_octets, sizeof(dst->vb_ip.addr.v4.s_addr));
2511 break;
2512
2513 case FR_TYPE_UINT32:
2514 {
2515 uint32_t net;
2516
2517 net = ntohl(src->vb_uint32);
2518 memcpy(&dst->vb_ip.addr.v4, (uint8_t *)&net, sizeof(dst->vb_ip.addr.v4.s_addr));
2519 }
2520 break;
2521
2522 default:
2523 fr_strerror_printf("Invalid cast from %s to %s. Unsupported",
2524 fr_type_to_str(src->type),
2525 fr_type_to_str(dst_type));
2526 return -1;
2527 }
2528
2529 return 0;
2530}
2531
2532/** Convert any supported type to an IPv6 address
2533 *
2534 * Allowed input types are:
2535 * - FR_TYPE_IPV4_ADDR
2536 * - FR_TYPE_IPV4_PREFIX (with 32bit mask).
2537 * - FR_TYPE_IPV6_PREFIX (with 128bit mask).
2538 * - FR_TYPE_OCTETS (of length 16).
2539 *
2540 * @param ctx unused.
2541 * @param dst Where to write result of casting.
2542 * @param dst_type to cast to.
2543 * @param dst_enumv enumeration values.
2544 * @param src Input data.
2545 */
2546static inline int fr_value_box_cast_to_ipv4prefix(TALLOC_CTX *ctx, fr_value_box_t *dst,
2547 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2548 fr_value_box_t const *src)
2549{
2550 fr_type_t src_type = src->type;
2551
2552 fr_assert(dst_type == FR_TYPE_IPV4_PREFIX);
2554
2555 switch (src_type) {
2556 case FR_TYPE_STRING:
2557 return fr_value_box_from_str(ctx, dst, dst_type, dst_enumv,
2558 src->vb_strvalue, src->vb_length,
2559 NULL);
2560
2562
2563 default:
2564 break;
2565 }
2566
2567 /*
2568 * Pre-initialise box for non-variable types
2569 */
2570 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
2571 dst->vb_ip.af = AF_INET;
2572 dst->vb_ip.scope_id = 0;
2573
2574 switch (src_type) {
2575 case FR_TYPE_IPV4_PREFIX: /* Needed for handling combo prefixes */
2576 dst->vb_ip.prefix = src->vb_ip.prefix;
2578
2579 case FR_TYPE_IPV4_ADDR:
2580 memcpy(&dst->vb_ip, &src->vb_ip, sizeof(dst->vb_ip));
2581 break;
2582
2583 /*
2584 * Copy the last four bytes, to make an IPv4prefix
2585 */
2586 case FR_TYPE_IPV6_ADDR:
2587 if (memcmp(src->vb_ip.addr.v6.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) {
2588 bad_v6_prefix_map:
2589 fr_strerror_printf("Invalid cast from %s to %s. No IPv4-IPv6 mapping prefix",
2590 fr_type_to_str(src->type),
2591 fr_type_to_str(dst_type));
2592 return -1;
2593 }
2594 memcpy(&dst->vb_ip.addr.v4.s_addr, &src->vb_ip.addr.v6.s6_addr[sizeof(v4_v6_map)],
2595 sizeof(dst->vb_ip.addr.v4.s_addr));
2596 dst->vb_ip.prefix = 32;
2597 break;
2598
2600 if (memcmp(src->vb_ip.addr.v6.s6_addr, v4_v6_map, sizeof(v4_v6_map)) != 0) goto bad_v6_prefix_map;
2601
2602 if (src->vb_ip.prefix < (sizeof(v4_v6_map) << 3)) {
2603 fr_strerror_printf("Invalid cast from %s to %s. Expected prefix >= %u bits got %u bits",
2604 fr_type_to_str(src->type),
2605 fr_type_to_str(dst_type),
2606 (unsigned int)(sizeof(v4_v6_map) << 3), src->vb_ip.prefix);
2607 return -1;
2608 }
2609 memcpy(&dst->vb_ip.addr.v4.s_addr, &src->vb_ip.addr.v6.s6_addr[sizeof(v4_v6_map)],
2610 sizeof(dst->vb_ip.addr.v4.s_addr));
2611
2612 /*
2613 * Subtract the bits used by the v4_v6_map to get the v4 prefix bits
2614 */
2615 dst->vb_ip.prefix = src->vb_ip.prefix - (sizeof(v4_v6_map) << 3);
2616 break;
2617
2618 case FR_TYPE_OCTETS:
2619 if (src->vb_length != sizeof(dst->vb_ip.addr.v4.s_addr) + 1) {
2620 fr_strerror_printf("Invalid cast from %s to %s. Needed octet string of length %zu, got %zu",
2621 fr_type_to_str(src->type),
2622 fr_type_to_str(dst_type),
2623 sizeof(dst->vb_ip.addr.v4.s_addr) + 1, src->vb_length);
2624 return -1;
2625 }
2626 dst->vb_ip.prefix = src->vb_octets[0];
2627 memcpy(&dst->vb_ip.addr.v4, &src->vb_octets[1], sizeof(dst->vb_ip.addr.v4.s_addr));
2628 break;
2629
2630 case FR_TYPE_UINT32:
2631 {
2632 uint32_t net;
2633
2634 net = ntohl(src->vb_uint32);
2635 memcpy(&dst->vb_ip.addr.v4, (uint8_t *)&net, sizeof(dst->vb_ip.addr.v4.s_addr));
2636 dst->vb_ip.prefix = 32;
2637 break;
2638 }
2639
2640 default:
2641 fr_strerror_printf("Invalid cast from %s to %s. Unsupported",
2642 fr_type_to_str(src->type),
2643 fr_type_to_str(dst_type));
2644 return -1;
2645 }
2646
2647 return 0;
2648}
2649
2650/** Convert any supported type to an IPv6 address
2651 *
2652 * Allowed input types are:
2653 * - FR_TYPE_IPV4_ADDR
2654 * - FR_TYPE_IPV4_PREFIX (with 32bit mask).
2655 * - FR_TYPE_IPV6_PREFIX (with 128bit mask).
2656 * - FR_TYPE_OCTETS (of length 16).
2657 *
2658 * @param ctx unused.
2659 * @param dst Where to write result of casting.
2660 * @param dst_type to cast to.
2661 * @param dst_enumv enumeration values.
2662 * @param src Input data.
2663 */
2664static inline int fr_value_box_cast_to_ipv6addr(TALLOC_CTX *ctx, fr_value_box_t *dst,
2665 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2666 fr_value_box_t const *src)
2667{
2668 fr_type_t src_type = src->type;
2669
2670 static_assert((sizeof(v4_v6_map) + sizeof(src->vb_ip.addr.v4)) <=
2671 sizeof(src->vb_ip.addr.v6), "IPv6 storage too small");
2672
2673 fr_assert(dst_type == FR_TYPE_IPV6_ADDR);
2675
2676 switch (src_type) {
2677 case FR_TYPE_STRING:
2678 return fr_value_box_from_str(ctx, dst, dst_type, dst_enumv,
2679 src->vb_strvalue, src->vb_length,
2680 NULL);
2681
2683
2684 default:
2685 break;
2686 }
2687
2688 /*
2689 * Pre-initialise box for non-variable types
2690 */
2691 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
2692 dst->vb_ip.af = AF_INET6;
2693 dst->vb_ip.prefix = 128;
2694
2695 switch (src_type) {
2696 case FR_TYPE_IPV4_ADDR:
2697 {
2698 uint8_t *p = dst->vb_ip.addr.v6.s6_addr;
2699
2700 /* Add the v4/v6 mapping prefix */
2701 memcpy(p, v4_v6_map, sizeof(v4_v6_map));
2702 p += sizeof(v4_v6_map);
2703 memcpy(p, (uint8_t const *)&src->vb_ip.addr.v4.s_addr, sizeof(src->vb_ip.addr.v4.s_addr));
2704 dst->vb_ip.scope_id = 0;
2705 }
2706 break;
2707
2709 {
2710 uint8_t *p = dst->vb_ip.addr.v6.s6_addr;
2711
2712 if (src->vb_ip.prefix != 32) {
2713 fr_strerror_printf("Invalid cast from %s to %s. Only /32 (not /%i) prefixes may be "
2714 "cast to IP address types",
2715 fr_type_to_str(src->type),
2716 fr_type_to_str(dst_type),
2717 src->vb_ip.prefix);
2718 return -1;
2719 }
2720
2721 /* Add the v4/v6 mapping prefix */
2722 memcpy(p, v4_v6_map, sizeof(v4_v6_map));
2723 p += sizeof(v4_v6_map);
2724 memcpy(p, (uint8_t const *)&src->vb_ip.addr.v4.s_addr, sizeof(src->vb_ip.addr.v4.s_addr));
2725 dst->vb_ip.scope_id = 0;
2726 }
2727 break;
2728
2730 if (src->vb_ip.prefix != 128) {
2731 fr_strerror_printf("Invalid cast from %s to %s. Only /128 (not /%i) prefixes may be "
2732 "cast to IP address types",
2733 fr_type_to_str(src->type),
2734 fr_type_to_str(dst_type),
2735 src->vb_ip.prefix);
2736 return -1;
2737 }
2739
2740 case FR_TYPE_IPV6_ADDR: /* Needed for handling combo addresses */
2741 memcpy(dst->vb_ip.addr.v6.s6_addr, src->vb_ip.addr.v6.s6_addr,
2742 sizeof(dst->vb_ip.addr.v6.s6_addr));
2743 dst->vb_ip.scope_id = src->vb_ip.scope_id;
2744 break;
2745
2746 case FR_TYPE_OCTETS:
2747 if (src->vb_length != sizeof(dst->vb_ip.addr.v6.s6_addr)) {
2748 fr_strerror_printf("Invalid cast from %s to %s. Needed octet string of length %zu, got %zu",
2749 fr_type_to_str(src->type),
2750 fr_type_to_str(dst_type),
2751 sizeof(dst->vb_ip.addr.v6.s6_addr), src->vb_length);
2752 return -1;
2753 }
2754 memcpy(&dst->vb_ip.addr.v6.s6_addr, src->vb_octets, sizeof(dst->vb_ip.addr.v6.s6_addr));
2755 break;
2756
2757 default:
2758 fr_strerror_printf("Invalid cast from %s to %s. Unsupported",
2759 fr_type_to_str(src->type),
2760 fr_type_to_str(dst_type));
2761 break;
2762 }
2763
2764 return 0;
2765}
2766
2767/** Convert any supported type to an IPv6 address
2768 *
2769 * Allowed input types are:
2770 * - FR_TYPE_IPV4_ADDR
2771 * - FR_TYPE_IPV4_PREFIX (with 32bit mask).
2772 * - FR_TYPE_IPV6_PREFIX (with 128bit mask).
2773 * - FR_TYPE_OCTETS (of length 16).
2774 *
2775 * @param ctx unused.
2776 * @param dst Where to write result of casting.
2777 * @param dst_type to cast to.
2778 * @param dst_enumv enumeration values.
2779 * @param src Input data.
2780 */
2781static inline int fr_value_box_cast_to_ipv6prefix(TALLOC_CTX *ctx, fr_value_box_t *dst,
2782 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2783 fr_value_box_t const *src)
2784{
2785 fr_type_t src_type = src->type;
2786
2787 fr_assert(dst_type == FR_TYPE_IPV6_PREFIX);
2789
2790 switch (src_type) {
2791 case FR_TYPE_STRING:
2792 return fr_value_box_from_str(ctx, dst, dst_type, dst_enumv,
2793 src->vb_strvalue, src->vb_length,
2794 NULL);
2795
2797
2798 default:
2799 break;
2800 }
2801
2802 /*
2803 * Pre-initialise box for non-variable types
2804 */
2805 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
2806 dst->vb_ip.af = AF_INET6;
2807
2808 switch (src_type) {
2809 case FR_TYPE_IPV4_ADDR:
2810 {
2811 uint8_t *p = dst->vb_ip.addr.v6.s6_addr;
2812
2813 /* Add the v4/v6 mapping prefix */
2814 memcpy(p, v4_v6_map, sizeof(v4_v6_map));
2815 p += sizeof(v4_v6_map);
2816 memcpy(p, (uint8_t const *)&src->vb_ip.addr.v4.s_addr, sizeof(src->vb_ip.addr.v4.s_addr));
2817 dst->vb_ip.prefix = 128;
2818 dst->vb_ip.scope_id = 0;
2819 }
2820 break;
2821
2823 {
2824 uint8_t *p = dst->vb_ip.addr.v6.s6_addr;
2825
2826 /* Add the v4/v6 mapping prefix */
2827 memcpy(p, v4_v6_map, sizeof(v4_v6_map));
2828 p += sizeof(v4_v6_map);
2829 memcpy(p, (uint8_t const *)&src->vb_ip.addr.v4.s_addr, sizeof(src->vb_ip.addr.v4.s_addr));
2830 dst->vb_ip.prefix = (sizeof(v4_v6_map) << 3) + src->vb_ip.prefix;
2831 dst->vb_ip.scope_id = 0;
2832 }
2833 break;
2834
2835 case FR_TYPE_IPV6_PREFIX: /* Needed for handling combo prefixes */
2836 dst->vb_ip.prefix = src->vb_ip.prefix;
2837 goto v6_common;
2838
2839 case FR_TYPE_IPV6_ADDR:
2840 dst->vb_ip.prefix = 128;
2841 v6_common:
2842 memcpy(dst->vb_ip.addr.v6.s6_addr, src->vb_ip.addr.v6.s6_addr,
2843 sizeof(dst->vb_ip.addr.v6.s6_addr));
2844 dst->vb_ip.scope_id = src->vb_ip.scope_id;
2845 break;
2846
2847 case FR_TYPE_OCTETS:
2848 if (src->vb_length != (sizeof(dst->vb_ip.addr.v6.s6_addr) + 2)) {
2849 fr_strerror_printf("Invalid cast from %s to %s. Needed octet string of length %zu, got %zu",
2850 fr_type_to_str(src->type),
2851 fr_type_to_str(dst_type),
2852 sizeof(dst->vb_ip.addr.v6.s6_addr) + 2, src->vb_length);
2853 return -1;
2854 }
2855 dst->vb_ip.scope_id = src->vb_octets[0];
2856 dst->vb_ip.prefix = src->vb_octets[1];
2857 memcpy(&dst->vb_ip.addr.v6.s6_addr, src->vb_octets, sizeof(dst->vb_ip.addr.v6.s6_addr));
2858 break;
2859
2860 default:
2861 fr_strerror_printf("Invalid cast from %s to %s. Unsupported",
2862 fr_type_to_str(src->type),
2863 fr_type_to_str(dst_type));
2864 return -1;
2865 }
2866 return 0;
2867}
2868
2869/** Convert any supported type to an ethernet address
2870 *
2871 * Allowed input types are:
2872 * - FR_TYPE_STRING ("00:11:22:33:44:55")
2873 * - FR_TYPE_OCTETS (0x001122334455)
2874 *
2875 *
2876 * @param ctx unused.
2877 * @param dst Where to write result of casting.
2878 * @param dst_type to cast to.
2879 * @param dst_enumv enumeration values.
2880 * @param src Input data.
2881 */
2882static inline int fr_value_box_cast_to_ethernet(TALLOC_CTX *ctx, fr_value_box_t *dst,
2883 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2884 fr_value_box_t const *src)
2885{
2886 fr_assert(dst_type == FR_TYPE_ETHERNET);
2888
2889 switch (src->type) {
2890 case FR_TYPE_STRING:
2891 return fr_value_box_from_str(ctx, dst, dst_type, dst_enumv,
2892 src->vb_strvalue, src->vb_length,
2893 NULL);
2894
2895 case FR_TYPE_OCTETS:
2896 return fr_value_box_fixed_size_from_octets(dst, dst_type, dst_enumv, src);
2897
2898 default:
2899 break;
2900 }
2901
2902 /*
2903 * Pre-initialise box for non-variable types
2904 */
2905 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
2906
2907 switch (src->type) {
2908 case FR_TYPE_UINT64: {
2909 uint8_t array[8];
2910
2911 fr_nbo_from_uint64(array, src->vb_uint64);
2912
2913 /*
2914 * For OUIs in the DB.
2915 */
2916 if ((array[0] != 0) || (array[1] != 0)) return -1;
2917
2918 memcpy(dst->vb_ether, &array[2], 6);
2919 break;
2920 }
2921
2922 default:
2923 fr_strerror_printf("Invalid cast from %s to %s. Unsupported",
2924 fr_type_to_str(src->type),
2925 fr_type_to_str(dst_type));
2926 return -1;
2927 }
2928
2929 return 0;
2930}
2931
2932/** Convert any supported type to a bool
2933 *
2934 * Allowed input types are:
2935 * - FR_TYPE_STRING ("yes", "true", "no", "false")
2936 *
2937 * @param ctx unused.
2938 * @param dst Where to write result of casting.
2939 * @param dst_type to cast to.
2940 * @param dst_enumv enumeration values.
2941 * @param src Input data.
2942 */
2943static inline int fr_value_box_cast_to_bool(TALLOC_CTX *ctx, fr_value_box_t *dst,
2944 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
2945 fr_value_box_t const *src)
2946{
2947 fr_assert(dst_type == FR_TYPE_BOOL);
2949
2950 switch (src->type) {
2951 case FR_TYPE_STRING:
2952 return fr_value_box_from_str(ctx, dst, dst_type, dst_enumv,
2953 src->vb_strvalue, src->vb_length,
2954 NULL);
2955
2956 case FR_TYPE_OCTETS:
2957 /*
2958 * This is really "bool from network"
2959 */
2960 return fr_value_box_fixed_size_from_octets(dst, dst_type, dst_enumv, src);
2961
2962 default:
2963 break;
2964 }
2965
2966 /*
2967 * Pre-initialise box for non-variable types
2968 */
2969 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
2970
2971 switch (src->type) {
2972 case FR_TYPE_INT8:
2973 dst->vb_bool = (src->vb_int8 != 0);
2974 break;
2975
2976 case FR_TYPE_UINT8:
2977 dst->vb_bool = (src->vb_uint8 != 0);
2978 break;
2979
2980 case FR_TYPE_INT16:
2981 dst->vb_bool = (src->vb_int16 != 0);
2982 break;
2983
2984 case FR_TYPE_UINT16:
2985 dst->vb_bool = (src->vb_uint16 != 0);
2986 break;
2987
2988 case FR_TYPE_INT32:
2989 dst->vb_bool = (src->vb_int32 != 0);
2990 break;
2991
2992 case FR_TYPE_UINT32:
2993 dst->vb_bool = (src->vb_uint32 != 0);
2994 break;
2995
2996 case FR_TYPE_INT64:
2997 dst->vb_bool = (src->vb_int64 != 0);
2998 break;
2999
3000 case FR_TYPE_UINT64:
3001 dst->vb_bool = (src->vb_uint64 != 0);
3002 break;
3003
3004 case FR_TYPE_SIZE:
3005 dst->vb_bool = (src->vb_size != 0);
3006 break;
3007
3008 case FR_TYPE_TIME_DELTA:
3009 dst->vb_bool = (fr_time_delta_unwrap(src->vb_time_delta) != 0);
3010 break;
3011
3012 case FR_TYPE_FLOAT32:
3013 dst->vb_bool = (fpclassify(src->vb_float32) == FP_ZERO);
3014 break;
3015
3016 case FR_TYPE_FLOAT64:
3017 dst->vb_bool = (fpclassify(src->vb_float64) == FP_ZERO);
3018 break;
3019
3020 default:
3021 fr_strerror_printf("Invalid cast from %s to %s. Unsupported",
3022 fr_type_to_str(src->type),
3023 fr_type_to_str(dst_type));
3024 return -1;
3025 }
3026
3027 return 0;
3028}
3029
3030/** Convert any signed or unsigned integer type to any other signed or unsigned integer type
3031 *
3032 */
3033static inline int fr_value_box_cast_integer_to_integer(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst,
3034 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
3035 fr_value_box_t const *src)
3036{
3037 uint64_t tmp = 0;
3038 size_t len = fr_value_box_field_sizes[src->type];
3039 int64_t min;
3040
3042
3043#define SIGN_BIT_HIGH(_int, _len) ((((uint64_t)1) << (((_len) << 3) - 1)) & (_int))
3044#define SIGN_PROMOTE(_int, _len) ((_len) < sizeof(_int) ? \
3045 (_int) | (~((__typeof__(_int))0)) << ((_len) << 3) : (_int))
3046
3047#if !defined(NDEBUG) || defined(STATIC_ANALYZER)
3048 /*
3049 * Helps catch invalid fr_value_box_field_sizes
3050 * entries, and shuts up clang analyzer.
3051 */
3052 if (!fr_cond_assert_msg(len > 0, "Invalid cast from %s to %s. "
3053 "invalid source type len, expected > 0, got %zu",
3054 fr_type_to_str(src->type),
3055 fr_type_to_str(dst_type),
3056 len)) return -1;
3057
3058 if (!fr_cond_assert_msg(len <= sizeof(uint64_t),
3059 "Invalid cast from %s to %s. "
3060 "invalid source type len, expected <= %zu, got %zu",
3061 fr_type_to_str(src->type),
3062 fr_type_to_str(dst_type),
3063 sizeof(uint64_t), len)) return -1;
3064#endif
3065
3066 switch (src->type) {
3067 /*
3068 * Dates are always represented in nanoseconds
3069 * internally, but when we convert to another
3070 * integer type, we scale appropriately.
3071 *
3072 * i.e. if the attribute value resolution is
3073 * seconds, then the integer value is
3074 * nanoseconds -> seconds.
3075 */
3076 case FR_TYPE_DATE:
3077 {
3079 if (src->enumv) res = src->enumv->flags.flag_time_res;
3080
3081 tmp = fr_unix_time_to_integer(src->vb_date, res);
3082 }
3083 break;
3084
3085 /*
3086 * Same deal with time deltas. Note that
3087 * even though we store the value as an
3088 * unsigned integer, it'll be cast to a
3089 * signed integer for comparisons.
3090 */
3091 case FR_TYPE_TIME_DELTA:
3092 {
3094
3095 if (src->enumv) res = src->enumv->flags.flag_time_res;
3096
3097 tmp = (uint64_t)fr_time_delta_to_integer(src->vb_time_delta, res);
3098 }
3099 break;
3100
3101 default:
3102#ifdef WORDS_BIGENDIAN
3103 memcpy(((uint8_t *)&tmp) + (sizeof(tmp) - len),
3104 fr_value_box_raw(src, src->type), len);
3105#else
3106 memcpy(&tmp, fr_value_box_raw(src, src->type), len);
3107#endif
3108 break;
3109 }
3110
3111 min = fr_value_box_integer_min[dst_type];
3112
3113 /*
3114 * Sign promote the input if the source type is
3115 * signed, and the high bit is set.
3116 */
3117 if (fr_value_box_integer_min[src->type] < 0) {
3118 if (SIGN_BIT_HIGH(tmp, len)) tmp = SIGN_PROMOTE(tmp, len);
3119
3120 if ((int64_t)tmp < min) {
3121 fr_strerror_printf("Invalid cast from %s to %s. %"PRId64" "
3122 "outside value range %"PRId64"-%"PRIu64,
3123 fr_type_to_str(src->type),
3124 fr_type_to_str(dst_type),
3125 (int64_t)tmp,
3126 min, fr_value_box_integer_max[dst_type]);
3127 return -1;
3128 }
3129 } else if (tmp > fr_value_box_integer_max[dst_type]) {
3130 fr_strerror_printf("Invalid cast from %s to %s. %"PRIu64" "
3131 "outside value range 0-%"PRIu64,
3132 fr_type_to_str(src->type),
3133 fr_type_to_str(dst_type),
3134 tmp, fr_value_box_integer_max[dst_type]);
3135 return -1;
3136 }
3137
3138 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
3139 switch (dst_type) {
3140 case FR_TYPE_DATE:
3141 {
3142 bool overflow;
3144 if (dst->enumv) res = dst->enumv->flags.flag_time_res;
3145
3146 dst->vb_date = fr_unix_time_from_integer(&overflow, tmp, res);
3147 if (overflow) {
3148 fr_strerror_const("Input to date type would overflow");
3149 return -1;
3150 }
3151 }
3152 break;
3153
3154 case FR_TYPE_TIME_DELTA:
3155 {
3156 bool overflow;
3158 if (dst->enumv) res = dst->enumv->flags.flag_time_res;
3159
3160 dst->vb_time_delta = fr_time_delta_from_integer(&overflow, tmp, res);
3161 if (overflow) {
3162 fr_strerror_const("Input to time_delta type would overflow");
3163 return -1;
3164 }
3165 }
3166 break;
3167
3168 default:
3169#ifdef WORDS_BIGENDIAN
3170 memcpy(fr_value_box_raw(dst, dst->type),
3171 ((uint8_t *)&tmp) + (sizeof(tmp) - len), fr_value_box_field_sizes[dst_type]);
3172#else
3173 memcpy(fr_value_box_raw(dst, dst->type),
3174 &tmp, fr_value_box_field_sizes[dst_type]);
3175#endif
3176 break;
3177 }
3178
3179 return 0;
3180}
3181
3182/** Convert any value to a signed or unsigned integer
3183 *
3184 * @param ctx unused.
3185 * @param dst Where to write result of casting.
3186 * @param dst_type to cast to.
3187 * @param dst_enumv enumeration values.
3188 * @param src Input data.
3189 */
3190static inline int fr_value_box_cast_to_integer(TALLOC_CTX *ctx, fr_value_box_t *dst,
3191 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
3192 fr_value_box_t const *src)
3193{
3194 switch (src->type) {
3195 case FR_TYPE_STRING:
3196 return fr_value_box_from_str(ctx, dst, dst_type, dst_enumv,
3197 src->vb_strvalue, src->vb_length,
3198 NULL);
3199
3200 case FR_TYPE_OCTETS:
3201 return fr_value_box_fixed_size_from_octets(dst, dst_type, dst_enumv, src);
3202
3203 case FR_TYPE_INTEGER:
3204 fr_value_box_init(dst, dst_type, dst_enumv, false);
3205 return fr_value_box_cast_integer_to_integer(ctx, dst, dst_type, dst_enumv, src);
3206
3207 case FR_TYPE_IPV4_ADDR:
3209 {
3210 fr_value_box_t tmp;
3211
3212 switch (dst_type) {
3213 case FR_TYPE_UINT32:
3214 case FR_TYPE_INT64:
3215 case FR_TYPE_UINT64:
3216 case FR_TYPE_DATE:
3217 case FR_TYPE_TIME_DELTA:
3218 break;
3219
3220 default:
3221 goto bad_cast;
3222 }
3223
3224 fr_value_box_init(&tmp, FR_TYPE_UINT32, src->enumv, src->tainted);
3225 memcpy(&tmp.vb_uint32, &src->vb_ip.addr.v4, sizeof(tmp.vb_uint32));
3226 fr_value_box_hton(&tmp, &tmp);
3227 return fr_value_box_cast_integer_to_integer(ctx, dst, dst_type, dst_enumv, &tmp);
3228 }
3229
3230 case FR_TYPE_ETHERNET:
3231 {
3232 fr_value_box_t tmp;
3233
3234 switch (dst_type) {
3235 case FR_TYPE_INT64:
3236 case FR_TYPE_UINT64:
3237 case FR_TYPE_DATE:
3238 case FR_TYPE_TIME_DELTA:
3239 break;
3240
3241 default:
3242 goto bad_cast;
3243 }
3244
3245 fr_value_box_init(&tmp, FR_TYPE_UINT64, src->enumv, src->tainted);
3246 memcpy(((uint8_t *)&tmp.vb_uint64) + (sizeof(tmp.vb_uint64) - sizeof(src->vb_ether)),
3247 &src->vb_ether, sizeof(src->vb_ether));
3248#ifndef WORDS_BIGENDIAN
3249 /*
3250 * Ethernet addresses are always stored bigendian,
3251 * convert to native on little endian systems
3252 */
3253 fr_value_box_hton(&tmp, &tmp);
3254#endif
3255 return fr_value_box_cast_integer_to_integer(ctx, dst, dst_type, dst_enumv, &tmp);
3256 }
3257
3258 case FR_TYPE_IFID:
3259 {
3260 switch (dst_type) {
3261 case FR_TYPE_UINT64:
3262 break;
3263
3264 default:
3265 goto bad_cast;
3266 }
3267
3268 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
3269 dst->vb_uint64 = fr_nbo_to_uint64(&src->vb_ifid[0]);
3270 return 0;
3271 }
3272
3273 case FR_TYPE_FLOAT32:
3274 if (src->vb_float32 < (double) fr_value_box_integer_min[dst_type]) {
3275 underflow:
3276 fr_strerror_const("Source value for cast would underflow destination type");
3277 return -1;
3278 }
3279
3280 if (src->vb_float32 > (double) fr_value_box_integer_max[dst_type]) {
3281 overflow:
3282 fr_strerror_const("Source value for cast would overflow destination type");
3283 return -1;
3284 }
3285
3286 switch (dst_type) {
3287 case FR_TYPE_UINT8:
3288 dst->vb_uint8 = src->vb_float32;
3289 break;
3290
3291 case FR_TYPE_UINT16:
3292 dst->vb_uint16 = src->vb_float32;
3293 break;
3294
3295 case FR_TYPE_UINT32:
3296 dst->vb_uint32 = src->vb_float32;
3297 break;
3298
3299 case FR_TYPE_UINT64:
3300 dst->vb_uint64 = src->vb_float32;
3301 break;
3302
3303 case FR_TYPE_INT8:
3304 dst->vb_int8 = src->vb_float32;
3305 break;
3306
3307 case FR_TYPE_INT16:
3308 dst->vb_int16 = src->vb_float32;
3309 break;
3310
3311 case FR_TYPE_INT32:
3312 dst->vb_int32 = src->vb_float32;
3313 break;
3314
3315 case FR_TYPE_INT64:
3316 dst->vb_int64 = src->vb_float32;
3317 break;
3318
3319 case FR_TYPE_SIZE:
3320 dst->vb_size = src->vb_float32;
3321 break;
3322
3323 case FR_TYPE_DATE: {
3324 int64_t sec, nsec;
3325
3326 sec = src->vb_float32;
3327 sec *= NSEC;
3328 nsec = ((src->vb_float32 * NSEC) - ((float) sec));
3329
3330 dst->vb_date = fr_unix_time_from_nsec(sec + nsec);
3331 }
3332 break;
3333
3334 case FR_TYPE_TIME_DELTA: {
3335 int64_t sec, nsec;
3336 int64_t res = NSEC;
3337 bool fail = false;
3338
3339 if (dst->enumv) res = fr_time_multiplier_by_res[dst->enumv->flags.flag_time_res];
3340
3341 sec = src->vb_float32;
3342 sec *= res;
3343 nsec = ((src->vb_float32 * res) - ((double) sec));
3344
3345 dst->vb_time_delta = fr_time_delta_from_integer(&fail, sec + nsec,
3346 dst->enumv ? dst->enumv->flags.flag_time_res : FR_TIME_RES_NSEC);
3347 if (fail) goto overflow;
3348 }
3349 break;
3350
3351 default:
3352 goto bad_cast;
3353 }
3354 return 0;
3355
3356 case FR_TYPE_FLOAT64:
3357 if (src->vb_float64 < (double) fr_value_box_integer_min[dst_type]) goto underflow;
3358
3359 if (src->vb_float64 > (double) fr_value_box_integer_max[dst_type]) goto overflow;
3360
3361 switch (dst_type) {
3362 case FR_TYPE_UINT8:
3363 dst->vb_uint8 = src->vb_float64;
3364 break;
3365
3366 case FR_TYPE_UINT16:
3367 dst->vb_uint16 = src->vb_float64;
3368 break;
3369
3370 case FR_TYPE_UINT32:
3371 dst->vb_uint32 = src->vb_float64;
3372 break;
3373
3374 case FR_TYPE_UINT64:
3375 dst->vb_uint64 = src->vb_float64;
3376 break;
3377
3378 case FR_TYPE_INT8:
3379 dst->vb_int8 = src->vb_float64;
3380 break;
3381
3382 case FR_TYPE_INT16:
3383 dst->vb_int16 = src->vb_float64;
3384 break;
3385
3386 case FR_TYPE_INT32:
3387 dst->vb_int32 = src->vb_float64;
3388 break;
3389
3390 case FR_TYPE_INT64:
3391 dst->vb_int64 = src->vb_float64;
3392 break;
3393
3394 case FR_TYPE_SIZE:
3395 dst->vb_size = src->vb_float64;
3396 break;
3397
3398 case FR_TYPE_DATE: {
3399 int64_t sec, nsec;
3400
3401 sec = src->vb_float64;
3402 sec *= NSEC;
3403 nsec = ((src->vb_float64 * NSEC) - ((double) sec));
3404
3405 dst->vb_date = fr_unix_time_from_nsec(sec + nsec);
3406 }
3407 break;
3408
3409 case FR_TYPE_TIME_DELTA: {
3410 int64_t sec, nsec;
3411 int64_t res = NSEC;
3412 bool fail = false;
3413
3414 if (dst->enumv) res = fr_time_multiplier_by_res[dst->enumv->flags.flag_time_res];
3415
3416 sec = src->vb_float64;
3417 sec *= res;
3418 nsec = ((src->vb_float64 * res) - ((double) sec));
3419
3420 dst->vb_time_delta = fr_time_delta_from_integer(&fail, sec + nsec,
3421 dst->enumv ? dst->enumv->flags.flag_time_res : FR_TIME_RES_NSEC);
3422 if (fail) goto overflow;
3423 }
3424 break;
3425
3426 default:
3427 goto bad_cast;
3428 }
3429 return 0;
3430
3431 default:
3432 break;
3433 }
3434
3435bad_cast:
3436 fr_strerror_printf("Invalid cast from %s to %s. Unsupported",
3437 fr_type_to_str(src->type),
3438 fr_type_to_str(dst_type));
3439 return -1;
3440}
3441
3442/** Convert any value to a floating point value
3443 *
3444 * @param ctx unused.
3445 * @param dst Where to write result of casting.
3446 * @param dst_type to cast to.
3447 * @param dst_enumv enumeration values.
3448 * @param src Input data.
3449 */
3450static inline int fr_value_box_cast_to_float(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst,
3451 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
3452 fr_value_box_t const *src)
3453{
3454 double num;
3455
3456 switch (src->type) {
3457 case FR_TYPE_FLOAT32:
3458 if (dst_type == FR_TYPE_FLOAT64) {
3459 num = (double) src->vb_float32;
3460 goto good_cast;
3461 }
3462
3463 goto bad_cast;
3464
3465 case FR_TYPE_FLOAT64:
3466 if (dst_type == FR_TYPE_FLOAT32) {
3467 num = src->vb_float64;
3468 goto good_cast;
3469 }
3470
3471 goto bad_cast;
3472
3473 case FR_TYPE_BOOL:
3474 num = src->vb_bool;
3475 goto good_cast;
3476
3477 case FR_TYPE_INT8:
3478 num = src->vb_int8;
3479 goto good_cast;
3480
3481 case FR_TYPE_INT16:
3482 num = src->vb_int16;
3483 goto good_cast;
3484
3485 case FR_TYPE_INT32:
3486 num = src->vb_int32;
3487 goto good_cast;
3488
3489 case FR_TYPE_INT64:
3490 num = src->vb_int64;
3491 goto good_cast;
3492
3493 case FR_TYPE_UINT8:
3494 num = src->vb_uint8;
3495 goto good_cast;
3496
3497 case FR_TYPE_UINT16:
3498 num = src->vb_uint16;
3499 goto good_cast;
3500
3501 case FR_TYPE_UINT32:
3502 num = src->vb_uint32;
3503 goto good_cast;
3504
3505 case FR_TYPE_UINT64:
3506 num = src->vb_uint64;
3507 goto good_cast;
3508
3509 case FR_TYPE_DATE:
3510 /*
3511 * Unix times are in nanoseconds
3512 */
3513 num = fr_unix_time_unwrap(src->vb_date);
3514 num /= NSEC;
3515 goto good_cast;
3516
3517 case FR_TYPE_TIME_DELTA:
3518 /*
3519 * Time deltas are in nanoseconds, but scaled.
3520 */
3521 num = fr_time_delta_unwrap(src->vb_time_delta);
3522 if (src->enumv) {
3523 num /= fr_time_multiplier_by_res[src->enumv->flags.flag_time_res];
3524 } else {
3525 num /= NSEC;
3526 }
3527 goto good_cast;
3528
3529 case FR_TYPE_SIZE:
3530 num = src->vb_size;
3531
3532 good_cast:
3533 fr_value_box_init(dst, dst_type, dst_enumv, src->tainted);
3535
3536 if (dst_type == FR_TYPE_FLOAT32) {
3537 dst->vb_float32 = num;
3538 } else {
3539 dst->vb_float64 = num;
3540 }
3541 return 0;
3542
3543 default:
3544 break;
3545 }
3546
3547bad_cast:
3548 fr_strerror_printf("Invalid cast from %s to %s. Unsupported",
3549 fr_type_to_str(src->type),
3550 fr_type_to_str(dst_type));
3551 return -1;
3552}
3553
3554
3555/** Convert one type of fr_value_box_t to another
3556 *
3557 * This should be the canonical function used to convert between INTERNAL data formats.
3558 *
3559 * If you want to convert from PRESENTATION format, use #fr_value_box_from_substr.
3560 *
3561 * @note src and dst must not be the same box. We do not support casting in place.
3562 *
3563 * @param ctx to allocate buffers in (usually the same as dst)
3564 * @param dst Where to write result of casting.
3565 * @param dst_type to cast to.
3566 * @param dst_enumv Aliases for values contained within this fr_value_box_t.
3567 * If #fr_value_box_t is passed to #fr_value_box_aprint
3568 * names will be printed instead of actual value.
3569 * @param src Input data.
3570 * @return
3571 * - 0 on success.
3572 * - -1 on failure.
3573 */
3574int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst,
3575 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
3576 fr_value_box_t const *src)
3577{
3578 if (!fr_cond_assert(dst_type != FR_TYPE_NULL)) return -1;
3579 if (!fr_cond_assert(src != dst)) return -1;
3580 if (!fr_cond_assert(src->type != FR_TYPE_NULL)) return -1;
3581
3582 if (fr_type_is_non_leaf(dst_type)) {
3583 fr_strerror_printf("Invalid cast from %s to %s. Can only cast simple data types",
3584 fr_type_to_str(src->type),
3585 fr_type_to_str(dst_type));
3586 return -1;
3587 }
3588
3589 /*
3590 * If it's the same type, copy, but set the enumv
3591 * in the destination box to be the one provided.
3592 *
3593 * The theory here is that the attribute value isn't
3594 * being converted into its presentation format and
3595 * re-parsed, and the enumv names only get applied
3596 * when converting internal values to/from strings,
3597 * so it's OK just to swap out the enumv.
3598 *
3599 * If there's a compelling case in the future we
3600 * might revisit this, but it'd likely mean fixing
3601 * all the casting functions to treat any value
3602 * with an enumv as a string, which seems weird.
3603 */
3604 if (dst_type == src->type) {
3605 int ret;
3606
3607 ret = fr_value_box_copy(ctx, dst, src);
3608 if (ret < 0) return ret;
3609
3610 dst->enumv = dst_enumv;
3611
3612 return ret;
3613 }
3614
3615 /*
3616 * Initialise dst
3617 */
3618 fr_value_box_init(dst, dst_type, NULL, src->tainted);
3619
3620 /*
3621 * Dispatch to specialised cast functions
3622 */
3623 switch (dst_type) {
3624 case FR_TYPE_STRING:
3625 return fr_value_box_cast_to_strvalue(ctx, dst, dst_type, dst_enumv, src);
3626
3627 case FR_TYPE_OCTETS:
3628 return fr_value_box_cast_to_octets(ctx, dst, dst_type, dst_enumv, src);
3629
3630 case FR_TYPE_IPV4_ADDR:
3631 return fr_value_box_cast_to_ipv4addr(ctx, dst, dst_type, dst_enumv, src);
3632
3634 return fr_value_box_cast_to_ipv4prefix(ctx, dst, dst_type, dst_enumv, src);
3635
3636 case FR_TYPE_IPV6_ADDR:
3637 return fr_value_box_cast_to_ipv6addr(ctx, dst, dst_type, dst_enumv, src);
3638
3640 return fr_value_box_cast_to_ipv6prefix(ctx, dst, dst_type, dst_enumv, src);
3641
3644 break;
3645 /*
3646 * Need func
3647 */
3648 case FR_TYPE_IFID:
3649 break;
3650
3651 case FR_TYPE_ETHERNET:
3652 return fr_value_box_cast_to_ethernet(ctx, dst, dst_type, dst_enumv, src);
3653
3654 case FR_TYPE_BOOL:
3655 return fr_value_box_cast_to_bool(ctx, dst, dst_type, dst_enumv, src);
3656
3657 case FR_TYPE_DATE:
3658 if (src->type != FR_TYPE_TIME_DELTA) return fr_value_box_cast_to_integer(ctx, dst, dst_type, dst_enumv, src);
3659
3660 if (fr_time_delta_isneg(src->vb_time_delta)) {
3661 fr_strerror_const("Input to data type would underflow");
3662 return -1;
3663 }
3664
3666 dst->enumv = dst_enumv;
3667 dst->vb_date = fr_unix_time_wrap(fr_time_delta_unwrap(src->vb_time_delta));
3668 return 0;
3669
3670 case FR_TYPE_TIME_DELTA:
3671 /*
3672 * Unix time cast to time_delta is just nanoseconds since the epoch.
3673 *
3674 * Note that we do NOT change time resolution, but we DO change enumv. Both unix time
3675 * and time_delta are tracked internally as nanoseconds, and the only use of precision is
3676 * for printing / parsing.
3677 */
3678 if (src->type == FR_TYPE_DATE) {
3679 uint64_t when;
3680
3681 when = fr_unix_time_unwrap(src->vb_date);
3682 if (when > INT64_MAX) {
3683 fr_strerror_const("Input to data type would overflow");
3684 return -1;
3685 }
3686
3688 dst->enumv = dst_enumv;
3689 dst->vb_time_delta = fr_time_delta_wrap((int64_t) when);
3690 return 0;
3691 }
3693
3694 case FR_TYPE_UINT8:
3695 case FR_TYPE_UINT16:
3696 case FR_TYPE_UINT32:
3697 case FR_TYPE_UINT64:
3698 case FR_TYPE_INT8:
3699 case FR_TYPE_INT16:
3700 case FR_TYPE_INT32:
3701 case FR_TYPE_INT64:
3702 case FR_TYPE_SIZE:
3703 return fr_value_box_cast_to_integer(ctx, dst, dst_type, dst_enumv, src);
3704
3705 case FR_TYPE_FLOAT32:
3706 case FR_TYPE_FLOAT64:
3707 if (fr_type_is_fixed_size(src->type)) {
3708 return fr_value_box_cast_to_float(ctx, dst, dst_type, dst_enumv, src);
3709 }
3710 break; /* use generic string/octets stuff below */
3711
3712 /*
3713 * Invalid types for casting (should have been caught earlier)
3714 */
3715 case FR_TYPE_VALUE_BOX:
3716 case FR_TYPE_STRUCTURAL:
3717 case FR_TYPE_NULL:
3718 case FR_TYPE_VOID:
3719 case FR_TYPE_MAX:
3720 fr_strerror_printf("Invalid cast from %s to %s. Invalid destination type",
3721 fr_type_to_str(src->type),
3722 fr_type_to_str(dst_type));
3723 return -1;
3724 }
3725
3726 /*
3727 * Deserialise a fr_value_box_t
3728 */
3729 if (src->type == FR_TYPE_STRING) return fr_value_box_from_str(ctx, dst, dst_type, dst_enumv,
3730 src->vb_strvalue, src->vb_length,
3731 NULL);
3732
3733 if (src->type == FR_TYPE_OCTETS) {
3734 fr_value_box_t tmp;
3735
3736 if (src->vb_length < network_min_size(dst_type)) {
3737 fr_strerror_printf("Invalid cast from %s to %s. Source is length %zu is smaller than "
3738 "destination type size %zu",
3739 fr_type_to_str(src->type),
3740 fr_type_to_str(dst_type),
3741 src->vb_length,
3742 network_min_size(dst_type));
3743 return -1;
3744 }
3745
3746 if (src->vb_length > network_max_size(dst_type)) {
3747 fr_strerror_printf("Invalid cast from %s to %s. Source length %zu is greater than "
3748 "destination type size %zu",
3749 fr_type_to_str(src->type),
3750 fr_type_to_str(dst_type),
3751 src->vb_length,
3752 network_max_size(dst_type));
3753 return -1;
3754 }
3755
3756 fr_value_box_init(&tmp, dst_type, NULL, false);
3757
3758 /*
3759 * Copy the raw octets into the datum of a value_box
3760 * inverting bytesex for uint32s (if LE).
3761 */
3762 memcpy(&tmp.datum, src->vb_octets, fr_value_box_field_sizes[dst_type]);
3763 tmp.type = dst_type;
3764 dst->enumv = dst_enumv;
3765
3766 fr_value_box_hton(dst, &tmp);
3767 fr_value_box_safety_copy(dst, src);
3768 return 0;
3769 }
3770
3771 memcpy(&dst->datum, &src->datum, fr_value_box_field_sizes[src->type]);
3772
3774 dst->enumv = dst_enumv;
3775
3776 return 0;
3777}
3778
3779/** Convert one type of fr_value_box_t to another in place
3780 *
3781 * This should be the canonical function used to convert between INTERNAL data formats.
3782 *
3783 * If you want to convert from PRESENTATION format, use #fr_value_box_from_substr.
3784 *
3785 * @param ctx to allocate buffers in (usually the same as dst)
3786 * @param vb to cast.
3787 * @param dst_type to cast to.
3788 * @param dst_enumv Aliases for values contained within this fr_value_box_t.
3789 * If #fr_value_box_t is passed to #fr_value_box_aprint
3790 * names will be printed instead of actual value.
3791 * @return
3792 * - 0 on success.
3793 * - -1 on failure.
3794 */
3796 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
3797{
3798 fr_value_box_t tmp;
3799 /*
3800 * Store list pointers to restore later - fr_value_box_cast clears them
3801 */
3802 fr_value_box_entry_t entry = vb->entry;
3803
3804 /*
3805 * Simple case, destination type and current
3806 * type are the same.
3807 */
3808 if (vb->type == dst_type) {
3809 vb->enumv = dst_enumv; /* Update the enumv as this may be different */
3810 return 0;
3811 }
3812
3813 /*
3814 * Copy meta data and any existing buffers to
3815 * a temporary box. We then clear that value
3816 * box after the cast has been completed,
3817 * freeing any old buffers.
3818 */
3819 fr_value_box_copy_shallow(NULL, &tmp, vb);
3820
3821 if (fr_value_box_cast(ctx, vb, dst_type, dst_enumv, &tmp) < 0) {
3822 /*
3823 * On error, make sure the original
3824 * box is left in a consistent state.
3825 */
3826 fr_value_box_copy_shallow(NULL, vb, &tmp);
3827 vb->entry = entry;
3828 return -1;
3829 }
3830 fr_value_box_clear_value(&tmp); /* Clear out any old buffers */
3831
3832 /*
3833 * Restore list pointers
3834 */
3835 vb->entry = entry;
3836
3837 return 0;
3838}
3839
3840/** Assign a #fr_value_box_t value from an #fr_ipaddr_t
3841 *
3842 * Automatically determines the type of the value box from the ipaddr address family
3843 * and the length of the prefix field.
3844 *
3845 * @param[in] dst to assign ipaddr to.
3846 * @param[in] enumv Aliases for values.
3847 * @param[in] ipaddr to copy address from.
3848 * @param[in] tainted Whether the value came from a trusted source.
3849 * @return
3850 * - 0 on success.
3851 * - -1 on failure.
3852 */
3853int fr_value_box_ipaddr(fr_value_box_t *dst, fr_dict_attr_t const *enumv, fr_ipaddr_t const *ipaddr, bool tainted)
3854{
3856
3857 switch (ipaddr->af) {
3858 case AF_INET:
3860 break;
3861
3862 case AF_INET6:
3864 break;
3865
3866 default:
3867 fr_strerror_printf("Invalid address family %i", ipaddr->af);
3868 return -1;
3869 }
3870
3871 fr_value_box_init(dst, type, enumv, tainted);
3872 memcpy(&dst->vb_ip, ipaddr, sizeof(dst->vb_ip));
3873
3874 return 0;
3875}
3876
3877/** Unbox an IP address performing a type check
3878 *
3879 * @param[out] dst Where to copy the IP address to.
3880 * @param[in] src Where to copy the IP address from.
3881 * @return
3882 * - 0 on success.
3883 * - -1 on type mismatch.
3884 */
3886{
3887 if (!fr_type_is_ip(src->type)) {
3888 fr_strerror_printf("Unboxing failed. Needed IPv4/6 addr/prefix, had type %s",
3889 fr_type_to_str(src->type));
3890 return -1;
3891 }
3892
3893 memcpy(dst, &src->vb_ip, sizeof(*dst));
3894
3895 return 0;
3896}
3897
3898/** Clear/free any existing value
3899 *
3900 * @note Do not use on uninitialised memory.
3901 *
3902 * @param[in] data to clear.
3903 */
3905{
3906 switch (data->type) {
3907 case FR_TYPE_OCTETS:
3908 case FR_TYPE_STRING:
3909 if (data->secret) memset_explicit(data->datum.ptr, 0, data->vb_length);
3910 talloc_free(data->datum.ptr);
3911 break;
3912
3913 case FR_TYPE_GROUP:
3914 /*
3915 * Depth first freeing of children
3916 *
3917 * This ensures orderly freeing, regardless
3918 * of talloc hierarchy.
3919 */
3920 {
3921 fr_value_box_t *vb = NULL;
3922
3923 while ((vb = fr_value_box_list_next(&data->vb_group, vb))) {
3925 talloc_free(vb);
3926 }
3927 }
3928 return;
3929
3930 case FR_TYPE_NULL:
3931 return;
3932
3933 default:
3934 break;
3935 }
3936
3937 memset(&data->datum, 0, sizeof(data->datum));
3938}
3939
3940/** Clear/free any existing value and metadata
3941 *
3942 * @note Do not use on uninitialised memory.
3943 *
3944 * @param[in] data to clear.
3945 */
3951
3952/** Copy value data verbatim duplicating any buffers
3953 *
3954 * @note Will free any exiting buffers associated with the dst #fr_value_box_t.
3955 *
3956 * @param ctx To allocate buffers in.
3957 * @param dst Where to copy value_box to.
3958 * @param src Where to copy value_box from.
3959 * @return
3960 * - 0 on success.
3961 * - -1 on failure.
3962 */
3963int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
3964{
3965 switch (src->type) {
3966 default:
3967 fr_value_box_memcpy_out(fr_value_box_raw(dst, src->type), src);
3968 fr_value_box_copy_meta(dst, src);
3969 break;
3970
3971 case FR_TYPE_NULL:
3972 fr_value_box_copy_meta(dst, src);
3973 break;
3974
3975 case FR_TYPE_STRING:
3976 {
3977 char *str = NULL;
3978
3979 /*
3980 * Zero length strings still have a one uint8 buffer
3981 */
3982 str = talloc_bstrndup(ctx, src->vb_strvalue, src->vb_length);
3983 if (!str) {
3984 fr_strerror_const("Failed allocating string buffer");
3985 return -1;
3986 }
3987 dst->vb_strvalue = str;
3988 fr_value_box_copy_meta(dst, src);
3989 }
3990 break;
3991
3992 case FR_TYPE_OCTETS:
3993 {
3994 uint8_t *bin;
3995
3996 if (src->vb_length) {
3997 bin = talloc_memdup(ctx, src->vb_octets, src->vb_length);
3998 if (!bin) {
3999 fr_strerror_const("Failed allocating octets buffer");
4000 return -1;
4001 }
4002 talloc_set_type(bin, uint8_t);
4003 } else {
4004 bin = talloc_array(ctx, uint8_t, 0);
4005 }
4006 dst->vb_octets = bin;
4007 fr_value_box_copy_meta(dst, src);
4008 }
4009 break;
4010
4011 case FR_TYPE_GROUP:
4012 {
4013 fr_value_box_t *child = NULL;
4014
4015 fr_value_box_copy_meta(dst, src); /* Initialises group child dlist */
4016
4017 while ((child = fr_value_box_list_next(&src->vb_group, child))) {
4018 fr_value_box_t *new;
4019
4020 /*
4021 * Build out the child
4022 */
4023 new = fr_value_box_alloc_null(ctx);
4024 if (unlikely(!new)) {
4025 group_error:
4026 fr_strerror_const("Failed duplicating group child");
4027 fr_value_box_list_talloc_free(&dst->vb_group);
4028 return -1;
4029 }
4030
4031 /*
4032 * Populate it with the data from the original child.
4033 *
4034 * We do NOT update the dst safety. The individual boxes have safety. A group
4035 * doesn't.
4036 */
4037 if (unlikely(fr_value_box_copy(new, new, child) < 0)) goto group_error;
4038 fr_value_box_list_insert_tail(&dst->vb_group, new);
4039 }
4040 }
4041 break;
4042
4043 case FR_TYPE_TLV:
4044 case FR_TYPE_STRUCT:
4045 case FR_TYPE_VSA:
4046 case FR_TYPE_VENDOR:
4047 case FR_TYPE_VALUE_BOX:
4048 case FR_TYPE_VOID:
4049 case FR_TYPE_MAX:
4050 fr_strerror_printf("Cannot copy data type '%s'", fr_type_to_str(src->type));
4051 return -1;
4052 }
4053
4054 return 0;
4055}
4056
4057/** Perform a shallow copy of a value_box
4058 *
4059 * Like #fr_value_box_copy, but does not duplicate the buffers of the src value_box.
4060 *
4061 * For #FR_TYPE_STRING and #FR_TYPE_OCTETS adds a reference from ctx so that the
4062 * buffer cannot be freed until the ctx is freed.
4063 *
4064 * @param[in] ctx to add reference from. If NULL no reference will be added.
4065 * @param[in] dst to copy value to.
4066 * @param[in] src to copy value from.
4067 */
4068void fr_value_box_copy_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *src)
4069{
4070 switch (src->type) {
4071 default:
4072 fr_value_box_copy(NULL, dst, src);
4073 break;
4074
4075 case FR_TYPE_STRING:
4076 case FR_TYPE_OCTETS:
4077 dst->datum.ptr = ctx ? talloc_reference(ctx, src->datum.ptr) : src->datum.ptr;
4078 fr_value_box_copy_meta(dst, src);
4079 break;
4080 }
4081}
4082
4083/** Copy value data verbatim moving any buffers to the specified context
4084 *
4085 * @param[in] ctx to allocate any new buffers in.
4086 * @param[in] dst to copy value to.
4087 * @param[in] src to copy value from.
4088 * @return
4089 * - 0 on success.
4090 * - -1 on failure.
4091 */
4092int fr_value_box_steal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t *src)
4093{
4094 if (!fr_cond_assert(src->type != FR_TYPE_NULL)) return -1;
4095
4096 switch (src->type) {
4097 default:
4098 return fr_value_box_copy(ctx, dst, src);
4099
4100 case FR_TYPE_STRING:
4101 {
4102 char const *str;
4103
4104 str = talloc_steal(ctx, src->vb_strvalue);
4105 if (!str) {
4106 fr_strerror_const("Failed stealing string buffer");
4107 return -1;
4108 }
4109 talloc_set_type(str, char);
4110 dst->vb_strvalue = str;
4111 fr_value_box_copy_meta(dst, src);
4112 memset(&src->datum, 0, sizeof(src->datum));
4113 }
4114 return 0;
4115
4116 case FR_TYPE_OCTETS:
4117 {
4118 uint8_t const *bin;
4119
4120 bin = talloc_steal(ctx, src->vb_octets);
4121 if (!bin) {
4122 fr_strerror_const("Failed stealing octets buffer");
4123 return -1;
4124 }
4125 talloc_set_type(bin, uint8_t);
4126
4127 dst->vb_octets = bin;
4128 fr_value_box_copy_meta(dst, src);
4129 memset(&src->datum, 0, sizeof(src->datum));
4130 }
4131 return 0;
4132
4133 case FR_TYPE_GROUP:
4134 {
4135 fr_value_box_t *child;
4136
4137 while ((child = fr_value_box_list_pop_head(&src->vb_group))) {
4138 child = talloc_steal(ctx, child);
4139 if (unlikely(!child)) {
4140 fr_strerror_const("Failed stealing child");
4141 return -1;
4142 }
4143 fr_value_box_list_insert_tail(&dst->vb_group, child);
4144 }
4145 }
4146 return 0;
4147 }
4148}
4149
4150/** Copy a nul terminated string to a #fr_value_box_t
4151 *
4152 * @param[in] ctx to allocate any new buffers in.
4153 * @param[in] dst to assign new buffer to.
4154 * @param[in] enumv Aliases for values.
4155 * @param[in] src a nul terminated buffer.
4156 * @param[in] tainted Whether the value came from a trusted source.
4157 * @return
4158 * - 0 on success.
4159 * - -1 on failure.
4160 */
4161int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4162 char const *src, bool tainted)
4163{
4164 char const *str;
4165
4166 str = talloc_typed_strdup(ctx, src);
4167 if (!str) {
4168 fr_strerror_const("Failed allocating string buffer");
4169 return -1;
4170 }
4171
4172 fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
4173 dst->vb_strvalue = str;
4174 dst->vb_length = talloc_array_length(str) - 1;
4175
4176 return 0;
4177}
4178
4179/** Trim the length of the string buffer to match the length of the C string
4180 *
4181 * @param[in] ctx to re-alloc the buffer in.
4182 * @param[in,out] vb to trim.
4183 * @return
4184 * - 0 on success.
4185 * - -1 on failure.
4186 */
4187int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb)
4188{
4189 size_t len;
4190 char *str;
4191
4192 if (!fr_cond_assert(vb->type == FR_TYPE_STRING)) return -1;
4193
4194 len = strlen(vb->vb_strvalue);
4195 str = talloc_realloc(ctx, UNCONST(char *, vb->vb_strvalue), char, len + 1);
4196 if (!str) {
4197 fr_strerror_const("Failed re-allocing string buffer");
4198 return -1;
4199 }
4200 vb->vb_length = len;
4201
4202 return 0;
4203}
4204
4205/** Print a formatted string using our internal printf wrapper and assign it to a value box
4206 *
4207 * @param[in] ctx to allocate any new buffers in.
4208 * @param[in] dst to assign new buffer to.
4209 * @param[in] enumv Aliases for values.
4210 * @param[in] fmt The printf format string to process.
4211 * @param[in] tainted Whether the value came from a trusted source.
4212 * @param[in] ap Substitution arguments.
4213 * @return
4214 * - 0 on success.
4215 * - -1 on failure.
4216 */
4217int fr_value_box_vasprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted,
4218 char const *fmt, va_list ap)
4219{
4220 va_list aq;
4221 char *str;
4222
4223 va_copy(aq, ap); /* See vlog_module_failure_msg for why */
4224 str = fr_vasprintf(ctx, fmt, aq);
4225 va_end(aq);
4226
4227 if (!str) return -1;
4228
4229 fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
4230 dst->vb_strvalue = str;
4231 dst->vb_length = talloc_array_length(str) - 1;
4232
4233 return 0;
4234}
4235
4236/** Print a formatted string using our internal printf wrapper and assign it to a value box
4237 *
4238 * @param[in] ctx to allocate any new buffers in.
4239 * @param[in] dst to assign new buffer to.
4240 * @param[in] enumv Aliases for values.
4241 * @param[in] tainted Whether the value came from a trusted source.
4242 * @param[in] fmt The printf format string to process.
4243 * @param[in] ... Substitution arguments.
4244 * @return
4245 * - 0 on success.
4246 * - -1 on failure.
4247 */
4248int fr_value_box_asprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted,
4249 char const *fmt, ...)
4250{
4251 va_list ap;
4252 int ret;
4253
4254 va_start(ap, fmt);
4255 ret = fr_value_box_vasprintf(ctx, dst, enumv, tainted, fmt, ap);
4256 va_end(ap);
4257
4258 return ret;
4259}
4260
4261/** Assign a buffer containing a nul terminated string to a box, but don't copy it
4262 *
4263 * @note Input string will not be duplicated.
4264 *
4265 * @param[in] dst to assign string to.
4266 * @param[in] enumv Aliases for values.
4267 * @param[in] src to copy string from.
4268 * @param[in] tainted Whether the value came from a trusted source.
4269 */
4271 char const *src, bool tainted)
4272{
4273 fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
4274 dst->vb_strvalue = src;
4275 dst->vb_length = strlen(src);
4276}
4277
4278/** Free the existing buffer (if talloced) associated with the valuebox, and replace it with a new one
4279 *
4280 * @note Input string will not be duplicated.
4281 *
4282 * @param[in] vb to replace string in.
4283 * @param[in] src to assign string from.
4284 * @param[in] len of src.
4285 */
4287{
4289 vb->vb_strvalue = src;
4290 vb->vb_length = len < 0 ? strlen(src) : (size_t)len;
4291}
4292
4293/** Alloc and assign an empty \0 terminated string to a #fr_value_box_t
4294 *
4295 * @param[in] ctx to allocate any new buffers in.
4296 * @param[out] out if non-null where to write a pointer to the new buffer.
4297 * @param[in] dst to assign new buffer to.
4298 * @param[in] enumv Aliases for values.
4299 * @param[in] len of buffer to allocate.
4300 * @param[in] tainted Whether the value came from a trusted source.
4301 * @return
4302 * - 0 on success.
4303 * - -1 on failure.
4304 */
4305int fr_value_box_bstr_alloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4306 size_t len, bool tainted)
4307{
4308 char *str;
4309
4310 str = talloc_zero_array(ctx, char, len + 1);
4311 if (!str) {
4312 fr_strerror_const("Failed allocating string buffer");
4313 return -1;
4314 }
4315 str[len] = '\0';
4316
4317 fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
4318 dst->vb_strvalue = str;
4319 dst->vb_length = len;
4320
4321 if (out) *out = str;
4322
4323 return 0;
4324}
4325
4326/** Change the length of a buffer already allocated to a value box
4327 *
4328 * @note Do not use on an uninitialised box.
4329 *
4330 * @param[in] ctx to realloc buffer in.
4331 * @param[out] out if non-null where to write a pointer to the new buffer.
4332 * @param[in] dst to realloc buffer for.
4333 * @param[in] len to realloc to (don't include nul byte).
4334 * @return
4335 * - 0 on success.
4336 * - -1 on failure.
4337 */
4338int fr_value_box_bstr_realloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, size_t len)
4339{
4340 size_t clen;
4341 char *cstr;
4342 char *str;
4343
4344 fr_assert(dst->type == FR_TYPE_STRING);
4345
4346 memcpy(&cstr, &dst->vb_strvalue, sizeof(cstr));
4347
4348 clen = talloc_array_length(dst->vb_strvalue) - 1;
4349 if (clen == len) return 0; /* No change */
4350
4351 str = talloc_realloc(ctx, cstr, char, len + 1);
4352 if (!str) {
4353 fr_strerror_printf("Failed reallocing value box buffer to %zu bytes", len + 1);
4354 return -1;
4355 }
4356
4357 /*
4358 * Zero out the additional bytes
4359 */
4360 if (clen < len) {
4361 memset(str + clen, '\0', (len - clen) + 1);
4362 } else {
4363 cstr[len] = '\0';
4364 }
4365 dst->vb_strvalue = str;
4366 dst->vb_length = len;
4367
4368 if (out) *out = str;
4369
4370 return 0;
4371}
4372
4373/** Copy a string to to a #fr_value_box_t
4374 *
4375 * @param[in] ctx to allocate any new buffers in.
4376 * @param[in] dst to assign buffer to.
4377 * @param[in] enumv Aliases for values.
4378 * @param[in] src a string. May be NULL only if len == 0.
4379 * @param[in] len of src.
4380 * @param[in] tainted Whether the value came from a trusted source.
4381 */
4382int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4383 char const *src, size_t len, bool tainted)
4384{
4385 char const *str;
4386
4387 if (unlikely((len > 0) && !src)) {
4388 fr_strerror_printf("Invalid arguments to %s. Len > 0 (%zu) but src string was NULL",
4389 __FUNCTION__, len);
4390 return -1;
4391 }
4392
4393 str = talloc_bstrndup(ctx, src, len);
4394 if (!str) {
4395 fr_strerror_const("Failed allocating string buffer");
4396 return -1;
4397 }
4398
4399 fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
4400 dst->vb_strvalue = str;
4401 dst->vb_length = len;
4402
4403 return 0;
4404}
4405
4406int fr_value_box_bstrndup_dbuff(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4407 fr_dbuff_t *dbuff, size_t len, bool tainted)
4408{
4409 char *str;
4410
4411 str = talloc_array(ctx, char, len + 1);
4412 if (!str) {
4413 fr_strerror_printf("Failed allocating string buffer");
4414 return -1;
4415 }
4416
4417 if (fr_dbuff_out_memcpy((uint8_t *)str, dbuff, len) < 0) return -1;
4418 str[len] = '\0';
4419
4420 fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
4421 dst->vb_strvalue = str;
4422 dst->vb_length = len;
4423
4424 return 0;
4425}
4426
4427/** Copy a nul terminated talloced buffer to a #fr_value_box_t
4428 *
4429 * Copy a talloced nul terminated buffer, setting fields in the dst value box appropriately.
4430 *
4431 * The buffer must be \0 terminated, or an error will be returned.
4432 *
4433 * @param[in] ctx to allocate any new buffers in.
4434 * @param[in] dst to assign new buffer to.
4435 * @param[in] enumv Aliases for values.
4436 * @param[in] src a talloced nul terminated buffer.
4437 * @param[in] tainted Whether the value came from a trusted source.
4438 * @return
4439 * - 0 on success.
4440 * - -1 on failure.
4441 */
4442int fr_value_box_bstrdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4443 char const *src, bool tainted)
4444{
4445 size_t len;
4446
4447 (void)talloc_get_type_abort_const(src, char);
4448
4449 len = talloc_array_length(src);
4450 if ((len == 0) || (src[len - 1] != '\0')) {
4451 fr_strerror_const("Input buffer not \\0 terminated");
4452 return -1;
4453 }
4454
4455 return fr_value_box_bstrndup(ctx, dst, enumv, src, len - 1, tainted);
4456}
4457
4458/** Assign a string to to a #fr_value_box_t
4459 *
4460 * @param[in] dst to assign new buffer to.
4461 * @param[in] enumv Aliases for values.
4462 * @param[in] src a string.
4463 * @param[in] len of src.
4464 * @param[in] tainted Whether the value came from a trusted source.
4465 */
4467 char const *src, size_t len, bool tainted)
4468{
4469 fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
4470 dst->vb_strvalue = src;
4471 dst->vb_length = len;
4472}
4473
4474/** Assign a talloced buffer containing a nul terminated string to a box, but don't copy it
4475 *
4476 * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed.
4477 *
4478 * @param[in] ctx to add reference from. If NULL no reference will be added.
4479 * @param[in] dst to assign string to.
4480 * @param[in] enumv Aliases for values.
4481 * @param[in] src to copy string from.
4482 * @param[in] tainted Whether the value came from a trusted source.
4483 * @return
4484 * - 0 on success.
4485 * - -1 on failure.
4486 */
4488 char const *src, bool tainted)
4489{
4490 size_t len;
4491
4492 (void) talloc_get_type_abort_const(src, char);
4493
4494 len = talloc_array_length(src);
4495 if ((len == 0) || (src[len - 1] != '\0')) {
4496 fr_strerror_const("Input buffer not \\0 terminated");
4497 return -1;
4498 }
4499
4500 fr_value_box_init(dst, FR_TYPE_STRING, enumv, tainted);
4501 dst->vb_strvalue = ctx ? talloc_reference(ctx, src) : src;
4502 dst->vb_length = len - 1;
4503
4504 return 0;
4505}
4506
4507/** Pre-allocate an octets buffer for filling by the caller
4508 *
4509 * @note Buffer will not be zeroed, as it's assumed the caller will be filling it.
4510 *
4511 * @param[in] ctx to allocate any new buffers in.
4512 * @param[out] out If non-null will be filled with a pointer to the
4513 * new buffer.
4514 * @param[in] dst to assign new buffer to.
4515 * @param[in] enumv Aliases for values.
4516 * @param[in] len of data in the buffer. If 0, a zero length
4517 * talloc buffer will be alloced. dst->vb_octets
4518 * will *NOT* be NULL. You should use the length
4519 * field of the box to determine if any value
4520 * is assigned.
4521 * @param[in] tainted Whether the value came from a trusted source.
4522 * @return
4523 * - 0 on success.
4524 * - -1 on failure.
4525 */
4526int fr_value_box_mem_alloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4527 size_t len, bool tainted)
4528{
4529 uint8_t *bin;
4530
4531 bin = talloc_array(ctx, uint8_t, len);
4532 if (!bin) {
4533 fr_strerror_const("Failed allocating octets buffer");
4534 return -1;
4535 }
4536 talloc_set_type(bin, uint8_t);
4537
4538 fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted);
4539 dst->vb_octets = bin;
4540 dst->vb_length = len;
4541
4542 if (out) *out = bin;
4543
4544 return 0;
4545}
4546
4547/** Change the length of a buffer already allocated to a value box
4548 *
4549 * @note Do not use on an uninitialised box.
4550 *
4551 * @param[in] ctx to realloc buffer in.
4552 * @param[out] out if non-null where to write a pointer to the new buffer.
4553 * @param[in] dst to realloc buffer for.
4554 * @param[in] len to realloc to.
4555 * @return
4556 * - 0 on success.
4557 * - -1 on failure.
4558 */
4559int fr_value_box_mem_realloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, size_t len)
4560{
4561 size_t clen;
4562 uint8_t *cbin;
4563 uint8_t *bin;
4564
4565 fr_assert(dst->type == FR_TYPE_OCTETS);
4566
4567 memcpy(&cbin, &dst->vb_octets, sizeof(cbin));
4568
4569 clen = talloc_array_length(dst->vb_octets);
4570 if (clen == len) return 0; /* No change */
4571
4572 /*
4573 * Realloc the buffer. If the new length is 0, we
4574 * need to call talloc_array() instead of talloc_realloc()
4575 * as talloc_realloc() will fail.
4576 */
4577 if (len > 0) {
4578 bin = talloc_realloc(ctx, cbin, uint8_t, len);
4579 } else {
4580 bin = talloc_array(ctx, uint8_t, 0);
4581 }
4582 if (!bin) {
4583 fr_strerror_printf("Failed reallocing value box buffer to %zu bytes", len);
4584 return -1;
4585 }
4586
4587 /*
4588 * Only free the original buffer once we've allocated
4589 * a new empty array.
4590 */
4591 if (len == 0) talloc_free(cbin);
4592
4593 /*
4594 * Zero out the additional bytes
4595 */
4596 if (clen < len) memset(bin + clen, 0x00, len - clen);
4597 dst->vb_octets = bin;
4598 dst->vb_length = len;
4599
4600 if (out) *out = bin;
4601
4602 return 0;
4603}
4604
4605/** Copy a buffer to a fr_value_box_t
4606 *
4607 * Copy a buffer containing binary data, setting fields in the dst value box appropriately.
4608 *
4609 * @param[in] ctx to allocate any new buffers in.
4610 * @param[in] dst to assign new buffer to.
4611 * @param[in] enumv Aliases for values.
4612 * @param[in] src a buffer.
4613 * @param[in] len of data in the buffer. If 0, a zero length
4614 * talloc buffer will be alloced. dst->vb_octets
4615 * will *NOT* be NULL. You should use the length
4616 * field of the box to determine if any value
4617 * is assigned.
4618 * @param[in] tainted Whether the value came from a trusted source.
4619 * @return
4620 * - 0 on success.
4621 * - -1 on failure.
4622 */
4623int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4624 uint8_t const *src, size_t len, bool tainted)
4625{
4626 uint8_t *bin;
4627
4628 if (unlikely((len > 0) && !src)) {
4629 fr_strerror_printf("Invalid arguments to %s. Len > 0 (%zu) but src was NULL",
4630 __FUNCTION__, len);
4631 return -1;
4632 }
4633
4634 bin = talloc_memdup(ctx, src, len);
4635 if (!bin) {
4636 fr_strerror_const("Failed allocating octets buffer");
4637 return -1;
4638 }
4639 talloc_set_type(bin, uint8_t);
4640
4641 fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted);
4642 dst->vb_octets = bin;
4643 dst->vb_length = len;
4644
4645 return 0;
4646}
4647
4648int fr_value_box_memdup_dbuff(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4649 fr_dbuff_t *dbuff, size_t len, bool tainted)
4650{
4651 uint8_t *bin;
4652
4653 bin = talloc_size(ctx, len);
4654 if (!bin) {
4655 fr_strerror_printf("Failed allocating octets buffer");
4656 return -1;
4657 }
4658 if (fr_dbuff_out_memcpy(bin, dbuff, len) < (ssize_t) len) return -1;
4659 talloc_set_type(bin, uint8_t);
4660
4661 fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted);
4662 dst->vb_octets = bin;
4663 dst->vb_length = len;
4664
4665 return 0;
4666}
4667
4668/** Copy a talloced buffer to a fr_value_box_t
4669 *
4670 * Copy a buffer containing binary data, setting fields in the dst value box appropriately.
4671 *
4672 * @param[in] ctx to allocate any new buffers in.
4673 * @param[in] dst to assign new buffer to.
4674 * @param[in] enumv Aliases for values.
4675 * @param[in] src a buffer.
4676 * @param[in] tainted Whether the value came from a trusted source.
4677 * @return
4678 * - 0 on success.
4679 * - -1 on failure.
4680 */
4681int fr_value_box_memdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv,
4682 uint8_t const *src, bool tainted)
4683{
4685
4686 return fr_value_box_memdup(ctx, dst, enumv, src, talloc_array_length(src), tainted);
4687}
4688
4689/** Assign a buffer to a box, but don't copy it
4690 *
4691 * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed.
4692 *
4693 * Caller should set dst->taint = true, where the value was acquired from an untrusted source.
4694 *
4695 * @note Will free any exiting buffers associated with the value box.
4696 *
4697 * @param[in] dst to assign buffer to.
4698 * @param[in] enumv Aliases for values.
4699 * @param[in] src a talloced buffer.
4700 * @param[in] len of buffer.
4701 * @param[in] tainted Whether the value came from a trusted source.
4702 */
4704 uint8_t const *src, size_t len, bool tainted)
4705{
4706 fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted);
4707 dst->vb_octets = src;
4708 dst->vb_length = len;
4709}
4710
4711/** Assign a talloced buffer to a box, but don't copy it
4712 *
4713 * Adds a reference to the src buffer so that it cannot be freed until the ctx is freed.
4714 *
4715 * @param[in] ctx to allocate any new buffers in.
4716 * @param[in] dst to assign buffer to.
4717 * @param[in] enumv Aliases for values.
4718 * @param[in] src a talloced buffer.
4719 * @param[in] tainted Whether the value came from a trusted source.
4720 */
4722 uint8_t const *src, bool tainted)
4723{
4725
4726 fr_value_box_init(dst, FR_TYPE_OCTETS, enumv, tainted);
4727 dst->vb_octets = ctx ? talloc_reference(ctx, src) : src;
4728 dst->vb_length = talloc_array_length(src);
4729}
4730
4731/** Increment a boxed value
4732 *
4733 * Implements safe integer overflow.
4734 *
4735 * @param[in] vb to increment.
4736 */
4738{
4739 switch (vb->type) {
4740 case FR_TYPE_UINT8:
4741 vb->vb_uint8 = vb->vb_uint8 == UINT8_MAX ? 0 : vb->vb_uint8 + 1;
4742 return;
4743
4744 case FR_TYPE_UINT16:
4745 vb->vb_uint16 = vb->vb_uint16 == UINT16_MAX ? 0 : vb->vb_uint16 + 1;
4746 return;
4747
4748 case FR_TYPE_UINT32:
4749 vb->vb_uint32 = vb->vb_uint32 == UINT32_MAX ? 0 : vb->vb_uint32 + 1;
4750 return;
4751
4752 case FR_TYPE_UINT64:
4753 vb->vb_uint64 = vb->vb_uint64 == UINT64_MAX ? 0 : vb->vb_uint64 + 1;
4754 return;
4755
4756 case FR_TYPE_INT8:
4757 vb->vb_int8 = vb->vb_int8 == INT8_MAX ? INT8_MIN : vb->vb_int8 + 1;
4758 return;
4759
4760 case FR_TYPE_INT16:
4761 vb->vb_int16 = vb->vb_int16 == INT16_MAX ? INT16_MIN : vb->vb_int16 + 1;
4762 return;
4763
4764 case FR_TYPE_INT32:
4765 vb->vb_int32 = vb->vb_int32 == INT32_MAX ? INT32_MIN : vb->vb_int32 + 1;
4766 return;
4767
4768 case FR_TYPE_INT64:
4769 vb->vb_int64 = vb->vb_int64 == INT64_MAX ? INT64_MIN : vb->vb_int64 + 1;
4770 return;
4771
4772 default:
4773 return;
4774 }
4775}
4776
4777/** Convert integer encoded as string to a fr_value_box_t type
4778 *
4779 * @param[out] dst where to write parsed value.
4780 * @param[in] dst_type type of integer to convert string to.
4781 * @param[in] dst_enumv Enumeration values.
4782 * @param[in] in String to convert to integer.
4783 * @param[in] rules for parsing string.
4784 * @param[in] tainted Whether the value came from a trusted source.
4785 * @return
4786 * - >= 0 on success (number of bytes parsed).
4787 * - < 0 on error (where the parse error occurred).
4788 */
4789static inline CC_HINT(always_inline)
4791 fr_dict_attr_t const *dst_enumv,
4792 fr_sbuff_t *in, fr_sbuff_parse_rules_t const *rules, bool tainted)
4793{
4794 fr_slen_t slen;
4796
4797 fr_value_box_init(dst, dst_type, dst_enumv, tainted);
4798
4799 switch (dst_type) {
4800 case FR_TYPE_UINT8:
4801 slen = fr_sbuff_out(&err, &dst->vb_uint8, in);
4802 break;
4803
4804 case FR_TYPE_UINT16:
4805 slen = fr_sbuff_out(&err, &dst->vb_uint16, in);
4806 break;
4807
4808 case FR_TYPE_UINT32:
4809 slen = fr_sbuff_out(&err, &dst->vb_uint32, in);
4810 break;
4811
4812 case FR_TYPE_UINT64:
4813 slen = fr_sbuff_out(&err, &dst->vb_uint64, in);
4814 break;
4815
4816 case FR_TYPE_INT8:
4817 slen = fr_sbuff_out(&err, &dst->vb_int8, in);
4818 break;
4819
4820 case FR_TYPE_INT16:
4821 slen = fr_sbuff_out(&err, &dst->vb_int16, in);
4822 break;
4823
4824 case FR_TYPE_INT32:
4825 slen = fr_sbuff_out(&err, &dst->vb_int32, in);
4826 break;
4827
4828 case FR_TYPE_INT64:
4829 slen = fr_sbuff_out(&err, &dst->vb_int64, in);
4830 break;
4831
4832 case FR_TYPE_SIZE:
4833 slen = fr_sbuff_out(&err, &dst->vb_size, in);
4834 break;
4835
4836 case FR_TYPE_FLOAT32:
4837 slen = fr_sbuff_out(&err, &dst->vb_float32, in);
4838 break;
4839
4840 case FR_TYPE_FLOAT64:
4841 slen = fr_sbuff_out(&err, &dst->vb_float64, in);
4842 break;
4843
4844 default:
4845 fr_assert_fail(NULL);
4846 return -1;
4847 }
4848
4849 if (slen < 0) {
4850 /*
4851 * If an enumeration attribute is provided and we
4852 * don't find an integer, assume this is an enumv
4853 * lookup fail, and produce a better error.
4854 */
4855 if (dst_enumv && dst_enumv->flags.has_value && (err == FR_SBUFF_PARSE_ERROR_NOT_FOUND)) {
4856 fr_sbuff_t our_in = FR_SBUFF(in);
4857 fr_sbuff_adv_until(&our_in, SIZE_MAX, rules->terminals,
4858 rules->escapes ? rules->escapes->chr : '\0');
4859
4860 fr_strerror_printf("Invalid enumeration value \"%pV\" for attribute %s",
4862 dst_enumv->name);
4863 return -1;
4864 }
4865
4867 fr_strerror_printf("Failed parsing string as type '%s'",
4868 fr_type_to_str(dst_type));
4869 } else {
4870 fr_sbuff_parse_error_to_strerror(err);
4871 }
4872 }
4873
4874
4875 return slen;
4876}
4877
4878/** Convert string value to a fr_value_box_t type
4879 *
4880 * @param[in] ctx to alloc strings in.
4881 * @param[out] dst where to write parsed value.
4882 * @param[in,out] dst_type of value data to create/dst_type of value created.
4883 * @param[in] dst_enumv fr_dict_attr_t with string names for uint32 values.
4884 * @param[in] in sbuff to read data from.
4885 * @param[in] rules unescape and termination rules.
4886 * @return
4887 * - >0 on success.
4888 * - <= 0 on parse error.
4889 */
4891 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
4892 fr_sbuff_t *in, fr_sbuff_parse_rules_t const *rules)
4893{
4894 static fr_sbuff_parse_rules_t default_rules;
4895 fr_sbuff_t *unescaped = NULL;
4896 fr_sbuff_t our_in = FR_SBUFF(in);
4897 fr_ipaddr_t addr;
4898 fr_slen_t slen;
4899 char buffer[256];
4900
4901 if (!rules) rules = &default_rules;
4902
4904
4905 /*
4906 * Lookup any names before continuing
4907 */
4908 if (dst_enumv && dst_enumv->flags.has_value) {
4909 size_t name_len;
4910 fr_dict_enum_value_t *enumv;
4911
4912 /*
4913 * @todo - allow enum names for IPv6 addresses and prefixes. See also
4914 * tmpl_afrom_enum().
4915 */
4916 (void) fr_sbuff_adv_past_str_literal(&our_in, "::");
4917
4918 /*
4919 * If there is no escaping, then we ignore the terminals. The list of allowed characters
4920 * in enum names will ensure that the parsing doesn't go too far. i.e. to '\r', '\n'. '}', etc.
4921 *
4922 * The reason is that the list of terminals may include things like '-', which is also a
4923 * valid character in enum names. We don't want to parse "Framed-User" as "Framed - User".
4924 */
4925 if (!rules->escapes) {
4926 size_t len;
4928
4929 fr_sbuff_marker(&m, &our_in);
4930
4931 len = fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in),
4933 fr_sbuff_set(&our_in, &m);
4934 fr_sbuff_marker_release(&m);
4935
4936 if (!len) goto parse; /* Zero length name can't match enum */
4937
4938 enumv = fr_dict_enum_by_name(dst_enumv, fr_sbuff_current(&our_in), len);
4939 if (!enumv) {
4940 goto parse; /* No enumeration matches escaped string */
4941 }
4942
4943 (void) fr_sbuff_advance(&our_in, len);
4944 goto cast_enum;
4945 }
4946
4947 /*
4948 * Create a thread-local extensible buffer to
4949 * store unescaped data.
4950 *
4951 * This is created once per-thread (the first time
4952 * this function is called), and freed when the
4953 * thread exits.
4954 */
4955 FR_SBUFF_TALLOC_THREAD_LOCAL(&unescaped, 256, 4096);
4956
4957 /*
4958 * This function only does escaping until a terminal character, such as '-'. So
4959 * Framed-User will get parsed as "Framed - User".
4960 *
4961 * Pretty much no other enum has this problem. For Service-Type, it defines "Framed" ss
4962 * an equivalent name to "Framed-User". The parser sees "Framed-User", stops at the '-',
4963 * and then finds the enum named "Framed". It then returns the trailing "-User" as
4964 * something more to parse.
4965 *
4966 * As a result, when the user passes in "Framed-User", the output is "Framed-User -
4967 * User", which is more than a bit surprising.
4968 */
4969 name_len = fr_sbuff_out_unescape_until(unescaped, &our_in, SIZE_MAX,
4970 rules->terminals, rules->escapes);
4971 if (!name_len) {
4972 fr_sbuff_set_to_start(&our_in);
4973 goto parse; /* Zero length name can't match enum */
4974 }
4975
4976 enumv = fr_dict_enum_by_name(dst_enumv, fr_sbuff_start(unescaped), fr_sbuff_used(unescaped));
4977 if (!enumv) {
4978 fr_sbuff_set_to_start(&our_in);
4979 goto parse; /* No enumeration matches escaped string */
4980 }
4981
4982 cast_enum:
4983 /*
4984 * dst_type may not match enumv type
4985 */
4986 if (fr_value_box_cast(ctx, dst, dst_type, dst_enumv, enumv->value) < 0) return -1;
4987
4988 FR_SBUFF_SET_RETURN(in, &our_in);
4989 }
4990
4991parse:
4992 /*
4993 * It's a variable ret src->dst_type so we just alloc a new buffer
4994 * of size len and copy.
4995 */
4996 switch (dst_type) {
4997 case FR_TYPE_STRING:
4998 /*
4999 * We've not unescaped the string yet, produce an unescaped version
5000 */
5001 if (!dst_enumv || !unescaped) {
5002 char *buff;
5003
5004 if (unlikely(fr_sbuff_out_aunescape_until(ctx, &buff, &our_in, SIZE_MAX,
5005 rules->terminals, rules->escapes) < 0)) {
5006 return -1;
5007 }
5008 fr_value_box_bstrdup_buffer_shallow(NULL, dst, dst_enumv, buff, false);
5009 /*
5010 * We already have an unescaped version, just use that
5011 */
5012 } else {
5013 fr_value_box_bstrndup(ctx, dst, dst_enumv,
5014 fr_sbuff_start(unescaped), fr_sbuff_used(unescaped), false);
5015 }
5016 FR_SBUFF_SET_RETURN(in, &our_in);
5017
5018 /* raw octets: 0x01020304... */
5019 case FR_TYPE_OCTETS:
5020 {
5021 fr_sbuff_marker_t hex_start;
5022 size_t hex_len;
5023 uint8_t *bin_buff;
5024
5025 /*
5026 * If there's escape sequences that need to be processed
5027 * or the string doesn't start with 0x, then assume this
5028 * is literal data, not hex encoded data.
5029 */
5030 if (rules->escapes || !fr_sbuff_adv_past_strcase_literal(&our_in, "0x")) {
5031 if (!dst_enumv || !unescaped) {
5032 char *buff = NULL;
5033 uint8_t *bin;
5034
5035 if (fr_sbuff_extend(&our_in)) {
5036 fr_sbuff_out_aunescape_until(ctx, &buff, &our_in, SIZE_MAX,
5037 rules->terminals, rules->escapes);
5038
5039 if (talloc_array_length(buff) == 1) {
5041 goto zero;
5042 }
5043
5044 bin = talloc_realloc(ctx, buff, uint8_t, talloc_array_length(buff) - 1);
5045 if (unlikely(!bin)) {
5046 fr_strerror_const("Failed trimming string buffer");
5048 return -1;
5049 }
5050 talloc_set_type(bin, uint8_t); /* talloc_realloc doesn't do this */
5051 /*
5052 * Input data is zero
5053 *
5054 * talloc realloc will refuse to realloc to
5055 * a zero length buffer. This is probably
5056 * a bug, because we can create zero length
5057 * arrays normally
5058 */
5059 } else {
5060 zero:
5061 bin = talloc_zero_array(ctx, uint8_t, 0);
5062 }
5063
5064 fr_value_box_memdup_buffer_shallow(NULL, dst, dst_enumv, bin, false);
5065 /*
5066 * We already have an unescaped version, just use that
5067 */
5068 } else {
5069 fr_value_box_memdup(ctx, dst, dst_enumv,
5070 (uint8_t *)fr_sbuff_start(unescaped),
5071 fr_sbuff_used(unescaped), false);
5072 }
5073 FR_SBUFF_SET_RETURN(in, &our_in);
5074 }
5075
5076 fr_sbuff_marker(&hex_start, &our_in); /* Record where the hexits start */
5077
5078 /*
5079 * Find the end of the hex sequence.
5080 *
5081 * We don't technically need to do this, fr_base16_decode
5082 * will find the end on its own.
5083 *
5084 * We do this so we can alloc the correct sized
5085 * output buffer.
5086 */
5087 hex_len = fr_sbuff_adv_past_allowed(&our_in, SIZE_MAX, sbuff_char_class_hex, rules->terminals);
5088 if (hex_len == 0) {
5089 if (fr_value_box_memdup(ctx, dst, dst_enumv, (uint8_t[]){ 0x00 }, 0, false) < 0) return -1;
5090 FR_SBUFF_SET_RETURN(in, &our_in);
5091 }
5092
5093 if ((hex_len & 0x01) != 0) {
5094 fr_strerror_printf("Length of hex string is not even, got %zu bytes", hex_len);
5095 FR_SBUFF_ERROR_RETURN(&our_in);
5096 }
5097
5098 /*
5099 * Pre-allocate the bin buff and initialise the box
5100 */
5101 if (fr_value_box_mem_alloc(ctx, &bin_buff, dst, dst_enumv, (hex_len >> 1), false) < 0) return -1;
5102
5103 /*
5104 * Reset to the start of the hex string
5105 */
5106 fr_sbuff_set(&our_in, &hex_start);
5107
5108 if (unlikely(fr_base16_decode(NULL, &FR_DBUFF_TMP(bin_buff, hex_len), &our_in, false) < 0)) {
5109 talloc_free(bin_buff);
5110 FR_SBUFF_ERROR_RETURN(&our_in);
5111 }
5112
5113 FR_SBUFF_SET_RETURN(in, &our_in);
5114 }
5115
5116 case FR_TYPE_IPV4_ADDR:
5117 {
5118 size_t name_len = fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in), sbuff_char_class_hostname, rules->terminals);
5119 if (!name_len) return 0;
5120
5121 if (fr_inet_pton4(&addr, fr_sbuff_current(in), name_len,
5122 fr_hostname_lookups, false, true) < 0) return -1;
5123
5124 /*
5125 * We allow v4 addresses to have a /32 suffix as some databases (PostgreSQL)
5126 * print them this way.
5127 */
5128 if (addr.prefix != 32) {
5129 fail_ipv4_prefix:
5130 fr_strerror_printf("Invalid IPv4 mask length \"/%i\". Only \"/32\" permitted "
5131 "for non-prefix types", addr.prefix);
5132 return -1;
5133 }
5134
5135 memcpy(&dst->vb_ip, &addr, sizeof(dst->vb_ip));
5136 }
5137 goto finish;
5138
5140 {
5141 size_t name_len = fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in), sbuff_char_class_hostname, rules->terminals);
5142 if (!name_len) return 0;
5143
5144 if (fr_inet_pton4(&dst->vb_ip, fr_sbuff_current(in), name_len,
5145 fr_hostname_lookups, false, true) < 0) return -1;
5146 }
5147 goto finish;
5148
5149 case FR_TYPE_IPV6_ADDR:
5150 {
5151 size_t name_len = fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in), sbuff_char_class_hostname, rules->terminals);
5152 if (!name_len) return 0;
5153
5154 /*
5155 * Parse scope, too.
5156 */
5157 if (fr_sbuff_next_if_char(&our_in, '%')) {
5158 name_len += fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in), sbuff_char_class_uint, rules->terminals);
5159 }
5160
5161 if (fr_inet_pton6(&addr, fr_sbuff_current(in), name_len,
5162 fr_hostname_lookups, false, true) < 0) return -1;
5163
5164 /*
5165 * We allow v6 addresses to have a /128 suffix as some databases (PostgreSQL)
5166 * print them this way.
5167 */
5168 if (addr.prefix != 128) {
5169 fail_ipv6_prefix:
5170 fr_strerror_printf("Invalid IPv6 mask length \"/%i\". Only \"/128\" permitted "
5171 "for non-prefix types", addr.prefix);
5172 return -1;
5173 }
5174
5175 memcpy(&dst->vb_ip, &addr, sizeof(dst->vb_ip));
5176 }
5177 goto finish;
5178
5180 {
5181 size_t name_len = fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in), sbuff_char_class_hostname, rules->terminals);
5182 if (!name_len) return 0;
5183
5184 if (fr_inet_pton6(&dst->vb_ip, fr_sbuff_current(in), name_len,
5185 fr_hostname_lookups, false, true) < 0) return -1;
5186 }
5187 goto finish;
5188
5190 {
5191 size_t name_len = fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in), sbuff_char_class_hostname, rules->terminals);
5192 if (!name_len) return 0;
5193
5194 /*
5195 * Parse scope, too.
5196 */
5197 if (fr_sbuff_next_if_char(&our_in, '%')) {
5198 name_len += fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in), sbuff_char_class_uint, rules->terminals);
5199 }
5200
5201 if (fr_inet_pton(&addr, fr_sbuff_current(in), name_len, AF_UNSPEC,
5202 fr_hostname_lookups, true) < 0) return -1;
5203
5204 if ((addr.af == AF_INET) && (addr.prefix != 32)) {
5205 goto fail_ipv4_prefix;
5206 }
5207
5208 if ((addr.af == AF_INET6) && (addr.prefix != 128)) {
5209 goto fail_ipv6_prefix;
5210 }
5211
5212 memcpy(&dst->vb_ip, &addr, sizeof(dst->vb_ip));
5213 }
5214 goto finish;
5215
5217 {
5218 size_t name_len = fr_sbuff_adv_past_allowed(&our_in, fr_sbuff_remaining(&our_in), sbuff_char_class_hostname, rules->terminals);
5219 if (!name_len) return 0;
5220
5221 if (fr_inet_pton(&dst->vb_ip, fr_sbuff_current(in), name_len, AF_UNSPEC,
5222 fr_hostname_lookups, true) < 0) return -1;
5223 }
5224 goto finish;
5225
5226 case FR_TYPE_UINT8:
5227 case FR_TYPE_UINT16:
5228 case FR_TYPE_UINT32:
5229 case FR_TYPE_UINT64:
5230 case FR_TYPE_INT8:
5231 case FR_TYPE_INT16:
5232 case FR_TYPE_INT32:
5233 case FR_TYPE_INT64:
5234 case FR_TYPE_FLOAT32:
5235 case FR_TYPE_FLOAT64:
5236 return fr_value_box_from_numeric_substr(dst, dst_type, dst_enumv, in, rules, false);
5237
5238 case FR_TYPE_SIZE:
5239 if (fr_size_from_str(&dst->datum.size, &our_in) < 0) return -1;
5240 goto finish;
5241
5242 case FR_TYPE_BOOL:
5243 fr_value_box_init(dst, dst_type, dst_enumv, false);
5244
5245 /*
5246 * Quoted boolean values are "yes", "no", "true", "false"
5247 */
5248 slen = fr_sbuff_out(NULL, &dst->vb_bool, in);
5249 if (slen >= 0) return slen;
5250
5251 /*
5252 * For barewords we also allow 0 for false and any other
5253 * integer value for true.
5254 */
5255 if (!rules->escapes) {
5256 int64_t stmp;
5257 uint64_t utmp;
5258
5259 slen = fr_sbuff_out(NULL, &stmp, in);
5260 if (slen >= 0) {
5261 dst->vb_bool = (stmp != 0);
5262 return slen;
5263 }
5264
5265 slen = fr_sbuff_out(NULL, &utmp, in);
5266 if (slen >= 0) {
5267 dst->vb_bool = (utmp != 0);
5268 return slen;
5269 }
5270 }
5271
5272 fr_strerror_const("Invalid boolean value. Accepted values are "
5273 "\"yes\", \"no\", \"true\", \"false\" or any unquoted integer");
5274
5275 return slen; /* Just whatever the last error offset was */
5276
5277 case FR_TYPE_ETHERNET:
5278 {
5279 uint64_t num;
5280 fr_ethernet_t ether;
5281 fr_dbuff_t dbuff;
5283
5284 fr_dbuff_init(&dbuff, ether.addr, sizeof(ether.addr));
5285
5286 /*
5287 * Convert things which are obviously integers to Ethernet addresses
5288 *
5289 * We assume the number is the decimal
5290 * representation of the ethernet address.
5291 * i.e. the ethernet address converted to a
5292 * number, and printed.
5293 *
5294 * The string gets converted to a network-order
5295 * 8-byte number, and then the lower bytes of
5296 * that get copied to the ethernet address.
5297 *
5298 * Note: We need to check for a terminal sequence
5299 * after the number, else we may just end up
5300 * parsing the first hexit and returning.
5301 *
5302 * i.e. 1c:00:00:00:00 -> 1
5303 */
5304 if ((fr_sbuff_out(NULL, &num, &our_in) > 0) && fr_sbuff_is_terminal(&our_in, rules->terminals)) {
5305 num = htonll(num);
5306
5307 FR_DBUFF_IN_MEMCPY_RETURN(&dbuff, ((uint8_t *) &num) + 2, sizeof(dst->vb_ether));
5308 fr_value_box_ethernet_addr(dst, dst_enumv, &ether, false);
5309
5310 FR_SBUFF_SET_RETURN(in, &our_in);
5311 }
5312
5313 fr_sbuff_set_to_start(&our_in);
5314
5315 fr_base16_decode(&err, &dbuff, &our_in, true);
5316 if (err != FR_SBUFF_PARSE_OK) {
5317 ether_error:
5318 fr_sbuff_parse_error_to_strerror(err);
5319 FR_SBUFF_ERROR_RETURN(&our_in);
5320 }
5321
5322 if (!fr_sbuff_next_if_char(&our_in, ':')) {
5323 ether_sep_error:
5324 fr_strerror_const("Missing separator, expected ':'");
5325 FR_SBUFF_ERROR_RETURN(&our_in);
5326 }
5327
5328 fr_base16_decode(&err, &dbuff, &our_in, true);
5329 if (err != FR_SBUFF_PARSE_OK) goto ether_error;
5330
5331 if (!fr_sbuff_next_if_char(&our_in, ':')) goto ether_sep_error;
5332
5333 fr_base16_decode(&err, &dbuff, &our_in, true);
5334 if (err != FR_SBUFF_PARSE_OK) goto ether_error;
5335
5336 if (!fr_sbuff_next_if_char(&our_in, ':')) goto ether_sep_error;
5337
5338 fr_base16_decode(&err, &dbuff, &our_in, true);
5339 if (err != FR_SBUFF_PARSE_OK) goto ether_error;
5340
5341 if (!fr_sbuff_next_if_char(&our_in, ':')) goto ether_sep_error;
5342
5343 fr_base16_decode(&err, &dbuff, &our_in, true);
5344 if (err != FR_SBUFF_PARSE_OK) goto ether_error;
5345
5346 if (!fr_sbuff_next_if_char(&our_in, ':')) goto ether_sep_error;
5347
5348 fr_base16_decode(&err, &dbuff, &our_in, true);
5349 if (err != FR_SBUFF_PARSE_OK) goto ether_error;
5350
5351 fr_value_box_ethernet_addr(dst, dst_enumv, (fr_ethernet_t * const)fr_dbuff_start(&dbuff), false);
5352
5353 FR_SBUFF_SET_RETURN(in, &our_in);
5354 }
5355
5356 case FR_TYPE_TIME_DELTA:
5357 fr_value_box_init(dst, FR_TYPE_TIME_DELTA, dst_enumv, false);
5358
5359 slen = fr_time_delta_from_substr(&dst->datum.time_delta, &our_in,
5360 dst_enumv ? dst_enumv->flags.flag_time_res : FR_TIME_RES_SEC,
5361 false, rules->terminals);
5362 if (slen < 0) return slen;
5363 FR_SBUFF_SET_RETURN(in, &our_in);
5364
5365 case FR_TYPE_NULL:
5366 if (!rules->escapes && fr_sbuff_adv_past_str_literal(&our_in, "NULL")) {
5367 fr_value_box_init(dst, dst_type, dst_enumv, false);
5368 FR_SBUFF_SET_RETURN(in, &our_in);
5369 }
5370
5371 fr_strerror_const("Unexpected value for data type NULL");
5372 return -1;
5373
5374 /*
5375 * Dealt with below
5376 */
5377 default:
5378 break;
5379 }
5380
5381 /*
5382 * We may have terminals. If so, respect them.
5383 */
5384 if (rules && rules->terminals) {
5385 size_t len;
5386
5387 len = fr_sbuff_out_unescape_until(&FR_SBUFF_OUT(buffer, sizeof(buffer)), &our_in, SIZE_MAX,
5388 rules->terminals, rules->escapes);
5389 if (len >= sizeof(buffer)) goto too_small;
5390
5391 buffer[len] = '\0';
5392
5393 } else {
5394 /*
5395 * It's a fixed size src->dst_type, copy to a temporary buffer and
5396 * \0 terminate.
5397 *
5398 * @todo - note that this brute-force copy means that the input sbuff
5399 * is NOT advanced, and this function will return 0, even though it parsed data!
5400 */
5401 if (fr_sbuff_remaining(in) >= sizeof(buffer)) {
5402 too_small:
5403 fr_strerror_const("Temporary buffer too small");
5404 return -1;
5405 }
5406
5408 buffer[fr_sbuff_remaining(in)] = '\0';
5409 }
5410
5411 switch (dst_type) {
5412 case FR_TYPE_DATE:
5413 {
5414 if (dst_enumv) {
5415 if (fr_unix_time_from_str(&dst->vb_date, buffer, dst_enumv->flags.flag_time_res) < 0) return -1;
5416 } else {
5417 if (fr_unix_time_from_str(&dst->vb_date, buffer, FR_TIME_RES_SEC) < 0) return -1;
5418 }
5419
5420 dst->enumv = dst_enumv;
5421 }
5422 break;
5423
5424 case FR_TYPE_IFID:
5425 if (fr_inet_ifid_pton((void *) dst->vb_ifid, buffer) == NULL) {
5426 fr_strerror_printf("Failed to parse interface-id string \"%s\"", buffer);
5427 return -1;
5428 }
5429 break;
5430
5431 default:
5432 fr_strerror_printf("Cannot parse input as data type %s", fr_type_to_str(dst_type));
5433 return -1;
5434 }
5435
5436finish:
5437 dst->type = dst_type;
5438 dst->tainted = false;
5440
5441 /*
5442 * Fixup enumvs
5443 */
5444 dst->enumv = dst_enumv;
5445 fr_value_box_list_entry_init(dst);
5446
5447 FR_SBUFF_SET_RETURN(in, &our_in);
5448}
5449
5451 fr_type_t dst_type, fr_dict_attr_t const *dst_enumv,
5452 char const *in, size_t inlen,
5453 fr_sbuff_unescape_rules_t const *erules)
5454{
5455 ssize_t slen;
5456 fr_sbuff_parse_rules_t prules = { .escapes = erules };
5457
5458 slen = fr_value_box_from_substr(ctx, dst, dst_type, dst_enumv, &FR_SBUFF_IN(in, inlen), &prules);
5459 if (slen <= 0) return slen;
5460
5461 if (slen != (ssize_t)inlen) {
5462 fr_strerror_printf("Failed parsing '%s'. %zu bytes of trailing data after string value \"%pV\"",
5463 fr_type_to_str(dst_type),
5464 inlen - slen,
5465 fr_box_strvalue_len(in + slen, inlen - slen));
5466 return (slen - inlen) - 1;
5467 }
5468
5469 return slen;
5470}
5471
5472/** Print one boxed value to a string
5473 *
5474 * This function should primarily be used when a #fr_value_box_t is being
5475 * serialized in some non-standard way, i.e. as a value for a field
5476 * in a database, in all other instances it's better to use
5477 * #fr_value_box_print_quoted.
5478 *
5479 * @note - this function does NOT respect tainting! The escaping rules
5480 * are ONLY for escaping quotation characters, CR, LF, etc.
5481 *
5482 * @param[in] out Where to write the printed string.
5483 * @param[in] data Value box to print.
5484 * @param[in] e_rules To apply to FR_TYPE_STRING types, for escaping quotation characters _only_.
5485 * Is not currently applied to any other box type.
5486 */
5488{
5489 fr_sbuff_t our_out = FR_SBUFF(out);
5490
5491 char buf[1024]; /* Interim buffer to use with poorly behaved printing functions */
5492
5493 if (data->enumv && data->enumv->flags.has_value) {
5494 char const *name;
5495
5497 if (name) {
5498 FR_SBUFF_IN_ESCAPE_BUFFER_RETURN(&our_out, name, NULL);
5499 goto done;
5500 }
5501 }
5502
5503 switch (data->type) {
5504 case FR_TYPE_STRING:
5505 if (data->vb_length) FR_SBUFF_IN_ESCAPE_RETURN(&our_out,
5506 data->vb_strvalue, data->vb_length, e_rules);
5507 break;
5508
5509 case FR_TYPE_OCTETS:
5510 FR_SBUFF_IN_CHAR_RETURN(&our_out, '0', 'x');
5511 if (data->vb_length) FR_SBUFF_RETURN(fr_base16_encode, &our_out,
5512 &FR_DBUFF_TMP(data->vb_octets, data->vb_length));
5513 break;
5514
5515 /*
5516 * We need to use the proper inet_ntop functions for IP
5517 * addresses, else the output might not match output of
5518 * other functions, which makes testing difficult.
5519 *
5520 * An example is tunneled ipv4 in ipv6 addresses.
5521 */
5522 case FR_TYPE_IPV4_ADDR:
5523 case FR_TYPE_IPV6_ADDR:
5525 if (!fr_inet_ntop(buf, sizeof(buf), &data->vb_ip)) return 0;
5526 FR_SBUFF_IN_STRCPY_RETURN(&our_out, buf);
5527 break;
5528
5532 if (!fr_inet_ntop_prefix(buf, sizeof(buf), &data->vb_ip)) return 0;
5533 FR_SBUFF_IN_STRCPY_RETURN(&our_out, buf);
5534 break;
5535
5536 case FR_TYPE_IFID:
5537 if (!fr_inet_ifid_ntop(buf, sizeof(buf), data->vb_ifid)) return 0;
5538 FR_SBUFF_IN_STRCPY_RETURN(&our_out, buf);
5539 break;
5540
5541 case FR_TYPE_ETHERNET:
5542 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%02x:%02x:%02x:%02x:%02x:%02x",
5543 data->vb_ether[0], data->vb_ether[1],
5544 data->vb_ether[2], data->vb_ether[3],
5545 data->vb_ether[4], data->vb_ether[5]);
5546 break;
5547
5548 case FR_TYPE_BOOL:
5549 FR_SBUFF_IN_STRCPY_RETURN(&our_out, data->vb_uint8 ? "yes" : "no");
5550 break;
5551
5552 case FR_TYPE_UINT8:
5553 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%u", data->vb_uint8);
5554 break;
5555
5556 case FR_TYPE_UINT16:
5557 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%u", data->vb_uint16);
5558 break;
5559
5560 case FR_TYPE_UINT32:
5561 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%u", data->vb_uint32);
5562 break;
5563
5564 case FR_TYPE_UINT64:
5565 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%" PRIu64, data->vb_uint64);
5566 break;
5567
5568 case FR_TYPE_INT8:
5569 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%d", data->vb_int8);
5570 break;
5571
5572 case FR_TYPE_INT16:
5573 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%d", data->vb_int16);
5574 break;
5575
5576 case FR_TYPE_INT32:
5577 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%d", data->vb_int32);
5578 break;
5579
5580 case FR_TYPE_INT64:
5581 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%" PRId64, data->vb_int64);
5582 break;
5583
5584 case FR_TYPE_FLOAT32:
5585 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%f", (double) data->vb_float32);
5586 break;
5587
5588 case FR_TYPE_FLOAT64:
5589 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%g", data->vb_float64);
5590 break;
5591
5592 case FR_TYPE_DATE:
5593 {
5595
5596 if (data->enumv) res = data->enumv->flags.flag_time_res;
5597
5598 FR_SBUFF_RETURN(fr_unix_time_to_str, &our_out, data->vb_date, res, true);
5599 break;
5600 }
5601
5602 case FR_TYPE_SIZE:
5603 FR_SBUFF_RETURN(fr_size_to_str, &our_out, data->datum.size);
5604 break;
5605
5606 case FR_TYPE_TIME_DELTA:
5607 {
5609 bool is_unsigned = false;
5610
5611 if (data->enumv) {
5612 res = data->enumv->flags.flag_time_res;
5613 is_unsigned = data->enumv->flags.is_unsigned;
5614 }
5615
5616
5617 FR_SBUFF_RETURN(fr_time_delta_to_str, &our_out, data->vb_time_delta, res, is_unsigned);
5618 }
5619 break;
5620
5621 case FR_TYPE_GROUP:
5622 /*
5623 * If the caller didn't ask to escape binary data
5624 * in 'octets' types, then we force that now.
5625 * Otherwise any 'octets' type which is buried
5626 * inside of a 'group' will get copied verbatim
5627 * from input to output, with no escaping!
5628 */
5629 if (!e_rules || (!e_rules->do_oct && !e_rules->do_hex)) {
5630 e_rules = &fr_value_escape_double;
5631 }
5632
5633 /*
5634 * Represent groups as:
5635 *
5636 * { <value0>, <value1>, { <sub-value0>, <sub-value1>, <sub-valueN> }}
5637 */
5638 FR_SBUFF_IN_CHAR_RETURN(&our_out, '{');
5640 NULL, &our_out, UNCONST(fr_value_box_list_t *, &data->vb_group),
5641 ", ", (sizeof(", ") - 1), e_rules,
5643 FR_SBUFF_IN_CHAR_RETURN(&our_out, '}');
5644 break;
5645
5646 case FR_TYPE_NULL:
5647 FR_SBUFF_IN_STRCPY_LITERAL_RETURN(&our_out, "NULL");
5648 break;
5649 /*
5650 * Don't add default here
5651 */
5652 case FR_TYPE_TLV: /* Not a box type */
5653 case FR_TYPE_STRUCT: /* Not a box type */
5654 case FR_TYPE_VSA: /* Not a box type */
5655 case FR_TYPE_VENDOR: /* Not a box type */
5656 case FR_TYPE_VALUE_BOX:
5657 case FR_TYPE_VOID:
5658 case FR_TYPE_MAX:
5659 (void)fr_cond_assert(0);
5660 return 0;
5661 }
5662
5663done:
5664 FR_SBUFF_SET_RETURN(out, &our_out);
5665}
5666
5667/** Print one boxed value to a string with quotes (where needed)
5668 *
5669 * @param[in] out Where to write the printed string.
5670 * @param[in] data Value box to print.
5671 * @param[in] quote To apply to FR_TYPE_STRING types.
5672 * Is not currently applied to any
5673 * other box type.
5674 */
5676{
5677 fr_sbuff_t our_out = FR_SBUFF(out);
5678
5679 if (quote == T_BARE_WORD) return fr_value_box_print(out, data, NULL);
5680
5681 switch (data->type) {
5682 case FR_TYPE_QUOTED:
5683 FR_SBUFF_IN_CHAR_RETURN(&our_out, fr_token_quote[quote]);
5685 FR_SBUFF_IN_CHAR_RETURN(&our_out, fr_token_quote[quote]);
5686 break;
5687
5688 default:
5689 return fr_value_box_print(out, data, NULL);
5690 }
5691
5692 FR_SBUFF_SET_RETURN(out, &our_out);
5693}
5694
5695/** Concatenate a list of value boxes together
5696 *
5697 * All boxes will be removed from the list.
5698 *
5699 * @param[out] safety if !NULL, the results of tainted / secret / safe_for will be stored here.
5700 * @param[out] sbuff to write the result of the concatenation to.
5701 * @param[in] list to concatenate.
5702 * @param[in] sep Insert a separator between the values.
5703 * @param[in] sep_len Length of the separator.
5704 * @param[in] e_rules To apply to FR_TYPE_STRING types.
5705 * Is not currently applied to any other box type.
5706 * @param[in] proc_action What to do with the boxes in the list once
5707 * they've been processed.
5708 * @param[in] safe_for if value has this safe_for value, don't apply the escape rules.
5709 * for values which are escaped, mash the safe_for value to this.
5710 * @param[in] flatten If true and we encounter a #FR_TYPE_GROUP,
5711 * we concat the contents of its children together.
5712 * If false, the contents will be cast to #FR_TYPE_STRING.
5713 * @return
5714 * - >=0 the number of bytes written to the sbuff.
5715 * - <0 how many additional bytes we would have needed to
5716 * concat the next box.
5717 */
5718ssize_t fr_value_box_list_concat_as_string(fr_value_box_t *safety, fr_sbuff_t *sbuff, fr_value_box_list_t *list,
5719 char const *sep, size_t sep_len, fr_sbuff_escape_rules_t const *e_rules,
5720 fr_value_box_list_action_t proc_action, fr_value_box_safe_for_t safe_for, bool flatten)
5721{
5722 fr_sbuff_t our_sbuff = FR_SBUFF(sbuff);
5723 ssize_t slen;
5724
5725 if (fr_value_box_list_empty(list)) return 0;
5726
5727 fr_value_box_list_foreach(list, vb) {
5728 fr_value_box_safe_for_t box_safe_for = vb->safe_for;
5729
5730 switch (vb->type) {
5731 case FR_TYPE_GROUP:
5732 if (!flatten) goto print;
5733 slen = fr_value_box_list_concat_as_string(safety, &our_sbuff, &vb->vb_group,
5734 sep, sep_len, e_rules,
5735 proc_action, safe_for, flatten);
5736 break;
5737
5738 case FR_TYPE_OCTETS:
5739
5740 /*
5741 * Copy the raw string over, if necessary with escaping.
5742 */
5743 if (e_rules && (!fr_value_box_is_safe_for(vb, safe_for) || e_rules->do_oct || e_rules->do_hex)) {
5744 box_safe_for = safe_for;
5745
5746 slen = fr_sbuff_in_escape(&our_sbuff, (char const *)vb->vb_strvalue, vb->vb_length, e_rules);
5747 } else {
5748 slen = fr_sbuff_in_bstrncpy(&our_sbuff, (char const *)vb->vb_strvalue, vb->vb_length);
5749 }
5750 break;
5751
5752 case FR_TYPE_STRING:
5753 if (!fr_value_box_is_safe_for(vb, safe_for) && e_rules) goto print;
5754
5755 slen = fr_sbuff_in_bstrncpy(&our_sbuff, vb->vb_strvalue, vb->vb_length);
5756 break;
5757
5758 case FR_TYPE_NULL: /* Skip null */
5759 continue;
5760
5761 default:
5762 print:
5763 /*
5764 * If we escaped it, set the output safe_for value.
5765 */
5766 if (e_rules) box_safe_for = safe_for;
5767 slen = fr_value_box_print(&our_sbuff, vb, e_rules);
5768 break;
5769 }
5770 if (slen < 0) return slen;
5771
5772 /*
5773 * Add in the separator
5774 */
5775 if (sep && fr_value_box_list_next(list, vb)) {
5776 slen = fr_sbuff_in_bstrncpy(&our_sbuff, sep, sep_len);
5777 if (slen < 0) return slen;
5778 }
5779
5780 /*
5781 * Merge in the safety rules.
5782 */
5783 if (!safety || (vb->type == FR_TYPE_GROUP)) continue;
5784
5785 /*
5786 * We can't call fr_box_safety_merge(), as we may have escaped the input box.
5787 */
5788 if ((safety->safe_for != FR_VALUE_BOX_SAFE_FOR_NONE) &&
5789 (safety->safe_for != box_safe_for)) {
5790 if (safety->safe_for == FR_VALUE_BOX_SAFE_FOR_ANY) {
5791 safety->safe_for = box_safe_for;
5792 } else {
5793 safety->safe_for = FR_VALUE_BOX_SAFE_FOR_NONE;
5794 }
5795 }
5796
5797 safety->tainted |= vb->tainted;
5798 safety->secret |= vb->secret;
5799 }
5800
5801 /*
5802 * Free the boxes last so if there's
5803 * an issue concatenating them, everything
5804 * is still in a known state.
5805 */
5807 if (vb_should_remove(proc_action)) fr_value_box_list_remove(list, vb);
5808 if (vb_should_free_value(proc_action)) fr_value_box_clear_value(vb);
5809 if (vb_should_free(proc_action)) talloc_free(vb);
5810 }}
5811
5812 FR_SBUFF_SET_RETURN(sbuff, &our_sbuff);
5813}
5814
5815/** Concatenate a list of value boxes together
5816 *
5817 * All boxes will be removed from the list.
5818 *
5819 * @param[out] safety if !NULL, the results of tainted / secret / safe_for will be stored here.
5820 * @param[out] dbuff to write the result of the concatenation to.
5821 * @param[in] list to concatenate.
5822 * @param[in] sep Insert a separator between the values.
5823 * @param[in] sep_len Length of the separator.
5824 * @param[in] proc_action What to do with the boxes in the list once
5825 * they've been processed.
5826 * @param[in] flatten If true and we encounter a #FR_TYPE_GROUP,
5827 * we concat the contents of its children together.
5828 * If false, the contents will be cast to #FR_TYPE_OCTETS.
5829 * @return
5830 * - >=0 the number of bytes written to the sbuff.
5831 * - <0 how many additional bytes we would have needed to
5832 * concat the next box.
5833 */
5834ssize_t fr_value_box_list_concat_as_octets(fr_value_box_t *safety, fr_dbuff_t *dbuff, fr_value_box_list_t *list,
5835 uint8_t const *sep, size_t sep_len,
5836 fr_value_box_list_action_t proc_action, bool flatten)
5837{
5838 fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
5839 TALLOC_CTX *tmp_ctx = NULL;
5840 ssize_t slen;
5841
5842 if (fr_value_box_list_empty(list)) return 0;
5843
5844 fr_value_box_list_foreach(list, vb) {
5845 switch (vb->type) {
5846 case FR_TYPE_GROUP:
5847 if (!flatten) goto cast;
5848 slen = fr_value_box_list_concat_as_octets(safety, &our_dbuff, &vb->vb_group,
5849 sep, sep_len,
5850 proc_action, flatten);
5851 break;
5852
5853 case FR_TYPE_OCTETS:
5854 slen = fr_dbuff_in_memcpy(&our_dbuff, vb->vb_octets, vb->vb_length);
5855 break;
5856
5857 case FR_TYPE_STRING:
5858 slen = fr_dbuff_in_memcpy(&our_dbuff, (uint8_t const *)vb->vb_strvalue, vb->vb_length);
5859 break;
5860
5861 case FR_TYPE_NULL: /* Skip null */
5862 continue;
5863
5864 default:
5865 cast:
5866 {
5867 fr_value_box_t tmp_vb;
5868
5869 if (!tmp_ctx) tmp_ctx = talloc_pool(NULL, 1024);
5870
5871 /*
5872 * Not equivalent to fr_value_box_to_network
5873 */
5874 if (fr_value_box_cast_to_octets(tmp_ctx, &tmp_vb, FR_TYPE_OCTETS, NULL, vb) < 0) {
5875 slen = -1;
5876 goto error;
5877 }
5878
5879 slen = fr_dbuff_in_memcpy(&our_dbuff, tmp_vb.vb_octets, tmp_vb.vb_length);
5880 fr_value_box_clear_value(&tmp_vb);
5881 break;
5882 }
5883 }
5884
5885 if (slen < 0) {
5886 error:
5887 talloc_free(tmp_ctx);
5888 return slen;
5889 }
5890
5891 if (sep && fr_value_box_list_next(list, vb)) {
5892 slen = fr_dbuff_in_memcpy(&our_dbuff, sep, sep_len);
5893 if (slen < 0) goto error;
5894 }
5895
5896 fr_value_box_safety_merge(safety, vb);
5897 }
5898
5899 talloc_free(tmp_ctx);
5900
5901 /*
5902 * Free the boxes last so if there's
5903 * an issue concatenating them, everything
5904 * is still in a known state.
5905 */
5907 if (vb_should_remove(proc_action)) fr_value_box_list_remove(list, vb);
5908 if (vb_should_free_value(proc_action)) fr_value_box_clear_value(vb);
5909 if (vb_should_free(proc_action)) talloc_free(vb);
5910 }}
5911
5912 return fr_dbuff_set(dbuff, &our_dbuff);
5913}
5914
5915/** Concatenate a list of value boxes
5916 *
5917 * @note Will automatically cast all #fr_value_box_t to type specified.
5918 *
5919 * @param[in] ctx to allocate new value buffer in.
5920 * @param[out] out Where to write the resulting box.
5921 * @param[in] list to concatenate together.
5922 * @param[in] type May be #FR_TYPE_STRING or #FR_TYPE_OCTETS, no other types are
5923 * supported.
5924 * @param[in] proc_action What to do with the boxes in the list once
5925 * they've been processed.
5926 * @param[in] flatten If true and we encounter a #FR_TYPE_GROUP,
5927 * we concat the contents of its children together.
5928 * If false, the contents will be cast to the given type.
5929 * @param[in] max_size of the value.
5930 * @return
5931 * - 0 on success.
5932 * - -1 on failure.
5933 */
5935 fr_value_box_t *out, fr_value_box_list_t *list, fr_type_t type,
5936 fr_value_box_list_action_t proc_action, bool flatten,
5937 size_t max_size)
5938{
5939 fr_dbuff_t dbuff; /* FR_TYPE_OCTETS */
5940 fr_dbuff_uctx_talloc_t dbuff_tctx;
5941
5942 fr_sbuff_t sbuff; /* FR_TYPE_STRING */
5943 fr_sbuff_uctx_talloc_t sbuff_tctx;
5944
5945 fr_value_box_t *head_vb = fr_value_box_list_head(list);
5946
5947 fr_value_box_entry_t entry;
5948
5949 if (fr_value_box_list_empty(list)) {
5950 fr_strerror_const("Invalid arguments. List contains no elements");
5951 return -1;
5952 }
5953
5954 /*
5955 * Exit quickly if the list is only one box of the correct type and
5956 * out points at that box.
5957 */
5958 if ((fr_value_box_list_num_elements(list) == 1) && (head_vb == out) && (head_vb->type == type)) return 0;
5959
5960 switch (type) {
5961 case FR_TYPE_STRING:
5962 if (unlikely(!fr_sbuff_init_talloc(ctx, &sbuff, &sbuff_tctx, 256, max_size))) return -1;
5963 break;
5964
5965 case FR_TYPE_OCTETS:
5966 if (unlikely(!fr_dbuff_init_talloc(ctx, &dbuff, &dbuff_tctx, 256, max_size))) return -1;
5967 break;
5968
5969 default:
5970 fr_strerror_printf("Invalid argument. Can't concatenate boxes to type %s",
5972 return -1;
5973 }
5974
5975 /*
5976 * Merge all siblings into list head.
5977 *
5978 * This is where the first element in the
5979 * list is the output box.
5980 *
5981 * i.e. we want to merge all its siblings
5982 * into it.
5983 */
5984 if (out == head_vb) {
5985 switch (type) {
5986 case FR_TYPE_STRING:
5987 /*
5988 * Head gets dealt with specially as we don't
5989 * want to free it, and we don't want to free
5990 * the buffer associated with it (just yet).
5991 *
5992 * Note that we don't convert 'octets' to a printable string
5993 * here. Doing so breaks the keyword tests.
5994 */
5995 if (fr_value_box_list_concat_as_string(out, &sbuff, list,
5996 NULL, 0, NULL,
5998 fr_strerror_printf("Concatenation exceeded max_size (%zu)", max_size);
5999 error:
6000 switch (type) {
6001 case FR_TYPE_STRING:
6002 talloc_free(fr_sbuff_buff(&sbuff));
6003 break;
6004
6005 case FR_TYPE_OCTETS:
6006 talloc_free(fr_dbuff_buff(&dbuff));
6007 break;
6008
6009 default:
6010 break;
6011 }
6012 return -1;
6013 }
6014
6015 /*
6016 * Concat the rest of the children...
6017 */
6018 if (fr_value_box_list_concat_as_string(out, &sbuff, list,
6019 NULL, 0, NULL,
6020 proc_action, FR_VALUE_BOX_SAFE_FOR_ANY, flatten) < 0) {
6021 fr_value_box_list_insert_head(list, head_vb);
6022 goto error;
6023 }
6024 (void)fr_sbuff_trim_talloc(&sbuff, SIZE_MAX);
6026 if (fr_value_box_bstrndup(ctx, out, NULL, fr_sbuff_buff(&sbuff), fr_sbuff_used(&sbuff), out->tainted) < 0) goto error;
6027 break;
6028
6029 case FR_TYPE_OCTETS:
6030 if (fr_value_box_list_concat_as_octets(out, &dbuff, list,
6031 NULL, 0,
6032 FR_VALUE_BOX_LIST_REMOVE, flatten) < 0) goto error;
6033
6034 if (fr_value_box_list_concat_as_octets(out, &dbuff, list,
6035 NULL, 0,
6036 proc_action, flatten) < 0) {
6037 fr_value_box_list_insert_head(list, head_vb);
6038 goto error;
6039 }
6040 (void)fr_dbuff_trim_talloc(&dbuff, SIZE_MAX);
6042 if (fr_value_box_memdup(ctx, out, NULL, fr_dbuff_buff(&dbuff), fr_dbuff_used(&dbuff), out->tainted) < 0) goto error;
6043 break;
6044
6045 default:
6046 break;
6047 }
6048
6049 fr_value_box_list_insert_head(list, out);
6050
6051 /*
6052 * Merge all the boxes in the list into
6053 * a single contiguous buffer.
6054 *
6055 * This deals with an unrelated out and list
6056 * and also where list is the children of
6057 * out.
6058 */
6059 } else {
6060 switch (type) {
6061 case FR_TYPE_STRING:
6062 if (fr_value_box_list_concat_as_string(out, &sbuff, list,
6063 NULL, 0, NULL,
6064 proc_action, FR_VALUE_BOX_SAFE_FOR_ANY, flatten) < 0) goto error;
6065 (void)fr_sbuff_trim_talloc(&sbuff, SIZE_MAX);
6066
6067 entry = out->entry;
6068 if (fr_value_box_bstrndup(ctx, out, NULL, fr_sbuff_buff(&sbuff), fr_sbuff_used(&sbuff), out->tainted) < 0) goto error;
6069 out->entry = entry;
6070 break;
6071
6072 case FR_TYPE_OCTETS:
6073 if (fr_value_box_list_concat_as_octets(out, &dbuff, list,
6074 NULL, 0,
6075 proc_action, flatten) < 0) goto error;
6076 (void)fr_dbuff_trim_talloc(&dbuff, SIZE_MAX);
6077
6078 entry = out->entry;
6079 if (fr_value_box_memdup(ctx, out, NULL, fr_dbuff_buff(&dbuff), fr_dbuff_used(&dbuff), out->tainted) < 0) goto error;
6080 out->entry = entry;
6081 break;
6082
6083 default:
6084 break;
6085 }
6086 }
6087
6088 return 0;
6089}
6090
6091/** Escape a single value box in place
6092 *
6093 * @note Applies recursively to the children of group boxes.
6094 *
6095 * @param[in] vb to escape.
6096 * @param[in] escape escape definition to apply to the value box.
6097 * @param[in] uctx user context to pass to the escape function.
6098 * @return
6099 * - 0 on success.
6100 * - -1 on failure.
6101 */
6103{
6104 int ret;
6105
6106 switch (vb->type) {
6107 case FR_TYPE_GROUP:
6108 return fr_value_box_list_escape_in_place(&vb->vb_group, escape, uctx);
6109
6110 case FR_TYPE_NULL:
6111 case FR_TYPE_TLV:
6112 case FR_TYPE_STRUCT:
6113 case FR_TYPE_VSA:
6114 case FR_TYPE_VENDOR:
6115 case FR_TYPE_VALUE_BOX:
6116 case FR_TYPE_VOID:
6117 case FR_TYPE_MAX:
6118 fr_strerror_printf("Cannot escape data type '%s'", fr_type_to_str(vb->type));
6119 return -1;
6120
6121 default:
6122 break;
6123 }
6124
6125 /*
6126 * Don't do double escaping.
6127 */
6128 if (!escape->always_escape && fr_value_box_is_safe_for(vb, escape->safe_for)) return 0;
6129
6130 ret = escape->func(vb, uctx);
6131 if (unlikely(ret < 0)) return ret;
6132
6133 /*
6134 * '1' means that the function mashed the safe_for value, so we don't need to.
6135 */
6136 if (!ret) vb->safe_for = escape->safe_for;
6137 vb->tainted = false;
6138
6139 return 0;
6140}
6141
6142/** Escape a list of value boxes in place
6143 *
6144 * @note Applies recursively to the children of group boxes.
6145 *
6146 * @note on error, the list may be left in an inconsistent/partially escaped state.
6147 *
6148 * @param[in] list to escape.
6149 * @param[in] escape escape definition to apply to the value box.
6150 * @param[in] uctx user context to pass to the escape function.
6151 * @return
6152 * - 0 on success.
6153 * - -1 on failure.
6154 */
6155int fr_value_box_list_escape_in_place(fr_value_box_list_t *list, fr_value_box_escape_t const *escape, void *uctx)
6156{
6157 int ret = 0;
6158
6159 fr_value_box_list_foreach(list, vb) {
6160 ret = fr_value_box_escape_in_place(vb, escape, uctx);
6161 if (unlikely(ret < 0)) return ret;
6162 }
6163
6164 return ret;
6165}
6166
6167/** Removes a single layer of nesting, moving all children into the parent list
6168 *
6169 * @param[in] ctx to reparent children in if steal is true.
6170 * @param[in] list to flatten.
6171 * @param[in] steal whether to change the talloc ctx of children.
6172 * @param[in] free whether to free any group boxes which have had
6173 * their children removed.
6174 */
6175void fr_value_box_flatten(TALLOC_CTX *ctx, fr_value_box_list_t *list, bool steal, bool free)
6176{
6177 fr_value_box_list_foreach_safe(list, child) {
6178 if (!fr_type_is_structural(child->type)) continue;
6179
6180 fr_value_box_list_foreach_safe(&child->vb_group, grandchild) {
6181 fr_value_box_list_remove(&child->vb_group, grandchild);
6182 if (steal) talloc_steal(ctx, grandchild);
6183 fr_value_box_list_insert_before(list, child, grandchild);
6184 }}
6185
6186 if (free) talloc_free(child);
6187 }}
6188}
6189
6190/** Concatenate the string representations of a list of value boxes together
6191 *
6192 * @param[in] ctx to allocate the buffer in.
6193 * @param[in] list of value boxes.
6194 * @param[in] delim to insert between value box values.
6195 * @param[in] e_rules to control escaping of the concatenated elements.
6196 * @return
6197 * - NULL on error.
6198 * - The concatenation of the string values of the value box list on success.
6199 */
6200char *fr_value_box_list_aprint(TALLOC_CTX *ctx, fr_value_box_list_t const *list, char const *delim,
6201 fr_sbuff_escape_rules_t const *e_rules)
6202{
6203 fr_value_box_t const *vb = fr_value_box_list_head(list);
6204 char *aggr, *td = NULL;
6205 TALLOC_CTX *pool = NULL;
6206
6207 if (!vb) return NULL;
6208
6209 fr_value_box_aprint(ctx, &aggr, vb, e_rules);
6210 if (!aggr) return NULL;
6211 if (!fr_value_box_list_next(list, vb)) return aggr;
6212
6213 /*
6214 * If we're aggregating more values,
6215 * allocate a temporary pool.
6216 */
6217 pool = talloc_pool(NULL, 255);
6218 if (delim) td = talloc_typed_strdup(pool, delim);
6219
6220 while ((vb = fr_value_box_list_next(list, vb))) {
6221 char *str, *new_aggr;
6222
6223 fr_value_box_aprint(pool, &str, vb, e_rules);
6224 if (!str) continue;
6225
6226 new_aggr = talloc_buffer_append_variadic_buffer(ctx, aggr, 2, td, str);
6227 if (unlikely(!new_aggr)) {
6228 talloc_free(aggr);
6229 talloc_free(pool);
6230 return NULL;
6231 }
6232 aggr = new_aggr;
6233 talloc_free(str);
6234 }
6235 talloc_free(pool);
6236
6237 return aggr;
6238}
6239
6240/** Concatenate the string representations of a list of value boxes together hiding "secret" values
6241 *
6242 * @param[in] ctx to allocate the buffer in.
6243 * @param[in] list of value boxes.
6244 * @param[in] delim to insert between value box values.
6245 * @param[in] e_rules to control escaping of the concatenated elements.
6246 * @return
6247 * - NULL on error.
6248 * - The concatenation of the string values of the value box list on success.
6249 */
6250char *fr_value_box_list_aprint_secure(TALLOC_CTX *ctx, fr_value_box_list_t const *list, char const *delim,
6251 fr_sbuff_escape_rules_t const *e_rules)
6252{
6253 fr_value_box_t const *vb = fr_value_box_list_head(list);
6254 char *aggr, *td = NULL;
6255 TALLOC_CTX *pool = NULL;
6256
6257 if (!vb) return NULL;
6258
6260 aggr = talloc_typed_strdup(ctx, "<<< secret >>>");
6261 } else {
6262 fr_value_box_aprint(ctx, &aggr, vb, e_rules);
6263 }
6264 if (!aggr) return NULL;
6265 if (!fr_value_box_list_next(list, vb)) return aggr;
6266
6267 /*
6268 * If we're aggregating more values,
6269 * allocate a temporary pool.
6270 */
6271 pool = talloc_pool(NULL, 255);
6272 if (delim) td = talloc_typed_strdup(pool, delim);
6273
6274 while ((vb = fr_value_box_list_next(list, vb))) {
6275 char *str, *new_aggr;
6276
6278 str = talloc_typed_strdup(pool, "<<< secret >>>");
6279 } else {
6280 fr_value_box_aprint(pool, &str, vb, e_rules);
6281 }
6282 if (!str) continue;
6283
6284 new_aggr = talloc_buffer_append_variadic_buffer(ctx, aggr, 2, td, str);
6285 if (unlikely(!new_aggr)) {
6286 talloc_free(aggr);
6287 talloc_free(pool);
6288 return NULL;
6289 }
6290 aggr = new_aggr;
6291 talloc_free(str);
6292 }
6293 talloc_free(pool);
6294
6295 return aggr;
6296}
6297
6298/** Hash the contents of a value box
6299 *
6300 */
6302{
6303 switch (vb->type) {
6304 case FR_TYPE_FIXED_SIZE:
6305 return fr_hash(fr_value_box_raw(vb, vb->type),
6306 fr_value_box_field_sizes[vb->type]);
6307
6308 case FR_TYPE_STRING:
6309 return fr_hash(vb->vb_strvalue, vb->vb_length);
6310
6311 case FR_TYPE_OCTETS:
6312 return fr_hash(vb->vb_octets, vb->vb_length);
6313
6314 default:
6315 break;
6316 }
6317
6318 return 0;
6319}
6320
6321/** Do a full copy of a list of value boxes
6322 *
6323 * @param[in] ctx to allocate boxes in.
6324 * @param[out] out Where to write the head of the new list.
6325 * @param[in] in boxes to copy.
6326 * @return
6327 * - A duplicate list of value boxes, allocated in the context of 'ctx'
6328 * - NULL on error, or empty input list.
6329 */
6330int fr_value_box_list_acopy(TALLOC_CTX *ctx, fr_value_box_list_t *out, fr_value_box_list_t const *in)
6331{
6332 fr_value_box_t const *in_p = NULL;
6333
6334 while ((in_p = fr_value_box_list_next(in, in_p))) {
6335 fr_value_box_t *n = NULL;
6336
6338 if (!n) {
6339 error:
6340 fr_value_box_list_talloc_free(out);
6341 return -1;
6342 }
6343
6344 if (fr_value_box_copy(n, n, in_p) < 0) goto error;
6345 fr_dlist_insert_tail(fr_value_box_list_dlist_head(out), n);
6346 }
6347
6348 return 0;
6349}
6350
6351/** Check to see if any list members (or their children) are tainted
6352 *
6353 * @param[in] head of list to check.
6354 * @return
6355 * - true if a list member is tainted.
6356 * - false if no list members are tainted.
6357 */
6358bool fr_value_box_list_tainted(fr_value_box_list_t const *head)
6359{
6360 fr_value_box_t *vb = NULL;
6361
6362 while ((vb = fr_value_box_list_next(head, vb))) {
6363 if (fr_type_is_group(vb->type) && fr_value_box_list_tainted(&vb->vb_group)) return true;
6364 if (vb->tainted) return true;
6365 }
6366
6367 return false;
6368}
6369
6370/** Taint every list member (and their children)
6371 *
6372 * @param[in] head of list.
6373 */
6374void fr_value_box_list_taint(fr_value_box_list_t *head)
6375{
6376 fr_value_box_t *vb = NULL;
6377
6378 while ((vb = fr_value_box_list_next(head, vb))) {
6379 if (fr_type_is_group(vb->type)) fr_value_box_list_taint(&vb->vb_group);
6381 vb->tainted = true;
6382 }
6383}
6384
6385/** Untaint every list member (and their children)
6386 *
6387 * @param[in] head of list.
6388 */
6389void fr_value_box_list_untaint(fr_value_box_list_t *head)
6390{
6391 fr_value_box_t *vb = NULL;
6392
6393 while ((vb = fr_value_box_list_next(head, vb))) {
6394 if (fr_type_is_group(vb->type)) fr_value_box_list_untaint(&vb->vb_group);
6395 vb->tainted = false;
6396 }
6397}
6398
6399/** Validation function to check that a fr_value_box_t is correctly initialised
6400 *
6401 */
6402void fr_value_box_verify(char const *file, int line, fr_value_box_t const *vb)
6403{
6404DIAG_OFF(nonnull-compare)
6405 /*
6406 * nonnull only does something if we're building
6407 * with ubsan... We still want to assert event
6408 * if we're building without sanitizers.
6409 */
6410 fr_fatal_assert_msg(vb, "CONSISTENCY CHECK FAILED %s[%i]: fr_value_box_t pointer was NULL", file, line);
6411DIAG_ON(nonnull-compare)
6412
6413 if (vb->talloced) vb = talloc_get_type_abort_const(vb, fr_value_box_t);
6414
6415#ifndef NDEBUG
6416 fr_fatal_assert_msg(vb->magic == FR_VALUE_BOX_MAGIC, "CONSISTENCY CHECK FAILED %s[%i]: fr_value_box_t magic "
6417 "incorrect, expected %" PRIx64 ", got %" PRIx64, file, line, FR_VALUE_BOX_MAGIC, vb->magic);
6418#endif
6419 switch (vb->type) {
6420 case FR_TYPE_STRING:
6421 fr_fatal_assert_msg(vb->vb_strvalue, "CONSISTENCY CHECK FAILED %s[%d]: fr_value_box_t strvalue field "
6422 "was NULL", file, line);
6423 fr_fatal_assert_msg(vb->vb_strvalue[vb->vb_length] == '\0',
6424 "CONSISTENCY CHECK FAILED %s[%i]: fr_value_box_t strvalue field "
6425 "not null terminated", file, line);
6426 if (vb->talloced) {
6427 size_t len = talloc_array_length(vb->vb_strvalue);
6428
6429 /* We always \0 terminate to be safe, even though most things should use the len field */
6430 if (len <= vb->vb_length) {
6431 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: Expected fr_value_box_t->vb_strvalue talloc buffer "
6432 "len >= %zu, got %zu",
6433 file, line, vb->vb_length + 1, len);
6434 }
6435 }
6436 break;
6437
6438 case FR_TYPE_OCTETS:
6439 fr_fatal_assert_msg(vb->vb_octets, "CONSISTENCY CHECK FAILED %s[%d]: fr_value_box_t octets field "
6440 "was NULL", file, line);
6441 break;
6442
6443 case FR_TYPE_VOID:
6444 fr_fatal_assert_msg(vb->vb_void, "CONSISTENCY CHECK FAILED %s[%d]: fr_value_box_t ptr field "
6445 "was NULL", file, line);
6446 break;
6447
6448 case FR_TYPE_GROUP:
6449 fr_value_box_list_verify(file, line, &vb->vb_group);
6450 break;
6451
6452 default:
6453 break;
6454 }
6455}
6456
6457void fr_value_box_list_verify(char const *file, int line, fr_value_box_list_t const *list)
6458{
6460}
6461
6462/** Mark a value-box as "safe", of a particular type.
6463 *
6464 */
6466{
6467 /*
6468 * Don't over-ride value-boxes which are already safe, unless we want to mark them as being
6469 * completely unsafe.
6470 */
6471 if ((vb->safe_for == FR_VALUE_BOX_SAFE_FOR_ANY) &&
6472 (safe_for != FR_VALUE_BOX_SAFE_FOR_NONE)) {
6473 fr_assert(!vb->tainted);
6474 return;
6475 }
6476
6477 vb->safe_for = safe_for;
6478}
6479
6480/** Mark a value-box as "unsafe"
6481 *
6482 * This always succeeds, and there are no side effects.
6483 */
6485{
6486 vb->safe_for = FR_VALUE_BOX_SAFE_FOR_NONE;
6487}
6488
6489/** Set the escaped flag for all value boxes in a list
6490 *
6491 * @note Only operates on a single level.
6492 *
6493 * @param[in] list to operate on.
6494 * @param[in] safe_for value to set.
6495 */
6496void fr_value_box_list_mark_safe_for(fr_value_box_list_t *list, fr_value_box_safe_for_t safe_for)
6497{
6498 fr_value_box_list_foreach(list, vb) {
6499 /*
6500 * Don't over-ride value-boxes which are already safe.
6501 */
6502 if (vb->safe_for == FR_VALUE_BOX_SAFE_FOR_ANY) {
6503 fr_assert(!vb->tainted);
6504
6505 } else {
6506 vb->safe_for = safe_for;
6507 }
6508 }
6509}
6510
6511/** Copy the safety values from one box to another.
6512 *
6513 */
6515{
6516 if (out == in) return;
6517
6518 out->safe_for = in->safe_for;
6519 out->tainted = in->tainted;
6520 out->secret = in->secret;
6521}
6522
6523/** Copy the safety values from one box to another.
6524 *
6525 * But note that we have changed the output format, so we reset the "safe_for" value to NONE.
6526 */
6528{
6529 out->safe_for = FR_VALUE_BOX_SAFE_FOR_NONE;
6530 out->tainted = in->tainted;
6531 out->secret = in->secret;
6532}
6533
6534/** Merge safety results.
6535 */
6537{
6538 if (out == in) return;
6539
6540 /*
6541 * If we're already at no safety, then we don't need to do anything.
6542 *
6543 * Otherwise we update the safety only if we need to change it.
6544 */
6545 if ((out->safe_for != FR_VALUE_BOX_SAFE_FOR_NONE) &&
6546 (out->safe_for != in->safe_for)) {
6547 /*
6548 * If the output is anything, then the input is more restrictive, so we switch to that.
6549 *
6550 * Otherwise the values are different. Either it's X/Y, or NONE/X, or X/NONE. In which
6551 * case the answer is always NONE.
6552 */
6553 if (out->safe_for == FR_VALUE_BOX_SAFE_FOR_ANY) {
6554 out->safe_for = in->safe_for;
6555
6556 } else {
6557 out->safe_for = FR_VALUE_BOX_SAFE_FOR_NONE;
6558 }
6559 }
6560
6561 out->tainted |= in->tainted;
6562 out->secret |= in->secret;
6563}
6564
6565
6566/** Check truthiness of values.
6567 *
6568 * The casting rules for expressions / conditions are slightly
6569 * different than fr_value_box_cast(). Largely because that
6570 * function is used to parse configuration files, and parses "yes
6571 * / no" and "true / false" strings, even if there's no
6572 * fr_dict_attr_t passed to it.
6573 */
6575{
6576 fr_value_box_t box;
6577
6578 switch (in->type) {
6579 case FR_TYPE_NULL:
6581 return false;
6582
6583 case FR_TYPE_GROUP:
6584 return (fr_value_box_list_num_elements(&in->vb_group) > 0);
6585
6586 case FR_TYPE_BOOL:
6587 return in->vb_bool;
6588
6589 case FR_TYPE_STRING:
6590 case FR_TYPE_OCTETS:
6591 return (in->vb_length > 0);
6592
6593 case FR_TYPE_IPV4_ADDR:
6594 case FR_TYPE_IPV6_ADDR:
6595 return !fr_ipaddr_is_inaddr_any(&in->vb_ip);
6596
6599 return !((in->vb_ip.prefix == 0) && fr_ipaddr_is_inaddr_any(&in->vb_ip));
6600
6601 default:
6603 (void) fr_value_box_cast(NULL, &box, FR_TYPE_BOOL, NULL, in);
6604 return box.vb_bool;
6605 }
6606}
6607
6608#define INFO_INDENT(_fmt, ...) FR_FAULT_LOG("%*s"_fmt, depth * 2, " ", ## __VA_ARGS__)
6609
6610static void _fr_value_box_debug(fr_value_box_t const *vb, int depth, int idx);
6611static void _fr_value_box_list_debug(fr_value_box_list_t const *head, int depth)
6612{
6613 int i = 0;
6614
6615 INFO_INDENT("{");
6617 INFO_INDENT("}");
6618}
6619
6620/** Print a list of value boxes as info messages
6621 *
6622 * @note Call directly from the debugger
6623 */
6624void fr_value_box_list_debug(fr_value_box_list_t const *head)
6625{
6627}
6628
6629static void _fr_value_box_debug(fr_value_box_t const *vb, int depth, int idx)
6630{
6631 char *value;
6632 char buffer[64];
6633
6634 if (fr_type_is_structural(vb->type)) {
6635 _fr_value_box_list_debug(&vb->vb_group, depth + 1);
6636 return;
6637 }
6638
6639 buffer[0] = '\0';
6640 if (vb->type == FR_TYPE_TIME_DELTA) {
6641 if (!vb->enumv) {
6642 snprintf(buffer, sizeof(buffer), " (sec!) %" PRId64, fr_time_delta_unwrap(vb->vb_time_delta));
6643 } else {
6644 snprintf(buffer, sizeof(buffer), " (%s) %" PRId64,
6645 fr_table_str_by_value(fr_time_precision_table, vb->enumv->flags.flag_time_res, "?"),
6646 fr_time_delta_unwrap(vb->vb_time_delta));
6647 }
6648 }
6649
6650 fr_value_box_aprint(NULL, &value, vb, NULL);
6651 if (idx >= 0) {
6652 INFO_INDENT("[%d] (%s) %s", idx, fr_type_to_str(vb->type), value);
6653 INFO_INDENT(" %s %s %lx%s",
6654 vb->secret ? "s" : "-",
6655 vb->tainted ? "t" : "-",
6656 vb->safe_for, buffer);
6657 } else {
6658 INFO_INDENT("(%s) %s", fr_type_to_str(vb->type), value);
6659 INFO_INDENT(" %s %s %lx%s",
6660 vb->secret ? "s" : "-",
6661 vb->tainted ? "t" : "-",
6662 vb->safe_for, buffer);
6663 }
6665}
6666
6667/** Print the value of a box as info messages
6668 *
6669 * @note Call directly from the debugger
6670 */
6672{
6673 _fr_value_box_debug(vb, 0, -1);
6674}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
va_end(args)
int n
Definition acutest.h:577
static int const char * fmt
Definition acutest.h:573
int const char int line
Definition acutest.h:702
va_start(args, fmt)
#define fr_base16_encode(_out, _in)
Definition base16.h:57
#define fr_base16_decode(_err, _out, _in, _no_trailing)
Definition base16.h:95
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define DIAG_ON(_x)
Definition build.h:460
#define SIZEOF_MEMBER(_t, _m)
Definition build.h:338
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:112
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define DIAG_OFF(_x)
Definition build.h:459
static fr_atomic_queue_t * aq
static size_t min(size_t x, size_t y)
Definition dbuff.c:66
int fr_dbuff_trim_talloc(fr_dbuff_t *dbuff, size_t len)
Trim a talloced dbuff to the minimum length required to represent the contained string.
Definition dbuff.c:297
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:767
#define FR_DBUFF_OUT_UINT64V_RETURN(_num, _dbuff_or_marker, _len)
Read bytes from a dbuff or marker and interpret them as a network order unsigned integer.
Definition dbuff.h:1858
#define fr_dbuff_set(_dst, _src)
Set the 'current' position in a dbuff or marker using another dbuff or marker, a char pointer,...
Definition dbuff.h:1004
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition dbuff.h:354
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:898
#define FR_DBUFF_OUT_INT64V_RETURN(_num, _dbuff_or_marker, _len)
Read bytes from a dbuff or marker and interpret them as a network order unsigned integer.
Definition dbuff.h:1898
#define fr_dbuff_buff(_dbuff_or_marker)
Return the underlying buffer in a dbuff or one of marker.
Definition dbuff.h:882
#define fr_dbuff_out_memcpy(_out, _dbuff_or_marker, _outlen)
Copy exactly _outlen bytes from the dbuff.
Definition dbuff.h:1732
#define FR_DBUFF_MEMSET_RETURN(_dbuff_or_marker, _c, _inlen)
Set _inlen bytes of a dbuff or marker to _c returning if there is insufficient space.
Definition dbuff.h:1508
#define FR_DBUFF_OUT_MEMCPY_RETURN(_out, _dbuff_or_marker, _outlen)
Copy outlen bytes from the dbuff returning if there's insufficient data in the dbuff.
Definition dbuff.h:1752
#define FR_DBUFF_IN_MEMCPY_RETURN(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1382
#define fr_dbuff_in_memcpy(_dbuff_or_marker, _in, _inlen)
Copy exactly _inlen bytes into a dbuff or marker.
Definition dbuff.h:1350
#define FR_DBUFF_IN_RETURN(_dbuff_or_marker, _in)
Copy data from a fixed sized C type into a dbuff returning if there is insufficient space.
Definition dbuff.h:1585
#define FR_DBUFF(_dbuff_or_marker)
Create a new dbuff pointing to the same underlying buffer.
Definition dbuff.h:222
#define FR_DBUFF_OUT_RETURN(_out, _dbuff_or_marker)
Copy data from a dbuff or marker to a fixed sized C type returning if there is insufficient data.
Definition dbuff.h:1818
static fr_dbuff_t * fr_dbuff_init_talloc(TALLOC_CTX *ctx, fr_dbuff_t *dbuff, fr_dbuff_uctx_talloc_t *tctx, size_t init, size_t max)
Initialise a special dbuff which automatically extends as additional data is written.
Definition dbuff.h:411
#define FR_DBUFF_IN_BYTES_RETURN(_dbuff_or_marker,...)
Copy a byte sequence into a dbuff or marker returning if there's insufficient space.
Definition dbuff.h:1472
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:514
#define fr_fatal_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:191
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
#define fr_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error.
Definition debug.h:216
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:156
#define fr_fatal_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:184
static fr_slen_t err
Definition dict.h:833
bool const fr_dict_enum_allowed_chars[UINT8_MAX+1]
Characters that are allowed in dictionary enumeration value names.
Definition dict_util.c:67
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:235
char const * fr_dict_enum_name_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the name of an enum value in a fr_dict_attr_t.
Definition dict_util.c:3384
fr_dict_enum_value_t * fr_dict_enum_by_name(fr_dict_attr_t const *da, char const *name, ssize_t len)
Definition dict_util.c:3397
#define da_is_length_field(_da)
Definition dict.h:159
static fr_slen_t in
Definition dict.h:833
@ FLAG_LENGTH_UINT8
string / octets type is prefixed by uint8 of length
Definition dict.h:153
@ FLAG_LENGTH_UINT16
string / octets type is prefixed by uint16 of length
Definition dict.h:154
Value of an enumerated attribute.
Definition dict.h:231
Test enumeration values.
Definition dict_test.h:92
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:378
uint32_t fr_hash(void const *data, size_t size)
Definition hash.c:812
free(array)
int fr_ipaddr_is_prefix(fr_ipaddr_t const *ipaddr)
Determine if an address is a prefix.
Definition inet.c:125
char * fr_inet_ntop_prefix(char out[static FR_IPADDR_PREFIX_STRLEN], size_t outlen, fr_ipaddr_t const *addr)
Print a fr_ipaddr_t as a CIDR style network prefix.
Definition inet.c:1073
int fr_inet_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask)
Parse an IPv6 address or IPv6 prefix in presentation format (and others)
Definition inet.c:629
bool fr_hostname_lookups
hostname -> IP lookups?
Definition inet.c:52
int fr_inet_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve, bool mask)
Simple wrapper to decide whether an IP value is v4 or v6 and call the appropriate parser.
Definition inet.c:778
int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr)
Determine if an address is the INADDR_ANY address for its address family.
Definition inet.c:62
char * fr_inet_ntop(char out[static FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t const *addr)
Print the address portion of a fr_ipaddr_t.
Definition inet.c:1018
int8_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
Definition inet.c:1346
char * fr_inet_ifid_ntop(char *out, size_t outlen, uint8_t const *ifid)
Print an interface-id in standard colon notation.
Definition inet.c:1099
uint8_t * fr_inet_ifid_pton(uint8_t out[static 8], char const *ifid_str)
Convert interface-id in colon notation to 8 byte binary form.
Definition inet.c:1113
uint8_t prefix
Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
Definition inet.h:69
int af
Address family.
Definition inet.h:64
uint8_t addr[6]
Ethernet address.
Definition inet.h:46
Struct to represent an ethernet address.
Definition inet.h:45
IPv4/6 prefix.
talloc_free(reap)
#define fr_multiply(_out, _a, _b)
Multiplies two integers together.
Definition math.h:118
static const uint8_t * zero
Definition md4.c:358
unsigned short uint16_t
size_t fr_sbuff_out_unescape_until(fr_sbuff_t *out, fr_sbuff_t *in, size_t len, fr_sbuff_term_t const *tt, fr_sbuff_unescape_rules_t const *u_rules)
fr_type_t
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_FLOAT32
Single precision floating point.
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_INT8
8 Bit signed integer.
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_ETHERNET
48 Bit Mac-Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_MAX
Number of defined data types.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_INT16
16 Bit signed integer.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_COMBO_IP_PREFIX
IPv4 or IPv6 address prefix depending on length.
@ FR_TYPE_VALUE_BOX
A boxed value.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_INT32
32 Bit signed integer.
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_SIZE
Unsigned integer capable of representing any memory address on the local system.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ FR_TYPE_IFID
Interface ID.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
@ FR_TYPE_FLOAT64
Double precision floating point.
unsigned int uint32_t
int fr_inet_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask_bits)
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
unsigned long int size_t
#define UINT8_MAX
fr_sbuff_parse_error_t
@ FR_SBUFF_PARSE_ERROR_NOT_FOUND
String does not contain a token matching the output type.
@ FR_SBUFF_PARSE_OK
No error.
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
int fr_digest_cmp(uint8_t const *a, uint8_t const *b, size_t length)
Do a comparison of two authentication digests by comparing the FULL data.
Definition misc.c:472
void * memset_explicit(void *ptr, int ch, size_t len)
Definition missing.c:620
#define check(_handle, _len_p)
Definition bio.c:44
static uint64_t fr_nbo_to_uint64(uint8_t const data[static sizeof(uint64_t)])
Read an unsigned 64bit integer from wire format (big endian)
Definition nbo.h:177
static void fr_nbo_from_uint64(uint8_t out[static sizeof(uint64_t)], uint64_t num)
Write out an unsigned 64bit integer in wire format (big endian)
Definition nbo.h:72
char * fr_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap)
Definition print.c:851
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:81
static uint32_t mask
Definition rbmonkey.c:39
static char const * name
int fr_sbuff_trim_talloc(fr_sbuff_t *sbuff, size_t len)
Trim a talloced sbuff to the minimum length required to represent the contained string.
Definition sbuff.c:421
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static UINT8_MAX+1], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
Definition sbuff.c:1779
ssize_t fr_sbuff_in_escape(fr_sbuff_t *sbuff, char const *in, size_t inlen, fr_sbuff_escape_rules_t const *e_rules)
Print an escaped string to an sbuff.
Definition sbuff.c:1622
bool const sbuff_char_class_hex[UINT8_MAX+1]
Definition sbuff.c:94
bool const sbuff_char_class_uint[UINT8_MAX+1]
Definition sbuff.c:60
bool const sbuff_char_class_hostname[UINT8_MAX+1]
Definition sbuff.c:82
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
Definition sbuff.c:2154
ssize_t fr_sbuff_in_bstrncpy(fr_sbuff_t *sbuff, char const *str, size_t len)
Copy bytes into the sbuff up to the first \0.
Definition sbuff.c:1482
size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr)
Wind position until we hit a character in the terminal set.
Definition sbuff.c:1854
size_t fr_sbuff_out_bstrncpy(fr_sbuff_t *out, fr_sbuff_t *in, size_t len)
Copy as many bytes as possible from a sbuff to a sbuff.
Definition sbuff.c:725
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
Definition sbuff.c:2090
#define fr_sbuff_start(_sbuff_or_marker)
#define fr_sbuff_adv_past_str_literal(_sbuff, _needle)
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_adv_past_strcase_literal(_sbuff, _needle)
#define fr_sbuff_current(_sbuff_or_marker)
char chr
Character at the start of an escape sequence.
Definition sbuff.h:204
#define FR_SBUFF_IN_ESCAPE_BUFFER_RETURN(...)
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition sbuff.h:192
char const * name
Name for rule set to aid we debugging.
Definition sbuff.h:202
#define FR_SBUFF_IN_STRCPY_LITERAL_RETURN(_sbuff, _str)
#define fr_sbuff_extend(_sbuff_or_marker)
#define fr_sbuff_buff(_sbuff_or_marker)
#define FR_SBUFF_RETURN(_func, _sbuff,...)
#define FR_SBUFF_ERROR_RETURN(_sbuff_or_marker)
#define FR_SBUFF_SET_RETURN(_dst, _src)
#define FR_SBUFF_IN_SPRINTF_RETURN(...)
#define SBUFF_CHAR_UNPRINTABLES_EXTENDED
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define fr_sbuff_out(_err, _out, _in)
#define FR_SBUFF_IN_ESCAPE_RETURN(...)
#define fr_sbuff_remaining(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
#define SBUFF_CHAR_UNPRINTABLES_LOW
#define fr_sbuff_used(_sbuff_or_marker)
#define FR_SBUFF_TERM(_str)
Initialise a terminal structure with a single string.
Definition sbuff.h:180
#define FR_SBUFF_IN_STRCPY_RETURN(...)
#define FR_SBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
Talloc sbuff extension structure.
Definition sbuff.h:139
Set of parsing rules for *unescape_until functions.
fr_slen_t fr_size_from_str(size_t *out, fr_sbuff_t *in)
Parse a size string with optional unit.
Definition size.c:40
fr_slen_t fr_size_to_str(fr_sbuff_t *out, size_t in)
Print a size string with unit.
Definition size.c:155
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
fr_aka_sim_id_type_t type
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
char * talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc,...)
Concatenate to + ...
Definition talloc.c:692
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:560
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:441
#define talloc_get_type_abort_const
Definition talloc.h:282
fr_table_num_ordered_t const fr_time_precision_table[]
Definition time.c:46
fr_slen_t fr_time_delta_from_substr(fr_time_delta_t *out, fr_sbuff_t *in, fr_time_res_t hint, bool no_trailing, fr_sbuff_term_t const *tt)
Create fr_time_delta_t from a string.
Definition time.c:214
int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_res_t hint)
Convert string in various formats to a fr_unix_time_t.
Definition time.c:794
int64_t fr_time_scale(int64_t t, fr_time_res_t hint)
Scale an input time to NSEC, clamping it at max / min.
Definition time.c:683
fr_slen_t fr_time_delta_to_str(fr_sbuff_t *out, fr_time_delta_t delta, fr_time_res_t res, bool is_unsigned)
Print fr_time_delta_t to a string with an appropriate suffix.
Definition time.c:435
fr_slen_t fr_unix_time_to_str(fr_sbuff_t *out, fr_unix_time_t time, fr_time_res_t res, bool utc)
Convert unix time to string.
Definition time.c:1122
int64_t const fr_time_multiplier_by_res[]
Definition time.c:32
static fr_time_delta_t fr_time_delta_from_integer(bool *overflow, int64_t integer, fr_time_res_t res)
Definition time.h:548
static int64_t fr_time_delta_to_integer(fr_time_delta_t delta, fr_time_res_t res)
Definition time.h:627
static fr_unix_time_t fr_unix_time_from_nsec(int64_t nsec)
Definition time.h:423
static int64_t fr_time_delta_unwrap(fr_time_delta_t time)
Definition time.h:154
static int8_t fr_time_delta_cmp(fr_time_delta_t a, fr_time_delta_t b)
Compare two fr_time_delta_t values.
Definition time.h:930
#define fr_time_delta_isneg(_a)
Definition time.h:291
#define fr_time_delta_wrap(_time)
Definition time.h:152
#define fr_unix_time_wrap(_time)
Definition time.h:160
fr_time_res_t
The base resolution for print parse operations.
Definition time.h:48
@ FR_TIME_RES_NSEC
Definition time.h:60
@ FR_TIME_RES_SEC
Definition time.h:50
static fr_unix_time_t fr_unix_time_from_integer(bool *overflow, int64_t integer, fr_time_res_t res)
Definition time.h:411
#define NSEC
Definition time.h:379
static int8_t fr_unix_time_cmp(fr_unix_time_t a, fr_unix_time_t b)
Compare two fr_unix_time_t values.
Definition time.h:944
static uint64_t fr_unix_time_unwrap(fr_unix_time_t time)
Definition time.h:161
static int64_t fr_unix_time_to_integer(fr_unix_time_t delta, fr_time_res_t res)
Definition time.h:486
const char fr_token_quote[T_TOKEN_LAST]
Convert tokens back to a quoting character.
Definition token.c:155
enum fr_token fr_token_t
@ T_SINGLE_QUOTED_STRING
Definition token.h:122
@ T_BARE_WORD
Definition token.h:120
@ T_BACK_QUOTED_STRING
Definition token.h:123
@ T_OP_NE
Definition token.h:97
@ T_OP_REG_EQ
Definition token.h:102
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
@ T_OP_CMP_EQ
Definition token.h:106
@ T_OP_LE
Definition token.h:100
@ T_OP_GE
Definition token.h:98
@ T_OP_GT
Definition token.h:99
@ T_SOLIDUS_QUOTED_STRING
Definition token.h:124
@ T_OP_LT
Definition token.h:101
@ T_OP_REG_NE
Definition token.h:103
#define T_TOKEN_LAST
Definition token.h:129
static fr_slen_t head
Definition xlat.h:416
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define FR_TYPE_VARIABLE_SIZE
Definition types.h:291
#define FR_TYPE_QUOTED
Definition types.h:292
#define FR_TYPE_STRUCTURAL_EXCEPT_GROUP
Definition types.h:295
#define fr_type_is_non_leaf(_x)
Definition types.h:373
#define fr_type_is_group(_x)
Definition types.h:355
#define fr_type_is_variable_size(_x)
Definition types.h:367
#define fr_type_is_structural(_x)
Definition types.h:371
#define FR_TYPE_NON_LEAF
Definition types.h:298
#define fr_type_is_fixed_size(_x)
Definition types.h:366
#define FR_TYPE_STRUCTURAL
Definition types.h:296
#define fr_type_is_ip(_x)
Definition types.h:364
#define FR_TYPE_INTEGER_EXCEPT_BOOL
Definition types.h:283
#define FR_TYPE_IP
Definition types.h:288
#define FR_TYPE_INTEGER
Definition types.h:284
#define fr_type_is_leaf(_x)
Definition types.h:372
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:433
#define FR_TYPE_NUMERIC
Definition types.h:286
#define FR_TYPE_FIXED_SIZE
Definition types.h:290
int fr_value_box_bstrndup_dbuff(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, fr_dbuff_t *dbuff, size_t len, bool tainted)
Definition value.c:4406
void fr_value_box_list_verify(char const *file, int line, fr_value_box_list_t const *list)
Definition value.c:6457
static void _fr_value_box_list_debug(fr_value_box_list_t const *head, int depth)
Definition value.c:6611
static void _fr_value_box_debug(fr_value_box_t const *vb, int depth, int idx)
Definition value.c:6629
void fr_value_box_memdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, bool tainted)
Assign a talloced buffer to a box, but don't copy it.
Definition value.c:4721
size_t const fr_value_box_field_sizes[]
How many bytes wide each of the value data fields are.
Definition value.c:148
fr_sbuff_escape_rules_t fr_value_escape_double
Definition value.c:349
int fr_value_box_hton(fr_value_box_t *dst, fr_value_box_t const *src)
Performs byte order reversal for types that need it.
Definition value.c:1277
size_t fr_value_box_network_length(fr_value_box_t const *value)
Get the size of the value held by the fr_value_box_t.
Definition value.c:1371
int fr_value_box_vasprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted, char const *fmt, va_list ap)
Print a formatted string using our internal printf wrapper and assign it to a value box.
Definition value.c:4217
void fr_value_box_debug(fr_value_box_t const *vb)
Print the value of a box as info messages.
Definition value.c:6671
#define INFO_INDENT(_fmt,...)
Definition value.c:6608
void fr_value_box_mark_unsafe(fr_value_box_t *vb)
Mark a value-box as "unsafe".
Definition value.c:6484
ssize_t fr_value_box_list_concat_as_string(fr_value_box_t *safety, fr_sbuff_t *sbuff, fr_value_box_list_t *list, char const *sep, size_t sep_len, fr_sbuff_escape_rules_t const *e_rules, fr_value_box_list_action_t proc_action, fr_value_box_safe_for_t safe_for, bool flatten)
Concatenate a list of value boxes together.
Definition value.c:5718
fr_sbuff_escape_rules_t fr_value_escape_single
Definition value.c:387
int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb)
Trim the length of the string buffer to match the length of the C string.
Definition value.c:4187
uint32_t fr_value_box_hash(fr_value_box_t const *vb)
Hash the contents of a value box.
Definition value.c:6301
ssize_t fr_value_box_print(fr_sbuff_t *out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules)
Print one boxed value to a string.
Definition value.c:5487
fr_sbuff_parse_rules_t const value_parse_rules_single_3quoted
Definition value.c:579
static fr_slen_t fr_value_box_from_numeric_substr(fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *rules, bool tainted)
Convert integer encoded as string to a fr_value_box_t type.
Definition value.c:4790
static int fr_value_box_cast_to_strvalue(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any supported type to a string.
Definition value.c:2255
fr_sbuff_parse_rules_t const value_parse_rules_double_unquoted
Definition value.c:483
char * fr_value_box_list_aprint_secure(TALLOC_CTX *ctx, fr_value_box_list_t const *list, char const *delim, fr_sbuff_escape_rules_t const *e_rules)
Concatenate the string representations of a list of value boxes together hiding "secret" values.
Definition value.c:6250
fr_sbuff_parse_rules_t const value_parse_rules_solidus_quoted
Definition value.c:558
ssize_t fr_value_box_from_network(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t type, fr_dict_attr_t const *enumv, fr_dbuff_t *dbuff, size_t len, bool tainted)
Decode a fr_value_box_t from serialized binary data.
Definition value.c:1800
int fr_value_box_mem_alloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, size_t len, bool tainted)
Pre-allocate an octets buffer for filling by the caller.
Definition value.c:4526
int fr_value_box_memdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, bool tainted)
Copy a talloced buffer to a fr_value_box_t.
Definition value.c:4681
#define network_min_size(_x)
Sanity checks.
Definition value.c:106
int fr_value_box_bstrdup_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated talloced buffer to a fr_value_box_t.
Definition value.c:4442
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:3574
int fr_value_box_asprintf(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, bool tainted, char const *fmt,...)
Print a formatted string using our internal printf wrapper and assign it to a value box.
Definition value.c:4248
fr_sbuff_parse_rules_t const * value_parse_rules_unquoted_char[UINT8_MAX]
Definition value.c:515
fr_sbuff_parse_rules_t const * value_parse_rules_quoted[T_TOKEN_LAST]
Parse rules for quoted strings.
Definition value.c:605
char * fr_value_box_list_aprint(TALLOC_CTX *ctx, fr_value_box_list_t const *list, char const *delim, fr_sbuff_escape_rules_t const *e_rules)
Concatenate the string representations of a list of value boxes together.
Definition value.c:6200
int fr_value_box_mem_realloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:4559
static size_t const fr_value_box_network_sizes[FR_TYPE_MAX+1][2]
Definition value.c:108
static int fr_value_box_cast_to_float(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any value to a floating point value.
Definition value.c:3450
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:722
#define SIGN_BIT_HIGH(_int, _len)
size_t const fr_value_box_offsets[]
Where the value starts in the fr_value_box_t.
Definition value.c:188
fr_sbuff_parse_rules_t const * value_parse_rules_quoted_char[UINT8_MAX]
Definition value.c:613
#define CAST_IP_FIX_COMBO
Definition value.c:2397
void fr_value_box_list_untaint(fr_value_box_list_t *head)
Untaint every list member (and their children)
Definition value.c:6389
fr_sbuff_parse_rules_t const value_parse_rules_bareword_unquoted
Default formatting rules.
Definition value.c:479
static int fr_value_box_cast_to_ipv4addr(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any supported type to an IPv4 address.
Definition value.c:2429
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:3963
fr_sbuff_parse_rules_t const value_parse_rules_single_unquoted
Definition value.c:487
int fr_value_box_cmp_op(fr_token_t op, fr_value_box_t const *a, fr_value_box_t const *b)
Compare two attributes using an operator.
Definition value.c:975
int fr_value_box_list_escape_in_place(fr_value_box_list_t *list, fr_value_box_escape_t const *escape, void *uctx)
Escape a list of value boxes in place.
Definition value.c:6155
fr_sbuff_unescape_rules_t fr_value_unescape_solidus
Definition value.c:295
bool fr_value_box_is_truthy(fr_value_box_t const *in)
Check truthiness of values.
Definition value.c:6574
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:3795
fr_sbuff_parse_rules_t const value_parse_rules_single_quoted
Definition value.c:552
static uint8_t const v4_v6_map[]
v4 to v6 mapping prefix
Definition value.c:2241
fr_sbuff_unescape_rules_t * fr_value_unescape_by_char[UINT8_MAX+1]
Definition value.c:342
void fr_value_box_list_debug(fr_value_box_list_t const *head)
Print a list of value boxes as info messages.
Definition value.c:6624
void fr_value_box_memdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Assign a buffer to a box, but don't copy it.
Definition value.c:4703
fr_sbuff_escape_rules_t fr_value_escape_solidus
Definition value.c:397
void fr_value_box_copy_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *src)
Perform a shallow copy of a value_box.
Definition value.c:4068
ssize_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:5450
ssize_t fr_value_box_list_concat_as_octets(fr_value_box_t *safety, fr_dbuff_t *dbuff, fr_value_box_list_t *list, uint8_t const *sep, size_t sep_len, fr_value_box_list_action_t proc_action, bool flatten)
Concatenate a list of value boxes together.
Definition value.c:5834
static int fr_value_box_cast_to_octets(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any supported type to octets.
Definition value.c:2306
void fr_value_box_increment(fr_value_box_t *vb)
Increment a boxed value.
Definition value.c:4737
fr_sbuff_escape_rules_t * fr_value_escape_by_quote[T_TOKEN_LAST]
Definition value.c:440
void _fr_value_box_mark_safe_for(fr_value_box_t *vb, fr_value_box_safe_for_t safe_for)
Mark a value-box as "safe", of a particular type.
Definition value.c:6465
size_t fr_value_str_unescape(fr_sbuff_t *out, fr_sbuff_t *in, size_t inlen, char quote)
Convert a string value with escape sequences into its binary form.
Definition value.c:1174
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:3904
void fr_value_box_verify(char const *file, int line, fr_value_box_t const *vb)
Validation function to check that a fr_value_box_t is correctly initialised.
Definition value.c:6402
fr_sbuff_parse_rules_t const * value_parse_rules_3quoted[T_TOKEN_LAST]
Definition value.c:621
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4161
#define network_max_size(_x)
Definition value.c:107
#define COMPARE(_type)
void fr_value_box_strdup_shallow_replace(fr_value_box_t *vb, char const *src, ssize_t len)
Free the existing buffer (if talloced) associated with the valuebox, and replace it with a new one.
Definition value.c:4286
ssize_t fr_value_box_print_quoted(fr_sbuff_t *out, fr_value_box_t const *data, fr_token_t quote)
Print one boxed value to a string with quotes (where needed)
Definition value.c:5675
fr_sbuff_parse_rules_t const value_parse_rules_double_3quoted
Definition value.c:573
static int fr_value_box_cast_to_integer(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any value to a signed or unsigned integer.
Definition value.c:3190
fr_sbuff_escape_rules_t fr_value_escape_unprintables
Definition value.c:454
static int fr_value_box_cast_to_ipv6prefix(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any supported type to an IPv6 address.
Definition value.c:2781
void fr_value_box_safety_copy_changed(fr_value_box_t *out, fr_value_box_t const *in)
Copy the safety values from one box to another.
Definition value.c:6527
fr_sbuff_escape_rules_t fr_value_escape_backtick
Definition value.c:418
int fr_value_box_ipaddr(fr_value_box_t *dst, fr_dict_attr_t const *enumv, fr_ipaddr_t const *ipaddr, bool tainted)
Assign a fr_value_box_t value from an fr_ipaddr_t.
Definition value.c:3853
void fr_value_box_list_taint(fr_value_box_list_t *head)
Taint every list member (and their children)
Definition value.c:6374
static int fr_value_box_cidr_cmp_op(fr_token_t op, int bytes, uint8_t a_net, uint8_t const *a, uint8_t b_net, uint8_t const *b)
Definition value.c:853
static void fr_value_box_copy_meta(fr_value_box_t *dst, fr_value_box_t const *src)
Copy flags and type data from one value box to another.
Definition value.c:637
void fr_value_box_list_mark_safe_for(fr_value_box_list_t *list, fr_value_box_safe_for_t safe_for)
Set the escaped flag for all value boxes in a list.
Definition value.c:6496
fr_sbuff_escape_rules_t * fr_value_escape_by_char[UINT8_MAX+1]
Definition value.c:447
static int fr_value_box_fixed_size_from_octets(fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert octets to a fixed size value box value.
Definition value.c:2193
static int fr_value_box_cast_to_bool(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any supported type to a bool.
Definition value.c:2943
fr_sbuff_unescape_rules_t fr_value_unescape_backtick
Definition value.c:316
int fr_value_unbox_ipaddr(fr_ipaddr_t *dst, fr_value_box_t *src)
Unbox an IP address performing a type check.
Definition value.c:3885
fr_sbuff_parse_rules_t const value_parse_rules_bareword_quoted
Definition value.c:523
void fr_value_box_safety_merge(fr_value_box_t *out, fr_value_box_t const *in)
Merge safety results.
Definition value.c:6536
fr_sbuff_parse_rules_t const value_parse_rules_backtick_3quoted
Definition value.c:591
static uint64_t const fr_value_box_integer_max[]
Definition value.c:225
void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4270
fr_sbuff_parse_rules_t const value_parse_rules_solidus_unquoted
Definition value.c:491
#define RETURN(_type)
fr_sbuff_parse_rules_t const value_parse_rules_backtick_quoted
Definition value.c:564
fr_sbuff_parse_rules_t const * value_parse_rules_unquoted[T_TOKEN_LAST]
Parse rules for non-quoted strings.
Definition value.c:507
static int fr_value_box_cast_to_ipv4prefix(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any supported type to an IPv6 address.
Definition value.c:2546
static int fr_value_box_cast_to_ethernet(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any supported type to an ethernet address.
Definition value.c:2882
fr_sbuff_unescape_rules_t fr_value_unescape_single
Definition value.c:284
void fr_value_box_safety_copy(fr_value_box_t *out, fr_value_box_t const *in)
Copy the safety values from one box to another.
Definition value.c:6514
fr_sbuff_parse_rules_t const value_parse_rules_backtick_unquoted
Definition value.c:495
fr_sbuff_parse_rules_t const value_parse_rules_double_quoted
Definition value.c:546
int fr_value_box_bstr_alloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, size_t len, bool tainted)
Alloc and assign an empty \0 terminated string to a fr_value_box_t.
Definition value.c:4305
fr_sbuff_unescape_rules_t * fr_value_unescape_by_quote[T_TOKEN_LAST]
Definition value.c:335
#define SIGN_PROMOTE(_int, _len)
fr_sbuff_parse_rules_t const value_parse_rules_solidus_3quoted
Definition value.c:585
int fr_value_box_steal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t *src)
Copy value data verbatim moving any buffers to the specified context.
Definition value.c:4092
int fr_value_box_to_key(uint8_t **out, size_t *outlen, fr_value_box_t const *value)
Get a key from a value box.
Definition value.c:2130
void fr_value_box_flatten(TALLOC_CTX *ctx, fr_value_box_list_t *list, bool steal, bool free)
Removes a single layer of nesting, moving all children into the parent list.
Definition value.c:6175
static int8_t float_cmp(double a, double b)
Compare two floating point numbers for equality.
Definition value.c:670
int fr_value_box_list_acopy(TALLOC_CTX *ctx, fr_value_box_list_t *out, fr_value_box_list_t const *in)
Do a full copy of a list of value boxes.
Definition value.c:6330
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:3946
bool fr_value_box_list_tainted(fr_value_box_list_t const *head)
Check to see if any list members (or their children) are tainted.
Definition value.c:6358
ssize_t fr_value_box_to_network(fr_dbuff_t *dbuff, fr_value_box_t const *value)
Encode a single value box, serializing its contents in generic network format.
Definition value.c:1450
static int64_t const fr_value_box_integer_min[]
Definition value.c:245
int fr_value_box_bstr_realloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:4338
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4382
int fr_value_box_memdup_dbuff(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, fr_dbuff_t *dbuff, size_t len, bool tainted)
Definition value.c:4648
int fr_regex_cmp_op(fr_token_t op, fr_value_box_t const *a, fr_value_box_t const *b)
Compare two boxes using an operator.
Definition regex.c:1392
fr_sbuff_unescape_rules_t fr_value_unescape_double
Definition value.c:265
int fr_value_box_bstrdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a talloced buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4487
size_t fr_value_substr_unescape(fr_sbuff_t *out, fr_sbuff_t *in, size_t inlen, char quote)
Convert a string value with escape sequences into its binary form.
Definition value.c:1247
fr_sbuff_escape_rules_t fr_value_escape_secret
Escape secret fields by simply mashing all data to '.
Definition value.c:380
ssize_t fr_value_box_from_substr(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *rules)
Convert string value to a fr_value_box_t type.
Definition value.c:4890
int fr_value_box_escape_in_place(fr_value_box_t *vb, fr_value_box_escape_t const *escape, void *uctx)
Escape a single value box in place.
Definition value.c:6102
void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Assign a string to to a fr_value_box_t.
Definition value.c:4466
static int fr_value_box_cast_to_ipv6addr(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any supported type to an IPv6 address.
Definition value.c:2664
int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Copy a buffer to a fr_value_box_t.
Definition value.c:4623
static int fr_value_box_cast_integer_to_integer(UNUSED TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert any signed or unsigned integer type to any other signed or unsigned integer type.
Definition value.c:3033
int fr_value_box_list_concat_in_place(TALLOC_CTX *ctx, fr_value_box_t *out, fr_value_box_list_t *list, fr_type_t type, fr_value_box_list_action_t proc_action, bool flatten, size_t max_size)
Concatenate a list of value boxes.
Definition value.c:5934
fr_value_box_list_action_t
Actions to perform when we process a box in a list.
Definition value.h:226
@ FR_VALUE_BOX_LIST_NONE
Do nothing to processed boxes.
Definition value.h:227
@ FR_VALUE_BOX_LIST_REMOVE
Remove the box from the input list.
Definition value.h:228
#define vb_should_free(_action)
Definition value.h:235
#define fr_value_box_list_foreach_safe(_list_head, _iter)
Definition value.h:218
#define vb_ether
Definition value.h:260
#define vb_date
Definition value.h:277
#define vb_int64
Definition value.h:272
#define vb_octets
Definition value.h:253
#define vb_should_free_value(_action)
Definition value.h:236
#define vb_should_remove(_action)
Definition value.h:237
#define vb_int32
Definition value.h:271
static int fr_value_box_memcpy_out(void *out, fr_value_box_t const *vb)
Copy the value of a value box to a field in a C struct.
Definition value.h:779
#define vb_int16
Definition value.h:270
static fr_slen_t fr_value_box_aprint(TALLOC_CTX *ctx, char **out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules) 1(fr_value_box_print
#define vb_uint8
Definition value.h:263
#define vb_length
Definition value.h:283
#define vb_int8
Definition value.h:269
static fr_slen_t data
Definition value.h:1274
static bool fr_value_box_contains_secret(fr_value_box_t const *box)
Definition value.h:1086
#define vb_float64
Definition value.h:275
#define FR_VALUE_BOX_NET_ERROR
Special value to indicate fr_value_box_from_network experienced a general error.
Definition value.h:1029
static uint8_t * fr_value_box_raw(fr_value_box_t const *vb, fr_type_t type)
Return a pointer to the "raw" value from a value-box.
Definition value.h:755
#define fr_box_strvalue_len(_val, _len)
Definition value.h:297
#define FR_VALUE_BOX_MAGIC
Definition value.h:91
#define fr_value_box_init_null(_vb)
Initialise an empty/null box that will be filled later.
Definition value.h:604
#define fr_value_box_is_safe_for(_box, _safe_for)
Definition value.h:1070
#define vb_ip
Definition value.h:257
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1012
fr_value_box_safe_for_t safe_for
Definition value.h:666
#define vb_uint16
Definition value.h:264
#define vb_bool
Definition value.h:262
#define vb_size
Definition value.h:279
#define FR_VALUE_BOX_SAFE_FOR_NONE
Definition value.h:165
uintptr_t fr_value_box_safe_for_t
Escaping that's been applied to a value box.
Definition value.h:155
#define vb_strvalue
Definition value.h:252
#define vb_uint32
Definition value.h:265
int nonnull(2, 5))
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:643
#define vb_ifid
Definition value.h:259
#define vb_time_delta
Definition value.h:281
fr_value_box_escape_func_t func
Definition value.h:665
static always_inline int fr_value_box_ethernet_addr(fr_value_box_t *dst, fr_dict_attr_t const *enumv, fr_ethernet_t const *src, bool tainted)
Definition value.h:838
#define vb_float32
Definition value.h:274
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:598
#define fr_value_box_list_foreach(_list_head, _iter)
Definition value.h:217
#define FR_VALUE_BOX_NET_OOM
Special value to indicate fr_value_box_from_network hit an out of memory error.
Definition value.h:1033
#define vb_uint64
Definition value.h:266
static size_t char ** out
Definition value.h:1012
#define FR_VALUE_BOX_SAFE_FOR_ANY
Definition value.h:166