LDAP:使用sAMAccountName在整个域中查询用户

时间:2011-09-09 13:37:19

标签: ldap

我正在使用python-ldap模块在​​Windows 2003 R2服务器上使用AD。

当我搜索ObjectClass=Person时,我发现查询结果中也会返回一些服务。 我想知道如何更改查询以便只返回用户条目,还请您指出任何专注于此的文档。

以下是ipython命令行的摘录:


    ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
    l=ldap.initialize(server)
    l.simple_bind_s(user, password) 
    user_filter = '(&(objectClass=person)(sAMAccountName=ouuser1))'
    base_dn='DC=id-ad, DC=idea, DC=com'
    qres=l.search_ext_s(base_dn, ldap.SCOPE_SUBTREE, user_filter)
    print qres

我得到的结果是


    [('CN=ouuser1,OU=newou,DC=id-ad,DC=idea,DC=com',
      {'accountExpires': ['9223372036854775807'],
       'badPasswordTime': ['0'],
       'badPwdCount': ['0'],
       'cn': ['ouuser1'],
       'codePage': ['0'],
       'countryCode': ['0'],
       'displayName': ['ouuser1'],
       'distinguishedName': ['CN=ouuser1,OU=newou,DC=id-ad,DC=idea,DC=com'],
       'givenName': ['ouuser1'],
       'instanceType': ['4'],
       'lastLogoff': ['0'],
       'lastLogon': ['0'],
       'logonCount': ['0'],
       'memberOf': ['CN=ougroup1,OU=newou,DC=id-ad,DC=idea,DC=com'],
       'name': ['ouuser1'],
       'objectCategory': ['CN=Person,CN=Schema,CN=Configuration,DC=id-ad,DC=idea,DC=com'],
       'objectClass': ['top', 'person', 'organizationalPerson', 'user'],
       'objectGUID': ['@\x87C\\\xdf\xbe\xe0M\x8c\xb7S-\xf4\x00.\xd0'],
       'objectSid': ['\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\x8c\xc6\xd8N\xe3`\x16\xe0\x96\xcf4\xabb\x04\x00\x00'],
       'primaryGroupID': ['513'],
       'pwdLastSet': ['0'],
       'sAMAccountName': ['ouuser1'],
       'sAMAccountType': ['805306368'],
       'uSNChanged': ['417845'],
       'uSNCreated': ['417839'],
       'userAccountControl': ['512'],
       'userPrincipalName': ['ouuser1@id-ad.idea.com'],
       'whenChanged': ['20110909055335.0Z'],
       'whenCreated': ['20110909055335.0Z']}),
     (None,
      ['ldaps://ForestDnsZones.id-ad.idea.com/DC=ForestDnsZones,DC=id-ad,DC=idea,DC=com']),
     (None,
      ['ldaps://DomainDnsZones.id-ad.idea.com/DC=DomainDnsZones,DC=id-ad,DC=idea,DC=com']),
     (None, ['ldaps://id-ad.idea.com/CN=Configuration,DC=id-ad,DC=idea,DC=com'])]

我要删除的条目是。

(None,
  ['ldaps://ForestDnsZones.id-ad.idea.com/DC=ForestDnsZones,DC=id-ad,DC=idea,DC=com']),
 (None,
  ['ldaps://DomainDnsZones.id-ad.idea.com/DC=DomainDnsZones,DC=id-ad,DC=idea,DC=com']),
 (None, ['ldaps://id-ad.idea.com/CN=Configuration,DC=id-ad,DC=idea,DC=com'])]

1 个答案:

答案 0 :(得分:0)

当我们的AD团队启用了AD的内置DNS功能(DNS应用程序分区)时,我最近遇到了这个问题。我有很多现有的代码可以迭代LDAP结果,如下所示:

for record in records:
    for key, value in record[1].items():
        # Do stuff with the returned keys/values here

这一变化导致我的代码中断,因为现在突然出现了与您相似的结果:

(None,
      ['ldaps://ForestDnsZones.ourdomain.com/DC=ForestDnsZones,DC=ourdomain,DC=com']),

修复相对简单:

for record in records:
    if record[0]: # Add this conditional
        for key, value in record[1].items():

如果AD / LDAP返回的元组中的第一项是None,则表示它是LDAP引用。这些可以安全地被忽略,因为我们不会关注推荐(因为它们本身已经被破坏;他们不会告诉您连接到推荐目的地所需的凭据)。

更多信息:如果您要查询整个域,则只会出现这种情况(例如,您的base_dn是&dc; dc = any,dc = yourcompany,dc = com'没有' ou =某事')。之所以发生这种情况,是因为AD认为其DNS记录服务是一个" peer"到全球领域。因此,当您仅使用全局域作为base_dn查询任何时,它会将这些奇怪的额外记录追加到每个成功查询的末尾(主要是:如果所有域控制器都配置相同的话) - 他们可能不是!)。

相关问题