/* Editor Settings: expandtabs and use 4 spaces for indentation
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 */

/*
 * Copyright Likewise Software    2004-2008
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the license, or (at
 * your option) any later version.
 *
 * This library 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 Lesser
 * General Public License for more details.  You should have received a copy
 * of the GNU Lesser 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
 * LESSER 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
 */

/*
 * Copyright (C) Likewise Software. All rights reserved.
 *
 * Module Name:
 *
 *        samr_changepassworduser2.c
 *
 * Abstract:
 *
 *        Remote Procedure Call (RPC) Server Interface
 *
 *        SamrSrvChangePasswordUser2 function
 *
 * Authors: Rafal Szczesniak (rafal@likewise.com)
 */

#include "includes.h"


NTSTATUS
SamrSrvChangePasswordUser2(
    /* [in] */ handle_t         hBinding,
    /* [in] */ UNICODE_STRING  *pDomainName,
    /* [in] */ UNICODE_STRING  *pAccountName,
    /* [in] */ CryptPassword   *pNtPasswordBlob,
    /* [in] */ HashPassword    *pNtVerifier,
    /* [in] */ UINT8            ussLmChange,
    /* [in] */ CryptPassword   *pLmPasswordBlob,
    /* [in] */ HashPassword    *pLmVerifier
    )
{
    NTSTATUS ntStatus = STATUS_SUCCESS;
    DWORD dwError = ERROR_SUCCESS;
    PWSTR pwszUserName = NULL;
    PWSTR pwszDomainName = NULL;
    WCHAR wszSystemName[] = { '\\', '\\', 0 };
    DWORD dwConnectFlags = SAMR_ACCESS_CONNECT_TO_SERVER;
    PCONNECT_CONTEXT pConnCtx = NULL;
    SAMDB_QUERY query = {0};
    PSAMDB_OBJECT_INFO* userObjects = NULL;
    PSAMDB_OBJECT_INFO userObject = NULL;
    SECURITY_INFORMATION securityInformation = 0;
    PSECURITY_DESCRIPTOR_RELATIVE pSecDescRelative = NULL;
    PSECURITY_DESCRIPTOR_ABSOLUTE pSecDesc = NULL;
    GENERIC_MAPPING GenericMapping = {0};
    DWORD dwAccessGranted = 0;
    PWSTR pwszNewPassword = NULL;
    size_t sNewPasswordLen = 0;
    SAMDB_OWF_PASSWORD newNtOwfPassword = {0};
    SAMDB_FIELDS updateFields = 0;

    BAIL_ON_INVALID_PTR(hBinding);
    BAIL_ON_INVALID_PTR(pDomainName);
    BAIL_ON_INVALID_PTR(pAccountName);
    BAIL_ON_INVALID_PTR(pNtPasswordBlob);
    BAIL_ON_INVALID_PTR(pNtVerifier);

    ntStatus = SamrSrvConnect2(hBinding,
                               (PWSTR)wszSystemName,
                               dwConnectFlags,
                               (CONNECT_HANDLE*)&pConnCtx);
    BAIL_ON_NT_STATUS(ntStatus);

    if (pConnCtx->pUserToken == NULL)
    {
        ntStatus = STATUS_ACCESS_DENIED;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    dwError = LwAllocateWc16StringFromUnicodeString(
                                   &pwszDomainName,
                                   pDomainName);
    BAIL_ON_LSA_ERROR(dwError);

    dwError = LwAllocateWc16StringFromUnicodeString(
                                   &pwszUserName,
                                   pAccountName);
    BAIL_ON_LSA_ERROR(dwError);

    query.Name = pwszUserName;

    dwError = SamDbFindObjects(
                        SAMDB_OBJECT_TYPE_USER,
                        SAMDB_QUERY_TYPE_NT_NAME,
                        1,
                        &query,
                        &userObjects);
    BAIL_ON_LSA_ERROR(dwError);

    userObject = userObjects[0];
    if (userObject == NULL)
    {
        ntStatus = STATUS_NO_SUCH_USER;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    /*
     * Access check if requesting user is allowed to change password
     */

    securityInformation =
        OWNER_SECURITY_INFORMATION |
        GROUP_SECURITY_INFORMATION |
        DACL_SECURITY_INFORMATION;

    dwError = SamDbQuerySecurityDescriptor(
                                   userObject->Sid,
                                   securityInformation,
                                   &pSecDescRelative);
    BAIL_ON_LSA_ERROR(dwError);

    dwError = SamrSrvAllocateAbsoluteFromSelfRelativeSD(
                                   pSecDescRelative,
                                   &pSecDesc);
    BAIL_ON_LSA_ERROR(dwError);

    if (!RtlAccessCheck(pSecDesc,
                        pConnCtx->pUserToken,
                        USER_ACCESS_CHANGE_PASSWORD,
                        0,
                        &GenericMapping,
                        &dwAccessGranted,
                        &ntStatus))
    {
        BAIL_ON_NT_STATUS(ntStatus);
    }

    /*
     * Decrypt the password with old NT hash
     */
    ntStatus = SamrSrvDecryptPasswordBlob(
                               pConnCtx,
                               pNtPasswordBlob,
                               userObject->Info.User.NtOwfPassword.Data,
                               sizeof(userObject->Info.User.NtOwfPassword.Data),
                               0,
                               &pwszNewPassword);
    BAIL_ON_NT_STATUS(ntStatus);

    dwError = LwWc16sLen(pwszNewPassword, &sNewPasswordLen);
    BAIL_ON_LSA_ERROR(dwError);

    /*
     * Calculate new NT hash
     */

    ntStatus = SamrCreateNtOwfPassword(
                               pwszNewPassword,
                               newNtOwfPassword.Data,
                               sizeof(newNtOwfPassword.Data));
    BAIL_ON_NT_STATUS(ntStatus);

    newNtOwfPassword.IsPresent = TRUE;

    /*
     * Verify new NT hash
     */
    ntStatus = SamrSrvVerifyNewNtPasswordHash(
                        newNtOwfPassword.Data,
                        sizeof(newNtOwfPassword.Data),
                        userObject->Info.User.NtOwfPassword.Data,
                        sizeof(userObject->Info.User.NtOwfPassword.Data),
                        pNtVerifier);
    BAIL_ON_NT_STATUS(ntStatus);

    /*
     * Set the password
     */

    RtlCopyMemory(&userObject->Info.User.NtOwfPassword,
                  &newNtOwfPassword,
                  sizeof(userObject->Info.User.NtOwfPassword));
    updateFields |= SAMDB_FIELD_USER_NT_OWF_PASSWORD;

    dwError = SamDbUpdateObject(
                        userObject,
                        updateFields);
    BAIL_ON_LSA_ERROR(dwError);

error:
    SamrSrvClose(hBinding,
                 (PVOID*)&pConnCtx);

    if (userObjects)
    {
        SamDbFreeObjects(1, userObjects);
    }

    if (pSecDescRelative)
    {
        SamDbFree(pSecDescRelative);
    }

    if (pSecDesc)
    {
        SamrSrvFreeSecurityDescriptor(&pSecDesc);
    }

    LW_SAFE_FREE_MEMORY(pwszDomainName);
    LW_SAFE_FREE_MEMORY(pwszUserName);
    LW_SECURE_FREE_WSTRING(pwszNewPassword);

    if (ntStatus == STATUS_SUCCESS &&
        dwError != ERROR_SUCCESS)
    {
        ntStatus = LwWin32ErrorToNtStatus(dwError);
    }

    return ntStatus;
}


/*
local variables:
mode: c
c-basic-offset: 4
indent-tabs-mode: nil
tab-width: 4
end:
*/
