add findlastuid function

This commit is contained in:
fram3d 2023-06-20 23:48:43 +02:00
parent 237e7bd14e
commit 1e2f119f7f
Signed by: fram3d
GPG Key ID: 938920E709EEA32A

View File

@ -13,9 +13,27 @@ class LUSER():
''' '''
def findlastuid(self):
'''
Return the largest uidNumber attribute of all users in base
'''
self.ldapconnection.search(search_base=self.base,search_filter=f'(objectClass=inetOrgPerson)', attributes=['uidNumber'])
alluids = self.ldapconnection.response
max = 0
for i in alluids:
i_uid = i['attributes']['uidNumber']
if i_uid > max:
max = i_uid
return max
def expandbase(self, basealt = ''): def expandbase(self, basealt = ''):
''' '''
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
basealt := string base in LDAP system where users are made, if not set the function uses base specified on creation of LUSER instance (self.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
if basealt == '': if basealt == '':
@ -52,7 +70,7 @@ class LUSER():
return organization, dc, dcfull, domain return organization, dc, dcfull, domain
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, lastUID = 1000):
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
@ -63,12 +81,21 @@ class LUSER():
self.alt = True self.alt = True
self.autoconnect = autoconnect self.autoconnect = autoconnect
ldapserver = Server(ldap_host, use_ssl=True) ldapserver = Server(ldap_host, use_ssl=True)
lastuidfound = 0
if self.autoconnect: if self.autoconnect:
self.ldapconnection = Connection(ldapserver, admin_user, admin_pass, auto_bind=True) self.ldapconnection = Connection(ldapserver, admin_user, admin_pass, auto_bind=True)
# uid and gid of most recently registered users # uid and gid of most recently registered users
self.lastuid = 1337 lastuidfound = self.findlastuid()
self.lastgid = 1337 else:
self.ldapconnection = Connection(ldapserver, admin_user, admin_pass, auto_bind=False)
if lastuidfound == 0:
self.lastuid = lastUID
self.lastgid = lastUID
else:
self.lastuid = lastuidfound
self.lastgid = lastuidfound
# Set alt boolean to false if basealt not set # Set alt boolean to false if basealt not set
if basealt == '': if basealt == '':
@ -115,7 +142,6 @@ class LUSER():
self.lastuid += 1 self.lastuid += 1
self.lastgid += 1 self.lastgid += 1
# Add user to base # Add user to base
id = f"uid={user}" id = f"uid={user}"