2021-08-22 19:02:38 +00:00
|
|
|
import base64, os, os.path, hmac, json, secrets
|
2021-08-22 20:07:16 +00:00
|
|
|
from datetime import timedelta
|
2014-06-21 23:42:48 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
from expiringdict import ExpiringDict
|
2014-06-21 23:42:48 +00:00
|
|
|
|
2020-09-26 13:58:25 +00:00
|
|
|
import utils
|
|
|
|
from mailconfig import get_mail_password, get_mail_user_privileges
|
2020-09-30 10:34:26 +00:00
|
|
|
from mfa import get_hash_mfa_state, validate_auth_mfa
|
2014-08-17 22:43:57 +00:00
|
|
|
|
2014-06-21 23:42:48 +00:00
|
|
|
DEFAULT_KEY_PATH = '/var/lib/mailinabox/api.key'
|
|
|
|
DEFAULT_AUTH_REALM = 'Mail-in-a-Box Management Server'
|
|
|
|
|
2021-08-22 19:02:38 +00:00
|
|
|
class AuthService:
|
2014-06-22 12:55:19 +00:00
|
|
|
def __init__(self):
|
2014-06-21 23:42:48 +00:00
|
|
|
self.auth_realm = DEFAULT_AUTH_REALM
|
2014-06-22 12:45:29 +00:00
|
|
|
self.key_path = DEFAULT_KEY_PATH
|
2021-08-22 20:07:16 +00:00
|
|
|
self.max_session_duration = timedelta(days=2)
|
|
|
|
|
2021-08-22 19:02:38 +00:00
|
|
|
self.init_system_api_key()
|
2021-08-22 20:07:16 +00:00
|
|
|
self.sessions = ExpiringDict(max_len=64, max_age_seconds=self.max_session_duration.total_seconds())
|
2014-06-21 23:42:48 +00:00
|
|
|
|
2021-08-22 19:02:38 +00:00
|
|
|
def init_system_api_key(self):
|
|
|
|
"""Write an API key to a local file so local processes can use the API"""
|
2014-06-21 23:42:48 +00:00
|
|
|
|
|
|
|
def create_file_with_mode(path, mode):
|
|
|
|
# Based on answer by A-B-B: http://stackoverflow.com/a/15015748
|
|
|
|
old_umask = os.umask(0)
|
|
|
|
try:
|
|
|
|
return os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT, mode), 'w')
|
|
|
|
finally:
|
|
|
|
os.umask(old_umask)
|
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
self.key = secrets.token_hex(32)
|
2021-08-22 19:02:38 +00:00
|
|
|
|
2014-06-21 23:42:48 +00:00
|
|
|
os.makedirs(os.path.dirname(self.key_path), exist_ok=True)
|
|
|
|
|
|
|
|
with create_file_with_mode(self.key_path, 0o640) as key_file:
|
|
|
|
key_file.write(self.key + '\n')
|
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
def authenticate(self, request, env, login_only=False, logout=False):
|
|
|
|
"""Test if the HTTP Authorization header's username matches the system key, a session key,
|
|
|
|
or if the username/password passed in the header matches a local user.
|
2014-12-01 19:20:46 +00:00
|
|
|
Returns a tuple of the user's email address and list of user privileges (e.g.
|
|
|
|
('my@email', []) or ('my@email', ['admin']); raises a ValueError on login failure.
|
2021-08-22 20:07:16 +00:00
|
|
|
If the user used the system API key, the user's email is returned as None since
|
|
|
|
this key is not associated with a user."""
|
2014-06-21 23:42:48 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
def parse_http_authorization_basic(header):
|
|
|
|
def decode(s):
|
|
|
|
return base64.b64decode(s.encode('ascii')).decode('ascii')
|
2014-08-08 19:36:00 +00:00
|
|
|
if " " not in header:
|
2014-08-17 22:43:57 +00:00
|
|
|
return None, None
|
2014-06-21 23:42:48 +00:00
|
|
|
scheme, credentials = header.split(maxsplit=1)
|
|
|
|
if scheme != 'Basic':
|
2014-08-17 22:43:57 +00:00
|
|
|
return None, None
|
2014-08-08 19:36:00 +00:00
|
|
|
credentials = decode(credentials)
|
|
|
|
if ":" not in credentials:
|
2014-08-17 22:43:57 +00:00
|
|
|
return None, None
|
2014-08-08 19:36:00 +00:00
|
|
|
username, password = credentials.split(':', maxsplit=1)
|
2014-08-17 22:43:57 +00:00
|
|
|
return username, password
|
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
username, password = parse_http_authorization_basic(request.headers.get('Authorization', ''))
|
2014-08-17 22:43:57 +00:00
|
|
|
if username in (None, ""):
|
2014-11-30 15:43:07 +00:00
|
|
|
raise ValueError("Authorization header invalid.")
|
2021-08-22 19:02:38 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
if username.strip() == "" and password.strip() == "":
|
|
|
|
raise ValueError("No email address, password, session key, or API key provided.")
|
|
|
|
|
|
|
|
# If user passed the system API key, grant administrative privs. This key
|
|
|
|
# is not associated with a user.
|
|
|
|
if username == self.key and not login_only:
|
2014-12-01 19:20:46 +00:00
|
|
|
return (None, ["admin"])
|
2020-09-02 15:23:32 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
# If the password corresponds with a session token for the user, grant access for that user.
|
|
|
|
if password in self.sessions and self.sessions[password]["email"] == username and not login_only:
|
|
|
|
sessionid = password
|
|
|
|
session = self.sessions[sessionid]
|
|
|
|
if session["password_token"] != self.create_user_password_state_token(username, env):
|
|
|
|
# This session is invalid because the user's password/MFA state changed
|
|
|
|
# after the session was created.
|
|
|
|
del self.sessions[sessionid]
|
|
|
|
raise ValueError("Session expired.")
|
|
|
|
if logout:
|
|
|
|
# Clear the session.
|
|
|
|
del self.sessions[sessionid]
|
|
|
|
else:
|
|
|
|
# Re-up the session so that it does not expire.
|
|
|
|
self.sessions[sessionid] = session
|
|
|
|
|
|
|
|
# If no password was given, but a username was given, we're missing some information.
|
|
|
|
elif password.strip() == "":
|
|
|
|
raise ValueError("Enter a password.")
|
2014-12-01 19:20:46 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
else:
|
|
|
|
# The user is trying to log in with a username and a password
|
|
|
|
# (and possibly a MFA token). On failure, an exception is raised.
|
|
|
|
self.check_user_auth(username, password, request, env)
|
2020-09-26 13:58:25 +00:00
|
|
|
|
2015-06-06 12:33:31 +00:00
|
|
|
# Get privileges for authorization. This call should never fail because by this
|
2021-08-22 20:07:16 +00:00
|
|
|
# point we know the email address is a valid user --- unless the user has been
|
|
|
|
# deleted after the session was granted. On error the call will return a tuple
|
|
|
|
# of an error message and an HTTP status code.
|
|
|
|
privs = get_mail_user_privileges(username, env)
|
2015-04-28 11:05:49 +00:00
|
|
|
if isinstance(privs, tuple): raise ValueError(privs[0])
|
2014-08-17 22:43:57 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
# Return the authorization information.
|
|
|
|
return (username, privs)
|
2014-06-21 23:42:48 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
def check_user_auth(self, email, pw, request, env):
|
|
|
|
# Validate a user's login email address and password. If MFA is enabled,
|
|
|
|
# check the MFA token in the X-Auth-Token header.
|
2020-09-26 13:58:25 +00:00
|
|
|
#
|
2021-08-22 20:07:16 +00:00
|
|
|
# On login failure, raises a ValueError with a login error message. On
|
|
|
|
# success, nothing is returned.
|
|
|
|
|
|
|
|
# Authenticate.
|
|
|
|
try:
|
|
|
|
# Get the hashed password of the user. Raise a ValueError if the
|
|
|
|
# email address does not correspond to a user. But wrap it in the
|
|
|
|
# same exception as if a password fails so we don't easily reveal
|
|
|
|
# if an email address is valid.
|
|
|
|
pw_hash = get_mail_password(email, env)
|
2020-09-26 13:58:25 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
# Use 'doveadm pw' to check credentials. doveadm will return
|
|
|
|
# a non-zero exit status if the credentials are no good,
|
|
|
|
# and check_call will raise an exception in that case.
|
|
|
|
utils.shell('check_call', [
|
|
|
|
"/usr/bin/doveadm", "pw",
|
|
|
|
"-p", pw,
|
|
|
|
"-t", pw_hash,
|
|
|
|
])
|
|
|
|
except:
|
|
|
|
# Login failed.
|
|
|
|
raise ValueError("Incorrect email address or password.")
|
|
|
|
|
|
|
|
# If MFA is enabled, check that MFA passes.
|
|
|
|
status, hints = validate_auth_mfa(email, request, env)
|
|
|
|
if not status:
|
|
|
|
# Login valid. Hints may have more info.
|
|
|
|
raise ValueError(",".join(hints))
|
|
|
|
|
|
|
|
def create_user_password_state_token(self, email, env):
|
|
|
|
# Create a token that changes if the user's password or MFA options change
|
|
|
|
# so that sessions become invalid if any of that information changes.
|
|
|
|
msg = get_mail_password(email, env).encode("utf8")
|
2020-09-12 14:34:06 +00:00
|
|
|
|
2020-09-26 13:58:25 +00:00
|
|
|
# Add to the message the current MFA state, which is a list of MFA information.
|
|
|
|
# Turn it into a string stably.
|
2020-09-30 10:34:26 +00:00
|
|
|
msg += b" " + json.dumps(get_hash_mfa_state(email, env), sort_keys=True).encode("utf8")
|
2020-09-12 14:34:06 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
# Make a HMAC using the system API key as a hash key.
|
2020-09-26 13:58:25 +00:00
|
|
|
hash_key = self.key.encode('ascii')
|
2020-09-12 14:34:06 +00:00
|
|
|
return hmac.new(hash_key, msg, digestmod="sha256").hexdigest()
|
2014-12-01 19:20:46 +00:00
|
|
|
|
2021-08-22 20:07:16 +00:00
|
|
|
def create_session_key(self, username, env, type=None):
|
|
|
|
# Create a new session.
|
|
|
|
token = secrets.token_hex(32)
|
|
|
|
self.sessions[token] = {
|
|
|
|
"email": username,
|
|
|
|
"password_token": self.create_user_password_state_token(username, env),
|
|
|
|
}
|
|
|
|
return token
|