Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I search for an object in LDAP based on its dn, in python-ldap?

I am trying to use e.g. the search_s function to search for an object based on its full distinguished name, but am not finding this to be convenient. For example,

search_s('DC=example, DC=com', ldap.SCOPE_SUBTREE,
    '(CN=Somebody, OU=Department, DC=example, DC=com)')

How do I just retrieve one object based on its full LDAP distinguished name?

like image 579
joeforker Avatar asked Jan 18 '11 22:01

joeforker


People also ask

What is LDAP search DN?

Search DN - An LDAP directory is organized as a tree structure, with a root node and a number of branches off this root. The Search DN specifies at which node the search originates. Entries greater than this level in the tree are searched. You must specify the correct base DN to obtain the results you want.

How do I use LDAP authentication in Python?

In order to use LDAP with Python we need to import the Server and the Connection object, and any additional constant we will use in our LDAP. As you might remember from the LDAP Protocol diagram the authentication operation is called Bind.

What is DN in Python?

dn : a string containing the DN (distinguished name) of the entry. attributes : a dictionary of returned attributes and their values. Values are list. Values are in UTF-8 format.


1 Answers

Use SCOPE_BASE and a wildcard filter to return only the dn given by the first argument (the filter still has to match that object!) For example,

import ldap
...
ldap_connection.search_s('CN=Somebody, OU=Department, DC=example, DC=com',
    ldap.SCOPE_BASE,
    '(objectClass=*)')
like image 62
joeforker Avatar answered Sep 26 '22 08:09

joeforker