| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 16 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 17 | #include "update_engine/connection_manager.h" | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 18 |  | 
| Eric Caruso | be8b755 | 2018-01-23 14:59:50 -0800 | [diff] [blame] | 19 | #include <memory> | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 20 | #include <set> | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 21 | #include <string> | 
|  | 22 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 23 | #include <base/stl_util.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 24 | #include <base/strings/string_util.h> | 
| Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 25 | #include <policy/device_policy.h> | 
| Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame] | 26 | #include <shill/dbus-constants.h> | 
|  | 27 | #include <shill/dbus-proxies.h> | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 28 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/prefs.h" | 
|  | 30 | #include "update_engine/common/utils.h" | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 31 | #include "update_engine/connection_utils.h" | 
| Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 32 | #include "update_engine/shill_proxy.h" | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 33 | #include "update_engine/system_state.h" | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 34 | #include "update_engine/update_attempter.h" | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 35 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 36 | using org::chromium::flimflam::ManagerProxyInterface; | 
|  | 37 | using org::chromium::flimflam::ServiceProxyInterface; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 38 | using std::set; | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 39 | using std::string; | 
|  | 40 |  | 
|  | 41 | namespace chromeos_update_engine { | 
|  | 42 |  | 
| Sen Jiang | f5bebae | 2016-06-03 15:36:54 -0700 | [diff] [blame] | 43 | namespace connection_manager { | 
|  | 44 | std::unique_ptr<ConnectionManagerInterface> CreateConnectionManager( | 
|  | 45 | SystemState* system_state) { | 
|  | 46 | return std::unique_ptr<ConnectionManagerInterface>( | 
|  | 47 | new ConnectionManager(new ShillProxy(), system_state)); | 
|  | 48 | } | 
|  | 49 | } | 
|  | 50 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 51 | ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy, | 
|  | 52 | SystemState* system_state) | 
|  | 53 | : shill_proxy_(shill_proxy), system_state_(system_state) {} | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 54 |  | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 55 | bool ConnectionManager::IsUpdateAllowedOver( | 
|  | 56 | ConnectionType type, ConnectionTethering tethering) const { | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 57 | switch (type) { | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 58 | case ConnectionType::kBluetooth: | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 59 | return false; | 
|  | 60 |  | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 61 | case ConnectionType::kCellular: { | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 62 | set<string> allowed_types; | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 63 |  | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 64 | const policy::DevicePolicy* device_policy = | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 65 | system_state_->device_policy(); | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 66 |  | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 67 | // The device_policy is loaded in a lazy way before an update check. Load | 
|  | 68 | // it now from the libbrillo cache if it wasn't already loaded. | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 69 | if (!device_policy) { | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 70 | UpdateAttempter* update_attempter = system_state_->update_attempter(); | 
|  | 71 | if (update_attempter) { | 
|  | 72 | update_attempter->RefreshDevicePolicy(); | 
|  | 73 | device_policy = system_state_->device_policy(); | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | if (!device_policy) { | 
|  | 78 | // Device policy fails to be loaded (possibly due to guest account). We | 
|  | 79 | // do not check the local user setting here, which should be checked by | 
|  | 80 | // |OmahaRequestAction| during checking for update. | 
|  | 81 | LOG(INFO) << "Allowing updates over cellular as device policy " | 
|  | 82 | "fails to be loaded."; | 
|  | 83 | return true; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 86 | if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { | 
|  | 87 | // The update setting is enforced by the device policy. | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 88 |  | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 89 | if (!base::ContainsKey(allowed_types, shill::kTypeCellular)) { | 
| Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 90 | LOG(INFO) << "Disabling updates over cellular connection as it's not " | 
|  | 91 | "allowed in the device policy."; | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 92 | return false; | 
|  | 93 | } | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 94 |  | 
| Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 95 | LOG(INFO) << "Allowing updates over cellular per device policy."; | 
| Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 96 | return true; | 
| Tao Bao | 5688d16 | 2017-06-06 13:09:06 -0700 | [diff] [blame] | 97 | } | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 98 |  | 
|  | 99 | // If there's no update setting in the device policy, we do not check | 
|  | 100 | // the local user setting here, which should be checked by | 
|  | 101 | // |OmahaRequestAction| during checking for update. | 
|  | 102 | LOG(INFO) << "Allowing updates over cellular as device policy does " | 
|  | 103 | "not include update setting."; | 
|  | 104 | return true; | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
|  | 107 | default: | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 108 | if (tethering == ConnectionTethering::kConfirmed) { | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 109 | // Treat this connection as if it is a cellular connection. | 
|  | 110 | LOG(INFO) << "Current connection is confirmed tethered, using Cellular " | 
|  | 111 | "setting."; | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 112 | return IsUpdateAllowedOver(ConnectionType::kCellular, | 
|  | 113 | ConnectionTethering::kUnknown); | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 114 | } | 
| Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 115 | return true; | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 |  | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 119 | bool ConnectionManager::IsAllowedConnectionTypesForUpdateSet() const { | 
|  | 120 | const policy::DevicePolicy* device_policy = system_state_->device_policy(); | 
|  | 121 | if (!device_policy) { | 
|  | 122 | LOG(INFO) << "There's no device policy loaded yet."; | 
|  | 123 | return false; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | set<string> allowed_types; | 
|  | 127 | if (!device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | return true; | 
|  | 132 | } | 
|  | 133 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 134 | bool ConnectionManager::GetConnectionProperties( | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 135 | ConnectionType* out_type, ConnectionTethering* out_tethering) { | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 136 | dbus::ObjectPath default_service_path; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 137 | TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path)); | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 138 | if (!default_service_path.IsValid()) | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 139 | return false; | 
| Alex Deymo | 1fbaac8 | 2015-11-04 04:41:40 -0800 | [diff] [blame] | 140 | // Shill uses the "/" service path to indicate that it is not connected. | 
|  | 141 | if (default_service_path.value() == "/") | 
|  | 142 | return false; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 143 | TEST_AND_RETURN_FALSE( | 
|  | 144 | GetServicePathProperties(default_service_path, out_type, out_tethering)); | 
|  | 145 | return true; | 
| Alex Deymo | 6ae9120 | 2014-03-10 19:21:25 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 148 | bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 149 | brillo::VariantDictionary properties; | 
|  | 150 | brillo::ErrorPtr error; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 151 | ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy(); | 
|  | 152 | if (!manager_proxy) | 
|  | 153 | return false; | 
|  | 154 | TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error)); | 
|  | 155 |  | 
|  | 156 | const auto& prop_default_service = | 
|  | 157 | properties.find(shill::kDefaultServiceProperty); | 
|  | 158 | if (prop_default_service == properties.end()) | 
|  | 159 | return false; | 
|  | 160 |  | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 161 | *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>(); | 
|  | 162 | return out_path->IsValid(); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
|  | 165 | bool ConnectionManager::GetServicePathProperties( | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 166 | const dbus::ObjectPath& path, | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 167 | ConnectionType* out_type, | 
|  | 168 | ConnectionTethering* out_tethering) { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 169 | // We create and dispose the ServiceProxyInterface on every request. | 
|  | 170 | std::unique_ptr<ServiceProxyInterface> service = | 
|  | 171 | shill_proxy_->GetServiceForPath(path); | 
|  | 172 |  | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 173 | brillo::VariantDictionary properties; | 
|  | 174 | brillo::ErrorPtr error; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 175 | TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error)); | 
|  | 176 |  | 
|  | 177 | // Populate the out_tethering. | 
|  | 178 | const auto& prop_tethering = properties.find(shill::kTetheringProperty); | 
|  | 179 | if (prop_tethering == properties.end()) { | 
|  | 180 | // Set to Unknown if not present. | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 181 | *out_tethering = ConnectionTethering::kUnknown; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 182 | } else { | 
|  | 183 | // If the property doesn't contain a string value, the empty string will | 
|  | 184 | // become kUnknown. | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 185 | *out_tethering = connection_utils::ParseConnectionTethering( | 
|  | 186 | prop_tethering->second.TryGet<string>()); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 187 | } | 
|  | 188 |  | 
|  | 189 | // Populate the out_type property. | 
|  | 190 | const auto& prop_type = properties.find(shill::kTypeProperty); | 
|  | 191 | if (prop_type == properties.end()) { | 
|  | 192 | // Set to Unknown if not present. | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 193 | *out_type = ConnectionType::kUnknown; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 194 | return false; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 | string type_str = prop_type->second.TryGet<string>(); | 
|  | 198 | if (type_str == shill::kTypeVPN) { | 
|  | 199 | const auto& prop_physical = | 
|  | 200 | properties.find(shill::kPhysicalTechnologyProperty); | 
|  | 201 | if (prop_physical == properties.end()) { | 
|  | 202 | LOG(ERROR) << "No PhysicalTechnology property found for a VPN" | 
| Alex Deymo | 758dd53 | 2015-09-09 15:21:22 -0700 | [diff] [blame] | 203 | " connection (service: " | 
|  | 204 | << path.value() << "). Returning default kUnknown value."; | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 205 | *out_type = ConnectionType::kUnknown; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 206 | } else { | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 207 | *out_type = connection_utils::ParseConnectionType( | 
|  | 208 | prop_physical->second.TryGet<string>()); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 209 | } | 
|  | 210 | } else { | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 211 | *out_type = connection_utils::ParseConnectionType(type_str); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 212 | } | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 213 | return true; | 
|  | 214 | } | 
|  | 215 |  | 
| Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 216 | }  // namespace chromeos_update_engine |