From 3b21d8c7f9c756c62d9e3fade67783a4bcd34067 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Tue, 08 Apr 2008 11:07:16 +0000
Subject: [PATCH] Fix for issue 3129 (stop-ds: -j/--bindPasswordFile option doesn't work) In fact the issue applies to most of the task based command-lines (like export-ldif or import-ldif).  THe fix consist basically on taking into account whether the user provided the password in a file or not.

---
 opends/src/server/org/opends/server/tools/ManageAccount.java                    |   38 ++++++++++++++++++
 opends/src/server/org/opends/server/tools/LDAPAuthenticationHandler.java        |    1 
 opends/src/server/org/opends/server/tools/StopDS.java                           |    6 ++-
 opends/src/server/org/opends/server/util/args/LDAPConnectionArgumentParser.java |   21 ++++++++++
 4 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/LDAPAuthenticationHandler.java b/opends/src/server/org/opends/server/tools/LDAPAuthenticationHandler.java
index 40fd29e..a24bec3 100644
--- a/opends/src/server/org/opends/server/tools/LDAPAuthenticationHandler.java
+++ b/opends/src/server/org/opends/server/tools/LDAPAuthenticationHandler.java
@@ -293,6 +293,7 @@
       {
         System.out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(
                 bindDN.stringValue()));
+        System.out.flush();
         char[] pwChars = PasswordReader.readPassword();
         if (pwChars == null)
         {
diff --git a/opends/src/server/org/opends/server/tools/ManageAccount.java b/opends/src/server/org/opends/server/tools/ManageAccount.java
index 0755767..6e7212e 100644
--- a/opends/src/server/org/opends/server/tools/ManageAccount.java
+++ b/opends/src/server/org/opends/server/tools/ManageAccount.java
@@ -50,6 +50,7 @@
 import org.opends.server.util.args.BooleanArgument;
 import org.opends.server.util.args.FileBasedArgument;
 import org.opends.server.util.args.IntegerArgument;
+import org.opends.server.util.args.LDAPConnectionArgumentParser;
 import org.opends.server.util.args.MultiChoiceArgument;
 import org.opends.server.util.args.StringArgument;
 import org.opends.server.util.args.SubCommand;
@@ -1243,6 +1244,40 @@
     connectionOptions.setVersionNumber(3);
 
 
+    //  If both a bind password and bind password file were provided, then
+    // return an error.
+    if (bindPW.isPresent() && bindPWFile.isPresent())
+    {
+      Message message = ERR_PWPSTATE_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(
+              bindPW.getLongIdentifier(),
+              bindPWFile.getLongIdentifier());
+      err.println(wrapText(message, MAX_LINE_WIDTH));
+      return LDAPResultCode.CLIENT_SIDE_PARAM_ERROR;
+    }
+
+    // If both a key store password and key store password file were provided,
+    // then return an error.
+    if (keyStorePW.isPresent() && keyStorePWFile.isPresent())
+    {
+      Message message = ERR_PWPSTATE_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(
+              keyStorePW.getLongIdentifier(),
+              keyStorePWFile.getLongIdentifier());
+      err.println(wrapText(message, MAX_LINE_WIDTH));
+      return LDAPResultCode.CLIENT_SIDE_PARAM_ERROR;
+    }
+
+
+    // If both a trust store password and trust store password file were
+    // provided, then return an error.
+    if (trustStorePW.isPresent() && trustStorePWFile.isPresent())
+    {
+      Message message = ERR_PWPSTATE_MUTUALLY_EXCLUSIVE_ARGUMENTS.get(
+              trustStorePW.getLongIdentifier(),
+              trustStorePWFile.getLongIdentifier());
+      err.println(wrapText(message, MAX_LINE_WIDTH));
+      return LDAPResultCode.CLIENT_SIDE_PARAM_ERROR;
+    }
+
     // See if we should use SSL or StartTLS when establishing the connection.
     // If so, then make sure only one of them was specified.
     if (useSSL.isPresent())
@@ -1353,7 +1388,8 @@
     {
       connection = new LDAPConnection(host.getValue(), port.getIntValue(),
                                       connectionOptions, out, err);
-      connection.connectToHost(bindDN.getValue(), bindPW.getValue(),
+      connection.connectToHost(bindDN.getValue(),
+          LDAPConnectionArgumentParser.getPasswordValue(bindPW, bindPWFile),
                                nextMessageID);
     }
     catch (ArgumentException ae)
diff --git a/opends/src/server/org/opends/server/tools/StopDS.java b/opends/src/server/org/opends/server/tools/StopDS.java
index 02374c1..3b4fc00 100644
--- a/opends/src/server/org/opends/server/tools/StopDS.java
+++ b/opends/src/server/org/opends/server/tools/StopDS.java
@@ -65,6 +65,7 @@
 import org.opends.server.util.args.BooleanArgument;
 import org.opends.server.util.args.FileBasedArgument;
 import org.opends.server.util.args.IntegerArgument;
+import org.opends.server.util.args.LDAPConnectionArgumentParser;
 import org.opends.server.util.args.StringArgument;
 
 import static org.opends.server.config.ConfigConstants.*;
@@ -654,8 +655,9 @@
     {
       connection = new LDAPConnection(host.getValue(), port.getIntValue(),
                                       connectionOptions, out, err);
-      connection.connectToHost(bindDN.getValue(), bindPW.getValue(),
-                               nextMessageID);
+      connection.connectToHost(bindDN.getValue(),
+          LDAPConnectionArgumentParser.getPasswordValue(bindPW, bindPWFile),
+          nextMessageID);
     }
     catch (ArgumentException ae)
     {
diff --git a/opends/src/server/org/opends/server/util/args/LDAPConnectionArgumentParser.java b/opends/src/server/org/opends/server/util/args/LDAPConnectionArgumentParser.java
index 970599d..e5789ac 100644
--- a/opends/src/server/org/opends/server/util/args/LDAPConnectionArgumentParser.java
+++ b/opends/src/server/org/opends/server/util/args/LDAPConnectionArgumentParser.java
@@ -327,7 +327,7 @@
             args.hostNameArg.getValue(),
             args.portArg.getIntValue(),
             args.bindDnArg.getValue(),
-            args.bindPasswordArg.getValue(),
+            getPasswordValue(args.bindPasswordArg, args.bindPasswordFileArg),
             connectionOptions, out, err);
   }
 
@@ -407,6 +407,25 @@
     return args;
   }
 
+  /**
+   * Commodity method that retrieves the password value analyzing the contents
+   * of a string argument and of a file based argument.  It assumes that the
+   * arguments have already been parsed and validated.
+   * @param bindPwdArg the string argument.
+   * @param bindPwdFileArg the file based argument.
+   * @return the password value.
+   */
+  public static String getPasswordValue(StringArgument bindPwdArg,
+      FileBasedArgument bindPwdFileArg)
+  {
+    String pwd = bindPwdArg.getValue();
+    if ((pwd == null) && bindPwdFileArg.isPresent())
+    {
+      pwd = bindPwdFileArg.getValue();
+    }
+    return pwd;
+  }
+
   private void addLdapConnectionArguments(ArgumentGroup argGroup) {
     args = new SecureConnectionCliArgs();
     try {

--
Gitblit v1.10.0