OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/enrollment_handler_chromeos.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/message_loop.h" | |
10 #include "chrome/browser/policy/cloud_policy_constants.h" | |
11 #include "chrome/browser/policy/device_cloud_policy_store_chromeos.h" | |
12 #include "chrome/browser/policy/enterprise_install_attributes.h" | |
13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
14 | |
15 namespace em = enterprise_management; | |
16 | |
17 namespace policy { | |
18 | |
19 namespace { | |
20 | |
21 // Retry for InstallAttrs initialization every 500ms. | |
22 const int kLockRetryIntervalMs = 500; | |
23 // Maximum time to retry InstallAttrs initialization before we give up. | |
24 const int kLockRetryTimeoutMs = 10 * 60 * 1000; // 10 minutes. | |
25 | |
26 } // namespace | |
27 | |
28 EnrollmentHandlerChromeOS::EnrollmentHandlerChromeOS( | |
29 DeviceCloudPolicyStoreChromeOS* store, | |
30 EnterpriseInstallAttributes* install_attributes, | |
31 scoped_ptr<CloudPolicyClient> client, | |
32 const std::string& auth_token, | |
33 const std::string& client_id, | |
34 bool is_auto_enrollment, | |
35 const AllowedDeviceModes& allowed_device_modes, | |
36 const EnrollmentCallback& completion_callback) | |
37 : store_(store), | |
38 install_attributes_(install_attributes), | |
39 client_(client.Pass()), | |
40 auth_token_(auth_token), | |
41 client_id_(client_id), | |
42 is_auto_enrollment_(is_auto_enrollment), | |
43 allowed_device_modes_(allowed_device_modes), | |
44 completion_callback_(completion_callback), | |
45 device_mode_(DEVICE_MODE_NOT_SET), | |
46 enrollment_step_(STEP_PENDING), | |
47 lockbox_init_duration_(0), | |
48 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
49 CHECK(!client_->is_registered()); | |
50 CHECK_EQ(DM_STATUS_SUCCESS, client_->status()); | |
51 store_->AddObserver(this); | |
52 client_->AddObserver(this); | |
53 client_->AddNamespaceToFetch(PolicyNamespaceKey( | |
54 dm_protocol::kChromeDevicePolicyType, std::string())); | |
55 } | |
56 | |
57 EnrollmentHandlerChromeOS::~EnrollmentHandlerChromeOS() { | |
58 Stop(); | |
59 store_->RemoveObserver(this); | |
60 } | |
61 | |
62 void EnrollmentHandlerChromeOS::StartEnrollment() { | |
63 CHECK_EQ(STEP_PENDING, enrollment_step_); | |
64 enrollment_step_ = STEP_LOADING_STORE; | |
65 AttemptRegistration(); | |
66 } | |
67 | |
68 scoped_ptr<CloudPolicyClient> EnrollmentHandlerChromeOS::ReleaseClient() { | |
69 Stop(); | |
70 return client_.Pass(); | |
71 } | |
72 | |
73 void EnrollmentHandlerChromeOS::OnPolicyFetched(CloudPolicyClient* client) { | |
74 DCHECK_EQ(client_.get(), client); | |
75 CHECK_EQ(STEP_POLICY_FETCH, enrollment_step_); | |
76 | |
77 enrollment_step_ = STEP_VALIDATION; | |
78 | |
79 // Validate the policy. | |
80 const em::PolicyFetchResponse* policy = client_->GetPolicyFor( | |
81 PolicyNamespaceKey(dm_protocol::kChromeDevicePolicyType, std::string())); | |
82 if (!policy) { | |
83 ReportResult(EnrollmentStatus::ForFetchError( | |
84 DM_STATUS_RESPONSE_DECODING_ERROR)); | |
85 return; | |
86 } | |
87 | |
88 scoped_ptr<DeviceCloudPolicyValidator> validator( | |
89 DeviceCloudPolicyValidator::Create( | |
90 scoped_ptr<em::PolicyFetchResponse>( | |
91 new em::PolicyFetchResponse(*policy)))); | |
92 | |
93 validator->ValidateTimestamp(base::Time(), base::Time::NowFromSystemTime(), | |
94 CloudPolicyValidatorBase::TIMESTAMP_REQUIRED); | |
95 if (install_attributes_->IsEnterpriseDevice()) | |
96 validator->ValidateDomain(install_attributes_->GetDomain()); | |
97 validator->ValidateDMToken(client->dm_token(), | |
98 CloudPolicyValidatorBase::DM_TOKEN_REQUIRED); | |
99 validator->ValidatePolicyType(dm_protocol::kChromeDevicePolicyType); | |
100 validator->ValidatePayload(); | |
101 validator->ValidateInitialKey(); | |
102 validator.release()->StartValidation( | |
103 base::Bind(&EnrollmentHandlerChromeOS::PolicyValidated, | |
104 weak_factory_.GetWeakPtr())); | |
105 } | |
106 | |
107 void EnrollmentHandlerChromeOS::OnRegistrationStateChanged( | |
108 CloudPolicyClient* client) { | |
109 DCHECK_EQ(client_.get(), client); | |
110 | |
111 if (enrollment_step_ == STEP_REGISTRATION && client_->is_registered()) { | |
112 enrollment_step_ = STEP_POLICY_FETCH, | |
113 device_mode_ = client_->device_mode(); | |
114 if (device_mode_ == DEVICE_MODE_NOT_SET) | |
115 device_mode_ = DEVICE_MODE_ENTERPRISE; | |
116 if (!allowed_device_modes_.test(device_mode_)) { | |
117 LOG(ERROR) << "Bad device mode " << device_mode_; | |
118 ReportResult(EnrollmentStatus::ForStatus( | |
119 EnrollmentStatus::STATUS_REGISTRATION_BAD_MODE)); | |
120 return; | |
121 } | |
122 client_->FetchPolicy(); | |
123 } else { | |
124 LOG(FATAL) << "Registration state changed to " << client_->is_registered() | |
125 << " in step " << enrollment_step_; | |
126 } | |
127 } | |
128 | |
129 void EnrollmentHandlerChromeOS::OnClientError(CloudPolicyClient* client) { | |
130 DCHECK_EQ(client_.get(), client); | |
131 | |
132 if (enrollment_step_ < STEP_POLICY_FETCH) | |
133 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); | |
134 else | |
135 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); | |
136 } | |
137 | |
138 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { | |
139 DCHECK_EQ(store_, store); | |
140 | |
141 if (enrollment_step_ == STEP_LOADING_STORE) { | |
142 AttemptRegistration(); | |
143 } else if (enrollment_step_ == STEP_STORE_POLICY) { | |
144 ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); | |
145 } | |
146 } | |
147 | |
148 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { | |
149 DCHECK_EQ(store_, store); | |
150 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), | |
151 store_->validation_status())); | |
152 } | |
153 | |
154 void EnrollmentHandlerChromeOS::AttemptRegistration() { | |
155 CHECK_EQ(STEP_LOADING_STORE, enrollment_step_); | |
156 if (store_->is_initialized()) { | |
157 enrollment_step_ = STEP_REGISTRATION; | |
158 client_->Register(em::DeviceRegisterRequest::DEVICE, | |
159 auth_token_, client_id_, is_auto_enrollment_); | |
160 } | |
161 } | |
162 | |
163 void EnrollmentHandlerChromeOS::PolicyValidated( | |
164 DeviceCloudPolicyValidator* validator) { | |
165 CHECK_EQ(STEP_VALIDATION, enrollment_step_); | |
166 if (validator->success()) { | |
167 policy_ = validator->policy().Pass(); | |
168 enrollment_step_ = STEP_LOCK_DEVICE; | |
169 WriteInstallAttributes(validator->policy_data()->username(), device_mode_, | |
170 validator->policy_data()->device_id()); | |
171 } else { | |
172 ReportResult(EnrollmentStatus::ForValidationError(validator->status())); | |
173 } | |
174 } | |
175 | |
176 void EnrollmentHandlerChromeOS::WriteInstallAttributes( | |
177 const std::string& user, | |
178 DeviceMode device_mode, | |
179 const std::string& device_id) { | |
180 CHECK_EQ(STEP_LOCK_DEVICE, enrollment_step_); | |
181 // Since this method is also called directly. | |
182 weak_factory_.InvalidateWeakPtrs(); | |
183 | |
184 EnterpriseInstallAttributes::LockResult lock_result = | |
185 install_attributes_->LockDevice(user, device_mode, device_id); | |
186 switch (lock_result) { | |
187 case EnterpriseInstallAttributes::LOCK_SUCCESS: | |
188 enrollment_step_ = STEP_STORE_POLICY; | |
189 store_->InstallInitialPolicy(*policy_); | |
190 return; | |
191 case EnterpriseInstallAttributes::LOCK_NOT_READY: | |
192 // We wait up to |kLockRetryTimeoutMs| milliseconds and if it hasn't | |
193 // succeeded by then show an error to the user and stop the enrollment. | |
194 if (lockbox_init_duration_ < kLockRetryTimeoutMs) { | |
195 // InstallAttributes not ready yet, retry later. | |
196 LOG(WARNING) << "Install Attributes not ready yet will retry in " | |
197 << kLockRetryIntervalMs << "ms."; | |
198 MessageLoop::current()->PostDelayedTask( | |
199 FROM_HERE, | |
200 base::Bind(&EnrollmentHandlerChromeOS::WriteInstallAttributes, | |
201 weak_factory_.GetWeakPtr(), | |
202 user, device_mode, device_id), | |
203 base::TimeDelta::FromMilliseconds(kLockRetryIntervalMs)); | |
204 lockbox_init_duration_ += kLockRetryIntervalMs; | |
205 } else { | |
206 ReportResult(EnrollmentStatus::ForStatus( | |
207 EnrollmentStatus::STATUS_LOCK_TIMEOUT)); | |
208 } | |
209 return; | |
210 case EnterpriseInstallAttributes::LOCK_BACKEND_ERROR: | |
211 ReportResult(EnrollmentStatus::ForStatus( | |
212 EnrollmentStatus::STATUS_LOCK_ERROR)); | |
213 return; | |
214 case EnterpriseInstallAttributes::LOCK_WRONG_USER: | |
215 LOG(ERROR) << "Enrollment cannot proceed because the InstallAttrs " | |
216 << "has been locked already!"; | |
217 ReportResult(EnrollmentStatus::ForStatus( | |
218 EnrollmentStatus::STATUS_LOCK_WRONG_USER)); | |
219 return; | |
220 } | |
221 | |
222 NOTREACHED() << "Invalid lock result " << lock_result; | |
223 ReportResult(EnrollmentStatus::ForStatus( | |
224 EnrollmentStatus::STATUS_LOCK_ERROR)); | |
225 } | |
226 | |
227 void EnrollmentHandlerChromeOS::Stop() { | |
228 if (client_.get()) | |
229 client_->RemoveObserver(this); | |
230 enrollment_step_ = STEP_FINISHED; | |
231 weak_factory_.InvalidateWeakPtrs(); | |
232 completion_callback_.Reset(); | |
233 } | |
234 | |
235 void EnrollmentHandlerChromeOS::ReportResult(EnrollmentStatus status) { | |
236 EnrollmentCallback callback = completion_callback_; | |
237 Stop(); | |
238 | |
239 if (status.status() != EnrollmentStatus::STATUS_SUCCESS) { | |
240 LOG(WARNING) << "Enrollment failed: " << status.status() | |
241 << " " << status.client_status() | |
242 << " " << status.validation_status() | |
243 << " " << status.store_status(); | |
244 } | |
245 | |
246 if (!callback.is_null()) | |
247 callback.Run(status); | |
248 } | |
249 | |
250 } // namespace policy | |
OLD | NEW |