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