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 },
102 * Create the hash of the name.
104 * We copy the hash function here because it's substantially faster.
106 #define FNV_MAGIC_INIT (0x811c9dc5)
107 #define FNV_MAGIC_PRIME (0x01000193)
109 static uint32_t dict_hashname(const char *name)
111 uint32_t hash = FNV_MAGIC_INIT;
114 for (p = name; *p != '\0'; p++) {
115 int c = *(const unsigned char *) p;
116 if (isalpha(c)) c = tolower(c);
118 hash *= FNV_MAGIC_PRIME;
119 hash ^= (uint32_t ) (c & 0xff);
127 * Hash callback functions.
129 static uint32_t dict_attr_name_hash(const void *data)
131 return dict_hashname(((const DICT_ATTR *)data)->name);
134 static int dict_attr_name_cmp(const void *one, const void *two)
136 const DICT_ATTR *a = one;
137 const DICT_ATTR *b = two;
139 return strcasecmp(a->name, b->name);
142 static uint32_t dict_attr_value_hash(const void *data)
145 const DICT_ATTR *attr = data;
147 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
148 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
151 static int dict_attr_value_cmp(const void *one, const void *two)
153 const DICT_ATTR *a = one;
154 const DICT_ATTR *b = two;
156 if (a->vendor < b->vendor) return -1;
157 if (a->vendor > b->vendor) return +1;
159 return a->attr - b->attr;
162 static uint32_t dict_vendor_name_hash(const void *data)
164 return dict_hashname(((const DICT_VENDOR *)data)->name);
167 static int dict_vendor_name_cmp(const void *one, const void *two)
169 const DICT_VENDOR *a = one;
170 const DICT_VENDOR *b = two;
172 return strcasecmp(a->name, b->name);
175 static uint32_t dict_vendor_value_hash(const void *data)
177 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
178 sizeof(((const DICT_VENDOR *)data)->vendorpec));
181 static int dict_vendor_value_cmp(const void *one, const void *two)
183 const DICT_VENDOR *a = one;
184 const DICT_VENDOR *b = two;
186 return a->vendorpec - b->vendorpec;
189 static uint32_t dict_value_name_hash(const void *data)
192 const DICT_VALUE *dval = data;
194 hash = dict_hashname(dval->name);
195 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
196 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
199 static int dict_value_name_cmp(const void *one, const void *two)
202 const DICT_VALUE *a = one;
203 const DICT_VALUE *b = two;
205 rcode = a->attr - b->attr;
206 if (rcode != 0) return rcode;
208 rcode = a->vendor - b->vendor;
209 if (rcode != 0) return rcode;
211 return strcasecmp(a->name, b->name);
214 static uint32_t dict_value_value_hash(const void *data)
217 const DICT_VALUE *dval = data;
219 hash = fr_hash(&dval->attr, sizeof(dval->attr));
220 hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
221 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
224 static int dict_value_value_cmp(const void *one, const void *two)
227 const DICT_VALUE *a = one;
228 const DICT_VALUE *b = two;
230 if (a->vendor < b->vendor) return -1;
231 if (a->vendor > b->vendor) return +1;
233 rcode = a->attr - b->attr;
234 if (rcode != 0) return rcode;
236 return a->value - b->value;
241 * Free the list of stat buffers
243 static void dict_stat_free(void)
245 dict_stat_t *this, *next;
248 stat_root_dir = NULL;
249 free(stat_root_file);
250 stat_root_file = NULL;
257 for (this = stat_head; this != NULL; this = next) {
263 stat_head = stat_tail = NULL;
268 * Add an entry to the list of stat buffers.
270 static void dict_stat_add(const char *name, const struct stat *stat_buf)
274 this = malloc(sizeof(*this));
276 memset(this, 0, sizeof(*this));
278 this->name = strdup(name);
279 this->mtime = stat_buf->st_mtime;
282 stat_head = stat_tail = this;
284 stat_tail->next = this;
291 * See if any dictionaries have changed. If not, don't
294 static int dict_stat_check(const char *root_dir, const char *root_file)
299 if (!stat_root_dir) return 0;
300 if (!stat_root_file) return 0;
302 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
303 if (strcmp(root_file, stat_root_file) != 0) return 0;
305 if (!stat_head) return 0; /* changed, reload */
307 for (this = stat_head; this != NULL; this = this->next) {
308 if (stat(this->name, &buf) < 0) return 0;
310 if (buf.st_mtime != this->mtime) return 0;
316 typedef struct fr_pool_t {
319 struct fr_pool_t *page_free;
320 struct fr_pool_t *page_next;
323 #define FR_POOL_SIZE (32768)
324 #define FR_ALLOC_ALIGN (8)
326 static fr_pool_t *dict_pool = NULL;
328 static fr_pool_t *fr_pool_create(void)
330 fr_pool_t *fp = malloc(FR_POOL_SIZE);
332 if (!fp) return NULL;
334 memset(fp, 0, FR_POOL_SIZE);
336 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
337 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
339 fp->page_next = NULL;
343 static void fr_pool_delete(fr_pool_t **pfp)
345 fr_pool_t *fp, *next;
347 if (!pfp || !*pfp) return;
349 for (fp = *pfp; fp != NULL; fp = next) {
350 next = fp->page_next;
356 static void *fr_pool_alloc(size_t size)
360 if (size == 0) return NULL;
362 if (size > 256) return NULL; /* shouldn't happen */
365 dict_pool = fr_pool_create();
366 if (!dict_pool) return NULL;
369 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
370 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
373 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
374 dict_pool->page_free->page_next = fr_pool_create();
375 if (!dict_pool->page_free->page_next) return NULL;
376 dict_pool->page_free = dict_pool->page_free->page_next;
379 ptr = dict_pool->page_free->free_ptr;
380 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
386 static void fr_pool_free(UNUSED void *ptr)
389 * Place-holder for later code.
394 * Free the dictionary_attributes and dictionary_values lists.
401 fr_hash_table_free(vendors_byname);
402 fr_hash_table_free(vendors_byvalue);
403 vendors_byname = NULL;
404 vendors_byvalue = NULL;
406 fr_hash_table_free(attributes_byname);
407 fr_hash_table_free(attributes_byvalue);
408 attributes_byname = NULL;
409 attributes_byvalue = NULL;
411 fr_hash_table_free(values_byname);
412 fr_hash_table_free(values_byvalue);
413 values_byname = NULL;
414 values_byvalue = NULL;
416 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
418 fr_pool_delete(&dict_pool);
425 * Add vendor to the list.
427 int dict_addvendor(const char *name, int value)
433 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 65535");
437 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
438 fr_strerror_printf("dict_addvendor: vendor name too long");
442 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
443 fr_strerror_printf("dict_addvendor: out of memory");
447 strcpy(dv->name, name);
448 dv->vendorpec = value;
449 dv->type = dv->length = 1; /* defaults */
451 if (!fr_hash_table_insert(vendors_byname, dv)) {
454 old_dv = fr_hash_table_finddata(vendors_byname, dv);
456 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
459 if (old_dv->vendorpec != dv->vendorpec) {
460 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
465 * Already inserted. Discard the duplicate entry.
472 * Insert the SAME pointer (not free'd when this table is
473 * deleted), into another table.
475 * We want this behaviour because we want OLD names for
476 * the attributes to be read from the configuration
477 * files, but when we're printing them, (and looking up
478 * by value) we want to use the NEW name.
480 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
481 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
490 * Add an attribute to the dictionary.
492 int dict_addattr(const char *name, int attr, int vendor, int type,
496 static int max_attr = 0;
499 namelen = strlen(name);
500 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
501 fr_strerror_printf("dict_addattr: attribute name too long");
506 * If the attr is '-1', that means use a pre-existing
507 * one (if it already exists). If one does NOT already exist,
508 * then create a new attribute, with a non-conflicting value,
512 if (dict_attrbyname(name)) {
513 return 0; /* exists, don't add it again */
518 } else if (vendor == 0) {
522 if (attr > max_attr) {
528 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
534 static DICT_VENDOR *last_vendor = NULL;
536 if (flags.is_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
537 fr_strerror_printf("Sub-TLV's cannot be encrypted");
541 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
542 fr_strerror_printf("TLV's cannot be encrypted");
546 if (flags.is_tlv && flags.has_tag) {
547 fr_strerror_printf("Sub-TLV's cannot have a tag");
551 if (flags.has_tlv && flags.has_tag) {
552 fr_strerror_printf("TLV's cannot have a tag");
557 * Most ATTRIBUTEs are bunched together by
558 * VENDOR. We can save a lot of lookups on
559 * dictionary initialization by caching the last
562 if (last_vendor && (vendor == last_vendor->vendorpec)) {
565 dv = dict_vendorbyvalue(vendor);
570 * If the vendor isn't defined, die.
573 fr_strerror_printf("dict_addattr: Unknown vendor");
578 * FIXME: Switch over dv->type, and limit things
581 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
582 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
584 } /* else 256..65535 are allowed */
588 * Create a new attribute for the list
590 if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
591 fr_strerror_printf("dict_addattr: out of memory");
595 memcpy(da->name, name, namelen);
596 da->name[namelen] = '\0';
604 * Insert the attribute, only if it's not a duplicate.
606 if (!fr_hash_table_insert(attributes_byname, da)) {
610 * If the attribute has identical number, then
611 * ignore the duplicate.
613 a = fr_hash_table_finddata(attributes_byname, da);
614 if (a && (strcasecmp(a->name, da->name) == 0)) {
615 if (a->attr != da->attr) {
616 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
622 * Same name, same vendor, same attr,
623 * maybe the flags and/or type is
624 * different. Let the new value
625 * over-ride the old one.
630 fr_hash_table_delete(attributes_byvalue, a);
632 if (!fr_hash_table_replace(attributes_byname, da)) {
633 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
640 * Insert the SAME pointer (not free'd when this entry is
641 * deleted), into another table.
643 * We want this behaviour because we want OLD names for
644 * the attributes to be read from the configuration
645 * files, but when we're printing them, (and looking up
646 * by value) we want to use the NEW name.
648 if (!fr_hash_table_replace(attributes_byvalue, da)) {
649 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
653 if (!vendor && (attr > 0) && (attr < 256)) {
654 dict_base_attrs[attr] = da;
662 * Add a value for an attribute to the dictionary.
664 int dict_addvalue(const char *namestr, const char *attrstr, int value)
670 static DICT_ATTR *last_attr = NULL;
673 fr_strerror_printf("dict_addvalue: empty names are not permitted");
677 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
678 fr_strerror_printf("dict_addvalue: value name too long");
682 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
683 fr_strerror_printf("dict_addvalue: out of memory");
686 memset(dval, 0, sizeof(*dval));
688 strcpy(dval->name, namestr);
692 * Most VALUEs are bunched together by ATTRIBUTE. We can
693 * save a lot of lookups on dictionary initialization by
694 * caching the last attribute.
696 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
699 dattr = dict_attrbyname(attrstr);
704 * Remember which attribute is associated with this
705 * value, if possible.
708 if (dattr->flags.has_value_alias) {
709 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
713 dval->attr = dattr->attr;
714 dval->vendor = dattr->vendor;
717 * Enforce valid values
719 * Don't worry about fixups...
721 switch (dattr->type) {
725 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
732 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
738 * Allow octets for now, because
739 * of dictionary.cablelabs
743 case PW_TYPE_INTEGER:
748 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
749 fr_int2str(type_table, dattr->type, "?Unknown?"));
753 dattr->flags.has_value = 1;
755 value_fixup_t *fixup;
757 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
760 fr_strerror_printf("dict_addvalue: out of memory");
763 memset(fixup, 0, sizeof(*fixup));
765 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
769 * Insert to the head of the list.
771 fixup->next = value_fixup;
778 * Add the value into the dictionary.
780 if (!fr_hash_table_insert(values_byname, dval)) {
785 * Suppress duplicates with the same
786 * name and value. There are lots in
789 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
790 if (old && (old->value == dval->value)) {
797 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
802 * There are multiple VALUE's, keyed by attribute, so we
803 * take care of that here.
805 if (!fr_hash_table_replace(values_byvalue, dval)) {
806 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
814 static int sscanf_i(const char *str, int *pvalue)
818 const char *tab = "0123456789";
820 if ((str[0] == '0') &&
821 ((str[1] == 'x') || (str[1] == 'X'))) {
822 tab = "0123456789abcdef";
831 c = memchr(tab, tolower((int) *str), base);
845 * Process the ATTRIBUTE command
847 static int process_attribute(const char* fn, const int line,
848 const int block_vendor, DICT_ATTR *block_tlv,
849 char **argv, int argc)
856 if ((argc < 3) || (argc > 4)) {
857 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
863 * Validate all entries
865 if (!sscanf_i(argv[1], &value)) {
866 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
871 * find the type of the attribute.
873 type = fr_str2int(type_table, argv[2], -1);
875 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
881 * Only look up the vendor if the string
884 memset(&flags, 0, sizeof(flags));
886 char *key, *next, *last;
890 next = strchr(key, ',');
891 if (next) *(next++) = '\0';
893 if (strcmp(key, "has_tag") == 0 ||
894 strcmp(key, "has_tag=1") == 0) {
895 /* Boolean flag, means this is a
899 } else if (strncmp(key, "encrypt=", 8) == 0) {
900 /* Encryption method, defaults to 0 (none).
901 Currently valid is just type 2,
902 Tunnel-Password style, which can only
903 be applied to strings. */
904 flags.encrypt = strtol(key + 8, &last, 0);
906 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
911 } else if (strncmp(key, "array", 8) == 0) {
918 case PW_TYPE_INTEGER:
923 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
929 * The only thing is the vendor name,
930 * and it's a known name: allow it.
932 } else if ((key == argv[3]) && !next && !block_vendor &&
933 ((vendor = dict_vendorbyname(key)) !=0)) {
937 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
943 if (key && !*key) break;
947 if (block_vendor) vendor = block_vendor;
950 * Special checks for tags, they make our life much more
955 * Only string, octets, and integer can be tagged.
959 case PW_TYPE_INTEGER:
963 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
965 fr_int2str(type_table, type, "?Unknown?"));
971 if (type == PW_TYPE_TLV) {
977 * TLV's can be only one octet.
979 if ((value <= 0) || (value > 255)) {
980 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv's cannot have value > 255",
985 if (flags.encrypt != FLAG_ENCRYPT_NONE) {
986 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv's cannot be encrypted",
995 if (block_tlv->flags.is_tlv) value <<= 8;
996 value |= (block_tlv->attr & 0xffff);
1003 if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1006 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1008 fr_strerror_printf("dict_init: %s[%d]: %s",
1018 * Process the VALUE command
1020 static int process_value(const char* fn, const int line, char **argv,
1026 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1031 * For Compatibility, skip "Server-Config"
1033 if (strcasecmp(argv[0], "Server-Config") == 0)
1037 * Validate all entries
1039 if (!sscanf_i(argv[2], &value)) {
1040 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1045 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1048 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1050 fr_strerror_printf("dict_init: %s[%d]: %s",
1060 * Process the VALUE-ALIAS command
1062 * This allows VALUE mappings to be shared among multiple
1065 static int process_value_alias(const char* fn, const int line, char **argv,
1068 DICT_ATTR *my_da, *da;
1072 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1077 my_da = dict_attrbyname(argv[0]);
1079 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1084 if (my_da->flags.has_value) {
1085 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1090 if (my_da->flags.has_value_alias) {
1091 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1096 da = dict_attrbyname(argv[1]);
1098 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1103 if (!da->flags.has_value) {
1104 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1109 if (da->flags.has_value_alias) {
1110 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1115 if (my_da->type != da->type) {
1116 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1121 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1122 fr_strerror_printf("dict_addvalue: out of memory");
1126 dval->name[0] = '\0'; /* empty name */
1127 dval->attr = my_da->attr;
1128 dval->vendor = my_da->vendor;
1129 dval->value = da->attr;
1131 if (!fr_hash_table_insert(values_byname, dval)) {
1132 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1143 * Process the VENDOR command
1145 static int process_vendor(const char* fn, const int line, char **argv,
1149 int continuation = 0;
1150 const char *format = NULL;
1152 if ((argc < 2) || (argc > 3)) {
1153 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1159 * Validate all entries
1161 if (!isdigit((int) argv[1][0])) {
1162 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1166 value = atoi(argv[1]);
1168 /* Create a new VENDOR entry for the list */
1169 if (dict_addvendor(argv[0], value) < 0) {
1172 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1174 fr_strerror_printf("dict_init: %s[%d]: %s",
1180 * Look for a format statement
1185 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1186 format = "format=4,0";
1188 } else if (value == VENDORPEC_LUCENT) {
1189 format = "format=2,1";
1191 } else if (value == VENDORPEC_STARENT) {
1192 format = "format=2,2";
1194 } /* else no fixups to do */
1201 if (strncasecmp(format, "format=", 7) != 0) {
1202 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1208 if ((strlen(p) < 3) ||
1209 !isdigit((int) p[0]) ||
1211 !isdigit((int) p[2]) ||
1212 (p[3] && (p[3] != ','))) {
1213 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1218 type = (int) (p[0] - '0');
1219 length = (int) (p[2] - '0');
1222 if ((p[4] != 'c') ||
1224 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1231 dv = dict_vendorbyvalue(value);
1233 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1238 if ((type != 1) && (type != 2) && (type != 4)) {
1239 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1244 if ((length != 0) && (length != 1) && (length != 2)) {
1245 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1251 dv->length = length;
1252 dv->flags = continuation;
1259 * String split routine. Splits an input string IN PLACE
1260 * into pieces, based on spaces.
1262 static int str2argv(char *str, char **argv, int max_argc)
1267 if (argc >= max_argc) return argc;
1270 * Chop out comments early.
1277 while ((*str == ' ') ||
1280 (*str == '\n')) *(str++) = '\0';
1282 if (!*str) return argc;
1291 (*str != '\n')) str++;
1297 #define MAX_ARGV (16)
1300 * Initialize the dictionary.
1302 static int my_dict_init(const char *dir, const char *fn,
1303 const char *src_file, int src_line)
1312 struct stat statbuf;
1313 char *argv[MAX_ARGV];
1315 DICT_ATTR *da, *block_tlv[3];
1316 int which_block_tlv = 0;
1318 block_tlv[0] = NULL;
1319 block_tlv[1] = NULL;
1320 block_tlv[2] = NULL;
1322 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1323 strlen(dir) >= sizeof(dirtmp) / 2) {
1324 fr_strerror_printf("dict_init: filename name too long");
1329 * First see if fn is relative to dir. If so, create
1330 * new filename. If not, remember the absolute dir.
1332 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1336 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1337 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1341 if ((fp = fopen(fn, "r")) == NULL) {
1343 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1344 fn, strerror(errno));
1346 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1347 src_file, src_line, fn, strerror(errno));
1352 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1353 if (!S_ISREG(statbuf.st_mode)) {
1355 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1361 * Globally writable dictionaries means that users can control
1362 * the server configuration with little difficulty.
1365 if ((statbuf.st_mode & S_IWOTH) != 0) {
1367 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1373 dict_stat_add(fn, &statbuf);
1376 * Seed the random pool with data.
1378 fr_rand_seed(&statbuf, sizeof(statbuf));
1382 while (fgets(buf, sizeof(buf), fp) != NULL) {
1384 if (buf[0] == '#' || buf[0] == 0 ||
1385 buf[0] == '\n' || buf[0] == '\r')
1389 * Comment characters should NOT be appearing anywhere but
1390 * as start of a comment;
1392 p = strchr(buf, '#');
1395 argc = str2argv(buf, argv, MAX_ARGV);
1396 if (argc == 0) continue;
1399 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1406 * Process VALUE lines.
1408 if (strcasecmp(argv[0], "VALUE") == 0) {
1409 if (process_value(fn, line,
1410 argv + 1, argc - 1) == -1) {
1418 * Perhaps this is an attribute.
1420 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1421 if (process_attribute(fn, line, block_vendor,
1422 block_tlv[which_block_tlv],
1423 argv + 1, argc - 1) == -1) {
1431 * See if we need to import another dictionary.
1433 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1434 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1441 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1442 if (process_value_alias(fn, line,
1443 argv + 1, argc - 1) == -1) {
1451 * Process VENDOR lines.
1453 if (strcasecmp(argv[0], "VENDOR") == 0) {
1454 if (process_vendor(fn, line,
1455 argv + 1, argc - 1) == -1) {
1462 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1465 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1471 da = dict_attrbyname(argv[1]);
1474 "dict_init: %s[%d]: unknown attribute %s",
1480 if (da->type != PW_TYPE_TLV) {
1482 "dict_init: %s[%d]: attribute %s is not of type tlv",
1488 if (which_block_tlv >= 2) {
1490 "dict_init: %s[%d]: TLVs are nested too deep",
1496 block_tlv[++which_block_tlv] = da;
1500 if (strcasecmp(argv[0], "END-TLV") == 0) {
1503 "dict_init: %s[%d] invalid END-TLV entry",
1509 da = dict_attrbyname(argv[1]);
1512 "dict_init: %s[%d]: unknown attribute %s",
1518 if (da != block_tlv[which_block_tlv]) {
1520 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1525 block_tlv[which_block_tlv--] = NULL;
1529 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1532 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1538 vendor = dict_vendorbyname(argv[1]);
1541 "dict_init: %s[%d]: unknown vendor %s",
1546 block_vendor = vendor;
1548 } /* BEGIN-VENDOR */
1550 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1553 "dict_init: %s[%d] invalid END-VENDOR entry",
1559 vendor = dict_vendorbyname(argv[1]);
1562 "dict_init: %s[%d]: unknown vendor %s",
1568 if (vendor != block_vendor) {
1570 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1580 * Any other string: We don't recognize it.
1582 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1593 * Empty callback for hash table initialization.
1595 static int null_callback(void *ctx, void *data)
1597 ctx = ctx; /* -Wunused */
1598 data = data; /* -Wunused */
1605 * Initialize the directory, then fix the attr member of
1608 int dict_init(const char *dir, const char *fn)
1611 * Check if we need to change anything. If not, don't do
1614 if (dict_stat_check(dir, fn)) {
1619 * Free the dictionaries, and the stat cache.
1622 stat_root_dir = strdup(dir);
1623 stat_root_file = strdup(fn);
1626 * Create the table of vendor by name. There MAY NOT
1627 * be multiple vendors of the same name.
1629 * Each vendor is malloc'd, so the free function is free.
1631 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1632 dict_vendor_name_cmp,
1634 if (!vendors_byname) {
1639 * Create the table of vendors by value. There MAY
1640 * be vendors of the same value. If there are, we
1641 * pick the latest one.
1643 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1644 dict_vendor_value_cmp,
1646 if (!vendors_byvalue) {
1651 * Create the table of attributes by name. There MAY NOT
1652 * be multiple attributes of the same name.
1654 * Each attribute is malloc'd, so the free function is free.
1656 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1659 if (!attributes_byname) {
1664 * Create the table of attributes by value. There MAY
1665 * be attributes of the same value. If there are, we
1666 * pick the latest one.
1668 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1669 dict_attr_value_cmp,
1671 if (!attributes_byvalue) {
1675 values_byname = fr_hash_table_create(dict_value_name_hash,
1676 dict_value_name_cmp,
1678 if (!values_byname) {
1682 values_byvalue = fr_hash_table_create(dict_value_value_hash,
1683 dict_value_value_cmp,
1685 if (!values_byvalue) {
1689 value_fixup = NULL; /* just to be safe. */
1691 if (my_dict_init(dir, fn, NULL, 0) < 0)
1696 value_fixup_t *this, *next;
1698 for (this = value_fixup; this != NULL; this = next) {
1701 a = dict_attrbyname(this->attrstr);
1704 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1705 this->attrstr, this->dval->name);
1706 return -1; /* leak, but they should die... */
1709 this->dval->attr = a->attr;
1712 * Add the value into the dictionary.
1714 if (!fr_hash_table_replace(values_byname,
1716 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1721 * Allow them to use the old name, but
1722 * prefer the new name when printing
1725 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
1726 fr_hash_table_replace(values_byvalue,
1732 * Just so we don't lose track of things.
1739 * Walk over all of the hash tables to ensure they're
1740 * initialized. We do this because the threads may perform
1741 * lookups, and we don't want multi-threaded re-ordering
1742 * of the table entries. That would be bad.
1744 fr_hash_table_walk(vendors_byname, null_callback, NULL);
1745 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
1747 fr_hash_table_walk(attributes_byname, null_callback, NULL);
1748 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
1750 fr_hash_table_walk(values_byvalue, null_callback, NULL);
1751 fr_hash_table_walk(values_byname, null_callback, NULL);
1757 * Get an attribute by its numerical value.
1759 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
1763 if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
1766 dattr.vendor = vendor;
1768 return fr_hash_table_finddata(attributes_byvalue, &dattr);
1772 * Get an attribute by its name.
1774 DICT_ATTR *dict_attrbyname(const char *name)
1777 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
1779 if (!name) return NULL;
1781 da = (DICT_ATTR *) buffer;
1782 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
1784 return fr_hash_table_finddata(attributes_byname, da);
1788 * Associate a value with an attribute and return it.
1790 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
1792 DICT_VALUE dval, *dv;
1795 * First, look up aliases.
1798 dval.vendor = vendor;
1799 dval.name[0] = '\0';
1802 * Look up the attribute alias target, and use
1803 * the correct attribute number if found.
1805 dv = fr_hash_table_finddata(values_byname, &dval);
1806 if (dv) dval.attr = dv->value;
1810 return fr_hash_table_finddata(values_byvalue, &dval);
1814 * Get a value by its name, keyed off of an attribute.
1816 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
1818 DICT_VALUE *my_dv, *dv;
1819 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1821 if (!name) return NULL;
1823 my_dv = (DICT_VALUE *) buffer;
1825 my_dv->vendor = vendor;
1826 my_dv->name[0] = '\0';
1829 * Look up the attribute alias target, and use
1830 * the correct attribute number if found.
1832 dv = fr_hash_table_finddata(values_byname, my_dv);
1833 if (dv) my_dv->attr = dv->value;
1835 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
1837 return fr_hash_table_finddata(values_byname, my_dv);
1841 * Get the vendor PEC based on the vendor name
1843 * This is efficient only for small numbers of vendors.
1845 int dict_vendorbyname(const char *name)
1848 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1850 if (!name) return 0;
1852 dv = (DICT_VENDOR *) buffer;
1853 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
1855 dv = fr_hash_table_finddata(vendors_byname, dv);
1858 return dv->vendorpec;
1862 * Return the vendor struct based on the PEC.
1864 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1868 dv.vendorpec = vendorpec;
1870 return fr_hash_table_finddata(vendors_byvalue, &dv);