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
|
// 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"
// globals
LPWSTR vpwzCustomActionData = NULL;
DWORD vdwCustomActionCost = 0;
HRESULT ScaMetabaseTransaction(__in_z LPCWSTR wzBackup)
{
HRESULT hr = S_OK;
// TODO: These functions have been reported to hang IIS (O11:51709). They may have been fixed in IIS6, but if not, need to be re-written the hard way
hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"StartMetabaseTransaction"), wzBackup, COST_IIS_TRANSACTIONS);
ExitOnFailure(hr, "Failed to schedule StartMetabaseTransaction");
hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"RollbackMetabaseTransaction"), wzBackup, 0); // rollback cost is irrelevant
ExitOnFailure(hr, "Failed to schedule RollbackMetabaseTransaction");
hr = WcaDoDeferredAction(CUSTOM_ACTION_DECORATION(L"CommitMetabaseTransaction"), wzBackup, 0); // commit is free
ExitOnFailure(hr, "Failed to schedule StartMetabaseTransaction");
LExit:
return hr;
}
HRESULT ScaCreateWeb(IMSAdminBase* piMetabase, LPCWSTR /*wzWeb*/, LPCWSTR wzWebBase)
{
Assert(piMetabase);
HRESULT hr = S_OK;
UINT ui = 0;
hr = ScaCreateMetabaseKey(piMetabase, wzWebBase, L"");
ExitOnFailure(hr, "Failed to create web");
hr = ScaWriteMetabaseValue(piMetabase, wzWebBase, L"", MD_KEY_TYPE, METADATA_NO_ATTRIBUTES, IIS_MD_UT_SERVER, STRING_METADATA, (LPVOID)L"IIsWebServer");
ExitOnFailure(hr, "Failed to set key type for web");
hr = ScaCreateMetabaseKey(piMetabase, wzWebBase, L"Root");
ExitOnFailure(hr, "Failed to create web root");
hr = ScaWriteMetabaseValue(piMetabase, wzWebBase, L"Root", MD_KEY_TYPE, METADATA_NO_ATTRIBUTES, IIS_MD_UT_SERVER, STRING_METADATA, (LPVOID)L"IIsWebVirtualDir");
ExitOnFailure(hr, "Failed to set key type for web root");
ui = 0x4000003e; // 1073741886; // default directory browsing rights
hr = ScaWriteMetabaseValue(piMetabase, wzWebBase, L"Root", MD_DIRECTORY_BROWSING, METADATA_INHERIT, IIS_MD_UT_FILE, DWORD_METADATA, (LPVOID)((DWORD_PTR)ui));
ExitOnFailure(hr, "Failed to set directory browsing for web");
hr = ScaCreateMetabaseKey(piMetabase, wzWebBase, L"Filters");
ExitOnFailure(hr, "Failed to create web filters root");
hr = ScaWriteMetabaseValue(piMetabase, wzWebBase, L"Filters", MD_KEY_TYPE, METADATA_NO_ATTRIBUTES, IIS_MD_UT_SERVER, STRING_METADATA, (LPVOID)L"IIsFilters");
ExitOnFailure(hr, "Failed to set key for web filters root");
hr = ScaWriteMetabaseValue(piMetabase, wzWebBase, L"Filters", MD_FILTER_LOAD_ORDER, METADATA_NO_ATTRIBUTES, IIS_MD_UT_SERVER, STRING_METADATA, (LPVOID)L"");
ExitOnFailure(hr, "Failed to set empty load order for web");
LExit:
return hr;
}
HRESULT ScaDeleteApp(IMSAdminBase* piMetabase, LPCWSTR wzWebRoot)
{
Assert(piMetabase);
Unused(piMetabase);
HRESULT hr = S_OK;
WCHAR wzKey[METADATA_MAX_NAME_LEN];
WCHAR* pwzCustomActionData = NULL;
hr = WcaWriteIntegerToCaData(MBA_DELETEAPP, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase delete app directive to CustomActionData");
hr = ::StringCchCopyW(wzKey, countof(wzKey), wzWebRoot);
ExitOnFailure(hr, "Failed to copy webroot string to key");
hr = WcaWriteStringToCaData(wzKey, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase key to CustomActionData");
hr = ScaAddToIisConfiguration(pwzCustomActionData, COST_IIS_DELETEAPP);
ExitOnFailure(hr, "Failed to add ScaDeleteApp action data: %ls cost: %d", pwzCustomActionData, COST_IIS_DELETEAPP);
LExit:
ReleaseStr(pwzCustomActionData);
return hr;
}
HRESULT ScaCreateApp(IMSAdminBase* piMetabase, LPCWSTR wzWebRoot,
DWORD dwIsolation)
{
Assert(piMetabase);
Unused(piMetabase);
HRESULT hr = S_OK;
WCHAR wzKey[METADATA_MAX_NAME_LEN];
BOOL fInProc = FALSE;
WCHAR* pwzCustomActionData = NULL;
hr = WcaWriteIntegerToCaData(MBA_CREATEAPP, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase create app directive to CustomActionData");
hr = ::StringCchCopyW(wzKey, countof(wzKey), wzWebRoot);
ExitOnFailure(hr, "Failed to copy webroot string to key");
hr = WcaWriteStringToCaData(wzKey, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase key to CustomActionData");
if (0 == dwIsolation)
fInProc = TRUE;
else
fInProc = FALSE;
hr = WcaWriteIntegerToCaData(fInProc, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add isolation value to CustomActionData");
hr = ScaAddToIisConfiguration(pwzCustomActionData, COST_IIS_CREATEAPP);
ExitOnFailure(hr, "Failed to add ScaCreateApp action data: %ls cost: %d", pwzCustomActionData, COST_IIS_CREATEAPP);
LExit:
ReleaseStr(pwzCustomActionData);
return hr;
}
HRESULT ScaCreateMetabaseKey(IMSAdminBase* piMetabase, LPCWSTR wzRootKey,
LPCWSTR wzSubKey)
{
Assert(piMetabase);
Unused(piMetabase);
HRESULT hr = S_OK;
WCHAR wzKey[METADATA_MAX_NAME_LEN];
WCHAR* pwzCustomActionData = NULL;
hr = ::StringCchCopyW(wzKey, countof(wzKey), wzRootKey);
ExitOnFailure(hr, "Failed to copy root key string to key");
if (L'/' != *(wzKey + lstrlenW(wzRootKey)))
{
hr = ::StringCchCatW(wzKey, countof(wzKey), L"/");
ExitOnFailure(hr, "Failed to concatenate / to key string");
}
if (wzSubKey && *wzSubKey)
{
if (L'/' == *wzSubKey)
{
hr = ::StringCchCatW(wzKey, countof(wzKey), wzSubKey + 1);
ExitOnFailure(hr, "Failed to concatenate subkey (minus slash) to key string");
}
else
{
hr = ::StringCchCatW(wzKey, countof(wzKey), wzSubKey);
ExitOnFailure(hr, "Failed to concatenate subkey to key string");
}
}
hr = WcaWriteIntegerToCaData(MBA_CREATEKEY, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase delete key directive to CustomActionData");
hr = WcaWriteStringToCaData(wzKey, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase key to CustomActionData");
hr = ScaAddToIisConfiguration(pwzCustomActionData, COST_IIS_CREATEKEY);
ExitOnFailure(hr, "Failed to add ScaCreateMetabaseKey action data: %ls cost: %d", pwzCustomActionData, COST_IIS_CREATEKEY);
LExit:
ReleaseStr(pwzCustomActionData);
return hr;
}
HRESULT ScaDeleteMetabaseKey(IMSAdminBase* piMetabase, LPCWSTR wzRootKey,
LPCWSTR wzSubKey)
{
Assert(piMetabase);
Unused(piMetabase);
HRESULT hr = S_OK;
WCHAR wzKey[METADATA_MAX_NAME_LEN];
WCHAR* pwzCustomActionData = NULL;
hr = ::StringCchCopyW(wzKey, countof(wzKey), wzRootKey);
ExitOnFailure(hr, "Failed to copy root key string to key");
if (L'/' != *(wzKey + lstrlenW(wzRootKey)))
{
hr = ::StringCchCatW(wzKey, countof(wzKey), L"/");
ExitOnFailure(hr, "Failed to concatenate / to key string");
}
if (*wzSubKey)
{
if (L'/' == *wzSubKey)
{
hr = ::StringCchCatW(wzKey, countof(wzKey), wzSubKey + 1);
ExitOnFailure(hr, "Failed to concatenate subkey (minus slash) to key string");
}
else
{
hr = ::StringCchCatW(wzKey, countof(wzKey), wzSubKey);
ExitOnFailure(hr, "Failed to concatenate subkey to key string");
}
}
hr = WcaWriteIntegerToCaData(MBA_DELETEKEY, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase delete key directive to CustomActionData");
hr = WcaWriteStringToCaData(wzKey, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase key to CustomActionData");
hr = ScaAddToIisConfiguration(pwzCustomActionData, COST_IIS_DELETEKEY);
ExitOnFailure(hr, "Failed to add ScaDeleteMetabaseKey action data: %ls cost: %d", pwzCustomActionData, COST_IIS_DELETEKEY);
LExit:
ReleaseStr(pwzCustomActionData);
return hr;
}
HRESULT ScaDeleteMetabaseValue(IMSAdminBase* piMetabase, LPCWSTR wzRootKey,
LPCWSTR wzSubKey, DWORD dwIdentifier,
DWORD dwDataType)
{
Assert(piMetabase);
Unused(piMetabase);
HRESULT hr = S_OK;
WCHAR wzKey[METADATA_MAX_NAME_LEN];
WCHAR* pwzCustomActionData = NULL;
hr = ::StringCchCopyW(wzKey, countof(wzKey), wzRootKey);
ExitOnFailure(hr, "Failed to copy root key string to key");
if (L'/' != *(wzKey + lstrlenW(wzRootKey)))
{
hr = ::StringCchCatW(wzKey, countof(wzKey), L"/");
ExitOnFailure(hr, "Failed to concatenate / to key string");
}
if (wzSubKey && *wzSubKey)
{
if (L'/' == *wzSubKey)
{
hr = ::StringCchCatW(wzKey, countof(wzKey), wzSubKey + 1);
ExitOnFailure(hr, "Failed to concatenate subkey (minus slash) to key string");
}
else
{
hr = ::StringCchCatW(wzKey, countof(wzKey), wzSubKey);
ExitOnFailure(hr, "Failed to concatenate subkey to key string");
}
}
hr = WcaWriteIntegerToCaData(MBA_DELETEVALUE, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase write value directive to CustomActionData");
hr = WcaWriteStringToCaData(wzKey, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase key to CustomActionData");
hr = WcaWriteIntegerToCaData(dwIdentifier, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase identifier to CustomActionData");
hr = WcaWriteIntegerToCaData(dwDataType, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase data type to CustomActionData");
hr = ScaAddToIisConfiguration(pwzCustomActionData, COST_IIS_DELETEVALUE);
ExitOnFailure(hr, "Failed to add ScaDeleteMetabaseValue action data: %ls, cost: %d", pwzCustomActionData, COST_IIS_DELETEVALUE);
LExit:
ReleaseStr(pwzCustomActionData);
return hr;
}
#pragma prefast(push)
#pragma prefast(disable:25120) // Disable "requires count parameter" warning - we do have a way to distinguish buffer sizes in all situations,
// it just depends on the dwDataType, and there's no way to annotate the situation in SAL
HRESULT ScaWriteMetabaseValue(IMSAdminBase* piMetabase, LPCWSTR wzRootKey,
LPCWSTR wzSubKey, DWORD dwIdentifier,
DWORD dwAttributes, DWORD dwUserType,
DWORD dwDataType, LPVOID pvData)
#pragma prefast(pop)
{
Assert(piMetabase && (pvData || (DWORD_METADATA == dwDataType))); // pvData may be 0 if it is DWORD data
Unused(piMetabase);
HRESULT hr = S_OK;
WCHAR wzKey[METADATA_MAX_NAME_LEN];
WCHAR* pwzCustomActionData = NULL;
if (BINARY_METADATA == dwDataType)
{
ExitOnNull(pvData, hr, E_INVALIDARG, "Failed to write binary metadata - no data available to write");
}
hr = ::StringCchCopyW(wzKey, countof(wzKey), wzRootKey);
ExitOnFailure(hr, "Failed to copy root key string to key");
if (L'/' != *(wzKey + lstrlenW(wzRootKey)))
{
hr = ::StringCchCatW(wzKey, countof(wzKey), L"/");
ExitOnFailure(hr, "Failed to concatenate / to key string");
}
if (wzSubKey && *wzSubKey)
{
if (L'/' == *wzSubKey)
{
hr = ::StringCchCatW(wzKey, countof(wzKey), wzSubKey + 1);
ExitOnFailure(hr, "Failed to concatenate subkey (minus slash) to key string");
}
else
{
hr = ::StringCchCatW(wzKey, countof(wzKey), wzSubKey);
ExitOnFailure(hr, "Failed to concatenate subkey to key string");
}
}
hr = WcaWriteIntegerToCaData(MBA_WRITEVALUE, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase write value directive to CustomActionData");
hr = WcaWriteStringToCaData(wzKey, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase key to CustomActionData");
hr = WcaWriteIntegerToCaData(dwIdentifier, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase identifier to CustomActionData");
hr = WcaWriteIntegerToCaData(dwAttributes, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase attributes to CustomActionData");
hr = WcaWriteIntegerToCaData(dwUserType, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase user type to CustomActionData");
hr = WcaWriteIntegerToCaData(dwDataType, &pwzCustomActionData);
ExitOnFailure(hr, "Failed to add metabase data type to CustomActionData");
switch (dwDataType)
{
case DWORD_METADATA:
hr = WcaWriteIntegerToCaData((DWORD)((DWORD_PTR)pvData), &pwzCustomActionData);
break;
case STRING_METADATA:
hr = WcaWriteStringToCaData((LPCWSTR)pvData, &pwzCustomActionData);
break;
case MULTISZ_METADATA:
{
// change NULLs to unprintable character to create a 'safe' MULTISZ string
LPWSTR pwz = (LPWSTR)pvData;
for (;;)
{
if ('\0' == *pwz)
{
*pwz = MAGIC_MULTISZ_CHAR;
if ('\0' == *(pwz + 1)) // second null back to back means end of string
break;
}
++pwz;
}
hr = WcaWriteStringToCaData((LPCWSTR)pvData, &pwzCustomActionData);
}
break;
case BINARY_METADATA:
hr = WcaWriteStreamToCaData(((BLOB*) pvData)->pBlobData, ((BLOB*) pvData)->cbSize, &pwzCustomActionData);
break;
default:
hr = E_UNEXPECTED;
}
ExitOnFailure(hr, "Failed to add metabase data to CustomActionData");
// TODO: maybe look the key up and make sure we're not just writing the same value that already there
hr = ScaAddToIisConfiguration(pwzCustomActionData, COST_IIS_WRITEVALUE);
ExitOnFailure(hr, "Failed to add ScaWriteMetabaseValue action data: %ls, cost: %d", pwzCustomActionData, COST_IIS_WRITEVALUE);
LExit:
ReleaseStr(pwzCustomActionData);
return hr;
}
HRESULT ScaAddToIisConfiguration(LPCWSTR pwzData, DWORD dwCost)
{
HRESULT hr = S_OK;
hr = WcaWriteStringToCaData(pwzData, &vpwzCustomActionData);
ExitOnFailure(hr, "failed to add to metabase configuration data string: %ls", pwzData);
vdwCustomActionCost += dwCost;
LExit:
return hr;
}
HRESULT ScaWriteConfigurationScript(__in LPCWSTR pwzCaScriptKey)
{
HRESULT hr = S_OK;
WCA_CASCRIPT_HANDLE hScript = NULL;
LPWSTR pwzHashString = NULL;
BYTE rgbActualHash[SHA1_HASH_LEN] = { };
DWORD dwHashedBytes = SHA1_HASH_LEN;
// Create CaScript for communication with WriteMetabaseChanges
hr = WcaCaScriptCreate(WCA_ACTION_INSTALL, WCA_CASCRIPT_SCHEDULED, FALSE, pwzCaScriptKey, FALSE, &hScript);
ExitOnFailure(hr, "Failed to write ca script for WriteMetabaseChanges script.");
if (vpwzCustomActionData && *vpwzCustomActionData)
{
// Write the actual custom action data to the ca script
WcaCaScriptWriteString(hScript, vpwzCustomActionData);
hr = CrypHashBuffer((BYTE*)vpwzCustomActionData, sizeof(vpwzCustomActionData) * sizeof(WCHAR), PROV_RSA_AES, CALG_SHA1, rgbActualHash, dwHashedBytes);
ExitOnFailure(hr, "Failed to calculate hash of CustomAction data.");
hr = StrAlloc(&pwzHashString, ((dwHashedBytes * 2) + 1));
ExitOnFailure(hr, "Failed to allocate string for script hash");
hr = StrHexEncode(rgbActualHash, dwHashedBytes, pwzHashString, ((dwHashedBytes * 2) + 1));
ExitOnFailure(hr, "Failed to convert hash bytes to string.");
WcaLog(LOGMSG_VERBOSE, "Custom action data hash: %ls", pwzHashString);
WcaLog(LOGMSG_TRACEONLY, "Custom action data being written to ca script: %ls", vpwzCustomActionData);
}
else
hr = S_FALSE;
LExit:
// Release the string
ReleaseStr(vpwzCustomActionData);
ReleaseStr(pwzHashString);
// Flush the ca script to disk as best we can
WcaCaScriptFlush(hScript);
WcaCaScriptClose(hScript, WCA_CASCRIPT_CLOSE_PRESERVE);
return hr;
}
HRESULT ScaLoadMetabase(IMSAdminBase** ppiMetabase)
{
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
// if IIS was uninstalled (thus no IID_IMSAdminBase) allow the
// user to still uninstall this package by clicking "Ignore"
do
{
hr = ::CoCreateInstance(CLSID_MSAdminBase, NULL, CLSCTX_ALL, IID_IMSAdminBase, (void**)ppiMetabase);
if (FAILED(hr))
{
WcaLog(LOGMSG_STANDARD, "failed to get IID_IMSAdminBase Object");
er = WcaErrorMessage(msierrIISCannotConnect, hr, INSTALLMESSAGE_ERROR | MB_ABORTRETRYIGNORE, 0);
switch (er)
{
case IDABORT:
ExitFunction(); // bail with the error result from the CoCreate to kick off a rollback
case IDRETRY:
hr = S_FALSE; // hit me, baby, one more time
break;
case IDIGNORE:
hr = S_OK; // pretend everything is okay and bail
break;
default: // For failure on uninstall continue
hr = S_OK;
break;
}
}
} while (S_FALSE == hr);
LExit:
return hr;
}
|