The LDAP search tool and sample program
Synopsis
ldapsearch [-a deref] [-A] [-b searchbase] [-B] [-C charset] [-d debuglevel] [-D binddn] [-e] [-f file] [-F sep] [-G realm] [-h ldaphost] [-i file] [-K keyfile] [-l timelimit] [-L] [-m mechanism] [-M] [-n] [-N certificatename] [-o attr_type] [-O maxhops] [-p ldapport] [-P keyfilepw] [-q pagesize] [-R] [-s scope ] [-t] [-T seconds] [-U username] [-v] [-V version] [-w passwd | ?] [-z sizelimit] [-y proxydn] [-Y] [-Z] filter [-9 p] [-9 s] [attrs...]
Description
ldapsearch is a command-line interface to the ldap_search application programming interface (API).
ldapsearch opens a connection to an LDAP server, binds, and performs a search using the filter. The filter should conform to the string representation for LDAP filters (see ldap_search in the Directory Server APIs for more information about filters).
If ldapsearch finds one or more entries, the attributes specified by attrs are retrieved and the entries and values are printed to standard output. If no attrs are listed, all attributes are returned.
To display syntax help for ldapsearch, type ldapsearch -?.
Options
If the utility cannot locate a key database, it will use a hard-coded set of default trusted certificate authority roots. The key database file typically contains one or more certificates of certification authorities (CAs) that are trusted by the client. These types of X.509 certificates are also known as trusted roots.
This parameter effectively enables the -Z switch. For Directory Server on i5/OS if you use -Z and do not use -K or -N, the certificate associated with the Directory Services Client application ID will be used.
-o sn -o -givennameThus, the syntax of the sort parameter is as follows:
[-]<attribute name>[:<matching rule OID>]where
These parameters can be useful when the client has limited resources or when it is connected through a low-bandwidth connection. In general, it allows you to control the rate at which data is returned from a search request. Instead of receiving all of the results at once, you can get them a few entries (a page) at a time. In addition, you can control the duration of the delay taken between each page request, giving the client time to process the results.
-q 25 -T 15If the -v (verbose) parameter is specified, ldapsearch lists how many entries have been returned so far, after each page of entries returned from the server, for example, 30 total entries have been returned.
Multiple -q parameters are enabled such that you can specify different page sizes throughout the life of a single search operation. In the following example, the first page is 15 entries, the second page is 20 entries, and the third parameter ends the paged result/search operation:
-q 15 -q 20 -q 0
In the following example, the first page is 15 entries, and all the rest of the pages are 20 entries, continuing with the last specified -q value until the search operation completes:
-q 15 -q 20
The default ldapsearch operation is to return all entries in a single request. No paging is done for the default ldapsearch operation.
<filter> ::='('<filtercomp>')' <filtercomp> ::= <and>|<or>|<not>|<simple> <and> ::= '&' <filterlist> <or> ::= '|' <filterlist> <not> ::= '!' <filter> <filterlist> ::= <filter>|<filter><filterlist> <simple> ::= <attributetype><filtertype> <attributevalue> <filtertype> ::= '='|'~='|'<='|'>='The '~=' construct is used to specify approximate matching. The representation for <attributetype> and <attributevalue> are as described in "RFC 2252, LDAP V3 Attribute Syntax Definitions" . In addition, if the filtertype is '=' then <attributevalue> can be a single * to achieve an attribute existence test, or can contain text and asterisks ( * ) interspersed to achieve substring matching.
For example, the filter "mail=*" finds any entries that have a mail attribute. The filter "mail=*@student.of.life.edu" finds any entries that have a mail attribute ending in the specified string. To put parentheses in a filter, escape them with a backslash (\) character.
See "RFC 2254, A String Representation of LDAP Search Filters" for a more complete description of allowable filters.
Output format
If one or more entries are found, each entry is written to standard output in the form:
Distinguished Name (DN) attributename=value attributename=value attributename=value ...
Multiple entries are separated with a single blank line. If the -F option is used to specify a separator character, it will be used instead of the `=' character. If the -t option is used, the name of a temporary file is used in place of the actual value. If the -A option is given, only the "attributename" part is written.
Examples
The following command:
ldapsearch "cn=john doe" cn telephoneNumberperforms a subtree search (using the default search base) for entries with a commonName of "john doe". The commonName and telephoneNumber values are retrieved and printed to standard output. The output might look something like this if two entries are found:cn=John E Doe, ou="College of Literature, Science, and the Arts", ou=Students, ou=People, o=University of Higher Learning, c=US cn=John Doe cn=John Edward Doe cn=John E Doe 1 cn=John E Doe telephoneNumber=+1 313 555-5432 cn=John B Doe, ou=Information Technology Division, ou=Faculty and Staff, ou=People, o=University of Higher Learning, c=US cn=John Doe cn=John B Doe 1 cn=John B Doe telephoneNumber=+1 313 555-1111
The command:
ldapsearch -t "uid=jed" jpegPhoto audioperforms a subtree search using the default search base for entries with user id of "jed". The jpegPhoto and audio values are retrieved and written to temporary files. The output might look like this if one entry with one value for each of the requested attributes is found:cn=John E Doe, ou=Information Technology Division, ou=Faculty and Staff, ou=People, o=University of Higher Learning, c=US audio=/tmp/ldapsearch-audio-a19924 jpegPhoto=/tmp/ldapsearch-jpegPhoto-a19924
The command:
ldapsearch -L -s one -b "c=US" "o=university*" o descriptionperforms a one-level search at the c=US level for all organizations whose organizationName begins with university. Search results will be displayed in the LDIF format (see LDAP Data Interchange Format). The organizationName and description attribute values will be retrieved and printed to standard output, resulting in output similar to this:dn: o=University of Alaska Fairbanks, c=US o: University of Alaska Fairbanks description: Preparing Alaska for a brave new tomorrow description: leaf node only dn: o=University of Colorado at Boulder, c=US o: University of Colorado at Boulder description: No personnel information description: Institution of education and research dn: o=University of Colorado at Denver, c=US o: University of Colorado at Denver o: UCD o: CU/Denver o: CU-Denver description: Institute for Higher Learning and Research dn: o=University of Florida, c=US o: University of Florida o: UFl description: Shaper of young minds ...
The command:
ldapsearch -b "c=US" -o ibm-slapdDN "objectclass=person" ibm-slapdDNperforms a subtree level search at the c=US level for all persons. This special attribute (ibm-slapdDN), when used for sorted searches, sorts the search results by the string representation of the Distinguished Name (DN). The output might look something like this:cn=Al Edwards,ou=Widget Division,ou=Austin,o=IBM,c=US cn=Al Garcia,ou=Home Entertainment,ou=Austin,o=IBM,c=US cn=Amy Nguyen,ou=In Flight Systems,ou=Austin,o=IBM,c=US cn=Arthur Edwards,ou=Widget Division,ou=Austin,o=IBM,c=US cn=Becky Garcia,ou=In Flight Systems,ou=Austin,o=IBM,c=US cn=Ben Catu,ou=In Flight Systems,ou=Austin,o=IBM,c=US cn=Ben Garcia Jr,ou=Home Entertainment,ou=Austin,o=IBM,c=US cn=Bill Keller Jr.,ou=In Flight Systems,ou=Austin,o=IBM,c=US cn=Bob Campbell,ou=In Flight Systems,ou=Austin,o=IBM,c=US
The command:
ldapsearch –h hostname –o sn –b "o=ibm,c=us" "title=engineer"returns all entries in an IBM employee directory whose title is "engineer", with the results sorted by surname.
The command:
ldapsearch –h hostname –o -sn –o cn –b "o=ibm,c=us" "title=engineer"returns all entries in an IBM employee directory whose title is "engineer", with the results sorted by surname (in descending order) and then by common name (in ascending order).
The command:
ldapsearch –h hostname –q 5 –T 3 –b o=ibm,c=us "title=engineer"returns five entries per page, with a delay of 3 seconds between pages for all entries in an IBM employee directory whose title is "engineer".
This example demonstrates searches where a referral object is involved. As discussed in LDAP directory referrals, Directory Server LDAP directories can contain referral objects, provided that they contain only the following:
Assume that 'System_A' holds the referral entry:
dn: cn=Barb Jensen, ou=Rochester, o=Big Company, c=US ref: ldap://System_B:389/cn=Barb Jensen, ou=Rochester, o=Big Company, c=US objectclass: referralAll attributes associated with the entry should reside on 'System_B'.
System_B contains an entry:
dn: cn=Barb Jensen, ou=Rochester, o=Big Company, c=US cn: Barb Jensen objectclass: organizationalPerson sn: Jensen telephonenumber: (800) 555 1212When a client issues a request to 'System_A', the LDAP server on System_A responds to the client with the URL:
ldap://System_B:389/cn=Barb Jensen, ou=Rochester, o=Big Company, c=USThe client uses this information to issue a request to System_B. If the entry on System_A contains attributes in addition to dn, objectclass, and ref, the server ignores those attributes (unless you specify the -R flag to indicate not to chase referrals).
When the client receives a referral response from a server, it issues the request again, this time to the server to which the returned URL refers. The new request has the same scope as the original request. The results of this search vary depending on the value you specify for the scope of the search (-b).
If you specify -s base, as shown here:
ldapsearch -h System_A -b 'ou=Rochester, o=Big Company, c=US' -s base 'sn=Jensen'the search returns all attributes for all entries with 'sn=Jensen' that reside in 'ou=Rochester, o=Big Company, c=US' on both System_A and System_B.
If you specify -s sub, as shown here:
ldapsearch -s sub "cn=John"the server would search all suffixes and return all entries with "cn=John". This is known as a subtree search on a null base. The entire directory is searched with one search operation instead of doing multiple searches each with a different suffix as the search base. This type of search operation takes longer and consumes more system resources because it is searching the entire directory (all suffixes).
Note:A subtree search on a null base does not return schema information, change log information, nor anything from the system-projected backend.If you specify -s sub, as shown here:
ldapsearch -h System_A -b 'ou=Rochester, o=Big Company, c=US' -s sub 'sn=Jensen'the search returns all attributes for all entries with 'sn=Jensen' that reside in or below 'ou=Rochester, o=Big Company, c=US' on both System_A and System_B.
If you specify -s one, as shown here:
ldapsearch -h System_A -b 'ou=Rochester, o=Big Company, c=US' -s one 'sn=Jensen'the search returns no entries on either system. Instead, the server returns the referral URL to the client:
ldap://System_B:389/cn=Barb Jensen, ou=Rochester, o=Big Company, c=USThe client in turn submits a request:
ldapsearch -h System_B -b 'ou=Rochester, o=Big Company, c=US' -s one 'sn=Jensen'This does not give any results either, because the entry
dn: cn=Barb Jensen, ou=Rochester, o=Big Company, c=USresides at
ou=Rochester, o=Big Company, c=USA search with -s one attempts to find entries in the level immediately below
ou=Rochester, o=Big Company, c=US
Diagnostics
Exit status is 0 if no errors occur. Errors result in a non-zero exit status and a diagnostic message being written to standard error.