ChangeNotifyHelper.cs 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 ChangeNotifyHelper
  14. {
  15. /// <remarks>
  16. /// 'NoRemoteChangeNotify' can be set in the registry to prevent the client from sending ChangeNotify requests altogether.
  17. /// </remarks>
  18. internal static SMB2Command GetChangeNotifyInterimResponse(ChangeNotifyRequest request, ISMBShare share, SMB2ConnectionState state)
  19. {
  20. SMB2Session session = state.GetSession(request.Header.SessionID);
  21. OpenFileObject openFile = session.GetOpenFileObject(request.FileId);
  22. bool watchTree = (request.Flags & ChangeNotifyFlags.WatchTree) > 0;
  23. SMB2AsyncContext asyncContext = state.CreateAsyncContext(request.FileId, state, request.Header.SessionID, request.Header.TreeID);
  24. // We have to make sure that we don't send an interim response after the final response.
  25. lock (asyncContext)
  26. {
  27. NTStatus status = share.FileStore.NotifyChange(out asyncContext.IORequest, openFile.Handle, request.CompletionFilter, watchTree, (int)request.OutputBufferLength, OnNotifyChangeCompleted, asyncContext);
  28. if (status == NTStatus.STATUS_PENDING)
  29. {
  30. state.LogToServer(Severity.Verbose, "NotifyChange: Monitoring of '{0}{1}' started. AsyncID: {2}.", share.Name, openFile.Path, asyncContext.AsyncID);
  31. }
  32. // [MS-SMB2] If the underlying object store does not support change notifications, the server MUST fail this request with STATUS_NOT_SUPPORTED
  33. ErrorResponse response = new ErrorResponse(request.CommandName, status);
  34. // Windows 7 / 8 / 10 will infinitely retry sending ChangeNotify requests if the response does not have SMB2_FLAGS_ASYNC_COMMAND set.
  35. response.Header.IsAsync = true;
  36. response.Header.AsyncID = asyncContext.AsyncID;
  37. return response;
  38. }
  39. }
  40. private static void OnNotifyChangeCompleted(NTStatus status, byte[] buffer, object context)
  41. {
  42. SMB2AsyncContext asyncContext = (SMB2AsyncContext)context;
  43. // Wait until the interim response has been sent
  44. lock (asyncContext)
  45. {
  46. SMB2ConnectionState connection = asyncContext.Connection;
  47. connection.RemoveAsyncContext(asyncContext);
  48. SMB2Session session = connection.GetSession(asyncContext.SessionID);
  49. if (session != null)
  50. {
  51. OpenFileObject openFile = session.GetOpenFileObject(asyncContext.FileID);
  52. if (openFile != null)
  53. {
  54. connection.LogToServer(Severity.Verbose, "NotifyChange: Monitoring of '{0}{1}' completed. NTStatus: {2}. AsyncID: {3}", openFile.ShareName, openFile.Path, status, asyncContext.AsyncID);
  55. }
  56. if (status == NTStatus.STATUS_SUCCESS ||
  57. status == NTStatus.STATUS_NOTIFY_CLEANUP ||
  58. status == NTStatus.STATUS_NOTIFY_ENUM_DIR)
  59. {
  60. ChangeNotifyResponse response = new ChangeNotifyResponse();
  61. response.Header.Status = status;
  62. response.Header.IsAsync = true;
  63. response.Header.IsSigned = session.SigningRequired;
  64. response.Header.AsyncID = asyncContext.AsyncID;
  65. response.Header.SessionID = asyncContext.SessionID;
  66. response.OutputBuffer = buffer;
  67. SMBServer.EnqueueResponse(connection, response);
  68. }
  69. else
  70. {
  71. // [MS-SMB2] If the object store returns an error, the server MUST fail the request with the error code received.
  72. ErrorResponse response = new ErrorResponse(SMB2CommandName.ChangeNotify);
  73. response.Header.Status = status;
  74. response.Header.IsAsync = true;
  75. response.Header.IsSigned = session.SigningRequired;
  76. response.Header.AsyncID = asyncContext.AsyncID;
  77. SMBServer.EnqueueResponse(connection, response);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }