aboutsummaryrefslogblamecommitdiffstatshomepage
path: root/installer/customactions.c
blob: 7217fca8e18189716cb4eb3310467956332d897b (plain) (tree)
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
400
401
402
403


















































































































































































































































































































































































































                                                                                                                                                                                                           
// SPDX-License-Identifier: MIT
/*
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

#include <windows.h>
#include <msi.h>
#include <msidefs.h>
#include <msiquery.h>
#include <shlwapi.h>
#include <stdbool.h>
#include <tchar.h>

#define MANAGER_SERVICE_NAME TEXT("WireGuardManager")
#define TUNNEL_SERVICE_PREFIX TEXT("WireGuardTunnel$")
#define ENUM_SERVICE_STATUS_PROCESS_SIZE 0x10000

typedef enum
{
	LOG_LEVEL_INFO = 0,
	LOG_LEVEL_WARN,
	LOG_LEVEL_ERR
} log_level_t;

static TCHAR *format_message(const TCHAR *template, const DWORD_PTR argv[])
{
	TCHAR *formatted_message = NULL;
	if (!FormatMessage(
		FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_MAX_WIDTH_MASK,
		template,
		0,
		0,
		(VOID *)&formatted_message,
		0,
		(va_list *)argv))
		return NULL;
	return formatted_message;
}

static void log_message(MSIHANDLE installer, log_level_t level, const TCHAR *log_line)
{
	MSIHANDLE record = MsiCreateRecord(2);
	if (!record)
		return;
	TCHAR *template;
	INSTALLMESSAGE type;
	switch (level) {
	case LOG_LEVEL_INFO:
		template = TEXT("Custom action: [1]");
		type = INSTALLMESSAGE_INFO;
		break;
	case LOG_LEVEL_WARN:
		template = TEXT("Custom action warning: [1]");
		type = INSTALLMESSAGE_INFO;
		break;
	case LOG_LEVEL_ERR:
		template = TEXT("Custom action error: [1]");
		type = INSTALLMESSAGE_ERROR;
		break;
	default:
		goto cleanup;
	}
	MsiRecordSetString(record, 0, template);
	MsiRecordSetString(record, 1, log_line);
	MsiProcessMessage(installer, type, record);
cleanup:
	MsiCloseHandle(record);
}

static void log_messagef(MSIHANDLE installer, log_level_t level, const TCHAR *template, const DWORD_PTR argv[])
{
	TCHAR *formatted_message = format_message(template, argv);
	if (formatted_message) {
		log_message(installer, level, formatted_message);
		LocalFree(formatted_message);
	}
}

static void log_error(MSIHANDLE installer, DWORD error_code, const TCHAR *prefix)
{
	TCHAR *system_message = NULL;
	FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_MAX_WIDTH_MASK,
		NULL,
		HRESULT_FROM_SETUPAPI(error_code),
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(VOID *)&system_message,
		0,
		NULL);
	log_messagef(
		installer,
		LOG_LEVEL_ERR,
		system_message ? TEXT("%1: %3(Code 0x%2!08X!)") : TEXT("%1: Code 0x%2!08X!"),
		(DWORD_PTR[]){ (DWORD_PTR)prefix, error_code, (DWORD_PTR)system_message });
	LocalFree(system_message);
}

static void log_errorf(MSIHANDLE installer, DWORD error_code, const TCHAR *template, const DWORD_PTR argv[])
{
	TCHAR *formatted_message = format_message(template, argv);
	if (formatted_message) {
		log_error(installer, error_code, formatted_message);
		LocalFree(formatted_message);
	}
}

static bool is_valid_tunnel_name(const TCHAR *tunnel_name)
{
	for (size_t i = 0; ; i++) {
		TCHAR c = tunnel_name[i];
		if (!c)
			return i > 0;
		if (i >= 32)
			return false;
		if ((c < TEXT('a') || c > TEXT('z')) &&
			(c < TEXT('A') || c > TEXT('Z')) &&
			(c < TEXT('0') || c > TEXT('9')) &&
			c != TEXT('_') &&
			c != TEXT('=') &&
			c != TEXT('+') &&
			c != TEXT('.') &&
			c != TEXT('-'))
			return false;
	}
}

static void replace_msi_operators(TCHAR *dest, const TCHAR *src, size_t count)
{
	for (size_t i = 0; i < count; i++) {
		TCHAR c = src[i];
		switch (c) {
			case 0:
				dest[i] = 0;
				return;
			case TEXT('='):
			case TEXT('+'):
			case TEXT('-'):
				dest[i] = TEXT('_');
				break;
			default:
				dest[i] = c;
		}
	}
}

static bool is_service_started(MSIHANDLE installer, SC_HANDLE scm, const TCHAR *service_name)
{
	bool ret = false;
	SC_HANDLE service = NULL;
	SERVICE_STATUS_PROCESS service_status;
	DWORD service_status_size = 0;
	const DWORD_PTR log_argv[] = { (DWORD_PTR)__FUNCTION__, (DWORD_PTR)service_name };

	service = OpenService(scm, MANAGER_SERVICE_NAME, SERVICE_QUERY_STATUS);
	if (!service) {
		log_errorf(installer, GetLastError(), TEXT("%1!hs!: OpenService failed for service %2"), log_argv);
		goto cleanup;
	}
	if (!QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO, (LPBYTE)&service_status, sizeof(service_status), &service_status_size)) {
		log_errorf(installer, GetLastError(), TEXT("%1!hs!: QueryServiceStatusEx failed for service %2"), log_argv);
		goto cleanup;
	}
	if (service_status.dwCurrentState != SERVICE_STOPPED && service_status.dwCurrentState != SERVICE_STOP_PENDING)
		ret = true;

cleanup:
	if (service)
		CloseServiceHandle(service);
	return ret;
}

static UINT insert_service_control(MSIHANDLE installer, MSIHANDLE view, const TCHAR *service_name, bool start)
{
	UINT ret = ERROR_INSTALL_FAILURE;
	MSIHANDLE record = 0;
	const DWORD_PTR log_argv[] = { (DWORD_PTR)__FUNCTION__, (DWORD_PTR)service_name };
	size_t service_name_len;
	TCHAR *sanitized_service_name = NULL, *service_control_stop = NULL, *service_control_start = NULL;
	static unsigned int index = 0;

	record = MsiCreateRecord(5);
	if (!record)
		goto cleanup;

	service_name_len = _tcslen(service_name) + 1;
	sanitized_service_name = LocalAlloc(LMEM_FIXED, sizeof(TCHAR) * service_name_len);
	if (!sanitized_service_name) {
		ret = GetLastError();
		log_errorf(installer, ret, TEXT("%1!hs!: LocalAlloc failed for service %2"), log_argv);
		goto cleanup;
	}
	replace_msi_operators(sanitized_service_name, service_name, service_name_len);

	log_messagef(installer, LOG_LEVEL_INFO, TEXT("%1!hs!: Scheduling stop on upgrade or removal on uninstall of service %2"), log_argv);
	service_control_stop = format_message(TEXT("stop_%1%2!u!"), (DWORD_PTR[]){ (DWORD_PTR)sanitized_service_name, index++ });
	if (!service_control_stop) {
		ret = GetLastError();
		log_errorf(installer, ret, TEXT("%1!hs!: FormatMessage failed for service %2"), log_argv);
		goto cleanup;
	}
	MsiRecordSetString (record, 1/*ServiceControl*/, service_control_stop);
	MsiRecordSetString (record, 2/*Name          */, service_name);
	MsiRecordSetInteger(record, 3/*Event         */, msidbServiceControlEventStop | msidbServiceControlEventUninstallStop | msidbServiceControlEventUninstallDelete);
	MsiRecordSetString (record, 4/*Component_    */, TEXT("WireGuardExecutable"));
	MsiRecordSetInteger(record, 5/*Wait          */, 1); /* Waits 30 seconds. */
	ret = MsiViewExecute(view, record);
	if (ret != ERROR_SUCCESS) {
		log_errorf(installer, ret, TEXT("%1!hs!: MsiViewExecute failed for service %2"), log_argv);
		goto cleanup;
	}
	if (!start)
		goto cleanup;

	log_messagef(installer, LOG_LEVEL_INFO, TEXT("%1!hs!: Scheduling start on upgrade of service %2"), log_argv);
	service_control_start = format_message(TEXT("start_%1%2!u!"), (DWORD_PTR[]){ (DWORD_PTR)sanitized_service_name, index++ });
	if (!service_control_start) {
		ret = GetLastError();
		log_errorf(installer, ret, TEXT("%1!hs!: FormatMessage failed for service %2"), log_argv);
		goto cleanup;
	}
	MsiRecordSetString (record, 1/*ServiceControl*/, service_control_start);
	MsiRecordSetString (record, 2/*Name          */, service_name);
	MsiRecordSetInteger(record, 3/*Event         */, msidbServiceControlEventStart);
	MsiRecordSetString (record, 4/*Component_    */, TEXT("WireGuardExecutable"));
	MsiRecordSetInteger(record, 5/*Wait          */, 0); /* No wait, so that failure to restart again isn't fatal. */
	ret = MsiViewExecute(view, record);
	if (ret != ERROR_SUCCESS) {
		log_errorf(installer, ret, TEXT("%1!hs!: MsiViewExecute failed for service %2"), log_argv);
		goto cleanup;
	}

cleanup:
	if (service_control_start)
		LocalFree(service_control_start);
	if (service_control_stop)
		LocalFree(service_control_stop);
	if (sanitized_service_name)
		LocalFree(sanitized_service_name);
	if (record)
		MsiCloseHandle(record);
	return ret;
}

static bool remove_folder(MSIHANDLE installer, TCHAR path[MAX_PATH])
{
	HANDLE find_handle;
	WIN32_FIND_DATA find_data;
	TCHAR *path_end;
	const DWORD_PTR log_argv[] = { (DWORD_PTR)__FUNCTION__, (DWORD_PTR)path };

	path_end = path + _tcsnlen(path, MAX_PATH);
	if (!PathAppend(path, TEXT("*.*"))) {
		log_messagef(installer, LOG_LEVEL_ERR, TEXT("%1!hs!: PathAppend(%2) failed"), log_argv);
		return false;
	}
	find_handle = FindFirstFileEx(path, FindExInfoBasic, &find_data, FindExSearchNameMatch, NULL, 0);
	if (find_handle == INVALID_HANDLE_VALUE) {
		log_errorf(installer, GetLastError(), TEXT("%1!hs!: FindFirstFileEx(%2) failed"), log_argv);
		return false;
	}
	do {
		if (find_data.cFileName[0] == TEXT('.') && (!find_data.cFileName[1] || (find_data.cFileName[1] == TEXT('.') && !find_data.cFileName[2])))
			continue;

		path_end[0] = 0;
		if (!PathAppend(path, find_data.cFileName)) {
			log_messagef(installer, LOG_LEVEL_ERR, TEXT("%1!hs!: PathAppend(%2) failed"), log_argv);
			continue;
		}

		if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
			remove_folder(installer, path);
			continue;
		}

		if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) && !SetFileAttributes(path, find_data.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY))
			log_errorf(installer, GetLastError(), TEXT("%1!hs!: SetFileAttributes(%2) failed"), log_argv);

		if (DeleteFile(path))
			log_messagef(installer, LOG_LEVEL_INFO, TEXT("%1!hs!: %2 removed"), log_argv);
		else
			log_errorf(installer, GetLastError(), TEXT("%1!hs!: DeleteFile(%2) failed"), log_argv);
	} while (FindNextFile(find_handle, &find_data));
	FindClose(find_handle);

	path_end[0] = 0;
	if (RemoveDirectory(path)) {
		log_messagef(installer, LOG_LEVEL_INFO, TEXT("%1!hs!: %2 removed"), log_argv);
		return true;
	} else {
		log_errorf(installer, GetLastError(), TEXT("%1!hs!: RemoveDirectory(%2) failed"), log_argv);
		return false;
	}
}

__declspec(dllexport) UINT __stdcall EvaluateWireGuardServices(MSIHANDLE installer)
{
	UINT ret = ERROR_INSTALL_FAILURE;
	BOOL is_com_initialized = SUCCEEDED(CoInitialize(NULL));
	MSIHANDLE db = 0, view = 0;
	SC_HANDLE scm = NULL;
	ENUM_SERVICE_STATUS_PROCESS *service_status = NULL;
	DWORD service_status_resume = 0;
	const DWORD_PTR log_argv[] = { (DWORD_PTR)__FUNCTION__ };

	db = MsiGetActiveDatabase(installer);
	if (!db) {
		log_messagef(installer, LOG_LEVEL_ERR, TEXT("%1!hs!: MsiGetActiveDatabase failed"), log_argv);
		goto cleanup;
	}
	ret = MsiDatabaseOpenView(
		db,
		TEXT("INSERT INTO `ServiceControl` (`ServiceControl`, `Name`, `Event`, `Component_`, `Wait`) VALUES(?, ?, ?, ?, ?) TEMPORARY"),
		&view);
	if (ret != ERROR_SUCCESS) {
		log_errorf(installer, ret, TEXT("%1!hs!: MsiDatabaseOpenView failed"), log_argv);
		goto cleanup;
	}
	scm = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);
	if (!scm) {
		ret = GetLastError();
		log_errorf(installer, ret, TEXT("%1!hs!: OpenSCManager failed"), log_argv);
		goto cleanup;
	}

	insert_service_control(installer, view, MANAGER_SERVICE_NAME, is_service_started(installer, scm, MANAGER_SERVICE_NAME));

	service_status = LocalAlloc(LMEM_FIXED, ENUM_SERVICE_STATUS_PROCESS_SIZE);
	if (!service_status) {
		ret = GetLastError();
		log_errorf(installer, ret, TEXT("%1!hs!: LocalAlloc failed"), log_argv);
		goto cleanup;
	}
	for (bool more_services = true; more_services;) {
		DWORD service_status_size = 0, service_status_count = 0;
		if (EnumServicesStatusEx(
			scm,
			SC_ENUM_PROCESS_INFO,
			SERVICE_WIN32,
			SERVICE_STATE_ALL,
			(LPBYTE)service_status,
			ENUM_SERVICE_STATUS_PROCESS_SIZE,
			&service_status_size,
			&service_status_count,
			&service_status_resume,
			NULL))
			more_services = false;
		else {
			ret = GetLastError();
			if (ret != ERROR_MORE_DATA) {
				log_errorf(installer, ret, TEXT("%1!hs!: EnumServicesStatusEx failed"), log_argv);
				break;
			}
		}

		for (DWORD i = 0; i < service_status_count; i++) {
			if (_tcsnicmp(service_status[i].lpServiceName, TUNNEL_SERVICE_PREFIX, _countof(TUNNEL_SERVICE_PREFIX) - 1) == 0) {
				const TCHAR *tunnel_name = service_status[i].lpServiceName + _countof(TUNNEL_SERVICE_PREFIX) - 1;
				if (is_valid_tunnel_name(tunnel_name))
					insert_service_control(
						installer,
						view,
						service_status[i].lpServiceName,
						service_status[i].ServiceStatusProcess.dwCurrentState != SERVICE_STOPPED && service_status[i].ServiceStatusProcess.dwCurrentState != SERVICE_STOP_PENDING);
			}
		}
	}

	ret = ERROR_SUCCESS;

cleanup:
	if (service_status)
		LocalFree(service_status);
	if (scm)
		CloseServiceHandle(scm);
	if (view)
		MsiCloseHandle(view);
	if (db)
		MsiCloseHandle(db);
	if (is_com_initialized)
		CoUninitialize();
	return ret == ERROR_SUCCESS ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
}

__declspec(dllexport) UINT __stdcall RemoveConfigFolder(MSIHANDLE installer)
{
	LSTATUS result;
	TCHAR path[MAX_PATH];

	result = SHRegGetPath(
		HKEY_LOCAL_MACHINE,
		TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\S-1-5-18"),
		TEXT("ProfileImagePath"),
		path,
		0);
	if (result != ERROR_SUCCESS) {
		log_errorf(installer, result, TEXT("%1!hs!: SHRegGetPath failed"), (DWORD_PTR[]){ (DWORD_PTR)__FUNCTION__ });
		return ERROR_SUCCESS;
	}
	PathAppend(path, TEXT("AppData\\Local\\WireGuard"));
	remove_folder(installer, path);
	return ERROR_SUCCESS;
}