/* -*- 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
 */

/*
 * Copyright (C) Likewise Software. All rights reserved.
 *
 * Module Name:
 *
 *        samdbmisc.c
 *
 * Abstract:
 *
 *        Likewise SAM Database Provider
 *
 *        Misc Functions
 *
 * Authors: Krishna Ganugapati (krishnag@likewisesoftware.com)
 *          Sriram Nambakam (snambakam@likewisesoftware.com)
 */

#include "includes.h"


static
BOOLEAN
SamDbIsValidName(
    PWSTR Name,
    DWORD MaxLength,
    CHAR* InvalidCharacters,
    DWORD InvalidCharactersLength,
    size_t* Length
    );


DWORD
SamDbComputeLMHash(
    PCSTR Password,
    PBYTE Hash,
    DWORD HashLength
    )
{
    DWORD error = ERROR_SUCCESS;

    if (!Hash || HashLength != 16)
    {
        error = ERROR_INVALID_PARAMETER;

        SAMDB_LOG_ERROR("NULL or invalid length (%u) buffer passed "
                        "for LM password hash",
                        HashLength);
        goto cleanup;
    }

    memset(Hash, 0, HashLength);

cleanup:
    return error;
}


DWORD
SamDbComputeNTHash(
    PCWSTR Password,
    PBYTE Hash,
    DWORD HashLength
    )
{
    DWORD error = ERROR_SUCCESS;
    size_t passwordLength = 0;

    if (!Hash || HashLength != 16)
    {
        error = ERROR_INVALID_PARAMETER;

        SAMDB_LOG_ERROR("NULL or invalid length (%u) buffer passed "
                        "for LM password hash",
                        HashLength);
        goto cleanup;
    }

    memset(Hash, 0, HashLength);
    if (Password)
    {
        error = LwWc16sLen(
                      Password,
                      &passwordLength);
        BAIL_ON_SAMDB_ERROR(error);

        MD4((PBYTE)Password,
            sizeof(Password[0]) * passwordLength,
            Hash);
    }

cleanup:
    return error;
}


LONG64
SamDbGetNTTime(
    time_t timeVal
    )
{
    return (timeVal + 11644473600LL) * 10000000LL;
}


static
BOOLEAN
SamDbIsValidName(
    PWSTR Name,
    DWORD MaxLength,
    CHAR* InvalidCharacters,
    DWORD InvalidCharactersLength,
    size_t* Length
    )
{
    BOOLEAN valid = TRUE;
    DWORD error = ERROR_SUCCESS;
    size_t nameLength = 0;
    DWORD index = 0;
    DWORD charIndex = 0;

    error = LwWc16sLen(
                    Name,
                    &nameLength);
    BAIL_ON_SAMDB_ERROR(error);

    if (nameLength > MaxLength)
    {
        valid = FALSE;
        goto cleanup;
    }

    for (index = 0; (size_t)index < nameLength; index++)
    {
        for (charIndex = 0;
             charIndex < InvalidCharactersLength;
             charIndex++)
        {
            if ((CHAR)Name[index] == InvalidCharacters[charIndex])
            {
                valid = FALSE;
                goto cleanup;
            }
        }
    }

cleanup:
    if (error)
    {
        valid = FALSE;
    }

    if (Length)
    {
        *Length = nameLength;
    }

    return valid;
}


BOOLEAN
SamDbIsValidNetBiosDomainName(
    PWSTR DomainName
    )
{
    BOOLEAN valid = TRUE;
    size_t domainNameMaxLength = 15;
    CHAR invalidCharacters[] = {'"','/','[',']',':','|',
                                '<','>','+','=',';','?',
                                ',','\\',' '};
    valid = SamDbIsValidName(
                   DomainName,
                   domainNameMaxLength,
                   invalidCharacters,
                   sizeof(invalidCharacters)/sizeof(invalidCharacters[0]),
                   NULL);

    return valid;
}


BOOLEAN
SamDbIsValidUserName(
    PWSTR UserName
    )
{
    BOOLEAN valid = TRUE;
    size_t userNameMaxLength = 20;
    size_t userNameLength = 0;
    CHAR invalidCharacters[] = {'"','/','[',']',':','|',
                                '<','>','+','=',';','?',
                                ',','\\'};
    valid = SamDbIsValidName(
                   UserName,
                   userNameMaxLength,
                   invalidCharacters,
                   sizeof(invalidCharacters)/sizeof(invalidCharacters[0]),
                   &userNameLength);

    // The last character in SamAccountName cannot be '.' (period)
    valid &= (UserName[(DWORD)(userNameLength - 1)] != '.');

    return valid;
}


BOOLEAN
SamDbIsValidLocalGroupName(
    PWSTR LocalGroupName
    )
{
    BOOLEAN valid = TRUE;
    size_t localGroupNameMaxLength = 256;
    size_t localGroupNameLength = 0;
    CHAR invalidCharacters[] = {'"','/','[',']',':','|',
                                '<','>','+','=',';','?',
                                ',','\\'};
    valid = SamDbIsValidName(
                   LocalGroupName,
                   localGroupNameMaxLength,
                   invalidCharacters,
                   sizeof(invalidCharacters)/sizeof(invalidCharacters[0]),
                   &localGroupNameLength);

    // The last character in SamAccountName cannot be '.' (period)
    valid &= (LocalGroupName[(DWORD)(localGroupNameLength - 1)] != '.');

    return valid;
}
