How can I make a LDAP query that returns only groups having OU=Groups from all levels?

橙三吉。 提交于 2019-12-03 20:51:44
Terry Gardner

First, on Microsoft Active Directory is impossible to do this in a single search, that's because AD is not fully LDAP compatible.

LDAP-compliant servers support an extensible-match filter which provides the necessary filtering. From RFC4511:

If the dnAttributes field is set to TRUE, the match is additionally applied against all the AttributeValueAssertions in an entry's distinguished name, and it evaluates to TRUE if there is at least one attribute or subtype in the distinguished name for which the filter item evaluates to TRUE. The dnAttributes field is present to alleviate the need for multiple versions of generic matching rules (such as word matching), where one applies to entries and another applies to entries and DN attributes as well.

Note that the extensible-match filter technique only works with LDAP-compliant servers, of which AD is not one.

For example, I added the following entries to a server:

dn: ou=legacy groups,o=training
objectClass: top
objectClass: organizationalUnit
ou: legacy groups

dn: ou=common groups,o=training
objectClass: top
objectClass: organizationalUnit
ou: common groups

dn: ou=groups,o=training
objectClass: top
objectClass: organizationalUnit
ou: groups

dn: cn=a,ou=common groups,o=training
objectClass: top
objectClass: groupOfUniqueNames
uniqueMember: uid=user.0,ou=people,o=training
cn: a

dn: cn=b,ou=groups,o=training
objectClass: top
objectClass: groupOfUniqueNames
uniqueMember: uid=user.0,ou=people,o=training
cn: b

dn: cn=c,ou=legacy groups,o=training
objectClass: top
objectClass: groupOfUniqueNames
uniqueMember: uid=user.0,ou=people,o=training
cn: c

Examine the filter in the following search after the above entries were added:

ldapsearch --propertiesFilePath ds-setup/11389/ldap-connection.properties \
    --baseDN o=training \
    --searchScope sub '(|(ou:dn:=groups)(ou:dn:=common groups))' 1.1

dn: ou=common groups,o=training

dn: cn=a,ou=common groups,o=training

dn: ou=groups,o=training

dn: cn=b,ou=groups,o=training

Note that ou=common groups, ou=groups, and their subordinates are returned, but not ou=legacy groups and subordinates.

This example uses the modern syntax of the ldapsearch command line tool. If the user is utilizing the legacy OpenLDAP version of ldapsearch, the parameters to the command line tool are somewhat different, but that does not matter. What matters is the filter.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!