Back to index

lightning-sunbird  0.9+nobinonly
instsec.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include <plarena.h>
00038 #include <prio.h>
00039 #include <prprf.h>
00040 #include <seccomon.h>
00041 #include <secmod.h>
00042 #include <jar.h>
00043 #include <secutil.h>
00044 
00045 /* These are installation functions that make calls to the security library.
00046  * We don't want to include security include files in the C++ code too much.
00047  */
00048 
00049 static char* PR_fgets(char *buf, int size, PRFileDesc *file);
00050 
00051 /***************************************************************************
00052  *
00053  * P k 1 1 I n s t a l l _ A d d N e w M o d u l e
00054  */
00055 int
00056 Pk11Install_AddNewModule(char* moduleName, char* dllPath,
00057                               unsigned long defaultMechanismFlags,
00058                               unsigned long cipherEnableFlags)
00059 {
00060        return (SECMOD_AddNewModule(moduleName, dllPath,
00061               SECMOD_PubMechFlagstoInternal(defaultMechanismFlags),
00062               SECMOD_PubCipherFlagstoInternal(cipherEnableFlags))
00063                                                                                            == SECSuccess) ? 0 : -1;
00064 }
00065 
00066 /*************************************************************************
00067  *
00068  * P k 1 1 I n s t a l l _ U s e r V e r i f y J a r
00069  *
00070  * Gives the user feedback on the signatures of a JAR files, asks them
00071  * whether they actually want to continue.
00072  * Assumes the jar structure has already been created and is valid.
00073  * Returns 0 if the user wants to continue the installation, nonzero
00074  * if the user wishes to abort.
00075  */
00076 short
00077 Pk11Install_UserVerifyJar(JAR *jar, PRFileDesc *out, PRBool query)
00078 {
00079        JAR_Context *ctx;
00080        JAR_Cert *fing;
00081        JAR_Item *item;
00082        char stdinbuf[80];
00083        int count=0;
00084 
00085        CERTCertificate *cert, *prev=NULL;
00086 
00087        PR_fprintf(out, "\nThis installation JAR file was signed by:\n");
00088 
00089        ctx = JAR_find(jar, NULL, jarTypeSign);
00090 
00091        while(JAR_find_next(ctx, &item) >= 0 ) {
00092               fing = (JAR_Cert*) item->data;
00093               cert = fing->cert;
00094               if(cert==prev) {
00095                      continue;
00096               }
00097 
00098               count++;
00099               PR_fprintf(out, "----------------------------------------------\n");
00100               if(cert) {
00101                      if(cert->nickname) {
00102                             PR_fprintf(out, "**NICKNAME**\n%s\n", cert->nickname);
00103                      }
00104                      if(cert->subjectName) {
00105                             PR_fprintf(out, "**SUBJECT NAME**\n%s\n", cert->subjectName); }
00106                      if(cert->issuerName) {
00107                             PR_fprintf(out, "**ISSUER NAME**\n%s\n", cert->issuerName);
00108                      }
00109               } else {
00110                      PR_fprintf(out, "No matching certificate could be found.\n");
00111               }
00112               PR_fprintf(out, "----------------------------------------------\n\n");
00113 
00114               prev=cert;
00115        }
00116 
00117        JAR_find_end(ctx);
00118 
00119        if(count==0) {
00120               PR_fprintf(out, "No signatures found: JAR FILE IS UNSIGNED.\n");
00121        }
00122 
00123        if(query) {
00124               PR_fprintf(out,
00125 "Do you wish to continue this installation? (y/n) ");
00126 
00127               if(PR_fgets(stdinbuf, 80, PR_STDIN) != NULL) {
00128                      char *response;
00129 
00130                      if( (response=strtok(stdinbuf, " \t\n\r")) ) {
00131                             if( !PL_strcasecmp(response, "y") ||
00132                                    !PL_strcasecmp(response, "yes") ) {
00133                                    return 0;
00134                             }
00135                      }
00136               }
00137        }
00138 
00139        return 1;
00140 }
00141 
00142 /**************************************************************************
00143  *
00144  * P R _ f g e t s
00145  *
00146  * fgets implemented with NSPR.
00147  */
00148 static char*
00149 PR_fgets(char *buf, int size, PRFileDesc *file)
00150 {
00151     int i;
00152     int status;
00153     char c;
00154 
00155     i=0;
00156     while(i < size-1) {
00157         status = PR_Read(file, (void*) &c, 1);
00158         if(status==-1) {
00159             return NULL;
00160         } else if(status==0) {
00161             break;
00162         }
00163         buf[i++] = c;
00164         if(c=='\n') {
00165             break;
00166         }
00167     }
00168     buf[i]='\0';
00169 
00170     return buf;
00171 }
00172 
00173 /**************************************************************************
00174  *
00175  * m y S E C U _ E r r o r S t r i n g
00176  *
00177  */
00178 const char* mySECU_ErrorString(int16 errnum)
00179 {
00180        return SECU_Strerror(errnum);
00181 }