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