From 81c079c69e266806cf0959f3e5dc553e98e475be Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 17 Mar 2017 17:04:51 +0100 Subject: [PATCH] First successful searches --- mk-home | 2 +- pp_lib/ldap_app.py | 83 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/mk-home b/mk-home index 12f5cca..fe02a25 100755 --- a/mk-home +++ b/mk-home @@ -34,7 +34,7 @@ app.initialized = True if app.verbose > 2: print("{c}-Object:\n{a}".format(c=app.__class__.__name__, a=app)) -#app() +app() sys.exit(0) diff --git a/pp_lib/ldap_app.py b/pp_lib/ldap_app.py index 5e828f4..e840fb7 100644 --- a/pp_lib/ldap_app.py +++ b/pp_lib/ldap_app.py @@ -34,7 +34,7 @@ from .merge import merge_structure from .cfg_app import PpCfgAppError, PpConfigApplication -__version__ = '0.2.1' +__version__ = '0.3.1' LOG = logging.getLogger(__name__) @@ -62,7 +62,7 @@ class PpLdapApplication(PpConfigApplication): default_ldap_base_dn = 'o=isp' default_ldap_bind_dn = 'uid=Solaris_NSS,ou=Unix NSS,ou=Applications,o=pixelpark,o=isp' - default_ldap_timeout = 10 + default_ldap_timeout = 30 fs_re = re.compile(r'(?:\s+|\s*[,;]\s*)') @@ -201,6 +201,85 @@ class PpLdapApplication(PpConfigApplication): msg = "No LDAP servers found in configuration." raise PpLdapAppError(msg) + # Init LDAP connection object + self.ldap_connection = ldap3.Connection( + self.ldap_server, user=self.ldap_bind_dn, password=self.ldap_bind_pw, + auto_bind=ldap3.AUTO_BIND_NONE, lazy=True, auto_range=True + ) + + # ------------------------------------------------------------------------- + def pre_run(self): + """ + Dummy function to run before the main routine. + Could be overwritten by descendant classes. + + """ + + if self.verbose > 1: + LOG.debug("executing pre_run() ...") + + LOG.debug("Binding to the LDAP servers ...") + self.ldap_connection.bind() + + # ------------------------------------------------------------------------- + def _run(self): + """ + Dummy function as main routine. + + MUST be overwritten by descendant classes. + + """ + + LOG.debug("Executing something ...") + + query_filter = ( + '(&' + '(objectclass=posixAccount)' + '(objectclass=shadowAccount)' + '(uid=frank.brehm)' + ')' + ) + + entries = self.ldap_search(query_filter) + + print("Found {} LDAP entries.".format(len(entries))) + i = 0 + for entry in entries: + i += 1 + print("\n{}".format(entry)) + if i >= 5: + break + + # ------------------------------------------------------------------------- + def ldap_search( + self, query_filter, dn=None, attributes=ldap3.ALL_ATTRIBUTES, + scope=ldap3.SUBTREE): + + if self.verbose > 1: + LOG.debug("Query string: {q!r}, attributes: {a}".format( + q=query_filter, a=pp(attributes))) + + if dn is None: + dn = self.ldap_base_dn + + self.ldap_connection.search( + dn, query_filter, search_scope=scope, attributes=attributes) + entries = self.ldap_connection.entries + return entries + + # ------------------------------------------------------------------------- + def post_run(self): + """ + Dummy function to run after the main routine. + Could be overwritten by descendant classes. + + """ + + if self.verbose > 1: + LOG.debug("executing post_run() ...") + + LOG.debug("Unbinding from the LDAP servers ...") + self.ldap_connection.unbind() # ============================================================================= -- 2.39.5