blob: fcf3aa0358ef7ae215c15a784fe29a60986c4723 [file] [log] [blame]
Darin Petkov33af05c2012-02-28 10:10:30 +01001// 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 Petkov02867712012-03-12 14:25:05 +01007#include <algorithm>
Darin Petkov33af05c2012-02-28 10:10:30 +01008
Darin Petkov02867712012-03-12 14:25:05 +01009#include <base/stringprintf.h>
10#include <chromeos/dbus/service_constants.h>
11
12#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070013#include "shill/logging.h"
Paul Stewart65512e12012-03-26 18:01:08 -070014#include "shill/manager.h"
Darin Petkov9c6e9812013-03-26 13:49:07 +010015#include "shill/profile.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010016#include "shill/technology.h"
17#include "shill/vpn_driver.h"
Darin Petkovc3505a52013-03-18 15:13:29 +010018#include "shill/vpn_provider.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010019
Darin Petkov5eb05422012-05-11 15:45:25 +020020using base::Bind;
Darin Petkov02867712012-03-12 14:25:05 +010021using base::StringPrintf;
Darin Petkov5eb05422012-05-11 15:45:25 +020022using base::Unretained;
Darin Petkov02867712012-03-12 14:25:05 +010023using std::replace_if;
Darin Petkov33af05c2012-02-28 10:10:30 +010024using std::string;
25
26namespace shill {
27
Darin Petkov79349f02013-01-24 16:18:26 +010028const char VPNService::kAutoConnNeverConnected[] = "never connected";
Darin Petkov4cbff5b2013-01-29 16:29:05 +010029const char VPNService::kAutoConnVPNAlreadyActive[] = "vpn already active";
Darin Petkov79349f02013-01-24 16:18:26 +010030
Darin Petkov79d74c92012-03-07 17:20:32 +010031VPNService::VPNService(ControlInterface *control,
Darin Petkov33af05c2012-02-28 10:10:30 +010032 EventDispatcher *dispatcher,
33 Metrics *metrics,
34 Manager *manager,
35 VPNDriver *driver)
Darin Petkov79d74c92012-03-07 17:20:32 +010036 : Service(control, dispatcher, metrics, manager, Technology::kVPN),
Paul Stewart22807992012-04-11 08:48:31 -070037 driver_(driver) {
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070038 SetConnectable(true);
Darin Petkovcb715292012-04-25 13:04:37 +020039 set_save_credentials(false);
Paul Stewart22807992012-04-11 08:48:31 -070040 mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_);
41}
Darin Petkov33af05c2012-02-28 10:10:30 +010042
43VPNService::~VPNService() {}
44
mukesh agrawaldc7b8442012-09-27 13:48:14 -070045void VPNService::Connect(Error *error, const char *reason) {
mukesh agrawaldba10462013-06-04 11:36:21 -070046 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 Petkov2f903b32012-04-18 12:56:43 +020056 return;
57 }
mukesh agrawaldc7b8442012-09-27 13:48:14 -070058 Service::Connect(error, reason);
Darin Petkov79d74c92012-03-07 17:20:32 +010059 driver_->Connect(this, error);
Darin Petkov33af05c2012-02-28 10:10:30 +010060}
61
Darin Petkov6aa21872012-03-09 16:10:19 +010062void VPNService::Disconnect(Error *error) {
Darin Petkov457728b2013-01-09 09:49:08 +010063 LOG(INFO) << "Disconnect from service " << unique_name();
Darin Petkov6aa21872012-03-09 16:10:19 +010064 Service::Disconnect(error);
65 driver_->Disconnect();
66}
67
Darin Petkov33af05c2012-02-28 10:10:30 +010068string VPNService::GetStorageIdentifier() const {
Darin Petkov02867712012-03-12 14:25:05 +010069 return storage_id_;
70}
71
72// static
73string VPNService::CreateStorageIdentifier(const KeyValueStore &args,
74 Error *error) {
Darin Petkov7f060332012-03-14 11:46:47 +010075 string host = args.LookupString(flimflam::kProviderHostProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010076 if (host.empty()) {
77 Error::PopulateAndLog(
78 error, Error::kInvalidProperty, "Missing VPN host.");
79 return "";
80 }
Darin Petkov4e02ba22013-04-02 13:44:08 +020081 string name = args.LookupString(flimflam::kNameProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010082 if (name.empty()) {
Darin Petkov4e02ba22013-04-02 13:44:08 +020083 Error::PopulateAndLog(error, Error::kNotSupported, "Missing VPN name.");
84 return "";
Darin Petkov02867712012-03-12 14:25:05 +010085 }
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 Petkov33af05c2012-02-28 10:10:30 +010089}
90
91string VPNService::GetDeviceRpcId(Error *error) {
Darin Petkov33af05c2012-02-28 10:10:30 +010092 error->Populate(Error::kNotSupported);
93 return "/";
94}
95
Darin Petkovf3c71d72012-03-21 12:32:15 +010096bool VPNService::Load(StoreInterface *storage) {
97 return Service::Load(storage) &&
98 driver_->Load(storage, GetStorageIdentifier());
99}
100
101bool VPNService::Save(StoreInterface *storage) {
102 return Service::Save(storage) &&
Darin Petkovcb715292012-04-25 13:04:37 +0200103 driver_->Save(storage, GetStorageIdentifier(), save_credentials());
Darin Petkovf3c71d72012-03-21 12:32:15 +0100104}
105
Paul Stewart65512e12012-03-26 18:01:08 -0700106bool VPNService::Unload() {
Darin Petkova0e645e2012-04-25 11:38:59 +0200107 // The base method also disconnects the service.
Paul Stewart65512e12012-03-26 18:01:08 -0700108 Service::Unload();
109
Darin Petkovcb715292012-04-25 13:04:37 +0200110 set_save_credentials(false);
111 driver_->UnloadCredentials();
112
Paul Stewart65512e12012-03-26 18:01:08 -0700113 // Ask the VPN provider to remove us from its list.
114 manager()->vpn_provider()->RemoveService(this);
115
116 return true;
117}
118
Paul Stewartebd38562012-03-23 13:06:40 -0700119void VPNService::InitDriverPropertyStore() {
120 driver_->InitPropertyStore(mutable_store());
121}
122
Darin Petkov1d0080a2012-04-30 17:10:36 +0200123void VPNService::MakeFavorite() {
124 // The base MakeFavorite method also sets auto_connect_ to true
125 // which is not desirable for VPN services.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700126 MarkAsFavorite();
Darin Petkov1d0080a2012-04-30 17:10:36 +0200127}
128
Darin Petkov5eb05422012-05-11 15:45:25 +0200129void VPNService::SetConnection(const ConnectionRefPtr &connection) {
130 // Construct the connection binder here rather than in the constructor because
Darin Petkov457728b2013-01-09 09:49:08 +0100131 // 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 Petkov5eb05422012-05-11 15:45:25 +0200135 if (!connection_binder_.get()) {
136 connection_binder_.reset(
Darin Petkov457728b2013-01-09 09:49:08 +0100137 new Connection::Binder(unique_name(),
Darin Petkov5eb05422012-05-11 15:45:25 +0200138 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 Petkov79349f02013-01-24 16:18:26 +0100151bool 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 Petkov4cbff5b2013-01-29 16:29:05 +0100161 // 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 Petkov79349f02013-01-24 16:18:26 +0100166 return true;
167}
168
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700169bool VPNService::SetNameProperty(const string &name, Error *error) {
Darin Petkov9c6e9812013-03-26 13:49:07 +0100170 if (name == friendly_name()) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700171 return false;
Darin Petkov9c6e9812013-03-26 13:49:07 +0100172 }
173 LOG(INFO) << "Renaming service " << unique_name() << ": "
174 << friendly_name() << " -> " << name;
175
176 KeyValueStore *args = driver_->args();
Darin Petkov9c6e9812013-03-26 13:49:07 +0100177 args->SetString(flimflam::kNameProperty, name);
178 string new_storage_id = CreateStorageIdentifier(*args, error);
179 if (new_storage_id.empty()) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700180 return false;
Darin Petkov9c6e9812013-03-26 13:49:07 +0100181 }
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 agrawalbebf1b82013-04-23 15:06:33 -0700192 return true;
Darin Petkov9c6e9812013-03-26 13:49:07 +0100193}
194
Darin Petkov33af05c2012-02-28 10:10:30 +0100195} // namespace shill