sonixlabs-net-ssh 2.3.0

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 (123) hide show
  1. data/CHANGELOG.rdoc +262 -0
  2. data/Manifest +121 -0
  3. data/README.rdoc +184 -0
  4. data/Rakefile +86 -0
  5. data/Rudyfile +96 -0
  6. data/THANKS.rdoc +19 -0
  7. data/lib/net/ssh.rb +223 -0
  8. data/lib/net/ssh/authentication/agent.rb +179 -0
  9. data/lib/net/ssh/authentication/constants.rb +18 -0
  10. data/lib/net/ssh/authentication/key_manager.rb +253 -0
  11. data/lib/net/ssh/authentication/methods/abstract.rb +60 -0
  12. data/lib/net/ssh/authentication/methods/hostbased.rb +75 -0
  13. data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +70 -0
  14. data/lib/net/ssh/authentication/methods/password.rb +43 -0
  15. data/lib/net/ssh/authentication/methods/publickey.rb +96 -0
  16. data/lib/net/ssh/authentication/pageant.rb +264 -0
  17. data/lib/net/ssh/authentication/session.rb +146 -0
  18. data/lib/net/ssh/buffer.rb +340 -0
  19. data/lib/net/ssh/buffered_io.rb +198 -0
  20. data/lib/net/ssh/config.rb +207 -0
  21. data/lib/net/ssh/connection/channel.rb +630 -0
  22. data/lib/net/ssh/connection/constants.rb +33 -0
  23. data/lib/net/ssh/connection/session.rb +597 -0
  24. data/lib/net/ssh/connection/term.rb +178 -0
  25. data/lib/net/ssh/errors.rb +88 -0
  26. data/lib/net/ssh/key_factory.rb +102 -0
  27. data/lib/net/ssh/known_hosts.rb +129 -0
  28. data/lib/net/ssh/loggable.rb +61 -0
  29. data/lib/net/ssh/packet.rb +102 -0
  30. data/lib/net/ssh/prompt.rb +93 -0
  31. data/lib/net/ssh/proxy/command.rb +75 -0
  32. data/lib/net/ssh/proxy/errors.rb +14 -0
  33. data/lib/net/ssh/proxy/http.rb +94 -0
  34. data/lib/net/ssh/proxy/socks4.rb +70 -0
  35. data/lib/net/ssh/proxy/socks5.rb +142 -0
  36. data/lib/net/ssh/ruby_compat.rb +43 -0
  37. data/lib/net/ssh/service/forward.rb +298 -0
  38. data/lib/net/ssh/test.rb +89 -0
  39. data/lib/net/ssh/test/channel.rb +129 -0
  40. data/lib/net/ssh/test/extensions.rb +152 -0
  41. data/lib/net/ssh/test/kex.rb +44 -0
  42. data/lib/net/ssh/test/local_packet.rb +51 -0
  43. data/lib/net/ssh/test/packet.rb +81 -0
  44. data/lib/net/ssh/test/remote_packet.rb +38 -0
  45. data/lib/net/ssh/test/script.rb +157 -0
  46. data/lib/net/ssh/test/socket.rb +64 -0
  47. data/lib/net/ssh/transport/algorithms.rb +386 -0
  48. data/lib/net/ssh/transport/cipher_factory.rb +79 -0
  49. data/lib/net/ssh/transport/constants.rb +30 -0
  50. data/lib/net/ssh/transport/hmac.rb +42 -0
  51. data/lib/net/ssh/transport/hmac/abstract.rb +79 -0
  52. data/lib/net/ssh/transport/hmac/md5.rb +12 -0
  53. data/lib/net/ssh/transport/hmac/md5_96.rb +11 -0
  54. data/lib/net/ssh/transport/hmac/none.rb +15 -0
  55. data/lib/net/ssh/transport/hmac/sha1.rb +13 -0
  56. data/lib/net/ssh/transport/hmac/sha1_96.rb +11 -0
  57. data/lib/net/ssh/transport/hmac/sha2_256.rb +15 -0
  58. data/lib/net/ssh/transport/hmac/sha2_256_96.rb +13 -0
  59. data/lib/net/ssh/transport/hmac/sha2_512.rb +14 -0
  60. data/lib/net/ssh/transport/hmac/sha2_512_96.rb +13 -0
  61. data/lib/net/ssh/transport/identity_cipher.rb +55 -0
  62. data/lib/net/ssh/transport/kex.rb +17 -0
  63. data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +208 -0
  64. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +80 -0
  65. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +15 -0
  66. data/lib/net/ssh/transport/key_expander.rb +26 -0
  67. data/lib/net/ssh/transport/openssl.rb +127 -0
  68. data/lib/net/ssh/transport/packet_stream.rb +235 -0
  69. data/lib/net/ssh/transport/server_version.rb +71 -0
  70. data/lib/net/ssh/transport/session.rb +278 -0
  71. data/lib/net/ssh/transport/state.rb +206 -0
  72. data/lib/net/ssh/verifiers/lenient.rb +30 -0
  73. data/lib/net/ssh/verifiers/null.rb +12 -0
  74. data/lib/net/ssh/verifiers/strict.rb +53 -0
  75. data/lib/net/ssh/version.rb +62 -0
  76. data/lib/sonixlabs-net-ssh.rb +1 -0
  77. data/net-ssh.gemspec +145 -0
  78. data/setup.rb +1585 -0
  79. data/support/arcfour_check.rb +20 -0
  80. data/support/ssh_tunnel_bug.rb +65 -0
  81. data/test/authentication/methods/common.rb +28 -0
  82. data/test/authentication/methods/test_abstract.rb +51 -0
  83. data/test/authentication/methods/test_hostbased.rb +114 -0
  84. data/test/authentication/methods/test_keyboard_interactive.rb +100 -0
  85. data/test/authentication/methods/test_password.rb +52 -0
  86. data/test/authentication/methods/test_publickey.rb +148 -0
  87. data/test/authentication/test_agent.rb +205 -0
  88. data/test/authentication/test_key_manager.rb +171 -0
  89. data/test/authentication/test_session.rb +106 -0
  90. data/test/common.rb +107 -0
  91. data/test/configs/eqsign +3 -0
  92. data/test/configs/exact_match +8 -0
  93. data/test/configs/host_plus +10 -0
  94. data/test/configs/multihost +4 -0
  95. data/test/configs/wild_cards +14 -0
  96. data/test/connection/test_channel.rb +467 -0
  97. data/test/connection/test_session.rb +488 -0
  98. data/test/test_all.rb +9 -0
  99. data/test/test_buffer.rb +336 -0
  100. data/test/test_buffered_io.rb +63 -0
  101. data/test/test_config.rb +120 -0
  102. data/test/test_key_factory.rb +79 -0
  103. data/test/transport/hmac/test_md5.rb +39 -0
  104. data/test/transport/hmac/test_md5_96.rb +25 -0
  105. data/test/transport/hmac/test_none.rb +34 -0
  106. data/test/transport/hmac/test_sha1.rb +34 -0
  107. data/test/transport/hmac/test_sha1_96.rb +25 -0
  108. data/test/transport/hmac/test_sha2_256.rb +35 -0
  109. data/test/transport/hmac/test_sha2_256_96.rb +25 -0
  110. data/test/transport/hmac/test_sha2_512.rb +35 -0
  111. data/test/transport/hmac/test_sha2_512_96.rb +25 -0
  112. data/test/transport/kex/test_diffie_hellman_group1_sha1.rb +146 -0
  113. data/test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb +92 -0
  114. data/test/transport/kex/test_diffie_hellman_group_exchange_sha256.rb +33 -0
  115. data/test/transport/test_algorithms.rb +308 -0
  116. data/test/transport/test_cipher_factory.rb +213 -0
  117. data/test/transport/test_hmac.rb +34 -0
  118. data/test/transport/test_identity_cipher.rb +40 -0
  119. data/test/transport/test_packet_stream.rb +736 -0
  120. data/test/transport/test_server_version.rb +78 -0
  121. data/test/transport/test_session.rb +315 -0
  122. data/test/transport/test_state.rb +179 -0
  123. metadata +178 -0
@@ -0,0 +1,142 @@
1
+ require 'socket'
2
+ require 'net/ssh/ruby_compat'
3
+ require 'net/ssh/proxy/errors'
4
+
5
+ module Net
6
+ module SSH
7
+ module Proxy
8
+
9
+ # An implementation of a SOCKS5 proxy. To use it, instantiate it, then
10
+ # pass the instantiated object via the :proxy key to Net::SSH.start:
11
+ #
12
+ # require 'net/ssh/proxy/socks5'
13
+ #
14
+ # proxy = Net::SSH::Proxy::SOCKS5.new('proxy.host', proxy_port,
15
+ # :user => 'user', :password => "password")
16
+ # Net::SSH.start('host', 'user', :proxy => proxy) do |ssh|
17
+ # ...
18
+ # end
19
+ class SOCKS5
20
+ # The SOCKS protocol version used by this class
21
+ VERSION = 5
22
+
23
+ # The SOCKS authentication type for requests without authentication
24
+ METHOD_NO_AUTH = 0
25
+
26
+ # The SOCKS authentication type for requests via username/password
27
+ METHOD_PASSWD = 2
28
+
29
+ # The SOCKS authentication type for when there are no supported
30
+ # authentication methods.
31
+ METHOD_NONE = 0xFF
32
+
33
+ # The SOCKS packet type for requesting a proxy connection.
34
+ CMD_CONNECT = 1
35
+
36
+ # The SOCKS address type for connections via IP address.
37
+ ATYP_IPV4 = 1
38
+
39
+ # The SOCKS address type for connections via domain name.
40
+ ATYP_DOMAIN = 3
41
+
42
+ # The SOCKS response code for a successful operation.
43
+ SUCCESS = 0
44
+
45
+ # The proxy's host name or IP address
46
+ attr_reader :proxy_host
47
+
48
+ # The proxy's port number
49
+ attr_reader :proxy_port
50
+
51
+ # The map of options given at initialization
52
+ attr_reader :options
53
+
54
+ # Create a new proxy connection to the given proxy host and port.
55
+ # Optionally, :user and :password options may be given to
56
+ # identify the username and password with which to authenticate.
57
+ def initialize(proxy_host, proxy_port=1080, options={})
58
+ @proxy_host = proxy_host
59
+ @proxy_port = proxy_port
60
+ @options = options
61
+ end
62
+
63
+ # Return a new socket connected to the given host and port via the
64
+ # proxy that was requested when the socket factory was instantiated.
65
+ def open(host, port)
66
+ socket = TCPSocket.new(proxy_host, proxy_port)
67
+
68
+ methods = [METHOD_NO_AUTH]
69
+ methods << METHOD_PASSWD if options[:user]
70
+
71
+ packet = [VERSION, methods.size, *methods].pack("C*")
72
+ socket.send packet, 0
73
+
74
+ version, method = socket.recv(2).unpack("CC")
75
+ if version != VERSION
76
+ socket.close
77
+ raise Net::SSH::Proxy::Error, "invalid SOCKS version (#{version})"
78
+ end
79
+
80
+ if method == METHOD_NONE
81
+ socket.close
82
+ raise Net::SSH::Proxy::Error, "no supported authorization methods"
83
+ end
84
+
85
+ negotiate_password(socket) if method == METHOD_PASSWD
86
+
87
+ packet = [VERSION, CMD_CONNECT, 0].pack("C*")
88
+
89
+ if host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
90
+ packet << [ATYP_IPV4, $1.to_i, $2.to_i, $3.to_i, $4.to_i].pack("C*")
91
+ else
92
+ packet << [ATYP_DOMAIN, host.length, host].pack("CCA*")
93
+ end
94
+
95
+ packet << [port].pack("n")
96
+ socket.send packet, 0
97
+
98
+ version, reply, = socket.recv(2).unpack("C*")
99
+ socket.recv(1)
100
+ address_type = socket.recv(1).getbyte(0)
101
+ case address_type
102
+ when 1
103
+ socket.recv(4) # get four bytes for IPv4 address
104
+ when 3
105
+ len = socket.recv(1).getbyte(0)
106
+ hostname = socket.recv(len)
107
+ when 4
108
+ ipv6addr hostname = socket.recv(16)
109
+ else
110
+ socket.close
111
+ raise ConnectionError, "Illegal response type"
112
+ end
113
+ portnum = socket.recv(2)
114
+
115
+ unless reply == SUCCESS
116
+ socket.close
117
+ raise ConnectError, "#{reply}"
118
+ end
119
+
120
+ return socket
121
+ end
122
+
123
+ private
124
+
125
+ # Simple username/password negotiation with the SOCKS5 server.
126
+ def negotiate_password(socket)
127
+ packet = [0x01, options[:user].length, options[:user],
128
+ options[:password].length, options[:password]].pack("CCA*CA*")
129
+ socket.send packet, 0
130
+
131
+ version, status = socket.recv(2).unpack("CC")
132
+
133
+ if status != SUCCESS
134
+ socket.close
135
+ raise UnauthorizedError, "could not authorize user"
136
+ end
137
+ end
138
+ end
139
+
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,43 @@
1
+ require 'thread'
2
+
3
+ class String
4
+ if RUBY_VERSION < "1.9"
5
+ def getbyte(index)
6
+ self[index]
7
+ end
8
+ end
9
+ end
10
+
11
+ module Net; module SSH
12
+
13
+ # This class contains miscellaneous patches and workarounds
14
+ # for different ruby implementations.
15
+ class Compat
16
+
17
+ # A workaround for an IO#select threading bug in certain versions of MRI 1.8.
18
+ # See: http://net-ssh.lighthouseapp.com/projects/36253/tickets/1-ioselect-threading-bug-in-ruby-18
19
+ # The root issue is documented here: http://redmine.ruby-lang.org/issues/show/1993
20
+ if RUBY_VERSION >= '1.9' || RUBY_PLATFORM == 'java'
21
+ def self.io_select(*params)
22
+ IO.select(*params)
23
+ end
24
+ else
25
+ SELECT_MUTEX = Mutex.new
26
+ def self.io_select(*params)
27
+ # It should be safe to wrap calls in a mutex when the timeout is 0
28
+ # (that is, the call is not supposed to block).
29
+ # We leave blocking calls unprotected to avoid causing deadlocks.
30
+ # This should still catch the main case for Capistrano users.
31
+ if params[3] == 0
32
+ SELECT_MUTEX.synchronize do
33
+ IO.select(*params)
34
+ end
35
+ else
36
+ IO.select(*params)
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end; end
@@ -0,0 +1,298 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'net/ssh/loggable'
3
+
4
+ module Net; module SSH; module Service
5
+
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
+
32
+ session.on_open_channel('forwarded-tcpip', &method(:forwarded_tcpip))
33
+ session.on_open_channel('auth-agent', &method(:auth_agent_channel))
34
+ session.on_open_channel('auth-agent@openssh.com', &method(:auth_agent_channel))
35
+ end
36
+
37
+ # Starts listening for connections on the local host, and forwards them
38
+ # to the specified remote host/port via the SSH connection. This method
39
+ # accepts either three or four arguments. When four arguments are given,
40
+ # they are:
41
+ #
42
+ # * the local address to bind to
43
+ # * the local port to listen on
44
+ # * the remote host to forward connections to
45
+ # * the port on the remote host to connect to
46
+ #
47
+ # If three arguments are given, it is as if the local bind address is
48
+ # "127.0.0.1", and the rest are applied as above.
49
+ #
50
+ # ssh.forward.local(1234, "www.capify.org", 80)
51
+ # ssh.forward.local("0.0.0.0", 1234, "www.capify.org", 80)
52
+ def local(*args)
53
+ if args.length < 3 || args.length > 4
54
+ raise ArgumentError, "expected 3 or 4 parameters, got #{args.length}"
55
+ end
56
+
57
+ local_port_type = :long
58
+
59
+ socket = begin
60
+ if args.first.class == UNIXServer
61
+ local_port_type = :string
62
+ args.shift
63
+ else
64
+ bind_address = "127.0.0.1"
65
+ bind_address = args.shift if args.first.is_a?(String) && args.first =~ /\D/
66
+ local_port = args.shift.to_i
67
+ local_port_type = :long
68
+ TCPServer.new(bind_address, local_port)
69
+ end
70
+ end
71
+
72
+ remote_host = args.shift
73
+ remote_port = args.shift.to_i
74
+
75
+ @local_forwarded_ports[[local_port, bind_address]] = socket
76
+
77
+ session.listen_to(socket) do |server|
78
+ client = server.accept
79
+ debug { "received connection on #{socket}" }
80
+
81
+ channel = session.open_channel("direct-tcpip", :string, remote_host, :long, remote_port, :string, bind_address, local_port_type, local_port) do |achannel|
82
+ achannel.info { "direct channel established" }
83
+ end
84
+
85
+ prepare_client(client, channel, :local)
86
+
87
+ channel.on_open_failed do |ch, code, description|
88
+ channel.error { "could not establish direct channel: #{description} (#{code})" }
89
+ channel[:socket].close
90
+ end
91
+ end
92
+ end
93
+
94
+ # Terminates an active local forwarded port. If no such forwarded port
95
+ # exists, this will raise an exception. Otherwise, the forwarded connection
96
+ # is terminated.
97
+ #
98
+ # ssh.forward.cancel_local(1234)
99
+ # ssh.forward.cancel_local(1234, "0.0.0.0")
100
+ def cancel_local(port, bind_address="127.0.0.1")
101
+ socket = @local_forwarded_ports.delete([port, bind_address])
102
+ socket.shutdown rescue nil
103
+ socket.close rescue nil
104
+ session.stop_listening_to(socket)
105
+ end
106
+
107
+ # Returns a list of all active locally forwarded ports. The returned value
108
+ # is an array of arrays, where each element is a two-element tuple
109
+ # consisting of the local port and bind address corresponding to the
110
+ # forwarding port.
111
+ def active_locals
112
+ @local_forwarded_ports.keys
113
+ end
114
+
115
+ # Requests that all connections on the given remote-port be forwarded via
116
+ # the local host to the given port/host. The last argument describes the
117
+ # bind address on the remote host, and defaults to 127.0.0.1.
118
+ #
119
+ # This method will return immediately, but the port will not actually be
120
+ # forwarded immediately. If the remote server is not able to begin the
121
+ # listener for this request, an exception will be raised asynchronously.
122
+ #
123
+ # If you want to know when the connection is active, it will show up in the
124
+ # #active_remotes list. If you want to block until the port is active, you
125
+ # could do something like this:
126
+ #
127
+ # ssh.forward.remote(80, "www.google.com", 1234, "0.0.0.0")
128
+ # ssh.loop { !ssh.forward.active_remotes.include?([1234, "0.0.0.0"]) }
129
+ def remote(port, host, remote_port, remote_host="127.0.0.1")
130
+ session.send_global_request("tcpip-forward", :string, remote_host, :long, remote_port) do |success, response|
131
+ if success
132
+ debug { "remote forward from remote #{remote_host}:#{remote_port} to #{host}:#{port} established" }
133
+ @remote_forwarded_ports[[remote_port, remote_host]] = Remote.new(host, port)
134
+ else
135
+ error { "remote forwarding request failed" }
136
+ raise Net::SSH::Exception, "remote forwarding request failed"
137
+ end
138
+ end
139
+ end
140
+
141
+ # an alias, for token backwards compatibility with the 1.x API
142
+ alias :remote_to :remote
143
+
144
+ # Requests that a remote forwarded port be cancelled. The remote forwarded
145
+ # port on the remote host, bound to the given address on the remote host,
146
+ # will be terminated, but not immediately. This method returns immediately
147
+ # after queueing the request to be sent to the server. If for some reason
148
+ # the port cannot be cancelled, an exception will be raised (asynchronously).
149
+ #
150
+ # If you want to know when the connection has been cancelled, it will no
151
+ # longer be present in the #active_remotes list. If you want to block until
152
+ # the port is no longer active, you could do something like this:
153
+ #
154
+ # ssh.forward.cancel_remote(1234, "0.0.0.0")
155
+ # ssh.loop { ssh.forward.active_remotes.include?([1234, "0.0.0.0"]) }
156
+ def cancel_remote(port, host="127.0.0.1")
157
+ session.send_global_request("cancel-tcpip-forward", :string, host, :long, port) do |success, response|
158
+ if success
159
+ @remote_forwarded_ports.delete([port, host])
160
+ else
161
+ raise Net::SSH::Exception, "could not cancel remote forward request on #{host}:#{port}"
162
+ end
163
+ end
164
+ end
165
+
166
+ # Returns all active forwarded remote ports. The returned value is an
167
+ # array of two-element tuples, where the first element is the port on the
168
+ # remote host and the second is the bind address.
169
+ def active_remotes
170
+ @remote_forwarded_ports.keys
171
+ end
172
+
173
+ # Enables SSH agent forwarding on the given channel. The forwarded agent
174
+ # will remain active even after the channel closes--the channel is only
175
+ # used as the transport for enabling the forwarded connection. You should
176
+ # never need to call this directly--it is called automatically the first
177
+ # time a session channel is opened, when the connection was created with
178
+ # :forward_agent set to true:
179
+ #
180
+ # Net::SSH.start("remote.host", "me", :forwrd_agent => true) do |ssh|
181
+ # ssh.open_channel do |ch|
182
+ # # agent will be automatically forwarded by this point
183
+ # end
184
+ # ssh.loop
185
+ # end
186
+ def agent(channel)
187
+ return if @agent_forwarded
188
+ @agent_forwarded = true
189
+
190
+ channel.send_channel_request("auth-agent-req@openssh.com") do |achannel, success|
191
+ if success
192
+ debug { "authentication agent forwarding is active" }
193
+ else
194
+ achannel.send_channel_request("auth-agent-req") do |a2channel, success2|
195
+ if success2
196
+ debug { "authentication agent forwarding is active" }
197
+ else
198
+ error { "could not establish forwarding of authentication agent" }
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ private
206
+
207
+ # Perform setup operations that are common to all forwarded channels.
208
+ # +client+ is a socket, +channel+ is the channel that was just created,
209
+ # and +type+ is an arbitrary string describing the type of the channel.
210
+ def prepare_client(client, channel, type)
211
+ client.extend(Net::SSH::BufferedIo)
212
+ client.extend(Net::SSH::ForwardedBufferedIo)
213
+ client.logger = logger
214
+
215
+ session.listen_to(client)
216
+ channel[:socket] = client
217
+
218
+ channel.on_data do |ch, data|
219
+ debug { "data:#{data.length} on #{type} forwarded channel" }
220
+ ch[:socket].enqueue(data)
221
+ end
222
+
223
+ # Handles server close on the sending side by Miklós Fazekas
224
+ channel.on_eof do |ch|
225
+ debug { "eof #{type} on #{type} forwarded channel" }
226
+ begin
227
+ ch[:socket].send_pending
228
+ ch[:socket].shutdown Socket::SHUT_WR
229
+ rescue IOError => e
230
+ if e.message =~ /closed/ then
231
+ debug { "epipe in on_eof => shallowing exception:#{e}" }
232
+ else
233
+ raise
234
+ end
235
+ rescue Errno::EPIPE => e
236
+ debug { "epipe in on_eof => shallowing exception:#{e}" }
237
+ rescue Errno::ENOTCONN => e
238
+ debug { "enotconn in on_eof => shallowing exception:#{e}" }
239
+ end
240
+ end
241
+
242
+ channel.on_close do |ch|
243
+ debug { "closing #{type} forwarded channel" }
244
+ ch[:socket].close if !client.closed?
245
+ session.stop_listening_to(ch[:socket])
246
+ end
247
+
248
+ channel.on_process do |ch|
249
+ if ch[:socket].closed?
250
+ ch.info { "#{type} forwarded connection closed" }
251
+ ch.close
252
+ elsif ch[:socket].available > 0
253
+ data = ch[:socket].read_available(8192)
254
+ ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
255
+ ch.send_data(data)
256
+ end
257
+ end
258
+ end
259
+
260
+ # The callback used when a new "forwarded-tcpip" channel is requested
261
+ # by the server. This will open a new socket to the host/port specified
262
+ # when the forwarded connection was first requested.
263
+ def forwarded_tcpip(session, channel, packet)
264
+ connected_address = packet.read_string
265
+ connected_port = packet.read_long
266
+ originator_address = packet.read_string
267
+ originator_port = packet.read_long
268
+
269
+ remote = @remote_forwarded_ports[[connected_port, connected_address]]
270
+
271
+ if remote.nil?
272
+ raise Net::SSH::ChannelOpenFailed.new(1, "unknown request from remote forwarded connection on #{connected_address}:#{connected_port}")
273
+ end
274
+
275
+ client = TCPSocket.new(remote.host, remote.port)
276
+ info { "connected #{connected_address}:#{connected_port} originator #{originator_address}:#{originator_port}" }
277
+
278
+ prepare_client(client, channel, :remote)
279
+ rescue SocketError => err
280
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to remote host (#{remote.host}:#{remote.port}): #{err.message}")
281
+ end
282
+
283
+ # The callback used when an auth-agent channel is requested by the server.
284
+ def auth_agent_channel(session, channel, packet)
285
+ info { "opening auth-agent channel" }
286
+ channel[:invisible] = true
287
+
288
+ begin
289
+ agent = Authentication::Agent.connect(logger)
290
+ prepare_client(agent.socket, channel, :agent)
291
+ rescue Exception => e
292
+ error { "attempted to connect to agent but failed: #{e.class.name} (#{e.message})" }
293
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to authentication agent")
294
+ end
295
+ end
296
+ end
297
+
298
+ end; end; end