blob: f922d39fb4e8ba7a1d37f57a1b3f7236a4417883 [file] [log] [blame]
Chris Masone52cd19b2011-06-29 17:23:04 -07001// Copyright (c) 2011 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/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>
Chris Masone52cd19b2011-06-29 17:23:04 -070012#include <base/logging.h>
Chris Masone6791a432011-07-12 13:23:19 -070013#include <base/stl_util-inl.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"
Chris Masone6791a432011-07-12 13:23:19 -070021#include "shill/manager.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070022#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070023#include "shill/service.h"
Chris Masone9d779932011-08-25 16:33:41 -070024#include "shill/store_interface.h"
Chris Masone52cd19b2011-06-29 17:23:04 -070025
Chris Masone6515aab2011-10-12 16:19:09 -070026using std::set;
Chris Masone52cd19b2011-06-29 17:23:04 -070027using std::string;
Chris Masone6791a432011-07-12 13:23:19 -070028using std::vector;
Chris Masone52cd19b2011-06-29 17:23:04 -070029
30namespace shill {
Darin Petkova4766822011-07-07 10:42:22 -070031
Darin Petkova4766822011-07-07 10:42:22 -070032Profile::Profile(ControlInterface *control_interface,
Chris Masone7df0c672011-07-15 10:24:54 -070033 Manager *manager,
34 const Identifier &name,
Chris Masone2ae797d2011-08-23 20:41:00 -070035 const string &user_storage_format,
Chris Masone7df0c672011-07-15 10:24:54 -070036 bool connect_to_rpc)
Chris Masone6791a432011-07-12 13:23:19 -070037 : manager_(manager),
Chris Masone7df0c672011-07-15 10:24:54 -070038 name_(name),
Chris Masone2ae797d2011-08-23 20:41:00 -070039 storage_format_(user_storage_format) {
Chris Masone7df0c672011-07-15 10:24:54 -070040 if (connect_to_rpc)
41 adaptor_.reset(control_interface->CreateProfileAdaptor(this));
42
Chris Masone88cbd5f2011-07-03 14:30:04 -070043 // flimflam::kCheckPortalListProperty: Registered in DefaultProfile
44 // flimflam::kCountryProperty: Registered in DefaultProfile
Chris Masone7df0c672011-07-15 10:24:54 -070045 store_.RegisterConstString(flimflam::kNameProperty, &name_.identifier);
Chris Masone88cbd5f2011-07-03 14:30:04 -070046
47 // flimflam::kOfflineModeProperty: Registered in DefaultProfile
48 // flimflam::kPortalURLProperty: Registered in DefaultProfile
49
Chris Masone6791a432011-07-12 13:23:19 -070050 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
51 &Profile::EnumerateAvailableServices,
52 NULL);
Chris Masone7df0c672011-07-15 10:24:54 -070053 HelpRegisterDerivedStrings(flimflam::kEntriesProperty,
54 &Profile::EnumerateEntries,
55 NULL);
Chris Masone52cd19b2011-06-29 17:23:04 -070056}
57
Chris Masone6515aab2011-10-12 16:19:09 -070058Profile::~Profile() {}
Chris Masone52cd19b2011-06-29 17:23:04 -070059
Paul Stewart5dc40aa2011-10-28 19:43:43 -070060bool Profile::InitStorage(GLib *glib, InitStorageOption storage_option,
61 Error *error) {
Chris Masoneb9c00592011-10-06 13:10:39 -070062 FilePath final_path;
63 if (!GetStoragePath(&final_path)) {
Paul Stewartbe005172011-11-02 18:10:29 -070064 Error::PopulateAndLog(error, Error::kInvalidArguments,
Chris Masoneb9c00592011-10-06 13:10:39 -070065 base::StringPrintf("Could not set up profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070066 name_.user.c_str(), name_.identifier.c_str()));
Chris Masoneb9c00592011-10-06 13:10:39 -070067 return false;
68 }
69 scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib));
70 storage->set_path(final_path);
Paul Stewart5dc40aa2011-10-28 19:43:43 -070071 bool already_exists = storage->IsNonEmpty();
72 if (!already_exists && storage_option != kCreateNew &&
73 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070074 Error::PopulateAndLog(error, Error::kNotFound,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070075 base::StringPrintf("Profile storage for %s:%s does not already exist",
Paul Stewartbe005172011-11-02 18:10:29 -070076 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070077 return false;
78 } else if (already_exists && storage_option != kOpenExisting &&
79 storage_option != kCreateOrOpenExisting) {
Paul Stewartbe005172011-11-02 18:10:29 -070080 Error::PopulateAndLog(error, Error::kAlreadyExists,
Paul Stewart5dc40aa2011-10-28 19:43:43 -070081 base::StringPrintf("Profile storage for %s:%s already exists",
Paul Stewartbe005172011-11-02 18:10:29 -070082 name_.user.c_str(), name_.identifier.c_str()));
Paul Stewart5dc40aa2011-10-28 19:43:43 -070083 return false;
84 }
Chris Masoneb9c00592011-10-06 13:10:39 -070085 if (!storage->Open()) {
Paul Stewartbe005172011-11-02 18:10:29 -070086 Error::PopulateAndLog(error, Error::kInternalError,
Chris Masoneb9c00592011-10-06 13:10:39 -070087 base::StringPrintf("Could not open profile storage for %s:%s",
Paul Stewartbe005172011-11-02 18:10:29 -070088 name_.user.c_str(), name_.identifier.c_str()));
Chris Masoneb9c00592011-10-06 13:10:39 -070089 return false;
90 }
Paul Stewart5dc40aa2011-10-28 19:43:43 -070091 if (!already_exists) {
92 // Add a descriptive header to the profile so even if nothing is stored
93 // to it, it still has some content. Completely empty keyfiles are not
94 // valid for reading.
95 storage->SetHeader(
96 base::StringPrintf("Profile %s:%s", name_.user.c_str(),
97 name_.identifier.c_str()));
98 }
Chris Masoneb9c00592011-10-06 13:10:39 -070099 set_storage(storage.release());
100 return true;
101}
102
Chris Masone7df0c672011-07-15 10:24:54 -0700103string Profile::GetFriendlyName() {
104 return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier;
105}
106
107string Profile::GetRpcIdentifier() {
108 return adaptor_->GetRpcIdentifier();
109}
110
Chris Masoneb9c00592011-10-06 13:10:39 -0700111void Profile::set_storage(StoreInterface *storage) {
112 storage_.reset(storage);
113}
114
Chris Masone6791a432011-07-12 13:23:19 -0700115bool Profile::AdoptService(const ServiceRefPtr &service) {
Chris Masone6515aab2011-10-12 16:19:09 -0700116 if (storage_->ContainsGroup(service->GetStorageIdentifier()))
Chris Masone6791a432011-07-12 13:23:19 -0700117 return false;
118 service->set_profile(this);
Chris Masone6515aab2011-10-12 16:19:09 -0700119 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700120}
121
Chris Masone6515aab2011-10-12 16:19:09 -0700122bool Profile::AbandonService(const ServiceRefPtr &service) {
123 if (service->profile() == this)
124 service->set_profile(NULL);
125 return storage_->DeleteGroup(service->GetStorageIdentifier()) &&
126 storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700127}
128
Chris Masone6515aab2011-10-12 16:19:09 -0700129bool Profile::UpdateService(const ServiceRefPtr &service) {
130 return service->Save(storage_.get()) && storage_->Flush();
Chris Masone6791a432011-07-12 13:23:19 -0700131}
132
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700133bool Profile::ConfigureService(const ServiceRefPtr &service) {
134 if (!ContainsService(service))
Chris Masone6515aab2011-10-12 16:19:09 -0700135 return false;
136 service->set_profile(this);
137 return service->Load(storage_.get());
Chris Masone6791a432011-07-12 13:23:19 -0700138}
139
Chris Masone6515aab2011-10-12 16:19:09 -0700140bool Profile::ContainsService(const ServiceConstRefPtr &service) {
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700141 return service->IsLoadableFrom(storage_.get());
Chris Masone7aa5f902011-07-11 11:13:35 -0700142}
143
Darin Petkova4766822011-07-07 10:42:22 -0700144bool Profile::IsValidIdentifierToken(const std::string &token) {
145 if (token.empty()) {
146 return false;
147 }
148 for (string::const_iterator it = token.begin(); it != token.end(); ++it) {
149 if (!IsAsciiAlpha(*it) && !IsAsciiDigit(*it)) {
150 return false;
151 }
152 }
153 return true;
154}
155
156bool Profile::ParseIdentifier(const string &raw, Identifier *parsed) {
157 if (raw.empty()) {
158 return false;
159 }
160 if (raw[0] == '~') {
161 // Format: "~user/identifier".
162 size_t slash = raw.find('/');
163 if (slash == string::npos) {
164 return false;
165 }
166 string user(raw.begin() + 1, raw.begin() + slash);
167 string identifier(raw.begin() + slash + 1, raw.end());
168 if (!IsValidIdentifierToken(user) || !IsValidIdentifierToken(identifier)) {
169 return false;
170 }
171 parsed->user = user;
172 parsed->identifier = identifier;
173 return true;
174 }
175
176 // Format: "identifier".
177 if (!IsValidIdentifierToken(raw)) {
178 return false;
179 }
180 parsed->user = "";
181 parsed->identifier = raw;
182 return true;
183}
184
Paul Stewart5dc40aa2011-10-28 19:43:43 -0700185bool Profile::MatchesIdentifier(const Identifier &name) const {
186 return name.user == name_.user && name.identifier == name_.identifier;
187}
188
Chris Masoneb9c00592011-10-06 13:10:39 -0700189bool Profile::Save() {
Chris Masone6515aab2011-10-12 16:19:09 -0700190 return storage_->Flush();
Chris Masone9d779932011-08-25 16:33:41 -0700191}
192
Chris Masone2ae797d2011-08-23 20:41:00 -0700193bool Profile::GetStoragePath(FilePath *path) {
194 if (name_.user.empty()) {
195 LOG(ERROR) << "Non-default profiles cannot be stored globally.";
196 return false;
197 }
198 FilePath dir(base::StringPrintf(storage_format_.c_str(), name_.user.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700199 // TODO(petkov): Validate the directory permissions, etc.
200 *path = dir.Append(base::StringPrintf("%s.profile",
Chris Masone2ae797d2011-08-23 20:41:00 -0700201 name_.identifier.c_str()));
Darin Petkova4766822011-07-07 10:42:22 -0700202 return true;
203}
204
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800205vector<string> Profile::EnumerateAvailableServices(Error *error) {
206 return manager_->EnumerateAvailableServices(error);
Chris Masone6791a432011-07-12 13:23:19 -0700207}
208
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800209vector<string> Profile::EnumerateEntries(Error */*error*/) {
Chris Masone6515aab2011-10-12 16:19:09 -0700210 // TODO(someone): Determine if we care about this wasteful copying; consider
211 // making GetGroups return a vector.
212 set<string> groups(storage_->GetGroups());
213 return vector<string>(groups.begin(), groups.end());
Chris Masone6791a432011-07-12 13:23:19 -0700214}
215
mukesh agrawalffa3d042011-10-06 15:26:10 -0700216void Profile::HelpRegisterDerivedStrings(
217 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800218 Strings(Profile::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700219 void(Profile::*set)(const Strings&, Error *)) {
Chris Masone6791a432011-07-12 13:23:19 -0700220 store_.RegisterDerivedStrings(
221 name,
222 StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set)));
223}
224
Chris Masone52cd19b2011-06-29 17:23:04 -0700225} // namespace shill