How to bind to LDAP over SSL using Python

How to bind to LDAP over SSL using Python

Just a quick note (more to myself than anybody else). If you want to bind to an LDAP server (something that I am doing for a big nasty virtualization project), and you want to do it over SSL, there is no documentation in the project manuals. After some quick source code browsing, and a serendipitous find in the sourceforge forums, I have a method. Here it is:

#This is only required if you are using a self signed cert. 
#Probably turn it off for production code.
#ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
#ActiveDirectory? Do this, otherwise, leave it out. Thanks: Deepak Giridharagopal
#ldap.set_option(ldap.OPT_REFERRALS, 0)
lconn=ldap.initialize("ldaps://"+theNameOfYourServerGoesHere)
lconn.simple_bind(userDistinguishedName,userSecretPassword)
#DO whatever: I am changing passwords...
return lconn.passwd_s(theDistinguishedNameOfTheUser,oldpass,newpass)        

This is really terribly important, since the passwd functions will not work unless you are either on TLS or SSL. I am using this method successfully on fedora-ds, but it should also work on OpenLDAP/SlapD.

Home Home
http://www.reaysmoving.com/