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 },
99 * Create the hash of the name.
101 * We copy the hash function here because it's substantially faster.
103 #define FNV_MAGIC_INIT (0x811c9dc5)
104 #define FNV_MAGIC_PRIME (0x01000193)
106 static uint32_t dict_hashname(const char *name)
108 uint32_t hash = FNV_MAGIC_INIT;
111 for (p = name; *p != '\0'; p++) {
112 int c = *(const unsigned char *) p;
113 if (isalpha(c)) c = tolower(c);
115 hash *= FNV_MAGIC_PRIME;
116 hash ^= (uint32_t ) (c & 0xff);
124 * Hash callback functions.
126 static uint32_t dict_attr_name_hash(const void *data)
128 return dict_hashname(((const DICT_ATTR *)data)->name);
131 static int dict_attr_name_cmp(const void *one, const void *two)
133 const DICT_ATTR *a = one;
134 const DICT_ATTR *b = two;
136 return strcasecmp(a->name, b->name);
139 static uint32_t dict_attr_value_hash(const void *data)
142 const DICT_ATTR *attr = data;
144 hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
145 return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
148 static int dict_attr_value_cmp(const void *one, const void *two)
150 const DICT_ATTR *a = one;
151 const DICT_ATTR *b = two;
153 if (a->vendor < b->vendor) return -1;
154 if (a->vendor > b->vendor) return +1;
156 return a->attr - b->attr;
159 static uint32_t dict_vendor_name_hash(const void *data)
161 return dict_hashname(((const DICT_VENDOR *)data)->name);
164 static int dict_vendor_name_cmp(const void *one, const void *two)
166 const DICT_VENDOR *a = one;
167 const DICT_VENDOR *b = two;
169 return strcasecmp(a->name, b->name);
172 static uint32_t dict_vendor_value_hash(const void *data)
174 return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
175 sizeof(((const DICT_VENDOR *)data)->vendorpec));
178 static int dict_vendor_value_cmp(const void *one, const void *two)
180 const DICT_VENDOR *a = one;
181 const DICT_VENDOR *b = two;
183 return a->vendorpec - b->vendorpec;
186 static uint32_t dict_value_name_hash(const void *data)
189 const DICT_VALUE *dval = data;
191 hash = dict_hashname(dval->name);
192 return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
195 static int dict_value_name_cmp(const void *one, const void *two)
198 const DICT_VALUE *a = one;
199 const DICT_VALUE *b = two;
201 rcode = a->attr - b->attr;
202 if (rcode != 0) return rcode;
204 return strcasecmp(a->name, b->name);
207 static uint32_t dict_value_value_hash(const void *data)
210 const DICT_VALUE *dval = data;
212 hash = fr_hash(&dval->attr, sizeof(dval->attr));
213 return fr_hash_update(&dval->value, sizeof(dval->value), hash);
216 static int dict_value_value_cmp(const void *one, const void *two)
219 const DICT_VALUE *a = one;
220 const DICT_VALUE *b = two;
222 rcode = a->attr - b->attr;
223 if (rcode != 0) return rcode;
225 return a->value - b->value;
230 * Free the list of stat buffers
232 static void dict_stat_free(void)
234 dict_stat_t *this, *next;
237 stat_root_dir = NULL;
238 free(stat_root_file);
239 stat_root_file = NULL;
246 for (this = stat_head; this != NULL; this = next) {
252 stat_head = stat_tail = NULL;
257 * Add an entry to the list of stat buffers.
259 static void dict_stat_add(const char *name, const struct stat *stat_buf)
263 this = malloc(sizeof(*this));
265 memset(this, 0, sizeof(*this));
267 this->name = strdup(name);
268 this->mtime = stat_buf->st_mtime;
271 stat_head = stat_tail = this;
273 stat_tail->next = this;
280 * See if any dictionaries have changed. If not, don't
283 static int dict_stat_check(const char *root_dir, const char *root_file)
288 if (!stat_root_dir) return 0;
289 if (!stat_root_file) return 0;
291 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
292 if (strcmp(root_file, stat_root_file) != 0) return 0;
294 if (!stat_head) return 0; /* changed, reload */
296 for (this = stat_head; this != NULL; this = this->next) {
297 if (stat(this->name, &buf) < 0) return 0;
299 if (buf.st_mtime != this->mtime) return 0;
305 typedef struct fr_pool_t {
308 struct fr_pool_t *page_free;
309 struct fr_pool_t *page_next;
312 #define FR_POOL_SIZE (32768)
313 #define FR_ALLOC_ALIGN (8)
315 static fr_pool_t *dict_pool = NULL;
317 static fr_pool_t *fr_pool_create(void)
319 fr_pool_t *fp = malloc(FR_POOL_SIZE);
321 if (!fp) return NULL;
323 fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
324 fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
326 fp->page_next = NULL;
330 static void fr_pool_delete(fr_pool_t **pfp)
332 fr_pool_t *fp, *next;
334 if (!pfp || !*pfp) return;
336 for (fp = *pfp; fp != NULL; fp = next) {
337 next = fp->page_next;
343 static void *fr_pool_alloc(size_t size)
347 if (size == 0) return NULL;
349 if (size > 256) return NULL; /* shouldn't happen */
352 dict_pool = fr_pool_create();
353 if (!dict_pool) return NULL;
356 if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
357 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
360 if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
361 dict_pool->page_free->page_next = fr_pool_create();
362 if (!dict_pool->page_free->page_next) return NULL;
363 dict_pool->page_free = dict_pool->page_free->page_next;
366 ptr = dict_pool->page_free->free_ptr;
367 dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
373 static void fr_pool_free(UNUSED void *ptr)
376 * Place-holder for later code.
381 * Free the dictionary_attributes and dictionary_values lists.
388 fr_hash_table_free(vendors_byname);
389 fr_hash_table_free(vendors_byvalue);
390 vendors_byname = NULL;
391 vendors_byvalue = NULL;
393 fr_hash_table_free(attributes_byname);
394 fr_hash_table_free(attributes_byvalue);
395 attributes_byname = NULL;
396 attributes_byvalue = NULL;
398 fr_hash_table_free(values_byname);
399 fr_hash_table_free(values_byvalue);
400 values_byname = NULL;
401 values_byvalue = NULL;
403 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
405 fr_pool_delete(&dict_pool);
412 * Add vendor to the list.
414 int dict_addvendor(const char *name, int value)
419 if (value >= 32767) {
420 librad_log("dict_addvendor: Cannot handle vendor ID larger than 65535");
424 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
425 librad_log("dict_addvendor: vendor name too long");
429 if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
430 librad_log("dict_addvendor: out of memory");
434 strcpy(dv->name, name);
435 dv->vendorpec = value;
436 dv->type = dv->length = 1; /* defaults */
438 if (!fr_hash_table_insert(vendors_byname, dv)) {
441 old_dv = fr_hash_table_finddata(vendors_byname, dv);
443 librad_log("dict_addvendor: Failed inserting vendor name %s", name);
446 if (old_dv->vendorpec != dv->vendorpec) {
447 librad_log("dict_addvendor: Duplicate vendor name %s", name);
452 * Already inserted. Discard the duplicate entry.
459 * Insert the SAME pointer (not free'd when this table is
460 * deleted), into another table.
462 * We want this behaviour because we want OLD names for
463 * the attributes to be read from the configuration
464 * files, but when we're printing them, (and looking up
465 * by value) we want to use the NEW name.
467 if (!fr_hash_table_replace(vendors_byvalue, dv)) {
468 librad_log("dict_addvendor: Failed inserting vendor %s",
477 * Add an attribute to the dictionary.
479 int dict_addattr(const char *name, int vendor, int type, int value,
483 static int max_attr = 0;
486 namelen = strlen(name);
487 if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
488 librad_log("dict_addattr: attribute name too long");
493 * If the value is '-1', that means use a pre-existing
494 * one (if it already exists). If one does NOT already exist,
495 * then create a new attribute, with a non-conflicting value,
499 if (dict_attrbyname(name)) {
500 return 0; /* exists, don't add it again */
505 } else if (vendor == 0) {
509 if (value > max_attr) {
515 librad_log("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
519 if (value >= 65536) {
520 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
526 static DICT_VENDOR *last_vendor = NULL;
529 * Most ATTRIBUTEs are bunched together by
530 * VENDOR. We can save a lot of lookups on
531 * dictionary initialization by caching the last
534 if (last_vendor && (vendor == last_vendor->vendorpec)) {
537 dv = dict_vendorbyvalue(vendor);
542 * If the vendor isn't defined, die.
545 librad_log("dict_addattr: Unknown vendor");
550 * FIXME: Switch over dv->type, and limit things
553 if ((dv->type == 1) && (value >= 256)) {
554 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
556 } /* else 256..65535 are allowed */
560 * Create a new attribute for the list
562 if ((attr = fr_pool_alloc(sizeof(*attr) + namelen)) == NULL) {
563 librad_log("dict_addattr: out of memory");
567 memcpy(attr->name, name, namelen);
568 attr->name[namelen] = '\0';
570 attr->attr |= (vendor << 16); /* FIXME: hack */
571 attr->vendor = vendor;
574 attr->vendor = vendor;
577 * Insert the attribute, only if it's not a duplicate.
579 if (!fr_hash_table_insert(attributes_byname, attr)) {
583 * If the attribute has identical number, then
584 * ignore the duplicate.
586 a = fr_hash_table_finddata(attributes_byname, attr);
587 if (a && (strcasecmp(a->name, attr->name) == 0)) {
588 if (a->attr != attr->attr) {
589 librad_log("dict_addattr: Duplicate attribute name %s", name);
595 * Same name, same vendor, same attr,
596 * maybe the flags and/or type is
597 * different. Let the new value
598 * over-ride the old one.
603 fr_hash_table_delete(attributes_byvalue, a);
605 if (!fr_hash_table_replace(attributes_byname, attr)) {
606 librad_log("dict_addattr: Internal error storing attribute %s", name);
613 * Insert the SAME pointer (not free'd when this entry is
614 * deleted), into another table.
616 * We want this behaviour because we want OLD names for
617 * the attributes to be read from the configuration
618 * files, but when we're printing them, (and looking up
619 * by value) we want to use the NEW name.
621 if (!fr_hash_table_replace(attributes_byvalue, attr)) {
622 librad_log("dict_addattr: Failed inserting attribute name %s", name);
626 if (!vendor && (value > 0) && (value < 256)) {
627 dict_base_attrs[value] = attr;
635 * Add a value for an attribute to the dictionary.
637 int dict_addvalue(const char *namestr, const char *attrstr, int value)
643 static DICT_ATTR *last_attr = NULL;
646 librad_log("dict_addvalue: empty names are not permitted");
650 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
651 librad_log("dict_addvalue: value name too long");
655 if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
656 librad_log("dict_addvalue: out of memory");
659 memset(dval, 0, sizeof(*dval));
661 strcpy(dval->name, namestr);
665 * Most VALUEs are bunched together by ATTRIBUTE. We can
666 * save a lot of lookups on dictionary initialization by
667 * caching the last attribute.
669 if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
672 dattr = dict_attrbyname(attrstr);
677 * Remember which attribute is associated with this
678 * value, if possible.
681 if (dattr->flags.has_value_alias) {
682 librad_log("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
686 dval->attr = dattr->attr;
689 * Enforce valid values
691 * Don't worry about fixups...
693 switch (dattr->type) {
697 librad_log("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
704 librad_log("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
710 * Allow octets for now, because
711 * of dictionary.cablelabs
715 case PW_TYPE_INTEGER:
720 librad_log("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
721 fr_int2str(type_table, dattr->type, "?Unknown?"));
725 dattr->flags.has_value = 1;
727 value_fixup_t *fixup;
729 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
732 librad_log("dict_addvalue: out of memory");
735 memset(fixup, 0, sizeof(*fixup));
737 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
741 * Insert to the head of the list.
743 fixup->next = value_fixup;
750 * Add the value into the dictionary.
752 if (!fr_hash_table_insert(values_byname, dval)) {
757 * Suppress duplicates with the same
758 * name and value. There are lots in
761 old = dict_valbyname(dattr->attr, namestr);
762 if (old && (old->value == dval->value)) {
769 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
774 * There are multiple VALUE's, keyed by attribute, so we
775 * take care of that here.
777 if (!fr_hash_table_replace(values_byvalue, dval)) {
778 librad_log("dict_addvalue: Failed inserting value %s",
786 static int sscanf_i(const char *str, int *pvalue)
790 const char *tab = "0123456789";
792 if ((str[0] == '0') &&
793 ((str[1] == 'x') || (str[1] == 'X'))) {
794 tab = "0123456789abcdef";
803 c = memchr(tab, tolower((int) *str), base);
817 * Process the ATTRIBUTE command
819 static int process_attribute(const char* fn, const int line,
820 const int block_vendor, char **argv,
828 if ((argc < 3) || (argc > 4)) {
829 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
835 * Validate all entries
837 if (!sscanf_i(argv[1], &value)) {
838 librad_log("dict_init: %s[%d]: invalid value", fn, line);
843 * find the type of the attribute.
845 type = fr_str2int(type_table, argv[2], -1);
847 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
853 * Only look up the vendor if the string
856 memset(&flags, 0, sizeof(flags));
858 char *key, *next, *last;
862 next = strchr(key, ',');
863 if (next) *(next++) = '\0';
865 if (strcmp(key, "has_tag") == 0 ||
866 strcmp(key, "has_tag=1") == 0) {
867 /* Boolean flag, means this is a
871 } else if (strncmp(key, "encrypt=", 8) == 0) {
872 /* Encryption method, defaults to 0 (none).
873 Currently valid is just type 2,
874 Tunnel-Password style, which can only
875 be applied to strings. */
876 flags.encrypt = strtol(key + 8, &last, 0);
878 librad_log( "dict_init: %s[%d] invalid option %s",
883 } else if (strncmp(key, "array", 8) == 0) {
890 case PW_TYPE_INTEGER:
895 librad_log( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
900 } else if (block_vendor) {
901 librad_log( "dict_init: %s[%d]: unknown option \"%s\"",
906 /* Must be a vendor 'flag'... */
907 if (strncmp(key, "vendor=", 7) == 0) {
912 vendor = dict_vendorbyname(key);
914 librad_log( "dict_init: %s[%d]: unknown vendor \"%s\"",
918 if (block_vendor && argv[3][0] &&
919 (block_vendor != vendor)) {
920 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
927 if (key && !*key) break;
931 if (block_vendor) vendor = block_vendor;
934 * Special checks for tags, they make our life much more
939 * Only string, octets, and integer can be tagged.
943 case PW_TYPE_INTEGER:
947 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
949 fr_int2str(type_table, type, "?Unknown?"));
958 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
959 librad_log("dict_init: %s[%d]: %s",
960 fn, line, librad_errstr);
969 * Process the VALUE command
971 static int process_value(const char* fn, const int line, char **argv,
977 librad_log("dict_init: %s[%d]: invalid VALUE line",
982 * For Compatibility, skip "Server-Config"
984 if (strcasecmp(argv[0], "Server-Config") == 0)
988 * Validate all entries
990 if (!sscanf_i(argv[2], &value)) {
991 librad_log("dict_init: %s[%d]: invalid value",
996 if (dict_addvalue(argv[1], argv[0], value) < 0) {
997 librad_log("dict_init: %s[%d]: %s",
998 fn, line, librad_errstr);
1007 * Process the VALUE-ALIAS command
1009 * This allows VALUE mappings to be shared among multiple
1012 static int process_value_alias(const char* fn, const int line, char **argv,
1015 DICT_ATTR *my_da, *da;
1019 librad_log("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1024 my_da = dict_attrbyname(argv[0]);
1026 librad_log("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1031 if (my_da->flags.has_value) {
1032 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1037 if (my_da->flags.has_value_alias) {
1038 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1043 da = dict_attrbyname(argv[1]);
1045 librad_log("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1050 if (!da->flags.has_value) {
1051 librad_log("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1056 if (da->flags.has_value_alias) {
1057 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1062 if (my_da->type != da->type) {
1063 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1068 if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1069 librad_log("dict_addvalue: out of memory");
1072 memset(dval, 0, sizeof(*dval));
1074 dval->name[0] = '\0'; /* empty name */
1075 dval->attr = my_da->attr;
1076 dval->value = da->attr;
1078 if (!fr_hash_table_insert(values_byname, dval)) {
1079 librad_log("dict_init: %s[%d]: Error create alias",
1090 * Process the VENDOR command
1092 static int process_vendor(const char* fn, const int line, char **argv,
1096 const char *format = NULL;
1098 if ((argc < 2) || (argc > 3)) {
1099 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
1105 * Validate all entries
1107 if (!isdigit((int) argv[1][0])) {
1108 librad_log("dict_init: %s[%d]: invalid value",
1112 value = atoi(argv[1]);
1114 /* Create a new VENDOR entry for the list */
1115 if (dict_addvendor(argv[0], value) < 0) {
1116 librad_log("dict_init: %s[%d]: %s",
1117 fn, line, librad_errstr);
1122 * Look for a format statement
1127 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1128 format = "format=4,0";
1130 } else if (value == VENDORPEC_LUCENT) {
1131 format = "format=2,1";
1133 } else if (value == VENDORPEC_STARENT) {
1134 format = "format=2,2";
1136 } /* else no fixups to do */
1143 if (strncasecmp(format, "format=", 7) != 0) {
1144 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1150 if ((strlen(p) != 3) ||
1151 !isdigit((int) p[0]) ||
1153 !isdigit((int) p[2])) {
1154 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1159 type = (int) (p[0] - '0');
1160 length = (int) (p[2] - '0');
1162 dv = dict_vendorbyvalue(value);
1164 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
1169 if ((type != 1) && (type != 2) && (type != 4)) {
1170 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
1175 if ((length != 0) && (length != 1) && (length != 2)) {
1176 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
1182 dv->length = length;
1189 * String split routine. Splits an input string IN PLACE
1190 * into pieces, based on spaces.
1192 static int str2argv(char *str, char **argv, int max_argc)
1197 if (argc >= max_argc) return argc;
1200 * Chop out comments early.
1207 while ((*str == ' ') ||
1210 (*str == '\n')) *(str++) = '\0';
1212 if (!*str) return argc;
1221 (*str != '\n')) str++;
1227 #define MAX_ARGV (16)
1230 * Initialize the dictionary.
1232 static int my_dict_init(const char *dir, const char *fn,
1233 const char *src_file, int src_line)
1242 struct stat statbuf;
1243 char *argv[MAX_ARGV];
1246 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1247 strlen(dir) >= sizeof(dirtmp) / 2) {
1248 librad_log("dict_init: filename name too long");
1253 * First see if fn is relative to dir. If so, create
1254 * new filename. If not, remember the absolute dir.
1256 if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1260 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1261 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1265 if ((fp = fopen(fn, "r")) == NULL) {
1267 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
1268 fn, strerror(errno));
1270 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1271 src_file, src_line, fn, strerror(errno));
1276 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1277 if (!S_ISREG(statbuf.st_mode)) {
1279 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
1285 * Globally writable dictionaries means that users can control
1286 * the server configuration with little difficulty.
1289 if ((statbuf.st_mode & S_IWOTH) != 0) {
1291 librad_log("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1297 dict_stat_add(fn, &statbuf);
1300 * Seed the random pool with data.
1302 fr_rand_seed(&statbuf, sizeof(statbuf));
1306 while (fgets(buf, sizeof(buf), fp) != NULL) {
1308 if (buf[0] == '#' || buf[0] == 0 ||
1309 buf[0] == '\n' || buf[0] == '\r')
1313 * Comment characters should NOT be appearing anywhere but
1314 * as start of a comment;
1316 p = strchr(buf, '#');
1319 argc = str2argv(buf, argv, MAX_ARGV);
1320 if (argc == 0) continue;
1323 librad_log( "dict_init: %s[%d] invalid entry",
1330 * Process VALUE lines.
1332 if (strcasecmp(argv[0], "VALUE") == 0) {
1333 if (process_value(fn, line,
1334 argv + 1, argc - 1) == -1) {
1342 * Perhaps this is an attribute.
1344 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1345 if (process_attribute(fn, line, block_vendor,
1346 argv + 1, argc - 1) == -1) {
1354 * See if we need to import another dictionary.
1356 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1357 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1364 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1365 if (process_value_alias(fn, line,
1366 argv + 1, argc - 1) == -1) {
1374 * Process VENDOR lines.
1376 if (strcasecmp(argv[0], "VENDOR") == 0) {
1377 if (process_vendor(fn, line,
1378 argv + 1, argc - 1) == -1) {
1385 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1388 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1394 vendor = dict_vendorbyname(argv[1]);
1397 "dict_init: %s[%d]: unknown vendor %s",
1402 block_vendor = vendor;
1404 } /* BEGIN-VENDOR */
1406 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1409 "dict_init: %s[%d] invalid END-VENDOR entry",
1415 vendor = dict_vendorbyname(argv[1]);
1418 "dict_init: %s[%d]: unknown vendor %s",
1424 if (vendor != block_vendor) {
1426 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1436 * Any other string: We don't recognize it.
1438 librad_log("dict_init: %s[%d] invalid keyword \"%s\"",
1449 * Empty callback for hash table initialization.
1451 static int null_callback(void *ctx, void *data)
1453 ctx = ctx; /* -Wunused */
1454 data = data; /* -Wunused */
1461 * Initialize the directory, then fix the attr member of
1464 int dict_init(const char *dir, const char *fn)
1467 * Check if we need to change anything. If not, don't do
1470 if (dict_stat_check(dir, fn)) {
1475 * Free the dictionaries, and the stat cache.
1478 stat_root_dir = strdup(dir);
1479 stat_root_file = strdup(fn);
1482 * Create the table of vendor by name. There MAY NOT
1483 * be multiple vendors of the same name.
1485 * Each vendor is malloc'd, so the free function is free.
1487 vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1488 dict_vendor_name_cmp,
1490 if (!vendors_byname) {
1495 * Create the table of vendors by value. There MAY
1496 * be vendors of the same value. If there are, we
1497 * pick the latest one.
1499 vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1500 dict_vendor_value_cmp,
1502 if (!vendors_byvalue) {
1507 * Create the table of attributes by name. There MAY NOT
1508 * be multiple attributes of the same name.
1510 * Each attribute is malloc'd, so the free function is free.
1512 attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1515 if (!attributes_byname) {
1520 * Create the table of attributes by value. There MAY
1521 * be attributes of the same value. If there are, we
1522 * pick the latest one.
1524 attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1525 dict_attr_value_cmp,
1527 if (!attributes_byvalue) {
1531 values_byname = fr_hash_table_create(dict_value_name_hash,
1532 dict_value_name_cmp,
1534 if (!values_byname) {
1538 values_byvalue = fr_hash_table_create(dict_value_value_hash,
1539 dict_value_value_cmp,
1541 if (!values_byvalue) {
1545 value_fixup = NULL; /* just to be safe. */
1547 if (my_dict_init(dir, fn, NULL, 0) < 0)
1552 value_fixup_t *this, *next;
1554 for (this = value_fixup; this != NULL; this = next) {
1557 a = dict_attrbyname(this->attrstr);
1560 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1561 this->attrstr, this->dval->name);
1562 return -1; /* leak, but they should die... */
1565 this->dval->attr = a->attr;
1568 * Add the value into the dictionary.
1570 if (!fr_hash_table_replace(values_byname,
1572 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1577 * Allow them to use the old name, but
1578 * prefer the new name when printing
1581 if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
1582 fr_hash_table_replace(values_byvalue,
1588 * Just so we don't lose track of things.
1595 * Walk over all of the hash tables to ensure they're
1596 * initialized. We do this because the threads may perform
1597 * lookups, and we don't want multi-threaded re-ordering
1598 * of the table entries. That would be bad.
1600 fr_hash_table_walk(vendors_byname, null_callback, NULL);
1601 fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
1603 fr_hash_table_walk(attributes_byname, null_callback, NULL);
1604 fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
1606 fr_hash_table_walk(values_byvalue, null_callback, NULL);
1607 fr_hash_table_walk(values_byname, null_callback, NULL);
1613 * Get an attribute by its numerical value.
1615 DICT_ATTR *dict_attrbyvalue(int attr)
1619 if ((attr > 0) && (attr < 256)) return dict_base_attrs[attr];
1622 dattr.vendor = VENDOR(attr) & 0x7fff;
1624 return fr_hash_table_finddata(attributes_byvalue, &dattr);
1628 * Get an attribute by its name.
1630 DICT_ATTR *dict_attrbyname(const char *name)
1633 uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
1635 if (!name) return NULL;
1637 da = (DICT_ATTR *) buffer;
1638 strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
1640 return fr_hash_table_finddata(attributes_byname, da);
1644 * Associate a value with an attribute and return it.
1646 DICT_VALUE *dict_valbyattr(int attr, int value)
1648 DICT_VALUE dval, *dv;
1651 * First, look up aliases.
1654 dval.name[0] = '\0';
1657 * Look up the attribute alias target, and use
1658 * the correct attribute number if found.
1660 dv = fr_hash_table_finddata(values_byname, &dval);
1661 if (dv) dval.attr = dv->value;
1665 return fr_hash_table_finddata(values_byvalue, &dval);
1669 * Get a value by its name, keyed off of an attribute.
1671 DICT_VALUE *dict_valbyname(int attr, const char *name)
1673 DICT_VALUE *my_dv, *dv;
1674 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1676 if (!name) return NULL;
1678 my_dv = (DICT_VALUE *) buffer;
1680 my_dv->name[0] = '\0';
1683 * Look up the attribute alias target, and use
1684 * the correct attribute number if found.
1686 dv = fr_hash_table_finddata(values_byname, my_dv);
1687 if (dv) my_dv->attr = dv->value;
1689 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
1691 return fr_hash_table_finddata(values_byname, my_dv);
1695 * Get the vendor PEC based on the vendor name
1697 * This is efficient only for small numbers of vendors.
1699 int dict_vendorbyname(const char *name)
1702 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1704 if (!name) return 0;
1706 dv = (DICT_VENDOR *) buffer;
1707 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
1709 dv = fr_hash_table_finddata(vendors_byname, dv);
1712 return dv->vendorpec;
1716 * Return the vendor struct based on the PEC.
1718 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1722 dv.vendorpec = vendorpec;
1724 return fr_hash_table_finddata(vendors_byvalue, &dv);