1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/credential_provider/setup/setup_lib.h"
#include <shlobj.h>
#include <iomanip>
#include <string>
#include "base/command_line.h"
#include "base/file_version_info.h"
#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/scoped_native_library.h"
#include "base/strings/string_number_conversions.h"
#include "base/win/atl.h"
#include "base/win/registry.h"
#include "base/win/scoped_handle.h"
#include "base/win/windows_handle_util.h"
#include "base/win/windows_version.h"
#include "chrome/credential_provider/common/gcp_strings.h"
#include "chrome/credential_provider/extension/extension_strings.h"
#include "chrome/credential_provider/extension/extension_utils.h"
#include "chrome/credential_provider/gaiacp/gcp_utils.h"
#include "chrome/credential_provider/gaiacp/logging.h"
#include "chrome/credential_provider/gaiacp/reg_utils.h"
#include "chrome/credential_provider/setup/gcpw_files.h"
#include "chrome/credential_provider/setup/setup_utils.h"
#include "chrome/installer/util/delete_after_reboot_helper.h"
namespace credential_provider {
namespace {
// Creates the directory where GCP is to be installed.
base::FilePath CreateInstallDirectory() {
base::FilePath dest_path = GetInstallDirectory();
if (dest_path.empty()) {
return dest_path;
}
if (!base::CreateDirectory(dest_path)) {
HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
LOGFN(ERROR) << "base::CreateDirectory hr=" << putHR(hr);
return base::FilePath();
}
return dest_path;
}
// Copies the files specified in |names| from |src_path| to |dest_path| creating
// any intermedidate subdirectories of |dest_path| as needed. Both |src_path|
// and |dest_path| are full paths. |names| are paths relative to |src_path|
// and are copied to the same relative path under |dest_path|.
HRESULT InstallFiles(const base::FilePath& src_path,
const base::FilePath& dest_path,
const base::FilePath::StringType names[],
size_t length) {
for (size_t i = 0; i < length; ++i) {
base::FilePath src = src_path.Append(names[i]);
base::FilePath dest = dest_path.Append(names[i]);
// Make sure parent of destination file exists.
if (!base::CreateDirectory(dest.DirName())) {
HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
LOGFN(ERROR) << "CreateDirectory hr=" << putHR(hr)
<< " name=" << names[i];
return hr;
}
if (!base::CopyFile(src, dest)) {
HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
LOGFN(ERROR) << "CopyFile hr=" << putHR(hr) << " name=" << names[i];
return hr;
}
LOGFN(INFO) << "Installed name=" << names[i];
}
return S_OK;
}
// Registers the named DLLs by calling the DllRegisterServer entrypoint. The
// DLLs are specified with the |names| argument which are paths relative to
// |dest_path|. |fakes| is non-null during unit tests to install fakes into
// the loaded DLL.
HRESULT RegisterDlls(const base::FilePath& dest_path,
const base::FilePath::StringType names[],
size_t length,
FakesForTesting* fakes) {
bool has_failures = false;
for (size_t i = 0; i < length; ++i) {
base::ScopedNativeLibrary library(dest_path.Append(names[i]));
if (fakes) {
SetFakesForTestingFn set_fakes_for_testing_fn =
reinterpret_cast<SetFakesForTestingFn>(
library.GetFunctionPointer("SetFakesForTesting"));
if (set_fakes_for_testing_fn)
(*set_fakes_for_testing_fn)(fakes);
}
FARPROC register_server_fn = reinterpret_cast<FARPROC>(
library.GetFunctionPointer("DllRegisterServer"));
HRESULT hr = S_OK;
if (register_server_fn) {
hr = static_cast<HRESULT>((*register_server_fn)());
LOGFN(VERBOSE) << "Registered name=" << names[i] << " hr=" << putHR(hr);
} else {
LOGFN(ERROR) << "Failed to register name=" << names[i];
hr = E_NOTIMPL;
}
has_failures |= FAILED(hr);
}
return has_failures ? E_UNEXPECTED : S_OK;
}
// Unregisters the named DLLs by calling the DllUneegisterServer entrypoint.
// The DLLs are specified with the |names| argument which are paths relative to
// |dest_path|. |fakes| is non-null during unit tests to install fakes into
// the loaded DLL.
HRESULT UnregisterDlls(const base::FilePath& dest_path,
const base::FilePath::StringType names[],
size_t length,
FakesForTesting* fakes) {
bool has_failures = false;
for (size_t i = 0; i < length; ++i) {
base::ScopedNativeLibrary library(dest_path.Append(names[i]));
if (fakes) {
SetFakesForTestingFn pmfn = reinterpret_cast<SetFakesForTestingFn>(
library.GetFunctionPointer("SetFakesForTesting"));
if (pmfn)
(*pmfn)(fakes);
}
FARPROC pfn = reinterpret_cast<FARPROC>(
library.GetFunctionPointer("DllUnregisterServer"));
HRESULT hr = pfn ? static_cast<HRESULT>((*pfn)()) : E_UNEXPECTED;
LOGFN(VERBOSE) << "Unregistered name=" << names[i] << " hr=" << putHR(hr);
has_failures |= FAILED(hr);
}
return has_failures ? E_UNEXPECTED : S_OK;
}
} // namespace
HRESULT DoInstall(const base::FilePath& installer_path,
const std::wstring& product_version,
FakesForTesting* fakes) {
const base::FilePath gcp_path = CreateInstallDirectory();
if (gcp_path.empty())
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
base::FilePath dest_path = gcp_path.Append(product_version);
LOGFN(VERBOSE) << "Install to: " << dest_path;
// Make sure nothing under the destination directory is pending delete
// after reboot, so that files installed now won't get deleted later.
if (!RemoveFromMovesPendingReboot(dest_path)) {
HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
LOGFN(ERROR) << "RemoveFromMovesPendingReboot hr=" << putHR(hr);
return hr;
}
base::FilePath src_path = installer_path.DirName();
auto install_files =
credential_provider::GCPWFiles::Get()->GetEffectiveInstallFiles();
HRESULT hr = InstallFiles(src_path, dest_path, install_files.data(),
install_files.size());
if (FAILED(hr))
return hr;
auto register_dlls =
credential_provider::GCPWFiles::Get()->GetRegistrationFiles();
hr = RegisterDlls(dest_path, register_dlls.data(), register_dlls.size(),
fakes);
if (FAILED(hr))
return hr;
// If all is good, try to delete all other versions on best effort basis.
if (SUCCEEDED(hr))
DeleteVersionsExcept(gcp_path, product_version);
base::FilePath setup_exe_path = dest_path.Append(kCredentialProviderSetupExe);
hr = WriteUninstallRegistryValues(setup_exe_path);
if (FAILED(hr)) {
LOGFN(ERROR) << "WriteUninstallRegistryValues failed hr=" << putHR(hr);
// Uninstall registry values are written for MSI wrapper. Failing to write
// them will only impact uninstalling through uninstall shortcuts on
// Windows. There is still a workaround to uninstall by calling
// "gcp_setup.exe --uninstall" from a terminal. So, ignoring the failure in
// this case until we support rollback of installation that fails mid-way
// through.
}
hr = WriteCredentialProviderRegistryValues(dest_path);
if (FAILED(hr)) {
LOGFN(ERROR) << "WriteCredentialProviderRegistryValues failed hr="
<< putHR(hr);
}
if (extension::IsGCPWExtensionEnabled()) {
DWORD error_code = extension::InstallGCPWExtension(
dest_path.Append(kCredentialProviderExtensionExe));
if (error_code != ERROR_SUCCESS) {
LOGFN(ERROR) << "InstallGCPWExtension failed win32=" << error_code;
return HRESULT_FROM_WIN32(error_code);
}
}
return S_OK;
}
HRESULT DoUninstall(const base::FilePath& installer_path,
const base::FilePath& dest_path,
FakesForTesting* fakes) {
bool has_failures = false;
auto register_dlls =
credential_provider::GCPWFiles::Get()->GetRegistrationFiles();
// Do all actions best effort and keep going.
has_failures |= FAILED(UnregisterDlls(dest_path, register_dlls.data(),
register_dlls.size(), fakes));
// If the DLLs are unregistered, Credential Provider will not be loaded by
// Winlogon. Therefore, it is safe to delete the startup sentinel file at this
// time.
if (!has_failures)
DeleteStartupSentinel();
has_failures |=
FAILED(HRESULT_FROM_WIN32(extension::UninstallGCPWExtension()));
// Delete all files in the destination directory. This directory does not
// contain any configuration files or anything else user generated.
if (!base::DeletePathRecursively(dest_path)) {
has_failures = true;
ScheduleDirectoryForDeletion(dest_path);
}
// |dest_path| is of the form %ProgramFile%\Google\GCP\VERSION. Now try to
// delete the parent directory if possible.
if (base::IsDirectoryEmpty(dest_path.DirName()))
has_failures |= !base::DeleteFile(dest_path.DirName());
StandaloneInstallerConfigurator* installer_config =
StandaloneInstallerConfigurator::Get();
if (installer_config->IsStandaloneInstallation()) {
has_failures |= FAILED(HRESULT_FROM_WIN32(
StandaloneInstallerConfigurator::Get()->RemoveUninstallKey()));
}
// TODO(rogerta): ask user to reboot if anything went wrong during uninstall.
return has_failures ? E_UNEXPECTED : S_OK;
}
HRESULT RelaunchUninstaller(const base::FilePath& installer_path) {
base::FilePath temp_path;
if (!base::CreateNewTempDirectory(FILE_PATH_LITERAL("gcp"), &temp_path)) {
HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
LOGFN(ERROR) << "CreateNewTempDirectory hr=" << putHR(hr);
return hr;
}
base::FilePath new_installer_path =
temp_path.Append(installer_path.BaseName());
if (!base::CopyFile(installer_path, new_installer_path)) {
HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
LOGFN(ERROR) << "Could not copy uninstaller hr=" << putHR(hr) << " '"
<< installer_path << "' --> '" << new_installer_path << "'";
return hr;
}
base::win::ScopedHandle::Handle this_process_handle_handle;
if (!::DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(),
GetCurrentProcess(), &this_process_handle_handle, 0,
TRUE, // Inheritable.
DUPLICATE_SAME_ACCESS)) {
HRESULT hr = HRESULT_FROM_WIN32(::GetLastError());
LOGFN(ERROR) << "Error duplicating parent process handle.";
return hr;
}
base::win::ScopedHandle this_process_handle(this_process_handle_handle);
base::CommandLine cmdline(new_installer_path);
cmdline.AppendSwitch(switches::kUninstall);
cmdline.AppendSwitchPath(switches::kInstallPath, installer_path.DirName());
cmdline.AppendSwitchNative(switches::kParentHandle,
base::NumberToWString(base::win::HandleToUint32(
this_process_handle_handle)));
LOGFN(VERBOSE) << "Cmd: " << cmdline.GetCommandLineString();
base::LaunchOptions options;
options.handles_to_inherit.push_back(this_process_handle_handle);
options.current_directory = temp_path;
base::Process process(base::LaunchProcess(cmdline, options));
return process.IsValid() ? S_OK : E_FAIL;
}
int EnableStatsCollection(const base::CommandLine& cmdline) {
DCHECK(cmdline.HasSwitch(switches::kEnableStats) ||
cmdline.HasSwitch(switches::kDisableStats));
bool enable = !cmdline.HasSwitch(switches::kDisableStats);
base::win::RegKey key;
LONG status = key.Create(HKEY_LOCAL_MACHINE, kRegUpdaterClientStateAppPath,
KEY_SET_VALUE | KEY_WOW64_32KEY);
if (status != ERROR_SUCCESS) {
LOGFN(ERROR) << "Unable to open omaha key=" << kRegUpdaterClientStateAppPath
<< " status=" << status;
} else {
status = key.WriteValue(kRegUsageStatsName, enable ? 1 : 0);
if (status != ERROR_SUCCESS) {
LOGFN(ERROR) << "Unable to write " << kRegUsageStatsName
<< " value status=" << status;
}
}
return status == ERROR_SUCCESS ? 0 : -1;
}
HRESULT WriteUninstallRegistryValues(const base::FilePath& setup_exe) {
base::win::RegKey key;
LONG status = key.Create(HKEY_LOCAL_MACHINE, kRegUpdaterClientStateAppPath,
KEY_SET_VALUE | KEY_WOW64_32KEY);
if (status != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(status);
LOGFN(ERROR) << "Unable to open " << kRegUpdaterClientStateAppPath
<< " hr=" << putHR(hr);
return hr;
} else {
status =
key.WriteValue(kRegUninstallStringField, setup_exe.value().c_str());
if (status != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(status);
LOGFN(ERROR) << "Unable to write " << kRegUninstallStringField
<< " hr=" << putHR(hr);
return hr;
}
base::CommandLine uninstall_arguments(base::CommandLine::NO_PROGRAM);
uninstall_arguments.AppendSwitch(switches::kUninstall);
status = key.WriteValue(kRegUninstallArgumentsField,
uninstall_arguments.GetCommandLineString().c_str());
if (status != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(status);
LOGFN(ERROR) << "Unable to write " << kRegUninstallArgumentsField
<< " hr=" << putHR(hr);
return hr;
}
}
return HRESULT_FROM_WIN32(status);
}
HRESULT WriteCredentialProviderRegistryValues(
const base::FilePath& install_path) {
HRESULT hr =
StandaloneInstallerConfigurator::Get()->AddUninstallKey(install_path);
if (FAILED(hr)) {
LOGFN(ERROR) << "AddUninstallKey hr=" << putHR(hr);
return hr;
}
base::win::RegKey key;
LONG status = key.Create(HKEY_LOCAL_MACHINE, kGcpRootKeyName, KEY_SET_VALUE);
if (status != ERROR_SUCCESS) {
hr = HRESULT_FROM_WIN32(status);
LOGFN(ERROR) << "Unable to create " << kGcpRootKeyName
<< " hr=" << putHR(hr);
return hr;
}
return S_OK;
}
} // namespace credential_provider
|