Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: chrome/browser/importer/firefox_importer_utils.cc

Issue 118443: Linux: Import passwords from Firefox. (Closed)
Patch Set: Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/importer/firefox_importer_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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/importer/firefox_importer_utils.h" 5 #include "chrome/browser/importer/firefox_importer_utils.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <shlobj.h> 10 #include <shlobj.h>
11 11
12 #include "app/win_util.h" 12 #include "app/win_util.h"
13 #include "base/registry.h" 13 #include "base/registry.h"
14 #endif 14 #endif
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/string_util.h" 16 #include "base/string_util.h"
17 #include "base/sys_string_conversions.h" 17 #include "base/sys_string_conversions.h"
18 #include "chrome/browser/search_engines/template_url.h" 18 #include "chrome/browser/search_engines/template_url.h"
19 #include "chrome/browser/search_engines/template_url_model.h" 19 #include "chrome/browser/search_engines/template_url_model.h"
20 #include "chrome/browser/search_engines/template_url_parser.h" 20 #include "chrome/browser/search_engines/template_url_parser.h"
21 #include "googleurl/src/gurl.h" 21 #include "googleurl/src/gurl.h"
22 #include "net/base/base64.h" 22 #include "net/base/base64.h"
23 23
24 24
25 namespace { 25 namespace {
26 26
27 #if defined(OS_WIN)
28 // NOTE: Keep these in order since we need test all those paths according
29 // to priority. For example. One machine has multiple users. One non-admin
30 // user installs Firefox 2, which causes there is a Firefox2 entry under HKCU.
31 // One admin user installs Firefox 3, which causes there is a Firefox 3 entry
32 // under HKLM. So when the non-admin user log in, we should deal with Firefox 2
33 // related data instead of Firefox 3.
34 static const HKEY kFireFoxRegistryPaths[] = {
35 HKEY_CURRENT_USER,
36 HKEY_LOCAL_MACHINE
37 };
38 #endif
39
40 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from 27 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from
41 // the search URL when importing search engines. 28 // the search URL when importing search engines.
42 class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter { 29 class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter {
43 public: 30 public:
44 FirefoxURLParameterFilter() { } 31 FirefoxURLParameterFilter() { }
45 ~FirefoxURLParameterFilter() { } 32 ~FirefoxURLParameterFilter() { }
46 33
47 // TemplateURLParser::ParameterFilter method. 34 // TemplateURLParser::ParameterFilter method.
48 virtual bool KeepParameter(const std::string& key, 35 virtual bool KeepParameter(const std::string& key,
49 const std::string& value) { 36 const std::string& value) {
(...skipping 27 matching lines...) Expand all
77 void set_func(SetDllDirectoryFunc func) { func_ = func; } 64 void set_func(SetDllDirectoryFunc func) { func_ = func; }
78 65
79 private: 66 private:
80 SetDllDirectoryFunc func_; 67 SetDllDirectoryFunc func_;
81 }; 68 };
82 #endif 69 #endif
83 70
84 } // namespace 71 } // namespace
85 72
86 #if defined(OS_WIN) 73 #if defined(OS_WIN)
74 // NOTE: Keep these in order since we need test all those paths according
75 // to priority. For example. One machine has multiple users. One non-admin
76 // user installs Firefox 2, which causes there is a Firefox2 entry under HKCU.
77 // One admin user installs Firefox 3, which causes there is a Firefox 3 entry
78 // under HKLM. So when the non-admin user log in, we should deal with Firefox 2
79 // related data instead of Firefox 3.
80 static const HKEY kFireFoxRegistryPaths[] = {
81 HKEY_CURRENT_USER,
82 HKEY_LOCAL_MACHINE
83 };
84
87 int GetCurrentFirefoxMajorVersionFromRegistry() { 85 int GetCurrentFirefoxMajorVersionFromRegistry() {
88 TCHAR ver_buffer[128]; 86 TCHAR ver_buffer[128];
89 DWORD ver_buffer_length = sizeof(ver_buffer); 87 DWORD ver_buffer_length = sizeof(ver_buffer);
90 int highest_version = 0; 88 int highest_version = 0;
91 // When installing Firefox with admin account, the product keys will be 89 // When installing Firefox with admin account, the product keys will be
92 // written under HKLM\Mozilla. Otherwise it the keys will be written under 90 // written under HKLM\Mozilla. Otherwise it the keys will be written under
93 // HKCU\Mozilla. 91 // HKCU\Mozilla.
94 for (int i = 0; i < arraysize(kFireFoxRegistryPaths); ++i) { 92 for (int i = 0; i < arraysize(kFireFoxRegistryPaths); ++i) {
95 bool result = ReadFromRegistry(kFireFoxRegistryPaths[i], 93 bool result = ReadFromRegistry(kFireFoxRegistryPaths[i],
96 L"Software\\Mozilla\\Mozilla Firefox", 94 L"Software\\Mozilla\\Mozilla Firefox",
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 const wchar_t NSSDecryptor::kNSS3Library[] = L"nss3.dll"; 445 const wchar_t NSSDecryptor::kNSS3Library[] = L"nss3.dll";
448 const wchar_t NSSDecryptor::kSoftokn3Library[] = L"softokn3.dll"; 446 const wchar_t NSSDecryptor::kSoftokn3Library[] = L"softokn3.dll";
449 const wchar_t NSSDecryptor::kPLDS4Library[] = L"plds4.dll"; 447 const wchar_t NSSDecryptor::kPLDS4Library[] = L"plds4.dll";
450 const wchar_t NSSDecryptor::kNSPR4Library[] = L"nspr4.dll"; 448 const wchar_t NSSDecryptor::kNSPR4Library[] = L"nspr4.dll";
451 449
452 NSSDecryptor::NSSDecryptor() 450 NSSDecryptor::NSSDecryptor()
453 : NSS_Init(NULL), NSS_Shutdown(NULL), PK11_GetInternalKeySlot(NULL), 451 : NSS_Init(NULL), NSS_Shutdown(NULL), PK11_GetInternalKeySlot(NULL),
454 PK11_CheckUserPassword(NULL), PK11_FreeSlot(NULL), 452 PK11_CheckUserPassword(NULL), PK11_FreeSlot(NULL),
455 PK11_Authenticate(NULL), PK11SDR_Decrypt(NULL), SECITEM_FreeItem(NULL), 453 PK11_Authenticate(NULL), PK11SDR_Decrypt(NULL), SECITEM_FreeItem(NULL),
456 PL_ArenaFinish(NULL), PR_Cleanup(NULL), 454 PL_ArenaFinish(NULL), PR_Cleanup(NULL),
457 #if defined(OS_WIN)
458 nss3_dll_(NULL), softokn3_dll_(NULL), 455 nss3_dll_(NULL), softokn3_dll_(NULL),
459 #endif
460 is_nss_initialized_(false) { 456 is_nss_initialized_(false) {
461 } 457 }
462 458
463 NSSDecryptor::~NSSDecryptor() { 459 NSSDecryptor::~NSSDecryptor() {
464 Free(); 460 Free();
465 } 461 }
466 462
467 bool NSSDecryptor::Init(const std::wstring& dll_path, 463 bool NSSDecryptor::Init(const std::wstring& dll_path,
468 const std::wstring& db_path) { 464 const std::wstring& db_path) {
469 #if defined(OS_WIN) 465 #if defined(OS_WIN)
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 // harmless for NSS 3.11. 503 // harmless for NSS 3.11.
508 path = dll_path; 504 path = dll_path;
509 file_util::AppendToPath(&path, kSoftokn3Library); 505 file_util::AppendToPath(&path, kSoftokn3Library);
510 softokn3_dll_ = LoadLibraryEx(path.c_str(), NULL, 506 softokn3_dll_ = LoadLibraryEx(path.c_str(), NULL,
511 LOAD_WITH_ALTERED_SEARCH_PATH); 507 LOAD_WITH_ALTERED_SEARCH_PATH);
512 if (softokn3_dll_ == NULL) { 508 if (softokn3_dll_ == NULL) {
513 Free(); 509 Free();
514 return false; 510 return false;
515 } 511 }
516 } 512 }
513 HMODULE plds4_dll = GetModuleHandle(kPLDS4Library);
514 HMODULE nspr4_dll = GetModuleHandle(kNSPR4Library);
515 #elif defined(OS_LINUX)
516 nss3_dll_ = base::LoadNativeLibrary(FilePath("libnss3.so"));
517 if (nss3_dll_ == NULL)
518 return false;
519 base::NativeLibrary plds4_dll = base::LoadNativeLibrary(
520 FilePath("libplds4.so"));
521 base::NativeLibrary nspr4_dll = base::LoadNativeLibrary(
522 FilePath("libnspr4.so"));
523 #else
524 // TODO(port): Check on MAC
525 base::NativeLibrary plds4_dll, nspr4_dll;
526 NOTIMPLEMENTED();
527 return false;
528 #endif
517 529
518 // NSPR DLLs are already loaded now. 530 // NSPR DLLs are already loaded now.
519 HMODULE plds4_dll = GetModuleHandle(kPLDS4Library);
520 HMODULE nspr4_dll = GetModuleHandle(kNSPR4Library);
521 if (plds4_dll == NULL || nspr4_dll == NULL) { 531 if (plds4_dll == NULL || nspr4_dll == NULL) {
522 Free(); 532 Free();
523 return false; 533 return false;
524 } 534 }
525 535
526 // Gets the function address. 536 // Gets the function address.
527 NSS_Init = (NSSInitFunc)GetProcAddress(nss3_dll_, "NSS_Init"); 537 NSS_Init = (NSSInitFunc)
528 NSS_Shutdown = (NSSShutdownFunc)GetProcAddress(nss3_dll_, "NSS_Shutdown"); 538 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "NSS_Init");
539 NSS_Shutdown = (NSSShutdownFunc)
540 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "NSS_Shutdown");
529 PK11_GetInternalKeySlot = (PK11GetInternalKeySlotFunc) 541 PK11_GetInternalKeySlot = (PK11GetInternalKeySlotFunc)
530 GetProcAddress(nss3_dll_, "PK11_GetInternalKeySlot"); 542 base::GetFunctionPointerFromNativeLibrary(nss3_dll_,
531 PK11_FreeSlot = (PK11FreeSlotFunc)GetProcAddress(nss3_dll_, "PK11_FreeSlot"); 543 "PK11_GetInternalKeySlot");
544 PK11_FreeSlot = (PK11FreeSlotFunc)
545 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11_FreeSlot");
532 PK11_Authenticate = (PK11AuthenticateFunc) 546 PK11_Authenticate = (PK11AuthenticateFunc)
533 GetProcAddress(nss3_dll_, "PK11_Authenticate"); 547 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11_Authenticate");
534 PK11SDR_Decrypt = (PK11SDRDecryptFunc) 548 PK11SDR_Decrypt = (PK11SDRDecryptFunc)
535 GetProcAddress(nss3_dll_, "PK11SDR_Decrypt"); 549 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11SDR_Decrypt");
536 SECITEM_FreeItem = (SECITEMFreeItemFunc) 550 SECITEM_FreeItem = (SECITEMFreeItemFunc)
537 GetProcAddress(nss3_dll_, "SECITEM_FreeItem"); 551 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "SECITEM_FreeItem");
538 PL_ArenaFinish = (PLArenaFinishFunc) 552 PL_ArenaFinish = (PLArenaFinishFunc)
539 GetProcAddress(plds4_dll, "PL_ArenaFinish"); 553 base::GetFunctionPointerFromNativeLibrary(plds4_dll, "PL_ArenaFinish");
540 PR_Cleanup = (PRCleanupFunc)GetProcAddress(nspr4_dll, "PR_Cleanup"); 554 PR_Cleanup = (PRCleanupFunc)
555 base::GetFunctionPointerFromNativeLibrary(nspr4_dll, "PR_Cleanup");
541 556
542 if (NSS_Init == NULL || NSS_Shutdown == NULL || 557 if (NSS_Init == NULL || NSS_Shutdown == NULL ||
543 PK11_GetInternalKeySlot == NULL || PK11_FreeSlot == NULL || 558 PK11_GetInternalKeySlot == NULL || PK11_FreeSlot == NULL ||
544 PK11_Authenticate == NULL || PK11SDR_Decrypt == NULL || 559 PK11_Authenticate == NULL || PK11SDR_Decrypt == NULL ||
545 SECITEM_FreeItem == NULL || PL_ArenaFinish == NULL || 560 SECITEM_FreeItem == NULL || PL_ArenaFinish == NULL ||
546 PR_Cleanup == NULL) { 561 PR_Cleanup == NULL) {
547 Free(); 562 Free();
548 return false; 563 return false;
549 } 564 }
550 565
551 SECStatus result = NSS_Init(base::SysWideToNativeMB(db_path).c_str()); 566 SECStatus result = NSS_Init(base::SysWideToNativeMB(db_path).c_str());
552 if (result != SECSuccess) { 567 if (result != SECSuccess) {
553 Free(); 568 Free();
554 return false; 569 return false;
555 } 570 }
556 571
557 is_nss_initialized_ = true; 572 is_nss_initialized_ = true;
558 return true; 573 return true;
559 #else
560 // TODO(port): Load NSS.
561 NOTIMPLEMENTED();
562 return false;
563 #endif
564 } 574 }
565 575
566 void NSSDecryptor::Free() { 576 void NSSDecryptor::Free() {
567 if (is_nss_initialized_) { 577 if (is_nss_initialized_) {
568 NSS_Shutdown(); 578 NSS_Shutdown();
569 PL_ArenaFinish(); 579 PL_ArenaFinish();
570 PR_Cleanup(); 580 PR_Cleanup();
571 is_nss_initialized_ = false; 581 is_nss_initialized_ = false;
572 } 582 }
573 #if defined(OS_WIN) 583 #if defined(OS_WIN) || defined(OS_LINUX)
574 if (softokn3_dll_ != NULL) 584 if (softokn3_dll_ != NULL)
575 FreeLibrary(softokn3_dll_); 585 base::UnloadNativeLibrary(softokn3_dll_);
576 softokn3_dll_ = NULL;
577 if (nss3_dll_ != NULL) 586 if (nss3_dll_ != NULL)
578 FreeLibrary(nss3_dll_); 587 base::UnloadNativeLibrary(nss3_dll_);
579 nss3_dll_ = NULL;
580 #endif 588 #endif
581 NSS_Init = NULL; 589 NSS_Init = NULL;
582 NSS_Shutdown = NULL; 590 NSS_Shutdown = NULL;
583 PK11_GetInternalKeySlot = NULL; 591 PK11_GetInternalKeySlot = NULL;
584 PK11_FreeSlot = NULL; 592 PK11_FreeSlot = NULL;
585 PK11_Authenticate = NULL; 593 PK11_Authenticate = NULL;
586 PK11SDR_Decrypt = NULL; 594 PK11SDR_Decrypt = NULL;
587 SECITEM_FreeItem = NULL; 595 SECITEM_FreeItem = NULL;
588 PL_ArenaFinish = NULL; 596 PL_ArenaFinish = NULL;
589 PR_Cleanup = NULL; 597 PR_Cleanup = NULL;
598 nss3_dll_ = NULL;
599 softokn3_dll_ = NULL;
590 } 600 }
591 601
592 // This method is based on some Firefox code in 602 // This method is based on some Firefox code in
593 // security/manager/ssl/src/nsSDR.cpp 603 // security/manager/ssl/src/nsSDR.cpp
594 // The license block is: 604 // The license block is:
595 605
596 /* ***** BEGIN LICENSE BLOCK ***** 606 /* ***** BEGIN LICENSE BLOCK *****
597 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 607 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
598 * 608 *
599 * The contents of this file are subject to the Mozilla Public License Version 609 * The contents of this file are subject to the Mozilla Public License Version
(...skipping 23 matching lines...) Expand all
623 * under the terms of either the GPL or the LGPL, and not to allow others to 633 * under the terms of either the GPL or the LGPL, and not to allow others to
624 * use your version of this file under the terms of the MPL, indicate your 634 * use your version of this file under the terms of the MPL, indicate your
625 * decision by deleting the provisions above and replace them with the notice 635 * decision by deleting the provisions above and replace them with the notice
626 * and other provisions required by the GPL or the LGPL. If you do not delete 636 * and other provisions required by the GPL or the LGPL. If you do not delete
627 * the provisions above, a recipient may use your version of this file under 637 * the provisions above, a recipient may use your version of this file under
628 * the terms of any one of the MPL, the GPL or the LGPL. 638 * the terms of any one of the MPL, the GPL or the LGPL.
629 * 639 *
630 * ***** END LICENSE BLOCK ***** */ 640 * ***** END LICENSE BLOCK ***** */
631 641
632 std::wstring NSSDecryptor::Decrypt(const std::string& crypt) const { 642 std::wstring NSSDecryptor::Decrypt(const std::string& crypt) const {
633 #if defined(OS_WIN) 643 #if !defined(OS_WIN) && !defined(OS_LINUX)
634 // Do nothing if NSS is not loaded.
635 if (!nss3_dll_)
636 return std::wstring();
637 #else
638 // TODO(port): Load nss3. 644 // TODO(port): Load nss3.
639 NOTIMPLEMENTED(); 645 NOTIMPLEMENTED();
640 return std::wstring(); 646 return std::wstring();
641 #endif 647 #endif
642 648
643 std::string plain; 649 // Do nothing if NSS is not loaded.
650 if (!nss3_dll_)
651 return std::wstring();
644 652
645 // The old style password is encoded in base64. They are identified 653 // The old style password is encoded in base64. They are identified
646 // by a leading '~'. Otherwise, we should decrypt the text. 654 // by a leading '~'. Otherwise, we should decrypt the text.
655 std::string plain;
647 if (crypt[0] != '~') { 656 if (crypt[0] != '~') {
648 std::string decoded_data; 657 std::string decoded_data;
649 net::Base64Decode(crypt, &decoded_data); 658 net::Base64Decode(crypt, &decoded_data);
650 PK11SlotInfo* slot = NULL; 659 PK11SlotInfo* slot = NULL;
651 slot = PK11_GetInternalKeySlot(); 660 slot = PK11_GetInternalKeySlot();
652 SECStatus result = PK11_Authenticate(slot, PR_TRUE, NULL); 661 SECStatus result = PK11_Authenticate(slot, PR_TRUE, NULL);
653 if (result != SECSuccess) { 662 if (result != SECSuccess) {
654 PK11_FreeSlot(slot); 663 PK11_FreeSlot(slot);
655 return std::wstring(); 664 return std::wstring();
656 } 665 }
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 } 804 }
796 // Version 3 has an extra line for further use. 805 // Version 3 has an extra line for further use.
797 if (version == 3) { 806 if (version == 3) {
798 ++begin; 807 ++begin;
799 } 808 }
800 809
801 forms->push_back(form); 810 forms->push_back(form);
802 } 811 }
803 } 812 }
804 } 813 }
OLDNEW
« no previous file with comments | « chrome/browser/importer/firefox_importer_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698