Blame nss/cmd/dbtest/dbtest.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
/*
Packit 40b132
** dbtest.c
Packit 40b132
**
Packit 40b132
** QA test for cert and key databases, especially to open
Packit 40b132
** database readonly (NSS_INIT_READONLY) and force initializations
Packit 40b132
** even if the databases cannot be opened (NSS_INIT_FORCEOPEN)
Packit 40b132
**
Packit 40b132
*/
Packit 40b132
#include <stdio.h>
Packit 40b132
#include <string.h>
Packit 40b132
Packit 40b132
#if defined(WIN32)
Packit 40b132
#include "fcntl.h"
Packit 40b132
#include "io.h"
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#include "secutil.h"
Packit 40b132
#include "pk11pub.h"
Packit 40b132
Packit 40b132
#if defined(XP_UNIX)
Packit 40b132
#include <unistd.h>
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#include "nspr.h"
Packit 40b132
#include "prtypes.h"
Packit 40b132
#include "certdb.h"
Packit 40b132
#include "nss.h"
Packit 40b132
#include "../modutil/modutil.h"
Packit 40b132
Packit 40b132
#include "plgetopt.h"
Packit 40b132
Packit 40b132
static char *progName;
Packit 40b132
Packit 40b132
char *dbDir  =  NULL;
Packit 40b132
Packit 40b132
static char *dbName[]={"secmod.db", "cert8.db", "key3.db"}; 
Packit 40b132
static char* dbprefix = "";
Packit 40b132
static char* secmodName = "secmod.db";
Packit 40b132
static char* userPassword = "";
Packit 40b132
PRBool verbose;
Packit 40b132
Packit 40b132
static char *
Packit 40b132
getPassword(PK11SlotInfo *slot, PRBool retry, void *arg)
Packit 40b132
{
Packit 40b132
    int *success = (int *)arg;
Packit 40b132
Packit 40b132
    if (retry) {
Packit 40b132
	*success = 0;
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    *success = 1;
Packit 40b132
     return PORT_Strdup(userPassword);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
static void Usage(const char *progName)
Packit 40b132
{
Packit 40b132
    printf("Usage:  %s [-r] [-f] [-i] [-d dbdir ] \n",
Packit 40b132
         progName);
Packit 40b132
    printf("%-20s open database readonly (NSS_INIT_READONLY)\n", "-r");
Packit 40b132
    printf("%-20s Continue to force initializations even if the\n", "-f");
Packit 40b132
    printf("%-20s databases cannot be opened (NSS_INIT_FORCEOPEN)\n", " ");
Packit 40b132
    printf("%-20s Try to initialize the database\n", "-i");
Packit 40b132
    printf("%-20s Supply a password with which to initialize the db\n", "-p");
Packit 40b132
    printf("%-20s Directory with cert database (default is .\n",
Packit 40b132
          "-d certdir");
Packit 40b132
    exit(1);
Packit 40b132
}
Packit 40b132
Packit 40b132
int main(int argc, char **argv)
Packit 40b132
{
Packit 40b132
    PLOptState *optstate;
Packit 40b132
    PLOptStatus optstatus;
Packit 40b132
Packit 40b132
    PRUint32 flags = 0;
Packit 40b132
    Error ret;
Packit 40b132
    SECStatus rv;
Packit 40b132
    char * dbString = NULL;
Packit 40b132
    PRBool doInitTest = PR_FALSE;
Packit 40b132
    int i;
Packit 40b132
Packit 40b132
    progName = strrchr(argv[0], '/');
Packit 40b132
    if (!progName)
Packit 40b132
        progName = strrchr(argv[0], '\\');
Packit 40b132
    progName = progName ? progName+1 : argv[0];
Packit 40b132
Packit 40b132
    optstate = PL_CreateOptState(argc, argv, "rfip:d:h");
Packit 40b132
Packit 40b132
    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
Packit 40b132
        switch (optstate->option) {
Packit 40b132
          case 'h':
Packit 40b132
          default : Usage(progName);                    break;
Packit 40b132
Packit 40b132
          case 'r': flags |= NSS_INIT_READONLY;         break;
Packit 40b132
Packit 40b132
          case 'f': flags |= NSS_INIT_FORCEOPEN;        break;
Packit 40b132
Packit 40b132
	  case 'i': doInitTest = PR_TRUE;		break;
Packit 40b132
Packit 40b132
	  case 'p':
Packit 40b132
		userPassword = PORT_Strdup(optstate->value);
Packit 40b132
		break;
Packit 40b132
Packit 40b132
          case 'd':
Packit 40b132
                dbDir = PORT_Strdup(optstate->value);
Packit 40b132
                break;
Packit 40b132
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
    if (optstatus == PL_OPT_BAD)
Packit 40b132
        Usage(progName);
Packit 40b132
Packit 40b132
    if (!dbDir) {
Packit 40b132
        dbDir = SECU_DefaultSSLDir(); /* Look in $SSL_DIR */
Packit 40b132
    }
Packit 40b132
    dbDir = SECU_ConfigDirectory(dbDir);
Packit 40b132
    PR_fprintf(PR_STDERR, "dbdir selected is %s\n\n", dbDir);
Packit 40b132
Packit 40b132
    if( dbDir[0] == '\0') {
Packit 40b132
        PR_fprintf(PR_STDERR, errStrings[DIR_DOESNT_EXIST_ERR], dbDir);
Packit 40b132
        ret= DIR_DOESNT_EXIST_ERR;
Packit 40b132
        goto loser;
Packit 40b132
    }
Packit 40b132
Packit 40b132
Packit 40b132
    PR_Init( PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
Packit 40b132
Packit 40b132
    /* get the status of the directory and databases and output message */
Packit 40b132
    if(PR_Access(dbDir, PR_ACCESS_EXISTS) != PR_SUCCESS) {
Packit 40b132
        PR_fprintf(PR_STDERR, errStrings[DIR_DOESNT_EXIST_ERR], dbDir);
Packit 40b132
    } else if(PR_Access(dbDir, PR_ACCESS_READ_OK) != PR_SUCCESS) {
Packit 40b132
        PR_fprintf(PR_STDERR, errStrings[DIR_NOT_READABLE_ERR], dbDir);
Packit 40b132
    } else {
Packit 40b132
        if( !( flags & NSS_INIT_READONLY ) &&
Packit 40b132
                PR_Access(dbDir, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
Packit 40b132
            PR_fprintf(PR_STDERR, errStrings[DIR_NOT_WRITEABLE_ERR], dbDir);
Packit 40b132
        }
Packit 40b132
	if (!doInitTest) {
Packit 40b132
            for (i=0;i<3;i++) {
Packit 40b132
        	dbString=PR_smprintf("%s/%s",dbDir,dbName[i]);
Packit 40b132
        	PR_fprintf(PR_STDOUT, "database checked is %s\n",dbString);
Packit 40b132
        	if(PR_Access(dbString, PR_ACCESS_EXISTS) != PR_SUCCESS) {
Packit 40b132
                    PR_fprintf(PR_STDERR, errStrings[FILE_DOESNT_EXIST_ERR], 
Packit 40b132
                                      dbString);
Packit 40b132
        	} else if(PR_Access(dbString, PR_ACCESS_READ_OK) != PR_SUCCESS) {
Packit 40b132
                    PR_fprintf(PR_STDERR, errStrings[FILE_NOT_READABLE_ERR], 
Packit 40b132
                                      dbString);
Packit 40b132
        	} else if( !( flags & NSS_INIT_READONLY ) &&
Packit 40b132
                       PR_Access(dbString, PR_ACCESS_WRITE_OK) != PR_SUCCESS) {
Packit 40b132
                    PR_fprintf(PR_STDERR, errStrings[FILE_NOT_WRITEABLE_ERR], 
Packit 40b132
                                      dbString);
Packit 40b132
        	}
Packit 40b132
	    }
Packit 40b132
        } 
Packit 40b132
    }
Packit 40b132
Packit 40b132
Packit 40b132
    rv = NSS_Initialize(SECU_ConfigDirectory(dbDir), dbprefix, dbprefix,
Packit 40b132
                   secmodName, flags);
Packit 40b132
    if (rv != SECSuccess) {
Packit 40b132
        SECU_PrintPRandOSError(progName);
Packit 40b132
        ret=NSS_INITIALIZE_FAILED_ERR;
Packit 40b132
    } else {
Packit 40b132
        ret=SUCCESS;
Packit 40b132
	if (doInitTest) {
Packit 40b132
	    PK11SlotInfo * slot = PK11_GetInternalKeySlot();
Packit 40b132
	    SECStatus rv;
Packit 40b132
	    int passwordSuccess = 0;
Packit 40b132
	    int type = CKM_DES3_CBC;
Packit 40b132
	    SECItem keyid = { 0, NULL, 0 };
Packit 40b132
	    unsigned char keyIdData[] = { 0xff, 0xfe };
Packit 40b132
	    PK11SymKey *key = NULL;
Packit 40b132
Packit 40b132
	    keyid.data = keyIdData;
Packit 40b132
	    keyid.len = sizeof(keyIdData);
Packit 40b132
Packit 40b132
	    PK11_SetPasswordFunc(getPassword);
Packit 40b132
	    rv = PK11_InitPin(slot, (char *)NULL, userPassword);
Packit 40b132
	    if (rv != SECSuccess) {
Packit 40b132
		PR_fprintf(PR_STDERR, "Failed to Init DB: %s\n", 
Packit 40b132
					SECU_Strerror(PORT_GetError()));
Packit 40b132
		ret = CHANGEPW_FAILED_ERR;
Packit 40b132
	    }
Packit 40b132
	    if (*userPassword && !PK11_IsLoggedIn(slot, &passwordSuccess)) {
Packit 40b132
                PR_fprintf(PR_STDERR, "New DB did not log in after init\n");
Packit 40b132
		ret = AUTHENTICATION_FAILED_ERR;
Packit 40b132
	    }
Packit 40b132
	    /* generate a symetric key */
Packit 40b132
	    key = PK11_TokenKeyGen(slot, type, NULL, 0, &keyid,
Packit 40b132
				    PR_TRUE, &passwordSuccess);
Packit 40b132
Packit 40b132
	    if (!key) {
Packit 40b132
		PR_fprintf(PR_STDERR, "Could not generated symetric key: %s\n",
Packit 40b132
				SECU_Strerror(PORT_GetError()));
Packit 40b132
		exit (UNSPECIFIED_ERR);
Packit 40b132
	    }
Packit 40b132
	    PK11_FreeSymKey(key);
Packit 40b132
	    PK11_Logout(slot);
Packit 40b132
Packit 40b132
	    PK11_Authenticate(slot, PR_TRUE, &passwordSuccess);
Packit 40b132
Packit 40b132
	    if (*userPassword && !passwordSuccess) {
Packit 40b132
		PR_fprintf(PR_STDERR, "New DB Did not initalize\n");
Packit 40b132
		ret = AUTHENTICATION_FAILED_ERR;
Packit 40b132
	    }
Packit 40b132
	    key = PK11_FindFixedKey(slot, type, &keyid, &passwordSuccess);
Packit 40b132
Packit 40b132
	    if (!key) {
Packit 40b132
		PR_fprintf(PR_STDERR, "Could not find generated key: %s\n",
Packit 40b132
				SECU_Strerror(PORT_GetError()));
Packit 40b132
		ret = UNSPECIFIED_ERR;
Packit 40b132
	    } else {
Packit 40b132
		PK11_FreeSymKey(key);
Packit 40b132
	    }
Packit 40b132
 	    PK11_FreeSlot(slot);
Packit 40b132
	}
Packit 40b132
	     
Packit 40b132
        if (NSS_Shutdown() != SECSuccess) {
Packit 40b132
	    PR_fprintf(PR_STDERR, "Could not find generated key: %s\n",
Packit 40b132
				SECU_Strerror(PORT_GetError()));
Packit 40b132
            exit(1);
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    return ret;
Packit 40b132
}
Packit 40b132