LDAPSearch.java revision 0218789478dbe859fd4593d9dd37360750a43893
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Portions Copyright 2006-2007 Sun Microsystems, Inc.
*/
/**
* This class provides a tool that can be used to issue search requests to the
* Directory Server.
*/
public class LDAPSearch
{
/**
* The fully-qualified name of this class.
*/
// The set of response controls for the search.
// The message ID counter to use for requests.
private AtomicInteger nextMessageID;
// The print stream to use for standard error.
private PrintStream err;
// The print stream to use for standard output.
private PrintStream out;
/**
* Constructor for the LDAPSearch object.
*
* @param nextMessageID The message ID counter to use for requests.
* @param out The print stream to use for standard output.
* @param err The print stream to use for standard error.
*/
{
this.nextMessageID = nextMessageID;
}
/**
* Execute the search based on the specified input parameters.
*
* @param connection The connection to use for the search.
* @param baseDN The base DN for the search request.
* @param filters The filters to use for the results.
* @param attributes The attributes to return in the results.
* @param searchOptions The constraints for the search.
* @param wrapColumn The column at which to wrap long lines.
*
* @return The number of matching entries returned by the server. If there
* were multiple search filters provided, then this will be the
* total number of matching entries for all searches.
*
* @throws IOException If a problem occurs while attempting to communicate
* with the Directory Server.
*
* @throws LDAPException If the Directory Server returns an error response.
*/
int wrapColumn )
throws IOException, LDAPException
{
int matchingEntries = 0;
{
false, filter, attributes);
if(!searchOptions.showOperations())
{
try
{
byte opType;
do
{
int resultCode = 0;
switch(opType)
{
for (LDAPControl c : responseControls)
{
{
try
{
c.getControl());
if (previousDN != null)
{
}
} catch (Exception e) {}
}
{
try
{
c.getControl());
{
{
msgID =
}
}
else
{
if (acrc.isInactive())
{
}
{
}
{
{
}
}
{
{
msgID =
}
}
}
} catch (Exception e) {}
}
}
break;
break;
break;
default:
// FIXME - throw exception?
break;
}
{
int msgID = MSGID_OPERATION_FAILED;
}
else if (errorMessage != null)
{
}
} while(opType != OP_TYPE_SEARCH_RESULT_DONE);
} catch(ASN1Exception ae)
{
if (debugEnabled())
{
}
}
}
}
{
}
return matchingEntries;
}
/**
* Appends an LDIF representation of the entry to the provided buffer.
*
* @param entry The entry to be written as LDIF.
* @param buffer The buffer to which the entry should be appended.
* @param wrapColumn The column at which long lines should be wrapped.
* @param typesOnly Indicates whether to include only attribute types
* without values.
*/
int wrapColumn, boolean typesOnly)
{
// Add the DN to the buffer.
int colsRemaining;
if (needsBase64Encoding(dnString))
{
}
else
{
}
{
}
else
{
int startPos = colsRemaining;
{
}
{
}
}
// Add the attributes to the buffer.
for (LDAPAttribute a : attributes)
{
if(typesOnly)
{
} else
{
for (ASN1OctetString v : a.getValues())
{
if (needsBase64Encoding(v.value()))
{
} else
{
valueString = v.stringValue();
}
{
} else
{
int startPos = colsRemaining;
{
}
if (startPos < valueLength)
{
}
}
}
}
}
// Make sure to add an extra blank line to ensure that there will be one
// between this entry and the next.
}
/**
* Retrieves the set of response controls included in the last search result
* done message.
*
* @return The set of response controls included in the last search result
* done message.
*/
{
return responseControls;
}
/**
* The main method for LDAPSearch tool.
*
* @param args The command-line arguments provided to this program.
*/
{
if(retCode != 0)
{
}
}
/**
* Parses the provided command-line arguments and uses that information to
* run the ldapsearch tool.
*
* @param args The command-line arguments provided to this program.
*
* @return The error code.
*/
{
}
/**
* Parses the provided command-line arguments and uses that information to
* run the ldapsearch tool.
*
* @param args The command-line arguments provided to this
* program.
* @param initializeServer Indicates whether to initialize the server.
* @param outStream The output stream to use for standard output, or
* <CODE>null</CODE> if standard output is not
* needed.
* @param errStream The output stream to use for standard error, or
* <CODE>null</CODE> if standard error is not
* needed.
*
* @return The error code.
*/
{
{
}
else
{
}
{
}
else
{
}
// Create the command-line argument parser for use with this program.
false, true, 0, 0,
"[filter] [attributes ...]");
try
{
OPTION_LONG_HOST, false, false, true,
OPTION_LONG_PORT, false, false, true,
OPTION_LONG_BINDDN, false, false, true,
false, false, true,
false, false,
OPTION_LONG_BASEDN, true, false, true,
OPTION_LONG_FILENAME, false, false,
"useSASLExternal",
OPTION_LONG_SASLOPTION, false,
true, true,
OPTION_LONG_KEYSTOREPATH, false, false, true,
OPTION_LONG_KEYSTORE_PWD, false, false,
new FileBasedArgument("keystorepasswordfile",
false, false,
false, false, true, "{nickname}", null,
false, false, true,
false, false, true, OPTION_VALUE_TRUSTSTORE_PWD,
null,
new FileBasedArgument("truststorepasswordfile",
OPTION_LONG_TRUSTSTORE_PWD_FILE, false, false,
OPTION_LONG_PROXYAUTHID, false,
false, true,
"usePasswordPolicyControl",
false, false, true,
"ps[:changetype[:changesonly[:entrychgcontrols]]]",
"simplePageSize", false, false, true,
false, 0,
false, false,
true, OPTION_VALUE_ASSERTION_FILE,
"matchedValuesFilter", false, true, true,
"{controloid[:criticality[:value|::b64value|:<fileurl]]}",
} catch (ArgumentException ae)
{
return 1;
}
// Parse the command-line arguments provided to this program.
try
{
}
catch (ArgumentException ae)
{
int msgID = MSGID_ERROR_PARSING_ARGS;
return 1;
}
// If we should just display usage information, then print it and exit.
if (argParser.usageDisplayed())
{
return 0;
}
{
try
{
} catch(LDAPException le)
{
if (debugEnabled())
{
}
return 1;
}
// The rest are attributes
for(String s : filterAndAttributeStrings)
{
attributes.add(s);
}
}
{
int msgID = MSGID_TOOL_CONFLICTING_ARGS;
return 1;
}
{
int msgID = MSGID_TOOL_CONFLICTING_ARGS;
return 1;
}
{
int msgID = MSGID_TOOL_CONFLICTING_ARGS;
return 1;
}
{
int msgID = MSGID_TOOL_CONFLICTING_ARGS;
return 1;
}
int portNumber = 389;
try
{
} catch(ArgumentException ae)
{
if (debugEnabled())
{
}
return 1;
}
// Read the LDAP version number.
try
{
{
return 1;
}
} catch(ArgumentException ae)
{
if (debugEnabled())
{
}
return 1;
}
// password policy control.
{
// read the password from the stdin.
try
{
{
if (debugEnabled())
{
}
return 1;
}
} else if(bindPasswordValue == null)
{
// Read from file if it exists.
}
if (keyStorePassword.isPresent())
{
}
else if (keyStorePasswordFile.isPresent())
{
}
if (trustStorePassword.isPresent())
{
}
else if (trustStorePasswordFile.isPresent())
{
}
try
{
} catch(ArgumentException ex1)
{
return 1;
}
if(val == false)
{
return 1;
}
if(val == false)
{
return 1;
}
if(controlStr.isPresent())
{
{
{
return 1;
}
}
}
if (proxyAuthzID.isPresent())
{
}
if (pSearchInfo.isPresent())
{
new HashSet<PersistentSearchChangeType>();
boolean changesOnly = true;
boolean returnECs = true;
if (! tokenizer.hasMoreTokens())
{
return 1;
}
else
{
{
return 1;
}
}
if (tokenizer.hasMoreTokens())
{
while (st.hasMoreTokens())
{
{
}
{
}
{
}
{
}
{
}
else
{
return 1;
}
}
}
if (changeTypes.isEmpty())
{
}
if (tokenizer.hasMoreTokens())
{
{
changesOnly = true;
}
{
changesOnly = false;
}
else
{
return 1;
}
}
if (tokenizer.hasMoreTokens())
{
{
returnECs = true;
}
{
returnECs = false;
}
else
{
return 1;
}
}
}
if (assertionFilter.isPresent())
{
try
{
// FIXME -- Change this to the correct OID when the official one is
// assigned.
new LDAPControl(OID_LDAP_ASSERTION, true,
}
catch (LDAPException le)
{
return 1;
}
}
if (matchedValuesFilter.isPresent())
{
new ArrayList<MatchedValuesFilter>();
for (String s : mvFilterStrings)
{
try
{
}
catch (LDAPException le)
{
return 1;
}
}
}
{
try
{
new LDAPControl(new ServerSideSortRequestControl(
}
catch (LDAPException le)
{
return 1;
}
}
// Set the connection options.
if(saslOptions.isPresent())
{
{
{
if(mechValue == false)
{
return 1;
}
} else
{
if(propValue == false)
{
return 1;
}
}
}
}
{
{
return 1;
}
if(keyStorePathValue == null)
{
return 1;
}
}
// Read the filter strings.
if(fileNameValue != null)
{
try
{
{
{
// ignore empty lines.
continue;
}
}
} catch(Exception e)
{
if (debugEnabled())
{
}
return 1;
}
finally
{
{
try
{
} catch (IOException ioe) {}
}
}
}
{
int msgid = MSGID_SEARCH_NO_FILTERS;
return 1;
}
int wrapColumn = 80;
{
wrapColumn = 0;
}
try
{
if (initializeServer)
{
// Bootstrap and initialize directory data structures.
}
// Connect to the specified host with the supplied userDN and password.
{
if (certNickname.isPresent())
{
}
else
{
clientAlias = null;
}
sslConnectionFactory = new SSLConnectionFactory();
}
{
// We don't actually need to open a connection or perform the search,
// so we're done. We should return 0 to either mean that the processing
// was successful or that there were no matching entries, based on
// countEntries.isPresent() (but in either case the return value should
// be zero).
return 0;
}
int matchingEntries = 0;
if (simplePageSize.isPresent())
{
{
}
while (true)
{
boolean responseFound = false;
for (LDAPControl c :responseControls)
{
{
try
{
responseFound = true;
break;
}
catch (LDAPException le)
{
}
}
}
if (! responseFound)
{
message);
}
{
break;
}
}
}
else
{
}
if (countEntries.isPresent())
{
return matchingEntries;
}
else
{
return 0;
}
} catch(LDAPException le)
{
if (debugEnabled())
{
}
return code;
} catch(LDAPConnectionException lce)
{
if (debugEnabled())
{
}
lce.getResultCode(),
lce.getMatchedDN());
return code;
} catch(Exception e)
{
if (debugEnabled())
{
}
return 1;
} finally
{
if(connection != null)
{
if (ldapSearch == null)
{
}
else
{
}
}
}
}
}