More descriptive errors in the dictionary parser
[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         { "extended",   PW_TYPE_EXTENDED },
98         { "extended-flags",     PW_TYPE_EXTENDED_FLAGS },
99         { "evs",        PW_TYPE_EVS },
100         { "uint8",      PW_TYPE_BYTE },
101         { "uint16",     PW_TYPE_SHORT },
102         { "uint32",     PW_TYPE_INTEGER },
103         { "int32",      PW_TYPE_SIGNED },
104         { NULL, 0 }
105 };
106
107
108 /*
109  *      WiMAX craziness.
110  */
111 #define MAX_TLV_NEST (4)
112 /*
113  *      Bit packing:
114  *      8 bits of base VSA
115  *      8 bits for nested TLV 1
116  *      8 bits for nested TLV 2
117  *      5 bits for nested TLV 3
118  *      3 bits for nested TLV 4
119  */
120 const int fr_attr_max_tlv = MAX_TLV_NEST;
121 const int fr_attr_shift[MAX_TLV_NEST + 1] = {
122   0, 8, 16, 24, 29
123 };
124
125 const int fr_attr_mask[MAX_TLV_NEST + 1] = {
126   0xff, 0xff, 0xff, 0x1f, 0x07
127 };
128
129
130 /*
131  *      Create the hash of the name.
132  *
133  *      We copy the hash function here because it's substantially faster.
134  */
135 #define FNV_MAGIC_INIT (0x811c9dc5)
136 #define FNV_MAGIC_PRIME (0x01000193)
137
138 static uint32_t dict_hashname(const char *name)
139 {
140         uint32_t hash = FNV_MAGIC_INIT;
141         const char *p;
142
143         for (p = name; *p != '\0'; p++) {
144                 int c = *(const unsigned char *) p;
145                 if (isalpha(c)) c = tolower(c);
146
147                 hash *= FNV_MAGIC_PRIME;
148                 hash ^= (uint32_t ) (c & 0xff);
149         }
150
151         return hash;
152 }
153
154
155 /*
156  *      Hash callback functions.
157  */
158 static uint32_t dict_attr_name_hash(const void *data)
159 {
160         return dict_hashname(((const DICT_ATTR *)data)->name);
161 }
162
163 static int dict_attr_name_cmp(const void *one, const void *two)
164 {
165         const DICT_ATTR *a = one;
166         const DICT_ATTR *b = two;
167
168         return strcasecmp(a->name, b->name);
169 }
170
171 static uint32_t dict_attr_value_hash(const void *data)
172 {
173         uint32_t hash;
174         const DICT_ATTR *attr = data;
175
176         hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
177         return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
178 }
179
180 static int dict_attr_value_cmp(const void *one, const void *two)
181 {
182         const DICT_ATTR *a = one;
183         const DICT_ATTR *b = two;
184
185         if (a->vendor < b->vendor) return -1;
186         if (a->vendor > b->vendor) return +1;
187
188         return a->attr - b->attr;
189 }
190
191 static uint32_t dict_vendor_name_hash(const void *data)
192 {
193         return dict_hashname(((const DICT_VENDOR *)data)->name);
194 }
195
196 static int dict_vendor_name_cmp(const void *one, const void *two)
197 {
198         const DICT_VENDOR *a = one;
199         const DICT_VENDOR *b = two;
200
201         return strcasecmp(a->name, b->name);
202 }
203
204 static uint32_t dict_vendor_value_hash(const void *data)
205 {
206         return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
207                          sizeof(((const DICT_VENDOR *)data)->vendorpec));
208 }
209
210 static int dict_vendor_value_cmp(const void *one, const void *two)
211 {
212         const DICT_VENDOR *a = one;
213         const DICT_VENDOR *b = two;
214
215         return a->vendorpec - b->vendorpec;
216 }
217
218 static uint32_t dict_value_name_hash(const void *data)
219 {
220         uint32_t hash;
221         const DICT_VALUE *dval = data;
222
223         hash = dict_hashname(dval->name);
224         hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
225         return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
226 }
227
228 static int dict_value_name_cmp(const void *one, const void *two)
229 {
230         int rcode;
231         const DICT_VALUE *a = one;
232         const DICT_VALUE *b = two;
233
234         rcode = a->attr - b->attr;
235         if (rcode != 0) return rcode;
236
237         rcode = a->vendor - b->vendor;
238         if (rcode != 0) return rcode;
239
240         return strcasecmp(a->name, b->name);
241 }
242
243 static uint32_t dict_value_value_hash(const void *data)
244 {
245         uint32_t hash;
246         const DICT_VALUE *dval = data;
247
248         hash = fr_hash(&dval->attr, sizeof(dval->attr));
249         hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
250         return fr_hash_update(&dval->value, sizeof(dval->value), hash);
251 }
252
253 static int dict_value_value_cmp(const void *one, const void *two)
254 {
255         int rcode;
256         const DICT_VALUE *a = one;
257         const DICT_VALUE *b = two;
258
259         if (a->vendor < b->vendor) return -1;
260         if (a->vendor > b->vendor) return +1;
261
262         rcode = a->attr - b->attr;
263         if (rcode != 0) return rcode;
264
265         return a->value - b->value;
266 }
267
268
269 /*
270  *      Free the list of stat buffers
271  */
272 static void dict_stat_free(void)
273 {
274         dict_stat_t *this, *next;
275
276         free(stat_root_dir);
277         stat_root_dir = NULL;
278         free(stat_root_file);
279         stat_root_file = NULL;
280
281         if (!stat_head) {
282                 stat_tail = NULL;
283                 return;
284         }
285
286         for (this = stat_head; this != NULL; this = next) {
287                 next = this->next;
288                 free(this->name);
289                 free(this);
290         }
291
292         stat_head = stat_tail = NULL;
293 }
294
295
296 /*
297  *      Add an entry to the list of stat buffers.
298  */
299 static void dict_stat_add(const char *name, const struct stat *stat_buf)
300 {
301         dict_stat_t *this;
302
303         this = malloc(sizeof(*this));
304         if (!this) return;
305         memset(this, 0, sizeof(*this));
306
307         this->name = strdup(name);
308         this->mtime = stat_buf->st_mtime;
309
310         if (!stat_head) {
311                 stat_head = stat_tail = this;
312         } else {
313                 stat_tail->next = this;
314                 stat_tail = this;
315         }
316 }
317
318
319 /*
320  *      See if any dictionaries have changed.  If not, don't
321  *      do anything.
322  */
323 static int dict_stat_check(const char *root_dir, const char *root_file)
324 {
325         struct stat buf;
326         dict_stat_t *this;
327
328         if (!stat_root_dir) return 0;
329         if (!stat_root_file) return 0;
330
331         if (strcmp(root_dir, stat_root_dir) != 0) return 0;
332         if (strcmp(root_file, stat_root_file) != 0) return 0;
333
334         if (!stat_head) return 0; /* changed, reload */
335
336         for (this = stat_head; this != NULL; this = this->next) {
337                 if (stat(this->name, &buf) < 0) return 0;
338
339                 if (buf.st_mtime != this->mtime) return 0;
340         }
341
342         return 1;
343 }
344
345 typedef struct fr_pool_t {
346         void    *page_end;
347         void    *free_ptr;
348         struct fr_pool_t *page_free;
349         struct fr_pool_t *page_next;
350 } fr_pool_t;
351
352 #define FR_POOL_SIZE (32768)
353 #define FR_ALLOC_ALIGN (8)
354
355 static fr_pool_t *dict_pool = NULL;
356
357 static fr_pool_t *fr_pool_create(void)
358 {
359         fr_pool_t *fp = malloc(FR_POOL_SIZE);
360
361         if (!fp) return NULL;
362
363         memset(fp, 0, FR_POOL_SIZE);
364
365         fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
366         fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
367         fp->page_free = fp;
368         fp->page_next = NULL;
369         return fp;
370 }
371
372 static void fr_pool_delete(fr_pool_t **pfp)
373 {
374         fr_pool_t *fp, *next;
375
376         if (!pfp || !*pfp) return;
377
378         for (fp = *pfp; fp != NULL; fp = next) {
379                 next = fp->page_next;
380                 fp->page_next = NULL;
381                 free(fp);
382         }
383         *pfp = NULL;
384 }
385
386
387 static void *fr_pool_alloc(size_t size)
388 {
389         void *ptr;
390
391         if (size == 0) return NULL;
392
393         if (size > 256) return NULL; /* shouldn't happen */
394
395         if (!dict_pool) {
396                 dict_pool = fr_pool_create();
397                 if (!dict_pool) return NULL;
398         }
399
400         if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
401                 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
402         }
403
404         if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
405                 dict_pool->page_free->page_next = fr_pool_create();
406                 if (!dict_pool->page_free->page_next) return NULL;
407                 dict_pool->page_free = dict_pool->page_free->page_next;
408         }
409
410         ptr = dict_pool->page_free->free_ptr;
411         dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
412
413         return ptr;
414 }
415
416
417 static void fr_pool_free(UNUSED void *ptr)
418 {
419         /*
420          *      Place-holder for later code.
421          */
422 }
423
424 /*
425  *      Free the dictionary_attributes and dictionary_values lists.
426  */
427 void dict_free(void)
428 {
429         /*
430          *      Free the tables
431          */
432         fr_hash_table_free(vendors_byname);
433         fr_hash_table_free(vendors_byvalue);
434         vendors_byname = NULL;
435         vendors_byvalue = NULL;
436
437         fr_hash_table_free(attributes_byname);
438         fr_hash_table_free(attributes_byvalue);
439         attributes_byname = NULL;
440         attributes_byvalue = NULL;
441
442         fr_hash_table_free(values_byname);
443         fr_hash_table_free(values_byvalue);
444         values_byname = NULL;
445         values_byvalue = NULL;
446
447         memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
448
449         fr_pool_delete(&dict_pool);
450
451         dict_stat_free();
452 }
453
454
455 /*
456  *      Add vendor to the list.
457  */
458 int dict_addvendor(const char *name, unsigned int value)
459 {
460         size_t length;
461         DICT_VENDOR *dv;
462
463         if (value > FR_MAX_VENDOR) {
464                 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
465                 return -1;
466         }
467
468         if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
469                 fr_strerror_printf("dict_addvendor: vendor name too long");
470                 return -1;
471         }
472
473         if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
474                 fr_strerror_printf("dict_addvendor: out of memory");
475                 return -1;
476         }
477
478         strcpy(dv->name, name);
479         dv->vendorpec  = value;
480         dv->type = dv->length = 1; /* defaults */
481
482         if (!fr_hash_table_insert(vendors_byname, dv)) {
483                 DICT_VENDOR *old_dv;
484
485                 old_dv = fr_hash_table_finddata(vendors_byname, dv);
486                 if (!old_dv) {
487                         fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
488                         return -1;
489                 }
490                 if (old_dv->vendorpec != dv->vendorpec) {
491                         fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
492                         return -1;
493                 }
494
495                 /*
496                  *      Already inserted.  Discard the duplicate entry.
497                  */
498                 fr_pool_free(dv);
499                 return 0;
500         }
501
502         /*
503          *      Insert the SAME pointer (not free'd when this table is
504          *      deleted), into another table.
505          *
506          *      We want this behaviour because we want OLD names for
507          *      the attributes to be read from the configuration
508          *      files, but when we're printing them, (and looking up
509          *      by value) we want to use the NEW name.
510          */
511         if (!fr_hash_table_replace(vendors_byvalue, dv)) {
512                 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
513                            name);
514                 return -1;
515         }
516
517         return 0;
518 }
519
520 /*
521  *      Add an attribute to the dictionary.
522  */
523 int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
524                  ATTR_FLAGS flags)
525 {
526         size_t namelen;
527         static int      max_attr = 0;
528         DICT_ATTR       *da;
529
530         namelen = strlen(name);
531         if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
532                 fr_strerror_printf("dict_addattr: attribute name too long");
533                 return -1;
534         }
535
536         /*
537          *      If the attr is '-1', that means use a pre-existing
538          *      one (if it already exists).  If one does NOT already exist,
539          *      then create a new attribute, with a non-conflicting value,
540          *      and use that.
541          */
542         if (attr == -1) {
543                 if (dict_attrbyname(name)) {
544                         return 0; /* exists, don't add it again */
545                 }
546
547                 attr = ++max_attr;
548
549         } else if (vendor == 0) {
550                 /*
551                  *  Update 'max_attr'
552                  */
553                 if (attr > max_attr) {
554                         max_attr = attr;
555                 }
556         }
557
558         /*
559          *      Additional checks for extended attributes.
560          */
561         if (flags.extended || flags.extended_flags || flags.evs) {
562                 if (vendor != 0) {
563                         fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
564                         return -1;
565                 }
566                 vendor = VENDORPEC_EXTENDED;
567
568                 if (flags.has_tag
569 #ifdef WITH_DHCP
570                     || flags.array
571 #endif
572                     || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
573                         fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
574                         return -1;
575                 }
576         }
577
578         if (flags.evs) {
579                 if (!(flags.extended || flags.extended_flags)) {
580                         fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
581                         return -1;
582                 }
583
584                 if (vendor <= FR_MAX_VENDOR) {
585                         fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
586                         return -1;
587                 }
588         }
589                 
590         if (attr < 0) {
591                 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
592                 return -1;
593         }
594
595         if (flags.has_tlv && flags.length) {
596                 fr_strerror_printf("TLVs cannot have a fixed length");
597                 return -1;
598         }
599
600         if (vendor && (vendor != VENDORPEC_EXTENDED)) {
601                 DICT_VENDOR *dv;
602                 static DICT_VENDOR *last_vendor = NULL;
603
604                 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
605                         fr_strerror_printf("TLV's cannot be encrypted");
606                         return -1;
607                 }
608
609                 if (flags.is_tlv && flags.has_tag) {
610                         fr_strerror_printf("Sub-TLV's cannot have a tag");
611                         return -1;
612                 }
613
614                 if (flags.has_tlv && flags.has_tag) {
615                         fr_strerror_printf("TLV's cannot have a tag");
616                         return -1;
617                 }
618
619                 /*
620                  *      Most ATTRIBUTEs are bunched together by
621                  *      VENDOR.  We can save a lot of lookups on
622                  *      dictionary initialization by caching the last
623                  *      vendor.
624                  */
625                 if (last_vendor &&
626                     ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
627                         dv = last_vendor;
628                 } else {
629                         /*
630                          *      Ignore the high byte (sigh)
631                          */
632                         dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
633                         last_vendor = dv;
634                 }
635
636                 /*
637                  *      If the vendor isn't defined, die.
638                  */
639                 if (!dv) {
640                         fr_strerror_printf("dict_addattr: Unknown vendor %u",
641                                            vendor);
642                         return -1;
643                 }
644
645                 /*
646                  *      FIXME: Switch over dv->type, and limit things
647                  *      properly.
648                  */
649                 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
650                         fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
651                         return -1;
652                 } /* else 256..65535 are allowed */
653
654                 /*
655                  *      Set the extended flags as appropriate.
656                  */
657                 if (vendor > FR_MAX_VENDOR) {
658                         unsigned int myattr;
659
660                         myattr = (vendor >> 24) & 0xff;
661                         myattr |= PW_VENDOR_SPECIFIC << 8;
662
663                         da = dict_attrbyvalue(myattr, VENDORPEC_EXTENDED);
664                         if (!da) {
665                                 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown \"evs\" type.");
666                                 return -1;
667                         }
668                         flags.extended = da->flags.extended;
669                         flags.extended_flags = da->flags.extended_flags;
670                         flags.evs = da->flags.evs;
671                 }
672
673                 /*
674                  *      <sigh> Alvarion, being *again* a horribly
675                  *      broken vendor, has re-used the WiMAX format in
676                  *      their proprietary vendor space.  This re-use
677                  *      means that there are *multiple* conflicting
678                  *      Alvarion dictionaries.
679                  */
680                 flags.wimax = dv->flags;
681         }
682
683         /*
684          *      Create a new attribute for the list
685          */
686         if ((da = fr_pool_alloc(sizeof(*da) + namelen)) == NULL) {
687                 fr_strerror_printf("dict_addattr: out of memory");
688                 return -1;
689         }
690
691         memcpy(da->name, name, namelen);
692         da->name[namelen] = '\0';
693         da->attr = attr;
694         da->vendor = vendor;
695         da->type = type;
696         da->flags = flags;
697
698         /*
699          *      Insert the attribute, only if it's not a duplicate.
700          */
701         if (!fr_hash_table_insert(attributes_byname, da)) {
702                 DICT_ATTR       *a;
703
704                 /*
705                  *      If the attribute has identical number, then
706                  *      ignore the duplicate.
707                  */
708                 a = fr_hash_table_finddata(attributes_byname, da);
709                 if (a && (strcasecmp(a->name, da->name) == 0)) {
710                         if (a->attr != da->attr) {
711                                 fr_strerror_printf("dict_addattr: Duplicate attribute name %s", name);
712                                 fr_pool_free(da);
713                                 return -1;
714                         }
715
716                         /*
717                          *      Same name, same vendor, same attr,
718                          *      maybe the flags and/or type is
719                          *      different.  Let the new value
720                          *      over-ride the old one.
721                          */
722                 }
723
724
725                 fr_hash_table_delete(attributes_byvalue, a);
726
727                 if (!fr_hash_table_replace(attributes_byname, da)) {
728                         fr_strerror_printf("dict_addattr: Internal error storing attribute %s", name);
729                         fr_pool_free(da);
730                         return -1;
731                 }
732         }
733
734         /*
735          *      Insert the SAME pointer (not free'd when this entry is
736          *      deleted), into another table.
737          *
738          *      We want this behaviour because we want OLD names for
739          *      the attributes to be read from the configuration
740          *      files, but when we're printing them, (and looking up
741          *      by value) we want to use the NEW name.
742          */
743         if (!fr_hash_table_replace(attributes_byvalue, da)) {
744                 fr_strerror_printf("dict_addattr: Failed inserting attribute name %s", name);
745                 return -1;
746         }
747
748         if (!vendor && (attr > 0) && (attr < 256)) {
749                  dict_base_attrs[attr] = da;
750         }
751
752         return 0;
753 }
754
755
756 /*
757  *      Add a value for an attribute to the dictionary.
758  */
759 int dict_addvalue(const char *namestr, const char *attrstr, int value)
760 {
761         size_t          length;
762         DICT_ATTR       *dattr;
763         DICT_VALUE      *dval;
764
765         static DICT_ATTR *last_attr = NULL;
766
767         if (!*namestr) {
768                 fr_strerror_printf("dict_addvalue: empty names are not permitted");
769                 return -1;
770         }
771
772         if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
773                 fr_strerror_printf("dict_addvalue: value name too long");
774                 return -1;
775         }
776
777         if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
778                 fr_strerror_printf("dict_addvalue: out of memory");
779                 return -1;
780         }
781         memset(dval, 0, sizeof(*dval));
782
783         strcpy(dval->name, namestr);
784         dval->value = value;
785
786         /*
787          *      Most VALUEs are bunched together by ATTRIBUTE.  We can
788          *      save a lot of lookups on dictionary initialization by
789          *      caching the last attribute.
790          */
791         if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
792                 dattr = last_attr;
793         } else {
794                 dattr = dict_attrbyname(attrstr);
795                 last_attr = dattr;
796         }
797
798         /*
799          *      Remember which attribute is associated with this
800          *      value, if possible.
801          */
802         if (dattr) {
803                 if (dattr->flags.has_value_alias) {
804                         fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
805                         return -1;
806                 }
807
808                 dval->attr = dattr->attr;
809                 dval->vendor = dattr->vendor;
810
811                 /*
812                  *      Enforce valid values
813                  *
814                  *      Don't worry about fixups...
815                  */
816                 switch (dattr->type) {
817                         case PW_TYPE_BYTE:
818                                 if (value > 255) {
819                                         fr_pool_free(dval);
820                                         fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
821                                         return -1;
822                                 }
823                                 break;
824                         case PW_TYPE_SHORT:
825                                 if (value > 65535) {
826                                         fr_pool_free(dval);
827                                         fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
828                                         return -1;
829                                 }
830                                 break;
831
832                                 /*
833                                  *      Allow octets for now, because
834                                  *      of dictionary.cablelabs
835                                  */
836                         case PW_TYPE_OCTETS:
837
838                         case PW_TYPE_INTEGER:
839                                 break;
840
841                         default:
842                                 fr_pool_free(dval);
843                                 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
844                                            fr_int2str(type_table, dattr->type, "?Unknown?"));
845                                 return -1;
846                 }
847
848                 dattr->flags.has_value = 1;
849         } else {
850                 value_fixup_t *fixup;
851
852                 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
853                 if (!fixup) {
854                         fr_pool_free(dval);
855                         fr_strerror_printf("dict_addvalue: out of memory");
856                         return -1;
857                 }
858                 memset(fixup, 0, sizeof(*fixup));
859
860                 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
861                 fixup->dval = dval;
862
863                 /*
864                  *      Insert to the head of the list.
865                  */
866                 fixup->next = value_fixup;
867                 value_fixup = fixup;
868
869                 return 0;
870         }
871
872         /*
873          *      Add the value into the dictionary.
874          */
875         if (!fr_hash_table_insert(values_byname, dval)) {
876                 if (dattr) {
877                         DICT_VALUE *old;
878
879                         /*
880                          *      Suppress duplicates with the same
881                          *      name and value.  There are lots in
882                          *      dictionary.ascend.
883                          */
884                         old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
885                         if (old && (old->value == dval->value)) {
886                                 fr_pool_free(dval);
887                                 return 0;
888                         }
889                 }
890
891                 fr_pool_free(dval);
892                 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
893                 return -1;
894         }
895
896         /*
897          *      There are multiple VALUE's, keyed by attribute, so we
898          *      take care of that here.
899          */
900         if (!fr_hash_table_replace(values_byvalue, dval)) {
901                 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
902                            namestr);
903                 return -1;
904         }
905
906         return 0;
907 }
908
909 static int sscanf_i(const char *str, unsigned int *pvalue)
910 {
911         int rcode = 0;
912         int base = 10;
913         static const char *tab = "0123456789";
914
915         if ((str[0] == '0') &&
916             ((str[1] == 'x') || (str[1] == 'X'))) {
917                 tab = "0123456789abcdef";
918                 base = 16;
919
920                 str += 2;
921         }
922
923         while (*str) {
924                 const char *c;
925
926                 if (*str == '.') break;
927
928                 c = memchr(tab, tolower((int) *str), base);
929                 if (!c) return 0;
930
931                 rcode *= base;
932                 rcode += (c - tab);
933                 str++;
934         }
935
936         *pvalue = rcode;
937         return 1;
938 }
939
940
941 int dict_str2oid(const char *ptr, unsigned int *pvalue, int vendor, int tlv_depth)
942 {
943         const char *p;
944         unsigned int value;
945         DICT_ATTR *da;
946
947         if (tlv_depth > fr_attr_max_tlv) {
948                 fr_strerror_printf("Attribute has too long OID");
949                 return 0;
950         }
951
952         if (*pvalue) {
953                 if (vendor) {
954                         da = dict_attrbyvalue(*pvalue, vendor);
955                 } else {
956                         da = dict_attrbyvalue(*pvalue, VENDORPEC_EXTENDED);
957                 }
958                 if (!da) {
959                         fr_strerror_printf("Parent attribute is undefined.");
960                         return 0;
961                 }
962         
963                 if (!(da->flags.has_tlv || da->flags.extended || da->flags.extended_flags)) {
964                         fr_strerror_printf("Parent attribute %s cannot have sub-tlvs",
965                                            da->name);
966                         return 0;
967                 }
968         }
969
970         p = strchr(ptr, '.');
971
972         if (!sscanf_i(ptr, &value)) {
973                 fr_strerror_printf("Failed parsing attribute identifier %s",
974                                    ptr);
975                 return 0;
976         }
977
978         if (*pvalue) {
979                 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
980         } else {
981                 *pvalue = value;
982         }
983
984         if (p) {
985                 return dict_str2oid(p + 1, pvalue, vendor, tlv_depth + 1);
986         }
987
988         return tlv_depth;
989 }
990
991
992 /*
993  *      Process the ATTRIBUTE command
994  */
995 static int process_attribute(const char* fn, const int line,
996                              const unsigned int block_vendor, DICT_ATTR *block_tlv,
997                              int tlv_depth, char **argv, int argc)
998 {
999         int             oid = 0;
1000         unsigned int    vendor = 0;
1001         unsigned int    value;
1002         int             type;
1003         unsigned int    length = 0;
1004         ATTR_FLAGS      flags;
1005         char            *p;
1006
1007         if ((argc < 3) || (argc > 4)) {
1008                 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
1009                         fn, line);
1010                 return -1;
1011         }
1012
1013         if (strncmp(argv[1], "Attr-", 5) == 0) {
1014                 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1015                                    fn, line);
1016                 return -1;
1017         }
1018
1019         memset(&flags, 0, sizeof(flags));
1020
1021         /*
1022          *      Look for extended attributes before doing anything else.
1023          */
1024         p = strchr(argv[1], '.');
1025         if (p) {
1026                 *p = '\0';
1027                 oid = 1;
1028         }
1029
1030         /*
1031          *      Validate all entries
1032          */
1033         if (!sscanf_i(argv[1], &value)) {
1034                 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1035                 return -1;
1036         }
1037
1038         if (!p) {
1039                 if (value > (1 << 24)) {
1040                         fr_strerror_printf("dict_init: %s[%d]: Attribute number is too large", fn, line);
1041                         return -1;
1042                 }
1043
1044                 
1045                 /*
1046                  *      Parse NUM.NUM.NUM.NUM
1047                  */
1048         } else {
1049                 int my_depth;
1050                 DICT_ATTR *da;
1051
1052                 *p = '.';       /* reset for later printing */
1053
1054                 if (block_vendor) {
1055                         da = dict_attrbyvalue(value, block_vendor);
1056                 } else if (value == PW_VENDOR_SPECIFIC) {
1057                         char *q;
1058                         const DICT_VENDOR *dv;
1059
1060                         p++;
1061                         q = strchr(p, '.');
1062                         if (!q) {
1063                         invalid:
1064                                 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier", fn, line);
1065                                 return -1;
1066                         }
1067                         *q = '\0';
1068
1069                         if (!sscanf_i(p, &vendor)) {
1070                                 *q = '.';
1071                                 goto invalid;
1072                         }
1073                         *(q++) = '.';
1074                         p = q;
1075                         dv = dict_vendorbyvalue(vendor);
1076                         if (!dv) {
1077                                 fr_strerror_printf("dict_init: %s[%d]: Unknown vendor \"%u\" in attribute identifier", fn, line, vendor);
1078                                 return -1;
1079                         }
1080
1081                         argv[1] = p;
1082                         return process_attribute(fn, line, vendor, NULL, 0,
1083                                                  argv, argc);
1084                 } else {
1085                         da = dict_attrbyvalue(value, VENDORPEC_EXTENDED);
1086                 }
1087                 if (!da) {
1088                         fr_strerror_printf("dict_init: %s[%d]: Entry refers to unknown attribute %d", fn, line, value);
1089                         return -1;
1090                 }
1091
1092                 /*
1093                  *      241.1 means 241 is of type "extended".
1094                  *      Otherwise, die.
1095                  */
1096                 if (!(da->flags.has_tlv || da->flags.extended || da->flags.extended_flags)) {
1097                         fr_strerror_printf("dict_init: %s[%d]: Parent attribute %s cannot contain sub-attributes", fn, line, da->name);
1098                         return -1;
1099                 }
1100
1101                 my_depth = dict_str2oid(p + 1, &value, block_vendor, tlv_depth + 1);
1102                 if (!my_depth) {
1103                         char buffer[256];
1104
1105                         strlcpy(buffer, fr_strerror(), sizeof(buffer));
1106                         
1107                         fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1108                         return -1;
1109                 }
1110
1111 #if 0
1112                 /*
1113                  *      Look up the REAL parent TLV.
1114                  */
1115                 if (my_depth > 1) {
1116                         unsigned int parent = value;
1117
1118                         parent &= ~(fr_attr_mask[my_depth] << fr_attr_shift[my_depth]);
1119
1120                         da = dict_attrbyvalue(parent, block_);
1121                         if (!da) {
1122                                 fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
1123                                 return -1;
1124                         }
1125                 }
1126 #endif
1127                 
1128                 /*
1129                  *      Set which type of attribute this is.
1130                  */
1131                 flags.extended = da->flags.extended;
1132                 flags.extended_flags = da->flags.extended_flags;
1133                 flags.evs = da->flags.evs;
1134                 if (da->flags.has_tlv) flags.is_tlv = 1;
1135         }
1136
1137         if (strncmp(argv[2], "octets[", 7) != 0) {
1138                 /*
1139                  *      find the type of the attribute.
1140                  */
1141                 type = fr_str2int(type_table, argv[2], -1);
1142                 if (type < 0) {
1143                         fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1144                                            fn, line, argv[2]);
1145                         return -1;
1146                 }
1147
1148         } else {
1149                 type = PW_TYPE_OCTETS;
1150                 
1151                 p = strchr(argv[2] + 7, ']');
1152                 if (!p) {
1153                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1154                         return -1;
1155                 }
1156
1157                 *p = 0;
1158
1159                 if (!sscanf_i(argv[1], &length)) {
1160                         fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1161                         return -1;
1162                 }
1163
1164                 if ((length == 0) || (length > 253)) {
1165                         fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1166                         return -1;
1167                 }
1168         }
1169
1170         /*
1171          *      Only look up the vendor if the string
1172          *      is non-empty.
1173          */
1174         if (argc < 4) {
1175                 /*
1176                  *      Force "length" for data types of fixed length;
1177                  */
1178                 switch (type) {
1179                 case PW_TYPE_BYTE:
1180                         length = 1;
1181                         break;
1182
1183                 case PW_TYPE_SHORT:
1184                         length = 2;
1185                         break;
1186
1187                 case PW_TYPE_DATE:
1188                 case PW_TYPE_IPADDR:
1189                 case PW_TYPE_INTEGER:
1190                 case PW_TYPE_SIGNED:
1191                         length = 4;
1192                         break;
1193
1194                 case PW_TYPE_ETHERNET:
1195                         length = 6;
1196                         break;
1197
1198                 case PW_TYPE_IFID:
1199                         length = 8;
1200                         break;
1201
1202                 case PW_TYPE_IPV6ADDR:
1203                         length = 16;
1204                         break;
1205
1206                 case PW_TYPE_EXTENDED:
1207                         if ((vendor != 0) || (value < 241)) {
1208                                 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended\" MUST be RFC attributes with value >= 241.", fn, line);
1209                                 return -1;
1210                         }
1211                         type = PW_TYPE_OCTETS;
1212                         flags.extended = 1;
1213                         break;
1214
1215                 case PW_TYPE_EXTENDED_FLAGS:
1216                         if ((vendor != 0) || (value < 241)) {
1217                                 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended-flags\" MUST be RFC attributes with value >= 241.", fn, line);
1218                                 return -1;
1219                         }
1220                         type = PW_TYPE_OCTETS;
1221                         flags.extended_flags = 1;
1222                         break;
1223
1224                 case PW_TYPE_EVS:
1225                         type = PW_TYPE_OCTETS;
1226                         flags.evs = 1;
1227                         if (((value >> fr_attr_shift[1]) & fr_attr_mask[1]) != PW_VENDOR_SPECIFIC) {
1228                                 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1229                                 return -1;
1230                         }
1231                         break;
1232
1233                 default:
1234                         break;
1235                 }
1236
1237                 flags.length = length;
1238
1239         } else {                /* argc == 4: we have options */
1240                 char *key, *next, *last;
1241
1242                 /*
1243                  *      Keep it real.
1244                  */
1245                 if (flags.extended || flags.extended_flags || flags.evs) {
1246                         fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1247                         return -1;
1248                 }
1249
1250                 if (length != 0) {
1251                         fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1252                         return -1;
1253                 }
1254
1255                 key = argv[3];
1256                 do {
1257                         next = strchr(key, ',');
1258                         if (next) *(next++) = '\0';
1259
1260                         if (strcmp(key, "has_tag") == 0 ||
1261                             strcmp(key, "has_tag=1") == 0) {
1262                                 /* Boolean flag, means this is a
1263                                    tagged attribute */
1264                                 flags.has_tag = 1;
1265                                 
1266                         } else if (strncmp(key, "encrypt=", 8) == 0) {
1267                                 /* Encryption method, defaults to 0 (none).
1268                                    Currently valid is just type 2,
1269                                    Tunnel-Password style, which can only
1270                                    be applied to strings. */
1271                                 flags.encrypt = strtol(key + 8, &last, 0);
1272                                 if (*last) {
1273                                         fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1274                                                     fn, line, key);
1275                                         return -1;
1276                                 }
1277                                 
1278                         } else if (strncmp(key, "array", 6) == 0) {
1279                                 flags.array = 1;
1280                                 
1281                                 switch (type) {
1282                                         case PW_TYPE_IPADDR:
1283                                         case PW_TYPE_BYTE:
1284                                         case PW_TYPE_SHORT:
1285                                         case PW_TYPE_INTEGER:
1286                                         case PW_TYPE_DATE:
1287                                                 break;
1288
1289                                         default:
1290                                                 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1291                                                             fn, line);
1292                                                 return -1;
1293                                 }
1294
1295                                 /*
1296                                  *      The only thing is the vendor name,
1297                                  *      and it's a known name: allow it.
1298                                  */
1299                         } else if ((key == argv[3]) && !next) {
1300                                 if (oid) {
1301                                         fr_strerror_printf( "dict_init: %s[%d] New-style attributes cannot use a vendor flag.",
1302                                                             fn, line);
1303                                         return -1;
1304                                 }
1305
1306                                 if (block_vendor) {
1307                                         fr_strerror_printf( "dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" is not allowed.",
1308                                                             fn, line);
1309                                         return -1;
1310                                 }
1311
1312                                 vendor = dict_vendorbyname(key);
1313                                 if (!vendor) goto unknown;
1314                                 break;
1315
1316                         } else {
1317                         unknown:
1318                                 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1319                                             fn, line, key);
1320                                 return -1;
1321                         }
1322
1323                         key = next;
1324                         if (key && !*key) break;
1325                 } while (key);
1326         }
1327
1328         if (block_vendor) vendor = block_vendor;
1329
1330         /*
1331          *      Special checks for tags, they make our life much more
1332          *      difficult.
1333          */
1334         if (flags.has_tag) {
1335                 /*
1336                  *      Only string, octets, and integer can be tagged.
1337                  */
1338                 switch (type) {
1339                 case PW_TYPE_STRING:
1340                 case PW_TYPE_INTEGER:
1341                         break;
1342
1343                 default:
1344                         fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1345                                    fn, line,
1346                                    fr_int2str(type_table, type, "?Unknown?"));
1347                         return -1;
1348                 }
1349         }
1350
1351         if (type == PW_TYPE_TLV) {
1352                 if (vendor
1353 #ifdef WITH_DHCP
1354                     && (vendor != DHCP_MAGIC_VENDOR)
1355 #endif
1356                         ) {
1357                         DICT_VENDOR *dv;
1358
1359                         dv = dict_vendorbyvalue(vendor);
1360                         if (!dv || (dv->type != 1) || (dv->length != 1)) {
1361                                 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1362                                                    fn, line);
1363                                 return -1;
1364                         }
1365
1366                 }
1367                 flags.has_tlv = 1;
1368         }
1369         
1370         if (block_tlv) {
1371                 /*
1372                  *      TLV's can be only one octet.
1373                  */
1374                 if ((value <= 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1375                         fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1376                                             fn, line);
1377                         return -1;
1378                 }
1379
1380                 /*
1381                  *      
1382                  */
1383                 value <<= fr_attr_shift[tlv_depth];
1384                 value |= block_tlv->attr;
1385                 flags.is_tlv = 1;
1386         }
1387
1388 #ifdef WITH_DICTIONARY_WARNINGS
1389         /*
1390          *      Hack to help us discover which vendors have illegal
1391          *      attributes.
1392          */
1393         if (!vendor && (value < 256) &&
1394             !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1395                 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1396                         argv[0], fn);
1397         }
1398 #endif
1399
1400         /*
1401          *      Add it in.
1402          */
1403         if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1404                 char buffer[256];
1405
1406                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1407
1408                 fr_strerror_printf("dict_init: %s[%d]: %s",
1409                                    fn, line, buffer);
1410                 return -1;
1411         }
1412
1413         return 0;
1414 }
1415
1416
1417 /*
1418  *      Process the VALUE command
1419  */
1420 static int process_value(const char* fn, const int line, char **argv,
1421                          int argc)
1422 {
1423         unsigned int    value;
1424
1425         if (argc != 3) {
1426                 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1427                         fn, line);
1428                 return -1;
1429         }
1430         /*
1431          *      For Compatibility, skip "Server-Config"
1432          */
1433         if (strcasecmp(argv[0], "Server-Config") == 0)
1434                 return 0;
1435
1436         /*
1437          *      Validate all entries
1438          */
1439         if (!sscanf_i(argv[2], &value)) {
1440                 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1441                         fn, line);
1442                 return -1;
1443         }
1444
1445         if (dict_addvalue(argv[1], argv[0], value) < 0) {
1446                 char buffer[256];
1447
1448                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1449
1450                 fr_strerror_printf("dict_init: %s[%d]: %s",
1451                                    fn, line, buffer);
1452                 return -1;
1453         }
1454
1455         return 0;
1456 }
1457
1458
1459 /*
1460  *      Process the VALUE-ALIAS command
1461  *
1462  *      This allows VALUE mappings to be shared among multiple
1463  *      attributes.
1464  */
1465 static int process_value_alias(const char* fn, const int line, char **argv,
1466                                int argc)
1467 {
1468         DICT_ATTR *my_da, *da;
1469         DICT_VALUE *dval;
1470
1471         if (argc != 2) {
1472                 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1473                         fn, line);
1474                 return -1;
1475         }
1476
1477         my_da = dict_attrbyname(argv[0]);
1478         if (!my_da) {
1479                 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1480                            fn, line, argv[1]);
1481                 return -1;
1482         }
1483
1484         if (my_da->flags.has_value) {
1485                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE",
1486                            fn, line, argv[0]);
1487                 return -1;
1488         }
1489
1490         if (my_da->flags.has_value_alias) {
1491                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1492                            fn, line, argv[0]);
1493                 return -1;
1494         }
1495
1496         da = dict_attrbyname(argv[1]);
1497         if (!da) {
1498                 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1499                            fn, line, argv[1]);
1500                 return -1;
1501         }
1502
1503         if (!da->flags.has_value) {
1504                 fr_strerror_printf("dict_init: %s[%d]: VALUE-ALIAS cannot refer to ATTRIBUTE %s: It has no values",
1505                            fn, line, argv[1]);
1506                 return -1;
1507         }
1508
1509         if (da->flags.has_value_alias) {
1510                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1511                            fn, line, argv[1]);
1512                 return -1;
1513         }
1514
1515         if (my_da->type != da->type) {
1516                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1517                            fn, line);
1518                 return -1;
1519         }
1520
1521         if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1522                 fr_strerror_printf("dict_addvalue: out of memory");
1523                 return -1;
1524         }
1525
1526         dval->name[0] = '\0';   /* empty name */
1527         dval->attr = my_da->attr;
1528         dval->vendor = my_da->vendor;
1529         dval->value = da->attr;
1530
1531         if (!fr_hash_table_insert(values_byname, dval)) {
1532                 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1533                            fn, line);
1534                 fr_pool_free(dval);
1535                 return -1;
1536         }
1537
1538         return 0;
1539 }
1540
1541
1542 /*
1543  *      Process the VENDOR command
1544  */
1545 static int process_vendor(const char* fn, const int line, char **argv,
1546                           int argc)
1547 {
1548         int     value;
1549         int     continuation = 0;
1550         const   char *format = NULL;
1551
1552         if ((argc < 2) || (argc > 3)) {
1553                 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1554                             fn, line);
1555                 return -1;
1556         }
1557
1558         /*
1559          *       Validate all entries
1560          */
1561         if (!isdigit((int) argv[1][0])) {
1562                 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1563                         fn, line);
1564                 return -1;
1565         }
1566         value = atoi(argv[1]);
1567
1568         /* Create a new VENDOR entry for the list */
1569         if (dict_addvendor(argv[0], value) < 0) {
1570                 char buffer[256];
1571
1572                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1573
1574                 fr_strerror_printf("dict_init: %s[%d]: %s",
1575                            fn, line, buffer);
1576                 return -1;
1577         }
1578
1579         /*
1580          *      Look for a format statement
1581          */
1582         if (argc == 3) {
1583                 format = argv[2];
1584
1585         } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1586                 format = "format=4,0";
1587
1588         } else if (value == VENDORPEC_LUCENT) {
1589                 format = "format=2,1";
1590
1591         } else if (value == VENDORPEC_STARENT) {
1592                 format = "format=2,2";
1593
1594         } /* else no fixups to do */
1595
1596         if (format) {
1597                 int type, length;
1598                 const char *p;
1599                 DICT_VENDOR *dv;
1600
1601                 if (strncasecmp(format, "format=", 7) != 0) {
1602                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected \"format=\", got \"%s\"",
1603                                    fn, line, format);
1604                         return -1;
1605                 }
1606
1607                 p = format + 7;
1608                 if ((strlen(p) < 3) ||
1609                     !isdigit((int) p[0]) ||
1610                     (p[1] != ',') ||
1611                     !isdigit((int) p[2]) ||
1612                     (p[3] && (p[3] != ','))) {
1613                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
1614                                    fn, line, p);
1615                         return -1;
1616                 }
1617
1618                 type = (int) (p[0] - '0');
1619                 length = (int) (p[2] - '0');
1620
1621                 if (p[3] == ',') {
1622                         if ((p[4] != 'c') ||
1623                             (p[5] != '\0')) {
1624                                 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
1625                                            fn, line, p);
1626                                 return -1;
1627                         }
1628                         continuation = 1;
1629
1630                         if ((value != VENDORPEC_WIMAX) ||
1631                             (type != 1) || (length != 1)) {
1632                                 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1633                                            fn, line);
1634                                 return -1;
1635                         }
1636                 }
1637
1638                 dv = dict_vendorbyvalue(value);
1639                 if (!dv) {
1640                         fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1641                                    fn, line);
1642                         return -1;
1643                 }
1644
1645                 if ((type != 1) && (type != 2) && (type != 4)) {
1646                         fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1647                                    fn, line, type);
1648                         return -1;
1649                 }
1650
1651                 if ((length != 0) && (length != 1) && (length != 2)) {
1652                         fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1653                                    fn, line, length);
1654                         return -1;
1655                 }
1656
1657                 dv->type = type;
1658                 dv->length = length;
1659                 dv->flags = continuation;
1660         }
1661
1662         return 0;
1663 }
1664
1665 /*
1666  *      String split routine.  Splits an input string IN PLACE
1667  *      into pieces, based on spaces.
1668  */
1669 static int str2argv(char *str, char **argv, int max_argc)
1670 {
1671         int argc = 0;
1672
1673         while (*str) {
1674                 if (argc >= max_argc) break;
1675
1676                 /*
1677                  *      Chop out comments early.
1678                  */
1679                 if (*str == '#') {
1680                         *str = '\0';
1681                         break;
1682                 }
1683
1684                 while ((*str == ' ') ||
1685                        (*str == '\t') ||
1686                        (*str == '\r') ||
1687                        (*str == '\n')) *(str++) = '\0';
1688
1689                 if (!*str) break;
1690
1691                 argv[argc] = str;
1692                 argc++;
1693
1694                 while (*str &&
1695                        (*str != ' ') &&
1696                        (*str != '\t') &&
1697                        (*str != '\r') &&
1698                        (*str != '\n')) str++;
1699         }
1700
1701         return argc;
1702 }
1703
1704 #define MAX_ARGV (16)
1705
1706 /*
1707  *      Initialize the dictionary.
1708  */
1709 static int my_dict_init(const char *dir, const char *fn,
1710                         const char *src_file, int src_line)
1711 {
1712         FILE    *fp;
1713         char    dirtmp[256];
1714         char    buf[256];
1715         char    *p;
1716         int     line = 0;
1717         unsigned int    vendor;
1718         unsigned int    block_vendor;
1719         struct stat statbuf;
1720         char    *argv[MAX_ARGV];
1721         int     argc;
1722         DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1723         int     which_block_tlv = 0;
1724
1725         block_tlv[0] = NULL;
1726         block_tlv[1] = NULL;
1727         block_tlv[2] = NULL;
1728
1729         if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1730             strlen(dir) >= sizeof(dirtmp) / 2) {
1731                 fr_strerror_printf("dict_init: filename name too long");
1732                 return -1;
1733         }
1734
1735         /*
1736          *      First see if fn is relative to dir. If so, create
1737          *      new filename. If not, remember the absolute dir.
1738          */
1739         if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1740                 strcpy(dirtmp, fn);
1741                 dirtmp[p - fn] = 0;
1742                 dir = dirtmp;
1743         } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1744                 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1745                 fn = dirtmp;
1746         }
1747
1748         if ((fp = fopen(fn, "r")) == NULL) {
1749                 if (!src_file) {
1750                         fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1751                                    fn, strerror(errno));
1752                 } else {
1753                         fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1754                                    src_file, src_line, fn, strerror(errno));
1755                 }
1756                 return -1;
1757         }
1758
1759         stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1760         if (!S_ISREG(statbuf.st_mode)) {
1761                 fclose(fp);
1762                 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1763                            fn);
1764                 return -1;
1765         }
1766
1767         /*
1768          *      Globally writable dictionaries means that users can control
1769          *      the server configuration with little difficulty.
1770          */
1771 #ifdef S_IWOTH
1772         if ((statbuf.st_mode & S_IWOTH) != 0) {
1773                 fclose(fp);
1774                 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable.  Refusing to start due to insecure configuration.",
1775                            fn);
1776                 return -1;
1777         }
1778 #endif
1779
1780         dict_stat_add(fn, &statbuf);
1781
1782         /*
1783          *      Seed the random pool with data.
1784          */
1785         fr_rand_seed(&statbuf, sizeof(statbuf));
1786
1787         block_vendor = 0;
1788
1789         while (fgets(buf, sizeof(buf), fp) != NULL) {
1790                 line++;
1791                 if (buf[0] == '#' || buf[0] == 0 ||
1792                     buf[0] == '\n' || buf[0] == '\r')
1793                         continue;
1794
1795                 /*
1796                  *  Comment characters should NOT be appearing anywhere but
1797                  *  as start of a comment;
1798                  */
1799                 p = strchr(buf, '#');
1800                 if (p) *p = '\0';
1801
1802                 argc = str2argv(buf, argv, MAX_ARGV);
1803                 if (argc == 0) continue;
1804
1805                 if (argc == 1) {
1806                         fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1807                                     fn, line);
1808                         fclose(fp);
1809                         return -1;
1810                 }
1811
1812                 /*
1813                  *      Process VALUE lines.
1814                  */
1815                 if (strcasecmp(argv[0], "VALUE") == 0) {
1816                         if (process_value(fn, line,
1817                                           argv + 1, argc - 1) == -1) {
1818                                 fclose(fp);
1819                                 return -1;
1820                         }
1821                         continue;
1822                 }
1823
1824                 /*
1825                  *      Perhaps this is an attribute.
1826                  */
1827                 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
1828                         if (process_attribute(fn, line, block_vendor,
1829                                               block_tlv[which_block_tlv],
1830                                               which_block_tlv,
1831                                               argv + 1, argc - 1) == -1) {
1832                                 fclose(fp);
1833                                 return -1;
1834                         }
1835                         continue;
1836                 }
1837
1838                 /*
1839                  *      See if we need to import another dictionary.
1840                  */
1841                 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
1842                         if (my_dict_init(dir, argv[1], fn, line) < 0) {
1843                                 fclose(fp);
1844                                 return -1;
1845                         }
1846                         continue;
1847                 } /* $INCLUDE */
1848
1849                 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
1850                         if (process_value_alias(fn, line,
1851                                                 argv + 1, argc - 1) == -1) {
1852                                 fclose(fp);
1853                                 return -1;
1854                         }
1855                         continue;
1856                 }
1857
1858                 /*
1859                  *      Process VENDOR lines.
1860                  */
1861                 if (strcasecmp(argv[0], "VENDOR") == 0) {
1862                         if (process_vendor(fn, line,
1863                                            argv + 1, argc - 1) == -1) {
1864                                 fclose(fp);
1865                                 return -1;
1866                         }
1867                         continue;
1868                 }
1869
1870                 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
1871                         if (argc != 2) {
1872                                 fr_strerror_printf(
1873                                 "dict_init: %s[%d] invalid BEGIN-TLV entry",
1874                                         fn, line);
1875                                 fclose(fp);
1876                                 return -1;
1877                         }
1878
1879                         da = dict_attrbyname(argv[1]);
1880                         if (!da) {
1881                                 fr_strerror_printf(
1882                                         "dict_init: %s[%d]: unknown attribute %s",
1883                                         fn, line, argv[1]);
1884                                 fclose(fp);
1885                                 return -1;
1886                         }
1887
1888                         if (da->type != PW_TYPE_TLV) {
1889                                 fr_strerror_printf(
1890                                         "dict_init: %s[%d]: attribute %s is not of type tlv",
1891                                         fn, line, argv[1]);
1892                                 fclose(fp);
1893                                 return -1;
1894                         }
1895
1896                         if (which_block_tlv >= MAX_TLV_NEST) {
1897                                 fr_strerror_printf(
1898                                         "dict_init: %s[%d]: TLVs are nested too deep",
1899                                         fn, line);
1900                                 fclose(fp);
1901                                 return -1;
1902                         }
1903
1904
1905                         block_tlv[++which_block_tlv] = da;
1906                         continue;
1907                 } /* BEGIN-TLV */
1908
1909                 if (strcasecmp(argv[0], "END-TLV") == 0) {
1910                         if (argc != 2) {
1911                                 fr_strerror_printf(
1912                                 "dict_init: %s[%d] invalid END-TLV entry",
1913                                         fn, line);
1914                                 fclose(fp);
1915                                 return -1;
1916                         }
1917
1918                         da = dict_attrbyname(argv[1]);
1919                         if (!da) {
1920                                 fr_strerror_printf(
1921                                         "dict_init: %s[%d]: unknown attribute %s",
1922                                         fn, line, argv[1]);
1923                                 fclose(fp);
1924                                 return -1;
1925                         }
1926
1927                         if (da != block_tlv[which_block_tlv]) {
1928                                 fr_strerror_printf(
1929                                         "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
1930                                         fn, line, argv[1]);
1931                                 fclose(fp);
1932                                 return -1;
1933                         }
1934                         block_tlv[which_block_tlv--] = NULL;
1935                         continue;
1936                 } /* END-VENDOR */
1937
1938                 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
1939                         if (argc < 2) {
1940                                 fr_strerror_printf(
1941                                 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
1942                                         fn, line);
1943                                 fclose(fp);
1944                                 return -1;
1945                         }
1946
1947                         vendor = dict_vendorbyname(argv[1]);
1948                         if (!vendor) {
1949                                 fr_strerror_printf(
1950                                         "dict_init: %s[%d]: unknown vendor %s",
1951                                         fn, line, argv[1]);
1952                                 fclose(fp);
1953                                 return -1;
1954                         }
1955
1956                         block_vendor = vendor;
1957
1958                         /*
1959                          *      Check for extended attr VSAs
1960                          */
1961                         if (argc > 2) {
1962                                 if (strncmp(argv[2], "format=", 7) != 0) {
1963                                         fr_strerror_printf(
1964                                                 "dict_init: %s[%d]: Invalid format %s",
1965                                                 fn, line, argv[2]);
1966                                         fclose(fp);
1967                                         return -1;
1968                                 }
1969                                 
1970                                 p = argv[2] + 7;
1971                                 da = dict_attrbyname(p);
1972                                 if (!da) {
1973                                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
1974                                                            fn, line, p);
1975                                         fclose(fp);
1976                                         return -1;
1977                                 }
1978
1979                                 if (!da->flags.evs) {
1980                                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR.  Attribute \"%s\" is not of \"evs\" data type",
1981                                                            fn, line, p);
1982                                         fclose(fp);
1983                                         return -1;
1984                                 }
1985                                 
1986                                 /*
1987                                  *      Pack the encapsulating attribute
1988                                  *      into the vendor Id.  This attribute
1989                                  *      MUST be >= 241.
1990                                  */
1991                                 block_vendor |= (da->attr & fr_attr_mask[1]) * FR_MAX_VENDOR;
1992                         }
1993
1994                         continue;
1995                 } /* BEGIN-VENDOR */
1996
1997                 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
1998                         if (argc != 2) {
1999                                 fr_strerror_printf(
2000                                 "dict_init: %s[%d] invalid END-VENDOR entry",
2001                                         fn, line);
2002                                 fclose(fp);
2003                                 return -1;
2004                         }
2005
2006                         vendor = dict_vendorbyname(argv[1]);
2007                         if (!vendor) {
2008                                 fr_strerror_printf(
2009                                         "dict_init: %s[%d]: unknown vendor %s",
2010                                         fn, line, argv[1]);
2011                                 fclose(fp);
2012                                 return -1;
2013                         }
2014
2015                         if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2016                                 fr_strerror_printf(
2017                                         "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2018                                         fn, line, argv[1]);
2019                                 fclose(fp);
2020                                 return -1;
2021                         }
2022                         block_vendor = 0;
2023                         continue;
2024                 } /* END-VENDOR */
2025
2026                 /*
2027                  *      Any other string: We don't recognize it.
2028                  */
2029                 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2030                            fn, line, argv[0]);
2031                 fclose(fp);
2032                 return -1;
2033         }
2034         fclose(fp);
2035         return 0;
2036 }
2037
2038
2039 /*
2040  *      Empty callback for hash table initialization.
2041  */
2042 static int null_callback(void *ctx, void *data)
2043 {
2044         ctx = ctx;              /* -Wunused */
2045         data = data;            /* -Wunused */
2046
2047         return 0;
2048 }
2049
2050
2051 /*
2052  *      Initialize the directory, then fix the attr member of
2053  *      all attributes.
2054  */
2055 int dict_init(const char *dir, const char *fn)
2056 {
2057         /*
2058          *      Check if we need to change anything.  If not, don't do
2059          *      anything.
2060          */
2061         if (dict_stat_check(dir, fn)) {
2062                 return 0;
2063         }
2064
2065         /*
2066          *      Free the dictionaries, and the stat cache.
2067          */
2068         dict_free();
2069         stat_root_dir = strdup(dir);
2070         stat_root_file = strdup(fn);
2071
2072         /*
2073          *      Create the table of vendor by name.   There MAY NOT
2074          *      be multiple vendors of the same name.
2075          *
2076          *      Each vendor is malloc'd, so the free function is free.
2077          */
2078         vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2079                                                 dict_vendor_name_cmp,
2080                                                 fr_pool_free);
2081         if (!vendors_byname) {
2082                 return -1;
2083         }
2084
2085         /*
2086          *      Create the table of vendors by value.  There MAY
2087          *      be vendors of the same value.  If there are, we
2088          *      pick the latest one.
2089          */
2090         vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2091                                                  dict_vendor_value_cmp,
2092                                                  fr_pool_free);
2093         if (!vendors_byvalue) {
2094                 return -1;
2095         }
2096
2097         /*
2098          *      Create the table of attributes by name.   There MAY NOT
2099          *      be multiple attributes of the same name.
2100          *
2101          *      Each attribute is malloc'd, so the free function is free.
2102          */
2103         attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2104                                                    dict_attr_name_cmp,
2105                                                    fr_pool_free);
2106         if (!attributes_byname) {
2107                 return -1;
2108         }
2109
2110         /*
2111          *      Create the table of attributes by value.  There MAY
2112          *      be attributes of the same value.  If there are, we
2113          *      pick the latest one.
2114          */
2115         attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2116                                                     dict_attr_value_cmp,
2117                                                     fr_pool_free);
2118         if (!attributes_byvalue) {
2119                 return -1;
2120         }
2121
2122         values_byname = fr_hash_table_create(dict_value_name_hash,
2123                                                dict_value_name_cmp,
2124                                                fr_pool_free);
2125         if (!values_byname) {
2126                 return -1;
2127         }
2128
2129         values_byvalue = fr_hash_table_create(dict_value_value_hash,
2130                                                 dict_value_value_cmp,
2131                                                 fr_pool_free);
2132         if (!values_byvalue) {
2133                 return -1;
2134         }
2135
2136         value_fixup = NULL;     /* just to be safe. */
2137
2138         if (my_dict_init(dir, fn, NULL, 0) < 0)
2139                 return -1;
2140
2141         if (value_fixup) {
2142                 DICT_ATTR *a;
2143                 value_fixup_t *this, *next;
2144
2145                 for (this = value_fixup; this != NULL; this = next) {
2146                         next = this->next;
2147
2148                         a = dict_attrbyname(this->attrstr);
2149                         if (!a) {
2150                                 fr_strerror_printf(
2151                                         "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2152                                         this->attrstr, this->dval->name);
2153                                 return -1; /* leak, but they should die... */
2154                         }
2155
2156                         this->dval->attr = a->attr;
2157
2158                         /*
2159                          *      Add the value into the dictionary.
2160                          */
2161                         if (!fr_hash_table_replace(values_byname,
2162                                                      this->dval)) {
2163                                 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2164                                 return -1;
2165                         }
2166
2167                         /*
2168                          *      Allow them to use the old name, but
2169                          *      prefer the new name when printing
2170                          *      values.
2171                          */
2172                         if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2173                                 fr_hash_table_replace(values_byvalue,
2174                                                         this->dval);
2175                         }
2176                         free(this);
2177
2178                         /*
2179                          *      Just so we don't lose track of things.
2180                          */
2181                         value_fixup = next;
2182                 }
2183         }
2184
2185         /*
2186          *      Walk over all of the hash tables to ensure they're
2187          *      initialized.  We do this because the threads may perform
2188          *      lookups, and we don't want multi-threaded re-ordering
2189          *      of the table entries.  That would be bad.
2190          */
2191         fr_hash_table_walk(vendors_byname, null_callback, NULL);
2192         fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2193
2194         fr_hash_table_walk(attributes_byname, null_callback, NULL);
2195         fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2196
2197         fr_hash_table_walk(values_byvalue, null_callback, NULL);
2198         fr_hash_table_walk(values_byname, null_callback, NULL);
2199
2200         return 0;
2201 }
2202
2203 /*
2204  *      Get an attribute by its numerical value.
2205  */
2206 DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2207 {
2208         DICT_ATTR dattr;
2209
2210         if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2211
2212         dattr.attr = attr;
2213         dattr.vendor = vendor;
2214
2215         return fr_hash_table_finddata(attributes_byvalue, &dattr);
2216 }
2217
2218 /*
2219  *      Get an attribute by its name.
2220  */
2221 DICT_ATTR *dict_attrbyname(const char *name)
2222 {
2223         DICT_ATTR *da;
2224         uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2225
2226         if (!name) return NULL;
2227
2228         da = (DICT_ATTR *) buffer;
2229         strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2230
2231         return fr_hash_table_finddata(attributes_byname, da);
2232 }
2233
2234 /*
2235  *      Associate a value with an attribute and return it.
2236  */
2237 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2238 {
2239         DICT_VALUE dval, *dv;
2240
2241         /*
2242          *      First, look up aliases.
2243          */
2244         dval.attr = attr;
2245         dval.vendor = vendor;
2246         dval.name[0] = '\0';
2247
2248         /*
2249          *      Look up the attribute alias target, and use
2250          *      the correct attribute number if found.
2251          */
2252         dv = fr_hash_table_finddata(values_byname, &dval);
2253         if (dv) dval.attr = dv->value;
2254
2255         dval.value = value;
2256
2257         return fr_hash_table_finddata(values_byvalue, &dval);
2258 }
2259
2260 /*
2261  *      Get a value by its name, keyed off of an attribute.
2262  */
2263 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2264 {
2265         DICT_VALUE *my_dv, *dv;
2266         uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2267
2268         if (!name) return NULL;
2269
2270         my_dv = (DICT_VALUE *) buffer;
2271         my_dv->attr = attr;
2272         my_dv->vendor = vendor;
2273         my_dv->name[0] = '\0';
2274
2275         /*
2276          *      Look up the attribute alias target, and use
2277          *      the correct attribute number if found.
2278          */
2279         dv = fr_hash_table_finddata(values_byname, my_dv);
2280         if (dv) my_dv->attr = dv->value;
2281
2282         strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2283
2284         return fr_hash_table_finddata(values_byname, my_dv);
2285 }
2286
2287 /*
2288  *      Get the vendor PEC based on the vendor name
2289  *
2290  *      This is efficient only for small numbers of vendors.
2291  */
2292 int dict_vendorbyname(const char *name)
2293 {
2294         DICT_VENDOR *dv;
2295         uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
2296
2297         if (!name) return 0;
2298
2299         dv = (DICT_VENDOR *) buffer;
2300         strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
2301
2302         dv = fr_hash_table_finddata(vendors_byname, dv);
2303         if (!dv) return 0;
2304
2305         return dv->vendorpec;
2306 }
2307
2308 /*
2309  *      Return the vendor struct based on the PEC.
2310  */
2311 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
2312 {
2313         DICT_VENDOR dv;
2314
2315         dv.vendorpec = vendorpec;
2316
2317         return fr_hash_table_finddata(vendors_byvalue, &dv);
2318 }