From 89f44c9e54a9e33d0d2fff47e576c98a611bd6d8 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 20 May 2022 13:15:03 +0200 Subject: [PATCH] Adding application get-ldap-server-info and its app module pp_admintools.get_ldap_server_info. --- bin/get-ldap-server-info | 65 +++++++ lib/pp_admintools/get_ldap_server_info.py | 205 ++++++++++++++++++++++ 2 files changed, 270 insertions(+) create mode 100755 bin/get-ldap-server-info create mode 100644 lib/pp_admintools/get_ldap_server_info.py diff --git a/bin/get-ldap-server-info b/bin/get-ldap-server-info new file mode 100755 index 0000000..797d68f --- /dev/null +++ b/bin/get-ldap-server-info @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +from __future__ import print_function + +# Standard modules +import sys + +__exp_py_version_major__ = 3 +__min_py_version_minor__ = 6 + +if sys.version_info[0] != __exp_py_version_major__: + print("This script is intended to use with Python {}.".format( + __exp_py_version_major__), file=sys.stderr) + print("You are using Python: {0}.{1}.{2}-{3}-{4}.".format( + *sys.version_info) + "\n", file=sys.stderr) + sys.exit(1) + +if sys.version_info[1] < __min_py_version_minor__: + print("A minimal Python version of {maj}.{min} is necessary to execute this script.".format( + maj=__exp_py_version_major__, min=__min_py_version_minor__), file=sys.stderr) + print("You are using Python: {0}.{1}.{2}-{3}-{4}.".format( + *sys.version_info) + "\n", file=sys.stderr) + sys.exit(1) + +# Standard modules +import os +import locale + +try: + from pathlib import Path +except ImportError: + from pathlib2 import Path + +# own modules: + +my_path = Path(__file__) +my_real_path = my_path.resolve() +bin_path = my_real_path.parent +base_dir = bin_path.parent +lib_dir = base_dir.joinpath('lib') +module_dir = lib_dir.joinpath('pp_admintools') + +if module_dir.exists(): + sys.path.insert(0, str(lib_dir)) + +from pp_admintools.get_ldap_server_info import GetLdapServerInfoApp + +__author__ = 'Frank Brehm ' +__copyright__ = '(C) 2022 by Frank Brehm, Pixelpark GmbH, Berlin' + +appname = os.path.basename(sys.argv[0]) + +locale.setlocale(locale.LC_ALL, '') + +app = GetLdapServerInfoApp(appname=appname, base_dir=base_dir) +app.initialized = True + +if app.verbose > 2: + print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) + +app() + +sys.exit(0) + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/lib/pp_admintools/get_ldap_server_info.py b/lib/pp_admintools/get_ldap_server_info.py new file mode 100644 index 0000000..5b3fede --- /dev/null +++ b/lib/pp_admintools/get_ldap_server_info.py @@ -0,0 +1,205 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank.brehm@pixelpark.com +@copyright: © 2022 by Frank Brehm, Berlin +@summary: A module for the application class for get-ldap-server-info application. +""" +from __future__ import absolute_import + +import os +import logging +import textwrap +import argparse + +# Third party modules +from pytz import timezone, UnknownTimeZoneError + +from ldap3 import NONE, DSA, SCHEMA, ALL + +from fb_tools.common import pp, to_str, is_sequence +from fb_tools.xlate import format_list + +# Own modules +from . import __version__ as GLOBAL_VERSION + +from .errors import PpAppError + +from .ldap_config import LdapConfigError, LdapConfiguration + +from .ldap_app import LdapAppError, BaseLdapApplication, LdapConnection + +from .xlate import XLATOR + +__version__ = '0.1.0' +LOG = logging.getLogger(__name__) + +_ = XLATOR.gettext +# ngettext = XLATOR.ngettext + + +# ============================================================================= +class GetLdapServerInfoError(PpAppError): + pass + + +# ============================================================================= +class GetLdapServerInfoApp(BaseLdapApplication): + """ + Application class for showing relevant information about a LDAP server. + """ + + # ------------------------------------------------------------------------- + def __init__( + self, appname=None, base_dir=None, version=GLOBAL_VERSION, + cfg_class=LdapConfiguration): + + description = _( + 'Shows all relevant information about the given LDAP server.') + + super(GetLdapServerInfoApp, self).__init__( + appname=appname, version=version, description=description, base_dir=base_dir, + cfg_class=cfg_class, initialized=False, + ) + + # ------------------------------------------------------------------------- + def _run(self): + + if self.verbose: + LOG.info(_("Starting ...")) + print('') + + connect_info = self.cfg.ldap_connection[self.ldap_instance] + timeout = self.cfg.ldap_timeout + + ldap = LdapConnection( + connect_info, get_info=ALL, connect_timeout=timeout, receive_timeout=timeout, + read_only=True, appname=self.appname, verbose=self.verbose, + base_dir=self.base_dir, initialized=True) + + ldap.connect() + + if self.verbose > 2: + LOG.debug(_("LDAP connection after binding:") + '\n' + pp(ldap.as_dict())) + + info_data = [] + + info_data.append((_("Supported LDAP versions"), self.get_supported_versions(ldap))) + info_data.append((_("Naming contexts"), self.get_naming_contexts(ldap))) + info_data.append((_("Schema entry"), self.get_schema_entry(ldap))) + info_data.append((_("Vendor name"), self.get_vendor_name(ldap))) + info_data.append((_("Vendor version"), self.get_vendor_version(ldap))) + + max_len = 1 + for entry in info_data: + if len(entry[0]) > max_len: + max_len = len(entry[0]) + max_len += 1 + + title = _("Information about LDAP server {}:").format(connect_info.url) + print('') + print(title) + print('-' * len(title)) + print('') + + for entry in info_data: + i = 0 + for value in entry[1]: + if i == 0: + title = entry[0] + ':' + else: + title = '' + i += 1 + print("{title:<{width}} {val}".format(title=title, width=max_len, val=value)) + print('') + + ldap = None + + # ------------------------------------------------------------------------- + def get_supported_versions(self, ldap): + + supported_versions = ldap.server.info.supported_ldap_versions + if supported_versions: + if is_sequence(supported_versions): + vlist = map(lambda x: str(x).strip(), supported_versions) + supported_versions = format_list(supported_versions) + else: + supported_versions = str(supported_versions).strip() + else: + supported_versions = _("unknown") + + return [supported_versions] + + # ------------------------------------------------------------------------- + def get_naming_contexts(self, ldap): + + name_contexts = ldap.server.info.naming_contexts + if name_contexts: + if is_sequence(name_contexts): + lst = [] + for context in name_contexts: + lst.append(str(context).strip()) + name_contexts = lst + else: + name_contexts = [name_contexts.strip()] + else: + name_contexts = [_("unknown")] + + return name_contexts + + # ------------------------------------------------------------------------- + def get_schema_entry(self, ldap): + + schema_entry = ldap.server.info.schema_entry + if schema_entry: + if is_sequence(schema_entry): + lst = [] + for entry in schema_entry: + lst.append(str(entry).strip()) + schema_entry = lst + else: + schema_entry = [schema_entry.strip()] + else: + schema_entry = [_("unknown")] + + return schema_entry + + # ------------------------------------------------------------------------- + def get_vendor_name(self, ldap): + + vendor_name = ldap.server.info.vendor_name + if vendor_name: + if is_sequence(vendor_name): + vname = [vendor_name[0].strip()] + else: + vname = [vendor_name.strip()] + else: + vname = [_("unknown")] + + return vname + + # ------------------------------------------------------------------------- + def get_vendor_version(self, ldap): + + vendor_version = ldap.server.info.vendor_version + if vendor_version: + if is_sequence(vendor_version): + vversion = [vendor_version[0].strip()] + else: + vversion = [vendor_version.strip()] + else: + vversion = [_("unknown")] + + return vversion + + +# ============================================================================= + +if __name__ == "__main__": + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 list -- 2.39.5