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