/* -*- 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
SrvUnmarshallSessionSetupRequest_WC_12(
    PLWIO_SRV_CONNECTION pConnection,
    PSRV_MESSAGE_SMB_V1  pSmbRequest,
    PBYTE*               ppSecurityBlob,
    PULONG               pulSecurityBlobLength
    );

static
NTSTATUS
SrvUnmarshallSessionSetupRequest_WC_13(
    PLWIO_SRV_CONNECTION                pConnection,
    PSRV_MESSAGE_SMB_V1                 pSmbRequest,
    PLW_MAP_SECURITY_NTLM_LOGON_INFO    pNtlmLogonInfo
    );

static
NTSTATUS
SrvMarshallSessionSetupResponse_WC_4(
    PSRV_EXEC_CONTEXT    pExecContext,
    PLWIO_SRV_SESSION    pSession,
    PBYTE                pSecurityBlob,
    ULONG                ulSecurityBlobLength
    );

static
NTSTATUS
SrvMarshallSessionSetupResponse_WC_3(
    PSRV_EXEC_CONTEXT    pExecContext
    );

static
NTSTATUS
SrvProcessSessionSetup_WC_12(
    PSRV_EXEC_CONTEXT pExecContext
    );

static
NTSTATUS
SrvProcessSessionSetup_WC_13(
    PSRV_EXEC_CONTEXT pExecContext
    );


static
VOID
SrvLogSessionSetupResult(
    PSRV_EXEC_CONTEXT pExecContext,
    NTSTATUS Status
    )
{
    if (!NT_SUCCESS(Status))
    {
        SrvEventSessionSetupFailure(
            pExecContext->pConnection->pClientAddress,
            Status);
    }
}

NTSTATUS
SrvProcessSessionSetup(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                 ntStatus    = 0;
    PSRV_EXEC_CONTEXT_SMB_V1 pCtxSmb1    = pExecContext->pProtocolContext;
    ULONG                    iMsg        = pCtxSmb1->iMsg;
    PSRV_MESSAGE_SMB_V1      pSmbRequest = &pCtxSmb1->pRequests[iMsg];

    if (pExecContext->bInline)
    {
        SrvScheduleExecContext(pExecContext);

        ntStatus = STATUS_PENDING;

        goto cleanup;
    }

    if (*pSmbRequest->pWordCount == 12)
    {
        ntStatus = SrvProcessSessionSetup_WC_12(pExecContext);
    }
    else if (*pSmbRequest->pWordCount == 13)
    {
        ntStatus = SrvProcessSessionSetup_WC_13(pExecContext);
    }
    else
    {
        ntStatus = STATUS_INVALID_NETWORK_RESPONSE;
    }

    BAIL_ON_NT_STATUS(ntStatus);

cleanup:

    if (!NT_SUCCESS(ntStatus))
    {
        SrvLogSessionSetupResult(pExecContext, ntStatus);
        ntStatus = SrvGssCollapseLogonFailureStatus(ntStatus);
    }

    return ntStatus;

error:

    goto cleanup;
}

static
NTSTATUS
SrvProcessSessionSetup_WC_12(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                   ntStatus      = 0;
    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];
    PBYTE                      pSecurityBlob = NULL; // Do Not Free
    ULONG                      ulSecurityBlobLength = 0;
    LW_MAP_SECURITY_GSS_CONTEXT hContextHandle = NULL;
    PSTR                       pszClientPrincipalName = NULL;
    PLWIO_SRV_SESSION          pSession               = NULL;
    PSRV_MESSAGE_SMB_V1        pSmbResponse = &pCtxSmb1->pResponses[iMsg];
    BOOLEAN                    bLoggedInAsGuest = FALSE;
    BOOLEAN gssLocked = FALSE;
    PWSTR pDomain = NULL;
    PWSTR pUser = NULL;

    ntStatus = SrvUnmarshallSessionSetupRequest_WC_12(
                    pConnection,
                    pSmbRequest,
                    &pSecurityBlob,
                    &ulSecurityBlobLength);
    BAIL_ON_NT_STATUS(ntStatus);

    if (pSmbRequest->pHeader->uid)
    {
        ntStatus = SrvConnectionFindSession(
                        pConnection,
                        pSmbRequest->pHeader->uid,
                        &pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        SRV_LOG_VERBOSE(
                pExecContext->pLogContext,
                SMB_PROTOCOL_VERSION_1,
                pSmbRequest->pHeader->command,
                "Using existing session: "
                "command(%u),uid(%u),mid(%u),pid(%u),tid(%u),session-id(%u)",
                pSmbRequest->pHeader->command,
                pSmbRequest->pHeader->uid,
                pSmbRequest->pHeader->mid,
                SMB_V1_GET_PROCESS_ID(pSmbRequest->pHeader),
                pSmbRequest->pHeader->tid,
                pSession->uid);

            /* Does this invalidate the current session key? */
    }
    else
    {
        ntStatus = SrvConnectionCreateSession(pConnection, &pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        SRV_LOG_VERBOSE(
                pExecContext->pLogContext,
                SMB_PROTOCOL_VERSION_1,
                pSmbRequest->pHeader->command,
                "Created session : "
                "command(%u),uid(%u),mid(%u),pid(%u),tid(%u),session-id(%u)",
                pSmbRequest->pHeader->command,
                pSmbRequest->pHeader->uid,
                pSmbRequest->pHeader->mid,
                SMB_V1_GET_PROCESS_ID(pSmbRequest->pHeader),
                pSmbRequest->pHeader->tid,
                pSession->uid);
    }

    LWIO_LOCK_MUTEX(gssLocked, &pSession->GssMutex);

    if (pSession->hGssNegotiate == NULL)
    {
        ntStatus = SrvGssBeginNegotiate(&pSession->hGssNegotiate);
        BAIL_ON_NT_STATUS(ntStatus);
    }

    ntStatus = SrvMarshallSessionSetupResponse_WC_4(
                    pExecContext,
                    pSession,
                    pSecurityBlob,
                    ulSecurityBlobLength);
    BAIL_ON_NT_STATUS(ntStatus);

    pSmbResponse->pHeader->uid = pSession->uid;

    if (SrvGssNegotiateIsComplete(pSession->hGssNegotiate))
    {
        ntStatus = SrvSetStatSessionInfo(pExecContext, pSession);
        BAIL_ON_NT_STATUS(ntStatus);

        /*
         * Session Setup on an existing session does not change the
         * session key.
         */
        ntStatus = SrvGssGetSessionDetails(
                       pSession->hGssNegotiate,
                       pSession->pSessionKey ? NULL : &pSession->pSessionKey,
                       pSession->pSessionKey ? NULL :
                       &pSession->ulSessionKeyLength,
                       &pszClientPrincipalName,
                       &hContextHandle);
        BAIL_ON_NT_STATUS(ntStatus);

        ntStatus = SrvIoSecCreateSecurityContextFromGssContext(
                       &pSession->pIoSecurityContext,
                       &bLoggedInAsGuest,
                       hContextHandle,
                       pszClientPrincipalName);
        BAIL_ON_NT_STATUS(ntStatus);

        if (bLoggedInAsGuest)
        {
            PBYTE pSessionBuffer = pSmbResponse->pBuffer + pSmbResponse->usHeaderSize;

            ((PSESSION_SETUP_RESPONSE_HEADER_WC_4)pSessionBuffer)->action = 0x1;

            SrvSessionSetUserFlags(pSession, SMB_SESSION_FLAG_GUEST);

            SRV_LOG_VERBOSE(
                    pExecContext->pLogContext,
                    SMB_PROTOCOL_VERSION_1,
                    pSmbRequest->pHeader->command,
                    "Session info: "
                    "command(%u),uid(%u),mid(%u),pid(%u),tid(%u),"
                    "session-id(%u) logged in as guest",
                    pSmbRequest->pHeader->command,
                    pSmbRequest->pHeader->uid,
                    pSmbRequest->pHeader->mid,
                    SMB_V1_GET_PROCESS_ID(pSmbRequest->pHeader),
                    pSmbRequest->pHeader->tid,
                    pSession->uid);
        }
        else
        {
            SrvConnectionSetSigningKeyIfUnset(
                pExecContext->pConnection,
                pSession->pSessionKey,
                pSession->ulSessionKeyLength);
        }

        // Go ahead and close out this GSS negotiate state so we can
        // handle another Session setup.

        SrvGssEndNegotiate(pSession->hGssNegotiate);

        pSession->hGssNegotiate = NULL;

        if (pszClientPrincipalName)
        {
            ntStatus = SrvSessionSetPrincipalName(
                            pSession,
                            pszClientPrincipalName);
            BAIL_ON_NT_STATUS(ntStatus);

            SRV_LOG_VERBOSE(
                    pExecContext->pLogContext,
                    SMB_PROTOCOL_VERSION_1,
                    pSmbRequest->pHeader->command,
                    "Session info: "
                    "command(%u),uid(%u),mid(%u),pid(%u),tid(%u),"
                    "session-id(%u),user-principal(%s)",
                    pSmbRequest->pHeader->command,
                    pSmbRequest->pHeader->uid,
                    pSmbRequest->pHeader->mid,
                    SMB_V1_GET_PROCESS_ID(pSmbRequest->pHeader),
                    pSmbRequest->pHeader->tid,
                    pSession->uid,
                    pszClientPrincipalName);
        }

        ntStatus = SrvIoGetNtNameFromIoSecurityContext(
                pSession->pIoSecurityContext,
                &pDomain,
                &pUser);
        BAIL_ON_NT_STATUS(ntStatus);

        SrvSessionSetNtName(
            pSession,
            &pDomain,
            &pUser);

        if (pConnection->pOEMConnection)
        {
            SRV_OEM_SESSION_ID sessionId =
            {
                    .ver   = SMB_PROTOCOL_VERSION_1,
                    .id    =
                        {
                            .usUid = pSession->uid
                        }
            };

            ntStatus = SrvOEMCreateSession(
                            pConnection->pOEMConnection,
                            pConnection->ulOEMConnectionLength,
                            &sessionId,
                            pSession->pIoSecurityContext,
                            pConnection->clientProperties.pwszNativeOS,
                            &pSession->pOEMSession,
                            &pSession->ulOEMSessionLength);
            BAIL_ON_NT_STATUS(ntStatus);
        }

        SrvConnectionSetState(pConnection, LWIO_SRV_CONN_STATE_READY);

        pCtxSmb1->pSession = SrvSessionAcquire(pSession);
    }

cleanup:

    LWIO_UNLOCK_MUTEX(gssLocked, &pSession->GssMutex);

    RTL_FREE(&pDomain);
    RTL_FREE(&pUser);
    SRV_SAFE_FREE_MEMORY(pszClientPrincipalName);

    if (pSession)
    {
        SrvSessionRelease(pSession);
    }

    return ntStatus;

error:

    /* If a session exists, rundown the session */
    if (!pSession && pSmbRequest->pHeader->uid)
    {
        (void) SrvConnectionFindSession(
                pConnection,
                pSmbRequest->pHeader->uid,
                &pSession);
    }

    if (pSession && pSession->hGssNegotiate)
    {
        SrvGssEndNegotiate(pSession->hGssNegotiate);

        pSession->hGssNegotiate = NULL;
    }

    LWIO_UNLOCK_MUTEX(gssLocked, &pSession->GssMutex);

    if (pSession)
    {
        SrvSessionRundown(pSession);
    }

    goto cleanup;
}

static
NTSTATUS
SrvProcessSessionSetup_WC_13(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS                    ntStatus    = STATUS_SUCCESS;
    PLWIO_SRV_CONNECTION        pConnection = pExecContext->pConnection;
    PSRV_EXEC_CONTEXT_SMB_V1    pCtxSmb1    = pExecContext->pProtocolContext;
    PLWIO_SRV_SESSION           pSession    = NULL;
    ULONG                       iMsg        = pCtxSmb1->iMsg;
    PSRV_MESSAGE_SMB_V1         pSmbRequest = &pCtxSmb1->pRequests[iMsg];
    PSRV_MESSAGE_SMB_V1         pSmbResponse= &pCtxSmb1->pResponses[iMsg];
    LW_MAP_SECURITY_NTLM_LOGON_INFO ntlmLogonInfo = { 0 };
    BOOLEAN                     bLoggedInAsGuest = FALSE;
    BOOLEAN                     bSessionSetupLocked = FALSE;
    PSTR                        pszUsername = NULL;
    PBYTE                       pSessionKey = NULL;
    ULONG                       ulSessionKeyLength = 0;
    PLW_MAP_SECURITY_CONTEXT mapSecurityContext = NULL;
    PSTR localMachineName = NULL;
    PSID localMachineSid = NULL;
    PWSTR pDomain = NULL;
    PWSTR pUser = NULL;

    // Unmarshall request
    ntStatus = SrvUnmarshallSessionSetupRequest_WC_13(
                    pConnection,
                    pSmbRequest,
                    &ntlmLogonInfo);
    BAIL_ON_NT_STATUS(ntStatus);

    RtlCopyMemory(ntlmLogonInfo.Challenge, pConnection->ServerChallenge,
                  sizeof(ntlmLogonInfo.Challenge));

    ntStatus = SrvMarshallSessionSetupResponse_WC_3(pExecContext);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SrvConnectionCreateSession(pConnection, &pSession);
    BAIL_ON_NT_STATUS(ntStatus);

    pSmbResponse->pHeader->uid = pSession->uid;

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

    ntStatus = LwMapSecurityCreateContext(&mapSecurityContext);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = LwMapSecurityGetLocalMachineInformation(
                   mapSecurityContext,
                   &localMachineName,
                   &localMachineSid);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = RtlWC16StringAllocateFromCString(
                   &ntlmLogonInfo.pwszWorkstation,
                   localMachineName);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SrvIoSecCreateSecurityContextFromNtlmLogon(
                    &pSession->pIoSecurityContext,
                    &bLoggedInAsGuest,
                    &pszUsername,
                    (PVOID*)&pSessionKey,
                    &ulSessionKeyLength,
                    &ntlmLogonInfo);
    BAIL_ON_NT_STATUS(ntStatus);

    if (bLoggedInAsGuest)
    {
        PBYTE pSessionBuffer = pSmbResponse->pBuffer + pSmbResponse->usHeaderSize;
        ((PSESSION_SETUP_RESPONSE_HEADER_WC_3)pSessionBuffer)->action = 0x1;

        SrvSessionSetUserFlags(pSession, SMB_SESSION_FLAG_GUEST);
    }
    else
    {
        SrvConnectionSetSigningKeyIfUnset(
            pExecContext->pConnection,
            pSession->pSessionKey,
            pSession->ulSessionKeyLength);
    }

    ntStatus = SrvSessionSetPrincipalName(pSession, pszUsername);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SrvIoGetNtNameFromIoSecurityContext(
            pSession->pIoSecurityContext,
            &pDomain,
            &pUser);
    BAIL_ON_NT_STATUS(ntStatus);

    SrvSessionSetNtName(
        pSession,
        &pDomain,
        &pUser);

    if (pConnection->pOEMConnection)
    {
        SRV_OEM_SESSION_ID sessionId =
        {
                .ver   = SMB_PROTOCOL_VERSION_1,
                .id    =
                    {
                        .usUid = pSession->uid
                    }
        };

        ntStatus = SrvOEMCreateSession(
                        pConnection->pOEMConnection,
                        pConnection->ulOEMConnectionLength,
                        &sessionId,
                        pSession->pIoSecurityContext,
                        pConnection->clientProperties.pwszNativeOS,
                        &pSession->pOEMSession,
                        &pSession->ulOEMSessionLength);
        BAIL_ON_NT_STATUS(ntStatus);
    }

    SrvConnectionSetState(pConnection, LWIO_SRV_CONN_STATE_READY);

    pCtxSmb1->pSession = SrvSessionAcquire(pSession);

cleanup:

    LWIO_UNLOCK_RWMUTEX(bSessionSetupLocked, &pSession->mutex);

    RtlWC16StringFree(&ntlmLogonInfo.pwszAccountName);
    RtlWC16StringFree(&ntlmLogonInfo.pwszDomain);
    RtlWC16StringFree(&ntlmLogonInfo.pwszWorkstation);

    if (mapSecurityContext)
    {
        if (localMachineName)
        {
            LwMapSecurityFreeCString(mapSecurityContext, &localMachineName);
        }
        if (localMachineSid)
        {
            LwMapSecurityFreeSid(mapSecurityContext, &localMachineSid);
        }

        LwMapSecurityFreeContext(&mapSecurityContext);
    }

    SRV_SAFE_FREE_MEMORY(pszUsername);
    SRV_SAFE_FREE_MEMORY(pSessionKey);
    RTL_FREE(&pDomain);
    RTL_FREE(&pUser);

    if (pSession)
    {
        SrvSessionRelease(pSession);
    }

    return ntStatus;

error:

    LWIO_UNLOCK_RWMUTEX(bSessionSetupLocked, &pSession->mutex);

    /* If a session exists, run down the session */
    if (pSession)
    {
        SrvSessionRundown(pSession);
    }

    goto cleanup;
}

static
NTSTATUS
SrvUnmarshallSessionSetupRequest_WC_12(
    PLWIO_SRV_CONNECTION pConnection,
    PSRV_MESSAGE_SMB_V1  pSmbRequest,
    PBYTE*               ppSecurityBlob,
    PULONG               pulSecurityBlobLength
    )
{
    NTSTATUS ntStatus = 0;
    SESSION_SETUP_REQUEST_HEADER_WC_12* pHeader = NULL; // Do not free
    PBYTE pSecurityBlob    = NULL; // Do not free
    PWSTR pwszNativeOS     = NULL; // Do not free
    PWSTR pwszNativeLanMan = NULL; // Do not free
    PBYTE pBuffer          = pSmbRequest->pBuffer + pSmbRequest->usHeaderSize;
    ULONG ulBytesAvailable = pSmbRequest->ulMessageSize - pSmbRequest->usHeaderSize;
    BOOLEAN inLock = FALSE;

    ntStatus = UnmarshallSessionSetupRequest_WC_12(
                    pSmbRequest->pHeader,
                    pBuffer,
                    ulBytesAvailable,
                    &pHeader,
                    &pSecurityBlob,
                    &pwszNativeOS,
                    &pwszNativeLanMan
                    );
    BAIL_ON_NT_STATUS(ntStatus);

    LWIO_LOCK_RWMUTEX_EXCLUSIVE(inLock, &pConnection->mutex);

    if (!pConnection->clientProperties.bIsInitialized)
    {
        if (pwszNativeOS)
        {
            ntStatus = SrvAllocateStringW(
                            pwszNativeOS,
                            &pConnection->clientProperties.pwszNativeOS);
            BAIL_ON_NT_STATUS(ntStatus);
        }

        if (pwszNativeLanMan)
        {
            ntStatus = SrvAllocateStringW(
                            pwszNativeLanMan,
                            &pConnection->clientProperties.pwszNativeLanMan);
            BAIL_ON_NT_STATUS(ntStatus);
        }

        pConnection->clientProperties.ulCapabilities = pHeader->capabilities;
        pConnection->clientProperties.usMaxBufferSize = pHeader->maxBufferSize;
        pConnection->clientProperties.usMaxMpxCount = pHeader->maxMpxCount;
        pConnection->clientProperties.ulSessionKey = pHeader->sessionKey;
        pConnection->clientProperties.usVcNumber = pHeader->vcNumber;

        if (pSmbRequest->pHeader->flags2 & FLAG2_SECURITY_SIG)
        {
            pConnection->clientProperties.bEnableSecuritySignatures = TRUE;
        }

        if (pSmbRequest->pHeader->flags2 & FLAG2_REQUIRE_SIG)
        {
            pConnection->clientProperties.bRequireSecuritySignatures = TRUE;
        }

        pConnection->clientProperties.bIsInitialized = TRUE;
    }

    *ppSecurityBlob = pSecurityBlob;
    *pulSecurityBlobLength = pHeader->securityBlobLength;

cleanup:
    RtlWC16StringFree(&pwszNativeOS);
    RtlWC16StringFree(&pwszNativeLanMan);

    LWIO_UNLOCK_RWMUTEX(inLock, &pConnection->mutex);

    return ntStatus;

error:

    *ppSecurityBlob = NULL;
    *pulSecurityBlobLength = 0;

    goto cleanup;
}

static
NTSTATUS
SrvUnmarshallSessionSetupRequest_WC_13(
    PLWIO_SRV_CONNECTION    pConnection,
    PSRV_MESSAGE_SMB_V1     pSmbRequest,
    PLW_MAP_SECURITY_NTLM_LOGON_INFO    pNtlmLogonInfo
    )
{
    NTSTATUS ntStatus = 0;
    SESSION_SETUP_REQUEST_HEADER_WC_13* pHeader = NULL; // Do not free
    PWSTR pwszNativeOS     = NULL; // Do not free
    PWSTR pwszNativeLanMan = NULL; // Do not free
    PBYTE pBuffer          = pSmbRequest->pBuffer + pSmbRequest->usHeaderSize;
    ULONG ulBytesAvailable = pSmbRequest->ulMessageSize - pSmbRequest->usHeaderSize;
    BOOLEAN inLock = FALSE;

    ntStatus = UnmarshallSessionSetupRequest_WC_13(
                    pSmbRequest->pHeader,
                    pBuffer,
                    ulBytesAvailable,
                    &pHeader,
                    pNtlmLogonInfo,
                    &pwszNativeOS,
                    &pwszNativeLanMan);
    BAIL_ON_NT_STATUS(ntStatus);

    LWIO_LOCK_RWMUTEX_EXCLUSIVE(inLock, &pConnection->mutex);

    if (!pConnection->clientProperties.bIsInitialized)
    {
        if (pwszNativeOS)
        {
            ntStatus = SrvAllocateStringW(
                            pwszNativeOS,
                            &pConnection->clientProperties.pwszNativeOS);
            BAIL_ON_NT_STATUS(ntStatus);
        }

        if (pwszNativeLanMan)
        {
            ntStatus = SrvAllocateStringW(
                            pwszNativeLanMan,
                            &pConnection->clientProperties.pwszNativeLanMan);
            BAIL_ON_NT_STATUS(ntStatus);
        }

        if (pNtlmLogonInfo->pwszDomain)
        {
            ntStatus = SrvAllocateStringW(
                            pNtlmLogonInfo->pwszDomain,
                            &pConnection->clientProperties.pwszNativeDomain);
            BAIL_ON_NT_STATUS(ntStatus);
        }

        pConnection->clientProperties.ulCapabilities = pHeader->capabilities;
        pConnection->clientProperties.usMaxBufferSize = pHeader->maxBufferSize;
        pConnection->clientProperties.usMaxMpxCount = pHeader->maxMpxCount;
        pConnection->clientProperties.ulSessionKey = pHeader->sessionKey;
        pConnection->clientProperties.usVcNumber = pHeader->vcNumber;

        if (pSmbRequest->pHeader->flags2 & FLAG2_SECURITY_SIG)
        {
            pConnection->clientProperties.bEnableSecuritySignatures = TRUE;
        }

        if (pSmbRequest->pHeader->flags2 & FLAG2_REQUIRE_SIG)
        {
            pConnection->clientProperties.bRequireSecuritySignatures = TRUE;
        }

        pConnection->clientProperties.bIsInitialized = TRUE;
    }

error:

    RtlWC16StringFree(&pwszNativeOS);
    RtlWC16StringFree(&pwszNativeLanMan);

    LWIO_UNLOCK_RWMUTEX(inLock, &pConnection->mutex);

    return ntStatus;
}

static
NTSTATUS
SrvMarshallSessionSetupResponse_WC_4(
    PSRV_EXEC_CONTEXT pExecContext,
    PLWIO_SRV_SESSION pSession,
    PBYTE             pSecurityBlob,
    ULONG             ulSecurityBlobLength
    )
{
    NTSTATUS ntStatus = 0;
    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];
    SESSION_SETUP_RESPONSE_HEADER_WC_4* pResponseHeader = NULL;
    PBYTE     pReplySecurityBlob = NULL;
    ULONG     ulReplySecurityBlobLength = 0;
    wchar16_t wszNativeOS[] = SRV_NATIVE_OS_W;
    wchar16_t wszNativeLanMan[] = SRV_NATIVE_LAN_MAN_W;
    wchar16_t wszNativeDomain[] = SRV_NATIVE_DOMAIN_W;
    PBYTE pOutBuffer         = pSmbResponse->pBuffer;
    ULONG ulBytesAvailable   = pSmbResponse->ulBytesAvailable;
    ULONG ulOffset           = 0;
    ULONG  ulBytesUsed       = 0;
    ULONG ulTotalBytesUsed   = 0;

    ntStatus = SrvGssNegotiate(
                    pSession->hGssNegotiate,
                    pSecurityBlob,
                    ulSecurityBlobLength,
                    &pReplySecurityBlob,
                    &ulReplySecurityBlobLength);

    BAIL_ON_NT_STATUS(ntStatus);

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

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

    *pSmbResponse->pWordCount = 4;

    if (ulBytesAvailable < sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_4))
    {
        ntStatus = STATUS_INVALID_BUFFER_SIZE;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    pResponseHeader = (PSESSION_SETUP_RESPONSE_HEADER_WC_4)pOutBuffer;

    ulTotalBytesUsed += sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_4);
    ulOffset         += sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_4);
    ulBytesAvailable -= sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_4);
    pOutBuffer       += sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_4);

    /* TODO : change to native domain */

    pResponseHeader->action = 0x0;
    pResponseHeader->securityBlobLength = (USHORT)ulReplySecurityBlobLength;

    ntStatus = MarshallSessionSetupResponseData(
                    pOutBuffer,
                    ulBytesAvailable,
                    ulOffset % 2,
                    &ulBytesUsed,
                    pReplySecurityBlob,
                    pResponseHeader->securityBlobLength,
                    &wszNativeOS[0],
                    &wszNativeLanMan[0],
                    &wszNativeDomain[0]);
    BAIL_ON_NT_STATUS(ntStatus);

    assert(ulBytesUsed <= UINT16_MAX);
    pResponseHeader->byteCount = (USHORT) ulBytesUsed;

    // pOutBuffer       += ulPackageByteCount;
    // ulOffset         += ulPackageByteCount;
    // ulBytesAvailable -= ulPackageByteCount;
    ulTotalBytesUsed += ulBytesUsed;

    if (!SrvGssNegotiateIsComplete(pSession->hGssNegotiate))
    {
        SrvUpdateHeaderError_SMB_V1(
            pSmbResponse->pHeader,
            STATUS_MORE_PROCESSING_REQUIRED);
    }

    pSmbResponse->ulMessageSize = ulTotalBytesUsed;

cleanup:

    if (pReplySecurityBlob)
    {
        SrvFreeMemory(pReplySecurityBlob);
    }

    return ntStatus;

error:

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

    pSmbResponse->ulMessageSize = 0;

    goto cleanup;
}

static
NTSTATUS
SrvMarshallSessionSetupResponse_WC_3(
    PSRV_EXEC_CONTEXT pExecContext
    )
{
    NTSTATUS ntStatus = 0;
    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];
    SESSION_SETUP_RESPONSE_HEADER_WC_3* pResponseHeader = NULL;
    wchar16_t wszNativeOS[] = SRV_NATIVE_OS_W;
    wchar16_t wszNativeLanMan[] = SRV_NATIVE_LAN_MAN_W;
    wchar16_t wszNativeDomain[] = SRV_NATIVE_DOMAIN_W;
    PBYTE pOutBuffer         = pSmbResponse->pBuffer;
    ULONG ulBytesAvailable   = pSmbResponse->ulBytesAvailable;
    ULONG ulOffset           = 0;
    ULONG ulBytesUsed        = 0;
    ULONG ulTotalBytesUsed   = 0;

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

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

    *pSmbResponse->pWordCount = 3;

    if (ulBytesAvailable < sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_3))
    {
        ntStatus = STATUS_INVALID_BUFFER_SIZE;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    pResponseHeader = (PSESSION_SETUP_RESPONSE_HEADER_WC_3)pOutBuffer;

    ulTotalBytesUsed += sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_3);
    ulOffset         += sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_3);
    ulBytesAvailable -= sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_3);
    pOutBuffer       += sizeof(SESSION_SETUP_RESPONSE_HEADER_WC_3);

    ntStatus = MarshallSessionSetupResponseData(
                    pOutBuffer,
                    ulBytesAvailable,
                    ulOffset % 2,
                    &ulBytesUsed,
                    NULL,
                    0,
                    &wszNativeOS[0],
                    &wszNativeLanMan[0],
                    &wszNativeDomain[0]);
    BAIL_ON_NT_STATUS(ntStatus);

    assert(ulBytesUsed <= UINT16_MAX);
    pResponseHeader->action = 0x0;
    pResponseHeader->byteCount = (USHORT) ulBytesUsed;

    ulTotalBytesUsed += ulBytesUsed;

    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;
}

