from .cfg_app import PpCfgAppError, PpConfigApplication
-__version__ = '0.2.1'
+__version__ = '0.3.1'
LOG = logging.getLogger(__name__)
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*)')
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()
# =============================================================================