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 },
104 #define MAX_TLV_NEST (4)
108 * 8 bits for nested TLV 1
109 * 8 bits for nested TLV 2
110 * 5 bits for nested TLV 3
111 * 3 bits for nested TLV 4
113 const int fr_wimax_max_tlv = MAX_TLV_NEST;
114 const int fr_wimax_shift[MAX_TLV_NEST + 1] = {
118 const int fr_wimax_mask[MAX_TLV_NEST + 1] = {
119 0, 0xff, 0xff, 0x1f, 0x07
124 * Create the hash of the name.
126 * We copy the hash function here because it's substantially faster.
128 #define FNV_MAGIC_INIT (0x811c9dc5)
129 #define FNV_MAGIC_PRIME (0x01000193)
131 static uint32_t dict_hashname(const char *name)
133 uint32_t hash = FNV_MAGIC_INIT;
136 for (p = name; *p != '\0'; p++) {
137 int c = *(const unsigned char *) p;
138 if (isalpha(c)) c = tolower(c);
140 hash *= FNV_MAGIC_PRIME;
141 hash ^= (uint32_t ) (c & 0xff);
149 * Hash callback functions.
151 static uint32_t dict_attr_name_hash(const void *data)
153 return dict_hashname(((const DICT_ATTR *)data)->name);
156 static int dict_attr_name_cmp(const void *one, const void *two)
158 const DICT_ATTR *a = one;
159 const DICT_ATTR *b = two;
161 return strcasecmp(a->name, b->name);
164 static uint32_t dict_attr_value_hash(const void *data)
167 const DICT_ATTR *attr = data;
169 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
170 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
173 static int dict_attr_value_cmp(const void *one, const void *two)
175 const DICT_ATTR *a = one;
176 const DICT_ATTR *b = two;
178 if (a->vendor < b->vendor) return -1;
179 if (a->vendor > b->vendor) return +1;
181 return a->attr - b->attr;
184 static uint32_t dict_vendor_name_hash(const void *data)
186 return dict_hashname(((const DICT_VENDOR *)data)->name);
189 static int dict_vendor_name_cmp(const void *one, const void *two)
191 const DICT_VENDOR *a = one;
192 const DICT_VENDOR *b = two;
194 return strcasecmp(a->name, b->name);
197 static uint32_t dict_vendor_value_hash(const void *data)
199 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
200 sizeof(((const DICT_VENDOR *)data)->vendorpec));
203 static int dict_vendor_value_cmp(const void *one, const void *two)
205 const DICT_VENDOR *a = one;
206 const DICT_VENDOR *b = two;
208 return a->vendorpec - b->vendorpec;
211 static uint32_t dict_value_name_hash(const void *data)
214 const DICT_VALUE *dval = data;
216 hash = dict_hashname(dval->name);
217 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
218 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
221 static int dict_value_name_cmp(const void *one, const void *two)
224 const DICT_VALUE *a = one;
225 const DICT_VALUE *b = two;
227 rcode = a->attr - b->attr;
228 if (rcode != 0) return rcode;
230 rcode = a->vendor - b->vendor;
231 if (rcode != 0) return rcode;
233 return strcasecmp(a->name, b->name);
236 static uint32_t dict_value_value_hash(const void *data)
239 const DICT_VALUE *dval = data;
241 hash = fr_hash(&dval->attr, sizeof(dval->attr));
242 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
243 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
246 static int dict_value_value_cmp(const void *one, const void *two)
249 const DICT_VALUE *a = one;
250 const DICT_VALUE *b = two;
252 if (a->vendor < b->vendor) return -1;
253 if (a->vendor > b->vendor) return +1;
255 rcode = a->attr - b->attr;
256 if (rcode != 0) return rcode;
258 return a->value - b->value;
263 * Free the list of stat buffers
265 static void dict_stat_free(void)
267 dict_stat_t *this, *next;
270 stat_root_dir = NULL;
271 free(stat_root_file);
272 stat_root_file = NULL;
279 for (this = stat_head; this != NULL; this = next) {
285 stat_head = stat_tail = NULL;
290 * Add an entry to the list of stat buffers.
292 static void dict_stat_add(const char *name, const struct stat *stat_buf)
296 this = malloc(sizeof(*this));
298 memset(this, 0, sizeof(*this));
300 this->name = strdup(name);
301 this->mtime = stat_buf->st_mtime;
304 stat_head = stat_tail = this;
306 stat_tail->next = this;
313 * See if any dictionaries have changed. If not, don't
316 static int dict_stat_check(const char *root_dir, const char *root_file)
321 if (!stat_root_dir) return 0;
322 if (!stat_root_file) return 0;
324 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
325 if (strcmp(root_file, stat_root_file) != 0) return 0;
327 if (!stat_head) return 0; /* changed, reload */
329 for (this = stat_head; this != NULL; this = this->next) {
330 if (stat(this->name, &buf) < 0) return 0;
332 if (buf.st_mtime != this->mtime) return 0;
338 typedef struct fr_pool_t {
341 struct fr_pool_t *page_free;
342 struct fr_pool_t *page_next;
345 #define FR_POOL_SIZE (32768)
346 #define FR_ALLOC_ALIGN (8)
348 static fr_pool_t *dict_pool = NULL;
350 static fr_pool_t *fr_pool_create(void)
352 fr_pool_t *fp = malloc(FR_POOL_SIZE);
354 if (!fp) return NULL;
356 memset(fp, 0, FR_POOL_SIZE);
358 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
359 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
361 fp->page_next = NULL;
365 static void fr_pool_delete(fr_pool_t **pfp)
367 fr_pool_t *fp, *next;
369 if (!pfp || !*pfp) return;
371 for (fp = *pfp; fp != NULL; fp = next) {
372 next = fp->page_next;
378 static void *fr_pool_alloc(size_t size)
382 if (size == 0) return NULL;
384 if (size > 256) return NULL; /* shouldn't happen */
387 dict_pool = fr_pool_create();
388 if (!dict_pool) return NULL;
391 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
392 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
395 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
396 dict_pool->page_free->page_next = fr_pool_create();
397 if (!dict_pool->page_free->page_next) return NULL;
398 dict_pool->page_free = dict_pool->page_free->page_next;
401 ptr = dict_pool->page_free->free_ptr;
402 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
408 static void fr_pool_free(UNUSED void *ptr)
411 * Place-holder for later code.
416 * Free the dictionary_attributes and dictionary_values lists.
423 fr_hash_table_free(vendors_byname);
424 fr_hash_table_free(vendors_byvalue);
425 vendors_byname = NULL;
426 vendors_byvalue = NULL;
428 fr_hash_table_free(attributes_byname);
429 fr_hash_table_free(attributes_byvalue);
430 attributes_byname = NULL;
431 attributes_byvalue = NULL;
433 fr_hash_table_free(values_byname);
434 fr_hash_table_free(values_byvalue);
435 values_byname = NULL;
436 values_byvalue = NULL;
438 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
440 fr_pool_delete(&dict_pool);
447 * Add vendor to the list.
449 int dict_addvendor(const char *name, int value)
454 if (value > FR_MAX_VENDOR) {
455 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
459 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
460 fr_strerror_printf("dict_addvendor: vendor name too long");
464 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
465 fr_strerror_printf("dict_addvendor: out of memory");
469 strcpy(dv->name, name);
470 dv->vendorpec = value;
471 dv->type = dv->length = 1; /* defaults */
473 if (!fr_hash_table_insert(vendors_byname, dv)) {
476 old_dv = fr_hash_table_finddata(vendors_byname, dv);
478 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
481 if (old_dv->vendorpec != dv->vendorpec) {
482 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
487 * Already inserted. Discard the duplicate entry.
494 * Insert the SAME pointer (not free'd when this table is
495 * deleted), into another table.
497 * We want this behaviour because we want OLD names for
498 * the attributes to be read from the configuration
499 * files, but when we're printing them, (and looking up
500 * by value) we want to use the NEW name.
502 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
503 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
512 * Add an attribute to the dictionary.
514 int dict_addattr(const char *name, int attr, int vendor, int type,
518 static int max_attr = 0;
521 namelen = strlen(name);
522 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
523 fr_strerror_printf("dict_addattr: attribute name too long");
528 * If the attr is '-1', that means use a pre-existing
529 * one (if it already exists). If one does NOT already exist,
530 * then create a new attribute, with a non-conflicting value,
534 if (dict_attrbyname(name)) {
535 return 0; /* exists, don't add it again */
540 } else if (vendor == 0) {
544 if (attr > max_attr) {
550 * Additional checks for extended attributes.
552 if (flags.extended || flags.extended_flags) {
554 fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" attribute format.");
557 vendor = VENDORPEC_EXTENDED;
559 if ((attr < 256) && (type != PW_TYPE_OCTETS)) {
560 fr_strerror_printf("dict_addattr: The base \"extended\" attribute definition MUST be of type \"octets\".");
564 if (flags.has_tag || flags.array || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
565 fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
571 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
575 if (vendor && (vendor != VENDORPEC_EXTENDED)) {
577 static DICT_VENDOR *last_vendor = NULL;
579 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
580 fr_strerror_printf("TLV's cannot be encrypted");
584 if (flags.is_tlv && flags.has_tag) {
585 fr_strerror_printf("Sub-TLV's cannot have a tag");
589 if (flags.has_tlv && flags.has_tag) {
590 fr_strerror_printf("TLV's cannot have a tag");
595 * Most ATTRIBUTEs are bunched together by
596 * VENDOR. We can save a lot of lookups on
597 * dictionary initialization by caching the last
600 if (last_vendor && (vendor == last_vendor->vendorpec)) {
603 dv = dict_vendorbyvalue(vendor);
608 * If the vendor isn't defined, die.
611 fr_strerror_printf("dict_addattr: Unknown vendor %d",
617 * FIXME: Switch over dv->type, and limit things
620 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
621 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
623 } /* else 256..65535 are allowed */
627 * Create a new attribute for the list
629 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
630 fr_strerror_printf("dict_addattr: out of memory");
634 memcpy(da->name, name, namelen);
635 da->name[namelen] = '\0';
642 * Insert the attribute, only if it's not a duplicate.
644 if (!fr_hash_table_insert(attributes_byname, da)) {
648 * If the attribute has identical number, then
649 * ignore the duplicate.
651 a = fr_hash_table_finddata(attributes_byname, da);
652 if (a && (strcasecmp(a->name, da->name) == 0)) {
653 if (a->attr != da->attr) {
654 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
660 * Same name, same vendor, same attr,
661 * maybe the flags and/or type is
662 * different. Let the new value
663 * over-ride the old one.
668 fr_hash_table_delete(attributes_byvalue, a);
670 if (!fr_hash_table_replace(attributes_byname, da)) {
671 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
678 * Insert the SAME pointer (not free'd when this entry is
679 * deleted), into another table.
681 * We want this behaviour because we want OLD names for
682 * the attributes to be read from the configuration
683 * files, but when we're printing them, (and looking up
684 * by value) we want to use the NEW name.
686 if (!fr_hash_table_replace(attributes_byvalue, da)) {
687 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
691 if (!vendor && (attr > 0) && (attr < 256)) {
692 dict_base_attrs[attr] = da;
700 * Add a value for an attribute to the dictionary.
702 int dict_addvalue(const char *namestr, const char *attrstr, int value)
708 static DICT_ATTR *last_attr = NULL;
711 fr_strerror_printf("dict_addvalue: empty names are not permitted");
715 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
716 fr_strerror_printf("dict_addvalue: value name too long");
720 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
721 fr_strerror_printf("dict_addvalue: out of memory");
724 memset(dval, 0, sizeof(*dval));
726 strcpy(dval->name, namestr);
730 * Most VALUEs are bunched together by ATTRIBUTE. We can
731 * save a lot of lookups on dictionary initialization by
732 * caching the last attribute.
734 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
737 dattr = dict_attrbyname(attrstr);
742 * Remember which attribute is associated with this
743 * value, if possible.
746 if (dattr->flags.has_value_alias) {
747 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
751 dval->attr = dattr->attr;
752 dval->vendor = dattr->vendor;
755 * Enforce valid values
757 * Don't worry about fixups...
759 switch (dattr->type) {
763 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
770 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
776 * Allow octets for now, because
777 * of dictionary.cablelabs
781 case PW_TYPE_INTEGER:
786 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
787 fr_int2str(type_table, dattr->type, "?Unknown?"));
791 dattr->flags.has_value = 1;
793 value_fixup_t *fixup;
795 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
798 fr_strerror_printf("dict_addvalue: out of memory");
801 memset(fixup, 0, sizeof(*fixup));
803 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
807 * Insert to the head of the list.
809 fixup->next = value_fixup;
816 * Add the value into the dictionary.
818 if (!fr_hash_table_insert(values_byname, dval)) {
823 * Suppress duplicates with the same
824 * name and value. There are lots in
827 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
828 if (old && (old->value == dval->value)) {
835 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
840 * There are multiple VALUE's, keyed by attribute, so we
841 * take care of that here.
843 if (!fr_hash_table_replace(values_byvalue, dval)) {
844 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
852 static int sscanf_i(const char *str, int *pvalue)
856 static const char *tab = "0123456789";
858 if ((str[0] == '0') &&
859 ((str[1] == 'x') || (str[1] == 'X'))) {
860 tab = "0123456789abcdef";
869 c = memchr(tab, tolower((int) *str), base);
883 * Process the ATTRIBUTE command
885 static int process_attribute(const char* fn, const int line,
886 const int block_vendor, DICT_ATTR *block_tlv,
887 int tlv_depth, char **argv, int argc)
896 if ((argc < 3) || (argc > 4)) {
897 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
902 memset(&flags, 0, sizeof(flags));
905 * Look for extended attributes before doing anything else.
907 p = strchr(argv[1], '.');
911 * Validate all entries
913 if (!sscanf_i(argv[1], &value)) {
914 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
919 * Parse extended attributes.
926 *p = '.'; /* reset forlater printing */
929 * Does the parent attribute exist?
931 da = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
933 fr_strerror_printf("dict_init: %s[%d]: Entry refers to unknown attribute %d", fn, line, value);
938 * 241.1 means 241 is of type "extended".
941 if (!da->flags.extended && !da->flags.extended_flags) {
942 fr_strerror_printf("dict_init: %s[%d]: Entry refers to a non-extended attribute %d", fn, line, value);
949 q = strchr(p + 1, '.');
955 if (!sscanf_i(p + 1, &sub)) {
956 fr_strerror_printf("dict_init: %s[%d]: Parse error in value \"%s\"", fn, line, argv[1]);
961 * Value is out of bounds.
963 if ((sub == 0) || (sub > 255)) {
964 fr_strerror_printf("dict_init: %s[%d]: Entry has value out of range 0..255: %d", fn, line, sub);
968 value |= (sub << fr_wimax_shift[1]);
971 * If this is defining the contents of a TLV,
972 * look for the parent, and check it.
977 tlv = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
978 if (!tlv || !tlv->flags.has_tlv ||
979 (!tlv->flags.extended && !tlv->flags.extended_flags)) {
980 fr_strerror_printf("dict_init: %s[%d]: Entry refers to Attribute \"%s\", which is not an extended attribute TLV", fn, line, argv[1]);
990 if (!sscanf_i(q + 1, &sub)) {
991 fr_strerror_printf("dict_init: %s[%d]: Parse error in value \"%s\"", fn, line, argv[1]);
996 * Value is out of bounds.
998 if ((sub == 0) || (sub > 255)) {
999 fr_strerror_printf("dict_init: %s[%d]: Entry has value out of range 0..255: %d", fn, line, sub);
1003 value |= (sub << fr_wimax_shift[2]);
1007 * Set which type of attribute this is.
1009 flags.extended = da->flags.extended;
1010 flags.extended_flags = da->flags.extended_flags;
1013 if (strncmp(argv[2], "octets[", 7) != 0) {
1015 * find the type of the attribute.
1017 type = fr_str2int(type_table, argv[2], -1);
1019 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1024 type = PW_TYPE_OCTETS;
1026 p = strchr(argv[2] + 7, ']');
1028 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1034 if (!sscanf_i(argv[1], &length)) {
1035 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1039 if ((length == 0) || (length > 253)) {
1040 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1046 * Only look up the vendor if the string
1051 * Force "length" for data types of fixed length;
1063 case PW_TYPE_IPADDR:
1064 case PW_TYPE_INTEGER:
1065 case PW_TYPE_SIGNED:
1069 case PW_TYPE_ETHERNET:
1077 case PW_TYPE_IPV6ADDR:
1085 flags.length = length;
1087 } else { /* argc == 4: we have options */
1088 char *key, *next, *last;
1091 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1097 next = strchr(key, ',');
1098 if (next) *(next++) = '\0';
1100 if (strcmp(key, "has_tag") == 0 ||
1101 strcmp(key, "has_tag=1") == 0) {
1102 /* Boolean flag, means this is a
1106 } else if (strncmp(key, "encrypt=", 8) == 0) {
1107 /* Encryption method, defaults to 0 (none).
1108 Currently valid is just type 2,
1109 Tunnel-Password style, which can only
1110 be applied to strings. */
1111 flags.encrypt = strtol(key + 8, &last, 0);
1113 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1118 } else if (strncmp(key, "array", 6) == 0) {
1122 case PW_TYPE_IPADDR:
1125 case PW_TYPE_INTEGER:
1130 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1136 * The only thing is the vendor name,
1137 * and it's a known name: allow it.
1139 } else if ((key == argv[3]) && !next && !block_vendor &&
1140 ((vendor = dict_vendorbyname(key)) !=0)) {
1143 } else if (strncmp(key, "extended-flags", 15) == 0) {
1144 if (flags.extended) {
1145 fr_strerror_printf( "dict_init: %s[%d] You cannot set two \"extended\" flags.",
1150 flags.extended_flags = 1;
1152 } else if (strncmp(key, "extended", 9) == 0) {
1153 if (flags.extended_flags) {
1154 fr_strerror_printf( "dict_init: %s[%d] You cannot set two \"extended\" flags.",
1161 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1167 if (key && !*key) break;
1171 if (block_vendor) vendor = block_vendor;
1174 * Special checks for tags, they make our life much more
1177 if (flags.has_tag) {
1179 * Only string, octets, and integer can be tagged.
1182 case PW_TYPE_STRING:
1183 case PW_TYPE_INTEGER:
1187 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1189 fr_int2str(type_table, type, "?Unknown?"));
1194 if (type == PW_TYPE_TLV) {
1200 * TLV's can be only one octet.
1202 if ((value <= 0) || ((value & ~fr_wimax_mask[tlv_depth]) != 0)) {
1203 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1211 value <<= fr_wimax_shift[tlv_depth];
1212 value |= block_tlv->attr;
1219 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1222 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1224 fr_strerror_printf("dict_init: %s[%d]: %s",
1234 * Process the VALUE command
1236 static int process_value(const char* fn, const int line, char **argv,
1242 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1247 * For Compatibility, skip "Server-Config"
1249 if (strcasecmp(argv[0], "Server-Config") == 0)
1253 * Validate all entries
1255 if (!sscanf_i(argv[2], &value)) {
1256 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1261 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1264 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1266 fr_strerror_printf("dict_init: %s[%d]: %s",
1276 * Process the VALUE-ALIAS command
1278 * This allows VALUE mappings to be shared among multiple
1281 static int process_value_alias(const char* fn, const int line, char **argv,
1284 DICT_ATTR *my_da, *da;
1288 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1293 my_da = dict_attrbyname(argv[0]);
1295 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1300 if (my_da->flags.has_value) {
1301 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1306 if (my_da->flags.has_value_alias) {
1307 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1312 da = dict_attrbyname(argv[1]);
1314 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1319 if (!da->flags.has_value) {
1320 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1325 if (da->flags.has_value_alias) {
1326 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1331 if (my_da->type != da->type) {
1332 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1337 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1338 fr_strerror_printf("dict_addvalue: out of memory");
1342 dval->name[0] = '\0'; /* empty name */
1343 dval->attr = my_da->attr;
1344 dval->vendor = my_da->vendor;
1345 dval->value = da->attr;
1347 if (!fr_hash_table_insert(values_byname, dval)) {
1348 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1359 * Process the VENDOR command
1361 static int process_vendor(const char* fn, const int line, char **argv,
1365 int continuation = 0;
1366 const char *format = NULL;
1368 if ((argc < 2) || (argc > 3)) {
1369 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1375 * Validate all entries
1377 if (!isdigit((int) argv[1][0])) {
1378 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1382 value = atoi(argv[1]);
1384 /* Create a new VENDOR entry for the list */
1385 if (dict_addvendor(argv[0], value) < 0) {
1388 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1390 fr_strerror_printf("dict_init: %s[%d]: %s",
1396 * Look for a format statement
1401 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1402 format = "format=4,0";
1404 } else if (value == VENDORPEC_LUCENT) {
1405 format = "format=2,1";
1407 } else if (value == VENDORPEC_STARENT) {
1408 format = "format=2,2";
1410 } /* else no fixups to do */
1417 if (strncasecmp(format, "format=", 7) != 0) {
1418 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1424 if ((strlen(p) < 3) ||
1425 !isdigit((int) p[0]) ||
1427 !isdigit((int) p[2]) ||
1428 (p[3] && (p[3] != ','))) {
1429 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1434 type = (int) (p[0] - '0');
1435 length = (int) (p[2] - '0');
1438 if ((p[4] != 'c') ||
1440 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1446 if ((value != VENDORPEC_WIMAX) ||
1447 (type != 1) || (length != 1)) {
1448 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1454 dv = dict_vendorbyvalue(value);
1456 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1461 if ((type != 1) && (type != 2) && (type != 4)) {
1462 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1467 if ((length != 0) && (length != 1) && (length != 2)) {
1468 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1474 dv->length = length;
1475 dv->flags = continuation;
1482 * String split routine. Splits an input string IN PLACE
1483 * into pieces, based on spaces.
1485 static int str2argv(char *str, char **argv, int max_argc)
1490 if (argc >= max_argc) break;
1493 * Chop out comments early.
1500 while ((*str == ' ') ||
1503 (*str == '\n')) *(str++) = '\0';
1514 (*str != '\n')) str++;
1520 #define MAX_ARGV (16)
1523 * Initialize the dictionary.
1525 static int my_dict_init(const char *dir, const char *fn,
1526 const char *src_file, int src_line)
1535 struct stat statbuf;
1536 char *argv[MAX_ARGV];
1538 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1539 int which_block_tlv = 0;
1541 block_tlv[0] = NULL;
1542 block_tlv[1] = NULL;
1543 block_tlv[2] = NULL;
1545 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1546 strlen(dir) >= sizeof(dirtmp) / 2) {
1547 fr_strerror_printf("dict_init: filename name too long");
1552 * First see if fn is relative to dir. If so, create
1553 * new filename. If not, remember the absolute dir.
1555 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1559 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1560 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1564 if ((fp = fopen(fn, "r")) == NULL) {
1566 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1567 fn, strerror(errno));
1569 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1570 src_file, src_line, fn, strerror(errno));
1575 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1576 if (!S_ISREG(statbuf.st_mode)) {
1578 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1584 * Globally writable dictionaries means that users can control
1585 * the server configuration with little difficulty.
1588 if ((statbuf.st_mode & S_IWOTH) != 0) {
1590 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1596 dict_stat_add(fn, &statbuf);
1599 * Seed the random pool with data.
1601 fr_rand_seed(&statbuf, sizeof(statbuf));
1605 while (fgets(buf, sizeof(buf), fp) != NULL) {
1607 if (buf[0] == '#' || buf[0] == 0 ||
1608 buf[0] == '\n' || buf[0] == '\r')
1612 * Comment characters should NOT be appearing anywhere but
1613 * as start of a comment;
1615 p = strchr(buf, '#');
1618 argc = str2argv(buf, argv, MAX_ARGV);
1619 if (argc == 0) continue;
1622 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1629 * Process VALUE lines.
1631 if (strcasecmp(argv[0], "VALUE") == 0) {
1632 if (process_value(fn, line,
1633 argv + 1, argc - 1) == -1) {
1641 * Perhaps this is an attribute.
1643 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1644 if (process_attribute(fn, line, block_vendor,
1645 block_tlv[which_block_tlv],
1647 argv + 1, argc - 1) == -1) {
1655 * See if we need to import another dictionary.
1657 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1658 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1665 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1666 if (process_value_alias(fn, line,
1667 argv + 1, argc - 1) == -1) {
1675 * Process VENDOR lines.
1677 if (strcasecmp(argv[0], "VENDOR") == 0) {
1678 if (process_vendor(fn, line,
1679 argv + 1, argc - 1) == -1) {
1686 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1689 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1695 da = dict_attrbyname(argv[1]);
1698 "dict_init: %s[%d]: unknown attribute %s",
1704 if (da->type != PW_TYPE_TLV) {
1706 "dict_init: %s[%d]: attribute %s is not of type tlv",
1712 if (which_block_tlv >= MAX_TLV_NEST) {
1714 "dict_init: %s[%d]: TLVs are nested too deep",
1721 block_tlv[++which_block_tlv] = da;
1725 if (strcasecmp(argv[0], "END-TLV") == 0) {
1728 "dict_init: %s[%d] invalid END-TLV entry",
1734 da = dict_attrbyname(argv[1]);
1737 "dict_init: %s[%d]: unknown attribute %s",
1743 if (da != block_tlv[which_block_tlv]) {
1745 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1750 block_tlv[which_block_tlv--] = NULL;
1754 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1757 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1763 vendor = dict_vendorbyname(argv[1]);
1766 "dict_init: %s[%d]: unknown vendor %s",
1771 block_vendor = vendor;
1773 } /* BEGIN-VENDOR */
1775 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1778 "dict_init: %s[%d] invalid END-VENDOR entry",
1784 vendor = dict_vendorbyname(argv[1]);
1787 "dict_init: %s[%d]: unknown vendor %s",
1793 if (vendor != block_vendor) {
1795 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1805 * Any other string: We don't recognize it.
1807 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1818 * Empty callback for hash table initialization.
1820 static int null_callback(void *ctx, void *data)
1822 ctx = ctx; /* -Wunused */
1823 data = data; /* -Wunused */
1830 * Initialize the directory, then fix the attr member of
1833 int dict_init(const char *dir, const char *fn)
1836 * Check if we need to change anything. If not, don't do
1839 if (dict_stat_check(dir, fn)) {
1844 * Free the dictionaries, and the stat cache.
1847 stat_root_dir = strdup(dir);
1848 stat_root_file = strdup(fn);
1851 * Create the table of vendor by name. There MAY NOT
1852 * be multiple vendors of the same name.
1854 * Each vendor is malloc'd, so the free function is free.
1856 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1857 dict_vendor_name_cmp,
1859 if (!vendors_byname) {
1864 * Create the table of vendors by value. There MAY
1865 * be vendors of the same value. If there are, we
1866 * pick the latest one.
1868 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1869 dict_vendor_value_cmp,
1871 if (!vendors_byvalue) {
1876 * Create the table of attributes by name. There MAY NOT
1877 * be multiple attributes of the same name.
1879 * Each attribute is malloc'd, so the free function is free.
1881 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1884 if (!attributes_byname) {
1889 * Create the table of attributes by value. There MAY
1890 * be attributes of the same value. If there are, we
1891 * pick the latest one.
1893 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1894 dict_attr_value_cmp,
1896 if (!attributes_byvalue) {
1900 values_byname = fr_hash_table_create(dict_value_name_hash,
1901 dict_value_name_cmp,
1903 if (!values_byname) {
1907 values_byvalue = fr_hash_table_create(dict_value_value_hash,
1908 dict_value_value_cmp,
1910 if (!values_byvalue) {
1914 value_fixup = NULL; /* just to be safe. */
1916 if (my_dict_init(dir, fn, NULL, 0) < 0)
1921 value_fixup_t *this, *next;
1923 for (this = value_fixup; this != NULL; this = next) {
1926 a = dict_attrbyname(this->attrstr);
1929 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1930 this->attrstr, this->dval->name);
1931 return -1; /* leak, but they should die... */
1934 this->dval->attr = a->attr;
1937 * Add the value into the dictionary.
1939 if (!fr_hash_table_replace(values_byname,
1941 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1946 * Allow them to use the old name, but
1947 * prefer the new name when printing
1950 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
1951 fr_hash_table_replace(values_byvalue,
1957 * Just so we don't lose track of things.
1964 * Walk over all of the hash tables to ensure they're
1965 * initialized. We do this because the threads may perform
1966 * lookups, and we don't want multi-threaded re-ordering
1967 * of the table entries. That would be bad.
1969 fr_hash_table_walk(vendors_byname, null_callback, NULL);
1970 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
1972 fr_hash_table_walk(attributes_byname, null_callback, NULL);
1973 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
1975 fr_hash_table_walk(values_byvalue, null_callback, NULL);
1976 fr_hash_table_walk(values_byname, null_callback, NULL);
1982 * Get an attribute by its numerical value.
1984 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
1988 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
1991 dattr.vendor = vendor;
1993 return fr_hash_table_finddata(attributes_byvalue, &dattr);
1997 * Get an attribute by its name.
1999 DICT_ATTR *dict_attrbyname(const char *name)
2002 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2004 if (!name) return NULL;
2006 da = (DICT_ATTR *) buffer;
2007 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2009 return fr_hash_table_finddata(attributes_byname, da);
2013 * Associate a value with an attribute and return it.
2015 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2017 DICT_VALUE dval, *dv;
2020 * First, look up aliases.
2023 dval.vendor = vendor;
2024 dval.name[0] = '\0';
2027 * Look up the attribute alias target, and use
2028 * the correct attribute number if found.
2030 dv = fr_hash_table_finddata(values_byname, &dval);
2031 if (dv) dval.attr = dv->value;
2035 return fr_hash_table_finddata(values_byvalue, &dval);
2039 * Get a value by its name, keyed off of an attribute.
2041 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2043 DICT_VALUE *my_dv, *dv;
2044 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2046 if (!name) return NULL;
2048 my_dv = (DICT_VALUE *) buffer;
2050 my_dv->vendor = vendor;
2051 my_dv->name[0] = '\0';
2054 * Look up the attribute alias target, and use
2055 * the correct attribute number if found.
2057 dv = fr_hash_table_finddata(values_byname, my_dv);
2058 if (dv) my_dv->attr = dv->value;
2060 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2062 return fr_hash_table_finddata(values_byname, my_dv);
2066 * Get the vendor PEC based on the vendor name
2068 * This is efficient only for small numbers of vendors.
2070 int dict_vendorbyname(const char *name)
2073 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
2075 if (!name) return 0;
2077 dv = (DICT_VENDOR *) buffer;
2078 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
2080 dv = fr_hash_table_finddata(vendors_byname, dv);
2083 return dv->vendorpec;
2087 * Return the vendor struct based on the PEC.
2089 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
2093 dv.vendorpec = vendorpec;
2095 return fr_hash_table_finddata(vendors_byvalue, &dv);