Update vp->da in pairalloc(), and update dictionary functions
[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 #ifdef WITH_DHCP
29 #include        <freeradius-devel/dhcp.h>
30 #endif
31
32 #include        <ctype.h>
33
34 #ifdef HAVE_MALLOC_H
35 #include        <malloc.h>
36 #endif
37
38 #ifdef HAVE_SYS_STAT_H
39 #include        <sys/stat.h>
40 #endif
41
42
43 #define DICT_VALUE_MAX_NAME_LEN (128)
44 #define DICT_VENDOR_MAX_NAME_LEN (128)
45 #define DICT_ATTR_MAX_NAME_LEN (128)
46
47 #define DICT_ATTR_SIZE sizeof(DICT_ATTR) + DICT_ATTR_MAX_NAME_LEN
48
49 static fr_hash_table_t *vendors_byname = NULL;
50 static fr_hash_table_t *vendors_byvalue = NULL;
51
52 static fr_hash_table_t *attributes_byname = NULL;
53 static fr_hash_table_t *attributes_byvalue = NULL;
54
55 static fr_hash_table_t *attributes_combo = NULL;
56
57 static fr_hash_table_t *values_byvalue = NULL;
58 static fr_hash_table_t *values_byname = NULL;
59
60 static DICT_ATTR *dict_base_attrs[256];
61
62 /*
63  *      For faster HUP's, we cache the stat information for
64  *      files we've $INCLUDEd
65  */
66 typedef struct dict_stat_t {
67         struct dict_stat_t *next;
68         char               *name;
69         time_t             mtime;
70 } dict_stat_t;
71
72 static char *stat_root_dir = NULL;
73 static char *stat_root_file = NULL;
74
75 static dict_stat_t *stat_head = NULL;
76 static dict_stat_t *stat_tail = NULL;
77
78 typedef struct value_fixup_t {
79         char            attrstr[DICT_ATTR_MAX_NAME_LEN];
80         DICT_VALUE      *dval;
81         struct value_fixup_t *next;
82 } value_fixup_t;
83
84
85 /*
86  *      So VALUEs in the dictionary can have forward references.
87  */
88 static value_fixup_t *value_fixup = NULL;
89
90 const FR_NAME_NUMBER dict_attr_types[] = {
91         { "integer",    PW_TYPE_INTEGER },
92         { "string",     PW_TYPE_STRING },
93         { "ipaddr",     PW_TYPE_IPADDR },
94         { "date",       PW_TYPE_DATE },
95         { "abinary",    PW_TYPE_ABINARY },
96         { "octets",     PW_TYPE_OCTETS },
97         { "ifid",       PW_TYPE_IFID },
98         { "ipv6addr",   PW_TYPE_IPV6ADDR },
99         { "ipv6prefix", PW_TYPE_IPV6PREFIX },
100         { "byte",       PW_TYPE_BYTE },
101         { "short",      PW_TYPE_SHORT },
102         { "ether",      PW_TYPE_ETHERNET },
103         { "combo-ip",   PW_TYPE_COMBO_IP },
104         { "tlv",        PW_TYPE_TLV },
105         { "signed",     PW_TYPE_SIGNED },
106         { "extended",   PW_TYPE_EXTENDED },
107         { "long-extended",      PW_TYPE_LONG_EXTENDED },
108         { "evs",        PW_TYPE_EVS },
109         { "uint8",      PW_TYPE_BYTE },
110         { "uint16",     PW_TYPE_SHORT },
111         { "uint32",     PW_TYPE_INTEGER },
112         { "int32",      PW_TYPE_SIGNED },
113         { "integer64",  PW_TYPE_INTEGER64 },
114         { "uint64",     PW_TYPE_INTEGER64 },
115         { "ipv4prefix", PW_TYPE_IPV4PREFIX },
116         { "vsa",        PW_TYPE_VSA },
117         { NULL, 0 }
118 };
119
120
121 /*
122  *      For packing multiple TLV numbers into one 32-bit integer.  The
123  *      first 3 bytes are just the 8-bit number.  The next two are
124  *      more limited.  We only allow 31 attributes nested 3 layers
125  *      deep, and only 7 nested 4 layers deep.  This should be
126  *      sufficient for most purposes.
127  *
128  *      For TLVs and extended attributes, we packet the base attribute
129  *      number into the upper 8 bits of the "vendor" field.
130  *
131  *      e.g.    OID             attribute       vendor
132  *              241.1           1               (241 << 8)
133  *              241.26.9.1      1               (241 << 8) | (9)
134  *              241.1.2         1 | (2 << 8)    (241 << 8)
135  */
136 #define MAX_TLV_NEST (4)
137 /*
138  *      Bit packing:
139  *      8 bits of base attribute
140  *      8 bits for nested TLV 1
141  *      8 bits for nested TLV 2
142  *      5 bits for nested TLV 3
143  *      3 bits for nested TLV 4
144  */
145 const int fr_attr_max_tlv = MAX_TLV_NEST;
146 const int fr_attr_shift[MAX_TLV_NEST + 1] = {
147   0, 8, 16, 24, 29
148 };
149
150 const int fr_attr_mask[MAX_TLV_NEST + 1] = {
151   0xff, 0xff, 0xff, 0x1f, 0x07
152 };
153
154
155 /*
156  *      Create the hash of the name.
157  *
158  *      We copy the hash function here because it's substantially faster.
159  */
160 #define FNV_MAGIC_INIT (0x811c9dc5)
161 #define FNV_MAGIC_PRIME (0x01000193)
162
163 static uint32_t dict_hashname(const char *name)
164 {
165         uint32_t hash = FNV_MAGIC_INIT;
166         const char *p;
167
168         for (p = name; *p != '\0'; p++) {
169                 int c = *(const unsigned char *) p;
170                 if (isalpha(c)) c = tolower(c);
171
172                 hash *= FNV_MAGIC_PRIME;
173                 hash ^= (uint32_t ) (c & 0xff);
174         }
175
176         return hash;
177 }
178
179
180 /*
181  *      Hash callback functions.
182  */
183 static uint32_t dict_attr_name_hash(const void *data)
184 {
185         return dict_hashname(((const DICT_ATTR *)data)->name);
186 }
187
188 static int dict_attr_name_cmp(const void *one, const void *two)
189 {
190         const DICT_ATTR *a = one;
191         const DICT_ATTR *b = two;
192
193         return strcasecmp(a->name, b->name);
194 }
195
196 static uint32_t dict_attr_value_hash(const void *data)
197 {
198         uint32_t hash;
199         const DICT_ATTR *attr = data;
200
201         hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
202         return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
203 }
204
205 static int dict_attr_value_cmp(const void *one, const void *two)
206 {
207         const DICT_ATTR *a = one;
208         const DICT_ATTR *b = two;
209
210         if (a->vendor < b->vendor) return -1;
211         if (a->vendor > b->vendor) return +1;
212
213         return a->attr - b->attr;
214 }
215
216 static uint32_t dict_attr_combo_hash(const void *data)
217 {
218         uint32_t hash;
219         const DICT_ATTR *attr = data;
220
221         hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
222         hash = fr_hash_update(&attr->type, sizeof(attr->type), hash);
223         return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
224 }
225
226 static int dict_attr_combo_cmp(const void *one, const void *two)
227 {
228         const DICT_ATTR *a = one;
229         const DICT_ATTR *b = two;
230
231         if (a->type < b->type) return -1;
232         if (a->type > b->type) return +1;
233
234         if (a->vendor < b->vendor) return -1;
235         if (a->vendor > b->vendor) return +1;
236
237         return a->attr - b->attr;
238 }
239
240 static uint32_t dict_vendor_name_hash(const void *data)
241 {
242         return dict_hashname(((const DICT_VENDOR *)data)->name);
243 }
244
245 static int dict_vendor_name_cmp(const void *one, const void *two)
246 {
247         const DICT_VENDOR *a = one;
248         const DICT_VENDOR *b = two;
249
250         return strcasecmp(a->name, b->name);
251 }
252
253 static uint32_t dict_vendor_value_hash(const void *data)
254 {
255         return fr_hash(&(((const DICT_VENDOR *)data)->vendorpec),
256                          sizeof(((const DICT_VENDOR *)data)->vendorpec));
257 }
258
259 static int dict_vendor_value_cmp(const void *one, const void *two)
260 {
261         const DICT_VENDOR *a = one;
262         const DICT_VENDOR *b = two;
263
264         return a->vendorpec - b->vendorpec;
265 }
266
267 static uint32_t dict_value_name_hash(const void *data)
268 {
269         uint32_t hash;
270         const DICT_VALUE *dval = data;
271
272         hash = dict_hashname(dval->name);
273         hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
274         return fr_hash_update(&dval->attr, sizeof(dval->attr), hash);
275 }
276
277 static int dict_value_name_cmp(const void *one, const void *two)
278 {
279         int rcode;
280         const DICT_VALUE *a = one;
281         const DICT_VALUE *b = two;
282
283         rcode = a->attr - b->attr;
284         if (rcode != 0) return rcode;
285
286         rcode = a->vendor - b->vendor;
287         if (rcode != 0) return rcode;
288
289         return strcasecmp(a->name, b->name);
290 }
291
292 static uint32_t dict_value_value_hash(const void *data)
293 {
294         uint32_t hash;
295         const DICT_VALUE *dval = data;
296
297         hash = fr_hash(&dval->attr, sizeof(dval->attr));
298         hash = fr_hash_update(&dval->vendor, sizeof(dval->vendor), hash);
299         return fr_hash_update(&dval->value, sizeof(dval->value), hash);
300 }
301
302 static int dict_value_value_cmp(const void *one, const void *two)
303 {
304         int rcode;
305         const DICT_VALUE *a = one;
306         const DICT_VALUE *b = two;
307
308         if (a->vendor < b->vendor) return -1;
309         if (a->vendor > b->vendor) return +1;
310
311         rcode = a->attr - b->attr;
312         if (rcode != 0) return rcode;
313
314         return a->value - b->value;
315 }
316
317
318 /*
319  *      Free the list of stat buffers
320  */
321 static void dict_stat_free(void)
322 {
323         dict_stat_t *this, *next;
324
325         free(stat_root_dir);
326         stat_root_dir = NULL;
327         free(stat_root_file);
328         stat_root_file = NULL;
329
330         if (!stat_head) {
331                 stat_tail = NULL;
332                 return;
333         }
334
335         for (this = stat_head; this != NULL; this = next) {
336                 next = this->next;
337                 free(this->name);
338                 free(this);
339         }
340
341         stat_head = stat_tail = NULL;
342 }
343
344
345 /*
346  *      Add an entry to the list of stat buffers.
347  */
348 static void dict_stat_add(const char *name, const struct stat *stat_buf)
349 {
350         dict_stat_t *this;
351
352         this = malloc(sizeof(*this));
353         if (!this) return;
354         memset(this, 0, sizeof(*this));
355
356         this->name = strdup(name);
357         this->mtime = stat_buf->st_mtime;
358
359         if (!stat_head) {
360                 stat_head = stat_tail = this;
361         } else {
362                 stat_tail->next = this;
363                 stat_tail = this;
364         }
365 }
366
367
368 /*
369  *      See if any dictionaries have changed.  If not, don't
370  *      do anything.
371  */
372 static int dict_stat_check(const char *root_dir, const char *root_file)
373 {
374         struct stat buf;
375         dict_stat_t *this;
376
377         if (!stat_root_dir) return 0;
378         if (!stat_root_file) return 0;
379
380         if (strcmp(root_dir, stat_root_dir) != 0) return 0;
381         if (strcmp(root_file, stat_root_file) != 0) return 0;
382
383         if (!stat_head) return 0; /* changed, reload */
384
385         for (this = stat_head; this != NULL; this = this->next) {
386                 if (stat(this->name, &buf) < 0) return 0;
387
388                 if (buf.st_mtime != this->mtime) return 0;
389         }
390
391         return 1;
392 }
393
394 typedef struct fr_pool_t {
395         void    *page_end;
396         void    *free_ptr;
397         struct fr_pool_t *page_free;
398         struct fr_pool_t *page_next;
399 } fr_pool_t;
400
401 #define FR_POOL_SIZE (32768)
402 #define FR_ALLOC_ALIGN (8)
403
404 static fr_pool_t *dict_pool = NULL;
405
406 static fr_pool_t *fr_pool_create(void)
407 {
408         fr_pool_t *fp = malloc(FR_POOL_SIZE);
409
410         if (!fp) return NULL;
411
412         memset(fp, 0, FR_POOL_SIZE);
413
414         fp->page_end = ((uint8_t *) fp) + FR_POOL_SIZE;
415         fp->free_ptr = ((uint8_t *) fp) + sizeof(*fp);
416         fp->page_free = fp;
417         fp->page_next = NULL;
418         return fp;
419 }
420
421 static void fr_pool_delete(fr_pool_t **pfp)
422 {
423         fr_pool_t *fp, *next;
424
425         if (!pfp || !*pfp) return;
426
427         for (fp = *pfp; fp != NULL; fp = next) {
428                 next = fp->page_next;
429                 fp->page_next = NULL;
430                 free(fp);
431         }
432         *pfp = NULL;
433 }
434
435
436 static void *fr_pool_alloc(size_t size)
437 {
438         void *ptr;
439
440         if (size == 0) return NULL;
441
442         if (size > 256) return NULL; /* shouldn't happen */
443
444         if (!dict_pool) {
445                 dict_pool = fr_pool_create();
446                 if (!dict_pool) return NULL;
447         }
448
449         if ((size & (FR_ALLOC_ALIGN - 1)) != 0) {
450                 size += FR_ALLOC_ALIGN - (size & (FR_ALLOC_ALIGN - 1));
451         }
452
453         if ((((uint8_t *) dict_pool->page_free->free_ptr) + size) > (uint8_t *) dict_pool->page_free->page_end) {
454                 dict_pool->page_free->page_next = fr_pool_create();
455                 if (!dict_pool->page_free->page_next) return NULL;
456                 dict_pool->page_free = dict_pool->page_free->page_next;
457         }
458
459         ptr = dict_pool->page_free->free_ptr;
460         dict_pool->page_free->free_ptr = ((uint8_t *) dict_pool->page_free->free_ptr) + size;
461
462         return ptr;
463 }
464
465
466 static void fr_pool_free(UNUSED void *ptr)
467 {
468         /*
469          *      Place-holder for later code.
470          */
471 }
472
473 /*
474  *      Free the dictionary_attributes and dictionary_values lists.
475  */
476 void dict_free(void)
477 {
478         /*
479          *      Free the tables
480          */
481         fr_hash_table_free(vendors_byname);
482         fr_hash_table_free(vendors_byvalue);
483         vendors_byname = NULL;
484         vendors_byvalue = NULL;
485
486         fr_hash_table_free(attributes_byname);
487         fr_hash_table_free(attributes_byvalue);
488         fr_hash_table_free(attributes_combo);
489         attributes_byname = NULL;
490         attributes_byvalue = NULL;
491         attributes_combo = NULL;
492
493         fr_hash_table_free(values_byname);
494         fr_hash_table_free(values_byvalue);
495         values_byname = NULL;
496         values_byvalue = NULL;
497
498         memset(dict_base_attrs, 0, sizeof(dict_base_attrs));
499
500         fr_pool_delete(&dict_pool);
501
502         dict_stat_free();
503 }
504
505 /*
506  *      Add vendor to the list.
507  */
508 int dict_addvendor(const char *name, unsigned int value)
509 {
510         size_t length;
511         DICT_VENDOR *dv;
512
513         if (value >= FR_MAX_VENDOR) {
514                 fr_strerror_printf("dict_addvendor: Cannot handle vendor ID larger than 2^24");
515                 return -1;
516         }
517
518         if ((length = strlen(name)) >= DICT_VENDOR_MAX_NAME_LEN) {
519                 fr_strerror_printf("dict_addvendor: vendor name too long");
520                 return -1;
521         }
522
523         if ((dv = fr_pool_alloc(sizeof(*dv) + length)) == NULL) {
524                 fr_strerror_printf("dict_addvendor: out of memory");
525                 return -1;
526         }
527
528         strcpy(dv->name, name);
529         dv->vendorpec  = value;
530         dv->type = dv->length = 1; /* defaults */
531
532         if (!fr_hash_table_insert(vendors_byname, dv)) {
533                 DICT_VENDOR *old_dv;
534
535                 old_dv = fr_hash_table_finddata(vendors_byname, dv);
536                 if (!old_dv) {
537                         fr_strerror_printf("dict_addvendor: Failed inserting vendor name %s", name);
538                         return -1;
539                 }
540                 if (old_dv->vendorpec != dv->vendorpec) {
541                         fr_strerror_printf("dict_addvendor: Duplicate vendor name %s", name);
542                         return -1;
543                 }
544
545                 /*
546                  *      Already inserted.  Discard the duplicate entry.
547                  */
548                 fr_pool_free(dv);
549                 return 0;
550         }
551
552         /*
553          *      Insert the SAME pointer (not free'd when this table is
554          *      deleted), into another table.
555          *
556          *      We want this behaviour because we want OLD names for
557          *      the attributes to be read from the configuration
558          *      files, but when we're printing them, (and looking up
559          *      by value) we want to use the NEW name.
560          */
561         if (!fr_hash_table_replace(vendors_byvalue, dv)) {
562                 fr_strerror_printf("dict_addvendor: Failed inserting vendor %s",
563                            name);
564                 return -1;
565         }
566
567         return 0;
568 }
569
570 /*
571  *      Add an attribute to the dictionary.
572  */
573 int dict_addattr(const char *name, int attr, unsigned int vendor, int type,
574                  ATTR_FLAGS flags)
575 {
576         size_t namelen;
577         static int      max_attr = 0;
578         const char      *p;
579         const DICT_ATTR *da;
580         DICT_ATTR *n;
581         
582         namelen = strlen(name);
583         if (namelen >= DICT_ATTR_MAX_NAME_LEN) {
584                 fr_strerror_printf("dict_addattr: attribute name too long");
585                 return -1;
586         }
587
588         for (p = name; *p != '\0'; p++) {
589                 if (*p < ' ') {
590                         fr_strerror_printf("dict_addattr: attribute name cannot contain control characters");
591                         return -1;
592                 }
593
594                 if ((*p == '"') || (*p == '\\')) {
595                         fr_strerror_printf("dict_addattr: attribute name cannot contain quotation or backslash");
596                         return -1;
597                 }
598
599                 if ((*p == '<') || (*p == '>') || (*p == '&')) {
600                         fr_strerror_printf("dict_addattr: attribute name cannot contain XML control characters");
601                         return -1;
602                 }
603         }
604
605         if (flags.has_tag &&
606             !((type == PW_TYPE_INTEGER) || (type == PW_TYPE_STRING))) {
607                 fr_strerror_printf("dict_addattr: Only 'integer' and 'string' attributes can have tags");
608                 return -1;
609         }
610
611
612         /*
613          *      If the attr is '-1', that means use a pre-existing
614          *      one (if it already exists).  If one does NOT already exist,
615          *      then create a new attribute, with a non-conflicting value,
616          *      and use that.
617          */
618         if (attr == -1) {
619                 if (dict_attrbyname(name)) {
620                         return 0; /* exists, don't add it again */
621                 }
622
623                 attr = ++max_attr;
624
625         } else if (vendor == 0) {
626                 /*
627                  *  Update 'max_attr'
628                  */
629                 if (attr > max_attr) {
630                         max_attr = attr;
631                 }
632         }
633
634         /*
635          *      Additional checks for extended attributes.
636          */
637         if (flags.extended || flags.long_extended || flags.evs) {
638                 if (vendor && (vendor < FR_MAX_VENDOR)) {
639                         fr_strerror_printf("dict_addattr: VSAs cannot use the \"extended\" or \"evs\" attribute formats.");
640                         return -1;
641                 }
642                 if (flags.has_tag
643 #ifdef WITH_DHCP
644                     || flags.array
645 #endif
646                     || (flags.encrypt != FLAG_ENCRYPT_NONE)) {
647                         fr_strerror_printf("dict_addattr: The \"extended\" attributes MUST NOT have any flags set.");
648                         return -1;
649                 }
650         }
651
652         if (flags.evs) {
653                 if (!(flags.extended || flags.long_extended)) {
654                         fr_strerror_printf("dict_addattr: Attributes of type \"evs\" MUST have a parent of type \"extended\"");
655                         return -1;
656                 }
657
658                 /* VSAs cannot be of format EVS */
659                 if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
660                         fr_strerror_printf("dict_addattr: Attribute of type \"evs\" fails internal sanity check");
661                         return -1;
662                 }
663         }
664                 
665         if (attr < 0) {
666                 fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (less than zero)");
667                 return -1;
668         }
669
670         if (flags.has_tlv && flags.length) {
671                 fr_strerror_printf("TLVs cannot have a fixed length");
672                 return -1;
673         }
674
675         if ((vendor & (FR_MAX_VENDOR -1)) != 0) {
676                 DICT_VENDOR *dv;
677                 static DICT_VENDOR *last_vendor = NULL;
678
679                 if (flags.has_tlv && (flags.encrypt != FLAG_ENCRYPT_NONE)) {
680                         fr_strerror_printf("TLV's cannot be encrypted");
681                         return -1;
682                 }
683
684                 if (flags.is_tlv && flags.has_tag) {
685                         fr_strerror_printf("Sub-TLV's cannot have a tag");
686                         return -1;
687                 }
688
689                 if (flags.has_tlv && flags.has_tag) {
690                         fr_strerror_printf("TLV's cannot have a tag");
691                         return -1;
692                 }
693
694                 /*
695                  *      Most ATTRIBUTEs are bunched together by
696                  *      VENDOR.  We can save a lot of lookups on
697                  *      dictionary initialization by caching the last
698                  *      vendor.
699                  */
700                 if (last_vendor &&
701                     ((vendor & (FR_MAX_VENDOR - 1)) == last_vendor->vendorpec)) {
702                         dv = last_vendor;
703                 } else {
704                         /*
705                          *      Ignore the high byte (sigh)
706                          */
707                         dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
708                         last_vendor = dv;
709                 }
710
711                 /*
712                  *      If the vendor isn't defined, die.
713                  */
714                 if (!dv) {
715                         fr_strerror_printf("dict_addattr: Unknown vendor %u",
716                                            vendor & (FR_MAX_VENDOR - 1));
717                         return -1;
718                 }
719
720                 /*
721                  *      FIXME: Switch over dv->type, and limit things
722                  *      properly.
723                  */
724                 if ((dv->type == 1) && (attr >= 256) && !flags.is_tlv) {
725                         fr_strerror_printf("dict_addattr: ATTRIBUTE has invalid number (larger than 255).");
726                         return -1;
727                 } /* else 256..65535 are allowed */
728
729                 /*
730                  *      If the attribute is in the standard space, AND
731                  *      has a sub-type (e.g. 241.1 or 255.3), then its
732                  *      number is placed into the upper 8 bits of the
733                  *      vendor field.
734                  *
735                  *      This also happens for the new VSAs.
736                  *
737                  *      If we find it, then set the various flags
738                  *      based on what we see.
739                  */
740                 if (vendor >= FR_MAX_VENDOR) {
741                         unsigned int parent;
742
743                         parent = (vendor / FR_MAX_VENDOR) & 0xff;
744
745                         da = dict_attrbyvalue(parent, 0);
746                         if (!da) {
747                                 fr_strerror_printf("dict_addattr: ATTRIBUTE refers to unknown parent attribute %u.", parent);
748                                 return -1;
749                         }
750
751                         /*
752                          *      These flags are inhereited inherited
753                          *      from the parent.
754                          */
755                         flags.extended = da->flags.extended;
756                         flags.long_extended = da->flags.long_extended;
757
758                         /*
759                          *      Non-extended attributes can't have VSAs.
760                          */
761                         if (!flags.extended &&
762                             ((vendor & (FR_MAX_VENDOR - 1)) != 0)) {
763                                 fr_strerror_printf("dict_addattr: ATTRIBUTE cannot be a VSA");
764                                 return -1;
765                         }
766
767                         if ((vendor & (FR_MAX_VENDOR - 1)) != 0) {
768                                 flags.evs = 1;
769                         }
770                 }
771
772                 /*
773                  *      <sigh> Alvarion, being *again* a horribly
774                  *      broken vendor, has re-used the WiMAX format in
775                  *      their proprietary vendor space.  This re-use
776                  *      means that there are *multiple* conflicting
777                  *      Alvarion dictionaries.
778                  */
779                 flags.wimax = dv->flags;
780         }
781
782         /*
783          *      Create a new attribute for the list
784          */
785         if ((n = fr_pool_alloc(sizeof(*n) + namelen)) == NULL) {
786         oom:
787                 fr_strerror_printf("dict_adnttr: out of memory");
788                 return -1;
789         }
790
791         memcpy(n->name, name, namelen);
792         n->name[namelen] = '\0';
793         n->attr = attr;
794         n->vendor = vendor;
795         n->type = type;
796         n->flags = flags;
797
798         /*
799          *      Insert the attribute, only if it's not a duplicate.
800          */
801         if (!fr_hash_table_insert(attributes_byname, n)) {
802                 DICT_ATTR       *a;
803
804                 /*
805                  *      If the attribute has identical number, then
806                  *      ignore the duplicate.
807                  */
808                 a = fr_hash_table_finddata(attributes_byname, n);
809                 if (a && (strcasecmp(a->name, n->name) == 0)) {
810                         if (a->attr != n->attr) {
811                                 fr_strerror_printf("dict_adnttr: Duplicate attribute name %s", name);
812                                 fr_pool_free(n);
813                                 return -1;
814                         }
815
816                         /*
817                          *      Same name, same vendor, same attr,
818                          *      maybe the flags and/or type is
819                          *      different.  Let the new value
820                          *      over-ride the old one.
821                          */
822                 }
823
824
825                 fr_hash_table_delete(attributes_byvalue, a);
826
827                 if (!fr_hash_table_replace(attributes_byname, n)) {
828                         fr_strerror_printf("dict_adnttr: Internal error storing attribute %s", name);
829                         fr_pool_free(n);
830                         return -1;
831                 }
832         }
833
834         /*
835          *      Insert the SAME pointer (not free'd when this entry is
836          *      deleted), into another table.
837          *
838          *      We want this behaviour because we want OLD names for
839          *      the attributes to be read from the configuration
840          *      files, but when we're printing them, (and looking up
841          *      by value) we want to use the NEW name.
842          */
843         if (!fr_hash_table_replace(attributes_byvalue, n)) {
844                 fr_strerror_printf("dict_adnttr: Failed inserting attribute name %s", name);
845                 return -1;
846         }
847
848         /*
849          *      Hacks for combo-IP
850          */
851         if (n->type == PW_TYPE_COMBO_IP) {
852                 DICT_ATTR *v4, *v6;
853
854                 v4 = malloc(sizeof(*v4));
855                 if (!v4) goto oom;
856
857                 v6 = malloc(sizeof(*v6));
858                 if (!v6) {
859                         free(v4);
860                         goto oom;
861                 }
862
863                 memcpy(v4, n, sizeof(*v4));
864                 v4->type = PW_TYPE_IPADDR;
865
866                 memcpy(v6, n, sizeof(*v6));
867                 v6->type = PW_TYPE_IPV6ADDR;
868
869                 if (!fr_hash_table_insert(attributes_combo, v4)) {
870                         fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv4", name);
871                         free(v4);
872                         free(v6);
873                         return -1;
874                 }
875
876                 if (!fr_hash_table_insert(attributes_combo, v6)) {
877                         fr_strerror_printf("dict_addattr: Failed inserting attribute name %s - IPv6", name);
878                         free(v6);
879                         return -1;
880                 }
881         }
882
883         if (!vendor && (attr > 0) && (attr < 256)) {
884                  dict_base_attrs[attr] = n;
885         }
886
887         return 0;
888 }
889
890
891 /*
892  *      Add a value for an attribute to the dictionary.
893  */
894 int dict_addvalue(const char *namestr, const char *attrstr, int value)
895 {
896         size_t          length;
897         const DICT_ATTR *dattr;
898         DICT_VALUE      *dval;
899
900         static const DICT_ATTR *last_attr = NULL;
901
902         if (!*namestr) {
903                 fr_strerror_printf("dict_addvalue: empty names are not permitted");
904                 return -1;
905         }
906
907         if ((length = strlen(namestr)) >= DICT_VALUE_MAX_NAME_LEN) {
908                 fr_strerror_printf("dict_addvalue: value name too long");
909                 return -1;
910         }
911
912         if ((dval = fr_pool_alloc(sizeof(*dval) + length)) == NULL) {
913                 fr_strerror_printf("dict_addvalue: out of memory");
914                 return -1;
915         }
916         memset(dval, 0, sizeof(*dval));
917
918         strcpy(dval->name, namestr);
919         dval->value = value;
920
921         /*
922          *      Most VALUEs are bunched together by ATTRIBUTE.  We can
923          *      save a lot of lookups on dictionary initialization by
924          *      caching the last attribute.
925          */
926         if (last_attr && (strcasecmp(attrstr, last_attr->name) == 0)) {
927                 dattr = last_attr;
928         } else {
929                 dattr = dict_attrbyname(attrstr);
930                 last_attr = dattr;
931         }
932
933         /*
934          *      Remember which attribute is associated with this
935          *      value, if possible.
936          */
937         if (dattr) {
938                 if (dattr->flags.has_value_alias) {
939                         fr_strerror_printf("dict_addvalue: Cannot add VALUE for ATTRIBUTE \"%s\": It already has a VALUE-ALIAS", attrstr);
940                         return -1;
941                 }
942
943                 dval->attr = dattr->attr;
944                 dval->vendor = dattr->vendor;
945
946                 /*
947                  *      Enforce valid values
948                  *
949                  *      Don't worry about fixups...
950                  */
951                 switch (dattr->type) {
952                         case PW_TYPE_BYTE:
953                                 if (value > 255) {
954                                         fr_pool_free(dval);
955                                         fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'byte' cannot have VALUEs larger than 255");
956                                         return -1;
957                                 }
958                                 break;
959                         case PW_TYPE_SHORT:
960                                 if (value > 65535) {
961                                         fr_pool_free(dval);
962                                         fr_strerror_printf("dict_addvalue: ATTRIBUTEs of type 'short' cannot have VALUEs larger than 65535");
963                                         return -1;
964                                 }
965                                 break;
966
967                                 /*
968                                  *      Allow octets for now, because
969                                  *      of dictionary.cablelabs
970                                  */
971                         case PW_TYPE_OCTETS:
972
973                         case PW_TYPE_INTEGER:
974                                 break;
975
976                         case PW_TYPE_INTEGER64:
977                         default:
978                                 fr_pool_free(dval);
979                                 fr_strerror_printf("dict_addvalue: VALUEs cannot be defined for attributes of type '%s'",
980                                            fr_int2str(dict_attr_types, dattr->type, "?Unknown?"));
981                                 return -1;
982                 }
983         } else {
984                 value_fixup_t *fixup;
985
986                 fixup = (value_fixup_t *) malloc(sizeof(*fixup));
987                 if (!fixup) {
988                         fr_pool_free(dval);
989                         fr_strerror_printf("dict_addvalue: out of memory");
990                         return -1;
991                 }
992                 memset(fixup, 0, sizeof(*fixup));
993
994                 strlcpy(fixup->attrstr, attrstr, sizeof(fixup->attrstr));
995                 fixup->dval = dval;
996
997                 /*
998                  *      Insert to the head of the list.
999                  */
1000                 fixup->next = value_fixup;
1001                 value_fixup = fixup;
1002
1003                 return 0;
1004         }
1005
1006         /*
1007          *      Add the value into the dictionary.
1008          */
1009         {
1010                 DICT_ATTR *tmp;
1011                 memcpy(&tmp, &dval, sizeof(tmp));
1012                 
1013                 if (!fr_hash_table_insert(values_byname, tmp)) {
1014                         if (dattr) {
1015                                 DICT_VALUE *old;
1016
1017                                 /*
1018                                  *      Suppress duplicates with the same
1019                                  *      name and value.  There are lots in
1020                                  *      dictionary.ascend.
1021                                  */
1022                                 old = dict_valbyname(dattr->attr, dattr->vendor, namestr);
1023                                 if (old && (old->value == dval->value)) {
1024                                         fr_pool_free(dval);
1025                                         return 0;
1026                                 }
1027                         }
1028
1029                         fr_pool_free(dval);
1030                         fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", namestr, attrstr);
1031                         return -1;
1032                 }
1033         }
1034
1035         /*
1036          *      There are multiple VALUE's, keyed by attribute, so we
1037          *      take care of that here.
1038          */
1039         if (!fr_hash_table_replace(values_byvalue, dval)) {
1040                 fr_strerror_printf("dict_addvalue: Failed inserting value %s",
1041                            namestr);
1042                 return -1;
1043         }
1044
1045         return 0;
1046 }
1047
1048 static int sscanf_i(const char *str, unsigned int *pvalue)
1049 {
1050         int rcode = 0;
1051         int base = 10;
1052         static const char *tab = "0123456789";
1053
1054         if ((str[0] == '0') &&
1055             ((str[1] == 'x') || (str[1] == 'X'))) {
1056                 tab = "0123456789abcdef";
1057                 base = 16;
1058
1059                 str += 2;
1060         }
1061
1062         while (*str) {
1063                 const char *c;
1064
1065                 if (*str == '.') break;
1066
1067                 c = memchr(tab, tolower((int) *str), base);
1068                 if (!c) return 0;
1069
1070                 rcode *= base;
1071                 rcode += (c - tab);
1072                 str++;
1073         }
1074
1075         *pvalue = rcode;
1076         return 1;
1077 }
1078
1079
1080 /*
1081  *      Get the OID based on various pieces of information.
1082  *
1083  *      Remember, the packing format is weird.
1084  *
1085  *      00VID   000000AA        normal VSA for vendor VID
1086  *      00VID   AABBCCDD        normal VSAs with TLVs
1087  *      EE000   000000AA        extended attr (241.1)
1088  *      EE000   AABBCCDD        extended attr with TLVs
1089  *      EEVID   000000AA        EVS with vendor VID, attr AAA
1090  *      EEVID   AABBCCDD        EVS with TLVs
1091  *
1092  *      <whew>!  Are we crazy, or what?
1093  */
1094 int dict_str2oid(const char *ptr, unsigned int *pvalue, unsigned int *pvendor,
1095                  int tlv_depth)
1096 {
1097         const char *p;
1098         unsigned int value;
1099         const DICT_ATTR *da;
1100
1101         if (tlv_depth > fr_attr_max_tlv) {
1102                 fr_strerror_printf("Too many sub-attributes");
1103                 return -1;
1104         }
1105
1106         /*
1107          *      If *pvalue is set, check if the attribute exists.
1108          *      Otherwise, check that the vendor exists.
1109          */
1110         if (*pvalue) {
1111                 da = dict_attrbyvalue(*pvalue, *pvendor);
1112                 if (!da) {
1113                         fr_strerror_printf("Parent attribute is undefined.");
1114                         return -1;
1115                 }
1116                 
1117                 if (!da->flags.has_tlv && !da->flags.extended) {
1118                         fr_strerror_printf("Parent attribute %s cannot have sub-attributes",
1119                                            da->name);
1120                         return -1;
1121                 }
1122
1123         } else if ((*pvendor & (FR_MAX_VENDOR - 1)) != 0) {
1124                 if (!dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1))) {
1125                         fr_strerror_printf("Unknown vendor %u",
1126                                            *pvendor & (FR_MAX_VENDOR - 1));
1127                         return -1;
1128                 }
1129         }
1130
1131         p = strchr(ptr, '.');
1132
1133         /*
1134          *      Look for 26.VID.x.y
1135          *
1136          *      If we find it, re-write the parameters, and recurse.
1137          */
1138         if (!*pvendor && (tlv_depth == 0) && (*pvalue == PW_VENDOR_SPECIFIC)) {
1139                 const DICT_VENDOR *dv;
1140
1141                 if (!p) {
1142                         fr_strerror_printf("VSA needs to have sub-attribute");
1143                         return -1;
1144                 }
1145
1146                 if (!sscanf_i(ptr, pvendor)) {
1147                         fr_strerror_printf("Invalid number in attribute");
1148                         return -1;
1149                 }
1150
1151                 if (*pvendor >= FR_MAX_VENDOR) {
1152                         fr_strerror_printf("Cannot handle vendor ID larger than 2^24");
1153                         
1154                         return -1;
1155                 }
1156
1157                 dv = dict_vendorbyvalue(*pvendor & (FR_MAX_VENDOR - 1));
1158                 if (!dv) {
1159                         fr_strerror_printf("Unknown vendor \"%u\" ",
1160                                            *pvendor  & (FR_MAX_VENDOR - 1));
1161                         return -1;
1162                 }
1163
1164                 /*
1165                  *      Start off with (attr=0, vendor=VID), and
1166                  *      recurse.  This causes the various checks above
1167                  *      to be done.
1168                  */
1169                 *pvalue = 0;
1170                 return dict_str2oid(p + 1, pvalue, pvendor, 0);
1171         }
1172
1173         if (!sscanf_i(ptr, &value)) {
1174                 fr_strerror_printf("Invalid number in attribute");
1175                 return -1;
1176         }
1177
1178         if (!*pvendor && (tlv_depth == 1) &&
1179             (da->flags.has_tlv || da->flags.extended)) {
1180
1181
1182                 *pvendor = *pvalue * FR_MAX_VENDOR;
1183                 *pvalue = value;
1184
1185                 if (!p) return 0;
1186                 return dict_str2oid(p + 1, pvalue, pvendor, 1);
1187         }
1188
1189         /*
1190          *      And pack the data according to the scheme described in
1191          *      the comments at the start of this function.
1192          */
1193         if (*pvalue) {
1194                 *pvalue |= (value & fr_attr_mask[tlv_depth]) << fr_attr_shift[tlv_depth];
1195         } else {
1196                 *pvalue = value;
1197         }
1198
1199         if (p) {
1200                 return dict_str2oid(p + 1, pvalue, pvendor, tlv_depth + 1);
1201         }
1202
1203         return tlv_depth;
1204 }
1205
1206 /*
1207  *      Bamboo skewers under the fingernails in 5, 4, 3, 2, ...
1208  */
1209 static const DICT_ATTR *dict_parent(unsigned int attr, unsigned int vendor)
1210 {
1211         if (vendor < FR_MAX_VENDOR) {
1212                 return dict_attrbyvalue(attr & 0xff, vendor);
1213         }
1214
1215         if (attr < 256) {
1216                 return dict_attrbyvalue((vendor / FR_MAX_VENDOR) & 0xff, 0);
1217         }
1218
1219         return dict_attrbyvalue(attr & 0xff, vendor);
1220 }
1221
1222
1223 /*
1224  *      Process the ATTRIBUTE command
1225  */
1226 static int process_attribute(const char* fn, const int line,
1227                              unsigned int block_vendor,
1228                              const DICT_ATTR *block_tlv, int tlv_depth,
1229                              char **argv, int argc)
1230 {
1231         int             oid = 0;
1232         unsigned int    vendor = 0;
1233         unsigned int    value;
1234         int             type;
1235         unsigned int    length = 0;
1236         ATTR_FLAGS      flags;
1237         char            *p;
1238
1239         if ((argc < 3) || (argc > 4)) {
1240                 fr_strerror_printf("dict_init: %s[%d]: invalid ATTRIBUTE line",
1241                         fn, line);
1242                 return -1;
1243         }
1244
1245         /*
1246          *      Dictionaries need to have real names, not shitty ones.
1247          */
1248         if (strncmp(argv[1], "Attr-", 5) == 0) {
1249                 fr_strerror_printf("dict_init: %s[%d]: Invalid attribute name",
1250                                    fn, line);
1251                 return -1;
1252         }
1253
1254         memset(&flags, 0, sizeof(flags));
1255
1256         /*
1257          *      Look for OIDs before doing anything else.
1258          */
1259         p = strchr(argv[1], '.');
1260         if (p) oid = 1;
1261
1262         /*
1263          *      Validate all entries
1264          */
1265         if (!sscanf_i(argv[1], &value)) {
1266                 fr_strerror_printf("dict_init: %s[%d]: invalid value", fn, line);
1267                 return -1;
1268         }
1269
1270         if (oid) {
1271                 const DICT_ATTR *da;
1272
1273                 vendor = block_vendor;
1274
1275                 /*
1276                  *      Parse the rest of the OID.
1277                  */
1278                 if (dict_str2oid(p + 1, &value, &vendor, tlv_depth + 1) < 0) {
1279                         char buffer[256];
1280
1281                         strlcpy(buffer, fr_strerror(), sizeof(buffer));
1282                         
1283                         fr_strerror_printf("dict_init: %s[%d]: Invalid attribute identifier: %s", fn, line, buffer);
1284                         return -1;
1285                 }
1286                 block_vendor = vendor;
1287
1288                 /*
1289                  *      Set the flags based on the parents flags.
1290                  */
1291                 da = dict_parent(value, vendor);
1292                 if (!da) {
1293                         fr_strerror_printf("dict_init: %s[%d]: Parent attribute is undefined.", fn, line);
1294                         return -1;
1295                 }
1296
1297                 flags.extended = da->flags.extended;
1298                 flags.long_extended = da->flags.long_extended;
1299                 flags.evs = da->flags.evs;
1300                 if (da->flags.has_tlv) flags.is_tlv = 1;
1301         }
1302
1303         if (strncmp(argv[2], "octets[", 7) != 0) {
1304                 /*
1305                  *      find the type of the attribute.
1306                  */
1307                 type = fr_str2int(dict_attr_types, argv[2], -1);
1308                 if (type < 0) {
1309                         fr_strerror_printf("dict_init: %s[%d]: invalid type \"%s\"",
1310                                            fn, line, argv[2]);
1311                         return -1;
1312                 }
1313
1314         } else {
1315                 type = PW_TYPE_OCTETS;
1316                 
1317                 p = strchr(argv[2] + 7, ']');
1318                 if (!p) {
1319                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for octets", fn, line);
1320                         return -1;
1321                 }
1322
1323                 *p = 0;
1324
1325                 if (!sscanf_i(argv[1], &length)) {
1326                         fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1327                         return -1;
1328                 }
1329
1330                 if ((length == 0) || (length > 253)) {
1331                         fr_strerror_printf("dict_init: %s[%d]: invalid length", fn, line);
1332                         return -1;
1333                 }
1334         }
1335
1336         /*
1337          *      Only look up the vendor if the string
1338          *      is non-empty.
1339          */
1340         if (argc < 4) {
1341                 /*
1342                  *      Force "length" for data types of fixed length;
1343                  */
1344                 switch (type) {
1345                 case PW_TYPE_BYTE:
1346                         length = 1;
1347                         break;
1348
1349                 case PW_TYPE_SHORT:
1350                         length = 2;
1351                         break;
1352
1353                 case PW_TYPE_DATE:
1354                 case PW_TYPE_IPADDR:
1355                 case PW_TYPE_INTEGER:
1356                 case PW_TYPE_SIGNED:
1357                         length = 4;
1358                         break;
1359
1360                 case PW_TYPE_INTEGER64:
1361                         length = 8;
1362                         break;
1363
1364                 case PW_TYPE_ETHERNET:
1365                         length = 6;
1366                         break;
1367
1368                 case PW_TYPE_IFID:
1369                         length = 8;
1370                         break;
1371
1372                 case PW_TYPE_IPV6ADDR:
1373                         length = 16;
1374                         break;
1375
1376                 case PW_TYPE_EXTENDED:
1377                         if ((vendor != 0) || (value < 241)) {
1378                                 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"extended\" MUST be RFC attributes with value >= 241.", fn, line);
1379                                 return -1;
1380                         }
1381                         type = PW_TYPE_OCTETS;
1382                         flags.extended = 1;
1383                         break;
1384
1385                 case PW_TYPE_LONG_EXTENDED:
1386                         if ((vendor != 0) || (value < 241)) {
1387                                 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"long-extended\" MUST be RFC attributes with value >= 241.", fn, line);
1388                                 return -1;
1389                         }
1390                         type = PW_TYPE_OCTETS;
1391                         flags.extended = 1;
1392                         flags.long_extended = 1;
1393                         break;
1394
1395                 case PW_TYPE_EVS:
1396                         type = PW_TYPE_OCTETS;
1397                         flags.extended = 1;
1398                         flags.evs = 1;
1399                         if (value != PW_VENDOR_SPECIFIC) {
1400                                 fr_strerror_printf("dict_init: %s[%d]: Attributes of type \"evs\" MUST have attribute code 26.", fn, line);
1401                                 return -1;
1402                         }
1403                         break;
1404
1405                 default:
1406                         break;
1407                 }
1408
1409                 flags.length = length;
1410
1411         } else {                /* argc == 4: we have options */
1412                 char *key, *next, *last;
1413
1414                 /*
1415                  *      Keep it real.
1416                  */
1417                 if (flags.extended) {
1418                         fr_strerror_printf("dict_init: %s[%d]: Extended attributes cannot use flags", fn, line);
1419                         return -1;
1420                 }
1421
1422                 if (length != 0) {
1423                         fr_strerror_printf("dict_init: %s[%d]: length cannot be used with options", fn, line);
1424                         return -1;
1425                 }
1426
1427                 key = argv[3];
1428                 do {
1429                         next = strchr(key, ',');
1430                         if (next) *(next++) = '\0';
1431
1432                         if (strcmp(key, "has_tag") == 0 ||
1433                             strcmp(key, "has_tag=1") == 0) {
1434                                 /* Boolean flag, means this is a
1435                                    tagged attribute */
1436                                 flags.has_tag = 1;
1437                                 
1438                         } else if (strncmp(key, "encrypt=", 8) == 0) {
1439                                 /* Encryption method, defaults to 0 (none).
1440                                    Currently valid is just type 2,
1441                                    Tunnel-Password style, which can only
1442                                    be applied to strings. */
1443                                 flags.encrypt = strtol(key + 8, &last, 0);
1444                                 if (*last) {
1445                                         fr_strerror_printf( "dict_init: %s[%d] invalid option %s",
1446                                                     fn, line, key);
1447                                         return -1;
1448                                 }
1449
1450                                 if ((flags.encrypt == FLAG_ENCRYPT_ASCEND_SECRET) &&
1451                                     (type != PW_TYPE_STRING)) {
1452                                         fr_strerror_printf( "dict_init: %s[%d] Only \"string\" types can have the \"encrypt=3\" flag set.",
1453                                                             fn, line);
1454                                         return -1;
1455                                 }
1456                                 
1457                         } else if (strncmp(key, "array", 6) == 0) {
1458                                 flags.array = 1;
1459                                 
1460                                 switch (type) {
1461                                         case PW_TYPE_IPADDR:
1462                                         case PW_TYPE_BYTE:
1463                                         case PW_TYPE_SHORT:
1464                                         case PW_TYPE_INTEGER:
1465                                         case PW_TYPE_DATE:
1466                                                 break;
1467
1468                                         default:
1469                                                 fr_strerror_printf( "dict_init: %s[%d] Only IP addresses can have the \"array\" flag set.",
1470                                                             fn, line);
1471                                                 return -1;
1472                                 }
1473
1474                                 /*
1475                                  *      The only thing is the vendor name,
1476                                  *      and it's a known name: allow it.
1477                                  */
1478                         } else if ((key == argv[3]) && !next) {
1479                                 if (oid) {
1480                                         fr_strerror_printf( "dict_init: %s[%d] New-style attributes cannot use a vendor flag.",
1481                                                             fn, line);
1482                                         return -1;
1483                                 }
1484
1485                                 if (block_vendor) {
1486                                         fr_strerror_printf( "dict_init: %s[%d] Vendor flag inside of \"BEGIN-VENDOR\" is not allowed.",
1487                                                             fn, line);
1488                                         return -1;
1489                                 }
1490
1491                                 vendor = dict_vendorbyname(key);
1492                                 if (!vendor) goto unknown;
1493                                 break;
1494
1495                         } else {
1496                         unknown:
1497                                 fr_strerror_printf( "dict_init: %s[%d]: unknown option \"%s\"",
1498                                             fn, line, key);
1499                                 return -1;
1500                         }
1501
1502                         key = next;
1503                         if (key && !*key) break;
1504                 } while (key);
1505         }
1506
1507         if (block_vendor) vendor = block_vendor;
1508
1509         /*
1510          *      Special checks for tags, they make our life much more
1511          *      difficult.
1512          */
1513         if (flags.has_tag) {
1514                 /*
1515                  *      Only string, octets, and integer can be tagged.
1516                  */
1517                 switch (type) {
1518                 case PW_TYPE_STRING:
1519                 case PW_TYPE_INTEGER:
1520                         break;
1521
1522                 default:
1523                         fr_strerror_printf("dict_init: %s[%d]: Attributes of type %s cannot be tagged.",
1524                                    fn, line,
1525                                    fr_int2str(dict_attr_types, type, "?Unknown?"));
1526                         return -1;
1527                 }
1528         }
1529
1530         if (type == PW_TYPE_TLV) {
1531                 if (vendor && (vendor < FR_MAX_VENDOR)
1532 #ifdef WITH_DHCP
1533                     && (vendor != DHCP_MAGIC_VENDOR)
1534 #endif
1535                         ) {
1536                         DICT_VENDOR *dv;
1537
1538                         dv = dict_vendorbyvalue(vendor);
1539                         if (!dv || (dv->type != 1) || (dv->length != 1)) {
1540                                 fr_strerror_printf("dict_init: %s[%d]: Type \"tlv\" can only be for \"format=1,1\".",
1541                                                    fn, line);
1542                                 return -1;
1543                         }
1544
1545                 }
1546                 flags.has_tlv = 1;
1547         }
1548         
1549         if (block_tlv) {
1550                 /*
1551                  *      TLV's can be only one octet.
1552                  */
1553                 if ((value == 0) || ((value & ~fr_attr_mask[tlv_depth]) != 0)) {
1554                         fr_strerror_printf( "dict_init: %s[%d]: sub-tlv has invalid attribute number",
1555                                             fn, line);
1556                         return -1;
1557                 }
1558
1559                 /*
1560                  *      
1561                  */
1562                 value <<= fr_attr_shift[tlv_depth];
1563                 value |= block_tlv->attr;
1564                 flags.is_tlv = 1;
1565         }
1566
1567 #ifdef WITH_DICTIONARY_WARNINGS
1568         /*
1569          *      Hack to help us discover which vendors have illegal
1570          *      attributes.
1571          */
1572         if (!vendor && (value < 256) &&
1573             !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1574                 fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
1575                         argv[0], fn);
1576         }
1577 #endif
1578
1579         /*
1580          *      Add it in.
1581          */
1582         if (dict_addattr(argv[0], value, vendor, type, flags) < 0) {
1583                 char buffer[256];
1584
1585                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1586
1587                 fr_strerror_printf("dict_init: %s[%d]: %s",
1588                                    fn, line, buffer);
1589                 return -1;
1590         }
1591
1592         return 0;
1593 }
1594
1595
1596 /*
1597  *      Process the VALUE command
1598  */
1599 static int process_value(const char* fn, const int line, char **argv,
1600                          int argc)
1601 {
1602         unsigned int    value;
1603
1604         if (argc != 3) {
1605                 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE line",
1606                         fn, line);
1607                 return -1;
1608         }
1609         /*
1610          *      For Compatibility, skip "Server-Config"
1611          */
1612         if (strcasecmp(argv[0], "Server-Config") == 0)
1613                 return 0;
1614
1615         /*
1616          *      Validate all entries
1617          */
1618         if (!sscanf_i(argv[2], &value)) {
1619                 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1620                         fn, line);
1621                 return -1;
1622         }
1623
1624         if (dict_addvalue(argv[1], argv[0], value) < 0) {
1625                 char buffer[256];
1626
1627                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1628
1629                 fr_strerror_printf("dict_init: %s[%d]: %s",
1630                                    fn, line, buffer);
1631                 return -1;
1632         }
1633
1634         return 0;
1635 }
1636
1637
1638 /*
1639  *      Process the VALUE-ALIAS command
1640  *
1641  *      This allows VALUE mappings to be shared among multiple
1642  *      attributes.
1643  */
1644 static int process_value_alias(const char* fn, const int line, char **argv,
1645                                int argc)
1646 {
1647         const DICT_ATTR *my_da, *da;
1648         DICT_VALUE *dval;
1649
1650         if (argc != 2) {
1651                 fr_strerror_printf("dict_init: %s[%d]: invalid VALUE-ALIAS line",
1652                         fn, line);
1653                 return -1;
1654         }
1655
1656         my_da = dict_attrbyname(argv[0]);
1657         if (!my_da) {
1658                 fr_strerror_printf("dict_init: %s[%d]: ATTRIBUTE \"%s\" does not exist",
1659                            fn, line, argv[1]);
1660                 return -1;
1661         }
1662
1663         if (my_da->flags.has_value_alias) {
1664                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" with pre-existing VALUE-ALIAS",
1665                            fn, line, argv[0]);
1666                 return -1;
1667         }
1668
1669         da = dict_attrbyname(argv[1]);
1670         if (!da) {
1671                 fr_strerror_printf("dict_init: %s[%d]: Cannot find ATTRIBUTE \"%s\" for alias",
1672                            fn, line, argv[1]);
1673                 return -1;
1674         }
1675
1676         if (da->flags.has_value_alias) {
1677                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS to ATTRIBUTE \"%s\" which itself has a VALUE-ALIAS",
1678                            fn, line, argv[1]);
1679                 return -1;
1680         }
1681
1682         if (my_da->type != da->type) {
1683                 fr_strerror_printf("dict_init: %s[%d]: Cannot add VALUE-ALIAS between attributes of differing type",
1684                            fn, line);
1685                 return -1;
1686         }
1687
1688         if ((dval = fr_pool_alloc(sizeof(*dval))) == NULL) {
1689                 fr_strerror_printf("dict_addvalue: out of memory");
1690                 return -1;
1691         }
1692
1693         dval->name[0] = '\0';   /* empty name */
1694         dval->attr = my_da->attr;
1695         dval->vendor = my_da->vendor;
1696         dval->value = da->attr;
1697
1698         if (!fr_hash_table_insert(values_byname, dval)) {
1699                 fr_strerror_printf("dict_init: %s[%d]: Error create alias",
1700                            fn, line);
1701                 fr_pool_free(dval);
1702                 return -1;
1703         }
1704
1705         return 0;
1706 }
1707
1708
1709 /*
1710  *      Process the VENDOR command
1711  */
1712 static int process_vendor(const char* fn, const int line, char **argv,
1713                           int argc)
1714 {
1715         int     value;
1716         int     continuation = 0;
1717         const   char *format = NULL;
1718
1719         if ((argc < 2) || (argc > 3)) {
1720                 fr_strerror_printf( "dict_init: %s[%d] invalid VENDOR entry",
1721                             fn, line);
1722                 return -1;
1723         }
1724
1725         /*
1726          *       Validate all entries
1727          */
1728         if (!isdigit((int) argv[1][0])) {
1729                 fr_strerror_printf("dict_init: %s[%d]: invalid value",
1730                         fn, line);
1731                 return -1;
1732         }
1733         value = atoi(argv[1]);
1734
1735         /* Create a new VENDOR entry for the list */
1736         if (dict_addvendor(argv[0], value) < 0) {
1737                 char buffer[256];
1738
1739                 strlcpy(buffer, fr_strerror(), sizeof(buffer));
1740
1741                 fr_strerror_printf("dict_init: %s[%d]: %s",
1742                            fn, line, buffer);
1743                 return -1;
1744         }
1745
1746         /*
1747          *      Look for a format statement
1748          */
1749         if (argc == 3) {
1750                 format = argv[2];
1751
1752         } else if (value == VENDORPEC_USR) { /* catch dictionary screw-ups */
1753                 format = "format=4,0";
1754
1755         } else if (value == VENDORPEC_LUCENT) {
1756                 format = "format=2,1";
1757
1758         } else if (value == VENDORPEC_STARENT) {
1759                 format = "format=2,2";
1760
1761         } /* else no fixups to do */
1762
1763         if (format) {
1764                 int type, length;
1765                 const char *p;
1766                 DICT_VENDOR *dv;
1767
1768                 if (strncasecmp(format, "format=", 7) != 0) {
1769                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected \"format=\", got \"%s\"",
1770                                    fn, line, format);
1771                         return -1;
1772                 }
1773
1774                 p = format + 7;
1775                 if ((strlen(p) < 3) ||
1776                     !isdigit((int) p[0]) ||
1777                     (p[1] != ',') ||
1778                     !isdigit((int) p[2]) ||
1779                     (p[3] && (p[3] != ','))) {
1780                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
1781                                    fn, line, p);
1782                         return -1;
1783                 }
1784
1785                 type = (int) (p[0] - '0');
1786                 length = (int) (p[2] - '0');
1787
1788                 if (p[3] == ',') {
1789                         if (!p[4]) {
1790                                 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
1791                                    fn, line, p);
1792                                 return -1;
1793                         }
1794
1795                         if ((p[4] != 'c') ||
1796                             (p[5] != '\0')) {
1797                                 fr_strerror_printf("dict_init: %s[%d]: Invalid format for VENDOR.  Expected text like \"1,1\", got \"%s\"",
1798                                            fn, line, p);
1799                                 return -1;
1800                         }
1801                         continuation = 1;
1802
1803                         if ((value != VENDORPEC_WIMAX) ||
1804                             (type != 1) || (length != 1)) {
1805                                 fr_strerror_printf("dict_init: %s[%d]: Only WiMAX VSAs can have continuations",
1806                                            fn, line);
1807                                 return -1;
1808                         }
1809                 }
1810
1811                 dv = dict_vendorbyvalue(value);
1812                 if (!dv) {
1813                         fr_strerror_printf("dict_init: %s[%d]: Failed adding format for VENDOR",
1814                                    fn, line);
1815                         return -1;
1816                 }
1817
1818                 if ((type != 1) && (type != 2) && (type != 4)) {
1819                         fr_strerror_printf("dict_init: %s[%d]: invalid type value %d for VENDOR",
1820                                    fn, line, type);
1821                         return -1;
1822                 }
1823
1824                 if ((length != 0) && (length != 1) && (length != 2)) {
1825                         fr_strerror_printf("dict_init: %s[%d]: invalid length value %d for VENDOR",
1826                                    fn, line, length);
1827                         return -1;
1828                 }
1829
1830                 dv->type = type;
1831                 dv->length = length;
1832                 dv->flags = continuation;
1833         }
1834
1835         return 0;
1836 }
1837
1838 /*
1839  *      String split routine.  Splits an input string IN PLACE
1840  *      into pieces, based on spaces.
1841  */
1842 int str2argv(char *str, char **argv, int max_argc)
1843 {
1844         int argc = 0;
1845
1846         while (*str) {
1847                 if (argc >= max_argc) break;
1848
1849                 /*
1850                  *      Chop out comments early.
1851                  */
1852                 if (*str == '#') {
1853                         *str = '\0';
1854                         break;
1855                 }
1856
1857                 while ((*str == ' ') ||
1858                        (*str == '\t') ||
1859                        (*str == '\r') ||
1860                        (*str == '\n')) *(str++) = '\0';
1861
1862                 if (!*str) break;
1863
1864                 argv[argc] = str;
1865                 argc++;
1866
1867                 while (*str &&
1868                        (*str != ' ') &&
1869                        (*str != '\t') &&
1870                        (*str != '\r') &&
1871                        (*str != '\n')) str++;
1872         }
1873
1874         return argc;
1875 }
1876
1877 #define MAX_ARGV (16)
1878
1879 /*
1880  *      Initialize the dictionary.
1881  */
1882 static int my_dict_init(const char *dir, const char *fn,
1883                         const char *src_file, int src_line)
1884 {
1885         FILE    *fp;
1886         char    dirtmp[256];
1887         char    buf[256];
1888         char    *p;
1889         int     line = 0;
1890         unsigned int    vendor;
1891         unsigned int    block_vendor;
1892         struct stat statbuf;
1893         char    *argv[MAX_ARGV];
1894         int     argc;
1895         const DICT_ATTR *da, *block_tlv[MAX_TLV_NEST + 1];
1896         int     which_block_tlv = 0;
1897
1898         block_tlv[0] = NULL;
1899         block_tlv[1] = NULL;
1900         block_tlv[2] = NULL;
1901         block_tlv[3] = NULL;
1902
1903         if (strlen(fn) >= sizeof(dirtmp) / 2 ||
1904             strlen(dir) >= sizeof(dirtmp) / 2) {
1905                 fr_strerror_printf("dict_init: filename name too long");
1906                 return -1;
1907         }
1908
1909         /*
1910          *      First see if fn is relative to dir. If so, create
1911          *      new filename. If not, remember the absolute dir.
1912          */
1913         if ((p = strrchr(fn, FR_DIR_SEP)) != NULL) {
1914                 strcpy(dirtmp, fn);
1915                 dirtmp[p - fn] = 0;
1916                 dir = dirtmp;
1917         } else if (dir && dir[0] && strcmp(dir, ".") != 0) {
1918                 snprintf(dirtmp, sizeof(dirtmp), "%s/%s", dir, fn);
1919                 fn = dirtmp;
1920         }
1921
1922         if ((fp = fopen(fn, "r")) == NULL) {
1923                 if (!src_file) {
1924                         fr_strerror_printf("dict_init: Couldn't open dictionary \"%s\": %s",
1925                                    fn, strerror(errno));
1926                 } else {
1927                         fr_strerror_printf("dict_init: %s[%d]: Couldn't open dictionary \"%s\": %s",
1928                                    src_file, src_line, fn, strerror(errno));
1929                 }
1930                 return -2;
1931         }
1932
1933         stat(fn, &statbuf); /* fopen() guarantees this will succeed */
1934         if (!S_ISREG(statbuf.st_mode)) {
1935                 fclose(fp);
1936                 fr_strerror_printf("dict_init: Dictionary \"%s\" is not a regular file",
1937                            fn);
1938                 return -1;
1939         }
1940
1941         /*
1942          *      Globally writable dictionaries means that users can control
1943          *      the server configuration with little difficulty.
1944          */
1945 #ifdef S_IWOTH
1946         if ((statbuf.st_mode & S_IWOTH) != 0) {
1947                 fclose(fp);
1948                 fr_strerror_printf("dict_init: Dictionary \"%s\" is globally writable.  Refusing to start due to insecure configuration.",
1949                            fn);
1950                 return -1;
1951         }
1952 #endif
1953
1954         dict_stat_add(fn, &statbuf);
1955
1956         /*
1957          *      Seed the random pool with data.
1958          */
1959         fr_rand_seed(&statbuf, sizeof(statbuf));
1960
1961         block_vendor = 0;
1962
1963         while (fgets(buf, sizeof(buf), fp) != NULL) {
1964                 line++;
1965                 if (buf[0] == '#' || buf[0] == 0 ||
1966                     buf[0] == '\n' || buf[0] == '\r')
1967                         continue;
1968
1969                 /*
1970                  *  Comment characters should NOT be appearing anywhere but
1971                  *  as start of a comment;
1972                  */
1973                 p = strchr(buf, '#');
1974                 if (p) *p = '\0';
1975
1976                 argc = str2argv(buf, argv, MAX_ARGV);
1977                 if (argc == 0) continue;
1978
1979                 if (argc == 1) {
1980                         fr_strerror_printf( "dict_init: %s[%d] invalid entry",
1981                                     fn, line);
1982                         fclose(fp);
1983                         return -1;
1984                 }
1985
1986                 /*
1987                  *      Process VALUE lines.
1988                  */
1989                 if (strcasecmp(argv[0], "VALUE") == 0) {
1990                         if (process_value(fn, line,
1991                                           argv + 1, argc - 1) == -1) {
1992                                 fclose(fp);
1993                                 return -1;
1994                         }
1995                         continue;
1996                 }
1997
1998                 /*
1999                  *      Perhaps this is an attribute.
2000                  */
2001                 if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
2002                         if (process_attribute(fn, line, block_vendor,
2003                                               block_tlv[which_block_tlv],
2004                                               which_block_tlv,
2005                                               argv + 1, argc - 1) == -1) {
2006                                 fclose(fp);
2007                                 return -1;
2008                         }
2009                         continue;
2010                 }
2011
2012                 /*
2013                  *      See if we need to import another dictionary.
2014                  */
2015                 if (strcasecmp(argv[0], "$INCLUDE") == 0) {
2016                         if (my_dict_init(dir, argv[1], fn, line) < 0) {
2017                                 fclose(fp);
2018                                 return -1;
2019                         }
2020                         continue;
2021                 } /* $INCLUDE */
2022
2023                 /*
2024                  *      Optionally include a dictionary
2025                  */
2026                 if (strcasecmp(argv[0], "$INCLUDE-") == 0) {
2027                         int rcode = my_dict_init(dir, argv[1], fn, line);
2028
2029                         if (rcode == -2) continue;
2030
2031                         if (rcode < 0) {
2032                                 fclose(fp);
2033                                 return -1;
2034                         }
2035                         continue;
2036                 } /* $INCLUDE- */
2037
2038                 if (strcasecmp(argv[0], "VALUE-ALIAS") == 0) {
2039                         if (process_value_alias(fn, line,
2040                                                 argv + 1, argc - 1) == -1) {
2041                                 fclose(fp);
2042                                 return -1;
2043                         }
2044                         continue;
2045                 }
2046
2047                 /*
2048                  *      Process VENDOR lines.
2049                  */
2050                 if (strcasecmp(argv[0], "VENDOR") == 0) {
2051                         if (process_vendor(fn, line,
2052                                            argv + 1, argc - 1) == -1) {
2053                                 fclose(fp);
2054                                 return -1;
2055                         }
2056                         continue;
2057                 }
2058
2059                 if (strcasecmp(argv[0], "BEGIN-TLV") == 0) {
2060                         if (argc != 2) {
2061                                 fr_strerror_printf(
2062                                 "dict_init: %s[%d] invalid BEGIN-TLV entry",
2063                                         fn, line);
2064                                 fclose(fp);
2065                                 return -1;
2066                         }
2067
2068                         da = dict_attrbyname(argv[1]);
2069                         if (!da) {
2070                                 fr_strerror_printf(
2071                                         "dict_init: %s[%d]: unknown attribute %s",
2072                                         fn, line, argv[1]);
2073                                 fclose(fp);
2074                                 return -1;
2075                         }
2076
2077                         if (da->type != PW_TYPE_TLV) {
2078                                 fr_strerror_printf(
2079                                         "dict_init: %s[%d]: attribute %s is not of type tlv",
2080                                         fn, line, argv[1]);
2081                                 fclose(fp);
2082                                 return -1;
2083                         }
2084
2085                         if (which_block_tlv >= MAX_TLV_NEST) {
2086                                 fr_strerror_printf(
2087                                         "dict_init: %s[%d]: TLVs are nested too deep",
2088                                         fn, line);
2089                                 fclose(fp);
2090                                 return -1;
2091                         }
2092
2093
2094                         block_tlv[++which_block_tlv] = da;
2095                         continue;
2096                 } /* BEGIN-TLV */
2097
2098                 if (strcasecmp(argv[0], "END-TLV") == 0) {
2099                         if (argc != 2) {
2100                                 fr_strerror_printf(
2101                                 "dict_init: %s[%d] invalid END-TLV entry",
2102                                         fn, line);
2103                                 fclose(fp);
2104                                 return -1;
2105                         }
2106
2107                         da = dict_attrbyname(argv[1]);
2108                         if (!da) {
2109                                 fr_strerror_printf(
2110                                         "dict_init: %s[%d]: unknown attribute %s",
2111                                         fn, line, argv[1]);
2112                                 fclose(fp);
2113                                 return -1;
2114                         }
2115
2116                         if (da != block_tlv[which_block_tlv]) {
2117                                 fr_strerror_printf(
2118                                         "dict_init: %s[%d]: END-TLV %s does not match any previous BEGIN-TLV",
2119                                         fn, line, argv[1]);
2120                                 fclose(fp);
2121                                 return -1;
2122                         }
2123                         block_tlv[which_block_tlv--] = NULL;
2124                         continue;
2125                 } /* END-VENDOR */
2126
2127                 if (strcasecmp(argv[0], "BEGIN-VENDOR") == 0) {
2128                         if (argc < 2) {
2129                                 fr_strerror_printf(
2130                                 "dict_init: %s[%d] invalid BEGIN-VENDOR entry",
2131                                         fn, line);
2132                                 fclose(fp);
2133                                 return -1;
2134                         }
2135
2136                         vendor = dict_vendorbyname(argv[1]);
2137                         if (!vendor) {
2138                                 fr_strerror_printf(
2139                                         "dict_init: %s[%d]: unknown vendor %s",
2140                                         fn, line, argv[1]);
2141                                 fclose(fp);
2142                                 return -1;
2143                         }
2144
2145                         block_vendor = vendor;
2146
2147                         /*
2148                          *      Check for extended attr VSAs
2149                          *
2150                          *      BEGIN-VENDOR foo format=Foo-Encapsulation-Attr
2151                          */
2152                         if (argc > 2) {
2153                                 if (strncmp(argv[2], "format=", 7) != 0) {
2154                                         fr_strerror_printf(
2155                                                 "dict_init: %s[%d]: Invalid format %s",
2156                                                 fn, line, argv[2]);
2157                                         fclose(fp);
2158                                         return -1;
2159                                 }
2160                                 
2161                                 p = argv[2] + 7;
2162                                 da = dict_attrbyname(p);
2163                                 if (!da) {
2164                                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR: unknown attribute \"%s\"",
2165                                                            fn, line, p);
2166                                         fclose(fp);
2167                                         return -1;
2168                                 }
2169
2170                                 if (!da->flags.evs) {
2171                                         fr_strerror_printf("dict_init: %s[%d]: Invalid format for BEGIN-VENDOR.  Attribute \"%s\" is not of \"evs\" data type",
2172                                                            fn, line, p);
2173                                         fclose(fp);
2174                                         return -1;
2175                                 }
2176                                 
2177                                 /*
2178                                  *      Pack the encapsulating
2179                                  *      attribute into the upper 8
2180                                  *      bits of the vendor ID
2181                                  */
2182                                 block_vendor |= (da->attr & fr_attr_mask[0]) * FR_MAX_VENDOR;
2183                         }
2184
2185                         continue;
2186                 } /* BEGIN-VENDOR */
2187
2188                 if (strcasecmp(argv[0], "END-VENDOR") == 0) {
2189                         if (argc != 2) {
2190                                 fr_strerror_printf(
2191                                 "dict_init: %s[%d] invalid END-VENDOR entry",
2192                                         fn, line);
2193                                 fclose(fp);
2194                                 return -1;
2195                         }
2196
2197                         vendor = dict_vendorbyname(argv[1]);
2198                         if (!vendor) {
2199                                 fr_strerror_printf(
2200                                         "dict_init: %s[%d]: unknown vendor %s",
2201                                         fn, line, argv[1]);
2202                                 fclose(fp);
2203                                 return -1;
2204                         }
2205
2206                         if (vendor != (block_vendor & (FR_MAX_VENDOR - 1))) {
2207                                 fr_strerror_printf(
2208                                         "dict_init: %s[%d]: END-VENDOR %s does not match any previous BEGIN-VENDOR",
2209                                         fn, line, argv[1]);
2210                                 fclose(fp);
2211                                 return -1;
2212                         }
2213                         block_vendor = 0;
2214                         continue;
2215                 } /* END-VENDOR */
2216
2217                 /*
2218                  *      Any other string: We don't recognize it.
2219                  */
2220                 fr_strerror_printf("dict_init: %s[%d] invalid keyword \"%s\"",
2221                            fn, line, argv[0]);
2222                 fclose(fp);
2223                 return -1;
2224         }
2225         fclose(fp);
2226         return 0;
2227 }
2228
2229
2230 /*
2231  *      Empty callback for hash table initialization.
2232  */
2233 static int null_callback(void *ctx, void *data)
2234 {
2235         ctx = ctx;              /* -Wunused */
2236         data = data;            /* -Wunused */
2237
2238         return 0;
2239 }
2240
2241
2242 /*
2243  *      Initialize the directory, then fix the attr member of
2244  *      all attributes.
2245  */
2246 int dict_init(const char *dir, const char *fn)
2247 {
2248         /*
2249          *      Check if we need to change anything.  If not, don't do
2250          *      anything.
2251          */
2252         if (dict_stat_check(dir, fn)) {
2253                 return 0;
2254         }
2255
2256         /*
2257          *      Free the dictionaries, and the stat cache.
2258          */
2259         dict_free();
2260         stat_root_dir = strdup(dir);
2261         stat_root_file = strdup(fn);
2262
2263         /*
2264          *      Create the table of vendor by name.   There MAY NOT
2265          *      be multiple vendors of the same name.
2266          *
2267          *      Each vendor is malloc'd, so the free function is free.
2268          */
2269         vendors_byname = fr_hash_table_create(dict_vendor_name_hash,
2270                                                 dict_vendor_name_cmp,
2271                                                 fr_pool_free);
2272         if (!vendors_byname) {
2273                 return -1;
2274         }
2275
2276         /*
2277          *      Create the table of vendors by value.  There MAY
2278          *      be vendors of the same value.  If there are, we
2279          *      pick the latest one.
2280          */
2281         vendors_byvalue = fr_hash_table_create(dict_vendor_value_hash,
2282                                                  dict_vendor_value_cmp,
2283                                                  fr_pool_free);
2284         if (!vendors_byvalue) {
2285                 return -1;
2286         }
2287
2288         /*
2289          *      Create the table of attributes by name.   There MAY NOT
2290          *      be multiple attributes of the same name.
2291          *
2292          *      Each attribute is malloc'd, so the free function is free.
2293          */
2294         attributes_byname = fr_hash_table_create(dict_attr_name_hash,
2295                                                    dict_attr_name_cmp,
2296                                                    fr_pool_free);
2297         if (!attributes_byname) {
2298                 return -1;
2299         }
2300
2301         /*
2302          *      Create the table of attributes by value.  There MAY
2303          *      be attributes of the same value.  If there are, we
2304          *      pick the latest one.
2305          */
2306         attributes_byvalue = fr_hash_table_create(dict_attr_value_hash,
2307                                                     dict_attr_value_cmp,
2308                                                     fr_pool_free);
2309         if (!attributes_byvalue) {
2310                 return -1;
2311         }
2312
2313         /*
2314          *      Horrible hacks for combo-IP.
2315          */
2316         attributes_combo = fr_hash_table_create(dict_attr_combo_hash,
2317                                                 dict_attr_combo_cmp,
2318                                                 fr_pool_free);
2319         if (!attributes_combo) {
2320                 return -1;
2321         }
2322
2323         values_byname = fr_hash_table_create(dict_value_name_hash,
2324                                                dict_value_name_cmp,
2325                                                fr_pool_free);
2326         if (!values_byname) {
2327                 return -1;
2328         }
2329
2330         values_byvalue = fr_hash_table_create(dict_value_value_hash,
2331                                                 dict_value_value_cmp,
2332                                                 fr_pool_free);
2333         if (!values_byvalue) {
2334                 return -1;
2335         }
2336
2337         value_fixup = NULL;     /* just to be safe. */
2338
2339         if (my_dict_init(dir, fn, NULL, 0) < 0)
2340                 return -1;
2341
2342         if (value_fixup) {
2343                 const DICT_ATTR *a;
2344                 value_fixup_t *this, *next;
2345
2346                 for (this = value_fixup; this != NULL; this = next) {
2347                         next = this->next;
2348
2349                         a = dict_attrbyname(this->attrstr);
2350                         if (!a) {
2351                                 fr_strerror_printf(
2352                                         "dict_init: No ATTRIBUTE \"%s\" defined for VALUE \"%s\"",
2353                                         this->attrstr, this->dval->name);
2354                                 return -1; /* leak, but they should die... */
2355                         }
2356
2357                         this->dval->attr = a->attr;
2358
2359                         /*
2360                          *      Add the value into the dictionary.
2361                          */
2362                         if (!fr_hash_table_replace(values_byname,
2363                                                      this->dval)) {
2364                                 fr_strerror_printf("dict_addvalue: Duplicate value name %s for attribute %s", this->dval->name, a->name);
2365                                 return -1;
2366                         }
2367
2368                         /*
2369                          *      Allow them to use the old name, but
2370                          *      prefer the new name when printing
2371                          *      values.
2372                          */
2373                         if (!fr_hash_table_finddata(values_byvalue, this->dval)) {
2374                                 fr_hash_table_replace(values_byvalue,
2375                                                         this->dval);
2376                         }
2377                         free(this);
2378
2379                         /*
2380                          *      Just so we don't lose track of things.
2381                          */
2382                         value_fixup = next;
2383                 }
2384         }
2385
2386         /*
2387          *      Walk over all of the hash tables to ensure they're
2388          *      initialized.  We do this because the threads may perform
2389          *      lookups, and we don't want multi-threaded re-ordering
2390          *      of the table entries.  That would be bad.
2391          */
2392         fr_hash_table_walk(vendors_byname, null_callback, NULL);
2393         fr_hash_table_walk(vendors_byvalue, null_callback, NULL);
2394
2395         fr_hash_table_walk(attributes_byname, null_callback, NULL);
2396         fr_hash_table_walk(attributes_byvalue, null_callback, NULL);
2397
2398         fr_hash_table_walk(values_byvalue, null_callback, NULL);
2399         fr_hash_table_walk(values_byname, null_callback, NULL);
2400
2401         return 0;
2402 }
2403
2404 static size_t print_attr_oid(char *buffer, size_t size, unsigned int attr,
2405                              int dv_type)
2406 {
2407         int nest;
2408         size_t outlen;
2409         size_t len;
2410
2411         switch (dv_type) {
2412         case 4:
2413                 return snprintf(buffer, size, "%u", attr);
2414
2415         case 2:
2416                 return snprintf(buffer, size, "%u", attr & 0xffff);
2417
2418         default:
2419         case 1:
2420                 len = snprintf(buffer, size, "%u", attr & 0xff);
2421                 break;
2422         }
2423
2424         if ((attr >> 8) == 0) return len;
2425
2426         outlen = len;
2427         buffer += len;
2428         size -= len;
2429
2430         for (nest = 1; nest <= fr_attr_max_tlv; nest++) {
2431                 if (((attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]) == 0) break;
2432
2433                 len = snprintf(buffer, size, ".%u",
2434                                (attr >> fr_attr_shift[nest]) & fr_attr_mask[nest]);
2435
2436                 outlen = len;
2437                 buffer += len;
2438                 size -= len;
2439         }
2440
2441         return outlen;
2442 }
2443
2444 /** Free dynamically allocated (unknown attributes)
2445  * 
2446  * If the da was dynamically allocated it will be freed, else the function
2447  * will return without doing anything.
2448  *
2449  * @param da to free.
2450  */
2451 void dict_attr_free(DICT_ATTR const **da)
2452 {
2453         DICT_ATTR **tmp;
2454         
2455         /* Don't free real DAs */
2456         if (!(*da)->flags.is_unknown) {
2457                 return;
2458         }
2459         
2460         memcpy(&tmp, &da, sizeof(*tmp));
2461         free(*tmp);
2462         
2463         *tmp = NULL;    
2464 }
2465
2466 /** Copies a dictionary attr
2467  *
2468  * If the attr is dynamically allocated (unknown attribute), then it will be
2469  * copied to a new attr.
2470  *
2471  * If the attr is known, a pointer to the da will be returned.
2472  *
2473  * @param da to copy.
2474  * @return return a copy of the da.
2475  */
2476 const DICT_ATTR *dict_attr_copy(const DICT_ATTR *da, int vp_free)
2477 {
2478         DICT_ATTR *copy;
2479         
2480         if (!da) return NULL;
2481         
2482         if (!da->flags.is_unknown) {
2483                 return da;
2484         }
2485         
2486         copy = malloc(DICT_ATTR_SIZE);
2487         if (!copy) {
2488                 fr_strerror_printf("Out of memory");
2489                 return NULL;
2490         }
2491         
2492         memcpy(copy, da, DICT_ATTR_SIZE);
2493         copy->flags.vp_free = (vp_free != 0);
2494         
2495         return copy;
2496 }
2497
2498
2499 /** Allocs an dictionary attr for unknown attributes
2500  *
2501  * Allocates a dict attr for an unknown attribute/vendor/type
2502  * without adding it to dictionary pools/hashes.
2503  *
2504  * @note Must be freed with dict_attr_free if not used as part of a valuepair.
2505  *
2506  * @param[in] attr number.
2507  * @param[in] vendor number.
2508  * @param[in] vp_free if > 0 DICT_ATTR will be freed on VALUE_PAIR free.
2509  * @return new dictionary attribute.
2510  */
2511 const DICT_ATTR *dict_attrunknown(unsigned int attr, unsigned int vendor,
2512                                   int vp_free)
2513 {
2514         DICT_ATTR *da;
2515         char *p;
2516         int dv_type = 1;
2517         size_t len = 0;
2518         size_t bufsize = DICT_ATTR_MAX_NAME_LEN;
2519         
2520         da = malloc(DICT_ATTR_SIZE);
2521         if (!da) {
2522                 fr_strerror_printf("Out of memory");
2523                 return NULL;
2524         }
2525         memset(da, 0, DICT_ATTR_SIZE);
2526         
2527         da->attr = attr;
2528         da->vendor = vendor;
2529         da->type = PW_TYPE_OCTETS;
2530         da->flags.is_unknown = TRUE;
2531         da->flags.vp_free = (vp_free != 0);
2532         
2533         p = da->name;
2534         
2535         len = snprintf(p, bufsize, "Attr-");
2536         p += len;
2537         bufsize -= len;
2538
2539         if (vendor > FR_MAX_VENDOR) {
2540                 len = snprintf(p, bufsize, "%u.", vendor / FR_MAX_VENDOR);
2541                 p += len;
2542                 bufsize -= len;
2543                 vendor &= (FR_MAX_VENDOR) - 1;
2544         }
2545
2546         if (vendor) {
2547                 DICT_VENDOR *dv;
2548
2549                 /*
2550                  *      dv_type is the length of the vendor's type field
2551                  *      RFC 2865 never defined a mandatory length, so
2552                  *      different vendors have different length type fields.
2553                  */
2554                 dv = dict_vendorbyvalue(vendor);
2555                 if (dv) {
2556                         dv_type = dv->type;
2557                 }
2558                 len = snprintf(p, bufsize, "26.%u.", vendor);
2559                 
2560                 p += len;
2561                 bufsize -= len;
2562         }
2563
2564         p += print_attr_oid(p, bufsize , attr, dv_type);
2565         
2566         return da;
2567 }
2568
2569 /** Create a DICT_ATTR from an ASCII attribute and value
2570  *
2571  * Where the attribute name is in the form:
2572  *  - Attr-%d
2573  *  - Attr-%d.%d.%d...
2574  *  - Vendor-%d-Attr-%d
2575  *  - VendorName-Attr-%d
2576  *
2577  * @todo should check attr/vendor against dictionary and return the real da.
2578  *
2579  * @param[in] attribute name.
2580  * @param[in] vp_free if > 0 DICT_ATTR will be freed on VALUE_PAIR free.
2581  * @return new da or NULL on error.
2582  */
2583 const DICT_ATTR *dict_attrunknownbyname(const char *attribute, int vp_free)
2584 {
2585         unsigned int    attr, vendor = 0;
2586         unsigned int    dv_type = 1;    /* The type of vendor field */
2587
2588         const char      *p = attribute;
2589         char            *q;
2590         
2591         DICT_VENDOR     *dv;
2592         const DICT_ATTR *da;
2593
2594         /*
2595          *      Pull off vendor prefix first.
2596          */
2597         if (strncasecmp(p, "Attr-", 5) != 0) {
2598                 if (strncasecmp(p, "Vendor-", 7) == 0) {
2599                         vendor = (int) strtol(p + 7, &q, 10);
2600                         if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2601                                 fr_strerror_printf("Invalid vendor value in "
2602                                                    "attribute name \"%s\"", 
2603                                                    attribute);
2604                                 return NULL;
2605                         }
2606
2607                         p = q;
2608
2609                 /* must be vendor name */
2610                 } else {
2611                         char buffer[256];
2612
2613                         q = strchr(p, '-');
2614
2615                         if (!q) {
2616                                 fr_strerror_printf("Invalid vendor name in "
2617                                                    "attribute name \"%s\"",
2618                                                    attribute);
2619                                 return NULL;
2620                         }
2621
2622                         if ((size_t) (q - p) >= sizeof(buffer)) {
2623                                 fr_strerror_printf("Vendor name too long "
2624                                                    "in attribute name \"%s\"",
2625                                                    attribute);
2626                                 return NULL;
2627                         }
2628
2629                         memcpy(buffer, p, (q - p));
2630                         buffer[q - p] = '\0';
2631
2632                         vendor = dict_vendorbyname(buffer);
2633                         if (!vendor) {
2634                                 fr_strerror_printf("Unknown vendor name in "
2635                                                    "attribute name \"%s\"",
2636                                                    attribute);
2637                                 return NULL;
2638                         }
2639
2640                         p = q;
2641                 }
2642
2643                 if (*p != '-') {
2644                         fr_strerror_printf("Invalid text following vendor "
2645                                            "definition in attribute name "
2646                                            "\"%s\"", attribute);
2647                         return NULL;
2648                 }
2649                 p++;
2650         }
2651         
2652         /*
2653          *      Attr-%d
2654          */
2655         if (strncasecmp(p, "Attr-", 5) != 0) {
2656                 fr_strerror_printf("Invalid format in attribute name \"%s\"",
2657                                    attribute);
2658                 return NULL;
2659         }
2660
2661         attr = strtol(p + 5, &q, 10);
2662
2663         /*
2664          *      Invalid attribute.
2665          */
2666         if (attr == 0) {
2667                 fr_strerror_printf("Invalid value in attribute name \"%s\"",
2668                                    attribute);
2669                 return NULL;
2670         }
2671
2672         p = q;
2673         
2674         /*
2675          *      Vendor-%d-Attr-%d
2676          *      VendorName-Attr-%d
2677          *      Attr-%d
2678          *      Attr-%d.
2679          *
2680          *      Anything else is invalid.
2681          */
2682         if (((vendor != 0) && (*p != '\0')) ||
2683             ((vendor == 0) && *p && (*p != '.'))) {
2684         invalid:
2685                 fr_strerror_printf("Invalid OID");
2686                 return NULL;
2687         }
2688         
2689         /*
2690          *      Look for OIDs.  Require the "Attr-26.Vendor-Id.type"
2691          *      format, and disallow "Vendor-%d-Attr-%d" and
2692          *      "VendorName-Attr-%d"
2693          *
2694          *      This section parses the Vendor-Id portion of
2695          *      Attr-%d.%d.  where the first number is 26, *or* an
2696          *      extended attribute of the "evs" data type.
2697          */
2698         if (*p == '.') {
2699                 da = dict_attrbyvalue(attr, 0);
2700                 if (!da) {
2701                         fr_strerror_printf("Cannot parse attributes without "
2702                                            "dictionaries");
2703                         return NULL;
2704                 }               
2705                 
2706                 if ((attr != PW_VENDOR_SPECIFIC) &&
2707                     !(da->flags.extended || da->flags.long_extended)) {
2708                         fr_strerror_printf("Standard attributes cannot use "
2709                                            "OIDs");
2710                         return NULL;
2711                 }
2712
2713                 if ((attr == PW_VENDOR_SPECIFIC) || da->flags.evs) {
2714                         vendor = strtol(p + 1, &q, 10);
2715                         if ((vendor == 0) || (vendor > FR_MAX_VENDOR)) {
2716                                 fr_strerror_printf("Invalid vendor");
2717                                 return NULL;
2718                         }
2719
2720                         if (*q != '.') goto invalid;
2721
2722                         p = q;
2723
2724                         if (da->flags.evs) {
2725                                 vendor |= attr * FR_MAX_VENDOR;
2726                         }
2727                         attr = 0;
2728                 } /* else the second number is a TLV number */
2729         }
2730
2731         /*
2732          *      Get the expected maximum size of the attribute.
2733          */
2734         if (vendor) {
2735                 dv = dict_vendorbyvalue(vendor & (FR_MAX_VENDOR - 1));
2736                 if (dv) {
2737                         dv_type = dv->type;
2738                         if (dv_type > 3) dv_type = 3; /* hack */
2739                 }
2740         }
2741         
2742         /*
2743          *      Parse the next number.  It could be a Vendor-Type
2744          *      of 1..2^24, or it could be a TLV.
2745          */
2746         if (*p == '.') {
2747                 attr = strtol(p + 1, &q, 10);
2748                 if (attr == 0) {
2749                         fr_strerror_printf("Invalid attribute number");
2750                         return NULL;
2751                 }
2752
2753                 if (*q) {
2754                         if (*q != '.') {
2755                                 goto invalid;
2756                         }
2757                         
2758                         if (dv_type != 1) {
2759                                 goto invalid;
2760                         }
2761                 }
2762
2763                 p = q;
2764         }
2765
2766         /*
2767          *      Enforce a maximum value on the attribute number.
2768          */
2769         if (attr >= (unsigned) (1 << (dv_type << 3))) goto invalid;
2770
2771         if (*p == '.') {
2772                 if (dict_str2oid(p + 1, &attr, &vendor, 1) < 0) {
2773                         return NULL;
2774                 }
2775         }
2776
2777         return dict_attrunknown(attr, vendor, vp_free);
2778 }
2779
2780 /*
2781  *      Get an attribute by its numerical value.
2782  */
2783 const DICT_ATTR *dict_attrbyvalue(unsigned int attr, unsigned int vendor)
2784 {
2785         DICT_ATTR dattr;
2786
2787         if ((attr > 0) && (attr < 256) && !vendor) return dict_base_attrs[attr];
2788
2789         dattr.attr = attr;
2790         dattr.vendor = vendor;
2791
2792         return fr_hash_table_finddata(attributes_byvalue, &dattr);
2793 }
2794
2795
2796 /**
2797  * @brief Get an attribute by its numerical value. and data type
2798  *
2799  *      Used only for COMBO_IP
2800  *
2801  * @return The attribute, or NULL if not found
2802  */
2803 const DICT_ATTR *dict_attrbytype(unsigned int attr, unsigned int vendor,
2804                            PW_TYPE type)
2805 {
2806         DICT_ATTR dattr;
2807
2808         dattr.attr = attr;
2809         dattr.vendor = vendor;
2810         dattr.type = type;
2811
2812         return fr_hash_table_finddata(attributes_combo, &dattr);
2813 }
2814
2815
2816 /**
2817  * @brief Using a parent and attr/vendor, find a child attr/vendor
2818  */
2819 int dict_attr_child(const DICT_ATTR *parent,
2820                     unsigned int *pattr, unsigned int *pvendor)
2821 {
2822         unsigned int attr, vendor;
2823         DICT_ATTR dattr;
2824
2825         if (!parent || !pattr || !pvendor) return FALSE;
2826
2827         attr = *pattr;
2828         vendor = *pvendor;
2829
2830         /*
2831          *      Only some types can have children
2832          */
2833         switch (parent->type) {
2834         default: return FALSE;
2835
2836         case PW_TYPE_VSA:
2837         case PW_TYPE_TLV:
2838         case PW_TYPE_EVS:
2839         case PW_TYPE_EXTENDED:
2840         case PW_TYPE_LONG_EXTENDED:
2841           break;
2842         }
2843
2844         if ((vendor == 0) && (parent->vendor != 0)) return FALSE;
2845
2846         /*
2847          *      Bootstrap by starting off with the parents values.
2848          */
2849         dattr.attr = parent->attr;
2850         dattr.vendor = parent->vendor;
2851
2852         /*
2853          *      Do various butchery to insert the "attr" value.
2854          *
2855          *      00VID   000000AA        normal VSA for vendor VID
2856          *      00VID   DDCCBBAA        normal VSAs with TLVs
2857          *      EE000   000000AA        extended attr (241.1)
2858          *      EE000   DDCCBBAA        extended attr with TLVs
2859          *      EEVID   000000AA        EVS with vendor VID, attr AAA
2860          *      EEVID   DDCCBBAA        EVS with TLVs
2861          */
2862         if (!dattr.vendor) {
2863                 dattr.vendor = parent->attr * FR_MAX_VENDOR;
2864                 dattr.vendor |= vendor;
2865                 dattr.attr = attr;
2866
2867         } else {
2868                 int i;
2869
2870                 /*
2871                  *      Trying to nest too deep.  It's an error
2872                  */
2873                 if (parent->attr & (fr_attr_mask[MAX_TLV_NEST] << fr_attr_shift[MAX_TLV_NEST])) {
2874                         return FALSE;
2875                 }
2876
2877                 for (i = MAX_TLV_NEST - 1; i >= 0; i--) {
2878                         if ((parent->attr & (fr_attr_mask[i] << fr_attr_shift[i]))) {
2879                                 dattr.attr |= (attr & fr_attr_mask[i + 1]) << fr_attr_shift[i + 1];
2880                                 goto find;
2881                         }
2882                 }
2883
2884                 return FALSE;
2885         }
2886
2887 find:
2888 #if 0
2889         fprintf(stderr, "LOOKING FOR %08x %08x + %08x %08x --> %08x %08x\n",
2890                 parent->vendor, parent->attr, attr, vendor,
2891                 dattr.vendor, dattr.attr);
2892 #endif
2893
2894         *pattr = dattr.attr;
2895         *pvendor = dattr.vendor;
2896         return TRUE;
2897 }
2898
2899 /*
2900  *      Get an attribute by it's numerical value, and the parent
2901  */
2902 const DICT_ATTR *dict_attrbyparent(const DICT_ATTR *parent,
2903                              unsigned int attr, unsigned int vendor)
2904 {
2905         unsigned int my_attr, my_vendor;
2906         DICT_ATTR dattr;
2907
2908         my_attr = attr;
2909         my_vendor = vendor;
2910
2911         if (!dict_attr_child(parent, &my_attr, &my_vendor)) return NULL;
2912
2913         dattr.attr = my_attr;
2914         dattr.vendor = my_vendor;
2915
2916         return fr_hash_table_finddata(attributes_byvalue, &dattr);
2917 }
2918
2919
2920 /*
2921  *      Get an attribute by its name.
2922  */
2923 const DICT_ATTR *dict_attrbyname(const char *name)
2924 {
2925         DICT_ATTR *da;
2926         uint32_t buffer[(sizeof(*da) + DICT_ATTR_MAX_NAME_LEN + 3)/4];
2927
2928         if (!name) return NULL;
2929
2930         da = (DICT_ATTR *) buffer;
2931         strlcpy(da->name, name, DICT_ATTR_MAX_NAME_LEN + 1);
2932
2933         return fr_hash_table_finddata(attributes_byname, da);
2934 }
2935
2936 /*
2937  *      Associate a value with an attribute and return it.
2938  */
2939 DICT_VALUE *dict_valbyattr(unsigned int attr, unsigned int vendor, int value)
2940 {
2941         DICT_VALUE dval, *dv;
2942
2943         /*
2944          *      First, look up aliases.
2945          */
2946         dval.attr = attr;
2947         dval.vendor = vendor;
2948         dval.name[0] = '\0';
2949
2950         /*
2951          *      Look up the attribute alias target, and use
2952          *      the correct attribute number if found.
2953          */
2954         dv = fr_hash_table_finddata(values_byname, &dval);
2955         if (dv) dval.attr = dv->value;
2956
2957         dval.value = value;
2958
2959         return fr_hash_table_finddata(values_byvalue, &dval);
2960 }
2961
2962 /*
2963  *      Associate a value with an attribute and return it.
2964  */
2965 const char *dict_valnamebyattr(unsigned int attr, unsigned int vendor, int value)
2966 {
2967         DICT_VALUE *dv;
2968
2969         dv = dict_valbyattr(attr, vendor, value);
2970         if (!dv) return "";
2971
2972         return dv->name;
2973 }
2974
2975 /*
2976  *      Get a value by its name, keyed off of an attribute.
2977  */
2978 DICT_VALUE *dict_valbyname(unsigned int attr, unsigned int vendor, const char *name)
2979 {
2980         DICT_VALUE *my_dv, *dv;
2981         uint32_t buffer[(sizeof(*my_dv) + DICT_VALUE_MAX_NAME_LEN + 3)/4];
2982
2983         if (!name) return NULL;
2984
2985         my_dv = (DICT_VALUE *) buffer;
2986         my_dv->attr = attr;
2987         my_dv->vendor = vendor;
2988         my_dv->name[0] = '\0';
2989
2990         /*
2991          *      Look up the attribute alias target, and use
2992          *      the correct attribute number if found.
2993          */
2994         dv = fr_hash_table_finddata(values_byname, my_dv);
2995         if (dv) my_dv->attr = dv->value;
2996
2997         strlcpy(my_dv->name, name, DICT_VALUE_MAX_NAME_LEN + 1);
2998
2999         return fr_hash_table_finddata(values_byname, my_dv);
3000 }
3001
3002 /*
3003  *      Get the vendor PEC based on the vendor name
3004  *
3005  *      This is efficient only for small numbers of vendors.
3006  */
3007 int dict_vendorbyname(const char *name)
3008 {
3009         DICT_VENDOR *dv;
3010         uint32_t buffer[(sizeof(*dv) + DICT_VENDOR_MAX_NAME_LEN + 3)/4];
3011
3012         if (!name) return 0;
3013
3014         dv = (DICT_VENDOR *) buffer;
3015         strlcpy(dv->name, name, DICT_VENDOR_MAX_NAME_LEN + 1);
3016
3017         dv = fr_hash_table_finddata(vendors_byname, dv);
3018         if (!dv) return 0;
3019
3020         return dv->vendorpec;
3021 }
3022
3023 /*
3024  *      Return the vendor struct based on the PEC.
3025  */
3026 DICT_VENDOR *dict_vendorbyvalue(int vendorpec)
3027 {
3028         DICT_VENDOR dv;
3029
3030         dv.vendorpec = vendorpec;
3031
3032         return fr_hash_table_finddata(vendors_byvalue, &dv);
3033 }