remtest.c (3505B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /* 6 ** 7 ** Sample client side test program that uses SSL and NSS 8 ** 9 */ 10 11 #include "secutil.h" 12 13 #if defined(XP_UNIX) 14 #include <unistd.h> 15 #else 16 #include "ctype.h" /* for isalpha() */ 17 #endif 18 19 #include <stdio.h> 20 #include <string.h> 21 #include <stdlib.h> 22 #include <errno.h> 23 #include <fcntl.h> 24 #include <stdarg.h> 25 26 #include "nspr.h" 27 #include "prio.h" 28 #include "prnetdb.h" 29 #include "nss.h" 30 #include "pk11func.h" 31 #include "plgetopt.h" 32 33 void 34 Usage(char *progName) 35 { 36 fprintf(stderr, "usage: %s [-d profiledir] -t tokenName [-r]\n", progName); 37 exit(1); 38 } 39 40 int 41 main(int argc, char **argv) 42 { 43 char *certDir = NULL; 44 PLOptState *optstate; 45 PLOptStatus optstatus; 46 SECStatus rv; 47 char *tokenName = NULL; 48 PRBool cont = PR_TRUE; 49 PK11TokenEvent event = PK11TokenPresentEvent; 50 PK11TokenStatus status; 51 char *progName; 52 PK11SlotInfo *slot; 53 54 progName = strrchr(argv[0], '/'); 55 if (!progName) 56 progName = strrchr(argv[0], '\\'); 57 progName = progName ? progName + 1 : argv[0]; 58 59 optstate = PL_CreateOptState(argc, argv, "rd:t:"); 60 while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) { 61 switch (optstate->option) { 62 63 case 'd': 64 certDir = strdup(optstate->value); 65 certDir = SECU_ConfigDirectory(certDir); 66 break; 67 case 't': 68 tokenName = strdup(optstate->value); 69 break; 70 case 'r': 71 event = PK11TokenRemovedOrChangedEvent; 72 break; 73 } 74 } 75 if (optstatus == PL_OPT_BAD) 76 Usage(progName); 77 78 if (tokenName == NULL) { 79 Usage(progName); 80 } 81 82 if (!certDir) { 83 certDir = SECU_DefaultSSLDir(); /* Look in $SSL_DIR */ 84 certDir = SECU_ConfigDirectory(certDir); /* call even if it's NULL */ 85 } 86 87 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1); 88 89 PK11_SetPasswordFunc(SECU_GetModulePassword); 90 91 /* open the cert DB, the key DB, and the secmod DB. */ 92 rv = NSS_Init(certDir); 93 if (rv != SECSuccess) { 94 SECU_PrintError(progName, "unable to open cert database"); 95 return 1; 96 } 97 98 printf("Looking up tokenNamed: <%s>\n", tokenName); 99 slot = PK11_FindSlotByName(tokenName); 100 if (slot == NULL) { 101 SECU_PrintError(progName, "unable to find token"); 102 return 1; 103 } 104 105 do { 106 status = 107 PK11_WaitForTokenEvent(slot, event, PR_INTERVAL_NO_TIMEOUT, 0, 0); 108 109 switch (status) { 110 case PK11TokenNotRemovable: 111 cont = PR_FALSE; 112 printf("%s Token Not Removable\n", tokenName); 113 break; 114 case PK11TokenChanged: 115 event = PK11TokenRemovedOrChangedEvent; 116 printf("%s Token Changed\n", tokenName); 117 break; 118 case PK11TokenRemoved: 119 event = PK11TokenPresentEvent; 120 printf("%s Token Removed\n", tokenName); 121 break; 122 case PK11TokenPresent: 123 event = PK11TokenRemovedOrChangedEvent; 124 printf("%s Token Present\n", tokenName); 125 break; 126 } 127 } while (cont); 128 129 PK11_FreeSlot(slot); 130 131 if (NSS_Shutdown() != SECSuccess) { 132 exit(1); 133 } 134 PR_Cleanup(); 135 return 0; 136 }