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 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
556 static DICT_VENDOR *last_vendor = NULL;
558 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
559 fr_strerror_printf("TLV's cannot be encrypted");
563 if (flags.is_tlv && flags.has_tag) {
564 fr_strerror_printf("Sub-TLV's cannot have a tag");
568 if (flags.has_tlv && flags.has_tag) {
569 fr_strerror_printf("TLV's cannot have a tag");
574 * Most ATTRIBUTEs are bunched together by
575 * VENDOR. We can save a lot of lookups on
576 * dictionary initialization by caching the last
579 if (last_vendor && (vendor == last_vendor->vendorpec)) {
582 dv = dict_vendorbyvalue(vendor);
587 * If the vendor isn't defined, die.
590 fr_strerror_printf("dict_addattr: Unknown vendor %d",
596 * FIXME: Switch over dv->type, and limit things
599 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
600 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
602 } /* else 256..65535 are allowed */
606 * Create a new attribute for the list
608 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
609 fr_strerror_printf("dict_addattr: out of memory");
613 memcpy(da->name, name, namelen);
614 da->name[namelen] = '\0';
622 * Insert the attribute, only if it's not a duplicate.
624 if (!fr_hash_table_insert(attributes_byname, da)) {
628 * If the attribute has identical number, then
629 * ignore the duplicate.
631 a = fr_hash_table_finddata(attributes_byname, da);
632 if (a && (strcasecmp(a->name, da->name) == 0)) {
633 if (a->attr != da->attr) {
634 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
640 * Same name, same vendor, same attr,
641 * maybe the flags and/or type is
642 * different. Let the new value
643 * over-ride the old one.
648 fr_hash_table_delete(attributes_byvalue, a);
650 if (!fr_hash_table_replace(attributes_byname, da)) {
651 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
658 * Insert the SAME pointer (not free'd when this entry is
659 * deleted), into another table.
661 * We want this behaviour because we want OLD names for
662 * the attributes to be read from the configuration
663 * files, but when we're printing them, (and looking up
664 * by value) we want to use the NEW name.
666 if (!fr_hash_table_replace(attributes_byvalue, da)) {
667 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
671 if (!vendor && (attr > 0) && (attr < 256)) {
672 dict_base_attrs[attr] = da;
680 * Add a value for an attribute to the dictionary.
682 int dict_addvalue(const char *namestr, const char *attrstr, int value)
688 static DICT_ATTR *last_attr = NULL;
691 fr_strerror_printf("dict_addvalue: empty names are not permitted");
695 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
696 fr_strerror_printf("dict_addvalue: value name too long");
700 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
701 fr_strerror_printf("dict_addvalue: out of memory");
704 memset(dval, 0, sizeof(*dval));
706 strcpy(dval->name, namestr);
710 * Most VALUEs are bunched together by ATTRIBUTE. We can
711 * save a lot of lookups on dictionary initialization by
712 * caching the last attribute.
714 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
717 dattr = dict_attrbyname(attrstr);
722 * Remember which attribute is associated with this
723 * value, if possible.
726 if (dattr->flags.has_value_alias) {
727 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
731 dval->attr = dattr->attr;
732 dval->vendor = dattr->vendor;
735 * Enforce valid values
737 * Don't worry about fixups...
739 switch (dattr->type) {
743 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
750 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
756 * Allow octets for now, because
757 * of dictionary.cablelabs
761 case PW_TYPE_INTEGER:
766 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
767 fr_int2str(type_table, dattr->type, "?Unknown?"));
771 dattr->flags.has_value = 1;
773 value_fixup_t *fixup;
775 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
778 fr_strerror_printf("dict_addvalue: out of memory");
781 memset(fixup, 0, sizeof(*fixup));
783 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
787 * Insert to the head of the list.
789 fixup->next = value_fixup;
796 * Add the value into the dictionary.
798 if (!fr_hash_table_insert(values_byname, dval)) {
803 * Suppress duplicates with the same
804 * name and value. There are lots in
807 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
808 if (old && (old->value == dval->value)) {
815 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
820 * There are multiple VALUE's, keyed by attribute, so we
821 * take care of that here.
823 if (!fr_hash_table_replace(values_byvalue, dval)) {
824 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
832 static int sscanf_i(const char *str, int *pvalue)
836 const char *tab = "0123456789";
838 if ((str[0] == '0') &&
839 ((str[1] == 'x') || (str[1] == 'X'))) {
840 tab = "0123456789abcdef";
849 c = memchr(tab, tolower((int) *str), base);
863 * Process the ATTRIBUTE command
865 static int process_attribute(const char* fn, const int line,
866 const int block_vendor, DICT_ATTR *block_tlv,
867 int tlv_depth, char **argv, int argc)
875 if ((argc < 3) || (argc > 4)) {
876 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
882 * Validate all entries
884 if (!sscanf_i(argv[1], &value)) {
885 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
889 if (strncmp(argv[2], "octets[", 7) != 0) {
891 * find the type of the attribute.
893 type = fr_str2int(type_table, argv[2], -1);
895 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
901 type = PW_TYPE_OCTETS;
903 p = strchr(argv[2] + 7, ']');
905 fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
911 if (!sscanf_i(argv[1], &length)) {
912 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
916 if ((length == 0) || (length > 253)) {
917 fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
923 * Only look up the vendor if the string
926 memset(&flags, 0, sizeof(flags));
929 * Force "length" for data types of fixed length;
942 case PW_TYPE_INTEGER:
947 case PW_TYPE_ETHERNET:
955 case PW_TYPE_IPV6ADDR:
963 flags.length = length;
965 } else { /* argc == 4: we have options */
966 char *key, *next, *last;
969 fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
975 next = strchr(key, ',');
976 if (next) *(next++) = '\0';
978 if (strcmp(key, "has_tag") == 0 ||
979 strcmp(key, "has_tag=1") == 0) {
980 /* Boolean flag, means this is a
984 } else if (strncmp(key, "encrypt=", 8) == 0) {
985 /* Encryption method, defaults to 0 (none).
986 Currently valid is just type 2,
987 Tunnel-Password style, which can only
988 be applied to strings. */
989 flags.encrypt = strtol(key + 8, &last, 0);
991 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
996 } else if (strncmp(key, "array", 8) == 0) {
1000 case PW_TYPE_IPADDR:
1003 case PW_TYPE_INTEGER:
1008 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1014 * The only thing is the vendor name,
1015 * and it's a known name: allow it.
1017 } else if ((key == argv[3]) && !next && !block_vendor &&
1018 ((vendor = dict_vendorbyname(key)) !=0)) {
1022 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1028 if (key && !*key) break;
1032 if (block_vendor) vendor = block_vendor;
1035 * Special checks for tags, they make our life much more
1038 if (flags.has_tag) {
1040 * Only string, octets, and integer can be tagged.
1043 case PW_TYPE_STRING:
1044 case PW_TYPE_INTEGER:
1048 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1050 fr_int2str(type_table, type, "?Unknown?"));
1055 if (type == PW_TYPE_TLV) {
1061 * TLV's can be only one octet.
1063 if ((value <= 0) || ((value & ~fr_wimax_mask[tlv_depth]) != 0)) {
1064 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1072 value <<= fr_wimax_shift[tlv_depth];
1073 value |= block_tlv->attr;
1081 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1084 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1086 fr_strerror_printf("dict_init: %s[%d]: %s",
1096 * Process the VALUE command
1098 static int process_value(const char* fn, const int line, char **argv,
1104 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1109 * For Compatibility, skip "Server-Config"
1111 if (strcasecmp(argv[0], "Server-Config") == 0)
1115 * Validate all entries
1117 if (!sscanf_i(argv[2], &value)) {
1118 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1123 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1126 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1128 fr_strerror_printf("dict_init: %s[%d]: %s",
1138 * Process the VALUE-ALIAS command
1140 * This allows VALUE mappings to be shared among multiple
1143 static int process_value_alias(const char* fn, const int line, char **argv,
1146 DICT_ATTR *my_da, *da;
1150 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1155 my_da = dict_attrbyname(argv[0]);
1157 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1162 if (my_da->flags.has_value) {
1163 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1168 if (my_da->flags.has_value_alias) {
1169 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1174 da = dict_attrbyname(argv[1]);
1176 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1181 if (!da->flags.has_value) {
1182 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1187 if (da->flags.has_value_alias) {
1188 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1193 if (my_da->type != da->type) {
1194 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1199 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1200 fr_strerror_printf("dict_addvalue: out of memory");
1204 dval->name[0] = '\0'; /* empty name */
1205 dval->attr = my_da->attr;
1206 dval->vendor = my_da->vendor;
1207 dval->value = da->attr;
1209 if (!fr_hash_table_insert(values_byname, dval)) {
1210 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1221 * Process the VENDOR command
1223 static int process_vendor(const char* fn, const int line, char **argv,
1227 int continuation = 0;
1228 const char *format = NULL;
1230 if ((argc < 2) || (argc > 3)) {
1231 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1237 * Validate all entries
1239 if (!isdigit((int) argv[1][0])) {
1240 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1244 value = atoi(argv[1]);
1246 /* Create a new VENDOR entry for the list */
1247 if (dict_addvendor(argv[0], value) < 0) {
1250 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1252 fr_strerror_printf("dict_init: %s[%d]: %s",
1258 * Look for a format statement
1263 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1264 format = "format=4,0";
1266 } else if (value == VENDORPEC_LUCENT) {
1267 format = "format=2,1";
1269 } else if (value == VENDORPEC_STARENT) {
1270 format = "format=2,2";
1272 } /* else no fixups to do */
1279 if (strncasecmp(format, "format=", 7) != 0) {
1280 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1286 if ((strlen(p) < 3) ||
1287 !isdigit((int) p[0]) ||
1289 !isdigit((int) p[2]) ||
1290 (p[3] && (p[3] != ','))) {
1291 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1296 type = (int) (p[0] - '0');
1297 length = (int) (p[2] - '0');
1300 if ((p[4] != 'c') ||
1302 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1308 if ((value != VENDORPEC_WIMAX) ||
1309 (type != 1) || (length != 1)) {
1310 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1316 dv = dict_vendorbyvalue(value);
1318 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1323 if ((type != 1) && (type != 2) && (type != 4)) {
1324 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1329 if ((length != 0) && (length != 1) && (length != 2)) {
1330 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1336 dv->length = length;
1337 dv->flags = continuation;
1344 * String split routine. Splits an input string IN PLACE
1345 * into pieces, based on spaces.
1347 static int str2argv(char *str, char **argv, int max_argc)
1352 if (argc >= max_argc) break;
1355 * Chop out comments early.
1362 while ((*str == ' ') ||
1365 (*str == '\n')) *(str++) = '\0';
1376 (*str != '\n')) str++;
1382 #define MAX_ARGV (16)
1385 * Initialize the dictionary.
1387 static int my_dict_init(const char *dir, const char *fn,
1388 const char *src_file, int src_line)
1397 struct stat statbuf;
1398 char *argv[MAX_ARGV];
1400 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1401 int which_block_tlv = 0;
1403 block_tlv[0] = NULL;
1404 block_tlv[1] = NULL;
1405 block_tlv[2] = NULL;
1407 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1408 strlen(dir) >= sizeof(dirtmp) / 2) {
1409 fr_strerror_printf("dict_init: filename name too long");
1414 * First see if fn is relative to dir. If so, create
1415 * new filename. If not, remember the absolute dir.
1417 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1421 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1422 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1426 if ((fp = fopen(fn, "r")) == NULL) {
1428 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1429 fn, strerror(errno));
1431 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1432 src_file, src_line, fn, strerror(errno));
1437 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1438 if (!S_ISREG(statbuf.st_mode)) {
1440 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1446 * Globally writable dictionaries means that users can control
1447 * the server configuration with little difficulty.
1450 if ((statbuf.st_mode & S_IWOTH) != 0) {
1452 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1458 dict_stat_add(fn, &statbuf);
1461 * Seed the random pool with data.
1463 fr_rand_seed(&statbuf, sizeof(statbuf));
1467 while (fgets(buf, sizeof(buf), fp) != NULL) {
1469 if (buf[0] == '#' || buf[0] == 0 ||
1470 buf[0] == '\n' || buf[0] == '\r')
1474 * Comment characters should NOT be appearing anywhere but
1475 * as start of a comment;
1477 p = strchr(buf, '#');
1480 argc = str2argv(buf, argv, MAX_ARGV);
1481 if (argc == 0) continue;
1484 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1491 * Process VALUE lines.
1493 if (strcasecmp(argv[0], "VALUE") == 0) {
1494 if (process_value(fn, line,
1495 argv + 1, argc - 1) == -1) {
1503 * Perhaps this is an attribute.
1505 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1506 if (process_attribute(fn, line, block_vendor,
1507 block_tlv[which_block_tlv],
1509 argv + 1, argc - 1) == -1) {
1517 * See if we need to import another dictionary.
1519 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1520 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1527 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1528 if (process_value_alias(fn, line,
1529 argv + 1, argc - 1) == -1) {
1537 * Process VENDOR lines.
1539 if (strcasecmp(argv[0], "VENDOR") == 0) {
1540 if (process_vendor(fn, line,
1541 argv + 1, argc - 1) == -1) {
1548 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1551 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1557 da = dict_attrbyname(argv[1]);
1560 "dict_init: %s[%d]: unknown attribute %s",
1566 if (da->type != PW_TYPE_TLV) {
1568 "dict_init: %s[%d]: attribute %s is not of type tlv",
1574 if (which_block_tlv >= MAX_TLV_NEST) {
1576 "dict_init: %s[%d]: TLVs are nested too deep",
1583 block_tlv[++which_block_tlv] = da;
1587 if (strcasecmp(argv[0], "END-TLV") == 0) {
1590 "dict_init: %s[%d] invalid END-TLV entry",
1596 da = dict_attrbyname(argv[1]);
1599 "dict_init: %s[%d]: unknown attribute %s",
1605 if (da != block_tlv[which_block_tlv]) {
1607 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1612 block_tlv[which_block_tlv--] = NULL;
1616 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1619 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1625 vendor = dict_vendorbyname(argv[1]);
1628 "dict_init: %s[%d]: unknown vendor %s",
1633 block_vendor = vendor;
1635 } /* BEGIN-VENDOR */
1637 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1640 "dict_init: %s[%d] invalid END-VENDOR entry",
1646 vendor = dict_vendorbyname(argv[1]);
1649 "dict_init: %s[%d]: unknown vendor %s",
1655 if (vendor != block_vendor) {
1657 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1667 * Any other string: We don't recognize it.
1669 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1680 * Empty callback for hash table initialization.
1682 static int null_callback(void *ctx, void *data)
1684 ctx = ctx; /* -Wunused */
1685 data = data; /* -Wunused */
1692 * Initialize the directory, then fix the attr member of
1695 int dict_init(const char *dir, const char *fn)
1698 * Check if we need to change anything. If not, don't do
1701 if (dict_stat_check(dir, fn)) {
1706 * Free the dictionaries, and the stat cache.
1709 stat_root_dir = strdup(dir);
1710 stat_root_file = strdup(fn);
1713 * Create the table of vendor by name. There MAY NOT
1714 * be multiple vendors of the same name.
1716 * Each vendor is malloc'd, so the free function is free.
1718 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1719 dict_vendor_name_cmp,
1721 if (!vendors_byname) {
1726 * Create the table of vendors by value. There MAY
1727 * be vendors of the same value. If there are, we
1728 * pick the latest one.
1730 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1731 dict_vendor_value_cmp,
1733 if (!vendors_byvalue) {
1738 * Create the table of attributes by name. There MAY NOT
1739 * be multiple attributes of the same name.
1741 * Each attribute is malloc'd, so the free function is free.
1743 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1746 if (!attributes_byname) {
1751 * Create the table of attributes by value. There MAY
1752 * be attributes of the same value. If there are, we
1753 * pick the latest one.
1755 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1756 dict_attr_value_cmp,
1758 if (!attributes_byvalue) {
1762 values_byname = fr_hash_table_create(dict_value_name_hash,
1763 dict_value_name_cmp,
1765 if (!values_byname) {
1769 values_byvalue = fr_hash_table_create(dict_value_value_hash,
1770 dict_value_value_cmp,
1772 if (!values_byvalue) {
1776 value_fixup = NULL; /* just to be safe. */
1778 if (my_dict_init(dir, fn, NULL, 0) < 0)
1783 value_fixup_t *this, *next;
1785 for (this = value_fixup; this != NULL; this = next) {
1788 a = dict_attrbyname(this->attrstr);
1791 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1792 this->attrstr, this->dval->name);
1793 return -1; /* leak, but they should die... */
1796 this->dval->attr = a->attr;
1799 * Add the value into the dictionary.
1801 if (!fr_hash_table_replace(values_byname,
1803 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1808 * Allow them to use the old name, but
1809 * prefer the new name when printing
1812 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
1813 fr_hash_table_replace(values_byvalue,
1819 * Just so we don't lose track of things.
1826 * Walk over all of the hash tables to ensure they're
1827 * initialized. We do this because the threads may perform
1828 * lookups, and we don't want multi-threaded re-ordering
1829 * of the table entries. That would be bad.
1831 fr_hash_table_walk(vendors_byname, null_callback, NULL);
1832 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
1834 fr_hash_table_walk(attributes_byname, null_callback, NULL);
1835 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
1837 fr_hash_table_walk(values_byvalue, null_callback, NULL);
1838 fr_hash_table_walk(values_byname, null_callback, NULL);
1844 * Get an attribute by its numerical value.
1846 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
1850 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
1853 dattr.vendor = vendor;
1855 return fr_hash_table_finddata(attributes_byvalue, &dattr);
1859 * Get an attribute by its name.
1861 DICT_ATTR *dict_attrbyname(const char *name)
1864 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
1866 if (!name) return NULL;
1868 da = (DICT_ATTR *) buffer;
1869 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
1871 return fr_hash_table_finddata(attributes_byname, da);
1875 * Associate a value with an attribute and return it.
1877 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
1879 DICT_VALUE dval, *dv;
1882 * First, look up aliases.
1885 dval.vendor = vendor;
1886 dval.name[0] = '\0';
1889 * Look up the attribute alias target, and use
1890 * the correct attribute number if found.
1892 dv = fr_hash_table_finddata(values_byname, &dval);
1893 if (dv) dval.attr = dv->value;
1897 return fr_hash_table_finddata(values_byvalue, &dval);
1901 * Get a value by its name, keyed off of an attribute.
1903 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
1905 DICT_VALUE *my_dv, *dv;
1906 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1908 if (!name) return NULL;
1910 my_dv = (DICT_VALUE *) buffer;
1912 my_dv->vendor = vendor;
1913 my_dv->name[0] = '\0';
1916 * Look up the attribute alias target, and use
1917 * the correct attribute number if found.
1919 dv = fr_hash_table_finddata(values_byname, my_dv);
1920 if (dv) my_dv->attr = dv->value;
1922 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
1924 return fr_hash_table_finddata(values_byname, my_dv);
1928 * Get the vendor PEC based on the vendor name
1930 * This is efficient only for small numbers of vendors.
1932 int dict_vendorbyname(const char *name)
1935 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1937 if (!name) return 0;
1939 dv = (DICT_VENDOR *) buffer;
1940 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
1942 dv = fr_hash_table_finddata(vendors_byname, dv);
1945 return dv->vendorpec;
1949 * Return the vendor struct based on the PEC.
1951 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1955 dv.vendorpec = vendorpec;
1957 return fr_hash_table_finddata(vendors_byvalue, &dv);