blob: c88d8359326aad3d5ca971d4ca2bcf6cb56b4e12 [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>
Paul Stewart78af94c2013-04-17 16:02:06 -070014#include <base/string_split.h>
Darin Petkova4766822011-07-07 10:42:22 -070015#include <base/string_util.h>
Chris Masone6791a432011-07-12 13:23:19 -070016#include <base/stringprintf.h>
Chris Masone88cbd5f2011-07-03 14:30:04 -070017#include <chromeos/dbus/service_constants.h>
Chris Masone52cd19b2011-06-29 17:23:04 -070018
19#include "shill/adaptor_interfaces.h"
20#include "shill/control_interface.h"
Chris Masoneb9c00592011-10-06 13:10:39 -070021#include "shill/key_file_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070022#include "shill/logging.h"
Chris Masone6791a432011-07-12 13:23:19 -070023#include "shill/manager.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070024#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070025#include "shill/service.h"
Chris Masone9d779932011-08-25 16:33:41 -070026#include "shill/store_interface.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070027
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080028using base::FilePath;
Chris Masone6515aab2011-10-12 16:19:09 -070029using std::set;
Chris Masone52cd19b2011-06-29 17:23:04 -070030using std::string;
Chris Masone6791a432011-07-12 13:23:19 -070031using std::vector;
Chris Masone52cd19b2011-06-29 17:23:04 -070032
33namespace shill {
Darin Petkova4766822011-07-07 10:42:22 -070034
Paul Stewart78af94c2013-04-17 16:02:06 -070035// static
36const char Profile::kUserProfileListPathname[] =
37 RUNDIR "/loaded_profile_list";
38
Darin Petkova4766822011-07-07 10:42:22 -070039Profile::Profile(ControlInterface *control_interface,
Thieu Le5133b712013-02-19 14:47:21 -080040 Metrics *metrics,
Chris Masone7df0c672011-07-15 10:24:54 -070041 Manager *manager,
42 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070043 const string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070044 bool connect_to_rpc)
Thieu Le5133b712013-02-19 14:47:21 -080045 : metrics_(metrics),
46 manager_(manager),
Chris Masone7df0c672011-07-15 10:24:54 -070047 name_(name),
Chris Masone2ae797d2011-08-23 20:41:00 -070048 storage_format_(user_storage_format) {
Chris Masone7df0c672011-07-15 10:24:54 -070049 if (connect_to_rpc)
50 adaptor_.reset(control_interface->CreateProfileAdaptor(this));
51
Chris Masone88cbd5f2011-07-03 14:30:04 -070052 // flimflam::kCheckPortalListProperty: Registered in DefaultProfile
53 // flimflam::kCountryProperty: Registered in DefaultProfile
Chris Masone7df0c672011-07-15 10:24:54 -070054 store_.RegisterConstString(flimflam::kNameProperty, &name_.identifier);
Paul Stewartf3eced92013-04-17 12:18:22 -070055 store_.RegisterConstString(kUserHashProperty, &name_.user_hash);
Chris Masone88cbd5f2011-07-03 14:30:04 -070056
57 // flimflam::kOfflineModeProperty: Registered in DefaultProfile
58 // flimflam::kPortalURLProperty: Registered in DefaultProfile
59
Chris Masone6791a432011-07-12 13:23:19 -070060 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
61 &Profile::EnumerateAvailableServices,
62 NULL);
Chris Masone7df0c672011-07-15 10:24:54 -070063 HelpRegisterDerivedStrings(flimflam::kEntriesProperty,
64 &Profile::EnumerateEntries,
65 NULL);
Chris Masone52cd19b2011-06-29 17:23:04 -070066}
67
Chris Masone6515aab2011-10-12 16:19:09 -070068Profile::~Profile() {}
Chris Masone52cd19b2011-06-29 17:23:04 -070069
Paul Stewart5dc40aa2011-10-28 19:43:43 -070070bool Profile::InitStorage(GLib *glib, InitStorageOption storage_option,
71 Error *error) {
Chris Masoneb9c00592011-10-06 13:10:39 -070072 FilePath final_path;
73 if (!GetStoragePath(&final_path)) {
Paul Stewartbe005172011-11-02 18:10:29 -070074 Error::PopulateAndLog(error, Error::kInvalidArguments,
Chris Masoneb9c00592011-10-06 13:10:39 -070075 base::StringPrintf("Could not set up profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070076 name_.user.c_str(), name_.identifier.c_str()));
Chris Masoneb9c00592011-10-06 13:10:39 -070077 return false;
78 }
79 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
80 storage->set_path(final_path);
Paul Stewart5dc40aa2011-10-28 19:43:43 -070081 bool already_exists = storage->IsNonEmpty();
82 if (!already_exists && storage_option != kCreateNew &&
83 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070084 Error::PopulateAndLog(error, Error::kNotFound,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070085 base::StringPrintf("Profile storage for %s:%s does not already exist",
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 } else if (already_exists && storage_option != kOpenExisting &&
89 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070090 Error::PopulateAndLog(error, Error::kAlreadyExists,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070091 base::StringPrintf("Profile storage for %s:%s already exists",
Paul Stewartbe005172011-11-02 18:10:29 -070092 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070093 return false;
94 }
Chris Masoneb9c00592011-10-06 13:10:39 -070095 if (!storage->Open()) {
Paul Stewartbe005172011-11-02 18:10:29 -070096 Error::PopulateAndLog(error, Error::kInternalError,
Chris Masoneb9c00592011-10-06 13:10:39 -070097 base::StringPrintf("Could not open profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070098 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart2ebc16d2012-08-23 10:38:39 -070099 if (already_exists) {
100 // The profile contents is corrupt, or we do not have access to
101 // this file. Move this file out of the way so a future open attempt
102 // will succeed, assuming the failure reason was the former.
103 storage->MarkAsCorrupted();
Thieu Le5133b712013-02-19 14:47:21 -0800104 metrics_->NotifyCorruptedProfile();
Paul Stewart2ebc16d2012-08-23 10:38:39 -0700105 }
Chris Masoneb9c00592011-10-06 13:10:39 -0700106 return false;
107 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700108 if (!already_exists) {
109 // Add a descriptive header to the profile so even if nothing is stored
110 // to it, it still has some content. Completely empty keyfiles are not
111 // valid for reading.
112 storage->SetHeader(
113 base::StringPrintf("Profile %s:%s", name_.user.c_str(),
114 name_.identifier.c_str()));
115 }
Chris Masoneb9c00592011-10-06 13:10:39 -0700116 set_storage(storage.release());
Paul Stewart3c504012013-01-17 17:49:58 -0800117 manager_->OnProfileStorageInitialized(storage_.get());
Chris Masoneb9c00592011-10-06 13:10:39 -0700118 return true;
119}
120
Paul Stewarte73d05c2012-03-29 16:26:05 -0700121bool Profile::RemoveStorage(GLib *glib, Error *error) {
122 FilePath path;
123
124 CHECK(!storage_.get());
125
126 if (!GetStoragePath(&path)) {
127 Error::PopulateAndLog(
128 error, Error::kInvalidArguments,
129 base::StringPrintf("Could get the storage path for %s:%s",
130 name_.user.c_str(), name_.identifier.c_str()));
131 return false;
132 }
133
134 if (!file_util::Delete(path, false)) {
135 Error::PopulateAndLog(
136 error, Error::kOperationFailed,
137 base::StringPrintf("Could not remove path %s", path.value().c_str()));
138 return false;
139 }
140
141 return true;
142}
143
Chris Masone7df0c672011-07-15 10:24:54 -0700144string Profile::GetFriendlyName() {
145 return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier;
146}
147
148string Profile::GetRpcIdentifier() {
Paul Stewart19c871d2011-12-15 16:10:13 -0800149 if (!adaptor_.get()) {
150 // NB: This condition happens in unit tests.
151 return string();
152 }
Chris Masone7df0c672011-07-15 10:24:54 -0700153 return adaptor_->GetRpcIdentifier();
154}
155
Chris Masoneb9c00592011-10-06 13:10:39 -0700156void Profile::set_storage(StoreInterface *storage) {
157 storage_.reset(storage);
158}
159
Chris Masone6791a432011-07-12 13:23:19 -0700160bool Profile::AdoptService(const ServiceRefPtr &service) {
Paul Stewart451aa7f2012-04-11 19:07:58 -0700161 if (service->profile() == this) {
Chris Masone6791a432011-07-12 13:23:19 -0700162 return false;
Paul Stewart451aa7f2012-04-11 19:07:58 -0700163 }
Philipp Neubeck79173602012-11-13 21:10:09 +0100164 service->SetProfile(this);
Chris Masone6515aab2011-10-12 16:19:09 -0700165 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700166}
167
Chris Masone6515aab2011-10-12 16:19:09 -0700168bool Profile::AbandonService(const ServiceRefPtr &service) {
169 if (service->profile() == this)
Philipp Neubeck79173602012-11-13 21:10:09 +0100170 service->SetProfile(NULL);
Chris Masone6515aab2011-10-12 16:19:09 -0700171 return storage_->DeleteGroup(service->GetStorageIdentifier()) &&
172 storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700173}
174
Chris Masone6515aab2011-10-12 16:19:09 -0700175bool Profile::UpdateService(const ServiceRefPtr &service) {
176 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700177}
178
Paul Stewart2c575d22012-12-07 12:28:57 -0800179bool Profile::LoadService(const ServiceRefPtr &service) {
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700180 if (!ContainsService(service))
Chris Masone6515aab2011-10-12 16:19:09 -0700181 return false;
Chris Masone6515aab2011-10-12 16:19:09 -0700182 return service->Load(storage_.get());
Chris Masone6791a432011-07-12 13:23:19 -0700183}
184
Paul Stewart2c575d22012-12-07 12:28:57 -0800185bool Profile::ConfigureService(const ServiceRefPtr &service) {
186 if (!LoadService(service))
187 return false;
188 service->SetProfile(this);
189 return true;
190}
191
Paul Stewarta41e38d2011-11-11 07:47:29 -0800192bool Profile::ConfigureDevice(const DeviceRefPtr &device) {
193 return device->Load(storage_.get());
194}
195
Chris Masone6515aab2011-10-12 16:19:09 -0700196bool Profile::ContainsService(const ServiceConstRefPtr &service) {
Paul Stewarte7de2942013-04-25 17:07:31 -0700197 return service->IsLoadableFrom(*storage_.get());
Chris Masone7aa5f902011-07-11 11:13:35 -0700198}
199
Paul Stewart75225512012-01-26 22:51:33 -0800200void Profile::DeleteEntry(const std::string &entry_name, Error *error) {
201 if (!storage_->ContainsGroup(entry_name)) {
202 Error::PopulateAndLog(error, Error::kNotFound,
203 base::StringPrintf("Entry %s does not exist in profile",
204 entry_name.c_str()));
205 return;
206 }
207 if (!manager_->HandleProfileEntryDeletion(this, entry_name)) {
208 // If HandleProfileEntryDeletion() returns succeeds, DeleteGroup()
209 // has already been called when AbandonService was called.
210 // Otherwise, we need to delete the group ourselves.
211 storage_->DeleteGroup(entry_name);
212 }
Paul Stewart0756db92012-01-27 08:34:47 -0800213 Save();
214}
215
216ServiceRefPtr Profile::GetServiceFromEntry(const std::string &entry_name,
217 Error *error) {
218 return manager_->GetServiceWithStorageIdentifier(this, entry_name, error);
Paul Stewart75225512012-01-26 22:51:33 -0800219}
220
Paul Stewarta41e38d2011-11-11 07:47:29 -0800221bool Profile::IsValidIdentifierToken(const string &token) {
Darin Petkova4766822011-07-07 10:42:22 -0700222 if (token.empty()) {
223 return false;
224 }
225 for (string::const_iterator it = token.begin(); it != token.end(); ++it) {
226 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) {
227 return false;
228 }
229 }
230 return true;
231}
232
Paul Stewartf3eced92013-04-17 12:18:22 -0700233// static
Darin Petkova4766822011-07-07 10:42:22 -0700234bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) {
235 if (raw.empty()) {
236 return false;
237 }
238 if (raw[0] == '~') {
239 // Format: "~user/identifier".
240 size_t slash = raw.find('/');
241 if (slash == string::npos) {
242 return false;
243 }
244 string user(raw.begin() + 1, raw.begin() + slash);
245 string identifier(raw.begin() + slash + 1, raw.end());
246 if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) {
247 return false;
248 }
249 parsed->user = user;
250 parsed->identifier = identifier;
251 return true;
252 }
253
254 // Format: "identifier".
255 if (!IsValidIdentifierToken(raw)) {
256 return false;
257 }
258 parsed->user = "";
259 parsed->identifier = raw;
260 return true;
261}
262
Paul Stewartf3eced92013-04-17 12:18:22 -0700263// static
264string Profile::IdentifierToString(const Identifier &name) {
265 if (name.user.empty()) {
266 // Format: "identifier".
267 return name.identifier;
268 }
269
270 // Format: "~user/identifier".
271 return base::StringPrintf(
272 "~%s/%s", name.user.c_str(), name.identifier.c_str());
273}
274
Paul Stewart78af94c2013-04-17 16:02:06 -0700275// static
276vector<Profile::Identifier> Profile::LoadUserProfileList(const FilePath &path) {
277 vector<Identifier> profile_identifiers;
278 string profile_data;
279 if (!file_util::ReadFileToString(path, &profile_data)) {
280 return profile_identifiers;
281 }
282
283 vector<string> profile_lines;
284 base::SplitStringDontTrim(profile_data, '\n', &profile_lines);
285 vector<string>::const_iterator it;
286 for (it = profile_lines.begin(); it != profile_lines.end(); ++it) {
287 const string &line = *it;
288 if (line.empty()) {
289 // This will be the case on the last line, so let's not complain about it.
290 continue;
291 }
292 size_t space = line.find(' ');
293 if (space == string::npos || space == 0) {
294 LOG(ERROR) << "Invalid line found in " << path.value()
295 << ": " << line;
296 continue;
297 }
298 string name(line.begin(), line.begin() + space);
299 Identifier identifier;
300 if (!ParseIdentifier(name, &identifier) || identifier.user.empty()) {
301 LOG(ERROR) << "Invalid profile name found in " << path.value()
302 << ": " << name;
303 continue;
304 }
305 identifier.user_hash = string(line.begin() + space + 1, line.end());
306 profile_identifiers.push_back(identifier);
307 }
308
309 return profile_identifiers;
310}
311
312// static
313bool Profile::SaveUserProfileList(const FilePath &path,
314 const vector<ProfileRefPtr> &profiles) {
315 vector<string> lines;
316 vector<ProfileRefPtr>::const_iterator it;
317 for (it = profiles.begin(); it != profiles.end(); ++it) {
318 Identifier &id = (*it)->name_;
319 if (id.user.empty()) {
320 continue;
321 }
322 lines.push_back(base::StringPrintf("%s %s\n",
323 IdentifierToString(id).c_str(),
324 id.user_hash.c_str()));
325 }
326 string content = JoinString(lines, "");
327 size_t ret = file_util::WriteFile(path, content.c_str(), content.length());
328 return ret == content.length();
329}
330
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700331bool Profile::MatchesIdentifier(const Identifier &name) const {
332 return name.user == name_.user && name.identifier == name_.identifier;
333}
334
Chris Masoneb9c00592011-10-06 13:10:39 -0700335bool Profile::Save() {
Chris Masone6515aab2011-10-12 16:19:09 -0700336 return storage_->Flush();
Chris Masone9d779932011-08-25 16:33:41 -0700337}
338
Chris Masone2ae797d2011-08-23 20:41:00 -0700339bool Profile::GetStoragePath(FilePath *path) {
340 if (name_.user.empty()) {
341 LOG(ERROR) << "Non-default profiles cannot be stored globally.";
342 return false;
343 }
344 FilePath dir(base::StringPrintf(storage_format_.c_str(), name_.user.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700345 // TODO(petkov): Validate the directory permissions, etc.
346 *path = dir.Append(base::StringPrintf("%s.profile",
Chris Masone2ae797d2011-08-23 20:41:00 -0700347 name_.identifier.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700348 return true;
349}
350
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800351vector<string> Profile::EnumerateAvailableServices(Error *error) {
Paul Stewart1b253142012-01-26 14:05:52 -0800352 // We should return the Manager's service list if this is the active profile.
353 if (manager_->IsActiveProfile(this)) {
354 return manager_->EnumerateAvailableServices(error);
355 } else {
356 return vector<string>();
357 }
Chris Masone6791a432011-07-12 13:23:19 -0700358}
359
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800360vector<string> Profile::EnumerateEntries(Error */*error*/) {
Chris Masone6515aab2011-10-12 16:19:09 -0700361 set<string> groups(storage_->GetGroups());
Paul Stewart1b253142012-01-26 14:05:52 -0800362 vector<string> service_groups;
363
364 // Filter this list down to only entries that correspond
365 // to a technology. (wifi_*, etc)
366 for (set<string>::iterator it = groups.begin();
367 it != groups.end(); ++it) {
368 if (Technology::IdentifierFromStorageGroup(*it) != Technology::kUnknown)
369 service_groups.push_back(*it);
370 }
371
372 return service_groups;
Chris Masone6791a432011-07-12 13:23:19 -0700373}
374
Darin Petkove7c6ad32012-06-29 10:22:09 +0200375bool Profile::UpdateDevice(const DeviceRefPtr &device) {
376 return false;
377}
378
Wade Guthrie60a37062013-04-02 11:39:09 -0700379bool Profile::UpdateWiFiProvider(const WiFiProvider &wifi_provider) {
380 return false;
381}
382
mukesh agrawalffa3d042011-10-06 15:26:10 -0700383void Profile::HelpRegisterDerivedStrings(
384 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800385 Strings(Profile::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700386 void(Profile::*set)(const Strings&, Error *)) {
Chris Masone6791a432011-07-12 13:23:19 -0700387 store_.RegisterDerivedStrings(
388 name,
389 StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set)));
390}
391
Chris Masone52cd19b2011-06-29 17:23:04 -0700392} // namespace shill