blob: 185f3b5ad7c8817d32dfa62d1294ffb928a6416c [file] [log] [blame]
Jay Srinivasan43488792012-06-19 00:25:31 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jay Srinivasan43488792012-06-19 00:25:31 -07005#include "update_engine/connection_manager.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07006
7#include <string>
8
Jay Srinivasan43488792012-06-19 00:25:31 -07009#include <base/stl_util.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070010#include <base/string_util.h>
Jay Srinivasan43488792012-06-19 00:25:31 -070011#include <chromeos/dbus/service_constants.h>
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070012#include <dbus/dbus-glib.h>
13#include <glib.h>
14
Alex Deymof4867c42013-06-28 14:41:39 -070015#include "update_engine/prefs.h"
Jay Srinivasan43488792012-06-19 00:25:31 -070016#include "update_engine/system_state.h"
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070017#include "update_engine/utils.h"
18
Jay Srinivasan43488792012-06-19 00:25:31 -070019using std::set;
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070020using std::string;
21
22namespace chromeos_update_engine {
23
24namespace {
25
26// Gets the DbusGProxy for FlimFlam. Must be free'd with ProxyUnref()
27bool 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 Srinivasan43488792012-06-19 00:25:31 -070041 flimflam::kFlimflamServiceName,
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070042 path,
43 interface,
44 &error);
45 if (!proxy) {
46 LOG(ERROR) << "Error getting FlimFlam proxy: "
Darin Petkova0b9e772011-10-06 05:05:56 -070047 << utils::GetAndFreeGError(&error);
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070048 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.
56bool 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.
89bool GetDefaultServicePath(DbusGlibInterface* dbus_iface, string* out_path) {
90 GHashTable* hash_table = NULL;
91
92 TEST_AND_RETURN_FALSE(GetProperties(dbus_iface,
Jay Srinivasan43488792012-06-19 00:25:31 -070093 flimflam::kFlimflamServicePath,
94 flimflam::kFlimflamManagerInterface,
Andrew de los Reyesd57d1472010-10-21 13:34:08 -070095 &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 agrawal88226ff2012-03-19 17:50:06 -0700101 if (G_VALUE_HOLDS(value, DBUS_TYPE_G_OBJECT_PATH_ARRAY) &&
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700102 (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 agrawal88226ff2012-03-19 17:50:06 -0700107
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700108 g_hash_table_unref(hash_table);
109 return success;
110}
111
112NetworkConnectionType ParseConnectionType(const char* type_str) {
Jay Srinivasan43488792012-06-19 00:25:31 -0700113 if (!strcmp(type_str, flimflam::kTypeEthernet)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700114 return kNetEthernet;
Jay Srinivasan43488792012-06-19 00:25:31 -0700115 } else if (!strcmp(type_str, flimflam::kTypeWifi)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700116 return kNetWifi;
Jay Srinivasan43488792012-06-19 00:25:31 -0700117 } else if (!strcmp(type_str, flimflam::kTypeWimax)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700118 return kNetWimax;
Jay Srinivasan43488792012-06-19 00:25:31 -0700119 } else if (!strcmp(type_str, flimflam::kTypeBluetooth)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700120 return kNetBluetooth;
Jay Srinivasan43488792012-06-19 00:25:31 -0700121 } else if (!strcmp(type_str, flimflam::kTypeCellular)) {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700122 return kNetCellular;
123 }
124 return kNetUnknown;
125}
126
127bool 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 Srinivasan43488792012-06-19 00:25:31 -0700134 flimflam::kFlimflamServiceInterface,
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700135 &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 Srinivasan43488792012-06-19 00:25:31 -0700150ConnectionManager::ConnectionManager(SystemState *system_state)
151 : system_state_(system_state) {}
152
153bool 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 Srinivasan6f6ea002012-12-14 11:26:28 -0800161 system_state_->device_policy();
Alex Deymof4867c42013-06-28 14:41:39 -0700162
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 Srinivasan43488792012-06-19 00:25:31 -0700166 if (!device_policy) {
Alex Deymof4867c42013-06-28 14:41:39 -0700167 LOG(INFO) << "Disabling updates over cellular networks as there's no "
168 "device policy loaded yet.";
Jay Srinivasan43488792012-06-19 00:25:31 -0700169 return false;
170 }
171
Alex Deymof4867c42013-06-28 14:41:39 -0700172 if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
173 // The update setting is enforced by the device policy.
Jay Srinivasan43488792012-06-19 00:25:31 -0700174
Alex Deymof4867c42013-06-28 14:41:39 -0700175 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 Srinivasan43488792012-06-19 00:25:31 -0700181
Alex Deymof4867c42013-06-28 14:41:39 -0700182 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
195 int64_t stored_value;
196 if (!prefs->GetInt64(kPrefsUpdateOverCellularPermission, &stored_value))
197 return false;
198
199 if (!stored_value) {
200 LOG(INFO) << "Disabling updates over cellular connection per user "
201 "setting.";
202 return false;
203 }
204 LOG(INFO) << "Allowing updates over cellular per user setting.";
205 return true;
206 }
Jay Srinivasan43488792012-06-19 00:25:31 -0700207 }
208
209 default:
210 return true;
211 }
212}
213
214const char* ConnectionManager::StringForConnectionType(
215 NetworkConnectionType type) const {
216 static const char* const kValues[] = {flimflam::kTypeEthernet,
217 flimflam::kTypeWifi,
218 flimflam::kTypeWimax,
219 flimflam::kTypeBluetooth,
220 flimflam::kTypeCellular};
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700221 if (type < 0 || type >= static_cast<int>(arraysize(kValues))) {
222 return "Unknown";
223 }
224 return kValues[type];
225}
226
Jay Srinivasan43488792012-06-19 00:25:31 -0700227bool ConnectionManager::GetConnectionType(
228 DbusGlibInterface* dbus_iface,
229 NetworkConnectionType* out_type) const {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700230 string default_service_path;
231 TEST_AND_RETURN_FALSE(GetDefaultServicePath(dbus_iface,
232 &default_service_path));
233 TEST_AND_RETURN_FALSE(GetServicePathType(dbus_iface,
234 default_service_path,
235 out_type));
236 return true;
237}
238
Andrew de los Reyesd57d1472010-10-21 13:34:08 -0700239} // namespace chromeos_update_engine