Compare commits

..

4 Commits

View File

@ -14,15 +14,16 @@ class LUSER():
def expandbase(self):
'''
Extract orgnaization, name of dc object and full domain part with all dc values from base
'''
'''
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 = ''
domain = ''
# Find ou in base and set it as organization variable
for i in baselist:
@ -39,19 +40,21 @@ class LUSER():
for i in baselist:
if i.split('=')[0] == 'dc':
# if first dc, add it from dc variable
if dcfull == '':
if dcfull == '' and domain == '':
dcfull = f'dc={dc}'
domain = dc
else:
dcfull += ',dc=' + i.split('=')[1]
domain += f'.{i.split("=")[1]}'
return organization, dc, dcfull
return organization, dc, dcfull, domain
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.organization, self.dc, self.dcfull, self.domain = self.expandbase()
self.basealt = basealt
self.alt = True
self.autoconnect = autoconnect
@ -67,73 +70,21 @@ class LUSER():
if basealt == '':
self.alt = False
def prepareluser(self):
def prepare(self):
'''
Create base on LDAP host
'''
# Create dcObject on LDAP server and store boolean indicating it's success
# Split base string with commas to find values of organization and dc
baselist = self.base.split(",")
basealtlist = self.basealt.split(",")
# Find ou in base and set it as organization variable
for i in baselist:
if i.split('=')[0] == 'ou':
organization = i.split('=')[1]
break
for i in basealtlist:
if i.split('=')[0] == 'ou':
organizationalt = i.split('=')[1]
break
# Find first dc and set it as dc variable
for i in baselist:
if i.split('=')[0] == 'dc':
dc = i.split('=')[1]
break
for i in basealtlist:
if i.split('=')[0] == 'dc':
dcalt = i.split('=')[1]
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]
for i in basealtlist:
if i.split('=')[0] == 'dc':
dcfullalt += ',dc=' + i.split('=')[1]
# Remove first column character
dcfull = dcfull[1:]
dcfullalt = dcfull[1:]
# Create organization on LDAP server and store boolean indicating it's success
rcode1 = self.ldapconnection.add(f'dc={dcfull}', ['dcObject', 'organization'], {'o' : dc, 'dc' : dc})
if self.alt:
rcode2 = self.ldapconnection.add(f'dc={dcfullalt}', ['dcObject', 'organization'], {'o' : dcalt, 'dc' : dcalt})
else:
rcode2 = True
rcode1 = self.ldapconnection.add(f'dc={self.dcfull}', ['dcObject', 'organization'], {'o' : self.dc, 'dc' : self.dc})
# Create organizational units on LDAP server and store boolean indicating it's success
rcode3 = self.ldapconnection.add(self.base, ['top', 'organizationalUnit'], {'ou' : organization})
if self.alt :
rcode4 = self.ldapconnection.add(self.basealt, ['top', 'organizationalUnit'], {'ou' : organizationalt})
else:
rcode4 = True
rcode2 = self.ldapconnection.add(self.base, ['top', 'organizationalUnit'], {'ou' : self.organization})
# Return True only if all return values are true
return rcode1 and rcode2 and rcode3 and rcode4
return rcode1 and rcode2
def lastpwchangenow(self):
'''
@ -163,9 +114,9 @@ class LUSER():
objectClass = ['top', 'person', 'organizationalPerson', 'inetOrgPerson', 'posixAccount', 'shadowAccount']
# Attributes for a user entry
attributes = {'cn' : user, 'sn' : user, 'givenName' : user, 'uid' : user, 'uidNumber' : self.lastuid, 'gidNumber' : self.lastgid, 'homeDirectory' : f'/home/{user}', 'loginShell' : '/usr/bin/git-shell', 'gecos' : 'SystemUser', 'shadowLastChange' : self.lastpwchangenow(), 'shadowMax' : '45', 'userPassword' : password }
attributes = {'cn' : user, 'sn' : user, 'givenName' : user, 'uid' : user, 'uidNumber' : self.lastuid, 'gidNumber' : self.lastgid, 'homeDirectory' : f'/home/{user}', 'loginShell' : '/usr/bin/git-shell', 'gecos' : 'SystemUser', 'shadowLastChange' : self.lastpwchangenow(), 'shadowMax' : '45', 'userPassword' : password, 'mail' : f'{user}@{self.domain}' }
attributesalt = {'cn' : user, 'sn' : user, 'givenName' : user, 'uid' : user, 'uidNumber' : self.lastuid, 'gidNumber' : self.lastgid, 'homeDirectory' : f'/home/{user}', 'loginShell' : '/usr//bin/git-shell', 'gecos' : 'SystemUser', 'shadowLastChange' : self.lastpwchangenow(), 'shadowMax' : '45', 'userPassword' : althash}
attributesalt = {'cn' : user, 'sn' : user, 'givenName' : user, 'uid' : user, 'uidNumber' : self.lastuid, 'gidNumber' : self.lastgid, 'homeDirectory' : f'/home/{user}', 'loginShell' : '/usr//bin/git-shell', 'gecos' : 'SystemUser', 'shadowLastChange' : self.lastpwchangenow(), 'shadowMax' : '45', 'userPassword' : althash, 'mail' : f'{user}@{self.domain}'}
# Return boolean value of new user entry
rcode1 = self.ldapconnection.add(f'{id},{self.base}', objectClass, attributes)