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
24 #include <freeradius-devel/libradius.h>
27 #include <freeradius-devel/dhcp.h>
36 #ifdef HAVE_SYS_STAT_H
40 static fr_hash_table_t *vendors_byname = NULL;
41 static fr_hash_table_t *vendors_byvalue = NULL;
43 static fr_hash_table_t *attributes_byname = NULL;
44 static fr_hash_table_t *attributes_byvalue = NULL;
46 static fr_hash_table_t *attributes_combo = NULL;
48 static fr_hash_table_t *values_byvalue = NULL;
49 static fr_hash_table_t *values_byname = NULL;
51 static DICT_ATTR *dict_base_attrs[256];
54 * For faster HUP's, we cache the stat information for
55 * files we've $INCLUDEd
57 typedef struct dict_stat_t {
58 struct dict_stat_t *next;
62 static dict_stat_t *stat_head = NULL;
63 static dict_stat_t *stat_tail = NULL;
65 typedef struct value_fixup_t {
66 char attrstr[DICT_ATTR_MAX_NAME_LEN];
68 struct value_fixup_t *next;
73 * So VALUEs in the dictionary can have forward references.
75 static value_fixup_t *value_fixup = NULL;
77 const FR_NAME_NUMBER dict_attr_types[] = {
78 { "integer", PW_TYPE_INTEGER },
79 { "string", PW_TYPE_STRING },
80 { "ipaddr", PW_TYPE_IPV4_ADDR },
81 { "date", PW_TYPE_DATE },
82 { "abinary", PW_TYPE_ABINARY },
83 { "octets", PW_TYPE_OCTETS },
84 { "ifid", PW_TYPE_IFID },
85 { "ipv6addr", PW_TYPE_IPV6_ADDR },
86 { "ipv6prefix", PW_TYPE_IPV6_PREFIX },
87 { "byte", PW_TYPE_BYTE },
88 { "short", PW_TYPE_SHORT },
89 { "ether", PW_TYPE_ETHERNET },
90 { "combo-ip", PW_TYPE_COMBO_IP_ADDR },
91 { "tlv", PW_TYPE_TLV },
92 { "signed", PW_TYPE_SIGNED },
93 { "extended", PW_TYPE_EXTENDED },
94 { "long-extended", PW_TYPE_LONG_EXTENDED },
95 { "evs", PW_TYPE_EVS },
96 { "uint8", PW_TYPE_BYTE },
97 { "uint16", PW_TYPE_SHORT },
98 { "uint32", PW_TYPE_INTEGER },
99 { "int32", PW_TYPE_SIGNED },
100 { "integer64", PW_TYPE_INTEGER64 },
101 { "uint64", PW_TYPE_INTEGER64 },
102 { "ipv4prefix", PW_TYPE_IPV4_PREFIX },
103 { "cidr", PW_TYPE_IPV4_PREFIX },
104 { "vsa", PW_TYPE_VSA },
109 * Map data types to min / max data sizes.
111 const size_t dict_attr_sizes[PW_TYPE_MAX][2] = {
112 [PW_TYPE_INVALID] = {~0, 0},
113 [PW_TYPE_STRING] = {0, ~0},
114 [PW_TYPE_INTEGER] = {4, 4 },
115 [PW_TYPE_IPV4_ADDR] = {4, 4},
116 [PW_TYPE_DATE] = {4, 4},
117 [PW_TYPE_ABINARY] = {32, ~0},
118 [PW_TYPE_OCTETS] = {0, ~0},
119 [PW_TYPE_IFID] = {8, 8},
120 [PW_TYPE_IPV6_ADDR] = {16, 16},
121 [PW_TYPE_IPV6_PREFIX] = {2, 18},
122 [PW_TYPE_BYTE] = {1, 1},
123 [PW_TYPE_SHORT] = {2, 2},
124 [PW_TYPE_ETHERNET] = {6, 6},
125 [PW_TYPE_SIGNED] = {4, 4},
126 [PW_TYPE_COMBO_IP_ADDR] = {4, 16},
127 [PW_TYPE_TLV] = {2, ~0},
128 [PW_TYPE_EXTENDED] = {2, ~0},
129 [PW_TYPE_LONG_EXTENDED] = {3, ~0},
130 [PW_TYPE_EVS] = {6, ~0},
131 [PW_TYPE_INTEGER64] = {8, 8},
132 [PW_TYPE_IPV4_PREFIX] = {6, 6},
133 [PW_TYPE_VSA] = {4, ~0}
137 * For packing multiple TLV numbers into one 32-bit integer. The
138 * first 3 bytes are just the 8-bit number. The next two are
139 * more limited. We only allow 31 attributes nested 3 layers
140 * deep, and only 7 nested 4 layers deep. This should be
141 * sufficient for most purposes.
143 * For TLVs and extended attributes, we packet the base attribute
144 * number into the upper 8 bits of the "vendor" field.
146 * e.g. OID attribute vendor
147 * 241.1 1 (241 << 24)
148 * 241.26.9.1 1 (241 << 24) | (9)
149 * 241.1.2 1 | (2 << 8) (241 << 24)
151 #define MAX_TLV_NEST (4)
154 * 8 bits of base attribute
155 * 8 bits for nested TLV 1
156 * 8 bits for nested TLV 2
157 * 5 bits for nested TLV 3
158 * 3 bits for nested TLV 4
160 int const fr_attr_max_tlv = MAX_TLV_NEST;
161 int const fr_attr_shift[MAX_TLV_NEST + 1] = { 0, 8, 16, 24, 29 };
163 int const fr_attr_mask[MAX_TLV_NEST + 1] = { 0xff, 0xff, 0xff, 0x1f, 0x07 };
166 * attr & fr_attr_parent_mask[i] == Nth parent of attr
168 static unsigned int const fr_attr_parent_mask[MAX_TLV_NEST + 1] = { 0, 0x000000ff, 0x0000ffff, 0x00ffffff, 0x1fffffff };
171 * Create the hash of the name.
173 * We copy the hash function here because it's substantially faster.
175 #define FNV_MAGIC_INIT (0x811c9dc5)
176 #define FNV_MAGIC_PRIME (0x01000193)
178 static uint32_t dict_hashname(char const *name)
180 uint32_t hash = FNV_MAGIC_INIT;
183 for (p = name; *p != '\0'; p++) {
184 int c = *(unsigned char const *) p;
185 if (isalpha(c)) c = tolower(c);
187 hash *= FNV_MAGIC_PRIME;
188 hash ^= (uint32_t ) (c & 0xff);
196 * Hash callback functions.
198 static uint32_t dict_attr_name_hash(void const *data)
200 return dict_hashname(((DICT_ATTR const *)data)->name);
203 static int dict_attr_name_cmp(void const *one, void const *two)
205 DICT_ATTR const *a = one;
206 DICT_ATTR const *b = two;
208 return strcasecmp(a->name, b->name);
211 static uint32_t dict_attr_value_hash(void const *data)
214 DICT_ATTR const *attr = data;
216 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
217 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
220 static int dict_attr_value_cmp(void const *one, void const *two)
222 DICT_ATTR const *a = one;
223 DICT_ATTR const *b = two;
225 if (a->vendor < b->vendor) return -1;
226 if (a->vendor > b->vendor) return +1;
228 return a->attr - b->attr;
231 static uint32_t dict_attr_combo_hash(void const *data)
234 DICT_ATTR const *attr = data;
236 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
237 hash = fr_hash_update(&attr->type, sizeof(attr->type), hash);
238 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
241 static int dict_attr_combo_cmp(void const *one, void const *two)
243 DICT_ATTR const *a = one;
244 DICT_ATTR const *b = two;
246 if (a->type < b->type) return -1;
247 if (a->type > b->type) return +1;
249 if (a->vendor < b->vendor) return -1;
250 if (a->vendor > b->vendor) return +1;
252 return a->attr - b->attr;
255 static uint32_t dict_vendor_name_hash(void const *data)
257 return dict_hashname(((DICT_VENDOR const *)data)->name);
260 static int dict_vendor_name_cmp(void const *one, void const *two)
262 DICT_VENDOR const *a = one;
263 DICT_VENDOR const *b = two;
265 return strcasecmp(a->name, b->name);
268 static uint32_t dict_vendor_value_hash(void const *data)
270 return fr_hash(&(((DICT_VENDOR const *)data)->vendorpec),
271 sizeof(((DICT_VENDOR const *)data)->vendorpec));
274 static int dict_vendor_value_cmp(void const *one, void const *two)
276 DICT_VENDOR const *a = one;
277 DICT_VENDOR const *b = two;
279 return a->vendorpec - b->vendorpec;
282 static uint32_t dict_value_name_hash(void const *data)
285 DICT_VALUE const *dval = data;
287 hash = dict_hashname(dval->name);
288 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
289 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
292 static int dict_value_name_cmp(void const *one, void const *two)
295 DICT_VALUE const *a = one;
296 DICT_VALUE const *b = two;
298 rcode = a->attr - b->attr;
299 if (rcode != 0) return rcode;
301 rcode = a->vendor - b->vendor;
302 if (rcode != 0) return rcode;
304 return strcasecmp(a->name, b->name);
307 static uint32_t dict_value_value_hash(void const *data)
310 DICT_VALUE const *dval = data;
312 hash = fr_hash(&dval->attr, sizeof(dval->attr));
313 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
314 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
317 static int dict_value_value_cmp(void const *one, void const *two)
320 DICT_VALUE const *a = one;
321 DICT_VALUE const *b = two;
323 if (a->vendor < b->vendor) return -1;
324 if (a->vendor > b->vendor) return +1;
326 rcode = a->attr - b->attr;
327 if (rcode != 0) return rcode;
329 return a->value - b->value;
334 * Free the list of stat buffers
336 static void dict_stat_free(void)
338 dict_stat_t *this, *next;
345 for (this = stat_head; this != NULL; this = next) {
350 stat_head = stat_tail = NULL;
355 * Add an entry to the list of stat buffers.
357 static void dict_stat_add(struct stat const *stat_buf)
361 this = malloc(sizeof(*this));
363 memset(this, 0, sizeof(*this));
365 memcpy(&(this->stat_buf), stat_buf, sizeof(this->stat_buf));
368 stat_head = stat_tail = this;
370 stat_tail->next = this;
377 * See if any dictionaries have changed. If not, don't
380 static int dict_stat_check(char const *dir, char const *file)
382 struct stat stat_buf;
387 * Nothing cached, all files are new.
389 if (!stat_head) return 0;
394 snprintf(buffer, sizeof(buffer), "%s/%s", dir, file);
395 if (stat(buffer, &stat_buf) < 0) return 0;
398 * Find the cache entry.
399 * FIXME: use a hash table.
400 * FIXME: check dependencies, via children.
401 * if A loads B and B changes, we probably want
402 * to reload B at the minimum.
404 for (this = stat_head; this != NULL; this = this->next) {
405 if (this->stat_buf.st_dev != stat_buf.st_dev) continue;
406 if (this->stat_buf.st_ino != stat_buf.st_ino) continue;
409 * The file has changed. Re-read it.
411 if (this->stat_buf.st_mtime < stat_buf.st_mtime) return 0;
414 * The file is the same. Ignore it.
425 typedef struct fr_pool_t {
428 struct fr_pool_t *page_free;
429 struct fr_pool_t *page_next;
432 #define FR_POOL_SIZE (32768)
433 #define FR_ALLOC_ALIGN (8)
435 static fr_pool_t *dict_pool = NULL;
437 static fr_pool_t *fr_pool_create(void)
439 fr_pool_t *fp = malloc(FR_POOL_SIZE);
441 if (!fp) return NULL;
443 memset(fp, 0, FR_POOL_SIZE);
445 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
446 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
448 fp->page_next = NULL;
452 static void fr_pool_delete(fr_pool_t **pfp)
454 fr_pool_t *fp, *next;
456 if (!pfp || !*pfp) return;
458 for (fp = *pfp; fp != NULL; fp = next) {
459 next = fp->page_next;
460 fp->page_next = NULL;
467 static void *fr_pool_alloc(size_t size)
471 if (size == 0) return NULL;
473 if (size > 256) return NULL; /* shouldn't happen */
476 dict_pool = fr_pool_create();
477 if (!dict_pool) return NULL;
480 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
481 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
484 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
485 dict_pool->page_free->page_next = fr_pool_create();
486 if (!dict_pool->page_free->page_next) return NULL;
487 dict_pool->page_free = dict_pool->page_free->page_next;
490 ptr = dict_pool->page_free->free_ptr;
491 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
497 static void fr_pool_free(UNUSED void *ptr)
500 * Place-holder for later code.
505 * Free the dictionary_attributes and dictionary_values lists.
512 fr_hash_table_free(vendors_byname);
513 fr_hash_table_free(vendors_byvalue);
514 vendors_byname = NULL;
515 vendors_byvalue = NULL;
517 fr_hash_table_free(attributes_byname);
518 fr_hash_table_free(attributes_byvalue);
519 fr_hash_table_free(attributes_combo);
520 attributes_byname = NULL;
521 attributes_byvalue = NULL;
522 attributes_combo = NULL;
524 fr_hash_table_free(values_byname);
525 fr_hash_table_free(values_byvalue);
526 values_byname = NULL;
527 values_byvalue = NULL;
529 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
531 fr_pool_delete(&dict_pool);
537 * Add vendor to the list.
539 int dict_addvendor(char const *name, unsigned int value)
544 if (value >= FR_MAX_VENDOR) {
545 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
549 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
550 fr_strerror_printf("dict_addvendor: vendor name too long");
554 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
555 fr_strerror_printf("dict_addvendor: out of memory");
559 strcpy(dv->name, name);
560 dv->vendorpec = value;
561 dv->type = dv->length = 1; /* defaults */
563 if (!fr_hash_table_insert(vendors_byname, dv)) {
566 old_dv = fr_hash_table_finddata(vendors_byname, dv);
568 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
571 if (old_dv->vendorpec != dv->vendorpec) {
572 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
577 * Already inserted. Discard the duplicate entry.
584 * Insert the SAME pointer (not free'd when this table is
585 * deleted), into another table.
587 * We want this behaviour because we want OLD names for
588 * the attributes to be read from the configuration
589 * files, but when we're printing them, (and looking up
590 * by value) we want to use the NEW name.
592 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
593 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
601 const int dict_attr_allowed_chars[256] = {
602 /* 0x 0 1 2 3 4 5 6 7 8 9 a b c d e f */
603 /* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
604 /* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
605 /* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
606 /* 3 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
607 /* 4 */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
608 /* 5 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
609 /* 6 */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
610 /* 7 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
611 /* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
612 /* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
613 /* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
614 /* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
615 /* c */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
616 /* d */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
617 /* e */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
618 /* f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
624 int dict_valid_name(char const *name)
628 for (p = (uint8_t const *) name; *p != '\0'; p++) {
629 if (!dict_attr_allowed_chars[*p]) {
632 fr_prints(buff, sizeof(buff), (char const *)p, 1, '\'');
633 fr_strerror_printf("Invalid character '%s' in attribute", buff);
635 return -(p - (uint8_t const *)name);
642 /** Add an attribute to the dictionary
644 * @return 0 on success -1 on failure.
646 int dict_addattr(char const *name, int attr, unsigned int vendor, PW_TYPE type,
650 static int max_attr = 0;
654 namelen = strlen(name);
655 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
656 fr_strerror_printf("dict_addattr: attribute name too long");
660 if (dict_valid_name(name) < 0) return -1;
663 !((type == PW_TYPE_INTEGER) || (type == PW_TYPE_STRING))) {
664 fr_strerror_printf("dict_addattr: Only 'integer' and 'string' attributes can have tags");
669 * Disallow attributes of type zero.
671 if (!attr && !vendor) {
672 fr_strerror_printf("dict_addattr: Attribute 0 is invalid and cannot be used");
677 * If the attr is '-1', that means use a pre-existing
678 * one (if it already exists). If one does NOT already exist,
679 * then create a new attribute, with a non-conflicting value,
683 if (dict_attrbyname(name)) {
684 return 0; /* exists, don't add it again */
689 } else if (vendor == 0) {
693 if (attr > max_attr) {
699 * Additional checks for extended attributes.
701 if (flags.extended || flags.long_extended || flags.evs) {
702 if (vendor && (vendor < FR_MAX_VENDOR)) {
703 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats");
710 || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
711 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set");
717 if (!(flags.extended || flags.long_extended)) {
718 fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
722 /* VSAs cannot be of format EVS */
723 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
724 fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
730 * Do various sanity checks.
733 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
737 if (flags.has_tlv && flags.length) {
738 fr_strerror_printf("TLVs cannot have a fixed length");
742 if (vendor && flags.concat) {
743 fr_strerror_printf("VSAs cannot have the \"concat\" flag set");
747 if (flags.concat && (type != PW_TYPE_OCTETS)) {
748 fr_strerror_printf("The \"concat\" flag can only be set for attributes of type \"octets\"");
752 if (flags.concat && (flags.has_tag || flags.array || flags.is_tlv || flags.has_tlv ||
753 flags.length || flags.evs || flags.extended || flags.long_extended ||
754 (flags.encrypt != FLAG_ENCRYPT_NONE))) {
755 fr_strerror_printf("The \"concat\" flag cannot be used with any other flag");
759 if (flags.length && (type != PW_TYPE_OCTETS)) {
760 fr_strerror_printf("The \"length\" flag can only be set for attributes of type \"octets\"");
764 if (flags.length && (flags.has_tag || flags.array || flags.is_tlv || flags.has_tlv ||
765 flags.concat || flags.evs || flags.extended || flags.long_extended ||
766 (flags.encrypt > FLAG_ENCRYPT_USER_PASSWORD))) {
767 fr_strerror_printf("The \"length\" flag cannot be used with any other flag");
772 * Force "length" for data types of fixed length;
784 case PW_TYPE_IPV4_ADDR:
785 case PW_TYPE_INTEGER:
790 case PW_TYPE_INTEGER64:
794 case PW_TYPE_ETHERNET:
802 case PW_TYPE_IPV6_ADDR:
806 case PW_TYPE_EXTENDED:
807 if ((vendor != 0) || (attr < 241)) {
808 fr_strerror_printf("Attributes of type \"extended\" MUST be "
809 "RFC attributes with value >= 241.");
817 case PW_TYPE_LONG_EXTENDED:
818 if ((vendor != 0) || (attr < 241)) {
819 fr_strerror_printf("Attributes of type \"long-extended\" MUST "
820 "be RFC attributes with value >= 241.");
826 flags.long_extended = 1;
830 if (attr != PW_VENDOR_SPECIFIC) {
831 fr_strerror_printf("Attributes of type \"evs\" MUST have "
832 "attribute code 26.");
844 flags.is_pointer = true;
852 * Stupid hacks for MS-CHAP-MPPE-Keys. The User-Password
853 * encryption method has no provisions for encoding the
854 * length of the data. For User-Password, the data is
855 * (presumably) all printable non-zero data. For
856 * MS-CHAP-MPPE-Keys, the data is binary crap. So... we
857 * MUST specify a length in the dictionary.
859 if ((flags.encrypt == FLAG_ENCRYPT_USER_PASSWORD) && (type != PW_TYPE_STRING)) {
860 if (type != PW_TYPE_OCTETS) {
861 fr_strerror_printf("The \"encrypt=1\" flag cannot be used with non-string data types");
865 if (flags.length == 0) {
866 fr_strerror_printf("The \"encrypt=1\" flag MUST be used with an explicit length for 'octets' data types");
871 if ((vendor & (FR_MAX_VENDOR -1)) != 0) {
873 static DICT_VENDOR *last_vendor = NULL;
875 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
876 fr_strerror_printf("TLV's cannot be encrypted");
880 if (flags.is_tlv && flags.has_tag) {
881 fr_strerror_printf("Sub-TLV's cannot have a tag");
885 if (flags.has_tlv && flags.has_tag) {
886 fr_strerror_printf("TLV's cannot have a tag");
891 * Most ATTRIBUTEs are bunched together by
892 * VENDOR. We can save a lot of lookups on
893 * dictionary initialization by caching the last
897 ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
901 * Ignore the high byte (sigh)
903 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
908 * If the vendor isn't defined, die.
911 fr_strerror_printf("dict_addattr: Unknown vendor %u",
912 vendor & (FR_MAX_VENDOR - 1));
916 if (!attr && dv->type != 1) {
917 fr_strerror_printf("dict_addattr: Attribute %s cannot have value zero",
923 * FIXME: Switch over dv->type, and limit things
926 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
927 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255)");
929 } /* else 256..65535 are allowed */
932 * If the attribute is in the standard space, AND
933 * has a sub-type (e.g. 241.1 or 255.3), then its
934 * number is placed into the upper 8 bits of the
937 * This also happens for the new VSAs.
939 * If we find it, then set the various flags
940 * based on what we see.
942 if (vendor >= FR_MAX_VENDOR) {
945 parent = (vendor / FR_MAX_VENDOR) & 0xff;
947 da = dict_attrbyvalue(parent, 0);
949 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown parent attribute %u.", parent);
954 * These flags are inherited from the
957 flags.extended = da->flags.extended;
958 flags.long_extended = da->flags.long_extended;
961 * Non-extended attributes can't have VSAs.
963 if (!flags.extended &&
964 ((vendor & (FR_MAX_VENDOR - 1)) != 0)) {
965 fr_strerror_printf("dict_addattr: ATTRIBUTE cannot be a VSA");
969 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
975 * <sigh> Alvarion, being *again* a horribly
976 * broken vendor, has re-used the WiMAX format in
977 * their proprietary vendor space. This re-use
978 * means that there are *multiple* conflicting
979 * Alvarion dictionaries.
981 flags.wimax = dv->flags;
985 * Create a new attribute for the list
987 if ((n = fr_pool_alloc(sizeof(*n) + namelen)) == NULL) {
989 fr_strerror_printf("dict_addattr: out of memory");
993 memcpy(n->name, name, namelen);
994 n->name[namelen] = '\0';
1001 * Insert the attribute, only if it's not a duplicate.
1003 if (!fr_hash_table_insert(attributes_byname, n)) {
1007 * If the attribute has identical number, then
1008 * ignore the duplicate.
1010 a = fr_hash_table_finddata(attributes_byname, n);
1011 if (a && (strcasecmp(a->name, n->name) == 0)) {
1012 if (a->attr != n->attr) {
1013 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
1019 * Same name, same vendor, same attr,
1020 * maybe the flags and/or type is
1021 * different. Let the new value
1022 * over-ride the old one.
1027 fr_hash_table_delete(attributes_byvalue, a);
1029 if (!fr_hash_table_replace(attributes_byname, n)) {
1030 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
1037 * Insert the SAME pointer (not free'd when this entry is
1038 * deleted), into another table.
1040 * We want this behaviour because we want OLD names for
1041 * the attributes to be read from the configuration
1042 * files, but when we're printing them, (and looking up
1043 * by value) we want to use the NEW name.
1045 if (!fr_hash_table_replace(attributes_byvalue, n)) {
1046 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
1051 * Hacks for combo-IP
1053 if (n->type == PW_TYPE_COMBO_IP_ADDR) {
1056 v4 = fr_pool_alloc(sizeof(*v4) + namelen);
1059 v6 = fr_pool_alloc(sizeof(*v6) + namelen);
1062 memcpy(v4, n, sizeof(*v4) + namelen);
1063 v4->type = PW_TYPE_IPV4_ADDR;
1065 memcpy(v6, n, sizeof(*v6) + namelen);
1066 v6->type = PW_TYPE_IPV6_ADDR;
1067 if (!fr_hash_table_replace(attributes_combo, v4)) {
1068 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv4", name);
1072 if (!fr_hash_table_replace(attributes_combo, v6)) {
1073 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv6", name);
1078 if (!vendor && (attr > 0) && (attr < 256)) {
1079 dict_base_attrs[attr] = n;
1087 * Add a value for an attribute to the dictionary.
1089 int dict_addvalue(char const *namestr, char const *attrstr, int value)
1092 DICT_ATTR const *da;
1095 static DICT_ATTR const *last_attr = NULL;
1098 fr_strerror_printf("dict_addvalue: empty names are not permitted");
1102 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
1103 fr_strerror_printf("dict_addvalue: value name too long");
1107 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
1108 fr_strerror_printf("dict_addvalue: out of memory");
1111 memset(dval, 0, sizeof(*dval));
1113 strcpy(dval->name, namestr);
1114 dval->value = value;
1117 * Most VALUEs are bunched together by ATTRIBUTE. We can
1118 * save a lot of lookups on dictionary initialization by
1119 * caching the last attribute.
1121 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
1124 da = dict_attrbyname(attrstr);
1129 * Remember which attribute is associated with this
1130 * value, if possible.
1133 if (da->flags.has_value_alias) {
1134 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
1138 dval->attr = da->attr;
1139 dval->vendor = da->vendor;
1142 * Enforce valid values
1144 * Don't worry about fixups...
1150 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
1155 if (value > 65535) {
1157 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
1163 * Allow octets for now, because
1164 * of dictionary.cablelabs
1166 case PW_TYPE_OCTETS:
1168 case PW_TYPE_INTEGER:
1171 case PW_TYPE_INTEGER64:
1174 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
1175 fr_int2str(dict_attr_types, da->type, "?Unknown?"));
1179 value_fixup_t *fixup;
1181 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
1184 fr_strerror_printf("dict_addvalue: out of memory");
1187 memset(fixup, 0, sizeof(*fixup));
1189 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
1193 * Insert to the head of the list.
1195 fixup->next = value_fixup;
1196 value_fixup = fixup;
1202 * Add the value into the dictionary.
1206 memcpy(&tmp, &dval, sizeof(tmp));
1208 if (!fr_hash_table_insert(values_byname, tmp)) {
1213 * Suppress duplicates with the same
1214 * name and value. There are lots in
1215 * dictionary.ascend.
1217 old = dict_valbyname(da->attr, da->vendor, namestr);
1218 if (old && (old->value == dval->value)) {
1225 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
1231 * There are multiple VALUE's, keyed by attribute, so we
1232 * take care of that here.
1234 if (!fr_hash_table_replace(values_byvalue, dval)) {
1235 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
1243 static int sscanf_i(char const *str, unsigned int *pvalue)
1247 static char const *tab = "0123456789";
1249 if ((str[0] == '0') &&
1250 ((str[1] == 'x') || (str[1] == 'X'))) {
1251 tab = "0123456789abcdef";
1260 if (*str == '.') break;
1262 c = memchr(tab, tolower((int) *str), base);
1276 * Get the OID based on various pieces of information.
1278 * Remember, the packing format is weird.
1280 * 00VID 000000AA normal VSA for vendor VID
1281 * 00VID AABBCCDD normal VSAs with TLVs
1282 * EE000 000000AA extended attr (241.1)
1283 * EE000 AABBCCDD extended attr with TLVs
1284 * EEVID 000000AA EVS with vendor VID, attr AAA
1285 * EEVID AABBCCDD EVS with TLVs
1287 * <whew>! Are we crazy, or what?
1289 int dict_str2oid(char const *ptr, unsigned int *pvalue, unsigned int *pvendor,
1294 DICT_ATTR const *da = NULL;
1296 if (tlv_depth > fr_attr_max_tlv) {
1297 fr_strerror_printf("Too many sub-attributes");
1302 * If *pvalue is set, check if the attribute exists.
1303 * Otherwise, check that the vendor exists.
1306 da = dict_attrbyvalue(*pvalue, *pvendor);
1308 fr_strerror_printf("Parent attribute is undefined");
1312 if (!da->flags.has_tlv && !da->flags.extended) {
1313 fr_strerror_printf("Parent attribute %s cannot have sub-attributes",
1318 } else if ((*pvendor & (FR_MAX_VENDOR - 1)) != 0) {
1319 if (!dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1))) {
1320 fr_strerror_printf("Unknown vendor %u",
1321 *pvendor & (FR_MAX_VENDOR - 1));
1326 p = strchr(ptr, '.');
1329 * Look for 26.VID.x.y
1331 * If we find it, re-write the parameters, and recurse.
1333 if (!*pvendor && (tlv_depth == 0) && (*pvalue == PW_VENDOR_SPECIFIC)) {
1334 DICT_VENDOR const *dv;
1337 fr_strerror_printf("VSA needs to have sub-attribute");
1341 if (!sscanf_i(ptr, pvendor)) {
1342 fr_strerror_printf("Invalid number in attribute");
1346 if (*pvendor >= FR_MAX_VENDOR) {
1347 fr_strerror_printf("Cannot handle vendor ID larger than 2^24");
1352 dv = dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1));
1354 fr_strerror_printf("Unknown vendor \"%u\" ",
1355 *pvendor & (FR_MAX_VENDOR - 1));
1360 * Start off with (attr=0, vendor=VID), and
1361 * recurse. This causes the various checks above
1365 return dict_str2oid(p + 1, pvalue, pvendor, 0);
1368 if (!sscanf_i(ptr, &value)) {
1369 fr_strerror_printf("Invalid number in attribute");
1373 if (!*pvendor && (tlv_depth == 1) && da &&
1374 (da->flags.has_tlv || da->flags.extended)) {
1377 *pvendor = *pvalue * FR_MAX_VENDOR;
1381 return dict_str2oid(p + 1, pvalue, pvendor, 1);
1385 * And pack the data according to the scheme described in
1386 * the comments at the start of this function.
1389 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
1395 return dict_str2oid(p + 1, pvalue, pvendor, tlv_depth + 1);
1402 * Bamboo skewers under the fingernails in 5, 4, 3, 2, ...
1404 static DICT_ATTR const *dict_parent(unsigned int attr, unsigned int vendor)
1407 unsigned int base_vendor;
1410 * RFC attributes can't be of type "tlv".
1412 if (!vendor) return NULL;
1414 base_vendor = vendor & (FR_MAX_VENDOR - 1);
1417 * It's a real vendor.
1419 if (base_vendor != 0) {
1420 DICT_VENDOR const *dv;
1422 dv = dict_vendorbyvalue(base_vendor);
1423 if (!dv) return NULL;
1426 * Only standard format attributes can be of type "tlv",
1427 * Except for DHCP. <sigh>
1429 if ((vendor != 54) && ((dv->type != 1) || (dv->length != 1))) return NULL;
1431 for (i = MAX_TLV_NEST; i > 0; i--) {
1432 unsigned int parent;
1434 parent = attr & fr_attr_parent_mask[i];
1436 if (parent != attr) return dict_attrbyvalue(parent, vendor); /* not base_vendor */
1440 * It was a top-level VSA. There's no parent.
1441 * We COULD return the appropriate enclosing VSA
1442 * (26, or 241.26, etc.) but that's not what we
1449 * It's an extended attribute. Return the base Extended-Attr-X
1451 if (attr < 256) return dict_attrbyvalue((vendor / FR_MAX_VENDOR) & 0xff, 0);
1455 * Figure out which attribute it is.
1457 for (i = MAX_TLV_NEST; i > 0; i--) {
1458 unsigned int parent;
1460 parent = attr & fr_attr_parent_mask[i];
1461 if (parent != attr) return dict_attrbyvalue(parent, vendor); /* not base_vendor */
1469 * Process the ATTRIBUTE command
1471 static int process_attribute(char const* fn, int const line,
1472 unsigned int block_vendor,
1473 DICT_ATTR const *block_tlv, int tlv_depth,
1474 char **argv, int argc)
1477 unsigned int vendor = 0;
1480 unsigned int length;
1484 if ((argc < 3) || (argc > 4)) {
1485 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
1491 * Dictionaries need to have real names, not shitty ones.
1493 if (strncmp(argv[1], "Attr-", 5) == 0) {
1494 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1499 memset(&flags, 0, sizeof(flags));
1502 * Look for OIDs before doing anything else.
1504 p = strchr(argv[1], '.');
1508 * Validate all entries
1510 if (!sscanf_i(argv[1], &value)) {
1511 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1516 DICT_ATTR const *da;
1518 vendor = block_vendor;
1521 * Parse the rest of the OID.
1523 if (dict_str2oid(p + 1, &value, &vendor, tlv_depth + 1) < 0) {
1526 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1528 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1531 block_vendor = vendor;
1534 * Set the flags based on the parents flags.
1536 da = dict_parent(value, vendor);
1538 fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
1542 flags.extended = da->flags.extended;
1543 flags.long_extended = da->flags.long_extended;
1544 flags.evs = da->flags.evs;
1545 if (da->flags.has_tlv) flags.is_tlv = 1;
1548 if (strncmp(argv[2], "octets[", 7) != 0) {
1550 * find the type of the attribute.
1552 type = fr_str2int(dict_attr_types, argv[2], -1);
1554 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1560 type = PW_TYPE_OCTETS;
1562 p = strchr(argv[2] + 7, ']');
1564 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1570 if (!sscanf_i(argv[1], &length)) {
1571 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1575 if ((length == 0) || (length > 253)) {
1576 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1580 flags.length = length;
1587 char *key, *next, *last;
1592 if (flags.extended) {
1593 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1599 next = strchr(key, ',');
1600 if (next) *(next++) = '\0';
1603 * Boolean flag, means this is a tagged
1606 if ((strcmp(key, "has_tag") == 0) || (strcmp(key, "has_tag=1") == 0)) {
1610 * Encryption method, defaults to 0 (none).
1611 * Currently valid is just type 2,
1612 * Tunnel-Password style, which can only
1613 * be applied to strings.
1615 } else if (strncmp(key, "encrypt=", 8) == 0) {
1616 flags.encrypt = strtol(key + 8, &last, 0);
1618 fr_strerror_printf("dict_init: %s[%d] invalid option %s",
1623 if ((flags.encrypt == FLAG_ENCRYPT_ASCEND_SECRET) &&
1624 (type != PW_TYPE_STRING)) {
1625 fr_strerror_printf("dict_init: %s[%d] Only \"string\" types can have the "
1626 "\"encrypt=3\" flag set", fn, line);
1630 } else if (strncmp(key, "array", 6) == 0) {
1634 case PW_TYPE_IPV4_ADDR:
1635 case PW_TYPE_IPV6_ADDR:
1638 case PW_TYPE_INTEGER:
1640 case PW_TYPE_STRING:
1644 fr_strerror_printf("dict_init: %s[%d] \"%s\" type cannot have the "
1645 "\"array\" flag set",
1647 fr_int2str(dict_attr_types, type, "<UNKNOWN>"));
1651 } else if (strncmp(key, "concat", 7) == 0) {
1654 if (type != PW_TYPE_OCTETS) {
1655 fr_strerror_printf("dict_init: %s[%d] Only \"octets\" type can have the "
1656 "\"concat\" flag set", fn, line);
1660 } else if (strncmp(key, "virtual", 8) == 0) {
1664 fr_strerror_printf("dict_init: %s[%d] VSAs cannot have the \"virtual\" "
1665 "flag set", fn, line);
1670 fr_strerror_printf("dict_init: %s[%d] Standard attributes cannot "
1671 "have the \"virtual\" flag set", fn, line);
1676 * The only thing is the vendor name,
1677 * and it's a known name: allow it.
1679 } else if ((key == argv[3]) && !next) {
1681 fr_strerror_printf("dict_init: %s[%d] New-style attributes cannot use "
1682 "a vendor flag", fn, line);
1687 fr_strerror_printf("dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" "
1688 "is not allowed", fn, line);
1692 vendor = dict_vendorbyname(key);
1693 if (!vendor) goto unknown;
1698 fr_strerror_printf("dict_init: %s[%d]: unknown option \"%s\"", fn, line, key);
1703 if (key && !*key) break;
1707 if (block_vendor) vendor = block_vendor;
1710 * Special checks for tags, they make our life much more
1713 if (flags.has_tag) {
1715 * Only string, octets, and integer can be tagged.
1718 case PW_TYPE_STRING:
1719 case PW_TYPE_INTEGER:
1723 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1725 fr_int2str(dict_attr_types, type, "?Unknown?"));
1730 if (type == PW_TYPE_TLV) {
1731 if (vendor && (vendor < FR_MAX_VENDOR)
1733 && (vendor != DHCP_MAGIC_VENDOR)
1738 dv = dict_vendorbyvalue(vendor);
1739 if (!dv || (dv->type != 1) || (dv->length != 1)) {
1740 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1751 * TLV's can be only one octet.
1753 if ((value == 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1754 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1760 * Shift the value left.
1762 value <<= fr_attr_shift[tlv_depth];
1763 value |= block_tlv->attr;
1767 #ifdef WITH_DICTIONARY_WARNINGS
1769 * Hack to help us discover which vendors have illegal
1772 if (!vendor && (value < 256) &&
1773 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1774 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1782 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1785 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1787 fr_strerror_printf("dict_init: %s[%d]: %s",
1797 * Process the VALUE command
1799 static int process_value(char const* fn, int const line, char **argv,
1805 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1810 * For Compatibility, skip "Server-Config"
1812 if (strcasecmp(argv[0], "Server-Config") == 0)
1816 * Validate all entries
1818 if (!sscanf_i(argv[2], &value)) {
1819 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1824 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1827 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1829 fr_strerror_printf("dict_init: %s[%d]: %s",
1839 * Process the VALUE-ALIAS command
1841 * This allows VALUE mappings to be shared among multiple
1844 static int process_value_alias(char const* fn, int const line, char **argv,
1847 DICT_ATTR const *my_da, *da;
1851 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1856 my_da = dict_attrbyname(argv[0]);
1858 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1863 if (my_da->flags.has_value_alias) {
1864 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1869 da = dict_attrbyname(argv[1]);
1871 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1876 if (da->flags.has_value_alias) {
1877 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1882 if (my_da->type != da->type) {
1883 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1888 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1889 fr_strerror_printf("dict_addvalue: out of memory");
1893 dval->name[0] = '\0'; /* empty name */
1894 dval->attr = my_da->attr;
1895 dval->vendor = my_da->vendor;
1896 dval->value = da->attr;
1898 if (!fr_hash_table_insert(values_byname, dval)) {
1899 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1909 static int parse_format(char const *fn, int line, char const *format, int *pvalue, int *ptype, int *plength, bool *pcontinuation)
1913 bool continuation = false;
1915 if (strncasecmp(format, "format=", 7) != 0) {
1916 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1922 if ((strlen(p) < 3) ||
1923 !isdigit((int) p[0]) ||
1925 !isdigit((int) p[2]) ||
1926 (p[3] && (p[3] != ','))) {
1927 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1932 type = (int) (p[0] - '0');
1933 length = (int) (p[2] - '0');
1935 if ((type != 1) && (type != 2) && (type != 4)) {
1936 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1941 if ((length != 0) && (length != 1) && (length != 2)) {
1942 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1949 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1954 if ((p[4] != 'c') ||
1956 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1960 continuation = true;
1962 if ((*pvalue != VENDORPEC_WIMAX) ||
1963 (type != 1) || (length != 1)) {
1964 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1972 *pcontinuation = continuation;
1978 * Process the VENDOR command
1980 static int process_vendor(char const* fn, int const line, char **argv,
1985 bool continuation = false;
1988 if ((argc < 2) || (argc > 3)) {
1989 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1995 * Validate all entries
1997 if (!isdigit((int) argv[1][0])) {
1998 fr_strerror_printf("dict_init: %s[%d]: invalid value",
2002 value = atoi(argv[1]);
2004 /* Create a new VENDOR entry for the list */
2005 if (dict_addvendor(argv[0], value) < 0) {
2008 strlcpy(buffer, fr_strerror(), sizeof(buffer));
2010 fr_strerror_printf("dict_init: %s[%d]: %s",
2016 * Look for a format statement. Allow it to over-ride the hard-coded formats below.
2019 if (parse_format(fn, line, argv[2], &value, &type, &length, &continuation) < 0) {
2023 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
2027 } else if (value == VENDORPEC_LUCENT) {
2031 } else if (value == VENDORPEC_STARENT) {
2039 dv = dict_vendorbyvalue(value);
2041 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
2047 dv->length = length;
2048 dv->flags = continuation;
2054 * String split routine. Splits an input string IN PLACE
2055 * into pieces, based on spaces.
2057 int str2argv(char *str, char **argv, int max_argc)
2062 if (argc >= max_argc) break;
2065 * Chop out comments early.
2072 while ((*str == ' ') ||
2075 (*str == '\n')) *(str++) = '\0';
2086 (*str != '\n')) str++;
2092 static int my_dict_init(char const *parent, char const *filename,
2093 char const *src_file, int src_line);
2095 int dict_read(char const *dir, char const *filename)
2097 if (!attributes_byname) {
2098 fr_strerror_printf("Must call dict_init() before dict_read()");
2102 return my_dict_init(dir, filename, NULL, 0);
2106 #define MAX_ARGV (16)
2109 * Initialize the dictionary.
2111 static int my_dict_init(char const *parent, char const *filename,
2112 char const *src_file, int src_line)
2115 char dir[256], fn[256];
2119 unsigned int vendor;
2120 unsigned int block_vendor;
2121 struct stat statbuf;
2122 char *argv[MAX_ARGV];
2124 DICT_ATTR const *da, *block_tlv[MAX_TLV_NEST + 1];
2125 int which_block_tlv = 0;
2127 block_tlv[0] = NULL;
2128 block_tlv[1] = NULL;
2129 block_tlv[2] = NULL;
2130 block_tlv[3] = NULL;
2132 if ((strlen(parent) + 3 + strlen(filename)) > sizeof(dir)) {
2133 fr_strerror_printf("dict_init: filename name too long");
2138 * If it's an absolute dir, forget the parent dir,
2139 * and remember the new one.
2141 * If it's a relative dir, tack on the current filename
2142 * to the parent dir. And use that.
2144 if (!FR_DIR_IS_RELATIVE(filename)) {
2145 strlcpy(dir, filename, sizeof(dir));
2146 p = strrchr(dir, FR_DIR_SEP);
2150 strlcat(dir, "/", sizeof(dir));
2153 strlcpy(fn, filename, sizeof(fn));
2155 strlcpy(dir, parent, sizeof(dir));
2156 p = strrchr(dir, FR_DIR_SEP);
2158 if (p[1]) strlcat(dir, "/", sizeof(dir));
2160 strlcat(dir, "/", sizeof(dir));
2162 strlcat(dir, filename, sizeof(dir));
2163 p = strrchr(dir, FR_DIR_SEP);
2167 strlcat(dir, "/", sizeof(dir));
2170 p = strrchr(filename, FR_DIR_SEP);
2172 snprintf(fn, sizeof(fn), "%s%s", dir, p);
2174 snprintf(fn, sizeof(fn), "%s%s", dir, filename);
2180 * Check if we've loaded this file before. If so, ignore it.
2182 p = strrchr(fn, FR_DIR_SEP);
2185 if (dict_stat_check(fn, p + 1)) {
2192 if ((fp = fopen(fn, "r")) == NULL) {
2194 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
2195 fn, fr_syserror(errno));
2197 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
2198 src_file, src_line, fn, fr_syserror(errno));
2203 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
2204 if (!S_ISREG(statbuf.st_mode)) {
2206 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
2212 * Globally writable dictionaries means that users can control
2213 * the server configuration with little difficulty.
2216 if ((statbuf.st_mode & S_IWOTH) != 0) {
2218 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
2224 dict_stat_add(&statbuf);
2227 * Seed the random pool with data.
2229 fr_rand_seed(&statbuf, sizeof(statbuf));
2233 while (fgets(buf, sizeof(buf), fp) != NULL) {
2235 if (buf[0] == '#' || buf[0] == 0 ||
2236 buf[0] == '\n' || buf[0] == '\r')
2240 * Comment characters should NOT be appearing anywhere but
2241 * as start of a comment;
2243 p = strchr(buf, '#');
2246 argc = str2argv(buf, argv, MAX_ARGV);
2247 if (argc == 0) continue;
2250 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
2257 * Process VALUE lines.
2259 if (strcasecmp(argv[0], "VALUE") == 0) {
2260 if (process_value(fn, line,
2261 argv + 1, argc - 1) == -1) {
2269 * Perhaps this is an attribute.
2271 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
2272 if (process_attribute(fn, line, block_vendor,
2273 block_tlv[which_block_tlv],
2275 argv + 1, argc - 1) == -1) {
2283 * See if we need to import another dictionary.
2285 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
2286 if (my_dict_init(dir, argv[1], fn, line) < 0) {
2294 * Optionally include a dictionary
2296 if (strcasecmp(argv[0], "$INCLUDE-") == 0) {
2297 int rcode = my_dict_init(dir, argv[1], fn, line);
2299 if (rcode == -2) continue;
2308 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
2309 if (process_value_alias(fn, line,
2310 argv + 1, argc - 1) == -1) {
2318 * Process VENDOR lines.
2320 if (strcasecmp(argv[0], "VENDOR") == 0) {
2321 if (process_vendor(fn, line,
2322 argv + 1, argc - 1) == -1) {
2329 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
2332 "dict_init: %s[%d] invalid BEGIN-TLV entry",
2338 da = dict_attrbyname(argv[1]);
2341 "dict_init: %s[%d]: unknown attribute %s",
2347 if (da->type != PW_TYPE_TLV) {
2349 "dict_init: %s[%d]: attribute %s is not of type tlv",
2355 if (which_block_tlv >= MAX_TLV_NEST) {
2357 "dict_init: %s[%d]: TLVs are nested too deep",
2364 block_tlv[++which_block_tlv] = da;
2368 if (strcasecmp(argv[0], "END-TLV") == 0) {
2371 "dict_init: %s[%d] invalid END-TLV entry",
2377 da = dict_attrbyname(argv[1]);
2380 "dict_init: %s[%d]: unknown attribute %s",
2386 if (da != block_tlv[which_block_tlv]) {
2388 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
2393 block_tlv[which_block_tlv--] = NULL;
2397 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
2400 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
2406 vendor = dict_vendorbyname(argv[1]);
2409 "dict_init: %s[%d]: unknown vendor %s",
2415 block_vendor = vendor;
2418 * Check for extended attr VSAs
2420 * BEGIN-VENDOR foo format=Foo-Encapsulation-Attr
2423 if (strncmp(argv[2], "format=", 7) != 0) {
2425 "dict_init: %s[%d]: Invalid format %s",
2432 da = dict_attrbyname(p);
2434 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
2440 if (!da->flags.evs) {
2441 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
2448 * Pack the encapsulating
2449 * attribute into the upper 8
2450 * bits of the vendor ID
2452 block_vendor |= da->vendor;
2456 } /* BEGIN-VENDOR */
2458 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
2461 "dict_init: %s[%d] invalid END-VENDOR entry",
2467 vendor = dict_vendorbyname(argv[1]);
2470 "dict_init: %s[%d]: unknown vendor %s",
2476 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2478 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2488 * Any other string: We don't recognize it.
2490 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2501 * Empty callback for hash table initialization.
2503 static int null_callback(UNUSED void *ctx, UNUSED void *data)
2510 * Initialize the directory, then fix the attr member of
2513 int dict_init(char const *dir, char const *fn)
2516 * Check if we need to change anything. If not, don't do
2519 if (dict_stat_check(dir, fn)) {
2524 * Free the dictionaries, and the stat cache.
2529 * Create the table of vendor by name. There MAY NOT
2530 * be multiple vendors of the same name.
2532 * Each vendor is malloc'd, so the free function is free.
2534 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2535 dict_vendor_name_cmp,
2537 if (!vendors_byname) {
2542 * Create the table of vendors by value. There MAY
2543 * be vendors of the same value. If there are, we
2544 * pick the latest one.
2546 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2547 dict_vendor_value_cmp,
2549 if (!vendors_byvalue) {
2554 * Create the table of attributes by name. There MAY NOT
2555 * be multiple attributes of the same name.
2557 * Each attribute is malloc'd, so the free function is free.
2559 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2562 if (!attributes_byname) {
2567 * Create the table of attributes by value. There MAY
2568 * be attributes of the same value. If there are, we
2569 * pick the latest one.
2571 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2572 dict_attr_value_cmp,
2574 if (!attributes_byvalue) {
2579 * Horrible hacks for combo-IP.
2581 attributes_combo = fr_hash_table_create(dict_attr_combo_hash,
2582 dict_attr_combo_cmp,
2584 if (!attributes_combo) {
2588 values_byname = fr_hash_table_create(dict_value_name_hash,
2589 dict_value_name_cmp,
2591 if (!values_byname) {
2595 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2596 dict_value_value_cmp,
2598 if (!values_byvalue) {
2602 value_fixup = NULL; /* just to be safe. */
2604 if (my_dict_init(dir, fn, NULL, 0) < 0)
2609 value_fixup_t *this, *next;
2611 for (this = value_fixup; this != NULL; this = next) {
2614 a = dict_attrbyname(this->attrstr);
2617 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2618 this->attrstr, this->dval->name);
2619 return -1; /* leak, but they should die... */
2622 this->dval->attr = a->attr;
2625 * Add the value into the dictionary.
2627 if (!fr_hash_table_replace(values_byname,
2629 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2634 * Allow them to use the old name, but
2635 * prefer the new name when printing
2638 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2639 fr_hash_table_replace(values_byvalue,
2645 * Just so we don't lose track of things.
2652 * Walk over all of the hash tables to ensure they're
2653 * initialized. We do this because the threads may perform
2654 * lookups, and we don't want multi-threaded re-ordering
2655 * of the table entries. That would be bad.
2657 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2658 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2660 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2661 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2663 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2664 fr_hash_table_walk(values_byname, null_callback, NULL);
2669 static size_t print_attr_oid(char *buffer, size_t size, unsigned int attr,
2679 len = snprintf(buffer, size, "%u", attr & 0xff);
2683 return snprintf(buffer, size, "%u", attr);
2686 return snprintf(buffer, size, "%u", attr & 0xffff);
2690 if ((attr >> 8) == 0) return len;
2696 for (nest = 1; nest <= fr_attr_max_tlv; nest++) {
2697 if (((attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]) == 0) break;
2699 len = snprintf(buffer, size, ".%u",
2700 (attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]);
2710 /** Free dynamically allocated (unknown attributes)
2712 * If the da was dynamically allocated it will be freed, else the function
2713 * will return without doing anything.
2715 * @param da to free.
2717 void dict_attr_free(DICT_ATTR const **da)
2721 if (!da || !*da) return;
2723 /* Don't free real DAs */
2724 if (!(*da)->flags.is_unknown) {
2728 memcpy(&tmp, &da, sizeof(*tmp));
2735 /** Initialises a dictionary attr for unknown attributes
2737 * Initialises a dict attr for an unknown attribute/vendor/type without adding
2738 * it to dictionary pools/hashes.
2740 * @param[in,out] da struct to initialise, must be at least DICT_ATTR_SIZE bytes.
2741 * @param[in] attr number.
2742 * @param[in] vendor number.
2743 * @return 0 on success.
2745 int dict_unknown_from_fields(DICT_ATTR *da, unsigned int attr, unsigned int vendor)
2750 size_t bufsize = DICT_ATTR_MAX_NAME_LEN;
2752 memset(da, 0, DICT_ATTR_SIZE);
2755 da->vendor = vendor;
2756 da->type = PW_TYPE_OCTETS;
2757 da->flags.is_unknown = true;
2758 da->flags.is_pointer = true;
2761 * Unknown attributes of the "WiMAX" vendor get marked up
2762 * as being for WiMAX.
2764 if (vendor == VENDORPEC_WIMAX) {
2765 da->flags.wimax = 1;
2770 len = snprintf(p, bufsize, "Attr-");
2774 if (vendor > FR_MAX_VENDOR) {
2775 len = snprintf(p, bufsize, "%u.", vendor / FR_MAX_VENDOR);
2778 vendor &= (FR_MAX_VENDOR) - 1;
2785 * dv_type is the length of the vendor's type field
2786 * RFC 2865 never defined a mandatory length, so
2787 * different vendors have different length type fields.
2789 dv = dict_vendorbyvalue(vendor);
2793 len = snprintf(p, bufsize, "26.%u.", vendor);
2799 print_attr_oid(p, bufsize , attr, dv_type);
2804 /** Allocs a dictionary attr for unknown attributes
2806 * Allocs a dict attr for an unknown attribute/vendor/type without adding
2807 * it to dictionary pools/hashes.
2809 * @param[in] ctx to allocate DA in.
2810 * @param[in] attr number.
2811 * @param[in] vendor number.
2812 * @return 0 on success.
2814 DICT_ATTR const *dict_unknown_afrom_fields(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor)
2819 p = talloc_zero_array(ctx, uint8_t, DICT_ATTR_SIZE);
2821 fr_strerror_printf("Out of memory");
2824 da = (DICT_ATTR *) p;
2825 talloc_set_type(da, DICT_ATTR);
2827 if (dict_unknown_from_fields(da, attr, vendor) < 0) {
2835 /** Create a DICT_ATTR from an ASCII attribute and value
2837 * Where the attribute name is in the form:
2839 * - Attr-%d.%d.%d...
2840 * - Vendor-%d-Attr-%d
2841 * - VendorName-Attr-%d
2843 * @param[in] da to initialise.
2844 * @param[in] name of attribute.
2845 * @return 0 on success -1 on failure.
2847 int dict_unknown_from_str(DICT_ATTR *da, char const *name)
2849 unsigned int attr, vendor = 0;
2850 unsigned int dv_type = 1; /* The type of vendor field */
2852 char const *p = name;
2856 DICT_ATTR const *found;
2858 if (dict_valid_name(name) < 0) return -1;
2861 * Pull off vendor prefix first.
2863 if (strncasecmp(p, "Attr-", 5) != 0) {
2864 if (strncasecmp(p, "Vendor-", 7) == 0) {
2865 vendor = (int) strtol(p + 7, &q, 10);
2866 if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2867 fr_strerror_printf("Invalid vendor value in attribute name \"%s\"", name);
2874 /* must be vendor name */
2881 fr_strerror_printf("Invalid vendor name in attribute name \"%s\"", name);
2885 if ((size_t) (q - p) >= sizeof(buffer)) {
2886 fr_strerror_printf("Vendor name too long in attribute name \"%s\"", name);
2891 memcpy(buffer, p, (q - p));
2892 buffer[q - p] = '\0';
2894 vendor = dict_vendorbyname(buffer);
2896 fr_strerror_printf("Unknown name \"%s\"", name);
2905 fr_strerror_printf("Invalid text following vendor definition in attribute name \"%s\"", name);
2915 if (strncasecmp(p, "Attr-", 5) != 0) {
2916 fr_strerror_printf("Unknown attribute \"%s\"", name);
2921 attr = strtol(p + 5, &q, 10);
2927 fr_strerror_printf("Invalid value in attribute name \"%s\"", name);
2936 * VendorName-Attr-%d
2940 * Anything else is invalid.
2942 if (((vendor != 0) && (*p != '\0')) ||
2943 ((vendor == 0) && *p && (*p != '.'))) {
2945 fr_strerror_printf("Invalid OID");
2950 * Look for OIDs. Require the "Attr-26.Vendor-Id.type"
2951 * format, and disallow "Vendor-%d-Attr-%d" and
2952 * "VendorName-Attr-%d"
2954 * This section parses the Vendor-Id portion of
2955 * Attr-%d.%d. where the first number is 26, *or* an
2956 * extended name of the "evs" foundta type.
2959 found = dict_attrbyvalue(attr, 0);
2961 fr_strerror_printf("Cannot parse names without dictionaries");
2966 if ((attr != PW_VENDOR_SPECIFIC) &&
2967 !(found->flags.extended || found->flags.long_extended)) {
2968 fr_strerror_printf("Standard attributes cannot use OIDs");
2973 if ((attr == PW_VENDOR_SPECIFIC) || found->flags.evs) {
2974 vendor = strtol(p + 1, &q, 10);
2975 if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2976 fr_strerror_printf("Invalid vendor");
2981 if (*q != '.') goto invalid;
2985 if (found->flags.evs) vendor |= attr * FR_MAX_VENDOR;
2987 } /* else the second number is a TLV number */
2991 * Get the expected maximum size of the name.
2994 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
2997 if (dv_type > 3) dv_type = 3; /* hack */
3002 * Parse the next number. It could be a Vendor-Type
3003 * of 1..2^24, or it could be a TLV.
3006 attr = strtol(p + 1, &q, 10);
3008 fr_strerror_printf("Invalid name number");
3026 * Enforce a maximum value on the attribute number.
3028 if ((vendor > 0) && (attr >= (unsigned) (1 << (dv_type << 3)))) goto invalid;
3031 if (dict_str2oid(p + 1, &attr, &vendor, 1) < 0) {
3037 * If the caller doesn't provide a DICT_ATTR
3038 * we can't call dict_unknown_from_fields.
3041 fr_strerror_printf("Unknown attributes disallowed");
3045 return dict_unknown_from_fields(da, attr, vendor);
3048 /** Create a DICT_ATTR from an ASCII attribute and value
3050 * Where the attribute name is in the form:
3052 * - Attr-%d.%d.%d...
3053 * - Vendor-%d-Attr-%d
3054 * - VendorName-Attr-%d
3056 * @param[in] ctx to alloc new attribute in.
3057 * @param[in] name of attribute.
3058 * @return 0 on success -1 on failure.
3060 DICT_ATTR const *dict_unknown_afrom_str(TALLOC_CTX *ctx, char const *name)
3065 p = talloc_zero_array(ctx, uint8_t, DICT_ATTR_SIZE);
3067 fr_strerror_printf("Out of memory");
3070 da = (DICT_ATTR *) p;
3071 talloc_set_type(da, DICT_ATTR);
3073 if (dict_unknown_from_str(da, name) < 0) {
3081 /** Create a dictionary attribute by name embedded in another string
3083 * Find the first invalid attribute name char in the string pointed
3086 * Copy the characters between the start of the name string and the first
3087 * none dict_attr_allowed_char to a buffer and initialise da as an
3088 * unknown attribute.
3090 * @param[out] da to initialise.
3091 * @param[in,out] name string start.
3092 * @return 0 on success or -1 on error;
3094 int dict_unknown_from_substr(DICT_ATTR *da, char const **name)
3098 char buffer[DICT_ATTR_MAX_NAME_LEN + 1];
3100 if (!name || !*name) return -1;
3103 * Advance p until we get something that's not part of
3104 * the dictionary attribute name.
3106 for (p = *name; dict_attr_allowed_chars[(int) *p] || (*p == '.' ) || (*p == '-'); p++);
3109 if (len > DICT_ATTR_MAX_NAME_LEN) {
3110 fr_strerror_printf("Attribute name too long");
3115 fr_strerror_printf("Invalid attribute name");
3118 strlcpy(buffer, *name, len + 1);
3120 if (dict_unknown_from_str(da, buffer) < 0) return -1;
3128 * Get an attribute by its numerical value.
3130 DICT_ATTR const *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
3134 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
3139 return fr_hash_table_finddata(attributes_byvalue, &da);
3143 /** Get an attribute by its numerical value and data type
3145 * Used only for COMBO_IP
3147 * @return The attribute, or NULL if not found
3149 DICT_ATTR const *dict_attrbytype(unsigned int attr, unsigned int vendor,
3158 return fr_hash_table_finddata(attributes_combo, &da);
3161 /** Using a parent and attr/vendor, find a child attr/vendor
3164 int dict_attr_child(DICT_ATTR const *parent,
3165 unsigned int *pattr, unsigned int *pvendor)
3167 unsigned int attr, vendor;
3170 if (!parent || !pattr || !pvendor) return false;
3176 * Only some types can have children
3178 switch (parent->type) {
3179 default: return false;
3184 case PW_TYPE_EXTENDED:
3185 case PW_TYPE_LONG_EXTENDED:
3189 if ((vendor == 0) && (parent->vendor != 0)) return false;
3192 * Bootstrap by starting off with the parents values.
3194 da.attr = parent->attr;
3195 da.vendor = parent->vendor;
3198 * Do various butchery to insert the "attr" value.
3200 * 00VID 000000AA normal VSA for vendor VID
3201 * 00VID DDCCBBAA normal VSAs with TLVs
3202 * EE000 000000AA extended attr (241.1)
3203 * EE000 DDCCBBAA extended attr with TLVs
3204 * EEVID 000000AA EVS with vendor VID, attr AAA
3205 * EEVID DDCCBBAA EVS with TLVs
3208 da.vendor = parent->attr * FR_MAX_VENDOR;
3209 da.vendor |= vendor;
3216 * Trying to nest too deep. It's an error
3218 if (parent->attr & (fr_attr_mask[MAX_TLV_NEST] << fr_attr_shift[MAX_TLV_NEST])) {
3222 for (i = MAX_TLV_NEST - 1; i >= 0; i--) {
3223 if ((parent->attr & (fr_attr_mask[i] << fr_attr_shift[i]))) {
3224 da.attr |= (attr & fr_attr_mask[i + 1]) << fr_attr_shift[i + 1];
3234 fprintf(stderr, "LOOKING FOR %08x %08x + %08x %08x --> %08x %08x\n",
3235 parent->vendor, parent->attr, attr, vendor,
3236 da.vendor, da.attr);
3240 *pvendor = da.vendor;
3245 * Get an attribute by it's numerical value, and the parent
3247 DICT_ATTR const *dict_attrbyparent(DICT_ATTR const *parent, unsigned int attr, unsigned int vendor)
3249 unsigned int my_attr, my_vendor;
3255 if (!dict_attr_child(parent, &my_attr, &my_vendor)) return NULL;
3258 da.vendor = my_vendor;
3260 return fr_hash_table_finddata(attributes_byvalue, &da);
3265 * Get an attribute by its name.
3267 DICT_ATTR const *dict_attrbyname(char const *name)
3270 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
3272 if (!name) return NULL;
3274 da = (DICT_ATTR *) buffer;
3275 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
3277 return fr_hash_table_finddata(attributes_byname, da);
3280 /** Look up a dictionary attribute by name embedded in another string
3282 * Find the first invalid attribute name char in the string pointed
3285 * Copy the characters between the start of the name string and the first
3286 * none dict_attr_allowed_char to a buffer and perform a dictionary lookup
3289 * If the attribute exists, advance the pointer pointed to by name
3290 * to the first none dict_attr_allowed_char char, and return the DA.
3292 * If the attribute does not exist, don't advance the pointer and return
3295 * @param[in,out] name string start.
3296 * @return NULL if no attributes matching the name could be found, else
3298 DICT_ATTR const *dict_attrbyname_substr(char const **name)
3301 DICT_ATTR const *da;
3304 uint32_t buffer[(sizeof(*find) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
3306 if (!name || !*name) return NULL;
3308 find = (DICT_ATTR *) buffer;
3311 * Advance p until we get something that's not part of
3312 * the dictionary attribute name.
3314 for (p = *name; dict_attr_allowed_chars[(int) *p]; p++);
3317 if (len > DICT_ATTR_MAX_NAME_LEN) {
3318 fr_strerror_printf("Attribute name too long");
3322 strlcpy(find->name, *name, len + 1);
3324 da = fr_hash_table_finddata(attributes_byname, find);
3326 fr_strerror_printf("Unknown attribute \"%s\"", find->name);
3335 * Associate a value with an attribute and return it.
3337 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
3339 DICT_VALUE dval, *dv;
3342 * First, look up aliases.
3345 dval.vendor = vendor;
3346 dval.name[0] = '\0';
3349 * Look up the attribute alias target, and use
3350 * the correct attribute number if found.
3352 dv = fr_hash_table_finddata(values_byname, &dval);
3353 if (dv) dval.attr = dv->value;
3357 return fr_hash_table_finddata(values_byvalue, &dval);
3361 * Associate a value with an attribute and return it.
3363 char const *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value)
3367 dv = dict_valbyattr(attr, vendor, value);
3374 * Get a value by its name, keyed off of an attribute.
3376 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, char const *name)
3378 DICT_VALUE *my_dv, *dv;
3379 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
3381 if (!name) return NULL;
3383 my_dv = (DICT_VALUE *) buffer;
3385 my_dv->vendor = vendor;
3386 my_dv->name[0] = '\0';
3389 * Look up the attribute alias target, and use
3390 * the correct attribute number if found.
3392 dv = fr_hash_table_finddata(values_byname, my_dv);
3393 if (dv) my_dv->attr = dv->value;
3395 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
3397 return fr_hash_table_finddata(values_byname, my_dv);
3401 * Get the vendor PEC based on the vendor name
3403 * This is efficient only for small numbers of vendors.
3405 int dict_vendorbyname(char const *name)
3408 size_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + sizeof(size_t) - 1) / sizeof(size_t)];
3410 if (!name) return 0;
3412 dv = (DICT_VENDOR *) buffer;
3413 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
3415 dv = fr_hash_table_finddata(vendors_byname, dv);
3418 return dv->vendorpec;
3422 * Return the vendor struct based on the PEC.
3424 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
3428 dv.vendorpec = vendorpec;
3430 return fr_hash_table_finddata(vendors_byvalue, &dv);
3433 /** Converts an unknown to a known by adding it to the internal dictionaries.
3435 * Does not free old DICT_ATTR, that is left up to the caller.
3437 * @param old unknown attribute to add.
3438 * @return existing DICT_ATTR if old was found in a dictionary, else the new entry in the dictionary
3441 DICT_ATTR const *dict_unknown_add(DICT_ATTR const *old)
3443 DICT_ATTR const *da, *parent;
3446 if (!old) return NULL;
3448 if (!old->flags.is_unknown) return old;
3450 da = dict_attrbyvalue(old->attr, old->vendor);
3453 memcpy(&flags, &old->flags, sizeof(flags));
3454 flags.is_unknown = false;
3456 parent = dict_parent(old->attr, old->vendor);
3458 if (parent->flags.has_tlv) flags.is_tlv = true;
3459 flags.evs = parent->flags.evs;
3460 flags.extended = parent->flags.extended;
3461 flags.long_extended = parent->flags.long_extended;
3464 if (dict_addattr(old->name, old->attr, old->vendor, old->type, flags) < 0) {
3468 da = dict_attrbyvalue(old->attr, old->vendor);