NTCreateHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. NTStatus createStatus = share.FileStore.CreateFile(out handle, out fileStatus, path, request.DesiredAccess, fileAttributes, request.ShareAccess, request.CreateDisposition, request.CreateOptions, session.SecurityContext);
  41. if (createStatus != NTStatus.STATUS_SUCCESS)
  42. {
  43. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. NTStatus: {2}.", share.Name, path, createStatus);
  44. header.Status = createStatus;
  45. return new ErrorResponse(request.CommandName);
  46. }
  47. state.LogToServer(Severity.Verbose, "Create: Opened '{0}{1}'.", share.Name, path);
  48. ushort? fileID = session.AddOpenFile(header.TID, path, handle);
  49. if (!fileID.HasValue)
  50. {
  51. share.FileStore.CloseFile(handle);
  52. header.Status = NTStatus.STATUS_TOO_MANY_OPENED_FILES;
  53. return new ErrorResponse(request.CommandName);
  54. }
  55. if (share is NamedPipeShare)
  56. {
  57. if (isExtended)
  58. {
  59. return CreateResponseExtendedForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
  60. }
  61. else
  62. {
  63. return CreateResponseForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
  64. }
  65. }
  66. else // FileSystemShare
  67. {
  68. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  69. if (isExtended)
  70. {
  71. NTCreateAndXResponseExtended response = CreateResponseExtendedFromFileInformation(fileInfo, fileID.Value, fileStatus);
  72. if ((request.Flags & NTCreateFlags.NT_CREATE_REQUEST_OPBATCH) > 0)
  73. {
  74. response.OpLockLevel = OpLockLevel.BatchOpLockGranted;
  75. }
  76. return response;
  77. }
  78. else
  79. {
  80. NTCreateAndXResponse response = CreateResponseFromFileInformation(fileInfo, fileID.Value, fileStatus);
  81. if ((request.Flags & NTCreateFlags.NT_CREATE_REQUEST_OPBATCH) > 0)
  82. {
  83. response.OpLockLevel = OpLockLevel.BatchOpLockGranted;
  84. }
  85. return response;
  86. }
  87. }
  88. }
  89. private static NTCreateAndXResponse CreateResponseForNamedPipe(ushort fileID, FileStatus fileStatus)
  90. {
  91. NTCreateAndXResponse response = new NTCreateAndXResponse();
  92. response.FID = fileID;
  93. response.CreateDisposition = ToCreateDisposition(fileStatus);
  94. response.ExtFileAttributes = ExtendedFileAttributes.Normal;
  95. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  96. response.NMPipeStatus.ICount = 255;
  97. response.NMPipeStatus.ReadMode = ReadMode.MessageMode;
  98. response.NMPipeStatus.NamedPipeType = NamedPipeType.MessageModePipe;
  99. return response;
  100. }
  101. private static NTCreateAndXResponseExtended CreateResponseExtendedForNamedPipe(ushort fileID, FileStatus fileStatus)
  102. {
  103. NTCreateAndXResponseExtended response = new NTCreateAndXResponseExtended();
  104. response.FID = fileID;
  105. response.CreateDisposition = ToCreateDisposition(fileStatus);
  106. response.ExtFileAttributes = ExtendedFileAttributes.Normal;
  107. response.ResourceType = ResourceType.FileTypeMessageModePipe;
  108. NamedPipeStatus status = new NamedPipeStatus();
  109. status.ICount = 255;
  110. status.ReadMode = ReadMode.MessageMode;
  111. status.NamedPipeType = NamedPipeType.MessageModePipe;
  112. response.NMPipeStatus = status;
  113. response.MaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  114. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  115. FileAccessMask.FILE_EXECUTE |
  116. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  117. FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
  118. response.GuestMaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  119. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  120. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  121. FileAccessMask.READ_CONTROL | FileAccessMask.SYNCHRONIZE;
  122. return response;
  123. }
  124. private static NTCreateAndXResponse CreateResponseFromFileInformation(FileNetworkOpenInformation fileInfo, ushort fileID, FileStatus fileStatus)
  125. {
  126. NTCreateAndXResponse response = new NTCreateAndXResponse();
  127. response.FID = fileID;
  128. response.CreateDisposition = ToCreateDisposition(fileStatus);
  129. response.CreateTime = fileInfo.CreationTime;
  130. response.LastAccessTime = fileInfo.LastAccessTime;
  131. response.LastWriteTime = fileInfo.LastWriteTime;
  132. response.LastChangeTime = fileInfo.LastWriteTime;
  133. response.AllocationSize = fileInfo.AllocationSize;
  134. response.EndOfFile = fileInfo.EndOfFile;
  135. response.ExtFileAttributes = (ExtendedFileAttributes)fileInfo.FileAttributes;
  136. response.ResourceType = ResourceType.FileTypeDisk;
  137. response.Directory = fileInfo.IsDirectory;
  138. return response;
  139. }
  140. private static NTCreateAndXResponseExtended CreateResponseExtendedFromFileInformation(FileNetworkOpenInformation fileInfo, ushort fileID, FileStatus fileStatus)
  141. {
  142. NTCreateAndXResponseExtended response = new NTCreateAndXResponseExtended();
  143. response.FID = fileID;
  144. response.CreateDisposition = ToCreateDisposition(fileStatus);
  145. response.CreateTime = fileInfo.CreationTime;
  146. response.LastAccessTime = fileInfo.LastAccessTime;
  147. response.LastWriteTime = fileInfo.LastWriteTime;
  148. response.LastChangeTime = fileInfo.LastWriteTime;
  149. response.ExtFileAttributes = (ExtendedFileAttributes)fileInfo.FileAttributes;
  150. response.AllocationSize = fileInfo.AllocationSize;
  151. response.EndOfFile = fileInfo.EndOfFile;
  152. response.ResourceType = ResourceType.FileTypeDisk;
  153. response.FileStatusFlags = FileStatusFlags.NO_EAS | FileStatusFlags.NO_SUBSTREAMS | FileStatusFlags.NO_REPARSETAG;
  154. response.Directory = fileInfo.IsDirectory;
  155. response.MaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA | FileAccessMask.FILE_APPEND_DATA |
  156. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  157. FileAccessMask.FILE_EXECUTE |
  158. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  159. FileAccessMask.DELETE | FileAccessMask.READ_CONTROL | FileAccessMask.WRITE_DAC | FileAccessMask.WRITE_OWNER | FileAccessMask.SYNCHRONIZE;
  160. response.GuestMaximalAccessRights.File = FileAccessMask.FILE_READ_DATA | FileAccessMask.FILE_WRITE_DATA |
  161. FileAccessMask.FILE_READ_EA | FileAccessMask.FILE_WRITE_EA |
  162. FileAccessMask.FILE_READ_ATTRIBUTES | FileAccessMask.FILE_WRITE_ATTRIBUTES |
  163. FileAccessMask.READ_CONTROL | FileAccessMask.SYNCHRONIZE;
  164. return response;
  165. }
  166. private static CreateDisposition ToCreateDisposition(FileStatus fileStatus)
  167. {
  168. if (fileStatus == FileStatus.FILE_SUPERSEDED)
  169. {
  170. return CreateDisposition.FILE_SUPERSEDE;
  171. }
  172. else if (fileStatus == FileStatus.FILE_CREATED)
  173. {
  174. return CreateDisposition.FILE_CREATE;
  175. }
  176. else if (fileStatus == FileStatus.FILE_OVERWRITTEN)
  177. {
  178. return CreateDisposition.FILE_OVERWRITE;
  179. }
  180. else
  181. {
  182. return CreateDisposition.FILE_OPEN;
  183. }
  184. }
  185. private static FileAttributes ToFileAttributes(ExtendedFileAttributes extendedFileAttributes)
  186. {
  187. // We only return flags that can be used with NtCreateFile
  188. FileAttributes fileAttributes = FileAttributes.ReadOnly |
  189. FileAttributes.Hidden |
  190. FileAttributes.System |
  191. FileAttributes.Archive |
  192. FileAttributes.Normal |
  193. FileAttributes.Temporary |
  194. FileAttributes.Offline |
  195. FileAttributes.Encrypted;
  196. return (fileAttributes & (FileAttributes)extendedFileAttributes);
  197. }
  198. }
  199. }