2018-10-08 01:12:32 +02:00
|
|
|
"""
|
|
|
|
Doorlockd -- Binary Kitchen's smart door opener
|
|
|
|
|
|
|
|
Copyright (c) Binary Kitchen e.V., 2018
|
|
|
|
|
|
|
|
Author:
|
|
|
|
Ralf Ramsauer <ralf@binary-kitchen.de>
|
|
|
|
|
|
|
|
This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
the LICENSE file in the top-level directory.
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import ldap
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from enum import Enum
|
|
|
|
|
2018-10-08 22:59:34 +02:00
|
|
|
from .Doorlock import DoorlockResponse
|
2018-10-08 01:12:32 +02:00
|
|
|
|
2018-10-08 22:59:34 +02:00
|
|
|
log = logging.getLogger()
|
2018-10-08 01:12:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AuthMethod(Enum):
|
|
|
|
LDAP_USER_PW = 1
|
|
|
|
LOCAL_USER_DB = 2
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self == AuthMethod.LDAP_USER_PW:
|
|
|
|
return 'LDAP'
|
|
|
|
elif self == AuthMethod.LOCAL_USER_DB:
|
|
|
|
return 'Local'
|
|
|
|
return 'Error'
|
|
|
|
|
|
|
|
|
|
|
|
class Authenticator:
|
2018-10-08 19:47:26 +02:00
|
|
|
def __init__(self, cfg):
|
|
|
|
self._simulate = cfg.boolean('SIMULATE_AUTH')
|
2018-10-08 01:12:32 +02:00
|
|
|
self._backends = set()
|
|
|
|
|
2018-10-08 19:47:26 +02:00
|
|
|
if self._simulate:
|
|
|
|
return
|
2018-10-08 01:12:32 +02:00
|
|
|
|
2018-10-08 19:47:26 +02:00
|
|
|
self._ldap_uri = cfg.str('LDAP_URI')
|
|
|
|
self._ldap_binddn = cfg.str('LDAP_BINDDN')
|
|
|
|
if self._ldap_uri and self._ldap_binddn:
|
|
|
|
log.info('Initialising LDAP auth backend')
|
|
|
|
self._backends.add(AuthMethod.LDAP_USER_PW)
|
|
|
|
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
|
|
|
|
ldap.set_option(ldap.OPT_REFERRALS, 0)
|
2018-10-08 01:12:32 +02:00
|
|
|
|
2018-10-08 19:47:26 +02:00
|
|
|
file_local_db = cfg.str('LOCAL_USER_DB')
|
|
|
|
if file_local_db:
|
|
|
|
log.info('Initialising local auth backend')
|
|
|
|
self._local_db = dict()
|
2018-10-08 01:12:32 +02:00
|
|
|
|
2018-10-08 19:47:26 +02:00
|
|
|
with open(file_local_db, 'r') as f:
|
|
|
|
for line in f:
|
|
|
|
line = line.split()
|
|
|
|
user = line[0]
|
|
|
|
pwd = line[1].split(':')
|
|
|
|
self._local_db[user] = pwd
|
2018-10-08 01:12:32 +02:00
|
|
|
|
2018-10-08 19:47:26 +02:00
|
|
|
self._backends.add(AuthMethod.LOCAL_USER_DB)
|
2018-10-08 01:12:32 +02:00
|
|
|
|
2018-10-08 19:47:26 +02:00
|
|
|
@property
|
|
|
|
def backends(self):
|
|
|
|
return self._backends
|
2018-10-08 01:12:32 +02:00
|
|
|
|
|
|
|
def _try_auth_local(self, user, password):
|
2021-10-18 18:17:16 +02:00
|
|
|
log.info(' Trying to local auth (user, password) as user %s', user)
|
2018-10-08 01:12:32 +02:00
|
|
|
if user not in self._local_db:
|
2019-03-06 22:28:25 +01:00
|
|
|
log.info(' No user %s in local database', user)
|
2018-10-08 22:59:34 +02:00
|
|
|
return DoorlockResponse.Perm
|
2018-10-08 01:12:32 +02:00
|
|
|
|
|
|
|
stored_pw = self._local_db[user][0]
|
|
|
|
stored_salt = self._local_db[user][1]
|
|
|
|
if stored_pw == hashlib.sha256(stored_salt.encode() + password.encode()).hexdigest():
|
2019-03-06 22:28:25 +01:00
|
|
|
log.info(' Authenticated as user %s', user)
|
2018-10-08 22:59:34 +02:00
|
|
|
return DoorlockResponse.Success
|
2018-10-08 01:12:32 +02:00
|
|
|
|
2019-03-06 22:28:25 +01:00
|
|
|
log.info(' Invalid credentials')
|
2018-10-08 22:59:34 +02:00
|
|
|
return DoorlockResponse.Perm
|
2018-10-08 01:12:32 +02:00
|
|
|
|
|
|
|
def _try_auth_ldap(self, user, password):
|
|
|
|
log.info(' Trying to LDAP auth (user, password) as user %s', user)
|
|
|
|
ldap_username = self._ldap_binddn % user
|
|
|
|
try:
|
|
|
|
l = ldap.initialize(self._ldap_uri)
|
|
|
|
l.simple_bind_s(ldap_username, password)
|
|
|
|
l.unbind_s()
|
|
|
|
except ldap.INVALID_CREDENTIALS:
|
|
|
|
log.info(' Invalid credentials')
|
2018-10-08 22:59:34 +02:00
|
|
|
return DoorlockResponse.Perm
|
2018-10-08 01:12:32 +02:00
|
|
|
except ldap.LDAPError as e:
|
|
|
|
log.info(' LDAP Error: %s' % e)
|
2018-10-08 22:59:34 +02:00
|
|
|
return DoorlockResponse.InternalError
|
2019-03-06 22:28:25 +01:00
|
|
|
log.info(' Authenticated as user %s', user)
|
2018-10-08 22:59:34 +02:00
|
|
|
return DoorlockResponse.Success
|
2018-10-08 01:12:32 +02:00
|
|
|
|
|
|
|
def try_auth(self, credentials):
|
|
|
|
if self._simulate:
|
|
|
|
log.info('SIMULATION MODE! ACCEPTING ANYTHING!')
|
2018-10-08 22:59:34 +02:00
|
|
|
return DoorlockResponse.Success
|
2018-11-27 08:40:23 +01:00
|
|
|
if AuthMethod.LDAP_USER_PW in self._backends:
|
|
|
|
retval = self._try_auth_ldap(credentials[0], credentials[1])
|
|
|
|
if retval == DoorlockResponse.Success:
|
|
|
|
return retval
|
|
|
|
if AuthMethod.LOCAL_USER_DB in self._backends:
|
|
|
|
return self._try_auth_local(credentials[0], credentials[1])
|
|
|
|
return DoorlockResponse.Perm
|