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.

302 lines
6.4 KiB

  1. /*++
  2. Copyright (c) 1989-1997 Microsoft Corporation
  3. Module Name:
  4. enumfile.c
  5. Abstract:
  6. This module contains tests for enumeration-by-fileref and bulk security test
  7. --*/
  8. extern "C" {
  9. #include <nt.h>
  10. #include <ntioapi.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. }
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <ddeml.h> // for CP_WINUNICODE
  17. #include <objidl.h>
  18. extern "C"
  19. {
  20. #include <propapi.h>
  21. }
  22. #include <stgprop.h>
  23. #include <stgvar.hxx>
  24. #include <propstm.hxx>
  25. #include <align.hxx>
  26. #include <sstream.hxx>
  27. #define Add2Ptr(P,I) ((PVOID)((PUCHAR)(P) + (I)))
  28. #define QuadAlign(P) ( \
  29. ((((ULONG)(P)) + 7) & 0xfffffff8) \
  30. )
  31. //
  32. // Simple wrapper for NtCreateFile
  33. //
  34. NTSTATUS
  35. OpenObject (
  36. WCHAR const *pwszFile,
  37. ULONG CreateOptions,
  38. ULONG DesiredAccess,
  39. ULONG ShareAccess,
  40. ULONG CreateDisposition,
  41. HANDLE *ph)
  42. {
  43. NTSTATUS Status;
  44. OBJECT_ATTRIBUTES oa;
  45. UNICODE_STRING str;
  46. IO_STATUS_BLOCK isb;
  47. RtlDosPathNameToNtPathName_U(pwszFile, &str, NULL, NULL);
  48. InitializeObjectAttributes(
  49. &oa,
  50. &str,
  51. OBJ_CASE_INSENSITIVE,
  52. NULL,
  53. NULL);
  54. Status = NtCreateFile(
  55. ph,
  56. DesiredAccess | SYNCHRONIZE,
  57. &oa,
  58. &isb,
  59. NULL, // pallocationsize (none!)
  60. FILE_ATTRIBUTE_NORMAL,
  61. ShareAccess,
  62. CreateDisposition,
  63. CreateOptions,
  64. NULL, // EA buffer (none!)
  65. 0);
  66. RtlFreeHeap(RtlProcessHeap(), 0, str.Buffer);
  67. return(Status);
  68. }
  69. void
  70. SzToWsz (
  71. OUT WCHAR *Unicode,
  72. IN char *Ansi
  73. )
  74. {
  75. while (*Unicode++ = *Ansi++)
  76. ;
  77. }
  78. void
  79. BulkSecurityTest (
  80. char *FileName
  81. )
  82. {
  83. NTSTATUS Status;
  84. HANDLE Handle;
  85. WCHAR WFileName[MAX_PATH];
  86. IO_STATUS_BLOCK Iosb;
  87. //
  88. // Open the file
  89. //
  90. SzToWsz( WFileName, FileName );
  91. Status = OpenObject( WFileName,
  92. FILE_SYNCHRONOUS_IO_NONALERT,
  93. FILE_READ_DATA | FILE_WRITE_DATA,
  94. FALSE,
  95. FILE_OPEN,
  96. &Handle );
  97. if (!NT_SUCCESS( Status )) {
  98. printf( "Unable to open %s - %x\n", FileName, Status );
  99. return;
  100. }
  101. #define SECURITY_COUNT 10
  102. char InputBuffer[sizeof( BULK_SECURITY_TEST_DATA ) - sizeof( ULONG ) + SECURITY_COUNT * sizeof( ULONG )];
  103. PBULK_SECURITY_TEST_DATA SecurityData = (PBULK_SECURITY_TEST_DATA) InputBuffer;
  104. SecurityData->DesiredAccess = FILE_READ_DATA | FILE_WRITE_DATA;
  105. for (int i = 0; i < SECURITY_COUNT; i++) {
  106. SecurityData->SecurityIds[i] = 0xFF + i;
  107. }
  108. NTSTATUS Output[SECURITY_COUNT];
  109. Status = NtFsControlFile(
  110. Handle,
  111. NULL,
  112. NULL,
  113. NULL,
  114. &Iosb,
  115. FSCTL_SECURITY_ID_CHECK,
  116. &InputBuffer,
  117. sizeof( InputBuffer ),
  118. Output,
  119. sizeof( Output ));
  120. printf( "Bulk test returned %x\n", Status );
  121. for (i = 0; i < SECURITY_COUNT; i++) {
  122. printf( " Status[%d] = %x\n", i, Output[i] );
  123. }
  124. NtClose( Handle );
  125. }
  126. void
  127. EnumFile (
  128. char *FileName
  129. )
  130. {
  131. NTSTATUS Status;
  132. HANDLE Handle;
  133. WCHAR WFileName[MAX_PATH];
  134. char InputBuffer[10];
  135. char OutputBuffer[200];
  136. IO_STATUS_BLOCK Iosb;
  137. //
  138. // Open the file
  139. //
  140. SzToWsz( WFileName, FileName );
  141. Status = OpenObject( WFileName,
  142. FILE_SYNCHRONOUS_IO_NONALERT,
  143. FILE_READ_DATA | FILE_WRITE_DATA,
  144. FALSE,
  145. FILE_OPEN,
  146. &Handle );
  147. if (!NT_SUCCESS( Status )) {
  148. printf( "Unable to open %s - %x\n", FileName, Status );
  149. return;
  150. }
  151. //
  152. // Set up input data
  153. //
  154. MFT_ENUM_DATA EnumData = { 1, 0, 1 };
  155. //
  156. // Set up output buffer
  157. //
  158. BYTE Output[4096];
  159. while (TRUE) {
  160. Status = NtFsControlFile(
  161. Handle,
  162. NULL,
  163. NULL,
  164. NULL,
  165. &Iosb,
  166. FSCTL_ENUM_USN_DATA,
  167. &EnumData,
  168. sizeof( EnumData ),
  169. Output,
  170. sizeof( Output ));
  171. if (!NT_SUCCESS( Status )) {
  172. printf( "NtfsControlFile returned %x\n", Status );
  173. break;
  174. }
  175. //
  176. // Display output buffer
  177. //
  178. printf( "Length is %x\n", Iosb.Information );
  179. if (Iosb.Information < sizeof( ULONGLONG ) + sizeof( USN_RECORD )) {
  180. break;
  181. }
  182. printf( "Next file reference is %16I64x\n", *(PULONGLONG)Output );
  183. PUSN_RECORD UsnRecord = (PUSN_RECORD) (Output + sizeof( ULONGLONG ));
  184. while ((PBYTE)UsnRecord < Output + Iosb.Information) {
  185. printf( "FR %16I64x Parent %016I64x Usn %016I64x SecurityId %08x Reason %08x Name(%d): '%.*ws'\n",
  186. UsnRecord->FileReferenceNumber,
  187. UsnRecord->ParentFileReferenceNumber,
  188. UsnRecord->Usn,
  189. UsnRecord->SecurityId,
  190. UsnRecord->Reason,
  191. UsnRecord->FileNameLength,
  192. UsnRecord->FileNameLength / sizeof( WCHAR ),
  193. UsnRecord->FileName );
  194. ULONG Length = sizeof( USN_RECORD ) + UsnRecord->FileNameLength - sizeof( WCHAR );
  195. Length = QuadAlign( Length );
  196. UsnRecord = (PUSN_RECORD) Add2Ptr( UsnRecord, Length );
  197. }
  198. EnumData.StartFileReferenceNumber = *(PLONGLONG)Output;
  199. }
  200. //
  201. // Close the file
  202. //
  203. Status = NtClose( Handle );
  204. if (!NT_SUCCESS( Status )) {
  205. printf( "Unable to close %s - %x\n", FileName, Status );
  206. }
  207. }
  208. #define SHIFT(c,v) ((c)--,(v)++)
  209. int __cdecl
  210. main (
  211. int argc,
  212. char **argv)
  213. {
  214. SHIFT( argc, argv );
  215. if (argc > 0) {
  216. if (!strcmp( *argv, "-e")) {
  217. SHIFT( argc, argv );
  218. while (argc != 0) {
  219. EnumFile( *argv );
  220. SHIFT( argc, argv );
  221. }
  222. } else {
  223. BulkSecurityTest( *argv );
  224. }
  225. }
  226. return 0;
  227. }