OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/password_manager/native_backend_gnome_x.h" | 5 #include "chrome/browser/password_manager/native_backend_gnome_x.h" |
6 | 6 |
7 #include <dlfcn.h> | |
8 #include <gnome-keyring.h> | |
9 #include <stddef.h> | |
10 #include <stdint.h> | |
11 | |
12 #include <limits> | 7 #include <limits> |
13 #include <map> | 8 #include <map> |
14 #include <memory> | 9 #include <memory> |
15 #include <string> | 10 #include <string> |
16 #include <utility> | 11 #include <utility> |
17 #include <vector> | 12 #include <vector> |
18 | 13 |
19 #include "base/logging.h" | 14 #include "base/logging.h" |
20 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
21 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
(...skipping 13 matching lines...) Expand all Loading... |
35 using base::UTF8ToUTF16; | 30 using base::UTF8ToUTF16; |
36 using base::UTF16ToUTF8; | 31 using base::UTF16ToUTF8; |
37 using content::BrowserThread; | 32 using content::BrowserThread; |
38 using namespace password_manager::metrics_util; | 33 using namespace password_manager::metrics_util; |
39 using password_manager::PasswordStore; | 34 using password_manager::PasswordStore; |
40 | 35 |
41 namespace { | 36 namespace { |
42 const int kMaxPossibleTimeTValue = std::numeric_limits<int>::max(); | 37 const int kMaxPossibleTimeTValue = std::numeric_limits<int>::max(); |
43 } | 38 } |
44 | 39 |
45 decltype(&::gnome_keyring_is_available) | |
46 GnomeKeyringLoader::gnome_keyring_is_available_ptr; | |
47 decltype(&::gnome_keyring_store_password) | |
48 GnomeKeyringLoader::gnome_keyring_store_password_ptr; | |
49 decltype(&::gnome_keyring_delete_password) | |
50 GnomeKeyringLoader::gnome_keyring_delete_password_ptr; | |
51 decltype(&::gnome_keyring_find_items) | |
52 GnomeKeyringLoader::gnome_keyring_find_items_ptr; | |
53 decltype(&::gnome_keyring_result_to_message) | |
54 GnomeKeyringLoader::gnome_keyring_result_to_message_ptr; | |
55 decltype(&::gnome_keyring_attribute_list_free) | |
56 GnomeKeyringLoader::gnome_keyring_attribute_list_free_ptr; | |
57 decltype(&::gnome_keyring_attribute_list_new) | |
58 GnomeKeyringLoader::gnome_keyring_attribute_list_new_ptr; | |
59 decltype(&::gnome_keyring_attribute_list_append_string) | |
60 GnomeKeyringLoader::gnome_keyring_attribute_list_append_string_ptr; | |
61 decltype(&::gnome_keyring_attribute_list_append_uint32) | |
62 GnomeKeyringLoader::gnome_keyring_attribute_list_append_uint32_ptr; | |
63 | |
64 bool GnomeKeyringLoader::keyring_loaded = false; | |
65 | |
66 const GnomeKeyringLoader::FunctionInfo GnomeKeyringLoader::functions[] = { | |
67 {"gnome_keyring_is_available", | |
68 reinterpret_cast<void**>(&gnome_keyring_is_available_ptr)}, | |
69 {"gnome_keyring_store_password", | |
70 reinterpret_cast<void**>(&gnome_keyring_store_password_ptr)}, | |
71 {"gnome_keyring_delete_password", | |
72 reinterpret_cast<void**>(&gnome_keyring_delete_password_ptr)}, | |
73 {"gnome_keyring_find_items", | |
74 reinterpret_cast<void**>(&gnome_keyring_find_items_ptr)}, | |
75 {"gnome_keyring_result_to_message", | |
76 reinterpret_cast<void**>(&gnome_keyring_result_to_message_ptr)}, | |
77 {"gnome_keyring_attribute_list_free", | |
78 reinterpret_cast<void**>(&gnome_keyring_attribute_list_free_ptr)}, | |
79 {"gnome_keyring_attribute_list_new", | |
80 reinterpret_cast<void**>(&gnome_keyring_attribute_list_new_ptr)}, | |
81 {"gnome_keyring_attribute_list_append_string", | |
82 reinterpret_cast<void**>(&gnome_keyring_attribute_list_append_string_ptr)}, | |
83 {"gnome_keyring_attribute_list_append_uint32", | |
84 reinterpret_cast<void**>( | |
85 &gnome_keyring_attribute_list_append_uint32_ptr)}}; | |
86 | |
87 /* Load the library and initialize the function pointers. */ | |
88 bool GnomeKeyringLoader::LoadGnomeKeyring() { | |
89 if (keyring_loaded) | |
90 return true; | |
91 | |
92 void* handle = dlopen("libgnome-keyring.so.0", RTLD_NOW | RTLD_GLOBAL); | |
93 if (!handle) { | |
94 // We wanted to use GNOME Keyring, but we couldn't load it. Warn, because | |
95 // either the user asked for this, or we autodetected it incorrectly. (Or | |
96 // the system has broken libraries, which is also good to warn about.) | |
97 LOG(WARNING) << "Could not load libgnome-keyring.so.0: " << dlerror(); | |
98 return false; | |
99 } | |
100 | |
101 for (size_t i = 0; i < arraysize(functions); ++i) { | |
102 dlerror(); | |
103 *functions[i].pointer = dlsym(handle, functions[i].name); | |
104 const char* error = dlerror(); | |
105 if (error) { | |
106 LOG(ERROR) << "Unable to load symbol " | |
107 << functions[i].name << ": " << error; | |
108 dlclose(handle); | |
109 return false; | |
110 } | |
111 } | |
112 | |
113 keyring_loaded = true; | |
114 // We leak the library handle. That's OK: this function is called only once. | |
115 return true; | |
116 } | |
117 | |
118 namespace { | 40 namespace { |
119 | 41 |
120 const char kGnomeKeyringAppString[] = "chrome"; | 42 const char kGnomeKeyringAppString[] = "chrome"; |
121 | 43 |
122 // Convert the attributes of a given keyring entry into a new PasswordForm. | 44 // Convert the attributes of a given keyring entry into a new PasswordForm. |
123 // Note: does *not* get the actual password, as that is not a key attribute! | 45 // Note: does *not* get the actual password, as that is not a key attribute! |
124 // Returns NULL if the attributes are for the wrong application. | 46 // Returns NULL if the attributes are for the wrong application. |
125 std::unique_ptr<PasswordForm> FormFromAttributes( | 47 std::unique_ptr<PasswordForm> FormFromAttributes( |
126 GnomeKeyringAttributeList* attrs) { | 48 GnomeKeyringAttributeList* attrs) { |
127 // Read the string and int attributes into the appropriate map. | 49 // Read the string and int attributes into the appropriate map. |
(...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 ScopedVector<PasswordForm> forms; | 769 ScopedVector<PasswordForm> forms; |
848 if (!GetLoginsBetween(get_begin, get_end, date_to_compare, &forms)) | 770 if (!GetLoginsBetween(get_begin, get_end, date_to_compare, &forms)) |
849 return false; | 771 return false; |
850 | 772 |
851 for (size_t i = 0; i < forms.size(); ++i) { | 773 for (size_t i = 0; i < forms.size(); ++i) { |
852 if (!RemoveLogin(*forms[i], changes)) | 774 if (!RemoveLogin(*forms[i], changes)) |
853 return false; | 775 return false; |
854 } | 776 } |
855 return true; | 777 return true; |
856 } | 778 } |
OLD | NEW |