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)
455 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 65535");
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");
595 * FIXME: Switch over dv->type, and limit things
598 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
599 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
601 } /* else 256..65535 are allowed */
605 * Create a new attribute for the list
607 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
608 fr_strerror_printf("dict_addattr: out of memory");
612 memcpy(da->name, name, namelen);
613 da->name[namelen] = '\0';
621 * Insert the attribute, only if it's not a duplicate.
623 if (!fr_hash_table_insert(attributes_byname, da)) {
627 * If the attribute has identical number, then
628 * ignore the duplicate.
630 a = fr_hash_table_finddata(attributes_byname, da);
631 if (a && (strcasecmp(a->name, da->name) == 0)) {
632 if (a->attr != da->attr) {
633 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
639 * Same name, same vendor, same attr,
640 * maybe the flags and/or type is
641 * different. Let the new value
642 * over-ride the old one.
647 fr_hash_table_delete(attributes_byvalue, a);
649 if (!fr_hash_table_replace(attributes_byname, da)) {
650 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
657 * Insert the SAME pointer (not free'd when this entry is
658 * deleted), into another table.
660 * We want this behaviour because we want OLD names for
661 * the attributes to be read from the configuration
662 * files, but when we're printing them, (and looking up
663 * by value) we want to use the NEW name.
665 if (!fr_hash_table_replace(attributes_byvalue, da)) {
666 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
670 if (!vendor && (attr > 0) && (attr < 256)) {
671 dict_base_attrs[attr] = da;
679 * Add a value for an attribute to the dictionary.
681 int dict_addvalue(const char *namestr, const char *attrstr, int value)
687 static DICT_ATTR *last_attr = NULL;
690 fr_strerror_printf("dict_addvalue: empty names are not permitted");
694 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
695 fr_strerror_printf("dict_addvalue: value name too long");
699 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
700 fr_strerror_printf("dict_addvalue: out of memory");
703 memset(dval, 0, sizeof(*dval));
705 strcpy(dval->name, namestr);
709 * Most VALUEs are bunched together by ATTRIBUTE. We can
710 * save a lot of lookups on dictionary initialization by
711 * caching the last attribute.
713 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
716 dattr = dict_attrbyname(attrstr);
721 * Remember which attribute is associated with this
722 * value, if possible.
725 if (dattr->flags.has_value_alias) {
726 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
730 dval->attr = dattr->attr;
731 dval->vendor = dattr->vendor;
734 * Enforce valid values
736 * Don't worry about fixups...
738 switch (dattr->type) {
742 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
749 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
755 * Allow octets for now, because
756 * of dictionary.cablelabs
760 case PW_TYPE_INTEGER:
765 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
766 fr_int2str(type_table, dattr->type, "?Unknown?"));
770 dattr->flags.has_value = 1;
772 value_fixup_t *fixup;
774 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
777 fr_strerror_printf("dict_addvalue: out of memory");
780 memset(fixup, 0, sizeof(*fixup));
782 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
786 * Insert to the head of the list.
788 fixup->next = value_fixup;
795 * Add the value into the dictionary.
797 if (!fr_hash_table_insert(values_byname, dval)) {
802 * Suppress duplicates with the same
803 * name and value. There are lots in
806 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
807 if (old && (old->value == dval->value)) {
814 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
819 * There are multiple VALUE's, keyed by attribute, so we
820 * take care of that here.
822 if (!fr_hash_table_replace(values_byvalue, dval)) {
823 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
831 static int sscanf_i(const char *str, int *pvalue)
835 const char *tab = "0123456789";
837 if ((str[0] == '0') &&
838 ((str[1] == 'x') || (str[1] == 'X'))) {
839 tab = "0123456789abcdef";
848 c = memchr(tab, tolower((int) *str), base);
862 * Process the ATTRIBUTE command
864 static int process_attribute(const char* fn, const int line,
865 const int block_vendor, DICT_ATTR *block_tlv,
866 int tlv_depth, char **argv, int argc)
873 if ((argc < 3) || (argc > 4)) {
874 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
880 * Validate all entries
882 if (!sscanf_i(argv[1], &value)) {
883 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
888 * find the type of the attribute.
890 type = fr_str2int(type_table, argv[2], -1);
892 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
898 * Only look up the vendor if the string
901 memset(&flags, 0, sizeof(flags));
903 char *key, *next, *last;
907 next = strchr(key, ',');
908 if (next) *(next++) = '\0';
910 if (strcmp(key, "has_tag") == 0 ||
911 strcmp(key, "has_tag=1") == 0) {
912 /* Boolean flag, means this is a
916 } else if (strncmp(key, "encrypt=", 8) == 0) {
917 /* Encryption method, defaults to 0 (none).
918 Currently valid is just type 2,
919 Tunnel-Password style, which can only
920 be applied to strings. */
921 flags.encrypt = strtol(key + 8, &last, 0);
923 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
928 } else if (strncmp(key, "array", 8) == 0) {
935 case PW_TYPE_INTEGER:
940 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
946 * The only thing is the vendor name,
947 * and it's a known name: allow it.
949 } else if ((key == argv[3]) && !next && !block_vendor &&
950 ((vendor = dict_vendorbyname(key)) !=0)) {
954 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
960 if (key && !*key) break;
964 if (block_vendor) vendor = block_vendor;
967 * Special checks for tags, they make our life much more
972 * Only string, octets, and integer can be tagged.
976 case PW_TYPE_INTEGER:
980 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
982 fr_int2str(type_table, type, "?Unknown?"));
987 if (type == PW_TYPE_TLV) {
988 if (!block_vendor || (block_vendor != VENDORPEC_WIMAX)) {
989 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX attributes can be of type \"tlv\"",
999 * TLV's can be only one octet.
1001 if ((value <= 0) || ((value & ~fr_wimax_mask[tlv_depth]) != 0)) {
1002 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1010 value <<= fr_wimax_shift[tlv_depth];
1011 value |= block_tlv->attr;
1018 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1021 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1023 fr_strerror_printf("dict_init: %s[%d]: %s",
1033 * Process the VALUE command
1035 static int process_value(const char* fn, const int line, char **argv,
1041 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1046 * For Compatibility, skip "Server-Config"
1048 if (strcasecmp(argv[0], "Server-Config") == 0)
1052 * Validate all entries
1054 if (!sscanf_i(argv[2], &value)) {
1055 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1060 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1063 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1065 fr_strerror_printf("dict_init: %s[%d]: %s",
1075 * Process the VALUE-ALIAS command
1077 * This allows VALUE mappings to be shared among multiple
1080 static int process_value_alias(const char* fn, const int line, char **argv,
1083 DICT_ATTR *my_da, *da;
1087 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1092 my_da = dict_attrbyname(argv[0]);
1094 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1099 if (my_da->flags.has_value) {
1100 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1105 if (my_da->flags.has_value_alias) {
1106 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1111 da = dict_attrbyname(argv[1]);
1113 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1118 if (!da->flags.has_value) {
1119 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1124 if (da->flags.has_value_alias) {
1125 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1130 if (my_da->type != da->type) {
1131 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1136 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1137 fr_strerror_printf("dict_addvalue: out of memory");
1141 dval->name[0] = '\0'; /* empty name */
1142 dval->attr = my_da->attr;
1143 dval->vendor = my_da->vendor;
1144 dval->value = da->attr;
1146 if (!fr_hash_table_insert(values_byname, dval)) {
1147 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1158 * Process the VENDOR command
1160 static int process_vendor(const char* fn, const int line, char **argv,
1164 int continuation = 0;
1165 const char *format = NULL;
1167 if ((argc < 2) || (argc > 3)) {
1168 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1174 * Validate all entries
1176 if (!isdigit((int) argv[1][0])) {
1177 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1181 value = atoi(argv[1]);
1183 /* Create a new VENDOR entry for the list */
1184 if (dict_addvendor(argv[0], value) < 0) {
1187 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1189 fr_strerror_printf("dict_init: %s[%d]: %s",
1195 * Look for a format statement
1200 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1201 format = "format=4,0";
1203 } else if (value == VENDORPEC_LUCENT) {
1204 format = "format=2,1";
1206 } else if (value == VENDORPEC_STARENT) {
1207 format = "format=2,2";
1209 } /* else no fixups to do */
1216 if (strncasecmp(format, "format=", 7) != 0) {
1217 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1223 if ((strlen(p) < 3) ||
1224 !isdigit((int) p[0]) ||
1226 !isdigit((int) p[2]) ||
1227 (p[3] && (p[3] != ','))) {
1228 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1233 type = (int) (p[0] - '0');
1234 length = (int) (p[2] - '0');
1237 if ((p[4] != 'c') ||
1239 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1245 if ((value != VENDORPEC_WIMAX) ||
1246 (type != 1) || (length != 1)) {
1247 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1253 dv = dict_vendorbyvalue(value);
1255 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1260 if ((type != 1) && (type != 2) && (type != 4)) {
1261 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1266 if ((length != 0) && (length != 1) && (length != 2)) {
1267 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1273 dv->length = length;
1274 dv->flags = continuation;
1281 * String split routine. Splits an input string IN PLACE
1282 * into pieces, based on spaces.
1284 static int str2argv(char *str, char **argv, int max_argc)
1289 if (argc >= max_argc) return argc;
1292 * Chop out comments early.
1299 while ((*str == ' ') ||
1302 (*str == '\n')) *(str++) = '\0';
1304 if (!*str) return argc;
1313 (*str != '\n')) str++;
1319 #define MAX_ARGV (16)
1322 * Initialize the dictionary.
1324 static int my_dict_init(const char *dir, const char *fn,
1325 const char *src_file, int src_line)
1334 struct stat statbuf;
1335 char *argv[MAX_ARGV];
1337 DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1338 int which_block_tlv = 0;
1340 block_tlv[0] = NULL;
1341 block_tlv[1] = NULL;
1342 block_tlv[2] = NULL;
1344 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1345 strlen(dir) >= sizeof(dirtmp) / 2) {
1346 fr_strerror_printf("dict_init: filename name too long");
1351 * First see if fn is relative to dir. If so, create
1352 * new filename. If not, remember the absolute dir.
1354 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1358 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1359 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1363 if ((fp = fopen(fn, "r")) == NULL) {
1365 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1366 fn, strerror(errno));
1368 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1369 src_file, src_line, fn, strerror(errno));
1374 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1375 if (!S_ISREG(statbuf.st_mode)) {
1377 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1383 * Globally writable dictionaries means that users can control
1384 * the server configuration with little difficulty.
1387 if ((statbuf.st_mode & S_IWOTH) != 0) {
1389 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1395 dict_stat_add(fn, &statbuf);
1398 * Seed the random pool with data.
1400 fr_rand_seed(&statbuf, sizeof(statbuf));
1404 while (fgets(buf, sizeof(buf), fp) != NULL) {
1406 if (buf[0] == '#' || buf[0] == 0 ||
1407 buf[0] == '\n' || buf[0] == '\r')
1411 * Comment characters should NOT be appearing anywhere but
1412 * as start of a comment;
1414 p = strchr(buf, '#');
1417 argc = str2argv(buf, argv, MAX_ARGV);
1418 if (argc == 0) continue;
1421 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1428 * Process VALUE lines.
1430 if (strcasecmp(argv[0], "VALUE") == 0) {
1431 if (process_value(fn, line,
1432 argv + 1, argc - 1) == -1) {
1440 * Perhaps this is an attribute.
1442 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1443 if (process_attribute(fn, line, block_vendor,
1444 block_tlv[which_block_tlv],
1446 argv + 1, argc - 1) == -1) {
1454 * See if we need to import another dictionary.
1456 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1457 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1464 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1465 if (process_value_alias(fn, line,
1466 argv + 1, argc - 1) == -1) {
1474 * Process VENDOR lines.
1476 if (strcasecmp(argv[0], "VENDOR") == 0) {
1477 if (process_vendor(fn, line,
1478 argv + 1, argc - 1) == -1) {
1485 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1488 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1494 da = dict_attrbyname(argv[1]);
1497 "dict_init: %s[%d]: unknown attribute %s",
1503 if (da->type != PW_TYPE_TLV) {
1505 "dict_init: %s[%d]: attribute %s is not of type tlv",
1511 if (which_block_tlv >= MAX_TLV_NEST) {
1513 "dict_init: %s[%d]: TLVs are nested too deep",
1520 block_tlv[++which_block_tlv] = da;
1524 if (strcasecmp(argv[0], "END-TLV") == 0) {
1527 "dict_init: %s[%d] invalid END-TLV entry",
1533 da = dict_attrbyname(argv[1]);
1536 "dict_init: %s[%d]: unknown attribute %s",
1542 if (da != block_tlv[which_block_tlv]) {
1544 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1549 block_tlv[which_block_tlv--] = NULL;
1553 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1556 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1562 vendor = dict_vendorbyname(argv[1]);
1565 "dict_init: %s[%d]: unknown vendor %s",
1570 block_vendor = vendor;
1572 } /* BEGIN-VENDOR */
1574 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1577 "dict_init: %s[%d] invalid END-VENDOR entry",
1583 vendor = dict_vendorbyname(argv[1]);
1586 "dict_init: %s[%d]: unknown vendor %s",
1592 if (vendor != block_vendor) {
1594 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1604 * Any other string: We don't recognize it.
1606 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1617 * Empty callback for hash table initialization.
1619 static int null_callback(void *ctx, void *data)
1621 ctx = ctx; /* -Wunused */
1622 data = data; /* -Wunused */
1629 * Initialize the directory, then fix the attr member of
1632 int dict_init(const char *dir, const char *fn)
1635 * Check if we need to change anything. If not, don't do
1638 if (dict_stat_check(dir, fn)) {
1643 * Free the dictionaries, and the stat cache.
1646 stat_root_dir = strdup(dir);
1647 stat_root_file = strdup(fn);
1650 * Create the table of vendor by name. There MAY NOT
1651 * be multiple vendors of the same name.
1653 * Each vendor is malloc'd, so the free function is free.
1655 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1656 dict_vendor_name_cmp,
1658 if (!vendors_byname) {
1663 * Create the table of vendors by value. There MAY
1664 * be vendors of the same value. If there are, we
1665 * pick the latest one.
1667 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1668 dict_vendor_value_cmp,
1670 if (!vendors_byvalue) {
1675 * Create the table of attributes by name. There MAY NOT
1676 * be multiple attributes of the same name.
1678 * Each attribute is malloc'd, so the free function is free.
1680 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1683 if (!attributes_byname) {
1688 * Create the table of attributes by value. There MAY
1689 * be attributes of the same value. If there are, we
1690 * pick the latest one.
1692 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1693 dict_attr_value_cmp,
1695 if (!attributes_byvalue) {
1699 values_byname = fr_hash_table_create(dict_value_name_hash,
1700 dict_value_name_cmp,
1702 if (!values_byname) {
1706 values_byvalue = fr_hash_table_create(dict_value_value_hash,
1707 dict_value_value_cmp,
1709 if (!values_byvalue) {
1713 value_fixup = NULL; /* just to be safe. */
1715 if (my_dict_init(dir, fn, NULL, 0) < 0)
1720 value_fixup_t *this, *next;
1722 for (this = value_fixup; this != NULL; this = next) {
1725 a = dict_attrbyname(this->attrstr);
1728 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1729 this->attrstr, this->dval->name);
1730 return -1; /* leak, but they should die... */
1733 this->dval->attr = a->attr;
1736 * Add the value into the dictionary.
1738 if (!fr_hash_table_replace(values_byname,
1740 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1745 * Allow them to use the old name, but
1746 * prefer the new name when printing
1749 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
1750 fr_hash_table_replace(values_byvalue,
1756 * Just so we don't lose track of things.
1763 * Walk over all of the hash tables to ensure they're
1764 * initialized. We do this because the threads may perform
1765 * lookups, and we don't want multi-threaded re-ordering
1766 * of the table entries. That would be bad.
1768 fr_hash_table_walk(vendors_byname, null_callback, NULL);
1769 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
1771 fr_hash_table_walk(attributes_byname, null_callback, NULL);
1772 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
1774 fr_hash_table_walk(values_byvalue, null_callback, NULL);
1775 fr_hash_table_walk(values_byname, null_callback, NULL);
1781 * Get an attribute by its numerical value.
1783 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
1787 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
1790 dattr.vendor = vendor;
1792 return fr_hash_table_finddata(attributes_byvalue, &dattr);
1796 * Get an attribute by its name.
1798 DICT_ATTR *dict_attrbyname(const char *name)
1801 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
1803 if (!name) return NULL;
1805 da = (DICT_ATTR *) buffer;
1806 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
1808 return fr_hash_table_finddata(attributes_byname, da);
1812 * Associate a value with an attribute and return it.
1814 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
1816 DICT_VALUE dval, *dv;
1819 * First, look up aliases.
1822 dval.vendor = vendor;
1823 dval.name[0] = '\0';
1826 * Look up the attribute alias target, and use
1827 * the correct attribute number if found.
1829 dv = fr_hash_table_finddata(values_byname, &dval);
1830 if (dv) dval.attr = dv->value;
1834 return fr_hash_table_finddata(values_byvalue, &dval);
1838 * Get a value by its name, keyed off of an attribute.
1840 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
1842 DICT_VALUE *my_dv, *dv;
1843 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1845 if (!name) return NULL;
1847 my_dv = (DICT_VALUE *) buffer;
1849 my_dv->vendor = vendor;
1850 my_dv->name[0] = '\0';
1853 * Look up the attribute alias target, and use
1854 * the correct attribute number if found.
1856 dv = fr_hash_table_finddata(values_byname, my_dv);
1857 if (dv) my_dv->attr = dv->value;
1859 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
1861 return fr_hash_table_finddata(values_byname, my_dv);
1865 * Get the vendor PEC based on the vendor name
1867 * This is efficient only for small numbers of vendors.
1869 int dict_vendorbyname(const char *name)
1872 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1874 if (!name) return 0;
1876 dv = (DICT_VENDOR *) buffer;
1877 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
1879 dv = fr_hash_table_finddata(vendors_byname, dv);
1882 return dv->vendorpec;
1886 * Return the vendor struct based on the PEC.
1888 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1892 dv.vendorpec = vendorpec;
1894 return fr_hash_table_finddata(vendors_byvalue, &dv);