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_); | |
|
danno
2011/02/11 11:00:42
Why do you only set the time stamp in the device p
Jakob Kummerow
2011/02/14 16:14:26
Because new-style cloud policy contains its own ti
| |
| 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()) { | |
|
danno
2011/02/11 11:00:42
assert !has_device_policy()?
Jakob Kummerow
2011/02/14 16:14:26
Not necessary. With the current implementation, de
| |
| 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( | |
|
danno
2011/02/11 11:00:42
assert !has_cloud_policy()?
Jakob Kummerow
2011/02/14 16:14:26
Not necessary: we're in the "else" clause of preci
| |
| 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 em::CloudPolicyResponse* policy_copy = new em::CloudPolicyResponse; | |
| 182 policy_copy->CopyFrom(policy); | |
| 183 BrowserThread::PostTask( | |
| 184 BrowserThread::FILE, | |
| 185 FROM_HERE, | |
| 186 new PersistPolicyTask(backing_file_path_, policy_copy, NULL, false)); | |
| 187 return new_policy_differs; | |
| 188 } | |
| 189 | |
| 190 bool CloudPolicyCache::SetDevicePolicy(const em::DevicePolicyResponse& policy) { | |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 192 is_unmanaged_ = false; | |
| 193 DictionaryValue* value = DecodeDevicePolicy(policy); | |
| 194 const bool new_policy_differs = !(value->Equals(device_policy_.get())); | |
| 195 base::Time now(base::Time::NowFromSystemTime()); | |
| 196 { | |
| 197 base::AutoLock lock(lock_); | |
| 198 device_policy_.reset(value); | |
| 199 fresh_policy_ = true; | |
| 200 last_policy_refresh_time_ = now; | |
| 201 has_device_policy_ = true; | |
| 202 } | |
| 203 | |
| 204 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse; | |
| 205 policy_copy->CopyFrom(policy); | |
| 206 BrowserThread::PostTask( | |
| 207 BrowserThread::FILE, | |
| 208 FROM_HERE, | |
| 209 new PersistPolicyTask(backing_file_path_, NULL, policy_copy, false)); | |
| 210 return new_policy_differs; | |
| 211 } | |
| 212 | |
| 213 DictionaryValue* CloudPolicyCache::GetDevicePolicy() { | |
| 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 215 base::AutoLock lock(lock_); | |
| 216 return device_policy_->DeepCopy(); | |
| 217 } | |
| 218 | |
| 219 const CloudPolicyCache::PolicyMapType* | |
| 220 CloudPolicyCache::GetMandatoryPolicy() const { | |
| 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 222 return &mandatory_policy_; | |
| 223 } | |
| 224 | |
| 225 const CloudPolicyCache::PolicyMapType* | |
| 226 CloudPolicyCache::GetRecommendedPolicy() const { | |
| 227 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 228 return &recommended_policy_; | |
| 229 } | |
| 230 | |
| 231 void CloudPolicyCache::SetUnmanaged() { | |
| 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 233 is_unmanaged_ = true; | |
| 234 { | |
| 235 base::AutoLock lock(lock_); | |
| 236 mandatory_policy_.clear(); | |
| 237 recommended_policy_.clear(); | |
| 238 device_policy_.reset(new DictionaryValue); | |
| 239 last_policy_refresh_time_ = base::Time::NowFromSystemTime(); | |
| 240 } | |
| 241 BrowserThread::PostTask( | |
| 242 BrowserThread::FILE, | |
| 243 FROM_HERE, | |
| 244 new PersistPolicyTask(backing_file_path_, NULL, NULL, true)); | |
| 245 } | |
| 246 | |
| 247 // static | |
| 248 bool CloudPolicyCache::DecodePolicyResponse( | |
| 249 const em::CloudPolicyResponse& policy_response, | |
| 250 PolicyMapType* mandatory, | |
| 251 PolicyMapType* recommended, | |
| 252 base::Time* timestamp) { | |
| 253 std::string data = policy_response.signed_response(); | |
| 254 | |
| 255 if (!VerifySignature(policy_response.signature(), data, | |
| 256 policy_response.certificate_chain())) { | |
| 257 LOG(WARNING) << "Failed to verify signature."; | |
| 258 return false; | |
| 259 } | |
| 260 | |
| 261 em::SignedCloudPolicyResponse response; | |
| 262 if (!response.ParseFromArray(data.c_str(), data.size())) { | |
| 263 LOG(WARNING) << "Failed to parse SignedCloudPolicyResponse protobuf."; | |
| 264 return false; | |
| 265 } | |
| 266 | |
| 267 // TODO(jkummerow): Verify response.device_token(). Needs final specification | |
| 268 // which token we're actually sending / expecting to get back. | |
| 269 | |
| 270 // TODO(jkummerow): Store response.device_name(), if we decide to transfer | |
| 271 // it from the server to the client. | |
| 272 | |
| 273 DCHECK(timestamp); | |
| 274 *timestamp = base::Time::FromTimeT(response.timestamp()); | |
| 275 DecodePolicy(response.settings(), mandatory, recommended); | |
| 276 return true; | |
| 277 } | |
| 278 | |
| 279 // static | |
| 280 bool CloudPolicyCache::VerifySignature( | |
| 281 const std::string& signature, | |
| 282 const std::string& data, | |
| 283 const RepeatedPtrField<std::string>& certificate_chain) { | |
| 284 // TODO(jkummerow): Implement this. Non-trivial because we want to do it | |
| 285 // for all platforms -> it's enough work to deserve its own CL. | |
| 286 // Don't forget to also verify the hostname of the server against the cert. | |
| 287 return true; | |
| 288 } | |
| 289 | |
| 290 // static | |
| 291 bool CloudPolicyCache::Equals(const PolicyMapType& a, const PolicyMapType& b) { | |
| 292 for (PolicyMapType::const_iterator it = a.begin(); it != a.end(); ++it) { | |
| 293 PolicyMapType::const_iterator is_in_b = b.find(it->first); | |
| 294 if (is_in_b == b.end()) | |
| 295 return false; | |
| 296 if (!it->second->Equals(is_in_b->second)) | |
| 297 return false; | |
| 298 } | |
| 299 for (PolicyMapType::const_iterator it = b.begin(); it != b.end(); ++it) { | |
| 300 if (a.find(it->first) == a.end()) | |
| 301 return false; | |
| 302 } | |
| 303 return true; | |
| 304 } | |
| 305 | |
| 306 // static | |
| 307 Value* CloudPolicyCache::DecodeIntegerValue(google::protobuf::int64 value) { | |
| 308 if (value < std::numeric_limits<int>::min() || | |
| 309 value > std::numeric_limits<int>::max()) { | |
| 310 LOG(WARNING) << "Integer value " << value | |
| 311 << " out of numeric limits, ignoring."; | |
| 312 return NULL; | |
| 313 } | |
| 314 | |
| 315 return Value::CreateIntegerValue(static_cast<int>(value)); | |
| 316 } | |
| 317 | |
| 318 // static | |
| 319 Value* CloudPolicyCache::DecodeValue(const em::GenericValue& value) { | |
| 320 if (!value.has_value_type()) | |
| 321 return NULL; | |
| 322 | |
| 323 switch (value.value_type()) { | |
| 324 case em::GenericValue::VALUE_TYPE_BOOL: | |
| 325 if (value.has_bool_value()) | |
| 326 return Value::CreateBooleanValue(value.bool_value()); | |
| 327 return NULL; | |
| 328 case em::GenericValue::VALUE_TYPE_INT64: | |
| 329 if (value.has_int64_value()) | |
| 330 return DecodeIntegerValue(value.int64_value()); | |
| 331 return NULL; | |
| 332 case em::GenericValue::VALUE_TYPE_STRING: | |
| 333 if (value.has_string_value()) | |
| 334 return Value::CreateStringValue(value.string_value()); | |
| 335 return NULL; | |
| 336 case em::GenericValue::VALUE_TYPE_DOUBLE: | |
| 337 if (value.has_double_value()) | |
| 338 return Value::CreateDoubleValue(value.double_value()); | |
| 339 return NULL; | |
| 340 case em::GenericValue::VALUE_TYPE_BYTES: | |
| 341 if (value.has_bytes_value()) { | |
| 342 std::string bytes = value.bytes_value(); | |
| 343 return BinaryValue::CreateWithCopiedBuffer(bytes.c_str(), bytes.size()); | |
| 344 } | |
| 345 return NULL; | |
| 346 case em::GenericValue::VALUE_TYPE_BOOL_ARRAY: { | |
| 347 ListValue* list = new ListValue; | |
| 348 RepeatedField<bool>::const_iterator i; | |
| 349 for (i = value.bool_array().begin(); i != value.bool_array().end(); ++i) | |
| 350 list->Append(Value::CreateBooleanValue(*i)); | |
| 351 return list; | |
| 352 } | |
| 353 case em::GenericValue::VALUE_TYPE_INT64_ARRAY: { | |
| 354 ListValue* list = new ListValue; | |
| 355 RepeatedField<google::protobuf::int64>::const_iterator i; | |
| 356 for (i = value.int64_array().begin(); | |
| 357 i != value.int64_array().end(); ++i) { | |
| 358 Value* int_value = DecodeIntegerValue(*i); | |
| 359 if (int_value) | |
| 360 list->Append(int_value); | |
| 361 } | |
| 362 return list; | |
| 363 } | |
| 364 case em::GenericValue::VALUE_TYPE_STRING_ARRAY: { | |
| 365 ListValue* list = new ListValue; | |
| 366 RepeatedPtrField<std::string>::const_iterator i; | |
| 367 for (i = value.string_array().begin(); | |
| 368 i != value.string_array().end(); ++i) | |
| 369 list->Append(Value::CreateStringValue(*i)); | |
| 370 return list; | |
| 371 } | |
| 372 case em::GenericValue::VALUE_TYPE_DOUBLE_ARRAY: { | |
| 373 ListValue* list = new ListValue; | |
| 374 RepeatedField<double>::const_iterator i; | |
| 375 for (i = value.double_array().begin(); | |
| 376 i != value.double_array().end(); ++i) | |
| 377 list->Append(Value::CreateDoubleValue(*i)); | |
| 378 return list; | |
| 379 } | |
| 380 default: | |
| 381 NOTREACHED() << "Unhandled value type"; | |
| 382 } | |
| 383 | |
| 384 return NULL; | |
| 385 } | |
| 386 | |
| 387 // static | |
| 388 DictionaryValue* CloudPolicyCache::DecodeDevicePolicy( | |
| 389 const em::DevicePolicyResponse& policy) { | |
| 390 DictionaryValue* result = new DictionaryValue; | |
| 391 RepeatedPtrField<em::DevicePolicySetting>::const_iterator setting; | |
| 392 for (setting = policy.setting().begin(); | |
| 393 setting != policy.setting().end(); | |
| 394 ++setting) { | |
| 395 // Wrong policy key? Skip. | |
| 396 if (setting->policy_key().compare(kChromeDevicePolicySettingKey) != 0) | |
| 397 continue; | |
| 398 | |
| 399 // No policy value? Skip. | |
| 400 if (!setting->has_policy_value()) | |
| 401 continue; | |
| 402 | |
| 403 // Iterate through all the name-value pairs wrapped in |setting|. | |
| 404 const em::GenericSetting& policy_value(setting->policy_value()); | |
| 405 RepeatedPtrField<em::GenericNamedValue>::const_iterator named_value; | |
| 406 for (named_value = policy_value.named_value().begin(); | |
| 407 named_value != policy_value.named_value().end(); | |
| 408 ++named_value) { | |
| 409 if (named_value->has_value()) { | |
| 410 Value* decoded_value = | |
| 411 CloudPolicyCache::DecodeValue(named_value->value()); | |
| 412 if (decoded_value) | |
| 413 result->Set(named_value->name(), decoded_value); | |
| 414 } | |
| 415 } | |
| 416 } | |
| 417 return result; | |
| 418 } | |
| 419 | |
| 420 } // namespace policy | |
| OLD | NEW |