htpasswd.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2008 Nicolas Kandel
  3. # Copyright © 2008 Pascal Halter
  4. # Copyright © 2008-2017 Guillaume Ayoub
  5. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  6. # Copyright © 2024 Peter Bieringer <pb@bieringer.de>
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. Authentication backend that checks credentials with a htpasswd file.
  22. Apache's htpasswd command (httpd.apache.org/docs/programs/htpasswd.html)
  23. manages a file for storing user credentials. It can encrypt passwords using
  24. different the methods BCRYPT/SHA256/SHA512 or MD5-APR1 (a version of MD5 modified for
  25. Apache). MD5-APR1 provides medium security as of 2015. Only BCRYPT/SHA256/SHA512 can be
  26. considered secure by current standards.
  27. MD5-APR1-encrypted credentials can be written by all versions of htpasswd (it
  28. is the default, in fact), whereas BCRYPT/SHA256/SHA512 requires htpasswd 2.4.x or newer.
  29. The `is_authenticated(user, password)` function provided by this module
  30. verifies the user-given credentials by parsing the htpasswd credential file
  31. pointed to by the ``htpasswd_filename`` configuration value while assuming
  32. the password encryption method specified via the ``htpasswd_encryption``
  33. configuration value.
  34. The following htpasswd password encrpytion methods are supported by Radicale
  35. out-of-the-box:
  36. - plain-text (created by htpasswd -p ...) -- INSECURE
  37. - MD5-APR1 (htpasswd -m ...) -- htpasswd's default method, INSECURE
  38. - SHA256 (htpasswd -2 ...)
  39. - SHA512 (htpasswd -5 ...)
  40. When bcrypt is installed:
  41. - BCRYPT (htpasswd -B ...) -- Requires htpasswd 2.4.x
  42. """
  43. import functools
  44. import hmac
  45. from typing import Any
  46. from passlib.hash import apr_md5_crypt, sha256_crypt, sha512_crypt
  47. from radicale import auth, config, logger
  48. class Auth(auth.BaseAuth):
  49. _filename: str
  50. _encoding: str
  51. def __init__(self, configuration: config.Configuration) -> None:
  52. super().__init__(configuration)
  53. self._filename = configuration.get("auth", "htpasswd_filename")
  54. self._encoding = configuration.get("encoding", "stock")
  55. encryption: str = configuration.get("auth", "htpasswd_encryption")
  56. logger.info("auth password encryption: %s", encryption)
  57. if encryption == "plain":
  58. self._verify = self._plain
  59. elif encryption == "md5":
  60. self._verify = self._md5apr1
  61. elif encryption == "sha256":
  62. self._verify = self._sha256
  63. elif encryption == "sha512":
  64. self._verify = self._sha512
  65. elif encryption == "bcrypt" or encryption == "autodetect":
  66. try:
  67. import bcrypt
  68. except ImportError as e:
  69. raise RuntimeError(
  70. "The htpasswd encryption method 'bcrypt' or 'auto' requires "
  71. "the bcrypt module.") from e
  72. if encryption == "bcrypt":
  73. self._verify = functools.partial(self._bcrypt, bcrypt)
  74. else:
  75. self._verify = self._autodetect
  76. self._verify_bcrypt = functools.partial(self._bcrypt, bcrypt)
  77. else:
  78. raise RuntimeError("The htpasswd encryption method %r is not "
  79. "supported." % encryption)
  80. def _plain(self, hash_value: str, password: str) -> bool:
  81. """Check if ``hash_value`` and ``password`` match, plain method."""
  82. return hmac.compare_digest(hash_value.encode(), password.encode())
  83. def _bcrypt(self, bcrypt: Any, hash_value: str, password: str) -> bool:
  84. return bcrypt.checkpw(password=password.encode('utf-8'), hashed_password=hash_value.encode())
  85. def _md5apr1(self, hash_value: str, password: str) -> bool:
  86. return apr_md5_crypt.verify(password, hash_value.strip())
  87. def _sha256(self, hash_value: str, password: str) -> bool:
  88. return sha256_crypt.verify(password, hash_value.strip())
  89. def _sha512(self, hash_value: str, password: str) -> bool:
  90. return sha512_crypt.verify(password, hash_value.strip())
  91. def _autodetect(self, hash_value: str, password: str) -> bool:
  92. if hash_value.startswith("$apr1$", 0, 6) and len(hash_value) == 37:
  93. # MD5-APR1
  94. return self._md5apr1(hash_value, password)
  95. elif hash_value.startswith("$2y$", 0, 4) and len(hash_value) == 60:
  96. # BCRYPT
  97. return self._verify_bcrypt(hash_value, password)
  98. elif hash_value.startswith("$5$", 0, 3) and len(hash_value) == 63:
  99. # SHA-256
  100. return self._sha256(hash_value, password)
  101. elif hash_value.startswith("$6$", 0, 3) and len(hash_value) == 106:
  102. # SHA-512
  103. return self._sha512(hash_value, password)
  104. else:
  105. # assumed plaintext
  106. return self._plain(hash_value, password)
  107. def login(self, login: str, password: str) -> str:
  108. """Validate credentials.
  109. Iterate through htpasswd credential file until login matches, extract
  110. hash (encrypted password) and check hash against password,
  111. using the method specified in the Radicale config.
  112. The content of the file is not cached because reading is generally a
  113. very cheap operation, and it's useful to get live updates of the
  114. htpasswd file.
  115. """
  116. try:
  117. with open(self._filename, encoding=self._encoding) as f:
  118. for line in f:
  119. line = line.rstrip("\n")
  120. if line.lstrip() and not line.lstrip().startswith("#"):
  121. try:
  122. hash_login, hash_value = line.split(
  123. ":", maxsplit=1)
  124. # Always compare both login and password to avoid
  125. # timing attacks, see #591.
  126. login_ok = hmac.compare_digest(
  127. hash_login.encode(), login.encode())
  128. password_ok = self._verify(hash_value, password)
  129. if login_ok and password_ok:
  130. return login
  131. except ValueError as e:
  132. raise RuntimeError("Invalid htpasswd file %r: %s" %
  133. (self._filename, e)) from e
  134. except OSError as e:
  135. raise RuntimeError("Failed to load htpasswd file %r: %s" %
  136. (self._filename, e)) from e
  137. return ""