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 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 54 std::string data; | |
| 55 em::CachedCloudPolicyResponse cached_policy; | |
| 56 if (cloud_policy_response_.get()) { | |
| 57 cached_policy.mutable_cloud_policy()->CopyFrom(*cloud_policy_response_); | |
| 58 } else if (device_policy_response_.get()) { | |
| 59 cached_policy.mutable_device_policy()->CopyFrom(*device_policy_response_); | |
| 60 cached_policy.set_timestamp(base::Time::NowFromSystemTime().ToTimeT()); | |
| 61 } | |
| 62 if (is_unmanaged_) { | |
| 63 cached_policy.set_unmanaged(true); | |
| 64 cached_policy.set_timestamp(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 device_policy_(new DictionaryValue), | |
| 82 fresh_policy_(false), | |
| 83 is_unmanaged_(false), | |
| 84 has_device_policy_(false) { | |
| 85 } | |
| 86 | |
| 87 CloudPolicyCache::~CloudPolicyCache() {} | |
| 88 | |
| 89 void CloudPolicyCache::LoadPolicyFromFile() { | |
| 90 // TODO(jkummerow): This method is doing file IO during browser startup. In | |
| 91 // the long run it would be better to delay this until the FILE thread exists. | |
| 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 PolicyMapType mandatory_policy; | |
| 112 PolicyMapType recommended_policy; | |
| 113 is_unmanaged_ = cached_response.unmanaged(); | |
| 114 if (is_unmanaged_ || cached_response.has_device_policy()) | |
| 115 timestamp = base::Time::FromTimeT(cached_response.timestamp()); | |
| 116 if (cached_response.has_cloud_policy()) { | |
| 117 DCHECK(!is_unmanaged_); | |
| 118 bool ok = DecodePolicyResponse(cached_response.cloud_policy(), | |
| 119 &mandatory_policy, | |
| 120 &recommended_policy, | |
| 121 ×tamp); | |
| 122 if (!ok) { | |
| 123 LOG(WARNING) << "Decoding policy data failed."; | |
| 124 return; | |
| 125 } | |
| 126 } | |
| 127 if (timestamp > base::Time::NowFromSystemTime()) { | |
| 128 LOG(WARNING) << "Rejected policy data from " << backing_file_path_.value() | |
| 129 << ", file is from the future."; | |
| 130 return; | |
| 131 } | |
| 132 // Swap in the new policy information. | |
| 133 if (is_unmanaged_) { | |
| 134 base::AutoLock lock(lock_); | |
| 135 last_policy_refresh_time_ = timestamp; | |
| 136 return; | |
| 137 } else if (cached_response.has_cloud_policy()) { | |
| 138 if (!fresh_policy_) { | |
| 139 base::AutoLock lock(lock_); | |
| 140 mandatory_policy_.swap(mandatory_policy); | |
| 141 recommended_policy_.swap(recommended_policy); | |
| 142 last_policy_refresh_time_ = timestamp; | |
| 143 has_device_policy_ = false; | |
| 144 } | |
| 145 } else if (cached_response.has_device_policy()) { | |
| 146 scoped_ptr<DictionaryValue> value( | |
| 147 DecodeDevicePolicy(cached_response.device_policy())); | |
| 148 if (!fresh_policy_) { | |
| 149 base::AutoLock lock(lock_); | |
| 150 device_policy_.reset(value.release()); | |
| 151 last_policy_refresh_time_ = timestamp; | |
| 152 has_device_policy_ = true; | |
| 153 } | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 bool CloudPolicyCache::SetPolicy(const em::CloudPolicyResponse& policy) { | |
| 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 159 is_unmanaged_ = false; | |
| 160 base::Time timestamp; | |
| 161 PolicyMapType mandatory_policy; | |
| 162 PolicyMapType recommended_policy; | |
| 163 bool ok = DecodePolicyResponse(policy, &mandatory_policy, &recommended_policy, | |
| 164 ×tamp); | |
| 165 if (!ok) { | |
| 166 // TODO(jkummerow): Signal error to PolicyProvider. | |
| 167 return false; | |
| 168 } | |
| 169 const bool new_policy_differs = | |
| 170 !Equals(mandatory_policy, mandatory_policy_) || | |
| 171 !Equals(recommended_policy, recommended_policy_); | |
| 172 { | |
| 173 base::AutoLock lock(lock_); | |
| 174 mandatory_policy_.swap(mandatory_policy); | |
| 175 recommended_policy_.swap(recommended_policy); | |
| 176 fresh_policy_ = true; | |
| 177 last_policy_refresh_time_ = timestamp; | |
| 178 has_device_policy_ = false; | |
| 179 } | |
| 180 | |
| 181 if (timestamp > base::Time::NowFromSystemTime() + | |
| 182 base::TimeDelta::FromMinutes(1)) { | |
| 183 LOG(WARNING) << "Server returned policy with timestamp from the future, " | |
| 184 "not persisting to disk."; | |
| 185 } else { | |
| 186 em::CloudPolicyResponse* policy_copy = new em::CloudPolicyResponse; | |
| 187 policy_copy->CopyFrom(policy); | |
| 188 BrowserThread::PostTask( | |
| 189 BrowserThread::FILE, | |
| 190 FROM_HERE, | |
| 191 new PersistPolicyTask(backing_file_path_, policy_copy, NULL, false)); | |
| 192 } | |
| 193 return new_policy_differs; | |
| 194 } | |
| 195 | |
| 196 bool CloudPolicyCache::SetDevicePolicy(const em::DevicePolicyResponse& policy) { | |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 198 is_unmanaged_ = false; | |
| 199 DictionaryValue* value = DecodeDevicePolicy(policy); | |
| 200 const bool new_policy_differs = !(value->Equals(device_policy_.get())); | |
| 201 base::Time now(base::Time::NowFromSystemTime()); | |
| 202 { | |
| 203 base::AutoLock lock(lock_); | |
| 204 device_policy_.reset(value); | |
| 205 fresh_policy_ = true; | |
| 206 last_policy_refresh_time_ = now; | |
| 207 has_device_policy_ = true; | |
| 208 } | |
| 209 | |
| 210 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse; | |
| 211 policy_copy->CopyFrom(policy); | |
| 212 BrowserThread::PostTask( | |
| 213 BrowserThread::FILE, | |
| 214 FROM_HERE, | |
| 215 new PersistPolicyTask(backing_file_path_, NULL, policy_copy, false)); | |
| 216 return new_policy_differs; | |
| 217 } | |
| 218 | |
| 219 DictionaryValue* CloudPolicyCache::GetDevicePolicy() { | |
| 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 221 base::AutoLock lock(lock_); | |
| 222 return device_policy_->DeepCopy(); | |
| 223 } | |
| 224 | |
| 225 const CloudPolicyCache::PolicyMapType* | |
| 226 CloudPolicyCache::GetMandatoryPolicy() const { | |
| 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 228 return &mandatory_policy_; | |
| 229 } | |
| 230 | |
| 231 const CloudPolicyCache::PolicyMapType* | |
| 232 CloudPolicyCache::GetRecommendedPolicy() const { | |
| 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 234 return &recommended_policy_; | |
| 235 } | |
| 236 | |
| 237 void CloudPolicyCache::SetUnmanaged() { | |
| 238 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 239 is_unmanaged_ = true; | |
| 240 { | |
| 241 base::AutoLock lock(lock_); | |
| 242 mandatory_policy_.clear(); | |
| 243 recommended_policy_.clear(); | |
| 244 device_policy_.reset(new DictionaryValue); | |
| 245 last_policy_refresh_time_ = base::Time::NowFromSystemTime(); | |
| 246 } | |
| 247 BrowserThread::PostTask( | |
| 248 BrowserThread::FILE, | |
| 249 FROM_HERE, | |
| 250 new PersistPolicyTask(backing_file_path_, NULL, NULL, true)); | |
| 251 } | |
| 252 | |
| 253 // static | |
| 254 bool CloudPolicyCache::DecodePolicyResponse( | |
| 255 const em::CloudPolicyResponse& policy_response, | |
| 256 PolicyMapType* mandatory, | |
| 257 PolicyMapType* recommended, | |
| 258 base::Time* timestamp) { | |
| 259 std::string data = policy_response.signed_response(); | |
| 260 | |
| 261 if (!VerifySignature(policy_response.signature(), data, | |
| 262 policy_response.certificate_chain())) { | |
| 263 LOG(WARNING) << "Failed to verify signature."; | |
| 264 return false; | |
| 265 } | |
| 266 | |
| 267 em::SignedCloudPolicyResponse response; | |
| 268 if (!response.ParseFromArray(data.c_str(), data.size())) { | |
| 269 LOG(WARNING) << "Failed to parse SignedCloudPolicyResponse protobuf."; | |
| 270 return false; | |
| 271 } | |
| 272 | |
| 273 // TODO(jkummerow): Verify response.device_token(). Needs final specification | |
| 274 // which token we're actually sending / expecting to get back. | |
| 275 | |
| 276 // TODO(jkummerow): Store response.device_name(), if we decide to transfer | |
| 277 // it from the server to the client. | |
| 278 | |
| 279 DCHECK(timestamp); | |
| 280 *timestamp = base::Time::FromTimeT(response.timestamp()); | |
| 281 DecodePolicy(response.settings(), mandatory, recommended); | |
| 282 return true; | |
| 283 } | |
| 284 | |
| 285 // static | |
| 286 bool CloudPolicyCache::VerifySignature( | |
| 287 const std::string& signature, | |
| 288 const std::string& data, | |
| 289 const RepeatedPtrField<std::string>& certificate_chain) { | |
| 290 // TODO(jkummerow): Implement this. Non-trivial because we want to do it | |
| 291 // for all platforms -> it's enough work to deserve its own CL. | |
| 292 // Don't forget to also verify the hostname of the server against the cert. | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 296 // static | |
| 297 bool CloudPolicyCache::Equals(const PolicyMapType& a, const PolicyMapType& b) { | |
| 298 for (PolicyMapType::const_iterator it = a.begin(); it != a.end(); ++it) { | |
| 299 PolicyMapType::const_iterator is_in_b = b.find(it->first); | |
| 300 if (is_in_b == b.end()) | |
| 301 return false; | |
| 302 if (!it->second->Equals(is_in_b->second)) | |
| 303 return false; | |
| 304 } | |
| 305 for (PolicyMapType::const_iterator it = b.begin(); it != b.end(); ++it) { | |
| 306 if (a.find(it->first) == a.end()) | |
| 307 return false; | |
| 308 } | |
| 309 return true; | |
| 310 } | |
|
Mattias Nissler (ping if slow)
2011/02/15 10:46:39
Is there anything wrong with the more std-like sol
Jakob Kummerow
2011/02/15 14:22:00
Done.
| |
| 311 | |
| 312 // static | |
| 313 Value* CloudPolicyCache::DecodeIntegerValue(google::protobuf::int64 value) { | |
| 314 if (value < std::numeric_limits<int>::min() || | |
| 315 value > std::numeric_limits<int>::max()) { | |
| 316 LOG(WARNING) << "Integer value " << value | |
| 317 << " out of numeric limits, ignoring."; | |
| 318 return NULL; | |
| 319 } | |
| 320 | |
| 321 return Value::CreateIntegerValue(static_cast<int>(value)); | |
| 322 } | |
| 323 | |
| 324 // static | |
| 325 Value* CloudPolicyCache::DecodeValue(const em::GenericValue& value) { | |
| 326 if (!value.has_value_type()) | |
| 327 return NULL; | |
| 328 | |
| 329 switch (value.value_type()) { | |
| 330 case em::GenericValue::VALUE_TYPE_BOOL: | |
| 331 if (value.has_bool_value()) | |
| 332 return Value::CreateBooleanValue(value.bool_value()); | |
| 333 return NULL; | |
| 334 case em::GenericValue::VALUE_TYPE_INT64: | |
| 335 if (value.has_int64_value()) | |
| 336 return DecodeIntegerValue(value.int64_value()); | |
| 337 return NULL; | |
| 338 case em::GenericValue::VALUE_TYPE_STRING: | |
| 339 if (value.has_string_value()) | |
| 340 return Value::CreateStringValue(value.string_value()); | |
| 341 return NULL; | |
| 342 case em::GenericValue::VALUE_TYPE_DOUBLE: | |
| 343 if (value.has_double_value()) | |
| 344 return Value::CreateDoubleValue(value.double_value()); | |
| 345 return NULL; | |
| 346 case em::GenericValue::VALUE_TYPE_BYTES: | |
| 347 if (value.has_bytes_value()) { | |
| 348 std::string bytes = value.bytes_value(); | |
| 349 return BinaryValue::CreateWithCopiedBuffer(bytes.c_str(), bytes.size()); | |
| 350 } | |
| 351 return NULL; | |
| 352 case em::GenericValue::VALUE_TYPE_BOOL_ARRAY: { | |
| 353 ListValue* list = new ListValue; | |
| 354 RepeatedField<bool>::const_iterator i; | |
| 355 for (i = value.bool_array().begin(); i != value.bool_array().end(); ++i) | |
| 356 list->Append(Value::CreateBooleanValue(*i)); | |
| 357 return list; | |
| 358 } | |
| 359 case em::GenericValue::VALUE_TYPE_INT64_ARRAY: { | |
| 360 ListValue* list = new ListValue; | |
| 361 RepeatedField<google::protobuf::int64>::const_iterator i; | |
| 362 for (i = value.int64_array().begin(); | |
| 363 i != value.int64_array().end(); ++i) { | |
| 364 Value* int_value = DecodeIntegerValue(*i); | |
| 365 if (int_value) | |
| 366 list->Append(int_value); | |
| 367 } | |
| 368 return list; | |
| 369 } | |
| 370 case em::GenericValue::VALUE_TYPE_STRING_ARRAY: { | |
| 371 ListValue* list = new ListValue; | |
| 372 RepeatedPtrField<std::string>::const_iterator i; | |
| 373 for (i = value.string_array().begin(); | |
| 374 i != value.string_array().end(); ++i) | |
| 375 list->Append(Value::CreateStringValue(*i)); | |
| 376 return list; | |
| 377 } | |
| 378 case em::GenericValue::VALUE_TYPE_DOUBLE_ARRAY: { | |
| 379 ListValue* list = new ListValue; | |
| 380 RepeatedField<double>::const_iterator i; | |
| 381 for (i = value.double_array().begin(); | |
| 382 i != value.double_array().end(); ++i) | |
| 383 list->Append(Value::CreateDoubleValue(*i)); | |
| 384 return list; | |
| 385 } | |
| 386 default: | |
| 387 NOTREACHED() << "Unhandled value type"; | |
| 388 } | |
| 389 | |
| 390 return NULL; | |
| 391 } | |
| 392 | |
| 393 // static | |
| 394 DictionaryValue* CloudPolicyCache::DecodeDevicePolicy( | |
| 395 const em::DevicePolicyResponse& policy) { | |
| 396 DictionaryValue* result = new DictionaryValue; | |
| 397 RepeatedPtrField<em::DevicePolicySetting>::const_iterator setting; | |
| 398 for (setting = policy.setting().begin(); | |
| 399 setting != policy.setting().end(); | |
| 400 ++setting) { | |
| 401 // Wrong policy key? Skip. | |
| 402 if (setting->policy_key().compare(kChromeDevicePolicySettingKey) != 0) | |
| 403 continue; | |
| 404 | |
| 405 // No policy value? Skip. | |
| 406 if (!setting->has_policy_value()) | |
| 407 continue; | |
| 408 | |
| 409 // Iterate through all the name-value pairs wrapped in |setting|. | |
| 410 const em::GenericSetting& policy_value(setting->policy_value()); | |
| 411 RepeatedPtrField<em::GenericNamedValue>::const_iterator named_value; | |
| 412 for (named_value = policy_value.named_value().begin(); | |
| 413 named_value != policy_value.named_value().end(); | |
| 414 ++named_value) { | |
| 415 if (named_value->has_value()) { | |
| 416 Value* decoded_value = | |
| 417 CloudPolicyCache::DecodeValue(named_value->value()); | |
| 418 if (decoded_value) | |
| 419 result->Set(named_value->name(), decoded_value); | |
| 420 } | |
| 421 } | |
| 422 } | |
| 423 return result; | |
| 424 } | |
| 425 | |
| 426 } // namespace policy | |
| OLD | NEW |