TransactionHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.Text;
  10. using SMBLibrary.SMB1;
  11. using SMBLibrary.RPC;
  12. using SMBLibrary.Services;
  13. using Utilities;
  14. namespace SMBLibrary.Server
  15. {
  16. public class TransactionHelper
  17. {
  18. /// <summary>
  19. /// There are no interim response messages.
  20. /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
  21. /// The server MUST respond to the transaction request as a whole.
  22. /// </summary>
  23. internal static SMB1Command GetTransactionResponse(SMB1Header header, TransactionRequest request, ISMBShare share, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  24. {
  25. ProcessStateObject processState = state.ObtainProcessState(header.PID);
  26. processState.MaxDataCount = request.MaxDataCount;
  27. if (request.TransParameters.Length < request.TotalParameterCount ||
  28. request.TransData.Length < request.TotalDataCount)
  29. {
  30. // A secondary transaction request is pending
  31. processState.TransactionSetup = request.Setup;
  32. processState.TransactionParameters = new byte[request.TotalParameterCount];
  33. processState.TransactionData = new byte[request.TotalDataCount];
  34. ByteWriter.WriteBytes(processState.TransactionParameters, 0, request.TransParameters);
  35. ByteWriter.WriteBytes(processState.TransactionData, 0, request.TransData);
  36. processState.TransactionParametersReceived += request.TransParameters.Length;
  37. processState.TransactionDataReceived += request.TransData.Length;
  38. return null;
  39. }
  40. else
  41. {
  42. // We have a complete command
  43. if (request is Transaction2Request)
  44. {
  45. return GetCompleteTransaction2Response(header, request.Setup, request.TransParameters, request.TransData, share, state, sendQueue);
  46. }
  47. else
  48. {
  49. return GetCompleteTransactionResponse(header, request.Setup, request.TransParameters, request.TransData, share, state, sendQueue);
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// There are no secondary response messages.
  55. /// The client MUST send as many secondary requests as are needed to complete the transfer of the transaction request.
  56. /// The server MUST respond to the transaction request as a whole.
  57. /// </summary>
  58. internal static SMB1Command GetTransactionResponse(SMB1Header header, TransactionSecondaryRequest request, ISMBShare share, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  59. {
  60. ProcessStateObject processState = state.GetProcessState(header.PID);
  61. if (processState == null)
  62. {
  63. throw new InvalidRequestException();
  64. }
  65. ByteWriter.WriteBytes(processState.TransactionParameters, request.ParameterDisplacement, request.TransParameters);
  66. ByteWriter.WriteBytes(processState.TransactionData, request.DataDisplacement, request.TransData);
  67. processState.TransactionParametersReceived += request.TransParameters.Length;
  68. processState.TransactionDataReceived += request.TransData.Length;
  69. if (processState.TransactionParametersReceived < processState.TransactionParameters.Length ||
  70. processState.TransactionDataReceived < processState.TransactionData.Length)
  71. {
  72. return null;
  73. }
  74. else
  75. {
  76. // We have a complete command
  77. if (request is Transaction2SecondaryRequest)
  78. {
  79. return GetCompleteTransaction2Response(header, processState.TransactionSetup, processState.TransactionParameters, processState.TransactionData, share, state, sendQueue);
  80. }
  81. else
  82. {
  83. return GetCompleteTransactionResponse(header, processState.TransactionSetup, processState.TransactionParameters, processState.TransactionData, share, state, sendQueue);
  84. }
  85. }
  86. }
  87. internal static SMB1Command GetCompleteTransactionResponse(SMB1Header header, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  88. {
  89. TransactionSubcommand subcommand = TransactionSubcommand.GetSubcommandRequest(requestSetup, requestParameters, requestData, header.UnicodeFlag);
  90. TransactionSubcommand subcommandResponse = null;
  91. if (subcommand is TransactionSetNamedPipeStateRequest)
  92. {
  93. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  94. }
  95. else if (subcommand is TransactionRawReadNamedPipeRequest)
  96. {
  97. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  98. }
  99. else if (subcommand is TransactionQueryNamedPipeStateRequest)
  100. {
  101. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  102. }
  103. else if (subcommand is TransactionQueryNamedPipeInfoRequest)
  104. {
  105. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  106. }
  107. else if (subcommand is TransactionPeekNamedPipeRequest)
  108. {
  109. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  110. }
  111. else if (subcommand is TransactionTransactNamedPipeRequest)
  112. {
  113. if (!(share is NamedPipeShare))
  114. {
  115. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  116. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION);
  117. }
  118. subcommandResponse = TransactionSubcommandHelper.GetSubcommandResponse(header, (TransactionTransactNamedPipeRequest)subcommand, (NamedPipeShare)share, state);
  119. }
  120. else if (subcommand is TransactionRawWriteNamedPipeRequest)
  121. {
  122. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  123. }
  124. else if (subcommand is TransactionReadNamedPipeRequest)
  125. {
  126. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  127. }
  128. else if (subcommand is TransactionWriteNamedPipeRequest)
  129. {
  130. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  131. }
  132. else if (subcommand is TransactionWaitNamedPipeRequest)
  133. {
  134. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  135. }
  136. else if (subcommand is TransactionCallNamedPipeRequest)
  137. {
  138. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  139. }
  140. else
  141. {
  142. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  143. }
  144. if (header.Status != NTStatus.STATUS_SUCCESS)
  145. {
  146. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION);
  147. }
  148. byte[] responseSetup = subcommandResponse.GetSetup();
  149. byte[] responseParameters = subcommandResponse.GetParameters(header.UnicodeFlag);
  150. byte[] responseData = subcommandResponse.GetData();
  151. TransactionResponse response = new TransactionResponse();
  152. PrepareResponse(response, responseSetup, responseParameters, responseData, state.MaxBufferSize, sendQueue);
  153. return response;
  154. }
  155. internal static SMB1Command GetCompleteTransaction2Response(SMB1Header header, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  156. {
  157. Transaction2Subcommand subcommand = Transaction2Subcommand.GetSubcommandRequest(requestSetup, requestParameters, requestData, header.UnicodeFlag);
  158. Transaction2Subcommand subcommandResponse = null;
  159. if (!(share is FileSystemShare))
  160. {
  161. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  162. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION2);
  163. }
  164. FileSystemShare fileSystemShare = (FileSystemShare)share;
  165. if (subcommand is Transaction2FindFirst2Request)
  166. {
  167. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2FindFirst2Request)subcommand, fileSystemShare, state);
  168. }
  169. else if (subcommand is Transaction2FindNext2Request)
  170. {
  171. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2FindNext2Request)subcommand, fileSystemShare, state);
  172. }
  173. else if (subcommand is Transaction2QueryFSInformationRequest)
  174. {
  175. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2QueryFSInformationRequest)subcommand, fileSystemShare);
  176. }
  177. else if (subcommand is Transaction2QueryPathInformationRequest)
  178. {
  179. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2QueryPathInformationRequest)subcommand, fileSystemShare);
  180. }
  181. else if (subcommand is Transaction2SetPathInformationRequest)
  182. {
  183. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  184. }
  185. else if (subcommand is Transaction2QueryFileInformationRequest)
  186. {
  187. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2QueryFileInformationRequest)subcommand, fileSystemShare, state);
  188. }
  189. else if (subcommand is Transaction2SetFileInformationRequest)
  190. {
  191. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2SetFileInformationRequest)subcommand, fileSystemShare, state);
  192. }
  193. else if (subcommand is Transaction2CreateDirectoryRequest)
  194. {
  195. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  196. }
  197. else if (subcommand is Transaction2GetDfsReferralRequest)
  198. {
  199. header.Status = NTStatus.STATUS_NO_SUCH_DEVICE;
  200. }
  201. else
  202. {
  203. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  204. }
  205. if (header.Status != NTStatus.STATUS_SUCCESS)
  206. {
  207. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION2);
  208. }
  209. byte[] responseSetup = subcommandResponse.GetSetup();
  210. byte[] responseParameters = subcommandResponse.GetParameters(header.UnicodeFlag);
  211. byte[] responseData = subcommandResponse.GetData(header.UnicodeFlag);
  212. Transaction2Response response = new Transaction2Response();
  213. PrepareResponse(response, responseSetup, responseParameters, responseData, state.MaxBufferSize, sendQueue);
  214. return response;
  215. }
  216. internal static void PrepareResponse(TransactionResponse response, byte[] responseSetup, byte[] responseParameters, byte[] responseData, int maxBufferSize, List<SMB1Command> sendQueue)
  217. {
  218. int responseSize = TransactionResponse.CalculateMessageSize(responseSetup.Length, responseParameters.Length, responseData.Length);
  219. if (responseSize <= maxBufferSize)
  220. {
  221. response.Setup = responseSetup;
  222. response.TotalParameterCount = (ushort)responseParameters.Length;
  223. response.TotalDataCount = (ushort)responseData.Length;
  224. response.TransParameters = responseParameters;
  225. response.TransData = responseData;
  226. }
  227. else
  228. {
  229. int currentDataLength = maxBufferSize - (responseSize - responseData.Length);
  230. byte[] buffer = new byte[currentDataLength];
  231. Array.Copy(responseData, 0, buffer, 0, currentDataLength);
  232. response.Setup = responseSetup;
  233. response.TotalParameterCount = (ushort)responseParameters.Length;
  234. response.TotalDataCount = (ushort)responseData.Length;
  235. response.TransParameters = responseParameters;
  236. response.TransData = buffer;
  237. int dataBytesLeftToSend = responseData.Length - currentDataLength;
  238. while (dataBytesLeftToSend > 0)
  239. {
  240. TransactionResponse additionalResponse;
  241. if (response is Transaction2Response)
  242. {
  243. additionalResponse = new Transaction2Response();
  244. }
  245. else
  246. {
  247. additionalResponse = new TransactionResponse();
  248. }
  249. currentDataLength = dataBytesLeftToSend;
  250. responseSize = TransactionResponse.CalculateMessageSize(0, 0, dataBytesLeftToSend);
  251. if (responseSize > maxBufferSize)
  252. {
  253. currentDataLength = maxBufferSize - (responseSize - dataBytesLeftToSend);
  254. }
  255. buffer = new byte[currentDataLength];
  256. int dataBytesSent = responseData.Length - dataBytesLeftToSend;
  257. Array.Copy(responseData, dataBytesSent, buffer, 0, currentDataLength);
  258. additionalResponse.TotalParameterCount = (ushort)responseParameters.Length;
  259. additionalResponse.TotalDataCount = (ushort)responseData.Length;
  260. additionalResponse.TransData = buffer;
  261. additionalResponse.ParameterDisplacement = (ushort)response.TransParameters.Length;
  262. additionalResponse.DataDisplacement = (ushort)dataBytesSent;
  263. sendQueue.Add(additionalResponse);
  264. dataBytesLeftToSend -= currentDataLength;
  265. }
  266. }
  267. }
  268. }
  269. }