From ac245e71e96575586b9a547959ce3f5c2ffd088a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Fri, 16 Jun 2017 09:56:58 +0200 Subject: [PATCH] fix for AD machine removal --- .../core/managers/userservice/opchecker.py | 10 ++++++++- .../WindowsOsManager/WinDomainOsManager.py | 21 +++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/server/src/uds/core/managers/userservice/opchecker.py b/server/src/uds/core/managers/userservice/opchecker.py index 37a4be4d2..4540669a1 100644 --- a/server/src/uds/core/managers/userservice/opchecker.py +++ b/server/src/uds/core/managers/userservice/opchecker.py @@ -40,7 +40,7 @@ from uds.models import UserService import logging -__updated__ = '2015-11-10' +__updated__ = '2017-06-15' logger = logging.getLogger(__name__) @@ -138,10 +138,18 @@ class UpdateFromPreparing(StateUpdater): class UpdateFromRemoving(StateUpdater): def finish(self): + osManager = self.userServiceInstance.osmanager() + if osManager is not None: + osManager.release(self.userService) + self.save(State.REMOVED) class UpdateFromCanceling(StateUpdater): def finish(self): + osManager = self.userServiceInstance.osmanager() + if osManager is not None: + osManager.release(self.userService) + self.save(State.CANCELED) class UpdateFromOther(StateUpdater): diff --git a/server/src/uds/osmanagers/WindowsOsManager/WinDomainOsManager.py b/server/src/uds/osmanagers/WindowsOsManager/WinDomainOsManager.py index db230f1ff..1f8e67918 100644 --- a/server/src/uds/osmanagers/WindowsOsManager/WinDomainOsManager.py +++ b/server/src/uds/osmanagers/WindowsOsManager/WinDomainOsManager.py @@ -53,7 +53,7 @@ class WinDomainOsManager(WindowsOsManager): if values['password'] == '': raise osmanagers.OSManager.ValidationException(_('Must provide a password for the account!')) self._domain = values['domain'] - self._ou = values['ou'] + self._ou = values['ou'].strip() self._account = values['account'] self._password = values['password'] else: @@ -121,6 +121,10 @@ class WinDomainOsManager(WindowsOsManager): ''' super(WinDomainOsManager, self).release(service) + if not '.' in self._domain: + logger.info('Releasing from a not FQDN domain is not supported') + return + try: l = self.__connectLdap() except dns.resolver.NXDOMAIN: # No domain found, log it and pass @@ -130,14 +134,19 @@ class WinDomainOsManager(WindowsOsManager): logger.exception('Ldap Exception caught') log.doLog(service, log.WARN, "Could not remove machine from domain (invalid credentials for {0})".format(self._account), log.OSMANAGER) - # _filter = '(&(objectClass=computer)(sAMAccountName=%s$))' % service.friendly_name try: - # res = l.search_ext_s(base = self._ou, scope = ldap.SCOPE_SUBTREE, - # filterstr = _filter)[0] - l.delete('cn={0},{1}'.format(service.friendly_name, self._ou)) + if self._ou: + ou = self._ou + else: + ou = ','.join(['DC=' + i for i in self._domain.split('.')]) + fltr = '(&(objectClass=computer)(sAMAccountName={}$))'.format(service.friendly_name) + res = l.search_ext_s(base=ou, scope=ldap.SCOPE_SUBTREE, filterstr=fltr)[0] + l.delete_s(res[0]) # Remove by DN, SYNC + except IndexError: + logger.error('Error deleting {} from BASE {}'.format(service.friendly_name, ou)) except Exception: - logger.exception('Not found: ') + logger.exception('Deleting from AD: ') def check(self): try: