modutil.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 875 行 · 第 1/2 页

C
875
字号
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape security libraries. *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. */#include "modutil.h"#include "install.h"#include <plstr.h>#include "secrng.h"#include "certdb.h" /* for CERT_DB_FILE_VERSION */static void install_error(char *message);static char* PR_fgets(char *buf, int size, PRFileDesc *file);/* 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,	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",	"-undefault"};/* this enum must be kept in sync with the optionStrings list */typedef enum {	ADD_ARG=0,	CHANGEPW_ARG,	CIPHERS_ARG,	CREATE_ARG,	DBDIR_ARG,	DEFAULT_ARG,	DELETE_ARG,	DISABLE_ARG,	ENABLE_ARG,	FIPS_ARG,	FORCE_ARG,	JAR_ARG,	LIBFILE_ARG,	LIST_ARG,	MECHANISMS_ARG,	NEWPWFILE_ARG,	PWFILE_ARG,	SLOT_ARG,	UNDEFAULT_ARG,	INSTALLDIR_ARG,	TEMPDIR_ARG,	NOCERTDB_ARG,	NUM_ARGS	/* must be last */} Arg;/* This list must be kept in sync with the Arg enum */static char *optionStrings[] = {	"-add",	"-changepw",	"-ciphers",	"-create",	"-dbdir",	"-default",	"-delete",	"-disable",	"-enable",	"-fips",	"-force",	"-jar",	"-libfile",	"-list",	"-mechanisms",	"-newpwfile",	"-pwfile",	"-slot",	"-undefault",	"-installdir",	"-tempdir",	"-nocertdb"};/* 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* slotName = NULL;static char* tokenName = NULL;static char* libFile = NULL;static char* dbdir = 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 Errorparse_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 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 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 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;		}	}	return SUCCESS;}/************************************************************************ * * v e r i f y _ p a r a m s */static Errorverify_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:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?