security/nss/cmd/signtool/signtool.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * SIGNTOOL
michael@0 7 *
michael@0 8 * A command line tool to create manifest files
michael@0 9 * from a directory hierarchy. It is assumed that
michael@0 10 * the tree will be equivalent to what resides
michael@0 11 * or will reside in an archive.
michael@0 12 *
michael@0 13 *
michael@0 14 */
michael@0 15
michael@0 16 #include "nss.h"
michael@0 17 #include "signtool.h"
michael@0 18 #include "prmem.h"
michael@0 19 #include "prio.h"
michael@0 20
michael@0 21 /***********************************************************************
michael@0 22 * Global Variable Definitions
michael@0 23 */
michael@0 24 char *progName; /* argv[0] */
michael@0 25
michael@0 26 /* password data */
michael@0 27 secuPWData pwdata = { PW_NONE, 0 };
michael@0 28
michael@0 29 /* directories or files to exclude in descent */
michael@0 30 PLHashTable *excludeDirs = NULL;
michael@0 31 static PRBool exclusionsGiven = PR_FALSE;
michael@0 32
michael@0 33 /* zatharus is the man who knows no time, dies tragic death */
michael@0 34 int no_time = 0;
michael@0 35
michael@0 36 /* -b basename of .rsa, .sf files */
michael@0 37 char *base = DEFAULT_BASE_NAME;
michael@0 38
michael@0 39 /* Only sign files with this extension */
michael@0 40 PLHashTable *extensions = NULL;
michael@0 41 PRBool extensionsGiven = PR_FALSE;
michael@0 42
michael@0 43 char *scriptdir = NULL;
michael@0 44
michael@0 45 int verbosity = 0;
michael@0 46
michael@0 47 PRFileDesc *outputFD = NULL, *errorFD = NULL;
michael@0 48
michael@0 49 int errorCount = 0, warningCount = 0;
michael@0 50
michael@0 51 int compression_level = DEFAULT_COMPRESSION_LEVEL;
michael@0 52 PRBool compression_level_specified = PR_FALSE;
michael@0 53
michael@0 54 int xpi_arc = 0;
michael@0 55
michael@0 56 /* Command-line arguments */
michael@0 57 static char *genkey = NULL;
michael@0 58 static char *verify = NULL;
michael@0 59 static char *zipfile = NULL;
michael@0 60 static char *cert_dir = NULL;
michael@0 61 static int javascript = 0;
michael@0 62 static char *jartree = NULL;
michael@0 63 static char *keyName = NULL;
michael@0 64 static char *metafile = NULL;
michael@0 65 static char *install_script = NULL;
michael@0 66 static int list_certs = 0;
michael@0 67 static int list_modules = 0;
michael@0 68 static int optimize = 0;
michael@0 69 static int enableOCSP = 0;
michael@0 70 static char *tell_who = NULL;
michael@0 71 static char *outfile = NULL;
michael@0 72 static char *cmdFile = NULL;
michael@0 73 static PRBool noRecurse = PR_FALSE;
michael@0 74 static PRBool leaveArc = PR_FALSE;
michael@0 75 static int keySize = -1;
michael@0 76 static char *token = NULL;
michael@0 77
michael@0 78 typedef enum {
michael@0 79 UNKNOWN_OPT,
michael@0 80 HELP_OPT,
michael@0 81 LONG_HELP_OPT,
michael@0 82 BASE_OPT,
michael@0 83 COMPRESSION_OPT,
michael@0 84 CERT_DIR_OPT,
michael@0 85 EXTENSION_OPT,
michael@0 86 INSTALL_SCRIPT_OPT,
michael@0 87 SCRIPTDIR_OPT,
michael@0 88 CERTNAME_OPT,
michael@0 89 LIST_OBJSIGN_CERTS_OPT,
michael@0 90 LIST_ALL_CERTS_OPT,
michael@0 91 METAFILE_OPT,
michael@0 92 OPTIMIZE_OPT,
michael@0 93 ENABLE_OCSP_OPT,
michael@0 94 PASSWORD_OPT,
michael@0 95 VERIFY_OPT,
michael@0 96 WHO_OPT,
michael@0 97 EXCLUDE_OPT,
michael@0 98 NO_TIME_OPT,
michael@0 99 JAVASCRIPT_OPT,
michael@0 100 ZIPFILE_OPT,
michael@0 101 GENKEY_OPT,
michael@0 102 MODULES_OPT,
michael@0 103 NORECURSE_OPT,
michael@0 104 SIGNDIR_OPT,
michael@0 105 OUTFILE_OPT,
michael@0 106 COMMAND_FILE_OPT,
michael@0 107 LEAVE_ARC_OPT,
michael@0 108 VERBOSITY_OPT,
michael@0 109 KEYSIZE_OPT,
michael@0 110 TOKEN_OPT,
michael@0 111 XPI_ARC_OPT
michael@0 112 }
michael@0 113
michael@0 114
michael@0 115 OPT_TYPE;
michael@0 116
michael@0 117 typedef enum {
michael@0 118 DUPLICATE_OPTION_ERR = 0,
michael@0 119 OPTION_NEEDS_ARG_ERR
michael@0 120 }
michael@0 121
michael@0 122
michael@0 123 Error;
michael@0 124
michael@0 125 static char *errStrings[] = {
michael@0 126 "warning: %s option specified more than once.\n"
michael@0 127 "Only last specification will be used.\n",
michael@0 128 "ERROR: option \"%s\" requires an argument.\n"
michael@0 129 };
michael@0 130
michael@0 131
michael@0 132 static int ProcessOneOpt(OPT_TYPE type, char *arg);
michael@0 133
michael@0 134 /*********************************************************************
michael@0 135 *
michael@0 136 * P r o c e s s C o m m a n d F i l e
michael@0 137 */
michael@0 138 int
michael@0 139 ProcessCommandFile()
michael@0 140 {
michael@0 141 PRFileDesc * fd;
michael@0 142 #define CMD_FILE_BUFSIZE 1024
michael@0 143 char buf[CMD_FILE_BUFSIZE];
michael@0 144 char *equals;
michael@0 145 int linenum = 0;
michael@0 146 int retval = -1;
michael@0 147 OPT_TYPE type;
michael@0 148
michael@0 149 fd = PR_Open(cmdFile, PR_RDONLY, 0777);
michael@0 150 if (!fd) {
michael@0 151 PR_fprintf(errorFD, "ERROR: Unable to open command file %s.\n");
michael@0 152 errorCount++;
michael@0 153 return - 1;
michael@0 154 }
michael@0 155
michael@0 156 while (pr_fgets(buf, CMD_FILE_BUFSIZE, fd)) {
michael@0 157 char *eol;
michael@0 158 linenum++;
michael@0 159
michael@0 160 /* Chop off final newline */
michael@0 161 eol = PL_strchr(buf, '\r');
michael@0 162 if (!eol) {
michael@0 163 eol = PL_strchr(buf, '\n');
michael@0 164 }
michael@0 165 if (eol)
michael@0 166 *eol = '\0';
michael@0 167
michael@0 168 equals = PL_strchr(buf, '=');
michael@0 169 if (!equals) {
michael@0 170 continue;
michael@0 171 }
michael@0 172
michael@0 173 *equals = '\0';
michael@0 174 equals++;
michael@0 175
michael@0 176 /* Now buf points to the attribute, and equals points to the value. */
michael@0 177
michael@0 178 /* This is pretty straightforward, just deal with whatever attribute
michael@0 179 * this is */
michael@0 180 if (!PL_strcasecmp(buf, "basename")) {
michael@0 181 type = BASE_OPT;
michael@0 182 } else if (!PL_strcasecmp(buf, "compression")) {
michael@0 183 type = COMPRESSION_OPT;
michael@0 184 } else if (!PL_strcasecmp(buf, "certdir")) {
michael@0 185 type = CERT_DIR_OPT;
michael@0 186 } else if (!PL_strcasecmp(buf, "extension")) {
michael@0 187 type = EXTENSION_OPT;
michael@0 188 } else if (!PL_strcasecmp(buf, "generate")) {
michael@0 189 type = GENKEY_OPT;
michael@0 190 } else if (!PL_strcasecmp(buf, "installScript")) {
michael@0 191 type = INSTALL_SCRIPT_OPT;
michael@0 192 } else if (!PL_strcasecmp(buf, "javascriptdir")) {
michael@0 193 type = SCRIPTDIR_OPT;
michael@0 194 } else if (!PL_strcasecmp(buf, "htmldir")) {
michael@0 195 type = JAVASCRIPT_OPT;
michael@0 196 if (jartree) {
michael@0 197 PR_fprintf(errorFD,
michael@0 198 "warning: directory to be signed specified more than once."
michael@0 199 " Only last specification will be used.\n");
michael@0 200 warningCount++;
michael@0 201 PR_Free(jartree);
michael@0 202 jartree = NULL;
michael@0 203 }
michael@0 204 jartree = PL_strdup(equals);
michael@0 205 } else if (!PL_strcasecmp(buf, "certname")) {
michael@0 206 type = CERTNAME_OPT;
michael@0 207 } else if (!PL_strcasecmp(buf, "signdir")) {
michael@0 208 type = SIGNDIR_OPT;
michael@0 209 } else if (!PL_strcasecmp(buf, "list")) {
michael@0 210 type = LIST_OBJSIGN_CERTS_OPT;
michael@0 211 } else if (!PL_strcasecmp(buf, "listall")) {
michael@0 212 type = LIST_ALL_CERTS_OPT;
michael@0 213 } else if (!PL_strcasecmp(buf, "metafile")) {
michael@0 214 type = METAFILE_OPT;
michael@0 215 } else if (!PL_strcasecmp(buf, "modules")) {
michael@0 216 type = MODULES_OPT;
michael@0 217 } else if (!PL_strcasecmp(buf, "optimize")) {
michael@0 218 type = OPTIMIZE_OPT;
michael@0 219 } else if (!PL_strcasecmp(buf, "ocsp")) {
michael@0 220 type = ENABLE_OCSP_OPT;
michael@0 221 } else if (!PL_strcasecmp(buf, "password")) {
michael@0 222 type = PASSWORD_OPT;
michael@0 223 } else if (!PL_strcasecmp(buf, "verify")) {
michael@0 224 type = VERIFY_OPT;
michael@0 225 } else if (!PL_strcasecmp(buf, "who")) {
michael@0 226 type = WHO_OPT;
michael@0 227 } else if (!PL_strcasecmp(buf, "exclude")) {
michael@0 228 type = EXCLUDE_OPT;
michael@0 229 } else if (!PL_strcasecmp(buf, "notime")) {
michael@0 230 type = NO_TIME_OPT;
michael@0 231 } else if (!PL_strcasecmp(buf, "jarfile")) {
michael@0 232 type = ZIPFILE_OPT;
michael@0 233 } else if (!PL_strcasecmp(buf, "outfile")) {
michael@0 234 type = OUTFILE_OPT;
michael@0 235 } else if (!PL_strcasecmp(buf, "leavearc")) {
michael@0 236 type = LEAVE_ARC_OPT;
michael@0 237 } else if (!PL_strcasecmp(buf, "verbosity")) {
michael@0 238 type = VERBOSITY_OPT;
michael@0 239 } else if (!PL_strcasecmp(buf, "keysize")) {
michael@0 240 type = KEYSIZE_OPT;
michael@0 241 } else if (!PL_strcasecmp(buf, "token")) {
michael@0 242 type = TOKEN_OPT;
michael@0 243 } else if (!PL_strcasecmp(buf, "xpi")) {
michael@0 244 type = XPI_ARC_OPT;
michael@0 245 } else {
michael@0 246 PR_fprintf(errorFD,
michael@0 247 "warning: unknown attribute \"%s\" in command file, line %d.\n",
michael@0 248 buf, linenum);
michael@0 249 warningCount++;
michael@0 250 type = UNKNOWN_OPT;
michael@0 251 }
michael@0 252
michael@0 253 /* Process the option, whatever it is */
michael@0 254 if (type != UNKNOWN_OPT) {
michael@0 255 if (ProcessOneOpt(type, equals) == -1) {
michael@0 256 goto finish;
michael@0 257 }
michael@0 258 }
michael@0 259 }
michael@0 260
michael@0 261 retval = 0;
michael@0 262
michael@0 263 finish:
michael@0 264 PR_Close(fd);
michael@0 265 return retval;
michael@0 266 }
michael@0 267
michael@0 268
michael@0 269 /*********************************************************************
michael@0 270 *
michael@0 271 * p a r s e _ a r g s
michael@0 272 */
michael@0 273 static int
michael@0 274 parse_args(int argc, char *argv[])
michael@0 275 {
michael@0 276 char *opt;
michael@0 277 char *arg;
michael@0 278 int needsInc = 0;
michael@0 279 int i;
michael@0 280 OPT_TYPE type;
michael@0 281
michael@0 282 /* Loop over all arguments */
michael@0 283 for (i = 1; i < argc; i++) {
michael@0 284 opt = argv[i];
michael@0 285 arg = NULL;
michael@0 286
michael@0 287 if (opt[0] == '-') {
michael@0 288 if (opt[1] == '-') {
michael@0 289 /* word option */
michael@0 290 if (i < argc - 1) {
michael@0 291 needsInc = 1;
michael@0 292 arg = argv[i+1];
michael@0 293 } else {
michael@0 294 arg = NULL;
michael@0 295 }
michael@0 296
michael@0 297 if ( !PL_strcasecmp(opt + 2, "norecurse")) {
michael@0 298 type = NORECURSE_OPT;
michael@0 299 } else if ( !PL_strcasecmp(opt + 2, "leavearc")) {
michael@0 300 type = LEAVE_ARC_OPT;
michael@0 301 } else if ( !PL_strcasecmp(opt + 2, "verbosity")) {
michael@0 302 type = VERBOSITY_OPT;
michael@0 303 } else if ( !PL_strcasecmp(opt + 2, "outfile")) {
michael@0 304 type = OUTFILE_OPT;
michael@0 305 } else if ( !PL_strcasecmp(opt + 2, "keysize")) {
michael@0 306 type = KEYSIZE_OPT;
michael@0 307 } else if ( !PL_strcasecmp(opt + 2, "token")) {
michael@0 308 type = TOKEN_OPT;
michael@0 309 } else {
michael@0 310 PR_fprintf(errorFD, "warning: unknown option: %s\n",
michael@0 311 opt);
michael@0 312 warningCount++;
michael@0 313 type = UNKNOWN_OPT;
michael@0 314 }
michael@0 315 } else {
michael@0 316 /* char option */
michael@0 317 if (opt[2] != '\0') {
michael@0 318 arg = opt + 2;
michael@0 319 } else if (i < argc - 1) {
michael@0 320 needsInc = 1;
michael@0 321 arg = argv[i+1];
michael@0 322 } else {
michael@0 323 arg = NULL;
michael@0 324 }
michael@0 325
michael@0 326 switch (opt[1]) {
michael@0 327 case 'b':
michael@0 328 type = BASE_OPT;
michael@0 329 break;
michael@0 330 case 'c':
michael@0 331 type = COMPRESSION_OPT;
michael@0 332 break;
michael@0 333 case 'd':
michael@0 334 type = CERT_DIR_OPT;
michael@0 335 break;
michael@0 336 case 'e':
michael@0 337 type = EXTENSION_OPT;
michael@0 338 break;
michael@0 339 case 'f':
michael@0 340 type = COMMAND_FILE_OPT;
michael@0 341 break;
michael@0 342 case 'h':
michael@0 343 type = HELP_OPT;
michael@0 344 break;
michael@0 345 case 'H':
michael@0 346 type = LONG_HELP_OPT;
michael@0 347 break;
michael@0 348 case 'i':
michael@0 349 type = INSTALL_SCRIPT_OPT;
michael@0 350 break;
michael@0 351 case 'j':
michael@0 352 type = SCRIPTDIR_OPT;
michael@0 353 break;
michael@0 354 case 'k':
michael@0 355 type = CERTNAME_OPT;
michael@0 356 break;
michael@0 357 case 'l':
michael@0 358 type = LIST_OBJSIGN_CERTS_OPT;
michael@0 359 break;
michael@0 360 case 'L':
michael@0 361 type = LIST_ALL_CERTS_OPT;
michael@0 362 break;
michael@0 363 case 'm':
michael@0 364 type = METAFILE_OPT;
michael@0 365 break;
michael@0 366 case 'o':
michael@0 367 type = OPTIMIZE_OPT;
michael@0 368 break;
michael@0 369 case 'O':
michael@0 370 type = ENABLE_OCSP_OPT;
michael@0 371 break;
michael@0 372 case 'p':
michael@0 373 type = PASSWORD_OPT;
michael@0 374 break;
michael@0 375 case 'v':
michael@0 376 type = VERIFY_OPT;
michael@0 377 break;
michael@0 378 case 'w':
michael@0 379 type = WHO_OPT;
michael@0 380 break;
michael@0 381 case 'x':
michael@0 382 type = EXCLUDE_OPT;
michael@0 383 break;
michael@0 384 case 'X':
michael@0 385 type = XPI_ARC_OPT;
michael@0 386 break;
michael@0 387 case 'z':
michael@0 388 type = NO_TIME_OPT;
michael@0 389 break;
michael@0 390 case 'J':
michael@0 391 type = JAVASCRIPT_OPT;
michael@0 392 break;
michael@0 393 case 'Z':
michael@0 394 type = ZIPFILE_OPT;
michael@0 395 break;
michael@0 396 case 'G':
michael@0 397 type = GENKEY_OPT;
michael@0 398 break;
michael@0 399 case 'M':
michael@0 400 type = MODULES_OPT;
michael@0 401 break;
michael@0 402 case 's':
michael@0 403 type = KEYSIZE_OPT;
michael@0 404 break;
michael@0 405 case 't':
michael@0 406 type = TOKEN_OPT;
michael@0 407 break;
michael@0 408 default:
michael@0 409 type = UNKNOWN_OPT;
michael@0 410 PR_fprintf(errorFD, "warning: unrecognized option: -%c.\n",
michael@0 411
michael@0 412 opt[1]);
michael@0 413 warningCount++;
michael@0 414 break;
michael@0 415 }
michael@0 416 }
michael@0 417 } else {
michael@0 418 type = UNKNOWN_OPT;
michael@0 419 if (i == argc - 1) {
michael@0 420 if (jartree) {
michael@0 421 PR_fprintf(errorFD,
michael@0 422 "warning: directory to be signed specified more than once.\n"
michael@0 423 " Only last specification will be used.\n");
michael@0 424 warningCount++;
michael@0 425 PR_Free(jartree);
michael@0 426 jartree = NULL;
michael@0 427 }
michael@0 428 jartree = PL_strdup(opt);
michael@0 429 } else {
michael@0 430 PR_fprintf(errorFD, "warning: unrecognized option: %s\n", opt);
michael@0 431 warningCount++;
michael@0 432 }
michael@0 433 }
michael@0 434
michael@0 435 if (type != UNKNOWN_OPT) {
michael@0 436 short ateArg = ProcessOneOpt(type, arg);
michael@0 437 if (ateArg == -1) {
michael@0 438 /* error */
michael@0 439 return - 1;
michael@0 440 }
michael@0 441 if (ateArg && needsInc) {
michael@0 442 i++;
michael@0 443 }
michael@0 444 }
michael@0 445 }
michael@0 446
michael@0 447 return 0;
michael@0 448 }
michael@0 449
michael@0 450
michael@0 451 /*********************************************************************
michael@0 452 *
michael@0 453 * P r o c e s s O n e O p t
michael@0 454 *
michael@0 455 * Since options can come from different places (command file, word options,
michael@0 456 * char options), this is a central function that is called to deal with
michael@0 457 * them no matter where they come from.
michael@0 458 *
michael@0 459 * type is the type of option.
michael@0 460 * arg is the argument to the option, possibly NULL.
michael@0 461 * Returns 1 if the argument was eaten, 0 if it wasn't, and -1 for error.
michael@0 462 */
michael@0 463 static int
michael@0 464 ProcessOneOpt(OPT_TYPE type, char *arg)
michael@0 465 {
michael@0 466 int ate = 0;
michael@0 467
michael@0 468 switch (type) {
michael@0 469 case HELP_OPT:
michael@0 470 Usage();
michael@0 471 break;
michael@0 472 case LONG_HELP_OPT:
michael@0 473 LongUsage();
michael@0 474 break;
michael@0 475 case BASE_OPT:
michael@0 476 if (base) {
michael@0 477 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR], "-b");
michael@0 478 warningCount++;
michael@0 479 PR_Free(base);
michael@0 480 base = NULL;
michael@0 481 }
michael@0 482 if (!arg) {
michael@0 483 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR], "-b");
michael@0 484 errorCount++;
michael@0 485 goto loser;
michael@0 486 }
michael@0 487 base = PL_strdup(arg);
michael@0 488 ate = 1;
michael@0 489 break;
michael@0 490 case COMPRESSION_OPT:
michael@0 491 if (compression_level_specified) {
michael@0 492 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR], "-c");
michael@0 493 warningCount++;
michael@0 494 }
michael@0 495 if ( !arg ) {
michael@0 496 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR], "-c");
michael@0 497 errorCount++;
michael@0 498 goto loser;
michael@0 499 }
michael@0 500 compression_level = atoi(arg);
michael@0 501 compression_level_specified = PR_TRUE;
michael@0 502 ate = 1;
michael@0 503 break;
michael@0 504 case CERT_DIR_OPT:
michael@0 505 if (cert_dir) {
michael@0 506 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR], "-d");
michael@0 507 warningCount++;
michael@0 508 PR_Free(cert_dir);
michael@0 509 cert_dir = NULL;
michael@0 510 }
michael@0 511 if (!arg) {
michael@0 512 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR], "-d");
michael@0 513 errorCount++;
michael@0 514 goto loser;
michael@0 515 }
michael@0 516 cert_dir = PL_strdup(arg);
michael@0 517 ate = 1;
michael@0 518 break;
michael@0 519 case EXTENSION_OPT:
michael@0 520 if (!arg) {
michael@0 521 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 522 "extension (-e)");
michael@0 523 errorCount++;
michael@0 524 goto loser;
michael@0 525 }
michael@0 526 PL_HashTableAdd(extensions, arg, arg);
michael@0 527 extensionsGiven = PR_TRUE;
michael@0 528 ate = 1;
michael@0 529 break;
michael@0 530 case INSTALL_SCRIPT_OPT:
michael@0 531 if (install_script) {
michael@0 532 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 533 "installScript (-i)");
michael@0 534 warningCount++;
michael@0 535 PR_Free(install_script);
michael@0 536 install_script = NULL;
michael@0 537 }
michael@0 538 if (!arg) {
michael@0 539 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 540 "installScript (-i)");
michael@0 541 errorCount++;
michael@0 542 goto loser;
michael@0 543 }
michael@0 544 install_script = PL_strdup(arg);
michael@0 545 ate = 1;
michael@0 546 break;
michael@0 547 case SCRIPTDIR_OPT:
michael@0 548 if (scriptdir) {
michael@0 549 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 550 "javascriptdir (-j)");
michael@0 551 warningCount++;
michael@0 552 PR_Free(scriptdir);
michael@0 553 scriptdir = NULL;
michael@0 554 }
michael@0 555 if (!arg) {
michael@0 556 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 557 "javascriptdir (-j)");
michael@0 558 errorCount++;
michael@0 559 goto loser;
michael@0 560 }
michael@0 561 scriptdir = PL_strdup(arg);
michael@0 562 ate = 1;
michael@0 563 break;
michael@0 564 case CERTNAME_OPT:
michael@0 565 if (keyName) {
michael@0 566 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 567 "keyName (-k)");
michael@0 568 warningCount++;
michael@0 569 PR_Free(keyName);
michael@0 570 keyName = NULL;
michael@0 571 }
michael@0 572 if (!arg) {
michael@0 573 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 574 "keyName (-k)");
michael@0 575 errorCount++;
michael@0 576 goto loser;
michael@0 577 }
michael@0 578 keyName = PL_strdup(arg);
michael@0 579 ate = 1;
michael@0 580 break;
michael@0 581 case LIST_OBJSIGN_CERTS_OPT:
michael@0 582 case LIST_ALL_CERTS_OPT:
michael@0 583 if (list_certs != 0) {
michael@0 584 PR_fprintf(errorFD,
michael@0 585 "warning: only one of -l and -L may be specified.\n");
michael@0 586 warningCount++;
michael@0 587 }
michael@0 588 list_certs = (type == LIST_OBJSIGN_CERTS_OPT ? 1 : 2);
michael@0 589 break;
michael@0 590 case METAFILE_OPT:
michael@0 591 if (metafile) {
michael@0 592 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 593 "metafile (-m)");
michael@0 594 warningCount++;
michael@0 595 PR_Free(metafile);
michael@0 596 metafile = NULL;
michael@0 597 }
michael@0 598 if (!arg) {
michael@0 599 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 600 "metafile (-m)");
michael@0 601 errorCount++;
michael@0 602 goto loser;
michael@0 603 }
michael@0 604 metafile = PL_strdup(arg);
michael@0 605 ate = 1;
michael@0 606 break;
michael@0 607 case OPTIMIZE_OPT:
michael@0 608 optimize = 1;
michael@0 609 break;
michael@0 610 case ENABLE_OCSP_OPT:
michael@0 611 enableOCSP = 1;
michael@0 612 break;
michael@0 613 case PASSWORD_OPT:
michael@0 614 if (pwdata.data) {
michael@0 615 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 616 "password (-p)");
michael@0 617 warningCount++;
michael@0 618 PR_Free(pwdata.data);
michael@0 619 pwdata.data = NULL;
michael@0 620 }
michael@0 621 if (!arg) {
michael@0 622 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 623 "password (-p)");
michael@0 624 errorCount++;
michael@0 625 goto loser;
michael@0 626 }
michael@0 627 pwdata.source = PW_PLAINTEXT;
michael@0 628 pwdata.data = PL_strdup(arg);
michael@0 629 ate = 1;
michael@0 630 break;
michael@0 631 case VERIFY_OPT:
michael@0 632 if (verify) {
michael@0 633 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 634 "verify (-v)");
michael@0 635 warningCount++;
michael@0 636 PR_Free(verify);
michael@0 637 verify = NULL;
michael@0 638 }
michael@0 639 if (!arg) {
michael@0 640 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 641 "verify (-v)");
michael@0 642 errorCount++;
michael@0 643 goto loser;
michael@0 644 }
michael@0 645 verify = PL_strdup(arg);
michael@0 646 ate = 1;
michael@0 647 break;
michael@0 648 case WHO_OPT:
michael@0 649 if (tell_who) {
michael@0 650 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 651 "who (-v)");
michael@0 652 warningCount++;
michael@0 653 PR_Free(tell_who);
michael@0 654 tell_who = NULL;
michael@0 655 }
michael@0 656 if (!arg) {
michael@0 657 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 658 "who (-w)");
michael@0 659 errorCount++;
michael@0 660 goto loser;
michael@0 661 }
michael@0 662 tell_who = PL_strdup(arg);
michael@0 663 ate = 1;
michael@0 664 break;
michael@0 665 case EXCLUDE_OPT:
michael@0 666 if (!arg) {
michael@0 667 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 668 "exclude (-x)");
michael@0 669 errorCount++;
michael@0 670 goto loser;
michael@0 671 }
michael@0 672 PL_HashTableAdd(excludeDirs, arg, arg);
michael@0 673 exclusionsGiven = PR_TRUE;
michael@0 674 ate = 1;
michael@0 675 break;
michael@0 676 case NO_TIME_OPT:
michael@0 677 no_time = 1;
michael@0 678 break;
michael@0 679 case JAVASCRIPT_OPT:
michael@0 680 javascript++;
michael@0 681 break;
michael@0 682 case ZIPFILE_OPT:
michael@0 683 if (zipfile) {
michael@0 684 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 685 "jarfile (-Z)");
michael@0 686 warningCount++;
michael@0 687 PR_Free(zipfile);
michael@0 688 zipfile = NULL;
michael@0 689 }
michael@0 690 if (!arg) {
michael@0 691 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 692 "jarfile (-Z)");
michael@0 693 errorCount++;
michael@0 694 goto loser;
michael@0 695 }
michael@0 696 zipfile = PL_strdup(arg);
michael@0 697 ate = 1;
michael@0 698 break;
michael@0 699 case GENKEY_OPT:
michael@0 700 if (genkey) {
michael@0 701 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 702 "generate (-G)");
michael@0 703 warningCount++;
michael@0 704 PR_Free(genkey);
michael@0 705 genkey = NULL;
michael@0 706 }
michael@0 707 if (!arg) {
michael@0 708 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 709 "generate (-G)");
michael@0 710 errorCount++;
michael@0 711 goto loser;
michael@0 712 }
michael@0 713 genkey = PL_strdup(arg);
michael@0 714 ate = 1;
michael@0 715 break;
michael@0 716 case MODULES_OPT:
michael@0 717 list_modules++;
michael@0 718 break;
michael@0 719 case SIGNDIR_OPT:
michael@0 720 if (jartree) {
michael@0 721 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 722 "signdir");
michael@0 723 warningCount++;
michael@0 724 PR_Free(jartree);
michael@0 725 jartree = NULL;
michael@0 726 }
michael@0 727 if (!arg) {
michael@0 728 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 729 "signdir");
michael@0 730 errorCount++;
michael@0 731 goto loser;
michael@0 732 }
michael@0 733 jartree = PL_strdup(arg);
michael@0 734 ate = 1;
michael@0 735 break;
michael@0 736 case OUTFILE_OPT:
michael@0 737 if (outfile) {
michael@0 738 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 739 "outfile");
michael@0 740 warningCount++;
michael@0 741 PR_Free(outfile);
michael@0 742 outfile = NULL;
michael@0 743 }
michael@0 744 if (!arg) {
michael@0 745 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 746 "outfile");
michael@0 747 errorCount++;
michael@0 748 goto loser;
michael@0 749 }
michael@0 750 outfile = PL_strdup(arg);
michael@0 751 ate = 1;
michael@0 752 break;
michael@0 753 case COMMAND_FILE_OPT:
michael@0 754 if (cmdFile) {
michael@0 755 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR],
michael@0 756 "-f");
michael@0 757 warningCount++;
michael@0 758 PR_Free(cmdFile);
michael@0 759 cmdFile = NULL;
michael@0 760 }
michael@0 761 if (!arg) {
michael@0 762 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 763 "-f");
michael@0 764 errorCount++;
michael@0 765 goto loser;
michael@0 766 }
michael@0 767 cmdFile = PL_strdup(arg);
michael@0 768 ate = 1;
michael@0 769 break;
michael@0 770 case NORECURSE_OPT:
michael@0 771 noRecurse = PR_TRUE;
michael@0 772 break;
michael@0 773 case LEAVE_ARC_OPT:
michael@0 774 leaveArc = PR_TRUE;
michael@0 775 break;
michael@0 776 case VERBOSITY_OPT:
michael@0 777 if (!arg) {
michael@0 778 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR],
michael@0 779 "--verbosity");
michael@0 780 errorCount++;
michael@0 781 goto loser;
michael@0 782 }
michael@0 783 verbosity = atoi(arg);
michael@0 784 ate = 1;
michael@0 785 break;
michael@0 786 case KEYSIZE_OPT:
michael@0 787 if ( keySize != -1 ) {
michael@0 788 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR], "-s");
michael@0 789 warningCount++;
michael@0 790 }
michael@0 791 keySize = atoi(arg);
michael@0 792 ate = 1;
michael@0 793 if ( keySize < 1 || keySize > MAX_RSA_KEY_SIZE ) {
michael@0 794 PR_fprintf(errorFD, "Invalid key size: %d.\n", keySize);
michael@0 795 errorCount++;
michael@0 796 goto loser;
michael@0 797 }
michael@0 798 break;
michael@0 799 case TOKEN_OPT:
michael@0 800 if ( token ) {
michael@0 801 PR_fprintf(errorFD, errStrings[DUPLICATE_OPTION_ERR], "-t");
michael@0 802 PR_Free(token);
michael@0 803 token = NULL;
michael@0 804 }
michael@0 805 if ( !arg ) {
michael@0 806 PR_fprintf(errorFD, errStrings[OPTION_NEEDS_ARG_ERR], "-t");
michael@0 807 errorCount++;
michael@0 808 goto loser;
michael@0 809 }
michael@0 810 token = PL_strdup(arg);
michael@0 811 ate = 1;
michael@0 812 break;
michael@0 813 case XPI_ARC_OPT:
michael@0 814 xpi_arc = 1;
michael@0 815 break;
michael@0 816 default:
michael@0 817 PR_fprintf(errorFD, "warning: unknown option\n");
michael@0 818 warningCount++;
michael@0 819 break;
michael@0 820 }
michael@0 821
michael@0 822 return ate;
michael@0 823 loser:
michael@0 824 return - 1;
michael@0 825 }
michael@0 826
michael@0 827
michael@0 828 /*********************************************************************
michael@0 829 *
michael@0 830 * m a i n
michael@0 831 */
michael@0 832 int
michael@0 833 main(int argc, char *argv[])
michael@0 834 {
michael@0 835 PRBool readOnly;
michael@0 836 int retval = 0;
michael@0 837
michael@0 838 outputFD = PR_STDOUT;
michael@0 839 errorFD = PR_STDERR;
michael@0 840
michael@0 841 progName = argv[0];
michael@0 842
michael@0 843 if (argc < 2) {
michael@0 844 Usage();
michael@0 845 }
michael@0 846
michael@0 847 excludeDirs = PL_NewHashTable(10, PL_HashString, PL_CompareStrings,
michael@0 848 PL_CompareStrings, NULL, NULL);
michael@0 849 extensions = PL_NewHashTable(10, PL_HashString, PL_CompareStrings,
michael@0 850 PL_CompareStrings, NULL, NULL);
michael@0 851
michael@0 852 if (parse_args(argc, argv)) {
michael@0 853 retval = -1;
michael@0 854 goto cleanup;
michael@0 855 }
michael@0 856
michael@0 857 /* Parse the command file if one was given */
michael@0 858 if (cmdFile) {
michael@0 859 if (ProcessCommandFile()) {
michael@0 860 retval = -1;
michael@0 861 goto cleanup;
michael@0 862 }
michael@0 863 }
michael@0 864
michael@0 865 /* Set up output redirection */
michael@0 866 if (outfile) {
michael@0 867 if (PR_Access(outfile, PR_ACCESS_EXISTS) == PR_SUCCESS) {
michael@0 868 /* delete the file if it is already present */
michael@0 869 PR_fprintf(errorFD,
michael@0 870 "warning: %s already exists and will be overwritten.\n",
michael@0 871 outfile);
michael@0 872 warningCount++;
michael@0 873 if (PR_Delete(outfile) != PR_SUCCESS) {
michael@0 874 PR_fprintf(errorFD, "ERROR: unable to delete %s.\n", outfile);
michael@0 875 errorCount++;
michael@0 876 exit(ERRX);
michael@0 877 }
michael@0 878 }
michael@0 879 outputFD = PR_Open(outfile,
michael@0 880 PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0777);
michael@0 881 if (!outputFD) {
michael@0 882 PR_fprintf(errorFD, "ERROR: Unable to create %s.\n",
michael@0 883 outfile);
michael@0 884 errorCount++;
michael@0 885 exit(ERRX);
michael@0 886 }
michael@0 887 errorFD = outputFD;
michael@0 888 }
michael@0 889
michael@0 890 /* This seems to be a fairly common user error */
michael@0 891
michael@0 892 if (verify && list_certs > 0) {
michael@0 893 PR_fprintf (errorFD, "%s: Can't use -l and -v at the same time\n",
michael@0 894 PROGRAM_NAME);
michael@0 895 errorCount++;
michael@0 896 retval = -1;
michael@0 897 goto cleanup;
michael@0 898 }
michael@0 899
michael@0 900 /* -J assumes -Z now */
michael@0 901
michael@0 902 if (javascript && zipfile) {
michael@0 903 PR_fprintf (errorFD, "%s: Can't use -J and -Z at the same time\n",
michael@0 904 PROGRAM_NAME);
michael@0 905 PR_fprintf (errorFD, "%s: -J option will create the jar files for you\n",
michael@0 906 PROGRAM_NAME);
michael@0 907 errorCount++;
michael@0 908 retval = -1;
michael@0 909 goto cleanup;
michael@0 910 }
michael@0 911
michael@0 912 /* -X needs -Z */
michael@0 913
michael@0 914 if (xpi_arc && !zipfile) {
michael@0 915 PR_fprintf (errorFD, "%s: option XPI (-X) requires option jarfile (-Z)\n",
michael@0 916 PROGRAM_NAME);
michael@0 917 errorCount++;
michael@0 918 retval = -1;
michael@0 919 goto cleanup;
michael@0 920 }
michael@0 921
michael@0 922 /* Less common mixing of -L with various options */
michael@0 923
michael@0 924 if (list_certs > 0 &&
michael@0 925 (tell_who || zipfile || javascript ||
michael@0 926 scriptdir || extensionsGiven || exclusionsGiven || install_script)) {
michael@0 927 PR_fprintf(errorFD, "%s: Can't use -l or -L with that option\n",
michael@0 928 PROGRAM_NAME);
michael@0 929 errorCount++;
michael@0 930 retval = -1;
michael@0 931 goto cleanup;
michael@0 932 }
michael@0 933
michael@0 934
michael@0 935 if (!cert_dir)
michael@0 936 cert_dir = get_default_cert_dir();
michael@0 937
michael@0 938 VerifyCertDir(cert_dir, keyName);
michael@0 939
michael@0 940
michael@0 941 if ( compression_level < MIN_COMPRESSION_LEVEL ||
michael@0 942 compression_level > MAX_COMPRESSION_LEVEL) {
michael@0 943 PR_fprintf(errorFD, "Compression level must be between %d and %d.\n",
michael@0 944 MIN_COMPRESSION_LEVEL, MAX_COMPRESSION_LEVEL);
michael@0 945 errorCount++;
michael@0 946 retval = -1;
michael@0 947 goto cleanup;
michael@0 948 }
michael@0 949
michael@0 950 if (jartree && !keyName) {
michael@0 951 PR_fprintf(errorFD, "You must specify a key with which to sign.\n");
michael@0 952 errorCount++;
michael@0 953 retval = -1;
michael@0 954 goto cleanup;
michael@0 955 }
michael@0 956
michael@0 957 readOnly = (genkey == NULL); /* only key generation requires write */
michael@0 958 if (InitCrypto(cert_dir, readOnly)) {
michael@0 959 PR_fprintf(errorFD, "ERROR: Cryptographic initialization failed.\n");
michael@0 960 errorCount++;
michael@0 961 retval = -1;
michael@0 962 goto cleanup;
michael@0 963 }
michael@0 964
michael@0 965 if (enableOCSP) {
michael@0 966 SECStatus rv = CERT_EnableOCSPChecking(CERT_GetDefaultCertDB());
michael@0 967 if (rv != SECSuccess) {
michael@0 968 PR_fprintf(errorFD, "ERROR: Attempt to enable OCSP Checking failed.\n");
michael@0 969 errorCount++;
michael@0 970 retval = -1;
michael@0 971 }
michael@0 972 }
michael@0 973
michael@0 974 if (verify) {
michael@0 975 if (VerifyJar(verify)) {
michael@0 976 errorCount++;
michael@0 977 retval = -1;
michael@0 978 goto cleanup;
michael@0 979 }
michael@0 980 } else if (list_certs) {
michael@0 981 if (ListCerts(keyName, list_certs)) {
michael@0 982 errorCount++;
michael@0 983 retval = -1;
michael@0 984 goto cleanup;
michael@0 985 }
michael@0 986 } else if (list_modules) {
michael@0 987 JarListModules();
michael@0 988 } else if (genkey) {
michael@0 989 if (GenerateCert(genkey, keySize, token)) {
michael@0 990 errorCount++;
michael@0 991 retval = -1;
michael@0 992 goto cleanup;
michael@0 993 }
michael@0 994 } else if (tell_who) {
michael@0 995 if (JarWho(tell_who)) {
michael@0 996 errorCount++;
michael@0 997 retval = -1;
michael@0 998 goto cleanup;
michael@0 999 }
michael@0 1000 } else if (javascript && jartree) {
michael@0 1001 /* make sure directory exists */
michael@0 1002 PRDir * dir;
michael@0 1003 dir = PR_OpenDir(jartree);
michael@0 1004 if (!dir) {
michael@0 1005 PR_fprintf(errorFD, "ERROR: unable to open directory %s.\n",
michael@0 1006 jartree);
michael@0 1007 errorCount++;
michael@0 1008 retval = -1;
michael@0 1009 goto cleanup;
michael@0 1010 } else {
michael@0 1011 PR_CloseDir(dir);
michael@0 1012 }
michael@0 1013
michael@0 1014 /* undo junk from prior runs of signtool*/
michael@0 1015 if (RemoveAllArc(jartree)) {
michael@0 1016 PR_fprintf(errorFD, "Error removing archive directories under %s\n",
michael@0 1017 jartree);
michael@0 1018 errorCount++;
michael@0 1019 retval = -1;
michael@0 1020 goto cleanup;
michael@0 1021 }
michael@0 1022
michael@0 1023 /* traverse all the htm|html files in the directory */
michael@0 1024 if (InlineJavaScript(jartree, !noRecurse)) {
michael@0 1025 retval = -1;
michael@0 1026 goto cleanup;
michael@0 1027 }
michael@0 1028
michael@0 1029 /* sign any resultant .arc directories created in above step */
michael@0 1030 if (SignAllArc(jartree, keyName, javascript, metafile, install_script,
michael@0 1031 optimize, !noRecurse)) {
michael@0 1032 retval = -1;
michael@0 1033 goto cleanup;
michael@0 1034 }
michael@0 1035
michael@0 1036 if (!leaveArc) {
michael@0 1037 RemoveAllArc(jartree);
michael@0 1038 }
michael@0 1039
michael@0 1040 if (errorCount > 0 || warningCount > 0) {
michael@0 1041 PR_fprintf(outputFD, "%d error%s, %d warning%s.\n",
michael@0 1042 errorCount,
michael@0 1043 errorCount == 1 ? "" : "s", warningCount, warningCount
michael@0 1044 == 1 ? "" : "s");
michael@0 1045 } else {
michael@0 1046 PR_fprintf(outputFD, "Directory %s signed successfully.\n",
michael@0 1047 jartree);
michael@0 1048 }
michael@0 1049 } else if (jartree) {
michael@0 1050 SignArchive(jartree, keyName, zipfile, javascript, metafile,
michael@0 1051 install_script, optimize, !noRecurse);
michael@0 1052 } else
michael@0 1053 Usage();
michael@0 1054
michael@0 1055 cleanup:
michael@0 1056 if (extensions) {
michael@0 1057 PL_HashTableDestroy(extensions);
michael@0 1058 extensions = NULL;
michael@0 1059 }
michael@0 1060 if (excludeDirs) {
michael@0 1061 PL_HashTableDestroy(excludeDirs);
michael@0 1062 excludeDirs = NULL;
michael@0 1063 }
michael@0 1064 if (outputFD != PR_STDOUT) {
michael@0 1065 PR_Close(outputFD);
michael@0 1066 }
michael@0 1067 rm_dash_r(TMP_OUTPUT);
michael@0 1068 if (retval == 0) {
michael@0 1069 if (NSS_Shutdown() != SECSuccess) {
michael@0 1070 exit(1);
michael@0 1071 }
michael@0 1072 }
michael@0 1073 return retval;
michael@0 1074 }
michael@0 1075
michael@0 1076

mercurial