diff --git a/models.py b/models.py index a7767fb..48bced6 100755 --- a/models.py +++ b/models.py @@ -8,9 +8,9 @@ USERATTRIBUTES = ['cn' , 'sn', 'givenName', 'uid', 'uidNumber' , 'gidNumber', 'h class logserver(): - def __init__(self, _host: str, admin_user: str, admin_pass: str, base: str, ssl: bool = True ): + def __init__(self, ldap_host: str, admin_user: str, admin_pass: str, base: str, ssl: bool = True ): - self._host = ldap_host + self.ldap_host = ldap_host self.admin_user = admin_user self.admin_pass = admin_pass self.base = base @@ -21,11 +21,11 @@ class logserver(): else: server = ldap3.Server(self.ldap_host) - self.conn = ldap3.Connection(ldapserver, admin_user, admin_pass, auto_bind=True) + self.conn = ldap3.Connection(server, admin_user, admin_pass, auto_bind=True) self.organization, self.dc, self.dcfull, self.domain = self.expandbase() - self.logbase = f'ou=log,{self.dc}' + self.logbase = f'ou=log,{self.dcfull}' # Unique id of the log branch self.id = self.getid() @@ -35,6 +35,7 @@ class logserver(): # How many changes are recoreded in total self.total = self.gettotal() + self.refreshtotal() def expandbase(self, base: str = '')->(str,str,str,str): ''' @@ -42,7 +43,7 @@ class logserver(): l dc values from base ''' if base == '': - base = self.logbase + base = self.base # Split base string with commas to find values of organization and dc baselist = base.split(",") @@ -158,7 +159,7 @@ l dc values from base if base == '': base = self.logbase - self.settotal(self.findtotal(base)) + self.settotal(self.findtotal(base), base) return self.gettotal(base) def findtotal(self, base: str = '')->int: @@ -190,21 +191,29 @@ l dc values from base log = self.getlog(log_number, base) - attributes = log['attribtes'] - uid = attributes['uid'] + if log == []: + return -1 + + attributes = log['attributes'] + + for key in attributes: + if isinstance(attributes[key], list): + attributes[key] = attributes[key][0] + + uid = attributes['cn'] action = attributes['description'] - attributes['description'] = '' + del attributes['description'] if action == 'ADD': - self.conn.add(f'uid={uid},{base}', OBJECTCLASSES, attributes) + self.conn.add(f'uid={uid},{self.base}', OBJECTCLASSES, attributes) elif action == 'DELETE': - self.conn.delete(f'uid={uid},{base}') + self.conn.delete(f'uid={uid},{self.base}') elif action == 'CHANGEPASS': - self.conn.modify(f'uid={uid},{base}', {'userPassword': (MODIFY_REPLACE,attributes['userPassword'])}) + self.conn.modify(f'uid={uid},{self.base}', {'userPassword': (ldap3.MODIFY_REPLACE,attributes['userPassword'])}) else: return f'Error: Unrecognized action in log entry: {action}' - self.setloaded(self.getloaded() + 1, base) + self.setloaded(self.getloaded(base) + 1, base) return self.conn.response def getlog(self, log_number: int, base: str = ''): @@ -212,13 +221,20 @@ l dc values from base base = self.logbase self.conn.search(search_base=f'uid={log_number},{base}',search_filter = '(objectClass=person)', attributes = USERATTRIBUTES) - return self.conn.response[0] + response = self.conn.response + + if response == []: + return response + else: + return response[0] + def setlog(self, attributes: dict, base: str = '', log_number: int = -1): if base == '': base = self.logbase if log_number == -1: + self.refreshtotal(base) log_number = self.gettotal(base) + 1 self.conn.add(f'uid={log_number},{base}', OBJECTCLASSES, attributes) @@ -229,7 +245,7 @@ l dc values from base Gets the base of a log copy of remote server on the local server ''' remoteid = remote.getid() - localdcfull = self.dcfull() + localdcfull = self.dcfull basecopy = f'ou=sync-{remoteid},{localdcfull}' @@ -241,18 +257,25 @@ l dc values from base return basecopy def pullfrom(self, source)->int: + pulled = 0 basecopy = self.getbasecopy(source) + self.refreshtotal(basecopy) + source.refreshtotal() + total_dest = self.gettotal(basecopy) total_src = source.gettotal() for log_number in range(total_dest + 1, total_src + 1): log = source.getlog(log_number) + if log == []: + break attributes = log['attributes'] self.setlog(attributes, basecopy) + pulled += 1 - return total_src - total_dest + return pulled def applyfrom(self, source)->int: basecopy = self.getbasecopy(source)