Minor code refactoring, do not try to process async commands other than CANCEL

This commit is contained in:
Tal Aloni 2017-07-25 16:53:42 +03:00
parent ca48ffd92f
commit 6507d88bf5
8 changed files with 126 additions and 21 deletions

View file

@ -200,8 +200,10 @@
<Compile Include="Server\Shares\ISMBShare.cs" />
<Compile Include="Server\Shares\NamedPipeShare.cs" />
<Compile Include="Server\Shares\SMBShareCollection.cs" />
<Compile Include="Server\SMB1\CancelHelper.cs" />
<Compile Include="Server\SMB1\FileStoreResponseHelper.cs" />
<Compile Include="Server\SMB1\NegotiateHelper.cs" />
<Compile Include="Server\SMB1\NotifyChangeHelper.cs" />
<Compile Include="Server\SMB1\NTCreateHelper.cs" />
<Compile Include="Server\SMB1\NTTransactHelper.cs" />
<Compile Include="Server\SMB1\OpenAndXHelper.cs" />
@ -217,6 +219,8 @@
<Compile Include="Server\SMB1\TransactionHelper.cs" />
<Compile Include="Server\SMB1\TransactionSubcommandHelper.cs" />
<Compile Include="Server\SMB1\TreeConnectHelper.cs" />
<Compile Include="Server\SMB2\CancelHelper.cs" />
<Compile Include="Server\SMB2\ChangeNotifyHelper.cs" />
<Compile Include="Server\SMB2\CloseHelper.cs" />
<Compile Include="Server\SMB2\CreateHelper.cs" />
<Compile Include="Server\SMB2\IOCtlHelper.cs" />

View file

@ -0,0 +1,20 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using SMBLibrary.SMB1;
using Utilities;
namespace SMBLibrary.Server.SMB1
{
internal class CancelHelper
{
internal static void ProcessNTCancelRequest(SMB1Header header, NTCancelRequest request, ISMBShare share, SMB1ConnectionState state)
{
}
}
}

View file

@ -102,9 +102,11 @@ namespace SMBLibrary.Server.SMB1
}
else if (subcommand is NTTransactNotifyChangeRequest)
{
// [MS-CIFS] If the server does not support the NT_TRANSACT_NOTIFY_CHANGE subcommand, it can return an
// error response with STATUS_NOT_IMPLEMENTED [..] in response to an NT_TRANSACT_NOTIFY_CHANGE Request.
header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
NotifyChangeHelper.ProcessNTTransactNotifyChangeRequest(header, maxParameterCount, (NTTransactNotifyChangeRequest)subcommand, share, state);
if (header.Status == NTStatus.STATUS_PENDING)
{
return new List<SMB1Command>();
}
}
else if (subcommand is NTTransactQuerySecurityDescriptorRequest)
{

View file

@ -0,0 +1,23 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using SMBLibrary.SMB1;
using Utilities;
namespace SMBLibrary.Server.SMB1
{
internal class NotifyChangeHelper
{
internal static void ProcessNTTransactNotifyChangeRequest(SMB1Header header, uint maxParameterCount, NTTransactNotifyChangeRequest subcommand, ISMBShare share, SMB1ConnectionState state)
{
// [MS-CIFS] If the server does not support the NT_TRANSACT_NOTIFY_CHANGE subcommand, it can return an
// error response with STATUS_NOT_IMPLEMENTED [..] in response to an NT_TRANSACT_NOTIFY_CHANGE Request.
header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
}
}
}

View file

@ -0,0 +1,30 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using SMBLibrary.SMB2;
using Utilities;
namespace SMBLibrary.Server.SMB2
{
internal class CancelHelper
{
internal static SMB2Command GetCancelResponse(CancelRequest request, SMB2ConnectionState state)
{
if (request.Header.IsAsync && request.Header.AsyncID == 0)
{
ErrorResponse response = new ErrorResponse(request.CommandName, NTStatus.STATUS_CANCELLED);
response.Header.IsAsync = true;
return response;
}
// [MS-SMB2] If a request is not found [..] no response is sent.
// [MS-SMB2] If the target request is not successfully canceled [..] no response is sent.
return null;
}
}
}

View file

@ -0,0 +1,26 @@
/* Copyright (C) 2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*/
using System;
using System.Collections.Generic;
using SMBLibrary.SMB2;
using Utilities;
namespace SMBLibrary.Server.SMB2
{
internal class ChangeNotifyHelper
{
internal static SMB2Command GetChangeNotifyInterimResponse(ChangeNotifyRequest request, ISMBShare share, SMB2ConnectionState state)
{
// [MS-SMB2] If the underlying object store does not support change notifications, the server MUST fail this request with STATUS_NOT_SUPPORTED
ErrorResponse response = new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
// Windows 7 / 8 / 10 will infinitely retry sending ChangeNotify requests if the response does not have SMB2_FLAGS_ASYNC_COMMAND set.
// Note: NoRemoteChangeNotify can be set in the registry to prevent the client from sending ChangeNotify requests altogether.
response.Header.IsAsync = true;
return response;
}
}
}

View file

@ -281,6 +281,13 @@ namespace SMBLibrary.Server
NTCreateAndXRequest request = (NTCreateAndXRequest)command;
return NTCreateHelper.GetNTCreateResponse(header, request, share, state);
}
else if (command is NTCancelRequest)
{
NTCancelRequest request = (NTCancelRequest)command;
CancelHelper.ProcessNTCancelRequest(header, request, share, state);
// [MS-CIFS] The SMB_COM_NT_CANCEL command MUST NOT send a response.
return new List<SMB1Command>();
}
}
}

View file

@ -113,22 +113,16 @@ namespace SMBLibrary.Server
state.RemoveSession(command.Header.SessionID);
return new LogoffResponse();
}
else
else if (command.Header.IsAsync)
{
// Cancel requests can have an ASYNC header (TreeID will not be present)
// TreeID will not be present in an ASYNC header
if (command is CancelRequest)
{
if (command.Header.IsAsync && command.Header.AsyncID == 0)
{
ErrorResponse response = new ErrorResponse(command.CommandName, NTStatus.STATUS_CANCELLED);
response.Header.IsAsync = true;
return response;
}
// [MS-SMB2] If a request is not found, the server MUST stop processing for this cancel request. No response is sent.
return null;
return CancelHelper.GetCancelResponse((CancelRequest)command, state);
}
}
else
{
ISMBShare share = session.GetConnectedTree(command.Header.TreeID);
if (share == null)
{
@ -175,14 +169,13 @@ namespace SMBLibrary.Server
{
return IOCtlHelper.GetIOCtlResponse((IOCtlRequest)command, share, state);
}
else if (command is CancelRequest)
{
return CancelHelper.GetCancelResponse((CancelRequest)command, state);
}
else if (command is ChangeNotifyRequest)
{
// [MS-SMB2] If the underlying object store does not support change notifications, the server MUST fail this request with STATUS_NOT_SUPPORTED
ErrorResponse response = new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
// Windows 7 / 8 / 10 will infinitely retry sending ChangeNotify requests if the response does not have SMB2_FLAGS_ASYNC_COMMAND set.
// Note: NoRemoteChangeNotify can be set in the registry to prevent the client from sending ChangeNotify requests altogether.
response.Header.IsAsync = true;
return response;
return ChangeNotifyHelper.GetChangeNotifyInterimResponse((ChangeNotifyRequest)command, share, state);
}
}
}