/
dict_tokenize.c
2562 lines (2144 loc) · 62 KB
/
dict_tokenize.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/** Parse dictionary files
*
* @file src/lib/util/dict_tokenize.c
*
* @copyright 2019 The FreeRADIUS server project
*/
RCSID("$Id$")
#include <freeradius-devel/util/dict_priv.h>
#include <freeradius-devel/util/syserror.h>
#include <freeradius-devel/util/rand.h>
#include <freeradius-devel/util/talloc.h>
#include <freeradius-devel/util/conf.h>
#include <freeradius-devel/radius/defs.h>
#include <sys/stat.h>
#include <ctype.h>
typedef struct dict_enum_fixup_s dict_enum_fixup_t;
#define MAX_ARGV (16)
/** A temporary enum value, which we'll resolve later
*
*/
struct dict_enum_fixup_s {
char *filename; //!< where the "enum" was defined
int line; //!< ditto
char *attribute; //!< we couldn't find (and will need to resolve later).
char *name; //!< Raw enum name.
char *value; //!< Raw enum value. We can't do anything with this until
//!< we know the attribute type, which we only find out later.
dict_enum_fixup_t *next; //!< Next in the linked list of fixups.
};
typedef struct dict_group_fixup_s dict_group_fixup_t;
/** A temporary group reference, which we'll resolve later
*
*/
struct dict_group_fixup_s {
char *filename; //!< where the "group" was defined
int line; //!< ditto
fr_dict_attr_t *da; //!< FR_TYPE_GROUP to fix
char *ref; //!< the reference name
dict_group_fixup_t *next; //!< Next in the linked list of fixups.
};
/** Parser context for dict_from_file
*
* Allows vendor and TLV context to persist across $INCLUDEs
*/
#define MAX_STACK (32)
typedef struct {
fr_dict_t *dict; //!< The dictionary before the current BEGIN-PROTOCOL block.
char *filename; //!< name of the file we're reading
int line; //!< line number of this file
fr_dict_attr_t const *da; //!< the da we care about
fr_type_t nest; //!< for manual vs automatic begin / end things
int member_num; //!< structure member numbers
} dict_tokenize_frame_t;
typedef struct {
fr_dict_t *dict; //!< Protocol dictionary we're inserting attributes into.
dict_tokenize_frame_t stack[MAX_STACK]; //!< stack of attributes to track
int stack_depth; //!< points to the last used stack frame
fr_dict_attr_t *value_attr; //!< Cache of last attribute to speed up
///< value processing.
fr_dict_attr_t const *relative_attr; //!< for ".82" instead of "1.2.3.82".
///< only for parents of type "tlv"
TALLOC_CTX *fixup_pool; //!< Temporary pool for fixups, reduces holes
dict_enum_fixup_t *enum_fixup;
dict_group_fixup_t *group_fixup;
} dict_tokenize_ctx_t;
/*
* String split routine. Splits an input string IN PLACE
* into pieces, based on spaces.
*/
int fr_dict_str_to_argv(char *str, char **argv, int max_argc)
{
int argc = 0;
while (*str) {
if (argc >= max_argc) break;
/*
* Chop out comments early.
*/
if (*str == '#') {
*str = '\0';
break;
}
while ((*str == ' ') ||
(*str == '\t') ||
(*str == '\r') ||
(*str == '\n'))
*(str++) = '\0';
if (!*str) break;
argv[argc] = str;
argc++;
while (*str &&
(*str != ' ') &&
(*str != '\t') &&
(*str != '\r') &&
(*str != '\n'))
str++;
}
return argc;
}
static int dict_read_sscanf_i(unsigned int *pvalue, char const *str)
{
int rcode = 0;
int base = 10;
static char const *tab = "0123456789";
if ((str[0] == '0') &&
((str[1] == 'x') || (str[1] == 'X'))) {
tab = "0123456789abcdef";
base = 16;
str += 2;
}
while (*str) {
char const *c;
if (*str == '.') break;
c = memchr(tab, tolower((int)*str), base);
if (!c) return 0;
rcode *= base;
rcode += (c - tab);
str++;
}
*pvalue = rcode;
return 1;
}
/** Set a new root dictionary attribute
*
* @note Must only be called once per dictionary.
*
* @param[in] dict to modify.
* @param[in] name of dictionary root.
* @param[in] proto_number The artificial (or IANA allocated) number for the protocol.
* This is only used for
* @return
* - 0 on success.
* - -1 on failure.
*/
static int dict_root_set(fr_dict_t *dict, char const *name, unsigned int proto_number)
{
fr_dict_attr_flags_t flags = {
.is_root = 1,
.type_size = 1,
.length = 1
};
if (!fr_cond_assert(!dict->root)) {
fr_strerror_printf("Dictionary root already set");
return -1;
}
dict->root = dict_attr_alloc_name(dict, name);
if (!dict->root) return -1;
dict_attr_init(dict->root, NULL, proto_number, FR_TYPE_TLV, &flags);
DA_VERIFY(dict->root);
return 0;
}
static int dict_process_type_field(dict_tokenize_ctx_t *ctx, char const *name, fr_type_t *type_p,
fr_dict_attr_flags_t *flags)
{
char *p;
fr_type_t type;
/*
* Some types can have fixed length
*/
p = strchr(name, '[');
if (p) {
char *q;
unsigned int length;
*p = '\0';
q = strchr(p + 1, ']');
if (!q) {
fr_strerror_printf("Invalid format for '%s[...]'", name);
return -1;
}
*q = '\0';
if (!dict_read_sscanf_i(&length, p + 1)) {
fr_strerror_printf("Invalid length for '%s[...]'", name);
return -1;
}
if ((length == 0) || (length > 253)) {
fr_strerror_printf("Invalid length for '%s[...]'", name);
return -1;
}
/*
* Now that we have a length, check the data type.
*/
if (strcmp(name, "octets") == 0) {
type = FR_TYPE_OCTETS;
} else if (strcmp(name, "bit") == 0) {
if (ctx->stack[ctx->stack_depth].da->type != FR_TYPE_STRUCT) {
fr_strerror_printf("Bit fields can only be used inside of a STRUCT");
return -1;
}
flags->extra = 1;
flags->subtype = FLAG_BIT_FIELD;
if (length == 1) {
type = FR_TYPE_BOOL;
} else if (length < 8) {
type = FR_TYPE_UINT8;
} else if (length < 16) {
type = FR_TYPE_UINT16;
} else if (length < 32) {
type = FR_TYPE_UINT32;
} else if (length < 56) { /* for laziness in encode / decode */
type = FR_TYPE_UINT64;
} else {
fr_strerror_printf("Invalid length for bit field");
return -1;
}
/*
* We track where on a byte boundary this bit field ends.
*/
flags->type_size = length;
} else {
fr_strerror_printf("Only 'octets' types can have a 'length' parameter");
return -1;
}
flags->length = length;
*type_p = type;
return 0;
}
/*
* find the type of the attribute.
*/
type = fr_table_value_by_str(fr_value_box_type_table, name, FR_TYPE_INVALID);
if (type == FR_TYPE_INVALID) {
fr_strerror_printf("Unknown data type '%s'", name);
return -1;
}
*type_p = type;
return 0;
}
static int dict_process_flag_field(dict_tokenize_ctx_t *ctx, char *name, fr_type_t type, fr_dict_attr_flags_t *flags,
char **ref)
{
char *p, *next = NULL;
if (ref) *ref = NULL;
for (p = name; p && *p != '\0' ; p = next) {
char *key, *value;
key = p;
/*
* Search for the first '=' or ','
*/
for (next = p + 1; *next && (*next != '=') && (*next != ','); next++) {
/* do nothing */
}
/*
* We have a value, zero out the '=' and point to the value.
*/
if (*next == '=') {
*(next++) = '\0';
value = next;
if (!*value || (*value == ',')) {
fr_strerror_printf("Missing value after '%s='", key);
return -1;
}
} else {
value = NULL;
}
/*
* Skip any trailing text in the value.
*/
for (/* nothing */; *next; next++) {
if (*next == ',') {
*(next++) = '\0';
break;
}
}
/*
* Boolean flag, means this is a tagged
* attribute.
*/
if (strcmp(key, "has_tag") == 0) {
if ((type != FR_TYPE_UINT32) && (type != FR_TYPE_STRING)) {
fr_strerror_printf("The 'has_tag' flag can only be used for attributes of type 'integer' "
"or 'string'");
return -1;
}
flags->has_tag = 1;
/*
* Marks the attribute up as internal.
* This means it can use numbers outside of the allowed
* protocol range, and also means it will not be included
* in replies or proxy requests.
*/
} else if (strcmp(key, "internal") == 0) {
flags->internal = 1;
} else if (strcmp(key, "array") == 0) {
flags->array = 1;
} else if (strcmp(key, "concat") == 0) {
if (type != FR_TYPE_OCTETS) {
fr_strerror_printf("The 'concat' flag can only be used for attributes of type 'octets'");
return -1;
}
flags->concat = 1;
} else if (strcmp(key, "virtual") == 0) {
flags->virtual = 1;
} else if (strcmp(key, "key") == 0) {
if ((type != FR_TYPE_UINT8) && (type != FR_TYPE_UINT16) && (type != FR_TYPE_UINT32)) {
fr_strerror_printf("The 'key' flag can only be used for attributes of type 'uint8', 'uint16', or 'uint32'");
return -1;
}
if (flags->extra) {
fr_strerror_printf("Bit fields cannot be key fields");
return -1;
}
flags->extra = 1;
flags->subtype = FLAG_KEY_FIELD;
} else if (strcmp(key, "length") == 0) {
if (!value || (strcmp(value, "uint16") != 0)) {
fr_strerror_printf("The 'length' flag can only be used with value 'uint16'");
}
flags->extra = 1;
flags->subtype = FLAG_LENGTH_UINT16;
} else if ((type == FR_TYPE_DATE) || (type == FR_TYPE_TIME_DELTA)) {
flags->length = 4;
flags->type_size = FR_TIME_RES_SEC;
if (strncmp(key, "uint", 4) == 0) {
fr_type_t subtype;
subtype = fr_table_value_by_str(fr_value_box_type_table, name, FR_TYPE_INVALID);
if (subtype == FR_TYPE_INVALID) {
unknown_type:
fr_strerror_printf("Unknown or unsupported %s type '%s'",
fr_table_str_by_value(fr_value_box_type_table, type, "<UNKNOWN>"),
key);
return -1;
}
switch (subtype) {
default:
goto unknown_type;
case FR_TYPE_INT16:
if (type == FR_TYPE_DATE) goto unknown_type;
/* FALL-THROUGH */
case FR_TYPE_UINT16:
flags->length = 2;
break;
case FR_TYPE_INT32:
if (type == FR_TYPE_DATE) goto unknown_type;
/* FALL-THROUGH */
case FR_TYPE_UINT32:
flags->length = 4;
break;
case FR_TYPE_INT64:
if (type == FR_TYPE_DATE) goto unknown_type;
/* FALL-THROUGH */
case FR_TYPE_UINT64:
flags->length = 8;
break;
}
} else {
int precision;
precision = fr_table_value_by_str(date_precision_table, key, -1);
if (precision < 0) {
fr_strerror_printf("Unknown %s precision '%s'",
fr_table_str_by_value(fr_value_box_type_table, type, "<UNKNOWN>"),
key);
return -1;
}
flags->type_size = precision;
}
} else if (strcmp(key, "ref") == 0) {
if (!ref || (type != FR_TYPE_GROUP)) {
fr_strerror_printf("The 'ref' flag can only be used for attributes of type 'group'");
return -1;
}
*ref = talloc_strdup(ctx->dict->pool, value);
} else if (ctx->dict->subtype_table) {
int subtype;
if (value) value[-1] = '='; /* hackity hack */
subtype = fr_table_value_by_str(ctx->dict->subtype_table, key, -1);
if (subtype < 0) goto unknown_option;
flags->subtype = subtype;
} else {
unknown_option:
fr_strerror_printf("Unknown option '%s'", key);
return -1;
}
}
/*
* Check that the flags are valid.
*/
if (!dict_attr_flags_valid(ctx->dict, ctx->stack[ctx->stack_depth].da, name, NULL, type, flags)) return -1;
return 0;
}
static int dict_gctx_push(dict_tokenize_ctx_t *ctx, fr_dict_attr_t const *da)
{
if ((ctx->stack_depth + 1) >= MAX_STACK) {
fr_strerror_printf_push("Attribute definitions are nested too deep.");
return -1;
}
ctx->stack_depth++;
memset(&ctx->stack[ctx->stack_depth], 0, sizeof(ctx->stack[ctx->stack_depth]));
ctx->stack[ctx->stack_depth].dict = ctx->stack[ctx->stack_depth - 1].dict;
ctx->stack[ctx->stack_depth].da = da;
ctx->stack[ctx->stack_depth].filename = ctx->stack[ctx->stack_depth - 1].filename;
ctx->stack[ctx->stack_depth].line = ctx->stack[ctx->stack_depth - 1].line;
return 0;
}
static fr_dict_attr_t const *dict_gctx_unwind(dict_tokenize_ctx_t *ctx)
{
while ((ctx->stack_depth > 0) &&
(ctx->stack[ctx->stack_depth].nest == FR_TYPE_INVALID)) {
ctx->stack_depth--;
}
return ctx->stack[ctx->stack_depth].da;
}
/*
* Process the ATTRIBUTE command
*/
static int dict_read_process_attribute(dict_tokenize_ctx_t *ctx, char **argv, int argc,
fr_dict_attr_flags_t *base_flags)
{
bool set_relative_attr = true;
ssize_t slen;
unsigned int attr;
fr_type_t type;
fr_dict_attr_flags_t flags;
fr_dict_attr_t const *parent, *da;
char *ref = NULL;
if ((argc < 3) || (argc > 4)) {
fr_strerror_printf("Invalid ATTRIBUTE syntax");
return -1;
}
/*
* Dictionaries need to have real names, not shitty ones.
*/
if (strncmp(argv[0], "Attr-", 5) == 0) {
fr_strerror_printf("Invalid ATTRIBUTE name");
return -1;
}
memcpy(&flags, base_flags, sizeof(flags));
if (dict_process_type_field(ctx, argv[2], &type, &flags) < 0) return -1;
if (flags.extra && (flags.subtype == FLAG_BIT_FIELD)) {
fr_strerror_printf("Bit fields can only be defined as a MEMBER of a STRUCT");
return -1;
}
/*
* Relative OIDs apply ONLY to attributes of type 'tlv'.
*/
if (type != FR_TYPE_TLV) set_relative_attr = false;
/*
* A non-relative ATTRIBUTE definition means that it is
* in the context of the previous BEGIN-FOO. So we
* unwind the stack to match.
*/
if (argv[1][0] != '.') {
parent = dict_gctx_unwind(ctx);
/*
* Allow '0xff00' as attribute numbers, but only
* if there is no OID component.
*/
if (strchr(argv[1], '.') == 0) {
if (!dict_read_sscanf_i(&attr, argv[1])) {
fr_strerror_printf("Invalid ATTRIBUTE number");
return -1;
}
} else {
slen = fr_dict_attr_by_oid(ctx->dict, &parent, &attr, argv[1]);
if (slen <= 0) return -1;
}
} else {
if (!ctx->relative_attr) {
fr_strerror_printf("Unknown parent for partial OID");
return -1;
}
parent = ctx->relative_attr;
set_relative_attr = false;
slen = fr_dict_attr_by_oid(ctx->dict, &parent, &attr, argv[1]);
if (slen <= 0) return -1;
}
if (!fr_cond_assert(parent)) return -1; /* Should have provided us with a parent */
/*
* Members of a 'struct' MUST use MEMBER, not ATTRIBUTE.
*/
if (parent->type == FR_TYPE_STRUCT) {
fr_strerror_printf("Member %s of ATTRIBUTE %s type 'struct' MUST use the \"MEMBER\" keyword",
argv[0], parent->name);
return -1;
}
/*
* Parse options.
*/
if ((argc >= 4) && (dict_process_flag_field(ctx, argv[3], type, &flags, &ref) < 0)) return -1;
#ifdef WITH_DICTIONARY_WARNINGS
/*
* Hack to help us discover which vendors have illegal
* attributes.
*/
if (!vendor && (attr < 256) &&
!strstr(fn, "rfc") && !strstr(fn, "illegal")) {
fprintf(stderr, "WARNING: Illegal Attribute %s in %s\n",
argv[0], fn);
}
#endif
#ifdef __clang_analyzer__
if (!ctx->dict) return -1;
#endif
/*
* Dynamically define where VSAs go. Note that we CANNOT
* define VSAs until we define an attribute of type VSA!
*/
if ((type == FR_TYPE_VSA) && (parent->flags.is_root)) {
ctx->dict->vsa_parent = attr;
}
/*
* Add in an attribute
*/
if (fr_dict_attr_add(ctx->dict, parent, argv[0], attr, type, &flags) < 0) return -1;
/*
* If we need to set the previous attribute, we have to
* look it up by number. This lets us set the
* *canonical* previous attribute, and not any potential
* duplicate which was just added.
*/
da = dict_attr_child_by_num(parent, attr);
if (!da) {
fr_strerror_printf("Failed to find attribute '%s' we just added.", argv[0]);
return -1;
}
if (set_relative_attr) ctx->relative_attr = da;
/*
* Hack up groups according to "ref"
*/
if (type == FR_TYPE_GROUP) {
fr_dict_attr_t *self;
fr_dict_t const *dict;
char *p;
memcpy(&self, &da, sizeof(self)); /* const issues */
/*
* No qualifiers, just point it to the root of the current dictionary.
*/
if (!ref) {
dict = ctx->dict;
da = ctx->dict->root;
goto check;
}
da = dict_attr_by_name(ctx->dict, ref);
if (da) {
dict = ctx->dict;
goto check;
}
/*
* The attribute doesn't exist, and the reference
* is FOO, it might be just a ref to a
* dictionary.
*/
p = strchr(ref, '.');
if (!p) goto save;
/*
* Get / skip protocol name.
*/
slen = fr_dict_by_protocol_substr(&dict, ref, ctx->dict);
if (slen < 0) {
talloc_free(ref);
return -1;
}
/*
* No known dictionary, so we're asked to just
* use the whole string. Which we did above. So
* either it's a bad ref, OR it's a ref to a
* dictionary which doesn't exist.
*/
if (slen == 0) {
dict_group_fixup_t *fixup;
save:
fixup = talloc_zero(ctx->fixup_pool, dict_group_fixup_t);
if (!fixup) {
oom:
talloc_free(ref);
return -1;
}
fixup->filename = talloc_strdup(fixup, ctx->stack[ctx->stack_depth].filename);
if (!fixup->filename) goto oom;
fixup->line = ctx->stack[ctx->stack_depth].line;
fixup->da = self;
fixup->ref = ref;
/*
* Insert to the head of the list.
*/
fixup->next = ctx->group_fixup;
ctx->group_fixup = fixup;
} else if (ref[slen] == '\0') {
da = dict->root;
goto check;
} else {
/*
* Look up the attribute.
*/
da = dict_attr_by_name(dict, ref + slen);
if (!da) {
fr_strerror_printf("protocol loaded, but no attribute '%s'", ref + slen);
talloc_free(ref);
return -1;
}
check:
if (da->type != FR_TYPE_TLV) {
fr_strerror_printf("References MUST be to attributes of type 'tlv'");
talloc_free(ref);
return -1;
}
talloc_free(ref);
self->dict = dict;
self->ref = da;
}
}
/*
* Adding an attribute of type 'struct' is an implicit
* BEGIN-STRUCT.
*/
if (type == FR_TYPE_STRUCT) {
if (dict_gctx_push(ctx, da) < 0) return -1;
}
return 0;
}
/*
* Process the MEMBER command
*/
static int dict_read_process_member(dict_tokenize_ctx_t *ctx, char **argv, int argc,
fr_dict_attr_flags_t *base_flags)
{
fr_type_t type;
fr_dict_attr_flags_t flags;
if ((argc < 2) || (argc > 3)) {
fr_strerror_printf("Invalid MEMBER syntax");
return -1;
}
if (ctx->stack[ctx->stack_depth].da->type != FR_TYPE_STRUCT) {
fr_strerror_printf("MEMBER can only be used for ATTRIBUTEs of type 'struct'");
return -1;
}
/*
* Dictionaries need to have real names, not shitty ones.
*/
if (strncmp(argv[0], "Attr-", 5) == 0) {
fr_strerror_printf("Invalid MEMBER name");
return -1;
}
memcpy(&flags, base_flags, sizeof(flags));
if (dict_process_type_field(ctx, argv[1], &type, &flags) < 0) return -1;
/*
* Parse options.
*/
if ((argc >= 3) && (dict_process_flag_field(ctx, argv[2], type, &flags, NULL) < 0)) return -1;
#ifdef __clang_analyzer__
if (!ctx->dict) return -1;
#endif
/*
* Double check bit field magic
*/
if (ctx->stack[ctx->stack_depth].member_num > 0) {
fr_dict_attr_t const *previous;
previous = dict_attr_child_by_num(ctx->stack[ctx->stack_depth].da,
ctx->stack[ctx->stack_depth].member_num);
if (!previous) {
fr_strerror_printf("Failed to find previous MEMBER");
return -1;
}
/*
* Check that the previous bit field ended on a
* byte boundary.
*/
if (previous->flags.extra && (previous->flags.subtype == FLAG_BIT_FIELD)) {
/*
* This attribute is a bit field. Keep
* track of where in the byte we are
* located.
*/
if (flags.extra && (flags.subtype == FLAG_BIT_FIELD)) {
flags.type_size += previous->flags.type_size;
flags.type_size &= 0x07;
} else {
if (previous->flags.type_size != 0) {
fr_strerror_printf("Previous bitfield %s did not end on a byte boundary",
previous->name);
return -1;
}
}
}
}
/*
* Add the MEMBER to the parent.
*/
if (fr_dict_attr_add(ctx->dict,
ctx->stack[ctx->stack_depth].da,
argv[0],
++ctx->stack[ctx->stack_depth].member_num,
type, &flags) < 0) return -1;
/*
* A 'struct' can have a MEMBER of type 'tlv', but ONLY
* as the last entry in the 'struct'. If we see that,
* set the previous attribute to the TLV we just added.
* This allows the children of the TLV to be parsed as
* partial OIDs, so we don't need to know the full path
* to them.
*/
if (type == FR_TYPE_TLV) {
ctx->relative_attr = dict_attr_child_by_num(ctx->stack[ctx->stack_depth].da,
ctx->stack[ctx->stack_depth].member_num);
if (dict_gctx_push(ctx, ctx->relative_attr) < 0) return -1;
} else {
fr_dict_attr_t *mutable;
/*
* Add the size of this member to the parent struct.
*
* Note that we top out at 256. This value is
* informative, so we don't really care to know the exact
* size. Plus, most of the time it's less than 256.
*/
memcpy(&mutable, &ctx->stack[ctx->stack_depth].da, sizeof(mutable));
if ( ((int) mutable->flags.length + flags.length) >= 256) {
mutable->flags.length = 255;
} else {
mutable->flags.length += flags.length;
}
}
return 0;
}
/** Process a value alias
*
*/
static int dict_read_process_value(dict_tokenize_ctx_t *ctx, char **argv, int argc)
{
fr_dict_attr_t *da;
fr_value_box_t value;
if (argc != 3) {
fr_strerror_printf("Invalid VALUE syntax");
return -1;
}
/*
* Most VALUEs are bunched together by ATTRIBUTE. We can
* save a lot of lookups on dictionary initialization by
* caching the last attribute for a VALUE.
*/
if (!ctx->value_attr || (strcasecmp(argv[0], ctx->value_attr->name) != 0)) {
ctx->value_attr = dict_attr_by_name(ctx->dict, argv[0]);
}
da = ctx->value_attr;
/*
* Remember which attribute is associated with this
* value. This allows us to define enum
* values before the attribute exists, and fix them
* up later.
*/
if (!da) {
dict_enum_fixup_t *fixup;
if (!fr_cond_assert_msg(ctx->fixup_pool, "fixup pool context invalid")) return -1;
fixup = talloc_zero(ctx->fixup_pool, dict_enum_fixup_t);
if (!fixup) {
oom:
talloc_free(fixup);
fr_strerror_printf("Out of memory");
return -1;
}
fixup->filename = talloc_strdup(fixup, ctx->stack[ctx->stack_depth].filename);
if (!fixup->filename) goto oom;
fixup->line = ctx->stack[ctx->stack_depth].line;
fixup->attribute = talloc_strdup(fixup, argv[0]);
if (!fixup->attribute) goto oom;
fixup->name = talloc_strdup(fixup, argv[1]);
if (!fixup->name) goto oom;
fixup->value = talloc_strdup(fixup, argv[2]);
if (!fixup->value) goto oom;
/*
* Insert to the head of the list.
*/
fixup->next = ctx->enum_fixup;
ctx->enum_fixup = fixup;
return 0;
}
/*
* Only a few data types can have VALUEs defined.
*/
switch (da->type) {
case FR_TYPE_ABINARY:
case FR_TYPE_GROUP:
case FR_TYPE_STRUCTURAL:
case FR_TYPE_INVALID:
case FR_TYPE_MAX:
fr_strerror_printf_push("Cannot define VALUE for ATTRIBUTE \"%s\" of data type \"%s\"", da->name,
fr_table_str_by_value(fr_value_box_type_table, da->type, "<INVALID>"));
return -1;
default:
break;
}
{
fr_type_t type = da->type; /* Might change - Stupid combo IP */
if (fr_value_box_from_str(NULL, &value, &type, NULL, argv[2], -1, '\0', false) < 0) {
fr_strerror_printf_push("Invalid VALUE for ATTRIBUTE \"%s\"", da->name);
return -1;
}
}
if (fr_dict_enum_add_name(da, argv[1], &value, false, true) < 0) {
fr_value_box_clear(&value);
return -1;
}
fr_value_box_clear(&value);
return 0;
}
/*
* Process the FLAGS command
*/
static int dict_read_process_flags(UNUSED fr_dict_t *dict, char **argv, int argc,
fr_dict_attr_flags_t *base_flags)
{
bool sense = true;
if (argc == 1) {
char *p;
p = argv[0];
if (*p == '!') {
sense = false;
p++;
}
if (strcmp(p, "internal") == 0) {