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.

270 lines
6.6 KiB

  1. /*++
  2. Copyright (c) 1989-1997 Microsoft Corporation
  3. Module Name:
  4. findfile.cxx
  5. Abstract:
  6. This module contains tests for finding files by sid
  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. #define SID_MAX_LENGTH \
  17. (FIELD_OFFSET(SID, SubAuthority) + sizeof(ULONG) * SID_MAX_SUB_AUTHORITIES)
  18. __inline
  19. VOID *
  20. Add2Ptr(VOID *pv, ULONG cb)
  21. {
  22. return((BYTE *) pv + cb);
  23. }
  24. __inline
  25. ULONG
  26. QuadAlign( ULONG Value )
  27. {
  28. return (Value + 7) & ~7;
  29. }
  30. //
  31. // Simple wrapper for NtCreateFile
  32. //
  33. NTSTATUS
  34. OpenObject (
  35. WCHAR const *pwszFile,
  36. HANDLE RelatedObject,
  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. if (RelatedObject == NULL) {
  48. RtlDosPathNameToNtPathName_U(pwszFile, &str, NULL, NULL);
  49. } else {
  50. RtlInitUnicodeString(&str, pwszFile);
  51. }
  52. InitializeObjectAttributes(
  53. &oa,
  54. &str,
  55. OBJ_CASE_INSENSITIVE,
  56. RelatedObject,
  57. NULL);
  58. Status = NtCreateFile(
  59. ph,
  60. DesiredAccess | SYNCHRONIZE,
  61. &oa,
  62. &isb,
  63. NULL, // pallocationsize (none!)
  64. FILE_ATTRIBUTE_NORMAL,
  65. ShareAccess,
  66. CreateDisposition,
  67. CreateOptions,
  68. NULL, // EA buffer (none!)
  69. 0);
  70. RtlFreeHeap(RtlProcessHeap(), 0, str.Buffer);
  71. return(Status);
  72. }
  73. void
  74. SzToWsz (
  75. OUT WCHAR *Unicode,
  76. IN char *Ansi
  77. )
  78. {
  79. while (*Unicode++ = *Ansi++)
  80. ;
  81. }
  82. int __cdecl
  83. main (
  84. int argc,
  85. char **argv)
  86. {
  87. if (argc != 3) {
  88. printf( "Usage: findfile [user] [dir]\n" );
  89. return 1;
  90. }
  91. WCHAR FileName[MAX_PATH];
  92. SzToWsz( FileName, argv[2] );
  93. HANDLE Handle;
  94. NTSTATUS Status = OpenObject( FileName,
  95. NULL,
  96. FILE_SYNCHRONOUS_IO_NONALERT,
  97. FILE_READ_ATTRIBUTES,
  98. FILE_SHARE_READ | FILE_SHARE_WRITE,
  99. FILE_OPEN,
  100. &Handle );
  101. if (!NT_SUCCESS( Status )) {
  102. printf( "OpenObject returned %x\n", Status );
  103. return 1;
  104. }
  105. struct {
  106. ULONG Restart;
  107. BYTE Sid[SID_MAX_LENGTH];
  108. } FsCtlInput;
  109. ULONG SidLength = sizeof( FsCtlInput.Sid );
  110. CHAR Domain[MAX_PATH];
  111. ULONG DomainLength = sizeof( Domain );
  112. SID_NAME_USE SidNameUse;
  113. if (!LookupAccountName( NULL,
  114. argv[1],
  115. FsCtlInput.Sid,
  116. &SidLength,
  117. Domain,
  118. &DomainLength,
  119. &SidNameUse )
  120. ) {
  121. printf( "LookupAccountName failed %x\n", GetLastError( ));
  122. return 1;
  123. }
  124. IO_STATUS_BLOCK Iosb;
  125. FsCtlInput.Restart = 1;
  126. BYTE Output[MAX_PATH + 10];
  127. while (TRUE) {
  128. Status = NtFsControlFile(
  129. Handle,
  130. NULL,
  131. NULL,
  132. NULL,
  133. &Iosb,
  134. FSCTL_FIND_FILES_BY_SID,
  135. &FsCtlInput,
  136. sizeof( FsCtlInput ),
  137. NULL,
  138. sizeof( Output ));
  139. Status = NtFsControlFile(
  140. Handle,
  141. NULL,
  142. NULL,
  143. NULL,
  144. &Iosb,
  145. FSCTL_FIND_FILES_BY_SID,
  146. &FsCtlInput,
  147. sizeof( FsCtlInput ),
  148. Output,
  149. sizeof( Output ));
  150. FsCtlInput.Restart = 0;
  151. if (!NT_SUCCESS( Status ) && Status != STATUS_BUFFER_OVERFLOW) {
  152. printf( "NtfsControlFile returned %x\n", Status );
  153. return 1;
  154. }
  155. //
  156. // Display output buffer
  157. //
  158. printf( "Length is %x\n", Iosb.Information );
  159. if (Iosb.Information == 0) {
  160. break;
  161. }
  162. PFILE_NAME_INFORMATION FileNameInfo = (PFILE_NAME_INFORMATION) Output;
  163. while ((PBYTE)FileNameInfo < Output + Iosb.Information) {
  164. ULONG Length =
  165. sizeof( FILE_NAME_INFORMATION ) - sizeof( WCHAR ) +
  166. FileNameInfo->FileNameLength;
  167. printf( "%d: '%.*ws' ",
  168. FileNameInfo->FileNameLength,
  169. FileNameInfo->FileNameLength / sizeof( WCHAR ),
  170. FileNameInfo->FileName );
  171. HANDLE ChildHandle;
  172. WCHAR ChildFileName[ MAX_PATH ];
  173. RtlMoveMemory( ChildFileName, FileNameInfo->FileName, FileNameInfo->FileNameLength );
  174. ChildFileName[FileNameInfo->FileNameLength / sizeof( WCHAR )] = L'\0';
  175. Status = OpenObject( ChildFileName,
  176. Handle,
  177. FILE_SYNCHRONOUS_IO_NONALERT,
  178. FILE_READ_ATTRIBUTES,
  179. FILE_SHARE_READ | FILE_SHARE_WRITE,
  180. FILE_OPEN,
  181. &ChildHandle );
  182. if (!NT_SUCCESS( Status )) {
  183. printf( "\nUnable to open child - %x\n", Status );
  184. } else {
  185. BYTE FileName[MAX_PATH+10];
  186. IO_STATUS_BLOCK Iosb2;
  187. Status = NtQueryInformationFile(
  188. ChildHandle,
  189. &Iosb2,
  190. FileName,
  191. sizeof( FileName ),
  192. FileNameInformation );
  193. if (!NT_SUCCESS( Status )) {
  194. printf( "\nNtQUeryInformationFile failed - %x\n", Status );
  195. } else {
  196. PFILE_NAME_INFORMATION fn = (PFILE_NAME_INFORMATION) FileName;
  197. printf( "%.*ws\n", fn->FileNameLength / sizeof( WCHAR ), fn->FileName );
  198. }
  199. NtClose( ChildHandle );
  200. }
  201. FileNameInfo =
  202. (PFILE_NAME_INFORMATION) Add2Ptr( FileNameInfo, QuadAlign( Length ));
  203. }
  204. }
  205. //
  206. // Close the file
  207. //
  208. Status = NtClose( Handle );
  209. if (!NT_SUCCESS( Status )) {
  210. printf( "Unable to close %s - %x\n", FileName, Status );
  211. }
  212. }