/*++ Copyright (c) 1998-2002 Microsoft Corporation. All rights reserved. --*/ #include "stdafx.h" #include "WebExeBlockFilterImpl.h" #include "WebExeBlockFilter.h" #include "Outputdebugstringf.h" #include "msfpccom_i.c" const WCHAR* x_wcsWebExeBlockFilterGuid = L"{79DD71D7-2834-48e1-9DC3-A784ADF9EBA7}"; const WCHAR* x_wcsWebExeBlockFilterName = L"Web Exe Block Filter"; const WCHAR* x_wcsWebExeBlockFilterRelativePath = L"WebExeBlockFilter.dll"; const WCHAR* x_wcsWebExeBlockFilterVersion = L"4.0"; const WCHAR* x_wcsWebExeBlockFilterDescription = L"A filter which blocks windows executables"; const WCHAR* x_wcsWebExeBlockFilterVendor = L"Microsoft (R) Corporation"; ///////////////////////////////////////////////////////////////////////////// // CWebExeBlockFilter() ///////////////////////////////////////////////////////////////////////////// CWebExeBlockFilter::CWebExeBlockFilter(): m_fCalledInit(FALSE) { } ///////////////////////////////////////////////////////////////////////////// // ~CWebExeBlockFilter() ///////////////////////////////////////////////////////////////////////////// CWebExeBlockFilter::~CWebExeBlockFilter() { } ///////////////////////////////////////////////////////////////////////////// // Init() // Called when filter is loaded by the proxy ///////////////////////////////////////////////////////////////////////////// DWORD CWebExeBlockFilter::Init() { if (m_fCalledInit) { return ERROR_SUCCESS; } m_fCalledInit = TRUE; return m_FilterImpl.Init(); } ///////////////////////////////////////////////////////////////////////////// // Reload() // Called when configuration was changed and filter need to reload it own configuration ///////////////////////////////////////////////////////////////////////////// DWORD CWebExeBlockFilter::Reload() { return m_FilterImpl.Reload(); } ///////////////////////////////////////////////////////////////////////////// // Shutdown() // Called when filter is unloaded by the proxy ///////////////////////////////////////////////////////////////////////////// DWORD CWebExeBlockFilter::Shutdown() { return m_FilterImpl.Shutdown(); } ///////////////////////////////////////////////////////////////////////////// // Register() // Register our web filter and the new authentication scheme. Called when // filter is registered. ///////////////////////////////////////////////////////////////////////////// HRESULT CWebExeBlockFilter::Register() { HRESULT hr = S_OK; CComPtr spWebFilters; hr = CoInitialize(NULL); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: CoInitialize Failed. Error code is %lx\n", hr); return hr; } // // Get web filters collection // hr = GetWebFilters(&spWebFilters); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Get the web filters object failed. Error code is %lx\n", hr); CoUninitialize(); return hr; } // // Add this web filter to the collection of web filters // hr = AddWebFilter(spWebFilters); if (FAILED(hr) ) { OutputDebugStringF("WEBEXEBLOCK: Failed to register the web filter. Error code is %lx\n", hr); CoUninitialize(); return hr; } CoUninitialize(); return hr; } ///////////////////////////////////////////////////////////////////////////// // Unregister() // Unregister our web filter and the new authentication scheme. Called when // filter is unregistered. ///////////////////////////////////////////////////////////////////////////// HRESULT CWebExeBlockFilter::Unregister() { HRESULT hr = S_OK; CComPtr spWebFilters; hr = CoInitialize(NULL); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: CoInitialize Failed. Error code is %lx\n", hr); return hr; } // // Get web filters collection // hr = GetWebFilters(&spWebFilters); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Get the web filters object failed. Error code is %lx\n", hr); CoUninitialize(); return hr; } // // Unregister our web filter // hr = RemoveWebFilter(spWebFilters); if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) { hr = S_OK; } if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Remove web filter failed. Error code is %lx\n", hr); CoUninitialize(); return hr; } CoUninitialize(); return hr; } ///////////////////////////////////////////////////////////////////////////// // RegisterISAPINotifications() // Define which ISAPI notification the filter is interested in. ///////////////////////////////////////////////////////////////////////////// BOOL CWebExeBlockFilter::RegisterISAPINotifications(PHTTP_FILTER_VERSION pVer) { pVer->dwFilterVersion = WPX_HTTP_FILTER_REVISION; pVer->dwFlags = SF_NOTIFY_END_OF_REQUEST; return TRUE; } ///////////////////////////////////////////////////////////////////////////// // RegisterWpxNotifications() // Define which WPX specific notification the filter is interested in. ///////////////////////////////////////////////////////////////////////////// BOOL CWebExeBlockFilter::RegisterWpxNotifications(PHTTP_FILTER_VERSION pVer) { pVer->dwFilterVersion = WPX_HTTP_FILTER_REVISION; pVer->dwFlags = SF_NOTIFY_RECEIVE_RESPONSE_RAW_DATA; return TRUE; } ///////////////////////////////////////////////////////////////////////////// // HttpFilterProc() // Handle ISAPI notifications. ///////////////////////////////////////////////////////////////////////////// DWORD CWebExeBlockFilter::HttpFilterProc ( PHTTP_FILTER_CONTEXT pfc, DWORD notificationType, LPVOID pvNotification) { assert (pfc); SetLastError(ERROR_SUCCESS); DWORD dwRes = 0; switch (notificationType) { case SF_NOTIFY_END_OF_REQUEST: { dwRes = m_FilterImpl.OnEndOfRequest(pfc); break; } default: { assert(FALSE); break; } } return dwRes; } ///////////////////////////////////////////////////////////////////////////// // HttpWpxFilterProc() // Handle WPX specific notifications. ///////////////////////////////////////////////////////////////////////////// DWORD CWebExeBlockFilter::HttpWpxFilterProc ( PHTTP_FILTER_CONTEXT pfc, DWORD notificationType, LPVOID pvNotification) { assert (pfc); SetLastError(ERROR_SUCCESS); DWORD dwRes = 0; switch (notificationType) { case SF_NOTIFY_RECEIVE_RESPONSE_RAW_DATA: { PHTTP_FILTER_RAW_DATA pRawData = (PHTTP_FILTER_RAW_DATA)pvNotification; dwRes = m_FilterImpl.OnReceiveResponseRawData(pfc, pRawData); break; } default: { assert(FALSE); break; } } return dwRes; } //////////////////////////////////////////////////////////////////////////// // GetWebFilters() // Helper function which retrieves the WebFilters container from ISA storage // using ISA COM interface. //////////////////////////////////////////////////////////////////////////// HRESULT CWebExeBlockFilter::GetWebFilters(IFPCWebFilters** ppWebFilters) { assert(ppWebFilters); HRESULT hr = S_OK; CComPtr spFpc; CComPtr spCurrentArray; CComPtr spExtentions; hr = CoCreateInstance ( CLSID_FPC, NULL, CLSCTX_SERVER, IID_IFPC, (VOID**)&spFpc); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed retrieving interface. Error code is %lx\n", hr); return hr; } hr = spFpc->GetContainingArray(&spCurrentArray); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to query for current array. Error code is %lx\n", hr); return hr; } hr = spCurrentArray->get_Extensions(&spExtentions); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to query for array extentions. Error code is %lx\n", hr); return hr; } hr = spExtentions->get_WebFilters(ppWebFilters); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to query for Web filters. Error code is %lx\n", hr); return hr; } return hr; } ////////////////////////////////////////////////////////////////////////////////// // AddWebFilter(): // Helper function which adds our filter to the ISA WebFilters container using ISA // COM interface. ////////////////////////////////////////////////////////////////////////////////// HRESULT CWebExeBlockFilter::AddWebFilter(IFPCWebFilters* pWebFilters) { HRESULT hr = S_OK; CComBSTR bstrTemp; CComBSTR bstrGuid; CComBSTR bstrName; CComBSTR bstrPath; CComPtr spFilter; bstrGuid = x_wcsWebExeBlockFilterGuid; if (!bstrGuid) { return E_OUTOFMEMORY; } bstrName = x_wcsWebExeBlockFilterName; if (!bstrName) { return E_OUTOFMEMORY; } bstrPath = x_wcsWebExeBlockFilterRelativePath; if (!bstrPath) { return E_OUTOFMEMORY; } hr = pWebFilters->Add( bstrGuid, bstrName, bstrPath, fpcFilterPriority_High, fpcFilterDirectionBoth, &spFilter); if (FAILED(hr)) { if (hr == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS)) // Filter already registered { return S_OK; } OutputDebugStringF("WEBEXEBLOCK: Failed to add the Web filter. Error code is %lx\n", hr); return hr; } bstrTemp = x_wcsWebExeBlockFilterDescription; if (!bstrTemp) { return E_OUTOFMEMORY; } hr = spFilter->put_Description(bstrTemp); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to set the filter's description. Error code is %lx\n", hr); return hr; } bstrTemp = x_wcsWebExeBlockFilterVendor; if (!bstrTemp) { return E_OUTOFMEMORY; } hr = spFilter->put_Vendor(bstrTemp); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to set the filter's vendor. Error code is %lx\n", hr); return hr; } bstrTemp = x_wcsWebExeBlockFilterVersion; if (!bstrTemp) { return E_OUTOFMEMORY; } hr = spFilter->put_Version(bstrTemp); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to set the filter's version. Error code is %lx\n", hr); return hr; } hr = spFilter->put_Enabled(VARIANT_TRUE); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to Enable the filter. Error code is %lx\n", hr); return hr; } hr = pWebFilters->Save(); if (FAILED(hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to save the filter. Error code is %lx\n", hr); return hr; } return hr; } /////////////////////////////////////////////////////////////////////////////////////// // RemoveWebFilter(): // Helper function which removes our filter to the ISA WebFilters container using ISA // COM interface. ////////////////////////////////////////////////////////////////////////////////////// HRESULT CWebExeBlockFilter::RemoveWebFilter(IFPCWebFilters* pWebFilters) { HRESULT hr = S_OK; CComVariant vtFilterGuid(x_wcsWebExeBlockFilterGuid); if (vtFilterGuid.vt != VT_BSTR || !vtFilterGuid.bstrVal) { return E_OUTOFMEMORY; } hr = pWebFilters->Remove(vtFilterGuid); if (FAILED (hr)) { if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) // Filter not registered, was already removed { return S_OK; } OutputDebugStringF("WEBEXEBLOCK: Failed to remove the Web filter. Error code is %lx\n", hr); return hr; } hr = pWebFilters->Save(); if (FAILED (hr)) { OutputDebugStringF("WEBEXEBLOCK: Failed to save. Error code is %lx\n", hr); return hr; } return hr; }