Here's an Ldap class using Ldap module.
Installing Python-LDAP requires only one commnad:
$ sudo apt-get install python-ldap
#!/usr/bin/python
import ldap
import ldap.modlist as modlist
import sys
class myLDAPClass:
def __init__(self):
self.result_count = 0
def simpleAuthentication(self, server, admin_dn, pw):
try:
# Open a connection
self.l = ldap.initialize(server)
# you shoud set this to ldap.VERSION2 if you're using a Version2 directory
self.l.protocol_version = ldap.VERSION3
# Pass in a valid username and password to get
# privileged directory access.
# If you leave them as empty strings or pass an invalid value
# you will still bind to the server but with limited privileges.
self.admin_dn = admin_dn
self.pw = pw
# Any errors will throw an ldap.LDAPError exception
# or related exception so you can ignore the result
if (self.l.simple_bind(self.admin_dn, self.pw)):
print "Connected, simple bind, to Ldap Server ..."
else:
print "Could not connect to Ldap Server."
sys.exit()
except ldap.LDAPError, e:
print e
def close(self):
self.l.unbind_s()
print "Disconnected, unbind, to Ldap Server ..."
def addEntry(self, dn, entry):
# convert our dictionary to nice syntax for the add-function using modlist-module
ldif = modlist.addModlist(entry)
# Do the actual synchronous add-operation to the ldapserver
self.l.add_s(dn, ldif)
def delEntry(self, dn):
try:
# you can safely ignore the results returned as an exception
# will be raised if the delete operation doesn't work
self.l.delete_s(dn)
except ldap.LDAPError, e:
print e
def search(self, base_dn, scope, search_filter, retrieve_attributes):
self.result_count = 0
try:
ldap_result_id = self.l.search(base_dn, scope, search_filter, retrieve_attributes)
self.result_set = []
while 1:
result_type, result_data = self.l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
self.result_set.append(result_data)
# counting total of data
self.result_count = self.result_count + 1
#print self.result_set
except ldap.LDAPError, e:
print "No hay registro en LDAP server : ",e
self.result_count = 0
if __name__ == "__main__":
# here's an example of how to use this class.
myldap = myLDAPClass()
myldap.simpleAuthentication('ldap://localhost','o=organization,dc=gov.ar','secret')
myldap.search('cn=name surname,ou=people,o=organization,dc=org', ldap.SCOPE_SUBTREE, '(objectclass=*)', ['employeeNumber'])
# it use a dictionary to add an entry in Ldap Server
entry={'objectClass':['top','person','organizationalPerson','inetOrgPerson'],'cn':['name surname'],'sn':['surname'], 'employeeNumber':['9999'],'mail':['test@change.com'],'uid':['1111'],'userPassword':['123456']}
dn='cn=name surname,ou=people,o=organization,dc=org'
myldap.addEntry(dn, entry)
myldap.close()
References:
http://www.grotan.com/ldap/python-ldap-samples.html
http://python-ldap.sourceforge.net/doc/html/index.html
http://www.linuxjournal.com/article/6988
http://python-ldap.sourceforge.net/apps.shtml
http://www.iaeste.or.at/doc/python-ldap-doc/html/node3.html
http://www.packtpub.com/article/installing-and-configuring-the-python-ldap-library-and-binding-to-an-ldap-directory
Thursday, October 30, 2008
Python using Ldap Module
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment