Python LDAP Entry Attribute for Active Directory

I can bind and query Active Directory through python-ldap without any problems, except when it comes to adding or changing attributes in AD. I can add an attribute, but the encoding seems to be deleted, because all the text is distorted.

I tried to encode my string with utf8 and several others without getting any luck.

I also tried to associate with the domain administrator account, as well as binding to the user account to which I will change the attribute, no matter what the result.

Here is the method I use to update the attribute:

class LdapHelpers:

def __init__(self): import ldap # set globals self.server = 'LDAP://dc.mycompany.com' self.admin_dn = 'CN=Administrator,CN=users,DC=mycompany,DC=com' self.admin_pass = 'coolpassword' # init LDAP connection #ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0) ldap.set_option(ldap.OPT_REFERRALS, 0) ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) ldap.protocol_version = ldap.VERSION3 self.ldap = ldap.initialize(self.server) def update_attribute(self, attrib, value): try: import ldap conn = self.ldap conn.simple_bind_s(self.admin_dn, self.admin_pass) mod_attrs = [( ldap.MOD_REPLACE, "mobile", "6306564123")] # I have tried other variations of the above # mod_attrs = [( ldap.MOD_REPLACE, "mobile", "6306564123".encode('utf-8)] conn.modify_s('CN=Mike Smith,OU=GoogleApps,DC=company,DC=com', mod_attrs) print 'record updated' except ldap.LDAPError as e: return e.message 

Running ldapsearch through the terminal, it looks like this:

 mobile:: MC8sAQAAAAAQNA== 

This is what β€œHello World” looks like when I establish a mobile connection for it:

 mobile:: 77+9ehsCAAAAABDvv70V 

I checked the MSDN and said that the ldap attribute is just a Unicode string.

System: Ubuntu 15.10 64bit Python: 2.7.10 Python-LDAP == 2.4.21

As a side note, I can search for AD without any problems, and the parse / display attributes return custom attributes, the only problem is that they create or modify the attributes to which this encoding problem comes into play.

+6
source share
3 answers

Well, I found out what was going on, I used PyPy 4.0.1 as an interpreter, and for some reason this caused problems with the python-ldap library and / or encoding for strings.

I switched to Python 2.7.10 for the interpreter, and now the same modification commands above work as expected using the python-ldap library. So definitely a word of caution when using PyPy and this particular library ....

0
source

The "=" at the end is often an indicator that it is Base64 . Python has a standard base64 encoding / decoding library (the link is for Python 3, but Python 2 also has a library). LDAP really uses Base64 for something. See LDAP Interchange Format (LDIF) .

0
source

Take a look at the code from pyad to clarify what to do: https://pypi.python.org/pypi/pyad

It is based on Python.

Another example of the answer already answered: Use a Python script to manage a remote LDAP server

0
source

All Articles