Example #1
0
NTSTATUS
SrvProcessGetInfo_SMB_V2(
    PSRV_EXEC_CONTEXT pExecContext
)
{
    NTSTATUS                   ntStatus      = STATUS_SUCCESS;
    PLWIO_SRV_CONNECTION       pConnection   = pExecContext->pConnection;
    PSRV_PROTOCOL_EXEC_CONTEXT pCtxProtocol  = pExecContext->pProtocolContext;
    PSRV_EXEC_CONTEXT_SMB_V2   pCtxSmb2      = pCtxProtocol->pSmb2Context;
    PSRV_GET_INFO_STATE_SMB_V2 pGetInfoState = NULL;
    PLWIO_SRV_SESSION_2        pSession      = NULL;
    PLWIO_SRV_TREE_2           pTree         = NULL;
    PLWIO_SRV_FILE_2           pFile         = NULL;
    BOOLEAN                    bInLock       = FALSE;

    pGetInfoState = (PSRV_GET_INFO_STATE_SMB_V2)pCtxSmb2->hState;
    if (pGetInfoState)
    {
        InterlockedIncrement(&pGetInfoState->refCount);
    }
    else
    {
        ULONG                      iMsg          = pCtxSmb2->iMsg;
        PSRV_MESSAGE_SMB_V2        pSmbRequest   = &pCtxSmb2->pRequests[iMsg];
        PSMB2_GET_INFO_REQUEST_HEADER pRequestHeader = NULL; // Do not free
        PBYTE                      pInputBuffer  = NULL;
        ULONG                      ulInputBufferLength = 0;

        ntStatus = SrvConnection2FindSession_SMB_V2(
                       pCtxSmb2,
                       pConnection,
                       pSmbRequest->pHeader->ullSessionId,
                       &pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvSetStatSession2Info(pExecContext, pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvSession2FindTree_SMB_V2(
                       pCtxSmb2,
                       pSession,
                       pSmbRequest->pHeader->ulTid,
                       &pTree);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SMB2UnmarshalGetInfoRequest(
                       pSmbRequest,
                       &pRequestHeader,
                       &pInputBuffer,
                       &ulInputBufferLength);
        BAIL_ON_NT_STATUS(ntStatus);

        SRV_LOG_DEBUG(
            pExecContext->pLogContext,
            SMB_PROTOCOL_VERSION_2,
            pSmbRequest->pHeader->command,
            "Get Info request params: "
            "command(%u),uid(%llu),cmd-seq(%llu),pid(%u),tid(%u),"
            "credits(%u),flags(0x%x),chain-offset(%u),"
            "file-id(persistent:0x%x,volatile:0x%x),"
            "info-class(0x%x),info-type(0x%x),flags(0x%x),"
            "input-buffer-length(%u),input-buffer-offset(%u),"
            "output-buffer-length(%u),additional-info(%u)",
            pSmbRequest->pHeader->command,
            (long long)pSmbRequest->pHeader->ullSessionId,
            (long long)pSmbRequest->pHeader->ullCommandSequence,
            pSmbRequest->pHeader->ulPid,
            pSmbRequest->pHeader->ulTid,
            pSmbRequest->pHeader->usCredits,
            pSmbRequest->pHeader->ulFlags,
            pSmbRequest->pHeader->ulChainOffset,
            (long long)pRequestHeader->fid.ullPersistentId,
            (long long)pRequestHeader->fid.ullVolatileId,
            pRequestHeader->ucInfoClass,
            pRequestHeader->ucInfoType,
            pRequestHeader->ulFlags,
            pRequestHeader->ulInputBufferLen,
            pRequestHeader->usInputBufferOffset,
            pRequestHeader->ulOutputBufferLen,
            pRequestHeader->ulAdditionalInfo);

        ntStatus = SrvTree2FindFile_SMB_V2(
                       pCtxSmb2,
                       pTree,
                       &pRequestHeader->fid,
                       &pFile);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvBuildGetInfoState_SMB_V2(
                       pRequestHeader,
                       pFile,
                       pInputBuffer,
                       ulInputBufferLength,
                       &pGetInfoState);
        BAIL_ON_NT_STATUS(ntStatus);

        pCtxSmb2->hState = pGetInfoState;
        InterlockedIncrement(&pGetInfoState->refCount);
        pCtxSmb2->pfnStateRelease = &SrvReleaseGetInfoStateHandle_SMB_V2;
    }

    LWIO_LOCK_MUTEX(bInLock, &pGetInfoState->mutex);

    switch (pGetInfoState->stage)
    {
    case SRV_GET_INFO_STAGE_SMB_V2_INITIAL:

        pGetInfoState->stage = SRV_GET_INFO_STAGE_SMB_V2_ATTEMPT_IO;

    // Intentional fall through

    case SRV_GET_INFO_STAGE_SMB_V2_ATTEMPT_IO:

        ntStatus = SrvQueryInfo_SMB_V2(pExecContext);
        BAIL_ON_NT_STATUS(ntStatus);

        pGetInfoState->stage = SRV_GET_INFO_STAGE_SMB_V2_BUILD_RESPONSE;

    // Intentional fall through

    case SRV_GET_INFO_STAGE_SMB_V2_BUILD_RESPONSE:

        ntStatus = SrvBuildGetInfoResponse_SMB_V2(pExecContext);
        BAIL_ON_NT_STATUS(ntStatus);

        pGetInfoState->stage = SRV_GET_INFO_STAGE_SMB_V2_DONE;

    // Intentional fall through

    case SRV_GET_INFO_STAGE_SMB_V2_DONE:

        break;
    }

cleanup:

    if (pFile)
    {
        SrvFile2Release(pFile);
    }

    if (pTree)
    {
        SrvTree2Release(pTree);
    }

    if (pSession)
    {
        SrvSession2Release(pSession);
    }

    if (pGetInfoState)
    {
        LWIO_UNLOCK_MUTEX(bInLock, &pGetInfoState->mutex);

        SrvReleaseGetInfoState_SMB_V2(pGetInfoState);
    }

    return ntStatus;

error:

    switch (ntStatus)
    {
    case STATUS_PENDING:

        // TODO: Add an indicator to the file object to trigger a
        //       cleanup if the connection gets closed and all the
        //       files involved have to be closed

        break;

    default:

        if (pGetInfoState)
        {
            SrvReleaseGetInfoStateAsync_SMB_V2(pGetInfoState);
        }

        break;
    }

    goto cleanup;
}
Example #2
0
NTSTATUS
SrvProcessFlush_SMB_V2(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                   ntStatus     = STATUS_SUCCESS;
    PLWIO_SRV_CONNECTION       pConnection  = pExecContext->pConnection;
    PSRV_PROTOCOL_EXEC_CONTEXT pCtxProtocol = pExecContext->pProtocolContext;
    PSRV_EXEC_CONTEXT_SMB_V2   pCtxSmb2     = pCtxProtocol->pSmb2Context;
    PSRV_FLUSH_STATE_SMB_V2    pFlushState  = NULL;
    PLWIO_SRV_SESSION_2        pSession     = NULL;
    PLWIO_SRV_TREE_2           pTree        = NULL;
    PLWIO_SRV_FILE_2           pFile        = NULL;
    BOOLEAN                    bInLock      = FALSE;

    pFlushState = (PSRV_FLUSH_STATE_SMB_V2)pCtxSmb2->hState;

    if (pFlushState)
    {
        InterlockedIncrement(&pFlushState->refCount);
    }
    else
    {
        ULONG               iMsg          = pCtxSmb2->iMsg;
        PSRV_MESSAGE_SMB_V2 pSmbRequest   = &pCtxSmb2->pRequests[iMsg];
        PSMB2_FID           pFid = NULL; // Do not free

        ntStatus = SrvConnection2FindSession_SMB_V2(
                        pCtxSmb2,
                        pConnection,
                        pSmbRequest->pHeader->ullSessionId,
                        &pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvSetStatSession2Info(pExecContext, pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvSession2FindTree_SMB_V2(
                        pCtxSmb2,
                        pSession,
                        pSmbRequest->pHeader->ulTid,
                        &pTree);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SMB2UnmarshalFlushRequest(pSmbRequest, &pFid);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvTree2FindFile_SMB_V2(
                        pCtxSmb2,
                        pTree,
                        pFid,
                        LwIsSetFlag(
                            pSmbRequest->pHeader->ulFlags,
                            SMB2_FLAGS_RELATED_OPERATION),
                        &pFile);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvBuildFlushState_SMB_V2(
                        pFid,
                        pFile,
                        &pFlushState);
        BAIL_ON_NT_STATUS(ntStatus);

        pCtxSmb2->hState = pFlushState;
        InterlockedIncrement(&pFlushState->refCount);
        pCtxSmb2->pfnStateRelease = &SrvReleaseFlushStateHandle_SMB_V2;
    }

    LWIO_LOCK_MUTEX(bInLock, &pFlushState->mutex);

    switch (pFlushState->stage)
    {
        case SRV_FLUSH_STAGE_SMB_V2_INITIAL:

            pFlushState->stage = SRV_FLUSH_STAGE_SMB_V2_FLUSH_COMPLETED;

            SrvPrepareFlushStateAsync_SMB_V2(pFlushState, pExecContext);

            ntStatus = IoFlushBuffersFile(
                            pFlushState->pFile->hFile,
                            pFlushState->pAcb,
                            &pFlushState->ioStatusBlock);
            BAIL_ON_NT_STATUS(ntStatus);

            SrvReleaseFlushStateAsync_SMB_V2(pFlushState); // completed synchronously

            // intentional fall through

        case SRV_FLUSH_STAGE_SMB_V2_FLUSH_COMPLETED:

            ntStatus = pFlushState->ioStatusBlock.Status;
            BAIL_ON_NT_STATUS(ntStatus);

            pFlushState->stage = SRV_FLUSH_STAGE_SMB_V2_BUILD_RESPONSE;

            // intentional fall through

        case SRV_FLUSH_STAGE_SMB_V2_BUILD_RESPONSE:

            ntStatus = SrvBuildFlushResponse_SMB_V2(pExecContext);
            BAIL_ON_NT_STATUS(ntStatus);

            pFlushState->stage = SRV_FLUSH_STAGE_SMB_V2_DONE;

            // intentional fall through

        case SRV_FLUSH_STAGE_SMB_V2_DONE:

            break;
    }

cleanup:

    if (pFile)
    {
        SrvFile2Release(pFile);
    }

    if (pTree)
    {
        SrvTree2Release(pTree);
    }

    if (pSession)
    {
        SrvSession2Release(pSession);
    }

    if (pFlushState)
    {
        LWIO_UNLOCK_MUTEX(bInLock, &pFlushState->mutex);

        SrvReleaseFlushState_SMB_V2(pFlushState);
    }

    return ntStatus;

error:

    switch (ntStatus)
    {
        case STATUS_PENDING:

            // TODO: Add an indicator to the file object to trigger a
            //       cleanup if the connection gets closed and all the
            //       files involved have to be closed

            break;

        default:

            if (pFlushState)
            {
                SrvReleaseFlushStateAsync_SMB_V2(pFlushState);
            }

            break;
    }

    goto cleanup;
}
Example #3
0
NTSTATUS
SrvProcessNotify_SMB_V2(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                   ntStatus     = STATUS_SUCCESS;
    PLWIO_SRV_CONNECTION       pConnection  = pExecContext->pConnection;
    PSRV_PROTOCOL_EXEC_CONTEXT pCtxProtocol = pExecContext->pProtocolContext;
    PSRV_EXEC_CONTEXT_SMB_V2   pCtxSmb2     = pCtxProtocol->pSmb2Context;
    ULONG                      iMsg         = pCtxSmb2->iMsg;
    PSRV_MESSAGE_SMB_V2        pSmbRequest  = &pCtxSmb2->pRequests[iMsg];
    PLWIO_SRV_SESSION_2        pSession     = NULL;
    PLWIO_SRV_TREE_2           pTree        = NULL;
    BOOLEAN                    bInLock      = FALSE;
    PSRV_NOTIFY_STATE_SMB_V2   pNotifyState = NULL;
    PLWIO_ASYNC_STATE          pAsyncState       = NULL;
    BOOLEAN                    bUnregisterAsync          = FALSE;
    PSRV_NOTIFY_REQUEST_STATE_SMB_V2 pNotifyRequestState = NULL;

    pNotifyRequestState = (PSRV_NOTIFY_REQUEST_STATE_SMB_V2)pCtxSmb2->hState;
    if (pNotifyRequestState)
    {
        InterlockedIncrement(&pNotifyRequestState->refCount);
    }
    else
    {
        PSMB2_NOTIFY_CHANGE_HEADER pRequestHeader = NULL; // Do not free

        ntStatus = SrvConnection2FindSession_SMB_V2(
                        pCtxSmb2,
                        pConnection,
                        pSmbRequest->pHeader->ullSessionId,
                        &pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvSetStatSession2Info(pExecContext, pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvSession2FindTree_SMB_V2(
                        pCtxSmb2,
                        pSession,
                        pSmbRequest->pHeader->ulTid,
                        &pTree);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SMB2UnmarshalNotifyRequest(pSmbRequest, &pRequestHeader);
        BAIL_ON_NT_STATUS(ntStatus);

        SRV_LOG_DEBUG(
                pExecContext->pLogContext,
                SMB_PROTOCOL_VERSION_2,
                pSmbRequest->pHeader->command,
                "Change notify request params: "
                "command(%u),uid(%llu),cmd-seq(%llu),pid(%u),tid(%u),"
                "credits(%u),flags(0x%x),chain-offset(%u),"
                "file-id(persistent:0x%x,volatile:0x%x),"
                "flags(0x%x),completion-filter(%u),output-buffer-length(%u)",
                pSmbRequest->pHeader->command,
                (long long)pSmbRequest->pHeader->ullSessionId,
                (long long)pSmbRequest->pHeader->ullCommandSequence,
                pSmbRequest->pHeader->ulPid,
                pSmbRequest->pHeader->ulTid,
                pSmbRequest->pHeader->usCredits,
                pSmbRequest->pHeader->ulFlags,
                pSmbRequest->pHeader->ulChainOffset,
                (long long)pRequestHeader->fid.ullPersistentId,
                (long long)pRequestHeader->fid.ullVolatileId,
                pRequestHeader->usFlags,
                pRequestHeader->ulCompletionFilter,
                pRequestHeader->ulOutputBufferLength);

        ntStatus = SrvBuildNotifyRequestState_SMB_V2(
                        pRequestHeader,
                        &pNotifyRequestState);
        BAIL_ON_NT_STATUS(ntStatus);

        pCtxSmb2->hState = pNotifyRequestState;
        InterlockedIncrement(&pNotifyRequestState->refCount);
        pCtxSmb2->pfnStateRelease = &SrvReleaseNotifyRequestStateHandle_SMB_V2;

        ntStatus = SrvTree2FindFile_SMB_V2(
                        pCtxSmb2,
                        pTree,
                        &pRequestHeader->fid,
                        &pNotifyRequestState->pFile);
        BAIL_ON_NT_STATUS(ntStatus);
    }

    LWIO_LOCK_MUTEX(bInLock, &pNotifyRequestState->mutex);

    switch (pNotifyRequestState->stage)
    {
        case SRV_NOTIFY_STAGE_SMB_V2_INITIAL:

             if (pNotifyRequestState->pRequestHeader->ulOutputBufferLength >
                 SMB_CN_MAX_BUFFER_SIZE)
             {
                 ntStatus = STATUS_INVALID_PARAMETER;
                 BAIL_ON_NT_STATUS(ntStatus);
             }

             // A change notify request can occur as the only message in
             // a request, or the last in a chained request
             if (pCtxSmb2->iMsg != (pCtxSmb2->ulNumRequests - 1))
             {
                 ntStatus = STATUS_INTERNAL_ERROR;
                 BAIL_ON_NT_STATUS(ntStatus);
             }

             ntStatus = SrvConnection2CreateAsyncState(
                             pConnection,
                             COM2_NOTIFY,
                             &SrvCancelNotifyState_SMB_V2,
                             &SrvNotifyStateReleaseHandle_SMB_V2,
                             &pAsyncState);
             BAIL_ON_NT_STATUS(ntStatus);

             bUnregisterAsync = TRUE;

             ntStatus = SrvNotifyCreateState_SMB_V2(
                             pAsyncState->ullAsyncId,
                             pExecContext->pConnection,
                             pCtxSmb2->pSession,
                             pCtxSmb2->pTree,
                             pNotifyRequestState->pFile,
                             pSmbRequest->pHeader->usEpoch,
                             pSmbRequest->pHeader->ullCommandSequence,
                             pSmbRequest->pHeader->ulPid,
                             pNotifyRequestState->pRequestHeader->ulCompletionFilter,
                             (pNotifyRequestState->pRequestHeader->usFlags &
                                     SMB2_NOTIFY_FLAGS_WATCH_TREE),
                             pNotifyRequestState->pRequestHeader->ulOutputBufferLength,
                             &pNotifyState);
             BAIL_ON_NT_STATUS(ntStatus);

             pAsyncState->hAsyncState =
                                 SrvNotifyStateAcquire_SMB_V2(pNotifyState);

             pNotifyRequestState->stage = SRV_NOTIFY_STAGE_SMB_V2_ATTEMPT_IO;

             // intentional fall through

        case SRV_NOTIFY_STAGE_SMB_V2_ATTEMPT_IO:

            ntStatus = SrvExecuteChangeNotify_SMB_V2(
                            pExecContext,
                            pNotifyState);
            switch (ntStatus)
            {
                case STATUS_PENDING:

                {
                    // TODO: Might have to cancel the entire operation
                    //
                    NTSTATUS ntStatus2 = SrvBuildInterimResponse_SMB_V2(
                                                pExecContext,
                                                pNotifyState->ullAsyncId);
                    if (ntStatus2 != STATUS_SUCCESS)
                    {
                        LWIO_LOG_ERROR(
                                "Failed to create interim response [code:0x%8x]",
                                ntStatus2);
                    }

                    bUnregisterAsync = FALSE;
                }

                    break;

                case STATUS_SUCCESS:

                    // completed synchronously; remove asynchronous state
                    //
                    SrvConnection2RemoveAsyncState(
                            pConnection,
                            pAsyncState->ullAsyncId);

                    pNotifyState->ullAsyncId = 0LL;

                    bUnregisterAsync = FALSE;

                    break;

                default:

                    break;
            }
            BAIL_ON_NT_STATUS(ntStatus);

            pNotifyRequestState->stage = SRV_NOTIFY_STAGE_SMB_V2_BUILD_RESPONSE;

            // intentional fall through

        case SRV_NOTIFY_STAGE_SMB_V2_BUILD_RESPONSE:

            ntStatus = SrvBuildNotifyResponse_SMB_V2(
                            pExecContext,
                            pNotifyState);
            BAIL_ON_NT_STATUS(ntStatus);

            pNotifyRequestState->stage = SRV_NOTIFY_STAGE_SMB_V2_DONE;

            // intentional fall through

        case SRV_NOTIFY_STAGE_SMB_V2_DONE:

            break;
    }

cleanup:

    if (pNotifyRequestState)
    {
        LWIO_UNLOCK_MUTEX(bInLock, &pNotifyRequestState->mutex);

        SrvReleaseNotifyRequestState_SMB_V2(pNotifyRequestState);
    }

    if (pNotifyState)
    {
        if (bUnregisterAsync)
        {
            SrvConnection2RemoveAsyncState(
                    pConnection,
                    pNotifyState->ullAsyncId);
        }

        SrvNotifyStateRelease_SMB_V2(pNotifyState);
    }

    if (pAsyncState)
    {
        SrvAsyncStateRelease(pAsyncState);
    }

    if (pTree)
    {
        SrvTree2Release(pTree);
    }

    if (pSession)
    {
        SrvSession2Release(pSession);
    }

    return ntStatus;

error:

    switch (ntStatus)
    {
        case STATUS_PENDING:

            break;

        default:

            if (pNotifyState)
            {
                SrvReleaseNotifyStateAsync_SMB_V2(pNotifyState);
            }

            break;
    }

    goto cleanup;
}