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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/updater/policy/dm_policy_manager.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/enterprise_util.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/enterprise_companion/device_management_storage/dm_storage.h"
#include "chrome/updater/constants.h"
#include "chrome/updater/policy/manager.h"
#include "chrome/updater/protos/omaha_settings.pb.h"
#include "device_management_backend.pb.h"
namespace updater {
namespace {
int PolicyValueFromProtoInstallDefaultValue(
::wireless_android_enterprise_devicemanagement::InstallDefaultValue
install_default_value) {
switch (install_default_value) {
case ::wireless_android_enterprise_devicemanagement::
INSTALL_DEFAULT_DISABLED:
return kPolicyDisabled;
case ::wireless_android_enterprise_devicemanagement::
INSTALL_DEFAULT_ENABLED_MACHINE_ONLY:
return kPolicyEnabledMachineOnly;
case ::wireless_android_enterprise_devicemanagement::
INSTALL_DEFAULT_ENABLED:
default:
return kPolicyEnabled;
}
}
int PolicyValueFromProtoInstallValue(
::wireless_android_enterprise_devicemanagement::InstallValue
install_value) {
switch (install_value) {
case ::wireless_android_enterprise_devicemanagement::INSTALL_DISABLED:
return kPolicyDisabled;
case ::wireless_android_enterprise_devicemanagement::
INSTALL_ENABLED_MACHINE_ONLY:
return kPolicyEnabledMachineOnly;
case ::wireless_android_enterprise_devicemanagement::INSTALL_FORCED:
return kPolicyForceInstallMachine;
case ::wireless_android_enterprise_devicemanagement::INSTALL_ENABLED:
default:
return kPolicyEnabled;
}
}
int PolicyValueFromProtoUpdateValue(
::wireless_android_enterprise_devicemanagement::UpdateValue update_value) {
switch (update_value) {
case ::wireless_android_enterprise_devicemanagement::UPDATES_DISABLED:
return kPolicyDisabled;
case ::wireless_android_enterprise_devicemanagement::MANUAL_UPDATES_ONLY:
return kPolicyManualUpdatesOnly;
case ::wireless_android_enterprise_devicemanagement::AUTOMATIC_UPDATES_ONLY:
return kPolicyAutomaticUpdatesOnly;
case ::wireless_android_enterprise_devicemanagement::UPDATES_ENABLED:
default:
return kPolicyEnabled;
}
}
} // namespace
DMPolicyManager::DMPolicyManager(
const ::wireless_android_enterprise_devicemanagement::
OmahaSettingsClientProto& omaha_settings,
std::optional<bool> override_is_managed_device)
: is_managed_device_(override_is_managed_device.value_or(true)),
omaha_settings_(omaha_settings) {}
DMPolicyManager::~DMPolicyManager() = default;
bool DMPolicyManager::HasActiveDevicePolicies() const {
return is_managed_device_;
}
std::string DMPolicyManager::source() const {
return kSourceDMPolicyManager;
}
std::optional<bool> DMPolicyManager::CloudPolicyOverridesPlatformPolicy()
const {
if (!omaha_settings_.has_cloud_policy_overrides_platform_policy()) {
return std::nullopt;
}
return omaha_settings_.cloud_policy_overrides_platform_policy();
}
std::optional<base::TimeDelta> DMPolicyManager::GetLastCheckPeriod() const {
if (!omaha_settings_.has_auto_update_check_period_minutes()) {
return std::nullopt;
}
return base::Minutes(omaha_settings_.auto_update_check_period_minutes());
}
std::optional<UpdatesSuppressedTimes>
DMPolicyManager::GetUpdatesSuppressedTimes() const {
if (!omaha_settings_.has_updates_suppressed()) {
return std::nullopt;
}
const auto& updates_suppressed = omaha_settings_.updates_suppressed();
if (!updates_suppressed.has_start_hour() ||
!updates_suppressed.has_start_minute() ||
!updates_suppressed.has_duration_min()) {
return std::nullopt;
}
UpdatesSuppressedTimes suppressed_times;
suppressed_times.start_hour_ = updates_suppressed.start_hour();
suppressed_times.start_minute_ = updates_suppressed.start_minute();
suppressed_times.duration_minute_ = updates_suppressed.duration_min();
return suppressed_times;
}
std::optional<std::string> DMPolicyManager::GetDownloadPreference() const {
if (!omaha_settings_.has_download_preference()) {
return std::nullopt;
}
return omaha_settings_.download_preference();
}
std::optional<int> DMPolicyManager::GetPackageCacheSizeLimitMBytes() const {
return std::nullopt;
}
std::optional<int> DMPolicyManager::GetPackageCacheExpirationTimeDays() const {
return std::nullopt;
}
std::optional<std::string> DMPolicyManager::GetProxyMode() const {
if (!omaha_settings_.has_proxy_mode()) {
return std::nullopt;
}
return omaha_settings_.proxy_mode();
}
std::optional<std::string> DMPolicyManager::GetProxyPacUrl() const {
if (!omaha_settings_.has_proxy_pac_url()) {
return std::nullopt;
}
return omaha_settings_.proxy_pac_url();
}
std::optional<std::string> DMPolicyManager::GetProxyServer() const {
if (!omaha_settings_.has_proxy_server()) {
return std::nullopt;
}
return omaha_settings_.proxy_server();
}
const ::wireless_android_enterprise_devicemanagement::ApplicationSettings*
DMPolicyManager::GetAppSettings(const std::string& app_id) const {
const auto& repeated_app_settings = omaha_settings_.application_settings();
for (const auto& app_settings_proto : repeated_app_settings) {
#if BUILDFLAG(IS_MAC)
// BundleIdentifier is preferred over AppGuid as product ID on Mac.
// If not found, fall back to AppGuid below.
if (app_settings_proto.has_bundle_identifier() &&
base::EqualsCaseInsensitiveASCII(app_settings_proto.bundle_identifier(),
app_id)) {
return &app_settings_proto;
}
#endif // BUILDFLAG(IS_MAC)
if (app_settings_proto.has_app_guid() &&
base::EqualsCaseInsensitiveASCII(app_settings_proto.app_guid(),
app_id)) {
return &app_settings_proto;
}
}
return nullptr;
}
std::optional<int> DMPolicyManager::GetEffectivePolicyForAppInstalls(
const std::string& app_id) const {
const auto* app_settings = GetAppSettings(app_id);
if (app_settings && app_settings->has_install()) {
return PolicyValueFromProtoInstallValue(app_settings->install());
}
// Fallback to global-level settings.
if (omaha_settings_.has_install_default()) {
return PolicyValueFromProtoInstallDefaultValue(
omaha_settings_.install_default());
}
return std::nullopt;
}
std::optional<int> DMPolicyManager::GetEffectivePolicyForAppUpdates(
const std::string& app_id) const {
const auto* app_settings = GetAppSettings(app_id);
if (app_settings && app_settings->has_update()) {
return PolicyValueFromProtoUpdateValue(app_settings->update());
}
// Fallback to global-level settings.
if (omaha_settings_.has_update_default()) {
return PolicyValueFromProtoUpdateValue(omaha_settings_.update_default());
}
return std::nullopt;
}
std::optional<std::string> DMPolicyManager::GetTargetVersionPrefix(
const std::string& app_id) const {
const auto* app_settings = GetAppSettings(app_id);
if (!app_settings || !app_settings->has_target_version_prefix()) {
return std::nullopt;
}
return app_settings->target_version_prefix();
}
std::optional<std::string> DMPolicyManager::GetTargetChannel(
const std::string& app_id) const {
const auto* app_settings = GetAppSettings(app_id);
if (!app_settings || !app_settings->has_target_channel()) {
return std::nullopt;
}
return app_settings->target_channel();
}
std::optional<bool> DMPolicyManager::IsRollbackToTargetVersionAllowed(
const std::string& app_id) const {
const auto* app_settings = GetAppSettings(app_id);
if (!app_settings || !app_settings->has_rollback_to_target_version()) {
return std::nullopt;
}
return (app_settings->rollback_to_target_version() ==
::wireless_android_enterprise_devicemanagement::
ROLLBACK_TO_TARGET_VERSION_ENABLED);
}
std::optional<int> DMPolicyManager::GetMajorVersionRolloutPolicy(
const std::string& app_id) const {
const auto* app_settings = GetAppSettings(app_id);
if (!app_settings || !app_settings->has_major_version_rollout_policy()) {
return std::nullopt;
}
return app_settings->major_version_rollout_policy();
}
std::optional<int> DMPolicyManager::GetMinorVersionRolloutPolicy(
const std::string& app_id) const {
const auto* app_settings = GetAppSettings(app_id);
if (!app_settings || !app_settings->has_minor_version_rollout_policy()) {
return std::nullopt;
}
return app_settings->minor_version_rollout_policy();
}
std::optional<std::vector<std::string>> DMPolicyManager::GetForceInstallApps()
const {
std::vector<std::string> force_install_apps;
for (const auto& app_settings_proto :
omaha_settings_.application_settings()) {
const std::string app_id = [&app_settings_proto, this] {
if (app_settings_proto.install() != kPolicyForceInstallMachine &&
omaha_settings_.install_default() != kPolicyForceInstallMachine) {
return std::string();
}
#if BUILDFLAG(IS_MAC)
if (app_settings_proto.has_bundle_identifier()) {
return app_settings_proto.bundle_identifier();
}
#endif
return app_settings_proto.app_guid();
}();
if (!app_id.empty()) {
force_install_apps.push_back(app_id);
}
}
return force_install_apps.empty()
? std::nullopt
: std::optional<std::vector<std::string>>(force_install_apps);
}
std::optional<std::vector<std::string>> DMPolicyManager::GetAppsWithPolicy()
const {
std::vector<std::string> apps_with_policy;
for (const auto& app_settings_proto :
omaha_settings_.application_settings()) {
#if BUILDFLAG(IS_MAC)
// BundleIdentifier is preferred over AppGuid as product ID on Mac.
// If not found, fall back to AppGuid below.
if (app_settings_proto.has_bundle_identifier()) {
apps_with_policy.push_back(app_settings_proto.bundle_identifier());
continue;
}
#endif // BUILDFLAG(IS_MAC)
if (app_settings_proto.has_app_guid()) {
apps_with_policy.push_back(app_settings_proto.app_guid());
}
}
return apps_with_policy;
}
std::optional<
wireless_android_enterprise_devicemanagement::OmahaSettingsClientProto>
GetOmahaPolicySettings(
scoped_refptr<device_management_storage::DMStorage> dm_storage) {
static constexpr char kGoogleUpdatePolicyType[] =
"google/machine-level-omaha";
wireless_android_enterprise_devicemanagement::OmahaSettingsClientProto
omaha_settings;
std::optional<enterprise_management::PolicyData> policy_data =
dm_storage->ReadPolicyData(kGoogleUpdatePolicyType);
if (!policy_data || !policy_data->has_policy_value()) {
return std::nullopt;
}
if (!omaha_settings.ParseFromString(policy_data->policy_value())) {
VLOG(1) << "Failed to parse OmahaSettingsClientProto";
return std::nullopt;
}
return omaha_settings;
}
scoped_refptr<PolicyManagerInterface> CreateDMPolicyManager(
std::optional<bool> override_is_managed_device) {
scoped_refptr<device_management_storage::DMStorage> default_dm_storage =
device_management_storage::GetDefaultDMStorage();
if (!default_dm_storage) {
return nullptr;
}
std::optional<
::wireless_android_enterprise_devicemanagement::OmahaSettingsClientProto>
omaha_settings = GetOmahaPolicySettings(default_dm_storage);
if (!omaha_settings) {
return nullptr;
}
return base::MakeRefCounted<DMPolicyManager>(*omaha_settings,
override_is_managed_device);
}
} // namespace updater
|