]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
First successful searches
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 17 Mar 2017 16:04:51 +0000 (17:04 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 17 Mar 2017 16:04:51 +0000 (17:04 +0100)
mk-home
pp_lib/ldap_app.py

diff --git a/mk-home b/mk-home
index 12f5ccab7ddb10b76e1739a37baf9a0b4dbd2815..fe02a25f850c3a6a8829807b55a848df8afaaf91 100755 (executable)
--- 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)
 
index 5e828f4e56854a1eaa787352ae98e08b97440ab0..e840fb7edf27dfa1029349ef6385e0165d751dad 100644 (file)
@@ -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()
 
 # =============================================================================