bool WinFirewall::IsApplicationFirewalled(const ACE_TString& exefile) { if(!m_fw) return false; BOOL bEnabled = FALSE; HRESULT hr = WindowsFirewallAppIsEnabled(m_fw, exefile.c_str(), &bEnabled); return bEnabled; }
HRESULT WindowsFirewallAddApp( IN INetFwProfile* fwProfile, IN const wchar_t* fwProcessImageFileName, IN const wchar_t* fwName ) { HRESULT hr = S_OK; BOOL fwAppEnabled; BSTR fwBstrName = NULL; BSTR fwBstrProcessImageFileName = NULL; INetFwAuthorizedApplication* fwApp = NULL; INetFwAuthorizedApplications* fwApps = NULL; _ASSERT(fwProfile != NULL); _ASSERT(fwProcessImageFileName != NULL); _ASSERT(fwName != NULL); // First check to see if the application is already authorized. hr = WindowsFirewallAppIsEnabled( fwProfile, fwProcessImageFileName, &fwAppEnabled ); if (FAILED(hr)) { goto error; } // Only add the application if it isn't already authorized. if (!fwAppEnabled) { // Retrieve the authorized application collection. hr = fwProfile->get_AuthorizedApplications(&fwApps); if (FAILED(hr)) { goto error; } // Create an instance of an authorized application. hr = CoCreateInstance( __uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), (void**)&fwApp ); if (FAILED(hr)) { goto error; } // Allocate a BSTR for the process image file name. fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName); if (fwBstrProcessImageFileName == NULL) { hr = E_OUTOFMEMORY; goto error; } // Set the process image file name. hr = fwApp->put_ProcessImageFileName(fwBstrProcessImageFileName); if (FAILED(hr)) { goto error; } // Allocate a BSTR for the application friendly name. fwBstrName = SysAllocString(fwName); if (SysStringLen(fwBstrName) == 0) { hr = E_OUTOFMEMORY; goto error; } // Set the application friendly name. hr = fwApp->put_Name(fwBstrName); if (FAILED(hr)) { goto error; } // Add the application to the collection. hr = fwApps->Add(fwApp); if (FAILED(hr)) { goto error; } } error: // Free the BSTRs. SysFreeString(fwBstrName); SysFreeString(fwBstrProcessImageFileName); // Release the authorized application instance. if (fwApp != NULL) { fwApp->Release(); } // Release the authorized application collection. if (fwApps != NULL) { fwApps->Release(); } return hr; }
HRESULT WindowsFirewallRemoveApp( IN INetFwProfile* fwProfile, IN const wchar_t* fwProcessImageFileName ) { HRESULT hr = S_OK; BOOL fwAppEnabled; BSTR fwBstrName = NULL; BSTR fwBstrProcessImageFileName = NULL; INetFwAuthorizedApplication* fwApp = NULL; INetFwAuthorizedApplications* fwApps = NULL; assert(fwProfile != NULL); assert(fwProcessImageFileName != NULL); // First check to see if the application is already authorized. hr = WindowsFirewallAppIsEnabled( fwProfile, fwProcessImageFileName, &fwAppEnabled ); if (FAILED(hr)) { MYTRACE(ACE_TEXT("WindowsFirewallAppIsEnabled failed: 0x%08lx\n"), hr); goto error; } // Only remove the application if it is already authorized. if (fwAppEnabled) { // Retrieve the authorized application collection. hr = fwProfile->get_AuthorizedApplications(&fwApps); if (FAILED(hr)) { MYTRACE(ACE_TEXT("get_AuthorizedApplications failed: 0x%08lx\n"), hr); goto error; } // Create an instance of an authorized application. hr = CoCreateInstance( __uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), (void**)&fwApp ); if (FAILED(hr)) { MYTRACE(ACE_TEXT("CoCreateInstance failed: 0x%08lx\n"), hr); goto error; } // Allocate a BSTR for the process image file name. fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName); if (fwBstrProcessImageFileName == NULL) { hr = E_OUTOFMEMORY; MYTRACE(ACE_TEXT("SysAllocString failed: 0x%08lx\n"), hr); goto error; } // Add the application to the collection. hr = fwApps->Remove(fwBstrProcessImageFileName); if (FAILED(hr)) { MYTRACE(ACE_TEXT("Add failed: 0x%08lx\n"), hr); goto error; } MYTRACE(ACE_TEXT( "Authorized application %lS is now removed in the firewall.\n"), fwProcessImageFileName ); } error: // Free the BSTRs. SysFreeString(fwBstrName); SysFreeString(fwBstrProcessImageFileName); // Release the authorized application instance. if (fwApp != NULL) { fwApp->Release(); } // Release the authorized application collection. if (fwApps != NULL) { fwApps->Release(); } return hr; }
HRESULT WindowsFirewallAddApp( IN INetFwProfile* fwProfile, IN const wchar_t* fwProcessImageFileName, IN const wchar_t* fwName ) { HRESULT hr = S_OK; BOOL fwAppEnabled; BSTR fwBstrName = NULL; BSTR fwBstrProcessImageFileName = NULL; INetFwAuthorizedApplication* fwApp = NULL; INetFwAuthorizedApplications* fwApps = NULL; assert(fwProfile != NULL); assert(fwProcessImageFileName != NULL); assert(fwName != NULL); // First check to see if the application is already authorized. hr = WindowsFirewallAppIsEnabled( fwProfile, fwProcessImageFileName, &fwAppEnabled ); if (FAILED(hr)) { MYTRACE(ACE_TEXT("WindowsFirewallAppIsEnabled failed: 0x%08lx\n"), hr); goto error; } // Only add the application if it isn't already authorized. if (!fwAppEnabled) { // Retrieve the authorized application collection. hr = fwProfile->get_AuthorizedApplications(&fwApps); if (FAILED(hr)) { MYTRACE(ACE_TEXT("get_AuthorizedApplications failed: 0x%08lx\n"), hr); goto error; } // Create an instance of an authorized application. hr = CoCreateInstance( __uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), (void**)&fwApp ); if (FAILED(hr)) { MYTRACE(ACE_TEXT("CoCreateInstance failed: 0x%08lx\n"), hr); goto error; } // Allocate a BSTR for the process image file name. fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName); if (fwBstrProcessImageFileName == NULL) { hr = E_OUTOFMEMORY; MYTRACE(ACE_TEXT("SysAllocString failed: 0x%08lx\n"), hr); goto error; } // Set the process image file name. hr = fwApp->put_ProcessImageFileName(fwBstrProcessImageFileName); if (FAILED(hr)) { MYTRACE(ACE_TEXT("put_ProcessImageFileName failed: 0x%08lx\n"), hr); goto error; } // Allocate a BSTR for the application friendly name. fwBstrName = SysAllocString(fwName); if (SysStringLen(fwBstrName) == 0) { hr = E_OUTOFMEMORY; MYTRACE(ACE_TEXT("SysAllocString failed: 0x%08lx\n"), hr); goto error; } // Set the application friendly name. hr = fwApp->put_Name(fwBstrName); if (FAILED(hr)) { MYTRACE(ACE_TEXT("put_Name failed: 0x%08lx\n"), hr); goto error; } // Set scope (added by bdr -- begin) //hr = fwApp->put_Scope(NET_FW_SCOPE_ALL); //if (FAILED(hr)) //{ // MYTRACE(ACE_TEXT("put_Scope failed: 0x%08lx\n"), hr); // goto error; //} //hr = fwApp->put_RemoteAddresses(L"*"); //if (FAILED(hr)) //{ // MYTRACE(ACE_TEXT("put_Scope failed: 0x%08lx\n"), hr); // goto error; //} // Set scope (added by bdr -- end) // Add the application to the collection. hr = fwApps->Add(fwApp); if (FAILED(hr)) { MYTRACE(ACE_TEXT("Add failed: 0x%08lx\n"), hr); goto error; } MYTRACE(ACE_TEXT( "Authorized application %lS is now enabled in the firewall.\n"), fwProcessImageFileName ); } error: // Free the BSTRs. SysFreeString(fwBstrName); SysFreeString(fwBstrProcessImageFileName); // Release the authorized application instance. if (fwApp != NULL) { fwApp->Release(); } // Release the authorized application collection. if (fwApps != NULL) { fwApps->Release(); } return hr; }