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",
679 static int sscanf_i(const char *str, int *pvalue)
683 const char *tab = "0123456789";
685 if ((str[0] == '0') &&
686 ((str[1] == 'x') || (str[1] == 'X'))) {
687 tab = "0123456789abcdef";
696 c = memchr(tab, tolower((int) *str), base);
710 * Process the ATTRIBUTE command
712 static int process_attribute(const char* fn, const int line,
713 const int block_vendor, char **argv,
721 if ((argc < 3) || (argc > 4)) {
722 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
728 * Validate all entries
730 if (!sscanf_i(argv[1], &value)) {
731 librad_log("dict_init: %s[%d]: invalid value", fn, line);
736 * find the type of the attribute.
738 type = lrad_str2int(type_table, argv[2], -1);
740 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
746 * Only look up the vendor if the string
749 memset(&flags, 0, sizeof(flags));
751 char *key, *next, *last;
755 next = strchr(key, ',');
756 if (next) *(next++) = '\0';
758 if (strcmp(key, "has_tag") == 0 ||
759 strcmp(key, "has_tag=1") == 0) {
760 /* Boolean flag, means this is a
764 } else if (strncmp(key, "encrypt=", 8) == 0) {
765 /* Encryption method, defaults to 0 (none).
766 Currently valid is just type 2,
767 Tunnel-Password style, which can only
768 be applied to strings. */
769 flags.encrypt = strtol(key + 8, &last, 0);
771 librad_log( "dict_init: %s[%d] invalid option %s",
776 } else if (strncmp(key, "array", 8) == 0) {
783 case PW_TYPE_INTEGER:
788 librad_log( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
793 } else if (block_vendor) {
794 librad_log( "dict_init: %s[%d]: unknown option \"%s\"",
799 /* Must be a vendor 'flag'... */
800 if (strncmp(key, "vendor=", 7) == 0) {
805 vendor = dict_vendorbyname(key);
807 librad_log( "dict_init: %s[%d]: unknown vendor \"%s\"",
811 if (block_vendor && argv[3][0] &&
812 (block_vendor != vendor)) {
813 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
820 if (key && !*key) break;
824 if (block_vendor) vendor = block_vendor;
827 * Special checks for tags, they make our life much more
832 * Only string, octets, and integer can be tagged.
836 case PW_TYPE_INTEGER:
840 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
842 lrad_int2str(type_table, type, "?Unknown?"));
851 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
852 librad_log("dict_init: %s[%d]: %s",
853 fn, line, librad_errstr);
862 * Process the VALUE command
864 static int process_value(const char* fn, const int line, char **argv,
870 librad_log("dict_init: %s[%d]: invalid VALUE line",
875 * For Compatibility, skip "Server-Config"
877 if (strcasecmp(argv[0], "Server-Config") == 0)
881 * Validate all entries
883 if (!sscanf_i(argv[2], &value)) {
884 librad_log("dict_init: %s[%d]: invalid value",
889 if (dict_addvalue(argv[1], argv[0], value) < 0) {
890 librad_log("dict_init: %s[%d]: %s",
891 fn, line, librad_errstr);
900 * Process the VALUE-ALIAS command
902 * This allows VALUE mappings to be shared among multiple
905 static int process_value_alias(const char* fn, const int line, char **argv,
908 DICT_ATTR *my_da, *da;
912 librad_log("dict_init: %s[%d]: invalid VALUE-ALIAS line",
917 my_da = dict_attrbyname(argv[0]);
919 librad_log("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
924 if (my_da->flags.has_value) {
925 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
930 if (my_da->flags.has_value_alias) {
931 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
936 da = dict_attrbyname(argv[1]);
938 librad_log("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
943 if (!da->flags.has_value) {
944 librad_log("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
949 if (da->flags.has_value_alias) {
950 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
955 if (my_da->type != da->type) {
956 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
961 if ((dval = malloc(sizeof(*dval))) == NULL) {
962 librad_log("dict_addvalue: out of memory");
965 memset(dval, 0, sizeof(*dval));
967 dval->name[0] = '\0'; /* empty name */
968 dval->attr = my_da->attr;
969 dval->value = da->attr;
971 if (!lrad_hash_table_insert(values_byname, dval)) {
972 librad_log("dict_init: %s[%d]: Error create alias",
983 * Process the VENDOR command
985 static int process_vendor(const char* fn, const int line, char **argv,
989 const char *format = NULL;
991 if ((argc < 2) || (argc > 3)) {
992 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
998 * Validate all entries
1000 if (!isdigit((int) argv[1][0])) {
1001 librad_log("dict_init: %s[%d]: invalid value",
1005 value = atoi(argv[1]);
1007 /* Create a new VENDOR entry for the list */
1008 if (dict_addvendor(argv[0], value) < 0) {
1009 librad_log("dict_init: %s[%d]: %s",
1010 fn, line, librad_errstr);
1015 * Look for a format statement
1020 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1021 format = "format=4,0";
1023 } else if (value == VENDORPEC_LUCENT) {
1024 format = "format=2,1";
1026 } else if (value == VENDORPEC_STARENT) {
1027 format = "format=2,2";
1029 } /* else no fixups to do */
1036 if (strncasecmp(format, "format=", 7) != 0) {
1037 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1043 if ((strlen(p) != 3) ||
1044 !isdigit((int) p[0]) ||
1046 !isdigit((int) p[2])) {
1047 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1052 type = (int) (p[0] - '0');
1053 length = (int) (p[2] - '0');
1055 dv = dict_vendorbyvalue(value);
1057 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
1062 if ((type != 1) && (type != 2) && (type != 4)) {
1063 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
1068 if ((length != 0) && (length != 1) && (length != 2)) {
1069 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
1075 dv->length = length;
1082 * String split routine. Splits an input string IN PLACE
1083 * into pieces, based on spaces.
1085 static int str2argv(char *str, char **argv, int max_argc)
1090 if (argc >= max_argc) return argc;
1093 * Chop out comments early.
1100 while ((*str == ' ') ||
1103 (*str == '\n')) *(str++) = '\0';
1105 if (!*str) return argc;
1114 (*str != '\n')) str++;
1120 #define MAX_ARGV (16)
1123 * Initialize the dictionary.
1125 static int my_dict_init(const char *dir, const char *fn,
1126 const char *src_file, int src_line)
1135 struct stat statbuf;
1136 char *argv[MAX_ARGV];
1139 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1140 strlen(dir) >= sizeof(dirtmp) / 2) {
1141 librad_log("dict_init: filename name too long");
1146 * First see if fn is relative to dir. If so, create
1147 * new filename. If not, remember the absolute dir.
1149 if ((p = strrchr(fn, '/')) != NULL) {
1153 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1154 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1158 if ((fp = fopen(fn, "r")) == NULL) {
1160 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
1161 fn, strerror(errno));
1163 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1164 src_file, src_line, fn, strerror(errno));
1169 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1170 if (!S_ISREG(statbuf.st_mode)) {
1172 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
1178 * Globally writable dictionaries means that users can control
1179 * the server configuration with little difficulty.
1182 if ((statbuf.st_mode & S_IWOTH) != 0) {
1184 librad_log("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1190 dict_stat_add(fn, &statbuf);
1193 * Seed the random pool with data.
1195 lrad_rand_seed(&statbuf, sizeof(statbuf));
1199 while (fgets(buf, sizeof(buf), fp) != NULL) {
1201 if (buf[0] == '#' || buf[0] == 0 ||
1202 buf[0] == '\n' || buf[0] == '\r')
1206 * Comment characters should NOT be appearing anywhere but
1207 * as start of a comment;
1209 p = strchr(buf, '#');
1212 argc = str2argv(buf, argv, MAX_ARGV);
1213 if (argc == 0) continue;
1216 librad_log( "dict_init: %s[%d] invalid entry",
1225 fprintf(stderr, "ARGC = %d\n",argc);
1226 for (i = 0; i < argc; i++) {
1227 fprintf(stderr, "\t%s\n", argv[i]);
1232 * See if we need to import another dictionary.
1234 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1235 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1243 * Perhaps this is an attribute.
1245 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1246 if (process_attribute(fn, line, block_vendor,
1247 argv + 1, argc - 1) == -1) {
1255 * Process VALUE lines.
1257 if (strcasecmp(argv[0], "VALUE") == 0) {
1258 if (process_value(fn, line,
1259 argv + 1, argc - 1) == -1) {
1266 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1267 if (process_value_alias(fn, line,
1268 argv + 1, argc - 1) == -1) {
1276 * Process VENDOR lines.
1278 if (strcasecmp(argv[0], "VENDOR") == 0) {
1279 if (process_vendor(fn, line,
1280 argv + 1, argc - 1) == -1) {
1287 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1290 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1296 vendor = dict_vendorbyname(argv[1]);
1299 "dict_init: %s[%d]: unknown vendor %s",
1304 block_vendor = vendor;
1306 } /* BEGIN-VENDOR */
1308 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1311 "dict_init: %s[%d] invalid END-VENDOR entry",
1317 vendor = dict_vendorbyname(argv[1]);
1320 "dict_init: %s[%d]: unknown vendor %s",
1326 if (vendor != block_vendor) {
1328 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1338 * Any other string: We don't recognize it.
1341 "dict_init: %s[%d] invalid keyword \"%s\"",
1352 * Empty callback for hash table initialization.
1354 static int null_callback(void *ctx, void *data)
1356 ctx = ctx; /* -Wunused */
1357 data = data; /* -Wunused */
1364 * Initialize the directory, then fix the attr member of
1367 int dict_init(const char *dir, const char *fn)
1370 * Check if we need to change anything. If not, don't do
1373 if (dict_stat_check(dir, fn)) {
1378 * Free the dictionaries, and the stat cache.
1381 stat_root_dir = strdup(dir);
1382 stat_root_file = strdup(fn);
1385 * Create the table of vendor by name. There MAY NOT
1386 * be multiple vendors of the same name.
1388 * Each vendor is malloc'd, so the free function is free.
1390 vendors_byname = lrad_hash_table_create(dict_vendor_name_hash,
1391 dict_vendor_name_cmp,
1393 if (!vendors_byname) {
1398 * Create the table of vendors by value. There MAY
1399 * be vendors of the same value. If there are, we
1400 * pick the latest one.
1402 vendors_byvalue = lrad_hash_table_create(dict_vendor_value_hash,
1403 dict_vendor_value_cmp,
1405 if (!vendors_byvalue) {
1410 * Create the table of attributes by name. There MAY NOT
1411 * be multiple attributes of the same name.
1413 * Each attribute is malloc'd, so the free function is free.
1415 attributes_byname = lrad_hash_table_create(dict_attr_name_hash,
1418 if (!attributes_byname) {
1423 * Create the table of attributes by value. There MAY
1424 * be attributes of the same value. If there are, we
1425 * pick the latest one.
1427 attributes_byvalue = lrad_hash_table_create(dict_attr_value_hash,
1428 dict_attr_value_cmp,
1430 if (!attributes_byvalue) {
1434 values_byname = lrad_hash_table_create(dict_value_name_hash,
1435 dict_value_name_cmp,
1437 if (!values_byname) {
1441 values_byvalue = lrad_hash_table_create(dict_value_value_hash,
1442 dict_value_value_cmp,
1444 if (!values_byvalue) {
1448 value_fixup = NULL; /* just to be safe. */
1450 if (my_dict_init(dir, fn, NULL, 0) < 0)
1455 value_fixup_t *this, *next;
1457 for (this = value_fixup; this != NULL; this = next) {
1460 a = dict_attrbyname(this->attrstr);
1463 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1464 this->attrstr, this->dval->name);
1465 return -1; /* leak, but they should die... */
1468 this->dval->attr = a->attr;
1471 * Add the value into the dictionary.
1473 if (!lrad_hash_table_replace(values_byname,
1475 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1480 * Allow them to use the old name, but
1481 * prefer the new name when printing
1484 if (!lrad_hash_table_finddata(values_byvalue, this->dval)) {
1485 lrad_hash_table_replace(values_byvalue,
1491 * Just so we don't lose track of things.
1498 * Walk over all of the hash tables to ensure they're
1499 * initialized. We do this because the threads may perform
1500 * lookups, and we don't want multi-threaded re-ordering
1501 * of the table entries. That would be bad.
1503 lrad_hash_table_walk(vendors_byname, null_callback, NULL);
1504 lrad_hash_table_walk(vendors_byvalue, null_callback, NULL);
1506 lrad_hash_table_walk(attributes_byname, null_callback, NULL);
1507 lrad_hash_table_walk(attributes_byvalue, null_callback, NULL);
1509 lrad_hash_table_walk(values_byvalue, null_callback, NULL);
1510 lrad_hash_table_walk(values_byname, null_callback, NULL);
1516 * Get an attribute by its numerical value.
1518 DICT_ATTR *dict_attrbyvalue(int attr)
1522 if ((attr > 0) && (attr < 256)) return dict_base_attrs[attr];
1525 dattr.vendor = VENDOR(attr) & 0x7fff;
1527 return lrad_hash_table_finddata(attributes_byvalue, &dattr);
1531 * Get an attribute by its name.
1533 DICT_ATTR *dict_attrbyname(const char *name)
1537 if (!name) return NULL;
1539 strlcpy(dattr.name, name, sizeof(dattr.name));
1541 return lrad_hash_table_finddata(attributes_byname, &dattr);
1545 * Associate a value with an attribute and return it.
1547 DICT_VALUE *dict_valbyattr(int attr, int value)
1549 DICT_VALUE dval, *dv;
1552 * First, look up aliases.
1555 dval.name[0] = '\0';
1558 * Look up the attribute alias target, and use
1559 * the correct attribute number if found.
1561 dv = lrad_hash_table_finddata(values_byname, &dval);
1562 if (dv) dval.attr = dv->value;
1566 return lrad_hash_table_finddata(values_byvalue, &dval);
1570 * Get a value by its name, keyed off of an attribute.
1572 DICT_VALUE *dict_valbyname(int attr, const char *name)
1574 DICT_VALUE *my_dv, *dv;
1575 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1577 if (!name) return NULL;
1579 my_dv = (DICT_VALUE *) buffer;
1581 my_dv->name[0] = '\0';
1584 * Look up the attribute alias target, and use
1585 * the correct attribute number if found.
1587 dv = lrad_hash_table_finddata(values_byname, my_dv);
1588 if (dv) my_dv->attr = dv->value;
1590 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN);
1592 return lrad_hash_table_finddata(values_byname, my_dv);
1596 * Get the vendor PEC based on the vendor name
1598 * This is efficient only for small numbers of vendors.
1600 int dict_vendorbyname(const char *name)
1603 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1605 if (!name) return 0;
1607 dv = (DICT_VENDOR *) buffer;
1608 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN);
1610 dv = lrad_hash_table_finddata(vendors_byname, dv);
1613 return dv->vendorpec;
1617 * Return the vendor struct based on the PEC.
1619 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1623 dv.vendorpec = vendorpec;
1625 return lrad_hash_table_finddata(vendors_byvalue, &dv);