EncodePassword.java revision d20053afa42700e3ec7a6f6fb474622e8680f4cc
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews/*
53e8ebc8f0f7315d4af29401a856d45f10ee2bd8Tinderbox User * CDDL HEADER START
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews *
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * The contents of this file are subject to the terms of the
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * Common Development and Distribution License, Version 1.0 only
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * (the "License"). You may not use this file except in compliance
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * with the License.
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews *
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * You can obtain a copy of the license at
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * trunk/opends/resource/legal-notices/OpenDS.LICENSE
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * See the License for the specific language governing permissions
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * and limitations under the License.
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews *
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * When distributing Covered Code, include this CDDL HEADER in each
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * file and include the License file at
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
91ddbb6e4f1b4b1cdcf26487b51761036df865b7Mark Andrews * add the following below this CDDL HEADER, with the fields enclosed
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews * by brackets "[]" replaced with your own identifying information:
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews * Portions Copyright [yyyy] [name of copyright owner]
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews *
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews * CDDL HEADER END
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews *
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews *
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews * Portions Copyright 2006-2007 Sun Microsystems, Inc.
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews */
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewspackage org.opends.server.tools;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport java.io.OutputStream;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport java.io.PrintStream;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport java.util.ArrayList;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport java.util.Arrays;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport java.util.concurrent.ConcurrentHashMap;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport org.opends.server.api.PasswordStorageScheme;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport org.opends.server.config.ConfigException;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.core.CoreConfigManager;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.core.DirectoryServer;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.core.PasswordStorageSchemeConfigManager;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.extensions.ConfigFileHandler;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.protocols.asn1.ASN1OctetString;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.protocols.ldap.LDAPResultCode;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.schema.AuthPasswordSyntax;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.schema.UserPasswordSyntax;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.types.ByteString;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport org.opends.server.types.DirectoryException;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.types.InitializationException;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.types.NullOutputStream;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport org.opends.server.util.args.ArgumentException;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport org.opends.server.util.args.ArgumentParser;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.util.args.BooleanArgument;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.util.args.FileBasedArgument;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport org.opends.server.util.args.StringArgument;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrews
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport static org.opends.server.config.ConfigConstants.*;
8f1e278931689460a49f0e9be610f2b2cc32c42dEvan Huntimport static org.opends.server.messages.MessageHandler.*;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport static org.opends.server.messages.ToolMessages.*;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport static org.opends.server.util.ServerConstants.*;
5df6758f252feb143c499a34b781ea9cc72794d0Mark Andrewsimport static org.opends.server.util.StaticUtils.*;
/**
* This program provides a utility that may be used to interact with the
* password storage schemes defined in the Directory Server. In particular,
* it can encode a clear-text password using a specified scheme, and it can also
* determine whether a given encoded password is the encoded representation of a
* given clear-text password. Alternately, it can be used to obtain a list of
* the available password storage scheme names.
*/
public class EncodePassword
{
/**
* Processes the command-line arguments and performs the requested action.
*
* @param args The command-line arguments provided to this program.
*/
public static void main(String[] args)
{
int returnCode = encodePassword(args, true, System.out, System.err);
if (returnCode != 0)
{
System.exit(returnCode);
}
}
/**
* Processes the command-line arguments and performs the requested action.
*
* @param args The command-line arguments provided to this program.
*
* @return An integer value that indicates whether processing was successful.
*/
public static int encodePassword(String[] args)
{
return encodePassword(args, true, System.out, System.err);
}
/**
* Processes the command-line arguments and performs the requested action.
*
* @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 An integer value that indicates whether processing was successful.
*/
public static int encodePassword(String[] args, boolean initializeServer,
OutputStream outStream,
OutputStream errStream)
{
PrintStream out;
if (outStream == null)
{
out = NullOutputStream.printStream();
}
else
{
out = new PrintStream(outStream);
}
PrintStream err;
if (errStream == null)
{
err = NullOutputStream.printStream();
}
else
{
err = new PrintStream(errStream);
}
// Define the command-line arguments that may be used with this program.
BooleanArgument authPasswordSyntax = null;
BooleanArgument useCompareResultCode = null;
BooleanArgument listSchemes = null;
BooleanArgument showUsage = null;
StringArgument clearPassword = null;
FileBasedArgument clearPasswordFile = null;
StringArgument encodedPassword = null;
FileBasedArgument encodedPasswordFile = null;
StringArgument configClass = null;
StringArgument configFile = null;
StringArgument schemeName = null;
// Create the command-line argument parser for use with this program.
String toolDescription = getMessage(MSGID_ENCPW_TOOL_DESCRIPTION);
ArgumentParser argParser =
new ArgumentParser("org.opends.server.tools.EncodePassword",
toolDescription, false);
// Initialize all the command-line argument types and register them with the
// parser.
try
{
listSchemes = new BooleanArgument("listschemes", 'l', "listSchemes",
MSGID_ENCPW_DESCRIPTION_LISTSCHEMES);
argParser.addArgument(listSchemes);
clearPassword = new StringArgument("clearpw", 'c', "clearPassword", false,
false, true, "{clearPW}", null, null,
MSGID_ENCPW_DESCRIPTION_CLEAR_PW);
argParser.addArgument(clearPassword);
clearPasswordFile =
new FileBasedArgument("clearpwfile", 'F', "clearPasswordFile", false,
false, "{filename}", null, null,
MSGID_ENCPW_DESCRIPTION_CLEAR_PW_FILE);
argParser.addArgument(clearPasswordFile);
encodedPassword = new StringArgument("encodedpw", 'e', "encodedPassword",
false, false, true, "{encodedPW}",
null, null,
MSGID_ENCPW_DESCRIPTION_ENCODED_PW);
argParser.addArgument(encodedPassword);
encodedPasswordFile =
new FileBasedArgument("encodedpwfile", 'E', "encodedPasswordFile",
false, false, "{filename}", null, null,
MSGID_ENCPW_DESCRIPTION_ENCODED_PW_FILE);
argParser.addArgument(encodedPasswordFile);
configClass = new StringArgument("configclass", 'C', "configClass",
true, false, true, "{configClass}",
ConfigFileHandler.class.getName(), null,
MSGID_ENCPW_DESCRIPTION_CONFIG_CLASS);
configClass.setHidden(true);
argParser.addArgument(configClass);
configFile = new StringArgument("configfile", 'f', "configFile",
true, false, true, "{configFile}", null,
null,
MSGID_ENCPW_DESCRIPTION_CONFIG_FILE);
configFile.setHidden(true);
argParser.addArgument(configFile);
schemeName = new StringArgument("scheme", 's', "storageScheme", false,
false, true, "{scheme}", null, null,
MSGID_ENCPW_DESCRIPTION_SCHEME);
argParser.addArgument(schemeName);
authPasswordSyntax = new BooleanArgument("authpasswordsyntax", 'a',
"authPasswordSyntax",
MSGID_ENCPW_DESCRIPTION_AUTHPW);
argParser.addArgument(authPasswordSyntax);
useCompareResultCode =
new BooleanArgument("usecompareresultcode", 'r',
"useCompareResultCode",
MSGID_ENCPW_DESCRIPTION_USE_COMPARE_RESULT);
argParser.addArgument(useCompareResultCode);
showUsage = new BooleanArgument("usage", 'H', "help",
MSGID_ENCPW_DESCRIPTION_USAGE);
argParser.addArgument(showUsage);
argParser.setUsageArgument(showUsage, out);
}
catch (ArgumentException ae)
{
int msgID = MSGID_ENCPW_CANNOT_INITIALIZE_ARGS;
String message = getMessage(msgID, ae.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
// Parse the command-line arguments provided to this program.
try
{
argParser.parseArguments(args);
}
catch (ArgumentException ae)
{
int msgID = MSGID_ENCPW_ERROR_PARSING_ARGS;
String message = getMessage(msgID, ae.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
err.println(argParser.getUsage());
return 1;
}
// If we should just display usage information, then we've already done it
// so just return without doing anything else.
if (argParser.usageDisplayed())
{
return 0;
}
// Check for conflicting arguments.
if (clearPassword.isPresent() && clearPasswordFile.isPresent())
{
int msgID = MSGID_TOOL_CONFLICTING_ARGS;
String message = getMessage(msgID, clearPassword.getLongIdentifier(),
clearPasswordFile.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
if (encodedPassword.isPresent() && encodedPasswordFile.isPresent())
{
int msgID = MSGID_TOOL_CONFLICTING_ARGS;
String message = getMessage(msgID, encodedPassword.getLongIdentifier(),
encodedPasswordFile.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
// If we are not going to just list the storage schemes, then the clear-text
// password must have been provided. If we're going to encode a password,
// then the scheme must have also been provided.
ASN1OctetString clearPW = null;
if (! listSchemes.isPresent())
{
if (clearPassword.hasValue())
{
clearPW = new ASN1OctetString(clearPassword.getValue());
}
else if (clearPasswordFile.hasValue())
{
clearPW = new ASN1OctetString(clearPasswordFile.getValue());
}
else
{
int msgID = MSGID_ENCPW_NO_CLEAR_PW;
String message = getMessage(msgID, clearPassword.getLongIdentifier(),
clearPasswordFile.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
err.println(argParser.getUsage());
return 1;
}
if ((! encodedPassword.isPresent()) && (! schemeName.isPresent()))
{
int msgID = MSGID_ENCPW_NO_SCHEME;
String message = getMessage(msgID, schemeName.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
err.println(argParser.getUsage());
return 1;
}
}
// Determine whether we're encoding the clear-text password or comparing it
// against an already-encoded password.
boolean compareMode;
ByteString encodedPW = null;
if (encodedPassword.hasValue())
{
compareMode = true;
encodedPW = new ASN1OctetString(encodedPassword.getValue());
}
else
{
compareMode = false;
}
// Perform the initial bootstrap of the Directory Server and process the
// configuration.
DirectoryServer directoryServer = DirectoryServer.getInstance();
if (initializeServer)
{
try
{
directoryServer.bootstrapClient();
directoryServer.initializeJMX();
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_SERVER_BOOTSTRAP_ERROR;
String message = getMessage(msgID, stackTraceToSingleLineString(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
try
{
directoryServer.initializeConfiguration(configClass.getValue(),
configFile.getValue());
}
catch (InitializationException ie)
{
int msgID = MSGID_ENCPW_CANNOT_LOAD_CONFIG;
String message = getMessage(msgID, ie.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_CANNOT_LOAD_CONFIG;
String message = getMessage(msgID, stackTraceToSingleLineString(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
// Initialize the Directory Server schema elements.
try
{
directoryServer.initializeSchema();
}
catch (ConfigException ce)
{
int msgID = MSGID_ENCPW_CANNOT_LOAD_SCHEMA;
String message = getMessage(msgID, ce.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (InitializationException ie)
{
int msgID = MSGID_ENCPW_CANNOT_LOAD_SCHEMA;
String message = getMessage(msgID, ie.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_CANNOT_LOAD_SCHEMA;
String message = getMessage(msgID, stackTraceToSingleLineString(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
// Initialize the Directory Server core configuration.
try
{
CoreConfigManager coreConfigManager = new CoreConfigManager();
coreConfigManager.initializeCoreConfig();
}
catch (ConfigException ce)
{
int msgID = MSGID_ENCPW_CANNOT_INITIALIZE_CORE_CONFIG;
String message = getMessage(msgID, ce.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (InitializationException ie)
{
int msgID = MSGID_ENCPW_CANNOT_INITIALIZE_CORE_CONFIG;
String message = getMessage(msgID, ie.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_CANNOT_INITIALIZE_CORE_CONFIG;
String message = getMessage(msgID, stackTraceToSingleLineString(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
// Initialize the password storage schemes.
try
{
PasswordStorageSchemeConfigManager storageSchemeConfigManager =
new PasswordStorageSchemeConfigManager();
storageSchemeConfigManager.initializePasswordStorageSchemes();
}
catch (ConfigException ce)
{
int msgID = MSGID_ENCPW_CANNOT_INITIALIZE_STORAGE_SCHEMES;
String message = getMessage(msgID, ce.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (InitializationException ie)
{
int msgID = MSGID_ENCPW_CANNOT_INITIALIZE_STORAGE_SCHEMES;
String message = getMessage(msgID, ie.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_CANNOT_INITIALIZE_STORAGE_SCHEMES;
String message = getMessage(msgID, stackTraceToSingleLineString(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
}
// If we are only trying to list the available schemes, then do so and exit.
if (listSchemes.isPresent())
{
if (authPasswordSyntax.isPresent())
{
ConcurrentHashMap<String,PasswordStorageScheme> storageSchemes =
DirectoryServer.getAuthPasswordStorageSchemes();
if (storageSchemes.isEmpty())
{
int msgID = MSGID_ENCPW_NO_STORAGE_SCHEMES;
String message = getMessage(msgID);
err.println(wrapText(message, MAX_LINE_WIDTH));
}
else
{
int size = storageSchemes.size();
ArrayList<String> nameList = new ArrayList<String>(size);
for (PasswordStorageScheme s : storageSchemes.values())
{
nameList.add(s.getAuthPasswordSchemeName());
}
String[] nameArray = new String[size];
nameList.toArray(nameArray);
Arrays.sort(nameArray);
for (String storageSchemeName : nameArray)
{
out.println(storageSchemeName);
}
}
return 0;
}
else
{
ConcurrentHashMap<String,PasswordStorageScheme> storageSchemes =
DirectoryServer.getPasswordStorageSchemes();
if (storageSchemes.isEmpty())
{
int msgID = MSGID_ENCPW_NO_STORAGE_SCHEMES;
String message = getMessage(msgID);
err.println(wrapText(message, MAX_LINE_WIDTH));
}
else
{
int size = storageSchemes.size();
ArrayList<String> nameList = new ArrayList<String>(size);
for (PasswordStorageScheme s : storageSchemes.values())
{
nameList.add(s.getStorageSchemeName());
}
String[] nameArray = new String[size];
nameList.toArray(nameArray);
Arrays.sort(nameArray);
for (String storageSchemeName : nameArray)
{
out.println(storageSchemeName);
}
}
return 0;
}
}
// Either encode the clear-text password using the provided scheme, or
// compare the clear-text password against the encoded password.
if (compareMode)
{
// Check to see if the provided password value was encoded. If so, then
// break it down into its component parts and use that to perform the
// comparison. Otherwise, the user must have provided the storage scheme.
if (authPasswordSyntax.isPresent())
{
String scheme;
String authInfo;
String authValue;
try
{
StringBuilder[] authPWElements =
AuthPasswordSyntax.decodeAuthPassword(encodedPW.stringValue());
scheme = authPWElements[0].toString();
authInfo = authPWElements[1].toString();
authValue = authPWElements[2].toString();
}
catch (DirectoryException de)
{
int msgID = MSGID_ENCPW_INVALID_ENCODED_AUTHPW;
String message = getMessage(msgID, de.getErrorMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_INVALID_ENCODED_AUTHPW;
String message = getMessage(msgID, String.valueOf(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
PasswordStorageScheme storageScheme =
DirectoryServer.getAuthPasswordStorageScheme(scheme);
if (storageScheme == null)
{
int msgID = MSGID_ENCPW_NO_SUCH_AUTH_SCHEME;
String message = getMessage(msgID, scheme);
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
if (storageScheme.authPasswordMatches(clearPW, authInfo, authValue))
{
int msgID = MSGID_ENCPW_PASSWORDS_MATCH;
String message = getMessage(msgID);
out.println(message);
if (useCompareResultCode.isPresent())
{
return LDAPResultCode.COMPARE_TRUE;
}
else
{
return 0;
}
}
else
{
int msgID = MSGID_ENCPW_PASSWORDS_DO_NOT_MATCH;
String message = getMessage(msgID);
out.println(message);
if (useCompareResultCode.isPresent())
{
return LDAPResultCode.COMPARE_FALSE;
}
else
{
return 0;
}
}
}
else
{
PasswordStorageScheme storageScheme;
String encodedPWString;
if (UserPasswordSyntax.isEncoded(encodedPW))
{
try
{
String[] userPWElements =
UserPasswordSyntax.decodeUserPassword(encodedPW.stringValue());
encodedPWString = userPWElements[1];
storageScheme =
DirectoryServer.getPasswordStorageScheme(userPWElements[0]);
if (storageScheme == null)
{
int msgID = MSGID_ENCPW_NO_SUCH_SCHEME;
String message = getMessage(msgID, userPWElements[0]);
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
}
catch (DirectoryException de)
{
int msgID = MSGID_ENCPW_INVALID_ENCODED_USERPW;
String message = getMessage(msgID, de.getErrorMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_INVALID_ENCODED_USERPW;
String message = getMessage(msgID, String.valueOf(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
}
else
{
if (! schemeName.isPresent())
{
int msgID = MSGID_ENCPW_NO_SCHEME;
String message = getMessage(msgID, schemeName.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
encodedPWString = encodedPW.toString();
String scheme = toLowerCase(schemeName.getValue());
storageScheme = directoryServer.getPasswordStorageScheme(scheme);
if (storageScheme == null)
{
int msgID = MSGID_ENCPW_NO_SUCH_SCHEME;
String message = getMessage(msgID, scheme);
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
}
if (storageScheme.passwordMatches(clearPW,
new ASN1OctetString(encodedPWString)))
{
int msgID = MSGID_ENCPW_PASSWORDS_MATCH;
String message = getMessage(msgID);
out.println(message);
if (useCompareResultCode.isPresent())
{
return LDAPResultCode.COMPARE_TRUE;
}
else
{
return 0;
}
}
else
{
int msgID = MSGID_ENCPW_PASSWORDS_DO_NOT_MATCH;
String message = getMessage(msgID);
out.println(message);
if (useCompareResultCode.isPresent())
{
return LDAPResultCode.COMPARE_FALSE;
}
else
{
return 0;
}
}
}
}
else
{
// Try to get a reference to the requested password storage scheme.
PasswordStorageScheme storageScheme;
if (authPasswordSyntax.isPresent())
{
String scheme = schemeName.getValue();
storageScheme = DirectoryServer.getAuthPasswordStorageScheme(scheme);
if (storageScheme == null)
{
int msgID = MSGID_ENCPW_NO_SUCH_AUTH_SCHEME;
String message = getMessage(msgID, scheme);
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
}
else
{
String scheme = toLowerCase(schemeName.getValue());
storageScheme = DirectoryServer.getPasswordStorageScheme(scheme);
if (storageScheme == null)
{
int msgID = MSGID_ENCPW_NO_SUCH_SCHEME;
String message = getMessage(msgID, scheme);
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
}
if (authPasswordSyntax.isPresent())
{
try
{
encodedPW = storageScheme.encodeAuthPassword(clearPW);
int msgID = MSGID_ENCPW_ENCODED_PASSWORD;
String message = getMessage(msgID, encodedPW.stringValue());
out.println(message);
}
catch (DirectoryException de)
{
int msgID = MSGID_ENCPW_CANNOT_ENCODE;
String message = getMessage(msgID, de.getErrorMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_CANNOT_ENCODE;
String message = getMessage(msgID, stackTraceToSingleLineString(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
}
else
{
try
{
encodedPW = storageScheme.encodePasswordWithScheme(clearPW);
int msgID = MSGID_ENCPW_ENCODED_PASSWORD;
String message = getMessage(msgID, encodedPW.stringValue());
out.println(message);
}
catch (DirectoryException de)
{
int msgID = MSGID_ENCPW_CANNOT_ENCODE;
String message = getMessage(msgID, de.getErrorMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
catch (Exception e)
{
int msgID = MSGID_ENCPW_CANNOT_ENCODE;
String message = getMessage(msgID, stackTraceToSingleLineString(e));
err.println(wrapText(message, MAX_LINE_WIDTH));
return 1;
}
}
}
// If we've gotten here, then all processing completed successfully.
return 0;
}
}