|
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 main(int argc, char **argv) |
|
41 { |
|
42 char * certDir = NULL; |
|
43 PLOptState *optstate; |
|
44 PLOptStatus optstatus; |
|
45 SECStatus rv; |
|
46 char * tokenName = NULL; |
|
47 PRBool cont=PR_TRUE; |
|
48 PK11TokenEvent event = PK11TokenPresentEvent; |
|
49 PK11TokenStatus status; |
|
50 char *progName; |
|
51 PK11SlotInfo *slot; |
|
52 |
|
53 progName = strrchr(argv[0], '/'); |
|
54 if (!progName) |
|
55 progName = strrchr(argv[0], '\\'); |
|
56 progName = progName ? progName+1 : argv[0]; |
|
57 |
|
58 optstate = PL_CreateOptState(argc, argv, "rd:t:"); |
|
59 while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) { |
|
60 switch (optstate->option) { |
|
61 |
|
62 case 'd': |
|
63 certDir = strdup(optstate->value); |
|
64 certDir = SECU_ConfigDirectory(certDir); |
|
65 break; |
|
66 case 't': |
|
67 tokenName = strdup(optstate->value); |
|
68 break; |
|
69 case 'r': |
|
70 event = PK11TokenRemovedOrChangedEvent; |
|
71 break; |
|
72 } |
|
73 } |
|
74 if (optstatus == PL_OPT_BAD) |
|
75 Usage(progName); |
|
76 |
|
77 if (tokenName == NULL) { |
|
78 Usage(progName); |
|
79 } |
|
80 |
|
81 if (!certDir) { |
|
82 certDir = SECU_DefaultSSLDir(); /* Look in $SSL_DIR */ |
|
83 certDir = SECU_ConfigDirectory(certDir); /* call even if it's NULL */ |
|
84 } |
|
85 |
|
86 PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1); |
|
87 |
|
88 PK11_SetPasswordFunc(SECU_GetModulePassword); |
|
89 |
|
90 /* open the cert DB, the key DB, and the secmod DB. */ |
|
91 rv = NSS_Init(certDir); |
|
92 if (rv != SECSuccess) { |
|
93 SECU_PrintError(progName, "unable to open cert database"); |
|
94 return 1; |
|
95 } |
|
96 |
|
97 printf("Looking up tokenNamed: <%s>\n",tokenName); |
|
98 slot = PK11_FindSlotByName(tokenName); |
|
99 if (slot == NULL) { |
|
100 SECU_PrintError(progName, "unable to find token"); |
|
101 return 1; |
|
102 } |
|
103 |
|
104 do { |
|
105 status = |
|
106 PK11_WaitForTokenEvent(slot,event,PR_INTERVAL_NO_TIMEOUT, 0, 0); |
|
107 |
|
108 switch (status) { |
|
109 case PK11TokenNotRemovable: |
|
110 cont = PR_FALSE; |
|
111 printf("%s Token Not Removable\n",tokenName); |
|
112 break; |
|
113 case PK11TokenChanged: |
|
114 event = PK11TokenRemovedOrChangedEvent; |
|
115 printf("%s Token Changed\n", tokenName); |
|
116 break; |
|
117 case PK11TokenRemoved: |
|
118 event = PK11TokenPresentEvent; |
|
119 printf("%s Token Removed\n", tokenName); |
|
120 break; |
|
121 case PK11TokenPresent: |
|
122 event = PK11TokenRemovedOrChangedEvent; |
|
123 printf("%s Token Present\n", tokenName); |
|
124 break; |
|
125 } |
|
126 } while (cont); |
|
127 |
|
128 PK11_FreeSlot(slot); |
|
129 |
|
130 if (NSS_Shutdown() != SECSuccess) { |
|
131 exit(1); |
|
132 } |
|
133 PR_Cleanup(); |
|
134 return 0; |
|
135 } |