blob: b88b6792b858d4535e09947f923e50b849c2cb7f [file] [log] [blame]
Darin Petkove7c6ad32012-06-29 10:22:09 +02001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone52cd19b2011-06-29 17:23:04 -07002// 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/profile.h"
6
Chris Masone6515aab2011-10-12 16:19:09 -07007#include <set>
Chris Masone52cd19b2011-06-29 17:23:04 -07008#include <string>
Chris Masone6791a432011-07-12 13:23:19 -07009#include <vector>
Chris Masone52cd19b2011-06-29 17:23:04 -070010
Chris Masoneb9c00592011-10-06 13:10:39 -070011#include <base/file_path.h>
Paul Stewarte73d05c2012-03-29 16:26:05 -070012#include <base/file_util.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050013#include <base/stl_util.h>
Darin Petkova4766822011-07-07 10:42:22 -070014#include <base/string_util.h>
Chris Masone6791a432011-07-12 13:23:19 -070015#include <base/stringprintf.h>
Chris Masone88cbd5f2011-07-03 14:30:04 -070016#include <chromeos/dbus/service_constants.h>
Chris Masone52cd19b2011-06-29 17:23:04 -070017
18#include "shill/adaptor_interfaces.h"
19#include "shill/control_interface.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070020#include "shill/key_file_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070021#include "shill/logging.h"
Chris Masone6791a432011-07-12 13:23:19 -070022#include "shill/manager.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070023#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070024#include "shill/service.h"
Chris Masone9d779932011-08-25 16:33:41 -070025#include "shill/store_interface.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070026
Chris Masone6515aab2011-10-12 16:19:09 -070027using std::set;
Chris Masone52cd19b2011-06-29 17:23:04 -070028using std::string;
Chris Masone6791a432011-07-12 13:23:19 -070029using std::vector;
Chris Masone52cd19b2011-06-29 17:23:04 -070030
31namespace shill {
Darin Petkova4766822011-07-07 10:42:22 -070032
Darin Petkova4766822011-07-07 10:42:22 -070033Profile::Profile(ControlInterface *control_interface,
Thieu Le5133b712013-02-19 14:47:21 -080034 Metrics *metrics,
Chris Masone7df0c672011-07-15 10:24:54 -070035 Manager *manager,
36 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070037 const string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070038 bool connect_to_rpc)
Thieu Le5133b712013-02-19 14:47:21 -080039 : metrics_(metrics),
40 manager_(manager),
Chris Masone7df0c672011-07-15 10:24:54 -070041 name_(name),
Chris Masone2ae797d2011-08-23 20:41:00 -070042 storage_format_(user_storage_format) {
Chris Masone7df0c672011-07-15 10:24:54 -070043 if (connect_to_rpc)
44 adaptor_.reset(control_interface->CreateProfileAdaptor(this));
45
Chris Masone88cbd5f2011-07-03 14:30:04 -070046 // flimflam::kCheckPortalListProperty: Registered in DefaultProfile
47 // flimflam::kCountryProperty: Registered in DefaultProfile
Chris Masone7df0c672011-07-15 10:24:54 -070048 store_.RegisterConstString(flimflam::kNameProperty, &name_.identifier);
Chris Masone88cbd5f2011-07-03 14:30:04 -070049
50 // flimflam::kOfflineModeProperty: Registered in DefaultProfile
51 // flimflam::kPortalURLProperty: Registered in DefaultProfile
52
Chris Masone6791a432011-07-12 13:23:19 -070053 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
54 &Profile::EnumerateAvailableServices,
55 NULL);
Chris Masone7df0c672011-07-15 10:24:54 -070056 HelpRegisterDerivedStrings(flimflam::kEntriesProperty,
57 &Profile::EnumerateEntries,
58 NULL);
Chris Masone52cd19b2011-06-29 17:23:04 -070059}
60
Chris Masone6515aab2011-10-12 16:19:09 -070061Profile::~Profile() {}
Chris Masone52cd19b2011-06-29 17:23:04 -070062
Paul Stewart5dc40aa2011-10-28 19:43:43 -070063bool Profile::InitStorage(GLib *glib, InitStorageOption storage_option,
64 Error *error) {
Chris Masoneb9c00592011-10-06 13:10:39 -070065 FilePath final_path;
66 if (!GetStoragePath(&final_path)) {
Paul Stewartbe005172011-11-02 18:10:29 -070067 Error::PopulateAndLog(error, Error::kInvalidArguments,
Chris Masoneb9c00592011-10-06 13:10:39 -070068 base::StringPrintf("Could not set up profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070069 name_.user.c_str(), name_.identifier.c_str()));
Chris Masoneb9c00592011-10-06 13:10:39 -070070 return false;
71 }
72 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
73 storage->set_path(final_path);
Paul Stewart5dc40aa2011-10-28 19:43:43 -070074 bool already_exists = storage->IsNonEmpty();
75 if (!already_exists && storage_option != kCreateNew &&
76 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070077 Error::PopulateAndLog(error, Error::kNotFound,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070078 base::StringPrintf("Profile storage for %s:%s does not already exist",
Paul Stewartbe005172011-11-02 18:10:29 -070079 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070080 return false;
81 } else if (already_exists && storage_option != kOpenExisting &&
82 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070083 Error::PopulateAndLog(error, Error::kAlreadyExists,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070084 base::StringPrintf("Profile storage for %s:%s already exists",
Paul Stewartbe005172011-11-02 18:10:29 -070085 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070086 return false;
87 }
Chris Masoneb9c00592011-10-06 13:10:39 -070088 if (!storage->Open()) {
Paul Stewartbe005172011-11-02 18:10:29 -070089 Error::PopulateAndLog(error, Error::kInternalError,
Chris Masoneb9c00592011-10-06 13:10:39 -070090 base::StringPrintf("Could not open profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070091 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart2ebc16d2012-08-23 10:38:39 -070092 if (already_exists) {
93 // The profile contents is corrupt, or we do not have access to
94 // this file. Move this file out of the way so a future open attempt
95 // will succeed, assuming the failure reason was the former.
96 storage->MarkAsCorrupted();
Thieu Le5133b712013-02-19 14:47:21 -080097 metrics_->NotifyCorruptedProfile();
Paul Stewart2ebc16d2012-08-23 10:38:39 -070098 }
Chris Masoneb9c00592011-10-06 13:10:39 -070099 return false;
100 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700101 if (!already_exists) {
102 // Add a descriptive header to the profile so even if nothing is stored
103 // to it, it still has some content. Completely empty keyfiles are not
104 // valid for reading.
105 storage->SetHeader(
106 base::StringPrintf("Profile %s:%s", name_.user.c_str(),
107 name_.identifier.c_str()));
108 }
Chris Masoneb9c00592011-10-06 13:10:39 -0700109 set_storage(storage.release());
Paul Stewart3c504012013-01-17 17:49:58 -0800110 manager_->OnProfileStorageInitialized(storage_.get());
Chris Masoneb9c00592011-10-06 13:10:39 -0700111 return true;
112}
113
Paul Stewarte73d05c2012-03-29 16:26:05 -0700114bool Profile::RemoveStorage(GLib *glib, Error *error) {
115 FilePath path;
116
117 CHECK(!storage_.get());
118
119 if (!GetStoragePath(&path)) {
120 Error::PopulateAndLog(
121 error, Error::kInvalidArguments,
122 base::StringPrintf("Could get the storage path for %s:%s",
123 name_.user.c_str(), name_.identifier.c_str()));
124 return false;
125 }
126
127 if (!file_util::Delete(path, false)) {
128 Error::PopulateAndLog(
129 error, Error::kOperationFailed,
130 base::StringPrintf("Could not remove path %s", path.value().c_str()));
131 return false;
132 }
133
134 return true;
135}
136
Chris Masone7df0c672011-07-15 10:24:54 -0700137string Profile::GetFriendlyName() {
138 return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier;
139}
140
141string Profile::GetRpcIdentifier() {
Paul Stewart19c871d2011-12-15 16:10:13 -0800142 if (!adaptor_.get()) {
143 // NB: This condition happens in unit tests.
144 return string();
145 }
Chris Masone7df0c672011-07-15 10:24:54 -0700146 return adaptor_->GetRpcIdentifier();
147}
148
Chris Masoneb9c00592011-10-06 13:10:39 -0700149void Profile::set_storage(StoreInterface *storage) {
150 storage_.reset(storage);
151}
152
Chris Masone6791a432011-07-12 13:23:19 -0700153bool Profile::AdoptService(const ServiceRefPtr &service) {
Paul Stewart451aa7f2012-04-11 19:07:58 -0700154 if (service->profile() == this) {
Chris Masone6791a432011-07-12 13:23:19 -0700155 return false;
Paul Stewart451aa7f2012-04-11 19:07:58 -0700156 }
Philipp Neubeck79173602012-11-13 21:10:09 +0100157 service->SetProfile(this);
Chris Masone6515aab2011-10-12 16:19:09 -0700158 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700159}
160
Chris Masone6515aab2011-10-12 16:19:09 -0700161bool Profile::AbandonService(const ServiceRefPtr &service) {
162 if (service->profile() == this)
Philipp Neubeck79173602012-11-13 21:10:09 +0100163 service->SetProfile(NULL);
Chris Masone6515aab2011-10-12 16:19:09 -0700164 return storage_->DeleteGroup(service->GetStorageIdentifier()) &&
165 storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700166}
167
Chris Masone6515aab2011-10-12 16:19:09 -0700168bool Profile::UpdateService(const ServiceRefPtr &service) {
169 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700170}
171
Paul Stewart2c575d22012-12-07 12:28:57 -0800172bool Profile::LoadService(const ServiceRefPtr &service) {
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700173 if (!ContainsService(service))
Chris Masone6515aab2011-10-12 16:19:09 -0700174 return false;
Chris Masone6515aab2011-10-12 16:19:09 -0700175 return service->Load(storage_.get());
Chris Masone6791a432011-07-12 13:23:19 -0700176}
177
Paul Stewart2c575d22012-12-07 12:28:57 -0800178bool Profile::ConfigureService(const ServiceRefPtr &service) {
179 if (!LoadService(service))
180 return false;
181 service->SetProfile(this);
182 return true;
183}
184
Paul Stewarta41e38d2011-11-11 07:47:29 -0800185bool Profile::ConfigureDevice(const DeviceRefPtr &device) {
186 return device->Load(storage_.get());
187}
188
Chris Masone6515aab2011-10-12 16:19:09 -0700189bool Profile::ContainsService(const ServiceConstRefPtr &service) {
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700190 return service->IsLoadableFrom(storage_.get());
Chris Masone7aa5f902011-07-11 11:13:35 -0700191}
192
Paul Stewart75225512012-01-26 22:51:33 -0800193void Profile::DeleteEntry(const std::string &entry_name, Error *error) {
194 if (!storage_->ContainsGroup(entry_name)) {
195 Error::PopulateAndLog(error, Error::kNotFound,
196 base::StringPrintf("Entry %s does not exist in profile",
197 entry_name.c_str()));
198 return;
199 }
200 if (!manager_->HandleProfileEntryDeletion(this, entry_name)) {
201 // If HandleProfileEntryDeletion() returns succeeds, DeleteGroup()
202 // has already been called when AbandonService was called.
203 // Otherwise, we need to delete the group ourselves.
204 storage_->DeleteGroup(entry_name);
205 }
Paul Stewart0756db92012-01-27 08:34:47 -0800206 Save();
207}
208
209ServiceRefPtr Profile::GetServiceFromEntry(const std::string &entry_name,
210 Error *error) {
211 return manager_->GetServiceWithStorageIdentifier(this, entry_name, error);
Paul Stewart75225512012-01-26 22:51:33 -0800212}
213
Paul Stewarta41e38d2011-11-11 07:47:29 -0800214bool Profile::IsValidIdentifierToken(const string &token) {
Darin Petkova4766822011-07-07 10:42:22 -0700215 if (token.empty()) {
216 return false;
217 }
218 for (string::const_iterator it = token.begin(); it != token.end(); ++it) {
219 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) {
220 return false;
221 }
222 }
223 return true;
224}
225
226bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) {
227 if (raw.empty()) {
228 return false;
229 }
230 if (raw[0] == '~') {
231 // Format: "~user/identifier".
232 size_t slash = raw.find('/');
233 if (slash == string::npos) {
234 return false;
235 }
236 string user(raw.begin() + 1, raw.begin() + slash);
237 string identifier(raw.begin() + slash + 1, raw.end());
238 if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) {
239 return false;
240 }
241 parsed->user = user;
242 parsed->identifier = identifier;
243 return true;
244 }
245
246 // Format: "identifier".
247 if (!IsValidIdentifierToken(raw)) {
248 return false;
249 }
250 parsed->user = "";
251 parsed->identifier = raw;
252 return true;
253}
254
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700255bool Profile::MatchesIdentifier(const Identifier &name) const {
256 return name.user == name_.user && name.identifier == name_.identifier;
257}
258
Chris Masoneb9c00592011-10-06 13:10:39 -0700259bool Profile::Save() {
Chris Masone6515aab2011-10-12 16:19:09 -0700260 return storage_->Flush();
Chris Masone9d779932011-08-25 16:33:41 -0700261}
262
Chris Masone2ae797d2011-08-23 20:41:00 -0700263bool Profile::GetStoragePath(FilePath *path) {
264 if (name_.user.empty()) {
265 LOG(ERROR) << "Non-default profiles cannot be stored globally.";
266 return false;
267 }
268 FilePath dir(base::StringPrintf(storage_format_.c_str(), name_.user.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700269 // TODO(petkov): Validate the directory permissions, etc.
270 *path = dir.Append(base::StringPrintf("%s.profile",
Chris Masone2ae797d2011-08-23 20:41:00 -0700271 name_.identifier.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700272 return true;
273}
274
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800275vector<string> Profile::EnumerateAvailableServices(Error *error) {
Paul Stewart1b253142012-01-26 14:05:52 -0800276 // We should return the Manager's service list if this is the active profile.
277 if (manager_->IsActiveProfile(this)) {
278 return manager_->EnumerateAvailableServices(error);
279 } else {
280 return vector<string>();
281 }
Chris Masone6791a432011-07-12 13:23:19 -0700282}
283
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800284vector<string> Profile::EnumerateEntries(Error */*error*/) {
Chris Masone6515aab2011-10-12 16:19:09 -0700285 set<string> groups(storage_->GetGroups());
Paul Stewart1b253142012-01-26 14:05:52 -0800286 vector<string> service_groups;
287
288 // Filter this list down to only entries that correspond
289 // to a technology. (wifi_*, etc)
290 for (set<string>::iterator it = groups.begin();
291 it != groups.end(); ++it) {
292 if (Technology::IdentifierFromStorageGroup(*it) != Technology::kUnknown)
293 service_groups.push_back(*it);
294 }
295
296 return service_groups;
Chris Masone6791a432011-07-12 13:23:19 -0700297}
298
Darin Petkove7c6ad32012-06-29 10:22:09 +0200299bool Profile::UpdateDevice(const DeviceRefPtr &device) {
300 return false;
301}
302
mukesh agrawalffa3d042011-10-06 15:26:10 -0700303void Profile::HelpRegisterDerivedStrings(
304 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800305 Strings(Profile::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700306 void(Profile::*set)(const Strings&, Error *)) {
Chris Masone6791a432011-07-12 13:23:19 -0700307 store_.RegisterDerivedStrings(
308 name,
309 StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set)));
310}
311
Chris Masone52cd19b2011-06-29 17:23:04 -0700312} // namespace shill