2019-05-11 19:34:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
2020-01-02 19:52:25 +01:00
|
|
|
* Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2019-05-11 19:34:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tlhelp32.h>
|
2019-08-30 21:51:27 +02:00
|
|
|
#include <accctrl.h>
|
|
|
|
#include <aclapi.h>
|
2019-05-11 19:34:19 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <fcntl.h>
|
2021-06-24 13:35:48 +02:00
|
|
|
#include <hashtable.h>
|
2019-05-11 19:34:19 +02:00
|
|
|
|
2019-08-30 21:51:27 +02:00
|
|
|
static FILE *userspace_interface_file(const char *iface)
|
2019-05-11 19:34:19 +02:00
|
|
|
{
|
2021-06-24 01:52:06 +02:00
|
|
|
char fname[MAX_PATH];
|
|
|
|
HANDLE pipe_handle;
|
2019-08-30 21:51:27 +02:00
|
|
|
SID expected_sid;
|
2021-06-24 01:52:06 +02:00
|
|
|
DWORD bytes = sizeof(expected_sid);
|
|
|
|
PSID pipe_sid;
|
|
|
|
PSECURITY_DESCRIPTOR pipe_sd;
|
|
|
|
bool equal;
|
2019-05-11 19:34:19 +02:00
|
|
|
int fd;
|
|
|
|
|
2019-08-30 21:51:27 +02:00
|
|
|
if (!CreateWellKnownSid(WinLocalSystemSid, NULL, &expected_sid, &bytes))
|
|
|
|
goto err;
|
2019-05-11 19:34:19 +02:00
|
|
|
|
2021-06-24 01:52:06 +02:00
|
|
|
snprintf(fname, sizeof(fname), "\\\\.\\pipe\\ProtectedPrefix\\Administrators\\WireGuard\\%s", iface);
|
|
|
|
pipe_handle = CreateFileA(fname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
|
|
|
|
if (pipe_handle == INVALID_HANDLE_VALUE)
|
2019-05-11 19:34:19 +02:00
|
|
|
goto err;
|
2021-06-24 01:52:06 +02:00
|
|
|
if (GetSecurityInfo(pipe_handle, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &pipe_sid, NULL, NULL, NULL, &pipe_sd) != ERROR_SUCCESS)
|
|
|
|
goto err_close;
|
|
|
|
equal = EqualSid(&expected_sid, pipe_sid);
|
|
|
|
LocalFree(pipe_sd);
|
|
|
|
if (!equal)
|
|
|
|
goto err_close;
|
2019-05-11 19:34:19 +02:00
|
|
|
fd = _open_osfhandle((intptr_t)pipe_handle, _O_RDWR);
|
|
|
|
if (fd == -1) {
|
|
|
|
CloseHandle(pipe_handle);
|
2021-06-24 01:52:06 +02:00
|
|
|
return NULL;
|
2019-05-11 19:34:19 +02:00
|
|
|
}
|
|
|
|
return _fdopen(fd, "r+");
|
2021-06-24 01:52:06 +02:00
|
|
|
err_close:
|
|
|
|
CloseHandle(pipe_handle);
|
2019-05-11 19:34:19 +02:00
|
|
|
err:
|
|
|
|
errno = EACCES;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:35:48 +02:00
|
|
|
static bool have_cached_interfaces;
|
|
|
|
static struct hashtable cached_interfaces;
|
|
|
|
|
2021-06-23 15:43:39 +02:00
|
|
|
static bool userspace_has_wireguard_interface(const char *iface)
|
|
|
|
{
|
|
|
|
char fname[MAX_PATH];
|
|
|
|
WIN32_FIND_DATA find_data;
|
|
|
|
HANDLE find_handle;
|
|
|
|
bool ret = false;
|
|
|
|
|
2021-06-24 13:35:48 +02:00
|
|
|
if (have_cached_interfaces)
|
|
|
|
return hashtable_find_entry(&cached_interfaces, iface) != NULL;
|
|
|
|
|
2021-06-23 15:43:39 +02:00
|
|
|
snprintf(fname, sizeof(fname), "ProtectedPrefix\\Administrators\\WireGuard\\%s", iface);
|
|
|
|
find_handle = FindFirstFile("\\\\.\\pipe\\*", &find_data);
|
|
|
|
if (find_handle == INVALID_HANDLE_VALUE)
|
2021-06-24 13:35:48 +02:00
|
|
|
return -EIO;
|
2021-06-23 15:43:39 +02:00
|
|
|
do {
|
|
|
|
if (!strcmp(fname, find_data.cFileName)) {
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (FindNextFile(find_handle, &find_data));
|
|
|
|
FindClose(find_handle);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:29:27 +01:00
|
|
|
static int userspace_get_wireguard_interfaces(struct string_list *list)
|
2019-05-11 19:34:19 +02:00
|
|
|
{
|
2020-03-19 23:33:14 +01:00
|
|
|
static const char prefix[] = "ProtectedPrefix\\Administrators\\WireGuard\\";
|
2019-05-11 19:34:19 +02:00
|
|
|
WIN32_FIND_DATA find_data;
|
|
|
|
HANDLE find_handle;
|
2021-06-24 13:35:48 +02:00
|
|
|
char *iface;
|
2019-05-11 19:34:19 +02:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
find_handle = FindFirstFile("\\\\.\\pipe\\*", &find_data);
|
|
|
|
if (find_handle == INVALID_HANDLE_VALUE)
|
2021-06-24 13:35:48 +02:00
|
|
|
return -EIO;
|
2019-05-11 19:34:19 +02:00
|
|
|
do {
|
2020-03-19 23:33:14 +01:00
|
|
|
if (strncmp(prefix, find_data.cFileName, strlen(prefix)))
|
2019-05-11 19:34:19 +02:00
|
|
|
continue;
|
2021-06-24 13:35:48 +02:00
|
|
|
iface = find_data.cFileName + strlen(prefix);
|
|
|
|
ret = string_list_add(list, iface);
|
2019-05-11 19:34:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
2021-06-24 13:35:48 +02:00
|
|
|
if (!hashtable_find_or_insert_entry(&cached_interfaces, iface)) {
|
|
|
|
ret = -errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-05-11 19:34:19 +02:00
|
|
|
} while (FindNextFile(find_handle, &find_data));
|
2021-06-24 13:35:48 +02:00
|
|
|
have_cached_interfaces = true;
|
2019-05-11 19:34:19 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
FindClose(find_handle);
|
|
|
|
return ret;
|
|
|
|
}
|