summaryrefslogtreecommitdiffstats
path: root/external/onlineupdate/install_updateservice.cxx
blob: c1d547156098a76e6ed24db775be390b18c6af8a (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 */

#include <sal/config.h>

#include <algorithm>
#include <cstddef>
#include <limits>
#include <memory>
#include <string>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <msiquery.h>

#include <pathhash.h>

// Replacements for functions used in
// workdir/UnpackedTarball/onlineupdate/onlineupdate/source/update/common/pathhash.cpp but not
// available here:

extern "C" wchar_t* _wcslwr(wchar_t* str)
{
    for (auto p = str; *p != L'\0'; ++p)
    {
        if (*p >= L'A' && *p <= L'Z')
        {
            *p += L'a' - L'A';
        }
    }
    return str;
};

extern "C" wchar_t* wcsncpy(wchar_t* strDest, wchar_t const* strSource, std::size_t count)
{
    for (std::size_t i = 0; i != count; ++i)
    {
        strDest[i] = *strSource;
        if (*strSource != L'\0')
        {
            ++strSource;
        }
    }
    return strDest;
}

namespace
{
bool getProperty(MSIHANDLE handle, wchar_t const* name, std::wstring* value)
{
    DWORD n = 0;
    if (MsiGetPropertyW(handle, name, const_cast<wchar_t*>(L""), &n) != ERROR_MORE_DATA
        || n == std::numeric_limits<DWORD>::max())
    {
        return false;
    }
    ++n;
    auto buf = std::make_unique<wchar_t[]>(n);
    if (MsiGetPropertyW(handle, name, buf.get(), &n) != ERROR_SUCCESS)
    {
        return false;
    }
    if (n != 0 && buf[n - 1] == L'\\')
    {
        --n;
    }
    value->assign(buf.get(), n);
    return true;
}

typedef std::unique_ptr<void, decltype(&CloseHandle)> CloseHandleGuard;

CloseHandleGuard guard(HANDLE handle) { return CloseHandleGuard(handle, CloseHandle); }

bool runExecutable(std::wstring const& installLocation, wchar_t const* argument)
{
    std::wstring cmdline(L"\"");
    cmdline += installLocation;
    cmdline += L"\\program\\update_service.exe\" ";
    cmdline += argument;
    auto const n = cmdline.size() + 1;
    auto const buf = std::make_unique<wchar_t[]>(n);
    std::copy_n(cmdline.data(), n, buf.get());
    STARTUPINFOW si{};
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi{};
    if (!CreateProcessW(nullptr, buf.get(), nullptr, nullptr, FALSE, CREATE_NO_WINDOW, nullptr,
                        nullptr, &si, &pi))
    {
        return false;
    }
    auto const g(guard(pi.hProcess));
    DWORD res = WaitForSingleObject(pi.hProcess, INFINITE);
    if (res != WAIT_OBJECT_0)
    {
        return false;
    }
    DWORD ec = 0;
    if (!GetExitCodeProcess(pi.hProcess, &ec))
    {
        return false;
    }
    if (ec != 0)
    {
        return false;
    }
    return true;
}

bool writeRegistry(std::wstring const& installLocation)
{
    WCHAR path[MAX_PATH + 1];
    if (!CalculateRegistryPathFromFilePath(installLocation.c_str(), path))
    {
        return false;
    }
    HKEY key;
    if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, (std::wstring(path) + L"\\0").c_str(), 0, nullptr,
                        REG_OPTION_NON_VOLATILE, KEY_WRITE | KEY_WOW64_64KEY, nullptr, &key,
                        nullptr)
        != ERROR_SUCCESS)
    {
        return false;
    }
    auto ok = true;
    if (RegSetValueExW(key, L"issuer", 0, REG_SZ,
                       reinterpret_cast<BYTE const*>(L"Certum Code Signing 2021 CA"),
                       sizeof L"Certum Code Signing 2021 CA")
        != ERROR_SUCCESS)
    {
        ok = false;
    }
    if (RegSetValueExW(key, L"name", 0, REG_SZ,
                       reinterpret_cast<BYTE const*>(L"The Document Foundation"),
                       sizeof L"The Document Foundation")
        != ERROR_SUCCESS)
    {
        ok = false;
    }
    if (RegCloseKey(key) != ERROR_SUCCESS)
    {
        ok = false;
    }
    return ok;
}

bool deleteRegistry(std::wstring const& installLocation)
{
    WCHAR path[MAX_PATH + 1];
    if (!CalculateRegistryPathFromFilePath(installLocation.c_str(), path))
    {
        return false;
    }
    if (RegDeleteTreeW(HKEY_LOCAL_MACHINE, path) != ERROR_SUCCESS)
    {
        return false;
    }
    return true;
}
}

extern "C" __declspec(dllexport) UINT __stdcall PrepareUpdateservice(MSIHANDLE handle)
{
    std::wstring loc;
    if (!getProperty(handle, L"INSTALLLOCATION", &loc))
    {
        return ERROR_INSTALL_FAILURE;
    }
    auto ok = true;
    if (MsiSetPropertyW(handle, L"install_updateservice", loc.c_str()) != ERROR_SUCCESS)
    {
        ok = false;
    }
    if (MsiSetPropertyW(handle, L"uninstall_updateservice", loc.c_str()) != ERROR_SUCCESS)
    {
        ok = false;
    }
    return ok ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
}

extern "C" __declspec(dllexport) UINT __stdcall InstallUpdateservice(MSIHANDLE handle)
{
    std::wstring loc;
    if (!getProperty(handle, L"CustomActionData", &loc))
    {
        return ERROR_INSTALL_FAILURE;
    }
    auto ok = true;
    if (!runExecutable(loc, L"install"))
    {
        ok = false;
    }
    if (!writeRegistry(loc))
    {
        ok = false;
    }
    return ok ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
}

extern "C" __declspec(dllexport) UINT __stdcall UninstallUpdateservice(MSIHANDLE handle)
{
    std::wstring loc;
    if (!getProperty(handle, L"CustomActionData", &loc))
    {
        return ERROR_INSTALL_FAILURE;
    }
    auto ok = true;
    if (!runExecutable(loc, L"uninstall"))
    {
        ok = false;
    }
    if (!deleteRegistry(loc))
    {
        ok = false;
    }
    return ok ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */