7

我正在使用pythonldap 模块连接到ldap 服务器。我可以查询数据库但我不知道如何检索数据库中存在的字段,以便我可以提前通知用户查询数据库,告诉他他试图访问的字段不在数据库中.

例如,如果存在的字段只是

cn
memberOf

如果用户尝试使用过滤器查询数据库

cn and memberOf and notcontained

我应该能够知道notcontained属性不在 dabase 模式中。

我怎样才能做到这一点。

谢谢。

4

3 回答 3

8

您需要阅读 ldap 服务器的架构。

这段代码可能对你有用,就像模板一样

#!/usr/bin/env python
#coding:utf-8
# Author:  peter --<pjl@hpc.com.py>
# Purpose: Tareas comunes a utilizar con respecto a schemas ldap
# Created: 01/05/12
import ldap
import ldap.schema



########################################################################
class SchemasIPA(object):

    __ldaps = ldap.schema

    #----------------------------------------------------------------------
    def __init__(self, url):
        """Constructor"""
        ldap._trace_level = 0
        ldap.set_option(ldap.OPT_DEBUG_LEVEL,0)
        subschemasubentry_dn, self.schema = ldap.schema.urlfetch(url,ldap._trace_level)
        self.oc_tree = self.schema.tree(ldap.schema.ObjectClass)        
        self.at_tree = self.schema.tree(ldap.schema.AttributeType)        

    def getobjectclasses(self):
        """
        trae la listas de objectclasses de un servidor dado
        """
        allobjc = {}
        for a in self.oc_tree.keys():
            objc = self.schema.get_obj(ldap.schema.ObjectClass, a)

            if objc != None:
                allobjc[objc.oid] = (objc.names, objc.must, objc.may, objc.sup, objc.obsolete)

        return allobjc

    def getatributes(self):
        """
        trae la lista de atributos de un servidor dado
        """
        allatt= {}
        o = []
        for a in self.at_tree.keys():
            att = self.schema.get_obj(ldap.schema.AttributeType, a)

            if att != None:
                allatt[att.oid] = (att.names, att.syntax, att.syntax_len, att.desc, att.collective, att.equality, att.single_value)

        return allatt

    def getvalidoid(self, objects):
        """
        retorno un valor oid libre valida para la creacion de esquemas y atributos
        el proceso valido es pedirle a la iana un oid valido, pero se tarda mas de un mes
        los oid a utilizar son valores predefinidos al momento de la instalacion del servidor ldap
        """
        pass

if __name__ == '__main__':
    sch = SchemasIPA('ldap://localhost')
    #at = sch.getatributes()
    ob = sch.getobjectclasses()

    for a, b in ob.iteritems():
        print a
        print b[0]

然后你可以像这样包装这个类

#a file contained the above class
import schemas

olschemas = schemas.SchemasIPA(url='ldap://192.168.1.81')

#here are, some magic :)
pa = olschemas.schema.get_obj(olschemas._SchemasIPA__ldaps.ObjectClass, 'posixaccount')
pa.must #going to print all the attributes that can't be null's
pa.may #going to print all the attributes that are optional's
于 2013-05-15T05:10:57.217 回答
3

模式的根 DSE 和可能的基本 DN

假设 LDAP 客户端只关心模式中定义的属性(见extensibleObject下文),要确定是否attribute在服务器模式中定义了 an,请检索模式。subSchemaSubEntry在许多目录服务器中,模式的基本 DN(或基本对象)在可能存在于根 DSE中的属性中定义。有关根 DSE 的更多信息,请参阅LDAP:根 DSE。为了检索根 DSE 的内容,向服务器发送一个搜索请求,该请求由 的基础对象''和 的搜索范围以及由和base组成的请求属性列表组成。*+

可扩展对象

请注意,objectClass 的存在extensibleObject允许 LDAP 客户端添加它们需要的任何属性名称和值,就像 FORTRAN 垃圾公共块一样,也就是说,属性可以存在于条目中但未在模式中定义。

subSchemaSubEntry 不存在

如果该subSchemaSubEntry属性不存在,请联系服务器管理员并询问有关检索模式的信息以及足够的访问权限。

subSchemaSubEntry 存在

如果该subSchemaSubEntry属性存在,则通过使用该subSchemaSubEntry属性的值作为基础对象、搜索范围one和请求的属性列表向服务器发送搜索请求来读取模式*+。属性类型定义和 objectClass 定义包含在模式中。

于 2012-07-10T12:37:45.747 回答
2

我正在使用 python 的 ldap 模块连接到 ldap 服务器。我可以查询数据库但我不知道如何检索数据库中存在的字段,以便我可以提前通知用户查询数据库,告诉他他试图访问的字段不在数据库中.

一个简单的解决方案是搜索然后从结果中打印一个键列表。

import ldap

# connect to your ldap server

some_dn = '...' # Your base dn
some_lookup = '...' # your lookup attr

result = conn.search_s(some_dn,ldap.SCOPE_SUBTREE,some_lookup)
result[0][1].keys()

例如,针对我的 AD 服务器,它返回以下内容:

['mailNickname',
 'publicDelegatesBL',
 'logonCount',
 'cn',
 'countryCode',
 'dSCorePropagationData',
 'objectClass',
 # ... many many more
'telephoneNumber',
'physicalDeliveryOfficeName',
'name',
'memberOf',
'codePage',
'userAccountControl',
'msExchMDBRulesQuota',
'lastLogon',
'protocolSettings',
'uSNChanged',
'sn',
'msExchVersion',
'mDBUseDefaults',
'givenName',
'msExchMailboxGuid',
'lastLogoff']
于 2012-07-10T13:01:43.603 回答