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 (3)
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_shift[MAX_TLV_NEST + 1] = {
117 const int fr_wimax_mask[MAX_TLV_NEST + 1] = {
118 0, 0xff, 0xff, 0xff, 0x07
123 * Create the hash of the name.
125 * We copy the hash function here because it's substantially faster.
127 #define FNV_MAGIC_INIT (0x811c9dc5)
128 #define FNV_MAGIC_PRIME (0x01000193)
130 static uint32_t dict_hashname(const char *name)
132 uint32_t hash = FNV_MAGIC_INIT;
135 for (p = name; *p != '\0'; p++) {
136 int c = *(const unsigned char *) p;
137 if (isalpha(c)) c = tolower(c);
139 hash *= FNV_MAGIC_PRIME;
140 hash ^= (uint32_t ) (c & 0xff);
148 * Hash callback functions.
150 static uint32_t dict_attr_name_hash(const void *data)
152 return dict_hashname(((const DICT_ATTR *)data)->name);
155 static int dict_attr_name_cmp(const void *one, const void *two)
157 const DICT_ATTR *a = one;
158 const DICT_ATTR *b = two;
160 return strcasecmp(a->name, b->name);
163 static uint32_t dict_attr_value_hash(const void *data)
166 const DICT_ATTR *attr = data;
168 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
169 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
172 static int dict_attr_value_cmp(const void *one, const void *two)
174 const DICT_ATTR *a = one;
175 const DICT_ATTR *b = two;
177 if (a->vendor < b->vendor) return -1;
178 if (a->vendor > b->vendor) return +1;
180 return a->attr - b->attr;
183 static uint32_t dict_vendor_name_hash(const void *data)
185 return dict_hashname(((const DICT_VENDOR *)data)->name);
188 static int dict_vendor_name_cmp(const void *one, const void *two)
190 const DICT_VENDOR *a = one;
191 const DICT_VENDOR *b = two;
193 return strcasecmp(a->name, b->name);
196 static uint32_t dict_vendor_value_hash(const void *data)
198 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
199 sizeof(((const DICT_VENDOR *)data)->vendorpec));
202 static int dict_vendor_value_cmp(const void *one, const void *two)
204 const DICT_VENDOR *a = one;
205 const DICT_VENDOR *b = two;
207 return a->vendorpec - b->vendorpec;
210 static uint32_t dict_value_name_hash(const void *data)
213 const DICT_VALUE *dval = data;
215 hash = dict_hashname(dval->name);
216 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
217 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
220 static int dict_value_name_cmp(const void *one, const void *two)
223 const DICT_VALUE *a = one;
224 const DICT_VALUE *b = two;
226 rcode = a->attr - b->attr;
227 if (rcode != 0) return rcode;
229 rcode = a->vendor - b->vendor;
230 if (rcode != 0) return rcode;
232 return strcasecmp(a->name, b->name);
235 static uint32_t dict_value_value_hash(const void *data)
238 const DICT_VALUE *dval = data;
240 hash = fr_hash(&dval->attr, sizeof(dval->attr));
241 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
242 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
245 static int dict_value_value_cmp(const void *one, const void *two)
248 const DICT_VALUE *a = one;
249 const DICT_VALUE *b = two;
251 if (a->vendor < b->vendor) return -1;
252 if (a->vendor > b->vendor) return +1;
254 rcode = a->attr - b->attr;
255 if (rcode != 0) return rcode;
257 return a->value - b->value;
262 * Free the list of stat buffers
264 static void dict_stat_free(void)
266 dict_stat_t *this, *next;
269 stat_root_dir = NULL;
270 free(stat_root_file);
271 stat_root_file = NULL;
278 for (this = stat_head; this != NULL; this = next) {
284 stat_head = stat_tail = NULL;
289 * Add an entry to the list of stat buffers.
291 static void dict_stat_add(const char *name, const struct stat *stat_buf)
295 this = malloc(sizeof(*this));
297 memset(this, 0, sizeof(*this));
299 this->name = strdup(name);
300 this->mtime = stat_buf->st_mtime;
303 stat_head = stat_tail = this;
305 stat_tail->next = this;
312 * See if any dictionaries have changed. If not, don't
315 static int dict_stat_check(const char *root_dir, const char *root_file)
320 if (!stat_root_dir) return 0;
321 if (!stat_root_file) return 0;
323 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
324 if (strcmp(root_file, stat_root_file) != 0) return 0;
326 if (!stat_head) return 0; /* changed, reload */
328 for (this = stat_head; this != NULL; this = this->next) {
329 if (stat(this->name, &buf) < 0) return 0;
331 if (buf.st_mtime != this->mtime) return 0;
337 typedef struct fr_pool_t {
340 struct fr_pool_t *page_free;
341 struct fr_pool_t *page_next;
344 #define FR_POOL_SIZE (32768)
345 #define FR_ALLOC_ALIGN (8)
347 static fr_pool_t *dict_pool = NULL;
349 static fr_pool_t *fr_pool_create(void)
351 fr_pool_t *fp = malloc(FR_POOL_SIZE);
353 if (!fp) return NULL;
355 memset(fp, 0, FR_POOL_SIZE);
357 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
358 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
360 fp->page_next = NULL;
364 static void fr_pool_delete(fr_pool_t **pfp)
366 fr_pool_t *fp, *next;
368 if (!pfp || !*pfp) return;
370 for (fp = *pfp; fp != NULL; fp = next) {
371 next = fp->page_next;
377 static void *fr_pool_alloc(size_t size)
381 if (size == 0) return NULL;
383 if (size > 256) return NULL; /* shouldn't happen */
386 dict_pool = fr_pool_create();
387 if (!dict_pool) return NULL;
390 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
391 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
394 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
395 dict_pool->page_free->page_next = fr_pool_create();
396 if (!dict_pool->page_free->page_next) return NULL;
397 dict_pool->page_free = dict_pool->page_free->page_next;
400 ptr = dict_pool->page_free->free_ptr;
401 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
407 static void fr_pool_free(UNUSED void *ptr)
410 * Place-holder for later code.
415 * Free the dictionary_attributes and dictionary_values lists.
422 fr_hash_table_free(vendors_byname);
423 fr_hash_table_free(vendors_byvalue);
424 vendors_byname = NULL;
425 vendors_byvalue = NULL;
427 fr_hash_table_free(attributes_byname);
428 fr_hash_table_free(attributes_byvalue);
429 attributes_byname = NULL;
430 attributes_byvalue = NULL;
432 fr_hash_table_free(values_byname);
433 fr_hash_table_free(values_byvalue);
434 values_byname = NULL;
435 values_byvalue = NULL;
437 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
439 fr_pool_delete(&dict_pool);
446 * Add vendor to the list.
448 int dict_addvendor(const char *name, int value)
454 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 65535");
458 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
459 fr_strerror_printf("dict_addvendor: vendor name too long");
463 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
464 fr_strerror_printf("dict_addvendor: out of memory");
468 strcpy(dv->name, name);
469 dv->vendorpec = value;
470 dv->type = dv->length = 1; /* defaults */
472 if (!fr_hash_table_insert(vendors_byname, dv)) {
475 old_dv = fr_hash_table_finddata(vendors_byname, dv);
477 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
480 if (old_dv->vendorpec != dv->vendorpec) {
481 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
486 * Already inserted. Discard the duplicate entry.
493 * Insert the SAME pointer (not free'd when this table is
494 * deleted), into another table.
496 * We want this behaviour because we want OLD names for
497 * the attributes to be read from the configuration
498 * files, but when we're printing them, (and looking up
499 * by value) we want to use the NEW name.
501 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
502 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
511 * Add an attribute to the dictionary.
513 int dict_addattr(const char *name, int attr, int vendor, int type,
517 static int max_attr = 0;
520 namelen = strlen(name);
521 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
522 fr_strerror_printf("dict_addattr: attribute name too long");
527 * If the attr is '-1', that means use a pre-existing
528 * one (if it already exists). If one does NOT already exist,
529 * then create a new attribute, with a non-conflicting value,
533 if (dict_attrbyname(name)) {
534 return 0; /* exists, don't add it again */
539 } else if (vendor == 0) {
543 if (attr > max_attr) {
549 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
555 static DICT_VENDOR *last_vendor = NULL;
557 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
558 fr_strerror_printf("TLV's cannot be encrypted");
562 if (flags.is_tlv && flags.has_tag) {
563 fr_strerror_printf("Sub-TLV's cannot have a tag");
567 if (flags.has_tlv && flags.has_tag) {
568 fr_strerror_printf("TLV's cannot have a tag");
573 * Most ATTRIBUTEs are bunched together by
574 * VENDOR. We can save a lot of lookups on
575 * dictionary initialization by caching the last
578 if (last_vendor && (vendor == last_vendor->vendorpec)) {
581 dv = dict_vendorbyvalue(vendor);
586 * If the vendor isn't defined, die.
589 fr_strerror_printf("dict_addattr: Unknown vendor");
594 * FIXME: Switch over dv->type, and limit things
597 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
598 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
600 } /* else 256..65535 are allowed */
604 * Create a new attribute for the list
606 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
607 fr_strerror_printf("dict_addattr: out of memory");
611 memcpy(da->name, name, namelen);
612 da->name[namelen] = '\0';
620 * Insert the attribute, only if it's not a duplicate.
622 if (!fr_hash_table_insert(attributes_byname, da)) {
626 * If the attribute has identical number, then
627 * ignore the duplicate.
629 a = fr_hash_table_finddata(attributes_byname, da);
630 if (a && (strcasecmp(a->name, da->name) == 0)) {
631 if (a->attr != da->attr) {
632 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
638 * Same name, same vendor, same attr,
639 * maybe the flags and/or type is
640 * different. Let the new value
641 * over-ride the old one.
646 fr_hash_table_delete(attributes_byvalue, a);
648 if (!fr_hash_table_replace(attributes_byname, da)) {
649 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
656 * Insert the SAME pointer (not free'd when this entry is
657 * deleted), into another table.
659 * We want this behaviour because we want OLD names for
660 * the attributes to be read from the configuration
661 * files, but when we're printing them, (and looking up
662 * by value) we want to use the NEW name.
664 if (!fr_hash_table_replace(attributes_byvalue, da)) {
665 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
669 if (!vendor && (attr > 0) && (attr < 256)) {
670 dict_base_attrs[attr] = da;
678 * Add a value for an attribute to the dictionary.
680 int dict_addvalue(const char *namestr, const char *attrstr, int value)
686 static DICT_ATTR *last_attr = NULL;
689 fr_strerror_printf("dict_addvalue: empty names are not permitted");
693 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
694 fr_strerror_printf("dict_addvalue: value name too long");
698 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
699 fr_strerror_printf("dict_addvalue: out of memory");
702 memset(dval, 0, sizeof(*dval));
704 strcpy(dval->name, namestr);
708 * Most VALUEs are bunched together by ATTRIBUTE. We can
709 * save a lot of lookups on dictionary initialization by
710 * caching the last attribute.
712 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
715 dattr = dict_attrbyname(attrstr);
720 * Remember which attribute is associated with this
721 * value, if possible.
724 if (dattr->flags.has_value_alias) {
725 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
729 dval->attr = dattr->attr;
730 dval->vendor = dattr->vendor;
733 * Enforce valid values
735 * Don't worry about fixups...
737 switch (dattr->type) {
741 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
748 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
754 * Allow octets for now, because
755 * of dictionary.cablelabs
759 case PW_TYPE_INTEGER:
764 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
765 fr_int2str(type_table, dattr->type, "?Unknown?"));
769 dattr->flags.has_value = 1;
771 value_fixup_t *fixup;
773 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
776 fr_strerror_printf("dict_addvalue: out of memory");
779 memset(fixup, 0, sizeof(*fixup));
781 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
785 * Insert to the head of the list.
787 fixup->next = value_fixup;
794 * Add the value into the dictionary.
796 if (!fr_hash_table_insert(values_byname, dval)) {
801 * Suppress duplicates with the same
802 * name and value. There are lots in
805 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
806 if (old && (old->value == dval->value)) {
813 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
818 * There are multiple VALUE's, keyed by attribute, so we
819 * take care of that here.
821 if (!fr_hash_table_replace(values_byvalue, dval)) {
822 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
830 static int sscanf_i(const char *str, int *pvalue)
834 const char *tab = "0123456789";
836 if ((str[0] == '0') &&
837 ((str[1] == 'x') || (str[1] == 'X'))) {
838 tab = "0123456789abcdef";
847 c = memchr(tab, tolower((int) *str), base);
861 * Process the ATTRIBUTE command
863 static int process_attribute(const char* fn, const int line,
864 const int block_vendor, DICT_ATTR *block_tlv,
865 int tlv_depth, char **argv, int argc)
872 if ((argc < 3) || (argc > 4)) {
873 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
879 * Validate all entries
881 if (!sscanf_i(argv[1], &value)) {
882 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
887 * find the type of the attribute.
889 type = fr_str2int(type_table, argv[2], -1);
891 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
897 * Only look up the vendor if the string
900 memset(&flags, 0, sizeof(flags));
902 char *key, *next, *last;
906 next = strchr(key, ',');
907 if (next) *(next++) = '\0';
909 if (strcmp(key, "has_tag") == 0 ||
910 strcmp(key, "has_tag=1") == 0) {
911 /* Boolean flag, means this is a
915 } else if (strncmp(key, "encrypt=", 8) == 0) {
916 /* Encryption method, defaults to 0 (none).
917 Currently valid is just type 2,
918 Tunnel-Password style, which can only
919 be applied to strings. */
920 flags.encrypt = strtol(key + 8, &last, 0);
922 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
927 } else if (strncmp(key, "array", 8) == 0) {
934 case PW_TYPE_INTEGER:
939 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
945 * The only thing is the vendor name,
946 * and it's a known name: allow it.
948 } else if ((key == argv[3]) && !next && !block_vendor &&
949 ((vendor = dict_vendorbyname(key)) !=0)) {
953 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
959 if (key && !*key) break;
963 if (block_vendor) vendor = block_vendor;
966 * Special checks for tags, they make our life much more
971 * Only string, octets, and integer can be tagged.
975 case PW_TYPE_INTEGER:
979 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
981 fr_int2str(type_table, type, "?Unknown?"));
987 if (type == PW_TYPE_TLV) {
993 * TLV's can be only one octet.
995 if ((value <= 0) || ((value & ~fr_wimax_mask[tlv_depth]) != 0)) {
996 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1004 value <<= fr_wimax_shift[tlv_depth];
1005 value |= block_tlv->attr;
1012 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1015 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1017 fr_strerror_printf("dict_init: %s[%d]: %s",
1027 * Process the VALUE command
1029 static int process_value(const char* fn, const int line, char **argv,
1035 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1040 * For Compatibility, skip "Server-Config"
1042 if (strcasecmp(argv[0], "Server-Config") == 0)
1046 * Validate all entries
1048 if (!sscanf_i(argv[2], &value)) {
1049 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1054 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1057 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1059 fr_strerror_printf("dict_init: %s[%d]: %s",
1069 * Process the VALUE-ALIAS command
1071 * This allows VALUE mappings to be shared among multiple
1074 static int process_value_alias(const char* fn, const int line, char **argv,
1077 DICT_ATTR *my_da, *da;
1081 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1086 my_da = dict_attrbyname(argv[0]);
1088 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1093 if (my_da->flags.has_value) {
1094 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1099 if (my_da->flags.has_value_alias) {
1100 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1105 da = dict_attrbyname(argv[1]);
1107 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1112 if (!da->flags.has_value) {
1113 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1118 if (da->flags.has_value_alias) {
1119 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1124 if (my_da->type != da->type) {
1125 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1130 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1131 fr_strerror_printf("dict_addvalue: out of memory");
1135 dval->name[0] = '\0'; /* empty name */
1136 dval->attr = my_da->attr;
1137 dval->vendor = my_da->vendor;
1138 dval->value = da->attr;
1140 if (!fr_hash_table_insert(values_byname, dval)) {
1141 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1152 * Process the VENDOR command
1154 static int process_vendor(const char* fn, const int line, char **argv,
1158 int continuation = 0;
1159 const char *format = NULL;
1161 if ((argc < 2) || (argc > 3)) {
1162 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1168 * Validate all entries
1170 if (!isdigit((int) argv[1][0])) {
1171 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1175 value = atoi(argv[1]);
1177 /* Create a new VENDOR entry for the list */
1178 if (dict_addvendor(argv[0], value) < 0) {
1181 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1183 fr_strerror_printf("dict_init: %s[%d]: %s",
1189 * Look for a format statement
1194 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1195 format = "format=4,0";
1197 } else if (value == VENDORPEC_LUCENT) {
1198 format = "format=2,1";
1200 } else if (value == VENDORPEC_STARENT) {
1201 format = "format=2,2";
1203 } /* else no fixups to do */
1210 if (strncasecmp(format, "format=", 7) != 0) {
1211 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1217 if ((strlen(p) < 3) ||
1218 !isdigit((int) p[0]) ||
1220 !isdigit((int) p[2]) ||
1221 (p[3] && (p[3] != ','))) {
1222 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1227 type = (int) (p[0] - '0');
1228 length = (int) (p[2] - '0');
1231 if ((p[4] != 'c') ||
1233 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1239 if ((value != VENDORPEC_WIMAX) ||
1240 (type != 1) || (length != 1)) {
1241 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1247 dv = dict_vendorbyvalue(value);
1249 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1254 if ((type != 1) && (type != 2) && (type != 4)) {
1255 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1260 if ((length != 0) && (length != 1) && (length != 2)) {
1261 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1267 dv->length = length;
1268 dv->flags = continuation;
1275 * String split routine. Splits an input string IN PLACE
1276 * into pieces, based on spaces.
1278 static int str2argv(char *str, char **argv, int max_argc)
1283 if (argc >= max_argc) return argc;
1286 * Chop out comments early.
1293 while ((*str == ' ') ||
1296 (*str == '\n')) *(str++) = '\0';
1298 if (!*str) return argc;
1307 (*str != '\n')) str++;
1313 #define MAX_ARGV (16)
1316 * Initialize the dictionary.
1318 static int my_dict_init(const char *dir, const char *fn,
1319 const char *src_file, int src_line)
1328 struct stat statbuf;
1329 char *argv[MAX_ARGV];
1331 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1332 int which_block_tlv = 0;
1334 block_tlv[0] = NULL;
1335 block_tlv[1] = NULL;
1336 block_tlv[2] = NULL;
1338 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1339 strlen(dir) >= sizeof(dirtmp) / 2) {
1340 fr_strerror_printf("dict_init: filename name too long");
1345 * First see if fn is relative to dir. If so, create
1346 * new filename. If not, remember the absolute dir.
1348 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1352 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1353 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1357 if ((fp = fopen(fn, "r")) == NULL) {
1359 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1360 fn, strerror(errno));
1362 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1363 src_file, src_line, fn, strerror(errno));
1368 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1369 if (!S_ISREG(statbuf.st_mode)) {
1371 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1377 * Globally writable dictionaries means that users can control
1378 * the server configuration with little difficulty.
1381 if ((statbuf.st_mode & S_IWOTH) != 0) {
1383 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1389 dict_stat_add(fn, &statbuf);
1392 * Seed the random pool with data.
1394 fr_rand_seed(&statbuf, sizeof(statbuf));
1398 while (fgets(buf, sizeof(buf), fp) != NULL) {
1400 if (buf[0] == '#' || buf[0] == 0 ||
1401 buf[0] == '\n' || buf[0] == '\r')
1405 * Comment characters should NOT be appearing anywhere but
1406 * as start of a comment;
1408 p = strchr(buf, '#');
1411 argc = str2argv(buf, argv, MAX_ARGV);
1412 if (argc == 0) continue;
1415 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1422 * Process VALUE lines.
1424 if (strcasecmp(argv[0], "VALUE") == 0) {
1425 if (process_value(fn, line,
1426 argv + 1, argc - 1) == -1) {
1434 * Perhaps this is an attribute.
1436 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1437 if (process_attribute(fn, line, block_vendor,
1438 block_tlv[which_block_tlv],
1440 argv + 1, argc - 1) == -1) {
1448 * See if we need to import another dictionary.
1450 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1451 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1458 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1459 if (process_value_alias(fn, line,
1460 argv + 1, argc - 1) == -1) {
1468 * Process VENDOR lines.
1470 if (strcasecmp(argv[0], "VENDOR") == 0) {
1471 if (process_vendor(fn, line,
1472 argv + 1, argc - 1) == -1) {
1479 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1482 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1488 da = dict_attrbyname(argv[1]);
1491 "dict_init: %s[%d]: unknown attribute %s",
1497 if (da->type != PW_TYPE_TLV) {
1499 "dict_init: %s[%d]: attribute %s is not of type tlv",
1505 if (which_block_tlv >= MAX_TLV_NEST) {
1507 "dict_init: %s[%d]: TLVs are nested too deep",
1514 block_tlv[++which_block_tlv] = da;
1518 if (strcasecmp(argv[0], "END-TLV") == 0) {
1521 "dict_init: %s[%d] invalid END-TLV entry",
1527 da = dict_attrbyname(argv[1]);
1530 "dict_init: %s[%d]: unknown attribute %s",
1536 if (da != block_tlv[which_block_tlv]) {
1538 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1543 block_tlv[which_block_tlv--] = NULL;
1547 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1550 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1556 vendor = dict_vendorbyname(argv[1]);
1559 "dict_init: %s[%d]: unknown vendor %s",
1564 block_vendor = vendor;
1566 } /* BEGIN-VENDOR */
1568 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1571 "dict_init: %s[%d] invalid END-VENDOR entry",
1577 vendor = dict_vendorbyname(argv[1]);
1580 "dict_init: %s[%d]: unknown vendor %s",
1586 if (vendor != block_vendor) {
1588 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1598 * Any other string: We don't recognize it.
1600 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1611 * Empty callback for hash table initialization.
1613 static int null_callback(void *ctx, void *data)
1615 ctx = ctx; /* -Wunused */
1616 data = data; /* -Wunused */
1623 * Initialize the directory, then fix the attr member of
1626 int dict_init(const char *dir, const char *fn)
1629 * Check if we need to change anything. If not, don't do
1632 if (dict_stat_check(dir, fn)) {
1637 * Free the dictionaries, and the stat cache.
1640 stat_root_dir = strdup(dir);
1641 stat_root_file = strdup(fn);
1644 * Create the table of vendor by name. There MAY NOT
1645 * be multiple vendors of the same name.
1647 * Each vendor is malloc'd, so the free function is free.
1649 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1650 dict_vendor_name_cmp,
1652 if (!vendors_byname) {
1657 * Create the table of vendors by value. There MAY
1658 * be vendors of the same value. If there are, we
1659 * pick the latest one.
1661 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1662 dict_vendor_value_cmp,
1664 if (!vendors_byvalue) {
1669 * Create the table of attributes by name. There MAY NOT
1670 * be multiple attributes of the same name.
1672 * Each attribute is malloc'd, so the free function is free.
1674 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1677 if (!attributes_byname) {
1682 * Create the table of attributes by value. There MAY
1683 * be attributes of the same value. If there are, we
1684 * pick the latest one.
1686 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1687 dict_attr_value_cmp,
1689 if (!attributes_byvalue) {
1693 values_byname = fr_hash_table_create(dict_value_name_hash,
1694 dict_value_name_cmp,
1696 if (!values_byname) {
1700 values_byvalue = fr_hash_table_create(dict_value_value_hash,
1701 dict_value_value_cmp,
1703 if (!values_byvalue) {
1707 value_fixup = NULL; /* just to be safe. */
1709 if (my_dict_init(dir, fn, NULL, 0) < 0)
1714 value_fixup_t *this, *next;
1716 for (this = value_fixup; this != NULL; this = next) {
1719 a = dict_attrbyname(this->attrstr);
1722 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1723 this->attrstr, this->dval->name);
1724 return -1; /* leak, but they should die... */
1727 this->dval->attr = a->attr;
1730 * Add the value into the dictionary.
1732 if (!fr_hash_table_replace(values_byname,
1734 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1739 * Allow them to use the old name, but
1740 * prefer the new name when printing
1743 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
1744 fr_hash_table_replace(values_byvalue,
1750 * Just so we don't lose track of things.
1757 * Walk over all of the hash tables to ensure they're
1758 * initialized. We do this because the threads may perform
1759 * lookups, and we don't want multi-threaded re-ordering
1760 * of the table entries. That would be bad.
1762 fr_hash_table_walk(vendors_byname, null_callback, NULL);
1763 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
1765 fr_hash_table_walk(attributes_byname, null_callback, NULL);
1766 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
1768 fr_hash_table_walk(values_byvalue, null_callback, NULL);
1769 fr_hash_table_walk(values_byname, null_callback, NULL);
1775 * Get an attribute by its numerical value.
1777 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
1781 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
1784 dattr.vendor = vendor;
1786 return fr_hash_table_finddata(attributes_byvalue, &dattr);
1790 * Get an attribute by its name.
1792 DICT_ATTR *dict_attrbyname(const char *name)
1795 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
1797 if (!name) return NULL;
1799 da = (DICT_ATTR *) buffer;
1800 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
1802 return fr_hash_table_finddata(attributes_byname, da);
1806 * Associate a value with an attribute and return it.
1808 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
1810 DICT_VALUE dval, *dv;
1813 * First, look up aliases.
1816 dval.vendor = vendor;
1817 dval.name[0] = '\0';
1820 * Look up the attribute alias target, and use
1821 * the correct attribute number if found.
1823 dv = fr_hash_table_finddata(values_byname, &dval);
1824 if (dv) dval.attr = dv->value;
1828 return fr_hash_table_finddata(values_byvalue, &dval);
1832 * Get a value by its name, keyed off of an attribute.
1834 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
1836 DICT_VALUE *my_dv, *dv;
1837 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1839 if (!name) return NULL;
1841 my_dv = (DICT_VALUE *) buffer;
1843 my_dv->vendor = vendor;
1844 my_dv->name[0] = '\0';
1847 * Look up the attribute alias target, and use
1848 * the correct attribute number if found.
1850 dv = fr_hash_table_finddata(values_byname, my_dv);
1851 if (dv) my_dv->attr = dv->value;
1853 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
1855 return fr_hash_table_finddata(values_byname, my_dv);
1859 * Get the vendor PEC based on the vendor name
1861 * This is efficient only for small numbers of vendors.
1863 int dict_vendorbyname(const char *name)
1866 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1868 if (!name) return 0;
1870 dv = (DICT_VENDOR *) buffer;
1871 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
1873 dv = fr_hash_table_finddata(vendors_byname, dv);
1876 return dv->vendorpec;
1880 * Return the vendor struct based on the PEC.
1882 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1886 dv.vendorpec = vendorpec;
1888 return fr_hash_table_finddata(vendors_byvalue, &dv);