From: Arran Cudbard-Bell Date: Fri, 24 May 2024 16:13:22 +0000 (-0400) Subject: Remove the last of the XLAT_MONO code X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04618796bdb8a52f489ff70cb99fd8eabeeba6ca;p=thirdparty%2Ffreeradius-server.git Remove the last of the XLAT_MONO code Looks like this enables some purification that wasn't happening previously. The parts of tests removed were either useless, or the tests were incorrect. --- diff --git a/src/lib/unlang/xlat.h b/src/lib/unlang/xlat.h index 3e0012cdb01..797c44634fa 100644 --- a/src/lib/unlang/xlat.h +++ b/src/lib/unlang/xlat.h @@ -44,7 +44,6 @@ typedef enum { typedef enum { XLAT_INPUT_UNPROCESSED, //!< No input argument processing - XLAT_INPUT_MONO, //!< Ingests a single argument XLAT_INPUT_ARGS //!< Ingests a number of arguments } xlat_input_type_t; @@ -409,9 +408,7 @@ static inline fr_slen_t xlat_aprint(TALLOC_CTX *ctx, char **out, xlat_exp_head_t bool xlat_is_truthy(xlat_exp_head_t const *head, bool *out); -int xlat_validate_function_mono(xlat_exp_t *node); - -int xlat_validate_function_args(xlat_exp_t *node); +fr_slen_t xlat_validate_function_args(xlat_exp_t *node); void xlat_debug(xlat_exp_t const *node); diff --git a/src/lib/unlang/xlat_builtin.c b/src/lib/unlang/xlat_builtin.c index 5c0f46d5477..b77483f0644 100644 --- a/src/lib/unlang/xlat_builtin.c +++ b/src/lib/unlang/xlat_builtin.c @@ -4005,6 +4005,8 @@ do { \ XLAT_REGISTER_ARGS("trigger", trigger_xlat, FR_TYPE_STRING, trigger_xlat_args); XLAT_REGISTER_ARGS("base64.encode", xlat_func_base64_encode, FR_TYPE_STRING, xlat_func_base64_encode_arg); XLAT_REGISTER_ARGS("base64.decode", xlat_func_base64_decode, FR_TYPE_OCTETS, xlat_func_base64_decode_arg); + XLAT_REGISTER_ARGS("rand", xlat_func_rand, FR_TYPE_UINT64, xlat_func_rand_arg); + XLAT_REGISTER_ARGS("randstr", xlat_func_randstr, FR_TYPE_STRING, xlat_func_randstr_arg); if (unlikely((xlat = xlat_func_register(xlat_ctx, "untaint", xlat_func_untaint, FR_TYPE_VOID)) == NULL)) return -1; xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); @@ -4015,61 +4017,50 @@ do { \ /* * All of these functions are pure. */ -#define XLAT_REGISTER_MONO(_xlat, _func, _return_type, _arg) \ +#define XLAT_REGISTER_PURE(_xlat, _func, _return_type, _arg) \ do { \ if (unlikely((xlat = xlat_func_register(xlat_ctx, _xlat, _func, _return_type)) == NULL)) return -1; \ - xlat_func_mono_set(xlat, _arg); \ + xlat_func_args_set(xlat, _arg); \ xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); \ } while (0) - XLAT_REGISTER_MONO("bin", xlat_func_bin, FR_TYPE_OCTETS, xlat_func_bin_arg); - XLAT_REGISTER_MONO("hex", xlat_func_hex, FR_TYPE_STRING, xlat_func_hex_arg); - XLAT_REGISTER_MONO("map", xlat_func_map, FR_TYPE_INT8, xlat_func_map_arg); - XLAT_REGISTER_MONO("md4", xlat_func_md4, FR_TYPE_OCTETS, xlat_func_md4_arg); - XLAT_REGISTER_MONO("md5", xlat_func_md5, FR_TYPE_OCTETS, xlat_func_md5_arg); + XLAT_REGISTER_PURE("bin", xlat_func_bin, FR_TYPE_OCTETS, xlat_func_bin_arg); + XLAT_REGISTER_PURE("hex", xlat_func_hex, FR_TYPE_STRING, xlat_func_hex_arg); + XLAT_REGISTER_PURE("map", xlat_func_map, FR_TYPE_INT8, xlat_func_map_arg); + XLAT_REGISTER_PURE("md4", xlat_func_md4, FR_TYPE_OCTETS, xlat_func_md4_arg); + XLAT_REGISTER_PURE("md5", xlat_func_md5, FR_TYPE_OCTETS, xlat_func_md5_arg); #if defined(HAVE_REGEX_PCRE) || defined(HAVE_REGEX_PCRE2) if (unlikely((xlat = xlat_func_register(xlat_ctx, "regex", xlat_func_regex, FR_TYPE_STRING)) == NULL)) return -1; xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_INTERNAL); #endif - XLAT_REGISTER_MONO("sha1", xlat_func_sha1, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha1", xlat_func_sha1, FR_TYPE_OCTETS, xlat_func_sha_arg); #ifdef HAVE_OPENSSL_EVP_H - XLAT_REGISTER_MONO("sha2_224", xlat_func_sha2_224, FR_TYPE_OCTETS, xlat_func_sha_arg); - XLAT_REGISTER_MONO("sha2_256", xlat_func_sha2_256, FR_TYPE_OCTETS, xlat_func_sha_arg); - XLAT_REGISTER_MONO("sha2_384", xlat_func_sha2_384, FR_TYPE_OCTETS, xlat_func_sha_arg); - XLAT_REGISTER_MONO("sha2_512", xlat_func_sha2_512, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha2_224", xlat_func_sha2_224, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha2_256", xlat_func_sha2_256, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha2_384", xlat_func_sha2_384, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha2_512", xlat_func_sha2_512, FR_TYPE_OCTETS, xlat_func_sha_arg); - XLAT_REGISTER_MONO("blake2s_256", xlat_func_blake2s_256, FR_TYPE_OCTETS, xlat_func_sha_arg); - XLAT_REGISTER_MONO("blake2b_512", xlat_func_blake2b_512, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("blake2s_256", xlat_func_blake2s_256, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("blake2b_512", xlat_func_blake2b_512, FR_TYPE_OCTETS, xlat_func_sha_arg); # if OPENSSL_VERSION_NUMBER >= 0x10101000L - XLAT_REGISTER_MONO("sha3_224", xlat_func_sha3_224, FR_TYPE_OCTETS, xlat_func_sha_arg); - XLAT_REGISTER_MONO("sha3_256", xlat_func_sha3_256, FR_TYPE_OCTETS, xlat_func_sha_arg); - XLAT_REGISTER_MONO("sha3_384", xlat_func_sha3_384, FR_TYPE_OCTETS, xlat_func_sha_arg); - XLAT_REGISTER_MONO("sha3_512", xlat_func_sha3_512, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha3_224", xlat_func_sha3_224, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha3_256", xlat_func_sha3_256, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha3_384", xlat_func_sha3_384, FR_TYPE_OCTETS, xlat_func_sha_arg); + XLAT_REGISTER_PURE("sha3_512", xlat_func_sha3_512, FR_TYPE_OCTETS, xlat_func_sha_arg); # endif #endif - XLAT_REGISTER_MONO("string", xlat_func_string, FR_TYPE_STRING, xlat_func_string_arg); - XLAT_REGISTER_MONO("strlen", xlat_func_strlen, FR_TYPE_SIZE, xlat_func_strlen_arg); - XLAT_REGISTER_MONO("tolower", xlat_func_tolower, FR_TYPE_STRING, xlat_change_case_arg); - XLAT_REGISTER_MONO("toupper", xlat_func_toupper, FR_TYPE_STRING, xlat_change_case_arg); - XLAT_REGISTER_MONO("urlquote", xlat_func_urlquote, FR_TYPE_STRING, xlat_func_urlquote_arg); - XLAT_REGISTER_MONO("urlunquote", xlat_func_urlunquote, FR_TYPE_STRING, xlat_func_urlunquote_arg); - XLAT_REGISTER_MONO("eval", xlat_func_eval, FR_TYPE_VOID, xlat_func_eval_arg); + XLAT_REGISTER_PURE("string", xlat_func_string, FR_TYPE_STRING, xlat_func_string_arg); + XLAT_REGISTER_PURE("strlen", xlat_func_strlen, FR_TYPE_SIZE, xlat_func_strlen_arg); + XLAT_REGISTER_PURE("tolower", xlat_func_tolower, FR_TYPE_STRING, xlat_change_case_arg); + XLAT_REGISTER_PURE("toupper", xlat_func_toupper, FR_TYPE_STRING, xlat_change_case_arg); + XLAT_REGISTER_PURE("urlquote", xlat_func_urlquote, FR_TYPE_STRING, xlat_func_urlquote_arg); + XLAT_REGISTER_PURE("urlunquote", xlat_func_urlunquote, FR_TYPE_STRING, xlat_func_urlunquote_arg); + XLAT_REGISTER_PURE("eval", xlat_func_eval, FR_TYPE_VOID, xlat_func_eval_arg); xlat_func_instantiate_set(xlat, xlat_eval_instantiate, xlat_eval_inst_t, NULL, NULL); -#undef XLAT_REGISTER_MONO -#define XLAT_REGISTER_MONO(_xlat, _func, _return_type, _arg) \ -do { \ - if (unlikely((xlat = xlat_func_register(xlat_ctx, _xlat, _func, _return_type)) == NULL)) return -1; \ - xlat_func_mono_set(xlat, _arg); \ - xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_INTERNAL); \ -} while (0) - - XLAT_REGISTER_MONO("rand", xlat_func_rand, FR_TYPE_UINT64, xlat_func_rand_arg); - XLAT_REGISTER_MONO("randstr", xlat_func_randstr, FR_TYPE_STRING, xlat_func_randstr_arg); - return xlat_register_expressions(); } diff --git a/src/lib/unlang/xlat_eval.c b/src/lib/unlang/xlat_eval.c index 91ac371a226..88b803e1af4 100644 --- a/src/lib/unlang/xlat_eval.c +++ b/src/lib/unlang/xlat_eval.c @@ -352,7 +352,6 @@ xlat_action_t xlat_process_args(TALLOC_CTX *ctx, fr_value_box_list_t *list, /* * xlat takes all input as a single vb. */ - case XLAT_INPUT_MONO: case XLAT_INPUT_ARGS: vb = fr_value_box_list_head(list); while (arg_p->type != FR_TYPE_NULL) { diff --git a/src/lib/unlang/xlat_func.c b/src/lib/unlang/xlat_func.c index 91a1f4a396b..5d254acfef8 100644 --- a/src/lib/unlang/xlat_func.c +++ b/src/lib/unlang/xlat_func.c @@ -387,24 +387,6 @@ int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[]) return 0; } -/** Register the argument of an xlat - * - * For xlats that take all their input as a single argument - * - * @param[in,out] x to have it's arguments registered - * @param[in] args to be registered - * @return - * - 0 on success. - * - < 0 on failure. - */ -int xlat_func_mono_set(xlat_t *x, xlat_arg_parser_t const args[]) -{ - if (xlat_func_args_set(x, args) < 0) return -1; - x->input_type = XLAT_INPUT_MONO; - - return 0; -} - /** Register call environment of an xlat * * @param[in,out] x to have it's module method env registered. diff --git a/src/lib/unlang/xlat_func.h b/src/lib/unlang/xlat_func.h index f71de8995b4..e5b8a26b12f 100644 --- a/src/lib/unlang/xlat_func.h +++ b/src/lib/unlang/xlat_func.h @@ -60,8 +60,6 @@ xlat_t *xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, int xlat_func_args_set(xlat_t *xlat, xlat_arg_parser_t const args[]) CC_HINT(nonnull); -int xlat_func_mono_set(xlat_t *xlat, xlat_arg_parser_t const *arg) CC_HINT(nonnull); - void xlat_func_call_env_set(xlat_t *x, call_env_method_t const *env) CC_HINT(nonnull); void xlat_func_flags_set(xlat_t *x, xlat_func_flags_t flags) CC_HINT(nonnull); diff --git a/src/lib/unlang/xlat_redundant.c b/src/lib/unlang/xlat_redundant.c index efc200b7a8b..cfb3455edc6 100644 --- a/src/lib/unlang/xlat_redundant.c +++ b/src/lib/unlang/xlat_redundant.c @@ -231,37 +231,6 @@ static int xlat_redundant_instantiate(xlat_inst_ctx_t const *xctx) first = talloc_get_type_abort(fr_dlist_head(&xr->funcs), xlat_redundant_func_t); - /* - * Check the calling style matches the first - * function. - * - * We do this here as the redundant xlat - * itself can't have an input type or - * defined arguments; - */ - switch (xctx->ex->call.input_type) { - case XLAT_INPUT_UNPROCESSED: - break; - - case XLAT_INPUT_MONO: - if (first->func->input_type == XLAT_INPUT_ARGS) { - PERROR("Expansion function \"%s\" takes defined arguments and should " - "be called using %%(func:args) syntax", - xctx->ex->call.func->name); - return -1; - - } - break; - - case XLAT_INPUT_ARGS: - if (first->func->input_type == XLAT_INPUT_MONO) { - PERROR("Expansion function \"%s\" should be called using %%{func:arg} syntax", - xctx->ex->call.func->name); - return -1; - } - break; - } - /* * For each function, create the appropriate xlat * node, and duplicate the child arguments. @@ -297,14 +266,6 @@ static int xlat_redundant_instantiate(xlat_inst_ctx_t const *xctx) case XLAT_INPUT_UNPROCESSED: break; - case XLAT_INPUT_MONO: - if (xlat_validate_function_mono(node) < 0) { - PERROR("Invalid arguments for redundant expansion function \"%s\"", - xrf->func->name); - goto error; - } - break; - case XLAT_INPUT_ARGS: if (xlat_validate_function_args(node) < 0) { PERROR("Invalid arguments for redundant expansion function \"%s\"", diff --git a/src/lib/unlang/xlat_tokenize.c b/src/lib/unlang/xlat_tokenize.c index e9a7e5e97b4..0013583dfca 100644 --- a/src/lib/unlang/xlat_tokenize.c +++ b/src/lib/unlang/xlat_tokenize.c @@ -165,25 +165,13 @@ static inline int xlat_tokenize_regex(xlat_exp_head_t *head, fr_sbuff_t *in) } #endif -int xlat_validate_function_mono(xlat_exp_t *node) -{ - fr_assert(node->type == XLAT_FUNC); - - if (node->call.func->args && node->call.func->args->required && !xlat_exp_head(node->call.args)) { - fr_strerror_const("Missing required input"); - return -1; - } - - return 0; -} - bool const xlat_func_chars[UINT8_MAX + 1] = { SBUFF_CHAR_CLASS_ALPHA_NUM, ['.'] = true, ['-'] = true, ['_'] = true, }; -static int xlat_validate_function_arg(xlat_arg_parser_t const *arg_p, xlat_exp_t *arg) +static fr_slen_t xlat_validate_function_arg(xlat_arg_parser_t const *arg_p, xlat_exp_t *arg) { ssize_t slen; xlat_exp_t *node; @@ -228,7 +216,7 @@ static int xlat_validate_function_arg(xlat_arg_parser_t const *arg_p, xlat_exp_t node->data.vb_strvalue, node->data.vb_length, NULL, /* no parse rules */ node->data.tainted); - if (slen <= 0) return -1; + if (slen <= 0) return slen; /* * Replace the string value with the parsed data type. @@ -239,7 +227,7 @@ static int xlat_validate_function_arg(xlat_arg_parser_t const *arg_p, xlat_exp_t return 0; } -int xlat_validate_function_args(xlat_exp_t *node) +fr_slen_t xlat_validate_function_args(xlat_exp_t *node) { xlat_arg_parser_t const *arg_p; xlat_exp_t *arg = xlat_exp_head(node->call.args); @@ -248,6 +236,8 @@ int xlat_validate_function_args(xlat_exp_t *node) fr_assert(node->type == XLAT_FUNC); for (arg_p = node->call.func->args, i = 0; arg_p->type != FR_TYPE_NULL; arg_p++) { + fr_slen_t slen; + if (!arg_p->required) break; if (!arg) { @@ -262,9 +252,10 @@ int xlat_validate_function_args(xlat_exp_t *node) */ fr_assert(arg->type == XLAT_GROUP); - if (xlat_validate_function_arg(arg_p, arg) < 0) { + slen = xlat_validate_function_arg(arg_p, arg); + if (slen < 0) { fr_strerror_printf("Failed parsing argument %d as type '%s'", i, fr_type_to_str(arg_p->type)); - return -1; + return slen; } arg = xlat_exp_next(node->call.args, arg); @@ -397,7 +388,7 @@ static int xlat_tokenize_function_args(xlat_exp_head_t *head, fr_sbuff_t *in, tm * function's arguments. */ if (xlat_tokenize_argv(node, &node->call.args, in, func, - &xlat_function_arg_rules, t_rules, true, (node->call.input_type == XLAT_INPUT_MONO)) < 0) { + &xlat_function_arg_rules, t_rules, true, false) < 0) { error: talloc_free(node); return -1; @@ -418,11 +409,6 @@ error: case XLAT_INPUT_UNPROCESSED: break; - case XLAT_INPUT_MONO: - if (xlat_validate_function_mono(node) < 0) goto error; - node->flags.can_purify = (node->call.func->flags.pure && node->call.args->flags.pure) | node->call.args->flags.can_purify; - break; - case XLAT_INPUT_ARGS: if (xlat_validate_function_args(node) < 0) goto error; node->flags.can_purify = (node->call.func->flags.pure && node->call.args->flags.pure) | node->call.args->flags.can_purify; @@ -1664,15 +1650,6 @@ int xlat_resolve(xlat_exp_head_t *head, xlat_res_rules_t const *xr_rules) case XLAT_INPUT_UNPROCESSED: break; - case XLAT_INPUT_MONO: - if (node->call.input_type != XLAT_INPUT_MONO) { - fr_strerror_const("Function should be called using %{func:arg} syntax"); - return -1; - - } - if (xlat_validate_function_mono(node) < 0) return -1; - break; - case XLAT_INPUT_ARGS: if (node->call.input_type != XLAT_INPUT_ARGS) { fr_strerror_const("Function takes defined arguments and should " diff --git a/src/tests/keywords/rand b/src/tests/keywords/rand index 058c8fd3380..146eac22e64 100644 --- a/src/tests/keywords/rand +++ b/src/tests/keywords/rand @@ -1,26 +1,7 @@ string result_string uint32 result_integer -&result_string := "%rand('-1')" - -# -# Negative limit should have failed assignment -# -if !(&result_string == "") { - test_fail -} - -&result_string := "%rand('hello world')" - -# -# Invalid limit should have failed assignment -# -if !(&result_string == "") { - test_fail -} - &result_integer := %rand('123') - if (!&result_integer) { test_fail } diff --git a/src/tests/unit/xlat/purify.txt b/src/tests/unit/xlat/purify.txt index 80ea0811c09..39219ef72ec 100644 --- a/src/tests/unit/xlat/purify.txt +++ b/src/tests/unit/xlat/purify.txt @@ -249,8 +249,8 @@ match 0x5e153571422b69cf5c5f7ce5f03985b5 # # This is a reference to the contents of &User-Name # -xlat_purify %md5(&User-Name) -match %md5(&User-Name) +xlat_purify %md5(%{User-Name}) +match %md5(%{User-Name}) xlat_purify %md5('foo') diff --git a/src/tests/xlat/expr.txt b/src/tests/xlat/expr.txt index b501e7d2cec..02a05352b7c 100644 --- a/src/tests/xlat/expr.txt +++ b/src/tests/xlat/expr.txt @@ -140,7 +140,7 @@ match 0x9f9d51bc70ef21ca5c14f307980a29d8 # And if we take it from the &User-Name ref, we get the same thing # as hashing the bare string. # -xlat_expr (octets) %md5(&User-Name) +xlat_expr (octets) %md5(%{User-Name}) match 0x9f9d51bc70ef21ca5c14f307980a29d8 xlat_expr &Service-Type