summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/cms/client/cli/MainCLI.java
diff options
context:
space:
mode:
Diffstat (limited to 'base/common/src/com/netscape/cms/client/cli/MainCLI.java')
-rw-r--r--base/common/src/com/netscape/cms/client/cli/MainCLI.java273
1 files changed, 121 insertions, 152 deletions
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);
}
}