| 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_posix.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 // |
| 13 // LibsecretLoader |
| 14 // |
| 15 |
| 12 decltype( | 16 decltype( |
| 13 &::secret_password_store_sync) LibsecretLoader::secret_password_store_sync; | 17 &::secret_password_store_sync) LibsecretLoader::secret_password_store_sync; |
| 14 decltype( | 18 decltype( |
| 15 &::secret_service_search_sync) LibsecretLoader::secret_service_search_sync; | 19 &::secret_service_search_sync) LibsecretLoader::secret_service_search_sync; |
| 16 decltype( | 20 decltype( |
| 17 &::secret_password_clear_sync) LibsecretLoader::secret_password_clear_sync; | 21 &::secret_password_clear_sync) LibsecretLoader::secret_password_clear_sync; |
| 18 decltype(&::secret_item_get_secret) LibsecretLoader::secret_item_get_secret; | 22 decltype(&::secret_item_get_secret) LibsecretLoader::secret_item_get_secret; |
| 19 decltype(&::secret_value_get_text) LibsecretLoader::secret_value_get_text; | 23 decltype(&::secret_value_get_text) LibsecretLoader::secret_value_get_text; |
| 20 decltype( | 24 decltype( |
| 21 &::secret_item_get_attributes) LibsecretLoader::secret_item_get_attributes; | 25 &::secret_item_get_attributes) LibsecretLoader::secret_item_get_attributes; |
| 22 decltype(&::secret_item_load_secret_sync) | 26 decltype(&::secret_item_load_secret_sync) |
| 23 LibsecretLoader::secret_item_load_secret_sync; | 27 LibsecretLoader::secret_item_load_secret_sync; |
| 24 decltype(&::secret_value_unref) LibsecretLoader::secret_value_unref; | 28 decltype(&::secret_value_unref) LibsecretLoader::secret_value_unref; |
| 29 decltype( |
| 30 &::secret_service_lookup_sync) LibsecretLoader::secret_service_lookup_sync; |
| 25 | 31 |
| 26 bool LibsecretLoader::libsecret_loaded_ = false; | 32 bool LibsecretLoader::libsecret_loaded_ = false; |
| 27 | 33 |
| 28 const LibsecretLoader::FunctionInfo LibsecretLoader::kFunctions[] = { | 34 const LibsecretLoader::FunctionInfo LibsecretLoader::kFunctions[] = { |
| 29 {"secret_password_store_sync", | |
| 30 reinterpret_cast<void**>(&secret_password_store_sync)}, | |
| 31 {"secret_service_search_sync", | |
| 32 reinterpret_cast<void**>(&secret_service_search_sync)}, | |
| 33 {"secret_password_clear_sync", | |
| 34 reinterpret_cast<void**>(&secret_password_clear_sync)}, | |
| 35 {"secret_item_get_secret", | 35 {"secret_item_get_secret", |
| 36 reinterpret_cast<void**>(&secret_item_get_secret)}, | 36 reinterpret_cast<void**>(&secret_item_get_secret)}, |
| 37 {"secret_value_get_text", reinterpret_cast<void**>(&secret_value_get_text)}, | |
| 38 {"secret_item_get_attributes", | 37 {"secret_item_get_attributes", |
| 39 reinterpret_cast<void**>(&secret_item_get_attributes)}, | 38 reinterpret_cast<void**>(&secret_item_get_attributes)}, |
| 40 {"secret_item_load_secret_sync", | 39 {"secret_item_load_secret_sync", |
| 41 reinterpret_cast<void**>(&secret_item_load_secret_sync)}, | 40 reinterpret_cast<void**>(&secret_item_load_secret_sync)}, |
| 42 {"secret_value_unref", reinterpret_cast<void**>(&secret_value_unref)}}; | 41 {"secret_password_clear_sync", |
| 42 reinterpret_cast<void**>(&secret_password_clear_sync)}, |
| 43 {"secret_password_store_sync", |
| 44 reinterpret_cast<void**>(&secret_password_store_sync)}, |
| 45 {"secret_service_lookup_sync", |
| 46 reinterpret_cast<void**>(&secret_service_lookup_sync)}, |
| 47 {"secret_service_search_sync", |
| 48 reinterpret_cast<void**>(&secret_service_search_sync)}, |
| 49 {"secret_value_get_text", reinterpret_cast<void**>(&secret_value_get_text)}, |
| 50 {"secret_value_unref", reinterpret_cast<void**>(&secret_value_unref)}, |
| 51 }; |
| 43 | 52 |
| 44 // static | 53 // static |
| 45 bool LibsecretLoader::EnsureLibsecretLoaded() { | 54 bool LibsecretLoader::EnsureLibsecretLoaded() { |
| 46 return LoadLibsecret() && LibsecretIsAvailable(); | 55 return LoadLibsecret() && LibsecretIsAvailable(); |
| 47 } | 56 } |
| 48 | 57 |
| 49 // static | 58 // static |
| 50 bool LibsecretLoader::LoadLibsecret() { | 59 bool LibsecretLoader::LoadLibsecret() { |
| 51 if (libsecret_loaded_) | 60 if (libsecret_loaded_) |
| 52 return true; | 61 return true; |
| 53 | 62 |
| 54 void* handle = dlopen("libsecret-1.so.0", RTLD_NOW | RTLD_GLOBAL); | 63 static void* handle = dlopen("libsecret-1.so.0", RTLD_NOW | RTLD_GLOBAL); |
| 55 if (!handle) { | 64 if (!handle) { |
| 56 // We wanted to use libsecret, but we couldn't load it. Warn, because | 65 // We wanted to use libsecret, but we couldn't load it. Warn, because |
| 57 // either the user asked for this, or we autodetected it incorrectly. (Or | 66 // either the user asked for this, or we autodetected it incorrectly. (Or |
| 58 // the system has broken libraries, which is also good to warn about.) | 67 // the system has broken libraries, which is also good to warn about.) |
| 59 // TODO(crbug.com/607435): Channel this message to the user-facing log | 68 // TODO(crbug.com/607435): Channel this message to the user-facing log |
| 60 VLOG(1) << "Could not load libsecret-1.so.0: " << dlerror(); | 69 VLOG(1) << "Could not load libsecret-1.so.0: " << dlerror(); |
| 61 return false; | 70 return false; |
| 62 } | 71 } |
| 63 | 72 |
| 64 for (const auto& function : kFunctions) { | 73 for (const auto& function : kFunctions) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 &error); | 109 &error); |
| 101 bool success = (error == nullptr); | 110 bool success = (error == nullptr); |
| 102 if (error) | 111 if (error) |
| 103 g_error_free(error); | 112 g_error_free(error); |
| 104 if (found) | 113 if (found) |
| 105 g_list_free(found); | 114 g_list_free(found); |
| 106 | 115 |
| 107 return success; | 116 return success; |
| 108 } | 117 } |
| 109 | 118 |
| 119 // |
| 120 // LibsecretAttributesBuilder |
| 121 // |
| 122 |
| 110 LibsecretAttributesBuilder::LibsecretAttributesBuilder() { | 123 LibsecretAttributesBuilder::LibsecretAttributesBuilder() { |
| 111 attrs_ = g_hash_table_new_full(g_str_hash, g_str_equal, | 124 attrs_ = g_hash_table_new_full(g_str_hash, g_str_equal, |
| 112 nullptr, // no deleter for keys | 125 nullptr, // no deleter for keys |
| 113 nullptr); // no deleter for values | 126 nullptr); // no deleter for values |
| 114 } | 127 } |
| 115 | 128 |
| 116 LibsecretAttributesBuilder::~LibsecretAttributesBuilder() { | 129 LibsecretAttributesBuilder::~LibsecretAttributesBuilder() { |
| 117 g_hash_table_destroy(attrs_); | 130 g_hash_table_destroy(attrs_); |
| 118 } | 131 } |
| 119 | 132 |
| 120 void LibsecretAttributesBuilder::Append(const std::string& name, | 133 void LibsecretAttributesBuilder::Append(const std::string& name, |
| 121 const std::string& value) { | 134 const std::string& value) { |
| 122 name_values_.push_back(name); | 135 name_values_.push_back(name); |
| 123 gpointer name_str = | 136 gpointer name_str = |
| 124 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); | 137 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
| 125 name_values_.push_back(value); | 138 name_values_.push_back(value); |
| 126 gpointer value_str = | 139 gpointer value_str = |
| 127 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); | 140 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
| 128 g_hash_table_insert(attrs_, name_str, value_str); | 141 g_hash_table_insert(attrs_, name_str, value_str); |
| 129 } | 142 } |
| 130 | 143 |
| 131 void LibsecretAttributesBuilder::Append(const std::string& name, | 144 void LibsecretAttributesBuilder::Append(const std::string& name, |
| 132 int64_t value) { | 145 int64_t value) { |
| 133 Append(name, base::Int64ToString(value)); | 146 Append(name, base::Int64ToString(value)); |
| 134 } | 147 } |
| OLD | NEW |