From 533a9885d427646b7974e3391c5ac6099d5061ce Mon Sep 17 00:00:00 2001 From: fram3d Date: Tue, 20 Jun 2023 19:54:11 +0200 Subject: [PATCH] add expand function --- luser/models.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/luser/models.py b/luser/models.py index b6cbb8e..ead7d4b 100644 --- a/luser/models.py +++ b/luser/models.py @@ -12,15 +12,52 @@ 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) - self.ldapconnection = Connection(ldapserver, admin_user, admin_pass, auto_bind=True) + if self.autoconnect: + self.ldapconnection = Connection(ldapserver, admin_user, admin_pass, auto_bind=True) # uid and gid of most recently registered users self.lastuid = 1337 @@ -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] @@ -125,7 +163,7 @@ 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 } 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}