blob: 0e8771dbdae702de9f422c9e8c114c1272328e2e [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) {
Darin Petkov2f903b32012-04-18 12:56:43 +020046 if (IsConnected() || IsConnecting()) {
47 Error::PopulateAndLog(
48 error, Error::kAlreadyConnected, "VPN service already connected.");
49 return;
50 }
mukesh agrawaldc7b8442012-09-27 13:48:14 -070051 Service::Connect(error, reason);
Darin Petkov79d74c92012-03-07 17:20:32 +010052 driver_->Connect(this, error);
Darin Petkov33af05c2012-02-28 10:10:30 +010053}
54
Darin Petkov6aa21872012-03-09 16:10:19 +010055void VPNService::Disconnect(Error *error) {
Darin Petkov457728b2013-01-09 09:49:08 +010056 LOG(INFO) << "Disconnect from service " << unique_name();
Darin Petkov6aa21872012-03-09 16:10:19 +010057 Service::Disconnect(error);
58 driver_->Disconnect();
59}
60
Darin Petkov33af05c2012-02-28 10:10:30 +010061string VPNService::GetStorageIdentifier() const {
Darin Petkov02867712012-03-12 14:25:05 +010062 return storage_id_;
63}
64
65// static
66string VPNService::CreateStorageIdentifier(const KeyValueStore &args,
67 Error *error) {
Darin Petkov7f060332012-03-14 11:46:47 +010068 string host = args.LookupString(flimflam::kProviderHostProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010069 if (host.empty()) {
70 Error::PopulateAndLog(
71 error, Error::kInvalidProperty, "Missing VPN host.");
72 return "";
73 }
Darin Petkov4e02ba22013-04-02 13:44:08 +020074 string name = args.LookupString(flimflam::kNameProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010075 if (name.empty()) {
Darin Petkov4e02ba22013-04-02 13:44:08 +020076 Error::PopulateAndLog(error, Error::kNotSupported, "Missing VPN name.");
77 return "";
Darin Petkov02867712012-03-12 14:25:05 +010078 }
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 Petkov33af05c2012-02-28 10:10:30 +010082}
83
84string VPNService::GetDeviceRpcId(Error *error) {
Darin Petkov33af05c2012-02-28 10:10:30 +010085 error->Populate(Error::kNotSupported);
86 return "/";
87}
88
Darin Petkovf3c71d72012-03-21 12:32:15 +010089bool VPNService::Load(StoreInterface *storage) {
90 return Service::Load(storage) &&
91 driver_->Load(storage, GetStorageIdentifier());
92}
93
94bool VPNService::Save(StoreInterface *storage) {
95 return Service::Save(storage) &&
Darin Petkovcb715292012-04-25 13:04:37 +020096 driver_->Save(storage, GetStorageIdentifier(), save_credentials());
Darin Petkovf3c71d72012-03-21 12:32:15 +010097}
98
Paul Stewart65512e12012-03-26 18:01:08 -070099bool VPNService::Unload() {
Darin Petkova0e645e2012-04-25 11:38:59 +0200100 // The base method also disconnects the service.
Paul Stewart65512e12012-03-26 18:01:08 -0700101 Service::Unload();
102
Darin Petkovcb715292012-04-25 13:04:37 +0200103 set_save_credentials(false);
104 driver_->UnloadCredentials();
105
Paul Stewart65512e12012-03-26 18:01:08 -0700106 // Ask the VPN provider to remove us from its list.
107 manager()->vpn_provider()->RemoveService(this);
108
109 return true;
110}
111
Paul Stewartebd38562012-03-23 13:06:40 -0700112void VPNService::InitDriverPropertyStore() {
113 driver_->InitPropertyStore(mutable_store());
114}
115
Darin Petkov1d0080a2012-04-30 17:10:36 +0200116void VPNService::MakeFavorite() {
117 // The base MakeFavorite method also sets auto_connect_ to true
118 // which is not desirable for VPN services.
mukesh agrawalcbfb34e2013-04-17 19:33:25 -0700119 MarkAsFavorite();
Darin Petkov1d0080a2012-04-30 17:10:36 +0200120}
121
Darin Petkov5eb05422012-05-11 15:45:25 +0200122void VPNService::SetConnection(const ConnectionRefPtr &connection) {
123 // Construct the connection binder here rather than in the constructor because
Darin Petkov457728b2013-01-09 09:49:08 +0100124 // 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 Petkov5eb05422012-05-11 15:45:25 +0200128 if (!connection_binder_.get()) {
129 connection_binder_.reset(
Darin Petkov457728b2013-01-09 09:49:08 +0100130 new Connection::Binder(unique_name(),
Darin Petkov5eb05422012-05-11 15:45:25 +0200131 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 Petkov79349f02013-01-24 16:18:26 +0100144bool 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 Petkov4cbff5b2013-01-29 16:29:05 +0100154 // 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 Petkov79349f02013-01-24 16:18:26 +0100159 return true;
160}
161
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700162bool VPNService::SetNameProperty(const string &name, Error *error) {
Darin Petkov9c6e9812013-03-26 13:49:07 +0100163 if (name == friendly_name()) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700164 return false;
Darin Petkov9c6e9812013-03-26 13:49:07 +0100165 }
166 LOG(INFO) << "Renaming service " << unique_name() << ": "
167 << friendly_name() << " -> " << name;
168
169 KeyValueStore *args = driver_->args();
Darin Petkov9c6e9812013-03-26 13:49:07 +0100170 args->SetString(flimflam::kNameProperty, name);
171 string new_storage_id = CreateStorageIdentifier(*args, error);
172 if (new_storage_id.empty()) {
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700173 return false;
Darin Petkov9c6e9812013-03-26 13:49:07 +0100174 }
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 agrawalbebf1b82013-04-23 15:06:33 -0700185 return true;
Darin Petkov9c6e9812013-03-26 13:49:07 +0100186}
187
Darin Petkov33af05c2012-02-28 10:10:30 +0100188} // namespace shill