Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [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_driver.h" |
| 6 | |
| 7 | #include <base/string_util.h> |
| 8 | #include <chromeos/dbus/service_constants.h> |
| 9 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 10 | #include "shill/connection.h" |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 11 | #include "shill/event_dispatcher.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 12 | #include "shill/logging.h" |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 13 | #include "shill/manager.h" |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 14 | #include "shill/property_accessor.h" |
| 15 | #include "shill/property_store.h" |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 16 | #include "shill/store_interface.h" |
| 17 | |
| 18 | using std::string; |
| 19 | |
| 20 | namespace shill { |
| 21 | |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 22 | // static |
| 23 | const int VPNDriver::kDefaultConnectTimeoutSeconds = 60; |
| 24 | |
| 25 | VPNDriver::VPNDriver(EventDispatcher *dispatcher, |
| 26 | Manager *manager, |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 27 | const Property *properties, |
| 28 | size_t property_count) |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 29 | : weak_ptr_factory_(this), |
| 30 | dispatcher_(dispatcher), |
| 31 | manager_(manager), |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 32 | properties_(properties), |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 33 | property_count_(property_count), |
| 34 | connect_timeout_seconds_(kDefaultConnectTimeoutSeconds) {} |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 35 | |
| 36 | VPNDriver::~VPNDriver() {} |
| 37 | |
| 38 | bool VPNDriver::Load(StoreInterface *storage, const string &storage_id) { |
| 39 | SLOG(VPN, 2) << __func__; |
| 40 | for (size_t i = 0; i < property_count_; i++) { |
| 41 | if ((properties_[i].flags & Property::kEphemeral)) { |
| 42 | continue; |
| 43 | } |
| 44 | const string property = properties_[i].property; |
| 45 | string value; |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 46 | bool loaded = (properties_[i].flags & Property::kCredential) ? |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 47 | storage->GetCryptedString(storage_id, property, &value) : |
| 48 | storage->GetString(storage_id, property, &value); |
| 49 | if (loaded) { |
| 50 | args_.SetString(property, value); |
| 51 | } else { |
| 52 | args_.RemoveString(property); |
| 53 | } |
| 54 | } |
| 55 | return true; |
| 56 | } |
| 57 | |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 58 | bool VPNDriver::Save(StoreInterface *storage, |
| 59 | const string &storage_id, |
| 60 | bool save_credentials) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 61 | SLOG(VPN, 2) << __func__; |
| 62 | for (size_t i = 0; i < property_count_; i++) { |
| 63 | if ((properties_[i].flags & Property::kEphemeral)) { |
| 64 | continue; |
| 65 | } |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 66 | bool credential = (properties_[i].flags & Property::kCredential); |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 67 | const string property = properties_[i].property; |
Darin Petkov | 4e9066f | 2012-06-11 13:17:06 +0200 | [diff] [blame] | 68 | if (!args_.ContainsString(property) || (credential && !save_credentials)) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 69 | storage->DeleteKey(storage_id, property); |
Darin Petkov | 4e9066f | 2012-06-11 13:17:06 +0200 | [diff] [blame] | 70 | continue; |
| 71 | } |
| 72 | string value = args_.GetString(property); |
| 73 | if (credential) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 74 | storage->SetCryptedString(storage_id, property, value); |
| 75 | } else { |
| 76 | storage->SetString(storage_id, property, value); |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 82 | void VPNDriver::UnloadCredentials() { |
| 83 | SLOG(VPN, 2) << __func__; |
| 84 | for (size_t i = 0; i < property_count_; i++) { |
| 85 | if ((properties_[i].flags & |
| 86 | (Property::kEphemeral | Property::kCredential))) { |
| 87 | args_.RemoveString(properties_[i].property); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 92 | void VPNDriver::InitPropertyStore(PropertyStore *store) { |
| 93 | SLOG(VPN, 2) << __func__; |
| 94 | for (size_t i = 0; i < property_count_; i++) { |
| 95 | store->RegisterDerivedString( |
| 96 | properties_[i].property, |
| 97 | StringAccessor( |
| 98 | new CustomMappedAccessor<VPNDriver, string, size_t>( |
| 99 | this, |
| 100 | &VPNDriver::ClearMappedProperty, |
| 101 | &VPNDriver::GetMappedProperty, |
| 102 | &VPNDriver::SetMappedProperty, |
| 103 | i))); |
| 104 | } |
| 105 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 106 | store->RegisterDerivedKeyValueStore( |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 107 | flimflam::kProviderProperty, |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 108 | KeyValueStoreAccessor( |
| 109 | new CustomAccessor<VPNDriver, KeyValueStore>( |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 110 | this, &VPNDriver::GetProvider, NULL))); |
| 111 | } |
| 112 | |
| 113 | void VPNDriver::ClearMappedProperty(const size_t &index, Error *error) { |
| 114 | CHECK(index < property_count_); |
| 115 | if (args_.ContainsString(properties_[index].property)) { |
| 116 | args_.RemoveString(properties_[index].property); |
| 117 | } else { |
| 118 | error->Populate(Error::kNotFound, "Property is not set"); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | string VPNDriver::GetMappedProperty(const size_t &index, Error *error) { |
| 123 | // Provider properties are set via SetProperty calls to "Provider.XXX", |
| 124 | // however, they are retrieved via a GetProperty call, which returns all |
| 125 | // properties in a single "Provider" dict. Therefore, none of the individual |
| 126 | // properties in the kProperties are available for enumeration in |
| 127 | // GetProperties. Instead, they are retrieved via GetProvider below. |
| 128 | error->Populate(Error::kInvalidArguments, |
| 129 | "Provider properties are not read back in this manner"); |
| 130 | return string(); |
| 131 | } |
| 132 | |
| 133 | void VPNDriver::SetMappedProperty( |
| 134 | const size_t &index, const string &value, Error *error) { |
| 135 | CHECK(index < property_count_); |
| 136 | args_.SetString(properties_[index].property, value); |
| 137 | } |
| 138 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 139 | KeyValueStore VPNDriver::GetProvider(Error *error) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 140 | SLOG(VPN, 2) << __func__; |
| 141 | string provider_prefix = string(flimflam::kProviderProperty) + "."; |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 142 | KeyValueStore provider_properties; |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 143 | |
| 144 | for (size_t i = 0; i < property_count_; i++) { |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 145 | if ((properties_[i].flags & Property::kWriteOnly)) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 146 | continue; |
| 147 | } |
Darin Petkov | 4e9066f | 2012-06-11 13:17:06 +0200 | [diff] [blame] | 148 | string prop = properties_[i].property; |
| 149 | if (!args_.ContainsString(prop)) { |
| 150 | continue; |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 151 | } |
Darin Petkov | 4e9066f | 2012-06-11 13:17:06 +0200 | [diff] [blame] | 152 | string value = args_.GetString(prop); |
| 153 | // Chomp off leading "Provider." from properties that have this prefix. |
| 154 | if (StartsWithASCII(prop, provider_prefix, false)) { |
| 155 | prop = prop.substr(provider_prefix.length()); |
| 156 | } |
| 157 | provider_properties.SetString(prop, value); |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return provider_properties; |
| 161 | } |
| 162 | |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 163 | void VPNDriver::StartConnectTimeout() { |
| 164 | SLOG(VPN, 2) << __func__; |
| 165 | if (IsConnectTimeoutStarted()) { |
| 166 | return; |
| 167 | } |
| 168 | connect_timeout_callback_.Reset( |
| 169 | Bind(&VPNDriver::OnConnectTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 170 | dispatcher_->PostDelayedTask( |
| 171 | connect_timeout_callback_.callback(), connect_timeout_seconds_ * 1000); |
| 172 | } |
| 173 | |
| 174 | void VPNDriver::StopConnectTimeout() { |
| 175 | SLOG(VPN, 2) << __func__; |
| 176 | connect_timeout_callback_.Cancel(); |
| 177 | } |
| 178 | |
| 179 | bool VPNDriver::IsConnectTimeoutStarted() const { |
| 180 | return !connect_timeout_callback_.IsCancelled(); |
| 181 | } |
| 182 | |
| 183 | void VPNDriver::OnConnectTimeout() { |
| 184 | LOG(ERROR) << "VPN connection timeout."; |
| 185 | StopConnectTimeout(); |
| 186 | OnConnectionDisconnected(); |
| 187 | } |
| 188 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 189 | } // namespace shill |