From 9ae6df3ffce37dd1ca1e70d68b6fd32245c86fea Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Wed, 24 Apr 2013 19:54:13 -0400
Subject: [PATCH] Updated default client database location for CLI.

The default client database location for CLI has been changed to
~/.dogtag/nssdb. The database will always be initialized regardless
whether it is actually used.

Ticket #491
---
 .../com/netscape/certsrv/client/PKIConnection.java | 25 -----------
 .../src/com/netscape/cmstools/cli/MainCLI.java     | 52 +++++++++++++---------
 2 files changed, 31 insertions(+), 46 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/client/PKIConnection.java b/base/common/src/com/netscape/certsrv/client/PKIConnection.java
index 62d54953221d758abe4898c823304d550c5ff75f..c86fd3ade81254865a39fc533264bab88b66eb0d 100644
--- a/base/common/src/com/netscape/certsrv/client/PKIConnection.java
+++ b/base/common/src/com/netscape/certsrv/client/PKIConnection.java
@@ -61,8 +61,6 @@ import org.jboss.resteasy.client.core.BaseClientResponse;
 import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;
 import org.jboss.resteasy.client.core.extractors.ClientErrorHandler;
 import org.jboss.resteasy.spi.ResteasyProviderFactory;
-import org.mozilla.jss.CryptoManager;
-import org.mozilla.jss.crypto.AlreadyInitializedException;
 import org.mozilla.jss.crypto.X509Certificate;
 import org.mozilla.jss.ssl.SSLCertificateApprovalCallback;
 import org.mozilla.jss.ssl.SSLSocket;
@@ -458,29 +456,6 @@ public class PKIConnection {
                 UnknownHostException,
                 ConnectTimeoutException {
 
-            // Initialize JSS before using SSLSocket,
-            // otherwise it will throw UnsatisfiedLinkError.
-            if (config.getCertDatabase() == null) {
-                try {
-                    // No database specified, use $HOME/.pki/nssdb.
-                    File homeDir = new File(System.getProperty("user.home"));
-                    File pkiDir = new File(homeDir, ".pki");
-                    File nssdbDir = new File(pkiDir, "nssdb");
-                    nssdbDir.mkdirs();
-
-                    CryptoManager.initialize(nssdbDir.getAbsolutePath());
-
-                } catch (AlreadyInitializedException e) {
-                    // ignore
-
-                } catch (Exception e) {
-                    throw new Error(e);
-                }
-
-            } else {
-                // Database specified, already initialized by the main program.
-            }
-
             String hostName = null;
             int port = 0;
             if (remoteAddress != null) {
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
index 8a9f544c28f4383d4c75ec12610a75316528e2ed..d5707d155809ccbdb19e6d02c542faab1292b7b1 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
@@ -57,6 +57,8 @@ public class MainCLI extends CLI {
     public Collection<Integer> rejectedCertStatuses;
     public Collection<Integer> ignoredCertStatuses;
 
+    public File certDatabase;
+
     public PKIClient client;
     public PKIConnection connection;
     public AccountClient accountClient;
@@ -328,33 +330,41 @@ public class MainCLI extends CLI {
 
         if (verbose) System.out.println("Server URI: "+config.getServerURI());
 
-        // initialize certificate database if specified
-        if (config.getCertDatabase() != null) {
+        // initialize certificate database
+        if (config.getCertDatabase() == null) {
+            this.certDatabase = new File(
+                    System.getProperty("user.home") + File.separator +
+                    ".dogtag" + File.separator + "nssdb");
 
-            try {
-                if (verbose) System.out.println("Certificate database: "+config.getCertDatabase());
-                CryptoManager.initialize(config.getCertDatabase());
+        } else {
+            this.certDatabase = new File(config.getCertDatabase());
+        }
 
-                if (config.getPassword() != null) {
-                    try {
-                        CryptoManager manager = CryptoManager.getInstance();
-                        CryptoToken token = manager.getInternalKeyStorageToken();
-                        Password password = new Password(config.getPassword().toCharArray());
-                        token.login(password);
+        certDatabase.mkdirs();
 
-                    } catch (IncorrectPasswordException e) {
-                        throw new Error("Incorrect certificate database password.", e);
-                    }
-                }
+        try {
+            if (verbose) System.out.println("Certificate database: "+certDatabase.getAbsolutePath());
+            CryptoManager.initialize(certDatabase.getAbsolutePath());
+
+            if (config.getPassword() != null) {
+                try {
+                    CryptoManager manager = CryptoManager.getInstance();
+                    CryptoToken token = manager.getInternalKeyStorageToken();
+                    Password password = new Password(config.getPassword().toCharArray());
+                    token.login(password);
 
-            } catch (Throwable t) {
-                if (verbose) {
-                    t.printStackTrace(System.err);
-                } else {
-                    System.err.println(t.getClass().getSimpleName()+": "+t.getMessage());
+                } catch (IncorrectPasswordException e) {
+                    throw new Error("Incorrect certificate database password.", e);
                 }
-                System.exit(1);
             }
+
+        } catch (Throwable t) {
+            if (verbose) {
+                t.printStackTrace(System.err);
+            } else {
+                System.err.println(t.getClass().getSimpleName()+": "+t.getMessage());
+            }
+            System.exit(1);
         }
 
         // execute command
-- 
1.8.1.4

