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.

283 lines
7.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: makecat.cpp
  8. //
  9. // Contents: Microsoft Internet Security Catalog Utilities
  10. //
  11. // Functions: main
  12. //
  13. // History: 05-May-1999 reidk created
  14. //
  15. //--------------------------------------------------------------------------
  16. #include <stdio.h>
  17. #include <windows.h>
  18. #include <io.h>
  19. #include <wchar.h>
  20. #include <malloc.h>
  21. #include <memory.h>
  22. #include "unicode.h"
  23. #include "wincrypt.h"
  24. #include "wintrust.h"
  25. #include "softpub.h"
  26. #include "mssip.h"
  27. #include "mscat.h"
  28. #include "dbgdef.h"
  29. #include "gendefs.h"
  30. #include "printfu.hxx"
  31. #include "cwargv.hxx"
  32. #include "resource.h"
  33. static void Usage(void)
  34. {
  35. printf("Usage: chckhash [options] filename\n");
  36. printf("Options are:\n");
  37. printf(" -? - This message\n");
  38. printf(" -catdb <param> - The catroot to search (default is the system DB)\n");
  39. printf(" -r [0|1] - Called from regress, 0 implies not found is expected, 1 implies found is expected\n");
  40. printf(" -l - Filename is a list of hyphen seperated files\n");
  41. printf(" -p - Expect 'paused' failure\n");
  42. printf("\n");
  43. }
  44. int __cdecl main(int argc, char * argv[])
  45. {
  46. int cMember;
  47. BYTE pbHash[40];
  48. DWORD cbHash = sizeof(pbHash);
  49. HANDLE hFile;
  50. HCATINFO hCatInfo;
  51. BOOL fFileFound = FALSE;
  52. CATALOG_INFO sCatInfo;
  53. LPSTR pszGUID = NULL;
  54. LPWSTR pwszGUID = NULL;
  55. BOOL fCalledFromRegress = FALSE;
  56. BOOL fFoundExpected = FALSE;
  57. BOOL fFileList = FALSE;
  58. BOOL fExpectPaused = FALSE;
  59. char *pszFile = NULL;
  60. int iRet = 1;
  61. GUID guidPassedIn = DRIVER_ACTION_VERIFY;
  62. GUID *pguidCatRoot = NULL;
  63. HCATADMIN hCatAdmin = NULL;
  64. char *pChar = NULL;
  65. while (--argc>0)
  66. {
  67. if (**++argv == '-')
  68. {
  69. switch(argv[0][1])
  70. {
  71. case 'c':
  72. case 'C':
  73. argv++;
  74. argc--;
  75. pszGUID = argv[0];
  76. break;
  77. case 'r':
  78. case 'R':
  79. argv++;
  80. argc--;
  81. fCalledFromRegress = TRUE;
  82. fFoundExpected = (argv[0][0] == '1');
  83. break;
  84. case 'l':
  85. case 'L':
  86. fFileList = TRUE;
  87. break;
  88. case 'p':
  89. case 'P':
  90. fExpectPaused = TRUE;
  91. break;
  92. case '?':
  93. default:
  94. Usage();
  95. return 1;
  96. }
  97. }
  98. else
  99. {
  100. pszFile = argv[0];
  101. }
  102. }
  103. SetLastError(0);
  104. //
  105. // get provider
  106. //
  107. if (pszGUID != NULL)
  108. {
  109. if (NULL == (pwszGUID = MkWStr(pszGUID)))
  110. {
  111. goto ErrorReturn;
  112. }
  113. if (!(wstr2guid(pwszGUID, &guidPassedIn)))
  114. {
  115. FreeWStr(pwszGUID);
  116. goto ErrorReturn;
  117. }
  118. FreeWStr(pwszGUID);
  119. }
  120. pguidCatRoot = &guidPassedIn;
  121. if (!(CryptCATAdminAcquireContext(&hCatAdmin, pguidCatRoot, 0)))
  122. {
  123. printf("CryptCATAdminAcquireContext failure\nGLE = %lx\n", GetLastError());
  124. goto ErrorReturn;
  125. }
  126. while (pszFile != NULL)
  127. {
  128. if (fFileList)
  129. {
  130. pChar = strchr(pszFile, '-');
  131. if (pChar != NULL)
  132. {
  133. *pChar = '\0';
  134. }
  135. }
  136. //
  137. // Open the file who's hash is being looked up, then calculate its hash
  138. //
  139. if ((hFile = CreateFileA(pszFile,
  140. GENERIC_READ,
  141. FILE_SHARE_READ,
  142. NULL,
  143. OPEN_EXISTING,
  144. FILE_ATTRIBUTE_NORMAL,
  145. NULL)) == INVALID_HANDLE_VALUE)
  146. {
  147. printf("Cannot open file\nGLE = %lx\n", GetLastError());
  148. goto CATCloseError;
  149. }
  150. if (!CryptCATAdminCalcHashFromFileHandle(hFile,
  151. &cbHash,
  152. pbHash,
  153. 0))
  154. {
  155. printf("Cannot calculate file hash\nGLE = %lx\n", GetLastError());
  156. goto CATCloseError;
  157. }
  158. hCatInfo = NULL;
  159. while (hCatInfo = CryptCATAdminEnumCatalogFromHash(hCatAdmin, pbHash, cbHash, 0, &hCatInfo))
  160. {
  161. fFileFound = TRUE;
  162. memset(&sCatInfo, 0x00, sizeof(CATALOG_INFO));
  163. sCatInfo.cbStruct = sizeof(CATALOG_INFO);
  164. if (!(CryptCATCatalogInfoFromContext(hCatInfo, &sCatInfo, 0)))
  165. {
  166. // should do something (??)
  167. continue;
  168. }
  169. if (!fCalledFromRegress)
  170. {
  171. printf("%S contains %s\n", &sCatInfo.wszCatalogFile[0], pszFile);
  172. }
  173. }
  174. if (fCalledFromRegress)
  175. {
  176. if (fFileFound)
  177. {
  178. if (fFoundExpected)
  179. {
  180. printf("Succeeded\n");
  181. iRet = 1;
  182. }
  183. else
  184. {
  185. printf("Failed: %s should NOT have been found\n", pszFile);
  186. iRet = 0;
  187. }
  188. }
  189. else
  190. {
  191. if (fFoundExpected)
  192. {
  193. printf("Failed: %s was not found: GLE - %lx\n", pszFile, GetLastError());
  194. iRet = 0;
  195. }
  196. else if ((GetLastError() == ERROR_SHARING_PAUSED) && (fExpectPaused))
  197. {
  198. printf("Succeeded\n");
  199. iRet = 1;
  200. }
  201. else if (GetLastError() == ERROR_NOT_FOUND)
  202. {
  203. printf("Succeeded\n");
  204. iRet = 1;
  205. }
  206. else
  207. {
  208. if (fExpectPaused)
  209. {
  210. printf("Failed: ERROR_SHARING_PAUSED expected, but got %lx\n", GetLastError());
  211. }
  212. else
  213. {
  214. printf("Failed: ERROR_NOT_FOUND expected, but got %lx\n", GetLastError());
  215. }
  216. }
  217. }
  218. }
  219. else if (!fFileFound)
  220. {
  221. printf("There are no catalog files registered that contain %s: GLE - %lx\n", pszFile, GetLastError());
  222. }
  223. if (fFileList)
  224. {
  225. if (pChar != NULL)
  226. {
  227. pszFile = ((LPSTR) pChar) + 1;
  228. }
  229. else
  230. {
  231. pszFile = NULL;
  232. }
  233. }
  234. else
  235. {
  236. pszFile = NULL;
  237. }
  238. }
  239. CommonReturn:
  240. if (hCatAdmin)
  241. {
  242. CryptCATAdminReleaseContext(hCatAdmin, 0);
  243. }
  244. return(iRet);
  245. ErrorReturn:
  246. iRet = 0;
  247. goto CommonReturn;
  248. TRACE_ERROR_EX(DBG_SS_APP, CATCloseError);
  249. }