DWORD
LwpsLegacyGetDefaultJoinedDomain(
    IN PLWPS_LEGACY_STATE pContext,
    OUT PSTR* ppszDomainName
    )
{
    DWORD dwError = 0;
    int EE = 0;
    PSTR pszDomainName = NULL;
    DWORD dwValueLen = 0;

    dwError = RegUtilGetValue(
                  pContext->hReg,
                  HKEY_THIS_MACHINE,
                  PSTOREDB_REGISTRY_AD_KEY,
                  NULL,
                  PSTOREDB_REGISTRY_DEFAULT_VALUE,
                  NULL,
                  (PVOID) &pszDomainName,
                  &dwValueLen);
    if (dwError == LWREG_ERROR_NO_SUCH_KEY_OR_VALUE)
    {
        // Treat as no default domain set
        assert(!pszDomainName);
        dwError = 0;
        GOTO_CLEANUP_EE(EE);
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LwpsLegacyReadPassword(
                    pContext,
                    pszDomainName,
                    NULL);
    if (dwError == NERR_SetupNotJoined)
    {
        // TODO-INCONSISTENT-FREE-FUNCTION!!!
        LW_SAFE_FREE_MEMORY(pszDomainName);
        dwError = 0;
        GOTO_CLEANUP_EE(EE);
    }
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

cleanup:
    if (dwError)
    {
        // TODO-INCONSISTENT-FREE-FUNCTION!!!
        LW_SAFE_FREE_MEMORY(pszDomainName);
    }

    *ppszDomainName = pszDomainName;

    LSA_PSTORE_LOG_LEAVE_ERROR_EE(dwError, EE);
    return dwError;
}
DWORD
LsaPstorepBackendGetPasswordInfoW(
    IN PLSA_PSTORE_BACKEND_STATE State,
    IN PCWSTR DnsDomainName,
    OUT OPTIONAL PLSA_MACHINE_PASSWORD_INFO_W* PasswordInfo
    )
{
    DWORD dwError = 0;
    int EE = 0;
    PSTR dnsDomainNameA = NULL;
    PLSA_MACHINE_PASSWORD_INFO_W passwordInfo = NULL;
    PLSA_MACHINE_PASSWORD_INFO_A passwordInfoA = NULL;

    dwError = LwNtStatusToWin32Error(LwRtlCStringAllocateFromWC16String(
                    &dnsDomainNameA,
                    DnsDomainName));
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LwpsLegacyReadPassword(
                    State->OldStoreHandle,
                    dnsDomainNameA,
                    &passwordInfoA);
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    dwError = LsaPstorepConvertAnsiToWidePasswordInfo(
                    passwordInfoA,
                    &passwordInfo);
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

cleanup:
    if (dwError)
    {
        LSA_PSTORE_FREE_PASSWORD_INFO_W(&passwordInfo);
    }

    LSA_PSTORE_FREE_PASSWORD_INFO_A(&passwordInfoA);
    LW_RTL_FREE(&dnsDomainNameA);

    if (PasswordInfo)
    {
        *PasswordInfo = passwordInfo;
    }
    else
    {
        LSA_PSTORE_FREE_PASSWORD_INFO_W(&passwordInfo);
    }

    LSA_PSTORE_LOG_LEAVE_ERROR_EE(dwError, EE);
    return dwError;
}
DWORD
LwpsLegacySetDefaultJoinedDomain(
    IN PLWPS_LEGACY_STATE pContext,
    IN OPTIONAL PCSTR pszDomainName
    )
{
    DWORD dwError = 0;
    int EE = 0;

    if (pszDomainName)
    {
        dwError = LwpsLegacyReadPassword(
                        pContext,
                        pszDomainName,
                        NULL);
        GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

        dwError = RegUtilAddKey(
                      pContext->hReg,
                      HKEY_THIS_MACHINE,
                      PSTOREDB_REGISTRY_AD_KEY,
                      NULL);
        GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

        dwError = RegUtilSetValue(
                      pContext->hReg,
                      HKEY_THIS_MACHINE,
                      PSTOREDB_REGISTRY_AD_KEY,
                      NULL,
                      PSTOREDB_REGISTRY_DEFAULT_VALUE,
                      REG_SZ,
                      (PVOID)pszDomainName,
                      pszDomainName ? strlen(pszDomainName) : 0);
        GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);
    }
    else
    {
        dwError = RegUtilDeleteValue(
                      pContext->hReg,
                      HKEY_THIS_MACHINE,
                      PSTOREDB_REGISTRY_AD_KEY,
                      NULL,
                      PSTOREDB_REGISTRY_DEFAULT_VALUE);
        GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);
    }

cleanup:
    LSA_PSTORE_LOG_LEAVE_ERROR_EE(dwError, EE);
    return dwError;
}
DWORD
LwpsLegacyGetJoinedDomains(
    IN PLWPS_LEGACY_STATE pContext,
    OUT PSTR** pppszDomainList,
    OUT PDWORD pdwDomainCount
    )
{
    DWORD dwError = 0;
    int EE = 0;
    PWSTR* ppwszSubKeys = NULL;
    PSTR pszSubKey = NULL;
    PSTR pszSubKeyPtr = NULL;
    PSTR* ppszDomainList = NULL;
    DWORD dwSubKeysLen = 0;
    DWORD dwIndexKeys = 0;
    DWORD dwIndexDomains = 0;
    DWORD dwDomainCount = 0;

    dwError = RegUtilIsValidKey(
                    pContext->hReg,
                    HKEY_THIS_MACHINE,
                    PSTOREDB_REGISTRY_AD_KEY);
    if (dwError)
    {
        dwError = 0;
        GOTO_CLEANUP_EE(EE);
    }

    dwError = RegUtilGetKeys(
                    pContext->hReg,
                    HKEY_THIS_MACHINE,
                    PSTOREDB_REGISTRY_AD_KEY,
                    NULL,
                    &ppwszSubKeys,
                    &dwSubKeysLen);
    GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

    if (dwSubKeysLen)
    {
        dwError = LwAllocateMemory(
                        sizeof(*ppszDomainList) * dwSubKeysLen,
                        (PVOID*)&ppszDomainList);
        GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

        for (dwIndexKeys = 0, dwIndexDomains = 0 ; dwIndexKeys < dwSubKeysLen; dwIndexKeys++)
        {
            dwError = LwWc16sToMbs(ppwszSubKeys[dwIndexKeys], &pszSubKey);
            GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

            pszSubKeyPtr = strrchr(pszSubKey, '\\');
            if (pszSubKeyPtr)
            {
                dwError = LwAllocateString(
                              ++pszSubKeyPtr,
                              &ppszDomainList[dwIndexDomains]);
                GOTO_CLEANUP_ON_WINERROR_EE(dwError, EE);

                LW_SAFE_FREE_STRING(pszSubKey);
            }
            else
            {
                ppszDomainList[dwIndexDomains] = pszSubKey;
                pszSubKey = NULL;
            }

            dwError = LwpsLegacyReadPassword(
                            pContext,
                            ppszDomainList[dwIndexDomains],
                            NULL);
            if (dwError == NERR_SetupNotJoined)
            {
                // The domain is not joined.
                dwError = 0;
                LW_SAFE_FREE_STRING(ppszDomainList[dwIndexDomains]);
            }
            else
            {
                dwIndexDomains++;
            }
        }
    }

    dwDomainCount = dwIndexDomains;

cleanup:
    if (dwError)
    {
        if (ppszDomainList)
        {
            LwpsLegacyFreeStringArray(ppszDomainList, dwSubKeysLen);
            ppszDomainList = NULL;
        }
        dwDomainCount = 0;
    }

    for (dwIndexKeys = 0; dwIndexKeys < dwSubKeysLen; dwIndexKeys++)
    {
        LW_SAFE_FREE_MEMORY(ppwszSubKeys[dwIndexKeys]);
    }
    LW_SAFE_FREE_MEMORY(ppwszSubKeys);

    LW_SAFE_FREE_STRING(pszSubKey);

    *pppszDomainList = ppszDomainList;
    *pdwDomainCount = dwDomainCount;

    LSA_PSTORE_LOG_LEAVE_ERROR_EE(dwError, EE);
    return dwError;
}