2020-10-13 19:40:52 +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-10-13 19:40:52 +02:00
|
|
|
*/
|
|
|
|
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "logger.h"
|
2021-07-28 20:20:09 +02:00
|
|
|
#include "adapter.h"
|
2021-02-02 13:12:45 +01:00
|
|
|
#include "ntdll.h"
|
2020-10-31 11:55:26 +01:00
|
|
|
#include <Windows.h>
|
2021-10-12 07:21:31 +02:00
|
|
|
#include <iphlpapi.h>
|
2021-02-02 13:12:45 +01:00
|
|
|
#include <winternl.h>
|
2020-11-04 12:55:25 +01:00
|
|
|
#include <wchar.h>
|
2021-07-23 18:19:00 +02:00
|
|
|
#include <stdlib.h>
|
2020-10-13 19:40:52 +02:00
|
|
|
|
2020-10-16 14:17:22 +02:00
|
|
|
static BOOL CALLBACK
|
2021-10-12 07:21:31 +02:00
|
|
|
NopLogger(_In_ WINTUN_LOGGER_LEVEL Level, _In_ DWORD64 Timestamp, _In_z_ LPCWSTR LogLine)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-10-16 14:17:22 +02:00
|
|
|
return TRUE;
|
2020-10-13 19:40:52 +02:00
|
|
|
}
|
|
|
|
|
2020-11-04 15:45:27 +01:00
|
|
|
WINTUN_LOGGER_CALLBACK Logger = NopLogger;
|
2020-10-13 19:40:52 +02:00
|
|
|
|
2021-10-12 07:21:31 +02:00
|
|
|
static DWORD64 Now(VOID)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER Timestamp;
|
|
|
|
NtQuerySystemTime(&Timestamp);
|
|
|
|
return Timestamp.QuadPart;
|
|
|
|
}
|
|
|
|
|
2021-07-28 20:20:09 +02:00
|
|
|
_Use_decl_annotations_
|
|
|
|
VOID WINAPI
|
|
|
|
WintunSetLogger(WINTUN_LOGGER_CALLBACK NewLogger)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2020-11-02 17:42:52 +01:00
|
|
|
if (!NewLogger)
|
|
|
|
NewLogger = NopLogger;
|
2020-10-13 19:40:52 +02:00
|
|
|
Logger = NewLogger;
|
|
|
|
}
|
|
|
|
|
2021-02-02 13:12:45 +01:00
|
|
|
static VOID
|
2021-07-28 20:20:09 +02:00
|
|
|
StrTruncate(_Inout_count_(StrChars) LPWSTR Str, _In_ SIZE_T StrChars)
|
2021-02-02 13:12:45 +01:00
|
|
|
{
|
|
|
|
Str[StrChars - 2] = L'\u2026'; /* Horizontal Ellipsis */
|
|
|
|
Str[StrChars - 1] = 0;
|
|
|
|
}
|
|
|
|
|
2021-07-28 20:20:09 +02:00
|
|
|
_Use_decl_annotations_
|
|
|
|
DWORD
|
2021-10-12 07:21:31 +02:00
|
|
|
LoggerLog(WINTUN_LOGGER_LEVEL Level, LPCWSTR LogLine)
|
2020-11-04 12:24:46 +01:00
|
|
|
{
|
|
|
|
DWORD LastError = GetLastError();
|
2021-10-12 07:21:31 +02:00
|
|
|
Logger(Level, Now(), LogLine);
|
2020-11-04 12:24:46 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return LastError;
|
|
|
|
}
|
|
|
|
|
2021-07-28 20:20:09 +02:00
|
|
|
_Use_decl_annotations_
|
|
|
|
DWORD
|
2021-10-12 07:21:31 +02:00
|
|
|
LoggerLogV(WINTUN_LOGGER_LEVEL Level, LPCWSTR Format, va_list Args)
|
2021-02-02 13:12:45 +01:00
|
|
|
{
|
|
|
|
DWORD LastError = GetLastError();
|
|
|
|
WCHAR LogLine[0x400];
|
|
|
|
if (_vsnwprintf_s(LogLine, _countof(LogLine), _TRUNCATE, Format, Args) == -1)
|
|
|
|
StrTruncate(LogLine, _countof(LogLine));
|
2021-10-12 07:21:31 +02:00
|
|
|
Logger(Level, Now(), LogLine);
|
2021-02-02 13:12:45 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return LastError;
|
|
|
|
}
|
|
|
|
|
2021-07-28 20:20:09 +02:00
|
|
|
_Use_decl_annotations_
|
|
|
|
DWORD
|
2021-10-12 07:21:31 +02:00
|
|
|
LoggerError(DWORD Error, LPCWSTR Prefix)
|
2020-10-13 19:40:52 +02:00
|
|
|
{
|
2021-07-28 20:20:09 +02:00
|
|
|
LPWSTR SystemMessage = NULL, FormattedMessage = NULL;
|
2020-10-13 19:40:52 +02:00
|
|
|
FormatMessageW(
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
|
|
|
NULL,
|
|
|
|
HRESULT_FROM_SETUPAPI(Error),
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
2021-07-28 20:20:09 +02:00
|
|
|
(VOID *)&SystemMessage,
|
2020-10-13 19:40:52 +02:00
|
|
|
0,
|
|
|
|
NULL);
|
|
|
|
FormatMessageW(
|
|
|
|
FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY |
|
|
|
|
FORMAT_MESSAGE_MAX_WIDTH_MASK,
|
2021-10-12 07:21:31 +02:00
|
|
|
SystemMessage ? L"%1: %3(Code 0x%2!08X!)" : L"%1: Code 0x%2!08X!",
|
2020-10-13 19:40:52 +02:00
|
|
|
0,
|
|
|
|
0,
|
2021-07-28 20:20:09 +02:00
|
|
|
(VOID *)&FormattedMessage,
|
2020-10-13 19:40:52 +02:00
|
|
|
0,
|
2021-10-12 07:21:31 +02:00
|
|
|
(va_list *)(DWORD_PTR[]){ (DWORD_PTR)Prefix, (DWORD_PTR)Error, (DWORD_PTR)SystemMessage });
|
2020-10-13 19:40:52 +02:00
|
|
|
if (FormattedMessage)
|
2021-10-12 07:21:31 +02:00
|
|
|
Logger(WINTUN_LOG_ERR, Now(), FormattedMessage);
|
2020-10-13 19:40:52 +02:00
|
|
|
LocalFree(FormattedMessage);
|
|
|
|
LocalFree(SystemMessage);
|
|
|
|
return Error;
|
|
|
|
}
|
2021-02-02 13:12:45 +01:00
|
|
|
|
2021-07-28 20:20:09 +02:00
|
|
|
_Use_decl_annotations_
|
|
|
|
DWORD
|
2021-10-12 07:21:31 +02:00
|
|
|
LoggerErrorV(DWORD Error, LPCWSTR Format, va_list Args)
|
2021-02-02 13:12:45 +01:00
|
|
|
{
|
2021-10-12 07:21:31 +02:00
|
|
|
WCHAR LogLine[0x400];
|
|
|
|
if (_vsnwprintf_s(LogLine, _countof(LogLine), _TRUNCATE, Format, Args) == -1)
|
|
|
|
StrTruncate(LogLine, _countof(LogLine));
|
|
|
|
return LoggerError(Error, LogLine);
|
2021-02-02 13:12:45 +01:00
|
|
|
}
|
|
|
|
|
2021-07-28 20:20:09 +02:00
|
|
|
_Use_decl_annotations_
|
2021-02-02 13:12:45 +01:00
|
|
|
VOID
|
2021-07-28 20:20:09 +02:00
|
|
|
LoggerGetRegistryKeyPath(HKEY Key, LPWSTR Path)
|
2021-02-02 13:12:45 +01:00
|
|
|
{
|
|
|
|
DWORD LastError = GetLastError();
|
|
|
|
if (Key == NULL)
|
|
|
|
{
|
2021-07-28 20:20:09 +02:00
|
|
|
wcsncpy_s(Path, MAX_REG_PATH, L"<null>", _TRUNCATE);
|
2021-02-02 13:12:45 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (_snwprintf_s(Path, MAX_REG_PATH, _TRUNCATE, L"0x%p", Key) == -1)
|
|
|
|
StrTruncate(Path, MAX_REG_PATH);
|
|
|
|
union
|
|
|
|
{
|
|
|
|
KEY_NAME_INFORMATION KeyNameInfo;
|
|
|
|
WCHAR Data[offsetof(KEY_NAME_INFORMATION, Name) + MAX_REG_PATH];
|
|
|
|
} Buffer;
|
|
|
|
DWORD Size;
|
|
|
|
if (!NT_SUCCESS(NtQueryKey(Key, 3, &Buffer, sizeof(Buffer), &Size)) ||
|
|
|
|
Size < offsetof(KEY_NAME_INFORMATION, Name) || Buffer.KeyNameInfo.NameLength >= MAX_REG_PATH * sizeof(WCHAR))
|
|
|
|
goto out;
|
|
|
|
Buffer.KeyNameInfo.NameLength /= sizeof(WCHAR);
|
|
|
|
wmemcpy_s(Path, MAX_REG_PATH, Buffer.KeyNameInfo.Name, Buffer.KeyNameInfo.NameLength);
|
|
|
|
Path[Buffer.KeyNameInfo.NameLength] = L'\0';
|
|
|
|
out:
|
|
|
|
SetLastError(LastError);
|
|
|
|
}
|