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/autoconf.h>
36 #ifdef HAVE_SYS_STAT_H
42 #include <freeradius-devel/missing.h>
43 #include <freeradius-devel/libradius.h>
45 #define DICT_VALUE_MAX_NAME_LEN (128)
46 #define DICT_VENDOR_MAX_NAME_LEN (128)
48 static lrad_hash_table_t *vendors_byname = NULL;
49 static lrad_hash_table_t *vendors_byvalue = NULL;
51 static lrad_hash_table_t *attributes_byname = NULL;
52 static lrad_hash_table_t *attributes_byvalue = NULL;
54 static lrad_hash_table_t *values_byvalue = NULL;
55 static lrad_hash_table_t *values_byname = NULL;
58 * For faster HUP's, we cache the stat information for
59 * files we've $INCLUDEd
61 typedef struct dict_stat_t {
62 struct dict_stat_t *next;
67 static char *stat_root_dir = NULL;
68 static char *stat_root_file = NULL;
70 static dict_stat_t *stat_head = NULL;
71 static dict_stat_t *stat_tail = NULL;
73 typedef struct value_fixup_t {
76 struct value_fixup_t *next;
81 * So VALUEs in the dictionary can have forward references.
83 static value_fixup_t *value_fixup = NULL;
85 static const LRAD_NAME_NUMBER type_table[] = {
86 { "string", PW_TYPE_STRING },
87 { "integer", PW_TYPE_INTEGER },
88 { "ipaddr", PW_TYPE_IPADDR },
89 { "date", PW_TYPE_DATE },
90 { "abinary", PW_TYPE_ABINARY },
91 { "octets", PW_TYPE_OCTETS },
92 { "ifid", PW_TYPE_IFID },
93 { "ipv6addr", PW_TYPE_IPV6ADDR },
94 { "ipv6prefix", PW_TYPE_IPV6PREFIX },
95 { "byte", PW_TYPE_BYTE },
96 { "short", PW_TYPE_SHORT },
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 = lrad_hash(&attr->vendor, sizeof(attr->vendor));
148 return lrad_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 lrad_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 lrad_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 = lrad_hash(&dval->attr, sizeof(dval->attr));
216 return lrad_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;
310 * Free the dictionary_attributes and dictionary_values lists.
317 lrad_hash_table_free(vendors_byname);
318 lrad_hash_table_free(vendors_byvalue);
319 vendors_byname = NULL;
320 vendors_byvalue = NULL;
322 lrad_hash_table_free(attributes_byname);
323 lrad_hash_table_free(attributes_byvalue);
324 attributes_byname = NULL;
325 attributes_byvalue = NULL;
327 lrad_hash_table_free(values_byname);
328 lrad_hash_table_free(values_byvalue);
329 values_byname = NULL;
330 values_byvalue = NULL;
337 * Add vendor to the list.
339 int dict_addvendor(const char *name, int value)
344 if (value >= 32767) {
345 librad_log("dict_addvendor: Cannot handle vendor ID larger than 65535");
349 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
350 librad_log("dict_addvendor: vendor name too long");
354 if ((dv = malloc(sizeof(*dv) + length)) == NULL) {
355 librad_log("dict_addvendor: out of memory");
359 strcpy(dv->name, name);
360 dv->vendorpec = value;
361 dv->type = dv->length = 1; /* defaults */
363 if (!lrad_hash_table_insert(vendors_byname, dv)) {
366 old_dv = lrad_hash_table_finddata(vendors_byname, dv);
368 librad_log("dict_addvendor: Failed inserting vendor name %s", name);
371 if (old_dv->vendorpec != dv->vendorpec) {
372 librad_log("dict_addvendor: Duplicate vendor name %s", name);
377 * Already inserted. Discard the duplicate entry.
384 * Insert the SAME pointer (not free'd when this table is
385 * deleted), into another table.
387 * We want this behaviour because we want OLD names for
388 * the attributes to be read from the configuration
389 * files, but when we're printing them, (and looking up
390 * by value) we want to use the NEW name.
392 if (!lrad_hash_table_replace(vendors_byvalue, dv)) {
393 librad_log("dict_addvendor: Failed inserting vendor %s",
402 * Add an attribute to the dictionary.
404 int dict_addattr(const char *name, int vendor, int type, int value,
407 static int max_attr = 0;
410 if (strlen(name) > (sizeof(attr->name) -1)) {
411 librad_log("dict_addattr: attribute name too long");
416 * If the value is '-1', that means use a pre-existing
417 * one (if it already exists). If one does NOT already exist,
418 * then create a new attribute, with a non-conflicting value,
422 if (dict_attrbyname(name)) {
423 return 0; /* exists, don't add it again */
428 } else if (vendor == 0) {
432 if (value > max_attr) {
438 librad_log("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
442 if (value >= 65536) {
443 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
448 DICT_VENDOR *dv = dict_vendorbyvalue(vendor);
451 * If the vendor isn't defined, die/
454 librad_log("dict_addattr: Unknown vendor");
459 * FIXME: Switch over dv->type, and limit things
462 if ((dv->type == 1) && (value >= 256)) {
463 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
465 } /* else 256..65535 are allowed */
469 * Create a new attribute for the list
471 if ((attr = malloc(sizeof(*attr))) == NULL) {
472 librad_log("dict_addattr: out of memory");
476 strcpy(attr->name, name);
478 attr->attr |= (vendor << 16); /* FIXME: hack */
479 attr->vendor = vendor;
482 attr->vendor = vendor;
485 * Yet another hack for Diameter-encoded attributes:
487 if (attr->flags.diameter) attr->attr |= (1 << 31);
490 * Insert the attribute, only if it's not a duplicate.
492 if (!lrad_hash_table_insert(attributes_byname, attr)) {
496 * If the attribute has identical number, then
497 * ignore the duplicate.
499 a = lrad_hash_table_finddata(attributes_byname, attr);
500 if (a && (strcasecmp(a->name, attr->name) == 0)) {
501 if (a->attr != attr->attr) {
502 librad_log("dict_addattr: Duplicate attribute name %s", name);
507 * Same name, same vendor, same attr,
508 * maybe the flags and/or type is
509 * different. Let the new value
510 * over-ride the old one.
516 * Insert the SAME pointer (not free'd when this entry is
517 * deleted), into another table.
519 * We want this behaviour because we want OLD names for
520 * the attributes to be read from the configuration
521 * files, but when we're printing them, (and looking up
522 * by value) we want to use the NEW name.
524 if (!lrad_hash_table_replace(attributes_byvalue, attr)) {
525 librad_log("dict_addattr: Failed inserting attribute name %s", name);
534 * Add a value for an attribute to the dictionary.
536 int dict_addvalue(const char *namestr, const char *attrstr, int value)
543 librad_log("dict_addvalue: empty names are not permitted");
547 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
548 librad_log("dict_addvalue: value name too long");
552 if ((dval = malloc(sizeof(*dval) + length)) == NULL) {
553 librad_log("dict_addvalue: out of memory");
556 memset(dval, 0, sizeof(*dval));
558 strcpy(dval->name, namestr);
562 * Remember which attribute is associated with this
563 * value, if possible.
565 dattr = dict_attrbyname(attrstr);
567 if (dattr->flags.has_value_alias) {
568 librad_log("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
572 dval->attr = dattr->attr;
575 * Enforce valid values
577 * Don't worry about fixups...
579 switch (dattr->type) {
582 librad_log("dict_addvalue: Value \"%d\" is larger than 255", value);
588 librad_log("dict_addvalue: Value \"%d\" is larger than 65535", value);
596 dattr->flags.has_value = 1;
598 value_fixup_t *fixup;
600 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
602 librad_log("dict_addvalue: out of memory");
605 memset(fixup, 0, sizeof(*fixup));
607 strNcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
611 * Insert to the head of the list.
613 fixup->next = value_fixup;
620 * Add the value into the dictionary.
622 if (!lrad_hash_table_insert(values_byname, dval)) {
627 * Suppress duplicates with the same
628 * name and value. There are lots in
631 old = dict_valbyname(dattr->attr, namestr);
632 if (old && (old->value == dval->value)) {
638 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
643 * There are multiple VALUE's, keyed by attribute, so we
644 * take care of that here.
646 if (!lrad_hash_table_replace(values_byvalue, dval)) {
647 librad_log("dict_addvalue: Failed inserting value %s",
656 * Process the ATTRIBUTE command
658 static int process_attribute(const char* fn, const int line,
659 const int block_vendor, char **argv,
668 if ((argc < 3) || (argc > 4)) {
669 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
675 * Validate all entries
677 if (!isdigit((int) argv[1][0])) {
678 librad_log("dict_init: %s[%d]: invalid value", fn, line);
681 sscanf(argv[1], "%i", &value);
684 * find the type of the attribute.
686 type = lrad_str2int(type_table, argv[2], -1);
688 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
694 * Only look up the vendor if the string
697 memset(&flags, 0, sizeof(flags));
700 * FIXME: replace strtok with str2argv
702 s = strtok(argv[3], ",");
704 if (strcmp(s, "has_tag") == 0 ||
705 strcmp(s, "has_tag=1") == 0) {
706 /* Boolean flag, means this is a
710 } else if (strncmp(s, "encrypt=", 8) == 0) {
711 /* Encryption method, defaults to 0 (none).
712 Currently valid is just type 2,
713 Tunnel-Password style, which can only
714 be applied to strings. */
715 flags.encrypt = strtol(s + 8, &c, 0);
717 librad_log( "dict_init: %s[%d] invalid option %s",
722 } else if (strncmp(s, "diameter", 8) == 0) {
725 } else if (strncmp(s, "array", 8) == 0) {
732 case PW_TYPE_INTEGER:
737 librad_log( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
742 } else if (block_vendor) {
743 librad_log( "dict_init: %s[%d]: unknown option \"%s\"",
748 /* Must be a vendor 'flag'... */
749 if (strncmp(s, "vendor=", 7) == 0) {
754 vendor = dict_vendorbyname(s);
756 librad_log( "dict_init: %s[%d]: unknown vendor \"%s\"",
760 if (block_vendor && argv[3][0] &&
761 (block_vendor != vendor)) {
762 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
767 s = strtok(NULL, ",");
771 if (block_vendor) vendor = block_vendor;
774 * Special checks for tags, they make our life much more
779 * Only string, octets, and integer can be tagged.
783 case PW_TYPE_INTEGER:
787 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
789 lrad_int2str(type_table, type, "?Unknown?"));
798 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
799 librad_log("dict_init: %s[%d]: %s",
800 fn, line, librad_errstr);
809 * Process the VALUE command
811 static int process_value(const char* fn, const int line, char **argv,
817 librad_log("dict_init: %s[%d]: invalid VALUE line",
822 * For Compatibility, skip "Server-Config"
824 if (strcasecmp(argv[0], "Server-Config") == 0)
828 * Validate all entries
830 if (!isdigit((int) argv[2][0])) {
831 librad_log("dict_init: %s[%d]: invalid value",
835 sscanf(argv[2], "%i", &value);
837 if (dict_addvalue(argv[1], argv[0], value) < 0) {
838 librad_log("dict_init: %s[%d]: %s",
839 fn, line, librad_errstr);
848 * Process the VALUE-ALIAS command
850 * This allows VALUE mappings to be shared among multiple
853 static int process_value_alias(const char* fn, const int line, char **argv,
856 DICT_ATTR *my_da, *da;
860 librad_log("dict_init: %s[%d]: invalid VALUE-ALIAS line",
865 my_da = dict_attrbyname(argv[0]);
867 librad_log("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
872 if (my_da->flags.has_value) {
873 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
878 if (my_da->flags.has_value_alias) {
879 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
884 da = dict_attrbyname(argv[1]);
886 librad_log("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
891 if (!da->flags.has_value) {
892 librad_log("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
897 if (da->flags.has_value_alias) {
898 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
903 if (my_da->type != da->type) {
904 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
909 if ((dval = malloc(sizeof(*dval))) == NULL) {
910 librad_log("dict_addvalue: out of memory");
913 memset(dval, 0, sizeof(*dval));
915 dval->name[0] = '\0'; /* empty name */
916 dval->attr = my_da->attr;
917 dval->value = da->attr;
919 if (!lrad_hash_table_insert(values_byname, dval)) {
920 librad_log("dict_init: %s[%d]: Error create alias",
931 * Process the VENDOR command
933 static int process_vendor(const char* fn, const int line, char **argv,
937 const char *format = NULL;
939 if ((argc < 2) || (argc > 3)) {
940 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
946 * Validate all entries
948 if (!isdigit((int) argv[1][0])) {
949 librad_log("dict_init: %s[%d]: invalid value",
953 value = atoi(argv[1]);
955 /* Create a new VENDOR entry for the list */
956 if (dict_addvendor(argv[0], value) < 0) {
957 librad_log("dict_init: %s[%d]: %s",
958 fn, line, librad_errstr);
963 * Look for a format statement
968 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
969 format = "format=4,0";
971 } else if (value == VENDORPEC_LUCENT) {
972 format = "format=2,1";
974 } else if (value == VENDORPEC_STARENT) {
975 format = "format=2,2";
977 } /* else no fixups to do */
984 if (strncasecmp(format, "format=", 7) != 0) {
985 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
991 if ((strlen(p) != 3) ||
992 !isdigit((int) p[0]) ||
994 !isdigit((int) p[2])) {
995 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1000 type = (int) (p[0] - '0');
1001 length = (int) (p[2] - '0');
1003 dv = dict_vendorbyvalue(value);
1005 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
1010 if ((type != 1) && (type != 2) && (type != 4)) {
1011 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
1016 if ((length != 0) && (length != 1) && (length != 2)) {
1017 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
1023 dv->length = length;
1030 * String split routine. Splits an input string IN PLACE
1031 * into pieces, based on spaces.
1033 static int str2argv(char *str, char **argv, int max_argc)
1038 if (argc >= max_argc) return argc;
1041 * Chop out comments early.
1048 while ((*str == ' ') ||
1051 (*str == '\n')) *(str++) = '\0';
1053 if (!*str) return argc;
1062 (*str != '\n')) str++;
1068 #define MAX_ARGV (16)
1071 * Initialize the dictionary.
1073 static int my_dict_init(const char *dir, const char *fn,
1074 const char *src_file, int src_line)
1083 struct stat statbuf;
1084 char *argv[MAX_ARGV];
1087 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1088 strlen(dir) >= sizeof(dirtmp) / 2) {
1089 librad_log("dict_init: filename name too long");
1094 * First see if fn is relative to dir. If so, create
1095 * new filename. If not, remember the absolute dir.
1097 if ((p = strrchr(fn, '/')) != NULL) {
1101 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1102 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1106 if ((fp = fopen(fn, "r")) == NULL) {
1108 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
1109 fn, strerror(errno));
1111 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1112 src_file, src_line, fn, strerror(errno));
1117 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1118 if (!S_ISREG(statbuf.st_mode)) {
1120 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
1124 dict_stat_add(fn, &statbuf);
1127 * Seed the random pool with data.
1129 lrad_rand_seed(&statbuf, sizeof(statbuf));
1133 while (fgets(buf, sizeof(buf), fp) != NULL) {
1135 if (buf[0] == '#' || buf[0] == 0 ||
1136 buf[0] == '\n' || buf[0] == '\r')
1140 * Comment characters should NOT be appearing anywhere but
1141 * as start of a comment;
1143 p = strchr(buf, '#');
1146 argc = str2argv(buf, argv, MAX_ARGV);
1147 if (argc == 0) continue;
1150 librad_log( "dict_init: %s[%d] invalid entry",
1159 fprintf(stderr, "ARGC = %d\n",argc);
1160 for (i = 0; i < argc; i++) {
1161 fprintf(stderr, "\t%s\n", argv[i]);
1166 * See if we need to import another dictionary.
1168 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1169 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1177 * Perhaps this is an attribute.
1179 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1180 if (process_attribute(fn, line, block_vendor,
1181 argv + 1, argc - 1) == -1) {
1189 * Process VALUE lines.
1191 if (strcasecmp(argv[0], "VALUE") == 0) {
1192 if (process_value(fn, line,
1193 argv + 1, argc - 1) == -1) {
1200 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1201 if (process_value_alias(fn, line,
1202 argv + 1, argc - 1) == -1) {
1210 * Process VENDOR lines.
1212 if (strcasecmp(argv[0], "VENDOR") == 0) {
1213 if (process_vendor(fn, line,
1214 argv + 1, argc - 1) == -1) {
1221 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1224 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1230 vendor = dict_vendorbyname(argv[1]);
1233 "dict_init: %s[%d]: unknown vendor %s",
1238 block_vendor = vendor;
1240 } /* BEGIN-VENDOR */
1242 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1245 "dict_init: %s[%d] invalid END-VENDOR entry",
1251 vendor = dict_vendorbyname(argv[1]);
1254 "dict_init: %s[%d]: unknown vendor %s",
1260 if (vendor != block_vendor) {
1262 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1272 * Any other string: We don't recognize it.
1275 "dict_init: %s[%d] invalid keyword \"%s\"",
1286 * Empty callback for hash table initialization.
1288 static int null_callback(void *ctx, void *data)
1290 ctx = ctx; /* -Wunused */
1291 data = data; /* -Wunused */
1298 * Initialize the directory, then fix the attr member of
1301 int dict_init(const char *dir, const char *fn)
1304 * Check if we need to change anything. If not, don't do
1307 if (dict_stat_check(dir, fn)) {
1312 * Free the dictionaries, and the stat cache.
1315 stat_root_dir = strdup(dir);
1316 stat_root_file = strdup(fn);
1319 * Create the table of vendor by name. There MAY NOT
1320 * be multiple vendors of the same name.
1322 * Each vendor is malloc'd, so the free function is free.
1324 vendors_byname = lrad_hash_table_create(dict_vendor_name_hash,
1325 dict_vendor_name_cmp,
1327 if (!vendors_byname) {
1332 * Create the table of vendors by value. There MAY
1333 * be vendors of the same value. If there are, we
1334 * pick the latest one.
1336 vendors_byvalue = lrad_hash_table_create(dict_vendor_value_hash,
1337 dict_vendor_value_cmp,
1339 if (!vendors_byvalue) {
1344 * Create the table of attributes by name. There MAY NOT
1345 * be multiple attributes of the same name.
1347 * Each attribute is malloc'd, so the free function is free.
1349 attributes_byname = lrad_hash_table_create(dict_attr_name_hash,
1352 if (!attributes_byname) {
1357 * Create the table of attributes by value. There MAY
1358 * be attributes of the same value. If there are, we
1359 * pick the latest one.
1361 attributes_byvalue = lrad_hash_table_create(dict_attr_value_hash,
1362 dict_attr_value_cmp,
1364 if (!attributes_byvalue) {
1368 values_byname = lrad_hash_table_create(dict_value_name_hash,
1369 dict_value_name_cmp,
1371 if (!values_byname) {
1375 values_byvalue = lrad_hash_table_create(dict_value_value_hash,
1376 dict_value_value_cmp,
1378 if (!values_byvalue) {
1382 value_fixup = NULL; /* just to be safe. */
1384 if (my_dict_init(dir, fn, NULL, 0) < 0)
1389 value_fixup_t *this, *next;
1391 for (this = value_fixup; this != NULL; this = next) {
1394 a = dict_attrbyname(this->attrstr);
1397 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1398 this->attrstr, this->dval->name);
1399 return -1; /* leak, but they should die... */
1402 this->dval->attr = a->attr;
1405 * Add the value into the dictionary.
1407 if (!lrad_hash_table_replace(values_byname,
1409 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1414 * Allow them to use the old name, but
1415 * prefer the new name when printing
1418 if (!lrad_hash_table_finddata(values_byvalue, this->dval)) {
1419 lrad_hash_table_replace(values_byvalue,
1425 * Just so we don't lose track of things.
1432 * Walk over all of the hash tables to ensure they're
1433 * initialized. We do this because the threads may perform
1434 * lookups, and we don't want multi-threaded re-ordering
1435 * of the table entries. That would be bad.
1437 lrad_hash_table_walk(vendors_byname, null_callback, NULL);
1438 lrad_hash_table_walk(vendors_byvalue, null_callback, NULL);
1440 lrad_hash_table_walk(attributes_byname, null_callback, NULL);
1441 lrad_hash_table_walk(attributes_byvalue, null_callback, NULL);
1443 lrad_hash_table_walk(values_byvalue, null_callback, NULL);
1444 lrad_hash_table_walk(values_byname, null_callback, NULL);
1450 * Get an attribute by its numerical value.
1452 DICT_ATTR *dict_attrbyvalue(int attr)
1457 dattr.vendor = VENDOR(attr) & 0x7fff;
1459 return lrad_hash_table_finddata(attributes_byvalue, &dattr);
1463 * Get an attribute by its name.
1465 DICT_ATTR *dict_attrbyname(const char *name)
1469 if (!name) return NULL;
1471 strNcpy(dattr.name, name, sizeof(dattr.name));
1473 return lrad_hash_table_finddata(attributes_byname, &dattr);
1477 * Associate a value with an attribute and return it.
1479 DICT_VALUE *dict_valbyattr(int attr, int value)
1481 DICT_VALUE dval, *dv;
1484 * First, look up aliases.
1487 dval.name[0] = '\0';
1490 * Look up the attribute alias target, and use
1491 * the correct attribute number if found.
1493 dv = lrad_hash_table_finddata(values_byname, &dval);
1494 if (dv) dval.attr = dv->value;
1498 return lrad_hash_table_finddata(values_byvalue, &dval);
1502 * Get a value by its name, keyed off of an attribute.
1504 DICT_VALUE *dict_valbyname(int attr, const char *name)
1506 DICT_VALUE *my_dv, *dv;
1507 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1509 if (!name) return NULL;
1511 my_dv = (DICT_VALUE *) buffer;
1513 my_dv->name[0] = '\0';
1516 * Look up the attribute alias target, and use
1517 * the correct attribute number if found.
1519 dv = lrad_hash_table_finddata(values_byname, my_dv);
1520 if (dv) my_dv->attr = dv->value;
1522 strNcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN);
1524 return lrad_hash_table_finddata(values_byname, my_dv);
1528 * Get the vendor PEC based on the vendor name
1530 * This is efficient only for small numbers of vendors.
1532 int dict_vendorbyname(const char *name)
1535 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1537 if (!name) return 0;
1539 dv = (DICT_VENDOR *) buffer;
1540 strNcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN);
1542 dv = lrad_hash_table_finddata(vendors_byname, dv);
1545 return dv->vendorpec;
1549 * Return the vendor struct based on the PEC.
1551 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1555 dv.vendorpec = vendorpec;
1557 return lrad_hash_table_finddata(vendors_byvalue, &dv);