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::functions[] = { |
| 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::LoadLibsecret() { |
| 46 if (libsecret_loaded) |
| 47 return true; |
| 48 |
| 49 void* handle = dlopen("libsecret-1.so.0", RTLD_NOW | RTLD_GLOBAL); |
| 50 if (!handle) { |
| 51 // We wanted to use libsecret, but we couldn't load it. Warn, because |
| 52 // either the user asked for this, or we autodetected it incorrectly. (Or |
| 53 // the system has broken libraries, which is also good to warn about.) |
| 54 // TODO(crbug.com/607435): Channel this message to the user-facing log |
| 55 VLOG(1) << "Could not load libsecret-1.so.0: " << dlerror(); |
| 56 return false; |
| 57 } |
| 58 |
| 59 for (const auto& function : functions) { |
| 60 dlerror(); |
| 61 *function.pointer = dlsym(handle, function.name); |
| 62 const char* error = dlerror(); |
| 63 if (error) { |
| 64 VLOG(1) << "Unable to load symbol " << function.name << ": " << error; |
| 65 dlclose(handle); |
| 66 return false; |
| 67 } |
| 68 } |
| 69 |
| 70 libsecret_loaded = true; |
| 71 // We leak the library handle. That's OK: this function is called only once. |
| 72 return true; |
| 73 } |
| 74 |
| 75 // static |
| 76 bool LibsecretLoader::LibsecretIsAvailable() { |
| 77 if (!libsecret_loaded) |
| 78 return false; |
| 79 // A dummy query is made to check for availability, because libsecret doesn't |
| 80 // have a dedicated availability function. For performance reasons, the query |
| 81 // is meant to return an empty result. |
| 82 LibsecretAttributesBuilder attrs; |
| 83 attrs.Append("application", "chrome-string_to_get_empty_result"); |
| 84 const SecretSchema dummySchema = { |
| 85 "_chrome_dummy_schema", |
| 86 SECRET_SCHEMA_DONT_MATCH_NAME, |
| 87 {{nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}}}; |
| 88 |
| 89 GError* error = nullptr; |
| 90 GList* found = |
| 91 secret_service_search_sync(nullptr, // default secret service |
| 92 &dummySchema, attrs.Get(), SECRET_SEARCH_ALL, |
| 93 nullptr, // no cancellable ojbect |
| 94 &error); |
| 95 bool success = (error == nullptr); |
| 96 if (error) |
| 97 g_error_free(error); |
| 98 if (found) |
| 99 g_list_free(found); |
| 100 |
| 101 return success; |
| 102 } |
| 103 |
| 104 LibsecretAttributesBuilder::LibsecretAttributesBuilder() { |
| 105 attrs_ = g_hash_table_new_full(g_str_hash, g_str_equal, |
| 106 nullptr, // no deleter for keys |
| 107 nullptr); // no deleter for values |
| 108 } |
| 109 |
| 110 LibsecretAttributesBuilder::~LibsecretAttributesBuilder() { |
| 111 g_hash_table_destroy(attrs_); |
| 112 } |
| 113 |
| 114 void LibsecretAttributesBuilder::Append(const std::string& name, |
| 115 const std::string& value) { |
| 116 name_values_.push_back(name); |
| 117 gpointer name_str = |
| 118 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
| 119 name_values_.push_back(value); |
| 120 gpointer value_str = |
| 121 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
| 122 g_hash_table_insert(attrs_, name_str, value_str); |
| 123 } |
| 124 |
| 125 void LibsecretAttributesBuilder::Append(const std::string& name, |
| 126 int64_t value) { |
| 127 Append(name, base::Int64ToString(value)); |
| 128 } |
OLD | NEW |