From 933efd4086fa0188abbe71af608fadd8ccfa3f10 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 16 Dec 2020 10:07:37 +0100 Subject: [PATCH] LdapServer: fix handing new superior DN in rename_object() and add delete_old parameter --- LdapServer.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/LdapServer.py b/LdapServer.py index 361be1a..7c9c652 100644 --- a/LdapServer.py +++ b/LdapServer.py @@ -205,13 +205,38 @@ class LdapServer(object): msg.append('%s - %s %s: %s' % (prefix, op, attr, val)) return '\n'.join(msg) - def rename_object(self,dn,new_rdn): + def rename_object(self, dn, new_rdn, new_sup=None, delete_old=True): + # If new_rdn is a complete DN, split new RDN and new superior DN + if len(new_rdn.split(',')) > 1: + self.logger.debug( + "LdapServer - Rename with a full new DN detected (%s): split new RDN and new superior DN", + new_rdn + ) + assert new_sup is None, "You can't provide a complete DN as new_rdn and also provide new_sup parameter" + new_dn_parts = new_rdn.split(',') + new_sup = ','.join(new_dn_parts[1:]) + new_rdn = new_dn_parts[0] try: - self.logger.debug("LdapServer - Rename %s in %s" % (dn,new_rdn)) - self.con.rename_s(dn,new_rdn) + self.logger.debug( + "LdapServer - Rename %s in %s (new superior: %s, delete old: %s)", + dn, + new_rdn, + "same" if new_sup is None else new_sup, + delete_old + ) + self.con.rename_s(dn, new_rdn, newsuperior=new_sup, delold=delete_old) return True except ldap.LDAPError, e: - self._error("LdapServer - Error renaming %s in %s : %s" % (dn,new_rdn,e), logging.ERROR) + self._error( + "LdapServer - Error renaming %s in %s (new superior: %s, delete old: %s): %s" % ( + dn, + new_rdn, + "same" if new_sup is None else new_sup, + delete_old, + e + ), + logging.ERROR + ) return False