You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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.
  26. */
  27. #include "talk/base/winfirewall.h"
  28. #include "talk/base/win32.h"
  29. #include <comdef.h>
  30. #include <netfw.h>
  31. #define RELEASE(lpUnk) do { \
  32. if ((lpUnk) != NULL) { \
  33. (lpUnk)->Release(); \
  34. (lpUnk) = NULL; \
  35. } \
  36. } while (0)
  37. namespace talk_base {
  38. //////////////////////////////////////////////////////////////////////
  39. // WinFirewall
  40. //////////////////////////////////////////////////////////////////////
  41. WinFirewall::WinFirewall() : mgr_(NULL), policy_(NULL), profile_(NULL) {
  42. }
  43. WinFirewall::~WinFirewall() {
  44. Shutdown();
  45. }
  46. bool WinFirewall::Initialize(HRESULT* result) {
  47. if (mgr_) {
  48. if (result) {
  49. *result = S_OK;
  50. }
  51. return true;
  52. }
  53. HRESULT hr = CoCreateInstance(__uuidof(NetFwMgr),
  54. 0, CLSCTX_INPROC_SERVER,
  55. __uuidof(INetFwMgr),
  56. reinterpret_cast<void **>(&mgr_));
  57. if (SUCCEEDED(hr) && (mgr_ != NULL))
  58. hr = mgr_->get_LocalPolicy(&policy_);
  59. if (SUCCEEDED(hr) && (policy_ != NULL))
  60. hr = policy_->get_CurrentProfile(&profile_);
  61. if (result)
  62. *result = hr;
  63. return SUCCEEDED(hr) && (profile_ != NULL);
  64. }
  65. void WinFirewall::Shutdown() {
  66. RELEASE(profile_);
  67. RELEASE(policy_);
  68. RELEASE(mgr_);
  69. }
  70. bool WinFirewall::Enabled() const {
  71. if (!profile_)
  72. return false;
  73. VARIANT_BOOL fwEnabled = VARIANT_FALSE;
  74. profile_->get_FirewallEnabled(&fwEnabled);
  75. return (fwEnabled != VARIANT_FALSE);
  76. }
  77. bool WinFirewall::QueryAuthorized(const char* filename, bool* authorized)
  78. const {
  79. return QueryAuthorizedW(ToUtf16(filename).c_str(), authorized);
  80. }
  81. bool WinFirewall::QueryAuthorizedW(const wchar_t* filename, bool* authorized)
  82. const {
  83. *authorized = false;
  84. bool success = false;
  85. if (!profile_)
  86. return false;
  87. _bstr_t bfilename = filename;
  88. INetFwAuthorizedApplications* apps = NULL;
  89. HRESULT hr = profile_->get_AuthorizedApplications(&apps);
  90. if (SUCCEEDED(hr) && (apps != NULL)) {
  91. INetFwAuthorizedApplication* app = NULL;
  92. hr = apps->Item(bfilename, &app);
  93. if (SUCCEEDED(hr) && (app != NULL)) {
  94. VARIANT_BOOL fwEnabled = VARIANT_FALSE;
  95. hr = app->get_Enabled(&fwEnabled);
  96. app->Release();
  97. if (SUCCEEDED(hr)) {
  98. success = true;
  99. *authorized = (fwEnabled != VARIANT_FALSE);
  100. }
  101. } else if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
  102. // No entry in list of authorized apps
  103. success = true;
  104. } else {
  105. // Unexpected error
  106. }
  107. apps->Release();
  108. }
  109. return success;
  110. }
  111. bool WinFirewall::AddApplication(const char* filename,
  112. const char* friendly_name,
  113. bool authorized,
  114. HRESULT* result) {
  115. return AddApplicationW(ToUtf16(filename).c_str(),
  116. ToUtf16(friendly_name).c_str(), authorized, result);
  117. }
  118. bool WinFirewall::AddApplicationW(const wchar_t* filename,
  119. const wchar_t* friendly_name,
  120. bool authorized,
  121. HRESULT* result) {
  122. INetFwAuthorizedApplications* apps = NULL;
  123. HRESULT hr = profile_->get_AuthorizedApplications(&apps);
  124. if (SUCCEEDED(hr) && (apps != NULL)) {
  125. INetFwAuthorizedApplication* app = NULL;
  126. hr = CoCreateInstance(__uuidof(NetFwAuthorizedApplication),
  127. 0, CLSCTX_INPROC_SERVER,
  128. __uuidof(INetFwAuthorizedApplication),
  129. reinterpret_cast<void **>(&app));
  130. if (SUCCEEDED(hr) && (app != NULL)) {
  131. _bstr_t bstr = filename;
  132. hr = app->put_ProcessImageFileName(bstr);
  133. bstr = friendly_name;
  134. if (SUCCEEDED(hr))
  135. hr = app->put_Name(bstr);
  136. if (SUCCEEDED(hr))
  137. hr = app->put_Enabled(authorized ? VARIANT_TRUE : VARIANT_FALSE);
  138. if (SUCCEEDED(hr))
  139. hr = apps->Add(app);
  140. app->Release();
  141. }
  142. apps->Release();
  143. }
  144. if (result)
  145. *result = hr;
  146. return SUCCEEDED(hr);
  147. }
  148. } // namespace talk_base