From: Alan T. DeKok Date: Thu, 24 Nov 2022 14:39:02 +0000 (-0500) Subject: add fr_dict_protocol_alloc() for use with local dictionaries X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2aad09fa4d6c33ef905cb88055cd587bcb92e91;p=thirdparty%2Ffreeradius-server.git add fr_dict_protocol_alloc() for use with local dictionaries --- diff --git a/src/lib/util/dict.h b/src/lib/util/dict.h index eda4c353b21..d3025549892 100644 --- a/src/lib/util/dict.h +++ b/src/lib/util/dict.h @@ -587,6 +587,8 @@ int fr_dict_internal_afrom_file(fr_dict_t **out, char const *internal_name, int fr_dict_protocol_afrom_file(fr_dict_t **out, char const *proto_name, char const *proto_dir, char const *dependent); +fr_dict_t *fr_dict_protocol_alloc(TALLOC_CTX *ctx); + int fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename); /** @} */ diff --git a/src/lib/util/dict_util.c b/src/lib/util/dict_util.c index 4bfea70df3f..f72f9397d07 100644 --- a/src/lib/util/dict_util.c +++ b/src/lib/util/dict_util.c @@ -3374,6 +3374,57 @@ fr_dict_t *dict_alloc(TALLOC_CTX *ctx) return dict; } +/** Allocate a new local dictionary + * + * @param[in] ctx to allocate dictionary in. + * @return + * - NULL on memory allocation error. + * + * This dictionary cannot define vendors, or inter-dictionary + * dependencies. However, we initialize the relevant fields just in + * case. We should arguably just skip initializing those fields, and + * just allow the server to crash if programmers do something stupid with it. + */ +fr_dict_t *fr_dict_protocol_alloc(TALLOC_CTX *ctx) +{ + fr_dict_t *dict; + fr_dict_attr_t *da; + + fr_dict_attr_flags_t flags = { + .is_root = 1, + .type_size = 2, + .length = 2 + }; + + + dict = dict_alloc(ctx); + if (!dict) return NULL; + + /* + * Allow for 64k local attributes. + */ + dict->default_type_size = 2; + dict->default_type_length = 2; + dict->self_allocated = (1 << 24); + + /* + * Allocate the root attribute. This dictionary is + * always protocol "local", and number "0". + */ + da = dict_attr_alloc(dict->pool, NULL, "local", 0, FR_TYPE_TLV, + &(dict_attr_args_t){ .flags = &flags }); + if (unlikely(!da)) { + talloc_free(dict); + return NULL; + } + + dict->root = da; + dict->root->dict = dict; + DA_VERIFY(dict->root); + + return dict; +} + /** Decrement the reference count on a previously loaded dictionary * * @param[in] dict to free.