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>
34 #ifdef HAVE_SYS_STAT_H
38 #define DICT_VALUE_MAX_NAME_LEN (128)
39 #define DICT_VENDOR_MAX_NAME_LEN (128)
40 #define DICT_ATTR_MAX_NAME_LEN (128)
42 static fr_hash_table_t *vendors_byname = NULL;
43 static fr_hash_table_t *vendors_byvalue = NULL;
45 static fr_hash_table_t *attributes_byname = NULL;
46 static fr_hash_table_t *attributes_byvalue = NULL;
48 static fr_hash_table_t *values_byvalue = NULL;
49 static fr_hash_table_t *values_byname = NULL;
51 static DICT_ATTR *dict_base_attrs[256];
54 * For faster HUP's, we cache the stat information for
55 * files we've $INCLUDEd
57 typedef struct dict_stat_t {
58 struct dict_stat_t *next;
63 static char *stat_root_dir = NULL;
64 static char *stat_root_file = NULL;
66 static dict_stat_t *stat_head = NULL;
67 static dict_stat_t *stat_tail = NULL;
69 typedef struct value_fixup_t {
70 char attrstr[DICT_ATTR_MAX_NAME_LEN];
72 struct value_fixup_t *next;
77 * So VALUEs in the dictionary can have forward references.
79 static value_fixup_t *value_fixup = NULL;
81 static const FR_NAME_NUMBER type_table[] = {
82 { "integer", PW_TYPE_INTEGER },
83 { "string", PW_TYPE_STRING },
84 { "ipaddr", PW_TYPE_IPADDR },
85 { "date", PW_TYPE_DATE },
86 { "abinary", PW_TYPE_ABINARY },
87 { "octets", PW_TYPE_OCTETS },
88 { "ifid", PW_TYPE_IFID },
89 { "ipv6addr", PW_TYPE_IPV6ADDR },
90 { "ipv6prefix", PW_TYPE_IPV6PREFIX },
91 { "byte", PW_TYPE_BYTE },
92 { "short", PW_TYPE_SHORT },
93 { "ether", PW_TYPE_ETHERNET },
94 { "combo-ip", PW_TYPE_COMBO_IP },
95 { "tlv", PW_TYPE_TLV },
96 { "signed", PW_TYPE_SIGNED },
97 { "extended", PW_TYPE_EXTENDED },
98 { "extended-flags", PW_TYPE_EXTENDED_FLAGS },
99 { "evs", PW_TYPE_EVS },
107 #define MAX_TLV_NEST (4)
111 * 8 bits for nested TLV 1
112 * 8 bits for nested TLV 2
113 * 5 bits for nested TLV 3
114 * 3 bits for nested TLV 4
116 const int fr_attr_max_tlv = MAX_TLV_NEST;
117 const int fr_attr_shift[MAX_TLV_NEST + 1] = {
121 const int fr_attr_mask[MAX_TLV_NEST + 1] = {
122 0xff, 0xff, 0xff, 0x1f, 0x07
127 * Create the hash of the name.
129 * We copy the hash function here because it's substantially faster.
131 #define FNV_MAGIC_INIT (0x811c9dc5)
132 #define FNV_MAGIC_PRIME (0x01000193)
134 static uint32_t dict_hashname(const char *name)
136 uint32_t hash = FNV_MAGIC_INIT;
139 for (p = name; *p != '\0'; p++) {
140 int c = *(const unsigned char *) p;
141 if (isalpha(c)) c = tolower(c);
143 hash *= FNV_MAGIC_PRIME;
144 hash ^= (uint32_t ) (c & 0xff);
152 * Hash callback functions.
154 static uint32_t dict_attr_name_hash(const void *data)
156 return dict_hashname(((const DICT_ATTR *)data)->name);
159 static int dict_attr_name_cmp(const void *one, const void *two)
161 const DICT_ATTR *a = one;
162 const DICT_ATTR *b = two;
164 return strcasecmp(a->name, b->name);
167 static uint32_t dict_attr_value_hash(const void *data)
170 const DICT_ATTR *attr = data;
172 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
173 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
176 static int dict_attr_value_cmp(const void *one, const void *two)
178 const DICT_ATTR *a = one;
179 const DICT_ATTR *b = two;
181 if (a->vendor < b->vendor) return -1;
182 if (a->vendor > b->vendor) return +1;
184 return a->attr - b->attr;
187 static uint32_t dict_vendor_name_hash(const void *data)
189 return dict_hashname(((const DICT_VENDOR *)data)->name);
192 static int dict_vendor_name_cmp(const void *one, const void *two)
194 const DICT_VENDOR *a = one;
195 const DICT_VENDOR *b = two;
197 return strcasecmp(a->name, b->name);
200 static uint32_t dict_vendor_value_hash(const void *data)
202 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
203 sizeof(((const DICT_VENDOR *)data)->vendorpec));
206 static int dict_vendor_value_cmp(const void *one, const void *two)
208 const DICT_VENDOR *a = one;
209 const DICT_VENDOR *b = two;
211 return a->vendorpec - b->vendorpec;
214 static uint32_t dict_value_name_hash(const void *data)
217 const DICT_VALUE *dval = data;
219 hash = dict_hashname(dval->name);
220 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
221 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
224 static int dict_value_name_cmp(const void *one, const void *two)
227 const DICT_VALUE *a = one;
228 const DICT_VALUE *b = two;
230 rcode = a->attr - b->attr;
231 if (rcode != 0) return rcode;
233 rcode = a->vendor - b->vendor;
234 if (rcode != 0) return rcode;
236 return strcasecmp(a->name, b->name);
239 static uint32_t dict_value_value_hash(const void *data)
242 const DICT_VALUE *dval = data;
244 hash = fr_hash(&dval->attr, sizeof(dval->attr));
245 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
246 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
249 static int dict_value_value_cmp(const void *one, const void *two)
252 const DICT_VALUE *a = one;
253 const DICT_VALUE *b = two;
255 if (a->vendor < b->vendor) return -1;
256 if (a->vendor > b->vendor) return +1;
258 rcode = a->attr - b->attr;
259 if (rcode != 0) return rcode;
261 return a->value - b->value;
266 * Free the list of stat buffers
268 static void dict_stat_free(void)
270 dict_stat_t *this, *next;
273 stat_root_dir = NULL;
274 free(stat_root_file);
275 stat_root_file = NULL;
282 for (this = stat_head; this != NULL; this = next) {
288 stat_head = stat_tail = NULL;
293 * Add an entry to the list of stat buffers.
295 static void dict_stat_add(const char *name, const struct stat *stat_buf)
299 this = malloc(sizeof(*this));
301 memset(this, 0, sizeof(*this));
303 this->name = strdup(name);
304 this->mtime = stat_buf->st_mtime;
307 stat_head = stat_tail = this;
309 stat_tail->next = this;
316 * See if any dictionaries have changed. If not, don't
319 static int dict_stat_check(const char *root_dir, const char *root_file)
324 if (!stat_root_dir) return 0;
325 if (!stat_root_file) return 0;
327 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
328 if (strcmp(root_file, stat_root_file) != 0) return 0;
330 if (!stat_head) return 0; /* changed, reload */
332 for (this = stat_head; this != NULL; this = this->next) {
333 if (stat(this->name, &buf) < 0) return 0;
335 if (buf.st_mtime != this->mtime) return 0;
341 typedef struct fr_pool_t {
344 struct fr_pool_t *page_free;
345 struct fr_pool_t *page_next;
348 #define FR_POOL_SIZE (32768)
349 #define FR_ALLOC_ALIGN (8)
351 static fr_pool_t *dict_pool = NULL;
353 static fr_pool_t *fr_pool_create(void)
355 fr_pool_t *fp = malloc(FR_POOL_SIZE);
357 if (!fp) return NULL;
359 memset(fp, 0, FR_POOL_SIZE);
361 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
362 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
364 fp->page_next = NULL;
368 static void fr_pool_delete(fr_pool_t **pfp)
370 fr_pool_t *fp, *next;
372 if (!pfp || !*pfp) return;
374 for (fp = *pfp; fp != NULL; fp = next) {
375 next = fp->page_next;
376 fp->page_next = NULL;
383 static void *fr_pool_alloc(size_t size)
387 if (size == 0) return NULL;
389 if (size > 256) return NULL; /* shouldn't happen */
392 dict_pool = fr_pool_create();
393 if (!dict_pool) return NULL;
396 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
397 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
400 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
401 dict_pool->page_free->page_next = fr_pool_create();
402 if (!dict_pool->page_free->page_next) return NULL;
403 dict_pool->page_free = dict_pool->page_free->page_next;
406 ptr = dict_pool->page_free->free_ptr;
407 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
413 static void fr_pool_free(UNUSED void *ptr)
416 * Place-holder for later code.
421 * Free the dictionary_attributes and dictionary_values lists.
428 fr_hash_table_free(vendors_byname);
429 fr_hash_table_free(vendors_byvalue);
430 vendors_byname = NULL;
431 vendors_byvalue = NULL;
433 fr_hash_table_free(attributes_byname);
434 fr_hash_table_free(attributes_byvalue);
435 attributes_byname = NULL;
436 attributes_byvalue = NULL;
438 fr_hash_table_free(values_byname);
439 fr_hash_table_free(values_byvalue);
440 values_byname = NULL;
441 values_byvalue = NULL;
443 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
445 fr_pool_delete(&dict_pool);
452 * Add vendor to the list.
454 int dict_addvendor(const char *name, unsigned int value)
459 if (value > FR_MAX_VENDOR) {
460 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
464 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
465 fr_strerror_printf("dict_addvendor: vendor name too long");
469 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
470 fr_strerror_printf("dict_addvendor: out of memory");
474 strcpy(dv->name, name);
475 dv->vendorpec = value;
476 dv->type = dv->length = 1; /* defaults */
478 if (!fr_hash_table_insert(vendors_byname, dv)) {
481 old_dv = fr_hash_table_finddata(vendors_byname, dv);
483 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
486 if (old_dv->vendorpec != dv->vendorpec) {
487 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
492 * Already inserted. Discard the duplicate entry.
499 * Insert the SAME pointer (not free'd when this table is
500 * deleted), into another table.
502 * We want this behaviour because we want OLD names for
503 * the attributes to be read from the configuration
504 * files, but when we're printing them, (and looking up
505 * by value) we want to use the NEW name.
507 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
508 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
517 * Add an attribute to the dictionary.
519 int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
523 static int max_attr = 0;
526 namelen = strlen(name);
527 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
528 fr_strerror_printf("dict_addattr: attribute name too long");
533 * If the attr is '-1', that means use a pre-existing
534 * one (if it already exists). If one does NOT already exist,
535 * then create a new attribute, with a non-conflicting value,
539 if (dict_attrbyname(name)) {
540 return 0; /* exists, don't add it again */
545 } else if (vendor == 0) {
549 if (attr > max_attr) {
555 * Additional checks for extended attributes.
557 if (flags.extended || flags.extended_flags || flags.evs) {
559 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
562 vendor = VENDORPEC_EXTENDED;
568 || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
569 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
575 if (!(flags.extended || flags.extended_flags)) {
576 fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
580 if (vendor <= FR_MAX_VENDOR) {
581 fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
587 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
591 if (flags.has_tlv && flags.length) {
592 fr_strerror_printf("TLVs cannot have a fixed length");
596 if (vendor && (vendor != VENDORPEC_EXTENDED)) {
598 static DICT_VENDOR *last_vendor = NULL;
600 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
601 fr_strerror_printf("TLV's cannot be encrypted");
605 if (flags.is_tlv && flags.has_tag) {
606 fr_strerror_printf("Sub-TLV's cannot have a tag");
610 if (flags.has_tlv && flags.has_tag) {
611 fr_strerror_printf("TLV's cannot have a tag");
616 * Most ATTRIBUTEs are bunched together by
617 * VENDOR. We can save a lot of lookups on
618 * dictionary initialization by caching the last
622 ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
626 * Ignore the high byte (sigh)
628 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
633 * If the vendor isn't defined, die.
636 fr_strerror_printf("dict_addattr: Unknown vendor %u",
642 * FIXME: Switch over dv->type, and limit things
645 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
646 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
648 } /* else 256..65535 are allowed */
651 * Set the extended flags as appropriate.
653 if (vendor > FR_MAX_VENDOR) {
656 myattr = (vendor >> 24) & 0xff;
657 myattr |= PW_VENDOR_SPECIFIC << 8;
659 da = dict_attrbyvalue(myattr, VENDORPEC_EXTENDED);
661 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown \"evs\" type.");
664 flags.extended = da->flags.extended;
665 flags.extended_flags = da->flags.extended_flags;
666 flags.evs = da->flags.evs;
671 * Create a new attribute for the list
673 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
674 fr_strerror_printf("dict_addattr: out of memory");
678 memcpy(da->name, name, namelen);
679 da->name[namelen] = '\0';
686 * Insert the attribute, only if it's not a duplicate.
688 if (!fr_hash_table_insert(attributes_byname, da)) {
692 * If the attribute has identical number, then
693 * ignore the duplicate.
695 a = fr_hash_table_finddata(attributes_byname, da);
696 if (a && (strcasecmp(a->name, da->name) == 0)) {
697 if (a->attr != da->attr) {
698 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
704 * Same name, same vendor, same attr,
705 * maybe the flags and/or type is
706 * different. Let the new value
707 * over-ride the old one.
712 fr_hash_table_delete(attributes_byvalue, a);
714 if (!fr_hash_table_replace(attributes_byname, da)) {
715 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
722 * Insert the SAME pointer (not free'd when this entry is
723 * deleted), into another table.
725 * We want this behaviour because we want OLD names for
726 * the attributes to be read from the configuration
727 * files, but when we're printing them, (and looking up
728 * by value) we want to use the NEW name.
730 if (!fr_hash_table_replace(attributes_byvalue, da)) {
731 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
735 if (!vendor && (attr > 0) && (attr < 256)) {
736 dict_base_attrs[attr] = da;
744 * Add a value for an attribute to the dictionary.
746 int dict_addvalue(const char *namestr, const char *attrstr, int value)
752 static DICT_ATTR *last_attr = NULL;
755 fr_strerror_printf("dict_addvalue: empty names are not permitted");
759 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
760 fr_strerror_printf("dict_addvalue: value name too long");
764 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
765 fr_strerror_printf("dict_addvalue: out of memory");
768 memset(dval, 0, sizeof(*dval));
770 strcpy(dval->name, namestr);
774 * Most VALUEs are bunched together by ATTRIBUTE. We can
775 * save a lot of lookups on dictionary initialization by
776 * caching the last attribute.
778 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
781 dattr = dict_attrbyname(attrstr);
786 * Remember which attribute is associated with this
787 * value, if possible.
790 if (dattr->flags.has_value_alias) {
791 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
795 dval->attr = dattr->attr;
796 dval->vendor = dattr->vendor;
799 * Enforce valid values
801 * Don't worry about fixups...
803 switch (dattr->type) {
807 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
814 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
820 * Allow octets for now, because
821 * of dictionary.cablelabs
825 case PW_TYPE_INTEGER:
830 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
831 fr_int2str(type_table, dattr->type, "?Unknown?"));
835 dattr->flags.has_value = 1;
837 value_fixup_t *fixup;
839 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
842 fr_strerror_printf("dict_addvalue: out of memory");
845 memset(fixup, 0, sizeof(*fixup));
847 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
851 * Insert to the head of the list.
853 fixup->next = value_fixup;
860 * Add the value into the dictionary.
862 if (!fr_hash_table_insert(values_byname, dval)) {
867 * Suppress duplicates with the same
868 * name and value. There are lots in
871 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
872 if (old && (old->value == dval->value)) {
879 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
884 * There are multiple VALUE's, keyed by attribute, so we
885 * take care of that here.
887 if (!fr_hash_table_replace(values_byvalue, dval)) {
888 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
896 static int sscanf_i(const char *str, unsigned int *pvalue)
900 static const char *tab = "0123456789";
902 if ((str[0] == '0') &&
903 ((str[1] == 'x') || (str[1] == 'X'))) {
904 tab = "0123456789abcdef";
913 if (*str == '.') break;
915 c = memchr(tab, tolower((int) *str), base);
928 int dict_str2oid(const char *ptr, unsigned int *pvalue, int vendor, int tlv_depth)
934 if (tlv_depth > fr_attr_max_tlv) {
935 fr_strerror_printf("Attribute has too long OID");
941 da = dict_attrbyvalue(*pvalue, vendor);
943 da = dict_attrbyvalue(*pvalue, VENDORPEC_EXTENDED);
946 fr_strerror_printf("No such attribute");
950 if (!(da->flags.has_tlv || da->flags.extended || da->flags.extended_flags)) {
951 fr_strerror_printf("Parent attribute %s cannot have sub-tlvs",
957 p = strchr(ptr, '.');
959 if (!sscanf_i(ptr, &value)) {
960 fr_strerror_printf("Failed parsing attribute identifier %s",
966 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
972 return dict_str2oid(p + 1, pvalue, vendor, tlv_depth + 1);
980 * Process the ATTRIBUTE command
982 static int process_attribute(const char* fn, const int line,
983 const unsigned int block_vendor, DICT_ATTR *block_tlv,
984 int tlv_depth, char **argv, int argc)
986 unsigned int vendor = 0;
989 unsigned int length = 0;
993 if ((argc < 3) || (argc > 4)) {
994 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
999 if (strncmp(argv[1], "Attr-", 5) == 0) {
1000 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1005 memset(&flags, 0, sizeof(flags));
1008 * Look for extended attributes before doing anything else.
1010 p = strchr(argv[1], '.');
1014 * Validate all entries
1016 if (!sscanf_i(argv[1], &value)) {
1017 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1022 if (value > (1 << 24)) {
1023 fr_strerror_printf("dict_init: %s[%d]: Attribute number is too large", fn, line);
1029 * Parse NUM.NUM.NUM.NUM
1034 *p = '.'; /* reset for later printing */
1037 da = dict_attrbyvalue(value, block_vendor);
1039 da = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
1042 fr_strerror_printf("dict_init: %s[%d]: Entry refers to unknown attribute %d", fn, line, value);
1047 * 241.1 means 241 is of type "extended".
1050 if (!(da->flags.has_tlv || da->flags.extended || da->flags.extended_flags)) {
1051 fr_strerror_printf("dict_init: %s[%d]: Parent attribute %s cannot contain sub-attributes", fn, line, da->name);
1055 if (!dict_str2oid(p + 1, &value, block_vendor, tlv_depth + 1)) {
1058 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1060 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1065 * Set which type of attribute this is.
1067 flags.extended = da->flags.extended;
1068 flags.extended_flags = da->flags.extended_flags;
1069 flags.evs = da->flags.evs;
1070 if (da->flags.has_tlv) flags.is_tlv = 1;
1073 if (strncmp(argv[2], "octets[", 7) != 0) {
1075 * find the type of the attribute.
1077 type = fr_str2int(type_table, argv[2], -1);
1079 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1085 type = PW_TYPE_OCTETS;
1087 p = strchr(argv[2] + 7, ']');
1089 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1095 if (!sscanf_i(argv[1], &length)) {
1096 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1100 if ((length == 0) || (length > 253)) {
1101 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1107 * Only look up the vendor if the string
1112 * Force "length" for data types of fixed length;
1124 case PW_TYPE_IPADDR:
1125 case PW_TYPE_INTEGER:
1126 case PW_TYPE_SIGNED:
1130 case PW_TYPE_ETHERNET:
1138 case PW_TYPE_IPV6ADDR:
1142 case PW_TYPE_EXTENDED:
1143 type = PW_TYPE_OCTETS;
1147 case PW_TYPE_EXTENDED_FLAGS:
1148 type = PW_TYPE_OCTETS;
1149 flags.extended_flags = 1;
1153 type = PW_TYPE_OCTETS;
1155 if (((value >> fr_attr_shift[1]) & fr_attr_mask[1]) != PW_VENDOR_SPECIFIC) {
1156 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1165 flags.length = length;
1167 } else { /* argc == 4: we have options */
1168 char *key, *next, *last;
1173 if (flags.extended || flags.extended_flags || flags.evs) {
1174 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1179 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1185 next = strchr(key, ',');
1186 if (next) *(next++) = '\0';
1188 if (strcmp(key, "has_tag") == 0 ||
1189 strcmp(key, "has_tag=1") == 0) {
1190 /* Boolean flag, means this is a
1194 } else if (strncmp(key, "encrypt=", 8) == 0) {
1195 /* Encryption method, defaults to 0 (none).
1196 Currently valid is just type 2,
1197 Tunnel-Password style, which can only
1198 be applied to strings. */
1199 flags.encrypt = strtol(key + 8, &last, 0);
1201 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1206 } else if (strncmp(key, "array", 6) == 0) {
1210 case PW_TYPE_IPADDR:
1213 case PW_TYPE_INTEGER:
1218 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1224 * The only thing is the vendor name,
1225 * and it's a known name: allow it.
1227 } else if ((key == argv[3]) && !next && !block_vendor &&
1228 ((vendor = dict_vendorbyname(key)) !=0)) {
1232 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1238 if (key && !*key) break;
1242 if (block_vendor) vendor = block_vendor;
1245 * Special checks for tags, they make our life much more
1248 if (flags.has_tag) {
1250 * Only string, octets, and integer can be tagged.
1253 case PW_TYPE_STRING:
1254 case PW_TYPE_INTEGER:
1258 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1260 fr_int2str(type_table, type, "?Unknown?"));
1265 if (type == PW_TYPE_TLV) {
1268 && (vendor != DHCP_MAGIC_VENDOR)
1273 dv = dict_vendorbyvalue(vendor);
1274 if (!dv || (dv->type != 1) || (dv->length != 1)) {
1275 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1286 * TLV's can be only one octet.
1288 if ((value <= 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1289 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1297 value <<= fr_attr_shift[tlv_depth];
1298 value |= block_tlv->attr;
1302 #ifdef WITH_DICTIONARY_WARNINGS
1304 * Hack to help us discover which vendors have illegal
1307 if (!vendor && (value < 256) &&
1308 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1309 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1317 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1320 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1322 fr_strerror_printf("dict_init: %s[%d]: %s",
1332 * Process the VALUE command
1334 static int process_value(const char* fn, const int line, char **argv,
1340 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1345 * For Compatibility, skip "Server-Config"
1347 if (strcasecmp(argv[0], "Server-Config") == 0)
1351 * Validate all entries
1353 if (!sscanf_i(argv[2], &value)) {
1354 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1359 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1362 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1364 fr_strerror_printf("dict_init: %s[%d]: %s",
1374 * Process the VALUE-ALIAS command
1376 * This allows VALUE mappings to be shared among multiple
1379 static int process_value_alias(const char* fn, const int line, char **argv,
1382 DICT_ATTR *my_da, *da;
1386 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1391 my_da = dict_attrbyname(argv[0]);
1393 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1398 if (my_da->flags.has_value) {
1399 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1404 if (my_da->flags.has_value_alias) {
1405 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1410 da = dict_attrbyname(argv[1]);
1412 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1417 if (!da->flags.has_value) {
1418 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1423 if (da->flags.has_value_alias) {
1424 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1429 if (my_da->type != da->type) {
1430 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1435 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1436 fr_strerror_printf("dict_addvalue: out of memory");
1440 dval->name[0] = '\0'; /* empty name */
1441 dval->attr = my_da->attr;
1442 dval->vendor = my_da->vendor;
1443 dval->value = da->attr;
1445 if (!fr_hash_table_insert(values_byname, dval)) {
1446 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1457 * Process the VENDOR command
1459 static int process_vendor(const char* fn, const int line, char **argv,
1463 int continuation = 0;
1464 const char *format = NULL;
1466 if ((argc < 2) || (argc > 3)) {
1467 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1473 * Validate all entries
1475 if (!isdigit((int) argv[1][0])) {
1476 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1480 value = atoi(argv[1]);
1482 /* Create a new VENDOR entry for the list */
1483 if (dict_addvendor(argv[0], value) < 0) {
1486 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1488 fr_strerror_printf("dict_init: %s[%d]: %s",
1494 * Look for a format statement
1499 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1500 format = "format=4,0";
1502 } else if (value == VENDORPEC_LUCENT) {
1503 format = "format=2,1";
1505 } else if (value == VENDORPEC_STARENT) {
1506 format = "format=2,2";
1508 } /* else no fixups to do */
1515 if (strncasecmp(format, "format=", 7) != 0) {
1516 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1522 if ((strlen(p) < 3) ||
1523 !isdigit((int) p[0]) ||
1525 !isdigit((int) p[2]) ||
1526 (p[3] && (p[3] != ','))) {
1527 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1532 type = (int) (p[0] - '0');
1533 length = (int) (p[2] - '0');
1536 if ((p[4] != 'c') ||
1538 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1544 if ((value != VENDORPEC_WIMAX) ||
1545 (type != 1) || (length != 1)) {
1546 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1552 dv = dict_vendorbyvalue(value);
1554 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1559 if ((type != 1) && (type != 2) && (type != 4)) {
1560 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1565 if ((length != 0) && (length != 1) && (length != 2)) {
1566 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1572 dv->length = length;
1573 dv->flags = continuation;
1580 * String split routine. Splits an input string IN PLACE
1581 * into pieces, based on spaces.
1583 static int str2argv(char *str, char **argv, int max_argc)
1588 if (argc >= max_argc) break;
1591 * Chop out comments early.
1598 while ((*str == ' ') ||
1601 (*str == '\n')) *(str++) = '\0';
1612 (*str != '\n')) str++;
1618 #define MAX_ARGV (16)
1621 * Initialize the dictionary.
1623 static int my_dict_init(const char *dir, const char *fn,
1624 const char *src_file, int src_line)
1631 unsigned int vendor;
1632 unsigned int block_vendor;
1633 struct stat statbuf;
1634 char *argv[MAX_ARGV];
1636 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1637 int which_block_tlv = 0;
1639 block_tlv[0] = NULL;
1640 block_tlv[1] = NULL;
1641 block_tlv[2] = NULL;
1643 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1644 strlen(dir) >= sizeof(dirtmp) / 2) {
1645 fr_strerror_printf("dict_init: filename name too long");
1650 * First see if fn is relative to dir. If so, create
1651 * new filename. If not, remember the absolute dir.
1653 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1657 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1658 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1662 if ((fp = fopen(fn, "r")) == NULL) {
1664 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1665 fn, strerror(errno));
1667 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1668 src_file, src_line, fn, strerror(errno));
1673 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1674 if (!S_ISREG(statbuf.st_mode)) {
1676 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1682 * Globally writable dictionaries means that users can control
1683 * the server configuration with little difficulty.
1686 if ((statbuf.st_mode & S_IWOTH) != 0) {
1688 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1694 dict_stat_add(fn, &statbuf);
1697 * Seed the random pool with data.
1699 fr_rand_seed(&statbuf, sizeof(statbuf));
1703 while (fgets(buf, sizeof(buf), fp) != NULL) {
1705 if (buf[0] == '#' || buf[0] == 0 ||
1706 buf[0] == '\n' || buf[0] == '\r')
1710 * Comment characters should NOT be appearing anywhere but
1711 * as start of a comment;
1713 p = strchr(buf, '#');
1716 argc = str2argv(buf, argv, MAX_ARGV);
1717 if (argc == 0) continue;
1720 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1727 * Process VALUE lines.
1729 if (strcasecmp(argv[0], "VALUE") == 0) {
1730 if (process_value(fn, line,
1731 argv + 1, argc - 1) == -1) {
1739 * Perhaps this is an attribute.
1741 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1742 if (process_attribute(fn, line, block_vendor,
1743 block_tlv[which_block_tlv],
1745 argv + 1, argc - 1) == -1) {
1753 * See if we need to import another dictionary.
1755 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1756 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1763 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1764 if (process_value_alias(fn, line,
1765 argv + 1, argc - 1) == -1) {
1773 * Process VENDOR lines.
1775 if (strcasecmp(argv[0], "VENDOR") == 0) {
1776 if (process_vendor(fn, line,
1777 argv + 1, argc - 1) == -1) {
1784 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1787 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1793 da = dict_attrbyname(argv[1]);
1796 "dict_init: %s[%d]: unknown attribute %s",
1802 if (da->type != PW_TYPE_TLV) {
1804 "dict_init: %s[%d]: attribute %s is not of type tlv",
1810 if (which_block_tlv >= MAX_TLV_NEST) {
1812 "dict_init: %s[%d]: TLVs are nested too deep",
1819 block_tlv[++which_block_tlv] = da;
1823 if (strcasecmp(argv[0], "END-TLV") == 0) {
1826 "dict_init: %s[%d] invalid END-TLV entry",
1832 da = dict_attrbyname(argv[1]);
1835 "dict_init: %s[%d]: unknown attribute %s",
1841 if (da != block_tlv[which_block_tlv]) {
1843 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1848 block_tlv[which_block_tlv--] = NULL;
1852 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1855 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1861 vendor = dict_vendorbyname(argv[1]);
1864 "dict_init: %s[%d]: unknown vendor %s",
1870 block_vendor = vendor;
1873 * Check for extended attr VSAs
1876 if (strncmp(argv[2], "format=", 7) != 0) {
1878 "dict_init: %s[%d]: Invalid format %s",
1885 da = dict_attrbyname(p);
1887 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
1893 if (!da->flags.evs) {
1894 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
1901 * Pack the encapsulating attribute
1902 * into the vendor Id.
1904 block_vendor |= (da->attr & fr_attr_mask[1]) * FR_MAX_VENDOR;
1908 } /* BEGIN-VENDOR */
1910 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1913 "dict_init: %s[%d] invalid END-VENDOR entry",
1919 vendor = dict_vendorbyname(argv[1]);
1922 "dict_init: %s[%d]: unknown vendor %s",
1928 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
1930 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1940 * Any other string: We don't recognize it.
1942 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1953 * Empty callback for hash table initialization.
1955 static int null_callback(void *ctx, void *data)
1957 ctx = ctx; /* -Wunused */
1958 data = data; /* -Wunused */
1965 * Initialize the directory, then fix the attr member of
1968 int dict_init(const char *dir, const char *fn)
1971 * Check if we need to change anything. If not, don't do
1974 if (dict_stat_check(dir, fn)) {
1979 * Free the dictionaries, and the stat cache.
1982 stat_root_dir = strdup(dir);
1983 stat_root_file = strdup(fn);
1986 * Create the table of vendor by name. There MAY NOT
1987 * be multiple vendors of the same name.
1989 * Each vendor is malloc'd, so the free function is free.
1991 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1992 dict_vendor_name_cmp,
1994 if (!vendors_byname) {
1999 * Create the table of vendors by value. There MAY
2000 * be vendors of the same value. If there are, we
2001 * pick the latest one.
2003 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2004 dict_vendor_value_cmp,
2006 if (!vendors_byvalue) {
2011 * Create the table of attributes by name. There MAY NOT
2012 * be multiple attributes of the same name.
2014 * Each attribute is malloc'd, so the free function is free.
2016 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2019 if (!attributes_byname) {
2024 * Create the table of attributes by value. There MAY
2025 * be attributes of the same value. If there are, we
2026 * pick the latest one.
2028 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2029 dict_attr_value_cmp,
2031 if (!attributes_byvalue) {
2035 values_byname = fr_hash_table_create(dict_value_name_hash,
2036 dict_value_name_cmp,
2038 if (!values_byname) {
2042 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2043 dict_value_value_cmp,
2045 if (!values_byvalue) {
2049 value_fixup = NULL; /* just to be safe. */
2051 if (my_dict_init(dir, fn, NULL, 0) < 0)
2056 value_fixup_t *this, *next;
2058 for (this = value_fixup; this != NULL; this = next) {
2061 a = dict_attrbyname(this->attrstr);
2064 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2065 this->attrstr, this->dval->name);
2066 return -1; /* leak, but they should die... */
2069 this->dval->attr = a->attr;
2072 * Add the value into the dictionary.
2074 if (!fr_hash_table_replace(values_byname,
2076 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2081 * Allow them to use the old name, but
2082 * prefer the new name when printing
2085 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2086 fr_hash_table_replace(values_byvalue,
2092 * Just so we don't lose track of things.
2099 * Walk over all of the hash tables to ensure they're
2100 * initialized. We do this because the threads may perform
2101 * lookups, and we don't want multi-threaded re-ordering
2102 * of the table entries. That would be bad.
2104 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2105 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2107 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2108 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2110 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2111 fr_hash_table_walk(values_byname, null_callback, NULL);
2117 * Get an attribute by its numerical value.
2119 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2123 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2126 dattr.vendor = vendor;
2128 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2132 * Get an attribute by its name.
2134 DICT_ATTR *dict_attrbyname(const char *name)
2137 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2139 if (!name) return NULL;
2141 da = (DICT_ATTR *) buffer;
2142 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2144 return fr_hash_table_finddata(attributes_byname, da);
2148 * Associate a value with an attribute and return it.
2150 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2152 DICT_VALUE dval, *dv;
2155 * First, look up aliases.
2158 dval.vendor = vendor;
2159 dval.name[0] = '\0';
2162 * Look up the attribute alias target, and use
2163 * the correct attribute number if found.
2165 dv = fr_hash_table_finddata(values_byname, &dval);
2166 if (dv) dval.attr = dv->value;
2170 return fr_hash_table_finddata(values_byvalue, &dval);
2174 * Get a value by its name, keyed off of an attribute.
2176 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2178 DICT_VALUE *my_dv, *dv;
2179 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2181 if (!name) return NULL;
2183 my_dv = (DICT_VALUE *) buffer;
2185 my_dv->vendor = vendor;
2186 my_dv->name[0] = '\0';
2189 * Look up the attribute alias target, and use
2190 * the correct attribute number if found.
2192 dv = fr_hash_table_finddata(values_byname, my_dv);
2193 if (dv) my_dv->attr = dv->value;
2195 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2197 return fr_hash_table_finddata(values_byname, my_dv);
2201 * Get the vendor PEC based on the vendor name
2203 * This is efficient only for small numbers of vendors.
2205 int dict_vendorbyname(const char *name)
2208 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
2210 if (!name) return 0;
2212 dv = (DICT_VENDOR *) buffer;
2213 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
2215 dv = fr_hash_table_finddata(vendors_byname, dv);
2218 return dv->vendorpec;
2222 * Return the vendor struct based on the PEC.
2224 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
2228 dv.vendorpec = vendorpec;
2230 return fr_hash_table_finddata(vendors_byvalue, &dv);