2020-01-21 05:54:35 +00:00
|
|
|
from geruecht import ldap
|
2020-01-26 22:31:22 +00:00
|
|
|
from ldap3 import SUBTREE, MODIFY_REPLACE, HASHED_SALTED_MD5
|
|
|
|
from ldap3.utils.hashed import hashed
|
2020-03-04 20:11:41 +00:00
|
|
|
from geruecht.model import MONEY, USER, GASTRO, BAR, VORSTAND, EXTERN
|
2019-12-28 20:52:49 +00:00
|
|
|
from geruecht.exceptions import PermissionDenied
|
|
|
|
from . import Singleton
|
2020-01-26 22:31:22 +00:00
|
|
|
from geruecht.exceptions import UsernameExistLDAP, LDAPExcetpion
|
2020-03-01 18:20:47 +00:00
|
|
|
from geruecht import ldapConfig
|
2020-03-10 18:23:52 +00:00
|
|
|
from geruecht.logger import getDebugLogger
|
|
|
|
|
|
|
|
debug = getDebugLogger()
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
class LDAPController(metaclass=Singleton):
|
|
|
|
'''
|
|
|
|
Authentification over LDAP. Create Account on-the-fly
|
|
|
|
'''
|
|
|
|
|
2020-03-01 18:20:47 +00:00
|
|
|
def __init__(self):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("init ldap controller")
|
2020-03-01 18:20:47 +00:00
|
|
|
self.dn = ldapConfig['dn']
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap = ldap
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("base dn is {{ {} }}".format(self.dn))
|
|
|
|
debug.debug("ldap is {{ {} }}".format(self.ldap))
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def login(self, username, password):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("login user {{ {} }} in ldap")
|
2019-12-19 07:12:29 +00:00
|
|
|
try:
|
2020-01-21 05:54:35 +00:00
|
|
|
retVal = self.ldap.authenticate(username, password, 'uid', self.dn)
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("authentification to ldap is {{ {} }}".format(retVal))
|
2020-01-21 05:54:35 +00:00
|
|
|
if not retVal:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("authenification is incorrect")
|
2020-01-21 05:54:35 +00:00
|
|
|
raise PermissionDenied("Invalid Password or Username")
|
|
|
|
except Exception as err:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.warning("exception while login into ldap", exc_info=True)
|
|
|
|
raise PermissionDenied("Invalid Password or Username. {}".format(err))
|
2019-12-19 07:12:29 +00:00
|
|
|
|
2020-01-26 22:31:22 +00:00
|
|
|
def bind(self, user, password):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("bind user {{ {} }} to ldap")
|
2020-01-26 22:31:22 +00:00
|
|
|
ldap_conn = self.ldap.connect(user.dn, password)
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("ldap_conn is {{ {} }}".format(ldap_conn))
|
2020-01-26 22:31:22 +00:00
|
|
|
return ldap_conn
|
|
|
|
|
2019-12-19 07:12:29 +00:00
|
|
|
def getUserData(self, username):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("get user data from ldap of user {{ {} }}".format(username))
|
2019-12-31 12:06:40 +00:00
|
|
|
try:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("search user in ldap")
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(uid={})'.format(username), SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
user = self.ldap.connection.response[0]['attributes']
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("user is {{ {} }}".format(user))
|
2020-01-21 05:54:35 +00:00
|
|
|
retVal = {
|
|
|
|
'dn': self.ldap.connection.response[0]['dn'],
|
|
|
|
'firstname': user['givenName'][0],
|
|
|
|
'lastname': user['sn'][0],
|
2020-06-05 21:43:16 +00:00
|
|
|
'uid': user['uid'][0],
|
2020-01-21 05:54:35 +00:00
|
|
|
}
|
2020-02-26 21:49:23 +00:00
|
|
|
if user['mail']:
|
|
|
|
retVal['mail'] = user['mail'][0]
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("user is {{ {} }}".format(retVal))
|
2020-06-05 21:43:16 +00:00
|
|
|
if retVal['uid'] == username:
|
|
|
|
return retVal
|
|
|
|
else:
|
|
|
|
raise Exception()
|
2019-12-31 12:06:40 +00:00
|
|
|
except:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.warning("exception in get user data from ldap", exc_info=True)
|
2019-12-31 12:06:40 +00:00
|
|
|
raise PermissionDenied("No User exists with this uid.")
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def getGroup(self, username):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("get group from user {{ {} }}".format(username))
|
2020-01-21 05:54:35 +00:00
|
|
|
try:
|
|
|
|
retVal = []
|
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(uid={})'.format(username), SUBTREE, attributes=['gidNumber'])
|
|
|
|
main_group_number = self.ldap.connection.response[0]['attributes']['gidNumber']
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("main group number is {{ {} }}".format(main_group_number))
|
2020-01-21 05:54:35 +00:00
|
|
|
if main_group_number:
|
2020-05-22 19:55:14 +00:00
|
|
|
if type(main_group_number) is list:
|
|
|
|
main_group_number = main_group_number[0]
|
2020-03-17 19:37:01 +00:00
|
|
|
self.ldap.connection.search('ou=group,{}'.format(self.dn), '(gidNumber={})'.format(main_group_number), attributes=['cn'])
|
2020-01-21 05:54:35 +00:00
|
|
|
group_name = self.ldap.connection.response[0]['attributes']['cn'][0]
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("group name is {{ {} }}".format(group_name))
|
2019-12-22 21:27:39 +00:00
|
|
|
if group_name == 'ldap-user':
|
|
|
|
retVal.append(USER)
|
2020-03-04 20:11:41 +00:00
|
|
|
if group_name == 'extern':
|
|
|
|
retVal.append(EXTERN)
|
2019-12-22 21:27:39 +00:00
|
|
|
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=group,{}'.format(self.dn), '(memberUID={})'.format(username), SUBTREE, attributes=['cn'])
|
|
|
|
groups_data = self.ldap.connection.response
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("groups number is {{ {} }}".format(groups_data))
|
2020-01-21 05:54:35 +00:00
|
|
|
for data in groups_data:
|
|
|
|
group_name = data['attributes']['cn'][0]
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("group name is {{ {} }}".format(group_name))
|
2020-01-21 05:54:35 +00:00
|
|
|
if group_name == 'finanzer':
|
|
|
|
retVal.append(MONEY)
|
|
|
|
elif group_name == 'gastro':
|
|
|
|
retVal.append(GASTRO)
|
|
|
|
elif group_name == 'bar':
|
|
|
|
retVal.append(BAR)
|
2020-03-04 20:11:41 +00:00
|
|
|
elif group_name == 'vorstand':
|
|
|
|
retVal.append(VORSTAND)
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("groups are {{ {} }}".format(retVal))
|
2020-01-21 05:54:35 +00:00
|
|
|
return retVal
|
|
|
|
except Exception as err:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.warning("exception in get groups from ldap", exc_info=True)
|
|
|
|
raise LDAPExcetpion(str(err))
|
2019-12-19 07:12:29 +00:00
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
def __isUserInList(self, list, username):
|
|
|
|
help_list = []
|
|
|
|
for user in list:
|
2019-12-26 09:28:30 +00:00
|
|
|
help_list.append(user['username'])
|
2019-12-22 22:09:18 +00:00
|
|
|
if username in help_list:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2019-12-26 09:28:30 +00:00
|
|
|
def getAllUser(self):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("get all users from ldap")
|
2019-12-26 09:28:30 +00:00
|
|
|
retVal = []
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(uid=*)', SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
data = self.ldap.connection.response
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("data is {{ {} }}".format(data))
|
2019-12-26 09:28:30 +00:00
|
|
|
for user in data:
|
2020-01-21 05:54:35 +00:00
|
|
|
if 'uid' in user['attributes']:
|
|
|
|
username = user['attributes']['uid'][0]
|
|
|
|
firstname = user['attributes']['givenName'][0]
|
|
|
|
lastname = user['attributes']['sn'][0]
|
2019-12-26 09:28:30 +00:00
|
|
|
retVal.append({'username': username, 'firstname': firstname, 'lastname': lastname})
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("users are {{ {} }}".format(retVal))
|
2019-12-26 09:28:30 +00:00
|
|
|
return retVal
|
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
def searchUser(self, searchString):
|
|
|
|
|
|
|
|
name = searchString.split(" ")
|
2019-12-26 09:28:30 +00:00
|
|
|
|
|
|
|
for i in range(len(name)):
|
|
|
|
name[i] = "*"+name[i]+"*"
|
|
|
|
|
|
|
|
|
|
|
|
print(name)
|
|
|
|
|
2019-12-22 22:09:18 +00:00
|
|
|
name_result = []
|
|
|
|
|
|
|
|
if len(name) == 1:
|
2019-12-26 09:28:30 +00:00
|
|
|
if name[0] == "**":
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(uid=*)', SUBTREE,
|
|
|
|
attributes=['uid', 'givenName', 'sn'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
2019-12-26 09:28:30 +00:00
|
|
|
else:
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(givenName={})'.format(name[0]), SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(sn={})'.format(name[0]), SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
2019-12-22 22:09:18 +00:00
|
|
|
else:
|
2020-01-21 05:54:35 +00:00
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(givenName={})'.format(name[1]), SUBTREE, attributes=['uid', 'givenName', 'sn'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
|
|
|
self.ldap.connection.search('ou=user,{}'.format(self.dn), '(sn={})'.format(name[1]), SUBTREE, attributes=['uid', 'givenName', 'sn', 'mail'])
|
|
|
|
name_result.append(self.ldap.connection.response)
|
2019-12-22 22:09:18 +00:00
|
|
|
retVal = []
|
|
|
|
|
2019-12-26 09:28:30 +00:00
|
|
|
for names in name_result:
|
|
|
|
for user in names:
|
2020-01-21 05:54:35 +00:00
|
|
|
if 'uid' in user['attributes']:
|
|
|
|
username = user['attributes']['uid'][0]
|
2019-12-26 09:28:30 +00:00
|
|
|
if not self.__isUserInList(retVal, username):
|
2020-01-21 05:54:35 +00:00
|
|
|
firstname = user['attributes']['givenName'][0]
|
|
|
|
lastname = user['attributes']['sn'][0]
|
2019-12-26 09:28:30 +00:00
|
|
|
retVal.append({'username': username, 'firstname': firstname, 'lastname': lastname})
|
2019-12-22 22:09:18 +00:00
|
|
|
|
|
|
|
return retVal
|
|
|
|
|
2020-01-26 22:31:22 +00:00
|
|
|
def modifyUser(self, user, conn, attributes):
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.info("modify ldap data from user {{ {} }} with attributes {{ {} }}".format(user, attributes))
|
2020-01-26 22:31:22 +00:00
|
|
|
try:
|
|
|
|
if 'username' in attributes:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("change username")
|
2020-01-26 22:31:22 +00:00
|
|
|
conn.search('ou=user,{}'.format(self.dn), '(uid={})'.format(attributes['username']))
|
|
|
|
if conn.entries:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.warning("username already exists", exc_info=True)
|
2020-01-26 22:31:22 +00:00
|
|
|
raise UsernameExistLDAP("Username already exists in LDAP")
|
|
|
|
#create modifyer
|
|
|
|
mody = {}
|
|
|
|
if 'username' in attributes:
|
|
|
|
mody['uid'] = [(MODIFY_REPLACE, [attributes['username']])]
|
|
|
|
if 'firstname' in attributes:
|
|
|
|
mody['givenName'] = [(MODIFY_REPLACE, [attributes['firstname']])]
|
|
|
|
if 'lastname' in attributes:
|
|
|
|
mody['sn'] = [(MODIFY_REPLACE, [attributes['lastname']])]
|
|
|
|
if 'mail' in attributes:
|
|
|
|
mody['mail'] = [(MODIFY_REPLACE, [attributes['mail']])]
|
|
|
|
if 'password' in attributes:
|
|
|
|
salted_password = hashed(HASHED_SALTED_MD5, attributes['password'])
|
|
|
|
mody['userPassword'] = [(MODIFY_REPLACE, [salted_password])]
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.debug("modyfier are {{ {} }}".format(mody))
|
2020-01-26 22:31:22 +00:00
|
|
|
conn.modify(user.dn, mody)
|
|
|
|
except Exception as err:
|
2020-03-10 18:23:52 +00:00
|
|
|
debug.warning("exception in modify user data from ldap", exc_info=True)
|
2020-01-26 22:31:22 +00:00
|
|
|
raise LDAPExcetpion("Something went wrong in LDAP: {}".format(err))
|
|
|
|
|
|
|
|
|
2019-12-19 07:12:29 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
a = LDAPController()
|
|
|
|
a.getUserData('jhille')
|