TransactionHelper.cs 15 KB

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