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.

413 lines
11 KiB

  1. #define INITGUID 1
  2. #include <nt.h>
  3. #include <ntdef.h>
  4. #include <ntrtl.h>
  5. #include <nturtl.h>
  6. #include <windows.h>
  7. #include <partmgrp.h>
  8. #include <stdio.h>
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define PROGRAM_TITLE L"SigCheck - Signature Check Program V0.1"
  13. #define HandleInvalid(_Handle) ((NULL == (_Handle)) || (INVALID_HANDLE_VALUE == (_Handle)))
  14. #define GET_STATUS_FROM_BOOL(_bSucceeded) ((_bSucceeded) ? NOERROR : HRESULT_FROM_WIN32 (GetLastError()))
  15. #define GET_STATUS_FROM_HANDLE(_handle) ((!HandleInvalid(_handle)) ? NOERROR : HRESULT_FROM_WIN32 (GetLastError()))
  16. #define GET_STATUS_FROM_POINTER(_ptr) ((NULL != (_ptr)) ? NOERROR : E_OUTOFMEMORY)
  17. void PrintResults (PPARTMGR_SIGNATURE_CHECK_DISKS pSignatureCheckBufferDisks);
  18. PSTR PartitionName = "\\Device\\Harddisk%d\\Partition%d";
  19. PSTR DeviceName = "Harddisk0";
  20. VOID PrintError (IN DWORD ErrorCode)
  21. {
  22. LPVOID lpMsgBuf;
  23. ULONG count;
  24. count = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
  25. FORMAT_MESSAGE_FROM_SYSTEM |
  26. FORMAT_MESSAGE_IGNORE_INSERTS,
  27. NULL,
  28. ErrorCode,
  29. 0,
  30. (LPTSTR) &lpMsgBuf,
  31. 0,
  32. NULL);
  33. if (count != 0)
  34. {
  35. printf (" (%d) %s\n", ErrorCode, (LPCTSTR) lpMsgBuf);
  36. LocalFree (lpMsgBuf);
  37. }
  38. else
  39. {
  40. printf ("Format message failed. Error: %d\n", GetLastError());
  41. }
  42. } // PrintError
  43. extern "C" __cdecl wmain (int argc, WCHAR *argv [])
  44. {
  45. NTSTATUS ntStatus;
  46. HANDLE fileHandle;
  47. DWORD accessMode;
  48. DWORD shareMode;
  49. DWORD errorCode;
  50. BOOL failed = FALSE;
  51. CHAR deviceNameString [128];
  52. DWORD bytesReturned;
  53. ULONG bufferLength;
  54. ANSI_STRING objName;
  55. UNICODE_STRING unicodeName;
  56. OBJECT_ATTRIBUTES objAttributes;
  57. IO_STATUS_BLOCK ioStatusBlock;
  58. PARTMGR_SIGNATURE_CHECK_EPOCH SignatureCheckBufferEpoch;
  59. PPARTMGR_SIGNATURE_CHECK_DISKS pSignatureCheckBufferDisks;
  60. UNREFERENCED_PARAMETER (argc);
  61. UNREFERENCED_PARAMETER (argv);
  62. //
  63. // Note it is important to access the device with 0 access mode so that
  64. // the file open code won't do extra I/O to the device
  65. //
  66. shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
  67. accessMode = GENERIC_READ | GENERIC_WRITE;
  68. strcpy (deviceNameString, "\\\\.\\");
  69. strcat (deviceNameString, DeviceName);
  70. fileHandle = CreateFileA (deviceNameString,
  71. accessMode,
  72. shareMode,
  73. NULL,
  74. OPEN_EXISTING,
  75. 0,
  76. NULL);
  77. if (fileHandle == INVALID_HANDLE_VALUE)
  78. {
  79. errorCode = GetLastError ();
  80. if ((errorCode == ERROR_PATH_NOT_FOUND) ||
  81. (errorCode == ERROR_FILE_NOT_FOUND))
  82. {
  83. strcpy (deviceNameString, "\\Device\\");
  84. strcat (deviceNameString, DeviceName);
  85. RtlInitString (&objName, deviceNameString);
  86. ntStatus = RtlAnsiStringToUnicodeString (&unicodeName,
  87. &objName,
  88. TRUE);
  89. if (!NT_SUCCESS(ntStatus))
  90. {
  91. printf ("Error converting device name %s to unicode. Error: %lx\n",
  92. deviceNameString,
  93. ntStatus);
  94. return -1;
  95. }
  96. InitializeObjectAttributes (&objAttributes,
  97. &unicodeName,
  98. OBJ_CASE_INSENSITIVE,
  99. NULL,
  100. NULL);
  101. ntStatus = NtCreateFile (&fileHandle,
  102. SYNCHRONIZE | FILE_READ_DATA | FILE_WRITE_DATA,
  103. &objAttributes,
  104. &ioStatusBlock,
  105. NULL,
  106. FILE_ATTRIBUTE_NORMAL,
  107. FILE_SHARE_READ | FILE_SHARE_WRITE,
  108. FILE_OPEN,
  109. 0,
  110. NULL,
  111. 0);
  112. if (!NT_SUCCESS (ntStatus))
  113. {
  114. failed = TRUE;
  115. }
  116. RtlFreeUnicodeString (&unicodeName);
  117. }
  118. else
  119. {
  120. printf ("Error opening %s. Error: %d\n",
  121. deviceNameString,
  122. errorCode = GetLastError());
  123. PrintError (errorCode);
  124. return -1;
  125. }
  126. }
  127. if (failed)
  128. {
  129. strcpy (deviceNameString, "\\Device\\");
  130. strcat (deviceNameString, DeviceName);
  131. strcat(deviceNameString, "\\Partition0");
  132. RtlInitString (&objName, deviceNameString);
  133. ntStatus = RtlAnsiStringToUnicodeString (&unicodeName,
  134. &objName,
  135. TRUE);
  136. if (!NT_SUCCESS(ntStatus))
  137. {
  138. printf("Error converting device name %s to unicode. Error: %lx\n",
  139. deviceNameString, ntStatus);
  140. return -1;
  141. }
  142. InitializeObjectAttributes (&objAttributes,
  143. &unicodeName,
  144. OBJ_CASE_INSENSITIVE,
  145. NULL,
  146. NULL);
  147. ntStatus = NtCreateFile (&fileHandle,
  148. SYNCHRONIZE | FILE_READ_DATA | FILE_WRITE_DATA,
  149. &objAttributes,
  150. &ioStatusBlock,
  151. NULL,
  152. FILE_ATTRIBUTE_NORMAL,
  153. FILE_SHARE_READ | FILE_SHARE_WRITE,
  154. FILE_OPEN,
  155. 0,
  156. NULL,
  157. 0);
  158. if (!NT_SUCCESS (ntStatus))
  159. {
  160. printf("Error opening device %ws. Error: %lx.\n",
  161. unicodeName.Buffer, ntStatus );
  162. return -1;
  163. }
  164. RtlFreeUnicodeString (&unicodeName);
  165. }
  166. //printf("Accessing %s ... \n", deviceNameString);
  167. bufferLength = sizeof (PARTMGR_SIGNATURE_CHECK_DISKS) + sizeof (ULONG) * 10;
  168. pSignatureCheckBufferDisks = (PPARTMGR_SIGNATURE_CHECK_DISKS) malloc (bufferLength);
  169. SignatureCheckBufferEpoch.RequestEpoch = PARTMGR_REQUEST_CURRENT_DISK_EPOCH;
  170. failed = !DeviceIoControl (fileHandle,
  171. IOCTL_PARTMGR_NOTIFY_SIGNATURE_CHECK,
  172. &SignatureCheckBufferEpoch,
  173. sizeof (SignatureCheckBufferEpoch),
  174. pSignatureCheckBufferDisks,
  175. bufferLength,
  176. &bytesReturned,
  177. FALSE);
  178. if (failed)
  179. {
  180. printf ("Error performing Claim; error was %d\n", errorCode = GetLastError());
  181. PrintError (errorCode);
  182. return errorCode;
  183. }
  184. else
  185. {
  186. PrintResults (pSignatureCheckBufferDisks);
  187. }
  188. SignatureCheckBufferEpoch.RequestEpoch = 9999999;
  189. failed = !DeviceIoControl (fileHandle,
  190. IOCTL_PARTMGR_NOTIFY_SIGNATURE_CHECK,
  191. &SignatureCheckBufferEpoch,
  192. sizeof (SignatureCheckBufferEpoch),
  193. pSignatureCheckBufferDisks,
  194. bufferLength,
  195. &bytesReturned,
  196. FALSE);
  197. if (failed)
  198. {
  199. printf ("Error performing Claim; error was %d\n", errorCode = GetLastError());
  200. PrintError (errorCode);
  201. // return errorCode;
  202. }
  203. else
  204. {
  205. PrintResults (pSignatureCheckBufferDisks);
  206. }
  207. SignatureCheckBufferEpoch.RequestEpoch = PARTMGR_REQUEST_CURRENT_DISK_EPOCH;
  208. failed = !DeviceIoControl (fileHandle,
  209. IOCTL_PARTMGR_NOTIFY_SIGNATURE_CHECK,
  210. &SignatureCheckBufferEpoch,
  211. sizeof (SignatureCheckBufferEpoch),
  212. pSignatureCheckBufferDisks,
  213. bufferLength,
  214. &bytesReturned,
  215. FALSE);
  216. if (failed)
  217. {
  218. printf ("Error performing Claim; error was %d\n", errorCode = GetLastError());
  219. PrintError (errorCode);
  220. return errorCode;
  221. }
  222. else
  223. {
  224. PrintResults (pSignatureCheckBufferDisks);
  225. }
  226. if (pSignatureCheckBufferDisks->CurrentEpoch > 0)
  227. {
  228. SignatureCheckBufferEpoch.RequestEpoch = pSignatureCheckBufferDisks->CurrentEpoch - 1;
  229. failed = !DeviceIoControl (fileHandle,
  230. IOCTL_PARTMGR_NOTIFY_SIGNATURE_CHECK,
  231. &SignatureCheckBufferEpoch,
  232. sizeof (SignatureCheckBufferEpoch),
  233. pSignatureCheckBufferDisks,
  234. bufferLength,
  235. &bytesReturned,
  236. FALSE);
  237. if (failed)
  238. {
  239. printf ("Error performing Claim; error was %d\n", errorCode = GetLastError());
  240. PrintError (errorCode);
  241. return errorCode;
  242. }
  243. else
  244. {
  245. PrintResults (pSignatureCheckBufferDisks);
  246. }
  247. }
  248. SignatureCheckBufferEpoch.RequestEpoch = 0;
  249. failed = !DeviceIoControl (fileHandle,
  250. IOCTL_PARTMGR_NOTIFY_SIGNATURE_CHECK,
  251. &SignatureCheckBufferEpoch,
  252. sizeof (SignatureCheckBufferEpoch),
  253. pSignatureCheckBufferDisks,
  254. bufferLength,
  255. &bytesReturned,
  256. FALSE);
  257. if (failed)
  258. {
  259. printf ("Error performing Claim; error was %d\n", errorCode = GetLastError());
  260. PrintError (errorCode);
  261. return errorCode;
  262. }
  263. else
  264. {
  265. PrintResults (pSignatureCheckBufferDisks);
  266. }
  267. SignatureCheckBufferEpoch.RequestEpoch = pSignatureCheckBufferDisks->CurrentEpoch;
  268. failed = !DeviceIoControl (fileHandle,
  269. IOCTL_PARTMGR_NOTIFY_SIGNATURE_CHECK,
  270. &SignatureCheckBufferEpoch,
  271. sizeof (SignatureCheckBufferEpoch),
  272. pSignatureCheckBufferDisks,
  273. bufferLength,
  274. &bytesReturned,
  275. FALSE);
  276. if (failed)
  277. {
  278. printf ("Error performing Claim; error was %d\n", errorCode = GetLastError());
  279. PrintError (errorCode);
  280. return errorCode;
  281. }
  282. else
  283. {
  284. PrintResults (pSignatureCheckBufferDisks);
  285. }
  286. ntStatus = NtCancelIoFile (fileHandle, &ioStatusBlock);
  287. ntStatus = NtClose (fileHandle);
  288. return ERROR_SUCCESS;
  289. }
  290. void PrintResults (PPARTMGR_SIGNATURE_CHECK_DISKS pSignatureCheckBufferDisks)
  291. {
  292. ULONG disk;
  293. printf ("Returned Values\n"
  294. "\tCurrent Epoch: 0x%08x\n"
  295. "\tHighest Epoch for returned disks: 0x%08x\n"
  296. "\tNumber of disks returned: 0x%08x\n"
  297. "\tDisks\n",
  298. pSignatureCheckBufferDisks->CurrentEpoch,
  299. pSignatureCheckBufferDisks->HighestDiskEpochReturned,
  300. pSignatureCheckBufferDisks->DiskNumbersReturned);
  301. for (disk = 0; disk < pSignatureCheckBufferDisks->DiskNumbersReturned; disk++)
  302. {
  303. printf ("\t\t0x%08x\n", pSignatureCheckBufferDisks->DiskNumber [disk]);
  304. }
  305. }