| 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_kwallet_x.h" | 5 #include "chrome/browser/password_manager/native_backend_kwallet_x.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "chrome/grit/chromium_strings.h" | 16 #include "chrome/grit/chromium_strings.h" |
| 17 #include "components/autofill/core/common/password_form.h" | 17 #include "components/autofill/core/common/password_form.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "dbus/bus.h" | 19 #include "dbus/bus.h" |
| 20 #include "dbus/message.h" | 20 #include "dbus/message.h" |
| 21 #include "dbus/object_path.h" | 21 #include "dbus/object_path.h" |
| 22 #include "dbus/object_proxy.h" | 22 #include "dbus/object_proxy.h" |
| 23 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 24 | 24 |
| 25 using autofill::PasswordForm; | 25 using autofill::PasswordForm; |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // In case the fields in the pickle ever change, version them so we can try to |
| 31 // read old pickles. (Note: do not eat old pickles past the expiration date.) |
| 32 const int kPickleVersion = 6; |
| 33 |
| 30 // We could localize this string, but then changing your locale would cause | 34 // We could localize this string, but then changing your locale would cause |
| 31 // you to lose access to all your stored passwords. Maybe best not to do that. | 35 // you to lose access to all your stored passwords. Maybe best not to do that. |
| 32 // Name of the folder to store passwords in. | 36 // Name of the folder to store passwords in. |
| 33 const char kKWalletFolder[] = "Chrome Form Data"; | 37 const char kKWalletFolder[] = "Chrome Form Data"; |
| 34 | 38 |
| 35 // DBus service, path, and interface names for klauncher and kwalletd. | 39 // DBus service, path, and interface names for klauncher and kwalletd. |
| 36 const char kKWalletServiceName[] = "org.kde.kwalletd"; | 40 const char kKWalletServiceName[] = "org.kde.kwalletd"; |
| 37 const char kKWalletPath[] = "/modules/kwalletd"; | 41 const char kKWalletPath[] = "/modules/kwalletd"; |
| 38 const char kKWalletInterface[] = "org.kde.KWallet"; | 42 const char kKWalletInterface[] = "org.kde.KWallet"; |
| 39 const char kKLauncherServiceName[] = "org.kde.klauncher"; | 43 const char kKLauncherServiceName[] = "org.kde.klauncher"; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 LOG(WARNING) << "Failed to deserialize version " << version | 97 LOG(WARNING) << "Failed to deserialize version " << version |
| 94 << " KWallet entry (realm: " << signon_realm | 98 << " KWallet entry (realm: " << signon_realm |
| 95 << ") with native architecture size; will try alternate " | 99 << ") with native architecture size; will try alternate " |
| 96 << "size."; | 100 << "size."; |
| 97 } else { | 101 } else { |
| 98 LOG(ERROR) << "Failed to deserialize version " << version | 102 LOG(ERROR) << "Failed to deserialize version " << version |
| 99 << " KWallet entry (realm: " << signon_realm << ")"; | 103 << " KWallet entry (realm: " << signon_realm << ")"; |
| 100 } | 104 } |
| 101 } | 105 } |
| 102 | 106 |
| 107 // Deserializes a list of credentials from the wallet to |forms| (replacing |
| 108 // the contents of |forms|). |size_32| controls reading the size field within |
| 109 // the pickle as 32 bits. We used to use Pickle::WriteSize() to write the number |
| 110 // of password forms, but that has a different size on 32- and 64-bit systems. |
| 111 // So, now we always write a 64-bit quantity, but we support trying to read it |
| 112 // as either size when reading old pickles that fail to deserialize using the |
| 113 // native size. Returns true on success. |
| 114 bool DeserializeValueSize(const std::string& signon_realm, |
| 115 const PickleIterator& init_iter, |
| 116 int version, |
| 117 bool size_32, |
| 118 bool warn_only, |
| 119 ScopedVector<autofill::PasswordForm>* forms) { |
| 120 PickleIterator iter = init_iter; |
| 121 |
| 122 size_t count = 0; |
| 123 if (size_32) { |
| 124 uint32_t count_32 = 0; |
| 125 if (!iter.ReadUInt32(&count_32)) { |
| 126 LOG(ERROR) << "Failed to deserialize KWallet entry " |
| 127 << "(realm: " << signon_realm << ")"; |
| 128 return false; |
| 129 } |
| 130 count = count_32; |
| 131 } else { |
| 132 if (!iter.ReadSizeT(&count)) { |
| 133 LOG(ERROR) << "Failed to deserialize KWallet entry " |
| 134 << "(realm: " << signon_realm << ")"; |
| 135 return false; |
| 136 } |
| 137 } |
| 138 |
| 139 if (count > 0xFFFF) { |
| 140 // Trying to pin down the cause of http://crbug.com/80728 (or fix it). |
| 141 // This is a very large number of passwords to be saved for a single realm. |
| 142 // It is almost certainly a corrupt pickle and not real data. Ignore it. |
| 143 // This very well might actually be http://crbug.com/107701, so if we're |
| 144 // reading an old pickle, we don't even log this the first time we try to |
| 145 // read it. (That is, when we're reading the native architecture size.) |
| 146 if (!warn_only) { |
| 147 LOG(ERROR) << "Suspiciously large number of entries in KWallet entry " |
| 148 << "(" << count << "; realm: " << signon_realm << ")"; |
| 149 } |
| 150 return false; |
| 151 } |
| 152 |
| 153 // We'll swap |converted_forms| with |*forms| on success, to make sure we |
| 154 // don't return partial results on failure. |
| 155 ScopedVector<autofill::PasswordForm> converted_forms; |
| 156 converted_forms.reserve(count); |
| 157 for (size_t i = 0; i < count; ++i) { |
| 158 scoped_ptr<PasswordForm> form(new PasswordForm()); |
| 159 form->signon_realm.assign(signon_realm); |
| 160 |
| 161 int scheme = 0; |
| 162 int64 date_created = 0; |
| 163 int type = 0; |
| 164 int generation_upload_status = 0; |
| 165 // Note that these will be read back in the order listed due to |
| 166 // short-circuit evaluation. This is important. |
| 167 if (!iter.ReadInt(&scheme) || |
| 168 !ReadGURL(&iter, warn_only, &form->origin) || |
| 169 !ReadGURL(&iter, warn_only, &form->action) || |
| 170 !iter.ReadString16(&form->username_element) || |
| 171 !iter.ReadString16(&form->username_value) || |
| 172 !iter.ReadString16(&form->password_element) || |
| 173 !iter.ReadString16(&form->password_value) || |
| 174 !iter.ReadString16(&form->submit_element) || |
| 175 !iter.ReadBool(&form->ssl_valid) || |
| 176 !iter.ReadBool(&form->preferred) || |
| 177 !iter.ReadBool(&form->blacklisted_by_user) || |
| 178 !iter.ReadInt64(&date_created)) { |
| 179 LogDeserializationWarning(version, signon_realm, warn_only); |
| 180 return false; |
| 181 } |
| 182 form->scheme = static_cast<PasswordForm::Scheme>(scheme); |
| 183 |
| 184 if (version > 1) { |
| 185 if (!iter.ReadInt(&type) || |
| 186 !iter.ReadInt(&form->times_used) || |
| 187 !autofill::DeserializeFormData(&iter, &form->form_data)) { |
| 188 LogDeserializationWarning(version, signon_realm, false); |
| 189 return false; |
| 190 } |
| 191 form->type = static_cast<PasswordForm::Type>(type); |
| 192 } |
| 193 |
| 194 if (version > 2) { |
| 195 int64 date_synced = 0; |
| 196 if (!iter.ReadInt64(&date_synced)) { |
| 197 LogDeserializationWarning(version, signon_realm, false); |
| 198 return false; |
| 199 } |
| 200 form->date_synced = base::Time::FromInternalValue(date_synced); |
| 201 } |
| 202 |
| 203 if (version > 3) { |
| 204 if (!iter.ReadString16(&form->display_name) || |
| 205 !ReadGURL(&iter, warn_only, &form->avatar_url) || |
| 206 !ReadGURL(&iter, warn_only, &form->federation_url) || |
| 207 !iter.ReadBool(&form->skip_zero_click)) { |
| 208 LogDeserializationWarning(version, signon_realm, false); |
| 209 return false; |
| 210 } |
| 211 } |
| 212 |
| 213 if (version > 4) { |
| 214 form->date_created = base::Time::FromInternalValue(date_created); |
| 215 } else { |
| 216 form->date_created = base::Time::FromTimeT(date_created); |
| 217 } |
| 218 |
| 219 if (version > 5) { |
| 220 if (!iter.ReadInt(&generation_upload_status)) { |
| 221 LogDeserializationWarning(version, signon_realm, false); |
| 222 } |
| 223 form->generation_upload_status = |
| 224 static_cast<PasswordForm::GenerationUploadStatus>( |
| 225 generation_upload_status); |
| 226 } |
| 227 |
| 228 converted_forms.push_back(form.release()); |
| 229 } |
| 230 |
| 231 forms->swap(converted_forms); |
| 232 return true; |
| 233 } |
| 234 |
| 235 // Serializes a list of PasswordForms to be stored in the wallet. |
| 236 void SerializeValue(const std::vector<autofill::PasswordForm*>& forms, |
| 237 Pickle* pickle) { |
| 238 pickle->WriteInt(kPickleVersion); |
| 239 pickle->WriteSizeT(forms.size()); |
| 240 for (autofill::PasswordForm* form : forms) { |
| 241 pickle->WriteInt(form->scheme); |
| 242 pickle->WriteString(form->origin.spec()); |
| 243 pickle->WriteString(form->action.spec()); |
| 244 pickle->WriteString16(form->username_element); |
| 245 pickle->WriteString16(form->username_value); |
| 246 pickle->WriteString16(form->password_element); |
| 247 pickle->WriteString16(form->password_value); |
| 248 pickle->WriteString16(form->submit_element); |
| 249 pickle->WriteBool(form->ssl_valid); |
| 250 pickle->WriteBool(form->preferred); |
| 251 pickle->WriteBool(form->blacklisted_by_user); |
| 252 pickle->WriteInt64(form->date_created.ToInternalValue()); |
| 253 pickle->WriteInt(form->type); |
| 254 pickle->WriteInt(form->times_used); |
| 255 autofill::SerializeFormData(form->form_data, pickle); |
| 256 pickle->WriteInt64(form->date_synced.ToInternalValue()); |
| 257 pickle->WriteString16(form->display_name); |
| 258 pickle->WriteString(form->avatar_url.spec()); |
| 259 pickle->WriteString(form->federation_url.spec()); |
| 260 pickle->WriteBool(form->skip_zero_click); |
| 261 } |
| 262 } |
| 263 |
| 264 // Moves the content of |second| to the end of |first|. |
| 265 void AppendSecondToFirst(ScopedVector<autofill::PasswordForm>* first, |
| 266 ScopedVector<autofill::PasswordForm> second) { |
| 267 first->reserve(first->size() + second.size()); |
| 268 first->insert(first->end(), second.begin(), second.end()); |
| 269 second.weak_clear(); |
| 270 } |
| 271 |
| 103 } // namespace | 272 } // namespace |
| 104 | 273 |
| 105 NativeBackendKWallet::NativeBackendKWallet(LocalProfileId id) | 274 NativeBackendKWallet::NativeBackendKWallet(LocalProfileId id) |
| 106 : profile_id_(id), | 275 : profile_id_(id), |
| 107 kwallet_proxy_(nullptr), | 276 kwallet_proxy_(nullptr), |
| 108 app_name_(l10n_util::GetStringUTF8(IDS_PRODUCT_NAME)) { | 277 app_name_(l10n_util::GetStringUTF8(IDS_PRODUCT_NAME)) { |
| 109 folder_name_ = GetProfileSpecificFolderName(); | 278 folder_name_ = GetProfileSpecificFolderName(); |
| 110 } | 279 } |
| 111 | 280 |
| 112 NativeBackendKWallet::~NativeBackendKWallet() { | 281 NativeBackendKWallet::~NativeBackendKWallet() { |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 return INIT_SUCCESS; | 434 return INIT_SUCCESS; |
| 266 } | 435 } |
| 267 | 436 |
| 268 password_manager::PasswordStoreChangeList NativeBackendKWallet::AddLogin( | 437 password_manager::PasswordStoreChangeList NativeBackendKWallet::AddLogin( |
| 269 const PasswordForm& form) { | 438 const PasswordForm& form) { |
| 270 int wallet_handle = WalletHandle(); | 439 int wallet_handle = WalletHandle(); |
| 271 if (wallet_handle == kInvalidKWalletHandle) | 440 if (wallet_handle == kInvalidKWalletHandle) |
| 272 return password_manager::PasswordStoreChangeList(); | 441 return password_manager::PasswordStoreChangeList(); |
| 273 | 442 |
| 274 ScopedVector<autofill::PasswordForm> forms; | 443 ScopedVector<autofill::PasswordForm> forms; |
| 275 GetLoginsList(form.signon_realm, wallet_handle, &forms); | 444 if (!GetLoginsList(form.signon_realm, wallet_handle, &forms)) |
| 445 return password_manager::PasswordStoreChangeList(); |
| 276 | 446 |
| 277 // We search for a login to update, rather than unconditionally appending the | 447 // We search for a login to update, rather than unconditionally appending the |
| 278 // login, because in some cases (especially involving sync) we can be asked to | 448 // login, because in some cases (especially involving sync) we can be asked to |
| 279 // add a login that already exists. In these cases we want to just update. | 449 // add a login that already exists. In these cases we want to just update. |
| 280 bool updated = false; | 450 bool updated = false; |
| 281 password_manager::PasswordStoreChangeList changes; | 451 password_manager::PasswordStoreChangeList changes; |
| 282 for (size_t i = 0; i < forms.size(); ++i) { | 452 for (size_t i = 0; i < forms.size(); ++i) { |
| 283 // Use the more restrictive removal comparison, so that we never have | 453 // Use the more restrictive removal comparison, so that we never have |
| 284 // duplicate logins that would all be removed together by RemoveLogin(). | 454 // duplicate logins that would all be removed together by RemoveLogin(). |
| 285 if (CompareForms(form, *forms[i], false)) { | 455 if (CompareForms(form, *forms[i], false)) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 304 bool NativeBackendKWallet::UpdateLogin( | 474 bool NativeBackendKWallet::UpdateLogin( |
| 305 const PasswordForm& form, | 475 const PasswordForm& form, |
| 306 password_manager::PasswordStoreChangeList* changes) { | 476 password_manager::PasswordStoreChangeList* changes) { |
| 307 DCHECK(changes); | 477 DCHECK(changes); |
| 308 changes->clear(); | 478 changes->clear(); |
| 309 int wallet_handle = WalletHandle(); | 479 int wallet_handle = WalletHandle(); |
| 310 if (wallet_handle == kInvalidKWalletHandle) | 480 if (wallet_handle == kInvalidKWalletHandle) |
| 311 return false; | 481 return false; |
| 312 | 482 |
| 313 ScopedVector<autofill::PasswordForm> forms; | 483 ScopedVector<autofill::PasswordForm> forms; |
| 314 GetLoginsList(form.signon_realm, wallet_handle, &forms); | 484 if (!GetLoginsList(form.signon_realm, wallet_handle, &forms)) |
| 485 return false; |
| 315 | 486 |
| 316 bool updated = false; | 487 bool updated = false; |
| 317 for (size_t i = 0; i < forms.size(); ++i) { | 488 for (size_t i = 0; i < forms.size(); ++i) { |
| 318 if (CompareForms(form, *forms[i], true)) { | 489 if (CompareForms(form, *forms[i], true)) { |
| 319 *forms[i] = form; | 490 *forms[i] = form; |
| 320 updated = true; | 491 updated = true; |
| 321 } | 492 } |
| 322 } | 493 } |
| 323 if (!updated) | 494 if (!updated) |
| 324 return true; | 495 return true; |
| 325 | 496 |
| 326 if (SetLoginsList(forms.get(), form.signon_realm, wallet_handle)) { | 497 if (SetLoginsList(forms.get(), form.signon_realm, wallet_handle)) { |
| 327 changes->push_back(password_manager::PasswordStoreChange( | 498 changes->push_back(password_manager::PasswordStoreChange( |
| 328 password_manager::PasswordStoreChange::UPDATE, form)); | 499 password_manager::PasswordStoreChange::UPDATE, form)); |
| 329 return true; | 500 return true; |
| 330 } | 501 } |
| 331 | 502 |
| 332 return false; | 503 return false; |
| 333 } | 504 } |
| 334 | 505 |
| 335 bool NativeBackendKWallet::RemoveLogin(const PasswordForm& form) { | 506 bool NativeBackendKWallet::RemoveLogin(const PasswordForm& form) { |
| 336 int wallet_handle = WalletHandle(); | 507 int wallet_handle = WalletHandle(); |
| 337 if (wallet_handle == kInvalidKWalletHandle) | 508 if (wallet_handle == kInvalidKWalletHandle) |
| 338 return false; | 509 return false; |
| 339 | 510 |
| 340 ScopedVector<autofill::PasswordForm> all_forms; | 511 ScopedVector<autofill::PasswordForm> all_forms; |
| 341 GetLoginsList(form.signon_realm, wallet_handle, &all_forms); | 512 if (!GetLoginsList(form.signon_realm, wallet_handle, &all_forms)) |
| 513 return false; |
| 342 | 514 |
| 343 ScopedVector<autofill::PasswordForm> kept_forms; | 515 ScopedVector<autofill::PasswordForm> kept_forms; |
| 344 kept_forms.reserve(all_forms.size()); | 516 kept_forms.reserve(all_forms.size()); |
| 345 for (auto& saved_form : all_forms) { | 517 for (auto& saved_form : all_forms) { |
| 346 if (!CompareForms(form, *saved_form, false)) { | 518 if (!CompareForms(form, *saved_form, false)) { |
| 347 kept_forms.push_back(saved_form); | 519 kept_forms.push_back(saved_form); |
| 348 saved_form = nullptr; | 520 saved_form = nullptr; |
| 349 } | 521 } |
| 350 } | 522 } |
| 351 | 523 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 375 if (wallet_handle == kInvalidKWalletHandle) | 547 if (wallet_handle == kInvalidKWalletHandle) |
| 376 return false; | 548 return false; |
| 377 return GetLoginsList(form.signon_realm, wallet_handle, forms); | 549 return GetLoginsList(form.signon_realm, wallet_handle, forms); |
| 378 } | 550 } |
| 379 | 551 |
| 380 bool NativeBackendKWallet::GetAutofillableLogins( | 552 bool NativeBackendKWallet::GetAutofillableLogins( |
| 381 ScopedVector<autofill::PasswordForm>* forms) { | 553 ScopedVector<autofill::PasswordForm>* forms) { |
| 382 int wallet_handle = WalletHandle(); | 554 int wallet_handle = WalletHandle(); |
| 383 if (wallet_handle == kInvalidKWalletHandle) | 555 if (wallet_handle == kInvalidKWalletHandle) |
| 384 return false; | 556 return false; |
| 385 return GetLoginsList(true, wallet_handle, forms); | 557 return GetLoginsList(BlacklistOptions::AUTOFILLABLE, wallet_handle, forms); |
| 386 } | 558 } |
| 387 | 559 |
| 388 bool NativeBackendKWallet::GetBlacklistLogins( | 560 bool NativeBackendKWallet::GetBlacklistLogins( |
| 389 ScopedVector<autofill::PasswordForm>* forms) { | 561 ScopedVector<autofill::PasswordForm>* forms) { |
| 390 int wallet_handle = WalletHandle(); | 562 int wallet_handle = WalletHandle(); |
| 391 if (wallet_handle == kInvalidKWalletHandle) | 563 if (wallet_handle == kInvalidKWalletHandle) |
| 392 return false; | 564 return false; |
| 393 return GetLoginsList(false, wallet_handle, forms); | 565 return GetLoginsList(BlacklistOptions::BLACKLISTED, wallet_handle, forms); |
| 394 } | 566 } |
| 395 | 567 |
| 396 bool NativeBackendKWallet::GetLoginsList( | 568 bool NativeBackendKWallet::GetLoginsList( |
| 397 const std::string& signon_realm, | 569 const std::string& signon_realm, |
| 398 int wallet_handle, | 570 int wallet_handle, |
| 399 ScopedVector<autofill::PasswordForm>* forms) { | 571 ScopedVector<autofill::PasswordForm>* forms) { |
| 400 // Is there an entry in the wallet? | 572 // Is there an entry in the wallet? |
| 401 { | 573 { |
| 402 dbus::MethodCall method_call(kKWalletInterface, "hasEntry"); | 574 dbus::MethodCall method_call(kKWalletInterface, "hasEntry"); |
| 403 dbus::MessageWriter builder(&method_call); | 575 dbus::MessageWriter builder(&method_call); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 452 if (!CheckSerializedValue(bytes, length, signon_realm)) { | 624 if (!CheckSerializedValue(bytes, length, signon_realm)) { |
| 453 // This is weird, but we choose not to call it an error. There is an | 625 // This is weird, but we choose not to call it an error. There is an |
| 454 // invalid entry somehow, but by just ignoring it, we make it easier to | 626 // invalid entry somehow, but by just ignoring it, we make it easier to |
| 455 // repair without having to delete it using kwalletmanager (that is, by | 627 // repair without having to delete it using kwalletmanager (that is, by |
| 456 // just saving a new password within this realm to overwrite it). | 628 // just saving a new password within this realm to overwrite it). |
| 457 return true; | 629 return true; |
| 458 } | 630 } |
| 459 | 631 |
| 460 // Can't we all just agree on whether bytes are signed or not? Please? | 632 // Can't we all just agree on whether bytes are signed or not? Please? |
| 461 Pickle pickle(reinterpret_cast<const char*>(bytes), length); | 633 Pickle pickle(reinterpret_cast<const char*>(bytes), length); |
| 462 DeserializeValue(signon_realm, pickle, forms); | 634 *forms = DeserializeValue(signon_realm, pickle); |
| 463 } | 635 } |
| 464 | 636 |
| 465 return true; | 637 return true; |
| 466 } | 638 } |
| 467 | 639 |
| 468 bool NativeBackendKWallet::GetLoginsList( | 640 bool NativeBackendKWallet::GetLoginsList( |
| 469 bool autofillable, | 641 BlacklistOptions options, |
| 470 int wallet_handle, | 642 int wallet_handle, |
| 471 ScopedVector<autofill::PasswordForm>* forms) { | 643 ScopedVector<autofill::PasswordForm>* forms) { |
| 644 forms->clear(); |
| 472 ScopedVector<autofill::PasswordForm> all_forms; | 645 ScopedVector<autofill::PasswordForm> all_forms; |
| 473 if (!GetAllLogins(wallet_handle, &all_forms)) | 646 if (!GetAllLogins(wallet_handle, &all_forms)) |
| 474 return false; | 647 return false; |
| 475 | 648 |
| 476 // We have to read all the entries, and then filter them here. | 649 // We have to read all the entries, and then filter them here. |
| 477 forms->reserve(forms->size() + all_forms.size()); | 650 forms->reserve(all_forms.size()); |
| 478 for (auto& saved_form : all_forms) { | 651 for (auto& saved_form : all_forms) { |
| 479 if (saved_form->blacklisted_by_user == !autofillable) { | 652 if (saved_form->blacklisted_by_user == |
| 653 (options == BlacklistOptions::BLACKLISTED)) { |
| 480 forms->push_back(saved_form); | 654 forms->push_back(saved_form); |
| 481 saved_form = nullptr; | 655 saved_form = nullptr; |
| 482 } | 656 } |
| 483 } | 657 } |
| 484 | 658 |
| 485 return true; | 659 return true; |
| 486 } | 660 } |
| 487 | 661 |
| 488 bool NativeBackendKWallet::GetAllLogins( | 662 bool NativeBackendKWallet::GetAllLogins( |
| 489 int wallet_handle, | 663 int wallet_handle, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 504 return false; | 678 return false; |
| 505 } | 679 } |
| 506 dbus::MessageReader reader(response.get()); | 680 dbus::MessageReader reader(response.get()); |
| 507 if (!reader.PopArrayOfStrings(&realm_list)) { | 681 if (!reader.PopArrayOfStrings(&realm_list)) { |
| 508 LOG(ERROR) << "Error reading response from kwalletd (entryList): " | 682 LOG(ERROR) << "Error reading response from kwalletd (entryList): " |
| 509 << response->ToString(); | 683 << response->ToString(); |
| 510 return false; | 684 return false; |
| 511 } | 685 } |
| 512 } | 686 } |
| 513 | 687 |
| 514 for (size_t i = 0; i < realm_list.size(); ++i) { | 688 // Swapping |collected_forms| with |*forms| just before "return true" makes |
| 515 const std::string& signon_realm = realm_list[i]; | 689 // sure partial results are not returned on failure. |
| 690 ScopedVector<autofill::PasswordForm> collected_forms; |
| 691 for (const std::string& signon_realm : realm_list) { |
| 516 dbus::MethodCall method_call(kKWalletInterface, "readEntry"); | 692 dbus::MethodCall method_call(kKWalletInterface, "readEntry"); |
| 517 dbus::MessageWriter builder(&method_call); | 693 dbus::MessageWriter builder(&method_call); |
| 518 builder.AppendInt32(wallet_handle); // handle | 694 builder.AppendInt32(wallet_handle); // handle |
| 519 builder.AppendString(folder_name_); // folder | 695 builder.AppendString(folder_name_); // folder |
| 520 builder.AppendString(signon_realm); // key | 696 builder.AppendString(signon_realm); // key |
| 521 builder.AppendString(app_name_); // appid | 697 builder.AppendString(app_name_); // appid |
| 522 scoped_ptr<dbus::Response> response( | 698 scoped_ptr<dbus::Response> response( |
| 523 kwallet_proxy_->CallMethodAndBlock( | 699 kwallet_proxy_->CallMethodAndBlock( |
| 524 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); | 700 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
| 525 if (!response.get()) { | 701 if (!response.get()) { |
| 526 LOG(ERROR) << "Error contacting kwalletd (readEntry)"; | 702 LOG(ERROR) << "Error contacting kwalletd (readEntry)"; |
| 527 return false; | 703 return false; |
| 528 } | 704 } |
| 529 dbus::MessageReader reader(response.get()); | 705 dbus::MessageReader reader(response.get()); |
| 530 const uint8_t* bytes = nullptr; | 706 const uint8_t* bytes = nullptr; |
| 531 size_t length = 0; | 707 size_t length = 0; |
| 532 if (!reader.PopArrayOfBytes(&bytes, &length)) { | 708 if (!reader.PopArrayOfBytes(&bytes, &length)) { |
| 533 LOG(ERROR) << "Error reading response from kwalletd (readEntry): " | 709 LOG(ERROR) << "Error reading response from kwalletd (readEntry): " |
| 534 << response->ToString(); | 710 << response->ToString(); |
| 535 return false; | 711 return false; |
| 536 } | 712 } |
| 537 if (!bytes || !CheckSerializedValue(bytes, length, signon_realm)) | 713 if (!bytes || !CheckSerializedValue(bytes, length, signon_realm)) |
| 538 continue; | 714 continue; |
| 539 | 715 |
| 540 // Can't we all just agree on whether bytes are signed or not? Please? | 716 // Can't we all just agree on whether bytes are signed or not? Please? |
| 541 Pickle pickle(reinterpret_cast<const char*>(bytes), length); | 717 Pickle pickle(reinterpret_cast<const char*>(bytes), length); |
| 542 DeserializeValue(signon_realm, pickle, forms); | 718 AppendSecondToFirst(&collected_forms, |
| 719 DeserializeValue(signon_realm, pickle)); |
| 543 } | 720 } |
| 721 forms->swap(collected_forms); |
| 544 return true; | 722 return true; |
| 545 } | 723 } |
| 546 | 724 |
| 547 bool NativeBackendKWallet::SetLoginsList( | 725 bool NativeBackendKWallet::SetLoginsList( |
| 548 const std::vector<autofill::PasswordForm*>& forms, | 726 const std::vector<autofill::PasswordForm*>& forms, |
| 549 const std::string& signon_realm, | 727 const std::string& signon_realm, |
| 550 int wallet_handle) { | 728 int wallet_handle) { |
| 551 if (forms.empty()) { | 729 if (forms.empty()) { |
| 552 // No items left? Remove the entry from the wallet. | 730 // No items left? Remove the entry from the wallet. |
| 553 dbus::MethodCall method_call(kKWalletInterface, "removeEntry"); | 731 dbus::MethodCall method_call(kKWalletInterface, "removeEntry"); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 if (!reader.PopArrayOfBytes(&bytes, &length)) { | 847 if (!reader.PopArrayOfBytes(&bytes, &length)) { |
| 670 LOG(ERROR) << "Error reading response from kwalletd (readEntry): " | 848 LOG(ERROR) << "Error reading response from kwalletd (readEntry): " |
| 671 << response->ToString(); | 849 << response->ToString(); |
| 672 continue; | 850 continue; |
| 673 } | 851 } |
| 674 if (!bytes || !CheckSerializedValue(bytes, length, signon_realm)) | 852 if (!bytes || !CheckSerializedValue(bytes, length, signon_realm)) |
| 675 continue; | 853 continue; |
| 676 | 854 |
| 677 // Can't we all just agree on whether bytes are signed or not? Please? | 855 // Can't we all just agree on whether bytes are signed or not? Please? |
| 678 Pickle pickle(reinterpret_cast<const char*>(bytes), length); | 856 Pickle pickle(reinterpret_cast<const char*>(bytes), length); |
| 679 ScopedVector<autofill::PasswordForm> all_forms; | 857 ScopedVector<autofill::PasswordForm> all_forms = |
| 680 DeserializeValue(signon_realm, pickle, &all_forms); | 858 DeserializeValue(signon_realm, pickle); |
| 681 | 859 |
| 682 ScopedVector<autofill::PasswordForm> kept_forms; | 860 ScopedVector<autofill::PasswordForm> kept_forms; |
| 683 kept_forms.reserve(all_forms.size()); | 861 kept_forms.reserve(all_forms.size()); |
| 684 base::Time autofill::PasswordForm::*date_member = | 862 base::Time autofill::PasswordForm::*date_member = |
| 685 date_to_compare == CREATION_TIMESTAMP | 863 date_to_compare == CREATION_TIMESTAMP |
| 686 ? &autofill::PasswordForm::date_created | 864 ? &autofill::PasswordForm::date_created |
| 687 : &autofill::PasswordForm::date_synced; | 865 : &autofill::PasswordForm::date_synced; |
| 688 for (auto& saved_form : all_forms) { | 866 for (auto& saved_form : all_forms) { |
| 689 if (delete_begin <= saved_form->*date_member && | 867 if (delete_begin <= saved_form->*date_member && |
| 690 (delete_end.is_null() || saved_form->*date_member < delete_end)) { | 868 (delete_end.is_null() || saved_form->*date_member < delete_end)) { |
| 691 changes->push_back(password_manager::PasswordStoreChange( | 869 changes->push_back(password_manager::PasswordStoreChange( |
| 692 password_manager::PasswordStoreChange::REMOVE, *saved_form)); | 870 password_manager::PasswordStoreChange::REMOVE, *saved_form)); |
| 693 } else { | 871 } else { |
| 694 kept_forms.push_back(saved_form); | 872 kept_forms.push_back(saved_form); |
| 695 saved_form = nullptr; | 873 saved_form = nullptr; |
| 696 } | 874 } |
| 697 } | 875 } |
| 698 | 876 |
| 699 if (!SetLoginsList(kept_forms.get(), signon_realm, wallet_handle)) { | 877 if (!SetLoginsList(kept_forms.get(), signon_realm, wallet_handle)) { |
| 700 ok = false; | 878 ok = false; |
| 701 changes->clear(); | 879 changes->clear(); |
| 702 } | 880 } |
| 703 } | 881 } |
| 704 return ok; | 882 return ok; |
| 705 } | 883 } |
| 706 | 884 |
| 707 // static | 885 // static |
| 708 void NativeBackendKWallet::SerializeValue( | 886 ScopedVector<autofill::PasswordForm> NativeBackendKWallet::DeserializeValue( |
| 709 const std::vector<autofill::PasswordForm*>& forms, | |
| 710 Pickle* pickle) { | |
| 711 pickle->WriteInt(kPickleVersion); | |
| 712 pickle->WriteSizeT(forms.size()); | |
| 713 for (autofill::PasswordForm* form : forms) { | |
| 714 pickle->WriteInt(form->scheme); | |
| 715 pickle->WriteString(form->origin.spec()); | |
| 716 pickle->WriteString(form->action.spec()); | |
| 717 pickle->WriteString16(form->username_element); | |
| 718 pickle->WriteString16(form->username_value); | |
| 719 pickle->WriteString16(form->password_element); | |
| 720 pickle->WriteString16(form->password_value); | |
| 721 pickle->WriteString16(form->submit_element); | |
| 722 pickle->WriteBool(form->ssl_valid); | |
| 723 pickle->WriteBool(form->preferred); | |
| 724 pickle->WriteBool(form->blacklisted_by_user); | |
| 725 pickle->WriteInt64(form->date_created.ToInternalValue()); | |
| 726 pickle->WriteInt(form->type); | |
| 727 pickle->WriteInt(form->times_used); | |
| 728 autofill::SerializeFormData(form->form_data, pickle); | |
| 729 pickle->WriteInt64(form->date_synced.ToInternalValue()); | |
| 730 pickle->WriteString16(form->display_name); | |
| 731 pickle->WriteString(form->avatar_url.spec()); | |
| 732 pickle->WriteString(form->federation_url.spec()); | |
| 733 pickle->WriteBool(form->skip_zero_click); | |
| 734 pickle->WriteInt(form->generation_upload_status); | |
| 735 } | |
| 736 } | |
| 737 | |
| 738 // static | |
| 739 bool NativeBackendKWallet::DeserializeValueSize( | |
| 740 const std::string& signon_realm, | 887 const std::string& signon_realm, |
| 741 const PickleIterator& init_iter, | 888 const Pickle& pickle) { |
| 742 int version, | |
| 743 bool size_32, | |
| 744 bool warn_only, | |
| 745 ScopedVector<autofill::PasswordForm>* forms) { | |
| 746 PickleIterator iter = init_iter; | |
| 747 | |
| 748 size_t count = 0; | |
| 749 if (size_32) { | |
| 750 uint32_t count_32 = 0; | |
| 751 if (!iter.ReadUInt32(&count_32)) { | |
| 752 LOG(ERROR) << "Failed to deserialize KWallet entry " | |
| 753 << "(realm: " << signon_realm << ")"; | |
| 754 return false; | |
| 755 } | |
| 756 count = count_32; | |
| 757 } else { | |
| 758 if (!iter.ReadSizeT(&count)) { | |
| 759 LOG(ERROR) << "Failed to deserialize KWallet entry " | |
| 760 << "(realm: " << signon_realm << ")"; | |
| 761 return false; | |
| 762 } | |
| 763 } | |
| 764 | |
| 765 if (count > 0xFFFF) { | |
| 766 // Trying to pin down the cause of http://crbug.com/80728 (or fix it). | |
| 767 // This is a very large number of passwords to be saved for a single realm. | |
| 768 // It is almost certainly a corrupt pickle and not real data. Ignore it. | |
| 769 // This very well might actually be http://crbug.com/107701, so if we're | |
| 770 // reading an old pickle, we don't even log this the first time we try to | |
| 771 // read it. (That is, when we're reading the native architecture size.) | |
| 772 if (!warn_only) { | |
| 773 LOG(ERROR) << "Suspiciously large number of entries in KWallet entry " | |
| 774 << "(" << count << "; realm: " << signon_realm << ")"; | |
| 775 } | |
| 776 return false; | |
| 777 } | |
| 778 | |
| 779 forms->reserve(forms->size() + count); | |
| 780 for (size_t i = 0; i < count; ++i) { | |
| 781 scoped_ptr<PasswordForm> form(new PasswordForm()); | |
| 782 form->signon_realm.assign(signon_realm); | |
| 783 | |
| 784 int scheme = 0; | |
| 785 int64 date_created = 0; | |
| 786 int type = 0; | |
| 787 int generation_upload_status = 0; | |
| 788 // Note that these will be read back in the order listed due to | |
| 789 // short-circuit evaluation. This is important. | |
| 790 if (!iter.ReadInt(&scheme) || | |
| 791 !ReadGURL(&iter, warn_only, &form->origin) || | |
| 792 !ReadGURL(&iter, warn_only, &form->action) || | |
| 793 !iter.ReadString16(&form->username_element) || | |
| 794 !iter.ReadString16(&form->username_value) || | |
| 795 !iter.ReadString16(&form->password_element) || | |
| 796 !iter.ReadString16(&form->password_value) || | |
| 797 !iter.ReadString16(&form->submit_element) || | |
| 798 !iter.ReadBool(&form->ssl_valid) || | |
| 799 !iter.ReadBool(&form->preferred) || | |
| 800 !iter.ReadBool(&form->blacklisted_by_user) || | |
| 801 !iter.ReadInt64(&date_created)) { | |
| 802 LogDeserializationWarning(version, signon_realm, warn_only); | |
| 803 return false; | |
| 804 } | |
| 805 form->scheme = static_cast<PasswordForm::Scheme>(scheme); | |
| 806 | |
| 807 if (version > 1) { | |
| 808 if (!iter.ReadInt(&type) || | |
| 809 !iter.ReadInt(&form->times_used) || | |
| 810 !autofill::DeserializeFormData(&iter, &form->form_data)) { | |
| 811 LogDeserializationWarning(version, signon_realm, false); | |
| 812 return false; | |
| 813 } | |
| 814 form->type = static_cast<PasswordForm::Type>(type); | |
| 815 } | |
| 816 | |
| 817 if (version > 2) { | |
| 818 int64 date_synced = 0; | |
| 819 if (!iter.ReadInt64(&date_synced)) { | |
| 820 LogDeserializationWarning(version, signon_realm, false); | |
| 821 return false; | |
| 822 } | |
| 823 form->date_synced = base::Time::FromInternalValue(date_synced); | |
| 824 } | |
| 825 | |
| 826 if (version > 3) { | |
| 827 if (!iter.ReadString16(&form->display_name) || | |
| 828 !ReadGURL(&iter, warn_only, &form->avatar_url) || | |
| 829 !ReadGURL(&iter, warn_only, &form->federation_url) || | |
| 830 !iter.ReadBool(&form->skip_zero_click)) { | |
| 831 LogDeserializationWarning(version, signon_realm, false); | |
| 832 return false; | |
| 833 } | |
| 834 } | |
| 835 | |
| 836 if (version > 4) { | |
| 837 form->date_created = base::Time::FromInternalValue(date_created); | |
| 838 } else { | |
| 839 form->date_created = base::Time::FromTimeT(date_created); | |
| 840 } | |
| 841 | |
| 842 if (version > 5) { | |
| 843 if (!iter.ReadInt(&generation_upload_status)) { | |
| 844 LogDeserializationWarning(version, signon_realm, false); | |
| 845 } | |
| 846 form->generation_upload_status = | |
| 847 static_cast<PasswordForm::GenerationUploadStatus>( | |
| 848 generation_upload_status); | |
| 849 } | |
| 850 | |
| 851 forms->push_back(form.release()); | |
| 852 } | |
| 853 | |
| 854 return true; | |
| 855 } | |
| 856 | |
| 857 // static | |
| 858 void NativeBackendKWallet::DeserializeValue( | |
| 859 const std::string& signon_realm, | |
| 860 const Pickle& pickle, | |
| 861 ScopedVector<autofill::PasswordForm>* forms) { | |
| 862 PickleIterator iter(pickle); | 889 PickleIterator iter(pickle); |
| 863 | 890 |
| 864 int version = -1; | 891 int version = -1; |
| 865 if (!iter.ReadInt(&version) || | 892 if (!iter.ReadInt(&version) || |
| 866 version < 0 || version > kPickleVersion) { | 893 version < 0 || version > kPickleVersion) { |
| 867 LOG(ERROR) << "Failed to deserialize KWallet entry " | 894 LOG(ERROR) << "Failed to deserialize KWallet entry " |
| 868 << "(realm: " << signon_realm << ")"; | 895 << "(realm: " << signon_realm << ")"; |
| 869 return; | 896 return ScopedVector<autofill::PasswordForm>(); |
| 870 } | 897 } |
| 871 | 898 |
| 899 ScopedVector<autofill::PasswordForm> forms; |
| 872 if (version > 0) { | 900 if (version > 0) { |
| 873 // In current pickles, we expect 64-bit sizes. Failure is an error. | 901 // In current pickles, we expect 64-bit sizes. Failure is an error. |
| 874 DeserializeValueSize(signon_realm, iter, version, false, false, forms); | 902 DeserializeValueSize(signon_realm, iter, version, false, false, &forms); |
| 875 return; | 903 return forms.Pass(); |
| 876 } | 904 } |
| 877 | 905 |
| 878 const size_t saved_forms_size = forms->size(); | |
| 879 const bool size_32 = sizeof(size_t) == sizeof(uint32_t); | 906 const bool size_32 = sizeof(size_t) == sizeof(uint32_t); |
| 880 if (!DeserializeValueSize( | 907 if (!DeserializeValueSize( |
| 881 signon_realm, iter, version, size_32, true, forms)) { | 908 signon_realm, iter, version, size_32, true, &forms)) { |
| 882 // We failed to read the pickle using the native architecture of the system. | 909 // We failed to read the pickle using the native architecture of the system. |
| 883 // Try again with the opposite architecture. Note that we do this even on | 910 // Try again with the opposite architecture. Note that we do this even on |
| 884 // 32-bit machines, in case we're reading a 64-bit pickle. (Probably rare, | 911 // 32-bit machines, in case we're reading a 64-bit pickle. (Probably rare, |
| 885 // since mostly we expect upgrades, not downgrades, but both are possible.) | 912 // since mostly we expect upgrades, not downgrades, but both are possible.) |
| 886 forms->resize(saved_forms_size); | 913 DeserializeValueSize(signon_realm, iter, version, !size_32, false, &forms); |
| 887 DeserializeValueSize(signon_realm, iter, version, !size_32, false, forms); | |
| 888 } | 914 } |
| 915 return forms.Pass(); |
| 889 } | 916 } |
| 890 | 917 |
| 891 int NativeBackendKWallet::WalletHandle() { | 918 int NativeBackendKWallet::WalletHandle() { |
| 892 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 919 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 893 | 920 |
| 894 // Open the wallet. | 921 // Open the wallet. |
| 895 // TODO(mdm): Are we leaking these handles? Find out. | 922 // TODO(mdm): Are we leaking these handles? Find out. |
| 896 int32_t handle = kInvalidKWalletHandle; | 923 int32_t handle = kInvalidKWalletHandle; |
| 897 { | 924 { |
| 898 dbus::MethodCall method_call(kKWalletInterface, "open"); | 925 dbus::MethodCall method_call(kKWalletInterface, "open"); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 970 } | 997 } |
| 971 | 998 |
| 972 return handle; | 999 return handle; |
| 973 } | 1000 } |
| 974 | 1001 |
| 975 std::string NativeBackendKWallet::GetProfileSpecificFolderName() const { | 1002 std::string NativeBackendKWallet::GetProfileSpecificFolderName() const { |
| 976 // Originally, the folder name was always just "Chrome Form Data". | 1003 // Originally, the folder name was always just "Chrome Form Data". |
| 977 // Now we use it to distinguish passwords for different profiles. | 1004 // Now we use it to distinguish passwords for different profiles. |
| 978 return base::StringPrintf("%s (%d)", kKWalletFolder, profile_id_); | 1005 return base::StringPrintf("%s (%d)", kKWalletFolder, profile_id_); |
| 979 } | 1006 } |
| OLD | NEW |