install.c

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

C
980
字号
/* * 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 "install.h"#include "install-ds.h"#include <prlock.h>#include <prio.h>#include <prmem.h>#include <prprf.h>#include <prsystem.h>#include <prproces.h>/*extern "C" {*/#include <jar.h>/*}*/extern /*"C"*/int Pk11Install_AddNewModule(char* moduleName, char* dllPath,                              unsigned long defaultMechanismFlags,                              unsigned long cipherEnableFlags);extern /*"C"*/short Pk11Install_UserVerifyJar(JAR *jar, PRFileDesc *out,	PRBool query);extern /*"C"*/char* mySECU_ErrorString(int16);extern int Pk11Install_yyparse();#define INSTALL_METAINFO_TAG "Pkcs11_install_script"#define SCRIPT_TEMP_FILE "pkcs11inst.tmp"#define ROOT_MARKER "%root%"#define TEMP_MARKER "%temp%"#define PRINTF_ROOT_MARKER "%%root%%"#define TEMPORARY_DIRECTORY_NAME "pk11inst.dir"#define JAR_BASE_END (JAR_BASE+100)static PRLock* errorHandlerLock=NULL;static Pk11Install_ErrorHandler errorHandler=NULL;static char* PR_Strdup(const char* str);static int rm_dash_r (char *path);static int make_dirs(char *path, int file_perms);static int dir_perms(int perms);static Pk11Install_Error DoInstall(JAR *jar, const char *installDir,	const char* tempDir, Pk11Install_Platform *platform, 	PRFileDesc *feedback, PRBool noverify);static char *errorString[]= {	"Operation was successful", /* PK11_INSTALL_NO_ERROR */	"Directory \"%s\" does not exist", /* PK11_INSTALL_DIR_DOESNT_EXIST */	"File \"%s\" does not exist", /* PK11_INSTALL_FILE_DOESNT_EXIST */	"File \"%s\" is not readable", /* PK11_INSTALL_FILE_NOT_READABLE */	"%s",		/* PK11_INSTALL_ERROR_STRING */	"Error in JAR file %s: %s",  /* PK11_INSTALL_JAR_ERROR */	"No Pkcs11_install_script specified in JAR metainfo file",				/* PK11_INSTALL_NO_INSTALLER_SCRIPT */	"Could not delete temporary file \"%s\"",				/*PK11_INSTALL_DELETE_TEMP_FILE */	"Could not open temporary file \"%s\"", /*PK11_INSTALL_OPEN_SCRIPT_FILE*/	"%s: %s", /* PK11_INSTALL_SCRIPT_PARSE */	"Error in script: %s",	"Unable to obtain system platform information",	"Installer script has no information about the current platform (%s)",	"Relative directory \"%s\" does not contain "PRINTF_ROOT_MARKER,	"Module File \"%s\" not found",	"Error occurred installing module \"%s\" into database",	"Error extracting \"%s\" from JAR file: %s",	"Directory \"%s\" is not writeable",	"Could not create directory \"%s\"",	"Could not remove directory \"%s\"",	"Unable to execute \"%s\"",	"Unable to wait for process \"%s\"",	"\"%s\" returned error code %d",	"User aborted operation",	"Unspecified error"};enum {	INSTALLED_FILE_MSG=0,	INSTALLED_MODULE_MSG,	INSTALLER_SCRIPT_NAME,	MY_PLATFORM_IS,	USING_PLATFORM,	PARSED_INSTALL_SCRIPT,	EXEC_FILE_MSG,	EXEC_SUCCESS,	INSTALLATION_COMPLETE_MSG,	USER_ABORT};static char *msgStrings[] = {	"Installed file %s to %s\n",	"Installed module \"%s\" into module database\n",	"Using installer script \"%s\"\n",	"Current platform is %s\n",	"Using installation parameters for platform %s\n",	"Successfully parsed installation script\n",	"Executing \"%s\"...\n",	"\"%s\" executed successfully\n",	"\nInstallation completed successfully\n",	"\nAborting...\n"};/************************************************************************** * S t r i n g N o d e */typedef struct StringNode_str {    char *str;    struct StringNode_str* next;} StringNode;StringNode* StringNode_new(){	StringNode* new_this;	new_this = (StringNode*)malloc(sizeof(StringNode));  new_this->str=NULL;  new_this->next=NULL;	return new_this;}void StringNode_delete(StringNode* s) {	if(s->str) {		PR_Free(s->str);		s->str=NULL;	}}/************************************************************************* * S t r i n g L i s t */typedef struct StringList_str {  StringNode* head;	StringNode* tail;} StringList;void StringList_new(StringList* list){  list->head=NULL;  list->tail=NULL;}void StringList_delete(StringList* list){	StringNode *tmp;	while(list->head) {		tmp = list->head;		list->head = list->head->next;		StringNode_delete(tmp);	}}voidStringList_Append(StringList* list, char* str){	if(!str) {		return;	}	if(!list->tail) {		/* This is the first element */	  list->head = list->tail = StringNode_new();	} else {		list->tail->next = StringNode_new();		list->tail = list->tail->next;	}	list->tail->str = PR_Strdup(str);	list->tail->next = NULL;	/* just to be sure */}/************************************************************************** * * P k 1 1 I n s t a l l _ S e t E r r o r H a n d l e r * * Sets the error handler to be used by the library.  Returns the current * error handler function. */Pk11Install_ErrorHandlerPk11Install_SetErrorHandler(Pk11Install_ErrorHandler handler){	Pk11Install_ErrorHandler old;	if(!errorHandlerLock) {		errorHandlerLock = PR_NewLock();	}	PR_Lock(errorHandlerLock);	old = errorHandler;	errorHandler = handler;	PR_Unlock(errorHandlerLock);	return old;}/************************************************************************** * * P k 1 1 I n s t a l l _ I n i t * * Does initialization that otherwise would be done on the fly.  Only * needs to be called by multithreaded apps, before they make any calls * to this library. */voidPk11Install_Init(){	if(!errorHandlerLock) {		errorHandlerLock = PR_NewLock();	}}/************************************************************************** * * P k 1 1 I n s t a l l _ R e l e a s e * * Releases static data structures used by the library.  Don't use the * library after calling this, unless you call Pk11Install_Init() * first.  This function doesn't have to be called at all unless you're * really anal about freeing memory before your program exits. */voidPk11Install_Release(){	if(errorHandlerLock) {		PR_Free(errorHandlerLock);		errorHandlerLock = NULL;	}}/************************************************************************* * * e r r o r * * Takes an error code and its arguments, creates the error string, * and sends the string to the handler function if it exists. */#ifdef OSF1/* stdarg has already been pulled in from NSPR */#undef va_start#undef va_end#undef va_arg#include <varargs.h>#else#include <stdarg.h>#endif#ifdef OSF1static voiderror(long va_alist, ...)#elsestatic voiderror(Pk11Install_Error errcode, ...)#endif{	va_list ap;	char *errstr;	Pk11Install_ErrorHandler handler;	if(!errorHandlerLock) {		errorHandlerLock = PR_NewLock();	}	PR_Lock(errorHandlerLock);	handler = errorHandler;	PR_Unlock(errorHandlerLock);	if(handler) {#ifdef OSF1		va_start(ap);		errstr = PR_vsmprintf(errorString[va_arg(ap, Pk11Install_Error)], ap);#else		va_start(ap, errcode);		errstr = PR_vsmprintf(errorString[errcode], ap);#endif		handler(errstr);		PR_smprintf_free(errstr);		va_end(ap);	}}/************************************************************************* * * j a r _ c a l l b a c k */static intjar_callback(int status, JAR *foo, const char *bar, char *pathname,	char *errortext) {	char *string;	string = PR_smprintf("JAR error %d: %s in file %s\n", status, errortext,		pathname);	error(PK11_INSTALL_ERROR_STRING, string);	PR_smprintf_free(string);	return 0;}	/************************************************************************* * * P k 1 1 I n s t a l l _ D o I n s t a l l * * jarFile is the path of a JAR in the PKCS #11 module JAR format. * installDir is the directory relative to which files will be *   installed. */Pk11Install_ErrorPk11Install_DoInstall(char *jarFile, const char *installDir,	const char *tempDir, PRFileDesc *feedback, short force, PRBool noverify){	JAR *jar;	char *installer;	unsigned long installer_len;	int status;	Pk11Install_Error ret;	PRBool made_temp_file;	Pk11Install_Info installInfo;	Pk11Install_Platform *platform;	char* errMsg;	char sysname[SYS_INFO_BUFFER_LENGTH], release[SYS_INFO_BUFFER_LENGTH],       arch[SYS_INFO_BUFFER_LENGTH];	char *myPlatform;	jar=NULL;	ret = PK11_INSTALL_UNSPECIFIED;	made_temp_file=PR_FALSE;	errMsg=NULL;	Pk11Install_Info_init(&installInfo);	/*	printf("Inside DoInstall, jarFile=%s, installDir=%s, tempDir=%s\n",		jarFile, installDir, tempDir);	*/	/*	 * Check out jarFile and installDir for validity	 */	if( PR_Access(installDir, PR_ACCESS_EXISTS) != PR_SUCCESS ) {		error(PK11_INSTALL_DIR_DOESNT_EXIST, installDir);		return PK11_INSTALL_DIR_DOESNT_EXIST;	}	if(!tempDir) {		tempDir = ".";	}	if( PR_Access(tempDir, PR_ACCESS_EXISTS) != PR_SUCCESS ) {		error(PK11_INSTALL_DIR_DOESNT_EXIST, tempDir);		return PK11_INSTALL_DIR_DOESNT_EXIST;	}	if( PR_Access(tempDir, PR_ACCESS_WRITE_OK) != PR_SUCCESS ) {		error(PK11_INSTALL_DIR_NOT_WRITEABLE, tempDir);		return PK11_INSTALL_DIR_NOT_WRITEABLE;	}	if( (PR_Access(jarFile, PR_ACCESS_EXISTS) != PR_SUCCESS) ) {		error(PK11_INSTALL_FILE_DOESNT_EXIST, jarFile);		return PK11_INSTALL_FILE_DOESNT_EXIST;	}	if( PR_Access(jarFile, PR_ACCESS_READ_OK) != PR_SUCCESS ) {		error(PK11_INSTALL_FILE_NOT_READABLE, jarFile);		return PK11_INSTALL_FILE_NOT_READABLE;	}	/*	 * Extract the JAR file	 */	jar = JAR_new();	JAR_set_callback(JAR_CB_SIGNAL, jar, jar_callback);	if(noverify) {		status = JAR_pass_archive_unverified(jar, jarArchGuess, jarFile, "url");	} else {		status = JAR_pass_archive(jar, jarArchGuess, jarFile, "url");	}	if( (status < 0) || (jar->valid < 0) ) {		if (status >= JAR_BASE && status <= JAR_BASE_END) {			error(PK11_INSTALL_JAR_ERROR, jarFile, JAR_get_error(status));		} else {			error(PK11_INSTALL_JAR_ERROR, jarFile,			  mySECU_ErrorString((int16) PORT_GetError()) );		}		ret=PK11_INSTALL_JAR_ERROR;		goto loser;	}	/*printf("passed the archive\n");*/	/*	 * Show the user security information, allow them to abort or continue	 */	if( Pk11Install_UserVerifyJar(jar, PR_STDOUT,		force?PR_FALSE:PR_TRUE) && !force) {		if(feedback) {			PR_fprintf(feedback, msgStrings[USER_ABORT]);		}		ret=PK11_INSTALL_USER_ABORT;		goto loser;	}	/*	 * Get the name of the installation file	 */	if( JAR_get_metainfo(jar, NULL, INSTALL_METAINFO_TAG, (void**)&installer,		(unsigned long*)&installer_len) ) {		error(PK11_INSTALL_NO_INSTALLER_SCRIPT);		ret=PK11_INSTALL_NO_INSTALLER_SCRIPT;		goto loser;	}	if(feedback) {		PR_fprintf(feedback, msgStrings[INSTALLER_SCRIPT_NAME], installer);	}	/*	 * Extract the installation file	 */	if( PR_Access(SCRIPT_TEMP_FILE, PR_ACCESS_EXISTS) == PR_SUCCESS) {		if( PR_Delete(SCRIPT_TEMP_FILE) != PR_SUCCESS) {			error(PK11_INSTALL_DELETE_TEMP_FILE, SCRIPT_TEMP_FILE);			ret=PK11_INSTALL_DELETE_TEMP_FILE;			goto loser;		}	}	if(noverify) {		status = JAR_extract(jar, installer, SCRIPT_TEMP_FILE);	} else {		status = JAR_verified_extract(jar, installer, SCRIPT_TEMP_FILE);	}	if(status) {		if (status >= JAR_BASE && status <= JAR_BASE_END) {			error(PK11_INSTALL_JAR_EXTRACT, installer, JAR_get_error(status));		} else {			error(PK11_INSTALL_JAR_EXTRACT, installer,			  mySECU_ErrorString((int16) PORT_GetError()) );		}		ret = PK11_INSTALL_JAR_EXTRACT;		goto loser;	} else {		made_temp_file = PR_TRUE;	}	/*	 * Parse the installation file into a syntax tree	 */	Pk11Install_FD = PR_Open(SCRIPT_TEMP_FILE, PR_RDONLY, 0);	if(!Pk11Install_FD) {		error(PK11_INSTALL_OPEN_SCRIPT_FILE, SCRIPT_TEMP_FILE);		ret=PK11_INSTALL_OPEN_SCRIPT_FILE;		goto loser;	}	if(Pk11Install_yyparse()) {		error(PK11_INSTALL_SCRIPT_PARSE, installer,			Pk11Install_yyerrstr ? Pk11Install_yyerrstr : "");		ret=PK11_INSTALL_SCRIPT_PARSE;		goto loser;	}#if 0	/* for debugging */	Pk11Install_valueList->Print(0);

⌨️ 快捷键说明

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