Chromium Code Reviews| 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 AllowedDeviceModes& allowed_device_modes, | |
| 34 const CompletionCallback& completion_callback) | |
| 35 : store_(store), | |
| 36 install_attributes_(install_attributes), | |
| 37 client_(client.Pass()), | |
| 38 auth_token_(auth_token), | |
| 39 allowed_device_modes_(allowed_device_modes), | |
| 40 completion_callback_(completion_callback), | |
| 41 device_mode_(DEVICE_MODE_NOT_SET), | |
| 42 enrollment_step_(STEP_PENDING), | |
| 43 lockbox_init_duration_(0), | |
| 44 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
| 45 CHECK(!client_->is_registered()); | |
| 46 CHECK_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 47 store_->AddObserver(this); | |
| 48 client_->AddObserver(this); | |
| 49 } | |
| 50 | |
| 51 EnrollmentHandlerChromeOS::~EnrollmentHandlerChromeOS() { | |
| 52 Stop(); | |
| 53 store_->RemoveObserver(this); | |
| 54 } | |
| 55 | |
| 56 void EnrollmentHandlerChromeOS::StartEnrollment() { | |
| 57 CHECK_EQ(STEP_PENDING, enrollment_step_); | |
| 58 AttemptRegistration(); | |
| 59 } | |
| 60 | |
| 61 scoped_ptr<CloudPolicyClient> EnrollmentHandlerChromeOS::ReleaseClient() { | |
| 62 Stop(); | |
| 63 return client_.Pass(); | |
| 64 } | |
| 65 | |
| 66 void EnrollmentHandlerChromeOS::OnPolicyFetched(CloudPolicyClient* client) { | |
| 67 DCHECK_EQ(client_.get(), client); | |
| 68 CHECK_EQ(STEP_POLICY_FETCH, enrollment_step_); | |
| 69 | |
| 70 enrollment_step_ = STEP_VALIDATION; | |
| 71 | |
| 72 // Validate the policy. | |
| 73 scoped_ptr<DeviceCloudPolicyValidator> validator( | |
| 74 DeviceCloudPolicyValidator::Create( | |
| 75 scoped_ptr<em::PolicyFetchResponse>( | |
| 76 new em::PolicyFetchResponse(*client_->policy())), | |
| 77 base::Bind(&EnrollmentHandlerChromeOS::PolicyValidated, | |
| 78 weak_factory_.GetWeakPtr()))); | |
| 79 | |
| 80 if (install_attributes_->IsEnterpriseDevice()) | |
| 81 validator->ValidateDomain(install_attributes_->GetDomain()); | |
| 82 validator->ValidatePolicyType(dm_protocol::kChromeDevicePolicyType); | |
| 83 validator->ValidatePayload(); | |
| 84 validator->ValidateInitialKey(); | |
|
Joao da Silva
2012/10/23 18:58:50
Should this also ValidateDMToken()?
Mattias Nissler (ping if slow)
2012/10/24 10:33:18
Yes! Fixed.
| |
| 85 validator.release()->StartValidation(); | |
| 86 } | |
| 87 | |
| 88 void EnrollmentHandlerChromeOS::OnRegistrationStateChanged( | |
| 89 CloudPolicyClient* client) { | |
| 90 DCHECK_EQ(client_.get(), client); | |
| 91 | |
| 92 if (enrollment_step_ == STEP_REGISTRATION && client_->is_registered()) { | |
| 93 enrollment_step_ = STEP_POLICY_FETCH, | |
| 94 device_mode_ = client_->device_mode(); | |
| 95 if (device_mode_ == DEVICE_MODE_NOT_SET) | |
| 96 device_mode_ = DEVICE_MODE_ENTERPRISE; | |
| 97 if (!allowed_device_modes_.test(device_mode_)) { | |
| 98 LOG(ERROR) << "Bad device mode " << device_mode_; | |
| 99 ReportResult(EnrollmentStatus::ForStatus( | |
| 100 EnrollmentStatus::STATUS_REGISTRATION_BAD_MODE)); | |
| 101 return; | |
| 102 } | |
| 103 client_->FetchPolicy(); | |
| 104 } else { | |
| 105 LOG(FATAL) << "Registration state changed to " << client_->is_registered() | |
| 106 << " in step " << enrollment_step_; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void EnrollmentHandlerChromeOS::OnClientError(CloudPolicyClient* client) { | |
| 111 DCHECK_EQ(client_.get(), client); | |
| 112 | |
| 113 if (!enrollment_step_ < STEP_POLICY_FETCH) | |
| 114 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); | |
| 115 else | |
| 116 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); | |
| 117 } | |
| 118 | |
| 119 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { | |
| 120 DCHECK_EQ(store_, store); | |
| 121 | |
| 122 if (enrollment_step_ == STEP_PENDING) { | |
| 123 AttemptRegistration(); | |
|
Joao da Silva
2012/10/23 18:58:50
Technically it's possible that StartEnrollment() h
Mattias Nissler (ping if slow)
2012/10/24 10:33:18
Good observation. Better safe than sorry, I've add
| |
| 124 } else if (enrollment_step_ == STEP_STORE_POLICY) { | |
| 125 ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { | |
| 130 DCHECK_EQ(store_, store); | |
| 131 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), | |
| 132 store_->validation_status())); | |
| 133 } | |
| 134 | |
| 135 void EnrollmentHandlerChromeOS::AttemptRegistration() { | |
| 136 if (store_->is_initialized()) { | |
| 137 enrollment_step_ = STEP_REGISTRATION; | |
| 138 client_->Register(auth_token_); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void EnrollmentHandlerChromeOS::PolicyValidated( | |
| 143 DeviceCloudPolicyValidator* validator) { | |
| 144 CHECK_EQ(STEP_VALIDATION, enrollment_step_); | |
| 145 if (validator->success()) { | |
| 146 policy_ = validator->policy().Pass(); | |
| 147 enrollment_step_ = STEP_LOCK_DEVICE; | |
| 148 WriteInstallAttributes(validator->policy_data()->username(), device_mode_, | |
| 149 validator->policy_data()->device_id()); | |
| 150 } else { | |
| 151 ReportResult(EnrollmentStatus::ForValidationError(validator->status())); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 void EnrollmentHandlerChromeOS::WriteInstallAttributes( | |
| 156 const std::string& user, | |
| 157 DeviceMode device_mode, | |
| 158 const std::string& device_id) { | |
| 159 CHECK_EQ(STEP_LOCK_DEVICE, enrollment_step_); | |
| 160 // Since this method is also called directly. | |
| 161 weak_factory_.InvalidateWeakPtrs(); | |
| 162 | |
| 163 switch (install_attributes_->LockDevice(user, device_mode, device_id)) { | |
| 164 case EnterpriseInstallAttributes::LOCK_SUCCESS: | |
| 165 enrollment_step_ = STEP_STORE_POLICY; | |
| 166 store_->InstallInitialPolicy(*policy_); | |
| 167 return; | |
| 168 case EnterpriseInstallAttributes::LOCK_NOT_READY: | |
| 169 // We wait up to |kLockRetryTimeoutMs| milliseconds and if it hasn't | |
| 170 // succeeded by then show an error to the user and stop the enrollment. | |
| 171 if (lockbox_init_duration_ < kLockRetryTimeoutMs) { | |
| 172 // InstallAttributes not ready yet, retry later. | |
| 173 LOG(WARNING) << "Install Attributes not ready yet will retry in " | |
| 174 << kLockRetryIntervalMs << "ms."; | |
| 175 MessageLoop::current()->PostDelayedTask( | |
| 176 FROM_HERE, | |
| 177 base::Bind(&EnrollmentHandlerChromeOS::WriteInstallAttributes, | |
| 178 weak_factory_.GetWeakPtr(), | |
| 179 user, device_mode, device_id), | |
| 180 base::TimeDelta::FromMilliseconds(kLockRetryIntervalMs)); | |
| 181 lockbox_init_duration_ += kLockRetryIntervalMs; | |
| 182 } else { | |
| 183 ReportResult(EnrollmentStatus::ForStatus( | |
| 184 EnrollmentStatus::STATUS_LOCK_TIMEOUT)); | |
| 185 } | |
| 186 return; | |
| 187 case EnterpriseInstallAttributes::LOCK_BACKEND_ERROR: | |
| 188 ReportResult(EnrollmentStatus::ForStatus( | |
| 189 EnrollmentStatus::STATUS_LOCK_ERROR)); | |
| 190 return; | |
| 191 case EnterpriseInstallAttributes::LOCK_WRONG_USER: | |
| 192 LOG(ERROR) << "Enrollment cannot proceed because the InstallAttrs " | |
| 193 << "has been locked already!"; | |
| 194 ReportResult(EnrollmentStatus::ForStatus( | |
| 195 EnrollmentStatus::STATUS_LOCK_WRONG_USER)); | |
| 196 return; | |
| 197 } | |
| 198 | |
| 199 NOTREACHED(); | |
|
Joao da Silva
2012/10/23 18:58:50
ReportResult() to avoid leaving the user stuck?
Mattias Nissler (ping if slow)
2012/10/24 10:33:18
Done.
| |
| 200 } | |
| 201 | |
| 202 void EnrollmentHandlerChromeOS::Stop() { | |
| 203 if (client_.get()) | |
| 204 client_->RemoveObserver(this); | |
| 205 enrollment_step_ = STEP_FINISHED; | |
| 206 weak_factory_.InvalidateWeakPtrs(); | |
| 207 completion_callback_.Reset(); | |
| 208 } | |
| 209 | |
| 210 void EnrollmentHandlerChromeOS::ReportResult(EnrollmentStatus status) { | |
| 211 CompletionCallback callback = completion_callback_; | |
| 212 Stop(); | |
| 213 | |
| 214 if (status.status() != EnrollmentStatus::STATUS_SUCCESS) { | |
| 215 LOG(WARNING) << "Enrollment failed: " << status.status() | |
| 216 << " " << status.client_status() | |
| 217 << " " << status.validation_status() | |
| 218 << " " << status.store_status(); | |
| 219 } | |
| 220 | |
| 221 if (!callback.is_null()) | |
| 222 callback.Run(status); | |
| 223 } | |
| 224 | |
| 225 } // namespace policy | |
| OLD | NEW |