summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/cms/client/cli
diff options
context:
space:
mode:
Diffstat (limited to 'base/common/src/com/netscape/cms/client/cli')
-rw-r--r--base/common/src/com/netscape/cms/client/cli/ClientConfig.java173
-rw-r--r--base/common/src/com/netscape/cms/client/cli/MainCLI.java273
2 files changed, 294 insertions, 152 deletions
diff --git a/base/common/src/com/netscape/cms/client/cli/ClientConfig.java b/base/common/src/com/netscape/cms/client/cli/ClientConfig.java
new file mode 100644
index 000000000..bad8ba626
--- /dev/null
+++ b/base/common/src/com/netscape/cms/client/cli/ClientConfig.java
@@ -0,0 +1,173 @@
+// --- BEGIN COPYRIGHT BLOCK ---
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program; if not, write to the Free Software Foundation, Inc.,
+// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+// (C) 2012 Red Hat, Inc.
+// All rights reserved.
+// --- END COPYRIGHT BLOCK ---
+
+package com.netscape.cms.client.cli;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * @author Endi S. Dewata
+ */
+@XmlRootElement(name="Client")
+public class ClientConfig {
+
+ public static Marshaller marshaller;
+ public static Unmarshaller unmarshaller;
+
+ static {
+ try {
+ marshaller = JAXBContext.newInstance(ClientConfig.class).createMarshaller();
+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+ unmarshaller = JAXBContext.newInstance(ClientConfig.class).createUnmarshaller();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ URI serverURI;
+
+ String certDatabase;
+ String certNickname;
+ String password;
+
+ @XmlElement(name="ServerURI")
+ public URI getServerURI() {
+ return serverURI;
+ }
+
+ public void setServerURI(String serverUri) throws URISyntaxException {
+ this.serverURI = new URI(serverUri);
+ }
+
+ public void setServerURI(URI serverUri) {
+ this.serverURI = serverUri;
+ }
+
+ @XmlElement(name="CertDatabase")
+ public String getCertDatabase() {
+ return certDatabase;
+ }
+
+ public void setCertDatabase(String certDatabase) {
+ this.certDatabase = certDatabase;
+ }
+
+ @XmlElement(name="CertNickname")
+ public String getCertNickname() {
+ return certNickname;
+ }
+
+ public void setCertNickname(String certNickname) {
+ this.certNickname = certNickname;
+ }
+
+ @XmlElement(name="Password")
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((certDatabase == null) ? 0 : certDatabase.hashCode());
+ result = prime * result + ((certNickname == null) ? 0 : certNickname.hashCode());
+ result = prime * result + ((password == null) ? 0 : password.hashCode());
+ result = prime * result + ((serverURI == null) ? 0 : serverURI.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ ClientConfig other = (ClientConfig) obj;
+ if (certDatabase == null) {
+ if (other.certDatabase != null)
+ return false;
+ } else if (!certDatabase.equals(other.certDatabase))
+ return false;
+ if (certNickname == null) {
+ if (other.certNickname != null)
+ return false;
+ } else if (!certNickname.equals(other.certNickname))
+ return false;
+ if (password == null) {
+ if (other.password != null)
+ return false;
+ } else if (!password.equals(other.password))
+ return false;
+ if (serverURI == null) {
+ if (other.serverURI != null)
+ return false;
+ } else if (!serverURI.equals(other.serverURI))
+ return false;
+ return true;
+ }
+
+ public String toString() {
+ try {
+ StringWriter sw = new StringWriter();
+ marshaller.marshal(this, sw);
+ return sw.toString();
+
+ } catch (Exception e) {
+ return super.toString();
+ }
+ }
+
+ public static ClientConfig valueOf(String string) throws Exception {
+ try {
+ return (ClientConfig)unmarshaller.unmarshal(new StringReader(string));
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ public static void main(String args[]) throws Exception {
+
+ ClientConfig before = new ClientConfig();
+ before.setServerURI("http://localhost:9180/ca");
+ before.setCertDatabase("certs");
+ before.setCertNickname("caadmin");
+ before.setPassword("12345");
+
+ String string = before.toString();
+ System.out.println(string);
+
+ ClientConfig after = ClientConfig.valueOf(string);
+ System.out.println(before.equals(after));
+ }
+}
diff --git a/base/common/src/com/netscape/cms/client/cli/MainCLI.java b/base/common/src/com/netscape/cms/client/cli/MainCLI.java
index 55cac0b64..0367cbbfd 100644
--- a/base/common/src/com/netscape/cms/client/cli/MainCLI.java
+++ b/base/common/src/com/netscape/cms/client/cli/MainCLI.java
@@ -18,12 +18,16 @@
package com.netscape.cms.client.cli;
+import java.io.File;
+import java.net.URISyntaxException;
+
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.AlreadyInitializedException;
import org.mozilla.jss.crypto.CryptoToken;
+import org.mozilla.jss.util.IncorrectPasswordException;
import org.mozilla.jss.util.Password;
import com.netscape.cms.client.cert.CertCLI;
@@ -35,16 +39,7 @@ import com.netscape.cms.client.user.UserCLI;
*/
public class MainCLI extends CLI {
- public String protocol;
- public String hostname;
- public String port;
- public String type;
-
- public String certDBDirectory;
- public String certDBPassword;
- public String certNickname;
-
- public String url;
+ public ClientConfig config = new ClientConfig();
public MainCLI() throws Exception {
super("pki", "PKI command-line interface");
@@ -54,65 +49,9 @@ public class MainCLI extends CLI {
addModule(new UserCLI(this));
}
- public String getProtocol() {
- return protocol;
- }
-
- public void setProtocol(String protocol) {
- this.protocol = protocol;
- }
-
- public String getHostname() {
- return hostname;
- }
-
- public void setHostname(String hostname) {
- this.hostname = hostname;
- }
-
- public String getPort() {
- return port;
- }
-
- public void setPort(String port) {
- this.port = port;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public String getCertDBDirectory() {
- return certDBDirectory;
- }
-
- public void setCertDBDirectory(String certDBDirectory) {
- this.certDBDirectory = certDBDirectory;
- }
-
- public String getCertDBPassword() {
- return certDBPassword;
- }
-
- public void setCertDBPassword(String certDBPassword) {
- this.certDBPassword = certDBPassword;
- }
-
- public String getCertNickname() {
- return certNickname;
- }
-
- public void setCertNickname(String certNickname) {
- this.certNickname = certNickname;
- }
-
public void printHelp() {
- formatter.printHelp(getName()+" [OPTIONS..] <command> [ARGS..]", options);
+ formatter.printHelp(name+" [OPTIONS..] <command> [ARGS..]", options);
System.out.println();
System.out.println("Commands:");
@@ -138,10 +77,10 @@ public class MainCLI extends CLI {
plugin.printHelp();
}
- public void execute(String[] args) throws Exception {
+ public void createOptions(Options options) {
- Option option = new Option("U", true, "URL");
- option.setArgName("url");
+ Option option = new Option("U", true, "Server URI");
+ option.setArgName("uri");
options.addOption(option);
option = new Option("P", true, "Protocol (default: http)");
@@ -160,133 +99,163 @@ public class MainCLI extends CLI {
option.setArgName("type");
options.addOption(option);
- option = new Option("d", true, "Certificate database directory");
- option.setArgName("directory");
+ option = new Option("d", true, "Certificate database");
+ option.setArgName("database");
options.addOption(option);
- option = new Option("w", true, "Certificate database password");
- option.setArgName("password");
+ option = new Option("n", true, "Certificate nickname");
+ option.setArgName("nickname");
options.addOption(option);
- option = new Option("n", true, "Certificate nickname");
- option.setArgName("cert");
+ option = new Option("w", true, "Password");
+ option.setArgName("password");
options.addOption(option);
options.addOption("v", false, "Verbose");
options.addOption(null, "help", false, "Help");
+ }
- CommandLine cmd = null;
+ public void parseOptions(CommandLine cmd) throws URISyntaxException {
- try {
- cmd = parser.parse(options, args, true);
+ String uri = cmd.getOptionValue("U");
- } catch (Exception e) {
- System.err.println("Error: " + e.getMessage());
- printHelp();
- System.exit(1);
- }
+ String protocol = cmd.getOptionValue("P", "http");
+ String hostname = cmd.getOptionValue("h", "localhost");
+ String port = cmd.getOptionValue("p", "8080");
+ String type = cmd.getOptionValue("t", "ca");
- String[] cmdArgs = cmd.getArgs();
+ if (uri == null)
+ uri = protocol + "://" + hostname + ":" + port + "/" + type;
- if (cmd.hasOption("help") || cmdArgs.length == 0) {
- printHelp();
- System.exit(1);
- }
+ config.setServerURI(uri);
- verbose = cmd.hasOption("v");
+ String certDatabase = cmd.getOptionValue("d");
+ String certNickname = cmd.getOptionValue("n");
+ String password = cmd.getOptionValue("w");
- url = cmd.getOptionValue("U");
- protocol = cmd.getOptionValue("P", "http");
- hostname = cmd.getOptionValue("h", "localhost");
- port = cmd.getOptionValue("p", "9180");
- type = cmd.getOptionValue("t", "ca");
+ // convert into absolute path
+ if (certDatabase != null)
+ config.setCertDatabase(new File(certDatabase).getAbsolutePath());
- if (url == null) {
- url = protocol + "://" + hostname + ":" + port + "/" + type;
- }
+ if (certNickname != null)
+ config.setCertNickname(certNickname);
- if (verbose) System.out.println("Server URL: "+url);
+ if (password != null)
+ config.setPassword(password);
+ }
- certDBDirectory = cmd.getOptionValue("d");
- certDBPassword = cmd.getOptionValue("w");
- certNickname = cmd.getOptionValue("n");
+ public void execute(String[] args) throws Exception {
- if (certDBDirectory != null && certDBPassword != null) {
+ CLI module;
+ String[] moduleArgs;
- if (verbose) System.out.println("Certificate DB: "+certDBDirectory);
+ try {
+ createOptions(options);
+ CommandLine cmd;
try {
- CryptoManager.initialize(certDBDirectory);
- } catch (AlreadyInitializedException e) {
- // ignore
+ cmd = parser.parse(options, args, true);
+ } catch (Exception e) {
+ throw new Error(e.getMessage(), e);
}
- CryptoManager manager = CryptoManager.getInstance();
- CryptoToken token = manager.getInternalKeyStorageToken();
- Password password = new Password(certDBPassword.toCharArray());
+ String[] cmdArgs = cmd.getArgs();
- try {
- token.login(password);
- } catch (Exception e) {
- System.err.println("Error: " + e.getMessage());
- if (!token.isLoggedIn()) {
- token.initPassword(password, password);
- }
+ if (cmdArgs.length == 0 || cmd.hasOption("help")) {
+ printHelp();
+ System.exit(1);
}
- }
- if (verbose) {
- System.out.print("Command:");
- for (String arg : cmdArgs) {
- System.out.print(" "+arg);
+ verbose = cmd.hasOption("v");
+
+ if (verbose) {
+ System.out.print("Command:");
+ for (String arg : cmdArgs) {
+ if (arg.contains(" ")) arg = "\""+arg+"\"";
+ System.out.print(" "+arg);
+ }
+ System.out.println();
}
- System.out.println();
- }
- // command-line args: <command> [command args...]
- if (cmdArgs.length == 0) {
- printHelp();
- System.exit(1);
- }
+ parseOptions(cmd);
- String command = cmdArgs[0];
+ String command = cmdArgs[0];
+ String moduleName;
+ String moduleCommand;
- String moduleName;
- String moduleCommand;
+ // If a command contains a '-' sign it will be
+ // split into module name and module command.
+ // Otherwise it's a single command.
+ int i = command.indexOf('-');
+ if (i >= 0) { // <module name>-<module command>
+ moduleName = command.substring(0, i);
+ moduleCommand = command.substring(i+1);
- // parse command: <module name>-<module command>
- int i = command.indexOf('-');
- if (i >= 0) {
- moduleName = command.substring(0, i);
- moduleCommand = command.substring(i+1);
- } else {
- moduleName = command;
- moduleCommand = null;
- }
+ } else { // <command>
+ moduleName = command;
+ moduleCommand = null;
+ }
+
+ // get command module
+ module = getModule(moduleName);
+ if (module == null)
+ throw new Error("Invalid command \"" + command + "\".");
+
+ // prepare module arguments
+ if (moduleCommand != null) {
+ moduleArgs = new String[cmdArgs.length];
+ moduleArgs[0] = moduleCommand;
+ System.arraycopy(cmdArgs, 1, moduleArgs, 1, cmdArgs.length-1);
+
+ } else {
+ moduleArgs = new String[cmdArgs.length-1];
+ System.arraycopy(cmdArgs, 1, moduleArgs, 0, cmdArgs.length-1);
+ }
- // get command module
- CLI module = getModule(moduleName);
- if (module == null) {
- System.err.println("Error: Invalid command \"" + command + "\"");
+ } catch (Throwable t) {
+ if (verbose) {
+ t.printStackTrace(System.err);
+ } else {
+ System.err.println(t.getClass().getSimpleName()+": "+t.getMessage());
+ }
printHelp();
System.exit(1);
+ return;
}
- // prepare module arguments
- String[] moduleArgs = new String[cmdArgs.length];
- moduleArgs[0] = moduleCommand;
- System.arraycopy(cmdArgs, 1, moduleArgs, 1, cmdArgs.length-1);
-
// execute module command
try {
+ if (verbose) System.out.println("Server URI: "+config.getServerURI());
+
+ // initialize certificate database if specified
+ if (config.getCertDatabase() != null) {
+
+ 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);
+ }
+ }
+ }
+
+ // execute module command
module.execute(moduleArgs);
} catch (Throwable t) {
if (verbose) {
- t.printStackTrace();
+ t.printStackTrace(System.err);
} else {
System.err.println(t.getClass().getSimpleName()+": "+t.getMessage());
}
+ System.exit(1);
}
}