ReadWriteResponseHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.RPC;
  12. using SMBLibrary.SMB1;
  13. using SMBLibrary.Services;
  14. using Utilities;
  15. namespace SMBLibrary.Server.SMB1
  16. {
  17. public class ReadWriteResponseHelper
  18. {
  19. internal static SMB1Command GetReadResponse(SMB1Header header, ReadRequest request, ISMBShare share, SMB1ConnectionState state)
  20. {
  21. byte[] data = PerformRead(header, share, request.FID, request.ReadOffsetInBytes, request.CountOfBytesToRead, state);
  22. if (header.Status != NTStatus.STATUS_SUCCESS)
  23. {
  24. return new ErrorResponse(CommandName.SMB_COM_READ);
  25. }
  26. ReadResponse response = new ReadResponse();
  27. response.Bytes = data;
  28. response.CountOfBytesReturned = (ushort)data.Length;
  29. return response;
  30. }
  31. internal static SMB1Command GetReadResponse(SMB1Header header, ReadAndXRequest request, ISMBShare share, SMB1ConnectionState state)
  32. {
  33. uint maxCount = request.MaxCount;
  34. if ((share is FileSystemShare) && state.LargeRead)
  35. {
  36. maxCount = request.MaxCountLarge;
  37. }
  38. byte[] data = PerformRead(header, share, request.FID, request.Offset, maxCount, state);
  39. if (header.Status != NTStatus.STATUS_SUCCESS)
  40. {
  41. return new ErrorResponse(CommandName.SMB_COM_READ_ANDX);
  42. }
  43. ReadAndXResponse response = new ReadAndXResponse();
  44. if (share is FileSystemShare)
  45. {
  46. // If the client reads from a disk file, this field MUST be set to -1 (0xFFFF)
  47. response.Available = 0xFFFF;
  48. }
  49. response.Data = data;
  50. return response;
  51. }
  52. public static byte[] PerformRead(SMB1Header header, ISMBShare share, ushort FID, ulong offset, uint maxCount, SMB1ConnectionState state)
  53. {
  54. if (offset > Int64.MaxValue || maxCount > Int32.MaxValue)
  55. {
  56. throw new NotImplementedException("Underlying filesystem does not support unsigned offset / read count");
  57. }
  58. return PerformRead(header, share, FID, (long)offset, (int)maxCount, state);
  59. }
  60. public static byte[] PerformRead(SMB1Header header, ISMBShare share, ushort FID, long offset, int maxCount, SMB1ConnectionState state)
  61. {
  62. OpenedFileObject openedFile = state.GetOpenedFileObject(FID);
  63. if (openedFile == null)
  64. {
  65. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  66. return null;
  67. }
  68. string openedFilePath = openedFile.Path;
  69. if (share is NamedPipeShare)
  70. {
  71. return state.RetrieveNamedPipeReply(FID);
  72. }
  73. else // FileSystemShare
  74. {
  75. FileSystemShare fileSystemShare = (FileSystemShare)share;
  76. IFileSystem fileSystem = fileSystemShare.FileSystem;
  77. Stream stream = openedFile.Stream;
  78. if (stream == null)
  79. {
  80. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  81. return null;
  82. }
  83. int bytesRead;
  84. byte[] data;
  85. try
  86. {
  87. stream.Seek(offset, SeekOrigin.Begin);
  88. data = new byte[maxCount];
  89. bytesRead = stream.Read(data, 0, maxCount);
  90. }
  91. catch (IOException ex)
  92. {
  93. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  94. if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  95. {
  96. // Returning STATUS_SHARING_VIOLATION is undocumented but apparently valid
  97. state.LogToServer(Severity.Debug, "ReadAndX: Cannot read '{0}'. Sharing Violation.", openedFilePath);
  98. header.Status = NTStatus.STATUS_SHARING_VIOLATION;
  99. return null;
  100. }
  101. else
  102. {
  103. state.LogToServer(Severity.Debug, "ReadAndX: Cannot read '{0}'. Data Error.", openedFilePath);
  104. header.Status = NTStatus.STATUS_DATA_ERROR;
  105. return null;
  106. }
  107. }
  108. catch (ArgumentOutOfRangeException)
  109. {
  110. state.LogToServer(Severity.Debug, "ReadAndX: Cannot read '{0}'. Offset Out Of Range.", openedFilePath);
  111. header.Status = NTStatus.STATUS_DATA_ERROR;
  112. return null;
  113. }
  114. catch (UnauthorizedAccessException)
  115. {
  116. state.LogToServer(Severity.Debug, "ReadAndX: Cannot read '{0}', Access Denied.", openedFilePath);
  117. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  118. return null;
  119. }
  120. if (bytesRead < maxCount)
  121. {
  122. // EOF, we must trim the response data array
  123. data = ByteReader.ReadBytes(data, 0, bytesRead);
  124. }
  125. return data;
  126. }
  127. }
  128. internal static SMB1Command GetWriteResponse(SMB1Header header, WriteRequest request, ISMBShare share, SMB1ConnectionState state)
  129. {
  130. ushort bytesWritten = (ushort)PerformWrite(header, share, request.FID, request.WriteOffsetInBytes, request.Data, state);
  131. if (header.Status != NTStatus.STATUS_SUCCESS)
  132. {
  133. return new ErrorResponse(CommandName.SMB_COM_WRITE_ANDX);
  134. }
  135. WriteResponse response = new WriteResponse();
  136. response.CountOfBytesWritten = bytesWritten;
  137. return response;
  138. }
  139. internal static SMB1Command GetWriteResponse(SMB1Header header, WriteAndXRequest request, ISMBShare share, SMB1ConnectionState state)
  140. {
  141. uint bytesWritten = PerformWrite(header, share, request.FID, request.Offset, request.Data, state);
  142. if (header.Status != NTStatus.STATUS_SUCCESS)
  143. {
  144. return new ErrorResponse(CommandName.SMB_COM_WRITE_ANDX);
  145. }
  146. WriteAndXResponse response = new WriteAndXResponse();
  147. response.Count = bytesWritten;
  148. if (share is FileSystemShare)
  149. {
  150. // If the client wrote to a disk file, this field MUST be set to 0xFFFF.
  151. response.Available = 0xFFFF;
  152. }
  153. return response;
  154. }
  155. public static uint PerformWrite(SMB1Header header, ISMBShare share, ushort FID, ulong offset, byte[] data, SMB1ConnectionState state)
  156. {
  157. OpenedFileObject openedFile = state.GetOpenedFileObject(FID);
  158. if (openedFile == null)
  159. {
  160. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  161. return 0;
  162. }
  163. string openedFilePath = openedFile.Path;
  164. if (share is NamedPipeShare)
  165. {
  166. RemoteService service = ((NamedPipeShare)share).GetService(openedFilePath);
  167. if (service != null)
  168. {
  169. RPCPDU rpcRequest = RPCPDU.GetPDU(data);
  170. RPCPDU rpcReply = RemoteServiceHelper.GetRPCReply(rpcRequest, service);
  171. byte[] replyData = rpcReply.GetBytes();
  172. state.StoreNamedPipeReply(FID, replyData);
  173. return (uint)data.Length;
  174. }
  175. // This code should not execute unless the SMB request (sequence) is invalid
  176. header.Status = NTStatus.STATUS_INVALID_SMB;
  177. return 0;
  178. }
  179. else // FileSystemShare
  180. {
  181. Stream stream = openedFile.Stream;
  182. if (stream == null)
  183. {
  184. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  185. return 0;
  186. }
  187. try
  188. {
  189. stream.Seek((long)offset, SeekOrigin.Begin);
  190. stream.Write(data, 0, data.Length);
  191. return (uint)data.Length;
  192. }
  193. catch (IOException ex)
  194. {
  195. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  196. if (errorCode == (ushort)Win32Error.ERROR_DISK_FULL)
  197. {
  198. header.Status = NTStatus.STATUS_DISK_FULL;
  199. return 0;
  200. }
  201. else if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  202. {
  203. // Returning STATUS_SHARING_VIOLATION is undocumented but apparently valid
  204. header.Status = NTStatus.STATUS_SHARING_VIOLATION;
  205. return 0;
  206. }
  207. else
  208. {
  209. header.Status = NTStatus.STATUS_DATA_ERROR;
  210. return 0;
  211. }
  212. }
  213. catch (ArgumentOutOfRangeException)
  214. {
  215. header.Status = NTStatus.STATUS_DATA_ERROR;
  216. return 0;
  217. }
  218. catch (UnauthorizedAccessException)
  219. {
  220. // The user may have tried to write to a readonly file
  221. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  222. return 0;
  223. }
  224. }
  225. }
  226. }
  227. }