blob: 13503d9ca9ecb465ff4d1e982b2b00fb14743cba [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"
Jay Srinivasan43488792012-06-19 00:25:31 -070031#include "update_engine/system_state.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070032
Alex Deymo30534502015-07-20 15:06:33 -070033using org::chromium::flimflam::ManagerProxyInterface;
34using org::chromium::flimflam::ServiceProxyInterface;
Jay Srinivasan43488792012-06-19 00:25:31 -070035using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070036using std::string;
37
38namespace chromeos_update_engine {
39
Alex Deymo30534502015-07-20 15:06:33 -070040ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
41 SystemState* system_state)
42 : shill_proxy_(shill_proxy), system_state_(system_state) {}
Jay Srinivasan43488792012-06-19 00:25:31 -070043
Sen Jiang255e22b2016-05-20 16:15:29 -070044bool ConnectionManager::IsUpdateAllowedOver(
45 ConnectionType type, ConnectionTethering tethering) const {
Jay Srinivasan43488792012-06-19 00:25:31 -070046 switch (type) {
Sen Jiang255e22b2016-05-20 16:15:29 -070047 case ConnectionType::kBluetooth:
Jay Srinivasan43488792012-06-19 00:25:31 -070048 return false;
49
Sen Jiang255e22b2016-05-20 16:15:29 -070050 case ConnectionType::kCellular: {
Jay Srinivasan43488792012-06-19 00:25:31 -070051 set<string> allowed_types;
52 const policy::DevicePolicy* device_policy =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080053 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -070054
55 // A device_policy is loaded in a lazy way right before an update check,
56 // so the device_policy should be already loaded at this point. If it's
57 // not, return a safe value for this setting.
Jay Srinivasan43488792012-06-19 00:25:31 -070058 if (!device_policy) {
Alex Deymof4867c42013-06-28 14:41:39 -070059 LOG(INFO) << "Disabling updates over cellular networks as there's no "
60 "device policy loaded yet.";
Jay Srinivasan43488792012-06-19 00:25:31 -070061 return false;
62 }
63
Alex Deymof4867c42013-06-28 14:41:39 -070064 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
65 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -070066
Gilad Arnold9a423ff2014-03-27 15:27:35 -070067 if (!ContainsKey(allowed_types, shill::kTypeCellular)) {
Alex Deymof4867c42013-06-28 14:41:39 -070068 LOG(INFO) << "Disabling updates over cellular connection as it's not "
69 "allowed in the device policy.";
70 return false;
71 }
Jay Srinivasan43488792012-06-19 00:25:31 -070072
Alex Deymof4867c42013-06-28 14:41:39 -070073 LOG(INFO) << "Allowing updates over cellular per device policy.";
74 return true;
75 } else {
76 // There's no update setting in the device policy, using the local user
77 // setting.
78 PrefsInterface* prefs = system_state_->prefs();
79
80 if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
81 LOG(INFO) << "Disabling updates over cellular connection as there's "
82 "no device policy setting nor user preference present.";
83 return false;
84 }
85
Alex Deymoefb7c4c2013-07-09 14:34:00 -070086 bool stored_value;
87 if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission,
88 &stored_value)) {
Alex Deymof4867c42013-06-28 14:41:39 -070089 return false;
Alex Deymoefb7c4c2013-07-09 14:34:00 -070090 }
Alex Deymof4867c42013-06-28 14:41:39 -070091
92 if (!stored_value) {
93 LOG(INFO) << "Disabling updates over cellular connection per user "
94 "setting.";
95 return false;
96 }
97 LOG(INFO) << "Allowing updates over cellular per user setting.";
98 return true;
99 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700100 }
101
102 default:
Sen Jiang255e22b2016-05-20 16:15:29 -0700103 if (tethering == ConnectionTethering::kConfirmed) {
Alex Deymo6ae91202014-03-10 19:21:25 -0700104 // Treat this connection as if it is a cellular connection.
105 LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
106 "setting.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700107 return IsUpdateAllowedOver(ConnectionType::kCellular,
108 ConnectionTethering::kUnknown);
Alex Deymo6ae91202014-03-10 19:21:25 -0700109 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700110 return true;
111 }
112}
113
Alex Deymo30534502015-07-20 15:06:33 -0700114bool ConnectionManager::GetConnectionProperties(
Sen Jiang255e22b2016-05-20 16:15:29 -0700115 ConnectionType* out_type, ConnectionTethering* out_tethering) {
Alex Deymo758dd532015-09-09 15:21:22 -0700116 dbus::ObjectPath default_service_path;
Alex Deymo30534502015-07-20 15:06:33 -0700117 TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
Alex Deymo758dd532015-09-09 15:21:22 -0700118 if (!default_service_path.IsValid())
Alex Deymo30534502015-07-20 15:06:33 -0700119 return false;
Alex Deymo1fbaac82015-11-04 04:41:40 -0800120 // Shill uses the "/" service path to indicate that it is not connected.
121 if (default_service_path.value() == "/")
122 return false;
Alex Deymo30534502015-07-20 15:06:33 -0700123 TEST_AND_RETURN_FALSE(
124 GetServicePathProperties(default_service_path, out_type, out_tethering));
125 return true;
Alex Deymo6ae91202014-03-10 19:21:25 -0700126}
127
Alex Deymo758dd532015-09-09 15:21:22 -0700128bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700129 brillo::VariantDictionary properties;
130 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700131 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
132 if (!manager_proxy)
133 return false;
134 TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
135
136 const auto& prop_default_service =
137 properties.find(shill::kDefaultServiceProperty);
138 if (prop_default_service == properties.end())
139 return false;
140
Alex Deymo758dd532015-09-09 15:21:22 -0700141 *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>();
142 return out_path->IsValid();
Alex Deymo30534502015-07-20 15:06:33 -0700143}
144
145bool ConnectionManager::GetServicePathProperties(
Alex Deymo758dd532015-09-09 15:21:22 -0700146 const dbus::ObjectPath& path,
Sen Jiang255e22b2016-05-20 16:15:29 -0700147 ConnectionType* out_type,
148 ConnectionTethering* out_tethering) {
Alex Deymo30534502015-07-20 15:06:33 -0700149 // We create and dispose the ServiceProxyInterface on every request.
150 std::unique_ptr<ServiceProxyInterface> service =
151 shill_proxy_->GetServiceForPath(path);
152
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700153 brillo::VariantDictionary properties;
154 brillo::ErrorPtr error;
Alex Deymo30534502015-07-20 15:06:33 -0700155 TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
156
157 // Populate the out_tethering.
158 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
159 if (prop_tethering == properties.end()) {
160 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700161 *out_tethering = ConnectionTethering::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700162 } else {
163 // If the property doesn't contain a string value, the empty string will
164 // become kUnknown.
Sen Jiang255e22b2016-05-20 16:15:29 -0700165 *out_tethering = connection_utils::ParseConnectionTethering(
166 prop_tethering->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700167 }
168
169 // Populate the out_type property.
170 const auto& prop_type = properties.find(shill::kTypeProperty);
171 if (prop_type == properties.end()) {
172 // Set to Unknown if not present.
Sen Jiang255e22b2016-05-20 16:15:29 -0700173 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700174 return false;
175 }
176
177 string type_str = prop_type->second.TryGet<string>();
178 if (type_str == shill::kTypeVPN) {
179 const auto& prop_physical =
180 properties.find(shill::kPhysicalTechnologyProperty);
181 if (prop_physical == properties.end()) {
182 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
Alex Deymo758dd532015-09-09 15:21:22 -0700183 " connection (service: "
184 << path.value() << "). Returning default kUnknown value.";
Sen Jiang255e22b2016-05-20 16:15:29 -0700185 *out_type = ConnectionType::kUnknown;
Alex Deymo30534502015-07-20 15:06:33 -0700186 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700187 *out_type = connection_utils::ParseConnectionType(
188 prop_physical->second.TryGet<string>());
Alex Deymo30534502015-07-20 15:06:33 -0700189 }
190 } else {
Sen Jiang255e22b2016-05-20 16:15:29 -0700191 *out_type = connection_utils::ParseConnectionType(type_str);
Alex Deymo30534502015-07-20 15:06:33 -0700192 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700193 return true;
194}
195
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700196} // namespace chromeos_update_engine