aboutsummaryrefslogtreecommitdiff
path: root/src/ext/Util/ca/utilca.cpp
blob: d41f00c2932946e05d3df895a59d6b5147f1376d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"

HRESULT GetDomainFromServerName(
    __deref_out_z LPWSTR* ppwzDomainName,
    __in_z LPCWSTR wzServerName,
    __in DWORD dwFlags
    )
{
    HRESULT hr = S_OK;
    DWORD er = ERROR_SUCCESS;
    PDOMAIN_CONTROLLER_INFOW pDomainControllerInfo = NULL;
    LPCWSTR wz = wzServerName ? wzServerName : L""; // initialize the domain to the provided server name (or empty string).

    // If the server name was not empty, try to get the domain name out of it.
    if (*wz)
    {
        er = ::DsGetDcNameW(NULL, wz, NULL, NULL, dwFlags, &pDomainControllerInfo);
        if (RPC_S_SERVER_UNAVAILABLE == er)
        {
            // MSDN says, if we get the above error code, try again with the "DS_FORCE_REDISCOVERY" flag.
            er = ::DsGetDcNameW(NULL, wz, NULL, NULL, dwFlags | DS_FORCE_REDISCOVERY, &pDomainControllerInfo);
        }
        ExitOnWin32Error(er, hr, "Could not get domain name from server name: %ls", wz);

        if (pDomainControllerInfo->DomainControllerName)
        {
            // Skip the \\ prefix if present.
            if ('\\' == *pDomainControllerInfo->DomainControllerName && '\\' == *(pDomainControllerInfo->DomainControllerName + 1))
            {
                wz = pDomainControllerInfo->DomainControllerName + 2;
            }
            else
            {
                wz = pDomainControllerInfo->DomainControllerName;
            }
        }
    }

LExit:
    // Note: we overwrite the error code here as failure to contact domain controller above is not a fatal error.
    if (wz && *wz)
    {
        hr = StrAllocString(ppwzDomainName, wz, 0);
    }
    else // return NULL the server name ended up empty.
    {
        ReleaseNullStr(*ppwzDomainName);
        hr = S_OK;
    }

    if (pDomainControllerInfo)
    {
        ::NetApiBufferFree((LPVOID)pDomainControllerInfo);
    }

    return hr;
}