2020-07-21 16:38:00 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018-2020 WireGuard LLC. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
2020-07-21 16:57:36 +02:00
|
|
|
#include "pch.h"
|
2020-07-21 16:38:00 +02:00
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
static WINTUN_STATUS
|
2020-07-21 18:19:15 +02:00
|
|
|
OpenKeyWait(_In_ HKEY Key, _Inout_z_ WCHAR *Path, _In_ DWORD Access, _In_ ULONGLONG Deadline, _Out_ HKEY *KeyOut)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
DWORD Result;
|
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-10-14 13:04:29 +02:00
|
|
|
return LOG_LAST_ERROR(L"Failed to create event");
|
2020-07-21 16:38:00 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
Result = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_NAME, Event, TRUE);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG_ERROR(L"Failed to setup notification", 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
|
|
|
|
|
|
|
HKEY Subkey;
|
|
|
|
Result = RegOpenKeyExW(Key, Path, 0, PathNext ? KEY_NOTIFY : Access, &Subkey);
|
|
|
|
if (Result == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
if (PathNext)
|
|
|
|
{
|
|
|
|
Result = OpenKeyWait(Subkey, PathNext + 1, Access, Deadline, KeyOut);
|
|
|
|
RegCloseKey(Subkey);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*KeyOut = Subkey;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (Result != ERROR_FILE_NOT_FOUND && Result != ERROR_PATH_NOT_FOUND)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG_ERROR(L"Failed to open", 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
|
|
|
|
|
|
|
LONGLONG TimeLeft = Deadline - GetTickCount64();
|
|
|
|
if (TimeLeft < 0)
|
|
|
|
TimeLeft = 0;
|
|
|
|
if (WaitForSingleObject(Event, (DWORD)TimeLeft) != WAIT_OBJECT_0)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-15 11:32:06 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Timeout waiting");
|
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);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
WINTUN_STATUS
|
2020-07-21 16:38:00 +02:00
|
|
|
RegistryOpenKeyWait(
|
|
|
|
_In_ HKEY Key,
|
2020-07-24 08:10:00 +02:00
|
|
|
_In_z_count_c_(MAX_REG_PATH) const WCHAR *Path,
|
2020-07-21 16:38:00 +02:00
|
|
|
_In_ DWORD Access,
|
|
|
|
_In_ DWORD Timeout,
|
|
|
|
_Out_ HKEY *KeyOut)
|
|
|
|
{
|
2020-07-24 08:10:00 +02:00
|
|
|
WCHAR Buf[MAX_REG_PATH];
|
2020-07-21 16:38:00 +02:00
|
|
|
wcscpy_s(Buf, _countof(Buf), Path);
|
|
|
|
return OpenKeyWait(Key, Buf, Access, GetTickCount64() + Timeout, KeyOut);
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
WINTUN_STATUS
|
2020-07-21 18:19:15 +02:00
|
|
|
RegistryGetString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
HANDLE Heap = GetProcessHeap();
|
|
|
|
|
|
|
|
if (wcsnlen(*Buf, Len) >= Len)
|
|
|
|
{
|
|
|
|
/* String is missing zero-terminator. */
|
2020-07-21 18:19:15 +02:00
|
|
|
WCHAR *BufZ = HeapAlloc(Heap, 0, ((size_t)Len + 1) * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!BufZ)
|
2020-10-15 12:38:05 +02:00
|
|
|
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
|
2020-07-21 16:38:00 +02:00
|
|
|
wmemcpy(BufZ, *Buf, Len);
|
|
|
|
BufZ[Len] = 0;
|
|
|
|
HeapFree(Heap, 0, *Buf);
|
|
|
|
*Buf = BufZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ValueType != REG_EXPAND_SZ)
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
|
|
|
/* ExpandEnvironmentStringsW() returns strlen on success or 0 on error. Bail out on empty input strings to
|
|
|
|
* disambiguate. */
|
|
|
|
if (!(*Buf)[0])
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
|
|
|
Len = Len * 2 + 64;
|
|
|
|
for (;;)
|
|
|
|
{
|
2020-07-21 18:19:15 +02:00
|
|
|
WCHAR *Expanded = HeapAlloc(Heap, 0, Len * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!Expanded)
|
2020-10-15 12:38:05 +02:00
|
|
|
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
|
2020-07-21 16:38:00 +02:00
|
|
|
DWORD Result = ExpandEnvironmentStringsW(*Buf, Expanded, Len);
|
|
|
|
if (!Result)
|
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
Result = LOG_LAST_ERROR(L"Failed to expand environment variables");
|
2020-07-21 16:38:00 +02:00
|
|
|
HeapFree(Heap, 0, Expanded);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
if (Result > Len)
|
|
|
|
{
|
|
|
|
HeapFree(Heap, 0, Expanded);
|
|
|
|
Len = Result;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
HeapFree(Heap, 0, *Buf);
|
|
|
|
*Buf = Expanded;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
WINTUN_STATUS
|
2020-07-21 18:19:15 +02:00
|
|
|
RegistryGetMultiString(_Inout_ WCHAR **Buf, _In_ DWORD Len, _In_ DWORD ValueType)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
HANDLE Heap = GetProcessHeap();
|
|
|
|
|
|
|
|
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-07-21 18:19:15 +02:00
|
|
|
WCHAR *BufZ = HeapAlloc(Heap, 0, ((size_t)Len + 2) * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!BufZ)
|
2020-10-15 12:38:05 +02:00
|
|
|
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
|
2020-07-21 16:38:00 +02:00
|
|
|
wmemcpy(BufZ, *Buf, Len);
|
|
|
|
BufZ[Len] = 0;
|
|
|
|
BufZ[Len + 1] = 0;
|
|
|
|
HeapFree(Heap, 0, *Buf);
|
|
|
|
*Buf = BufZ;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
if (i == Len)
|
|
|
|
{
|
|
|
|
/* Missing list terminator. */
|
2020-07-21 18:19:15 +02:00
|
|
|
WCHAR *BufZ = HeapAlloc(Heap, 0, ((size_t)Len + 1) * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!BufZ)
|
2020-10-15 12:38:05 +02:00
|
|
|
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
|
2020-07-21 16:38:00 +02:00
|
|
|
wmemcpy(BufZ, *Buf, Len);
|
|
|
|
BufZ[Len] = 0;
|
|
|
|
HeapFree(Heap, 0, *Buf);
|
|
|
|
*Buf = BufZ;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
if (!(*Buf)[i])
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanitize REG_SZ/REG_EXPAND_SZ and append a list terminator to make a multi-string. */
|
|
|
|
DWORD Result = RegistryGetString(Buf, Len, ValueType);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
|
|
|
return Result;
|
|
|
|
Len = (DWORD)wcslen(*Buf) + 1;
|
2020-07-21 18:19:15 +02:00
|
|
|
WCHAR *BufZ = HeapAlloc(Heap, 0, ((size_t)Len + 1) * sizeof(WCHAR));
|
2020-07-21 16:38:00 +02:00
|
|
|
if (!BufZ)
|
2020-10-15 12:38:05 +02:00
|
|
|
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
|
2020-07-21 16:38:00 +02:00
|
|
|
wmemcpy(BufZ, *Buf, Len);
|
|
|
|
BufZ[Len] = 0;
|
|
|
|
HeapFree(Heap, 0, *Buf);
|
|
|
|
*Buf = BufZ;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
static WINTUN_STATUS
|
2020-07-21 16:38:00 +02:00
|
|
|
RegistryQuery(
|
|
|
|
_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,
|
|
|
|
_Out_ void **Buf,
|
|
|
|
_Inout_ DWORD *BufLen)
|
|
|
|
{
|
|
|
|
HANDLE Heap = GetProcessHeap();
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
*Buf = HeapAlloc(Heap, 0, *BufLen);
|
|
|
|
if (!*Buf)
|
2020-10-15 12:38:05 +02:00
|
|
|
return LOG(WINTUN_LOG_ERR, L"Out of memory"), ERROR_OUTOFMEMORY;
|
2020-07-21 16:38:00 +02:00
|
|
|
LSTATUS Result = RegQueryValueExW(Key, Name, NULL, ValueType, (BYTE *)*Buf, BufLen);
|
|
|
|
if (Result == ERROR_SUCCESS)
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
HeapFree(Heap, 0, *Buf);
|
|
|
|
if (Result != ERROR_MORE_DATA)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_ERROR(L"Querying value failed", Result);
|
2020-07-21 16:38:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
WINTUN_STATUS
|
2020-07-21 18:19:15 +02:00
|
|
|
RegistryQueryString(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ WCHAR **Value)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
DWORD ValueType, Size = 256 * sizeof(WCHAR);
|
|
|
|
DWORD Result = RegistryQuery(Key, Name, &ValueType, Value, &Size);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
|
|
|
return Result;
|
|
|
|
switch (ValueType)
|
|
|
|
{
|
|
|
|
case REG_SZ:
|
|
|
|
case REG_EXPAND_SZ:
|
|
|
|
case REG_MULTI_SZ:
|
|
|
|
Result = RegistryGetString(Value, Size / sizeof(WCHAR), ValueType);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
|
|
|
HeapFree(GetProcessHeap(), 0, *Value);
|
|
|
|
return Result;
|
|
|
|
default:
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Value is not a string");
|
2020-07-21 16:38:00 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, *Value);
|
|
|
|
return ERROR_INVALID_DATATYPE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
WINTUN_STATUS
|
2020-07-21 18:19:15 +02:00
|
|
|
RegistryQueryStringWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD Timeout, _Out_ WCHAR **Value)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
DWORD Result;
|
|
|
|
ULONGLONG Deadline = GetTickCount64() + Timeout;
|
|
|
|
HANDLE Event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!Event)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_LAST_ERROR(L"Failed to create event");
|
2020-07-21 16:38:00 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
Result = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_LAST_SET, Event, TRUE);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG_ERROR(L"Failed to setup notification", 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
|
|
|
Result = RegistryQueryString(Key, Name, Value);
|
|
|
|
if (Result != ERROR_FILE_NOT_FOUND && Result != ERROR_PATH_NOT_FOUND)
|
|
|
|
break;
|
|
|
|
LONGLONG TimeLeft = Deadline - GetTickCount64();
|
|
|
|
if (TimeLeft < 0)
|
|
|
|
TimeLeft = 0;
|
|
|
|
if (WaitForSingleObject(Event, (DWORD)TimeLeft) != WAIT_OBJECT_0)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-15 11:32:06 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Timeout waiting");
|
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);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
WINTUN_STATUS
|
2020-07-21 18:19:15 +02:00
|
|
|
RegistryQueryDWORD(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _Out_ DWORD *Value)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
DWORD ValueType, Size = sizeof(DWORD);
|
|
|
|
DWORD Result = RegQueryValueExW(Key, Name, NULL, &ValueType, (BYTE *)Value, &Size);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_ERROR(L"Querying failed", Result);
|
2020-07-21 16:38:00 +02:00
|
|
|
if (ValueType != REG_DWORD)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Value is not a DWORD");
|
2020-07-21 16:38:00 +02:00
|
|
|
return ERROR_INVALID_DATATYPE;
|
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
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Value size is not 4 bytes");
|
2020-07-21 16:38:00 +02:00
|
|
|
return ERROR_INVALID_DATA;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
2020-07-21 16:38:00 +02:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-07-21 16:43:34 +02:00
|
|
|
WINTUN_STATUS
|
2020-07-21 18:19:15 +02:00
|
|
|
RegistryQueryDWORDWait(_In_ HKEY Key, _In_opt_z_ const WCHAR *Name, _In_ DWORD Timeout, _Out_ DWORD *Value)
|
2020-07-21 16:38:00 +02:00
|
|
|
{
|
|
|
|
DWORD Result;
|
|
|
|
ULONGLONG Deadline = GetTickCount64() + Timeout;
|
|
|
|
HANDLE Event = CreateEventW(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (!Event)
|
2020-10-14 13:04:29 +02:00
|
|
|
return LOG_LAST_ERROR(L"Failed to create event");
|
2020-07-21 16:38:00 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
Result = RegNotifyChangeKeyValue(Key, FALSE, REG_NOTIFY_CHANGE_LAST_SET, Event, TRUE);
|
|
|
|
if (Result != ERROR_SUCCESS)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-14 13:04:29 +02:00
|
|
|
LOG_ERROR(L"Failed to setup notification", 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
|
|
|
Result = RegistryQueryDWORD(Key, Name, Value);
|
|
|
|
if (Result != ERROR_FILE_NOT_FOUND && Result != ERROR_PATH_NOT_FOUND)
|
|
|
|
break;
|
|
|
|
LONGLONG TimeLeft = Deadline - GetTickCount64();
|
|
|
|
if (TimeLeft < 0)
|
|
|
|
TimeLeft = 0;
|
|
|
|
if (WaitForSingleObject(Event, (DWORD)TimeLeft) != WAIT_OBJECT_0)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-15 11:32:06 +02:00
|
|
|
LOG(WINTUN_LOG_ERR, L"Timeout waiting");
|
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);
|
|
|
|
return Result;
|
|
|
|
}
|