Back to index

lightning-sunbird  0.9+nobinonly
dbtest.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  *   Sonja Mirtitsch Sun Microsystems
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** dbtest.c
00040 **
00041 ** QA test for cert and key databases, especially to open
00042 ** database readonly (NSS_INIT_READONLY) and force initializations
00043 ** even if the databases cannot be opened (NSS_INIT_FORCEOPEN)
00044 **
00045 */
00046 #include <stdio.h>
00047 #include <string.h>
00048 
00049 #if defined(WIN32)
00050 #include "fcntl.h"
00051 #include "io.h"
00052 #endif
00053 
00054 #include "secutil.h"
00055 
00056 #if defined(XP_UNIX)
00057 #include <unistd.h>
00058 #endif
00059 
00060 #include "nspr.h"
00061 #include "prtypes.h"
00062 #include "certdb.h"
00063 #include "nss.h"
00064 #include "../modutil/modutil.h"
00065 
00066 #include "plgetopt.h"
00067 
00068 static char *progName;
00069 
00070 char *dbDir  =  NULL;
00071 
00072 static char *dbName[]={"secmod.db", "cert8.db", "key3.db"}; 
00073 static char* dbprefix = "";
00074 static char* secmodName = "secmod.db";
00075 PRBool verbose;
00076 
00077 
00078 static void Usage(const char *progName)
00079 {
00080     printf("Usage:  %s [-r] [-f] [-d dbdir ] \n",
00081          progName);
00082     printf("%-20s open database readonly (NSS_INIT_READONLY)\n", "-r");
00083     printf("%-20s Continue to force initializations even if the\n", "-f");
00084     printf("%-20s databases cannot be opened (NSS_INIT_FORCEOPEN)\n", " ");
00085     printf("%-20s Directory with cert database (default is .\n",
00086           "-d certdir");
00087     exit(1);
00088 }
00089 
00090 int main(int argc, char **argv)
00091 {
00092     PLOptState *optstate;
00093     PLOptStatus optstatus;
00094 
00095     PRUint32 flags = 0;
00096     Error ret;
00097     SECStatus rv;
00098     char * dbString = NULL;
00099     int i;
00100 
00101     progName = strrchr(argv[0], '/');
00102     if (!progName)
00103         progName = strrchr(argv[0], '\\');
00104     progName = progName ? progName+1 : argv[0];
00105 
00106     optstate = PL_CreateOptState(argc, argv, "rfd:h");
00107 
00108     while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
00109         switch (optstate->option) {
00110           case 'h':
00111           default : Usage(progName);                    break;
00112 
00113           case 'r': flags |= NSS_INIT_READONLY;         break;
00114 
00115           case 'f': flags |= NSS_INIT_FORCEOPEN;        break;
00116 
00117           case 'd':
00118                 dbDir = PORT_Strdup(optstate->value);
00119                 break;
00120 
00121         }
00122     }
00123     if (optstatus == PL_OPT_BAD)
00124         Usage(progName);
00125 
00126     if (!dbDir) {
00127         dbDir = SECU_DefaultSSLDir(); /* Look in $SSL_DIR */
00128     }
00129     dbDir = SECU_ConfigDirectory(dbDir);
00130     PR_fprintf(PR_STDERR, "dbdir selected is %s\n\n", dbDir);
00131 
00132     if( dbDir[0] == '\0') {
00133         PR_fprintf(PR_STDERR, errStrings[DIR_DOESNT_EXIST_ERR], dbDir);
00134         ret= DIR_DOESNT_EXIST_ERR;
00135         goto loser;
00136     }
00137 
00138 
00139     PR_Init( PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00140 
00141     /* get the status of the directory and databases and output message */
00142     if(PR_Access(dbDir, PR_ACCESS_EXISTS) != PR_SUCCESS) {
00143         PR_fprintf(PR_STDERR, errStrings[DIR_DOESNT_EXIST_ERR], dbDir);
00144     } else if(PR_Access(dbDir, PR_ACCESS_READ_OK) != PR_SUCCESS) {
00145         PR_fprintf(PR_STDERR, errStrings[DIR_NOT_READABLE_ERR], dbDir);
00146     } else {
00147         if( !( flags & NSS_INIT_READONLY ) &&
00148                 PR_Access(dbDir, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
00149             PR_fprintf(PR_STDERR, errStrings[DIR_NOT_WRITEABLE_ERR], dbDir);
00150         }
00151         for (i=0;i<3;i++) {
00152             dbString=PR_smprintf("%s/%s",dbDir,dbName[i]);
00153             PR_fprintf(PR_STDOUT, "database checked is %s\n",dbString);
00154             if(PR_Access(dbString, PR_ACCESS_EXISTS) != PR_SUCCESS) {
00155                 PR_fprintf(PR_STDERR, errStrings[FILE_DOESNT_EXIST_ERR], 
00156                                       dbString);
00157             } else if(PR_Access(dbString, PR_ACCESS_READ_OK) != PR_SUCCESS) {
00158                 PR_fprintf(PR_STDERR, errStrings[FILE_NOT_READABLE_ERR], 
00159                                       dbString);
00160             } else if( !( flags & NSS_INIT_READONLY ) &&
00161                     PR_Access(dbString, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
00162                 PR_fprintf(PR_STDERR, errStrings[FILE_NOT_WRITEABLE_ERR], 
00163                                       dbString);
00164             }
00165         }
00166     }
00167 
00168     rv = NSS_Initialize(SECU_ConfigDirectory(dbDir), dbprefix, dbprefix,
00169                    secmodName, flags);
00170     if (rv != SECSuccess) {
00171         SECU_PrintPRandOSError(progName);
00172         ret=NSS_INITIALIZE_FAILED_ERR;
00173     } else {
00174         if (NSS_Shutdown() != SECSuccess) {
00175             exit(1);
00176         }
00177         ret=SUCCESS;
00178     }
00179 
00180 loser:
00181     return ret;
00182 }
00183