aboutsummaryrefslogtreecommitdiff
path: root/src/ext/Iis/ca/scaapppool7.cpp
blob: 0fac43462ea4d99da6e2fd0aba9453249ff40191 (plain)
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
// 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"

// prototypes
static HRESULT AppPoolExists(
    __in LPCWSTR wzAppPool
    );

// functions
HRESULT ScaFindAppPool7(
    __in LPCWSTR wzAppPool,
    __out_ecount(cchName) LPWSTR wzName,
    __in DWORD cchName,
    __in SCA_APPPOOL *psapList
    )
{
    Assert(wzAppPool && *wzAppPool && wzName && *wzName);

    HRESULT hr = S_OK;

    // check memory first
    SCA_APPPOOL* psap = psapList;
    for (; psap; psap = psap->psapNext)
    {
        if (0 == wcscmp(psap->wzAppPool, wzAppPool))
        {
            break;
        }
    }
    ExitOnNull(psap, hr, HRESULT_FROM_WIN32(ERROR_NOT_FOUND), "Could not find the app pool: %ls", wzAppPool);

    // copy the web app pool name
#pragma prefast(suppress:26037, "Source string is null terminated - it is populated as target of ::StringCchCopyW")
    hr = ::StringCchCopyW(wzName, cchName, psap->wzName);
    ExitOnFailure(hr, "failed to copy app pool name while finding app pool: %ls", psap->wzName);

    // if it's not being installed now, check if it exists already
    if (!psap->fHasComponent)
    {
        hr = AppPoolExists(psap->wzName);
        ExitOnFailure(hr, "failed to check for existence of app pool: %ls", psap->wzName);
    }

LExit:
    return hr;
}


static HRESULT AppPoolExists(
    __in LPCWSTR /*wzAppPool*/
    )
{
    HRESULT hr = S_OK;

    //this function checks for existance of app pool in IIS7 config
    //at schedule time, we will defer this to execute time.

    return hr;
}


HRESULT ScaAppPoolInstall7(
    __in SCA_APPPOOL* psapList
    )
{
    HRESULT hr = S_OK;

    for (SCA_APPPOOL* psap = psapList; psap; psap = psap->psapNext)
    {
        // if we are installing the app pool
        if (psap->fHasComponent && WcaIsInstalling(psap->isInstalled, psap->isAction))
        {
            hr = ScaWriteAppPool7(psap);
            ExitOnFailure(hr, "failed to write AppPool '%ls' to metabase", psap->wzAppPool);
        }
    }

LExit:
    return hr;
}


HRESULT ScaAppPoolUninstall7(
    __in SCA_APPPOOL* psapList
    )
{

    HRESULT hr = S_OK;

    for (SCA_APPPOOL* psap = psapList; psap; psap = psap->psapNext)
    {
        // if we are uninstalling the app pool
        if (psap->fHasComponent && WcaIsUninstalling(psap->isInstalled, psap->isAction))
        {
            hr = ScaRemoveAppPool7(psap);
            ExitOnFailure(hr, "Failed to remove AppPool '%ls' from metabase", psap->wzAppPool);
        }
    }

LExit:
    return hr;
}


HRESULT ScaWriteAppPool7(
    __in const SCA_APPPOOL* psap
    )
{
    Assert(psap);

    HRESULT hr = S_OK;
    DWORD dwIdentity = 0xFFFFFFFF;
    LPWSTR pwzValue = NULL;
    LPWSTR wz = NULL;

    //create the app pool
    hr = ScaWriteConfigID(IIS_APPPOOL);
    ExitOnFailure(hr, "failed to write AppPool key.");

    hr = ScaWriteConfigID(IIS_CREATE);
    ExitOnFailure(hr, "failed to write AppPool create action.");

    hr = ScaWriteConfigString(psap->wzName);
    ExitOnFailure(hr, "failed to write AppPool name: %ls", psap->wzName);

    // Now do all the optional stuff

    // Set the AppPool Recycling Tab
    if (MSI_NULL_INTEGER != psap->iRecycleMinutes)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_MIN);
        ExitOnFailure(hr, "failed to set periodic restart time id");
        hr = ScaWriteConfigInteger(psap->iRecycleMinutes);
        ExitOnFailure(hr, "failed to set periodic restart time");
    }

    if (MSI_NULL_INTEGER != psap->iRecycleRequests)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_REQ);
        ExitOnFailure(hr, "failed to set periodic restart request count id");
        hr = ScaWriteConfigInteger(psap->iRecycleRequests);
        ExitOnFailure(hr, "failed to set periodic restart request count");
    }

    if (*psap->wzRecycleTimes)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_TIMES);
        ExitOnFailure(hr, "failed to set periodic restart schedule id");
        hr = ScaWriteConfigString(psap->wzRecycleTimes);
        ExitOnFailure(hr, "failed to set periodic restart schedule");
    }

    if (MSI_NULL_INTEGER != psap->iVirtualMemory)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_VIRMEM);
        ExitOnFailure(hr, "failed to set periodic restart memory count id");
        hr = ScaWriteConfigInteger(psap->iVirtualMemory);
        ExitOnFailure(hr, "failed to set periodic restart memory count");
    }

    if (MSI_NULL_INTEGER != psap->iPrivateMemory)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_PRIVMEM);
        ExitOnFailure(hr, "failed to set periodic restart private memory count id");
        hr = ScaWriteConfigInteger(psap->iPrivateMemory);
        ExitOnFailure(hr, "failed to set periodic restart private memory count");
    }

    // Set AppPool Performance Tab
    if (MSI_NULL_INTEGER != psap->iIdleTimeout)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_IDLTIMEOUT);
        ExitOnFailure(hr, "failed to set idle timeout value id");
        hr = ScaWriteConfigInteger(psap->iIdleTimeout);
        ExitOnFailure(hr, "failed to set idle timeout value");
    }

    if (MSI_NULL_INTEGER != psap->iQueueLimit)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_QUEUELIMIT);
        ExitOnFailure(hr, "failed to set request queue limit value id");
        hr = ScaWriteConfigInteger(psap->iQueueLimit);
        ExitOnFailure(hr, "failed to set request queue limit value");
    }
    if (*psap->wzCpuMon)
    {
#pragma prefast(suppress:26037, "Source string is null terminated - it is populated as target of ::StringCchCopyW")
        hr = ::StrAllocString(&pwzValue, psap->wzCpuMon, 0);
        ExitOnFailure(hr, "failed to allocate CPUMonitor string");

        DWORD dwPercent = 0;
        DWORD dwRefreshMinutes = 0;
        DWORD dwAction = 0;

        dwPercent = wcstoul(pwzValue, &wz, 10);
        if (100  < dwPercent)
        {
            ExitOnFailure(hr = E_INVALIDARG, "invalid maximum cpu percentage value: %d", dwPercent);
        }
        if (wz && L',' == *wz)
        {
            ++wz;
            dwRefreshMinutes = wcstoul(wz, &wz, 10);
            if (wz && L',' == *wz)
            {
                ++wz;
                dwAction = wcstoul(wz, &wz, 10);
            }
        }
        if (dwPercent)
        {
            hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_CPU_PCT);
            ExitOnFailure(hr, "failed to set recycle pct id");
            hr = ScaWriteConfigInteger(dwPercent);
            ExitOnFailure(hr, "failed to set CPU percentage max");
        }
        if (dwRefreshMinutes)
        {
            hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_CPU_REFRESH);
            ExitOnFailure(hr, "failed to set recycle refresh id");
            hr = ScaWriteConfigInteger(dwRefreshMinutes);
            ExitOnFailure(hr, "failed to set refresh CPU minutes");
        }
        if (dwAction)
        {
            // 0 = No Action
            // 1 = Shutdown
            hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_CPU_ACTION);
            ExitOnFailure(hr, "failed to set recycle refresh id");
            hr = ScaWriteConfigInteger(dwAction);
            ExitOnFailure(hr, "failed to set CPU action");
        }
    }

    if (MSI_NULL_INTEGER != psap->iMaxProcesses)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_MAXPROCESS);
        ExitOnFailure(hr, "Failed to write max processes config ID");

        hr = ScaWriteConfigInteger(psap->iMaxProcesses);
        ExitOnFailure(hr, "failed to set web garden maximum worker processes");
    }

    hr = ScaWriteConfigID(IIS_APPPOOL_32BIT);
    ExitOnFailure(hr, "Failed to write 32 bit app pool config ID");
    hr = ScaWriteConfigInteger(psap->iCompAttributes & msidbComponentAttributes64bit ? 0 : 1);
    ExitOnFailure(hr, "Failed to write 32 bit app pool config value");

    //
    // Set the AppPool Identity tab
    //
    if (psap->iAttributes & APATTR_APPPOOLIDENTITY)
    {
        dwIdentity = 4;
    }
    else if (psap->iAttributes & APATTR_NETSERVICE)
    {
        dwIdentity = 2;
    }
    else if (psap->iAttributes & APATTR_LOCSERVICE)
    {
        dwIdentity = 1;
    }
    else if (psap->iAttributes & APATTR_LOCSYSTEM)
    {
        dwIdentity = 0;
    }
    else if (psap->iAttributes & APATTR_OTHERUSER)
    {
        if (!*psap->suUser.wzDomain || 0 == _wcsicmp(psap->suUser.wzDomain, L"."))
        {
            if (0 == _wcsicmp(psap->suUser.wzName, L"NetworkService"))
            {
                dwIdentity = 2;
            }
            else if (0 == _wcsicmp(psap->suUser.wzName, L"LocalService"))
            {
                dwIdentity = 1;
            }
            else if (0 == _wcsicmp(psap->suUser.wzName, L"LocalSystem"))
            {
                dwIdentity = 0;
            }
            else
            {
                dwIdentity = 3;
            }
        }
        else if (0 == _wcsicmp(psap->suUser.wzDomain, L"NT AUTHORITY"))
        {
            if (0 == _wcsicmp(psap->suUser.wzName, L"NETWORK SERVICE"))
            {
                dwIdentity = 2;
            }
            else if (0 == _wcsicmp(psap->suUser.wzName, L"SERVICE"))
            {
                dwIdentity = 1;
            }
            else if (0 == _wcsicmp(psap->suUser.wzName, L"SYSTEM"))
            {
                dwIdentity = 0;
            }
            else
            {
                dwIdentity = 3;
            }
        }
        else
        {
            dwIdentity = 3;
        }
    }

    if (-1 != dwIdentity)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_IDENTITY);
        ExitOnFailure(hr, "failed to set app pool identity id");
        hr = ScaWriteConfigInteger(dwIdentity);
        ExitOnFailure(hr, "failed to set app pool identity");

        if (3 == dwIdentity)
        {
            if (*psap->suUser.wzDomain)
            {
                hr = StrAllocFormatted(&pwzValue, L"%s\\%s", psap->suUser.wzDomain, psap->suUser.wzName);
                ExitOnFailure(hr, "failed to format user name: %ls domain: %ls", psap->suUser.wzName, psap->suUser.wzDomain);
            }
            else
            {
                hr = StrAllocFormatted(&pwzValue, L"%s", psap->suUser.wzName);
                ExitOnFailure(hr, "failed to format user name: %ls", psap->suUser.wzName);
            }

            hr = ScaWriteConfigID(IIS_APPPOOL_USER);
            ExitOnFailure(hr, "failed to set app pool identity name id");
            hr = ScaWriteConfigString(pwzValue);
            ExitOnFailure(hr, "failed to set app pool identity name");

            hr = ScaWriteConfigID(IIS_APPPOOL_PWD);
            ExitOnFailure(hr, "failed to set app pool identity password id");
            hr = ScaWriteConfigString(psap->suUser.wzPassword);
            ExitOnFailure(hr, "failed to set app pool identity password");
        }
    }
    
    if (*psap->wzManagedPipelineMode)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_MANAGED_PIPELINE_MODE);
        ExitOnFailure(hr, "failed to set app pool integrated mode");
        hr = ScaWriteConfigString(psap->wzManagedPipelineMode);
        ExitOnFailure(hr, "failed to set app pool managed pipeline mode value");
    }

    if (*psap->wzManagedRuntimeVersion)
    {
        hr = ScaWriteConfigID(IIS_APPPOOL_MANAGED_RUNTIME_VERSION);
        ExitOnFailure(hr, "failed to set app pool managed runtime version mode");
        hr = ScaWriteConfigString(psap->wzManagedRuntimeVersion);
        ExitOnFailure(hr, "failed to set app pool managed runtime version value");
    }

    //
    //The number of properties above is variable so we put an end tag in so the
    //execute CA will know when to stop looking for AppPool properties
    //
    hr = ScaWriteConfigID(IIS_APPPOOL_END);
    ExitOnFailure(hr, "failed to set app pool end of properties id");

LExit:
    ReleaseStr(pwzValue);

    return hr;
}


HRESULT ScaRemoveAppPool7(
    __in const SCA_APPPOOL* psap
    )
{
    Assert(psap);

    HRESULT hr = S_OK;

    //do not delete the default App Pool
    if (0 != _wcsicmp(psap->wzAppPool, L"DefaultAppPool"))
    {
        //delete the app pool
        hr = ScaWriteConfigID(IIS_APPPOOL);
        ExitOnFailure(hr, "failed to write AppPool key.");

        hr = ScaWriteConfigID(IIS_DELETE);
        ExitOnFailure(hr, "failed to write AppPool delete action.");

        hr = ScaWriteConfigString(psap->wzName);
        ExitOnFailure(hr, "failed to delete AppPool: %ls", psap->wzName);
    }

LExit:
    return hr;
}