/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*-
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 * Editor Settings: expandtabs and use 4 spaces for indentation */

/*
 * Copyright Likewise Software
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.  You should have received a copy of the GNU General
 * Public License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

#include "includes.h"

static
NTSTATUS
SrvBuildDeletedirState(
    PDELETE_DIRECTORY_REQUEST_HEADER pRequestHeader,
    PWSTR                            pwszPathFragment,
    LONG64                           SnapshotTime,
    PSRV_DELETEDIR_STATE_SMB_V1*     ppDeletedirState
    );

static
NTSTATUS
SrvSetFileInformation(
    PSRV_EXEC_CONTEXT pExecContext
    );

static
VOID
SrvLogDelDirState_SMB_V1(
    PSRV_LOG_CONTEXT pLogContext,
    LWIO_LOG_LEVEL   logLevel,
    PCSTR            pszFunction,
    PCSTR            pszFile,
    ULONG            ulLine,
    ...
    );

static
NTSTATUS
SrvBuildDeleteDirectoryResponse(
    PSRV_EXEC_CONTEXT pExecContext
    );

static
VOID
SrvFreeDeletedirStateHandle(
    HANDLE hState
    );

static
VOID
SrvFreeDeletedirState(
    PSRV_DELETEDIR_STATE_SMB_V1 pDeletedirState
    );

NTSTATUS
SrvProcessDeleteDirectory(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                    ntStatus     = 0;
    PLWIO_SRV_CONNECTION        pConnection  = pExecContext->pConnection;
    PSRV_EXEC_CONTEXT_SMB_V1    pCtxSmb1     = pExecContext->pProtocolContext;
    PLWIO_SRV_SESSION           pSession     = NULL;
    PLWIO_SRV_TREE              pTree        = NULL;
    PSRV_DELETEDIR_STATE_SMB_V1 pDeletedirState = pCtxSmb1->hState;

    if (pDeletedirState)
    {
        ntStatus = SRV_SCHEDULE_IF(
                        pExecContext,
                        pDeletedirState->hFile);
        BAIL_ON_NT_STATUS(ntStatus);
    }
    else
    {
        ULONG               iMsg         = pCtxSmb1->iMsg;
        PSRV_MESSAGE_SMB_V1 pSmbRequest  = &pCtxSmb1->pRequests[iMsg];
        PBYTE pBuffer          = pSmbRequest->pBuffer + pSmbRequest->usHeaderSize;
        ULONG ulOffset         = pSmbRequest->usHeaderSize;
        ULONG ulBytesAvailable = pSmbRequest->ulMessageSize - pSmbRequest->usHeaderSize;
        PDELETE_DIRECTORY_REQUEST_HEADER pRequestHeader   = NULL; // Do not free
        PWSTR                            pwszPathFragment = NULL; // Do not free
        BOOLEAN processSnapshots = FALSE;
        LONG64 snapshotTime = 0;

        ntStatus = SrvConnectionFindSession_SMB_V1(
                        pCtxSmb1,
                        pConnection,
                        pSmbRequest->pHeader->uid,
                        &pSession);
        BAIL_ON_NT_STATUS(ntStatus);

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

        ntStatus = SrvSessionFindTree_SMB_V1(
                        pCtxSmb1,
                        pSession,
                        pSmbRequest->pHeader->tid,
                        &pTree);
        BAIL_ON_NT_STATUS(ntStatus);

        processSnapshots = SrvShouldProcessSnapshots(
                                pSmbRequest->pHeader,
                                pCtxSmb1->pTree->UnderstandSnapshots);
        ntStatus = WireUnmarshallDirectoryDeleteRequest(
                        pBuffer,
                        ulBytesAvailable,
                        ulOffset,
                        &pRequestHeader,
                        &pwszPathFragment,
                        processSnapshots ? &snapshotTime : NULL);
        BAIL_ON_NT_STATUS(ntStatus);

        if (!pwszPathFragment || !*pwszPathFragment)
        {
            ntStatus = STATUS_CANNOT_DELETE;
            BAIL_ON_NT_STATUS(ntStatus);
        }

        ntStatus = SrvBuildDeletedirState(
                        pRequestHeader,
                        pwszPathFragment,
                        snapshotTime,
                        &pDeletedirState);
        BAIL_ON_NT_STATUS(ntStatus);

        pCtxSmb1->hState = pDeletedirState;
        pCtxSmb1->pfnStateRelease = &SrvFreeDeletedirStateHandle;
    }

    switch (pDeletedirState->stage)
    {
        case SRV_DELETEDIR_STAGE_SMB_V1_INITIAL:

            SRV_LOG_CALL_DEBUG(
                    pExecContext->pLogContext,
                    SMB_PROTOCOL_VERSION_1,
                    pCtxSmb1->pRequests[pCtxSmb1->iMsg].pHeader->command,
                    &SrvLogDelDirState_SMB_V1,
                    pDeletedirState);

            ntStatus = SrvBuildTreeRelativePath(
                            pCtxSmb1->pTree,
                            pDeletedirState->pwszPathFragment,
                            &pDeletedirState->fileName);
            BAIL_ON_NT_STATUS(ntStatus);

            pDeletedirState->stage = SRV_DELETEDIR_STAGE_SMB_V1_ATTEMPT_SET_INFO;

            SrvPrepareExecContextAsync(pExecContext);

            ntStatus = SrvIoCreateFile(
                            pExecContext,
                            pCtxSmb1->pTree->pShareInfo,
                            &pDeletedirState->hFile,
                            pExecContext->pAcb,
                            &pExecContext->ioStatusBlock,
                            pCtxSmb1->pSession->pIoSecurityContext,
                            &pDeletedirState->fileName,
                            pDeletedirState->pSecurityDescriptor,
                            pDeletedirState->pSecurityQOS,
                            DELETE,
                            0,
                            FILE_ATTRIBUTE_NORMAL,
                            FILE_SHARE_READ | FILE_SHARE_WRITE |
                            FILE_SHARE_DELETE,
                            FILE_OPEN,
                            FILE_DIRECTORY_FILE,
                            NULL,
                            0,
                            &pDeletedirState->SnapshotTime,
                            &pDeletedirState->pEcpList);
            BAIL_ON_NT_STATUS(ntStatus);

            SrvReleaseExecContextAsync(pExecContext);

            // intentional fall through

        case SRV_DELETEDIR_STAGE_SMB_V1_ATTEMPT_SET_INFO:

            pDeletedirState->stage = SRV_DELETEDIR_STAGE_SMB_V1_COMPLETED;

            ntStatus = SrvSetFileInformation(pExecContext);
            BAIL_ON_NT_STATUS(ntStatus);

            // intentional fall through

        case SRV_DELETEDIR_STAGE_SMB_V1_COMPLETED:

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

            // intentional fall through

        case SRV_DELETEDIR_STAGE_SMB_V1_BUILD_RESPONSE:

            ntStatus = SrvBuildDeleteDirectoryResponse(pExecContext);
            BAIL_ON_NT_STATUS(ntStatus);

            // intentional fall through

        case SRV_DELETEDIR_STAGE_SMB_V1_DONE:

            break;
    }

cleanup:

    if (pTree)
    {
        SrvTreeRelease(pTree);
    }

    if (pSession)
    {
        SrvSessionRelease(pSession);
    }

    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:

            SrvReleaseExecContextAsync(pExecContext);

            break;
    }

    goto cleanup;
}

static
NTSTATUS
SrvBuildDeletedirState(
    PDELETE_DIRECTORY_REQUEST_HEADER pRequestHeader,
    PWSTR                            pwszPathFragment,
    LONG64                           SnapshotTime,
    PSRV_DELETEDIR_STATE_SMB_V1*     ppDeletedirState
    )
{
    NTSTATUS                    ntStatus        = STATUS_SUCCESS;
    PSRV_DELETEDIR_STATE_SMB_V1 pDeletedirState = NULL;

    ntStatus = SrvAllocateMemory(
                    sizeof(SRV_DELETEDIR_STATE_SMB_V1),
                    (PVOID*)&pDeletedirState);
    BAIL_ON_NT_STATUS(ntStatus);

    pDeletedirState->stage = SRV_DELETEDIR_STAGE_SMB_V1_INITIAL;

    pDeletedirState->pwszPathFragment = pwszPathFragment;
    pDeletedirState->pRequestHeader   = pRequestHeader;

    pDeletedirState->pFileDispositionInfo =
	&pDeletedirState->fileDispositionInfo;

    pDeletedirState->pFileDispositionInfo->DeleteFile = TRUE;
    pDeletedirState->SnapshotTime = SnapshotTime;

    *ppDeletedirState = pDeletedirState;

cleanup:

    return ntStatus;

error:

    *ppDeletedirState = NULL;

    if (pDeletedirState)
    {
        SrvFreeDeletedirState(pDeletedirState);
    }

    goto cleanup;
}

static
NTSTATUS
SrvSetFileInformation(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                    ntStatus = 0;
    PSRV_EXEC_CONTEXT_SMB_V1    pCtxSmb1        = pExecContext->pProtocolContext;
    PSRV_DELETEDIR_STATE_SMB_V1 pDeletedirState = pCtxSmb1->hState;

    SrvPrepareExecContextAsync(pExecContext);

    ntStatus = IoSetInformationFile(
                    pDeletedirState->hFile,
                    pExecContext->pAcb,
                    &pExecContext->ioStatusBlock,
                    pDeletedirState->pFileDispositionInfo,
                    sizeof(pDeletedirState->fileDispositionInfo),
                    FileDispositionInformation);
    BAIL_ON_NT_STATUS(ntStatus);

    SrvReleaseExecContextAsync(pExecContext); // completed synchronously

cleanup:

    return ntStatus;

error:

    goto cleanup;
}

static
VOID
SrvLogDelDirState_SMB_V1(
    PSRV_LOG_CONTEXT pLogContext,
    LWIO_LOG_LEVEL   logLevel,
    PCSTR            pszFunction,
    PCSTR            pszFile,
    ULONG            ulLine,
    ...
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    PSRV_DELETEDIR_STATE_SMB_V1 pDeldirState = NULL;
    PSTR pszPath = NULL;
    va_list msgList;

    va_start(msgList, ulLine);

    pDeldirState = va_arg(msgList, PSRV_DELETEDIR_STATE_SMB_V1);

    if (pDeldirState)
    {
        if (pDeldirState->pwszPathFragment)
        {
            ntStatus = SrvWc16sToMbs(pDeldirState->pwszPathFragment, &pszPath);
            BAIL_ON_NT_STATUS(ntStatus);
        }

        LW_RTL_LOG_RAW(
            logLevel,
            "srv",
            pszFunction,
            pszFile,
            ulLine,
            "Delete directory state: Path(%s)",
            LWIO_SAFE_LOG_STRING(pszPath));
    }

error:

    va_end(msgList);

    SRV_SAFE_FREE_MEMORY(pszPath);

    return;
}

static
NTSTATUS
SrvBuildDeleteDirectoryResponse(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                    ntStatus     = STATUS_SUCCESS;
    PLWIO_SRV_CONNECTION        pConnection  = pExecContext->pConnection;
    PSRV_EXEC_CONTEXT_SMB_V1    pCtxSmb1     = pExecContext->pProtocolContext;
    ULONG                       iMsg         = pCtxSmb1->iMsg;
    PSRV_MESSAGE_SMB_V1         pSmbRequest  = &pCtxSmb1->pRequests[iMsg];
    PSRV_MESSAGE_SMB_V1         pSmbResponse = &pCtxSmb1->pResponses[iMsg];
    PSMB_DELETE_RESPONSE_HEADER pResponseHeader = NULL; // Do not free
    PBYTE  pOutBuffer           = pSmbResponse->pBuffer;
    ULONG  ulBytesAvailable     = pSmbResponse->ulBytesAvailable;
    ULONG  ulOffset             = 0;
    USHORT usBytesUsed          = 0;
    ULONG  ulTotalBytesUsed     = 0;

    if (!pSmbResponse->ulSerialNum)
    {
        ntStatus = SrvMarshalHeader_SMB_V1(
                        pOutBuffer,
                        ulOffset,
                        ulBytesAvailable,
                        COM_DELETE_DIRECTORY,
                        STATUS_SUCCESS,
                        TRUE,
                        (pSmbRequest->pHeader->flags2 & FLAG2_ERR_STATUS) != 0,
                        pConnection->serverProperties.ulCapabilities,
                        pCtxSmb1->pTree->tid,
                        SMB_V1_GET_PROCESS_ID(pSmbRequest->pHeader),
                        pCtxSmb1->pSession->uid,
                        pSmbRequest->pHeader->mid,
                        &pSmbResponse->pHeader,
                        &pSmbResponse->pWordCount,
                        &pSmbResponse->pAndXHeader,
                        &pSmbResponse->usHeaderSize);
    }
    else
    {
        ntStatus = SrvMarshalHeaderAndX_SMB_V1(
                        pOutBuffer,
                        ulOffset,
                        ulBytesAvailable,
                        COM_DELETE_DIRECTORY,
                        &pSmbResponse->pWordCount,
                        &pSmbResponse->pAndXHeader,
                        &pSmbResponse->usHeaderSize);
    }
    BAIL_ON_NT_STATUS(ntStatus);

    pOutBuffer       += pSmbResponse->usHeaderSize;
    ulOffset         += pSmbResponse->usHeaderSize;
    ulBytesAvailable -= pSmbResponse->usHeaderSize;
    ulTotalBytesUsed += pSmbResponse->usHeaderSize;

    *pSmbResponse->pWordCount = 0;

    ntStatus = WireMarshallDeleteResponse(
                    pOutBuffer,
                    ulBytesAvailable,
                    ulOffset,
                    &pResponseHeader,
                    &usBytesUsed);
    BAIL_ON_NT_STATUS(ntStatus);

    // pOutBuffer       += usBytesUsed;
    // ulOffset         += usBytesUsed;
    // ulBytesAvailable -= usBytesUsed;
    ulTotalBytesUsed += usBytesUsed;

    pSmbResponse->ulMessageSize = ulTotalBytesUsed;

cleanup:

    return ntStatus;

error:

    if (ulTotalBytesUsed)
    {
        pSmbResponse->pHeader = NULL;
        pSmbResponse->pAndXHeader = NULL;
        memset(pSmbResponse->pBuffer, 0, ulTotalBytesUsed);
    }

    pSmbResponse->ulMessageSize = 0;

    goto cleanup;
}

static
VOID
SrvFreeDeletedirStateHandle(
    HANDLE hState
    )
{
    SrvFreeDeletedirState(hState);
}

static
VOID
SrvFreeDeletedirState(
    PSRV_DELETEDIR_STATE_SMB_V1 pDeletedirState
    )
{
    if (pDeletedirState->pEcpList)
    {
        IoRtlEcpListFree(&pDeletedirState->pEcpList);
    }

    // TODO: Free the following if set
    // pSecurityDescriptor;
    // pSecurityQOS;

    SRV_FREE_UNICODE_STRING(&pDeletedirState->fileName.Name);

    if (pDeletedirState->hFile)
    {
        IoCloseFile(pDeletedirState->hFile);
    }

    SrvFreeMemory(pDeletedirState);
}
