From 7cc19ca5abaa44b9c68435690e9e035688ff7247 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Thu, 6 Nov 2025 15:25:55 +0200 Subject: [PATCH] auth: auth_cache_parse_key_and_fields() - Change to return error instead of i_fatal() --- src/auth/auth-cache.c | 19 +++++++++++-------- src/auth/auth-cache.h | 8 +++++--- src/auth/db-lua.c | 20 ++++++++++++-------- src/auth/passdb.c | 8 +++----- src/auth/test-auth-cache.c | 36 ++++++++++++++++-------------------- src/auth/userdb.c | 8 +++----- 6 files changed, 50 insertions(+), 49 deletions(-) diff --git a/src/auth/auth-cache.c b/src/auth/auth-cache.c index 2f875ac45e..2fa8da0b25 100644 --- a/src/auth/auth-cache.c +++ b/src/auth/auth-cache.c @@ -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 diff --git a/src/auth/auth-cache.h b/src/auth/auth-cache.h index d63621b1a4..ccee0aaed4 100644 --- a/src/auth/auth-cache.h +++ b/src/auth/auth-cache.h @@ -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 ":" */ -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 diff --git a/src/auth/db-lua.c b/src/auth/db-lua.c index c3c2e67396..25389f783a 100644 --- a/src/auth/db-lua.c +++ b/src/auth/db-lua.c @@ -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", + ¶ms->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", + ¶ms->userdb_module->module.default_cache_key, + error_r) < 0) + return -1; break; default: i_unreached(); diff --git a/src/auth/passdb.c b/src/auth/passdb.c index 98feb5f152..f3bc36f679 100644 --- a/src/auth/passdb.c +++ b/src/auth/passdb.c @@ -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 * diff --git a/src/auth/test-auth-cache.c b/src/auth/test-auth-cache.c index f58c21f7af..1bdbef894f 100644 --- a/src/auth/test-auth-cache.c +++ b/src/auth/test-auth-cache.c @@ -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; diff --git a/src/auth/userdb.c b/src/auth/userdb.c index 9fd4148c90..93912c3ab1 100644 --- a/src/auth/userdb.c +++ b/src/auth/userdb.c @@ -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 * -- 2.47.3