NTCreateHelper.cs 10 KB

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