The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
dict_fixup.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/** Code to apply fctx and finalisation steps to a dictionary
18 *
19 * @file src/lib/util/dict_fixup.c
20 *
21 * @copyright 2020 The FreeRADIUS server project
22 * @copyright 2020,2024 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
23 */
24RCSID("$Id: 72bf7b7b65f22fdd631ff230317bedad420775bd $")
25
26#include <freeradius-devel/util/dict.h>
27#include <freeradius-devel/util/file.h>
28#include <freeradius-devel/util/value.h>
29
30#include "dict_fixup_priv.h"
31
32/** Common fields for every fixup structure
33 *
34 */
35typedef struct {
36 fr_dlist_t entry; //!< Entry in linked list of fctx.
38
39/** Add an enumeration value to an attribute that wasn't defined at the time the value was parsed
40 *
41 */
42typedef struct {
43 dict_fixup_common_t common; //!< Common fields.
44
45 char *filename; //!< where the line being fixed up.
46 int line; //!< ditto.
47
48 char *alias; //!< we need to create.
49 fr_dict_attr_t *alias_parent; //!< Where to add the alias.
50
51 char *ref; //!< what the alias references.
52 fr_dict_attr_t *ref_parent; //!< Parent attribute to resolve the 'attribute' string in.
54
55/** Add an enumeration value to an attribute that wasn't defined at the time the value was parsed
56 *
57 */
58typedef struct {
59 dict_fixup_common_t common; //!< Common fields.
60
61 char *filename; //!< where the line being fixed up.
62 int line; //!< ditto.
63
64 char *attribute; //!< we couldn't find (and will need to resolve later).
65 char *name; //!< Raw enum name.
66 char *value; //!< Raw enum value. We can't do anything with this until
67 //!< we know the attribute type, which we only find out later.
68
69 fr_dict_attr_t const *parent; //!< Parent attribute to resolve the 'attribute' string in.
71
72/** Resolve a group reference
73 *
74 */
75typedef struct {
76 dict_fixup_common_t common; //!< Common fields.
77
78 fr_dict_attr_t *da; //!< FR_TYPE_GROUP to fix
79 char *ref; //!< the reference name
81
82/** Clone operation from one tree node to another
83 *
84 */
85typedef struct {
86 dict_fixup_common_t common; //!< Common fields.
87
88 fr_dict_attr_t *da; //!< to populate with cloned information.
89 char *ref; //!< the target attribute to clone
91
92/** Run fixup callbacks for a VSA
93 *
94 */
95typedef struct {
96 dict_fixup_common_t common; //!< Common fields.
97
98 fr_dict_attr_t *da; //!< FR_TYPE_VSA to fix
100
101/** Dictionary attribute namespaces need their hash tables finalised
102 *
103 */
104typedef struct {
105 dict_fixup_common_t common; //!< Common fields.
106
107 fr_hash_table_t *hash; //!< We need to finalise.
109
110/** Initialise common fields in fixup struct, and add it to a fixup list
111 *
112 * @param[in] fixup_list to add fixup to.
113 * @param[in] common common header to populate.
114 * @return
115 * - 0 on success.
116 * - -1 on out of memory.
117 */
118static inline CC_HINT(always_inline) int dict_fixup_common(fr_dlist_head_t *fixup_list, dict_fixup_common_t *common)
119{
120 fr_dlist_insert_tail(fixup_list, common);
121
122 return 0;
123}
124
125/** Resolve a reference string to a dictionary attribute
126 *
127 * @param[out] da_p Where the attribute will be stored
128 * @param[in] rel Relative attribute to resolve from.
129 * @param[in] in Reference string.
130 * @return
131 * - <0 on error
132 * - 0 on parse OK, but *da_p is NULL;
133 * - 1 for parse OK, and *da_p is !NULL
134 */
136{
137 fr_dict_t *dict = fr_dict_unconst(rel->dict);
138 fr_dict_attr_t const *da = rel;
139 ssize_t slen;
140
141 *da_p = NULL;
142
143 /*
144 * Are we resolving a foreign reference?
145 */
146 if (fr_sbuff_next_if_char(in, '@')) {
147 char proto_name[FR_DICT_ATTR_MAX_NAME_LEN + 1];
148 fr_sbuff_t proto_name_sbuff = FR_SBUFF_OUT(proto_name, sizeof(proto_name));
149
150 /*
151 * @.foo is "foo from the current root".
152 *
153 * This is a bit clearer than "foo".
154 */
155 if (fr_sbuff_next_if_char(in, '.')) {
156 if (fr_sbuff_is_char(in, '.')) goto above_root;
157
158 da = rel->dict->root;
159 goto more;
160 }
161
162 slen = dict_by_protocol_substr(NULL, &dict, in, NULL);
163 /* Need to load it... */
164 if (slen <= 0) {
165 /* Quiet coverity */
166 fr_sbuff_terminate(&proto_name_sbuff);
167
168 /* Fixme, probably want to limit allowed chars */
169 if (fr_sbuff_out_bstrncpy_until(&proto_name_sbuff, in, SIZE_MAX,
170 &FR_SBUFF_TERMS(L(""), L(".")), NULL) <= 0) {
171 invalid_name:
172 fr_strerror_const("Invalid protocol name");
173 return -1;
174 }
175
176 /*
177 * The filenames are lowercase. The names in the dictionaries are case-insensitive. So
178 * we mash the name to all lowercase.
179 */
180 fr_tolower(proto_name);
181
182 /*
183 * Catch this early, so people don't do stupid things
184 * like put slashes in the references and then claim
185 * it's a security issue.
186 */
187 if (fr_dict_valid_oid_str(proto_name, -1) < 0) goto invalid_name;
188
189 /*
190 * Load the new dictionary, and mark it as loaded from our dictionary.
191 */
192 if (fr_dict_protocol_afrom_file(&dict, proto_name, NULL, (rel->dict)->root->name) < 0) {
193 return -1;
194 }
195
196 if (!fr_hash_table_insert((rel->dict)->autoref, dict)) {
197 fr_strerror_const("Failed inserting into internal autoref table");
198 return -1;
199 }
200 }
201
202 /*
203 * Didn't stop at an attribute ref... we're done
204 */
205 if (fr_sbuff_eof(in)) {
206 *da_p = dict->root;
207 return 1;
208 }
209
210 da = dict->root;
211 }
212
213 if (!fr_sbuff_next_if_char(in, '.')) {
214 fr_strerror_printf("Attribute %s has reference '%s' which does not begin with '.' or '@'",
215 rel->name, fr_sbuff_start(in));
216 return -1;
217 }
218
219 /*
220 * First '.' makes it reletive, subsequent ones traverse up the tree.
221 *
222 * No '.' means use the root.
223 */
224 while (fr_sbuff_next_if_char(in, '.')) {
225 if (!da->parent) {
226 above_root:
227 fr_strerror_const("Reference attempted to navigate above dictionary root");
228 return -1;
229 }
230 da = da->parent;
231 }
232
233 /*
234 * Look up the attribute. Note that this call will
235 * update *da_p with a partial reference if it exists.
236 */
237more:
238 slen = fr_dict_attr_by_oid_substr(NULL, da_p, da, in, NULL);
239 if (slen < 0) return -1;
240
241 if (slen == 0) {
242 *da_p = NULL;
243 return 0;
244 }
245
246 return 1;
247}
248
249/** Add an enumeration value to an attribute which has not yet been defined
250 *
251 * @param[in] fctx Holds current dictionary parsing information.
252 * @param[in] filename this fixup relates to.
253 * @param[in] line this fixup relates to.
254 * @param[in] attr The OID string pointing to the attribute
255 * to add the enumeration value to.
256 * @param[in] attr_len The length of the attr string.
257 * @param[in] name The name of the enumv.
258 * @param[in] name_len Length of the name string.
259 * @param[in] value Value string. This is kept as a string until we know
260 * what type we want to transform it into.
261 * @param[in] value_len Length of the value string.
262 * @param[in] parent of this attribute.
263 * @return
264 * - 0 on success.
265 * - -1 on out of memory.
266 */
267int dict_fixup_enumv_enqueue(dict_fixup_ctx_t *fctx, char const *filename, int line,
268 char const *attr, size_t attr_len,
269 char const *name, size_t name_len,
270 char const *value, size_t value_len,
271 fr_dict_attr_t const *parent)
272{
273 dict_fixup_enumv_t *fixup;
274
275 fixup = talloc(fctx->pool, dict_fixup_enumv_t);
276 if (!fixup) {
277 oom:
278 fr_strerror_const("Out of memory");
279 return -1;
280 }
281 *fixup = (dict_fixup_enumv_t) {
282 .attribute = talloc_bstrndup(fixup, attr, attr_len),
283 .name = talloc_bstrndup(fixup, name, name_len),
284 .value = talloc_bstrndup(fixup, value, value_len),
285 .parent = parent
286 };
287 if (!fixup->attribute || !fixup->name || !fixup->value) goto oom;
288
289 fixup->filename = talloc_strdup(fixup, filename);
290 if (!fixup->filename) goto oom;
291 fixup->line = line;
292
293 return dict_fixup_common(&fctx->enumv, &fixup->common);
294}
295
296/** Add a previously defined enumeration value to an existing attribute
297 *
298 * @param[in] fctx Holds current dictionary parsing information.
299 * @param[in] fixup Hash table to fill.
300 * @return
301 * - 0 on success.
302 * - -1 on failure.
303 */
304static inline CC_HINT(always_inline) int dict_fixup_enumv_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_enumv_t *fixup)
305{
306 fr_dict_attr_t *da;
309 int ret;
310 fr_dict_attr_t const *da_const;
311
312 da_const = fr_dict_attr_by_oid(NULL, fixup->parent, fixup->attribute);
313 if (!da_const) {
314 fr_strerror_printf_push("Failed resolving ATTRIBUTE referenced by VALUE '%s' at %s[%d]",
315 fixup->name, fr_cwd_strip(fixup->filename), fixup->line);
316 return -1;
317 }
318 da = fr_dict_attr_unconst(da_const);
319 type = da->type;
320
321 if (fr_value_box_from_str(fixup, &value, type, NULL,
322 fixup->value, talloc_array_length(fixup->value) - 1,
323 NULL) < 0) {
324 fr_strerror_printf_push("Invalid VALUE '%pV' for attribute '%s' at %s[%d]",
326 da->name,
327 fr_cwd_strip(fixup->filename), fixup->line);
328 return -1;
329 }
330
331 ret = fr_dict_enum_add_name(da, fixup->name, &value, false, false);
333 da->flags.has_fixup = false;
334
335 return ret;
336}
337
338/** Resolve a group reference
339 *
340 * This is required as the reference may point to another dictionary which
341 * hasn't been loaded yet.
342 *
343 * @param[in] fctx Holds current dictionary parsing information.
344 * @param[in] da The group dictionary attribute.
345 * @param[in] ref OID string representing what the group references.
346 * @return
347 * - 0 on success.
348 * - -1 on out of memory.
349 */
351{
352 dict_fixup_group_t *fixup;
353
354 fixup = talloc(fctx->pool, dict_fixup_group_t);
355 if (!fixup) {
356 fr_strerror_const("Out of memory");
357 return -1;
358 }
359 *fixup = (dict_fixup_group_t) {
360 .da = da,
361 .ref = talloc_strdup(fixup, ref),
362 };
363
364 da->flags.has_fixup = true;
365
366 return dict_fixup_common(&fctx->group, &fixup->common);
367}
368
369/** Resolve a group reference
370 *
371 * @param[in] fctx Holds current dictionary parsing information.
372 * @param[in] fixup Hash table to fill.
373 * @return
374 * - 0 on success.
375 * - -1 on failure.
376 */
377static inline CC_HINT(always_inline) int dict_fixup_group_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_group_t *fixup)
378{
379 fr_dict_attr_t const *da;
380
381 (void) fr_dict_protocol_reference(&da, fixup->da->parent, &FR_SBUFF_IN_STR(fixup->ref));
382 if (!da) {
383 fr_strerror_printf_push("Failed resolving reference for attribute at %s[%d]",
384 fr_cwd_strip(fixup->da->filename), fixup->da->line);
385 return -1;
386 }
387
388 if (da->type != FR_TYPE_TLV) {
389 fr_strerror_printf("References MUST be to attributes of type 'tlv' at %s[%d]",
390 fr_cwd_strip(fixup->da->filename), fixup->da->line);
391 return -1;
392 }
393
394 if (fr_dict_attr_ref(da)) {
395 fr_strerror_printf("References MUST NOT refer to an ATTRIBUTE which also has 'ref=...' at %s[%d]",
396 fr_cwd_strip(fixup->da->filename), fixup->da->line);
397 return -1;
398 }
399
400 fixup->da->flags.has_fixup = false;
401
402 return dict_attr_ref_resolve(fixup->da, da);
403}
404
405/** Clone one area of a tree into another
406 *
407 * These must be processed later to ensure that we've finished building an
408 * attribute by the time it has been cloned.
409 *
410 * @param[in] fctx Holds current dictionary parsing information.
411 * @param[in] da The group dictionary attribute.
412 * @param[in] ref OID string representing what the group references..
413 * @return
414 * - 0 on success.
415 * - -1 on out of memory.
416 */
418{
419 dict_fixup_clone_t *fixup;
420
421 /*
422 * Delay type checks until we've loaded all of the
423 * dictionaries. This means that errors are produced
424 * later, but that shouldn't matter for the default
425 * dictionaries. They're supposed to work.
426 */
427 fixup = talloc(fctx->pool, dict_fixup_clone_t);
428 if (!fixup) {
429 fr_strerror_const("Out of memory");
430 return -1;
431 }
432 *fixup = (dict_fixup_clone_t) {
433 .da = da,
434 .ref = talloc_typed_strdup(fixup, ref)
435 };
436
437 return dict_fixup_common(&fctx->clone, &fixup->common);
438}
439
440/** Clone a dictionary attribute from a ref
441 *
442 * @param[in] dst_p will either be inserted directly, with fields from the clone, or will be
443 * cloned, and then inserted. In this case the original dst da will be freed
444 * and the new cloned attribute will be written back to dst_p.
445 * @param[in] src to clone.
446 * @return
447 * - 0 on success.
448 * - -1 on failure.
449 */
451{
452 fr_dict_attr_t *dst = *dst_p;
453 fr_dict_t *dict = fr_dict_unconst(dst->dict);
454 fr_dict_attr_t *cloned;
455
456 if (src->dict->proto != dst->dict->proto) {
457 fr_strerror_printf("Incompatible protocols. Referenced '%s', referencing '%s'. Defined at %s[%d]",
458 src->dict->proto->name, dst->dict->proto->name, dst->filename, dst->line);
459 return -1;
460 }
461
462 /*
463 * The referenced DA is higher than the one we're
464 * creating. Ensure it's not a parent.
465 */
466 if (src->depth < dst->depth) {
467 fr_dict_attr_t const *parent;
468
469 for (parent = dst->parent; !parent->flags.is_root; parent = parent->parent) {
470 if (parent == src) {
471 fr_strerror_printf("References MUST NOT refer to a parent attribute %s at %s[%d]",
472 parent->name, fr_cwd_strip(dst->filename), dst->line);
473 return -1;
474 }
475 }
476 }
477
478 if (fr_dict_attr_ref(src)) {
479 fr_strerror_printf("References MUST NOT refer to an ATTRIBUTE which itself has a 'ref=...' at %s[%d]",
480 fr_cwd_strip(dst->filename), dst->line);
481 return -1;
482 }
483
484 /*
485 * If the attributes are of different types, then we have
486 * to _manually_ clone the values. This means looping
487 * over the ref da, and _casting_ the values to the new
488 * data type. If the cast succeeds, we add the value.
489 * Otherwise we don't
490 *
491 * We do this if the source type is a leaf node, AND the
492 * types are different, or the destination has no
493 * children.
494 */
495 if (!fr_type_is_non_leaf(dst->type) &&
496 ((src->type != dst->type) || !dict_attr_children(src))) {
497 int copied;
498
499 /*
500 * Only TLV and STRUCT types can be the source or destination of clones.
501 *
502 * Leaf types can be cloned, even if they're
503 * different types. But only if they don't have
504 * children (i.e. key fields).
505 */
506 if (fr_type_is_non_leaf(src->type) || fr_type_is_non_leaf(dst->type) ||
508 fr_strerror_printf("Reference MUST be to a simple data type of type '%s' at %s[%d]",
509 fr_type_to_str(dst->type),
510 fr_cwd_strip(dst->filename), dst->line);
511 return -1;
512 }
513
514 /*
515 * We copy all of the VALUEs over from the source
516 * da by hand, by casting them.
517 *
518 * We have to do this work manually because we
519 * can't call dict_attr_acopy(), as that function
520 * copies the VALUE with the *source* data type,
521 * where we need the *destination* data type.
522 */
523 copied = dict_attr_acopy_enumv(dst, src);
524 if (copied < 0) return -1;
525
526 if (!copied) {
527 fr_strerror_printf("Reference copied no VALUEs from type type '%s' at %s[%d]",
528 fr_type_to_str(dst->type),
529 fr_cwd_strip(dst->filename), dst->line);
530 return -1;
531 }
532
533 return 0;
534 }
535
536 /*
537 * Can't clone KEY fields directly, you MUST clone the parent struct.
538 */
540 fr_strerror_printf("Invalid reference from '%s' to %s", dst->name, src->name);
541 return -1;
542 }
543
544 /*
545 * Copy the source attribute, but with a
546 * new name and a new attribute number.
547 */
548 cloned = dict_attr_acopy(dict->pool, src, dst->name);
549 if (!cloned) {
550 fr_strerror_printf("Failed copying attribute '%s' to %s", src->name, dst->name);
551 return -1;
552 }
553
554 cloned->attr = dst->attr;
555 cloned->parent = dst->parent; /* we need to re-parent this attribute */
556 cloned->depth = cloned->parent->depth + 1;
557
558 /*
559 * Copy any pre-existing children over.
560 */
561 if (dict_attr_children(dst)) {
562 if (dict_attr_acopy_children(dict, cloned, dst) < 0) {
563 fr_strerror_printf("Failed populating attribute '%s' with children of %s - %s", src->name, dst->name, fr_strerror());
564 return -1;
565 }
566 }
567
568 /*
569 * Copy children of the DA we're cloning.
570 */
571 if (dict_attr_children(src)) {
572 if (dict_attr_acopy_children(dict, cloned, src) < 0) {
573 fr_strerror_printf("Failed populating attribute '%s' with children of %s - %s", src->name, dst->name, fr_strerror());
574 return -1;
575 }
576 }
577
578 if (fr_dict_attr_add_initialised(cloned) < 0) {
579 talloc_free(cloned);
580 return -1;
581 }
582
583 /*
584 * Free the original and pass back our new cloned attribute
585 */
586 talloc_free(dst);
587 *dst_p = cloned;
588
589 return 0;
590}
591
592/** Clone one are of a tree into another
593 *
594 * @param[in] fctx Holds current dictionary parsing information.
595 * @param[in] fixup Containing source/destination of the clone.
596 * @return
597 * - 0 on success.
598 * - -1 on failure.
599 */
600static inline CC_HINT(always_inline) int dict_fixup_clone_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_clone_t *fixup)
601{
602 fr_dict_attr_t const *src;
603
604 (void) fr_dict_protocol_reference(&src, fixup->da->parent, &FR_SBUFF_IN_STR(fixup->ref));
605 if (!src) {
606 fr_strerror_printf_push("Failed resolving reference for attribute at %s[%d]",
607 fr_cwd_strip(fixup->da->filename), fixup->da->line);
608 return -1;
609 }
610
611 fixup->da->flags.has_fixup = false;
612 return dict_fixup_clone(&fixup->da, src);
613}
614
615/** Clone enumeration values from one attribute to another
616 *
617 * These must be processed later to ensure that we've finished building an
618 * attribute by the time it has been cloned.
619 *
620 * @param[in] fctx Holds current dictionary parsing information.
621 * @param[in] da The group dictionary attribute.
622 * @param[in] ref OID string representing what the group references..
623 * @return
624 * - 0 on success.
625 * - -1 on out of memory.
626 */
628{
629 dict_fixup_clone_t *fixup;
630
631 /*
632 * Delay type checks until we've loaded all of the
633 * dictionaries. This means that errors are produced
634 * later, but that shouldn't matter for the default
635 * dictionaries. They're supposed to work.
636 */
637 fixup = talloc(fctx->pool, dict_fixup_clone_t);
638 if (!fixup) {
639 fr_strerror_const("Out of memory");
640 return -1;
641 }
642 *fixup = (dict_fixup_clone_t) {
643 .da = da,
644 .ref = talloc_typed_strdup(fixup, ref)
645 };
646
647 return dict_fixup_common(&fctx->clone, &fixup->common);
648}
649
650/** Clone one are of a tree into another
651 *
652 * @param[in] fctx Holds current dictionary parsing information.
653 * @param[in] fixup Containing source/destination of the clone.
654 * @return
655 * - 0 on success.
656 * - -1 on failure.
657 */
658static inline CC_HINT(always_inline) int dict_fixup_clone_enum_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_clone_t *fixup)
659{
660 fr_dict_attr_t const *src;
661 int copied;
662
663 (void) fr_dict_protocol_reference(&src, fixup->da->parent, &FR_SBUFF_IN_STR(fixup->ref));
664 if (!src) {
665 fr_strerror_printf_push("Failed resolving reference for attribute at %s[%d]",
666 fr_cwd_strip(fixup->da->filename), fixup->da->line);
667 return -1;
668 }
669
670 if (src->dict->proto != fixup->da->dict->proto) {
671 fr_strerror_printf("Incompatible protocols. Referenced '%s', referencing '%s'. Defined at %s[%d]",
672 src->dict->proto->name, fixup->da->dict->proto->name, fixup->da->filename, fixup->da->line);
673 return -1;
674 }
675
676 if (fr_dict_attr_ref(src)) {
677 fr_strerror_printf("References MUST NOT refer to an ATTRIBUTE which itself has a 'ref=...' at %s[%d]",
678 fr_cwd_strip(fixup->da->filename), fixup->da->line);
679 return -1;
680 }
681
682 if (!fr_type_is_non_leaf(fixup->da->type)) {
683 fr_strerror_printf("enum copy can only be applied to leaf types, not %s", fr_type_to_str(fixup->da->type));
684 return -1;
685 }
686
687 if (src->type != fixup->da->type) {
688 fr_strerror_printf("enum copy type mismatch. src '%s', dst '%s'",
689 fr_type_to_str(src->type), fr_type_to_str(fixup->da->type));
690 return -1;
691 }
692
693 /*
694 * We copy all of the VALUEs over from the source
695 * da by hand, by casting them.
696 *
697 * We have to do this work manually because we
698 * can't call dict_attr_acopy(), as that function
699 * copies the VALUE with the *source* data type,
700 * where we need the *destination* data type.
701 */
702 copied = dict_attr_acopy_enumv(fixup->da, src);
703 if (copied < 0) return -1;
704
705 if (!copied) {
706 fr_strerror_printf("Reference copied no VALUEs from type type '%s' at %s[%d]",
707 fr_type_to_str(fixup->da->type),
708 fr_cwd_strip(fixup->da->filename), fixup->da->line);
709 return -1;
710 }
711
712 fixup->da->flags.has_fixup = false;
713 return 0;
714}
715
716/** Push a fixup for a VSA.
717 *
718 * This is required so that we can define VENDORs for all VSAs, even
719 * if the dictionary doesn't contain VENDOR children for that VSA.
720 * This fixup means that we can define VENDORs elsewhere, and then
721 * use them in all VSA definitions. It means that we don't have to
722 * do these lookups at run-time.
723 *
724 * @param[in] fctx Holds current dictionary parsing information.
725 * @param[in] da The group dictionary attribute.
726 * @return
727 * - 0 on success.
728 * - -1 on out of memory.
729 */
731{
732 dict_fixup_vsa_t *fixup;
733
734 fixup = talloc(fctx->pool, dict_fixup_vsa_t);
735 if (!fixup) {
736 fr_strerror_const("Out of memory");
737 return -1;
738 }
739 *fixup = (dict_fixup_vsa_t) {
740 .da = da,
741 };
742
743 return dict_fixup_common(&fctx->vsa, &fixup->common);
744}
745
746/** Run VSA fixups
747 *
748 * @param[in] fctx Holds current dictionary parsing information.
749 * @param[in] fixup entry for fixup
750 * @return
751 * - 0 on success.
752 * - -1 on failure.
753 */
754static inline CC_HINT(always_inline) int dict_fixup_vsa_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_vsa_t *fixup)
755{
757 fr_dict_t *dict = fr_dict_unconst(fr_dict_by_da(fixup->da));
758 fr_hash_iter_t iter;
759
760 if (!dict->vendors_by_num) return 0;
761
762 for (dv = fr_hash_table_iter_init(dict->vendors_by_num, &iter);
763 dv;
764 dv = fr_hash_table_iter_next(dict->vendors_by_num, &iter)) {
765 if (dict_attr_child_by_num(fixup->da, dv->pen)) continue;
766
767 if (fr_dict_attr_add(dict, fixup->da, dv->name, dv->pen, FR_TYPE_VENDOR, NULL) < 0) return -1;
768 }
769
770 fixup->da->flags.has_fixup = false;
771 return 0;
772}
773
774
775/** Resolve a group reference
776 *
777 * This is required as the reference may point to another dictionary which
778 * hasn't been loaded yet.
779 *
780 * @param[in] fctx Holds current dictionary parsing information.
781 * @param[in] filename this fixup relates to.
782 * @param[in] line this fixup relates to.
783 * @param[in] alias_parent where to add the alias.
784 * @param[in] alias alias to add.
785 * @param[in] ref_parent attribute that should contain the reference.
786 * @param[in] ref OID string representing what the group references.
787 * @return
788 * - 0 on success.
789 * - -1 on out of memory.
790 */
791int dict_fixup_alias_enqueue(dict_fixup_ctx_t *fctx, char const *filename, int line,
792 fr_dict_attr_t *alias_parent, char const *alias,
793 fr_dict_attr_t *ref_parent, char const *ref)
794{
795 dict_fixup_alias_t *fixup;
796
797 fixup = talloc(fctx->pool, dict_fixup_alias_t);
798 if (!fixup) {
799 oom:
800 fr_strerror_const("Out of memory");
801 return -1;
802 }
803 *fixup = (dict_fixup_alias_t) {
804 .alias = talloc_typed_strdup(fixup, alias),
805 .alias_parent = alias_parent,
806 .ref = talloc_typed_strdup(fixup, ref),
807 .ref_parent = ref_parent
808 };
809
810 fixup->filename = talloc_strdup(fixup, filename);
811 if (!fixup->filename) goto oom;
812 fixup->line = line;
813
814 return dict_fixup_common(&fctx->alias, &fixup->common);
815}
816
817static inline CC_HINT(always_inline) int dict_fixup_alias_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_alias_t *fixup)
818{
819 fr_dict_attr_t const *da;
820
821 /*
822 * The <ref> can be a name.
823 */
824 da = fr_dict_attr_by_oid(NULL, fixup->ref_parent, fixup->ref);
825 if (!da) {
826 fr_strerror_printf("Attribute '%s' aliased by '%s' doesn't exist in namespace '%s', at %s[%d]",
827 fixup->ref, fixup->alias, fixup->ref_parent->name, fixup->filename, fixup->line);
828 return -1;
829 }
830
831 fr_dict_attr_unconst(da)->flags.has_fixup = false;
832 return dict_attr_alias_add(fixup->alias_parent, fixup->alias, da);
833}
834
835/** Initialise a fixup ctx
836 *
837 * @param[in] ctx to allocate the fixup pool in.
838 * @param[in] fctx to initialise.
839 * @return
840 * - 0 on success.
841 * - -1 on failure.
842 */
843int dict_fixup_init(TALLOC_CTX *ctx, dict_fixup_ctx_t *fctx)
844{
845 if (fctx->pool) return 0;
846
847 fr_dlist_talloc_init(&fctx->enumv, dict_fixup_enumv_t, common.entry);
848 fr_dlist_talloc_init(&fctx->group, dict_fixup_group_t, common.entry);
849 fr_dlist_talloc_init(&fctx->clone, dict_fixup_clone_t, common.entry);
850 fr_dlist_talloc_init(&fctx->vsa, dict_fixup_vsa_t, common.entry);
851 fr_dlist_talloc_init(&fctx->alias, dict_fixup_alias_t, common.entry);
852
853 fctx->pool = talloc_pool(ctx, DICT_FIXUP_POOL_SIZE);
854 if (!fctx->pool) return -1;
855
856 return 0;
857}
858
859/** Apply all outstanding fixes to a set of dictionaries
860 *
861 */
863{
864
865#define APPLY_FIXUP(_fctx, _list, _func, _type) \
866do { \
867 _type *_fixup; \
868 while ((_fixup = fr_dlist_head(&(_fctx)->_list))) { \
869 if (_func(_fctx, _fixup) < 0) return -1; \
870 fr_dlist_remove(&(_fctx)->_list, _fixup); \
871 talloc_free(_fixup); \
872 } \
873} while (0)
874
875 /*
876 * Apply all the fctx in order
877 *
878
879 * - Enumerations first as they have no dependencies
880 * - Group references next, as group attributes may be cloned.
881 * - Clones last as all other references and additions should
882 * be applied before cloning.
883 * - Clone enum clones the enumeration values from a dedicated
884 * enum, or another attribute with enumerations.
885 * - VSAs
886 * - Aliases last as all attributes need to be defined.
887 */
894
895 TALLOC_FREE(fctx->pool);
896
897 return 0;
898}
899
900/** Fixup all hash tables in the dictionary so they're suitable for threaded access
901 *
902 */
903static int _dict_attr_fixup_hash_tables(fr_dict_attr_t const *da, UNUSED void *uctx)
904{
905 {
907
909 if (ext) {
912 }
913 }
914
915 {
917
920 }
921
922 return 0;
923}
924
925/** Walk a dictionary finalising the hash tables in all attributes with a distinct namespace
926 *
927 * @param[in] dict to finalise namespaces for.
928 */
930{
932
933 (void)_dict_attr_fixup_hash_tables(root, NULL);
934
936
937 /*
938 * Walk over all of the hash tables to ensure they're
939 * initialized. We do this because the threads may perform
940 * lookups, and we don't want multi-threaded re-ordering
941 * of the table entries. That would be bad.
942 */
943 fr_hash_table_fill(dict->vendors_by_name);
944 fr_hash_table_fill(dict->vendors_by_num);
945}
int const char int line
Definition acutest.h:702
#define RCSID(id)
Definition build.h:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define UNUSED
Definition build.h:317
int dict_attr_acopy_children(fr_dict_t *dict, fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy the children of an existing attribute.
Definition dict_util.c:1177
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:1665
ssize_t fr_dict_valid_oid_str(char const *name, ssize_t len)
Definition dict_util.c:4781
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:2016
char const * name
Vendor name.
Definition dict.h:256
fr_dict_t * fr_dict_unconst(fr_dict_t const *dict)
Coerce to non-const.
Definition dict_util.c:4703
fr_dict_t const * fr_dict_by_da(fr_dict_attr_t const *da)
Attempt to locate the protocol dictionary containing an attribute.
Definition dict_util.c:2681
int fr_dict_walk(fr_dict_attr_t const *da, fr_dict_walk_t callback, void *uctx)
Definition dict_util.c:4895
fr_dict_attr_t * fr_dict_attr_unconst(fr_dict_attr_t const *da)
Coerce to non-const.
Definition dict_util.c:4715
fr_slen_t fr_dict_attr_by_oid_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_attr_t const *parent, fr_sbuff_t *in, fr_sbuff_term_t const *tt))
Resolve an attribute using an OID string.
Definition dict_util.c:2399
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
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2475
uint32_t pen
Private enterprise number.
Definition dict.h:252
@ FR_DICT_ATTR_EXT_ENUMV
Enumeration values.
Definition dict.h:175
int fr_dict_attr_add(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, unsigned int attr, fr_type_t type, fr_dict_attr_flags_t const *flags))
Add an attribute to the dictionary.
Definition dict_util.c:1787
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:2448
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:159
static fr_slen_t in
Definition dict.h:848
#define FR_DICT_ATTR_MAX_NAME_LEN
Maximum length of a attribute name.
Definition dict.h:480
Private enterprise.
Definition dict.h:251
fr_hash_table_t * name_by_value
Lookup a name by value.
Definition dict_ext.h:110
static void * fr_dict_attr_ext(fr_dict_attr_t const *da, fr_dict_attr_ext_t ext)
Definition dict_ext.h:140
fr_hash_table_t * value_by_name
Lookup an enumeration value by name.
Definition dict_ext.h:109
static fr_dict_attr_t const * fr_dict_attr_ref(fr_dict_attr_t const *da)
Return the reference associated with a group type attribute.
Definition dict_ext.h:184
Attribute extension - Holds enumeration values.
Definition dict_ext.h:107
static fr_hash_table_t * dict_attr_namespace(fr_dict_attr_t const *da)
Return the namespace hash table associated with the attribute.
static int dict_attr_ref_resolve(fr_dict_attr_t const *da, fr_dict_attr_t const *ref)
static fr_dict_attr_t const ** dict_attr_children(fr_dict_attr_t const *da)
fr_dict_attr_t * da
FR_TYPE_GROUP to fix.
Definition dict_fixup.c:78
dict_fixup_common_t common
Common fields.
Definition dict_fixup.c:59
int dict_fixup_apply(dict_fixup_ctx_t *fctx)
Apply all outstanding fixes to a set of dictionaries.
Definition dict_fixup.c:862
char * ref
the target attribute to clone
Definition dict_fixup.c:89
char * attribute
we couldn't find (and will need to resolve later).
Definition dict_fixup.c:64
static int dict_fixup_clone_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_clone_t *fixup)
Clone one are of a tree into another.
Definition dict_fixup.c:600
char * value
Raw enum value.
Definition dict_fixup.c:66
char * alias
we need to create.
Definition dict_fixup.c:48
char * ref
what the alias references.
Definition dict_fixup.c:51
static int dict_fixup_common(fr_dlist_head_t *fixup_list, dict_fixup_common_t *common)
Initialise common fields in fixup struct, and add it to a fixup list.
Definition dict_fixup.c:118
char * filename
where the line being fixed up.
Definition dict_fixup.c:61
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:267
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:791
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:417
dict_fixup_common_t common
Common fields.
Definition dict_fixup.c:96
dict_fixup_common_t common
Common fields.
Definition dict_fixup.c:76
static int dict_fixup_clone_enum_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_clone_t *fixup)
Clone one are of a tree into another.
Definition dict_fixup.c:658
int dict_fixup_init(TALLOC_CTX *ctx, dict_fixup_ctx_t *fctx)
Initialise a fixup ctx.
Definition dict_fixup.c:843
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:627
static int dict_fixup_vsa_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_vsa_t *fixup)
Run VSA fixups.
Definition dict_fixup.c:754
fr_dict_attr_t * alias_parent
Where to add the alias.
Definition dict_fixup.c:49
fr_dlist_t entry
Entry in linked list of fctx.
Definition dict_fixup.c:36
fr_dict_attr_t const * parent
Parent attribute to resolve the 'attribute' string in.
Definition dict_fixup.c:69
static int _dict_attr_fixup_hash_tables(fr_dict_attr_t const *da, UNUSED void *uctx)
Fixup all hash tables in the dictionary so they're suitable for threaded access.
Definition dict_fixup.c:903
dict_fixup_common_t common
Common fields.
Definition dict_fixup.c:105
char * filename
where the line being fixed up.
Definition dict_fixup.c:45
fr_hash_table_t * hash
We need to finalise.
Definition dict_fixup.c:107
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:730
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:450
static int dict_fixup_group_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_group_t *fixup)
Resolve a group reference.
Definition dict_fixup.c:377
dict_fixup_common_t common
Common fields.
Definition dict_fixup.c:43
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:350
char * ref
the reference name
Definition dict_fixup.c:79
int fr_dict_protocol_reference(fr_dict_attr_t const **da_p, fr_dict_attr_t const *rel, fr_sbuff_t *in)
Resolve a reference string to a dictionary attribute.
Definition dict_fixup.c:135
static int dict_fixup_enumv_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_enumv_t *fixup)
Add a previously defined enumeration value to an existing attribute.
Definition dict_fixup.c:304
#define APPLY_FIXUP(_fctx, _list, _func, _type)
fr_dict_attr_t * ref_parent
Parent attribute to resolve the 'attribute' string in.
Definition dict_fixup.c:52
char * name
Raw enum name.
Definition dict_fixup.c:65
void dict_hash_tables_finalise(fr_dict_t *dict)
Walk a dictionary finalising the hash tables in all attributes with a distinct namespace.
Definition dict_fixup.c:929
fr_dict_attr_t * da
FR_TYPE_VSA to fix.
Definition dict_fixup.c:98
static int dict_fixup_alias_apply(UNUSED dict_fixup_ctx_t *fctx, dict_fixup_alias_t *fixup)
Definition dict_fixup.c:817
fr_dict_attr_t * da
to populate with cloned information.
Definition dict_fixup.c:88
dict_fixup_common_t common
Common fields.
Definition dict_fixup.c:86
Add an enumeration value to an attribute that wasn't defined at the time the value was parsed.
Definition dict_fixup.c:42
Clone operation from one tree node to another.
Definition dict_fixup.c:85
Common fields for every fixup structure.
Definition dict_fixup.c:35
Add an enumeration value to an attribute that wasn't defined at the time the value was parsed.
Definition dict_fixup.c:58
Resolve a group reference.
Definition dict_fixup.c:75
Dictionary attribute namespaces need their hash tables finalised.
Definition dict_fixup.c:104
Run fixup callbacks for a VSA.
Definition dict_fixup.c:95
Functions to finalise and fixup dictionaries.
fr_dlist_head_t alias
Aliases that can't be resolved immediately.
fr_dlist_head_t group
Group references to resolve.
fr_dlist_head_t clone
Clone operation to apply.
fr_dlist_head_t vsa
VSAs to add vendors for.
fr_dlist_head_t enumv
Raw enumeration values to add.
TALLOC_CTX * pool
Temporary pool for fixups, reduces holes.
fr_slen_t dict_by_protocol_substr(fr_dict_attr_err_t *err, fr_dict_t **out, fr_sbuff_t *name, fr_dict_t const *dict_def)
Definition dict_util.c:2490
#define DICT_FIXUP_POOL_SIZE
Definition dict_priv.h:38
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:3356
fr_dict_attr_t * dict_attr_acopy(TALLOC_CTX *ctx, fr_dict_attr_t const *in, char const *new_name)
Copy a an existing attribute.
Definition dict_util.c:1089
int dict_attr_alias_add(fr_dict_attr_t const *parent, char const *alias, fr_dict_attr_t const *ref)
Add an alias to an existing attribute.
Definition dict_util.c:1249
int dict_attr_acopy_enumv(fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy the VALUEs of an existing attribute, by casting them.
Definition dict_util.c:1220
Test enumeration values.
Definition dict_test.h:92
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:378
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:275
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
char const * fr_cwd_strip(char const *filename)
Intended to be used in logging functions to make output more readable.
Definition file.c:384
void * fr_hash_table_iter_next(fr_hash_table_t *ht, fr_hash_iter_t *iter)
Iterate over entries in a hash table.
Definition hash.c:626
void * fr_hash_table_iter_init(fr_hash_table_t *ht, fr_hash_iter_t *iter)
Initialise an iterator.
Definition hash.c:678
bool fr_hash_table_insert(fr_hash_table_t *ht, void const *data)
Insert data into a hash table.
Definition hash.c:468
void fr_hash_table_fill(fr_hash_table_t *ht)
Ensure all buckets are filled.
Definition hash.c:719
Stores the state of the current iteration operation.
Definition hash.h:41
talloc_free(reap)
fr_type_t
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_VENDOR
Attribute that represents a vendor in the attribute tree.
long int ssize_t
size_t fr_sbuff_out_bstrncpy_until(fr_sbuff_t *out, fr_sbuff_t *in, size_t len, fr_sbuff_term_t const *tt, fr_sbuff_unescape_rules_t const *u_rules)
char * fr_tolower(char *str)
Definition misc.c:226
static unsigned int hash(char const *username, unsigned int tablesize)
Definition rlm_passwd.c:132
static char const * name
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
Definition sbuff.c:2116
#define fr_sbuff_start(_sbuff_or_marker)
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition sbuff.h:192
#define fr_sbuff_is_char(_sbuff_or_marker, _c)
#define fr_sbuff_eof(_x)
#define FR_SBUFF_IN_STR(_start)
#define FR_SBUFF_OUT(_start, _len_or_end)
fr_aka_sim_id_type_t type
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:586
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:467
static fr_slen_t parent
Definition pair.h:841
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#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(_msg)
Definition strerror.h:223
#define fr_type_is_non_leaf(_x)
Definition types.h:392
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:452
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:5754
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:4139
#define fr_box_strvalue_buffer(_val)
Definition value.h:311
#define FR_VALUE_BOX_INITIALISER_NULL(_vb)
A static initialiser for stack/globally allocated boxes.
Definition value.h:510