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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
// 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"
#include "scanet.h"
LPCWSTR vcsGroupQuery = L"SELECT `Group`, `Component_`, `Name`, `Domain` FROM `Wix4Group` WHERE `Group`=?";
enum eGroupQuery { vgqGroup = 1, vgqComponent, vgqName, vgqDomain };
LPCWSTR vcsGroupGroupQuery = L"SELECT `Parent_`, `Child_` FROM `Wix6GroupGroup` WHERE `Child_`=?";
enum eGroupGroupQuery { vggqParent = 1, vggqChild };
LPCWSTR vActionableGroupQuery = L"SELECT `Group`,`Component_`,`Name`,`Domain`,`Comment`,`Attributes` FROM `Wix4Group`,`Wix6Group` WHERE `Component_` IS NOT NULL AND `Group`=`Group_`";
enum eActionableGroupQuery { vagqGroup = 1, vagqComponent, vagqName, vagqDomain, vagqComment, vagqAttributes };
static HRESULT AddGroupToList(
__inout SCA_GROUP** ppsgList
);
HRESULT __stdcall ScaGetGroup(
__in LPCWSTR wzGroup,
__out SCA_GROUP* pscag
)
{
if (!wzGroup || *wzGroup==0 || !pscag)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
PMSIHANDLE hView, hRec;
LPWSTR pwzData = NULL;
hRec = ::MsiCreateRecord(1);
hr = WcaSetRecordString(hRec, 1, wzGroup);
ExitOnFailure(hr, "Failed to look up Group");
hr = WcaOpenView(vcsGroupQuery, &hView);
ExitOnFailure(hr, "Failed to open view on Wix4Group table");
hr = WcaExecuteView(hView, hRec);
ExitOnFailure(hr, "Failed to execute view on Wix4Group table");
hr = WcaFetchSingleRecord(hView, &hRec);
if (S_OK == hr)
{
hr = WcaGetRecordString(hRec, vgqGroup, &pwzData);
ExitOnFailure(hr, "Failed to get Wix4Group.Group");
hr = ::StringCchCopyW(pscag->wzKey, countof(pscag->wzKey), pwzData);
ExitOnFailure(hr, "Failed to copy key string to group object");
hr = WcaGetRecordString(hRec, vgqComponent, &pwzData);
ExitOnFailure(hr, "Failed to get Wix4Group.Component_");
hr = ::StringCchCopyW(pscag->wzComponent, countof(pscag->wzComponent), pwzData);
ExitOnFailure(hr, "Failed to copy component string to group object");
hr = WcaGetRecordFormattedString(hRec, vgqName, &pwzData);
ExitOnFailure(hr, "Failed to get Wix4Group.Name");
hr = ::StringCchCopyW(pscag->wzName, countof(pscag->wzName), pwzData);
ExitOnFailure(hr, "Failed to copy name string to group object");
hr = WcaGetRecordFormattedString(hRec, vgqDomain, &pwzData);
ExitOnFailure(hr, "Failed to get Wix4Group.Domain");
hr = ::StringCchCopyW(pscag->wzDomain, countof(pscag->wzDomain), pwzData);
ExitOnFailure(hr, "Failed to copy domain string to group object");
}
else if (E_NOMOREITEMS == hr)
{
WcaLog(LOGMSG_STANDARD, "Error: Cannot locate Wix4Group.Group='%ls'", wzGroup);
hr = E_FAIL;
}
else
{
ExitOnFailure(hr, "Error or found multiple matching Wix4Group rows");
}
LExit:
ReleaseStr(pwzData);
return hr;
}
HRESULT __stdcall ScaGetGroupDeferred(
__in LPCWSTR wzGroup,
__in WCA_WRAPQUERY_HANDLE hGroupQuery,
__out SCA_USER* pscag
)
{
if (!wzGroup || !pscag)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
MSIHANDLE hRec, hRecTest;
LPWSTR pwzData = NULL;
// clear struct and bail right away if no group key was passed to search for
::ZeroMemory(pscag, sizeof(*pscag));
if (!*wzGroup)
{
ExitFunction1(hr = S_OK);
}
// Reset back to the first record
WcaFetchWrappedReset(hGroupQuery);
hr = WcaFetchWrappedRecordWhereString(hGroupQuery, vgqGroup, wzGroup, &hRec);
if (S_OK == hr)
{
hr = WcaFetchWrappedRecordWhereString(hGroupQuery, vgqGroup, wzGroup, &hRecTest);
if (S_OK == hr)
{
AssertSz(FALSE, "Found multiple matching Wix4Group rows");
}
hr = WcaGetRecordString(hRec, vgqGroup, &pwzData);
ExitOnFailure(hr, "Failed to get Wix4Group.Group");
hr = ::StringCchCopyW(pscag->wzKey, countof(pscag->wzKey), pwzData);
ExitOnFailure(hr, "Failed to copy key string to group object (in deferred CA)");
hr = WcaGetRecordString(hRec, vgqComponent, &pwzData);
ExitOnFailure(hr, "Failed to get Wix4Group.Component_");
hr = ::StringCchCopyW(pscag->wzComponent, countof(pscag->wzComponent), pwzData);
ExitOnFailure(hr, "Failed to copy component string to group object (in deferred CA)");
hr = WcaGetRecordString(hRec, vgqName, &pwzData);
ExitOnFailure(hr, "Failed to get Wix4Group.Name");
hr = ::StringCchCopyW(pscag->wzName, countof(pscag->wzName), pwzData);
ExitOnFailure(hr, "Failed to copy name string to group object (in deferred CA)");
hr = WcaGetRecordString(hRec, vgqDomain, &pwzData);
ExitOnFailure(hr, "Failed to get Wix4Group.Domain");
hr = ::StringCchCopyW(pscag->wzDomain, countof(pscag->wzDomain), pwzData);
ExitOnFailure(hr, "Failed to copy domain string to group object (in deferred CA)");
}
else if (E_NOMOREITEMS == hr)
{
WcaLog(LOGMSG_STANDARD, "Error: Cannot locate Wix4Group.Group='%ls'", wzGroup);
hr = E_FAIL;
}
else
{
ExitOnFailure(hr, "Error fetching single Wix4Group row");
}
LExit:
ReleaseStr(pwzData);
return hr;
}
void ScaGroupFreeList(
__in SCA_GROUP* psgList
)
{
SCA_GROUP* psgDelete = psgList;
while (psgList)
{
psgDelete = psgList;
psgList = psgList->psgNext;
MemFree(psgDelete);
}
}
HRESULT ScaGroupRead(
__out SCA_GROUP** ppsgList
)
{
//Assert(FALSE);
Assert(ppsgList);
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
PMSIHANDLE hView, hRec, hGroupRec, hGroupGroupView;
LPWSTR pwzData = NULL;
BOOL fGroupGroupExists = FALSE;
SCA_GROUP *psg = NULL;
INSTALLSTATE isInstalled, isAction;
if (S_OK != WcaTableExists(L"Wix4Group"))
{
WcaLog(LOGMSG_VERBOSE, "Wix4Group Table does not exist, exiting");
ExitFunction1(hr = S_FALSE);
}
if (S_OK != WcaTableExists(L"Wix6Group"))
{
WcaLog(LOGMSG_VERBOSE, "Wix6Group Table does not exist, exiting");
ExitFunction1(hr = S_FALSE);
}
if (S_OK == WcaTableExists(L"Wix6GroupGroup"))
{
fGroupGroupExists = TRUE;
}
//
// loop through all the groups
//
hr = WcaOpenExecuteView(vActionableGroupQuery, &hView);
ExitOnFailure(hr, "failed to open view on Wix4Group,Wix6Group table(s)");
while (S_OK == (hr = WcaFetchRecord(hView, &hRec)))
{
hr = WcaGetRecordString(hRec, vagqComponent, &pwzData);
ExitOnFailure(hr, "failed to get Wix4Group.Component");
er = ::MsiGetComponentStateW(WcaGetInstallHandle(), pwzData, &isInstalled, &isAction);
hr = HRESULT_FROM_WIN32(er);
ExitOnFailure(hr, "failed to get Component state for Wix4Group");
// don't bother if we aren't installing or uninstalling this component
if (WcaIsInstalling(isInstalled, isAction) || WcaIsUninstalling(isInstalled, isAction))
{
//
// Add the group to the list and populate it's values
//
hr = AddGroupToList(ppsgList);
ExitOnFailure(hr, "failed to add group to list");
psg = *ppsgList;
psg->isInstalled = isInstalled;
psg->isAction = isAction;
hr = ::StringCchCopyW(psg->wzComponent, countof(psg->wzComponent), pwzData);
ExitOnFailure(hr, "failed to copy component name: %ls", pwzData);
hr = WcaGetRecordString(hRec, vagqGroup, &pwzData);
ExitOnFailure(hr, "failed to get Wix4Group.Group");
hr = ::StringCchCopyW(psg->wzKey, countof(psg->wzKey), pwzData);
ExitOnFailure(hr, "failed to copy group key: %ls", pwzData);
hr = WcaGetRecordFormattedString(hRec, vagqName, &pwzData);
ExitOnFailure(hr, "failed to get Wix4Group.Name");
hr = ::StringCchCopyW(psg->wzName, countof(psg->wzName), pwzData);
ExitOnFailure(hr, "failed to copy group name: %ls", pwzData);
hr = WcaGetRecordFormattedString(hRec, vagqDomain, &pwzData);
ExitOnFailure(hr, "failed to get Wix4Group.Domain");
hr = ::StringCchCopyW(psg->wzDomain, countof(psg->wzDomain), pwzData);
ExitOnFailure(hr, "failed to copy group domain: %ls", pwzData);
hr = WcaGetRecordFormattedString(hRec, vagqComment, &pwzData);
ExitOnFailure(hr, "failed to get Wix6Group.Comment");
hr = ::StringCchCopyW(psg->wzComment, countof(psg->wzComment), pwzData);
ExitOnFailure(hr, "failed to copy group comment: %ls", pwzData);
hr = WcaGetRecordInteger(hRec, vagqAttributes, &psg->iAttributes);
ExitOnFailure(hr, "failed to get Wix6Group.Attributes");
// Check if this group is to be added to any other groups
if (fGroupGroupExists)
{
hGroupRec = ::MsiCreateRecord(1);
hr = WcaSetRecordString(hGroupRec, 1, psg->wzKey);
ExitOnFailure(hr, "Failed to create group record for querying Wix6GroupGroup table");
hr = WcaOpenExecuteView(vcsGroupGroupQuery, &hGroupGroupView);
ExitOnFailure(hr, "Failed to open view on Wix6GroupGroup table for group %ls", psg->wzKey);/*
hr = WcaExecuteView(hGroupGroupView, hGroupRec);
ExitOnFailure(hr, "Failed to execute view on Wix6GroupGroup table for group: %ls", psg->wzKey);*/
while (S_OK == (hr = WcaFetchRecord(hGroupGroupView, &hRec)))
{
hr = WcaGetRecordString(hRec, vggqParent, &pwzData);
ExitOnFailure(hr, "failed to get Wix6GroupGroup.Parent");
hr = AddGroupToList(&(psg->psgGroups));
ExitOnFailure(hr, "failed to add group to list");
hr = ScaGetGroup(pwzData, psg->psgGroups);
ExitOnFailure(hr, "failed to get information for group: %ls", pwzData);
}
if (E_NOMOREITEMS == hr)
{
hr = S_OK;
}
ExitOnFailure(hr, "failed to enumerate selected rows from Wix4UserGroup table");
}
}
}
if (E_NOMOREITEMS == hr)
{
hr = S_OK;
}
ExitOnFailure(hr, "failed to enumerate selected rows from Wix4Group table");
LExit:
ReleaseStr(pwzData);
return hr;
}
/* ****************************************************************
ScaGroupExecute - Schedules group account creation or removal based on
component state.
******************************************************************/
HRESULT ScaGroupExecute(
__in SCA_GROUP *psgList
)
{
HRESULT hr = S_OK;
DWORD er = 0;
LPWSTR pwzBaseScriptKey = NULL;
DWORD cScriptKey = 0;
LOCALGROUP_INFO_0 *pGroupInfo = NULL;
LPWSTR pwzScriptKey = NULL;
LPWSTR pwzActionData = NULL;
LPWSTR pwzRollbackData = NULL;
LPWSTR pwzServerName = NULL;
// Get the base script key for this CustomAction.
hr = WcaCaScriptCreateKey(&pwzBaseScriptKey);
ExitOnFailure(hr, "Failed to get encoding key.");
// Loop through all the users to be configured.
for (SCA_GROUP *psg = psgList; psg; psg = psg->psgNext)
{
GROUP_EXISTS geGroupExists = GROUP_EXISTS_INDETERMINATE;
// Always put the Group Name, Domain, and Comment on the front of the CustomAction data.
// The attributes will be added when we have finished adjusting them. Sometimes we'll
// add more data.
Assert(psg->wzName);
hr = WcaWriteStringToCaData(psg->wzName, &pwzActionData);
ExitOnFailure(hr, "Failed to add group name to custom action data: %ls", psg->wzName);
hr = WcaWriteStringToCaData(psg->wzDomain, &pwzActionData);
ExitOnFailure(hr, "Failed to add group domain to custom action data: %ls", psg->wzDomain);
hr = WcaWriteStringToCaData(psg->wzComment, &pwzActionData);
ExitOnFailure(hr, "Failed to add group comment to custom action data: %ls", psg->wzComment);
// Check to see if the group already exists since we have to be very careful when adding
// and removing groups. Note: MSDN says that it is safe to call these APIs from any
// user, so we should be safe calling it during immediate mode.
LPCWSTR wzDomain = psg->wzDomain;
hr = GetDomainServerName(wzDomain, &pwzServerName);
er = ::NetLocalGroupGetInfo(pwzServerName, psg->wzName, 0, reinterpret_cast<LPBYTE*>(&pGroupInfo));
if (NERR_Success == er)
{
geGroupExists = GROUP_EXISTS_YES;
}
else if (NERR_GroupNotFound == er)
{
geGroupExists = GROUP_EXISTS_NO;
}
else
{
geGroupExists = GROUP_EXISTS_INDETERMINATE;
hr = HRESULT_FROM_WIN32(er);
WcaLog(LOGMSG_VERBOSE, "Failed to check existence of domain: %ls, group: %ls (error code 0x%x) - continuing", wzDomain, psg->wzName, hr);
hr = S_OK;
er = ERROR_SUCCESS;
}
if (WcaIsInstalling(psg->isInstalled, psg->isAction))
{
// If the group exists, check to see if we are supposed to fail if the group exists before
// the install.
if (GROUP_EXISTS_YES == geGroupExists)
{
// Re-installs will always fail if we don't remove the check for "fail if exists".
if (WcaIsReInstalling(psg->isInstalled, psg->isAction))
{
psg->iAttributes &= ~SCAG_FAIL_IF_EXISTS;
// If install would create the group, re-install should be able to update the group.
if (!(psg->iAttributes & SCAG_DONT_CREATE_GROUP))
{
psg->iAttributes |= SCAG_UPDATE_IF_EXISTS;
}
}
if (SCAG_FAIL_IF_EXISTS & psg->iAttributes
&& !(SCAG_UPDATE_IF_EXISTS & psg->iAttributes))
{
hr = HRESULT_FROM_WIN32(NERR_GroupExists);
MessageExitOnFailure(hr, msierrGRPFailedGroupCreateExists, "Failed to create group: %ls because group already exists.", psg->wzName);
}
}
hr = WcaWriteIntegerToCaData(psg->iAttributes, &pwzActionData);
ExitOnFailure(hr, "failed to add group attributes to custom action data for group: %ls", psg->wzKey);
// Rollback only if the group already exists, we couldn't determine if the group exists, or we are going to create the group
if ((GROUP_EXISTS_YES == geGroupExists)
|| (GROUP_EXISTS_INDETERMINATE == geGroupExists)
|| !(psg->iAttributes & SCAG_DONT_CREATE_GROUP))
{
++cScriptKey;
hr = StrAllocFormatted(&pwzScriptKey, L"%ls%u", pwzBaseScriptKey, cScriptKey);
ExitOnFailure(hr, "Failed to create encoding key.");
// Write the script key to CustomActionData for install and rollback so information can be passed to rollback.
hr = WcaWriteStringToCaData(pwzScriptKey, &pwzActionData);
ExitOnFailure(hr, "Failed to add encoding key to custom action data.");
hr = WcaWriteStringToCaData(pwzScriptKey, &pwzRollbackData);
ExitOnFailure(hr, "Failed to add encoding key to rollback custom action data.");
INT iRollbackUserAttributes = psg->iAttributes;
// If the user already exists, ensure this is accounted for in rollback
if (GROUP_EXISTS_YES == geGroupExists)
{
iRollbackUserAttributes |= SCAG_DONT_CREATE_GROUP;
}
else
{
iRollbackUserAttributes &= ~SCAG_DONT_CREATE_GROUP;
}
hr = WcaWriteStringToCaData(psg->wzName, &pwzRollbackData);
ExitOnFailure(hr, "Failed to add group name to rollback custom action data: %ls", psg->wzName);
hr = WcaWriteStringToCaData(psg->wzDomain, &pwzRollbackData);
ExitOnFailure(hr, "Failed to add group domain to rollback custom action data: %ls", psg->wzDomain);
hr = WcaWriteIntegerToCaData(iRollbackUserAttributes, &pwzRollbackData);
ExitOnFailure(hr, "failed to add group attributes to rollback custom action data for group: %ls", psg->wzKey);
hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"CreateGroupRollback"), pwzRollbackData, COST_GROUP_DELETE);
ExitOnFailure(hr, "failed to schedule CreateGroupRollback");
}
else
{
// Write empty script key to CustomActionData since there is no rollback.
hr = WcaWriteStringToCaData(L"", &pwzActionData);
ExitOnFailure(hr, "Failed to add empty encoding key to custom action data.");
}
//
// Schedule the creation now.
//
hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"CreateGroup"), pwzActionData, COST_GROUP_ADD);
ExitOnFailure(hr, "failed to schedule CreateGroup");
}
else if (((GROUP_EXISTS_YES == geGroupExists)
|| (GROUP_EXISTS_INDETERMINATE == geGroupExists))
&& WcaIsUninstalling(psg->isInstalled, psg->isAction)
&& !(psg->iAttributes & SCAG_DONT_REMOVE_ON_UNINSTALL))
{
hr = WcaWriteIntegerToCaData(psg->iAttributes, &pwzActionData);
ExitOnFailure(hr, "failed to add group attributes to custom action data for group: %ls", psg->wzKey);
// Schedule the removal because the group exists and we don't have any flags set
// that say not to remove the group on uninstall.
//
// Note: We can't rollback the removal of a group which is why RemoveGroup is a commit
// CustomAction.
hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"RemoveGroup"), pwzActionData, COST_GROUP_DELETE);
ExitOnFailure(hr, "failed to schedule RemoveGroup");
}
ReleaseNullStr(pwzScriptKey);
ReleaseNullStr(pwzActionData);
ReleaseNullStr(pwzRollbackData);
ReleaseNullStr(pwzServerName);
if (pGroupInfo)
{
::NetApiBufferFree(static_cast<LPVOID>(pGroupInfo));
pGroupInfo = NULL;
}
}
LExit:
ReleaseStr(pwzBaseScriptKey);
ReleaseStr(pwzScriptKey);
ReleaseStr(pwzActionData);
ReleaseStr(pwzRollbackData);
ReleaseStr(pwzServerName);
if (pGroupInfo)
{
::NetApiBufferFree(static_cast<LPVOID>(pGroupInfo));
pGroupInfo = NULL;
}
return hr;
}
static HRESULT AddGroupToList(
__inout SCA_GROUP** ppsgList
)
{
HRESULT hr = S_OK;
SCA_GROUP* psg = static_cast<SCA_GROUP*>(MemAlloc(sizeof(SCA_GROUP), TRUE));
ExitOnNull(psg, hr, E_OUTOFMEMORY, "failed to allocate memory for new group list element");
psg->psgNext = *ppsgList;
*ppsgList = psg;
LExit:
return hr;
}
|