Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/cloud_policy_cache.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/task.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/browser_thread.h" | |
| 14 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | |
| 15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 16 #include "chrome/browser/policy/proto/device_management_constants.h" | |
| 17 #include "chrome/browser/policy/proto/device_management_local.pb.h" | |
| 18 | |
| 19 using google::protobuf::RepeatedField; | |
| 20 using google::protobuf::RepeatedPtrField; | |
| 21 | |
| 22 // This CloudPolicyCache currently supports two protocols for the interaction | |
| 23 // with DMServer: the old "DevicePolicy" format, which is being used in the | |
| 24 // CrOS Pilot Program and will be deprecated afterwards, and the new | |
| 25 // "CloudPolicy" format, which will be used exclusively after the public launch | |
| 26 // of ChromeOS. | |
| 27 | |
| 28 namespace policy { | |
| 29 | |
| 30 // Saves policy information to a file. | |
| 31 class PersistPolicyTask : public Task { | |
| 32 public: | |
| 33 PersistPolicyTask(const FilePath& path, | |
| 34 const em::CloudPolicyResponse* cloud_policy_response, | |
| 35 const em::DevicePolicyResponse* device_policy_response, | |
| 36 const bool is_unmanaged) | |
| 37 : path_(path), | |
| 38 cloud_policy_response_(cloud_policy_response), | |
| 39 device_policy_response_(device_policy_response), | |
| 40 is_unmanaged_(is_unmanaged) {} | |
| 41 | |
| 42 private: | |
| 43 // Task override. | |
| 44 virtual void Run(); | |
| 45 | |
| 46 const FilePath path_; | |
| 47 scoped_ptr<const em::CloudPolicyResponse> cloud_policy_response_; | |
| 48 scoped_ptr<const em::DevicePolicyResponse> device_policy_response_; | |
| 49 const bool is_unmanaged_; | |
| 50 }; | |
| 51 | |
| 52 void PersistPolicyTask::Run() { | |
| 53 std::string data; | |
| 54 em::CachedCloudPolicyResponse cached_policy; | |
| 55 if (cloud_policy_response_.get()) { | |
| 56 cached_policy.mutable_cloud_policy()->CopyFrom(*cloud_policy_response_); | |
| 57 } else if (device_policy_response_.get()) { | |
| 58 cached_policy.mutable_device_policy()->CopyFrom(*device_policy_response_); | |
| 59 cached_policy.set_timestamp(base::Time::NowFromSystemTime().ToTimeT()); | |
| 60 } | |
| 61 if (is_unmanaged_) { | |
| 62 cached_policy.set_unmanaged(true); | |
| 63 cached_policy.set_timestamp( | |
| 64 base::Time::NowFromSystemTime().ToTimeT()); | |
| 65 } | |
| 66 if (!cached_policy.SerializeToString(&data)) { | |
| 67 LOG(WARNING) << "Failed to serialize policy data"; | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 int size = data.size(); | |
| 72 if (file_util::WriteFile(path_, data.c_str(), size) != size) { | |
| 73 LOG(WARNING) << "Failed to write " << path_.value(); | |
| 74 return; | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 CloudPolicyCache::CloudPolicyCache( | |
| 79 const FilePath& backing_file_path) | |
| 80 : backing_file_path_(backing_file_path), | |
| 81 mandatory_policy_(new ConfigurationPolicyProvider::PolicyMapType), | |
| 82 recommended_policy_(new ConfigurationPolicyProvider::PolicyMapType), | |
| 83 device_policy_(new DictionaryValue), | |
| 84 fresh_policy_(false), | |
| 85 is_unmanaged_(false), | |
| 86 has_device_policy_(false) { | |
| 87 } | |
| 88 | |
| 89 CloudPolicyCache::~CloudPolicyCache() {} | |
| 90 | |
| 91 void CloudPolicyCache::LoadPolicyFromFile() { | |
| 92 if (!file_util::PathExists(backing_file_path_) || fresh_policy_) { | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 // Read the protobuf from the file. | |
| 97 std::string data; | |
| 98 if (!file_util::ReadFileToString(backing_file_path_, &data)) { | |
| 99 LOG(WARNING) << "Failed to read policy data from " | |
| 100 << backing_file_path_.value(); | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 em::CachedCloudPolicyResponse cached_response; | |
| 105 if (!cached_response.ParseFromArray(data.c_str(), data.size())) { | |
| 106 LOG(WARNING) << "Failed to parse policy data read from " | |
| 107 << backing_file_path_.value(); | |
| 108 return; | |
| 109 } | |
| 110 base::Time timestamp; | |
| 111 is_unmanaged_ = cached_response.unmanaged(); | |
| 112 if (is_unmanaged_ || cached_response.has_device_policy()) { | |
| 113 timestamp = base::Time::FromTimeT(cached_response.timestamp()); | |
| 114 if (timestamp > base::Time::NowFromSystemTime()) { | |
| 115 LOG(WARNING) << "Rejected policy data from " << backing_file_path_.value() | |
| 116 << ", file is from the future."; | |
| 117 return; | |
| 118 } | |
| 119 } | |
| 120 if (is_unmanaged_) { | |
| 121 base::AutoLock lock(lock_); | |
| 122 last_policy_refresh_time_ = timestamp; | |
| 123 return; | |
| 124 } | |
| 125 // Decode and swap in the new policy information. | |
| 126 if (cached_response.has_cloud_policy()) { | |
| 127 scoped_ptr<ConfigurationPolicyProvider::PolicyMapType> mandatory_policy( | |
| 128 new ConfigurationPolicyProvider::PolicyMapType); | |
| 129 scoped_ptr<ConfigurationPolicyProvider::PolicyMapType> recommended_policy( | |
| 130 new ConfigurationPolicyProvider::PolicyMapType); | |
| 131 bool ok = DecodePolicyResponse(cached_response.cloud_policy(), | |
| 132 mandatory_policy.get(), | |
| 133 recommended_policy.get(), | |
| 134 ×tamp); | |
| 135 if (!is_unmanaged_ && !ok) { | |
| 136 LOG(WARNING) << "Decoding policy data failed."; | |
| 137 return; | |
| 138 } | |
| 139 { | |
| 140 base::AutoLock lock(lock_); | |
| 141 if (!fresh_policy_) { | |
| 142 mandatory_policy_.reset(mandatory_policy.release()); | |
| 143 recommended_policy_.reset(recommended_policy.release()); | |
| 144 last_policy_refresh_time_ = timestamp; | |
| 145 has_device_policy_ = false; | |
| 146 } | |
| 147 } | |
| 148 } else if (cached_response.has_device_policy()) { | |
| 149 scoped_ptr<DictionaryValue> value( | |
| 150 DecodeDevicePolicy(cached_response.device_policy())); | |
| 151 { | |
| 152 base::AutoLock lock(lock_); | |
| 153 if (!fresh_policy_) { | |
| 154 device_policy_.reset(value.release()); | |
| 155 last_policy_refresh_time_ = timestamp; | |
| 156 has_device_policy_ = true; | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 bool CloudPolicyCache::SetPolicy( | |
| 163 const em::CloudPolicyResponse& policy) { | |
| 164 is_unmanaged_ = false; | |
| 165 base::Time timestamp; | |
| 166 scoped_ptr<ConfigurationPolicyProvider::PolicyMapType> mandatory_policy_map( | |
|
Mattias Nissler (ping if slow)
2011/02/03 16:23:41
why are all maps wrapped in a scoped_ptr? I think
Jakob Kummerow
2011/02/08 16:15:43
Done.
| |
| 167 new ConfigurationPolicyProvider::PolicyMapType); | |
| 168 scoped_ptr<ConfigurationPolicyProvider::PolicyMapType> recommended_policy_map( | |
| 169 new ConfigurationPolicyProvider::PolicyMapType); | |
| 170 bool ok = DecodePolicyResponse(policy, mandatory_policy_map.get(), | |
| 171 recommended_policy_map.get(), ×tamp); | |
| 172 if (!ok) { | |
| 173 // TODO(jkummerow): Signal error to PolicyProvider. | |
| 174 return false; | |
| 175 } | |
| 176 const bool new_policy_differs = | |
| 177 !Equals(mandatory_policy_map.get(), mandatory_policy_.get()) || | |
| 178 !Equals(recommended_policy_map.get(), recommended_policy_.get()); | |
| 179 { | |
| 180 base::AutoLock lock(lock_); | |
| 181 mandatory_policy_.reset(mandatory_policy_map.release()); | |
| 182 recommended_policy_.reset(recommended_policy_map.release()); | |
| 183 fresh_policy_ = true; | |
| 184 last_policy_refresh_time_ = timestamp; | |
| 185 has_device_policy_ = false; | |
| 186 } | |
| 187 | |
| 188 em::CloudPolicyResponse* policy_copy = new em::CloudPolicyResponse; | |
| 189 policy_copy->CopyFrom(policy); | |
| 190 BrowserThread::PostTask( | |
| 191 BrowserThread::FILE, | |
| 192 FROM_HERE, | |
| 193 new PersistPolicyTask(backing_file_path_, policy_copy, NULL, false)); | |
| 194 return new_policy_differs; | |
| 195 } | |
| 196 | |
| 197 bool CloudPolicyCache::SetPolicy( | |
| 198 const em::DevicePolicyResponse& policy) { | |
| 199 is_unmanaged_ = false; | |
| 200 DictionaryValue* value = DecodeDevicePolicy(policy); | |
| 201 const bool new_policy_differs = !(value->Equals(device_policy_.get())); | |
| 202 base::Time now(base::Time::NowFromSystemTime()); | |
| 203 { | |
| 204 base::AutoLock lock(lock_); | |
| 205 device_policy_.reset(value); | |
| 206 fresh_policy_ = true; | |
| 207 last_policy_refresh_time_ = now; | |
| 208 has_device_policy_ = true; | |
| 209 } | |
| 210 | |
| 211 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse; | |
| 212 policy_copy->CopyFrom(policy); | |
| 213 BrowserThread::PostTask( | |
| 214 BrowserThread::FILE, | |
| 215 FROM_HERE, | |
| 216 new PersistPolicyTask(backing_file_path_, NULL, policy_copy, false)); | |
| 217 return new_policy_differs; | |
| 218 } | |
| 219 | |
| 220 DictionaryValue* CloudPolicyCache::GetDevicePolicy() { | |
| 221 base::AutoLock lock(lock_); | |
| 222 return device_policy_->DeepCopy(); | |
| 223 } | |
| 224 | |
| 225 ConfigurationPolicyProvider::PolicyMapType* | |
| 226 CloudPolicyCache::GetMandatoryPolicy() { | |
| 227 return CopyPolicyMap(mandatory_policy_.get()); | |
|
Mattias Nissler (ping if slow)
2011/02/03 16:23:41
Can't we just return a const pointer instead of co
Jakob Kummerow
2011/02/08 16:15:43
Done.
| |
| 228 } | |
| 229 | |
| 230 ConfigurationPolicyProvider::PolicyMapType* | |
| 231 CloudPolicyCache::GetRecommendedPolicy() { | |
| 232 return CopyPolicyMap(recommended_policy_.get()); | |
|
Mattias Nissler (ping if slow)
2011/02/03 16:23:41
same here.
Jakob Kummerow
2011/02/08 16:15:43
Done.
| |
| 233 } | |
| 234 | |
| 235 void CloudPolicyCache::SetUnmanaged() { | |
| 236 is_unmanaged_ = true; | |
| 237 { | |
| 238 base::AutoLock lock(lock_); | |
| 239 mandatory_policy_.reset(new ConfigurationPolicyProvider::PolicyMapType); | |
| 240 recommended_policy_.reset(new ConfigurationPolicyProvider::PolicyMapType); | |
| 241 last_policy_refresh_time_ = base::Time::NowFromSystemTime(); | |
| 242 } | |
| 243 BrowserThread::PostTask( | |
| 244 BrowserThread::FILE, | |
| 245 FROM_HERE, | |
| 246 new PersistPolicyTask(backing_file_path_, NULL, NULL, true)); | |
| 247 } | |
| 248 | |
| 249 // static | |
| 250 bool CloudPolicyCache::DecodePolicyResponse( | |
| 251 const em::CloudPolicyResponse& policy_response, | |
| 252 ConfigurationPolicyProvider::PolicyMapType* mandatory, | |
| 253 ConfigurationPolicyProvider::PolicyMapType* recommended, | |
| 254 base::Time* timestamp) { | |
| 255 std::string data = policy_response.signed_response(); | |
| 256 | |
| 257 if (!VerifySignature(policy_response.signature(), data, | |
| 258 policy_response.certificate_chain())) { | |
| 259 LOG(WARNING) << "Failed to verify signature."; | |
| 260 return false; | |
| 261 } | |
| 262 | |
| 263 em::SignedCloudPolicyResponse response; | |
| 264 if (!response.ParseFromArray(data.c_str(), data.size())) { | |
| 265 LOG(WARNING) << "Failed to parse SignedCloudPolicyResponse protobuf."; | |
| 266 return false; | |
| 267 } | |
| 268 | |
| 269 // TODO(jkummerow): Verify response.device_token(). Needs final specification | |
| 270 // which token we're actually sending / expecting to get back. | |
| 271 | |
| 272 // TODO(jkummerow): Store response.device_name(), if we decide to transfer | |
| 273 // it from the server to the client. | |
| 274 | |
| 275 // Reject policies that claim to be from the future (with a safety margin of | |
| 276 // one minute). | |
| 277 DCHECK(timestamp); | |
| 278 *timestamp = base::Time::FromTimeT(response.timestamp()); | |
| 279 if (*timestamp > base::Time::NowFromSystemTime() + | |
| 280 base::TimeDelta::FromMinutes(1)) { | |
|
Mattias Nissler (ping if slow)
2011/02/03 16:23:41
I would do this only for data that comes from the
Jakob Kummerow
2011/02/08 16:15:43
Done.
| |
| 281 LOG(WARNING) << "Rejected policy data, timestamp is from the future."; | |
| 282 return false; | |
| 283 } | |
| 284 | |
| 285 DecodePolicy(response.settings(), mandatory, recommended); | |
| 286 return true; | |
| 287 } | |
| 288 | |
| 289 // static | |
| 290 bool CloudPolicyCache::VerifySignature( | |
| 291 const std::string& signature, | |
| 292 const std::string& data, | |
| 293 const RepeatedPtrField<std::string>& certificate_chain) { | |
| 294 // TODO(jkummerow): Implement this. Non-trivial because we want to do it | |
| 295 // for all platforms -> it's enough work to deserve its own CL. | |
| 296 // Don't forget to also verify the hostname of the server against the cert. | |
| 297 return true; | |
| 298 } | |
| 299 | |
| 300 // static | |
| 301 bool CloudPolicyCache::Equals( | |
| 302 const ConfigurationPolicyProvider::PolicyMapType* a, | |
| 303 const ConfigurationPolicyProvider::PolicyMapType* b) { | |
| 304 for (ConfigurationPolicyProvider::PolicyMapType::const_iterator it = | |
|
danno
2011/02/03 15:31:38
nit: move the declaration to another line. easier
Jakob Kummerow
2011/02/08 16:15:43
Done.
| |
| 305 a->begin(); it != a->end(); ++it) { | |
| 306 ConfigurationPolicyProvider::PolicyMapType::const_iterator is_in_b = | |
| 307 b->find(it->first); | |
| 308 if (is_in_b == b->end()) | |
| 309 return false; | |
| 310 if (!it->second->Equals(is_in_b->second)) | |
| 311 return false; | |
| 312 } | |
| 313 for (ConfigurationPolicyProvider::PolicyMapType::const_iterator it = | |
|
danno
2011/02/03 15:31:38
nit: move the declaration to another line. easier
Jakob Kummerow
2011/02/08 16:15:43
Done.
| |
| 314 b->begin(); it != b->end(); ++it) { | |
| 315 if (a->find(it->first) == a->end()) | |
| 316 return false; | |
| 317 } | |
| 318 return true; | |
| 319 } | |
| 320 | |
| 321 ConfigurationPolicyProvider::PolicyMapType* | |
| 322 CloudPolicyCache::CopyPolicyMap( | |
| 323 const ConfigurationPolicyProvider::PolicyMapType* original) { | |
| 324 base::AutoLock lock(lock_); | |
| 325 ConfigurationPolicyProvider::PolicyMapType* policy_copy = | |
| 326 new ConfigurationPolicyProvider::PolicyMapType; | |
| 327 for (ConfigurationPolicyProvider::PolicyMapType::const_iterator it = | |
|
danno
2011/02/03 15:31:38
nit: move the declaration to another line. easier
Jakob Kummerow
2011/02/08 16:15:43
Done.
| |
| 328 original->begin(); | |
| 329 it != original->end(); ++it) { | |
| 330 policy_copy->insert(std::make_pair(it->first, it->second->DeepCopy())); | |
| 331 } | |
| 332 return policy_copy; | |
| 333 } | |
| 334 | |
| 335 // static | |
| 336 Value* CloudPolicyCache::DecodeIntegerValue( | |
| 337 google::protobuf::int64 value) { | |
| 338 if (value < std::numeric_limits<int>::min() || | |
| 339 value > std::numeric_limits<int>::max()) { | |
| 340 LOG(WARNING) << "Integer value " << value | |
| 341 << " out of numeric limits, ignoring."; | |
| 342 return NULL; | |
| 343 } | |
| 344 | |
| 345 return Value::CreateIntegerValue(static_cast<int>(value)); | |
| 346 } | |
| 347 | |
| 348 // static | |
| 349 Value* CloudPolicyCache::DecodeValue(const em::GenericValue& value) { | |
| 350 if (!value.has_value_type()) | |
| 351 return NULL; | |
| 352 | |
| 353 switch (value.value_type()) { | |
| 354 case em::GenericValue::VALUE_TYPE_BOOL: | |
| 355 if (value.has_bool_value()) | |
| 356 return Value::CreateBooleanValue(value.bool_value()); | |
| 357 return NULL; | |
| 358 case em::GenericValue::VALUE_TYPE_INT64: | |
| 359 if (value.has_int64_value()) | |
| 360 return DecodeIntegerValue(value.int64_value()); | |
| 361 return NULL; | |
| 362 case em::GenericValue::VALUE_TYPE_STRING: | |
| 363 if (value.has_string_value()) | |
| 364 return Value::CreateStringValue(value.string_value()); | |
| 365 return NULL; | |
| 366 case em::GenericValue::VALUE_TYPE_DOUBLE: | |
| 367 if (value.has_double_value()) | |
| 368 return Value::CreateDoubleValue(value.double_value()); | |
| 369 return NULL; | |
| 370 case em::GenericValue::VALUE_TYPE_BYTES: | |
| 371 if (value.has_bytes_value()) { | |
| 372 std::string bytes = value.bytes_value(); | |
| 373 return BinaryValue::CreateWithCopiedBuffer(bytes.c_str(), bytes.size()); | |
| 374 } | |
| 375 return NULL; | |
| 376 case em::GenericValue::VALUE_TYPE_BOOL_ARRAY: { | |
| 377 ListValue* list = new ListValue; | |
| 378 RepeatedField<bool>::const_iterator i; | |
| 379 for (i = value.bool_array().begin(); i != value.bool_array().end(); ++i) | |
| 380 list->Append(Value::CreateBooleanValue(*i)); | |
| 381 return list; | |
| 382 } | |
| 383 case em::GenericValue::VALUE_TYPE_INT64_ARRAY: { | |
| 384 ListValue* list = new ListValue; | |
| 385 RepeatedField<google::protobuf::int64>::const_iterator i; | |
| 386 for (i = value.int64_array().begin(); | |
| 387 i != value.int64_array().end(); ++i) { | |
| 388 Value* int_value = DecodeIntegerValue(*i); | |
| 389 if (int_value) | |
| 390 list->Append(int_value); | |
| 391 } | |
| 392 return list; | |
| 393 } | |
| 394 case em::GenericValue::VALUE_TYPE_STRING_ARRAY: { | |
| 395 ListValue* list = new ListValue; | |
| 396 RepeatedPtrField<std::string>::const_iterator i; | |
| 397 for (i = value.string_array().begin(); | |
| 398 i != value.string_array().end(); ++i) | |
| 399 list->Append(Value::CreateStringValue(*i)); | |
| 400 return list; | |
| 401 } | |
| 402 case em::GenericValue::VALUE_TYPE_DOUBLE_ARRAY: { | |
| 403 ListValue* list = new ListValue; | |
| 404 RepeatedField<double>::const_iterator i; | |
| 405 for (i = value.double_array().begin(); | |
| 406 i != value.double_array().end(); ++i) | |
| 407 list->Append(Value::CreateDoubleValue(*i)); | |
| 408 return list; | |
| 409 } | |
| 410 default: | |
| 411 NOTREACHED() << "Unhandled value type"; | |
| 412 } | |
| 413 | |
| 414 return NULL; | |
| 415 } | |
| 416 | |
| 417 // static | |
| 418 DictionaryValue* CloudPolicyCache::DecodeDevicePolicy( | |
| 419 const em::DevicePolicyResponse& policy) { | |
| 420 DictionaryValue* result = new DictionaryValue; | |
| 421 RepeatedPtrField<em::DevicePolicySetting>::const_iterator setting; | |
| 422 for (setting = policy.setting().begin(); | |
| 423 setting != policy.setting().end(); | |
| 424 ++setting) { | |
| 425 // Wrong policy key? Skip. | |
| 426 if (setting->policy_key().compare(kChromeDevicePolicySettingKey) != 0) | |
| 427 continue; | |
| 428 | |
| 429 // No policy value? Skip. | |
| 430 if (!setting->has_policy_value()) | |
| 431 continue; | |
| 432 | |
| 433 // Iterate through all the name-value pairs wrapped in |setting|. | |
| 434 const em::GenericSetting& policy_value(setting->policy_value()); | |
| 435 RepeatedPtrField<em::GenericNamedValue>::const_iterator named_value; | |
| 436 for (named_value = policy_value.named_value().begin(); | |
| 437 named_value != policy_value.named_value().end(); | |
| 438 ++named_value) { | |
| 439 if (named_value->has_value()) { | |
| 440 Value* decoded_value = | |
| 441 CloudPolicyCache::DecodeValue(named_value->value()); | |
| 442 if (decoded_value) | |
| 443 result->Set(named_value->name(), decoded_value); | |
| 444 } | |
| 445 } | |
| 446 } | |
| 447 return result; | |
| 448 } | |
| 449 | |
| 450 } // namespace policy | |
| OLD | NEW |