使用 Python-LDAP 操作 LD

发布时间:2019-08-26 08:43:44编辑:auto阅读(1733)

    转自:http://www.vpsee.com/

    周末看到那些排队血拼的人们,不用走进 shopping mall、不用看到那些五颜六色的打折和视觉冲击就能感受到 “节日要到了!”。一年又快结束了,这周完成备份、升级之类的收尾工作,接下来就是6周的假期,没啥大安排,假期第1周去南非德班参加高性能计算会议,回来后和家人短途旅行,然后圣诞节在家休息学点新东西,比如修车什么的,几次痛苦经历告诉我出来玩迟早是要坏的,对于 hiking/camping/road trip/4×4 这几个关键字的爱好者来说懂点维修常识是必须的。废话留到假期再说吧,接下来六周可能没有技术方面的博客更新

    最近对 LDAP 服务器上面的数据做处理,有机会接触了一下 Python-LDAP 这个库和 LDAP/Kerberos. 去除所有打印和错误处理的代码后,用 Python-LDAP 操作 LDAP 的骨干代码其实很简单,就这么几行,唯一遇到的一个小麻烦就是折腾了一个多小时才知道 ‘TRUE’ 要大写(后面有说到)。

    安装 Python-LDAP

    在 Ubuntu/Debian 下安装 python-ldap 模块:

    $ sudo apt-get install python-ldap
    

    在 CentOS/RHEL 下安装 python-ldap 模块:

    # yum install python-ldap
    

    创建

    创建一条 LDAP 新纪录。有个要注意的地方,我们的 LDAP 有个属性 active,用来判断用户帐号是否是激活的 attrs['active'] = ‘TRUE’,这里的 ‘TRUE’ 不能用小写的 ‘true’,刚开始被 LDAP 管理工具上的小写 ‘true’ 误导,老以为 Python 程序里也应该用小写,结果总报错。

    phpLDAPadmin

    def ldap_add(firstname, lastname, username):
        l = ldap.open(LDAP_HOST)
        l.protocol_version = ldap.VERSION3
        l.simple_bind(LDAP_BIND, LDAP_PASS)
    
        cn = firstname + ' ' + lastname
        addDN = "cn=%s,ou=People,dc=vpsee,dc=com" % cn
        attrs = {}
        attrs['objectclass'] = ['top','person','inetOrgPerson','posixAccount','vpseeAccount']
        attrs['cn'] = cn
        attrs['givenName'] = firstname
        attrs['homeDirectory'] = '/home/people/%s' % username
        attrs['loginShell'] = '/bin/bash'
        attrs['sn'] = lastname
        attrs['uid'] = username
        attrs['uidNumber'] = ldap_newuid()
        attrs['gidNumber'] = ldap_getgid()
        attrs['active'] = 'TRUE'
        ldif = modlist.addModlist(attrs)
        l.add_s(addDN, ldif)
        l.unbind_s()
    

    查找和读取

    查找和读取一条 LDAP 纪录,比如根据 username 查找出 cn:

    def ldap_getcn(username):
        try:
            l = ldap.open(LDAP_HOST)
            l.protocol_version = ldap.VERSION3
            l.simple_bind(LDAP_BIND, LDAP_PASS)
    
            searchScope = ldap.SCOPE_SUBTREE
            searchFilter = "uid=*" + username + "*"
            resultID = l.search(LDAP_BASE, searchScope, searchFilter, None)
            result_set = []
            while 1:
                result_type, result_data = l.result(resultID, 0)
                if (result_data == []):
                    break
                else:
                    if result_type == ldap.RES_SEARCH_ENTRY:
                        result_set.append(result_data)
            return result_set[0][0][1]['cn'][0]
        except ldap.LDAPError, e:
            print e
    

    更新

    更新一条 LDAP 纪录,比如更新用户状态 active 为 false:

    def ldap_deactive(username):
        try:
            l = ldap.open(LDAP_HOST)
            l.protocol_version = ldap.VERSION3
            l.simple_bind(LDAP_BIND, LDAP_PASS)
    
            deactiveDN = ("cn=%s," + LDAP_BASE) % ldap_getcn(username)
            old = {'active':'TRUE'}
            new = {'active':'FALSE'}
            ldif = modlist.modifyModlist(old, new)
            l.modify_s(deactiveDN, ldif)
            l.unbind_s()
        except ldap.LDAPError, e:
            print e
    

    删除

    删除一条 LDAP 纪录:

    def ldap_delete(username):
        try:
            l = ldap.open(LDAP_HOST)
            l.protocol_version = ldap.VERSION3
            l.simple_bind(LDAP_BIND, LDAP_PASS)
    
            deleteDN = ("cn=%s," + LDAP_BASE) % ldap_getcn(username)
            l.delete_s(deleteDN)
        except ldap.LDAPError, e:
            print e

关键字