Darin Petkov | 083047b | 2011-06-23 20:42:48 -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/key_file_store.h" |
| 6 | |
| 7 | #include <base/file_util.h> |
| 8 | #include <base/logging.h> |
| 9 | |
| 10 | using std::set; |
| 11 | using std::string; |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 12 | using std::vector; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 13 | |
| 14 | namespace shill { |
| 15 | |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 16 | KeyFileStore::KeyFileStore(GLib *glib) |
| 17 | : glib_(glib), |
| 18 | crypto_(glib), |
| 19 | key_file_(NULL) {} |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 20 | |
| 21 | KeyFileStore::~KeyFileStore() { |
| 22 | ReleaseKeyFile(); |
| 23 | } |
| 24 | |
| 25 | void KeyFileStore::ReleaseKeyFile() { |
| 26 | if (key_file_) { |
| 27 | glib_->KeyFileFree(key_file_); |
| 28 | key_file_ = NULL; |
| 29 | } |
| 30 | } |
| 31 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 32 | bool KeyFileStore::IsNonEmpty() const { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 33 | int64 file_size = 0; |
| 34 | return file_util::GetFileSize(path_, &file_size) && file_size != 0; |
| 35 | } |
| 36 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 37 | bool KeyFileStore::Open() { |
| 38 | CHECK(!path_.empty()); |
| 39 | CHECK(!key_file_); |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 40 | crypto_.Init(); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 41 | key_file_ = glib_->KeyFileNew(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 42 | if (!IsNonEmpty()) { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 43 | LOG(INFO) << "Creating a new key file at " << path_.value(); |
| 44 | return true; |
| 45 | } |
| 46 | GError *error = NULL; |
| 47 | if (glib_->KeyFileLoadFromFile( |
| 48 | key_file_, |
| 49 | path_.value().c_str(), |
| 50 | static_cast<GKeyFileFlags>(G_KEY_FILE_KEEP_COMMENTS | |
| 51 | G_KEY_FILE_KEEP_TRANSLATIONS), |
| 52 | &error)) { |
| 53 | return true; |
| 54 | } |
| 55 | LOG(ERROR) << "Failed to load key file from " << path_.value() << ": " |
| 56 | << glib_->ConvertErrorToMessage(error); |
| 57 | ReleaseKeyFile(); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | bool KeyFileStore::Close() { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 62 | bool success = Flush(); |
| 63 | ReleaseKeyFile(); |
| 64 | return success; |
| 65 | } |
| 66 | |
| 67 | bool KeyFileStore::Flush() { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 68 | CHECK(key_file_); |
| 69 | GError *error = NULL; |
| 70 | gsize length = 0; |
| 71 | gchar *data = glib_->KeyFileToData(key_file_, &length, &error); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 72 | |
| 73 | bool success = true; |
| 74 | if (path_.empty()) { |
| 75 | LOG(ERROR) << "Empty key file path."; |
| 76 | success = false; |
| 77 | } |
| 78 | if (success && (!data || error)) { |
| 79 | LOG(ERROR) << "Failed to convert key file to string: " |
| 80 | << glib_->ConvertErrorToMessage(error); |
| 81 | success = false; |
| 82 | } |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 83 | if (success) { |
| 84 | int written = file_util::WriteFile(path_, data, length); |
| 85 | if (written < 0 || |
| 86 | static_cast<unsigned int>(written) != length) { |
| 87 | LOG(ERROR) << "Failed to store key file: " << path_.value(); |
| 88 | success = false; |
| 89 | } |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 90 | } |
| 91 | glib_->Free(data); |
| 92 | return success; |
| 93 | } |
| 94 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 95 | set<string> KeyFileStore::GetGroups() const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 96 | CHECK(key_file_); |
| 97 | gsize length = 0; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 98 | gchar **groups = glib_->KeyFileGetGroups(key_file_, &length); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 99 | if (!groups) { |
| 100 | LOG(ERROR) << "Unable to obtain groups."; |
| 101 | return set<string>(); |
| 102 | } |
| 103 | set<string> group_set(groups, groups + length); |
| 104 | glib_->Strfreev(groups); |
| 105 | return group_set; |
| 106 | } |
| 107 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 108 | // Returns a set so that caller can easily test whether a particular group |
| 109 | // is contained within this collection. |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 110 | set<string> KeyFileStore::GetGroupsWithKey(const string &key) const { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 111 | set<string> groups = GetGroups(); |
| 112 | set<string> groups_with_key; |
| 113 | for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) { |
| 114 | if (glib_->KeyFileHasKey(key_file_, (*it).c_str(), key.c_str(), NULL)) { |
| 115 | groups_with_key.insert(*it); |
| 116 | } |
| 117 | } |
| 118 | return groups_with_key; |
| 119 | } |
| 120 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 121 | bool KeyFileStore::ContainsGroup(const string &group) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 122 | CHECK(key_file_); |
| 123 | return glib_->KeyFileHasGroup(key_file_, group.c_str()); |
| 124 | } |
| 125 | |
| 126 | bool KeyFileStore::DeleteKey(const string &group, const string &key) { |
| 127 | CHECK(key_file_); |
| 128 | GError *error = NULL; |
| 129 | glib_->KeyFileRemoveKey(key_file_, group.c_str(), key.c_str(), &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 130 | if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 131 | LOG(ERROR) << "Failed to delete (" << group << ":" << key << "): " |
| 132 | << glib_->ConvertErrorToMessage(error); |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | bool KeyFileStore::DeleteGroup(const string &group) { |
| 139 | CHECK(key_file_); |
| 140 | GError *error = NULL; |
| 141 | glib_->KeyFileRemoveGroup(key_file_, group.c_str(), &error); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 142 | if (error && error->code != G_KEY_FILE_ERROR_GROUP_NOT_FOUND) { |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 143 | LOG(ERROR) << "Failed to delete group " << group << ": " |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 144 | << glib_->ConvertErrorToMessage(error); |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 150 | bool KeyFileStore::SetHeader(const string &header) { |
| 151 | GError *error = NULL; |
| 152 | glib_->KeyFileSetComment(key_file_, NULL, NULL, header.c_str(), &error); |
| 153 | if (error) { |
| 154 | LOG(ERROR) << "Failed to to set header: " |
| 155 | << glib_->ConvertErrorToMessage(error); |
| 156 | return false; |
| 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 161 | bool KeyFileStore::GetString(const string &group, |
| 162 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 163 | string *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 164 | CHECK(key_file_); |
| 165 | GError *error = NULL; |
| 166 | gchar *data = |
| 167 | glib_->KeyFileGetString(key_file_, group.c_str(), key.c_str(), &error); |
| 168 | if (!data) { |
| 169 | LOG(ERROR) << "Failed to lookup (" << group << ":" << key << "): " |
| 170 | << glib_->ConvertErrorToMessage(error); |
| 171 | return false; |
| 172 | } |
| 173 | if (value) { |
| 174 | *value = data; |
| 175 | } |
| 176 | glib_->Free(data); |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | bool KeyFileStore::SetString(const string &group, |
| 181 | const string &key, |
| 182 | const string &value) { |
| 183 | CHECK(key_file_); |
| 184 | glib_->KeyFileSetString(key_file_, group.c_str(), key.c_str(), value.c_str()); |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | bool KeyFileStore::GetBool(const string &group, |
| 189 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 190 | bool *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 191 | CHECK(key_file_); |
| 192 | GError *error = NULL; |
| 193 | gboolean data = |
| 194 | glib_->KeyFileGetBoolean(key_file_, group.c_str(), key.c_str(), &error); |
| 195 | if (error) { |
| 196 | LOG(ERROR) << "Failed to lookup (" << group << ":" << key << "): " |
| 197 | << glib_->ConvertErrorToMessage(error); |
| 198 | return false; |
| 199 | } |
| 200 | if (value) { |
| 201 | *value = data; |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | bool KeyFileStore::SetBool(const string &group, const string &key, bool value) { |
| 207 | CHECK(key_file_); |
| 208 | glib_->KeyFileSetBoolean(key_file_, |
| 209 | group.c_str(), |
| 210 | key.c_str(), |
| 211 | value ? TRUE : FALSE); |
| 212 | return true; |
| 213 | } |
| 214 | |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 215 | bool KeyFileStore::GetInt( |
| 216 | const string &group, const string &key, int *value) const { |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 217 | CHECK(key_file_); |
| 218 | GError *error = NULL; |
| 219 | gint data = |
| 220 | glib_->KeyFileGetInteger(key_file_, group.c_str(), key.c_str(), &error); |
| 221 | if (error) { |
| 222 | LOG(ERROR) << "Failed to lookup (" << group << ":" << key << "): " |
| 223 | << glib_->ConvertErrorToMessage(error); |
| 224 | return false; |
| 225 | } |
| 226 | if (value) { |
| 227 | *value = data; |
| 228 | } |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | bool KeyFileStore::SetInt(const string &group, const string &key, int value) { |
| 233 | CHECK(key_file_); |
| 234 | glib_->KeyFileSetInteger(key_file_, group.c_str(), key.c_str(), value); |
| 235 | return true; |
| 236 | } |
| 237 | |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 238 | bool KeyFileStore::GetStringList(const string &group, |
| 239 | const string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 240 | vector<string> *value) const { |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 241 | CHECK(key_file_); |
| 242 | gsize length = 0; |
| 243 | GError *error = NULL; |
| 244 | gchar **data = glib_->KeyFileGetStringList(key_file_, |
| 245 | group.c_str(), |
| 246 | key.c_str(), |
| 247 | &length, |
| 248 | &error); |
| 249 | if (!data) { |
| 250 | LOG(ERROR) << "Failed to lookup (" << group << ":" << key << "): " |
| 251 | << glib_->ConvertErrorToMessage(error); |
| 252 | return false; |
| 253 | } |
| 254 | if (value) { |
| 255 | value->assign(data, data + length); |
| 256 | } |
| 257 | glib_->Strfreev(data); |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | bool KeyFileStore::SetStringList(const string &group, |
| 262 | const string &key, |
| 263 | const vector<string> &value) { |
| 264 | CHECK(key_file_); |
| 265 | vector<const char *> list; |
| 266 | for (vector<string>::const_iterator it = value.begin(); |
| 267 | it != value.end(); ++it) { |
| 268 | list.push_back(it->c_str()); |
| 269 | } |
| 270 | glib_->KeyFileSetStringList(key_file_, |
| 271 | group.c_str(), |
| 272 | key.c_str(), |
| 273 | list.data(), |
| 274 | list.size()); |
| 275 | return true; |
| 276 | } |
| 277 | |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 278 | bool KeyFileStore::GetCryptedString(const string &group, |
| 279 | const string &key, |
| 280 | string *value) { |
| 281 | if (!GetString(group, key, value)) { |
| 282 | return false; |
| 283 | } |
| 284 | if (value) { |
| 285 | *value = crypto_.Decrypt(*value); |
| 286 | } |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | bool KeyFileStore::SetCryptedString(const string &group, |
| 291 | const string &key, |
| 292 | const string &value) { |
| 293 | return SetString(group, key, crypto_.Encrypt(value)); |
| 294 | } |
| 295 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 296 | } // namespace shill |