Python+LDAP+SSL(Python+LDAP+SSL)
问题描述
美好的一天.
提前为我的英语道歉,我的国家论坛和资源没有帮助.
In advance to apologize for my English, my national forums and resources did not help.
脚本中需要在 AD 中更改(或创建)用户密码.
There was a need in the script that changes (or creates) a user password in AD.
研究这个问题后,很明显
After studying the issue, it became clear that
- 分配或更改的密码只能与服务器建立加密连接
- 发送密码只需要编码utf-16-le
一般第二个没有问题,但是第一个有问题
In general there is no problem with the second, but first have a problem with
$ python ldap-test-starttls.py
Traceback (most recent call last):
File "ldap-test-starttls.py", line 9, in <module>
l.simple_bind_s( "cn=admin,ou=users,dc=test,dc=ru", "password" )
File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 206, in simple_bind_s
msgid = self.simple_bind(who,cred,serverctrls,clientctrls)
File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 200, in simple_bind
return self._ldap_call(self._l.simple_bind,who,cred,EncodeControlTuples(serverctrls),EncodeControlTuples(clientctrls))
File "/usr/lib/python2.7/dist-packages/ldap/ldapobject.py", line 96, in _ldap_call
result = func(*args,**kwargs)
ldap.SERVER_DOWN: {'info': 'A TLS packet with unexpected length was received.', 'desc': "Can't contact LDAP server"}
脚本代码
import ldap
host = 'ldaps://ldap:636'
l = ldap.initialize(host)
l.set_option( ldap.OPT_X_TLS_DEMAND, True )
l.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
username = 'someUser'
new_pass = 'ne$wP4assw0rd3!'
new_password = ('"%s"' % new_pass).encode("utf-16-le")
l.simple_bind_s( "cn=admin,ou=users,dc=test,dc=ru", "password" )
mod_attrs = [(ldap.MOD_REPLACE, 'unicodePwd', new_password)],[( ldap.MOD_REPLACE, 'unicodePwd', new_password)]
l.modify_s('CN=%s,dc=users,dc=test,dc=ru' % username, mod_attrs)
l.unbind_s()
print "Successfully changed password."
可能有人已经解决了类似的问题.是的,脚本在 Centos 上运行,不能使用 py32win.
Chances are someone has already solved a similar problem. Yes, the script is running on Centos and use py32win not possible.
提前致谢.
推荐答案
在深入研究之后,我想出了一个解决方案:
After looking into it more I was able to come up with a solution:
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
l = ldap.initialize("ldaps://ldap:636")
l.set_option(ldap.OPT_REFERRALS, 0)
l.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
l.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
l.set_option( ldap.OPT_X_TLS_DEMAND, True )
l.set_option( ldap.OPT_DEBUG_LEVEL, 255 )
l.simple_bind_s("admin@tester.com","password")
这篇关于Python+LDAP+SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!