OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/user_cloud_policy_store_chromeos.h" | 5 #include "chrome/browser/policy/user_cloud_policy_store_chromeos.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/command_line.h" | |
13 #include "base/file_util.h" | 12 #include "base/file_util.h" |
14 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
15 #include "base/path_service.h" | |
16 #include "chrome/browser/chromeos/login/user_manager.h" | 14 #include "chrome/browser/chromeos/login/user_manager.h" |
17 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 15 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
18 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 16 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
19 #include "chrome/browser/policy/user_policy_disk_cache.h" | 17 #include "chrome/browser/policy/user_policy_disk_cache.h" |
20 #include "chrome/browser/policy/user_policy_token_cache.h" | 18 #include "chrome/browser/policy/user_policy_token_cache.h" |
21 #include "chrome/common/net/gaia/gaia_auth_util.h" | 19 #include "chrome/common/net/gaia/gaia_auth_util.h" |
22 #include "chrome/common/chrome_paths.h" | |
23 #include "chrome/common/chrome_switches.h" | |
24 #include "chromeos/dbus/dbus_thread_manager.h" | |
25 #include "chromeos/dbus/session_manager_client.h" | 20 #include "chromeos/dbus/session_manager_client.h" |
26 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
27 | 22 |
28 namespace em = enterprise_management; | 23 namespace em = enterprise_management; |
29 | 24 |
30 namespace policy { | 25 namespace policy { |
31 | 26 |
32 namespace { | 27 // Decodes a CloudPolicySettings object into a policy map. The implementation is |
33 // Subdirectory in the user's profile for storing user policies. | 28 // generated code in policy/cloud_policy_generated.cc. |
34 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management"); | 29 void DecodePolicy(const em::CloudPolicySettings& policy, |
35 // File in the above directory for stroing user policy dmtokens. | 30 PolicyMap* policies); |
36 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token"); | |
37 // File in the above directory for storing user policy data. | |
38 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy"); | |
39 } // namespace | |
40 | |
41 | 31 |
42 // Helper class for loading legacy policy caches. | 32 // Helper class for loading legacy policy caches. |
43 class LegacyPolicyCacheLoader : public UserPolicyTokenCache::Delegate, | 33 class LegacyPolicyCacheLoader : public UserPolicyTokenCache::Delegate, |
44 public UserPolicyDiskCache::Delegate { | 34 public UserPolicyDiskCache::Delegate { |
45 public: | 35 public: |
46 typedef base::Callback<void(const std::string&, | 36 typedef base::Callback<void(const std::string&, |
47 const std::string&, | 37 const std::string&, |
48 CloudPolicyStore::Status, | 38 CloudPolicyStore::Status, |
49 scoped_ptr<em::PolicyFetchResponse>)> Callback; | 39 scoped_ptr<em::PolicyFetchResponse>)> Callback; |
50 | 40 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 legacy_loader_(new LegacyPolicyCacheLoader(legacy_token_cache_file, | 151 legacy_loader_(new LegacyPolicyCacheLoader(legacy_token_cache_file, |
162 legacy_policy_cache_file)), | 152 legacy_policy_cache_file)), |
163 legacy_caches_loaded_(false) {} | 153 legacy_caches_loaded_(false) {} |
164 | 154 |
165 UserCloudPolicyStoreChromeOS::~UserCloudPolicyStoreChromeOS() {} | 155 UserCloudPolicyStoreChromeOS::~UserCloudPolicyStoreChromeOS() {} |
166 | 156 |
167 void UserCloudPolicyStoreChromeOS::Store( | 157 void UserCloudPolicyStoreChromeOS::Store( |
168 const em::PolicyFetchResponse& policy) { | 158 const em::PolicyFetchResponse& policy) { |
169 // Cancel all pending requests. | 159 // Cancel all pending requests. |
170 weak_factory_.InvalidateWeakPtrs(); | 160 weak_factory_.InvalidateWeakPtrs(); |
171 Validate( | 161 Validate(scoped_ptr<em::PolicyFetchResponse>( |
172 scoped_ptr<em::PolicyFetchResponse>(new em::PolicyFetchResponse(policy)), | 162 new em::PolicyFetchResponse(policy)), |
173 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyToStoreValidated, | 163 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyToStoreValidated, |
174 weak_factory_.GetWeakPtr())); | 164 weak_factory_.GetWeakPtr())); |
175 } | 165 } |
176 | 166 |
177 void UserCloudPolicyStoreChromeOS::Load() { | 167 void UserCloudPolicyStoreChromeOS::Load() { |
178 // Cancel all pending requests. | 168 // Cancel all pending requests. |
179 weak_factory_.InvalidateWeakPtrs(); | 169 weak_factory_.InvalidateWeakPtrs(); |
180 session_manager_client_->RetrieveUserPolicy( | 170 session_manager_client_->RetrieveUserPolicy( |
181 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyRetrieved, | 171 base::Bind(&UserCloudPolicyStoreChromeOS::OnPolicyRetrieved, |
182 weak_factory_.GetWeakPtr())); | 172 weak_factory_.GetWeakPtr())); |
183 } | 173 } |
184 | 174 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 if (!success) { | 255 if (!success) { |
266 status_ = STATUS_STORE_ERROR; | 256 status_ = STATUS_STORE_ERROR; |
267 NotifyStoreError(); | 257 NotifyStoreError(); |
268 } else { | 258 } else { |
269 // TODO(mnissler): Once we do signature verifications, we'll have to reload | 259 // TODO(mnissler): Once we do signature verifications, we'll have to reload |
270 // the key at this point to account for key rotations. | 260 // the key at this point to account for key rotations. |
271 Load(); | 261 Load(); |
272 } | 262 } |
273 } | 263 } |
274 | 264 |
| 265 void UserCloudPolicyStoreChromeOS::InstallPolicy( |
| 266 scoped_ptr<em::PolicyData> policy_data, |
| 267 scoped_ptr<em::CloudPolicySettings> payload) { |
| 268 // Decode the payload. |
| 269 policy_map_.Clear(); |
| 270 DecodePolicy(*payload, &policy_map_); |
| 271 policy_ = policy_data.Pass(); |
| 272 } |
| 273 |
275 void UserCloudPolicyStoreChromeOS::Validate( | 274 void UserCloudPolicyStoreChromeOS::Validate( |
276 scoped_ptr<em::PolicyFetchResponse> policy, | 275 scoped_ptr<em::PolicyFetchResponse> policy, |
277 const UserCloudPolicyValidator::CompletionCallback& callback) { | 276 const UserCloudPolicyValidator::CompletionCallback& callback) { |
278 // Configure the validator. | 277 // Configure the validator. |
279 scoped_ptr<UserCloudPolicyValidator> validator = | 278 UserCloudPolicyValidator* validator = |
280 CreateValidator(policy.Pass(), callback); | 279 UserCloudPolicyValidator::Create(policy.Pass(), callback); |
281 validator->ValidateUsername( | 280 validator->ValidateUsername( |
282 chromeos::UserManager::Get()->GetLoggedInUser().email()); | 281 chromeos::UserManager::Get()->GetLoggedInUser().email()); |
| 282 validator->ValidatePolicyType(dm_protocol::kChromeUserPolicyType); |
| 283 validator->ValidateAgainstCurrentPolicy(policy_.get()); |
| 284 validator->ValidatePayload(); |
283 | 285 |
284 // TODO(mnissler): Do a signature check here as well. The key is stored by | 286 // TODO(mnissler): Do a signature check here as well. The key is stored by |
285 // session_manager in the root-owned cryptohome area, which is currently | 287 // session_manager in the root-owned cryptohome area, which is currently |
286 // inaccessible to Chrome though. | 288 // inaccessible to Chrome though. |
287 | 289 |
288 // Start validation. The Validator will free itself once validation is | 290 // Start validation. |
289 // complete. | 291 validator->StartValidation(); |
290 validator.release()->StartValidation(); | |
291 } | 292 } |
292 | 293 |
293 void UserCloudPolicyStoreChromeOS::OnLegacyLoadFinished( | 294 void UserCloudPolicyStoreChromeOS::OnLegacyLoadFinished( |
294 const std::string& dm_token, | 295 const std::string& dm_token, |
295 const std::string& device_id, | 296 const std::string& device_id, |
296 Status status, | 297 Status status, |
297 scoped_ptr<em::PolicyFetchResponse> policy) { | 298 scoped_ptr<em::PolicyFetchResponse> policy) { |
298 status_ = status; | 299 status_ = status; |
299 if (policy.get()) { | 300 if (policy.get()) { |
300 Validate(policy.Pass(), | 301 Validate(policy.Pass(), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 // Tell the rest of the world that the policy load completed. | 345 // Tell the rest of the world that the policy load completed. |
345 NotifyStoreLoaded(); | 346 NotifyStoreLoaded(); |
346 } | 347 } |
347 | 348 |
348 // static | 349 // static |
349 void UserCloudPolicyStoreChromeOS::RemoveLegacyCacheDir(const FilePath& dir) { | 350 void UserCloudPolicyStoreChromeOS::RemoveLegacyCacheDir(const FilePath& dir) { |
350 if (file_util::PathExists(dir) && !file_util::Delete(dir, true)) | 351 if (file_util::PathExists(dir) && !file_util::Delete(dir, true)) |
351 LOG(ERROR) << "Failed to remove cache dir " << dir.value(); | 352 LOG(ERROR) << "Failed to remove cache dir " << dir.value(); |
352 } | 353 } |
353 | 354 |
354 // static | |
355 scoped_ptr<CloudPolicyStore> CloudPolicyStore::CreateUserPolicyStore( | |
356 Profile* profile) { | |
357 FilePath profile_dir; | |
358 CHECK(PathService::Get(chrome::DIR_USER_DATA, &profile_dir)); | |
359 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
360 const FilePath policy_dir = | |
361 profile_dir | |
362 .Append(command_line->GetSwitchValuePath(switches::kLoginProfile)) | |
363 .Append(kPolicyDir); | |
364 const FilePath policy_cache_file = policy_dir.Append(kPolicyCacheFile); | |
365 const FilePath token_cache_file = policy_dir.Append(kTokenCacheFile); | |
366 | |
367 return scoped_ptr<CloudPolicyStore>(new UserCloudPolicyStoreChromeOS( | |
368 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(), | |
369 token_cache_file, policy_cache_file)); | |
370 } | |
371 | |
372 } // namespace policy | 355 } // namespace policy |
OLD | NEW |