Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 "shill/vpn_service.h" |
| 6 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 7 | #include <algorithm> |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 8 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 9 | #include <base/stringprintf.h> |
| 10 | #include <chromeos/dbus/service_constants.h> |
| 11 | |
| 12 | #include "shill/key_value_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 13 | #include "shill/logging.h" |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 14 | #include "shill/manager.h" |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 15 | #include "shill/profile.h" |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 16 | #include "shill/property_accessor.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 17 | #include "shill/technology.h" |
| 18 | #include "shill/vpn_driver.h" |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 19 | #include "shill/vpn_provider.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 20 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 21 | using base::Bind; |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 22 | using base::StringPrintf; |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 23 | using base::Unretained; |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 24 | using std::replace_if; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 25 | using std::string; |
| 26 | |
| 27 | namespace shill { |
| 28 | |
Darin Petkov | 79349f0 | 2013-01-24 16:18:26 +0100 | [diff] [blame] | 29 | const char VPNService::kAutoConnNeverConnected[] = "never connected"; |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 30 | const char VPNService::kAutoConnVPNAlreadyActive[] = "vpn already active"; |
Darin Petkov | 79349f0 | 2013-01-24 16:18:26 +0100 | [diff] [blame] | 31 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 32 | VPNService::VPNService(ControlInterface *control, |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 33 | EventDispatcher *dispatcher, |
| 34 | Metrics *metrics, |
| 35 | Manager *manager, |
| 36 | VPNDriver *driver) |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 37 | : Service(control, dispatcher, metrics, manager, Technology::kVPN), |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 38 | driver_(driver) { |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 39 | SetConnectable(true); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 40 | set_save_credentials(false); |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 41 | mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_); |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 42 | mutable_store()->RegisterDerivedString( |
| 43 | kPhysicalTechnologyProperty, |
| 44 | StringAccessor( |
| 45 | new CustomAccessor<VPNService, string>( |
| 46 | this, |
| 47 | &VPNService::GetPhysicalTechologyProperty, |
| 48 | NULL))); |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 49 | } |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 50 | |
| 51 | VPNService::~VPNService() {} |
| 52 | |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 53 | void VPNService::Connect(Error *error, const char *reason) { |
mukesh agrawal | dba1046 | 2013-06-04 11:36:21 -0700 | [diff] [blame] | 54 | if (IsConnected()) { |
| 55 | Error::PopulateAndLog(error, Error::kAlreadyConnected, |
| 56 | StringPrintf("VPN service %s already connected.", |
| 57 | unique_name().c_str())); |
| 58 | return; |
| 59 | } |
| 60 | if (IsConnecting()) { |
| 61 | Error::PopulateAndLog(error, Error::kInProgress, |
| 62 | StringPrintf("VPN service %s already connecting.", |
| 63 | unique_name().c_str())); |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 64 | return; |
| 65 | } |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 66 | Service::Connect(error, reason); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 67 | driver_->Connect(this, error); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 70 | void VPNService::Disconnect(Error *error) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 71 | LOG(INFO) << "Disconnect from service " << unique_name(); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 72 | Service::Disconnect(error); |
| 73 | driver_->Disconnect(); |
| 74 | } |
| 75 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 76 | string VPNService::GetStorageIdentifier() const { |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 77 | return storage_id_; |
| 78 | } |
| 79 | |
| 80 | // static |
| 81 | string VPNService::CreateStorageIdentifier(const KeyValueStore &args, |
| 82 | Error *error) { |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 83 | string host = args.LookupString(flimflam::kProviderHostProperty, ""); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 84 | if (host.empty()) { |
| 85 | Error::PopulateAndLog( |
| 86 | error, Error::kInvalidProperty, "Missing VPN host."); |
| 87 | return ""; |
| 88 | } |
Darin Petkov | 4e02ba2 | 2013-04-02 13:44:08 +0200 | [diff] [blame] | 89 | string name = args.LookupString(flimflam::kNameProperty, ""); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 90 | if (name.empty()) { |
Darin Petkov | 4e02ba2 | 2013-04-02 13:44:08 +0200 | [diff] [blame] | 91 | Error::PopulateAndLog(error, Error::kNotSupported, "Missing VPN name."); |
| 92 | return ""; |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 93 | } |
| 94 | string id = StringPrintf("vpn_%s_%s", host.c_str(), name.c_str()); |
| 95 | replace_if(id.begin(), id.end(), &Service::IllegalChar, '_'); |
| 96 | return id; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | string VPNService::GetDeviceRpcId(Error *error) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 100 | error->Populate(Error::kNotSupported); |
| 101 | return "/"; |
| 102 | } |
| 103 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 104 | bool VPNService::Load(StoreInterface *storage) { |
| 105 | return Service::Load(storage) && |
| 106 | driver_->Load(storage, GetStorageIdentifier()); |
| 107 | } |
| 108 | |
| 109 | bool VPNService::Save(StoreInterface *storage) { |
| 110 | return Service::Save(storage) && |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 111 | driver_->Save(storage, GetStorageIdentifier(), save_credentials()); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 114 | bool VPNService::Unload() { |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 115 | // The base method also disconnects the service. |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 116 | Service::Unload(); |
| 117 | |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 118 | set_save_credentials(false); |
| 119 | driver_->UnloadCredentials(); |
| 120 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 121 | // Ask the VPN provider to remove us from its list. |
| 122 | manager()->vpn_provider()->RemoveService(this); |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 127 | void VPNService::InitDriverPropertyStore() { |
| 128 | driver_->InitPropertyStore(mutable_store()); |
| 129 | } |
| 130 | |
Darin Petkov | 1d0080a | 2012-04-30 17:10:36 +0200 | [diff] [blame] | 131 | void VPNService::MakeFavorite() { |
| 132 | // The base MakeFavorite method also sets auto_connect_ to true |
| 133 | // which is not desirable for VPN services. |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 134 | MarkAsFavorite(); |
Darin Petkov | 1d0080a | 2012-04-30 17:10:36 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 137 | void VPNService::SetConnection(const ConnectionRefPtr &connection) { |
| 138 | // Construct the connection binder here rather than in the constructor because |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 139 | // there's really no reason to construct a binder if we never connect to this |
| 140 | // service. It's safe to use an unretained callback to driver's method because |
| 141 | // both the binder and the driver will be destroyed when this service is |
| 142 | // destructed. |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 143 | if (!connection_binder_.get()) { |
| 144 | connection_binder_.reset( |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 145 | new Connection::Binder(unique_name(), |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 146 | Bind(&VPNDriver::OnConnectionDisconnected, |
| 147 | Unretained(driver_.get())))); |
| 148 | } |
| 149 | // Note that |connection_| is a reference-counted pointer and is always set |
| 150 | // through this method. This means that the connection binder will not be |
| 151 | // notified when the connection is destructed (because we will unbind it first |
| 152 | // here when it's set to NULL, or because the binder will already be destroyed |
| 153 | // by ~VPNService) -- it will be notified only if the connection disconnects |
| 154 | // (e.g., because an underlying connection is destructed). |
| 155 | connection_binder_->Attach(connection); |
| 156 | Service::SetConnection(connection); |
| 157 | } |
| 158 | |
Darin Petkov | 79349f0 | 2013-01-24 16:18:26 +0100 | [diff] [blame] | 159 | bool VPNService::IsAutoConnectable(const char **reason) const { |
| 160 | if (!Service::IsAutoConnectable(reason)) { |
| 161 | return false; |
| 162 | } |
| 163 | // Don't auto-connect VPN services that have never connected. This improves |
| 164 | // the chances that the VPN service is connectable and avoids dialog popups. |
| 165 | if (!has_ever_connected()) { |
| 166 | *reason = kAutoConnNeverConnected; |
| 167 | return false; |
| 168 | } |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 169 | // Don't auto-connect a VPN service if another VPN service is already active. |
| 170 | if (manager()->vpn_provider()->HasActiveService()) { |
| 171 | *reason = kAutoConnVPNAlreadyActive; |
| 172 | return false; |
| 173 | } |
Darin Petkov | 79349f0 | 2013-01-24 16:18:26 +0100 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 177 | bool VPNService::SetNameProperty(const string &name, Error *error) { |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 178 | if (name == friendly_name()) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 179 | return false; |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 180 | } |
| 181 | LOG(INFO) << "Renaming service " << unique_name() << ": " |
| 182 | << friendly_name() << " -> " << name; |
| 183 | |
| 184 | KeyValueStore *args = driver_->args(); |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 185 | args->SetString(flimflam::kNameProperty, name); |
| 186 | string new_storage_id = CreateStorageIdentifier(*args, error); |
| 187 | if (new_storage_id.empty()) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 188 | return false; |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 189 | } |
| 190 | string old_storage_id = storage_id_; |
| 191 | DCHECK_NE(old_storage_id, new_storage_id); |
| 192 | |
| 193 | SetFriendlyName(name); |
| 194 | |
| 195 | // Update the storage identifier before invoking DeleteEntry to prevent it |
| 196 | // from unloading this service. |
| 197 | storage_id_ = new_storage_id; |
| 198 | profile()->DeleteEntry(old_storage_id, NULL); |
| 199 | profile()->UpdateService(this); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 200 | return true; |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 203 | string VPNService::GetPhysicalTechologyProperty(Error *error) { |
| 204 | ConnectionRefPtr conn = connection(); |
| 205 | if (conn) |
| 206 | conn = conn->GetCarrierConnection(); |
| 207 | |
| 208 | if (!conn) { |
| 209 | Error::PopulateAndLog(error, |
| 210 | Error::kOperationFailed, |
| 211 | Error::GetDefaultMessage(Error::kOperationFailed)); |
| 212 | return ""; |
| 213 | } |
| 214 | |
| 215 | return Technology::NameFromIdentifier(conn->technology()); |
| 216 | } |
| 217 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 218 | } // namespace shill |