2 * dict.c Routines to read the dictionary file.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/libradius.h>
29 #include <freeradius-devel/dhcp.h>
38 #ifdef HAVE_SYS_STAT_H
43 #define DICT_VALUE_MAX_NAME_LEN (128)
44 #define DICT_VENDOR_MAX_NAME_LEN (128)
45 #define DICT_ATTR_MAX_NAME_LEN (128)
47 static fr_hash_table_t *vendors_byname = NULL;
48 static fr_hash_table_t *vendors_byvalue = NULL;
50 static fr_hash_table_t *attributes_byname = NULL;
51 static fr_hash_table_t *attributes_byvalue = NULL;
53 static fr_hash_table_t *values_byvalue = NULL;
54 static fr_hash_table_t *values_byname = NULL;
56 static DICT_ATTR *dict_base_attrs[256];
59 * For faster HUP's, we cache the stat information for
60 * files we've $INCLUDEd
62 typedef struct dict_stat_t {
63 struct dict_stat_t *next;
68 static char *stat_root_dir = NULL;
69 static char *stat_root_file = NULL;
71 static dict_stat_t *stat_head = NULL;
72 static dict_stat_t *stat_tail = NULL;
74 typedef struct value_fixup_t {
75 char attrstr[DICT_ATTR_MAX_NAME_LEN];
77 struct value_fixup_t *next;
82 * So VALUEs in the dictionary can have forward references.
84 static value_fixup_t *value_fixup = NULL;
86 const FR_NAME_NUMBER dict_attr_types[] = {
87 { "integer", PW_TYPE_INTEGER },
88 { "string", PW_TYPE_STRING },
89 { "ipaddr", PW_TYPE_IPADDR },
90 { "date", PW_TYPE_DATE },
91 { "abinary", PW_TYPE_ABINARY },
92 { "octets", PW_TYPE_OCTETS },
93 { "ifid", PW_TYPE_IFID },
94 { "ipv6addr", PW_TYPE_IPV6ADDR },
95 { "ipv6prefix", PW_TYPE_IPV6PREFIX },
96 { "byte", PW_TYPE_BYTE },
97 { "short", PW_TYPE_SHORT },
98 { "ether", PW_TYPE_ETHERNET },
99 { "combo-ip", PW_TYPE_COMBO_IP },
100 { "tlv", PW_TYPE_TLV },
101 { "signed", PW_TYPE_SIGNED },
102 { "extended", PW_TYPE_EXTENDED },
103 { "long-extended", PW_TYPE_LONG_EXTENDED },
104 { "evs", PW_TYPE_EVS },
105 { "uint8", PW_TYPE_BYTE },
106 { "uint16", PW_TYPE_SHORT },
107 { "uint32", PW_TYPE_INTEGER },
108 { "int32", PW_TYPE_SIGNED },
109 { "integer64", PW_TYPE_INTEGER64 },
110 { "uint64", PW_TYPE_INTEGER64 },
111 { "ipv4prefix", PW_TYPE_IPV4PREFIX },
117 * For packing multiple TLV numbers into one 32-bit integer. The
118 * first 3 bytes are just the 8-bit number. The next two are
119 * more limited. We only allow 31 attributes nested 3 layers
120 * deep, and only 7 nested 4 layers deep. This should be
121 * sufficient for most purposes.
123 * For TLVs and extended attributes, we packet the base attribute
124 * number into the upper 8 bits of the "vendor" field.
126 * e.g. OID attribute vendor
128 * 241.26.9.1 1 (241 << 8) | (9)
129 * 241.1.2 1 | (2 << 8) (241 << 8)
131 #define MAX_TLV_NEST (4)
134 * 8 bits of base attribute
135 * 8 bits for nested TLV 1
136 * 8 bits for nested TLV 2
137 * 5 bits for nested TLV 3
138 * 3 bits for nested TLV 4
140 const int fr_attr_max_tlv = MAX_TLV_NEST;
141 const int fr_attr_shift[MAX_TLV_NEST + 1] = {
145 const int fr_attr_mask[MAX_TLV_NEST + 1] = {
146 0xff, 0xff, 0xff, 0x1f, 0x07
151 * Create the hash of the name.
153 * We copy the hash function here because it's substantially faster.
155 #define FNV_MAGIC_INIT (0x811c9dc5)
156 #define FNV_MAGIC_PRIME (0x01000193)
158 static uint32_t dict_hashname(const char *name)
160 uint32_t hash = FNV_MAGIC_INIT;
163 for (p = name; *p != '\0'; p++) {
164 int c = *(const unsigned char *) p;
165 if (isalpha(c)) c = tolower(c);
167 hash *= FNV_MAGIC_PRIME;
168 hash ^= (uint32_t ) (c & 0xff);
176 * Hash callback functions.
178 static uint32_t dict_attr_name_hash(const void *data)
180 return dict_hashname(((const DICT_ATTR *)data)->name);
183 static int dict_attr_name_cmp(const void *one, const void *two)
185 const DICT_ATTR *a = one;
186 const DICT_ATTR *b = two;
188 return strcasecmp(a->name, b->name);
191 static uint32_t dict_attr_value_hash(const void *data)
194 const DICT_ATTR *attr = data;
196 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
197 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
200 static int dict_attr_value_cmp(const void *one, const void *two)
202 const DICT_ATTR *a = one;
203 const DICT_ATTR *b = two;
205 if (a->vendor < b->vendor) return -1;
206 if (a->vendor > b->vendor) return +1;
208 return a->attr - b->attr;
211 static uint32_t dict_vendor_name_hash(const void *data)
213 return dict_hashname(((const DICT_VENDOR *)data)->name);
216 static int dict_vendor_name_cmp(const void *one, const void *two)
218 const DICT_VENDOR *a = one;
219 const DICT_VENDOR *b = two;
221 return strcasecmp(a->name, b->name);
224 static uint32_t dict_vendor_value_hash(const void *data)
226 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
227 sizeof(((const DICT_VENDOR *)data)->vendorpec));
230 static int dict_vendor_value_cmp(const void *one, const void *two)
232 const DICT_VENDOR *a = one;
233 const DICT_VENDOR *b = two;
235 return a->vendorpec - b->vendorpec;
238 static uint32_t dict_value_name_hash(const void *data)
241 const DICT_VALUE *dval = data;
243 hash = dict_hashname(dval->name);
244 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
245 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
248 static int dict_value_name_cmp(const void *one, const void *two)
251 const DICT_VALUE *a = one;
252 const DICT_VALUE *b = two;
254 rcode = a->attr - b->attr;
255 if (rcode != 0) return rcode;
257 rcode = a->vendor - b->vendor;
258 if (rcode != 0) return rcode;
260 return strcasecmp(a->name, b->name);
263 static uint32_t dict_value_value_hash(const void *data)
266 const DICT_VALUE *dval = data;
268 hash = fr_hash(&dval->attr, sizeof(dval->attr));
269 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
270 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
273 static int dict_value_value_cmp(const void *one, const void *two)
276 const DICT_VALUE *a = one;
277 const DICT_VALUE *b = two;
279 if (a->vendor < b->vendor) return -1;
280 if (a->vendor > b->vendor) return +1;
282 rcode = a->attr - b->attr;
283 if (rcode != 0) return rcode;
285 return a->value - b->value;
290 * Free the list of stat buffers
292 static void dict_stat_free(void)
294 dict_stat_t *this, *next;
297 stat_root_dir = NULL;
298 free(stat_root_file);
299 stat_root_file = NULL;
306 for (this = stat_head; this != NULL; this = next) {
312 stat_head = stat_tail = NULL;
317 * Add an entry to the list of stat buffers.
319 static void dict_stat_add(const char *name, const struct stat *stat_buf)
323 this = malloc(sizeof(*this));
325 memset(this, 0, sizeof(*this));
327 this->name = strdup(name);
328 this->mtime = stat_buf->st_mtime;
331 stat_head = stat_tail = this;
333 stat_tail->next = this;
340 * See if any dictionaries have changed. If not, don't
343 static int dict_stat_check(const char *root_dir, const char *root_file)
348 if (!stat_root_dir) return 0;
349 if (!stat_root_file) return 0;
351 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
352 if (strcmp(root_file, stat_root_file) != 0) return 0;
354 if (!stat_head) return 0; /* changed, reload */
356 for (this = stat_head; this != NULL; this = this->next) {
357 if (stat(this->name, &buf) < 0) return 0;
359 if (buf.st_mtime != this->mtime) return 0;
365 typedef struct fr_pool_t {
368 struct fr_pool_t *page_free;
369 struct fr_pool_t *page_next;
372 #define FR_POOL_SIZE (32768)
373 #define FR_ALLOC_ALIGN (8)
375 static fr_pool_t *dict_pool = NULL;
377 static fr_pool_t *fr_pool_create(void)
379 fr_pool_t *fp = malloc(FR_POOL_SIZE);
381 if (!fp) return NULL;
383 memset(fp, 0, FR_POOL_SIZE);
385 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
386 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
388 fp->page_next = NULL;
392 static void fr_pool_delete(fr_pool_t **pfp)
394 fr_pool_t *fp, *next;
396 if (!pfp || !*pfp) return;
398 for (fp = *pfp; fp != NULL; fp = next) {
399 next = fp->page_next;
400 fp->page_next = NULL;
407 static void *fr_pool_alloc(size_t size)
411 if (size == 0) return NULL;
413 if (size > 256) return NULL; /* shouldn't happen */
416 dict_pool = fr_pool_create();
417 if (!dict_pool) return NULL;
420 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
421 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
424 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
425 dict_pool->page_free->page_next = fr_pool_create();
426 if (!dict_pool->page_free->page_next) return NULL;
427 dict_pool->page_free = dict_pool->page_free->page_next;
430 ptr = dict_pool->page_free->free_ptr;
431 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
437 static void fr_pool_free(UNUSED void *ptr)
440 * Place-holder for later code.
445 * Free the dictionary_attributes and dictionary_values lists.
452 fr_hash_table_free(vendors_byname);
453 fr_hash_table_free(vendors_byvalue);
454 vendors_byname = NULL;
455 vendors_byvalue = NULL;
457 fr_hash_table_free(attributes_byname);
458 fr_hash_table_free(attributes_byvalue);
459 attributes_byname = NULL;
460 attributes_byvalue = NULL;
462 fr_hash_table_free(values_byname);
463 fr_hash_table_free(values_byvalue);
464 values_byname = NULL;
465 values_byvalue = NULL;
467 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
469 fr_pool_delete(&dict_pool);
476 * Add vendor to the list.
478 int dict_addvendor(const char *name, unsigned int value)
483 if (value >= FR_MAX_VENDOR) {
484 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
488 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
489 fr_strerror_printf("dict_addvendor: vendor name too long");
493 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
494 fr_strerror_printf("dict_addvendor: out of memory");
498 strcpy(dv->name, name);
499 dv->vendorpec = value;
500 dv->type = dv->length = 1; /* defaults */
502 if (!fr_hash_table_insert(vendors_byname, dv)) {
505 old_dv = fr_hash_table_finddata(vendors_byname, dv);
507 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
510 if (old_dv->vendorpec != dv->vendorpec) {
511 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
516 * Already inserted. Discard the duplicate entry.
523 * Insert the SAME pointer (not free'd when this table is
524 * deleted), into another table.
526 * We want this behaviour because we want OLD names for
527 * the attributes to be read from the configuration
528 * files, but when we're printing them, (and looking up
529 * by value) we want to use the NEW name.
531 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
532 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
541 * Add an attribute to the dictionary.
543 int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
547 static int max_attr = 0;
551 namelen = strlen(name);
552 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
553 fr_strerror_printf("dict_addattr: attribute name too long");
557 for (p = name; *p != '\0'; p++) {
559 fr_strerror_printf("dict_addattr: attribute name cannot contain control characters");
563 if ((*p == '"') || (*p == '\\')) {
564 fr_strerror_printf("dict_addattr: attribute name cannot contain quotation or backslash");
568 if ((*p == '<') || (*p == '>') || (*p == '&')) {
569 fr_strerror_printf("dict_addattr: attribute name cannot contain XML control characters");
575 !((type == PW_TYPE_INTEGER) || (type == PW_TYPE_STRING))) {
576 fr_strerror_printf("dict_addattr: Only 'integer' and 'string' attributes can have tags");
582 * If the attr is '-1', that means use a pre-existing
583 * one (if it already exists). If one does NOT already exist,
584 * then create a new attribute, with a non-conflicting value,
588 if (dict_attrbyname(name)) {
589 return 0; /* exists, don't add it again */
594 } else if (vendor == 0) {
598 if (attr > max_attr) {
604 * Additional checks for extended attributes.
606 if (flags.extended || flags.long_extended || flags.evs) {
607 if (vendor && (vendor < FR_MAX_VENDOR)) {
608 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
615 || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
616 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
622 if (!(flags.extended || flags.long_extended)) {
623 fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
627 /* VSAs cannot be of format EVS */
628 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
629 fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
635 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
639 if (flags.has_tlv && flags.length) {
640 fr_strerror_printf("TLVs cannot have a fixed length");
644 if ((vendor & (FR_MAX_VENDOR -1)) != 0) {
646 static DICT_VENDOR *last_vendor = NULL;
648 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
649 fr_strerror_printf("TLV's cannot be encrypted");
653 if (flags.is_tlv && flags.has_tag) {
654 fr_strerror_printf("Sub-TLV's cannot have a tag");
658 if (flags.has_tlv && flags.has_tag) {
659 fr_strerror_printf("TLV's cannot have a tag");
664 * Most ATTRIBUTEs are bunched together by
665 * VENDOR. We can save a lot of lookups on
666 * dictionary initialization by caching the last
670 ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
674 * Ignore the high byte (sigh)
676 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
681 * If the vendor isn't defined, die.
684 fr_strerror_printf("dict_addattr: Unknown vendor %u",
685 vendor & (FR_MAX_VENDOR - 1));
690 * FIXME: Switch over dv->type, and limit things
693 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
694 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
696 } /* else 256..65535 are allowed */
699 * If the attribute is in the standard space, AND
700 * has a sub-type (e.g. 241.1 or 255.3), then its
701 * number is placed into the upper 8 bits of the
704 * This also happens for the new VSAs.
706 * If we find it, then set the various flags
707 * based on what we see.
709 if (vendor >= FR_MAX_VENDOR) {
712 parent = (vendor / FR_MAX_VENDOR) & 0xff;
714 da = dict_attrbyvalue(parent, 0);
716 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown parent attribute %u.", parent);
721 * These flags are inhereited inherited
724 flags.extended = da->flags.extended;
725 flags.long_extended = da->flags.long_extended;
728 * Non-extended attributes can't have VSAs.
730 if (!flags.extended &&
731 ((vendor & (FR_MAX_VENDOR - 1)) != 0)) {
732 fr_strerror_printf("dict_addattr: ATTRIBUTE cannot be a VSA");
736 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
742 * <sigh> Alvarion, being *again* a horribly
743 * broken vendor, has re-used the WiMAX format in
744 * their proprietary vendor space. This re-use
745 * means that there are *multiple* conflicting
746 * Alvarion dictionaries.
748 flags.wimax = dv->flags;
752 * Create a new attribute for the list
754 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
755 fr_strerror_printf("dict_addattr: out of memory");
759 memcpy(da->name, name, namelen);
760 da->name[namelen] = '\0';
767 * Insert the attribute, only if it's not a duplicate.
769 if (!fr_hash_table_insert(attributes_byname, da)) {
773 * If the attribute has identical number, then
774 * ignore the duplicate.
776 a = fr_hash_table_finddata(attributes_byname, da);
777 if (a && (strcasecmp(a->name, da->name) == 0)) {
778 if (a->attr != da->attr) {
779 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
785 * Same name, same vendor, same attr,
786 * maybe the flags and/or type is
787 * different. Let the new value
788 * over-ride the old one.
793 fr_hash_table_delete(attributes_byvalue, a);
795 if (!fr_hash_table_replace(attributes_byname, da)) {
796 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
803 * Insert the SAME pointer (not free'd when this entry is
804 * deleted), into another table.
806 * We want this behaviour because we want OLD names for
807 * the attributes to be read from the configuration
808 * files, but when we're printing them, (and looking up
809 * by value) we want to use the NEW name.
811 if (!fr_hash_table_replace(attributes_byvalue, da)) {
812 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
816 if (!vendor && (attr > 0) && (attr < 256)) {
817 dict_base_attrs[attr] = da;
825 * Add a value for an attribute to the dictionary.
827 int dict_addvalue(const char *namestr, const char *attrstr, int value)
833 static DICT_ATTR *last_attr = NULL;
836 fr_strerror_printf("dict_addvalue: empty names are not permitted");
840 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
841 fr_strerror_printf("dict_addvalue: value name too long");
845 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
846 fr_strerror_printf("dict_addvalue: out of memory");
849 memset(dval, 0, sizeof(*dval));
851 strcpy(dval->name, namestr);
855 * Most VALUEs are bunched together by ATTRIBUTE. We can
856 * save a lot of lookups on dictionary initialization by
857 * caching the last attribute.
859 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
862 dattr = dict_attrbyname(attrstr);
867 * Remember which attribute is associated with this
868 * value, if possible.
871 if (dattr->flags.has_value_alias) {
872 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
876 dval->attr = dattr->attr;
877 dval->vendor = dattr->vendor;
880 * Enforce valid values
882 * Don't worry about fixups...
884 switch (dattr->type) {
888 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
895 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
901 * Allow octets for now, because
902 * of dictionary.cablelabs
906 case PW_TYPE_INTEGER:
909 case PW_TYPE_INTEGER64:
912 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
913 fr_int2str(dict_attr_types, dattr->type, "?Unknown?"));
917 dattr->flags.has_value = 1;
919 value_fixup_t *fixup;
921 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
924 fr_strerror_printf("dict_addvalue: out of memory");
927 memset(fixup, 0, sizeof(*fixup));
929 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
933 * Insert to the head of the list.
935 fixup->next = value_fixup;
942 * Add the value into the dictionary.
944 if (!fr_hash_table_insert(values_byname, dval)) {
949 * Suppress duplicates with the same
950 * name and value. There are lots in
953 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
954 if (old && (old->value == dval->value)) {
961 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
966 * There are multiple VALUE's, keyed by attribute, so we
967 * take care of that here.
969 if (!fr_hash_table_replace(values_byvalue, dval)) {
970 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
978 static int sscanf_i(const char *str, unsigned int *pvalue)
982 static const char *tab = "0123456789";
984 if ((str[0] == '0') &&
985 ((str[1] == 'x') || (str[1] == 'X'))) {
986 tab = "0123456789abcdef";
995 if (*str == '.') break;
997 c = memchr(tab, tolower((int) *str), base);
1011 * Get the OID based on various pieces of information.
1013 * Remember, the packing format is weird.
1015 * 00VID 000000AA normal VSA for vendor VID
1016 * 00VID AABBCCDD normal VSAs with TLVs
1017 * EE000 000000AA extended attr (241.1)
1018 * EE000 AABBCCDD extended attr with TLVs
1019 * EEVID 000000AA EVS with vendor VID, attr AAA
1020 * EEVID AABBCCDD EVS with TLVs
1022 * <whew>! Are we crazy, or what?
1024 int dict_str2oid(const char *ptr, unsigned int *pvalue, unsigned int *pvendor,
1031 if (tlv_depth > fr_attr_max_tlv) {
1032 fr_strerror_printf("Too many sub-attributes");
1037 * If *pvalue is set, check if the attribute exists.
1038 * Otherwise, check that the vendor exists.
1041 da = dict_attrbyvalue(*pvalue, *pvendor);
1043 fr_strerror_printf("Parent attribute is undefined.");
1047 if (!da->flags.has_tlv && !da->flags.extended) {
1048 fr_strerror_printf("Parent attribute %s cannot have sub-attributes",
1053 } else if ((*pvendor & (FR_MAX_VENDOR - 1)) != 0) {
1054 if (!dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1))) {
1055 fr_strerror_printf("Unknown vendor %u",
1056 *pvendor & (FR_MAX_VENDOR - 1));
1061 p = strchr(ptr, '.');
1064 * Look for 26.VID.x.y
1066 * If we find it, re-write the parameters, and recurse.
1068 if (!*pvendor && (tlv_depth == 0) && (*pvalue == PW_VENDOR_SPECIFIC)) {
1069 const DICT_VENDOR *dv;
1072 fr_strerror_printf("VSA needs to have sub-attribute");
1076 if (!sscanf_i(ptr, pvendor)) {
1077 fr_strerror_printf("Invalid number in attribute");
1081 if (*pvendor >= FR_MAX_VENDOR) {
1082 fr_strerror_printf("Cannot handle vendor ID larger than 2^24");
1087 dv = dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1));
1089 fr_strerror_printf("Unknown vendor \"%u\" ",
1090 *pvendor & (FR_MAX_VENDOR - 1));
1095 * Start off with (attr=0, vendor=VID), and
1096 * recurse. This causes the various checks above
1100 return dict_str2oid(p + 1, pvalue, pvendor, 0);
1103 if (!sscanf_i(ptr, &value)) {
1104 fr_strerror_printf("Invalid number in attribute");
1108 if (!*pvendor && (tlv_depth == 1) &&
1109 (da->flags.has_tlv || da->flags.extended)) {
1112 *pvendor = *pvalue * FR_MAX_VENDOR;
1116 return dict_str2oid(p + 1, pvalue, pvendor, 1);
1120 * And pack the data according to the scheme described in
1121 * the comments at the start of this function.
1124 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
1130 return dict_str2oid(p + 1, pvalue, pvendor, tlv_depth + 1);
1137 * Bamboo skewers under the fingernails in 5, 4, 3, 2, ...
1139 static DICT_ATTR *dict_parent(unsigned int attr, unsigned int vendor)
1141 if (vendor < FR_MAX_VENDOR) {
1142 return dict_attrbyvalue(attr & 0xff, vendor);
1146 return dict_attrbyvalue((vendor / FR_MAX_VENDOR) & 0xff, 0);
1149 return dict_attrbyvalue(attr & 0xff, vendor);
1154 * Process the ATTRIBUTE command
1156 static int process_attribute(const char* fn, const int line,
1157 unsigned int block_vendor, DICT_ATTR *block_tlv,
1158 int tlv_depth, char **argv, int argc)
1161 unsigned int vendor = 0;
1164 unsigned int length = 0;
1168 if ((argc < 3) || (argc > 4)) {
1169 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
1175 * Dictionaries need to have real names, not shitty ones.
1177 if (strncmp(argv[1], "Attr-", 5) == 0) {
1178 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1183 memset(&flags, 0, sizeof(flags));
1186 * Look for OIDs before doing anything else.
1188 p = strchr(argv[1], '.');
1192 * Validate all entries
1194 if (!sscanf_i(argv[1], &value)) {
1195 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1202 vendor = block_vendor;
1205 * Parse the rest of the OID.
1207 if (dict_str2oid(p + 1, &value, &vendor, tlv_depth + 1) < 0) {
1210 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1212 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1215 block_vendor = vendor;
1218 * Set the flags based on the parents flags.
1220 da = dict_parent(value, vendor);
1222 fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
1226 flags.extended = da->flags.extended;
1227 flags.long_extended = da->flags.long_extended;
1228 flags.evs = da->flags.evs;
1229 if (da->flags.has_tlv) flags.is_tlv = 1;
1232 if (strncmp(argv[2], "octets[", 7) != 0) {
1234 * find the type of the attribute.
1236 type = fr_str2int(dict_attr_types, argv[2], -1);
1238 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1244 type = PW_TYPE_OCTETS;
1246 p = strchr(argv[2] + 7, ']');
1248 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1254 if (!sscanf_i(argv[1], &length)) {
1255 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1259 if ((length == 0) || (length > 253)) {
1260 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1266 * Only look up the vendor if the string
1271 * Force "length" for data types of fixed length;
1283 case PW_TYPE_IPADDR:
1284 case PW_TYPE_INTEGER:
1285 case PW_TYPE_SIGNED:
1289 case PW_TYPE_INTEGER64:
1293 case PW_TYPE_ETHERNET:
1301 case PW_TYPE_IPV6ADDR:
1305 case PW_TYPE_EXTENDED:
1306 if ((vendor != 0) || (value < 241)) {
1307 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended\" MUST be RFC attributes with value >= 241.", fn, line);
1310 type = PW_TYPE_OCTETS;
1314 case PW_TYPE_LONG_EXTENDED:
1315 if ((vendor != 0) || (value < 241)) {
1316 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"long-extended\" MUST be RFC attributes with value >= 241.", fn, line);
1319 type = PW_TYPE_OCTETS;
1321 flags.long_extended = 1;
1325 type = PW_TYPE_OCTETS;
1328 if (value != PW_VENDOR_SPECIFIC) {
1329 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1338 flags.length = length;
1340 } else { /* argc == 4: we have options */
1341 char *key, *next, *last;
1346 if (flags.extended) {
1347 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1352 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1358 next = strchr(key, ',');
1359 if (next) *(next++) = '\0';
1361 if (strcmp(key, "has_tag") == 0 ||
1362 strcmp(key, "has_tag=1") == 0) {
1363 /* Boolean flag, means this is a
1367 } else if (strncmp(key, "encrypt=", 8) == 0) {
1368 /* Encryption method, defaults to 0 (none).
1369 Currently valid is just type 2,
1370 Tunnel-Password style, which can only
1371 be applied to strings. */
1372 flags.encrypt = strtol(key + 8, &last, 0);
1374 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1379 if ((flags.encrypt == FLAG_ENCRYPT_ASCEND_SECRET) &&
1380 (type != PW_TYPE_STRING)) {
1381 fr_strerror_printf( "dict_init: %s[%d] Only \"string\" types can have the \"encrypt=3\" flag set.",
1386 } else if (strncmp(key, "array", 6) == 0) {
1390 case PW_TYPE_IPADDR:
1393 case PW_TYPE_INTEGER:
1398 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1404 * The only thing is the vendor name,
1405 * and it's a known name: allow it.
1407 } else if ((key == argv[3]) && !next) {
1409 fr_strerror_printf( "dict_init: %s[%d] New-style attributes cannot use a vendor flag.",
1415 fr_strerror_printf( "dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" is not allowed.",
1420 vendor = dict_vendorbyname(key);
1421 if (!vendor) goto unknown;
1426 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1432 if (key && !*key) break;
1436 if (block_vendor) vendor = block_vendor;
1439 * Special checks for tags, they make our life much more
1442 if (flags.has_tag) {
1444 * Only string, octets, and integer can be tagged.
1447 case PW_TYPE_STRING:
1448 case PW_TYPE_INTEGER:
1452 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1454 fr_int2str(dict_attr_types, type, "?Unknown?"));
1459 if (type == PW_TYPE_TLV) {
1460 if (vendor && (vendor < FR_MAX_VENDOR)
1462 && (vendor != DHCP_MAGIC_VENDOR)
1467 dv = dict_vendorbyvalue(vendor);
1468 if (!dv || (dv->type != 1) || (dv->length != 1)) {
1469 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1480 * TLV's can be only one octet.
1482 if ((value == 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1483 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1491 value <<= fr_attr_shift[tlv_depth];
1492 value |= block_tlv->attr;
1496 #ifdef WITH_DICTIONARY_WARNINGS
1498 * Hack to help us discover which vendors have illegal
1501 if (!vendor && (value < 256) &&
1502 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1503 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1511 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1514 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1516 fr_strerror_printf("dict_init: %s[%d]: %s",
1526 * Process the VALUE command
1528 static int process_value(const char* fn, const int line, char **argv,
1534 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1539 * For Compatibility, skip "Server-Config"
1541 if (strcasecmp(argv[0], "Server-Config") == 0)
1545 * Validate all entries
1547 if (!sscanf_i(argv[2], &value)) {
1548 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1553 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1556 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1558 fr_strerror_printf("dict_init: %s[%d]: %s",
1568 * Process the VALUE-ALIAS command
1570 * This allows VALUE mappings to be shared among multiple
1573 static int process_value_alias(const char* fn, const int line, char **argv,
1576 DICT_ATTR *my_da, *da;
1580 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1585 my_da = dict_attrbyname(argv[0]);
1587 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1592 if (my_da->flags.has_value) {
1593 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1598 if (my_da->flags.has_value_alias) {
1599 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1604 da = dict_attrbyname(argv[1]);
1606 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1611 if (!da->flags.has_value) {
1612 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1617 if (da->flags.has_value_alias) {
1618 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1623 if (my_da->type != da->type) {
1624 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1629 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1630 fr_strerror_printf("dict_addvalue: out of memory");
1634 dval->name[0] = '\0'; /* empty name */
1635 dval->attr = my_da->attr;
1636 dval->vendor = my_da->vendor;
1637 dval->value = da->attr;
1639 if (!fr_hash_table_insert(values_byname, dval)) {
1640 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1651 * Process the VENDOR command
1653 static int process_vendor(const char* fn, const int line, char **argv,
1657 int continuation = 0;
1658 const char *format = NULL;
1660 if ((argc < 2) || (argc > 3)) {
1661 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1667 * Validate all entries
1669 if (!isdigit((int) argv[1][0])) {
1670 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1674 value = atoi(argv[1]);
1676 /* Create a new VENDOR entry for the list */
1677 if (dict_addvendor(argv[0], value) < 0) {
1680 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1682 fr_strerror_printf("dict_init: %s[%d]: %s",
1688 * Look for a format statement
1693 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1694 format = "format=4,0";
1696 } else if (value == VENDORPEC_LUCENT) {
1697 format = "format=2,1";
1699 } else if (value == VENDORPEC_STARENT) {
1700 format = "format=2,2";
1702 } /* else no fixups to do */
1709 if (strncasecmp(format, "format=", 7) != 0) {
1710 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1716 if ((strlen(p) < 3) ||
1717 !isdigit((int) p[0]) ||
1719 !isdigit((int) p[2]) ||
1720 (p[3] && (p[3] != ','))) {
1721 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1726 type = (int) (p[0] - '0');
1727 length = (int) (p[2] - '0');
1731 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1736 if ((p[4] != 'c') ||
1738 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1744 if ((value != VENDORPEC_WIMAX) ||
1745 (type != 1) || (length != 1)) {
1746 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1752 dv = dict_vendorbyvalue(value);
1754 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1759 if ((type != 1) && (type != 2) && (type != 4)) {
1760 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1765 if ((length != 0) && (length != 1) && (length != 2)) {
1766 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1772 dv->length = length;
1773 dv->flags = continuation;
1780 * String split routine. Splits an input string IN PLACE
1781 * into pieces, based on spaces.
1783 int str2argv(char *str, char **argv, int max_argc)
1788 if (argc >= max_argc) break;
1791 * Chop out comments early.
1798 while ((*str == ' ') ||
1801 (*str == '\n')) *(str++) = '\0';
1812 (*str != '\n')) str++;
1818 #define MAX_ARGV (16)
1821 * Initialize the dictionary.
1823 static int my_dict_init(const char *dir, const char *fn,
1824 const char *src_file, int src_line)
1831 unsigned int vendor;
1832 unsigned int block_vendor;
1833 struct stat statbuf;
1834 char *argv[MAX_ARGV];
1836 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1837 int which_block_tlv = 0;
1839 block_tlv[0] = NULL;
1840 block_tlv[1] = NULL;
1841 block_tlv[2] = NULL;
1842 block_tlv[3] = NULL;
1844 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1845 strlen(dir) >= sizeof(dirtmp) / 2) {
1846 fr_strerror_printf("dict_init: filename name too long");
1851 * First see if fn is relative to dir. If so, create
1852 * new filename. If not, remember the absolute dir.
1854 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1858 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1859 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1863 if ((fp = fopen(fn, "r")) == NULL) {
1865 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1866 fn, strerror(errno));
1868 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1869 src_file, src_line, fn, strerror(errno));
1874 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1875 if (!S_ISREG(statbuf.st_mode)) {
1877 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1883 * Globally writable dictionaries means that users can control
1884 * the server configuration with little difficulty.
1887 if ((statbuf.st_mode & S_IWOTH) != 0) {
1889 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1895 dict_stat_add(fn, &statbuf);
1898 * Seed the random pool with data.
1900 fr_rand_seed(&statbuf, sizeof(statbuf));
1904 while (fgets(buf, sizeof(buf), fp) != NULL) {
1906 if (buf[0] == '#' || buf[0] == 0 ||
1907 buf[0] == '\n' || buf[0] == '\r')
1911 * Comment characters should NOT be appearing anywhere but
1912 * as start of a comment;
1914 p = strchr(buf, '#');
1917 argc = str2argv(buf, argv, MAX_ARGV);
1918 if (argc == 0) continue;
1921 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1928 * Process VALUE lines.
1930 if (strcasecmp(argv[0], "VALUE") == 0) {
1931 if (process_value(fn, line,
1932 argv + 1, argc - 1) == -1) {
1940 * Perhaps this is an attribute.
1942 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1943 if (process_attribute(fn, line, block_vendor,
1944 block_tlv[which_block_tlv],
1946 argv + 1, argc - 1) == -1) {
1954 * See if we need to import another dictionary.
1956 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1957 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1965 * Optionally include a dictionary
1967 if (strcasecmp(argv[0], "$INCLUDE-") == 0) {
1968 int rcode = my_dict_init(dir, argv[1], fn, line);
1970 if (rcode == -2) continue;
1979 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1980 if (process_value_alias(fn, line,
1981 argv + 1, argc - 1) == -1) {
1989 * Process VENDOR lines.
1991 if (strcasecmp(argv[0], "VENDOR") == 0) {
1992 if (process_vendor(fn, line,
1993 argv + 1, argc - 1) == -1) {
2000 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
2003 "dict_init: %s[%d] invalid BEGIN-TLV entry",
2009 da = dict_attrbyname(argv[1]);
2012 "dict_init: %s[%d]: unknown attribute %s",
2018 if (da->type != PW_TYPE_TLV) {
2020 "dict_init: %s[%d]: attribute %s is not of type tlv",
2026 if (which_block_tlv >= MAX_TLV_NEST) {
2028 "dict_init: %s[%d]: TLVs are nested too deep",
2035 block_tlv[++which_block_tlv] = da;
2039 if (strcasecmp(argv[0], "END-TLV") == 0) {
2042 "dict_init: %s[%d] invalid END-TLV entry",
2048 da = dict_attrbyname(argv[1]);
2051 "dict_init: %s[%d]: unknown attribute %s",
2057 if (da != block_tlv[which_block_tlv]) {
2059 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
2064 block_tlv[which_block_tlv--] = NULL;
2068 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
2071 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
2077 vendor = dict_vendorbyname(argv[1]);
2080 "dict_init: %s[%d]: unknown vendor %s",
2086 block_vendor = vendor;
2089 * Check for extended attr VSAs
2091 * BEGIN-VENDOR foo format=Foo-Encapsulation-Attr
2094 if (strncmp(argv[2], "format=", 7) != 0) {
2096 "dict_init: %s[%d]: Invalid format %s",
2103 da = dict_attrbyname(p);
2105 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
2111 if (!da->flags.evs) {
2112 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
2119 * Pack the encapsulating
2120 * attribute into the upper 8
2121 * bits of the vendor ID
2123 block_vendor |= (da->attr & fr_attr_mask[0]) * FR_MAX_VENDOR;
2127 } /* BEGIN-VENDOR */
2129 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
2132 "dict_init: %s[%d] invalid END-VENDOR entry",
2138 vendor = dict_vendorbyname(argv[1]);
2141 "dict_init: %s[%d]: unknown vendor %s",
2147 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2149 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2159 * Any other string: We don't recognize it.
2161 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2172 * Empty callback for hash table initialization.
2174 static int null_callback(void *ctx, void *data)
2176 ctx = ctx; /* -Wunused */
2177 data = data; /* -Wunused */
2184 * Initialize the directory, then fix the attr member of
2187 int dict_init(const char *dir, const char *fn)
2190 * Check if we need to change anything. If not, don't do
2193 if (dict_stat_check(dir, fn)) {
2198 * Free the dictionaries, and the stat cache.
2201 stat_root_dir = strdup(dir);
2202 stat_root_file = strdup(fn);
2205 * Create the table of vendor by name. There MAY NOT
2206 * be multiple vendors of the same name.
2208 * Each vendor is malloc'd, so the free function is free.
2210 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2211 dict_vendor_name_cmp,
2213 if (!vendors_byname) {
2218 * Create the table of vendors by value. There MAY
2219 * be vendors of the same value. If there are, we
2220 * pick the latest one.
2222 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2223 dict_vendor_value_cmp,
2225 if (!vendors_byvalue) {
2230 * Create the table of attributes by name. There MAY NOT
2231 * be multiple attributes of the same name.
2233 * Each attribute is malloc'd, so the free function is free.
2235 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2238 if (!attributes_byname) {
2243 * Create the table of attributes by value. There MAY
2244 * be attributes of the same value. If there are, we
2245 * pick the latest one.
2247 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2248 dict_attr_value_cmp,
2250 if (!attributes_byvalue) {
2254 values_byname = fr_hash_table_create(dict_value_name_hash,
2255 dict_value_name_cmp,
2257 if (!values_byname) {
2261 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2262 dict_value_value_cmp,
2264 if (!values_byvalue) {
2268 value_fixup = NULL; /* just to be safe. */
2270 if (my_dict_init(dir, fn, NULL, 0) < 0)
2275 value_fixup_t *this, *next;
2277 for (this = value_fixup; this != NULL; this = next) {
2280 a = dict_attrbyname(this->attrstr);
2283 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2284 this->attrstr, this->dval->name);
2285 return -1; /* leak, but they should die... */
2288 this->dval->attr = a->attr;
2291 * Add the value into the dictionary.
2293 if (!fr_hash_table_replace(values_byname,
2295 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2300 * Allow them to use the old name, but
2301 * prefer the new name when printing
2304 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2305 fr_hash_table_replace(values_byvalue,
2311 * Just so we don't lose track of things.
2318 * Walk over all of the hash tables to ensure they're
2319 * initialized. We do this because the threads may perform
2320 * lookups, and we don't want multi-threaded re-ordering
2321 * of the table entries. That would be bad.
2323 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2324 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2326 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2327 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2329 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2330 fr_hash_table_walk(values_byname, null_callback, NULL);
2336 * Get an attribute by its numerical value.
2338 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2342 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2345 dattr.vendor = vendor;
2347 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2351 * Get an attribute by its name.
2353 DICT_ATTR *dict_attrbyname(const char *name)
2356 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2358 if (!name) return NULL;
2360 da = (DICT_ATTR *) buffer;
2361 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2363 return fr_hash_table_finddata(attributes_byname, da);
2367 * Associate a value with an attribute and return it.
2369 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2371 DICT_VALUE dval, *dv;
2374 * First, look up aliases.
2377 dval.vendor = vendor;
2378 dval.name[0] = '\0';
2381 * Look up the attribute alias target, and use
2382 * the correct attribute number if found.
2384 dv = fr_hash_table_finddata(values_byname, &dval);
2385 if (dv) dval.attr = dv->value;
2389 return fr_hash_table_finddata(values_byvalue, &dval);
2393 * Associate a value with an attribute and return it.
2395 const char *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value)
2399 dv = dict_valbyattr(attr, vendor, value);
2406 * Get a value by its name, keyed off of an attribute.
2408 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2410 DICT_VALUE *my_dv, *dv;
2411 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2413 if (!name) return NULL;
2415 my_dv = (DICT_VALUE *) buffer;
2417 my_dv->vendor = vendor;
2418 my_dv->name[0] = '\0';
2421 * Look up the attribute alias target, and use
2422 * the correct attribute number if found.
2424 dv = fr_hash_table_finddata(values_byname, my_dv);
2425 if (dv) my_dv->attr = dv->value;
2427 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2429 return fr_hash_table_finddata(values_byname, my_dv);
2433 * Get the vendor PEC based on the vendor name
2435 * This is efficient only for small numbers of vendors.
2437 int dict_vendorbyname(const char *name)
2440 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
2442 if (!name) return 0;
2444 dv = (DICT_VENDOR *) buffer;
2445 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
2447 dv = fr_hash_table_finddata(vendors_byname, dv);
2450 return dv->vendorpec;
2454 * Return the vendor struct based on the PEC.
2456 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
2460 dv.vendorpec = vendorpec;
2462 return fr_hash_table_finddata(vendors_byvalue, &dv);