-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathDelegationConfig.cpp
307 lines (244 loc) · 12.2 KB
/
DelegationConfig.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "DelegationConfig.hpp"
#include "RegistrySerialization.hpp"
#include <wil/resource.h>
#include <wil/winrt.h>
#include <windows.foundation.collections.h>
#include <Windows.ApplicationModel.h>
#include <Windows.ApplicationModel.AppExtensions.h>
#include "../inc/conint.h"
#include <initguid.h>
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::ApplicationModel;
using namespace ABI::Windows::ApplicationModel::AppExtensions;
#pragma hdrstop
#define DELEGATION_CONSOLE_KEY_NAME L"DelegationConsole"
#define DELEGATION_TERMINAL_KEY_NAME L"DelegationTerminal"
#define DELEGATION_CONSOLE_EXTENSION_NAME L"com.microsoft.windows.console.host"
#define DELEGATION_TERMINAL_EXTENSION_NAME L"com.microsoft.windows.terminal.host"
[[nodiscard]] static HRESULT _lookupCatalog(PCWSTR extensionName, std::vector<DelegationConfig::DelegationBase>& vec) noexcept
{
ComPtr<IAppExtensionCatalogStatics> catalogStatics;
RETURN_IF_FAILED(Windows::Foundation::GetActivationFactory(HStringReference(RuntimeClass_Windows_ApplicationModel_AppExtensions_AppExtensionCatalog).Get(), &catalogStatics));
ComPtr<IAppExtensionCatalog> catalog;
RETURN_IF_FAILED(catalogStatics->Open(HStringReference(extensionName).Get(), &catalog));
ComPtr<IAsyncOperation<IVectorView<AppExtension*>*>> findOperation;
RETURN_IF_FAILED(catalog->FindAllAsync(&findOperation));
ComPtr<IVectorView<AppExtension*>> extensionList;
RETURN_IF_FAILED(wil::wait_for_completion_nothrow(findOperation.Get(), &extensionList));
UINT extensionCount;
RETURN_IF_FAILED(extensionList->get_Size(&extensionCount));
for (UINT index = 0; index < extensionCount; index++)
{
DelegationConfig::PackageInfo extensionMetadata;
ComPtr<IAppExtension> extension;
RETURN_IF_FAILED(extensionList->GetAt(index, &extension));
ComPtr<IPackage> extensionPackage;
RETURN_IF_FAILED(extension->get_Package(&extensionPackage));
ComPtr<IPackage2> extensionPackage2;
RETURN_IF_FAILED(extensionPackage.As(&extensionPackage2));
ComPtr<IPackageId> extensionPackageId;
RETURN_IF_FAILED(extensionPackage->get_Id(&extensionPackageId));
HString publisherId;
RETURN_IF_FAILED(extensionPackageId->get_PublisherId(publisherId.GetAddressOf()));
HString name;
RETURN_IF_FAILED(extensionPackage2->get_DisplayName(name.GetAddressOf()));
extensionMetadata.name = std::wstring{ name.GetRawBuffer(nullptr) };
HString publisher;
RETURN_IF_FAILED(extensionPackage2->get_PublisherDisplayName(publisher.GetAddressOf()));
extensionMetadata.author = std::wstring{ publisher.GetRawBuffer(nullptr) };
// Try to get the logo. Don't completely bail if we fail to get it. It's non-critical.
ComPtr<IUriRuntimeClass> logoUri;
LOG_IF_FAILED(extensionPackage2->get_Logo(logoUri.GetAddressOf()));
// If we did manage to get one, extract the string and store in the structure
if (logoUri)
{
HString logo;
RETURN_IF_FAILED(logoUri->get_AbsoluteUri(logo.GetAddressOf()));
extensionMetadata.logo = std::wstring{ logo.GetRawBuffer(nullptr) };
}
HString pfn;
RETURN_IF_FAILED(extensionPackageId->get_FamilyName(pfn.GetAddressOf()));
extensionMetadata.pfn = std::wstring{ pfn.GetRawBuffer(nullptr) };
PackageVersion version;
RETURN_IF_FAILED(extensionPackageId->get_Version(&version));
extensionMetadata.version.major = version.Major;
extensionMetadata.version.minor = version.Minor;
extensionMetadata.version.build = version.Build;
extensionMetadata.version.revision = version.Revision;
// Fetch the custom properties XML out of the extension information
ComPtr<IAsyncOperation<IPropertySet*>> propertiesOperation;
RETURN_IF_FAILED(extension->GetExtensionPropertiesAsync(&propertiesOperation));
// Wait for async to complete and return the property set.
ComPtr<IPropertySet> properties;
RETURN_IF_FAILED(wil::wait_for_completion_nothrow(propertiesOperation.Get(), &properties));
// We can't do anything on a set, but it must also be convertible to this type of map per the Windows.Foundation specs for this
ComPtr<IMap<HSTRING, IInspectable*>> map;
RETURN_IF_FAILED(properties.As(&map));
// Looking it up is going to get us an inspectable
ComPtr<IInspectable> inspectable;
RETURN_IF_FAILED(map->Lookup(HStringReference(L"Clsid").Get(), &inspectable));
// Unfortunately that inspectable is another set because we're dealing with XML data payload that we put in the manifest.
ComPtr<IPropertySet> anotherSet;
RETURN_IF_FAILED(inspectable.As(&anotherSet));
// And we can't look at sets directly, so move it to map.
ComPtr<IMap<HSTRING, IInspectable*>> anotherMap;
RETURN_IF_FAILED(anotherSet.As(&anotherMap));
// Use the magic value of #text to get the body between the XML tags. And of course it's an obtuse Inspectable again.
ComPtr<IInspectable> anotherInspectable;
RETURN_IF_FAILED(anotherMap->Lookup(HStringReference(L"#text").Get(), &anotherInspectable));
// But this time that Inspectable is an IPropertyValue, which is a PROPVARIANT in a trench coat.
ComPtr<IPropertyValue> propValue;
RETURN_IF_FAILED(anotherInspectable.As(&propValue));
// Check the type of the variant
PropertyType propType;
RETURN_IF_FAILED(propValue->get_Type(&propType));
// If we're not a string, bail because I don't know what's going on.
if (propType != PropertyType::PropertyType_String)
{
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
}
// Get that string out.
HString value;
RETURN_IF_FAILED(propValue->GetString(value.GetAddressOf()));
// Holy cow. It should be a GUID. Try to parse it.
IID iid;
RETURN_IF_FAILED(IIDFromString(value.GetRawBuffer(nullptr), &iid));
vec.push_back({ iid, extensionMetadata });
}
return S_OK;
}
[[nodiscard]] HRESULT DelegationConfig::s_GetAvailablePackages(std::vector<DelegationPackage>& packages, DelegationPackage& def) noexcept
try
{
auto coinit = wil::CoInitializeEx(COINIT_APARTMENTTHREADED);
packages.clear();
packages.push_back({ DefaultDelegationPair });
packages.push_back({ ConhostDelegationPair });
// Get consoles and terminals.
// If we fail to look up any, we should still have ONE come back to us as the hard-coded default console host.
// The errors aren't really useful except for debugging, so log only.
std::vector<DelegationBase> consoles;
LOG_IF_FAILED(_lookupCatalog(DELEGATION_CONSOLE_EXTENSION_NAME, consoles));
std::vector<DelegationBase> terminals;
LOG_IF_FAILED(_lookupCatalog(DELEGATION_TERMINAL_EXTENSION_NAME, terminals));
// TODO: I hate this algorithm (it's bad performance), but I couldn't
// find an AppModel interface that would let me look up all the extensions
// in one package.
for (const auto& term : terminals)
{
for (const auto& con : consoles)
{
if (term.info.IsFromSamePackage(con.info))
{
DelegationPackage package;
package.pair = { DelegationPairKind::Custom, con.clsid, term.clsid };
package.info = term.info;
packages.push_back(std::move(package));
break;
}
}
}
// We should find at least one package.
RETURN_HR_IF(E_FAIL, packages.empty());
// Get the currently set default console/terminal.
// Then, search through and find a package that matches.
// If we find one, then return it.
const auto delegationPair = s_GetDelegationPair();
for (auto& pkg : packages)
{
if (pkg.pair == delegationPair)
{
def = pkg;
return S_OK;
}
}
// The default is DefaultDelegationPair ("Let Windows decide").
def = packages.at(0);
return S_OK;
}
CATCH_RETURN()
[[nodiscard]] HRESULT DelegationConfig::s_SetDefaultConsoleById(const IID& iid) noexcept
{
return s_Set(DELEGATION_CONSOLE_KEY_NAME, iid);
}
[[nodiscard]] HRESULT DelegationConfig::s_SetDefaultTerminalById(const IID& iid) noexcept
{
return s_Set(DELEGATION_TERMINAL_KEY_NAME, iid);
}
[[nodiscard]] HRESULT DelegationConfig::s_SetDefaultByPackage(const DelegationPackage& package) noexcept
{
RETURN_IF_FAILED(s_SetDefaultConsoleById(package.pair.console));
RETURN_IF_FAILED(s_SetDefaultTerminalById(package.pair.terminal));
return S_OK;
}
[[nodiscard]] DelegationConfig::DelegationPair DelegationConfig::s_GetDelegationPair() noexcept
{
wil::unique_hkey currentUserKey;
wil::unique_hkey consoleKey;
wil::unique_hkey startupKey;
if (FAILED_NTSTATUS_LOG(RegistrySerialization::s_OpenConsoleKey(¤tUserKey, &consoleKey)) ||
FAILED_NTSTATUS_LOG(RegistrySerialization::s_OpenKey(consoleKey.get(), L"%%Startup", &startupKey)))
{
return DefaultDelegationPair;
}
static constexpr const wchar_t* keys[2]{ DELEGATION_CONSOLE_KEY_NAME, DELEGATION_TERMINAL_KEY_NAME };
// values[0]/[1] will contain the delegation console/terminal
// respectively if set to a valid value within the registry.
IID values[2]{ CLSID_Default, CLSID_Default };
for (size_t i = 0; i < 2; ++i)
{
// The GUID is stored as: {00000000-0000-0000-0000-000000000000}
// = 38 characters + trailing null terminator.
wchar_t buffer[39];
DWORD bytesUsed = 0;
const auto result = RegistrySerialization::s_QueryValue(startupKey.get(), keys[i], sizeof(buffer), REG_SZ, reinterpret_cast<BYTE*>(&buffer[0]), &bytesUsed);
if (result != S_OK)
{
// Don't log the more common key-not-found error.
if (result != NTSTATUS_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
LOG_NTSTATUS(result);
}
continue;
}
if (bytesUsed == sizeof(buffer))
{
// RegQueryValueExW docs:
// If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the
// proper terminating null characters. Therefore, even if the function returns ERROR_SUCCESS, the application
// should ensure that the string is properly terminated before using it; otherwise, it may overwrite a buffer.
buffer[std::size(buffer) - 1] = 0;
LOG_IF_FAILED(IIDFromString(&buffer[0], &values[i]));
}
}
if (values[0] == CLSID_Default || values[1] == CLSID_Default)
{
return DefaultDelegationPair;
}
if (values[0] == CLSID_Conhost || values[1] == CLSID_Conhost)
{
return ConhostDelegationPair;
}
return { DelegationPairKind::Custom, values[0], values[1] };
}
[[nodiscard]] HRESULT DelegationConfig::s_Set(PCWSTR value, const CLSID clsid) noexcept
try
{
wil::unique_hkey currentUserKey;
wil::unique_hkey consoleKey;
RETURN_IF_NTSTATUS_FAILED(RegistrySerialization::s_OpenConsoleKey(¤tUserKey, &consoleKey));
// Create method for registry is a "create if not exists, otherwise open" function.
wil::unique_hkey startupKey;
RETURN_IF_NTSTATUS_FAILED(RegistrySerialization::s_CreateKey(consoleKey.get(), L"%%Startup", &startupKey));
wil::unique_cotaskmem_string str;
RETURN_IF_FAILED(StringFromCLSID(clsid, &str));
RETURN_IF_NTSTATUS_FAILED(RegistrySerialization::s_SetValue(startupKey.get(), value, REG_SZ, reinterpret_cast<BYTE*>(str.get()), gsl::narrow<DWORD>(wcslen(str.get()) * sizeof(wchar_t))));
return S_OK;
}
CATCH_RETURN()