From: Nick Porter Date: Fri, 19 Mar 2021 10:51:06 +0000 (+0000) Subject: v4: Convert %(debug: ) xlat to new api (#4005) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=70f8caa3dd1b92302057a3dc94beb9eeaf7fef74;p=thirdparty%2Ffreeradius-server.git v4: Convert %(debug: ) xlat to new api (#4005) * Convert %(debug: ) xlat to new xlat api * Update tests to match %(debug: ) syntax * Compile %() xlats as well as %{} * Improve error message about %() xlat syntax --- diff --git a/src/lib/unlang/compile.c b/src/lib/unlang/compile.c index 5286da23836..ae3d5a7a900 100644 --- a/src/lib/unlang/compile.c +++ b/src/lib/unlang/compile.c @@ -3547,7 +3547,7 @@ static unlang_t *compile_item(unlang_t *parent, unlang_compile_t *unlang_ctx, CO * * This should really be removed from the server. */ - if (((name[0] == '%') && (name[1] == '{')) || + if (((name[0] == '%') && ((name[1] == '{') || (name[1] == '('))) || (cf_pair_attr_quote(cp) == T_BACK_QUOTED_STRING)) { return compile_tmpl(parent, unlang_ctx, cp); } diff --git a/src/lib/unlang/xlat_builtin.c b/src/lib/unlang/xlat_builtin.c index 6218fd092e3..6ac7e40715e 100644 --- a/src/lib/unlang/xlat_builtin.c +++ b/src/lib/unlang/xlat_builtin.c @@ -841,33 +841,42 @@ int xlat_register_legacy_redundant(CONF_SECTION *cs) */ +static xlat_arg_parser_t const xlat_func_debug_args[] = { + { .single = true, .type = FR_TYPE_INT8 }, + XLAT_ARG_PARSER_TERMINATOR +}; + /** Dynamically change the debugging level for the current request * * Example: @verbatim -"%{debug:3}" +"%(debug:3)" @endverbatim * * @ingroup xlat_functions */ -static ssize_t xlat_func_debug(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen, - UNUSED void const *mod_inst, UNUSED void const *xlat_inst, - request_t *request, char const *fmt) +static xlat_action_t xlat_func_debug(TALLOC_CTX *ctx, fr_dcursor_t *out, + request_t *request, UNUSED void const *xlat_inst, + UNUSED void *xlat_thread_inst, fr_value_box_list_t *in) { int level = 0; + fr_value_box_t *vb; + fr_value_box_t *in_head = fr_dlist_head(in); /* * Expand to previous (or current) level */ - snprintf(*out, outlen, "%d", request->log.lvl); + MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_INT8, NULL, false)); + vb->vb_int8 = request->log.lvl; + fr_dcursor_append(out, vb); /* * Assume we just want to get the current value and NOT set it to 0 */ - if (!*fmt) + if (!in_head) goto done; - level = atoi(fmt); + level = in_head->vb_int8; if (level == 0) { request->log.lvl = RAD_REQUEST_LVL_NONE; } else { @@ -876,7 +885,7 @@ static ssize_t xlat_func_debug(UNUSED TALLOC_CTX *ctx, char **out, size_t outlen } done: - return strlen(*out); + return XLAT_ACTION_DONE; } @@ -3197,8 +3206,6 @@ int xlat_init(void) #define XLAT_REGISTER(_x) xlat = xlat_register_legacy(NULL, STRINGIFY(_x), xlat_func_ ## _x, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN); \ xlat_internal(xlat); - xlat = xlat_register_legacy(NULL, "debug", xlat_func_debug, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN); - xlat_internal(xlat); XLAT_REGISTER(debug_attr); xlat_register_legacy(NULL, "explode", xlat_func_explode, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN); XLAT_REGISTER(integer); @@ -3213,6 +3220,7 @@ do { \ xlat_func_args(xlat, _args); \ } while (0) + XLAT_REGISTER_ARGS("debug", xlat_func_debug, xlat_func_debug_args); XLAT_REGISTER_ARGS("hmacmd5", xlat_func_hmac_md5, xlat_hmac_args); XLAT_REGISTER_ARGS("hmacsha1", xlat_func_hmac_sha1, xlat_hmac_args); XLAT_REGISTER_ARGS("concat", xlat_func_concat, xlat_func_concat_args); diff --git a/src/lib/unlang/xlat_tokenize.c b/src/lib/unlang/xlat_tokenize.c index 839ccd2cbc1..feceba2a245 100644 --- a/src/lib/unlang/xlat_tokenize.c +++ b/src/lib/unlang/xlat_tokenize.c @@ -396,7 +396,7 @@ static inline int xlat_tokenize_function_mono(TALLOC_CTX *ctx, xlat_exp_t **head node->flags.needs_resolving = true; /* Needs resolution during pass2 */ } else { if (func->input_type == XLAT_INPUT_ARGS) { - fr_strerror_const("Function takes multiple arguments and should " + fr_strerror_const("Function takes defined arguments and should " "be called using %(func:args) syntax"); error: head = NULL; diff --git a/src/tests/keywords/return-within-if-after-policy b/src/tests/keywords/return-within-if-after-policy index cf266050cd9..38cc0707e03 100644 --- a/src/tests/keywords/return-within-if-after-policy +++ b/src/tests/keywords/return-within-if-after-policy @@ -7,7 +7,7 @@ if ('true' == 'true') { accept success if(&User-Name == 'bob') { - "%{debug:%{debug:0}}" # Noop + "%(debug:%(debug:0))" # Noop return } test_fail diff --git a/src/tests/keywords/xlat-inline b/src/tests/keywords/xlat-inline index 1355ae2eca2..bbef2d6d4c1 100644 --- a/src/tests/keywords/xlat-inline +++ b/src/tests/keywords/xlat-inline @@ -4,19 +4,19 @@ # Set debug to something high, recording the old level update request { - &Tmp-Integer-0 := "%{debug:4}" + &Tmp-Integer-0 := "%(debug:4)" } # Check debug level is now 4 -if ("%{debug:4}" != 4) { +if ("%(debug:4)" != 4) { fail } # Set debug back to the original level -"%{debug:%{Tmp-Integer-0}}" +"%(debug:%{Tmp-Integer-0})" # Read out the current debug level to verify it changed -if ("%{debug:%{Tmp-Integer-0}}" != "%{Tmp-Integer-0}") { +if ("%(debug:%{Tmp-Integer-0})" != "%{Tmp-Integer-0}") { fail }