The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
dict_util.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program 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
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Multi-protocol AVP dictionary API
18 *
19 * @file src/lib/util/dict_util.c
20 *
21 * @copyright 2000,2006 The FreeRADIUS server project
22 * @copyright 2024 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 */
24RCSID("$Id: 76fde9090e1ef2b2a893b84c96f31717822cbbb8 $")
25
26#define _DICT_PRIVATE 1
27
28#include <freeradius-devel/util/atexit.h>
29#include <freeradius-devel/util/conf.h>
30#include <freeradius-devel/util/dict.h>
31#include <freeradius-devel/util/dict_ext_priv.h>
32#include <freeradius-devel/util/dict_fixup_priv.h>
33#include <freeradius-devel/util/hash.h>
34#include <freeradius-devel/util/proto.h>
35#include <freeradius-devel/util/rand.h>
36#include <freeradius-devel/util/syserror.h>
37
38#ifdef HAVE_SYS_STAT_H
39# include <sys/stat.h>
40#endif
41
42fr_dict_gctx_t *dict_gctx = NULL; //!< Top level structure containing global dictionary state.
43
44#define DICT_ATTR_ALLOWED_CHARS \
45 ['-'] = true, ['/'] = true, ['_'] = true, \
46 ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true, \
47 ['5'] = true, ['6'] = true, ['7'] = true, ['8'] = true, ['9'] = true, \
48 ['A'] = true, ['B'] = true, ['C'] = true, ['D'] = true, ['E'] = true, \
49 ['F'] = true, ['G'] = true, ['H'] = true, ['I'] = true, ['J'] = true, \
50 ['K'] = true, ['L'] = true, ['M'] = true, ['N'] = true, ['O'] = true, \
51 ['P'] = true, ['Q'] = true, ['R'] = true, ['S'] = true, ['T'] = true, \
52 ['U'] = true, ['V'] = true, ['W'] = true, ['X'] = true, ['Y'] = true, \
53 ['Z'] = true, \
54 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true, \
55 ['f'] = true, ['g'] = true, ['h'] = true, ['i'] = true, ['j'] = true, \
56 ['k'] = true, ['l'] = true, ['m'] = true, ['n'] = true, ['o'] = true, \
57 ['p'] = true, ['q'] = true, ['r'] = true, ['s'] = true, ['t'] = true, \
58 ['u'] = true, ['v'] = true, ['w'] = true, ['x'] = true, ['y'] = true, \
59 ['z'] = true
60
61/** Characters allowed in a single dictionary attribute name
62 *
63 */
67
68/** Characters allowed in a nested dictionary attribute name
69 *
70 */
73 [ '.' ] = true
74};
75
76/** Characters allowed in enumeration value names
77 *
78 */
80 ['+'] = true, ['-'] = true, ['.'] = true, ['/'] = true, ['_'] = true,
81 ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true,
82 ['5'] = true, ['6'] = true, ['7'] = true, ['8'] = true, ['9'] = true,
83 ['A'] = true, ['B'] = true, ['C'] = true, ['D'] = true, ['E'] = true,
84 ['F'] = true, ['G'] = true, ['H'] = true, ['I'] = true, ['J'] = true,
85 ['K'] = true, ['L'] = true, ['M'] = true, ['N'] = true, ['O'] = true,
86 ['P'] = true, ['Q'] = true, ['R'] = true, ['S'] = true, ['T'] = true,
87 ['U'] = true, ['V'] = true, ['W'] = true, ['X'] = true, ['Y'] = true,
88 ['Z'] = true,
89 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true,
90 ['f'] = true, ['g'] = true, ['h'] = true, ['i'] = true, ['j'] = true,
91 ['k'] = true, ['l'] = true, ['m'] = true, ['n'] = true, ['o'] = true,
92 ['p'] = true, ['q'] = true, ['r'] = true, ['s'] = true, ['t'] = true,
93 ['u'] = true, ['v'] = true, ['w'] = true, ['x'] = true, ['y'] = true,
94 ['z'] = true
95};
96
97/** Default protocol rules set for every dictionary
98 *
99 * This is usually overriden by the public symbol from the protocol library
100 * associated with the dictionary
101 * e.g. libfreeradius-dhcpv6.so -> libfreeradius_dhcpv6_dict_protocol.
102 */
104 .name = "default",
105 .default_type_size = 2,
106 .default_type_length = 2,
107};
108
109/*
110 * Create the hash of the name.
111 *
112 * We copy the hash function here because it's substantially faster.
113 */
114#define FNV_MAGIC_INIT (0x811c9dc5)
115#define FNV_MAGIC_PRIME (0x01000193)
116
117static void hash_pool_free(void *to_free)
118{
119 talloc_free(to_free);
120}
121
122/** Apply a simple (case insensitive) hashing function to the name of an attribute, vendor or protocol
123 *
124 * @param[in] name of the attribute, vendor or protocol.
125 * @param[in] len length of the input string.
126 *
127 * @return the hashed derived from the name.
128 */
129static uint32_t dict_hash_name(char const *name, size_t len)
130{
132
133 char const *p = name, *q = name + len;
134
135 while (p < q) {
136 int c = *(unsigned char const *)p;
137 if (isalpha(c)) c = tolower(c);
138
139 /* coverity[overflow_const] */
141 hash ^= (uint32_t)(c & 0xff);
142 p++;
143 }
144
145 return hash;
146}
147
148/** Wrap name hash function for fr_dict_protocol_t
149 *
150 * @param[in] data fr_dict_attr_t to hash.
151 * @return the hash derived from the name of the attribute.
152 */
154{
155 char const *name;
156
157 name = ((fr_dict_t const *)data)->root->name;
158
159 return dict_hash_name(name, strlen(name));
160}
161
162/** Compare two protocol names
163 *
164 */
165static int8_t dict_protocol_name_cmp(void const *one, void const *two)
166{
167 fr_dict_t const *a = one;
168 fr_dict_t const *b = two;
169 int ret;
170
171 ret = strcasecmp(a->root->name, b->root->name);
172 return CMP(ret, 0);
173}
174
175/** Hash a protocol number
176 *
177 */
179{
180 return fr_hash(&(((fr_dict_t const *)data)->root->attr), sizeof(((fr_dict_t const *)data)->root->attr));
181}
182
183/** Compare two protocol numbers
184 *
185 */
186static int8_t dict_protocol_num_cmp(void const *one, void const *two)
187{
188 fr_dict_t const *a = one;
189 fr_dict_t const *b = two;
190
191 return CMP(a->root->attr, b->root->attr);
192}
193
194/** Wrap name hash function for fr_dict_attr_t
195 *
196 * @param data fr_dict_attr_t to hash.
197 * @return the hash derived from the name of the attribute.
198 */
200{
201 char const *name;
202
203 name = ((fr_dict_attr_t const *)data)->name;
204
205 return dict_hash_name(name, strlen(name));
206}
207
208/** Compare two attribute names
209 *
210 */
211static int8_t dict_attr_name_cmp(void const *one, void const *two)
212{
213 fr_dict_attr_t const *a = one, *b = two;
214 int ret;
215
216 ret = strcasecmp(a->name, b->name);
217 return CMP(ret, 0);
218}
219
220/** Compare two attributes by total order.
221 *
222 * This function is safe / ordered even when the attributes are in
223 * different dictionaries. This allows it to work for local
224 * variables, as those are in a different dictionary from the
225 * protocol ones.
226 *
227 * This function orders parents first, then their children.
228 */
230{
231 int8_t ret;
232
233 /*
234 * Order by parent first. If the parents are different,
235 * we order by parent numbers.
236 *
237 * If the attributes share the same parent at some point,
238 * then the deeper child is sorted later.
239 */
240 if (a->depth < b->depth) {
241 ret = fr_dict_attr_ordered_cmp(a, b->parent);
242 if (ret != 0) return ret;
243
244 return -1; /* order a before b */
245 }
246
247 if (a->depth > b->depth) {
248 ret = fr_dict_attr_ordered_cmp(a->parent, b);
249 if (ret != 0) return ret;
250
251 return +1; /* order b before a */
252 }
253
254 /*
255 * We're at the root (e.g. "RADIUS"). Compare by
256 * protocol number.
257 *
258 * Or, the parents are the same. We can then order by
259 * our (i.e. child) attribute number.
260 */
261 if ((a->depth == 0) || (a->parent == b->parent)) {
262 /*
263 * Order known attributes before unknown / raw ones.
264 */
265 ret = (b->flags.is_unknown | b->flags.is_raw) - (a->flags.is_unknown | a->flags.is_raw);
266 if (ret != 0) return 0;
267
268 return CMP(a->attr, b->attr);
269 }
270
271 /*
272 * The parents are different, we don't need to order by
273 * our attribute number. Instead, we order by the
274 * parent.
275 *
276 * Note that at this point, the call below will never
277 * return 0, because the parents are different.
278 */
279 return fr_dict_attr_ordered_cmp(a->parent, b->parent);
280}
281
282/** Wrap name hash function for fr_dict_vendor_t
283 *
284 * @param data fr_dict_vendor_t to hash.
285 * @return the hash derived from the name of the attribute.
286 */
288{
289 char const *name;
290
291 name = ((fr_dict_vendor_t const *)data)->name;
292
293 return dict_hash_name(name, strlen(name));
294}
295
296/** Compare two attribute names
297 *
298 */
299static int8_t dict_vendor_name_cmp(void const *one, void const *two)
300{
301 fr_dict_vendor_t const *a = one;
302 fr_dict_vendor_t const *b = two;
303 int ret;
304
305 ret = strcasecmp(a->name, b->name);
306 return CMP(ret, 0);
307}
308
309/** Hash a vendor number
310 *
311 */
313{
314 return fr_hash(&(((fr_dict_vendor_t const *)data)->pen),
315 sizeof(((fr_dict_vendor_t const *)data)->pen));
316}
317
318/** Compare two vendor numbers
319 *
320 */
321static int8_t dict_vendor_pen_cmp(void const *one, void const *two)
322{
323 fr_dict_vendor_t const *a = one;
324 fr_dict_vendor_t const *b = two;
325
326 return CMP(a->pen, b->pen);
327}
328
329/** Hash a enumeration name
330 *
331 */
333{
334 fr_dict_enum_value_t const *enumv = data;
335
336 return dict_hash_name((void const *)enumv->name, enumv->name_len);
337}
338
339/** Compare two dictionary attribute enum values
340 *
341 */
342static int8_t dict_enum_name_cmp(void const *one, void const *two)
343{
344 fr_dict_enum_value_t const *a = one;
345 fr_dict_enum_value_t const *b = two;
346 size_t len;
347 int ret;
348
349 if (a->name_len >= b->name_len) {
350 len = a->name_len;
351 } else {
352 len = b->name_len;
353 }
354
355 ret = strncasecmp(a->name, b->name, len);
356 return CMP(ret, 0);
357}
358
359/** Hash a dictionary enum value
360 *
361 */
363{
364 fr_dict_enum_value_t const *enumv = data;
365
366 return fr_value_box_hash(enumv->value);
367}
368
369/** Compare two dictionary enum values
370 *
371 */
372static int8_t dict_enum_value_cmp(void const *one, void const *two)
373{
374 fr_dict_enum_value_t const *a = one;
375 fr_dict_enum_value_t const *b = two;
376 int ret;
377
378 ret = fr_value_box_cmp(a->value, b->value); /* not yet int8_t! */
379 return CMP(ret, 0);
380}
381
382/** Resolve an alias attribute to the concrete attribute it points to
383 *
384 * @param[out] err where to write the error (if any).
385 * @param[in] da to resolve.
386 * @return
387 * - NULL on error.
388 * - The concrete attribute on success.
389 */
391{
392 fr_dict_attr_t const *ref;
393
394 if (!da->flags.is_alias) return da;
395
396 ref = fr_dict_attr_ref(da);
397 if (unlikely(!ref)) {
398 fr_strerror_printf("ALIAS attribute '%s' missing reference", da->name);
400 return NULL;
401 } else {
402 if (err) *err = FR_DICT_ATTR_OK;
403 }
404
405 return ref;
406}
407
408/** Set a dictionary attribute's name
409 *
410 * @note This function can only be used _before_ the attribute is inserted into the dictionary.
411 *
412 * @param[in] da_p to set name for.
413 * @param[in] name to set. If NULL a name will be automatically generated.
414 */
415static inline CC_HINT(always_inline) int dict_attr_name_set(fr_dict_attr_t **da_p, char const *name)
416{
418 size_t name_len;
419 char *name_start, *name_end;
420 fr_dict_attr_t *da = *da_p;
421
422 /*
423 * Generate a name if none is specified
424 */
425 if (!name) {
426 fr_sbuff_t unknown_name = FR_SBUFF_OUT(buffer, sizeof(buffer));
427
428
429 (void) fr_sbuff_in_sprintf(&unknown_name, "%u", da->attr);
430
431 name = fr_sbuff_buff(&unknown_name);
432 name_len = fr_sbuff_used(&unknown_name);
433 } else {
434 name_len = strlen(name);
435 }
436
437 /*
438 * Grow the structure to hold the name
439 *
440 * We add the name as an extension because it makes
441 * the code less complex, and means the name value
442 * is copied automatically when if the fr_dict_attr_t
443 * is copied.
444 *
445 * We do still need to fixup the da->name pointer
446 * though.
447 */
448 name_start = dict_attr_ext_alloc_size(da_p, FR_DICT_ATTR_EXT_NAME, name_len + 1);
449 if (!name_start) return -1;
450
451 name_end = name_start + name_len;
452
453 memcpy(name_start, name, name_len);
454 *name_end = '\0';
455
456 (*da_p)->name = name_start;
457 (*da_p)->name_len = name_len;
458
459 return 0;
460}
461
462/** Add a child/nesting extension to an attribute
463 *
464 * @note This function can only be used _before_ the attribute is inserted into the dictionary.
465 *
466 * @param[in] da_p to set a group reference for.
467 */
468static inline CC_HINT(always_inline) int dict_attr_children_init(fr_dict_attr_t **da_p)
469{
471
473 if (unlikely(!ext)) return -1;
474
475 return 0;
476}
477
478/** Cache the vendor pointer for an attribute
479 *
480 * @note This function can only be used _before_ the attribute is inserted into the dictionary.
481 *
482 * @param[in] da_p to set a group reference for.
483 * @param[in] vendor to set.
484 */
485static inline CC_HINT(always_inline) int dict_attr_vendor_set(fr_dict_attr_t **da_p, fr_dict_attr_t const *vendor)
486{
488
490 if (unlikely(!ext)) return -1;
491
492 ext->vendor = vendor;
493
494 return 0;
495}
496
497/** Initialise a per-attribute enumeration table
498 *
499 * @note This function can only be used _before_ the attribute is inserted into the dictionary.
500 *
501 * @param[in] da_p to set a group reference for.
502 */
503static inline CC_HINT(always_inline) int dict_attr_enumv_init(fr_dict_attr_t **da_p)
504{
506
508 if (unlikely(!ext)) return -1;
509
510 return 0;
511}
512
513/** Initialise a per-attribute namespace
514 *
515 * @note This function can only be used _before_ the attribute is inserted into the dictionary.
516 *
517 * @param[in] da_p to set a group reference for.
518 */
519static inline CC_HINT(always_inline) int dict_attr_namespace_init(fr_dict_attr_t **da_p)
520{
522
524 if (unlikely(!ext)) return -1;
525
526 /*
527 * Create the table of attributes by name.
528 * There MAY NOT be multiple attributes of the same name.
529 *
530 * If the attribute already has extensions
531 * then we don't want to leak the old
532 * namespace hash table.
533 */
534 if (!ext->namespace) {
535 ext->namespace = fr_hash_table_talloc_alloc(*da_p, fr_dict_attr_t,
537 if (!ext->namespace) {
538 fr_strerror_printf("Failed allocating \"namespace\" table");
539 return -1;
540 }
541 }
542
543 return 0;
544}
545
546/** Initialise type specific fields within the dictionary attribute
547 *
548 * Call when the type of the attribute is known.
549 *
550 * @param[in,out] da_p to set the type for.
551 * @param[in] type to set.
552 * @return
553 * - 0 on success.
554 * - < 0 on error.
555 */
557{
558 if (unlikely(((*da_p)->type != FR_TYPE_NULL) &&
559 ((*da_p)->type != type))) {
560 fr_strerror_printf("Cannot set data type to '%s' - it is already set to '%s'",
561 fr_type_to_str(type), fr_type_to_str((*da_p)->type));
562 return -1;
563 }
564
565 if (unlikely((*da_p)->state.finalised == true)) {
566 fr_strerror_const("Can't perform type initialisation on finalised attribute");
567 return -1;
568 }
569
570 /*
571 * Structural types can have children
572 * so add the extension for them.
573 */
574 switch (type) {
576 /*
577 * Groups don't have children or namespaces. But
578 * they always have refs. Either to the root of
579 * the current dictionary, or to another dictionary,
580 * via its top-level TLV.
581 *
582 * Note that when multiple TLVs have the same
583 * children, the dictionary has to use "clone="
584 * instead of "ref=". That's because the
585 * children of the TLVs all require the correct
586 * parentage. Perhaps that can be changed when
587 * the encoders / decoders are updated. It would be good to just reference the DAs instead of cloning an entire subtree.
588 */
590 if (dict_attr_ext_alloc(da_p, FR_DICT_ATTR_EXT_REF) == NULL) return -1;
591 break;
592 }
593
594 if (dict_attr_children_init(da_p) < 0) return -1;
595 if (dict_attr_namespace_init(da_p) < 0) return -1; /* Needed for all TLV style attributes */
596
597 (*da_p)->last_child_attr = (1 << 24); /* High enough not to conflict with protocol numbers */
598 break;
599
600 /*
601 * Leaf types
602 */
603 default:
604 if (dict_attr_enumv_init(da_p) < 0) return -1;
605 break;
606 }
607
608 (*da_p)->flags.is_known_width |= fr_type_fixed_size[type];
609
610 /*
611 * Set default type-based flags
612 */
613 switch (type) {
614 case FR_TYPE_DATE:
616 (*da_p)->flags.length = 4;
617 (*da_p)->flags.flag_time_res = FR_TIME_RES_SEC;
618 break;
619
620
621 case FR_TYPE_OCTETS:
622 case FR_TYPE_STRING:
623 (*da_p)->flags.is_known_width = ((*da_p)->flags.length != 0);
624 break;
625
626 default:
627 break;
628 }
629
630 (*da_p)->type = type;
631
632 return 0;
633}
634
635/** Initialise fields which depend on a parent attribute
636 *
637 * @param[in,out] da_p to initialise.
638 * @param[in] parent of the attribute.
639 * @return
640 * - 0 on success.
641 * - < 0 on error.
642 */
644{
645 fr_dict_attr_t *da = *da_p;
646 fr_dict_t const *dict = parent->dict;
648
649 if (unlikely((*da_p)->type == FR_TYPE_NULL)) {
650 fr_strerror_const("Attribute type must be set before initialising parent. Use dict_attr_type_init() first");
651 return -1;
652 }
653
654 if (unlikely(da->parent != NULL)) {
655 fr_strerror_printf("Attempting to set parent for '%s' to '%s', but parent already set to '%s'",
656 da->name, parent->name, da->parent->name);
657 return -1;
658 }
659
660 if (unlikely((*da_p)->state.finalised == true)) {
661 fr_strerror_printf("Attempting to set parent for '%s' to '%s', but attribute already finalised",
662 da->name, parent->name);
663 return -1;
664 }
665
666 da->parent = parent;
667 da->dict = parent->dict;
668 da->depth = parent->depth + 1;
669 da->flags.internal |= parent->flags.internal;
670
671 /*
672 * Point to the vendor definition. Since ~90% of
673 * attributes are VSAs, caching this pointer will help.
674 */
675 if (da->type == FR_TYPE_VENDOR) {
676 da->flags.type_size = dict->root->flags.type_size;
677 da->flags.length = dict->root->flags.type_size;
678
679 if ((dict->root->attr == FR_DICT_PROTO_RADIUS) && (da->depth == 2)) {
680 fr_dict_vendor_t const *dv;
681
682 dv = fr_dict_vendor_by_num(dict, da->attr);
683 if (dv) {
684 da->flags.type_size = dv->type;
685 da->flags.length = dv->length;
686 }
687 }
688
689 } else if (da->type == FR_TYPE_TLV) {
690 da->flags.type_size = dict->root->flags.type_size;
691 da->flags.length = dict->root->flags.type_size;
692 }
693
694 if (parent->type == FR_TYPE_VENDOR) {
696 if (unlikely(!ext)) return -1;
697
698 ext->vendor = parent;
699
700 } else {
701 ext = dict_attr_ext_copy(da_p, parent, FR_DICT_ATTR_EXT_VENDOR); /* Noop if no vendor extension */
702 }
703
704 da = *da_p;
705
706 if (!ext || ((da->type != FR_TYPE_TLV) && (da->type != FR_TYPE_VENDOR))) return 0;
707
708 da->flags.type_size = ext->vendor->flags.type_size;
709 da->flags.length = ext->vendor->flags.type_size;
710
711 return 0;
712}
713
714/** Set the attribute number (if any)
715 *
716 * @param[in] da to set the attribute number for.
717 * @param[in] num to set.
718 */
719int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num)
720{
721 if (da->state.attr_set) {
722 fr_strerror_const("Attribute number already set");
723 return -1;
724 }
725 da->attr = num;
726 da->state.attr_set = true;
727
728 return 0;
729}
730
731/** Set the attribute number (if any)
732 *
733 * @note Must have a parent set.
734 *
735 * @param[in] da to set the attribute number for.
736 */
738{
739 if (!da->parent) {
740 fr_strerror_const("Attribute must have parent set before automatically setting attribute number");
741 return -1;
742 }
743 return dict_attr_num_init(da, ++fr_dict_attr_unconst(da->parent)->last_child_attr);
744}
745
746/** Set where the dictionary attribute was defined
747 *
748 */
749void dict_attr_location_init(fr_dict_attr_t *da, char const *filename, int line)
750{
751 da->filename = filename;
752 da->line = line;
753}
754
755/** Set remaining fields in a dictionary attribute before insertion
756 *
757 * @param[in] da_p to finalise.
758 * @param[in] name of the attribute.
759 * @return
760 * - 0 on success.
761 * - < 0 on error.
762 */
763int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name)
764{
765 fr_dict_attr_t *da;
766
767 /*
768 * Finalising the attribute allocates its
769 * automatic number if its a name only attribute.
770 */
771 da = *da_p;
772
773 /*
774 * Initialize the length field automatically if it's not been set already
775 */
776 if (!da->flags.length && fr_type_is_leaf(da->type) && !fr_type_is_variable_size(da->type)) {
777 fr_value_box_t box;
778
779 fr_value_box_init(&box, da->type, NULL, false);
780 da->flags.length = fr_value_box_network_length(&box);
781 }
782
783 switch(da->type) {
784 case FR_TYPE_STRUCT:
785 da->flags.is_known_width |= da->flags.array;
786 break;
787
788 case FR_TYPE_GROUP:
789 {
791 /*
792 * If it's a group attribute, the default
793 * reference goes to the root of the
794 * dictionary as that's where the default
795 * name/numberspace is.
796 *
797 * This may be updated by the caller.
798 */
800 if (unlikely(ext == NULL)) {
801 fr_strerror_const("Missing ref extension");
802 return -1;
803 }
804
805 /*
806 * For groups, if a ref wasn't provided then
807 * set it to the dictionary root.
808 */
809 if ((ext->type == FR_DICT_ATTR_REF_NONE) &&
811 return -1;
812 }
813 }
814 break;
815
816 default:
817 break;
818 }
819
820 /*
821 * Name is a separate talloc chunk. We allocate
822 * it last because we cache the pointer value.
823 */
824 if (dict_attr_name_set(da_p, name) < 0) return -1;
825
826 DA_VERIFY(*da_p);
827
828 (*da_p)->state.finalised = true;
829
830 return 0;
831}
832
833static inline CC_HINT(always_inline)
834int dict_attr_init_common(char const *filename, int line,
835 fr_dict_attr_t **da_p,
836 fr_dict_attr_t const *parent,
838{
839 dict_attr_location_init((*da_p), filename, line);
840
841 if (unlikely(dict_attr_type_init(da_p, type) < 0)) return -1;
842
843 if (args->flags) (*da_p)->flags = *args->flags;
844
845 if (parent && (dict_attr_parent_init(da_p, parent) < 0)) return -1;
846
847 if (args->ref && (dict_attr_ref_aset(da_p, args->ref, FR_DICT_ATTR_REF_ALIAS) < 0)) return -1;
848
849 /*
850 * Everything should be created correctly.
851 */
852 if (!(*da_p)->flags.internal && !(*da_p)->flags.is_alias &&
853 parent && ((parent->type == FR_TYPE_TLV) || (parent->type ==FR_TYPE_VENDOR))) {
854 if (!parent->flags.type_size) {
855 fr_strerror_printf("Parent %s has zero type_size", parent->name);
856 return -1;
857 }
858
859 if ((uint64_t) (*da_p)->attr >= ((uint64_t) 1 << (8 * parent->flags.type_size))) {
860 fr_strerror_printf("Child of parent %s has invalid attribute number %u for type_size %u",
861 parent->name, (*da_p)->attr, parent->flags.type_size);
862 return -1;
863 }
864 }
865
866 return 0;
867}
868
869/** Initialise fields in a dictionary attribute structure
870 *
871 * This function is a wrapper around the other initialisation functions.
872 *
873 * The reason for the separation, is that sometimes we're initialising a dictionary attribute
874 * by parsing an actual dictionary file, and other times we're copying attribute, or initialising
875 * them programatically.
876 *
877 * This function should only be used for the second case, where we have a complet attribute
878 * definition already.
879 *
880 * @note This function can only be used _before_ the attribute is inserted into the dictionary.
881 *
882 * @param[in] filename file.
883 * @param[in] line number.
884 * @param[in] da_p to initialise.
885 * @param[in] parent of the attribute, if none, this attribute will
886 * be initialised as a dictionary root.
887 * @param[in] name of attribute. Pass NULL for auto-generated name.
888 * @param[in] attr number.
889 * @param[in] type of the attribute.
890 * @param[in] args optional initialisation arguments.
891 * @return
892 * - 0 on success.
893 * - <0 on error.
894 */
895int _dict_attr_init(char const *filename, int line,
896 fr_dict_attr_t **da_p,
897 fr_dict_attr_t const *parent,
898 char const *name, unsigned int attr,
900{
901 /*
902 * We initialize the number first, as doing that doesn't have any other side effects.
903 */
904 if (unlikely(dict_attr_num_init(*da_p, attr) < 0)) return -1;
905
906 /*
907 * This function then checks the number, for things like VSAs.
908 */
909 if (unlikely(dict_attr_init_common(filename, line, da_p, parent, type, args) < 0)) return -1;
910
911 if (unlikely(dict_attr_finalise(da_p, name) < 0)) return -1;
912
913 return 0;
914}
915
916/** Initialise fields in a dictionary attribute structure
917 *
918 * This function is a wrapper around the other initialisation functions.
919 *
920 * The reason for the separation, is that sometimes we're initialising a dictionary attribute
921 * by parsing an actual dictionary file, and other times we're copying attribute, or initialising
922 * them programatically.
923 *
924 * This function should only be used for the second case, where we have a complet attribute
925 * definition already.
926 *
927 * @note This function can only be used _before_ the attribute is inserted into the dictionary.
928 *
929 * @param[in] filename file.
930 * @param[in] line number.
931 * @param[in] da_p to initialise.
932 * @param[in] parent of the attribute, if none, this attribute will
933 * be initialised as a dictionary root.
934 * @param[in] name of attribute. Pass NULL for auto-generated name.
935 * automatically generated.
936 * @param[in] type of the attribute.
937 * @param[in] args optional initialisation arguments.
938 * @return
939 * - 0 on success.
940 * - <0 on error.
941 */
942int _dict_attr_init_name_only(char const *filename, int line,
943 fr_dict_attr_t **da_p,
944 fr_dict_attr_t const *parent,
945 char const *name,
947{
948 if (unlikely(dict_attr_init_common(filename, line, da_p, parent, type, args) < 0)) return -1;
949
950 /*
951 * Automatically generate the attribute number when the attribut is added.
952 */
953 (*da_p)->flags.name_only = true;
954
955 if (unlikely(dict_attr_finalise(da_p, name) < 0)) return -1;
956
957 return 0;
958}
959
961{
963
964#if 0
965#ifdef WITH_VERIFY_PTR
966 /*
967 * Check that any attribute we reference is still valid
968 * when we're being freed.
969 */
970 fr_dict_attr_t const *ref = fr_dict_attr_ref(da);
971
972 if (ref) (void)talloc_get_type_abort_const(ref, fr_dict_attr_t);
973#endif
974#endif
975
977 if (ext) talloc_free(ext->value_by_name); /* Ensure this is freed before the enumvs */
978
979 return 0;
980}
981
982/** Allocate a partially completed attribute
983 *
984 * This is useful in some instances where we need to pre-allocate the attribute
985 * for talloc hierarchy reasons, but want to finish initialising it
986 * with #dict_attr_init later.
987 *
988 * @param[in] ctx to allocate attribute in.
989 * @param[in] proto protocol specific extensions.
990 * @return
991 * - A new, partially completed, fr_dict_attr_t on success.
992 * - NULL on failure (memory allocation error).
993 */
995{
996 fr_dict_attr_t *da;
997
998 /*
999 * Do not use talloc zero, the caller
1000 * always initialises memory allocated
1001 * here.
1002 */
1003 da = talloc_zero(ctx, fr_dict_attr_t);
1004 if (unlikely(!da)) return NULL;
1005
1006 /*
1007 * Allocate room for the protocol specific flags
1008 */
1009 if (proto->attr.flags.len > 0) {
1011 proto->attr.flags.len) == NULL)) {
1012 talloc_free(da);
1013 return NULL;
1014 }
1015 }
1016 talloc_set_destructor(da, _dict_attr_free);
1017
1018 return da;
1019}
1020
1021/** Allocate a dictionary root attribute on the heap
1022 *
1023 * @param[in] filename file.
1024 * @param[in] line number.
1025 * @param[in] ctx to allocate the attribute in.
1026 * @param[in] dict the attribute will be used in.
1027 * @param[in] name of the attribute. If NULL an OID string
1028 * will be created and set as the name.
1029 * @param[in] proto_number number. This should be
1030 * @param[in] args optional initialisation arguments.
1031 * @return
1032 * - A new fr_dict_attr_t on success.
1033 * - NULL on failure.
1034 */
1035fr_dict_attr_t *_dict_attr_alloc_root(char const *filename, int line,
1036 TALLOC_CTX *ctx,
1037 fr_dict_t const *dict,
1038 char const *name, int proto_number,
1039 dict_attr_args_t const *args)
1040{
1042
1043 n = dict_attr_alloc_null(ctx, dict->proto);
1044 if (unlikely(!n)) return NULL;
1045
1046 if (_dict_attr_init(filename, line, &n, NULL, name, proto_number, FR_TYPE_TLV, args) < 0) {
1047 talloc_free(n);
1048 return NULL;
1049 }
1050
1051 return n;
1052}
1053
1054/** Allocate a dictionary attribute on the heap
1055 *
1056 * @param[in] filename file.
1057 * @param[in] line number.
1058 * @param[in] ctx to allocate the attribute in.
1059 * @param[in] parent of the attribute.
1060 * @param[in] name of the attribute. If NULL an OID string
1061 * will be created and set as the name.
1062 * @param[in] attr number.
1063 * @param[in] type of the attribute.
1064 * @param[in] args optional initialisation arguments.
1065 * @return
1066 * - A new fr_dict_attr_t on success.
1067 * - NULL on failure.
1068 */
1069fr_dict_attr_t *_dict_attr_alloc(char const *filename, int line,
1070 TALLOC_CTX *ctx,
1071 fr_dict_attr_t const *parent,
1072 char const *name, int attr,
1074{
1076
1077 n = dict_attr_alloc_null(ctx, parent->dict->proto);
1078 if (unlikely(!n)) return NULL;
1079
1080 if (_dict_attr_init(filename, line, &n, parent, name, attr, type, args) < 0) {
1081 talloc_free(n);
1082 return NULL;
1083 }
1084
1085 return n;
1086}
1087
1088/** Copy a an existing attribute, possibly to a new location
1089 *
1090 * @param[in] ctx to allocate new attribute in.
1091 * @param[in] parent where to parent the copy from. If NULL, in->parent is used.
1092 * @param[in] in attribute to copy.
1093 * @param[in] name to assign to the attribute. If NULL, in->name is used.
1094 * @return
1095 * - A copy of the input fr_dict_attr_t on success.
1096 * - NULL on failure.
1097 */
1099 char const *name)
1100{
1102
1103 if (in->flags.has_fixup) {
1104 fr_strerror_printf("Cannot copy from %s - source attribute is waiting for additional definitions",
1105 in->name);
1106 return NULL;
1107 }
1108
1109 fr_assert(parent || name);
1110
1111 n = dict_attr_alloc(ctx, parent ? parent : in->parent, name ? name : in->name,
1112 in->attr, in->type, &(dict_attr_args_t){ .flags = &in->flags });
1113 if (unlikely(!n)) return NULL;
1114
1115 /*
1116 * This newly allocated attribute is not the target of a ref.
1117 */
1118 n->flags.is_ref_target = false;
1119
1120 if (dict_attr_ext_copy_all(&n, in) < 0) {
1121 error:
1122 talloc_free(n);
1123 return NULL;
1124 }
1125 DA_VERIFY(n);
1126
1127 if (fr_type_is_structural(in->type) && in->flags.has_alias) {
1128 if (dict_attr_acopy_aliases(n, in) < 0) goto error;
1129 }
1130
1131 return n;
1132}
1133
1135{
1136 if (!dst->flags.local) {
1137 fr_strerror_const("Cannot copy attributes to a non-local dictionary");
1138 return -1;
1139 }
1140
1141 if (src->flags.has_fixup) {
1142 fr_strerror_printf("Cannot copy from %s to %s - source attribute is waiting for additional definitions",
1143 src->name, dst->name);
1144 return -1;
1145 }
1146
1147 /*
1148 * Why not? @todo - check and fix
1149 */
1150 if (src->flags.local) {
1151 fr_strerror_const("Cannot copy a local attribute");
1152 return -1;
1153 }
1154
1155 return dict_attr_acopy_children(dst->dict, UNCONST(fr_dict_attr_t *, dst), src);
1156}
1157
1159 fr_dict_attr_t const *child)
1160{
1161 fr_dict_attr_t *copy;
1162
1163 copy = dict_attr_acopy(dict->pool, dst, child, child->name);
1164 if (!copy) return -1;
1165
1166 fr_assert(copy->parent == dst);
1167 copy->depth = copy->parent->depth + 1;
1168
1169 if (dict_attr_child_add(dst, copy) < 0) return -1;
1170
1171 if (dict_attr_add_to_namespace(dst, copy) < 0) return -1;
1172
1173 if (!dict_attr_children(child)) return 0;
1174
1175 if (dict_attr_acopy_children(dict, copy, child) < 0) return -1;
1176
1177 /*
1178 * Children of a UNION get an ALIAS added to the parent of the UNION. This allows the UNION
1179 * attribute to be omitted from parsing and printing.
1180 */
1181 if (src->type != FR_TYPE_UNION) return 0;
1182
1183 return dict_attr_alias_add(dst->parent, copy->name, copy);
1184}
1185
1186
1187/** Copy the children of an existing attribute
1188 *
1189 * @param[in] dict to allocate the children in
1190 * @param[in] dst where to copy the children to
1191 * @param[in] src where to copy the children from
1192 * @return
1193 * - 0 on success
1194 * - <0 on error
1195 */
1197{
1198 uint child_num;
1199 fr_dict_attr_t const *child = NULL, *src_key = NULL;
1200 fr_dict_attr_t *dst_key;
1201
1203 fr_assert(dst->type == src->type);
1205
1206 /*
1207 * For non-struct parents, we can copy their children in any order.
1208 */
1209 if (likely(src->type != FR_TYPE_STRUCT)) {
1210 for (child = fr_dict_attr_iterate_children(src, &child);
1211 child != NULL;
1212 child = fr_dict_attr_iterate_children(src, &child)) {
1213 if (dict_attr_acopy_child(dict, dst, src, child) < 0) return -1;
1214 }
1215
1216 return 0;
1217 }
1218
1219 /*
1220 * For structs, we copy the children in order. This allows "key" fields to be copied before
1221 * fields which depend on them.
1222 *
1223 * Note that due to the checks in the DEFINE and ATTRIBUTE parsers (but not the validate
1224 * routines), STRUCTs can only have children which are MEMBERs. And MEMBERs are allocated in
1225 * order.
1226 */
1227 for (child_num = 1, child = fr_dict_attr_child_by_num(src, child_num);
1228 child != NULL;
1229 child_num++, child = fr_dict_attr_child_by_num(src, child_num)) {
1230 /*
1231 * If the key field has enums, then delay copying the enums until after we've copied all
1232 * of the other children.
1233 *
1234 * For a UNION which is inside of a STRUCT, the UNION has a reference to the key field.
1235 * So the key field needs to be defined before we create the UNION.
1236 *
1237 * But the key field also has a set of ENUMs, each of which has a key ref to the UNION
1238 * member which is associated with that key value. This means that we have circular
1239 * dependencies.
1240 *
1241 * The loop is resolved by creating the key first, and allocating room for an ENUM
1242 * extension. This allows the UNION to reference the key. Once the UNION is created, we
1243 * go back and copy all of the ENUMs over. The ENUM copy routine will take care of
1244 * fixing up the refs.
1245 */
1246 if (unlikely(fr_dict_attr_is_key_field(child) && child->flags.has_value)) {
1247 src_key = child;
1248
1249 if (src_key->flags.has_fixup) {
1250 fr_strerror_printf("Cannot copy from %s - source attribute is waiting for additional definitions",
1251 src_key->name);
1252 return -1;
1253 }
1254
1255 dst_key = dict_attr_alloc(dict, dst, src_key->name,
1256 src_key->attr, src_key->type, &(dict_attr_args_t){ .flags = &src_key->flags });
1257 if (unlikely(!dst_key)) return -1;
1258
1259 if (!dict_attr_ext_alloc(&dst_key, FR_DICT_ATTR_EXT_ENUMV)) return -1;
1260
1261 fr_assert(dst_key->parent == dst);
1262 dst_key->depth = dst->depth + 1;
1263
1264 if (dict_attr_child_add(dst, dst_key) < 0) return -1;
1265
1266 if (dict_attr_add_to_namespace(dst, dst_key) < 0) return -1;
1267
1268 continue;
1269 }
1270
1271 if (dict_attr_acopy_child(dict, dst, src, child) < 0) return -1;
1272
1273 DA_VERIFY(child);
1274 }
1275
1276 DA_VERIFY(dst);
1277
1278 if (!src_key) return 0;
1279
1280 if (!dict_attr_ext_copy(&dst_key, src_key, FR_DICT_ATTR_EXT_ENUMV)) return -1;
1281
1282 return 0;
1283}
1284
1285/** Copy the VALUEs of an existing attribute, by casting them
1286 *
1287 * @param[in] dst where to cast the VALUEs to
1288 * @param[in] src where to cast the VALUEs from
1289 * @return
1290 * - 0 on success
1291 * - <0 on error
1292 */
1294{
1296
1297 fr_assert(!fr_type_is_non_leaf(dst->type));
1298 fr_assert(!fr_type_is_non_leaf(src->type));
1299
1302
1304 if (!ext) {
1305 fr_assert(0);
1306 return -1;
1307 }
1308
1309 if (!ext->name_by_value) {
1310 fr_strerror_printf("Reference enum %s does not have any VALUEs to copy", src->name);
1311 return -1;
1312 }
1313
1315
1316 return -1;
1317}
1318
1319
1320/** Copy aliases of an existing attribute to a new one.
1321 *
1322 * @param[in] dst where to copy the children to
1323 * @param[in] src where to copy the children from
1324 * @return
1325 * - 0 on success
1326 * - <0 on error
1327 */
1329{
1330 fr_hash_table_t *namespace;
1331 fr_hash_iter_t iter;
1332 fr_dict_attr_t const *da;
1333
1334 if (!src->flags.has_alias) return 0;
1335
1336 switch (src->type) {
1337 case FR_TYPE_TLV:
1338 case FR_TYPE_VENDOR:
1339 case FR_TYPE_VSA:
1340 break;
1341
1342 /*
1343 * Automatically added aliases are copied in dict_attr_acopy_child().
1344 */
1345 case FR_TYPE_STRUCT:
1346 return 0;
1347
1348 default:
1349 fr_strerror_printf("Cannot add ALIAS to parent attribute %s of data type '%s'", src->name, fr_type_to_str(src->type));
1350 return -1;
1351
1352 }
1353
1354 namespace = dict_attr_namespace(src);
1355 fr_assert(namespace != NULL);
1356
1357 for (da = fr_hash_table_iter_init(namespace, &iter);
1358 da != NULL;
1359 da = fr_hash_table_iter_next(namespace, &iter)) {
1360 if (!da->flags.is_alias) continue;
1361
1362#if 1
1363 fr_strerror_printf("Cannot clone ALIAS %s.%s to %s.%s", src->name, da->name, dst->name, da->name);
1364 return -1;
1365
1366#else
1367 fr_dict_attr_t const *parent, *ref;
1368 fr_dict_attr_t const *new_ref;
1369
1370 ref = fr_dict_attr_ref(da);
1371 fr_assert(ref != NULL);
1372
1373 /*
1374 * ALIASes are normally down the tree, to shorten sibling relationships.
1375 * e.g. Cisco-AVPAir -> Vendor-Specific.Cisco.AV-Pair.
1376 *
1377 * The question is to we want to allow aliases to create cross-tree links? I suspect
1378 * not.
1379 */
1380 parent = fr_dict_attr_common_parent(src, ref, true);
1381 if (!parent) {
1382 fr_strerror_printf("Cannot clone ALIAS %s.%s to %s.%s, the alias reference %s is outside of the shared tree",
1383 src->name, da->name, dst->name, da->name, ref->name);
1384 return -1;
1385 }
1386
1387 fr_assert(parent == src);
1388
1389 new_ref = fr_dict_attr_by_name(NULL, dst, da->name);
1390 fr_assert(new_ref == NULL);
1391
1392 /*
1393 * This function needs to walk back up from "ref" to "src", finding the intermediate DAs.
1394 * Once that's done, it needs to walk down from "dst" to create a new "ref".
1395 */
1396 new_ref = dict_alias_reref(dst, src, ref);
1397 fr_assert(new_ref != NULL);
1398
1399 if (dict_attr_alias_add(dst, da->name, new_ref) < 0) return -1;
1400#endif
1401 }
1402
1403 return 0;
1404}
1405
1406/** Add an alias to an existing attribute
1407 *
1408 */
1409int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref)
1410{
1411 fr_dict_attr_t const *da, *common;
1412 fr_dict_attr_t *self;
1413 fr_hash_table_t *namespace;
1414
1415 switch (parent->type) {
1416 case FR_TYPE_STRUCT:
1417 /*
1418 * If we are a STRUCT, the reference an only be to children of a UNION.
1419 */
1420 fr_assert(ref->parent->type == FR_TYPE_UNION);
1421
1422 /*
1423 * And the UNION must be a MEMBER of the STRUCT.
1424 */
1425 fr_assert(ref->parent->parent == parent);
1426 break;
1427
1428 case FR_TYPE_TLV:
1429 case FR_TYPE_VENDOR:
1430 case FR_TYPE_VSA:
1431 case FR_TYPE_GROUP:
1432 break;
1433
1434 default:
1435 fr_strerror_printf("Cannot add ALIAS to parent attribute %s of data type '%s'",
1436 parent->name, fr_type_to_str(parent->type));
1437 return -1;
1438 }
1439
1440 if ((ref->type == FR_TYPE_UNION) || fr_dict_attr_is_key_field(ref)) {
1441 fr_strerror_printf("Cannot add ALIAS to target attribute %s of data type '%s'",
1442 ref->name, fr_type_to_str(ref->type));
1443 return -1;
1444 }
1445
1446 da = dict_attr_by_name(NULL, parent, alias);
1447 if (da) {
1448 fr_strerror_printf("ALIAS '%s' conflicts with another attribute in namespace %s",
1449 alias, parent->name);
1450 return -1;
1451 }
1452
1453 /*
1454 * ALIASes can point across the tree and down, for the same parent. ALIASes cannot go back up
1455 * the tree.
1456 */
1457 common = fr_dict_attr_common_parent(parent, ref, true);
1458 if (!common) {
1459 fr_strerror_printf("Invalid ALIAS to target attribute %s of data type '%s' - the attributes do not share a parent",
1460 ref->name, fr_type_to_str(ref->type));
1461 return -1;
1462 }
1463
1464 /*
1465 * Note that we do NOT call fr_dict_attr_add() here.
1466 *
1467 * When that function adds two equivalent attributes, the
1468 * second one is prioritized for printing. For ALIASes,
1469 * we want the pre-existing one to be prioritized.
1470 *
1471 * i.e. you can lookup the ALIAS by "name", but you
1472 * actually get returned "ref".
1473 */
1474 {
1475 fr_dict_attr_flags_t flags = ref->flags;
1476
1477 flags.is_alias = 1; /* These get followed automatically by public functions */
1478
1479 self = dict_attr_alloc(parent->dict->pool, parent, alias, ref->attr, FR_TYPE_VOID, (&(dict_attr_args_t){ .flags = &flags, .ref = ref }));
1480 if (unlikely(!self)) return -1;
1481 }
1482
1483 self->dict = parent->dict;
1484 UNCONST(fr_dict_attr_t *, parent)->flags.has_alias = true;
1485
1486 fr_assert(fr_dict_attr_ref(self) == ref);
1487
1488 namespace = dict_attr_namespace(parent);
1489 if (!namespace) {
1490 fr_strerror_printf("Attribute '%s' does not contain a namespace", parent->name);
1491 error:
1492 talloc_free(self);
1493 return -1;
1494 }
1495
1496 if (!fr_hash_table_insert(namespace, self)) {
1497 fr_strerror_const("Internal error storing attribute");
1498 goto error;
1499 }
1500
1501 return 0;
1502}
1503
1504/** Add a protocol to the global protocol table
1505 *
1506 * Inserts a protocol into the global protocol table. Uses the root attributes
1507 * of the dictionary for comparisons.
1508 *
1509 * @param[in] dict of protocol we're inserting.
1510 * @return
1511 * - 0 on success.
1512 * - -1 on failure.
1513 */
1515{
1516 if (!dict->root) return -1; /* Should always have root */
1517
1519 fr_dict_t *old_proto;
1520
1521 old_proto = fr_hash_table_find(dict_gctx->protocol_by_name, dict);
1522 if (!old_proto) {
1523 fr_strerror_printf("%s: Failed inserting protocol name %s", __FUNCTION__, dict->root->name);
1524 return -1;
1525 }
1526
1527 if ((strcmp(old_proto->root->name, dict->root->name) == 0) &&
1528 (old_proto->root->name == dict->root->name)) {
1529 fr_strerror_printf("%s: Duplicate protocol name %s", __FUNCTION__, dict->root->name);
1530 return -1;
1531 }
1532
1533 return 0;
1534 }
1535 dict->in_protocol_by_name = true;
1536
1538 fr_strerror_printf("%s: Duplicate protocol number %u", __FUNCTION__, dict->root->attr);
1539 return -1;
1540 }
1541 dict->in_protocol_by_num = true;
1542
1543 dict_dependent_add(dict, "global");
1544
1545 /*
1546 * Create and add sub-attributes which allow other
1547 * protocols to be encapsulated in the internal
1548 * namespace.
1549 */
1550 if (dict_gctx->internal && (dict != dict_gctx->internal)) {
1551 fr_dict_attr_t const *da;
1552 fr_dict_attr_flags_t flags = { 0 };
1553
1556
1558 if (!da) {
1560 dict->root->name, dict->root->attr, FR_TYPE_GROUP, &flags) < 0) {
1561 return -1;
1562 }
1563
1565 fr_assert(da != NULL);
1566 }
1567
1569 }
1570
1571 return 0;
1572}
1573
1574/** Add a vendor to the dictionary
1575 *
1576 * Inserts a vendor entry into the vendor hash table. This must be done before adding
1577 * attributes under a VSA.
1578 *
1579 * @param[in] dict of protocol context we're operating in.
1580 * If NULL the internal dictionary will be used.
1581 * @param[in] name of the vendor.
1582 * @param[in] num Vendor's Private Enterprise Number.
1583 * @return
1584 * - 0 on success.
1585 * - -1 on failure.
1586 */
1587int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
1588{
1589 size_t len;
1590 fr_dict_vendor_t *vendor;
1591
1592 INTERNAL_IF_NULL(dict, -1);
1593
1594 len = strlen(name);
1595 if (len >= FR_DICT_VENDOR_MAX_NAME_LEN) {
1596 fr_strerror_printf("%s: Vendor name too long", __FUNCTION__);
1597 return -1;
1598 }
1599
1600 vendor = talloc_zero(dict, fr_dict_vendor_t);
1601 if (!vendor) {
1602 oom:
1603 fr_strerror_const("Out of memory");
1604 return -1;
1605 }
1606
1607 vendor->name = talloc_typed_strdup(vendor, name);
1608 if (!vendor->name) {
1609 talloc_free(vendor);
1610 goto oom;
1611 }
1612 vendor->pen = num;
1613 vendor->type = vendor->length = 1; /* defaults */
1614
1615 if (!fr_hash_table_insert(dict->vendors_by_name, vendor)) {
1616 fr_dict_vendor_t const *old_vendor;
1617
1618 old_vendor = fr_hash_table_find(dict->vendors_by_name, vendor);
1619 if (!old_vendor) {
1620 fr_strerror_printf("%s: Failed inserting vendor name %s", __FUNCTION__, name);
1621 return -1;
1622 }
1623 if ((strcmp(old_vendor->name, vendor->name) == 0) && (old_vendor->pen != vendor->pen)) {
1624 fr_strerror_printf("%s: Duplicate vendor name %s", __FUNCTION__, name);
1625 return -1;
1626 }
1627
1628 /*
1629 * Already inserted. Discard the duplicate entry.
1630 */
1631 talloc_free(vendor);
1632
1633 return 0;
1634 }
1635
1636 /*
1637 * Insert the SAME pointer (not free'd when this table is
1638 * deleted), into another table.
1639 *
1640 * We want this behaviour because we want OLD names for
1641 * the attributes to be read from the configuration
1642 * files, but when we're printing them, (and looking up
1643 * by value) we want to use the NEW name.
1644 */
1645 if (fr_hash_table_replace(NULL, dict->vendors_by_num, vendor) < 0) {
1646 fr_strerror_printf("%s: Failed inserting vendor %s", __FUNCTION__, name);
1647 return -1;
1648 }
1649
1650 return 0;
1651}
1652
1653/** See if a #fr_dict_attr_t can have children
1654 *
1655 * @param da the dictionary attribute to check.
1656 */
1658{
1659 switch (da->type) {
1660 case FR_TYPE_TLV:
1661 case FR_TYPE_VENDOR:
1662 case FR_TYPE_VSA:
1663 case FR_TYPE_STRUCT:
1664 case FR_TYPE_UNION:
1665 return true;
1666
1667 default:
1668 break;
1669 }
1670
1671 return false;
1672}
1673
1674/** Add a child to a parent.
1675 *
1676 * @param[in] parent we're adding a child to.
1677 * @param[in] child to add to parent.
1678 * @return
1679 * - 0 on success.
1680 * - -1 on failure (memory allocation error).
1681 */
1683{
1684 fr_dict_attr_t const * const *bin;
1685 fr_dict_attr_t **this;
1686 fr_dict_attr_t const **children;
1687
1688 /*
1689 * Setup fields in the child
1690 */
1691 fr_assert(child->parent == parent);
1692
1693 DA_VERIFY(child);
1694
1695 if (fr_dict_attr_ref(parent)) {
1696 fr_strerror_printf("Cannot add children to attribute '%s' which has 'ref=%s'",
1698 return -1;
1699 }
1700
1702 fr_strerror_printf("Cannot add children to attribute '%s' of type %s",
1703 parent->name,
1704 fr_type_to_str(parent->type));
1705 return -1;
1706 }
1707
1708 if ((parent->type == FR_TYPE_VSA) && (child->type != FR_TYPE_VENDOR)) {
1709 fr_strerror_printf("Cannot add non-vendor children to attribute '%s' of type %s",
1710 parent->name,
1711 fr_type_to_str(parent->type));
1712 return -1;
1713 }
1714
1715 /*
1716 * The parent has children by name only, not by number. Don't even bother trying to track
1717 * numbers, except for VENDOR in root, and MEMBER of a struct.
1718 */
1719 if (!parent->flags.is_root && parent->flags.name_only &&
1720 (parent->type != FR_TYPE_STRUCT) && (parent->type != FR_TYPE_TLV)) {
1721 return 0;
1722 }
1723
1724 /*
1725 * We only allocate the pointer array *if* the parent has children.
1726 */
1727 children = dict_attr_children(parent);
1728 if (!children) {
1729 children = talloc_zero_array(parent, fr_dict_attr_t const *, UINT8_MAX + 1);
1730 if (!children) {
1731 fr_strerror_const("Out of memory");
1732 return -1;
1733 }
1734 if (dict_attr_children_set(parent, children) < 0) return -1;
1735 }
1736
1737 /*
1738 * Treat the array as a hash of 255 bins, with attributes
1739 * sorted into bins using num % 255.
1740 *
1741 * Although the various protocols may define numbers higher than 255:
1742 *
1743 * RADIUS/DHCPv4 - 1-255
1744 * Diameter/Internal - 1-4294967295
1745 * DHCPv6 - 1-65535
1746 *
1747 * In reality very few will ever use attribute numbers > 500, so for
1748 * the majority of lookups we get O(1) performance.
1749 *
1750 * Attributes are inserted into the bin in order of their attribute
1751 * numbers to allow slightly more efficient lookups.
1752 */
1753 for (bin = &children[child->attr & 0xff]; *bin; bin = &(*bin)->next) {
1754 /*
1755 * Workaround for vendors that overload the RFC space.
1756 * Structural attributes always take priority.
1757 */
1758 bool child_is_struct = fr_type_is_structural(child->type);
1759 bool bin_is_struct = fr_type_is_structural((*bin)->type);
1760
1761 if (child_is_struct && !bin_is_struct) break;
1762 if (fr_dict_vendor_num_by_da(child) <= fr_dict_vendor_num_by_da(*bin)) break; /* Prioritise RFC attributes */
1763 if (child->attr <= (*bin)->attr) break;
1764 }
1765
1766 memcpy(&this, &bin, sizeof(this));
1767 child->next = *this;
1768 *this = child;
1769
1770 return 0;
1771}
1772
1773/** Add an attribute to the name table for an attribute
1774 *
1775 * @param[in] parent containing the namespace to add this attribute to.
1776 * @param[in] da to add to the name lookup tables.
1777 * @return
1778 * - 0 on success.
1779 * - -1 on failure.
1780 */
1782{
1783 fr_hash_table_t *namespace;
1784
1785 namespace = dict_attr_namespace(parent);
1786 if (unlikely(!namespace)) {
1787 fr_strerror_printf("Parent \"%s\" has no namespace", parent->name);
1788 error:
1789 return -1;
1790 }
1791
1792 /*
1793 * Sanity check to stop children of vendors ending
1794 * up in the Vendor-Specific or root namespace.
1795 */
1796 if ((fr_dict_vendor_num_by_da(da) != 0) && (da->type != FR_TYPE_VENDOR) &&
1797 ((parent->type == FR_TYPE_VSA) || parent->flags.is_root)) {
1798 fr_strerror_printf("Cannot insert attribute '%s' of type %s into %s",
1799 da->name,
1800 fr_type_to_str(da->type),
1801 parent->name);
1802 goto error;
1803 }
1804
1805 /*
1806 * Insert the attribute, only if it's not a duplicate.
1807 */
1808 if (!fr_hash_table_insert(namespace, da)) {
1809 fr_dict_attr_t *a;
1810
1811 /*
1812 * Find the old name. If it's the same name and
1813 * but the parent, or number, or type are
1814 * different, that's an error.
1815 */
1816 a = fr_hash_table_find(namespace, da);
1817 if (a && (strcasecmp(a->name, da->name) == 0)) {
1818 if ((a->attr != da->attr) || (a->type != da->type) || (a->parent != da->parent)) {
1819 fr_strerror_printf("Duplicate attribute name '%s' in namespace '%s'. "
1820 "Originally defined %s[%d]",
1821 da->name, parent->name,
1822 a->filename, a->line);
1823 goto error;
1824 }
1825 }
1826
1827 /*
1828 * Otherwise the attribute has been redefined later
1829 * in the dictionary.
1830 *
1831 * The original fr_dict_attr_t remains in the
1832 * dictionary but entry in the name hash table is
1833 * updated to point to the new definition.
1834 */
1835 if (fr_hash_table_replace(NULL, namespace, da) < 0) {
1836 fr_strerror_const("Internal error storing attribute");
1837 goto error;
1838 }
1839 }
1840
1841 return 0;
1842}
1843
1844/** A variant of fr_dict_attr_t that allows a pre-allocated, populated fr_dict_attr_t to be added
1845 *
1846 */
1848{
1849 fr_dict_attr_t const *exists;
1850
1851 if (unlikely(da->dict->read_only)) {
1852 fr_strerror_printf("%s dictionary has been marked as read only", fr_dict_root(da->dict)->name);
1853 return -1;
1854 }
1855
1856 if (unlikely(da->state.finalised == false)) {
1857 fr_strerror_const("Attribute has not been finalised");
1858 return -1;
1859 }
1860
1861 /*
1862 * Check that the definition is valid.
1863 */
1864 if (!dict_attr_valid(da)) return -1;
1865
1866 /*
1867 * Don't allow duplicate names
1868 *
1869 * Previously we allowed duplicate names, but only if the
1870 * attributes were compatible (we'd just ignore the operation).
1871 *
1872 * But as attribute parsing may have generated fixups, which
1873 * we'd now need to unpick, it's easier just to error out
1874 * and have the user fix the duplicate.
1875 */
1876 exists = fr_dict_attr_by_name(NULL, da->parent, da->name);
1877 if (exists) {
1878 fr_strerror_printf("Duplicate attribute name '%s' in namespace '%s'. "
1879 "Originally defined %s[%d]", da->name, da->parent->name,
1880 exists->filename, exists->line);
1881 return -1;
1882 }
1883
1884 /*
1885 * In some cases name_only attributes may have explicitly
1886 * assigned numbers. Ensure that there are no conflicts
1887 * between auto-assigned and explkicitly assigned.
1888 */
1889 if (da->flags.name_only) {
1890 if (da->state.attr_set) {
1892
1893 if (da->attr > da->parent->last_child_attr) {
1894 parent->last_child_attr = da->attr;
1895
1896 /*
1897 * If the attribute is outside of the bounds of
1898 * the type size, then it MUST be an internal
1899 * attribute. Set the flag in this attribute, so
1900 * that the encoder doesn't have to do complex
1901 * checks.
1902 */
1903 if ((da->attr >= (((uint64_t)1) << (8 * parent->flags.type_size)))) da->flags.internal = true;
1904 }
1905 } else if (unlikely(dict_attr_num_init_name_only(da)) < 0) {
1906 return -1;
1907 }
1908 }
1909
1910 /*
1911 * Attributes can also be indexed by number. Ensure that
1912 * all attributes of the same number have the same
1913 * properties.
1914 */
1915 exists = fr_dict_attr_child_by_num(da->parent, da->attr);
1916 if (exists) {
1917 fr_strerror_printf("Duplicate attribute number %u in namespace '%s'. "
1918 "Originally defined by '%s' at %s[%d]",
1919 da->attr, da->parent->name, exists->name, exists->filename, exists->line);
1920 return -1;
1921 }
1922
1923 /*
1924 * Add in by number
1925 */
1926 if (dict_attr_child_add(UNCONST(fr_dict_attr_t *, da->parent), da) < 0) return -1;
1927
1928 /*
1929 * Add in by name
1930 */
1931 if (dict_attr_add_to_namespace(da->parent, da) < 0) return -1;
1932
1933#ifndef NDEBUG
1934 {
1935 fr_dict_attr_t const *found;
1936
1937 /*
1938 * Check if we added the attribute
1939 */
1940 found = dict_attr_child_by_num(da->parent, da->attr);
1941 if (!found) {
1942 fr_strerror_printf("FATAL - Failed to find attribute number %u we just added to namespace '%s'", da->attr, da->parent->name);
1943 return -1;
1944 }
1945
1946 if (!dict_attr_by_name(NULL, da->parent, da->name)) {
1947 fr_strerror_printf("FATAL - Failed to find attribute '%s' we just added to namespace '%s'", da->name, da->parent->name);
1948 return -1;
1949 }
1950 }
1951#endif
1952
1953 return 0;
1954}
1955
1956/** Add an attribute to the dictionary
1957 *
1958 * @param[in] dict of protocol context we're operating in.
1959 * If NULL the internal dictionary will be used.
1960 * @param[in] parent to add attribute under.
1961 * @param[in] name of the attribute.
1962 * @param[in] attr number.
1963 * @param[in] type of attribute.
1964 * @param[in] flags to set in the attribute.
1965 * @return
1966 * - 0 on success.
1967 * - -1 on failure.
1968 */
1970 char const *name, unsigned int attr, fr_type_t type, fr_dict_attr_flags_t const *flags)
1971{
1972 fr_dict_attr_t *da;
1973
1974 if (fr_dict_attr_ref(parent)) {
1975 fr_strerror_printf("Cannot add children to attribute '%s' which has 'ref=%s'",
1977 return -1;
1978 }
1979
1981 fr_strerror_printf("Cannot add children to attribute '%s' of type %s",
1982 parent->name,
1983 fr_type_to_str(parent->type));
1984 return -1;
1985 }
1986
1987 da = dict_attr_alloc_null(dict->pool, dict->proto);
1988 if (unlikely(!da)) return -1;
1989
1990 if (dict_attr_init(&da, parent, name,
1991 attr, type, &(dict_attr_args_t){ .flags = flags}) < 0) return -1;
1992
1994}
1995
1996/** Add an attribute to the dictionary
1997 *
1998 * @param[in] dict of protocol context we're operating in.
1999 * If NULL the internal dictionary will be used.
2000 * @param[in] parent to add attribute under.
2001 * @param[in] name of the attribute.
2002 * @param[in] type of attribute.
2003 * @param[in] flags to set in the attribute.
2004 * @return
2005 * - 0 on success.
2006 * - -1 on failure.
2007 */
2009 char const *name, fr_type_t type, fr_dict_attr_flags_t const *flags)
2010{
2011 fr_dict_attr_t *da;
2012
2013 da = dict_attr_alloc_null(dict->pool, dict->proto);
2014 if (unlikely(!da)) return -1;
2015
2016 if (dict_attr_init_name_only(&da, parent, name,type, &(dict_attr_args_t){ .flags = flags}) < 0) return -1;
2017
2019}
2020
2021
2023 fr_value_box_t const *value,
2024 bool coerce, bool takes_precedence,
2025 fr_dict_attr_t const *key_child_ref)
2026{
2027 size_t len;
2028 fr_dict_enum_value_t *enumv = NULL;
2029 fr_value_box_t *enum_value = NULL;
2031
2032 if (!da) {
2033 fr_strerror_printf("%s: Dictionary attribute not specified", __FUNCTION__);
2034 return -1;
2035 }
2036
2037 if (!*name) {
2038 fr_strerror_printf("%s: Empty names are not permitted", __FUNCTION__);
2039 return -1;
2040 }
2041
2042 len = strlen(name);
2043 if (len >= FR_DICT_ENUM_MAX_NAME_LEN) {
2044 fr_strerror_printf("VALUE name is too long");
2045 return -1;
2046 }
2047
2048 /*
2049 * If the parent isn't a key field, then we CANNOT add a child struct.
2050 */
2051 if (!fr_dict_attr_is_key_field(da) && key_child_ref) {
2052 fr_strerror_const("Child attributes cannot be defined for VALUEs which are not 'key' attributes");
2053 return -1;
2054 }
2055
2056 if (fr_type_is_structural(da->type) || (da->type == FR_TYPE_STRING)) {
2057 fr_strerror_printf("Enumeration names cannot be added for data type '%s'", fr_type_to_str(da->type));
2058 return -1;
2059 }
2060
2061 if (da->flags.is_alias) {
2062 fr_strerror_printf("Enumeration names cannot be added for ALIAS '%s'", da->name);
2063 return -1;
2064 }
2065
2067 if (!ext) {
2068 fr_strerror_printf("VALUE cannot be defined for %s", da->name);
2069 return -1;
2070 }
2071
2072 /*
2073 * Initialise enumv hash tables
2074 */
2075 if (!ext->value_by_name || !ext->name_by_value) {
2078 if (!ext->value_by_name) {
2079 fr_strerror_printf("Failed allocating \"value_by_name\" table");
2080 return -1;
2081 }
2082
2084 dict_enum_value_cmp, NULL);
2085 if (!ext->name_by_value) {
2086 fr_strerror_printf("Failed allocating \"name_by_value\" table");
2087 return -1;
2088 }
2089 }
2090
2091 /*
2092 * Allocate a structure to map between
2093 * the name and value.
2094 */
2095 enumv = talloc_zero_size(da, sizeof(fr_dict_enum_value_t));
2096 if (!enumv) {
2097 oom:
2098 fr_strerror_printf("%s: Out of memory", __FUNCTION__);
2099 return -1;
2100 }
2101 talloc_set_type(enumv, fr_dict_enum_value_t);
2102
2103 enumv->name = talloc_typed_strdup(enumv, name);
2104 enumv->name_len = len;
2105
2106 if (key_child_ref) {
2108
2110 if (!ref) goto oom;
2111
2112 ref->da = key_child_ref;
2113 }
2114
2115 enum_value = fr_value_box_alloc(enumv, da->type, NULL);
2116 if (!enum_value) goto oom;
2117
2118 if (da->type != value->type) {
2119 if (!coerce) {
2120 fr_strerror_printf("Type mismatch between attribute (%s) and enum (%s)",
2121 fr_type_to_str(da->type),
2122 fr_type_to_str(value->type));
2123 return -1;
2124 }
2125
2126 if (fr_value_box_cast(enumv, enum_value, da->type, NULL, value) < 0) {
2127 fr_strerror_printf_push("Failed coercing enum type (%s) to attribute type (%s)",
2128 fr_type_to_str(value->type),
2129 fr_type_to_str(da->type));
2130
2131 return -1;
2132 }
2133 } else {
2134 if (unlikely(fr_value_box_copy(enum_value, enum_value, value) < 0)) {
2135 fr_strerror_printf_push("%s: Failed copying value into enum", __FUNCTION__);
2136 return -1;
2137 }
2138 }
2139
2140 enumv->value = enum_value;
2141
2142 /*
2143 * Add the value into the dictionary.
2144 */
2145 {
2146 fr_dict_attr_t *tmp;
2147 memcpy(&tmp, &enumv, sizeof(tmp));
2148
2149 if (!fr_hash_table_insert(ext->value_by_name, tmp)) {
2150 fr_dict_enum_value_t const *old;
2151
2152 /*
2153 * Suppress duplicates with the same
2154 * name and value. There are lots in
2155 * dictionary.ascend.
2156 */
2157 old = fr_dict_enum_by_name(da, name, -1);
2158 if (!fr_cond_assert(old)) return -1;
2159
2160 if (fr_value_box_cmp(old->value, enumv->value) == 0) {
2161 talloc_free(enumv);
2162 return 0;
2163 }
2164
2165 fr_strerror_printf("Duplicate VALUE name \"%s\" for Attribute '%s'. "
2166 "Old value was \"%pV\", new value was \"%pV\"", name, da->name,
2167 old->value, enumv->value);
2168 talloc_free(enumv);
2169 return -1;
2170 }
2171
2172 if (enumv->name_len > ext->max_name_len) ext->max_name_len = enumv->name_len;
2173 }
2174
2175 /*
2176 * There are multiple VALUE's, keyed by attribute, so we
2177 * take care of that here.
2178 */
2179 if (takes_precedence) {
2180 if (fr_hash_table_replace(NULL, ext->name_by_value, enumv) < 0) {
2181 fr_strerror_printf("%s: Failed inserting value %s", __FUNCTION__, name);
2182 return -1;
2183 }
2184 } else {
2185 (void) fr_hash_table_insert(ext->name_by_value, enumv);
2186 }
2187
2188 /*
2189 * Mark the attribute up as having an enumv
2190 */
2191 UNCONST(fr_dict_attr_t *, da)->flags.has_value = 1;
2192
2193 return 0;
2194}
2195
2196/** Add a value name
2197 *
2198 * Aliases are textual (string) names for a given value.
2199 *
2200 * Value names are not limited to integers, and may be added for any non-structural
2201 * attribute type.
2202 *
2203 * @param[in] da to add enumeration value to.
2204 * @param[in] name Name of value name.
2205 * @param[in] value to associate with name.
2206 * @param[in] coerce if the type of the value does not match the
2207 * type of the da, attempt to cast it to match
2208 * the type of the da. If this is false and there's
2209 * a type mismatch, we fail.
2210 * We also fail if the value cannot be coerced to
2211 * the attribute type.
2212 * @param[in] takes_precedence This name should take precedence over previous
2213 * names for the same value, when resolving value
2214 * to name.
2215 * @return
2216 * - 0 on success.
2217 * - -1 on failure.
2218 */
2220 fr_value_box_t const *value,
2221 bool coerce, bool takes_precedence)
2222{
2223 return dict_attr_enum_add_name(da, name, value, coerce, takes_precedence, NULL);
2224}
2225
2226/** Add an name to an integer attribute hashing the name for the integer value
2227 *
2228 * If the integer value conflicts with an existing name, it's incremented
2229 * until we find a free value.
2230 */
2232{
2233 fr_value_box_t v = {
2234 .type = da->type
2235 };
2236 fr_value_box_t s = {
2237 .type = da->type
2238 };
2239
2240 if (fr_dict_enum_by_name(da, name, -1)) return 0;
2241
2242 switch (da->type) {
2243 case FR_TYPE_INT8:
2244 v.vb_int8 = s.vb_int8 = fr_hash_string(name) & INT8_MAX;
2245 break;
2246
2247 case FR_TYPE_INT16:
2248 v.vb_int16 = s.vb_int16 = fr_hash_string(name) & INT16_MAX;
2249 break;
2250
2251 case FR_TYPE_INT32:
2252 v.vb_int32 = s.vb_int32 = fr_hash_string(name) & INT32_MAX;
2253 break;
2254
2255 case FR_TYPE_INT64:
2256 v.vb_int64 = s.vb_int64 = fr_hash_string(name) & INT64_MAX;
2257 break;
2258
2259 case FR_TYPE_UINT8:
2260 v.vb_uint8 = s.vb_uint8 = fr_hash_string(name) & UINT8_MAX;
2261 break;
2262
2263 case FR_TYPE_UINT16:
2264 v.vb_uint16 = s.vb_uint16 = fr_hash_string(name) & UINT16_MAX;
2265 break;
2266
2267 case FR_TYPE_UINT32:
2268 v.vb_uint32 = s.vb_uint32 = fr_hash_string(name) & UINT32_MAX;
2269 break;
2270
2271 case FR_TYPE_UINT64:
2272 v.vb_uint64 = s.vb_uint64 = fr_hash_string(name) & UINT64_MAX;
2273 break;
2274
2275 default:
2276 fr_strerror_printf("Attribute is wrong type for auto-numbering, expected numeric type, got %s",
2277 fr_type_to_str(da->type));
2278 return -1;
2279 }
2280
2281 /*
2282 * If there's no existing value, add an enum
2283 * with the hash value of the name.
2284 *
2285 * This helps with debugging as the values
2286 * are consistent.
2287 */
2288 if (!fr_dict_enum_by_value(da, &v)) {
2289 add:
2290 return fr_dict_enum_add_name(da, name, &v, false, false);
2291 }
2292
2293 for (;;) {
2295
2296 if (fr_value_box_cmp_op(T_OP_CMP_EQ, &v, &s) == 0) {
2297 fr_strerror_const("No free integer values for enumeration");
2298 return -1;
2299 }
2300
2301 if (!fr_dict_enum_by_value(da, &v)) goto add;
2302 }
2303 /* NEVER REACHED */
2304}
2305
2306/** Find a common ancestor that two TLV type attributes share
2307 *
2308 * @param[in] a first TLV attribute.
2309 * @param[in] b second TLV attribute.
2310 * @param[in] is_ancestor Enforce a->b relationship (a is parent or ancestor of b).
2311 * @return
2312 * - Common ancestor if one exists.
2313 * - NULL if no common ancestor exists.
2314 */
2316{
2317 unsigned int i;
2318 fr_dict_attr_t const *p_a, *p_b;
2319
2320 if (!a || !b) return NULL;
2321
2322 if (is_ancestor && (b->depth <= a->depth)) return NULL; /* fast_path */
2323
2324 /*
2325 * Find a common depth to work back from
2326 */
2327 if (a->depth > b->depth) {
2328 p_b = b;
2329 for (p_a = a, i = a->depth - b->depth; p_a && (i > 0); p_a = p_a->parent, i--);
2330 if (is_ancestor && (p_a != p_b)) return NULL;
2331 } else if (a->depth < b->depth) {
2332 p_a = a;
2333 for (p_b = b, i = b->depth - a->depth; p_b && (i > 0); p_b = p_b->parent, i--);
2334 if (is_ancestor && (p_a != p_b)) return NULL;
2335 } else {
2336 p_a = a;
2337 p_b = b;
2338 }
2339
2340 while (p_a && p_b) {
2341 if (p_a == p_b) return p_a;
2342
2343 p_a = p_a->parent;
2344 p_b = p_b->parent;
2345 }
2346
2347 return NULL;
2348}
2349
2350/** Process a single OID component
2351 *
2352 * @param[out] out Value of component.
2353 * @param[in] oid string to parse.
2354 * @return
2355 * - 0 on success.
2356 * - -1 on format error.
2357 */
2358int fr_dict_oid_component_legacy(unsigned int *out, char const **oid)
2359{
2360 char const *p = *oid;
2361 char *q;
2362 unsigned long num;
2363
2364 *out = 0;
2365
2366 num = strtoul(p, &q, 10);
2367 if ((p == q) || (num == ULONG_MAX)) {
2368 fr_strerror_printf("Invalid OID component \"%s\" (%lu)", p, num);
2369 return -1;
2370 }
2371
2372 switch (*q) {
2373 case '\0':
2374 case '.':
2375 *oid = q;
2376 *out = (unsigned int)num;
2377
2378 return 0;
2379
2380 default:
2381 fr_strerror_const("Unexpected text after OID component");
2382 *out = 0;
2383 return -1;
2384 }
2385}
2386
2387/** Get the leaf attribute of an OID string
2388 *
2389 * @note On error, vendor will be set (if present), parent will be the
2390 * maximum depth we managed to resolve to, and attr will be the child
2391 * we failed to resolve.
2392 *
2393 * @param[in] dict of protocol context we're operating in.
2394 * If NULL the internal dictionary will be used.
2395 * @param[out] attr Number we parsed.
2396 * @param[in,out] parent attribute (or root of dictionary).
2397 * Will be updated to the parent directly beneath the leaf.
2398 * @param[in] oid string to parse.
2399 * @return
2400 * - > 0 on success (number of bytes parsed).
2401 * - <= 0 on parse error (negative offset of parse error).
2402 */
2403ssize_t fr_dict_attr_by_oid_legacy(fr_dict_t const *dict, fr_dict_attr_t const **parent, unsigned int *attr, char const *oid)
2404{
2405 char const *p = oid;
2406 unsigned int num = 0;
2407 ssize_t slen;
2408
2409 if (!*parent) return -1;
2410
2411 /*
2412 * It's a partial OID. Grab it, and skip to the next bit.
2413 */
2414 if (p[0] == '.') {
2415 p++;
2416 }
2417
2418 *attr = 0;
2419
2420 if (fr_dict_oid_component_legacy(&num, &p) < 0) return oid - p;
2421
2422 /*
2423 * Record progress even if we error out.
2424 *
2425 * Don't change this, you will break things.
2426 */
2427 *attr = num;
2428
2429 /*
2430 * Only a limited number of structural types can have children. Specifically, groups cannot.
2431 */
2433 fr_strerror_printf("Attribute %s (%u) cannot contain a child attribute. "
2434 "Error at sub OID \"%s\"", (*parent)->name, (*parent)->attr, oid);
2435 return 0; /* We parsed nothing */
2436 }
2437
2438 switch (p[0]) {
2439 /*
2440 * We've not hit the leaf yet, so the attribute must be
2441 * defined already.
2442 */
2443 case '.':
2444 {
2445 fr_dict_attr_t const *child;
2446 p++;
2447
2448 child = dict_attr_child_by_num(*parent, num);
2449 if (!child) {
2450 fr_strerror_printf("Unknown attribute '%u' in OID string \"%s\" for parent %s",
2451 num, oid, (*parent)->name);
2452 return 0; /* We parsed nothing */
2453 }
2454
2455 /*
2456 * Record progress even if we error out.
2457 *
2458 * Don't change this, you will break things.
2459 */
2460 *parent = child;
2461
2462 slen = fr_dict_attr_by_oid_legacy(dict, parent, attr, p);
2463 if (slen <= 0) return slen - (p - oid);
2464 return slen + (p - oid);
2465 }
2466
2467 /*
2468 * Hit the leaf, this is the attribute we need to define.
2469 */
2470 case '\0':
2471 *attr = num;
2472 return p - oid;
2473
2474 default:
2475 fr_strerror_printf("Malformed OID string, got trailing garbage '%s'", p);
2476 return oid - p;
2477 }
2478}
2479
2480/** Parse an OID component, resolving it to a defined attribute
2481 *
2482 * @note Will leave the sbuff pointing at the component the error occurred at
2483 * so that the caller can attempt to process the component in another way.
2484 *
2485 * @param[out] err The parsing error that occurred.
2486 * @param[out] out The deepest attribute we resolved.
2487 * @param[in] parent Where to resolve relative attributes from.
2488 * @param[in] in string to parse.
2489 * @param[in] tt Terminal strings.
2490 * @return
2491 * - >0 the number of bytes consumed.
2492 * - <0 Parse error occurred here.
2493 */
2495 fr_dict_attr_t const **out, fr_dict_attr_t const *parent,
2496 fr_sbuff_t *in, fr_sbuff_term_t const *tt)
2497{
2498 fr_sbuff_t our_in = FR_SBUFF(in);
2499 uint32_t num = 0;
2501 fr_dict_attr_t const *child;
2502
2503 if (err) *err = FR_DICT_ATTR_OK;
2504
2505 *out = NULL;
2506
2508 fr_strerror_printf("Attribute '%s' is type %s and cannot contain child attributes. "
2509 "Error at OID \"%.*s\"",
2510 parent->name,
2511 fr_type_to_str(parent->type),
2512 (int)fr_sbuff_remaining(&our_in),
2513 fr_sbuff_current(&our_in));
2515 FR_SBUFF_ERROR_RETURN(&our_in);
2516 }
2517
2518 if (fr_dict_attr_by_name_substr(err, &child, parent, &our_in, tt) > 0) goto done;
2519
2520 fr_sbuff_out(&sberr, &num, &our_in);
2521 switch (sberr) {
2522 /*
2523 * Lookup by number
2524 */
2525 case FR_SBUFF_PARSE_OK:
2526 if (!fr_sbuff_is_char(&our_in, '.') && !fr_sbuff_is_terminal(&our_in, tt)) {
2528 fr_strerror_printf("Invalid OID component (%s) \"%.*s\"",
2530 (int)fr_sbuff_remaining(&our_in), fr_sbuff_current(&our_in));
2531 goto fail;
2532 }
2533
2534 child = dict_attr_child_by_num(parent, num);
2535 if (!child) {
2536 fr_sbuff_set_to_start(&our_in);
2537 fr_strerror_printf("Failed resolving child %u in namespace '%s'",
2538 num, parent->name);
2540 FR_SBUFF_ERROR_RETURN(&our_in);
2541 }
2542
2543 if (err) *err = FR_DICT_ATTR_OK;
2544 break;
2545
2548
2549 fr_sbuff_set_to_start(&our_in);
2550
2551 {
2552 fr_sbuff_marker_t c_start;
2553
2554 fr_sbuff_marker(&c_start, &our_in);
2556 fr_strerror_printf("Invalid value \"%.*s\" - attribute numbers must be less than 2^32",
2557 (int)fr_sbuff_behind(&c_start), fr_sbuff_current(&c_start));
2558 }
2559 FR_SBUFF_ERROR_RETURN(&our_in);
2560
2561 default:
2562 fail:
2563 /*
2564 * Leave *err from the call to fr_dict_attr_by_name_substr().
2565 */
2566 fr_sbuff_set_to_start(&our_in);
2567 FR_SBUFF_ERROR_RETURN(&our_in);
2568 }
2569
2570done:
2571 child = dict_attr_alias(err, child);
2572 if (unlikely(!child)) FR_SBUFF_ERROR_RETURN(&our_in);
2573
2574 *out = child;
2575
2576 FR_SBUFF_SET_RETURN(in, &our_in);
2577}
2578
2579/** Resolve an attribute using an OID string
2580 *
2581 * @note Will leave the sbuff pointing at the component the error occurred at
2582 * so that the caller can attempt to process the component in another way.
2583 * An err pointer should be provided in order to determine if an error
2584 * occurred.
2585 *
2586 * @param[out] err The parsing error that occurred.
2587 * @param[out] out The deepest attribute we resolved.
2588 * @param[in] parent Where to resolve relative attributes from.
2589 * @param[in] in string to parse.
2590 * @param[in] tt Terminal strings.
2591 * @return The number of bytes of name consumed.
2592 */
2594 fr_dict_attr_t const **out, fr_dict_attr_t const *parent,
2595 fr_sbuff_t *in, fr_sbuff_term_t const *tt)
2596{
2597 fr_sbuff_t our_in = FR_SBUFF(in);
2599 fr_dict_attr_t const *our_parent = parent;
2600
2601 fr_sbuff_marker(&m_c, &our_in);
2602
2603 /*
2604 * If the OID doesn't begin with '.' we
2605 * resolve it from the root.
2606 */
2607#if 0
2608 if (!fr_sbuff_next_if_char(&our_in, '.')) our_parent = fr_dict_root(fr_dict_by_da(parent));
2609#else
2610 (void) fr_sbuff_next_if_char(&our_in, '.');
2611#endif
2612 *out = NULL;
2613
2614 for (;;) {
2615 fr_dict_attr_t const *child;
2616
2617 if ((fr_dict_oid_component(err, &child, our_parent, &our_in, tt) < 0) || !child) {
2618 *out = our_parent;
2619 fr_sbuff_set(&our_in, &m_c); /* Reset to the start of the last component */
2620 break; /* Resolved as much as we can */
2621 }
2622
2623 our_parent = child;
2624 *out = child;
2625
2626 fr_sbuff_set(&m_c, &our_in);
2627 if (!fr_sbuff_next_if_char(&our_in, '.')) break;
2628 }
2629
2630 FR_SBUFF_SET_RETURN(in, &our_in);
2631}
2632
2633/** Resolve an attribute using an OID string
2634 *
2635 * @param[out] err The parsing error that occurred.
2636 * @param[in] parent Where to resolve relative attributes from.
2637 * @param[in] oid string to parse.
2638 * @return
2639 * - NULL if we couldn't resolve the attribute.
2640 * - The resolved attribute.
2641 */
2643{
2644 fr_sbuff_t sbuff = FR_SBUFF_IN(oid, strlen(oid));
2645 fr_dict_attr_t const *da;
2646
2647 if (fr_dict_attr_by_oid_substr(err, &da, parent, &sbuff, NULL) <= 0) return NULL;
2648 if (err && *err != FR_DICT_ATTR_OK) return NULL;
2649
2650 /*
2651 * If we didn't parse the entire string, then the parsing stopped at an unknown child.
2652 * e.g. Vendor-Specific.Cisco.Foo. In that case, the full attribute wasn't found.
2653 */
2654 if (fr_sbuff_remaining(&sbuff) > 0) {
2656 return NULL;
2657 }
2658
2659 return da;
2660}
2661
2662/** Return the root attribute of a dictionary
2663 *
2664 * @param dict to return root for.
2665 * @return the root attribute of the dictionary.
2666 *
2667 * @hidecallergraph
2668 */
2670{
2671 return dict->root;
2672}
2673
2675{
2676 return dict->read_only;
2677}
2678
2680{
2681 return dict->dl;
2682}
2683
2685 fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def)
2686{
2687 fr_dict_attr_t root;
2688
2689 fr_sbuff_t our_name;
2690 fr_dict_t *dict;
2691 fr_slen_t slen;
2692 char buffer[FR_DICT_ATTR_MAX_NAME_LEN + 1 + 1]; /* +1 \0 +1 for "too long" */
2693
2694 if (!dict_gctx || !name || !out) {
2695 if (err) *err = FR_DICT_ATTR_EINVAL;
2697 return 0;
2698 }
2699
2700 our_name = FR_SBUFF(name);
2701 memset(&root, 0, sizeof(root));
2702
2703 /*
2704 * Advance p until we get something that's not part of
2705 * the dictionary attribute name.
2706 */
2708 &our_name, SIZE_MAX,
2710 if (slen == 0) {
2711 fr_strerror_const("Zero length attribute name");
2713 FR_SBUFF_ERROR_RETURN(&our_name);
2714 }
2715 if (slen > FR_DICT_ATTR_MAX_NAME_LEN) {
2716 fr_strerror_const("Attribute name too long");
2718 FR_SBUFF_ERROR_RETURN(&our_name);
2719 }
2720
2721 /*
2722 * The remaining operations don't generate errors
2723 */
2724 if (err) *err = FR_DICT_ATTR_OK;
2725
2726 /*
2727 * If what we stopped at wasn't a '.', then there
2728 * can't be a protocol name in this string.
2729 */
2730 if (*(our_name.p) && (*(our_name.p) != '.')) {
2731 memcpy(out, &dict_def, sizeof(*out));
2732 return 0;
2733 }
2734
2735 root.name = buffer;
2736 dict = fr_hash_table_find(dict_gctx->protocol_by_name, &(fr_dict_t){ .root = &root });
2737
2738 if (!dict) {
2739 if (strcasecmp(root.name, "internal") != 0) {
2740 fr_strerror_printf("Unknown protocol '%s'", root.name);
2741 memcpy(out, &dict_def, sizeof(*out));
2742 fr_sbuff_set_to_start(&our_name);
2743 FR_SBUFF_ERROR_RETURN(&our_name);
2744 }
2745
2746 dict = dict_gctx->internal;
2747 }
2748
2749 *out = dict;
2750
2751 FR_SBUFF_SET_RETURN(name, &our_name);
2752}
2753
2754/** Look up a protocol name embedded in another string
2755 *
2756 * @param[out] err Parsing error.
2757 * @param[out] out the resolve dictionary or NULL if the dictionary
2758 * couldn't be resolved.
2759 * @param[in] name string start.
2760 * @param[in] dict_def The dictionary to return if no dictionary qualifier was found.
2761 * @return
2762 * - 0 and *out != NULL. Couldn't find a dictionary qualifier, so returned dict_def.
2763 * - < 0 on error and (*out == NULL) (offset as negative integer)
2764 * - > 0 on success (number of bytes parsed).
2765 */
2770
2771/** Internal version of #fr_dict_by_protocol_name
2772 *
2773 * @note For internal use by the dictionary API only.
2774 *
2775 * @copybrief fr_dict_by_protocol_name
2776 */
2778{
2779 if (!dict_gctx || !name) return NULL;
2780
2782 &(fr_dict_t){ .root = &(fr_dict_attr_t){ .name = name } });
2783}
2784
2785/** Internal version of #fr_dict_by_protocol_num
2786 *
2787 * @note For internal use by the dictionary API only.
2788 *
2789 * @copybrief fr_dict_by_protocol_num
2790 */
2792{
2793 if (!dict_gctx) return NULL;
2794
2796 &(fr_dict_t) { .root = &(fr_dict_attr_t){ .attr = num } });
2797}
2798
2799/** Internal version of #fr_dict_by_da
2800 *
2801 * @note For internal use by the dictionary API only.
2802 *
2803 * @copybrief fr_dict_by_da
2804 */
2806{
2807#ifndef NDEBUG
2808 {
2809 fr_dict_attr_t const *da_p = da;
2810 fr_dict_t const *dict;
2811
2812 dict = da->dict;
2813 while (da_p->parent) {
2814 da_p = da_p->parent;
2815 fr_cond_assert_msg(da_p->dict == dict, "Inconsistent dict membership. "
2816 "Expected %s, got %s",
2817 !da_p->dict ? "(null)" : fr_dict_root(da_p->dict)->name,
2818 !dict ? "(null)" : fr_dict_root(dict)->name);
2819 DA_VERIFY(da_p);
2820 }
2821
2822 if (!da_p->flags.is_root) {
2823 fr_strerror_printf("%s: Attribute %s has not been inserted into a dictionary",
2824 __FUNCTION__, da->name);
2825 return NULL;
2826 }
2827 }
2828#endif
2829
2830 /*
2831 * Parent of the root attribute must
2832 * be the dictionary.
2833 */
2834 return talloc_get_type_abort(da->dict, fr_dict_t);
2835}
2836
2837/** Lookup a protocol by its name
2838 *
2839 * @note For internal use by the dictionary API only.
2840 *
2841 * @param[in] name of the protocol to locate.
2842 * @return
2843 * - Attribute matching name.
2844 * - NULL if no matching protocol could be found.
2845 */
2847{
2849}
2850
2851/** Lookup a protocol by its number
2852 *
2853 * Returns the #fr_dict_t belonging to the protocol with the specified number
2854 * if any have been registered.
2855 *
2856 * @param[in] num to search for.
2857 * @return dictionary representing the protocol (if it exists).
2858 */
2859fr_dict_t const *fr_dict_by_protocol_num(unsigned int num)
2860{
2861 return dict_by_protocol_num(num);
2862}
2863
2864/** Attempt to locate the protocol dictionary containing an attribute
2865 *
2866 * @note Unlike fr_dict_by_attr_name, doesn't search through all the dictionaries,
2867 * just uses the fr_dict_attr_t hierarchy and the talloc hierarchy to locate
2868 * the dictionary (much much faster and more scalable).
2869 *
2870 * @param[in] da To get the containing dictionary for.
2871 * @return
2872 * - The dictionary containing da.
2873 * - NULL.
2874 */
2876{
2877 return dict_by_da(da);
2878}
2879
2880/** See if two dictionaries have the same end parent
2881 *
2882 * @param[in] dict1 one dictionary
2883 * @param[in] dict2 two dictionary
2884 * @return
2885 * - true the dictionaries have the same end parent
2886 * - false the dictionaries do not have the same end parent.
2887 */
2888bool fr_dict_compatible(fr_dict_t const *dict1, fr_dict_t const *dict2)
2889{
2890 while (dict1->next) dict1 = dict1->next;
2891
2892 while (dict2->next) dict2 = dict2->next;
2893
2894 return (dict1 == dict2);
2895}
2896
2897/** Look up a vendor by one of its child attributes
2898 *
2899 * @param[in] da The vendor attribute.
2900 * @return
2901 * - The vendor.
2902 * - NULL if no vendor with that number was registered for this protocol.
2903 */
2905{
2906 fr_dict_t *dict;
2908
2910 if (!dv.pen) return NULL;
2911
2912 dict = dict_by_da(da);
2913
2914 return fr_hash_table_find(dict->vendors_by_num, &dv);
2915}
2916
2917/** Look up a vendor by its name
2918 *
2919 * @param[in] dict of protocol context we're operating in.
2920 * If NULL the internal dictionary will be used.
2921 * @param[in] name to search for.
2922 * @return
2923 * - The vendor.
2924 * - NULL if no vendor with that name was registered for this protocol.
2925 */
2927{
2928 fr_dict_vendor_t *found;
2929
2930 INTERNAL_IF_NULL(dict, NULL);
2931
2932 if (!name) return 0;
2933
2934 found = fr_hash_table_find(dict->vendors_by_name, &(fr_dict_vendor_t) { .name = name });
2935 if (!found) return 0;
2936
2937 return found;
2938}
2939
2940/** Look up a vendor by its PEN
2941 *
2942 * @param[in] dict of protocol context we're operating in.
2943 * If NULL the internal dictionary will be used.
2944 * @param[in] vendor_pen to search for.
2945 * @return
2946 * - The vendor.
2947 * - NULL if no vendor with that number was registered for this protocol.
2948 */
2950{
2951 INTERNAL_IF_NULL(dict, NULL);
2952
2953 return fr_hash_table_find(dict->vendors_by_num, &(fr_dict_vendor_t) { .pen = vendor_pen });
2954}
2955
2956/** Return vendor attribute for the specified dictionary and pen
2957 *
2958 * @param[in] vendor_root of the vendor root attribute. Could be 26 (for example) in RADIUS.
2959 * @param[in] vendor_pen to find.
2960 * @return
2961 * - NULL if vendor does not exist.
2962 * - A fr_dict_attr_t representing the vendor in the dictionary hierarchy.
2963 */
2965{
2966 fr_dict_attr_t const *vendor;
2967
2968 switch (vendor_root->type) {
2969 case FR_TYPE_VSA: /* Vendor specific attribute */
2970 break;
2971
2972 default:
2973 fr_strerror_printf("Wrong type for vendor root, expected '%s', got '%s'",
2975 fr_type_to_str(vendor_root->type));
2976 return NULL;
2977 }
2978
2979 vendor = dict_attr_child_by_num(vendor_root, vendor_pen);
2980 if (!vendor) {
2981 fr_strerror_printf("Vendor %u not defined", vendor_pen);
2982 return NULL;
2983 }
2984
2985 if (vendor->type != FR_TYPE_VENDOR) {
2986 fr_strerror_printf("Wrong type for vendor, expected '%s' got '%s'",
2987 fr_type_to_str(vendor->type),
2989 return NULL;
2990 }
2991
2992 return vendor;
2993}
2994
2995/** Callback function for resolving dictionary attributes
2996 *
2997 * @param[out] err Where to write error codes. Any error
2998 * other than FR_DICT_ATTR_NOTFOUND will
2999 * prevent resolution from continuing.
3000 * @param[out] out Where to write resolved DA.
3001 * @param[in] parent The dictionary root or other attribute to search from.
3002 * @param[in] in Contains the string to resolve.
3003 * @param[in] tt Terminal sequences to use to determine the portion
3004 * of in to search.
3005 * @return
3006 * - < 0 on failure.
3007 * - The number of bytes of name consumed on success.
3008 */
3010 fr_dict_attr_t const **out, fr_dict_attr_t const *parent,
3011 fr_sbuff_t *in, fr_sbuff_term_t const *tt);
3012
3013/** Internal function for searching for attributes in multiple dictionaries
3014 *
3015 * @param[out] err Any errors that occurred searching.
3016 * @param[out] out The attribute we found.
3017 * @param[in] dict_def The default dictionary to search in.
3018 * @param[in] in string to resolve to an attribute.
3019 * @param[in] tt terminals that indicate the end of the string.
3020 * @param[in] internal Resolve the attribute in the internal dictionary.
3021 * @param[in] foreign Resolve attribute in a foreign dictionary,
3022 * i.e. one other than dict_def.
3023 * @param[in] func to use for resolution.
3024 * @return
3025 * - <=0 on error (the offset of the error).
3026 * - >0 on success.
3027 */
3028static inline CC_HINT(always_inline)
3030 fr_dict_t const *dict_def,
3031 fr_sbuff_t *in, fr_sbuff_term_t const *tt,
3032 bool internal, bool foreign,
3034{
3036 fr_hash_iter_t iter;
3037 fr_dict_t *dict = NULL;
3038 fr_sbuff_t our_in = FR_SBUFF(in);
3039
3040 if (internal && !dict_gctx->internal) internal = false;
3041
3042 /*
3043 * Always going to fail...
3044 */
3045 if (unlikely(!internal && !foreign && !dict_def)) {
3046 if (err) *err = FR_DICT_ATTR_EINVAL;
3047 *out = NULL;
3048 return 0;
3049 }
3050
3051 /*
3052 * dict_def search in the specified dictionary
3053 */
3054 if (dict_def) {
3055 (void)func(&our_err, out, fr_dict_root(dict_def), &our_in, tt);
3056 switch (our_err) {
3057 case FR_DICT_ATTR_OK:
3058 FR_SBUFF_SET_RETURN(in, &our_in);
3059
3061 if (!internal && !foreign) goto error;
3062 break;
3063
3064 default:
3065 goto error;
3066 }
3067 }
3068
3069 /*
3070 * Next in the internal dictionary
3071 */
3072 if (internal) {
3073 (void)func(&our_err, out, fr_dict_root(dict_gctx->internal), &our_in, tt);
3074 switch (our_err) {
3075 case FR_DICT_ATTR_OK:
3076 FR_SBUFF_SET_RETURN(in, &our_in);
3077
3079 if (!foreign) goto error;
3080 break;
3081
3082 default:
3083 goto error;
3084 }
3085 }
3086
3087 /*
3088 * Now loop over the protocol dictionaries
3089 */
3091 dict;
3093 if (dict == dict_def) continue;
3094 if (dict == dict_gctx->internal) continue;
3095
3096 (void)func(&our_err, out, fr_dict_root(dict), &our_in, tt);
3097 switch (our_err) {
3098 case FR_DICT_ATTR_OK:
3099 FR_SBUFF_SET_RETURN(in, &our_in);
3100
3102 continue;
3103
3104 default:
3105 break;
3106 }
3107 }
3108
3109error:
3110 /*
3111 * Add a more helpful error message about
3112 * which dictionaries we tried to locate
3113 * the attribute in.
3114 */
3115 if (our_err == FR_DICT_ATTR_NOTFOUND) {
3116 fr_sbuff_marker_t start;
3117 char *list = NULL;
3118
3119#define DICT_NAME_APPEND(_in, _dict) \
3120do { \
3121 char *_n; \
3122 _n = talloc_strdup_append_buffer(_in, fr_dict_root(_dict)->name); \
3123 if (unlikely(!_n)) { \
3124 talloc_free(_in); \
3125 goto done; \
3126 } \
3127 _in = _n; \
3128 _n = talloc_strdup_append_buffer(_in, ", "); \
3129 if (unlikely(!_n)) { \
3130 talloc_free(_in); \
3131 goto done; \
3132 } \
3133 _in = _n; \
3134} while (0)
3135
3136 our_in = FR_SBUFF(in);
3137 fr_sbuff_marker(&start, &our_in);
3138
3139 list = talloc_strdup(NULL, "");
3140 if (unlikely(!list)) goto done;
3141
3142 if (dict_def) DICT_NAME_APPEND(list, dict_def);
3143 if (internal) DICT_NAME_APPEND(list, dict_gctx->internal);
3144
3145 if (foreign) {
3147 dict;
3149 if (dict == dict_def) continue;
3150 if (dict == dict_gctx->internal) continue;
3151
3152 if (internal) DICT_NAME_APPEND(list, dict);
3153 }
3154 }
3155
3156 fr_strerror_printf("Attribute '%pV' not found. Searched in: %pV",
3158 fr_sbuff_adv_until(&our_in, SIZE_MAX, tt, '\0')),
3159 fr_box_strvalue_len(list, talloc_array_length(list) - 3));
3160
3161 talloc_free(list);
3162 }
3163
3164done:
3165 if (err) *err = our_err;
3166 *out = NULL;
3167
3168 FR_SBUFF_ERROR_RETURN(&our_in);
3169}
3170
3171/** Internal function for searching for attributes in multiple dictionaries
3172 *
3173 * Unlike #dict_attr_search this function searches for a protocol name preceding
3174 * the attribute identifier.
3175 */
3176static inline CC_HINT(always_inline)
3178 fr_dict_t const *dict_def,
3179 fr_sbuff_t *in, fr_sbuff_term_t const *tt,
3180 bool internal, bool foreign,
3182{
3183 fr_sbuff_t our_in = FR_SBUFF(in);
3184 fr_dict_attr_err_t our_err;
3185 fr_dict_t *initial;
3186 fr_slen_t slen;
3187
3188 /*
3189 * Check for dictionary prefix
3190 */
3191 slen = dict_by_protocol_substr(&our_err, &initial, &our_in, dict_def);
3192 if (our_err != FR_DICT_ATTR_OK) {
3193 error:
3194 if (err) *err = our_err;
3195 *out = NULL;
3196 FR_SBUFF_ERROR_RETURN(&our_in);
3197 }
3198
3199 /*
3200 * Has dictionary qualifier, can't fallback
3201 */
3202 if (slen > 0) {
3203 /*
3204 * Next thing SHOULD be a '.'
3205 */
3206 if (!fr_sbuff_next_if_char(&our_in, '.')) {
3208 *out = NULL;
3209 FR_SBUFF_ERROR_RETURN(&our_in);
3210 }
3211
3212 internal = foreign = false;
3213 }
3214
3215 if (dict_attr_search(&our_err, out, initial, &our_in, tt, internal, foreign, func) < 0) goto error;
3216 if (err) *err = FR_DICT_ATTR_OK;
3217
3218 FR_SBUFF_SET_RETURN(in, &our_in);
3219}
3220
3221/** Locate a qualified #fr_dict_attr_t by its name and a dictionary qualifier
3222 *
3223 * This function will search through all loaded dictionaries, or a subset of
3224 * loaded dictionaries, for a matching attribute in the top level namespace.
3225 *
3226 * This attribute may be qualified with `<protocol>.` to selection an attribute
3227 * in a specific case.
3228 *
3229 * @note If calling this function from the server any list or request qualifiers
3230 * should be stripped first.
3231 *
3232 * @param[out] err Why parsing failed. May be NULL.
3233 * @see fr_dict_attr_err_t
3234 * @param[out] out Dictionary found attribute.
3235 * @param[in] dict_def Default dictionary for non-qualified dictionaries.
3236 * @param[in] name Dictionary/Attribute name.
3237 * @param[in] tt Terminal strings.
3238 * @param[in] internal If true, fallback to the internal dictionary.
3239 * @param[in] foreign If true, fallback to foreign dictionaries.
3240 * @return
3241 * - < 0 on failure.
3242 * - The number of bytes of name consumed on success.
3243 */
3245 fr_dict_t const *dict_def,
3246 fr_sbuff_t *name, fr_sbuff_term_t const *tt,
3247 bool internal, bool foreign)
3248{
3249 return dict_attr_search_qualified(err, out, dict_def, name, tt,
3250 internal, foreign, fr_dict_attr_by_name_substr);
3251}
3252
3253/** Locate a #fr_dict_attr_t by its name in the top level namespace of a dictionary
3254 *
3255 * This function will search through all loaded dictionaries, or a subset of
3256 * loaded dictionaries, for a matching attribute in the top level namespace.
3257 *
3258 * @note If calling this function from the server any list or request qualifiers
3259 * should be stripped first.
3260 *
3261 * @param[out] err Why parsing failed. May be NULL.
3262 * @see fr_dict_attr_err_t
3263 * @param[out] out Dictionary found attribute.
3264 * @param[in] dict_def Default dictionary for non-qualified dictionaries.
3265 * @param[in] name Dictionary/Attribute name.
3266 * @param[in] tt Terminal strings.
3267 * @param[in] internal If true, fallback to the internal dictionary.
3268 * @param[in] foreign If true, fallback to foreign dictionaries.
3269 * @return
3270 * - < 0 on failure.
3271 * - The number of bytes of name consumed on success.
3272 */
3274 fr_dict_t const *dict_def,
3275 fr_sbuff_t *name, fr_sbuff_term_t const *tt,
3276 bool internal, bool foreign)
3277{
3278 return dict_attr_search_qualified(err, out, dict_def, name, tt,
3279 internal, foreign, fr_dict_attr_by_name_substr);
3280}
3281
3282/** Locate a qualified #fr_dict_attr_t by a dictionary qualified OID string
3283 *
3284 * This function will search through all loaded dictionaries, or a subset of
3285 * loaded dictionaries, for a matching attribute.
3286 *
3287 * @note If calling this function from the server any list or request qualifiers
3288 * should be stripped first.
3289 *
3290 * @note err should be checked to determine if a parse error occurred.
3291 *
3292 * @param[out] err Why parsing failed. May be NULL.
3293 * @see fr_dict_attr_err_t
3294 * @param[out] out Dictionary found attribute.
3295 * @param[in] dict_def Default dictionary for non-qualified dictionaries.
3296 * @param[in] in Dictionary/Attribute name.
3297 * @param[in] tt Terminal strings.
3298 * @param[in] internal If true, fallback to the internal dictionary.
3299 * @param[in] foreign If true, fallback to foreign dictionaries.
3300 * @return The number of bytes of name consumed.
3301 */
3303 fr_dict_t const *dict_def,
3304 fr_sbuff_t *in, fr_sbuff_term_t const *tt,
3305 bool internal, bool foreign)
3306{
3307 return dict_attr_search_qualified(err, out, dict_def, in, tt,
3308 internal, foreign, fr_dict_attr_by_oid_substr);
3309}
3310
3311/** Locate a qualified #fr_dict_attr_t by a dictionary using a non-qualified OID string
3312 *
3313 * This function will search through all loaded dictionaries, or a subset of
3314 * loaded dictionaries, for a matching attribute.
3315 *
3316 * @note If calling this function from the server any list or request qualifiers
3317 * should be stripped first.
3318 *
3319 * @note err should be checked to determine if a parse error occurred.
3320 *
3321 * @param[out] err Why parsing failed. May be NULL.
3322 * @see fr_dict_attr_err_t
3323 * @param[out] out Dictionary found attribute.
3324 * @param[in] dict_def Default dictionary for non-qualified dictionaries.
3325 * @param[in] in Dictionary/Attribute name.
3326 * @param[in] tt Terminal strings.
3327 * @param[in] internal If true, fallback to the internal dictionary.
3328 * @param[in] foreign If true, fallback to foreign dictionaries.
3329 * @return The number of bytes of name consumed.
3330 */
3332 fr_dict_t const *dict_def,
3333 fr_sbuff_t *in, fr_sbuff_term_t const *tt,
3334 bool internal, bool foreign)
3335{
3336 return dict_attr_search_qualified(err, out, dict_def, in, tt,
3337 internal, foreign, fr_dict_attr_by_oid_substr);
3338}
3339
3340/** Locate a qualified #fr_dict_attr_t by its name and a dictionary qualifier
3341 *
3342 * @param[out] err Why parsing failed. May be NULL.
3343 * @see fr_dict_attr_err_t.
3344 * @param[in] dict_def Default dictionary for non-qualified dictionaries.
3345 * @param[in] name Dictionary/Attribute name.
3346 * @param[in] internal If true, fallback to the internal dictionary.
3347 * @param[in] foreign If true, fallback to foreign dictionaries.
3348 * @return an #fr_dict_attr_err_t value.
3349 */
3351 char const *name,
3352 bool internal, bool foreign)
3353{
3354 ssize_t slen;
3355 fr_sbuff_t our_name;
3356 fr_dict_attr_t const *da;
3357 fr_dict_attr_err_t our_err;
3358
3359 fr_sbuff_init_in(&our_name, name, strlen(name));
3360
3361 slen = fr_dict_attr_search_by_qualified_oid_substr(&our_err, &da, dict_def, &our_name, NULL, internal, foreign);
3362 if (our_err != FR_DICT_ATTR_OK) {
3363 if (err) *err = our_err;
3364 return NULL;
3365 }
3366 if ((size_t)slen != fr_sbuff_len(&our_name)) {
3367 fr_strerror_printf("Trailing garbage after attr string \"%s\"", name);
3369 return NULL;
3370 }
3371
3372 return da;
3373}
3374
3375/** Look up a dictionary attribute by a name embedded in another string
3376 *
3377 * Find the first invalid attribute name char in the string pointed
3378 * to by name.
3379 *
3380 * Copy the characters between the start of the name string and the first
3381 * none #fr_dict_attr_allowed_chars char to a buffer and perform a dictionary lookup
3382 * using that value.
3383 *
3384 * If the attribute exists, advance the pointer pointed to by name
3385 * to the first none #fr_dict_attr_allowed_chars char, and return the DA.
3386 *
3387 * If the attribute does not exist, don't advance the pointer and return
3388 * NULL.
3389 *
3390 * @param[out] err Why parsing failed. May be NULL.
3391 * @see fr_dict_attr_err_t
3392 * @param[out] out Where to store the resolve attribute.
3393 * @param[in] parent containing the namespace to search in.
3394 * @param[in] name string start.
3395 * @param[in] tt Terminal sequences to use to determine the portion
3396 * of in to search.
3397 * @return
3398 * - <= 0 on failure.
3399 * - The number of bytes of name consumed on success.
3400 */
3403{
3404 fr_dict_attr_t const *da;
3405 size_t len;
3406 fr_dict_attr_t const *ref;
3407 char const *p;
3408 char buffer[FR_DICT_ATTR_MAX_NAME_LEN + 1 + 1]; /* +1 \0 +1 for "too long" */
3409 fr_sbuff_t our_name = FR_SBUFF(name);
3410 fr_hash_table_t *namespace;
3411
3412 *out = NULL;
3413
3414#ifdef STATIC_ANALYZER
3415 memset(buffer, 0, sizeof(buffer));
3416#endif
3417
3419 &our_name, SIZE_MAX,
3421 if (len == 0) {
3422 fr_strerror_const("Zero length attribute name");
3424 FR_SBUFF_ERROR_RETURN(&our_name);
3425 }
3426 if (len > FR_DICT_ATTR_MAX_NAME_LEN) {
3427 fr_strerror_const("Attribute name too long");
3429 FR_SBUFF_ERROR_RETURN(&our_name);
3430 }
3431
3432 /*
3433 * Do a second pass, ensuring that the name has at least one alphanumeric character.
3434 */
3435 for (p = buffer; p < (buffer + len); p++) {
3436 if (sbuff_char_alpha_num[(uint8_t) *p]) break;
3437 }
3438
3439 if ((size_t) (p - buffer) == len) {
3440 fr_strerror_const("Invalid attribute name");
3442 FR_SBUFF_ERROR_RETURN(&our_name);
3443 }
3444
3445 ref = fr_dict_attr_ref(parent);
3446 if (ref) parent = ref;
3447
3448redo:
3449 namespace = dict_attr_namespace(parent);
3450 if (!namespace) {
3451 fr_strerror_printf("Attribute '%s' does not contain a namespace", parent->name);
3453 fr_sbuff_set_to_start(&our_name);
3454 FR_SBUFF_ERROR_RETURN(&our_name);
3455 }
3456
3457 da = fr_hash_table_find(namespace, &(fr_dict_attr_t){ .name = buffer });
3458 if (!da) {
3459 if (parent->flags.is_root) {
3460 fr_dict_t const *dict = fr_dict_by_da(parent);
3461
3462 if (dict->next) {
3463 parent = dict->next->root;
3464 goto redo;
3465 }
3466 }
3467
3469 fr_strerror_printf("Attribute '%s' not found in namespace '%s'", buffer, parent->name);
3470 fr_sbuff_set_to_start(&our_name);
3471 FR_SBUFF_ERROR_RETURN(&our_name);
3472 }
3473
3474 da = dict_attr_alias(err, da);
3475 if (unlikely(!da)) FR_SBUFF_ERROR_RETURN(&our_name);
3476
3477 *out = da;
3478 if (err) *err = FR_DICT_ATTR_OK;
3479
3480 FR_SBUFF_SET_RETURN(name, &our_name);
3481}
3482
3483/* Internal version of fr_dict_attr_by_name
3484 *
3485 */
3487{
3488 fr_hash_table_t *namespace;
3489 fr_dict_attr_t *da;
3490
3492
3493redo:
3494 namespace = dict_attr_namespace(parent);
3495 if (!namespace) {
3496 fr_strerror_printf("Attribute '%s' does not contain a namespace", parent->name);
3498 return NULL;
3499 }
3500
3501 da = fr_hash_table_find(namespace, &(fr_dict_attr_t) { .name = name });
3502 if (!da) {
3503 if (parent->flags.is_root) {
3504 fr_dict_t const *dict = fr_dict_by_da(parent);
3505
3506 if (dict->next) {
3507 parent = dict->next->root;
3508 goto redo;
3509 }
3510 }
3511
3513 fr_strerror_printf("Attribute '%s' not found in namespace '%s'", name, parent->name);
3514 return NULL;
3515 }
3516
3517 if (err) *err = FR_DICT_ATTR_OK;
3518
3519 return da;
3520}
3521
3522/** Locate a #fr_dict_attr_t by its name
3523 *
3524 * @param[out] err Why the lookup failed. May be NULL.
3525 * @see fr_dict_attr_err_t.
3526 * @param[in] parent containing the namespace we're searching in.
3527 * @param[in] name of the attribute to locate.
3528 * @return
3529 * - Attribute matching name.
3530 * - NULL if no matching attribute could be found.
3531 */
3533{
3534 fr_dict_attr_t const *da;
3535
3537
3539 if (!da) return NULL;
3540
3541 da = dict_attr_alias(err, da);
3542 if (unlikely(!da)) return NULL;
3543
3544 return da;
3545}
3546
3547/** Internal version of fr_dict_attr_child_by_num
3548 *
3549 */
3551{
3552 fr_dict_attr_t const *bin;
3553 fr_dict_attr_t const **children;
3554 fr_dict_attr_t const *ref;
3555
3557
3558 /*
3559 * Do any necessary dereferencing
3560 */
3561 ref = fr_dict_attr_ref(parent);
3562 if (ref) parent = ref;
3563
3564 children = dict_attr_children(parent);
3565 if (!children) return NULL;
3566
3567 /*
3568 * Child arrays may be trimmed back to save memory.
3569 * Check that so we don't SEGV.
3570 */
3571 if ((attr & 0xff) > talloc_array_length(children)) return NULL;
3572
3573 bin = children[attr & 0xff];
3574 for (;;) {
3575 if (!bin) return NULL;
3576 if (bin->attr == attr) {
3578
3579 memcpy(&out, &bin, sizeof(bin));
3580
3581 return out;
3582 }
3583 bin = bin->next;
3584 }
3585
3586 return NULL;
3587}
3588
3589/** Check if a child attribute exists in a parent using an attribute number
3590 *
3591 * @param[in] parent to check for child in.
3592 * @param[in] attr number to look for.
3593 * @return
3594 * - The child attribute on success.
3595 * - NULL if the child attribute does not exist.
3596 */
3598{
3599 fr_dict_attr_t const *da;
3600
3601 da = dict_attr_child_by_num(parent, attr);
3602 if (!da) return NULL;
3603
3604 da = dict_attr_alias(NULL, da);
3605 if (unlikely(!da)) return NULL;
3606
3607 return da;
3608}
3609
3610/** Iterate over all enumeration values for an attribute
3611 *
3612 * @param[in] da to iterate over.
3613 * @param[in] iter to use for iteration.
3614 * @return
3615 * - First #fr_dict_enum_value_t in the attribute.
3616 * - NULL if no enumeration values exist.
3617 */
3619{
3621
3623 if (!ext) {
3624 fr_strerror_printf("%s has no enumeration values to iterate over", da->name);
3625 return NULL;
3626 }
3627
3628 return fr_hash_table_iter_init(ext->value_by_name, iter);
3629}
3630
3631/* Iterate over next enumeration value for an attribute
3632 *
3633 * @param[in] da to iterate over.
3634 * @param[in] iter to use for iteration.
3635 * @return
3636 * - Next #fr_dict_enum_value_t in the attribute.
3637 * - NULL if no more enumeration values exist.
3638 */
3640{
3643 if (!ext) {
3644 fr_strerror_printf("%s has no enumeration values to iterate over", da->name);
3645 return NULL;
3646 }
3647
3648 return fr_hash_table_iter_next(ext->value_by_name, iter);;
3649}
3650
3651/** Lookup the structure representing an enum value in a #fr_dict_attr_t
3652 *
3653 * @param[in] da to search in.
3654 * @param[in] value to search for.
3655 * @return
3656 * - Matching #fr_dict_enum_value_t.
3657 * - NULL if no matching #fr_dict_enum_value_t could be found.
3658 */
3660{
3662
3664 if (!ext) {
3665 fr_strerror_printf("VALUE cannot be defined for %s attributes",
3666 fr_type_to_str(da->type));
3667 return NULL;
3668 }
3669
3670 /*
3671 * No values associated with this attribute
3672 */
3673 if (!ext->name_by_value) return NULL;
3674
3675 /*
3676 * Could be NULL or an unknown attribute, in which case
3677 * we want to avoid the lookup gracefully...
3678 */
3679 if (value->type != da->type) return NULL;
3680
3681 return fr_hash_table_find(ext->name_by_value, &(fr_dict_enum_value_t){ .value = value });
3682}
3683
3684/** Lookup the name of an enum value in a #fr_dict_attr_t
3685 *
3686 * @param[in] da to search in.
3687 * @param[in] value number to search for.
3688 * @return
3689 * - Name of value.
3690 * - NULL if no matching value could be found.
3691 */
3693{
3694 fr_dict_enum_value_t const *dv;
3695
3696 dv = fr_dict_enum_by_value(da, value);
3697 if (!dv) return NULL;
3698
3699 return dv->name;
3700}
3701
3702/*
3703 * Get a value by its name, keyed off of an attribute.
3704 */
3706{
3708
3709 if (!name) return NULL;
3710
3712 if (!ext) {
3713 fr_strerror_printf("VALUE cannot be defined for %s attributes",
3714 fr_type_to_str(da->type));
3715 return NULL;
3716 }
3717
3718 /*
3719 * No values associated with this attribute
3720 */
3721 if (!ext->value_by_name) return NULL;
3722
3723 if (len < 0) len = strlen(name);
3724
3725 return fr_hash_table_find(ext->value_by_name, &(fr_dict_enum_value_t){ .name = name, .name_len = len});
3726}
3727
3728/*
3729 * Get a value by its name, keyed off of an attribute, from an sbuff
3730 */
3732{
3734 fr_sbuff_t our_in = FR_SBUFF(in);
3735 fr_dict_enum_value_t *found = NULL;
3736 size_t found_len = 0;
3737 uint8_t *p;
3739
3740 /*
3741 * No values associated with this attribute, do nothing.
3742 */
3744 if (!ext || !ext->value_by_name) return 0;
3745
3746 /*
3747 * Loop until we exhaust all of the possibilities.
3748 */
3749 for (p = name; (size_t) (p - name) < ext->max_name_len; p++) {
3750 int len = (p - name) + 1;
3751 fr_dict_enum_value_t *enumv;
3752
3753 *p = fr_sbuff_char(&our_in, '\0');
3754 if (!fr_dict_enum_allowed_chars[*p]) {
3755 break;
3756 }
3757 fr_sbuff_next(&our_in);
3758
3759 enumv = fr_hash_table_find(ext->value_by_name, &(fr_dict_enum_value_t){ .name = (char const *) name,
3760 .name_len = len});
3761
3762 /*
3763 * Return the LONGEST match, as there may be
3764 * overlaps. e.g. "Framed", and "Framed-User".
3765 */
3766 if (enumv) {
3767 found = enumv;
3768 found_len = len;
3769 }
3770 }
3771
3772 if (found) {
3773 *out = found;
3774 FR_SBUFF_SET_RETURN(in, found_len);
3775 }
3776
3777 return 0;
3778}
3779
3780/** Extract an enumeration name from a string
3781 *
3782 * This function defines the canonical format for an enumeration name.
3783 *
3784 * An enumeration name is made up of one or more fr_dict_attr_allowed_chars
3785 * with at least one character in the sequence not being a special character
3786 * i.e. [-+/_] or a number.
3787 *
3788 * This disambiguates enumeration identifiers from mathematical expressions.
3789 *
3790 * If we allowed enumeration names consisting of sequences of numbers separated
3791 * by special characters it would not be possible to determine if the special
3792 * character were an operator in a subexpression.
3793 *
3794 * For example take:
3795 *
3796 * &My-Enum-Attr == 01234-5678
3797 *
3798 * Without having access to the enumeration values of My-Enum-Attr (which we
3799 * might not have during tokenisation), we cannot tell if this is:
3800 *
3801 * (&My-Enum-Attr == 01234-5678)
3802 *
3803 * OR
3804 *
3805 * ((&My-Enum-Attr == 01234) - 5678)
3806 *
3807 * If an alpha character occurs anywhere in the string i.e:
3808 *
3809 * (&My-Enum-Attr == 01234-A5678)
3810 *
3811 * we know 01234-A5678 can't be a mathematical sub-expression because the
3812 * second potential operand can no longer be parsed as an integer constant.
3813 *
3814 * @param[out] out The name string we managed to extract.
3815 * May be NULL in which case only the length of the name
3816 * will be returned.
3817 * @param[out] err Type of parsing error which occurred. May be NULL.
3818 * @param[in] in The string containing the enum identifier.
3819 * @param[in] tt If non-null verify that a terminal sequence occurs
3820 * after the enumeration name.
3821 * @return
3822 * - <0 the offset at which the parse error occurred.
3823 * - >1 the number of bytes parsed.
3824 */
3826 fr_sbuff_t *in, fr_sbuff_term_t const *tt)
3827{
3828 fr_sbuff_t our_in = FR_SBUFF(in);
3829 bool seen_alpha = false;
3830
3831 while (fr_sbuff_is_in_charset(&our_in, fr_dict_enum_allowed_chars)) {
3832 if (fr_sbuff_is_alpha(&our_in)) seen_alpha = true;
3833 fr_sbuff_next(&our_in);
3834 }
3835
3836 if (!seen_alpha) {
3837 if (fr_sbuff_used(&our_in) == 0) {
3838 fr_strerror_const("VALUE name is empty");
3840 FR_SBUFF_ERROR_RETURN(&our_in);
3841 }
3842
3843 fr_strerror_const("VALUE name must contain at least one alpha character");
3845 fr_sbuff_set_to_start(&our_in); /* Marker should be at the start of the enum */
3846 FR_SBUFF_ERROR_RETURN(&our_in);
3847 }
3848
3849 /*
3850 * Check that the sequence is correctly terminated
3851 */
3852 if (tt && !fr_sbuff_is_terminal(&our_in, tt)) {
3853 fr_strerror_const("VALUE name has trailing text");
3855 FR_SBUFF_ERROR_RETURN(&our_in);
3856 }
3857
3858 if (out) return fr_sbuff_out_bstrncpy_exact(out, in, fr_sbuff_used(&our_in));
3859
3860 if (err) *err = FR_SBUFF_PARSE_OK;
3861
3862 FR_SBUFF_SET_RETURN(in, &our_in);
3863}
3864
3865int dict_dlopen(fr_dict_t *dict, char const *name)
3866{
3867 char *lib_name;
3868 char *sym_name;
3869 fr_dict_protocol_t *proto;
3870
3871 if (!name) return 0;
3872
3873 lib_name = talloc_typed_asprintf(NULL, "libfreeradius-%s", name);
3874 if (unlikely(lib_name == NULL)) {
3875 oom:
3876 fr_strerror_const("Out of memory");
3877 return -1;
3878 }
3879 talloc_bstr_tolower(lib_name);
3880
3881 dict->dl = dl_by_name(dict_gctx->dict_loader, lib_name, NULL, false);
3882 if (!dict->dl) {
3883 fr_strerror_printf_push("Failed loading dictionary validation library \"%s\"", lib_name);
3884 talloc_free(lib_name);
3885 return -1;
3886 }
3887 talloc_free(lib_name);
3888
3889 /*
3890 * The public symbol that contains per-protocol rules
3891 * and extensions.
3892 *
3893 * It ends up being easier to do this using dlsym to
3894 * resolve the symbol and not use the autoloader
3895 * callbacks as theoretically multiple dictionaries
3896 * could use the same protocol library, and then the
3897 * autoloader callback would only run for the first
3898 * dictionary which loaded the protocol.
3899 */
3900 sym_name = talloc_typed_asprintf(NULL, "libfreeradius_%s_dict_protocol", name);
3901 if (unlikely(sym_name == NULL)) {
3902 talloc_free(lib_name);
3903 goto oom;
3904 }
3905 talloc_bstr_tolower(sym_name);
3906
3907 /*
3908 * De-hyphenate the symbol name
3909 */
3910 {
3911 char *p, *q;
3912
3913 for (p = sym_name, q = p + (talloc_array_length(sym_name) - 1); p < q; p++) *p = *p == '-' ? '_' : *p;
3914 }
3915
3916 proto = dlsym(dict->dl->handle, sym_name);
3917 talloc_free(sym_name);
3918
3919 /*
3920 * Soft failure, not all protocol libraires provide
3921 * custom validation functions or flats.
3922 */
3923 if (!proto) return 0;
3924
3925 /*
3926 * Replace the default protocol with the custom one
3927 * if we have it...
3928 */
3929 dict->proto = proto;
3930
3931 return 0;
3932}
3933
3934/** Find a dependent in the tree of dependents
3935 *
3936 */
3937static int8_t _dict_dependent_cmp(void const *a, void const *b)
3938{
3939 fr_dict_dependent_t const *dep_a = a;
3940 fr_dict_dependent_t const *dep_b = b;
3941 int ret;
3942
3943 ret = strcmp(dep_a->dependent, dep_b->dependent);
3944 return CMP(ret, 0);
3945}
3946
3947/** Record a new dependency on a dictionary
3948 *
3949 * These are used to determine what is currently depending on a dictionary.
3950 *
3951 * @param[in] dict to record dependency on.
3952 * @param[in] dependent Either C src file, or another dictionary.
3953 * @return
3954 * - 0 on success.
3955 * - -1 on failure.
3956 */
3957int dict_dependent_add(fr_dict_t *dict, char const *dependent)
3958{
3959 fr_dict_dependent_t *found;
3960
3961 found = fr_rb_find(dict->dependents, &(fr_dict_dependent_t){ .dependent = dependent } );
3962 if (!found) {
3964
3965 new = talloc_zero(dict->dependents, fr_dict_dependent_t);
3966 if (unlikely(!new)) return -1;
3967
3968 /*
3969 * If the dependent is in a module that gets
3970 * unloaded, any strings in the text area also
3971 * get unloaded (including dependent locations).
3972 *
3973 * Strdup the string here so we don't get
3974 * random segfaults if a module forgets to unload
3975 * a dictionary.
3976 */
3977 new->dependent = talloc_typed_strdup(new, dependent);
3978 fr_rb_insert(dict->dependents, new);
3979
3980 new->count = 1;
3981
3982 return 0;
3983 }
3984
3985 found->count++; /* Increase ref count */
3986
3987 return 0;
3988}
3989
3990/** Manually increase the reference count for a dictionary
3991 *
3992 * This is useful if a previously loaded dictionary needs to
3993 * be bound to the lifetime of an additional object.
3994 *
3995 * @param[in] dict to increase the reference count for.
3996 * @param[in] dependent requesting the loading of the dictionary.
3997 * @return
3998 * - 0 on success.
3999 * - -1 on error.
4000 */
4001int fr_dict_dependent_add(fr_dict_t const *dict, char const *dependent)
4002{
4003 fr_dict_t *m_dict = fr_dict_unconst(dict);
4004
4005 if (unlikely(!m_dict)) return -1;
4006
4007 return dict_dependent_add(m_dict, dependent);
4008}
4009
4010/** Decrement ref count for a dependent in a dictionary
4011 *
4012 * @param[in] dict to remove dependency from.
4013 * @param[in] dependent Either C src, or another dictionary dependent.
4014 * What depends on this dictionary.
4015 */
4016int dict_dependent_remove(fr_dict_t *dict, char const *dependent)
4017{
4018 fr_dict_dependent_t *found;
4019
4020 found = fr_rb_find(dict->dependents, &(fr_dict_dependent_t){ .dependent = dependent } );
4021 if (!found) {
4022 fr_strerror_printf("Dependent \"%s\" not found in dictionary \"%s\"", dependent, dict->root->name);
4023 return -1;
4024 }
4025
4026 if (found->count == 0) {
4027 fr_strerror_printf("Zero ref count invalid for dependent \"%s\", dictionary \"%s\"",
4028 dependent, dict->root->name);
4029 return -1;
4030 }
4031
4032 if (--found->count == 0) {
4033 fr_rb_delete(dict->dependents, found);
4034 talloc_free(found);
4035 return 0;
4036 }
4037
4038 return 1;
4039}
4040
4041/** Check if a dictionary still has dependents
4042 *
4043 * @param[in] dict to check
4044 * @return
4045 * - true if there's still at least one dependent.
4046 * - false if there are no dependents.
4047 */
4049{
4050 return (fr_rb_num_elements(dict->dependents) > 0);
4051}
4052
4053#ifndef NDEBUG
4054static void dependent_debug(fr_dict_t *dict)
4055{
4058
4059 if (!dict_has_dependents(dict)) return;
4060
4061 fprintf(stderr, "DEPENDENTS FOR %s\n", dict->root->name);
4062
4063 for (dep = fr_rb_iter_init_inorder(dict->dependents, &iter);
4064 dep;
4065 dep = fr_rb_iter_next_inorder(dict->dependents, &iter)) {
4066 fprintf(stderr, "\t<- %s (%d)\n", dep->dependent, dep->count);
4067 }
4068}
4069#endif
4070
4071
4073{
4074 fr_dict_t **refd_list;
4075 unsigned int i;
4076
4077 if (!dict->autoref) return 0;
4078
4079 if (fr_hash_table_flatten(dict->autoref, (void ***)&refd_list, dict->autoref) < 0) {
4080 fr_strerror_const("failed flattening autoref hash table");
4081 return -1;
4082 }
4083
4084 /*
4085 * Free the dictionary. It will call proto->free() if there's nothing more to do.
4086 */
4087 for (i = 0; i < talloc_array_length(refd_list); i++) {
4088 if (fr_dict_free(&refd_list[i], dict->root->name) < 0) {
4089 fr_strerror_printf("failed freeing autoloaded protocol %s", refd_list[i]->root->name);
4090 return -1;
4091 }
4092 }
4093
4094 TALLOC_FREE(dict->autoref);
4095
4096 return 0;
4097}
4098
4099static int _dict_free(fr_dict_t *dict)
4100{
4101 /*
4102 * We don't necessarily control the order of freeing
4103 * children.
4104 */
4105 if (dict != dict->gctx->internal) {
4106 fr_dict_attr_t const *da;
4107
4108 if (dict->gctx->attr_protocol_encapsulation && dict->root) {
4109 da = fr_dict_attr_child_by_num(dict->gctx->attr_protocol_encapsulation, dict->root->attr);
4110 if (da && fr_dict_attr_ref(da)) dict_attr_ref_null(da);
4111 }
4112 }
4113
4114#ifdef STATIC_ANALYZER
4115 if (!dict->root) {
4116 fr_strerror_const("dict root is missing");
4117 return -1;
4118 }
4119#endif
4120
4121 /*
4122 * If we called init(), then call free()
4123 */
4124 if (dict->proto && dict->proto->free) {
4125 dict->proto->free();
4126 }
4127
4128 if (!fr_cond_assert(!dict->in_protocol_by_name || fr_hash_table_delete(dict->gctx->protocol_by_name, dict))) {
4129 fr_strerror_printf("Failed removing dictionary from protocol hash \"%s\"", dict->root->name);
4130 return -1;
4131 }
4132 dict->in_protocol_by_name = false;
4133
4134 if (!fr_cond_assert(!dict->in_protocol_by_num || fr_hash_table_delete(dict->gctx->protocol_by_num, dict))) {
4135 fr_strerror_printf("Failed removing dictionary from protocol number_hash \"%s\"", dict->root->name);
4136 return -1;
4137 }
4138 dict->in_protocol_by_num = false;
4139
4140 if (dict_has_dependents(dict)) {
4143
4144 fr_strerror_printf("Refusing to free dictionary \"%s\", still has dependents", dict->root->name);
4145
4146 for (dep = fr_rb_iter_init_inorder(dict->dependents, &iter);
4147 dep;
4148 dep = fr_rb_iter_next_inorder(dict->dependents, &iter)) {
4149 fr_strerror_printf_push("%s (%d)", dep->dependent, dep->count);
4150 }
4151
4152 return -1;
4153 }
4154
4155 /*
4156 * Free the hash tables with free functions first
4157 * so that the things the hash tables reference
4158 * are still there.
4159 */
4160 talloc_free(dict->vendors_by_name);
4161
4162 /*
4163 * Decrease the reference count on the validation
4164 * library we loaded.
4165 */
4166 dl_free(dict->dl);
4167
4168 if (dict == dict->gctx->internal) {
4169 dict->gctx->internal = NULL;
4170 dict->gctx->attr_protocol_encapsulation = NULL;
4171 }
4172
4173 return 0;
4174}
4175
4176/** Allocate a new dictionary
4177 *
4178 * @param[in] ctx to allocate dictionary in.
4179 * @return
4180 * - NULL on memory allocation error.
4181 */
4182fr_dict_t *dict_alloc(TALLOC_CTX *ctx)
4183{
4184 fr_dict_t *dict;
4185
4186 if (!dict_gctx) {
4187 fr_strerror_const("Initialise global dictionary ctx with fr_dict_global_ctx_init()");
4188 return NULL;
4189 }
4190
4191 dict = talloc_zero(ctx, fr_dict_t);
4192 if (!dict) {
4193 fr_strerror_const("Failed allocating memory for dictionary");
4194 error:
4195 talloc_free(dict);
4196 return NULL;
4197 }
4198 dict->gctx = dict_gctx; /* Record which global context this was allocated in */
4199 talloc_set_destructor(dict, _dict_free);
4200
4201 /*
4202 * A list of all the files that constitute this dictionary
4203 */
4204 fr_dlist_talloc_init(&dict->filenames, fr_dict_filename_t, entry);
4205
4206 /*
4207 * Pre-Allocate pool memory for rapid startup
4208 * As that's the working memory required during
4209 * dictionary initialisation.
4210 */
4211 dict->pool = talloc_pool(dict, DICT_POOL_SIZE);
4212 if (!dict->pool) {
4213 fr_strerror_const("Failed allocating talloc pool for dictionary");
4214 goto error;
4215 }
4216
4217 /*
4218 * Create the table of vendor by name. There MAY NOT
4219 * be multiple vendors of the same name.
4220 */
4222 if (!dict->vendors_by_name) {
4223 fr_strerror_printf("Failed allocating \"vendors_by_name\" table");
4224 goto error;
4225 }
4226 /*
4227 * Create the table of vendors by value. There MAY
4228 * be vendors of the same value. If there are, we
4229 * pick the latest one.
4230 */
4231 dict->vendors_by_num = fr_hash_table_alloc(dict, dict_vendor_pen_hash, dict_vendor_pen_cmp, NULL);
4232 if (!dict->vendors_by_num) {
4233 fr_strerror_printf("Failed allocating \"vendors_by_num\" table");
4234 goto error;
4235 }
4236
4237 /*
4238 * Inter-dictionary reference caching
4239 */
4241 if (!dict->autoref) {
4242 fr_strerror_printf("Failed allocating \"autoref\" table");
4243 goto error;
4244 }
4245
4246 /*
4247 * Who/what depends on this dictionary
4248 */
4249 dict->dependents = fr_rb_inline_alloc(dict, fr_dict_dependent_t, node, _dict_dependent_cmp, NULL);
4250
4251 /*
4252 * Set the default dictionary protocol, this can
4253 * be overriden by the protocol library.
4254 */
4255 dict->proto = &dict_proto_default;
4256
4257 return dict;
4258}
4259
4260/** Allocate a new local dictionary
4261 *
4262 * @param[in] parent parent dictionary and talloc ctx
4263 * @return
4264 * - NULL on memory allocation error.
4265 *
4266 * This dictionary cannot define vendors, or inter-dictionary
4267 * dependencies. However, we initialize the relevant fields just in
4268 * case. We should arguably just skip initializing those fields, and
4269 * just allow the server to crash if programmers do something stupid with it.
4270 */
4272{
4273 fr_dict_t *dict;
4274 fr_dict_attr_t *da;
4275
4276 fr_dict_attr_flags_t flags = {
4277 .is_root = true,
4278 .local = true,
4279 .internal = true,
4280 .type_size = parent->root->flags.type_size,
4281 .length = parent->root->flags.length,
4282 };
4283
4284 dict = dict_alloc(UNCONST(fr_dict_t *, parent));
4285 if (!dict) return NULL;
4286
4287 /*
4288 * Allocate the root attribute. This dictionary is
4289 * always protocol "local", and number "0".
4290 */
4291 da = dict_attr_alloc_root(dict->pool, parent, "local", 0,
4292 &(dict_attr_args_t){ .flags = &flags });
4293 if (unlikely(!da)) {
4294 talloc_free(dict);
4295 return NULL;
4296 }
4297
4298 da->last_child_attr = fr_dict_root(parent)->last_child_attr;
4299
4300 dict->root = da;
4301 dict->root->dict = dict;
4302 dict->next = parent;
4303
4304 DA_VERIFY(dict->root);
4305
4306 return dict;
4307}
4308
4309/** Decrement the reference count on a previously loaded dictionary
4310 *
4311 * @param[in] dict to free.
4312 * @param[in] dependent that originally allocated this dictionary.
4313 * @return
4314 * - 0 on success (dictionary freed).
4315 * - 1 if other things still depend on the dictionary.
4316 * - -1 on error (dependent doesn't exist)
4317 */
4318int fr_dict_const_free(fr_dict_t const **dict, char const *dependent)
4319{
4320 fr_dict_t **our_dict = UNCONST(fr_dict_t **, dict);
4321
4322 return fr_dict_free(our_dict, dependent);
4323}
4324
4325/** Decrement the reference count on a previously loaded dictionary
4326 *
4327 * @param[in] dict to free.
4328 * @param[in] dependent that originally allocated this dictionary.
4329 * @return
4330 * - 0 on success (dictionary freed).
4331 * - 1 if other things still depend on the dictionary.
4332 * - -1 on error (dependent doesn't exist)
4333 */
4334int fr_dict_free(fr_dict_t **dict, char const *dependent)
4335{
4336 if (!*dict) return 0;
4337
4338 switch (dict_dependent_remove(*dict, dependent)) {
4339 case 0: /* dependent has no more refs */
4340 if (!dict_has_dependents(*dict)) {
4341 talloc_free(*dict);
4342 return 0;
4343 }
4345
4346 case 1: /* dependent has more refs */
4347 return 1;
4348
4349 default: /* error */
4350 return -1;
4351 }
4352}
4353
4354/** Process a dict_attr_autoload element to load/verify a dictionary attribute
4355 *
4356 * @param[in] to_load attribute definition
4357 * @return
4358 * - 0 on success.
4359 * - -1 on failure.
4360 */
4362{
4363 fr_dict_enum_autoload_t const *p = to_load;
4364 fr_dict_enum_value_t const *enumv;
4365
4366 for (p = to_load; p->out; p++) {
4367 if (unlikely(!p->attr)) {
4368 fr_strerror_printf("Invalid attribute autoload entry for \"%s\", missing attribute pointer", p->name);
4369 return -1;
4370 }
4371
4372 if (unlikely(!*p->attr)) {
4373 fr_strerror_printf("Can't resolve value \"%s\", attribute not loaded", p->name);
4374 fr_strerror_printf_push("Check fr_dict_attr_autoload_t struct has "
4375 "an entry to load the attribute \"%s\" is located in, and that "
4376 "the fr_dict_autoload_attr_t symbol name is correct", p->name);
4377 return -1;
4378 }
4379
4380 enumv = fr_dict_enum_by_name(*(p->attr), p->name, -1);
4381 if (!enumv) {
4382 fr_strerror_printf("Value '%s' not found in \"%s\" attribute",
4383 p->name, (*(p->attr))->name);
4384 return -1;
4385 }
4386
4387 if (p->out) *(p->out) = enumv->value;
4388 }
4389
4390 return 0;
4391}
4392
4393/** Process a dict_attr_autoload element to load/verify a dictionary attribute
4394 *
4395 * @param[in] to_load attribute definition
4396 * @return
4397 * - 0 on success.
4398 * - -1 on failure.
4399 */
4401{
4402 fr_dict_attr_t const *da;
4403 fr_dict_attr_autoload_t const *p = to_load;
4404 fr_dict_attr_t const *root = NULL;
4405
4406 for (p = to_load; p->out; p++) {
4407 if (!p->dict) {
4408 fr_strerror_printf("Invalid attribute autoload entry for \"%s\", missing dictionary pointer", p->name);
4409 return -1;
4410 }
4411
4412 if (!*p->dict) {
4413 fr_strerror_printf("Autoloader autoloader can't resolve attribute \"%s\", dictionary not loaded", p->name);
4414 fr_strerror_printf_push("Check fr_dict_autoload_t struct has "
4415 "an entry to load the dictionary \"%s\" is located in, and that "
4416 "the fr_dict_autoload_t symbol name is correct", p->name);
4417 return -1;
4418 }
4419
4420 if (!root || (root->dict != *p->dict) || (p->name[0] != '.')) {
4421 root = (*p->dict)->root;
4422 }
4423
4424 if (p->name[0] == '.') {
4425 da = fr_dict_attr_by_oid(NULL, root, p->name + 1);
4426 if (!da) {
4427 fr_strerror_printf("Autoloader attribute \"%s\" not found in \"%s\" dictionary under attribute %s", p->name,
4428 *p->dict ? (*p->dict)->root->name : "internal", root->name);
4429 return -1;
4430 }
4431 } else {
4432 da = fr_dict_attr_by_oid(NULL, fr_dict_root(*p->dict), p->name);
4433 if (!da) {
4434 fr_strerror_printf("Autoloader attribute \"%s\" not found in \"%s\" dictionary", p->name,
4435 *p->dict ? (*p->dict)->root->name : "internal");
4436 return -1;
4437 }
4438
4439 if (fr_type_is_structural(da->type)) root = da;
4440 }
4441
4442 if (da->type != p->type) {
4443 fr_strerror_printf("Autoloader attribute \"%s\" should be type %s, but defined as type %s", da->name,
4444 fr_type_to_str(p->type),
4445 fr_type_to_str(da->type));
4446 return -1;
4447 }
4448
4449 DA_VERIFY(da);
4450
4451 if (p->out) *(p->out) = da;
4452 }
4453
4454 return 0;
4455}
4456
4457/** Process a dict_autoload element to load a protocol
4458 *
4459 * @param[in] to_load dictionary definition.
4460 * @param[in] dependent that is loading this dictionary.
4461 * @return
4462 * - 0 on success.
4463 * - -1 on failure.
4464 */
4465int _fr_dict_autoload(fr_dict_autoload_t const *to_load, char const *dependent)
4466{
4467 fr_dict_autoload_t const *p;
4468
4469 for (p = to_load; p->out; p++) {
4470 fr_dict_t *dict = NULL;
4471
4472 if (unlikely(!p->proto)) {
4473 fr_strerror_const("autoload missing parameter proto");
4474 return -1;
4475 }
4476
4477 /*
4478 * Load the internal dictionary
4479 */
4480 if (strcmp(p->proto, "freeradius") == 0) {
4481 if (fr_dict_internal_afrom_file(&dict, p->proto, dependent) < 0) return -1;
4482 } else {
4483 if (fr_dict_protocol_afrom_file(&dict, p->proto, p->base_dir, dependent) < 0) return -1;
4484 }
4485
4486 *(p->out) = dict;
4487 }
4488
4489 return 0;
4490}
4491
4492
4493/** Decrement the reference count on a previously loaded dictionary
4494 *
4495 * @param[in] to_free previously loaded dictionary to free.
4496 * @param[in] dependent that originally allocated this dictionary
4497 */
4498int _fr_dict_autofree(fr_dict_autoload_t const *to_free, char const *dependent)
4499{
4500 fr_dict_autoload_t const *p;
4501
4502 for (p = to_free; p->out; p++) {
4503 int ret;
4504
4505 if (!*p->out) continue;
4506 ret = fr_dict_const_free(p->out, dependent);
4507
4508 if (ret == 0) *p->out = NULL;
4509 if (ret < 0) return -1;
4510 }
4511
4512 return 0;
4513}
4514
4515/** Structure used to managed the lifetime of a dictionary
4516 *
4517 * This should only be used when dictionaries are being dynamically loaded during
4518 * compilation. It should not be used to load dictionaries at runtime, or if
4519 * modules need to load dictionaries (use static fr_dict_autoload_t defs).
4520
4521 */
4523 fr_dict_autoload_t load[2]; //!< Autoloader def.
4524 char const *dependent; //!< Dependent that loaded the dictionary.
4525};
4526
4527/** Talloc destructor to automatically free dictionaries
4528 *
4529 * @param[in] to_free dictionary autoloader definition describing the dictionary to free.
4530 */
4532{
4533 return _fr_dict_autofree(to_free->load, to_free->dependent);
4534}
4535
4536/** Autoload a dictionary and bind the lifetime to a talloc chunk
4537 *
4538 * Mainly useful for resolving "forward" references from unlang immediately.
4539 *
4540 * @note If the talloc chunk is freed it does not mean the dictionary will
4541 * be immediately freed. It will be freed when all other references
4542 * to the dictionary are gone.
4543 *
4544 * @param[in] ctx to bind the dictionary lifetime to.
4545 * @param[out] out pointer to the loaded dictionary.
4546 * @param[in] proto to load.
4547 * @param[in] dependent to register this reference to. Will be dupd.
4548 */
4549fr_dict_autoload_talloc_t *_fr_dict_autoload_talloc(TALLOC_CTX *ctx, fr_dict_t const **out, char const *proto, char const *dependent)
4550{
4551 fr_dict_autoload_talloc_t *dict_ref;
4552 int ret;
4553
4554 dict_ref = talloc(ctx, fr_dict_autoload_talloc_t);
4555 if (unlikely(dict_ref == NULL)) {
4556 oom:
4557 fr_strerror_const("Out of memory");
4558 return NULL;
4559 }
4560
4561 dict_ref->load[0] = (fr_dict_autoload_t){ .proto = proto, .out = out};
4563 dict_ref->dependent = talloc_strdup(dict_ref, dependent);
4564 if (unlikely(dict_ref->dependent == NULL)) {
4565 talloc_free(dict_ref);
4566 goto oom;
4567 }
4568
4569 ret = _fr_dict_autoload(dict_ref->load, dependent);
4570 if (ret < 0) {
4571 talloc_free(dict_ref);
4572 return NULL;
4573 }
4574
4575 return dict_ref;
4576}
4577
4578/** Callback to automatically resolve enum values
4579 *
4580 * @param[in] module being loaded.
4581 * @param[in] symbol An array of fr_dict_enum_autoload_t to load.
4582 * @param[in] user_ctx unused.
4583 * @return
4584 * - 0 on success.
4585 * - -1 on failure.
4586 */
4587int fr_dl_dict_enum_autoload(UNUSED dl_t const *module, void *symbol, UNUSED void *user_ctx)
4588{
4589 if (fr_dict_enum_autoload((fr_dict_enum_autoload_t *)symbol) < 0) return -1;
4590
4591 return 0;
4592}
4593
4594/** Callback to automatically resolve attributes and check the types are correct
4595 *
4596 * @param[in] module being loaded.
4597 * @param[in] symbol An array of fr_dict_attr_autoload_t to load.
4598 * @param[in] user_ctx unused.
4599 * @return
4600 * - 0 on success.
4601 * - -1 on failure.
4602 */
4603int fr_dl_dict_attr_autoload(UNUSED dl_t const *module, void *symbol, UNUSED void *user_ctx)
4604{
4605 if (fr_dict_attr_autoload((fr_dict_attr_autoload_t *)symbol) < 0) return -1;
4606
4607 return 0;
4608}
4609
4610/** Callback to automatically load dictionaries required by modules
4611 *
4612 * @param[in] module being loaded.
4613 * @param[in] symbol An array of fr_dict_autoload_t to load.
4614 * @param[in] user_ctx unused.
4615 * @return
4616 * - 0 on success.
4617 * - -1 on failure.
4618 */
4619int fr_dl_dict_autoload(UNUSED dl_t const *module, void *symbol, UNUSED void *user_ctx)
4620{
4621 if (fr_dict_autoload((fr_dict_autoload_t const *)symbol) < 0) return -1;
4622
4623 return 0;
4624}
4625
4626/** Callback to automatically free a dictionary when the module is unloaded
4627 *
4628 * @param[in] module being loaded.
4629 * @param[in] symbol An array of fr_dict_autoload_t to load.
4630 * @param[in] user_ctx unused.
4631 */
4632void fr_dl_dict_autofree(UNUSED dl_t const *module, void *symbol, UNUSED void *user_ctx)
4633{
4635}
4636
4637static int _dict_global_free_at_exit(void *uctx)
4638{
4639 return talloc_free(uctx);
4640}
4641
4643{
4644 fr_hash_iter_t iter;
4645 fr_dict_t *dict;
4646 bool still_loaded = false;
4647
4648 /*
4649 * Make sure this doesn't fire later and mess
4650 * things up...
4651 */
4653
4654 /*
4655 * Free up autorefs first, which will free up inter-dictionary dependencies.
4656 */
4657 for (dict = fr_hash_table_iter_init(gctx->protocol_by_name, &iter);
4658 dict;
4659 dict = fr_hash_table_iter_next(gctx->protocol_by_name, &iter)) {
4660 (void)talloc_get_type_abort(dict, fr_dict_t);
4661
4662 if (dict_autoref_free(dict) < 0) return -1;
4663 }
4664
4665 for (dict = fr_hash_table_iter_init(gctx->protocol_by_name, &iter);
4666 dict;
4667 dict = fr_hash_table_iter_next(gctx->protocol_by_name, &iter)) {
4668 (void)talloc_get_type_abort(dict, fr_dict_t);
4669 dict_dependent_remove(dict, "global"); /* remove our dependency */
4670
4671 if (talloc_free(dict) < 0) {
4672#ifndef NDEBUG
4673 FR_FAULT_LOG("gctx failed to free dictionary %s - %s", dict->root->name, fr_strerror());
4674#endif
4675 still_loaded = true;
4676 }
4677 }
4678
4679 /*
4680 * Free the internal dictionary as the last step, after all of the protocol dictionaries and
4681 * libraries have freed their references to it.
4682 */
4683 if (gctx->internal) {
4684 dict_dependent_remove(gctx->internal, "global"); /* remove our dependency */
4685
4686 if (talloc_free(gctx->internal) < 0) still_loaded = true;
4687 }
4688
4689 if (still_loaded) {
4690#ifndef NDEBUG
4691 fr_dict_gctx_debug(stderr, gctx);
4692#endif
4693 return -1;
4694 }
4695
4696 /*
4697 * Set this to NULL just in case the caller tries to use
4698 * dict_global_init() again.
4699 */
4700 if (gctx == dict_gctx) dict_gctx = NULL; /* In case the active context isn't this one */
4701
4702 return 0;
4703}
4704
4705/** Initialise the global protocol hashes
4706 *
4707 * @note Must be called before any other dictionary functions.
4708 *
4709 * @param[in] ctx to allocate global resources in.
4710 * @param[in] free_at_exit Install an at_exit handler to free the global ctx.
4711 * This is useful when dictionaries are held by other
4712 * libraries which free them using atexit handlers.
4713 * @param[in] dict_dir the default location for the dictionaries.
4714 * @return
4715 * - A pointer to the new global context on success.
4716 * - NULL on failure.
4717 */
4718fr_dict_gctx_t *fr_dict_global_ctx_init(TALLOC_CTX *ctx, bool free_at_exit, char const *dict_dir)
4719{
4720 fr_dict_gctx_t *new_ctx;
4721
4722 if (!dict_dir) {
4723 fr_strerror_const("No dictionary location provided");
4724 return NULL;
4725 }
4726
4727 new_ctx = talloc_zero(ctx, fr_dict_gctx_t);
4728 if (!new_ctx) {
4729 fr_strerror_const("Out of Memory");
4730 return NULL;
4731 }
4732 new_ctx->perm_check = true; /* Check file permissions by default */
4733
4735 if (!new_ctx->protocol_by_name) {
4736 fr_strerror_const("Failed initializing protocol_by_name hash");
4737 error:
4738 talloc_free(new_ctx);
4739 return NULL;
4740 }
4741
4743 if (!new_ctx->protocol_by_num) {
4744 fr_strerror_const("Failed initializing protocol_by_num hash");
4745 goto error;
4746 }
4747
4748 new_ctx->dict_dir_default = talloc_strdup(new_ctx, dict_dir);
4749 if (!new_ctx->dict_dir_default) goto error;
4750
4751 new_ctx->dict_loader = dl_loader_init(new_ctx, NULL, false, false);
4752 if (!new_ctx->dict_loader) goto error;
4753
4754 new_ctx->free_at_exit = free_at_exit;
4755
4756 talloc_set_destructor(new_ctx, _dict_global_free);
4757
4758 if (!dict_gctx) dict_gctx = new_ctx; /* Set as the default */
4759
4760 if (free_at_exit) fr_atexit_global(_dict_global_free_at_exit, new_ctx);
4761
4762 return new_ctx;
4763}
4764
4765/** Set whether we check dictionary file permissions
4766 *
4767 * @param[in] gctx to alter.
4768 * @param[in] enable Whether we should check file permissions as they're loaded.
4769 */
4771{
4772 gctx->perm_check = enable;
4773}
4774
4775/** Set a new, active, global dictionary context
4776 *
4777 * @param[in] gctx To set.
4778 */
4780{
4781 memcpy(&dict_gctx, &gctx, sizeof(dict_gctx));
4782}
4783
4784/** Explicitly free all data associated with a global dictionary context
4785 *
4786 * @note You should *NOT* ignore the return code of this function.
4787 * You should use perror() or PERROR() to print out the reason
4788 * why freeing failed.
4789 *
4790 * @param[in] gctx To set.
4791 * @return
4792 * - 0 on success.
4793 * - -1 on failure.
4794 */
4796{
4797 if (dict_gctx == gctx) dict_gctx = NULL;
4798
4799 return talloc_const_free(gctx);
4800}
4801
4802/** Allow the default dict dir to be changed after initialisation
4803 *
4804 * @param[in] dict_dir New default dict dir to use.
4805 * @return
4806 * - 0 on success.
4807 * - -1 on failure.
4808 */
4809int fr_dict_global_ctx_dir_set(char const *dict_dir)
4810{
4811 if (!dict_gctx) return -1;
4812
4813 talloc_free(dict_gctx->dict_dir_default); /* Free previous value */
4814 dict_gctx->dict_dir_default = talloc_strdup(dict_gctx, dict_dir);
4815 if (!dict_gctx->dict_dir_default) return -1;
4816
4817 return 0;
4818}
4819
4820char const *fr_dict_global_ctx_dir(void)
4821{
4823}
4824
4825/** Mark all dictionaries and the global dictionary ctx as read only
4826 *
4827 * Any attempts to add new attributes will now fail.
4828 */
4830{
4831 fr_hash_iter_t iter;
4832 fr_dict_t *dict;
4833
4834 if (!dict_gctx) return;
4835
4836 /*
4837 * Set everything to read only
4838 */
4840 dict;
4843 dict->read_only = true;
4844 }
4845
4846 dict = dict_gctx->internal;
4848 dict->read_only = true;
4849 dict_gctx->read_only = true;
4850}
4851
4852/** Dump information about currently loaded dictionaries
4853 *
4854 * Intended to be called from a debugger
4855 */
4856void fr_dict_gctx_debug(FILE *fp, fr_dict_gctx_t const *gctx)
4857{
4858 fr_hash_iter_t dict_iter;
4859 fr_dict_t *dict;
4860 fr_rb_iter_inorder_t dep_iter;
4862
4863 if (gctx == NULL) gctx = dict_gctx;
4864
4865 if (!gctx) {
4866 fprintf(fp, "gctx not initialised\n");
4867 return;
4868 }
4869
4870 fprintf(fp, "gctx %p report\n", dict_gctx);
4871 for (dict = fr_hash_table_iter_init(gctx->protocol_by_num, &dict_iter);
4872 dict;
4873 dict = fr_hash_table_iter_next(gctx->protocol_by_num, &dict_iter)) {
4874 for (dep = fr_rb_iter_init_inorder(dict->dependents, &dep_iter);
4875 dep;
4876 dep = fr_rb_iter_next_inorder(dict->dependents, &dep_iter)) {
4877 fprintf(fp, "\t%s is referenced from %s count (%d)\n",
4878 dict->root->name, dep->dependent, dep->count);
4879 }
4880 }
4881
4882 if (gctx->internal) {
4883 for (dep = fr_rb_iter_init_inorder(gctx->internal->dependents, &dep_iter);
4884 dep;
4885 dep = fr_rb_iter_next_inorder(gctx->internal->dependents, &dep_iter)) {
4886 fprintf(fp, "\t%s is referenced from %s count (%d)\n",
4887 gctx->internal->root->name, dep->dependent, dep->count);
4888 }
4889 }
4890}
4891
4892/** Iterate protocols by name
4893 *
4894 */
4901
4908
4909
4910/** Coerce to non-const
4911 *
4912 */
4914{
4915 if (unlikely(dict->read_only)) {
4916 fr_strerror_printf("%s dictionary has been marked as read only", fr_dict_root(dict)->name);
4917 return NULL;
4918 }
4919 return UNCONST(fr_dict_t *, dict);
4920}
4921
4922/** Coerce to non-const
4923 *
4924 */
4926{
4927 fr_dict_t *dict;
4928
4929 dict = dict_by_da(da);
4930 if (unlikely(dict->read_only)) {
4931 fr_strerror_printf("%s dictionary has been marked as read only", fr_dict_root(dict)->name);
4932 return NULL;
4933 }
4934
4935 return UNCONST(fr_dict_attr_t *, da);
4936}
4937
4939{
4940 if (!dict_gctx) return NULL;
4941
4942 return dict_gctx->internal;
4943}
4944
4945/*
4946 * Check for the allowed characters.
4947 */
4949{
4950 char const *p = name, *end;
4951 bool unknown = false;
4952 bool alnum = false;
4953
4954 if (len < 0) len = strlen(name);
4955
4956 if (len > FR_DICT_ATTR_MAX_NAME_LEN) {
4957 fr_strerror_const("Attribute name is too long");
4958 return -1;
4959 }
4960
4961 end = p + len;
4962
4963 /*
4964 * Unknown attributes can have '.' in their name.
4965 */
4966 if ((len > 5) && (memcmp(name, "Attr-", 5) == 0)) unknown = true;
4967
4968 while (p < end) {
4969 if ((*p == '.') && unknown) p++;
4970
4972 fr_strerror_printf("Invalid character '%pV' in attribute name \"%pV\"",
4974
4975 return -(p - name);
4976 }
4977
4978 alnum |= sbuff_char_alpha_num[(uint8_t)*p];
4979
4980 p++;
4981 }
4982
4983 if (!alnum) {
4984 fr_strerror_const("Invalid attribute name");
4985 return -1;
4986 }
4987
4988 return len;
4989}
4990
4992{
4993 char const *p = name, *end;
4994 bool alnum = false;
4995
4996 if (len < 0) len = strlen(name);
4997 end = p + len;
4998
4999 do {
5000 if (!fr_dict_attr_allowed_chars[(uint8_t)*p] && (*p != '.')) {
5001 fr_strerror_printf("Invalid character '%pV' in oid string \"%pV\"",
5003
5004 return -(p - name);
5005 }
5006
5007 alnum |= sbuff_char_alpha_num[(uint8_t)*p];
5008 p++;
5009 } while (p < end);
5010
5011 if (!alnum) return 0;
5012
5013 return len;
5014}
5015
5016/** Iterate over children of a DA.
5017 *
5018 * @param[in] parent the parent da to iterate over
5019 * @param[in,out] prev pointer to NULL to start, otherwise pointer to the previously returned child
5020 * @return
5021 * - NULL for end of iteration
5022 * - !NULL for a valid child. This child MUST be passed to the next loop.
5023 */
5025{
5026 fr_dict_attr_t const * const *bin;
5027 fr_dict_attr_t const **children;
5028 fr_dict_attr_t const *ref;
5029 size_t len, i, start;
5030
5031 if (!parent || !prev) return NULL;
5032
5033 ref = fr_dict_attr_ref(parent);
5034 if (ref) parent = ref;
5035
5036 children = dict_attr_children(parent);
5037 if (!children) return NULL;
5038
5039 if (!*prev) {
5040 start = 0;
5041
5042 } else if ((*prev)->next) {
5043 /*
5044 * There are more children in this bin, return
5045 * the next one.
5046 */
5047 return (*prev)->next;
5048
5049 } else {
5050 /*
5051 * Figure out which bin we were in. If it was
5052 * the last one, we're done.
5053 */
5054 start = (*prev)->attr & 0xff;
5055 if (start == 255) return NULL;
5056
5057 /*
5058 * Start at the next bin.
5059 */
5060 start++;
5061 }
5062
5063 /*
5064 * Look for a non-empty bin, and return the first child
5065 * from there.
5066 */
5067 len = talloc_array_length(children);
5068 for (i = start; i < len; i++) {
5069 bin = &children[i & 0xff];
5070
5071 if (*bin) return *bin;
5072 }
5073
5074 return NULL;
5075}
5076
5077/** Call the specified callback for da and then for all its children
5078 *
5079 */
5080static int dict_walk(fr_dict_attr_t const *da, fr_dict_walk_t callback, void *uctx)
5081{
5082 size_t i, len;
5083 fr_dict_attr_t const **children;
5084
5085 children = dict_attr_children(da);
5086
5087 if (fr_dict_attr_ref(da) || !children) return callback(da, uctx);
5088
5089 len = talloc_array_length(children);
5090 for (i = 0; i < len; i++) {
5091 int ret;
5092 fr_dict_attr_t const *bin;
5093
5094 if (!children[i]) continue;
5095
5096 for (bin = children[i]; bin; bin = bin->next) {
5097 ret = dict_walk(bin, callback, uctx);
5098 if (ret < 0) return ret;
5099 }
5100 }
5101
5102 return 0;
5103}
5104
5105int fr_dict_walk(fr_dict_attr_t const *da, fr_dict_walk_t callback, void *uctx)
5106{
5107 return dict_walk(da, callback, uctx);
5108}
5109
5110
5111void fr_dict_attr_verify(char const *file, int line, fr_dict_attr_t const *da)
5112{
5113 int i;
5114 fr_dict_attr_t const *da_p;
5115
5116 if (!da) fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_dict_attr_t pointer was NULL", file, line);
5117
5119
5120 if ((!da->flags.is_root) && (da->depth == 0)) {
5121 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_dict_attr_t %s vendor: %u, attr %u: "
5122 "Is not root, but depth is 0",
5123 file, line, da->name, fr_dict_vendor_num_by_da(da), da->attr);
5124 }
5125
5126 if (da->depth > FR_DICT_MAX_TLV_STACK) {
5127 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_dict_attr_t %s vendor: %u, attr %u: "
5128 "Indicated depth (%u) greater than TLV stack depth (%d)",
5129 file, line, da->name, fr_dict_vendor_num_by_da(da), da->attr,
5130 da->depth, FR_DICT_MAX_TLV_STACK);
5131 }
5132
5133 for (da_p = da; da_p; da_p = da_p->next) {
5135 }
5136
5137 for (i = da->depth, da_p = da; (i >= 0) && da; i--, da_p = da_p->parent) {
5138 if (!da_p) {
5139 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_dict_attr_t %s vendor: %u, attr %u: "
5140 "Depth indicated there should be a parent, but parent is NULL",
5141 file, line, da->name, fr_dict_vendor_num_by_da(da), da->attr);
5142 }
5143 if (i != (int)da_p->depth) {
5144 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_dict_attr_t %s vendor: %u, attr %u: "
5145 "Depth out of sequence, expected %i, got %u",
5146 file, line, da->name, fr_dict_vendor_num_by_da(da), da->attr, i, da_p->depth);
5147 }
5148
5149 }
5150
5151 if ((i + 1) < 0) {
5152 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: fr_dict_attr_t top of hierarchy was not at depth 0",
5153 file, line);
5154 }
5155
5156 if (da->parent && (da->parent->type == FR_TYPE_VENDOR) && !fr_dict_attr_has_ext(da, FR_DICT_ATTR_EXT_VENDOR)) {
5157 fr_fatal_assert_fail("CONSISTENCY CHECK FAILED %s[%d]: VSA missing 'vendor' extension", file, line);
5158 }
5159
5160 switch (da->type) {
5161 case FR_TYPE_STRUCTURAL:
5162 {
5163 fr_hash_table_t *ht;
5164
5165 if (da->type == FR_TYPE_GROUP) break;
5166
5168 "CONSISTENCY CHECK FAILED %s[%d]: %s missing 'children' extension",
5169 file, line,
5170 fr_type_to_str(da->type));
5171
5173 "CONSISTENCY CHECK FAILED %s[%d]: %s missing 'namespace' extension",
5174 file, line,
5175 fr_type_to_str(da->type));
5176
5177 /*
5178 * Check the namespace hash table is ok
5179 */
5180 ht = dict_attr_namespace(da);
5181 if (unlikely(!ht)) break;
5183 }
5184 break;
5185
5186 default:
5187 break;
5188 }
5189}
5190
5191/** See if a structural da is allowed to contain another da
5192 *
5193 * We have some complex rules with different structural types,
5194 * different protocol dictionaries, references to other protocols,
5195 * etc.
5196 *
5197 * @param[in] parent The parent da, must be structural
5198 * @param[in] child The alleged child
5199 * @return
5200 * - false - the child is not allowed to be contained by the parent
5201 * - true - the child is allowed to be contained by the parent
5202 */
5204{
5205 /*
5206 * This is the common case: child is from the parent.
5207 */
5208 if (child->parent == parent) return true;
5209
5210 if (child->flags.is_raw) return true; /* let people do stupid things */
5211
5212 /*
5213 * Only structural types can have children.
5214 */
5215 if (!fr_type_structural[parent->type]) return false;
5216
5217 /*
5218 * An internal attribute can go into any other container.
5219 *
5220 * Any other attribute can go into an internal structural
5221 * attribute, because why not?
5222 */
5223 if (dict_gctx) {
5224 if (child->dict == dict_gctx->internal) return true;
5225
5226 if (parent->dict == dict_gctx->internal) return true;
5227 }
5228
5229 /*
5230 * Anything can go into internal groups.
5231 */
5232 if ((parent->type == FR_TYPE_GROUP) && parent->flags.internal) return true;
5233
5234 /*
5235 * Protocol attributes have to be in the same dictionary.
5236 *
5237 * Unless they're a cross-protocol grouping attribute.
5238 * In which case we check if the ref is the same.
5239 */
5240 if (child->dict != parent->dict) {
5241 fr_dict_attr_t const *ref;
5242
5243 ref = fr_dict_attr_ref(parent);
5244
5245 return (ref && (ref->dict == child->dict));
5246 }
5247
5248 /*
5249 * Key fields can have children, but everyone else thinks
5250 * that the struct is the parent. <sigh>
5251 */
5252 if ((parent->type == FR_TYPE_STRUCT) && child->parent->parent == parent) return true;
5253
5254 /*
5255 * We're in the same protocol dictionary, but the child
5256 * isn't directly from the parent. Therefore the only
5257 * type of same-protocol structure it can go into is a
5258 * group.
5259 */
5260 return (parent->type == FR_TYPE_GROUP);
5261}
5262
5263/** Return the protocol descriptor for the dictionary.
5264 *
5265 */
5267{
5268 return dict->proto;
5269}
5270
5271/*
5272 * Get the real protocol namespace behind a local one.
5273 */
5275{
5276 if (!da->flags.local) return da;
5277
5278 fr_assert(da->dict->root == da);
5279
5280 while (da->dict->next) {
5281 da = da->dict->next->root;
5282 }
5283
5284 return da;
5285}
5286
5287/*
5288 * Get the real protocol dictionary behind a local one.
5289 */
5291{
5292 while (dict->next) dict = dict->next;
5293
5294 return dict;
5295}
5296
5298{
5299 if ((*da_p)->type == FR_TYPE_GROUP) {
5301 return 0;
5302 }
5303
5304 (*da_p)->type = FR_TYPE_GROUP;
5305 (*da_p)->flags.type_size = 0;
5306 (*da_p)->flags.length = 0;
5307
5309
5310 return dict_attr_ref_aset(da_p, ref, FR_DICT_ATTR_REF_ALIAS);
5311}
static int const char char buffer[256]
Definition acutest.h:578
int const char * file
Definition acutest.h:704
int n
Definition acutest.h:579
va_list args
Definition acutest.h:772
int const char int line
Definition acutest.h:704
unsigned int fr_atexit_global_disarm(bool uctx_scope, fr_atexit_t func, void const *uctx)
Remove a specific global destructor (without executing it)
Definition atexit.c:229
#define fr_atexit_global(_func, _uctx)
Add a free function to the global free list.
Definition atexit.h:59
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:487
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:112
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define fr_fatal_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:183
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:202
#define FR_FAULT_LOG(_fmt,...)
Definition debug.h:50
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:148
size_t type
Length of type data.
Definition dict.h:274
size_t name_len
Allows for efficient name lookups when operating on partial buffers.
Definition dict.h:257
char const * name
of the attribute.
Definition dict.h:299
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *internal_name, char const *dependent)
(Re-)Initialize the special internal dictionary
@ FR_DICT_ENUM_EXT_ATTR_REF
Reference to a child attribute associated with this key value.
Definition dict.h:240
char const * name
Vendor name.
Definition dict.h:276
fr_dict_attr_t const ** attr
The protocol dictionary the attribute should be resolved in.
Definition dict.h:284
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition dict_util.c:2875
unsigned int is_root
Is root of a dictionary.
Definition dict.h:77
#define fr_dict_autofree(_to_free)
Definition dict.h:917
fr_dict_attr_t const * fr_dict_attr_common_parent(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition dict_util.c:2315
static fr_slen_t err
Definition dict.h:884
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3532
fr_value_box_t const ** out
Enumeration value.
Definition dict.h:283
fr_dict_t const ** dict
The protocol dictionary the attribute should be resolved in.
Definition dict.h:296
int fr_dict_protocol_afrom_file(fr_dict_t **out, char const *proto_name, char const *proto_dir, char const *dependent)
(Re)-initialize a protocol dictionary
int(* fr_dict_walk_t)(fr_dict_attr_t const *da, void *uctx)
Definition dict.h:994
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:294
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:307
#define DA_VERIFY(_x)
Definition dict.h:68
char const * name
of the attribute.
Definition dict.h:287
fr_value_box_t const * value
Enum value (what name maps to).
Definition dict.h:259
struct fr_dict_protocol_t::@127 attr
uint32_t pen
Private enterprise number.
Definition dict.h:272
fr_type_t type
of the attribute. Mismatch is a fatal error.
Definition dict.h:300
size_t length
Length of length data.
Definition dict.h:275
char const * base_dir
Directory structure beneath share.
Definition dict.h:309
@ FR_DICT_ATTR_EXT_PROTOCOL_SPECIFIC
Protocol specific extensions.
Definition dict.h:192
@ FR_DICT_ATTR_EXT_ENUMV
Enumeration values.
Definition dict.h:190
@ FR_DICT_ATTR_EXT_NAMESPACE
Attribute has its own namespace.
Definition dict.h:191
@ FR_DICT_ATTR_EXT_REF
Attribute references another attribute and/or dictionary.
Definition dict.h:186
@ FR_DICT_ATTR_EXT_VENDOR
Cached vendor pointer.
Definition dict.h:189
@ FR_DICT_ATTR_EXT_NAME
Name of the attribute.
Definition dict.h:184
@ FR_DICT_ATTR_EXT_CHILDREN
Attribute has children.
Definition dict.h:185
#define fr_dict_autoload(_to_load)
Definition dict.h:914
#define FR_DICT_MAX_TLV_STACK
Maximum TLV stack size.
Definition dict.h:519
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition dict.h:319
@ FR_DICT_ATTR_OK
No error.
Definition dict.h:320
@ FR_DICT_ATTR_NOTFOUND
Attribute couldn't be found.
Definition dict.h:321
@ FR_DICT_ATTR_EINVAL
Invalid arguments.
Definition dict.h:331
@ FR_DICT_ATTR_NO_CHILDREN
Child lookup in attribute with no children.
Definition dict.h:330
@ FR_DICT_ATTR_PARSE_ERROR
Attribute string couldn't be parsed.
Definition dict.h:323
@ FR_DICT_ATTR_INTERNAL_ERROR
Internal error occurred.
Definition dict.h:324
#define FR_DICT_ENUM_MAX_NAME_LEN
Maximum length of a enum value.
Definition dict.h:501
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:313
char const * proto
The protocol dictionary name.
Definition dict.h:310
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:172
char const * name
Enum name.
Definition dict.h:256
static fr_slen_t in
Definition dict.h:884
char const * name
name of this protocol
Definition dict.h:458
#define FR_DICT_VENDOR_MAX_NAME_LEN
Maximum length of a vendor name.
Definition dict.h:502
#define FR_DICT_ATTR_MAX_NAME_LEN
Maximum length of a attribute name.
Definition dict.h:503
unsigned int is_alias
This isn't a real attribute, it's a reference to to one.
Definition dict.h:87
fr_dict_attr_t const * da
the child structure referenced by this value of key
Definition dict.h:248
Specifies an attribute which must be present for the module to function.
Definition dict.h:293
Values of the encryption flags.
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:306
Specifies a value which must be present for the module to function.
Definition dict.h:282
Enum extension - Sub-struct or union pointer.
Definition dict.h:247
Value of an enumerated attribute.
Definition dict.h:255
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:457
Private enterprise.
Definition dict.h:271
size_t max_name_len
maximum length of a name
Definition dict_ext.h:96
fr_dict_attr_t const * vendor
ancestor which has type FR_TYPE_VENDOR
Definition dict_ext.h:89
fr_dict_attr_ref_type_t type
The state of the reference.
Definition dict_ext.h:78
fr_hash_table_t * name_by_value
Lookup a name by value.
Definition dict_ext.h:98
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:121
fr_hash_table_t * value_by_name
Lookup an enumeration value by name.
Definition dict_ext.h:97
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:148
static bool fr_dict_attr_has_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Return whether a da has a given extension or not.
Definition dict_ext.h:136
@ FR_DICT_ATTR_REF_ALIAS
The attribute is an alias for another attribute.
Definition dict_ext.h:59
@ FR_DICT_ATTR_REF_NONE
No ref set.
Definition dict_ext.h:58
static uint32_t fr_dict_vendor_num_by_da(fr_dict_attr_t const *da)
Return the vendor number for an attribute.
Definition dict_ext.h:176
Attribute extension - Holds children for an attribute.
Definition dict_ext.h:52
Attribute extension - Holds enumeration values.
Definition dict_ext.h:95
Attribute extension - Holds a hash table with the names of all children of this attribute.
Definition dict_ext.h:104
Attribute extension - Holds a reference to an attribute in another dictionary.
Definition dict_ext.h:77
Attribute extension - Cached vendor pointer.
Definition dict_ext.h:88
static int dict_attr_ref_set(fr_dict_attr_t const *da, fr_dict_attr_t const *ref, fr_dict_attr_ref_type_t type)
static int dict_attr_children_set(fr_dict_attr_t const *da, fr_dict_attr_t const **children)
static fr_hash_table_t * dict_attr_namespace(fr_dict_attr_t const *da)
Return the namespace hash table associated with the attribute.
static int dict_attr_ref_null(fr_dict_attr_t const *da)
static int dict_attr_ext_copy_all(fr_dict_attr_t **da_out_p, fr_dict_attr_t const *da_in)
Copy all attribute extensions from one attribute to another.
static void * dict_attr_ext_copy(fr_dict_attr_t **da_out_p, fr_dict_attr_t const *da_in, fr_dict_attr_ext_t ext)
Copy a single attribute extension from one attribute to another.
static fr_dict_attr_t const ** dict_attr_children(fr_dict_attr_t const *da)
static void * dict_attr_ext_alloc_size(fr_dict_attr_t **da_p, fr_dict_attr_ext_t ext, size_t ext_len)
Allocate an attribute extension of a particular size.
static void * dict_enum_ext_alloc(fr_dict_enum_value_t **enumv_p, fr_dict_enum_ext_t ext)
Allocate an enum extension.
static int dict_attr_ref_aset(fr_dict_attr_t **da_p, fr_dict_attr_t const *ref, fr_dict_attr_ref_type_t type)
static void * dict_attr_ext_alloc(fr_dict_attr_t **da_p, fr_dict_attr_ext_t ext)
Allocate an attribute extension.
void dict_hash_tables_finalise(fr_dict_t *dict)
Walk a dictionary finalising the hash tables in all attributes with a distinct namespace.
Definition dict_fixup.c:876
char * dict_dir_default
The default location for loading dictionaries if one wasn't provided.
Definition dict_priv.h:137
fr_hash_table_t * protocol_by_name
Hash containing names of all the registered protocols.
Definition dict_priv.h:142
#define dict_attr_alloc(_ctx, _parent, _name, _attr, _type, _args)
Definition dict_priv.h:255
#define INTERNAL_IF_NULL(_dict, _ret)
Set the internal dictionary if none was provided.
Definition dict_priv.h:45
fr_hash_table_t * protocol_by_num
Hash containing numbers of all the registered protocols.
Definition dict_priv.h:144
fr_dict_attr_t * root
Root attribute of this dictionary.
Definition dict_priv.h:109
#define DICT_POOL_SIZE
Definition dict_priv.h:37
dl_loader_t * dict_loader
for protocol validation
Definition dict_priv.h:140
int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref)
Add an alias to an existing attribute.
Definition dict_util.c:1409
#define dict_attr_init(_da_p, _parent, _name, _attr, _type, _args)
Full initialisation functions.
Definition dict_priv.h:231
char const * dependent
File holding the reference.
Definition dict_priv.h:62
bool dict_attr_valid(fr_dict_attr_t *da)
Validate a new attribute definition.
#define dict_attr_init_name_only(_da_p, _parent, _name, _type, _args)
Definition dict_priv.h:239
fr_dict_attr_t const * attr_protocol_encapsulation
Definition dict_priv.h:157
@ FR_DICT_PROTO_RADIUS
Definition dict_priv.h:161
fr_dict_t * internal
Magic internal dictionary.
Definition dict_priv.h:155
bool free_at_exit
This gctx will be freed on exit.
Definition dict_priv.h:130
#define dict_attr_alloc_root(_ctx, _dict, _name, _attr, _args)
Definition dict_priv.h:247
bool perm_check
Whether we should check dictionary file permissions as they're loaded.
Definition dict_priv.h:132
int count
How many references are held by this file.
Definition dict_priv.h:60
Optional arguments for initialising/allocating attributes.
Definition dict_priv.h:191
Entry recording dictionary reference holders by file.
Definition dict_priv.h:58
Entry in the filename list of files associated with this dictionary.
Definition dict_priv.h:69
Test enumeration values.
Definition dict_test.h:92
static fr_dict_protocol_t dict_proto_default
Default protocol rules set for every dictionary.
Definition dict_util.c:103
int fr_dl_dict_attr_autoload(UNUSED dl_t const *module, void *symbol, UNUSED void *user_ctx)
Callback to automatically resolve attributes and check the types are correct.
Definition dict_util.c:4603
fr_dict_t * fr_dict_global_ctx_iter_next(fr_dict_global_ctx_iter_t *iter)
Definition dict_util.c:4902
int fr_dict_attr_set_group(fr_dict_attr_t **da_p, fr_dict_attr_t const *ref)
Definition dict_util.c:5297
int fr_dict_enum_add_name(fr_dict_attr_t *da, char const *name, fr_value_box_t const *value, bool coerce, bool takes_precedence)
Add a value name.
Definition dict_util.c:2219
fr_slen_t fr_dict_attr_search_by_name_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_t const *dict_def, fr_sbuff_t *name, fr_sbuff_term_t const *tt, bool internal, bool foreign)
Locate a fr_dict_attr_t by its name in the top level namespace of a dictionary.
Definition dict_util.c:3273
ssize_t fr_dict_attr_by_oid_legacy(fr_dict_t const *dict, fr_dict_attr_t const **parent, unsigned int *attr, char const *oid)
Get the leaf attribute of an OID string.
Definition dict_util.c:2403
fr_dict_gctx_t * fr_dict_global_ctx_init(TALLOC_CTX *ctx, bool free_at_exit, char const *dict_dir)
Initialise the global protocol hashes.
Definition dict_util.c:4718
int fr_dict_attr_add_initialised(fr_dict_attr_t *da)
A variant of fr_dict_attr_t that allows a pre-allocated, populated fr_dict_attr_t to be added.
Definition dict_util.c:1847
ssize_t fr_dict_valid_oid_str(char const *name, ssize_t len)
Definition dict_util.c:4991
int fr_dict_global_ctx_dir_set(char const *dict_dir)
Allow the default dict dir to be changed after initialisation.
Definition dict_util.c:4809
fr_slen_t fr_dict_enum_name_from_substr(fr_sbuff_t *out, fr_sbuff_parse_error_t *err, fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Extract an enumeration name from a string.
Definition dict_util.c:3825
static int _dict_global_free_at_exit(void *uctx)
Definition dict_util.c:4637
static uint32_t dict_protocol_num_hash(void const *data)
Hash a protocol number.
Definition dict_util.c:178
static uint32_t dict_vendor_name_hash(void const *data)
Wrap name hash function for fr_dict_vendor_t.
Definition dict_util.c:287
#define DICT_NAME_APPEND(_in, _dict)
static int _dict_free(fr_dict_t *dict)
Definition dict_util.c:4099
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition dict_util.c:4913
static int dict_attr_init_common(char const *filename, int line, fr_dict_attr_t **da_p, fr_dict_attr_t const *parent, fr_type_t type, dict_attr_args_t const *args)
Definition dict_util.c:834
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition dict_util.c:2875
int fr_dict_enum_autoload(fr_dict_enum_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4361
fr_dict_enum_value_t const * fr_dict_enum_iter_init(fr_dict_attr_t const *da, fr_dict_enum_iter_t *iter)
Iterate over all enumeration values for an attribute.
Definition dict_util.c:3618
int _fr_dict_autofree(fr_dict_autoload_t const *to_free, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4498
static int dict_autoref_free(fr_dict_t *dict)
Definition dict_util.c:4072
int fr_dict_walk(fr_dict_attr_t const *da, fr_dict_walk_t callback, void *uctx)
Definition dict_util.c:5105
fr_slen_t dict_by_protocol_substr(fr_dict_attr_err_t *err, fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def)
Definition dict_util.c:2684
fr_dict_attr_t const * fr_dict_unlocal(fr_dict_attr_t const *da)
Definition dict_util.c:5274
fr_dict_attr_t const * fr_dict_attr_common_parent(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition dict_util.c:2315
static int dict_walk(fr_dict_attr_t const *da, fr_dict_walk_t callback, void *uctx)
Call the specified callback for da and then for all its children.
Definition dict_util.c:5080
fr_dict_attr_t * dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *proto)
Allocate a partially completed attribute.
Definition dict_util.c:994
int dict_attr_type_init(fr_dict_attr_t **da_p, fr_type_t type)
Initialise type specific fields within the dictionary attribute.
Definition dict_util.c:556
int dict_attr_parent_init(fr_dict_attr_t **da_p, fr_dict_attr_t const *parent)
Initialise fields which depend on a parent attribute.
Definition dict_util.c:643
static void dependent_debug(fr_dict_t *dict)
Definition dict_util.c:4054
fr_dict_t const * fr_dict_proto_dict(fr_dict_t const *dict)
Definition dict_util.c:5290
fr_dict_t * dict_alloc(TALLOC_CTX *ctx)
Allocate a new dictionary.
Definition dict_util.c:4182
static uint32_t dict_vendor_pen_hash(void const *data)
Hash a vendor number.
Definition dict_util.c:312
bool const fr_dict_attr_allowed_chars[UINT8_MAX+1]
Characters allowed in a single dictionary attribute name.
Definition dict_util.c:64
int8_t fr_dict_attr_ordered_cmp(fr_dict_attr_t const *a, fr_dict_attr_t const *b)
Compare two attributes by total order.
Definition dict_util.c:229
fr_dict_protocol_t const * fr_dict_protocol(fr_dict_t const *dict)
Return the protocol descriptor for the dictionary.
Definition dict_util.c:5266
fr_dict_attr_t * _dict_attr_alloc_root(char const *filename, int line, TALLOC_CTX *ctx, fr_dict_t const *dict, char const *name, int proto_number, dict_attr_args_t const *args)
Allocate a dictionary root attribute on the heap.
Definition dict_util.c:1035
fr_dict_attr_t * _dict_attr_alloc(char const *filename, int line, TALLOC_CTX *ctx, fr_dict_attr_t const *parent, char const *name, int attr, fr_type_t type, dict_attr_args_t const *args)
Allocate a dictionary attribute on the heap.
Definition dict_util.c:1069
int fr_dict_attr_acopy_local(fr_dict_attr_t const *dst, fr_dict_attr_t const *src)
Definition dict_util.c:1134
void fr_dl_dict_autofree(UNUSED dl_t const *module, void *symbol, UNUSED void *user_ctx)
Callback to automatically free a dictionary when the module is unloaded.
Definition dict_util.c:4632
fr_dict_autoload_talloc_t * _fr_dict_autoload_talloc(TALLOC_CTX *ctx, fr_dict_t const **out, char const *proto, char const *dependent)
Autoload a dictionary and bind the lifetime to a talloc chunk.
Definition dict_util.c:4549
bool fr_dict_compatible(fr_dict_t const *dict1, fr_dict_t const *dict2)
See if two dictionaries have the same end parent.
Definition dict_util.c:2888
static fr_slen_t dict_attr_search_qualified(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_t const *dict_def, fr_sbuff_t *in, fr_sbuff_term_t const *tt, bool internal, bool foreign, dict_attr_resolve_func_t func)
Internal function for searching for attributes in multiple dictionaries.
Definition dict_util.c:3177
bool dict_attr_can_have_children(fr_dict_attr_t const *da)
See if a fr_dict_attr_t can have children.
Definition dict_util.c:1657
fr_dict_attr_t * fr_dict_attr_unconst(fr_dict_attr_t const *da)
Coerce to non-const.
Definition dict_util.c:4925
static int8_t _dict_dependent_cmp(void const *a, void const *b)
Find a dependent in the tree of dependents.
Definition dict_util.c:3937
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:2593
void fr_dict_attr_verify(char const *file, int line, fr_dict_attr_t const *da)
Definition dict_util.c:5111
#define FNV_MAGIC_PRIME
Definition dict_util.c:115
bool fr_dict_attr_can_contain(fr_dict_attr_t const *parent, fr_dict_attr_t const *child)
See if a structural da is allowed to contain another da.
Definition dict_util.c:5203
void dict_attr_location_init(fr_dict_attr_t *da, char const *filename, int line)
Set where the dictionary attribute was defined.
Definition dict_util.c:749
fr_dict_t * dict_by_da(fr_dict_attr_t const *da)
Internal version of fr_dict_by_da.
Definition dict_util.c:2805
bool const fr_dict_attr_nested_allowed_chars[UINT8_MAX+1]
Characters allowed in a nested dictionary attribute name.
Definition dict_util.c:71
fr_dict_t * fr_dict_global_ctx_iter_init(fr_dict_global_ctx_iter_t *iter)
Iterate protocols by name.
Definition dict_util.c:4895
static int8_t dict_vendor_name_cmp(void const *one, void const *two)
Compare two attribute names.
Definition dict_util.c:299
fr_slen_t fr_dict_attr_search_by_qualified_name_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_t const *dict_def, fr_sbuff_t *name, fr_sbuff_term_t const *tt, bool internal, bool foreign)
Locate a qualified fr_dict_attr_t by its name and a dictionary qualifier.
Definition dict_util.c:3244
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2669
fr_dict_attr_t const * fr_dict_vendor_da_by_num(fr_dict_attr_t const *vendor_root, uint32_t vendor_pen)
Return vendor attribute for the specified dictionary and pen.
Definition dict_util.c:2964
int dict_attr_add_to_namespace(fr_dict_attr_t const *parent, fr_dict_attr_t *da)
Add an attribute to the name table for an attribute.
Definition dict_util.c:1781
fr_dict_attr_t * dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Internal version of fr_dict_attr_child_by_num.
Definition dict_util.c:3550
void fr_dict_global_ctx_set(fr_dict_gctx_t const *gctx)
Set a new, active, global dictionary context.
Definition dict_util.c:4779
int fr_dl_dict_autoload(UNUSED dl_t const *module, void *symbol, UNUSED void *user_ctx)
Callback to automatically load dictionaries required by modules.
Definition dict_util.c:4619
static int8_t dict_attr_name_cmp(void const *one, void const *two)
Compare two attribute names.
Definition dict_util.c:211
static int8_t dict_vendor_pen_cmp(void const *one, void const *two)
Compare two vendor numbers.
Definition dict_util.c:321
static int8_t dict_enum_value_cmp(void const *one, void const *two)
Compare two dictionary enum values.
Definition dict_util.c:372
fr_dict_attr_t * dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *name)
Definition dict_util.c:3486
static void hash_pool_free(void *to_free)
Definition dict_util.c:117
static uint32_t dict_attr_name_hash(void const *data)
Wrap name hash function for fr_dict_attr_t.
Definition dict_util.c:199
bool const fr_dict_enum_allowed_chars[UINT8_MAX+1]
Characters allowed in enumeration value names.
Definition dict_util.c:79
static int _fr_dict_autoload_talloc_free(fr_dict_autoload_talloc_t const *to_free)
Talloc destructor to automatically free dictionaries.
Definition dict_util.c:4531
fr_dict_t * dict_by_protocol_num(unsigned int num)
Internal version of fr_dict_by_protocol_num.
Definition dict_util.c:2791
static int dict_attr_acopy_child(fr_dict_t *dict, fr_dict_attr_t *dst, fr_dict_attr_t const *src, fr_dict_attr_t const *child)
Definition dict_util.c:1158
fr_slen_t fr_dict_attr_search_by_qualified_oid_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_t const *dict_def, fr_sbuff_t *in, fr_sbuff_term_t const *tt, bool internal, bool foreign)
Locate a qualified fr_dict_attr_t by a dictionary qualified OID string.
Definition dict_util.c:3302
dl_t * fr_dict_dl(fr_dict_t const *dict)
Definition dict_util.c:2679
static fr_slen_t dict_attr_search(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_t const *dict_def, fr_sbuff_t *in, fr_sbuff_term_t const *tt, bool internal, bool foreign, dict_attr_resolve_func_t func)
Internal function for searching for attributes in multiple dictionaries.
Definition dict_util.c:3029
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:3692
fr_dict_enum_value_t const * fr_dict_enum_iter_next(fr_dict_attr_t const *da, fr_dict_enum_iter_t *iter)
Definition dict_util.c:3639
int fr_dict_enum_add_name_next(fr_dict_attr_t *da, char const *name)
Add an name to an integer attribute hashing the name for the integer value.
Definition dict_util.c:2231
int dict_attr_child_add(fr_dict_attr_t *parent, fr_dict_attr_t *child)
Add a child to a parent.
Definition dict_util.c:1682
static int dict_attr_children_init(fr_dict_attr_t **da_p)
Add a child/nesting extension to an attribute.
Definition dict_util.c:468
fr_dict_autoload_t load[2]
Autoloader def.
Definition dict_util.c:4523
int fr_dict_free(fr_dict_t **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4334
fr_dict_enum_value_t const * fr_dict_enum_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the structure representing an enum value in a fr_dict_attr_t.
Definition dict_util.c:3659
int dict_dependent_remove(fr_dict_t *dict, char const *dependent)
Decrement ref count for a dependent in a dictionary.
Definition dict_util.c:4016
int fr_dict_oid_component_legacy(unsigned int *out, char const **oid)
Process a single OID component.
Definition dict_util.c:2358
fr_slen_t fr_dict_enum_by_name_substr(fr_dict_enum_value_t **out, fr_dict_attr_t const *da, fr_sbuff_t *in)
Definition dict_util.c:3731
static int8_t dict_enum_name_cmp(void const *one, void const *two)
Compare two dictionary attribute enum values.
Definition dict_util.c:342
void fr_dict_global_ctx_perm_check(fr_dict_gctx_t *gctx, bool enable)
Set whether we check dictionary file permissions.
Definition dict_util.c:4770
void fr_dict_global_ctx_read_only(void)
Mark all dictionaries and the global dictionary ctx as read only.
Definition dict_util.c:4829
int _dict_attr_init_name_only(char const *filename, int line, fr_dict_attr_t **da_p, fr_dict_attr_t const *parent, char const *name, fr_type_t type, dict_attr_args_t const *args)
Initialise fields in a dictionary attribute structure.
Definition dict_util.c:942
fr_dict_attr_t const * fr_dict_attr_by_oid(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *oid)
Resolve an attribute using an OID string.
Definition dict_util.c:2642
int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
Add a vendor to the dictionary.
Definition dict_util.c:1587
static int _dict_global_free(fr_dict_gctx_t *gctx)
Definition dict_util.c:4642
static int8_t dict_protocol_name_cmp(void const *one, void const *two)
Compare two protocol names.
Definition dict_util.c:165
static int dict_attr_enumv_init(fr_dict_attr_t **da_p)
Initialise a per-attribute enumeration table.
Definition dict_util.c:503
int fr_dict_const_free(fr_dict_t const **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4318
int fr_dl_dict_enum_autoload(UNUSED dl_t const *module, void *symbol, UNUSED void *user_ctx)
Callback to automatically resolve enum values.
Definition dict_util.c:4587
int fr_dict_attr_autoload(fr_dict_attr_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4400
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4938
fr_slen_t fr_dict_by_protocol_substr(fr_dict_attr_err_t *err, fr_dict_t const **out, fr_sbuff_t *name, fr_dict_t const *dict_def)
Look up a protocol name embedded in another string.
Definition dict_util.c:2766
int fr_dict_dependent_add(fr_dict_t const *dict, char const *dependent)
Manually increase the reference count for a dictionary.
Definition dict_util.c:4001
static int8_t dict_protocol_num_cmp(void const *one, void const *two)
Compare two protocol numbers.
Definition dict_util.c:186
int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref)
Add an alias to an existing attribute.
Definition dict_util.c:1409
fr_dict_t const * fr_dict_by_protocol_name(char const *name)
Lookup a protocol by its name.
Definition dict_util.c:2846
static uint32_t dict_protocol_name_hash(void const *data)
Wrap name hash function for fr_dict_protocol_t.
Definition dict_util.c:153
int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name)
Set remaining fields in a dictionary attribute before insertion.
Definition dict_util.c:763
bool fr_dict_is_read_only(fr_dict_t const *dict)
Definition dict_util.c:2674
static uint32_t dict_hash_name(char const *name, size_t len)
Apply a simple (case insensitive) hashing function to the name of an attribute, vendor or protocol.
Definition dict_util.c:129
int fr_dict_attr_add_name_only(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, fr_type_t type, fr_dict_attr_flags_t const *flags)
Add an attribute to the dictionary.
Definition dict_util.c:2008
#define FNV_MAGIC_INIT
Definition dict_util.c:114
int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num)
Set the attribute number (if any)
Definition dict_util.c:719
static int dict_attr_namespace_init(fr_dict_attr_t **da_p)
Initialise a per-attribute namespace.
Definition dict_util.c:519
char const * fr_dict_global_ctx_dir(void)
Definition dict_util.c:4820
int dict_attr_num_init_name_only(fr_dict_attr_t *da)
Set the attribute number (if any)
Definition dict_util.c:737
static int _dict_attr_free(fr_dict_attr_t *da)
Definition dict_util.c:960
int fr_dict_global_ctx_free(fr_dict_gctx_t const *gctx)
Explicitly free all data associated with a global dictionary context.
Definition dict_util.c:4795
int dict_dlopen(fr_dict_t *dict, char const *name)
Definition dict_util.c:3865
fr_dict_t * dict_by_protocol_name(char const *name)
Internal version of fr_dict_by_protocol_name.
Definition dict_util.c:2777
int dict_attr_acopy_children(fr_dict_t *dict, fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy the children of an existing attribute.
Definition dict_util.c:1196
fr_dict_vendor_t const * fr_dict_vendor_by_name(fr_dict_t const *dict, char const *name)
Look up a vendor by its name.
Definition dict_util.c:2926
int dict_attr_acopy_aliases(UNUSED fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy aliases of an existing attribute to a new one.
Definition dict_util.c:1328
fr_slen_t(* dict_attr_resolve_func_t)(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)
Callback function for resolving dictionary attributes.
Definition dict_util.c:3009
void fr_dict_gctx_debug(FILE *fp, fr_dict_gctx_t const *gctx)
Dump information about currently loaded dictionaries.
Definition dict_util.c:4856
fr_dict_attr_t const * fr_dict_attr_iterate_children(fr_dict_attr_t const *parent, fr_dict_attr_t const **prev)
Iterate over children of a DA.
Definition dict_util.c:5024
int dict_attr_enum_add_name(fr_dict_attr_t *da, char const *name, fr_value_box_t const *value, bool coerce, bool takes_precedence, fr_dict_attr_t const *key_child_ref)
Definition dict_util.c:2022
int _fr_dict_autoload(fr_dict_autoload_t const *to_load, char const *dependent)
Process a dict_autoload element to load a protocol.
Definition dict_util.c:4465
fr_dict_vendor_t const * fr_dict_vendor_by_num(fr_dict_t const *dict, uint32_t vendor_pen)
Look up a vendor by its PEN.
Definition dict_util.c:2949
fr_slen_t fr_dict_attr_search_by_oid_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_t const *dict_def, fr_sbuff_t *in, fr_sbuff_term_t const *tt, bool internal, bool foreign)
Locate a qualified fr_dict_attr_t by a dictionary using a non-qualified OID string.
Definition dict_util.c:3331
ssize_t fr_dict_valid_name(char const *name, ssize_t len)
Definition dict_util.c:4948
#define DICT_ATTR_ALLOWED_CHARS
Definition dict_util.c:44
fr_dict_t * fr_dict_protocol_alloc(fr_dict_t const *parent)
Allocate a new local dictionary.
Definition dict_util.c:4271
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *name)
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3532
fr_slen_t fr_dict_attr_by_name_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_attr_t const *parent, fr_sbuff_t *name, UNUSED fr_sbuff_term_t const *tt)
Look up a dictionary attribute by a name embedded in another string.
Definition dict_util.c:3401
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:3597
int dict_dependent_add(fr_dict_t *dict, char const *dependent)
Record a new dependency on a dictionary.
Definition dict_util.c:3957
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:3705
static int dict_attr_name_set(fr_dict_attr_t **da_p, char const *name)
Set a dictionary attribute's name.
Definition dict_util.c:415
fr_slen_t fr_dict_oid_component(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)
Parse an OID component, resolving it to a defined attribute.
Definition dict_util.c:2494
static int dict_attr_vendor_set(fr_dict_attr_t **da_p, fr_dict_attr_t const *vendor)
Cache the vendor pointer for an attribute.
Definition dict_util.c:485
static uint32_t dict_enum_name_hash(void const *data)
Hash a enumeration name.
Definition dict_util.c:332
int dict_protocol_add(fr_dict_t *dict)
Add a protocol to the global protocol table.
Definition dict_util.c:1514
static uint32_t dict_enum_value_hash(void const *data)
Hash a dictionary enum value.
Definition dict_util.c:362
fr_dict_attr_t const * fr_dict_attr_search_by_qualified_oid(fr_dict_attr_err_t *err, fr_dict_t const *dict_def, char const *name, bool internal, bool foreign)
Locate a qualified fr_dict_attr_t by its name and a dictionary qualifier.
Definition dict_util.c:3350
bool dict_has_dependents(fr_dict_t *dict)
Check if a dictionary still has dependents.
Definition dict_util.c:4048
fr_dict_gctx_t * dict_gctx
Top level structure containing global dictionary state.
Definition dict_util.c:42
fr_dict_t const * fr_dict_by_protocol_num(unsigned int num)
Lookup a protocol by its number.
Definition dict_util.c:2859
int dict_attr_acopy_enumv(fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy the VALUEs of an existing attribute, by casting them.
Definition dict_util.c:1293
int fr_dict_attr_add(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, unsigned int attr, fr_type_t type, fr_dict_attr_flags_t const *flags)
Add an attribute to the dictionary.
Definition dict_util.c:1969
char const * dependent
Dependent that loaded the dictionary.
Definition dict_util.c:4524
static fr_dict_attr_t const * dict_attr_alias(fr_dict_attr_err_t *err, fr_dict_attr_t const *da)
Resolve an alias attribute to the concrete attribute it points to.
Definition dict_util.c:390
fr_dict_vendor_t const * fr_dict_vendor_by_da(fr_dict_attr_t const *da)
Look up a vendor by one of its child attributes.
Definition dict_util.c:2904
fr_dict_attr_t * dict_attr_acopy(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, fr_dict_attr_t const *in, char const *name)
Copy a an existing attribute, possibly to a new location.
Definition dict_util.c:1098
int _dict_attr_init(char const *filename, int line, fr_dict_attr_t **da_p, fr_dict_attr_t const *parent, char const *name, unsigned int attr, fr_type_t type, dict_attr_args_t const *args)
Initialise fields in a dictionary attribute structure.
Definition dict_util.c:895
Structure used to managed the lifetime of a dictionary.
Definition dict_util.c:4522
dl_loader_t * dl_loader_init(TALLOC_CTX *ctx, void *uctx, bool uctx_free, bool defer_symbol_init)
Initialise structures needed by the dynamic linker.
Definition dl.c:885
int dl_free(dl_t const *dl)
"free" a dl handle, possibly actually freeing it, and unloading the library
Definition dl.c:678
dl_t * dl_by_name(dl_loader_t *dl_loader, char const *name, void *uctx, bool uctx_free)
Search for a dl's shared object in various locations.
Definition dl.c:470
Module handle.
Definition dl.h:58
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:257
void * fr_hash_table_iter_next(fr_hash_table_t *ht, fr_hash_iter_t *iter)
Iterate over entries in a hash table.
Definition hash.c:627
void * fr_hash_table_find(fr_hash_table_t *ht, void const *data)
Find data in a hash table.
Definition hash.c:430
void * fr_hash_table_iter_init(fr_hash_table_t *ht, fr_hash_iter_t *iter)
Initialise an iterator.
Definition hash.c:679
uint32_t fr_hash(void const *data, size_t size)
Definition hash.c:813
bool fr_hash_table_insert(fr_hash_table_t *ht, void const *data)
Insert data into a hash table.
Definition hash.c:469
int fr_hash_table_flatten(TALLOC_CTX *ctx, void **out[], fr_hash_table_t *ht)
Copy all entries out of a hash table into an array.
Definition hash.c:696
uint32_t fr_hash_string(char const *p)
Definition hash.c:866
bool fr_hash_table_delete(fr_hash_table_t *ht, void const *data)
Remove and free data (if a free function was specified)
Definition hash.c:595
void fr_hash_table_verify(fr_hash_table_t *ht)
Check hash table is sane.
Definition hash.c:898
int fr_hash_table_replace(void **old, fr_hash_table_t *ht, void const *data)
Replace old data with new data, OR insert if there is no old.
Definition hash.c:529
uint32_t fr_hash_table_num_elements(fr_hash_table_t *ht)
Definition hash.c:611
#define fr_hash_table_alloc(_ctx, _hash_node, _cmp_node, _free_node)
Definition hash.h:58
#define fr_hash_table_talloc_alloc(_ctx, _type, _hash_node, _cmp_node, _free_node)
Definition hash.h:61
Stores the state of the current iteration operation.
Definition hash.h:41
talloc_free(hp)
fr_type_t
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_INT8
8 Bit signed integer.
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_STRING
String of printable characters.
@ 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_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_VOID
User data.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
unsigned int uint32_t
long int ssize_t
ssize_t fr_sbuff_out_bstrncpy_exact(fr_sbuff_t *out, fr_sbuff_t *in, size_t len)
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_NUM_OVERFLOW
Integer type would overflow.
@ FR_SBUFF_PARSE_ERROR_NOT_FOUND
String does not contain a token matching the output type.
@ FR_SBUFF_PARSE_ERROR_FORMAT
Format of data was invalid.
@ FR_SBUFF_PARSE_OK
No error.
@ FR_SBUFF_PARSE_ERROR_TRAILING
Trailing characters found.
size_t fr_sbuff_out_bstrncpy_allowed(fr_sbuff_t *out, fr_sbuff_t *in, size_t len, bool const allowed[static UINT8_MAX+1])
int strncasecmp(char *s1, char *s2, int n)
Definition missing.c:36
int strcasecmp(char *s1, char *s2)
Definition missing.c:66
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:83
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
Return how many nodes there are in a tree.
Definition rb.c:781
void * fr_rb_iter_init_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Initialise an in-order iterator.
Definition rb.c:824
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
bool fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
Definition rb.c:741
void * fr_rb_iter_next_inorder(UNUSED fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Return the next node.
Definition rb.c:850
#define fr_rb_inline_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black tree.
Definition rb.h:271
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:321
static unsigned int hash(char const *username, unsigned int tablesize)
Definition rlm_passwd.c:132
static char const * name
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static UINT8_MAX+1], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
Definition sbuff.c:1805
bool fr_sbuff_is_terminal(fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Efficient terminal string search.
Definition sbuff.c:2180
size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr)
Wind position until we hit a character in the terminal set.
Definition sbuff.c:1880
fr_table_num_ordered_t const sbuff_parse_error_table[]
Definition sbuff.c:43
ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt,...)
Print using a fmt string to an sbuff.
Definition sbuff.c:1592
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
Definition sbuff.c:2116
bool const sbuff_char_alpha_num[UINT8_MAX+1]
Definition sbuff.c:98
#define fr_sbuff_set(_dst, _src)
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_current(_sbuff_or_marker)
#define fr_sbuff_char(_sbuff_or_marker, _eob)
#define fr_sbuff_is_alpha(_sbuff_or_marker)
#define fr_sbuff_buff(_sbuff_or_marker)
#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(_sbuff_or_marker)
#define fr_sbuff_out(_err, _out, _in)
#define fr_sbuff_init_in(_out, _start, _len_or_end)
#define fr_sbuff_remaining(_sbuff_or_marker)
#define fr_sbuff_len(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
#define fr_sbuff_used(_sbuff_or_marker)
#define fr_sbuff_behind(_sbuff_or_marker)
Set of terminal elements.
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_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:514
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:467
#define talloc_get_type_abort_const
Definition talloc.h:244
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:229
static void talloc_bstr_tolower(char *str)
Convert a talloced string to lowercase.
Definition talloc.h:128
@ FR_TIME_RES_SEC
Definition time.h:50
@ T_OP_CMP_EQ
Definition token.h:106
static fr_slen_t parent
Definition pair.h:859
#define add(_type, _out, _in)
Definition stats.c:187
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#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
bool const fr_type_fixed_size[FR_TYPE_MAX+1]
Definition types.c:189
bool const fr_type_structural[FR_TYPE_MAX+1]
Definition types.c:194
#define fr_type_is_non_leaf(_x)
Definition types.h:395
#define fr_type_is_variable_size(_x)
Definition types.h:389
#define fr_type_is_structural(_x)
Definition types.h:393
@ FR_TYPE_UNION
A union of limited children.
Definition types.h:82
#define FR_TYPE_STRUCTURAL
Definition types.h:317
#define fr_type_is_leaf(_x)
Definition types.h:394
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:455
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:1431
uint32_t fr_value_box_hash(fr_value_box_t const *vb)
Hash the contents of a value box.
Definition value.c:6955
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:3953
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:745
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:4401
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:1009
void fr_value_box_increment(fr_value_box_t *vb)
Increment a boxed value.
Definition value.c:5243
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:644
static fr_slen_t data
Definition value.h:1332
#define fr_box_strvalue_len(_val, _len)
Definition value.h:309
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:610
static size_t char ** out
Definition value.h:1024