【发布时间】:2015-12-09 21:35:13
【问题描述】:
我能够通过 python-ldap 绑定和查询 Active Directory,除了在 AD 上添加或修改属性时,没有任何问题。我可以添加属性,但由于所有文本都是乱码,因此编码似乎有问题。
我尝试使用 utf8 和其他一些方法对我的字符串进行编码,但没有成功。
我还尝试与域管理员帐户绑定以及与我将更改属性的用户帐户绑定,但结果相同。
这是我用来更新属性的方法:
类 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
通过终端进行 ldapsearch 属性如下所示:
mobile:: MC8sAQAAAAAQNA==
当我将手机设置为“Hello World”时,这就是它的样子:
mobile:: 77+9ehsCAAAAABDvv70V
我查看了 MSDN,它说 ldap 属性只是一个 Unicode 字符串。
系统:Ubuntu 15.10 64bit 蟒蛇:2.7.10 python-ldap==2.4.21
作为旁注,我可以毫无问题地搜索 AD 并解析/显示返回的用户属性,问题似乎只在于创建或修改此编码问题所涉及的属性。
【问题讨论】:
-
你能检查一下
tcpflow -c port 389发送的查询吗? -
这里是上面修改查询的转储:tcpflow -c port 389 tcpflow: listener on eth0 010.001.200.029.54760-010.000.000.039.00389: 0C`>-CN=Administrator,CN=用户,DC=公司,DC=com 酷密码 010.000.000.039.00389-010.001.200.029.54760:0a 010.001.200.029.54760-010.000.000.039.00389:0[fV4CN=Jassen Michaels,OU=company,DC DC=com00 pmobile1 010.000.000.039.00389-010.001.200.029.54760: 0g
标签: python python-2.7 python-ldap