blob: 95b89561f80938bab9a2e633a51e07ff058a07d2 [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,
Chris Masone7df0c672011-07-15 10:24:54 -070034 Manager *manager,
35 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070036 const string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070037 bool connect_to_rpc)
Chris Masone6791a432011-07-12 13:23:19 -070038 : manager_(manager),
Chris Masone7df0c672011-07-15 10:24:54 -070039 name_(name),
Chris Masone2ae797d2011-08-23 20:41:00 -070040 storage_format_(user_storage_format) {
Chris Masone7df0c672011-07-15 10:24:54 -070041 if (connect_to_rpc)
42 adaptor_.reset(control_interface->CreateProfileAdaptor(this));
43
Chris Masone88cbd5f2011-07-03 14:30:04 -070044 // flimflam::kCheckPortalListProperty: Registered in DefaultProfile
45 // flimflam::kCountryProperty: Registered in DefaultProfile
Chris Masone7df0c672011-07-15 10:24:54 -070046 store_.RegisterConstString(flimflam::kNameProperty, &name_.identifier);
Chris Masone88cbd5f2011-07-03 14:30:04 -070047
48 // flimflam::kOfflineModeProperty: Registered in DefaultProfile
49 // flimflam::kPortalURLProperty: Registered in DefaultProfile
50
Chris Masone6791a432011-07-12 13:23:19 -070051 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
52 &Profile::EnumerateAvailableServices,
53 NULL);
Chris Masone7df0c672011-07-15 10:24:54 -070054 HelpRegisterDerivedStrings(flimflam::kEntriesProperty,
55 &Profile::EnumerateEntries,
56 NULL);
Chris Masone52cd19b2011-06-29 17:23:04 -070057}
58
Chris Masone6515aab2011-10-12 16:19:09 -070059Profile::~Profile() {}
Chris Masone52cd19b2011-06-29 17:23:04 -070060
Paul Stewart5dc40aa2011-10-28 19:43:43 -070061bool Profile::InitStorage(GLib *glib, InitStorageOption storage_option,
62 Error *error) {
Chris Masoneb9c00592011-10-06 13:10:39 -070063 FilePath final_path;
64 if (!GetStoragePath(&final_path)) {
Paul Stewartbe005172011-11-02 18:10:29 -070065 Error::PopulateAndLog(error, Error::kInvalidArguments,
Chris Masoneb9c00592011-10-06 13:10:39 -070066 base::StringPrintf("Could not set up profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070067 name_.user.c_str(), name_.identifier.c_str()));
Chris Masoneb9c00592011-10-06 13:10:39 -070068 return false;
69 }
70 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
71 storage->set_path(final_path);
Paul Stewart5dc40aa2011-10-28 19:43:43 -070072 bool already_exists = storage->IsNonEmpty();
73 if (!already_exists && storage_option != kCreateNew &&
74 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070075 Error::PopulateAndLog(error, Error::kNotFound,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070076 base::StringPrintf("Profile storage for %s:%s does not already exist",
Paul Stewartbe005172011-11-02 18:10:29 -070077 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070078 return false;
79 } else if (already_exists && storage_option != kOpenExisting &&
80 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070081 Error::PopulateAndLog(error, Error::kAlreadyExists,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070082 base::StringPrintf("Profile storage for %s:%s already exists",
Paul Stewartbe005172011-11-02 18:10:29 -070083 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070084 return false;
85 }
Chris Masoneb9c00592011-10-06 13:10:39 -070086 if (!storage->Open()) {
Paul Stewartbe005172011-11-02 18:10:29 -070087 Error::PopulateAndLog(error, Error::kInternalError,
Chris Masoneb9c00592011-10-06 13:10:39 -070088 base::StringPrintf("Could not open profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070089 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart2ebc16d2012-08-23 10:38:39 -070090 if (already_exists) {
91 // The profile contents is corrupt, or we do not have access to
92 // this file. Move this file out of the way so a future open attempt
93 // will succeed, assuming the failure reason was the former.
94 storage->MarkAsCorrupted();
95 }
Chris Masoneb9c00592011-10-06 13:10:39 -070096 return false;
97 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -070098 if (!already_exists) {
99 // Add a descriptive header to the profile so even if nothing is stored
100 // to it, it still has some content. Completely empty keyfiles are not
101 // valid for reading.
102 storage->SetHeader(
103 base::StringPrintf("Profile %s:%s", name_.user.c_str(),
104 name_.identifier.c_str()));
105 }
Chris Masoneb9c00592011-10-06 13:10:39 -0700106 set_storage(storage.release());
107 return true;
108}
109
Paul Stewarte73d05c2012-03-29 16:26:05 -0700110bool Profile::RemoveStorage(GLib *glib, Error *error) {
111 FilePath path;
112
113 CHECK(!storage_.get());
114
115 if (!GetStoragePath(&path)) {
116 Error::PopulateAndLog(
117 error, Error::kInvalidArguments,
118 base::StringPrintf("Could get the storage path for %s:%s",
119 name_.user.c_str(), name_.identifier.c_str()));
120 return false;
121 }
122
123 if (!file_util::Delete(path, false)) {
124 Error::PopulateAndLog(
125 error, Error::kOperationFailed,
126 base::StringPrintf("Could not remove path %s", path.value().c_str()));
127 return false;
128 }
129
130 return true;
131}
132
Chris Masone7df0c672011-07-15 10:24:54 -0700133string Profile::GetFriendlyName() {
134 return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier;
135}
136
137string Profile::GetRpcIdentifier() {
Paul Stewart19c871d2011-12-15 16:10:13 -0800138 if (!adaptor_.get()) {
139 // NB: This condition happens in unit tests.
140 return string();
141 }
Chris Masone7df0c672011-07-15 10:24:54 -0700142 return adaptor_->GetRpcIdentifier();
143}
144
Chris Masoneb9c00592011-10-06 13:10:39 -0700145void Profile::set_storage(StoreInterface *storage) {
146 storage_.reset(storage);
147}
148
Chris Masone6791a432011-07-12 13:23:19 -0700149bool Profile::AdoptService(const ServiceRefPtr &service) {
Paul Stewart451aa7f2012-04-11 19:07:58 -0700150 if (service->profile() == this) {
Chris Masone6791a432011-07-12 13:23:19 -0700151 return false;
Paul Stewart451aa7f2012-04-11 19:07:58 -0700152 }
Chris Masone6791a432011-07-12 13:23:19 -0700153 service->set_profile(this);
Chris Masone6515aab2011-10-12 16:19:09 -0700154 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700155}
156
Chris Masone6515aab2011-10-12 16:19:09 -0700157bool Profile::AbandonService(const ServiceRefPtr &service) {
158 if (service->profile() == this)
159 service->set_profile(NULL);
160 return storage_->DeleteGroup(service->GetStorageIdentifier()) &&
161 storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700162}
163
Chris Masone6515aab2011-10-12 16:19:09 -0700164bool Profile::UpdateService(const ServiceRefPtr &service) {
165 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700166}
167
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700168bool Profile::ConfigureService(const ServiceRefPtr &service) {
169 if (!ContainsService(service))
Chris Masone6515aab2011-10-12 16:19:09 -0700170 return false;
171 service->set_profile(this);
172 return service->Load(storage_.get());
Chris Masone6791a432011-07-12 13:23:19 -0700173}
174
Paul Stewarta41e38d2011-11-11 07:47:29 -0800175bool Profile::ConfigureDevice(const DeviceRefPtr &device) {
176 return device->Load(storage_.get());
177}
178
Chris Masone6515aab2011-10-12 16:19:09 -0700179bool Profile::ContainsService(const ServiceConstRefPtr &service) {
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700180 return service->IsLoadableFrom(storage_.get());
Chris Masone7aa5f902011-07-11 11:13:35 -0700181}
182
Paul Stewart75225512012-01-26 22:51:33 -0800183void Profile::DeleteEntry(const std::string &entry_name, Error *error) {
184 if (!storage_->ContainsGroup(entry_name)) {
185 Error::PopulateAndLog(error, Error::kNotFound,
186 base::StringPrintf("Entry %s does not exist in profile",
187 entry_name.c_str()));
188 return;
189 }
190 if (!manager_->HandleProfileEntryDeletion(this, entry_name)) {
191 // If HandleProfileEntryDeletion() returns succeeds, DeleteGroup()
192 // has already been called when AbandonService was called.
193 // Otherwise, we need to delete the group ourselves.
194 storage_->DeleteGroup(entry_name);
195 }
Paul Stewart0756db92012-01-27 08:34:47 -0800196 Save();
197}
198
199ServiceRefPtr Profile::GetServiceFromEntry(const std::string &entry_name,
200 Error *error) {
201 return manager_->GetServiceWithStorageIdentifier(this, entry_name, error);
Paul Stewart75225512012-01-26 22:51:33 -0800202}
203
Paul Stewarta41e38d2011-11-11 07:47:29 -0800204bool Profile::IsValidIdentifierToken(const string &token) {
Darin Petkova4766822011-07-07 10:42:22 -0700205 if (token.empty()) {
206 return false;
207 }
208 for (string::const_iterator it = token.begin(); it != token.end(); ++it) {
209 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) {
210 return false;
211 }
212 }
213 return true;
214}
215
216bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) {
217 if (raw.empty()) {
218 return false;
219 }
220 if (raw[0] == '~') {
221 // Format: "~user/identifier".
222 size_t slash = raw.find('/');
223 if (slash == string::npos) {
224 return false;
225 }
226 string user(raw.begin() + 1, raw.begin() + slash);
227 string identifier(raw.begin() + slash + 1, raw.end());
228 if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) {
229 return false;
230 }
231 parsed->user = user;
232 parsed->identifier = identifier;
233 return true;
234 }
235
236 // Format: "identifier".
237 if (!IsValidIdentifierToken(raw)) {
238 return false;
239 }
240 parsed->user = "";
241 parsed->identifier = raw;
242 return true;
243}
244
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700245bool Profile::MatchesIdentifier(const Identifier &name) const {
246 return name.user == name_.user && name.identifier == name_.identifier;
247}
248
Chris Masoneb9c00592011-10-06 13:10:39 -0700249bool Profile::Save() {
Chris Masone6515aab2011-10-12 16:19:09 -0700250 return storage_->Flush();
Chris Masone9d779932011-08-25 16:33:41 -0700251}
252
Chris Masone2ae797d2011-08-23 20:41:00 -0700253bool Profile::GetStoragePath(FilePath *path) {
254 if (name_.user.empty()) {
255 LOG(ERROR) << "Non-default profiles cannot be stored globally.";
256 return false;
257 }
258 FilePath dir(base::StringPrintf(storage_format_.c_str(), name_.user.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700259 // TODO(petkov): Validate the directory permissions, etc.
260 *path = dir.Append(base::StringPrintf("%s.profile",
Chris Masone2ae797d2011-08-23 20:41:00 -0700261 name_.identifier.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700262 return true;
263}
264
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800265vector<string> Profile::EnumerateAvailableServices(Error *error) {
Paul Stewart1b253142012-01-26 14:05:52 -0800266 // We should return the Manager's service list if this is the active profile.
267 if (manager_->IsActiveProfile(this)) {
268 return manager_->EnumerateAvailableServices(error);
269 } else {
270 return vector<string>();
271 }
Chris Masone6791a432011-07-12 13:23:19 -0700272}
273
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800274vector<string> Profile::EnumerateEntries(Error */*error*/) {
Chris Masone6515aab2011-10-12 16:19:09 -0700275 set<string> groups(storage_->GetGroups());
Paul Stewart1b253142012-01-26 14:05:52 -0800276 vector<string> service_groups;
277
278 // Filter this list down to only entries that correspond
279 // to a technology. (wifi_*, etc)
280 for (set<string>::iterator it = groups.begin();
281 it != groups.end(); ++it) {
282 if (Technology::IdentifierFromStorageGroup(*it) != Technology::kUnknown)
283 service_groups.push_back(*it);
284 }
285
286 return service_groups;
Chris Masone6791a432011-07-12 13:23:19 -0700287}
288
Darin Petkove7c6ad32012-06-29 10:22:09 +0200289bool Profile::UpdateDevice(const DeviceRefPtr &device) {
290 return false;
291}
292
mukesh agrawalffa3d042011-10-06 15:26:10 -0700293void Profile::HelpRegisterDerivedStrings(
294 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800295 Strings(Profile::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700296 void(Profile::*set)(const Strings&, Error *)) {
Chris Masone6791a432011-07-12 13:23:19 -0700297 store_.RegisterDerivedStrings(
298 name,
299 StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set)));
300}
301
Chris Masone52cd19b2011-06-29 17:23:04 -0700302} // namespace shill