minmb-net-ssh 2.5.1

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 (137) hide show
  1. data/CHANGELOG.rdoc +291 -0
  2. data/Manifest +132 -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 +23 -0
  9. data/lib/net/ssh/authentication/agent/java_pageant.rb +85 -0
  10. data/lib/net/ssh/authentication/agent/socket.rb +170 -0
  11. data/lib/net/ssh/authentication/constants.rb +18 -0
  12. data/lib/net/ssh/authentication/key_manager.rb +253 -0
  13. data/lib/net/ssh/authentication/methods/abstract.rb +60 -0
  14. data/lib/net/ssh/authentication/methods/hostbased.rb +75 -0
  15. data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +70 -0
  16. data/lib/net/ssh/authentication/methods/password.rb +43 -0
  17. data/lib/net/ssh/authentication/methods/publickey.rb +96 -0
  18. data/lib/net/ssh/authentication/pageant.rb +301 -0
  19. data/lib/net/ssh/authentication/session.rb +154 -0
  20. data/lib/net/ssh/buffer.rb +350 -0
  21. data/lib/net/ssh/buffered_io.rb +207 -0
  22. data/lib/net/ssh/config.rb +207 -0
  23. data/lib/net/ssh/connection/channel.rb +630 -0
  24. data/lib/net/ssh/connection/constants.rb +33 -0
  25. data/lib/net/ssh/connection/session.rb +603 -0
  26. data/lib/net/ssh/connection/term.rb +178 -0
  27. data/lib/net/ssh/errors.rb +88 -0
  28. data/lib/net/ssh/key_factory.rb +107 -0
  29. data/lib/net/ssh/known_hosts.rb +141 -0
  30. data/lib/net/ssh/loggable.rb +61 -0
  31. data/lib/net/ssh/packet.rb +102 -0
  32. data/lib/net/ssh/prompt.rb +93 -0
  33. data/lib/net/ssh/proxy/command.rb +75 -0
  34. data/lib/net/ssh/proxy/errors.rb +14 -0
  35. data/lib/net/ssh/proxy/http.rb +94 -0
  36. data/lib/net/ssh/proxy/socks4.rb +70 -0
  37. data/lib/net/ssh/proxy/socks5.rb +142 -0
  38. data/lib/net/ssh/ruby_compat.rb +77 -0
  39. data/lib/net/ssh/service/forward.rb +327 -0
  40. data/lib/net/ssh/test.rb +89 -0
  41. data/lib/net/ssh/test/channel.rb +129 -0
  42. data/lib/net/ssh/test/extensions.rb +152 -0
  43. data/lib/net/ssh/test/kex.rb +44 -0
  44. data/lib/net/ssh/test/local_packet.rb +51 -0
  45. data/lib/net/ssh/test/packet.rb +81 -0
  46. data/lib/net/ssh/test/remote_packet.rb +38 -0
  47. data/lib/net/ssh/test/script.rb +157 -0
  48. data/lib/net/ssh/test/socket.rb +64 -0
  49. data/lib/net/ssh/transport/algorithms.rb +407 -0
  50. data/lib/net/ssh/transport/cipher_factory.rb +106 -0
  51. data/lib/net/ssh/transport/constants.rb +32 -0
  52. data/lib/net/ssh/transport/ctr.rb +95 -0
  53. data/lib/net/ssh/transport/hmac.rb +45 -0
  54. data/lib/net/ssh/transport/hmac/abstract.rb +79 -0
  55. data/lib/net/ssh/transport/hmac/md5.rb +12 -0
  56. data/lib/net/ssh/transport/hmac/md5_96.rb +11 -0
  57. data/lib/net/ssh/transport/hmac/none.rb +15 -0
  58. data/lib/net/ssh/transport/hmac/ripemd160.rb +13 -0
  59. data/lib/net/ssh/transport/hmac/sha1.rb +13 -0
  60. data/lib/net/ssh/transport/hmac/sha1_96.rb +11 -0
  61. data/lib/net/ssh/transport/hmac/sha2_256.rb +15 -0
  62. data/lib/net/ssh/transport/hmac/sha2_256_96.rb +13 -0
  63. data/lib/net/ssh/transport/hmac/sha2_512.rb +14 -0
  64. data/lib/net/ssh/transport/hmac/sha2_512_96.rb +13 -0
  65. data/lib/net/ssh/transport/identity_cipher.rb +55 -0
  66. data/lib/net/ssh/transport/kex.rb +28 -0
  67. data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +44 -0
  68. data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +216 -0
  69. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +80 -0
  70. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +15 -0
  71. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +93 -0
  72. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +13 -0
  73. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +13 -0
  74. data/lib/net/ssh/transport/key_expander.rb +26 -0
  75. data/lib/net/ssh/transport/openssl.rb +237 -0
  76. data/lib/net/ssh/transport/packet_stream.rb +235 -0
  77. data/lib/net/ssh/transport/server_version.rb +71 -0
  78. data/lib/net/ssh/transport/session.rb +278 -0
  79. data/lib/net/ssh/transport/state.rb +206 -0
  80. data/lib/net/ssh/verifiers/lenient.rb +30 -0
  81. data/lib/net/ssh/verifiers/null.rb +12 -0
  82. data/lib/net/ssh/verifiers/strict.rb +53 -0
  83. data/lib/net/ssh/version.rb +62 -0
  84. data/net-ssh.gemspec +164 -0
  85. data/setup.rb +1585 -0
  86. data/support/arcfour_check.rb +20 -0
  87. data/support/ssh_tunnel_bug.rb +65 -0
  88. data/test/authentication/methods/common.rb +28 -0
  89. data/test/authentication/methods/test_abstract.rb +51 -0
  90. data/test/authentication/methods/test_hostbased.rb +114 -0
  91. data/test/authentication/methods/test_keyboard_interactive.rb +100 -0
  92. data/test/authentication/methods/test_password.rb +52 -0
  93. data/test/authentication/methods/test_publickey.rb +148 -0
  94. data/test/authentication/test_agent.rb +205 -0
  95. data/test/authentication/test_key_manager.rb +218 -0
  96. data/test/authentication/test_session.rb +106 -0
  97. data/test/common.rb +107 -0
  98. data/test/configs/eqsign +3 -0
  99. data/test/configs/exact_match +8 -0
  100. data/test/configs/host_plus +10 -0
  101. data/test/configs/multihost +4 -0
  102. data/test/configs/wild_cards +14 -0
  103. data/test/connection/test_channel.rb +467 -0
  104. data/test/connection/test_session.rb +488 -0
  105. data/test/known_hosts/github +1 -0
  106. data/test/test_all.rb +9 -0
  107. data/test/test_buffer.rb +426 -0
  108. data/test/test_buffered_io.rb +63 -0
  109. data/test/test_config.rb +120 -0
  110. data/test/test_key_factory.rb +121 -0
  111. data/test/test_known_hosts.rb +13 -0
  112. data/test/transport/hmac/test_md5.rb +39 -0
  113. data/test/transport/hmac/test_md5_96.rb +25 -0
  114. data/test/transport/hmac/test_none.rb +34 -0
  115. data/test/transport/hmac/test_ripemd160.rb +34 -0
  116. data/test/transport/hmac/test_sha1.rb +34 -0
  117. data/test/transport/hmac/test_sha1_96.rb +25 -0
  118. data/test/transport/hmac/test_sha2_256.rb +35 -0
  119. data/test/transport/hmac/test_sha2_256_96.rb +25 -0
  120. data/test/transport/hmac/test_sha2_512.rb +35 -0
  121. data/test/transport/hmac/test_sha2_512_96.rb +25 -0
  122. data/test/transport/kex/test_diffie_hellman_group14_sha1.rb +13 -0
  123. data/test/transport/kex/test_diffie_hellman_group1_sha1.rb +146 -0
  124. data/test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb +92 -0
  125. data/test/transport/kex/test_diffie_hellman_group_exchange_sha256.rb +33 -0
  126. data/test/transport/kex/test_ecdh_sha2_nistp256.rb +161 -0
  127. data/test/transport/kex/test_ecdh_sha2_nistp384.rb +37 -0
  128. data/test/transport/kex/test_ecdh_sha2_nistp521.rb +37 -0
  129. data/test/transport/test_algorithms.rb +330 -0
  130. data/test/transport/test_cipher_factory.rb +441 -0
  131. data/test/transport/test_hmac.rb +34 -0
  132. data/test/transport/test_identity_cipher.rb +40 -0
  133. data/test/transport/test_packet_stream.rb +1745 -0
  134. data/test/transport/test_server_version.rb +78 -0
  135. data/test/transport/test_session.rb +315 -0
  136. data/test/transport/test_state.rb +179 -0
  137. metadata +208 -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,77 @@
1
+ require 'thread'
2
+
3
+ class String
4
+ if RUBY_VERSION < "1.9"
5
+ def getbyte(index)
6
+ self[index]
7
+ end
8
+ def setbyte(index, c)
9
+ self[index] = c
10
+ end
11
+ end
12
+ if RUBY_VERSION < "1.8.7"
13
+ def bytesize
14
+ self.size
15
+ end
16
+ end
17
+ end
18
+
19
+ module Net; module SSH
20
+
21
+ # This class contains miscellaneous patches and workarounds
22
+ # for different ruby implementations.
23
+ class Compat
24
+
25
+ # A workaround for an IO#select threading bug in certain versions of MRI 1.8.
26
+ # See: http://net-ssh.lighthouseapp.com/projects/36253/tickets/1-ioselect-threading-bug-in-ruby-18
27
+ # The root issue is documented here: http://redmine.ruby-lang.org/issues/show/1993
28
+ if RUBY_VERSION >= '1.9' || RUBY_PLATFORM == 'java'
29
+
30
+ # problem: Pageant sockets aren't real sockets/don't inherit from IO, so we can't call IO.select on them
31
+ # solution: when a Pageant socket is found in the readers/writers array, slice it out then merge it back in before returning (just assume it's always ready)
32
+ # unfortunately this fix will also need to be ported to any other library that reaches in to Net::SSH and select's its sockets (like Capistrano)
33
+ # this is slightly more verbose than necessary, but it's useful for debugging
34
+ def self.io_select(*params)
35
+ read_array = params[0]
36
+ write_array = params[1]
37
+ error_array = params[2]
38
+ timeout = params[3]
39
+
40
+ read_sockets = read_array.nil? ? [] : read_array.reject {|s| s.class.name =~ /Pageant/ }
41
+ read_pageant = read_array.nil? ? [] : read_array.select {|s| s.class.name =~ /Pageant/ }
42
+
43
+ write_sockets = write_array.nil? ? [] : write_array.reject {|s| s.class.name =~ /Pageant/ }
44
+ write_pageant = write_array.nil? ? [] : write_array.select {|s| s.class.name =~ /Pageant/ }
45
+
46
+ result = IO.select(read_sockets, write_sockets, error_array, timeout)
47
+
48
+ if result.nil?
49
+ return nil
50
+ else
51
+ ready_read_sockets = result[0]
52
+ ready_write_sockets = result[1]
53
+ ready_error_array_only_sockets = result[2]
54
+
55
+ return [ready_read_sockets | read_pageant, ready_write_sockets | write_pageant, ready_error_array_only_sockets]
56
+ end
57
+ end
58
+ else
59
+ SELECT_MUTEX = Mutex.new
60
+ def self.io_select(*params)
61
+ # It should be safe to wrap calls in a mutex when the timeout is 0
62
+ # (that is, the call is not supposed to block).
63
+ # We leave blocking calls unprotected to avoid causing deadlocks.
64
+ # This should still catch the main case for Capistrano users.
65
+ if params[3] == 0
66
+ SELECT_MUTEX.synchronize do
67
+ IO.select(*params)
68
+ end
69
+ else
70
+ IO.select(*params)
71
+ end
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ end; end
@@ -0,0 +1,327 @@
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
+
212
+ # TODO: consider, should there be an override of this method which is Pageant specific, even though that would mean mild reimplementation
213
+ # would mean: no shutdown in eof
214
+ # no casing around class name right here
215
+
216
+ # Since Pageant sockets aren't real sockets,
217
+ # the client object doesn't need (and shouln't have) these modules included
218
+ if (client.class.name =~ /Pageant/).nil?
219
+ client.extend(Net::SSH::BufferedIo)
220
+ client.extend(Net::SSH::ForwardedBufferedIo)
221
+ client.logger = logger
222
+ end
223
+
224
+ session.listen_to(client)
225
+ channel[:socket] = client
226
+
227
+ channel.on_data do |ch, data|
228
+ debug { "data: enqueueing #{data.length} bytes on #{type} forwarded channel" }
229
+ ch[:socket].enqueue(data)
230
+ end
231
+
232
+ # Handles server close on the sending side by Miklós Fazekas
233
+ channel.on_eof do |ch|
234
+ debug { "eof #{type} on #{type} forwarded channel" }
235
+ begin
236
+ ch[:socket].send_pending
237
+
238
+ ch[:socket].shutdown Socket::SHUT_WR
239
+ ch[:socket].close
240
+ rescue IOError => e
241
+ if e.message =~ /closed/ then
242
+ debug { "epipe in on_eof => shallowing exception:#{e}" }
243
+ else
244
+ raise
245
+ end
246
+ rescue Errno::EPIPE => e
247
+ debug { "epipe in on_eof => shallowing exception:#{e}" }
248
+ rescue Errno::ENOTCONN => e
249
+ debug { "enotconn in on_eof => shallowing exception:#{e}" }
250
+ end
251
+ end
252
+
253
+ channel.on_close do |ch|
254
+ debug { "closing #{type} forwarded channel" }
255
+ ch[:socket].close if !client.closed?
256
+ session.stop_listening_to(ch[:socket])
257
+ end
258
+
259
+ channel.on_process do |ch|
260
+ ch.debug { "processing #{type} forwarded connection" }
261
+
262
+ if ch[:socket].closed?
263
+ ch.info { "#{type} forwarded connection closed" }
264
+ ch.close
265
+ else
266
+ # For pageant sockets, the recieved and queued data (from on_data above) should be "sent" immediately
267
+ # That way, the read operation below is successful with that result
268
+
269
+ # Alternatively, instead of enqueuing the message in on_data, send_query could be called directly,
270
+ # so the pageant's response data would be available here as well.
271
+ # This requires special consideration when data is split into muliple segments (ex: carriage return split issue)
272
+ # Todo: figure out why the forwarding message starting with 000001920D... seemed to break consistantly after the first four bytes (followed by \r, in the next segment)
273
+ if ch[:socket].available == 0 && ch[:socket].class.name =~ /Pageant/
274
+ ch.debug { "Pageant socket: no data to read, so send pending" }
275
+ ch[:socket].send_pending
276
+ end
277
+
278
+ available = ch[:socket].available
279
+ if available > 0
280
+ ch.debug { "Pageant socket: now there are #{available} bytes" } if ch[:socket].class.name =~ /Pageant/
281
+ data = ch[:socket].read_available(available > 0 ? available : 8192)
282
+ ch.debug { "read #{data.length} bytes from client, sending over #{type} forwarded connection" }
283
+ ch.send_data(data)
284
+ end
285
+ end
286
+ end
287
+ end
288
+
289
+ # The callback used when a new "forwarded-tcpip" channel is requested
290
+ # by the server. This will open a new socket to the host/port specified
291
+ # when the forwarded connection was first requested.
292
+ def forwarded_tcpip(session, channel, packet)
293
+ connected_address = packet.read_string
294
+ connected_port = packet.read_long
295
+ originator_address = packet.read_string
296
+ originator_port = packet.read_long
297
+
298
+ remote = @remote_forwarded_ports[[connected_port, connected_address]]
299
+
300
+ if remote.nil?
301
+ raise Net::SSH::ChannelOpenFailed.new(1, "unknown request from remote forwarded connection on #{connected_address}:#{connected_port}")
302
+ end
303
+
304
+ client = TCPSocket.new(remote.host, remote.port)
305
+ info { "connected #{connected_address}:#{connected_port} originator #{originator_address}:#{originator_port}" }
306
+
307
+ prepare_client(client, channel, :remote)
308
+ rescue SocketError => err
309
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to remote host (#{remote.host}:#{remote.port}): #{err.message}")
310
+ end
311
+
312
+ # The callback used when an auth-agent channel is requested by the server.
313
+ def auth_agent_channel(session, channel, packet)
314
+ info { "opening auth-agent channel" }
315
+ channel[:invisible] = true
316
+
317
+ begin
318
+ agent = Authentication::Agent.connect(logger)
319
+ prepare_client(agent.socket, channel, :agent)
320
+ rescue Exception => e
321
+ error { "attempted to connect to agent but failed: #{e.class.name} (#{e.message})" }
322
+ raise Net::SSH::ChannelOpenFailed.new(2, "could not connect to authentication agent")
323
+ end
324
+ end
325
+ end
326
+
327
+ end; end; end