lukasza | c285c9a | 2015-01-29 23:18:28 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Most of this code is copied from: |
| 6 | // src/chrome/browser/policy/asynchronous_policy_loader.{h,cc} |
| 7 | |
lukasza | c285c9a | 2015-01-29 23:18:28 | [diff] [blame] | 8 | #include "remoting/host/policy_watcher.h" |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 9 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 10 | #include <utility> |
| 11 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 12 | #include "base/bind.h" |
| 13 | #include "base/compiler_specific.h" |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 15 | #include "base/location.h" |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 16 | #include "base/memory/ptr_util.h" |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 17 | #include "base/single_thread_task_runner.h" |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 18 | #include "base/values.h" |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 19 | #include "build/build_config.h" |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 20 | #include "components/policy/core/common/async_policy_loader.h" |
| 21 | #include "components/policy/core/common/async_policy_provider.h" |
| 22 | #include "components/policy/core/common/policy_namespace.h" |
| 23 | #include "components/policy/core/common/policy_service_impl.h" |
| 24 | #include "components/policy/core/common/schema.h" |
| 25 | #include "components/policy/core/common/schema_registry.h" |
Lukasz Anforowicz | 2b91a480 | 2015-01-16 22:39:46 | [diff] [blame] | 26 | #include "policy/policy_constants.h" |
[email protected] | 11bb05a | 2012-09-01 03:05:21 | [diff] [blame] | 27 | #include "remoting/host/dns_blackhole_checker.h" |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 28 | #include "remoting/host/third_party_auth_config.h" |
| 29 | #include "remoting/protocol/port_range.h" |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 30 | |
[email protected] | 158b287 | 2012-11-13 10:08:38 | [diff] [blame] | 31 | #if !defined(NDEBUG) |
| 32 | #include "base/json/json_reader.h" |
| 33 | #endif |
| 34 | |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 35 | #if defined(OS_WIN) |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 36 | #include "components/policy/core/common/policy_loader_win.h" |
| 37 | #elif defined(OS_MACOSX) |
| 38 | #include "components/policy/core/common/policy_loader_mac.h" |
| 39 | #include "components/policy/core/common/preferences_mac.h" |
| 40 | #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
| 41 | #include "components/policy/core/common/config_dir_policy_loader.h" |
| 42 | #endif |
| 43 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 44 | namespace remoting { |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 45 | |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 46 | namespace key = ::policy::key; |
| 47 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 48 | namespace { |
[email protected] | bf931a4 | 2012-11-10 19:07:02 | [diff] [blame] | 49 | |
[email protected] | bf931a4 | 2012-11-10 19:07:02 | [diff] [blame] | 50 | // Copies all policy values from one dictionary to another, using values from |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 51 | // |default_values| if they are not set in |from|. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 52 | std::unique_ptr<base::DictionaryValue> CopyValuesAndAddDefaults( |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 53 | const base::DictionaryValue& from, |
| 54 | const base::DictionaryValue& default_values) { |
dcheng | efaf1b11 | 2016-05-03 01:12:49 | [diff] [blame] | 55 | std::unique_ptr<base::DictionaryValue> to(default_values.CreateDeepCopy()); |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 56 | for (base::DictionaryValue::Iterator i(default_values); !i.IsAtEnd(); |
lukasza | c285c9a | 2015-01-29 23:18:28 | [diff] [blame] | 57 | i.Advance()) { |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 58 | const base::Value* value = nullptr; |
[email protected] | bf931a4 | 2012-11-10 19:07:02 | [diff] [blame] | 59 | |
| 60 | // If the policy isn't in |from|, use the default. |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 61 | if (!from.Get(i.key(), &value)) { |
[email protected] | bf931a4 | 2012-11-10 19:07:02 | [diff] [blame] | 62 | continue; |
| 63 | } |
| 64 | |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 65 | CHECK(value->IsType(i.value().GetType())); |
dcheng | efaf1b11 | 2016-05-03 01:12:49 | [diff] [blame] | 66 | to->Set(i.key(), value->CreateDeepCopy()); |
[email protected] | b9612a5 | 2012-07-28 02:17:48 | [diff] [blame] | 67 | } |
[email protected] | 50409b3 | 2012-07-31 18:54:02 | [diff] [blame] | 68 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 69 | return to; |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 70 | } |
| 71 | |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 72 | policy::PolicyNamespace GetPolicyNamespace() { |
| 73 | return policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, std::string()); |
| 74 | } |
| 75 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 76 | std::unique_ptr<policy::SchemaRegistry> CreateSchemaRegistry() { |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 77 | // TODO(lukasza): Schema below should ideally only cover Chromoting-specific |
| 78 | // policies (expecting perf and maintanability improvement, but no functional |
| 79 | // impact). |
| 80 | policy::Schema schema = policy::Schema::Wrap(policy::GetChromeSchemaData()); |
| 81 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 82 | std::unique_ptr<policy::SchemaRegistry> schema_registry( |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 83 | new policy::SchemaRegistry()); |
| 84 | schema_registry->RegisterComponent(GetPolicyNamespace(), schema); |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 85 | return schema_registry; |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 86 | } |
| 87 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 88 | std::unique_ptr<base::DictionaryValue> CopyChromotingPoliciesIntoDictionary( |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 89 | const policy::PolicyMap& current) { |
| 90 | const char kPolicyNameSubstring[] = "RemoteAccessHost"; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 91 | std::unique_ptr<base::DictionaryValue> policy_dict( |
| 92 | new base::DictionaryValue()); |
dcheng | 3b344bc2 | 2016-05-10 02:26:09 | [diff] [blame] | 93 | for (const auto& entry : current) { |
| 94 | const std::string& key = entry.first; |
| 95 | const base::Value* value = entry.second.value.get(); |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 96 | |
| 97 | // Copying only Chromoting-specific policies helps avoid false alarms |
| 98 | // raised by NormalizePolicies below (such alarms shutdown the host). |
| 99 | // TODO(lukasza): Removing this somewhat brittle filtering will be possible |
| 100 | // after having separate, Chromoting-specific schema. |
| 101 | if (key.find(kPolicyNameSubstring) != std::string::npos) { |
dcheng | efaf1b11 | 2016-05-03 01:12:49 | [diff] [blame] | 102 | policy_dict->Set(key, value->CreateDeepCopy()); |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 106 | return policy_dict; |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Takes a dictionary containing only 1) recognized policy names and 2) |
| 110 | // well-typed policy values and further verifies policy contents. |
| 111 | bool VerifyWellformedness(const base::DictionaryValue& changed_policies) { |
| 112 | // Verify ThirdPartyAuthConfig policy. |
| 113 | ThirdPartyAuthConfig not_used; |
| 114 | switch (ThirdPartyAuthConfig::Parse(changed_policies, ¬_used)) { |
| 115 | case ThirdPartyAuthConfig::NoPolicy: |
| 116 | case ThirdPartyAuthConfig::ParsingSuccess: |
| 117 | break; // Well-formed. |
| 118 | case ThirdPartyAuthConfig::InvalidPolicy: |
| 119 | return false; // Malformed. |
| 120 | default: |
| 121 | NOTREACHED(); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | // Verify UdpPortRange policy. |
| 126 | std::string udp_port_range_string; |
| 127 | PortRange udp_port_range; |
| 128 | if (changed_policies.GetString(policy::key::kRemoteAccessHostUdpPortRange, |
| 129 | &udp_port_range_string)) { |
| 130 | if (!PortRange::Parse(udp_port_range_string, &udp_port_range)) { |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Report that all the policies were well-formed. |
| 136 | return true; |
| 137 | } |
| 138 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 139 | } // namespace |
| 140 | |
lukasza | 0fdd951 | 2014-11-25 17:47:46 | [diff] [blame] | 141 | void PolicyWatcher::StartWatching( |
| 142 | const PolicyUpdatedCallback& policy_updated_callback, |
| 143 | const PolicyErrorCallback& policy_error_callback) { |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 144 | DCHECK(CalledOnValidThread()); |
| 145 | DCHECK(!policy_updated_callback.is_null()); |
| 146 | DCHECK(!policy_error_callback.is_null()); |
| 147 | DCHECK(policy_updated_callback_.is_null()); |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 148 | |
lukasza | 0fdd951 | 2014-11-25 17:47:46 | [diff] [blame] | 149 | policy_updated_callback_ = policy_updated_callback; |
| 150 | policy_error_callback_ = policy_error_callback; |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 151 | |
| 152 | // Listen for future policy changes. |
| 153 | policy_service_->AddObserver(policy::POLICY_DOMAIN_CHROME, this); |
| 154 | |
| 155 | // Process current policy state. |
| 156 | if (policy_service_->IsInitializationComplete(policy::POLICY_DOMAIN_CHROME)) { |
| 157 | OnPolicyServiceInitialized(policy::POLICY_DOMAIN_CHROME); |
| 158 | } |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 159 | } |
| 160 | |
lukasza | 0fdd951 | 2014-11-25 17:47:46 | [diff] [blame] | 161 | void PolicyWatcher::SignalPolicyError() { |
lukasza | fecb18b | 2015-02-24 01:04:12 | [diff] [blame] | 162 | old_policies_->Clear(); |
lukasza | 0fdd951 | 2014-11-25 17:47:46 | [diff] [blame] | 163 | policy_error_callback_.Run(); |
| 164 | } |
| 165 | |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 166 | PolicyWatcher::PolicyWatcher( |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 167 | policy::PolicyService* policy_service, |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 168 | std::unique_ptr<policy::PolicyService> owned_policy_service, |
| 169 | std::unique_ptr<policy::ConfigurationPolicyProvider> owned_policy_provider, |
| 170 | std::unique_ptr<policy::SchemaRegistry> owned_schema_registry) |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 171 | : old_policies_(new base::DictionaryValue()), |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 172 | default_values_(new base::DictionaryValue()), |
| 173 | policy_service_(policy_service), |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 174 | owned_schema_registry_(std::move(owned_schema_registry)), |
| 175 | owned_policy_provider_(std::move(owned_policy_provider)), |
| 176 | owned_policy_service_(std::move(owned_policy_service)) { |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 177 | DCHECK(policy_service_); |
| 178 | DCHECK(owned_schema_registry_); |
| 179 | |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 180 | // Initialize the default values for each policy. |
| 181 | default_values_->SetBoolean(key::kRemoteAccessHostFirewallTraversal, true); |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 182 | default_values_->SetBoolean(key::kRemoteAccessHostRequireCurtain, false); |
| 183 | default_values_->SetBoolean(key::kRemoteAccessHostMatchUsername, false); |
jamiewalch | b438fb9d | 2016-02-08 23:20:12 | [diff] [blame] | 184 | default_values_->SetString(key::kRemoteAccessHostClientDomain, std::string()); |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 185 | default_values_->SetString(key::kRemoteAccessHostDomain, std::string()); |
| 186 | default_values_->SetString(key::kRemoteAccessHostTalkGadgetPrefix, |
| 187 | kDefaultHostTalkGadgetPrefix); |
| 188 | default_values_->SetString(key::kRemoteAccessHostTokenUrl, std::string()); |
| 189 | default_values_->SetString(key::kRemoteAccessHostTokenValidationUrl, |
| 190 | std::string()); |
| 191 | default_values_->SetString( |
| 192 | key::kRemoteAccessHostTokenValidationCertificateIssuer, std::string()); |
| 193 | default_values_->SetBoolean(key::kRemoteAccessHostAllowClientPairing, true); |
| 194 | default_values_->SetBoolean(key::kRemoteAccessHostAllowGnubbyAuth, true); |
| 195 | default_values_->SetBoolean(key::kRemoteAccessHostAllowRelayedConnection, |
| 196 | true); |
| 197 | default_values_->SetString(key::kRemoteAccessHostUdpPortRange, ""); |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | PolicyWatcher::~PolicyWatcher() { |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 201 | // Stop observing |policy_service_| if StartWatching() has been called. |
| 202 | if (!policy_updated_callback_.is_null()) { |
| 203 | policy_service_->RemoveObserver(policy::POLICY_DOMAIN_CHROME, this); |
| 204 | } |
| 205 | |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 206 | if (owned_policy_provider_) { |
| 207 | owned_policy_provider_->Shutdown(); |
| 208 | } |
| 209 | } |
| 210 | |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 211 | const policy::Schema* PolicyWatcher::GetPolicySchema() const { |
| 212 | return owned_schema_registry_->schema_map()->GetSchema(GetPolicyNamespace()); |
| 213 | } |
| 214 | |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 215 | bool PolicyWatcher::NormalizePolicies(base::DictionaryValue* policy_dict) { |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 216 | // Allowing unrecognized policy names allows presence of |
| 217 | // 1) comments (i.e. JSON of the form: { "_comment": "blah", ... }), |
| 218 | // 2) policies intended for future/newer versions of the host, |
| 219 | // 3) policies not supported on all OS-s (i.e. RemoteAccessHostMatchUsername |
| 220 | // is not supported on Windows and therefore policy_templates.json omits |
| 221 | // schema for this policy on this particular platform). |
| 222 | auto strategy = policy::SCHEMA_ALLOW_UNKNOWN_TOPLEVEL; |
| 223 | |
| 224 | std::string path; |
| 225 | std::string error; |
lukasza | 1ffc24b | 2015-02-23 23:28:03 | [diff] [blame] | 226 | bool changed = false; |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 227 | const policy::Schema* schema = GetPolicySchema(); |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 228 | if (schema->Normalize(policy_dict, strategy, &path, &error, &changed)) { |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 229 | if (changed) { |
| 230 | LOG(WARNING) << "Unknown (unrecognized or unsupported) policy: " << path |
| 231 | << ": " << error; |
| 232 | } |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 233 | return true; |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 234 | } else { |
| 235 | LOG(ERROR) << "Invalid policy contents: " << path << ": " << error; |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 236 | return false; |
lukasza | e5d69ea | 2015-02-23 21:00:31 | [diff] [blame] | 237 | } |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 238 | } |
| 239 | |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 240 | namespace { |
| 241 | void CopyDictionaryValue(const base::DictionaryValue& from, |
| 242 | base::DictionaryValue& to, |
| 243 | std::string key) { |
| 244 | const base::Value* value; |
| 245 | if (from.Get(key, &value)) { |
dcheng | efaf1b11 | 2016-05-03 01:12:49 | [diff] [blame] | 246 | to.Set(key, value->CreateDeepCopy()); |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } // namespace |
| 250 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 251 | std::unique_ptr<base::DictionaryValue> |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 252 | PolicyWatcher::StoreNewAndReturnChangedPolicies( |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 253 | std::unique_ptr<base::DictionaryValue> new_policies) { |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 254 | // Find the changed policies. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 255 | std::unique_ptr<base::DictionaryValue> changed_policies( |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 256 | new base::DictionaryValue()); |
| 257 | base::DictionaryValue::Iterator iter(*new_policies); |
| 258 | while (!iter.IsAtEnd()) { |
| 259 | base::Value* old_policy; |
| 260 | if (!(old_policies_->Get(iter.key(), &old_policy) && |
| 261 | old_policy->Equals(&iter.value()))) { |
dcheng | efaf1b11 | 2016-05-03 01:12:49 | [diff] [blame] | 262 | changed_policies->Set(iter.key(), iter.value().CreateDeepCopy()); |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 263 | } |
| 264 | iter.Advance(); |
| 265 | } |
| 266 | |
| 267 | // If one of ThirdPartyAuthConfig policies changed, we need to include all. |
| 268 | if (changed_policies->HasKey(key::kRemoteAccessHostTokenUrl) || |
| 269 | changed_policies->HasKey(key::kRemoteAccessHostTokenValidationUrl) || |
| 270 | changed_policies->HasKey( |
| 271 | key::kRemoteAccessHostTokenValidationCertificateIssuer)) { |
| 272 | CopyDictionaryValue(*new_policies, *changed_policies, |
| 273 | key::kRemoteAccessHostTokenUrl); |
| 274 | CopyDictionaryValue(*new_policies, *changed_policies, |
| 275 | key::kRemoteAccessHostTokenValidationUrl); |
| 276 | CopyDictionaryValue(*new_policies, *changed_policies, |
| 277 | key::kRemoteAccessHostTokenValidationCertificateIssuer); |
| 278 | } |
| 279 | |
| 280 | // Save the new policies. |
| 281 | old_policies_.swap(new_policies); |
| 282 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 283 | return changed_policies; |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void PolicyWatcher::OnPolicyUpdated(const policy::PolicyNamespace& ns, |
| 287 | const policy::PolicyMap& previous, |
| 288 | const policy::PolicyMap& current) { |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 289 | std::unique_ptr<base::DictionaryValue> new_policies = |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 290 | CopyChromotingPoliciesIntoDictionary(current); |
| 291 | |
| 292 | // Check for mistyped values and get rid of unknown policies. |
| 293 | if (!NormalizePolicies(new_policies.get())) { |
| 294 | SignalPolicyError(); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | // Use default values for any missing policies. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 299 | std::unique_ptr<base::DictionaryValue> filled_policies = |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 300 | CopyValuesAndAddDefaults(*new_policies, *default_values_); |
| 301 | |
| 302 | // Limit reporting to only the policies that were changed. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 303 | std::unique_ptr<base::DictionaryValue> changed_policies = |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 304 | StoreNewAndReturnChangedPolicies(std::move(filled_policies)); |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 305 | if (changed_policies->empty()) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | // Verify that we are calling the callback with valid policies. |
| 310 | if (!VerifyWellformedness(*changed_policies)) { |
| 311 | SignalPolicyError(); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | // Notify our client of the changed policies. |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 316 | policy_updated_callback_.Run(std::move(changed_policies)); |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 317 | } |
| 318 | |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 319 | void PolicyWatcher::OnPolicyServiceInitialized(policy::PolicyDomain domain) { |
| 320 | policy::PolicyNamespace ns = GetPolicyNamespace(); |
| 321 | const policy::PolicyMap& current = policy_service_->GetPolicies(ns); |
| 322 | OnPolicyUpdated(ns, current, current); |
| 323 | } |
| 324 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 325 | std::unique_ptr<PolicyWatcher> PolicyWatcher::CreateFromPolicyLoader( |
| 326 | std::unique_ptr<policy::AsyncPolicyLoader> async_policy_loader) { |
| 327 | std::unique_ptr<policy::SchemaRegistry> schema_registry = |
| 328 | CreateSchemaRegistry(); |
| 329 | std::unique_ptr<policy::AsyncPolicyProvider> policy_provider( |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 330 | new policy::AsyncPolicyProvider(schema_registry.get(), |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 331 | std::move(async_policy_loader))); |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 332 | policy_provider->Init(schema_registry.get()); |
| 333 | |
| 334 | policy::PolicyServiceImpl::Providers providers; |
| 335 | providers.push_back(policy_provider.get()); |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 336 | std::unique_ptr<policy::PolicyService> policy_service( |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 337 | new policy::PolicyServiceImpl(providers)); |
| 338 | |
| 339 | policy::PolicyService* borrowed_policy_service = policy_service.get(); |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 340 | return base::WrapUnique(new PolicyWatcher( |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 341 | borrowed_policy_service, std::move(policy_service), |
| 342 | std::move(policy_provider), std::move(schema_registry))); |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 343 | } |
| 344 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 345 | std::unique_ptr<PolicyWatcher> PolicyWatcher::Create( |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 346 | policy::PolicyService* policy_service, |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 347 | const scoped_refptr<base::SingleThreadTaskRunner>& file_task_runner) { |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 348 | #if defined(OS_CHROMEOS) |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 349 | // On Chrome OS the PolicyService is owned by the browser. |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 350 | DCHECK(policy_service); |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 351 | return base::WrapUnique(new PolicyWatcher(policy_service, nullptr, nullptr, |
| 352 | CreateSchemaRegistry())); |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 353 | #else // !defined(OS_CHROMEOS) |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 354 | DCHECK(!policy_service); |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 355 | |
| 356 | // Create platform-specific PolicyLoader. Always read the Chrome policies |
| 357 | // (even on Chromium) so that policy enforcement can't be bypassed by running |
| 358 | // Chromium. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 359 | std::unique_ptr<policy::AsyncPolicyLoader> policy_loader; |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 360 | #if defined(OS_WIN) |
lukasza | bccb49e | 2015-02-24 20:10:28 | [diff] [blame] | 361 | policy_loader.reset(new policy::PolicyLoaderWin( |
| 362 | file_task_runner, L"SOFTWARE\\Policies\\Google\\Chrome", |
| 363 | nullptr)); // nullptr = don't use GPO / always read from the registry. |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 364 | #elif defined(OS_MACOSX) |
| 365 | CFStringRef bundle_id = CFSTR("com.google.Chrome"); |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 366 | policy_loader.reset(new policy::PolicyLoaderMac( |
| 367 | file_task_runner, |
| 368 | policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id), |
| 369 | new MacPreferences(), bundle_id)); |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 370 | #elif defined(OS_POSIX) && !defined(OS_ANDROID) |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 371 | policy_loader.reset(new policy::ConfigDirPolicyLoader( |
| 372 | file_task_runner, |
| 373 | base::FilePath(FILE_PATH_LITERAL("/etc/opt/chrome/policies")), |
| 374 | policy::POLICY_SCOPE_MACHINE)); |
lambroslambrou | 51e19b9 | 2016-04-13 21:30:41 | [diff] [blame] | 375 | #elif defined(OS_ANDROID) |
| 376 | NOTIMPLEMENTED(); |
lambroslambrou | 709327e | 2016-04-26 19:07:34 | [diff] [blame] | 377 | policy::PolicyServiceImpl::Providers providers; |
| 378 | std::unique_ptr<policy::PolicyService> owned_policy_service( |
| 379 | new policy::PolicyServiceImpl(providers)); |
| 380 | return base::WrapUnique(new PolicyWatcher( |
| 381 | owned_policy_service.get(), std::move(owned_policy_service), nullptr, |
| 382 | CreateSchemaRegistry())); |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 383 | #else |
| 384 | #error OS that is not yet supported by PolicyWatcher code. |
| 385 | #endif |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 386 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 387 | return PolicyWatcher::CreateFromPolicyLoader(std::move(policy_loader)); |
sergeyu | b2ae7e3 | 2015-01-30 23:33:25 | [diff] [blame] | 388 | #endif // !(OS_CHROMEOS) |
lukasza | a9376cd | 2015-01-28 20:29:01 | [diff] [blame] | 389 | } |
| 390 | |
[email protected] | 000d1f6 | 2012-07-24 01:56:54 | [diff] [blame] | 391 | } // namespace remoting |