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