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