NTCreateHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. FileAccessMask desiredAccess = request.DesiredAccess | 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. ushort? fileID = session.AddOpenFile(header.TID, share.Name, path, handle);
  50. if (!fileID.HasValue)
  51. {
  52. share.FileStore.CloseFile(handle);
  53. state.LogToServer(Severity.Verbose, "Create: Opening '{0}{1}' failed. Too many open files.", share.Name, path);
  54. header.Status = NTStatus.STATUS_TOO_MANY_OPENED_FILES;
  55. return new ErrorResponse(request.CommandName);
  56. }
  57. state.LogToServer(Severity.Verbose, "Create: Opened '{0}{1}'. (UID: {2}, TID: {3}, FID: {4})", share.Name, path, header.UID, header.TID, fileID.Value);
  58. if (share is NamedPipeShare)
  59. {
  60. if (isExtended)
  61. {
  62. return CreateResponseExtendedForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
  63. }
  64. else
  65. {
  66. return CreateResponseForNamedPipe(fileID.Value, FileStatus.FILE_OPENED);
  67. }
  68. }
  69. else // FileSystemShare
  70. {
  71. FileNetworkOpenInformation fileInfo = NTFileStoreHelper.GetNetworkOpenInformation(share.FileStore, handle);
  72. if (isExtended)
  73. {
  74. NTCreateAndXResponseExtended response = CreateResponseExtendedFromFileInformation(fileInfo, fileID.Value, fileStatus);
  75. return response;
  76. }
  77. else
  78. {
  79. NTCreateAndXResponse response = CreateResponseFromFileInformation(fileInfo, fileID.Value, fileStatus);
  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.MessageModePipe;
  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.MessageModePipe;
  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. private static FileAttributes ToFileAttributes(ExtendedFileAttributes extendedFileAttributes)
  181. {
  182. // We only return flags that can be used with NtCreateFile
  183. FileAttributes fileAttributes = FileAttributes.ReadOnly |
  184. FileAttributes.Hidden |
  185. FileAttributes.System |
  186. FileAttributes.Archive |
  187. FileAttributes.Normal |
  188. FileAttributes.Temporary |
  189. FileAttributes.Offline |
  190. FileAttributes.Encrypted;
  191. return (fileAttributes & (FileAttributes)extendedFileAttributes);
  192. }
  193. }
  194. }