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 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
198 static int dict_value_name_cmp(const void *one, const void *two)
201 const DICT_VALUE *a = one;
202 const DICT_VALUE *b = two;
204 rcode = a->attr - b->attr;
205 if (rcode != 0) return rcode;
207 return strcasecmp(a->name, b->name);
210 static uint32_t dict_value_value_hash(const void *data)
213 const DICT_VALUE *dval = data;
215 hash = fr_hash(&dval->attr, sizeof(dval->attr));
216 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
219 static int dict_value_value_cmp(const void *one, const void *two)
222 const DICT_VALUE *a = one;
223 const DICT_VALUE *b = two;
225 rcode = a->attr - b->attr;
226 if (rcode != 0) return rcode;
228 return a->value - b->value;
233 * Free the list of stat buffers
235 static void dict_stat_free(void)
237 dict_stat_t *this, *next;
240 stat_root_dir = NULL;
241 free(stat_root_file);
242 stat_root_file = NULL;
249 for (this = stat_head; this != NULL; this = next) {
255 stat_head = stat_tail = NULL;
260 * Add an entry to the list of stat buffers.
262 static void dict_stat_add(const char *name, const struct stat *stat_buf)
266 this = malloc(sizeof(*this));
268 memset(this, 0, sizeof(*this));
270 this->name = strdup(name);
271 this->mtime = stat_buf->st_mtime;
274 stat_head = stat_tail = this;
276 stat_tail->next = this;
283 * See if any dictionaries have changed. If not, don't
286 static int dict_stat_check(const char *root_dir, const char *root_file)
291 if (!stat_root_dir) return 0;
292 if (!stat_root_file) return 0;
294 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
295 if (strcmp(root_file, stat_root_file) != 0) return 0;
297 if (!stat_head) return 0; /* changed, reload */
299 for (this = stat_head; this != NULL; this = this->next) {
300 if (stat(this->name, &buf) < 0) return 0;
302 if (buf.st_mtime != this->mtime) return 0;
308 typedef struct fr_pool_t {
311 struct fr_pool_t *page_free;
312 struct fr_pool_t *page_next;
315 #define FR_POOL_SIZE (32768)
316 #define FR_ALLOC_ALIGN (8)
318 static fr_pool_t *dict_pool = NULL;
320 static fr_pool_t *fr_pool_create(void)
322 fr_pool_t *fp = malloc(FR_POOL_SIZE);
324 if (!fp) return NULL;
326 memset(fp, 0, FR_POOL_SIZE);
328 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
329 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
331 fp->page_next = NULL;
335 static void fr_pool_delete(fr_pool_t **pfp)
337 fr_pool_t *fp, *next;
339 if (!pfp || !*pfp) return;
341 for (fp = *pfp; fp != NULL; fp = next) {
342 next = fp->page_next;
348 static void *fr_pool_alloc(size_t size)
352 if (size == 0) return NULL;
354 if (size > 256) return NULL; /* shouldn't happen */
357 dict_pool = fr_pool_create();
358 if (!dict_pool) return NULL;
361 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
362 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
365 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
366 dict_pool->page_free->page_next = fr_pool_create();
367 if (!dict_pool->page_free->page_next) return NULL;
368 dict_pool->page_free = dict_pool->page_free->page_next;
371 ptr = dict_pool->page_free->free_ptr;
372 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
378 static void fr_pool_free(UNUSED void *ptr)
381 * Place-holder for later code.
386 * Free the dictionary_attributes and dictionary_values lists.
393 fr_hash_table_free(vendors_byname);
394 fr_hash_table_free(vendors_byvalue);
395 vendors_byname = NULL;
396 vendors_byvalue = NULL;
398 fr_hash_table_free(attributes_byname);
399 fr_hash_table_free(attributes_byvalue);
400 attributes_byname = NULL;
401 attributes_byvalue = NULL;
403 fr_hash_table_free(values_byname);
404 fr_hash_table_free(values_byvalue);
405 values_byname = NULL;
406 values_byvalue = NULL;
408 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
410 fr_pool_delete(&dict_pool);
417 * Add vendor to the list.
419 int dict_addvendor(const char *name, int value)
424 if (value >= 32767) {
425 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 65535");
429 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
430 fr_strerror_printf("dict_addvendor: vendor name too long");
434 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
435 fr_strerror_printf("dict_addvendor: out of memory");
439 strcpy(dv->name, name);
440 dv->vendorpec = value;
441 dv->type = dv->length = 1; /* defaults */
443 if (!fr_hash_table_insert(vendors_byname, dv)) {
446 old_dv = fr_hash_table_finddata(vendors_byname, dv);
448 fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
451 if (old_dv->vendorpec != dv->vendorpec) {
452 fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
457 * Already inserted. Discard the duplicate entry.
464 * Insert the SAME pointer (not free'd when this table is
465 * deleted), into another table.
467 * We want this behaviour because we want OLD names for
468 * the attributes to be read from the configuration
469 * files, but when we're printing them, (and looking up
470 * by value) we want to use the NEW name.
472 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
473 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
482 * Add an attribute to the dictionary.
484 int dict_addattr(const char *name, int vendor, int type, int value,
488 static int max_attr = 0;
491 namelen = strlen(name);
492 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
493 fr_strerror_printf("dict_addattr: attribute name too long");
498 * If the value is '-1', that means use a pre-existing
499 * one (if it already exists). If one does NOT already exist,
500 * then create a new attribute, with a non-conflicting value,
504 if (dict_attrbyname(name)) {
505 return 0; /* exists, don't add it again */
510 } else if (vendor == 0) {
514 if (value > max_attr) {
520 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
524 if (value >= 65536) {
525 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
531 static DICT_VENDOR *last_vendor = NULL;
533 if (flags.is_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
534 fr_strerror_printf("Sub-TLV's cannot be encrypted");
538 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
539 fr_strerror_printf("TLV's cannot be encrypted");
543 if (flags.is_tlv && flags.has_tag) {
544 fr_strerror_printf("Sub-TLV's cannot have a tag");
548 if (flags.has_tlv && flags.has_tag) {
549 fr_strerror_printf("TLV's cannot have a tag");
554 * Most ATTRIBUTEs are bunched together by
555 * VENDOR. We can save a lot of lookups on
556 * dictionary initialization by caching the last
559 if (last_vendor && (vendor == last_vendor->vendorpec)) {
562 dv = dict_vendorbyvalue(vendor);
567 * If the vendor isn't defined, die.
570 fr_strerror_printf("dict_addattr: Unknown vendor");
575 * FIXME: Switch over dv->type, and limit things
578 if ((dv->type == 1) && (value >= 256) && !flags.is_tlv) {
579 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
581 } /* else 256..65535 are allowed */
585 * Create a new attribute for the list
587 if ((attr = fr_pool_alloc(sizeof(*attr) + namelen)) == NULL) {
588 fr_strerror_printf("dict_addattr: out of memory");
592 memcpy(attr->name, name, namelen);
593 attr->name[namelen] = '\0';
595 attr->attr |= (vendor << 16); /* FIXME: hack */
596 attr->vendor = vendor;
599 attr->vendor = vendor;
602 * Insert the attribute, only if it's not a duplicate.
604 if (!fr_hash_table_insert(attributes_byname, attr)) {
608 * If the attribute has identical number, then
609 * ignore the duplicate.
611 a = fr_hash_table_finddata(attributes_byname, attr);
612 if (a && (strcasecmp(a->name, attr->name) == 0)) {
613 if (a->attr != attr->attr) {
614 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
620 * Same name, same vendor, same attr,
621 * maybe the flags and/or type is
622 * different. Let the new value
623 * over-ride the old one.
628 fr_hash_table_delete(attributes_byvalue, a);
630 if (!fr_hash_table_replace(attributes_byname, attr)) {
631 fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
638 * Insert the SAME pointer (not free'd when this entry is
639 * deleted), into another table.
641 * We want this behaviour because we want OLD names for
642 * the attributes to be read from the configuration
643 * files, but when we're printing them, (and looking up
644 * by value) we want to use the NEW name.
646 if (!fr_hash_table_replace(attributes_byvalue, attr)) {
647 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
651 if (!vendor && (value > 0) && (value < 256)) {
652 dict_base_attrs[value] = attr;
660 * Add a value for an attribute to the dictionary.
662 int dict_addvalue(const char *namestr, const char *attrstr, int value)
668 static DICT_ATTR *last_attr = NULL;
671 fr_strerror_printf("dict_addvalue: empty names are not permitted");
675 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
676 fr_strerror_printf("dict_addvalue: value name too long");
680 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
681 fr_strerror_printf("dict_addvalue: out of memory");
684 memset(dval, 0, sizeof(*dval));
686 strcpy(dval->name, namestr);
690 * Most VALUEs are bunched together by ATTRIBUTE. We can
691 * save a lot of lookups on dictionary initialization by
692 * caching the last attribute.
694 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
697 dattr = dict_attrbyname(attrstr);
702 * Remember which attribute is associated with this
703 * value, if possible.
706 if (dattr->flags.has_value_alias) {
707 fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
711 dval->attr = dattr->attr;
714 * Enforce valid values
716 * Don't worry about fixups...
718 switch (dattr->type) {
722 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
729 fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
735 * Allow octets for now, because
736 * of dictionary.cablelabs
740 case PW_TYPE_INTEGER:
745 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
746 fr_int2str(type_table, dattr->type, "?Unknown?"));
750 dattr->flags.has_value = 1;
752 value_fixup_t *fixup;
754 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
757 fr_strerror_printf("dict_addvalue: out of memory");
760 memset(fixup, 0, sizeof(*fixup));
762 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
766 * Insert to the head of the list.
768 fixup->next = value_fixup;
775 * Add the value into the dictionary.
777 if (!fr_hash_table_insert(values_byname, dval)) {
782 * Suppress duplicates with the same
783 * name and value. There are lots in
786 old = dict_valbyname(dattr->attr, namestr);
787 if (old && (old->value == dval->value)) {
794 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
799 * There are multiple VALUE's, keyed by attribute, so we
800 * take care of that here.
802 if (!fr_hash_table_replace(values_byvalue, dval)) {
803 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
811 static int sscanf_i(const char *str, int *pvalue)
815 const char *tab = "0123456789";
817 if ((str[0] == '0') &&
818 ((str[1] == 'x') || (str[1] == 'X'))) {
819 tab = "0123456789abcdef";
828 c = memchr(tab, tolower((int) *str), base);
842 * Process the ATTRIBUTE command
844 static int process_attribute(const char* fn, const int line,
845 const int block_vendor, DICT_ATTR *block_tlv,
846 char **argv, int argc)
853 if ((argc < 3) || (argc > 4)) {
854 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
860 * Validate all entries
862 if (!sscanf_i(argv[1], &value)) {
863 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
868 * find the type of the attribute.
870 type = fr_str2int(type_table, argv[2], -1);
872 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
878 * Only look up the vendor if the string
881 memset(&flags, 0, sizeof(flags));
883 char *key, *next, *last;
887 next = strchr(key, ',');
888 if (next) *(next++) = '\0';
890 if (strcmp(key, "has_tag") == 0 ||
891 strcmp(key, "has_tag=1") == 0) {
892 /* Boolean flag, means this is a
896 } else if (strncmp(key, "encrypt=", 8) == 0) {
897 /* Encryption method, defaults to 0 (none).
898 Currently valid is just type 2,
899 Tunnel-Password style, which can only
900 be applied to strings. */
901 flags.encrypt = strtol(key + 8, &last, 0);
903 fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
908 } else if (strncmp(key, "array", 8) == 0) {
915 case PW_TYPE_INTEGER:
920 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
926 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
932 if (key && !*key) break;
936 if (block_vendor) vendor = block_vendor;
939 * Special checks for tags, they make our life much more
944 * Only string, octets, and integer can be tagged.
948 case PW_TYPE_INTEGER:
952 fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
954 fr_int2str(type_table, type, "?Unknown?"));
960 if (type == PW_TYPE_TLV) {
966 * TLV's can be only one octet.
968 if ((value <= 0) || (value > 255)) {
969 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv's cannot have value > 255",
974 if (flags.encrypt != FLAG_ENCRYPT_NONE) {
975 fr_strerror_printf( "dict_init: %s[%d]: sub-tlv's cannot be encrypted",
984 value |= (block_tlv->attr & 0xffff);
991 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
992 fr_strerror_printf("dict_init: %s[%d]: %s",
993 fn, line, fr_strerror());
1002 * Process the VALUE command
1004 static int process_value(const char* fn, const int line, char **argv,
1010 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1015 * For Compatibility, skip "Server-Config"
1017 if (strcasecmp(argv[0], "Server-Config") == 0)
1021 * Validate all entries
1023 if (!sscanf_i(argv[2], &value)) {
1024 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1029 if (dict_addvalue(argv[1], argv[0], value) < 0) {
1030 fr_strerror_printf("dict_init: %s[%d]: %s",
1031 fn, line, fr_strerror());
1040 * Process the VALUE-ALIAS command
1042 * This allows VALUE mappings to be shared among multiple
1045 static int process_value_alias(const char* fn, const int line, char **argv,
1048 DICT_ATTR *my_da, *da;
1052 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1057 my_da = dict_attrbyname(argv[0]);
1059 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1064 if (my_da->flags.has_value) {
1065 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1070 if (my_da->flags.has_value_alias) {
1071 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1076 da = dict_attrbyname(argv[1]);
1078 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1083 if (!da->flags.has_value) {
1084 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1089 if (da->flags.has_value_alias) {
1090 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1095 if (my_da->type != da->type) {
1096 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1101 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1102 fr_strerror_printf("dict_addvalue: out of memory");
1106 dval->name[0] = '\0'; /* empty name */
1107 dval->attr = my_da->attr;
1108 dval->value = da->attr;
1110 if (!fr_hash_table_insert(values_byname, dval)) {
1111 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1122 * Process the VENDOR command
1124 static int process_vendor(const char* fn, const int line, char **argv,
1128 int continuation = 0;
1129 const char *format = NULL;
1131 if ((argc < 2) || (argc > 3)) {
1132 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1138 * Validate all entries
1140 if (!isdigit((int) argv[1][0])) {
1141 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1145 value = atoi(argv[1]);
1147 /* Create a new VENDOR entry for the list */
1148 if (dict_addvendor(argv[0], value) < 0) {
1149 fr_strerror_printf("dict_init: %s[%d]: %s",
1150 fn, line, fr_strerror());
1155 * Look for a format statement
1160 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1161 format = "format=4,0";
1163 } else if (value == VENDORPEC_LUCENT) {
1164 format = "format=2,1";
1166 } else if (value == VENDORPEC_STARENT) {
1167 format = "format=2,2";
1169 } /* else no fixups to do */
1176 if (strncasecmp(format, "format=", 7) != 0) {
1177 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1183 if ((strlen(p) < 3) ||
1184 !isdigit((int) p[0]) ||
1186 !isdigit((int) p[2]) ||
1187 (p[3] && (p[3] != ','))) {
1188 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1193 type = (int) (p[0] - '0');
1194 length = (int) (p[2] - '0');
1197 if ((p[4] != 'c') ||
1199 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1206 dv = dict_vendorbyvalue(value);
1208 fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1213 if ((type != 1) && (type != 2) && (type != 4)) {
1214 fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1219 if ((length != 0) && (length != 1) && (length != 2)) {
1220 fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1226 dv->length = length;
1227 dv->flags = continuation;
1234 * String split routine. Splits an input string IN PLACE
1235 * into pieces, based on spaces.
1237 static int str2argv(char *str, char **argv, int max_argc)
1242 if (argc >= max_argc) return argc;
1245 * Chop out comments early.
1252 while ((*str == ' ') ||
1255 (*str == '\n')) *(str++) = '\0';
1257 if (!*str) return argc;
1266 (*str != '\n')) str++;
1272 #define MAX_ARGV (16)
1275 * Initialize the dictionary.
1277 static int my_dict_init(const char *dir, const char *fn,
1278 const char *src_file, int src_line)
1287 struct stat statbuf;
1288 char *argv[MAX_ARGV];
1290 DICT_ATTR *da, *block_tlv = NULL;
1292 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1293 strlen(dir) >= sizeof(dirtmp) / 2) {
1294 fr_strerror_printf("dict_init: filename name too long");
1299 * First see if fn is relative to dir. If so, create
1300 * new filename. If not, remember the absolute dir.
1302 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1306 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1307 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1311 if ((fp = fopen(fn, "r")) == NULL) {
1313 fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1314 fn, strerror(errno));
1316 fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1317 src_file, src_line, fn, strerror(errno));
1322 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1323 if (!S_ISREG(statbuf.st_mode)) {
1325 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1331 * Globally writable dictionaries means that users can control
1332 * the server configuration with little difficulty.
1335 if ((statbuf.st_mode & S_IWOTH) != 0) {
1337 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1343 dict_stat_add(fn, &statbuf);
1346 * Seed the random pool with data.
1348 fr_rand_seed(&statbuf, sizeof(statbuf));
1352 while (fgets(buf, sizeof(buf), fp) != NULL) {
1354 if (buf[0] == '#' || buf[0] == 0 ||
1355 buf[0] == '\n' || buf[0] == '\r')
1359 * Comment characters should NOT be appearing anywhere but
1360 * as start of a comment;
1362 p = strchr(buf, '#');
1365 argc = str2argv(buf, argv, MAX_ARGV);
1366 if (argc == 0) continue;
1369 fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1376 * Process VALUE lines.
1378 if (strcasecmp(argv[0], "VALUE") == 0) {
1379 if (process_value(fn, line,
1380 argv + 1, argc - 1) == -1) {
1388 * Perhaps this is an attribute.
1390 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1391 if (process_attribute(fn, line, block_vendor,
1393 argv + 1, argc - 1) == -1) {
1401 * See if we need to import another dictionary.
1403 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1404 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1411 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1412 if (process_value_alias(fn, line,
1413 argv + 1, argc - 1) == -1) {
1421 * Process VENDOR lines.
1423 if (strcasecmp(argv[0], "VENDOR") == 0) {
1424 if (process_vendor(fn, line,
1425 argv + 1, argc - 1) == -1) {
1432 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1435 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1441 da = dict_attrbyname(argv[1]);
1444 "dict_init: %s[%d]: unknown attribute %s",
1450 if (da->type != PW_TYPE_TLV) {
1452 "dict_init: %s[%d]: attribute %s is not of type tlv",
1462 if (strcasecmp(argv[0], "END-TLV") == 0) {
1465 "dict_init: %s[%d] invalid END-TLV entry",
1471 da = dict_attrbyname(argv[1]);
1474 "dict_init: %s[%d]: unknown attribute %s",
1480 if (da != block_tlv) {
1482 "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1491 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1494 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1500 vendor = dict_vendorbyname(argv[1]);
1503 "dict_init: %s[%d]: unknown vendor %s",
1508 block_vendor = vendor;
1510 } /* BEGIN-VENDOR */
1512 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1515 "dict_init: %s[%d] invalid END-VENDOR entry",
1521 vendor = dict_vendorbyname(argv[1]);
1524 "dict_init: %s[%d]: unknown vendor %s",
1530 if (vendor != block_vendor) {
1532 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1542 * Any other string: We don't recognize it.
1544 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1555 * Empty callback for hash table initialization.
1557 static int null_callback(void *ctx, void *data)
1559 ctx = ctx; /* -Wunused */
1560 data = data; /* -Wunused */
1567 * Initialize the directory, then fix the attr member of
1570 int dict_init(const char *dir, const char *fn)
1573 * Check if we need to change anything. If not, don't do
1576 if (dict_stat_check(dir, fn)) {
1581 * Free the dictionaries, and the stat cache.
1584 stat_root_dir = strdup(dir);
1585 stat_root_file = strdup(fn);
1588 * Create the table of vendor by name. There MAY NOT
1589 * be multiple vendors of the same name.
1591 * Each vendor is malloc'd, so the free function is free.
1593 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1594 dict_vendor_name_cmp,
1596 if (!vendors_byname) {
1601 * Create the table of vendors by value. There MAY
1602 * be vendors of the same value. If there are, we
1603 * pick the latest one.
1605 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1606 dict_vendor_value_cmp,
1608 if (!vendors_byvalue) {
1613 * Create the table of attributes by name. There MAY NOT
1614 * be multiple attributes of the same name.
1616 * Each attribute is malloc'd, so the free function is free.
1618 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1621 if (!attributes_byname) {
1626 * Create the table of attributes by value. There MAY
1627 * be attributes of the same value. If there are, we
1628 * pick the latest one.
1630 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1631 dict_attr_value_cmp,
1633 if (!attributes_byvalue) {
1637 values_byname = fr_hash_table_create(dict_value_name_hash,
1638 dict_value_name_cmp,
1640 if (!values_byname) {
1644 values_byvalue = fr_hash_table_create(dict_value_value_hash,
1645 dict_value_value_cmp,
1647 if (!values_byvalue) {
1651 value_fixup = NULL; /* just to be safe. */
1653 if (my_dict_init(dir, fn, NULL, 0) < 0)
1658 value_fixup_t *this, *next;
1660 for (this = value_fixup; this != NULL; this = next) {
1663 a = dict_attrbyname(this->attrstr);
1666 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1667 this->attrstr, this->dval->name);
1668 return -1; /* leak, but they should die... */
1671 this->dval->attr = a->attr;
1674 * Add the value into the dictionary.
1676 if (!fr_hash_table_replace(values_byname,
1678 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1683 * Allow them to use the old name, but
1684 * prefer the new name when printing
1687 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
1688 fr_hash_table_replace(values_byvalue,
1694 * Just so we don't lose track of things.
1701 * Walk over all of the hash tables to ensure they're
1702 * initialized. We do this because the threads may perform
1703 * lookups, and we don't want multi-threaded re-ordering
1704 * of the table entries. That would be bad.
1706 fr_hash_table_walk(vendors_byname, null_callback, NULL);
1707 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
1709 fr_hash_table_walk(attributes_byname, null_callback, NULL);
1710 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
1712 fr_hash_table_walk(values_byvalue, null_callback, NULL);
1713 fr_hash_table_walk(values_byname, null_callback, NULL);
1719 * Get an attribute by its numerical value.
1721 DICT_ATTR *dict_attrbyvalue(unsigned int attr)
1725 if ((attr > 0) && (attr < 256)) return dict_base_attrs[attr];
1728 dattr.vendor = VENDOR(attr) & 0x7fff;
1730 return fr_hash_table_finddata(attributes_byvalue, &dattr);
1734 * Get an attribute by its name.
1736 DICT_ATTR *dict_attrbyname(const char *name)
1739 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
1741 if (!name) return NULL;
1743 da = (DICT_ATTR *) buffer;
1744 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
1746 return fr_hash_table_finddata(attributes_byname, da);
1750 * Associate a value with an attribute and return it.
1752 DICT_VALUE *dict_valbyattr(unsigned int attr, int value)
1754 DICT_VALUE dval, *dv;
1757 * First, look up aliases.
1760 dval.name[0] = '\0';
1763 * Look up the attribute alias target, and use
1764 * the correct attribute number if found.
1766 dv = fr_hash_table_finddata(values_byname, &dval);
1767 if (dv) dval.attr = dv->value;
1771 return fr_hash_table_finddata(values_byvalue, &dval);
1775 * Get a value by its name, keyed off of an attribute.
1777 DICT_VALUE *dict_valbyname(unsigned int attr, const char *name)
1779 DICT_VALUE *my_dv, *dv;
1780 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1782 if (!name) return NULL;
1784 my_dv = (DICT_VALUE *) buffer;
1786 my_dv->name[0] = '\0';
1789 * Look up the attribute alias target, and use
1790 * the correct attribute number if found.
1792 dv = fr_hash_table_finddata(values_byname, my_dv);
1793 if (dv) my_dv->attr = dv->value;
1795 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
1797 return fr_hash_table_finddata(values_byname, my_dv);
1801 * Get the vendor PEC based on the vendor name
1803 * This is efficient only for small numbers of vendors.
1805 int dict_vendorbyname(const char *name)
1808 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1810 if (!name) return 0;
1812 dv = (DICT_VENDOR *) buffer;
1813 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
1815 dv = fr_hash_table_finddata(vendors_byname, dv);
1818 return dv->vendorpec;
1822 * Return the vendor struct based on the PEC.
1824 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1828 dv.vendorpec = vendorpec;
1830 return fr_hash_table_finddata(vendors_byvalue, &dv);