From: Stephan Bosch Date: Sat, 4 Mar 2023 16:46:21 +0000 (+0100) Subject: auth: mech-otp - Merge mech-otp-common.c into mech-otp.c X-Git-Tag: 2.4.2~328 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f4a3896bbfd27f8bdfe66716c348af8e8ae1b808;p=thirdparty%2Fdovecot%2Fcore.git auth: mech-otp - Merge mech-otp-common.c into mech-otp.c Separate file is not useful and structure is confusing. --- diff --git a/src/auth/Makefile.am b/src/auth/Makefile.am index f0fdd91373..dc06918f22 100644 --- a/src/auth/Makefile.am +++ b/src/auth/Makefile.am @@ -84,7 +84,6 @@ auth_common_sources = \ auth-client-connection.c \ auth-master-connection.c \ auth-policy.c \ - mech-otp-common.c \ mech-plain-common.c \ auth-penalty.c \ auth-request.c \ @@ -139,7 +138,7 @@ headers = \ auth-client-connection.h \ auth-common.h \ auth-master-connection.h \ - mech-otp-common.h \ + mech-otp.h \ mech-plain-common.h \ mech-digest-md5-private.h \ mech-scram.h \ diff --git a/src/auth/main.c b/src/auth/main.c index 99e78d8710..6151eb521a 100644 --- a/src/auth/main.c +++ b/src/auth/main.c @@ -21,7 +21,7 @@ #include "passdb-cache.h" #include "mech.h" #include "otp.h" -#include "mech-otp-common.h" +#include "mech-otp.h" #include "auth.h" #include "auth-penalty.h" #include "auth-token.h" diff --git a/src/auth/mech-otp-common.c b/src/auth/mech-otp-common.c deleted file mode 100644 index 753fcbbbed..0000000000 --- a/src/auth/mech-otp-common.c +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Common code for OTP authentication mechanisms. - * - * Copyright (c) 2006 Andrey Panin - * - * This software is released under the MIT license. - */ - -#include "auth-common.h" -#include "hash.h" -#include "mech.h" - -#include "otp.h" -#include "mech-otp-common.h" - -static HASH_TABLE(char *, struct auth_request *) otp_lock_table; - -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); -} - -bool otp_try_lock(struct auth_request *auth_request) -{ - if (hash_table_lookup(otp_lock_table, auth_request->fields.user) != NULL) - return FALSE; - - hash_table_insert(otp_lock_table, auth_request->fields.user, auth_request); - return TRUE; -} - -void otp_unlock(struct auth_request *auth_request) -{ - struct otp_auth_request *request = - (struct otp_auth_request *)auth_request; - - if (!request->lock) - return; - - hash_table_remove(otp_lock_table, auth_request->fields.user); - request->lock = FALSE; -} - -void otp_set_credentials_callback(bool success, - struct auth_request *auth_request) -{ - if (success) - auth_request_success(auth_request, "", 0); - else { - auth_request_internal_failure(auth_request); - otp_unlock(auth_request); - } - - otp_unlock(auth_request); -} - -void mech_otp_auth_free(struct auth_request *auth_request) -{ - otp_unlock(auth_request); - - pool_unref(&auth_request->pool); -} - -void mech_otp_deinit(void) -{ - hash_table_destroy(&otp_lock_table); -} diff --git a/src/auth/mech-otp-common.h b/src/auth/mech-otp-common.h deleted file mode 100644 index 37a6551a15..0000000000 --- a/src/auth/mech-otp-common.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef MECH_OTP_COMMON_H -#define MECH_OTP_COMMON_H - -struct otp_auth_request { - struct auth_request auth_request; - - pool_t pool; - - bool lock; - - struct otp_state state; -}; - -void otp_lock_init(void); -bool otp_try_lock(struct auth_request *auth_request); -void otp_unlock(struct auth_request *auth_request); - -void otp_set_credentials_callback(bool success, - struct auth_request *auth_request); -void mech_otp_auth_free(struct auth_request *auth_request); -void mech_otp_deinit(void); - -#endif diff --git a/src/auth/mech-otp.c b/src/auth/mech-otp.c index 165db609b1..99bcd0cb20 100644 --- a/src/auth/mech-otp.c +++ b/src/auth/mech-otp.c @@ -13,7 +13,65 @@ #include "passdb.h" #include "hex-binary.h" #include "otp.h" -#include "mech-otp-common.h" + +#include "mech-otp.h" + +struct otp_auth_request { + struct auth_request auth_request; + + pool_t pool; + + bool lock; + + struct otp_state state; +}; + +static HASH_TABLE(char *, struct auth_request *) otp_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 auth_request *auth_request) +{ + if (hash_table_lookup(otp_lock_table, + auth_request->fields.user) != NULL) + return FALSE; + + hash_table_insert(otp_lock_table, auth_request->fields.user, + auth_request); + return TRUE; +} + +static void otp_unlock(struct auth_request *auth_request) +{ + struct otp_auth_request *request = + (struct otp_auth_request *)auth_request; + + if (!request->lock) + return; + + hash_table_remove(otp_lock_table, auth_request->fields.user); + request->lock = FALSE; +} + +/* + * Authentication + */ static void otp_send_challenge(struct auth_request *auth_request, @@ -110,6 +168,19 @@ mech_otp_auth_phase1(struct auth_request *auth_request, otp_credentials_callback); } +static void +otp_set_credentials_callback(bool success, struct auth_request *auth_request) +{ + if (success) + auth_request_success(auth_request, "", 0); + else { + auth_request_internal_failure(auth_request); + otp_unlock(auth_request); + } + + otp_unlock(auth_request); +} + static void mech_otp_verify(struct auth_request *auth_request, const char *data, bool hex) { @@ -227,6 +298,17 @@ static struct auth_request *mech_otp_auth_new(void) return &request->auth_request; } +static void mech_otp_auth_free(struct auth_request *auth_request) +{ + otp_unlock(auth_request); + + pool_unref(&auth_request->pool); +} + +/* + * Mechanism + */ + const struct mech_module mech_otp = { "OTP", @@ -238,3 +320,8 @@ const struct mech_module mech_otp = { mech_otp_auth_continue, mech_otp_auth_free }; + +void mech_otp_deinit(void) +{ + otp_lock_deinit(); +} diff --git a/src/auth/mech-otp.h b/src/auth/mech-otp.h new file mode 100644 index 0000000000..fa534d4c69 --- /dev/null +++ b/src/auth/mech-otp.h @@ -0,0 +1,6 @@ +#ifndef MECH_OTP_COMMON_H +#define MECH_OTP_COMMON_H + +void mech_otp_deinit(void); + +#endif diff --git a/src/auth/test-auth.c b/src/auth/test-auth.c index 91bd422875..be8b42ebc6 100644 --- a/src/auth/test-auth.c +++ b/src/auth/test-auth.c @@ -8,7 +8,7 @@ #include "auth-penalty.h" #include "mech.h" #include "otp.h" -#include "mech-otp-common.h" +#include "mech-otp.h" #include "db-oauth2.h" #include "passdb.h" #include "userdb.h"