blob: 6aa0406a2d7608a3e635607336e94fa492dcf90b [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
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080027using base::FilePath;
Chris Masone6515aab2011-10-12 16:19:09 -070028using std::set;
Chris Masone52cd19b2011-06-29 17:23:04 -070029using std::string;
Chris Masone6791a432011-07-12 13:23:19 -070030using std::vector;
Chris Masone52cd19b2011-06-29 17:23:04 -070031
32namespace shill {
Darin Petkova4766822011-07-07 10:42:22 -070033
Darin Petkova4766822011-07-07 10:42:22 -070034Profile::Profile(ControlInterface *control_interface,
Thieu Le5133b712013-02-19 14:47:21 -080035 Metrics *metrics,
Chris Masone7df0c672011-07-15 10:24:54 -070036 Manager *manager,
37 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070038 const string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070039 bool connect_to_rpc)
Thieu Le5133b712013-02-19 14:47:21 -080040 : metrics_(metrics),
41 manager_(manager),
Chris Masone7df0c672011-07-15 10:24:54 -070042 name_(name),
Chris Masone2ae797d2011-08-23 20:41:00 -070043 storage_format_(user_storage_format) {
Chris Masone7df0c672011-07-15 10:24:54 -070044 if (connect_to_rpc)
45 adaptor_.reset(control_interface->CreateProfileAdaptor(this));
46
Chris Masone88cbd5f2011-07-03 14:30:04 -070047 // flimflam::kCheckPortalListProperty: Registered in DefaultProfile
48 // flimflam::kCountryProperty: Registered in DefaultProfile
Chris Masone7df0c672011-07-15 10:24:54 -070049 store_.RegisterConstString(flimflam::kNameProperty, &name_.identifier);
Chris Masone88cbd5f2011-07-03 14:30:04 -070050
51 // flimflam::kOfflineModeProperty: Registered in DefaultProfile
52 // flimflam::kPortalURLProperty: Registered in DefaultProfile
53
Chris Masone6791a432011-07-12 13:23:19 -070054 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
55 &Profile::EnumerateAvailableServices,
56 NULL);
Chris Masone7df0c672011-07-15 10:24:54 -070057 HelpRegisterDerivedStrings(flimflam::kEntriesProperty,
58 &Profile::EnumerateEntries,
59 NULL);
Chris Masone52cd19b2011-06-29 17:23:04 -070060}
61
Chris Masone6515aab2011-10-12 16:19:09 -070062Profile::~Profile() {}
Chris Masone52cd19b2011-06-29 17:23:04 -070063
Paul Stewart5dc40aa2011-10-28 19:43:43 -070064bool Profile::InitStorage(GLib *glib, InitStorageOption storage_option,
65 Error *error) {
Chris Masoneb9c00592011-10-06 13:10:39 -070066 FilePath final_path;
67 if (!GetStoragePath(&final_path)) {
Paul Stewartbe005172011-11-02 18:10:29 -070068 Error::PopulateAndLog(error, Error::kInvalidArguments,
Chris Masoneb9c00592011-10-06 13:10:39 -070069 base::StringPrintf("Could not set up profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070070 name_.user.c_str(), name_.identifier.c_str()));
Chris Masoneb9c00592011-10-06 13:10:39 -070071 return false;
72 }
73 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
74 storage->set_path(final_path);
Paul Stewart5dc40aa2011-10-28 19:43:43 -070075 bool already_exists = storage->IsNonEmpty();
76 if (!already_exists && storage_option != kCreateNew &&
77 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070078 Error::PopulateAndLog(error, Error::kNotFound,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070079 base::StringPrintf("Profile storage for %s:%s does not already exist",
Paul Stewartbe005172011-11-02 18:10:29 -070080 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070081 return false;
82 } else if (already_exists && storage_option != kOpenExisting &&
83 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070084 Error::PopulateAndLog(error, Error::kAlreadyExists,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070085 base::StringPrintf("Profile storage for %s:%s already exists",
Paul Stewartbe005172011-11-02 18:10:29 -070086 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070087 return false;
88 }
Chris Masoneb9c00592011-10-06 13:10:39 -070089 if (!storage->Open()) {
Paul Stewartbe005172011-11-02 18:10:29 -070090 Error::PopulateAndLog(error, Error::kInternalError,
Chris Masoneb9c00592011-10-06 13:10:39 -070091 base::StringPrintf("Could not open profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070092 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart2ebc16d2012-08-23 10:38:39 -070093 if (already_exists) {
94 // The profile contents is corrupt, or we do not have access to
95 // this file. Move this file out of the way so a future open attempt
96 // will succeed, assuming the failure reason was the former.
97 storage->MarkAsCorrupted();
Thieu Le5133b712013-02-19 14:47:21 -080098 metrics_->NotifyCorruptedProfile();
Paul Stewart2ebc16d2012-08-23 10:38:39 -070099 }
Chris Masoneb9c00592011-10-06 13:10:39 -0700100 return false;
101 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700102 if (!already_exists) {
103 // Add a descriptive header to the profile so even if nothing is stored
104 // to it, it still has some content. Completely empty keyfiles are not
105 // valid for reading.
106 storage->SetHeader(
107 base::StringPrintf("Profile %s:%s", name_.user.c_str(),
108 name_.identifier.c_str()));
109 }
Chris Masoneb9c00592011-10-06 13:10:39 -0700110 set_storage(storage.release());
Paul Stewart3c504012013-01-17 17:49:58 -0800111 manager_->OnProfileStorageInitialized(storage_.get());
Chris Masoneb9c00592011-10-06 13:10:39 -0700112 return true;
113}
114
Paul Stewarte73d05c2012-03-29 16:26:05 -0700115bool Profile::RemoveStorage(GLib *glib, Error *error) {
116 FilePath path;
117
118 CHECK(!storage_.get());
119
120 if (!GetStoragePath(&path)) {
121 Error::PopulateAndLog(
122 error, Error::kInvalidArguments,
123 base::StringPrintf("Could get the storage path for %s:%s",
124 name_.user.c_str(), name_.identifier.c_str()));
125 return false;
126 }
127
128 if (!file_util::Delete(path, false)) {
129 Error::PopulateAndLog(
130 error, Error::kOperationFailed,
131 base::StringPrintf("Could not remove path %s", path.value().c_str()));
132 return false;
133 }
134
135 return true;
136}
137
Chris Masone7df0c672011-07-15 10:24:54 -0700138string Profile::GetFriendlyName() {
139 return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier;
140}
141
142string Profile::GetRpcIdentifier() {
Paul Stewart19c871d2011-12-15 16:10:13 -0800143 if (!adaptor_.get()) {
144 // NB: This condition happens in unit tests.
145 return string();
146 }
Chris Masone7df0c672011-07-15 10:24:54 -0700147 return adaptor_->GetRpcIdentifier();
148}
149
Chris Masoneb9c00592011-10-06 13:10:39 -0700150void Profile::set_storage(StoreInterface *storage) {
151 storage_.reset(storage);
152}
153
Chris Masone6791a432011-07-12 13:23:19 -0700154bool Profile::AdoptService(const ServiceRefPtr &service) {
Paul Stewart451aa7f2012-04-11 19:07:58 -0700155 if (service->profile() == this) {
Chris Masone6791a432011-07-12 13:23:19 -0700156 return false;
Paul Stewart451aa7f2012-04-11 19:07:58 -0700157 }
Philipp Neubeck79173602012-11-13 21:10:09 +0100158 service->SetProfile(this);
Chris Masone6515aab2011-10-12 16:19:09 -0700159 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700160}
161
Chris Masone6515aab2011-10-12 16:19:09 -0700162bool Profile::AbandonService(const ServiceRefPtr &service) {
163 if (service->profile() == this)
Philipp Neubeck79173602012-11-13 21:10:09 +0100164 service->SetProfile(NULL);
Chris Masone6515aab2011-10-12 16:19:09 -0700165 return storage_->DeleteGroup(service->GetStorageIdentifier()) &&
166 storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700167}
168
Chris Masone6515aab2011-10-12 16:19:09 -0700169bool Profile::UpdateService(const ServiceRefPtr &service) {
170 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700171}
172
Paul Stewart2c575d22012-12-07 12:28:57 -0800173bool Profile::LoadService(const ServiceRefPtr &service) {
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700174 if (!ContainsService(service))
Chris Masone6515aab2011-10-12 16:19:09 -0700175 return false;
Chris Masone6515aab2011-10-12 16:19:09 -0700176 return service->Load(storage_.get());
Chris Masone6791a432011-07-12 13:23:19 -0700177}
178
Paul Stewart2c575d22012-12-07 12:28:57 -0800179bool Profile::ConfigureService(const ServiceRefPtr &service) {
180 if (!LoadService(service))
181 return false;
182 service->SetProfile(this);
183 return true;
184}
185
Paul Stewarta41e38d2011-11-11 07:47:29 -0800186bool Profile::ConfigureDevice(const DeviceRefPtr &device) {
187 return device->Load(storage_.get());
188}
189
Chris Masone6515aab2011-10-12 16:19:09 -0700190bool Profile::ContainsService(const ServiceConstRefPtr &service) {
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700191 return service->IsLoadableFrom(storage_.get());
Chris Masone7aa5f902011-07-11 11:13:35 -0700192}
193
Paul Stewart75225512012-01-26 22:51:33 -0800194void Profile::DeleteEntry(const std::string &entry_name, Error *error) {
195 if (!storage_->ContainsGroup(entry_name)) {
196 Error::PopulateAndLog(error, Error::kNotFound,
197 base::StringPrintf("Entry %s does not exist in profile",
198 entry_name.c_str()));
199 return;
200 }
201 if (!manager_->HandleProfileEntryDeletion(this, entry_name)) {
202 // If HandleProfileEntryDeletion() returns succeeds, DeleteGroup()
203 // has already been called when AbandonService was called.
204 // Otherwise, we need to delete the group ourselves.
205 storage_->DeleteGroup(entry_name);
206 }
Paul Stewart0756db92012-01-27 08:34:47 -0800207 Save();
208}
209
210ServiceRefPtr Profile::GetServiceFromEntry(const std::string &entry_name,
211 Error *error) {
212 return manager_->GetServiceWithStorageIdentifier(this, entry_name, error);
Paul Stewart75225512012-01-26 22:51:33 -0800213}
214
Paul Stewarta41e38d2011-11-11 07:47:29 -0800215bool Profile::IsValidIdentifierToken(const string &token) {
Darin Petkova4766822011-07-07 10:42:22 -0700216 if (token.empty()) {
217 return false;
218 }
219 for (string::const_iterator it = token.begin(); it != token.end(); ++it) {
220 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) {
221 return false;
222 }
223 }
224 return true;
225}
226
227bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) {
228 if (raw.empty()) {
229 return false;
230 }
231 if (raw[0] == '~') {
232 // Format: "~user/identifier".
233 size_t slash = raw.find('/');
234 if (slash == string::npos) {
235 return false;
236 }
237 string user(raw.begin() + 1, raw.begin() + slash);
238 string identifier(raw.begin() + slash + 1, raw.end());
239 if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) {
240 return false;
241 }
242 parsed->user = user;
243 parsed->identifier = identifier;
244 return true;
245 }
246
247 // Format: "identifier".
248 if (!IsValidIdentifierToken(raw)) {
249 return false;
250 }
251 parsed->user = "";
252 parsed->identifier = raw;
253 return true;
254}
255
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700256bool Profile::MatchesIdentifier(const Identifier &name) const {
257 return name.user == name_.user && name.identifier == name_.identifier;
258}
259
Chris Masoneb9c00592011-10-06 13:10:39 -0700260bool Profile::Save() {
Chris Masone6515aab2011-10-12 16:19:09 -0700261 return storage_->Flush();
Chris Masone9d779932011-08-25 16:33:41 -0700262}
263
Chris Masone2ae797d2011-08-23 20:41:00 -0700264bool Profile::GetStoragePath(FilePath *path) {
265 if (name_.user.empty()) {
266 LOG(ERROR) << "Non-default profiles cannot be stored globally.";
267 return false;
268 }
269 FilePath dir(base::StringPrintf(storage_format_.c_str(), name_.user.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700270 // TODO(petkov): Validate the directory permissions, etc.
271 *path = dir.Append(base::StringPrintf("%s.profile",
Chris Masone2ae797d2011-08-23 20:41:00 -0700272 name_.identifier.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700273 return true;
274}
275
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800276vector<string> Profile::EnumerateAvailableServices(Error *error) {
Paul Stewart1b253142012-01-26 14:05:52 -0800277 // We should return the Manager's service list if this is the active profile.
278 if (manager_->IsActiveProfile(this)) {
279 return manager_->EnumerateAvailableServices(error);
280 } else {
281 return vector<string>();
282 }
Chris Masone6791a432011-07-12 13:23:19 -0700283}
284
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800285vector<string> Profile::EnumerateEntries(Error */*error*/) {
Chris Masone6515aab2011-10-12 16:19:09 -0700286 set<string> groups(storage_->GetGroups());
Paul Stewart1b253142012-01-26 14:05:52 -0800287 vector<string> service_groups;
288
289 // Filter this list down to only entries that correspond
290 // to a technology. (wifi_*, etc)
291 for (set<string>::iterator it = groups.begin();
292 it != groups.end(); ++it) {
293 if (Technology::IdentifierFromStorageGroup(*it) != Technology::kUnknown)
294 service_groups.push_back(*it);
295 }
296
297 return service_groups;
Chris Masone6791a432011-07-12 13:23:19 -0700298}
299
Darin Petkove7c6ad32012-06-29 10:22:09 +0200300bool Profile::UpdateDevice(const DeviceRefPtr &device) {
301 return false;
302}
303
Wade Guthrie60a37062013-04-02 11:39:09 -0700304bool Profile::UpdateWiFiProvider(const WiFiProvider &wifi_provider) {
305 return false;
306}
307
mukesh agrawalffa3d042011-10-06 15:26:10 -0700308void Profile::HelpRegisterDerivedStrings(
309 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800310 Strings(Profile::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700311 void(Profile::*set)(const Strings&, Error *)) {
Chris Masone6791a432011-07-12 13:23:19 -0700312 store_.RegisterDerivedStrings(
313 name,
314 StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set)));
315}
316
Chris Masone52cd19b2011-06-29 17:23:04 -0700317} // namespace shill