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