Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 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/key_file_store.h" |
| 6 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 7 | #include <map> |
| 8 | |
Peter Qiu | 3e7aff5 | 2014-03-26 13:03:40 -0700 | [diff] [blame] | 9 | #include <base/files/important_file_writer.h> |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 10 | #include <base/files/file_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 11 | #include <base/strings/string_number_conversions.h> |
Gary Morain | 9697024 | 2012-04-20 10:59:58 -0700 | [diff] [blame] | 12 | #include <fcntl.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <sys/types.h> |
Paul Stewart | 7996364 | 2013-06-12 15:44:17 -0700 | [diff] [blame] | 15 | #include <unistd.h> |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 16 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 17 | #include "shill/key_value_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 18 | #include "shill/logging.h" |
mukesh agrawal | 3ff527c | 2014-04-08 17:07:56 -0700 | [diff] [blame] | 19 | #include "shill/scoped_umask.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 20 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 21 | using std::map; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 22 | using std::set; |
| 23 | using std::string; |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 24 | using std::vector; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 25 | |
| 26 | namespace shill { |
| 27 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 28 | namespace Logging { |
| 29 | static auto kModuleLogScope = ScopeLogger::kStorage; |
| 30 | static string ObjectID(const KeyFileStore *k) { return "(key_file_store)"; } |
| 31 | } |
| 32 | |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 33 | const char KeyFileStore::kCorruptSuffix[] = ".corrupted"; |
| 34 | |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 35 | KeyFileStore::KeyFileStore(GLib *glib) |
| 36 | : glib_(glib), |
| 37 | crypto_(glib), |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 38 | key_file_(nullptr) {} |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 39 | |
| 40 | KeyFileStore::~KeyFileStore() { |
| 41 | ReleaseKeyFile(); |
| 42 | } |
| 43 | |
| 44 | void KeyFileStore::ReleaseKeyFile() { |
| 45 | if (key_file_) { |
| 46 | glib_->KeyFileFree(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 47 | key_file_ = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 51 | bool KeyFileStore::IsNonEmpty() const { |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 52 | int64_t file_size = 0; |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 53 | return base::GetFileSize(path_, &file_size) && file_size != 0; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 56 | bool KeyFileStore::Open() { |
| 57 | CHECK(!path_.empty()); |
| 58 | CHECK(!key_file_); |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 59 | crypto_.Init(); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 60 | key_file_ = glib_->KeyFileNew(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 61 | if (!IsNonEmpty()) { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 62 | LOG(INFO) << "Creating a new key file at " << path_.value(); |
| 63 | return true; |
| 64 | } |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 65 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 66 | if (glib_->KeyFileLoadFromFile( |
| 67 | key_file_, |
| 68 | path_.value().c_str(), |
| 69 | static_cast<GKeyFileFlags>(G_KEY_FILE_KEEP_COMMENTS | |
| 70 | G_KEY_FILE_KEEP_TRANSLATIONS), |
| 71 | &error)) { |
| 72 | return true; |
| 73 | } |
| 74 | LOG(ERROR) << "Failed to load key file from " << path_.value() << ": " |
| 75 | << glib_->ConvertErrorToMessage(error); |
| 76 | ReleaseKeyFile(); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool KeyFileStore::Close() { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 81 | bool success = Flush(); |
| 82 | ReleaseKeyFile(); |
| 83 | return success; |
| 84 | } |
| 85 | |
| 86 | bool KeyFileStore::Flush() { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 87 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 88 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 89 | gsize length = 0; |
| 90 | gchar *data = glib_->KeyFileToData(key_file_, &length, &error); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 91 | |
| 92 | bool success = true; |
| 93 | if (path_.empty()) { |
| 94 | LOG(ERROR) << "Empty key file path."; |
| 95 | success = false; |
| 96 | } |
| 97 | if (success && (!data || error)) { |
| 98 | LOG(ERROR) << "Failed to convert key file to string: " |
| 99 | << glib_->ConvertErrorToMessage(error); |
| 100 | success = false; |
| 101 | } |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 102 | if (success) { |
mukesh agrawal | 3ff527c | 2014-04-08 17:07:56 -0700 | [diff] [blame] | 103 | ScopedUmask owner_only_umask(~(S_IRUSR | S_IWUSR)); |
Peter Qiu | 3e7aff5 | 2014-03-26 13:03:40 -0700 | [diff] [blame] | 104 | success = base::ImportantFileWriter::WriteFileAtomically(path_, data); |
Peter Qiu | 3e7aff5 | 2014-03-26 13:03:40 -0700 | [diff] [blame] | 105 | if (!success) { |
| 106 | LOG(ERROR) << "Failed to store key file: " << path_.value(); |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 107 | } |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 108 | } |
| 109 | glib_->Free(data); |
| 110 | return success; |
| 111 | } |
| 112 | |
Paul Stewart | 2ebc16d | 2012-08-23 10:38:39 -0700 | [diff] [blame] | 113 | bool KeyFileStore::MarkAsCorrupted() { |
| 114 | LOG(INFO) << "In " << __func__ << " for " << path_.value(); |
| 115 | if (path_.empty()) { |
| 116 | LOG(ERROR) << "Empty key file path."; |
| 117 | return false; |
| 118 | } |
| 119 | string corrupted_path = path_.value() + kCorruptSuffix; |
| 120 | int ret = rename(path_.value().c_str(), corrupted_path.c_str()); |
| 121 | if (ret != 0) { |
| 122 | PLOG(ERROR) << "File rename failed"; |
| 123 | return false; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 128 | set<string> KeyFileStore::GetGroups() const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 129 | CHECK(key_file_); |
| 130 | gsize length = 0; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 131 | gchar **groups = glib_->KeyFileGetGroups(key_file_, &length); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 132 | if (!groups) { |
| 133 | LOG(ERROR) << "Unable to obtain groups."; |
| 134 | return set<string>(); |
| 135 | } |
| 136 | set<string> group_set(groups, groups + length); |
| 137 | glib_->Strfreev(groups); |
| 138 | return group_set; |
| 139 | } |
| 140 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 141 | // Returns a set so that caller can easily test whether a particular group |
| 142 | // is contained within this collection. |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 143 | set<string> KeyFileStore::GetGroupsWithKey(const string &key) const { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 144 | set<string> groups = GetGroups(); |
| 145 | set<string> groups_with_key; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 146 | for (const auto &group : groups) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 147 | if (glib_->KeyFileHasKey(key_file_, group.c_str(), key.c_str(), nullptr)) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 148 | groups_with_key.insert(group); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | return groups_with_key; |
| 152 | } |
| 153 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 154 | set<string> KeyFileStore::GetGroupsWithProperties( |
| 155 | const KeyValueStore &properties) const { |
| 156 | set<string> groups = GetGroups(); |
| 157 | set<string> groups_with_properties; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 158 | for (const auto &group : groups) { |
| 159 | if (DoesGroupMatchProperties(group, properties)) { |
| 160 | groups_with_properties.insert(group); |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | return groups_with_properties; |
| 164 | } |
| 165 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 166 | bool KeyFileStore::ContainsGroup(const string &group) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 167 | CHECK(key_file_); |
| 168 | return glib_->KeyFileHasGroup(key_file_, group.c_str()); |
| 169 | } |
| 170 | |
| 171 | bool KeyFileStore::DeleteKey(const string &group, const string &key) { |
| 172 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 173 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 174 | glib_->KeyFileRemoveKey(key_file_, group.c_str(), key.c_str(), &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 175 | if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 176 | LOG(ERROR) << "Failed to delete (" << group << ":" << key << "): " |
| 177 | << glib_->ConvertErrorToMessage(error); |
| 178 | return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | bool KeyFileStore::DeleteGroup(const string &group) { |
| 184 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 185 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 186 | glib_->KeyFileRemoveGroup(key_file_, group.c_str(), &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 187 | if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND) { |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 188 | LOG(ERROR) << "Failed to delete group " << group << ": " |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 189 | << glib_->ConvertErrorToMessage(error); |
| 190 | return false; |
| 191 | } |
| 192 | return true; |
| 193 | } |
| 194 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 195 | bool KeyFileStore::SetHeader(const string &header) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 196 | GError *error = nullptr; |
| 197 | glib_->KeyFileSetComment(key_file_, nullptr, nullptr, header.c_str(), &error); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 198 | if (error) { |
| 199 | LOG(ERROR) << "Failed to to set header: " |
| 200 | << glib_->ConvertErrorToMessage(error); |
| 201 | return false; |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 206 | bool KeyFileStore::GetString(const string &group, |
| 207 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 208 | string *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 209 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 210 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 211 | gchar *data = |
| 212 | glib_->KeyFileGetString(key_file_, group.c_str(), key.c_str(), &error); |
| 213 | if (!data) { |
Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 214 | string s = glib_->ConvertErrorToMessage(error); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 215 | SLOG(this, 10) << "Failed to lookup (" << group << ":" << key << "): " << s; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 216 | return false; |
| 217 | } |
| 218 | if (value) { |
| 219 | *value = data; |
| 220 | } |
| 221 | glib_->Free(data); |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | bool KeyFileStore::SetString(const string &group, |
| 226 | const string &key, |
| 227 | const string &value) { |
| 228 | CHECK(key_file_); |
| 229 | glib_->KeyFileSetString(key_file_, group.c_str(), key.c_str(), value.c_str()); |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool KeyFileStore::GetBool(const string &group, |
| 234 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 235 | bool *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 236 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 237 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 238 | gboolean data = |
| 239 | glib_->KeyFileGetBoolean(key_file_, group.c_str(), key.c_str(), &error); |
| 240 | if (error) { |
Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 241 | string s = glib_->ConvertErrorToMessage(error); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 242 | SLOG(this, 10) << "Failed to lookup (" << group << ":" << key << "): " << s; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | if (value) { |
| 246 | *value = data; |
| 247 | } |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | bool KeyFileStore::SetBool(const string &group, const string &key, bool value) { |
| 252 | CHECK(key_file_); |
| 253 | glib_->KeyFileSetBoolean(key_file_, |
| 254 | group.c_str(), |
| 255 | key.c_str(), |
| 256 | value ? TRUE : FALSE); |
| 257 | return true; |
| 258 | } |
| 259 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 260 | bool KeyFileStore::GetInt( |
| 261 | const string &group, const string &key, int *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 262 | CHECK(key_file_); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 263 | GError *error = nullptr; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 264 | gint data = |
| 265 | glib_->KeyFileGetInteger(key_file_, group.c_str(), key.c_str(), &error); |
| 266 | if (error) { |
Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 267 | string s = glib_->ConvertErrorToMessage(error); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 268 | SLOG(this, 10) << "Failed to lookup (" << group << ":" << key << "): " << s; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | if (value) { |
| 272 | *value = data; |
| 273 | } |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | bool KeyFileStore::SetInt(const string &group, const string &key, int value) { |
| 278 | CHECK(key_file_); |
| 279 | glib_->KeyFileSetInteger(key_file_, group.c_str(), key.c_str(), value); |
| 280 | return true; |
| 281 | } |
| 282 | |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 283 | bool KeyFileStore::GetUint64( |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 284 | const string &group, const string &key, uint64_t *value) const { |
| 285 | // Read the value in as a string and then convert to uint64_t because glib's |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 286 | // g_key_file_set_uint64 appears not to work correctly on 32-bit platforms |
| 287 | // in unit tests. |
| 288 | string data_string; |
| 289 | if (!GetString(group, key, &data_string)) { |
| 290 | return false; |
| 291 | } |
| 292 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 293 | uint64_t data; |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 294 | if (!base::StringToUint64(data_string, &data)) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 295 | SLOG(this, 10) << "Failed to convert (" << group << ":" << key << "): " |
| 296 | << "string to uint64_t conversion failed"; |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | |
| 300 | if (value) { |
| 301 | *value = data; |
| 302 | } |
| 303 | |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | bool KeyFileStore::SetUint64( |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 308 | const string &group, const string &key, uint64_t value) { |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 309 | // Convert the value to a string first, then save the value because glib's |
| 310 | // g_key_file_get_uint64 appears not to work on 32-bit platforms in our |
| 311 | // unit tests. |
| 312 | return SetString(group, key, base::Uint64ToString(value)); |
| 313 | } |
| 314 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 315 | bool KeyFileStore::GetStringList(const string &group, |
| 316 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 317 | vector<string> *value) const { |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 318 | CHECK(key_file_); |
| 319 | gsize length = 0; |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 320 | GError *error = nullptr; |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 321 | gchar **data = glib_->KeyFileGetStringList(key_file_, |
| 322 | group.c_str(), |
| 323 | key.c_str(), |
| 324 | &length, |
| 325 | &error); |
| 326 | if (!data) { |
Darin Petkov | c529c83 | 2012-04-18 14:59:42 +0200 | [diff] [blame] | 327 | string s = glib_->ConvertErrorToMessage(error); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 328 | SLOG(this, 10) << "Failed to lookup (" << group << ":" << key << "): " << s; |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | if (value) { |
| 332 | value->assign(data, data + length); |
| 333 | } |
| 334 | glib_->Strfreev(data); |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | bool KeyFileStore::SetStringList(const string &group, |
| 339 | const string &key, |
| 340 | const vector<string> &value) { |
| 341 | CHECK(key_file_); |
| 342 | vector<const char *> list; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 343 | for (const auto &string_entry : value) { |
| 344 | list.push_back(string_entry.c_str()); |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 345 | } |
| 346 | glib_->KeyFileSetStringList(key_file_, |
| 347 | group.c_str(), |
| 348 | key.c_str(), |
| 349 | list.data(), |
| 350 | list.size()); |
| 351 | return true; |
| 352 | } |
| 353 | |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 354 | bool KeyFileStore::GetCryptedString(const string &group, |
| 355 | const string &key, |
| 356 | string *value) { |
| 357 | if (!GetString(group, key, value)) { |
| 358 | return false; |
| 359 | } |
| 360 | if (value) { |
| 361 | *value = crypto_.Decrypt(*value); |
| 362 | } |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | bool KeyFileStore::SetCryptedString(const string &group, |
| 367 | const string &key, |
| 368 | const string &value) { |
| 369 | return SetString(group, key, crypto_.Encrypt(value)); |
| 370 | } |
| 371 | |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 372 | bool KeyFileStore::DoesGroupMatchProperties( |
| 373 | const string &group, const KeyValueStore &properties) const { |
| 374 | map<string, bool>::const_iterator bool_it; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 375 | for (const auto &property : properties.bool_properties()) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 376 | bool value; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 377 | if (!GetBool(group, property.first, &value) || value != property.second) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | } |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 381 | for (const auto &property : properties.int_properties()) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 382 | int value; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 383 | if (!GetInt(group, property.first, &value) || value != property.second) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | } |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 387 | for (const auto &property : properties.string_properties()) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 388 | string value; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 389 | if (!GetString(group, property.first, &value) || value != property.second) { |
Paul Stewart | 5b9ec98 | 2013-01-18 14:12:14 -0800 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | } |
| 393 | return true; |
| 394 | } |
| 395 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 396 | } // namespace shill |