The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
dict_tokenize.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Parse dictionary files
18 *
19 * @file src/lib/util/dict_tokenize.c
20 *
21 * @copyright 2019 The FreeRADIUS server project
22 * @copyright 2024 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 */
24RCSID("$Id: 9a49cfd49cd8aad2fc5f30ddb176d8d5a663c23a $")
25
26#include <freeradius-devel/radius/defs.h>
27#include <freeradius-devel/util/conf.h>
28#include <freeradius-devel/util/dict_fixup_priv.h>
29#include <freeradius-devel/util/file.h>
30#include <freeradius-devel/util/rand.h>
31#include <freeradius-devel/util/syserror.h>
32
33#include <sys/stat.h>
34
35/** Maximum number of arguments
36 *
37 * For any one keyword, this is the maxiumum number of arguments that can be passed.
38 */
39#define DICT_MAX_ARGV (8)
40
41/** Maximum stack size
42 *
43 * This is the maximum number of nested BEGIN and $INCLUDE statements.
44 */
45#define DICT_MAX_STACK (32)
46
47/** This represents explicit BEGIN/END frames pushed onto the stack
48 *
49 * These are flags to allow multiple nesting types to be passed to the search function.
50 */
51DIAG_OFF(attributes)
52typedef enum CC_HINT(flag_enum) {
53 NEST_NONE = 0x00,
54 NEST_TOP = 0x01, //!< top of the stack
55 NEST_PROTOCOL = 0x02, //!< BEGIN-PROTOCOL
56 NEST_VENDOR = 0x04, //!< BEGIN-VENDOR
57 NEST_ATTRIBUTE = 0x08 //!< BEGIN foo
59DIAG_ON(attributes)
60
61#define NEST_ANY (NEST_TOP | NEST_PROTOCOL | NEST_VENDOR | NEST_ATTRIBUTE)
62
64 { L("ATTRIBUTE"), NEST_ATTRIBUTE },
65 { L("NONE"), NEST_NONE },
66 { L("PROTOCOL"), NEST_PROTOCOL },
67 { L("TOP"), NEST_TOP },
68 { L("VENDOR"), NEST_VENDOR }
69};
71
73
74/** Parser context for dict_from_file
75 *
76 * Allows vendor and TLV context to persist across $INCLUDEs
77 */
78typedef struct {
79 char *filename; //!< name of the file where we read this entry
80 int line; //!< line number where we read this entry
81 fr_dict_attr_t const *da; //!< the da we care about
82 dict_nest_t nest; //!< for manual vs automatic begin / end things
83
84 fr_dict_keyword_finalise_t finalise; //!< function to call when popping
85 int member_num; //!< structure member numbers
86 fr_dict_attr_t const *struct_is_closed; //!< no more members are allowed
87 ssize_t struct_size; //!< size of the struct.
89
91 fr_dict_t *dict; //!< Protocol dictionary we're inserting attributes into.
92
93 dict_tokenize_frame_t stack[DICT_MAX_STACK]; //!< stack of attributes to track
94 int stack_depth; //!< points to the last used stack frame
95
96 fr_dict_attr_t *value_attr; //!< Cache of last attribute to speed up value processing.
97 fr_dict_attr_t const *relative_attr; //!< for ".82" instead of "1.2.3.82". only for parents of type "tlv"
99
100 char *filename; //!< current filename
101 int line; //!< current line
102};
103
105 char const *dir_name, char const *filename,
106 char const *src_file, int src_line);
107
108#define CURRENT_FRAME(_dctx) (&(_dctx)->stack[(_dctx)->stack_depth])
109#define CURRENT_DA(_dctx) (CURRENT_FRAME(_dctx)->da)
110#define CURRENT_FILENAME(_dctx) (CURRENT_FRAME(_dctx)->filename)
111#define CURRENT_LINE(_dctx) (CURRENT_FRAME(_dctx)->line)
112
113#define ASSERT_CURRENT_NEST(_dctx, _nest) fr_assert_msg(CURRENT_FRAME(_dctx)->nest == (_nest), "Expected frame type %s, got %s", \
114 fr_table_str_by_value(dict_nest_table, (_nest), "<INVALID>"), fr_table_str_by_value(dict_nest_table, CURRENT_FRAME(_dctx)->nest, "<INVALID>"))
115
117{
118 int i;
119
120 for (i = 0; i <= dctx->stack_depth; i++) {
121 dict_tokenize_frame_t const *frame = &dctx->stack[i];
122
123 FR_FAULT_LOG("[%d]: %s %s (%s): %s[%d]",
124 i,
125 fr_table_str_by_value(dict_nest_table, frame->nest, "<INVALID>"),
126 frame->da->name,
127 fr_type_to_str(frame->da->type),
128 frame->filename, frame->line);
129 }
130}
131
133{
134 int i;
135
136 for (i = dctx->stack_depth; i >= 0; i--) {
137 if (dctx->stack[i].nest & nest) return &dctx->stack[i];
138 }
139
140 return NULL;
141}
142
143static int CC_HINT(nonnull) dict_dctx_push(dict_tokenize_ctx_t *dctx, fr_dict_attr_t const *da, dict_nest_t nest)
144{
145 if ((dctx->stack_depth + 1) >= DICT_MAX_STACK) {
146 fr_strerror_const("Attribute definitions are nested too deep.");
147 return -1;
148 }
149
150 dctx->stack[++dctx->stack_depth] = (dict_tokenize_frame_t) {
151 .da = da,
152 .filename = dctx->filename,
153 .line = dctx->line,
154 .nest = nest,
155 };
156
157 return 0;
158}
159
160
161/** Pop the current stack frame
162 *
163 * @param[in] dctx Stack to pop from.
164 * @return
165 * - Pointer to the current stack frame.
166 * - NULL, if we're already at the root.
167 */
169{
170 if (dctx->stack_depth == 0) return NULL;
171
172 fr_assert(!dctx->stack[dctx->stack_depth].finalise);
173
174 return &dctx->stack[dctx->stack_depth--];
175}
176
177/** Unwind the stack until it points to a particular type of stack frame
178 *
179 * @param[in] dctx Stack to unwind.
180 * @param[in] nest Frame type to unwind to.
181 * @return
182 * - Pointer to the frame matching nest
183 * - NULL, if we unwound the complete stack and didn't find the frame.
184 */
186{
187 int i;
188
189 for (i = dctx->stack_depth; i >= 0; i--) {
191
192 /*
193 * We mash the stack depth here, because the finalisation function needs it. Plus, if
194 * there's any error, we don't care about the dctx stack, we just return up the C stack.
195 */
196 dctx->stack_depth = i;
197 frame = CURRENT_FRAME(dctx);
198
199 if (frame->finalise) {
200 if (frame->finalise(dctx) < 0) return NULL;
201 frame->finalise = NULL;
202 }
203
204 /*
205 * END-foo cannot be used without BEGIN-foo.
206 */
207 if (frame->filename && (frame->filename != dctx->filename) &&
208 (nest != NEST_ANY)) {
209 char const *name;
210
211 name = fr_table_str_by_value(dict_nest_table, nest, "<INVALID>");
212 fr_strerror_printf("END-%s in file %s[%d] without matching BEGIN-%s",
213 name, dctx->filename, dctx->line, name);
214 return NULL;
215 }
216
217 if ((frame->nest & nest) != 0) {
218 return frame;
219 }
220 }
221
222 return NULL;
223}
224
226{
227 return dict_dctx_unwind_until(dctx, NEST_ANY);
228}
229
230/*
231 * String split routine. Splits an input string IN PLACE
232 * into pieces, based on spaces.
233 */
234int fr_dict_str_to_argv(char *str, char **argv, int max_argc)
235{
236 int argc = 0;
237
238 while (*str) {
239 if (argc >= max_argc) break;
240
241 /*
242 * Chop out comments early.
243 */
244 if (*str == '#') {
245 *str = '\0';
246 break;
247 }
248
249 while ((*str == ' ') ||
250 (*str == '\t') ||
251 (*str == '\r') ||
252 (*str == '\n'))
253 *(str++) = '\0';
254
255 if (!*str) break;
256
257 argv[argc] = str;
258 argc++;
259
260 while (*str &&
261 (*str != ' ') &&
262 (*str != '\t') &&
263 (*str != '\r') &&
264 (*str != '\n'))
265 str++;
266 }
267
268 return argc;
269}
270
271static bool dict_read_sscanf_i(unsigned int *pvalue, char const *str)
272{
273 int unsigned ret = 0;
274 int base = 10;
275 char const *tab = "0123456789";
276
277 if ((str[0] == '0') &&
278 ((str[1] == 'x') || (str[1] == 'X'))) {
279 tab = "0123456789abcdef";
280 base = 16;
281
282 str += 2;
283 }
284
285 while (*str) {
286 char const *c;
287
288 if (*str == '.') break;
289
290 c = memchr(tab, tolower((uint8_t)*str), base);
291 if (!c) return false;
292
293 if (ret >= (UINT_MAX / base)) return false;
294
295 ret *= base;
296 ret += (c - tab);
297 str++;
298 }
299
300 *pvalue = ret;
301 return true;
302}
303
304/** Set a new root dictionary attribute
305 *
306 * @note Must only be called once per dictionary.
307 *
308 * @param[in] dict to modify.
309 * @param[in] name of dictionary root.
310 * @param[in] proto_number The artificial (or IANA allocated) number for the protocol.
311 * This is only used for
312 * @return
313 * - 0 on success.
314 * - -1 on failure.
315 */
316static int dict_root_set(fr_dict_t *dict, char const *name, unsigned int proto_number)
317{
318 fr_dict_attr_t *da;
319
320 fr_dict_attr_flags_t flags = {
321 .is_root = 1,
322 .type_size = dict->proto->default_type_size,
323 .length = dict->proto->default_type_length,
324 };
325
326 if (!fr_cond_assert(!dict->root)) {
327 fr_strerror_const("Dictionary root already set");
328 return -1;
329 }
330
331 da = dict_attr_alloc_root(dict->pool, dict, name, proto_number, &(dict_attr_args_t){ .flags = &flags });
332 if (unlikely(!da)) return -1;
333
334 dict->root = da;
335 dict->root->dict = dict;
336 DA_VERIFY(dict->root);
337
338 return 0;
339}
340
341static int dict_process_type_field(dict_tokenize_ctx_t *dctx, char const *name_in, fr_dict_attr_t **da_p)
342{
343 char name_buff[128];
344 char *name;
345 char *p;
347
348 /*
349 * Work on a writable copy so we can split the type name and length
350 * in place without modifying the caller's buffer.
351 */
352 if (strlcpy(name_buff, name_in, sizeof(name_buff)) >= sizeof(name_buff)) {
353 fr_strerror_printf("Type field '%s' is too long", name_in);
354 return -1;
355 }
356 name = name_buff;
357
358 /*
359 * Some types can have fixed length
360 */
361 p = strchr(name, '[');
362 if (p) {
363 char *q;
364 unsigned int length;
365
366 *p = '\0';
367 q = strchr(p + 1, ']');
368 if (!q) {
369 fr_strerror_printf("Invalid format for '%s[...]'", name);
370 return -1;
371 }
372
373 *q = '\0';
374 if (q[1]) {
375 fr_strerror_const("length, if present, must end type field");
376 return -1;
377 }
378
379 if (!dict_read_sscanf_i(&length, p + 1)) {
380 fr_strerror_printf("Invalid length for '%s[...]'", name);
381 return -1;
382 }
383
384 /*
385 * "length" has to fit into the flags.length field.
386 */
387 if ((length == 0) || (length > UINT16_MAX)) {
388 fr_strerror_printf("Invalid length for '%s[...]'", name);
389 return -1;
390 }
391
392 /*
393 * Now that we have a length, check the data type.
394 */
395 if (strcmp(name, "octets") == 0) {
397
398 } else if (strcmp(name, "string") == 0) {
400
401 } else if (strcmp(name, "struct") == 0) {
403
404 } else if (strcmp(name, "union") == 0) {
406
407 } else if (strcmp(name, "bit") == 0) {
408 if (CURRENT_FRAME(dctx)->da->type != FR_TYPE_STRUCT) {
409 fr_strerror_const("Bit fields can only be defined as a MEMBER of data type 'struct'");
410 return -1;
411 }
412
413 (*da_p)->flags.extra = 1;
414 (*da_p)->flags.subtype = FLAG_BIT_FIELD;
415
416 if (length == 1) {
418 } else if (length <= 8) {
420 } else if (length <= 16) {
422 } else if (length <= 32) {
424 } else if (length <= 56) { /* for laziness in encode / decode */
426 } else {
427 fr_strerror_const("Invalid length for bit field");
428 return -1;
429 }
430
431 /*
432 * Cache where on a byte boundary this
433 * bit field ends. We could have the
434 * validation function loop through all
435 * previous siblings, but that's
436 * annoying.
437 */
438 (*da_p)->flags.flag_byte_offset = length;
439
440 } else {
441 fr_strerror_printf("Attributes of type '%s' cannot use the %s[...] syntax",
442 name, name);
443 return -1;
444 }
445
446 (*da_p)->flags.is_known_width = true;
447 (*da_p)->flags.length = length;
448 return dict_attr_type_init(da_p, type);
449 }
450
451 /*
452 * We default to using the standard FreeRADIUS types.
453 *
454 * However, if there is a protocol-specific type parsing
455 * function, we call that, too. That ordering allows the
456 * protocol-specific names to over-ride the default ones.
457 */
459
460 if (dctx->dict->proto->attr.type_parse &&
461 !dctx->dict->proto->attr.type_parse(&type, da_p, name)) {
462 return -1;
463 }
464
465 switch (type) {
466 /*
467 * Still not known, or is still a NULL type, that's an error.
468 *
469 * The protocol-specific function can return an error if
470 * it has an error in its parsing. Or, it can return
471 * "true"
472 */
473 case FR_TYPE_NULL:
474 fr_strerror_printf("Unknown data type '%s'", name);
475 return -1;
476
477 case FR_TYPE_LEAF:
478 case FR_TYPE_TLV:
479 case FR_TYPE_STRUCT:
480 case FR_TYPE_VSA:
481 case FR_TYPE_GROUP:
482 case FR_TYPE_UNION:
483 break;
484
485 /*
486 * @todo - allow definitions of type 'vendor' only if we need to have different
487 * type_size/length for VSAs or "evs" in the Extended-Attribute space.
488 */
489 case FR_TYPE_VENDOR:
490 fr_strerror_const("Cannot use data type 'vendor' - use BEGIN-VENDOR instead");
491 return -1;
492
493 default:
494 fr_strerror_printf("Invalid data type '%s'", name);
495 return -1;
496 }
497
498 return dict_attr_type_init(da_p, type);
499}
500
501/** Define a flag setting function, which sets one bit in a fr_dict_attr_flags_t
502 *
503 * This is here, because AFAIK there's no completely portable way to get the bit
504 * offset of a bit field in a structure.
505 */
506#define FLAG_FUNC(_name) \
507static int dict_flag_##_name(fr_dict_attr_t **da_p, UNUSED char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)\
508{ \
509 (*da_p)->flags._name = 1; \
510 return 0; \
511}
512
513FLAG_FUNC(array)
514
515static int dict_flag_clone(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)
516{
517 /*
518 * Clone has a limited scope.
519 */
520 switch ((*da_p)->type) {
521 case FR_TYPE_LEAF:
522 case FR_TYPE_STRUCT:
523 case FR_TYPE_TLV:
524 break;
525
526 default:
527 fr_strerror_printf("Attributes of data type '%s' cannot use 'clone=...'", fr_type_to_str((*da_p)->type));
528 return -1;
529 }
530
531 /*
532 * Allow cloning of any types, so long as
533 * the types are the same. We do the checks later.
534 */
536
537 /*
538 * We don't know how big the cloned reference is, so it isn't known width.
539 */
540 (*da_p)->flags.is_known_width = 0;
541
542 return 0;
543}
544
545FLAG_FUNC(counter)
546
547static int dict_flag_enum(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
548{
549 /*
550 * Allow enum=... as an almost synonym for "clone", where we copy only the VALUEs, and not any
551 * children.
552 */
553 if (!fr_type_is_leaf((*da_p)->type)) {
554 fr_strerror_const("'enum=...' references cannot be used for structural types");
555 return -1;
556 }
557
558 /*
559 * Ensure that this attribute has room for enums.
560 */
561 if (!dict_attr_ext_alloc(da_p, FR_DICT_ATTR_EXT_ENUMV)) return -1;
562
564
565 return 0;
566}
567
568/** "flat"
569 *
570 * We have to parse the flat flag for tests, but only the various
571 * protocol libraries can set it for protocol-specific attributes.
572 */
573static int dict_flag_flat(fr_dict_attr_t **da_p, UNUSED char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
574{
575 if ((*da_p)->type != FR_TYPE_GROUP) {
576 fr_strerror_const("'flat' flag can only be used for data type 'group'");
577 return -1;
578 }
579
580 if (!(*da_p)->flags.internal) {
581 fr_strerror_const("'flat' flag can only be used for 'internal' attributes");
582 return -1;
583 }
584
585 (*da_p)->flags.allow_flat = true;
586
587 return 0;
588}
589
590FLAG_FUNC(internal)
591
592static int dict_flag_key(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
593{
594 fr_dict_attr_t *da = *da_p;
595 fr_dict_attr_t const *key;
597
598 if (fr_type_is_leaf(da->type)) {
599 if (value) {
600 fr_strerror_const("Attributes defining a 'key' field cannot specify a key reference");
601 return -1;
602 }
603
604 if ((da->type != FR_TYPE_UINT8) && (da->type != FR_TYPE_UINT16) && (da->type != FR_TYPE_UINT32)) {
605 fr_strerror_const("The 'key' flag can only be used for attributes of type 'uint8', 'uint16', or 'uint32'");
606 return -1;
607 }
608
609 if (da->flags.extra) {
610 fr_strerror_const("Bit fields cannot be key fields");
611 return -1;
612 }
613
614 da->flags.extra = 1;
615 da->flags.subtype = FLAG_KEY_FIELD;
616 return 0;
617 }
618
619 if (da->type != FR_TYPE_UNION) {
620 fr_strerror_printf("Attributes of type '%s' cannot define a 'key' reference", fr_type_to_str(da->type));
621 return -1;
622 }
623
624 if (!value) {
625 fr_strerror_const("Missing reference for 'key=...'");
626 return -1;
627 }
628
629 /*
630 * The reference must be to a sibling, which is marked "is key".
631 */
632 key = fr_dict_attr_by_name(NULL, da->parent, value);
633 if (!key) {
634 fr_strerror_printf("Invalid reference for 'key=...'. Parent %s does not have a child attribute named %s",
635 da->parent->name, value);
636 return -1;
637 }
638
639 if (da->parent != key->parent) {
640 fr_strerror_printf("Invalid reference for 'key=...'. Reference %s does not share a common parent",
641 value);
642 return -1;
643 }
644
645 if (!fr_dict_attr_is_key_field(key)) {
646 fr_strerror_printf("Invalid reference for 'key=...'. Reference %s is not a 'key' field",
647 value);
648 return -1;
649 }
650
651 /*
652 * Allocate the ref and save the value. This link exists solely so that the children of the
653 * UNION can easily find the key field of the parent STRUCT.
654 */
656 if (ext) {
657 fr_strerror_printf("Attribute already has a 'key=...' defined");
658 return -1;
659 }
660
661 ext = dict_attr_ext_alloc(da_p, FR_DICT_ATTR_EXT_KEY); /* can change da_p */
662 if (unlikely(!ext)) return -1;
663
665 ext->ref = key;
666
667 return 0;
668}
669
670static int dict_flag_length(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
671{
672 fr_dict_attr_t *da = *da_p;
673
674 if (strcmp(value, "uint8") == 0) {
675 da->flags.is_known_width = true;
676 da->flags.extra = 1;
677 da->flags.subtype = FLAG_LENGTH_UINT8;
678
679 } else if (strcmp(value, "uint16") == 0) {
680 da->flags.is_known_width = true;
681 da->flags.extra = 1;
682 da->flags.subtype = FLAG_LENGTH_UINT16;
683
684 } else {
685 fr_strerror_const("Invalid value given for the 'length' flag");
686 return -1;
687 }
688 da->flags.type_size = 0;
689
690 return 0;
691}
692
693static int dict_flag_offset(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
694{
695 fr_dict_attr_t *da = *da_p;
696 int offset;
697
698 if (da->type != FR_TYPE_STRUCT) {
699 fr_strerror_const("The 'offset' flag can only be used with data type 'struct'");
700 return -1;
701 }
702
703 if (!da_is_length_field(da)) {
704 fr_strerror_const("The 'offset' flag can only be used in combination with 'length=uint8' or 'length=uint16'");
705 return -1;
706 }
707
708 offset = atoi(value);
709 if ((offset <= 0) || (offset > 255)) {
710 fr_strerror_const("The 'offset' value must be between 1..255");
711 return -1;
712 }
713 da->flags.type_size = offset;
714
715 return 0;
716}
717
719{
720 fr_dict_attr_t *da = *da_p;
721 int precision;
722
723 switch (da->type) {
724 case FR_TYPE_DATE:
726 break;
727
728 default:
729 fr_strerror_const("The 'precision' flag can only be used with data types 'date' or 'time'");
730 return -1;
731 }
732
734 if (precision < 0) {
735 fr_strerror_printf("Unknown %s precision '%s'", fr_type_to_str(da->type), value);
736 return -1;
737 }
738 da->flags.flag_time_res = precision;
739
740 return 0;
741}
742
743static int dict_flag_ref(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
744{
745 fr_dict_attr_t *da = *da_p;
746
747 if (da->flags.extra) {
748 fr_strerror_const("Cannot use 'ref' with other flags");
749 return -1;
750 }
751
752 if (da->type != FR_TYPE_GROUP) {
753 fr_strerror_printf("The 'ref' flag cannot be used for type '%s'",
754 fr_type_to_str(da->type));
755 return -1;
756 }
757
759
760 return 0;
761}
762
764{
765 fr_dict_attr_t *da = *da_p;
766
767 da->flags.secret = 1;
768
769 if ((da->type != FR_TYPE_STRING) && (da->type != FR_TYPE_OCTETS)) {
770 fr_strerror_const("The 'secret' flag can only be used with data types 'string' or 'octets'");
771 return -1;
772 }
773
774 return 0;
775}
776
777static int dict_flag_subtype(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
778{
779 fr_dict_attr_t *da = *da_p;
780 fr_type_t subtype;
781
782 switch (da->type) {
783 case FR_TYPE_DATE:
785 break;
786
787 default:
788 fr_strerror_const("The 'subtype' flag can only be used with data types 'date' or 'time'");
789 return -1;
790 }
791
792 subtype = fr_type_from_str(value);
793 if (fr_type_is_null(subtype)) {
794 unknown_type:
795 fr_strerror_printf("Unknown or unsupported %s type '%s'",
796 fr_type_to_str(subtype),
797 value);
798 return -1;
799 }
800
801 switch (subtype) {
802 default:
803 goto unknown_type;
804
805 case FR_TYPE_INT16:
806 if (da->type == FR_TYPE_DATE) goto unknown_type;
807 da->flags.length = 2;
808 break;
809
810 case FR_TYPE_UINT16:
811 da->flags.is_unsigned = true;
812 da->flags.length = 2;
813 break;
814
815 case FR_TYPE_INT32:
816 if (da->type == FR_TYPE_DATE) goto unknown_type;
817 da->flags.length = 4;
818 break;
819
820 case FR_TYPE_UINT32:
821 da->flags.is_unsigned = true;
822 da->flags.length = 4;
823 break;
824
825 case FR_TYPE_INT64:
826 if (da->type == FR_TYPE_DATE) goto unknown_type;
827 da->flags.length = 8;
828 break;
829
830 case FR_TYPE_UINT64:
831 da->flags.is_unsigned = true;
832 da->flags.length = 8;
833 break;
834 }
835
836 return 0;
837}
838
839FLAG_FUNC(unsafe)
840
841/** A lookup function for dictionary attribute flags
842 *
843 */
845 fr_dict_attr_flag_to_parser, fr_dict_flag_parser_rule_t const *, fr_dict_flag_parser_rule_t const *)
846
847static int CC_HINT(nonnull) dict_process_flag_field(dict_tokenize_ctx_t *dctx, char *name, fr_dict_attr_t **da_p)
848{
849 static fr_dict_flag_parser_t dict_common_flags[] = {
850 { L("array"), { .func = dict_flag_array } },
851 { L("clone"), { .func = dict_flag_clone, .needs_value = true } },
852 { L("counter"), { .func = dict_flag_counter } },
853 { L("enum"), { .func = dict_flag_enum, .needs_value = true } },
854 { L("flat"), { .func = dict_flag_flat } },
855 { L("internal"), { .func = dict_flag_internal } },
856 { L("key"), { .func = dict_flag_key } },
857 { L("length"), { .func = dict_flag_length, .needs_value = true } },
858 { L("offset"), { .func = dict_flag_offset, .needs_value = true } },
859 { L("precision"), { .func = dict_flag_precision, .needs_value = true } },
860 { L("ref"), { .func = dict_flag_ref, .needs_value = true } },
861 { L("secret"), { .func = dict_flag_secret } },
862 { L("subtype"), { .func = dict_flag_subtype, .needs_value = true } },
863 { L("unsafe"), { .func = dict_flag_unsafe } },
864 };
865 static size_t dict_common_flags_len = NUM_ELEMENTS(dict_common_flags);
866
867 char *p, *next = NULL;
868
869 if ((*da_p)->type == FR_TYPE_NULL) {
870 fr_strerror_const("Type must be specified before parsing flags");
871 return -1;
872 }
873
874 for (p = name; p && *p != '\0' ; p = next) {
875 char *key, *value;
876 fr_dict_flag_parser_rule_t const *parser;
877
878 key = p;
879
880 /*
881 * Search for the first '=' or ','
882 */
883 for (next = p + 1; *next && (*next != '=') && (*next != ','); next++) {
884 /* do nothing */
885 }
886
887 /*
888 * We have a value, zero out the '=' and point to the value.
889 */
890 if (*next == '=') {
891 *(next++) = '\0';
892 value = next;
893
894 if (!*value || (*value == ',')) {
895 fr_strerror_printf("Missing value after '%s='", key);
896 return -1;
897 }
898 } else {
899 value = NULL;
900 }
901
902 /*
903 * Skip any trailing text in the value.
904 */
905 for (/* nothing */; *next; next++) {
906 if (*next == ',') {
907 *(next++) = '\0';
908 break;
909 }
910 }
911
912 /*
913 * Search the protocol table, then the main table.
914 * This allows protocols to overload common flags.
915 */
916 if (!((dctx->dict->proto->attr.flags.table &&
917 fr_dict_attr_flag_to_parser(&parser, dctx->dict->proto->attr.flags.table,
918 dctx->dict->proto->attr.flags.table_len, key, NULL)) ||
919 fr_dict_attr_flag_to_parser(&parser, dict_common_flags, dict_common_flags_len, key, NULL))) {
920 fr_strerror_printf("Unknown flag '%s'", key);
921 return -1;
922 }
923
924 if (parser->needs_value && !value) {
925 fr_strerror_printf("Flag '%s' requires a value", key);
926 return -1;
927 }
928
929 if (unlikely(parser->func(da_p, value, parser) < 0)) return -1;
930 }
931
932 /*
933 * Don't check the flags field for validity via
934 * dict_attr_flags_valid(). It may be updated by various
935 * protocol-specific callback functions. And,
936 * fr_dict_attr_add() calls dict_attr_flags_valid() anyways.
937 */
938
939 return 0;
940}
941
943{
944 if (dict_fixup_apply(&dctx->fixup) < 0) return -1;
945
946 dctx->value_attr = NULL;
947 dctx->relative_attr = NULL;
948
949 return 0;
950}
951
952static inline CC_HINT(always_inline)
954{
955 da->filename = CURRENT_FILENAME(dctx);
956 da->line = CURRENT_LINE(dctx);
957}
958
959/** Add an attribute to the dictionary, or add it to a list of attributes to clone later
960 *
961 * @param[in] fixup context to add an entry to (if needed).
962 * @param[in] da_p to either add, or create a fixup for.
963 * @return
964 * - 0 on success, and an attribute was added.
965 * - 1 on success, and a deferred entry was added.
966 * - -1 on failure.
967 */
969{
971 fr_dict_attr_t *da = *da_p;
972 int ret = 0;
973
974 /*
975 * Check for any references associated with the attribute,
976 * if they're unresolved, then add fixups.
977 *
978 * We do this now, as we know the attribute memory chunk
979 * is stable, and we can safely add the fixups.
980 */
982 if (ref && fr_dict_attr_ref_is_unresolved(ref->type)) {
983 /*
984 * See if we can immediately apply the ref.
985 */
986 fr_dict_attr_t const *src;
987
988 switch (fr_dict_attr_ref_type(ref->type)) {
990 /*
991 * IF the ref exists, we can always add it. The ref won't be changed later.
992 */
993 if (fr_dict_protocol_reference(&src, da->parent, &FR_SBUFF_IN_STR(ref->unresolved)) < 0) return -1;
994
995 if (src && (dict_attr_ref_set(*da_p, src, FR_DICT_ATTR_REF_ALIAS) < 0)) return -1;
996
997 if (fr_dict_attr_add_initialised(da) < 0) {
998 error:
999 talloc_free(da);
1000 *da_p = NULL;
1001 return -1;
1002 }
1003
1004 if (!src && (dict_fixup_group_enqueue(fixup, da, ref->unresolved) < 0)) return -1;
1005 ret = 1;
1006 break;
1007
1009 /*
1010 * Do NOT copy the enums now. Later dictionaries may add more values, and we
1011 * want to be able to copy all values.
1012 */
1013 if (fr_dict_attr_add_initialised(da) < 0) goto error;
1014
1015 if (dict_fixup_clone_enum_enqueue(fixup, da, ref->unresolved) < 0) return -1;
1016 break;
1017
1019 /*
1020 * @todo - if we defer this clone, we get errors loading dictionary.wimax. That
1021 * likely means there are issues with the dict_fixup_clone_apply() function.
1022 */
1023 if (fr_dict_protocol_reference(&src, da->parent, &FR_SBUFF_IN_STR(ref->unresolved)) < 0) return -1;
1024 if (src) {
1025 if (dict_fixup_clone(da_p, src) < 0) return -1;
1026 break;
1027 }
1028
1029 if (dict_fixup_clone_enqueue(fixup, da, ref->unresolved) < 0) return -1;
1030 ret = 1;
1031 break;
1032
1033 default:
1034 fr_strerror_const("Unknown reference type");
1035 return -1;
1036 }
1037 } else {
1038 if (fr_dict_attr_add_initialised(da) < 0) goto error;
1039 }
1040
1041 return ret;
1042}
1043
1044/** Check if this definition is a duplicate, and if it is, whether we should skip it error out
1045 *
1046 * @return
1047 * - 1 if this is not a duplicate.
1048 * - 0 if this is a duplicate, and we should ignore the definition.
1049 * - -1 if this is a duplicate, and we should error out.
1050 */
1052{
1053 fr_dict_attr_t const *dup_name = NULL;
1054 fr_dict_attr_t const *dup_num = NULL;
1055 fr_dict_attr_t const *found;
1056
1057 /*
1058 * Search in the parent for a duplicate by name and then by num
1059 */
1060 if (!da->parent) return 1; /* no parent no conflicts possible */
1061
1062 dup_name = fr_dict_attr_by_name(NULL, da->parent, da->name);
1063 if (!da->flags.name_only) dup_num = fr_dict_attr_child_by_num(da->parent, da->attr);
1064
1065 /*
1066 * Not a duplicate...
1067 */
1068 if (!dup_name && !dup_num) return 1;
1069
1070 found = dup_name ? dup_name : dup_num;
1071
1072 switch (da->type) {
1073 /*
1074 * For certain types, we allow strict duplicates as if
1075 * the user wants to add extra children in the custom
1076 * dictionary, or wants to avoid ordering issues between
1077 * multiple dictionaries, we need to support this.
1078 */
1079 case FR_TYPE_VSA:
1080 case FR_TYPE_VENDOR:
1081 case FR_TYPE_TLV:
1082 if (fr_dict_attr_cmp_fields(da, found) == 0) return 0;
1083 break;
1084
1085 case FR_TYPE_LEAF:
1086 /*
1087 * Leaf types can be duplicated if they are identical.
1088 */
1089 if ((da->type == found->type) &&
1090 (fr_dict_attr_cmp_fields(da, found) == 0)) return 0;
1091 break;
1092
1093 default:
1094 break;
1095 }
1096
1097 if (dup_name) {
1098 fr_strerror_printf("Duplicate attribute name '%s' in namespace '%s'. Originally defined %s[%d]",
1099 da->name, da->parent->name, dup_name->filename, dup_name->line);
1100 return -1;
1101 }
1102
1103 fr_strerror_printf("Duplicate attribute number %u in parent '%s'. Originally defined %s[%d]",
1104 da->attr, da->parent->name, dup_num->filename, dup_num->line);
1105 return -1;
1106}
1107
1109{
1110 fr_dict_attr_t const *da;
1111 dict_tokenize_frame_t const *frame = CURRENT_FRAME(dctx);
1112
1113 da = frame->da;
1114 fr_assert(da->type == FR_TYPE_STRUCT);
1115
1116 /*
1117 * The structure was fixed-size, but the fields don't fill it. That's an error.
1118 *
1119 * Since process_member() checks for overflow, the check here is really only for
1120 * underflow.
1121 */
1122 if (da->flags.is_known_width) {
1123 if (CURRENT_FRAME(dctx)->struct_size != da->flags.length) {
1124 fr_strerror_printf("MEMBERs of %s struct[%u] do not exactly fill the fixed-size structure",
1125 da->name, da->flags.length);
1126 return -1;
1127 }
1128
1129 return 0;
1130 }
1131
1132 /*
1133 * If we have discovered that the structure has a fixed size, then update the da with that
1134 * information.
1135 */
1136 if (frame->struct_size < UINT16_MAX) {
1137 UNCONST(fr_dict_attr_t *, da)->flags.length = frame->struct_size;
1138 } /* else length 0 means "unknown / variable size / too large */
1139
1140 return 0;
1141}
1142
1144{
1145 /*
1146 * Adding an attribute of type 'struct' is an implicit
1147 * BEGIN-STRUCT.
1148 */
1149 if (da->type == FR_TYPE_STRUCT) {
1150 if (dict_dctx_push(dctx, da, NEST_NONE) < 0) return -1;
1151
1152 CURRENT_FRAME(dctx)->finalise = dict_struct_finalise;
1153 dctx->value_attr = NULL;
1154
1155 } else if (fr_type_is_leaf(da->type)) {
1156 dctx->value_attr = da;
1157
1158 } else {
1159 dctx->value_attr = NULL;
1160 }
1161
1162 return 0;
1163}
1164
1166 fr_dict_attr_t const *parent, char const *name,
1167 char const *type_name, char *flag_name,
1168 fr_dict_attr_flags_t const *base_flags)
1169{
1170 fr_dict_attr_t *da, *to_free = NULL;
1171 size_t len;
1172
1173 /*
1174 * Dictionaries need to have real names, not v3-style ones "Attr-#". And not ones which are
1175 * solely numerical.
1176 */
1177 if (strncmp(name, "Attr-", 5) == 0) {
1178 fr_strerror_const("Invalid name - 'Attr-' is an invalid name");
1179 return -1;
1180 }
1181
1182 len = strlen(name);
1183 if (fr_sbuff_adv_past_allowed( &FR_SBUFF_IN(name, len), SIZE_MAX, sbuff_char_class_int, NULL) == len) {
1184 fr_strerror_printf("Invalid attribute name '%s' - the name cannot be an integer", name);
1185 return -1;
1186 }
1187
1188 /*
1189 * Allocate the attribute here, and then fill in the fields
1190 * as we start parsing the various elements of the definition.
1191 */
1192 if (!*da_p) {
1193 da = dict_attr_alloc_null(dctx->dict->pool, dctx->dict->proto);
1194 if (unlikely(!da)) return -1;
1195 to_free = da;
1196
1197 } else {
1198 da = *da_p;
1199 }
1200 dict_attr_location_set(dctx, da);
1201 da->dict = dctx->dict;
1202
1203 /*
1204 * Set some fields to be friendlier to the type / flag
1205 * parsing and validation routines.
1206 */
1207 da->parent = parent;
1208 da->name = name;
1209
1210 /*
1211 * Set the attribute flags from the base flags.
1212 */
1213 memcpy(&da->flags, base_flags, sizeof(da->flags));
1214
1215 if (unlikely(strcmp(type_name, "auto") == 0)) {
1216 fr_dict_attr_t const *src;
1217 char const *p, *end;
1218
1219 if (!flag_name || !(p = strstr(flag_name, "clone="))) {
1220 fr_strerror_const("Data type of 'auto' is missing the required flag 'clone=...'");
1221 goto error;
1222 }
1223
1224 p += 6;
1225 for (end = p; *end != '\0'; end++) {
1226 if (*end == ',') break;
1227 }
1228
1229 if (fr_dict_protocol_reference(&src, parent, &FR_SBUFF_IN(p, end)) < 0) goto error;
1230 if (!src) {
1231 fr_strerror_const("Data type 'auto' requires that the 'clone=...' reference points to an attribute which already exists");
1232 goto error;
1233 }
1234
1235 /*
1236 * Don't copy the source yet, as later things may add enums, children, etc. to the source
1237 * attribute. Instead, we just copy the data type.
1238 */
1239 if (dict_attr_type_init(&da, src->type) < 0) goto error;
1240
1241 } else {
1242 /*
1243 * Set the base type of the attribute.
1244 */
1245 if (dict_process_type_field(dctx, type_name, &da) < 0) {
1246 error:
1247 if (da == to_free) talloc_free(to_free);
1248 return -1;
1249 }
1250 }
1251
1252 /*
1253 * Clear the temporary parent pointer.
1254 */
1255 da->parent = NULL;
1256 if (unlikely(dict_attr_parent_init(&da, parent) < 0)) goto error;
1257
1258 /*
1259 * Parse optional flags. We pass in the partially allocated
1260 * attribute so that flags can be set directly.
1261 *
1262 * Where flags contain variable length fields, this is
1263 * significantly easier than populating a temporary struct.
1264 */
1265 if (flag_name) if (dict_process_flag_field(dctx, flag_name, &da) < 0) goto error;
1266
1267 da->name = NULL; /* the real name will be a talloc'd chunk */
1268
1269 *da_p = da;
1270 return 0;
1271}
1272
1273/*
1274 * Process the $INCLUDE command
1275 */
1276static int dict_read_process_include(dict_tokenize_ctx_t *dctx, char **argv, int argc, char const *dir)
1277{
1278 int rcode;
1279 bool required = true;
1280 int stack_depth = dctx->stack_depth;
1281 char *src_file = dctx->filename;
1282 int src_line = dctx->line;
1283 char *pattern;
1284 char const *filename;
1285 fr_globdir_iter_t iter;
1286
1287 /*
1288 * Allow "$INCLUDE" or "$INCLUDE-", but
1289 * not anything else.
1290 */
1291 if ((argv[0][8] != '\0') && ((argv[0][8] != '-') || (argv[0][9] != '\0'))) {
1292 fr_strerror_printf("Invalid keyword '%s'", argv[0]);
1293 return -1;
1294 }
1295
1296 if (argc != 2) {
1297 fr_strerror_printf("Unexpected text after $INCLUDE at %s[%d]", fr_cwd_strip(src_file), src_line);
1298 return -1;
1299 }
1300
1301 pattern = argv[1];
1302 required = (argv[0][8] != '-');
1303
1304 /*
1305 * Allow limited macro capability, so people don't have
1306 * to remember where the root dictionaries are located.
1307 */
1308 if (strncmp(pattern, "${dictdir}/", 11) == 0) {
1309 dir = fr_dict_global_ctx_dir();
1310 pattern += 11;
1311 }
1312
1313 /*
1314 * Figure out what we need to open, and put the result into "filename".
1315 */
1316 rcode = fr_globdir_iter_init(&filename, dir, pattern, &iter);
1317 if (rcode < 0) {
1318 failed:
1319 fr_strerror_printf("Failed opening $INCLUDE of %s/%s at %s[%d] - %s",
1320 dir, pattern, fr_cwd_strip(src_file), src_line, fr_syserror(errno));
1321 return -1;
1322 }
1323
1324 /*
1325 * No files may or may not be an error, depending on if the $INCLUDE was required.
1326 */
1327 if (rcode == 0) {
1328 if (required) {
1329 errno = ENOENT;
1330 goto failed;
1331 }
1332
1333 fr_strerror_clear(); /* delete all errors */
1334 return 0;
1335 }
1336
1337 /*
1338 * "filename" is already the file, so we use do{}while() instead of while{}
1339 */
1340 do {
1341 rcode = _dict_from_file(dctx, dir, filename, src_file, src_line);
1342 if (rcode < 0) {
1343 fr_strerror_printf_push("from $INCLUDE at %s[%d]", fr_cwd_strip(src_file), src_line);
1344 break;
1345 }
1346
1347 if (dctx->stack_depth < stack_depth) {
1348 fr_strerror_printf("unexpected END-??? in $INCLUDE at %s[%d]",
1349 fr_cwd_strip(src_file), src_line);
1350 rcode = -1;
1351 break;
1352 }
1353
1354 } while ((rcode = fr_globdir_iter_next(&filename, &iter)) == 1);
1355 (void) fr_globdir_iter_free(&iter);
1356
1357 return rcode; /* could be an error! */
1358}
1359
1360static int dict_read_parse_format(char const *format, int *ptype, int *plength, bool *pcontinuation)
1361{
1362 char const *p;
1363 int type, length;
1364 bool continuation = false;
1365
1366 if (strncasecmp(format, "format=", 7) != 0) {
1367 fr_strerror_printf("Invalid format for VENDOR. Expected 'format=', got '%s'",
1368 format);
1369 return -1;
1370 }
1371
1372 p = format + 7;
1373 if ((strlen(p) < 3) ||
1374 !isdigit((uint8_t)p[0]) ||
1375 (p[1] != ',') ||
1376 !isdigit((uint8_t)p[2]) ||
1377 (p[3] && (p[3] != ','))) {
1378 fr_strerror_printf("Invalid format for VENDOR. Expected text like '1,1', got '%s'",
1379 p);
1380 return -1;
1381 }
1382
1383 type = (int)(p[0] - '0');
1384 length = (int)(p[2] - '0');
1385
1386 if ((type != 1) && (type != 2) && (type != 4)) {
1387 fr_strerror_printf("Invalid type value %d for VENDOR", type);
1388 return -1;
1389 }
1390
1391 if ((length != 0) && (length != 1) && (length != 2)) {
1392 fr_strerror_printf("Invalid length value %d for VENDOR", length);
1393 return -1;
1394 }
1395
1396 if (p[3] == ',') {
1397 if (!p[4]) {
1398 fr_strerror_printf("Invalid format for VENDOR. Expected text like '1,1', got '%s'",
1399 p);
1400 return -1;
1401 }
1402
1403 if ((p[4] != 'c') ||
1404 (p[5] != '\0')) {
1405 fr_strerror_printf("Invalid format for VENDOR. Expected text like '1,1', got '%s'",
1406 p);
1407 return -1;
1408 }
1409 continuation = true;
1410
1411 if ((type != 1) || (length != 1)) {
1412 fr_strerror_const("Only VSAs with 'format=1,1' can have continuations");
1413 return -1;
1414 }
1415 }
1416
1417 *ptype = type;
1418 *plength = length;
1419 *pcontinuation = continuation;
1420 return 0;
1421}
1422
1423/*
1424 * Process the ALIAS command
1425 *
1426 * ALIAS name ref
1427 *
1428 * Creates an attribute "name" in the root namespace of the current
1429 * dictionary, which is a pointer to "ref".
1430 */
1431static int dict_read_process_alias(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
1432{
1433 fr_dict_attr_t const *da;
1434 fr_dict_attr_t const *parent = CURRENT_FRAME(dctx)->da;
1435
1436 if (argc != 2) {
1437 fr_strerror_const("Invalid ALIAS syntax");
1438 return -1;
1439 }
1440
1441 /*
1442 * Dictionaries need to have real names, not shitty ones.
1443 */
1444 if (strncmp(argv[0], "Attr-", 5) == 0) {
1445 fr_strerror_const("Invalid ALIAS name");
1446 return -1;
1447 }
1448
1449 if (strchr(argv[0], '.') != NULL) {
1450 fr_strerror_const("ALIAS names must be in the local context, and cannot contain '.'");
1451 return -1;
1452 }
1453
1454 /*
1455 * Internally we can add aliases to STRUCTs and GROUPs. But the poor user can't.
1456 *
1457 * This limitation is mainly so that we can differentiate automatically added aliases (which
1458 * point to unions), from ones added by users. If we make dict_attr_acopy_aliases() a little
1459 * smarter, then we can relax those checks.
1460 */
1461 switch (parent->type) {
1462 case FR_TYPE_TLV:
1463 case FR_TYPE_VSA:
1464 case FR_TYPE_VENDOR:
1465 break;
1466
1467 default:
1468 fr_strerror_printf("ALIAS cannot be added to data type '%s'", fr_type_to_str(parent->type));
1469 return -1;
1470 }
1471
1472 /*
1473 * Relative refs get resolved from the current namespace.
1474 */
1475 if (argv[1][0] == '@') {
1476 fr_strerror_const("An ALIAS reference cannot cross protocol boundaries");
1477 return -1;
1478
1479 } else if (argv[1][0] == '.') {
1480 if (argv[1][1] == '.') {
1481 fr_strerror_const("An ALIAS reference cannot use '..' to go back up to another parent");
1482 return -1;
1483 }
1484
1485 } else if (parent != dctx->dict->root) {
1486 fr_strerror_const("An ALIAS reference must use a leading '.' when referring to attributes with the same parent");
1487 return -1;
1488 }
1489
1490 /*
1491 * The <ref> can be a name.
1492 */
1493 da = fr_dict_attr_by_oid(NULL, parent, argv[1]);
1494 if (!da) {
1495 /*
1496 * If we can't find it now, the file containing the ALIASes may have been read before
1497 * the ALIASed attributes.
1498 *
1499 * @todo - we likely just want to forbid this.
1500 */
1501 return dict_fixup_alias_enqueue(&dctx->fixup, CURRENT_FILENAME(dctx), CURRENT_LINE(dctx),
1502 fr_dict_attr_unconst(parent), argv[0],
1503 fr_dict_attr_unconst(parent), argv[1]);
1504 }
1505
1506 return dict_attr_alias_add(fr_dict_attr_unconst(parent), argv[0], da, true);
1507}
1508
1509/*
1510 * Process the ATTRIBUTE command
1511 */
1512static int dict_read_process_attribute(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
1513{
1514 bool set_relative_attr;
1515
1516 ssize_t slen;
1517 unsigned int attr;
1518
1519 fr_dict_attr_t const *parent, *key = NULL;
1520 fr_dict_attr_t *da;
1521 fr_value_box_t box;
1522
1523 if ((argc < 3) || (argc > 4)) {
1524 fr_strerror_const("Invalid ATTRIBUTE syntax");
1525 return -1;
1526 }
1527
1528#ifdef STATIC_ANALYZER
1529 if (!dctx->dict) return -1;
1530#endif
1531
1532 /*
1533 * A non-relative ATTRIBUTE definition means that it is
1534 * in the context of the previous BEGIN-FOO. So we
1535 * unwind the stack to match.
1536 */
1537 if (argv[1][0] != '.') {
1538 dict_tokenize_frame_t const *frame;
1539
1540 frame = dict_dctx_unwind(dctx);
1541 if (!frame) return -1;
1542
1543 parent = frame->da;
1544
1545 /*
1546 * Allow '0xff00' as attribute numbers, but only
1547 * if there is no OID component.
1548 */
1549 if (strchr(argv[1], '.') == 0) {
1550 if (!dict_read_sscanf_i(&attr, argv[1])) {
1551 fr_strerror_const("Invalid ATTRIBUTE number");
1552 return -1;
1553 }
1554
1555 } else {
1556 slen = fr_dict_attr_by_oid_legacy(&parent, &attr, argv[1]);
1557 if (slen <= 0) return -1;
1558 }
1559
1560 /*
1561 * We allow relative attributes only for TLVs.
1562 *
1563 * We haven't parsed the type field yet, so we
1564 * just check it here manually.
1565 */
1566 set_relative_attr = (strcasecmp(argv[2], "tlv") == 0);
1567
1568 } else {
1569 if (!dctx->relative_attr) {
1570 fr_strerror_printf("No parent attribute reference was set for partial OID %s", argv[1]);
1571 return -1;
1572 }
1573
1574 parent = dctx->relative_attr;
1575
1576 slen = fr_dict_attr_by_oid_legacy(&parent, &attr, argv[1]);
1577 if (slen <= 0) return -1;
1578
1579 set_relative_attr = false;
1580 }
1581
1582 if (!fr_cond_assert(parent)) return -1; /* Should have provided us with a parent */
1583
1584 /*
1585 * Members of a 'struct' MUST use MEMBER, not ATTRIBUTE.
1586 */
1587 if (parent->type == FR_TYPE_STRUCT) {
1588 fr_strerror_printf("Member %s of ATTRIBUTE %s type 'struct' MUST use the \"MEMBER\" keyword",
1589 argv[0], parent->name);
1590 return -1;
1591 }
1592
1593 /*
1594 * A UNION can have child ATTRIBUTEs
1595 */
1596 if (parent->type == FR_TYPE_UNION) {
1598
1599 /*
1600 * The parent is a union. Get and verify the key ref.
1601 */
1603 fr_assert(ext != NULL);
1604
1605 /*
1606 * Double-check names against the reference.
1607 */
1608 key = ext->ref;
1609 fr_assert(key);
1611 }
1612
1613 da = dict_attr_alloc_null(dctx->dict->pool, dctx->dict->proto);
1614 if (unlikely(!da)) return -1;
1615
1616 /*
1617 * Record the attribute number BEFORE we parse the type and flags.
1618 *
1619 * This is needed for the DER dictionaries, and 'option'.
1620 *
1621 * It can also be useful for other protocols, which may
1622 * have restrictions on the various fields. It is
1623 * therefore useful to have all fields initialized before
1624 * the type/flag validation routines are called.
1625 */
1626 if (unlikely(dict_attr_num_init(da, attr) < 0)) {
1627 error:
1628 talloc_free(da);
1629 return -1;
1630 }
1631
1632 /*
1633 * Check the attribute number against the allowed values.
1634 */
1635 if (key) {
1636 fr_value_box_init(&box, FR_TYPE_UINT32, NULL, false);
1637 box.vb_uint32 = attr;
1638
1639 if (fr_value_box_cast_in_place(da, &box, key->type, NULL) < 0) {
1640 fr_strerror_printf_push("Invalid attribute number as key field %s has data type %s",
1641 key->name, fr_type_to_str(key->type));
1642 goto error;
1643 }
1644 }
1645
1646 if (dict_read_process_common(dctx, &da, parent, argv[0], argv[2],
1647 (argc > 3) ? argv[3] : NULL, base_flags) < 0) {
1648 goto error;
1649 }
1650
1651 if (da_is_bit_field(da)) {
1652 fr_strerror_const("Bit fields can only be defined as a MEMBER of data type 'struct'");
1653 goto error;
1654 }
1655
1656 /*
1657 * Unions need a key field. And key fields can only appear inside of a struct.
1658 */
1659 if (da->type == FR_TYPE_UNION) {
1660 fr_strerror_const("ATTRIBUTEs of type 'union' can only be defined as a MEMBER of data type 'struct'");
1661 goto error;
1662 }
1663
1664 /*
1665 * Cross-check fixed lengths.
1666 */
1667 if (key && (parent->flags.is_known_width)) {
1668 if (!da->flags.is_known_width) {
1669 da->flags.is_known_width = 1;
1670 da->flags.length = parent->flags.length;
1671
1672 } else if (da->flags.length != parent->flags.length) {
1673 fr_strerror_printf("Invalid length %u for struct, the parent union %s has a different length %u",
1674 da->flags.length, parent->name, parent->flags.length);
1675 goto error;
1676 }
1677 }
1678
1679#ifdef WITH_DICTIONARY_WARNINGS
1680 /*
1681 * Hack to help us discover which vendors have illegal
1682 * attributes.
1683 */
1684 if (!vendor && (attr < 256) &&
1685 !strstr(fn, "rfc") && !strstr(fn, "illegal")) {
1686 fprintf(stderr, "WARNING: Illegal attribute %s in %s\n",
1687 argv[0], fn);
1688 }
1689#endif
1690
1691 /*
1692 * Set the attribute name
1693 */
1694 if (unlikely(dict_attr_finalise(&da, argv[0]) < 0)) {
1695 goto error;
1696 }
1697
1698 /*
1699 * Check to see if this is a duplicate attribute
1700 * and whether we should ignore it or error out...
1701 */
1702 switch (dict_attr_allow_dup(da)) {
1703 case 1:
1704 break;
1705
1706 case 0:
1707 talloc_free(da);
1708 return 0;
1709
1710 default:
1711 goto error;
1712 }
1713
1714 /*
1715 * Add the attribute we allocated earlier
1716 */
1717 switch (dict_attr_add_or_fixup(&dctx->fixup, &da)) {
1718 default:
1719 goto error;
1720
1721 /* New attribute, fixup stack */
1722 case 0:
1723 /*
1724 * Dynamically define where VSAs go. Note that we CANNOT
1725 * define VSAs until we define an attribute of type VSA!
1726 */
1727 if (da->type == FR_TYPE_VSA) {
1728 if (parent->flags.is_root) dctx->dict->vsa_parent = attr;
1729
1730 if (dict_fixup_vsa_enqueue(&dctx->fixup, da) < 0) {
1731 return -1; /* Leaves attr added */
1732 }
1733 }
1734
1735 /*
1736 * Add the VALUE to the key attribute, and ensure that
1737 * the VALUE also contains a pointer to the child struct.
1738 */
1739 if (key && (dict_attr_enum_add_name(fr_dict_attr_unconst(key), da->name, &box, false, true, da) < 0)) {
1740 return -1; /* Leaves attr added */
1741 }
1742
1743 /*
1744 * Adding an attribute of type 'struct' is an implicit
1745 * BEGIN-STRUCT.
1746 */
1747 if (da->type == FR_TYPE_STRUCT) {
1748 if (dict_dctx_push(dctx, da, NEST_NONE) < 0) return -1;
1749
1750 CURRENT_FRAME(dctx)->finalise = dict_struct_finalise;
1751 dctx->value_attr = NULL;
1752 } else {
1753 dctx->value_attr = da;
1754 }
1755
1756 if (set_relative_attr) dctx->relative_attr = da;
1757 break;
1758
1759 /* Deferred attribute, don't begin the TLV section automatically */
1760 case 1:
1761 break;
1762 }
1763
1764 /*
1765 * While UNIONs are named, it's nicer to hide them.
1766 * Therefore we automatically add an ALIAS in the unions
1767 * parent, for the child in the union.
1768 */
1769 if (parent->type == FR_TYPE_UNION) {
1770 fr_assert(parent->parent);
1771
1772 if (dict_attr_alias_add(parent->parent, da->name, da, false) < 0) {
1773 return -1; /* Leaves attr added */
1774 }
1775 }
1776
1777 return 0;
1778}
1779
1780static int dict_read_process_begin(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
1781{
1782 dict_tokenize_frame_t const *frame;
1783 fr_dict_attr_t const *da;
1784 fr_dict_attr_t const *common;
1785
1786 dctx->value_attr = NULL;
1787 dctx->relative_attr = NULL;
1788
1789 if (argc != 1) {
1790 fr_strerror_const("Invalid BEGIN keyword. Expected BEGIN <name>");
1791 return -1;
1792 }
1793
1795 if (!fr_cond_assert_msg(frame, "Context stack doesn't have an attribute or dictionary "
1796 "root to begin searching from %s[%d]", CURRENT_FILENAME(dctx), CURRENT_LINE(dctx)) ||
1797 !fr_cond_assert_msg(fr_type_is_structural(frame->da->type), "Context attribute is not structural %s[%d]",
1798 CURRENT_FILENAME(dctx), CURRENT_LINE(dctx))) {
1799 return -1;
1800 }
1801
1802 /*
1803 * Not really a reference as we don't support any of the
1804 * fancy syntaxes like refs do. A straight OID string
1805 * resolved from the current level of nesting is all we support.
1806 */
1807 da = fr_dict_attr_by_oid(NULL, frame->da, argv[0]);
1808 if (!da) {
1809 fr_strerror_printf("BEGIN %s is not resolvable in current context '%s'", argv[0], frame->da->name);
1810 return -1;
1811 }
1812
1813 /*
1814 * We cannot use BEGIN/END on structs. Once they're defined, they can't be modified.
1815 *
1816 * This restriction can be lifted once we don't auto-push on FR_TYPE_STRUCT.
1817 */
1818 if (!fr_type_is_tlv(da->type) && (da->type != FR_TYPE_UNION)) {
1819 fr_strerror_printf("BEGIN %s cannot be used with data type '%s'",
1820 argv[0],
1821 fr_type_to_str(da->type));
1822 return -1;
1823 }
1824
1825 common = fr_dict_attr_common_parent(frame->da, da, true);
1826 if (!common) {
1827 fr_strerror_printf("BEGIN %s should be a child of '%s'",
1828 argv[0], CURRENT_FRAME(dctx)->da->name);
1829 return -1;
1830 }
1831
1832 return dict_dctx_push(dctx, da, NEST_ATTRIBUTE);
1833}
1834
1835static int dict_read_process_begin_protocol(dict_tokenize_ctx_t *dctx, char **argv, int argc,
1836 UNUSED fr_dict_attr_flags_t *base_flags)
1837{
1838 fr_dict_t *found;
1839 dict_tokenize_frame_t const *frame;
1840
1841 dctx->value_attr = NULL;
1842 dctx->relative_attr = NULL;
1843
1844 if (argc != 1) {
1845 fr_strerror_const("Invalid BEGIN-PROTOCOL entry");
1846 return -1;
1847 }
1848
1849 /*
1850 * If we're not parsing in the context of the internal
1851 * dictionary, then we don't allow BEGIN-PROTOCOL
1852 * statements.
1853 */
1854 if (dctx->dict != dict_gctx->internal) {
1855 fr_strerror_const("Nested BEGIN-PROTOCOL statements are not allowed");
1856 return -1;
1857 }
1858
1859 found = dict_by_protocol_name(argv[0]);
1860 if (!found) {
1861 fr_strerror_printf("Unknown protocol '%s'", argv[0]);
1862 return -1;
1863 }
1864
1866 if (frame) {
1867 fr_strerror_printf("BEGIN-PROTOCOL cannot be used inside of any other BEGIN/END block. Previous definition is at %s[%d]",
1868 frame->filename, frame->line);
1869 return -1;
1870 }
1871
1872 /*
1873 * Add a temporary fixup pool
1874 */
1875 if (dict_fixup_init(&dctx->fixup) < 0) return -1;
1876
1877 /*
1878 * We're in the middle of loading this dictionary. Tell
1879 * fr_dict_protocol_afrom_file() to suppress recursive references.
1880 */
1881 found->loading = true;
1882
1883 dctx->dict = found;
1884
1885 return dict_dctx_push(dctx, dctx->dict->root, NEST_PROTOCOL);
1886}
1887
1888static int dict_read_process_begin_vendor(dict_tokenize_ctx_t *dctx, char **argv, int argc,
1889 UNUSED fr_dict_attr_flags_t *base_flags)
1890{
1891 fr_dict_vendor_t const *vendor;
1892
1893 fr_dict_attr_t const *vsa_da;
1894 fr_dict_attr_t const *vendor_da;
1895 fr_dict_attr_t *new;
1896 dict_tokenize_frame_t const *frame;
1897 char *p;
1898
1899 dctx->value_attr = NULL;
1900 dctx->relative_attr = NULL;
1901
1902 if (argc < 1) {
1903 fr_strerror_const("Invalid BEGIN-VENDOR entry");
1904 return -1;
1905 }
1906
1907 vendor = fr_dict_vendor_by_name(dctx->dict, argv[0]);
1908 if (!vendor) {
1909 fr_strerror_printf("Unknown vendor '%s'", argv[0]);
1910 return -1;
1911 }
1912
1913 /*
1914 * Check for extended attr VSAs
1915 *
1916 * BEGIN-VENDOR foo parent=Foo-Encapsulation-Attr
1917 */
1918 if (argc > 1) {
1919 fr_dict_attr_t const *da;
1920
1921 if (strncmp(argv[1], "parent=", 7) != 0) {
1922 fr_strerror_const("BEGIN-VENDOR invalid argument - expected 'parent='");
1923 return -1;
1924 }
1925
1926 p = argv[1] + 7;
1927 da = fr_dict_attr_by_oid(NULL, CURRENT_FRAME(dctx)->da, p);
1928 if (!da) {
1929 fr_strerror_printf("BEGIN-VENDOR Failed to find attribute '%s'", p);
1930 return -1;
1931 }
1932
1933 if (da->type != FR_TYPE_VSA) {
1934 fr_strerror_printf("Invalid parent for BEGIN-VENDOR. "
1935 "Attribute '%s' should be 'vsa' but is '%s'", p,
1936 fr_type_to_str(da->type));
1937 return -1;
1938 }
1939
1940 vsa_da = da;
1941
1942 } else if (dctx->dict->vsa_parent) {
1943 /*
1944 * Check that the protocol-specific VSA parent exists.
1945 */
1946 vsa_da = dict_attr_child_by_num(CURRENT_FRAME(dctx)->da, dctx->dict->vsa_parent);
1947 if (!vsa_da) {
1948 fr_strerror_printf("Failed finding VSA parent for Vendor %s",
1949 vendor->name);
1950 return -1;
1951 }
1952
1953 } else if (dctx->dict->string_based) {
1954 vsa_da = dctx->dict->root;
1955
1956 } else {
1957 fr_strerror_printf("BEGIN-VENDOR is forbidden for protocol %s - it has no ATTRIBUTE of type 'vsa'",
1958 dctx->dict->root->name);
1959 return -1;
1960 }
1961
1962 frame = dict_dctx_find_frame(dctx, NEST_VENDOR);
1963 if (frame) {
1964 fr_strerror_printf("Nested BEGIN-VENDOR is forbidden. Previous definition is at %s[%d]",
1965 frame->filename, frame->line);
1966 return -1;
1967 }
1968
1969 /*
1970 * Check if the VENDOR attribute exists under this VSA. If not, create one.
1971 *
1972 * @todo - There are no vendor fixups, so if the vendor has unusual type sizes, it MUST be
1973 * defined before the BEGIN-VENDOR is used.
1974 */
1975 vendor_da = dict_attr_child_by_num(vsa_da, vendor->pen);
1976 if (!vendor_da) {
1977 fr_dict_attr_flags_t flags = {};
1978
1979 new = dict_attr_alloc(dctx->dict->pool,
1980 vsa_da, argv[0], vendor->pen, FR_TYPE_VENDOR,
1981 &(dict_attr_args_t){ .flags = &flags });
1982 if (unlikely(!new)) return -1;
1983
1984 if (dict_attr_child_add(UNCONST(fr_dict_attr_t *, vsa_da), new) < 0) {
1985 talloc_free(new);
1986 return -1;
1987 }
1988
1989 if (dict_attr_add_to_namespace(UNCONST(fr_dict_attr_t *, vsa_da), new) < 0) {
1990 return -1; /* leaves attr added */
1991 }
1992
1993 vendor_da = new;
1994 } else {
1995 fr_assert(vendor_da->type == FR_TYPE_VENDOR);
1996 }
1997
1998 return dict_dctx_push(dctx, vendor_da, NEST_VENDOR);
1999}
2000
2001/*
2002 * Process the DEFINE command
2003 *
2004 * Which is mostly like ATTRIBUTE, but does not have a number.
2005 */
2006static int dict_read_process_define(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2007 fr_dict_attr_flags_t *base_flags)
2008{
2009 fr_dict_attr_t const *parent;
2010 fr_dict_attr_t *da = NULL;
2011 dict_tokenize_frame_t const *frame;
2012
2013 if ((argc < 2) || (argc > 3)) {
2014 fr_strerror_const("Invalid DEFINE syntax");
2015 return -1;
2016 }
2017
2018 frame = dict_dctx_unwind(dctx);
2019 if (!fr_cond_assert(frame && frame->da)) return -1; /* Should have provided us with a parent */
2020
2021 parent = frame->da;
2022
2023 /*
2024 * Members of a 'struct' MUST use MEMBER, not ATTRIBUTE.
2025 */
2026 if (parent->type == FR_TYPE_STRUCT) {
2027 fr_strerror_printf("Member %s of parent %s type 'struct' MUST use the \"MEMBER\" keyword",
2028 argv[0], parent->name);
2029 return -1;
2030 }
2031
2032 if (parent->type == FR_TYPE_UNION) {
2033 fr_strerror_printf("Parent attribute %s is of type 'union', and cannot use DEFINE for children",
2034 parent->name);
2035 return -1;
2036 }
2037
2038 /*
2039 * We don't set the attribute number before parsing the
2040 * type and flags. The number is chosen internally, and
2041 * no one should depend on it.
2042 */
2043 if (dict_read_process_common(dctx, &da, parent, argv[0], argv[1],
2044 (argc > 2) ? argv[2] : NULL, base_flags) < 0) {
2045 return -1;
2046 }
2047
2048 /*
2049 * Certain structural types MUST have numbers.
2050 */
2051 switch (da->type) {
2052 case FR_TYPE_VSA:
2053 case FR_TYPE_VENDOR:
2054 fr_strerror_printf("DEFINE cannot be used for type '%s'", argv[1]);
2055 error:
2056 talloc_free(da);
2057 return -1;
2058
2059 default:
2060 break;
2061 }
2062
2063 if (da_is_bit_field(da)) {
2064 fr_strerror_const("Bit fields can only be defined as a MEMBER of data type 'struct'");
2065 goto error;
2066 }
2067
2068#ifdef STATIC_ANALYZER
2069 if (!dctx->dict) goto error;
2070#endif
2071
2072 /*
2073 * Since there is no number, the attribute cannot be
2074 * encoded as a number.
2075 */
2076 da->flags.name_only = true;
2077
2078 /*
2079 * Add an attribute number now so the allocations occur in order
2080 */
2081 if (unlikely(dict_attr_num_init_name_only(da) < 0)) goto error;
2082
2083 /*
2084 * Set the attribute name
2085 */
2086 if (unlikely(dict_attr_finalise(&da, argv[0]) < 0)) goto error;
2087
2088 /*
2089 * Check to see if this is a duplicate attribute
2090 * and whether we should ignore it or error out...
2091 */
2092 switch (dict_attr_allow_dup(da)) {
2093 case 1:
2094 break;
2095
2096 case 0:
2097 talloc_free(da);
2098 return 0;
2099
2100 default:
2101 goto error;
2102 }
2103
2104 /*
2105 * Add the attribute we allocated earlier
2106 */
2107 switch (dict_attr_add_or_fixup(&dctx->fixup, &da)) {
2108 default:
2109 goto error;
2110
2111 /* New attribute, fixup stack */
2112 case 0:
2113 if (dict_set_value_attr(dctx, da) < 0) return -1;
2114
2115 if (da->type == FR_TYPE_TLV) {
2116 dctx->relative_attr = da;
2117 } else {
2118 dctx->relative_attr = NULL;
2119 }
2120 break;
2121
2122 /* Deferred attribute, don't begin the TLV section automatically */
2123 case 1:
2124 break;
2125 }
2126
2127 return 0;
2128}
2129
2130static int dict_read_process_end_protocol(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2131 fr_dict_attr_flags_t *base_flags);
2132
2133static int dict_read_process_end_vendor(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2134 fr_dict_attr_flags_t *base_flags);
2135
2136static int dict_read_process_end(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2137 fr_dict_attr_flags_t *base_flags)
2138{
2139 fr_dict_attr_t const *current;
2140 fr_dict_attr_t const *da;
2141 dict_tokenize_frame_t const *frame;
2142
2143 dctx->value_attr = NULL;
2144 dctx->relative_attr = NULL;
2145
2146 if (argc > 2) {
2147 fr_strerror_const("Invalid END syntax, expected END <ref>");
2148 return -1;
2149 }
2150
2151 /*
2152 * Allow for the obvious.
2153 */
2154 if (strcmp(argv[0], "PROTOCOL") == 0) return dict_read_process_end_protocol(dctx, argv + 1, argc - 1, base_flags);
2155
2156 if (strcmp(argv[0], "VENDOR") == 0) return dict_read_process_end_vendor(dctx, argv + 1, argc - 1, base_flags);
2157
2158 /*
2159 * Unwind until we hit an attribute nesting section
2160 */
2162 return -1;
2163 }
2164
2165 /*
2166 * Pop the stack to get the attribute we're ending.
2167 */
2168 current = dict_dctx_pop(dctx)->da;
2169
2170 /*
2171 * No checks on the attribute, we're just popping _A_ frame,
2172 * we don't care what attribute it represents.
2173 */
2174 if (argc == 0) return 0;
2175
2176 /*
2177 * This is where we'll have begun the previous search to
2178 * evaluate the BEGIN keyword.
2179 */
2181 if (!fr_cond_assert(frame)) return -1;
2182
2183 da = fr_dict_attr_by_oid(NULL, frame->da, argv[0]);
2184 if (!da) {
2185 fr_strerror_const_push("Failed resolving attribute in BEGIN entry");
2186 return -1;
2187 }
2188
2189 if (da != current) {
2190 fr_strerror_printf("END %s does not match previous BEGIN %s", argv[0], current->name);
2191 return -1;
2192 }
2193
2194 return 0;
2195}
2196
2197static int dict_read_process_end_protocol(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2198 UNUSED fr_dict_attr_flags_t *base_flags)
2199{
2200 fr_dict_t const *found;
2201
2202 dctx->value_attr = NULL;
2203 dctx->relative_attr = NULL;
2204
2205 if (argc != 1) {
2206 fr_strerror_const("Invalid END-PROTOCOL entry");
2207 return -1;
2208 }
2209
2210 found = dict_by_protocol_name(argv[0]);
2211 if (!found) {
2212 fr_strerror_printf("END-PROTOCOL %s does not refer to a valid protocol", argv[0]);
2213 return -1;
2214 }
2215
2216 if (found != dctx->dict) {
2217 fr_strerror_printf("END-PROTOCOL %s does not match previous BEGIN-PROTOCOL %s",
2218 argv[0], dctx->dict->root->name);
2219 return -1;
2220 }
2221
2222 /*
2223 * Unwind until we get to a BEGIN-PROTOCOL nesting.
2224 */
2226 return -1;
2227 }
2228
2229 if (found->root != CURRENT_FRAME(dctx)->da) {
2230 fr_strerror_printf("END-PROTOCOL %s does not match previous BEGIN-PROTOCOL %s", argv[0],
2231 CURRENT_FRAME(dctx)->da->name);
2232 return -1;
2233 }
2234
2235 /*
2236 * Applies fixups to any attributes added to the protocol
2237 * dictionary. Note that the finalise function prints
2238 * out the original filename / line of the error. So we
2239 * don't need to do that here.
2240 */
2241 if (dict_finalise(dctx) < 0) return -1;
2242
2244
2245 fr_assert(!dctx->stack[dctx->stack_depth].finalise);
2246 dctx->stack_depth--; /* nuke the BEGIN-PROTOCOL */
2247
2249 dctx->dict = dict_gctx->internal;
2250
2251 return 0;
2252}
2253
2254static int dict_read_process_end_vendor(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2255 UNUSED fr_dict_attr_flags_t *base_flags)
2256{
2257 fr_dict_vendor_t const *vendor;
2258
2259 dctx->value_attr = NULL;
2260 dctx->relative_attr = NULL;
2261
2262 if (argc != 1) {
2263 fr_strerror_const("END-VENDOR is missing vendor name");
2264 return -1;
2265 }
2266
2267 vendor = fr_dict_vendor_by_name(dctx->dict, argv[0]);
2268 if (!vendor) {
2269 fr_strerror_printf("Unknown vendor '%s'", argv[0]);
2270 return -1;
2271 }
2272
2273 /*
2274 * Unwind until we get to a BEGIN-VENDOR nesting.
2275 */
2276 if (!dict_dctx_unwind_until(dctx, NEST_VENDOR)) {
2277 return -1;
2278 }
2279
2280 if (vendor->pen != CURRENT_FRAME(dctx)->da->attr) {
2281 fr_strerror_printf("END-VENDOR %s does not match previous BEGIN-VENDOR %s", argv[0],
2282 CURRENT_FRAME(dctx)->da->name);
2283 return -1;
2284 }
2285
2286 fr_assert(!dctx->stack[dctx->stack_depth].finalise);
2287 dctx->stack_depth--; /* nuke the BEGIN-VENDOR */
2288
2289 return 0;
2290}
2291
2292/*
2293 * Process the ENUM command
2294 */
2295static int dict_read_process_enum(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2296 fr_dict_attr_flags_t *base_flags)
2297{
2298 fr_dict_attr_t const *parent;
2299 fr_dict_attr_t *da = NULL;
2300
2301 if (argc != 2) {
2302 fr_strerror_const("Invalid ENUM syntax");
2303 return -1;
2304 }
2305
2306 /*
2307 * Dictionaries need to have real names, not shitty ones.
2308 */
2309 if (strncmp(argv[0], "Attr-", 5) == 0) {
2310 fr_strerror_const("Invalid ENUM name");
2311 return -1;
2312 }
2313
2314#ifdef STATIC_ANALYZER
2315 if (!dctx->dict) goto error;
2316#endif
2317
2318 /*
2319 * Allocate the attribute here, and then fill in the fields
2320 * as we start parsing the various elements of the definition.
2321 */
2322 da = dict_attr_alloc_null(dctx->dict->pool, dctx->dict->proto);
2323 if (unlikely(da == NULL)) return -1;
2324 dict_attr_location_set(dctx, da);
2325 da->dict = dctx->dict;
2326
2327 /*
2328 * Set the attribute flags from the base flags.
2329 */
2330 memcpy(&da->flags, base_flags, sizeof(da->flags));
2331
2332 da->flags.name_only = true; /* values for ENUM are irrelevant */
2333 da->flags.internal = true; /* ENUMs will never get encoded into a protocol */
2334#if 0
2335 flags.is_enum = true; /* it's an enum, and can't be assigned to a #fr_pair_t */
2336#endif
2337
2338 /*
2339 * Set the base type of the attribute.
2340 */
2341 if (dict_process_type_field(dctx, argv[1], &da) < 0) {
2342 error:
2343 talloc_free(da);
2344 return -1;
2345 }
2346
2347 if (da_is_bit_field(da)) {
2348 fr_strerror_const("Bit fields can only be defined as a MEMBER of a data type 'struct'");
2349 goto error;
2350 }
2351
2352 switch (da->type) {
2353 case FR_TYPE_LEAF:
2354 break;
2355
2356 default:
2357 fr_strerror_printf("ENUMs can only be a leaf type, not %s",
2358 fr_type_to_str(da->type));
2359 goto error;
2360 }
2361
2362 parent = CURRENT_FRAME(dctx)->da;
2363 if (!parent) {
2364 fr_strerror_const("Invalid location for ENUM");
2365 goto error;
2366 }
2367
2368 /*
2369 * ENUMs cannot have a flag field, so we don't parse that.
2370 *
2371 * Maybe we do want a flag field for named time deltas?
2372 */
2373
2374 if (unlikely(dict_attr_parent_init(&da, parent) < 0)) goto error;
2375 if (unlikely(dict_attr_finalise(&da, argv[0]) < 0)) goto error;
2376
2377 /*
2378 * Add the attribute we allocated earlier
2379 */
2380 switch (dict_attr_add_or_fixup(&dctx->fixup, &da)) {
2381 default:
2382 goto error;
2383
2384 case 0:
2385 memcpy(&dctx->value_attr, &da, sizeof(da));
2386 break;
2387
2388 case 1:
2389 break;
2390 }
2391
2392 return 0;
2393}
2394
2395/*
2396 * Process the FLAGS command
2397 */
2398static int dict_read_process_flags(UNUSED dict_tokenize_ctx_t *dctx, char **argv, int argc,
2399 fr_dict_attr_flags_t *base_flags)
2400{
2401 bool sense = true;
2402
2403 if (argc == 1) {
2404 char *p;
2405
2406 p = argv[0];
2407 if (*p == '!') {
2408 sense = false;
2409 p++;
2410 }
2411
2412 if (strcmp(p, "internal") == 0) {
2413 base_flags->internal = sense;
2414 return 0;
2415 }
2416 }
2417
2418 fr_strerror_const("Invalid FLAGS syntax");
2419 return -1;
2420}
2421
2422/*
2423 * Process the MEMBER command
2424 */
2425static int dict_read_process_member(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2426 fr_dict_attr_flags_t *base_flags)
2427{
2428 fr_dict_attr_t *da = NULL;
2429
2430 if ((argc < 2) || (argc > 3)) {
2431 fr_strerror_const("Invalid MEMBER syntax");
2432 return -1;
2433 }
2434
2435 if (CURRENT_FRAME(dctx)->da->type != FR_TYPE_STRUCT) {
2436 fr_strerror_printf("MEMBER can only be used for ATTRIBUTEs of type 'struct', not for data type %s",
2437 fr_type_to_str(CURRENT_FRAME(dctx)->da->type));
2438 return -1;
2439 }
2440
2441 /*
2442 * Check if the parent 'struct' is fixed size. And if
2443 * so, complain if we're adding a variable sized member.
2444 */
2445 if (CURRENT_FRAME(dctx)->struct_is_closed) {
2446 fr_strerror_printf("Cannot add MEMBER to 'struct' %s after a variable sized member %s",
2447 CURRENT_FRAME(dctx)->da->name,
2448 CURRENT_FRAME(dctx)->struct_is_closed->name);
2449 return -1;
2450 }
2451
2452 /*
2453 * We don't set the attribute number before parsing the
2454 * type and flags. The number is chosen internally, and
2455 * no one should depend on it.
2456 *
2457 * Although _arguably_, it may be useful to know which
2458 * field this is, 0..N?
2459 */
2460 if (dict_read_process_common(dctx, &da, CURRENT_FRAME(dctx)->da, argv[0], argv[1],
2461 (argc > 2) ? argv[2] : NULL, base_flags) < 0) {
2462 return -1;
2463 }
2464
2465#ifdef STATIC_ANALYZER
2466 if (!dctx->dict) goto error;
2467#endif
2468
2469 /*
2470 * If our parent is a known width struct, then we're
2471 * allowed to be variable width. The parent might just
2472 * have a "length=16" prefix, which lets its children be
2473 * variable sized.
2474 */
2475
2476 /*
2477 * Double check any bit field magic
2478 */
2479 if (CURRENT_FRAME(dctx)->member_num > 0) {
2480 fr_dict_attr_t const *previous;
2481
2482 previous = dict_attr_child_by_num(CURRENT_FRAME(dctx)->da,
2483 CURRENT_FRAME(dctx)->member_num);
2484 /*
2485 * Check that the previous bit field ended on a
2486 * byte boundary.
2487 *
2488 * Note that the previous attribute might be a deferred TLV, in which case it doesn't
2489 * exist. That's fine.
2490 */
2491 if (previous && da_is_bit_field(previous)) {
2492 /*
2493 * This attribute is a bit field. Keep
2494 * track of where in the byte we are
2495 * located.
2496 */
2497 if (da_is_bit_field(da)) {
2498 da->flags.flag_byte_offset = (da->flags.length + previous->flags.flag_byte_offset) & 0x07;
2499
2500 } else {
2501 if (previous->flags.flag_byte_offset != 0) {
2502 fr_strerror_printf("Previous bitfield %s did not end on a byte boundary",
2503 previous->name);
2504 error:
2505 talloc_free(da);
2506 return -1;
2507 }
2508 }
2509 }
2510 }
2511
2512 /*
2513 * Ensure that no previous child has "key" or "length" set.
2514 */
2515 if (da->type == FR_TYPE_TLV) {
2516 fr_dict_attr_t const *key;
2517 int i;
2518
2519 /*
2520 * @todo - cache the key field in the stack frame, so we don't have to loop over the children.
2521 */
2522 for (i = 1; i <= CURRENT_FRAME(dctx)->member_num; i++) {
2523 key = dict_attr_child_by_num(CURRENT_FRAME(dctx)->da, i);
2524 if (!key) continue; /* really should be WTF? */
2525
2526 /*
2527 * @todo - we can allow this if the _rest_ of the struct is fixed size, i.e. if
2528 * there is a key field, and then the union is fixed size.
2529 */
2530 if (fr_dict_attr_is_key_field(key)) {
2531 fr_strerror_printf("'struct' %s has a 'key' field %s, and cannot end with a TLV %s",
2532 CURRENT_FRAME(dctx)->da->name, key->name, argv[0]);
2533 goto error;
2534 }
2535
2536 if (da_is_length_field(key)) {
2537 fr_strerror_printf("'struct' %s has a 'length' field %s, and cannot end with a TLV %s",
2538 CURRENT_FRAME(dctx)->da->name, key->name, argv[0]);
2539 goto error;
2540 }
2541 }
2542
2543 /*
2544 * TLVs are variable sized, and close the parent struct.
2545 */
2546 CURRENT_FRAME(dctx)->struct_is_closed = da;
2547 }
2548
2549 /*
2550 * Unions close the parent struct, even if they're fixed size. For now, the struct to/from
2551 * network code assumes that a union is the last member of a structure.
2552 */
2553 if (da->type == FR_TYPE_UNION) {
2554 CURRENT_FRAME(dctx)->struct_is_closed = da;
2555 }
2556
2557 if (unlikely(dict_attr_num_init(da, CURRENT_FRAME(dctx)->member_num + 1) < 0)) goto error;
2558 if (unlikely(dict_attr_finalise(&da, argv[0]) < 0)) goto error;
2559
2560 /*
2561 * Check to see if this is a duplicate attribute
2562 * and whether we should ignore it or error out...
2563 */
2564 switch (dict_attr_allow_dup(da)) {
2565 case 1:
2566 break;
2567
2568 case 0:
2569 talloc_free(da);
2570 return 0;
2571
2572 default:
2573 goto error;
2574 }
2575
2576 switch (dict_attr_add_or_fixup(&dctx->fixup, &da)) {
2577 default:
2578 goto error;
2579
2580 case 1:
2581 /*
2582 * @todo - a MEMBER can theoretically have a "ref=..", though non currently do.
2583 *
2584 * If the ref is deferred, then we cannot finalise the parent struct until we have
2585 * resolved the reference. But the "finalise struct on fixup" code isn't written. So
2586 * instead of silently doing the wrong thing, we just return an error.
2587 */
2588 fr_strerror_printf("Cannot have MEMBER with deferred ref=...");
2589 return -1;
2590
2591 case 0:
2592 /*
2593 * New attribute - avoid lots of indentation.
2594 */
2595 break;
2596 }
2597
2598 /*
2599 * Check if this MEMBER closes the structure.
2600 *
2601 * Close this struct if the child struct is variable sized. For now, it we only support
2602 * child structs at the end of the parent.
2603 *
2604 * The solution is to update the unwind() function to check if the da we've
2605 * unwound to is a struct, and then if so... get the last child, and mark it
2606 * closed.
2607 *
2608 * @todo - a MEMBER which is of type 'struct' and has 'clone=foo', we delay the clone
2609 * until after all of the dictionaries have been loaded. As such, this attribute
2610 * is unknown width, and MUST be at the end of the parent structure.
2611 *
2612 * If the cloned MEMBER is in the middle of a structure, then the user will get an opaque
2613 * error. But that case should be rare.
2614 */
2615 if (!da->flags.is_known_width) {
2616 /*
2617 * The child is unknown width, but we were told that the parent has known width.
2618 * That's an error.
2619 */
2620 if (CURRENT_FRAME(dctx)->da->flags.length) {
2621 fr_strerror_printf("'struct' %s has fixed size %u, but member %s is of unknown size",
2622 CURRENT_FRAME(dctx)->da->name, CURRENT_FRAME(dctx)->da->flags.length,
2623 argv[0]);
2624 return -1;
2625 }
2626
2627 /*
2628 * Mark the structure as closed by this attribute. And then set the size to
2629 * zero, for "unknown size".
2630 */
2631 CURRENT_FRAME(dctx)->struct_is_closed = da;
2632 CURRENT_FRAME(dctx)->struct_size = 0;
2633
2634 /*
2635 * A 'struct' can have a MEMBER of type 'tlv', but ONLY
2636 * as the last entry in the 'struct'. If we see that,
2637 * set the previous attribute to the TLV we just added.
2638 * This allows the children of the TLV to be parsed as
2639 * partial OIDs, so we don't need to know the full path
2640 * to them.
2641 */
2642 if (da->type == FR_TYPE_TLV) {
2643 dctx->relative_attr = da;
2644 if (dict_dctx_push(dctx, dctx->relative_attr, NEST_NONE) < 0) return -1;
2645 }
2646
2647 } else if (CURRENT_FRAME(dctx)->da->flags.length) {
2648 /*
2649 * The parent is fixed size, so we track the length of the children.
2650 */
2651 CURRENT_FRAME(dctx)->struct_size += da->flags.length;
2652
2653 /*
2654 * Adding this child may result in an overflow, so we check that.
2655 */
2656 if (CURRENT_FRAME(dctx)->struct_size > CURRENT_FRAME(dctx)->da->flags.length) {
2657 fr_strerror_printf("'struct' %s has fixed size %u, but member %s overflows that length",
2658 CURRENT_FRAME(dctx)->da->name, CURRENT_FRAME(dctx)->da->flags.length,
2659 argv[0]);
2660 return -1;
2661 }
2662 }
2663
2664 /*
2665 * Now that we know everything is OK, we can increase the number.
2666 */
2667 CURRENT_FRAME(dctx)->member_num++;
2668
2669 /*
2670 * Set or clear the attribute for VALUE statements.
2671 */
2672 return dict_set_value_attr(dctx, da);
2673}
2674
2675
2676/** Process a value alias
2677 *
2678 */
2679static int dict_read_process_value(dict_tokenize_ctx_t *dctx, char **argv, int argc,
2680 UNUSED fr_dict_attr_flags_t *base_flags)
2681{
2682 fr_dict_attr_t *da;
2684 size_t enum_len;
2685 fr_dict_attr_t const *parent = CURRENT_FRAME(dctx)->da;
2686 fr_dict_attr_t const *enumv = NULL;
2687
2688 if (argc != 3) {
2689 fr_strerror_const("Invalid VALUE syntax");
2690 return -1;
2691 }
2692
2693 /*
2694 * Most VALUEs are bunched together by ATTRIBUTE. We can
2695 * save a lot of lookups on dictionary initialization by
2696 * caching the last attribute for a VALUE.
2697 *
2698 * If it's not the same, we look up the attribute in the
2699 * current context, which is generally:
2700 *
2701 * * the current attribute of type `struct`
2702 * * if no `struct`, then the VENDOR for VSAs
2703 * * if no VENDOR, then the dictionary root
2704 */
2705 if (!dctx->value_attr || (strcasecmp(argv[0], dctx->value_attr->name) != 0)) {
2706 fr_dict_attr_t const *tmp;
2707
2708 if (!(tmp = fr_dict_attr_by_oid(NULL, parent, argv[0]))) goto fixup;
2709 dctx->value_attr = fr_dict_attr_unconst(tmp);
2710 }
2711 da = dctx->value_attr;
2712
2713 /*
2714 * Verify the enum name matches the expected from.
2715 */
2716 enum_len = strlen(argv[1]);
2717 if (fr_dict_enum_name_from_substr(NULL, NULL, &FR_SBUFF_IN(argv[1], enum_len), NULL) != (fr_slen_t) enum_len) {
2718 fr_strerror_printf_push("Invalid VALUE name '%s' for attribute '%s'", argv[1], da->name);
2719 return -1;
2720 }
2721
2722 /*
2723 * enum names cannot be integers. People should just use the integer instead.
2724 *
2725 * But what about IPv6 addresses, which also use a "::" prefix?
2726 *
2727 * The ::FOO addresses were historically part of the "ipv4 compatible ipv6 address" range
2728 * "::0.0.0.0/96". That range has since been deprecated, and the "::FOO" range is tracked in the
2729 * IANA Special-Purpose Address Registry. That lists three things beginning with ::
2730 *
2731 * * ::/128 - unspecified address (i.e. ::0/128).
2732 * * ::1/128 - Loopback address
2733 * * ::ffff:0:0/96 - IPv4-mapped address.
2734 *
2735 * Since IPv6 addresses are 128 bits, the first two are just ::0 and ::1. No other possibilities
2736 * exist.
2737 *
2738 * For the range "::ffff:0:0/96", a value such as "::ffff:192.168.1.2 is not a valid enum name.
2739 * It contains an extra ':' (and MUST contain the extra ':'), and the ':' is not allowed in an
2740 * enum name.
2741 *
2742 * IANA could assign other values in the :: range, but this seems unlikely.
2743 *
2744 * As a result, the only overlap between enum ::FOO and IPv6 addresses is the single case of ::1.
2745 * This check disallows that.
2746 */
2747 if (fr_sbuff_adv_past_allowed( &FR_SBUFF_IN(argv[1], enum_len), SIZE_MAX, sbuff_char_class_int, NULL) == enum_len) {
2748 fr_strerror_printf("Invalid VALUE name '%s' for attribute '%s' - the name cannot be an integer", argv[1], da->name);
2749 return -1;
2750 }
2751
2752 /*
2753 * Remember which attribute is associated with this
2754 * value. This allows us to define enum
2755 * values before the attribute exists, and fix them
2756 * up later.
2757 */
2758 if (!da) {
2759 fixup:
2760 if (!fr_cond_assert_msg(dctx->fixup.pool, "fixup pool context invalid")) return -1;
2761
2763 CURRENT_FILENAME(dctx), CURRENT_LINE(dctx),
2764 argv[0], strlen(argv[0]),
2765 argv[1], strlen(argv[1]),
2766 argv[2], strlen(argv[2]), parent) < 0) {
2767 fr_strerror_const("Out of memory");
2768 return -1;
2769 }
2770 return 0;
2771 }
2772
2773 /*
2774 * Only a leaf types can have values defined.
2775 */
2776 if (!fr_type_is_leaf(da->type)) {
2777 fr_strerror_printf("Cannot define VALUE for attribute '%s' of data type '%s'", da->name,
2778 fr_type_to_str(da->type));
2779 return -1;
2780 }
2781
2782 /*
2783 * Pass in the DA. The value-box parsing functions will figure out where the enums are found.
2784 */
2785 if (da->type == FR_TYPE_ATTR) enumv = da;
2786
2787 if (fr_value_box_from_str(NULL, &value, da->type, enumv,
2788 argv[2], strlen(argv[2]),
2789 NULL) < 0) {
2790 fr_strerror_printf_push("Invalid VALUE '%s' for attribute '%s' of data type '%s'",
2791 argv[2],
2792 da->name,
2793 fr_type_to_str(da->type));
2794 return -1;
2795 }
2796
2797 if (fr_dict_enum_add_name(da, argv[1], &value, false, true) < 0) {
2799 return -1;
2800 }
2802
2803 return 0;
2804}
2805
2806/*
2807 * Process the VENDOR command
2808 */
2809static int dict_read_process_vendor(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
2810{
2811 unsigned int value;
2812 int type, length;
2813 bool continuation = false;
2814 fr_dict_vendor_t const *dv;
2815 fr_dict_vendor_t *mutable;
2816 fr_dict_t *dict = dctx->dict;
2817
2818 dctx->value_attr = NULL;
2819 dctx->relative_attr = NULL;
2820
2821 if ((argc < 2) || (argc > 3)) {
2822 fr_strerror_const("Invalid VENDOR syntax");
2823 return -1;
2824 }
2825
2826 /*
2827 * Validate all entries
2828 */
2829 if (!dict_read_sscanf_i(&value, argv[1])) {
2830 fr_strerror_const("Invalid number in VENDOR");
2831 return -1;
2832 }
2833
2834 /*
2835 * Look for a format statement. Allow it to over-ride the hard-coded formats below.
2836 */
2837 if (argc == 3) {
2838 if (dict_read_parse_format(argv[2], &type, &length, &continuation) < 0) return -1;
2839
2840 } else {
2841 type = length = 1;
2842 }
2843
2844 /* Create a new VENDOR entry for the list */
2845 if (dict_vendor_add(dict, argv[0], value) < 0) return -1;
2846
2848 if (!dv) {
2849 fr_strerror_const("Failed adding format for VENDOR");
2850 return -1;
2851 }
2852
2853 mutable = UNCONST(fr_dict_vendor_t *, dv);
2854 mutable->type = type;
2855 mutable->length = length;
2856 mutable->continuation = continuation;
2857
2858 return 0;
2859}
2860
2861/** The main protocols that we care about.
2862 *
2863 * Not all of them are listed here, but that should be fine.
2864 *
2865 */
2867 { L("RADIUS"), FR_DICT_PROTO_RADIUS },
2868 { L("DHCPv4"), FR_DICT_PROTO_DHCPv4 },
2869 { L("DHCPv6"), FR_DICT_PROTO_DHCPv6 },
2870 { L("Ethernet"), FR_DICT_PROTO_ETHERNET },
2871 { L("TACACS"), FR_DICT_PROTO_TACACS },
2872 { L("VMPS"), FR_DICT_PROTO_VMPS },
2873 { L("SNMP"), FR_DICT_PROTO_SNMP },
2874 { L("ARP"), FR_DICT_PROTO_ARP },
2875 { L("TFTP"), FR_DICT_PROTO_TFTP },
2876 { L("TLS"), FR_DICT_PROTO_TLS },
2877 { L("DNS"), FR_DICT_PROTO_DNS },
2878 { L("LDAP"), FR_DICT_PROTO_LDAP },
2879 { L("BFD"), FR_DICT_PROTO_BFD },
2880 { L("CRL"), FR_DICT_PROTO_CRL },
2881};
2883
2884/** Register the specified dictionary as a protocol dictionary
2885 *
2886 * Allows vendor and TLV context to persist across $INCLUDEs
2887 */
2888static int dict_read_process_protocol(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flag)
2889{
2890 unsigned int value;
2891 unsigned int type_size = 0;
2892 fr_dict_t *dict;
2893 unsigned int required_value;
2894 char const *required_name;
2895 bool require_dl = false;
2896 bool string_based = false;
2897
2898 /*
2899 * We cannot define a PROTOCOL inside of another protocol.
2900 */
2901 if (CURRENT_FRAME(dctx)->nest != NEST_TOP) {
2902 fr_strerror_const("PROTOCOL definitions cannot occur inside of any other BEGIN/END block");
2903 return -1;
2904 }
2905
2906 dctx->value_attr = NULL;
2907 dctx->relative_attr = NULL;
2908
2909 if ((argc < 2) || (argc > 3)) {
2910 fr_strerror_const("Missing arguments after PROTOCOL. Expected PROTOCOL <name> <number>");
2911 return -1;
2912 }
2913
2914 /*
2915 * Validate all entries
2916 */
2917 if (!dict_read_sscanf_i(&value, argv[1])) {
2918 fr_strerror_printf("Invalid number '%s' following PROTOCOL", argv[1]);
2919 return -1;
2920 }
2921
2922 /*
2923 * 255 protocols FR_TYPE_GROUP type_size hack
2924 */
2925 if (!value) {
2926 fr_strerror_printf("Invalid value '%u' following PROTOCOL", value);
2927 return -1;
2928 }
2929
2930 /*
2931 * While the numbers are in the dictionaries, the administrator cannot change "RADIUS" to be a
2932 * different number. Similarly, they can't assign the RADIUS number to a different protocol.
2933 */
2934 required_value = fr_table_value_by_str(dict_proto_table, argv[0], 0);
2935 if (required_value && (required_value != value)) {
2936 fr_strerror_printf("Invalid value '%u' following PROTOCOL - expected '%u'", value, required_value);
2937 return -1;
2938 }
2939
2940 /*
2941 * And the administrator can't define the name to be a different number.
2942 */
2943 required_name = fr_table_str_by_value(dict_proto_table, value, NULL);
2944 if (required_name && (strcasecmp(required_name, argv[0]) != 0)) {
2945 fr_strerror_printf("Invalid value '%u' for PROTOCOL '%s' - that value is already used by '%s'",
2946 value, argv[0], required_name);
2947 return -1;
2948 }
2949
2950 /*
2951 * Look for a format statement. This may specify the
2952 * type length of the protocol's types.
2953 */
2954 if (argc == 3) {
2955 char const *p;
2956 char *q;
2957
2958 /*
2959 * For now, we don't allow multiple options here.
2960 *
2961 * @todo - allow multiple options.
2962 */
2963 if (strcmp(argv[2], "verify=lib") == 0) {
2964 require_dl = true;
2965 goto post_option;
2966 }
2967
2968 if (strcmp(argv[2], "format=string") == 0) {
2969 type_size = 4;
2970 string_based = true;
2971 goto post_option;
2972 }
2973
2974 if (strncasecmp(argv[2], "format=", 7) != 0) {
2975 fr_strerror_printf("Invalid format for PROTOCOL. Expected 'format=', got '%s'", argv[2]);
2976 return -1;
2977 }
2978 p = argv[2] + 7;
2979
2980 type_size = strtoul(p, &q, 10);
2981 if (q != (p + strlen(p))) {
2982 fr_strerror_printf("Found trailing garbage '%s' after format specifier", p);
2983 return -1;
2984 }
2985 }
2986post_option:
2987
2988 /*
2989 * Cross check name / number.
2990 */
2991 dict = dict_by_protocol_name(argv[0]);
2992 if (dict) {
2993#ifdef STATIC_ANALYZER
2994 if (!dict->root) return -1;
2995#endif
2996
2997 if (dict->root->attr != value) {
2998 fr_strerror_printf("Conflicting numbers %u vs %u for PROTOCOL \"%s\"",
2999 dict->root->attr, value, dict->root->name);
3000 return -1;
3001 }
3002
3003 } else if ((dict = dict_by_protocol_num(value)) != NULL) {
3004#ifdef STATIC_ANALYZER
3005 if (!dict->root || !dict->root->name || !argv[0]) return -1;
3006#endif
3007
3008 if (strcasecmp(dict->root->name, argv[0]) != 0) {
3009 fr_strerror_printf("Conflicting names current \"%s\" vs new \"%s\" for PROTOCOL %u",
3010 dict->root->name, argv[0], dict->root->attr);
3011 return -1;
3012 }
3013 }
3014
3015 /*
3016 * And check types no matter what.
3017 */
3018 if (dict) {
3019 if (type_size && (dict->root->flags.type_size != type_size)) {
3020 fr_strerror_printf("Conflicting flags for PROTOCOL \"%s\" (current %d versus new %u)",
3021 dict->root->name, dict->root->flags.type_size, type_size);
3022 return -1;
3023 }
3024
3025 /*
3026 * Do NOT talloc_free() dict on error.
3027 */
3028 return dict_dctx_push(dctx, dict->root, NEST_NONE);
3029 }
3030
3032 if (!dict) return -1;
3033
3034 /*
3035 * Try to load protocol-specific validation routines.
3036 * Some protocols don't need them, so it's OK if the
3037 * validation routines don't exist.
3038 */
3039 if ((dict_dlopen(dict, argv[0]) < 0) && require_dl) {
3040 error:
3042 return -1;
3043 }
3044
3045 /*
3046 * Set the root attribute with the protocol name
3047 */
3048 if (dict_root_set(dict, argv[0], value) < 0) goto error;
3049
3050 if (dict_protocol_add(dict) < 0) goto error;
3051
3052 dict->string_based = string_based;
3053 if (type_size) {
3054 fr_dict_attr_t *mutable;
3055
3056 mutable = UNCONST(fr_dict_attr_t *, dict->root);
3057 mutable->flags.type_size = type_size;
3058 mutable->flags.length = 1; /* who knows... */
3059 }
3060
3061 /*
3062 * Make the root available on the stack, in case
3063 * something wants to begin it. Note that we mark it as
3064 * NONE, so that it can be cleaned up by anything.
3065 *
3066 * This stack entry is just a place-holder so that the
3067 * BEGIN statement can find the dictionary.
3068 */
3069 if (unlikely(dict_dctx_push(dctx, dict->root, NEST_NONE) < 0)) goto error;
3070
3071 return 0;
3072}
3073
3074/** Maintain a linked list of filenames which we've seen loading this dictionary
3075 *
3076 * This is used for debug messages, so we have a copy of the original file path
3077 * that we can reference from fr_dict_attr_t without having the memory bloat of
3078 * assigning a buffer to every attribute.
3079 */
3080static inline int dict_filename_add(char **filename_out, fr_dict_t *dict, char const *filename,
3081 char const *src_file, int src_line)
3082{
3084
3085 file = talloc_zero(dict, fr_dict_filename_t);
3086 if (unlikely(!file)) {
3087 oom:
3088 fr_strerror_const("Out of memory");
3089 return -1;
3090 }
3091 *filename_out = file->filename = talloc_strdup(file, filename);
3092 if (unlikely(!*filename_out)) goto oom;
3093
3094 if (src_file) {
3095 file->src_line = src_line;
3096 file->src_file = talloc_strdup(file, src_file);
3097 if (!file->src_file) goto oom;
3098 }
3099
3100 fr_dlist_insert_tail(&dict->filenames, file);
3101
3102 return 0;
3103}
3104
3105/** See if we have already loaded the file,
3106 *
3107 */
3108static inline bool dict_filename_loaded(fr_dict_t const *dict, char const *filename,
3109 char const *src_file, int src_line)
3110{
3112
3113 for (file = (fr_dict_filename_t *) fr_dlist_head(&dict->filenames);
3114 file != NULL;
3115 file = (fr_dict_filename_t *) fr_dlist_next(&dict->filenames, &file->entry)) {
3116 if (file->src_file && src_file) {
3117 if (file->src_line != src_line) continue;
3118 if (strcmp(file->src_file, src_file) != 0) continue;
3119 }
3120
3121 if (strcmp(file->filename, filename) == 0) return true; /* this should always be true */
3122 }
3123
3124 return false;
3125}
3126
3127bool fr_dict_filename_loaded(fr_dict_t const *dict, char const *dir, char const *filename)
3128{
3129 char buffer[PATH_MAX];
3130
3131 if (!dict) return false;
3132
3133 snprintf(buffer, sizeof(buffer), "%s/%s", dir, filename);
3134
3135 return dict_filename_loaded(dict, buffer, NULL, 0);
3136}
3137
3138/** Process an inline BEGIN PROTOCOL block
3139 *
3140 * This function is called *after* the PROTOCOL handler.
3141 */
3143{
3145 fr_assert(CURRENT_DA(dctx)->flags.is_root);
3146
3147 /*
3148 * Rewrite it in place.
3149 */
3150 CURRENT_FRAME(dctx)->nest = NEST_PROTOCOL;
3151 dctx->dict = CURRENT_DA(dctx)->dict;
3152
3153 return 0;
3154}
3155
3156/** Keyword parser
3157 *
3158 * @param[in] dctx containing the dictionary we're currently parsing.
3159 * @param[in] argv arguments to the keyword.
3160 * @param[in] argc number of arguments.
3161 * @param[in] base_flags set in the context of the current file.
3162 * @return
3163 * - 0 on success.
3164 * - -1 on failure.
3165 */
3166typedef int (*fr_dict_keyword_parse_t)(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags);
3167
3168/** Pushes a new frame onto the top of the stack based on the current frame
3169 *
3170 * Whenever a protocol, vendor, or attribute is defined in the dictionary it either mutates or
3171 * pushes a new NONE frame onto the stack. This holds the last defined object at a given level
3172 * of nesting.
3173 *
3174 * This function is used to push an additional frame onto the stack, effectively entering the
3175 * context of the last defined object at a given level of nesting
3176 *
3177 * @param[in] dctx Contains the current state of the dictionary parser.
3178 * Used to track what PROTOCOL, VENDOR or TLV block
3179 * we're in.
3180 * @return
3181 * - 0 on success.
3182 * - -1 on failure.
3183 */
3185
3186typedef struct {
3187 fr_dict_keyword_parse_t parse; //!< Function to parse the keyword with.
3188 fr_dict_section_begin_t begin; //!< Can have a BEGIN prefix
3190
3191typedef struct {
3192 fr_table_elem_name_t name; //!< Name of the keyword, e.g. "ATTRIBUTE"
3193 fr_dict_keyword_parser_t value; //!< Value to return from lookup.
3195
3197 fr_dict_keyword, fr_dict_keyword_parser_t const *, fr_dict_keyword_parser_t const *)
3198
3199/** Parse a dictionary file
3200 *
3201 * @param[in] dctx Contains the current state of the dictionary parser.
3202 * Used to track what PROTOCOL, VENDOR or TLV block
3203 * we're in. Block context changes in $INCLUDEs should
3204 * not affect the context of the including file.
3205 * @param[in] dir Directory containing the dictionary we're loading.
3206 * @param[in] filename we're parsing.
3207 * @param[in] src_file The including file.
3208 * @param[in] src_line Line on which the $INCLUDE or $NCLUDE- statement was found.
3209 * @return
3210 * - 0 on success.
3211 * - -1 on failure.
3212 */
3213static int _dict_from_file(dict_tokenize_ctx_t *dctx,
3214 char const *dir, char const *filename,
3215 char const *src_file, int src_line)
3216{
3217 static fr_dict_keyword_t const keywords[] = {
3218 { L("ALIAS"), { .parse = dict_read_process_alias } },
3219 { L("ATTRIBUTE"), { .parse = dict_read_process_attribute } },
3220 { L("BEGIN-PROTOCOL"), { .parse = dict_read_process_begin_protocol } },
3221 { L("BEGIN-VENDOR"), { .parse = dict_read_process_begin_vendor } },
3222 { L("DEFINE"), { .parse = dict_read_process_define } },
3223 { L("END"), { .parse = dict_read_process_end } },
3224 { L("END-PROTOCOL"), { .parse = dict_read_process_end_protocol } },
3225 { L("END-VENDOR"), { .parse = dict_read_process_end_vendor } },
3226 { L("ENUM"), { .parse = dict_read_process_enum } },
3227 { L("FLAGS"), { .parse = dict_read_process_flags } },
3228 { L("MEMBER"), { .parse = dict_read_process_member } },
3229 { L("PROTOCOL"), { .parse = dict_read_process_protocol, .begin = dict_begin_protocol }},
3230 { L("VALUE"), { .parse = dict_read_process_value } },
3231 { L("VENDOR"), { .parse = dict_read_process_vendor } },
3232 };
3233
3234 FILE *fp;
3235 char filename_buf[256];
3236 char buf[256];
3237 char *p;
3238 int line = 0;
3239
3240 struct stat statbuf;
3241 char *argv[DICT_MAX_ARGV];
3242 int argc;
3243
3244 int stack_depth = dctx->stack_depth;
3245 char *old_filename;
3246 int old_line;
3247
3248 /*
3249 * Base flags are only set for the current file
3250 */
3251 fr_dict_attr_flags_t base_flags = {};
3252
3253 if (!fr_cond_assert(!dctx->dict->root || CURRENT_FRAME(dctx)->da)) return -1;
3254
3255 /*
3256 * The filename is relative to the current directory.
3257 *
3258 * Ensure that the directory name doesn't end with 2 '/',
3259 * and then create the full path from dir + filename.
3260 */
3261 if (FR_DIR_IS_RELATIVE(filename)) {
3262 char const *q;
3263 bool slash = false;
3264
3265 if (!dir) {
3266 fr_strerror_printf("Error reading dictionary: No directory was supplied");
3267 return -1;
3268 }
3269
3270 if ((strlen(dir) + 2 + strlen(filename)) > sizeof(filename_buf)) {
3271 fr_strerror_printf("%s: Filename name too long", "Error reading dictionary");
3272 return -1;
3273 }
3274
3275 /*
3276 * We either have to do strcpy + strrchr(), or manual checks.
3277 */
3278 p = filename_buf;
3279 for (q = dir; *q != '\0'; q++) {
3280 if (*q != '/') {
3281 *(p++) = *q;
3282 slash = false;
3283 continue;
3284 }
3285
3286 /*
3287 * Suppress multiple consecutive slashes.
3288 */
3289 if (slash) continue;
3290
3291 *(p++) = *q;
3292 slash = true;
3293 }
3294
3295 if (!slash) *(p++) = '/';
3296 strcpy(p, filename);
3297
3298 filename = filename_buf;
3299 }
3300 /*
3301 * Else we ignore the input directory. We also assume
3302 * that the filename is normalized, and therefore don't
3303 * change it.
3304 */
3305
3306 /*
3307 * See if we have already loaded this filename. If so, suppress it.
3308 */
3309 if (unlikely(dict_filename_loaded(dctx->dict, filename, src_file, src_line))) {
3310 fr_strerror_printf("ERROR - we have a recursive $INCLUDE or load of file %s", filename);
3311 return -1;
3312 }
3313
3314
3315 if ((fp = fopen(filename, "r")) == NULL) {
3316 if (!src_file) {
3317 fr_strerror_printf("Couldn't open dictionary %s: %s", fr_syserror(errno), filename);
3318 } else {
3319 fr_strerror_printf("Error reading dictionary: %s[%d]: Couldn't open dictionary '%s': %s",
3320 fr_cwd_strip(src_file), src_line, filename,
3321 fr_syserror(errno));
3322 }
3323 return -2;
3324 }
3325
3326 /*
3327 * If fopen works, this works.
3328 */
3329 if (fstat(fileno(fp), &statbuf) < 0) {
3330 fr_strerror_printf("Failed stating dictionary \"%s\" - %s", filename, fr_syserror(errno));
3331
3332 perm_error:
3333 fclose(fp);
3334 return -1;
3335 }
3336
3337 if (!S_ISREG(statbuf.st_mode)) {
3338 fr_strerror_printf("Dictionary is not a regular file: %s", filename);
3339 goto perm_error;
3340 }
3341
3342 /*
3343 * Globally writable dictionaries means that users can control
3344 * the server configuration with little difficulty.
3345 */
3346#ifdef S_IWOTH
3347 if (dict_gctx->perm_check && ((statbuf.st_mode & S_IWOTH) != 0)) {
3348 fr_strerror_printf("Dictionary is globally writable: %s. "
3349 "Refusing to start due to insecure configuration", filename);
3350 goto perm_error;
3351 }
3352#endif
3353
3354 old_filename = dctx->filename;
3355 old_line = dctx->line;
3356
3357 /*
3358 * Now that we've opened the file, copy the filename into the dictionary and add it to the ctx
3359 * This string is safe to assign to the filename pointer in any attributes added beneath the
3360 * dictionary.
3361 */
3362 if (unlikely(dict_filename_add(&dctx->filename, dctx->dict, filename, src_file, src_line) < 0)) {
3363 goto perm_error;
3364 }
3365
3366 CURRENT_FRAME(dctx)->filename = dctx->filename;
3367 CURRENT_FRAME(dctx)->line = line;
3368
3369 while (fgets(buf, sizeof(buf), fp) != NULL) {
3370 bool do_begin = false;
3371 fr_dict_keyword_parser_t const *parser;
3372 char **argv_p = argv;
3373
3374 dctx->line = ++line;
3375
3376 switch (buf[0]) {
3377 case '#':
3378 case '\0':
3379 case '\n':
3380 case '\r':
3381 continue;
3382 }
3383
3384 /*
3385 * Comment characters should NOT be appearing anywhere but
3386 * as start of a comment;
3387 */
3388 p = strchr(buf, '#');
3389 if (p) *p = '\0';
3390
3391 argc = fr_dict_str_to_argv(buf, argv, DICT_MAX_ARGV);
3392 if (argc == 0) continue;
3393
3394 if (argc == 1) {
3395 /*
3396 * Be nice.
3397 */
3398 if ((strcmp(argv[0], "BEGIN") == 0) ||
3399 (fr_dict_keyword(&parser, keywords, NUM_ELEMENTS(keywords), argv_p[0], NULL))) {
3400 fr_strerror_printf("Keyword %s is missing all of its arguments", argv[0]);
3401 } else {
3402 fr_strerror_printf("Invalid syntax - unknown keyword %s", argv[0]);
3403 }
3404
3405 error:
3406 fr_strerror_printf_push("Failed parsing dictionary at %s[%d]", fr_cwd_strip(filename), line);
3407 fclose(fp);
3408
3409 dctx->filename = CURRENT_FRAME(dctx)->filename = old_filename;
3410 dctx->line = CURRENT_FRAME(dctx)->line = old_line;
3411 return -1;
3412 }
3413
3414 /*
3415 * Special prefix for "beginnable" keywords.
3416 * These are keywords that can automatically change
3417 * the context of subsequent definitions if they're
3418 * prefixed with a BEGIN keyword.
3419 */
3420 if (strcasecmp(argv_p[0], "BEGIN") == 0) {
3421 do_begin = true;
3422 argv_p++;
3423 argc--;
3424 }
3425
3426 if (fr_dict_keyword(&parser, keywords, NUM_ELEMENTS(keywords), argv_p[0], NULL)) {
3427 /*
3428 * We are allowed to have attributes
3429 * named for keywords. Most notably
3430 * "value". If there's no such attribute
3431 * 'value', then the user will get a
3432 * descriptive error.
3433 */
3434 if (do_begin && !parser->begin) {
3435 goto process_begin;
3436 }
3437
3438 if (unlikely(parser->parse(dctx, argv_p + 1 , argc - 1, &base_flags) < 0)) goto error;
3439
3440 /*
3441 * We've processed the definition, now enter the section
3442 */
3443 if (do_begin && unlikely(parser->begin(dctx) < 0)) goto error;
3444 continue;
3445 }
3446
3447 /*
3448 * It's a naked BEGIN keyword
3449 */
3450 if (do_begin) {
3451 process_begin:
3452 if (unlikely(dict_read_process_begin(dctx, argv_p, argc, &base_flags) < 0)) goto error;
3453 continue;
3454 }
3455
3456 /*
3457 * See if we need to import another dictionary.
3458 */
3459 if (strncasecmp(argv_p[0], "$INCLUDE", 8) == 0) {
3460 /*
3461 * Included files operate on a copy of the context.
3462 *
3463 * This copy means that they inherit the
3464 * current context, including parents,
3465 * TLVs, etc. But if the included file
3466 * leaves a "dangling" TLV or "last
3467 * attribute", then it won't affect the
3468 * parent.
3469 */
3470 if (dict_read_process_include(dctx, argv_p, argc, dir) < 0) goto error;
3471 continue;
3472 } /* $INCLUDE */
3473
3474 /*
3475 * Any other string: We don't recognize it.
3476 */
3477 fr_strerror_printf("Invalid keyword '%s'", argv_p[0]);
3478 goto error;
3479 }
3480
3481 /*
3482 * Unwind until the stack depth matches what we had on input.
3483 */
3484 while (dctx->stack_depth > stack_depth) {
3485 dict_tokenize_frame_t *frame = CURRENT_FRAME(dctx);
3486
3487 if (frame->nest == NEST_PROTOCOL) {
3488 fr_strerror_printf("BEGIN-PROTOCOL at %s[%d] is missing END-PROTOCOL",
3489 fr_cwd_strip(frame->filename), line);
3490 goto error;
3491 }
3492
3493 if (frame->nest == NEST_ATTRIBUTE) {
3494 fr_strerror_printf("BEGIN %s at %s[%d] is missing END %s",
3495 frame->da->name, fr_cwd_strip(frame->filename), line,
3496 frame->da->name);
3497 goto error;
3498 }
3499
3500 if (frame->nest == NEST_VENDOR) {
3501 fr_strerror_printf("BEGIN-VENDOR at %s[%d] is missing END-VENDOR",
3502 fr_cwd_strip(frame->filename), line);
3503 goto error;
3504 }
3505
3506 /*
3507 * Run any necessary finalise callback, and then pop the frame.
3508 */
3509 if (frame->finalise) {
3510 if (frame->finalise(dctx) < 0) goto error;
3511 frame->finalise = NULL;
3512 }
3513
3514 fr_assert(!dctx->stack[dctx->stack_depth].finalise);
3515 dctx->stack_depth--;
3516 }
3517
3518 fclose(fp);
3519 dctx->filename = CURRENT_FRAME(dctx)->filename = old_filename;
3520 dctx->line = CURRENT_FRAME(dctx)->line = old_line;
3521
3522 return 0;
3523}
3524
3525static int dict_from_file(fr_dict_t *dict,
3526 char const *dir_name, char const *filename,
3527 char const *src_file, int src_line)
3528{
3529 int ret;
3531
3532 memset(&dctx, 0, sizeof(dctx));
3533 dctx.dict = dict;
3534 if (dict_fixup_init(&dctx.fixup) < 0) return -1;
3535 dctx.stack[0].da = dict->root;
3536 dctx.stack[0].nest = NEST_TOP;
3537
3538 ret = _dict_from_file(&dctx, dir_name, filename, src_file, src_line);
3539 if (ret < 0) {
3540 talloc_free(dctx.fixup.pool);
3541 return ret;
3542 }
3543
3544 /*
3545 * Applies to any attributes added to the *internal*
3546 * dictionary.
3547 *
3548 * Fixups should have been applied already to any protocol
3549 * dictionaries.
3550 */
3551 return dict_finalise(&dctx);
3552}
3553
3554/** (Re-)Initialize the special internal dictionary
3555 *
3556 * This dictionary has additional programmatically generated attributes added to it,
3557 * and is checked in addition to the protocol specific dictionaries.
3558 *
3559 * @note The dictionary pointer returned in out must have its reference counter
3560 * decremented with #fr_dict_free when no longer used.
3561 *
3562 * @param[out] out Where to write pointer to the internal dictionary.
3563 * @param[in] dict_subdir name of the internal dictionary dir (may be NULL).
3564 * @param[in] dependent Either C src file, or another dictionary.
3565 * @return
3566 * - 0 on success.
3567 * - -1 on failure.
3568 */
3569int fr_dict_internal_afrom_file(fr_dict_t **out, char const *dict_subdir, char const *dependent)
3570{
3571 fr_dict_t *dict;
3572 char *dict_path = NULL;
3573 size_t i;
3574 fr_dict_attr_flags_t flags = { .internal = true };
3575 char *type_name;
3576 fr_dict_attr_t *cast_base;
3578
3579 if (unlikely(!dict_gctx)) {
3580 fr_strerror_const("fr_dict_global_ctx_init() must be called before loading dictionary files");
3581 return -1;
3582 }
3583
3584 /*
3585 * Increase the reference count of the internal dictionary.
3586 */
3587 if (dict_gctx->internal) {
3590 return 0;
3591 }
3592
3593 dict_path = dict_subdir ?
3594 talloc_asprintf(NULL, "%s%c%s", fr_dict_global_ctx_dir(), FR_DIR_SEP, dict_subdir) :
3596
3597 fr_strerror_clear(); /* Ensure we don't report spurious errors */
3598
3600 if (!dict) {
3601 error:
3603 talloc_free(dict_path);
3604 return -1;
3605 }
3606
3607 /*
3608 * Set the root name of the dictionary
3609 */
3610 if (dict_root_set(dict, "internal", 0) < 0) goto error;
3611
3612 if (dict_path && dict_from_file(dict, dict_path, FR_DICTIONARY_FILE, NULL, 0) < 0) goto error;
3613
3614 TALLOC_FREE(dict_path);
3615
3616 dict_dependent_add(dict, dependent);
3617
3618 if (!dict_gctx->internal) {
3620 dict_dependent_add(dict, "global");
3621 }
3622
3623 /*
3624 * Try to load libfreeradius-internal, too. If that
3625 * fails (i.e. fuzzers???), ignore it.
3626 */
3627 (void) dict_dlopen(dict, "internal");
3628
3629 cast_base = dict_attr_child_by_num(dict->root, FR_CAST_BASE);
3630 if (!cast_base) {
3631 fr_strerror_printf("Failed to find 'Cast-Base' in internal dictionary");
3632 goto error;
3633 }
3634
3635 fr_assert(cast_base->type == FR_TYPE_UINT8);
3636 fr_value_box_init(&box, FR_TYPE_UINT8, NULL, false);
3637
3638 /*
3639 * Add cast attributes. We do it this way,
3640 * so cast attributes get added automatically for new types.
3641 *
3642 * We manually add the attributes to the dictionary, and bypass
3643 * fr_dict_attr_add(), because we know what we're doing, and
3644 * that function does too many checks.
3645 */
3646 for (i = 0; i < fr_type_table_len; i++) {
3649
3650 switch (p->value) {
3651 case FR_TYPE_NULL: /* Can't cast to NULL */
3652 case FR_TYPE_VENDOR: /* Vendors can't exist in dictionaries as attributes */
3653 continue;
3654 }
3655
3656 type_name = talloc_typed_asprintf(NULL, "Tmp-Cast-%s", p->name.str);
3657
3658 n = dict_attr_alloc(dict->pool, dict->root, type_name,
3659 FR_CAST_BASE + p->value, p->value, &(dict_attr_args_t){ .flags = &flags});
3660 if (!n) {
3661 talloc_free(type_name);
3662 goto error;
3663 }
3664
3665 if (dict_attr_add_to_namespace(dict->root, n) < 0) {
3666 fr_strerror_printf_push("Failed inserting '%s' into internal dictionary", type_name);
3667 talloc_free(type_name);
3668 goto error;
3669 }
3670
3671 talloc_free(type_name);
3672
3673 /*
3674 * Set up parenting for the attribute.
3675 */
3676 if (dict_attr_child_add(dict->root, n) < 0) goto error;
3677
3678 /*
3679 * Add the enum, too.
3680 */
3681 box.vb_uint8 = p->value;
3682 if (dict_attr_enum_add_name(cast_base, p->name.str, &box, false, false, NULL) < 0) {
3683 fr_strerror_printf_push("Failed adding '%s' as a VALUE into internal dictionary", p->name.str);
3684 goto error;
3685 }
3686 }
3687
3688 *out = dict;
3689
3690 return 0;
3691}
3692
3693/** (Re)-initialize a protocol dictionary
3694 *
3695 * Initialize the directory, then fix the attr number of all attributes.
3696 *
3697 * @param[out] out Where to write a pointer to the new dictionary. Will free existing
3698 * dictionary if files have changed and *out is not NULL.
3699 * @param[in] proto_name that we're loading the dictionary for.
3700 * @param[in] proto_dir Explicitly set where to hunt for the dictionary files. May be NULL.
3701 * @param[in] dependent Either C src file, or another dictionary.
3702 * @return
3703 * - 0 on success.
3704 * - -1 on failure.
3705 */
3706int fr_dict_protocol_afrom_file(fr_dict_t **out, char const *proto_name, char const *proto_dir, char const *dependent)
3707{
3708 char *dict_dir = NULL;
3709 fr_dict_t *dict;
3710 bool added = false;
3711
3712 *out = NULL;
3713
3714 if (unlikely(!dict_gctx)) {
3715 fr_strerror_const("fr_dict_global_ctx_init() must be called before loading dictionary files");
3716 return -1;
3717 }
3718
3719 if (unlikely(!dict_gctx->internal)) {
3720 fr_strerror_const("Internal dictionary must be initialised before loading protocol dictionaries");
3721 return -1;
3722 }
3723
3724 /*
3725 * Increment the reference count if the dictionary
3726 * has already been loaded and return that.
3727 */
3728 dict = dict_by_protocol_name(proto_name);
3729 if (dict) {
3730 /*
3731 * If we're in the middle of loading this dictionary, then the only way we get back here
3732 * is via a circular reference. So we catch that, and drop the circular dependency.
3733 *
3734 * When we have A->B->A, it means that we don't need to track B->A, because we track
3735 * A->B. And if A is freed, then B is freed.
3736 */
3737 added = true;
3738 dict_dependent_add(dict, dependent);
3739
3740 /*
3741 * But we only return a pre-existing dict if _this function_ has loaded it.
3742 */
3743 if (dict->loaded) {
3744 *out = dict;
3745 return 0;
3746 }
3747
3748 /*
3749 * Set the flag to true _before_ loading the file. That prevents recursion.
3750 */
3751 dict->loaded = true;
3752 }
3753
3754 if (!proto_dir) {
3755 dict_dir = talloc_asprintf(NULL, "%s%c%s", fr_dict_global_ctx_dir(), FR_DIR_SEP, proto_name);
3756 } else {
3757 dict_dir = talloc_asprintf(NULL, "%s%c%s", fr_dict_global_ctx_dir(), FR_DIR_SEP, proto_dir);
3758 }
3759
3760 fr_strerror_clear(); /* Ensure we don't report spurious errors */
3761
3762 /*
3763 * Start in the context of the internal dictionary,
3764 * and switch to the context of a protocol dictionary
3765 * when we hit a BEGIN-PROTOCOL line.
3766 *
3767 * This allows a single file to provide definitions
3768 * for multiple protocols, which'll probably be useful
3769 * at some point.
3770 */
3771 if (dict_from_file(dict_gctx->internal, dict_dir, FR_DICTIONARY_FILE, NULL, 0) < 0) {
3772 error:
3773 if (dict) dict->loading = false;
3774 talloc_free(dict_dir);
3775 return -1;
3776 }
3777
3778 /*
3779 * Check the dictionary actually defined the protocol
3780 */
3781 dict = dict_by_protocol_name(proto_name);
3782 if (!dict) {
3783 fr_strerror_printf("Dictionary \"%s\" missing \"BEGIN-PROTOCOL %s\" declaration", dict_dir, proto_name);
3784 goto error;
3785 }
3786
3787 /*
3788 * Initialize the library.
3789 */
3790 dict->loaded = true;
3791 if (dict->proto && dict->proto->init) {
3792 if (dict->proto->init() < 0) goto error;
3793 }
3794 dict->loading = false;
3795
3796 dict->dir = talloc_steal(dict, dict_dir);
3797
3798 if (!added) dict_dependent_add(dict, dependent);
3799
3800 *out = dict;
3801
3802 return 0;
3803}
3804
3805/* Alloc a new root dictionary attribute
3806 *
3807 * @note Must only be called once per dictionary.
3808 *
3809 * @param[in] proto_name that we're loading the dictionary for.
3810 * @param[in] proto_number The artificial (or IANA allocated) number for the protocol.
3811 * @return
3812 * - A pointer to the new dict context on success.
3813 * - NULL on failure.
3814 */
3815fr_dict_t *fr_dict_alloc(char const *proto_name, unsigned int proto_number)
3816{
3817 fr_dict_t *dict;
3818
3819 if (unlikely(!dict_gctx)) {
3820 fr_strerror_printf("fr_dict_global_ctx_init() must be called before loading dictionary files");
3821 return NULL;
3822 }
3823
3824 /*
3825 * Alloc dict instance.
3826 */
3828 if (!dict) return NULL;
3829
3830 /*
3831 * Set the root name of the dictionary
3832 */
3833 if (dict_root_set(dict, proto_name, proto_number) < 0) {
3835 return NULL;
3836 }
3837
3838 return dict;
3839}
3840
3841/** Read supplementary attribute definitions into an existing dictionary
3842 *
3843 * @param[in] dict Existing dictionary.
3844 * @param[in] dir dictionary is located in.
3845 * @param[in] filename of the dictionary.
3846 * @return
3847 * - 0 on success.
3848 * - -1 on failure.
3849 */
3850int fr_dict_read(fr_dict_t *dict, char const *dir, char const *filename)
3851{
3853
3854 if (!dir) dir = dict->dir;
3855
3856 if (unlikely(dict->read_only)) {
3857 fr_strerror_printf("%s dictionary has been marked as read only", fr_dict_root(dict)->name);
3858 return -1;
3859 }
3860
3861 if (!dict->vendors_by_name) {
3862 fr_strerror_printf("%s: Must initialise dictionary before calling fr_dict_read()", __FUNCTION__);
3863 return -1;
3864 }
3865
3866 return dict_from_file(dict, dir, filename, NULL, 0);
3867}
3868
3869/*
3870 * External API for testing
3871 */
3872int fr_dict_parse_str(fr_dict_t *dict, char const *input, fr_dict_attr_t const *parent)
3873{
3874 int argc;
3875 char *argv[DICT_MAX_ARGV];
3876 int ret;
3877 fr_dict_attr_flags_t base_flags = {};
3879 char *buf;
3880
3882
3883 /*
3884 * str_to_argv() mangles the input buffer, which messes with 'unit_test_attribute -w foo'
3885 */
3886 buf = talloc_strdup(NULL, input);
3887
3888 argc = fr_dict_str_to_argv(buf, argv, DICT_MAX_ARGV);
3889 if (argc == 0) {
3890 talloc_free(buf);
3891 return 0;
3892 }
3893
3894 memset(&dctx, 0, sizeof(dctx));
3895 dctx.dict = dict;
3896
3897 dctx.stack[0].da = parent;
3898 dctx.stack[0].nest = NEST_TOP;
3899
3900 if (dict_fixup_init(&dctx.fixup) < 0) {
3901 error:
3902 TALLOC_FREE(dctx.fixup.pool);
3903 talloc_free(buf);
3904 return -1;
3905 }
3906
3907 if (strcasecmp(argv[0], "VALUE") == 0) {
3908 if (argc < 4) {
3909 fr_strerror_printf("VALUE needs at least 4 arguments, got %i", argc);
3910 goto error;
3911 }
3912
3913 if (!fr_dict_attr_by_oid(NULL, fr_dict_root(dict), argv[1])) {
3914 fr_strerror_printf("Attribute '%s' does not exist in dictionary \"%s\"",
3915 argv[1], dict->root->name);
3916 goto error;
3917 }
3918 ret = dict_read_process_value(&dctx, argv + 1, argc - 1, &base_flags);
3919
3920 } else if (strcasecmp(argv[0], "ATTRIBUTE") == 0) {
3921 if (parent != dict->root) {
3922 (void) dict_dctx_push(&dctx, parent, NEST_NONE);
3923 }
3924
3925 ret = dict_read_process_attribute(&dctx,
3926 argv + 1, argc - 1, &base_flags);
3927
3928 } else if (strcasecmp(argv[0], "DEFINE") == 0) {
3929 if (parent != dict->root) {
3930 (void) dict_dctx_push(&dctx, parent, NEST_NONE);
3931 }
3932
3933 ret = dict_read_process_define(&dctx,
3934 argv + 1, argc - 1, &base_flags);
3935
3936 } else if (strcasecmp(argv[0], "VENDOR") == 0) {
3937 ret = dict_read_process_vendor(&dctx, argv + 1, argc - 1, &base_flags);
3938
3939 } else {
3940 fr_strerror_printf("Invalid input '%s'", argv[0]);
3941 goto error;
3942 }
3943
3944 if (ret < 0) goto error;
3945
3946 talloc_free(buf);
3947
3948 return dict_finalise(&dctx);
3949}
3950
3951/** Load one dictionary file.
3952 *
3953 * @param[out] out Where to write a pointer to the new dictionary. Will free existing
3954 * dictionary if files have changed and *out is not NULL.
3955 * @param[in] dir directory
3956 * @param[in] filename file to load.
3957 * @return
3958 * - 0 on success.
3959 * - -1 on failure.
3960 */
3961int fr_dict_afrom_file(fr_dict_t **out, char const *dir, char const *filename)
3962{
3963 fr_dict_t *dict;
3964
3965 *out = NULL;
3966
3967 if (unlikely(!dict_gctx)) {
3968 fr_strerror_const("fr_dict_global_ctx_init() must be called before loading dictionary files");
3969 return -1;
3970 }
3971
3972 if (unlikely(!dict_gctx->internal)) {
3973 fr_strerror_const("Internal dictionary must be initialised before loading test dictionaries");
3974 return -1;
3975 }
3976
3977 fr_strerror_clear(); /* Ensure we don't report spurious errors */
3978
3980 if (!dict) return -1;
3981
3982 if (dict_from_file(dict, dir, filename, NULL, 0) < 0) {
3984 return -1;
3985 }
3986
3987 /*
3988 * Finalize the library
3989 *
3990 * Note that we cannot use this function to test loading of protocol dictionaries without
3991 * significant changes. We would have to free up all of the dependencies, etc. which isn't
3992 * entirely done right now.
3993 */
3994 fr_assert(dict->proto);
3995 fr_assert(strcmp(dict->proto->name, "default") == 0);
3996 fr_assert(!dict->proto->init);
3997 fr_assert(!dict->proto->free);
3998 fr_assert(!dict->proto->encode);
3999 fr_assert(!dict->proto->decode);
4000
4001 dict->loaded = true;
4002 dict->loading = false;
4003
4004 *out = dict;
4005
4006 return 0;
4007}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
int n
Definition acutest.h:577
strcpy(log_entry->msg, buffer)
int const char int line
Definition acutest.h:702
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define RCSID(id)
Definition build.h:512
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:228
#define NDEBUG_UNUSED
Definition build.h:347
#define DIAG_ON(_x)
Definition build.h:487
#define unlikely(_x)
Definition build.h:407
#define UNUSED
Definition build.h:336
#define NUM_ELEMENTS(_t)
Definition build.h:358
#define DIAG_OFF(_x)
Definition build.h:486
fr_dict_t * dict
Definition common.c:31
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:131
#define FR_FAULT_LOG(_fmt,...)
Definition debug.h:50
#define fr_cond_assert_msg(_x, _fmt,...)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:148
int fr_dict_attr_add_initialised(fr_dict_attr_t *da)
A variant of fr_dict_attr_t that allows a pre-allocated, populated fr_dict_attr_t to be added.
Definition dict_util.c:1820
fr_slen_t fr_dict_enum_name_from_substr(fr_sbuff_t *out, fr_sbuff_parse_error_t *err, fr_sbuff_t *in, fr_sbuff_term_t const *tt)
Extract an enumeration name from a string.
Definition dict_util.c:3798
int fr_dict_enum_add_name(fr_dict_attr_t *da, char const *name, fr_value_box_t const *value, bool coerce, bool replace)
Add a value name.
Definition dict_util.c:2191
@ FLAG_LENGTH_UINT8
string / octets type is prefixed by uint8 of length
Definition dict.h:166
@ FLAG_LENGTH_UINT16
string / octets type is prefixed by uint16 of length
Definition dict.h:167
@ FLAG_KEY_FIELD
this is a key field for a subsequent struct
Definition dict.h:164
@ FLAG_BIT_FIELD
bit field inside of a struct
Definition dict.h:165
char const * name
Vendor name.
Definition dict.h:274
unsigned int is_root
Is root of a dictionary.
Definition dict.h:75
fr_dict_attr_t const * fr_dict_attr_common_parent(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition dict_util.c:2287
int fr_dict_protocol_reference(fr_dict_attr_t const **da_p, fr_dict_attr_t const *root, fr_sbuff_t *in)
Resolve a reference string to a dictionary attribute.
Definition dict_fixup.c:135
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3505
fr_dict_attr_t * fr_dict_attr_unconst(fr_dict_attr_t const *da)
Coerce to non-const.
Definition dict_util.c:4892
fr_slen_t fr_dict_attr_by_oid_legacy(fr_dict_attr_t const **parent, unsigned int *attr, char const *oid)
Get the leaf attribute of an OID string.
Definition dict_util.c:2373
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2639
fr_dict_flag_parse_func_t func
Custom parsing function to convert a flag value string to a C type value.
Definition dict.h:395
unsigned int internal
Internal attribute, should not be received in protocol packets, should not be encoded.
Definition dict.h:88
#define DA_VERIFY(_x)
Definition dict.h:66
#define da_is_bit_field(_da)
Definition dict.h:171
uint32_t pen
Private enterprise number.
Definition dict.h:270
#define da_is_length_field(_da)
Definition dict.h:172
char const * fr_dict_global_ctx_dir(void)
Definition dict_util.c:4787
@ FR_DICT_ATTR_EXT_ENUMV
Enumeration values.
Definition dict.h:188
@ FR_DICT_ATTR_EXT_REF
Attribute references another attribute and/or dictionary.
Definition dict.h:184
@ FR_DICT_ATTR_EXT_KEY
UNION attribute references a key.
Definition dict.h:186
fr_dict_vendor_t const * fr_dict_vendor_by_name(fr_dict_t const *dict, char const *name)
Look up a vendor by its name.
Definition dict_util.c:2899
bool needs_value
This parsing flag must have a value. Else we error.
Definition dict.h:397
fr_dict_attr_t const * fr_dict_attr_by_oid(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *oid))
Resolve an attribute using an OID string.
Definition dict_util.c:2612
fr_dict_vendor_t const * fr_dict_vendor_by_num(fr_dict_t const *dict, uint32_t vendor_pen)
Look up a vendor by its PEN.
Definition dict_util.c:2922
fr_dict_attr_t const * fr_dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Check if a child attribute exists in a parent using an attribute number.
Definition dict_util.c:3570
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:170
static int8_t fr_dict_attr_cmp_fields(const fr_dict_attr_t *a, const fr_dict_attr_t *b)
Compare two dictionary attributes by their contents.
Definition dict.h:691
Values of the encryption flags.
Protocol specific custom flag definitnion.
Definition dict.h:425
Private enterprise.
Definition dict.h:269
#define fr_dict_attr_ref_type(_type)
Definition dict_ext.h:72
fr_dict_attr_ref_type_t type
The state of the reference.
Definition dict_ext.h:78
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:121
#define fr_dict_attr_ref_is_unresolved(_type)
Definition dict_ext.h:71
@ FR_DICT_ATTR_REF_ENUM
The attribute is an enumeration value.
Definition dict_ext.h:63
@ FR_DICT_ATTR_REF_KEY
it is a UNION which has a ref to a key, and children.
Definition dict_ext.h:64
@ FR_DICT_ATTR_REF_ALIAS
The attribute is an alias for another attribute.
Definition dict_ext.h:59
@ FR_DICT_ATTR_REF_CLONE
The attribute is a "copy" of another attribute.
Definition dict_ext.h:62
Attribute extension - Holds a reference to an attribute in another dictionary.
Definition dict_ext.h:77
static int dict_attr_ref_set(fr_dict_attr_t const *da, fr_dict_attr_t const *ref, fr_dict_attr_ref_type_t type)
static int dict_attr_ref_aunresolved(fr_dict_attr_t **da_p, char const *ref, fr_dict_attr_ref_type_t type)
static void * dict_attr_ext_alloc(fr_dict_attr_t **da_p, fr_dict_attr_ext_t ext)
Allocate an attribute extension.
int dict_fixup_apply(dict_fixup_ctx_t *fctx)
Apply all outstanding fixes to a set of dictionaries.
Definition dict_fixup.c:810
int dict_fixup_enumv_enqueue(dict_fixup_ctx_t *fctx, char const *filename, int line, char const *attr, size_t attr_len, char const *name, size_t name_len, char const *value, size_t value_len, fr_dict_attr_t const *parent)
Add an enumeration value to an attribute which has not yet been defined.
Definition dict_fixup.c:275
int dict_fixup_alias_enqueue(dict_fixup_ctx_t *fctx, char const *filename, int line, fr_dict_attr_t *alias_parent, char const *alias, fr_dict_attr_t *ref_parent, char const *ref)
Resolve a group reference.
Definition dict_fixup.c:737
int dict_fixup_clone_enqueue(dict_fixup_ctx_t *fctx, fr_dict_attr_t *da, char const *ref)
Clone one area of a tree into another.
Definition dict_fixup.c:425
int dict_fixup_clone_enum_enqueue(dict_fixup_ctx_t *fctx, fr_dict_attr_t *da, char const *ref)
Clone enumeration values from one attribute to another.
Definition dict_fixup.c:577
int dict_fixup_vsa_enqueue(dict_fixup_ctx_t *fctx, fr_dict_attr_t *da)
Push a fixup for a VSA.
Definition dict_fixup.c:676
int dict_fixup_clone(fr_dict_attr_t **dst_p, fr_dict_attr_t const *src)
Clone a dictionary attribute from a ref.
Definition dict_fixup.c:460
int dict_fixup_group_enqueue(dict_fixup_ctx_t *fctx, fr_dict_attr_t *da, char const *ref)
Resolve a group reference.
Definition dict_fixup.c:358
int dict_fixup_init(dict_fixup_ctx_t *fctx)
Initialise a fixup ctx.
Definition dict_fixup.c:790
TALLOC_CTX * pool
Temporary pool for fixups, reduces holes.
int dict_attr_enum_add_name(fr_dict_attr_t *da, char const *name, fr_value_box_t const *value, bool coerce, bool replace, fr_dict_attr_t const *child_struct)
Definition dict_util.c:1995
int dict_attr_type_init(fr_dict_attr_t **da_p, fr_type_t type)
Initialise type specific fields within the dictionary attribute.
Definition dict_util.c:526
int dict_attr_parent_init(fr_dict_attr_t **da_p, fr_dict_attr_t const *parent)
Initialise fields which depend on a parent attribute.
Definition dict_util.c:613
#define dict_attr_alloc(_ctx, _parent, _name, _attr, _type, _args)
Definition dict_priv.h:256
fr_dict_t * dict_alloc(TALLOC_CTX *ctx)
Allocate a new dictionary.
Definition dict_util.c:4154
#define INTERNAL_IF_NULL(_dict, _ret)
Set the internal dictionary if none was provided.
Definition dict_priv.h:45
int dict_attr_add_to_namespace(fr_dict_attr_t const *parent, fr_dict_attr_t *da)
Add an attribute to the name table for an attribute.
Definition dict_util.c:1754
fr_dict_attr_t * dict_attr_child_by_num(fr_dict_attr_t const *parent, unsigned int attr)
Internal version of fr_dict_attr_child_by_num.
Definition dict_util.c:3523
fr_dict_t * dict_by_protocol_num(unsigned int num)
Internal version of fr_dict_by_protocol_num.
Definition dict_util.c:2764
int dict_attr_child_add(fr_dict_attr_t *parent, fr_dict_attr_t *child)
Add a child to a parent.
Definition dict_util.c:1655
int dict_vendor_add(fr_dict_t *dict, char const *name, unsigned int num)
Add a vendor to the dictionary.
Definition dict_util.c:1560
int dict_attr_finalise(fr_dict_attr_t **da_p, char const *name)
Set remaining fields in a dictionary attribute before insertion.
Definition dict_util.c:733
int dict_attr_num_init(fr_dict_attr_t *da, unsigned int num)
Set the attribute number (if any)
Definition dict_util.c:689
int dict_attr_num_init_name_only(fr_dict_attr_t *da)
Set the attribute number (if any)
Definition dict_util.c:707
int dict_dlopen(fr_dict_t *dict, char const *name)
Definition dict_util.c:3838
fr_dict_t * dict_by_protocol_name(char const *name)
Internal version of fr_dict_by_protocol_name.
Definition dict_util.c:2750
@ FR_DICT_PROTO_LDAP
Definition dict_priv.h:172
@ FR_DICT_PROTO_DNS
Definition dict_priv.h:171
@ FR_DICT_PROTO_RADIUS
Definition dict_priv.h:161
@ FR_DICT_PROTO_SNMP
Definition dict_priv.h:167
@ FR_DICT_PROTO_DHCPv4
Definition dict_priv.h:162
@ FR_DICT_PROTO_DHCPv6
Definition dict_priv.h:163
@ FR_DICT_PROTO_TACACS
Definition dict_priv.h:165
@ FR_DICT_PROTO_TLS
Definition dict_priv.h:170
@ FR_DICT_PROTO_CRL
Definition dict_priv.h:174
@ FR_DICT_PROTO_VMPS
Definition dict_priv.h:166
@ FR_DICT_PROTO_ARP
Definition dict_priv.h:168
@ FR_DICT_PROTO_ETHERNET
Definition dict_priv.h:164
@ FR_DICT_PROTO_TFTP
Definition dict_priv.h:169
@ FR_DICT_PROTO_BFD
Definition dict_priv.h:173
int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref, bool from_public)
Add an alias to an existing attribute.
Definition dict_util.c:1379
fr_dict_t * internal
Magic internal dictionary.
Definition dict_priv.h:155
fr_dict_attr_t * dict_attr_alloc_null(TALLOC_CTX *ctx, fr_dict_protocol_t const *dict)
Partial initialisation functions.
Definition dict_util.c:964
int dict_dependent_add(fr_dict_t *dict, char const *dependent)
Record a new dependency on a dictionary.
Definition dict_util.c:3929
#define dict_attr_alloc_root(_ctx, _dict, _name, _attr, _args)
Definition dict_priv.h:248
int dict_protocol_add(fr_dict_t *dict)
Add a protocol to the global protocol table.
Definition dict_util.c:1487
fr_dict_gctx_t * dict_gctx
Top level structure containing global dictionary state.
Definition dict_util.c:41
bool perm_check
Whether we should check dictionary file permissions as they're loaded.
Definition dict_priv.h:132
Optional arguments for initialising/allocating attributes.
Definition dict_priv.h:192
Entry in the filename list of files associated with this dictionary.
Definition dict_priv.h:69
Test enumeration values.
Definition dict_test.h:92
fr_dict_keyword_finalise_t finalise
function to call when popping
bool fr_dict_filename_loaded(fr_dict_t const *dict, char const *dir, char const *filename)
static int dict_flag_flat(fr_dict_attr_t **da_p, UNUSED char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
"flat"
static fr_table_num_sorted_t const dict_nest_table[]
static int dict_read_process_include(dict_tokenize_ctx_t *dctx, char **argv, int argc, char const *dir)
dict_nest_t nest
for manual vs automatic begin / end things
dict_fixup_ctx_t fixup
static int dict_read_process_common(dict_tokenize_ctx_t *dctx, fr_dict_attr_t **da_p, fr_dict_attr_t const *parent, char const *name, char const *type_name, char *flag_name, fr_dict_attr_flags_t const *base_flags)
static int dict_process_type_field(dict_tokenize_ctx_t *dctx, char const *name_in, fr_dict_attr_t **da_p)
#define NEST_ANY
static int dict_read_process_attribute(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
int member_num
structure member numbers
static int dict_filename_add(char **filename_out, fr_dict_t *dict, char const *filename, char const *src_file, int src_line)
Maintain a linked list of filenames which we've seen loading this dictionary.
static int dict_read_process_alias(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
static int _dict_from_file(dict_tokenize_ctx_t *dctx, char const *dir_name, char const *filename, char const *src_file, int src_line)
fr_dict_section_begin_t begin
Can have a BEGIN prefix.
static int dict_attr_allow_dup(fr_dict_attr_t const *da)
Check if this definition is a duplicate, and if it is, whether we should skip it error out.
int fr_dict_parse_str(fr_dict_t *dict, char const *input, fr_dict_attr_t const *parent)
static int dict_finalise(dict_tokenize_ctx_t *dctx)
static int dict_read_process_member(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
fr_dict_t * fr_dict_alloc(char const *proto_name, unsigned int proto_number)
static int dict_flag_ref(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
static int dict_flag_precision(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
static fr_table_num_ordered_t const dict_proto_table[]
The main protocols that we care about.
static int dict_read_process_end(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
int stack_depth
points to the last used stack frame
ssize_t struct_size
size of the struct.
static bool dict_read_sscanf_i(unsigned int *pvalue, char const *str)
static int dict_read_process_end_protocol(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
int fr_dict_afrom_file(fr_dict_t **out, char const *dir, char const *filename)
Load one dictionary file.
fr_dict_attr_t const * struct_is_closed
no more members are allowed
char * filename
current filename
int(* fr_dict_keyword_parse_t)(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
Keyword parser.
static int dict_read_process_vendor(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
int fr_dict_protocol_afrom_file(fr_dict_t **out, char const *proto_name, char const *proto_dir, char const *dependent)
(Re)-initialize a protocol dictionary
int(* fr_dict_section_begin_t)(dict_tokenize_ctx_t *dctx)
Pushes a new frame onto the top of the stack based on the current frame.
int fr_dict_str_to_argv(char *str, char **argv, int max_argc)
static int dict_read_process_define(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
static size_t const dict_nest_table_len
static int dict_read_parse_format(char const *format, int *ptype, int *plength, bool *pcontinuation)
static int dict_read_process_begin_vendor(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
#define ASSERT_CURRENT_NEST(_dctx, _nest)
static dict_tokenize_frame_t const * dict_dctx_unwind_until(dict_tokenize_ctx_t *dctx, dict_nest_t nest)
Unwind the stack until it points to a particular type of stack frame.
static int dict_from_file(fr_dict_t *dict, char const *dir_name, char const *filename, char const *src_file, int src_line)
fr_dict_attr_t const * da
the da we care about
fr_dict_attr_t * value_attr
Cache of last attribute to speed up value processing.
static int dict_read_process_protocol(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flag)
Register the specified dictionary as a protocol dictionary.
static int dict_read_process_end_vendor(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
#define FLAG_FUNC(_name)
Define a flag setting function, which sets one bit in a fr_dict_attr_flags_t.
#define CURRENT_LINE(_dctx)
static dict_tokenize_frame_t const * dict_dctx_pop(dict_tokenize_ctx_t *dctx)
Pop the current stack frame.
char * filename
name of the file where we read this entry
static int dict_set_value_attr(dict_tokenize_ctx_t *dctx, fr_dict_attr_t *da)
static int dict_read_process_enum(dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
int line
current line
#define CURRENT_FILENAME(_dctx)
static int dict_struct_finalise(dict_tokenize_ctx_t *dctx)
static int dict_flag_offset(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
static int dict_read_process_begin_protocol(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
static int dict_flag_key(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
fr_dict_t * dict
Protocol dictionary we're inserting attributes into.
static int dict_begin_protocol(NDEBUG_UNUSED dict_tokenize_ctx_t *dctx)
Process an inline BEGIN PROTOCOL block.
static int dict_flag_clone(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)
dict_tokenize_frame_t stack[DICT_MAX_STACK]
stack of attributes to track
int line
line number where we read this entry
static int dict_flag_enum(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
static dict_tokenize_frame_t const * dict_dctx_unwind(dict_tokenize_ctx_t *dctx)
#define CURRENT_DA(_dctx)
#define DICT_MAX_ARGV
Maximum number of arguments.
static int dict_read_process_value(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
Process a value alias.
static dict_tokenize_frame_t const * dict_dctx_find_frame(dict_tokenize_ctx_t *dctx, dict_nest_t nest)
static size_t const dict_proto_table_len
fr_dict_keyword_parser_t value
Value to return from lookup.
static int dict_flag_length(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
static bool dict_filename_loaded(fr_dict_t const *dict, char const *filename, char const *src_file, int src_line)
See if we have already loaded the file,.
int fr_dict_read(fr_dict_t *dict, char const *dir, char const *filename)
Read supplementary attribute definitions into an existing dictionary.
#define DICT_MAX_STACK
Maximum stack size.
static int dict_flag_subtype(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
static int dict_read_process_begin(dict_tokenize_ctx_t *dctx, char **argv, int argc, UNUSED fr_dict_attr_flags_t *base_flags)
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *dict_subdir, char const *dependent)
(Re-)Initialize the special internal dictionary
static int dict_dctx_push(dict_tokenize_ctx_t *dctx, fr_dict_attr_t const *da, dict_nest_t nest)
static int dict_read_process_flags(UNUSED dict_tokenize_ctx_t *dctx, char **argv, int argc, fr_dict_attr_flags_t *base_flags)
static int dict_attr_add_or_fixup(dict_fixup_ctx_t *fixup, fr_dict_attr_t **da_p)
Add an attribute to the dictionary, or add it to a list of attributes to clone later.
int(* fr_dict_keyword_finalise_t)(dict_tokenize_ctx_t *dctx)
#define CURRENT_FRAME(_dctx)
static void dict_attr_location_set(dict_tokenize_ctx_t *dctx, fr_dict_attr_t *da)
fr_dict_attr_t const * relative_attr
for ".82" instead of "1.2.3.82". only for parents of type "tlv"
static int dict_flag_secret(fr_dict_attr_t **da_p, UNUSED char const *value, UNUSED fr_dict_flag_parser_rule_t const *rule)
fr_table_elem_name_t name
Name of the keyword, e.g. "ATTRIBUTE".
fr_dict_keyword_parse_t parse
Function to parse the keyword with.
void dict_dctx_debug(dict_tokenize_ctx_t *dctx)
dict_nest_t
This represents explicit BEGIN/END frames pushed onto the stack.
@ NEST_TOP
top of the stack
@ NEST_VENDOR
BEGIN-VENDOR.
@ NEST_PROTOCOL
BEGIN-PROTOCOL.
@ NEST_ATTRIBUTE
BEGIN foo.
@ NEST_NONE
static int dict_root_set(fr_dict_t *dict, char const *name, unsigned int proto_number)
Set a new root dictionary attribute.
Parser context for dict_from_file.
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:537
talloc_free(hp)
int fr_globdir_iter_init(char const **filename, char const *dir, char const *pattern, fr_globdir_iter_t *iter)
Initialize an iterator over filenames.
Definition file.c:693
int fr_globdir_iter_next(char const **filename, fr_globdir_iter_t *iter)
Get the next filename.
Definition file.c:851
char const * fr_cwd_strip(char const *filename)
Intended to be used in logging functions to make output more readable.
Definition file.c:383
int fr_globdir_iter_free(fr_globdir_iter_t *iter)
Definition file.c:885
static int stack_depth
Definition radmin.c:156
fr_type_t
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_INT16
16 Bit signed integer.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_STRUCT
like TLV, but without T or L, and fixed-width children
@ FR_TYPE_INT32
32 Bit signed integer.
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_VSA
Vendor-Specific, for RADIUS attribute 26.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
int strncasecmp(char *s1, char *s2, int n)
Definition missing.c:35
int strcasecmp(char *s1, char *s2)
Definition missing.c:65
#define fr_assert(_expr)
Definition rad_assert.h:37
static char const * name
size_t fr_sbuff_adv_past_allowed(fr_sbuff_t *sbuff, size_t len, bool const allowed[static SBUFF_CHAR_CLASS], fr_sbuff_term_t const *tt)
Wind position past characters in the allowed set.
Definition sbuff.c:1822
bool const sbuff_char_class_int[SBUFF_CHAR_CLASS]
Definition sbuff.c:69
#define FR_SBUFF_IN(_start, _len_or_end)
#define FR_SBUFF_IN_STR(_start)
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
fr_aka_sim_id_type_t type
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition strlcpy.c:34
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
char const * str
Literal string.
Definition table.h:42
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:685
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
#define TABLE_TYPE_NAME_FUNC_RPTR(_func, _our_table_type, _our_name, _our_def_type, _our_out_type)
Create a type-specific name-to-value function.
Definition table.h:174
static void const * table_sorted_value_by_str(void const *table, size_t table_len, size_t element_size, char const *name)
Convert a string to a value using a lexicographically sorted table.
Definition table.h:360
fr_table_elem_name_t name
Definition table.h:58
An element in an arbitrarily ordered array of name to num mappings.
Definition table.h:57
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:546
#define talloc_asprintf
Definition talloc.h:151
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
fr_table_num_ordered_t const fr_time_precision_table[]
Definition time.c:46
#define FR_DICTIONARY_FILE
Definition conf.h:7
static fr_slen_t parent
Definition pair.h:858
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:581
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#define fr_strerror_const(_msg)
Definition strerror.h:223
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
size_t fr_type_table_len
Definition types.c:87
#define fr_type_is_structural(_x)
Definition types.h:392
@ FR_TYPE_UNION
A union of limited children.
Definition types.h:81
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:83
#define fr_type_is_null(_x)
Definition types.h:347
#define fr_type_is_tlv(_x)
Definition types.h:372
#define fr_type_is_leaf(_x)
Definition types.h:393
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
static fr_type_t fr_type_from_str(char const *type)
Return the constant value representing a type.
Definition types.h:479
#define FR_TYPE_LEAF
Definition types.h:317
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:4196
ssize_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:6068
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:4377
#define FR_VALUE_BOX_INITIALISER_NULL(_vb)
A static initialiser for stack/globally allocated boxes.
Definition value.h:511
int nonnull(2, 5))
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:610
int format(printf, 5, 0))
static size_t char ** out
Definition value.h:1030