add expand function

This commit is contained in:
fram3d 2023-06-20 19:54:11 +02:00
parent 35abb85365
commit 533a9885d4
Signed by: fram3d
GPG Key ID: 938920E709EEA32A
1 changed files with 41 additions and 3 deletions

View File

@ -12,14 +12,51 @@ class LUSER():
basealt := string base in LDAP system where users are made with password hashes generated for openalt
'''
def __init__(self, ldap_host, admin_user, admin_pass, base, basealt=''):
def expandbase(self):
'''
Extract orgnaization, name of dc object and full domain part with all dc values from base
'''
# Split base string with commas to find values of organization and dc
baselist = self.base.split(",")
organization = ''
dc = ''
dcfull = ''
# Find ou in base and set it as organization variable
for i in baselist:
if i.split('=')[0] == 'ou':
organization = i.split('=')[1]
# Find first dc and set it as dc variable
for i in baselist:
if i.split('=')[0] == 'dc':
dc = i.split('=')[1]
break
# Find full dc and set it as dcfull variable
for i in baselist:
if i.split('=')[0] == 'dc':
# if first dc, add it from dc variable
if dcfull == '':
dcfull = f'dc={dc}'
else:
dcfull += ',dc=' + i.split('=')[1]
return organization, dc, dcfull
def __init__(self, ldap_host, admin_user, admin_pass, base, basealt='', autoconnect=True):
self.ldap_host = ldap_host
self.admin_user = admin_user
self.admin_pass = admin_pass
self.base = base
self.organization, self.dc, self.dcfull = self.expandbase()
self.basealt = basealt
self.alt = True
self.autoconnect = autoconnect
ldapserver = Server(ldap_host, use_ssl=True)
if self.autoconnect:
self.ldapconnection = Connection(ldapserver, admin_user, admin_pass, auto_bind=True)
# uid and gid of most recently registered users
@ -62,6 +99,7 @@ class LUSER():
break
# Find full dc and set it as dcfull variable
dcfull = ''
for i in baselist:
if i.split('=')[0] == 'dc':
dcfull += ',dc=' + i.split('=')[1]