Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4760)

Unified Diff: chrome/browser/policy/cloud_policy_cache.cc

Issue 6409040: New policy protobuf protocol. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ready for review! Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/cloud_policy_cache.cc
diff --git a/chrome/browser/policy/cloud_policy_cache.cc b/chrome/browser/policy/cloud_policy_cache.cc
new file mode 100644
index 0000000000000000000000000000000000000000..dc7a03c3eff0f4fafbd044d73b22e9a5de75ecfd
--- /dev/null
+++ b/chrome/browser/policy/cloud_policy_cache.cc
@@ -0,0 +1,244 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/policy/cloud_policy_cache.h"
+
+#include <limits>
+
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/task.h"
+#include "base/values.h"
+#include "chrome/browser/browser_thread.h"
+#include "chrome/browser/policy/proto/cloud_policy.pb.h"
+#include "chrome/browser/policy/proto/device_management_backend.pb.h"
+#include "chrome/browser/policy/proto/device_management_constants.h"
+#include "chrome/browser/policy/proto/device_management_local.pb.h"
+// configuration_policy_type.h is generated. See policy_templates.json for
+// policy definitions.
+#include "policy/configuration_policy_type.h"
+
+using google::protobuf::RepeatedPtrField;
+
+namespace policy {
+
+// Saves policy information to a file.
+class PersistCloudPolicyTask : public Task {
+ public:
+ PersistCloudPolicyTask(const FilePath& path,
+ const em::CloudPolicyResponse* policy_response,
+ const bool is_unmanaged)
+ : path_(path),
+ policy_response_(policy_response),
+ is_unmanaged_(is_unmanaged) {}
+
+ private:
+ // Task override.
+ virtual void Run();
+
+ const FilePath path_;
+ scoped_ptr<const em::CloudPolicyResponse> policy_response_;
+ const bool is_unmanaged_;
+};
+
+void PersistCloudPolicyTask::Run() {
+ std::string data;
+ em::CachedCloudPolicyResponse cached_policy;
+ if (policy_response_.get())
+ cached_policy.mutable_policy_response()->CopyFrom(*policy_response_);
+ if (is_unmanaged_) {
+ cached_policy.set_unmanaged(true);
+ cached_policy.set_timestamp(
+ base::Time::NowFromSystemTime().ToTimeT());
+ }
+ if (!cached_policy.SerializeToString(&data)) {
+ LOG(WARNING) << "Failed to serialize policy data";
+ return;
+ }
+
+ int size = data.size();
+ if (file_util::WriteFile(path_, data.c_str(), size) != size) {
+ LOG(WARNING) << "Failed to write " << path_.value();
+ return;
+ }
+}
+
+CloudPolicyCache::CloudPolicyCache(
+ const FilePath& backing_file_path)
+ : backing_file_path_(backing_file_path),
+ mandatory_policy_(new PolicyMapType),
+ recommended_policy_(new PolicyMapType),
+ fresh_policy_(false),
+ is_unmanaged_(false) {
+}
+
+CloudPolicyCache::~CloudPolicyCache() {}
+
+void CloudPolicyCache::LoadPolicyFromFile() {
+ if (!file_util::PathExists(backing_file_path_) || fresh_policy_) {
+ return;
+ }
+
+ // Read the protobuf from the file.
+ std::string data;
+ if (!file_util::ReadFileToString(backing_file_path_, &data)) {
+ LOG(WARNING) << "Failed to read policy data from "
+ << backing_file_path_.value();
+ return;
+ }
+
+ em::CachedCloudPolicyResponse cached_response;
+ if (!cached_response.ParseFromArray(data.c_str(), data.size())) {
+ LOG(WARNING) << "Failed to parse policy data read from "
+ << backing_file_path_.value();
+ return;
+ }
+ base::Time timestamp;
+ is_unmanaged_ = cached_response.unmanaged();
+ // Decode and swap in the new policy information.
+ scoped_ptr<PolicyMapType> mandatory_policy_map(new PolicyMapType);
+ scoped_ptr<PolicyMapType> recommended_policy_map(new PolicyMapType);
+ bool ok = DecodePolicyResponse(cached_response.policy_response(),
+ mandatory_policy_map.get(),
+ recommended_policy_map.get(),
+ &timestamp);
+ if (is_unmanaged_) {
+ timestamp = base::Time::FromTimeT(cached_response.timestamp());
+ } else {
+ if (!ok) {
+ LOG(WARNING) << "Decoding policy data failed.";
+ return;
+ }
+ }
+ {
+ base::AutoLock lock(lock_);
+ if (!fresh_policy_)
+ mandatory_policy_.reset(mandatory_policy_map.release());
+ recommended_policy_.reset(recommended_policy_map.release());
+ last_policy_refresh_time_ = timestamp;
+ }
+}
+
+bool CloudPolicyCache::SetPolicy(
+ const em::CloudPolicyResponse& policy) {
+ is_unmanaged_ = false;
+ base::Time timestamp;
+ scoped_ptr<PolicyMapType> mandatory_policy_map(new PolicyMapType);
+ scoped_ptr<PolicyMapType> recommended_policy_map(new PolicyMapType);
+ bool ok = DecodePolicyResponse(policy, mandatory_policy_map.get(),
+ recommended_policy_map.get(), &timestamp);
+ if (!ok) {
+ // TODO(jkummerow): Signal error to PolicyProvider.
+ return false;
+ }
+ const bool new_policy_differs =
+ !Equals(mandatory_policy_map.get(), mandatory_policy_.get()) ||
+ !Equals(recommended_policy_map.get(), recommended_policy_.get());
+ {
+ base::AutoLock lock(lock_);
+ mandatory_policy_.reset(mandatory_policy_map.release());
+ recommended_policy_.reset(recommended_policy_map.release());
+ fresh_policy_ = true;
+ last_policy_refresh_time_ = timestamp;
+ }
+
+ em::CloudPolicyResponse* policy_copy = new em::CloudPolicyResponse;
+ policy_copy->CopyFrom(policy);
+ BrowserThread::PostTask(
+ BrowserThread::FILE,
+ FROM_HERE,
+ new PersistCloudPolicyTask(backing_file_path_, policy_copy, false));
+ return new_policy_differs;
+}
+
+PolicyMapType* CloudPolicyCache::GetPolicy() {
+ base::AutoLock lock(lock_);
+ PolicyMapType* policy_copy = new PolicyMapType;
+ for (PolicyMapType::iterator it = mandatory_policy_->begin();
+ it != mandatory_policy_->end(); ++it) {
gfeher 2011/02/02 08:42:45 Nit: indent.
Jakob Kummerow 2011/02/03 14:36:52 Done.
+ policy_copy->insert(std::make_pair(it->first, it->second->DeepCopy()));
+ }
+ return policy_copy;
+}
+
+void CloudPolicyCache::SetUnmanaged() {
+ is_unmanaged_ = true;
+ {
+ base::AutoLock lock(lock_);
+ mandatory_policy_.reset(new PolicyMapType);
+ recommended_policy_.reset(new PolicyMapType);
+ last_policy_refresh_time_ = base::Time::NowFromSystemTime();
+ }
+ BrowserThread::PostTask(
+ BrowserThread::FILE,
+ FROM_HERE,
+ new PersistCloudPolicyTask(backing_file_path_, NULL, true));
+}
+
+// static
+bool CloudPolicyCache::DecodePolicyResponse(
+ const em::CloudPolicyResponse& policy_response,
+ PolicyMapType* mandatory,
+ PolicyMapType* recommended,
+ base::Time* timestamp) {
+ std::string data = policy_response.signed_response();
+
+ if (!VerifySignature(policy_response.signature(), data,
+ policy_response.certificate_chain())) {
+ LOG(WARNING) << "Failed to verify signature.";
+ return false;
+ }
+
+ em::SignedCloudPolicyResponse response;
+ if (!response.ParseFromArray(data.c_str(), data.size())) {
+ LOG(WARNING) << "Failed to parse SignedCloudPolicyResponse protobuf.";
+ return false;
+ }
+
+ // TODO(jkummerow): Verify response.device_token(). Needs final specification
+ // which token we're actually sending / expecting to get back.
+
+ // TODO(jkummerow): Store response.device_name(), if we decide to transfer
+ // it from the server to the client.
+
+ // Reject policies that claim to be from the future.
+ DCHECK(timestamp);
+ *timestamp = base::Time::FromTimeT(response.timestamp());
+ if (*timestamp > base::Time::NowFromSystemTime()) {
gfeher 2011/02/02 08:42:45 Allow a small difference for safety?
Jakob Kummerow 2011/02/03 14:36:52 Done.
+ LOG(WARNING) << "Rejected policy data, timestamp is from the future.";
+ return false;
+ }
+
+ DecodePolicy(response.settings(), mandatory, recommended);
+ return true;
+}
+
+// static
+bool CloudPolicyCache::VerifySignature(
+ const std::string& signature,
+ const std::string& data,
+ const RepeatedPtrField<std::string>& certificate_chain) {
+ // TODO(jkummerow): Implement this. Non-trivial because we want to do it
+ // for all platforms -> it's enough work to deserve its own CL.
+ // Don't forget to also verify the hostname of the server against the cert.
+ return true;
+}
+
+// static
+bool CloudPolicyCache::Equals(const PolicyMapType* a, const PolicyMapType* b) {
+ for (PolicyMapType::const_iterator it = a->begin(); it != a->end(); ++it) {
+ PolicyMapType::const_iterator is_in_b = b->find(it->first);
+ if (is_in_b == b->end())
+ return false;
+ if (!it->second->Equals(is_in_b->second))
+ return false;
+ }
+ for (PolicyMapType::const_iterator it = b->begin(); it != b->end(); ++it) {
+ if (a->find(it->first) == a->end())
+ return false;
+ }
+ return true;
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698