OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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_libsecret.h" | 5 #include "chrome/browser/password_manager/native_backend_libsecret.h" |
6 | 6 |
7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 name_values_.push_back(value); | 229 name_values_.push_back(value); |
230 gpointer value_str = | 230 gpointer value_str = |
231 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); | 231 static_cast<gpointer>(const_cast<char*>(name_values_.back().c_str())); |
232 g_hash_table_insert(attrs_, name_str, value_str); | 232 g_hash_table_insert(attrs_, name_str, value_str); |
233 } | 233 } |
234 | 234 |
235 void LibsecretAttributesBuilder::Append(const std::string& name, int64 value) { | 235 void LibsecretAttributesBuilder::Append(const std::string& name, int64 value) { |
236 Append(name, base::Int64ToString(value)); | 236 Append(name, base::Int64ToString(value)); |
237 } | 237 } |
238 | 238 |
| 239 // Generates a profile-specific app string based on profile_id_. |
| 240 std::string GetProfileSpecificAppString(LocalProfileId id) { |
| 241 // Originally, the application string was always just "chrome" and used only |
| 242 // so that we had *something* to search for since GNOME Keyring won't search |
| 243 // for nothing. Now we use it to distinguish passwords for different profiles. |
| 244 return base::StringPrintf("%s-%d", kLibsecretAppString, id); |
| 245 } |
| 246 |
239 } // namespace | 247 } // namespace |
240 | 248 |
241 bool LibsecretLoader::LibsecretIsAvailable() { | 249 bool LibsecretLoader::LibsecretIsAvailable() { |
242 if (!libsecret_loaded) | 250 if (!libsecret_loaded) |
243 return false; | 251 return false; |
244 // A dummy query is made to check for availability, because libsecret doesn't | 252 // A dummy query is made to check for availability, because libsecret doesn't |
245 // have a dedicated availability function. For performance reasons, the query | 253 // have a dedicated availability function. For performance reasons, the query |
246 // is meant to return an empty result. | 254 // is meant to return an empty result. |
247 LibsecretAttributesBuilder attrs; | 255 LibsecretAttributesBuilder attrs; |
248 attrs.Append("application", "chrome-string_to_get_empty_result"); | 256 attrs.Append("application", "chrome-string_to_get_empty_result"); |
(...skipping 24 matching lines...) Expand all Loading... |
273 return LoadLibsecret() && LibsecretIsAvailable(); | 281 return LoadLibsecret() && LibsecretIsAvailable(); |
274 } | 282 } |
275 | 283 |
276 password_manager::PasswordStoreChangeList NativeBackendLibsecret::AddLogin( | 284 password_manager::PasswordStoreChangeList NativeBackendLibsecret::AddLogin( |
277 const PasswordForm& form) { | 285 const PasswordForm& form) { |
278 // Based on LoginDatabase::AddLogin(), we search for an existing match based | 286 // Based on LoginDatabase::AddLogin(), we search for an existing match based |
279 // on origin_url, username_element, username_value, password_element, submit | 287 // on origin_url, username_element, username_value, password_element, submit |
280 // element, and signon_realm first, remove that, and then add the new entry. | 288 // element, and signon_realm first, remove that, and then add the new entry. |
281 // We'd add the new one first, and then delete the original, but then the | 289 // We'd add the new one first, and then delete the original, but then the |
282 // delete might actually delete the newly-added entry! | 290 // delete might actually delete the newly-added entry! |
283 ScopedVector<autofill::PasswordForm> forms; | 291 ScopedVector<autofill::PasswordForm> forms = |
284 AddUpdateLoginSearch(form, SEARCH_USE_SUBMIT, &forms); | 292 AddUpdateLoginSearch(form, SEARCH_USE_SUBMIT); |
285 password_manager::PasswordStoreChangeList changes; | 293 password_manager::PasswordStoreChangeList changes; |
286 if (forms.size() > 0) { | 294 if (forms.size() > 0) { |
287 if (forms.size() > 1) { | 295 if (forms.size() > 1) { |
288 VLOG(1) << "Adding login when there are " << forms.size() | 296 VLOG(1) << "Adding login when there are " << forms.size() |
289 << " matching logins already! Will replace only the first."; | 297 << " matching logins already! Will replace only the first."; |
290 } | 298 } |
291 | 299 |
292 if (RemoveLogin(*forms[0])) { | 300 if (RemoveLogin(*forms[0])) { |
293 changes.push_back(password_manager::PasswordStoreChange( | 301 changes.push_back(password_manager::PasswordStoreChange( |
294 password_manager::PasswordStoreChange::REMOVE, *forms[0])); | 302 password_manager::PasswordStoreChange::REMOVE, *forms[0])); |
(...skipping 11 matching lines...) Expand all Loading... |
306 password_manager::PasswordStoreChangeList* changes) { | 314 password_manager::PasswordStoreChangeList* changes) { |
307 // Based on LoginDatabase::UpdateLogin(), we search for forms to update by | 315 // Based on LoginDatabase::UpdateLogin(), we search for forms to update by |
308 // origin_url, username_element, username_value, password_element, and | 316 // origin_url, username_element, username_value, password_element, and |
309 // signon_realm. We then compare the result to the updated form. If they | 317 // signon_realm. We then compare the result to the updated form. If they |
310 // differ in any of the mutable fields, then we remove the original, and | 318 // differ in any of the mutable fields, then we remove the original, and |
311 // then add the new entry. We'd add the new one first, and then delete the | 319 // then add the new entry. We'd add the new one first, and then delete the |
312 // original, but then the delete might actually delete the newly-added entry! | 320 // original, but then the delete might actually delete the newly-added entry! |
313 DCHECK(changes); | 321 DCHECK(changes); |
314 changes->clear(); | 322 changes->clear(); |
315 | 323 |
316 ScopedVector<autofill::PasswordForm> forms; | 324 ScopedVector<autofill::PasswordForm> forms = |
317 AddUpdateLoginSearch(form, SEARCH_IGNORE_SUBMIT, &forms); | 325 AddUpdateLoginSearch(form, SEARCH_IGNORE_SUBMIT); |
318 | 326 |
319 bool removed = false; | 327 bool removed = false; |
320 for (size_t i = 0; i < forms.size(); ++i) { | 328 for (size_t i = 0; i < forms.size(); ++i) { |
321 if (*forms[i] != form) { | 329 if (*forms[i] != form) { |
322 RemoveLogin(*forms[i]); | 330 RemoveLogin(*forms[i]); |
323 removed = true; | 331 removed = true; |
324 } | 332 } |
325 } | 333 } |
326 if (!removed) | 334 if (!removed) |
327 return true; | 335 return true; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 password_manager::PasswordStoreChangeList* changes) { | 376 password_manager::PasswordStoreChangeList* changes) { |
369 return RemoveLoginsBetween(delete_begin, delete_end, SYNC_TIMESTAMP, changes); | 377 return RemoveLoginsBetween(delete_begin, delete_end, SYNC_TIMESTAMP, changes); |
370 } | 378 } |
371 | 379 |
372 bool NativeBackendLibsecret::GetLogins( | 380 bool NativeBackendLibsecret::GetLogins( |
373 const PasswordForm& form, | 381 const PasswordForm& form, |
374 ScopedVector<autofill::PasswordForm>* forms) { | 382 ScopedVector<autofill::PasswordForm>* forms) { |
375 return GetLoginsList(&form, ALL_LOGINS, forms); | 383 return GetLoginsList(&form, ALL_LOGINS, forms); |
376 } | 384 } |
377 | 385 |
378 void NativeBackendLibsecret::AddUpdateLoginSearch( | 386 ScopedVector<autofill::PasswordForm> |
| 387 NativeBackendLibsecret::AddUpdateLoginSearch( |
379 const autofill::PasswordForm& lookup_form, | 388 const autofill::PasswordForm& lookup_form, |
380 AddUpdateLoginSearchOptions options, | 389 AddUpdateLoginSearchOptions options) { |
381 ScopedVector<autofill::PasswordForm>* forms) { | |
382 LibsecretAttributesBuilder attrs; | 390 LibsecretAttributesBuilder attrs; |
383 attrs.Append("origin_url", lookup_form.origin.spec()); | 391 attrs.Append("origin_url", lookup_form.origin.spec()); |
384 attrs.Append("username_element", UTF16ToUTF8(lookup_form.username_element)); | 392 attrs.Append("username_element", UTF16ToUTF8(lookup_form.username_element)); |
385 attrs.Append("username_value", UTF16ToUTF8(lookup_form.username_value)); | 393 attrs.Append("username_value", UTF16ToUTF8(lookup_form.username_value)); |
386 attrs.Append("password_element", UTF16ToUTF8(lookup_form.password_element)); | 394 attrs.Append("password_element", UTF16ToUTF8(lookup_form.password_element)); |
387 if (options == SEARCH_USE_SUBMIT) | 395 if (options == SEARCH_USE_SUBMIT) |
388 attrs.Append("submit_element", UTF16ToUTF8(lookup_form.submit_element)); | 396 attrs.Append("submit_element", UTF16ToUTF8(lookup_form.submit_element)); |
389 attrs.Append("signon_realm", lookup_form.signon_realm); | 397 attrs.Append("signon_realm", lookup_form.signon_realm); |
390 attrs.Append("application", app_string_); | 398 attrs.Append("application", app_string_); |
391 | 399 |
392 GError* error = nullptr; | 400 GError* error = nullptr; |
393 GList* found = secret_service_search_sync(nullptr, // default secret service | 401 GList* found = secret_service_search_sync(nullptr, // default secret service |
394 &kLibsecretSchema, attrs.Get(), | 402 &kLibsecretSchema, attrs.Get(), |
395 SECRET_SEARCH_ALL, | 403 SECRET_SEARCH_ALL, |
396 nullptr, // no cancellable ojbect | 404 nullptr, // no cancellable ojbect |
397 &error); | 405 &error); |
398 if (error) { | 406 if (error) { |
399 VLOG(1) << "Unable to get logins " << error->message; | 407 VLOG(1) << "Unable to get logins " << error->message; |
400 g_error_free(error); | 408 g_error_free(error); |
401 if (found) | 409 if (found) |
402 g_list_free(found); | 410 g_list_free(found); |
403 return; | 411 return ScopedVector<autofill::PasswordForm>(); |
404 } | 412 } |
405 | 413 |
406 ConvertFormList(found, &lookup_form, forms); | 414 return ConvertFormList(found, &lookup_form); |
407 } | 415 } |
408 | 416 |
409 bool NativeBackendLibsecret::RawAddLogin(const PasswordForm& form) { | 417 bool NativeBackendLibsecret::RawAddLogin(const PasswordForm& form) { |
410 int64 date_created = form.date_created.ToInternalValue(); | 418 int64 date_created = form.date_created.ToInternalValue(); |
411 // If we are asked to save a password with 0 date, use the current time. | 419 // If we are asked to save a password with 0 date, use the current time. |
412 // We don't want to actually save passwords as though on January 1, 1601. | 420 // We don't want to actually save passwords as though on January 1, 1601. |
413 if (!date_created) | 421 if (!date_created) |
414 date_created = base::Time::Now().ToInternalValue(); | 422 date_created = base::Time::Now().ToInternalValue(); |
415 int64 date_synced = form.date_synced.ToInternalValue(); | 423 int64 date_synced = form.date_synced.ToInternalValue(); |
416 std::string form_data; | 424 std::string form_data; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 | 469 |
462 bool NativeBackendLibsecret::GetBlacklistLogins( | 470 bool NativeBackendLibsecret::GetBlacklistLogins( |
463 ScopedVector<autofill::PasswordForm>* forms) { | 471 ScopedVector<autofill::PasswordForm>* forms) { |
464 return GetLoginsList(nullptr, BLACKLISTED_LOGINS, forms); | 472 return GetLoginsList(nullptr, BLACKLISTED_LOGINS, forms); |
465 } | 473 } |
466 | 474 |
467 bool NativeBackendLibsecret::GetLoginsList( | 475 bool NativeBackendLibsecret::GetLoginsList( |
468 const PasswordForm* lookup_form, | 476 const PasswordForm* lookup_form, |
469 GetLoginsListOptions options, | 477 GetLoginsListOptions options, |
470 ScopedVector<autofill::PasswordForm>* forms) { | 478 ScopedVector<autofill::PasswordForm>* forms) { |
| 479 forms->clear(); |
471 LibsecretAttributesBuilder attrs; | 480 LibsecretAttributesBuilder attrs; |
472 attrs.Append("application", app_string_); | 481 attrs.Append("application", app_string_); |
473 if (options != ALL_LOGINS) | 482 if (options != ALL_LOGINS) |
474 attrs.Append("blacklisted_by_user", options == BLACKLISTED_LOGINS); | 483 attrs.Append("blacklisted_by_user", options == BLACKLISTED_LOGINS); |
475 if (lookup_form && | 484 if (lookup_form && |
476 !password_manager::ShouldPSLDomainMatchingApply( | 485 !password_manager::ShouldPSLDomainMatchingApply( |
477 password_manager::GetRegistryControlledDomain( | 486 password_manager::GetRegistryControlledDomain( |
478 GURL(lookup_form->signon_realm)))) | 487 GURL(lookup_form->signon_realm)))) |
479 attrs.Append("signon_realm", lookup_form->signon_realm); | 488 attrs.Append("signon_realm", lookup_form->signon_realm); |
480 | 489 |
481 GError* error = nullptr; | 490 GError* error = nullptr; |
482 GList* found = secret_service_search_sync(nullptr, // default secret service | 491 GList* found = secret_service_search_sync(nullptr, // default secret service |
483 &kLibsecretSchema, attrs.Get(), | 492 &kLibsecretSchema, attrs.Get(), |
484 SECRET_SEARCH_ALL, | 493 SECRET_SEARCH_ALL, |
485 nullptr, // no cancellable ojbect | 494 nullptr, // no cancellable ojbect |
486 &error); | 495 &error); |
487 if (error) { | 496 if (error) { |
488 VLOG(1) << "Unable to get logins " << error->message; | 497 VLOG(1) << "Unable to get logins " << error->message; |
489 g_error_free(error); | 498 g_error_free(error); |
490 if (found) | 499 if (found) |
491 g_list_free(found); | 500 g_list_free(found); |
492 return false; | 501 return false; |
493 } | 502 } |
494 | 503 |
495 return ConvertFormList(found, lookup_form, forms); | 504 *forms = ConvertFormList(found, lookup_form); |
496 } | 505 return true; |
497 | |
498 bool NativeBackendLibsecret::GetAllLogins( | |
499 ScopedVector<autofill::PasswordForm>* forms) { | |
500 return GetLoginsList(nullptr, ALL_LOGINS, forms); | |
501 } | 506 } |
502 | 507 |
503 bool NativeBackendLibsecret::GetLoginsBetween( | 508 bool NativeBackendLibsecret::GetLoginsBetween( |
504 base::Time get_begin, | 509 base::Time get_begin, |
505 base::Time get_end, | 510 base::Time get_end, |
506 TimestampToCompare date_to_compare, | 511 TimestampToCompare date_to_compare, |
507 ScopedVector<autofill::PasswordForm>* forms) { | 512 ScopedVector<autofill::PasswordForm>* forms) { |
| 513 forms->clear(); |
508 ScopedVector<autofill::PasswordForm> all_forms; | 514 ScopedVector<autofill::PasswordForm> all_forms; |
509 if (!GetAllLogins(&all_forms)) | 515 if (!GetLoginsList(nullptr, ALL_LOGINS, &all_forms)) |
510 return false; | 516 return false; |
511 | 517 |
512 base::Time autofill::PasswordForm::*date_member = | 518 base::Time autofill::PasswordForm::*date_member = |
513 date_to_compare == CREATION_TIMESTAMP | 519 date_to_compare == CREATION_TIMESTAMP |
514 ? &autofill::PasswordForm::date_created | 520 ? &autofill::PasswordForm::date_created |
515 : &autofill::PasswordForm::date_synced; | 521 : &autofill::PasswordForm::date_synced; |
516 for (auto& saved_form : all_forms) { | 522 for (auto& saved_form : all_forms) { |
517 if (get_begin <= saved_form->*date_member && | 523 if (get_begin <= saved_form->*date_member && |
518 (get_end.is_null() || saved_form->*date_member < get_end)) { | 524 (get_end.is_null() || saved_form->*date_member < get_end)) { |
519 forms->push_back(saved_form); | 525 forms->push_back(saved_form); |
(...skipping 20 matching lines...) Expand all Loading... |
540 if (RemoveLogin(*forms[i])) { | 546 if (RemoveLogin(*forms[i])) { |
541 changes->push_back(password_manager::PasswordStoreChange( | 547 changes->push_back(password_manager::PasswordStoreChange( |
542 password_manager::PasswordStoreChange::REMOVE, *forms[i])); | 548 password_manager::PasswordStoreChange::REMOVE, *forms[i])); |
543 } else { | 549 } else { |
544 ok = false; | 550 ok = false; |
545 } | 551 } |
546 } | 552 } |
547 return ok; | 553 return ok; |
548 } | 554 } |
549 | 555 |
550 bool NativeBackendLibsecret::ConvertFormList( | 556 ScopedVector<autofill::PasswordForm> NativeBackendLibsecret::ConvertFormList( |
551 GList* found, | 557 GList* found, |
552 const PasswordForm* lookup_form, | 558 const PasswordForm* lookup_form) { |
553 ScopedVector<autofill::PasswordForm>* forms) { | 559 ScopedVector<autofill::PasswordForm> forms; |
554 password_manager::PSLDomainMatchMetric psl_domain_match_metric = | 560 password_manager::PSLDomainMatchMetric psl_domain_match_metric = |
555 password_manager::PSL_DOMAIN_MATCH_NONE; | 561 password_manager::PSL_DOMAIN_MATCH_NONE; |
556 GError* error = nullptr; | 562 GError* error = nullptr; |
557 for (GList* element = g_list_first(found); element != nullptr; | 563 for (GList* element = g_list_first(found); element != nullptr; |
558 element = g_list_next(element)) { | 564 element = g_list_next(element)) { |
559 SecretItem* secretItem = static_cast<SecretItem*>(element->data); | 565 SecretItem* secretItem = static_cast<SecretItem*>(element->data); |
560 LibsecretLoader::secret_item_load_secret_sync(secretItem, nullptr, &error); | 566 LibsecretLoader::secret_item_load_secret_sync(secretItem, nullptr, &error); |
561 if (error) { | 567 if (error) { |
562 VLOG(1) << "Unable to load secret item" << error->message; | 568 VLOG(1) << "Unable to load secret item" << error->message; |
563 g_error_free(error); | 569 g_error_free(error); |
(...skipping 17 matching lines...) Expand all Loading... |
581 form->signon_realm = lookup_form->signon_realm; | 587 form->signon_realm = lookup_form->signon_realm; |
582 form->origin = lookup_form->origin; | 588 form->origin = lookup_form->origin; |
583 } | 589 } |
584 SecretValue* secretValue = secret_item_get_secret(secretItem); | 590 SecretValue* secretValue = secret_item_get_secret(secretItem); |
585 if (secretValue) { | 591 if (secretValue) { |
586 form->password_value = UTF8ToUTF16(secret_value_get_text(secretValue)); | 592 form->password_value = UTF8ToUTF16(secret_value_get_text(secretValue)); |
587 secret_value_unref(secretValue); | 593 secret_value_unref(secretValue); |
588 } else { | 594 } else { |
589 VLOG(1) << "Unable to access password from list element!"; | 595 VLOG(1) << "Unable to access password from list element!"; |
590 } | 596 } |
591 forms->push_back(form.release()); | 597 forms.push_back(form.release()); |
592 } else { | 598 } else { |
593 VLOG(1) << "Could not initialize PasswordForm from attributes!"; | 599 VLOG(1) << "Could not initialize PasswordForm from attributes!"; |
594 } | 600 } |
595 } | 601 } |
596 | 602 |
597 if (lookup_form) { | 603 if (lookup_form) { |
598 const GURL signon_realm(lookup_form->signon_realm); | 604 const GURL signon_realm(lookup_form->signon_realm); |
599 std::string registered_domain = | 605 std::string registered_domain = |
600 password_manager::GetRegistryControlledDomain(signon_realm); | 606 password_manager::GetRegistryControlledDomain(signon_realm); |
601 UMA_HISTOGRAM_ENUMERATION( | 607 UMA_HISTOGRAM_ENUMERATION( |
602 "PasswordManager.PslDomainMatchTriggering", | 608 "PasswordManager.PslDomainMatchTriggering", |
603 password_manager::ShouldPSLDomainMatchingApply(registered_domain) | 609 password_manager::ShouldPSLDomainMatchingApply(registered_domain) |
604 ? psl_domain_match_metric | 610 ? psl_domain_match_metric |
605 : password_manager::PSL_DOMAIN_MATCH_NOT_USED, | 611 : password_manager::PSL_DOMAIN_MATCH_NOT_USED, |
606 password_manager::PSL_DOMAIN_MATCH_COUNT); | 612 password_manager::PSL_DOMAIN_MATCH_COUNT); |
607 } | 613 } |
608 g_list_free(found); | 614 g_list_free(found); |
609 return true; | 615 return forms.Pass(); |
610 } | 616 } |
611 | |
612 std::string NativeBackendLibsecret::GetProfileSpecificAppString( | |
613 LocalProfileId id) { | |
614 // Originally, the application string was always just "chrome" and used only | |
615 // so that we had *something* to search for since GNOME Keyring won't search | |
616 // for nothing. Now we use it to distinguish passwords for different profiles. | |
617 return base::StringPrintf("%s-%d", kLibsecretAppString, id); | |
618 } | |
OLD | NEW |