aboutsummaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/polcutil.cpp
blob: 1fdfa18cc204ff93d9104e679303db739b0561c4 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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"


// Exit macros
#define PolcExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
#define PolcExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
#define PolcExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
#define PolcExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
#define PolcExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
#define PolcExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__)
#define PolcExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_POLCUTIL, p, x, e, s, __VA_ARGS__)
#define PolcExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, p, x, s, __VA_ARGS__)
#define PolcExitOnNullDebugTrace(p, x, e, s, ...)  ExitOnNullDebugTraceSource(DUTIL_SOURCE_POLCUTIL, p, x, e, s, __VA_ARGS__)
#define PolcExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, p, x, s, __VA_ARGS__)
#define PolcExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_POLCUTIL, e, x, s, __VA_ARGS__)
#define PolcExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_POLCUTIL, g, x, s, __VA_ARGS__)

const LPCWSTR REGISTRY_POLICIES_KEY = L"SOFTWARE\\Policies\\";

static HRESULT OpenPolicyKey(
    __in_z LPCWSTR wzPolicyPath,
    __out HKEY* phk
    );


extern "C" HRESULT DAPI PolcReadNumber(
    __in_z LPCWSTR wzPolicyPath,
    __in_z LPCWSTR wzPolicyName,
    __in DWORD dwDefault,
    __out DWORD* pdw
    )
{
    HRESULT hr = S_OK;
    HKEY hk = NULL;

    hr = OpenPolicyKey(wzPolicyPath, &hk);
    if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
    {
        ExitFunction1(hr = S_FALSE);
    }
    PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath);

    hr = RegReadNumber(hk, wzPolicyName, pdw);
    if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
    {
        ExitFunction1(hr = S_FALSE);
    }
    PolcExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName);

LExit:
    ReleaseRegKey(hk);

    if (S_FALSE == hr || FAILED(hr))
    {
        *pdw = dwDefault;
    }

    return hr;
}

extern "C" HRESULT DAPI PolcReadString(
    __in_z LPCWSTR wzPolicyPath,
    __in_z LPCWSTR wzPolicyName,
    __in_z_opt LPCWSTR wzDefault,
    __deref_out_z LPWSTR* pscz
    )
{
    HRESULT hr = S_OK;
    HKEY hk = NULL;

    hr = OpenPolicyKey(wzPolicyPath, &hk);
    if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
    {
        ExitFunction1(hr = S_FALSE);
    }
    PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath);

    hr = RegReadString(hk, wzPolicyName, pscz);
    if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr)
    {
        ExitFunction1(hr = S_FALSE);
    }
    PolcExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName);

LExit:
    ReleaseRegKey(hk);

    if (S_FALSE == hr || FAILED(hr))
    {
        if (NULL == wzDefault)
        {
            ReleaseNullStr(*pscz);
        }
        else
        {
            hr = StrAllocString(pscz, wzDefault, 0);
        }
    }

    return hr;
}


// internal functions

static HRESULT OpenPolicyKey(
    __in_z LPCWSTR wzPolicyPath,
    __out HKEY* phk
    )
{
    HRESULT hr = S_OK;
    LPWSTR sczPath = NULL;

    hr = PathConcat(REGISTRY_POLICIES_KEY, wzPolicyPath, &sczPath);
    PolcExitOnFailure(hr, "Failed to combine logging path with root path.");

    hr = RegOpen(HKEY_LOCAL_MACHINE, sczPath, KEY_READ, phk);
    PolcExitOnFailure(hr, "Failed to open policy registry key.");

LExit:
    ReleaseStr(sczPath);

    return hr;
}