Class: Net::SSH::Authentication::Agent

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/net/ssh/authentication/agent.rb

Overview

This class implements a simple client for the ssh-agent protocol. It does not implement any specific protocol, but instead copies the behavior of the ssh-agent functions in the OpenSSH library (3.8).

This means that although it behaves like a SSH1 client, it also has some SSH2 functionality (like signing data).

Defined Under Namespace

Modules: Comment

Constant Summary collapse

SSH2_AGENT_REQUEST_VERSION =
1
SSH2_AGENT_REQUEST_IDENTITIES =
11
SSH2_AGENT_IDENTITIES_ANSWER =
12
SSH2_AGENT_SIGN_REQUEST =
13
SSH2_AGENT_SIGN_RESPONSE =
14
SSH2_AGENT_FAILURE =
30
SSH2_AGENT_VERSION_RESPONSE =
103
SSH_COM_AGENT2_FAILURE =
102
SSH_AGENT_REQUEST_RSA_IDENTITIES =
1
SSH_AGENT_RSA_IDENTITIES_ANSWER1 =
2
SSH_AGENT_RSA_IDENTITIES_ANSWER2 =
5
SSH_AGENT_FAILURE =
5

Instance Attribute Summary collapse

Attributes included from Loggable

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#debug, #error, #fatal, #info, #lwarn

Constructor Details

#initialize(logger = nil) ⇒ Agent

Creates a new Agent object, using the optional logger instance to report status.



61
62
63
# File 'lib/net/ssh/authentication/agent.rb', line 61

def initialize(logger=nil)
  self.logger = logger
end

Instance Attribute Details

#socketObject (readonly)

The underlying socket being used to communicate with the SSH agent.



48
49
50
# File 'lib/net/ssh/authentication/agent.rb', line 48

def socket
  @socket
end

Class Method Details

.connect(logger = nil, agent_socket_factory = nil) ⇒ Object

Instantiates a new agent object, connects to a running SSH agent, negotiates the agent protocol version, and returns the agent object.



52
53
54
55
56
57
# File 'lib/net/ssh/authentication/agent.rb', line 52

def self.connect(logger=nil, agent_socket_factory = nil)
  agent = new(logger)
  agent.connect!(agent_socket_factory)
  agent.negotiate!
  agent
end

Instance Method Details

#closeObject

Closes this socket. This agent reference is no longer able to query the agent.



127
128
129
# File 'lib/net/ssh/authentication/agent.rb', line 127

def close
  @socket.close
end

#connect!(agent_socket_factory = nil) ⇒ Object

Connect to the agent process using the socket factory and socket name given by the attribute writers. If the agent on the other end of the socket reports that it is an SSH2-compatible agent, this will fail (it only supports the ssh-agent distributed by OpenSSH).



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/net/ssh/authentication/agent.rb', line 69

def connect!(agent_socket_factory = nil)
  debug { "connecting to ssh-agent" }
  @socket =
    if agent_socket_factory
      agent_socket_factory.call
    elsif ENV['SSH_AUTH_SOCK'] && defined?(unix_socket_class)
      unix_socket_class.open(ENV['SSH_AUTH_SOCK'])
    elsif Gem.win_platform? && RUBY_ENGINE != "jruby"
      Pageant::Socket.open
    else
      raise AgentNotAvailable, "Agent not configured"
    end
rescue StandardError => e
  error { "could not connect to ssh-agent: #{e.message}" }
  raise AgentNotAvailable, $!.message
end

#identitiesObject

Return an array of all identities (public keys) known to the agent. Each key returned is augmented with a comment property which is set to the comment returned by the agent for that key.

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/net/ssh/authentication/agent.rb', line 103

def identities
  type, body = send_and_wait(SSH2_AGENT_REQUEST_IDENTITIES)
  raise AgentError, "could not get identity count" if agent_failed(type)
  raise AgentError, "bad authentication reply: #{type}" if type != SSH2_AGENT_IDENTITIES_ANSWER

  identities = []
  body.read_long.times do
    key_str = body.read_string
    comment_str = body.read_string
    begin
      key = Buffer.new(key_str).read_key
      key.extend(Comment)
      key.comment = comment_str
      identities.push key
    rescue NotImplementedError => e
      error { "ignoring unimplemented key:#{e.message} #{comment_str}" }
    end
  end

  return identities
end

#negotiate!Object

Attempts to negotiate the SSH agent protocol version. Raises an error if the version could not be negotiated successfully.

Raises:



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/net/ssh/authentication/agent.rb', line 88

def negotiate!
  # determine what type of agent we're communicating with
  type, body = send_and_wait(SSH2_AGENT_REQUEST_VERSION, :string, Transport::ServerVersion::PROTO_VERSION)

  raise AgentNotAvailable, "SSH2 agents are not yet supported" if type == SSH2_AGENT_VERSION_RESPONSE
  if type == SSH2_AGENT_FAILURE
    debug { "Unexpected response type==#{type}, this will be ignored" }
  elsif type != SSH_AGENT_RSA_IDENTITIES_ANSWER1 && type != SSH_AGENT_RSA_IDENTITIES_ANSWER2
    raise AgentNotAvailable, "unknown response from agent: #{type}, #{body.to_s.inspect}"
  end
end

#sign(key, data) ⇒ Object

Using the agent and the given public key, sign the given data. The signature is returned in SSH2 format.

Raises:



133
134
135
136
137
138
139
140
# File 'lib/net/ssh/authentication/agent.rb', line 133

def sign(key, data)
  type, reply = send_and_wait(SSH2_AGENT_SIGN_REQUEST, :string, Buffer.from(:key, key), :string, data, :long, 0)

  raise AgentError, "agent could not sign data with requested identity" if agent_failed(type)
  raise AgentError, "bad authentication response #{type}" if type != SSH2_AGENT_SIGN_RESPONSE

  return reply.read_string
end