【问题标题】:LDAP: querying for user in entire domain using sAMAccountNameLDAP:使用 sAMAccountName 查询整个域中的用户
【发布时间】:2011-09-09 13:37:19
【问题描述】:

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

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

这是来自我的ipython 命令行的 sn-p:

ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,ldap.OPT_X_TLS_NEVER) l=ldap.initialize(服务器) l.simple_bind_s(用户、密码) 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) 打印请求

我得到的结果是

[('CN=ouuser1,OU=newou,DC=id-ad,DC=idea,DC=com', {'accountExpires': ['9223372036854775807'], 'badPasswordTime': ['0'], 'badPwdCount': ['0'], 'cn': ['ouuser1'], 'codePage': ['0'], '国家代码':['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'], '名称':['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']}), (没有任何, ['ldaps://ForestDnsZones.id-ad.idea.com/DC=ForestDnsZones,DC=id-ad,DC=idea,DC=com']), (没有任何, ['ldaps://DomainDnsZones.id-ad.idea.com/DC=DomainDnsZones,DC=id-ad,DC=idea,DC=com']), (无,['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'])]

【问题讨论】:

  • 试试objectClass=organizationalPersonobjectClass=user
  • 你能把完整的条目发到ldaps://ForestDnsZones.id-ad.idea.com/DC=ForestDnsZones,DC=id-ad,DC=idea,DC=com吗?
  • @Ingmar。我确实尝试了这两个对象类,但没有骰子。
  • @Terry:对不起,我没有找到你

标签: ldap


【解决方案1】:

最近,当我们的 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=whatever,dc=yourcompany,dc=com' 而没有 'ou=something')。发生这种情况是因为 AD 将其 DNS 记录服务视为全球域的“对等点”。因此,当您仅使用全局域作为 base_dn 查询 anything 时,它会将这些奇怪的额外记录附加到每个成功查询的末尾(主要是:如果所有域控制器配置相同--他们可能不是!)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    • 2013-05-19
    相关资源
    最近更新 更多