htpasswd.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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-2025 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 encryption 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. When argon2 is installed:
  43. - ARGON2 (python -c 'from passlib.hash import argon2; print(argon2.using(type="ID").hash("password"))'
  44. """
  45. import functools
  46. import hmac
  47. import os
  48. import re
  49. import threading
  50. import time
  51. from typing import Any, Tuple
  52. from passlib.hash import apr_md5_crypt, sha256_crypt, sha512_crypt
  53. from radicale import auth, config, logger
  54. class Auth(auth.BaseAuth):
  55. _filename: str
  56. _encoding: str
  57. _htpasswd: dict # login -> digest
  58. _htpasswd_mtime_ns: int
  59. _htpasswd_size: int
  60. _htpasswd_ok: bool
  61. _htpasswd_not_ok_time: float
  62. _htpasswd_not_ok_reminder_seconds: int
  63. _htpasswd_bcrypt_use: int
  64. _htpasswd_cache: bool
  65. _has_bcrypt: bool
  66. _encryption: str
  67. _lock: threading.Lock
  68. def __init__(self, configuration: config.Configuration) -> None:
  69. super().__init__(configuration)
  70. self._filename = configuration.get("auth", "htpasswd_filename")
  71. logger.info("auth htpasswd file: %r", self._filename)
  72. self._encoding = configuration.get("encoding", "stock")
  73. logger.info("auth htpasswd file encoding: %r", self._encoding)
  74. self._htpasswd_cache = configuration.get("auth", "htpasswd_cache")
  75. logger.info("auth htpasswd cache: %s", self._htpasswd_cache)
  76. self._encryption: str = configuration.get("auth", "htpasswd_encryption")
  77. logger.info("auth htpasswd encryption is 'radicale.auth.htpasswd_encryption.%s'", self._encryption)
  78. self._has_bcrypt = False
  79. self._htpasswd_ok = False
  80. self._htpasswd_not_ok_reminder_seconds = 60 # currently hardcoded
  81. (self._htpasswd_ok, self._htpasswd_bcrypt_use, self._htpasswd, self._htpasswd_size, self._htpasswd_mtime_ns) = self._read_htpasswd(True, False)
  82. self._lock = threading.Lock()
  83. if self._encryption == "plain":
  84. self._verify = self._plain
  85. elif self._encryption == "md5":
  86. self._verify = self._md5apr1
  87. elif self._encryption == "sha256":
  88. self._verify = self._sha256
  89. elif self._encryption == "sha512":
  90. self._verify = self._sha512
  91. elif self._encryption == "bcrypt" or self._encryption == "autodetect":
  92. try:
  93. import bcrypt
  94. except ImportError as e:
  95. if (self._encryption == "autodetect") and (self._htpasswd_bcrypt_use == 0):
  96. logger.warning("auth htpasswd encryption is 'radicale.auth.htpasswd_encryption.%s' which can require bycrypt module, but currently no entries found", self._encryption)
  97. else:
  98. raise RuntimeError(
  99. "The htpasswd encryption method 'bcrypt' or 'autodetect' requires "
  100. "the bcrypt module (entries found: %d)." % self._htpasswd_bcrypt_use) from e
  101. else:
  102. self._has_bcrypt = True
  103. if self._encryption == "autodetect":
  104. if self._htpasswd_bcrypt_use == 0:
  105. logger.info("auth htpasswd encryption is 'radicale.auth.htpasswd_encryption.%s' and bycrypt module found, but currently not required", self._encryption)
  106. else:
  107. logger.info("auth htpasswd encryption is 'radicale.auth.htpasswd_encryption.%s' and bycrypt module found (bcrypt entries found: %d)", self._encryption, self._htpasswd_bcrypt_use)
  108. if self._encryption == "bcrypt":
  109. self._verify = functools.partial(self._bcrypt, bcrypt)
  110. else:
  111. self._verify = self._autodetect
  112. if self._htpasswd_bcrypt_use:
  113. self._verify_bcrypt = functools.partial(self._bcrypt, bcrypt)
  114. else:
  115. raise RuntimeError("The htpasswd encryption method %r is not "
  116. "supported." % self._encryption)
  117. def _plain(self, hash_value: str, password: str) -> tuple[str, bool]:
  118. """Check if ``hash_value`` and ``password`` match, plain method."""
  119. return ("PLAIN", hmac.compare_digest(hash_value.encode(), password.encode()))
  120. def _plain_fallback(self, method_orig, hash_value: str, password: str) -> tuple[str, bool]:
  121. """Check if ``hash_value`` and ``password`` match, plain method / fallback in case of hash length is not matching on autodetection."""
  122. info = "PLAIN/fallback as hash length not matching for " + method_orig + ": " + str(len(hash_value))
  123. return (info, hmac.compare_digest(hash_value.encode(), password.encode()))
  124. def _bcrypt(self, bcrypt: Any, hash_value: str, password: str) -> tuple[str, bool]:
  125. if self._encryption == "autodetect" and len(hash_value) != 60:
  126. return self._plain_fallback("BCRYPT", hash_value, password)
  127. else:
  128. return ("BCRYPT", bcrypt.checkpw(password=password.encode('utf-8'), hashed_password=hash_value.encode()))
  129. def _md5apr1(self, hash_value: str, password: str) -> tuple[str, bool]:
  130. if self._encryption == "autodetect" and len(hash_value) != 37:
  131. return self._plain_fallback("MD5-APR1", hash_value, password)
  132. else:
  133. return ("MD5-APR1", apr_md5_crypt.verify(password, hash_value.strip()))
  134. def _sha256(self, hash_value: str, password: str) -> tuple[str, bool]:
  135. if self._encryption == "autodetect" and len(hash_value) != 63:
  136. return self._plain_fallback("SHA-256", hash_value, password)
  137. else:
  138. return ("SHA-256", sha256_crypt.verify(password, hash_value.strip()))
  139. def _sha512(self, hash_value: str, password: str) -> tuple[str, bool]:
  140. if self._encryption == "autodetect" and len(hash_value) != 106:
  141. return self._plain_fallback("SHA-512", hash_value, password)
  142. else:
  143. return ("SHA-512", sha512_crypt.verify(password, hash_value.strip()))
  144. def _autodetect(self, hash_value: str, password: str) -> tuple[str, bool]:
  145. if hash_value.startswith("$apr1$", 0, 6):
  146. # MD5-APR1
  147. return self._md5apr1(hash_value, password)
  148. elif re.match(r"^\$2(a|b|x|y)?\$", hash_value):
  149. # BCRYPT
  150. return self._verify_bcrypt(hash_value, password)
  151. elif hash_value.startswith("$5$", 0, 3):
  152. # SHA-256
  153. return self._sha256(hash_value, password)
  154. elif hash_value.startswith("$6$", 0, 3):
  155. # SHA-512
  156. return self._sha512(hash_value, password)
  157. else:
  158. return self._plain(hash_value, password)
  159. def _read_htpasswd(self, init: bool, suppress: bool) -> Tuple[bool, int, dict, int, int]:
  160. """Read htpasswd file
  161. init == True: stop on error
  162. init == False: warn/skip on error and set mark to log reminder every interval
  163. suppress == True: suppress warnings, change info to debug (used in non-caching mode)
  164. suppress == False: do not suppress warnings (used in caching mode)
  165. """
  166. htpasswd_ok = True
  167. bcrypt_use = 0
  168. if (init is True) or (suppress is True):
  169. info = "Read"
  170. else:
  171. info = "Re-read"
  172. if suppress is False:
  173. logger.info("%s content of htpasswd file start: %r", info, self._filename)
  174. else:
  175. logger.debug("%s content of htpasswd file start: %r", info, self._filename)
  176. htpasswd: dict[str, str] = dict()
  177. entries = 0
  178. duplicates = 0
  179. errors = 0
  180. try:
  181. with open(self._filename, encoding=self._encoding) as f:
  182. line_num = 0
  183. for line in f:
  184. line_num += 1
  185. line = line.rstrip("\n")
  186. if line.lstrip() and not line.lstrip().startswith("#"):
  187. try:
  188. login, digest = line.split(":", maxsplit=1)
  189. skip = False
  190. if login == "" or digest == "":
  191. if init is True:
  192. raise ValueError("htpasswd file contains problematic line not matching <login>:<digest> in line: %d" % line_num)
  193. else:
  194. errors += 1
  195. logger.warning("htpasswd file contains problematic line not matching <login>:<digest> in line: %d (ignored)", line_num)
  196. htpasswd_ok = False
  197. skip = True
  198. else:
  199. if htpasswd.get(login):
  200. duplicates += 1
  201. if init is True:
  202. raise ValueError("htpasswd file contains duplicate login: '%s'", login, line_num)
  203. else:
  204. logger.warning("htpasswd file contains duplicate login: '%s' (line: %d / ignored)", login, line_num)
  205. htpasswd_ok = False
  206. skip = True
  207. else:
  208. if re.match(r"^\$2(a|b|x|y)?\$", digest) and len(digest) == 60:
  209. if init is True:
  210. bcrypt_use += 1
  211. else:
  212. if self._has_bcrypt is False:
  213. logger.warning("htpasswd file contains bcrypt digest login: '%s' (line: %d / ignored because module is not loaded)", login, line_num)
  214. skip = True
  215. htpasswd_ok = False
  216. if skip is False:
  217. htpasswd[login] = digest
  218. entries += 1
  219. except ValueError as e:
  220. if init is True:
  221. raise RuntimeError("Invalid htpasswd file %r: %s" % (self._filename, e)) from e
  222. except OSError as e:
  223. if init is True:
  224. raise RuntimeError("Failed to load htpasswd file %r: %s" % (self._filename, e)) from e
  225. else:
  226. logger.warning("Failed to load htpasswd file on re-read: %r" % self._filename)
  227. htpasswd_ok = False
  228. htpasswd_size = os.stat(self._filename).st_size
  229. htpasswd_mtime_ns = os.stat(self._filename).st_mtime_ns
  230. if suppress is False:
  231. logger.info("%s content of htpasswd file done: %r (entries: %d, duplicates: %d, errors: %d)", info, self._filename, entries, duplicates, errors)
  232. else:
  233. logger.debug("%s content of htpasswd file done: %r (entries: %d, duplicates: %d, errors: %d)", info, self._filename, entries, duplicates, errors)
  234. if htpasswd_ok is True:
  235. self._htpasswd_not_ok_time = 0
  236. else:
  237. self._htpasswd_not_ok_time = time.time()
  238. return (htpasswd_ok, bcrypt_use, htpasswd, htpasswd_size, htpasswd_mtime_ns)
  239. def _login(self, login: str, password: str) -> str:
  240. """Validate credentials.
  241. Iterate through htpasswd credential file until login matches, extract
  242. hash (encrypted password) and check hash against password,
  243. using the method specified in the Radicale config.
  244. Optional: the content of the file is cached and live updates will be detected by
  245. comparing mtime_ns and size
  246. """
  247. login_ok = False
  248. digest: str
  249. if self._htpasswd_cache is True:
  250. # check and re-read file if required
  251. with self._lock:
  252. htpasswd_size = os.stat(self._filename).st_size
  253. htpasswd_mtime_ns = os.stat(self._filename).st_mtime_ns
  254. if (htpasswd_size != self._htpasswd_size) or (htpasswd_mtime_ns != self._htpasswd_mtime_ns):
  255. (self._htpasswd_ok, self._htpasswd_bcrypt_use, self._htpasswd, self._htpasswd_size, self._htpasswd_mtime_ns) = self._read_htpasswd(False, False)
  256. self._htpasswd_not_ok_time = 0
  257. # log reminder of problemantic file every interval
  258. current_time = time.time()
  259. if (self._htpasswd_ok is False):
  260. if (self._htpasswd_not_ok_time > 0):
  261. if (current_time - self._htpasswd_not_ok_time) > self._htpasswd_not_ok_reminder_seconds:
  262. logger.warning("htpasswd file still contains issues (REMINDER, check warnings in the past): %r" % self._filename)
  263. self._htpasswd_not_ok_time = current_time
  264. else:
  265. self._htpasswd_not_ok_time = current_time
  266. if self._htpasswd.get(login):
  267. digest = self._htpasswd[login]
  268. login_ok = True
  269. else:
  270. # read file on every request
  271. (htpasswd_ok, htpasswd_bcrypt_use, htpasswd, htpasswd_size, htpasswd_mtime_ns) = self._read_htpasswd(False, True)
  272. if htpasswd.get(login):
  273. digest = htpasswd[login]
  274. login_ok = True
  275. if login_ok is True:
  276. try:
  277. (method, password_ok) = self._verify(digest, password)
  278. except ValueError as e:
  279. logger.error("Login verification failed for user: '%s' (htpasswd/%s) with errror '%s'", login, self._encryption, e)
  280. return ""
  281. if password_ok:
  282. logger.debug("Login verification successful for user: '%s' (htpasswd/%s/%s)", login, self._encryption, method)
  283. return login
  284. else:
  285. logger.warning("Login verification failed for user: '%s' (htpasswd/%s/%s)", login, self._encryption, method)
  286. else:
  287. logger.warning("Login verification user not found (htpasswd): '%s'", login)
  288. return ""