security/nss/cmd/modutil/instsec.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/modutil/instsec.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include <plarena.h>
     1.9 +#include <prerror.h>
    1.10 +#include <prio.h>
    1.11 +#include <prprf.h>
    1.12 +#include <seccomon.h>
    1.13 +#include <secmod.h>
    1.14 +#include <jar.h>
    1.15 +#include <secutil.h>
    1.16 +
    1.17 +/* These are installation functions that make calls to the security library.
    1.18 + * We don't want to include security include files in the C++ code too much.
    1.19 + */
    1.20 +
    1.21 +static char* PR_fgets(char *buf, int size, PRFileDesc *file);
    1.22 +
    1.23 +/***************************************************************************
    1.24 + *
    1.25 + * P k 1 1 I n s t a l l _ A d d N e w M o d u l e
    1.26 + */
    1.27 +int
    1.28 +Pk11Install_AddNewModule(char* moduleName, char* dllPath,
    1.29 +                              unsigned long defaultMechanismFlags,
    1.30 +                              unsigned long cipherEnableFlags)
    1.31 +{
    1.32 +	return (SECMOD_AddNewModule(moduleName, dllPath,
    1.33 +		SECMOD_PubMechFlagstoInternal(defaultMechanismFlags),
    1.34 +		SECMOD_PubCipherFlagstoInternal(cipherEnableFlags))
    1.35 +													== SECSuccess) ? 0 : -1;
    1.36 +}
    1.37 +
    1.38 +/*************************************************************************
    1.39 + *
    1.40 + * P k 1 1 I n s t a l l _ U s e r V e r i f y J a r
    1.41 + *
    1.42 + * Gives the user feedback on the signatures of a JAR files, asks them
    1.43 + * whether they actually want to continue.
    1.44 + * Assumes the jar structure has already been created and is valid.
    1.45 + * Returns 0 if the user wants to continue the installation, nonzero
    1.46 + * if the user wishes to abort.
    1.47 + */
    1.48 +short
    1.49 +Pk11Install_UserVerifyJar(JAR *jar, PRFileDesc *out, PRBool query)
    1.50 +{
    1.51 +	JAR_Context *ctx;
    1.52 +	JAR_Cert *fing;
    1.53 +	JAR_Item *item;
    1.54 +	char stdinbuf[80];
    1.55 +	int count=0;
    1.56 +
    1.57 +	CERTCertificate *cert, *prev=NULL;
    1.58 +
    1.59 +	PR_fprintf(out, "\nThis installation JAR file was signed by:\n");
    1.60 +
    1.61 +	ctx = JAR_find(jar, NULL, jarTypeSign);
    1.62 +
    1.63 +	while(JAR_find_next(ctx, &item) >= 0 ) {
    1.64 +		fing = (JAR_Cert*) item->data;
    1.65 +		cert = fing->cert;
    1.66 +		if(cert==prev) {
    1.67 +			continue;
    1.68 +		}
    1.69 +
    1.70 +		count++;
    1.71 +		PR_fprintf(out, "----------------------------------------------\n");
    1.72 +		if(cert) {
    1.73 +			if(cert->nickname) {
    1.74 +				PR_fprintf(out, "**NICKNAME**\n%s\n", cert->nickname);
    1.75 +			}
    1.76 +			if(cert->subjectName) {
    1.77 +				PR_fprintf(out, "**SUBJECT NAME**\n%s\n", cert->subjectName); }
    1.78 +			if(cert->issuerName) {
    1.79 +				PR_fprintf(out, "**ISSUER NAME**\n%s\n", cert->issuerName);
    1.80 +			}
    1.81 +		} else {
    1.82 +			PR_fprintf(out, "No matching certificate could be found.\n");
    1.83 +		}
    1.84 +		PR_fprintf(out, "----------------------------------------------\n\n");
    1.85 +
    1.86 +		prev=cert;
    1.87 +	}
    1.88 +
    1.89 +	JAR_find_end(ctx);
    1.90 +
    1.91 +	if(count==0) {
    1.92 +		PR_fprintf(out, "No signatures found: JAR FILE IS UNSIGNED.\n");
    1.93 +	}
    1.94 +
    1.95 +	if(query) {
    1.96 +		PR_fprintf(out,
    1.97 +"Do you wish to continue this installation? (y/n) ");
    1.98 +
    1.99 +		if(PR_fgets(stdinbuf, 80, PR_STDIN) != NULL) {
   1.100 +			char *response;
   1.101 +
   1.102 +			if( (response=strtok(stdinbuf, " \t\n\r")) ) {
   1.103 +				if( !PL_strcasecmp(response, "y") ||
   1.104 +					!PL_strcasecmp(response, "yes") ) {
   1.105 +					return 0;
   1.106 +				}
   1.107 +			}
   1.108 +		}
   1.109 +	}
   1.110 +
   1.111 +	return 1;
   1.112 +}
   1.113 +
   1.114 +/**************************************************************************
   1.115 + *
   1.116 + * P R _ f g e t s
   1.117 + *
   1.118 + * fgets implemented with NSPR.
   1.119 + */
   1.120 +static char*
   1.121 +PR_fgets(char *buf, int size, PRFileDesc *file)
   1.122 +{
   1.123 +    int i;
   1.124 +    int status;
   1.125 +    char c;
   1.126 +
   1.127 +    i=0;
   1.128 +    while(i < size-1) {
   1.129 +        status = PR_Read(file, (void*) &c, 1);
   1.130 +        if(status==-1) {
   1.131 +            return NULL;
   1.132 +        } else if(status==0) {
   1.133 +            break;
   1.134 +        }
   1.135 +        buf[i++] = c;
   1.136 +        if(c=='\n') {
   1.137 +            break;
   1.138 +        }
   1.139 +    }
   1.140 +    buf[i]='\0';
   1.141 +
   1.142 +    return buf;
   1.143 +}
   1.144 +
   1.145 +/**************************************************************************
   1.146 + *
   1.147 + * m y S E C U _ E r r o r S t r i n g
   1.148 + *
   1.149 + */
   1.150 +const char* mySECU_ErrorString(PRErrorCode errnum)
   1.151 +{
   1.152 +	return SECU_Strerror(errnum);
   1.153 +}

mercurial