ReadWriteResponseHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. internal class ReadWriteResponseHelper
  18. {
  19. internal static SMB1Command GetReadResponse(SMB1Header header, ReadRequest request, ISMBShare share, SMB1ConnectionState state)
  20. {
  21. SMB1Session session = state.GetSession(header.UID);
  22. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  23. if (openFile == null)
  24. {
  25. state.LogToServer(Severity.Verbose, "Read failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, request.FID);
  26. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  27. return new ErrorResponse(request.CommandName);
  28. }
  29. if (share is FileSystemShare)
  30. {
  31. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, openFile.Path))
  32. {
  33. state.LogToServer(Severity.Verbose, "Read from '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  34. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  35. return new ErrorResponse(request.CommandName);
  36. }
  37. }
  38. byte[] data;
  39. header.Status = share.FileStore.ReadFile(out data, openFile.Handle, request.ReadOffsetInBytes, request.CountOfBytesToRead);
  40. if (header.Status != NTStatus.STATUS_SUCCESS)
  41. {
  42. state.LogToServer(Severity.Verbose, "Read from '{0}{1}' failed. NTStatus: {2}. (FID: {3})", share.Name, openFile.Path, header.Status, request.FID);
  43. return new ErrorResponse(request.CommandName);
  44. }
  45. ReadResponse response = new ReadResponse();
  46. response.Bytes = data;
  47. response.CountOfBytesReturned = (ushort)data.Length;
  48. return response;
  49. }
  50. internal static SMB1Command GetReadResponse(SMB1Header header, ReadAndXRequest request, ISMBShare share, SMB1ConnectionState state)
  51. {
  52. SMB1Session session = state.GetSession(header.UID);
  53. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  54. if (openFile == null)
  55. {
  56. state.LogToServer(Severity.Verbose, "Read failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, request.FID);
  57. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  58. return new ErrorResponse(request.CommandName);
  59. }
  60. if (share is FileSystemShare)
  61. {
  62. if (!((FileSystemShare)share).HasReadAccess(session.SecurityContext, openFile.Path))
  63. {
  64. state.LogToServer(Severity.Verbose, "Read from '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  65. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  66. return new ErrorResponse(request.CommandName);
  67. }
  68. }
  69. uint maxCount = request.MaxCount;
  70. if ((share is FileSystemShare) && state.LargeRead)
  71. {
  72. maxCount = request.MaxCountLarge;
  73. }
  74. byte[] data;
  75. header.Status = share.FileStore.ReadFile(out data, openFile.Handle, (long)request.Offset, (int)maxCount);
  76. if (header.Status == NTStatus.STATUS_END_OF_FILE)
  77. {
  78. // [MS-CIFS] Windows servers set the DataLength field to 0x0000 and return STATUS_SUCCESS.
  79. // JCIFS expects the same response.
  80. data = new byte[0];
  81. header.Status = NTStatus.STATUS_SUCCESS;
  82. }
  83. else if (header.Status != NTStatus.STATUS_SUCCESS)
  84. {
  85. state.LogToServer(Severity.Verbose, "Read from '{0}{1}' failed. NTStatus: {2}. (FID: {3})", share.Name, openFile.Path, header.Status, request.FID);
  86. return new ErrorResponse(request.CommandName);
  87. }
  88. ReadAndXResponse response = new ReadAndXResponse();
  89. if (share is FileSystemShare)
  90. {
  91. // If the client reads from a disk file, this field MUST be set to -1 (0xFFFF)
  92. response.Available = 0xFFFF;
  93. }
  94. response.Data = data;
  95. return response;
  96. }
  97. internal static SMB1Command GetWriteResponse(SMB1Header header, WriteRequest request, ISMBShare share, SMB1ConnectionState state)
  98. {
  99. SMB1Session session = state.GetSession(header.UID);
  100. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  101. if (openFile == null)
  102. {
  103. state.LogToServer(Severity.Verbose, "Write failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, request.FID);
  104. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  105. return new ErrorResponse(request.CommandName);
  106. }
  107. if (share is FileSystemShare)
  108. {
  109. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, openFile.Path))
  110. {
  111. state.LogToServer(Severity.Verbose, "Write to '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  112. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  113. return new ErrorResponse(request.CommandName);
  114. }
  115. }
  116. int numberOfBytesWritten;
  117. header.Status = share.FileStore.WriteFile(out numberOfBytesWritten, openFile.Handle, request.WriteOffsetInBytes, request.Data);
  118. if (header.Status != NTStatus.STATUS_SUCCESS)
  119. {
  120. state.LogToServer(Severity.Verbose, "Write to '{0}{1}' failed. NTStatus: {2}. (FID: {3})", share.Name, openFile.Path, header.Status, request.FID);
  121. return new ErrorResponse(request.CommandName);
  122. }
  123. WriteResponse response = new WriteResponse();
  124. response.CountOfBytesWritten = (ushort)numberOfBytesWritten;
  125. return response;
  126. }
  127. internal static SMB1Command GetWriteResponse(SMB1Header header, WriteAndXRequest request, ISMBShare share, SMB1ConnectionState state)
  128. {
  129. SMB1Session session = state.GetSession(header.UID);
  130. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  131. if (openFile == null)
  132. {
  133. state.LogToServer(Severity.Verbose, "Write failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, request.FID);
  134. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  135. return new ErrorResponse(request.CommandName);
  136. }
  137. if (share is FileSystemShare)
  138. {
  139. if (!((FileSystemShare)share).HasWriteAccess(session.SecurityContext, openFile.Path))
  140. {
  141. state.LogToServer(Severity.Verbose, "Write to '{0}{1}' failed. User '{2}' was denied access.", share.Name, openFile.Path, session.UserName);
  142. header.Status = NTStatus.STATUS_ACCESS_DENIED;
  143. return new ErrorResponse(request.CommandName);
  144. }
  145. }
  146. int numberOfBytesWritten;
  147. header.Status = share.FileStore.WriteFile(out numberOfBytesWritten, openFile.Handle, (long)request.Offset, request.Data);
  148. if (header.Status != NTStatus.STATUS_SUCCESS)
  149. {
  150. state.LogToServer(Severity.Verbose, "Write to '{0}{1}' failed. NTStatus: {2}. (FID: {3})", share.Name, openFile.Path, header.Status, request.FID);
  151. return new ErrorResponse(request.CommandName);
  152. }
  153. WriteAndXResponse response = new WriteAndXResponse();
  154. response.Count = (uint)numberOfBytesWritten;
  155. if (share is FileSystemShare)
  156. {
  157. // If the client wrote to a disk file, this field MUST be set to 0xFFFF.
  158. response.Available = 0xFFFF;
  159. }
  160. return response;
  161. }
  162. internal static SMB1Command GetFlushResponse(SMB1Header header, FlushRequest request, ISMBShare share, SMB1ConnectionState state)
  163. {
  164. SMB1Session session = state.GetSession(header.UID);
  165. if (request.FID == 0xFFFF)
  166. {
  167. // [MS-CIFS] If the FID is 0xFFFF, the Server.Connection.FileOpenTable MUST be scanned for
  168. // all files that were opened by the PID listed in the request header.
  169. // The server MUST attempt to flush each Server.Open so listed.
  170. return new FlushResponse();
  171. }
  172. OpenFileObject openFile = session.GetOpenFileObject(request.FID);
  173. if (openFile == null)
  174. {
  175. state.LogToServer(Severity.Verbose, "Flush failed. Invalid FID. (UID: {0}, TID: {1}, FID: {2})", header.UID, header.TID, request.FID);
  176. header.Status = NTStatus.STATUS_INVALID_HANDLE;
  177. return new ErrorResponse(request.CommandName);
  178. }
  179. header.Status = share.FileStore.FlushFileBuffers(openFile.Handle);
  180. if (header.Status != NTStatus.STATUS_SUCCESS)
  181. {
  182. state.LogToServer(Severity.Verbose, "Flush '{0}{1}' failed. NTStatus: {2}. (FID: {3})", share.Name, openFile.Path, header.Status, request.FID);
  183. return new ErrorResponse(request.CommandName);
  184. }
  185. return new FlushResponse();
  186. }
  187. }
  188. }