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

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

Powered by Google App Engine
This is Rietveld 408576698