blob: 5085842e8325c06c61fb5a85e7af33d21a3b27a9 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070016
Jay Srinivasan43488792012-06-19 00:25:31 -070017#include "update_engine/connection_manager.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070018
Alex Vakulenkod2779df2014-06-16 13:19:00 -070019#include <set>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070020#include <string>
21
Jay Srinivasan43488792012-06-19 00:25:31 -070022#include <base/stl_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070023#include <base/strings/string_util.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070024#include <policy/device_policy.h>
Alex Deymod6deb1d2015-08-28 15:54:37 -070025#include <shill/dbus-constants.h>
26#include <shill/dbus-proxies.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/prefs.h"
29#include "update_engine/common/utils.h"
Sen Jiang255e22b2016-05-20 16:15:29 -070030#include "update_engine/connection_utils.h"
Sen Jiangf5bebae2016-06-03 15:36:54 -070031#include "update_engine/shill_proxy.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070032#include "update_engine/system_state.h"
Weidong Guo4b0d6032017-04-17 10:08:38 -070033#include "update_engine/update_attempter.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070034
Alex Deymo30534502015-07-20 15:06:33 -070035using org::chromium::flimflam::ManagerProxyInterface;
36using org::chromium::flimflam::ServiceProxyInterface;
Jay Srinivasan43488792012-06-19 00:25:31 -070037using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070038using std::string;
39
40namespace chromeos_update_engine {
41
Sen Jiangf5bebae2016-06-03 15:36:54 -070042namespace connection_manager {
43std::unique_ptr<ConnectionManagerInterface> CreateConnectionManager(
44 SystemState* system_state) {
45 return std::unique_ptr<ConnectionManagerInterface>(
46 new ConnectionManager(new ShillProxy(), system_state));
47}
48}
49
Alex Deymo30534502015-07-20 15:06:33 -070050ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
51 SystemState* system_state)
52 : shill_proxy_(shill_proxy), system_state_(system_state) {}
Jay Srinivasan43488792012-06-19 00:25:31 -070053
Sen Jiang255e22b2016-05-20 16:15:29 -070054bool ConnectionManager::IsUpdateAllowedOver(
55 ConnectionType type, ConnectionTethering tethering) const {
Jay Srinivasan43488792012-06-19 00:25:31 -070056 switch (type) {
Sen Jiang255e22b2016-05-20 16:15:29 -070057 case ConnectionType::kBluetooth:
Jay Srinivasan43488792012-06-19 00:25:31 -070058 return false;
59
Sen Jiang255e22b2016-05-20 16:15:29 -070060 case ConnectionType::kCellular: {
Jay Srinivasan43488792012-06-19 00:25:31 -070061 set<string> allowed_types;
Weidong Guo4b0d6032017-04-17 10:08:38 -070062
Jay Srinivasan43488792012-06-19 00:25:31 -070063 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080064 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -070065
Weidong Guo4b0d6032017-04-17 10:08:38 -070066 // The device_policy is loaded in a lazy way before an update check. Load
67 // it now from the libbrillo cache if it wasn't already loaded.
Jay Srinivasan43488792012-06-19 00:25:31 -070068 if (!device_policy) {
Weidong Guo4b0d6032017-04-17 10:08:38 -070069 UpdateAttempter* update_attempter = system_state_->update_attempter();
70 if (update_attempter) {
71 update_attempter->RefreshDevicePolicy();
72 device_policy = system_state_->device_policy();
73 }
74 }
75
76 if (!device_policy) {
Weidong Guo840703a2018-01-04 19:41:56 -080077 // Device policy fails to be loaded (possibly due to guest account). We
78 // do not check the local user setting here, which should be checked by
79 // |OmahaRequestAction| during checking for update.
80 LOG(INFO) << "Allowing updates over cellular as device policy "
Weidong Guo4b0d6032017-04-17 10:08:38 -070081 "fails to be loaded.";
Weidong Guo840703a2018-01-04 19:41:56 -080082 return true;
Jay Srinivasan43488792012-06-19 00:25:31 -070083 }
84
Alex Deymof4867c42013-06-28 14:41:39 -070085 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
86 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -070087
Gilad Arnold9a423ff2014-03-27 15:27:35 -070088 if (!ContainsKey(allowed_types, shill::kTypeCellular)) {
Weidong Guo4b0d6032017-04-17 10:08:38 -070089 LOG(INFO) << "Disabling updates over cellular per device policy.";
Alex Deymof4867c42013-06-28 14:41:39 -070090 return false;
91 }
Jay Srinivasan43488792012-06-19 00:25:31 -070092
Alex Deymof4867c42013-06-28 14:41:39 -070093 LOG(INFO) << "Allowing updates over cellular per device policy.";
Weidong Guo840703a2018-01-04 19:41:56 -080094 return true;
Alex Deymof4867c42013-06-28 14:41:39 -070095 }
Weidong Guo4b0d6032017-04-17 10:08:38 -070096
97 // If there's no update setting in the device policy, we do not check
98 // the local user setting here, which should be checked by
99 // |OmahaRequestAction| during checking for update.
Weidong Guo840703a2018-01-04 19:41:56 -0800100 LOG(INFO) << "Allowing updates over cellular as device policy does "
101 "not include update setting.";
Weidong Guo4b0d6032017-04-17 10:08:38 -0700102 return true;
Jay Srinivasan43488792012-06-19 00:25:31 -0700103 }
104
105 default:
Sen Jiang255e22b2016-05-20 16:15:29 -0700106 if (tethering == ConnectionTethering::kConfirmed) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700107 // Treat this connection as if it is a cellular connection.
108 LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
109 "setting.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700110 return IsUpdateAllowedOver(ConnectionType::kCellular,
111 ConnectionTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700112 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700113 return true;
114 }
115}
116
Weidong Guo4b0d6032017-04-17 10:08:38 -0700117bool ConnectionManager::IsAllowedConnectionTypesForUpdateSet() const {
118 const policy::DevicePolicy* device_policy = system_state_->device_policy();
119 if (!device_policy) {
120 LOG(INFO) << "There's no device policy loaded yet.";
121 return false;
122 }
123
124 set<string> allowed_types;
125 if (!device_policy->GetAllowedConnectionTypesForUpdate(
126 &allowed_types)) {
127 return false;
128 }
129
130 return true;
131}
132
Alex Deymo30534502015-07-20 15:06:33 -0700133bool ConnectionManager::GetConnectionProperties(
Sen Jiang255e22b2016-05-20 16:15:29 -0700134 ConnectionType* out_type, ConnectionTethering* out_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700135 dbus::ObjectPath default_service_path;
Alex Deymo30534502015-07-20 15:06:33 -0700136 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
Alex Deymo758dd532015-09-09 15:21:22 -0700137 if (!default_service_path.IsValid())
Alex Deymo30534502015-07-20 15:06:33 -0700138 return false;
Alex Deymo1fbaac82015-11-04 04:41:40 -0800139 // Shill uses the "/" service path to indicate that it is not connected.
140 if (default_service_path.value() == "/")
141 return false;
Alex Deymo30534502015-07-20 15:06:33 -0700142 TEST_AND_RETURN_FALSE(
143 GetServicePathProperties(default_service_path, out_type, out_tethering));
144 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700145}
146
Alex Deymo758dd532015-09-09 15:21:22 -0700147bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700148 brillo::VariantDictionary properties;
149 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700150 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
151 if (!manager_proxy)
152 return false;
153 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
154
155 const auto& prop_default_service =
156 properties.find(shill::kDefaultServiceProperty);
157 if (prop_default_service == properties.end())
158 return false;
159
Alex Deymo758dd532015-09-09 15:21:22 -0700160 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>();
161 return out_path->IsValid();
Alex Deymo30534502015-07-20 15:06:33 -0700162}
163
164bool ConnectionManager::GetServicePathProperties(
Alex Deymo758dd532015-09-09 15:21:22 -0700165 const dbus::ObjectPath& path,
Sen Jiang255e22b2016-05-20 16:15:29 -0700166 ConnectionType* out_type,
167 ConnectionTethering* out_tethering) {
Alex Deymo30534502015-07-20 15:06:33 -0700168 // We create and dispose the ServiceProxyInterface on every request.
169 std::unique_ptr<ServiceProxyInterface> service =
170 shill_proxy_->GetServiceForPath(path);
171
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700172 brillo::VariantDictionary properties;
173 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700174 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
175
176 // Populate the out_tethering.
177 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
178 if (prop_tethering == properties.end()) {
179 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700180 *out_tethering = ConnectionTethering::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700181 } else {
182 // If the property doesn't contain a string value, the empty string will
183 // become kUnknown.
Sen Jiang255e22b2016-05-20 16:15:29 -0700184 *out_tethering = connection_utils::ParseConnectionTethering(
185 prop_tethering->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700186 }
187
188 // Populate the out_type property.
189 const auto& prop_type = properties.find(shill::kTypeProperty);
190 if (prop_type == properties.end()) {
191 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700192 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700193 return false;
194 }
195
196 string type_str = prop_type->second.TryGet<string>();
197 if (type_str == shill::kTypeVPN) {
198 const auto& prop_physical =
199 properties.find(shill::kPhysicalTechnologyProperty);
200 if (prop_physical == properties.end()) {
201 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
Alex Deymo758dd532015-09-09 15:21:22 -0700202 " connection (service: "
203 << path.value() << "). Returning default kUnknown value.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700204 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700205 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700206 *out_type = connection_utils::ParseConnectionType(
207 prop_physical->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700208 }
209 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700210 *out_type = connection_utils::ParseConnectionType(type_str);
Alex Deymo30534502015-07-20 15:06:33 -0700211 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700212 return true;
213}
214
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700215} // namespace chromeos_update_engine