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.

401 lines
12 KiB

  1. // oboid.c
  2. #include "oidtst.h"
  3. //
  4. // Build with USE_RELATIVE_OPEN set to one to use relative opens for
  5. // object IDs. Build with USE_RELATIVE_OPEN set to zero to use a device
  6. // path open for object IDs.
  7. // Opens by File ID always use a relative open.
  8. //
  9. #define USE_RELATIVE_OPEN 1
  10. #define VOLUME_PATH L"\\\\.\\H:"
  11. #define VOLUME_DRIVE_LETTER_INDEX 4
  12. #define FULL_PATH L"\\??\\H:\\1234567890123456"
  13. #define FULL_DRIVE_LETTER_INDEX 4
  14. #define DEVICE_PREFIX_LEN 14
  15. int
  16. FsTestOpenByOid (
  17. IN UCHAR *ObjectId,
  18. IN ULONG ArgLength,
  19. IN PWCHAR DriveLetter
  20. )
  21. {
  22. HANDLE File;
  23. HANDLE DumpFile;
  24. IO_STATUS_BLOCK IoStatusBlock;
  25. NTSTATUS Status;
  26. NTSTATUS ReadStatus;
  27. NTSTATUS WriteStatus;
  28. NTSTATUS GetNameStatus;
  29. NTSTATUS CloseStatus;
  30. OBJECT_ATTRIBUTES ObjectAttributes;
  31. UNICODE_STRING str;
  32. char mybuffer[32768];
  33. PFILE_NAME_INFORMATION FileName;
  34. LARGE_INTEGER ByteOffset;
  35. HANDLE VolumeHandle;
  36. DWORD WStatus;
  37. WCHAR Full[] = FULL_PATH; // Arrays of WCHAR's aren't constants
  38. WCHAR Volume[] = VOLUME_PATH;
  39. ULONG BytesToWrite;
  40. RtlInitUnicodeString( &str, Full );
  41. RtlCopyMemory( &str.Buffer[FULL_DRIVE_LETTER_INDEX], DriveLetter, sizeof(WCHAR) );
  42. str.Length = 0x1E;
  43. if (ArgLength == 32) {
  44. //
  45. // Open by Object ID.
  46. //
  47. #if USE_RELATIVE_OPEN
  48. //
  49. // Open the volume for relative opens.
  50. //
  51. RtlCopyMemory( &Volume[VOLUME_DRIVE_LETTER_INDEX], DriveLetter, sizeof(WCHAR) );
  52. printf( "\nOpening volume handle, this may take a while..." );
  53. VolumeHandle = CreateFileW( (PUSHORT) &Volume,
  54. GENERIC_READ | GENERIC_WRITE,
  55. FILE_SHARE_READ | FILE_SHARE_WRITE,
  56. NULL,
  57. OPEN_EXISTING,
  58. 0,
  59. NULL );
  60. if (VolumeHandle == INVALID_HANDLE_VALUE) {
  61. WStatus = GetLastError();
  62. printf( "Unable to open %ws volume\n", &Volume );
  63. printf( "Error from CreateFile", WStatus );
  64. return WStatus;
  65. }
  66. str.Length = 16;
  67. RtlCopyMemory( &str.Buffer[0], // no device prefix for relative open.
  68. ObjectId,
  69. 16 );
  70. #else
  71. //
  72. // Form open path using a device prefix string.
  73. //
  74. str.Length = DEVICE_PREFIX_LEN+16;
  75. RtlCopyMemory( &str.Buffer[DEVICE_PREFIX_LEN/2], // DEVICE_PREFIX_LEN/2 goes past "\??\D:\"
  76. ObjectId,
  77. 16 );
  78. VolumeHandle = NULL;
  79. #endif
  80. } else if (ArgLength == 16) {
  81. //
  82. // Open by File Reference Number (FileID),
  83. // Relative opens from the Volume Handle.
  84. //
  85. RtlCopyMemory( &Volume[VOLUME_DRIVE_LETTER_INDEX], DriveLetter, sizeof(WCHAR) );
  86. printf( "\nOpening volume handle, this may take a while..." );
  87. VolumeHandle = CreateFileW( (PUSHORT) &Volume,
  88. GENERIC_READ | GENERIC_WRITE,
  89. FILE_SHARE_READ | FILE_SHARE_WRITE,
  90. NULL,
  91. OPEN_EXISTING,
  92. 0,
  93. NULL );
  94. if (VolumeHandle == INVALID_HANDLE_VALUE ) {
  95. WStatus = GetLastError();
  96. printf( "Unable to open %ws volume\n", &Volume );
  97. printf( "Error from CreateFile", WStatus );
  98. return WStatus;
  99. }
  100. str.Length = 8;
  101. RtlCopyMemory( &str.Buffer[0], // no device prefix for relative open.
  102. ObjectId,
  103. 8 );
  104. } else {
  105. return 0;
  106. }
  107. InitializeObjectAttributes( &ObjectAttributes,
  108. &str,
  109. OBJ_CASE_INSENSITIVE,
  110. VolumeHandle,
  111. NULL );
  112. Status = NtCreateFile( &File,
  113. GENERIC_READ, // GENERIC_ALL | STANDARD_RIGHTS_ALL,
  114. &ObjectAttributes,
  115. &IoStatusBlock,
  116. NULL, // AllocationSize
  117. FILE_ATTRIBUTE_NORMAL,
  118. FILE_SHARE_READ | FILE_SHARE_WRITE,
  119. FILE_OPEN,
  120. FILE_OPEN_BY_FILE_ID,
  121. NULL, // EaBuffer
  122. 0 );
  123. if (NT_SUCCESS( Status )) {
  124. printf( "\nOpened file succesfully" );
  125. #if 0
  126. ByteOffset.HighPart = ByteOffset.LowPart = 0;
  127. ReadStatus = NtReadFile( File,
  128. NULL, // Event
  129. NULL, // ApcRoutine
  130. NULL, // ApcContext
  131. &IoStatusBlock,
  132. mybuffer,
  133. sizeof(mybuffer),
  134. &ByteOffset, // ByteOffset
  135. NULL ); // Key
  136. printf( "\nReadstatus %x, read %x bytes. Here they are: ",
  137. ReadStatus,
  138. IoStatusBlock.Information );
  139. printf( "\n%s", mybuffer );
  140. printf( "\nThat's it" );
  141. #endif
  142. #if 1
  143. FileName = (PFILE_NAME_INFORMATION) &mybuffer[0];
  144. FileName->FileNameLength = sizeof(mybuffer) - sizeof(ULONG);
  145. GetNameStatus = NtQueryInformationFile( File,
  146. &IoStatusBlock,
  147. FileName,
  148. sizeof(mybuffer),
  149. FileNameInformation );
  150. printf( "\nGetNameStatus %x, Filename is ", GetNameStatus );
  151. printf( "%S\n", FileName->FileName );
  152. #endif
  153. #if 0
  154. DumpFile = CreateFile( "c:\\dumpfile",
  155. GENERIC_WRITE,
  156. FILE_SHARE_READ | FILE_SHARE_WRITE,
  157. NULL,
  158. CREATE_ALWAYS,
  159. 0,
  160. NULL );
  161. if (DumpFile == INVALID_HANDLE_VALUE) {
  162. printf( "Error opening dump file %x\n", GetLastError() );
  163. return FALSE;
  164. }
  165. ByteOffset.HighPart = ByteOffset.LowPart = 0;
  166. while(TRUE) {
  167. ReadStatus = NtReadFile( File,
  168. NULL, // Event
  169. NULL, // ApcRoutine
  170. NULL, // ApcContext
  171. &IoStatusBlock,
  172. mybuffer,
  173. sizeof(mybuffer),
  174. &ByteOffset, // ByteOffset
  175. NULL ); // Key
  176. BytesToWrite = IoStatusBlock.Information;
  177. if (NT_SUCCESS( ReadStatus) &&
  178. BytesToWrite > 0) {
  179. WriteStatus = NtWriteFile( DumpFile,
  180. NULL,
  181. NULL,
  182. NULL,
  183. &IoStatusBlock,
  184. mybuffer,
  185. BytesToWrite,
  186. &ByteOffset,
  187. NULL );
  188. printf( "\nOffset %x", ByteOffset.LowPart );
  189. ByteOffset.LowPart += BytesToWrite;
  190. } else {
  191. break;
  192. }
  193. }
  194. printf( "\n" );
  195. CloseStatus = NtClose( DumpFile );
  196. #endif
  197. CloseStatus = NtClose( File );
  198. if (!NT_SUCCESS( CloseStatus )) {
  199. printf( "\nCloseStatus %x", CloseStatus );
  200. }
  201. }
  202. if (VolumeHandle != NULL) {
  203. CloseHandle( VolumeHandle );
  204. }
  205. return FsTestDecipherStatus( Status );
  206. }
  207. VOID
  208. StrToGuid(
  209. IN PCHAR s,
  210. OUT GUID *pGuid
  211. )
  212. /*++
  213. Routine Description:
  214. Convert a string in GUID display format to an object ID that
  215. can be used to lookup a file.
  216. based on a routine by Mac McLain
  217. Arguments:
  218. pGuid - ptr to the GUID.
  219. s - The input character buffer in display guid format.
  220. e.g.: b81b486b-c338-11d0-ba4f0000f80007df
  221. Must be at least GUID_CHAR_LEN (35 bytes) long.
  222. Function Return Value:
  223. None.
  224. --*/
  225. {
  226. #define DEBSUB "GuidToStr:"
  227. if (pGuid != NULL) {
  228. sscanf( s, "%08lx-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
  229. &pGuid->Data1,
  230. &pGuid->Data2,
  231. &pGuid->Data3,
  232. &pGuid->Data4[0],
  233. &pGuid->Data4[1],
  234. &pGuid->Data4[2],
  235. &pGuid->Data4[3],
  236. &pGuid->Data4[4],
  237. &pGuid->Data4[5],
  238. &pGuid->Data4[6],
  239. &pGuid->Data4[7] );
  240. } else {
  241. sprintf( s, "<ptr-null>" );
  242. }
  243. }
  244. VOID
  245. FsTestObOidHelp(
  246. char *ExeName
  247. )
  248. {
  249. printf( "This program opens a file by its file id or object id (ntfs only).\n\n" );
  250. printf( "usage: %s x: [FileID | Raw ObjectId | Guid Display Format ObjectID]\n", ExeName );
  251. printf( "Where x: is the drive letter\n" );
  252. printf( "A FileID is a string of 16 hex digits with a space between each\n"
  253. "group of 8. E.G. oboid 00010000 00000024\n\n" );
  254. printf( "A raw object ID is a string of 32 hex digits with a space\n"
  255. "between each group of 8\n"
  256. "E.G. ObjectId:df0700f8 00004fba 11d0c338 b81b485f\n\n" );
  257. printf( "A GUID display format object ID is a string of the form \n"
  258. "b81b486b-c338-11d0-ba4f0000f80007df\n"
  259. "See the struct def for GUID in sdk\\inc\\winnt.h for byte layout.\n" );
  260. }
  261. VOID
  262. _cdecl
  263. main(
  264. int argc,
  265. char *argv[]
  266. )
  267. {
  268. ULONG ObjectId[4];
  269. ULONG Length;
  270. WCHAR Drive;
  271. //
  272. // Get parameters.
  273. //
  274. if (argc < 3) {
  275. FsTestObOidHelp( argv[0] );
  276. return;
  277. }
  278. RtlZeroBytes( ObjectId,
  279. sizeof( ObjectId ) );
  280. Length = strlen( argv[2] );
  281. if ((argc == 3) && (Length == 35) && (argv[2][8] == '-')) {
  282. StrToGuid( argv[2], (GUID *)ObjectId );
  283. printf( "\nUsing ObjectId: %08x %08x %08x %08x\n",
  284. ObjectId[3], ObjectId[2], ObjectId[1], ObjectId[0] );
  285. Length = 32;
  286. } else if (argc == 6) {
  287. sscanf( argv[2], "%08x", &ObjectId[3] );
  288. sscanf( argv[3], "%08x", &ObjectId[2] );
  289. sscanf( argv[4], "%08x", &ObjectId[1] );
  290. sscanf( argv[5], "%08x", &ObjectId[0] );
  291. printf( "\nUsing ObjectId: %08x %08x %08x %08x\n",
  292. ObjectId[3], ObjectId[2], ObjectId[1], ObjectId[0] );
  293. Length = 32;
  294. } else if (argc == 4) {
  295. sscanf( argv[2], "%08x", &ObjectId[1] );
  296. sscanf( argv[3], "%08x", &ObjectId[0] );
  297. printf( "\nUsing FileId: %08x %08x\n", ObjectId[1], ObjectId[0] );
  298. Length = 16;
  299. } else {
  300. printf("Arg (%s) invalid format.\n\n", argv[2]);
  301. FsTestObOidHelp( argv[0] );
  302. }
  303. Drive = *argv[1];
  304. FsTestOpenByOid( (PUCHAR) ObjectId, Length, &Drive );
  305. return;
  306. }