OLD | NEW |
1 // Copyright (c) 2009 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/nss_decryptor.h" |
6 | 6 |
7 #include <algorithm> | 7 #include "build/build_config.h" |
8 | 8 |
9 #include "base/file_util.h" | 9 #if defined(OS_LINUX) |
10 #include "base/logging.h" | 10 #include <pk11pub.h> |
| 11 #include <pk11sdr.h> |
| 12 #endif // defined(OS_LINUX) |
| 13 |
11 #include "base/string_util.h" | 14 #include "base/string_util.h" |
12 #include "base/sys_string_conversions.h" | |
13 #include "base/values.h" | |
14 #include "chrome/browser/search_engines/template_url.h" | |
15 #include "chrome/browser/search_engines/template_url_model.h" | |
16 #include "chrome/browser/search_engines/template_url_parser.h" | |
17 #include "googleurl/src/gurl.h" | |
18 #include "net/base/base64.h" | 15 #include "net/base/base64.h" |
19 #include "webkit/glue/password_form.h" | 16 #include "webkit/glue/password_form.h" |
20 | 17 |
21 using webkit_glue::PasswordForm; | 18 using webkit_glue::PasswordForm; |
22 | 19 |
23 namespace { | |
24 | |
25 // FirefoxURLParameterFilter is used to remove parameter mentioning Firefox from | |
26 // the search URL when importing search engines. | |
27 class FirefoxURLParameterFilter : public TemplateURLParser::ParameterFilter { | |
28 public: | |
29 FirefoxURLParameterFilter() { } | |
30 ~FirefoxURLParameterFilter() { } | |
31 | |
32 // TemplateURLParser::ParameterFilter method. | |
33 virtual bool KeepParameter(const std::string& key, | |
34 const std::string& value) { | |
35 std::string low_value = StringToLowerASCII(value); | |
36 if (low_value.find("mozilla") != std::string::npos || | |
37 low_value.find("firefox") != std::string::npos || | |
38 low_value.find("moz:") != std::string::npos ) | |
39 return false; | |
40 return true; | |
41 } | |
42 | |
43 private: | |
44 DISALLOW_EVIL_CONSTRUCTORS(FirefoxURLParameterFilter); | |
45 }; | |
46 } // namespace | |
47 | |
48 bool GetFirefoxVersionAndPathFromProfile(const std::wstring& profile_path, | |
49 int* version, | |
50 std::wstring* app_path) { | |
51 bool ret = false; | |
52 std::wstring compatibility_file(profile_path); | |
53 file_util::AppendToPath(&compatibility_file, L"compatibility.ini"); | |
54 std::string content; | |
55 file_util::ReadFileToString(compatibility_file, &content); | |
56 ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n"); | |
57 std::vector<std::string> lines; | |
58 SplitString(content, '\n', &lines); | |
59 | |
60 for (size_t i = 0; i < lines.size(); ++i) { | |
61 const std::string& line = lines[i]; | |
62 if (line.empty() || line[0] == '#' || line[0] == ';') | |
63 continue; | |
64 size_t equal = line.find('='); | |
65 if (equal != std::string::npos) { | |
66 std::string key = line.substr(0, equal); | |
67 if (key == "LastVersion") { | |
68 *version = line.substr(equal + 1)[0] - '0'; | |
69 ret = true; | |
70 } else if (key == "LastAppDir") { | |
71 *app_path = UTF8ToWide(line.substr(equal + 1)); | |
72 } | |
73 } | |
74 } | |
75 return ret; | |
76 } | |
77 | |
78 void ParseProfileINI(std::wstring file, DictionaryValue* root) { | |
79 // Reads the whole INI file. | |
80 std::string content; | |
81 file_util::ReadFileToString(file, &content); | |
82 ReplaceSubstringsAfterOffset(&content, 0, "\r\n", "\n"); | |
83 std::vector<std::string> lines; | |
84 SplitString(content, '\n', &lines); | |
85 | |
86 // Parses the file. | |
87 root->Clear(); | |
88 std::wstring current_section; | |
89 for (size_t i = 0; i < lines.size(); ++i) { | |
90 std::wstring line = UTF8ToWide(lines[i]); | |
91 if (line.empty()) { | |
92 // Skips the empty line. | |
93 continue; | |
94 } | |
95 if (line[0] == L'#' || line[0] == L';') { | |
96 // This line is a comment. | |
97 continue; | |
98 } | |
99 if (line[0] == L'[') { | |
100 // It is a section header. | |
101 current_section = line.substr(1); | |
102 size_t end = current_section.rfind(L']'); | |
103 if (end != std::wstring::npos) | |
104 current_section.erase(end); | |
105 } else { | |
106 std::wstring key, value; | |
107 size_t equal = line.find(L'='); | |
108 if (equal != std::wstring::npos) { | |
109 key = line.substr(0, equal); | |
110 value = line.substr(equal + 1); | |
111 // Checks whether the section and key contain a '.' character. | |
112 // Those sections and keys break DictionaryValue's path format, | |
113 // so we discard them. | |
114 if (current_section.find(L'.') == std::wstring::npos && | |
115 key.find(L'.') == std::wstring::npos) | |
116 root->SetString(current_section + L"." + key, value); | |
117 } | |
118 } | |
119 } | |
120 } | |
121 | |
122 bool CanImportURL(const GURL& url) { | |
123 const char* kInvalidSchemes[] = {"wyciwyg", "place", "about", "chrome"}; | |
124 | |
125 // The URL is not valid. | |
126 if (!url.is_valid()) | |
127 return false; | |
128 | |
129 // Filter out the URLs with unsupported schemes. | |
130 for (size_t i = 0; i < arraysize(kInvalidSchemes); ++i) { | |
131 if (url.SchemeIs(kInvalidSchemes[i])) | |
132 return false; | |
133 } | |
134 | |
135 return true; | |
136 } | |
137 | |
138 void ParseSearchEnginesFromXMLFiles(const std::vector<std::wstring>& xml_files, | |
139 std::vector<TemplateURL*>* search_engines) { | |
140 DCHECK(search_engines); | |
141 | |
142 std::map<std::wstring, TemplateURL*> search_engine_for_url; | |
143 std::string content; | |
144 // The first XML file represents the default search engine in Firefox 3, so we | |
145 // need to keep it on top of the list. | |
146 TemplateURL* default_turl = NULL; | |
147 for (std::vector<std::wstring>::const_iterator file_iter = xml_files.begin(); | |
148 file_iter != xml_files.end(); ++file_iter) { | |
149 file_util::ReadFileToString(*file_iter, &content); | |
150 TemplateURL* template_url = new TemplateURL(); | |
151 FirefoxURLParameterFilter param_filter; | |
152 if (TemplateURLParser::Parse( | |
153 reinterpret_cast<const unsigned char*>(content.data()), | |
154 content.length(), ¶m_filter, template_url) && | |
155 template_url->url()) { | |
156 std::wstring url = template_url->url()->url(); | |
157 std::map<std::wstring, TemplateURL*>::iterator iter = | |
158 search_engine_for_url.find(url); | |
159 if (iter != search_engine_for_url.end()) { | |
160 // We have already found a search engine with the same URL. We give | |
161 // priority to the latest one found, as GetSearchEnginesXMLFiles() | |
162 // returns a vector with first Firefox default search engines and then | |
163 // the user's ones. We want to give priority to the user ones. | |
164 delete iter->second; | |
165 search_engine_for_url.erase(iter); | |
166 } | |
167 // Give this a keyword to facilitate tab-to-search, if possible. | |
168 template_url->set_keyword( | |
169 TemplateURLModel::GenerateKeyword(GURL(WideToUTF8(url)), false)); | |
170 template_url->set_show_in_default_list(true); | |
171 search_engine_for_url[url] = template_url; | |
172 if (!default_turl) | |
173 default_turl = template_url; | |
174 } else { | |
175 delete template_url; | |
176 } | |
177 content.clear(); | |
178 } | |
179 | |
180 // Put the results in the |search_engines| vector. | |
181 std::map<std::wstring, TemplateURL*>::iterator t_iter; | |
182 for (t_iter = search_engine_for_url.begin(); | |
183 t_iter != search_engine_for_url.end(); ++t_iter) { | |
184 if (t_iter->second == default_turl) | |
185 search_engines->insert(search_engines->begin(), default_turl); | |
186 else | |
187 search_engines->push_back(t_iter->second); | |
188 } | |
189 } | |
190 | |
191 bool ReadPrefFile(const std::wstring& path_name, | |
192 const std::wstring& file_name, | |
193 std::string* content) { | |
194 if (content == NULL) | |
195 return false; | |
196 | |
197 std::wstring file = path_name; | |
198 file_util::AppendToPath(&file, file_name.c_str()); | |
199 | |
200 file_util::ReadFileToString(file, content); | |
201 | |
202 if (content->empty()) { | |
203 NOTREACHED() << L"Firefox preference file " << file_name.c_str() | |
204 << L" is empty."; | |
205 return false; | |
206 } | |
207 | |
208 return true; | |
209 } | |
210 | |
211 std::string ReadBrowserConfigProp(const std::wstring& app_path, | |
212 const std::string& pref_key) { | |
213 std::string content; | |
214 if (!ReadPrefFile(app_path, L"browserconfig.properties", &content)) | |
215 return ""; | |
216 | |
217 // This file has the syntax: key=value. | |
218 size_t prop_index = content.find(pref_key + "="); | |
219 if (prop_index == std::string::npos) | |
220 return ""; | |
221 | |
222 size_t start = prop_index + pref_key.length(); | |
223 size_t stop = std::string::npos; | |
224 if (start != std::string::npos) | |
225 stop = content.find("\n", start + 1); | |
226 | |
227 if (start == std::string::npos || | |
228 stop == std::string::npos || (start == stop)) { | |
229 NOTREACHED() << "Firefox property " << pref_key << " could not be parsed."; | |
230 return ""; | |
231 } | |
232 | |
233 return content.substr(start + 1, stop - start - 1); | |
234 } | |
235 | |
236 std::string ReadPrefsJsValue(const std::wstring& profile_path, | |
237 const std::string& pref_key) { | |
238 std::string content; | |
239 if (!ReadPrefFile(profile_path, L"prefs.js", &content)) | |
240 return ""; | |
241 | |
242 // This file has the syntax: user_pref("key", value); | |
243 std::string search_for = std::string("user_pref(\"") + pref_key + | |
244 std::string("\", "); | |
245 size_t prop_index = content.find(search_for); | |
246 if (prop_index == std::string::npos) | |
247 return ""; | |
248 | |
249 size_t start = prop_index + search_for.length(); | |
250 size_t stop = std::string::npos; | |
251 if (start != std::string::npos) | |
252 stop = content.find(")", start + 1); | |
253 | |
254 if (start == std::string::npos || stop == std::string::npos) { | |
255 NOTREACHED() << "Firefox property " << pref_key << " could not be parsed."; | |
256 return ""; | |
257 } | |
258 | |
259 // String values have double quotes we don't need to return to the caller. | |
260 if (content[start] == '\"' && content[stop - 1] == '\"') { | |
261 ++start; | |
262 --stop; | |
263 } | |
264 | |
265 return content.substr(start, stop - start); | |
266 } | |
267 | |
268 int GetFirefoxDefaultSearchEngineIndex( | |
269 const std::vector<TemplateURL*>& search_engines, | |
270 const std::wstring& profile_path) { | |
271 // The default search engine is contained in the file prefs.js found in the | |
272 // profile directory. | |
273 // It is the "browser.search.selectedEngine" property. | |
274 if (search_engines.empty()) | |
275 return -1; | |
276 | |
277 std::wstring default_se_name = UTF8ToWide( | |
278 ReadPrefsJsValue(profile_path, "browser.search.selectedEngine")); | |
279 | |
280 if (default_se_name.empty()) { | |
281 // browser.search.selectedEngine does not exist if the user has not changed | |
282 // from the default (or has selected the default). | |
283 // TODO: should fallback to 'browser.search.defaultengine' if selectedEngine | |
284 // is empty. | |
285 return -1; | |
286 } | |
287 | |
288 int default_se_index = -1; | |
289 for (std::vector<TemplateURL*>::const_iterator iter = search_engines.begin(); | |
290 iter != search_engines.end(); ++iter) { | |
291 if (default_se_name == (*iter)->short_name()) { | |
292 default_se_index = static_cast<int>(iter - search_engines.begin()); | |
293 break; | |
294 } | |
295 } | |
296 if (default_se_index == -1) { | |
297 NOTREACHED() << | |
298 "Firefox default search engine not found in search engine list"; | |
299 } | |
300 | |
301 return default_se_index; | |
302 } | |
303 | |
304 GURL GetHomepage(const std::wstring& profile_path) { | |
305 std::string home_page_list = | |
306 ReadPrefsJsValue(profile_path, "browser.startup.homepage"); | |
307 | |
308 size_t seperator = home_page_list.find_first_of('|'); | |
309 if (seperator == std::string::npos) | |
310 return GURL(home_page_list); | |
311 | |
312 return GURL(home_page_list.substr(0, seperator)); | |
313 } | |
314 | |
315 bool IsDefaultHomepage(const GURL& homepage, | |
316 const std::wstring& app_path) { | |
317 if (!homepage.is_valid()) | |
318 return false; | |
319 | |
320 std::string default_homepages = | |
321 ReadBrowserConfigProp(app_path, "browser.startup.homepage"); | |
322 | |
323 size_t seperator = default_homepages.find_first_of('|'); | |
324 if (seperator == std::string::npos) | |
325 return homepage.spec() == GURL(default_homepages).spec(); | |
326 | |
327 // Crack the string into separate homepage urls. | |
328 std::vector<std::string> urls; | |
329 SplitString(default_homepages, '|', &urls); | |
330 | |
331 for (size_t i = 0; i < urls.size(); ++i) { | |
332 if (homepage.spec() == GURL(urls[i]).spec()) | |
333 return true; | |
334 } | |
335 | |
336 return false; | |
337 } | |
338 | |
339 // class NSSDecryptor. | |
340 | |
341 NSSDecryptor::NSSDecryptor() | |
342 : NSS_Init(NULL), NSS_Shutdown(NULL), PK11_GetInternalKeySlot(NULL), | |
343 PK11_CheckUserPassword(NULL), PK11_FreeSlot(NULL), | |
344 PK11_Authenticate(NULL), PK11SDR_Decrypt(NULL), SECITEM_FreeItem(NULL), | |
345 PL_ArenaFinish(NULL), PR_Cleanup(NULL), | |
346 nss3_dll_(NULL), softokn3_dll_(NULL), | |
347 is_nss_initialized_(false) { | |
348 } | |
349 | |
350 NSSDecryptor::~NSSDecryptor() { | |
351 Free(); | |
352 } | |
353 | |
354 bool NSSDecryptor::InitNSS(const std::wstring& db_path, | |
355 base::NativeLibrary plds4_dll, | |
356 base::NativeLibrary nspr4_dll) { | |
357 // NSPR DLLs are already loaded now. | |
358 if (plds4_dll == NULL || nspr4_dll == NULL) { | |
359 Free(); | |
360 return false; | |
361 } | |
362 | |
363 // Gets the function address. | |
364 NSS_Init = (NSSInitFunc) | |
365 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "NSS_Init"); | |
366 NSS_Shutdown = (NSSShutdownFunc) | |
367 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "NSS_Shutdown"); | |
368 PK11_GetInternalKeySlot = (PK11GetInternalKeySlotFunc) | |
369 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, | |
370 "PK11_GetInternalKeySlot"); | |
371 PK11_FreeSlot = (PK11FreeSlotFunc) | |
372 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11_FreeSlot"); | |
373 PK11_Authenticate = (PK11AuthenticateFunc) | |
374 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11_Authenticate"); | |
375 PK11SDR_Decrypt = (PK11SDRDecryptFunc) | |
376 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "PK11SDR_Decrypt"); | |
377 SECITEM_FreeItem = (SECITEMFreeItemFunc) | |
378 base::GetFunctionPointerFromNativeLibrary(nss3_dll_, "SECITEM_FreeItem"); | |
379 PL_ArenaFinish = (PLArenaFinishFunc) | |
380 base::GetFunctionPointerFromNativeLibrary(plds4_dll, "PL_ArenaFinish"); | |
381 PR_Cleanup = (PRCleanupFunc) | |
382 base::GetFunctionPointerFromNativeLibrary(nspr4_dll, "PR_Cleanup"); | |
383 | |
384 if (NSS_Init == NULL || NSS_Shutdown == NULL || | |
385 PK11_GetInternalKeySlot == NULL || PK11_FreeSlot == NULL || | |
386 PK11_Authenticate == NULL || PK11SDR_Decrypt == NULL || | |
387 SECITEM_FreeItem == NULL || PL_ArenaFinish == NULL || | |
388 PR_Cleanup == NULL) { | |
389 Free(); | |
390 return false; | |
391 } | |
392 | |
393 SECStatus result = NSS_Init(base::SysWideToNativeMB(db_path).c_str()); | |
394 if (result != SECSuccess) { | |
395 Free(); | |
396 return false; | |
397 } | |
398 | |
399 is_nss_initialized_ = true; | |
400 return true; | |
401 } | |
402 | |
403 void NSSDecryptor::Free() { | |
404 if (is_nss_initialized_) { | |
405 NSS_Shutdown(); | |
406 PL_ArenaFinish(); | |
407 PR_Cleanup(); | |
408 is_nss_initialized_ = false; | |
409 } | |
410 if (softokn3_dll_ != NULL) | |
411 base::UnloadNativeLibrary(softokn3_dll_); | |
412 if (nss3_dll_ != NULL) | |
413 base::UnloadNativeLibrary(nss3_dll_); | |
414 NSS_Init = NULL; | |
415 NSS_Shutdown = NULL; | |
416 PK11_GetInternalKeySlot = NULL; | |
417 PK11_FreeSlot = NULL; | |
418 PK11_Authenticate = NULL; | |
419 PK11SDR_Decrypt = NULL; | |
420 SECITEM_FreeItem = NULL; | |
421 PL_ArenaFinish = NULL; | |
422 PR_Cleanup = NULL; | |
423 nss3_dll_ = NULL; | |
424 softokn3_dll_ = NULL; | |
425 } | |
426 | |
427 // This method is based on some Firefox code in | 20 // This method is based on some Firefox code in |
428 // security/manager/ssl/src/nsSDR.cpp | 21 // security/manager/ssl/src/nsSDR.cpp |
429 // The license block is: | 22 // The license block is: |
430 | 23 |
431 /* ***** BEGIN LICENSE BLOCK ***** | 24 /* ***** BEGIN LICENSE BLOCK ***** |
432 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 25 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
433 * | 26 * |
434 * The contents of this file are subject to the Mozilla Public License Version | 27 * The contents of this file are subject to the Mozilla Public License Version |
435 * 1.1 (the "License"); you may not use this file except in compliance with | 28 * 1.1 (the "License"); you may not use this file except in compliance with |
436 * the License. You may obtain a copy of the License at | 29 * the License. You may obtain a copy of the License at |
(...skipping 22 matching lines...) Loading... |
459 * use your version of this file under the terms of the MPL, indicate your | 52 * use your version of this file under the terms of the MPL, indicate your |
460 * decision by deleting the provisions above and replace them with the notice | 53 * decision by deleting the provisions above and replace them with the notice |
461 * and other provisions required by the GPL or the LGPL. If you do not delete | 54 * and other provisions required by the GPL or the LGPL. If you do not delete |
462 * the provisions above, a recipient may use your version of this file under | 55 * the provisions above, a recipient may use your version of this file under |
463 * the terms of any one of the MPL, the GPL or the LGPL. | 56 * the terms of any one of the MPL, the GPL or the LGPL. |
464 * | 57 * |
465 * ***** END LICENSE BLOCK ***** */ | 58 * ***** END LICENSE BLOCK ***** */ |
466 | 59 |
467 std::wstring NSSDecryptor::Decrypt(const std::string& crypt) const { | 60 std::wstring NSSDecryptor::Decrypt(const std::string& crypt) const { |
468 // Do nothing if NSS is not loaded. | 61 // Do nothing if NSS is not loaded. |
469 if (!nss3_dll_) | 62 if (!is_nss_initialized_) |
470 return std::wstring(); | 63 return std::wstring(); |
471 | 64 |
472 // The old style password is encoded in base64. They are identified | 65 // The old style password is encoded in base64. They are identified |
473 // by a leading '~'. Otherwise, we should decrypt the text. | 66 // by a leading '~'. Otherwise, we should decrypt the text. |
474 std::string plain; | 67 std::string plain; |
475 if (crypt[0] != '~') { | 68 if (crypt[0] != '~') { |
476 std::string decoded_data; | 69 std::string decoded_data; |
477 net::Base64Decode(crypt, &decoded_data); | 70 net::Base64Decode(crypt, &decoded_data); |
478 PK11SlotInfo* slot = NULL; | 71 PK11SlotInfo* slot = NULL; |
479 slot = PK11_GetInternalKeySlot(); | 72 slot = PK11_GetInternalKeySlot(); |
(...skipping 143 matching lines...) Loading... |
623 } | 216 } |
624 // Version 3 has an extra line for further use. | 217 // Version 3 has an extra line for further use. |
625 if (version == 3) { | 218 if (version == 3) { |
626 ++begin; | 219 ++begin; |
627 } | 220 } |
628 | 221 |
629 forms->push_back(form); | 222 forms->push_back(form); |
630 } | 223 } |
631 } | 224 } |
632 } | 225 } |
OLD | NEW |