Compare commits

..

No commits in common. "ca91a5512b258c7822533a73de50fcd419046031" and "533a9885d427646b7974e3391c5ac6099d5061ce" have entirely different histories.

View File

@ -14,16 +14,15 @@ class LUSER():
def expandbase(self): 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 # Split base string with commas to find values of organization and dc
baselist = self.base.split(",") baselist = self.base.split(",")
organization = '' organization = ''
dc = '' dc = ''
dcfull = '' dcfull = ''
domain = ''
# Find ou in base and set it as organization variable # Find ou in base and set it as organization variable
for i in baselist: for i in baselist:
@ -40,21 +39,19 @@ class LUSER():
for i in baselist: for i in baselist:
if i.split('=')[0] == 'dc': if i.split('=')[0] == 'dc':
# if first dc, add it from dc variable # if first dc, add it from dc variable
if dcfull == '' and domain == '': if dcfull == '':
dcfull = f'dc={dc}' dcfull = f'dc={dc}'
domain = dc
else: else:
dcfull += ',dc=' + i.split('=')[1] dcfull += ',dc=' + i.split('=')[1]
domain += f'.{i.split("=")[1]}'
return organization, dc, dcfull, domain return organization, dc, dcfull
def __init__(self, ldap_host, admin_user, admin_pass, base, basealt='', autoconnect=True): def __init__(self, ldap_host, admin_user, admin_pass, base, basealt='', autoconnect=True):
self.ldap_host = ldap_host self.ldap_host = ldap_host
self.admin_user = admin_user self.admin_user = admin_user
self.admin_pass = admin_pass self.admin_pass = admin_pass
self.base = base self.base = base
self.organization, self.dc, self.dcfull, self.domain = self.expandbase() self.organization, self.dc, self.dcfull = self.expandbase()
self.basealt = basealt self.basealt = basealt
self.alt = True self.alt = True
self.autoconnect = autoconnect self.autoconnect = autoconnect
@ -70,21 +67,73 @@ class LUSER():
if basealt == '': if basealt == '':
self.alt = False self.alt = False
def prepare(self): def prepareluser(self):
''' '''
Create base on LDAP host Create base on LDAP host
''' '''
# Create dcObject on LDAP server and store boolean indicating it's success
rcode1 = self.ldapconnection.add(f'dc={self.dcfull}', ['dcObject', 'organization'], {'o' : self.dc, 'dc' : self.dc}) # 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
# Create organizational units on LDAP server and store boolean indicating it's success # Create organizational units on LDAP server and store boolean indicating it's success
rcode2 = self.ldapconnection.add(self.base, ['top', 'organizationalUnit'], {'ou' : self.organization}) 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
# Return True only if all return values are true # Return True only if all return values are true
return rcode1 and rcode2 return rcode1 and rcode2 and rcode3 and rcode4
def lastpwchangenow(self): def lastpwchangenow(self):
''' '''
@ -114,9 +163,9 @@ class LUSER():
objectClass = ['top', 'person', 'organizationalPerson', 'inetOrgPerson', 'posixAccount', 'shadowAccount'] objectClass = ['top', 'person', 'organizationalPerson', 'inetOrgPerson', 'posixAccount', 'shadowAccount']
# Attributes for a user entry # 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, 'mail' : f'{user}@{self.domain}' } 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 }
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}'} 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}
# Return boolean value of new user entry # Return boolean value of new user entry
rcode1 = self.ldapconnection.add(f'{id},{self.base}', objectClass, attributes) rcode1 = self.ldapconnection.add(f'{id},{self.base}', objectClass, attributes)