# # # patch "win32/ssh_agent_platform.cc" # from [030848729e09a9805430b724a20a85483a0903b9] # to [eed79fc741df3c122b6fb29ee858714516e1bd27] # # patch "win32/ssh_agent_platform.hh" # from [0ae0c2d4134e6eea20659b3ff40f4c7d6eddb444] # to [d2a6baca38e52093193b449bcceb1023ed32bd01] # ============================================================ --- win32/ssh_agent_platform.cc 030848729e09a9805430b724a20a85483a0903b9 +++ win32/ssh_agent_platform.cc eed79fc741df3c122b6fb29ee858714516e1bd27 @@ -8,35 +8,111 @@ // PURPOSE. #include "base.hh" + +#include "../sanity.hh" + #include "ssh_agent_platform.hh" using std::string; +#define AGENT_COPYDATA_ID 0x804e50ba /* random goop */ +#define AGENT_MAX_MSGLEN 8192 + bool -ssh_agent_platform::connect() +ssh_agent_platform::connect() { - return false; + char mapname[32]; + L(FL("ssh_agent: connect")); + hwnd = FindWindow("Pageant", "Pageant"); + + if (!hwnd) + return false; + + sprintf(mapname, "PageantRequest%08x", (unsigned)GetCurrentThreadId()); + filemap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, + 0, AGENT_MAX_MSGLEN, mapname); + if (filemap == NULL || filemap == INVALID_HANDLE_VALUE) + { + hwnd = NULL; + return false; + } + + filemap_view = (char*)MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); + + if (filemap_view == 0) + { + hwnd = NULL; + CloseHandle(filemap); + filemap = NULL; + return false; + } + + return true; } bool ssh_agent_platform::disconnect() { - return false; + if (filemap != NULL) + { + CloseHandle(filemap); + filemap = NULL; + hwnd = NULL; + UnmapViewOfFile(filemap_view); + filemap_view = NULL; + } + return true; } bool ssh_agent_platform::connected() { - return false; + return hwnd != NULL && (IsWindow(hwnd) != 0); } void ssh_agent_platform::write_data(string const & data) { + unsigned char *p; + int id; + COPYDATASTRUCT cds; + char mapname[32]; + + if (!connected()) + return; + + sprintf(mapname, "PageantRequest%08x", (unsigned)GetCurrentThreadId()); + + L(FL("ssh_agent_platform::write_data: writing %u bytes to %s") % data.length() % mapname); + + E(data.length() < AGENT_MAX_MSGLEN, F("Asked to write more than %u to pageant.") % AGENT_MAX_MSGLEN); + + memcpy(filemap_view, data.c_str(), data.length()); + cds.dwData = AGENT_COPYDATA_ID; + cds.cbData = 1 + strlen(mapname); + cds.lpData = mapname; + + id = SendMessage(hwnd, WM_COPYDATA, (WPARAM) NULL, (LPARAM) &cds); + + E(id > 0, F("Error sending message to pageant (%d).") % id); + + //Start our read counter again + read_len = 0; } -void +void ssh_agent_platform::read_data(u32 const len, string & out) { + if (!connected()) + return; + + L(FL("ssh_agent: read_data: asked to read %u bytes") % len); + + E((read_len + len) < AGENT_MAX_MSGLEN, F("Asked to read more than %u from pageant.") % AGENT_MAX_MSGLEN); + + out.append(filemap_view + read_len, len); + + //keep track of how much we've read + read_len += len; } ============================================================ --- win32/ssh_agent_platform.hh 0ae0c2d4134e6eea20659b3ff40f4c7d6eddb444 +++ win32/ssh_agent_platform.hh d2a6baca38e52093193b449bcceb1023ed32bd01 @@ -8,8 +8,15 @@ // PURPOSE. #include "../numeric_vocab.hh" +#include "windows.h" class ssh_agent_platform { +private: + HWND hwnd; + HANDLE filemap; + char *filemap_view; + u32 read_len; + public: bool connect(); bool disconnect();