2020-07-21 16:38:00 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
2021-01-30 16:45:26 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2020-07-21 16:38:00 +02:00
|
|
|
*/
|
|
|
|
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "entry.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "registry.h"
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <wchar.h>
|
2020-07-21 16:38:00 +02:00
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
static _Return_type_success_(return != NULL) HKEY
|
|
|
|
OpenKeyWait(_In_ HKEY Key, _Inout_z_ WCHAR *Path, _In_ DWORD Access, _In_ ULONGLONG Deadline)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-07-21 18:19:15 +02:00
|
|
|
WCHAR *PathNext = wcschr(Path, L'\\');
|
2020-07-21 16:38:00 +02:00
|
|
|
if (PathNext)
|
|
|
|
*PathNext = 0;
|
|
|
|
|
|
|
|
HANDLE Event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!Event)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
|
|
|
LOG_LAST_ERROR(L"Failed to create event");
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_NAME, Event, TRUE);
|
|
|
|
if (LastError != ERROR_SUCCESS)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG_ERROR(LastError, L"Failed to setup registry key %.*s notification", MAX_REG_PATH, RegPath);
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
|
|
|
|
HKEY Subkey;
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = RegOpenKeyExW(Key, Path, 0, PathNext ? KEY_NOTIFY : Access, &Subkey);
|
|
|
|
if (LastError == ERROR_SUCCESS)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
if (PathNext)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
HKEY KeyOut = OpenKeyWait(Subkey, PathNext + 1, Access, Deadline);
|
|
|
|
if (KeyOut)
|
|
|
|
{
|
|
|
|
RegCloseKey(Subkey);
|
|
|
|
CloseHandle(Event);
|
|
|
|
return KeyOut;
|
|
|
|
}
|
|
|
|
LastError = GetLastError();
|
|
|
|
break;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
else
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
|
|
|
CloseHandle(Event);
|
|
|
|
return Subkey;
|
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
if (LastError != ERROR_FILE_NOT_FOUND && LastError != ERROR_PATH_NOT_FOUND)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG_ERROR(LastError, L"Failed to open registry key %.*s\\%s", MAX_REG_PATH, RegPath, Path);
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
|
|
|
|
LONGLONG TimeLeft = Deadline - GetTickCount64();
|
|
|
|
if (TimeLeft < 0)
|
|
|
|
TimeLeft = 0;
|
2021-02-02 13:12:45 +01:00
|
|
|
DWORD Result = WaitForSingleObject(Event, (DWORD)TimeLeft);
|
|
|
|
if (Result != WAIT_OBJECT_0)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG(WINTUN_LOG_ERR,
|
|
|
|
L"Timeout waiting for registry key %.*s\\%s (status: 0x%x)",
|
|
|
|
MAX_REG_PATH,
|
|
|
|
RegPath,
|
|
|
|
Path,
|
|
|
|
Result);
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
CloseHandle(Event);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return NULL;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != NULL) HKEY
|
|
|
|
RegistryOpenKeyWait(_In_ HKEY Key, _In_z_ const WCHAR *Path, _In_ DWORD Access, _In_ DWORD Timeout)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
2020-07-24 08:10:00 +02:00
|
|
|
WCHAR Buf[MAX_REG_PATH];
|
2020-10-31 18:13:36 +01:00
|
|
|
if (wcsncpy_s(Buf, _countof(Buf), Path, _TRUNCATE) == STRUNCATE)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Registry path too long: %s", Path);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return OpenKeyWait(Key, Buf, Access, GetTickCount64() + Timeout);
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != FALSE) BOOL RegistryGetString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
if (wcsnlen(*Buf, Len) >= Len)
|
|
|
|
{
|
|
|
|
/* String is missing zero-terminator. */
|
2020-11-04 12:55:25 +01:00
|
|
|
WCHAR *BufZ = Alloc(((size_t)Len + 1) * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!BufZ)
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
2020-07-21 16:38:00 +02:00
|
|
|
wmemcpy(BufZ, *Buf, Len);
|
|
|
|
BufZ[Len] = 0;
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(*Buf);
|
2020-07-21 16:38:00 +02:00
|
|
|
*Buf = BufZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ValueType != REG_EXPAND_SZ)
|
2020-11-03 12:29:34 +01:00
|
|
|
return TRUE;
|
2020-07-21 16:38:00 +02:00
|
|
|
|
|
|
|
/* ExpandEnvironmentStringsW() returns strlen on success or 0 on error. Bail out on empty input strings to
|
|
|
|
* disambiguate. */
|
|
|
|
if (!(*Buf)[0])
|
2020-11-03 12:29:34 +01:00
|
|
|
return TRUE;
|
2020-07-21 16:38:00 +02:00
|
|
|
|
|
|
|
Len = Len * 2 + 64;
|
|
|
|
for (;;)
|
|
|
|
{
|
2020-11-04 12:55:25 +01:00
|
|
|
WCHAR *Expanded = Alloc(Len * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!Expanded)
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
2020-07-21 16:38:00 +02:00
|
|
|
DWORD Result = ExpandEnvironmentStringsW(*Buf, Expanded, Len);
|
|
|
|
if (!Result)
|
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LOG_LAST_ERROR(L"Failed to expand environment variables: %s", *Buf);
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(Expanded);
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
if (Result > Len)
|
|
|
|
{
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(Expanded);
|
2020-07-21 16:38:00 +02:00
|
|
|
Len = Result;
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(*Buf);
|
2020-07-21 16:38:00 +02:00
|
|
|
*Buf = Expanded;
|
2020-11-03 12:29:34 +01:00
|
|
|
return TRUE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != FALSE) BOOL
|
|
|
|
RegistryGetMultiString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
if (ValueType == REG_MULTI_SZ)
|
|
|
|
{
|
|
|
|
for (size_t i = 0;; i += wcsnlen(*Buf + i, Len - i) + 1)
|
|
|
|
{
|
|
|
|
if (i > Len)
|
|
|
|
{
|
|
|
|
/* Missing string and list terminators. */
|
2020-11-04 12:55:25 +01:00
|
|
|
WCHAR *BufZ = Alloc(((size_t)Len + 2) * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!BufZ)
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
2020-07-21 16:38:00 +02:00
|
|
|
wmemcpy(BufZ, *Buf, Len);
|
|
|
|
BufZ[Len] = 0;
|
|
|
|
BufZ[Len + 1] = 0;
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(*Buf);
|
2020-07-21 16:38:00 +02:00
|
|
|
*Buf = BufZ;
|
2020-11-03 12:29:34 +01:00
|
|
|
return TRUE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
if (i == Len)
|
|
|
|
{
|
|
|
|
/* Missing list terminator. */
|
2020-11-04 12:55:25 +01:00
|
|
|
WCHAR *BufZ = Alloc(((size_t)Len + 1) * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!BufZ)
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
2020-07-21 16:38:00 +02:00
|
|
|
wmemcpy(BufZ, *Buf, Len);
|
|
|
|
BufZ[Len] = 0;
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(*Buf);
|
2020-07-21 16:38:00 +02:00
|
|
|
*Buf = BufZ;
|
2020-11-03 12:29:34 +01:00
|
|
|
return TRUE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
if (!(*Buf)[i])
|
2020-11-03 12:29:34 +01:00
|
|
|
return TRUE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanitize REG_SZ/REG_EXPAND_SZ and append a list terminator to make a multi-string. */
|
2020-11-03 12:29:34 +01:00
|
|
|
if (!RegistryGetString(Buf, Len, ValueType))
|
|
|
|
return FALSE;
|
2020-07-21 16:38:00 +02:00
|
|
|
Len = (DWORD)wcslen(*Buf) + 1;
|
2020-11-04 12:55:25 +01:00
|
|
|
WCHAR *BufZ = Alloc(((size_t)Len + 1) * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!BufZ)
|
2020-11-03 12:29:34 +01:00
|
|
|
return FALSE;
|
2020-07-21 16:38:00 +02:00
|
|
|
wmemcpy(BufZ, *Buf, Len);
|
|
|
|
BufZ[Len] = 0;
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(*Buf);
|
2020-07-21 16:38:00 +02:00
|
|
|
*Buf = BufZ;
|
2020-11-03 12:29:34 +01:00
|
|
|
return TRUE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
static _Return_type_success_(return != NULL) void *RegistryQuery(
|
2020-07-21 16:38:00 +02:00
|
|
|
_In_ HKEY Key,
|
2020-07-21 18:19:15 +02:00
|
|
|
_In_opt_z_ const WCHAR *Name,
|
2020-07-21 16:38:00 +02:00
|
|
|
_Out_opt_ DWORD *ValueType,
|
2020-10-23 22:04:40 +02:00
|
|
|
_Inout_ DWORD *BufLen,
|
|
|
|
_In_ BOOL Log)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
2020-11-04 12:55:25 +01:00
|
|
|
BYTE *p = Alloc(*BufLen);
|
2020-10-30 13:08:57 +01:00
|
|
|
if (!p)
|
2020-11-03 12:29:34 +01:00
|
|
|
return NULL;
|
|
|
|
LSTATUS LastError = RegQueryValueExW(Key, Name, NULL, ValueType, p, BufLen);
|
|
|
|
if (LastError == ERROR_SUCCESS)
|
|
|
|
return p;
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(p);
|
2020-11-03 12:29:34 +01:00
|
|
|
if (LastError != ERROR_MORE_DATA)
|
|
|
|
{
|
|
|
|
if (Log)
|
2021-02-02 13:12:45 +01:00
|
|
|
{
|
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG_ERROR(LastError, L"Querying registry value %.*s\\%s failed", MAX_REG_PATH, RegPath, Name);
|
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(
|
|
|
|
return != NULL) WCHAR *RegistryQueryString(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ BOOL Log)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError, ValueType, Size = 256 * sizeof(WCHAR);
|
|
|
|
WCHAR *Value = RegistryQuery(Key, Name, &ValueType, &Size, Log);
|
|
|
|
if (!Value)
|
|
|
|
return NULL;
|
2020-07-21 16:38:00 +02:00
|
|
|
switch (ValueType)
|
|
|
|
{
|
|
|
|
case REG_SZ:
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
case REG_MULTI_SZ:
|
2020-11-03 12:29:34 +01:00
|
|
|
if (RegistryGetString(&Value, Size / sizeof(WCHAR), ValueType))
|
|
|
|
return Value;
|
|
|
|
LastError = GetLastError();
|
|
|
|
break;
|
2021-02-02 13:12:45 +01:00
|
|
|
default: {
|
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG(WINTUN_LOG_ERR,
|
|
|
|
L"Registry value %.*s\\%s is not a string (type: %u)",
|
|
|
|
MAX_REG_PATH,
|
|
|
|
RegPath,
|
|
|
|
Name,
|
|
|
|
ValueType);
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_INVALID_DATATYPE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
2021-02-02 13:12:45 +01:00
|
|
|
}
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(Value);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return NULL;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(
|
|
|
|
return != NULL) WCHAR *RegistryQueryStringWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD Timeout)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-07-21 16:38:00 +02:00
|
|
|
ULONGLONG Deadline = GetTickCount64() + Timeout;
|
|
|
|
HANDLE Event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!Event)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
|
|
|
LOG_LAST_ERROR(L"Failed to create event");
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_LAST_SET, Event, TRUE);
|
|
|
|
if (LastError != ERROR_SUCCESS)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG_ERROR(LastError, L"Failed to setup registry key %.*s notification", MAX_REG_PATH, RegPath);
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
WCHAR *Value = RegistryQueryString(Key, Name, FALSE);
|
|
|
|
if (Value)
|
|
|
|
{
|
|
|
|
CloseHandle(Event);
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
LastError = GetLastError();
|
|
|
|
if (LastError != ERROR_FILE_NOT_FOUND && LastError != ERROR_PATH_NOT_FOUND)
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
|
|
|
LONGLONG TimeLeft = Deadline - GetTickCount64();
|
|
|
|
if (TimeLeft < 0)
|
|
|
|
TimeLeft = 0;
|
2021-02-02 13:12:45 +01:00
|
|
|
DWORD Result = WaitForSingleObject(Event, (DWORD)TimeLeft);
|
|
|
|
if (Result != WAIT_OBJECT_0)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG(WINTUN_LOG_ERR,
|
|
|
|
L"Timeout waiting for registry value %.*s\\%s (status: 0x%x)",
|
|
|
|
MAX_REG_PATH,
|
|
|
|
RegPath,
|
|
|
|
Name,
|
|
|
|
Result);
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
CloseHandle(Event);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return NULL;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != FALSE) BOOL
|
|
|
|
RegistryQueryDWORD(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ DWORD *Value, _In_ BOOL Log)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
DWORD ValueType, Size = sizeof(DWORD);
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError = RegQueryValueExW(Key, Name, NULL, &ValueType, (BYTE *)Value, &Size);
|
|
|
|
if (LastError != ERROR_SUCCESS)
|
2020-10-29 12:04:34 +01:00
|
|
|
{
|
|
|
|
if (Log)
|
2021-02-02 13:12:45 +01:00
|
|
|
{
|
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG_ERROR(LastError, L"Querying registry value %.*s\\%s failed", MAX_REG_PATH, RegPath, Name);
|
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return FALSE;
|
2020-10-29 12:04:34 +01:00
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
if (ValueType != REG_DWORD)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG(WINTUN_LOG_ERR, L"Value %.*s\\%s is not a DWORD (type: %u)", MAX_REG_PATH, RegPath, Name, ValueType);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(ERROR_INVALID_DATATYPE);
|
|
|
|
return FALSE;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
if (Size != sizeof(DWORD))
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG(WINTUN_LOG_ERR, L"Value %.*s\\%s size is not 4 bytes (size: %u)", MAX_REG_PATH, RegPath, Name, Size);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(ERROR_INVALID_DATA);
|
|
|
|
return FALSE;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
return TRUE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != FALSE) BOOL
|
|
|
|
RegistryQueryDWORDWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD Timeout, _Out_ DWORD *Value)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-07-21 16:38:00 +02:00
|
|
|
ULONGLONG Deadline = GetTickCount64() + Timeout;
|
|
|
|
HANDLE Event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!Event)
|
2020-11-03 12:29:34 +01:00
|
|
|
{
|
|
|
|
LOG_LAST_ERROR(L"Failed to create event");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_LAST_SET, Event, TRUE);
|
|
|
|
if (LastError != ERROR_SUCCESS)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG_ERROR(LastError, L"Failed to setup registry key %.*s notification", MAX_REG_PATH, RegPath);
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
if (RegistryQueryDWORD(Key, Name, Value, FALSE))
|
|
|
|
{
|
|
|
|
CloseHandle(Event);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
LastError = GetLastError();
|
|
|
|
if (LastError != ERROR_FILE_NOT_FOUND && LastError != ERROR_PATH_NOT_FOUND)
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
|
|
|
LONGLONG TimeLeft = Deadline - GetTickCount64();
|
|
|
|
if (TimeLeft < 0)
|
|
|
|
TimeLeft = 0;
|
2021-02-02 13:12:45 +01:00
|
|
|
DWORD Result = WaitForSingleObject(Event, (DWORD)TimeLeft);
|
|
|
|
if (Result != WAIT_OBJECT_0)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
WCHAR RegPath[MAX_REG_PATH];
|
|
|
|
LoggerGetRegistryKeyPath(Key, RegPath);
|
|
|
|
LOG(WINTUN_LOG_ERR,
|
|
|
|
L"Timeout waiting registry value %.*s\\%s (status: 0x%x)",
|
|
|
|
MAX_REG_PATH,
|
|
|
|
RegPath,
|
|
|
|
Name,
|
|
|
|
Result);
|
2020-07-21 16:38:00 +02:00
|
|
|
break;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
CloseHandle(Event);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return FALSE;
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|