Allow nested TLVs to be read from the dictionary.
[freeradius.git] / src / lib / dict.c
1 /*
2  * dict.c       Routines to read the dictionary file.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2000,2006  The FreeRADIUS server project
21  */
22
23 #include        <freeradius-devel/ident.h>
24 RCSID("$Id$")
25
26 #include        <freeradius-devel/libradius.h>
27
28 #include        <ctype.h>
29
30 #ifdef HAVE_MALLOC_H
31 #include        <malloc.h>
32 #endif
33
34 #ifdef HAVE_SYS_STAT_H
35 #include        <sys/stat.h>
36 #endif
37
38 #define DICT_VALUE_MAX_NAME_LEN (128)
39 #define DICT_VENDOR_MAX_NAME_LEN (128)
40 #define DICT_ATTR_MAX_NAME_LEN (128)
41
42 static fr_hash_table_t *vendors_byname = NULL;
43 static fr_hash_table_t *vendors_byvalue = NULL;
44
45 static fr_hash_table_t *attributes_byname = NULL;
46 static fr_hash_table_t *attributes_byvalue = NULL;
47
48 static fr_hash_table_t *values_byvalue = NULL;
49 static fr_hash_table_t *values_byname = NULL;
50
51 static DICT_ATTR *dict_base_attrs[256];
52
53 /*
54  *      For faster HUP's, we cache the stat information for
55  *      files we've $INCLUDEd
56  */
57 typedef struct dict_stat_t {
58         struct dict_stat_t *next;
59         char               *name;
60         time_t             mtime;
61 } dict_stat_t;
62
63 static char *stat_root_dir = NULL;
64 static char *stat_root_file = NULL;
65
66 static dict_stat_t *stat_head = NULL;
67 static dict_stat_t *stat_tail = NULL;
68
69 typedef struct value_fixup_t {
70         char            attrstr[DICT_ATTR_MAX_NAME_LEN];
71         DICT_VALUE      *dval;
72         struct value_fixup_t *next;
73 } value_fixup_t;
74
75
76 /*
77  *      So VALUEs in the dictionary can have forward references.
78  */
79 static value_fixup_t *value_fixup = NULL;
80
81 static const FR_NAME_NUMBER type_table[] = {
82         { "integer",    PW_TYPE_INTEGER },
83         { "string",     PW_TYPE_STRING },
84         { "ipaddr",     PW_TYPE_IPADDR },
85         { "date",       PW_TYPE_DATE },
86         { "abinary",    PW_TYPE_ABINARY },
87         { "octets",     PW_TYPE_OCTETS },
88         { "ifid",       PW_TYPE_IFID },
89         { "ipv6addr",   PW_TYPE_IPV6ADDR },
90         { "ipv6prefix", PW_TYPE_IPV6PREFIX },
91         { "byte",       PW_TYPE_BYTE },
92         { "short",      PW_TYPE_SHORT },
93         { "ether",      PW_TYPE_ETHERNET },
94         { "combo-ip",   PW_TYPE_COMBO_IP },
95         { "tlv",        PW_TYPE_TLV },
96         { "signed",     PW_TYPE_SIGNED },
97         { NULL, 0 }
98 };
99
100
101 /*
102  *      Create the hash of the name.
103  *
104  *      We copy the hash function here because it's substantially faster.
105  */
106 #define FNV_MAGIC_INIT (0x811c9dc5)
107 #define FNV_MAGIC_PRIME (0x01000193)
108
109 static uint32_t dict_hashname(const char *name)
110 {
111         uint32_t hash = FNV_MAGIC_INIT;
112         const char *p;
113
114         for (p = name; *p != '\0'; p++) {
115                 int c = *(const unsigned char *) p;
116                 if (isalpha(c)) c = tolower(c);
117
118                 hash *= FNV_MAGIC_PRIME;
119                 hash ^= (uint32_t ) (c & 0xff);
120         }
121
122         return hash;
123 }
124
125
126 /*
127  *      Hash callback functions.
128  */
129 static uint32_t dict_attr_name_hash(const void *data)
130 {
131         return dict_hashname(((const DICT_ATTR *)data)->name);
132 }
133
134 static int dict_attr_name_cmp(const void *one, const void *two)
135 {
136         const DICT_ATTR *a = one;
137         const DICT_ATTR *b = two;
138
139         return strcasecmp(a->name, b->name);
140 }
141
142 static uint32_t dict_attr_value_hash(const void *data)
143 {
144         uint32_t hash;
145         const DICT_ATTR *attr = data;
146
147         hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
148         return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
149 }
150
151 static int dict_attr_value_cmp(const void *one, const void *two)
152 {
153         const DICT_ATTR *a = one;
154         const DICT_ATTR *b = two;
155
156         if (a->vendor < b->vendor) return -1;
157         if (a->vendor > b->vendor) return +1;
158
159         return a->attr - b->attr;
160 }
161
162 static uint32_t dict_vendor_name_hash(const void *data)
163 {
164         return dict_hashname(((const DICT_VENDOR *)data)->name);
165 }
166
167 static int dict_vendor_name_cmp(const void *one, const void *two)
168 {
169         const DICT_VENDOR *a = one;
170         const DICT_VENDOR *b = two;
171
172         return strcasecmp(a->name, b->name);
173 }
174
175 static uint32_t dict_vendor_value_hash(const void *data)
176 {
177         return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
178                          sizeof(((const DICT_VENDOR *)data)->vendorpec));
179 }
180
181 static int dict_vendor_value_cmp(const void *one, const void *two)
182 {
183         const DICT_VENDOR *a = one;
184         const DICT_VENDOR *b = two;
185
186         return a->vendorpec - b->vendorpec;
187 }
188
189 static uint32_t dict_value_name_hash(const void *data)
190 {
191         uint32_t hash;
192         const DICT_VALUE *dval = data;
193
194         hash = dict_hashname(dval->name);
195         hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
196         return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
197 }
198
199 static int dict_value_name_cmp(const void *one, const void *two)
200 {
201         int rcode;
202         const DICT_VALUE *a = one;
203         const DICT_VALUE *b = two;
204
205         rcode = a->attr - b->attr;
206         if (rcode != 0) return rcode;
207
208         rcode = a->vendor - b->vendor;
209         if (rcode != 0) return rcode;
210
211         return strcasecmp(a->name, b->name);
212 }
213
214 static uint32_t dict_value_value_hash(const void *data)
215 {
216         uint32_t hash;
217         const DICT_VALUE *dval = data;
218
219         hash = fr_hash(&dval->attr, sizeof(dval->attr));
220         hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
221         return fr_hash_update(&dval->value, sizeof(dval->value), hash);
222 }
223
224 static int dict_value_value_cmp(const void *one, const void *two)
225 {
226         int rcode;
227         const DICT_VALUE *a = one;
228         const DICT_VALUE *b = two;
229
230         if (a->vendor < b->vendor) return -1;
231         if (a->vendor > b->vendor) return +1;
232
233         rcode = a->attr - b->attr;
234         if (rcode != 0) return rcode;
235
236         return a->value - b->value;
237 }
238
239
240 /*
241  *      Free the list of stat buffers
242  */
243 static void dict_stat_free(void)
244 {
245         dict_stat_t *this, *next;
246
247         free(stat_root_dir);
248         stat_root_dir = NULL;
249         free(stat_root_file);
250         stat_root_file = NULL;
251
252         if (!stat_head) {
253                 stat_tail = NULL;
254                 return;
255         }
256
257         for (this = stat_head; this != NULL; this = next) {
258                 next = this->next;
259                 free(this->name);
260                 free(this);
261         }
262
263         stat_head = stat_tail = NULL;
264 }
265
266
267 /*
268  *      Add an entry to the list of stat buffers.
269  */
270 static void dict_stat_add(const char *name, const struct stat *stat_buf)
271 {
272         dict_stat_t *this;
273
274         this = malloc(sizeof(*this));
275         if (!this) return;
276         memset(this, 0, sizeof(*this));
277
278         this->name = strdup(name);
279         this->mtime = stat_buf->st_mtime;
280
281         if (!stat_head) {
282                 stat_head = stat_tail = this;
283         } else {
284                 stat_tail->next = this;
285                 stat_tail = this;
286         }
287 }
288
289
290 /*
291  *      See if any dictionaries have changed.  If not, don't
292  *      do anything.
293  */
294 static int dict_stat_check(const char *root_dir, const char *root_file)
295 {
296         struct stat buf;
297         dict_stat_t *this;
298
299         if (!stat_root_dir) return 0;
300         if (!stat_root_file) return 0;
301
302         if (strcmp(root_dir, stat_root_dir) != 0) return 0;
303         if (strcmp(root_file, stat_root_file) != 0) return 0;
304
305         if (!stat_head) return 0; /* changed, reload */
306
307         for (this = stat_head; this != NULL; this = this->next) {
308                 if (stat(this->name, &buf) < 0) return 0;
309
310                 if (buf.st_mtime != this->mtime) return 0;
311         }
312
313         return 1;
314 }
315
316 typedef struct fr_pool_t {
317         void    *page_end;
318         void    *free_ptr;
319         struct fr_pool_t *page_free;
320         struct fr_pool_t *page_next;
321 } fr_pool_t;
322
323 #define FR_POOL_SIZE (32768)
324 #define FR_ALLOC_ALIGN (8)
325
326 static fr_pool_t *dict_pool = NULL;
327
328 static fr_pool_t *fr_pool_create(void)
329 {
330         fr_pool_t *fp = malloc(FR_POOL_SIZE);
331
332         if (!fp) return NULL;
333
334         memset(fp, 0, FR_POOL_SIZE);
335
336         fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
337         fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
338         fp->page_free = fp;
339         fp->page_next = NULL;
340         return fp;
341 }
342
343 static void fr_pool_delete(fr_pool_t **pfp)
344 {
345         fr_pool_t *fp, *next;
346
347         if (!pfp || !*pfp) return;
348
349         for (fp = *pfp; fp != NULL; fp = next) {
350                 next = fp->page_next;
351                 free(fp);
352         }
353 }
354
355
356 static void *fr_pool_alloc(size_t size)
357 {
358         void *ptr;
359
360         if (size == 0) return NULL;
361
362         if (size > 256) return NULL; /* shouldn't happen */
363
364         if (!dict_pool) {
365                 dict_pool = fr_pool_create();
366                 if (!dict_pool) return NULL;
367         }
368
369         if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
370                 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
371         }
372
373         if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
374                 dict_pool->page_free->page_next = fr_pool_create();
375                 if (!dict_pool->page_free->page_next) return NULL;
376                 dict_pool->page_free = dict_pool->page_free->page_next;
377         }
378
379         ptr = dict_pool->page_free->free_ptr;
380         dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
381
382         return ptr;
383 }
384
385
386 static void fr_pool_free(UNUSED void *ptr)
387 {
388         /*
389          *      Place-holder for later code.
390          */
391 }
392
393 /*
394  *      Free the dictionary_attributes and dictionary_values lists.
395  */
396 void dict_free(void)
397 {
398         /*
399          *      Free the tables
400          */
401         fr_hash_table_free(vendors_byname);
402         fr_hash_table_free(vendors_byvalue);
403         vendors_byname = NULL;
404         vendors_byvalue = NULL;
405
406         fr_hash_table_free(attributes_byname);
407         fr_hash_table_free(attributes_byvalue);
408         attributes_byname = NULL;
409         attributes_byvalue = NULL;
410
411         fr_hash_table_free(values_byname);
412         fr_hash_table_free(values_byvalue);
413         values_byname = NULL;
414         values_byvalue = NULL;
415
416         memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
417
418         fr_pool_delete(&dict_pool);
419
420         dict_stat_free();
421 }
422
423
424 /*
425  *      Add vendor to the list.
426  */
427 int dict_addvendor(const char *name, int value)
428 {
429         size_t length;
430         DICT_VENDOR *dv;
431
432         if (value > 65535) {
433                 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 65535");
434                 return -1;
435         }
436
437         if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
438                 fr_strerror_printf("dict_addvendor: vendor name too long");
439                 return -1;
440         }
441
442         if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
443                 fr_strerror_printf("dict_addvendor: out of memory");
444                 return -1;
445         }
446
447         strcpy(dv->name, name);
448         dv->vendorpec  = value;
449         dv->type = dv->length = 1; /* defaults */
450
451         if (!fr_hash_table_insert(vendors_byname, dv)) {
452                 DICT_VENDOR *old_dv;
453
454                 old_dv = fr_hash_table_finddata(vendors_byname, dv);
455                 if (!old_dv) {
456                         fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
457                         return -1;
458                 }
459                 if (old_dv->vendorpec != dv->vendorpec) {
460                         fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
461                         return -1;
462                 }
463
464                 /*
465                  *      Already inserted.  Discard the duplicate entry.
466                  */
467                 fr_pool_free(dv);
468                 return 0;
469         }
470
471         /*
472          *      Insert the SAME pointer (not free'd when this table is
473          *      deleted), into another table.
474          *
475          *      We want this behaviour because we want OLD names for
476          *      the attributes to be read from the configuration
477          *      files, but when we're printing them, (and looking up
478          *      by value) we want to use the NEW name.
479          */
480         if (!fr_hash_table_replace(vendors_byvalue, dv)) {
481                 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
482                            name);
483                 return -1;
484         }
485
486         return 0;
487 }
488
489 /*
490  *      Add an attribute to the dictionary.
491  */
492 int dict_addattr(const char *name, int attr, int vendor, int type,
493                  ATTR_FLAGS flags)
494 {
495         size_t namelen;
496         static int      max_attr = 0;
497         DICT_ATTR       *da;
498
499         namelen = strlen(name);
500         if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
501                 fr_strerror_printf("dict_addattr: attribute name too long");
502                 return -1;
503         }
504
505         /*
506          *      If the attr is '-1', that means use a pre-existing
507          *      one (if it already exists).  If one does NOT already exist,
508          *      then create a new attribute, with a non-conflicting value,
509          *      and use that.
510          */
511         if (attr == -1) {
512                 if (dict_attrbyname(name)) {
513                         return 0; /* exists, don't add it again */
514                 }
515
516                 attr = ++max_attr;
517
518         } else if (vendor == 0) {
519                 /*
520                  *  Update 'max_attr'
521                  */
522                 if (attr > max_attr) {
523                         max_attr = attr;
524                 }
525         }
526
527         if (attr < 0) {
528                 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
529                 return -1;
530         }
531
532         if (vendor) {
533                 DICT_VENDOR *dv;
534                 static DICT_VENDOR *last_vendor = NULL;
535
536                 if (flags.is_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
537                         fr_strerror_printf("Sub-TLV's cannot be encrypted");
538                         return -1;
539                 }
540
541                 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
542                         fr_strerror_printf("TLV's cannot be encrypted");
543                         return -1;
544                 }
545
546                 if (flags.is_tlv && flags.has_tag) {
547                         fr_strerror_printf("Sub-TLV's cannot have a tag");
548                         return -1;
549                 }
550
551                 if (flags.has_tlv && flags.has_tag) {
552                         fr_strerror_printf("TLV's cannot have a tag");
553                         return -1;
554                 }
555
556                 /*
557                  *      Most ATTRIBUTEs are bunched together by
558                  *      VENDOR.  We can save a lot of lookups on
559                  *      dictionary initialization by caching the last
560                  *      vendor.
561                  */
562                 if (last_vendor && (vendor == last_vendor->vendorpec)) {
563                         dv = last_vendor;
564                 } else {
565                         dv = dict_vendorbyvalue(vendor);
566                         last_vendor = dv;
567                 }
568
569                 /*
570                  *      If the vendor isn't defined, die.
571                  */
572                 if (!dv) {
573                         fr_strerror_printf("dict_addattr: Unknown vendor");
574                         return -1;
575                 }
576
577                 /*
578                  *      FIXME: Switch over dv->type, and limit things
579                  *      properly.
580                  */
581                 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
582                         fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
583                         return -1;
584                 } /* else 256..65535 are allowed */
585         }
586
587         /*
588          *      Create a new attribute for the list
589          */
590         if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
591                 fr_strerror_printf("dict_addattr: out of memory");
592                 return -1;
593         }
594
595         memcpy(da->name, name, namelen);
596         da->name[namelen] = '\0';
597         da->attr = attr;
598         da->vendor = vendor;
599         da->type = type;
600         da->flags = flags;
601         da->vendor = vendor;
602
603         /*
604          *      Insert the attribute, only if it's not a duplicate.
605          */
606         if (!fr_hash_table_insert(attributes_byname, da)) {
607                 DICT_ATTR       *a;
608
609                 /*
610                  *      If the attribute has identical number, then
611                  *      ignore the duplicate.
612                  */
613                 a = fr_hash_table_finddata(attributes_byname, da);
614                 if (a && (strcasecmp(a->name, da->name) == 0)) {
615                         if (a->attr != da->attr) {
616                                 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
617                                 fr_pool_free(da);
618                                 return -1;
619                         }
620
621                         /*
622                          *      Same name, same vendor, same attr,
623                          *      maybe the flags and/or type is
624                          *      different.  Let the new value
625                          *      over-ride the old one.
626                          */
627                 }
628
629
630                 fr_hash_table_delete(attributes_byvalue, a);
631
632                 if (!fr_hash_table_replace(attributes_byname, da)) {
633                         fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
634                         fr_pool_free(da);
635                         return -1;
636                 }
637         }
638
639         /*
640          *      Insert the SAME pointer (not free'd when this entry is
641          *      deleted), into another table.
642          *
643          *      We want this behaviour because we want OLD names for
644          *      the attributes to be read from the configuration
645          *      files, but when we're printing them, (and looking up
646          *      by value) we want to use the NEW name.
647          */
648         if (!fr_hash_table_replace(attributes_byvalue, da)) {
649                 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
650                 return -1;
651         }
652
653         if (!vendor && (attr > 0) && (attr < 256)) {
654                  dict_base_attrs[attr] = da;
655         }
656
657         return 0;
658 }
659
660
661 /*
662  *      Add a value for an attribute to the dictionary.
663  */
664 int dict_addvalue(const char *namestr, const char *attrstr, int value)
665 {
666         size_t          length;
667         DICT_ATTR       *dattr;
668         DICT_VALUE      *dval;
669
670         static DICT_ATTR *last_attr = NULL;
671
672         if (!*namestr) {
673                 fr_strerror_printf("dict_addvalue: empty names are not permitted");
674                 return -1;
675         }
676
677         if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
678                 fr_strerror_printf("dict_addvalue: value name too long");
679                 return -1;
680         }
681
682         if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
683                 fr_strerror_printf("dict_addvalue: out of memory");
684                 return -1;
685         }
686         memset(dval, 0, sizeof(*dval));
687
688         strcpy(dval->name, namestr);
689         dval->value = value;
690
691         /*
692          *      Most VALUEs are bunched together by ATTRIBUTE.  We can
693          *      save a lot of lookups on dictionary initialization by
694          *      caching the last attribute.
695          */
696         if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
697                 dattr = last_attr;
698         } else {
699                 dattr = dict_attrbyname(attrstr);
700                 last_attr = dattr;
701         }
702
703         /*
704          *      Remember which attribute is associated with this
705          *      value, if possible.
706          */
707         if (dattr) {
708                 if (dattr->flags.has_value_alias) {
709                         fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
710                         return -1;
711                 }
712
713                 dval->attr = dattr->attr;
714                 dval->vendor = dattr->vendor;
715
716                 /*
717                  *      Enforce valid values
718                  *
719                  *      Don't worry about fixups...
720                  */
721                 switch (dattr->type) {
722                         case PW_TYPE_BYTE:
723                                 if (value > 255) {
724                                         fr_pool_free(dval);
725                                         fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
726                                         return -1;
727                                 }
728                                 break;
729                         case PW_TYPE_SHORT:
730                                 if (value > 65535) {
731                                         fr_pool_free(dval);
732                                         fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
733                                         return -1;
734                                 }
735                                 break;
736
737                                 /*
738                                  *      Allow octets for now, because
739                                  *      of dictionary.cablelabs
740                                  */
741                         case PW_TYPE_OCTETS:
742
743                         case PW_TYPE_INTEGER:
744                                 break;
745
746                         default:
747                                 fr_pool_free(dval);
748                                 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
749                                            fr_int2str(type_table, dattr->type, "?Unknown?"));
750                                 return -1;
751                 }
752
753                 dattr->flags.has_value = 1;
754         } else {
755                 value_fixup_t *fixup;
756
757                 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
758                 if (!fixup) {
759                         fr_pool_free(dval);
760                         fr_strerror_printf("dict_addvalue: out of memory");
761                         return -1;
762                 }
763                 memset(fixup, 0, sizeof(*fixup));
764
765                 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
766                 fixup->dval = dval;
767
768                 /*
769                  *      Insert to the head of the list.
770                  */
771                 fixup->next = value_fixup;
772                 value_fixup = fixup;
773
774                 return 0;
775         }
776
777         /*
778          *      Add the value into the dictionary.
779          */
780         if (!fr_hash_table_insert(values_byname, dval)) {
781                 if (dattr) {
782                         DICT_VALUE *old;
783
784                         /*
785                          *      Suppress duplicates with the same
786                          *      name and value.  There are lots in
787                          *      dictionary.ascend.
788                          */
789                         old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
790                         if (old && (old->value == dval->value)) {
791                                 fr_pool_free(dval);
792                                 return 0;
793                         }
794                 }
795
796                 fr_pool_free(dval);
797                 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
798                 return -1;
799         }
800
801         /*
802          *      There are multiple VALUE's, keyed by attribute, so we
803          *      take care of that here.
804          */
805         if (!fr_hash_table_replace(values_byvalue, dval)) {
806                 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
807                            namestr);
808                 return -1;
809         }
810
811         return 0;
812 }
813
814 static int sscanf_i(const char *str, int *pvalue)
815 {
816         int rcode = 0;
817         int base = 10;
818         const char *tab = "0123456789";
819
820         if ((str[0] == '0') &&
821             ((str[1] == 'x') || (str[1] == 'X'))) {
822                 tab = "0123456789abcdef";
823                 base = 16;
824
825                 str += 2;
826         }
827
828         while (*str) {
829                 const char *c;
830
831                 c = memchr(tab, tolower((int) *str), base);
832                 if (!c) return 0;
833
834                 rcode *= base;
835                 rcode += (c - tab);
836                 str++;
837         }
838
839         *pvalue = rcode;
840         return 1;
841 }
842
843
844 /*
845  *      Process the ATTRIBUTE command
846  */
847 static int process_attribute(const char* fn, const int line,
848                              const int block_vendor, DICT_ATTR *block_tlv,
849                              char **argv, int argc)
850 {
851         int             vendor = 0;
852         int             value;
853         int             type;
854         ATTR_FLAGS      flags;
855
856         if ((argc < 3) || (argc > 4)) {
857                 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
858                         fn, line);
859                 return -1;
860         }
861
862         /*
863          *      Validate all entries
864          */
865         if (!sscanf_i(argv[1], &value)) {
866                 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
867                 return -1;
868         }
869
870         /*
871          *      find the type of the attribute.
872          */
873         type = fr_str2int(type_table, argv[2], -1);
874         if (type < 0) {
875                 fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
876                         fn, line, argv[2]);
877                 return -1;
878         }
879
880         /*
881          *      Only look up the vendor if the string
882          *      is non-empty.
883          */
884         memset(&flags, 0, sizeof(flags));
885         if (argc == 4) {
886                 char *key, *next, *last;
887
888                 key = argv[3];
889                 do {
890                         next = strchr(key, ',');
891                         if (next) *(next++) = '\0';
892
893                         if (strcmp(key, "has_tag") == 0 ||
894                             strcmp(key, "has_tag=1") == 0) {
895                                 /* Boolean flag, means this is a
896                                    tagged attribute */
897                                 flags.has_tag = 1;
898                                 
899                         } else if (strncmp(key, "encrypt=", 8) == 0) {
900                                 /* Encryption method, defaults to 0 (none).
901                                    Currently valid is just type 2,
902                                    Tunnel-Password style, which can only
903                                    be applied to strings. */
904                                 flags.encrypt = strtol(key + 8, &last, 0);
905                                 if (*last) {
906                                         fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
907                                                     fn, line, key);
908                                         return -1;
909                                 }
910                                 
911                         } else if (strncmp(key, "array", 8) == 0) {
912                                 flags.array = 1;
913                                 
914                                 switch (type) {
915                                         case PW_TYPE_IPADDR:
916                                         case PW_TYPE_BYTE:
917                                         case PW_TYPE_SHORT:
918                                         case PW_TYPE_INTEGER:
919                                         case PW_TYPE_DATE:
920                                                 break;
921
922                                         default:
923                                                 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
924                                                             fn, line);
925                                                 return -1;
926                                 }
927
928                                 /*
929                                  *      The only thing is the vendor name,
930                                  *      and it's a known name: allow it.
931                                  */
932                         } else if ((key == argv[3]) && !next && !block_vendor &&
933                                    ((vendor = dict_vendorbyname(key)) !=0)) {
934                                 break;
935
936                         } else {
937                                 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
938                                             fn, line, key);
939                                 return -1;
940                         }
941
942                         key = next;
943                         if (key && !*key) break;
944                 } while (key);
945         }
946
947         if (block_vendor) vendor = block_vendor;
948
949         /*
950          *      Special checks for tags, they make our life much more
951          *      difficult.
952          */
953         if (flags.has_tag) {
954                 /*
955                  *      Only string, octets, and integer can be tagged.
956                  */
957                 switch (type) {
958                 case PW_TYPE_STRING:
959                 case PW_TYPE_INTEGER:
960                         break;
961
962                 default:
963                         fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
964                                    fn, line,
965                                    fr_int2str(type_table, type, "?Unknown?"));
966                         return -1;
967
968                 }
969         }
970
971         if (type == PW_TYPE_TLV) {
972                 flags.has_tlv = 1;
973         }
974
975         if (block_tlv) {
976                 /*
977                  *      TLV's can be only one octet.
978                  */
979                 if ((value <= 0) || (value > 255)) {
980                         fr_strerror_printf( "dict_init: %s[%d]: sub-tlv's cannot have value > 255",
981                                     fn, line);
982                         return -1;
983                 }
984
985                 if (flags.encrypt != FLAG_ENCRYPT_NONE) {
986                         fr_strerror_printf( "dict_init: %s[%d]: sub-tlv's cannot be encrypted",
987                                     fn, line);
988                         return -1;
989                 }
990
991                 /*
992                  *      
993                  */
994                 value <<= 8;
995                 if (block_tlv->flags.is_tlv) value <<= 8;
996                 value |= (block_tlv->attr & 0xffff);
997                 flags.is_tlv = 1;
998         }
999
1000         /*
1001          *      Add it in.
1002          */
1003         if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1004                 char buffer[256];
1005
1006                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1007
1008                 fr_strerror_printf("dict_init: %s[%d]: %s",
1009                                    fn, line, buffer);
1010                 return -1;
1011         }
1012
1013         return 0;
1014 }
1015
1016
1017 /*
1018  *      Process the VALUE command
1019  */
1020 static int process_value(const char* fn, const int line, char **argv,
1021                          int argc)
1022 {
1023         int     value;
1024
1025         if (argc != 3) {
1026                 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1027                         fn, line);
1028                 return -1;
1029         }
1030         /*
1031          *      For Compatibility, skip "Server-Config"
1032          */
1033         if (strcasecmp(argv[0], "Server-Config") == 0)
1034                 return 0;
1035
1036         /*
1037          *      Validate all entries
1038          */
1039         if (!sscanf_i(argv[2], &value)) {
1040                 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1041                         fn, line);
1042                 return -1;
1043         }
1044
1045         if (dict_addvalue(argv[1], argv[0], value) < 0) {
1046                 char buffer[256];
1047
1048                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1049
1050                 fr_strerror_printf("dict_init: %s[%d]: %s",
1051                                    fn, line, buffer);
1052                 return -1;
1053         }
1054
1055         return 0;
1056 }
1057
1058
1059 /*
1060  *      Process the VALUE-ALIAS command
1061  *
1062  *      This allows VALUE mappings to be shared among multiple
1063  *      attributes.
1064  */
1065 static int process_value_alias(const char* fn, const int line, char **argv,
1066                                int argc)
1067 {
1068         DICT_ATTR *my_da, *da;
1069         DICT_VALUE *dval;
1070
1071         if (argc != 2) {
1072                 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1073                         fn, line);
1074                 return -1;
1075         }
1076
1077         my_da = dict_attrbyname(argv[0]);
1078         if (!my_da) {
1079                 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1080                            fn, line, argv[1]);
1081                 return -1;
1082         }
1083
1084         if (my_da->flags.has_value) {
1085                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1086                            fn, line, argv[0]);
1087                 return -1;
1088         }
1089
1090         if (my_da->flags.has_value_alias) {
1091                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1092                            fn, line, argv[0]);
1093                 return -1;
1094         }
1095
1096         da = dict_attrbyname(argv[1]);
1097         if (!da) {
1098                 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1099                            fn, line, argv[1]);
1100                 return -1;
1101         }
1102
1103         if (!da->flags.has_value) {
1104                 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1105                            fn, line, argv[1]);
1106                 return -1;
1107         }
1108
1109         if (da->flags.has_value_alias) {
1110                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1111                            fn, line, argv[1]);
1112                 return -1;
1113         }
1114
1115         if (my_da->type != da->type) {
1116                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1117                            fn, line);
1118                 return -1;
1119         }
1120
1121         if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1122                 fr_strerror_printf("dict_addvalue: out of memory");
1123                 return -1;
1124         }
1125
1126         dval->name[0] = '\0';   /* empty name */
1127         dval->attr = my_da->attr;
1128         dval->vendor = my_da->vendor;
1129         dval->value = da->attr;
1130
1131         if (!fr_hash_table_insert(values_byname, dval)) {
1132                 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1133                            fn, line);
1134                 fr_pool_free(dval);
1135                 return -1;
1136         }
1137
1138         return 0;
1139 }
1140
1141
1142 /*
1143  *      Process the VENDOR command
1144  */
1145 static int process_vendor(const char* fn, const int line, char **argv,
1146                           int argc)
1147 {
1148         int     value;
1149         int     continuation = 0;
1150         const   char *format = NULL;
1151
1152         if ((argc < 2) || (argc > 3)) {
1153                 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1154                             fn, line);
1155                 return -1;
1156         }
1157
1158         /*
1159          *       Validate all entries
1160          */
1161         if (!isdigit((int) argv[1][0])) {
1162                 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1163                         fn, line);
1164                 return -1;
1165         }
1166         value = atoi(argv[1]);
1167
1168         /* Create a new VENDOR entry for the list */
1169         if (dict_addvendor(argv[0], value) < 0) {
1170                 char buffer[256];
1171
1172                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1173
1174                 fr_strerror_printf("dict_init: %s[%d]: %s",
1175                            fn, line, buffer);
1176                 return -1;
1177         }
1178
1179         /*
1180          *      Look for a format statement
1181          */
1182         if (argc == 3) {
1183                 format = argv[2];
1184
1185         } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1186                 format = "format=4,0";
1187
1188         } else if (value == VENDORPEC_LUCENT) {
1189                 format = "format=2,1";
1190
1191         } else if (value == VENDORPEC_STARENT) {
1192                 format = "format=2,2";
1193
1194         } /* else no fixups to do */
1195
1196         if (format) {
1197                 int type, length;
1198                 const char *p;
1199                 DICT_VENDOR *dv;
1200
1201                 if (strncasecmp(format, "format=", 7) != 0) {
1202                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected \"format=\", got \"%s\"",
1203                                    fn, line, format);
1204                         return -1;
1205                 }
1206
1207                 p = format + 7;
1208                 if ((strlen(p) < 3) ||
1209                     !isdigit((int) p[0]) ||
1210                     (p[1] != ',') ||
1211                     !isdigit((int) p[2]) ||
1212                     (p[3] && (p[3] != ','))) {
1213                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
1214                                    fn, line, p);
1215                         return -1;
1216                 }
1217
1218                 type = (int) (p[0] - '0');
1219                 length = (int) (p[2] - '0');
1220
1221                 if (p[3] == ',') {
1222                         if ((p[4] != 'c') ||
1223                             (p[5] != '\0')) {
1224                                 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
1225                                            fn, line, p);
1226                                 return -1;
1227                         }
1228                         continuation = 1;
1229                 }
1230
1231                 dv = dict_vendorbyvalue(value);
1232                 if (!dv) {
1233                         fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1234                                    fn, line);
1235                         return -1;
1236                 }
1237
1238                 if ((type != 1) && (type != 2) && (type != 4)) {
1239                         fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1240                                    fn, line, type);
1241                         return -1;
1242                 }
1243
1244                 if ((length != 0) && (length != 1) && (length != 2)) {
1245                         fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1246                                    fn, line, length);
1247                         return -1;
1248                 }
1249
1250                 dv->type = type;
1251                 dv->length = length;
1252                 dv->flags = continuation;
1253         }
1254
1255         return 0;
1256 }
1257
1258 /*
1259  *      String split routine.  Splits an input string IN PLACE
1260  *      into pieces, based on spaces.
1261  */
1262 static int str2argv(char *str, char **argv, int max_argc)
1263 {
1264         int argc = 0;
1265
1266         while (*str) {
1267                 if (argc >= max_argc) return argc;
1268
1269                 /*
1270                  *      Chop out comments early.
1271                  */
1272                 if (*str == '#') {
1273                         *str = '\0';
1274                         break;
1275                 }
1276
1277                 while ((*str == ' ') ||
1278                        (*str == '\t') ||
1279                        (*str == '\r') ||
1280                        (*str == '\n')) *(str++) = '\0';
1281
1282                 if (!*str) return argc;
1283
1284                 argv[argc] = str;
1285                 argc++;
1286
1287                 while (*str &&
1288                        (*str != ' ') &&
1289                        (*str != '\t') &&
1290                        (*str != '\r') &&
1291                        (*str != '\n')) str++;
1292         }
1293
1294         return argc;
1295 }
1296
1297 #define MAX_ARGV (16)
1298
1299 /*
1300  *      Initialize the dictionary.
1301  */
1302 static int my_dict_init(const char *dir, const char *fn,
1303                         const char *src_file, int src_line)
1304 {
1305         FILE    *fp;
1306         char    dirtmp[256];
1307         char    buf[256];
1308         char    *p;
1309         int     line = 0;
1310         int     vendor;
1311         int     block_vendor;
1312         struct stat statbuf;
1313         char    *argv[MAX_ARGV];
1314         int     argc;
1315         DICT_ATTR *da, *block_tlv[3];
1316         int     which_block_tlv = 0;
1317
1318         block_tlv[0] = NULL;
1319         block_tlv[1] = NULL;
1320         block_tlv[2] = NULL;
1321
1322         if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1323             strlen(dir) >= sizeof(dirtmp) / 2) {
1324                 fr_strerror_printf("dict_init: filename name too long");
1325                 return -1;
1326         }
1327
1328         /*
1329          *      First see if fn is relative to dir. If so, create
1330          *      new filename. If not, remember the absolute dir.
1331          */
1332         if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1333                 strcpy(dirtmp, fn);
1334                 dirtmp[p - fn] = 0;
1335                 dir = dirtmp;
1336         } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1337                 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1338                 fn = dirtmp;
1339         }
1340
1341         if ((fp = fopen(fn, "r")) == NULL) {
1342                 if (!src_file) {
1343                         fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1344                                    fn, strerror(errno));
1345                 } else {
1346                         fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1347                                    src_file, src_line, fn, strerror(errno));
1348                 }
1349                 return -1;
1350         }
1351
1352         stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1353         if (!S_ISREG(statbuf.st_mode)) {
1354                 fclose(fp);
1355                 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1356                            fn);
1357                 return -1;
1358         }
1359
1360         /*
1361          *      Globally writable dictionaries means that users can control
1362          *      the server configuration with little difficulty.
1363          */
1364 #ifdef S_IWOTH
1365         if ((statbuf.st_mode & S_IWOTH) != 0) {
1366                 fclose(fp);
1367                 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable.  Refusing to start due to insecure configuration.",
1368                            fn);
1369                 return -1;
1370         }
1371 #endif
1372
1373         dict_stat_add(fn, &statbuf);
1374
1375         /*
1376          *      Seed the random pool with data.
1377          */
1378         fr_rand_seed(&statbuf, sizeof(statbuf));
1379
1380         block_vendor = 0;
1381
1382         while (fgets(buf, sizeof(buf), fp) != NULL) {
1383                 line++;
1384                 if (buf[0] == '#' || buf[0] == 0 ||
1385                     buf[0] == '\n' || buf[0] == '\r')
1386                         continue;
1387
1388                 /*
1389                  *  Comment characters should NOT be appearing anywhere but
1390                  *  as start of a comment;
1391                  */
1392                 p = strchr(buf, '#');
1393                 if (p) *p = '\0';
1394
1395                 argc = str2argv(buf, argv, MAX_ARGV);
1396                 if (argc == 0) continue;
1397
1398                 if (argc == 1) {
1399                         fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1400                                     fn, line);
1401                         fclose(fp);
1402                         return -1;
1403                 }
1404
1405                 /*
1406                  *      Process VALUE lines.
1407                  */
1408                 if (strcasecmp(argv[0], "VALUE") == 0) {
1409                         if (process_value(fn, line,
1410                                           argv + 1, argc - 1) == -1) {
1411                                 fclose(fp);
1412                                 return -1;
1413                         }
1414                         continue;
1415                 }
1416
1417                 /*
1418                  *      Perhaps this is an attribute.
1419                  */
1420                 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1421                         if (process_attribute(fn, line, block_vendor,
1422                                               block_tlv[which_block_tlv],
1423                                               argv + 1, argc - 1) == -1) {
1424                                 fclose(fp);
1425                                 return -1;
1426                         }
1427                         continue;
1428                 }
1429
1430                 /*
1431                  *      See if we need to import another dictionary.
1432                  */
1433                 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1434                         if (my_dict_init(dir, argv[1], fn, line) < 0) {
1435                                 fclose(fp);
1436                                 return -1;
1437                         }
1438                         continue;
1439                 } /* $INCLUDE */
1440
1441                 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1442                         if (process_value_alias(fn, line,
1443                                                 argv + 1, argc - 1) == -1) {
1444                                 fclose(fp);
1445                                 return -1;
1446                         }
1447                         continue;
1448                 }
1449
1450                 /*
1451                  *      Process VENDOR lines.
1452                  */
1453                 if (strcasecmp(argv[0], "VENDOR") == 0) {
1454                         if (process_vendor(fn, line,
1455                                            argv + 1, argc - 1) == -1) {
1456                                 fclose(fp);
1457                                 return -1;
1458                         }
1459                         continue;
1460                 }
1461
1462                 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1463                         if (argc != 2) {
1464                                 fr_strerror_printf(
1465                                 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1466                                         fn, line);
1467                                 fclose(fp);
1468                                 return -1;
1469                         }
1470
1471                         da = dict_attrbyname(argv[1]);
1472                         if (!da) {
1473                                 fr_strerror_printf(
1474                                         "dict_init: %s[%d]: unknown attribute %s",
1475                                         fn, line, argv[1]);
1476                                 fclose(fp);
1477                                 return -1;
1478                         }
1479
1480                         if (da->type != PW_TYPE_TLV) {
1481                                 fr_strerror_printf(
1482                                         "dict_init: %s[%d]: attribute %s is not of type tlv",
1483                                         fn, line, argv[1]);
1484                                 fclose(fp);
1485                                 return -1;
1486                         }
1487
1488                         if (which_block_tlv >= 2) {
1489                                 fr_strerror_printf(
1490                                         "dict_init: %s[%d]: TLVs are nested too deep",
1491                                         fn, line);
1492                                 fclose(fp);
1493                                 return -1;
1494                         }
1495
1496                         block_tlv[++which_block_tlv] = da;
1497                         continue;
1498                 } /* BEGIN-TLV */
1499
1500                 if (strcasecmp(argv[0], "END-TLV") == 0) {
1501                         if (argc != 2) {
1502                                 fr_strerror_printf(
1503                                 "dict_init: %s[%d] invalid END-TLV entry",
1504                                         fn, line);
1505                                 fclose(fp);
1506                                 return -1;
1507                         }
1508
1509                         da = dict_attrbyname(argv[1]);
1510                         if (!da) {
1511                                 fr_strerror_printf(
1512                                         "dict_init: %s[%d]: unknown attribute %s",
1513                                         fn, line, argv[1]);
1514                                 fclose(fp);
1515                                 return -1;
1516                         }
1517
1518                         if (da != block_tlv[which_block_tlv]) {
1519                                 fr_strerror_printf(
1520                                         "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1521                                         fn, line, argv[1]);
1522                                 fclose(fp);
1523                                 return -1;
1524                         }
1525                         block_tlv[which_block_tlv--] = NULL;
1526                         continue;
1527                 } /* END-VENDOR */
1528
1529                 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1530                         if (argc != 2) {
1531                                 fr_strerror_printf(
1532                                 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1533                                         fn, line);
1534                                 fclose(fp);
1535                                 return -1;
1536                         }
1537
1538                         vendor = dict_vendorbyname(argv[1]);
1539                         if (!vendor) {
1540                                 fr_strerror_printf(
1541                                         "dict_init: %s[%d]: unknown vendor %s",
1542                                         fn, line, argv[1]);
1543                                 fclose(fp);
1544                                 return -1;
1545                         }
1546                         block_vendor = vendor;
1547                         continue;
1548                 } /* BEGIN-VENDOR */
1549
1550                 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1551                         if (argc != 2) {
1552                                 fr_strerror_printf(
1553                                 "dict_init: %s[%d] invalid END-VENDOR entry",
1554                                         fn, line);
1555                                 fclose(fp);
1556                                 return -1;
1557                         }
1558
1559                         vendor = dict_vendorbyname(argv[1]);
1560                         if (!vendor) {
1561                                 fr_strerror_printf(
1562                                         "dict_init: %s[%d]: unknown vendor %s",
1563                                         fn, line, argv[1]);
1564                                 fclose(fp);
1565                                 return -1;
1566                         }
1567
1568                         if (vendor != block_vendor) {
1569                                 fr_strerror_printf(
1570                                         "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
1571                                         fn, line, argv[1]);
1572                                 fclose(fp);
1573                                 return -1;
1574                         }
1575                         block_vendor = 0;
1576                         continue;
1577                 } /* END-VENDOR */
1578
1579                 /*
1580                  *      Any other string: We don't recognize it.
1581                  */
1582                 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
1583                            fn, line, argv[0]);
1584                 fclose(fp);
1585                 return -1;
1586         }
1587         fclose(fp);
1588         return 0;
1589 }
1590
1591
1592 /*
1593  *      Empty callback for hash table initialization.
1594  */
1595 static int null_callback(void *ctx, void *data)
1596 {
1597         ctx = ctx;              /* -Wunused */
1598         data = data;            /* -Wunused */
1599
1600         return 0;
1601 }
1602
1603
1604 /*
1605  *      Initialize the directory, then fix the attr member of
1606  *      all attributes.
1607  */
1608 int dict_init(const char *dir, const char *fn)
1609 {
1610         /*
1611          *      Check if we need to change anything.  If not, don't do
1612          *      anything.
1613          */
1614         if (dict_stat_check(dir, fn)) {
1615                 return 0;
1616         }
1617
1618         /*
1619          *      Free the dictionaries, and the stat cache.
1620          */
1621         dict_free();
1622         stat_root_dir = strdup(dir);
1623         stat_root_file = strdup(fn);
1624
1625         /*
1626          *      Create the table of vendor by name.   There MAY NOT
1627          *      be multiple vendors of the same name.
1628          *
1629          *      Each vendor is malloc'd, so the free function is free.
1630          */
1631         vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
1632                                                 dict_vendor_name_cmp,
1633                                                 fr_pool_free);
1634         if (!vendors_byname) {
1635                 return -1;
1636         }
1637
1638         /*
1639          *      Create the table of vendors by value.  There MAY
1640          *      be vendors of the same value.  If there are, we
1641          *      pick the latest one.
1642          */
1643         vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
1644                                                  dict_vendor_value_cmp,
1645                                                  fr_pool_free);
1646         if (!vendors_byvalue) {
1647                 return -1;
1648         }
1649
1650         /*
1651          *      Create the table of attributes by name.   There MAY NOT
1652          *      be multiple attributes of the same name.
1653          *
1654          *      Each attribute is malloc'd, so the free function is free.
1655          */
1656         attributes_byname = fr_hash_table_create(dict_attr_name_hash,
1657                                                    dict_attr_name_cmp,
1658                                                    fr_pool_free);
1659         if (!attributes_byname) {
1660                 return -1;
1661         }
1662
1663         /*
1664          *      Create the table of attributes by value.  There MAY
1665          *      be attributes of the same value.  If there are, we
1666          *      pick the latest one.
1667          */
1668         attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
1669                                                     dict_attr_value_cmp,
1670                                                     fr_pool_free);
1671         if (!attributes_byvalue) {
1672                 return -1;
1673         }
1674
1675         values_byname = fr_hash_table_create(dict_value_name_hash,
1676                                                dict_value_name_cmp,
1677                                                fr_pool_free);
1678         if (!values_byname) {
1679                 return -1;
1680         }
1681
1682         values_byvalue = fr_hash_table_create(dict_value_value_hash,
1683                                                 dict_value_value_cmp,
1684                                                 fr_pool_free);
1685         if (!values_byvalue) {
1686                 return -1;
1687         }
1688
1689         value_fixup = NULL;     /* just to be safe. */
1690
1691         if (my_dict_init(dir, fn, NULL, 0) < 0)
1692                 return -1;
1693
1694         if (value_fixup) {
1695                 DICT_ATTR *a;
1696                 value_fixup_t *this, *next;
1697
1698                 for (this = value_fixup; this != NULL; this = next) {
1699                         next = this->next;
1700
1701                         a = dict_attrbyname(this->attrstr);
1702                         if (!a) {
1703                                 fr_strerror_printf(
1704                                         "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
1705                                         this->attrstr, this->dval->name);
1706                                 return -1; /* leak, but they should die... */
1707                         }
1708
1709                         this->dval->attr = a->attr;
1710
1711                         /*
1712                          *      Add the value into the dictionary.
1713                          */
1714                         if (!fr_hash_table_replace(values_byname,
1715                                                      this->dval)) {
1716                                 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
1717                                 return -1;
1718                         }
1719
1720                         /*
1721                          *      Allow them to use the old name, but
1722                          *      prefer the new name when printing
1723                          *      values.
1724                          */
1725                         if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
1726                                 fr_hash_table_replace(values_byvalue,
1727                                                         this->dval);
1728                         }
1729                         free(this);
1730
1731                         /*
1732                          *      Just so we don't lose track of things.
1733                          */
1734                         value_fixup = next;
1735                 }
1736         }
1737
1738         /*
1739          *      Walk over all of the hash tables to ensure they're
1740          *      initialized.  We do this because the threads may perform
1741          *      lookups, and we don't want multi-threaded re-ordering
1742          *      of the table entries.  That would be bad.
1743          */
1744         fr_hash_table_walk(vendors_byname, null_callback, NULL);
1745         fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
1746
1747         fr_hash_table_walk(attributes_byname, null_callback, NULL);
1748         fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
1749
1750         fr_hash_table_walk(values_byvalue, null_callback, NULL);
1751         fr_hash_table_walk(values_byname, null_callback, NULL);
1752
1753         return 0;
1754 }
1755
1756 /*
1757  *      Get an attribute by its numerical value.
1758  */
1759 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
1760 {
1761         DICT_ATTR dattr;
1762
1763         if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
1764
1765         dattr.attr = attr;
1766         dattr.vendor = vendor;
1767
1768         return fr_hash_table_finddata(attributes_byvalue, &dattr);
1769 }
1770
1771 /*
1772  *      Get an attribute by its name.
1773  */
1774 DICT_ATTR *dict_attrbyname(const char *name)
1775 {
1776         DICT_ATTR *da;
1777         uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
1778
1779         if (!name) return NULL;
1780
1781         da = (DICT_ATTR *) buffer;
1782         strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
1783
1784         return fr_hash_table_finddata(attributes_byname, da);
1785 }
1786
1787 /*
1788  *      Associate a value with an attribute and return it.
1789  */
1790 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
1791 {
1792         DICT_VALUE dval, *dv;
1793
1794         /*
1795          *      First, look up aliases.
1796          */
1797         dval.attr = attr;
1798         dval.vendor = vendor;
1799         dval.name[0] = '\0';
1800
1801         /*
1802          *      Look up the attribute alias target, and use
1803          *      the correct attribute number if found.
1804          */
1805         dv = fr_hash_table_finddata(values_byname, &dval);
1806         if (dv) dval.attr = dv->value;
1807
1808         dval.value = value;
1809
1810         return fr_hash_table_finddata(values_byvalue, &dval);
1811 }
1812
1813 /*
1814  *      Get a value by its name, keyed off of an attribute.
1815  */
1816 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
1817 {
1818         DICT_VALUE *my_dv, *dv;
1819         uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
1820
1821         if (!name) return NULL;
1822
1823         my_dv = (DICT_VALUE *) buffer;
1824         my_dv->attr = attr;
1825         my_dv->vendor = vendor;
1826         my_dv->name[0] = '\0';
1827
1828         /*
1829          *      Look up the attribute alias target, and use
1830          *      the correct attribute number if found.
1831          */
1832         dv = fr_hash_table_finddata(values_byname, my_dv);
1833         if (dv) my_dv->attr = dv->value;
1834
1835         strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
1836
1837         return fr_hash_table_finddata(values_byname, my_dv);
1838 }
1839
1840 /*
1841  *      Get the vendor PEC based on the vendor name
1842  *
1843  *      This is efficient only for small numbers of vendors.
1844  */
1845 int dict_vendorbyname(const char *name)
1846 {
1847         DICT_VENDOR *dv;
1848         uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
1849
1850         if (!name) return 0;
1851
1852         dv = (DICT_VENDOR *) buffer;
1853         strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
1854
1855         dv = fr_hash_table_finddata(vendors_byname, dv);
1856         if (!dv) return 0;
1857
1858         return dv->vendorpec;
1859 }
1860
1861 /*
1862  *      Return the vendor struct based on the PEC.
1863  */
1864 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
1865 {
1866         DICT_VENDOR dv;
1867
1868         dv.vendorpec = vendorpec;
1869
1870         return fr_hash_table_finddata(vendors_byvalue, &dv);
1871 }