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)
41 static lrad_hash_table_t *vendors_byname = NULL;
42 static lrad_hash_table_t *vendors_byvalue = NULL;
44 static lrad_hash_table_t *attributes_byname = NULL;
45 static lrad_hash_table_t *attributes_byvalue = NULL;
47 static lrad_hash_table_t *values_byvalue = NULL;
48 static lrad_hash_table_t *values_byname = NULL;
50 static DICT_ATTR *dict_base_attrs[256];
53 * For faster HUP's, we cache the stat information for
54 * files we've $INCLUDEd
56 typedef struct dict_stat_t {
57 struct dict_stat_t *next;
62 static char *stat_root_dir = NULL;
63 static char *stat_root_file = NULL;
65 static dict_stat_t *stat_head = NULL;
66 static dict_stat_t *stat_tail = NULL;
68 typedef struct value_fixup_t {
71 struct value_fixup_t *next;
76 * So VALUEs in the dictionary can have forward references.
78 static value_fixup_t *value_fixup = NULL;
80 static const LRAD_NAME_NUMBER type_table[] = {
81 { "integer", PW_TYPE_INTEGER },
82 { "string", PW_TYPE_STRING },
83 { "ipaddr", PW_TYPE_IPADDR },
84 { "date", PW_TYPE_DATE },
85 { "abinary", PW_TYPE_ABINARY },
86 { "octets", PW_TYPE_OCTETS },
87 { "ifid", PW_TYPE_IFID },
88 { "ipv6addr", PW_TYPE_IPV6ADDR },
89 { "ipv6prefix", PW_TYPE_IPV6PREFIX },
90 { "byte", PW_TYPE_BYTE },
91 { "short", PW_TYPE_SHORT },
92 { "ether", PW_TYPE_ETHERNET },
98 * Create the hash of the name.
100 * We copy the hash function here because it's substantially faster.
102 #define FNV_MAGIC_INIT (0x811c9dc5)
103 #define FNV_MAGIC_PRIME (0x01000193)
105 static uint32_t dict_hashname(const char *name)
107 uint32_t hash = FNV_MAGIC_INIT;
110 for (p = name; *p != '\0'; p++) {
111 int c = *(const unsigned char *) p;
112 if (isalpha(c)) c = tolower(c);
114 hash *= FNV_MAGIC_PRIME;
115 hash ^= (uint32_t ) (c & 0xff);
123 * Hash callback functions.
125 static uint32_t dict_attr_name_hash(const void *data)
127 return dict_hashname(((const DICT_ATTR *)data)->name);
130 static int dict_attr_name_cmp(const void *one, const void *two)
132 const DICT_ATTR *a = one;
133 const DICT_ATTR *b = two;
135 return strcasecmp(a->name, b->name);
138 static uint32_t dict_attr_value_hash(const void *data)
141 const DICT_ATTR *attr = data;
143 hash = lrad_hash(&attr->vendor, sizeof(attr->vendor));
144 return lrad_hash_update(&attr->attr, sizeof(attr->attr), hash);
147 static int dict_attr_value_cmp(const void *one, const void *two)
149 const DICT_ATTR *a = one;
150 const DICT_ATTR *b = two;
152 if (a->vendor < b->vendor) return -1;
153 if (a->vendor > b->vendor) return +1;
155 return a->attr - b->attr;
158 static uint32_t dict_vendor_name_hash(const void *data)
160 return dict_hashname(((const DICT_VENDOR *)data)->name);
163 static int dict_vendor_name_cmp(const void *one, const void *two)
165 const DICT_VENDOR *a = one;
166 const DICT_VENDOR *b = two;
168 return strcasecmp(a->name, b->name);
171 static uint32_t dict_vendor_value_hash(const void *data)
173 return lrad_hash(&(((const DICT_VENDOR *)data)->vendorpec),
174 sizeof(((const DICT_VENDOR *)data)->vendorpec));
177 static int dict_vendor_value_cmp(const void *one, const void *two)
179 const DICT_VENDOR *a = one;
180 const DICT_VENDOR *b = two;
182 return a->vendorpec - b->vendorpec;
185 static uint32_t dict_value_name_hash(const void *data)
188 const DICT_VALUE *dval = data;
190 hash = dict_hashname(dval->name);
191 return lrad_hash_update(&dval->attr, sizeof(dval->attr), hash);
194 static int dict_value_name_cmp(const void *one, const void *two)
197 const DICT_VALUE *a = one;
198 const DICT_VALUE *b = two;
200 rcode = a->attr - b->attr;
201 if (rcode != 0) return rcode;
203 return strcasecmp(a->name, b->name);
206 static uint32_t dict_value_value_hash(const void *data)
209 const DICT_VALUE *dval = data;
211 hash = lrad_hash(&dval->attr, sizeof(dval->attr));
212 return lrad_hash_update(&dval->value, sizeof(dval->value), hash);
215 static int dict_value_value_cmp(const void *one, const void *two)
218 const DICT_VALUE *a = one;
219 const DICT_VALUE *b = two;
221 rcode = a->attr - b->attr;
222 if (rcode != 0) return rcode;
224 return a->value - b->value;
229 * Free the list of stat buffers
231 static void dict_stat_free(void)
233 dict_stat_t *this, *next;
236 stat_root_dir = NULL;
237 free(stat_root_file);
238 stat_root_file = NULL;
245 for (this = stat_head; this != NULL; this = next) {
251 stat_head = stat_tail = NULL;
256 * Add an entry to the list of stat buffers.
258 static void dict_stat_add(const char *name, const struct stat *stat_buf)
262 this = malloc(sizeof(*this));
264 memset(this, 0, sizeof(*this));
266 this->name = strdup(name);
267 this->mtime = stat_buf->st_mtime;
270 stat_head = stat_tail = this;
272 stat_tail->next = this;
279 * See if any dictionaries have changed. If not, don't
282 static int dict_stat_check(const char *root_dir, const char *root_file)
287 if (!stat_root_dir) return 0;
288 if (!stat_root_file) return 0;
290 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
291 if (strcmp(root_file, stat_root_file) != 0) return 0;
293 if (!stat_head) return 0; /* changed, reload */
295 for (this = stat_head; this != NULL; this = this->next) {
296 if (stat(this->name, &buf) < 0) return 0;
298 if (buf.st_mtime != this->mtime) return 0;
304 typedef struct fr_pool_t {
307 struct fr_pool_t *page_free;
308 struct fr_pool_t *page_next;
311 #define FR_POOL_SIZE (32768)
312 #define FR_ALLOC_ALIGN (8)
314 static fr_pool_t *dict_pool = NULL;
316 static fr_pool_t *fr_pool_create(void)
318 fr_pool_t *fp = malloc(FR_POOL_SIZE);
320 if (!fp) return NULL;
322 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
323 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
325 fp->page_next = NULL;
329 static void fr_pool_delete(fr_pool_t **pfp)
331 fr_pool_t *fp, *next;
333 if (!pfp || !*pfp) return;
335 for (fp = *pfp; fp != NULL; fp = next) {
336 next = fp->page_next;
342 static void *fr_pool_alloc(size_t size)
346 if (size == 0) return NULL;
348 if (size > 256) return NULL; /* shouldn't happen */
351 dict_pool = fr_pool_create();
352 if (!dict_pool) return NULL;
355 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
356 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
359 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
360 dict_pool->page_free->page_next = fr_pool_create();
361 if (!dict_pool->page_free->page_next) return NULL;
362 dict_pool->page_free = dict_pool->page_free->page_next;
365 ptr = dict_pool->page_free->free_ptr;
366 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
372 static void fr_pool_free(UNUSED void *ptr)
375 * Place-holder for later code.
380 * Free the dictionary_attributes and dictionary_values lists.
387 lrad_hash_table_free(vendors_byname);
388 lrad_hash_table_free(vendors_byvalue);
389 vendors_byname = NULL;
390 vendors_byvalue = NULL;
392 lrad_hash_table_free(attributes_byname);
393 lrad_hash_table_free(attributes_byvalue);
394 attributes_byname = NULL;
395 attributes_byvalue = NULL;
397 lrad_hash_table_free(values_byname);
398 lrad_hash_table_free(values_byvalue);
399 values_byname = NULL;
400 values_byvalue = NULL;
402 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
404 fr_pool_delete(&dict_pool);
411 * Add vendor to the list.
413 int dict_addvendor(const char *name, int value)
418 if (value >= 32767) {
419 librad_log("dict_addvendor: Cannot handle vendor ID larger than 65535");
423 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
424 librad_log("dict_addvendor: vendor name too long");
428 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
429 librad_log("dict_addvendor: out of memory");
433 strcpy(dv->name, name);
434 dv->vendorpec = value;
435 dv->type = dv->length = 1; /* defaults */
437 if (!lrad_hash_table_insert(vendors_byname, dv)) {
440 old_dv = lrad_hash_table_finddata(vendors_byname, dv);
442 librad_log("dict_addvendor: Failed inserting vendor name %s", name);
445 if (old_dv->vendorpec != dv->vendorpec) {
446 librad_log("dict_addvendor: Duplicate vendor name %s", name);
451 * Already inserted. Discard the duplicate entry.
458 * Insert the SAME pointer (not free'd when this table is
459 * deleted), into another table.
461 * We want this behaviour because we want OLD names for
462 * the attributes to be read from the configuration
463 * files, but when we're printing them, (and looking up
464 * by value) we want to use the NEW name.
466 if (!lrad_hash_table_replace(vendors_byvalue, dv)) {
467 librad_log("dict_addvendor: Failed inserting vendor %s",
476 * Add an attribute to the dictionary.
478 int dict_addattr(const char *name, int vendor, int type, int value,
481 static int max_attr = 0;
484 if (strlen(name) > (sizeof(attr->name) -1)) {
485 librad_log("dict_addattr: attribute name too long");
490 * If the value is '-1', that means use a pre-existing
491 * one (if it already exists). If one does NOT already exist,
492 * then create a new attribute, with a non-conflicting value,
496 if (dict_attrbyname(name)) {
497 return 0; /* exists, don't add it again */
502 } else if (vendor == 0) {
506 if (value > max_attr) {
512 librad_log("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
516 if (value >= 65536) {
517 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
523 static DICT_VENDOR *last_vendor = NULL;
526 * Most ATTRIBUTEs are bunched together by
527 * VENDOR. We can save a lot of lookups on
528 * dictionary initialization by caching the last
531 if (last_vendor && (vendor == last_vendor->vendorpec)) {
534 dv = dict_vendorbyvalue(vendor);
539 * If the vendor isn't defined, die.
542 librad_log("dict_addattr: Unknown vendor");
547 * FIXME: Switch over dv->type, and limit things
550 if ((dv->type == 1) && (value >= 256)) {
551 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
553 } /* else 256..65535 are allowed */
557 * Create a new attribute for the list
559 if ((attr = fr_pool_alloc(sizeof(*attr))) == NULL) {
560 librad_log("dict_addattr: out of memory");
564 strcpy(attr->name, name);
566 attr->attr |= (vendor << 16); /* FIXME: hack */
567 attr->vendor = vendor;
570 attr->vendor = vendor;
573 * Insert the attribute, only if it's not a duplicate.
575 if (!lrad_hash_table_insert(attributes_byname, attr)) {
579 * If the attribute has identical number, then
580 * ignore the duplicate.
582 a = lrad_hash_table_finddata(attributes_byname, attr);
583 if (a && (strcasecmp(a->name, attr->name) == 0)) {
584 if (a->attr != attr->attr) {
585 librad_log("dict_addattr: Duplicate attribute name %s", name);
591 * Same name, same vendor, same attr,
592 * maybe the flags and/or type is
593 * different. Let the new value
594 * over-ride the old one.
599 lrad_hash_table_delete(attributes_byvalue, a);
601 if (!lrad_hash_table_replace(attributes_byname, attr)) {
602 librad_log("dict_addattr: Internal error storing attribute %s", name);
609 * Insert the SAME pointer (not free'd when this entry is
610 * deleted), into another table.
612 * We want this behaviour because we want OLD names for
613 * the attributes to be read from the configuration
614 * files, but when we're printing them, (and looking up
615 * by value) we want to use the NEW name.
617 if (!lrad_hash_table_replace(attributes_byvalue, attr)) {
618 librad_log("dict_addattr: Failed inserting attribute name %s", name);
622 if (!vendor && (value > 0) && (value < 256)) {
623 dict_base_attrs[value] = attr;
631 * Add a value for an attribute to the dictionary.
633 int dict_addvalue(const char *namestr, const char *attrstr, int value)
639 static DICT_ATTR *last_attr = NULL;
642 librad_log("dict_addvalue: empty names are not permitted");
646 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
647 librad_log("dict_addvalue: value name too long");
651 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
652 librad_log("dict_addvalue: out of memory");
655 memset(dval, 0, sizeof(*dval));
657 strcpy(dval->name, namestr);
661 * Most VALUEs are bunched together by ATTRIBUTE. We can
662 * save a lot of lookups on dictionary initialization by
663 * caching the last attribute.
665 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
668 dattr = dict_attrbyname(attrstr);
673 * Remember which attribute is associated with this
674 * value, if possible.
677 if (dattr->flags.has_value_alias) {
678 librad_log("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
682 dval->attr = dattr->attr;
685 * Enforce valid values
687 * Don't worry about fixups...
689 switch (dattr->type) {
693 librad_log("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
700 librad_log("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
706 * Allow octets for now, because
707 * of dictionary.cablelabs
711 case PW_TYPE_INTEGER:
716 librad_log("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
717 lrad_int2str(type_table, dattr->type, "?Unknown?"));
721 dattr->flags.has_value = 1;
723 value_fixup_t *fixup;
725 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
728 librad_log("dict_addvalue: out of memory");
731 memset(fixup, 0, sizeof(*fixup));
733 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
737 * Insert to the head of the list.
739 fixup->next = value_fixup;
746 * Add the value into the dictionary.
748 if (!lrad_hash_table_insert(values_byname, dval)) {
753 * Suppress duplicates with the same
754 * name and value. There are lots in
757 old = dict_valbyname(dattr->attr, namestr);
758 if (old && (old->value == dval->value)) {
765 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
770 * There are multiple VALUE's, keyed by attribute, so we
771 * take care of that here.
773 if (!lrad_hash_table_replace(values_byvalue, dval)) {
774 librad_log("dict_addvalue: Failed inserting value %s",
782 static int sscanf_i(const char *str, int *pvalue)
786 const char *tab = "0123456789";
788 if ((str[0] == '0') &&
789 ((str[1] == 'x') || (str[1] == 'X'))) {
790 tab = "0123456789abcdef";
799 c = memchr(tab, tolower((int) *str), base);
813 * Process the ATTRIBUTE command
815 static int process_attribute(const char* fn, const int line,
816 const int block_vendor, char **argv,
824 if ((argc < 3) || (argc > 4)) {
825 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
831 * Validate all entries
833 if (!sscanf_i(argv[1], &value)) {
834 librad_log("dict_init: %s[%d]: invalid value", fn, line);
839 * find the type of the attribute.
841 type = lrad_str2int(type_table, argv[2], -1);
843 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
849 * Only look up the vendor if the string
852 memset(&flags, 0, sizeof(flags));
854 char *key, *next, *last;
858 next = strchr(key, ',');
859 if (next) *(next++) = '\0';
861 if (strcmp(key, "has_tag") == 0 ||
862 strcmp(key, "has_tag=1") == 0) {
863 /* Boolean flag, means this is a
867 } else if (strncmp(key, "encrypt=", 8) == 0) {
868 /* Encryption method, defaults to 0 (none).
869 Currently valid is just type 2,
870 Tunnel-Password style, which can only
871 be applied to strings. */
872 flags.encrypt = strtol(key + 8, &last, 0);
874 librad_log( "dict_init: %s[%d] invalid option %s",
879 } else if (strncmp(key, "array", 8) == 0) {
886 case PW_TYPE_INTEGER:
891 librad_log( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
896 } else if (block_vendor) {
897 librad_log( "dict_init: %s[%d]: unknown option \"%s\"",
902 /* Must be a vendor 'flag'... */
903 if (strncmp(key, "vendor=", 7) == 0) {
908 vendor = dict_vendorbyname(key);
910 librad_log( "dict_init: %s[%d]: unknown vendor \"%s\"",
914 if (block_vendor && argv[3][0] &&
915 (block_vendor != vendor)) {
916 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
923 if (key && !*key) break;
927 if (block_vendor) vendor = block_vendor;
930 * Special checks for tags, they make our life much more
935 * Only string, octets, and integer can be tagged.
939 case PW_TYPE_INTEGER:
943 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
945 lrad_int2str(type_table, type, "?Unknown?"));
954 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
955 librad_log("dict_init: %s[%d]: %s",
956 fn, line, librad_errstr);
965 * Process the VALUE command
967 static int process_value(const char* fn, const int line, char **argv,
973 librad_log("dict_init: %s[%d]: invalid VALUE line",
978 * For Compatibility, skip "Server-Config"
980 if (strcasecmp(argv[0], "Server-Config") == 0)
984 * Validate all entries
986 if (!sscanf_i(argv[2], &value)) {
987 librad_log("dict_init: %s[%d]: invalid value",
992 if (dict_addvalue(argv[1], argv[0], value) < 0) {
993 librad_log("dict_init: %s[%d]: %s",
994 fn, line, librad_errstr);
1003 * Process the VALUE-ALIAS command
1005 * This allows VALUE mappings to be shared among multiple
1008 static int process_value_alias(const char* fn, const int line, char **argv,
1011 DICT_ATTR *my_da, *da;
1015 librad_log("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1020 my_da = dict_attrbyname(argv[0]);
1022 librad_log("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1027 if (my_da->flags.has_value) {
1028 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1033 if (my_da->flags.has_value_alias) {
1034 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1039 da = dict_attrbyname(argv[1]);
1041 librad_log("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1046 if (!da->flags.has_value) {
1047 librad_log("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1052 if (da->flags.has_value_alias) {
1053 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1058 if (my_da->type != da->type) {
1059 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1064 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1065 librad_log("dict_addvalue: out of memory");
1068 memset(dval, 0, sizeof(*dval));
1070 dval->name[0] = '\0'; /* empty name */
1071 dval->attr = my_da->attr;
1072 dval->value = da->attr;
1074 if (!lrad_hash_table_insert(values_byname, dval)) {
1075 librad_log("dict_init: %s[%d]: Error create alias",
1086 * Process the VENDOR command
1088 static int process_vendor(const char* fn, const int line, char **argv,
1092 const char *format = NULL;
1094 if ((argc < 2) || (argc > 3)) {
1095 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
1101 * Validate all entries
1103 if (!isdigit((int) argv[1][0])) {
1104 librad_log("dict_init: %s[%d]: invalid value",
1108 value = atoi(argv[1]);
1110 /* Create a new VENDOR entry for the list */
1111 if (dict_addvendor(argv[0], value) < 0) {
1112 librad_log("dict_init: %s[%d]: %s",
1113 fn, line, librad_errstr);
1118 * Look for a format statement
1123 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1124 format = "format=4,0";
1126 } else if (value == VENDORPEC_LUCENT) {
1127 format = "format=2,1";
1129 } else if (value == VENDORPEC_STARENT) {
1130 format = "format=2,2";
1132 } /* else no fixups to do */
1139 if (strncasecmp(format, "format=", 7) != 0) {
1140 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1146 if ((strlen(p) != 3) ||
1147 !isdigit((int) p[0]) ||
1149 !isdigit((int) p[2])) {
1150 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1155 type = (int) (p[0] - '0');
1156 length = (int) (p[2] - '0');
1158 dv = dict_vendorbyvalue(value);
1160 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
1165 if ((type != 1) && (type != 2) && (type != 4)) {
1166 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
1171 if ((length != 0) && (length != 1) && (length != 2)) {
1172 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
1178 dv->length = length;
1185 * String split routine. Splits an input string IN PLACE
1186 * into pieces, based on spaces.
1188 static int str2argv(char *str, char **argv, int max_argc)
1193 if (argc >= max_argc) return argc;
1196 * Chop out comments early.
1203 while ((*str == ' ') ||
1206 (*str == '\n')) *(str++) = '\0';
1208 if (!*str) return argc;
1217 (*str != '\n')) str++;
1223 #define MAX_ARGV (16)
1226 * Initialize the dictionary.
1228 static int my_dict_init(const char *dir, const char *fn,
1229 const char *src_file, int src_line)
1238 struct stat statbuf;
1239 char *argv[MAX_ARGV];
1242 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1243 strlen(dir) >= sizeof(dirtmp) / 2) {
1244 librad_log("dict_init: filename name too long");
1249 * First see if fn is relative to dir. If so, create
1250 * new filename. If not, remember the absolute dir.
1252 if ((p = strrchr(fn, '/')) != NULL) {
1256 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1257 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1261 if ((fp = fopen(fn, "r")) == NULL) {
1263 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
1264 fn, strerror(errno));
1266 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1267 src_file, src_line, fn, strerror(errno));
1272 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1273 if (!S_ISREG(statbuf.st_mode)) {
1275 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
1281 * Globally writable dictionaries means that users can control
1282 * the server configuration with little difficulty.
1285 if ((statbuf.st_mode & S_IWOTH) != 0) {
1287 librad_log("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1293 dict_stat_add(fn, &statbuf);
1296 * Seed the random pool with data.
1298 lrad_rand_seed(&statbuf, sizeof(statbuf));
1302 while (fgets(buf, sizeof(buf), fp) != NULL) {
1304 if (buf[0] == '#' || buf[0] == 0 ||
1305 buf[0] == '\n' || buf[0] == '\r')
1309 * Comment characters should NOT be appearing anywhere but
1310 * as start of a comment;
1312 p = strchr(buf, '#');
1315 argc = str2argv(buf, argv, MAX_ARGV);
1316 if (argc == 0) continue;
1319 librad_log( "dict_init: %s[%d] invalid entry",
1326 * Process VALUE lines.
1328 if (strcasecmp(argv[0], "VALUE") == 0) {
1329 if (process_value(fn, line,
1330 argv + 1, argc - 1) == -1) {
1338 * Perhaps this is an attribute.
1340 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1341 if (process_attribute(fn, line, block_vendor,
1342 argv + 1, argc - 1) == -1) {
1350 * See if we need to import another dictionary.
1352 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1353 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1360 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1361 if (process_value_alias(fn, line,
1362 argv + 1, argc - 1) == -1) {
1370 * Process VENDOR lines.
1372 if (strcasecmp(argv[0], "VENDOR") == 0) {
1373 if (process_vendor(fn, line,
1374 argv + 1, argc - 1) == -1) {
1381 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1384 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1390 vendor = dict_vendorbyname(argv[1]);
1393 "dict_init: %s[%d]: unknown vendor %s",
1398 block_vendor = vendor;
1400 } /* BEGIN-VENDOR */
1402 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1405 "dict_init: %s[%d] invalid END-VENDOR entry",
1411 vendor = dict_vendorbyname(argv[1]);
1414 "dict_init: %s[%d]: unknown vendor %s",
1420 if (vendor != block_vendor) {
1422 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1432 * Any other string: We don't recognize it.
1434 librad_log("dict_init: %s[%d] invalid keyword \"%s\"",
1445 * Empty callback for hash table initialization.
1447 static int null_callback(void *ctx, void *data)
1449 ctx = ctx; /* -Wunused */
1450 data = data; /* -Wunused */
1457 * Initialize the directory, then fix the attr member of
1460 int dict_init(const char *dir, const char *fn)
1463 * Check if we need to change anything. If not, don't do
1466 if (dict_stat_check(dir, fn)) {
1471 * Free the dictionaries, and the stat cache.
1474 stat_root_dir = strdup(dir);
1475 stat_root_file = strdup(fn);
1478 * Create the table of vendor by name. There MAY NOT
1479 * be multiple vendors of the same name.
1481 * Each vendor is malloc'd, so the free function is free.
1483 vendors_byname = lrad_hash_table_create(dict_vendor_name_hash,
1484 dict_vendor_name_cmp,
1486 if (!vendors_byname) {
1491 * Create the table of vendors by value. There MAY
1492 * be vendors of the same value. If there are, we
1493 * pick the latest one.
1495 vendors_byvalue = lrad_hash_table_create(dict_vendor_value_hash,
1496 dict_vendor_value_cmp,
1498 if (!vendors_byvalue) {
1503 * Create the table of attributes by name. There MAY NOT
1504 * be multiple attributes of the same name.
1506 * Each attribute is malloc'd, so the free function is free.
1508 attributes_byname = lrad_hash_table_create(dict_attr_name_hash,
1511 if (!attributes_byname) {
1516 * Create the table of attributes by value. There MAY
1517 * be attributes of the same value. If there are, we
1518 * pick the latest one.
1520 attributes_byvalue = lrad_hash_table_create(dict_attr_value_hash,
1521 dict_attr_value_cmp,
1523 if (!attributes_byvalue) {
1527 values_byname = lrad_hash_table_create(dict_value_name_hash,
1528 dict_value_name_cmp,
1530 if (!values_byname) {
1534 values_byvalue = lrad_hash_table_create(dict_value_value_hash,
1535 dict_value_value_cmp,
1537 if (!values_byvalue) {
1541 value_fixup = NULL; /* just to be safe. */
1543 if (my_dict_init(dir, fn, NULL, 0) < 0)
1548 value_fixup_t *this, *next;
1550 for (this = value_fixup; this != NULL; this = next) {
1553 a = dict_attrbyname(this->attrstr);
1556 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1557 this->attrstr, this->dval->name);
1558 return -1; /* leak, but they should die... */
1561 this->dval->attr = a->attr;
1564 * Add the value into the dictionary.
1566 if (!lrad_hash_table_replace(values_byname,
1568 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1573 * Allow them to use the old name, but
1574 * prefer the new name when printing
1577 if (!lrad_hash_table_finddata(values_byvalue, this->dval)) {
1578 lrad_hash_table_replace(values_byvalue,
1584 * Just so we don't lose track of things.
1591 * Walk over all of the hash tables to ensure they're
1592 * initialized. We do this because the threads may perform
1593 * lookups, and we don't want multi-threaded re-ordering
1594 * of the table entries. That would be bad.
1596 lrad_hash_table_walk(vendors_byname, null_callback, NULL);
1597 lrad_hash_table_walk(vendors_byvalue, null_callback, NULL);
1599 lrad_hash_table_walk(attributes_byname, null_callback, NULL);
1600 lrad_hash_table_walk(attributes_byvalue, null_callback, NULL);
1602 lrad_hash_table_walk(values_byvalue, null_callback, NULL);
1603 lrad_hash_table_walk(values_byname, null_callback, NULL);
1609 * Get an attribute by its numerical value.
1611 DICT_ATTR *dict_attrbyvalue(int attr)
1615 if ((attr > 0) && (attr < 256)) return dict_base_attrs[attr];
1618 dattr.vendor = VENDOR(attr) & 0x7fff;
1620 return lrad_hash_table_finddata(attributes_byvalue, &dattr);
1624 * Get an attribute by its name.
1626 DICT_ATTR *dict_attrbyname(const char *name)
1630 if (!name) return NULL;
1632 strlcpy(dattr.name, name, sizeof(dattr.name));
1634 return lrad_hash_table_finddata(attributes_byname, &dattr);
1638 * Associate a value with an attribute and return it.
1640 DICT_VALUE *dict_valbyattr(int attr, int value)
1642 DICT_VALUE dval, *dv;
1645 * First, look up aliases.
1648 dval.name[0] = '\0';
1651 * Look up the attribute alias target, and use
1652 * the correct attribute number if found.
1654 dv = lrad_hash_table_finddata(values_byname, &dval);
1655 if (dv) dval.attr = dv->value;
1659 return lrad_hash_table_finddata(values_byvalue, &dval);
1663 * Get a value by its name, keyed off of an attribute.
1665 DICT_VALUE *dict_valbyname(int attr, const char *name)
1667 DICT_VALUE *my_dv, *dv;
1668 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1670 if (!name) return NULL;
1672 my_dv = (DICT_VALUE *) buffer;
1674 my_dv->name[0] = '\0';
1677 * Look up the attribute alias target, and use
1678 * the correct attribute number if found.
1680 dv = lrad_hash_table_finddata(values_byname, my_dv);
1681 if (dv) my_dv->attr = dv->value;
1683 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN);
1685 return lrad_hash_table_finddata(values_byname, my_dv);
1689 * Get the vendor PEC based on the vendor name
1691 * This is efficient only for small numbers of vendors.
1693 int dict_vendorbyname(const char *name)
1696 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1698 if (!name) return 0;
1700 dv = (DICT_VENDOR *) buffer;
1701 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN);
1703 dv = lrad_hash_table_finddata(vendors_byname, dv);
1706 return dv->vendorpec;
1710 * Return the vendor struct based on the PEC.
1712 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1716 dv.vendorpec = vendorpec;
1718 return lrad_hash_table_finddata(vendors_byvalue, &dv);