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 The FreeRADIUS server project
23 static const char rcsid[] = "$Id$";
25 #include <freeradius-devel/autoconf.h>
35 #ifdef HAVE_SYS_STAT_H
41 #include <freeradius-devel/missing.h>
42 #include <freeradius-devel/libradius.h>
44 #define DICT_VALUE_MAX_NAME_LEN (128)
45 #define DICT_VENDOR_MAX_NAME_LEN (128)
47 static lrad_hash_table_t *vendors_byname = NULL;
48 static lrad_hash_table_t *vendors_byvalue = NULL;
50 static lrad_hash_table_t *attributes_byname = NULL;
51 static lrad_hash_table_t *attributes_byvalue = NULL;
53 static lrad_hash_table_t *values_byvalue = NULL;
54 static lrad_hash_table_t *values_byname = NULL;
57 * For faster HUP's, we cache the stat information for
58 * files we've $INCLUDEd
60 typedef struct dict_stat_t {
61 struct dict_stat_t *next;
66 static char *stat_root_dir = NULL;
67 static char *stat_root_file = NULL;
69 static dict_stat_t *stat_head = NULL;
70 static dict_stat_t *stat_tail = NULL;
72 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 },
100 * Create the hash of the name.
102 * We copy the hash function here because it's substantially faster.
104 #define FNV_MAGIC_INIT (0x811c9dc5)
105 #define FNV_MAGIC_PRIME (0x01000193)
107 static uint32_t dict_hashname(const char *name)
109 uint32_t hash = FNV_MAGIC_INIT;
110 const unsigned char *p;
112 for (p = name; *p != '\0'; p++) {
114 if (isalpha(c)) c = tolower(c);
116 hash *= FNV_MAGIC_PRIME;
117 hash ^= (uint32_t ) (c & 0xff);
125 * Free the list of stat buffers
127 static void dict_stat_free(void)
129 dict_stat_t *this, *next;
132 stat_root_dir = NULL;
133 free(stat_root_file);
134 stat_root_file = NULL;
141 for (this = stat_head; this != NULL; this = next) {
147 stat_head = stat_tail = NULL;
152 * Add an entry to the list of stat buffers.
154 static void dict_stat_add(const char *name, const struct stat *stat_buf)
158 this = malloc(sizeof(*this));
159 memset(this, 0, sizeof(*this));
161 this->name = strdup(name);
162 this->mtime = stat_buf->st_mtime;
165 stat_head = stat_tail = this;
167 stat_tail->next = this;
174 * See if any dictionaries have changed. If not, don't
177 static int dict_stat_check(const char *root_dir, const char *root_file)
182 if (!stat_root_dir) return 0;
183 if (!stat_root_file) return 0;
185 if (strcmp(root_dir, stat_root_dir) != 0) return 0;
186 if (strcmp(root_file, stat_root_file) != 0) return 0;
188 if (!stat_head) return 0; /* changed, reload */
190 for (this = stat_head; this != NULL; this = this->next) {
191 if (stat(this->name, &buf) < 0) return 0;
193 if (buf.st_mtime != this->mtime) return 0;
201 * Free the dictionary_attributes and dictionary_values lists.
208 lrad_hash_table_free(vendors_byname);
209 lrad_hash_table_free(vendors_byvalue);
210 vendors_byname = NULL;
211 vendors_byvalue = NULL;
213 lrad_hash_table_free(attributes_byname);
214 lrad_hash_table_free(attributes_byvalue);
215 attributes_byname = NULL;
216 attributes_byvalue = NULL;
218 lrad_hash_table_free(values_byname);
219 lrad_hash_table_free(values_byvalue);
220 values_byname = NULL;
221 values_byvalue = NULL;
228 * Add vendor to the list.
230 int dict_addvendor(const char *name, int value)
236 if (value >= (1 << 16)) {
237 librad_log("dict_addvendor: Cannot handle vendor ID larger than 65535");
241 if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
242 librad_log("dict_addvendor: vendor name too long");
246 if ((dv = malloc(sizeof(*dv) + length)) == NULL) {
247 librad_log("dict_addvendor: out of memory");
251 hash = dict_hashname(name);
252 strcpy(dv->name, name);
253 dv->vendorpec = value;
254 dv->type = dv->length = 1; /* defaults */
256 if (!lrad_hash_table_insert(vendors_byname, hash, dv)) {
259 old_dv = lrad_hash_table_finddata(vendors_byname, hash);
261 librad_log("dict_addvendor: Failed inserting vendor name %s", name);
264 if (old_dv->vendorpec != dv->vendorpec) {
265 librad_log("dict_addvendor: Duplicate vendor name %s", name);
270 * Already inserted. Discard the duplicate entry.
277 * Insert the SAME pointer (not free'd when this tree is
278 * deleted), into another tree.
280 * We want this behaviour because we want OLD names for
281 * the attributes to be read from the configuration
282 * files, but when we're printing them, (and looking up
283 * by value) we want to use the NEW name.
285 if (!lrad_hash_table_replace(vendors_byvalue,
286 lrad_hash(&dv->vendorpec,
287 sizeof(dv->vendorpec)),
289 librad_log("dict_addvendor: Failed inserting vendor %s",
298 * Add an attribute to the dictionary.
300 int dict_addattr(const char *name, int vendor, int type, int value,
303 static int max_attr = 0;
307 if (strlen(name) > (sizeof(attr->name) -1)) {
308 librad_log("dict_addattr: attribute name too long");
313 * If the value is '-1', that means use a pre-existing
314 * one (if it already exists). If one does NOT already exist,
315 * then create a new attribute, with a non-conflicting value,
319 if (dict_attrbyname(name)) {
320 return 0; /* exists, don't add it again */
325 } else if (vendor == 0) {
329 if (value > max_attr) {
335 librad_log("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
339 if (value >= 65536) {
340 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 65535).");
345 DICT_VENDOR *dv = dict_vendorbyvalue(vendor);
348 * If the vendor isn't defined, die/
351 librad_log("dict_addattr: Unknown vendor");
356 * With a few exceptions, attributes can only be
357 * 1..255. The check above catches the less than
360 if ((dv->type == 1) && (value >= 256)) {
361 librad_log("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
363 } /* else 256..65535 are allowed */
367 * Create a new attribute for the list
369 if ((attr = malloc(sizeof(*attr))) == NULL) {
370 librad_log("dict_addattr: out of memory");
374 hash = dict_hashname(name);
375 strcpy(attr->name, name);
377 attr->attr |= (vendor << 16); /* FIXME: hack */
380 attr->vendor = vendor;
384 * Insert the attribute, only if it's not a duplicate.
386 if (!lrad_hash_table_insert(attributes_byname, hash, attr)) {
390 * If the attribute has identical number, then
391 * ignore the duplicate.
393 a = lrad_hash_table_finddata(attributes_byname, hash);
394 if (a && (strcasecmp(a->name, attr->name) == 0)) {
395 if (a->attr != attr->attr) {
396 librad_log("dict_addattr: Duplicate attribute name %s", name);
401 * Same name, same vendor, same attr,
402 * maybe the flags and/or type is
403 * different. Let the new value
404 * over-ride the old one.
410 * Insert the SAME pointer (not free'd when this tree is
411 * deleted), into another tree.
413 * We want this behaviour because we want OLD names for
414 * the attributes to be read from the configuration
415 * files, but when we're printing them, (and looking up
416 * by value) we want to use the NEW name.
418 if (!lrad_hash_table_replace(attributes_byvalue,
419 lrad_hash(&attr->attr, sizeof(attr->attr)),
421 librad_log("dict_addattr: Failed inserting attribute name %s", name);
430 * Add a value for an attribute to the dictionary.
432 int dict_addvalue(const char *namestr, const char *attrstr, int value)
439 if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
440 librad_log("dict_addvalue: value name too long");
444 if ((dval = malloc(sizeof(*dval) + length)) == NULL) {
445 librad_log("dict_addvalue: out of memory");
448 memset(dval, 0, sizeof(*dval));
450 hash = dict_hashname(namestr);
451 strcpy(dval->name, namestr);
455 * Remember which attribute is associated with this
456 * value, if possible.
458 dattr = dict_attrbyname(attrstr);
460 dval->attr = dattr->attr;
461 hash = lrad_hash_update(&dval->attr, sizeof(dval->attr), hash);
463 value_fixup_t *fixup;
465 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
467 librad_log("dict_addvalue: out of memory");
470 memset(fixup, 0, sizeof(*fixup));
472 strNcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
477 * Insert to the head of the list.
479 fixup->next = value_fixup;
486 * Add the value into the dictionary.
488 if (!lrad_hash_table_insert(values_byname, hash, dval)) {
493 * Suppress duplicates with the same
494 * name and value. There are lots in
497 old = dict_valbyname(dattr->attr, namestr);
498 if (old && (old->value == dval->value)) {
504 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
509 * There are multiple VALUE's, keyed by attribute, so we
510 * take care of that here.
513 hash = lrad_hash_update(&dval->value, sizeof(dval->value), hash);
514 if (!lrad_hash_table_replace(values_byvalue, hash, dval)) {
515 librad_log("dict_addvalue: Failed inserting value %s",
524 * Process the ATTRIBUTE command
526 static int process_attribute(const char* fn, const int line,
527 const int block_vendor, char **argv,
536 if ((argc < 3) || (argc > 4)) {
537 librad_log("dict_init: %s[%d]: invalid ATTRIBUTE line",
543 * Validate all entries
545 if (!isdigit((int) argv[1][0])) {
546 librad_log("dict_init: %s[%d]: invalid value", fn, line);
549 sscanf(argv[1], "%i", &value);
552 * find the type of the attribute.
554 type = lrad_str2int(type_table, argv[2], -1);
556 librad_log("dict_init: %s[%d]: invalid type \"%s\"",
562 * Only look up the vendor if the string
565 memset(&flags, 0, sizeof(flags));
567 s = strtok(argv[3], ",");
569 if (strcmp(s, "has_tag") == 0 ||
570 strcmp(s, "has_tag=1") == 0) {
571 /* Boolean flag, means this is a
575 } else if (strncmp(s, "encrypt=", 8) == 0) {
576 /* Encryption method, defaults to 0 (none).
577 Currently valid is just type 2,
578 Tunnel-Password style, which can only
579 be applied to strings. */
580 flags.encrypt = strtol(s + 8, &c, 0);
582 librad_log( "dict_init: %s[%d] invalid option %s",
587 /* Must be a vendor 'flag'... */
588 if (strncmp(s, "vendor=", 7) == 0) {
593 vendor = dict_vendorbyname(s);
595 librad_log( "dict_init: %s[%d]: unknown vendor %s",
599 if (block_vendor && argv[3][0] &&
600 (block_vendor != vendor)) {
601 librad_log("dict_init: %s[%d]: mismatched vendor %s within BEGIN-VENDOR/END-VENDOR block",
606 s = strtok(NULL, ",");
610 if (block_vendor) vendor = block_vendor;
613 * Special checks for tags, they make our life much more
618 * VSA's can't be tagged.
621 librad_log("dict_init: %s[%d]: Vendor attributes cannot be tagged.",
627 * Only string, octets, and integer can be tagged.
631 case PW_TYPE_INTEGER:
635 librad_log("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
637 lrad_int2str(type_table, type, "?Unknown?"));
646 if (dict_addattr(argv[0], vendor, type, value, flags) < 0) {
647 librad_log("dict_init: %s[%d]: %s",
648 fn, line, librad_errstr);
657 * Process the VALUE command
659 static int process_value(const char* fn, const int line, char **argv,
665 librad_log("dict_init: %s[%d]: invalid VALUE line",
670 * For Compatibility, skip "Server-Config"
672 if (strcasecmp(argv[0], "Server-Config") == 0)
676 * Validate all entries
678 if (!isdigit((int) argv[2][0])) {
679 librad_log("dict_init: %s[%d]: invalid value",
683 sscanf(argv[2], "%i", &value);
686 * valuepair.c will get excited when creating attributes,
687 * if it sees values which look like integers, so we can't
690 if (isdigit(argv[1][0])) {
691 librad_log("dict_init: %s[%d]: Names for VALUEs cannot start with a digit.",
695 if (dict_addvalue(argv[1], argv[0], value) < 0) {
696 librad_log("dict_init: %s[%d]: %s",
697 fn, line, librad_errstr);
706 * Process the VENDOR command
708 static int process_vendor(const char* fn, const int line, char **argv,
712 const char *format = NULL;
714 if ((argc < 2) || (argc > 3)) {
715 librad_log( "dict_init: %s[%d] invalid VENDOR entry",
721 * Validate all entries
723 if (!isdigit((int) argv[1][0])) {
724 librad_log("dict_init: %s[%d]: invalid value",
728 value = atoi(argv[1]);
730 /* Create a new VENDOR entry for the list */
731 if (dict_addvendor(argv[0], value) < 0) {
732 librad_log("dict_init: %s[%d]: %s",
733 fn, line, librad_errstr);
738 * Look for a format statement
743 } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
744 format = "format=4,0";
746 } else if (value == VENDORPEC_LUCENT) {
747 format = "format=2,1";
749 } else if (value == VENDORPEC_STARENT) {
750 format = "format=2,2";
752 } /* else no fixups to do */
759 if (strncasecmp(format, "format=", 7) != 0) {
760 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected \"format=\", got \"%s\"",
766 if ((strlen(p) != 3) ||
767 !isdigit((int) p[0]) ||
769 !isdigit((int) p[2])) {
770 librad_log("dict_init: %s[%d]: Invalid format for VENDOR. Expected text like \"1,1\", got \"%s\"",
775 type = (int) (p[0] - '0');
776 length = (int) (p[2] - '0');
778 dv = dict_vendorbyvalue(value);
780 librad_log("dict_init: %s[%d]: Failed adding format for VENDOR",
785 if ((type != 1) && (type != 2) && (type != 4)) {
786 librad_log("dict_init: %s[%d]: invalid type value %d for VENDOR",
791 if ((length != 0) && (length != 1) && (length != 2)) {
792 librad_log("dict_init: %s[%d]: invalid length value %d for VENDOR",
805 * String split routine. Splits an input string IN PLACE
806 * into pieces, based on spaces.
808 static int str2argv(char *str, char **argv, int max_argc)
813 if (argc >= max_argc) return argc;
816 * Chop out comments early.
823 while ((*str == ' ') ||
826 (*str == '\n')) *(str++) = '\0';
828 if (!*str) return argc;
837 (*str != '\n')) str++;
843 #define MAX_ARGV (16)
846 * Initialize the dictionary.
848 static int my_dict_init(const char *dir, const char *fn,
849 const char *src_file, int src_line)
859 char *argv[MAX_ARGV];
862 if (strlen(fn) >= sizeof(dirtmp) / 2 ||
863 strlen(dir) >= sizeof(dirtmp) / 2) {
864 librad_log("dict_init: filename name too long");
869 * First see if fn is relative to dir. If so, create
870 * new filename. If not, remember the absolute dir.
872 if ((p = strrchr(fn, '/')) != NULL) {
876 } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
877 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
881 if ((fp = fopen(fn, "r")) == NULL) {
883 librad_log("dict_init: Couldn't open dictionary \"%s\": %s",
884 fn, strerror(errno));
886 librad_log("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
887 src_file, src_line, fn, strerror(errno));
892 stat(fn, &statbuf); /* fopen() guarantees this will succeed */
893 if (!S_ISREG(statbuf.st_mode)) {
895 librad_log("dict_init: Dictionary \"%s\" is not a regular file",
899 dict_stat_add(fn, &statbuf);
902 * Seed the random pool with data.
904 lrad_rand_seed(&statbuf, sizeof(statbuf));
908 while (fgets(buf, sizeof(buf), fp) != NULL) {
910 if (buf[0] == '#' || buf[0] == 0 ||
911 buf[0] == '\n' || buf[0] == '\r')
915 * Comment characters should NOT be appearing anywhere but
916 * as start of a comment;
918 p = strchr(buf, '#');
921 argc = str2argv(buf, argv, MAX_ARGV);
922 if (argc == 0) continue;
925 librad_log( "dict_init: %s[%d] invalid entry",
934 fprintf(stderr, "ARGC = %d\n",argc);
935 for (i = 0; i < argc; i++) {
936 fprintf(stderr, "\t%s\n", argv[i]);
941 * See if we need to import another dictionary.
943 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
944 if (my_dict_init(dir, argv[1], fn, line) < 0) {
952 * Perhaps this is an attribute.
954 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
955 if (process_attribute(fn, line, block_vendor,
956 argv + 1, argc - 1) == -1) {
964 * Process VALUE lines.
966 if (strcasecmp(argv[0], "VALUE") == 0) {
967 if (process_value(fn, line,
968 argv + 1, argc - 1) == -1) {
976 * Process VENDOR lines.
978 if (strcasecmp(argv[0], "VENDOR") == 0) {
979 if (process_vendor(fn, line,
980 argv + 1, argc - 1) == -1) {
987 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
990 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
996 vendor = dict_vendorbyname(argv[1]);
999 "dict_init: %s[%d]: unknown vendor %s",
1004 block_vendor = vendor;
1006 } /* BEGIN-VENDOR */
1008 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1011 "dict_init: %s[%d] invalid END-VENDOR entry",
1017 vendor = dict_vendorbyname(argv[1]);
1020 "dict_init: %s[%d]: unknown vendor %s",
1026 if (vendor != block_vendor) {
1028 "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1038 * Any other string: We don't recognize it.
1041 "dict_init: %s[%d] invalid keyword \"%s\"",
1051 * Initialize the directory, then fix the attr member of
1054 int dict_init(const char *dir, const char *fn)
1057 * Check if we need to change anything. If not, don't do
1060 if (dict_stat_check(dir, fn)) {
1065 * Free the dictionaries, and the stat cache.
1068 stat_root_dir = strdup(dir);
1069 stat_root_file = strdup(fn);
1072 * Create the tree of vendor by name. There MAY NOT
1073 * be multiple vendors of the same name.
1075 * Each vendor is malloc'd, so the free function is free.
1077 vendors_byname = lrad_hash_table_create(free);
1078 if (!vendors_byname) {
1083 * Create the tree of vendors by value. There MAY
1084 * be vendors of the same value. If there are, we
1085 * pick the latest one.
1087 vendors_byvalue = lrad_hash_table_create(NULL);
1088 if (!vendors_byvalue) {
1093 * Create the tree of attributes by name. There MAY NOT
1094 * be multiple attributes of the same name.
1096 * Each attribute is malloc'd, so the free function is free.
1098 attributes_byname = lrad_hash_table_create(free);
1099 if (!attributes_byname) {
1104 * Create the tree of attributes by value. There MAY
1105 * be attributes of the same value. If there are, we
1106 * pick the latest one.
1108 attributes_byvalue = lrad_hash_table_create(NULL);
1109 if (!attributes_byvalue) {
1113 values_byname = lrad_hash_table_create(free);
1114 if (!values_byname) {
1118 values_byvalue = lrad_hash_table_create(NULL);
1119 if (!values_byvalue) {
1123 value_fixup = NULL; /* just to be safe. */
1125 if (my_dict_init(dir, fn, NULL, 0) < 0)
1131 value_fixup_t *this, *next;
1133 for (this = value_fixup; this != NULL; this = next) {
1136 a = dict_attrbyname(this->attrstr);
1139 "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1140 this->attrstr, this->dval->name);
1141 return -1; /* leak, but they should die... */
1144 this->dval->attr = a->attr;
1147 * Add the value into the dictionary.
1149 hash = lrad_hash_update(&this->dval->attr,
1150 sizeof(this->dval->attr),
1152 if (!lrad_hash_table_replace(values_byname,
1153 hash, this->dval)) {
1154 librad_log("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1159 * Allow them to use the old name, but
1160 * prefer the new name when printing
1163 hash = lrad_hash(&this->dval->attr,
1164 sizeof(this->dval->attr));
1165 hash = lrad_hash_update(&this->dval->value,
1166 sizeof(this->dval->value),
1168 if (!lrad_hash_table_finddata(values_byvalue, hash)) {
1169 lrad_hash_table_replace(values_byvalue,
1175 * Just so we don't lose track of things.
1185 * Get an attribute by its numerical value.
1187 DICT_ATTR *dict_attrbyvalue(int val)
1189 return lrad_hash_table_finddata(attributes_byvalue,
1190 lrad_hash(&val, sizeof(val)));
1194 * Get an attribute by its name.
1196 DICT_ATTR *dict_attrbyname(const char *name)
1198 if (!name) return NULL;
1200 return lrad_hash_table_finddata(attributes_byname,
1201 dict_hashname(name));
1205 * Associate a value with an attribute and return it.
1207 DICT_VALUE *dict_valbyattr(int attr, int val)
1209 uint32_t hash = attr;
1211 hash = lrad_hash_update(&val, sizeof(val), hash);
1213 return lrad_hash_table_finddata(values_byvalue, hash);
1217 * Get a value by its name, keyed off of an attribute.
1219 DICT_VALUE *dict_valbyname(int attr, const char *name)
1223 if (!name) return NULL;
1225 hash = dict_hashname(name);
1226 hash = lrad_hash_update(&attr, sizeof(attr), hash);
1228 return lrad_hash_table_finddata(values_byname, hash);
1232 * Get the vendor PEC based on the vendor name
1234 * This is efficient only for small numbers of vendors.
1236 int dict_vendorbyname(const char *name)
1241 if (!name) return 0;
1243 hash = dict_hashname(name);
1245 dv = lrad_hash_table_finddata(vendors_byname, hash);
1248 return dv->vendorpec;
1252 * Return the vendor struct based on the PEC.
1254 DICT_VENDOR *dict_vendorbyvalue(int vendor)
1256 return lrad_hash_table_finddata(vendors_byvalue,
1257 lrad_hash(&vendor, sizeof(vendor)));