Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/os_crypt/libsecret_util_linux.h" | 5 #include "components/os_crypt/libsecret_util_linux.h" |
| 6 | 6 |
| 7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 | 11 |
| 12 // | 12 // |
| 13 // LibsecretLoader | 13 // LibsecretLoader |
| 14 // | 14 // |
| 15 | 15 |
| 16 namespace { | |
| 17 | |
| 18 // TODO(crbug.com/660005) A message that is attached to useless entries that we | |
| 19 // create, to explain its existence. | |
| 20 const char kExplanationMessage[] = | |
| 21 "Because of quirks in the gnome libsecret API, Chrome needs to store a " | |
| 22 "dummy entry to quarantee that this keyring was properly unlocked. More " | |
| 23 "details at http://crbug.com/660005."; | |
| 24 | |
| 25 // True if we're already ensured that the default keyring has been unlocked | |
| 26 // once. | |
| 27 bool s_ensured_keyring_unlocked = false; | |
|
vasilii
2016/10/28 12:56:06
Are you thread-safe?
I'm not a big fan of globals.
cfroussios
2016/10/28 13:50:12
EnsureDefaultUnlocked() is not necessary if a keyr
vasilii
2016/10/28 14:17:48
Given that you call the method only once from KeyS
cfroussios
2016/10/28 14:36:39
Done.
| |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 16 decltype( | 31 decltype( |
| 17 &::secret_password_store_sync) LibsecretLoader::secret_password_store_sync; | 32 &::secret_password_store_sync) LibsecretLoader::secret_password_store_sync; |
| 18 decltype( | 33 decltype( |
| 19 &::secret_service_search_sync) LibsecretLoader::secret_service_search_sync; | 34 &::secret_service_search_sync) LibsecretLoader::secret_service_search_sync; |
| 20 decltype( | 35 decltype( |
| 21 &::secret_password_clear_sync) LibsecretLoader::secret_password_clear_sync; | 36 &::secret_password_clear_sync) LibsecretLoader::secret_password_clear_sync; |
| 22 decltype(&::secret_item_get_secret) LibsecretLoader::secret_item_get_secret; | 37 decltype(&::secret_item_get_secret) LibsecretLoader::secret_item_get_secret; |
| 23 decltype(&::secret_value_get_text) LibsecretLoader::secret_value_get_text; | 38 decltype(&::secret_value_get_text) LibsecretLoader::secret_value_get_text; |
| 24 decltype( | 39 decltype( |
| 25 &::secret_item_get_attributes) LibsecretLoader::secret_item_get_attributes; | 40 &::secret_item_get_attributes) LibsecretLoader::secret_item_get_attributes; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 &error); | 121 &error); |
| 107 bool success = (error == nullptr); | 122 bool success = (error == nullptr); |
| 108 if (error) | 123 if (error) |
| 109 g_error_free(error); | 124 g_error_free(error); |
| 110 if (found) | 125 if (found) |
| 111 g_list_free(found); | 126 g_list_free(found); |
| 112 | 127 |
| 113 return success; | 128 return success; |
| 114 } | 129 } |
| 115 | 130 |
| 131 // TODO(crbug.com/660005) This is needed to properly unlock the default keyring. | |
| 132 // We don't need to ever read it. | |
| 133 void LibsecretLoader::EnsureKeyringUnlocked() { | |
| 134 if (s_ensured_keyring_unlocked) | |
| 135 return; | |
| 136 | |
| 137 VLOG(1) << "Adding dummy entry to keyring to ensure that it unlocked " | |
| 138 "properly."; | |
|
vasilii
2016/10/28 12:56:06
Do you need this print on every startup?
cfroussios
2016/10/28 14:36:39
Done.
| |
| 139 | |
| 140 const SecretSchema kDummySchema = { | |
| 141 "_chrome_dummy_schema_for_unlocking", | |
| 142 SECRET_SCHEMA_NONE, | |
| 143 {{"explanation", SECRET_SCHEMA_ATTRIBUTE_STRING}, | |
| 144 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}}}; | |
| 145 | |
| 146 GError* error = nullptr; | |
| 147 bool success = LibsecretLoader::secret_password_store_sync( | |
| 148 &kDummySchema, nullptr /* default keyring */, | |
| 149 "Chrome Safe Storage Control" /* entry title */, | |
| 150 "The meaning of life" /* password */, nullptr, &error, "explanation", | |
| 151 kExplanationMessage, | |
| 152 nullptr /* null-terminated variable argument list */); | |
| 153 if (error) { | |
| 154 VLOG(1) << "Dummy store to unlock the default keyring failed: " | |
| 155 << error->message; | |
| 156 g_error_free(error); | |
| 157 } else if (!success) { | |
| 158 VLOG(1) << "Dummy store to unlock the default keyring failed."; | |
| 159 } else { | |
| 160 s_ensured_keyring_unlocked = true; | |
| 161 } | |
| 162 } | |
| 163 | |
| 116 // | 164 // |
| 117 // LibsecretAttributesBuilder | 165 // LibsecretAttributesBuilder |
| 118 // | 166 // |
| 119 | 167 |
| 120 LibsecretAttributesBuilder::LibsecretAttributesBuilder() { | 168 LibsecretAttributesBuilder::LibsecretAttributesBuilder() { |
| 121 attrs_ = g_hash_table_new_full(g_str_hash, g_str_equal, | 169 attrs_ = g_hash_table_new_full(g_str_hash, g_str_equal, |
| 122 nullptr, // no deleter for keys | 170 nullptr, // no deleter for keys |
| 123 nullptr); // no deleter for values | 171 nullptr); // no deleter for values |
| 124 } | 172 } |
| 125 | 173 |
| 126 LibsecretAttributesBuilder::~LibsecretAttributesBuilder() { | 174 LibsecretAttributesBuilder::~LibsecretAttributesBuilder() { |
| 127 g_hash_table_destroy(attrs_); | 175 g_hash_table_destroy(attrs_); |
| 128 } | 176 } |
| 129 | 177 |
| 130 void LibsecretAttributesBuilder::Append(const std::string& name, | 178 void LibsecretAttributesBuilder::Append(const std::string& name, |
| 131 const std::string& value) { | 179 const std::string& value) { |
| 132 name_values_.push_back(name); | 180 name_values_.push_back(name); |
| 133 gpointer name_str = | 181 gpointer name_str = |
| 134 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); | 182 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
| 135 name_values_.push_back(value); | 183 name_values_.push_back(value); |
| 136 gpointer value_str = | 184 gpointer value_str = |
| 137 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); | 185 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
| 138 g_hash_table_insert(attrs_, name_str, value_str); | 186 g_hash_table_insert(attrs_, name_str, value_str); |
| 139 } | 187 } |
| 140 | 188 |
| 141 void LibsecretAttributesBuilder::Append(const std::string& name, | 189 void LibsecretAttributesBuilder::Append(const std::string& name, |
| 142 int64_t value) { | 190 int64_t value) { |
| 143 Append(name, base::Int64ToString(value)); | 191 Append(name, base::Int64ToString(value)); |
| 144 } | 192 } |
| OLD | NEW |