| 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 | enum eWebLogQuery { wlqLog = 1, wlqFormat }; |
| 6 | |
| 7 | /* **************************************************************** |
| 8 | * LookupLogFormatGUID -Looks up a given IIS Log format type in |
| 9 | * the metabase and returns the GUID for it. |
| 10 | * ****************************************************************/ |
| 11 | static HRESULT LookupLogFormatGUID( |
| 12 | __in IMSAdminBase* piMetabase, |
| 13 | __in LPCWSTR wzLogFormat, |
| 14 | __out_ecount(cchGUID) LPWSTR wzGUID, |
| 15 | __in int cchGUID |
| 16 | ) |
| 17 | { |
| 18 | WCHAR wzKey[METADATA_MAX_NAME_LEN]; |
| 19 | HRESULT hr = S_OK; |
| 20 | |
| 21 | METADATA_RECORD mrKeyType; |
| 22 | ::ZeroMemory(&mrKeyType, sizeof(mrKeyType)); |
| 23 | |
| 24 | mrKeyType.dwMDIdentifier = MD_KEY_TYPE; |
| 25 | mrKeyType.dwMDAttributes = METADATA_NO_ATTRIBUTES; |
| 26 | mrKeyType.dwMDUserType = IIS_MD_UT_SERVER; |
| 27 | mrKeyType.dwMDDataType = ALL_METADATA; |
| 28 | mrKeyType.dwMDDataLen = 0; |
| 29 | mrKeyType.pbMDData = NULL; |
| 30 | |
| 31 | METADATA_RECORD mrPluginId; |
| 32 | ::ZeroMemory(&mrPluginId, sizeof(mrPluginId)); |
| 33 | |
| 34 | mrPluginId.dwMDIdentifier = MD_LOG_PLUGIN_MOD_ID; |
| 35 | mrPluginId.dwMDAttributes = METADATA_INHERIT; |
| 36 | mrPluginId.dwMDUserType = IIS_MD_UT_SERVER; |
| 37 | mrPluginId.dwMDDataType = ALL_METADATA; |
| 38 | mrPluginId.dwMDDataLen = 0; |
| 39 | mrPluginId.pbMDData = NULL; |
| 40 | |
| 41 | hr = ::StringCchPrintfW(wzKey, countof(wzKey), L"/LM/Logging/%s", wzLogFormat); |
| 42 | ExitOnFailure(hr, "failed to format logging metabase key name"); |
| 43 | |
| 44 | // verify that we have this log format available in IIS |
| 45 | hr = MetaGetValue(piMetabase, METADATA_MASTER_ROOT_HANDLE, wzKey, &mrKeyType); |
| 46 | ExitOnFailure(hr, "Failed to find specified Log format key in IIS for log format: %ls", wzLogFormat); |
| 47 | ExitOnNull(mrKeyType.pbMDData, hr, E_POINTER, "Request for Log format key in IIS for log format returned success, but value was NULL"); |
| 48 | |
| 49 | if (0 != lstrcmpW(L"IIsLogModule", (LPCWSTR)mrKeyType.pbMDData)) |
| 50 | { |
| 51 | ExitOnFailure(hr = E_UNEXPECTED, "Found invalid log format in IIS: %ls", (LPCWSTR)mrKeyType.pbMDData); |
| 52 | } |
| 53 | |
| 54 | // find the GUID for that log format |
| 55 | hr = MetaGetValue(piMetabase, METADATA_MASTER_ROOT_HANDLE, wzKey, &mrPluginId); |
| 56 | ExitOnFailure(hr, "Failed to retrieve IISLog format GUID. Key: %ls", wzKey); |
| 57 | ExitOnNull(mrPluginId.pbMDData, hr, E_POINTER, "Retrieval of IISLog format GUID returned success, but value was NULL"); |
| 58 | |
| 59 | hr = ::StringCchCopyW(wzGUID, cchGUID, (LPCWSTR)mrPluginId.pbMDData); |
| 60 | ExitOnFailure(hr, "failed to copy metabase value: %ls", (LPCWSTR)mrPluginId.pbMDData); |
| 61 | |
| 62 | LExit: |
| 63 | |
| 64 | if (mrKeyType.pbMDData) |
| 65 | { |
| 66 | MetaFreeValue(&mrKeyType); |
| 67 | } |
| 68 | |
| 69 | if (mrPluginId.pbMDData) |
| 70 | { |
| 71 | MetaFreeValue(&mrPluginId); |
| 72 | } |
| 73 | |
| 74 | return hr; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* **************************************************************** |
| 79 | * ScaGetWebLog -Retrieves Log table data for the specified Log key |
| 80 | * |
| 81 | * ****************************************************************/ |
| 82 | HRESULT ScaGetWebLog( |
| 83 | IMSAdminBase* piMetabase, |
| 84 | LPCWSTR wzLog, |
| 85 | __in WCA_WRAPQUERY_HANDLE hWebLogQuery, |
| 86 | SCA_WEB_LOG* pswl |
| 87 | ) |
| 88 | { |
| 89 | HRESULT hr = S_OK; |
| 90 | LPWSTR pwzData = NULL; |
| 91 | MSIHANDLE hRec; |
| 92 | |
| 93 | if (0 == WcaGetQueryRecords(hWebLogQuery)) |
| 94 | { |
| 95 | WcaLog(LOGMSG_VERBOSE, "Skipping ScaGetWebLog() - no records to process"); |
| 96 | ExitFunction1(hr = S_FALSE); |
| 97 | } |
| 98 | |
| 99 | WcaFetchWrappedReset(hWebLogQuery); |
| 100 | |
| 101 | hr = WcaFetchWrappedRecordWhereString(hWebLogQuery, wlqLog, wzLog, &hRec); |
| 102 | if (E_NOMOREITEMS == hr) |
| 103 | { |
| 104 | ExitOnFailure(hr, "cannot locate IIsWebLog.Log='%ls'", wzLog); |
| 105 | } |
| 106 | HRESULT hrTemp = WcaFetchWrappedRecordWhereString(hWebLogQuery, wlqLog, wzLog, &hRec); |
| 107 | |
| 108 | if (SUCCEEDED(hrTemp)) |
| 109 | { |
| 110 | ExitOnFailure(hr, "error - found multiple matching IIsWebLog rows"); |
| 111 | } |
| 112 | |
| 113 | ::ZeroMemory(pswl, sizeof(SCA_WEB_LOG)); |
| 114 | |
| 115 | // check that log key matches |
| 116 | hr = WcaGetRecordString(hRec, wlqLog, &pwzData); |
| 117 | ExitOnFailure(hr, "failed to get IIsWebLog.Log for Log: %ls", wzLog); |
| 118 | hr = ::StringCchCopyW(pswl->wzLog, countof(pswl->wzLog), pwzData); |
| 119 | ExitOnFailure(hr, "failed to copy log name: %ls", pwzData); |
| 120 | |
| 121 | hr = WcaGetRecordString(hRec, wlqFormat, &pwzData); |
| 122 | ExitOnFailure(hr, "failed to get IIsWebLog.Format for Log:", wzLog); |
| 123 | hr = ::StringCchCopyW(pswl->wzFormat, countof(pswl->wzFormat), pwzData); |
| 124 | ExitOnFailure(hr, "failed to copy log format: %ls", pwzData); |
| 125 | |
| 126 | // if they specified a log format, look up its GUID in the metabase |
| 127 | if (*pswl->wzFormat && 0 != lstrcmpW(pswl->wzFormat, L"none")) |
| 128 | { |
| 129 | hr = LookupLogFormatGUID(piMetabase, pswl->wzFormat, pswl->wzFormatGUID, countof(pswl->wzFormatGUID)); |
| 130 | ExitOnFailure(hr, "Failed to get Log Format GUID for Log: %ls", wzLog); |
| 131 | } |
| 132 | |
| 133 | LExit: |
| 134 | ReleaseStr(pwzData); |
| 135 | |
| 136 | return hr; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /* **************************************************************** |
| 141 | * ScaWriteWebLog -Writes the IIS log values to the metabase. |
| 142 | * |
| 143 | * ****************************************************************/ |
| 144 | HRESULT ScaWriteWebLog( |
| 145 | IMSAdminBase* piMetabase, |
| 146 | LPCWSTR wzWebBase, |
| 147 | SCA_WEB_LOG *pswl |
| 148 | ) |
| 149 | { |
| 150 | HRESULT hr = S_OK; |
| 151 | |
| 152 | if (*pswl->wzFormat) |
| 153 | { |
| 154 | if (0 == lstrcmpW(pswl->wzFormat, L"none")) |
| 155 | { |
| 156 | // user wishes for Logging to be turned 'off' |
| 157 | hr = ScaWriteMetabaseValue(piMetabase, wzWebBase, L"", MD_LOG_TYPE, METADATA_INHERIT, IIS_MD_UT_SERVER, DWORD_METADATA, (LPVOID)((DWORD_PTR)0)); |
| 158 | ExitOnFailure(hr, "Failed to write Log Type for Web: %ls", wzWebBase); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | Assert(*pswl->wzFormatGUID); |
| 163 | |
| 164 | // write the GUID for the log format for the web to the metabase |
| 165 | hr = ScaWriteMetabaseValue(piMetabase, wzWebBase, L"", MD_LOG_PLUGIN_ORDER, METADATA_INHERIT, IIS_MD_UT_SERVER, STRING_METADATA, pswl->wzFormatGUID); |
| 166 | ExitOnFailure(hr, "Failed to write Log GUID for Web: %ls", wzWebBase); |
| 167 | |
| 168 | hr = ScaWriteMetabaseValue(piMetabase, wzWebBase, L"", MD_LOG_TYPE, METADATA_INHERIT, IIS_MD_UT_SERVER, DWORD_METADATA, (LPVOID)((DWORD_PTR)1)); |
| 169 | ExitOnFailure(hr, "Failed to write Log Type for Web: %ls", wzWebBase); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | LExit: |
| 174 | return hr; |
| 175 | } |
| 176 | |
| 177 |