From e48009fc4315d5652161a0609e02abe182ecb7e0 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 12 May 2023 17:13:14 +0200 Subject: [PATCH] Starting with lib/pp_admintools/app/barracuda_sync.py --- lib/pp_admintools/app/barracuda_sync.py | 189 ++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 lib/pp_admintools/app/barracuda_sync.py diff --git a/lib/pp_admintools/app/barracuda_sync.py b/lib/pp_admintools/app/barracuda_sync.py new file mode 100644 index 0000000..422e6e1 --- /dev/null +++ b/lib/pp_admintools/app/barracuda_sync.py @@ -0,0 +1,189 @@ +# -*- coding: utf-8 -*- +""" +@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 re +# import sys + +from pathlib import Path + +from fb_tools.multi_config import DEFAULT_ENCODING + +from fb_tools.argparse_actions import DirectoryOptionAction + +# Own modules +from ..xlate import XLATOR + +from .ldap import BaseLdapApplication + +__version__ = '0.6.0' +LOG = logging.getLogger(__name__) + +_ = XLATOR.gettext +ngettext = XLATOR.ngettext + + +# ============================================================================= +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.""" + + default_barracuda_base_dn = 'ou=barracuda,ou=Applications,o=Pixelpark,o=isp' + postfix_config_dir = Path('/etc/postfix') + postfix_maps_dir = postfix_config_dir / 'maps' + + default_virtaliases_basename = 'virtual-aliases' + default_virtaliases_files = [postfix_maps_dir / default_virtaliases_basename, ] + default_postfix_db_hashtype = 'hash' + + postfix_filetype_extensions = { + 'btree': '.db', + 'cdb': '.cdb', + 'dbm': '.pag', + 'hash': '.db', + 'lmdb': '.lmdb', + 'sdbm': '.pag', + } + + default_ignore_aliases = ['root', ] + + default_origin = 'pixelpark.com' + + re_virtaliases_line = re.compile(r'^([^#\s:]+)\s', re.MULTILINE) + + default_postmap_command = 'postmap' + default_postconf_command = 'postconf' + + open_args = { + 'encoding': DEFAULT_ENCODING, + 'errors': 'surrogateescape', + } + + additional_searchpaths = [ + Path('/sbin'), + Path('/usr/sbin'), + Path('/usr/local/sbin'), + Path('/usr/local/bin'), + ] + + show_simulate_option = True + + use_default_ldap_connection = False + use_multiple_ldap_connections = False + show_cmdline_ldap_timeout = True + apply_default_ldap_instance_if_not_given = False + + # ------------------------------------------------------------------------- + def __init__(self, appname=None, base_dir=None): + """Constructor.""" + 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.postconf_command = Path('/sbin') / self.default_postconf_command + self.postmap_command = Path('/sbin') / self.default_postmap_command + + self.existing_aliases = [] + self.ldap_aliases = [] + self.aliases_to_create = [] + self.aliases_to_remove = [] + self.ignore_aliases_res = [] + + desc = _( + "Synchronization of existing virtual aliases " + "with alias definitions in LDAP for Barracuda." + ) + + super(BarracudaSyncApp, self).__init__( + appname=appname, description=desc, base_dir=base_dir, + version=__version__, initialized=False) + + self.initialized = True + + # ------------------------------------------------------------------------- + def init_arg_parser(self): + """Initialize additional command line parameters.""" + 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( + '-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), + ) + + 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), + ) + + 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), + ) + + super(BarracudaSyncApp, self).init_arg_parser() + + # ------------------------------------------------------------------------- + def _verify_instances(self): + + super(BarracudaSyncApp, self)._verify_instances(readonly=False) + + # ------------------------------------------------------------------------- + def post_init(self): + """ + Method to execute before calling run(). + """ + + super(BarracudaSyncApp, self).post_init() + + self._check_postfix() + self._check_virtaliases_files() + # self._init_ignore_aliases_res() + + # ------------------------------------------------------------------------- + def _check_postfix(self): + """Checking postfix commands and lookup table types.""" + LOG.debug(_('Checking postfix commands and lookup table types ...')) + + # ------------------------------------------------------------------------- + def _check_virtaliases_files(self): + """Checking existence of given .""" + LOG.debug(_('Checking postfix commands and lookup table types ...')) + + # ------------------------------------------------------------------------- + def _run(self): + + LOG.info("And here we go ...") + + +# ============================================================================= +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list -- 2.39.5