2008-12-30 16:25:42 +00:00
|
|
|
# This file is part of Radicale Server - Calendar Server
|
2009-07-27 15:04:54 +00:00
|
|
|
# Copyright © 2008 Nicolas Kandel
|
|
|
|
# Copyright © 2008 Pascal Halter
|
2017-05-27 17:28:07 +02:00
|
|
|
# Copyright © 2008-2017 Guillaume Ayoub
|
2019-06-17 04:13:24 +02:00
|
|
|
# Copyright © 2017-2018 Unrud <unrud@outlook.com>
|
2008-12-30 16:25:42 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2010-01-21 18:52:53 +01:00
|
|
|
import base64
|
2016-07-04 14:32:33 +02:00
|
|
|
import functools
|
2010-01-21 18:52:53 +01:00
|
|
|
import hashlib
|
2017-05-23 03:02:22 +02:00
|
|
|
import hmac
|
2014-05-14 01:42:19 +02:00
|
|
|
import os
|
2009-07-27 15:04:54 +00:00
|
|
|
|
2018-08-28 16:19:36 +02:00
|
|
|
from radicale import auth
|
2018-08-16 07:59:55 +02:00
|
|
|
|
2017-06-21 09:48:57 +02:00
|
|
|
|
2018-08-28 16:19:36 +02:00
|
|
|
class Auth(auth.BaseAuth):
|
2018-08-16 07:59:55 +02:00
|
|
|
def __init__(self, configuration):
|
|
|
|
super().__init__(configuration)
|
2016-04-22 11:37:02 +09:00
|
|
|
self.filename = os.path.expanduser(
|
|
|
|
configuration.get("auth", "htpasswd_filename"))
|
|
|
|
self.encryption = configuration.get("auth", "htpasswd_encryption")
|
|
|
|
|
|
|
|
if self.encryption == "ssha":
|
|
|
|
self.verify = self._ssha
|
|
|
|
elif self.encryption == "sha1":
|
|
|
|
self.verify = self._sha1
|
|
|
|
elif self.encryption == "plain":
|
|
|
|
self.verify = self._plain
|
|
|
|
elif self.encryption == "md5":
|
|
|
|
try:
|
2016-05-04 19:25:34 +02:00
|
|
|
from passlib.hash import apr_md5_crypt
|
2017-05-31 11:08:32 +02:00
|
|
|
except ImportError as e:
|
2016-04-22 11:37:02 +09:00
|
|
|
raise RuntimeError(
|
|
|
|
"The htpasswd encryption method 'md5' requires "
|
2017-05-31 11:08:32 +02:00
|
|
|
"the passlib module.") from e
|
2016-05-04 19:25:34 +02:00
|
|
|
self.verify = functools.partial(self._md5apr1, apr_md5_crypt)
|
2016-04-22 11:37:02 +09:00
|
|
|
elif self.encryption == "bcrypt":
|
|
|
|
try:
|
2016-05-04 19:25:34 +02:00
|
|
|
from passlib.hash import bcrypt
|
2017-05-31 11:08:32 +02:00
|
|
|
except ImportError as e:
|
2016-04-22 11:37:02 +09:00
|
|
|
raise RuntimeError(
|
|
|
|
"The htpasswd encryption method 'bcrypt' requires "
|
2017-05-31 11:08:32 +02:00
|
|
|
"the passlib module with bcrypt support.") from e
|
2016-04-22 11:37:02 +09:00
|
|
|
# A call to `encrypt` raises passlib.exc.MissingBackendError with a
|
|
|
|
# good error message if bcrypt backend is not available. Trigger
|
|
|
|
# this here.
|
2018-09-08 14:42:19 +02:00
|
|
|
bcrypt.hash("test-bcrypt-backend")
|
2016-05-04 19:25:34 +02:00
|
|
|
self.verify = functools.partial(self._bcrypt, bcrypt)
|
2016-04-22 11:37:02 +09:00
|
|
|
elif self.encryption == "crypt":
|
|
|
|
try:
|
|
|
|
import crypt
|
2017-05-31 11:08:32 +02:00
|
|
|
except ImportError as e:
|
2016-04-22 11:37:02 +09:00
|
|
|
raise RuntimeError(
|
|
|
|
"The htpasswd encryption method 'crypt' requires "
|
2017-05-31 11:08:32 +02:00
|
|
|
"the crypt() system support.") from e
|
2016-05-04 19:25:34 +02:00
|
|
|
self.verify = functools.partial(self._crypt, crypt)
|
2016-04-22 11:37:02 +09:00
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
2017-05-31 11:08:32 +02:00
|
|
|
"The htpasswd encryption method %r is not "
|
2016-04-22 11:37:02 +09:00
|
|
|
"supported." % self.encryption)
|
|
|
|
|
|
|
|
def _plain(self, hash_value, password):
|
2016-05-18 22:41:05 +02:00
|
|
|
"""Check if ``hash_value`` and ``password`` match, plain method."""
|
2017-05-23 03:02:22 +02:00
|
|
|
return hmac.compare_digest(hash_value, password)
|
2016-04-22 11:37:02 +09:00
|
|
|
|
2016-05-04 19:25:34 +02:00
|
|
|
def _crypt(self, crypt, hash_value, password):
|
2016-05-18 22:41:05 +02:00
|
|
|
"""Check if ``hash_value`` and ``password`` match, crypt method."""
|
2017-08-17 06:46:38 +02:00
|
|
|
hash_value = hash_value.strip()
|
2017-05-23 03:02:22 +02:00
|
|
|
return hmac.compare_digest(crypt.crypt(password, hash_value),
|
|
|
|
hash_value)
|
2016-04-22 11:37:02 +09:00
|
|
|
|
|
|
|
def _sha1(self, hash_value, password):
|
2016-05-18 22:41:05 +02:00
|
|
|
"""Check if ``hash_value`` and ``password`` match, sha1 method."""
|
2017-08-17 06:46:38 +02:00
|
|
|
hash_value = base64.b64decode(hash_value.strip().replace(
|
|
|
|
"{SHA}", "").encode("ascii"))
|
2016-04-22 11:37:02 +09:00
|
|
|
password = password.encode(self.configuration.get("encoding", "stock"))
|
2016-08-02 14:39:20 +02:00
|
|
|
sha1 = hashlib.sha1()
|
2016-04-22 11:37:02 +09:00
|
|
|
sha1.update(password)
|
2017-08-17 06:46:38 +02:00
|
|
|
return hmac.compare_digest(sha1.digest(), hash_value)
|
2016-04-22 11:37:02 +09:00
|
|
|
|
2016-05-18 22:41:05 +02:00
|
|
|
def _ssha(self, hash_value, password):
|
|
|
|
"""Check if ``hash_value`` and ``password`` match, salted sha1 method.
|
|
|
|
|
|
|
|
This method is not directly supported by htpasswd, but it can be
|
|
|
|
written with e.g. openssl, and nginx can parse it.
|
|
|
|
|
|
|
|
"""
|
2017-08-17 06:46:38 +02:00
|
|
|
hash_value = base64.b64decode(hash_value.strip().replace(
|
2017-05-23 03:01:56 +02:00
|
|
|
"{SSHA}", "").encode("ascii"))
|
2016-04-22 11:37:02 +09:00
|
|
|
password = password.encode(self.configuration.get("encoding", "stock"))
|
2016-05-18 22:41:05 +02:00
|
|
|
salt_value = hash_value[20:]
|
2017-05-23 03:01:56 +02:00
|
|
|
hash_value = hash_value[:20]
|
2016-08-02 14:39:20 +02:00
|
|
|
sha1 = hashlib.sha1()
|
2016-04-22 11:37:02 +09:00
|
|
|
sha1.update(password)
|
|
|
|
sha1.update(salt_value)
|
2017-05-23 03:02:22 +02:00
|
|
|
return hmac.compare_digest(sha1.digest(), hash_value)
|
2016-04-22 11:37:02 +09:00
|
|
|
|
2016-05-04 19:25:34 +02:00
|
|
|
def _bcrypt(self, bcrypt, hash_value, password):
|
2017-08-17 06:46:38 +02:00
|
|
|
hash_value = hash_value.strip()
|
2016-05-04 19:25:34 +02:00
|
|
|
return bcrypt.verify(password, hash_value)
|
2016-04-22 11:37:02 +09:00
|
|
|
|
2016-05-04 19:25:34 +02:00
|
|
|
def _md5apr1(self, md5_apr1, hash_value, password):
|
2017-08-17 06:46:38 +02:00
|
|
|
hash_value = hash_value.strip()
|
2016-05-04 19:25:34 +02:00
|
|
|
return md5_apr1.verify(password, hash_value)
|
2016-04-22 11:37:02 +09:00
|
|
|
|
2018-08-16 07:59:58 +02:00
|
|
|
def login(self, login, password):
|
2017-09-17 14:03:46 +02:00
|
|
|
"""Validate credentials.
|
|
|
|
|
2018-08-16 07:59:58 +02:00
|
|
|
Iterate through htpasswd credential file until login matches, extract
|
|
|
|
hash (encrypted password) and check hash against password,
|
2017-09-17 14:03:46 +02:00
|
|
|
using the method specified in the Radicale config.
|
|
|
|
|
|
|
|
The content of the file is not cached because reading is generally a
|
|
|
|
very cheap operation, and it's useful to get live updates of the
|
|
|
|
htpasswd file.
|
|
|
|
|
|
|
|
"""
|
2017-05-31 11:08:32 +02:00
|
|
|
try:
|
2017-08-17 06:46:38 +02:00
|
|
|
with open(self.filename) as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.rstrip("\n")
|
2017-08-17 06:46:40 +02:00
|
|
|
if line.lstrip() and not line.lstrip().startswith("#"):
|
2017-05-31 11:08:32 +02:00
|
|
|
try:
|
2018-08-16 07:59:58 +02:00
|
|
|
hash_login, hash_value = line.split(
|
|
|
|
":", maxsplit=1)
|
2017-05-31 11:08:32 +02:00
|
|
|
# Always compare both login and password to avoid
|
|
|
|
# timing attacks, see #591.
|
2018-08-16 07:59:58 +02:00
|
|
|
login_ok = hmac.compare_digest(hash_login, login)
|
2017-05-31 11:08:32 +02:00
|
|
|
password_ok = self.verify(hash_value, password)
|
2017-09-17 14:03:48 +02:00
|
|
|
if login_ok and password_ok:
|
2018-08-16 07:59:58 +02:00
|
|
|
return login
|
2017-05-31 11:08:32 +02:00
|
|
|
except ValueError as e:
|
|
|
|
raise RuntimeError("Invalid htpasswd file %r: %s" %
|
|
|
|
(self.filename, e)) from e
|
|
|
|
except OSError as e:
|
|
|
|
raise RuntimeError("Failed to load htpasswd file %r: %s" %
|
|
|
|
(self.filename, e)) from e
|
2018-08-16 07:59:58 +02:00
|
|
|
return ""
|