# # # add_file "unix/ssh_agent_platform.cc" # content [f037df9bd9f715c860fc427da164d8bb067304cf] # # add_file "unix/ssh_agent_platform.hh" # content [84f5bae672bcde4f6402d17f99fdf357d571df23] # # add_file "win32/ssh_agent_platform.cc" # content [ebaf6effbc0bc1be8cd15c620381a769aaf243d2] # # add_file "win32/ssh_agent_platform.hh" # content [c5e78a7ce93fd3a40500d4d57df4e031d46c534f] # # patch "Makefile.am" # from [f888b0509c062adfb96485cfa71045515d050c74] # to [3c32e913ca8273b4641b404c94388bfba0346310] # # patch "platform.hh" # from [7224b228b7c8f0476b45ed5fb929aba4d6878fd5] # to [c863285ed181c5aa91939da08f560f1e833d5f4e] # # patch "ssh_agent.cc" # from [1d08ec27ca6d868527c9d58170102f02818ba3f5] # to [89f1bb9d298175c656ef606a0a416ed958e1f21c] # # patch "ssh_agent.hh" # from [90e9eb5957fc913e896ecee54f6fc7a3da04b281] # to [2609a4aaaa2dca86b713fb84572ec79299513d36] # ============================================================ --- unix/ssh_agent_platform.cc f037df9bd9f715c860fc427da164d8bb067304cf +++ unix/ssh_agent_platform.cc f037df9bd9f715c860fc427da164d8bb067304cf @@ -0,0 +1,96 @@ +#include +#include +#include +#include + +#include "../sanity.hh" + +#include "ssh_agent_platform.hh" + +using boost::shared_ptr; +using Netxx::Stream; +using std::min; +using std::string; + +bool +ssh_agent_platform::connect() +{ + const char *authsocket; + int sock; + struct sockaddr_un sunaddr; + + authsocket = getenv("SSH_AUTH_SOCK"); + + if (!authsocket || !strlen(authsocket)) + { + L(FL("ssh_agent: connect: ssh-agent socket not found")); + return false; + } + + sunaddr.sun_family = AF_UNIX; + strncpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)); + + sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (sock < 0) + { + W(F("ssh_agent: connect: could not open socket to ssh-agent")); + return false; + } + + int ret = fcntl(sock, F_SETFD, FD_CLOEXEC); + if (ret == -1) + { + close(sock); + W(F("ssh_agent: connect: could not set up socket for ssh-agent")); + return false; + } + ret = ::connect(sock, (struct sockaddr *)&sunaddr, sizeof sunaddr); + if (ret < 0) + { + close(sock); + W(F("ssh_agent: connect: could not connect to socket for ssh-agent")); + return false; + } + stream = shared_ptr(new Stream(sock)); + return true; +} + +bool +ssh_agent_platform::disconnect() +{ + if (connected()) + stream->close(); + return true; +} + +bool +ssh_agent_platform::connected() +{ + return stream != NULL; +} + +void +ssh_agent_platform::write_data(string const & data) +{ + stream->write(data.c_str(), data.length()); +} + +void +ssh_agent_platform::read_data(u32 const len, string & out) +{ + int ret; + const u32 bufsize = 4096; + char read_buf[bufsize]; + u32 get = len; + while (get > 0) + { + ret = stream->read(read_buf, min(get, bufsize)); + E(ret >= 0, F("stream read failed (%i)") % ret); + if (ret > 0) + L(FL("ssh_agent: read_num_bytes: read %i bytes") % ret); + out.append(read_buf, ret); + get -= ret; + } + L(FL("ssh_agent: read_num_bytes: get: %u") % get); + L(FL("ssh_agent: read_num_bytes: length %u") % out.length()); +} ============================================================ --- unix/ssh_agent_platform.hh 84f5bae672bcde4f6402d17f99fdf357d571df23 +++ unix/ssh_agent_platform.hh 84f5bae672bcde4f6402d17f99fdf357d571df23 @@ -0,0 +1,15 @@ +#include +#include "../numeric_vocab.hh" +#include "../netxx/stream.h" + +class ssh_agent_platform { +private: + boost::shared_ptr stream; + +public: + bool connect(); + bool disconnect(); + bool connected(); + void write_data(std::string const & data); + void read_data(u32 const len, std::string & out); +}; ============================================================ --- win32/ssh_agent_platform.cc ebaf6effbc0bc1be8cd15c620381a769aaf243d2 +++ win32/ssh_agent_platform.cc ebaf6effbc0bc1be8cd15c620381a769aaf243d2 @@ -0,0 +1,25 @@ +bool +ssh_agent_platform::connect() +{ +} + +bool +ssh_agent_platform::disconnect() +{ +} + + +bool +ssh_agent_platform::connected() +{ +} + +void +ssh_agent_platform::write_data(string const & data) +{ +} + +void +ssh_agent_platform::read_data(u32 const len, string & out) +{ +} ============================================================ --- win32/ssh_agent_platform.hh c5e78a7ce93fd3a40500d4d57df4e031d46c534f +++ win32/ssh_agent_platform.hh c5e78a7ce93fd3a40500d4d57df4e031d46c534f @@ -0,0 +1,10 @@ +#include "../numeric_vocab.hh" + +class ssh_agent_platform { +public: + bool connect(); + bool disconnect(); + bool connected(); + void write_data(std::string const & data); + void read_data(u32 const len, std::string & out); +}; ============================================================ --- Makefile.am f888b0509c062adfb96485cfa71045515d050c74 +++ Makefile.am 3c32e913ca8273b4641b404c94388bfba0346310 @@ -274,13 +274,13 @@ UNIX_PLATFORM_SOURCES = \ unix/read_password.cc unix/get_system_flavour.cc \ unix/process.cc unix/terminal.cc unix/inodeprint.cc \ unix/fs.cc unix/make_io_binary.cc unix/os_strerror.cc \ - unix/cputime.cc + unix/cputime.cc unix/ssh_agent_platform.cc WIN32_PLATFORM_SOURCES = \ win32/read_password.cc win32/get_system_flavour.cc \ win32/process.cc win32/terminal.cc win32/inodeprint.cc \ win32/fs.cc win32/make_io_binary.cc win32/os_strerror.cc \ - win32/cputime.cc + win32/cputime.cc win32/ssh_agent_platform.cc # primaries ============================================================ --- platform.hh 7224b228b7c8f0476b45ed5fb929aba4d6878fd5 +++ platform.hh c863285ed181c5aa91939da08f560f1e833d5f4e @@ -128,6 +128,12 @@ double cpu_now(); // arbitrary constant, measured in seconds. double cpu_now(); +#ifdef WIN32_PLATFORM +#include "win32/ssh_agent_platform.hh" +#else +#include "unix/ssh_agent_platform.hh" +#endif + // Local Variables: // mode: C++ // fill-column: 76 ============================================================ --- ssh_agent.cc 1d08ec27ca6d868527c9d58170102f02818ba3f5 +++ ssh_agent.cc 89f1bb9d298175c656ef606a0a416ed958e1f21c @@ -16,7 +16,6 @@ using std::vector; using boost::shared_ptr; using std::string; using std::vector; -using std::min; /* * The ssh-agent network format is essentially based on a u32 which @@ -88,60 +87,14 @@ ssh_agent::ssh_agent() ssh_agent::ssh_agent() { - const char *authsocket; - int sock; - struct sockaddr_un sunaddr; - - authsocket = getenv("SSH_AUTH_SOCK"); - - if (!authsocket || !strlen(authsocket)) - { - L(FL("ssh_agent: connect: ssh-agent socket not found")); - return; - } - - //FIXME: move to platform.cc - sunaddr.sun_family = AF_UNIX; - strncpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)); - - sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (sock < 0) - { - W(F("ssh_agent: connect: could not open socket to ssh-agent")); - return; - } - - int ret = fcntl(sock, F_SETFD, FD_CLOEXEC); - if (ret == -1) - { - close(sock); - W(F("ssh_agent: connect: could not set up socket for ssh-agent")); - return; - } - ret = ::connect(sock, (struct sockaddr *)&sunaddr, sizeof sunaddr); - if (ret < 0) - { - close(sock); - W(F("ssh_agent: connect: could not connect to socket for ssh-agent")); - return; - } - stream = shared_ptr(new Stream(sock)); + connect(); } ssh_agent::~ssh_agent() { - if (connected()) - { - stream->close(); - } + disconnect(); } -bool -ssh_agent::connected() -{ - return stream != NULL; -} - u32 ssh_agent::get_long(char const * buf) { @@ -272,38 +225,18 @@ void } void -ssh_agent::read_num_bytes(u32 const len, string & out) -{ - int ret; - const u32 bufsize = 4096; - char read_buf[bufsize]; - u32 get = len; - while (get > 0) - { - ret = stream->read(read_buf, min(get, bufsize)); - E(ret >= 0, F("stream read failed (%i)") % ret); - if (ret > 0) - L(FL("ssh_agent: read_num_bytes: read %i bytes") % ret); - out.append(read_buf, ret); - get -= ret; - } - L(FL("ssh_agent: read_num_bytes: get: %u") % get); - L(FL("ssh_agent: read_num_bytes: length %u") % out.length()); -} - -void ssh_agent::fetch_packet(string & packet) { u32 len; string len_buf; - read_num_bytes(4, len_buf); + read_data(4, len_buf); u32 l = 0; len = get_long_from_buf(len_buf, l); E(len > 0, F("ssh_agent: fetch_packet: zero-length packet from ssh-agent")); L(FL("ssh_agent: fetch_packet: response len %u") % len); - read_num_bytes(len, packet); + read_data(len, packet); } vector const @@ -315,6 +248,8 @@ ssh_agent::get_keys() return keys; } + string out("\0\0\0\11", 4); + /* unsigned int ch; void * v = (void *)&ch; ch = 0; @@ -325,7 +260,8 @@ ssh_agent::get_keys() stream->write(v, 1); ch = 11; stream->write(v, 1); - + */ + write_data(out); string packet; fetch_packet(packet); @@ -452,7 +388,8 @@ ssh_agent::sign_data(RSA_PublicKey const string packet_out; put_string_into_buf(data_out, packet_out); - stream->write(packet_out.c_str(), packet_out.length()); + //stream->write(packet_out.c_str(), packet_out.length()); + write_data(packet_out); string packet_in; fetch_packet(packet_in); @@ -488,6 +425,12 @@ ssh_agent::sign_data(RSA_PublicKey const % packet_in.length())); } +bool +ssh_agent::connected() +{ + return ssh_agent_platform::connected(); +} + void ssh_agent::add_identity(RSA_PrivateKey const & key, string const & comment) { @@ -511,7 +454,8 @@ ssh_agent::add_identity(RSA_PrivateKey c string packet_out; put_string_into_buf(key_buf, packet_out); - stream->write(packet_out.c_str(), packet_out.length()); + //stream->write(packet_out.c_str(), packet_out.length()); + write_data(packet_out); string packet_in; fetch_packet(packet_in); ============================================================ --- ssh_agent.hh 90e9eb5957fc913e896ecee54f6fc7a3da04b281 +++ ssh_agent.hh 2609a4aaaa2dca86b713fb84572ec79299513d36 @@ -7,26 +7,25 @@ #include "botan/bigint.h" #include #include +#include "platform.hh" -class ssh_agent +class ssh_agent : ssh_agent_platform { public: ssh_agent(); ~ssh_agent(); - bool connected(); std::vector const get_keys(); void sign_data(Botan::RSA_PublicKey const & key, std::string const & data, std::string & out); void add_identity(Botan::RSA_PrivateKey const & key, std::string const & comment); + bool connected(); private: - boost::shared_ptr stream; std::vector keys; //helper functions for reading and unpacking data from ssh-agent void fetch_packet(std::string & packet); - void read_num_bytes(u32 const len, std::string & out); u32 get_long(char const * buf); u32 get_long_from_buf(std::string const & buf, u32 & loc); void get_string_from_buf(std::string const & buf,