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;
381 static void *fr_pool_alloc(size_t size)
385 if (size == 0) return NULL;
387 if (size > 256) return NULL; /* shouldn't happen */
390 dict_pool = fr_pool_create();
391 if (!dict_pool) return NULL;
394 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
395 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
398 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
399 dict_pool->page_free->page_next = fr_pool_create();
400 if (!dict_pool->page_free->page_next) return NULL;
401 dict_pool->page_free = dict_pool->page_free->page_next;
404 ptr = dict_pool->page_free->free_ptr;
405 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
411 static void fr_pool_free(UNUSED void *ptr)
414 * Place-holder for later code.
419 * Free the dictionary_attributes and dictionary_values lists.
426 fr_hash_table_free(vendors_byname);
427 fr_hash_table_free(vendors_byvalue);
428 vendors_byname = NULL;
429 vendors_byvalue = NULL;
431 fr_hash_table_free(attributes_byname);
432 fr_hash_table_free(attributes_byvalue);
433 attributes_byname = NULL;
434 attributes_byvalue = NULL;
436 fr_hash_table_free(values_byname);
437 fr_hash_table_free(values_byvalue);
438 values_byname = NULL;
439 values_byvalue = NULL;
441 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
443 fr_pool_delete(&dict_pool);
450 * Add vendor to the list.
452 int dict_addvendor(const char *name, unsigned int value)
457 if (value > FR_MAX_VENDOR) {
458 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
462 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
463 fr_strerror_printf("dict_addvendor: vendor name too long");
467 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
468 fr_strerror_printf("dict_addvendor: out of memory");
472 strcpy(dv->name, name);
473 dv->vendorpec = value;
474 dv->type = dv->length = 1; /* defaults */
476 if (!fr_hash_table_insert(vendors_byname, dv)) {
479 old_dv = fr_hash_table_finddata(vendors_byname, dv);
481 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
484 if (old_dv->vendorpec != dv->vendorpec) {
485 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
490 * Already inserted. Discard the duplicate entry.
497 * Insert the SAME pointer (not free'd when this table is
498 * deleted), into another table.
500 * We want this behaviour because we want OLD names for
501 * the attributes to be read from the configuration
502 * files, but when we're printing them, (and looking up
503 * by value) we want to use the NEW name.
505 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
506 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
515 * Add an attribute to the dictionary.
517 int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
521 static int max_attr = 0;
524 namelen = strlen(name);
525 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
526 fr_strerror_printf("dict_addattr: attribute name too long");
531 * If the attr is '-1', that means use a pre-existing
532 * one (if it already exists). If one does NOT already exist,
533 * then create a new attribute, with a non-conflicting value,
537 if (dict_attrbyname(name)) {
538 return 0; /* exists, don't add it again */
543 } else if (vendor == 0) {
547 if (attr > max_attr) {
553 * Additional checks for extended attributes.
555 if (flags.extended || flags.extended_flags || flags.evs) {
557 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
560 vendor = VENDORPEC_EXTENDED;
562 if (flags.has_tag || flags.array || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
563 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
569 if (!(flags.extended || flags.extended_flags)) {
570 fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
574 if (vendor <= FR_MAX_VENDOR) {
575 fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
581 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
585 if (flags.has_tlv && flags.length) {
586 fr_strerror_printf("TLVs cannot have a fixed length");
590 if (vendor && (vendor != VENDORPEC_EXTENDED)) {
592 static DICT_VENDOR *last_vendor = NULL;
594 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
595 fr_strerror_printf("TLV's cannot be encrypted");
599 if (flags.is_tlv && flags.has_tag) {
600 fr_strerror_printf("Sub-TLV's cannot have a tag");
604 if (flags.has_tlv && flags.has_tag) {
605 fr_strerror_printf("TLV's cannot have a tag");
610 * Most ATTRIBUTEs are bunched together by
611 * VENDOR. We can save a lot of lookups on
612 * dictionary initialization by caching the last
616 ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
620 * Ignore the high byte (sigh)
622 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
627 * If the vendor isn't defined, die.
630 fr_strerror_printf("dict_addattr: Unknown vendor %u",
636 * FIXME: Switch over dv->type, and limit things
639 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
640 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
642 } /* else 256..65535 are allowed */
645 * Set the extended flags as appropriate.
647 if (vendor > FR_MAX_VENDOR) {
650 myattr = (vendor >> 24) & 0xff;
651 myattr |= PW_VENDOR_SPECIFIC << 8;
653 da = dict_attrbyvalue(myattr, VENDORPEC_EXTENDED);
655 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown \"evs\" type.");
658 flags.extended = da->flags.extended;
659 flags.extended_flags = da->flags.extended_flags;
660 flags.evs = da->flags.evs;
665 * Create a new attribute for the list
667 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
668 fr_strerror_printf("dict_addattr: out of memory");
672 memcpy(da->name, name, namelen);
673 da->name[namelen] = '\0';
680 * Insert the attribute, only if it's not a duplicate.
682 if (!fr_hash_table_insert(attributes_byname, da)) {
686 * If the attribute has identical number, then
687 * ignore the duplicate.
689 a = fr_hash_table_finddata(attributes_byname, da);
690 if (a && (strcasecmp(a->name, da->name) == 0)) {
691 if (a->attr != da->attr) {
692 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
698 * Same name, same vendor, same attr,
699 * maybe the flags and/or type is
700 * different. Let the new value
701 * over-ride the old one.
706 fr_hash_table_delete(attributes_byvalue, a);
708 if (!fr_hash_table_replace(attributes_byname, da)) {
709 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
716 * Insert the SAME pointer (not free'd when this entry is
717 * deleted), into another table.
719 * We want this behaviour because we want OLD names for
720 * the attributes to be read from the configuration
721 * files, but when we're printing them, (and looking up
722 * by value) we want to use the NEW name.
724 if (!fr_hash_table_replace(attributes_byvalue, da)) {
725 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
729 if (!vendor && (attr > 0) && (attr < 256)) {
730 dict_base_attrs[attr] = da;
738 * Add a value for an attribute to the dictionary.
740 int dict_addvalue(const char *namestr, const char *attrstr, int value)
746 static DICT_ATTR *last_attr = NULL;
749 fr_strerror_printf("dict_addvalue: empty names are not permitted");
753 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
754 fr_strerror_printf("dict_addvalue: value name too long");
758 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
759 fr_strerror_printf("dict_addvalue: out of memory");
762 memset(dval, 0, sizeof(*dval));
764 strcpy(dval->name, namestr);
768 * Most VALUEs are bunched together by ATTRIBUTE. We can
769 * save a lot of lookups on dictionary initialization by
770 * caching the last attribute.
772 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
775 dattr = dict_attrbyname(attrstr);
780 * Remember which attribute is associated with this
781 * value, if possible.
784 if (dattr->flags.has_value_alias) {
785 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
789 dval->attr = dattr->attr;
790 dval->vendor = dattr->vendor;
793 * Enforce valid values
795 * Don't worry about fixups...
797 switch (dattr->type) {
801 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
808 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
814 * Allow octets for now, because
815 * of dictionary.cablelabs
819 case PW_TYPE_INTEGER:
824 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
825 fr_int2str(type_table, dattr->type, "?Unknown?"));
829 dattr->flags.has_value = 1;
831 value_fixup_t *fixup;
833 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
836 fr_strerror_printf("dict_addvalue: out of memory");
839 memset(fixup, 0, sizeof(*fixup));
841 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
845 * Insert to the head of the list.
847 fixup->next = value_fixup;
854 * Add the value into the dictionary.
856 if (!fr_hash_table_insert(values_byname, dval)) {
861 * Suppress duplicates with the same
862 * name and value. There are lots in
865 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
866 if (old && (old->value == dval->value)) {
873 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
878 * There are multiple VALUE's, keyed by attribute, so we
879 * take care of that here.
881 if (!fr_hash_table_replace(values_byvalue, dval)) {
882 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
890 static int sscanf_i(const char *str, unsigned int *pvalue)
894 static const char *tab = "0123456789";
896 if ((str[0] == '0') &&
897 ((str[1] == 'x') || (str[1] == 'X'))) {
898 tab = "0123456789abcdef";
907 if (*str == '.') break;
909 c = memchr(tab, tolower((int) *str), base);
922 int dict_str2oid(const char *ptr, unsigned int *pvalue, int vendor, int tlv_depth)
928 if (tlv_depth > fr_attr_max_tlv) {
929 fr_strerror_printf("Attribute has too long OID");
935 da = dict_attrbyvalue(*pvalue, vendor);
937 da = dict_attrbyvalue(*pvalue, VENDORPEC_EXTENDED);
940 fr_strerror_printf("No such attribute");
944 if (!(da->flags.has_tlv || da->flags.extended || da->flags.extended_flags)) {
945 fr_strerror_printf("Parent attribute %s cannot have sub-tlvs",
951 p = strchr(ptr, '.');
953 if (!sscanf_i(ptr, &value)) {
954 fr_strerror_printf("Failed parsing attribute identifier %s",
960 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
966 return dict_str2oid(p + 1, pvalue, vendor, tlv_depth + 1);
974 * Process the ATTRIBUTE command
976 static int process_attribute(const char* fn, const int line,
977 const unsigned int block_vendor, DICT_ATTR *block_tlv,
978 int tlv_depth, char **argv, int argc)
980 unsigned int vendor = 0;
983 unsigned int length = 0;
987 if ((argc < 3) || (argc > 4)) {
988 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
993 if (strncmp(argv[1], "Attr-", 5) == 0) {
994 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
999 memset(&flags, 0, sizeof(flags));
1002 * Look for extended attributes before doing anything else.
1004 p = strchr(argv[1], '.');
1008 * Validate all entries
1010 if (!sscanf_i(argv[1], &value)) {
1011 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1016 if (value > (1 <<24)) {
1017 fr_strerror_printf("dict_init: %s[%d]: Attribute number is too large", fn, line);
1023 * Parse NUM.NUM.NUM.NUM
1028 *p = '.'; /* reset for later printing */
1031 da = dict_attrbyvalue(value, block_vendor);
1033 da = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
1036 fr_strerror_printf("dict_init: %s[%d]: Entry refers to unknown attribute %d", fn, line, value);
1041 * 241.1 means 241 is of type "extended".
1044 if (!(da->flags.has_tlv || da->flags.extended || da->flags.extended_flags)) {
1045 fr_strerror_printf("dict_init: %s[%d]: Parent attribute %s cannot contain sub-attributes", fn, line, da->name);
1049 if (!dict_str2oid(p + 1, &value, block_vendor, tlv_depth + 1)) {
1052 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1054 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1059 * Set which type of attribute this is.
1061 flags.extended = da->flags.extended;
1062 flags.extended_flags = da->flags.extended_flags;
1063 flags.evs = da->flags.evs;
1064 if (da->flags.has_tlv) flags.is_tlv = 1;
1067 if (strncmp(argv[2], "octets[", 7) != 0) {
1069 * find the type of the attribute.
1071 type = fr_str2int(type_table, argv[2], -1);
1073 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1079 type = PW_TYPE_OCTETS;
1081 p = strchr(argv[2] + 7, ']');
1083 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1089 if (!sscanf_i(argv[1], &length)) {
1090 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1094 if ((length == 0) || (length > 253)) {
1095 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1101 * Only look up the vendor if the string
1106 * Force "length" for data types of fixed length;
1118 case PW_TYPE_IPADDR:
1119 case PW_TYPE_INTEGER:
1120 case PW_TYPE_SIGNED:
1124 case PW_TYPE_ETHERNET:
1132 case PW_TYPE_IPV6ADDR:
1136 case PW_TYPE_EXTENDED:
1137 type = PW_TYPE_OCTETS;
1141 case PW_TYPE_EXTENDED_FLAGS:
1142 type = PW_TYPE_OCTETS;
1143 flags.extended_flags = 1;
1147 type = PW_TYPE_OCTETS;
1149 if (((value >> fr_attr_shift[1]) & fr_attr_mask[1]) != PW_VENDOR_SPECIFIC) {
1150 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1159 flags.length = length;
1161 } else { /* argc == 4: we have options */
1162 char *key, *next, *last;
1167 if (flags.extended || flags.extended_flags || flags.evs) {
1168 fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1173 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1179 next = strchr(key, ',');
1180 if (next) *(next++) = '\0';
1182 if (strcmp(key, "has_tag") == 0 ||
1183 strcmp(key, "has_tag=1") == 0) {
1184 /* Boolean flag, means this is a
1188 } else if (strncmp(key, "encrypt=", 8) == 0) {
1189 /* Encryption method, defaults to 0 (none).
1190 Currently valid is just type 2,
1191 Tunnel-Password style, which can only
1192 be applied to strings. */
1193 flags.encrypt = strtol(key + 8, &last, 0);
1195 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1200 } else if (strncmp(key, "array", 6) == 0) {
1204 case PW_TYPE_IPADDR:
1207 case PW_TYPE_INTEGER:
1212 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1218 * The only thing is the vendor name,
1219 * and it's a known name: allow it.
1221 } else if ((key == argv[3]) && !next && !block_vendor &&
1222 ((vendor = dict_vendorbyname(key)) !=0)) {
1226 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1232 if (key && !*key) break;
1236 if (block_vendor) vendor = block_vendor;
1239 * Special checks for tags, they make our life much more
1242 if (flags.has_tag) {
1244 * Only string, octets, and integer can be tagged.
1247 case PW_TYPE_STRING:
1248 case PW_TYPE_INTEGER:
1252 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1254 fr_int2str(type_table, type, "?Unknown?"));
1259 if (type == PW_TYPE_TLV) {
1265 * TLV's can be only one octet.
1267 if ((value <= 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1268 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1276 value <<= fr_attr_shift[tlv_depth];
1277 value |= block_tlv->attr;
1284 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1287 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1289 fr_strerror_printf("dict_init: %s[%d]: %s",
1299 * Process the VALUE command
1301 static int process_value(const char* fn, const int line, char **argv,
1307 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1312 * For Compatibility, skip "Server-Config"
1314 if (strcasecmp(argv[0], "Server-Config") == 0)
1318 * Validate all entries
1320 if (!sscanf_i(argv[2], &value)) {
1321 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1326 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1329 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1331 fr_strerror_printf("dict_init: %s[%d]: %s",
1341 * Process the VALUE-ALIAS command
1343 * This allows VALUE mappings to be shared among multiple
1346 static int process_value_alias(const char* fn, const int line, char **argv,
1349 DICT_ATTR *my_da, *da;
1353 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1358 my_da = dict_attrbyname(argv[0]);
1360 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1365 if (my_da->flags.has_value) {
1366 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1371 if (my_da->flags.has_value_alias) {
1372 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1377 da = dict_attrbyname(argv[1]);
1379 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1384 if (!da->flags.has_value) {
1385 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1390 if (da->flags.has_value_alias) {
1391 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1396 if (my_da->type != da->type) {
1397 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1402 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1403 fr_strerror_printf("dict_addvalue: out of memory");
1407 dval->name[0] = '\0'; /* empty name */
1408 dval->attr = my_da->attr;
1409 dval->vendor = my_da->vendor;
1410 dval->value = da->attr;
1412 if (!fr_hash_table_insert(values_byname, dval)) {
1413 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1424 * Process the VENDOR command
1426 static int process_vendor(const char* fn, const int line, char **argv,
1430 int continuation = 0;
1431 const char *format = NULL;
1433 if ((argc < 2) || (argc > 3)) {
1434 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1440 * Validate all entries
1442 if (!isdigit((int) argv[1][0])) {
1443 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1447 value = atoi(argv[1]);
1449 /* Create a new VENDOR entry for the list */
1450 if (dict_addvendor(argv[0], value) < 0) {
1453 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1455 fr_strerror_printf("dict_init: %s[%d]: %s",
1461 * Look for a format statement
1466 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1467 format = "format=4,0";
1469 } else if (value == VENDORPEC_LUCENT) {
1470 format = "format=2,1";
1472 } else if (value == VENDORPEC_STARENT) {
1473 format = "format=2,2";
1475 } /* else no fixups to do */
1482 if (strncasecmp(format, "format=", 7) != 0) {
1483 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1489 if ((strlen(p) < 3) ||
1490 !isdigit((int) p[0]) ||
1492 !isdigit((int) p[2]) ||
1493 (p[3] && (p[3] != ','))) {
1494 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1499 type = (int) (p[0] - '0');
1500 length = (int) (p[2] - '0');
1503 if ((p[4] != 'c') ||
1505 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1511 if ((value != VENDORPEC_WIMAX) ||
1512 (type != 1) || (length != 1)) {
1513 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1519 dv = dict_vendorbyvalue(value);
1521 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1526 if ((type != 1) && (type != 2) && (type != 4)) {
1527 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1532 if ((length != 0) && (length != 1) && (length != 2)) {
1533 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1539 dv->length = length;
1540 dv->flags = continuation;
1547 * String split routine. Splits an input string IN PLACE
1548 * into pieces, based on spaces.
1550 static int str2argv(char *str, char **argv, int max_argc)
1555 if (argc >= max_argc) break;
1558 * Chop out comments early.
1565 while ((*str == ' ') ||
1568 (*str == '\n')) *(str++) = '\0';
1579 (*str != '\n')) str++;
1585 #define MAX_ARGV (16)
1588 * Initialize the dictionary.
1590 static int my_dict_init(const char *dir, const char *fn,
1591 const char *src_file, int src_line)
1598 unsigned int vendor;
1599 unsigned int block_vendor;
1600 struct stat statbuf;
1601 char *argv[MAX_ARGV];
1603 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1604 int which_block_tlv = 0;
1606 block_tlv[0] = NULL;
1607 block_tlv[1] = NULL;
1608 block_tlv[2] = NULL;
1610 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1611 strlen(dir) >= sizeof(dirtmp) / 2) {
1612 fr_strerror_printf("dict_init: filename name too long");
1617 * First see if fn is relative to dir. If so, create
1618 * new filename. If not, remember the absolute dir.
1620 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1624 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1625 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1629 if ((fp = fopen(fn, "r")) == NULL) {
1631 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1632 fn, strerror(errno));
1634 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1635 src_file, src_line, fn, strerror(errno));
1640 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1641 if (!S_ISREG(statbuf.st_mode)) {
1643 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1649 * Globally writable dictionaries means that users can control
1650 * the server configuration with little difficulty.
1653 if ((statbuf.st_mode & S_IWOTH) != 0) {
1655 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1661 dict_stat_add(fn, &statbuf);
1664 * Seed the random pool with data.
1666 fr_rand_seed(&statbuf, sizeof(statbuf));
1670 while (fgets(buf, sizeof(buf), fp) != NULL) {
1672 if (buf[0] == '#' || buf[0] == 0 ||
1673 buf[0] == '\n' || buf[0] == '\r')
1677 * Comment characters should NOT be appearing anywhere but
1678 * as start of a comment;
1680 p = strchr(buf, '#');
1683 argc = str2argv(buf, argv, MAX_ARGV);
1684 if (argc == 0) continue;
1687 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1694 * Process VALUE lines.
1696 if (strcasecmp(argv[0], "VALUE") == 0) {
1697 if (process_value(fn, line,
1698 argv + 1, argc - 1) == -1) {
1706 * Perhaps this is an attribute.
1708 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1709 if (process_attribute(fn, line, block_vendor,
1710 block_tlv[which_block_tlv],
1712 argv + 1, argc - 1) == -1) {
1720 * See if we need to import another dictionary.
1722 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1723 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1730 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1731 if (process_value_alias(fn, line,
1732 argv + 1, argc - 1) == -1) {
1740 * Process VENDOR lines.
1742 if (strcasecmp(argv[0], "VENDOR") == 0) {
1743 if (process_vendor(fn, line,
1744 argv + 1, argc - 1) == -1) {
1751 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1754 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1760 da = dict_attrbyname(argv[1]);
1763 "dict_init: %s[%d]: unknown attribute %s",
1769 if (da->type != PW_TYPE_TLV) {
1771 "dict_init: %s[%d]: attribute %s is not of type tlv",
1777 if (which_block_tlv >= MAX_TLV_NEST) {
1779 "dict_init: %s[%d]: TLVs are nested too deep",
1786 block_tlv[++which_block_tlv] = da;
1790 if (strcasecmp(argv[0], "END-TLV") == 0) {
1793 "dict_init: %s[%d] invalid END-TLV entry",
1799 da = dict_attrbyname(argv[1]);
1802 "dict_init: %s[%d]: unknown attribute %s",
1808 if (da != block_tlv[which_block_tlv]) {
1810 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1815 block_tlv[which_block_tlv--] = NULL;
1819 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1822 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1828 vendor = dict_vendorbyname(argv[1]);
1831 "dict_init: %s[%d]: unknown vendor %s",
1837 block_vendor = vendor;
1840 * Check for extended attr VSAs
1843 if (strncmp(argv[2], "format=", 7) != 0) {
1845 "dict_init: %s[%d]: Invalid format %s",
1852 da = dict_attrbyname(p);
1854 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
1860 if (!da->flags.evs) {
1861 fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR. Attribute \"%s\" is not of \"evs\" data type",
1868 * Pack the encapsulating attribute
1869 * into the vendor Id.
1871 block_vendor |= (da->attr & fr_attr_mask[1]) * FR_MAX_VENDOR;
1875 } /* BEGIN-VENDOR */
1877 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1880 "dict_init: %s[%d] invalid END-VENDOR entry",
1886 vendor = dict_vendorbyname(argv[1]);
1889 "dict_init: %s[%d]: unknown vendor %s",
1895 if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
1897 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1907 * Any other string: We don't recognize it.
1909 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1920 * Empty callback for hash table initialization.
1922 static int null_callback(void *ctx, void *data)
1924 ctx = ctx; /* -Wunused */
1925 data = data; /* -Wunused */
1932 * Initialize the directory, then fix the attr member of
1935 int dict_init(const char *dir, const char *fn)
1938 * Check if we need to change anything. If not, don't do
1941 if (dict_stat_check(dir, fn)) {
1946 * Free the dictionaries, and the stat cache.
1949 stat_root_dir = strdup(dir);
1950 stat_root_file = strdup(fn);
1953 * Create the table of vendor by name. There MAY NOT
1954 * be multiple vendors of the same name.
1956 * Each vendor is malloc'd, so the free function is free.
1958 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1959 dict_vendor_name_cmp,
1961 if (!vendors_byname) {
1966 * Create the table of vendors by value. There MAY
1967 * be vendors of the same value. If there are, we
1968 * pick the latest one.
1970 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1971 dict_vendor_value_cmp,
1973 if (!vendors_byvalue) {
1978 * Create the table of attributes by name. There MAY NOT
1979 * be multiple attributes of the same name.
1981 * Each attribute is malloc'd, so the free function is free.
1983 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1986 if (!attributes_byname) {
1991 * Create the table of attributes by value. There MAY
1992 * be attributes of the same value. If there are, we
1993 * pick the latest one.
1995 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1996 dict_attr_value_cmp,
1998 if (!attributes_byvalue) {
2002 values_byname = fr_hash_table_create(dict_value_name_hash,
2003 dict_value_name_cmp,
2005 if (!values_byname) {
2009 values_byvalue = fr_hash_table_create(dict_value_value_hash,
2010 dict_value_value_cmp,
2012 if (!values_byvalue) {
2016 value_fixup = NULL; /* just to be safe. */
2018 if (my_dict_init(dir, fn, NULL, 0) < 0)
2023 value_fixup_t *this, *next;
2025 for (this = value_fixup; this != NULL; this = next) {
2028 a = dict_attrbyname(this->attrstr);
2031 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2032 this->attrstr, this->dval->name);
2033 return -1; /* leak, but they should die... */
2036 this->dval->attr = a->attr;
2039 * Add the value into the dictionary.
2041 if (!fr_hash_table_replace(values_byname,
2043 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2048 * Allow them to use the old name, but
2049 * prefer the new name when printing
2052 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2053 fr_hash_table_replace(values_byvalue,
2059 * Just so we don't lose track of things.
2066 * Walk over all of the hash tables to ensure they're
2067 * initialized. We do this because the threads may perform
2068 * lookups, and we don't want multi-threaded re-ordering
2069 * of the table entries. That would be bad.
2071 fr_hash_table_walk(vendors_byname, null_callback, NULL);
2072 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2074 fr_hash_table_walk(attributes_byname, null_callback, NULL);
2075 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2077 fr_hash_table_walk(values_byvalue, null_callback, NULL);
2078 fr_hash_table_walk(values_byname, null_callback, NULL);
2084 * Get an attribute by its numerical value.
2086 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2090 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2093 dattr.vendor = vendor;
2095 return fr_hash_table_finddata(attributes_byvalue, &dattr);
2099 * Get an attribute by its name.
2101 DICT_ATTR *dict_attrbyname(const char *name)
2104 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2106 if (!name) return NULL;
2108 da = (DICT_ATTR *) buffer;
2109 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2111 return fr_hash_table_finddata(attributes_byname, da);
2115 * Associate a value with an attribute and return it.
2117 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2119 DICT_VALUE dval, *dv;
2122 * First, look up aliases.
2125 dval.vendor = vendor;
2126 dval.name[0] = '\0';
2129 * Look up the attribute alias target, and use
2130 * the correct attribute number if found.
2132 dv = fr_hash_table_finddata(values_byname, &dval);
2133 if (dv) dval.attr = dv->value;
2137 return fr_hash_table_finddata(values_byvalue, &dval);
2141 * Get a value by its name, keyed off of an attribute.
2143 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2145 DICT_VALUE *my_dv, *dv;
2146 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2148 if (!name) return NULL;
2150 my_dv = (DICT_VALUE *) buffer;
2152 my_dv->vendor = vendor;
2153 my_dv->name[0] = '\0';
2156 * Look up the attribute alias target, and use
2157 * the correct attribute number if found.
2159 dv = fr_hash_table_finddata(values_byname, my_dv);
2160 if (dv) my_dv->attr = dv->value;
2162 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2164 return fr_hash_table_finddata(values_byname, my_dv);
2168 * Get the vendor PEC based on the vendor name
2170 * This is efficient only for small numbers of vendors.
2172 int dict_vendorbyname(const char *name)
2175 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
2177 if (!name) return 0;
2179 dv = (DICT_VENDOR *) buffer;
2180 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
2182 dv = fr_hash_table_finddata(vendors_byname, dv);
2185 return dv->vendorpec;
2189 * Return the vendor struct based on the PEC.
2191 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
2195 dv.vendorpec = vendorpec;
2197 return fr_hash_table_finddata(vendors_byvalue, &dv);