From f420f952326f7a82c7c18b7a21cc975c55e2ecfc Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Fri, 15 Feb 2008 17:24:26 +0000
Subject: [PATCH] Fix for issue 2886 (Cannot get status from running server without authenticating)

---
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java        |    2 
 opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ConfigFromLDAP.java     |   16 +++++
 opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/StatusCli.java          |  100 +++++++++++++++++++++++----------
 opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ServerStatusPooler.java |    9 +--
 opendj-sdk/opends/src/messages/messages/admin_tool.properties                          |    8 +-
 5 files changed, 94 insertions(+), 41 deletions(-)

diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ConfigFromLDAP.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ConfigFromLDAP.java
index e9f276e..d13b39c 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ConfigFromLDAP.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ConfigFromLDAP.java
@@ -223,6 +223,22 @@
       {
         errorMessage = ERR_READING_CONFIG_LDAP.get(detail);
       }
+
+      /*
+       *  Display the information that we find in the off line configuration.
+       */
+      if (listeners.isEmpty())
+      {
+        listeners.addAll(offlineConf.getListeners());
+      }
+      if (databases.isEmpty())
+      {
+        databases.addAll(offlineConf.getDatabases());
+      }
+      if (administrativeUsers.isEmpty())
+      {
+        administrativeUsers.addAll(offlineConf.getAdministrativeUsers());
+      }
     }
     catch (Throwable t)
     {
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ServerStatusPooler.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ServerStatusPooler.java
index e2a4319..14b233a78 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ServerStatusPooler.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/ServerStatusPooler.java
@@ -338,20 +338,17 @@
       {
         if ((dn == null) || (pwd == null))
         {
-          desc.setAdministrativeUsers(new HashSet<String>());
-          desc.setDatabases(new HashSet<DatabaseDescriptor>());
-          desc.setListeners(new HashSet<ListenerDescriptor>());
-          desc.setOpenConnections(-1);
+          updateDescriptorWithOffLineInfo(desc);
         }
         else
         {
           updateDescriptorWithOnLineInfo(desc);
         }
       }
-      catch (Exception ex)
+      catch (Throwable t)
       {
         // Bug
-        ex.printStackTrace();
+        t.printStackTrace();
       }
     }
 
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/StatusCli.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/StatusCli.java
index fa166b0..7e33122 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/StatusCli.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/statuspanel/StatusCli.java
@@ -50,6 +50,7 @@
 
 import static org.opends.quicksetup.util.Utils.*;
 
+import org.opends.server.admin.client.ManagementContext;
 import org.opends.server.admin.client.cli.DsFrameworkCliReturnCode;
 import org.opends.server.admin.client.cli.SecureConnectionCliArgs;
 import org.opends.server.core.DirectoryServer;
@@ -91,6 +92,8 @@
 
   private TrustManager interactiveTrustManager;
 
+  private boolean useInteractiveTrustManager;
+
   /**
    * The enumeration containing the different return codes that the command-line
    * can have.
@@ -313,6 +316,7 @@
        */
       ConfigFromFile offLineConf = new ConfigFromFile();
       offLineConf.readConfiguration();
+      boolean authProvided = false;
       try
       {
         if (isServerRunning)
@@ -323,6 +327,8 @@
           boolean useStartTLS = argParser.useStartTLS();
           if (argParser.isInteractive())
           {
+            ManagementContext ctx = null;
+
             boolean canUseSSL = offLineConf.getLDAPSURL() != null;
             boolean canUseStartTLS = offLineConf.getStartTLSURL() != null;
             // This is done because we do not need to ask the user about these
@@ -388,8 +394,9 @@
               }
               LDAPManagementContextFactory factory =
                 new LDAPManagementContextFactory();
-              factory.getManagementContext(this, ci);
+              ctx = factory.getManagementContext(this, ci);
               interactiveTrustManager = ci.getTrustManager();
+              useInteractiveTrustManager = true;
             }
             catch (ConfigException ce)
             {
@@ -407,39 +414,72 @@
             }
             catch (ClientException e) {
               println(e.getMessageObject());
+              // Display the information in the config file
+              ServerStatusDescriptor desc = createServerStatusDescriptor(null,
+                  null);
+              updateDescriptorWithOffLineInfo(desc, offLineConf);
+              writeStatus(desc);
               return
                 ErrorReturnCode.USER_CANCELLED_OR_DATA_ERROR.getReturnCode();
             }
+            finally
+            {
+              if (ctx != null)
+              {
+                try
+                {
+                  ctx.close();
+                }
+                catch (Throwable t)
+                {
+                }
+              }
+            }
           }
           else
           {
             bindDn = argParser.getBindDN();
             bindPwd = argParser.getBindPassword();
+          }
 
-            if (bindDn == null)
+          authProvided = bindPwd != null;
+
+          if (bindDn == null)
+          {
+            bindDn = "";
+          }
+          if (bindPwd == null)
+          {
+            bindPwd = "";
+          }
+
+          if (authProvided)
+          {
+            ServerStatusDescriptor desc = createServerStatusDescriptor(
+                bindDn, bindPwd);
+            ConfigFromLDAP onLineConf = new ConfigFromLDAP();
+            ConnectionProtocolPolicy policy =
+              ConnectionProtocolPolicy.getConnectionPolicy(useSSL, useStartTLS);
+            onLineConf.setConnectionInfo(offLineConf, policy, bindDn,
+                bindPwd, getTrustManager());
+            onLineConf.readConfiguration();
+            updateDescriptorWithOnLineInfo(desc, onLineConf);
+            writeStatus(desc);
+
+            if (desc.getErrorMessage() != null)
             {
-              bindDn = "";
-            }
-            if (bindPwd == null)
-            {
-              bindPwd = "";
+              return ErrorReturnCode.ERROR_READING_CONFIGURATION_WITH_LDAP.
+              getReturnCode();
             }
           }
-          ServerStatusDescriptor desc = createServerStatusDescriptor(
-              bindDn, bindPwd);
-          ConfigFromLDAP onLineConf = new ConfigFromLDAP();
-          ConnectionProtocolPolicy policy =
-            ConnectionProtocolPolicy.getConnectionPolicy(useSSL, useStartTLS);
-          onLineConf.setConnectionInfo(offLineConf, policy, bindDn,
-              bindPwd, getTrustManager());
-          onLineConf.readConfiguration();
-          updateDescriptorWithOnLineInfo(desc, onLineConf);
-          writeStatus(desc);
-
-          if (desc.getErrorMessage() != null)
+          else
           {
-            return ErrorReturnCode.ERROR_READING_CONFIGURATION_WITH_LDAP.
-            getReturnCode();
+            // The user did not provide authentication: just display the
+            // information we can get reading the config file.
+            ServerStatusDescriptor desc = createServerStatusDescriptor(null,
+                null);
+            updateDescriptorWithOffLineInfo(desc, offLineConf);
+            writeStatus(desc);
           }
         }
         else
@@ -647,7 +687,7 @@
       }
       else
       {
-        if (!desc.isAuthenticated())
+        if (!desc.isAuthenticated() || (desc.getErrorMessage() != null))
         {
           text = getNotAvailableBecauseAuthenticationIsRequiredText();
         }
@@ -714,7 +754,7 @@
     {
       if (desc.getStatus() == ServerStatusDescriptor.ServerStatus.STARTED)
       {
-        if (!desc.isAuthenticated())
+        if (!desc.isAuthenticated() || (desc.getErrorMessage() != null))
         {
           text = getNotAvailableBecauseAuthenticationIsRequiredText();
         }
@@ -778,7 +818,7 @@
       text = Message.raw(desc.getJavaVersion());
       if (text == null)
       {
-        if (!desc.isAuthenticated())
+        if (!desc.isAuthenticated() || (desc.getErrorMessage() != null))
         {
           text = getNotAvailableBecauseAuthenticationIsRequiredText();
         }
@@ -1006,7 +1046,7 @@
         }
         else
         {
-          if (!desc.isAuthenticated())
+          if (!desc.isAuthenticated() || (desc.getErrorMessage() != null))
           {
             s = getNotAvailableBecauseAuthenticationIsRequiredText();
           }
@@ -1101,7 +1141,7 @@
               {
                 value = getNotAvailableBecauseServerIsDownText();
               }
-              if (!desc.isAuthenticated())
+              if (!desc.isAuthenticated() || (desc.getErrorMessage() != null))
               {
                 value = getNotAvailableBecauseAuthenticationIsRequiredText();
               }
@@ -1127,7 +1167,7 @@
           {
             value = getNotAvailableBecauseServerIsDownText();
           }
-          if (!desc.isAuthenticated())
+          if (!desc.isAuthenticated() || (desc.getErrorMessage() != null))
           {
             value = getNotAvailableBecauseAuthenticationIsRequiredText();
           }
@@ -1193,13 +1233,13 @@
    */
   private TrustManager getTrustManager()
   {
-    if (interactiveTrustManager == null)
+    if (useInteractiveTrustManager)
     {
-      return argParser.getTrustManager();
+      return interactiveTrustManager;
     }
     else
     {
-      return interactiveTrustManager;
+      return argParser.getTrustManager();
     }
   }
 
diff --git a/opendj-sdk/opends/src/messages/messages/admin_tool.properties b/opendj-sdk/opends/src/messages/messages/admin_tool.properties
index cccb87a..4b6cdf5 100644
--- a/opendj-sdk/opends/src/messages/messages/admin_tool.properties
+++ b/opendj-sdk/opends/src/messages/messages/admin_tool.properties
@@ -233,14 +233,14 @@
 INFO_NOT_AVAILABLE_AUTHENTICATION_REQUIRED_CLI_LABEL=<not available> (*)
 INFO_NOT_AVAILABLE_SHORT_LABEL=N/A
 INFO_NOT_AVAILABLE_AUTHENTICATION_REQUIRED_CLI_LEGEND=* Information only \
- available if you provide authentication information when launching the status \
- command.
+ available if you provide valid authentication information when launching the \
+ status command.
 INFO_NOT_AVAILABLE_AUTHENTICATION_REQUIRED_TOOLTIP=<html>Information is only \
  available if you are authenticated<br>as an administrative user.
 INFO_NOT_AVAILABLE_SERVER_DOWN_CLI_LABEL=<not available> (*)
 INFO_NOT_AVAILABLE_SERVER_DOWN_CLI_LEGEND=* Information only available if \
- server is running and you provide authentication information when launching \
- the status command.
+ server is running and you provide valid authentication information when \
+ launching the status command.
 INFO_NOT_AVAILABLE_SERVER_DOWN_TOOLTIP=<html>Information is only available if \
  server is running and you are authenticated<br>as an administrative user.
 INFO_NOTHING_SELECTED_TO_UNINSTALL=You must select something to be \
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
index c645cb6..617628f 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/Installer.java
@@ -2213,8 +2213,8 @@
       if ((isADS || isSchema) && isVerbose())
       {
         notifyListeners(getFormattedDone());
+        notifyListeners(getLineBreak());
       }
-      notifyListeners(getLineBreak());
       checkAbort();
     }
   }

--
Gitblit v1.10.0