| OLD | NEW |
| (Empty) |
| 1 // Copyright 2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include "omaha/plugins/update/site_lock.h" | |
| 17 #include <wininet.h> | |
| 18 #include <shlobj.h> | |
| 19 #include "base/scoped_ptr.h" | |
| 20 #include "omaha/base/atl_regexp.h" | |
| 21 #include "omaha/base/constants.h" | |
| 22 #include "omaha/base/error.h" | |
| 23 #include "omaha/base/safe_format.h" | |
| 24 #include "omaha/base/reg_key.h" | |
| 25 #include "omaha/plugins/update/npapi/urlpropbag.h" | |
| 26 | |
| 27 namespace omaha { | |
| 28 | |
| 29 SiteLock::SiteLock() { | |
| 30 for (int i = 0; i < arraysize(kSiteLockPatternStrings); ++i) { | |
| 31 VERIFY1(AddPattern(kSiteLockPatternStrings[i])); | |
| 32 } | |
| 33 | |
| 34 // TODO(omaha): should this be wrapped in a #ifdef DEBUG? | |
| 35 CString dev_pattern_string; | |
| 36 if (SUCCEEDED(RegKey::GetValue(MACHINE_REG_UPDATE_DEV, | |
| 37 kRegValueOneClickHostPattern, | |
| 38 &dev_pattern_string)) && | |
| 39 !dev_pattern_string.IsEmpty()) { | |
| 40 VERIFY1(AddPattern(dev_pattern_string)); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 SiteLock::~SiteLock() { | |
| 45 for (size_t i = 0; i < patterns_.size(); ++i) { | |
| 46 delete patterns_[i]; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 bool SiteLock::InApprovedDomain(IObjectWithSite* plugin) { | |
| 51 CString url; | |
| 52 if (FAILED(GetCurrentBrowserUrl(plugin, &url))) { | |
| 53 return false; | |
| 54 } | |
| 55 return InApprovedDomain(url); | |
| 56 } | |
| 57 | |
| 58 bool SiteLock::InApprovedDomain(const WCHAR* url) { | |
| 59 // TODO(omaha): investigate using CUrl to remove dependency on wininet | |
| 60 URL_COMPONENTS components = {sizeof(components)}; | |
| 61 components.dwHostNameLength = 1; | |
| 62 if (!::InternetCrackUrl(url, 0, 0, &components)) { | |
| 63 return false; | |
| 64 } | |
| 65 CString hostname(components.lpszHostName, components.dwHostNameLength); | |
| 66 for (std::vector<AtlRegExp*>::const_iterator it = patterns_.begin(); | |
| 67 it != patterns_.end(); | |
| 68 ++it) { | |
| 69 AtlMatchContext context; | |
| 70 if ((*it)->Match(hostname, &context)) { | |
| 71 return true; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 HRESULT SiteLock::GetCurrentBrowserUrl(IObjectWithSite* plugin, CString* url) { | |
| 79 if (SUCCEEDED(ExtractUrlFromBrowser(plugin, url))) { | |
| 80 return S_OK; | |
| 81 } | |
| 82 if (SUCCEEDED(ExtractUrlFromPropBag(plugin, url))) { | |
| 83 return S_OK; | |
| 84 } | |
| 85 return E_FAIL; | |
| 86 } | |
| 87 | |
| 88 // TODO(omaha): Move this to common\webplugin_utils. | |
| 89 HRESULT SiteLock::GetUrlDomain(const CString& url, CString* url_domain) { | |
| 90 ASSERT1(url_domain); | |
| 91 url_domain->Empty(); | |
| 92 | |
| 93 URL_COMPONENTS urlComponents = {0}; | |
| 94 urlComponents.dwStructSize = sizeof(urlComponents); | |
| 95 urlComponents.dwSchemeLength = 1; | |
| 96 urlComponents.dwHostNameLength = 1; | |
| 97 if (!::InternetCrackUrl(url, 0, 0, &urlComponents)) { | |
| 98 HRESULT hr = HRESULTFromLastError(); | |
| 99 CORE_LOG(L2, (_T("[InternetCrackUrl failed][0x%08x]"), hr)); | |
| 100 return hr; | |
| 101 } | |
| 102 | |
| 103 CString scheme(urlComponents.lpszScheme, urlComponents.dwSchemeLength); | |
| 104 CString host_name(urlComponents.lpszHostName, urlComponents.dwHostNameLength); | |
| 105 ASSERT1(!scheme.IsEmpty()); | |
| 106 ASSERT1(!host_name.IsEmpty()); | |
| 107 | |
| 108 SafeCStringFormat(url_domain, _T("%s://%s/"), scheme, host_name); | |
| 109 return S_OK; | |
| 110 } | |
| 111 | |
| 112 bool SiteLock::AddPattern(const WCHAR* pattern) { | |
| 113 ASSERT1(pattern); | |
| 114 | |
| 115 // An empty pattern will match everything... | |
| 116 if (!*pattern) { | |
| 117 ASSERT1(false); | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 scoped_ptr<AtlRegExp> re(new AtlRegExp); | |
| 122 REParseError error = re->Parse(pattern); | |
| 123 if (REPARSE_ERROR_OK != error) { | |
| 124 ASSERT(false, (L"Failed to parse site lock pattern: %s", | |
| 125 pattern)); | |
| 126 return false; | |
| 127 } | |
| 128 patterns_.push_back(re.release()); | |
| 129 return true; | |
| 130 } | |
| 131 | |
| 132 // If the plugin is being hosted inside an NPAPI environment, NPUpdate will set | |
| 133 // a UrlPropertyBag object as our object site. Fetch the URL used to create | |
| 134 // our object from it. | |
| 135 HRESULT SiteLock::ExtractUrlFromPropBag(IObjectWithSite* plugin, CString* url) { | |
| 136 ASSERT1(plugin); | |
| 137 ASSERT1(url); | |
| 138 | |
| 139 CComPtr<IPropertyBag> property_bag; | |
| 140 HRESULT hr = plugin->GetSite(IID_PPV_ARGS(&property_bag)); | |
| 141 if (FAILED(hr)) { | |
| 142 return hr; | |
| 143 } | |
| 144 | |
| 145 CComVariant var; | |
| 146 hr = property_bag->Read(kUrlPropertyBag_Url, &var, NULL); | |
| 147 if (FAILED(hr)) { | |
| 148 return hr; | |
| 149 } | |
| 150 if (var.vt != VT_BSTR || !var.bstrVal) { | |
| 151 return E_UNEXPECTED; | |
| 152 } | |
| 153 *url = var.bstrVal; | |
| 154 return S_OK; | |
| 155 } | |
| 156 | |
| 157 // If the plugin is hosted in an ActiveX environment, IE will set itself as the | |
| 158 // object site. Fetch the current URL from it. | |
| 159 HRESULT SiteLock::ExtractUrlFromBrowser(IObjectWithSite* plugin, CString* url) { | |
| 160 ASSERT1(plugin); | |
| 161 ASSERT1(url); | |
| 162 | |
| 163 CComPtr<IServiceProvider> service_provider; | |
| 164 HRESULT hr = plugin->GetSite(IID_PPV_ARGS(&service_provider)); | |
| 165 if (FAILED(hr)) { | |
| 166 return hr; | |
| 167 } | |
| 168 | |
| 169 CComPtr<IWebBrowser2> web_browser; | |
| 170 hr = service_provider->QueryService(SID_SWebBrowserApp, | |
| 171 IID_PPV_ARGS(&web_browser)); | |
| 172 | |
| 173 CComBSTR bstr_url; | |
| 174 if (SUCCEEDED(hr)) { | |
| 175 hr = web_browser->get_LocationURL(&bstr_url); | |
| 176 } else { | |
| 177 // Do things the hard way... | |
| 178 CComPtr<IOleClientSite> client_site; | |
| 179 hr = plugin->GetSite(IID_PPV_ARGS(&client_site)); | |
| 180 if (FAILED(hr)) { | |
| 181 return hr; | |
| 182 } | |
| 183 | |
| 184 CComPtr<IOleContainer> container; | |
| 185 hr = client_site->GetContainer(&container); | |
| 186 if (FAILED(hr)) { | |
| 187 return hr; | |
| 188 } | |
| 189 | |
| 190 CComPtr<IHTMLDocument2> html_document; | |
| 191 hr = container.QueryInterface(&html_document); | |
| 192 if (FAILED(hr)) { | |
| 193 return hr; | |
| 194 } | |
| 195 | |
| 196 hr = html_document->get_URL(&bstr_url); | |
| 197 } | |
| 198 | |
| 199 if (SUCCEEDED(hr)) { | |
| 200 *url = bstr_url; | |
| 201 } | |
| 202 | |
| 203 return hr; | |
| 204 } | |
| 205 | |
| 206 } // namespace omaha | |
| OLD | NEW |