简体   繁体   English

尝试使用python-ldap模块使用TLS连接到LDAP服务器

[英]Trying to connect to an LDAP server with TLS using python-ldap module

Connection to LDAP server fails through TLS connection 通过TLS连接到LDAP服务器的连接失败

I am using Python 2.7 ldap module, and have tried connecting to an LDAP server with TLS enabled, but so far I have only run into many issues. 我正在使用Python 2.7 ldap模块,并尝试连接到启用了TLS的LDAP服务器,但是到目前为止,我只遇到了很多问题。 When trying to debug the issue I get very little information back. 当尝试调试问题时,我得到的信息很少。 Here is a simple script that I am testing with below 这是我正在下面测试的简单脚本

import ldap

LDAP_SERVER = 'ldap://ldap.somedomain.com:389'
LDAP_BASE = 'ou=users,dc=ldap,dc=test,dc=com'

try:
    conn = ldap.initialize(LDAP_SERVER, bytes_mode=False)
    conn.set_option(ldap.OPT_REFERRALS, 0)
    conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
    conn.set_option(ldap.OPT_X_TLS_CACERTFILE, "/path/to/cacert.pem")
    conn.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
    conn.set_option(ldap.OPT_X_TLS_DEMAND, True)
    conn.set_option(ldap.OPT_DEBUG_LEVEL, 255)
    conn.start_tls_s()
except ldap.LDAPError, e:
    print e
    raise

print 'done'

When testing the script, an Exception is raised when the conn.start_tls_s line is executed. 测试脚本时,在执行conn.start_tls_s行时会引发异常。 here is the error that is returned: 这是返回的错误:

ldap.CONNECT_ERROR: {'info': u'(unknown error code)', 'errno': 2, 'desc': u'Connect error'}

Stack Trace: 堆栈跟踪:

File "/home/eric/Desktop/test_ldap.py", line 14, in <module>
    conn.start_tls_s()
  File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 864, in start_tls_s
    return self._ldap_call(self._l.start_tls_s)
  File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 329, in _ldap_call
    reraise(exc_type, exc_value, exc_traceback)
  File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 313, in _ldap_call
    result = func(*args,**kwargs)

Note - Port 389 on my LDAP server is secure and accepts TLS connections, which is why the LDAP_SERVER is not set to ldaps://ldap.somedomain.com:636 in the example above. 注意-我的LDAP服务器上的端口389是安全的,并且接受TLS连接,这就是为什么在上例中LDAP_SERVER未设置为ldaps://ldap.somedomain.com:636的原因。

I am also using an Ubuntu 14.04 virtual machine. 我也在使用Ubuntu 14.04虚拟机。 Any ideas, and tips are greatly appreciated. 任何想法和技巧,将不胜感激。

Okay, so basically I needed to do a couple of things. 好的,所以基本上我需要做几件事。 I had to add a copy of the cacert.pem file, then edit an ldap.conf file, and then I was finally able to connect to the LDAP server with TLS. 我必须添加cacert.pem文件的副本,然后编辑ldap.conf文件,然后终于能够使用TLS连接到LDAP服务器。

If you do not have a cacert.pem file, then you may need to make one, or ask a network admin to get one if you don't know how. 如果您没有cacert.pem文件,则可能需要制作一个文件,或者如果您不知道怎么做,可以请网络管理员获取一个文件。 See the link below for details on Using Certificates: http://www.openldap.org/faq/data/cache/185.html 有关使用证书的详细信息,请参见下面的链接: http : //www.openldap.org/faq/data/cache/185.html

Since python-ldap is based on OpenLdap, then you may need to install some dependencies as stated here: I can't install python-ldap 由于python-ldap基于OpenLdap,因此您可能需要安装一些依赖项,如下所示: 我无法安装python-ldap

If ldap is installed on Ubuntu, then you should see a file structure like this: 如果ldap安装在Ubuntu上,那么您应该会看到类似以下的文件结构:

/etc/ldap
    ldap.conf sasl2 schema slapd.d

I made a new directory in ldap called cacert , then added the cacert.pem file in there like so: /etc/ldap/cacert/cacert.pem 我在ldap cacert了一个名为cacert的新目录,然后在其中添加cacert.pem文件,如下所示: /etc/ldap/cacert/cacert.pem

Then I edited ldap.conf with these changes below: 然后,我对ldap.conf进行了以下更改:

TLS_CACERT      /etc/ldap/cacert/cacert.pem
TLS_REQCERT     allow

After saving the ldap.conf, I tested the connection by entering the following in the terminal, and was finally able to connect and get results back from the server: 保存ldap.conf之后,我通过在终端中输入以下命令测试了连接,最终能够连接并从服务器获取结果:

ldapsearch -H ldap://ldap.yourdomain.com -Z -x -D "cn=admin,dc=ldap,dc=test,dc=com" -w "P@ssWerd2LD@pP" -b "dc=ldap,dc=test,dc=com" uid=*

Then I tested my python script and was able to run it without raising any exceptions after starting TLS I hope this helps anyone else that is having trouble connecting to LDAP with TLS on Ubuntu 然后我测试了我的python脚本,并在启动TLS后能够在不引发任何异常的情况下运行它,希望这对在Ubuntu上使用TLS连接到LDAP遇到问题的其他人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM