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;
51 * For faster HUP's, we cache the stat information for
52 * files we've $INCLUDEd
54 typedef struct dict_stat_t {
55 struct dict_stat_t *next;
60 static char *stat_root_dir = NULL;
61 static char *stat_root_file = NULL;
63 static dict_stat_t *stat_head = NULL;
64 static dict_stat_t *stat_tail = NULL;
66 typedef struct value_fixup_t {
69 struct value_fixup_t *next;
74 * So VALUEs in the dictionary can have forward references.
76 static value_fixup_t *value_fixup = NULL;
78 static const LRAD_NAME_NUMBER type_table[] = {
79 { "string", PW_TYPE_STRING },
80 { "integer", PW_TYPE_INTEGER },
81 { "ipaddr", PW_TYPE_IPADDR },
82 { "date", PW_TYPE_DATE },
83 { "abinary", PW_TYPE_ABINARY },
84 { "octets", PW_TYPE_OCTETS },
85 { "ifid", PW_TYPE_IFID },
86 { "ipv6addr", PW_TYPE_IPV6ADDR },
87 { "ipv6prefix", PW_TYPE_IPV6PREFIX },
88 { "byte", PW_TYPE_BYTE },
89 { "short", PW_TYPE_SHORT },
95 * Create the hash of the name.
97 * We copy the hash function here because it's substantially faster.
99 #define FNV_MAGIC_INIT (0x811c9dc5)
100 #define FNV_MAGIC_PRIME (0x01000193)
102 static uint32_t dict_hashname(const char *name)
104 uint32_t hash = FNV_MAGIC_INIT;
107 for (p = name; *p != '\0'; p++) {
108 int c = *(const unsigned char *) p;
109 if (isalpha(c)) c = tolower(c);
111 hash *= FNV_MAGIC_PRIME;
112 hash ^= (uint32_t ) (c & 0xff);
120 * Hash callback functions.
122 static uint32_t dict_attr_name_hash(const void *data)
124 return dict_hashname(((const DICT_ATTR *)data)->name);
127 static int dict_attr_name_cmp(const void *one, const void *two)
129 const DICT_ATTR *a = one;
130 const DICT_ATTR *b = two;
132 return strcasecmp(a->name, b->name);
135 static uint32_t dict_attr_value_hash(const void *data)
138 const DICT_ATTR *attr = data;
140 hash = lrad_hash(&attr->vendor, sizeof(attr->vendor));
141 return lrad_hash_update(&attr->attr, sizeof(attr->attr), hash);
144 static int dict_attr_value_cmp(const void *one, const void *two)
146 const DICT_ATTR *a = one;
147 const DICT_ATTR *b = two;
149 if (a->vendor < b->vendor) return -1;
150 if (a->vendor > b->vendor) return +1;
152 return a->attr - b->attr;
155 static uint32_t dict_vendor_name_hash(const void *data)
157 return dict_hashname(((const DICT_VENDOR *)data)->name);
160 static int dict_vendor_name_cmp(const void *one, const void *two)
162 const DICT_VENDOR *a = one;
163 const DICT_VENDOR *b = two;
165 return strcasecmp(a->name, b->name);
168 static uint32_t dict_vendor_value_hash(const void *data)
170 return lrad_hash(&(((const DICT_VENDOR *)data)->vendorpec),
171 sizeof(((const DICT_VENDOR *)data)->vendorpec));
174 static int dict_vendor_value_cmp(const void *one, const void *two)
176 const DICT_VENDOR *a = one;
177 const DICT_VENDOR *b = two;
179 return a->vendorpec - b->vendorpec;
182 static uint32_t dict_value_name_hash(const void *data)
185 const DICT_VALUE *dval = data;
187 hash = dict_hashname(dval->name);
188 return lrad_hash_update(&dval->attr, sizeof(dval->attr), hash);
191 static int dict_value_name_cmp(const void *one, const void *two)
194 const DICT_VALUE *a = one;
195 const DICT_VALUE *b = two;
197 rcode = a->attr - b->attr;
198 if (rcode != 0) return rcode;
200 return strcasecmp(a->name, b->name);
203 static uint32_t dict_value_value_hash(const void *data)
206 const DICT_VALUE *dval = data;
208 hash = lrad_hash(&dval->attr, sizeof(dval->attr));
209 return lrad_hash_update(&dval->value, sizeof(dval->value), hash);
212 static int dict_value_value_cmp(const void *one, const void *two)
215 const DICT_VALUE *a = one;
216 const DICT_VALUE *b = two;
218 rcode = a->attr - b->attr;
219 if (rcode != 0) return rcode;
221 return a->value - b->value;
226 * Free the list of stat buffers
228 static void dict_stat_free(void)
230 dict_stat_t *this, *next;
233 stat_root_dir = NULL;
234 free(stat_root_file);
235 stat_root_file = NULL;
242 for (this = stat_head; this != NULL; this = next) {
248 stat_head = stat_tail = NULL;
253 * Add an entry to the list of stat buffers.
255 static void dict_stat_add(const char *name, const struct stat *stat_buf)
259 this = malloc(sizeof(*this));
261 memset(this, 0, sizeof(*this));
263 this->name = strdup(name);
264 this->mtime = stat_buf->st_mtime;
267 stat_head = stat_tail = this;
269 stat_tail->next = this;
276 * See if any dictionaries have changed. If not, don't
279 static int dict_stat_check(const char *root_dir, const char *root_file)
284 if (!stat_root_dir) return 0;
285 if (!stat_root_file) return 0;
287 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
288 if (strcmp(root_file, stat_root_file) != 0) return 0;
290 if (!stat_head) return 0; /* changed, reload */
292 for (this = stat_head; this != NULL; this = this->next) {
293 if (stat(this->name, &buf) < 0) return 0;
295 if (buf.st_mtime != this->mtime) return 0;
303 * Free the dictionary_attributes and dictionary_values lists.
310 lrad_hash_table_free(vendors_byname);
311 lrad_hash_table_free(vendors_byvalue);
312 vendors_byname = NULL;
313 vendors_byvalue = NULL;
315 lrad_hash_table_free(attributes_byname);
316 lrad_hash_table_free(attributes_byvalue);
317 attributes_byname = NULL;
318 attributes_byvalue = NULL;
320 lrad_hash_table_free(values_byname);
321 lrad_hash_table_free(values_byvalue);
322 values_byname = NULL;
323 values_byvalue = NULL;
330 * Add vendor to the list.
332 int dict_addvendor(const char *name, int value)
337 if (value >= 32767) {
338 librad_log("dict_addvendor: Cannot handle vendor ID larger than 65535");
342 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
343 librad_log("dict_addvendor: vendor name too long");
347 if ((dv = malloc(sizeof(*dv) + length)) == NULL) {
348 librad_log("dict_addvendor: out of memory");
352 strcpy(dv->name, name);
353 dv->vendorpec = value;
354 dv->type = dv->length = 1; /* defaults */
356 if (!lrad_hash_table_insert(vendors_byname, dv)) {
359 old_dv = lrad_hash_table_finddata(vendors_byname, dv);
361 librad_log("dict_addvendor: Failed inserting vendor name %s", name);
364 if (old_dv->vendorpec != dv->vendorpec) {
365 librad_log("dict_addvendor: Duplicate vendor name %s", name);
370 * Already inserted. Discard the duplicate entry.
377 * Insert the SAME pointer (not free'd when this table is
378 * deleted), into another table.
380 * We want this behaviour because we want OLD names for
381 * the attributes to be read from the configuration
382 * files, but when we're printing them, (and looking up
383 * by value) we want to use the NEW name.
385 if (!lrad_hash_table_replace(vendors_byvalue, dv)) {
386 librad_log("dict_addvendor: Failed inserting vendor %s",
395 * Add an attribute to the dictionary.
397 int dict_addattr(const char *name, int vendor, int type, int value,
400 static int max_attr = 0;
403 if (strlen(name) > (sizeof(attr->name) -1)) {
404 librad_log("dict_addattr: attribute name too long");
409 * If the value is '-1', that means use a pre-existing
410 * one (if it already exists). If one does NOT already exist,
411 * then create a new attribute, with a non-conflicting value,
415 if (dict_attrbyname(name)) {
416 return 0; /* exists, don't add it again */
421 } else if (vendor == 0) {
425 if (value > max_attr) {
431 librad_log("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
435 if (value >= 65536) {
436 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
441 DICT_VENDOR *dv = dict_vendorbyvalue(vendor);
444 * If the vendor isn't defined, die/
447 librad_log("dict_addattr: Unknown vendor");
452 * FIXME: Switch over dv->type, and limit things
455 if ((dv->type == 1) && (value >= 256)) {
456 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
458 } /* else 256..65535 are allowed */
462 * Create a new attribute for the list
464 if ((attr = malloc(sizeof(*attr))) == NULL) {
465 librad_log("dict_addattr: out of memory");
469 strcpy(attr->name, name);
471 attr->attr |= (vendor << 16); /* FIXME: hack */
472 attr->vendor = vendor;
475 attr->vendor = vendor;
478 * Insert the attribute, only if it's not a duplicate.
480 if (!lrad_hash_table_insert(attributes_byname, attr)) {
484 * If the attribute has identical number, then
485 * ignore the duplicate.
487 a = lrad_hash_table_finddata(attributes_byname, attr);
488 if (a && (strcasecmp(a->name, attr->name) == 0)) {
489 if (a->attr != attr->attr) {
490 librad_log("dict_addattr: Duplicate attribute name %s", name);
496 * Same name, same vendor, same attr,
497 * maybe the flags and/or type is
498 * different. Let the new value
499 * over-ride the old one.
504 lrad_hash_table_delete(attributes_byvalue, a);
506 if (!lrad_hash_table_replace(attributes_byname, attr)) {
507 librad_log("dict_addattr: Internal error storing attribute %s", name);
514 * Insert the SAME pointer (not free'd when this entry is
515 * deleted), into another table.
517 * We want this behaviour because we want OLD names for
518 * the attributes to be read from the configuration
519 * files, but when we're printing them, (and looking up
520 * by value) we want to use the NEW name.
522 if (!lrad_hash_table_replace(attributes_byvalue, attr)) {
523 librad_log("dict_addattr: Failed inserting attribute name %s", name);
532 * Add a value for an attribute to the dictionary.
534 int dict_addvalue(const char *namestr, const char *attrstr, int value)
541 librad_log("dict_addvalue: empty names are not permitted");
545 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
546 librad_log("dict_addvalue: value name too long");
550 if ((dval = malloc(sizeof(*dval) + length)) == NULL) {
551 librad_log("dict_addvalue: out of memory");
554 memset(dval, 0, sizeof(*dval));
556 strcpy(dval->name, namestr);
560 * Remember which attribute is associated with this
561 * value, if possible.
563 dattr = dict_attrbyname(attrstr);
565 if (dattr->flags.has_value_alias) {
566 librad_log("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
570 dval->attr = dattr->attr;
573 * Enforce valid values
575 * Don't worry about fixups...
577 switch (dattr->type) {
580 librad_log("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
586 librad_log("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
592 * Allow octets for now, because
593 * of dictionary.cablelabs
597 case PW_TYPE_INTEGER:
601 librad_log("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
602 lrad_int2str(type_table, dattr->type, "?Unknown?"));
606 dattr->flags.has_value = 1;
608 value_fixup_t *fixup;
610 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
612 librad_log("dict_addvalue: out of memory");
615 memset(fixup, 0, sizeof(*fixup));
617 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
621 * Insert to the head of the list.
623 fixup->next = value_fixup;
630 * Add the value into the dictionary.
632 if (!lrad_hash_table_insert(values_byname, dval)) {
637 * Suppress duplicates with the same
638 * name and value. There are lots in
641 old = dict_valbyname(dattr->attr, namestr);
642 if (old && (old->value == dval->value)) {
648 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
653 * There are multiple VALUE's, keyed by attribute, so we
654 * take care of that here.
656 if (!lrad_hash_table_replace(values_byvalue, dval)) {
657 librad_log("dict_addvalue: Failed inserting value %s",
666 * Process the ATTRIBUTE command
668 static int process_attribute(const char* fn, const int line,
669 const int block_vendor, char **argv,
678 if ((argc < 3) || (argc > 4)) {
679 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
685 * Validate all entries
687 if (!isdigit((int) argv[1][0])) {
688 librad_log("dict_init: %s[%d]: invalid value", fn, line);
691 sscanf(argv[1], "%i", &value);
694 * find the type of the attribute.
696 type = lrad_str2int(type_table, argv[2], -1);
698 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
704 * Only look up the vendor if the string
707 memset(&flags, 0, sizeof(flags));
710 * FIXME: replace strtok with str2argv
712 s = strtok(argv[3], ",");
714 if (strcmp(s, "has_tag") == 0 ||
715 strcmp(s, "has_tag=1") == 0) {
716 /* Boolean flag, means this is a
720 } else if (strncmp(s, "encrypt=", 8) == 0) {
721 /* Encryption method, defaults to 0 (none).
722 Currently valid is just type 2,
723 Tunnel-Password style, which can only
724 be applied to strings. */
725 flags.encrypt = strtol(s + 8, &c, 0);
727 librad_log( "dict_init: %s[%d] invalid option %s",
732 } else if (strncmp(s, "array", 8) == 0) {
739 case PW_TYPE_INTEGER:
744 librad_log( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
749 } else if (block_vendor) {
750 librad_log( "dict_init: %s[%d]: unknown option \"%s\"",
755 /* Must be a vendor 'flag'... */
756 if (strncmp(s, "vendor=", 7) == 0) {
761 vendor = dict_vendorbyname(s);
763 librad_log( "dict_init: %s[%d]: unknown vendor \"%s\"",
767 if (block_vendor && argv[3][0] &&
768 (block_vendor != vendor)) {
769 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
774 s = strtok(NULL, ",");
778 if (block_vendor) vendor = block_vendor;
781 * Special checks for tags, they make our life much more
786 * Only string, octets, and integer can be tagged.
790 case PW_TYPE_INTEGER:
794 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
796 lrad_int2str(type_table, type, "?Unknown?"));
805 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
806 librad_log("dict_init: %s[%d]: %s",
807 fn, line, librad_errstr);
816 * Process the VALUE command
818 static int process_value(const char* fn, const int line, char **argv,
824 librad_log("dict_init: %s[%d]: invalid VALUE line",
829 * For Compatibility, skip "Server-Config"
831 if (strcasecmp(argv[0], "Server-Config") == 0)
835 * Validate all entries
837 if (!isdigit((int) argv[2][0])) {
838 librad_log("dict_init: %s[%d]: invalid value",
842 sscanf(argv[2], "%i", &value);
844 if (dict_addvalue(argv[1], argv[0], value) < 0) {
845 librad_log("dict_init: %s[%d]: %s",
846 fn, line, librad_errstr);
855 * Process the VALUE-ALIAS command
857 * This allows VALUE mappings to be shared among multiple
860 static int process_value_alias(const char* fn, const int line, char **argv,
863 DICT_ATTR *my_da, *da;
867 librad_log("dict_init: %s[%d]: invalid VALUE-ALIAS line",
872 my_da = dict_attrbyname(argv[0]);
874 librad_log("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
879 if (my_da->flags.has_value) {
880 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
885 if (my_da->flags.has_value_alias) {
886 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
891 da = dict_attrbyname(argv[1]);
893 librad_log("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
898 if (!da->flags.has_value) {
899 librad_log("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
904 if (da->flags.has_value_alias) {
905 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
910 if (my_da->type != da->type) {
911 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
916 if ((dval = malloc(sizeof(*dval))) == NULL) {
917 librad_log("dict_addvalue: out of memory");
920 memset(dval, 0, sizeof(*dval));
922 dval->name[0] = '\0'; /* empty name */
923 dval->attr = my_da->attr;
924 dval->value = da->attr;
926 if (!lrad_hash_table_insert(values_byname, dval)) {
927 librad_log("dict_init: %s[%d]: Error create alias",
938 * Process the VENDOR command
940 static int process_vendor(const char* fn, const int line, char **argv,
944 const char *format = NULL;
946 if ((argc < 2) || (argc > 3)) {
947 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
953 * Validate all entries
955 if (!isdigit((int) argv[1][0])) {
956 librad_log("dict_init: %s[%d]: invalid value",
960 value = atoi(argv[1]);
962 /* Create a new VENDOR entry for the list */
963 if (dict_addvendor(argv[0], value) < 0) {
964 librad_log("dict_init: %s[%d]: %s",
965 fn, line, librad_errstr);
970 * Look for a format statement
975 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
976 format = "format=4,0";
978 } else if (value == VENDORPEC_LUCENT) {
979 format = "format=2,1";
981 } else if (value == VENDORPEC_STARENT) {
982 format = "format=2,2";
984 } /* else no fixups to do */
991 if (strncasecmp(format, "format=", 7) != 0) {
992 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
998 if ((strlen(p) != 3) ||
999 !isdigit((int) p[0]) ||
1001 !isdigit((int) p[2])) {
1002 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1007 type = (int) (p[0] - '0');
1008 length = (int) (p[2] - '0');
1010 dv = dict_vendorbyvalue(value);
1012 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
1017 if ((type != 1) && (type != 2) && (type != 4)) {
1018 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
1023 if ((length != 0) && (length != 1) && (length != 2)) {
1024 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
1030 dv->length = length;
1037 * String split routine. Splits an input string IN PLACE
1038 * into pieces, based on spaces.
1040 static int str2argv(char *str, char **argv, int max_argc)
1045 if (argc >= max_argc) return argc;
1048 * Chop out comments early.
1055 while ((*str == ' ') ||
1058 (*str == '\n')) *(str++) = '\0';
1060 if (!*str) return argc;
1069 (*str != '\n')) str++;
1075 #define MAX_ARGV (16)
1078 * Initialize the dictionary.
1080 static int my_dict_init(const char *dir, const char *fn,
1081 const char *src_file, int src_line)
1090 struct stat statbuf;
1091 char *argv[MAX_ARGV];
1094 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1095 strlen(dir) >= sizeof(dirtmp) / 2) {
1096 librad_log("dict_init: filename name too long");
1101 * First see if fn is relative to dir. If so, create
1102 * new filename. If not, remember the absolute dir.
1104 if ((p = strrchr(fn, '/')) != NULL) {
1108 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1109 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1113 if ((fp = fopen(fn, "r")) == NULL) {
1115 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
1116 fn, strerror(errno));
1118 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1119 src_file, src_line, fn, strerror(errno));
1124 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1125 if (!S_ISREG(statbuf.st_mode)) {
1127 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
1133 * Globally writable dictionaries means that users can control
1134 * the server configuration with little difficulty.
1136 if ((statbuf.st_mode & S_IWOTH) != 0) {
1138 librad_log("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1143 dict_stat_add(fn, &statbuf);
1146 * Seed the random pool with data.
1148 lrad_rand_seed(&statbuf, sizeof(statbuf));
1152 while (fgets(buf, sizeof(buf), fp) != NULL) {
1154 if (buf[0] == '#' || buf[0] == 0 ||
1155 buf[0] == '\n' || buf[0] == '\r')
1159 * Comment characters should NOT be appearing anywhere but
1160 * as start of a comment;
1162 p = strchr(buf, '#');
1165 argc = str2argv(buf, argv, MAX_ARGV);
1166 if (argc == 0) continue;
1169 librad_log( "dict_init: %s[%d] invalid entry",
1178 fprintf(stderr, "ARGC = %d\n",argc);
1179 for (i = 0; i < argc; i++) {
1180 fprintf(stderr, "\t%s\n", argv[i]);
1185 * See if we need to import another dictionary.
1187 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1188 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1196 * Perhaps this is an attribute.
1198 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1199 if (process_attribute(fn, line, block_vendor,
1200 argv + 1, argc - 1) == -1) {
1208 * Process VALUE lines.
1210 if (strcasecmp(argv[0], "VALUE") == 0) {
1211 if (process_value(fn, line,
1212 argv + 1, argc - 1) == -1) {
1219 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1220 if (process_value_alias(fn, line,
1221 argv + 1, argc - 1) == -1) {
1229 * Process VENDOR lines.
1231 if (strcasecmp(argv[0], "VENDOR") == 0) {
1232 if (process_vendor(fn, line,
1233 argv + 1, argc - 1) == -1) {
1240 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1243 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1249 vendor = dict_vendorbyname(argv[1]);
1252 "dict_init: %s[%d]: unknown vendor %s",
1257 block_vendor = vendor;
1259 } /* BEGIN-VENDOR */
1261 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1264 "dict_init: %s[%d] invalid END-VENDOR entry",
1270 vendor = dict_vendorbyname(argv[1]);
1273 "dict_init: %s[%d]: unknown vendor %s",
1279 if (vendor != block_vendor) {
1281 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1291 * Any other string: We don't recognize it.
1294 "dict_init: %s[%d] invalid keyword \"%s\"",
1305 * Empty callback for hash table initialization.
1307 static int null_callback(void *ctx, void *data)
1309 ctx = ctx; /* -Wunused */
1310 data = data; /* -Wunused */
1317 * Initialize the directory, then fix the attr member of
1320 int dict_init(const char *dir, const char *fn)
1323 * Check if we need to change anything. If not, don't do
1326 if (dict_stat_check(dir, fn)) {
1331 * Free the dictionaries, and the stat cache.
1334 stat_root_dir = strdup(dir);
1335 stat_root_file = strdup(fn);
1338 * Create the table of vendor by name. There MAY NOT
1339 * be multiple vendors of the same name.
1341 * Each vendor is malloc'd, so the free function is free.
1343 vendors_byname = lrad_hash_table_create(dict_vendor_name_hash,
1344 dict_vendor_name_cmp,
1346 if (!vendors_byname) {
1351 * Create the table of vendors by value. There MAY
1352 * be vendors of the same value. If there are, we
1353 * pick the latest one.
1355 vendors_byvalue = lrad_hash_table_create(dict_vendor_value_hash,
1356 dict_vendor_value_cmp,
1358 if (!vendors_byvalue) {
1363 * Create the table of attributes by name. There MAY NOT
1364 * be multiple attributes of the same name.
1366 * Each attribute is malloc'd, so the free function is free.
1368 attributes_byname = lrad_hash_table_create(dict_attr_name_hash,
1371 if (!attributes_byname) {
1376 * Create the table of attributes by value. There MAY
1377 * be attributes of the same value. If there are, we
1378 * pick the latest one.
1380 attributes_byvalue = lrad_hash_table_create(dict_attr_value_hash,
1381 dict_attr_value_cmp,
1383 if (!attributes_byvalue) {
1387 values_byname = lrad_hash_table_create(dict_value_name_hash,
1388 dict_value_name_cmp,
1390 if (!values_byname) {
1394 values_byvalue = lrad_hash_table_create(dict_value_value_hash,
1395 dict_value_value_cmp,
1397 if (!values_byvalue) {
1401 value_fixup = NULL; /* just to be safe. */
1403 if (my_dict_init(dir, fn, NULL, 0) < 0)
1408 value_fixup_t *this, *next;
1410 for (this = value_fixup; this != NULL; this = next) {
1413 a = dict_attrbyname(this->attrstr);
1416 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1417 this->attrstr, this->dval->name);
1418 return -1; /* leak, but they should die... */
1421 this->dval->attr = a->attr;
1424 * Add the value into the dictionary.
1426 if (!lrad_hash_table_replace(values_byname,
1428 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1433 * Allow them to use the old name, but
1434 * prefer the new name when printing
1437 if (!lrad_hash_table_finddata(values_byvalue, this->dval)) {
1438 lrad_hash_table_replace(values_byvalue,
1444 * Just so we don't lose track of things.
1451 * Walk over all of the hash tables to ensure they're
1452 * initialized. We do this because the threads may perform
1453 * lookups, and we don't want multi-threaded re-ordering
1454 * of the table entries. That would be bad.
1456 lrad_hash_table_walk(vendors_byname, null_callback, NULL);
1457 lrad_hash_table_walk(vendors_byvalue, null_callback, NULL);
1459 lrad_hash_table_walk(attributes_byname, null_callback, NULL);
1460 lrad_hash_table_walk(attributes_byvalue, null_callback, NULL);
1462 lrad_hash_table_walk(values_byvalue, null_callback, NULL);
1463 lrad_hash_table_walk(values_byname, null_callback, NULL);
1469 * Get an attribute by its numerical value.
1471 DICT_ATTR *dict_attrbyvalue(int attr)
1476 dattr.vendor = VENDOR(attr) & 0x7fff;
1478 return lrad_hash_table_finddata(attributes_byvalue, &dattr);
1482 * Get an attribute by its name.
1484 DICT_ATTR *dict_attrbyname(const char *name)
1488 if (!name) return NULL;
1490 strlcpy(dattr.name, name, sizeof(dattr.name));
1492 return lrad_hash_table_finddata(attributes_byname, &dattr);
1496 * Associate a value with an attribute and return it.
1498 DICT_VALUE *dict_valbyattr(int attr, int value)
1500 DICT_VALUE dval, *dv;
1503 * First, look up aliases.
1506 dval.name[0] = '\0';
1509 * Look up the attribute alias target, and use
1510 * the correct attribute number if found.
1512 dv = lrad_hash_table_finddata(values_byname, &dval);
1513 if (dv) dval.attr = dv->value;
1517 return lrad_hash_table_finddata(values_byvalue, &dval);
1521 * Get a value by its name, keyed off of an attribute.
1523 DICT_VALUE *dict_valbyname(int attr, const char *name)
1525 DICT_VALUE *my_dv, *dv;
1526 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1528 if (!name) return NULL;
1530 my_dv = (DICT_VALUE *) buffer;
1532 my_dv->name[0] = '\0';
1535 * Look up the attribute alias target, and use
1536 * the correct attribute number if found.
1538 dv = lrad_hash_table_finddata(values_byname, my_dv);
1539 if (dv) my_dv->attr = dv->value;
1541 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN);
1543 return lrad_hash_table_finddata(values_byname, my_dv);
1547 * Get the vendor PEC based on the vendor name
1549 * This is efficient only for small numbers of vendors.
1551 int dict_vendorbyname(const char *name)
1554 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1556 if (!name) return 0;
1558 dv = (DICT_VENDOR *) buffer;
1559 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN);
1561 dv = lrad_hash_table_finddata(vendors_byname, dv);
1564 return dv->vendorpec;
1568 * Return the vendor struct based on the PEC.
1570 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1574 dv.vendorpec = vendorpec;
1576 return lrad_hash_table_finddata(vendors_byvalue, &dv);