3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/winfirewall.h"
30 #include "talk/base/win32.h"
35 #define RELEASE(lpUnk) do { \
36 if ((lpUnk) != NULL) { \
44 //////////////////////////////////////////////////////////////////////
46 //////////////////////////////////////////////////////////////////////
48 WinFirewall::WinFirewall() : mgr_(NULL), policy_(NULL), profile_(NULL) {
51 WinFirewall::~WinFirewall() {
55 bool WinFirewall::Initialize(HRESULT* result) {
63 HRESULT hr = CoCreateInstance(__uuidof(NetFwMgr),
64 0, CLSCTX_INPROC_SERVER,
66 reinterpret_cast<void **>(&mgr_));
67 if (SUCCEEDED(hr) && (mgr_ != NULL))
68 hr = mgr_->get_LocalPolicy(&policy_);
69 if (SUCCEEDED(hr) && (policy_ != NULL))
70 hr = policy_->get_CurrentProfile(&profile_);
74 return SUCCEEDED(hr) && (profile_ != NULL);
77 void WinFirewall::Shutdown() {
83 bool WinFirewall::Enabled() const {
87 VARIANT_BOOL fwEnabled = VARIANT_FALSE;
88 profile_->get_FirewallEnabled(&fwEnabled);
89 return (fwEnabled != VARIANT_FALSE);
92 bool WinFirewall::QueryAuthorized(const char* filename, bool* authorized)
94 return QueryAuthorizedW(ToUtf16(filename).c_str(), authorized);
97 bool WinFirewall::QueryAuthorizedW(const wchar_t* filename, bool* authorized)
100 bool success = false;
105 _bstr_t bfilename = filename;
107 INetFwAuthorizedApplications* apps = NULL;
108 HRESULT hr = profile_->get_AuthorizedApplications(&apps);
109 if (SUCCEEDED(hr) && (apps != NULL)) {
110 INetFwAuthorizedApplication* app = NULL;
111 hr = apps->Item(bfilename, &app);
112 if (SUCCEEDED(hr) && (app != NULL)) {
113 VARIANT_BOOL fwEnabled = VARIANT_FALSE;
114 hr = app->get_Enabled(&fwEnabled);
119 *authorized = (fwEnabled != VARIANT_FALSE);
121 } else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
122 // No entry in list of authorized apps
133 bool WinFirewall::AddApplication(const char* filename,
134 const char* friendly_name,
137 return AddApplicationW(ToUtf16(filename).c_str(),
138 ToUtf16(friendly_name).c_str(), authorized, result);
141 bool WinFirewall::AddApplicationW(const wchar_t* filename,
142 const wchar_t* friendly_name,
145 INetFwAuthorizedApplications* apps = NULL;
146 HRESULT hr = profile_->get_AuthorizedApplications(&apps);
147 if (SUCCEEDED(hr) && (apps != NULL)) {
148 INetFwAuthorizedApplication* app = NULL;
149 hr = CoCreateInstance(__uuidof(NetFwAuthorizedApplication),
150 0, CLSCTX_INPROC_SERVER,
151 __uuidof(INetFwAuthorizedApplication),
152 reinterpret_cast<void **>(&app));
153 if (SUCCEEDED(hr) && (app != NULL)) {
154 _bstr_t bstr = filename;
155 hr = app->put_ProcessImageFileName(bstr);
156 bstr = friendly_name;
158 hr = app->put_Name(bstr);
160 hr = app->put_Enabled(authorized ? VARIANT_TRUE : VARIANT_FALSE);
169 return SUCCEEDED(hr);
172 } // namespace talk_base