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

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

Issue 119267: Linux: Import passwords from Firefox. (Closed)
Patch Set: use base/native_library 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 NOTIMPLEMENTED();
526 return false;
527 #endif
517 528
518 // NSPR DLLs are already loaded now. 529 // 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) { 530 if (plds4_dll == NULL || nspr4_dll == NULL) {
522 Free(); 531 Free();
523 return false; 532 return false;
524 } 533 }
525 534
526 // Gets the function address. 535 // Gets the function address.
527 NSS_Init = (NSSInitFunc)GetProcAddress(nss3_dll_, "NSS_Init"); 536 NSS_Init = (NSSInitFunc)
528 NSS_Shutdown = (NSSShutdownFunc)GetProcAddress(nss3_dll_, "NSS_Shutdown"); 537 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "NSS_Init");
538 NSS_Shutdown = (NSSShutdownFunc)
539 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "NSS_Shutdown");
529 PK11_GetInternalKeySlot = (PK11GetInternalKeySlotFunc) 540 PK11_GetInternalKeySlot = (PK11GetInternalKeySlotFunc)
530 GetProcAddress(nss3_dll_, "PK11_GetInternalKeySlot"); 541 base::GetFunctionPointerFromNativeLibrary(nss3_dll_,
531 PK11_FreeSlot = (PK11FreeSlotFunc)GetProcAddress(nss3_dll_, "PK11_FreeSlot"); 542 "PK11_GetInternalKeySlot");
543 PK11_FreeSlot = (PK11FreeSlotFunc)
544 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11_FreeSlot");
532 PK11_Authenticate = (PK11AuthenticateFunc) 545 PK11_Authenticate = (PK11AuthenticateFunc)
533 GetProcAddress(nss3_dll_, "PK11_Authenticate"); 546 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11_Authenticate");
534 PK11SDR_Decrypt = (PK11SDRDecryptFunc) 547 PK11SDR_Decrypt = (PK11SDRDecryptFunc)
535 GetProcAddress(nss3_dll_, "PK11SDR_Decrypt"); 548 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11SDR_Decrypt");
536 SECITEM_FreeItem = (SECITEMFreeItemFunc) 549 SECITEM_FreeItem = (SECITEMFreeItemFunc)
537 GetProcAddress(nss3_dll_, "SECITEM_FreeItem"); 550 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "SECITEM_FreeItem");
538 PL_ArenaFinish = (PLArenaFinishFunc) 551 PL_ArenaFinish = (PLArenaFinishFunc)
539 GetProcAddress(plds4_dll, "PL_ArenaFinish"); 552 base::GetFunctionPointerFromNativeLibrary(plds4_dll, "PL_ArenaFinish");
540 PR_Cleanup = (PRCleanupFunc)GetProcAddress(nspr4_dll, "PR_Cleanup"); 553 PR_Cleanup = (PRCleanupFunc)
554 base::GetFunctionPointerFromNativeLibrary(nspr4_dll, "PR_Cleanup");
541 555
542 if (NSS_Init == NULL || NSS_Shutdown == NULL || 556 if (NSS_Init == NULL || NSS_Shutdown == NULL ||
543 PK11_GetInternalKeySlot == NULL || PK11_FreeSlot == NULL || 557 PK11_GetInternalKeySlot == NULL || PK11_FreeSlot == NULL ||
544 PK11_Authenticate == NULL || PK11SDR_Decrypt == NULL || 558 PK11_Authenticate == NULL || PK11SDR_Decrypt == NULL ||
545 SECITEM_FreeItem == NULL || PL_ArenaFinish == NULL || 559 SECITEM_FreeItem == NULL || PL_ArenaFinish == NULL ||
546 PR_Cleanup == NULL) { 560 PR_Cleanup == NULL) {
547 Free(); 561 Free();
548 return false; 562 return false;
549 } 563 }
550 564
551 SECStatus result = NSS_Init(base::SysWideToNativeMB(db_path).c_str()); 565 SECStatus result = NSS_Init(base::SysWideToNativeMB(db_path).c_str());
552 if (result != SECSuccess) { 566 if (result != SECSuccess) {
553 Free(); 567 Free();
554 return false; 568 return false;
555 } 569 }
556 570
557 is_nss_initialized_ = true; 571 is_nss_initialized_ = true;
558 return true; 572 return true;
559 #else
560 // TODO(port): Load NSS.
561 NOTIMPLEMENTED();
562 return false;
563 #endif
564 } 573 }
565 574
566 void NSSDecryptor::Free() { 575 void NSSDecryptor::Free() {
567 if (is_nss_initialized_) { 576 if (is_nss_initialized_) {
568 NSS_Shutdown(); 577 NSS_Shutdown();
569 PL_ArenaFinish(); 578 PL_ArenaFinish();
570 PR_Cleanup(); 579 PR_Cleanup();
571 is_nss_initialized_ = false; 580 is_nss_initialized_ = false;
572 } 581 }
573 #if defined(OS_WIN) 582 #if defined(OS_WIN) || defined(OS_LINUX)
574 if (softokn3_dll_ != NULL) 583 if (softokn3_dll_ != NULL)
575 FreeLibrary(softokn3_dll_); 584 base::UnloadNativeLibrary(softokn3_dll_);
576 softokn3_dll_ = NULL;
577 if (nss3_dll_ != NULL) 585 if (nss3_dll_ != NULL)
578 FreeLibrary(nss3_dll_); 586 base::UnloadNativeLibrary(nss3_dll_);
579 nss3_dll_ = NULL;
580 #endif 587 #endif
581 NSS_Init = NULL; 588 NSS_Init = NULL;
582 NSS_Shutdown = NULL; 589 NSS_Shutdown = NULL;
583 PK11_GetInternalKeySlot = NULL; 590 PK11_GetInternalKeySlot = NULL;
584 PK11_FreeSlot = NULL; 591 PK11_FreeSlot = NULL;
585 PK11_Authenticate = NULL; 592 PK11_Authenticate = NULL;
586 PK11SDR_Decrypt = NULL; 593 PK11SDR_Decrypt = NULL;
587 SECITEM_FreeItem = NULL; 594 SECITEM_FreeItem = NULL;
588 PL_ArenaFinish = NULL; 595 PL_ArenaFinish = NULL;
589 PR_Cleanup = NULL; 596 PR_Cleanup = NULL;
597 nss3_dll_ = NULL;
598 softokn3_dll_ = NULL;
590 } 599 }
591 600
592 // This method is based on some Firefox code in 601 // This method is based on some Firefox code in
593 // security/manager/ssl/src/nsSDR.cpp 602 // security/manager/ssl/src/nsSDR.cpp
594 // The license block is: 603 // The license block is:
595 604
596 /* ***** BEGIN LICENSE BLOCK ***** 605 /* ***** BEGIN LICENSE BLOCK *****
597 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 606 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
598 * 607 *
599 * The contents of this file are subject to the Mozilla Public License Version 608 * 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 632 * 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 633 * 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 634 * 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 635 * 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 636 * 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. 637 * the terms of any one of the MPL, the GPL or the LGPL.
629 * 638 *
630 * ***** END LICENSE BLOCK ***** */ 639 * ***** END LICENSE BLOCK ***** */
631 640
632 std::wstring NSSDecryptor::Decrypt(const std::string& crypt) const { 641 std::wstring NSSDecryptor::Decrypt(const std::string& crypt) const {
633 #if defined(OS_WIN) 642 #if !defined(OS_WIN) && !defined(OS_LINUX)
tony 2009/06/09 00:00:51 Nit: Do we need this block or will the if (!nss3_d
kuchhal 2009/06/09 00:18:31 Yes returning empty string will work too but may b
634 // Do nothing if NSS is not loaded.
635 if (!nss3_dll_)
636 return std::wstring();
637 #else
638 // TODO(port): Load nss3. 643 // TODO(port): Load nss3.
639 NOTIMPLEMENTED(); 644 NOTIMPLEMENTED();
640 return std::wstring(); 645 return std::wstring();
641 #endif 646 #endif
642 647
643 std::string plain; 648 // Do nothing if NSS is not loaded.
649 if (!nss3_dll_)
650 return std::wstring();
644 651
645 // The old style password is encoded in base64. They are identified 652 // The old style password is encoded in base64. They are identified
646 // by a leading '~'. Otherwise, we should decrypt the text. 653 // by a leading '~'. Otherwise, we should decrypt the text.
654 std::string plain;
647 if (crypt[0] != '~') { 655 if (crypt[0] != '~') {
648 std::string decoded_data; 656 std::string decoded_data;
649 net::Base64Decode(crypt, &decoded_data); 657 net::Base64Decode(crypt, &decoded_data);
650 PK11SlotInfo* slot = NULL; 658 PK11SlotInfo* slot = NULL;
651 slot = PK11_GetInternalKeySlot(); 659 slot = PK11_GetInternalKeySlot();
652 SECStatus result = PK11_Authenticate(slot, PR_TRUE, NULL); 660 SECStatus result = PK11_Authenticate(slot, PR_TRUE, NULL);
653 if (result != SECSuccess) { 661 if (result != SECSuccess) {
654 PK11_FreeSlot(slot); 662 PK11_FreeSlot(slot);
655 return std::wstring(); 663 return std::wstring();
656 } 664 }
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
795 } 803 }
796 // Version 3 has an extra line for further use. 804 // Version 3 has an extra line for further use.
797 if (version == 3) { 805 if (version == 3) {
798 ++begin; 806 ++begin;
799 } 807 }
800 808
801 forms->push_back(form); 809 forms->push_back(form);
802 } 810 }
803 } 811 }
804 } 812 }
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