Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / password_manager / native_backend_gnome_x.cc
blob198d73fa5794b15dff9ca8869e2d4ac563b6f9ba
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
3 // found in the LICENSE file.
5 #include "chrome/browser/password_manager/native_backend_gnome_x.h"
7 #include <dlfcn.h>
8 #include <gnome-keyring.h>
10 #include <map>
11 #include <string>
12 #include <vector>
14 #include "base/basictypes.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/metrics/histogram.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_piece.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/synchronization/waitable_event.h"
24 #include "base/time/time.h"
25 #include "components/autofill/core/common/password_form.h"
26 #include "components/password_manager/core/browser/password_manager_metrics_util.h"
27 #include "components/password_manager/core/browser/psl_matching_helper.h"
28 #include "content/public/browser/browser_thread.h"
30 using autofill::PasswordForm;
31 using base::UTF8ToUTF16;
32 using base::UTF16ToUTF8;
33 using content::BrowserThread;
34 using namespace password_manager::metrics_util;
36 namespace {
37 const int kMaxPossibleTimeTValue = std::numeric_limits<int>::max();
40 #define GNOME_KEYRING_DEFINE_POINTER(name) \
41 typeof(&::gnome_keyring_##name) GnomeKeyringLoader::gnome_keyring_##name;
42 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DEFINE_POINTER)
43 #undef GNOME_KEYRING_DEFINE_POINTER
45 bool GnomeKeyringLoader::keyring_loaded = false;
47 #if defined(DLOPEN_GNOME_KEYRING)
49 #define GNOME_KEYRING_FUNCTION_INFO(name) \
50 {"gnome_keyring_"#name, reinterpret_cast<void**>(&gnome_keyring_##name)},
51 const GnomeKeyringLoader::FunctionInfo GnomeKeyringLoader::functions[] = {
52 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_FUNCTION_INFO)
53 {nullptr, nullptr}
55 #undef GNOME_KEYRING_FUNCTION_INFO
57 /* Load the library and initialize the function pointers. */
58 bool GnomeKeyringLoader::LoadGnomeKeyring() {
59 if (keyring_loaded)
60 return true;
62 void* handle = dlopen("libgnome-keyring.so.0", RTLD_NOW | RTLD_GLOBAL);
63 if (!handle) {
64 // We wanted to use GNOME Keyring, but we couldn't load it. Warn, because
65 // either the user asked for this, or we autodetected it incorrectly. (Or
66 // the system has broken libraries, which is also good to warn about.)
67 LOG(WARNING) << "Could not load libgnome-keyring.so.0: " << dlerror();
68 return false;
71 for (size_t i = 0; functions[i].name; ++i) {
72 dlerror();
73 *functions[i].pointer = dlsym(handle, functions[i].name);
74 const char* error = dlerror();
75 if (error) {
76 LOG(ERROR) << "Unable to load symbol "
77 << functions[i].name << ": " << error;
78 dlclose(handle);
79 return false;
83 keyring_loaded = true;
84 // We leak the library handle. That's OK: this function is called only once.
85 return true;
88 #else // defined(DLOPEN_GNOME_KEYRING)
90 bool GnomeKeyringLoader::LoadGnomeKeyring() {
91 if (keyring_loaded)
92 return true;
93 #define GNOME_KEYRING_ASSIGN_POINTER(name) \
94 gnome_keyring_##name = &::gnome_keyring_##name;
95 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_ASSIGN_POINTER)
96 #undef GNOME_KEYRING_ASSIGN_POINTER
97 keyring_loaded = true;
98 return true;
101 #endif // defined(DLOPEN_GNOME_KEYRING)
103 namespace {
105 const char kGnomeKeyringAppString[] = "chrome";
107 // Convert the attributes of a given keyring entry into a new PasswordForm.
108 // Note: does *not* get the actual password, as that is not a key attribute!
109 // Returns NULL if the attributes are for the wrong application.
110 scoped_ptr<PasswordForm> FormFromAttributes(GnomeKeyringAttributeList* attrs) {
111 // Read the string and int attributes into the appropriate map.
112 std::map<std::string, std::string> string_attr_map;
113 std::map<std::string, uint32_t> uint_attr_map;
114 for (guint i = 0; i < attrs->len; ++i) {
115 GnomeKeyringAttribute attr = gnome_keyring_attribute_list_index(attrs, i);
116 if (attr.type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING)
117 string_attr_map[attr.name] = attr.value.string;
118 else if (attr.type == GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32)
119 uint_attr_map[attr.name] = attr.value.integer;
121 // Check to make sure this is a password we care about.
122 const std::string& app_value = string_attr_map["application"];
123 if (!base::StringPiece(app_value).starts_with(kGnomeKeyringAppString))
124 return scoped_ptr<PasswordForm>();
126 scoped_ptr<PasswordForm> form(new PasswordForm());
127 form->origin = GURL(string_attr_map["origin_url"]);
128 form->action = GURL(string_attr_map["action_url"]);
129 form->username_element = UTF8ToUTF16(string_attr_map["username_element"]);
130 form->username_value = UTF8ToUTF16(string_attr_map["username_value"]);
131 form->password_element = UTF8ToUTF16(string_attr_map["password_element"]);
132 form->submit_element = UTF8ToUTF16(string_attr_map["submit_element"]);
133 form->signon_realm = string_attr_map["signon_realm"];
134 form->ssl_valid = uint_attr_map["ssl_valid"];
135 form->preferred = uint_attr_map["preferred"];
136 int64 date_created = 0;
137 bool date_ok = base::StringToInt64(string_attr_map["date_created"],
138 &date_created);
139 DCHECK(date_ok);
140 // In the past |date_created| was stored as time_t. Currently is stored as
141 // base::Time's internal value. We need to distinguish, which format the
142 // number in |date_created| was stored in. We use the fact that
143 // kMaxPossibleTimeTValue interpreted as the internal value corresponds to an
144 // unlikely date back in 17th century, and anything above
145 // kMaxPossibleTimeTValue clearly must be in the internal value format.
146 form->date_created = date_created < kMaxPossibleTimeTValue
147 ? base::Time::FromTimeT(date_created)
148 : base::Time::FromInternalValue(date_created);
149 form->blacklisted_by_user = uint_attr_map["blacklisted_by_user"];
150 form->type = static_cast<PasswordForm::Type>(uint_attr_map["type"]);
151 form->times_used = uint_attr_map["times_used"];
152 form->scheme = static_cast<PasswordForm::Scheme>(uint_attr_map["scheme"]);
153 int64 date_synced = 0;
154 base::StringToInt64(string_attr_map["date_synced"], &date_synced);
155 form->date_synced = base::Time::FromInternalValue(date_synced);
156 form->display_name = UTF8ToUTF16(string_attr_map["display_name"]);
157 form->avatar_url = GURL(string_attr_map["avatar_url"]);
158 form->federation_url = GURL(string_attr_map["federation_url"]);
159 form->skip_zero_click = uint_attr_map["skip_zero_click"];
160 form->generation_upload_status =
161 static_cast<PasswordForm::GenerationUploadStatus>(
162 uint_attr_map["generation_upload_status"]);
163 if (!string_attr_map["form_data"].empty()) {
164 bool success = DeserializeFormDataFromBase64String(
165 string_attr_map["form_data"], &form->form_data);
166 FormDeserializationStatus status = success ? GNOME_SUCCESS : GNOME_FAILURE;
167 LogFormDataDeserializationStatus(status);
169 return form.Pass();
172 // Converts native credentials in |found| to PasswordForms. If not NULL,
173 // |lookup_form| is used to filter out results -- only credentials with signon
174 // realms passing the PSL matching against |lookup_form->signon_realm| will be
175 // kept. PSL matched results get their signon_realm, origin, and action
176 // rewritten to those of |lookup_form_|, with the original signon_realm saved
177 // into the result's original_signon_realm data member.
178 ScopedVector<PasswordForm> ConvertFormList(GList* found,
179 const PasswordForm* lookup_form) {
180 ScopedVector<PasswordForm> forms;
181 password_manager::PSLDomainMatchMetric psl_domain_match_metric =
182 password_manager::PSL_DOMAIN_MATCH_NONE;
183 for (GList* element = g_list_first(found); element;
184 element = g_list_next(element)) {
185 GnomeKeyringFound* data = static_cast<GnomeKeyringFound*>(element->data);
186 GnomeKeyringAttributeList* attrs = data->attributes;
188 scoped_ptr<PasswordForm> form(FormFromAttributes(attrs));
189 if (form) {
190 if (lookup_form && form->signon_realm != lookup_form->signon_realm) {
191 // This is not an exact match, we try PSL matching.
192 if (lookup_form->scheme != PasswordForm::SCHEME_HTML ||
193 form->scheme != PasswordForm::SCHEME_HTML ||
194 !(password_manager::IsPublicSuffixDomainMatch(
195 lookup_form->signon_realm, form->signon_realm))) {
196 continue;
198 psl_domain_match_metric = password_manager::PSL_DOMAIN_MATCH_FOUND;
199 form->original_signon_realm = form->signon_realm;
200 form->signon_realm = lookup_form->signon_realm;
201 form->origin = lookup_form->origin;
202 form->action = lookup_form->action;
204 if (data->secret) {
205 form->password_value = UTF8ToUTF16(data->secret);
206 } else {
207 LOG(WARNING) << "Unable to access password from list element!";
209 forms.push_back(form.release());
210 } else {
211 LOG(WARNING) << "Could not initialize PasswordForm from attributes!";
214 if (lookup_form) {
215 const GURL signon_realm(lookup_form->signon_realm);
216 std::string registered_domain =
217 password_manager::GetRegistryControlledDomain(signon_realm);
218 UMA_HISTOGRAM_ENUMERATION(
219 "PasswordManager.PslDomainMatchTriggering",
220 password_manager::ShouldPSLDomainMatchingApply(registered_domain)
221 ? psl_domain_match_metric
222 : password_manager::PSL_DOMAIN_MATCH_NOT_USED,
223 password_manager::PSL_DOMAIN_MATCH_COUNT);
225 return forms.Pass();
228 // Schema is analagous to the fields in PasswordForm.
229 const GnomeKeyringPasswordSchema kGnomeSchema = {
230 GNOME_KEYRING_ITEM_GENERIC_SECRET, {
231 { "origin_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
232 { "action_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
233 { "username_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
234 { "username_value", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
235 { "password_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
236 { "submit_element", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
237 { "signon_realm", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
238 { "ssl_valid", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
239 { "preferred", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
240 { "date_created", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
241 { "blacklisted_by_user", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
242 { "scheme", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
243 { "type", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
244 { "times_used", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
245 { "date_synced", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
246 { "display_name", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
247 { "avatar_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
248 { "federation_url", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
249 { "skip_zero_click", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
250 { "generation_upload_status", GNOME_KEYRING_ATTRIBUTE_TYPE_UINT32 },
251 { "form_data", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
252 // This field is always "chrome" so that we can search for it.
253 { "application", GNOME_KEYRING_ATTRIBUTE_TYPE_STRING },
254 { nullptr }
258 // Sadly, PasswordStore goes to great lengths to switch from the originally
259 // calling thread to the DB thread, and to provide an asynchronous API to
260 // callers while using a synchronous (virtual) API provided by subclasses like
261 // PasswordStoreX -- but GNOME Keyring really wants to be on the GLib main
262 // thread, which is the UI thread to us. So we end up having to switch threads
263 // again, possibly back to the very same thread (in case the UI thread is the
264 // caller, e.g. in the password management UI), and *block* the DB thread
265 // waiting for a response from the UI thread to provide the synchronous API
266 // PasswordStore expects of us. (It will then in turn switch back to the
267 // original caller to send the asynchronous reply to the original request.)
269 // This class represents a call to a GNOME Keyring method. A RunnableMethod
270 // should be posted to the UI thread to call one of its action methods, and then
271 // a WaitResult() method should be called to wait for the result. Each instance
272 // supports only one outstanding method at a time, though multiple instances may
273 // be used in parallel.
274 class GKRMethod : public GnomeKeyringLoader {
275 public:
276 GKRMethod() : event_(false, false), result_(GNOME_KEYRING_RESULT_CANCELLED) {}
278 // Action methods. These call gnome_keyring_* functions. Call from UI thread.
279 // See GetProfileSpecificAppString() for more information on the app string.
280 void AddLogin(const PasswordForm& form, const char* app_string);
281 void AddLoginSearch(const PasswordForm& form, const char* app_string);
282 void UpdateLoginSearch(const PasswordForm& form, const char* app_string);
283 void RemoveLogin(const PasswordForm& form, const char* app_string);
284 void GetLogins(const PasswordForm& form, const char* app_string);
285 void GetLoginsList(uint32_t blacklisted_by_user, const char* app_string);
286 void GetAllLogins(const char* app_string);
288 // Use after AddLogin, RemoveLogin.
289 GnomeKeyringResult WaitResult();
291 // Use after AddLoginSearch, UpdateLoginSearch, GetLogins, GetLoginsList,
292 // GetAllLogins. Replaces the content of |forms| with found logins.
293 GnomeKeyringResult WaitResult(ScopedVector<PasswordForm>* forms);
295 private:
296 struct GnomeKeyringAttributeListFreeDeleter {
297 inline void operator()(void* list) const {
298 gnome_keyring_attribute_list_free(
299 static_cast<GnomeKeyringAttributeList*>(list));
303 typedef scoped_ptr<GnomeKeyringAttributeList,
304 GnomeKeyringAttributeListFreeDeleter> ScopedAttributeList;
306 // Helper methods to abbreviate Gnome Keyring long API names.
307 static void AppendString(ScopedAttributeList* list,
308 const char* name,
309 const char* value);
310 static void AppendString(ScopedAttributeList* list,
311 const char* name,
312 const std::string& value);
313 static void AppendUint32(ScopedAttributeList* list,
314 const char* name,
315 guint32 value);
317 // All these callbacks are called on UI thread.
318 static void OnOperationDone(GnomeKeyringResult result, gpointer data);
320 // This is marked as static, but acts on the GKRMethod instance that |data|
321 // points to. Saves |result| to |result_|. If the result is OK, overwrites
322 // |forms_| with the found credentials. Clears |forms_| otherwise.
323 static void OnOperationGetList(GnomeKeyringResult result, GList* list,
324 gpointer data);
326 base::WaitableEvent event_;
327 GnomeKeyringResult result_;
328 ScopedVector<PasswordForm> forms_;
329 // If the credential search is specified by a single form and needs to use PSL
330 // matching, then the specifying form is stored in |lookup_form_|. If PSL
331 // matching is used to find a result, then the results signon realm, origin
332 // and action are stored are replaced by those of |lookup_form_|.
333 // Additionally, |lookup_form_->signon_realm| is also used to narrow down the
334 // found logins to those which indeed PSL-match the look-up. And finally,
335 // |lookup_form_| set to NULL means that PSL matching is not required.
336 scoped_ptr<PasswordForm> lookup_form_;
339 void GKRMethod::AddLogin(const PasswordForm& form, const char* app_string) {
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
341 int64 date_created = form.date_created.ToInternalValue();
342 // If we are asked to save a password with 0 date, use the current time.
343 // We don't want to actually save passwords as though on January 1, 1601.
344 if (!date_created)
345 date_created = base::Time::Now().ToInternalValue();
346 int64 date_synced = form.date_synced.ToInternalValue();
347 std::string form_data;
348 SerializeFormDataToBase64String(form.form_data, &form_data);
349 gnome_keyring_store_password(
350 &kGnomeSchema,
351 nullptr, // Default keyring.
352 form.origin.spec().c_str(), // Display name.
353 UTF16ToUTF8(form.password_value).c_str(),
354 OnOperationDone,
355 this, // data
356 nullptr, // destroy_data
357 "origin_url", form.origin.spec().c_str(),
358 "action_url", form.action.spec().c_str(),
359 "username_element", UTF16ToUTF8(form.username_element).c_str(),
360 "username_value", UTF16ToUTF8(form.username_value).c_str(),
361 "password_element", UTF16ToUTF8(form.password_element).c_str(),
362 "submit_element", UTF16ToUTF8(form.submit_element).c_str(),
363 "signon_realm", form.signon_realm.c_str(),
364 "ssl_valid", form.ssl_valid,
365 "preferred", form.preferred,
366 "date_created", base::Int64ToString(date_created).c_str(),
367 "blacklisted_by_user", form.blacklisted_by_user,
368 "type", form.type,
369 "times_used", form.times_used,
370 "scheme", form.scheme,
371 "date_synced", base::Int64ToString(date_synced).c_str(),
372 "display_name", UTF16ToUTF8(form.display_name).c_str(),
373 "avatar_url", form.avatar_url.spec().c_str(),
374 "federation_url", form.federation_url.spec().c_str(),
375 "skip_zero_click", form.skip_zero_click,
376 "generation_upload_status", form.generation_upload_status,
377 "form_data", form_data.c_str(),
378 "application", app_string,
379 nullptr);
382 void GKRMethod::AddLoginSearch(const PasswordForm& form,
383 const char* app_string) {
384 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
385 lookup_form_.reset(nullptr);
386 // Search GNOME Keyring for matching passwords to update.
387 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
388 AppendString(&attrs, "origin_url", form.origin.spec());
389 AppendString(&attrs, "username_element", UTF16ToUTF8(form.username_element));
390 AppendString(&attrs, "username_value", UTF16ToUTF8(form.username_value));
391 AppendString(&attrs, "password_element", UTF16ToUTF8(form.password_element));
392 AppendString(&attrs, "submit_element", UTF16ToUTF8(form.submit_element));
393 AppendString(&attrs, "signon_realm", form.signon_realm);
394 AppendString(&attrs, "application", app_string);
395 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
396 attrs.get(),
397 OnOperationGetList,
398 /*data=*/this,
399 /*destroy_data=*/nullptr);
402 void GKRMethod::UpdateLoginSearch(const PasswordForm& form,
403 const char* app_string) {
404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
405 lookup_form_.reset(nullptr);
406 // Search GNOME Keyring for matching passwords to update.
407 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
408 AppendString(&attrs, "origin_url", form.origin.spec());
409 AppendString(&attrs, "username_element", UTF16ToUTF8(form.username_element));
410 AppendString(&attrs, "username_value", UTF16ToUTF8(form.username_value));
411 AppendString(&attrs, "password_element", UTF16ToUTF8(form.password_element));
412 AppendString(&attrs, "signon_realm", form.signon_realm);
413 AppendString(&attrs, "application", app_string);
414 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
415 attrs.get(),
416 OnOperationGetList,
417 /*data=*/this,
418 /*destroy_data=*/nullptr);
421 void GKRMethod::RemoveLogin(const PasswordForm& form, const char* app_string) {
422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
423 // We find forms using the same fields as LoginDatabase::RemoveLogin().
424 gnome_keyring_delete_password(
425 &kGnomeSchema,
426 OnOperationDone,
427 this, // data
428 nullptr, // destroy_data
429 "origin_url", form.origin.spec().c_str(),
430 "username_element", UTF16ToUTF8(form.username_element).c_str(),
431 "username_value", UTF16ToUTF8(form.username_value).c_str(),
432 "password_element", UTF16ToUTF8(form.password_element).c_str(),
433 "submit_element", UTF16ToUTF8(form.submit_element).c_str(),
434 "signon_realm", form.signon_realm.c_str(),
435 "application", app_string,
436 nullptr);
439 void GKRMethod::GetLogins(const PasswordForm& form, const char* app_string) {
440 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
441 lookup_form_.reset(new PasswordForm(form));
442 // Search GNOME Keyring for matching passwords.
443 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
444 if (!password_manager::ShouldPSLDomainMatchingApply(
445 password_manager::GetRegistryControlledDomain(
446 GURL(form.signon_realm)))) {
447 AppendString(&attrs, "signon_realm", form.signon_realm);
449 AppendString(&attrs, "application", app_string);
450 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
451 attrs.get(),
452 OnOperationGetList,
453 /*data=*/this,
454 /*destroy_data=*/nullptr);
457 void GKRMethod::GetLoginsList(uint32_t blacklisted_by_user,
458 const char* app_string) {
459 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
460 lookup_form_.reset(nullptr);
461 // Search GNOME Keyring for matching passwords.
462 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
463 AppendUint32(&attrs, "blacklisted_by_user", blacklisted_by_user);
464 AppendString(&attrs, "application", app_string);
465 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
466 attrs.get(),
467 OnOperationGetList,
468 /*data=*/this,
469 /*destroy_data=*/nullptr);
472 void GKRMethod::GetAllLogins(const char* app_string) {
473 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
474 lookup_form_.reset(nullptr);
475 // We need to search for something, otherwise we get no results - so
476 // we search for the fixed application string.
477 ScopedAttributeList attrs(gnome_keyring_attribute_list_new());
478 AppendString(&attrs, "application", app_string);
479 gnome_keyring_find_items(GNOME_KEYRING_ITEM_GENERIC_SECRET,
480 attrs.get(),
481 OnOperationGetList,
482 /*data=*/this,
483 /*destroy_data=*/nullptr);
486 GnomeKeyringResult GKRMethod::WaitResult() {
487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
488 event_.Wait();
489 return result_;
492 GnomeKeyringResult GKRMethod::WaitResult(ScopedVector<PasswordForm>* forms) {
493 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
494 event_.Wait();
495 *forms = forms_.Pass();
496 return result_;
499 // static
500 void GKRMethod::AppendString(GKRMethod::ScopedAttributeList* list,
501 const char* name,
502 const char* value) {
503 gnome_keyring_attribute_list_append_string(list->get(), name, value);
506 // static
507 void GKRMethod::AppendString(GKRMethod::ScopedAttributeList* list,
508 const char* name,
509 const std::string& value) {
510 AppendString(list, name, value.c_str());
513 // static
514 void GKRMethod::AppendUint32(GKRMethod::ScopedAttributeList* list,
515 const char* name,
516 guint32 value) {
517 gnome_keyring_attribute_list_append_uint32(list->get(), name, value);
520 // static
521 void GKRMethod::OnOperationDone(GnomeKeyringResult result, gpointer data) {
522 GKRMethod* method = static_cast<GKRMethod*>(data);
523 method->result_ = result;
524 method->event_.Signal();
527 // static
528 void GKRMethod::OnOperationGetList(GnomeKeyringResult result, GList* list,
529 gpointer data) {
530 GKRMethod* method = static_cast<GKRMethod*>(data);
531 method->result_ = result;
532 // |list| will be freed after this callback returns, so convert it now.
533 if (result == GNOME_KEYRING_RESULT_OK)
534 method->forms_ = ConvertFormList(list, method->lookup_form_.get());
535 else
536 method->forms_.clear();
537 method->lookup_form_.reset();
538 method->event_.Signal();
541 // Generates a profile-specific app string based on profile_id.
542 std::string GetProfileSpecificAppString(LocalProfileId profile_id) {
543 // Originally, the application string was always just "chrome" and used only
544 // so that we had *something* to search for since GNOME Keyring won't search
545 // for nothing. Now we use it to distinguish passwords for different profiles.
546 return base::StringPrintf("%s-%d", kGnomeKeyringAppString, profile_id);
549 } // namespace
551 NativeBackendGnome::NativeBackendGnome(LocalProfileId id)
552 : profile_id_(id), app_string_(GetProfileSpecificAppString(id)) {
555 NativeBackendGnome::~NativeBackendGnome() {
558 bool NativeBackendGnome::Init() {
559 return LoadGnomeKeyring() && gnome_keyring_is_available();
562 bool NativeBackendGnome::RawAddLogin(const PasswordForm& form) {
563 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
564 GKRMethod method;
565 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
566 base::Bind(&GKRMethod::AddLogin,
567 base::Unretained(&method),
568 form, app_string_.c_str()));
569 GnomeKeyringResult result = method.WaitResult();
570 if (result != GNOME_KEYRING_RESULT_OK) {
571 LOG(ERROR) << "Keyring save failed: "
572 << gnome_keyring_result_to_message(result);
573 return false;
575 return true;
578 password_manager::PasswordStoreChangeList NativeBackendGnome::AddLogin(
579 const PasswordForm& form) {
580 // Based on LoginDatabase::AddLogin(), we search for an existing match based
581 // on origin_url, username_element, username_value, password_element, submit
582 // element, and signon_realm first, remove that, and then add the new entry.
583 // We'd add the new one first, and then delete the original, but then the
584 // delete might actually delete the newly-added entry!
585 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
586 GKRMethod method;
587 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
588 base::Bind(&GKRMethod::AddLoginSearch,
589 base::Unretained(&method),
590 form, app_string_.c_str()));
591 ScopedVector<PasswordForm> forms;
592 GnomeKeyringResult result = method.WaitResult(&forms);
593 if (result != GNOME_KEYRING_RESULT_OK &&
594 result != GNOME_KEYRING_RESULT_NO_MATCH) {
595 LOG(ERROR) << "Keyring find failed: "
596 << gnome_keyring_result_to_message(result);
597 return password_manager::PasswordStoreChangeList();
599 password_manager::PasswordStoreChangeList changes;
600 if (forms.size() > 0) {
601 if (forms.size() > 1) {
602 LOG(WARNING) << "Adding login when there are " << forms.size()
603 << " matching logins already! Will replace only the first.";
606 if (RemoveLogin(*forms[0])) {
607 changes.push_back(password_manager::PasswordStoreChange(
608 password_manager::PasswordStoreChange::REMOVE, *forms[0]));
611 if (RawAddLogin(form)) {
612 changes.push_back(password_manager::PasswordStoreChange(
613 password_manager::PasswordStoreChange::ADD, form));
615 return changes;
618 bool NativeBackendGnome::UpdateLogin(
619 const PasswordForm& form,
620 password_manager::PasswordStoreChangeList* changes) {
621 // Based on LoginDatabase::UpdateLogin(), we search for forms to update by
622 // origin_url, username_element, username_value, password_element, and
623 // signon_realm. We then compare the result to the updated form. If they
624 // differ in any of the mutable fields, then we remove the original, and
625 // then add the new entry. We'd add the new one first, and then delete the
626 // original, but then the delete might actually delete the newly-added entry!
627 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
628 DCHECK(changes);
629 changes->clear();
630 GKRMethod method;
631 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
632 base::Bind(&GKRMethod::UpdateLoginSearch,
633 base::Unretained(&method),
634 form, app_string_.c_str()));
635 ScopedVector<PasswordForm> forms;
636 GnomeKeyringResult result = method.WaitResult(&forms);
637 if (result != GNOME_KEYRING_RESULT_OK) {
638 LOG(ERROR) << "Keyring find failed: "
639 << gnome_keyring_result_to_message(result);
640 return false;
643 bool removed = false;
644 for (size_t i = 0; i < forms.size(); ++i) {
645 if (*forms[i] != form) {
646 RemoveLogin(*forms[i]);
647 removed = true;
650 if (!removed)
651 return true;
653 if (RawAddLogin(form)) {
654 password_manager::PasswordStoreChange change(
655 password_manager::PasswordStoreChange::UPDATE, form);
656 changes->push_back(change);
657 return true;
659 return false;
662 bool NativeBackendGnome::RemoveLogin(const PasswordForm& form) {
663 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
664 GKRMethod method;
665 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
666 base::Bind(&GKRMethod::RemoveLogin,
667 base::Unretained(&method),
668 form, app_string_.c_str()));
669 GnomeKeyringResult result = method.WaitResult();
670 if (result != GNOME_KEYRING_RESULT_OK) {
671 // Warning, not error, because this can sometimes happen due to the user
672 // racing with the daemon to delete the password a second time.
673 LOG(WARNING) << "Keyring delete failed: "
674 << gnome_keyring_result_to_message(result);
675 return false;
677 return true;
680 bool NativeBackendGnome::RemoveLoginsCreatedBetween(
681 base::Time delete_begin,
682 base::Time delete_end,
683 password_manager::PasswordStoreChangeList* changes) {
684 return RemoveLoginsBetween(
685 delete_begin, delete_end, CREATION_TIMESTAMP, changes);
688 bool NativeBackendGnome::RemoveLoginsSyncedBetween(
689 base::Time delete_begin,
690 base::Time delete_end,
691 password_manager::PasswordStoreChangeList* changes) {
692 return RemoveLoginsBetween(delete_begin, delete_end, SYNC_TIMESTAMP, changes);
695 bool NativeBackendGnome::GetLogins(const PasswordForm& form,
696 ScopedVector<PasswordForm>* forms) {
697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
698 GKRMethod method;
699 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
700 base::Bind(&GKRMethod::GetLogins,
701 base::Unretained(&method),
702 form, app_string_.c_str()));
703 GnomeKeyringResult result = method.WaitResult(forms);
704 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
705 return true;
706 if (result != GNOME_KEYRING_RESULT_OK) {
707 LOG(ERROR) << "Keyring find failed: "
708 << gnome_keyring_result_to_message(result);
709 return false;
711 return true;
714 bool NativeBackendGnome::GetAutofillableLogins(
715 ScopedVector<PasswordForm>* forms) {
716 return GetLoginsList(true, forms);
719 bool NativeBackendGnome::GetBlacklistLogins(ScopedVector<PasswordForm>* forms) {
720 return GetLoginsList(false, forms);
723 bool NativeBackendGnome::GetLoginsList(bool autofillable,
724 ScopedVector<PasswordForm>* forms) {
725 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
727 uint32_t blacklisted_by_user = !autofillable;
729 GKRMethod method;
730 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
731 base::Bind(&GKRMethod::GetLoginsList,
732 base::Unretained(&method),
733 blacklisted_by_user, app_string_.c_str()));
734 GnomeKeyringResult result = method.WaitResult(forms);
735 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
736 return true;
737 if (result != GNOME_KEYRING_RESULT_OK) {
738 LOG(ERROR) << "Keyring find failed: "
739 << gnome_keyring_result_to_message(result);
740 return false;
742 return true;
745 bool NativeBackendGnome::GetAllLogins(ScopedVector<PasswordForm>* forms) {
746 GKRMethod method;
747 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
748 base::Bind(&GKRMethod::GetAllLogins,
749 base::Unretained(&method),
750 app_string_.c_str()));
751 GnomeKeyringResult result = method.WaitResult(forms);
752 if (result == GNOME_KEYRING_RESULT_NO_MATCH)
753 return true;
754 if (result != GNOME_KEYRING_RESULT_OK) {
755 LOG(ERROR) << "Keyring find failed: "
756 << gnome_keyring_result_to_message(result);
757 return false;
759 return true;
762 bool NativeBackendGnome::GetLoginsBetween(base::Time get_begin,
763 base::Time get_end,
764 TimestampToCompare date_to_compare,
765 ScopedVector<PasswordForm>* forms) {
766 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
767 forms->clear();
768 // We could walk the list and add items as we find them, but it is much
769 // easier to build the list and then filter the results.
770 ScopedVector<PasswordForm> all_forms;
771 if (!GetAllLogins(&all_forms))
772 return false;
774 base::Time PasswordForm::*date_member = date_to_compare == CREATION_TIMESTAMP
775 ? &PasswordForm::date_created
776 : &PasswordForm::date_synced;
777 for (auto& saved_form : all_forms) {
778 if (get_begin <= saved_form->*date_member &&
779 (get_end.is_null() || saved_form->*date_member < get_end)) {
780 forms->push_back(saved_form);
781 saved_form = nullptr;
785 return true;
788 bool NativeBackendGnome::RemoveLoginsBetween(
789 base::Time get_begin,
790 base::Time get_end,
791 TimestampToCompare date_to_compare,
792 password_manager::PasswordStoreChangeList* changes) {
793 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
794 DCHECK(changes);
795 changes->clear();
796 // We could walk the list and delete items as we find them, but it is much
797 // easier to build the list and use RemoveLogin() to delete them.
798 ScopedVector<PasswordForm> forms;
799 if (!GetLoginsBetween(get_begin, get_end, date_to_compare, &forms))
800 return false;
802 bool ok = true;
803 for (size_t i = 0; i < forms.size(); ++i) {
804 if (RemoveLogin(*forms[i])) {
805 changes->push_back(password_manager::PasswordStoreChange(
806 password_manager::PasswordStoreChange::REMOVE, *forms[i]));
807 } else {
808 ok = false;
811 return ok;