From: Stephan Bosch Date: Sat, 28 Oct 2023 03:01:45 +0000 (+0200) Subject: auth: sasl-server-mech-otp - Move global state to global mechanism state X-Git-Tag: 2.4.2~178 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc5251e85c4ae8e53d9043068020cccef037b281;p=thirdparty%2Fdovecot%2Fcore.git auth: sasl-server-mech-otp - Move global state to global mechanism state --- diff --git a/src/auth/Makefile.am b/src/auth/Makefile.am index b45df4c3b6..2dba3a4dce 100644 --- a/src/auth/Makefile.am +++ b/src/auth/Makefile.am @@ -182,7 +182,6 @@ headers = \ db-sql.h \ db-passwd-file.h \ db-oauth2.h \ - mech-otp.h \ sasl-server.h \ sasl-server-gssapi.h \ sasl-server-oauth2.h \ diff --git a/src/auth/main.c b/src/auth/main.c index de3633b13f..51ebb53c86 100644 --- a/src/auth/main.c +++ b/src/auth/main.c @@ -20,8 +20,6 @@ #include "password-scheme.h" #include "passdb-cache.h" #include "sasl-server.h" -#include "otp.h" -#include "mech-otp.h" #include "auth.h" #include "auth-sasl.h" #include "auth-penalty.h" @@ -275,8 +273,6 @@ static void main_deinit(void) /* there are no more auth requests */ auths_free(); - mech_otp_deinit(); - /* allow modules to unregister their dbs/drivers/etc. before freeing the whole data structures containing them. */ module_dir_unload(&modules); diff --git a/src/auth/mech-otp.h b/src/auth/mech-otp.h deleted file mode 100644 index fa534d4c69..0000000000 --- a/src/auth/mech-otp.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef MECH_OTP_COMMON_H -#define MECH_OTP_COMMON_H - -void mech_otp_deinit(void); - -#endif diff --git a/src/auth/sasl-server-mech-otp.c b/src/auth/sasl-server-mech-otp.c index 98790a787c..20c0b4bbaf 100644 --- a/src/auth/sasl-server-mech-otp.c +++ b/src/auth/sasl-server-mech-otp.c @@ -13,7 +13,6 @@ #include "otp.h" #include "sasl-server-protected.h" -#include "mech-otp.h" struct otp_auth_request { struct sasl_server_mech_request auth_request; @@ -23,35 +22,29 @@ struct otp_auth_request { struct otp_state state; }; -static HASH_TABLE(const char *, struct sasl_server_mech_request *) -otp_lock_table; +struct otp_auth_mech_data { + struct sasl_server_mech_data data; + + HASH_TABLE(const char *, struct otp_auth_request *) lock_table; +}; /* * Locking */ -static void otp_lock_init(void) -{ - if (hash_table_is_created(otp_lock_table)) - return; - - hash_table_create(&otp_lock_table, default_pool, 128, - strcase_hash, strcasecmp); -} - -static void otp_lock_deinit(void) -{ - hash_table_destroy(&otp_lock_table); -} - static bool otp_try_lock(struct otp_auth_request *request) { struct sasl_server_mech_request *auth_request = &request->auth_request; + struct otp_auth_mech_data *otp_mdata = + container_of(auth_request->mech->data, + struct otp_auth_mech_data, data); - if (hash_table_lookup(otp_lock_table, auth_request->authid) != NULL) + i_assert(auth_request->authid != NULL); + if (hash_table_lookup(otp_mdata->lock_table, + auth_request->authid) != NULL) return FALSE; - hash_table_insert(otp_lock_table, auth_request->authid, auth_request); + hash_table_insert(otp_mdata->lock_table, auth_request->authid, request); request->lock = TRUE; return TRUE; } @@ -59,11 +52,15 @@ static bool otp_try_lock(struct otp_auth_request *request) static void otp_unlock(struct otp_auth_request *request) { struct sasl_server_mech_request *auth_request = &request->auth_request; + struct otp_auth_mech_data *otp_mdata = + container_of(auth_request->mech->data, + struct otp_auth_mech_data, data); if (!request->lock) return; - hash_table_remove(otp_lock_table, auth_request->authid); + i_assert(auth_request->authid != NULL); + hash_table_remove(otp_mdata->lock_table, auth_request->authid); request->lock = FALSE; } @@ -176,10 +173,8 @@ otp_set_credentials_callback(struct sasl_server_mech_request *auth_request, if (result->status == SASL_PASSDB_RESULT_OK) sasl_server_request_success(auth_request, "", 0); - else { + else sasl_server_request_internal_failure(auth_request); - otp_unlock(request); - } otp_unlock(request); } @@ -291,8 +286,6 @@ mech_otp_auth_new(const struct sasl_server_mech *mech ATTR_UNUSED, pool_t pool) { struct otp_auth_request *request; - otp_lock_init(); - request = p_new(pool, struct otp_auth_request, 1); request->lock = FALSE; @@ -312,11 +305,33 @@ static void mech_otp_auth_free(struct sasl_server_mech_request *auth_request) * Mechanism */ +static struct sasl_server_mech_data *mech_otp_data_new(pool_t pool) +{ + struct otp_auth_mech_data *otp_mdata; + + otp_mdata = p_new(pool, struct otp_auth_mech_data, 1); + hash_table_create(&otp_mdata->lock_table, default_pool, 128, + strcase_hash, strcasecmp); + + return &otp_mdata->data; +} + +static void mech_otp_data_free(struct sasl_server_mech_data *mdata) +{ + struct otp_auth_mech_data *otp_mdata = + container_of(mdata, struct otp_auth_mech_data, data); + + hash_table_destroy(&otp_mdata->lock_table); +} + static const struct sasl_server_mech_funcs mech_otp_funcs = { .auth_new = mech_otp_auth_new, .auth_initial = sasl_server_mech_generic_auth_initial, .auth_continue = mech_otp_auth_continue, .auth_free = mech_otp_auth_free, + + .data_new = mech_otp_data_new, + .data_free = mech_otp_data_free, }; static const struct sasl_server_mech_def mech_otp = { @@ -329,11 +344,6 @@ static const struct sasl_server_mech_def mech_otp = { .funcs = &mech_otp_funcs, }; -void mech_otp_deinit(void) -{ - otp_lock_deinit(); -} - void sasl_server_mech_register_otp(struct sasl_server_instance *sinst) { sasl_server_mech_register(sinst, &mech_otp); diff --git a/src/auth/test-auth.c b/src/auth/test-auth.c index fc13d178fb..6035b095e5 100644 --- a/src/auth/test-auth.c +++ b/src/auth/test-auth.c @@ -7,8 +7,6 @@ #include "auth-token.h" #include "auth-penalty.h" #include "sasl-server.h" -#include "otp.h" -#include "mech-otp.h" #include "db-oauth2.h" #include "passdb.h" #include "userdb.h" @@ -73,7 +71,6 @@ void test_auth_init(void) void test_auth_deinit(void) { auth_penalty_deinit(&auth_penalty); - mech_otp_deinit(); db_oauth2_deinit(); auths_deinit(); auth_token_deinit();