Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 5 | #include "update_engine/connection_manager.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 9 | #include <base/stl_util.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 10 | #include <base/string_util.h> |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 12 | #include <dbus/dbus-glib.h> |
| 13 | #include <glib.h> |
| 14 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 15 | #include "update_engine/prefs.h" |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 16 | #include "update_engine/system_state.h" |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
| 18 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 19 | using std::set; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 20 | using std::string; |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // Gets the DbusGProxy for FlimFlam. Must be free'd with ProxyUnref() |
| 27 | bool GetFlimFlamProxy(DbusGlibInterface* dbus_iface, |
| 28 | const char* path, |
| 29 | const char* interface, |
| 30 | DBusGProxy** out_proxy) { |
| 31 | DBusGConnection* bus; |
| 32 | DBusGProxy* proxy; |
| 33 | GError* error = NULL; |
| 34 | |
| 35 | bus = dbus_iface->BusGet(DBUS_BUS_SYSTEM, &error); |
| 36 | if (!bus) { |
| 37 | LOG(ERROR) << "Failed to get system bus"; |
| 38 | return false; |
| 39 | } |
| 40 | proxy = dbus_iface->ProxyNewForNameOwner(bus, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 41 | flimflam::kFlimflamServiceName, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 42 | path, |
| 43 | interface, |
| 44 | &error); |
| 45 | if (!proxy) { |
| 46 | LOG(ERROR) << "Error getting FlimFlam proxy: " |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 47 | << utils::GetAndFreeGError(&error); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 48 | return false; |
| 49 | } |
| 50 | *out_proxy = proxy; |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | // On success, caller owns the GHashTable at out_hash_table. |
| 55 | // Returns true on success. |
| 56 | bool GetProperties(DbusGlibInterface* dbus_iface, |
| 57 | const char* path, |
| 58 | const char* interface, |
| 59 | GHashTable** out_hash_table) { |
| 60 | DBusGProxy* proxy; |
| 61 | GError* error = NULL; |
| 62 | |
| 63 | TEST_AND_RETURN_FALSE(GetFlimFlamProxy(dbus_iface, |
| 64 | path, |
| 65 | interface, |
| 66 | &proxy)); |
| 67 | |
| 68 | gboolean rc = dbus_iface->ProxyCall(proxy, |
| 69 | "GetProperties", |
| 70 | &error, |
| 71 | G_TYPE_INVALID, |
| 72 | dbus_g_type_get_map("GHashTable", |
| 73 | G_TYPE_STRING, |
| 74 | G_TYPE_VALUE), |
| 75 | out_hash_table, |
| 76 | G_TYPE_INVALID); |
| 77 | dbus_iface->ProxyUnref(proxy); |
| 78 | if (rc == FALSE) { |
| 79 | LOG(ERROR) << "dbus_g_proxy_call failed"; |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | // Returns (via out_path) the default network path, or empty string if |
| 87 | // there's no network up. |
| 88 | // Returns true on success. |
| 89 | bool GetDefaultServicePath(DbusGlibInterface* dbus_iface, string* out_path) { |
| 90 | GHashTable* hash_table = NULL; |
| 91 | |
| 92 | TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 93 | flimflam::kFlimflamServicePath, |
| 94 | flimflam::kFlimflamManagerInterface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 95 | &hash_table)); |
| 96 | |
| 97 | GValue* value = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table, |
| 98 | "Services")); |
| 99 | GArray* array = NULL; |
| 100 | bool success = false; |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 101 | if (G_VALUE_HOLDS(value, DBUS_TYPE_G_OBJECT_PATH_ARRAY) && |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 102 | (array = reinterpret_cast<GArray*>(g_value_get_boxed(value))) && |
| 103 | (array->len > 0)) { |
| 104 | *out_path = g_array_index(array, const char*, 0); |
| 105 | success = true; |
| 106 | } |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 107 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 108 | g_hash_table_unref(hash_table); |
| 109 | return success; |
| 110 | } |
| 111 | |
| 112 | NetworkConnectionType ParseConnectionType(const char* type_str) { |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 113 | if (!strcmp(type_str, flimflam::kTypeEthernet)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 114 | return kNetEthernet; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 115 | } else if (!strcmp(type_str, flimflam::kTypeWifi)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 116 | return kNetWifi; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 117 | } else if (!strcmp(type_str, flimflam::kTypeWimax)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 118 | return kNetWimax; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 119 | } else if (!strcmp(type_str, flimflam::kTypeBluetooth)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 120 | return kNetBluetooth; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 121 | } else if (!strcmp(type_str, flimflam::kTypeCellular)) { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 122 | return kNetCellular; |
| 123 | } |
| 124 | return kNetUnknown; |
| 125 | } |
| 126 | |
| 127 | bool GetServicePathType(DbusGlibInterface* dbus_iface, |
| 128 | const string& path, |
| 129 | NetworkConnectionType* out_type) { |
| 130 | GHashTable* hash_table = NULL; |
| 131 | |
| 132 | TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, |
| 133 | path.c_str(), |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 134 | flimflam::kFlimflamServiceInterface, |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 135 | &hash_table)); |
| 136 | |
| 137 | GValue* value = (GValue*)g_hash_table_lookup(hash_table, "Type"); |
| 138 | const char* type_str = NULL; |
| 139 | bool success = false; |
| 140 | if (value != NULL && (type_str = g_value_get_string(value)) != NULL) { |
| 141 | *out_type = ParseConnectionType(type_str); |
| 142 | success = true; |
| 143 | } |
| 144 | g_hash_table_unref(hash_table); |
| 145 | return success; |
| 146 | } |
| 147 | |
| 148 | } // namespace {} |
| 149 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 150 | ConnectionManager::ConnectionManager(SystemState *system_state) |
| 151 | : system_state_(system_state) {} |
| 152 | |
| 153 | bool ConnectionManager::IsUpdateAllowedOver(NetworkConnectionType type) const { |
| 154 | switch (type) { |
| 155 | case kNetBluetooth: |
| 156 | return false; |
| 157 | |
| 158 | case kNetCellular: { |
| 159 | set<string> allowed_types; |
| 160 | const policy::DevicePolicy* device_policy = |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 161 | system_state_->device_policy(); |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 162 | |
| 163 | // A device_policy is loaded in a lazy way right before an update check, |
| 164 | // so the device_policy should be already loaded at this point. If it's |
| 165 | // not, return a safe value for this setting. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 166 | if (!device_policy) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 167 | LOG(INFO) << "Disabling updates over cellular networks as there's no " |
| 168 | "device policy loaded yet."; |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 172 | if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) { |
| 173 | // The update setting is enforced by the device policy. |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 174 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 175 | if ((type == kNetCellular && |
| 176 | !ContainsKey(allowed_types, flimflam::kTypeCellular))) { |
| 177 | LOG(INFO) << "Disabling updates over cellular connection as it's not " |
| 178 | "allowed in the device policy."; |
| 179 | return false; |
| 180 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 181 | |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 182 | LOG(INFO) << "Allowing updates over cellular per device policy."; |
| 183 | return true; |
| 184 | } else { |
| 185 | // There's no update setting in the device policy, using the local user |
| 186 | // setting. |
| 187 | PrefsInterface* prefs = system_state_->prefs(); |
| 188 | |
| 189 | if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) { |
| 190 | LOG(INFO) << "Disabling updates over cellular connection as there's " |
| 191 | "no device policy setting nor user preference present."; |
| 192 | return false; |
| 193 | } |
| 194 | |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 195 | bool stored_value; |
| 196 | if (!prefs->GetBoolean(kPrefsUpdateOverCellularPermission, |
| 197 | &stored_value)) { |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 198 | return false; |
Alex Deymo | efb7c4c | 2013-07-09 14:34:00 -0700 | [diff] [blame] | 199 | } |
Alex Deymo | f4867c4 | 2013-06-28 14:41:39 -0700 | [diff] [blame] | 200 | |
| 201 | if (!stored_value) { |
| 202 | LOG(INFO) << "Disabling updates over cellular connection per user " |
| 203 | "setting."; |
| 204 | return false; |
| 205 | } |
| 206 | LOG(INFO) << "Allowing updates over cellular per user setting."; |
| 207 | return true; |
| 208 | } |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | default: |
| 212 | return true; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | const char* ConnectionManager::StringForConnectionType( |
| 217 | NetworkConnectionType type) const { |
| 218 | static const char* const kValues[] = {flimflam::kTypeEthernet, |
| 219 | flimflam::kTypeWifi, |
| 220 | flimflam::kTypeWimax, |
| 221 | flimflam::kTypeBluetooth, |
| 222 | flimflam::kTypeCellular}; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 223 | if (type < 0 || type >= static_cast<int>(arraysize(kValues))) { |
| 224 | return "Unknown"; |
| 225 | } |
| 226 | return kValues[type]; |
| 227 | } |
| 228 | |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 229 | bool ConnectionManager::GetConnectionType( |
| 230 | DbusGlibInterface* dbus_iface, |
| 231 | NetworkConnectionType* out_type) const { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 232 | string default_service_path; |
| 233 | TEST_AND_RETURN_FALSE(GetDefaultServicePath(dbus_iface, |
| 234 | &default_service_path)); |
| 235 | TEST_AND_RETURN_FALSE(GetServicePathType(dbus_iface, |
| 236 | default_service_path, |
| 237 | out_type)); |
| 238 | return true; |
| 239 | } |
| 240 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 241 | } // namespace chromeos_update_engine |