From 56bcb6cf46dcb08511d8c20a700e4d5c428a7235 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 18 Nov 2009 12:46:01 +0000
Subject: [PATCH] Fix for issue 3256 (Add certificate settings options in setup CLI) When the user chooses to generate a self-signed certificate in the command-line setup, prompt to provide the host name that will be used to generate the certificate.

---
 opendj-sdk/opends/src/messages/messages/tools.properties                          |    6 ++++++
 opendj-sdk/opends/src/server/org/opends/server/tools/InstallDSArgumentParser.java |   10 ++++++++++
 opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java               |   37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/opendj-sdk/opends/src/messages/messages/tools.properties b/opendj-sdk/opends/src/messages/messages/tools.properties
index f1f2763..9e13b15 100644
--- a/opendj-sdk/opends/src/messages/messages/tools.properties
+++ b/opendj-sdk/opends/src/messages/messages/tools.properties
@@ -2540,4 +2540,10 @@
  argument '%s'
 INFO_DESCRIPTION_SUBENTRIES_1702=Use subentries control to specify that \
  subentries are visible and normal entries are not
+INFO_INSTALLDS_DESCRIPTION_HOST_NAME_1703=Directory server host name or IP \
+ address that will be used to generate the self-signed certificate.  This \
+ argument will be taken into account only if the self-signed certificate \
+ argument %s is specified
+INFO_INSTALLDS_PROMPT_HOST_NAME_1704=Provide the fully-qualified host name or \
+ IP address that will be used to generate the self-signed certificate
 
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java b/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java
index 8dcc98e..1f66e80 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDS.java
@@ -949,6 +949,7 @@
     }
     SecurityOptions securityOptions;
     LinkedList<String> keystoreAliases = new LinkedList<String>();
+    uData.setHostName(argParser.hostNameArg.getValue());
     if (argParser.generateSelfSignedCertificateArg.isPresent())
     {
       securityOptions = SecurityOptions.createSelfSignedCertificateOptions(
@@ -1739,6 +1740,8 @@
     {
       securityOptions = SecurityOptions.createSelfSignedCertificateOptions(
           enableSSL, enableStartTLS, ldapsPort);
+      String hostName = promptForHostNameIfRequired();
+      uData.setHostName(hostName);
     }
     else if (argParser.useJavaKeyStoreArg.isPresent())
     {
@@ -1850,6 +1853,8 @@
         }
         if (certType == SELF_SIGNED)
         {
+          String hostName = promptForHostNameIfRequired();
+          uData.setHostName(hostName);
           securityOptions = SecurityOptions.createSelfSignedCertificateOptions(
                 enableSSL, enableStartTLS, ldapsPort);
         }
@@ -2711,4 +2716,36 @@
       LOG.log(Level.WARNING, "Error resetting arguments: "+t, t);
     }
   }
+
+  private String promptForHostNameIfRequired() throws UserDataException
+  {
+    String hostName = null;
+    if (argParser.hostNameArg.isPresent())
+    {
+      hostName = argParser.hostNameArg.getValue();
+    }
+    else
+    {
+      int nTries = 0;
+      while (hostName == null)
+      {
+        if (nTries >= CONFIRMATION_MAX_TRIES)
+        {
+          throw new UserDataException(null,
+              ERR_TRIES_LIMIT_REACHED.get(CONFIRMATION_MAX_TRIES));
+        }
+
+        try
+        {
+          hostName = readInput(INFO_INSTALLDS_PROMPT_HOST_NAME.get(),
+              argParser.hostNameArg.getDefaultValue());
+        }
+        catch (CLIException ce)
+        {
+          LOG.log(Level.WARNING, "Error reading input: "+ce, ce);
+        }
+      }
+    }
+    return hostName;
+  }
 }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDSArgumentParser.java b/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDSArgumentParser.java
index 98f4a32..26ee6c1 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDSArgumentParser.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/InstallDSArgumentParser.java
@@ -75,6 +75,7 @@
   BooleanArgument   doNotStartArg;
   BooleanArgument   enableStartTLSArg;
   BooleanArgument   generateSelfSignedCertificateArg;
+  StringArgument    hostNameArg;
   BooleanArgument   usePkcs11Arg;
   FileBasedArgument directoryManagerPwdFileArg;
   FileBasedArgument keyStorePasswordFileArg;
@@ -351,6 +352,15 @@
         "generateSelfSignedCertificate");
     addArgument(generateSelfSignedCertificateArg);
 
+    hostNameArg = new StringArgument(OPTION_LONG_HOST.toLowerCase(),
+        OPTION_SHORT_HOST,
+        OPTION_LONG_HOST, false, false, true, INFO_HOST_PLACEHOLDER.get(),
+        UserData.getDefaultHostName(),
+        null, INFO_INSTALLDS_DESCRIPTION_HOST_NAME.get(
+            generateSelfSignedCertificateArg.getLongIdentifier()));
+    hostNameArg.setPropertyName(OPTION_LONG_HOST);
+    addArgument(hostNameArg);
+
     usePkcs11Arg = new BooleanArgument("usePkcs11Keystore".toLowerCase(),
         null, "usePkcs11Keystore",
         INFO_INSTALLDS_DESCRIPTION_USE_PKCS11.get());

--
Gitblit v1.10.0