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