Background: I am working on an API to centralize the creation and management of users for several resources (e.g. Google Apps, Dropbox, etc.). On a Linux virtual machine, I developed an API and a web interface that allows me (and my employees) to authenticate and manage user accounts for these services. The next thing I need for integration is our Active Directory hosted on a remote Windows Server 2008.
I am trying to use python-ldap to connect and retrieve / modify information, but you had problems with DIR_ERROR operation errors (when trying to query for users) and NAMING_VIOLATION errors (when trying to add users).
* Code based on http://www.grotan.com/ldap/python-ldap-samples.html , stack questions and documentation python-ldap Binding code, which I believe works:
import ldap try: l = ldap.open("serverip") l.protocol_version = ldap.VERSION3 username = " myUserName@adtest.local " password = "secret" result = l.simple_bind(username, password) print result except ldap.LDAPError, e: print e
which prints: (97, [], 1, [])
The request for users is script: (I tried without binding, as suggested in the article, but got "To complete this operation, successful binding must be completed in the connection.")
import ldap try: l = ldap.open("serverIp", port=389) l.protocol_version = ldap.VERSION3 username = " myUserName@adtest.local " password = "secret" result = l.simple_bind(username, password) print result except ldap.LDAPError, e: print e # handle error however you like baseDN = "ou=Users, o=adtest.local" searchScope = ldap.SCOPE_SUBTREE retrieveAttributes = None searchFilter = "cn=*myUserName*" try: ldap_result_id = l.search(baseDN, searchScope, searchFilter, retrieveAttributes) result_set = [] while 1: result_type, result_data = l.result(ldap_result_id, 0) if (result_data == []): break else: if result_type == ldap.RES_SEARCH_ENTRY: result_set.append(result_data) print result_set except ldap.LDAPError, e: print e
which leads to the following: (97, [], 1, []) {'info': '000020D6: SvcErr: DSID-031007DB, problem 5012 (DIR_ERROR), data 0 \ n', 'desc': 'Operation error' }
Add user script :( using ldaps)
import ldap import ldap.modlist as modlist
The result is: ldap.SERVER_DOWN: {'info': 'Received TLS packet with unexpected length.', 'Desc': "Unable to contact the LDAP server"}
* It made me think that the port might be a problem, so I changed the initialization line to l = ldap.initialize("ldap://serverIp:389/") , similar to the other two scripts.
Now I get: ldap.NAMING_VIOLATION: {'info': "00002099: NameErr: DSID-0305109C, issue 2005 (NAMING_VIOLATION), data 0, best match: \ n \ t'dc = adtest, dc = local '\ n" , 'desc': 'Naming violation'}
In addition, I came across adding uu and uid to attrs, but with no change in error.
What am I doing wrong or what can I do differently? Thanks for any help / suggestions!
edit: I checked my server and port 636 is configured correctly to allow Secure LDAP traffic, so I donβt know why this gave me different errors than regular LDAP. edit2: I tried changing the next line in my add script dn = "cn = test, dc = adtest.local"
and I have a new output (stack trace) (I added a print expression in to show that the binding is actually happening now before the error):
(97, [], 1, [])
Traceback (last last call):
File "test2.py", line 21, in <module>
l.add_s (dp, LDIF)
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 202, in add_s
return self.result (msgid, all = 1, timeout = self.timeout)
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 465, resulting
resp_type, resp_data, resp_msgid = self.result2 (msgid, all, timeout)
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 469, in result2
resp_type, resp_data, resp_msgid, resp_ctrls = self.result3 (msgid, all, timeout)
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 476, in result3
resp_ctrl_classes = resp_ctrl_classes
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 483, in result4
ldap_result = self._ldap_call (self._l.result4, msgid, all, timeout, add_ctrls, add_intermediates, add_extop)
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 106, in _ldap_call
result = func (* args, ** kwargs)
ldap.REFERRAL: {'info': 'Referral: \ nldap: //adtest.local/cn=test,dc=adtest.local', 'desc': 'Referral'}