From: Frank Brehm Date: Thu, 4 May 2017 16:25:41 +0000 (+0200) Subject: Finishing barracuda-sync so far X-Git-Tag: 0.1.2~175^2~1 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=ed28190f2afbc1d9d59fabbfe31d35855f080d11;p=pixelpark%2Fadmin-tools.git Finishing barracuda-sync so far --- diff --git a/pp_lib/barracuda_sync_app.py b/pp_lib/barracuda_sync_app.py index 3aaaae2..4d0d83b 100644 --- a/pp_lib/barracuda_sync_app.py +++ b/pp_lib/barracuda_sync_app.py @@ -44,7 +44,7 @@ from .ldap_app import PpLdapAppError, PpLdapApplication from .mailaddress import MailAddress -__version__ = '0.4.1' +__version__ = '0.4.2' LOG = logging.getLogger(__name__) @@ -60,7 +60,7 @@ class PpBarracudaSyncApp(PpLdapApplication): LDAP entries used by Barracuda to ensure the existence of aliases. """ - default_barracuda_base_dn = 'ou=barracuda,ou=Applications, o=Pixelpark,o=isp' + default_barracuda_base_dn = 'ou=barracuda,ou=Applications,o=Pixelpark,o=isp' postfix_config_dir = os.sep + os.path.join('etc', 'postfix') postfix_maps_dir = os.path.join(postfix_config_dir, 'maps') @@ -258,7 +258,7 @@ class PpBarracudaSyncApp(PpLdapApplication): continue pattern = r'^' + alias if not MailAddress.valid_address(alias): - pattern += r'(?:@' + re.escape(self.origin) + r')?' + pattern += r'(?:@(?:.*\.)?' + re.escape(self.origin) + r')?' pattern += r'\s*$' regex = re.compile(pattern, re.IGNORECASE) self.ignore_aliases_res.append(regex) @@ -285,9 +285,13 @@ class PpBarracudaSyncApp(PpLdapApplication): LOG.debug("Existing aliases:\n{}".format(pp(self.existing_aliases))) self.read_ldap_aliases() - self.eval_diffs() + self.add_failing_ldap_entries() + self.remove_unnecessary_aliases() + + LOG.info("Fertsch!") + # ------------------------------------------------------------------------- def read_virtaliases_files(self): @@ -366,6 +370,60 @@ class PpBarracudaSyncApp(PpLdapApplication): LOG.info("Aliases to create in LDAP:\n{}".format(pp(self.aliases_to_create))) LOG.info("Aliases to remove from LDAP:\n{}".format(pp(self.aliases_to_remove))) + # ------------------------------------------------------------------------- + def add_failing_ldap_entries(self): + + LOG.info("Adding failing LDAP aliases ...") + + for alias in self.aliases_to_create: + + mail = alias + if not MailAddress.valid_address(alias): + mail += '@' + self.origin + + dn = 'cn=' + alias + ',' + self.barracuda_base_dn + object_class = ["top", "mailRecipient"] + attributes = { + 'mail': mail, + } + LOG.info("Creating LDAP alias {a!r} => {dn!r}.".format(a=alias, dn=dn)) + LOG.debug("Object-Classes: {}".format(pp(object_class))) + LOG.debug("Attributes: {}".format(pp(attributes))) + if not self.simulate: + self.ldap_connection.add(dn, object_class, attributes) + LOG.debug("Result: {}".format(self.ldap_connection.result)) + + # ------------------------------------------------------------------------- + def remove_unnecessary_aliases(self): + + LOG.info("Removing unnecessary LDAP aliases ...") + + attributes = ['cn', 'mail'] + + for alias in self.aliases_to_remove: + + query = '(&(objectclass=mailRecipient)(cn=' + alias + '))' + LOG.debug("Searching for entry with CN {!r}.".format(alias)) + self.ldap_connection.search( + search_base=self.barracuda_base_dn, + search_filter=query, + search_scope=LEVEL, + attributes=attributes) + + LOG.debug("Found {} entries.".format(len(self.ldap_connection.response))) + + if not self.ldap_connection.response: + LOG.error("No LDAP entry found for CN {!r}.".format(alias)) + continue + entry = self.ldap_connection.response[0] + dn = entry['dn'] + + LOG.info("Removing LDAP entry {!r} ...".format(dn)) + if not self.simulate: + self.ldap_connection.delete(dn) + LOG.debug("Result: {}".format(self.ldap_connection.result)) + + # ============================================================================= if __name__ == "__main__":