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 #define DICT_ATTR_SIZE sizeof(DICT_ATTR) + DICT_ATTR_MAX_NAME_LEN
49 static fr_hash_table_t *vendors_byname = NULL;
50 static fr_hash_table_t *vendors_byvalue = NULL;
52 static fr_hash_table_t *attributes_byname = NULL;
53 static fr_hash_table_t *attributes_byvalue = NULL;
55 static fr_hash_table_t *attributes_combo = NULL;
57 static fr_hash_table_t *values_byvalue = NULL;
58 static fr_hash_table_t *values_byname = NULL;
60 static DICT_ATTR *dict_base_attrs[256];
63 * For faster HUP's, we cache the stat information for
64 * files we've $INCLUDEd
66 typedef struct dict_stat_t {
67 struct dict_stat_t *next;
72 static char *stat_root_dir = NULL;
73 static char *stat_root_file = NULL;
75 static dict_stat_t *stat_head = NULL;
76 static dict_stat_t *stat_tail = NULL;
78 typedef struct value_fixup_t {
79 char attrstr[DICT_ATTR_MAX_NAME_LEN];
81 struct value_fixup_t *next;
86 * So VALUEs in the dictionary can have forward references.
88 static value_fixup_t *value_fixup = NULL;
90 const FR_NAME_NUMBER dict_attr_types[] = {
91 { "integer", PW_TYPE_INTEGER },
92 { "string", PW_TYPE_STRING },
93 { "ipaddr", PW_TYPE_IPADDR },
94 { "date", PW_TYPE_DATE },
95 { "abinary", PW_TYPE_ABINARY },
96 { "octets", PW_TYPE_OCTETS },
97 { "ifid", PW_TYPE_IFID },
98 { "ipv6addr", PW_TYPE_IPV6ADDR },
99 { "ipv6prefix", PW_TYPE_IPV6PREFIX },
100 { "byte", PW_TYPE_BYTE },
101 { "short", PW_TYPE_SHORT },
102 { "ether", PW_TYPE_ETHERNET },
103 { "combo-ip", PW_TYPE_COMBO_IP },
104 { "tlv", PW_TYPE_TLV },
105 { "signed", PW_TYPE_SIGNED },
106 { "extended", PW_TYPE_EXTENDED },
107 { "long-extended", PW_TYPE_LONG_EXTENDED },
108 { "evs", PW_TYPE_EVS },
109 { "uint8", PW_TYPE_BYTE },
110 { "uint16", PW_TYPE_SHORT },
111 { "uint32", PW_TYPE_INTEGER },
112 { "int32", PW_TYPE_SIGNED },
113 { "integer64", PW_TYPE_INTEGER64 },
114 { "uint64", PW_TYPE_INTEGER64 },
115 { "ipv4prefix", PW_TYPE_IPV4PREFIX },
116 { "vsa", PW_TYPE_VSA },
122 * For packing multiple TLV numbers into one 32-bit integer. The
123 * first 3 bytes are just the 8-bit number. The next two are
124 * more limited. We only allow 31 attributes nested 3 layers
125 * deep, and only 7 nested 4 layers deep. This should be
126 * sufficient for most purposes.
128 * For TLVs and extended attributes, we packet the base attribute
129 * number into the upper 8 bits of the "vendor" field.
131 * e.g. OID attribute vendor
133 * 241.26.9.1 1 (241 << 8) | (9)
134 * 241.1.2 1 | (2 << 8) (241 << 8)
136 #define MAX_TLV_NEST (4)
139 * 8 bits of base attribute
140 * 8 bits for nested TLV 1
141 * 8 bits for nested TLV 2
142 * 5 bits for nested TLV 3
143 * 3 bits for nested TLV 4
145 const int fr_attr_max_tlv = MAX_TLV_NEST;
146 const int fr_attr_shift[MAX_TLV_NEST + 1] = {
150 const int fr_attr_mask[MAX_TLV_NEST + 1] = {
151 0xff, 0xff, 0xff, 0x1f, 0x07
156 * Create the hash of the name.
158 * We copy the hash function here because it's substantially faster.
160 #define FNV_MAGIC_INIT (0x811c9dc5)
161 #define FNV_MAGIC_PRIME (0x01000193)
163 static uint32_t dict_hashname(const char *name)
165 uint32_t hash = FNV_MAGIC_INIT;
168 for (p = name; *p != '\0'; p++) {
169 int c = *(const unsigned char *) p;
170 if (isalpha(c)) c = tolower(c);
172 hash *= FNV_MAGIC_PRIME;
173 hash ^= (uint32_t ) (c & 0xff);
181 * Hash callback functions.
183 static uint32_t dict_attr_name_hash(const void *data)
185 return dict_hashname(((const DICT_ATTR *)data)->name);
188 static int dict_attr_name_cmp(const void *one, const void *two)
190 const DICT_ATTR *a = one;
191 const DICT_ATTR *b = two;
193 return strcasecmp(a->name, b->name);
196 static uint32_t dict_attr_value_hash(const void *data)
199 const DICT_ATTR *attr = data;
201 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
202 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
205 static int dict_attr_value_cmp(const void *one, const void *two)
207 const DICT_ATTR *a = one;
208 const DICT_ATTR *b = two;
210 if (a->vendor < b->vendor) return -1;
211 if (a->vendor > b->vendor) return +1;
213 return a->attr - b->attr;
216 static uint32_t dict_attr_combo_hash(const void *data)
219 const DICT_ATTR *attr = data;
221 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
222 hash = fr_hash_update(&attr->type, sizeof(attr->type), hash);
223 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
226 static int dict_attr_combo_cmp(const void *one, const void *two)
228 const DICT_ATTR *a = one;
229 const DICT_ATTR *b = two;
231 if (a->type < b->type) return -1;
232 if (a->type > b->type) return +1;
234 if (a->vendor < b->vendor) return -1;
235 if (a->vendor > b->vendor) return +1;
237 return a->attr - b->attr;
240 static uint32_t dict_vendor_name_hash(const void *data)
242 return dict_hashname(((const DICT_VENDOR *)data)->name);
245 static int dict_vendor_name_cmp(const void *one, const void *two)
247 const DICT_VENDOR *a = one;
248 const DICT_VENDOR *b = two;
250 return strcasecmp(a->name, b->name);
253 static uint32_t dict_vendor_value_hash(const void *data)
255 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
256 sizeof(((const DICT_VENDOR *)data)->vendorpec));
259 static int dict_vendor_value_cmp(const void *one, const void *two)
261 const DICT_VENDOR *a = one;
262 const DICT_VENDOR *b = two;
264 return a->vendorpec - b->vendorpec;
267 static uint32_t dict_value_name_hash(const void *data)
270 const DICT_VALUE *dval = data;
272 hash = dict_hashname(dval->name);
273 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
274 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
277 static int dict_value_name_cmp(const void *one, const void *two)
280 const DICT_VALUE *a = one;
281 const DICT_VALUE *b = two;
283 rcode = a->attr - b->attr;
284 if (rcode != 0) return rcode;
286 rcode = a->vendor - b->vendor;
287 if (rcode != 0) return rcode;
289 return strcasecmp(a->name, b->name);
292 static uint32_t dict_value_value_hash(const void *data)
295 const DICT_VALUE *dval = data;
297 hash = fr_hash(&dval->attr, sizeof(dval->attr));
298 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
299 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
302 static int dict_value_value_cmp(const void *one, const void *two)
305 const DICT_VALUE *a = one;
306 const DICT_VALUE *b = two;
308 if (a->vendor < b->vendor) return -1;
309 if (a->vendor > b->vendor) return +1;
311 rcode = a->attr - b->attr;
312 if (rcode != 0) return rcode;
314 return a->value - b->value;
319 * Free the list of stat buffers
321 static void dict_stat_free(void)
323 dict_stat_t *this, *next;
326 stat_root_dir = NULL;
327 free(stat_root_file);
328 stat_root_file = NULL;
335 for (this = stat_head; this != NULL; this = next) {
341 stat_head = stat_tail = NULL;
346 * Add an entry to the list of stat buffers.
348 static void dict_stat_add(const char *name, const struct stat *stat_buf)
352 this = malloc(sizeof(*this));
354 memset(this, 0, sizeof(*this));
356 this->name = strdup(name);
357 this->mtime = stat_buf->st_mtime;
360 stat_head = stat_tail = this;
362 stat_tail->next = this;
369 * See if any dictionaries have changed. If not, don't
372 static int dict_stat_check(const char *root_dir, const char *root_file)
377 if (!stat_root_dir) return 0;
378 if (!stat_root_file) return 0;
380 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
381 if (strcmp(root_file, stat_root_file) != 0) return 0;
383 if (!stat_head) return 0; /* changed, reload */
385 for (this = stat_head; this != NULL; this = this->next) {
386 if (stat(this->name, &buf) < 0) return 0;
388 if (buf.st_mtime != this->mtime) return 0;
394 typedef struct fr_pool_t {
397 struct fr_pool_t *page_free;
398 struct fr_pool_t *page_next;
401 #define FR_POOL_SIZE (32768)
402 #define FR_ALLOC_ALIGN (8)
404 static fr_pool_t *dict_pool = NULL;
406 static fr_pool_t *fr_pool_create(void)
408 fr_pool_t *fp = malloc(FR_POOL_SIZE);
410 if (!fp) return NULL;
412 memset(fp, 0, FR_POOL_SIZE);
414 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
415 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
417 fp->page_next = NULL;
421 static void fr_pool_delete(fr_pool_t **pfp)
423 fr_pool_t *fp, *next;
425 if (!pfp || !*pfp) return;
427 for (fp = *pfp; fp != NULL; fp = next) {
428 next = fp->page_next;
429 fp->page_next = NULL;
436 static void *fr_pool_alloc(size_t size)
440 if (size == 0) return NULL;
442 if (size > 256) return NULL; /* shouldn't happen */
445 dict_pool = fr_pool_create();
446 if (!dict_pool) return NULL;
449 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
450 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
453 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
454 dict_pool->page_free->page_next = fr_pool_create();
455 if (!dict_pool->page_free->page_next) return NULL;
456 dict_pool->page_free = dict_pool->page_free->page_next;
459 ptr = dict_pool->page_free->free_ptr;
460 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
466 static void fr_pool_free(UNUSED void *ptr)
469 * Place-holder for later code.
474 * Free the dictionary_attributes and dictionary_values lists.
481 fr_hash_table_free(vendors_byname);
482 fr_hash_table_free(vendors_byvalue);
483 vendors_byname = NULL;
484 vendors_byvalue = NULL;
486 fr_hash_table_free(attributes_byname);
487 fr_hash_table_free(attributes_byvalue);
488 fr_hash_table_free(attributes_combo);
489 attributes_byname = NULL;
490 attributes_byvalue = NULL;
491 attributes_combo = NULL;
493 fr_hash_table_free(values_byname);
494 fr_hash_table_free(values_byvalue);
495 values_byname = NULL;
496 values_byvalue = NULL;
498 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
500 fr_pool_delete(&dict_pool);
506 * Add vendor to the list.
508 int dict_addvendor(const char *name, unsigned int value)
513 if (value >= FR_MAX_VENDOR) {
514 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
518 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
519 fr_strerror_printf("dict_addvendor: vendor name too long");
523 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
524 fr_strerror_printf("dict_addvendor: out of memory");
528 strcpy(dv->name, name);
529 dv->vendorpec = value;
530 dv->type = dv->length = 1; /* defaults */
532 if (!fr_hash_table_insert(vendors_byname, dv)) {
535 old_dv = fr_hash_table_finddata(vendors_byname, dv);
537 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
540 if (old_dv->vendorpec != dv->vendorpec) {
541 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
546 * Already inserted. Discard the duplicate entry.
553 * Insert the SAME pointer (not free'd when this table is
554 * deleted), into another table.
556 * We want this behaviour because we want OLD names for
557 * the attributes to be read from the configuration
558 * files, but when we're printing them, (and looking up
559 * by value) we want to use the NEW name.
561 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
562 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
571 * Add an attribute to the dictionary.
573 int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
577 static int max_attr = 0;
582 namelen = strlen(name);
583 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
584 fr_strerror_printf("dict_addattr: attribute name too long");
588 for (p = name; *p != '\0'; p++) {
590 fr_strerror_printf("dict_addattr: attribute name cannot contain control characters");
594 if ((*p == '"') || (*p == '\\')) {
595 fr_strerror_printf("dict_addattr: attribute name cannot contain quotation or backslash");
599 if ((*p == '<') || (*p == '>') || (*p == '&')) {
600 fr_strerror_printf("dict_addattr: attribute name cannot contain XML control characters");
606 !((type == PW_TYPE_INTEGER) || (type == PW_TYPE_STRING))) {
607 fr_strerror_printf("dict_addattr: Only 'integer' and 'string' attributes can have tags");
613 * If the attr is '-1', that means use a pre-existing
614 * one (if it already exists). If one does NOT already exist,
615 * then create a new attribute, with a non-conflicting value,
619 if (dict_attrbyname(name)) {
620 return 0; /* exists, don't add it again */
625 } else if (vendor == 0) {
629 if (attr > max_attr) {
635 * Additional checks for extended attributes.
637 if (flags.extended || flags.long_extended || flags.evs) {
638 if (vendor && (vendor < FR_MAX_VENDOR)) {
639 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
646 || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
647 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
653 if (!(flags.extended || flags.long_extended)) {
654 fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
658 /* VSAs cannot be of format EVS */
659 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
660 fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
666 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
670 if (flags.has_tlv && flags.length) {
671 fr_strerror_printf("TLVs cannot have a fixed length");
675 if ((vendor & (FR_MAX_VENDOR -1)) != 0) {
677 static DICT_VENDOR *last_vendor = NULL;
679 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
680 fr_strerror_printf("TLV's cannot be encrypted");
684 if (flags.is_tlv && flags.has_tag) {
685 fr_strerror_printf("Sub-TLV's cannot have a tag");
689 if (flags.has_tlv && flags.has_tag) {
690 fr_strerror_printf("TLV's cannot have a tag");
695 * Most ATTRIBUTEs are bunched together by
696 * VENDOR. We can save a lot of lookups on
697 * dictionary initialization by caching the last
701 ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
705 * Ignore the high byte (sigh)
707 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
712 * If the vendor isn't defined, die.
715 fr_strerror_printf("dict_addattr: Unknown vendor %u",
716 vendor & (FR_MAX_VENDOR - 1));
721 * FIXME: Switch over dv->type, and limit things
724 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
725 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
727 } /* else 256..65535 are allowed */
730 * If the attribute is in the standard space, AND
731 * has a sub-type (e.g. 241.1 or 255.3), then its
732 * number is placed into the upper 8 bits of the
735 * This also happens for the new VSAs.
737 * If we find it, then set the various flags
738 * based on what we see.
740 if (vendor >= FR_MAX_VENDOR) {
743 parent = (vendor / FR_MAX_VENDOR) & 0xff;
745 da = dict_attrbyvalue(parent, 0);
747 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown parent attribute %u.", parent);
752 * These flags are inhereited inherited
755 flags.extended = da->flags.extended;
756 flags.long_extended = da->flags.long_extended;
759 * Non-extended attributes can't have VSAs.
761 if (!flags.extended &&
762 ((vendor & (FR_MAX_VENDOR - 1)) != 0)) {
763 fr_strerror_printf("dict_addattr: ATTRIBUTE cannot be a VSA");
767 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
773 * <sigh> Alvarion, being *again* a horribly
774 * broken vendor, has re-used the WiMAX format in
775 * their proprietary vendor space. This re-use
776 * means that there are *multiple* conflicting
777 * Alvarion dictionaries.
779 flags.wimax = dv->flags;
783 * Create a new attribute for the list
785 if ((n = fr_pool_alloc(sizeof(*n) + namelen)) == NULL) {
787 fr_strerror_printf("dict_adnttr: out of memory");
791 memcpy(n->name, name, namelen);
792 n->name[namelen] = '\0';
799 * Insert the attribute, only if it's not a duplicate.
801 if (!fr_hash_table_insert(attributes_byname, n)) {
805 * If the attribute has identical number, then
806 * ignore the duplicate.
808 a = fr_hash_table_finddata(attributes_byname, n);
809 if (a && (strcasecmp(a->name, n->name) == 0)) {
810 if (a->attr != n->attr) {
811 fr_strerror_printf("dict_adnttr: Duplicate attribute name %s", name);
817 * Same name, same vendor, same attr,
818 * maybe the flags and/or type is
819 * different. Let the new value
820 * over-ride the old one.
825 fr_hash_table_delete(attributes_byvalue, a);
827 if (!fr_hash_table_replace(attributes_byname, n)) {
828 fr_strerror_printf("dict_adnttr: Internal error storing attribute %s", name);
835 * Insert the SAME pointer (not free'd when this entry is
836 * deleted), into another table.
838 * We want this behaviour because we want OLD names for
839 * the attributes to be read from the configuration
840 * files, but when we're printing them, (and looking up
841 * by value) we want to use the NEW name.
843 if (!fr_hash_table_replace(attributes_byvalue, n)) {
844 fr_strerror_printf("dict_adnttr: Failed inserting attribute name %s", name);
851 if (n->type == PW_TYPE_COMBO_IP) {
854 v4 = fr_pool_alloc(sizeof(*v4));
857 v6 = fr_pool_alloc(sizeof(*v6));
863 memcpy(v4, n, sizeof(*v4));
864 v4->type = PW_TYPE_IPADDR;
866 memcpy(v6, n, sizeof(*v6));
867 v6->type = PW_TYPE_IPV6ADDR;
869 if (!fr_hash_table_insert(attributes_combo, v4)) {
870 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv4", name);
876 if (!fr_hash_table_insert(attributes_combo, v6)) {
877 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv6", name);
883 if (!vendor && (attr > 0) && (attr < 256)) {
884 dict_base_attrs[attr] = n;
892 * Add a value for an attribute to the dictionary.
894 int dict_addvalue(const char *namestr, const char *attrstr, int value)
897 const DICT_ATTR *dattr;
900 static const DICT_ATTR *last_attr = NULL;
903 fr_strerror_printf("dict_addvalue: empty names are not permitted");
907 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
908 fr_strerror_printf("dict_addvalue: value name too long");
912 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
913 fr_strerror_printf("dict_addvalue: out of memory");
916 memset(dval, 0, sizeof(*dval));
918 strcpy(dval->name, namestr);
922 * Most VALUEs are bunched together by ATTRIBUTE. We can
923 * save a lot of lookups on dictionary initialization by
924 * caching the last attribute.
926 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
929 dattr = dict_attrbyname(attrstr);
934 * Remember which attribute is associated with this
935 * value, if possible.
938 if (dattr->flags.has_value_alias) {
939 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
943 dval->attr = dattr->attr;
944 dval->vendor = dattr->vendor;
947 * Enforce valid values
949 * Don't worry about fixups...
951 switch (dattr->type) {
955 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
962 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
968 * Allow octets for now, because
969 * of dictionary.cablelabs
973 case PW_TYPE_INTEGER:
976 case PW_TYPE_INTEGER64:
979 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
980 fr_int2str(dict_attr_types, dattr->type, "?Unknown?"));
984 value_fixup_t *fixup;
986 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
989 fr_strerror_printf("dict_addvalue: out of memory");
992 memset(fixup, 0, sizeof(*fixup));
994 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
998 * Insert to the head of the list.
1000 fixup->next = value_fixup;
1001 value_fixup = fixup;
1007 * Add the value into the dictionary.
1011 memcpy(&tmp, &dval, sizeof(tmp));
1013 if (!fr_hash_table_insert(values_byname, tmp)) {
1018 * Suppress duplicates with the same
1019 * name and value. There are lots in
1020 * dictionary.ascend.
1022 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
1023 if (old && (old->value == dval->value)) {
1030 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
1036 * There are multiple VALUE's, keyed by attribute, so we
1037 * take care of that here.
1039 if (!fr_hash_table_replace(values_byvalue, dval)) {
1040 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
1048 static int sscanf_i(const char *str, unsigned int *pvalue)
1052 static const char *tab = "0123456789";
1054 if ((str[0] == '0') &&
1055 ((str[1] == 'x') || (str[1] == 'X'))) {
1056 tab = "0123456789abcdef";
1065 if (*str == '.') break;
1067 c = memchr(tab, tolower((int) *str), base);
1081 * Get the OID based on various pieces of information.
1083 * Remember, the packing format is weird.
1085 * 00VID 000000AA normal VSA for vendor VID
1086 * 00VID AABBCCDD normal VSAs with TLVs
1087 * EE000 000000AA extended attr (241.1)
1088 * EE000 AABBCCDD extended attr with TLVs
1089 * EEVID 000000AA EVS with vendor VID, attr AAA
1090 * EEVID AABBCCDD EVS with TLVs
1092 * <whew>! Are we crazy, or what?
1094 int dict_str2oid(const char *ptr, unsigned int *pvalue, unsigned int *pvendor,
1099 const DICT_ATTR *da = NULL;
1101 if (tlv_depth > fr_attr_max_tlv) {
1102 fr_strerror_printf("Too many sub-attributes");
1107 * If *pvalue is set, check if the attribute exists.
1108 * Otherwise, check that the vendor exists.
1111 da = dict_attrbyvalue(*pvalue, *pvendor);
1113 fr_strerror_printf("Parent attribute is undefined.");
1117 if (!da->flags.has_tlv && !da->flags.extended) {
1118 fr_strerror_printf("Parent attribute %s cannot have sub-attributes",
1123 } else if ((*pvendor & (FR_MAX_VENDOR - 1)) != 0) {
1124 if (!dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1))) {
1125 fr_strerror_printf("Unknown vendor %u",
1126 *pvendor & (FR_MAX_VENDOR - 1));
1131 p = strchr(ptr, '.');
1134 * Look for 26.VID.x.y
1136 * If we find it, re-write the parameters, and recurse.
1138 if (!*pvendor && (tlv_depth == 0) && (*pvalue == PW_VENDOR_SPECIFIC)) {
1139 const DICT_VENDOR *dv;
1142 fr_strerror_printf("VSA needs to have sub-attribute");
1146 if (!sscanf_i(ptr, pvendor)) {
1147 fr_strerror_printf("Invalid number in attribute");
1151 if (*pvendor >= FR_MAX_VENDOR) {
1152 fr_strerror_printf("Cannot handle vendor ID larger than 2^24");
1157 dv = dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1));
1159 fr_strerror_printf("Unknown vendor \"%u\" ",
1160 *pvendor & (FR_MAX_VENDOR - 1));
1165 * Start off with (attr=0, vendor=VID), and
1166 * recurse. This causes the various checks above
1170 return dict_str2oid(p + 1, pvalue, pvendor, 0);
1173 if (!sscanf_i(ptr, &value)) {
1174 fr_strerror_printf("Invalid number in attribute");
1178 if (!*pvendor && (tlv_depth == 1) && da &&
1179 (da->flags.has_tlv || da->flags.extended)) {
1182 *pvendor = *pvalue * FR_MAX_VENDOR;
1186 return dict_str2oid(p + 1, pvalue, pvendor, 1);
1190 * And pack the data according to the scheme described in
1191 * the comments at the start of this function.
1194 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
1200 return dict_str2oid(p + 1, pvalue, pvendor, tlv_depth + 1);
1207 * Bamboo skewers under the fingernails in 5, 4, 3, 2, ...
1209 static const DICT_ATTR *dict_parent(unsigned int attr, unsigned int vendor)
1211 if (vendor < FR_MAX_VENDOR) {
1212 return dict_attrbyvalue(attr & 0xff, vendor);
1216 return dict_attrbyvalue((vendor / FR_MAX_VENDOR) & 0xff, 0);
1219 return dict_attrbyvalue(attr & 0xff, vendor);
1224 * Process the ATTRIBUTE command
1226 static int process_attribute(const char* fn, const int line,
1227 unsigned int block_vendor,
1228 const DICT_ATTR *block_tlv, int tlv_depth,
1229 char **argv, int argc)
1232 unsigned int vendor = 0;
1235 unsigned int length = 0;
1239 if ((argc < 3) || (argc > 4)) {
1240 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
1246 * Dictionaries need to have real names, not shitty ones.
1248 if (strncmp(argv[1], "Attr-", 5) == 0) {
1249 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1254 memset(&flags, 0, sizeof(flags));
1257 * Look for OIDs before doing anything else.
1259 p = strchr(argv[1], '.');
1263 * Validate all entries
1265 if (!sscanf_i(argv[1], &value)) {
1266 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1271 const DICT_ATTR *da;
1273 vendor = block_vendor;
1276 * Parse the rest of the OID.
1278 if (dict_str2oid(p + 1, &value, &vendor, tlv_depth + 1) < 0) {
1281 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1283 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1286 block_vendor = vendor;
1289 * Set the flags based on the parents flags.
1291 da = dict_parent(value, vendor);
1293 fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
1297 flags.extended = da->flags.extended;
1298 flags.long_extended = da->flags.long_extended;
1299 flags.evs = da->flags.evs;
1300 if (da->flags.has_tlv) flags.is_tlv = 1;
1303 if (strncmp(argv[2], "octets[", 7) != 0) {
1305 * find the type of the attribute.
1307 type = fr_str2int(dict_attr_types, argv[2], -1);
1309 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1315 type = PW_TYPE_OCTETS;
1317 p = strchr(argv[2] + 7, ']');
1319 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1325 if (!sscanf_i(argv[1], &length)) {
1326 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1330 if ((length == 0) || (length > 253)) {
1331 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1337 * Only look up the vendor if the string
1342 * Force "length" for data types of fixed length;
1354 case PW_TYPE_IPADDR:
1355 case PW_TYPE_INTEGER:
1356 case PW_TYPE_SIGNED:
1360 case PW_TYPE_INTEGER64:
1364 case PW_TYPE_ETHERNET:
1372 case PW_TYPE_IPV6ADDR:
1376 case PW_TYPE_EXTENDED:
1377 if ((vendor != 0) || (value < 241)) {
1378 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended\" MUST be RFC attributes with value >= 241.", fn, line);
1384 case PW_TYPE_LONG_EXTENDED:
1385 if ((vendor != 0) || (value < 241)) {
1386 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"long-extended\" MUST be RFC attributes with value >= 241.", fn, line);
1390 flags.long_extended = 1;
1396 if (value != PW_VENDOR_SPECIFIC) {
1397 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1406 flags.length = length;
1408 } else { /* argc == 4: we have options */
1409 char *key, *next, *last;
1414 if (flags.extended) {
1415 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1420 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1426 next = strchr(key, ',');
1427 if (next) *(next++) = '\0';
1429 if (strcmp(key, "has_tag") == 0 ||
1430 strcmp(key, "has_tag=1") == 0) {
1431 /* Boolean flag, means this is a
1435 } else if (strncmp(key, "encrypt=", 8) == 0) {
1436 /* Encryption method, defaults to 0 (none).
1437 Currently valid is just type 2,
1438 Tunnel-Password style, which can only
1439 be applied to strings. */
1440 flags.encrypt = strtol(key + 8, &last, 0);
1442 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1447 if ((flags.encrypt == FLAG_ENCRYPT_ASCEND_SECRET) &&
1448 (type != PW_TYPE_STRING)) {
1449 fr_strerror_printf( "dict_init: %s[%d] Only \"string\" types can have the \"encrypt=3\" flag set.",
1454 } else if (strncmp(key, "array", 6) == 0) {
1458 case PW_TYPE_IPADDR:
1461 case PW_TYPE_INTEGER:
1466 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1472 * The only thing is the vendor name,
1473 * and it's a known name: allow it.
1475 } else if ((key == argv[3]) && !next) {
1477 fr_strerror_printf( "dict_init: %s[%d] New-style attributes cannot use a vendor flag.",
1483 fr_strerror_printf( "dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" is not allowed.",
1488 vendor = dict_vendorbyname(key);
1489 if (!vendor) goto unknown;
1494 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1500 if (key && !*key) break;
1504 if (block_vendor) vendor = block_vendor;
1507 * Special checks for tags, they make our life much more
1510 if (flags.has_tag) {
1512 * Only string, octets, and integer can be tagged.
1515 case PW_TYPE_STRING:
1516 case PW_TYPE_INTEGER:
1520 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1522 fr_int2str(dict_attr_types, type, "?Unknown?"));
1527 if (type == PW_TYPE_TLV) {
1528 if (vendor && (vendor < FR_MAX_VENDOR)
1530 && (vendor != DHCP_MAGIC_VENDOR)
1535 dv = dict_vendorbyvalue(vendor);
1536 if (!dv || (dv->type != 1) || (dv->length != 1)) {
1537 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1548 * TLV's can be only one octet.
1550 if ((value == 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1551 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1559 value <<= fr_attr_shift[tlv_depth];
1560 value |= block_tlv->attr;
1564 #ifdef WITH_DICTIONARY_WARNINGS
1566 * Hack to help us discover which vendors have illegal
1569 if (!vendor && (value < 256) &&
1570 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1571 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1579 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1582 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1584 fr_strerror_printf("dict_init: %s[%d]: %s",
1594 * Process the VALUE command
1596 static int process_value(const char* fn, const int line, char **argv,
1602 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1607 * For Compatibility, skip "Server-Config"
1609 if (strcasecmp(argv[0], "Server-Config") == 0)
1613 * Validate all entries
1615 if (!sscanf_i(argv[2], &value)) {
1616 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1621 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1624 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1626 fr_strerror_printf("dict_init: %s[%d]: %s",
1636 * Process the VALUE-ALIAS command
1638 * This allows VALUE mappings to be shared among multiple
1641 static int process_value_alias(const char* fn, const int line, char **argv,
1644 const DICT_ATTR *my_da, *da;
1648 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1653 my_da = dict_attrbyname(argv[0]);
1655 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1660 if (my_da->flags.has_value_alias) {
1661 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1666 da = dict_attrbyname(argv[1]);
1668 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1673 if (da->flags.has_value_alias) {
1674 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1679 if (my_da->type != da->type) {
1680 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1685 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1686 fr_strerror_printf("dict_addvalue: out of memory");
1690 dval->name[0] = '\0'; /* empty name */
1691 dval->attr = my_da->attr;
1692 dval->vendor = my_da->vendor;
1693 dval->value = da->attr;
1695 if (!fr_hash_table_insert(values_byname, dval)) {
1696 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1707 * Process the VENDOR command
1709 static int process_vendor(const char* fn, const int line, char **argv,
1713 int continuation = 0;
1714 const char *format = NULL;
1716 if ((argc < 2) || (argc > 3)) {
1717 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1723 * Validate all entries
1725 if (!isdigit((int) argv[1][0])) {
1726 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1730 value = atoi(argv[1]);
1732 /* Create a new VENDOR entry for the list */
1733 if (dict_addvendor(argv[0], value) < 0) {
1736 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1738 fr_strerror_printf("dict_init: %s[%d]: %s",
1744 * Look for a format statement
1749 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1750 format = "format=4,0";
1752 } else if (value == VENDORPEC_LUCENT) {
1753 format = "format=2,1";
1755 } else if (value == VENDORPEC_STARENT) {
1756 format = "format=2,2";
1758 } /* else no fixups to do */
1765 if (strncasecmp(format, "format=", 7) != 0) {
1766 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1772 if ((strlen(p) < 3) ||
1773 !isdigit((int) p[0]) ||
1775 !isdigit((int) p[2]) ||
1776 (p[3] && (p[3] != ','))) {
1777 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1782 type = (int) (p[0] - '0');
1783 length = (int) (p[2] - '0');
1787 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1792 if ((p[4] != 'c') ||
1794 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1800 if ((value != VENDORPEC_WIMAX) ||
1801 (type != 1) || (length != 1)) {
1802 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1808 dv = dict_vendorbyvalue(value);
1810 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1815 if ((type != 1) && (type != 2) && (type != 4)) {
1816 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1821 if ((length != 0) && (length != 1) && (length != 2)) {
1822 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1828 dv->length = length;
1829 dv->flags = continuation;
1836 * String split routine. Splits an input string IN PLACE
1837 * into pieces, based on spaces.
1839 int str2argv(char *str, char **argv, int max_argc)
1844 if (argc >= max_argc) break;
1847 * Chop out comments early.
1854 while ((*str == ' ') ||
1857 (*str == '\n')) *(str++) = '\0';
1868 (*str != '\n')) str++;
1874 #define MAX_ARGV (16)
1877 * Initialize the dictionary.
1879 static int my_dict_init(const char *parent, const char *filename,
1880 const char *src_file, int src_line)
1883 char dir[256], fn[256];
1887 unsigned int vendor;
1888 unsigned int block_vendor;
1889 struct stat statbuf;
1890 char *argv[MAX_ARGV];
1892 const DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1893 int which_block_tlv = 0;
1895 block_tlv[0] = NULL;
1896 block_tlv[1] = NULL;
1897 block_tlv[2] = NULL;
1898 block_tlv[3] = NULL;
1900 if ((strlen(parent) + 3 + strlen(filename)) > sizeof(dir)) {
1901 fr_strerror_printf("dict_init: filename name too long");
1906 * If it's an absolute dir, forget the parent dir,
1907 * and remember the new one.
1909 * If it's a relative dir, tack on the current filename
1910 * to the parent dir. And use that.
1912 if (!FR_DIR_IS_RELATIVE(filename)) {
1913 strlcpy(dir, filename, sizeof(dir));
1914 p = strrchr(dir, FR_DIR_SEP);
1917 strlcpy(fn, filename, sizeof(fn));
1919 strlcpy(dir, parent, sizeof(dir));
1920 p = strrchr(dir, FR_DIR_SEP);
1921 if (p && p[1]) strlcat(dir, "/", sizeof(dir));
1922 strlcat(dir, filename, sizeof(dir));
1923 p = strrchr(dir, FR_DIR_SEP);
1926 p = strrchr(filename, FR_DIR_SEP);
1928 snprintf(fn, sizeof(fn), "%s%s", dir, p);
1930 snprintf(fn, sizeof(fn), "%s%s", dir, filename);
1935 if ((fp = fopen(fn, "r")) == NULL) {
1937 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1938 fn, strerror(errno));
1940 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1941 src_file, src_line, fn, strerror(errno));
1946 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1947 if (!S_ISREG(statbuf.st_mode)) {
1949 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1955 * Globally writable dictionaries means that users can control
1956 * the server configuration with little difficulty.
1959 if ((statbuf.st_mode & S_IWOTH) != 0) {
1961 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1967 dict_stat_add(fn, &statbuf);
1970 * Seed the random pool with data.
1972 fr_rand_seed(&statbuf, sizeof(statbuf));
1976 while (fgets(buf, sizeof(buf), fp) != NULL) {
1978 if (buf[0] == '#' || buf[0] == 0 ||
1979 buf[0] == '\n' || buf[0] == '\r')
1983 * Comment characters should NOT be appearing anywhere but
1984 * as start of a comment;
1986 p = strchr(buf, '#');
1989 argc = str2argv(buf, argv, MAX_ARGV);
1990 if (argc == 0) continue;
1993 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
2000 * Process VALUE lines.
2002 if (strcasecmp(argv[0], "VALUE") == 0) {
2003 if (process_value(fn, line,
2004 argv + 1, argc - 1) == -1) {
2012 * Perhaps this is an attribute.
2014 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
2015 if (process_attribute(fn, line, block_vendor,
2016 block_tlv[which_block_tlv],
2018 argv + 1, argc - 1) == -1) {
2026 * See if we need to import another dictionary.
2028 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
2029 if (my_dict_init(dir, argv[1], fn, line) < 0) {
2037 * Optionally include a dictionary
2039 if (strcasecmp(argv[0], "$INCLUDE-") == 0) {
2040 int rcode = my_dict_init(dir, argv[1], fn, line);
2042 if (rcode == -2) continue;
2051 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
2052 if (process_value_alias(fn, line,
2053 argv + 1, argc - 1) == -1) {
2061 * Process VENDOR lines.
2063 if (strcasecmp(argv[0], "VENDOR") == 0) {
2064 if (process_vendor(fn, line,
2065 argv + 1, argc - 1) == -1) {
2072 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
2075 "dict_init: %s[%d] invalid BEGIN-TLV entry",
2081 da = dict_attrbyname(argv[1]);
2084 "dict_init: %s[%d]: unknown attribute %s",
2090 if (da->type != PW_TYPE_TLV) {
2092 "dict_init: %s[%d]: attribute %s is not of type tlv",
2098 if (which_block_tlv >= MAX_TLV_NEST) {
2100 "dict_init: %s[%d]: TLVs are nested too deep",
2107 block_tlv[++which_block_tlv] = da;
2111 if (strcasecmp(argv[0], "END-TLV") == 0) {
2114 "dict_init: %s[%d] invalid END-TLV entry",
2120 da = dict_attrbyname(argv[1]);
2123 "dict_init: %s[%d]: unknown attribute %s",
2129 if (da != block_tlv[which_block_tlv]) {
2131 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
2136 block_tlv[which_block_tlv--] = NULL;
2140 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
2143 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
2149 vendor = dict_vendorbyname(argv[1]);
2152 "dict_init: %s[%d]: unknown vendor %s",
2158 block_vendor = vendor;
2161 * Check for extended attr VSAs
2163 * BEGIN-VENDOR foo format=Foo-Encapsulation-Attr
2166 if (strncmp(argv[2], "format=", 7) != 0) {
2168 "dict_init: %s[%d]: Invalid format %s",
2175 da = dict_attrbyname(p);
2177 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
2183 if (!da->flags.evs) {
2184 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
2191 * Pack the encapsulating
2192 * attribute into the upper 8
2193 * bits of the vendor ID
2195 block_vendor |= (da->attr & fr_attr_mask[0]) * FR_MAX_VENDOR;
2199 } /* BEGIN-VENDOR */
2201 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
2204 "dict_init: %s[%d] invalid END-VENDOR entry",
2210 vendor = dict_vendorbyname(argv[1]);
2213 "dict_init: %s[%d]: unknown vendor %s",
2219 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2221 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2231 * Any other string: We don't recognize it.
2233 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2244 * Empty callback for hash table initialization.
2246 static int null_callback(void *ctx, void *data)
2248 ctx = ctx; /* -Wunused */
2249 data = data; /* -Wunused */
2256 * Initialize the directory, then fix the attr member of
2259 int dict_init(const char *dir, const char *fn)
2262 * Check if we need to change anything. If not, don't do
2265 if (dict_stat_check(dir, fn)) {
2270 * Free the dictionaries, and the stat cache.
2273 stat_root_dir = strdup(dir);
2274 stat_root_file = strdup(fn);
2277 * Create the table of vendor by name. There MAY NOT
2278 * be multiple vendors of the same name.
2280 * Each vendor is malloc'd, so the free function is free.
2282 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2283 dict_vendor_name_cmp,
2285 if (!vendors_byname) {
2290 * Create the table of vendors by value. There MAY
2291 * be vendors of the same value. If there are, we
2292 * pick the latest one.
2294 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2295 dict_vendor_value_cmp,
2297 if (!vendors_byvalue) {
2302 * Create the table of attributes by name. There MAY NOT
2303 * be multiple attributes of the same name.
2305 * Each attribute is malloc'd, so the free function is free.
2307 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2310 if (!attributes_byname) {
2315 * Create the table of attributes by value. There MAY
2316 * be attributes of the same value. If there are, we
2317 * pick the latest one.
2319 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2320 dict_attr_value_cmp,
2322 if (!attributes_byvalue) {
2327 * Horrible hacks for combo-IP.
2329 attributes_combo = fr_hash_table_create(dict_attr_combo_hash,
2330 dict_attr_combo_cmp,
2332 if (!attributes_combo) {
2336 values_byname = fr_hash_table_create(dict_value_name_hash,
2337 dict_value_name_cmp,
2339 if (!values_byname) {
2343 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2344 dict_value_value_cmp,
2346 if (!values_byvalue) {
2350 value_fixup = NULL; /* just to be safe. */
2352 if (my_dict_init(dir, fn, NULL, 0) < 0)
2357 value_fixup_t *this, *next;
2359 for (this = value_fixup; this != NULL; this = next) {
2362 a = dict_attrbyname(this->attrstr);
2365 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2366 this->attrstr, this->dval->name);
2367 return -1; /* leak, but they should die... */
2370 this->dval->attr = a->attr;
2373 * Add the value into the dictionary.
2375 if (!fr_hash_table_replace(values_byname,
2377 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2382 * Allow them to use the old name, but
2383 * prefer the new name when printing
2386 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2387 fr_hash_table_replace(values_byvalue,
2393 * Just so we don't lose track of things.
2400 * Walk over all of the hash tables to ensure they're
2401 * initialized. We do this because the threads may perform
2402 * lookups, and we don't want multi-threaded re-ordering
2403 * of the table entries. That would be bad.
2405 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2406 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2408 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2409 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2411 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2412 fr_hash_table_walk(values_byname, null_callback, NULL);
2417 static size_t print_attr_oid(char *buffer, size_t size, unsigned int attr,
2426 return snprintf(buffer, size, "%u", attr);
2429 return snprintf(buffer, size, "%u", attr & 0xffff);
2433 len = snprintf(buffer, size, "%u", attr & 0xff);
2437 if ((attr >> 8) == 0) return len;
2443 for (nest = 1; nest <= fr_attr_max_tlv; nest++) {
2444 if (((attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]) == 0) break;
2446 len = snprintf(buffer, size, ".%u",
2447 (attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]);
2457 /** Free dynamically allocated (unknown attributes)
2459 * If the da was dynamically allocated it will be freed, else the function
2460 * will return without doing anything.
2462 * @param da to free.
2464 void dict_attr_free(DICT_ATTR const **da)
2468 if (!da || !*da) return;
2470 /* Don't free real DAs */
2471 if (!(*da)->flags.is_unknown) {
2475 memcpy(&tmp, &da, sizeof(*tmp));
2481 /** Copies a dictionary attr
2483 * If the attr is dynamically allocated (unknown attribute), then it will be
2484 * copied to a new attr.
2486 * If the attr is known, a pointer to the da will be returned.
2488 * @param da to copy.
2489 * @param vp_free if TRUE, da will be freed at the same time as the
2490 * VALUE_PAIR which contains it.
2491 * @return return a copy of the da.
2493 const DICT_ATTR *dict_attr_copy(const DICT_ATTR *da, int vp_free)
2497 if (!da) return NULL;
2499 if (!da->flags.is_unknown) {
2503 copy = malloc(DICT_ATTR_SIZE);
2505 fr_strerror_printf("Out of memory");
2509 memcpy(copy, da, DICT_ATTR_SIZE);
2510 copy->flags.vp_free = (vp_free != 0);
2516 /** Allocs an dictionary attr for unknown attributes
2518 * Allocates a dict attr for an unknown attribute/vendor/type
2519 * without adding it to dictionary pools/hashes.
2521 * @note Must be freed with dict_attr_free if not used as part of a valuepair.
2523 * @param[in] attr number.
2524 * @param[in] vendor number.
2525 * @param[in] vp_free if > 0 DICT_ATTR will be freed on VALUE_PAIR free.
2526 * @return new dictionary attribute.
2528 const DICT_ATTR *dict_attrunknown(unsigned int attr, unsigned int vendor,
2535 size_t bufsize = DICT_ATTR_MAX_NAME_LEN;
2537 da = malloc(DICT_ATTR_SIZE);
2539 fr_strerror_printf("Out of memory");
2542 memset(da, 0, DICT_ATTR_SIZE);
2545 da->vendor = vendor;
2546 da->type = PW_TYPE_OCTETS;
2547 da->flags.is_unknown = TRUE;
2548 da->flags.vp_free = (vp_free != 0);
2552 len = snprintf(p, bufsize, "Attr-");
2556 if (vendor > FR_MAX_VENDOR) {
2557 len = snprintf(p, bufsize, "%u.", vendor / FR_MAX_VENDOR);
2560 vendor &= (FR_MAX_VENDOR) - 1;
2567 * dv_type is the length of the vendor's type field
2568 * RFC 2865 never defined a mandatory length, so
2569 * different vendors have different length type fields.
2571 dv = dict_vendorbyvalue(vendor);
2575 len = snprintf(p, bufsize, "26.%u.", vendor);
2581 p += print_attr_oid(p, bufsize , attr, dv_type);
2586 /** Create a DICT_ATTR from an ASCII attribute and value
2588 * Where the attribute name is in the form:
2590 * - Attr-%d.%d.%d...
2591 * - Vendor-%d-Attr-%d
2592 * - VendorName-Attr-%d
2594 * @todo should check attr/vendor against dictionary and return the real da.
2596 * @param[in] attribute name.
2597 * @param[in] vp_free if > 0 DICT_ATTR will be freed on VALUE_PAIR free.
2598 * @return new da or NULL on error.
2600 const DICT_ATTR *dict_attrunknownbyname(const char *attribute, int vp_free)
2602 unsigned int attr, vendor = 0;
2603 unsigned int dv_type = 1; /* The type of vendor field */
2605 const char *p = attribute;
2609 const DICT_ATTR *da;
2612 * Pull off vendor prefix first.
2614 if (strncasecmp(p, "Attr-", 5) != 0) {
2615 if (strncasecmp(p, "Vendor-", 7) == 0) {
2616 vendor = (int) strtol(p + 7, &q, 10);
2617 if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2618 fr_strerror_printf("Invalid vendor value in "
2619 "attribute name \"%s\"",
2626 /* must be vendor name */
2633 fr_strerror_printf("Invalid vendor name in "
2634 "attribute name \"%s\"",
2639 if ((size_t) (q - p) >= sizeof(buffer)) {
2640 fr_strerror_printf("Vendor name too long "
2641 "in attribute name \"%s\"",
2646 memcpy(buffer, p, (q - p));
2647 buffer[q - p] = '\0';
2649 vendor = dict_vendorbyname(buffer);
2651 fr_strerror_printf("Unknown vendor name in "
2652 "attribute name \"%s\"",
2661 fr_strerror_printf("Invalid text following vendor "
2662 "definition in attribute name "
2663 "\"%s\"", attribute);
2672 if (strncasecmp(p, "Attr-", 5) != 0) {
2673 fr_strerror_printf("Invalid format in attribute name \"%s\"",
2678 attr = strtol(p + 5, &q, 10);
2681 * Invalid attribute.
2684 fr_strerror_printf("Invalid value in attribute name \"%s\"",
2693 * VendorName-Attr-%d
2697 * Anything else is invalid.
2699 if (((vendor != 0) && (*p != '\0')) ||
2700 ((vendor == 0) && *p && (*p != '.'))) {
2702 fr_strerror_printf("Invalid OID");
2707 * Look for OIDs. Require the "Attr-26.Vendor-Id.type"
2708 * format, and disallow "Vendor-%d-Attr-%d" and
2709 * "VendorName-Attr-%d"
2711 * This section parses the Vendor-Id portion of
2712 * Attr-%d.%d. where the first number is 26, *or* an
2713 * extended attribute of the "evs" data type.
2716 da = dict_attrbyvalue(attr, 0);
2718 fr_strerror_printf("Cannot parse attributes without "
2723 if ((attr != PW_VENDOR_SPECIFIC) &&
2724 !(da->flags.extended || da->flags.long_extended)) {
2725 fr_strerror_printf("Standard attributes cannot use "
2730 if ((attr == PW_VENDOR_SPECIFIC) || da->flags.evs) {
2731 vendor = strtol(p + 1, &q, 10);
2732 if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2733 fr_strerror_printf("Invalid vendor");
2737 if (*q != '.') goto invalid;
2741 if (da->flags.evs) {
2742 vendor |= attr * FR_MAX_VENDOR;
2745 } /* else the second number is a TLV number */
2749 * Get the expected maximum size of the attribute.
2752 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
2755 if (dv_type > 3) dv_type = 3; /* hack */
2760 * Parse the next number. It could be a Vendor-Type
2761 * of 1..2^24, or it could be a TLV.
2764 attr = strtol(p + 1, &q, 10);
2766 fr_strerror_printf("Invalid attribute number");
2784 * Enforce a maximum value on the attribute number.
2786 if (attr >= (unsigned) (1 << (dv_type << 3))) goto invalid;
2789 if (dict_str2oid(p + 1, &attr, &vendor, 1) < 0) {
2794 return dict_attrunknown(attr, vendor, vp_free);
2798 * Get an attribute by its numerical value.
2800 const DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2804 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2807 dattr.vendor = vendor;
2809 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2814 * @brief Get an attribute by its numerical value. and data type
2816 * Used only for COMBO_IP
2818 * @return The attribute, or NULL if not found
2820 const DICT_ATTR *dict_attrbytype(unsigned int attr, unsigned int vendor,
2826 dattr.vendor = vendor;
2829 return fr_hash_table_finddata(attributes_combo, &dattr);
2833 * @brief Using a parent and attr/vendor, find a child attr/vendor
2835 int dict_attr_child(const DICT_ATTR *parent,
2836 unsigned int *pattr, unsigned int *pvendor)
2838 unsigned int attr, vendor;
2841 if (!parent || !pattr || !pvendor) return FALSE;
2847 * Only some types can have children
2849 switch (parent->type) {
2850 default: return FALSE;
2855 case PW_TYPE_EXTENDED:
2856 case PW_TYPE_LONG_EXTENDED:
2860 if ((vendor == 0) && (parent->vendor != 0)) return FALSE;
2863 * Bootstrap by starting off with the parents values.
2865 dattr.attr = parent->attr;
2866 dattr.vendor = parent->vendor;
2869 * Do various butchery to insert the "attr" value.
2871 * 00VID 000000AA normal VSA for vendor VID
2872 * 00VID DDCCBBAA normal VSAs with TLVs
2873 * EE000 000000AA extended attr (241.1)
2874 * EE000 DDCCBBAA extended attr with TLVs
2875 * EEVID 000000AA EVS with vendor VID, attr AAA
2876 * EEVID DDCCBBAA EVS with TLVs
2878 if (!dattr.vendor) {
2879 dattr.vendor = parent->attr * FR_MAX_VENDOR;
2880 dattr.vendor |= vendor;
2887 * Trying to nest too deep. It's an error
2889 if (parent->attr & (fr_attr_mask[MAX_TLV_NEST] << fr_attr_shift[MAX_TLV_NEST])) {
2893 for (i = MAX_TLV_NEST - 1; i >= 0; i--) {
2894 if ((parent->attr & (fr_attr_mask[i] << fr_attr_shift[i]))) {
2895 dattr.attr |= (attr & fr_attr_mask[i + 1]) << fr_attr_shift[i + 1];
2905 fprintf(stderr, "LOOKING FOR %08x %08x + %08x %08x --> %08x %08x\n",
2906 parent->vendor, parent->attr, attr, vendor,
2907 dattr.vendor, dattr.attr);
2910 *pattr = dattr.attr;
2911 *pvendor = dattr.vendor;
2916 * Get an attribute by it's numerical value, and the parent
2918 const DICT_ATTR *dict_attrbyparent(const DICT_ATTR *parent,
2919 unsigned int attr, unsigned int vendor)
2921 unsigned int my_attr, my_vendor;
2927 if (!dict_attr_child(parent, &my_attr, &my_vendor)) return NULL;
2929 dattr.attr = my_attr;
2930 dattr.vendor = my_vendor;
2932 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2937 * Get an attribute by its name.
2939 const DICT_ATTR *dict_attrbyname(const char *name)
2942 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2944 if (!name) return NULL;
2946 da = (DICT_ATTR *) buffer;
2947 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2949 return fr_hash_table_finddata(attributes_byname, da);
2953 * Associate a value with an attribute and return it.
2955 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2957 DICT_VALUE dval, *dv;
2960 * First, look up aliases.
2963 dval.vendor = vendor;
2964 dval.name[0] = '\0';
2967 * Look up the attribute alias target, and use
2968 * the correct attribute number if found.
2970 dv = fr_hash_table_finddata(values_byname, &dval);
2971 if (dv) dval.attr = dv->value;
2975 return fr_hash_table_finddata(values_byvalue, &dval);
2979 * Associate a value with an attribute and return it.
2981 const char *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value)
2985 dv = dict_valbyattr(attr, vendor, value);
2992 * Get a value by its name, keyed off of an attribute.
2994 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2996 DICT_VALUE *my_dv, *dv;
2997 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2999 if (!name) return NULL;
3001 my_dv = (DICT_VALUE *) buffer;
3003 my_dv->vendor = vendor;
3004 my_dv->name[0] = '\0';
3007 * Look up the attribute alias target, and use
3008 * the correct attribute number if found.
3010 dv = fr_hash_table_finddata(values_byname, my_dv);
3011 if (dv) my_dv->attr = dv->value;
3013 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
3015 return fr_hash_table_finddata(values_byname, my_dv);
3019 * Get the vendor PEC based on the vendor name
3021 * This is efficient only for small numbers of vendors.
3023 int dict_vendorbyname(const char *name)
3026 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
3028 if (!name) return 0;
3030 dv = (DICT_VENDOR *) buffer;
3031 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
3033 dv = fr_hash_table_finddata(vendors_byname, dv);
3036 return dv->vendorpec;
3040 * Return the vendor struct based on the PEC.
3042 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
3046 dv.vendorpec = vendorpec;
3048 return fr_hash_table_finddata(vendors_byvalue, &dv);