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