]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Verifying Barracuda lDAP container
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 24 May 2023 11:57:14 +0000 (13:57 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 24 May 2023 11:57:14 +0000 (13:57 +0200)
lib/pp_admintools/app/barracuda_sync.py

index 6850e3c9d0ab63333cbc3f6bc8194f463cf2369d..6279e9f8bc644c7e04a4762b7063327084619569 100644 (file)
@@ -16,17 +16,18 @@ import re
 from pathlib import Path
 
 # Third party modules
-from fb_tools.argparse_actions import DirectoryOptionAction
 from fb_tools.common import pp
 from fb_tools.handler import BaseHandler
 from fb_tools.multi_config import DEFAULT_ENCODING
 # from fb_tools.xlate import format_list
 
+from ldap3 import BASE
+
 # Own modules
 from .ldap import BaseLdapApplication
 from ..xlate import XLATOR
 
-__version__ = '0.7.7'
+__version__ = '0.8.1'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -95,7 +96,7 @@ class BarracudaSyncApp(BaseLdapApplication):
 
     # -------------------------------------------------------------------------
     def __init__(self, appname=None, base_dir=None):
-        """Constructz the application object."""
+        """Construct the application object."""
         self.barracuda_base_dn = self.default_barracuda_base_dn
         self.virtalias_mappings = []
         self.postfix_db_hashtype = self.default_postfix_db_hashtype
@@ -107,6 +108,7 @@ class BarracudaSyncApp(BaseLdapApplication):
         self.lookup_table_types = []
         self.existing_aliases = {}
         self.ldap_aliases = []
+        self.ldap_mail_dns = []
         self.aliases_to_create = []
         self.aliases_to_remove = []
         self.ignore_aliases_res = []
@@ -128,15 +130,7 @@ class BarracudaSyncApp(BaseLdapApplication):
         sync_group = self.arg_parser.add_argument_group(_('Barracuda sync options'))
 
         sync_group.add_argument(
-            '-D', '--directory', dest='directory', metavar=_('DIR'),
-            action=DirectoryOptionAction, must_exists=True,
-            help=_(
-                'The directory containing the virtual aliases mapping file. '
-                'It has to be exists. Default: {!r}.').format(str(self.postfix_maps_dir)),
-        )
-
-        sync_group.add_argument(
-            '--base-dn', dest='baase_dn', metavar='DN',
+            '-B', '--base-dn', dest='base_dn', metavar='DN',
             help=_(
                 'The DN of LDAP container (mostly an OU), where the virtual alias entries '
                 'should be located. Default: {!r}.').format(self.default_barracuda_base_dn),
@@ -154,6 +148,9 @@ class BarracudaSyncApp(BaseLdapApplication):
         """Execute this method before calling run()."""
         super(BarracudaSyncApp, self).post_init()
 
+        if self.args.base_dn and self.args.base_dn.strip():
+            self.barracuda_base_dn = self.args.base_dn.strip()
+
         self._check_postfix_commands()
         self._check_postfix_table_types()
         self._get_postfix_default_db_type()
@@ -407,10 +404,13 @@ class BarracudaSyncApp(BaseLdapApplication):
             m = self.re_pf_mapping.match(line)
             if m:
                 alias = m.group('key')
+                cn = alias
                 if '@' not in alias:
                     alias += '@' + self.postfix_origin
-                if alias not in self.existing_aliases:
-                    self.existing_aliases[alias] = []
+                if cn not in self.existing_aliases:
+                    self.existing_aliases[cn] = {}
+                    self.existing_aliases[cn]['alias'] = alias
+                    self.existing_aliases[cn]['targets'] = []
 
                 val = m.group('value').strip()
                 targets = self.re_pf_fieldsep.split(val)
@@ -418,12 +418,32 @@ class BarracudaSyncApp(BaseLdapApplication):
                     target = target.strip()
                     if '@' not in target:
                         target += '@' + self.postfix_origin
-                    if target not in self.existing_aliases[alias]:
-                        self.existing_aliases[alias].append(target)
+                    if target not in self.existing_aliases[cn]['targets']:
+                        self.existing_aliases[cn]['targets'].append(target)
+
+    # -------------------------------------------------------------------------
+    def verify_barracuda_container(self):
+        """Verify existence of the LDAP container (OU) of the alias definitions for Barracuda."""
+        LOG.info(_('Verifying existence of the LDAP container (OU) {!r} for Barracuda.').format(
+            self.barracuda_base_dn))
+
+        inst = self.ldap_instances[0]
+        dn_list = self.get_all_entry_dns(inst, base_dn=self.barracuda_base_dn, scope=BASE)
+
+        if len(dn_list) == 0:
+            msg = _('LDAP container {c!r} for Barracuda not found in instance {i!r}.').format(
+                c=self.barracuda_base_dn, i=inst)
+            LOG.error(msg)
+            self.exit(8)
+
+        msg = _('LDAP container {c!r} for Barracuda found in instance {i!r}.').format(
+            c=self.barracuda_base_dn, i=inst)
+        LOG.debug(msg)
 
     # -------------------------------------------------------------------------
     def _run(self):
 
+        self.verify_barracuda_container()
         self.read_virtual_alias_mappings()