| 1 | // 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. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | |
| 6 | // Exit macros |
| 7 | #define PolcExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__) |
| 8 | #define PolcExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__) |
| 9 | #define PolcExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__) |
| 10 | #define PolcExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__) |
| 11 | #define PolcExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__) |
| 12 | #define PolcExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_POLCUTIL, x, s, __VA_ARGS__) |
| 13 | #define PolcExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_POLCUTIL, p, x, e, s, __VA_ARGS__) |
| 14 | #define PolcExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, p, x, s, __VA_ARGS__) |
| 15 | #define PolcExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_POLCUTIL, p, x, e, s, __VA_ARGS__) |
| 16 | #define PolcExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, p, x, s, __VA_ARGS__) |
| 17 | #define PolcExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_POLCUTIL, e, x, s, __VA_ARGS__) |
| 18 | #define PolcExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_POLCUTIL, g, x, s, __VA_ARGS__) |
| 19 | #define PolcExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_POLCUTIL, x, b, s, __VA_ARGS__) |
| 20 | |
| 21 | const LPCWSTR REGISTRY_POLICIES_KEY = L"SOFTWARE\\Policies\\"; |
| 22 | |
| 23 | static HRESULT OpenPolicyKey( |
| 24 | __in_z LPCWSTR wzPolicyPath, |
| 25 | __out HKEY* phk |
| 26 | ); |
| 27 | |
| 28 | |
| 29 | extern "C" HRESULT DAPI PolcReadNumber( |
| 30 | __in_z LPCWSTR wzPolicyPath, |
| 31 | __in_z LPCWSTR wzPolicyName, |
| 32 | __in DWORD dwDefault, |
| 33 | __out DWORD* pdw |
| 34 | ) |
| 35 | { |
| 36 | HRESULT hr = S_OK; |
| 37 | HKEY hk = NULL; |
| 38 | BOOL fExists = FALSE; |
| 39 | |
| 40 | hr = OpenPolicyKey(wzPolicyPath, &hk); |
| 41 | PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); |
| 42 | |
| 43 | if (!hk) |
| 44 | { |
| 45 | ExitFunction1(hr = S_FALSE); |
| 46 | } |
| 47 | |
| 48 | hr = RegReadNumber(hk, wzPolicyName, pdw); |
| 49 | PolcExitOnPathFailure(hr, fExists, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); |
| 50 | |
| 51 | if (!fExists) |
| 52 | { |
| 53 | ExitFunction1(hr = S_FALSE); |
| 54 | } |
| 55 | |
| 56 | LExit: |
| 57 | ReleaseRegKey(hk); |
| 58 | |
| 59 | if (!fExists) |
| 60 | { |
| 61 | *pdw = dwDefault; |
| 62 | } |
| 63 | |
| 64 | return hr; |
| 65 | } |
| 66 | |
| 67 | extern "C" HRESULT DAPI PolcReadString( |
| 68 | __in_z LPCWSTR wzPolicyPath, |
| 69 | __in_z LPCWSTR wzPolicyName, |
| 70 | __in_z_opt LPCWSTR wzDefault, |
| 71 | __deref_out_z LPWSTR* pscz |
| 72 | ) |
| 73 | { |
| 74 | HRESULT hr = S_OK; |
| 75 | HKEY hk = NULL; |
| 76 | BOOL fExists = FALSE; |
| 77 | |
| 78 | hr = OpenPolicyKey(wzPolicyPath, &hk); |
| 79 | PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); |
| 80 | |
| 81 | if (!hk) |
| 82 | { |
| 83 | ExitFunction1(hr = S_FALSE); |
| 84 | } |
| 85 | |
| 86 | hr = RegReadString(hk, wzPolicyName, pscz); |
| 87 | PolcExitOnPathFailure(hr, fExists, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); |
| 88 | |
| 89 | if (!fExists) |
| 90 | { |
| 91 | ExitFunction1(hr = S_FALSE); |
| 92 | } |
| 93 | |
| 94 | LExit: |
| 95 | ReleaseRegKey(hk); |
| 96 | |
| 97 | if (!fExists) |
| 98 | { |
| 99 | if (NULL == wzDefault) |
| 100 | { |
| 101 | ReleaseNullStr(*pscz); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | hr = StrAllocString(pscz, wzDefault, 0); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return hr; |
| 110 | } |
| 111 | |
| 112 | extern "C" HRESULT DAPI PolcReadUnexpandedString( |
| 113 | __in_z LPCWSTR wzPolicyPath, |
| 114 | __in_z LPCWSTR wzPolicyName, |
| 115 | __in_z_opt LPCWSTR wzDefault, |
| 116 | __inout BOOL* pfNeedsExpansion, |
| 117 | __deref_out_z LPWSTR* pscz |
| 118 | ) |
| 119 | { |
| 120 | HRESULT hr = S_OK; |
| 121 | HKEY hk = NULL; |
| 122 | BOOL fExists = FALSE; |
| 123 | |
| 124 | hr = OpenPolicyKey(wzPolicyPath, &hk); |
| 125 | PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); |
| 126 | |
| 127 | if (!hk) |
| 128 | { |
| 129 | ExitFunction1(hr = S_FALSE); |
| 130 | } |
| 131 | |
| 132 | hr = RegReadUnexpandedString(hk, wzPolicyName, pfNeedsExpansion, pscz); |
| 133 | PolcExitOnPathFailure(hr, fExists, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); |
| 134 | |
| 135 | if (!fExists) |
| 136 | { |
| 137 | ExitFunction1(hr = S_FALSE); |
| 138 | } |
| 139 | |
| 140 | LExit: |
| 141 | ReleaseRegKey(hk); |
| 142 | |
| 143 | if (!fExists) |
| 144 | { |
| 145 | if (NULL == wzDefault) |
| 146 | { |
| 147 | ReleaseNullStr(*pscz); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | hr = StrAllocString(pscz, wzDefault, 0); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return hr; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | // internal functions |
| 160 | |
| 161 | static HRESULT OpenPolicyKey( |
| 162 | __in_z LPCWSTR wzPolicyPath, |
| 163 | __out HKEY* phk |
| 164 | ) |
| 165 | { |
| 166 | HRESULT hr = S_OK; |
| 167 | LPWSTR sczPath = NULL; |
| 168 | BOOL fExists = FALSE; |
| 169 | |
| 170 | hr = PathConcat(REGISTRY_POLICIES_KEY, wzPolicyPath, &sczPath); |
| 171 | PolcExitOnFailure(hr, "Failed to combine logging path with root path."); |
| 172 | |
| 173 | hr = RegOpen(HKEY_LOCAL_MACHINE, sczPath, KEY_READ, phk); |
| 174 | PolcExitOnPathFailure(hr, fExists, "Failed to open policy registry key."); |
| 175 | |
| 176 | if (!fExists) |
| 177 | { |
| 178 | ReleaseRegKey(*phk); |
| 179 | } |
| 180 | |
| 181 | LExit: |
| 182 | ReleaseStr(sczPath); |
| 183 | |
| 184 | return hr; |
| 185 | } |