nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / cmd / modutil / instsec.c
blobcfc0082342d46a21e08dcf84a60c848b58cf7b4c
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include <plarena.h>
38 #include <prio.h>
39 #include <prprf.h>
40 #include <seccomon.h>
41 #include <secmod.h>
42 #include <jar.h>
43 #include <secutil.h>
45 /* These are installation functions that make calls to the security library.
46 * We don't want to include security include files in the C++ code too much.
49 static char* PR_fgets(char *buf, int size, PRFileDesc *file);
51 /***************************************************************************
53 * P k 1 1 I n s t a l l _ A d d N e w M o d u l e
55 int
56 Pk11Install_AddNewModule(char* moduleName, char* dllPath,
57 unsigned long defaultMechanismFlags,
58 unsigned long cipherEnableFlags)
60 return (SECMOD_AddNewModule(moduleName, dllPath,
61 SECMOD_PubMechFlagstoInternal(defaultMechanismFlags),
62 SECMOD_PubCipherFlagstoInternal(cipherEnableFlags))
63 == SECSuccess) ? 0 : -1;
66 /*************************************************************************
68 * P k 1 1 I n s t a l l _ U s e r V e r i f y J a r
70 * Gives the user feedback on the signatures of a JAR files, asks them
71 * whether they actually want to continue.
72 * Assumes the jar structure has already been created and is valid.
73 * Returns 0 if the user wants to continue the installation, nonzero
74 * if the user wishes to abort.
76 short
77 Pk11Install_UserVerifyJar(JAR *jar, PRFileDesc *out, PRBool query)
79 JAR_Context *ctx;
80 JAR_Cert *fing;
81 JAR_Item *item;
82 char stdinbuf[80];
83 int count=0;
85 CERTCertificate *cert, *prev=NULL;
87 PR_fprintf(out, "\nThis installation JAR file was signed by:\n");
89 ctx = JAR_find(jar, NULL, jarTypeSign);
91 while(JAR_find_next(ctx, &item) >= 0 ) {
92 fing = (JAR_Cert*) item->data;
93 cert = fing->cert;
94 if(cert==prev) {
95 continue;
98 count++;
99 PR_fprintf(out, "----------------------------------------------\n");
100 if(cert) {
101 if(cert->nickname) {
102 PR_fprintf(out, "**NICKNAME**\n%s\n", cert->nickname);
104 if(cert->subjectName) {
105 PR_fprintf(out, "**SUBJECT NAME**\n%s\n", cert->subjectName); }
106 if(cert->issuerName) {
107 PR_fprintf(out, "**ISSUER NAME**\n%s\n", cert->issuerName);
109 } else {
110 PR_fprintf(out, "No matching certificate could be found.\n");
112 PR_fprintf(out, "----------------------------------------------\n\n");
114 prev=cert;
117 JAR_find_end(ctx);
119 if(count==0) {
120 PR_fprintf(out, "No signatures found: JAR FILE IS UNSIGNED.\n");
123 if(query) {
124 PR_fprintf(out,
125 "Do you wish to continue this installation? (y/n) ");
127 if(PR_fgets(stdinbuf, 80, PR_STDIN) != NULL) {
128 char *response;
130 if( (response=strtok(stdinbuf, " \t\n\r")) ) {
131 if( !PL_strcasecmp(response, "y") ||
132 !PL_strcasecmp(response, "yes") ) {
133 return 0;
139 return 1;
142 /**************************************************************************
144 * P R _ f g e t s
146 * fgets implemented with NSPR.
148 static char*
149 PR_fgets(char *buf, int size, PRFileDesc *file)
151 int i;
152 int status;
153 char c;
155 i=0;
156 while(i < size-1) {
157 status = PR_Read(file, (void*) &c, 1);
158 if(status==-1) {
159 return NULL;
160 } else if(status==0) {
161 break;
163 buf[i++] = c;
164 if(c=='\n') {
165 break;
168 buf[i]='\0';
170 return buf;
173 /**************************************************************************
175 * m y S E C U _ E r r o r S t r i n g
178 const char* mySECU_ErrorString(int16 errnum)
180 return SECU_Strerror(errnum);