OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chromeos/dbus/fake_auth_policy_client.h" | 5 #include "chromeos/dbus/fake_auth_policy_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/md5.h" | 11 #include "base/md5.h" |
12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
14 #include "base/threading/worker_pool.h" | 14 #include "base/threading/worker_pool.h" |
15 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | 15 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
16 #include "chromeos/chromeos_paths.h" | 16 #include "chromeos/chromeos_paths.h" |
| 17 #include "chromeos/cryptohome/cryptohome_parameters.h" |
| 18 #include "chromeos/dbus/cryptohome_client.h" |
| 19 #include "components/policy/proto/cloud_policy.pb.h" |
17 #include "components/policy/proto/device_management_backend.pb.h" | 20 #include "components/policy/proto/device_management_backend.pb.h" |
| 21 #include "components/signin/core/account_id/account_id.h" |
18 | 22 |
19 namespace em = enterprise_management; | 23 namespace em = enterprise_management; |
20 | 24 |
21 namespace { | 25 namespace { |
22 | 26 |
23 // Create minimal stub device policy file and drop it at the place where | 27 // Drop stub policy file of |policy_type| at |policy_path| containing |
24 // SessionManagerClientStubImpl is looking for it. | 28 // |serialized_payload|. |
25 bool WriteDevicePolicyFile() { | 29 bool WritePolicyFile(const base::FilePath& policy_path, |
26 em::ChromeDeviceSettingsProto policy; | 30 const std::string& serialized_payload, |
| 31 const std::string& policy_type) { |
27 em::PolicyData data; | 32 em::PolicyData data; |
28 policy.SerializeToString(data.mutable_policy_value()); | 33 data.set_policy_value(serialized_payload); |
29 data.set_policy_type("google/chromeos/device"); | 34 data.set_policy_type(policy_type); |
30 | 35 |
31 em::PolicyFetchResponse response; | 36 em::PolicyFetchResponse response; |
32 data.SerializeToString(response.mutable_policy_data()); | 37 CHECK(data.SerializeToString(response.mutable_policy_data())); |
33 std::string serialized_response; | 38 std::string serialized_response; |
34 response.SerializeToString(&serialized_response); | 39 CHECK(response.SerializeToString(&serialized_response)); |
35 | 40 |
36 base::FilePath owner_key_path; | 41 if (!base::CreateDirectory(policy_path.DirName())) |
37 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path)) | |
38 return false; | 42 return false; |
39 | 43 |
40 const base::FilePath device_policy_path = | |
41 owner_key_path.DirName().AppendASCII("stub_device_policy"); | |
42 | |
43 // Note that in theory there could be a short time window in which a | 44 // Note that in theory there could be a short time window in which a |
44 // concurrent reader sees a partial (and thus invalid) file, but given the | 45 // concurrent reader sees a partial (and thus invalid) file, but given the |
45 // small file size that seems very unlikely in practice. | 46 // small file size that seems very unlikely in practice. |
46 const int bytes_written = | 47 const int bytes_written = base::WriteFile( |
47 base::WriteFile(device_policy_path, serialized_response.c_str(), | 48 policy_path, serialized_response.c_str(), serialized_response.size()); |
48 serialized_response.size()); | |
49 if (bytes_written < 0) | 49 if (bytes_written < 0) |
50 return false; | 50 return false; |
51 return bytes_written == static_cast<int>(serialized_response.size()); | 51 return bytes_written == static_cast<int>(serialized_response.size()); |
52 } | 52 } |
53 | 53 |
54 } // namespace | 54 } // namespace |
55 | 55 |
56 namespace chromeos { | 56 namespace chromeos { |
57 | 57 |
58 FakeAuthPolicyClient::FakeAuthPolicyClient() {} | 58 FakeAuthPolicyClient::FakeAuthPolicyClient() {} |
(...skipping 12 matching lines...) Expand all Loading... |
71 void FakeAuthPolicyClient::AuthenticateUser( | 71 void FakeAuthPolicyClient::AuthenticateUser( |
72 const std::string& user_principal_name, | 72 const std::string& user_principal_name, |
73 int password_fd, | 73 int password_fd, |
74 const AuthCallback& callback) { | 74 const AuthCallback& callback) { |
75 callback.Run(authpolicy::AUTH_USER_ERROR_NONE, | 75 callback.Run(authpolicy::AUTH_USER_ERROR_NONE, |
76 base::MD5String(user_principal_name)); | 76 base::MD5String(user_principal_name)); |
77 } | 77 } |
78 | 78 |
79 void FakeAuthPolicyClient::RefreshDevicePolicy( | 79 void FakeAuthPolicyClient::RefreshDevicePolicy( |
80 const RefreshPolicyCallback& callback) { | 80 const RefreshPolicyCallback& callback) { |
| 81 base::FilePath policy_path; |
| 82 if (!PathService::Get(chromeos::FILE_OWNER_KEY, &policy_path)) { |
| 83 callback.Run(false); |
| 84 return; |
| 85 } |
| 86 policy_path = policy_path.DirName().AppendASCII("stub_device_policy"); |
| 87 |
| 88 em::ChromeDeviceSettingsProto policy; |
| 89 std::string payload; |
| 90 CHECK(policy.SerializeToString(&payload)); |
| 91 |
| 92 // Drop file for SessionManagerClientStubImpl to read. |
81 if (!base::PostTaskAndReplyWithResult( | 93 if (!base::PostTaskAndReplyWithResult( |
82 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), | 94 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), |
83 FROM_HERE, base::Bind(&WriteDevicePolicyFile), callback)) { | 95 FROM_HERE, base::Bind(&WritePolicyFile, policy_path, payload, |
| 96 "google/chromeos/device"), |
| 97 callback)) { |
84 callback.Run(false); | 98 callback.Run(false); |
85 } | 99 } |
86 } | 100 } |
87 | 101 |
88 void FakeAuthPolicyClient::RefreshUserPolicy( | 102 void FakeAuthPolicyClient::RefreshUserPolicy( |
89 const std::string& account_id, | 103 const AccountId& account_id, |
90 const RefreshPolicyCallback& callback) { | 104 const RefreshPolicyCallback& callback) { |
91 callback.Run(true); | 105 base::FilePath policy_path; |
| 106 if (!PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &policy_path)) { |
| 107 callback.Run(false); |
| 108 return; |
| 109 } |
| 110 const cryptohome::Identification cryptohome_identification(account_id); |
| 111 const std::string sanitized_username = |
| 112 chromeos::CryptohomeClient::GetStubSanitizedUsername( |
| 113 cryptohome_identification); |
| 114 policy_path = policy_path.AppendASCII(sanitized_username); |
| 115 policy_path = policy_path.AppendASCII("stub_policy"); |
| 116 |
| 117 em::CloudPolicySettings policy; |
| 118 std::string payload; |
| 119 CHECK(policy.SerializeToString(&payload)); |
| 120 |
| 121 // Drop file for SessionManagerClientStubImpl to read. |
| 122 if (!base::PostTaskAndReplyWithResult( |
| 123 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), |
| 124 FROM_HERE, base::Bind(&WritePolicyFile, policy_path, payload, |
| 125 "google/chromeos/user"), |
| 126 callback)) { |
| 127 callback.Run(false); |
| 128 } |
92 } | 129 } |
93 | 130 |
94 } // namespace chromeos | 131 } // namespace chromeos |
OLD | NEW |