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 * Insert the attribute, only if it's not a duplicate.
487 if (!lrad_hash_table_insert(attributes_byname, attr)) {
491 * If the attribute has identical number, then
492 * ignore the duplicate.
494 a = lrad_hash_table_finddata(attributes_byname, attr);
495 if (a && (strcasecmp(a->name, attr->name) == 0)) {
496 if (a->attr != attr->attr) {
497 librad_log("dict_addattr: Duplicate attribute name %s", name);
503 * Same name, same vendor, same attr,
504 * maybe the flags and/or type is
505 * different. Let the new value
506 * over-ride the old one.
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);
537 * Add a value for an attribute to the dictionary.
539 int dict_addvalue(const char *namestr, const char *attrstr, int value)
546 librad_log("dict_addvalue: empty names are not permitted");
550 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
551 librad_log("dict_addvalue: value name too long");
555 if ((dval = malloc(sizeof(*dval) + length)) == NULL) {
556 librad_log("dict_addvalue: out of memory");
559 memset(dval, 0, sizeof(*dval));
561 strcpy(dval->name, namestr);
565 * Remember which attribute is associated with this
566 * value, if possible.
568 dattr = dict_attrbyname(attrstr);
570 if (dattr->flags.has_value_alias) {
571 librad_log("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
575 dval->attr = dattr->attr;
578 * Enforce valid values
580 * Don't worry about fixups...
582 switch (dattr->type) {
585 librad_log("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
591 librad_log("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
597 * Allow octets for now, because
598 * of dictionary.cablelabs
602 case PW_TYPE_INTEGER:
606 librad_log("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
607 lrad_int2str(type_table, dattr->type, "?Unknown?"));
611 dattr->flags.has_value = 1;
613 value_fixup_t *fixup;
615 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
617 librad_log("dict_addvalue: out of memory");
620 memset(fixup, 0, sizeof(*fixup));
622 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
626 * Insert to the head of the list.
628 fixup->next = value_fixup;
635 * Add the value into the dictionary.
637 if (!lrad_hash_table_insert(values_byname, dval)) {
642 * Suppress duplicates with the same
643 * name and value. There are lots in
646 old = dict_valbyname(dattr->attr, namestr);
647 if (old && (old->value == dval->value)) {
653 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
658 * There are multiple VALUE's, keyed by attribute, so we
659 * take care of that here.
661 if (!lrad_hash_table_replace(values_byvalue, dval)) {
662 librad_log("dict_addvalue: Failed inserting value %s",
671 * Process the ATTRIBUTE command
673 static int process_attribute(const char* fn, const int line,
674 const int block_vendor, char **argv,
683 if ((argc < 3) || (argc > 4)) {
684 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
690 * Validate all entries
692 if (!isdigit((int) argv[1][0])) {
693 librad_log("dict_init: %s[%d]: invalid value", fn, line);
696 sscanf(argv[1], "%i", &value);
699 * find the type of the attribute.
701 type = lrad_str2int(type_table, argv[2], -1);
703 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
709 * Only look up the vendor if the string
712 memset(&flags, 0, sizeof(flags));
715 * FIXME: replace strtok with str2argv
717 s = strtok(argv[3], ",");
719 if (strcmp(s, "has_tag") == 0 ||
720 strcmp(s, "has_tag=1") == 0) {
721 /* Boolean flag, means this is a
725 } else if (strncmp(s, "encrypt=", 8) == 0) {
726 /* Encryption method, defaults to 0 (none).
727 Currently valid is just type 2,
728 Tunnel-Password style, which can only
729 be applied to strings. */
730 flags.encrypt = strtol(s + 8, &c, 0);
732 librad_log( "dict_init: %s[%d] invalid option %s",
737 } else if (strncmp(s, "array", 8) == 0) {
744 case PW_TYPE_INTEGER:
749 librad_log( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
754 } else if (block_vendor) {
755 librad_log( "dict_init: %s[%d]: unknown option \"%s\"",
760 /* Must be a vendor 'flag'... */
761 if (strncmp(s, "vendor=", 7) == 0) {
766 vendor = dict_vendorbyname(s);
768 librad_log( "dict_init: %s[%d]: unknown vendor \"%s\"",
772 if (block_vendor && argv[3][0] &&
773 (block_vendor != vendor)) {
774 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
779 s = strtok(NULL, ",");
783 if (block_vendor) vendor = block_vendor;
786 * Special checks for tags, they make our life much more
791 * Only string, octets, and integer can be tagged.
795 case PW_TYPE_INTEGER:
799 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
801 lrad_int2str(type_table, type, "?Unknown?"));
810 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
811 librad_log("dict_init: %s[%d]: %s",
812 fn, line, librad_errstr);
821 * Process the VALUE command
823 static int process_value(const char* fn, const int line, char **argv,
829 librad_log("dict_init: %s[%d]: invalid VALUE line",
834 * For Compatibility, skip "Server-Config"
836 if (strcasecmp(argv[0], "Server-Config") == 0)
840 * Validate all entries
842 if (!isdigit((int) argv[2][0])) {
843 librad_log("dict_init: %s[%d]: invalid value",
847 sscanf(argv[2], "%i", &value);
849 if (dict_addvalue(argv[1], argv[0], value) < 0) {
850 librad_log("dict_init: %s[%d]: %s",
851 fn, line, librad_errstr);
860 * Process the VALUE-ALIAS command
862 * This allows VALUE mappings to be shared among multiple
865 static int process_value_alias(const char* fn, const int line, char **argv,
868 DICT_ATTR *my_da, *da;
872 librad_log("dict_init: %s[%d]: invalid VALUE-ALIAS line",
877 my_da = dict_attrbyname(argv[0]);
879 librad_log("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
884 if (my_da->flags.has_value) {
885 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
890 if (my_da->flags.has_value_alias) {
891 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
896 da = dict_attrbyname(argv[1]);
898 librad_log("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
903 if (!da->flags.has_value) {
904 librad_log("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
909 if (da->flags.has_value_alias) {
910 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
915 if (my_da->type != da->type) {
916 librad_log("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
921 if ((dval = malloc(sizeof(*dval))) == NULL) {
922 librad_log("dict_addvalue: out of memory");
925 memset(dval, 0, sizeof(*dval));
927 dval->name[0] = '\0'; /* empty name */
928 dval->attr = my_da->attr;
929 dval->value = da->attr;
931 if (!lrad_hash_table_insert(values_byname, dval)) {
932 librad_log("dict_init: %s[%d]: Error create alias",
943 * Process the VENDOR command
945 static int process_vendor(const char* fn, const int line, char **argv,
949 const char *format = NULL;
951 if ((argc < 2) || (argc > 3)) {
952 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
958 * Validate all entries
960 if (!isdigit((int) argv[1][0])) {
961 librad_log("dict_init: %s[%d]: invalid value",
965 value = atoi(argv[1]);
967 /* Create a new VENDOR entry for the list */
968 if (dict_addvendor(argv[0], value) < 0) {
969 librad_log("dict_init: %s[%d]: %s",
970 fn, line, librad_errstr);
975 * Look for a format statement
980 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
981 format = "format=4,0";
983 } else if (value == VENDORPEC_LUCENT) {
984 format = "format=2,1";
986 } else if (value == VENDORPEC_STARENT) {
987 format = "format=2,2";
989 } /* else no fixups to do */
996 if (strncasecmp(format, "format=", 7) != 0) {
997 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
1003 if ((strlen(p) != 3) ||
1004 !isdigit((int) p[0]) ||
1006 !isdigit((int) p[2])) {
1007 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
1012 type = (int) (p[0] - '0');
1013 length = (int) (p[2] - '0');
1015 dv = dict_vendorbyvalue(value);
1017 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
1022 if ((type != 1) && (type != 2) && (type != 4)) {
1023 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
1028 if ((length != 0) && (length != 1) && (length != 2)) {
1029 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
1035 dv->length = length;
1042 * String split routine. Splits an input string IN PLACE
1043 * into pieces, based on spaces.
1045 static int str2argv(char *str, char **argv, int max_argc)
1050 if (argc >= max_argc) return argc;
1053 * Chop out comments early.
1060 while ((*str == ' ') ||
1063 (*str == '\n')) *(str++) = '\0';
1065 if (!*str) return argc;
1074 (*str != '\n')) str++;
1080 #define MAX_ARGV (16)
1083 * Initialize the dictionary.
1085 static int my_dict_init(const char *dir, const char *fn,
1086 const char *src_file, int src_line)
1095 struct stat statbuf;
1096 char *argv[MAX_ARGV];
1099 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1100 strlen(dir) >= sizeof(dirtmp) / 2) {
1101 librad_log("dict_init: filename name too long");
1106 * First see if fn is relative to dir. If so, create
1107 * new filename. If not, remember the absolute dir.
1109 if ((p = strrchr(fn, '/')) != NULL) {
1113 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1114 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1118 if ((fp = fopen(fn, "r")) == NULL) {
1120 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
1121 fn, strerror(errno));
1123 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1124 src_file, src_line, fn, strerror(errno));
1129 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1130 if (!S_ISREG(statbuf.st_mode)) {
1132 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
1138 * Globally writable dictionaries means that users can control
1139 * the server configuration with little difficulty.
1141 if ((statbuf.st_mode & S_IWOTH) != 0) {
1143 librad_log("dict_init: Dictionary \"%s\" is globally writable. Refusing to start due to insecure configuration.",
1148 dict_stat_add(fn, &statbuf);
1151 * Seed the random pool with data.
1153 lrad_rand_seed(&statbuf, sizeof(statbuf));
1157 while (fgets(buf, sizeof(buf), fp) != NULL) {
1159 if (buf[0] == '#' || buf[0] == 0 ||
1160 buf[0] == '\n' || buf[0] == '\r')
1164 * Comment characters should NOT be appearing anywhere but
1165 * as start of a comment;
1167 p = strchr(buf, '#');
1170 argc = str2argv(buf, argv, MAX_ARGV);
1171 if (argc == 0) continue;
1174 librad_log( "dict_init: %s[%d] invalid entry",
1183 fprintf(stderr, "ARGC = %d\n",argc);
1184 for (i = 0; i < argc; i++) {
1185 fprintf(stderr, "\t%s\n", argv[i]);
1190 * See if we need to import another dictionary.
1192 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1193 if (my_dict_init(dir, argv[1], fn, line) < 0) {
1201 * Perhaps this is an attribute.
1203 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1204 if (process_attribute(fn, line, block_vendor,
1205 argv + 1, argc - 1) == -1) {
1213 * Process VALUE lines.
1215 if (strcasecmp(argv[0], "VALUE") == 0) {
1216 if (process_value(fn, line,
1217 argv + 1, argc - 1) == -1) {
1224 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1225 if (process_value_alias(fn, line,
1226 argv + 1, argc - 1) == -1) {
1234 * Process VENDOR lines.
1236 if (strcasecmp(argv[0], "VENDOR") == 0) {
1237 if (process_vendor(fn, line,
1238 argv + 1, argc - 1) == -1) {
1245 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1248 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1254 vendor = dict_vendorbyname(argv[1]);
1257 "dict_init: %s[%d]: unknown vendor %s",
1262 block_vendor = vendor;
1264 } /* BEGIN-VENDOR */
1266 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1269 "dict_init: %s[%d] invalid END-VENDOR entry",
1275 vendor = dict_vendorbyname(argv[1]);
1278 "dict_init: %s[%d]: unknown vendor %s",
1284 if (vendor != block_vendor) {
1286 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1296 * Any other string: We don't recognize it.
1299 "dict_init: %s[%d] invalid keyword \"%s\"",
1310 * Empty callback for hash table initialization.
1312 static int null_callback(void *ctx, void *data)
1314 ctx = ctx; /* -Wunused */
1315 data = data; /* -Wunused */
1322 * Initialize the directory, then fix the attr member of
1325 int dict_init(const char *dir, const char *fn)
1328 * Check if we need to change anything. If not, don't do
1331 if (dict_stat_check(dir, fn)) {
1336 * Free the dictionaries, and the stat cache.
1339 stat_root_dir = strdup(dir);
1340 stat_root_file = strdup(fn);
1343 * Create the table of vendor by name. There MAY NOT
1344 * be multiple vendors of the same name.
1346 * Each vendor is malloc'd, so the free function is free.
1348 vendors_byname = lrad_hash_table_create(dict_vendor_name_hash,
1349 dict_vendor_name_cmp,
1351 if (!vendors_byname) {
1356 * Create the table of vendors by value. There MAY
1357 * be vendors of the same value. If there are, we
1358 * pick the latest one.
1360 vendors_byvalue = lrad_hash_table_create(dict_vendor_value_hash,
1361 dict_vendor_value_cmp,
1363 if (!vendors_byvalue) {
1368 * Create the table of attributes by name. There MAY NOT
1369 * be multiple attributes of the same name.
1371 * Each attribute is malloc'd, so the free function is free.
1373 attributes_byname = lrad_hash_table_create(dict_attr_name_hash,
1376 if (!attributes_byname) {
1381 * Create the table of attributes by value. There MAY
1382 * be attributes of the same value. If there are, we
1383 * pick the latest one.
1385 attributes_byvalue = lrad_hash_table_create(dict_attr_value_hash,
1386 dict_attr_value_cmp,
1388 if (!attributes_byvalue) {
1392 values_byname = lrad_hash_table_create(dict_value_name_hash,
1393 dict_value_name_cmp,
1395 if (!values_byname) {
1399 values_byvalue = lrad_hash_table_create(dict_value_value_hash,
1400 dict_value_value_cmp,
1402 if (!values_byvalue) {
1406 value_fixup = NULL; /* just to be safe. */
1408 if (my_dict_init(dir, fn, NULL, 0) < 0)
1413 value_fixup_t *this, *next;
1415 for (this = value_fixup; this != NULL; this = next) {
1418 a = dict_attrbyname(this->attrstr);
1421 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1422 this->attrstr, this->dval->name);
1423 return -1; /* leak, but they should die... */
1426 this->dval->attr = a->attr;
1429 * Add the value into the dictionary.
1431 if (!lrad_hash_table_replace(values_byname,
1433 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1438 * Allow them to use the old name, but
1439 * prefer the new name when printing
1442 if (!lrad_hash_table_finddata(values_byvalue, this->dval)) {
1443 lrad_hash_table_replace(values_byvalue,
1449 * Just so we don't lose track of things.
1456 * Walk over all of the hash tables to ensure they're
1457 * initialized. We do this because the threads may perform
1458 * lookups, and we don't want multi-threaded re-ordering
1459 * of the table entries. That would be bad.
1461 lrad_hash_table_walk(vendors_byname, null_callback, NULL);
1462 lrad_hash_table_walk(vendors_byvalue, null_callback, NULL);
1464 lrad_hash_table_walk(attributes_byname, null_callback, NULL);
1465 lrad_hash_table_walk(attributes_byvalue, null_callback, NULL);
1467 lrad_hash_table_walk(values_byvalue, null_callback, NULL);
1468 lrad_hash_table_walk(values_byname, null_callback, NULL);
1474 * Get an attribute by its numerical value.
1476 DICT_ATTR *dict_attrbyvalue(int attr)
1481 dattr.vendor = VENDOR(attr) & 0x7fff;
1483 return lrad_hash_table_finddata(attributes_byvalue, &dattr);
1487 * Get an attribute by its name.
1489 DICT_ATTR *dict_attrbyname(const char *name)
1493 if (!name) return NULL;
1495 strlcpy(dattr.name, name, sizeof(dattr.name));
1497 return lrad_hash_table_finddata(attributes_byname, &dattr);
1501 * Associate a value with an attribute and return it.
1503 DICT_VALUE *dict_valbyattr(int attr, int value)
1505 DICT_VALUE dval, *dv;
1508 * First, look up aliases.
1511 dval.name[0] = '\0';
1514 * Look up the attribute alias target, and use
1515 * the correct attribute number if found.
1517 dv = lrad_hash_table_finddata(values_byname, &dval);
1518 if (dv) dval.attr = dv->value;
1522 return lrad_hash_table_finddata(values_byvalue, &dval);
1526 * Get a value by its name, keyed off of an attribute.
1528 DICT_VALUE *dict_valbyname(int attr, const char *name)
1530 DICT_VALUE *my_dv, *dv;
1531 uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1533 if (!name) return NULL;
1535 my_dv = (DICT_VALUE *) buffer;
1537 my_dv->name[0] = '\0';
1540 * Look up the attribute alias target, and use
1541 * the correct attribute number if found.
1543 dv = lrad_hash_table_finddata(values_byname, my_dv);
1544 if (dv) my_dv->attr = dv->value;
1546 strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN);
1548 return lrad_hash_table_finddata(values_byname, my_dv);
1552 * Get the vendor PEC based on the vendor name
1554 * This is efficient only for small numbers of vendors.
1556 int dict_vendorbyname(const char *name)
1559 uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1561 if (!name) return 0;
1563 dv = (DICT_VENDOR *) buffer;
1564 strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN);
1566 dv = lrad_hash_table_finddata(vendors_byname, dv);
1569 return dv->vendorpec;
1573 * Return the vendor struct based on the PEC.
1575 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1579 dv.vendorpec = vendorpec;
1581 return lrad_hash_table_finddata(vendors_byvalue, &dv);