NTCreateHelper.cs 10 KB

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