-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
dict.c
3748 lines (3141 loc) · 89 KB
/
dict.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* dict.c Routines to read the dictionary file.
*
* Version: $Id$
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2000,2006 The FreeRADIUS server project
*/
RCSID("$Id$")
#include <freeradius-devel/libradius.h>
#ifdef WITH_DHCP
#include <freeradius-devel/dhcp.h>
#endif
#include <ctype.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
/** Vendors and attribute names
*
* It's very likely that the same vendors will operate in multiple
* protocol spaces, but number their attributes differently, so we need
* per protocol dictionaries.
*
* There would also be conflicts for DHCP(v6)/RADIUS attributes etc...
*/
struct fr_dict {
fr_hash_table_t *vendors_by_name;
fr_hash_table_t *vendors_by_num;
fr_hash_table_t *attributes_by_name; //!< Lookup an attribute by its name.
fr_hash_table_t *attributes_by_num; //!< Lookup an attribute by its number.
fr_hash_table_t *attributes_combo; //!< Attributes that can be multiple types.
fr_hash_table_t *values_by_da; //!< Lookup an attribute enum by integer value.
fr_hash_table_t *values_by_name; //!< Lookup an attribute enum by name.
fr_dict_attr_t *base_attrs[256]; //!< Quick lookup for protocols with an 8bit attribute space.
fr_dict_attr_t *root; //!< Root attribute of this dictionary.
TALLOC_CTX *pool; //!< Talloc memory pool to reduce mallocs.
};
fr_dict_t *fr_main_dict;
/*
* For faster HUP's, we cache the stat information for
* files we've $INCLUDEd
*/
typedef struct dict_stat_t {
struct dict_stat_t *next;
struct stat stat_buf;
} dict_stat_t;
static dict_stat_t *stat_head = NULL;
static dict_stat_t *stat_tail = NULL;
typedef struct value_fixup_t {
char attrstr[FR_DICT_ATTR_MAX_NAME_LEN];
fr_dict_value_t *dval;
struct value_fixup_t *next;
} value_fixup_t;
/*
* So VALUEs in the dictionary can have forward references.
*/
static value_fixup_t *value_fixup = NULL;
const FR_NAME_NUMBER dict_attr_types[] = {
{ "integer", PW_TYPE_INTEGER },
{ "string", PW_TYPE_STRING },
{ "ipaddr", PW_TYPE_IPV4_ADDR },
{ "date", PW_TYPE_DATE },
{ "abinary", PW_TYPE_ABINARY },
{ "octets", PW_TYPE_OCTETS },
{ "ifid", PW_TYPE_IFID },
{ "ipv6addr", PW_TYPE_IPV6_ADDR },
{ "ipv6prefix", PW_TYPE_IPV6_PREFIX },
{ "byte", PW_TYPE_BYTE },
{ "short", PW_TYPE_SHORT },
{ "ether", PW_TYPE_ETHERNET },
{ "combo-ip", PW_TYPE_COMBO_IP_ADDR },
{ "tlv", PW_TYPE_TLV },
{ "signed", PW_TYPE_SIGNED },
{ "extended", PW_TYPE_EXTENDED },
{ "long-extended", PW_TYPE_LONG_EXTENDED },
{ "evs", PW_TYPE_EVS },
{ "uint8", PW_TYPE_BYTE },
{ "uint16", PW_TYPE_SHORT },
{ "uint32", PW_TYPE_INTEGER },
{ "int32", PW_TYPE_SIGNED },
{ "integer64", PW_TYPE_INTEGER64 },
{ "uint64", PW_TYPE_INTEGER64 },
{ "ipv4prefix", PW_TYPE_IPV4_PREFIX },
{ "cidr", PW_TYPE_IPV4_PREFIX },
{ "vsa", PW_TYPE_VSA },
{ "vendor", PW_TYPE_VENDOR },
{ NULL, 0 }
};
/*
* Map data types to min / max data sizes.
*/
const size_t dict_attr_sizes[PW_TYPE_MAX][2] = {
[PW_TYPE_INVALID] = {~0, 0},
[PW_TYPE_STRING] = {0, ~0},
[PW_TYPE_INTEGER] = {4, 4 },
[PW_TYPE_IPV4_ADDR] = {4, 4},
[PW_TYPE_DATE] = {4, 4},
[PW_TYPE_ABINARY] = {32, ~0},
[PW_TYPE_OCTETS] = {0, ~0},
[PW_TYPE_IFID] = {8, 8},
[PW_TYPE_IPV6_ADDR] = {16, 16},
[PW_TYPE_IPV6_PREFIX] = {2, 18},
[PW_TYPE_BYTE] = {1, 1},
[PW_TYPE_SHORT] = {2, 2},
[PW_TYPE_ETHERNET] = {6, 6},
[PW_TYPE_SIGNED] = {4, 4},
[PW_TYPE_COMBO_IP_ADDR] = {4, 16},
[PW_TYPE_TLV] = {2, ~0},
[PW_TYPE_EXTENDED] = {2, ~0},
[PW_TYPE_LONG_EXTENDED] = {3, ~0},
[PW_TYPE_EVS] = {6, ~0},
[PW_TYPE_INTEGER64] = {8, 8},
[PW_TYPE_IPV4_PREFIX] = {6, 6},
[PW_TYPE_VSA] = {4, ~0},
[PW_TYPE_VENDOR] = {0, 0}
};
/*
* Create the hash of the name.
*
* We copy the hash function here because it's substantially faster.
*/
#define FNV_MAGIC_INIT (0x811c9dc5)
#define FNV_MAGIC_PRIME (0x01000193)
static uint32_t dict_hashname(char const *name)
{
uint32_t hash = FNV_MAGIC_INIT;
char const *p;
for (p = name; *p != '\0'; p++) {
int c = *(unsigned char const *)p;
if (isalpha(c)) c = tolower(c);
hash *= FNV_MAGIC_PRIME;
hash ^= (uint32_t)(c & 0xff);
}
return hash;
}
/*
* Hash callback functions.
*/
static uint32_t dict_attr_name_hash(void const *data)
{
return dict_hashname(((fr_dict_attr_t const *)data)->name);
}
static int dict_attr_name_cmp(void const *one, void const *two)
{
fr_dict_attr_t const *a = one;
fr_dict_attr_t const *b = two;
return strcasecmp(a->name, b->name);
}
static uint32_t dict_attr_value_hash(void const *data)
{
uint32_t hash;
fr_dict_attr_t const *attr = data;
hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
}
static int dict_attr_value_cmp(void const *one, void const *two)
{
fr_dict_attr_t const *a = one;
fr_dict_attr_t const *b = two;
if (a->vendor < b->vendor) return -1;
if (a->vendor > b->vendor) return +1;
return a->attr - b->attr;
}
static uint32_t dict_attr_combo_hash(void const *data)
{
uint32_t hash;
fr_dict_attr_t const *attr = data;
hash = fr_hash(&attr->vendor, sizeof(attr->vendor));
hash = fr_hash_update(&attr->type, sizeof(attr->type), hash);
return fr_hash_update(&attr->attr, sizeof(attr->attr), hash);
}
static int dict_attr_combo_cmp(void const *one, void const *two)
{
fr_dict_attr_t const *a = one;
fr_dict_attr_t const *b = two;
if (a->type < b->type) return -1;
if (a->type > b->type) return +1;
if (a->vendor < b->vendor) return -1;
if (a->vendor > b->vendor) return +1;
return a->attr - b->attr;
}
static uint32_t dict_vendor_name_hash(void const *data)
{
return dict_hashname(((fr_dict_vendor_t const *)data)->name);
}
static int dict_vendor_name_cmp(void const *one, void const *two)
{
fr_dict_vendor_t const *a = one;
fr_dict_vendor_t const *b = two;
return strcasecmp(a->name, b->name);
}
static uint32_t dict_vendor_value_hash(void const *data)
{
return fr_hash(&(((fr_dict_vendor_t const *)data)->vendorpec),
sizeof(((fr_dict_vendor_t const *)data)->vendorpec));
}
static int dict_vendor_value_cmp(void const *one, void const *two)
{
fr_dict_vendor_t const *a = one;
fr_dict_vendor_t const *b = two;
return a->vendorpec - b->vendorpec;
}
static uint32_t dict_value_name_hash(void const *data)
{
uint32_t hash;
fr_dict_value_t const *dval = data;
hash = dict_hashname(dval->name);
return fr_hash_update(&dval->da, sizeof(dval->da), hash);
}
static int dict_value_name_cmp(void const *one, void const *two)
{
int rcode;
fr_dict_value_t const *a = one;
fr_dict_value_t const *b = two;
rcode = a->da - b->da;
if (rcode != 0) return rcode;
return strcasecmp(a->name, b->name);
}
static uint32_t dict_value_value_hash(void const *data)
{
uint32_t hash = 0;
fr_dict_value_t const *dval = data;
hash = fr_hash_update(&dval->da, sizeof(dval->da), hash);
return fr_hash_update(&dval->value, sizeof(dval->value), hash);
}
static int dict_value_value_cmp(void const *one, void const *two)
{
int rcode;
fr_dict_value_t const *a = one;
fr_dict_value_t const *b = two;
rcode = a->da - b->da;
if (rcode != 0) return rcode;
return a->value - b->value;
}
/*
* Free the list of stat buffers
*/
static void dict_stat_free(void)
{
dict_stat_t *this, *next;
if (!stat_head) {
stat_tail = NULL;
return;
}
for (this = stat_head; this != NULL; this = next) {
next = this->next;
free(this);
}
stat_head = stat_tail = NULL;
}
/*
* Add an entry to the list of stat buffers.
*/
static void dict_stat_add(struct stat const *stat_buf)
{
dict_stat_t *this;
this = malloc(sizeof(*this));
if (!this) return;
memset(this, 0, sizeof(*this));
memcpy(&(this->stat_buf), stat_buf, sizeof(this->stat_buf));
if (!stat_head) {
stat_head = stat_tail = this;
} else {
stat_tail->next = this;
stat_tail = this;
}
}
/*
* See if any dictionaries have changed. If not, don't
* do anything.
*/
static int dict_stat_check(char const *dir, char const *file)
{
struct stat stat_buf;
dict_stat_t *this;
char buffer[2048];
/*
* Nothing cached, all files are new.
*/
if (!stat_head) return 0;
/*
* Stat the file.
*/
snprintf(buffer, sizeof(buffer), "%s/%s", dir, file);
if (stat(buffer, &stat_buf) < 0) return 0;
/*
* Find the cache entry.
* FIXME: use a hash table.
* FIXME: check dependencies, via children.
* if A loads B and B changes, we probably want
* to reload B at the minimum.
*/
for (this = stat_head; this != NULL; this = this->next) {
if (this->stat_buf.st_dev != stat_buf.st_dev) continue;
if (this->stat_buf.st_ino != stat_buf.st_ino) continue;
/*
* The file has changed. Re-read it.
*/
if (this->stat_buf.st_mtime < stat_buf.st_mtime) return 0;
/*
* The file is the same. Ignore it.
*/
return 1;
}
/*
* Not in the cache.
*/
return 0;
}
/*
* Free the dictionary_attributes and dictionary_values lists.
*/
static int _fr_dict_free(UNUSED fr_dict_t *dict)
{
dict_stat_free(); /* Fixme - should be in the same struct as dict */
return 0;
}
const int fr_dict_attr_allowed_chars[256] = {
/* 0x 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/* 0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 1 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 2 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
/* 3 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
/* 4 */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 5 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
/* 6 */ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/* 7 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
/* 8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 9 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* a */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* b */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* c */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* d */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* e */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/*
* [a-zA-Z0-9_-:.]+
*/
int fr_dict_valid_name(char const *name)
{
uint8_t const *p;
for (p = (uint8_t const *)name; *p != '\0'; p++) {
if (!fr_dict_attr_allowed_chars[*p]) {
char buff[5];
fr_snprint(buff, sizeof(buff), (char const *)p, 1, '\'');
fr_strerror_printf("Invalid character '%s' in attribute", buff);
return -(p - (uint8_t const *)name);
}
}
return 0;
}
static void fr_dict_snprint_flags(char *out, size_t outlen, ATTR_FLAGS flags)
{
char *p = out, *end = p + outlen;
size_t len;
out[0] = '\0';
#define FLAG_SET(_flag) \
do { \
if (flags._flag) {\
p += strlcpy(p, STRINGIFY(_flag)",", end - p);\
if (p >= end) return;\
}\
} while (0)
FLAG_SET(is_root);
FLAG_SET(is_unknown);
FLAG_SET(internal);
FLAG_SET(has_tag);
FLAG_SET(array);
FLAG_SET(has_value);
FLAG_SET(has_value_alias);
FLAG_SET(extended);
FLAG_SET(long_extended);
FLAG_SET(evs);
FLAG_SET(wimax);
FLAG_SET(concat);
FLAG_SET(is_pointer);
FLAG_SET(virtual);
FLAG_SET(compare);
if (flags.encrypt) {
p += snprintf(p, end - p, "encrypt=%i,", flags.encrypt);
if (p >= end) return;
}
if (flags.length) {
p += snprintf(p, end - p, "length=%i,", flags.length);
if (p >= end) return;
}
if (!out[0]) return;
/*
* Trim the comma
*/
len = strlen(out);
if (out[len - 1] == ',') out[len - 1] = '\0';
}
void fr_dict_print(fr_dict_attr_t const *da, int depth)
{
char buff[256];
unsigned int i;
char const *name;
fr_dict_snprint_flags(buff, sizeof(buff), da->flags);
switch (da->type) {
case PW_TYPE_VSA:
name = "VSA";
break;
case PW_TYPE_EXTENDED:
name = "EXTENDED";
break;
case PW_TYPE_TLV:
name = "TLV";
break;
case PW_TYPE_EVS:
name = "EVS";
break;
case PW_TYPE_VENDOR:
name = "VENDOR";
break;
case PW_TYPE_LONG_EXTENDED:
name = "LONG EXTENDED";
break;
default:
name = "ATTRIBUTE";
break;
}
printf("%i%.*s%s \"%s\" vendor: %x (%i), num: %x (%i), type: %s, flags: %s\n", da->depth, depth,
"\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t", name, da->name,
da->vendor, da->vendor, da->attr, da->attr,
fr_int2str(dict_attr_types, da->type, "?Unknown?"), buff);
if (da->children) for (i = 0; i < talloc_array_length(da->children); i++) {
if (da->children[i]) {
fr_dict_attr_t const *bin;
for (bin = da->children[i]; bin; bin = bin->next) fr_dict_print(bin, depth + 1);
}
}
}
/** Find a common ancestor that two TLV type attributes share
*
* @param a first TLV attribute.
* @param b second TLV attribute.
* @return
* - Common ancestor if one exists.
* - NULL if no common ancestor exists.
*/
fr_dict_attr_t const *fr_dict_parent_common(fr_dict_attr_t const *a, fr_dict_attr_t const *b)
{
unsigned int i;
fr_dict_attr_t const *p_a, *p_b;
if (!a || !b) return NULL;
if (!a->parent || !b->parent) return NULL; /* Either are at the root */
/*
* Find a common depth to work back from
*/
if (a->depth > b->depth) {
p_b = b;
for (p_a = a, i = a->depth - b->depth; p_a && i; p_a = p_a->parent, i++);
} else if (a->depth < b->depth) {
p_a = a;
for (p_b = b, i = b->depth - a->depth; p_b && i; p_b = p_b->parent, i++);
} else {
p_a = a;
p_b = b;
}
while (p_a && p_b) {
if (p_a == p_b) return p_a;
p_a = p_a->parent;
p_b = p_b->parent;
}
return NULL;
}
/** Add a child to a parent.
*
* @param parent we're adding a child to.
* @param child to add to parent.
* @return
* - 0 on success.
* - -1 on failure (memory allocation error).
*/
static inline int fr_dict_attr_child_add(fr_dict_attr_t *parent, fr_dict_attr_t *child)
{
fr_dict_attr_t const * const *bin;
fr_dict_attr_t **this;
/*
* Setup fields in the child
*/
child->parent = parent;
child->depth = parent->depth + 1;
/*
* We only allocate the pointer array *if* the parent has children.
*/
if (!parent->children) parent->children = talloc_zero_array(parent, fr_dict_attr_t const *, UINT8_MAX + 1);
if (!parent->children) return -1;
/*
* Treat the array as a hash of 255 bins, with attributes
* sorted into bins using num % 255.
*
* Although the various protocols may define numbers higher than 255:
*
* RADIUS/DHCPv4 - 1-255
* Diameter/Internal - 1-4294967295
* DHCPv6 - 1-65535
*
* In reality very few will ever use attribute numbers > 500, so for
* the majority of lookups we get O(1) performance.
*
* Attributes are inserted into the bin in order of their attribute
* numbers to allow slightly more efficient lookups.
*/
bin = &parent->children[child->attr & 0xff];
for (;;) {
bool child_is_struct = false;
bool bin_is_struct = false;
if (!*bin) break;
/*
* Workaround for vendors that overload the RFC space.
* Structural attributes always take priority.
*/
switch (child->type) {
case PW_TYPE_STRUCTURAL:
child_is_struct = true;
break;
default:
break;
}
switch ((*bin)->type) {
case PW_TYPE_STRUCTURAL:
bin_is_struct = true;
break;
default:
break;
}
if (child_is_struct && !bin_is_struct) break;
else if (child->vendor <= (*bin)->vendor) break; /* Prioritise RFC attributes */
else if (child->attr <= (*bin)->attr) break;
bin = &(*bin)->next;
}
memcpy(&this, &bin, sizeof(this));
child->next = *this;
*this = child;
return 0;
}
/** Check if a child attribute exists in a parent using an attribute number
*
* @param parent to check for child in.
* @param attr number to look for.
* @return
* - The child attribute on success.
* - NULL if the child attribute does not exist.
*/
fr_dict_attr_t const *fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
{
fr_dict_attr_t const *bin;
if (!parent->children) return NULL;
/*
* Only some types can have children
*/
switch (parent->type) {
default:
return NULL;
case PW_TYPE_STRUCTURAL:
break;
}
/*
* Child arrays may be trimmed back to save memory.
* Check that so we don't SEGV.
*/
if ((attr & 0xff) > talloc_array_length(parent->children)) return NULL;
bin = parent->children[attr & 0xff];
for (;;) {
if (!bin) return NULL;
if (bin->attr == attr) return bin;
bin = bin->next;
}
return NULL;
}
/** Check if a child attribute exists in a parent using a pointer (da)
*
* @param parent to check for child in.
* @param child to look for.
* @return
* - The child attribute on success.
* - NULL if the child attribute does not exist.
*/
fr_dict_attr_t const *fr_dict_attr_child_by_da(fr_dict_attr_t const *parent, fr_dict_attr_t const *child)
{
fr_dict_attr_t const *bin;
if (!parent->children) return NULL;
/*
* Only some types can have children
*/
switch (parent->type) {
default:
return NULL;
case PW_TYPE_STRUCTURAL:
break;
}
/*
* Child arrays may be trimmed back to save memory.
* Check that so we don't SEGV.
*/
if ((child->attr & 0xff) > talloc_array_length(parent->children)) return NULL;
bin = parent->children[child->attr & 0xff];
for (;;) {
if (!bin) return NULL;
if (bin == child) return bin;
bin = bin->next;
}
return NULL;
}
static fr_dict_attr_t *fr_dict_attr_alloc(TALLOC_CTX *ctx,
char const *name, unsigned int vendor, int attr,
PW_TYPE type, ATTR_FLAGS flags)
{
fr_dict_attr_t *da;
size_t namelen = strlen(name);
da = (fr_dict_attr_t *)talloc_zero_array(ctx, uint8_t, sizeof(*da) + namelen);
if (!da) {
fr_strerror_printf("Out of memory");
return NULL;
}
talloc_set_type(da, fr_dict_attr_t);
memcpy(da->name, name, namelen);
da->name[namelen] = '\0';
da->attr = attr;
da->vendor = vendor;
da->type = type;
da->flags = flags;
return da;
}
/** Process a single OID component
*
* @param[out] out Value of component.
* @param[in] oid string to parse.
* @return
* - 0 on success.
* - -1 on format error.
*/
static int fr_dict_oid_component(unsigned int *out, char const **oid)
{
char const *p = *oid;
char *q;
unsigned long num;
*out = 0;
num = strtoul(p, &q, 10);
if (p == q) {
fr_strerror_printf("Invalid OID component %lu", num);
return -1;
}
switch (*q) {
case '\0':
case '.':
*oid = q;
*out = (unsigned int)num;
return 0;
default:
fr_strerror_printf("Unexpected text after OID component");
*out = 0;
return -1;
}
}
/** Get the leaf attribute of an OID string
*
* @param[out] attr Number we parsed.
* @param[in,out] vendor number of attribute.
* @param[in,out] parent attribute (or root of dictionary). Will be updated to the parent
* directly beneath the leaf.
* @param[in] oid string to parse.
* @return
* - > 0 on success (number of bytes parsed).
* - <= 0 on parse error (negative offset of parse error).
*/
ssize_t fr_dict_str_to_oid(unsigned int *vendor, unsigned int *attr, fr_dict_attr_t const **parent, char const *oid)
{
char const *p = oid;
unsigned int num = 0;
ssize_t slen;
if (!fr_assert(parent)) return 0;
*attr = 0;
if (fr_dict_oid_component(&num, &p) < 0) return oid - p;
/*
* Look for 26.VID.x.y
*
* This allows us to specify a VSA if our parent is the root
* of the dictionary, and we're operating outside of a vendor
* block.
*
* The additional code is because we need at least three components
* the VSA attribute (26), the vendor ID, and actual attribute.
*/
if (((*parent)->flags.is_root) && !*vendor && (num == PW_VENDOR_SPECIFIC)) {
fr_dict_vendor_t const *dv;
if (p[0] == '\0') {
fr_strerror_printf("Vendor attribute must specify a VID");
return oid - p;
}
p++;
if (fr_dict_oid_component(&num, &p) < 0) return oid - p;
if (p[0] == '\0') {
fr_strerror_printf("Vendor attribute must specify a child");
return oid - p;
}
p++;
if (num >= FR_MAX_VENDOR) {
fr_strerror_printf("Cannot handle vendor ID larger than 2^24");
return oid - p;
}
dv = fr_dict_vendor_by_num(num);
if (!dv) {
fr_strerror_printf("Unknown vendor \"%u\" ", num);
return oid - p;
}
/*
* Recurse to get the attribute.
*/
slen = fr_dict_str_to_oid(vendor, attr, parent, p);
if (slen <= 0) return slen + (oid - p);
slen += p - oid;
if (slen > 0) *vendor = dv->vendorpec; /* Record vendor number */
return slen;
}
/*
* If it's not a vendor type, it must be between 0-255
*/
if (((*parent)->type != PW_TYPE_VENDOR) && ((num == 0) || (num > UINT8_MAX))) {
fr_strerror_printf("TLV attributes must be between 1-255 inclusive");
return oid - p;
}
switch (p[0]) {
/*
* We've not hit the leaf yet, so the attribute must be
* defined already.
*/
case '.':
{
fr_dict_attr_t const *child;
p++;
child = fr_dict_attr_child_by_num(*parent, num);
if (!child) {
fr_strerror_printf("Parent attribute for %s not defined", oid);
return 0;
}
*parent = child;
slen = fr_dict_str_to_oid(vendor, attr, parent, p);
if (slen <= 0) return slen + (oid - p);
slen += p - oid;
return slen;
}
/*
* Hit the leaf, this is the attribute we need to define.
*/
case '\0':
*attr = num;
return p - oid;
default:
fr_strerror_printf("Malformed OID string, got trailing garbage \"%s\"", p);
return oid - p;
}
}
/*
* Add vendor to the list.
*/
int fr_dict_vendor_add(char const *name, unsigned int num)
{
size_t length;
fr_dict_vendor_t *dv;
if (num >= FR_MAX_VENDOR) {
fr_strerror_printf("fr_dict_vendor_add: Cannot handle vendor ID larger than 2^24");
return -1;
}
if ((length = strlen(name)) >= FR_DICT_VENDOR_MAX_NAME_LEN) {
fr_strerror_printf("fr_dict_vendor_add: vendor name too long");
return -1;
}
dv = (fr_dict_vendor_t *)talloc_zero_array(fr_main_dict->pool, uint8_t, sizeof(*dv) + length);
if (dv == NULL) {
fr_strerror_printf("fr_dict_vendor_add: out of memory");
return -1;
}
talloc_set_type(dv, fr_dict_vendor_t);
strcpy(dv->name, name);
dv->vendorpec = num;
dv->type = dv->length = 1; /* defaults */
if (!fr_hash_table_insert(fr_main_dict->vendors_by_name, dv)) {
fr_dict_vendor_t *old_dv;
old_dv = fr_hash_table_finddata(fr_main_dict->vendors_by_name, dv);
if (!old_dv) {
fr_strerror_printf("fr_dict_vendor_add: Failed inserting vendor name %s", name);
return -1;
}
if (old_dv->vendorpec != dv->vendorpec) {
fr_strerror_printf("fr_dict_vendor_add: Duplicate vendor name %s", name);
return -1;
}
/*
* Already inserted. Discard the duplicate entry.
*/
talloc_free(dv);
return 0;
}
/*
* Insert the SAME pointer (not free'd when this table is
* deleted), into another table.
*
* We want this behaviour because we want OLD names for
* the attributes to be read from the configuration
* files, but when we're printing them, (and looking up
* by value) we want to use the NEW name.
*/
if (!fr_hash_table_replace(fr_main_dict->vendors_by_num, dv)) {
fr_strerror_printf("fr_dict_vendor_add: Failed inserting vendor %s",
name);
return -1;
}
return 0;
}