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