| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/firefox_importer_utils.h" | |
| 6 | |
| 7 #include <shlobj.h> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/registry.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "base/sys_string_conversions.h" | |
| 14 #include "base/time.h" | |
| 15 #include "chrome/browser/template_url.h" | |
| 16 #include "chrome/browser/template_url_model.h" | |
| 17 #include "chrome/browser/template_url_parser.h" | |
| 18 #include "chrome/common/win_util.h" | |
| 19 #include "googleurl/src/gurl.h" | |
| 20 #include "net/base/base64.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // NOTE: Keep these in order since we need test all those paths according | |
| 25 // to priority. For example. One machine has multiple users. One non-admin | |
| 26 // user installs Firefox 2, which causes there is a Firefox2 entry under HKCU. | |
| 27 // One admin user installs Firefox 3, which causes there is a Firefox 3 entry | |
| 28 // under HKLM. So when the non-admin user log in, we should deal with Firefox 2 | |
| 29 // related data instead of Firefox 3. | |
| 30 static const HKEY kFireFoxRegistryPaths[] = { | |
| 31 HKEY_CURRENT_USER, | |
| 32 HKEY_LOCAL_MACHINE | |
| 33 }; | |
| 34 | |
| 35 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from | |
| 36 // the search URL when importing search engines. | |
| 37 class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter { | |
| 38 public: | |
| 39 FirefoxURLParameterFilter() { } | |
| 40 ~FirefoxURLParameterFilter() { } | |
| 41 | |
| 42 // TemplateURLParser::ParameterFilter method. | |
| 43 virtual bool KeepParameter(const std::string& key, | |
| 44 const std::string& value) { | |
| 45 std::string low_value = StringToLowerASCII(value); | |
| 46 if (low_value.find("mozilla") != -1 || low_value.find("firefox") != -1 || | |
| 47 low_value.find("moz:") != -1 ) | |
| 48 return false; | |
| 49 return true; | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 DISALLOW_EVIL_CONSTRUCTORS(FirefoxURLParameterFilter); | |
| 54 }; | |
| 55 | |
| 56 typedef BOOL (WINAPI* SetDllDirectoryFunc)(LPCTSTR lpPathName); | |
| 57 | |
| 58 // A helper class whose destructor calls SetDllDirectory(NULL) to undo the | |
| 59 // effects of a previous SetDllDirectory call. | |
| 60 class SetDllDirectoryCaller { | |
| 61 public: | |
| 62 explicit SetDllDirectoryCaller() : func_(NULL) { } | |
| 63 | |
| 64 ~SetDllDirectoryCaller() { | |
| 65 if (func_) | |
| 66 func_(NULL); | |
| 67 } | |
| 68 | |
| 69 // Sets the SetDllDirectory function pointer to activates this object. | |
| 70 void set_func(SetDllDirectoryFunc func) { func_ = func; } | |
| 71 | |
| 72 private: | |
| 73 SetDllDirectoryFunc func_; | |
| 74 }; | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 int GetCurrentFirefoxMajorVersion() { | |
| 79 TCHAR ver_buffer[128]; | |
| 80 DWORD ver_buffer_length = sizeof(ver_buffer); | |
| 81 // When installing Firefox with admin account, the product keys will be | |
| 82 // written under HKLM\Mozilla. Otherwise it the keys will be written under | |
| 83 // HKCU\Mozilla. | |
| 84 for (int i = 0; i < arraysize(kFireFoxRegistryPaths); ++i) { | |
| 85 bool result = ReadFromRegistry(kFireFoxRegistryPaths[i], | |
| 86 L"Software\\Mozilla\\Mozilla Firefox", | |
| 87 L"CurrentVersion", ver_buffer, &ver_buffer_length); | |
| 88 if (!result) | |
| 89 continue; | |
| 90 return _wtoi(ver_buffer); | |
| 91 } | |
| 92 return 0; | |
| 93 } | |
| 94 | |
| 95 std::wstring GetProfilesINI() { | |
| 96 // The default location of the profile folder containing user data is | |
| 97 // under the "Application Data" folder in Windows XP. | |
| 98 std::wstring ini_file; | |
| 99 wchar_t buffer[MAX_PATH] = {0}; | |
| 100 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, | |
| 101 SHGFP_TYPE_CURRENT, buffer))) { | |
| 102 ini_file = buffer; | |
| 103 file_util::AppendToPath(&ini_file, L"Mozilla\\Firefox\\profiles.ini"); | |
| 104 } | |
| 105 if (!file_util::PathExists(ini_file)) | |
| 106 ini_file.clear(); | |
| 107 | |
| 108 return ini_file; | |
| 109 } | |
| 110 | |
| 111 std::wstring GetFirefoxInstallPath() { | |
| 112 // Detects the path that Firefox is installed in. | |
| 113 std::wstring registry_path = L"Software\\Mozilla\\Mozilla Firefox"; | |
| 114 TCHAR buffer[MAX_PATH]; | |
| 115 DWORD buffer_length = sizeof(buffer); | |
| 116 bool result; | |
| 117 result = ReadFromRegistry(HKEY_LOCAL_MACHINE, registry_path.c_str(), | |
| 118 L"CurrentVersion", buffer, &buffer_length); | |
| 119 if (!result) | |
| 120 return std::wstring(); | |
| 121 registry_path += L"\\" + std::wstring(buffer) + L"\\Main"; | |
| 122 buffer_length = sizeof(buffer); | |
| 123 result = ReadFromRegistry(HKEY_LOCAL_MACHINE, registry_path.c_str(), | |
| 124 L"Install Directory", buffer, &buffer_length); | |
| 125 if (!result) | |
| 126 return std::wstring(); | |
| 127 return buffer; | |
| 128 } | |
| 129 | |
| 130 void ParseProfileINI(std::wstring file, DictionaryValue* root) { | |
| 131 // Reads the whole INI file. | |
| 132 std::string content; | |
| 133 file_util::ReadFileToString(file, &content); | |
| 134 ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n"); | |
| 135 std::vector<std::string> lines; | |
| 136 SplitString(content, '\n', &lines); | |
| 137 | |
| 138 // Parses the file. | |
| 139 root->Clear(); | |
| 140 std::wstring current_section; | |
| 141 for (size_t i = 0; i < lines.size(); ++i) { | |
| 142 std::wstring line = UTF8ToWide(lines[i]); | |
| 143 if (line.empty()) { | |
| 144 // Skips the empty line. | |
| 145 continue; | |
| 146 } | |
| 147 if (line[0] == L'#' || line[0] == L';') { | |
| 148 // This line is a comment. | |
| 149 continue; | |
| 150 } | |
| 151 if (line[0] == L'[') { | |
| 152 // It is a section header. | |
| 153 current_section = line.substr(1); | |
| 154 size_t end = current_section.rfind(L']'); | |
| 155 if (end != std::wstring::npos) | |
| 156 current_section.erase(end); | |
| 157 } else { | |
| 158 std::wstring key, value; | |
| 159 size_t equal = line.find(L'='); | |
| 160 if (equal != std::wstring::npos) { | |
| 161 key = line.substr(0, equal); | |
| 162 value = line.substr(equal + 1); | |
| 163 // Checks whether the section and key contain a '.' character. | |
| 164 // Those sections and keys break DictionaryValue's path format, | |
| 165 // so we discard them. | |
| 166 if (current_section.find(L'.') == std::wstring::npos && | |
| 167 key.find(L'.') == std::wstring::npos) | |
| 168 root->SetString(current_section + L"." + key, value); | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 bool CanImportURL(const GURL& url) { | |
| 175 const char* kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"}; | |
| 176 | |
| 177 // The URL is not valid. | |
| 178 if (!url.is_valid()) | |
| 179 return false; | |
| 180 | |
| 181 // Filter out the URLs with unsupported schemes. | |
| 182 for (int i = 0; i < arraysize(kInvalidSchemes); ++i) { | |
| 183 if (url.SchemeIs(kInvalidSchemes[i])) | |
| 184 return false; | |
| 185 } | |
| 186 | |
| 187 return true; | |
| 188 } | |
| 189 | |
| 190 void ParseSearchEnginesFromXMLFiles(const std::vector<std::wstring>& xml_files, | |
| 191 std::vector<TemplateURL*>* search_engines) { | |
| 192 DCHECK(search_engines); | |
| 193 | |
| 194 std::map<std::wstring, TemplateURL*> search_engine_for_url; | |
| 195 std::string content; | |
| 196 // The first XML file represents the default search engine in Firefox 3, so we | |
| 197 // need to keep it on top of the list. | |
| 198 TemplateURL* default_turl = NULL; | |
| 199 for (std::vector<std::wstring>::const_iterator iter = xml_files.begin(); | |
| 200 iter != xml_files.end(); ++iter) { | |
| 201 file_util::ReadFileToString(*iter, &content); | |
| 202 TemplateURL* template_url = new TemplateURL(); | |
| 203 FirefoxURLParameterFilter param_filter; | |
| 204 if (TemplateURLParser::Parse( | |
| 205 reinterpret_cast<const unsigned char*>(content.data()), | |
| 206 content.length(), ¶m_filter, template_url) && | |
| 207 template_url->url()) { | |
| 208 std::wstring url = template_url->url()->url(); | |
| 209 std::map<std::wstring, TemplateURL*>::iterator iter = | |
| 210 search_engine_for_url.find(url); | |
| 211 if (iter != search_engine_for_url.end()) { | |
| 212 // We have already found a search engine with the same URL. We give | |
| 213 // priority to the latest one found, as GetSearchEnginesXMLFiles() | |
| 214 // returns a vector with first Firefox default search engines and then | |
| 215 // the user's ones. We want to give priority to the user ones. | |
| 216 delete iter->second; | |
| 217 search_engine_for_url.erase(iter); | |
| 218 } | |
| 219 // Give this a keyword to facilitate tab-to-search, if possible. | |
| 220 template_url->set_keyword(TemplateURLModel::GenerateKeyword(GURL(url), | |
| 221 false)); | |
| 222 template_url->set_show_in_default_list(true); | |
| 223 search_engine_for_url[url] = template_url; | |
| 224 if (!default_turl) | |
| 225 default_turl = template_url; | |
| 226 } else { | |
| 227 delete template_url; | |
| 228 } | |
| 229 content.clear(); | |
| 230 } | |
| 231 | |
| 232 // Put the results in the |search_engines| vector. | |
| 233 std::map<std::wstring, TemplateURL*>::iterator t_iter; | |
| 234 for (t_iter = search_engine_for_url.begin(); | |
| 235 t_iter != search_engine_for_url.end(); ++t_iter) { | |
| 236 if (t_iter->second == default_turl) | |
| 237 search_engines->insert(search_engines->begin(), default_turl); | |
| 238 else | |
| 239 search_engines->push_back(t_iter->second); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 bool ReadPrefFile(const std::wstring& path_name, | |
| 244 const std::wstring& file_name, | |
| 245 std::string* content) { | |
| 246 if (content == NULL) | |
| 247 return false; | |
| 248 | |
| 249 std::wstring file = path_name; | |
| 250 file_util::AppendToPath(&file, file_name.c_str()); | |
| 251 | |
| 252 file_util::ReadFileToString(file, content); | |
| 253 | |
| 254 if (content->empty()) { | |
| 255 NOTREACHED() << L"Firefox preference file " << file_name.c_str() | |
| 256 << L" is empty."; | |
| 257 return false; | |
| 258 } | |
| 259 | |
| 260 return true; | |
| 261 } | |
| 262 | |
| 263 std::string ReadBrowserConfigProp(const std::wstring& app_path, | |
| 264 const std::string& pref_key) { | |
| 265 std::string content; | |
| 266 if (!ReadPrefFile(app_path, L"browserconfig.properties", &content)) | |
| 267 return ""; | |
| 268 | |
| 269 // This file has the syntax: key=value. | |
| 270 size_t prop_index = content.find(pref_key + "="); | |
| 271 if (prop_index == -1) | |
| 272 return ""; | |
| 273 | |
| 274 size_t start = prop_index + pref_key.length(); | |
| 275 size_t stop = -1; | |
| 276 if (start != -1) | |
| 277 stop = content.find("\n", start + 1); | |
| 278 | |
| 279 if (start == -1 || stop == -1 || (start == stop)) { | |
| 280 NOTREACHED() << "Firefox property " << pref_key << " could not be parsed."; | |
| 281 return ""; | |
| 282 } | |
| 283 | |
| 284 return content.substr(start + 1, stop - start - 1); | |
| 285 } | |
| 286 | |
| 287 std::string ReadPrefsJsValue(const std::wstring& profile_path, | |
| 288 const std::string& pref_key) { | |
| 289 std::string content; | |
| 290 if (!ReadPrefFile(profile_path, L"prefs.js", &content)) | |
| 291 return ""; | |
| 292 | |
| 293 // This file has the syntax: user_pref("key", value); | |
| 294 std::string search_for = std::string("user_pref(\"") + pref_key + | |
| 295 std::string("\", "); | |
| 296 size_t prop_index = content.find(search_for); | |
| 297 if (prop_index == -1) | |
| 298 return ""; | |
| 299 | |
| 300 size_t start = prop_index + search_for.length(); | |
| 301 size_t stop = -1; | |
| 302 if (start != -1) | |
| 303 stop = content.find(")", start + 1); | |
| 304 | |
| 305 if (start == -1 || stop == -1) { | |
| 306 NOTREACHED() << "Firefox property " << pref_key << " could not be parsed."; | |
| 307 return ""; | |
| 308 } | |
| 309 | |
| 310 // String values have double quotes we don't need to return to the caller. | |
| 311 if (content[start] == '\"' && content[stop - 1] == '\"') { | |
| 312 ++start; | |
| 313 --stop; | |
| 314 } | |
| 315 | |
| 316 return content.substr(start, stop - start); | |
| 317 } | |
| 318 | |
| 319 int GetFirefoxDefaultSearchEngineIndex( | |
| 320 const std::vector<TemplateURL*>& search_engines, | |
| 321 const std::wstring& profile_path) { | |
| 322 // The default search engine is contained in the file prefs.js found in the | |
| 323 // profile directory. | |
| 324 // It is the "browser.search.selectedEngine" property. | |
| 325 if (search_engines.empty()) | |
| 326 return -1; | |
| 327 | |
| 328 std::wstring default_se_name = UTF8ToWide( | |
| 329 ReadPrefsJsValue(profile_path, "browser.search.selectedEngine")); | |
| 330 | |
| 331 int default_se_index = -1; | |
| 332 for (std::vector<TemplateURL*>::const_iterator iter = search_engines.begin(); | |
| 333 iter != search_engines.end(); ++iter) { | |
| 334 if (default_se_name == (*iter)->short_name()) { | |
| 335 default_se_index = static_cast<int>(iter - search_engines.begin()); | |
| 336 break; | |
| 337 } | |
| 338 } | |
| 339 if (default_se_index == -1) { | |
| 340 NOTREACHED() << | |
| 341 "Firefox default search engine not found in search engine list"; | |
| 342 } | |
| 343 | |
| 344 return default_se_index; | |
| 345 } | |
| 346 | |
| 347 GURL GetHomepage(const std::wstring& profile_path) { | |
| 348 std::string home_page_list = | |
| 349 ReadPrefsJsValue(profile_path, "browser.startup.homepage"); | |
| 350 | |
| 351 size_t seperator = home_page_list.find_first_of('|'); | |
| 352 if (seperator == std::string::npos) | |
| 353 return GURL(home_page_list); | |
| 354 | |
| 355 return GURL(home_page_list.substr(0, seperator)); | |
| 356 } | |
| 357 | |
| 358 bool IsDefaultHomepage(const GURL& homepage, | |
| 359 const std::wstring& app_path) { | |
| 360 if (!homepage.is_valid()) | |
| 361 return false; | |
| 362 | |
| 363 std::string default_homepages = | |
| 364 ReadBrowserConfigProp(app_path, "browser.startup.homepage"); | |
| 365 | |
| 366 size_t seperator = default_homepages.find_first_of('|'); | |
| 367 if (seperator == std::string::npos) | |
| 368 return homepage.spec() == GURL(default_homepages).spec(); | |
| 369 | |
| 370 // Crack the string into separate homepage urls. | |
| 371 std::vector<std::string> urls; | |
| 372 SplitString(default_homepages, '|', &urls); | |
| 373 | |
| 374 for (size_t i = 0; i < urls.size(); ++i) { | |
| 375 if (homepage.spec() == GURL(urls[i]).spec()) | |
| 376 return true; | |
| 377 } | |
| 378 | |
| 379 return false; | |
| 380 } | |
| 381 | |
| 382 // class NSSDecryptor. | |
| 383 | |
| 384 // static | |
| 385 const wchar_t NSSDecryptor::kNSS3Library[] = L"nss3.dll"; | |
| 386 const wchar_t NSSDecryptor::kSoftokn3Library[] = L"softokn3.dll"; | |
| 387 const wchar_t NSSDecryptor::kPLDS4Library[] = L"plds4.dll"; | |
| 388 const wchar_t NSSDecryptor::kNSPR4Library[] = L"nspr4.dll"; | |
| 389 | |
| 390 NSSDecryptor::NSSDecryptor() | |
| 391 : NSS_Init(NULL), NSS_Shutdown(NULL), PK11_GetInternalKeySlot(NULL), | |
| 392 PK11_CheckUserPassword(NULL), PK11_FreeSlot(NULL), | |
| 393 PK11_Authenticate(NULL), PK11SDR_Decrypt(NULL), SECITEM_FreeItem(NULL), | |
| 394 PL_ArenaFinish(NULL), PR_Cleanup(NULL), | |
| 395 nss3_dll_(NULL), softokn3_dll_(NULL), | |
| 396 is_nss_initialized_(false) { | |
| 397 } | |
| 398 | |
| 399 NSSDecryptor::~NSSDecryptor() { | |
| 400 Free(); | |
| 401 } | |
| 402 | |
| 403 bool NSSDecryptor::Init(const std::wstring& dll_path, | |
| 404 const std::wstring& db_path) { | |
| 405 // We call SetDllDirectory to work around a Purify bug (GetModuleHandle | |
| 406 // fails inside Purify under certain conditions). SetDllDirectory only | |
| 407 // exists on Windows XP SP1 or later, so we look up its address at run time. | |
| 408 HMODULE kernel32_dll = GetModuleHandle(L"kernel32.dll"); | |
| 409 if (kernel32_dll == NULL) | |
| 410 return false; | |
| 411 SetDllDirectoryFunc set_dll_directory = | |
| 412 (SetDllDirectoryFunc)GetProcAddress(kernel32_dll, "SetDllDirectoryW"); | |
| 413 SetDllDirectoryCaller caller; | |
| 414 | |
| 415 if (set_dll_directory != NULL) { | |
| 416 if (!set_dll_directory(dll_path.c_str())) | |
| 417 return false; | |
| 418 caller.set_func(set_dll_directory); | |
| 419 nss3_dll_ = LoadLibrary(kNSS3Library); | |
| 420 if (nss3_dll_ == NULL) | |
| 421 return false; | |
| 422 } else { | |
| 423 // Fall back on LoadLibraryEx if SetDllDirectory isn't available. We | |
| 424 // actually prefer this method because it doesn't change the DLL search | |
| 425 // path, which is a process-wide property. | |
| 426 std::wstring path = dll_path; | |
| 427 file_util::AppendToPath(&path, kNSS3Library); | |
| 428 nss3_dll_ = LoadLibraryEx(path.c_str(), NULL, | |
| 429 LOAD_WITH_ALTERED_SEARCH_PATH); | |
| 430 if (nss3_dll_ == NULL) | |
| 431 return false; | |
| 432 | |
| 433 // Firefox 2 uses NSS 3.11. Firefox 3 uses NSS 3.12. NSS 3.12 has two | |
| 434 // changes in its DLLs: | |
| 435 // 1. nss3.dll is not linked with softokn3.dll at build time, but rather | |
| 436 // loads softokn3.dll using LoadLibrary in NSS_Init. | |
| 437 // 2. softokn3.dll has a new dependency sqlite3.dll. | |
| 438 // NSS_Init's LoadLibrary call has trouble finding sqlite3.dll. To help | |
| 439 // it out, we preload softokn3.dll using LoadLibraryEx with the | |
| 440 // LOAD_WITH_ALTERED_SEARCH_PATH flag. This helps because LoadLibrary | |
| 441 // doesn't load a DLL again if it's already loaded. This workaround is | |
| 442 // harmless for NSS 3.11. | |
| 443 path = dll_path; | |
| 444 file_util::AppendToPath(&path, kSoftokn3Library); | |
| 445 softokn3_dll_ = LoadLibraryEx(path.c_str(), NULL, | |
| 446 LOAD_WITH_ALTERED_SEARCH_PATH); | |
| 447 if (softokn3_dll_ == NULL) { | |
| 448 Free(); | |
| 449 return false; | |
| 450 } | |
| 451 } | |
| 452 | |
| 453 // NSPR DLLs are already loaded now. | |
| 454 HMODULE plds4_dll = GetModuleHandle(kPLDS4Library); | |
| 455 HMODULE nspr4_dll = GetModuleHandle(kNSPR4Library); | |
| 456 if (plds4_dll == NULL || nspr4_dll == NULL) { | |
| 457 Free(); | |
| 458 return false; | |
| 459 } | |
| 460 | |
| 461 // Gets the function address. | |
| 462 NSS_Init = (NSSInitFunc)GetProcAddress(nss3_dll_, "NSS_Init"); | |
| 463 NSS_Shutdown = (NSSShutdownFunc)GetProcAddress(nss3_dll_, "NSS_Shutdown"); | |
| 464 PK11_GetInternalKeySlot = (PK11GetInternalKeySlotFunc) | |
| 465 GetProcAddress(nss3_dll_, "PK11_GetInternalKeySlot"); | |
| 466 PK11_FreeSlot = (PK11FreeSlotFunc)GetProcAddress(nss3_dll_, "PK11_FreeSlot"); | |
| 467 PK11_Authenticate = (PK11AuthenticateFunc) | |
| 468 GetProcAddress(nss3_dll_, "PK11_Authenticate"); | |
| 469 PK11SDR_Decrypt = (PK11SDRDecryptFunc) | |
| 470 GetProcAddress(nss3_dll_, "PK11SDR_Decrypt"); | |
| 471 SECITEM_FreeItem = (SECITEMFreeItemFunc) | |
| 472 GetProcAddress(nss3_dll_, "SECITEM_FreeItem"); | |
| 473 PL_ArenaFinish = (PLArenaFinishFunc) | |
| 474 GetProcAddress(plds4_dll, "PL_ArenaFinish"); | |
| 475 PR_Cleanup = (PRCleanupFunc)GetProcAddress(nspr4_dll, "PR_Cleanup"); | |
| 476 | |
| 477 if (NSS_Init == NULL || NSS_Shutdown == NULL || | |
| 478 PK11_GetInternalKeySlot == NULL || PK11_FreeSlot == NULL || | |
| 479 PK11_Authenticate == NULL || PK11SDR_Decrypt == NULL || | |
| 480 SECITEM_FreeItem == NULL || PL_ArenaFinish == NULL || | |
| 481 PR_Cleanup == NULL) { | |
| 482 Free(); | |
| 483 return false; | |
| 484 } | |
| 485 | |
| 486 SECStatus result = NSS_Init(base::SysWideToNativeMB(db_path).c_str()); | |
| 487 if (result != SECSuccess) { | |
| 488 Free(); | |
| 489 return false; | |
| 490 } | |
| 491 | |
| 492 is_nss_initialized_ = true; | |
| 493 return true; | |
| 494 } | |
| 495 | |
| 496 void NSSDecryptor::Free() { | |
| 497 if (is_nss_initialized_) { | |
| 498 NSS_Shutdown(); | |
| 499 PL_ArenaFinish(); | |
| 500 PR_Cleanup(); | |
| 501 is_nss_initialized_ = false; | |
| 502 } | |
| 503 if (softokn3_dll_ != NULL) | |
| 504 FreeLibrary(softokn3_dll_); | |
| 505 softokn3_dll_ = NULL; | |
| 506 if (nss3_dll_ != NULL) | |
| 507 FreeLibrary(nss3_dll_); | |
| 508 nss3_dll_ = NULL; | |
| 509 NSS_Init = NULL; | |
| 510 NSS_Shutdown = NULL; | |
| 511 PK11_GetInternalKeySlot = NULL; | |
| 512 PK11_FreeSlot = NULL; | |
| 513 PK11_Authenticate = NULL; | |
| 514 PK11SDR_Decrypt = NULL; | |
| 515 SECITEM_FreeItem = NULL; | |
| 516 PL_ArenaFinish = NULL; | |
| 517 PR_Cleanup = NULL; | |
| 518 } | |
| 519 | |
| 520 // This method is based on some Firefox code in | |
| 521 // security/manager/ssl/src/nsSDR.cpp | |
| 522 // The license block is: | |
| 523 | |
| 524 /* ***** BEGIN LICENSE BLOCK ***** | |
| 525 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
| 526 * | |
| 527 * The contents of this file are subject to the Mozilla Public License Version | |
| 528 * 1.1 (the "License"); you may not use this file except in compliance with | |
| 529 * the License. You may obtain a copy of the License at | |
| 530 * http://www.mozilla.org/MPL/ | |
| 531 * | |
| 532 * Software distributed under the License is distributed on an "AS IS" basis, | |
| 533 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
| 534 * for the specific language governing rights and limitations under the | |
| 535 * License. | |
| 536 * | |
| 537 * The Original Code is the Netscape security libraries. | |
| 538 * | |
| 539 * The Initial Developer of the Original Code is | |
| 540 * Netscape Communications Corporation. | |
| 541 * Portions created by the Initial Developer are Copyright (C) 1994-2000 | |
| 542 * the Initial Developer. All Rights Reserved. | |
| 543 * | |
| 544 * Contributor(s): | |
| 545 * | |
| 546 * Alternatively, the contents of this file may be used under the terms of | |
| 547 * either the GNU General Public License Version 2 or later (the "GPL"), or | |
| 548 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
| 549 * in which case the provisions of the GPL or the LGPL are applicable instead | |
| 550 * of those above. If you wish to allow use of your version of this file only | |
| 551 * under the terms of either the GPL or the LGPL, and not to allow others to | |
| 552 * use your version of this file under the terms of the MPL, indicate your | |
| 553 * decision by deleting the provisions above and replace them with the notice | |
| 554 * and other provisions required by the GPL or the LGPL. If you do not delete | |
| 555 * the provisions above, a recipient may use your version of this file under | |
| 556 * the terms of any one of the MPL, the GPL or the LGPL. | |
| 557 * | |
| 558 * ***** END LICENSE BLOCK ***** */ | |
| 559 | |
| 560 std::wstring NSSDecryptor::Decrypt(const std::string& crypt) const { | |
| 561 // Do nothing if NSS is not loaded. | |
| 562 if (!nss3_dll_) | |
| 563 return std::wstring(); | |
| 564 | |
| 565 std::string plain; | |
| 566 | |
| 567 // The old style password is encoded in base64. They are identified | |
| 568 // by a leading '~'. Otherwise, we should decrypt the text. | |
| 569 if (crypt[0] != '~') { | |
| 570 std::string decoded_data; | |
| 571 net::Base64Decode(crypt, &decoded_data); | |
| 572 PK11SlotInfo* slot = NULL; | |
| 573 slot = PK11_GetInternalKeySlot(); | |
| 574 SECStatus result = PK11_Authenticate(slot, PR_TRUE, NULL); | |
| 575 if (result != SECSuccess) { | |
| 576 PK11_FreeSlot(slot); | |
| 577 return std::wstring(); | |
| 578 } | |
| 579 | |
| 580 SECItem request; | |
| 581 request.data = reinterpret_cast<unsigned char*>( | |
| 582 const_cast<char*>(decoded_data.data())); | |
| 583 request.len = static_cast<unsigned int>(decoded_data.size()); | |
| 584 SECItem reply; | |
| 585 reply.data = NULL; | |
| 586 reply.len = 0; | |
| 587 result = PK11SDR_Decrypt(&request, &reply, NULL); | |
| 588 if (result == SECSuccess) | |
| 589 plain.assign(reinterpret_cast<char*>(reply.data), reply.len); | |
| 590 | |
| 591 SECITEM_FreeItem(&reply, PR_FALSE); | |
| 592 PK11_FreeSlot(slot); | |
| 593 } else { | |
| 594 // Deletes the leading '~' before decoding. | |
| 595 net::Base64Decode(crypt.substr(1), &plain); | |
| 596 } | |
| 597 | |
| 598 return UTF8ToWide(plain); | |
| 599 } | |
| 600 | |
| 601 // There are three versions of password filess. They store saved user | |
| 602 // names and passwords. | |
| 603 // References: | |
| 604 // http://kb.mozillazine.org/Signons.txt | |
| 605 // http://kb.mozillazine.org/Signons2.txt | |
| 606 // http://kb.mozillazine.org/Signons3.txt | |
| 607 void NSSDecryptor::ParseSignons(const std::string& content, | |
| 608 std::vector<PasswordForm>* forms) { | |
| 609 forms->clear(); | |
| 610 | |
| 611 // Splits the file content into lines. | |
| 612 std::vector<std::string> lines; | |
| 613 SplitString(content, '\n', &lines); | |
| 614 | |
| 615 // The first line is the file version. We skip the unknown versions. | |
| 616 if (lines.empty()) | |
| 617 return; | |
| 618 int version; | |
| 619 if (lines[0] == "#2c") | |
| 620 version = 1; | |
| 621 else if (lines[0] == "#2d") | |
| 622 version = 2; | |
| 623 else if (lines[0] == "#2e") | |
| 624 version = 3; | |
| 625 else | |
| 626 return; | |
| 627 | |
| 628 GURL::Replacements rep; | |
| 629 rep.ClearQuery(); | |
| 630 rep.ClearRef(); | |
| 631 rep.ClearUsername(); | |
| 632 rep.ClearPassword(); | |
| 633 | |
| 634 // Reads never-saved list. Domains are stored one per line. | |
| 635 size_t i; | |
| 636 for (i = 1; i < lines.size() && lines[i].compare(".") != 0; ++i) { | |
| 637 PasswordForm form; | |
| 638 form.origin = GURL(lines[i]).ReplaceComponents(rep); | |
| 639 form.signon_realm = form.origin.GetOrigin().spec(); | |
| 640 form.blacklisted_by_user = true; | |
| 641 forms->push_back(form); | |
| 642 } | |
| 643 ++i; | |
| 644 | |
| 645 // Reads saved passwords. The information is stored in blocks | |
| 646 // seperated by lines that only contain a dot. We find a block | |
| 647 // by the seperator and parse them one by one. | |
| 648 while (i < lines.size()) { | |
| 649 size_t begin = i; | |
| 650 size_t end = i + 1; | |
| 651 while (end < lines.size() && lines[end].compare(".") != 0) | |
| 652 ++end; | |
| 653 i = end + 1; | |
| 654 | |
| 655 // A block has at least five lines. | |
| 656 if (end - begin < 5) | |
| 657 continue; | |
| 658 | |
| 659 PasswordForm form; | |
| 660 | |
| 661 // The first line is the site URL. | |
| 662 // For HTTP authentication logins, the URL may contain http realm, | |
| 663 // which will be in bracket: | |
| 664 // sitename:8080 (realm) | |
| 665 GURL url; | |
| 666 std::string realm; | |
| 667 const char kRealmBracketBegin[] = " ("; | |
| 668 const char kRealmBracketEnd[] = ")"; | |
| 669 if (lines[begin].find(kRealmBracketBegin) != std::string::npos) { | |
| 670 // In this case, the scheme may not exsit. We assume that the | |
| 671 // scheme is HTTP. | |
| 672 if (lines[begin].find("://") == std::string::npos) | |
| 673 lines[begin] = "http://" + lines[begin]; | |
| 674 | |
| 675 size_t start = lines[begin].find(kRealmBracketBegin); | |
| 676 url = GURL(lines[begin].substr(0, start)); | |
| 677 | |
| 678 start += std::string(kRealmBracketBegin).size(); | |
| 679 size_t end = lines[begin].rfind(kRealmBracketEnd); | |
| 680 realm = lines[begin].substr(start, end - start); | |
| 681 } else { | |
| 682 // Don't have http realm. It is the URL that the following passwords | |
| 683 // belong to. | |
| 684 url = GURL(lines[begin]); | |
| 685 } | |
| 686 // Skips this block if the URL is not valid. | |
| 687 if (!url.is_valid()) | |
| 688 continue; | |
| 689 form.origin = url.ReplaceComponents(rep); | |
| 690 form.signon_realm = form.origin.GetOrigin().spec(); | |
| 691 if (!realm.empty()) | |
| 692 form.signon_realm += realm; | |
| 693 form.ssl_valid = form.origin.SchemeIsSecure(); | |
| 694 ++begin; | |
| 695 | |
| 696 // There may be multiple username/password pairs for this site. | |
| 697 // In this case, they are saved in one block without a seperated | |
| 698 // line (contains a dot). | |
| 699 while (begin + 4 < end) { | |
| 700 // The user name. | |
| 701 form.username_element = UTF8ToWide(lines[begin++]); | |
| 702 form.username_value = Decrypt(lines[begin++]); | |
| 703 // The element name has a leading '*'. | |
| 704 if (lines[begin].at(0) == '*') { | |
| 705 form.password_element = UTF8ToWide(lines[begin++].substr(1)); | |
| 706 form.password_value = Decrypt(lines[begin++]); | |
| 707 } else { | |
| 708 // Maybe the file is bad, we skip to next block. | |
| 709 break; | |
| 710 } | |
| 711 // The action attribute from the form element. This line exists | |
| 712 // in versin 2 or above. | |
| 713 if (version >= 2) { | |
| 714 if (begin < end) | |
| 715 form.action = GURL(lines[begin]).ReplaceComponents(rep); | |
| 716 ++begin; | |
| 717 } | |
| 718 // Version 3 has an extra line for further use. | |
| 719 if (version == 3) { | |
| 720 ++begin; | |
| 721 } | |
| 722 | |
| 723 forms->push_back(form); | |
| 724 } | |
| 725 } | |
| 726 } | |
| 727 | |
| OLD | NEW |