-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
98 lines (77 loc) · 2.45 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
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <thread>
#include <mutex>
#include "network.h"
#include "pageant.h"
#include "common.h"
#include <Windows.h>
#include <Winbase.h>
const char* const ssh = GIT_SSH_PATH;
Pageant pageant;
std::mutex pageantMtx;
int SendToAgent(char* buff, int len)
{
Buffer msg(buff, len);
{
std::lock_guard<std::mutex> lock(pageantMtx);
pageant.Query(msg);
}
return msg.len;
}
int main(int argc, char* argv[])
{
try {
{
Network net(&SendToAgent);
FakeSocketFile sFile(net.GetPort());
// below we just launch ssh
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
char* const commandLine = GetCommandLineA();
if (!CreateProcessA(
ssh,
commandLine,
&sa,
&sa,
TRUE,
0,
NULL,
NULL,
&si,
&pi
)) {
LOG_ERROR("Couldn't create process:\n " << ssh << ' ' << commandLine
<< "\nError " << GetLastError());
return -1;
}
LOG_DEBUG("Child SSH-client process has started:\n " << ssh << ' ' << commandLine
<< "\nPID=" << pi.dwProcessId);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD exitCode = 0;
if (!GetExitCodeProcess(pi.hProcess, &exitCode)) {
LOG_ERROR("Couldn't get exit code of child SSH-client process: " << GetLastError());
exitCode = -1;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return exitCode;
}
return GetLastError();
} catch (const std::exception& exc) {
std::cerr << exc.what() << std::endl;
}
return -1;
}