# -*- coding: utf-8 -*-
"""
+@summary: An application module for the barracuda-sync application object.
+
@author: Frank Brehm
@contact: frank.brehm@pixelpark.com
@copyright: © 2023 by Frank Brehm, Berlin
-@summary: An application module for the barracuda-sync application object.
"""
from __future__ import absolute_import
# Standard modules
-import logging
import copy
+import logging
import re
# import sys
-
from pathlib import Path
-from fb_tools.multi_config import DEFAULT_ENCODING
-
+# Third party modules
from fb_tools.argparse_actions import DirectoryOptionAction
+from fb_tools.handler import BaseHandler
+from fb_tools.multi_config import DEFAULT_ENCODING
+from fb_tools.xlate import format_list
# Own modules
-from ..xlate import XLATOR
-
from .ldap import BaseLdapApplication
+from ..xlate import XLATOR
-__version__ = '0.6.0'
+__version__ = '0.6.1'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
# =============================================================================
class BarracudaSyncApp(BaseLdapApplication):
- """Class for the 'barracuda-sync' application to ensure a synchronisation
- of all existing aliases and virtual aliases in Postfix with the
- LDAP entries used by Barracuda to ensure the existence of aliases."""
+ """Class for the 'barracuda-sync' application.
+
+ It ensures a synchronisation of all existing aliases and virtual aliases in Postfix with
+ the LDAP entries used by Barracuda to ensure the existence of aliases.
+ """
default_barracuda_base_dn = 'ou=barracuda,ou=Applications,o=Pixelpark,o=isp'
postfix_config_dir = Path('/etc/postfix')
# -------------------------------------------------------------------------
def __init__(self, appname=None, base_dir=None):
- """Constructor."""
+ """Constructz the application object."""
self.barracuda_base_dn = self.default_barracuda_base_dn
self.virtaliases_files = copy.copy(self.default_virtaliases_files)
- self.postfix_map_extension = self.default_postfix_map_extension
+ self.postfix_db_hashtype = self.default_postfix_db_hashtype
self.postconf_command = Path('/sbin') / self.default_postconf_command
self.postmap_command = Path('/sbin') / self.default_postmap_command
+ self.lookup_table_types = []
self.existing_aliases = []
self.ldap_aliases = []
self.aliases_to_create = []
self.ignore_aliases_res = []
desc = _(
- "Synchronization of existing virtual aliases "
- "with alias definitions in LDAP for Barracuda."
+ 'Synchronization of existing virtual aliases '
+ 'with alias definitions in LDAP for Barracuda.'
)
super(BarracudaSyncApp, self).__init__(
'-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)),
+ '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(
'-B', '--basename', '--virtalias-basename', nargs='*', dest='basename',
metavar=_('NAME'),
help=_(
- "All possible basenames of the virtual aliases file below the latter "
- "directory. All of these basenames are used as source of the virtual aliases. "
- "Default: {!r}.").format(self.default_virtaliases_basename),
+ 'All possible basenames of the virtual aliases file below the latter '
+ 'directory. All of these basenames are used as source of the virtual aliases. '
+ 'Default: {!r}.').format(self.default_virtaliases_basename),
)
sync_group.add_argument(
'--type', dest='hashtype', metavar=_('TYPE'),
help=_(
- "The used lookup table type of all virtual aliases table. "
- "Default: {!r}.").format(self.default_postfix_db_hashtype),
+ 'The used lookup table type of all virtual aliases table. '
+ 'Default: {!r}.').format(self.default_postfix_db_hashtype),
)
sync_group.add_argument(
'--base-dn', dest='baase_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),
+ 'The DN of LDAP container (mostly an OU), where the virtual alias entries '
+ 'should be located. Default: {!r}.').format(self.default_barracuda_base_dn),
)
super(BarracudaSyncApp, self).init_arg_parser()
# -------------------------------------------------------------------------
def post_init(self):
- """
- Method to execute before calling run().
- """
-
+ """Execute this method before calling run()."""
super(BarracudaSyncApp, self).post_init()
self._check_postfix()
# -------------------------------------------------------------------------
def _check_postfix(self):
- """Checking postfix commands and lookup table types."""
+ """Check postfix commands and lookup table types."""
LOG.debug(_('Checking postfix commands and lookup table types ...'))
+ found_all_commands = True
+
+ cmd_path = self.get_command(self.default_postconf_command, quiet=False, resolve=True)
+ if cmd_path:
+ self.postconf_command = cmd_path
+ else:
+ found_all_commands = False
+
+ cmd_path = self.get_command(self.default_postmap_command, quiet=False, resolve=True)
+ if cmd_path:
+ self.postmap_command = cmd_path
+ else:
+ found_all_commands = False
+
+ if not found_all_commands:
+ LOG.err(_('Postfix seems not to be installed.'))
+ self.exit(5)
+
+ LOG.debug(_('Evaluating lookup table types.'))
+ handler = BaseHandler(appname=self.appname, verbose=self.verbose)
+
+ pdata = handler.call([str(self.postconf_command), '-m'], quiet=True)
+ if pdata.returncode > 0:
+ msg = _('Error {} on evaluating lookup table types').format(pdata.returncode)
+ if pdata.stderr:
+ msg += ': ' + pdata.stderr
+ else:
+ msg += '.'
+ LOG.err(msg)
+ self.exit(6)
+
+ if self.verbose > 2:
+ LOG.debug(_('Result:') + '\n' + str(pdata))
+
+ self.lookup_table_types = []
+ for token in re.split(r'\s+', pdata.stdout):
+ token = token.strip()
+ if token and token not in self.lookup_table_types:
+ self.lookup_table_types.append(token)
+
+ if len(self.lookup_table_types) == 0:
+ msg = _('Did not found valid lookup table types.')
+ LOG.err(msg)
+ self.exit(6)
+
+ if self.args.hashtype:
+ htype = self.args.hashtype
+ if htype not in self.lookup_table_types:
+ msg = _('Wrong lookup table type {!r} given. Valid types are:').format(htype)
+ msg += ' ' + format_list(self.lookup_table_types, do_repr=True)
+ LOG.error(msg)
+ self.exit(1)
+ self.postfix_db_hashtype = htype
+
# -------------------------------------------------------------------------
def _check_virtaliases_files(self):
- """Checking existence of given ."""
+ """Check existence of given ."""
LOG.debug(_('Checking postfix commands and lookup table types ...'))
# -------------------------------------------------------------------------
def _run(self):
- LOG.info("And here we go ...")
+ LOG.info('And here we go ...')
# =============================================================================
-if __name__ == "__main__":
+if __name__ == '__main__':
pass