from .mailaddress import MailAddress
-__version__ = '0.4.1'
+__version__ = '0.4.2'
LOG = logging.getLogger(__name__)
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')
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)
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):
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__":