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 { "string", PW_TYPE_STRING },
82 { "integer", PW_TYPE_INTEGER },
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 },
97 * Create the hash of the name.
99 * We copy the hash function here because it's substantially faster.
101 #define FNV_MAGIC_INIT (0x811c9dc5)
102 #define FNV_MAGIC_PRIME (0x01000193)
104 static uint32_t dict_hashname(const char *name)
106 uint32_t hash = FNV_MAGIC_INIT;
109 for (p = name; *p != '\0'; p++) {
110 int c = *(const unsigned char *) p;
111 if (isalpha(c)) c = tolower(c);
113 hash *= FNV_MAGIC_PRIME;
114 hash ^= (uint32_t ) (c & 0xff);
122 * Hash callback functions.
124 static uint32_t dict_attr_name_hash(const void *data)
126 return dict_hashname(((const DICT_ATTR *)data)->name);
129 static int dict_attr_name_cmp(const void *one, const void *two)
131 const DICT_ATTR *a = one;
132 const DICT_ATTR *b = two;
134 return strcasecmp(a->name, b->name);
137 static uint32_t dict_attr_value_hash(const void *data)
140 const DICT_ATTR *attr = data;
142 hash = lrad_hash(&attr->vendor, sizeof(attr->vendor));
143 return lrad_hash_update(&attr->attr, sizeof(attr->attr), hash);
146 static int dict_attr_value_cmp(const void *one, const void *two)
148 const DICT_ATTR *a = one;
149 const DICT_ATTR *b = two;
151 if (a->vendor < b->vendor) return -1;
152 if (a->vendor > b->vendor) return +1;
154 return a->attr - b->attr;
157 static uint32_t dict_vendor_name_hash(const void *data)
159 return dict_hashname(((const DICT_VENDOR *)data)->name);
162 static int dict_vendor_name_cmp(const void *one, const void *two)
164 const DICT_VENDOR *a = one;
165 const DICT_VENDOR *b = two;
167 return strcasecmp(a->name, b->name);
170 static uint32_t dict_vendor_value_hash(const void *data)
172 return lrad_hash(&(((const DICT_VENDOR *)data)->vendorpec),
173 sizeof(((const DICT_VENDOR *)data)->vendorpec));
176 static int dict_vendor_value_cmp(const void *one, const void *two)
178 const DICT_VENDOR *a = one;
179 const DICT_VENDOR *b = two;
181 return a->vendorpec - b->vendorpec;
184 static uint32_t dict_value_name_hash(const void *data)
187 const DICT_VALUE *dval = data;
189 hash = dict_hashname(dval->name);
190 return lrad_hash_update(&dval->attr, sizeof(dval->attr), hash);
193 static int dict_value_name_cmp(const void *one, const void *two)
196 const DICT_VALUE *a = one;
197 const DICT_VALUE *b = two;
199 rcode = a->attr - b->attr;
200 if (rcode != 0) return rcode;
202 return strcasecmp(a->name, b->name);
205 static uint32_t dict_value_value_hash(const void *data)
208 const DICT_VALUE *dval = data;
210 hash = lrad_hash(&dval->attr, sizeof(dval->attr));
211 return lrad_hash_update(&dval->value, sizeof(dval->value), hash);
214 static int dict_value_value_cmp(const void *one, const void *two)
217 const DICT_VALUE *a = one;
218 const DICT_VALUE *b = two;
220 rcode = a->attr - b->attr;
221 if (rcode != 0) return rcode;
223 return a->value - b->value;
228 * Free the list of stat buffers
230 static void dict_stat_free(void)
232 dict_stat_t *this, *next;
235 stat_root_dir = NULL;
236 free(stat_root_file);
237 stat_root_file = NULL;
244 for (this = stat_head; this != NULL; this = next) {
250 stat_head = stat_tail = NULL;
255 * Add an entry to the list of stat buffers.
257 static void dict_stat_add(const char *name, const struct stat *stat_buf)
261 this = malloc(sizeof(*this));
263 memset(this, 0, sizeof(*this));
265 this->name = strdup(name);
266 this->mtime = stat_buf->st_mtime;
269 stat_head = stat_tail = this;
271 stat_tail->next = this;
278 * See if any dictionaries have changed. If not, don't
281 static int dict_stat_check(const char *root_dir, const char *root_file)
286 if (!stat_root_dir) return 0;
287 if (!stat_root_file) return 0;
289 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
290 if (strcmp(root_file, stat_root_file) != 0) return 0;
292 if (!stat_head) return 0; /* changed, reload */
294 for (this = stat_head; this != NULL; this = this->next) {
295 if (stat(this->name, &buf) < 0) return 0;
297 if (buf.st_mtime != this->mtime) return 0;
305 * Free the dictionary_attributes and dictionary_values lists.
312 lrad_hash_table_free(vendors_byname);
313 lrad_hash_table_free(vendors_byvalue);
314 vendors_byname = NULL;
315 vendors_byvalue = NULL;
317 lrad_hash_table_free(attributes_byname);
318 lrad_hash_table_free(attributes_byvalue);
319 attributes_byname = NULL;
320 attributes_byvalue = NULL;
322 lrad_hash_table_free(values_byname);
323 lrad_hash_table_free(values_byvalue);
324 values_byname = NULL;
325 values_byvalue = NULL;
327 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
334 * Add vendor to the list.
336 int dict_addvendor(const char *name, int value)
341 if (value >= 32767) {
342 librad_log("dict_addvendor: Cannot handle vendor ID larger than 65535");
346 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
347 librad_log("dict_addvendor: vendor name too long");
351 if ((dv = malloc(sizeof(*dv) + length)) == NULL) {
352 librad_log("dict_addvendor: out of memory");
356 strcpy(dv->name, name);
357 dv->vendorpec = value;
358 dv->type = dv->length = 1; /* defaults */
360 if (!lrad_hash_table_insert(vendors_byname, dv)) {
363 old_dv = lrad_hash_table_finddata(vendors_byname, dv);
365 librad_log("dict_addvendor: Failed inserting vendor name %s", name);
368 if (old_dv->vendorpec != dv->vendorpec) {
369 librad_log("dict_addvendor: Duplicate vendor name %s", name);
374 * Already inserted. Discard the duplicate entry.
381 * Insert the SAME pointer (not free'd when this table is
382 * deleted), into another table.
384 * We want this behaviour because we want OLD names for
385 * the attributes to be read from the configuration
386 * files, but when we're printing them, (and looking up
387 * by value) we want to use the NEW name.
389 if (!lrad_hash_table_replace(vendors_byvalue, dv)) {
390 librad_log("dict_addvendor: Failed inserting vendor %s",
399 * Add an attribute to the dictionary.
401 int dict_addattr(const char *name, int vendor, int type, int value,
404 static int max_attr = 0;
407 if (strlen(name) > (sizeof(attr->name) -1)) {
408 librad_log("dict_addattr: attribute name too long");
413 * If the value is '-1', that means use a pre-existing
414 * one (if it already exists). If one does NOT already exist,
415 * then create a new attribute, with a non-conflicting value,
419 if (dict_attrbyname(name)) {
420 return 0; /* exists, don't add it again */
425 } else if (vendor == 0) {
429 if (value > max_attr) {
435 librad_log("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
439 if (value >= 65536) {
440 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
445 DICT_VENDOR *dv = dict_vendorbyvalue(vendor);
448 * If the vendor isn't defined, die/
451 librad_log("dict_addattr: Unknown vendor");
456 * FIXME: Switch over dv->type, and limit things
459 if ((dv->type == 1) && (value >= 256)) {
460 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
462 } /* else 256..65535 are allowed */
466 * Create a new attribute for the list
468 if ((attr = malloc(sizeof(*attr))) == NULL) {
469 librad_log("dict_addattr: out of memory");
473 strcpy(attr->name, name);
475 attr->attr |= (vendor << 16); /* FIXME: hack */
476 attr->vendor = vendor;
479 attr->vendor = vendor;
482 * Insert the attribute, only if it's not a duplicate.
484 if (!lrad_hash_table_insert(attributes_byname, attr)) {
488 * If the attribute has identical number, then
489 * ignore the duplicate.
491 a = lrad_hash_table_finddata(attributes_byname, attr);
492 if (a && (strcasecmp(a->name, attr->name) == 0)) {
493 if (a->attr != attr->attr) {
494 librad_log("dict_addattr: Duplicate attribute name %s", name);
500 * Same name, same vendor, same attr,
501 * maybe the flags and/or type is
502 * different. Let the new value
503 * over-ride the old one.
508 lrad_hash_table_delete(attributes_byvalue, a);
510 if (!lrad_hash_table_replace(attributes_byname, attr)) {
511 librad_log("dict_addattr: Internal error storing attribute %s", name);
518 * Insert the SAME pointer (not free'd when this entry is
519 * deleted), into another table.
521 * We want this behaviour because we want OLD names for
522 * the attributes to be read from the configuration
523 * files, but when we're printing them, (and looking up
524 * by value) we want to use the NEW name.
526 if (!lrad_hash_table_replace(attributes_byvalue, attr)) {
527 librad_log("dict_addattr: Failed inserting attribute name %s", name);
531 if (!vendor && (value > 0) && (value < 256)) {
532 dict_base_attrs[value] = attr;
540 * Add a value for an attribute to the dictionary.
542 int dict_addvalue(const char *namestr, const char *attrstr, int value)
549 librad_log("dict_addvalue: empty names are not permitted");
553 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
554 librad_log("dict_addvalue: value name too long");
558 if ((dval = malloc(sizeof(*dval) + length)) == NULL) {
559 librad_log("dict_addvalue: out of memory");
562 memset(dval, 0, sizeof(*dval));
564 strcpy(dval->name, namestr);
568 * Remember which attribute is associated with this
569 * value, if possible.
571 dattr = dict_attrbyname(attrstr);
573 if (dattr->flags.has_value_alias) {
574 librad_log("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
578 dval->attr = dattr->attr;
581 * Enforce valid values
583 * Don't worry about fixups...
585 switch (dattr->type) {
588 librad_log("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
594 librad_log("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
600 * Allow octets for now, because
601 * of dictionary.cablelabs
605 case PW_TYPE_INTEGER:
609 librad_log("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
610 lrad_int2str(type_table, dattr->type, "?Unknown?"));
614 dattr->flags.has_value = 1;
616 value_fixup_t *fixup;
618 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
620 librad_log("dict_addvalue: out of memory");
623 memset(fixup, 0, sizeof(*fixup));
625 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
629 * Insert to the head of the list.
631 fixup->next = value_fixup;
638 * Add the value into the dictionary.
640 if (!lrad_hash_table_insert(values_byname, dval)) {
645 * Suppress duplicates with the same
646 * name and value. There are lots in
649 old = dict_valbyname(dattr->attr, namestr);
650 if (old && (old->value == dval->value)) {
656 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
661 * There are multiple VALUE's, keyed by attribute, so we
662 * take care of that here.
664 if (!lrad_hash_table_replace(values_byvalue, dval)) {
665 librad_log("dict_addvalue: Failed inserting value %s",
674 * Process the ATTRIBUTE command
676 static int process_attribute(const char* fn, const int line,
677 const int block_vendor, char **argv,
686 if ((argc < 3) || (argc > 4)) {
687 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
693 * Validate all entries
695 if (!isdigit((int) argv[1][0])) {
696 librad_log("dict_init: %s[%d]: invalid value", fn, line);
699 sscanf(argv[1], "%i", &value);
702 * find the type of the attribute.
704 type = lrad_str2int(type_table, argv[2], -1);
706 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
712 * Only look up the vendor if the string
715 memset(&flags, 0, sizeof(flags));
718 * FIXME: replace strtok with str2argv
720 s = strtok(argv[3], ",");
722 if (strcmp(s, "has_tag") == 0 ||
723 strcmp(s, "has_tag=1") == 0) {
724 /* Boolean flag, means this is a
728 } else if (strncmp(s, "encrypt=", 8) == 0) {
729 /* Encryption method, defaults to 0 (none).
730 Currently valid is just type 2,
731 Tunnel-Password style, which can only
732 be applied to strings. */
733 flags.encrypt = strtol(s + 8, &c, 0);
735 librad_log( "dict_init: %s[%d] invalid option %s",
740 } else if (strncmp(s, "array", 8) == 0) {
747 case PW_TYPE_INTEGER:
752 librad_log( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
757 } else if (block_vendor) {
758 librad_log( "dict_init: %s[%d]: unknown option \"%s\"",
763 /* Must be a vendor 'flag'... */
764 if (strncmp(s, "vendor=", 7) == 0) {
769 vendor = dict_vendorbyname(s);
771 librad_log( "dict_init: %s[%d]: unknown vendor \"%s\"",
775 if (block_vendor && argv[3][0] &&
776 (block_vendor != vendor)) {
777 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
782 s = strtok(NULL, ",");
786 if (block_vendor) vendor = block_vendor;
789 * Special checks for tags, they make our life much more
794 * Only string, octets, and integer can be tagged.
798 case PW_TYPE_INTEGER:
802 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
804 lrad_int2str(type_table, type, "?Unknown?"));
813 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
814 librad_log("dict_init: %s[%d]: %s",
815 fn, line, librad_errstr);
824 * Process the VALUE command
826 static int process_value(const char* fn, const int line, char **argv,
832 librad_log("dict_init: %s[%d]: invalid VALUE line",
837 * For Compatibility, skip "Server-Config"
839 if (strcasecmp(argv[0], "Server-Config") == 0)
843 * Validate all entries
845 if (!isdigit((int) argv[2][0])) {
846 librad_log("dict_init: %s[%d]: invalid value",
850 sscanf(argv[2], "%i", &value);
852 if (dict_addvalue(argv[1], argv[0], value) < 0) {
853 librad_log("dict_init: %s[%d]: %s",
854 fn, line, librad_errstr);
863 * Process the VALUE-ALIAS command
865 * This allows VALUE mappings to be shared among multiple
868 static int process_value_alias(const char* fn, const int line, char **argv,
871 DICT_ATTR *my_da, *da;
875 librad_log("dict_init: %s[%d]: invalid VALUE-ALIAS line",
880 my_da = dict_attrbyname(argv[0]);
882 librad_log("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
887 if (my_da->flags.has_value) {
888 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
893 if (my_da->flags.has_value_alias) {
894 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
899 da = dict_attrbyname(argv[1]);
901 librad_log("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
906 if (!da->flags.has_value) {
907 librad_log("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
912 if (da->flags.has_value_alias) {
913 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
918 if (my_da->type != da->type) {
919 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
924 if ((dval = malloc(sizeof(*dval))) == NULL) {
925 librad_log("dict_addvalue: out of memory");
928 memset(dval, 0, sizeof(*dval));
930 dval->name[0] = '\0'; /* empty name */
931 dval->attr = my_da->attr;
932 dval->value = da->attr;
934 if (!lrad_hash_table_insert(values_byname, dval)) {
935 librad_log("dict_init: %s[%d]: Error create alias",
946 * Process the VENDOR command
948 static int process_vendor(const char* fn, const int line, char **argv,
952 const char *format = NULL;
954 if ((argc < 2) || (argc > 3)) {
955 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
961 * Validate all entries
963 if (!isdigit((int) argv[1][0])) {
964 librad_log("dict_init: %s[%d]: invalid value",
968 value = atoi(argv[1]);
970 /* Create a new VENDOR entry for the list */
971 if (dict_addvendor(argv[0], value) < 0) {
972 librad_log("dict_init: %s[%d]: %s",
973 fn, line, librad_errstr);
978 * Look for a format statement
983 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
984 format = "format=4,0";
986 } else if (value == VENDORPEC_LUCENT) {
987 format = "format=2,1";
989 } else if (value == VENDORPEC_STARENT) {
990 format = "format=2,2";
992 } /* else no fixups to do */
999 if (strncasecmp(format, "format=", 7) != 0) {
1000 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1006 if ((strlen(p) != 3) ||
1007 !isdigit((int) p[0]) ||
1009 !isdigit((int) p[2])) {
1010 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1015 type = (int) (p[0] - '0');
1016 length = (int) (p[2] - '0');
1018 dv = dict_vendorbyvalue(value);
1020 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
1025 if ((type != 1) && (type != 2) && (type != 4)) {
1026 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
1031 if ((length != 0) && (length != 1) && (length != 2)) {
1032 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
1038 dv->length = length;
1045 * String split routine. Splits an input string IN PLACE
1046 * into pieces, based on spaces.
1048 static int str2argv(char *str, char **argv, int max_argc)
1053 if (argc >= max_argc) return argc;
1056 * Chop out comments early.
1063 while ((*str == ' ') ||
1066 (*str == '\n')) *(str++) = '\0';
1068 if (!*str) return argc;
1077 (*str != '\n')) str++;
1083 #define MAX_ARGV (16)
1086 * Initialize the dictionary.
1088 static int my_dict_init(const char *dir, const char *fn,
1089 const char *src_file, int src_line)
1098 struct stat statbuf;
1099 char *argv[MAX_ARGV];
1102 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1103 strlen(dir) >= sizeof(dirtmp) / 2) {
1104 librad_log("dict_init: filename name too long");
1109 * First see if fn is relative to dir. If so, create
1110 * new filename. If not, remember the absolute dir.
1112 if ((p = strrchr(fn, '/')) != NULL) {
1116 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1117 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1121 if ((fp = fopen(fn, "r")) == NULL) {
1123 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
1124 fn, strerror(errno));
1126 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1127 src_file, src_line, fn, strerror(errno));
1132 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1133 if (!S_ISREG(statbuf.st_mode)) {
1135 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
1141 * Globally writable dictionaries means that users can control
1142 * the server configuration with little difficulty.
1145 if ((statbuf.st_mode & S_IWOTH) != 0) {
1147 librad_log("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1153 dict_stat_add(fn, &statbuf);
1156 * Seed the random pool with data.
1158 lrad_rand_seed(&statbuf, sizeof(statbuf));
1162 while (fgets(buf, sizeof(buf), fp) != NULL) {
1164 if (buf[0] == '#' || buf[0] == 0 ||
1165 buf[0] == '\n' || buf[0] == '\r')
1169 * Comment characters should NOT be appearing anywhere but
1170 * as start of a comment;
1172 p = strchr(buf, '#');
1175 argc = str2argv(buf, argv, MAX_ARGV);
1176 if (argc == 0) continue;
1179 librad_log( "dict_init: %s[%d] invalid entry",
1188 fprintf(stderr, "ARGC = %d\n",argc);
1189 for (i = 0; i < argc; i++) {
1190 fprintf(stderr, "\t%s\n", argv[i]);
1195 * See if we need to import another dictionary.
1197 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1198 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1206 * Perhaps this is an attribute.
1208 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1209 if (process_attribute(fn, line, block_vendor,
1210 argv + 1, argc - 1) == -1) {
1218 * Process VALUE lines.
1220 if (strcasecmp(argv[0], "VALUE") == 0) {
1221 if (process_value(fn, line,
1222 argv + 1, argc - 1) == -1) {
1229 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1230 if (process_value_alias(fn, line,
1231 argv + 1, argc - 1) == -1) {
1239 * Process VENDOR lines.
1241 if (strcasecmp(argv[0], "VENDOR") == 0) {
1242 if (process_vendor(fn, line,
1243 argv + 1, argc - 1) == -1) {
1250 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1253 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1259 vendor = dict_vendorbyname(argv[1]);
1262 "dict_init: %s[%d]: unknown vendor %s",
1267 block_vendor = vendor;
1269 } /* BEGIN-VENDOR */
1271 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1274 "dict_init: %s[%d] invalid END-VENDOR entry",
1280 vendor = dict_vendorbyname(argv[1]);
1283 "dict_init: %s[%d]: unknown vendor %s",
1289 if (vendor != block_vendor) {
1291 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1301 * Any other string: We don't recognize it.
1304 "dict_init: %s[%d] invalid keyword \"%s\"",
1315 * Empty callback for hash table initialization.
1317 static int null_callback(void *ctx, void *data)
1319 ctx = ctx; /* -Wunused */
1320 data = data; /* -Wunused */
1327 * Initialize the directory, then fix the attr member of
1330 int dict_init(const char *dir, const char *fn)
1333 * Check if we need to change anything. If not, don't do
1336 if (dict_stat_check(dir, fn)) {
1341 * Free the dictionaries, and the stat cache.
1344 stat_root_dir = strdup(dir);
1345 stat_root_file = strdup(fn);
1348 * Create the table of vendor by name. There MAY NOT
1349 * be multiple vendors of the same name.
1351 * Each vendor is malloc'd, so the free function is free.
1353 vendors_byname = lrad_hash_table_create(dict_vendor_name_hash,
1354 dict_vendor_name_cmp,
1356 if (!vendors_byname) {
1361 * Create the table of vendors by value. There MAY
1362 * be vendors of the same value. If there are, we
1363 * pick the latest one.
1365 vendors_byvalue = lrad_hash_table_create(dict_vendor_value_hash,
1366 dict_vendor_value_cmp,
1368 if (!vendors_byvalue) {
1373 * Create the table of attributes by name. There MAY NOT
1374 * be multiple attributes of the same name.
1376 * Each attribute is malloc'd, so the free function is free.
1378 attributes_byname = lrad_hash_table_create(dict_attr_name_hash,
1381 if (!attributes_byname) {
1386 * Create the table of attributes by value. There MAY
1387 * be attributes of the same value. If there are, we
1388 * pick the latest one.
1390 attributes_byvalue = lrad_hash_table_create(dict_attr_value_hash,
1391 dict_attr_value_cmp,
1393 if (!attributes_byvalue) {
1397 values_byname = lrad_hash_table_create(dict_value_name_hash,
1398 dict_value_name_cmp,
1400 if (!values_byname) {
1404 values_byvalue = lrad_hash_table_create(dict_value_value_hash,
1405 dict_value_value_cmp,
1407 if (!values_byvalue) {
1411 value_fixup = NULL; /* just to be safe. */
1413 if (my_dict_init(dir, fn, NULL, 0) < 0)
1418 value_fixup_t *this, *next;
1420 for (this = value_fixup; this != NULL; this = next) {
1423 a = dict_attrbyname(this->attrstr);
1426 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1427 this->attrstr, this->dval->name);
1428 return -1; /* leak, but they should die... */
1431 this->dval->attr = a->attr;
1434 * Add the value into the dictionary.
1436 if (!lrad_hash_table_replace(values_byname,
1438 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1443 * Allow them to use the old name, but
1444 * prefer the new name when printing
1447 if (!lrad_hash_table_finddata(values_byvalue, this->dval)) {
1448 lrad_hash_table_replace(values_byvalue,
1454 * Just so we don't lose track of things.
1461 * Walk over all of the hash tables to ensure they're
1462 * initialized. We do this because the threads may perform
1463 * lookups, and we don't want multi-threaded re-ordering
1464 * of the table entries. That would be bad.
1466 lrad_hash_table_walk(vendors_byname, null_callback, NULL);
1467 lrad_hash_table_walk(vendors_byvalue, null_callback, NULL);
1469 lrad_hash_table_walk(attributes_byname, null_callback, NULL);
1470 lrad_hash_table_walk(attributes_byvalue, null_callback, NULL);
1472 lrad_hash_table_walk(values_byvalue, null_callback, NULL);
1473 lrad_hash_table_walk(values_byname, null_callback, NULL);
1479 * Get an attribute by its numerical value.
1481 DICT_ATTR *dict_attrbyvalue(int attr)
1485 if ((attr > 0) && (attr < 256)) return dict_base_attrs[attr];
1488 dattr.vendor = VENDOR(attr) & 0x7fff;
1490 return lrad_hash_table_finddata(attributes_byvalue, &dattr);
1494 * Get an attribute by its name.
1496 DICT_ATTR *dict_attrbyname(const char *name)
1500 if (!name) return NULL;
1502 strlcpy(dattr.name, name, sizeof(dattr.name));
1504 return lrad_hash_table_finddata(attributes_byname, &dattr);
1508 * Associate a value with an attribute and return it.
1510 DICT_VALUE *dict_valbyattr(int attr, int value)
1512 DICT_VALUE dval, *dv;
1515 * First, look up aliases.
1518 dval.name[0] = '\0';
1521 * Look up the attribute alias target, and use
1522 * the correct attribute number if found.
1524 dv = lrad_hash_table_finddata(values_byname, &dval);
1525 if (dv) dval.attr = dv->value;
1529 return lrad_hash_table_finddata(values_byvalue, &dval);
1533 * Get a value by its name, keyed off of an attribute.
1535 DICT_VALUE *dict_valbyname(int attr, const char *name)
1537 DICT_VALUE *my_dv, *dv;
1538 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1540 if (!name) return NULL;
1542 my_dv = (DICT_VALUE *) buffer;
1544 my_dv->name[0] = '\0';
1547 * Look up the attribute alias target, and use
1548 * the correct attribute number if found.
1550 dv = lrad_hash_table_finddata(values_byname, my_dv);
1551 if (dv) my_dv->attr = dv->value;
1553 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN);
1555 return lrad_hash_table_finddata(values_byname, my_dv);
1559 * Get the vendor PEC based on the vendor name
1561 * This is efficient only for small numbers of vendors.
1563 int dict_vendorbyname(const char *name)
1566 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1568 if (!name) return 0;
1570 dv = (DICT_VENDOR *) buffer;
1571 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN);
1573 dv = lrad_hash_table_finddata(vendors_byname, dv);
1576 return dv->vendorpec;
1580 * Return the vendor struct based on the PEC.
1582 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1586 dv.vendorpec = vendorpec;
1588 return lrad_hash_table_finddata(vendors_byvalue, &dv);