From 3ca301a033c22c03341f87c0d0899bce30c45279 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 10 May 2023 15:53:09 +0200 Subject: [PATCH] Adding method get_all_entries() to lib/pp_admintools/app/ldap.py --- lib/pp_admintools/app/ldap.py | 64 ++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/lib/pp_admintools/app/ldap.py b/lib/pp_admintools/app/ldap.py index d19ef19..23dd5f8 100644 --- a/lib/pp_admintools/app/ldap.py +++ b/lib/pp_admintools/app/ldap.py @@ -54,7 +54,7 @@ from ..config.ldap import LdapConnectionInfo, LdapConfiguration # rom ..config.ldap import DEFAULT_PORT_LDAP, DEFAULT_PORT_LDAPS from ..config.ldap import DEFAULT_TIMEOUT -__version__ = '0.11.1' +__version__ = '0.11.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -578,6 +578,9 @@ class BaseLdapApplication(BaseDPXApplication): continue filtered_instances.append(inst.lower()) + if self.verbose > 2: + LOG.debug(_("Filtered instances:") + ' ' + pp(filtered_instances)) + self._validate_given_instances(filtered_instances) if self.verbose > 1: @@ -770,6 +773,64 @@ class BaseLdapApplication(BaseDPXApplication): LOG.debug(_("Disconnecting from LDAP server {!r} ...").format(connect_info.url)) del self.ldap_server[inst] + # ------------------------------------------------------------------------- + def get_all_entries(self, inst, base_dn=None, ldap_filter=None, attributes=None): + """Get all LDAP entries bellow the given BaseDN and the given LDAP filter. + If no attributes are given, all attributes are given back. + The result is a hash with the DNs if the resulting entries as keys, and a hash + with the resulting attributes as values. + """ + connect_info = self.cfg.ldap_connection[inst] + ldap = self.ldap_connection[inst] + + result = {} + + if not base_dn: + base_dn = connect_info.base_dn + if attributes is None: + attributes = [ALL_ATTRIBUTES] + if ldap_filter is None: + ldap_filter = '(objectClass=*)' + + if self.verbose > 2: + msg = _( + "Searching in {uri}/{bdn} for all entries with filter {fltr!r}, " + "giving attributes:").format(uri=connect_info.url, bdn=base_dn, fltr=ldap_filter) + msg += ' ' + format_list(attributes, do_repr=True) + LOG.debug(msg) + + req_status, req_result, req_response, req_whatever = ldap.search( + search_base=base_dn, search_scope=SUBTREE, attributes=attributes, + search_filter=ldap_filter, time_limit=self.cfg.ldap_timeout) + + if req_status: + if self.verbose > 4: + LOG.debug(_("Result of searching:") + '\n' + pp(req_result)) + + for entry in req_response: + dn = entry['dn'] + if self.verbose > 3: + LOG.debug(_("Found entry {!r}.").format(dn)) + result[dn] = self.normalized_attributes(entry) + + if self.verbose > 2: + msg = ngettext( + "Found one entry with filter {fltr!r} in {uri}/{bdn}.", + "Found {nr} enries with filter {fltr!r} in {uri}/{bdn}.", + len(result)).format(nr=len(result), uri=connect_info.url, + bdn=base_dn, fltr=ldap_filter) + LOG.debug(msg) + if self.verbose > 4: + LOG.debug(_("Got response entries:") + '\n' + pp(result)) + + else: + if self.verbose > 3: + msg = _("No entry found with filter {fltr!r} in {uri}/{bdn}.").format( + uri=connect_info.url, bdn=base_dn, fltr=ldap_filter) + LOG.debug(msg) + + return result + # ------------------------------------------------------------------------- def get_all_entry_dns(self, inst, ldap_filter=None): """Get DNs of all entries in the given LDAP instance and sort them.""" @@ -779,6 +840,7 @@ class BaseLdapApplication(BaseDPXApplication): ldap = self.ldap_connection[inst] result = [] + attributes = ['dn'] if not ldap_filter: ldap_filter = '(objectClass=*)' -- 2.39.5