DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (38f1c92a5e11)

VCS Links

Line Code
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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* To edit this file, set TABSTOPS to 4 spaces.
 * This is not the normal NSS convention.
 */

#include "modutil.h"
#include "install.h"
#include <plstr.h>
#include "certdb.h" /* for CERT_DB_FILE_VERSION */
#include "nss.h"

static void install_error(char* message);
static char* PR_fgets(char* buf, int size, PRFileDesc* file);
static char* progName;

/* This enum must be kept in sync with the commandNames list */
typedef enum {
    NO_COMMAND,
    ADD_COMMAND,
    CHANGEPW_COMMAND,
    CREATE_COMMAND,
    DEFAULT_COMMAND,
    DELETE_COMMAND,
    DISABLE_COMMAND,
    ENABLE_COMMAND,
    FIPS_COMMAND,
    JAR_COMMAND,
    LIST_COMMAND,
    RAW_LIST_COMMAND,
    RAW_ADD_COMMAND,
    CHKFIPS_COMMAND,
    UNDEFAULT_COMMAND
} Command;

/* This list must be kept in sync with the Command enum */
static char* commandNames[] = {
    "(no command)",
    "-add",
    "-changepw",
    "-create",
    "-default",
    "-delete",
    "-disable",
    "-enable",
    "-fips",
    "-jar",
    "-list",
    "-rawlist",
    "-rawadd",
    "-chkfips",
    "-undefault"
};

/* this enum must be kept in sync with the optionStrings list */
typedef enum {
    ADD_ARG = 0,
    RAW_ADD_ARG,
    CHANGEPW_ARG,
    CIPHERS_ARG,
    CREATE_ARG,
    DBDIR_ARG,
    DBPREFIX_ARG,
    DEFAULT_ARG,
    DELETE_ARG,
    DISABLE_ARG,
    ENABLE_ARG,
    FIPS_ARG,
    FORCE_ARG,
    JAR_ARG,
    LIBFILE_ARG,
    LIST_ARG,
    RAW_LIST_ARG,
    MECHANISMS_ARG,
    NEWPWFILE_ARG,
    PWFILE_ARG,
    SLOT_ARG,
    UNDEFAULT_ARG,
    INSTALLDIR_ARG,
    TEMPDIR_ARG,
    SECMOD_ARG,
    NOCERTDB_ARG,
    STRING_ARG,
    CHKFIPS_ARG,

    NUM_ARGS /* must be last */
} Arg;

/* This list must be kept in sync with the Arg enum */
static char* optionStrings[] = {
    "-add",
    "-rawadd",
    "-changepw",
    "-ciphers",
    "-create",
    "-dbdir",
    "-dbprefix",
    "-default",
    "-delete",
    "-disable",
    "-enable",
    "-fips",
    "-force",
    "-jar",
    "-libfile",
    "-list",
    "-rawlist",
    "-mechanisms",
    "-newpwfile",
    "-pwfile",
    "-slot",
    "-undefault",
    "-installdir",
    "-tempdir",
    "-secmod",
    "-nocertdb",
    "-string",
    "-chkfips",
};

char* msgStrings[] = {
    "FIPS mode enabled.\n",
    "FIPS mode disabled.\n",
    "Using database directory %s...\n",
    "Creating \"%s\"...",
    "Module \"%s\" added to database.\n",
    "Module \"%s\" deleted from database.\n",
    "Token \"%s\" password changed successfully.\n",
    "Incorrect password, try again...\n",
    "Passwords do not match, try again...\n",
    "done.\n",
    "Slot \"%s\" %s.\n",
    "Successfully changed defaults.\n",
    "Successfully changed defaults.\n",
    "\nWARNING: Performing this operation while the browser is running could cause"
    "\ncorruption of your security databases. If the browser is currently running,"
    "\nyou should exit browser before continuing this operation. Type "
    "\n'q <enter>' to abort, or <enter> to continue: ",
    "\nAborting...\n",
    "\nWARNING: Manually adding a module while p11-kit is enabled could cause"
    "\nduplicate module registration in your security database. It is suggested "
    "\nto configure the module through p11-kit configuration file instead.\n"
    "\nType 'q <enter>' to abort, or <enter> to continue: "
};

/* Increment i if doing so would have i still be less than j.  If you
   are able to do this, return 0.  Otherwise return 1. */
#define TRY_INC(i, j) (((i + 1) < j) ? (++i, 0) : 1)

/********************************************************************
 *
 * file-wide variables obtained from the command line
 */
static Command command = NO_COMMAND;
static char* pwFile = NULL;
static char* newpwFile = NULL;
static char* moduleName = NULL;
static char* moduleSpec = NULL;
static char* slotName = NULL;
static char* secmodName = NULL;
static char* tokenName = NULL;
static char* libFile = NULL;
static char* dbdir = NULL;
static char* dbprefix = "";
static char* secmodString = NULL;
static char* mechanisms = NULL;
static char* ciphers = NULL;
static char* fipsArg = NULL;
static char* jarFile = NULL;
static char* installDir = NULL;
static char* tempDir = NULL;
static short force = 0;
static PRBool nocertdb = PR_FALSE;

/*******************************************************************
 *
 * p a r s e _ a r g s
 */
static Error
parse_args(int argc, char* argv[])
{
    int i;
    char* arg;
    int optionType;

    /* Loop over all arguments */
    for (i = 1; i < argc; i++) {
        arg = argv[i];

        /* Make sure this is an option and not some floating argument */
        if (arg[0] != '-') {
            PR_fprintf(PR_STDERR, errStrings[UNEXPECTED_ARG_ERR], argv[i]);
            return UNEXPECTED_ARG_ERR;
        }

        /* Find which option this is */
        for (optionType = 0; optionType < NUM_ARGS; optionType++) {
            if (!strcmp(arg, optionStrings[optionType])) {
                break;
            }
        }

        /* Deal with this specific option */
        switch (optionType) {
            case NUM_ARGS:
            default:
                PR_fprintf(PR_STDERR, errStrings[UNKNOWN_OPTION_ERR], arg);
                return UNKNOWN_OPTION_ERR;
                break;
            case ADD_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = ADD_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                moduleName = argv[i];
                break;
            case CHANGEPW_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = CHANGEPW_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                tokenName = argv[i];
                break;
            case CIPHERS_ARG:
                if (ciphers != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                ciphers = argv[i];
                break;
            case CREATE_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = CREATE_COMMAND;
                break;
            case DBDIR_ARG:
                if (dbdir != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                dbdir = argv[i];
                break;
            case DBPREFIX_ARG:
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                dbprefix = argv[i];
                break;
            case UNDEFAULT_ARG:
            case DEFAULT_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                if (optionType == DEFAULT_ARG) {
                    command = DEFAULT_COMMAND;
                } else {
                    command = UNDEFAULT_COMMAND;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                moduleName = argv[i];
                break;
            case DELETE_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = DELETE_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                moduleName = argv[i];
                break;
            case DISABLE_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = DISABLE_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                moduleName = argv[i];
                break;
            case ENABLE_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = ENABLE_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                moduleName = argv[i];
                break;
            case FIPS_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = FIPS_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                fipsArg = argv[i];
                break;
            case CHKFIPS_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = CHKFIPS_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                fipsArg = argv[i];
                break;
            case FORCE_ARG:
                force = 1;
                break;
            case NOCERTDB_ARG:
                nocertdb = PR_TRUE;
                break;
            case INSTALLDIR_ARG:
                if (installDir != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                installDir = argv[i];
                break;
            case TEMPDIR_ARG:
                if (tempDir != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                tempDir = argv[i];
                break;
            case JAR_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = JAR_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                jarFile = argv[i];
                break;
            case LIBFILE_ARG:
                if (libFile != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                libFile = argv[i];
                break;
            case LIST_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = LIST_COMMAND;
                /* This option may or may not have an argument */
                if ((i + 1 < argc) && (argv[i + 1][0] != '-')) {
                    moduleName = argv[++i];
                }
                break;
            case RAW_LIST_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = RAW_LIST_COMMAND;
                /* This option may or may not have an argument */
                if ((i + 1 < argc) && (argv[i + 1][0] != '-')) {
                    moduleName = argv[++i];
                }
                break;
            case RAW_ADD_ARG:
                if (command != NO_COMMAND) {
                    PR_fprintf(PR_STDERR, errStrings[MULTIPLE_COMMAND_ERR], arg);
                    return MULTIPLE_COMMAND_ERR;
                }
                command = RAW_ADD_COMMAND;
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                moduleSpec = argv[i];
                break;
            case MECHANISMS_ARG:
                if (mechanisms != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                mechanisms = argv[i];
                break;
            case NEWPWFILE_ARG:
                if (newpwFile != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                newpwFile = argv[i];
                break;
            case PWFILE_ARG:
                if (pwFile != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                pwFile = argv[i];
                break;
            case SLOT_ARG:
                if (slotName != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                slotName = argv[i];
                break;
            case SECMOD_ARG:
                if (secmodName != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                secmodName = argv[i];
                break;
            case STRING_ARG:
                if (secmodString != NULL) {
                    PR_fprintf(PR_STDERR, errStrings[DUPLICATE_OPTION_ERR], arg);
                    return DUPLICATE_OPTION_ERR;
                }
                if (TRY_INC(i, argc)) {
                    PR_fprintf(PR_STDERR, errStrings[OPTION_NEEDS_ARG_ERR], arg);
                    return OPTION_NEEDS_ARG_ERR;
                }
                secmodString = argv[i];
                break;
        }
    }
    return SUCCESS;
}

/************************************************************************
 *
 * v e r i f y _ p a r a m s
 */
static Error
verify_params()
{
    switch (command) {
        case ADD_COMMAND:
            if (libFile == NULL) {
                PR_fprintf(PR_STDERR, errStrings[MISSING_PARAM_ERR],
                           commandNames[ADD_COMMAND], optionStrings[LIBFILE_ARG]);
                return MISSING_PARAM_ERR;
            }
            break;
        case CHANGEPW_COMMAND:
            break;
        case CREATE_COMMAND:
            break;
        case DELETE_COMMAND:
            break;
        case DISABLE_COMMAND:
            break;
        case ENABLE_COMMAND:
            break;
        case FIPS_COMMAND:
        case CHKFIPS_COMMAND:
            if (PL_strcasecmp(fipsArg, "true") &&
                PL_strcasecmp(fipsArg, "false")) {
                PR_fprintf(PR_STDERR, errStrings[INVALID_FIPS_ARG]);
                return INVALID_FIPS_ARG;
            }
            break;
        case JAR_COMMAND:
            if (installDir == NULL) {
                PR_fprintf(PR_STDERR, errStrings[MISSING_PARAM_ERR],
                           commandNames[JAR_COMMAND], optionStrings[INSTALLDIR_ARG]);
                return MISSING_PARAM_ERR;
            }
            break;
        case LIST_COMMAND:
        case RAW_LIST_COMMAND:
            break;
        case RAW_ADD_COMMAND:
            break;
        case UNDEFAULT_COMMAND:
        case DEFAULT_COMMAND:
            if (mechanisms == NULL) {
                PR_fprintf(PR_STDERR, errStrings[MISSING_PARAM_ERR],
                           commandNames[command], optionStrings[MECHANISMS_ARG]);
                return MISSING_PARAM_ERR;
            }
            break;
        default:
            /* Ignore this here */
            break;
    }

    return SUCCESS;
}

/********************************************************************
 *
 * i n i t _ c r y p t o
 *
 * Does crypto initialization that all commands will require.
 * If -nocertdb option is specified, don't open key or cert db (we don't
 * need them if we aren't going to be verifying signatures).  This is
 * because serverland doesn't always have cert and key database files
 * available.
 *
 * This function is ill advised. Names and locations of databases are
 * private to NSS proper. Such functions only confuse other users.
 *
 */
static Error
check_crypto(PRBool create, PRBool readOnly)
{
    char* dir;
    char* moddbname = NULL;
    Error retval;
    static const char multiaccess[] = { "multiaccess:" };

    dir = SECU_ConfigDirectory(dbdir); /* dir is never NULL */
    if (dir[0] == '\0') {
        PR_fprintf(PR_STDERR, errStrings[NO_DBDIR_ERR]);
        retval = NO_DBDIR_ERR;
        goto loser;
    }
    if (strncmp(dir, multiaccess, sizeof multiaccess - 1) == 0) {
        /* won't attempt to handle the multiaccess case. */
        return SUCCESS;
    }
#ifdef notdef
    /* Make sure db directory exists and is readable */
    if (PR_Access(dir, PR_ACCESS_EXISTS) != PR_SUCCESS) {
        PR_fprintf(PR_STDERR, errStrings[DIR_DOESNT_EXIST_ERR], dir);
        retval = DIR_DOESNT_EXIST_ERR;
        goto loser;
    } else if (PR_Access(dir, PR_ACCESS_READ_OK) != PR_SUCCESS) {
        PR_fprintf(PR_STDERR, errStrings[DIR_NOT_READABLE_ERR], dir);
        retval = DIR_NOT_READABLE_ERR;
        goto loser;
    }

    if (secmodName == NULL) {
        secmodName = "secmod.db";
    }

    moddbname = PR_smprintf("%s/%s", dir, secmodName);
    if (!moddbname)
        return OUT_OF_MEM_ERR;

    /* Check for the proper permissions on databases */
    if (create) {
        /* Make sure dbs don't already exist, and the directory is
            writeable */
        if (PR_Access(moddbname, PR_ACCESS_EXISTS) == PR_SUCCESS) {
            PR_fprintf(PR_STDERR, errStrings[FILE_ALREADY_EXISTS_ERR],
                       moddbname);
            retval = FILE_ALREADY_EXISTS_ERR;
            goto loser;
        } else if (PR_Access(dir, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
            PR_fprintf(PR_STDERR, errStrings[DIR_NOT_WRITEABLE_ERR], dir);
            retval = DIR_NOT_WRITEABLE_ERR;
            goto loser;
        }
    } else {
        /* Make sure dbs are readable and writeable */
        if (PR_Access(moddbname, PR_ACCESS_READ_OK) != PR_SUCCESS) {
            PR_fprintf(PR_STDERR, errStrings[FILE_NOT_READABLE_ERR], moddbname);
            retval = FILE_NOT_READABLE_ERR;
            goto loser;
        }

        /* Check for write access if we'll be making changes */
        if (!readOnly) {
            if (PR_Access(moddbname, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
                PR_fprintf(PR_STDERR, errStrings[FILE_NOT_WRITEABLE_ERR],
                           moddbname);
                retval = FILE_NOT_WRITEABLE_ERR;
                goto loser;
            }
        }
        PR_fprintf(PR_STDOUT, msgStrings[USING_DBDIR_MSG],
                   SECU_ConfigDirectory(NULL));
    }
#endif
    retval = SUCCESS;
loser:
    if (moddbname) {
        PR_Free(moddbname);
    }
    return retval;
}

static Error
init_crypto(PRBool create, PRBool readOnly)
{

    PRUint32 flags = 0;
    SECStatus rv;
    Error retval;
    /* Open/create key database */

    if (readOnly)
        flags |= NSS_INIT_READONLY;
    if (nocertdb)
        flags |= NSS_INIT_NOCERTDB;
    rv = NSS_Initialize(SECU_ConfigDirectory(NULL), dbprefix, dbprefix,
                        secmodName, flags);
    if (rv != SECSuccess) {
        SECU_PrintPRandOSError(progName);
        retval = NSS_INITIALIZE_FAILED_ERR;
    } else
        retval = SUCCESS;

    return retval;
}

/*************************************************************************
 *
 * u s a g e
 */
static void
usage()
{
    PR_fprintf(PR_STDOUT,
               "\nNetscape Cryptographic Module Utility\n"
               "Usage: modutil [command] [options]\n\n"
               "                            COMMANDS\n"
               "---------------------------------------------------------------------------\n"
               "-add MODULE_NAME                 Add the named module to the module database\n"
               "   -libfile LIBRARY_FILE         The name of the file (.so or .dll)\n"
               "                                 containing the implementation of PKCS #11\n"
               "   [-ciphers CIPHER_LIST]        Enable the given ciphers on this module\n"
               "   [-mechanisms MECHANISM_LIST]  Make the module a default provider of the\n"
               "                                 given mechanisms\n"
               "   [-string CONFIG_STRING]       Pass a configuration string to this module\n"
               "-changepw TOKEN                  Change the password on the named token\n"
               "   [-pwfile FILE]                The old password is in this file\n"
               "   [-newpwfile FILE]             The new password is in this file\n"
               "-chkfips [ true | false ]        If true, verify  FIPS mode.  If false,\n"
               "                                 verify not FIPS mode\n"
               "-create                          Create a new set of security databases\n"
               "-default MODULE                  Make the given module a default provider\n"
               "   -mechanisms MECHANISM_LIST    of the given mechanisms\n"
               "   [-slot SLOT]                  limit change to only the given slot\n"
               "-delete MODULE                   Remove the named module from the module\n"
               "                                 database\n"
               "-disable MODULE                  Disable the named module\n"
               "   [-slot SLOT]                  Disable only the named slot on the module\n"
               "-enable MODULE                   Enable the named module\n"
               "   [-slot SLOT]                  Enable only the named slot on the module\n"
               "-fips [ true | false ]           If true, enable FIPS mode.  If false,\n"
               "                                 disable FIPS mode\n"
               "-force                           Do not run interactively\n"
               "-jar JARFILE                     Install a PKCS #11 module from the given\n"
               "                                 JAR file in the PKCS #11 JAR format\n"
               "   -installdir DIR               Use DIR as the root directory of the\n"
               "                                 installation\n"
               "   [-tempdir DIR]                Use DIR as the temporary installation\n"
               "                                 directory. If not specified, the current\n"
               "                                 directory is used\n"
               "-list [MODULE]                   Lists information about the specified module\n"
               "                                 or about all modules if none is specified\n"
               "-rawadd MODULESPEC               Add module spec string to secmod DB\n"
               "-rawlist [MODULE]                Display module spec(s) for one or all\n"
               "                                 loadable modules\n"
               "-undefault MODULE                The given module is NOT a default provider\n"
               "   -mechanisms MECHANISM_LIST    of the listed mechanisms\n"
               "   [-slot SLOT]                  limit change to only the given slot\n"
               "---------------------------------------------------------------------------\n"
               "\n"
               "                             OPTIONS\n"
               "---------------------------------------------------------------------------\n"
               "-dbdir DIR                       Directory DIR contains the security databases\n"
               "-dbprefix prefix                 Prefix for the security databases\n"
               "-nocertdb                        Do not load certificate or key databases. No\n"
               "                                 verification will be performed on JAR files.\n"
               "-secmod secmodName               Name of the security modules file\n"
               "---------------------------------------------------------------------------\n"
               "\n"
               "Mechanism lists are colon-separated.  The following mechanisms are recognized:\n"
               "RSA, DSA, DH, RC2, RC4, RC5, AES, CAMELLIA, DES, MD2, MD5, SHA1, SHA256, SHA512,\n"
               "SSL, TLS, RANDOM, and FRIENDLY\n"
               "\n"
               "Cipher lists are colon-separated.  The following ciphers are recognized:\n"
               "\n"
               "\nQuestions or bug reports should be sent to modutil-support@netscape.com.\n");
}

/*************************************************************************
 *
 * m a i n
 */
int
main(int argc, char* argv[])
{
    int errcode = SUCCESS;
    PRBool createdb, readOnly;
#define STDINBUF_SIZE 80
    char stdinbuf[STDINBUF_SIZE];

    progName = strrchr(argv[0], '/');
    progName = progName ? progName + 1 : argv[0];

    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);

    if (parse_args(argc, argv) != SUCCESS) {
        usage();
        errcode = INVALID_USAGE_ERR;
        goto loser;
    }

    if (verify_params() != SUCCESS) {
        usage();
        errcode = INVALID_USAGE_ERR;
        goto loser;
    }

    if (command == NO_COMMAND) {
        PR_fprintf(PR_STDERR, errStrings[NO_COMMAND_ERR]);
        usage();
        errcode = INVALID_USAGE_ERR;
        goto loser;
    }

    /* Set up crypto stuff */
    createdb = command == CREATE_COMMAND;
    readOnly = ((command == LIST_COMMAND) ||
                (command == CHKFIPS_COMMAND) ||
                (command == RAW_LIST_COMMAND));

    /* Make sure browser is not running if we're writing to a database */
    /* Do this before initializing crypto */
    if (!readOnly && !force) {
        char* response;

        PR_fprintf(PR_STDOUT, msgStrings[BROWSER_RUNNING_MSG]);
        if (!PR_fgets(stdinbuf, STDINBUF_SIZE, PR_STDIN)) {
            PR_fprintf(PR_STDERR, errStrings[STDIN_READ_ERR]);
            errcode = STDIN_READ_ERR;
            goto loser;
        }
        if ((response = strtok(stdinbuf, " \r\n\t"))) {
            if (!PL_strcasecmp(response, "q")) {
                PR_fprintf(PR_STDOUT, msgStrings[ABORTING_MSG]);
                errcode = SUCCESS;
                goto loser;
            }
        }
        PR_fprintf(PR_STDOUT, "\n");
    }

    errcode = check_crypto(createdb, readOnly);
    if (errcode != SUCCESS) {
        goto loser;
    }

    if ((command == RAW_LIST_COMMAND) || (command == RAW_ADD_COMMAND)) {
        if (!moduleName) {
            char *readOnlyStr, *noCertDBStr, *sep;
            if (!secmodName)
                secmodName = "secmod.db";
            if (!dbprefix)
                dbprefix = "";
            sep = ((command == RAW_LIST_COMMAND) && nocertdb) ? "," : " ";
            readOnlyStr = (command == RAW_LIST_COMMAND) ? "readOnly" : "";
            noCertDBStr = nocertdb ? "noCertDB" : "";
            SECU_ConfigDirectory(dbdir);

            moduleName = PR_smprintf(
                "name=\"NSS default Module DB\" parameters=\"configdir=%s certPrefix=%s "
                "keyPrefix=%s secmod=%s flags=%s%s%s\" NSS=\"flags=internal,moduleDB,"
                "moduleDBOnly,critical\"",
                SECU_ConfigDirectory(NULL), dbprefix, dbprefix,
                secmodName, readOnlyStr, sep, noCertDBStr);
        }
        if (command == RAW_LIST_COMMAND) {
            errcode = RawListModule(moduleName);
        } else {
            PORT_Assert(moduleSpec);
            errcode = RawAddModule(moduleName, moduleSpec);
        }
        goto loser;
    }

    errcode = init_crypto(createdb, readOnly);
    if (errcode != SUCCESS) {
        goto loser;
    }

    errcode = LoadMechanismList();
    if (errcode != SUCCESS) {
        goto loser;
    }

    /* Warn if we are adding a module while p11-kit is enabled in the
     * database. */
    if ((command == ADD_COMMAND || command == RAW_ADD_COMMAND) &&
        IsP11KitEnabled()) {
        char* response;

        PR_fprintf(PR_STDOUT, msgStrings[P11_KIT_ENABLED_MSG]);
        if (!PR_fgets(stdinbuf, STDINBUF_SIZE, PR_STDIN)) {
            PR_fprintf(PR_STDERR, errStrings[STDIN_READ_ERR]);
            errcode = STDIN_READ_ERR;
            goto loser;
        }
        if ((response = strtok(stdinbuf, " \r\n\t"))) {
            if (!PL_strcasecmp(response, "q")) {
                PR_fprintf(PR_STDOUT, msgStrings[ABORTING_MSG]);
                errcode = SUCCESS;
                goto loser;
            }
        }
        PR_fprintf(PR_STDOUT, "\n");
    }

    /* Execute the command */
    switch (command) {
        case ADD_COMMAND:
            errcode = AddModule(moduleName, libFile, ciphers, mechanisms, secmodString);
            break;
        case CHANGEPW_COMMAND:
            errcode = ChangePW(tokenName, pwFile, newpwFile);
            break;
        case CREATE_COMMAND:
            errcode = InitPW();
            break;
        case DEFAULT_COMMAND:
            errcode = SetDefaultModule(moduleName, slotName, mechanisms);
            break;
        case DELETE_COMMAND:
            errcode = DeleteModule(moduleName);
            break;
        case DISABLE_COMMAND:
            errcode = EnableModule(moduleName, slotName, PR_FALSE);
            break;
        case ENABLE_COMMAND:
            errcode = EnableModule(moduleName, slotName, PR_TRUE);
            break;
        case FIPS_COMMAND:
            errcode = FipsMode(fipsArg);
            break;
        case CHKFIPS_COMMAND:
            errcode = ChkFipsMode(fipsArg);
            break;
        case JAR_COMMAND:
            Pk11Install_SetErrorHandler(install_error);
            errcode = Pk11Install_DoInstall(jarFile, installDir, tempDir,
                                            PR_STDOUT, force, nocertdb);
            break;
        case LIST_COMMAND:
            if (moduleName) {
                errcode = ListModule(moduleName);
            } else {
                errcode = ListModules();
            }
            break;
        case UNDEFAULT_COMMAND:
            errcode = UnsetDefaultModule(moduleName, slotName, mechanisms);
            break;
        default:
            PR_fprintf(PR_STDERR, "This command is not supported yet.\n");
            errcode = INVALID_USAGE_ERR;
            break;
    }

    if (NSS_Shutdown() != SECSuccess) {
        exit(1);
    }

loser:
    PR_Cleanup();
    return errcode;
}

/************************************************************************
 *
 * i n s t a l l _ e r r o r
 *
 * Callback function to handle errors in PK11 JAR file installation.
 */
static void
install_error(char* message)
{
    PR_fprintf(PR_STDERR, "Install error: %s\n", message);
}

/*************************************************************************
 *
 * o u t _ o f _ m e m o r y
 */
void
out_of_memory(void)
{
    PR_fprintf(PR_STDERR, errStrings[OUT_OF_MEM_ERR]);
    exit(OUT_OF_MEM_ERR);
}

/**************************************************************************
 *
 * P R _ f g e t s
 *
 * fgets implemented with NSPR.
 */
static char*
PR_fgets(char* buf, int size, PRFileDesc* file)
{
    int i;
    int status;
    char c;

    i = 0;
    while (i < size - 1) {
        status = PR_Read(file, (void*)&c, 1);
        if (status == -1) {
            return NULL;
        } else if (status == 0) {
            break;
        }
        buf[i++] = c;
        if (c == '\n') {
            break;
        }
    }
    buf[i] = '\0';

    return buf;
}