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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
// 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"
// private structs
struct PCA_WELLKNOWN_SID
{
LPCWSTR pwzName;
SID_IDENTIFIER_AUTHORITY iaIdentifierAuthority;
BYTE nSubAuthorityCount;
DWORD dwSubAuthority[8];
};
// well known SIDs
PCA_WELLKNOWN_SID wsWellKnownSids[] = {
{L"\\Everyone", SECURITY_WORLD_SID_AUTHORITY, 1, {SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0}},
{L"\\Administrators", SECURITY_NT_AUTHORITY, 2, {SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0}},
{L"\\LocalSystem", SECURITY_NT_AUTHORITY, 1, {SECURITY_LOCAL_SYSTEM_RID, 0, 0, 0, 0, 0, 0, 0}},
{L"\\LocalService", SECURITY_NT_AUTHORITY, 1, {SECURITY_LOCAL_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0}},
{L"\\NetworkService", SECURITY_NT_AUTHORITY, 1, {SECURITY_NETWORK_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0}},
{L"\\AuthenticatedUser", SECURITY_NT_AUTHORITY, 1, {SECURITY_AUTHENTICATED_USER_RID, 0, 0, 0, 0, 0, 0, 0}},
{L"\\Guests", SECURITY_NT_AUTHORITY, 2, {SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_GUESTS, 0, 0, 0, 0, 0, 0}},
{L"\\Users", SECURITY_NT_AUTHORITY, 2, {SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS, 0, 0, 0, 0, 0, 0}},
{L"\\CREATOR OWNER", SECURITY_NT_AUTHORITY, 1, {SECURITY_CREATOR_OWNER_RID, 0, 0, 0, 0, 0, 0, 0}},
{NULL, SECURITY_NULL_SID_AUTHORITY, 0, {0, 0, 0, 0, 0, 0, 0, 0}}
};
// prototypes for private helper functions
static HRESULT CreateSidFromDomainRidPair(
PSID pDomainSid,
DWORD dwRid,
PSID* ppSid
);
static HRESULT InitLsaUnicodeString(
PLSA_UNICODE_STRING plusStr,
LPCWSTR pwzStr,
SIZE_T dwLen
);
static void FreeLsaUnicodeString(
PLSA_UNICODE_STRING plusStr
);
// function definitions
HRESULT PcaActionDataMessage(
DWORD cArgs,
...
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
PMSIHANDLE hRec;
va_list args;
// record
hRec = ::MsiCreateRecord(cArgs);
ExitOnNull(hRec, hr, E_OUTOFMEMORY, "Failed to create record");
va_start(args, cArgs);
for (DWORD i = 1; i <= cArgs; i++)
{
LPCWSTR pwzArg = va_arg(args, WCHAR*);
if (pwzArg && *pwzArg)
{
er = ::MsiRecordSetStringW(hRec, i, pwzArg);
ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set record field string");
}
}
va_end(args);
// message
er = WcaProcessMessage(INSTALLMESSAGE_ACTIONDATA, hRec);
if (0 == er || IDOK == er || IDYES == er)
{
hr = S_OK;
}
else if (ERROR_INSTALL_USEREXIT == er || IDABORT == er || IDCANCEL == er)
{
WcaSetReturnValue(ERROR_INSTALL_USEREXIT); // note that the user said exit
hr = S_FALSE;
}
else
hr = E_UNEXPECTED;
LExit:
return hr;
}
HRESULT PcaAccountNameToSid(
LPCWSTR pwzAccountName,
PSID* ppSid
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
NTSTATUS st = 0;
PSID pSid = NULL;
LSA_OBJECT_ATTRIBUTES loaAttributes;
LSA_HANDLE lsahPolicy = NULL;
LSA_UNICODE_STRING lusName;
PLSA_REFERENCED_DOMAIN_LIST plrdsDomains = NULL;
PLSA_TRANSLATED_SID pltsSid = NULL;
::ZeroMemory(&loaAttributes, sizeof(loaAttributes));
::ZeroMemory(&lusName, sizeof(lusName));
// identify well known SIDs
for (PCA_WELLKNOWN_SID* pWS = wsWellKnownSids; pWS->pwzName; pWS++)
{
if (0 == lstrcmpiW(pwzAccountName, pWS->pwzName))
{
// allocate SID buffer
pSid = (PSID)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, ::GetSidLengthRequired(pWS->nSubAuthorityCount));
ExitOnNull(pSid, hr, E_OUTOFMEMORY, "Failed to allocate buffer for SID");
// initialize SID
::InitializeSid(pSid, &pWS->iaIdentifierAuthority, pWS->nSubAuthorityCount);
// copy sub autorities
for (DWORD i = 0; i < pWS->nSubAuthorityCount; i++)
*::GetSidSubAuthority(pSid, i) = pWS->dwSubAuthority[i];
break;
}
}
// lookup name
if (!pSid)
{
// open policy handle
st = ::LsaOpenPolicy(NULL, &loaAttributes, POLICY_ALL_ACCESS, &lsahPolicy);
er = ::LsaNtStatusToWinError(st);
ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to open policy handle");
// create account name lsa unicode string
hr = InitLsaUnicodeString(&lusName, pwzAccountName, wcslen(pwzAccountName));
ExitOnFailure(hr, "Failed to initialize account name string");
// lookup name
st = ::LsaLookupNames(lsahPolicy, 1, &lusName, &plrdsDomains, &pltsSid);
er = ::LsaNtStatusToWinError(st);
ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to lookup account names");
if (SidTypeDomain == pltsSid->Use)
ExitOnFailure(hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), "Domain SIDs not supported");
// convert sid
hr = CreateSidFromDomainRidPair(plrdsDomains->Domains[pltsSid->DomainIndex].Sid, pltsSid->RelativeId, &pSid);
ExitOnFailure(hr, "Failed to convert SID");
}
*ppSid = pSid;
pSid = NULL;
hr = S_OK;
LExit:
// clean up
if (pSid)
::HeapFree(::GetProcessHeap(), 0, pSid);
if (lsahPolicy)
::LsaClose(lsahPolicy);
if (plrdsDomains)
::LsaFreeMemory(plrdsDomains);
if (pltsSid)
::LsaFreeMemory(pltsSid);
FreeLsaUnicodeString(&lusName);
return hr;
}
HRESULT PcaSidToAccountName(
PSID pSid,
LPWSTR* ppwzAccountName
)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
NTSTATUS st = 0;
LSA_OBJECT_ATTRIBUTES loaAttributes;
LSA_HANDLE lsahPolicy = NULL;
PLSA_REFERENCED_DOMAIN_LIST plrdsDomains = NULL;
PLSA_TRANSLATED_NAME pltnName = NULL;
LPWSTR pwzDomain = NULL;
LPWSTR pwzName = NULL;
::ZeroMemory(&loaAttributes, sizeof(loaAttributes));
// open policy handle
st = ::LsaOpenPolicy(NULL, &loaAttributes, POLICY_ALL_ACCESS, &lsahPolicy);
er = ::LsaNtStatusToWinError(st);
ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to open policy handle");
// lookup SID
st = ::LsaLookupSids(lsahPolicy, 1, &pSid, &plrdsDomains, &pltnName);
er = ::LsaNtStatusToWinError(st);
ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to lookup SID");
if (SidTypeDomain == pltnName->Use)
ExitOnFailure(hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), "Domain SIDs not supported");
// format account name string
if (SidTypeWellKnownGroup != pltnName->Use)
{
PLSA_UNICODE_STRING plusDomain = &plrdsDomains->Domains[pltnName->DomainIndex].Name;
hr = StrAllocString(&pwzDomain, plusDomain->Buffer, plusDomain->Length / sizeof(WCHAR));
ExitOnFailure(hr, "Failed to allocate name string");
}
hr = StrAllocString(&pwzName, pltnName->Name.Buffer, pltnName->Name.Length / sizeof(WCHAR));
ExitOnFailure(hr, "Failed to allocate domain string");
hr = StrAllocFormatted(ppwzAccountName, L"%s\\%s", pwzDomain ? pwzDomain : L"", pwzName);
ExitOnFailure(hr, "Failed to format account name string");
hr = S_OK;
LExit:
// clean up
if (lsahPolicy)
::LsaClose(lsahPolicy);
if (plrdsDomains)
::LsaFreeMemory(plrdsDomains);
if (pltnName)
::LsaFreeMemory(pltnName);
ReleaseStr(pwzDomain);
ReleaseStr(pwzName);
return hr;
}
HRESULT PcaBuildAccountName(
LPCWSTR pwzDomain,
LPCWSTR pwzName,
LPWSTR* ppwzAccount
)
{
HRESULT hr = S_OK;
WCHAR wzComputerName[MAX_COMPUTERNAME_LENGTH + 1];
::ZeroMemory(wzComputerName, sizeof(wzComputerName));
// if domain is '.', get computer name
if (0 == lstrcmpW(pwzDomain, L"."))
{
DWORD dwSize = countof(wzComputerName);
if (!::GetComputerNameW(wzComputerName, &dwSize))
ExitOnFailure(hr = HRESULT_FROM_WIN32(::GetLastError()), "Failed to get computer name");
}
// build account name
hr = StrAllocFormatted(ppwzAccount, L"%s\\%s", *wzComputerName ? wzComputerName : pwzDomain, pwzName);
ExitOnFailure(hr, "Failed to build domain user name");
hr = S_OK;
LExit:
return hr;
}
HRESULT PcaGuidFromString(
LPCWSTR pwzGuid,
LPGUID pGuid
)
{
HRESULT hr = S_OK;
int cch = 0;
WCHAR wz[39];
::ZeroMemory(wz, sizeof(wz));
cch = lstrlenW(pwzGuid);
if (38 == cch && L'{' == pwzGuid[0] && L'}' == pwzGuid[37])
StringCchCopyW(wz, countof(wz), pwzGuid);
else if (36 == cch)
StringCchPrintfW(wz, countof(wz), L"{%s}", pwzGuid);
else
ExitFunction1(hr = E_INVALIDARG);
hr = ::CLSIDFromString(wz, pGuid);
LExit:
return hr;
}
// helper function definitions
static HRESULT CreateSidFromDomainRidPair(
PSID pDomainSid,
DWORD dwRid,
PSID* ppSid
)
{
HRESULT hr = S_OK;
PSID pSid = NULL;
// get domain SID sub authority count
UCHAR ucSubAuthorityCount = *::GetSidSubAuthorityCount(pDomainSid);
// allocate SID buffer
DWORD dwLengthRequired = ::GetSidLengthRequired(ucSubAuthorityCount + (UCHAR)1);
if (*ppSid)
{
SIZE_T ccb = ::HeapSize(::GetProcessHeap(), 0, *ppSid);
if (-1 == ccb)
ExitOnFailure(hr = E_FAIL, "Failed to get size of SID buffer");
if (ccb < dwLengthRequired)
{
pSid = (PSID)::HeapReAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, *ppSid, dwLengthRequired);
ExitOnNull(pSid, hr, E_OUTOFMEMORY, "Failed to reallocate buffer for SID, len: %d", dwLengthRequired);
*ppSid = pSid;
}
}
else
{
*ppSid = (PSID)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, dwLengthRequired);
ExitOnNull(*ppSid, hr, E_OUTOFMEMORY, "Failed to allocate buffer for SID, len: %d", dwLengthRequired);
}
::InitializeSid(*ppSid, ::GetSidIdentifierAuthority(pDomainSid), ucSubAuthorityCount + (UCHAR)1);
// copy sub autorities
DWORD i = 0;
for (; i < ucSubAuthorityCount; i++)
*::GetSidSubAuthority(*ppSid, i) = *::GetSidSubAuthority(pDomainSid, i);
*::GetSidSubAuthority(*ppSid, i) = dwRid;
hr = S_OK;
LExit:
return hr;
}
static HRESULT InitLsaUnicodeString(
PLSA_UNICODE_STRING plusStr,
LPCWSTR pwzStr,
SIZE_T dwLen
)
{
HRESULT hr = S_OK;
plusStr->Length = (USHORT)dwLen * sizeof(WCHAR);
plusStr->MaximumLength = (USHORT)(dwLen + 1) * sizeof(WCHAR);
plusStr->Buffer = (WCHAR*)::HeapAlloc(::GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR) * (dwLen + 1));
ExitOnNull(plusStr->Buffer, hr, E_OUTOFMEMORY, "Failed to allocate account name string");
hr = StringCchCopyW(plusStr->Buffer, dwLen + 1, pwzStr);
ExitOnFailure(hr, "Failed to copy buffer");
hr = S_OK;
LExit:
return hr;
}
static void FreeLsaUnicodeString(
PLSA_UNICODE_STRING plusStr
)
{
if (plusStr->Buffer)
::HeapFree(::GetProcessHeap(), 0, plusStr->Buffer);
}
|