how to read attributes for given DN in ldap3 (how to search with ldap3 if no filter)

If I already have an LDAP DN, how do I get the attributes for that DN with ldap3.Connection.search()? There is no other search criteria, I already have the DN...

I tried searching for dn attribute, but it returned no objects found. I also tried forcing search_filter to '', '()' or None and they all returned malformed filter string.

I also couldn't find a way to do this with the abstract Reader...

In ldapsearch you don't need to specify a search filter if you are doing a baseDN lookup...

import ldap3

ldap_conn = ldap3.Connection('ldapserver', raise_exceptions=True, 
    auto_bind=True, user='me', password='mypassword')

my_dn = "attrib1=blahblah, ou=org1, dc=dc1, dc=dcroot"

ldap_conn.search(
    search_base=my_dn,
    search_filter= '(????)', # required
    search_scope=ldap3.BASE,
    attributes='*'
)

print ldap_conn.response

1 answer

  • answered 2017-12-06 01:18 cowbert

    I just realized that objectClass will always be present, so setting it to wildcard should shim search_filter to return the 1 entry associated with base DN:

    ldap_conn.search(
        search_base=my_dn,
        search_filter= '(objectClass=*)', # required
        search_scope=ldap3.BASE,
        attributes='*'
    )
    

    However it seems silly there is no special case for LOOKUP operation against the connection given a DN in ldap3.