1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/policy/device_policy_cache.h"
#include <string>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/values.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/cros/update_library.h"
#include "chrome/browser/chromeos/cros_settings_names.h"
#include "chrome/browser/chromeos/login/ownership_service.h"
#include "chrome/browser/chromeos/user_cros_settings_provider.h"
#include "chrome/browser/policy/cloud_policy_data_store.h"
#include "chrome/browser/policy/enterprise_install_attributes.h"
#include "chrome/browser/policy/enterprise_metrics.h"
#include "chrome/browser/policy/policy_map.h"
#include "chrome/browser/policy/proto/device_management_backend.pb.h"
#include "chrome/browser/policy/proto/device_management_constants.h"
#include "chrome/browser/policy/proto/device_management_local.pb.h"
#include "policy/configuration_policy_type.h"
namespace {
// Stores policy, updates the owner key if required and reports the status
// through a callback.
class StorePolicyOperation : public chromeos::SignedSettingsHelper::Callback,
public chromeos::OwnerManager::KeyUpdateDelegate {
public:
typedef Callback1<chromeos::SignedSettings::ReturnCode>::Type Callback;
StorePolicyOperation(chromeos::SignedSettingsHelper* signed_settings_helper,
const em::PolicyFetchResponse& policy,
Callback* callback)
: signed_settings_helper_(signed_settings_helper),
policy_(policy),
callback_(callback) {
signed_settings_helper_->StartStorePolicyOp(policy, this);
}
virtual ~StorePolicyOperation() {
signed_settings_helper_->CancelCallback(this);
}
// SignedSettingsHelper implementation:
virtual void OnStorePolicyCompleted(
chromeos::SignedSettings::ReturnCode code) OVERRIDE {
if (code != chromeos::SignedSettings::SUCCESS) {
callback_->Run(code);
delete this;
return;
}
if (policy_.has_new_public_key()) {
// The session manager has successfully done a key rotation. Replace the
// owner key also in chrome.
const std::string& new_key = policy_.new_public_key();
const std::vector<uint8> new_key_data(new_key.c_str(),
new_key.c_str() + new_key.size());
chromeos::OwnershipService::GetSharedInstance()->StartUpdateOwnerKey(
new_key_data, this);
return;
} else {
UpdateUserCrosSettings();
callback_->Run(chromeos::SignedSettings::SUCCESS);
delete this;
return;
}
}
// OwnerManager::KeyUpdateDelegate implementation:
virtual void OnKeyUpdated() OVERRIDE {
UpdateUserCrosSettings();
callback_->Run(chromeos::SignedSettings::SUCCESS);
delete this;
}
private:
void UpdateUserCrosSettings() {
// TODO(mnissler): Find a better way. This is a hack that updates the
// UserCrosSettingsProvider's cache, since it is unable to notice we've
// updated policy information.
chromeos::UserCrosSettingsProvider().Reload();
}
chromeos::SignedSettingsHelper* signed_settings_helper_;
em::PolicyFetchResponse policy_;
scoped_ptr<Callback> callback_;
DISALLOW_COPY_AND_ASSIGN(StorePolicyOperation);
};
// Decodes a protobuf integer to an IntegerValue. The caller assumes ownership
// of the return Value*. Returns NULL in case the input value is out of bounds.
Value* DecodeIntegerValue(google::protobuf::int64 value) {
if (value < std::numeric_limits<int>::min() ||
value > std::numeric_limits<int>::max()) {
LOG(WARNING) << "Integer value " << value
<< " out of numeric limits, ignoring.";
return NULL;
}
return Value::CreateIntegerValue(static_cast<int>(value));
}
} // namespace
namespace policy {
DevicePolicyCache::DevicePolicyCache(
CloudPolicyDataStore* data_store,
EnterpriseInstallAttributes* install_attributes)
: data_store_(data_store),
install_attributes_(install_attributes),
signed_settings_helper_(chromeos::SignedSettingsHelper::Get()),
ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) {
}
DevicePolicyCache::DevicePolicyCache(
CloudPolicyDataStore* data_store,
EnterpriseInstallAttributes* install_attributes,
chromeos::SignedSettingsHelper* signed_settings_helper)
: data_store_(data_store),
install_attributes_(install_attributes),
signed_settings_helper_(signed_settings_helper),
ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) {
}
DevicePolicyCache::~DevicePolicyCache() {
signed_settings_helper_->CancelCallback(this);
}
void DevicePolicyCache::Load() {
signed_settings_helper_->StartRetrievePolicyOp(this);
}
void DevicePolicyCache::SetPolicy(const em::PolicyFetchResponse& policy) {
DCHECK(IsReady());
// Make sure we have an enterprise device.
std::string registration_user(install_attributes_->GetRegistrationUser());
if (registration_user.empty()) {
LOG(WARNING) << "Refusing to accept policy on non-enterprise device.";
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy,
kMetricPolicyFetchNonEnterpriseDevice,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::POLICY_LOCAL_ERROR);
return;
}
// Check the user this policy is for against the device-locked name.
em::PolicyData policy_data;
if (!policy_data.ParseFromString(policy.policy_data())) {
LOG(WARNING) << "Invalid policy protobuf";
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchInvalidPolicy,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::POLICY_LOCAL_ERROR);
return;
}
if (registration_user != policy_data.username()) {
LOG(WARNING) << "Refusing policy blob for " << policy_data.username()
<< " which doesn't match " << registration_user;
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchUserMismatch,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::POLICY_LOCAL_ERROR);
return;
}
set_last_policy_refresh_time(base::Time::NowFromSystemTime());
// Start a store operation.
new StorePolicyOperation(signed_settings_helper_,
policy,
callback_factory_.NewCallback(
&DevicePolicyCache::PolicyStoreOpCompleted));
}
void DevicePolicyCache::SetUnmanaged() {
LOG(WARNING) << "Tried to set DevicePolicyCache to 'unmanaged'!";
// This is not supported for DevicePolicyCache.
}
void DevicePolicyCache::OnRetrievePolicyCompleted(
chromeos::SignedSettings::ReturnCode code,
const em::PolicyFetchResponse& policy) {
DCHECK(CalledOnValidThread());
if (!IsReady()) {
std::string device_token;
InstallInitialPolicy(code, policy, &device_token);
// We need to call SetDeviceToken unconditionally to indicate the cache has
// finished loading.
data_store_->SetDeviceToken(device_token, true);
SetReady();
} else { // In other words, IsReady() == true
if (code != chromeos::SignedSettings::SUCCESS) {
if (code == chromeos::SignedSettings::BAD_SIGNATURE) {
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchBadSignature,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::SIGNATURE_MISMATCH);
} else {
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchOtherFailed,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::POLICY_LOCAL_ERROR);
}
return;
}
bool ok = SetPolicyInternal(policy, NULL, false);
if (ok) {
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchOK,
kMetricPolicySize);
}
}
}
bool DevicePolicyCache::DecodePolicyData(const em::PolicyData& policy_data,
PolicyMap* mandatory,
PolicyMap* recommended) {
em::ChromeDeviceSettingsProto policy;
if (!policy.ParseFromString(policy_data.policy_value())) {
LOG(WARNING) << "Failed to parse ChromeDeviceSettingsProto.";
return false;
}
DecodeDevicePolicy(policy, mandatory, recommended);
return true;
}
void DevicePolicyCache::PolicyStoreOpCompleted(
chromeos::SignedSettings::ReturnCode code) {
DCHECK(CalledOnValidThread());
if (code != chromeos::SignedSettings::SUCCESS) {
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyStoreFailed,
kMetricPolicySize);
if (code == chromeos::SignedSettings::BAD_SIGNATURE) {
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchBadSignature,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::SIGNATURE_MISMATCH);
} else {
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyFetchOtherFailed,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::POLICY_LOCAL_ERROR);
}
return;
}
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyStoreSucceeded,
kMetricPolicySize);
signed_settings_helper_->StartRetrievePolicyOp(this);
}
void DevicePolicyCache::InstallInitialPolicy(
chromeos::SignedSettings::ReturnCode code,
const em::PolicyFetchResponse& policy,
std::string* device_token) {
if (code == chromeos::SignedSettings::NOT_FOUND ||
code == chromeos::SignedSettings::KEY_UNAVAILABLE ||
!policy.has_policy_data()) {
InformNotifier(CloudPolicySubsystem::UNENROLLED,
CloudPolicySubsystem::NO_DETAILS);
return;
}
em::PolicyData policy_data;
if (!policy_data.ParseFromString(policy.policy_data())) {
LOG(WARNING) << "Failed to parse PolicyData protobuf.";
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyLoadFailed,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::POLICY_LOCAL_ERROR);
return;
}
if (!policy_data.has_request_token() ||
policy_data.request_token().empty()) {
SetUnmanagedInternal(base::Time::NowFromSystemTime());
InformNotifier(CloudPolicySubsystem::UNMANAGED,
CloudPolicySubsystem::NO_DETAILS);
// TODO(jkummerow): Reminder: When we want to feed device-wide settings
// made by a local owner into this cache, we need to call
// SetPolicyInternal() here.
return;
}
if (!policy_data.has_username() || !policy_data.has_device_id()) {
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyLoadFailed,
kMetricPolicySize);
InformNotifier(CloudPolicySubsystem::LOCAL_ERROR,
CloudPolicySubsystem::POLICY_LOCAL_ERROR);
return;
}
UMA_HISTOGRAM_ENUMERATION(kMetricPolicy, kMetricPolicyLoadSucceeded,
kMetricPolicySize);
data_store_->set_user_name(policy_data.username());
data_store_->set_device_id(policy_data.device_id());
*device_token = policy_data.request_token();
SetPolicyInternal(policy, NULL, false);
}
// static
void DevicePolicyCache::DecodeDevicePolicy(
const em::ChromeDeviceSettingsProto& policy,
PolicyMap* mandatory,
PolicyMap* recommended) {
if (policy.has_device_policy_refresh_rate()) {
const em::DevicePolicyRefreshRateProto container =
policy.device_policy_refresh_rate();
if (container.has_device_policy_refresh_rate()) {
mandatory->Set(kPolicyDevicePolicyRefreshRate,
DecodeIntegerValue(
container.device_policy_refresh_rate()));
}
}
if (policy.has_device_proxy_settings()) {
const em::DeviceProxySettingsProto container =
policy.device_proxy_settings();
if (container.has_proxy_mode()) {
recommended->Set(kPolicyProxyMode,
Value::CreateStringValue(container.proxy_mode()));
}
if (container.has_proxy_server()) {
recommended->Set(kPolicyProxyServer,
Value::CreateStringValue(container.proxy_server()));
}
if (container.has_proxy_pac_url()) {
recommended->Set(kPolicyProxyPacUrl,
Value::CreateStringValue(container.proxy_pac_url()));
}
if (container.has_proxy_bypass_list()) {
recommended->Set(kPolicyProxyBypassList,
Value::CreateStringValue(container.proxy_bypass_list()));
}
}
if (policy.has_release_channel() &&
policy.release_channel().has_release_channel()) {
std::string channel = policy.release_channel().release_channel();
mandatory->Set(
kPolicyChromeOsReleaseChannel, Value::CreateStringValue(channel));
// TODO(dubroy): Once http://crosbug.com/17015 is implemented, we won't
// have to pass the channel in here, only ping the update engine to tell
// it to fetch the channel from the policy.
chromeos::CrosLibrary::Get()->GetUpdateLibrary()->SetReleaseTrack(channel);
}
}
} // namespace policy
|