2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-04 20:19:14 -08:00
|
|
|
|
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shlwapi.h>
|
|
|
|
#include <wtsapi32.h>
|
|
|
|
#include <userenv.h>
|
|
|
|
#include <shellapi.h>
|
|
|
|
|
|
|
|
#pragma comment(lib, "wtsapi32.lib")
|
|
|
|
#pragma comment(lib, "userenv.lib")
|
|
|
|
#pragma comment(lib, "shlwapi.lib")
|
2012-01-04 20:19:17 -08:00
|
|
|
#pragma comment(lib, "ole32.lib")
|
|
|
|
#pragma comment(lib, "rpcrt4.lib")
|
2012-01-04 20:19:14 -08:00
|
|
|
|
|
|
|
#include "nsWindowsHelpers.h"
|
|
|
|
|
|
|
|
#include "workmonitor.h"
|
|
|
|
#include "serviceinstall.h"
|
|
|
|
#include "servicebase.h"
|
|
|
|
#include "registrycertificates.h"
|
|
|
|
#include "uachelper.h"
|
2012-01-04 20:19:15 -08:00
|
|
|
#include "updatehelper.h"
|
2012-03-05 12:47:46 -08:00
|
|
|
#include "errors.h"
|
2012-01-04 20:19:14 -08:00
|
|
|
|
|
|
|
// Wait 15 minutes for an update operation to run at most.
|
|
|
|
// Updates usually take less than a minute so this seems like a
|
|
|
|
// significantly large and safe amount of time to wait.
|
|
|
|
static const int TIME_TO_WAIT_ON_UPDATER = 15 * 60 * 1000;
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t* MakeCommandLine(int argc, char16_t **argv);
|
2012-01-04 20:19:14 -08:00
|
|
|
BOOL WriteStatusFailure(LPCWSTR updateDirPath, int errorCode);
|
|
|
|
BOOL PathGetSiblingFilePath(LPWSTR destinationBuffer, LPCWSTR siblingFilePath,
|
|
|
|
LPCWSTR newFileName);
|
|
|
|
|
2011-12-28 18:08:37 -08:00
|
|
|
/*
|
|
|
|
* Read the update.status file and sets isApplying to true if
|
|
|
|
* the status is set to applying
|
|
|
|
*
|
|
|
|
* @param updateDirPath The directory where update.status is stored
|
|
|
|
* @param isApplying Out parameter for specifying if the status
|
|
|
|
* is set to applying or not.
|
|
|
|
* @return TRUE if the information was filled.
|
|
|
|
*/
|
|
|
|
static BOOL
|
|
|
|
IsStatusApplying(LPCWSTR updateDirPath, BOOL &isApplying)
|
|
|
|
{
|
|
|
|
isApplying = FALSE;
|
2013-03-07 13:41:37 -08:00
|
|
|
WCHAR updateStatusFilePath[MAX_PATH + 1] = {L'\0'};
|
|
|
|
wcsncpy(updateStatusFilePath, updateDirPath, MAX_PATH);
|
2011-12-28 18:08:37 -08:00
|
|
|
if (!PathAppendSafe(updateStatusFilePath, L"update.status")) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not append path for update.status file"));
|
2011-12-28 18:08:37 -08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoHandle statusFile(CreateFileW(updateStatusFilePath, GENERIC_READ,
|
|
|
|
FILE_SHARE_READ |
|
|
|
|
FILE_SHARE_WRITE |
|
|
|
|
FILE_SHARE_DELETE,
|
2013-10-10 13:38:05 -07:00
|
|
|
nullptr, OPEN_EXISTING, 0, nullptr));
|
2012-01-10 08:28:50 -08:00
|
|
|
|
|
|
|
if (INVALID_HANDLE_VALUE == statusFile) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not open update.status file"));
|
2012-01-10 08:28:50 -08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-12-28 18:08:37 -08:00
|
|
|
char buf[32] = { 0 };
|
|
|
|
DWORD read;
|
2013-10-10 13:38:05 -07:00
|
|
|
if (!ReadFile(statusFile, buf, sizeof(buf), &read, nullptr)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not read from update.status file"));
|
2011-12-28 18:08:37 -08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("updater.exe returned status: %s", buf));
|
2012-01-10 08:28:50 -08:00
|
|
|
|
2011-12-28 18:08:37 -08:00
|
|
|
const char kApplying[] = "applying";
|
|
|
|
isApplying = strncmp(buf, kApplying,
|
|
|
|
sizeof(kApplying) - 1) == 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-06-18 13:06:01 -07:00
|
|
|
/**
|
|
|
|
* Determines whether we're staging an update.
|
|
|
|
*
|
|
|
|
* @param argc The argc value normally sent to updater.exe
|
|
|
|
* @param argv The argv value normally sent to updater.exe
|
|
|
|
* @param boolean True if we're staging an update
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
IsUpdateBeingStaged(int argc, LPWSTR *argv)
|
|
|
|
{
|
|
|
|
// PID will be set to -1 if we're supposed to stage an update.
|
|
|
|
return argc == 4 && !wcscmp(argv[3], L"-1");
|
|
|
|
}
|
2012-01-04 20:19:14 -08:00
|
|
|
|
2012-05-22 07:50:04 -07:00
|
|
|
/**
|
|
|
|
* Gets the installation directory from the arguments passed to updater.exe.
|
|
|
|
*
|
|
|
|
* @param argcTmp The argc value normally sent to updater.exe
|
|
|
|
* @param argvTmp The argv value normally sent to updater.exe
|
|
|
|
* @param aResultDir Buffer to hold the installation directory.
|
|
|
|
*/
|
|
|
|
static BOOL
|
2013-03-07 13:41:37 -08:00
|
|
|
GetInstallationDir(int argcTmp, LPWSTR *argvTmp, WCHAR aResultDir[MAX_PATH + 1])
|
2012-05-22 07:50:04 -07:00
|
|
|
{
|
|
|
|
if (argcTmp < 2) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-03-07 13:41:37 -08:00
|
|
|
wcsncpy(aResultDir, argvTmp[2], MAX_PATH);
|
2012-05-22 07:50:04 -07:00
|
|
|
WCHAR* backSlash = wcsrchr(aResultDir, L'\\');
|
|
|
|
// Make sure that the path does not include trailing backslashes
|
|
|
|
if (backSlash && (backSlash[1] == L'\0')) {
|
|
|
|
*backSlash = L'\0';
|
|
|
|
}
|
2012-06-18 13:06:01 -07:00
|
|
|
bool backgroundUpdate = IsUpdateBeingStaged(argcTmp, argvTmp);
|
2012-05-22 07:50:04 -07:00
|
|
|
bool replaceRequest = (argcTmp >= 4 && wcsstr(argvTmp[3], L"/replace"));
|
|
|
|
if (backgroundUpdate || replaceRequest) {
|
|
|
|
return PathRemoveFileSpecW(aResultDir);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:14 -08:00
|
|
|
/**
|
2012-01-04 20:19:15 -08:00
|
|
|
* Runs an update process as the service using the SYSTEM account.
|
2012-01-04 20:19:14 -08:00
|
|
|
*
|
2012-01-04 20:19:17 -08:00
|
|
|
* @param argc The number of arguments in argv
|
|
|
|
* @param argv The arguments normally passed to updater.exe
|
|
|
|
* argv[0] must be the path to updater.exe
|
2012-01-04 20:19:15 -08:00
|
|
|
* @param processStarted Set to TRUE if the process was started.
|
2012-01-04 20:19:14 -08:00
|
|
|
* @return TRUE if the update process was run had a return code of 0.
|
|
|
|
*/
|
|
|
|
BOOL
|
2012-01-04 20:19:17 -08:00
|
|
|
StartUpdateProcess(int argc,
|
|
|
|
LPWSTR *argv,
|
2012-09-20 12:40:45 -07:00
|
|
|
LPCWSTR installDir,
|
2012-01-04 20:19:15 -08:00
|
|
|
BOOL &processStarted)
|
2012-01-04 20:19:14 -08:00
|
|
|
{
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("Starting update process as the service in session 0."));
|
2012-01-04 20:19:14 -08:00
|
|
|
STARTUPINFO si = {0};
|
|
|
|
si.cb = sizeof(STARTUPINFO);
|
|
|
|
si.lpDesktop = L"winsta0\\Default";
|
|
|
|
PROCESS_INFORMATION pi = {0};
|
|
|
|
|
|
|
|
// The updater command line is of the form:
|
|
|
|
// updater.exe update-dir apply [wait-pid [callback-dir callback-path args]]
|
2012-01-04 20:19:17 -08:00
|
|
|
LPWSTR cmdLine = MakeCommandLine(argc, argv);
|
2012-01-04 20:19:14 -08:00
|
|
|
|
|
|
|
// If we're about to start the update process from session 0,
|
|
|
|
// then we should not show a GUI. This only really needs to be done
|
|
|
|
// on Vista and higher, but it's better to keep everything consistent
|
|
|
|
// across all OS if it's of no harm.
|
2012-01-04 20:19:17 -08:00
|
|
|
if (argc >= 2 ) {
|
2012-01-04 20:19:14 -08:00
|
|
|
// Setting the desktop to blank will ensure no GUI is displayed
|
|
|
|
si.lpDesktop = L"";
|
|
|
|
si.dwFlags |= STARTF_USESHOWWINDOW;
|
|
|
|
si.wShowWindow = SW_HIDE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We move the updater.ini file out of the way because we will handle
|
|
|
|
// executing PostUpdate through the service. We handle PostUpdate from
|
|
|
|
// the service because there are some per user things that happen that
|
|
|
|
// can't run in session 0 which we run updater.exe in.
|
|
|
|
// Once we are done running updater.exe we rename updater.ini back so
|
|
|
|
// that if there were any errors the next updater.exe will run correctly.
|
|
|
|
WCHAR updaterINI[MAX_PATH + 1];
|
|
|
|
WCHAR updaterINITemp[MAX_PATH + 1];
|
|
|
|
BOOL selfHandlePostUpdate = FALSE;
|
|
|
|
// We use the updater.ini from the same directory as the updater.exe
|
|
|
|
// because of background updates.
|
2012-01-04 20:19:17 -08:00
|
|
|
if (PathGetSiblingFilePath(updaterINI, argv[0], L"updater.ini") &&
|
|
|
|
PathGetSiblingFilePath(updaterINITemp, argv[0], L"updater.tmp")) {
|
2011-12-28 18:08:37 -08:00
|
|
|
selfHandlePostUpdate = MoveFileExW(updaterINI, updaterINITemp,
|
|
|
|
MOVEFILE_REPLACE_EXISTING);
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:19 -08:00
|
|
|
// Add an env var for MOZ_USING_SERVICE so the updater.exe can
|
2012-01-04 20:19:15 -08:00
|
|
|
// do anything special that it needs to do for service updates.
|
2011-12-28 18:08:37 -08:00
|
|
|
// Search in updater.cpp for more info on MOZ_USING_SERVICE.
|
|
|
|
putenv(const_cast<char*>("MOZ_USING_SERVICE=1"));
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("Starting service with cmdline: %ls", cmdLine));
|
2012-01-04 20:19:17 -08:00
|
|
|
processStarted = CreateProcessW(argv[0], cmdLine,
|
2013-10-10 13:38:05 -07:00
|
|
|
nullptr, nullptr, FALSE,
|
2012-01-04 20:19:19 -08:00
|
|
|
CREATE_DEFAULT_ERROR_MODE,
|
2013-10-10 13:38:05 -07:00
|
|
|
nullptr,
|
|
|
|
nullptr, &si, &pi);
|
2011-12-28 18:08:37 -08:00
|
|
|
// Empty value on putenv is how you remove an env variable in Windows
|
|
|
|
putenv(const_cast<char*>("MOZ_USING_SERVICE="));
|
2012-01-04 20:19:19 -08:00
|
|
|
|
2012-01-04 20:19:14 -08:00
|
|
|
BOOL updateWasSuccessful = FALSE;
|
|
|
|
if (processStarted) {
|
|
|
|
// Wait for the updater process to finish
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("Process was started... waiting on result."));
|
2012-01-04 20:19:14 -08:00
|
|
|
DWORD waitRes = WaitForSingleObject(pi.hProcess, TIME_TO_WAIT_ON_UPDATER);
|
|
|
|
if (WAIT_TIMEOUT == waitRes) {
|
|
|
|
// We waited a long period of time for updater.exe and it never finished
|
|
|
|
// so kill it.
|
|
|
|
TerminateProcess(pi.hProcess, 1);
|
|
|
|
} else {
|
|
|
|
// Check the return code of updater.exe to make sure we get 0
|
|
|
|
DWORD returnCode;
|
|
|
|
if (GetExitCodeProcess(pi.hProcess, &returnCode)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("Process finished with return code %d.", returnCode));
|
2012-01-04 20:19:14 -08:00
|
|
|
// updater returns 0 if successful.
|
|
|
|
updateWasSuccessful = (returnCode == 0);
|
|
|
|
} else {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Process finished but could not obtain return code."));
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
2011-12-28 18:08:37 -08:00
|
|
|
|
|
|
|
// Check just in case updater.exe didn't change the status from
|
|
|
|
// applying. If this is the case we report an error.
|
|
|
|
BOOL isApplying = FALSE;
|
|
|
|
if (IsStatusApplying(argv[1], isApplying) && isApplying) {
|
|
|
|
if (updateWasSuccessful) {
|
|
|
|
LOG(("update.status is still applying even know update "
|
2012-10-25 19:34:03 -07:00
|
|
|
" was successful."));
|
2011-12-28 18:08:37 -08:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
|
|
|
SERVICE_STILL_APPLYING_ON_SUCCESS)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status still applying on"
|
|
|
|
" success error."));
|
2011-12-28 18:08:37 -08:00
|
|
|
}
|
|
|
|
// Since we still had applying we know updater.exe didn't do its
|
|
|
|
// job correctly.
|
|
|
|
updateWasSuccessful = FALSE;
|
|
|
|
} else {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("update.status is still applying and update was not successful."));
|
2011-12-28 18:08:37 -08:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
|
|
|
SERVICE_STILL_APPLYING_ON_FAILURE)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status still applying on"
|
|
|
|
" success error."));
|
2011-12-28 18:08:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 20:19:14 -08:00
|
|
|
} else {
|
|
|
|
DWORD lastError = GetLastError();
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not create process as current user, "
|
|
|
|
"updaterPath: %ls; cmdLine: %ls. (%d)",
|
|
|
|
argv[0], cmdLine, lastError));
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're done with the update, restore back the updater.ini file
|
|
|
|
// We use it ourselves, and also we want it back in case we had any type
|
|
|
|
// of error so that the normal update process can use it.
|
|
|
|
if (selfHandlePostUpdate) {
|
2011-12-28 18:08:37 -08:00
|
|
|
MoveFileExW(updaterINITemp, updaterINI, MOVEFILE_REPLACE_EXISTING);
|
2012-01-04 20:19:14 -08:00
|
|
|
|
2012-01-04 20:19:16 -08:00
|
|
|
// Only run the PostUpdate if the update was successful
|
2012-01-04 20:19:17 -08:00
|
|
|
if (updateWasSuccessful && argc > 2) {
|
|
|
|
LPCWSTR updateInfoDir = argv[1];
|
2012-06-18 13:06:01 -07:00
|
|
|
bool backgroundUpdate = IsUpdateBeingStaged(argc, argv);
|
2012-01-04 20:19:15 -08:00
|
|
|
|
|
|
|
// Launch the PostProcess with admin access in session 0. This is
|
|
|
|
// actually launching the post update process but it takes in the
|
|
|
|
// callback app path to figure out where to apply to.
|
|
|
|
// The PostUpdate process with user only access will be done inside
|
|
|
|
// the unelevated updater.exe after the update process is complete
|
|
|
|
// from the service. We don't know here which session to start
|
|
|
|
// the user PostUpdate process from.
|
2012-05-30 15:09:59 -07:00
|
|
|
// Note that we don't need to do this if we're just staging the
|
|
|
|
// update in the background, as the PostUpdate step runs when
|
|
|
|
// performing the replacing in that case.
|
|
|
|
if (!backgroundUpdate) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("Launching post update process as the service in session 0."));
|
2013-10-10 13:38:05 -07:00
|
|
|
if (!LaunchWinPostProcess(installDir, updateInfoDir, true, nullptr)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("The post update process could not be launched."
|
|
|
|
" installDir: %ls, updateInfoDir: %ls",
|
|
|
|
installDir, updateInfoDir));
|
2012-05-30 15:09:59 -07:00
|
|
|
}
|
2012-01-04 20:19:16 -08:00
|
|
|
}
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(cmdLine);
|
|
|
|
return updateWasSuccessful;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-04 20:19:17 -08:00
|
|
|
* Processes a software update command
|
2012-01-04 20:19:14 -08:00
|
|
|
*
|
2012-01-04 20:19:17 -08:00
|
|
|
* @param argc The number of arguments in argv
|
|
|
|
* @param argv The arguments normally passed to updater.exe
|
|
|
|
* argv[0] must be the path to updater.exe
|
|
|
|
* @return TRUE if the update was successful.
|
2012-01-04 20:19:14 -08:00
|
|
|
*/
|
|
|
|
BOOL
|
2011-12-28 18:08:37 -08:00
|
|
|
ProcessSoftwareUpdateCommand(DWORD argc, LPWSTR *argv)
|
2012-01-04 20:19:14 -08:00
|
|
|
{
|
2012-01-04 20:19:17 -08:00
|
|
|
BOOL result = TRUE;
|
|
|
|
if (argc < 3) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Not enough command line parameters specified. "
|
|
|
|
"Updating update.status."));
|
2012-01-04 20:19:14 -08:00
|
|
|
|
2012-01-04 20:19:17 -08:00
|
|
|
// We can only update update.status if argv[1] exists. argv[1] is
|
|
|
|
// the directory where the update.status file exists.
|
2012-09-06 14:58:47 -07:00
|
|
|
if (argc < 2 ||
|
2012-01-04 20:19:17 -08:00
|
|
|
!WriteStatusFailure(argv[1],
|
|
|
|
SERVICE_NOT_ENOUGH_COMMAND_LINE_ARGS)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status service update failure. (%d)",
|
|
|
|
GetLastError()));
|
2012-05-02 12:40:33 -07:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-03-07 13:41:37 -08:00
|
|
|
WCHAR installDir[MAX_PATH + 1] = {L'\0'};
|
2012-05-22 07:50:04 -07:00
|
|
|
if (!GetInstallationDir(argc, argv, installDir)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not get the installation directory"));
|
2012-05-22 07:50:04 -07:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
|
|
|
SERVICE_INSTALLDIR_ERROR)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status for GetInstallationDir failure."));
|
2012-05-22 07:50:04 -07:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-05-02 12:40:33 -07:00
|
|
|
// Make sure the path to the updater to use for the update is local.
|
|
|
|
// We do this check to make sure that file locking is available for
|
|
|
|
// race condition security checks.
|
|
|
|
BOOL isLocal = FALSE;
|
|
|
|
if (!IsLocalFile(argv[0], isLocal) || !isLocal) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Filesystem in path %ls is not supported (%d)",
|
|
|
|
argv[0], GetLastError()));
|
2012-05-02 12:40:33 -07:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
|
|
|
SERVICE_UPDATER_NOT_FIXED_DRIVE)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status service update failure. (%d)",
|
|
|
|
GetLastError()));
|
2012-05-02 12:40:33 -07:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoHandle noWriteLock(CreateFileW(argv[0], GENERIC_READ, FILE_SHARE_READ,
|
2013-10-10 13:38:05 -07:00
|
|
|
nullptr, OPEN_EXISTING, 0, nullptr));
|
2012-05-02 12:40:33 -07:00
|
|
|
if (INVALID_HANDLE_VALUE == noWriteLock) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not set no write sharing access on file. (%d)",
|
|
|
|
GetLastError()));
|
2012-05-02 12:40:33 -07:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
|
|
|
SERVICE_COULD_NOT_LOCK_UPDATER)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status service update failure. (%d)",
|
|
|
|
GetLastError()));
|
2012-01-04 20:19:17 -08:00
|
|
|
}
|
|
|
|
return FALSE;
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:16 -08:00
|
|
|
// Verify that the updater.exe that we are executing is the same
|
|
|
|
// as the one in the installation directory which we are updating.
|
2012-05-22 07:50:04 -07:00
|
|
|
// The installation dir that we are installing to is installDir.
|
2013-07-01 16:11:52 -07:00
|
|
|
WCHAR installDirUpdater[MAX_PATH + 1] = { L'\0' };
|
2012-05-22 07:50:04 -07:00
|
|
|
wcsncpy(installDirUpdater, installDir, MAX_PATH);
|
2012-01-04 20:19:16 -08:00
|
|
|
if (!PathAppendSafe(installDirUpdater, L"updater.exe")) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Install directory updater could not be determined."));
|
2012-01-04 20:19:16 -08:00
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL updaterIsCorrect;
|
2012-01-04 20:19:17 -08:00
|
|
|
if (result && !VerifySameFiles(argv[0], installDirUpdater,
|
2012-01-04 20:19:16 -08:00
|
|
|
updaterIsCorrect)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Error checking if the updaters are the same.\n"
|
|
|
|
"Path 1: %ls\nPath 2: %ls", argv[0], installDirUpdater));
|
2012-01-04 20:19:16 -08:00
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result && !updaterIsCorrect) {
|
2013-04-04 10:32:47 -07:00
|
|
|
LOG_WARN(("The updaters do not match, updater will not run."));
|
2012-01-04 20:19:16 -08:00
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
LOG(("updater.exe was compared successfully to the installation directory"
|
2012-10-25 19:34:03 -07:00
|
|
|
" updater.exe."));
|
2012-01-04 20:19:16 -08:00
|
|
|
} else {
|
2012-01-04 20:19:17 -08:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
2012-01-04 20:19:16 -08:00
|
|
|
SERVICE_UPDATER_COMPARE_ERROR)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status updater compare failure."));
|
2012-01-04 20:19:16 -08:00
|
|
|
}
|
2012-01-04 20:19:17 -08:00
|
|
|
return FALSE;
|
2012-01-04 20:19:16 -08:00
|
|
|
}
|
|
|
|
|
2013-04-04 10:32:47 -07:00
|
|
|
// Check to make sure the updater.exe module has the unique updater identity.
|
2012-01-04 20:19:16 -08:00
|
|
|
// This is a security measure to make sure that the signed executable that
|
|
|
|
// we will run is actually an updater.
|
2013-10-10 13:38:05 -07:00
|
|
|
HMODULE updaterModule = LoadLibraryEx(argv[0], nullptr,
|
2012-05-02 12:40:28 -07:00
|
|
|
LOAD_LIBRARY_AS_DATAFILE);
|
2012-01-04 20:19:16 -08:00
|
|
|
if (!updaterModule) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("updater.exe module could not be loaded. (%d)", GetLastError()));
|
2012-01-04 20:19:16 -08:00
|
|
|
result = FALSE;
|
|
|
|
} else {
|
|
|
|
char updaterIdentity[64];
|
|
|
|
if (!LoadStringA(updaterModule, IDS_UPDATER_IDENTITY,
|
|
|
|
updaterIdentity, sizeof(updaterIdentity))) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("The updater.exe application does not contain the Mozilla"
|
|
|
|
" updater identity."));
|
2012-01-04 20:19:16 -08:00
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(updaterIdentity, UPDATER_IDENTITY_STRING)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("The updater.exe identity string is not valid."));
|
2012-01-04 20:19:16 -08:00
|
|
|
result = FALSE;
|
|
|
|
}
|
|
|
|
FreeLibrary(updaterModule);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
LOG(("The updater.exe application contains the Mozilla"
|
2012-10-25 19:34:03 -07:00
|
|
|
" updater identity."));
|
2012-01-04 20:19:16 -08:00
|
|
|
} else {
|
2012-01-04 20:19:17 -08:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
2012-01-04 20:19:16 -08:00
|
|
|
SERVICE_UPDATER_IDENTITY_ERROR)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status no updater identity."));
|
2012-01-04 20:19:16 -08:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:15 -08:00
|
|
|
// Check for updater.exe sign problems
|
2012-01-04 20:19:14 -08:00
|
|
|
BOOL updaterSignProblem = FALSE;
|
2012-01-04 20:19:15 -08:00
|
|
|
#ifndef DISABLE_UPDATER_AUTHENTICODE_CHECK
|
2012-05-22 07:50:04 -07:00
|
|
|
updaterSignProblem = !DoesBinaryMatchAllowedCertificates(installDir,
|
2012-01-04 20:19:17 -08:00
|
|
|
argv[0]);
|
2012-01-04 20:19:14 -08:00
|
|
|
#endif
|
|
|
|
|
2012-01-04 20:19:17 -08:00
|
|
|
// Only proceed with the update if we have no signing problems
|
|
|
|
if (!updaterSignProblem) {
|
2012-01-04 20:19:14 -08:00
|
|
|
BOOL updateProcessWasStarted = FALSE;
|
2012-09-20 12:40:45 -07:00
|
|
|
if (StartUpdateProcess(argc, argv, installDir,
|
2012-01-04 20:19:15 -08:00
|
|
|
updateProcessWasStarted)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("updater.exe was launched and run successfully!"));
|
2012-01-04 20:19:18 -08:00
|
|
|
LogFlush();
|
|
|
|
|
2012-06-18 13:06:01 -07:00
|
|
|
// Don't attempt to update the service when the update is being staged.
|
|
|
|
if (!IsUpdateBeingStaged(argc, argv)) {
|
|
|
|
// We might not execute code after StartServiceUpdate because
|
|
|
|
// the service installer will stop the service if it is running.
|
|
|
|
StartServiceUpdate(installDir);
|
|
|
|
}
|
2012-01-04 20:19:14 -08:00
|
|
|
} else {
|
2012-01-04 20:19:17 -08:00
|
|
|
result = FALSE;
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Error running update process. Updating update.status (%d)",
|
|
|
|
GetLastError()));
|
2012-01-04 20:19:18 -08:00
|
|
|
LogFlush();
|
2012-01-04 20:19:14 -08:00
|
|
|
|
|
|
|
// If the update process was started, then updater.exe is responsible for
|
2012-01-04 20:19:15 -08:00
|
|
|
// setting the failure code. If it could not be started then we do the
|
|
|
|
// work. We set an error instead of directly setting status pending
|
|
|
|
// so that the app.update.service.errors pref can be updated when
|
|
|
|
// the callback app restarts.
|
2012-01-04 20:19:14 -08:00
|
|
|
if (!updateProcessWasStarted) {
|
2012-01-04 20:19:17 -08:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
2012-01-04 20:19:14 -08:00
|
|
|
SERVICE_UPDATER_COULD_NOT_BE_STARTED)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write update.status service update failure. (%d)",
|
|
|
|
GetLastError()));
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-01-04 20:19:17 -08:00
|
|
|
result = FALSE;
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not start process due to certificate check error on "
|
|
|
|
"updater.exe. Updating update.status. (%d)", GetLastError()));
|
2012-01-04 20:19:14 -08:00
|
|
|
|
|
|
|
// When there is a certificate check error on the updater.exe application,
|
|
|
|
// we want to write out the error.
|
2012-01-04 20:19:17 -08:00
|
|
|
if (!WriteStatusFailure(argv[1],
|
2012-01-04 20:19:14 -08:00
|
|
|
SERVICE_UPDATER_SIGN_ERROR)) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Could not write pending state to update.status. (%d)",
|
|
|
|
GetLastError()));
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
}
|
2012-02-11 14:38:37 -08:00
|
|
|
|
2012-01-04 20:19:17 -08:00
|
|
|
return result;
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
|
2013-04-04 10:32:47 -07:00
|
|
|
/**
|
|
|
|
* Obtains the updater path alongside a subdir of the service binary.
|
|
|
|
* The purpose of this function is to return a path that is likely high
|
|
|
|
* integrity and therefore more safe to execute code from.
|
|
|
|
*
|
|
|
|
* @param serviceUpdaterPath Out parameter for the path where the updater
|
|
|
|
* should be copied to.
|
|
|
|
* @return TRUE if a file path was obtained.
|
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
GetSecureUpdaterPath(WCHAR serviceUpdaterPath[MAX_PATH + 1])
|
|
|
|
{
|
2013-10-10 13:38:05 -07:00
|
|
|
if (!GetModuleFileNameW(nullptr, serviceUpdaterPath, MAX_PATH)) {
|
2013-04-04 10:32:47 -07:00
|
|
|
LOG_WARN(("Could not obtain module filename when attempting to "
|
|
|
|
"use a secure updater path. (%d)", GetLastError()));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PathRemoveFileSpecW(serviceUpdaterPath)) {
|
|
|
|
LOG_WARN(("Couldn't remove file spec when attempting to use a secure "
|
|
|
|
"updater path. (%d)", GetLastError()));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PathAppendSafe(serviceUpdaterPath, L"update")) {
|
|
|
|
LOG_WARN(("Couldn't append file spec when attempting to use a secure "
|
|
|
|
"updater path. (%d)", GetLastError()));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-10-10 13:38:05 -07:00
|
|
|
CreateDirectoryW(serviceUpdaterPath, nullptr);
|
2013-04-04 10:32:47 -07:00
|
|
|
|
|
|
|
if (!PathAppendSafe(serviceUpdaterPath, L"updater.exe")) {
|
|
|
|
LOG_WARN(("Couldn't append file spec when attempting to use a secure "
|
|
|
|
"updater path. (%d)", GetLastError()));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the passed in updater path and the associated updater.ini file.
|
|
|
|
*
|
|
|
|
* @param serviceUpdaterPath The path to delete.
|
|
|
|
* @return TRUE if a file was deleted.
|
|
|
|
*/
|
|
|
|
BOOL
|
|
|
|
DeleteSecureUpdater(WCHAR serviceUpdaterPath[MAX_PATH + 1])
|
|
|
|
{
|
|
|
|
BOOL result = FALSE;
|
|
|
|
if (serviceUpdaterPath[0]) {
|
|
|
|
result = DeleteFileW(serviceUpdaterPath);
|
|
|
|
if (!result && GetLastError() != ERROR_PATH_NOT_FOUND &&
|
|
|
|
GetLastError() != ERROR_FILE_NOT_FOUND) {
|
|
|
|
LOG_WARN(("Could not delete service updater path: '%ls'.",
|
|
|
|
serviceUpdaterPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR updaterINIPath[MAX_PATH + 1] = { L'\0' };
|
|
|
|
if (PathGetSiblingFilePath(updaterINIPath, serviceUpdaterPath,
|
|
|
|
L"updater.ini")) {
|
|
|
|
result = DeleteFileW(updaterINIPath);
|
|
|
|
if (!result && GetLastError() != ERROR_PATH_NOT_FOUND &&
|
|
|
|
GetLastError() != ERROR_FILE_NOT_FOUND) {
|
|
|
|
LOG_WARN(("Could not delete service updater INI path: '%ls'.",
|
|
|
|
updaterINIPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:14 -08:00
|
|
|
/**
|
2012-01-04 20:19:17 -08:00
|
|
|
* Executes a service command.
|
2012-01-04 20:19:14 -08:00
|
|
|
*
|
2012-01-04 20:19:17 -08:00
|
|
|
* @param argc The number of arguments in argv
|
|
|
|
* @param argv The service command line arguments, argv[0] and argv[1]
|
|
|
|
* and automatically included by Windows. argv[2] is the
|
|
|
|
* service command.
|
|
|
|
*
|
|
|
|
* @return FALSE if there was an error executing the service command.
|
2012-01-04 20:19:14 -08:00
|
|
|
*/
|
|
|
|
BOOL
|
2011-12-28 18:08:37 -08:00
|
|
|
ExecuteServiceCommand(int argc, LPWSTR *argv)
|
2012-01-04 20:19:14 -08:00
|
|
|
{
|
2012-01-04 20:19:17 -08:00
|
|
|
if (argc < 3) {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Not enough command line arguments to execute a service command"));
|
2012-01-04 20:19:14 -08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-01-04 20:19:17 -08:00
|
|
|
// The tests work by making sure the log has changed, so we put a
|
|
|
|
// unique ID in the log.
|
|
|
|
RPC_WSTR guidString = RPC_WSTR(L"");
|
|
|
|
GUID guid;
|
|
|
|
HRESULT hr = CoCreateGuid(&guid);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
UuidToString(&guid, &guidString);
|
|
|
|
}
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("Executing service command %ls, ID: %ls",
|
2012-01-04 20:19:17 -08:00
|
|
|
argv[2], reinterpret_cast<LPCWSTR>(guidString)));
|
|
|
|
RpcStringFree(&guidString);
|
|
|
|
|
|
|
|
BOOL result = FALSE;
|
|
|
|
if (!lstrcmpi(argv[2], L"software-update")) {
|
2013-04-04 10:32:47 -07:00
|
|
|
|
|
|
|
// Use the passed in command line arguments for the update, except for the
|
|
|
|
// path to updater.exe. We copy updater.exe to a the directory of the
|
|
|
|
// MozillaMaintenance service so that a low integrity process cannot
|
|
|
|
// replace the updater.exe at any point and use that for the update.
|
|
|
|
// It also makes DLL injection attacks harder.
|
|
|
|
LPWSTR oldUpdaterPath = argv[3];
|
|
|
|
WCHAR secureUpdaterPath[MAX_PATH + 1] = { L'\0' };
|
|
|
|
result = GetSecureUpdaterPath(secureUpdaterPath); // Does its own logging
|
|
|
|
if (result) {
|
|
|
|
LOG(("Passed in path: '%ls'; Using this path for updating: '%ls'.",
|
|
|
|
oldUpdaterPath, secureUpdaterPath));
|
|
|
|
DeleteSecureUpdater(secureUpdaterPath);
|
|
|
|
result = CopyFileW(oldUpdaterPath, secureUpdaterPath, FALSE);
|
|
|
|
}
|
|
|
|
|
2013-05-23 10:26:44 -07:00
|
|
|
if (!result) {
|
|
|
|
LOG_WARN(("Could not copy path to secure location. (%d)",
|
|
|
|
GetLastError()));
|
|
|
|
if (argc > 4 && !WriteStatusFailure(argv[4],
|
|
|
|
SERVICE_COULD_NOT_COPY_UPDATER)) {
|
|
|
|
LOG_WARN(("Could not write update.status could not copy updater error"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// We obtained the path and copied it successfully, update the path to
|
|
|
|
// use for the service update.
|
2013-04-04 10:32:47 -07:00
|
|
|
argv[3] = secureUpdaterPath;
|
|
|
|
|
|
|
|
WCHAR oldUpdaterINIPath[MAX_PATH + 1] = { L'\0' };
|
|
|
|
WCHAR secureUpdaterINIPath[MAX_PATH + 1] = { L'\0' };
|
|
|
|
if (PathGetSiblingFilePath(secureUpdaterINIPath, secureUpdaterPath,
|
|
|
|
L"updater.ini") &&
|
|
|
|
PathGetSiblingFilePath(oldUpdaterINIPath, oldUpdaterPath,
|
|
|
|
L"updater.ini")) {
|
|
|
|
// This is non fatal if it fails there is no real harm
|
|
|
|
if (!CopyFileW(oldUpdaterINIPath, secureUpdaterINIPath, FALSE)) {
|
|
|
|
LOG_WARN(("Could not copy updater.ini from: '%ls' to '%ls'. (%d)",
|
|
|
|
oldUpdaterINIPath, secureUpdaterINIPath, GetLastError()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 10:26:44 -07:00
|
|
|
result = ProcessSoftwareUpdateCommand(argc - 3, argv + 3);
|
|
|
|
DeleteSecureUpdater(secureUpdaterPath);
|
|
|
|
}
|
2013-04-04 10:32:47 -07:00
|
|
|
|
2012-01-04 20:19:19 -08:00
|
|
|
// We might not reach here if the service install succeeded
|
|
|
|
// because the service self updates itself and the service
|
|
|
|
// installer will stop the service.
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("Service command %ls complete.", argv[2]));
|
2012-07-09 16:00:03 -07:00
|
|
|
} else {
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG_WARN(("Service command not recognized: %ls.", argv[2]));
|
2012-01-04 20:19:17 -08:00
|
|
|
// result is already set to FALSE
|
2012-01-04 20:19:14 -08:00
|
|
|
}
|
|
|
|
|
2012-10-25 19:34:03 -07:00
|
|
|
LOG(("service command %ls complete with result: %ls.",
|
2012-01-04 20:19:17 -08:00
|
|
|
argv[1], (result ? L"Success" : L"Failure")));
|
2012-01-04 20:19:14 -08:00
|
|
|
return TRUE;
|
|
|
|
}
|