security/nss/cmd/tests/remtest.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/tests/remtest.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     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 +/*
     1.9 +**
    1.10 +** Sample client side test program that uses SSL and NSS
    1.11 +**
    1.12 +*/
    1.13 +
    1.14 +#include "secutil.h"
    1.15 +
    1.16 +#if defined(XP_UNIX)
    1.17 +#include <unistd.h>
    1.18 +#else
    1.19 +#include "ctype.h"	/* for isalpha() */
    1.20 +#endif
    1.21 +
    1.22 +#include <stdio.h>
    1.23 +#include <string.h>
    1.24 +#include <stdlib.h>
    1.25 +#include <errno.h>
    1.26 +#include <fcntl.h>
    1.27 +#include <stdarg.h>
    1.28 +
    1.29 +#include "nspr.h"
    1.30 +#include "prio.h"
    1.31 +#include "prnetdb.h"
    1.32 +#include "nss.h"
    1.33 +#include "pk11func.h"
    1.34 +#include "plgetopt.h"
    1.35 +
    1.36 +void
    1.37 +Usage(char *progName) 
    1.38 +{
    1.39 +    fprintf(stderr,"usage: %s [-d profiledir] -t tokenName [-r]\n", progName);
    1.40 +    exit(1);
    1.41 +}
    1.42 +
    1.43 +int main(int argc, char **argv)
    1.44 +{
    1.45 +    char *             certDir  =  NULL;
    1.46 +    PLOptState *optstate;
    1.47 +    PLOptStatus optstatus;
    1.48 +    SECStatus rv;
    1.49 +    char * tokenName = NULL;
    1.50 +    PRBool cont=PR_TRUE;
    1.51 +    PK11TokenEvent event = PK11TokenPresentEvent;
    1.52 +    PK11TokenStatus status;
    1.53 +    char *progName;
    1.54 +    PK11SlotInfo *slot;
    1.55 +
    1.56 +    progName = strrchr(argv[0], '/');
    1.57 +    if (!progName)
    1.58 +	progName = strrchr(argv[0], '\\');
    1.59 +    progName = progName ? progName+1 : argv[0];
    1.60 +
    1.61 +    optstate = PL_CreateOptState(argc, argv, "rd:t:");
    1.62 +    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
    1.63 +	switch (optstate->option) {
    1.64 +
    1.65 +	  case 'd':
    1.66 +	    certDir = strdup(optstate->value);
    1.67 +	    certDir = SECU_ConfigDirectory(certDir);
    1.68 +	    break;
    1.69 +	  case 't':
    1.70 +	    tokenName = strdup(optstate->value);
    1.71 +	    break;
    1.72 +	  case 'r':
    1.73 +	    event = PK11TokenRemovedOrChangedEvent;
    1.74 +	    break;
    1.75 +	}
    1.76 +    }
    1.77 +    if (optstatus == PL_OPT_BAD)
    1.78 +	Usage(progName);
    1.79 +
    1.80 +    if (tokenName == NULL) {
    1.81 +	Usage(progName);
    1.82 +    }
    1.83 +
    1.84 +    if (!certDir) {
    1.85 +	certDir = SECU_DefaultSSLDir();	/* Look in $SSL_DIR */
    1.86 +	certDir = SECU_ConfigDirectory(certDir); /* call even if it's NULL */
    1.87 +    }
    1.88 +
    1.89 +    PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
    1.90 +
    1.91 +    PK11_SetPasswordFunc(SECU_GetModulePassword);
    1.92 +
    1.93 +    /* open the cert DB, the key DB, and the secmod DB. */
    1.94 +    rv = NSS_Init(certDir);
    1.95 +    if (rv != SECSuccess) {
    1.96 +	SECU_PrintError(progName, "unable to open cert database");
    1.97 +	return 1;
    1.98 +    }
    1.99 +
   1.100 +    printf("Looking up tokenNamed: <%s>\n",tokenName);
   1.101 +    slot = PK11_FindSlotByName(tokenName);
   1.102 +    if (slot == NULL) {
   1.103 +	SECU_PrintError(progName, "unable to find token");
   1.104 +	return 1;
   1.105 +    }
   1.106 +
   1.107 +    do {
   1.108 +	status = 
   1.109 +	   PK11_WaitForTokenEvent(slot,event,PR_INTERVAL_NO_TIMEOUT, 0, 0);
   1.110 +
   1.111 +	switch (status) {
   1.112 +	case PK11TokenNotRemovable:
   1.113 +	    cont = PR_FALSE;
   1.114 +	    printf("%s Token Not Removable\n",tokenName);
   1.115 +	    break;
   1.116 +	case PK11TokenChanged:
   1.117 +	    event = PK11TokenRemovedOrChangedEvent;
   1.118 +	    printf("%s Token Changed\n", tokenName);
   1.119 +	    break;
   1.120 +	case PK11TokenRemoved:
   1.121 +	    event = PK11TokenPresentEvent;
   1.122 +	    printf("%s Token Removed\n", tokenName);
   1.123 +	    break;
   1.124 +	case PK11TokenPresent:
   1.125 +	    event = PK11TokenRemovedOrChangedEvent;
   1.126 +	    printf("%s Token Present\n", tokenName);
   1.127 +	    break;
   1.128 +	}
   1.129 +    } while (cont);
   1.130 +
   1.131 +    PK11_FreeSlot(slot);
   1.132 +
   1.133 +    if (NSS_Shutdown() != SECSuccess) {
   1.134 +        exit(1);
   1.135 +    }
   1.136 +    PR_Cleanup();
   1.137 +    return 0;
   1.138 +}

mercurial