ReadWriteResponseHelper.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. Stream stream = openedFile.Stream;
  70. if (share is NamedPipeShare)
  71. {
  72. byte[] data = new byte[maxCount];
  73. int bytesRead = stream.Read(data, 0, maxCount);
  74. if (bytesRead < maxCount)
  75. {
  76. // EOF, we must trim the response data array
  77. data = ByteReader.ReadBytes(data, 0, bytesRead);
  78. }
  79. return data;
  80. }
  81. else // FileSystemShare
  82. {
  83. if (stream == null)
  84. {
  85. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  86. return null;
  87. }
  88. int bytesRead;
  89. byte[] data;
  90. try
  91. {
  92. stream.Seek(offset, SeekOrigin.Begin);
  93. data = new byte[maxCount];
  94. bytesRead = stream.Read(data, 0, maxCount);
  95. }
  96. catch (IOException ex)
  97. {
  98. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  99. if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  100. {
  101. // Returning STATUS_SHARING_VIOLATION is undocumented but apparently valid
  102. state.LogToServer(Severity.Debug, "ReadAndX: Cannot read '{0}'. Sharing Violation.", openedFilePath);
  103. header.Status = NTStatus.STATUS_SHARING_VIOLATION;
  104. return null;
  105. }
  106. else
  107. {
  108. state.LogToServer(Severity.Debug, "ReadAndX: Cannot read '{0}'. Data Error.", openedFilePath);
  109. header.Status = NTStatus.STATUS_DATA_ERROR;
  110. return null;
  111. }
  112. }
  113. catch (ArgumentOutOfRangeException)
  114. {
  115. state.LogToServer(Severity.Debug, "ReadAndX: Cannot read '{0}'. Offset Out Of Range.", openedFilePath);
  116. header.Status = NTStatus.STATUS_DATA_ERROR;
  117. return null;
  118. }
  119. catch (UnauthorizedAccessException)
  120. {
  121. state.LogToServer(Severity.Debug, "ReadAndX: Cannot read '{0}', Access Denied.", openedFilePath);
  122. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  123. return null;
  124. }
  125. if (bytesRead < maxCount)
  126. {
  127. // EOF, we must trim the response data array
  128. data = ByteReader.ReadBytes(data, 0, bytesRead);
  129. }
  130. return data;
  131. }
  132. }
  133. internal static SMB1Command GetWriteResponse(SMB1Header header, WriteRequest request, ISMBShare share, SMB1ConnectionState state)
  134. {
  135. ushort bytesWritten = (ushort)PerformWrite(header, share, request.FID, request.WriteOffsetInBytes, request.Data, state);
  136. if (header.Status != NTStatus.STATUS_SUCCESS)
  137. {
  138. return new ErrorResponse(CommandName.SMB_COM_WRITE_ANDX);
  139. }
  140. WriteResponse response = new WriteResponse();
  141. response.CountOfBytesWritten = bytesWritten;
  142. return response;
  143. }
  144. internal static SMB1Command GetWriteResponse(SMB1Header header, WriteAndXRequest request, ISMBShare share, SMB1ConnectionState state)
  145. {
  146. uint bytesWritten = PerformWrite(header, share, request.FID, request.Offset, request.Data, state);
  147. if (header.Status != NTStatus.STATUS_SUCCESS)
  148. {
  149. return new ErrorResponse(CommandName.SMB_COM_WRITE_ANDX);
  150. }
  151. WriteAndXResponse response = new WriteAndXResponse();
  152. response.Count = bytesWritten;
  153. if (share is FileSystemShare)
  154. {
  155. // If the client wrote to a disk file, this field MUST be set to 0xFFFF.
  156. response.Available = 0xFFFF;
  157. }
  158. return response;
  159. }
  160. public static uint PerformWrite(SMB1Header header, ISMBShare share, ushort FID, ulong offset, byte[] data, SMB1ConnectionState state)
  161. {
  162. OpenedFileObject openedFile = state.GetOpenedFileObject(FID);
  163. if (openedFile == null)
  164. {
  165. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  166. return 0;
  167. }
  168. string openedFilePath = openedFile.Path;
  169. Stream stream = openedFile.Stream;
  170. if (share is NamedPipeShare)
  171. {
  172. stream.Write(data, 0, data.Length);
  173. return (uint)data.Length;
  174. }
  175. else // FileSystemShare
  176. {
  177. if (stream == null)
  178. {
  179. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  180. return 0;
  181. }
  182. try
  183. {
  184. stream.Seek((long)offset, SeekOrigin.Begin);
  185. stream.Write(data, 0, data.Length);
  186. return (uint)data.Length;
  187. }
  188. catch (IOException ex)
  189. {
  190. ushort errorCode = IOExceptionHelper.GetWin32ErrorCode(ex);
  191. if (errorCode == (ushort)Win32Error.ERROR_DISK_FULL)
  192. {
  193. header.Status = NTStatus.STATUS_DISK_FULL;
  194. return 0;
  195. }
  196. else if (errorCode == (ushort)Win32Error.ERROR_SHARING_VIOLATION)
  197. {
  198. // Returning STATUS_SHARING_VIOLATION is undocumented but apparently valid
  199. header.Status = NTStatus.STATUS_SHARING_VIOLATION;
  200. return 0;
  201. }
  202. else
  203. {
  204. header.Status = NTStatus.STATUS_DATA_ERROR;
  205. return 0;
  206. }
  207. }
  208. catch (ArgumentOutOfRangeException)
  209. {
  210. header.Status = NTStatus.STATUS_DATA_ERROR;
  211. return 0;
  212. }
  213. catch (UnauthorizedAccessException)
  214. {
  215. // The user may have tried to write to a readonly file
  216. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  217. return 0;
  218. }
  219. }
  220. }
  221. }
  222. }