Chromium Code Reviews| 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/os_crypt_posix_util_libsecret.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 bool LibsecretLoader::LibsecretIsAvailable() { | |
|
vabr (Chromium)
2016/04/28 14:42:13
// static
cfroussios
2016/04/28 16:26:57
Done.
| |
| 76 if (!libsecret_loaded) | |
| 77 return false; | |
| 78 // A dummy query is made to check for availability, because libsecret doesn't | |
| 79 // have a dedicated availability function. For performance reasons, the query | |
| 80 // is meant to return an empty result. | |
| 81 LibsecretAttributesBuilder attrs; | |
| 82 attrs.Append("application", "chrome-string_to_get_empty_result"); | |
| 83 const SecretSchema dummySchema = { | |
| 84 "_chrome_dummy_schema", | |
| 85 SECRET_SCHEMA_DONT_MATCH_NAME, | |
| 86 {{nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}}}; | |
| 87 | |
| 88 GError* error = nullptr; | |
| 89 GList* found = | |
| 90 secret_service_search_sync(nullptr, // default secret service | |
| 91 &dummySchema, attrs.Get(), SECRET_SEARCH_ALL, | |
| 92 nullptr, // no cancellable ojbect | |
| 93 &error); | |
| 94 bool success = (error == nullptr); | |
| 95 if (error) | |
| 96 g_error_free(error); | |
| 97 if (found) | |
| 98 g_list_free(found); | |
| 99 | |
| 100 return success; | |
| 101 } | |
| 102 | |
| 103 LibsecretAttributesBuilder::LibsecretAttributesBuilder() { | |
| 104 attrs_ = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 105 nullptr, // no deleter for keys | |
| 106 nullptr); // no deleter for values | |
| 107 } | |
| 108 | |
| 109 LibsecretAttributesBuilder::~LibsecretAttributesBuilder() { | |
| 110 g_hash_table_destroy(attrs_); | |
| 111 } | |
| 112 | |
| 113 void LibsecretAttributesBuilder::Append(const std::string& name, | |
| 114 const std::string& value) { | |
| 115 name_values_.push_back(name); | |
| 116 gpointer name_str = | |
| 117 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); | |
| 118 name_values_.push_back(value); | |
| 119 gpointer value_str = | |
| 120 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); | |
| 121 g_hash_table_insert(attrs_, name_str, value_str); | |
| 122 } | |
| 123 | |
| 124 void LibsecretAttributesBuilder::Append(const std::string& name, | |
| 125 int64_t value) { | |
| 126 Append(name, base::Int64ToString(value)); | |
| 127 } | |
| OLD | NEW |