69 lines
2.2 KiB
C++
69 lines
2.2 KiB
C++
#ifndef UNICODE
|
|
#define UNICODE
|
|
#endif
|
|
#ifndef _UNICODE
|
|
#define _UNICODE
|
|
#endif
|
|
|
|
#include <windows.h>
|
|
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
std::wstring module_directory() {
|
|
std::vector<wchar_t> buffer(32768U, L'\0');
|
|
const DWORD length = GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size()));
|
|
if (length == 0 || length >= buffer.size()) return {};
|
|
std::wstring path(buffer.data(), length);
|
|
const size_t slash = path.find_last_of(L"\\/");
|
|
return slash == std::wstring::npos ? std::wstring{} : path.substr(0, slash);
|
|
}
|
|
|
|
std::wstring quote(const std::wstring& value) {
|
|
std::wstring output = L"\"";
|
|
size_t slashCount = 0;
|
|
for (wchar_t character : value) {
|
|
if (character == L'\\') {
|
|
++slashCount;
|
|
continue;
|
|
}
|
|
if (character == L'\"') output.append(slashCount * 2U + 1U, L'\\');
|
|
else output.append(slashCount, L'\\');
|
|
slashCount = 0;
|
|
output.push_back(character);
|
|
}
|
|
output.append(slashCount * 2U, L'\\');
|
|
output.push_back(L'\"');
|
|
return output;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int wmain(int argc, wchar_t** argv) {
|
|
const std::wstring service = module_directory() + L"\\DuoRdpAuthService.exe";
|
|
if (GetFileAttributesW(service.c_str()) == INVALID_FILE_ATTRIBUTES) {
|
|
std::fwprintf(stderr, L"DuoRdpAuthService.exe is missing next to DuoRdpAdmin.exe.\n");
|
|
return 2;
|
|
}
|
|
std::wstring command = quote(service) + L" --admin";
|
|
for (int index = 1; index < argc; ++index) command += L" " + quote(argv[index]);
|
|
std::vector<wchar_t> mutableCommand(command.begin(), command.end());
|
|
mutableCommand.push_back(L'\0');
|
|
STARTUPINFOW startup{};
|
|
startup.cb = sizeof(startup);
|
|
PROCESS_INFORMATION process{};
|
|
if (!CreateProcessW(nullptr, mutableCommand.data(), nullptr, nullptr, TRUE, 0, nullptr, module_directory().c_str(), &startup, &process)) {
|
|
std::fwprintf(stderr, L"Cannot launch native admin core, error=%lu.\n", GetLastError());
|
|
return 1;
|
|
}
|
|
CloseHandle(process.hThread);
|
|
WaitForSingleObject(process.hProcess, INFINITE);
|
|
DWORD exitCode = 1;
|
|
GetExitCodeProcess(process.hProcess, &exitCode);
|
|
CloseHandle(process.hProcess);
|
|
return static_cast<int>(exitCode);
|
|
}
|