// If both a bind password and bind password file were provided, then return
// an error.
if (args.bindPasswordArg.isPresent() &&
args.bindPasswordFileArg.isPresent())
{
Message message = ERR_LDAP_CONN_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(
args.bindPasswordArg.getLongIdentifier(),
args.bindPasswordFileArg.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
throw new ArgumentException(message);
}
// If both a key store password and key store password file were provided,
// then return an error.
if (args.keyStorePasswordArg.isPresent() &&
args.keyStorePasswordFileArg.isPresent())
{
Message message = ERR_LDAP_CONN_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(
args.keyStorePasswordArg.getLongIdentifier(),
args.keyStorePasswordFileArg.getLongIdentifier());
throw new ArgumentException(message);
}
// If both a trust store password and trust store password file were
// provided, then return an error.
if (args.trustStorePasswordArg.isPresent() &&
args.trustStorePasswordFileArg.isPresent())
{
Message message = ERR_LDAP_CONN_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(
args.trustStorePasswordArg.getLongIdentifier(),
args.trustStorePasswordFileArg.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
throw new ArgumentException(message);
}
// Create the LDAP connection options object, which will be used to
// customize the way that we connect to the server and specify a set of
// basic defaults.
LDAPConnectionOptions connectionOptions = new LDAPConnectionOptions();
connectionOptions.setVersionNumber(3);
// See if we should use SSL or StartTLS when establishing the connection.
// If so, then make sure only one of them was specified.
if (args.useSSLArg.isPresent())
{
if (args.useStartTLSArg.isPresent())
{
Message message = ERR_LDAP_CONN_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(
args.useSSLArg.getLongIdentifier(),
args.useSSLArg.getLongIdentifier());
err.println(wrapText(message, MAX_LINE_WIDTH));
throw new ArgumentException(message);
}
else
{
connectionOptions.setUseSSL(true);
}
}
else if (args.useStartTLSArg.isPresent())
{
connectionOptions.setStartTLS(true);
}
// If we should blindly trust any certificate, then install the appropriate
// SSL connection factory.
if (args.useSSLArg.isPresent() || args.useStartTLSArg.isPresent())
{
try
{
String clientAlias;
if (args.certNicknameArg.isPresent())
{
clientAlias = args.certNicknameArg.getValue();
}
else
{
clientAlias = null;
}
SSLConnectionFactory sslConnectionFactory = new SSLConnectionFactory();
sslConnectionFactory.init(args.trustAllArg.isPresent(),
args.keyStorePathArg.getValue(),
args.keyStorePasswordArg.getValue(),
clientAlias,
args.trustStorePathArg.getValue(),
args.trustStorePasswordArg.getValue());
connectionOptions.setSSLConnectionFactory(sslConnectionFactory);
}
catch (SSLConnectionException sce)
{
Message message =
ERR_LDAP_CONN_CANNOT_INITIALIZE_SSL.get(sce.getMessage());
err.println(wrapText(message, MAX_LINE_WIDTH));
}
}
// If one or more SASL options were provided, then make sure that one of
// them was "mech" and specified a valid SASL mechanism.
if (args.saslOptionArg.isPresent())
{
String mechanism = null;
LinkedList<String> options = new LinkedList<String>();
for (String s : args.saslOptionArg.getValues())
{
int equalPos = s.indexOf('=');
if (equalPos <= 0)
{
Message message = ERR_LDAP_CONN_CANNOT_PARSE_SASL_OPTION.get(s);
err.println(wrapText(message, MAX_LINE_WIDTH));
throw new ArgumentException(message);
}
else
{
String name = s.substring(0, equalPos);
if (name.equalsIgnoreCase("mech"))
{
mechanism = s;
}
else
{
options.add(s);
}
}
}
if (mechanism == null)
{
Message message = ERR_LDAP_CONN_NO_SASL_MECHANISM.get();
err.println(wrapText(message, MAX_LINE_WIDTH));
throw new ArgumentException(message);
}
connectionOptions.setSASLMechanism(mechanism);