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" |
| 11 | #include "shill/manager.h" |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 12 | #include "shill/property_accessor.h" |
| 13 | #include "shill/property_store.h" |
| 14 | #include "shill/scope_logger.h" |
| 15 | #include "shill/store_interface.h" |
| 16 | |
| 17 | using std::string; |
| 18 | |
| 19 | namespace shill { |
| 20 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 21 | VPNDriver::VPNDriver(Manager *manager, |
| 22 | const Property *properties, |
| 23 | size_t property_count) |
| 24 | : manager_(manager), |
| 25 | properties_(properties), |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 26 | property_count_(property_count) {} |
| 27 | |
| 28 | VPNDriver::~VPNDriver() {} |
| 29 | |
| 30 | bool VPNDriver::Load(StoreInterface *storage, const string &storage_id) { |
| 31 | SLOG(VPN, 2) << __func__; |
| 32 | for (size_t i = 0; i < property_count_; i++) { |
| 33 | if ((properties_[i].flags & Property::kEphemeral)) { |
| 34 | continue; |
| 35 | } |
| 36 | const string property = properties_[i].property; |
| 37 | string value; |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 38 | bool loaded = (properties_[i].flags & Property::kCredential) ? |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 39 | storage->GetCryptedString(storage_id, property, &value) : |
| 40 | storage->GetString(storage_id, property, &value); |
| 41 | if (loaded) { |
| 42 | args_.SetString(property, value); |
| 43 | } else { |
| 44 | args_.RemoveString(property); |
| 45 | } |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 50 | bool VPNDriver::Save(StoreInterface *storage, |
| 51 | const string &storage_id, |
| 52 | bool save_credentials) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 53 | SLOG(VPN, 2) << __func__; |
| 54 | for (size_t i = 0; i < property_count_; i++) { |
| 55 | if ((properties_[i].flags & Property::kEphemeral)) { |
| 56 | continue; |
| 57 | } |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 58 | bool credential = (properties_[i].flags & Property::kCredential); |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 59 | const string property = properties_[i].property; |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 60 | string value; |
| 61 | if (!credential || save_credentials) { |
| 62 | value = args_.LookupString(property, ""); |
| 63 | } |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 64 | if (value.empty()) { |
| 65 | storage->DeleteKey(storage_id, property); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 66 | } else if (credential) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 67 | storage->SetCryptedString(storage_id, property, value); |
| 68 | } else { |
| 69 | storage->SetString(storage_id, property, value); |
| 70 | } |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 75 | void VPNDriver::UnloadCredentials() { |
| 76 | SLOG(VPN, 2) << __func__; |
| 77 | for (size_t i = 0; i < property_count_; i++) { |
| 78 | if ((properties_[i].flags & |
| 79 | (Property::kEphemeral | Property::kCredential))) { |
| 80 | args_.RemoveString(properties_[i].property); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 85 | void VPNDriver::InitPropertyStore(PropertyStore *store) { |
| 86 | SLOG(VPN, 2) << __func__; |
| 87 | for (size_t i = 0; i < property_count_; i++) { |
| 88 | store->RegisterDerivedString( |
| 89 | properties_[i].property, |
| 90 | StringAccessor( |
| 91 | new CustomMappedAccessor<VPNDriver, string, size_t>( |
| 92 | this, |
| 93 | &VPNDriver::ClearMappedProperty, |
| 94 | &VPNDriver::GetMappedProperty, |
| 95 | &VPNDriver::SetMappedProperty, |
| 96 | i))); |
| 97 | } |
| 98 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 99 | store->RegisterDerivedKeyValueStore( |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 100 | flimflam::kProviderProperty, |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 101 | KeyValueStoreAccessor( |
| 102 | new CustomAccessor<VPNDriver, KeyValueStore>( |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 103 | this, &VPNDriver::GetProvider, NULL))); |
| 104 | } |
| 105 | |
| 106 | void VPNDriver::ClearMappedProperty(const size_t &index, Error *error) { |
| 107 | CHECK(index < property_count_); |
| 108 | if (args_.ContainsString(properties_[index].property)) { |
| 109 | args_.RemoveString(properties_[index].property); |
| 110 | } else { |
| 111 | error->Populate(Error::kNotFound, "Property is not set"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | string VPNDriver::GetMappedProperty(const size_t &index, Error *error) { |
| 116 | // Provider properties are set via SetProperty calls to "Provider.XXX", |
| 117 | // however, they are retrieved via a GetProperty call, which returns all |
| 118 | // properties in a single "Provider" dict. Therefore, none of the individual |
| 119 | // properties in the kProperties are available for enumeration in |
| 120 | // GetProperties. Instead, they are retrieved via GetProvider below. |
| 121 | error->Populate(Error::kInvalidArguments, |
| 122 | "Provider properties are not read back in this manner"); |
| 123 | return string(); |
| 124 | } |
| 125 | |
| 126 | void VPNDriver::SetMappedProperty( |
| 127 | const size_t &index, const string &value, Error *error) { |
| 128 | CHECK(index < property_count_); |
| 129 | args_.SetString(properties_[index].property, value); |
| 130 | } |
| 131 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 132 | KeyValueStore VPNDriver::GetProvider(Error *error) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 133 | SLOG(VPN, 2) << __func__; |
| 134 | string provider_prefix = string(flimflam::kProviderProperty) + "."; |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 135 | KeyValueStore provider_properties; |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 136 | |
| 137 | for (size_t i = 0; i < property_count_; i++) { |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 138 | if ((properties_[i].flags & Property::kWriteOnly)) { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 139 | continue; |
| 140 | } |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 141 | string value = args_.LookupString(properties_[i].property, ""); |
| 142 | if (!value.empty()) { |
| 143 | // Chomp off leading "Provider." from properties that have this prefix. |
| 144 | string prop = properties_[i].property; |
| 145 | if (StartsWithASCII(prop, provider_prefix, false)) { |
| 146 | prop = prop.substr(provider_prefix.length()); |
| 147 | } |
| 148 | provider_properties.SetString(prop, value); |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | return provider_properties; |
| 153 | } |
| 154 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 155 | } // namespace shill |