blob: 17d32370d88d7804fe5cfac14f7b8af96a54bde1 [file] [log] [blame]
Darin Petkovb451d6e2012-04-23 11:56:41 +02001// 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 Petkov0e9735d2012-04-24 12:33:45 +020010#include "shill/connection.h"
11#include "shill/manager.h"
Darin Petkovb451d6e2012-04-23 11:56:41 +020012#include "shill/property_accessor.h"
13#include "shill/property_store.h"
14#include "shill/scope_logger.h"
15#include "shill/store_interface.h"
16
17using std::string;
18
19namespace shill {
20
Darin Petkov0e9735d2012-04-24 12:33:45 +020021VPNDriver::VPNDriver(Manager *manager,
22 const Property *properties,
23 size_t property_count)
24 : manager_(manager),
25 properties_(properties),
Darin Petkovb451d6e2012-04-23 11:56:41 +020026 property_count_(property_count) {}
27
28VPNDriver::~VPNDriver() {}
29
30bool 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 Petkovcb715292012-04-25 13:04:37 +020038 bool loaded = (properties_[i].flags & Property::kCredential) ?
Darin Petkovb451d6e2012-04-23 11:56:41 +020039 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 Petkovcb715292012-04-25 13:04:37 +020050bool VPNDriver::Save(StoreInterface *storage,
51 const string &storage_id,
52 bool save_credentials) {
Darin Petkovb451d6e2012-04-23 11:56:41 +020053 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 Petkovcb715292012-04-25 13:04:37 +020058 bool credential = (properties_[i].flags & Property::kCredential);
Darin Petkovb451d6e2012-04-23 11:56:41 +020059 const string property = properties_[i].property;
Darin Petkovcb715292012-04-25 13:04:37 +020060 string value;
61 if (!credential || save_credentials) {
62 value = args_.LookupString(property, "");
63 }
Darin Petkovb451d6e2012-04-23 11:56:41 +020064 if (value.empty()) {
65 storage->DeleteKey(storage_id, property);
Darin Petkovcb715292012-04-25 13:04:37 +020066 } else if (credential) {
Darin Petkovb451d6e2012-04-23 11:56:41 +020067 storage->SetCryptedString(storage_id, property, value);
68 } else {
69 storage->SetString(storage_id, property, value);
70 }
71 }
72 return true;
73}
74
Darin Petkovcb715292012-04-25 13:04:37 +020075void 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 Petkovb451d6e2012-04-23 11:56:41 +020085void 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 Petkovb536a742012-04-26 11:31:28 +020099 store->RegisterDerivedKeyValueStore(
Darin Petkovb451d6e2012-04-23 11:56:41 +0200100 flimflam::kProviderProperty,
Darin Petkovb536a742012-04-26 11:31:28 +0200101 KeyValueStoreAccessor(
102 new CustomAccessor<VPNDriver, KeyValueStore>(
Darin Petkovb451d6e2012-04-23 11:56:41 +0200103 this, &VPNDriver::GetProvider, NULL)));
104}
105
106void 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
115string 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
126void 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 Petkovb536a742012-04-26 11:31:28 +0200132KeyValueStore VPNDriver::GetProvider(Error *error) {
Darin Petkovb451d6e2012-04-23 11:56:41 +0200133 SLOG(VPN, 2) << __func__;
134 string provider_prefix = string(flimflam::kProviderProperty) + ".";
Darin Petkovb536a742012-04-26 11:31:28 +0200135 KeyValueStore provider_properties;
Darin Petkovb451d6e2012-04-23 11:56:41 +0200136
137 for (size_t i = 0; i < property_count_; i++) {
Darin Petkovcb715292012-04-25 13:04:37 +0200138 if ((properties_[i].flags & Property::kWriteOnly)) {
Darin Petkovb451d6e2012-04-23 11:56:41 +0200139 continue;
140 }
Darin Petkovb536a742012-04-26 11:31:28 +0200141 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 Petkovb451d6e2012-04-23 11:56:41 +0200149 }
150 }
151
152 return provider_properties;
153}
154
Darin Petkovb451d6e2012-04-23 11:56:41 +0200155} // namespace shill