From 569de0b5cfe57748ec244ee19846ce34d0837a1e Mon Sep 17 00:00:00 2001
From: ludovicp <ludovicp@localhost>
Date: Fri, 30 Jul 2010 13:40:30 +0000
Subject: [PATCH] Fix issue 4573 - Admin Connector certificate should use the host name provided by the user in setup. This is achieved by storing the specified hostname in a temporary file under cn=config, which will be used for generating the self-signed certificates and then deleted. If changing or deleting the self-signed certificates, the provided host name is lost and must be manually specified again.

---
 opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java |   77 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java b/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java
index b91ff88..b3268b3 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/util/SetupUtils.java
@@ -22,20 +22,22 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2009 Sun Microsystems, Inc.
+ *      Copyright 2006-2010 Sun Microsystems, Inc.
  */
 package org.opends.server.util;
 
 
-
+import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.UnknownHostException;
 import java.security.KeyStoreException;
 import java.security.cert.Certificate;
 import java.security.cert.CertificateEncodingException;
@@ -44,6 +46,7 @@
 import java.util.Set;
 
 import java.util.Random;
+
 import org.opends.server.types.OperatingSystem;
 
 
@@ -94,6 +97,14 @@
    */
   public static final String LIBRARIES_PATH_RELATIVE = "lib";
 
+  /**
+   * The relative path where the setup stores the name of the host the user
+   * provides. This is used for instance to generate the self-signed admin
+   * certificate the first time the server starts.
+   */
+  public static final String HOST_NAME_FILE = "config" + File.separatorChar
+      + "hostname";
+
   /* These string values must be synchronized with Directory Server's main
    * method.  These string values are considered stable by the server team and
    * not candidates for internationalization. */
@@ -135,6 +146,12 @@
   public static final String BUILD_NUMBER = "Build Number";
 
   /**
+   * A variable used to keep the latest read host name from the file written
+   * by the setup.
+   */
+  private static String lastReadHostName;
+
+  /**
    * Creates a MakeLDIF template file using the provided information.
    *
    * @param  baseDN      The base DN for the data in the template file.
@@ -635,5 +652,61 @@
     return (random.nextInt() & modulo);
   }
 
+  /**
+   * Returns the host name to be used to create self-signed certificates. <br>
+   * The method will first try to read the host name file written by the setup
+   * where the user provided the host name where OpenDS has been installed. If
+   * the file cannot be read, the class {@link java.net.InetAddress} is used.
+   *
+   * @param installationRoot the path where the server is installed.
+   * @return the host name to be used to create self-signed certificates.
+   * @throws UnknownHostException
+   *           if a host name could not be used.
+   */
+  public static String getHostNameForCertificate(
+      String installationRoot) throws UnknownHostException
+  {
+    String hostName = null;
+    File f = new File(installationRoot + File.separator + HOST_NAME_FILE);
+    BufferedReader br = null;
+    try
+    {
+      br = new BufferedReader(new FileReader(f));
+      String s = br.readLine();
+      s = s.trim();
+
+      if (s.length() > 0)
+      {
+        hostName = s;
+        lastReadHostName = hostName;
+      }
+    }
+    catch (IOException ioe)
+    {
+    }
+    finally
+    {
+      try
+      {
+        if (br != null)
+        {
+          br.close();
+        }
+      }
+      catch (Exception e)
+      {
+        // ignore
+      }
+    }
+    if (hostName == null)
+    {
+      hostName = lastReadHostName;
+    }
+    if (hostName == null)
+    {
+      hostName = java.net.InetAddress.getLocalHost().getHostName();
+    }
+    return hostName;
+  }
 }
 

--
Gitblit v1.10.0