NTCreateHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using SMBLibrary.Services;
  12. using SMBLibrary.SMB1;
  13. using Utilities;
  14. namespace SMBLibrary.Server.SMB1
  15. {
  16. internal class NTCreateHelper
  17. {
  18. internal static SMB1Command GetNTCreateResponse(SMB1Header header, NTCreateAndXRequest request, ISMBShare share, SMB1ConnectionState state)
  19. {
  20. SMB1Session session = state.GetSession(header.UID);
  21. bool isExtended = (request.Flags & NTCreateFlags.NT_CREATE_REQUEST_EXTENDED_RESPONSE) > 0;
  22. string path = request.FileName;
  23. if (!path.StartsWith(@"\"))
  24. {
  25. path = @"\" + path;
  26. }
  27. FileAccess createAccess = NTFileStoreHelper.ToCreateFileAccess(request.DesiredAccess, request.CreateDisposition);
  28. if (share is FileSystemShare)
  29. {
  30. if (!((FileSystemShare)share).HasAccess(session.SecurityContext, path, createAccess))
  31. {
  32. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. User '{2}' was denied access.", share.Name, request.FileName, session.UserName);
  33. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  34. return new ErrorResponse(request.CommandName);
  35. }
  36. }
  37. object handle;
  38. FileStatus fileStatus;
  39. FileAttributes fileAttributes = ToFileAttributes(request.ExtFileAttributes);
  40. // GetFileInformation/FileNetworkOpenInformation requires FILE_READ_ATTRIBUTES
  41. AccessMask desiredAccess = request.DesiredAccess | (AccessMask)FileAccessMask.FILE_READ_ATTRIBUTES;
  42. NTStatus createStatus = share.FileStore.CreateFile(out handle, out fileStatus, path, desiredAccess, fileAttributes, request.ShareAccess, request.CreateDisposition, request.CreateOptions, session.SecurityContext);
  43. if (createStatus != NTStatus.STATUS_SUCCESS)
  44. {
  45. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. NTStatus: {2}.", share.Name, path, createStatus);
  46. header.Status = createStatus;
  47. return new ErrorResponse(request.CommandName);
  48. }
  49. FileAccess fileAccess = NTFileStoreHelper.ToFileAccess(desiredAccess);
  50. ushort? fileID = session.AddOpenFile(header.TID, share.Name, path, handle, fileAccess);
  51. if (!fileID.HasValue)
  52. {
  53. share.FileStore.CloseFile(handle);
  54. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. Too many open files.", share.Name, path);
  55. header.Status = NTStatus.STATUS_TOO_MANY_OPENED_FILES;
  56. return new ErrorResponse(request.CommandName);
  57. }
  58. state.LogToServer(Severity.Verbose, "Create: Opened '{0}{1}'. (UID: {2}, TID: {3}, FID: {4})", share.Name, path, header.UID, header.TID, fileID.Value);
  59. if (share is NamedPipeShare)
  60. {
  61. if (isExtended)
  62. {
  63. return CreateResponseExtendedForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
  64. }
  65. else
  66. {
  67. return CreateResponseForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
  68. }
  69. }
  70. else // FileSystemShare
  71. {
  72. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  73. if (isExtended)
  74. {
  75. NTCreateAndXResponseExtended response = CreateResponseExtendedFromFileInformation(fileInfo, fileID.Value, fileStatus);
  76. return response;
  77. }
  78. else
  79. {
  80. NTCreateAndXResponse response = CreateResponseFromFileInformation(fileInfo, fileID.Value, fileStatus);
  81. return response;
  82. }
  83. }
  84. }
  85. private static NTCreateAndXResponse CreateResponseForNamedPipe(ushort fileID, FileStatus fileStatus)
  86. {
  87. NTCreateAndXResponse response = new NTCreateAndXResponse();
  88. response.FID = fileID;
  89. response.CreateDisposition = ToCreateDisposition(fileStatus);
  90. response.ExtFileAttributes = ExtendedFileAttributes.Normal;
  91. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  92. response.NMPipeStatus.ICount = 255;
  93. response.NMPipeStatus.ReadMode = ReadMode.MessageMode;
  94. response.NMPipeStatus.NamedPipeType = NamedPipeType.MessageModePipe;
  95. return response;
  96. }
  97. private static NTCreateAndXResponseExtended CreateResponseExtendedForNamedPipe(ushort fileID, FileStatus fileStatus)
  98. {
  99. NTCreateAndXResponseExtended response = new NTCreateAndXResponseExtended();
  100. response.FID = fileID;
  101. response.CreateDisposition = ToCreateDisposition(fileStatus);
  102. response.ExtFileAttributes = ExtendedFileAttributes.Normal;
  103. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  104. NamedPipeStatus status = new NamedPipeStatus();
  105. status.ICount = 255;
  106. status.ReadMode = ReadMode.MessageMode;
  107. status.NamedPipeType = NamedPipeType.MessageModePipe;
  108. response.NMPipeStatus = status;
  109. response.MaximalAccessRights = (AccessMask)(FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  110. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  111. FileAccessMask.FILE_EXECUTE |
  112. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES) |
  113. AccessMask.DELETE | AccessMask.READ_CONTROL | AccessMask.WRITE_DAC | AccessMask.WRITE_OWNER | AccessMask.SYNCHRONIZE;
  114. response.GuestMaximalAccessRights = (AccessMask)(FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  115. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  116. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES) |
  117. AccessMask.READ_CONTROL | AccessMask.SYNCHRONIZE;
  118. return response;
  119. }
  120. private static NTCreateAndXResponse CreateResponseFromFileInformation(FileNetworkOpenInformation fileInfo, ushort fileID, FileStatus fileStatus)
  121. {
  122. NTCreateAndXResponse response = new NTCreateAndXResponse();
  123. response.FID = fileID;
  124. response.CreateDisposition = ToCreateDisposition(fileStatus);
  125. response.CreateTime = fileInfo.CreationTime;
  126. response.LastAccessTime = fileInfo.LastAccessTime;
  127. response.LastWriteTime = fileInfo.LastWriteTime;
  128. response.LastChangeTime = fileInfo.LastWriteTime;
  129. response.AllocationSize = fileInfo.AllocationSize;
  130. response.EndOfFile = fileInfo.EndOfFile;
  131. response.ExtFileAttributes = (ExtendedFileAttributes)fileInfo.FileAttributes;
  132. response.ResourceType = ResourceType.FileTypeDisk;
  133. response.Directory = fileInfo.IsDirectory;
  134. return response;
  135. }
  136. private static NTCreateAndXResponseExtended CreateResponseExtendedFromFileInformation(FileNetworkOpenInformation fileInfo, ushort fileID, FileStatus fileStatus)
  137. {
  138. NTCreateAndXResponseExtended response = new NTCreateAndXResponseExtended();
  139. response.FID = fileID;
  140. response.CreateDisposition = ToCreateDisposition(fileStatus);
  141. response.CreateTime = fileInfo.CreationTime;
  142. response.LastAccessTime = fileInfo.LastAccessTime;
  143. response.LastWriteTime = fileInfo.LastWriteTime;
  144. response.LastChangeTime = fileInfo.LastWriteTime;
  145. response.ExtFileAttributes = (ExtendedFileAttributes)fileInfo.FileAttributes;
  146. response.AllocationSize = fileInfo.AllocationSize;
  147. response.EndOfFile = fileInfo.EndOfFile;
  148. response.ResourceType = ResourceType.FileTypeDisk;
  149. response.FileStatusFlags = FileStatusFlags.NO_EAS | FileStatusFlags.NO_SUBSTREAMS | FileStatusFlags.NO_REPARSETAG;
  150. response.Directory = fileInfo.IsDirectory;
  151. response.MaximalAccessRights = (AccessMask)(FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  152. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  153. FileAccessMask.FILE_EXECUTE |
  154. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES) |
  155. AccessMask.DELETE | AccessMask.READ_CONTROL | AccessMask.WRITE_DAC | AccessMask.WRITE_OWNER | AccessMask.SYNCHRONIZE;
  156. response.GuestMaximalAccessRights = (AccessMask)(FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  157. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  158. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES) |
  159. AccessMask.READ_CONTROL | AccessMask.SYNCHRONIZE;
  160. return response;
  161. }
  162. private static CreateDisposition ToCreateDisposition(FileStatus fileStatus)
  163. {
  164. if (fileStatus == FileStatus.FILE_SUPERSEDED)
  165. {
  166. return CreateDisposition.FILE_SUPERSEDE;
  167. }
  168. else if (fileStatus == FileStatus.FILE_CREATED)
  169. {
  170. return CreateDisposition.FILE_CREATE;
  171. }
  172. else if (fileStatus == FileStatus.FILE_OVERWRITTEN)
  173. {
  174. return CreateDisposition.FILE_OVERWRITE;
  175. }
  176. else
  177. {
  178. return CreateDisposition.FILE_OPEN;
  179. }
  180. }
  181. private static FileAttributes ToFileAttributes(ExtendedFileAttributes extendedFileAttributes)
  182. {
  183. // We only return flags that can be used with NtCreateFile
  184. FileAttributes fileAttributes = FileAttributes.ReadOnly |
  185. FileAttributes.Hidden |
  186. FileAttributes.System |
  187. FileAttributes.Archive |
  188. FileAttributes.Normal |
  189. FileAttributes.Temporary |
  190. FileAttributes.Offline |
  191. FileAttributes.Encrypted;
  192. return (fileAttributes & (FileAttributes)extendedFileAttributes);
  193. }
  194. }
  195. }