ChangeNotifyHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. else if (status == NTStatus.STATUS_NOT_SUPPORTED)
  33. {
  34. // [MS-SMB2] If the underlying object store does not support change notifications, the server MUST fail this request with STATUS_NOT_SUPPORTED.
  35. // Unfortunately, Windows 7 / 8 / 10 will immediately retry sending another ChangeNotify request upon getting STATUS_NOT_SUPPORTED,
  36. // To prevent flooding, we must return a valid interim response (Status set to STATUS_PENDING and SMB2_FLAGS_ASYNC_COMMAND bit is set in Flags).
  37. status = NTStatus.STATUS_PENDING;
  38. }
  39. else
  40. {
  41. state.RemoveAsyncContext(asyncContext);
  42. }
  43. ErrorResponse response = new ErrorResponse(request.CommandName, status);
  44. if (status == NTStatus.STATUS_PENDING)
  45. {
  46. response.Header.IsAsync = true;
  47. response.Header.AsyncID = asyncContext.AsyncID;
  48. }
  49. return response;
  50. }
  51. }
  52. private static void OnNotifyChangeCompleted(NTStatus status, byte[] buffer, object context)
  53. {
  54. SMB2AsyncContext asyncContext = (SMB2AsyncContext)context;
  55. // Wait until the interim response has been sent
  56. lock (asyncContext)
  57. {
  58. SMB2ConnectionState connection = asyncContext.Connection;
  59. connection.RemoveAsyncContext(asyncContext);
  60. SMB2Session session = connection.GetSession(asyncContext.SessionID);
  61. if (session != null)
  62. {
  63. OpenFileObject openFile = session.GetOpenFileObject(asyncContext.FileID);
  64. if (openFile != null)
  65. {
  66. connection.LogToServer(Severity.Verbose, "NotifyChange: Monitoring of '{0}{1}' completed. NTStatus: {2}. AsyncID: {3}", openFile.ShareName, openFile.Path, status, asyncContext.AsyncID);
  67. }
  68. if (status == NTStatus.STATUS_SUCCESS ||
  69. status == NTStatus.STATUS_NOTIFY_CLEANUP ||
  70. status == NTStatus.STATUS_NOTIFY_ENUM_DIR)
  71. {
  72. ChangeNotifyResponse response = new ChangeNotifyResponse();
  73. response.Header.Status = status;
  74. response.Header.IsAsync = true;
  75. response.Header.IsSigned = session.SigningRequired;
  76. response.Header.AsyncID = asyncContext.AsyncID;
  77. response.Header.SessionID = asyncContext.SessionID;
  78. response.OutputBuffer = buffer;
  79. SMBServer.EnqueueResponse(connection, response);
  80. }
  81. else
  82. {
  83. // [MS-SMB2] If the object store returns an error, the server MUST fail the request with the error code received.
  84. ErrorResponse response = new ErrorResponse(SMB2CommandName.ChangeNotify);
  85. response.Header.Status = status;
  86. response.Header.IsAsync = true;
  87. response.Header.IsSigned = session.SigningRequired;
  88. response.Header.AsyncID = asyncContext.AsyncID;
  89. SMBServer.EnqueueResponse(connection, response);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }