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 },
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;
306 * Free the dictionary_attributes and dictionary_values lists.
313 lrad_hash_table_free(vendors_byname);
314 lrad_hash_table_free(vendors_byvalue);
315 vendors_byname = NULL;
316 vendors_byvalue = NULL;
318 lrad_hash_table_free(attributes_byname);
319 lrad_hash_table_free(attributes_byvalue);
320 attributes_byname = NULL;
321 attributes_byvalue = NULL;
323 lrad_hash_table_free(values_byname);
324 lrad_hash_table_free(values_byvalue);
325 values_byname = NULL;
326 values_byvalue = NULL;
328 memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
335 * Add vendor to the list.
337 int dict_addvendor(const char *name, int value)
342 if (value >= 32767) {
343 librad_log("dict_addvendor: Cannot handle vendor ID larger than 65535");
347 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
348 librad_log("dict_addvendor: vendor name too long");
352 if ((dv = malloc(sizeof(*dv) + length)) == NULL) {
353 librad_log("dict_addvendor: out of memory");
357 strcpy(dv->name, name);
358 dv->vendorpec = value;
359 dv->type = dv->length = 1; /* defaults */
361 if (!lrad_hash_table_insert(vendors_byname, dv)) {
364 old_dv = lrad_hash_table_finddata(vendors_byname, dv);
366 librad_log("dict_addvendor: Failed inserting vendor name %s", name);
369 if (old_dv->vendorpec != dv->vendorpec) {
370 librad_log("dict_addvendor: Duplicate vendor name %s", name);
375 * Already inserted. Discard the duplicate entry.
382 * Insert the SAME pointer (not free'd when this table is
383 * deleted), into another table.
385 * We want this behaviour because we want OLD names for
386 * the attributes to be read from the configuration
387 * files, but when we're printing them, (and looking up
388 * by value) we want to use the NEW name.
390 if (!lrad_hash_table_replace(vendors_byvalue, dv)) {
391 librad_log("dict_addvendor: Failed inserting vendor %s",
400 * Add an attribute to the dictionary.
402 int dict_addattr(const char *name, int vendor, int type, int value,
405 static int max_attr = 0;
408 if (strlen(name) > (sizeof(attr->name) -1)) {
409 librad_log("dict_addattr: attribute name too long");
414 * If the value is '-1', that means use a pre-existing
415 * one (if it already exists). If one does NOT already exist,
416 * then create a new attribute, with a non-conflicting value,
420 if (dict_attrbyname(name)) {
421 return 0; /* exists, don't add it again */
426 } else if (vendor == 0) {
430 if (value > max_attr) {
436 librad_log("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
440 if (value >= 65536) {
441 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
446 DICT_VENDOR *dv = dict_vendorbyvalue(vendor);
449 * If the vendor isn't defined, die/
452 librad_log("dict_addattr: Unknown vendor");
457 * FIXME: Switch over dv->type, and limit things
460 if ((dv->type == 1) && (value >= 256)) {
461 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
463 } /* else 256..65535 are allowed */
467 * Create a new attribute for the list
469 if ((attr = malloc(sizeof(*attr))) == NULL) {
470 librad_log("dict_addattr: out of memory");
474 strcpy(attr->name, name);
476 attr->attr |= (vendor << 16); /* FIXME: hack */
477 attr->vendor = vendor;
480 attr->vendor = vendor;
483 * Insert the attribute, only if it's not a duplicate.
485 if (!lrad_hash_table_insert(attributes_byname, attr)) {
489 * If the attribute has identical number, then
490 * ignore the duplicate.
492 a = lrad_hash_table_finddata(attributes_byname, attr);
493 if (a && (strcasecmp(a->name, attr->name) == 0)) {
494 if (a->attr != attr->attr) {
495 librad_log("dict_addattr: Duplicate attribute name %s", name);
501 * Same name, same vendor, same attr,
502 * maybe the flags and/or type is
503 * different. Let the new value
504 * over-ride the old one.
509 lrad_hash_table_delete(attributes_byvalue, a);
511 if (!lrad_hash_table_replace(attributes_byname, attr)) {
512 librad_log("dict_addattr: Internal error storing attribute %s", name);
519 * Insert the SAME pointer (not free'd when this entry is
520 * deleted), into another table.
522 * We want this behaviour because we want OLD names for
523 * the attributes to be read from the configuration
524 * files, but when we're printing them, (and looking up
525 * by value) we want to use the NEW name.
527 if (!lrad_hash_table_replace(attributes_byvalue, attr)) {
528 librad_log("dict_addattr: Failed inserting attribute name %s", name);
532 if (!vendor && (value > 0) && (value < 256)) {
533 dict_base_attrs[value] = attr;
541 * Add a value for an attribute to the dictionary.
543 int dict_addvalue(const char *namestr, const char *attrstr, int value)
550 librad_log("dict_addvalue: empty names are not permitted");
554 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
555 librad_log("dict_addvalue: value name too long");
559 if ((dval = malloc(sizeof(*dval) + length)) == NULL) {
560 librad_log("dict_addvalue: out of memory");
563 memset(dval, 0, sizeof(*dval));
565 strcpy(dval->name, namestr);
569 * Remember which attribute is associated with this
570 * value, if possible.
572 dattr = dict_attrbyname(attrstr);
574 if (dattr->flags.has_value_alias) {
575 librad_log("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
579 dval->attr = dattr->attr;
582 * Enforce valid values
584 * Don't worry about fixups...
586 switch (dattr->type) {
590 librad_log("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
597 librad_log("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
603 * Allow octets for now, because
604 * of dictionary.cablelabs
608 case PW_TYPE_INTEGER:
613 librad_log("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
614 lrad_int2str(type_table, dattr->type, "?Unknown?"));
618 dattr->flags.has_value = 1;
620 value_fixup_t *fixup;
622 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
625 librad_log("dict_addvalue: out of memory");
628 memset(fixup, 0, sizeof(*fixup));
630 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
634 * Insert to the head of the list.
636 fixup->next = value_fixup;
643 * Add the value into the dictionary.
645 if (!lrad_hash_table_insert(values_byname, dval)) {
650 * Suppress duplicates with the same
651 * name and value. There are lots in
654 old = dict_valbyname(dattr->attr, namestr);
655 if (old && (old->value == dval->value)) {
662 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
667 * There are multiple VALUE's, keyed by attribute, so we
668 * take care of that here.
670 if (!lrad_hash_table_replace(values_byvalue, dval)) {
671 librad_log("dict_addvalue: Failed inserting value %s",
680 * Process the ATTRIBUTE command
682 static int process_attribute(const char* fn, const int line,
683 const int block_vendor, char **argv,
691 if ((argc < 3) || (argc > 4)) {
692 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
698 * Validate all entries
700 if (!isdigit((int) argv[1][0])) {
701 librad_log("dict_init: %s[%d]: invalid value", fn, line);
704 sscanf(argv[1], "%i", &value);
707 * find the type of the attribute.
709 type = lrad_str2int(type_table, argv[2], -1);
711 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
717 * Only look up the vendor if the string
720 memset(&flags, 0, sizeof(flags));
722 char *key, *next, *last;
726 next = strchr(key, ',');
727 if (next) *(next++) = '\0';
729 if (strcmp(key, "has_tag") == 0 ||
730 strcmp(key, "has_tag=1") == 0) {
731 /* Boolean flag, means this is a
735 } else if (strncmp(key, "encrypt=", 8) == 0) {
736 /* Encryption method, defaults to 0 (none).
737 Currently valid is just type 2,
738 Tunnel-Password style, which can only
739 be applied to strings. */
740 flags.encrypt = strtol(key + 8, &last, 0);
742 librad_log( "dict_init: %s[%d] invalid option %s",
747 } else if (strncmp(key, "array", 8) == 0) {
754 case PW_TYPE_INTEGER:
759 librad_log( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
764 } else if (block_vendor) {
765 librad_log( "dict_init: %s[%d]: unknown option \"%s\"",
770 /* Must be a vendor 'flag'... */
771 if (strncmp(key, "vendor=", 7) == 0) {
776 vendor = dict_vendorbyname(key);
778 librad_log( "dict_init: %s[%d]: unknown vendor \"%s\"",
782 if (block_vendor && argv[3][0] &&
783 (block_vendor != vendor)) {
784 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
791 if (key && !*key) break;
795 if (block_vendor) vendor = block_vendor;
798 * Special checks for tags, they make our life much more
803 * Only string, octets, and integer can be tagged.
807 case PW_TYPE_INTEGER:
811 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
813 lrad_int2str(type_table, type, "?Unknown?"));
822 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
823 librad_log("dict_init: %s[%d]: %s",
824 fn, line, librad_errstr);
833 * Process the VALUE command
835 static int process_value(const char* fn, const int line, char **argv,
841 librad_log("dict_init: %s[%d]: invalid VALUE line",
846 * For Compatibility, skip "Server-Config"
848 if (strcasecmp(argv[0], "Server-Config") == 0)
852 * Validate all entries
854 if (!isdigit((int) argv[2][0])) {
855 librad_log("dict_init: %s[%d]: invalid value",
859 sscanf(argv[2], "%i", &value);
861 if (dict_addvalue(argv[1], argv[0], value) < 0) {
862 librad_log("dict_init: %s[%d]: %s",
863 fn, line, librad_errstr);
872 * Process the VALUE-ALIAS command
874 * This allows VALUE mappings to be shared among multiple
877 static int process_value_alias(const char* fn, const int line, char **argv,
880 DICT_ATTR *my_da, *da;
884 librad_log("dict_init: %s[%d]: invalid VALUE-ALIAS line",
889 my_da = dict_attrbyname(argv[0]);
891 librad_log("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
896 if (my_da->flags.has_value) {
897 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
902 if (my_da->flags.has_value_alias) {
903 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
908 da = dict_attrbyname(argv[1]);
910 librad_log("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
915 if (!da->flags.has_value) {
916 librad_log("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
921 if (da->flags.has_value_alias) {
922 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
927 if (my_da->type != da->type) {
928 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
933 if ((dval = malloc(sizeof(*dval))) == NULL) {
934 librad_log("dict_addvalue: out of memory");
937 memset(dval, 0, sizeof(*dval));
939 dval->name[0] = '\0'; /* empty name */
940 dval->attr = my_da->attr;
941 dval->value = da->attr;
943 if (!lrad_hash_table_insert(values_byname, dval)) {
944 librad_log("dict_init: %s[%d]: Error create alias",
955 * Process the VENDOR command
957 static int process_vendor(const char* fn, const int line, char **argv,
961 const char *format = NULL;
963 if ((argc < 2) || (argc > 3)) {
964 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
970 * Validate all entries
972 if (!isdigit((int) argv[1][0])) {
973 librad_log("dict_init: %s[%d]: invalid value",
977 value = atoi(argv[1]);
979 /* Create a new VENDOR entry for the list */
980 if (dict_addvendor(argv[0], value) < 0) {
981 librad_log("dict_init: %s[%d]: %s",
982 fn, line, librad_errstr);
987 * Look for a format statement
992 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
993 format = "format=4,0";
995 } else if (value == VENDORPEC_LUCENT) {
996 format = "format=2,1";
998 } else if (value == VENDORPEC_STARENT) {
999 format = "format=2,2";
1001 } /* else no fixups to do */
1008 if (strncasecmp(format, "format=", 7) != 0) {
1009 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1015 if ((strlen(p) != 3) ||
1016 !isdigit((int) p[0]) ||
1018 !isdigit((int) p[2])) {
1019 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1024 type = (int) (p[0] - '0');
1025 length = (int) (p[2] - '0');
1027 dv = dict_vendorbyvalue(value);
1029 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
1034 if ((type != 1) && (type != 2) && (type != 4)) {
1035 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
1040 if ((length != 0) && (length != 1) && (length != 2)) {
1041 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
1047 dv->length = length;
1054 * String split routine. Splits an input string IN PLACE
1055 * into pieces, based on spaces.
1057 static int str2argv(char *str, char **argv, int max_argc)
1062 if (argc >= max_argc) return argc;
1065 * Chop out comments early.
1072 while ((*str == ' ') ||
1075 (*str == '\n')) *(str++) = '\0';
1077 if (!*str) return argc;
1086 (*str != '\n')) str++;
1092 #define MAX_ARGV (16)
1095 * Initialize the dictionary.
1097 static int my_dict_init(const char *dir, const char *fn,
1098 const char *src_file, int src_line)
1107 struct stat statbuf;
1108 char *argv[MAX_ARGV];
1111 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1112 strlen(dir) >= sizeof(dirtmp) / 2) {
1113 librad_log("dict_init: filename name too long");
1118 * First see if fn is relative to dir. If so, create
1119 * new filename. If not, remember the absolute dir.
1121 if ((p = strrchr(fn, '/')) != NULL) {
1125 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1126 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1130 if ((fp = fopen(fn, "r")) == NULL) {
1132 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
1133 fn, strerror(errno));
1135 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1136 src_file, src_line, fn, strerror(errno));
1141 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1142 if (!S_ISREG(statbuf.st_mode)) {
1144 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
1150 * Globally writable dictionaries means that users can control
1151 * the server configuration with little difficulty.
1154 if ((statbuf.st_mode & S_IWOTH) != 0) {
1156 librad_log("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1162 dict_stat_add(fn, &statbuf);
1165 * Seed the random pool with data.
1167 lrad_rand_seed(&statbuf, sizeof(statbuf));
1171 while (fgets(buf, sizeof(buf), fp) != NULL) {
1173 if (buf[0] == '#' || buf[0] == 0 ||
1174 buf[0] == '\n' || buf[0] == '\r')
1178 * Comment characters should NOT be appearing anywhere but
1179 * as start of a comment;
1181 p = strchr(buf, '#');
1184 argc = str2argv(buf, argv, MAX_ARGV);
1185 if (argc == 0) continue;
1188 librad_log( "dict_init: %s[%d] invalid entry",
1197 fprintf(stderr, "ARGC = %d\n",argc);
1198 for (i = 0; i < argc; i++) {
1199 fprintf(stderr, "\t%s\n", argv[i]);
1204 * See if we need to import another dictionary.
1206 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1207 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1215 * Perhaps this is an attribute.
1217 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1218 if (process_attribute(fn, line, block_vendor,
1219 argv + 1, argc - 1) == -1) {
1227 * Process VALUE lines.
1229 if (strcasecmp(argv[0], "VALUE") == 0) {
1230 if (process_value(fn, line,
1231 argv + 1, argc - 1) == -1) {
1238 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1239 if (process_value_alias(fn, line,
1240 argv + 1, argc - 1) == -1) {
1248 * Process VENDOR lines.
1250 if (strcasecmp(argv[0], "VENDOR") == 0) {
1251 if (process_vendor(fn, line,
1252 argv + 1, argc - 1) == -1) {
1259 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1262 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1268 vendor = dict_vendorbyname(argv[1]);
1271 "dict_init: %s[%d]: unknown vendor %s",
1276 block_vendor = vendor;
1278 } /* BEGIN-VENDOR */
1280 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1283 "dict_init: %s[%d] invalid END-VENDOR entry",
1289 vendor = dict_vendorbyname(argv[1]);
1292 "dict_init: %s[%d]: unknown vendor %s",
1298 if (vendor != block_vendor) {
1300 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1310 * Any other string: We don't recognize it.
1313 "dict_init: %s[%d] invalid keyword \"%s\"",
1324 * Empty callback for hash table initialization.
1326 static int null_callback(void *ctx, void *data)
1328 ctx = ctx; /* -Wunused */
1329 data = data; /* -Wunused */
1336 * Initialize the directory, then fix the attr member of
1339 int dict_init(const char *dir, const char *fn)
1342 * Check if we need to change anything. If not, don't do
1345 if (dict_stat_check(dir, fn)) {
1350 * Free the dictionaries, and the stat cache.
1353 stat_root_dir = strdup(dir);
1354 stat_root_file = strdup(fn);
1357 * Create the table of vendor by name. There MAY NOT
1358 * be multiple vendors of the same name.
1360 * Each vendor is malloc'd, so the free function is free.
1362 vendors_byname = lrad_hash_table_create(dict_vendor_name_hash,
1363 dict_vendor_name_cmp,
1365 if (!vendors_byname) {
1370 * Create the table of vendors by value. There MAY
1371 * be vendors of the same value. If there are, we
1372 * pick the latest one.
1374 vendors_byvalue = lrad_hash_table_create(dict_vendor_value_hash,
1375 dict_vendor_value_cmp,
1377 if (!vendors_byvalue) {
1382 * Create the table of attributes by name. There MAY NOT
1383 * be multiple attributes of the same name.
1385 * Each attribute is malloc'd, so the free function is free.
1387 attributes_byname = lrad_hash_table_create(dict_attr_name_hash,
1390 if (!attributes_byname) {
1395 * Create the table of attributes by value. There MAY
1396 * be attributes of the same value. If there are, we
1397 * pick the latest one.
1399 attributes_byvalue = lrad_hash_table_create(dict_attr_value_hash,
1400 dict_attr_value_cmp,
1402 if (!attributes_byvalue) {
1406 values_byname = lrad_hash_table_create(dict_value_name_hash,
1407 dict_value_name_cmp,
1409 if (!values_byname) {
1413 values_byvalue = lrad_hash_table_create(dict_value_value_hash,
1414 dict_value_value_cmp,
1416 if (!values_byvalue) {
1420 value_fixup = NULL; /* just to be safe. */
1422 if (my_dict_init(dir, fn, NULL, 0) < 0)
1427 value_fixup_t *this, *next;
1429 for (this = value_fixup; this != NULL; this = next) {
1432 a = dict_attrbyname(this->attrstr);
1435 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1436 this->attrstr, this->dval->name);
1437 return -1; /* leak, but they should die... */
1440 this->dval->attr = a->attr;
1443 * Add the value into the dictionary.
1445 if (!lrad_hash_table_replace(values_byname,
1447 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1452 * Allow them to use the old name, but
1453 * prefer the new name when printing
1456 if (!lrad_hash_table_finddata(values_byvalue, this->dval)) {
1457 lrad_hash_table_replace(values_byvalue,
1463 * Just so we don't lose track of things.
1470 * Walk over all of the hash tables to ensure they're
1471 * initialized. We do this because the threads may perform
1472 * lookups, and we don't want multi-threaded re-ordering
1473 * of the table entries. That would be bad.
1475 lrad_hash_table_walk(vendors_byname, null_callback, NULL);
1476 lrad_hash_table_walk(vendors_byvalue, null_callback, NULL);
1478 lrad_hash_table_walk(attributes_byname, null_callback, NULL);
1479 lrad_hash_table_walk(attributes_byvalue, null_callback, NULL);
1481 lrad_hash_table_walk(values_byvalue, null_callback, NULL);
1482 lrad_hash_table_walk(values_byname, null_callback, NULL);
1488 * Get an attribute by its numerical value.
1490 DICT_ATTR *dict_attrbyvalue(int attr)
1494 if ((attr > 0) && (attr < 256)) return dict_base_attrs[attr];
1497 dattr.vendor = VENDOR(attr) & 0x7fff;
1499 return lrad_hash_table_finddata(attributes_byvalue, &dattr);
1503 * Get an attribute by its name.
1505 DICT_ATTR *dict_attrbyname(const char *name)
1509 if (!name) return NULL;
1511 strlcpy(dattr.name, name, sizeof(dattr.name));
1513 return lrad_hash_table_finddata(attributes_byname, &dattr);
1517 * Associate a value with an attribute and return it.
1519 DICT_VALUE *dict_valbyattr(int attr, int value)
1521 DICT_VALUE dval, *dv;
1524 * First, look up aliases.
1527 dval.name[0] = '\0';
1530 * Look up the attribute alias target, and use
1531 * the correct attribute number if found.
1533 dv = lrad_hash_table_finddata(values_byname, &dval);
1534 if (dv) dval.attr = dv->value;
1538 return lrad_hash_table_finddata(values_byvalue, &dval);
1542 * Get a value by its name, keyed off of an attribute.
1544 DICT_VALUE *dict_valbyname(int attr, const char *name)
1546 DICT_VALUE *my_dv, *dv;
1547 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1549 if (!name) return NULL;
1551 my_dv = (DICT_VALUE *) buffer;
1553 my_dv->name[0] = '\0';
1556 * Look up the attribute alias target, and use
1557 * the correct attribute number if found.
1559 dv = lrad_hash_table_finddata(values_byname, my_dv);
1560 if (dv) my_dv->attr = dv->value;
1562 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN);
1564 return lrad_hash_table_finddata(values_byname, my_dv);
1568 * Get the vendor PEC based on the vendor name
1570 * This is efficient only for small numbers of vendors.
1572 int dict_vendorbyname(const char *name)
1575 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1577 if (!name) return 0;
1579 dv = (DICT_VENDOR *) buffer;
1580 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN);
1582 dv = lrad_hash_table_finddata(vendors_byname, dv);
1585 return dv->vendorpec;
1589 * Return the vendor struct based on the PEC.
1591 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1595 dv.vendorpec = vendorpec;
1597 return lrad_hash_table_finddata(vendors_byvalue, &dv);