network_configuration_handler.cc revision 58537e28ecd584eab876aee8be7156509866d23a
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "chromeos/network/network_configuration_handler.h" 6 7#include <string> 8#include <vector> 9 10#include "base/bind.h" 11#include "base/format_macros.h" 12#include "base/json/json_writer.h" 13#include "base/logging.h" 14#include "base/memory/ref_counted.h" 15#include "base/memory/scoped_ptr.h" 16#include "base/stl_util.h" 17#include "base/strings/stringprintf.h" 18#include "base/values.h" 19#include "chromeos/dbus/dbus_thread_manager.h" 20#include "chromeos/dbus/shill_manager_client.h" 21#include "chromeos/dbus/shill_profile_client.h" 22#include "chromeos/dbus/shill_service_client.h" 23#include "chromeos/network/network_event_log.h" 24#include "chromeos/network/network_state_handler.h" 25#include "chromeos/network/shill_property_util.h" 26#include "dbus/object_path.h" 27#include "third_party/cros_system_api/dbus/service_constants.h" 28 29namespace chromeos { 30 31namespace { 32 33// Strip surrounding "" from keys (if present). 34std::string StripQuotations(const std::string& in_str) { 35 size_t len = in_str.length(); 36 if (len >= 2 && in_str[0] == '"' && in_str[len-1] == '"') 37 return in_str.substr(1, len-2); 38 return in_str; 39} 40 41void InvokeErrorCallback(const std::string& error, 42 const std::string& path, 43 const network_handler::ErrorCallback& error_callback) { 44 NET_LOG_ERROR(error, path); 45 if (error_callback.is_null()) 46 return; 47 scoped_ptr<base::DictionaryValue> error_data( 48 network_handler::CreateErrorData(path, error, "")); 49 error_callback.Run(error, error_data.Pass()); 50} 51 52void GetPropertiesCallback( 53 const network_handler::DictionaryResultCallback& callback, 54 const network_handler::ErrorCallback& error_callback, 55 const std::string& service_path, 56 DBusMethodCallStatus call_status, 57 const base::DictionaryValue& properties) { 58 // Get the correct name from WifiHex if necessary. 59 scoped_ptr<base::DictionaryValue> properties_copy(properties.DeepCopy()); 60 std::string name = 61 shill_property_util::GetNameFromProperties(service_path, properties); 62 if (!name.empty()) { 63 properties_copy->SetStringWithoutPathExpansion( 64 flimflam::kNameProperty, name); 65 } 66 if (call_status != DBUS_METHOD_CALL_SUCCESS) { 67 // Because network services are added and removed frequently, we will see 68 // failures regularly, so don't log these. 69 if (!error_callback.is_null()) { 70 scoped_ptr<base::DictionaryValue> error_data( 71 network_handler::CreateErrorData( 72 service_path, 73 network_handler::kDBusFailedError, 74 network_handler::kDBusFailedErrorMessage)); 75 error_callback.Run(network_handler::kDBusFailedError, error_data.Pass()); 76 } 77 } else if (!callback.is_null()) { 78 callback.Run(service_path, *properties_copy.get()); 79 } 80} 81 82void SetNetworkProfileErrorCallback( 83 const std::string& service_path, 84 const std::string& profile_path, 85 const network_handler::ErrorCallback& error_callback, 86 const std::string& dbus_error_name, 87 const std::string& dbus_error_message) { 88 network_handler::ShillErrorCallbackFunction( 89 "Config.SetNetworkProfile Failed: " + profile_path, 90 service_path, error_callback, 91 dbus_error_name, dbus_error_message); 92} 93 94bool IsPassphrase(const std::string& key) { 95 return key == flimflam::kEapPrivateKeyPasswordProperty || 96 key == flimflam::kEapPasswordProperty || 97 key == flimflam::kL2tpIpsecPasswordProperty || 98 key == flimflam::kOpenVPNPasswordProperty || 99 key == flimflam::kPassphraseProperty || 100 key == flimflam::kOpenVPNOTPProperty || 101 key == flimflam::kEapPrivateKeyProperty || 102 key == flimflam::kEapPinProperty || 103 key == flimflam::kApnPasswordProperty; 104} 105 106void LogConfigProperties(const std::string& desc, 107 const std::string& path, 108 const base::DictionaryValue& properties) { 109 for (base::DictionaryValue::Iterator iter(properties); 110 !iter.IsAtEnd(); iter.Advance()) { 111 std::string v = "******"; 112 if (!IsPassphrase(iter.key())) 113 base::JSONWriter::Write(&iter.value(), &v); 114 NET_LOG_DEBUG(desc, path + "." + iter.key() + "=" + v); 115 } 116} 117 118} // namespace 119 120// Helper class to request from Shill the profile entries associated with a 121// Service and delete the service from each profile. Triggers either 122// |callback| on success or |error_callback| on failure, and calls 123// |handler|->ProfileEntryDeleterCompleted() on completion to delete itself. 124class NetworkConfigurationHandler::ProfileEntryDeleter 125 : public base::SupportsWeakPtr<ProfileEntryDeleter> { 126 public: 127 ProfileEntryDeleter(NetworkConfigurationHandler* handler, 128 const std::string& service_path, 129 const base::Closure& callback, 130 const network_handler::ErrorCallback& error_callback) 131 : owner_(handler), 132 service_path_(service_path), 133 callback_(callback), 134 error_callback_(error_callback) { 135 } 136 137 void Run() { 138 DBusThreadManager::Get()->GetShillServiceClient()-> 139 GetLoadableProfileEntries( 140 dbus::ObjectPath(service_path_), 141 base::Bind(&ProfileEntryDeleter::GetProfileEntriesToDeleteCallback, 142 AsWeakPtr())); 143 } 144 145 private: 146 void GetProfileEntriesToDeleteCallback( 147 DBusMethodCallStatus call_status, 148 const base::DictionaryValue& profile_entries) { 149 if (call_status != DBUS_METHOD_CALL_SUCCESS) { 150 InvokeErrorCallback( 151 "GetLoadableProfileEntries Failed", service_path_, error_callback_); 152 owner_->ProfileEntryDeleterCompleted(service_path_); // Deletes this. 153 return; 154 } 155 156 for (base::DictionaryValue::Iterator iter(profile_entries); 157 !iter.IsAtEnd(); iter.Advance()) { 158 std::string profile_path = StripQuotations(iter.key()); 159 std::string entry_path; 160 iter.value().GetAsString(&entry_path); 161 if (profile_path.empty() || entry_path.empty()) { 162 NET_LOG_ERROR("Failed to parse Profile Entry", base::StringPrintf( 163 "%s: %s", profile_path.c_str(), entry_path.c_str())); 164 continue; 165 } 166 if (profile_delete_entries_.count(profile_path) != 0) { 167 NET_LOG_ERROR("Multiple Profile Entries", base::StringPrintf( 168 "%s: %s", profile_path.c_str(), entry_path.c_str())); 169 continue; 170 } 171 NET_LOG_DEBUG("Delete Profile Entry", base::StringPrintf( 172 "%s: %s", profile_path.c_str(), entry_path.c_str())); 173 profile_delete_entries_[profile_path] = entry_path; 174 DBusThreadManager::Get()->GetShillProfileClient()->DeleteEntry( 175 dbus::ObjectPath(profile_path), 176 entry_path, 177 base::Bind(&ProfileEntryDeleter::ProfileEntryDeletedCallback, 178 AsWeakPtr(), profile_path, entry_path), 179 base::Bind(&ProfileEntryDeleter::ShillErrorCallback, 180 AsWeakPtr(), profile_path, entry_path)); 181 } 182 } 183 184 void ProfileEntryDeletedCallback(const std::string& profile_path, 185 const std::string& entry) { 186 NET_LOG_DEBUG("Profile Entry Deleted", base::StringPrintf( 187 "%s: %s", profile_path.c_str(), entry.c_str())); 188 profile_delete_entries_.erase(profile_path); 189 if (!profile_delete_entries_.empty()) 190 return; 191 // Run the callback if this is the last pending deletion. 192 if (!callback_.is_null()) 193 callback_.Run(); 194 // Request NetworkStateHandler manager update to update ServiceCompleteList. 195 owner_->network_state_handler_->UpdateManagerProperties(); 196 owner_->ProfileEntryDeleterCompleted(service_path_); // Deletes this. 197 } 198 199 void ShillErrorCallback(const std::string& profile_path, 200 const std::string& entry, 201 const std::string& dbus_error_name, 202 const std::string& dbus_error_message) { 203 // Any Shill Error triggers a failure / error. 204 network_handler::ShillErrorCallbackFunction( 205 "GetLoadableProfileEntries Failed", profile_path, error_callback_, 206 dbus_error_name, dbus_error_message); 207 // Delete this even if there are pending deletions; any callbacks will 208 // safely become no-ops (by invalidating the WeakPtrs). 209 owner_->ProfileEntryDeleterCompleted(service_path_); // Deletes this. 210 } 211 212 NetworkConfigurationHandler* owner_; // Unowned 213 std::string service_path_; 214 base::Closure callback_; 215 network_handler::ErrorCallback error_callback_; 216 217 // Map of pending profile entry deletions, indexed by profile path. 218 std::map<std::string, std::string> profile_delete_entries_; 219 220 DISALLOW_COPY_AND_ASSIGN(ProfileEntryDeleter); 221}; 222 223// NetworkConfigurationHandler 224 225void NetworkConfigurationHandler::GetProperties( 226 const std::string& service_path, 227 const network_handler::DictionaryResultCallback& callback, 228 const network_handler::ErrorCallback& error_callback) const { 229 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( 230 dbus::ObjectPath(service_path), 231 base::Bind(&GetPropertiesCallback, 232 callback, error_callback, service_path)); 233} 234 235void NetworkConfigurationHandler::SetProperties( 236 const std::string& service_path, 237 const base::DictionaryValue& properties, 238 const base::Closure& callback, 239 const network_handler::ErrorCallback& error_callback) { 240 if (properties.empty()) { 241 if (!callback.is_null()) 242 callback.Run(); 243 return; 244 } 245 NET_LOG_USER("SetProperties", service_path); 246 LogConfigProperties("SetProperty", service_path, properties); 247 248 DBusThreadManager::Get()->GetShillServiceClient()->SetProperties( 249 dbus::ObjectPath(service_path), 250 properties, 251 base::Bind(&NetworkConfigurationHandler::SetPropertiesSuccessCallback, 252 AsWeakPtr(), service_path, callback), 253 base::Bind(&NetworkConfigurationHandler::SetPropertiesErrorCallback, 254 AsWeakPtr(), service_path, error_callback)); 255} 256 257void NetworkConfigurationHandler::ClearProperties( 258 const std::string& service_path, 259 const std::vector<std::string>& names, 260 const base::Closure& callback, 261 const network_handler::ErrorCallback& error_callback) { 262 if (names.empty()) { 263 if (!callback.is_null()) 264 callback.Run(); 265 return; 266 } 267 NET_LOG_USER("ClearProperties", service_path); 268 for (std::vector<std::string>::const_iterator iter = names.begin(); 269 iter != names.end(); ++iter) { 270 NET_LOG_DEBUG("ClearProperty", service_path + "." + *iter); 271 } 272 DBusThreadManager::Get()->GetShillServiceClient()->ClearProperties( 273 dbus::ObjectPath(service_path), 274 names, 275 base::Bind(&NetworkConfigurationHandler::ClearPropertiesSuccessCallback, 276 AsWeakPtr(), service_path, names, callback, error_callback), 277 base::Bind(&NetworkConfigurationHandler::ClearPropertiesErrorCallback, 278 AsWeakPtr(), service_path, error_callback)); 279} 280 281void NetworkConfigurationHandler::CreateConfiguration( 282 const base::DictionaryValue& properties, 283 const network_handler::StringResultCallback& callback, 284 const network_handler::ErrorCallback& error_callback) { 285 ShillManagerClient* manager = 286 DBusThreadManager::Get()->GetShillManagerClient(); 287 std::string type; 288 properties.GetStringWithoutPathExpansion(flimflam::kTypeProperty, &type); 289 290 NET_LOG_USER("CreateConfiguration", type); 291 LogConfigProperties("Configure", type, properties); 292 293 std::string profile; 294 properties.GetStringWithoutPathExpansion(flimflam::kProfileProperty, 295 &profile); 296 DCHECK(!profile.empty()); 297 manager->ConfigureServiceForProfile( 298 dbus::ObjectPath(profile), 299 properties, 300 base::Bind(&NetworkConfigurationHandler::RunCreateNetworkCallback, 301 AsWeakPtr(), 302 callback), 303 base::Bind(&network_handler::ShillErrorCallbackFunction, 304 "Config.CreateConfiguration Failed", 305 "", 306 error_callback)); 307} 308 309void NetworkConfigurationHandler::RemoveConfiguration( 310 const std::string& service_path, 311 const base::Closure& callback, 312 const network_handler::ErrorCallback& error_callback) { 313 // Service.Remove is not reliable. Instead, request the profile entries 314 // for the service and remove each entry. 315 if (profile_entry_deleters_.count(service_path)) { 316 InvokeErrorCallback( 317 "RemoveConfiguration In-Progress", service_path, error_callback); 318 return; 319 } 320 NET_LOG_USER("Remove Configuration", service_path); 321 ProfileEntryDeleter* deleter = 322 new ProfileEntryDeleter(this, service_path, callback, error_callback); 323 profile_entry_deleters_[service_path] = deleter; 324 deleter->Run(); 325} 326 327void NetworkConfigurationHandler::SetNetworkProfile( 328 const std::string& service_path, 329 const std::string& profile_path, 330 const base::Closure& callback, 331 const network_handler::ErrorCallback& error_callback) { 332 NET_LOG_USER("SetNetworkProfile", service_path + ": " + profile_path); 333 base::StringValue profile_path_value(profile_path); 334 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty( 335 dbus::ObjectPath(service_path), 336 flimflam::kProfileProperty, 337 profile_path_value, 338 callback, 339 base::Bind(&SetNetworkProfileErrorCallback, 340 service_path, profile_path, error_callback)); 341} 342 343// NetworkConfigurationHandler Private methods 344 345NetworkConfigurationHandler::NetworkConfigurationHandler() 346 : network_state_handler_(NULL) { 347} 348 349NetworkConfigurationHandler::~NetworkConfigurationHandler() { 350 STLDeleteContainerPairSecondPointers( 351 profile_entry_deleters_.begin(), profile_entry_deleters_.end()); 352} 353 354void NetworkConfigurationHandler::Init( 355 NetworkStateHandler* network_state_handler) { 356 network_state_handler_ = network_state_handler; 357} 358 359void NetworkConfigurationHandler::RunCreateNetworkCallback( 360 const network_handler::StringResultCallback& callback, 361 const dbus::ObjectPath& service_path) { 362 if (!callback.is_null()) 363 callback.Run(service_path.value()); 364 // This may also get called when CreateConfiguration is used to update an 365 // existing configuration, so request a service update just in case. 366 // TODO(pneubeck): Separate 'Create' and 'Update' calls and only trigger 367 // this on an update. 368 network_state_handler_->RequestUpdateForNetwork(service_path.value()); 369} 370 371void NetworkConfigurationHandler::ProfileEntryDeleterCompleted( 372 const std::string& service_path) { 373 std::map<std::string, ProfileEntryDeleter*>::iterator iter = 374 profile_entry_deleters_.find(service_path); 375 DCHECK(iter != profile_entry_deleters_.end()); 376 delete iter->second; 377 profile_entry_deleters_.erase(iter); 378} 379 380void NetworkConfigurationHandler::SetPropertiesSuccessCallback( 381 const std::string& service_path, 382 const base::Closure& callback) { 383 if (!callback.is_null()) 384 callback.Run(); 385 network_state_handler_->RequestUpdateForNetwork(service_path); 386} 387 388void NetworkConfigurationHandler::SetPropertiesErrorCallback( 389 const std::string& service_path, 390 const network_handler::ErrorCallback& error_callback, 391 const std::string& dbus_error_name, 392 const std::string& dbus_error_message) { 393 network_handler::ShillErrorCallbackFunction( 394 "Config.SetProperties Failed", 395 service_path, error_callback, 396 dbus_error_name, dbus_error_message); 397 // Some properties may have changed so request an update regardless. 398 network_state_handler_->RequestUpdateForNetwork(service_path); 399} 400 401void NetworkConfigurationHandler::ClearPropertiesSuccessCallback( 402 const std::string& service_path, 403 const std::vector<std::string>& names, 404 const base::Closure& callback, 405 const network_handler::ErrorCallback& error_callback, 406 const base::ListValue& result) { 407 const std::string kClearPropertiesFailedError("Error.ClearPropertiesFailed"); 408 DCHECK(names.size() == result.GetSize()) 409 << "Incorrect result size from ClearProperties."; 410 411 bool some_failed = false; 412 for (size_t i = 0; i < result.GetSize(); ++i) { 413 bool success = false; 414 result.GetBoolean(i, &success); 415 if (!success) { 416 NET_LOG_ERROR("ClearProperties Failed: " + names[i], service_path); 417 some_failed = true; 418 } 419 } 420 421 if (some_failed) { 422 if (!error_callback.is_null()) { 423 scoped_ptr<base::DictionaryValue> error_data( 424 network_handler::CreateErrorData( 425 service_path, kClearPropertiesFailedError, 426 base::StringPrintf("Errors: %" PRIuS, result.GetSize()))); 427 error_data->Set("errors", result.DeepCopy()); 428 scoped_ptr<base::ListValue> name_list(new base::ListValue); 429 name_list->AppendStrings(names); 430 error_data->Set("names", name_list.release()); 431 error_callback.Run(kClearPropertiesFailedError, error_data.Pass()); 432 } 433 } else if (!callback.is_null()) { 434 callback.Run(); 435 } 436 network_state_handler_->RequestUpdateForNetwork(service_path); 437} 438 439void NetworkConfigurationHandler::ClearPropertiesErrorCallback( 440 const std::string& service_path, 441 const network_handler::ErrorCallback& error_callback, 442 const std::string& dbus_error_name, 443 const std::string& dbus_error_message) { 444 network_handler::ShillErrorCallbackFunction( 445 "Config.ClearProperties Failed", 446 service_path, error_callback, 447 dbus_error_name, dbus_error_message); 448 // Some properties may have changed so request an update regardless. 449 network_state_handler_->RequestUpdateForNetwork(service_path); 450} 451 452// static 453NetworkConfigurationHandler* NetworkConfigurationHandler::InitializeForTest( 454 NetworkStateHandler* network_state_handler) { 455 NetworkConfigurationHandler* handler = new NetworkConfigurationHandler(); 456 handler->Init(network_state_handler); 457 return handler; 458} 459 460} // namespace chromeos 461