views:

382

answers:

1

I want to connect to a ldap server with python-ldap using a specific baseDN.

import ldap

baseDN="ou=unit,o=org.c=xx" # doesn't work
#baseDN="" # works
host="ldaps://test.org.xx:636"
userDN="cn=proxyhlrb,ou=services,o=org,c=xx"
passwd="secret"

server=ldap.initialize(host+"/"+baseDN)

server.bind_s(userDN,passwd,ldap.AUTH_SIMPLE)

What is wrong here? According to the documentation the argument of ldap.initialize must be a valid LDAP URL according to RFC4516 and therefore using a host+baseDN should work. Is there another way to specify a baseDN in python-ldap?

A: 

I think this resource would be interesting for you. It nicely explains how to combine LDAP with Python.

http://www.packtpub.com/article/python-ldap-applications-ldap-opearations

Edit: is the port you are using correct? In PHP, developers mainly use port 389 for LDAP connects, bindings and queries.

Ben Fransen
I know the source you are recommending. It does explain how to use a base DN for searching, but not how to use a general base DN for the connection. The port 636 is the default port for ldaps!
asmaier
Doen't it works kinda the same? You connect to your server, you bind with valid credentials or anonymous. If the binding succeeds you can search the ldap server and use your baseDN for filtering the results to your needs?
Ben Fransen
It is kinda the same, but very inconvenient. I would have to change every search call in my code using a searchDN like searchDN="ou=persons,"+baseDN or searchDN="ou=accounts,"+baseDN and so on. I thought there should be a better way...
asmaier
Correct me if I'm wrong, but that's a thing you always need to do. Just like when you're quering your database, you have to specify which table and which columns you want to retreive. Try creating a configfile where you store things like baseDN, OU, etc. in constants or variables when you need to change them in specific searchcalls.
Ben Fransen
I just though that there might be a standard (but possibly undocumented) option which is used by ldap.search and I could set like ldap.setoption(ldap.BASEDN,"ou=unit,o=org.c=xx"), so that I do not have to invent my own variable name and store them in my own configfile. But if such an predefined variable does not exist, I agree with you, that I have to do this myself.
asmaier
I'm now using a baseDN to filter my searches as you suggested, because there really seems to be no other way to specify a baseDN. So I agree that your answer is the correct one.
asmaier
...and the question remains open :(
Isaac