From: Arran Cudbard-Bell Date: Wed, 23 Oct 2024 05:30:52 +0000 (-0600) Subject: Explicitly mark up attributes as name only instead of using magic numbers X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a45235e07b5f0c069fd37f47784ba2897e4bba9b;p=thirdparty%2Ffreeradius-server.git Explicitly mark up attributes as name only instead of using magic numbers --- diff --git a/src/lib/server/tmpl_tokenize.c b/src/lib/server/tmpl_tokenize.c index b872bf00e48..254a02dd17f 100644 --- a/src/lib/server/tmpl_tokenize.c +++ b/src/lib/server/tmpl_tokenize.c @@ -4517,9 +4517,12 @@ int tmpl_attr_unknown_add(tmpl_t *vpt) * - -1 on failure. */ int tmpl_attr_tail_unresolved_add(fr_dict_t *dict_def, tmpl_t *vpt, - fr_type_t type, fr_dict_attr_flags_t const *flags) + fr_type_t type, fr_dict_attr_flags_t const *flags) { fr_dict_attr_t const *da; + fr_dict_attr_flags_t our_flags = *flags; + + our_flags.name_only = true; if (!vpt) return -1; @@ -4528,7 +4531,7 @@ int tmpl_attr_tail_unresolved_add(fr_dict_t *dict_def, tmpl_t *vpt, if (!tmpl_is_attr_unresolved(vpt)) return 1; if (fr_dict_attr_add(dict_def, - fr_dict_root(fr_dict_internal()), tmpl_attr_tail_unresolved(vpt), -1, type, flags) < 0) { + fr_dict_root(fr_dict_internal()), tmpl_attr_tail_unresolved(vpt), 0, type, &our_flags) < 0) { return -1; } da = fr_dict_attr_by_name(NULL, fr_dict_root(dict_def), tmpl_attr_tail_unresolved(vpt)); diff --git a/src/lib/server/virtual_servers.c b/src/lib/server/virtual_servers.c index c2428dce2c3..a3598e0e5cb 100644 --- a/src/lib/server/virtual_servers.c +++ b/src/lib/server/virtual_servers.c @@ -1260,6 +1260,7 @@ static int define_server_attrs(CONF_SECTION *cs, fr_dict_t *dict, fr_dict_attr_t fr_dict_attr_flags_t flags = { .internal = true, + .name_only = true, .local = true, }; @@ -1339,7 +1340,7 @@ static int define_server_attrs(CONF_SECTION *cs, fr_dict_t *dict, fr_dict_attr_t return -1; } - if (fr_dict_attr_add(dict, parent, value, -1, type, &flags) < 0) { + if (fr_dict_attr_add(dict, parent, value, 0, type, &flags) < 0) { cf_log_err(ci, "Failed adding local variable '%s' - %s", value, fr_strerror()); return -1; } diff --git a/src/modules/rlm_ldap/rlm_ldap.c b/src/modules/rlm_ldap/rlm_ldap.c index 652889ec8ce..60c35a34817 100644 --- a/src/modules/rlm_ldap/rlm_ldap.c +++ b/src/modules/rlm_ldap/rlm_ldap.c @@ -2673,11 +2673,10 @@ static int mod_bootstrap(module_inst_ctx_t const *mctx) * If the group attribute was not in the dictionary, create it */ if (!boot->group_da) { - fr_dict_attr_flags_t flags; + fr_dict_attr_flags_t flags = { .name_only = 1 }; - memset(&flags, 0, sizeof(flags)); if (fr_dict_attr_add(fr_dict_unconst(dict_freeradius), fr_dict_root(dict_freeradius), - group_attribute, -1, FR_TYPE_STRING, &flags) < 0) { + group_attribute, 0, FR_TYPE_STRING, &flags) < 0) { PERROR("Error creating group attribute"); return -1; @@ -2689,11 +2688,10 @@ static int mod_bootstrap(module_inst_ctx_t const *mctx) * Setup the cache attribute */ if (inst->group.cache_attribute) { - fr_dict_attr_flags_t flags; + fr_dict_attr_flags_t flags = { .name_only = 1 }; - memset(&flags, 0, sizeof(flags)); if (fr_dict_attr_add(fr_dict_unconst(dict_freeradius), fr_dict_root(dict_freeradius), - inst->group.cache_attribute, -1, FR_TYPE_STRING, &flags) < 0) { + inst->group.cache_attribute, 0, FR_TYPE_STRING, &flags) < 0) { PERROR("Error creating cache attribute"); return -1; diff --git a/src/modules/rlm_sql/rlm_sql.c b/src/modules/rlm_sql/rlm_sql.c index 3b5fc7abab6..243e79497f8 100644 --- a/src/modules/rlm_sql/rlm_sql.c +++ b/src/modules/rlm_sql/rlm_sql.c @@ -35,6 +35,7 @@ RCSID("$Id$") #include #include #include +#include #include #include #include @@ -2212,7 +2213,7 @@ static int mod_bootstrap(module_inst_ctx_t const *mctx) */ if (cf_pair_find(conf, "group_membership_query")) { char const *group_attribute; - fr_dict_attr_flags_t flags = {}; + fr_dict_attr_flags_t flags = { .name_only = 1 }; char buffer[256]; if (inst->config.group_attribute) { @@ -2224,17 +2225,20 @@ static int mod_bootstrap(module_inst_ctx_t const *mctx) group_attribute = "SQL-Group"; } - if (fr_dict_attr_add(fr_dict_unconst(dict_freeradius), fr_dict_root(dict_freeradius), group_attribute, -1, - FR_TYPE_STRING, &flags) < 0) { - cf_log_perr(conf, "Failed defining group attribute"); - return -1; - } - - boot->group_da = fr_dict_attr_search_by_qualified_oid(NULL, dict_freeradius, group_attribute, - false, false); + boot->group_da = fr_dict_attr_by_name(NULL, fr_dict_root(dict_freeradius), group_attribute); if (!boot->group_da) { - cf_log_perr(conf, "Failed resolving group attribute"); - return -1; + if (fr_dict_attr_add(fr_dict_unconst(dict_freeradius), fr_dict_root(dict_freeradius), group_attribute, 0, + FR_TYPE_STRING, &flags) < 0) { + cf_log_perr(conf, "Failed defining group attribute"); + return -1; + } + + boot->group_da = fr_dict_attr_search_by_qualified_oid(NULL, dict_freeradius, group_attribute, + false, false); + if (!boot->group_da) { + cf_log_perr(conf, "Failed resolving group attribute"); + return -1; + } } /* diff --git a/src/modules/rlm_sqlcounter/rlm_sqlcounter.c b/src/modules/rlm_sqlcounter/rlm_sqlcounter.c index d2eb231889e..a712f718b61 100644 --- a/src/modules/rlm_sqlcounter/rlm_sqlcounter.c +++ b/src/modules/rlm_sqlcounter/rlm_sqlcounter.c @@ -30,6 +30,7 @@ RCSID("$Id$") #include #include #include +#include #include #include @@ -538,28 +539,34 @@ static int mod_instantiate(module_inst_ctx_t const *mctx) return 0; } -#define ATTR_CHECK(_tmpl, _name) if (tmpl_is_attr_unresolved(inst->_tmpl)) { \ - if (fr_dict_attr_add(fr_dict_unconst(dict_freeradius), fr_dict_root(dict_freeradius), tmpl_attr_tail_unresolved(inst->_tmpl), -1, FR_TYPE_UINT64, &flags) < 0) { \ - cf_log_perr(conf, "Failed defining %s attribute", _name); \ - return -1; \ - } \ -} else if (tmpl_is_attr(inst->_tmpl)) { \ - if (tmpl_attr_tail_da(inst->_tmpl)->type != FR_TYPE_UINT64) { \ - cf_log_err(conf, "%s attribute %s must be uint64", _name, tmpl_attr_tail_da(inst->_tmpl)->name); \ - return -1; \ - } \ +static inline int attr_check(CONF_SECTION *conf, tmpl_t *tmpl, char const *name, fr_dict_attr_flags_t *flags) +{ + if (tmpl_is_attr_unresolved(tmpl) && !fr_dict_attr_by_name(NULL, fr_dict_root(dict_freeradius), tmpl_attr_tail_unresolved(tmpl))) { + if (fr_dict_attr_add(fr_dict_unconst(dict_freeradius), fr_dict_root(dict_freeradius), + tmpl_attr_tail_unresolved(tmpl), 0, FR_TYPE_UINT64, flags) < 0) { + cf_log_perr(conf, "Failed defining %s attribute", name); + return -1; + } + } else if (tmpl_is_attr(tmpl)) { + if (tmpl_attr_tail_da(tmpl)->type != FR_TYPE_UINT64) { + cf_log_err(conf, "%s attribute %s must be uint64", name, tmpl_attr_tail_da(tmpl)->name); + return -1; + } + } + + return 0; } static int mod_bootstrap(module_inst_ctx_t const *mctx) { rlm_sqlcounter_t const *inst = talloc_get_type_abort(mctx->mi->data, rlm_sqlcounter_t); CONF_SECTION *conf = mctx->mi->conf; - fr_dict_attr_flags_t flags = (fr_dict_attr_flags_t) { .internal = 1, .length = 8 }; + fr_dict_attr_flags_t flags = { .internal = 1, .length = 8, .name_only = 1 }; - ATTR_CHECK(start_attr, "reset_period_start") - ATTR_CHECK(end_attr, "reset_period_end") - ATTR_CHECK(counter_attr, "counter") - ATTR_CHECK(limit_attr, "check") + if (unlikely(attr_check(conf, inst->start_attr, "reset_period_start", &flags) < 0)) return -1; + if (unlikely(attr_check(conf, inst->end_attr, "reset_period_end", &flags) < 0)) return -1; + if (unlikely(attr_check(conf, inst->counter_attr, "counter", &flags) < 0)) return -1; + if (unlikely(attr_check(conf, inst->limit_attr, "check", &flags) < 0)) return -1; return 0; }