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.