TransactionHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 = TransactionSubcommand.GetSubcommandRequest(requestSetup, requestParameters, requestData, header.UnicodeFlag);
  98. TransactionSubcommand subcommandResponse = null;
  99. if (subcommand is TransactionSetNamedPipeStateRequest)
  100. {
  101. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  102. }
  103. else if (subcommand is TransactionRawReadNamedPipeRequest)
  104. {
  105. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  106. }
  107. else if (subcommand is TransactionQueryNamedPipeStateRequest)
  108. {
  109. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  110. }
  111. else if (subcommand is TransactionQueryNamedPipeInfoRequest)
  112. {
  113. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  114. }
  115. else if (subcommand is TransactionPeekNamedPipeRequest)
  116. {
  117. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  118. }
  119. else if (subcommand is TransactionTransactNamedPipeRequest)
  120. {
  121. if (!(share is NamedPipeShare))
  122. {
  123. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  124. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION);
  125. }
  126. subcommandResponse = TransactionSubcommandHelper.GetSubcommandResponse(header, (TransactionTransactNamedPipeRequest)subcommand, (NamedPipeShare)share, state);
  127. }
  128. else if (subcommand is TransactionRawWriteNamedPipeRequest)
  129. {
  130. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  131. }
  132. else if (subcommand is TransactionReadNamedPipeRequest)
  133. {
  134. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  135. }
  136. else if (subcommand is TransactionWriteNamedPipeRequest)
  137. {
  138. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  139. }
  140. else if (subcommand is TransactionWaitNamedPipeRequest)
  141. {
  142. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  143. }
  144. else if (subcommand is TransactionCallNamedPipeRequest)
  145. {
  146. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  147. }
  148. else
  149. {
  150. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  151. }
  152. if (header.Status != NTStatus.STATUS_SUCCESS)
  153. {
  154. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION);
  155. }
  156. byte[] responseSetup = subcommandResponse.GetSetup();
  157. byte[] responseParameters = subcommandResponse.GetParameters(header.UnicodeFlag);
  158. byte[] responseData = subcommandResponse.GetData();
  159. TransactionResponse response = new TransactionResponse();
  160. PrepareResponse(response, responseSetup, responseParameters, responseData, state.MaxBufferSize, sendQueue);
  161. return response;
  162. }
  163. internal static SMB1Command GetCompleteTransaction2Response(SMB1Header header, byte[] requestSetup, byte[] requestParameters, byte[] requestData, ISMBShare share, SMB1ConnectionState state, List<SMB1Command> sendQueue)
  164. {
  165. Transaction2Subcommand subcommand = Transaction2Subcommand.GetSubcommandRequest(requestSetup, requestParameters, requestData, header.UnicodeFlag);
  166. Transaction2Subcommand subcommandResponse = null;
  167. if (!(share is FileSystemShare))
  168. {
  169. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  170. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION2);
  171. }
  172. FileSystemShare fileSystemShare = (FileSystemShare)share;
  173. if (subcommand is Transaction2FindFirst2Request)
  174. {
  175. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2FindFirst2Request)subcommand, fileSystemShare, state);
  176. }
  177. else if (subcommand is Transaction2FindNext2Request)
  178. {
  179. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2FindNext2Request)subcommand, fileSystemShare, state);
  180. }
  181. else if (subcommand is Transaction2QueryFSInformationRequest)
  182. {
  183. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2QueryFSInformationRequest)subcommand, fileSystemShare, state);
  184. }
  185. else if (subcommand is Transaction2QueryPathInformationRequest)
  186. {
  187. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2QueryPathInformationRequest)subcommand, fileSystemShare, state);
  188. }
  189. else if (subcommand is Transaction2SetPathInformationRequest)
  190. {
  191. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  192. }
  193. else if (subcommand is Transaction2QueryFileInformationRequest)
  194. {
  195. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2QueryFileInformationRequest)subcommand, fileSystemShare, state);
  196. }
  197. else if (subcommand is Transaction2SetFileInformationRequest)
  198. {
  199. subcommandResponse = Transaction2SubcommandHelper.GetSubcommandResponse(header, (Transaction2SetFileInformationRequest)subcommand, fileSystemShare, state);
  200. }
  201. else if (subcommand is Transaction2CreateDirectoryRequest)
  202. {
  203. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  204. }
  205. else if (subcommand is Transaction2GetDfsReferralRequest)
  206. {
  207. header.Status = NTStatus.STATUS_NO_SUCH_DEVICE;
  208. }
  209. else
  210. {
  211. header.Status = NTStatus.STATUS_SMB_BAD_COMMAND;
  212. }
  213. if (header.Status != NTStatus.STATUS_SUCCESS)
  214. {
  215. return new ErrorResponse(CommandName.SMB_COM_TRANSACTION2);
  216. }
  217. byte[] responseSetup = subcommandResponse.GetSetup();
  218. byte[] responseParameters = subcommandResponse.GetParameters(header.UnicodeFlag);
  219. byte[] responseData = subcommandResponse.GetData(header.UnicodeFlag);
  220. Transaction2Response response = new Transaction2Response();
  221. PrepareResponse(response, responseSetup, responseParameters, responseData, state.MaxBufferSize, sendQueue);
  222. return response;
  223. }
  224. internal static void PrepareResponse(TransactionResponse response, byte[] responseSetup, byte[] responseParameters, byte[] responseData, int maxBufferSize, List<SMB1Command> sendQueue)
  225. {
  226. int responseSize = TransactionResponse.CalculateMessageSize(responseSetup.Length, responseParameters.Length, responseData.Length);
  227. if (responseSize <= maxBufferSize)
  228. {
  229. response.Setup = responseSetup;
  230. response.TotalParameterCount = (ushort)responseParameters.Length;
  231. response.TotalDataCount = (ushort)responseData.Length;
  232. response.TransParameters = responseParameters;
  233. response.TransData = responseData;
  234. }
  235. else
  236. {
  237. int currentDataLength = maxBufferSize - (responseSize - responseData.Length);
  238. byte[] buffer = new byte[currentDataLength];
  239. Array.Copy(responseData, 0, buffer, 0, currentDataLength);
  240. response.Setup = responseSetup;
  241. response.TotalParameterCount = (ushort)responseParameters.Length;
  242. response.TotalDataCount = (ushort)responseData.Length;
  243. response.TransParameters = responseParameters;
  244. response.TransData = buffer;
  245. int dataBytesLeftToSend = responseData.Length - currentDataLength;
  246. while (dataBytesLeftToSend > 0)
  247. {
  248. TransactionResponse additionalResponse;
  249. if (response is Transaction2Response)
  250. {
  251. additionalResponse = new Transaction2Response();
  252. }
  253. else
  254. {
  255. additionalResponse = new TransactionResponse();
  256. }
  257. currentDataLength = dataBytesLeftToSend;
  258. responseSize = TransactionResponse.CalculateMessageSize(0, 0, dataBytesLeftToSend);
  259. if (responseSize > maxBufferSize)
  260. {
  261. currentDataLength = maxBufferSize - (responseSize - dataBytesLeftToSend);
  262. }
  263. buffer = new byte[currentDataLength];
  264. int dataBytesSent = responseData.Length - dataBytesLeftToSend;
  265. Array.Copy(responseData, dataBytesSent, buffer, 0, currentDataLength);
  266. additionalResponse.TotalParameterCount = (ushort)responseParameters.Length;
  267. additionalResponse.TotalDataCount = (ushort)responseData.Length;
  268. additionalResponse.TransData = buffer;
  269. additionalResponse.ParameterDisplacement = (ushort)response.TransParameters.Length;
  270. additionalResponse.DataDisplacement = (ushort)dataBytesSent;
  271. sendQueue.Add(additionalResponse);
  272. dataBytesLeftToSend -= currentDataLength;
  273. }
  274. }
  275. }
  276. }
  277. }