real_device_policy_provider.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Copyright (C) 2014 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/update_manager/real_device_policy_provider.h"
  17. #include <stdint.h>
  18. #include <vector>
  19. #include <base/location.h>
  20. #include <base/logging.h>
  21. #include <base/time/time.h>
  22. #include <policy/device_policy.h>
  23. #include "update_engine/common/utils.h"
  24. #include "update_engine/connection_utils.h"
  25. #include "update_engine/update_manager/generic_variables.h"
  26. using base::TimeDelta;
  27. using brillo::MessageLoop;
  28. using chromeos_update_engine::ConnectionType;
  29. using policy::DevicePolicy;
  30. using std::set;
  31. using std::string;
  32. using std::vector;
  33. namespace {
  34. const int kDevicePolicyRefreshRateInMinutes = 60;
  35. } // namespace
  36. namespace chromeos_update_manager {
  37. RealDevicePolicyProvider::~RealDevicePolicyProvider() {
  38. MessageLoop::current()->CancelTask(scheduled_refresh_);
  39. }
  40. bool RealDevicePolicyProvider::Init() {
  41. CHECK(policy_provider_ != nullptr);
  42. // On Init() we try to get the device policy and keep updating it.
  43. RefreshDevicePolicyAndReschedule();
  44. #if USE_DBUS
  45. // We also listen for signals from the session manager to force a device
  46. // policy refresh.
  47. session_manager_proxy_->RegisterPropertyChangeCompleteSignalHandler(
  48. base::Bind(&RealDevicePolicyProvider::OnPropertyChangedCompletedSignal,
  49. base::Unretained(this)),
  50. base::Bind(&RealDevicePolicyProvider::OnSignalConnected,
  51. base::Unretained(this)));
  52. #endif // USE_DBUS
  53. return true;
  54. }
  55. void RealDevicePolicyProvider::OnPropertyChangedCompletedSignal(
  56. const string& success) {
  57. if (success != "success") {
  58. LOG(WARNING) << "Received device policy updated signal with a failure.";
  59. }
  60. // We refresh the policy file even if the payload string is kSignalFailure.
  61. LOG(INFO) << "Reloading and re-scheduling device policy due to signal "
  62. "received.";
  63. MessageLoop::current()->CancelTask(scheduled_refresh_);
  64. scheduled_refresh_ = MessageLoop::kTaskIdNull;
  65. RefreshDevicePolicyAndReschedule();
  66. }
  67. void RealDevicePolicyProvider::OnSignalConnected(const string& interface_name,
  68. const string& signal_name,
  69. bool successful) {
  70. if (!successful) {
  71. LOG(WARNING) << "We couldn't connect to SessionManager signal for updates "
  72. "on the device policy blob. We will reload the policy file "
  73. "periodically.";
  74. }
  75. // We do a one-time refresh of the DevicePolicy just in case we missed a
  76. // signal between the first refresh and the time the signal handler was
  77. // actually connected.
  78. RefreshDevicePolicy();
  79. }
  80. void RealDevicePolicyProvider::RefreshDevicePolicyAndReschedule() {
  81. RefreshDevicePolicy();
  82. scheduled_refresh_ = MessageLoop::current()->PostDelayedTask(
  83. FROM_HERE,
  84. base::Bind(&RealDevicePolicyProvider::RefreshDevicePolicyAndReschedule,
  85. base::Unretained(this)),
  86. TimeDelta::FromMinutes(kDevicePolicyRefreshRateInMinutes));
  87. }
  88. template <typename T>
  89. void RealDevicePolicyProvider::UpdateVariable(
  90. AsyncCopyVariable<T>* var, bool (DevicePolicy::*getter_method)(T*) const) {
  91. T new_value;
  92. if (policy_provider_->device_policy_is_loaded() &&
  93. (policy_provider_->GetDevicePolicy().*getter_method)(&new_value)) {
  94. var->SetValue(new_value);
  95. } else {
  96. var->UnsetValue();
  97. }
  98. }
  99. template <typename T>
  100. void RealDevicePolicyProvider::UpdateVariable(
  101. AsyncCopyVariable<T>* var,
  102. bool (RealDevicePolicyProvider::*getter_method)(T*) const) {
  103. T new_value;
  104. if (policy_provider_->device_policy_is_loaded() &&
  105. (this->*getter_method)(&new_value)) {
  106. var->SetValue(new_value);
  107. } else {
  108. var->UnsetValue();
  109. }
  110. }
  111. bool RealDevicePolicyProvider::ConvertRollbackToTargetVersion(
  112. RollbackToTargetVersion* rollback_to_target_version) const {
  113. int rollback_to_target_version_int;
  114. if (!policy_provider_->GetDevicePolicy().GetRollbackToTargetVersion(
  115. &rollback_to_target_version_int)) {
  116. return false;
  117. }
  118. if (rollback_to_target_version_int < 0 ||
  119. rollback_to_target_version_int >=
  120. static_cast<int>(RollbackToTargetVersion::kMaxValue)) {
  121. return false;
  122. }
  123. *rollback_to_target_version =
  124. static_cast<RollbackToTargetVersion>(rollback_to_target_version_int);
  125. return true;
  126. }
  127. bool RealDevicePolicyProvider::ConvertAllowedConnectionTypesForUpdate(
  128. set<ConnectionType>* allowed_types) const {
  129. set<string> allowed_types_str;
  130. if (!policy_provider_->GetDevicePolicy().GetAllowedConnectionTypesForUpdate(
  131. &allowed_types_str)) {
  132. return false;
  133. }
  134. allowed_types->clear();
  135. for (auto& type_str : allowed_types_str) {
  136. ConnectionType type =
  137. chromeos_update_engine::connection_utils::ParseConnectionType(type_str);
  138. if (type != ConnectionType::kUnknown) {
  139. allowed_types->insert(type);
  140. } else {
  141. LOG(WARNING) << "Policy includes unknown connection type: " << type_str;
  142. }
  143. }
  144. return true;
  145. }
  146. bool RealDevicePolicyProvider::ConvertScatterFactor(
  147. TimeDelta* scatter_factor) const {
  148. int64_t scatter_factor_in_seconds;
  149. if (!policy_provider_->GetDevicePolicy().GetScatterFactorInSeconds(
  150. &scatter_factor_in_seconds)) {
  151. return false;
  152. }
  153. if (scatter_factor_in_seconds < 0) {
  154. LOG(WARNING) << "Ignoring negative scatter factor: "
  155. << scatter_factor_in_seconds;
  156. return false;
  157. }
  158. *scatter_factor = TimeDelta::FromSeconds(scatter_factor_in_seconds);
  159. return true;
  160. }
  161. bool RealDevicePolicyProvider::ConvertDisallowedTimeIntervals(
  162. WeeklyTimeIntervalVector* disallowed_intervals_out) const {
  163. vector<DevicePolicy::WeeklyTimeInterval> parsed_intervals;
  164. if (!policy_provider_->GetDevicePolicy().GetDisallowedTimeIntervals(
  165. &parsed_intervals)) {
  166. return false;
  167. }
  168. disallowed_intervals_out->clear();
  169. for (const auto& interval : parsed_intervals) {
  170. disallowed_intervals_out->emplace_back(
  171. WeeklyTime(interval.start_day_of_week, interval.start_time),
  172. WeeklyTime(interval.end_day_of_week, interval.end_time));
  173. }
  174. return true;
  175. }
  176. void RealDevicePolicyProvider::RefreshDevicePolicy() {
  177. if (!policy_provider_->Reload()) {
  178. LOG(INFO) << "No device policies/settings present.";
  179. }
  180. var_device_policy_is_loaded_.SetValue(
  181. policy_provider_->device_policy_is_loaded());
  182. UpdateVariable(&var_release_channel_, &DevicePolicy::GetReleaseChannel);
  183. UpdateVariable(&var_release_channel_delegated_,
  184. &DevicePolicy::GetReleaseChannelDelegated);
  185. UpdateVariable(&var_update_disabled_, &DevicePolicy::GetUpdateDisabled);
  186. UpdateVariable(&var_target_version_prefix_,
  187. &DevicePolicy::GetTargetVersionPrefix);
  188. UpdateVariable(&var_rollback_to_target_version_,
  189. &RealDevicePolicyProvider::ConvertRollbackToTargetVersion);
  190. UpdateVariable(&var_rollback_allowed_milestones_,
  191. &DevicePolicy::GetRollbackAllowedMilestones);
  192. if (policy_provider_->IsConsumerDevice()) {
  193. // For consumer devices (which won't ever have policy), set value to 0.
  194. var_rollback_allowed_milestones_.SetValue(0);
  195. }
  196. UpdateVariable(&var_scatter_factor_,
  197. &RealDevicePolicyProvider::ConvertScatterFactor);
  198. UpdateVariable(
  199. &var_allowed_connection_types_for_update_,
  200. &RealDevicePolicyProvider::ConvertAllowedConnectionTypesForUpdate);
  201. UpdateVariable(&var_owner_, &DevicePolicy::GetOwner);
  202. UpdateVariable(&var_http_downloads_enabled_,
  203. &DevicePolicy::GetHttpDownloadsEnabled);
  204. UpdateVariable(&var_au_p2p_enabled_, &DevicePolicy::GetAuP2PEnabled);
  205. UpdateVariable(&var_allow_kiosk_app_control_chrome_version_,
  206. &DevicePolicy::GetAllowKioskAppControlChromeVersion);
  207. UpdateVariable(&var_auto_launched_kiosk_app_id_,
  208. &DevicePolicy::GetAutoLaunchedKioskAppId);
  209. UpdateVariable(&var_disallowed_time_intervals_,
  210. &RealDevicePolicyProvider::ConvertDisallowedTimeIntervals);
  211. }
  212. } // namespace chromeos_update_manager