net-ssh-backports 6.3.0.backports

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yml +93 -0
  3. data/.gitignore +13 -0
  4. data/.rubocop.yml +21 -0
  5. data/.rubocop_todo.yml +1074 -0
  6. data/.travis.yml +51 -0
  7. data/CHANGES.txt +698 -0
  8. data/Gemfile +13 -0
  9. data/Gemfile.noed25519 +12 -0
  10. data/ISSUE_TEMPLATE.md +30 -0
  11. data/LICENSE.txt +19 -0
  12. data/Manifest +132 -0
  13. data/README.md +287 -0
  14. data/Rakefile +105 -0
  15. data/THANKS.txt +110 -0
  16. data/appveyor.yml +58 -0
  17. data/lib/net/ssh/authentication/agent.rb +284 -0
  18. data/lib/net/ssh/authentication/certificate.rb +183 -0
  19. data/lib/net/ssh/authentication/constants.rb +20 -0
  20. data/lib/net/ssh/authentication/ed25519.rb +185 -0
  21. data/lib/net/ssh/authentication/ed25519_loader.rb +31 -0
  22. data/lib/net/ssh/authentication/key_manager.rb +297 -0
  23. data/lib/net/ssh/authentication/methods/abstract.rb +69 -0
  24. data/lib/net/ssh/authentication/methods/hostbased.rb +72 -0
  25. data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +77 -0
  26. data/lib/net/ssh/authentication/methods/none.rb +34 -0
  27. data/lib/net/ssh/authentication/methods/password.rb +80 -0
  28. data/lib/net/ssh/authentication/methods/publickey.rb +95 -0
  29. data/lib/net/ssh/authentication/pageant.rb +497 -0
  30. data/lib/net/ssh/authentication/pub_key_fingerprint.rb +43 -0
  31. data/lib/net/ssh/authentication/session.rb +163 -0
  32. data/lib/net/ssh/buffer.rb +434 -0
  33. data/lib/net/ssh/buffered_io.rb +202 -0
  34. data/lib/net/ssh/config.rb +406 -0
  35. data/lib/net/ssh/connection/channel.rb +695 -0
  36. data/lib/net/ssh/connection/constants.rb +33 -0
  37. data/lib/net/ssh/connection/event_loop.rb +123 -0
  38. data/lib/net/ssh/connection/keepalive.rb +59 -0
  39. data/lib/net/ssh/connection/session.rb +712 -0
  40. data/lib/net/ssh/connection/term.rb +180 -0
  41. data/lib/net/ssh/errors.rb +106 -0
  42. data/lib/net/ssh/key_factory.rb +218 -0
  43. data/lib/net/ssh/known_hosts.rb +264 -0
  44. data/lib/net/ssh/loggable.rb +62 -0
  45. data/lib/net/ssh/packet.rb +106 -0
  46. data/lib/net/ssh/prompt.rb +62 -0
  47. data/lib/net/ssh/proxy/command.rb +123 -0
  48. data/lib/net/ssh/proxy/errors.rb +16 -0
  49. data/lib/net/ssh/proxy/http.rb +98 -0
  50. data/lib/net/ssh/proxy/https.rb +50 -0
  51. data/lib/net/ssh/proxy/jump.rb +54 -0
  52. data/lib/net/ssh/proxy/socks4.rb +67 -0
  53. data/lib/net/ssh/proxy/socks5.rb +140 -0
  54. data/lib/net/ssh/service/forward.rb +426 -0
  55. data/lib/net/ssh/test/channel.rb +147 -0
  56. data/lib/net/ssh/test/extensions.rb +173 -0
  57. data/lib/net/ssh/test/kex.rb +46 -0
  58. data/lib/net/ssh/test/local_packet.rb +53 -0
  59. data/lib/net/ssh/test/packet.rb +101 -0
  60. data/lib/net/ssh/test/remote_packet.rb +40 -0
  61. data/lib/net/ssh/test/script.rb +180 -0
  62. data/lib/net/ssh/test/socket.rb +65 -0
  63. data/lib/net/ssh/test.rb +94 -0
  64. data/lib/net/ssh/transport/algorithms.rb +502 -0
  65. data/lib/net/ssh/transport/cipher_factory.rb +103 -0
  66. data/lib/net/ssh/transport/constants.rb +40 -0
  67. data/lib/net/ssh/transport/ctr.rb +115 -0
  68. data/lib/net/ssh/transport/hmac/abstract.rb +97 -0
  69. data/lib/net/ssh/transport/hmac/md5.rb +10 -0
  70. data/lib/net/ssh/transport/hmac/md5_96.rb +9 -0
  71. data/lib/net/ssh/transport/hmac/none.rb +13 -0
  72. data/lib/net/ssh/transport/hmac/ripemd160.rb +11 -0
  73. data/lib/net/ssh/transport/hmac/sha1.rb +11 -0
  74. data/lib/net/ssh/transport/hmac/sha1_96.rb +9 -0
  75. data/lib/net/ssh/transport/hmac/sha2_256.rb +11 -0
  76. data/lib/net/ssh/transport/hmac/sha2_256_96.rb +9 -0
  77. data/lib/net/ssh/transport/hmac/sha2_256_etm.rb +12 -0
  78. data/lib/net/ssh/transport/hmac/sha2_512.rb +11 -0
  79. data/lib/net/ssh/transport/hmac/sha2_512_96.rb +9 -0
  80. data/lib/net/ssh/transport/hmac/sha2_512_etm.rb +12 -0
  81. data/lib/net/ssh/transport/hmac.rb +47 -0
  82. data/lib/net/ssh/transport/identity_cipher.rb +57 -0
  83. data/lib/net/ssh/transport/kex/abstract.rb +130 -0
  84. data/lib/net/ssh/transport/kex/abstract5656.rb +72 -0
  85. data/lib/net/ssh/transport/kex/curve25519_sha256.rb +39 -0
  86. data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +30 -0
  87. data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +37 -0
  88. data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha256.rb +11 -0
  89. data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +122 -0
  90. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +72 -0
  91. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +11 -0
  92. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +39 -0
  93. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +21 -0
  94. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +21 -0
  95. data/lib/net/ssh/transport/kex.rb +31 -0
  96. data/lib/net/ssh/transport/key_expander.rb +30 -0
  97. data/lib/net/ssh/transport/openssl.rb +253 -0
  98. data/lib/net/ssh/transport/packet_stream.rb +280 -0
  99. data/lib/net/ssh/transport/server_version.rb +77 -0
  100. data/lib/net/ssh/transport/session.rb +354 -0
  101. data/lib/net/ssh/transport/state.rb +208 -0
  102. data/lib/net/ssh/verifiers/accept_new.rb +33 -0
  103. data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +33 -0
  104. data/lib/net/ssh/verifiers/always.rb +58 -0
  105. data/lib/net/ssh/verifiers/never.rb +19 -0
  106. data/lib/net/ssh/version.rb +68 -0
  107. data/lib/net/ssh.rb +330 -0
  108. data/net-ssh-public_cert.pem +20 -0
  109. data/net-ssh.gemspec +44 -0
  110. data/support/ssh_tunnel_bug.rb +65 -0
  111. metadata +271 -0
@@ -0,0 +1,426 @@
1
+ require 'net/ssh/loggable'
2
+
3
+ module Net
4
+ module SSH
5
+ module Service
6
+ # This class implements various port forwarding services for use by
7
+ # Net::SSH clients. The Forward class should never need to be instantiated
8
+ # directly; instead, it should be accessed via the singleton instance
9
+ # returned by Connection::Session#forward:
10
+ #
11
+ # ssh.forward.local(1234, "www.capify.org", 80)
12
+ class Forward
13
+ include Loggable
14
+
15
+ # The underlying connection service instance that the port-forwarding
16
+ # services employ.
17
+ attr_reader :session
18
+
19
+ # A simple class for representing a requested remote forwarded port.
20
+ Remote = Struct.new(:host, :port) #:nodoc:
21
+
22
+ # Instantiates a new Forward service instance atop the given connection
23
+ # service session. This will register new channel open handlers to handle
24
+ # the specialized channels that the SSH port forwarding protocols employ.
25
+ def initialize(session)
26
+ @session = session
27
+ self.logger = session.logger
28
+ @remote_forwarded_ports = {}
29
+ @local_forwarded_ports = {}
30
+ @agent_forwarded = false
31
+ @local_forwarded_sockets = {}
32
+
33
+ session.on_open_channel('forwarded-tcpip', &method(:forwarded_tcpip))
34
+ session.on_open_channel('auth-agent', &method(:auth_agent_channel))
35
+ session.on_open_channel('auth-agent@openssh.com', &method(:auth_agent_channel))
36
+ end
37
+
38
+ # Starts listening for connections on the local host, and forwards them
39
+ # to the specified remote host/port via the SSH connection. This method
40
+ # accepts either three or four arguments. When four arguments are given,
41
+ # they are:
42
+ #
43
+ # * the local address to bind to
44
+ # * the local port to listen on
45
+ # * the remote host to forward connections to
46
+ # * the port on the remote host to connect to
47
+ #
48
+ # If three arguments are given, it is as if the local bind address is
49
+ # "127.0.0.1", and the rest are applied as above.
50
+ #
51
+ # To request an ephemeral port on the remote server, provide 0 (zero) for
52
+ # the port number. In all cases, this method will return the port that
53
+ # has been assigned.
54
+ #
55
+ # ssh.forward.local(1234, "www.capify.org", 80)
56
+ # assigned_port = ssh.forward.local("0.0.0.0", 0, "www.capify.org", 80)
57
+ def local(*args)
58
+ if args.length < 3 || args.length > 4
59
+ raise ArgumentError, "expected 3 or 4 parameters, got #{args.length}"
60
+ end
61
+
62
+ local_port_type = :long
63
+
64
+ socket = begin
65
+ if defined?(UNIXServer) and args.first.class == UNIXServer
66
+ local_port_type = :string
67
+ args.shift
68
+ else
69
+ bind_address = "127.0.0.1"
70
+ bind_address = args.shift if args.first.is_a?(String) && args.first =~ /\D/
71
+ local_port = args.shift.to_i
72
+ local_port_type = :long
73
+ TCPServer.new(bind_address, local_port)
74
+ end
75
+ end
76
+
77
+ local_port = socket.addr[1] if local_port == 0 # ephemeral port was requested
78
+ remote_host = args.shift
79
+ remote_port = args.shift.to_i
80
+
81
+ @local_forwarded_ports[[local_port, bind_address]] = socket
82
+
83
+ session.listen_to(socket) do |server|
84
+ client = server.accept
85
+ debug { "received connection on #{socket}" }
86
+
87
+ channel = session.open_channel("direct-tcpip", :string, remote_host, :long,
88
+ remote_port, :string, bind_address, local_port_type, local_port) do |achannel|
89
+ achannel.info { "direct channel established" }
90
+ end
91
+
92
+ prepare_client(client, channel, :local)
93
+
94
+ channel.on_open_failed do |ch, code, description|
95
+ channel.error { "could not establish direct channel: #{description} (#{code})" }
96
+ session.stop_listening_to(channel[:socket])
97
+ channel[:socket].close
98
+ end
99
+ end
100
+
101
+ local_port
102
+ end
103
+
104
+ # Terminates an active local forwarded port.
105
+ #
106
+ # ssh.forward.cancel_local(1234)
107
+ # ssh.forward.cancel_local(1234, "0.0.0.0")
108
+ def cancel_local(port, bind_address="127.0.0.1")
109
+ socket = @local_forwarded_ports.delete([port, bind_address])
110
+ socket.shutdown rescue nil
111
+ socket.close rescue nil
112
+ session.stop_listening_to(socket)
113
+ end
114
+
115
+ # Returns a list of all active locally forwarded ports. The returned value
116
+ # is an array of arrays, where each element is a two-element tuple
117
+ # consisting of the local port and bind address corresponding to the
118
+ # forwarding port.
119
+ def active_locals
120
+ @local_forwarded_ports.keys
121
+ end
122
+
123
+ # Starts listening for connections on the local host, and forwards them
124
+ # to the specified remote socket via the SSH connection. This will
125
+ # (re)create the local socket file. The remote server needs to have the
126
+ # socket file already available.
127
+ #
128
+ # ssh.forward.local_socket('/tmp/local.sock', '/tmp/remote.sock')
129
+ def local_socket(local_socket_path, remote_socket_path)
130
+ File.delete(local_socket_path) if File.exist?(local_socket_path)
131
+ socket = Socket.unix_server_socket(local_socket_path)
132
+
133
+ @local_forwarded_sockets[local_socket_path] = socket
134
+
135
+ session.listen_to(socket) do |server|
136
+ client = server.accept[0]
137
+ debug { "received connection on #{socket}" }
138
+
139
+ channel = session.open_channel("direct-streamlocal@openssh.com",
140
+ :string, remote_socket_path,
141
+ :string, nil,
142
+ :long, 0) do |achannel|
143
+ achannel.info { "direct channel established" }
144
+ end
145
+
146
+ prepare_client(client, channel, :local)
147
+
148
+ channel.on_open_failed do |ch, code, description|
149
+ channel.error { "could not establish direct channel: #{description} (#{code})" }
150
+ session.stop_listening_to(channel[:socket])
151
+ channel[:socket].close
152
+ end
153
+ end
154
+
155
+ local_socket_path
156
+ end
157
+
158
+ # Terminates an active local forwarded socket.
159
+ #
160
+ # ssh.forward.cancel_local_socket('/tmp/foo.sock')
161
+ def cancel_local_socket(local_socket_path)
162
+ socket = @local_forwarded_sockets.delete(local_socket_path)
163
+ socket.shutdown rescue nil
164
+ socket.close rescue nil
165
+ session.stop_listening_to(socket)
166
+ end
167
+
168
+ # Returns a list of all active locally forwarded sockets. The returned value
169
+ # is an array of Unix domain socket file paths.
170
+ def active_local_sockets
171
+ @local_forwarded_sockets.keys
172
+ end
173
+
174
+ # Requests that all connections on the given remote-port be forwarded via
175
+ # the local host to the given port/host. The last argument describes the
176
+ # bind address on the remote host, and defaults to 127.0.0.1.
177
+ #
178
+ # This method will return immediately, but the port will not actually be
179
+ # forwarded immediately. If the remote server is not able to begin the
180
+ # listener for this request, an exception will be raised asynchronously.
181
+ #
182
+ # To request an ephemeral port on the remote server, provide 0 (zero) for
183
+ # the port number. The assigned port will show up in the # #active_remotes
184
+ # list.
185
+ #
186
+ # remote_host is interpreted by the server per RFC 4254, which has these
187
+ # special values:
188
+ #
189
+ # - "" means that connections are to be accepted on all protocol
190
+ # families supported by the SSH implementation.
191
+ # - "0.0.0.0" means to listen on all IPv4 addresses.
192
+ # - "::" means to listen on all IPv6 addresses.
193
+ # - "localhost" means to listen on all protocol families supported by
194
+ # the SSH implementation on loopback addresses only ([RFC3330] and
195
+ # [RFC3513]).
196
+ # - "127.0.0.1" and "::1" indicate listening on the loopback
197
+ # interfaces for IPv4 and IPv6, respectively.
198
+ #
199
+ # You may pass a block that will be called when the the port forward
200
+ # request receives a response. This block will be passed the remote_port
201
+ # that was actually bound to, or nil if the binding failed. If the block
202
+ # returns :no_exception, the "failed binding" exception will not be thrown.
203
+ #
204
+ # If you want to block until the port is active, you could do something
205
+ # like this:
206
+ #
207
+ # got_remote_port = nil
208
+ # remote(port, host, remote_port, remote_host) do |actual_remote_port|
209
+ # got_remote_port = actual_remote_port || :error
210
+ # :no_exception # will yield the exception on my own thread
211
+ # end
212
+ # session.loop { !got_remote_port }
213
+ # if got_remote_port == :error
214
+ # raise Net::SSH::Exception, "remote forwarding request failed"
215
+ # end
216
+ #
217
+ def remote(port, host, remote_port, remote_host="127.0.0.1")
218
+ session.send_global_request("tcpip-forward", :string, remote_host, :long, remote_port) do |success, response|
219
+ if success
220
+ remote_port = response.read_long if remote_port == 0
221
+ debug { "remote forward from remote #{remote_host}:#{remote_port} to #{host}:#{port} established" }
222
+ @remote_forwarded_ports[[remote_port, remote_host]] = Remote.new(host, port)
223
+ yield remote_port, remote_host if block_given?
224
+ else
225
+ instruction = if block_given?
226
+ yield :error
227
+ end
228
+ unless instruction == :no_exception
229
+ error { "remote forwarding request failed" }
230
+ raise Net::SSH::Exception, "remote forwarding request failed"
231
+ end
232
+ end
233
+ end
234
+ end
235
+
236
+ # an alias, for token backwards compatibility with the 1.x API
237
+ alias :remote_to :remote
238
+
239
+ # Requests that a remote forwarded port be cancelled. The remote forwarded
240
+ # port on the remote host, bound to the given address on the remote host,
241
+ # will be terminated, but not immediately. This method returns immediately
242
+ # after queueing the request to be sent to the server. If for some reason
243
+ # the port cannot be cancelled, an exception will be raised (asynchronously).
244
+ #
245
+ # If you want to know when the connection has been cancelled, it will no
246
+ # longer be present in the #active_remotes list. If you want to block until
247
+ # the port is no longer active, you could do something like this:
248
+ #
249
+ # ssh.forward.cancel_remote(1234, "0.0.0.0")
250
+ # ssh.loop { ssh.forward.active_remotes.include?([1234, "0.0.0.0"]) }
251
+ def cancel_remote(port, host="127.0.0.1")
252
+ session.send_global_request("cancel-tcpip-forward", :string, host, :long, port) do |success, response|
253
+ if success
254
+ @remote_forwarded_ports.delete([port, host])
255
+ else
256
+ raise Net::SSH::Exception, "could not cancel remote forward request on #{host}:#{port}"
257
+ end
258
+ end
259
+ end
260
+
261
+ # Returns all active forwarded remote ports. The returned value is an
262
+ # array of two-element tuples, where the first element is the port on the
263
+ # remote host and the second is the bind address.
264
+ def active_remotes
265
+ @remote_forwarded_ports.keys
266
+ end
267
+
268
+ # Returns all active remote forwarded ports and where they forward to. The
269
+ # returned value is a hash from [<forwarding port on the local host>, <local forwarding address>]
270
+ # to [<port on the remote host>, <remote bind address>].
271
+ def active_remote_destinations
272
+ @remote_forwarded_ports.each_with_object({}) do |(remote, local), result|
273
+ result[[local.port, local.host]] = remote
274
+ end
275
+ end
276
+
277
+ # Enables SSH agent forwarding on the given channel. The forwarded agent
278
+ # will remain active even after the channel closes--the channel is only
279
+ # used as the transport for enabling the forwarded connection. You should
280
+ # never need to call this directly--it is called automatically the first
281
+ # time a session channel is opened, when the connection was created with
282
+ # :forward_agent set to true:
283
+ #
284
+ # Net::SSH.start("remote.host", "me", :forward_agent => true) do |ssh|
285
+ # ssh.open_channel do |ch|
286
+ # # agent will be automatically forwarded by this point
287
+ # end
288
+ # ssh.loop
289
+ # end
290
+ def agent(channel)
291
+ return if @agent_forwarded
292
+
293
+ @agent_forwarded = true
294
+
295
+ channel.send_channel_request("auth-agent-req@openssh.com") do |achannel, success|
296
+ if success
297
+ debug { "authentication agent forwarding is active" }
298
+ else
299
+ achannel.send_channel_request("auth-agent-req") do |a2channel, success2|
300
+ if success2
301
+ debug { "authentication agent forwarding is active" }
302
+ else
303
+ error { "could not establish forwarding of authentication agent" }
304
+ end
305
+ end
306
+ end
307
+ end
308
+ end
309
+
310
+ private
311
+
312
+ # Perform setup operations that are common to all forwarded channels.
313
+ # +client+ is a socket, +channel+ is the channel that was just created,
314
+ # and +type+ is an arbitrary string describing the type of the channel.
315
+ def prepare_client(client, channel, type)
316
+ client.extend(Net::SSH::BufferedIo)
317
+ client.extend(Net::SSH::ForwardedBufferedIo)
318
+ client.logger = logger
319
+
320
+ session.listen_to(client)
321
+ channel[:socket] = client
322
+
323
+ channel.on_data do |ch, data|
324
+ debug { "data:#{data.length} on #{type} forwarded channel" }
325
+ ch[:socket].enqueue(data)
326
+ end
327
+
328
+ channel.on_eof do |ch|
329
+ debug { "eof #{type} on #{type} forwarded channel" }
330
+ begin
331
+ ch[:socket].send_pending
332
+ ch[:socket].shutdown Socket::SHUT_WR
333
+ rescue IOError => e
334
+ if e.message =~ /closed/ then
335
+ debug { "epipe in on_eof => shallowing exception:#{e}" }
336
+ else
337
+ raise
338
+ end
339
+ rescue Errno::EPIPE => e
340
+ debug { "epipe in on_eof => shallowing exception:#{e}" }
341
+ rescue Errno::ENOTCONN => e
342
+ debug { "enotconn in on_eof => shallowing exception:#{e}" }
343
+ end
344
+ end
345
+
346
+ channel.on_close do |ch|
347
+ debug { "closing #{type} forwarded channel" }
348
+ ch[:socket].close if !client.closed?
349
+ session.stop_listening_to(ch[:socket])
350
+ end
351
+
352
+ channel.on_process do |ch|
353
+ if ch[:socket].closed?
354
+ ch.info { "#{type} forwarded connection closed" }
355
+ ch.close
356
+ elsif ch[:socket].available > 0
357
+ data = ch[:socket].read_available(8192)
358
+ ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
359
+ ch.send_data(data)
360
+ end
361
+ end
362
+ end
363
+
364
+ # not a real socket, so use a simpler behaviour
365
+ def prepare_simple_client(client, channel, type)
366
+ channel[:socket] = client
367
+
368
+ channel.on_data do |ch, data|
369
+ ch.debug { "data:#{data.length} on #{type} forwarded channel" }
370
+ ch[:socket].send(data)
371
+ end
372
+
373
+ channel.on_process do |ch|
374
+ data = ch[:socket].read(8192)
375
+ if data
376
+ ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
377
+ ch.send_data(data)
378
+ end
379
+ end
380
+ end
381
+
382
+ # The callback used when a new "forwarded-tcpip" channel is requested
383
+ # by the server. This will open a new socket to the host/port specified
384
+ # when the forwarded connection was first requested.
385
+ def forwarded_tcpip(session, channel, packet)
386
+ connected_address = packet.read_string
387
+ connected_port = packet.read_long
388
+ originator_address = packet.read_string
389
+ originator_port = packet.read_long
390
+
391
+ puts "REMOTE 0: #{connected_port} #{connected_address} #{originator_address} #{originator_port}"
392
+ remote = @remote_forwarded_ports[[connected_port, connected_address]]
393
+ if remote.nil?
394
+ raise Net::SSH::ChannelOpenFailed.new(1, "unknown request from remote forwarded connection on #{connected_address}:#{connected_port}")
395
+ end
396
+
397
+ puts "REMOTE: #{remote.host} #{remote.port}"
398
+ client = TCPSocket.new(remote.host, remote.port)
399
+ info { "connected #{connected_address}:#{connected_port} originator #{originator_address}:#{originator_port}" }
400
+
401
+ prepare_client(client, channel, :remote)
402
+ rescue SocketError => err
403
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to remote host (#{remote.host}:#{remote.port}): #{err.message}")
404
+ end
405
+
406
+ # The callback used when an auth-agent channel is requested by the server.
407
+ def auth_agent_channel(session, channel, packet)
408
+ info { "opening auth-agent channel" }
409
+ channel[:invisible] = true
410
+
411
+ begin
412
+ agent = Authentication::Agent.connect(logger, session.options[:agent_socket_factory])
413
+ if (agent.socket.is_a? ::IO)
414
+ prepare_client(agent.socket, channel, :agent)
415
+ else
416
+ prepare_simple_client(agent.socket, channel, :agent)
417
+ end
418
+ rescue Exception => e
419
+ error { "attempted to connect to agent but failed: #{e.class.name} (#{e.message})" }
420
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to authentication agent")
421
+ end
422
+ end
423
+ end
424
+ end
425
+ end
426
+ end
@@ -0,0 +1,147 @@
1
+ module Net
2
+ module SSH
3
+ module Test
4
+ # A mock channel, used for scripting actions in tests. It wraps a
5
+ # Net::SSH::Test::Script instance, and delegates to it for the most part.
6
+ # This class has little real functionality on its own, but rather acts as
7
+ # a convenience for scripting channel-related activity for later comparison
8
+ # in a unit test.
9
+ #
10
+ # story do |session|
11
+ # channel = session.opens_channel
12
+ # channel.sends_exec "ls"
13
+ # channel.gets_data "result of ls"
14
+ # channel.gets_extended_data "some error coming from ls"
15
+ # channel.gets_close
16
+ # channel.sends_close
17
+ # end
18
+ class Channel
19
+ # The Net::SSH::Test::Script instance employed by this mock channel.
20
+ attr_reader :script
21
+
22
+ # Sets the local-id of this channel object (the id assigned by the client).
23
+ attr_writer :local_id
24
+
25
+ # Sets the remote-id of this channel object (the id assigned by the mock-server).
26
+ attr_writer :remote_id
27
+
28
+ # Creates a new Test::Channel instance on top of the given +script+ (which
29
+ # must be a Net::SSH::Test::Script instance).
30
+ def initialize(script)
31
+ @script = script
32
+ @local_id = @remote_id = nil
33
+ end
34
+
35
+ # Returns the local (client-assigned) id for this channel, or a Proc object
36
+ # that will return the local-id later if the local id has not yet been set.
37
+ # (See Net::SSH::Test::Packet#instantiate!.)
38
+ def local_id
39
+ @local_id || Proc.new { @local_id or raise "local-id has not been set yet!" }
40
+ end
41
+
42
+ # Returns the remote (server-assigned) id for this channel, or a Proc object
43
+ # that will return the remote-id later if the remote id has not yet been set.
44
+ # (See Net::SSH::Test::Packet#instantiate!.)
45
+ def remote_id
46
+ @remote_id || Proc.new { @remote_id or raise "remote-id has not been set yet!" }
47
+ end
48
+
49
+ # Because adjacent calls to #gets_data will sometimes cause the data packets
50
+ # to be concatenated (causing expectations in tests to fail), you may
51
+ # need to separate those calls with calls to #inject_remote_delay! (which
52
+ # essentially just mimics receiving an empty data packet):
53
+ #
54
+ # channel.gets_data "abcdefg"
55
+ # channel.inject_remote_delay!
56
+ # channel.gets_data "hijklmn"
57
+ def inject_remote_delay!
58
+ gets_data("")
59
+ end
60
+
61
+ # Scripts the sending of an "exec" channel request packet to the mock
62
+ # server. If +reply+ is true, then the server is expected to reply to the
63
+ # request, otherwise no response to this request will be sent. If +success+
64
+ # is +true+, then the request will be successful, otherwise a failure will
65
+ # be scripted.
66
+ #
67
+ # channel.sends_exec "ls -l"
68
+ def sends_exec(command, reply=true, success=true)
69
+ script.sends_channel_request(self, "exec", reply, command, success)
70
+ end
71
+
72
+ # Scripts the sending of a "subsystem" channel request packet to the mock
73
+ # server. See #sends_exec for a discussion of the meaning of the +reply+
74
+ # and +success+ arguments.
75
+ #
76
+ # channel.sends_subsystem "sftp"
77
+ def sends_subsystem(subsystem, reply=true, success=true)
78
+ script.sends_channel_request(self, "subsystem", reply, subsystem, success)
79
+ end
80
+
81
+ # Scripts the sending of a data packet across the channel.
82
+ #
83
+ # channel.sends_data "foo"
84
+ def sends_data(data)
85
+ script.sends_channel_data(self, data)
86
+ end
87
+
88
+ # Scripts the sending of an EOF packet across the channel.
89
+ #
90
+ # channel.sends_eof
91
+ def sends_eof
92
+ script.sends_channel_eof(self)
93
+ end
94
+
95
+ # Scripts the sending of a "channel close" packet across the channel.
96
+ #
97
+ # channel.sends_close
98
+ def sends_close
99
+ script.sends_channel_close(self)
100
+ end
101
+
102
+ # Scripts the sending of a "request pty" request packet across the channel.
103
+ #
104
+ # channel.sends_request_pty
105
+ def sends_request_pty
106
+ script.sends_channel_request_pty(self)
107
+ end
108
+
109
+ # Scripts the reception of a channel data packet from the remote end.
110
+ #
111
+ # channel.gets_data "bar"
112
+ def gets_data(data)
113
+ script.gets_channel_data(self, data)
114
+ end
115
+
116
+ # Scripts the reception of a channel extended data packet from the remote
117
+ # end.
118
+ #
119
+ # channel.gets_extended_data "whoops"
120
+ def gets_extended_data(data)
121
+ script.gets_channel_extended_data(self, data)
122
+ end
123
+
124
+ # Scripts the reception of an "exit-status" channel request packet.
125
+ #
126
+ # channel.gets_exit_status(127)
127
+ def gets_exit_status(status=0)
128
+ script.gets_channel_request(self, "exit-status", false, status)
129
+ end
130
+
131
+ # Scripts the reception of an EOF packet from the remote end.
132
+ #
133
+ # channel.gets_eof
134
+ def gets_eof
135
+ script.gets_channel_eof(self)
136
+ end
137
+
138
+ # Scripts the reception of a "channel close" packet from the remote end.
139
+ #
140
+ # channel.gets_close
141
+ def gets_close
142
+ script.gets_channel_close(self)
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end