]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Remove the last of the XLAT_MONO code
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 24 May 2024 16:13:22 +0000 (12:13 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 24 May 2024 16:13:31 +0000 (12:13 -0400)
Looks like this enables some purification that wasn't happening previously.  The parts of tests removed were either useless, or the tests were incorrect.

src/lib/unlang/xlat.h
src/lib/unlang/xlat_builtin.c
src/lib/unlang/xlat_eval.c
src/lib/unlang/xlat_func.c
src/lib/unlang/xlat_func.h
src/lib/unlang/xlat_redundant.c
src/lib/unlang/xlat_tokenize.c
src/tests/keywords/rand
src/tests/unit/xlat/purify.txt
src/tests/xlat/expr.txt

index 3e0012cdb01cb4df7a366b910de6d04cb278ca36..797c44634fa5ce86fe7e7e7d678d4528cccf203f 100644 (file)
@@ -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);
 
index 5c0f46d547730dade5eb38e37820fe4b64185b43..b77483f0644a2e04bda2532264149b927bdbb45c 100644 (file)
@@ -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();
 }
 
index 91ac371a2266df042c545e2df2bd04011b3e0afe..88b803e1af4ad0eb0a4d50ddf1d2b0c797e6ea4a 100644 (file)
@@ -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) {
index 91a1f4a396b60a1063f8947fdcfc9b1f3598c384..5d254acfef8e0cb1080d2a13ec3edc37aa70d93c 100644 (file)
@@ -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.
index f71de8995b4ccfc167b94b95d752ca8c3f4a56c9..e5b8a26b12fffe822cc34c9c0ac91b073a09f3f5 100644 (file)
@@ -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);
index efc200b7a8b88541760d2370d0ec7d3a3b790ca1..cfb3455edc63ece60d5dd351dca5eab750c0e051 100644 (file)
@@ -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\"",
index e9a7e5e97b438c809bc8c40985f876fb7c2277e2..0013583dfcaae696820f5084d73fc281f38c55e9 100644 (file)
@@ -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 "
index 058c8fd3380ab086a7644cbcbaf16ac1dd59d2c3..146eac22e64b09c611ec827dc12ae9574add3e7d 100644 (file)
@@ -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
 }
index 80ea0811c09a8587cc423467b2a054b65cf58da1..39219ef72eccdcda253322e90f2eb97e86a70952 100644 (file)
@@ -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')
index b501e7d2cec7797c3f84c128a2f7b70a9c372c2b..02a05352b7c5fef3bb6da43a2dad518a369d4664 100644 (file)
@@ -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