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
# -------------------------------------------------------------------------
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
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 = []
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),
"""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()
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)
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()