-
Notifications
You must be signed in to change notification settings - Fork 19
/
real_device_policy_provider.cc
273 lines (243 loc) · 9.6 KB
/
real_device_policy_provider.cc
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
//
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/update_manager/real_device_policy_provider.h"
#include <stdint.h>
#include <vector>
#include <base/location.h>
#include <base/logging.h>
#include <base/time/time.h>
#include <policy/device_policy.h>
#include "update_engine/common/connection_utils.h"
#include "update_engine/common/utils.h"
#include "update_engine/update_manager/generic_variables.h"
using base::TimeDelta;
using brillo::MessageLoop;
using chromeos_update_engine::ConnectionType;
using policy::DevicePolicy;
using std::set;
using std::string;
using std::vector;
namespace {
const int kDevicePolicyRefreshRateInMinutes = 60;
} // namespace
namespace chromeos_update_manager {
RealDevicePolicyProvider::~RealDevicePolicyProvider() {
MessageLoop::current()->CancelTask(scheduled_refresh_);
}
bool RealDevicePolicyProvider::Init() {
CHECK(policy_provider_ != nullptr);
// On Init() we try to get the device policy and keep updating it.
RefreshDevicePolicyAndReschedule();
#if USE_DBUS
// We also listen for signals from the session manager to force a device
// policy refresh.
session_manager_proxy_->RegisterPropertyChangeCompleteSignalHandler(
base::Bind(&RealDevicePolicyProvider::OnPropertyChangedCompletedSignal,
base::Unretained(this)),
base::Bind(&RealDevicePolicyProvider::OnSignalConnected,
base::Unretained(this)));
#endif // USE_DBUS
return true;
}
void RealDevicePolicyProvider::OnPropertyChangedCompletedSignal(
const string& success) {
if (success != "success") {
LOG(WARNING) << "Received device policy updated signal with a failure.";
}
// We refresh the policy file even if the payload string is kSignalFailure.
LOG(INFO) << "Reloading and re-scheduling device policy due to signal "
"received.";
MessageLoop::current()->CancelTask(scheduled_refresh_);
scheduled_refresh_ = MessageLoop::kTaskIdNull;
RefreshDevicePolicyAndReschedule();
}
void RealDevicePolicyProvider::OnSignalConnected(const string& interface_name,
const string& signal_name,
bool successful) {
if (!successful) {
LOG(WARNING) << "We couldn't connect to SessionManager signal for updates "
"on the device policy blob. We will reload the policy file "
"periodically.";
}
// We do a one-time refresh of the DevicePolicy just in case we missed a
// signal between the first refresh and the time the signal handler was
// actually connected.
RefreshDevicePolicy();
}
void RealDevicePolicyProvider::RefreshDevicePolicyAndReschedule() {
RefreshDevicePolicy();
scheduled_refresh_ = MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&RealDevicePolicyProvider::RefreshDevicePolicyAndReschedule,
base::Unretained(this)),
TimeDelta::FromMinutes(kDevicePolicyRefreshRateInMinutes));
}
template <typename T>
void RealDevicePolicyProvider::UpdateVariable(
AsyncCopyVariable<T>* var,
// NOLINTNEXTLINE(readability/casting)
bool (DevicePolicy::*getter)(T*) const) {
T new_value;
if (policy_provider_->device_policy_is_loaded() &&
(policy_provider_->GetDevicePolicy().*getter)(&new_value)) {
var->SetValue(new_value);
} else {
var->UnsetValue();
}
}
template <typename T>
void RealDevicePolicyProvider::UpdateVariable(
AsyncCopyVariable<T>* var,
bool (RealDevicePolicyProvider::*getter)(T*) const) {
T new_value;
if (policy_provider_->device_policy_is_loaded() &&
(this->*getter)(&new_value)) {
var->SetValue(new_value);
} else {
var->UnsetValue();
}
}
bool RealDevicePolicyProvider::ConvertRollbackToTargetVersion(
RollbackToTargetVersion* rollback_to_target_version) const {
int rollback_to_target_version_int;
if (!policy_provider_->GetDevicePolicy().GetRollbackToTargetVersion(
&rollback_to_target_version_int)) {
return false;
}
if (rollback_to_target_version_int < 0 ||
rollback_to_target_version_int >=
static_cast<int>(RollbackToTargetVersion::kMaxValue)) {
return false;
}
*rollback_to_target_version =
static_cast<RollbackToTargetVersion>(rollback_to_target_version_int);
return true;
}
bool RealDevicePolicyProvider::ConvertAllowedConnectionTypesForUpdate(
set<ConnectionType>* allowed_types) const {
set<string> allowed_types_str;
if (!policy_provider_->GetDevicePolicy().GetAllowedConnectionTypesForUpdate(
&allowed_types_str)) {
return false;
}
allowed_types->clear();
for (auto& type_str : allowed_types_str) {
ConnectionType type =
chromeos_update_engine::connection_utils::ParseConnectionType(type_str);
if (type != ConnectionType::kUnknown) {
allowed_types->insert(type);
} else {
LOG(WARNING) << "Policy includes unknown connection type: " << type_str;
}
}
return true;
}
bool RealDevicePolicyProvider::ConvertScatterFactor(
TimeDelta* scatter_factor) const {
int64_t scatter_factor_in_seconds;
if (!policy_provider_->GetDevicePolicy().GetScatterFactorInSeconds(
&scatter_factor_in_seconds)) {
return false;
}
if (scatter_factor_in_seconds < 0) {
LOG(WARNING) << "Ignoring negative scatter factor: "
<< scatter_factor_in_seconds;
return false;
}
*scatter_factor = TimeDelta::FromSeconds(scatter_factor_in_seconds);
return true;
}
bool RealDevicePolicyProvider::ConvertDisallowedTimeIntervals(
WeeklyTimeIntervalVector* disallowed_intervals_out) const {
vector<DevicePolicy::WeeklyTimeInterval> parsed_intervals;
if (!policy_provider_->GetDevicePolicy().GetDisallowedTimeIntervals(
&parsed_intervals)) {
return false;
}
disallowed_intervals_out->clear();
for (const auto& interval : parsed_intervals) {
disallowed_intervals_out->emplace_back(
WeeklyTime(interval.start_day_of_week, interval.start_time),
WeeklyTime(interval.end_day_of_week, interval.end_time));
}
return true;
}
bool RealDevicePolicyProvider::ConvertHasOwner(bool* has_owner) const {
string owner;
if (!policy_provider_->GetDevicePolicy().GetOwner(&owner)) {
return false;
}
*has_owner = !owner.empty();
return true;
}
bool RealDevicePolicyProvider::ConvertChannelDowngradeBehavior(
ChannelDowngradeBehavior* channel_downgrade_behavior) const {
int behavior;
if (!policy_provider_->GetDevicePolicy().GetChannelDowngradeBehavior(
&behavior)) {
return false;
}
if (behavior < static_cast<int>(ChannelDowngradeBehavior::kFirstValue) ||
behavior > static_cast<int>(ChannelDowngradeBehavior::kLastValue)) {
return false;
}
*channel_downgrade_behavior = static_cast<ChannelDowngradeBehavior>(behavior);
return true;
}
void RealDevicePolicyProvider::RefreshDevicePolicy() {
if (!policy_provider_->Reload()) {
LOG(INFO) << "No device policies/settings present.";
}
var_device_policy_is_loaded_.SetValue(
policy_provider_->device_policy_is_loaded());
UpdateVariable(&var_release_channel_, &DevicePolicy::GetReleaseChannel);
UpdateVariable(&var_release_channel_delegated_,
&DevicePolicy::GetReleaseChannelDelegated);
UpdateVariable(&var_release_lts_tag_, &DevicePolicy::GetReleaseLtsTag);
UpdateVariable(&var_update_disabled_, &DevicePolicy::GetUpdateDisabled);
UpdateVariable(&var_target_version_prefix_,
&DevicePolicy::GetTargetVersionPrefix);
UpdateVariable(&var_rollback_to_target_version_,
&RealDevicePolicyProvider::ConvertRollbackToTargetVersion);
UpdateVariable(&var_rollback_allowed_milestones_,
&DevicePolicy::GetRollbackAllowedMilestones);
if (policy_provider_->IsConsumerDevice()) {
// For consumer devices (which won't ever have policy), set value to 0.
var_rollback_allowed_milestones_.SetValue(0);
}
UpdateVariable(&var_scatter_factor_,
&RealDevicePolicyProvider::ConvertScatterFactor);
UpdateVariable(
&var_allowed_connection_types_for_update_,
&RealDevicePolicyProvider::ConvertAllowedConnectionTypesForUpdate);
UpdateVariable(&var_has_owner_, &RealDevicePolicyProvider::ConvertHasOwner);
UpdateVariable(&var_http_downloads_enabled_,
&DevicePolicy::GetHttpDownloadsEnabled);
UpdateVariable(&var_au_p2p_enabled_, &DevicePolicy::GetAuP2PEnabled);
UpdateVariable(&var_allow_kiosk_app_control_chrome_version_,
&DevicePolicy::GetAllowKioskAppControlChromeVersion);
UpdateVariable(&var_auto_launched_kiosk_app_id_,
&DevicePolicy::GetAutoLaunchedKioskAppId);
UpdateVariable(&var_disallowed_time_intervals_,
&RealDevicePolicyProvider::ConvertDisallowedTimeIntervals);
UpdateVariable(&var_channel_downgrade_behavior_,
&RealDevicePolicyProvider::ConvertChannelDowngradeBehavior);
UpdateVariable(&var_device_minimum_version_,
&DevicePolicy::GetHighestDeviceMinimumVersion);
UpdateVariable(&var_quick_fix_build_token_,
&DevicePolicy::GetDeviceQuickFixBuildToken);
}
} // namespace chromeos_update_manager