Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 1 | // 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 Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 7 | #include <set> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 8 | #include <string> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 9 | #include <vector> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 10 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 11 | #include <base/file_path.h> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 12 | #include <base/logging.h> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 13 | #include <base/stl_util-inl.h> |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 14 | #include <base/string_util.h> |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 15 | #include <base/stringprintf.h> |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/adaptor_interfaces.h" |
| 19 | #include "shill/control_interface.h" |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 20 | #include "shill/key_file_store.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 21 | #include "shill/manager.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 22 | #include "shill/property_accessor.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 23 | #include "shill/service.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 24 | #include "shill/store_interface.h" |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 25 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 26 | using std::set; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 27 | using std::string; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 28 | using std::vector; |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 29 | |
| 30 | namespace shill { |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 31 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 32 | Profile::Profile(ControlInterface *control_interface, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 33 | Manager *manager, |
| 34 | const Identifier &name, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 35 | const string &user_storage_format, |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 36 | bool connect_to_rpc) |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 37 | : manager_(manager), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 38 | name_(name), |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 39 | storage_format_(user_storage_format) { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 40 | if (connect_to_rpc) |
| 41 | adaptor_.reset(control_interface->CreateProfileAdaptor(this)); |
| 42 | |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 43 | // flimflam::kCheckPortalListProperty: Registered in DefaultProfile |
| 44 | // flimflam::kCountryProperty: Registered in DefaultProfile |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 45 | store_.RegisterConstString(flimflam::kNameProperty, &name_.identifier); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 46 | |
| 47 | // flimflam::kOfflineModeProperty: Registered in DefaultProfile |
| 48 | // flimflam::kPortalURLProperty: Registered in DefaultProfile |
| 49 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 50 | HelpRegisterDerivedStrings(flimflam::kServicesProperty, |
| 51 | &Profile::EnumerateAvailableServices, |
| 52 | NULL); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 53 | HelpRegisterDerivedStrings(flimflam::kEntriesProperty, |
| 54 | &Profile::EnumerateEntries, |
| 55 | NULL); |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 58 | Profile::~Profile() {} |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 59 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 60 | bool Profile::InitStorage(GLib *glib, InitStorageOption storage_option, |
| 61 | Error *error) { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 62 | FilePath final_path; |
| 63 | if (!GetStoragePath(&final_path)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame^] | 64 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 65 | base::StringPrintf("Could not set up profile storage for %s:%s", |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame^] | 66 | name_.user.c_str(), name_.identifier.c_str())); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | scoped_ptr<KeyFileStore> storage(new KeyFileStore(glib)); |
| 70 | storage->set_path(final_path); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 71 | bool already_exists = storage->IsNonEmpty(); |
| 72 | if (!already_exists && storage_option != kCreateNew && |
| 73 | storage_option != kCreateOrOpenExisting) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame^] | 74 | Error::PopulateAndLog(error, Error::kNotFound, |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 75 | base::StringPrintf("Profile storage for %s:%s does not already exist", |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame^] | 76 | name_.user.c_str(), name_.identifier.c_str())); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } else if (already_exists && storage_option != kOpenExisting && |
| 79 | storage_option != kCreateOrOpenExisting) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame^] | 80 | Error::PopulateAndLog(error, Error::kAlreadyExists, |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 81 | base::StringPrintf("Profile storage for %s:%s already exists", |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame^] | 82 | name_.user.c_str(), name_.identifier.c_str())); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 85 | if (!storage->Open()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame^] | 86 | Error::PopulateAndLog(error, Error::kInternalError, |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 87 | base::StringPrintf("Could not open profile storage for %s:%s", |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame^] | 88 | name_.user.c_str(), name_.identifier.c_str())); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 89 | return false; |
| 90 | } |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 91 | 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 Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 99 | set_storage(storage.release()); |
| 100 | return true; |
| 101 | } |
| 102 | |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 103 | string Profile::GetFriendlyName() { |
| 104 | return (name_.user.empty() ? "" : name_.user + "/") + name_.identifier; |
| 105 | } |
| 106 | |
| 107 | string Profile::GetRpcIdentifier() { |
| 108 | return adaptor_->GetRpcIdentifier(); |
| 109 | } |
| 110 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 111 | void Profile::set_storage(StoreInterface *storage) { |
| 112 | storage_.reset(storage); |
| 113 | } |
| 114 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 115 | bool Profile::AdoptService(const ServiceRefPtr &service) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 116 | if (storage_->ContainsGroup(service->GetStorageIdentifier())) |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 117 | return false; |
| 118 | service->set_profile(this); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 119 | return service->Save(storage_.get()) && storage_->Flush(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 122 | bool 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 Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 129 | bool Profile::UpdateService(const ServiceRefPtr &service) { |
| 130 | return service->Save(storage_.get()) && storage_->Flush(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 133 | bool Profile::ConfigureService(const ServiceRefPtr &service) { |
| 134 | if (!ContainsService(service)) |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 135 | return false; |
| 136 | service->set_profile(this); |
| 137 | return service->Load(storage_.get()); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 140 | bool Profile::ContainsService(const ServiceConstRefPtr &service) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 141 | return service->IsLoadableFrom(storage_.get()); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 144 | bool 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 | |
| 156 | bool 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 Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 185 | bool Profile::MatchesIdentifier(const Identifier &name) const { |
| 186 | return name.user == name_.user && name.identifier == name_.identifier; |
| 187 | } |
| 188 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 189 | bool Profile::Save() { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 190 | return storage_->Flush(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 193 | bool 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 Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 199 | // TODO(petkov): Validate the directory permissions, etc. |
| 200 | *path = dir.Append(base::StringPrintf("%s.profile", |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 201 | name_.identifier.c_str())); |
Darin Petkov | a476682 | 2011-07-07 10:42:22 -0700 | [diff] [blame] | 202 | return true; |
| 203 | } |
| 204 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 205 | vector<string> Profile::EnumerateAvailableServices() { |
| 206 | return manager_->EnumerateAvailableServices(); |
| 207 | } |
| 208 | |
| 209 | vector<string> Profile::EnumerateEntries() { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 210 | // 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 Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 214 | } |
| 215 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 216 | void Profile::HelpRegisterDerivedStrings( |
| 217 | const string &name, |
| 218 | Strings(Profile::*get)(void), |
| 219 | void(Profile::*set)(const Strings&, Error *)) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 220 | store_.RegisterDerivedStrings( |
| 221 | name, |
| 222 | StringsAccessor(new CustomAccessor<Profile, Strings>(this, get, set))); |
| 223 | } |
| 224 | |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 225 | } // namespace shill |