net-ssh 2.5.0 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,8 @@
1
1
 
2
+ === 2.5.1 / 24 May 2012
3
+
4
+ * Added missing file to manifest [Marco Sandrini]
5
+
2
6
  === 2.5.0 / 24 May 2012
3
7
 
4
8
  * Implement many algorithms [Ryosuke Yamazaki]
@@ -0,0 +1,85 @@
1
+ require 'jruby_pageant'
2
+
3
+ module Net; module SSH; module Authentication
4
+
5
+ # This class implements an agent for JRuby + Pageant.
6
+ #
7
+ # Written by Artūras Šlajus <arturas.slajus@gmail.com>
8
+ class Agent
9
+ include Loggable
10
+ include JRubyPageant
11
+
12
+ # A simple module for extending keys, to allow blobs and comments to be
13
+ # specified for them.
14
+ module Key
15
+ # :blob is used by OpenSSL::PKey::RSA#to_blob
16
+ attr_accessor :java_blob
17
+ attr_accessor :comment
18
+ end
19
+
20
+ # Instantiates a new agent object, connects to a running SSH agent,
21
+ # negotiates the agent protocol version, and returns the agent object.
22
+ def self.connect(logger=nil)
23
+ agent = new(logger)
24
+ agent.connect!
25
+ agent
26
+ end
27
+
28
+ # Creates a new Agent object, using the optional logger instance to
29
+ # report status.
30
+ def initialize(logger=nil)
31
+ self.logger = logger
32
+ end
33
+
34
+ # Connect to the agent process using the socket factory and socket name
35
+ # given by the attribute writers. If the agent on the other end of the
36
+ # socket reports that it is an SSH2-compatible agent, this will fail
37
+ # (it only supports the ssh-agent distributed by OpenSSH).
38
+ def connect!
39
+ debug { "connecting to Pageant ssh-agent (via java connector)" }
40
+ @agent_proxy = JRubyPageant.create
41
+ unless @agent_proxy.is_running
42
+ raise AgentNotAvailable, "Pageant is not running!"
43
+ end
44
+ debug { "connection to Pageant ssh-agent (via java connector) succeeded" }
45
+ rescue AgentProxyException => e
46
+ error { "could not connect to Pageant ssh-agent (via java connector)" }
47
+ raise AgentNotAvailable, e.message, e.backtrace
48
+ end
49
+
50
+ # Return an array of all identities (public keys) known to the agent.
51
+ # Each key returned is augmented with a +comment+ property which is set
52
+ # to the comment returned by the agent for that key.
53
+ def identities
54
+ debug { "getting identities from Pageant" }
55
+ @agent_proxy.get_identities.map do |identity|
56
+ blob = identity.get_blob
57
+ key = Buffer.new(String.from_java_bytes(blob)).read_key
58
+ key.extend(Key)
59
+ key.java_blob = blob
60
+ key.comment = String.from_java_bytes(identity.get_comment)
61
+ key
62
+ end
63
+ rescue AgentProxyException => e
64
+ raise AgentError, "Cannot get identities: #{e.message}", e.backtrace
65
+ end
66
+
67
+ # Simulate agent close. This agent reference is no longer able to
68
+ # query the agent.
69
+ def close
70
+ @agent_proxy = nil
71
+ end
72
+
73
+ # Using the agent and the given public key, sign the given data. The
74
+ # signature is returned in SSH2 format.
75
+ def sign(key, data)
76
+ signed = @agent_proxy.sign(key.java_blob, data.to_java_bytes)
77
+ String.from_java_bytes(signed)
78
+ rescue AgentProxyException => e
79
+ raise AgentError,
80
+ "agent could not sign data with requested identity: #{e.message}",
81
+ e.backtrace
82
+ end
83
+ end
84
+
85
+ end; end; end
@@ -0,0 +1,170 @@
1
+ require 'net/ssh/transport/server_version'
2
+
3
+ # Only load pageant on Windows
4
+ if Net::SSH::Authentication::PLATFORM == :win32
5
+ require 'net/ssh/authentication/pageant'
6
+ end
7
+
8
+ module Net; module SSH; module Authentication
9
+
10
+ # This class implements a simple client for the ssh-agent protocol. It
11
+ # does not implement any specific protocol, but instead copies the
12
+ # behavior of the ssh-agent functions in the OpenSSH library (3.8).
13
+ #
14
+ # This means that although it behaves like a SSH1 client, it also has
15
+ # some SSH2 functionality (like signing data).
16
+ class Agent
17
+ include Loggable
18
+
19
+ # A simple module for extending keys, to allow comments to be specified
20
+ # for them.
21
+ module Comment
22
+ attr_accessor :comment
23
+ end
24
+
25
+ SSH2_AGENT_REQUEST_VERSION = 1
26
+ SSH2_AGENT_REQUEST_IDENTITIES = 11
27
+ SSH2_AGENT_IDENTITIES_ANSWER = 12
28
+ SSH2_AGENT_SIGN_REQUEST = 13
29
+ SSH2_AGENT_SIGN_RESPONSE = 14
30
+ SSH2_AGENT_FAILURE = 30
31
+ SSH2_AGENT_VERSION_RESPONSE = 103
32
+
33
+ SSH_COM_AGENT2_FAILURE = 102
34
+
35
+ SSH_AGENT_REQUEST_RSA_IDENTITIES = 1
36
+ SSH_AGENT_RSA_IDENTITIES_ANSWER1 = 2
37
+ SSH_AGENT_RSA_IDENTITIES_ANSWER2 = 5
38
+ SSH_AGENT_FAILURE = 5
39
+
40
+ # The underlying socket being used to communicate with the SSH agent.
41
+ attr_reader :socket
42
+
43
+ # Instantiates a new agent object, connects to a running SSH agent,
44
+ # negotiates the agent protocol version, and returns the agent object.
45
+ def self.connect(logger=nil)
46
+ agent = new(logger)
47
+ agent.connect!
48
+ agent.negotiate!
49
+ agent
50
+ end
51
+
52
+ # Creates a new Agent object, using the optional logger instance to
53
+ # report status.
54
+ def initialize(logger=nil)
55
+ self.logger = logger
56
+ end
57
+
58
+ # Connect to the agent process using the socket factory and socket name
59
+ # given by the attribute writers. If the agent on the other end of the
60
+ # socket reports that it is an SSH2-compatible agent, this will fail
61
+ # (it only supports the ssh-agent distributed by OpenSSH).
62
+ def connect!
63
+ begin
64
+ debug { "connecting to ssh-agent" }
65
+ @socket = agent_socket_factory.open(ENV['SSH_AUTH_SOCK'])
66
+ rescue
67
+ error { "could not connect to ssh-agent" }
68
+ raise AgentNotAvailable, $!.message
69
+ end
70
+ end
71
+
72
+ # Attempts to negotiate the SSH agent protocol version. Raises an error
73
+ # if the version could not be negotiated successfully.
74
+ def negotiate!
75
+ # determine what type of agent we're communicating with
76
+ type, body = send_and_wait(SSH2_AGENT_REQUEST_VERSION, :string, Transport::ServerVersion::PROTO_VERSION)
77
+
78
+ if type == SSH2_AGENT_VERSION_RESPONSE
79
+ raise NotImplementedError, "SSH2 agents are not yet supported"
80
+ elsif type != SSH_AGENT_RSA_IDENTITIES_ANSWER1 && type != SSH_AGENT_RSA_IDENTITIES_ANSWER2
81
+ raise AgentError, "unknown response from agent: #{type}, #{body.to_s.inspect}"
82
+ end
83
+ end
84
+
85
+ # Return an array of all identities (public keys) known to the agent.
86
+ # Each key returned is augmented with a +comment+ property which is set
87
+ # to the comment returned by the agent for that key.
88
+ def identities
89
+ type, body = send_and_wait(SSH2_AGENT_REQUEST_IDENTITIES)
90
+ raise AgentError, "could not get identity count" if agent_failed(type)
91
+ raise AgentError, "bad authentication reply: #{type}" if type != SSH2_AGENT_IDENTITIES_ANSWER
92
+
93
+ identities = []
94
+ body.read_long.times do
95
+ key = Buffer.new(body.read_string).read_key
96
+ key.extend(Comment)
97
+ key.comment = body.read_string
98
+ identities.push key
99
+ end
100
+
101
+ return identities
102
+ end
103
+
104
+ # Closes this socket. This agent reference is no longer able to
105
+ # query the agent.
106
+ def close
107
+ @socket.close
108
+ end
109
+
110
+ # Using the agent and the given public key, sign the given data. The
111
+ # signature is returned in SSH2 format.
112
+ def sign(key, data)
113
+ type, reply = send_and_wait(SSH2_AGENT_SIGN_REQUEST, :string, Buffer.from(:key, key), :string, data, :long, 0)
114
+
115
+ if agent_failed(type)
116
+ raise AgentError, "agent could not sign data with requested identity"
117
+ elsif type != SSH2_AGENT_SIGN_RESPONSE
118
+ raise AgentError, "bad authentication response #{type}"
119
+ end
120
+
121
+ return reply.read_string
122
+ end
123
+
124
+ private
125
+
126
+ # Returns the agent socket factory to use.
127
+ def agent_socket_factory
128
+ if Net::SSH::Authentication::PLATFORM == :win32
129
+ Pageant::socket_factory
130
+ else
131
+ UNIXSocket
132
+ end
133
+ end
134
+
135
+ # Send a new packet of the given type, with the associated data.
136
+ def send_packet(type, *args)
137
+ buffer = Buffer.from(*args)
138
+ data = [buffer.length + 1, type.to_i, buffer.to_s].pack("NCA*")
139
+ debug { "sending agent request #{type} len #{buffer.length}" }
140
+ @socket.send data, 0
141
+ end
142
+
143
+ # Read the next packet from the agent. This will return a two-part
144
+ # tuple consisting of the packet type, and the packet's body (which
145
+ # is returned as a Net::SSH::Buffer).
146
+ def read_packet
147
+ buffer = Net::SSH::Buffer.new(@socket.read(4))
148
+ buffer.append(@socket.read(buffer.read_long))
149
+ type = buffer.read_byte
150
+ debug { "received agent packet #{type} len #{buffer.length-4}" }
151
+ return type, buffer
152
+ end
153
+
154
+ # Send the given packet and return the subsequent reply from the agent.
155
+ # (See #send_packet and #read_packet).
156
+ def send_and_wait(type, *args)
157
+ send_packet(type, *args)
158
+ read_packet
159
+ end
160
+
161
+ # Returns +true+ if the parameter indicates a "failure" response from
162
+ # the agent, and +false+ otherwise.
163
+ def agent_failed(type)
164
+ type == SSH_AGENT_FAILURE ||
165
+ type == SSH2_AGENT_FAILURE ||
166
+ type == SSH_COM_AGENT2_FAILURE
167
+ end
168
+ end
169
+
170
+ end; end; end
@@ -51,7 +51,7 @@ module Net; module SSH
51
51
  MINOR = 5
52
52
 
53
53
  # The tiny component of this version of the Net::SSH library
54
- TINY = 0
54
+ TINY = 1
55
55
 
56
56
  # The current version of the Net::SSH library as a Version instance
57
57
  CURRENT = new(MAJOR, MINOR, TINY)
@@ -1,7 +1,7 @@
1
1
  @spec = Gem::Specification.new do |s|
2
2
  s.name = "net-ssh"
3
3
  s.rubyforge_project = 'net-ssh'
4
- s.version = "2.5.0"
4
+ s.version = "2.5.1"
5
5
  s.summary = "Net::SSH: a pure-Ruby implementation of the SSH2 client protocol."
6
6
  s.description = s.summary + " It allows you to write programs that invoke and interact with processes on remote servers, via SSH2."
7
7
  s.authors = ["Jamis Buck", "Delano Mandelbaum"]
@@ -40,6 +40,8 @@
40
40
  lib/net/ssh/authentication/methods/publickey.rb
41
41
  lib/net/ssh/authentication/pageant.rb
42
42
  lib/net/ssh/authentication/session.rb
43
+ lib/net/ssh/authentication/agent/java_pageant.rb
44
+ lib/net/ssh/authentication/agent/socket.rb
43
45
  lib/net/ssh/buffer.rb
44
46
  lib/net/ssh/buffered_io.rb
45
47
  lib/net/ssh/config.rb
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: net-ssh
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.5.0
5
+ version: 2.5.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jamis Buck
@@ -53,6 +53,8 @@ files:
53
53
  - lib/net/ssh/authentication/methods/publickey.rb
54
54
  - lib/net/ssh/authentication/pageant.rb
55
55
  - lib/net/ssh/authentication/session.rb
56
+ - lib/net/ssh/authentication/agent/java_pageant.rb
57
+ - lib/net/ssh/authentication/agent/socket.rb
56
58
  - lib/net/ssh/buffer.rb
57
59
  - lib/net/ssh/buffered_io.rb
58
60
  - lib/net/ssh/config.rb