Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1995 - 1996
  6. //
  7. // File: tstore4.cpp
  8. //
  9. // Contents: Test certificate store resync and notify change functions
  10. //
  11. // See Usage() for a list of test options.
  12. //
  13. //
  14. // Functions: main
  15. //
  16. // History: 28-Aug-97 philh created
  17. //--------------------------------------------------------------------------
  18. #include <windows.h>
  19. #include <assert.h>
  20. #include "wincrypt.h"
  21. #include "certtest.h"
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <memory.h>
  26. #include <time.h>
  27. static void Usage(void)
  28. {
  29. printf("Usage: tstore4 [options] <SystemStoreName>\n");
  30. printf("Options are:\n");
  31. printf(" -h - This message\n");
  32. printf(" -v - Verbose\n");
  33. printf(" -t<number> - Timeout (milliseconds, default INFINITE)\n");
  34. printf(" -f<number> - Flags\n");
  35. printf(" -i - Iterations (default to infinite)\n");
  36. printf(" -a - Auto Resync\n");
  37. printf(" -A - Auto Resync (user prompt to display)\n");
  38. printf(" -s - System store (ignored)\n");
  39. printf(" -RefreshCUGP - Refresh client CurrentUser GPT\n");
  40. printf(" -RefreshLMGP - Refresh client LocalMachine GPT\n");
  41. printf("\n");
  42. }
  43. typedef BOOL (WINAPI *PFN_REFRESH_POLICY)(
  44. IN BOOL bMachine
  45. );
  46. #define sz_USERENV_DLL "userenv.dll"
  47. #define sz_RefreshPolicy "RefreshPolicy"
  48. static void CallRefreshPolicy(
  49. IN BOOL fMachine
  50. )
  51. {
  52. HMODULE hDll = NULL;
  53. PFN_REFRESH_POLICY pfnRefreshPolicy;
  54. if (NULL == (hDll = LoadLibraryA(sz_USERENV_DLL))) {
  55. PrintLastError("LoadLibrary(userenv.dll)");
  56. goto ErrorReturn;
  57. }
  58. if (NULL == (pfnRefreshPolicy =
  59. (PFN_REFRESH_POLICY) GetProcAddress(
  60. hDll, sz_RefreshPolicy))) {
  61. PrintLastError("GetProcAddress(RefreshPolicy)");
  62. goto ErrorReturn;
  63. }
  64. if (!pfnRefreshPolicy(fMachine)) {
  65. PrintLastError("RefreshPolicy");
  66. goto ErrorReturn;
  67. }
  68. ErrorReturn:
  69. if (hDll)
  70. FreeLibrary(hDll);
  71. return;
  72. }
  73. int _cdecl main(int argc, char * argv[])
  74. {
  75. BOOL fResult;
  76. int status = 0;
  77. DWORD i;
  78. DWORD dwError;
  79. DWORD dwDisplayFlags = DISPLAY_BRIEF_FLAG;
  80. HCERTSTORE hStoreToClose = NULL;
  81. HCERTSTORE hStore = NULL;
  82. DWORD dwFlags = CERT_STORE_READONLY_FLAG;
  83. DWORD dwMilliseconds = INFINITE;
  84. BOOL fAutoResync = FALSE;
  85. BOOL fUserPrompt = FALSE;
  86. DWORD dwIterations = 0;
  87. BOOL fRefreshPolicy = FALSE;
  88. BOOL fMachine = FALSE;
  89. HANDLE hEvent = NULL;
  90. HANDLE hEvent2 = NULL;
  91. HANDLE hEventToClose = NULL;
  92. LPSTR pszSystemName = NULL; // not allocated
  93. SYSTEMTIME SystemTime;
  94. FILETIME FileTime;
  95. while (--argc>0) {
  96. if (**++argv == '-')
  97. {
  98. if (0 == _stricmp(argv[0]+1, "RefreshCUGP")) {
  99. fRefreshPolicy = TRUE;
  100. fMachine = FALSE;
  101. } else if (0 == _stricmp(argv[0]+1, "RefreshLMGP")) {
  102. fRefreshPolicy = TRUE;
  103. fMachine = TRUE;
  104. } else {
  105. switch(argv[0][1])
  106. {
  107. case 'v':
  108. dwDisplayFlags = DISPLAY_VERBOSE_FLAG;
  109. break;
  110. case 'i':
  111. dwIterations = strtoul(argv[0]+2, NULL, 0);
  112. break;
  113. case 'f':
  114. dwFlags = strtoul(argv[0]+2, NULL, 0);
  115. break;
  116. case 't':
  117. dwMilliseconds = strtoul(argv[0]+2, NULL, 0);
  118. break;
  119. case 'A':
  120. fUserPrompt = TRUE;
  121. case 'a':
  122. fAutoResync = TRUE;
  123. break;
  124. case 's':
  125. break;
  126. case 'h':
  127. default:
  128. goto BadUsage;
  129. }
  130. }
  131. } else {
  132. if (pszSystemName) {
  133. printf("Too many names starting with:: %s\n", argv[0]);
  134. goto BadUsage;
  135. }
  136. pszSystemName = argv[0];
  137. }
  138. }
  139. printf("command line: %s\n", GetCommandLine());
  140. if (fRefreshPolicy) {
  141. CallRefreshPolicy(fMachine);
  142. goto SuccessReturn;
  143. }
  144. if (NULL == pszSystemName) {
  145. printf("Missing <SystemStoreName>\n");
  146. goto BadUsage;
  147. }
  148. hStore = OpenStoreEx(TRUE, pszSystemName, dwFlags);
  149. if (NULL == hStore) {
  150. PrintLastError("CertOpenStore");
  151. goto ErrorReturn;
  152. }
  153. // Create event to be notified
  154. if (NULL == (hEvent = CreateEvent(
  155. NULL, // lpsa
  156. FALSE, // fManualReset
  157. FALSE, // fInitialState
  158. NULL))) { // lpszEventName
  159. PrintLastError("CreateEvent");
  160. goto ErrorReturn;
  161. }
  162. // Create second event to be notified
  163. if (NULL == (hEvent2 = CreateEvent(
  164. NULL, // lpsa
  165. FALSE, // fManualReset
  166. FALSE, // fInitialState
  167. NULL))) { // lpszEventName
  168. PrintLastError("CreateEvent");
  169. goto ErrorReturn;
  170. }
  171. if (!fUserPrompt) {
  172. hStoreToClose = OpenStoreEx(TRUE, pszSystemName, dwFlags);
  173. if (NULL == hStoreToClose) {
  174. PrintLastError("CertOpenStore(StoreToClose)");
  175. goto ErrorReturn;
  176. }
  177. if (NULL == (hEventToClose = CreateEvent(
  178. NULL, // lpsa
  179. FALSE, // fManualReset
  180. FALSE, // fInitialState
  181. NULL))) { // lpszEventName
  182. PrintLastError("CreateEvent");
  183. goto ErrorReturn;
  184. }
  185. // Register the event to be signaled when the store changes
  186. if (!CertControlStore(
  187. hStoreToClose,
  188. 0, // dwFlags
  189. CERT_STORE_CTRL_NOTIFY_CHANGE,
  190. &hEventToClose
  191. )) {
  192. PrintLastError("CertControlStore(NOTIFY_CHANGE)");
  193. goto ErrorReturn;
  194. }
  195. // Register second event to be signaled when the store changes
  196. if (!CertControlStore(
  197. hStoreToClose,
  198. 0, // dwFlags
  199. CERT_STORE_CTRL_NOTIFY_CHANGE,
  200. &hEventToClose
  201. )) {
  202. PrintLastError("CertControlStore(NOTIFY_CHANGE)");
  203. goto ErrorReturn;
  204. }
  205. // Register the event to be signaled when the store changes
  206. if (!CertControlStore(
  207. hStore,
  208. 0, // dwFlags
  209. CERT_STORE_CTRL_NOTIFY_CHANGE,
  210. &hEvent
  211. )) {
  212. PrintLastError("CertControlStore(NOTIFY_CHANGE)");
  213. goto ErrorReturn;
  214. }
  215. // Register second event to be signaled when the store changes
  216. if (!CertControlStore(
  217. hStore,
  218. 0, // dwFlags
  219. CERT_STORE_CTRL_NOTIFY_CHANGE,
  220. &hEvent2
  221. )) {
  222. PrintLastError("CertControlStore(NOTIFY_CHANGE)");
  223. goto ErrorReturn;
  224. }
  225. CertCloseStore(hStoreToClose, 0);
  226. hStoreToClose = NULL;
  227. }
  228. if (fAutoResync) {
  229. printf("Auto Resync is enabled\n");
  230. if (!CertControlStore(
  231. hStore,
  232. 0, // dwFlags
  233. CERT_STORE_CTRL_AUTO_RESYNC,
  234. NULL // pvCtrlPara
  235. )) {
  236. PrintLastError("CertControlStore(AUTO_RESYNC)");
  237. goto ErrorReturn;
  238. }
  239. }
  240. // Loop and wait for store changes
  241. i = 0;
  242. while (TRUE) {
  243. if (fUserPrompt) {
  244. int c;
  245. fputs("Waiting to sync (q)uit ->", stdout);
  246. fflush(stdin);
  247. fflush(stdout);
  248. c = getchar();
  249. if ('q' == c)
  250. break;
  251. } else {
  252. DWORD dwWait;
  253. dwWait = WaitForSingleObjectEx(
  254. hEvent,
  255. dwMilliseconds,
  256. FALSE // bAlertable
  257. );
  258. if (!(WAIT_OBJECT_0 == dwWait || WAIT_TIMEOUT == dwWait)) {
  259. PrintLastError("WaitForSingleObjectEx");
  260. goto ErrorReturn;
  261. }
  262. }
  263. i++;
  264. GetSystemTime(&SystemTime);
  265. SystemTimeToFileTime(&SystemTime, &FileTime);
  266. printf("\n");
  267. if (fAutoResync) {
  268. printf(">>>>> Auto Resync[%d] at: %s >>>>>\n",
  269. i, FileTimeText(&FileTime));
  270. if (!fUserPrompt) {
  271. if (!CertControlStore(
  272. hStore,
  273. 0, // dwFlags
  274. CERT_STORE_CTRL_RESYNC,
  275. &hEvent
  276. )) {
  277. PrintLastError("CertControlStore(RESYNC, for AutoResync)");
  278. goto ErrorReturn;
  279. }
  280. }
  281. DisplayStore(hStore, dwDisplayFlags);
  282. } else {
  283. printf(">>>>> Before Resync[%d] >>>>>\n", i);
  284. DisplayStore(hStore, dwDisplayFlags);
  285. if (!CertControlStore(
  286. hStore,
  287. 0, // dwFlags
  288. CERT_STORE_CTRL_RESYNC,
  289. fUserPrompt ? NULL : &hEvent
  290. )) {
  291. PrintLastError("CertControlStore(RESYNC)");
  292. goto ErrorReturn;
  293. }
  294. printf("\n");
  295. printf(">>>>> After Resync[%d] at: %s >>>>>\n",
  296. i, FileTimeText(&FileTime));
  297. DisplayStore(hStore, dwDisplayFlags);
  298. }
  299. if (!fUserPrompt) {
  300. // Check that the second event is still signalled
  301. DWORD dwWait;
  302. dwWait = WaitForSingleObjectEx(
  303. hEvent2,
  304. 0,
  305. FALSE // bAlertable
  306. );
  307. if (WAIT_OBJECT_0 != dwWait) {
  308. printf("@@@@ second event NOT signaled 0x%x @@@@\n", dwWait);
  309. } else {
  310. printf("second event signalled\n");
  311. if (!CertControlStore(
  312. hStore,
  313. 0, // dwFlags
  314. CERT_STORE_CTRL_RESYNC,
  315. &hEvent2
  316. )) {
  317. PrintLastError(
  318. "CertControlStore(RESYNC, for second event)");
  319. goto ErrorReturn;
  320. }
  321. }
  322. }
  323. if (0 != dwIterations && i == dwIterations)
  324. break;
  325. }
  326. SuccessReturn:
  327. status = 0;
  328. CommonReturn:
  329. if (hEvent)
  330. CloseHandle(hEvent);
  331. if (hEvent2)
  332. CloseHandle(hEvent2);
  333. if (hEventToClose)
  334. CloseHandle(hEventToClose);
  335. if (hStoreToClose)
  336. CertCloseStore(hStoreToClose, 0);
  337. if (hStore)
  338. CertCloseStore(hStore, 0);
  339. return status;
  340. BadUsage:
  341. Usage();
  342. status = -1;
  343. goto CommonReturn;
  344. ErrorReturn:
  345. status = -1;
  346. goto CommonReturn;
  347. }