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