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
|
// 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"
// sql queries
enum eWebSvcExtQuery { ldqComponent=1 , ldqFile, ldqDescription, ldqGroup, ldqAttributes, ldqInstalled, ldqAction };
LPCWSTR vcsWebSvcExtRoot = L"/LM/W3SVC";
// prototypes for private helper functions
static HRESULT AddWebSvcExtToList(
__in SCA_WEBSVCEXT** ppsWseList
);
//static HRESULT ScaCheckWebSvcExtValue(
// __in IMSAdminBase* piMetabase,
// __in DWORD dwMDIdentifier
// );
static HRESULT ScaWebSvcExtInstall(
__in LPWSTR *pwzWebSvcExtList,
__in DWORD_PTR *pcchWebSvcExtList,
__in SCA_WEBSVCEXT* psWseList
);
static HRESULT ScaWebSvcExtUninstall(
__in LPWSTR *pwzWebSvcExtList,
__in const DWORD *pcchWebSvcExtList,
__in SCA_WEBSVCEXT* psWseList
);
// functions
HRESULT __stdcall ScaWebSvcExtRead(
__in SCA_WEBSVCEXT** ppsWseList,
__inout LPWSTR *ppwzCustomActionData
)
{
Assert(ppsWseList);
HRESULT hr = S_OK;
MSIHANDLE hRec;
LPWSTR pwzData = NULL;
INSTALLSTATE isInstalled = INSTALLSTATE_UNKNOWN;
INSTALLSTATE isAction = INSTALLSTATE_UNKNOWN;
SCA_WEBSVCEXT* psWebSvcExt = NULL;
WCA_WRAPQUERY_HANDLE hWrapQuery = NULL;
hr = WcaBeginUnwrapQuery(&hWrapQuery, ppwzCustomActionData);
ExitOnFailure(hr, "Failed to unwrap query for ScaWebSvcExtRead");
if (0 == WcaGetQueryRecords(hWrapQuery))
{
WcaLog(LOGMSG_VERBOSE, "Skipping ScaWebSvcExtRead() because IIsWebServiceExtension data not present");
ExitFunction1(hr = S_FALSE);
}
// loop through all the web service extensions
while (S_OK == (hr = WcaFetchWrappedRecord(hWrapQuery, &hRec)))
{
// Get the Component first. If the Component is not being modified during
// this transaction, skip processing this whole record.
hr = WcaGetRecordString(hRec, ldqComponent, &pwzData);
ExitOnFailure(hr, "Failed to get Component for WebSvcExt");
hr = WcaGetRecordInteger(hRec, ldqInstalled, (int *)&isInstalled);
ExitOnFailure(hr, "Failed to get Component installed state for WebSvcExt");
hr = WcaGetRecordInteger(hRec, ldqAction, (int *)&isAction);
ExitOnFailure(hr, "Failed to get Component action state for WebSvcExt");
if (!WcaIsInstalling(isInstalled, isAction) &&
!WcaIsReInstalling(isInstalled, isAction) &&
!WcaIsUninstalling(isInstalled, isAction))
{
continue; // skip this record.
}
hr = AddWebSvcExtToList(ppsWseList);
ExitOnFailure(hr, "failed to add element to web svc ext list");
psWebSvcExt = *ppsWseList;
Assert(psWebSvcExt);
psWebSvcExt->isInstalled = isInstalled;
psWebSvcExt->isAction = isAction;
hr = WcaGetRecordString(hRec, ldqFile, &pwzData);
ExitOnFailure(hr, "Failed to get File for WebSvcExt");
hr = ::StringCchCopyW(psWebSvcExt->wzFile, countof(psWebSvcExt->wzFile), pwzData);
ExitOnFailure(hr, "Failed to copy File for WebSvcExt");
hr = WcaGetRecordString(hRec, ldqDescription, &pwzData);
ExitOnFailure(hr, "Failed to get Description for WebSvcExt");
hr = ::StringCchCopyW(psWebSvcExt->wzDescription, countof(psWebSvcExt->wzDescription), pwzData);
ExitOnFailure(hr, "Failed to copy Description for WebSvcExt");
hr = WcaGetRecordString(hRec, ldqGroup, &pwzData);
ExitOnFailure(hr, "Failed to get Group for WebSvcExt");
hr = ::StringCchCopyW(psWebSvcExt->wzGroup, countof(psWebSvcExt->wzGroup), pwzData);
ExitOnFailure(hr, "Failed to copy Group for WebSvcExt");
hr = WcaGetRecordInteger(hRec, ldqAttributes, &psWebSvcExt->iAttributes);
ExitOnFailure(hr, "Failed to get Attributes for WebSvcExt");
}
if (E_NOMOREITEMS == hr)
hr = S_OK;
ExitOnFailure(hr, "Failure while processing WebSvcExt");
LExit:
WcaFinishUnwrapQuery(hWrapQuery);
ReleaseStr(pwzData);
return hr;
}
// Commit does both install and uninstall
HRESULT __stdcall ScaWebSvcExtCommit(
__in IMSAdminBase* piMetabase,
__in SCA_WEBSVCEXT* psWseList
)
{
Assert(piMetabase);
HRESULT hr = S_OK;
METADATA_RECORD mr;
LPWSTR wzWebSvcExtList = NULL;
DWORD cbWebSvcExtList = 0;
DWORD_PTR cchWebSvcExtList = 0;
if (!psWseList)
{
WcaLog(LOGMSG_VERBOSE, "Skipping ScaWebSvcExtCommit() because there are no web service extensions in the list");
ExitFunction();
}
// Get current set of web service extensions.
::ZeroMemory(&mr, sizeof(mr));
mr.dwMDIdentifier = MD_WEB_SVC_EXT_RESTRICTION_LIST;
mr.dwMDAttributes = 0;
mr.dwMDUserType = IIS_MD_UT_SERVER;
mr.dwMDDataType = ALL_METADATA;
mr.pbMDData = NULL;
mr.dwMDDataLen = 0;
hr = piMetabase->GetData(METADATA_MASTER_ROOT_HANDLE, vcsWebSvcExtRoot, &mr, &cbWebSvcExtList);
if (MD_ERROR_DATA_NOT_FOUND == hr)
{
WcaLog(LOGMSG_VERBOSE, "Skipping ScaWebSvcExtCommit() because WebSvcExtRestrictionList value is not present");
ExitFunction1(hr = S_FALSE);
}
else if (HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) == hr)
{
// cchWebSvcExtList is returned in bytes. Convert to WCHAR size to call StrAlloc
cchWebSvcExtList = cbWebSvcExtList / sizeof(WCHAR);
hr = StrAlloc(&wzWebSvcExtList, cchWebSvcExtList);
ExitOnFailure(hr, "Failed allocating space for web service extensions");
}
else
{
ExitOnFailure(hr, "Failed retrieving web service extensions");
}
mr.pbMDData = (unsigned char*)wzWebSvcExtList;
mr.dwMDDataLen = cbWebSvcExtList;
hr = piMetabase->GetData(METADATA_MASTER_ROOT_HANDLE, vcsWebSvcExtRoot, &mr, &cbWebSvcExtList);
ExitOnFailure(hr, "Failed retrieving web service extensions");
// Make changes to local copy of metabase
while (psWseList)
{
if (WcaIsInstalling(psWseList->isInstalled, psWseList->isAction))
{
hr = ScaWebSvcExtInstall(&wzWebSvcExtList, &cchWebSvcExtList, psWseList);
ExitOnFailure(hr, "Failed to install Web Service extension");
}
else if (WcaIsUninstalling(psWseList->isInstalled, psWseList->isAction))
{
hr = ScaWebSvcExtUninstall(&wzWebSvcExtList, (DWORD *)&cchWebSvcExtList, psWseList);
ExitOnFailure(hr, "Failed to uninstall Web Service extension");
}
psWseList = psWseList->psWseNext;
}
// Write Metabase
hr = ScaWriteMetabaseValue(piMetabase, vcsWebSvcExtRoot, NULL, MD_WEB_SVC_EXT_RESTRICTION_LIST, METADATA_INHERIT, IIS_MD_UT_FILE, MULTISZ_METADATA, wzWebSvcExtList);
ExitOnFailure(hr, "Failed to write WebServiceExtensions: '%ls'", wzWebSvcExtList);
LExit:
ReleaseStr(wzWebSvcExtList);
return hr;
}
static HRESULT ScaWebSvcExtInstall(
__in LPWSTR *ppwzWebSvcExtList,
__in DWORD_PTR *pcchWebSvcExtList,
__in SCA_WEBSVCEXT* psWseList
)
{
Assert( ppwzWebSvcExtList && pcchWebSvcExtList && psWseList);
Assert(*ppwzWebSvcExtList);
HRESULT hr = S_OK;
LPWSTR pwzWebSvcExt = NULL;
int iAllow;
int iUiDeletable;
BOOL fAlreadyExists = FALSE;
DWORD_PTR dwIndex = 0xFFFFFFFF;
LPCWSTR wzFoundString = NULL;
// Check if it's already in there
hr = MultiSzFindSubstring(*ppwzWebSvcExtList, psWseList->wzFile, &dwIndex, &wzFoundString);
ExitOnFailure(hr, "failed to search for string:%ls in web service extension MULTISZ", psWseList->wzFile);
if (S_FALSE != hr && NULL != wcsstr(wzFoundString, psWseList->wzGroup) && NULL != wcsstr(wzFoundString, psWseList->wzDescription))
{
fAlreadyExists = TRUE;
}
// Construct the single string in the format required for the WebSvc Ext list in metabase
iAllow = (psWseList->iAttributes & 1);
iUiDeletable = ((psWseList->iAttributes >> 1) & 1);
hr = StrAllocFormatted(&pwzWebSvcExt, L"%d,%s,%d,%s,%s", iAllow, psWseList->wzFile, iUiDeletable, psWseList->wzGroup, psWseList->wzDescription);
ExitOnFailure(hr, "Failure allocating space for web service extensions");
if (fAlreadyExists)
{
hr = MultiSzReplaceString(ppwzWebSvcExtList, dwIndex, pwzWebSvcExt);
ExitOnFailure(hr, "failed to update web service extension string: %ls", pwzWebSvcExt);
}
else
{
hr = MultiSzPrepend(ppwzWebSvcExtList, pcchWebSvcExtList, pwzWebSvcExt);
ExitOnFailure(hr, "failed to prepend web service extension string: %ls", pwzWebSvcExt);
}
LExit:
ReleaseStr(pwzWebSvcExt);
return hr;
}
static HRESULT ScaWebSvcExtUninstall(
__in LPWSTR *ppwzWebSvcExtList,
__in const DWORD* /*pcchWebSvcExtList*/,
__in SCA_WEBSVCEXT* psWseList
)
{
Assert(ppwzWebSvcExtList && *ppwzWebSvcExtList && psWseList);
Assert(*ppwzWebSvcExtList);
HRESULT hr = S_OK;
DWORD_PTR dwIndex = 0xFFFFFFFF;
LPCWSTR wzFoundString = NULL;
// Find the string to remove
hr = MultiSzFindSubstring(*ppwzWebSvcExtList, psWseList->wzFile, &dwIndex, &wzFoundString);
ExitOnFailure(hr, "failed to search for string:%ls in web service extension MULTISZ", psWseList->wzFile);
// If we found a match (ignoring the Allow and Deletable flags)
if (S_FALSE != hr && NULL != wcsstr(wzFoundString, psWseList->wzGroup) && NULL != wcsstr(wzFoundString, psWseList->wzDescription))
{
hr = MultiSzRemoveString(ppwzWebSvcExtList, dwIndex);
ExitOnFailure(hr, "failed to remove string: %d from web service extension MULTISZ", dwIndex);
}
LExit:
return hr;
}
//static HRESULT ScaCheckWebSvcExtValue(
// __in IMSAdminBase* piMetabase,
// __in DWORD dwMDIdentifier
// )
//{
// if (!piMetabase)
// {
// return E_INVALIDARG;
// }
//
// HRESULT hr = S_OK;
// METADATA_RECORD mr = { 0 };
// DWORD cch = 0;
//
// mr.dwMDIdentifier = dwMDIdentifier;
// mr.dwMDUserType = IIS_MD_UT_SERVER;
//
// hr = piMetabase->GetData(METADATA_MASTER_ROOT_HANDLE, vcsWebSvcExtRoot, &mr, &cch);
// if (HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) == hr)
// {
// hr = S_OK;
// }
// else if (MD_ERROR_DATA_NOT_FOUND == hr)
// {
// hr = S_FALSE;
// }
//
// return hr;
//}
void ScaWebSvcExtFreeList(
__in SCA_WEBSVCEXT* psWseList
)
{
SCA_WEBSVCEXT* psWseDelete = psWseList;
while (psWseList)
{
psWseDelete = psWseList;
psWseList = psWseList->psWseNext;
MemFree(psWseDelete);
}
}
static HRESULT AddWebSvcExtToList(
__in SCA_WEBSVCEXT** ppsWseList
)
{
HRESULT hr = S_OK;
SCA_WEBSVCEXT* psWse = static_cast<SCA_WEBSVCEXT*>(MemAlloc(sizeof(SCA_WEBSVCEXT), TRUE));
ExitOnNull(psWse, hr, E_OUTOFMEMORY, "failed to allocate element for web svc ext list");
psWse->psWseNext = *ppsWseList;
*ppsWseList = psWse;
LExit:
return hr;
}
|