This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
327 lines (283 loc) · 8.26 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "pch.h"
#include <DispatcherQueue.h>
#include <Windows.h>
#include <string_view>
#include "helper.h"
#include "TrayWindow.h"
#include "PipeServer.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Networking;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::System;
using namespace std::chrono_literals;
const UINT AGENT_MAX_MESSAGE_LENGTH = 8192;
static std::string_view inline rtrim(const std::string_view& s) {
return s.substr(0, std::find_if(s.rbegin(), s.rend(), [](int ch) {
return !std::isspace(ch);
}).base() - s.begin());
}
IBuffer ReadPipe(HANDLE hIn)
{
constexpr auto RBUFSIZE = 4096U;
BYTE BUF[RBUFSIZE];
DWORD nRead;
DataWriter writer;
for (;;) {
auto status = ReadFile(hIn, BUF, RBUFSIZE, &nRead, nullptr);
if (!status || nRead == 0) {
break;
}
writer.WriteBytes(array_view(BUF, BUF + nRead));
}
CloseHandle(hIn);
return writer.DetachBuffer();
}
IAsyncAction WritePipe(HANDLE hOut, const std::string& buf)
{
co_await winrt::resume_background();
DWORD nWrite;
WriteFile(hOut, buf.data(), (DWORD)buf.size(), &nWrite, nullptr);
CloseHandle(hOut);
co_return;
}
IAsyncOperation<IBuffer> GetProcessOutput(LPCTSTR cmdline, const std::optional<std::string> input = std::nullopt)
{
DWORD err = 0;
DWORD retCode = 0;
IBuffer buf{ nullptr };
SECURITY_ATTRIBUTES saAttr{};
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
co_await winrt::resume_background();
HANDLE hChild_STDOUT_Rd, hChild_STDOUT_Wr;
check_bool(CreatePipeEx(&hChild_STDOUT_Rd, &hChild_STDOUT_Wr, &saAttr, 0, TRUE));
HANDLE hChild_STDIN_Rd, hChild_STDIN_Wr;
if (input && !CreatePipeEx(&hChild_STDIN_Rd, &hChild_STDIN_Wr, &saAttr, 0, TRUE)) {
err = GetLastError();
CloseHandle(hChild_STDOUT_Wr);
CloseHandle(hChild_STDOUT_Rd);
check_win32(err);
}
// Writable cmdline for CreateProcess.
LPTSTR cmd = _tcsdup(cmdline);
STARTUPINFO si{};
PROCESS_INFORMATION pi{};
// Ensure the STDOUTRd/STDINWr is not inherited by child process.
if (!SetHandleInformation(hChild_STDOUT_Rd, HANDLE_FLAG_INHERIT, 0)
|| (input && !SetHandleInformation(hChild_STDIN_Wr, HANDLE_FLAG_INHERIT, 0))) {
goto closePipeExitL;
}
si.cb = sizeof(si);
si.hStdOutput = hChild_STDOUT_Wr;
if (input) {
si.hStdInput = hChild_STDIN_Rd;
}
si.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
if (CreateProcess(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
// Close stdout write end.
CloseHandle(hChild_STDOUT_Wr);
IAsyncAction writer{ nullptr };
if (input) {
// Close stdin read end.
CloseHandle(hChild_STDIN_Rd);
writer = WritePipe(hChild_STDIN_Wr, *input);
}
// Returns when child closes its stdout/exits.
buf = ReadPipe(hChild_STDOUT_Rd);
// Wait for the process to exit.
co_await resume_on_signal(pi.hProcess);
// writer should have finished sice child is done and closed its stdin.
if (input) {
writer.get();
}
if (!GetExitCodeProcess(pi.hProcess, &retCode)) {
err = GetLastError();
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else {
closePipeExitL:
err = GetLastError();
CloseHandle(hChild_STDOUT_Wr);
CloseHandle(hChild_STDOUT_Rd);
if (input) {
CloseHandle(hChild_STDIN_Wr);
CloseHandle(hChild_STDIN_Rd);
}
}
// If any
if (err || retCode) {
buf = nullptr;
}
free(cmd);
check_win32(err);
co_return buf;
}
IAsyncOperation<bool> VerifyAgentConf()
{
auto iBuf = co_await GetProcessOutput(_T("gpgconf --list-options gpg-agent"));
if (!iBuf) {
co_return false;
}
bool puttyEnabled = false;
auto puttyOption = "enable-putty-support";
size_t next;
std::string_view s((char*)iBuf.data(), iBuf.Length());
while ((next = s.find('\n')) != std::string_view::npos) {
auto l = s.substr(0, next);
if (!l.compare(0, min(l.size(), strlen(puttyOption)), puttyOption)) {
if (size_t val_pos = l.rfind(':'); val_pos != std::string_view::npos) {
auto s_val = l.substr(val_pos + 1);
int val = 0;
if (auto [p, ec] = std::from_chars(s_val.data(), s_val.data() + s_val.size(), val);
ec == std::errc()) {
puttyEnabled = (val > 0);
}
}
break;
}
s = s.substr(next + 1);
}
if (!puttyEnabled) {
co_return
co_await GetProcessOutput(_T("gpgconf --change-options gpg-agent"), "enable-putty-support:0:1") &&
co_await GetProcessOutput(_T("gpgconf --kill gpg-agent"));
}
co_return true;
}
IAsyncAction KeepAgentLive(const TrayWindow& w, DispatcherQueue queue)
{
co_await queue;
for (;;) {
// Read the socket information.
std::string svc;
BYTE cookie[16]{ 0 };
auto canStart{ co_await VerifyAgentConf() };
if (!canStart) {
co_await queue;
w.forceClose(to_hstring("Failed to verify gpg-agent configuration.\nPlease make sure you have GnuPG installed and available in PATH"));
break;
}
else {
if (!FindWindow(_T("Pageant"), _T("Pageant")) && !(co_await GetProcessOutput(_T("gpgconf --launch gpg-agent")))) {
co_await queue;
w.forceClose(to_hstring("Failed to start gpg-agent. Exiting."));
break;
}
}
// Read svc port and cookie.
{
auto agent_path = co_await GetProcessOutput(_T("gpgconf --list-dir agent-socket"));
if (!agent_path) {
co_await 1s;
continue;
}
auto file = co_await StorageFile::GetFileFromPathAsync(
to_hstring(rtrim(std::string_view((char*)agent_path.data(), agent_path.Length())))
);
auto iBuf = co_await FileIO::ReadBufferAsync(file);
array_view buf(iBuf.data(), iBuf.data() + iBuf.Length());
auto nlinePos = std::find(buf.begin(), buf.end(), '\n');
if (buf.end() - nlinePos <= 16) {
co_await 1s;
continue;
}
svc.assign(buf.begin(), nlinePos);
std::copy_n(nlinePos + 1, 16, cookie);
}
// Monitor the socket.
Sockets::StreamSocket gpgSocket;
try {
gpgSocket.Control().KeepAlive(true);
co_await gpgSocket.ConnectAsync(HostName(L"localhost"), to_hstring(svc));
Buffer buf{ 4096 };
std::copy_n(cookie, 16, buf.data());
buf.Length(16);
co_await gpgSocket.OutputStream().WriteAsync(buf);
while (1) {
co_await gpgSocket.InputStream().ReadAsync(buf, buf.Capacity(), InputStreamOptions::Partial);
}
}
catch (...)
{
continue;
}
}
co_return;
}
DispatcherQueueController CreateDispatcherQueueController()
{
DispatcherQueueOptions options
{
sizeof(DispatcherQueueOptions),
DQTYPE_THREAD_CURRENT,
DQTAT_COM_STA
};
ABI::Windows::System::IDispatcherQueueController* ptr{};
check_hresult(CreateDispatcherQueueController(options, &ptr));
return { ptr, take_ownership_from_abi };
}
int WINAPI wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ PWSTR, _In_ int)
{
HANDLE hAppMutex = nullptr;
MSG msg;
int retCode = 0;
// Single instance of application per session.
if ((hAppMutex = CreateMutex(nullptr, TRUE, _T("ssh-gpg-bridge-MUTEX"))) == nullptr ||
GetLastError() == ERROR_ALREADY_EXISTS) {
winrt::hstring text = __MODULENAME + _T(":\n ") +
L"Another instance is already running.\n\nSet SSH_AUTH_SOCK to " + to_hstring(gPipeName).data();
MessageBoxW(nullptr, text.data(), L"Error", MB_OK | MB_ICONERROR | MB_TOPMOST);
return -1;
}
try {
auto trayWnd = TrayWindow();
auto controller{ CreateDispatcherQueueController() };
auto monitorAgent{ KeepAgentLive(trayWnd, controller.DispatcherQueue()) };
PipeServer server(gPipeName);
if (!server.Start()) {
throw std::runtime_error("Failed to start Named pipeserver.");
}
auto onExit = concurrency::create_task([&]() {
auto eStatus = server.Wait();
if (!eStatus) {
trayWnd.forceClose(eStatus.message());
}
return;
});
BOOL bRet;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (bRet == -1)
{
throw_last_error();
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
retCode = (int)msg.wParam;
monitorAgent.Cancel();
server.Stop();
onExit.get();
}
catch (hresult_error& e) {
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
retCode = 2;
MessageBoxW(nullptr, e.message().data(), L"Exception", MB_OK | MB_ICONERROR | MB_TOPMOST);
}
catch (std::exception& e) {
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
retCode = 2;
MessageBoxA(nullptr, e.what(), "Exception", MB_OK | MB_ICONERROR | MB_TOPMOST);
}
ReleaseMutex(hAppMutex);
return retCode;
}