SecureConnectionCliArgs.java revision 9e0f94e6fc8b116fbbd7e319bdf57ff3dfdfb446
/*
* 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
*
*
* Copyright 2007-2009 Sun Microsystems, Inc.
*/
/**
* This is a commodity class that can be used to check the arguments required
* to establish a secure connection in the command line. It can be used
* to generate an ApplicationTrustManager object based on the options provided
* by the user in the command line.
*
*/
public final class SecureConnectionCliArgs
{
/**
* The 'hostName' global argument.
*/
/**
* The 'port' global argument.
*/
/**
* The 'bindDN' global argument.
*/
/**
* The 'adminUID' global argument.
*/
/**
* The 'bindPasswordFile' global argument.
*/
/**
* The 'bindPassword' global argument.
*/
/**
* The 'trustAllArg' global argument.
*/
/**
* The 'trustStore' global argument.
*/
/**
* The 'trustStorePassword' global argument.
*/
/**
* The 'trustStorePasswordFile' global argument.
*/
/**
* The 'keyStore' global argument.
*/
/**
* The 'keyStorePassword' global argument.
*/
/**
* The 'keyStorePasswordFile' global argument.
*/
/**
* The 'certNicknameArg' global argument.
*/
/**
* The 'useSSLArg' global argument.
*/
/**
* The 'useStartTLSArg' global argument.
*/
/**
* Argument indicating a SASL option.
*/
/**
* Private container for global arguments.
*/
// the trust manager.
private ApplicationTrustManager trustManager;
/**
* The tracer object for the debug logger.
*/
/**
* End Of Line.
*/
/**
* The Logger.
*/
// Defines if the CLI always use the SSL connection type.
private boolean alwaysSSL = false;
/**
* Creates a new instance of secure arguments.
*
* @param alwaysSSL If true, always use the SSL connection type. In this case,
* the arguments useSSL and startTLS are not present.
*/
public SecureConnectionCliArgs(boolean alwaysSSL)
{
if (alwaysSSL) {
this.alwaysSSL = true;
}
}
/**
* Indicates whether or not any of the arguments are present.
*
* @return boolean where true indicates that at least one of the
* arguments is present
*/
public boolean argumentsPresent() {
boolean present = false;
present = true;
break;
}
}
}
return present;
}
/**
* Get the admin UID which has to be used for the command.
*
* @return The admin UID specified by the command line argument, or the
* default value, if not specified.
*/
public String getAdministratorUID()
{
if (adminUidArg.isPresent())
{
return adminUidArg.getValue();
}
else
{
return adminUidArg.getDefaultValue();
}
}
/**
* Tells whether this parser uses the Administrator UID (instead of the
* bind DN) or not.
* @return <CODE>true</CODE> if this parser uses the Administrator UID and
* <CODE>false</CODE> otherwise.
*/
public boolean useAdminUID()
{
return !adminUidArg.isHidden();
}
/**
* Get the bindDN which has to be used for the command.
*
* @return The bindDN specified by the command line argument, or the
* default value, if not specified.
*/
{
{
}
else
{
return bindDnArg.getDefaultValue();
}
}
/**
* Get the password which has to be used for the command.
*
* @param dn
* The user DN for which to password could be asked.
* @param out
* The input stream to used if we have to prompt to the
* user.
* @param err
* The error stream to used if we have to prompt to the
* user.
* @param clearArg
* The password StringArgument argument.
* @param fileArg
* The password FileBased argument.
* @return The password stored into the specified file on by the
* command line argument, or prompts it if not specified.
*/
{
{
{
// read the password from the stdin.
try
{
{
if (debugEnabled())
{
}
try
{
}
catch (IOException e)
{
}
return null;
}
}
return bindPasswordValue;
}
else
{
}
else
{
// read the password from the stdin.
try
{
}
{
if (debugEnabled())
{
}
try
{
}
catch (IOException e)
{
}
return null;
}
}
}
/**
* Get the password which has to be used for the command.
*
* @param dn
* The user DN for which to password could be asked.
* @param out
* The input stream to used if we have to prompt to the
* user.
* @param err
* The error stream to used if we have to prompt to the
* user.
* @return The password stored into the specified file on by the
* command line argument, or prompts it if not specified.
*/
{
}
/**
* Get the password which has to be used for the command without prompting
* the user. If no password was specified, return null.
*
* @param clearArg
* The password StringArgument argument.
* @param fileArg
* The password FileBased argument.
* @return The password stored into the specified file on by the
* command line argument, or null it if not specified.
*/
{
{
}
else
{
}
else
{
}
return pwd;
}
/**
* Get the password which has to be used for the command without prompting
* the user. If no password was specified, return null.
*
* @return The password stored into the specified file on by the
* command line argument, or null it if not specified.
*/
public String getBindPassword()
{
}
/**
* Initialize Global option.
*
* @throws ArgumentException
* If there is a problem with any of the parameters used
* to create this argument.
* @return a ArrayList with the options created.
*/
throws ArgumentException
{
if (!alwaysSSL) {
} else {
// simulate that the useSSL arg has been given in the CLI
useSSLArg.setPresent(true);
}
if (!alwaysSSL) {
}
try {
} catch (Exception e) {
}
if (alwaysSSL) {
}
false, false, true, INFO_PORT_PLACEHOLDER.get(),
// It is up to the classes that required admin UID to make this argument
// visible and add it.
OPTION_LONG_ADMIN_UID, false, false, true,
adminUidArg.setHidden(true);
OPTION_SHORT_BINDPWD, OPTION_LONG_BINDPWD, false, false, true,
OPTION_SHORT_BINDPWD_FILE, OPTION_LONG_BINDPWD_FILE, false, false,
saslOptionArg = new StringArgument(
"sasloption", OPTION_SHORT_SASLOPTION,
OPTION_LONG_SASLOPTION, false,
true, true,
OPTION_SHORT_TRUSTSTORE_PWD, OPTION_LONG_TRUSTSTORE_PWD, false, false,
OPTION_SHORT_KEYSTOREPATH, OPTION_LONG_KEYSTOREPATH, false, false,
OPTION_LONG_KEYSTORE_PWD, false, false, true,
return argList;
}
/**
* Get the host name which has to be used for the command.
*
* @return The host name specified by the command line argument, or
* the default value, if not specified.
*/
public String getHostName()
{
if (hostNameArg.isPresent())
{
return hostNameArg.getValue();
}
else
{
return hostNameArg.getDefaultValue();
}
}
/**
* Get the port which has to be used for the command.
*
* @return The port specified by the command line argument, or the
* default value, if not specified.
*/
{
{
}
else
{
return portArg.getDefaultValue();
}
}
/**
* Indication if provided global options are validate.
*
* @param buf the MessageBuilder to write the error messages.
* @return return code.
*/
{
// Couldn't have at the same time bindPassword and bindPasswordFile
}
// Couldn't have at the same time trustAll and
// trustStore related arg
}
}
}
// Couldn't have at the same time trustStorePasswordArg and
// trustStorePasswordFileArg
&& trustStorePasswordFileArg.isPresent()) {
}
if (trustStorePathArg.isPresent())
{
// Check that the path exists and is readable
{
value);
}
}
if (keyStorePathArg.isPresent())
{
// Check that the path exists and is readable
{
value);
}
}
// Couldn't have at the same time startTLSArg and
// useSSLArg
if (useStartTLSArg.isPresent()
}
{
{
{
}
}
return CONFLICTING_ARGS.getReturnCode();
}
return SUCCESSFUL_NOP.getReturnCode();
}
/**
* Indication if provided global options are validate.
*
* @param err the stream to be used to print error message.
* @return return code.
*/
{
{
}
return returnValue;
}
/**
* Indicate if the SSL mode is required.
*
* @return True if SSL mode is required
*/
public boolean useSSL()
{
{
return true;
}
else
{
return false ;
}
}
/**
* Indicate if the startTLS mode is required.
*
* @return True if startTLS mode is required
*/
public boolean useStartTLS()
{
if (useStartTLSArg.isPresent())
{
return true;
}
else
{
return false ;
}
}
/**
* Indicate if the SSL mode is always used.
*
* @return True if SSL mode is always used.
*/
public boolean alwaysSSL()
{
return alwaysSSL;
}
/**
* Handle TrustStore.
*
* @return The trustStore manager to be used for the command.
*/
public ApplicationTrustManager getTrustManager()
{
if (trustManager == null)
{
if (trustAllArg.isPresent())
{
// Running a null TrustManager will force createLdapsContext and
// createStartTLSContext to use a bindTrustManager.
return null ;
}
else
if (trustStorePathArg.isPresent())
{
try
{
char[] trustStorePasswordValue = null;
if (trustStorePasswordArg.isPresent())
{
}
else if (trustStorePasswordFileArg.isPresent())
{
}
if (trustStorePasswordStringValue != null)
{
.getProperty("javax.net.ssl.trustStorePassword");
}
if (trustStorePasswordStringValue != null)
{
}
}
catch (KeyStoreException e)
{
// Nothing to do: if this occurs we will systematically refuse the
// certificates. Maybe we should avoid this and be strict, but we
// are in a best effort mode.
}
catch (NoSuchAlgorithmException e)
{
// Nothing to do: if this occurs we will systematically refuse the
// certificates. Maybe we should avoid this and be strict, but we
// are in a best effort mode.
}
catch (CertificateException e)
{
// Nothing to do: if this occurs we will systematically refuse the
// certificates. Maybe we should avoid this and be strict, but we
// are in a best effort mode.
}
catch (IOException e)
{
// Nothing to do: if this occurs we will systematically refuse the
// certificates. Maybe we should avoid this and be strict, but we
// are in a best effort mode.
}
}
}
return trustManager;
}
/**
* Handle KeyStore.
*
* @return The keyStore manager to be used for the command.
*/
public KeyManager getKeyManager()
{
char[] keyStorePasswordValue = null;
if (keyStorePathArg.isPresent())
{
try
{
if (keyStorePasswordArg.isPresent())
{
}
else if (keyStorePasswordFileArg.isPresent())
{
}
if (keyStorePasswordStringValue != null)
{
}
}
catch (KeyStoreException e)
{
// Nothing to do: if this occurs we will systematically refuse
// the
// certificates. Maybe we should avoid this and be strict, but
// we are in a best effort mode.
}
catch (NoSuchAlgorithmException e)
{
// Nothing to do: if this occurs we will systematically refuse
// the
// certificates. Maybe we should avoid this and be strict, but
// we are
// in a best effort mode.
}
catch (CertificateException e)
{
// Nothing to do: if this occurs we will systematically refuse
// the
// certificates. Maybe we should avoid this and be strict, but
// we are
// in a best effort mode.
}
catch (IOException e)
{
// Nothing to do: if this occurs we will systematically refuse
// the
// certificates. Maybe we should avoid this and be strict, but
// we are
// in a best effort mode.
}
if (keyStorePasswordStringValue != null)
{
}
if (certNicknameArg.isPresent())
{
.getValue());
}
else
{
return akm;
}
}
else
{
return null;
}
}
/**
* Returns <CODE>true</CODE> if we can read on the provided path and
* <CODE>false</CODE> otherwise.
* @param path the path.
* @return <CODE>true</CODE> if we can read on the provided path and
* <CODE>false</CODE> otherwise.
*/
{
boolean canRead;
{
}
else
{
canRead = false;
}
return canRead;
}
}