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

Side by Side Diff: setup/setup_files.cc

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 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 | « setup/setup_files.h ('k') | setup/setup_files_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2007-2009 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15
16 #include "omaha/setup/setup_files.h"
17
18 #include <atlpath.h>
19 #include <vector>
20 #include "base/basictypes.h"
21 #include "omaha/base/app_util.h"
22 #include "omaha/base/debug.h"
23 #include "omaha/base/error.h"
24 #include "omaha/base/file.h"
25 #include "omaha/base/highres_timer-win32.h"
26 #include "omaha/base/logging.h"
27 #include "omaha/base/omaha_version.h"
28 #include "omaha/base/path.h"
29 #include "omaha/base/reg_key.h"
30 #include "omaha/base/scoped_any.h"
31 #include "omaha/base/scoped_current_directory.h"
32 #include "omaha/base/signatures.h"
33 #include "omaha/base/signaturevalidator.h"
34 #include "omaha/base/utils.h"
35 #include "omaha/base/vistautil.h"
36 #include "omaha/common/config_manager.h"
37 #include "omaha/common/const_goopdate.h"
38 #include "omaha/common/goopdate_utils.h"
39 #include "omaha/goopdate/resource_manager.h"
40 #include "omaha/setup/setup_metrics.h"
41
42 namespace omaha {
43
44 namespace {
45
46 const int kNumberOfCreateServiceRetries = 5;
47 const int kSleepBetweenCreateServiceRetryMs = 200;
48
49 } // namespace
50
51 SetupFiles::SetupFiles(bool is_machine)
52 : is_machine_(is_machine) {
53 SETUP_LOG(L2, (_T("[SetupFiles::SetupFiles]")));
54 }
55
56 SetupFiles::~SetupFiles() {
57 SETUP_LOG(L2, (_T("[SetupFiles::~SetupFiles]")));
58
59 if (!saved_shell_path_.IsEmpty()) {
60 // Delete the saved copy of the previous shell.
61 VERIFY1(SUCCEEDED(File::Remove(saved_shell_path_)));
62 }
63 }
64
65 HRESULT SetupFiles::Init() {
66 SETUP_LOG(L2, (_T("[SetupFiles::Init]")));
67
68 HRESULT hr = BuildFileLists();
69 if (FAILED(hr)) {
70 return hr;
71 }
72
73 return S_OK;
74 }
75
76 // We only do these checks for the same exact version. This is especially true
77 // when doing file comparisons, because the filenames as well as the number of
78 // files can change from version to version. An earlier version should not
79 // overinstall a newer version by mistake because it is checking for files that
80 // no longer exist in the new version.
81 bool SetupFiles::ShouldOverinstallSameVersion() {
82 SETUP_LOG(L2, (_T("[SetupFiles::ShouldOverinstallSameVersion]")));
83
84 CPath install_dir = goopdate_utils::BuildInstallDirectory(is_machine_,
85 GetVersionString());
86 for (size_t i = 0 ; i < core_program_files_.size(); ++i) {
87 CString full_path = ConcatenatePath(install_dir, core_program_files_[i]);
88 if (full_path.IsEmpty()) {
89 ASSERT1(false);
90 return true;
91 }
92 if (!File::Exists(full_path)) {
93 SETUP_LOG(L2, (_T("[core file missing - overinstall][%s]]"), full_path));
94 return true;
95 }
96 }
97
98 for (size_t i = 0 ; i < optional_files_.size(); ++i) {
99 CString full_path = ConcatenatePath(install_dir, optional_files_[i]);
100 if (full_path.IsEmpty()) {
101 ASSERT1(false);
102 return true;
103 }
104 if (!File::Exists(full_path)) {
105 SETUP_LOG(L2, (_T("[optional file missing - overinstall][%s]]"),
106 full_path));
107 return true;
108 }
109 }
110
111 CString shell_path = goopdate_utils::BuildGoogleUpdateExePath(is_machine_);
112 if (!File::Exists(shell_path)) {
113 SETUP_LOG(L2, (_T("[shell missing - overinstall][%s]]"), shell_path));
114 return true;
115 }
116
117 return false;
118 }
119
120 // Install the required and optional files.
121 // Assumes that the user already has the appropriate permissions
122 // (e.g. is elevated for a machine install).
123 // Assumes ShouldInstall has been called and returned true.
124 // Assumes no other instances of GoogleUpdate.exe are running.
125 HRESULT SetupFiles::Install() {
126 OPT_LOG(L1, (_T("[Install files]")));
127 ASSERT1(vista_util::IsUserAdmin() || !is_machine_);
128
129 ++metric_setup_files_total;
130 HighresTimer metrics_timer;
131
132 const bool should_over_install = ConfigManager::Instance()->CanOverInstall();
133
134 // Copy the core program files.
135 CPath install_dir = goopdate_utils::BuildInstallDirectory(is_machine_,
136 GetVersionString());
137 HRESULT hr = CopyInstallFiles(core_program_files_,
138 install_dir,
139 should_over_install);
140 if (FAILED(hr)) {
141 OPT_LOG(LEVEL_ERROR, (_T("[Failed to copy the files][0x%08x]"), hr));
142 if (E_ACCESSDENIED == hr) {
143 return GOOPDATE_E_ACCESSDENIED_COPYING_CORE_FILES;
144 }
145 return hr;
146 }
147
148 hr = CopyShell();
149 if (FAILED(hr)) {
150 OPT_LOG(LEVEL_ERROR, (_T("[Failed to copy shell][0x%08x]"), hr));
151 if (E_ACCESSDENIED == hr) {
152 return GOOPDATE_E_ACCESSDENIED_COPYING_SHELL;
153 }
154 return hr;
155 }
156
157 // Copy the optional files.
158 VERIFY1(SUCCEEDED(CopyInstallFiles(optional_files_,
159 install_dir,
160 should_over_install)));
161
162 metric_setup_files_ms.AddSample(metrics_timer.GetElapsedMs());
163 ++metric_setup_files_verification_succeeded;
164 return S_OK;
165 }
166
167 // Currently only rolls back the shell file.
168 HRESULT SetupFiles::RollBack() {
169 OPT_LOG(L1, (_T("[Roll back files]")));
170 ++metric_setup_rollback_files;
171
172 if (!saved_shell_path_.IsEmpty()) {
173 SETUP_LOG(L1, (_T("[Rolling back shell from %s]"), saved_shell_path_));
174 ++metric_setup_files_rollback_shell;
175
176 std::vector<CString> saved_paths;
177 saved_paths.push_back(saved_shell_path_);
178 std::vector<CString> install_paths;
179 install_paths.push_back(
180 goopdate_utils::BuildGoogleUpdateExePath(is_machine_));
181
182 HRESULT hr = CopyAndValidateFiles(saved_paths, install_paths, true);
183 if (FAILED(hr)) {
184 SETUP_LOG(LE, (_T("[CopyAndValidateFiles failed][0x%08x]"), hr));
185 return hr;
186 }
187 }
188
189 return S_OK;
190 }
191
192 void SetupFiles::Uninstall() {
193 SETUP_LOG(L2, (_T("[SetupFiles::Uninstall]")));
194
195 // In case we are deleting the current directory as well, let's reset the
196 // current directory to a temporary directory. On exit, we'll try to restore
197 // the directory (if it still exists).
198 scoped_current_directory root_dir(app_util::GetTempDir());
199
200 // Delete the install and crash reports directories.
201 CString install_dir(
202 is_machine_ ? ConfigManager::Instance()->GetMachineGoopdateInstallDir() :
203 ConfigManager::Instance()->GetUserGoopdateInstallDir());
204 HRESULT hr = DeleteDirectory(install_dir);
205 if (FAILED(hr)) {
206 SETUP_LOG(LE, (_T("[DeleteDirectory failed][%s][0x%08x]"),
207 install_dir, hr));
208 }
209 }
210
211 HRESULT SetupFiles::CopyShell() {
212 bool should_copy = false;
213 bool already_exists = false;
214 CString shell_path = goopdate_utils::BuildGoogleUpdateExePath(is_machine_);
215
216 HRESULT hr = ShouldCopyShell(shell_path,
217 &should_copy,
218 &already_exists);
219 if (FAILED(hr)) {
220 SETUP_LOG(LE, (_T("[ShouldCopyShell failed][0x%08x]"), hr));
221 return hr;
222 }
223
224 if (should_copy) {
225 if (already_exists) {
226 ++metric_setup_files_replace_shell;
227 VERIFY1(SUCCEEDED(SaveShellForRollback(shell_path)));
228 }
229
230 std::vector<CString> shell_files;
231 shell_files.push_back(kOmahaShellFileName);
232 CPath shell_dir(shell_path);
233 VERIFY1(shell_dir.RemoveFileSpec());
234 hr = CopyInstallFiles(shell_files, shell_dir, already_exists);
235 if (FAILED(hr)) {
236 SETUP_LOG(LE, (_T("[CopyInstallFiles of shell failed][0x%08x]"), hr));
237 // TODO(omaha): If a shell already exists, we could try using the
238 // existing one, but that may lead to unexpected behavior.
239 return hr;
240 }
241 }
242
243 return S_OK;
244 }
245
246 HRESULT SetupFiles::ShouldCopyShell(const CString& shell_install_path,
247 bool* should_copy,
248 bool* already_exists) const {
249 ASSERT1(should_copy);
250 ASSERT1(already_exists);
251 *should_copy = false;
252 *already_exists = false;
253
254 CPath source_shell_path(app_util::GetCurrentModuleDirectory());
255 if (!source_shell_path.Append(kOmahaShellFileName)) {
256 return GOOPDATE_E_PATH_APPEND_FAILED;
257 }
258
259 if (!File::Exists(shell_install_path)) {
260 SETUP_LOG(L3, (_T("[shell does not exist - copying]")));
261 *should_copy = true;
262 return S_OK;
263 }
264 *already_exists = true;
265
266 ULONGLONG existing_version = app_util::GetVersionFromFile(shell_install_path);
267 if (!existing_version) {
268 ASSERT(false, (_T("[failed to get existing shell version - replacing]")));
269 *should_copy = true;
270 return S_OK;
271 }
272
273 ULONGLONG source_version = app_util::GetVersionFromFile(source_shell_path);
274 if (!source_version) {
275 ASSERT(false, (_T("[failed to get this shell version - not replacing]")));
276 *should_copy = false;
277 return E_FAIL;
278 }
279
280 if (existing_version > source_version) {
281 SETUP_LOG(L2, (_T("[newer shell version exists - not copying]")));
282 *should_copy = false;
283 } else if (existing_version < source_version) {
284 if (IsOlderShellVersionCompatible(existing_version)) {
285 SETUP_LOG(L2, (_T("[compatible shell version exists - not copying]")));
286 *should_copy = false;
287 } else {
288 SETUP_LOG(L2, (_T("[older shell version exists - copying]")));
289 *should_copy = true;
290 }
291 } else {
292 // Same version.
293 *should_copy = ConfigManager::Instance()->CanOverInstall();
294 SETUP_LOG(L2, (_T("[same version exists - %s copying]"),
295 *should_copy ? _T("") : _T("not")));
296 }
297
298 return S_OK;
299 }
300
301 HRESULT SetupFiles::SaveShellForRollback(const CString& shell_install_path) {
302 // Copy existing file to a temporary file in case we need to roll back.
303 CString temp_file;
304 if (!::GetTempFileName(app_util::GetTempDir(),
305 _T("gsh"),
306 0,
307 CStrBuf(temp_file, MAX_PATH))) {
308 const DWORD error = ::GetLastError();
309 SETUP_LOG(LEVEL_WARNING, (_T("[::GetTempFileName failed][%d]"), error));
310 return HRESULT_FROM_WIN32(error);
311 }
312
313 HRESULT hr = File::Copy(shell_install_path, temp_file, true);
314 if (FAILED(hr)) {
315 return hr;
316 }
317
318 saved_shell_path_ = temp_file;
319 return S_OK;
320 }
321
322 // The list of files below needs to be kept in sync with payload_files in
323 // omaha_version_utils.py.
324 HRESULT SetupFiles::BuildFileLists() {
325 ASSERT1(core_program_files_.empty());
326 ASSERT1(optional_files_.empty());
327
328 core_program_files_.clear();
329 core_program_files_.push_back(kOmahaShellFileName);
330 core_program_files_.push_back(kOmahaDllName);
331 core_program_files_.push_back(kCrashHandlerFileName);
332
333 // TODO(omaha3): Try to not depend on ResourceManager. Maybe just find the
334 // files using wildcards.
335 ResourceManager::GetSupportedLanguageDllNames(&core_program_files_);
336
337 core_program_files_.push_back(kHelperInstallerName);
338
339 core_program_files_.push_back(kPSFileNameUser);
340 core_program_files_.push_back(kPSFileNameMachine);
341
342 // If files are removed from this list, unit tests such as
343 // ShouldInstall_SameVersionOptionalFileMissing may need to be updated.
344 optional_files_.clear();
345 optional_files_.push_back(UPDATE_PLUGIN_FILENAME);
346 optional_files_.push_back(kOmahaBrokerFileName);
347 optional_files_.push_back(kOmahaOnDemandFileName);
348 // Machine-specific files are always installed, to support cross installs from
349 // user to machine and machine to user.
350 // TODO(omaha3): Enable once it is being built.
351 #if 0
352 optional_files_.push_back(BHO_FILENAME);
353 #endif
354
355 return S_OK;
356 }
357
358 // Assumes that an install is needed.
359 HRESULT SetupFiles::CopyInstallFiles(const std::vector<CString>& file_names,
360 const CString& destination_dir,
361 bool overwrite) {
362 SETUP_LOG(L1, (_T("[SetupFiles::CopyInstallFiles]")
363 _T("[destination dir=%s][overwrite=%d]"),
364 destination_dir, overwrite));
365 ASSERT1(!file_names.empty());
366
367 CPath source_dir(app_util::GetCurrentModuleDirectory());
368 SETUP_LOG(L2, (_T("[source_dir=%s]"),
369 static_cast<const TCHAR*>(source_dir)));
370
371 if (!File::Exists(destination_dir)) {
372 // This creates the dir recursively.
373 HRESULT hr = CreateDir(destination_dir, NULL);
374 if (FAILED(hr)) {
375 return hr;
376 }
377 }
378
379 // Clean up any leftover pending removals that a previous uninstall
380 // may have left behind.
381 // Only do a prefix match if the directory is not the main Update directory.
382 // Otherwise, we may remove entries for previous version directories.
383 CPath install_path(destination_dir);
384 install_path.Canonicalize();
385 CPath goopdate_install_path(
386 is_machine_ ?
387 ConfigManager::Instance()->GetMachineGoopdateInstallDir() :
388 ConfigManager::Instance()->GetUserGoopdateInstallDir());
389 goopdate_install_path.Canonicalize();
390 bool prefix_match = install_path.m_strPath != goopdate_install_path.m_strPath;
391 HRESULT hr = File::RemoveFromMovesPendingReboot(destination_dir,
392 prefix_match);
393 VERIFY1(SUCCEEDED(hr) || !vista_util::IsUserAdmin());
394
395 std::vector<CString> source_file_paths;
396 std::vector<CString> destination_file_paths;
397 for (size_t i = 0; i < file_names.size(); ++i) {
398 CPath file_from(source_dir);
399 if (!file_from.Append(file_names[i])) {
400 return GOOPDATE_E_PATH_APPEND_FAILED;
401 }
402 source_file_paths.push_back(file_from);
403
404 CPath file(destination_dir);
405 if (!file.Append(file_names[i])) {
406 return GOOPDATE_E_PATH_APPEND_FAILED;
407 }
408 destination_file_paths.push_back(file);
409 }
410
411 hr = CopyAndValidateFiles(source_file_paths,
412 destination_file_paths,
413 overwrite);
414
415 SETUP_LOG(L2, (_T("[SetupFiles::CopyInstallFiles][Done]")));
416 return hr;
417 }
418
419 HRESULT SetupFiles::CopyAndValidateFiles(
420 const std::vector<CString>& source_file_paths,
421 const std::vector<CString>& destination_file_paths,
422 bool overwrite) {
423 ASSERT1(!source_file_paths.empty());
424 ASSERT1(!destination_file_paths.empty());
425 ASSERT1(source_file_paths.size() == destination_file_paths.size());
426
427 if (overwrite) {
428 // Best effort attempt to delete the current set of files:
429 // * try to remove an .old file that might be there.
430 // * move the current file to a .old and delete it after reboot.
431 // Because this is a best effort, we do not propogate errors.
432
433 for (size_t i = 0; i != destination_file_paths.size(); ++i) {
434 const CString cur_file = destination_file_paths[i];
435 const CString dot_old(cur_file + _T(".old"));
436 VERIFY1(SUCCEEDED(File::Remove(dot_old)));
437 HRESULT hr = File::Move(cur_file, dot_old, true);
438 if (SUCCEEDED(hr)) {
439 // Delete after reboot only works for admins. .old files will be left
440 // for user installs not being run by elevated admins.
441 hr = File::DeleteAfterReboot(dot_old);
442 if (FAILED(hr)) {
443 SETUP_LOG(LW, (_T("DeleteAfterReboot of %s failed with 0x%08x."),
444 dot_old, hr));
445 }
446 } else {
447 SETUP_LOG(L2, (_T("[failed to move][%s][0x%08x]"), cur_file, hr));
448 ASSERT1(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr);
449 }
450 }
451 }
452
453 for (size_t i = 0; i != source_file_paths.size(); ++i) {
454 const CString& source_file = source_file_paths[i];
455 const CString& destination_file = destination_file_paths[i];
456 SETUP_LOG(L2, (_T("[CopyAndValidateFiles][from=%s][to=%s][overwrite=%d]")
457 _T("[destination file exists=%d]"), source_file, destination_file,
458 overwrite, File::Exists(destination_file)));
459
460 extra_code1_ = i + 1; // 1-based; reserves 0 for success or not set.
461
462 if (overwrite || !File::Exists(destination_file)) {
463 HRESULT hr = VerifyFileSignature(source_file);
464 if (FAILED(hr)) {
465 OPT_LOG(LE, (_T("[precopy signature validation failed][from=%s][0x%x]"),
466 source_file, hr));
467 ++metric_setup_files_verification_failed_pre;
468 return hr;
469 }
470
471 hr = File::Copy(source_file, destination_file, true);
472 if (FAILED(hr)) {
473 OPT_LOG(LE, (_T("[copy failed][from=%s][to=%s][0x%08x]"),
474 source_file, destination_file, hr));
475 return hr;
476 }
477 }
478
479 HRESULT hr = File::AreFilesIdentical(source_file, destination_file) ?
480 VerifyFileSignature(destination_file) :
481 GOOPDATE_E_POST_COPY_VERIFICATION_FAILED;
482
483 if (FAILED(hr)) {
484 OPT_LOG(LE, (_T("[postcopy verification failed][from=%s][to=%s][0x%x]"),
485 source_file, destination_file, hr));
486 ++metric_setup_files_verification_failed_post;
487 VERIFY1(SUCCEEDED(File::Remove(destination_file)));
488 return hr;
489 }
490 }
491
492 extra_code1_ = 0;
493 return S_OK;
494 }
495
496 // The only secure location we copy to is Program Files, which only happens for
497 // machine installs.
498 HRESULT SetupFiles::VerifyFileSignature(const CString& filepath) {
499 if (!is_machine_) {
500 return S_OK;
501 }
502
503 HighresTimer verification_timer;
504
505 // Verify the Authenticode signature but use use only the local cache for
506 // revocation checks.
507 HRESULT hr = VerifySignature(filepath, false);
508 #if TEST_CERTIFICATE
509 // The chain of trust will not validate on builds signed with the test
510 // certificate.
511 if (CERT_E_UNTRUSTEDROOT == hr) {
512 hr = S_OK;
513 }
514 #endif
515 if (FAILED(hr)) {
516 return hr;
517 }
518
519 // Verify that there is a Google certificate and that it has not expired.
520 if (!VerifySigneeIsGoogle(filepath)) {
521 return GOOPDATE_E_VERIFY_SIGNEE_IS_GOOGLE_FAILED;
522 }
523
524 CORE_LOG(L3, (_T("[SetupFiles::VerifyFileSignature succeeded][%d ms]"),
525 verification_timer.GetElapsedMs()));
526 return S_OK;
527 }
528
529 bool SetupFiles::IsOlderShellVersionCompatible(ULONGLONG version) {
530 for (int i = 0; i < arraysize(kCompatibleOlderShellVersions); ++i) {
531 if (version == kCompatibleOlderShellVersions[i]) {
532 return true;
533 }
534 }
535 return false;
536 }
537
538 } // namespace omaha
OLDNEW
« no previous file with comments | « setup/setup_files.h ('k') | setup/setup_files_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698