OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/os_crypt/libsecret_util_posix.h" |
| 6 |
| 7 #include <dlfcn.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 |
| 12 decltype( |
| 13 &::secret_password_store_sync) LibsecretLoader::secret_password_store_sync; |
| 14 decltype( |
| 15 &::secret_service_search_sync) LibsecretLoader::secret_service_search_sync; |
| 16 decltype( |
| 17 &::secret_password_clear_sync) LibsecretLoader::secret_password_clear_sync; |
| 18 decltype(&::secret_item_get_secret) LibsecretLoader::secret_item_get_secret; |
| 19 decltype(&::secret_value_get_text) LibsecretLoader::secret_value_get_text; |
| 20 decltype( |
| 21 &::secret_item_get_attributes) LibsecretLoader::secret_item_get_attributes; |
| 22 decltype(&::secret_item_load_secret_sync) |
| 23 LibsecretLoader::secret_item_load_secret_sync; |
| 24 decltype(&::secret_value_unref) LibsecretLoader::secret_value_unref; |
| 25 |
| 26 bool LibsecretLoader::libsecret_loaded_ = false; |
| 27 |
| 28 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", |
| 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", |
| 39 reinterpret_cast<void**>(&secret_item_get_attributes)}, |
| 40 {"secret_item_load_secret_sync", |
| 41 reinterpret_cast<void**>(&secret_item_load_secret_sync)}, |
| 42 {"secret_value_unref", reinterpret_cast<void**>(&secret_value_unref)}}; |
| 43 |
| 44 // static |
| 45 bool LibsecretLoader::EnsureLibsecretLoaded() { |
| 46 return LoadLibsecret() && LibsecretIsAvailable(); |
| 47 } |
| 48 |
| 49 // static |
| 50 bool LibsecretLoader::LoadLibsecret() { |
| 51 if (libsecret_loaded_) |
| 52 return true; |
| 53 |
| 54 void* handle = dlopen("libsecret-1.so.0", RTLD_NOW | RTLD_GLOBAL); |
| 55 if (!handle) { |
| 56 // 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 |
| 58 // 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 |
| 60 VLOG(1) << "Could not load libsecret-1.so.0: " << dlerror(); |
| 61 return false; |
| 62 } |
| 63 |
| 64 for (const auto& function : kFunctions) { |
| 65 dlerror(); |
| 66 *function.pointer = dlsym(handle, function.name); |
| 67 const char* error = dlerror(); |
| 68 if (error) { |
| 69 VLOG(1) << "Unable to load symbol " << function.name << ": " << error; |
| 70 dlclose(handle); |
| 71 return false; |
| 72 } |
| 73 } |
| 74 |
| 75 libsecret_loaded_ = true; |
| 76 // We leak the library handle. That's OK: this function is called only once. |
| 77 return true; |
| 78 } |
| 79 |
| 80 // static |
| 81 bool LibsecretLoader::LibsecretIsAvailable() { |
| 82 if (!libsecret_loaded_) |
| 83 return false; |
| 84 // A dummy query is made to check for availability, because libsecret doesn't |
| 85 // have a dedicated availability function. For performance reasons, the query |
| 86 // is meant to return an empty result. |
| 87 LibsecretAttributesBuilder attrs; |
| 88 attrs.Append("application", "chrome-string_to_get_empty_result"); |
| 89 const SecretSchema kDummySchema = { |
| 90 "_chrome_dummy_schema", |
| 91 SECRET_SCHEMA_DONT_MATCH_NAME, |
| 92 {{"application", SECRET_SCHEMA_ATTRIBUTE_STRING}, |
| 93 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}}}; |
| 94 |
| 95 GError* error = nullptr; |
| 96 GList* found = |
| 97 secret_service_search_sync(nullptr, // default secret service |
| 98 &kDummySchema, attrs.Get(), SECRET_SEARCH_ALL, |
| 99 nullptr, // no cancellable ojbect |
| 100 &error); |
| 101 bool success = (error == nullptr); |
| 102 if (error) |
| 103 g_error_free(error); |
| 104 if (found) |
| 105 g_list_free(found); |
| 106 |
| 107 return success; |
| 108 } |
| 109 |
| 110 LibsecretAttributesBuilder::LibsecretAttributesBuilder() { |
| 111 attrs_ = g_hash_table_new_full(g_str_hash, g_str_equal, |
| 112 nullptr, // no deleter for keys |
| 113 nullptr); // no deleter for values |
| 114 } |
| 115 |
| 116 LibsecretAttributesBuilder::~LibsecretAttributesBuilder() { |
| 117 g_hash_table_destroy(attrs_); |
| 118 } |
| 119 |
| 120 void LibsecretAttributesBuilder::Append(const std::string& name, |
| 121 const std::string& value) { |
| 122 name_values_.push_back(name); |
| 123 gpointer name_str = |
| 124 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
| 125 name_values_.push_back(value); |
| 126 gpointer value_str = |
| 127 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
| 128 g_hash_table_insert(attrs_, name_str, value_str); |
| 129 } |
| 130 |
| 131 void LibsecretAttributesBuilder::Append(const std::string& name, |
| 132 int64_t value) { |
| 133 Append(name, base::Int64ToString(value)); |
| 134 } |
OLD | NEW |