From fcdfa65579518c2d9d9fa74231455a08a0e783f3 Mon Sep 17 00:00:00 2001 From: Timo Weingärtner Date: Tue, 27 Apr 2021 12:40:17 +0200 Subject: factor out communication with the upstream agent --- ssh-agent-filter.C | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/ssh-agent-filter.C b/ssh-agent-filter.C index e94425b..d22ced1 100644 --- a/ssh-agent-filter.C +++ b/ssh-agent-filter.C @@ -182,6 +182,14 @@ int make_listen_sock () { return sock; } +rfc4251::string ask_upstream_agent (rfc4251::string const & request) { + io::stream agent{make_upstream_agent_conn(), io::close_handle}; + arm(agent); + + agent << request; + return rfc4251::string{agent}; +} + void parse_cmdline (int const argc, char const * const * const argv) { po::options_description opts{"Options"}; opts.add_options() @@ -229,11 +237,7 @@ void parse_cmdline (int const argc, char const * const * const argv) { } void setup_filters () { - io::stream agent{make_upstream_agent_conn(), io::close_handle}; - arm(agent); - - agent << rfc4251::string{string{SSH2_AGENTC_REQUEST_IDENTITIES}}; - rfc4251::string const answer{agent}; + auto const answer = ask_upstream_agent({SSH2_AGENTC_REQUEST_IDENTITIES}); io::stream answer_iss{answer.data(), answer.size()}; arm(answer_iss); rfc4251::byte const resp_code{answer_iss}; @@ -475,12 +479,9 @@ rfc4251::string handle_request (rfc4251::string const & r) { switch (request_code) { case SSH2_AGENTC_REQUEST_IDENTITIES: { - io::stream agent{make_upstream_agent_conn(), io::close_handle}; - arm(agent); - agent << rfc4251::string{string{SSH2_AGENTC_REQUEST_IDENTITIES}}; + auto const agent_answer = ask_upstream_agent({SSH2_AGENTC_REQUEST_IDENTITIES}); // temp to test key filtering when signing - //return rfc4251::string{agent}; - rfc4251::string const agent_answer{agent}; + //return agent_answer; io::stream agent_answer_iss{agent_answer.data(), agent_answer.size()}; arm(agent_answer_iss); rfc4251::byte const answer_code{agent_answer_iss}; @@ -518,12 +519,7 @@ rfc4251::string handle_request (rfc4251::string const & r) { } if (allow) { - io::stream agent{make_upstream_agent_conn(), io::close_handle}; - arm(agent); - rfc4251::string agent_answer; - - agent << r; - return rfc4251::string{agent}; + return ask_upstream_agent(r); } else answer << rfc4251::byte{SSH_AGENT_FAILURE}; } -- cgit v1.2.3