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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
package com.sun.grid.ca;
import com.sun.grid.security.login.ConsoleCallbackHandler;
import com.sun.grid.security.login.TextCallbackHandler;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Logger;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
/**
* CLI util for the <code>GridCA</code>.
*
* <H3>Usage of the CLI util</H3>
*
*<pre>
* com.sun.grid.ca.Main [options] <command>
*
* Commands:
*
* init initialize CA infrastructure
* user <u:g:e> generate certificates and keys for <u:g:e>
* with u=Unix User, g=Common Name, e=email
* initks <type> <u> <file> [<pwfile>] create keystore for user <u>
* printcert <u> print certificate of a user
* renew <u> [<days>] renew certificate for a user
*
* Mandatory Options:
* -catop <dir> path to the ca root directory
* -calocaltop <dir> path to the local ca root directory
* -cascript <script> path to the sge_ca script
*
* Optional Options:
* -tmp <dir> path tmp files (default system property java.io.tmp)
* -config <dir> path to CA configuration files (default $cadist/util/sgeCA
* -adminuser <user> name of the admin user (default system proeprty user.name)
* -days <days> number of days the certificates are valid (default 365 days)
*
* </pre>
*
*/
public class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
private static GridCA ca;
private static void usage(String msg, int exitCode) {
if(msg != null) {
System.err.println(msg);
}
String usage = ResourceBundle.getBundle(GridCAConstants.BUNDLE).getString("main.usage");
usage = MessageFormat.format(usage, new Object [] { Main.class.getName() });
System.err.println(usage);
System.exit(exitCode);
}
private static int consumeArgument(GridCAConfiguration config, String [] args, int index) throws GridCAException {
if(args[index].equals("-catop")) {
index++;
if(index >= args.length) {
throw new GridCAException("-catop option requires <catop>");
}
config.setCaTop(new File(args[index]));
return ++index;
} else if(args[index].equals("-calocaltop")) {
index++;
if(index >= args.length) {
throw new GridCAException("-calocaltop option requires <calocaltop>");
}
config.setCaLocalTop(new File(args[index]));
return ++index;
} else if(args[index].equals("-tmpdir")) {
index++;
if(index >= args.length) {
throw new GridCAException("-tmp option requires <tmp>");
}
config.setTmpDir(new File(args[index]));
return ++index;
} else if(args[index].equals("-adminuser")) {
index++;
if(index >= args.length) {
throw new GridCAException("-adminuser option requires <adminuser>");
}
config.setAdminUser(args[index]);
return ++index;
} else if(args[index].equals("-cascript")) {
index++;
if(index >= args.length) {
throw new GridCAException("-cascript option requires <script>");
}
config.setSgeCaScript(new File(args[index]));
return ++index;
} else if(args[index].equals("-cahost")) {
index++;
if(index >= args.length) {
throw new GridCAException("-cahost option requires <host>");
}
config.setCaHost(args[index]);
return ++index;
} else if(args[index].equals("-days")) {
index++;
if(index >= args.length) {
throw new GridCAException("-days option requires <days>");
}
try {
config.setDaysValid(Integer.parseInt(args[index]));
} catch (NumberFormatException ne) {
throw new GridCAException("argument for -days must be an integer");
}
return ++index;
} else {
// Unknown option
return 0;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int argIndex = 0;
GridCAConfiguration config = new GridCAConfiguration();
config.setCaHost("localhost");
try {
// Parse the cmdArgs
while(argIndex < args.length) {
int newIndex = consumeArgument(config, args, argIndex);
if(newIndex > 0) {
argIndex = newIndex;
} else {
break;
}
}
ca = GridCAFactory.newInstance(config);
} catch (GridCAException ex) {
usage(ex.getMessage(), 1);
System.exit(1);
}
if(argIndex >= args.length) {
usage("Missing command", 1);
}
String cmd = args[argIndex++];
ArrayList cmdArgs = new ArrayList(args.length-argIndex);
while(argIndex < args.length) {
cmdArgs.add(args[argIndex++]);
}
if ("init".equals(cmd)) {
init();
} else if ("create".equals(cmd)) {
create(cmdArgs);
} else if ("initks".equals(cmd)) {
initks(cmdArgs);
} else if ("printcert".equals(cmd)) {
printCert(cmdArgs);
} else if ("renew".equals(cmd)) {
renew(cmdArgs);
} else {
usage("Unknown command " + cmd, 1);
}
}
private static void init() {
try {
ConsoleCallbackHandler cbh = new ConsoleCallbackHandler();
InitCAParameters params = InitCAParameters.queryNewInstance(cbh);
ca.init(params);
} catch (UnsupportedCallbackException ex) {
IllegalStateException ex1 = new IllegalStateException("unexpected error");
ex1.initCause(ex);
throw ex1;
} catch (IOException ex) {
System.err.println(ex.getMessage());
System.exit(1);
} catch (GridCAException ex) {
ex.printStackTrace();
System.exit(1);
}
}
public static final int TYPE_USER = 1;
public static final int TYPE_DAEMON = 2;
public static final int TYPE_SGE_DAEMON = 3;
public static int parseType(String type) {
if(type.equals("user")) {
return TYPE_USER;
} else if (type.equals("daemon")) {
return TYPE_DAEMON;
} else if (type.equals("sge_daemon")) {
return TYPE_SGE_DAEMON;
} else {
usage("invalid <type> '" + type + "'", 1);
return 0;
}
}
private static void create(ArrayList args) {
if(args.size() != 2) {
usage("Invalid number of arguments for the create command", 1);
}
int type = parseType((String)args.get(0));
String [] userSpec = ((String)args.get(1)).split(":");
if(userSpec.length != 3) {
usage("Invalid user definition '" + args.get(1) + "'", 1);
}
try {
switch(type) {
case TYPE_USER:
ca.createUser(userSpec[0], userSpec[2]);
break;
case TYPE_DAEMON:
ca.createDaemon(userSpec[0], userSpec[1], userSpec[2]);
break;
default:
throw new IllegalStateException("unknown type " + type);
}
} catch (GridCAException ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
}
private static void renew(ArrayList args) {
if(args.size() != 3) {
usage("Invalid number of arguments for renew command", 1);
}
int type = parseType((String)args.get(0));
String name = (String)args.get(1);
int days = -1;
try {
days = Integer.parseInt((String)args.get(2));
} catch(NumberFormatException nfe) {
usage("invalid <days>", 1);
}
try {
X509Certificate cert = null;
switch(type) {
case TYPE_USER:
cert = ca.renewCertificate(name, days);
break;
case TYPE_DAEMON:
cert = ca.renewDaemonCertificate(name, days);
break;
default:
throw new IllegalStateException("unknown type " + type);
}
printCert(cert);
} catch(Exception ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
}
private static void initks(ArrayList args) {
int type = 0;
String name = null;
File keyStoreFile = null;
File pwFile = null;
switch(args.size()) {
case 4:
pwFile = new File((String)args.get(3));
// fall through
case 3:
type = parseType((String)args.get(0));
name = (String)args.get(1);
keyStoreFile = new File((String)args.get(2));
break;
default:
usage("Invalid number of arguments for initks command", 1);
}
try {
KeyStore ks = null;
char [] pw = null;
switch(type) {
case TYPE_USER:
case TYPE_SGE_DAEMON:
if(pwFile == null) {
CallbackHandler cbh = new TextCallbackHandler();
PasswordCallback keystorePWCallback = new PasswordCallback("keystore password: ", false);
Callback [] cb = new Callback [] {
keystorePWCallback,
};
cbh.handle(cb);
pw = keystorePWCallback.getPassword();
if(pw == null) {
pw = new char[0];
}
} else {
FileReader fr = new FileReader(pwFile);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
if(line == null || line.length() == 0) {
pw = new char[0];
} else {
pw = line.toCharArray();
}
}
if (type == TYPE_USER) {
ks = ca.createKeyStore(name, pw, pw);
} else {
ks = ca.createSGEDaemonKeyStore(name, pw, pw);
}
break;
case TYPE_DAEMON:
pw = new char[0];
ks = ca.createDaemonKeyStore(name);
break;
default:
throw new IllegalStateException("unknown type " + type);
}
ks.store(new FileOutputStream(keyStoreFile), pw);
} catch (Exception ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
}
private static void printCert(X509Certificate cert) throws CertificateException {
System.out.println(cert);
}
private static void printCert(ArrayList args) {
if(args.size() != 2) {
usage("Invalid number of arguments for printcert command", 1);
}
int type = parseType((String)args.get(0));
String name = (String)args.get(1);
try {
X509Certificate cert = null;
switch(type) {
case TYPE_USER:
cert = ca.getCertificate(name);
break;
case TYPE_DAEMON:
cert = ca.getDaemonCertificate(name);
break;
default:
throw new IllegalStateException("unknown type " + type);
}
printCert(cert);
} catch(Exception ex) {
System.err.println(ex.getMessage());
System.exit(1);
}
}
}
|