DWORD
LsaPstorepBackendInitialize(
    OUT PLSA_PSTORE_BACKEND_STATE* State
    )
{
    DWORD dwError = 0;
    int EE = 0;
    PLSA_PSTORE_BACKEND_STATE state = NULL;

    dwError = LSA_PSTORE_ALLOCATE_AUTO(&state);
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LwpsLegacyOpenProvider(&state->OldStoreHandle);
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

cleanup:
    if (dwError)
    {
        LsaPstorepBackendCleanup(state);
        state = NULL;
    }

    *State = state;

    LSA_PSTORE_LOG_LEAVE_ERROR_EE(dwError, EE);
    return dwError;
}
DWORD
LwpsLegacyReadPassword(
    IN PLWPS_LEGACY_STATE pContext,
    IN PCSTR pszDnsDomainName,
    OUT OPTIONAL PLSA_MACHINE_PASSWORD_INFO_A* ppPasswordInfo
    )
{
    DWORD dwError = 0;
    int EE = 0;
    PLSA_MACHINE_PASSWORD_INFO_A pPasswordInfo = NULL;
    HKEY rootKeyHandle = NULL;
    HKEY accountKeyHandle = NULL;
    HKEY passwordKeyHandle = NULL;
    PSTR pszRegistryPath = NULL;
    PSTR pszDefaultDomain = NULL;
    DWORD unixLastChangeTime = 0;

    //
    // Compute registry path
    //

    dwError = LwAllocateStringPrintf(
                  &pszRegistryPath,
                  "%s\\%s\\%s",
                  PSTOREDB_REGISTRY_AD_KEY,
                  pszDnsDomainName,
                  PSTOREDB_REGISTRY_PSTORE_SUBKEY);
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    //
    // Open keys
    //

    dwError = LwRegOpenKeyExA(
                    pContext->hReg,
                    NULL,
                    HKEY_THIS_MACHINE,
                    0,
                    KEY_READ,
                    &rootKeyHandle);
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LwRegOpenKeyExA(
                    pContext->hReg,
                    rootKeyHandle,
                    pszRegistryPath,
                    0,
                    KEY_READ,
                    &accountKeyHandle);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LwRegOpenKeyExA(
                    pContext->hReg,
                    accountKeyHandle,
                    PSTOREDB_REGISTRY_PASSWORD_INFO_SUBKEY,
                    0,
                    KEY_READ,
                    &passwordKeyHandle);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    //
    // Allocate info structure
    //

    dwError = LSA_PSTORE_ALLOCATE_AUTO(&pPasswordInfo);
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    //
    // Read account portion
    //

    dwError = LsaPstorepRegGetStringA(
                    pContext->hReg,
                    accountKeyHandle,
                    LWPS_REG_DNS_DOMAIN_NAME,
                    &pPasswordInfo->Account.DnsDomainName);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LsaPstorepRegGetStringA(
                    pContext->hReg,
                    accountKeyHandle,
                    LWPS_REG_NETBIOS_DOMAIN_NAME,
                    &pPasswordInfo->Account.NetbiosDomainName);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LsaPstorepRegGetStringA(
                    pContext->hReg,
                    accountKeyHandle,
                    LWPS_REG_DOMAIN_SID,
                    &pPasswordInfo->Account.DomainSid);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LsaPstorepRegGetStringA(
                    pContext->hReg,
                    accountKeyHandle,
                    LWPS_REG_SAM_ACCOUNT_NAME,
                    &pPasswordInfo->Account.SamAccountName);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LsaPstorepRegGetDword(
                    pContext->hReg,
                    accountKeyHandle,
                    LWPS_REG_ACCOUNT_FLAGS,
                    &pPasswordInfo->Account.AccountFlags);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LsaPstorepRegGetDword(
                    pContext->hReg,
                    accountKeyHandle,
                    LWPS_REG_KEY_VERSION_NUMBER,
                    &pPasswordInfo->Account.KeyVersionNumber);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LsaPstorepRegGetStringA(
                    pContext->hReg,
                    accountKeyHandle,
                    LWPS_REG_FQDN,
                    &pPasswordInfo->Account.Fqdn);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LsaPstorepRegGetDword(
                    pContext->hReg,
                    accountKeyHandle,
                    LWPS_REG_UNIX_LAST_CHANGE_TIME,
                    &unixLastChangeTime);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LwpsConvertTimeUnixToWindows(
                    (time_t) unixLastChangeTime,
                    &pPasswordInfo->Account.LastChangeTime);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    //
    // Read password portion
    //

    dwError = LsaPstorepRegGetStringA(
                    pContext->hReg,
                    passwordKeyHandle,
                    LWPS_REG_PASSWORD,
                    &pPasswordInfo->Password);
    if (LWREG_ERROR_NO_SUCH_KEY_OR_VALUE == dwError)
    {
        dwError = NERR_SetupNotJoined;
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

cleanup:
    if (dwError)
    {
        LSA_PSTORE_FREE_PASSWORD_INFO_A(&pPasswordInfo);
    }

    if (passwordKeyHandle)
    {
        LwRegCloseKey(pContext->hReg, passwordKeyHandle);
    }
    if (accountKeyHandle)
    {
        LwRegCloseKey(pContext->hReg, accountKeyHandle);
    }
    if (rootKeyHandle)
    {
        LwRegCloseKey(pContext->hReg, rootKeyHandle);
    }

    LW_SAFE_FREE_MEMORY(pszRegistryPath);
    LW_SAFE_FREE_MEMORY(pszDefaultDomain);

    if (ppPasswordInfo)
    {
        *ppPasswordInfo = pPasswordInfo;
    }
    else
    {
        LSA_PSTORE_FREE_PASSWORD_INFO_A(&pPasswordInfo);
    }

    LSA_PSTORE_LOG_LEAVE_ERROR_EE(dwError, EE);
    return dwError;
}