]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
auth: sasl-server-mech-otp - Move global state to global mechanism state
authorStephan Bosch <stephan.bosch@open-xchange.com>
Sat, 28 Oct 2023 03:01:45 +0000 (05:01 +0200)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Thu, 9 Oct 2025 08:41:22 +0000 (08:41 +0000)
src/auth/Makefile.am
src/auth/main.c
src/auth/mech-otp.h [deleted file]
src/auth/sasl-server-mech-otp.c
src/auth/test-auth.c

index b45df4c3b632be1c2a74c38d2c3e30e421f4992d..2dba3a4dce96fc1f712e31aac4dce7f1ec0807b3 100644 (file)
@@ -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 \
index de3633b13f3917648f6a3f9fcb214d6b20f05f75..51ebb53c86b1d0a7da0bdccdb0edf9bb76ffa127 100644 (file)
@@ -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 (file)
index fa534d4..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef MECH_OTP_COMMON_H
-#define MECH_OTP_COMMON_H
-
-void mech_otp_deinit(void);
-
-#endif
index 98790a787cf86188017dbf2ca962c1c951540eae..20c0b4bbaf8f2ef118b7f0687ae12f11adaa17d8 100644 (file)
@@ -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);
index fc13d178fb006304a4d722cd20b8439b5887f7bf..6035b095e55ba6c6b0682b182ba67fd67661d8b7 100644 (file)
@@ -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();