From: Timo Sirainen Date: Thu, 6 Nov 2025 13:16:18 +0000 (+0200) Subject: auth: Add and use passdb_set_cache_key() to set cache key X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=594a726722ae0c45ab1fd567b1ff1c435b012c81;p=thirdparty%2Fdovecot%2Fcore.git auth: Add and use passdb_set_cache_key() to set cache key --- diff --git a/src/auth/passdb-bsdauth.c b/src/auth/passdb-bsdauth.c index c2372b6a56..c70a0590da 100644 --- a/src/auth/passdb-bsdauth.c +++ b/src/auth/passdb-bsdauth.c @@ -6,7 +6,6 @@ #ifdef PASSDB_BSDAUTH #include "safe-memset.h" -#include "auth-cache.h" #include "ipwd.h" #include "mycrypt.h" #include "settings.h" @@ -102,13 +101,13 @@ bsdauth_preinit(pool_t pool, struct event *event, SETTINGS_GET_FLAG_NO_EXPAND, &post_set, error_r) < 0) return -1; - module->default_cache_key = !passdb_params->use_cache ? NULL : - auth_cache_parse_key_and_fields(pool, AUTH_CACHE_KEY_USER, - &post_set->fields, "bsdauth"); + int ret = passdb_set_cache_key(module, passdb_params, pool, + AUTH_CACHE_KEY_USER, &post_set->fields, + "bsdauth", error_r); settings_free(post_set); *module_r = module; - return 0; + return ret; } static void bsdauth_deinit(struct passdb_module *module ATTR_UNUSED) diff --git a/src/auth/passdb-ldap.c b/src/auth/passdb-ldap.c index a661552e12..cda63231f2 100644 --- a/src/auth/passdb-ldap.c +++ b/src/auth/passdb-ldap.c @@ -9,7 +9,6 @@ #include "array.h" #include "str.h" #include "password-scheme.h" -#include "auth-cache.h" #include "settings.h" #include "auth-settings.h" #include "db-ldap.h" @@ -465,15 +464,14 @@ passdb_ldap_preinit(pool_t pool, struct event *event, ldap_pre->passdb_ldap_bind ? "password" : NULL); - module->module.default_cache_key = !passdb_params->use_cache ? NULL : - auth_cache_parse_key_and_fields(pool, - t_strconcat(ldap_pre->ldap_base, - ldap_pre->passdb_ldap_bind_userdn, - ldap_pre->passdb_ldap_filter, NULL), - &auth_post->fields, NULL); + const char *query = t_strconcat(ldap_pre->ldap_base, + ldap_pre->passdb_ldap_bind_userdn, + ldap_pre->passdb_ldap_filter, NULL); + ret = passdb_set_cache_key(&module->module, passdb_params, pool, + query, &auth_post->fields, + NULL, error_r); *module_r = &module->module; - ret = 0; failed: settings_free(auth_post); diff --git a/src/auth/passdb-pam.c b/src/auth/passdb-pam.c index 1dffd2abaf..a4fb7aebf1 100644 --- a/src/auth/passdb-pam.c +++ b/src/auth/passdb-pam.c @@ -16,7 +16,6 @@ #include "str.h" #include "net.h" #include "safe-memset.h" -#include "auth-cache.h" #include "settings.h" #include @@ -415,11 +414,10 @@ pam_preinit(pool_t pool, struct event *event, } module = p_new(pool, struct pam_passdb_module, 1); - module->module.default_cache_key = !passdb_params->use_cache ? NULL : - auth_cache_parse_key_and_fields(pool, - t_strdup_printf("%"AUTH_CACHE_KEY_USER"\t%s", - set->service_name), - &post_set->fields, "pam"); + const char *query = t_strdup_printf("%"AUTH_CACHE_KEY_USER"\t%s", + set->service_name); + int ret = passdb_set_cache_key(&module->module, passdb_params, pool, + query, &post_set->fields, "pam", error_r); module->requests_left = set->max_requests; module->pam_setcred = set->setcred; module->pam_session = set->session; @@ -429,7 +427,7 @@ pam_preinit(pool_t pool, struct event *event, settings_free(set); *module_r = &module->module; - return 0; + return ret; } struct passdb_module_interface passdb_pam = { diff --git a/src/auth/passdb-passwd.c b/src/auth/passdb-passwd.c index 4d08de753b..71f5800cf8 100644 --- a/src/auth/passdb-passwd.c +++ b/src/auth/passdb-passwd.c @@ -1,7 +1,6 @@ /* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */ #include "auth-common.h" -#include "auth-cache.h" #include "passdb.h" #include "settings.h" @@ -142,12 +141,13 @@ passwd_preinit(pool_t pool, struct event *event, SETTINGS_GET_FLAG_NO_EXPAND, &post_set, error_r) < 0) return -1; - module->default_cache_key = !passdb_params->use_cache ? NULL : - auth_cache_parse_key_and_fields(pool, AUTH_CACHE_KEY_USER, - &post_set->fields, "passwd"); + + int ret = passdb_set_cache_key(module, passdb_params, pool, + AUTH_CACHE_KEY_USER, + &post_set->fields, "passwd", error_r); settings_free(post_set); *module_r = module; - return 0; + return ret; } static void passwd_deinit(struct passdb_module *module ATTR_UNUSED) diff --git a/src/auth/passdb-sql.c b/src/auth/passdb-sql.c index 1b31c0bd40..c478860030 100644 --- a/src/auth/passdb-sql.c +++ b/src/auth/passdb-sql.c @@ -9,7 +9,6 @@ #include "settings.h" #include "settings-parser.h" #include "password-scheme.h" -#include "auth-cache.h" #include "db-sql.h" #include @@ -305,14 +304,14 @@ passdb_sql_preinit(pool_t pool, struct event *event, return -1; } - module->module.default_cache_key = !passdb_params->use_cache ? NULL : - auth_cache_parse_key_and_fields(pool, set->query, - &post_set->fields, "sql"); + int ret = passdb_set_cache_key(&module->module, passdb_params, pool, + set->query, &post_set->fields, "sql", + error_r); settings_free(set); settings_free(post_set); *module_r = &module->module; - return 0; + return ret; } static void passdb_sql_init(struct passdb_module *_module) diff --git a/src/auth/passdb.c b/src/auth/passdb.c index af7fd43034..98feb5f152 100644 --- a/src/auth/passdb.c +++ b/src/auth/passdb.c @@ -3,6 +3,7 @@ #include "auth-common.h" #include "array.h" #include "password-scheme.h" +#include "auth-cache.h" #include "auth-worker-connection.h" #include "passdb.h" @@ -164,6 +165,21 @@ void passdb_handle_credentials(enum passdb_result result, callback(result, credentials, size, auth_request); } +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) +{ + if (!passdb_params->use_cache) + return 0; + + module->default_cache_key = + auth_cache_parse_key_and_fields(pool, query, fields, + exclude_driver); + return 0; +} + struct passdb_module * passdb_preinit(pool_t pool, struct event *event, const struct auth_passdb_settings *set, bool use_cache) diff --git a/src/auth/passdb.h b/src/auth/passdb.h index ab37636089..1743a93027 100644 --- a/src/auth/passdb.h +++ b/src/auth/passdb.h @@ -104,6 +104,12 @@ void passdb_handle_credentials(enum passdb_result result, lookup_credentials_callback_t *callback, struct auth_request *auth_request); +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); + struct passdb_module * passdb_preinit(pool_t pool, struct event *event, const struct auth_passdb_settings *set, bool use_cache);