]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
auth: auth_cache_parse_key_and_fields() - Change to return error instead of i_fatal()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Thu, 6 Nov 2025 13:25:55 +0000 (15:25 +0200)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Thu, 13 Nov 2025 22:50:48 +0000 (22:50 +0000)
src/auth/auth-cache.c
src/auth/auth-cache.h
src/auth/db-lua.c
src/auth/passdb.c
src/auth/test-auth-cache.c
src/auth/userdb.c

index 2f875ac45eb18992f55070bca4346eb4d4d40e57..2fa8da0b250df4cd170fdfa6ad6276c315b6434a 100644 (file)
@@ -66,7 +66,7 @@ static void auth_cache_key_add_tab_idx(string_t *str, unsigned int i)
 
 static int auth_cache_parse_key_exclude(pool_t pool, const char *query,
                                        const char *exclude_driver,
-                                       char **cache_key_r,
+                                       const char **cache_key_r,
                                        const char **error_r)
 {
        string_t *str;
@@ -130,9 +130,11 @@ static int auth_cache_parse_key_exclude(pool_t pool, const char *query,
        return 0;
 }
 
-char *auth_cache_parse_key_and_fields(pool_t pool, const char *query,
-                                     const ARRAY_TYPE(const_string) *fields,
-                                     const char *exclude_driver)
+int auth_cache_parse_key_and_fields(pool_t pool, const char *query,
+                                   const ARRAY_TYPE(const_string) *fields,
+                                   const char *exclude_driver,
+                                   const char **cache_key_r,
+                                   const char **error_r)
 {
        if (fields != NULL && !array_is_empty(fields)) {
                unsigned int i, count;
@@ -146,12 +148,13 @@ char *auth_cache_parse_key_and_fields(pool_t pool, const char *query,
                query = str_c(full_query);
        }
 
-       char *cache_key;
        const char *error;
        if (auth_cache_parse_key_exclude(pool, query, exclude_driver,
-                                        &cache_key, &error) < 0)
-               i_fatal("auth-cache: %s", error);
-       return cache_key;
+                                        cache_key_r, &error) < 0) {
+               *error_r = t_strdup_printf("auth-cache: %s", error);
+               return -1;
+       }
+       return 0;
 }
 
 static void
index d63621b1a4cb0d44ca9df10149e55c41556ea826..ccee0aaed4b416184e46795165dc24b8f88d4e47 100644 (file)
@@ -19,9 +19,11 @@ struct auth_request;
 /* Parses all %variables from query and compresses them into tab-separated
    list, so it can be used as a cache key. Adds also variables from "fields",
    except variables prefixed with <exclude_driver>":" */
-char *auth_cache_parse_key_and_fields(pool_t pool, const char *query,
-                                     const ARRAY_TYPE(const_string) *fields,
-                                     const char *exclude_driver);
+int auth_cache_parse_key_and_fields(pool_t pool, const char *query,
+                                   const ARRAY_TYPE(const_string) *fields,
+                                   const char *exclude_driver,
+                                   const char **cache_key_r,
+                                   const char **error_r);
 
 /* Create a new cache. max_size specifies the maximum amount of memory in
    bytes to use for cache (it's not fully exact). ttl_secs specifies time to
index c3c2e6739621812b786a0a2762a24d5a633fc7ea..25389f783a366f04737b31af46838c0d4b060ee8 100644 (file)
@@ -474,17 +474,21 @@ auth_lua_script_get_default_cache_key(const struct auth_lua_script_parameters *p
                switch (params->stype) {
                case AUTH_LUA_SCRIPT_TYPE_PASSDB:
                        i_assert(params->passdb_module != NULL);
-                       params->passdb_module->module.default_cache_key =
-                               auth_cache_parse_key_and_fields(params->pool,
-                                               lua_tostring(script->L, -1),
-                                               &post_set->fields, "lua");
+                       if (auth_cache_parse_key_and_fields(params->pool,
+                                       lua_tostring(script->L, -1),
+                                       &post_set->fields, "lua",
+                                       &params->passdb_module->module.default_cache_key,
+                                       error_r) < 0)
+                               return -1;
                        break;
                case AUTH_LUA_SCRIPT_TYPE_USERDB:
                        i_assert(params->userdb_module != NULL);
-                       params->userdb_module->module.default_cache_key =
-                               auth_cache_parse_key_and_fields(params->pool,
-                                               lua_tostring(script->L, -1),
-                                               &post_set->fields, "lua");
+                       if (auth_cache_parse_key_and_fields(params->pool,
+                                       lua_tostring(script->L, -1),
+                                       &post_set->fields, "lua",
+                                       &params->userdb_module->module.default_cache_key,
+                                       error_r) < 0)
+                               return -1;
                        break;
                default:
                        i_unreached();
index 98feb5f1521ea2b4f26689c161625d796bde8d2e..f3bc36f679dab5ce888426fb9465b6cfeb322ae5 100644 (file)
@@ -169,15 +169,13 @@ int passdb_set_cache_key(struct passdb_module *module,
                         const struct passdb_parameters *passdb_params,
                         pool_t pool, const char *query,
                         const ARRAY_TYPE(const_string) *fields,
-                        const char *exclude_driver, const char **error_r ATTR_UNUSED)
+                        const char *exclude_driver, const char **error_r)
 {
        if (!passdb_params->use_cache)
                return 0;
 
-       module->default_cache_key =
-               auth_cache_parse_key_and_fields(pool, query, fields,
-                                               exclude_driver);
-       return 0;
+       return auth_cache_parse_key_and_fields(pool, query, fields,
+                       exclude_driver, &module->default_cache_key, error_r);
 }
 
 struct passdb_module *
index f58c21f7afb532dca491f3e65f70d03abbc18c21..1bdbef894f1fe5d1b387932fdd847710d0071e07 100644 (file)
@@ -87,26 +87,23 @@ static void test_auth_cache_parse_key(void)
                /* test that other providers are dropped */
                { "%{a}%{provider:user}", "%{a}" },
        };
-       const char *cache_key;
+       const char *cache_key, *error;
        unsigned int i;
 
        test_begin("auth cache parse key");
 
        for (i = 0; i < N_ELEMENTS(tests); i++) {
-               cache_key = auth_cache_parse_key_and_fields(pool_datastack_create(),
-                                                           tests[i].in, NULL, NULL);
+               test_assert_idx(auth_cache_parse_key_and_fields(
+                       pool_datastack_create(), tests[i].in, NULL, NULL,
+                       &cache_key, &error) == 0, i);
                test_assert_strcmp_idx(cache_key, tests[i].out, i);
        }
 
        test_end();
 }
 
-static enum fatal_test_state test_cache_key_missing_variable(unsigned int i)
+static void test_auth_cache_parse_key_errors(void)
 {
-       if (i == 0)
-               test_begin("auth cache missing variable");
-
-       /* ensure that we do not accept static string */
        static const struct {
                const char *in, *out;
        } tests_bad[] = {
@@ -116,16 +113,19 @@ static enum fatal_test_state test_cache_key_missing_variable(unsigned int i)
                            "failed: syntax error, unexpected end of file, " \
                            "expecting CCBRACE or PIPE" },
        };
+       const char *cache_key, *error;
+       unsigned int i;
+
+       test_begin("auth cache parse key errors");
 
-       if (i < N_ELEMENTS(tests_bad)) {
-               test_expect_fatal_string(tests_bad[i].out);
-               (void)auth_cache_parse_key_and_fields(pool_datastack_create(),
-                                                     tests_bad[i].in, NULL, NULL);
-               return FATAL_TEST_FAILURE;
+       for (i = 0; i < N_ELEMENTS(tests_bad); i++) {
+               test_assert_idx(auth_cache_parse_key_and_fields(
+                       pool_datastack_create(), tests_bad[i].in, NULL, NULL,
+                       &cache_key, &error) < 0, i);
+               test_assert_strcmp_idx(error, tests_bad[i].out, i);
        }
 
        test_end();
-       return FATAL_TEST_FINISHED;
 }
 
 int main(void)
@@ -134,15 +134,11 @@ int main(void)
        auth_event = event_create(NULL);
        static void (*const test_functions[])(void) = {
                test_auth_cache_parse_key,
+               test_auth_cache_parse_key_errors,
                NULL
        };
 
-       static test_fatal_func_t *const fatal_functions[] = {
-               test_cache_key_missing_variable,
-               NULL,
-       };
-
-       int ret = test_run_with_fatals(test_functions, fatal_functions);
+       int ret = test_run(test_functions);
 
        event_unref(&auth_event);
        return ret;
index 9fd4148c90983d7aded3abfc402fde7e2c6c1f77..93912c3ab1d18a88b0c29e5627ea4d95a29bdf20 100644 (file)
@@ -103,15 +103,13 @@ int userdb_set_cache_key(struct userdb_module *module,
                         const struct userdb_parameters *userdb_params,
                         pool_t pool, const char *query,
                         const ARRAY_TYPE(const_string) *fields,
-                        const char *exclude_driver, const char **error_r ATTR_UNUSED)
+                        const char *exclude_driver, const char **error_r)
 {
        if (!userdb_params->use_cache)
                return 0;
 
-       module->default_cache_key =
-               auth_cache_parse_key_and_fields(pool, query, fields,
-                                               exclude_driver);
-       return 0;
+       return auth_cache_parse_key_and_fields(pool, query, fields,
+                       exclude_driver, &module->default_cache_key, error_r);
 }
 
 struct userdb_module *