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 = malloc(sizeof(*v4));
857 v6 = malloc(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;
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) &&
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);
1381 type = PW_TYPE_OCTETS;
1385 case PW_TYPE_LONG_EXTENDED:
1386 if ((vendor != 0) || (value < 241)) {
1387 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"long-extended\" MUST be RFC attributes with value >= 241.", fn, line);
1390 type = PW_TYPE_OCTETS;
1392 flags.long_extended = 1;
1396 type = PW_TYPE_OCTETS;
1399 if (value != PW_VENDOR_SPECIFIC) {
1400 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1409 flags.length = length;
1411 } else { /* argc == 4: we have options */
1412 char *key, *next, *last;
1417 if (flags.extended) {
1418 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1423 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1429 next = strchr(key, ',');
1430 if (next) *(next++) = '\0';
1432 if (strcmp(key, "has_tag") == 0 ||
1433 strcmp(key, "has_tag=1") == 0) {
1434 /* Boolean flag, means this is a
1438 } else if (strncmp(key, "encrypt=", 8) == 0) {
1439 /* Encryption method, defaults to 0 (none).
1440 Currently valid is just type 2,
1441 Tunnel-Password style, which can only
1442 be applied to strings. */
1443 flags.encrypt = strtol(key + 8, &last, 0);
1445 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1450 if ((flags.encrypt == FLAG_ENCRYPT_ASCEND_SECRET) &&
1451 (type != PW_TYPE_STRING)) {
1452 fr_strerror_printf( "dict_init: %s[%d] Only \"string\" types can have the \"encrypt=3\" flag set.",
1457 } else if (strncmp(key, "array", 6) == 0) {
1461 case PW_TYPE_IPADDR:
1464 case PW_TYPE_INTEGER:
1469 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1475 * The only thing is the vendor name,
1476 * and it's a known name: allow it.
1478 } else if ((key == argv[3]) && !next) {
1480 fr_strerror_printf( "dict_init: %s[%d] New-style attributes cannot use a vendor flag.",
1486 fr_strerror_printf( "dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" is not allowed.",
1491 vendor = dict_vendorbyname(key);
1492 if (!vendor) goto unknown;
1497 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1503 if (key && !*key) break;
1507 if (block_vendor) vendor = block_vendor;
1510 * Special checks for tags, they make our life much more
1513 if (flags.has_tag) {
1515 * Only string, octets, and integer can be tagged.
1518 case PW_TYPE_STRING:
1519 case PW_TYPE_INTEGER:
1523 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1525 fr_int2str(dict_attr_types, type, "?Unknown?"));
1530 if (type == PW_TYPE_TLV) {
1531 if (vendor && (vendor < FR_MAX_VENDOR)
1533 && (vendor != DHCP_MAGIC_VENDOR)
1538 dv = dict_vendorbyvalue(vendor);
1539 if (!dv || (dv->type != 1) || (dv->length != 1)) {
1540 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1551 * TLV's can be only one octet.
1553 if ((value == 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1554 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1562 value <<= fr_attr_shift[tlv_depth];
1563 value |= block_tlv->attr;
1567 #ifdef WITH_DICTIONARY_WARNINGS
1569 * Hack to help us discover which vendors have illegal
1572 if (!vendor && (value < 256) &&
1573 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1574 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1582 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1585 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1587 fr_strerror_printf("dict_init: %s[%d]: %s",
1597 * Process the VALUE command
1599 static int process_value(const char* fn, const int line, char **argv,
1605 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1610 * For Compatibility, skip "Server-Config"
1612 if (strcasecmp(argv[0], "Server-Config") == 0)
1616 * Validate all entries
1618 if (!sscanf_i(argv[2], &value)) {
1619 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1624 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1627 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1629 fr_strerror_printf("dict_init: %s[%d]: %s",
1639 * Process the VALUE-ALIAS command
1641 * This allows VALUE mappings to be shared among multiple
1644 static int process_value_alias(const char* fn, const int line, char **argv,
1647 const DICT_ATTR *my_da, *da;
1651 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1656 my_da = dict_attrbyname(argv[0]);
1658 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1663 if (my_da->flags.has_value_alias) {
1664 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1669 da = dict_attrbyname(argv[1]);
1671 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1676 if (da->flags.has_value_alias) {
1677 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1682 if (my_da->type != da->type) {
1683 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1688 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1689 fr_strerror_printf("dict_addvalue: out of memory");
1693 dval->name[0] = '\0'; /* empty name */
1694 dval->attr = my_da->attr;
1695 dval->vendor = my_da->vendor;
1696 dval->value = da->attr;
1698 if (!fr_hash_table_insert(values_byname, dval)) {
1699 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1710 * Process the VENDOR command
1712 static int process_vendor(const char* fn, const int line, char **argv,
1716 int continuation = 0;
1717 const char *format = NULL;
1719 if ((argc < 2) || (argc > 3)) {
1720 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1726 * Validate all entries
1728 if (!isdigit((int) argv[1][0])) {
1729 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1733 value = atoi(argv[1]);
1735 /* Create a new VENDOR entry for the list */
1736 if (dict_addvendor(argv[0], value) < 0) {
1739 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1741 fr_strerror_printf("dict_init: %s[%d]: %s",
1747 * Look for a format statement
1752 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1753 format = "format=4,0";
1755 } else if (value == VENDORPEC_LUCENT) {
1756 format = "format=2,1";
1758 } else if (value == VENDORPEC_STARENT) {
1759 format = "format=2,2";
1761 } /* else no fixups to do */
1768 if (strncasecmp(format, "format=", 7) != 0) {
1769 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1775 if ((strlen(p) < 3) ||
1776 !isdigit((int) p[0]) ||
1778 !isdigit((int) p[2]) ||
1779 (p[3] && (p[3] != ','))) {
1780 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1785 type = (int) (p[0] - '0');
1786 length = (int) (p[2] - '0');
1790 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1795 if ((p[4] != 'c') ||
1797 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1803 if ((value != VENDORPEC_WIMAX) ||
1804 (type != 1) || (length != 1)) {
1805 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1811 dv = dict_vendorbyvalue(value);
1813 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1818 if ((type != 1) && (type != 2) && (type != 4)) {
1819 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1824 if ((length != 0) && (length != 1) && (length != 2)) {
1825 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1831 dv->length = length;
1832 dv->flags = continuation;
1839 * String split routine. Splits an input string IN PLACE
1840 * into pieces, based on spaces.
1842 int str2argv(char *str, char **argv, int max_argc)
1847 if (argc >= max_argc) break;
1850 * Chop out comments early.
1857 while ((*str == ' ') ||
1860 (*str == '\n')) *(str++) = '\0';
1871 (*str != '\n')) str++;
1877 #define MAX_ARGV (16)
1880 * Initialize the dictionary.
1882 static int my_dict_init(const char *dir, const char *fn,
1883 const char *src_file, int src_line)
1890 unsigned int vendor;
1891 unsigned int block_vendor;
1892 struct stat statbuf;
1893 char *argv[MAX_ARGV];
1895 const DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1896 int which_block_tlv = 0;
1898 block_tlv[0] = NULL;
1899 block_tlv[1] = NULL;
1900 block_tlv[2] = NULL;
1901 block_tlv[3] = NULL;
1903 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1904 strlen(dir) >= sizeof(dirtmp) / 2) {
1905 fr_strerror_printf("dict_init: filename name too long");
1910 * First see if fn is relative to dir. If so, create
1911 * new filename. If not, remember the absolute dir.
1913 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1917 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1918 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1922 if ((fp = fopen(fn, "r")) == NULL) {
1924 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1925 fn, strerror(errno));
1927 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1928 src_file, src_line, fn, strerror(errno));
1933 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1934 if (!S_ISREG(statbuf.st_mode)) {
1936 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1942 * Globally writable dictionaries means that users can control
1943 * the server configuration with little difficulty.
1946 if ((statbuf.st_mode & S_IWOTH) != 0) {
1948 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1954 dict_stat_add(fn, &statbuf);
1957 * Seed the random pool with data.
1959 fr_rand_seed(&statbuf, sizeof(statbuf));
1963 while (fgets(buf, sizeof(buf), fp) != NULL) {
1965 if (buf[0] == '#' || buf[0] == 0 ||
1966 buf[0] == '\n' || buf[0] == '\r')
1970 * Comment characters should NOT be appearing anywhere but
1971 * as start of a comment;
1973 p = strchr(buf, '#');
1976 argc = str2argv(buf, argv, MAX_ARGV);
1977 if (argc == 0) continue;
1980 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1987 * Process VALUE lines.
1989 if (strcasecmp(argv[0], "VALUE") == 0) {
1990 if (process_value(fn, line,
1991 argv + 1, argc - 1) == -1) {
1999 * Perhaps this is an attribute.
2001 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
2002 if (process_attribute(fn, line, block_vendor,
2003 block_tlv[which_block_tlv],
2005 argv + 1, argc - 1) == -1) {
2013 * See if we need to import another dictionary.
2015 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
2016 if (my_dict_init(dir, argv[1], fn, line) < 0) {
2024 * Optionally include a dictionary
2026 if (strcasecmp(argv[0], "$INCLUDE-") == 0) {
2027 int rcode = my_dict_init(dir, argv[1], fn, line);
2029 if (rcode == -2) continue;
2038 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
2039 if (process_value_alias(fn, line,
2040 argv + 1, argc - 1) == -1) {
2048 * Process VENDOR lines.
2050 if (strcasecmp(argv[0], "VENDOR") == 0) {
2051 if (process_vendor(fn, line,
2052 argv + 1, argc - 1) == -1) {
2059 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
2062 "dict_init: %s[%d] invalid BEGIN-TLV entry",
2068 da = dict_attrbyname(argv[1]);
2071 "dict_init: %s[%d]: unknown attribute %s",
2077 if (da->type != PW_TYPE_TLV) {
2079 "dict_init: %s[%d]: attribute %s is not of type tlv",
2085 if (which_block_tlv >= MAX_TLV_NEST) {
2087 "dict_init: %s[%d]: TLVs are nested too deep",
2094 block_tlv[++which_block_tlv] = da;
2098 if (strcasecmp(argv[0], "END-TLV") == 0) {
2101 "dict_init: %s[%d] invalid END-TLV entry",
2107 da = dict_attrbyname(argv[1]);
2110 "dict_init: %s[%d]: unknown attribute %s",
2116 if (da != block_tlv[which_block_tlv]) {
2118 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
2123 block_tlv[which_block_tlv--] = NULL;
2127 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
2130 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
2136 vendor = dict_vendorbyname(argv[1]);
2139 "dict_init: %s[%d]: unknown vendor %s",
2145 block_vendor = vendor;
2148 * Check for extended attr VSAs
2150 * BEGIN-VENDOR foo format=Foo-Encapsulation-Attr
2153 if (strncmp(argv[2], "format=", 7) != 0) {
2155 "dict_init: %s[%d]: Invalid format %s",
2162 da = dict_attrbyname(p);
2164 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
2170 if (!da->flags.evs) {
2171 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
2178 * Pack the encapsulating
2179 * attribute into the upper 8
2180 * bits of the vendor ID
2182 block_vendor |= (da->attr & fr_attr_mask[0]) * FR_MAX_VENDOR;
2186 } /* BEGIN-VENDOR */
2188 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
2191 "dict_init: %s[%d] invalid END-VENDOR entry",
2197 vendor = dict_vendorbyname(argv[1]);
2200 "dict_init: %s[%d]: unknown vendor %s",
2206 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2208 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2218 * Any other string: We don't recognize it.
2220 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2231 * Empty callback for hash table initialization.
2233 static int null_callback(void *ctx, void *data)
2235 ctx = ctx; /* -Wunused */
2236 data = data; /* -Wunused */
2243 * Initialize the directory, then fix the attr member of
2246 int dict_init(const char *dir, const char *fn)
2249 * Check if we need to change anything. If not, don't do
2252 if (dict_stat_check(dir, fn)) {
2257 * Free the dictionaries, and the stat cache.
2260 stat_root_dir = strdup(dir);
2261 stat_root_file = strdup(fn);
2264 * Create the table of vendor by name. There MAY NOT
2265 * be multiple vendors of the same name.
2267 * Each vendor is malloc'd, so the free function is free.
2269 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2270 dict_vendor_name_cmp,
2272 if (!vendors_byname) {
2277 * Create the table of vendors by value. There MAY
2278 * be vendors of the same value. If there are, we
2279 * pick the latest one.
2281 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2282 dict_vendor_value_cmp,
2284 if (!vendors_byvalue) {
2289 * Create the table of attributes by name. There MAY NOT
2290 * be multiple attributes of the same name.
2292 * Each attribute is malloc'd, so the free function is free.
2294 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2297 if (!attributes_byname) {
2302 * Create the table of attributes by value. There MAY
2303 * be attributes of the same value. If there are, we
2304 * pick the latest one.
2306 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2307 dict_attr_value_cmp,
2309 if (!attributes_byvalue) {
2314 * Horrible hacks for combo-IP.
2316 attributes_combo = fr_hash_table_create(dict_attr_combo_hash,
2317 dict_attr_combo_cmp,
2319 if (!attributes_combo) {
2323 values_byname = fr_hash_table_create(dict_value_name_hash,
2324 dict_value_name_cmp,
2326 if (!values_byname) {
2330 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2331 dict_value_value_cmp,
2333 if (!values_byvalue) {
2337 value_fixup = NULL; /* just to be safe. */
2339 if (my_dict_init(dir, fn, NULL, 0) < 0)
2344 value_fixup_t *this, *next;
2346 for (this = value_fixup; this != NULL; this = next) {
2349 a = dict_attrbyname(this->attrstr);
2352 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2353 this->attrstr, this->dval->name);
2354 return -1; /* leak, but they should die... */
2357 this->dval->attr = a->attr;
2360 * Add the value into the dictionary.
2362 if (!fr_hash_table_replace(values_byname,
2364 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2369 * Allow them to use the old name, but
2370 * prefer the new name when printing
2373 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2374 fr_hash_table_replace(values_byvalue,
2380 * Just so we don't lose track of things.
2387 * Walk over all of the hash tables to ensure they're
2388 * initialized. We do this because the threads may perform
2389 * lookups, and we don't want multi-threaded re-ordering
2390 * of the table entries. That would be bad.
2392 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2393 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2395 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2396 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2398 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2399 fr_hash_table_walk(values_byname, null_callback, NULL);
2404 static size_t print_attr_oid(char *buffer, size_t size, unsigned int attr,
2413 return snprintf(buffer, size, "%u", attr);
2416 return snprintf(buffer, size, "%u", attr & 0xffff);
2420 len = snprintf(buffer, size, "%u", attr & 0xff);
2424 if ((attr >> 8) == 0) return len;
2430 for (nest = 1; nest <= fr_attr_max_tlv; nest++) {
2431 if (((attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]) == 0) break;
2433 len = snprintf(buffer, size, ".%u",
2434 (attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]);
2444 /** Free dynamically allocated (unknown attributes)
2446 * If the da was dynamically allocated it will be freed, else the function
2447 * will return without doing anything.
2449 * @param da to free.
2451 void dict_attr_free(DICT_ATTR const **da)
2455 /* Don't free real DAs */
2456 if (!(*da)->flags.is_unknown) {
2460 memcpy(&tmp, &da, sizeof(*tmp));
2466 /** Copies a dictionary attr
2468 * If the attr is dynamically allocated (unknown attribute), then it will be
2469 * copied to a new attr.
2471 * If the attr is known, a pointer to the da will be returned.
2473 * @param da to copy.
2474 * @return return a copy of the da.
2476 const DICT_ATTR *dict_attr_copy(const DICT_ATTR *da, int vp_free)
2480 if (!da) return NULL;
2482 if (!da->flags.is_unknown) {
2486 copy = malloc(DICT_ATTR_SIZE);
2488 fr_strerror_printf("Out of memory");
2492 memcpy(copy, da, DICT_ATTR_SIZE);
2493 copy->flags.vp_free = (vp_free != 0);
2499 /** Allocs an dictionary attr for unknown attributes
2501 * Allocates a dict attr for an unknown attribute/vendor/type
2502 * without adding it to dictionary pools/hashes.
2504 * @note Must be freed with dict_attr_free if not used as part of a valuepair.
2506 * @param[in] attr number.
2507 * @param[in] vendor number.
2508 * @param[in] vp_free if > 0 DICT_ATTR will be freed on VALUE_PAIR free.
2509 * @return new dictionary attribute.
2511 const DICT_ATTR *dict_attrunknown(unsigned int attr, unsigned int vendor,
2518 size_t bufsize = DICT_ATTR_MAX_NAME_LEN;
2520 da = malloc(DICT_ATTR_SIZE);
2522 fr_strerror_printf("Out of memory");
2525 memset(da, 0, DICT_ATTR_SIZE);
2528 da->vendor = vendor;
2529 da->type = PW_TYPE_OCTETS;
2530 da->flags.is_unknown = TRUE;
2531 da->flags.vp_free = (vp_free != 0);
2535 len = snprintf(p, bufsize, "Attr-");
2539 if (vendor > FR_MAX_VENDOR) {
2540 len = snprintf(p, bufsize, "%u.", vendor / FR_MAX_VENDOR);
2543 vendor &= (FR_MAX_VENDOR) - 1;
2550 * dv_type is the length of the vendor's type field
2551 * RFC 2865 never defined a mandatory length, so
2552 * different vendors have different length type fields.
2554 dv = dict_vendorbyvalue(vendor);
2558 len = snprintf(p, bufsize, "26.%u.", vendor);
2564 p += print_attr_oid(p, bufsize , attr, dv_type);
2569 /** Create a DICT_ATTR from an ASCII attribute and value
2571 * Where the attribute name is in the form:
2573 * - Attr-%d.%d.%d...
2574 * - Vendor-%d-Attr-%d
2575 * - VendorName-Attr-%d
2577 * @todo should check attr/vendor against dictionary and return the real da.
2579 * @param[in] attribute name.
2580 * @param[in] vp_free if > 0 DICT_ATTR will be freed on VALUE_PAIR free.
2581 * @return new da or NULL on error.
2583 const DICT_ATTR *dict_attrunknownbyname(const char *attribute, int vp_free)
2585 unsigned int attr, vendor = 0;
2586 unsigned int dv_type = 1; /* The type of vendor field */
2588 const char *p = attribute;
2592 const DICT_ATTR *da;
2595 * Pull off vendor prefix first.
2597 if (strncasecmp(p, "Attr-", 5) != 0) {
2598 if (strncasecmp(p, "Vendor-", 7) == 0) {
2599 vendor = (int) strtol(p + 7, &q, 10);
2600 if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2601 fr_strerror_printf("Invalid vendor value in "
2602 "attribute name \"%s\"",
2609 /* must be vendor name */
2616 fr_strerror_printf("Invalid vendor name in "
2617 "attribute name \"%s\"",
2622 if ((size_t) (q - p) >= sizeof(buffer)) {
2623 fr_strerror_printf("Vendor name too long "
2624 "in attribute name \"%s\"",
2629 memcpy(buffer, p, (q - p));
2630 buffer[q - p] = '\0';
2632 vendor = dict_vendorbyname(buffer);
2634 fr_strerror_printf("Unknown vendor name in "
2635 "attribute name \"%s\"",
2644 fr_strerror_printf("Invalid text following vendor "
2645 "definition in attribute name "
2646 "\"%s\"", attribute);
2655 if (strncasecmp(p, "Attr-", 5) != 0) {
2656 fr_strerror_printf("Invalid format in attribute name \"%s\"",
2661 attr = strtol(p + 5, &q, 10);
2664 * Invalid attribute.
2667 fr_strerror_printf("Invalid value in attribute name \"%s\"",
2676 * VendorName-Attr-%d
2680 * Anything else is invalid.
2682 if (((vendor != 0) && (*p != '\0')) ||
2683 ((vendor == 0) && *p && (*p != '.'))) {
2685 fr_strerror_printf("Invalid OID");
2690 * Look for OIDs. Require the "Attr-26.Vendor-Id.type"
2691 * format, and disallow "Vendor-%d-Attr-%d" and
2692 * "VendorName-Attr-%d"
2694 * This section parses the Vendor-Id portion of
2695 * Attr-%d.%d. where the first number is 26, *or* an
2696 * extended attribute of the "evs" data type.
2699 da = dict_attrbyvalue(attr, 0);
2701 fr_strerror_printf("Cannot parse attributes without "
2706 if ((attr != PW_VENDOR_SPECIFIC) &&
2707 !(da->flags.extended || da->flags.long_extended)) {
2708 fr_strerror_printf("Standard attributes cannot use "
2713 if ((attr == PW_VENDOR_SPECIFIC) || da->flags.evs) {
2714 vendor = strtol(p + 1, &q, 10);
2715 if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2716 fr_strerror_printf("Invalid vendor");
2720 if (*q != '.') goto invalid;
2724 if (da->flags.evs) {
2725 vendor |= attr * FR_MAX_VENDOR;
2728 } /* else the second number is a TLV number */
2732 * Get the expected maximum size of the attribute.
2735 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
2738 if (dv_type > 3) dv_type = 3; /* hack */
2743 * Parse the next number. It could be a Vendor-Type
2744 * of 1..2^24, or it could be a TLV.
2747 attr = strtol(p + 1, &q, 10);
2749 fr_strerror_printf("Invalid attribute number");
2767 * Enforce a maximum value on the attribute number.
2769 if (attr >= (unsigned) (1 << (dv_type << 3))) goto invalid;
2772 if (dict_str2oid(p + 1, &attr, &vendor, 1) < 0) {
2777 return dict_attrunknown(attr, vendor, vp_free);
2781 * Get an attribute by its numerical value.
2783 const DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2787 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2790 dattr.vendor = vendor;
2792 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2797 * @brief Get an attribute by its numerical value. and data type
2799 * Used only for COMBO_IP
2801 * @return The attribute, or NULL if not found
2803 const DICT_ATTR *dict_attrbytype(unsigned int attr, unsigned int vendor,
2809 dattr.vendor = vendor;
2812 return fr_hash_table_finddata(attributes_combo, &dattr);
2817 * @brief Using a parent and attr/vendor, find a child attr/vendor
2819 int dict_attr_child(const DICT_ATTR *parent,
2820 unsigned int *pattr, unsigned int *pvendor)
2822 unsigned int attr, vendor;
2825 if (!parent || !pattr || !pvendor) return FALSE;
2831 * Only some types can have children
2833 switch (parent->type) {
2834 default: return FALSE;
2839 case PW_TYPE_EXTENDED:
2840 case PW_TYPE_LONG_EXTENDED:
2844 if ((vendor == 0) && (parent->vendor != 0)) return FALSE;
2847 * Bootstrap by starting off with the parents values.
2849 dattr.attr = parent->attr;
2850 dattr.vendor = parent->vendor;
2853 * Do various butchery to insert the "attr" value.
2855 * 00VID 000000AA normal VSA for vendor VID
2856 * 00VID DDCCBBAA normal VSAs with TLVs
2857 * EE000 000000AA extended attr (241.1)
2858 * EE000 DDCCBBAA extended attr with TLVs
2859 * EEVID 000000AA EVS with vendor VID, attr AAA
2860 * EEVID DDCCBBAA EVS with TLVs
2862 if (!dattr.vendor) {
2863 dattr.vendor = parent->attr * FR_MAX_VENDOR;
2864 dattr.vendor |= vendor;
2871 * Trying to nest too deep. It's an error
2873 if (parent->attr & (fr_attr_mask[MAX_TLV_NEST] << fr_attr_shift[MAX_TLV_NEST])) {
2877 for (i = MAX_TLV_NEST - 1; i >= 0; i--) {
2878 if ((parent->attr & (fr_attr_mask[i] << fr_attr_shift[i]))) {
2879 dattr.attr |= (attr & fr_attr_mask[i + 1]) << fr_attr_shift[i + 1];
2889 fprintf(stderr, "LOOKING FOR %08x %08x + %08x %08x --> %08x %08x\n",
2890 parent->vendor, parent->attr, attr, vendor,
2891 dattr.vendor, dattr.attr);
2894 *pattr = dattr.attr;
2895 *pvendor = dattr.vendor;
2900 * Get an attribute by it's numerical value, and the parent
2902 const DICT_ATTR *dict_attrbyparent(const DICT_ATTR *parent,
2903 unsigned int attr, unsigned int vendor)
2905 unsigned int my_attr, my_vendor;
2911 if (!dict_attr_child(parent, &my_attr, &my_vendor)) return NULL;
2913 dattr.attr = my_attr;
2914 dattr.vendor = my_vendor;
2916 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2921 * Get an attribute by its name.
2923 const DICT_ATTR *dict_attrbyname(const char *name)
2926 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2928 if (!name) return NULL;
2930 da = (DICT_ATTR *) buffer;
2931 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2933 return fr_hash_table_finddata(attributes_byname, da);
2937 * Associate a value with an attribute and return it.
2939 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2941 DICT_VALUE dval, *dv;
2944 * First, look up aliases.
2947 dval.vendor = vendor;
2948 dval.name[0] = '\0';
2951 * Look up the attribute alias target, and use
2952 * the correct attribute number if found.
2954 dv = fr_hash_table_finddata(values_byname, &dval);
2955 if (dv) dval.attr = dv->value;
2959 return fr_hash_table_finddata(values_byvalue, &dval);
2963 * Associate a value with an attribute and return it.
2965 const char *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value)
2969 dv = dict_valbyattr(attr, vendor, value);
2976 * Get a value by its name, keyed off of an attribute.
2978 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2980 DICT_VALUE *my_dv, *dv;
2981 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2983 if (!name) return NULL;
2985 my_dv = (DICT_VALUE *) buffer;
2987 my_dv->vendor = vendor;
2988 my_dv->name[0] = '\0';
2991 * Look up the attribute alias target, and use
2992 * the correct attribute number if found.
2994 dv = fr_hash_table_finddata(values_byname, my_dv);
2995 if (dv) my_dv->attr = dv->value;
2997 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2999 return fr_hash_table_finddata(values_byname, my_dv);
3003 * Get the vendor PEC based on the vendor name
3005 * This is efficient only for small numbers of vendors.
3007 int dict_vendorbyname(const char *name)
3010 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
3012 if (!name) return 0;
3014 dv = (DICT_VENDOR *) buffer;
3015 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
3017 dv = fr_hash_table_finddata(vendors_byname, dv);
3020 return dv->vendorpec;
3024 * Return the vendor struct based on the PEC.
3026 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
3030 dv.vendorpec = vendorpec;
3032 return fr_hash_table_finddata(vendors_byvalue, &dv);