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