| 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.h" | 5 #include "chrome/browser/policy/user_cloud_policy_store.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" |
| 8 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 9 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 10 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 11 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/signin/signin_manager.h" | 13 #include "chrome/browser/signin/signin_manager.h" |
| 10 #include "chrome/browser/signin/signin_manager_factory.h" | 14 #include "chrome/browser/signin/signin_manager_factory.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 11 | 16 |
| 12 using enterprise_management::PolicyData; | 17 namespace em = enterprise_management; |
| 13 | 18 |
| 14 namespace policy { | 19 namespace policy { |
| 15 | 20 |
| 16 UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile) | 21 enum PolicyLoadStatus { |
| 22 // Policy blob was successfully loaded and parsed. |
| 23 LOAD_RESULT_SUCCESS, |
| 24 |
| 25 // No previously stored policy was found. |
| 26 LOAD_RESULT_NO_POLICY_FILE, |
| 27 |
| 28 // Could not load the previously stored policy due to either a parse or |
| 29 // file read error. |
| 30 LOAD_RESULT_LOAD_ERROR, |
| 31 }; |
| 32 |
| 33 // Struct containing the result of a policy load - if |status| == |
| 34 // LOAD_RESULT_SUCCESS, |policy| is initialized from the policy file on disk. |
| 35 struct PolicyLoadResult { |
| 36 PolicyLoadStatus status; |
| 37 em::PolicyFetchResponse policy; |
| 38 }; |
| 39 |
| 40 namespace { |
| 41 |
| 42 // Subdirectory in the user's profile for storing user policies. |
| 43 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Policy"); |
| 44 // File in the above directory for storing user policy data. |
| 45 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("User Policy"); |
| 46 |
| 47 // Loads policy from the backing file (must be called via a task on |
| 48 // the FILE thread). Returns a PolicyLoadStruct with the results of the fetch. |
| 49 policy::PolicyLoadResult LoadPolicyFromDiskOnFileThread(const FilePath& path) { |
| 50 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 51 policy::PolicyLoadResult result; |
| 52 // If the backing file does not exist, just return. |
| 53 if (!file_util::PathExists(path)) { |
| 54 result.status = policy::LOAD_RESULT_NO_POLICY_FILE; |
| 55 return result; |
| 56 } |
| 57 std::string data; |
| 58 if (!file_util::ReadFileToString(path, &data) || |
| 59 !result.policy.ParseFromArray(data.c_str(), data.size())) { |
| 60 LOG(WARNING) << "Failed to read or parse policy data from " << path.value(); |
| 61 result.status = policy::LOAD_RESULT_LOAD_ERROR; |
| 62 return result; |
| 63 } |
| 64 |
| 65 result.status = policy::LOAD_RESULT_SUCCESS; |
| 66 return result; |
| 67 } |
| 68 |
| 69 // Stores policy to the backing file (must be called via a task on |
| 70 // the FILE thread). |
| 71 void StorePolicyToDiskOnFileThread(const FilePath& path, |
| 72 const em::PolicyFetchResponse& policy) { |
| 73 DVLOG(1) << "Storing policy to " << path.value(); |
| 74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 75 std::string data; |
| 76 if (!policy.SerializeToString(&data)) { |
| 77 DLOG(WARNING) << "Failed to serialize policy data"; |
| 78 return; |
| 79 } |
| 80 |
| 81 if (!file_util::CreateDirectory(path.DirName())) { |
| 82 DLOG(WARNING) << "Failed to create directory " << path.DirName().value(); |
| 83 return; |
| 84 } |
| 85 |
| 86 int size = data.size(); |
| 87 if (file_util::WriteFile(path, data.c_str(), size) != size) { |
| 88 DLOG(WARNING) << "Failed to write " << path.value(); |
| 89 } |
| 90 } |
| 91 |
| 92 } // namespace |
| 93 |
| 94 UserCloudPolicyStore::UserCloudPolicyStore(Profile* profile, |
| 95 const FilePath& path) |
| 17 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 96 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 18 profile_(profile) { | 97 profile_(profile), |
| 98 backing_file_path_(path) { |
| 19 } | 99 } |
| 20 | 100 |
| 21 UserCloudPolicyStore::~UserCloudPolicyStore() { | 101 UserCloudPolicyStore::~UserCloudPolicyStore() { |
| 22 } | 102 } |
| 23 | 103 |
| 24 void UserCloudPolicyStore::Load() { | 104 void UserCloudPolicyStore::Load() { |
| 25 // TODO(atwilson): Read policy from file. | 105 DVLOG(1) << "Initiating policy load from disk"; |
| 26 policy_.reset(); | 106 // Cancel any pending Load/Store/Validate operations. |
| 27 policy_map_.Clear(); | 107 weak_factory_.InvalidateWeakPtrs(); |
| 108 |
| 109 // Start a new Load operation and have us get called back when it is |
| 110 // complete. |
| 111 content::BrowserThread::PostTaskAndReplyWithResult( |
| 112 content::BrowserThread::FILE, FROM_HERE, |
| 113 base::Bind(&LoadPolicyFromDiskOnFileThread, backing_file_path_), |
| 114 base::Bind(&UserCloudPolicyStore::PolicyLoaded, |
| 115 weak_factory_.GetWeakPtr())); |
| 116 } |
| 117 |
| 118 void UserCloudPolicyStore::PolicyLoaded(PolicyLoadResult result) { |
| 119 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 120 switch (result.status) { |
| 121 case LOAD_RESULT_LOAD_ERROR: |
| 122 status_ = STATUS_LOAD_ERROR; |
| 123 NotifyStoreError(); |
| 124 break; |
| 125 |
| 126 case LOAD_RESULT_NO_POLICY_FILE: |
| 127 DVLOG(1) << "No policy found on disk"; |
| 128 NotifyStoreLoaded(); |
| 129 break; |
| 130 |
| 131 case LOAD_RESULT_SUCCESS: { |
| 132 // Found policy on disk - need to validate it before it can be used. |
| 133 scoped_ptr<em::PolicyFetchResponse> cloud_policy( |
| 134 new em::PolicyFetchResponse(result.policy)); |
| 135 Validate(cloud_policy.Pass(), |
| 136 base::Bind( |
| 137 &UserCloudPolicyStore::InstallLoadedPolicyAfterValidation, |
| 138 weak_factory_.GetWeakPtr())); |
| 139 break; |
| 140 } |
| 141 default: |
| 142 NOTREACHED(); |
| 143 } |
| 144 } |
| 145 |
| 146 void UserCloudPolicyStore::InstallLoadedPolicyAfterValidation( |
| 147 UserCloudPolicyValidator* validator) { |
| 148 validation_status_ = validator->status(); |
| 149 if (!validator->success()) { |
| 150 DVLOG(1) << "Validation failed: status=" << validation_status_; |
| 151 status_ = STATUS_VALIDATION_ERROR; |
| 152 NotifyStoreError(); |
| 153 return; |
| 154 } |
| 155 |
| 156 DVLOG(1) << "Validation succeeded - installing policy with dm_token: " << |
| 157 validator->policy_data()->request_token(); |
| 158 DVLOG(1) << "Device ID: " << validator->policy_data()->device_id(); |
| 159 |
| 160 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); |
| 161 status_ = STATUS_OK; |
| 28 NotifyStoreLoaded(); | 162 NotifyStoreLoaded(); |
| 29 } | 163 } |
| 30 | 164 |
| 31 void UserCloudPolicyStore::RemoveStoredPolicy() { | 165 void UserCloudPolicyStore::RemoveStoredPolicy() { |
| 32 // TODO(atwilson): Remove policy from disk. | 166 content::BrowserThread::PostTask( |
| 167 content::BrowserThread::FILE, FROM_HERE, |
| 168 base::Bind(base::IgnoreResult(&file_util::Delete), |
| 169 backing_file_path_, |
| 170 false)); |
| 33 } | 171 } |
| 34 | 172 |
| 35 void UserCloudPolicyStore::Store( | 173 void UserCloudPolicyStore::Store(const em::PolicyFetchResponse& policy) { |
| 36 const enterprise_management::PolicyFetchResponse& policy) { | |
| 37 // Stop any pending requests to store policy, then validate the new policy | 174 // Stop any pending requests to store policy, then validate the new policy |
| 38 // before storing it. | 175 // before storing it. |
| 39 weak_factory_.InvalidateWeakPtrs(); | 176 weak_factory_.InvalidateWeakPtrs(); |
| 40 scoped_ptr<enterprise_management::PolicyFetchResponse> policy_copy( | 177 scoped_ptr<em::PolicyFetchResponse> policy_copy( |
| 41 new enterprise_management::PolicyFetchResponse(policy)); | 178 new em::PolicyFetchResponse(policy)); |
| 42 Validate(policy_copy.Pass(), | 179 Validate(policy_copy.Pass(), |
| 43 base::Bind(&UserCloudPolicyStore::StorePolicyAfterValidation, | 180 base::Bind(&UserCloudPolicyStore::StorePolicyAfterValidation, |
| 44 weak_factory_.GetWeakPtr())); | 181 weak_factory_.GetWeakPtr())); |
| 45 } | 182 } |
| 46 | 183 |
| 47 void UserCloudPolicyStore::Validate( | 184 void UserCloudPolicyStore::Validate( |
| 48 scoped_ptr<enterprise_management::PolicyFetchResponse> policy, | 185 scoped_ptr<em::PolicyFetchResponse> policy, |
| 49 const UserCloudPolicyValidator::CompletionCallback& callback) { | 186 const UserCloudPolicyValidator::CompletionCallback& callback) { |
| 50 // Configure the validator. | 187 // Configure the validator. |
| 51 scoped_ptr<UserCloudPolicyValidator> validator = | 188 scoped_ptr<UserCloudPolicyValidator> validator = |
| 52 CreateValidator(policy.Pass(), callback); | 189 CreateValidator(policy.Pass(), callback); |
| 53 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); | 190 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); |
| 54 std::string username = signin->GetAuthenticatedUsername(); | 191 std::string username = signin->GetAuthenticatedUsername(); |
| 55 DCHECK(!username.empty()); | 192 DCHECK(!username.empty()); |
| 56 validator->ValidateUsername(username); | 193 validator->ValidateUsername(username); |
| 57 | 194 |
| 58 // Start validation. The Validator will free itself once validation is | 195 // Start validation. The Validator will free itself once validation is |
| 59 // complete. | 196 // complete. |
| 60 validator.release()->StartValidation(); | 197 validator.release()->StartValidation(); |
| 61 } | 198 } |
| 62 | 199 |
| 63 void UserCloudPolicyStore::StorePolicyAfterValidation( | 200 void UserCloudPolicyStore::StorePolicyAfterValidation( |
| 64 UserCloudPolicyValidator* validator) { | 201 UserCloudPolicyValidator* validator) { |
| 65 validation_status_ = validator->status(); | 202 validation_status_ = validator->status(); |
| 203 DVLOG(1) << "Policy validation complete: status = " << validation_status_; |
| 66 if (!validator->success()) { | 204 if (!validator->success()) { |
| 67 status_ = STATUS_VALIDATION_ERROR; | 205 status_ = STATUS_VALIDATION_ERROR; |
| 68 NotifyStoreError(); | 206 NotifyStoreError(); |
| 69 return; | 207 return; |
| 70 } | 208 } |
| 71 // TODO(atwilson): Write policy to file. | 209 |
| 210 // Persist the validated policy (just fire a task - don't bother getting a |
| 211 // reply because we can't do anything if it fails). |
| 212 content::BrowserThread::PostTask( |
| 213 content::BrowserThread::FILE, FROM_HERE, |
| 214 base::Bind(&StorePolicyToDiskOnFileThread, |
| 215 backing_file_path_, *validator->policy())); |
| 72 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); | 216 InstallPolicy(validator->policy_data().Pass(), validator->payload().Pass()); |
| 73 status_ = STATUS_OK; | 217 status_ = STATUS_OK; |
| 74 NotifyStoreLoaded(); | 218 NotifyStoreLoaded(); |
| 75 } | 219 } |
| 76 | 220 |
| 77 // static | 221 // static |
| 78 scoped_ptr<CloudPolicyStore> CloudPolicyStore::CreateUserPolicyStore( | 222 scoped_ptr<CloudPolicyStore> CloudPolicyStore::CreateUserPolicyStore( |
| 79 Profile* profile) { | 223 Profile* profile) { |
| 80 return scoped_ptr<CloudPolicyStore>(new UserCloudPolicyStore(profile)); | 224 FilePath path = |
| 225 profile->GetPath().Append(kPolicyDir).Append(kPolicyCacheFile); |
| 226 return scoped_ptr<CloudPolicyStore>(new UserCloudPolicyStore(profile, path)); |
| 81 } | 227 } |
| 82 | 228 |
| 83 } // namespace policy | 229 } // namespace policy |
| OLD | NEW |