LDAP查询基础

这篇文章讨论了轻量级目录访问协议(Lightweight Directory Access Protocol - LDAP)的查询. 在排查Microsoft® Exchange Server 和它与目录之间关系的时候这项技术非常有用, 但是确有点不容易搞清楚. 这篇文章提供了关于LDAP查询的基础知识.

LDAP语法基础

=============

  • = (EQUAL TO)

    这个LDAP参数的意思是某项属性与某个值相等这项条件必须为真. 比如说, 如果你想要找到所有名(firstname)为John的对象, 你应该使用:

    (givenName=John)

    这会返回所有first name是John的对象. 这里的括号用来强调LDAP语句的开始与结束的位置.

  • & (logical AND)

    当你有多个条件都需要满足的时候, 你可以使用这项语法. 比如说, 如果你想要找到所有的first name是John, 并且住在Dallas的人, 你应该这样用:

    (&(givenName=John)(l=Dallas))

    注意每一个参数都有自己的一套括号. 整个LDAP语句必须被一套主括号包围起来. 这里的 & 操作符意味着对于你查询的对象, 对于您指定的filter中的每一个参数条件都必须为真.

  • ! (logical NOT)

    这个操作符被用来排除满足某项条件的对象. 假设你需要找到除去那些first name是John的所有对象. 你需要使用下面的语句:

    (!givenName=John)

    这个语句会找到所有first name不是John的对象. 注意 ! 操作符直接放在参数的前面, 并且处在括号对儿的里面. 因为这个语句里只有一个参数, 为了说明问题把它用括号包围起来了.

  • * (wildcard)
    You use the wildcard operator to represent a value that could be equal to anything. One such situation might be if you wanted to find all objects that have a value for title. You would then use:

    你可以使用通配符操作符来表示一个可以等于任何值的数值. 一个可能的情形是你想要找到所有title被设置了值的(有值的)对象. 你应该这样用:

    (title=*)

    这会返回所有title属性有值的对象. 另一个例子, 如果你知道一个对象的first name由Jo开始. 那么, 你可以使用下面的查询语句来查找这样的对象.

    (givenName=Jo*)

    This would apply to all objects whose first name starts with Jo.这项查询会在所有以Jo开始的first name的对象上.

  • 下面的是一些LDAP语法的更加高级些的例子:

    你需要一个filter来找到所有在Dallas或者Austin的对象, 并且first name还得是John. 这个filter应该是:

    (&(givenName=John)(|(l=Dallas)(l=Austin)))

    你已经在Application log中收到了9548个event, 你需要找到所有的引发这个日志事件的对象. 在这种情况下, 你需要找到所有的被disable的user, (msExchUserAccountControl=2) 并且在msExchMasterAccountSID这个属性上没有值. 

    (&(msExchUserAccountControl=2)(!msExchMasterAccountSID=*))

    The ! operator in conjunction with the wildcard operator will look for objects where that attribute is not set to anything.

    注意. ! 操作符跟通配符连接在一起, 会寻找属性没被设为任何值的对象.

    在哪里使用LDAP查询呢?

    ==============

    在与Exchange服务器一起工作的时候, 你会遇到使用LDAP字符串的情形. 比如说, 无论何时, 你设置一个接受者, 或者mailbox管理者策略, 或者一个地址列表filter, 或者当你搜索Active Directroy目录服务的时候, Exchange服务器都会使用LDAP. 在下一个部分, 会讨论一些你可以使用LDAP语句的方法:

    一. 使用Active Directory Users and Computers进行搜索

    下面的例子描述了如何使用Active Directory Users and Computers进行搜索:

    1. 打开Active Directory Users and Computers (Win + R, dsa.msc)

    2. 右键点击域对象(domain object), 然后选择Find.

    Aa996205_2f779541-ae57-4be9-a2d4-10378092e603(en-us,EXCHG_65)

    3. 点击紧挨着Find的下拉列表, 然后选择Custom Search.

    4. 在下面的屏幕中, 选择Advanced 选项卡.

    5. 在Enter LDAP query下面输入恰当的LDAP语句.

    Aa996205_42d946fc-6827-43ae-b85e-0bb8be4b051e(en-us,EXCHG_65)

    这个例子会试图寻找所有满足下面条件的对象: 他们的title属性是prez, 或者他们的名字由test开始. 如果你点击Find Now, 你应该看到恰当的输出结果, 请看下面的截屏:

    Aa996205_9e9364e5-54b9-479e-92fd-40bbfb1502df(en-us,EXCHG_65)

    二. 使用LDP进行搜索

    你还可以使用LDP进行搜索, 这个工具包含在Microsoft Windows Server™ 2003 和 Windows® 2000 Server support tools中. 这个工具给予你不仅仅是搜索domain container的能力, 还有搜索Configuration container的能力. 你首先会打开LDP, 然后连接到合法的domain controller上. 然后你需要使用合适的credential来绑定. 选择View, 然后选择Tree. 保持Base DN栏为空, 点击OK.导航到你想要搜索的container上, 右击container, 然后选择Search.

    Aa996205_e7c4bdd5-5c25-4cdd-9c56-989193446aba(en-us,EXCHG_65)

    一个对话框会tanchu,其中会包含Base DN的正确值. 输入你想要filter过滤的LDAP语句, 像前面那样的结果应该会出现的.

    Aa996205_829e620d-dc18-4103-a6be-e1f64e5cbd9b(en-us,EXCHG_65)

    如果你想要搜索你当前所在的containter下面的所有层次, 你需要确保选项Subtree被选中. 点击Run, 它应该会找到所有满足条件的结果.

    三. 使用LDIFDE搜索

    你需要为获取一个满足某种条件的对象集而进行查询, 而且你希望建立一个这些user的列表. LDIFDE是一个命令行工具, 它可以帮助你建立这样的列表. 假设你希望找到并保存所有mailNickName 以Jeff开始的用户列表. 你可以运行下面的命令.

    C:\>ldifde -d "DC=witaylorroot,DC=com" -f c:\output.txt -r "(&(objectClass=user)(mailNickName=jeff*))

     

    在这个例子里, 你将会找到三个对象, 然后输出所有他们的属性到output.txt文件中. 如果你输出近千个用户的结果, 输出结果文件就会变得非常庞大. 还有另一个不同的选项. 你可以使用-l(小写的L)开关来指定要输出那些属性. distinguished name 永远会被输出的, 但是如果你不想要其他的东西, 你可以添加-l nothing到字符串后面, 这会streamline 输出结果. 下面就是一个例子:

    C:\>ldifde -d "DC=witaylorroot,DC=com" -f c:\output.txt -l nothing -r "(&(objectClass=user)(mailNickName=jeff*))

     

    Consider that in this case, you want to use the LDIFDE export referenced earlier, but only want to include the homeMDB attribute in the output. You must use the following command.

    如果你想要包括homeMDB 属性到结果中. 那么你就要运行下面的命令:

    C:\>ldifde -d "DC=witaylorroot,DC=com" -f c:\output.txt -l "homeMDB" -r "(&(objectClass=user)(mailNickName=jeff*))

     

    dn: CN=jeff,OU=55Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First Storage Group,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=Administrative Groups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

     

    dn: CN=jeff2,CN=Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First Storage Group,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=Administrative Groups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

     

    dn: CN=jeff3,CN=Users,DC=witaylorroot,DC=com changetype: add homeMDB: CN=Private Information Store (WITAYLORNT4EX55),CN=First Storage Group,CN=InformationStore,CN=WITAYLORNT4EX55,CN=Servers,CN=WITAYLORMIXEDSITE,CN=Administrative Groups,CN=WITAYLORORG,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=witaylorroot,DC=com

     

    如果你进行一个输出操作而不指定哪些属性被输出, 你就会想要使用-n开关来去掉普通情形下被包括进来的值. 这会帮助防止输出文件变得过大.

    四, 在ADModify中使用LDAP查询

    译注: 这个工具不常用, 还得上case才能拿到这个工具, 就不翻译了. 列在这里.

    To obtain the ADModify tool, contact Microsoft Product Support Services. For more information about how to contact Microsoft Product Support Services, see the Microsoft Help and Support Web site.

    To obtain the ADModify tool from a third-party Web site, see the following GotDotNet Web site: ADModify.NET: Workspace Home.

    ADModify is a tool that Microsoft Product Support Services uses on a daily basis. With large Active Directory environments, it is not always easy to add an entire organizational unit (OU) that could have thousands of users in it to the list on the right side, and then parse through all of them to find the users you need to change. There is an alternative. On the first screen, choose Modify Existing User Attributes and click Next. On the Modify Active Directory Users screen, there is an Advanced button.

    ADModify Tool Main Screen

    If you click the Advanced button, the Custom LDAP Filter dialog box appears. In this dialog box, you type the LDAP filter that you want to use. In this example, you only want to list the groups that are mail-enabled. It would look like the following.

    ADModify Tool Custom LDAP Filter Dialogue

    Next, click OK. Select the OU or the domain where you want ADModify to search. If you want it to look in lower-level containers within the one you selected, make sure to select Traverse Subcontainers when Enumerating Users. Click Add to List, and then click Yes when warned about how long it could take. The objects that meet the criteria you specified should now appear in the right pane.

    ADmodify Tool Options Screen

    From here, highlight the objects that you want to modify, and continue with the wizard.

    译自:

    LDAP Query Basics

    http://technet.microsoft.com/en-us/library/aa996205%28EXCHG.65%29.aspx

    链接:

    LDIFDE - Export / Import data from Active Directory - LDIFDE commands

    http://support.microsoft.com/kb/555636/en-us

  • 原文地址:https://www.cnblogs.com/awpatp/p/1668097.html