| 1 | // 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. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | // prototypes |
| 6 | static HRESULT AppPoolExists( |
| 7 | __in LPCWSTR wzAppPool |
| 8 | ); |
| 9 | |
| 10 | // functions |
| 11 | HRESULT ScaFindAppPool7( |
| 12 | __in LPCWSTR wzAppPool, |
| 13 | __out_ecount(cchName) LPWSTR wzName, |
| 14 | __in DWORD cchName, |
| 15 | __in SCA_APPPOOL *psapList |
| 16 | ) |
| 17 | { |
| 18 | Assert(wzAppPool && *wzAppPool && wzName && *wzName); |
| 19 | |
| 20 | HRESULT hr = S_OK; |
| 21 | |
| 22 | // check memory first |
| 23 | SCA_APPPOOL* psap = psapList; |
| 24 | for (; psap; psap = psap->psapNext) |
| 25 | { |
| 26 | if (0 == wcscmp(psap->wzAppPool, wzAppPool)) |
| 27 | { |
| 28 | break; |
| 29 | } |
| 30 | } |
| 31 | ExitOnNull(psap, hr, HRESULT_FROM_WIN32(ERROR_NOT_FOUND), "Could not find the app pool: %ls", wzAppPool); |
| 32 | |
| 33 | // copy the web app pool name |
| 34 | #pragma prefast(suppress:26037, "Source string is null terminated - it is populated as target of ::StringCchCopyW") |
| 35 | hr = ::StringCchCopyW(wzName, cchName, psap->wzName); |
| 36 | ExitOnFailure(hr, "failed to copy app pool name while finding app pool: %ls", psap->wzName); |
| 37 | |
| 38 | // if it's not being installed now, check if it exists already |
| 39 | if (!psap->fHasComponent) |
| 40 | { |
| 41 | hr = AppPoolExists(psap->wzName); |
| 42 | ExitOnFailure(hr, "failed to check for existence of app pool: %ls", psap->wzName); |
| 43 | } |
| 44 | |
| 45 | LExit: |
| 46 | return hr; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static HRESULT AppPoolExists( |
| 51 | __in LPCWSTR /*wzAppPool*/ |
| 52 | ) |
| 53 | { |
| 54 | HRESULT hr = S_OK; |
| 55 | |
| 56 | //this function checks for existance of app pool in IIS7 config |
| 57 | //at schedule time, we will defer this to execute time. |
| 58 | |
| 59 | return hr; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | HRESULT ScaAppPoolInstall7( |
| 64 | __in SCA_APPPOOL* psapList |
| 65 | ) |
| 66 | { |
| 67 | HRESULT hr = S_OK; |
| 68 | |
| 69 | for (SCA_APPPOOL* psap = psapList; psap; psap = psap->psapNext) |
| 70 | { |
| 71 | // if we are installing the app pool |
| 72 | if (psap->fHasComponent && WcaIsInstalling(psap->isInstalled, psap->isAction)) |
| 73 | { |
| 74 | hr = ScaWriteAppPool7(psap); |
| 75 | ExitOnFailure(hr, "failed to write AppPool '%ls' to metabase", psap->wzAppPool); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | LExit: |
| 80 | return hr; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | HRESULT ScaAppPoolUninstall7( |
| 85 | __in SCA_APPPOOL* psapList |
| 86 | ) |
| 87 | { |
| 88 | |
| 89 | HRESULT hr = S_OK; |
| 90 | |
| 91 | for (SCA_APPPOOL* psap = psapList; psap; psap = psap->psapNext) |
| 92 | { |
| 93 | // if we are uninstalling the app pool |
| 94 | if (psap->fHasComponent && WcaIsUninstalling(psap->isInstalled, psap->isAction)) |
| 95 | { |
| 96 | hr = ScaRemoveAppPool7(psap); |
| 97 | ExitOnFailure(hr, "Failed to remove AppPool '%ls' from metabase", psap->wzAppPool); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | LExit: |
| 102 | return hr; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | HRESULT ScaWriteAppPool7( |
| 107 | __in const SCA_APPPOOL* psap |
| 108 | ) |
| 109 | { |
| 110 | Assert(psap); |
| 111 | |
| 112 | HRESULT hr = S_OK; |
| 113 | DWORD dwIdentity = 0xFFFFFFFF; |
| 114 | LPWSTR pwzValue = NULL; |
| 115 | LPWSTR wz = NULL; |
| 116 | |
| 117 | //create the app pool |
| 118 | hr = ScaWriteConfigID(IIS_APPPOOL); |
| 119 | ExitOnFailure(hr, "failed to write AppPool key."); |
| 120 | |
| 121 | hr = ScaWriteConfigID(IIS_CREATE); |
| 122 | ExitOnFailure(hr, "failed to write AppPool create action."); |
| 123 | |
| 124 | hr = ScaWriteConfigString(psap->wzName); |
| 125 | ExitOnFailure(hr, "failed to write AppPool name: %ls", psap->wzName); |
| 126 | |
| 127 | // Now do all the optional stuff |
| 128 | |
| 129 | // Set the AppPool Recycling Tab |
| 130 | if (MSI_NULL_INTEGER != psap->iRecycleMinutes) |
| 131 | { |
| 132 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_MIN); |
| 133 | ExitOnFailure(hr, "failed to set periodic restart time id"); |
| 134 | hr = ScaWriteConfigInteger(psap->iRecycleMinutes); |
| 135 | ExitOnFailure(hr, "failed to set periodic restart time"); |
| 136 | } |
| 137 | |
| 138 | if (MSI_NULL_INTEGER != psap->iRecycleRequests) |
| 139 | { |
| 140 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_REQ); |
| 141 | ExitOnFailure(hr, "failed to set periodic restart request count id"); |
| 142 | hr = ScaWriteConfigInteger(psap->iRecycleRequests); |
| 143 | ExitOnFailure(hr, "failed to set periodic restart request count"); |
| 144 | } |
| 145 | |
| 146 | if (*psap->wzRecycleTimes) |
| 147 | { |
| 148 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_TIMES); |
| 149 | ExitOnFailure(hr, "failed to set periodic restart schedule id"); |
| 150 | hr = ScaWriteConfigString(psap->wzRecycleTimes); |
| 151 | ExitOnFailure(hr, "failed to set periodic restart schedule"); |
| 152 | } |
| 153 | |
| 154 | if (MSI_NULL_INTEGER != psap->iVirtualMemory) |
| 155 | { |
| 156 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_VIRMEM); |
| 157 | ExitOnFailure(hr, "failed to set periodic restart memory count id"); |
| 158 | hr = ScaWriteConfigInteger(psap->iVirtualMemory); |
| 159 | ExitOnFailure(hr, "failed to set periodic restart memory count"); |
| 160 | } |
| 161 | |
| 162 | if (MSI_NULL_INTEGER != psap->iPrivateMemory) |
| 163 | { |
| 164 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_PRIVMEM); |
| 165 | ExitOnFailure(hr, "failed to set periodic restart private memory count id"); |
| 166 | hr = ScaWriteConfigInteger(psap->iPrivateMemory); |
| 167 | ExitOnFailure(hr, "failed to set periodic restart private memory count"); |
| 168 | } |
| 169 | |
| 170 | // Set AppPool Performance Tab |
| 171 | if (MSI_NULL_INTEGER != psap->iIdleTimeout) |
| 172 | { |
| 173 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_IDLTIMEOUT); |
| 174 | ExitOnFailure(hr, "failed to set idle timeout value id"); |
| 175 | hr = ScaWriteConfigInteger(psap->iIdleTimeout); |
| 176 | ExitOnFailure(hr, "failed to set idle timeout value"); |
| 177 | } |
| 178 | |
| 179 | if (MSI_NULL_INTEGER != psap->iQueueLimit) |
| 180 | { |
| 181 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_QUEUELIMIT); |
| 182 | ExitOnFailure(hr, "failed to set request queue limit value id"); |
| 183 | hr = ScaWriteConfigInteger(psap->iQueueLimit); |
| 184 | ExitOnFailure(hr, "failed to set request queue limit value"); |
| 185 | } |
| 186 | if (*psap->wzCpuMon) |
| 187 | { |
| 188 | #pragma prefast(suppress:26037, "Source string is null terminated - it is populated as target of ::StringCchCopyW") |
| 189 | hr = ::StrAllocString(&pwzValue, psap->wzCpuMon, 0); |
| 190 | ExitOnFailure(hr, "failed to allocate CPUMonitor string"); |
| 191 | |
| 192 | DWORD dwPercent = 0; |
| 193 | DWORD dwRefreshMinutes = 0; |
| 194 | DWORD dwAction = 0; |
| 195 | |
| 196 | dwPercent = wcstoul(pwzValue, &wz, 10); |
| 197 | if (100 < dwPercent) |
| 198 | { |
| 199 | ExitOnFailure(hr = E_INVALIDARG, "invalid maximum cpu percentage value: %d", dwPercent); |
| 200 | } |
| 201 | if (wz && L',' == *wz) |
| 202 | { |
| 203 | ++wz; |
| 204 | dwRefreshMinutes = wcstoul(wz, &wz, 10); |
| 205 | if (wz && L',' == *wz) |
| 206 | { |
| 207 | ++wz; |
| 208 | dwAction = wcstoul(wz, &wz, 10); |
| 209 | } |
| 210 | } |
| 211 | if (dwPercent) |
| 212 | { |
| 213 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_CPU_PCT); |
| 214 | ExitOnFailure(hr, "failed to set recycle pct id"); |
| 215 | hr = ScaWriteConfigInteger(dwPercent); |
| 216 | ExitOnFailure(hr, "failed to set CPU percentage max"); |
| 217 | } |
| 218 | if (dwRefreshMinutes) |
| 219 | { |
| 220 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_CPU_REFRESH); |
| 221 | ExitOnFailure(hr, "failed to set recycle refresh id"); |
| 222 | hr = ScaWriteConfigInteger(dwRefreshMinutes); |
| 223 | ExitOnFailure(hr, "failed to set refresh CPU minutes"); |
| 224 | } |
| 225 | if (dwAction) |
| 226 | { |
| 227 | // 0 = No Action |
| 228 | // 1 = Shutdown |
| 229 | hr = ScaWriteConfigID(IIS_APPPOOL_RECYCLE_CPU_ACTION); |
| 230 | ExitOnFailure(hr, "failed to set recycle refresh id"); |
| 231 | hr = ScaWriteConfigInteger(dwAction); |
| 232 | ExitOnFailure(hr, "failed to set CPU action"); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (MSI_NULL_INTEGER != psap->iMaxProcesses) |
| 237 | { |
| 238 | hr = ScaWriteConfigID(IIS_APPPOOL_MAXPROCESS); |
| 239 | ExitOnFailure(hr, "Failed to write max processes config ID"); |
| 240 | |
| 241 | hr = ScaWriteConfigInteger(psap->iMaxProcesses); |
| 242 | ExitOnFailure(hr, "failed to set web garden maximum worker processes"); |
| 243 | } |
| 244 | |
| 245 | hr = ScaWriteConfigID(IIS_APPPOOL_32BIT); |
| 246 | ExitOnFailure(hr, "Failed to write 32 bit app pool config ID"); |
| 247 | hr = ScaWriteConfigInteger(psap->iCompAttributes & msidbComponentAttributes64bit ? 0 : 1); |
| 248 | ExitOnFailure(hr, "Failed to write 32 bit app pool config value"); |
| 249 | |
| 250 | // |
| 251 | // Set the AppPool Identity tab |
| 252 | // |
| 253 | if (psap->iAttributes & APATTR_APPPOOLIDENTITY) |
| 254 | { |
| 255 | dwIdentity = 4; |
| 256 | } |
| 257 | else if (psap->iAttributes & APATTR_NETSERVICE) |
| 258 | { |
| 259 | dwIdentity = 2; |
| 260 | } |
| 261 | else if (psap->iAttributes & APATTR_LOCSERVICE) |
| 262 | { |
| 263 | dwIdentity = 1; |
| 264 | } |
| 265 | else if (psap->iAttributes & APATTR_LOCSYSTEM) |
| 266 | { |
| 267 | dwIdentity = 0; |
| 268 | } |
| 269 | else if (psap->iAttributes & APATTR_OTHERUSER) |
| 270 | { |
| 271 | if (!*psap->suUser.wzDomain || 0 == _wcsicmp(psap->suUser.wzDomain, L".")) |
| 272 | { |
| 273 | if (0 == _wcsicmp(psap->suUser.wzName, L"NetworkService")) |
| 274 | { |
| 275 | dwIdentity = 2; |
| 276 | } |
| 277 | else if (0 == _wcsicmp(psap->suUser.wzName, L"LocalService")) |
| 278 | { |
| 279 | dwIdentity = 1; |
| 280 | } |
| 281 | else if (0 == _wcsicmp(psap->suUser.wzName, L"LocalSystem")) |
| 282 | { |
| 283 | dwIdentity = 0; |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | dwIdentity = 3; |
| 288 | } |
| 289 | } |
| 290 | else if (0 == _wcsicmp(psap->suUser.wzDomain, L"NT AUTHORITY")) |
| 291 | { |
| 292 | if (0 == _wcsicmp(psap->suUser.wzName, L"NETWORK SERVICE")) |
| 293 | { |
| 294 | dwIdentity = 2; |
| 295 | } |
| 296 | else if (0 == _wcsicmp(psap->suUser.wzName, L"SERVICE")) |
| 297 | { |
| 298 | dwIdentity = 1; |
| 299 | } |
| 300 | else if (0 == _wcsicmp(psap->suUser.wzName, L"SYSTEM")) |
| 301 | { |
| 302 | dwIdentity = 0; |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | dwIdentity = 3; |
| 307 | } |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | dwIdentity = 3; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (-1 != dwIdentity) |
| 316 | { |
| 317 | hr = ScaWriteConfigID(IIS_APPPOOL_IDENTITY); |
| 318 | ExitOnFailure(hr, "failed to set app pool identity id"); |
| 319 | hr = ScaWriteConfigInteger(dwIdentity); |
| 320 | ExitOnFailure(hr, "failed to set app pool identity"); |
| 321 | |
| 322 | if (3 == dwIdentity) |
| 323 | { |
| 324 | if (*psap->suUser.wzDomain) |
| 325 | { |
| 326 | hr = StrAllocFormatted(&pwzValue, L"%s\\%s", psap->suUser.wzDomain, psap->suUser.wzName); |
| 327 | ExitOnFailure(hr, "failed to format user name: %ls domain: %ls", psap->suUser.wzName, psap->suUser.wzDomain); |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | hr = StrAllocFormatted(&pwzValue, L"%s", psap->suUser.wzName); |
| 332 | ExitOnFailure(hr, "failed to format user name: %ls", psap->suUser.wzName); |
| 333 | } |
| 334 | |
| 335 | hr = ScaWriteConfigID(IIS_APPPOOL_USER); |
| 336 | ExitOnFailure(hr, "failed to set app pool identity name id"); |
| 337 | hr = ScaWriteConfigString(pwzValue); |
| 338 | ExitOnFailure(hr, "failed to set app pool identity name"); |
| 339 | |
| 340 | hr = ScaWriteConfigID(IIS_APPPOOL_PWD); |
| 341 | ExitOnFailure(hr, "failed to set app pool identity password id"); |
| 342 | hr = ScaWriteConfigString(psap->suUser.wzPassword); |
| 343 | ExitOnFailure(hr, "failed to set app pool identity password"); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if (*psap->wzManagedPipelineMode) |
| 348 | { |
| 349 | hr = ScaWriteConfigID(IIS_APPPOOL_MANAGED_PIPELINE_MODE); |
| 350 | ExitOnFailure(hr, "failed to set app pool integrated mode"); |
| 351 | hr = ScaWriteConfigString(psap->wzManagedPipelineMode); |
| 352 | ExitOnFailure(hr, "failed to set app pool managed pipeline mode value"); |
| 353 | } |
| 354 | |
| 355 | if (*psap->wzManagedRuntimeVersion) |
| 356 | { |
| 357 | hr = ScaWriteConfigID(IIS_APPPOOL_MANAGED_RUNTIME_VERSION); |
| 358 | ExitOnFailure(hr, "failed to set app pool managed runtime version mode"); |
| 359 | hr = ScaWriteConfigString(psap->wzManagedRuntimeVersion); |
| 360 | ExitOnFailure(hr, "failed to set app pool managed runtime version value"); |
| 361 | } |
| 362 | |
| 363 | // |
| 364 | //The number of properties above is variable so we put an end tag in so the |
| 365 | //execute CA will know when to stop looking for AppPool properties |
| 366 | // |
| 367 | hr = ScaWriteConfigID(IIS_APPPOOL_END); |
| 368 | ExitOnFailure(hr, "failed to set app pool end of properties id"); |
| 369 | |
| 370 | LExit: |
| 371 | ReleaseStr(pwzValue); |
| 372 | |
| 373 | return hr; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | HRESULT ScaRemoveAppPool7( |
| 378 | __in const SCA_APPPOOL* psap |
| 379 | ) |
| 380 | { |
| 381 | Assert(psap); |
| 382 | |
| 383 | HRESULT hr = S_OK; |
| 384 | |
| 385 | //do not delete the default App Pool |
| 386 | if (0 != _wcsicmp(psap->wzAppPool, L"DefaultAppPool")) |
| 387 | { |
| 388 | //delete the app pool |
| 389 | hr = ScaWriteConfigID(IIS_APPPOOL); |
| 390 | ExitOnFailure(hr, "failed to write AppPool key."); |
| 391 | |
| 392 | hr = ScaWriteConfigID(IIS_DELETE); |
| 393 | ExitOnFailure(hr, "failed to write AppPool delete action."); |
| 394 | |
| 395 | hr = ScaWriteConfigString(psap->wzName); |
| 396 | ExitOnFailure(hr, "failed to delete AppPool: %ls", psap->wzName); |
| 397 | } |
| 398 | |
| 399 | LExit: |
| 400 | return hr; |
| 401 | } |