]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
v4: Convert %(debug: ) xlat to new api (#4005)
authorNick Porter <nick@portercomputing.co.uk>
Fri, 19 Mar 2021 10:51:06 +0000 (10:51 +0000)
committerGitHub <noreply@github.com>
Fri, 19 Mar 2021 10:51:06 +0000 (10:51 +0000)
* Convert %(debug: ) xlat to new xlat api

* Update tests to match %(debug: ) syntax

* Compile %() xlats as well as %{}

* Improve error message about %() xlat syntax

src/lib/unlang/compile.c
src/lib/unlang/xlat_builtin.c
src/lib/unlang/xlat_tokenize.c
src/tests/keywords/return-within-if-after-policy
src/tests/keywords/xlat-inline

index 5286da238362489800bbe01c3adccd8d418b6c03..ae3d5a7a90083ad52d6fe0c069630f1a19ce95ce 100644 (file)
@@ -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);
                }
index 6218fd092e3048f81785fe298e82e2071d27af6a..6ac7e40715e27141269dfde62ba5a5fb18f86226 100644 (file)
@@ -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);
index 839ccd2cbc1fa088e2cd8ad27b016bcc37e52a98..feceba2a24510b4ac0af6b4787f032b697fdb004 100644 (file)
@@ -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;
index cf266050cd9f7fe0e8de9233817fc2c8149762f6..38cc0707e0363f608fc7f158f622a856dcfacbb9 100644 (file)
@@ -7,7 +7,7 @@ if ('true' == 'true') {
        accept
        success
        if(&User-Name == 'bob') {
-               "%{debug:%{debug:0}}"   # Noop
+               "%(debug:%(debug:0))"   # Noop
                return
        }
        test_fail
index 1355ae2eca2ac168bd1750ca822a857db9e097f6..bbef2d6d4c1116e37564a44638efcfaf004c6bd8 100644 (file)
@@ -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
 }