From 30667711f68a03daa940d186ab8864f17c62a489 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 code that initializes the database has been
moved from PKIConnection to PKIClient.

Ticket #491
---
 .../src/com/netscape/certsrv/client/PKIClient.java | 43 +++++++++++++++++++---
 .../com/netscape/certsrv/client/PKIConnection.java | 25 -------------
 .../src/com/netscape/cmstools/cli/MainCLI.java     | 33 -----------------
 3 files changed, 38 insertions(+), 63 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/client/PKIClient.java b/base/common/src/com/netscape/certsrv/client/PKIClient.java
index 00b71694be0f19a622990ad28e0ce0b813b13d1b..9f65747e64b3dde6eb8780409dd375dd00455682 100644
--- a/base/common/src/com/netscape/certsrv/client/PKIClient.java
+++ b/base/common/src/com/netscape/certsrv/client/PKIClient.java
@@ -1,5 +1,6 @@
 package com.netscape.certsrv.client;
 
+import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -15,6 +16,7 @@ import org.mozilla.jss.CryptoManager;
 import org.mozilla.jss.CryptoManager.NicknameConflictException;
 import org.mozilla.jss.CryptoManager.NotInitializedException;
 import org.mozilla.jss.CryptoManager.UserCertConflictException;
+import org.mozilla.jss.crypto.AlreadyInitializedException;
 import org.mozilla.jss.crypto.CryptoToken;
 import org.mozilla.jss.crypto.InternalCertificate;
 import org.mozilla.jss.crypto.NoSuchItemOnTokenException;
@@ -22,6 +24,8 @@ import org.mozilla.jss.crypto.ObjectNotFoundException;
 import org.mozilla.jss.crypto.TokenCertificate;
 import org.mozilla.jss.crypto.TokenException;
 import org.mozilla.jss.crypto.X509Certificate;
+import org.mozilla.jss.util.IncorrectPasswordException;
+import org.mozilla.jss.util.Password;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NodeList;
@@ -35,12 +39,46 @@ public class PKIClient {
     public ClientConfig config;
     public PKIConnection connection;
 
+    public CryptoManager manager;
+    public File certDatabase;
+
     public boolean verbose;
 
     public PKIClient(ClientConfig config) {
         this.config = config;
 
         connection = new PKIConnection(this);
+
+        if (config.getCertDatabase() == null) {
+            File homeDir = new File(System.getProperty("user.home"));
+            File pkiDir = new File(homeDir, ".dogtag");
+            certDatabase = new File(pkiDir, "nssdb");
+
+        } else {
+            certDatabase = new File(config.getCertDatabase());
+        }
+
+        certDatabase.mkdirs();
+
+        try {
+            CryptoManager.initialize(certDatabase.getAbsolutePath());
+            manager = CryptoManager.getInstance();
+
+            if (config.getPassword() != null) {
+                CryptoToken token = manager.getInternalKeyStorageToken();
+                Password password = new Password(config.getPassword().toCharArray());
+                token.login(password);
+            }
+
+        } catch (AlreadyInitializedException e) {
+            // ignore
+
+        } catch (IncorrectPasswordException e) {
+            throw new Error("Incorrect certificate database password.", e);
+
+        } catch (Exception t) {
+            throw new Error(t);
+        }
     }
 
     public <T> T createProxy(Class<T> clazz) throws URISyntaxException {
@@ -68,12 +106,10 @@ public class PKIClient {
     }
 
     public X509Certificate[] getCerts() throws NotInitializedException {
-        CryptoManager manager = CryptoManager.getInstance();
         return manager.getPermCerts();
     }
 
     public X509Certificate[] getCACerts() throws NotInitializedException {
-        CryptoManager manager = CryptoManager.getInstance();
         return manager.getCACerts();
     }
 
@@ -98,14 +134,12 @@ public class PKIClient {
             NicknameConflictException, UserCertConflictException,
             NoSuchItemOnTokenException, TokenException {
 
-        CryptoManager manager = CryptoManager.getInstance();
         return manager.importCertPackage(bytes, nickname);
     }
 
     public X509Certificate importCACertPackage(byte[] bytes)
             throws NotInitializedException, CertificateEncodingException, TokenException {
 
-        CryptoManager manager = CryptoManager.getInstance();
         InternalCertificate cert = (InternalCertificate)manager.importCACertPackage(bytes);
 
         cert.setSSLTrust(
@@ -120,7 +154,6 @@ public class PKIClient {
             throws TokenException, ObjectNotFoundException,
             NoSuchItemOnTokenException, NotInitializedException {
 
-        CryptoManager manager = CryptoManager.getInstance();
         X509Certificate cert = manager.findCertByNickname(nickname);
 
         CryptoToken cryptoToken;
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..61e08b2af593369d1633f29644d6883aa83b9ca6 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
@@ -29,11 +29,7 @@ import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.lang.StringUtils;
-import org.mozilla.jss.CryptoManager;
-import org.mozilla.jss.crypto.CryptoToken;
 import org.mozilla.jss.ssl.SSLCertificateApprovalCallback;
-import org.mozilla.jss.util.IncorrectPasswordException;
-import org.mozilla.jss.util.Password;
 
 import com.netscape.certsrv.account.AccountClient;
 import com.netscape.certsrv.client.ClientConfig;
@@ -328,35 +324,6 @@ public class MainCLI extends CLI {
 
         if (verbose) System.out.println("Server URI: "+config.getServerURI());
 
-        // initialize certificate database if specified
-        if (config.getCertDatabase() != null) {
-
-            try {
-                if (verbose) System.out.println("Certificate database: "+config.getCertDatabase());
-                CryptoManager.initialize(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);
-
-                    } catch (IncorrectPasswordException e) {
-                        throw new Error("Incorrect certificate database password.", e);
-                    }
-                }
-
-            } catch (Throwable t) {
-                if (verbose) {
-                    t.printStackTrace(System.err);
-                } else {
-                    System.err.println(t.getClass().getSimpleName()+": "+t.getMessage());
-                }
-                System.exit(1);
-            }
-        }
-
         // execute command
         boolean loggedIn = false;
         try {
-- 
1.8.1.4

