NTCreateHelper.cs 12 KB

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