from pathlib2 import Path
# Third party modules
+from ldap3 import Server, Connection, DSA, IP_V4_PREFERRED, SAFE_SYNC
+# from ldap3 import ALL
+# from ldap3 import BASE, LEVEL, SUBTREE, DEREF_NEVER, DEREF_SEARCH, DEREF_BASE, DEREF_ALWAYS
+# from ldap3 import ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES
+# from ldap3 import MODIFY_ADD, MODIFY_DELETE, MODIFY_REPLACE
+# from ldap3.core.exceptions import LDAPInvalidDnError, LDAPInvalidValueError
+# from ldap3.core.exceptions import LDAPException, LDAPBindError
+
from fb_tools.common import pp, to_bool
from fb_tools.cfg_app import FbConfigApplication
from fb_tools.errors import FbAppError
# rom ..config.ldap import DEFAULT_PORT_LDAP, DEFAULT_PORT_LDAPS
from ..config.ldap import DEFAULT_TIMEOUT, MAX_TIMEOUT
-__version__ = '0.2.2'
+__version__ = '0.3.1'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
self._password_file = None
self.ldap_instances = []
self._yes = False
+ self.ldap_server = {}
+ self.ldap_connection = {}
super(BaseLdapApplication, self).__init__(
appname=appname, verbose=verbose, version=version, base_dir=base_dir,
if v:
default_connection.bind_pw = v
+ # -------------------------------------------------------------------------
+ def __del__(self):
+
+ self.disconnect_all()
+
+ # -------------------------------------------------------------------------
+ def pre_run(self):
+
+ LOG.debug(_("Preparations ..."))
+ super(BaseLdapApplication, self).pre_run()
+
+ LOG.debug(_("Open all necessary LDAP connections ..."))
+
+ for inst in self.ldap_instances:
+ self.connect_instance(inst)
+
+ # -------------------------------------------------------------------------
+ def connect_instance(self, inst):
+
+ connect_info = self.cfg.ldap_connection[inst]
+
+ if self.verbose > 1:
+ LOG.debug(_("Connecting to LDAP server {} ...").format(connect_info.url))
+
+ server_opts = {}
+ if connect_info.use_ldaps:
+ server_opts['use_ssl'] = True
+ if connect_info.port != 636:
+ server_opts['port'] = connect_info.port
+ else:
+ server_opts['use_ssl'] = False
+ if connect_info.port != 389:
+ server_opts['port'] = connect_info.port
+ server_opts['get_info'] = DSA
+ server_opts['mode'] = IP_V4_PREFERRED
+ server_opts['connect_timeout'] = self.cfg.ldap_timeout
+ if self.verbose > 1:
+ msg = _("Connect options to server {!r}:").format(connect_info.url)
+ msg += ' ' + pp(server_opts)
+ LOG.debug(msg)
+
+ ldap_server = Server(connect_info.host, **server_opts)
+ self.ldap_server[inst] = ldap_server
+
+ if self.verbose > 2:
+ LOG.debug(_("LDAP server {s}: {re}").format(s=ldap_server, re=repr(ldap_server)))
+
+ ldap_connection = Connection(
+ ldap_server, connect_info.bind_dn, connect_info.bind_pw,
+ client_strategy=SAFE_SYNC, auto_bind=True)
+ self.ldap_connection[inst] = ldap_connection
+
+ if self.verbose > 2:
+ msg = _("Info about LDAP server {}:").format(connect_info.url)
+ msg += ' ' + repr(ldap_connection)
+ LOG.debug(msg)
+
+ # -------------------------------------------------------------------------
+ def post_run(self):
+
+ LOG.debug(_("Finishing ..."))
+ super(BaseLdapApplication, self).post_run()
+
+ self.disconnect_all()
+
+ # -------------------------------------------------------------------------
+ def disconnect_all(self):
+
+ if len(self.ldap_connection) or len(self.ldap_server):
+ LOG.debug(_("Disconnecting all remaining LDAP instances ..."))
+
+ for inst in self.ldap_instances:
+ self.disconnect_instance(inst)
+
+ # -------------------------------------------------------------------------
+ def disconnect_instance(self, inst):
+
+ connect_info = self.cfg.ldap_connection[inst]
+
+ if inst in self.ldap_connection:
+ ldap_connection = self.ldap_connection[inst]
+ LOG.debug(_("Unbinding from LDAP server {!r} ...").format(connect_info.url))
+ ldap_connection.unbind()
+ ldap_connection = None
+ del self.ldap_connection[inst]
+
+ if inst in self.ldap_server:
+ LOG.debug(_("Disconnecting from LDAP server {!r} ...").format(connect_info.url))
+ del self.ldap_server[inst]
+
# =============================================================================
if __name__ == "__main__":