private SMB2Command ProcessSMB2Command(SMB2Command command, SMB2ConnectionState state) { if (command is SessionSetupRequest) { return(SessionSetupHelper.GetSessionSetupResponse((SessionSetupRequest)command, m_securityProvider, state)); } else if (command is EchoRequest) { return(new EchoResponse()); } else { SMB2Session session = state.GetSession(command.Header.SessionID); if (session == null) { return(new ErrorResponse(command.CommandName, NTStatus.STATUS_USER_SESSION_DELETED)); } if (command is TreeConnectRequest) { return(TreeConnectHelper.GetTreeConnectResponse((TreeConnectRequest)command, state, m_services, m_shares)); } else if (command is LogoffRequest) { state.LogToServer(Severity.Information, "Logoff: User '{0}' logged off. (SessionID: {1})", session.UserName, command.Header.SessionID); m_securityProvider.DeleteSecurityContext(ref session.SecurityContext.AuthenticationContext); state.RemoveSession(command.Header.SessionID); return(new LogoffResponse()); } else if (command.Header.IsAsync) { // TreeID will not be present in an ASYNC header if (command is CancelRequest) { return(CancelHelper.GetCancelResponse((CancelRequest)command, state)); } } else { ISMBShare share = session.GetConnectedTree(command.Header.TreeID); if (share == null) { state.LogToServer(Severity.Verbose, "{0} failed. Invalid TreeID (SessionID: {1}, TreeID: {2}).", command.CommandName, command.Header.SessionID, command.Header.TreeID); return(new ErrorResponse(command.CommandName, NTStatus.STATUS_NETWORK_NAME_DELETED)); } if (command is TreeDisconnectRequest) { return(TreeConnectHelper.GetTreeDisconnectResponse((TreeDisconnectRequest)command, share, state)); } else if (command is CreateRequest) { return(CreateHelper.GetCreateResponse((CreateRequest)command, share, state)); } else if (command is QueryInfoRequest) { return(QueryInfoHelper.GetQueryInfoResponse((QueryInfoRequest)command, share, state)); } else if (command is SetInfoRequest) { return(SetInfoHelper.GetSetInfoResponse((SetInfoRequest)command, share, state)); } else if (command is QueryDirectoryRequest) { return(QueryDirectoryHelper.GetQueryDirectoryResponse((QueryDirectoryRequest)command, share, state)); } else if (command is ReadRequest) { return(ReadWriteResponseHelper.GetReadResponse((ReadRequest)command, share, state)); } else if (command is WriteRequest) { return(ReadWriteResponseHelper.GetWriteResponse((WriteRequest)command, share, state)); } else if (command is LockRequest) { return(LockHelper.GetLockResponse((LockRequest)command, share, state)); } else if (command is FlushRequest) { return(ReadWriteResponseHelper.GetFlushResponse((FlushRequest)command, share, state)); } else if (command is CloseRequest) { return(CloseHelper.GetCloseResponse((CloseRequest)command, share, state)); } else if (command is IOCtlRequest) { return(IOCtlHelper.GetIOCtlResponse((IOCtlRequest)command, share, state)); } else if (command is CancelRequest) { return(CancelHelper.GetCancelResponse((CancelRequest)command, state)); } else if (command is ChangeNotifyRequest) { return(ChangeNotifyHelper.GetChangeNotifyInterimResponse((ChangeNotifyRequest)command, share, state)); } } } return(new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED)); }
private SMB2Command ProcessSMB2Command(SMB2Command command, SMB2ConnectionState state) { if (command is SessionSetupRequest) { return(SessionSetupHelper.GetSessionSetupResponse((SessionSetupRequest)command, m_securityProvider, state)); } else if (command is EchoRequest) { return(new EchoResponse()); } else { SMB2Session session = state.GetSession(command.Header.SessionID); if (session == null) { return(new ErrorResponse(command.CommandName, NTStatus.STATUS_USER_SESSION_DELETED)); } if (command is TreeConnectRequest) { return(TreeConnectHelper.GetTreeConnectResponse((TreeConnectRequest)command, state, m_services, m_shares)); } else if (command is LogoffRequest) { state.LogToServer(Severity.Information, "Logoff: User '{0}' logged off.", session.UserName); m_securityProvider.DeleteSecurityContext(ref session.SecurityContext.AuthenticationContext); state.RemoveSession(command.Header.SessionID); return(new LogoffResponse()); } else { // Cancel requests can have an ASYNC header (TreeID will not be present) 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); } ISMBShare share = session.GetConnectedTree(command.Header.TreeID); if (share == null) { return(new ErrorResponse(command.CommandName, NTStatus.STATUS_NETWORK_NAME_DELETED)); } if (command is TreeDisconnectRequest) { return(TreeConnectHelper.GetTreeDisconnectResponse((TreeDisconnectRequest)command, share, state)); } else if (command is CreateRequest) { return(CreateHelper.GetCreateResponse((CreateRequest)command, share, state)); } else if (command is QueryInfoRequest) { return(QueryInfoHelper.GetQueryInfoResponse((QueryInfoRequest)command, share, state)); } else if (command is SetInfoRequest) { return(SetInfoHelper.GetSetInfoResponse((SetInfoRequest)command, share, state)); } else if (command is QueryDirectoryRequest) { return(QueryDirectoryHelper.GetQueryDirectoryResponse((QueryDirectoryRequest)command, share, state)); } else if (command is ReadRequest) { return(ReadWriteResponseHelper.GetReadResponse((ReadRequest)command, share, state)); } else if (command is WriteRequest) { return(ReadWriteResponseHelper.GetWriteResponse((WriteRequest)command, share, state)); } else if (command is FlushRequest) { FlushRequest request = (FlushRequest)command; OpenFileObject openFile = session.GetOpenFileObject(request.FileId); if (openFile == null) { return(new ErrorResponse(request.CommandName, NTStatus.STATUS_FILE_CLOSED)); } NTStatus status = share.FileStore.FlushFileBuffers(openFile.Handle); if (status != NTStatus.STATUS_SUCCESS) { return(new ErrorResponse(request.CommandName, status)); } return(new FlushResponse()); } else if (command is CloseRequest) { return(CloseHelper.GetCloseResponse((CloseRequest)command, share, state)); } else if (command is IOCtlRequest) { return(IOCtlHelper.GetIOCtlResponse((IOCtlRequest)command, share, 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(new ErrorResponse(command.CommandName, NTStatus.STATUS_NOT_SUPPORTED)); }
private List <SMB1Command> ProcessSMB1Command(SMB1Header header, SMB1Command command, SMB1ConnectionState state) { if (command is SessionSetupAndXRequest) { SessionSetupAndXRequest request = (SessionSetupAndXRequest)command; state.MaxBufferSize = request.MaxBufferSize; return(SessionSetupHelper.GetSessionSetupResponse(header, request, m_securityProvider, state)); } else if (command is SessionSetupAndXRequestExtended) { SessionSetupAndXRequestExtended request = (SessionSetupAndXRequestExtended)command; state.MaxBufferSize = request.MaxBufferSize; return(SessionSetupHelper.GetSessionSetupResponseExtended(header, request, m_securityProvider, state)); } else if (command is EchoRequest) { return(ServerResponseHelper.GetEchoResponse((EchoRequest)command)); } else { SMB1Session session = state.GetSession(header.UID); if (session == null) { header.Status = NTStatus.STATUS_USER_SESSION_DELETED; return(new ErrorResponse(command.CommandName)); } if (command is TreeConnectAndXRequest) { TreeConnectAndXRequest request = (TreeConnectAndXRequest)command; return(TreeConnectHelper.GetTreeConnectResponse(header, request, state, m_services, m_shares)); } else if (command is LogoffAndXRequest) { state.LogToServer(Severity.Information, "Logoff: User '{0}' logged off.", session.UserName); m_securityProvider.DeleteSecurityContext(ref session.SecurityContext.AuthenticationContext); state.RemoveSession(header.UID); return(new LogoffAndXResponse()); } else { ISMBShare share = session.GetConnectedTree(header.TID); if (share == null) { header.Status = NTStatus.STATUS_SMB_BAD_TID; return(new ErrorResponse(command.CommandName)); } if (command is CreateDirectoryRequest) { CreateDirectoryRequest request = (CreateDirectoryRequest)command; return(FileStoreResponseHelper.GetCreateDirectoryResponse(header, request, share, state)); } else if (command is DeleteDirectoryRequest) { DeleteDirectoryRequest request = (DeleteDirectoryRequest)command; return(FileStoreResponseHelper.GetDeleteDirectoryResponse(header, request, share, state)); } else if (command is CloseRequest) { CloseRequest request = (CloseRequest)command; return(ServerResponseHelper.GetCloseResponse(header, request, share, state)); } else if (command is FlushRequest) { return(new FlushResponse()); } else if (command is DeleteRequest) { DeleteRequest request = (DeleteRequest)command; return(FileStoreResponseHelper.GetDeleteResponse(header, request, share, state)); } else if (command is RenameRequest) { RenameRequest request = (RenameRequest)command; return(FileStoreResponseHelper.GetRenameResponse(header, request, share, state)); } else if (command is QueryInformationRequest) { QueryInformationRequest request = (QueryInformationRequest)command; return(FileStoreResponseHelper.GetQueryInformationResponse(header, request, share, state)); } else if (command is SetInformationRequest) { SetInformationRequest request = (SetInformationRequest)command; return(FileStoreResponseHelper.GetSetInformationResponse(header, request, share, state)); } else if (command is ReadRequest) { ReadRequest request = (ReadRequest)command; return(ReadWriteResponseHelper.GetReadResponse(header, request, share, state)); } else if (command is WriteRequest) { WriteRequest request = (WriteRequest)command; return(ReadWriteResponseHelper.GetWriteResponse(header, request, share, state)); } else if (command is CheckDirectoryRequest) { CheckDirectoryRequest request = (CheckDirectoryRequest)command; return(FileStoreResponseHelper.GetCheckDirectoryResponse(header, request, share, state)); } else if (command is WriteRawRequest) { // [MS-CIFS] 3.3.5.26 - Receiving an SMB_COM_WRITE_RAW Request: // the server MUST verify that the Server.Capabilities include CAP_RAW_MODE, // If an error is detected [..] the Write Raw operation MUST fail and // the server MUST return a Final Server Response [..] with the Count field set to zero. return(new WriteRawFinalResponse()); } else if (command is SetInformation2Request) { SetInformation2Request request = (SetInformation2Request)command; return(FileStoreResponseHelper.GetSetInformation2Response(header, request, share, state)); } else if (command is LockingAndXRequest) { header.Status = NTStatus.STATUS_ACCESS_DENIED; return(new ErrorResponse(command.CommandName)); } else if (command is OpenAndXRequest) { OpenAndXRequest request = (OpenAndXRequest)command; return(OpenAndXHelper.GetOpenAndXResponse(header, request, share, state)); } else if (command is ReadAndXRequest) { ReadAndXRequest request = (ReadAndXRequest)command; return(ReadWriteResponseHelper.GetReadResponse(header, request, share, state)); } else if (command is WriteAndXRequest) { WriteAndXRequest request = (WriteAndXRequest)command; return(ReadWriteResponseHelper.GetWriteResponse(header, request, share, state)); } else if (command is FindClose2Request) { return(ServerResponseHelper.GetFindClose2Request(header, (FindClose2Request)command, state)); } else if (command is TreeDisconnectRequest) { TreeDisconnectRequest request = (TreeDisconnectRequest)command; return(TreeConnectHelper.GetTreeDisconnectResponse(header, request, share, state)); } else if (command is TransactionRequest) // Both TransactionRequest and Transaction2Request { TransactionRequest request = (TransactionRequest)command; return(TransactionHelper.GetTransactionResponse(header, request, share, state)); } else if (command is TransactionSecondaryRequest) // Both TransactionSecondaryRequest and Transaction2SecondaryRequest { TransactionSecondaryRequest request = (TransactionSecondaryRequest)command; return(TransactionHelper.GetTransactionResponse(header, request, share, state)); } else if (command is NTTransactRequest) { NTTransactRequest request = (NTTransactRequest)command; return(NTTransactHelper.GetNTTransactResponse(header, request, share, state)); } else if (command is NTTransactSecondaryRequest) { NTTransactSecondaryRequest request = (NTTransactSecondaryRequest)command; return(NTTransactHelper.GetNTTransactResponse(header, request, share, state)); } else if (command is NTCreateAndXRequest) { NTCreateAndXRequest request = (NTCreateAndXRequest)command; return(NTCreateHelper.GetNTCreateResponse(header, request, share, state)); } } } header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); }
/// <summary> /// May return null /// </summary> public SMBCommand ProcessCommand(SMBHeader header, SMBCommand command, StateObject state, List <SMBCommand> sendQueue) { if (command is NegotiateRequest) { NegotiateRequest request = (NegotiateRequest)command; if (request.Dialects.Contains(SMBServer.NTLanManagerDialect)) { if (EnableExtendedSecurity && header.ExtendedSecurityFlag) { return(NegotiateHelper.GetNegotiateResponseExtended(request, m_serverGuid)); } else { return(new NegotiateResponseNotSupported()); } } else { return(new NegotiateResponseNotSupported()); } } else if (command is SessionSetupAndXRequest) { SessionSetupAndXRequest request = (SessionSetupAndXRequest)command; state.MaxBufferSize = request.MaxBufferSize; //this probably won't work return(NegotiateHelper.GetSessionSetupResponse(header, request, state)); } else if (command is SessionSetupAndXRequestExtended) { SessionSetupAndXRequestExtended request = (SessionSetupAndXRequestExtended)command; state.MaxBufferSize = request.MaxBufferSize; return(NegotiateHelper.GetSessionSetupResponseExtended(header, request, state)); } else if (command is EchoRequest) { return(ServerResponseHelper.GetEchoResponse((EchoRequest)command, sendQueue)); } else if (state.IsAuthenticated(header.UID)) { if (command is TreeConnectAndXRequest) { TreeConnectAndXRequest request = (TreeConnectAndXRequest)command; return(TreeConnectHelper.GetTreeConnectResponse(header, request, state, m_shares)); } else if (command is LogoffAndXRequest) { return(new LogoffAndXResponse()); } else if (state.IsTreeConnected(header.TID)) { string rootPath = state.GetConnectedTreePath(header.TID); object share; if (state.IsIPC(header.TID)) { share = m_services; } else { share = m_shares.GetShareFromRelativePath(rootPath); } if (command is CreateDirectoryRequest) { if (!(share is FileSystemShare)) { header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); } CreateDirectoryRequest request = (CreateDirectoryRequest)command; return(FileSystemResponseHelper.GetCreateDirectoryResponse(header, request, (FileSystemShare)share, state)); } else if (command is DeleteDirectoryRequest) { if (!(share is FileSystemShare)) { header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); } DeleteDirectoryRequest request = (DeleteDirectoryRequest)command; return(FileSystemResponseHelper.GetDeleteDirectoryResponse(header, request, (FileSystemShare)share, state)); } else if (command is CloseRequest) { CloseRequest request = (CloseRequest)command; return(ServerResponseHelper.GetCloseResponse(header, request, state)); } else if (command is FlushRequest) { return(new FlushResponse()); } else if (command is DeleteRequest) { if (!(share is FileSystemShare)) { header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); } DeleteRequest request = (DeleteRequest)command; return(FileSystemResponseHelper.GetDeleteResponse(header, request, (FileSystemShare)share, state)); } else if (command is RenameRequest) { if (!(share is FileSystemShare)) { header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); } RenameRequest request = (RenameRequest)command; return(FileSystemResponseHelper.GetRenameResponse(header, request, (FileSystemShare)share, state)); } else if (command is QueryInformationRequest) { if (!(share is FileSystemShare)) { header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); } QueryInformationRequest request = (QueryInformationRequest)command; return(FileSystemResponseHelper.GetQueryInformationResponse(header, request, (FileSystemShare)share)); } else if (command is SetInformationRequest) { if (!(share is FileSystemShare)) { header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); } SetInformationRequest request = (SetInformationRequest)command; return(FileSystemResponseHelper.GetSetInformationResponse(header, request, (FileSystemShare)share, state)); } else if (command is ReadRequest) { ReadRequest request = (ReadRequest)command; return(ReadWriteResponseHelper.GetReadResponse(header, request, share, state)); } else if (command is WriteRequest) { string userName = state.GetConnectedUserName(header.UID); if (share is FileSystemShare && !((FileSystemShare)share).HasWriteAccess(userName)) { header.Status = NTStatus.STATUS_ACCESS_DENIED; return(new ErrorResponse(command.CommandName)); } WriteRequest request = (WriteRequest)command; return(ReadWriteResponseHelper.GetWriteResponse(header, request, share, state)); } else if (command is CheckDirectoryRequest) { if (!(share is FileSystemShare)) { header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); } CheckDirectoryRequest request = (CheckDirectoryRequest)command; return(FileSystemResponseHelper.GetCheckDirectoryResponse(header, request, (FileSystemShare)share)); } else if (command is WriteRawRequest) { // [MS-CIFS] 3.3.5.26 - Receiving an SMB_COM_WRITE_RAW Request: // the server MUST verify that the Server.Capabilities include CAP_RAW_MODE, // If an error is detected [..] the Write Raw operation MUST fail and // the server MUST return a Final Server Response [..] with the Count field set to zero. return(new WriteRawFinalResponse()); } else if (command is SetInformation2Request) { if (!(share is FileSystemShare)) { header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); } SetInformation2Request request = (SetInformation2Request)command; return(FileSystemResponseHelper.GetSetInformation2Response(header, request, (FileSystemShare)share, state)); } else if (command is LockingAndXRequest) { header.Status = NTStatus.STATUS_ACCESS_DENIED; return(new ErrorResponse(CommandName.SMB_COM_LOCKING_ANDX)); } else if (command is OpenAndXRequest) { OpenAndXRequest request = (OpenAndXRequest)command; return(OpenAndXHelper.GetOpenAndXResponse(header, request, share, state)); } else if (command is ReadAndXRequest) { ReadAndXRequest request = (ReadAndXRequest)command; return(ReadWriteResponseHelper.GetReadResponse(header, request, share, state)); } else if (command is WriteAndXRequest) { string userName = state.GetConnectedUserName(header.UID); if (share is FileSystemShare && !((FileSystemShare)share).HasWriteAccess(userName)) { header.Status = NTStatus.STATUS_ACCESS_DENIED; return(new ErrorResponse(command.CommandName)); } WriteAndXRequest request = (WriteAndXRequest)command; return(ReadWriteResponseHelper.GetWriteResponse(header, request, share, state)); } else if (command is FindClose2Request) { return(ServerResponseHelper.GetFindClose2Request(header, (FindClose2Request)command, state)); } else if (command is TreeDisconnectRequest) { TreeDisconnectRequest request = (TreeDisconnectRequest)command; return(TreeConnectHelper.GetTreeDisconnectResponse(header, request, state)); } else if (command is TransactionRequest) // Both TransactionRequest and Transaction2Request { TransactionRequest request = (TransactionRequest)command; try { return(TransactionHelper.GetTransactionResponse(header, request, share, state, sendQueue)); } catch (UnsupportedInformationLevelException) { header.Status = NTStatus.STATUS_INVALID_PARAMETER; return(new ErrorResponse(command.CommandName)); } } else if (command is TransactionSecondaryRequest) // Both TransactionSecondaryRequest and Transaction2SecondaryRequest { TransactionSecondaryRequest request = (TransactionSecondaryRequest)command; try { return(TransactionHelper.GetTransactionResponse(header, request, share, state, sendQueue)); } catch (UnsupportedInformationLevelException) { header.Status = NTStatus.STATUS_INVALID_PARAMETER; return(new ErrorResponse(command.CommandName)); } } else if (command is NTTransactRequest) { NTTransactRequest request = (NTTransactRequest)command; return(NTTransactHelper.GetNTTransactResponse(header, request, share, state, sendQueue)); } else if (command is NTTransactSecondaryRequest) { NTTransactSecondaryRequest request = (NTTransactSecondaryRequest)command; return(NTTransactHelper.GetNTTransactResponse(header, request, share, state, sendQueue)); } else if (command is NTCreateAndXRequest) { NTCreateAndXRequest request = (NTCreateAndXRequest)command; return(NTCreateHelper.GetNTCreateResponse(header, request, share, state)); } } else { header.Status = NTStatus.STATUS_SMB_BAD_TID; return(new ErrorResponse(command.CommandName)); } } header.Status = NTStatus.STATUS_SMB_BAD_COMMAND; return(new ErrorResponse(command.CommandName)); }