diff --git a/modoboa/core/password_hashers/__init__.py b/modoboa/core/password_hashers/__init__.py index 23982d4f3..0a0b3f778 100644 --- a/modoboa/core/password_hashers/__init__.py +++ b/modoboa/core/password_hashers/__init__.py @@ -2,6 +2,7 @@ Password hashers for Modoboa. """ +from django.conf import settings from django.utils.encoding import smart_text from modoboa.core.password_hashers.advanced import ( # NOQA:F401 @@ -32,17 +33,21 @@ def get_password_hasher(scheme): def get_dovecot_schemes(): - """Return schemes supported by dovecot""" - supported_schemes = None - try: - _, schemes = doveadm_cmd("pw -l") - except OSError: - pass - else: - supported_schemes = ["{{{}}}".format(smart_text(scheme)) - for scheme in schemes.split()] + """Return schemes supported by Dovecot. - if not supported_schemes: - supported_schemes = ['{MD5-CRYPT}', '{PLAIN}'] + It first try to get supported schemes fron the settings, then from the + doveadm output, and fallback to {MD5-CRYPT} and {PLAIN} if the command + is not found. - return supported_schemes + :return: A list of supported '{SCHEME}' + """ + schemes = getattr(settings, "DOVECOT_SUPPORTED_SCHEMES", None) + + if not schemes: + try: + _, schemes = doveadm_cmd("pw -l") + except OSError: + schemes = "MD5-CRYPT PLAIN" + + return ["{{{}}}".format(smart_text(scheme)) + for scheme in schemes.split()] diff --git a/modoboa/core/tests/test_authentication.py b/modoboa/core/tests/test_authentication.py index d08e1421b..f7aa8b3ab 100644 --- a/modoboa/core/tests/test_authentication.py +++ b/modoboa/core/tests/test_authentication.py @@ -173,7 +173,7 @@ class AuthenticationTestCase(ModoTestCase): @override_settings(DOVEADM_LOOKUP_PATH=[DOVEADM_TEST_PATH]) def test_dovecot_supported_schemes(self): - """Validate dovecot supported schemes with fake output.""" + """Validate Dovecot supported schemes with fake command output.""" supported_schemes = get_dovecot_schemes() self.assertEqual(supported_schemes, ["{MD5}", @@ -207,6 +207,12 @@ class AuthenticationTestCase(ModoTestCase): "{SHA256-CRYPT}", "{SHA512-CRYPT}"]) + @override_settings(DOVECOT_SUPPORTED_SCHEMES="SHA1 SHA512-CRYPT") + def test_dovecot_supported_schemes_from_settings(self): + """Validate dovecot supported schemes from the settings.""" + supported_schemes = get_dovecot_schemes() + self.assertEqual(supported_schemes, ["{SHA1}", "{SHA512-CRYPT}"]) + def test_dovecot_default_schemes(self): """Check default scheme if doveadm is not found.""" supported_schemes = get_dovecot_schemes()