summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/cms/client/cli/MainCLI.java
blob: 96e1ea1ee093d0c22a6ab02d5fd5431b435bff27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// --- 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 org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
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.Password;

import com.netscape.cms.client.user.UserCLI;

/**
 * @author Endi S. Dewata
 */
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 MainCLI() throws Exception {
        super("pki", "PKI command-line interface");

        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);

        System.out.println();
        System.out.println("Commands:");

        int leftPadding = 1;
        int rightPadding = 18;

        for (CLI plugin : modules.values()) {
            String label = plugin.getName();

            int padding = rightPadding - leftPadding - label.length();
            if (padding < 1) padding = 1;

            System.out.print(StringUtils.repeat(" ", leftPadding));
            System.out.print(label);
            System.out.print(StringUtils.repeat(" ", padding));
            System.out.println(plugin.getDescription());
        }
    }

    public void printHelpCommand(String pluginName) {
        CLI plugin = getModule(pluginName);
        plugin.printHelp();
    }

    public void execute(String[] args) throws Exception {

        Option option = new Option("U", true, "URL");
        option.setArgName("url");
        options.addOption(option);

        option = new Option("P", true, "Protocol (default: http)");
        option.setArgName("protocol");
        options.addOption(option);

        option = new Option("h", true, "Hostname (default: localhost)");
        option.setArgName("hostname");
        options.addOption(option);

        option = new Option("p", true, "Port (default: 9180)");
        option.setArgName("port");
        options.addOption(option);

        option = new Option("t", true, "Subsystem type (default: ca)");
        option.setArgName("type");
        options.addOption(option);

        option = new Option("d", true, "Certificate database directory");
        option.setArgName("directory");
        options.addOption(option);

        option = new Option("w", true, "Certificate database password");
        option.setArgName("password");
        options.addOption(option);

        option = new Option("n", true, "Certificate nickname");
        option.setArgName("cert");
        options.addOption(option);

        options.addOption("v", false, "Verbose");
        options.addOption(null, "help", false, "Help");

        CommandLine cmd = null;

        try {
            cmd = parser.parse(options, args, true);

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            printHelp();
            System.exit(1);
        }

        String[] cmdArgs = cmd.getArgs();

        if (cmd.hasOption("help") || cmdArgs.length == 0) {
            printHelp();
            System.exit(1);
        }

        verbose = cmd.hasOption("v");

        url = cmd.getOptionValue("U");
        protocol = cmd.getOptionValue("P", "http");
        hostname = cmd.getOptionValue("h", "localhost");
        port = cmd.getOptionValue("p", "9180");
        type = cmd.getOptionValue("t", "ca");

        if (url == null) {
            url = protocol + "://" + hostname + ":" + port + "/" + type;
        }

        if (verbose) System.out.println("Server URL: "+url);

        certDBDirectory = cmd.getOptionValue("d");
        certDBPassword = cmd.getOptionValue("w");
        certNickname = cmd.getOptionValue("n");

        if (certDBDirectory != null && certDBPassword != null) {

            if (verbose) System.out.println("Certificate DB: "+certDBDirectory);

            try {
                CryptoManager.initialize(certDBDirectory);
            } catch (AlreadyInitializedException e) {
                // ignore
            }

            CryptoManager manager = CryptoManager.getInstance();
            CryptoToken token = manager.getInternalKeyStorageToken();
            Password password = new Password(certDBPassword.toCharArray());

            try {
                token.login(password);
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
                if (!token.isLoggedIn()) {
                    token.initPassword(password, password);
                }
            }
        }

        if (verbose) {
            System.out.print("Command:");
            for (String arg : cmdArgs) {
                System.out.print(" "+arg);
            }
            System.out.println();
        }

        // command-line args: <command> [command args...]
        if (cmdArgs.length == 0) {
            printHelp();
            System.exit(1);
        }

        String command = cmdArgs[0];

        String moduleName;
        String moduleCommand;

        // 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;
        }

        // get command module
        CLI module = getModule(moduleName);
        if (module == null) {
            System.err.println("Error: Invalid command \"" + command + "\"");
            printHelp();
            System.exit(1);
        }

        // 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 {
            module.execute(moduleArgs);

        } catch (Throwable t) {
            if (verbose) {
                t.printStackTrace();
            } else {
                System.err.println(t.getClass().getSimpleName()+": "+t.getMessage());
            }
        }
    }

    public static void printMessage(String message) {
        System.out.println(StringUtils.repeat("-", message.length()));
        System.out.println(message);
        System.out.println(StringUtils.repeat("-", message.length()));
    }

    public static void main(String args[]) throws Exception {
        MainCLI cli = new MainCLI();
        cli.execute(args);
    }
}