CancelHelper.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright (C) 2017-2019 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 SMBLibrary.SMB2;
  10. using Utilities;
  11. namespace SMBLibrary.Server.SMB2
  12. {
  13. internal class CancelHelper
  14. {
  15. internal static SMB2Command GetCancelResponse(CancelRequest request, SMB2ConnectionState state)
  16. {
  17. SMB2Session session = state.GetSession(request.Header.SessionID);
  18. if (request.Header.IsAsync)
  19. {
  20. SMB2AsyncContext context = state.GetAsyncContext(request.Header.AsyncID);
  21. if (context != null)
  22. {
  23. ISMBShare share = session.GetConnectedTree(context.TreeID);
  24. OpenFileObject openFile = session.GetOpenFileObject(context.FileID);
  25. NTStatus status = share.FileStore.Cancel(context.IORequest);
  26. if (openFile != null)
  27. {
  28. state.LogToServer(Severity.Information, "Cancel: Requested cancel on '{0}{1}'. NTStatus: {2}, AsyncID: {3}.", share.Name, openFile.Path, status, context.AsyncID);
  29. }
  30. if (status == NTStatus.STATUS_SUCCESS ||
  31. status == NTStatus.STATUS_CANCELLED ||
  32. status == NTStatus.STATUS_NOT_SUPPORTED) // See ChangeNotifyHelper.cs
  33. {
  34. state.RemoveAsyncContext(context);
  35. // [MS-SMB2] If the target request is successfully canceled, the target request MUST be failed by sending
  36. // an ERROR response packet [..] with the status field of the SMB2 header set to STATUS_CANCELLED.
  37. ErrorResponse response = new ErrorResponse(request.CommandName, NTStatus.STATUS_CANCELLED);
  38. response.Header.IsAsync = true;
  39. response.Header.AsyncID = context.AsyncID;
  40. return response;
  41. }
  42. // [MS-SMB2] If the target request is not successfully canceled [..] no response is sent.
  43. // Note: Failing to respond might cause the client to disconnect the connection as per [MS-SMB2] 3.2.6.1 Request Expiration Timer Event
  44. return null;
  45. }
  46. else
  47. {
  48. // [MS-SMB2] If a request is not found [..] no response is sent.
  49. return null;
  50. }
  51. }
  52. else
  53. {
  54. // [MS-SMB2] the SMB2 CANCEL Request MUST use an ASYNC header for canceling requests that have received an interim response.
  55. // [MS-SMB2] If the target request is not successfully canceled [..] no response is sent.
  56. return null;
  57. }
  58. }
  59. }
  60. }