net-ssh 4.1.0 → 6.1.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.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.gitignore +5 -0
- data/.rubocop.yml +8 -2
- data/.rubocop_todo.yml +405 -552
- data/.travis.yml +23 -22
- data/CHANGES.txt +112 -1
- data/Gemfile +1 -7
- data/{Gemfile.norbnacl → Gemfile.noed25519} +1 -1
- data/Manifest +4 -5
- data/README.md +287 -0
- data/Rakefile +40 -29
- data/appveyor.yml +12 -6
- data/lib/net/ssh.rb +68 -32
- data/lib/net/ssh/authentication/agent.rb +234 -222
- data/lib/net/ssh/authentication/certificate.rb +175 -164
- data/lib/net/ssh/authentication/constants.rb +17 -14
- data/lib/net/ssh/authentication/ed25519.rb +162 -141
- data/lib/net/ssh/authentication/ed25519_loader.rb +32 -29
- data/lib/net/ssh/authentication/key_manager.rb +40 -9
- data/lib/net/ssh/authentication/methods/abstract.rb +53 -47
- data/lib/net/ssh/authentication/methods/hostbased.rb +32 -33
- data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +1 -1
- data/lib/net/ssh/authentication/methods/none.rb +10 -10
- data/lib/net/ssh/authentication/methods/password.rb +13 -13
- data/lib/net/ssh/authentication/methods/publickey.rb +56 -55
- data/lib/net/ssh/authentication/pageant.rb +468 -465
- data/lib/net/ssh/authentication/pub_key_fingerprint.rb +43 -0
- data/lib/net/ssh/authentication/session.rb +130 -122
- data/lib/net/ssh/buffer.rb +345 -312
- data/lib/net/ssh/buffered_io.rb +163 -163
- data/lib/net/ssh/config.rb +316 -238
- data/lib/net/ssh/connection/channel.rb +670 -650
- data/lib/net/ssh/connection/constants.rb +30 -26
- data/lib/net/ssh/connection/event_loop.rb +108 -105
- data/lib/net/ssh/connection/keepalive.rb +54 -50
- data/lib/net/ssh/connection/session.rb +682 -671
- data/lib/net/ssh/connection/term.rb +180 -176
- data/lib/net/ssh/errors.rb +101 -99
- data/lib/net/ssh/key_factory.rb +195 -108
- data/lib/net/ssh/known_hosts.rb +161 -152
- data/lib/net/ssh/loggable.rb +57 -55
- data/lib/net/ssh/packet.rb +82 -78
- data/lib/net/ssh/prompt.rb +55 -53
- data/lib/net/ssh/proxy/command.rb +104 -89
- data/lib/net/ssh/proxy/errors.rb +12 -8
- data/lib/net/ssh/proxy/http.rb +93 -91
- data/lib/net/ssh/proxy/https.rb +42 -39
- data/lib/net/ssh/proxy/jump.rb +50 -47
- data/lib/net/ssh/proxy/socks4.rb +0 -2
- data/lib/net/ssh/proxy/socks5.rb +11 -12
- data/lib/net/ssh/service/forward.rb +370 -317
- data/lib/net/ssh/test.rb +83 -77
- data/lib/net/ssh/test/channel.rb +146 -142
- data/lib/net/ssh/test/extensions.rb +150 -146
- data/lib/net/ssh/test/kex.rb +35 -31
- data/lib/net/ssh/test/local_packet.rb +48 -44
- data/lib/net/ssh/test/packet.rb +87 -84
- data/lib/net/ssh/test/remote_packet.rb +35 -31
- data/lib/net/ssh/test/script.rb +173 -171
- data/lib/net/ssh/test/socket.rb +59 -55
- data/lib/net/ssh/transport/algorithms.rb +430 -364
- data/lib/net/ssh/transport/cipher_factory.rb +95 -91
- data/lib/net/ssh/transport/constants.rb +33 -25
- data/lib/net/ssh/transport/ctr.rb +33 -11
- data/lib/net/ssh/transport/hmac.rb +15 -13
- data/lib/net/ssh/transport/hmac/abstract.rb +82 -63
- data/lib/net/ssh/transport/hmac/sha2_256.rb +7 -11
- data/lib/net/ssh/transport/hmac/sha2_256_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_256_etm.rb +12 -0
- data/lib/net/ssh/transport/hmac/sha2_512.rb +6 -9
- data/lib/net/ssh/transport/hmac/sha2_512_96.rb +4 -8
- data/lib/net/ssh/transport/hmac/sha2_512_etm.rb +12 -0
- data/lib/net/ssh/transport/identity_cipher.rb +55 -51
- data/lib/net/ssh/transport/kex.rb +14 -13
- data/lib/net/ssh/transport/kex/abstract.rb +123 -0
- data/lib/net/ssh/transport/kex/abstract5656.rb +72 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256.rb +38 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +30 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +33 -40
- data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +112 -217
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +53 -62
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +5 -9
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +36 -90
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +18 -10
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +18 -10
- data/lib/net/ssh/transport/key_expander.rb +29 -25
- data/lib/net/ssh/transport/openssl.rb +116 -116
- data/lib/net/ssh/transport/packet_stream.rb +223 -190
- data/lib/net/ssh/transport/server_version.rb +64 -66
- data/lib/net/ssh/transport/session.rb +306 -257
- data/lib/net/ssh/transport/state.rb +198 -196
- data/lib/net/ssh/verifiers/accept_new.rb +35 -0
- data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +34 -0
- data/lib/net/ssh/verifiers/always.rb +56 -0
- data/lib/net/ssh/verifiers/never.rb +21 -0
- data/lib/net/ssh/version.rb +55 -53
- data/net-ssh-public_cert.pem +18 -19
- data/net-ssh.gemspec +12 -11
- data/support/ssh_tunnel_bug.rb +2 -2
- metadata +86 -75
- metadata.gz.sig +0 -0
- data/Gemfile.norbnacl.lock +0 -41
- data/README.rdoc +0 -169
- data/lib/net/ssh/ruby_compat.rb +0 -24
- data/lib/net/ssh/verifiers/lenient.rb +0 -30
- data/lib/net/ssh/verifiers/null.rb +0 -12
- data/lib/net/ssh/verifiers/secure.rb +0 -52
- data/lib/net/ssh/verifiers/strict.rb +0 -24
- data/support/arcfour_check.rb +0 -20
data/lib/net/ssh/buffered_io.rb
CHANGED
@@ -1,144 +1,145 @@
|
|
1
1
|
require 'net/ssh/buffer'
|
2
2
|
require 'net/ssh/loggable'
|
3
|
-
require 'net/ssh/ruby_compat'
|
4
3
|
|
5
|
-
module Net
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
# Tries to read up to +n+ bytes of data from the remote end, and appends
|
61
|
-
# the data to the input buffer. It returns the number of bytes read, or 0
|
62
|
-
# if no data was available to be read.
|
63
|
-
def fill(n=8192)
|
64
|
-
input.consume!
|
65
|
-
data = recv(n)
|
66
|
-
debug { "read #{data.length} bytes" }
|
67
|
-
input.append(data)
|
68
|
-
return data.length
|
69
|
-
rescue EOFError => e
|
70
|
-
@input_errors << e
|
71
|
-
return 0
|
72
|
-
end
|
73
|
-
|
74
|
-
# Read up to +length+ bytes from the input buffer. If +length+ is nil,
|
75
|
-
# all available data is read from the buffer. (See #available.)
|
76
|
-
def read_available(length=nil)
|
77
|
-
input.read(length || available)
|
78
|
-
end
|
79
|
-
|
80
|
-
# Returns the number of bytes available to be read from the input buffer.
|
81
|
-
# (See #read_available.)
|
82
|
-
def available
|
83
|
-
input.available
|
84
|
-
end
|
85
|
-
|
86
|
-
# Enqueues data in the output buffer, to be written when #send_pending
|
87
|
-
# is called. Note that the data is _not_ sent immediately by this method!
|
88
|
-
def enqueue(data)
|
89
|
-
output.append(data)
|
90
|
-
end
|
91
|
-
|
92
|
-
# Returns +true+ if there is data waiting in the output buffer, and
|
93
|
-
# +false+ otherwise.
|
94
|
-
def pending_write?
|
95
|
-
output.length > 0
|
96
|
-
end
|
97
|
-
|
98
|
-
# Sends as much of the pending output as possible. Returns +true+ if any
|
99
|
-
# data was sent, and +false+ otherwise.
|
100
|
-
def send_pending
|
101
|
-
if output.length > 0
|
102
|
-
sent = send(output.to_s, 0)
|
103
|
-
debug { "sent #{sent} bytes" }
|
104
|
-
output.consume!(sent)
|
105
|
-
return sent > 0
|
106
|
-
else
|
107
|
-
return false
|
4
|
+
module Net
|
5
|
+
module SSH
|
6
|
+
|
7
|
+
# This module is used to extend sockets and other IO objects, to allow
|
8
|
+
# them to be buffered for both read and write. This abstraction makes it
|
9
|
+
# quite easy to write a select-based event loop
|
10
|
+
# (see Net::SSH::Connection::Session#listen_to).
|
11
|
+
#
|
12
|
+
# The general idea is that instead of calling #read directly on an IO that
|
13
|
+
# has been extended with this module, you call #fill (to add pending input
|
14
|
+
# to the internal read buffer), and then #read_available (to read from that
|
15
|
+
# buffer). Likewise, you don't call #write directly, you call #enqueue to
|
16
|
+
# add data to the write buffer, and then #send_pending or #wait_for_pending_sends
|
17
|
+
# to actually send the data across the wire.
|
18
|
+
#
|
19
|
+
# In this way you can easily use the object as an argument to IO.select,
|
20
|
+
# calling #fill when it is available for read, or #send_pending when it is
|
21
|
+
# available for write, and then call #enqueue and #read_available during
|
22
|
+
# the idle times.
|
23
|
+
#
|
24
|
+
# socket = TCPSocket.new(address, port)
|
25
|
+
# socket.extend(Net::SSH::BufferedIo)
|
26
|
+
#
|
27
|
+
# ssh.listen_to(socket)
|
28
|
+
#
|
29
|
+
# ssh.loop do
|
30
|
+
# if socket.available > 0
|
31
|
+
# puts socket.read_available
|
32
|
+
# socket.enqueue("response\n")
|
33
|
+
# end
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# Note that this module must be used to extend an instance, and should not
|
37
|
+
# be included in a class. If you do want to use it via an include, then you
|
38
|
+
# must make sure to invoke the private #initialize_buffered_io method in
|
39
|
+
# your class' #initialize method:
|
40
|
+
#
|
41
|
+
# class Foo < IO
|
42
|
+
# include Net::SSH::BufferedIo
|
43
|
+
#
|
44
|
+
# def initialize
|
45
|
+
# initialize_buffered_io
|
46
|
+
# # ...
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
module BufferedIo
|
50
|
+
include Loggable
|
51
|
+
|
52
|
+
# Called when the #extend is called on an object, with this module as the
|
53
|
+
# argument. It ensures that the modules instance variables are all properly
|
54
|
+
# initialized.
|
55
|
+
def self.extended(object) #:nodoc:
|
56
|
+
# need to use __send__ because #send is overridden in Socket
|
57
|
+
object.__send__(:initialize_buffered_io)
|
108
58
|
end
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
59
|
+
|
60
|
+
# Tries to read up to +n+ bytes of data from the remote end, and appends
|
61
|
+
# the data to the input buffer. It returns the number of bytes read, or 0
|
62
|
+
# if no data was available to be read.
|
63
|
+
def fill(n=8192)
|
64
|
+
input.consume!
|
65
|
+
data = recv(n)
|
66
|
+
debug { "read #{data.length} bytes" }
|
67
|
+
input.append(data)
|
68
|
+
return data.length
|
69
|
+
rescue EOFError => e
|
70
|
+
@input_errors << e
|
71
|
+
return 0
|
72
|
+
end
|
73
|
+
|
74
|
+
# Read up to +length+ bytes from the input buffer. If +length+ is nil,
|
75
|
+
# all available data is read from the buffer. (See #available.)
|
76
|
+
def read_available(length=nil)
|
77
|
+
input.read(length || available)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns the number of bytes available to be read from the input buffer.
|
81
|
+
# (See #read_available.)
|
82
|
+
def available
|
83
|
+
input.available
|
84
|
+
end
|
85
|
+
|
86
|
+
# Enqueues data in the output buffer, to be written when #send_pending
|
87
|
+
# is called. Note that the data is _not_ sent immediately by this method!
|
88
|
+
def enqueue(data)
|
89
|
+
output.append(data)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Returns +true+ if there is data waiting in the output buffer, and
|
93
|
+
# +false+ otherwise.
|
94
|
+
def pending_write?
|
95
|
+
output.length > 0
|
96
|
+
end
|
97
|
+
|
98
|
+
# Sends as much of the pending output as possible. Returns +true+ if any
|
99
|
+
# data was sent, and +false+ otherwise.
|
100
|
+
def send_pending
|
101
|
+
if output.length > 0
|
102
|
+
sent = send(output.to_s, 0)
|
103
|
+
debug { "sent #{sent} bytes" }
|
104
|
+
output.consume!(sent)
|
105
|
+
return sent > 0
|
106
|
+
else
|
107
|
+
return false
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Calls #send_pending repeatedly, if necessary, blocking until the output
|
112
|
+
# buffer is empty.
|
113
|
+
def wait_for_pending_sends
|
118
114
|
send_pending
|
115
|
+
while output.length > 0
|
116
|
+
result = IO.select(nil, [self]) or next
|
117
|
+
next unless result[1].any?
|
118
|
+
send_pending
|
119
|
+
end
|
119
120
|
end
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
121
|
+
|
122
|
+
public # these methods are primarily for use in tests
|
123
|
+
|
124
124
|
def write_buffer #:nodoc:
|
125
125
|
output.to_s
|
126
126
|
end
|
127
|
-
|
127
|
+
|
128
128
|
def read_buffer #:nodoc:
|
129
129
|
input.to_s
|
130
130
|
end
|
131
|
-
|
132
|
-
|
133
|
-
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
134
|
#--
|
135
135
|
# Can't use attr_reader here (after +private+) without incurring the
|
136
136
|
# wrath of "ruby -w". We hates it.
|
137
137
|
#++
|
138
|
-
|
138
|
+
|
139
139
|
def input; @input; end
|
140
|
-
def output; @output; end
|
141
140
|
|
141
|
+
def output; @output; end
|
142
|
+
|
142
143
|
# Initializes the intput and output buffers for this object. This method
|
143
144
|
# is called automatically when the module is mixed into an object via
|
144
145
|
# Object#extend (see Net::SSH::BufferedIo.extended), but must be called
|
@@ -150,54 +151,53 @@ module Net; module SSH
|
|
150
151
|
@output = Net::SSH::Buffer.new
|
151
152
|
@output_errors = []
|
152
153
|
end
|
153
|
-
|
154
|
-
|
155
|
-
|
154
|
+
end
|
156
155
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
debug { "connection was reset => shallowing exception:#{e}" }
|
175
|
-
return 0
|
176
|
-
rescue IOError => e
|
177
|
-
if e.message =~ /closed/ then
|
156
|
+
# Fixes for two issues by Miklós Fazekas:
|
157
|
+
#
|
158
|
+
# * if client closes a forwarded connection, but the server is
|
159
|
+
# reading, net-ssh terminates with IOError socket closed.
|
160
|
+
# * if client force closes (RST) a forwarded connection, but
|
161
|
+
# server is reading, net-ssh terminates with [an exception]
|
162
|
+
#
|
163
|
+
# See:
|
164
|
+
#
|
165
|
+
# http://net-ssh.lighthouseapp.com/projects/36253/tickets/7
|
166
|
+
# http://github.com/net-ssh/net-ssh/tree/portfwfix
|
167
|
+
#
|
168
|
+
module ForwardedBufferedIo
|
169
|
+
def fill(n=8192)
|
170
|
+
begin
|
171
|
+
super(n)
|
172
|
+
rescue Errno::ECONNRESET => e
|
178
173
|
debug { "connection was reset => shallowing exception:#{e}" }
|
179
174
|
return 0
|
180
|
-
|
181
|
-
|
175
|
+
rescue IOError => e
|
176
|
+
if e.message =~ /closed/ then
|
177
|
+
debug { "connection was reset => shallowing exception:#{e}" }
|
178
|
+
return 0
|
179
|
+
else
|
180
|
+
raise
|
181
|
+
end
|
182
182
|
end
|
183
183
|
end
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
rescue Errno::ECONNRESET => e
|
190
|
-
debug { "connection was reset => shallowing exception:#{e}" }
|
191
|
-
return 0
|
192
|
-
rescue IOError => e
|
193
|
-
if e.message =~ /closed/ then
|
184
|
+
|
185
|
+
def send_pending
|
186
|
+
begin
|
187
|
+
super
|
188
|
+
rescue Errno::ECONNRESET => e
|
194
189
|
debug { "connection was reset => shallowing exception:#{e}" }
|
195
190
|
return 0
|
196
|
-
|
197
|
-
|
191
|
+
rescue IOError => e
|
192
|
+
if e.message =~ /closed/ then
|
193
|
+
debug { "connection was reset => shallowing exception:#{e}" }
|
194
|
+
return 0
|
195
|
+
else
|
196
|
+
raise
|
197
|
+
end
|
198
198
|
end
|
199
199
|
end
|
200
200
|
end
|
201
|
-
end
|
202
201
|
|
203
|
-
end
|
202
|
+
end
|
203
|
+
end
|
data/lib/net/ssh/config.rb
CHANGED
@@ -1,266 +1,300 @@
|
|
1
|
-
module Net
|
1
|
+
module Net
|
2
|
+
module SSH
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
4
|
+
# The Net::SSH::Config class is used to parse OpenSSH configuration files,
|
5
|
+
# and translates that syntax into the configuration syntax that Net::SSH
|
6
|
+
# understands. This lets Net::SSH scripts read their configuration (to
|
7
|
+
# some extent) from OpenSSH configuration files (~/.ssh/config, /etc/ssh_config,
|
8
|
+
# and so forth).
|
9
|
+
#
|
10
|
+
# Only a subset of OpenSSH configuration options are understood:
|
11
|
+
#
|
12
|
+
# * ChallengeResponseAuthentication => maps to the :auth_methods option challenge-response (then coleasced into keyboard-interactive)
|
13
|
+
# * KbdInteractiveAuthentication => maps to the :auth_methods keyboard-interactive
|
14
|
+
# * CertificateFile => maps to the :keycerts option
|
15
|
+
# * Ciphers => maps to the :encryption option
|
16
|
+
# * Compression => :compression
|
17
|
+
# * CompressionLevel => :compression_level
|
18
|
+
# * ConnectTimeout => maps to the :timeout option
|
19
|
+
# * ForwardAgent => :forward_agent
|
20
|
+
# * GlobalKnownHostsFile => :global_known_hosts_file
|
21
|
+
# * HostBasedAuthentication => maps to the :auth_methods option
|
22
|
+
# * HostKeyAlgorithms => maps to :host_key option
|
23
|
+
# * HostKeyAlias => :host_key_alias
|
24
|
+
# * HostName => :host_name
|
25
|
+
# * IdentityFile => maps to the :keys option
|
26
|
+
# * IdentityAgent => :identity_agent
|
27
|
+
# * IdentitiesOnly => :keys_only
|
28
|
+
# * CheckHostIP => :check_host_ip
|
29
|
+
# * Macs => maps to the :hmac option
|
30
|
+
# * PasswordAuthentication => maps to the :auth_methods option password
|
31
|
+
# * Port => :port
|
32
|
+
# * PreferredAuthentications => maps to the :auth_methods option
|
33
|
+
# * ProxyCommand => maps to the :proxy option
|
34
|
+
# * ProxyJump => maps to the :proxy option
|
35
|
+
# * PubKeyAuthentication => maps to the :auth_methods option
|
36
|
+
# * RekeyLimit => :rekey_limit
|
37
|
+
# * StrictHostKeyChecking => :strict_host_key_checking
|
38
|
+
# * User => :user
|
39
|
+
# * UserKnownHostsFile => :user_known_hosts_file
|
40
|
+
# * NumberOfPasswordPrompts => :number_of_password_prompts
|
41
|
+
# * FingerprintHash => :fingerprint_hash
|
42
|
+
#
|
43
|
+
# Note that you will never need to use this class directly--you can control
|
44
|
+
# whether the OpenSSH configuration files are read by passing the :config
|
45
|
+
# option to Net::SSH.start. (They are, by default.)
|
46
|
+
class Config
|
47
|
+
class << self
|
48
|
+
@@default_files = %w[~/.ssh/config /etc/ssh_config /etc/ssh/ssh_config]
|
49
|
+
# The following defaults follow the openssh client ssh_config defaults.
|
50
|
+
# http://lwn.net/Articles/544640/
|
51
|
+
# "hostbased" is off and "none" is not supported but we allow it since
|
52
|
+
# it's used by some clients to query the server for allowed auth methods
|
53
|
+
@@default_auth_methods = %w[none publickey password keyboard-interactive]
|
48
54
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
55
|
+
# Returns an array of locations of OpenSSH configuration files
|
56
|
+
# to parse by default.
|
57
|
+
def default_files
|
58
|
+
@@default_files.clone
|
59
|
+
end
|
54
60
|
|
55
|
-
|
56
|
-
|
57
|
-
|
61
|
+
def default_auth_methods
|
62
|
+
@@default_auth_methods.clone
|
63
|
+
end
|
58
64
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
65
|
+
# Loads the configuration data for the given +host+ from all of the
|
66
|
+
# given +files+ (defaulting to the list of files returned by
|
67
|
+
# #default_files), translates the resulting hash into the options
|
68
|
+
# recognized by Net::SSH, and returns them.
|
69
|
+
def for(host, files=expandable_default_files)
|
70
|
+
translate(files.inject({}) { |settings, file|
|
71
|
+
load(file, host, settings)
|
72
|
+
})
|
73
|
+
end
|
68
74
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
75
|
+
# Load the OpenSSH configuration settings in the given +file+ for the
|
76
|
+
# given +host+. If +settings+ is given, the options are merged into
|
77
|
+
# that hash, with existing values taking precedence over newly parsed
|
78
|
+
# ones. Returns a hash containing the OpenSSH options. (See
|
79
|
+
# #translate for how to convert the OpenSSH options into Net::SSH
|
80
|
+
# options.)
|
81
|
+
def load(path, host, settings={}, base_dir = nil)
|
82
|
+
file = File.expand_path(path)
|
83
|
+
base_dir ||= File.dirname(file)
|
84
|
+
return settings unless File.readable?(file)
|
79
85
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
86
|
+
globals = {}
|
87
|
+
block_matched = false
|
88
|
+
block_seen = false
|
89
|
+
IO.foreach(file) do |line|
|
90
|
+
next if line =~ /^\s*(?:#.*)?$/
|
85
91
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
92
|
+
if line =~ /^\s*(\S+)\s*=(.*)$/
|
93
|
+
key, value = $1, $2
|
94
|
+
else
|
95
|
+
key, value = line.strip.split(/\s+/, 2)
|
96
|
+
end
|
91
97
|
|
92
|
-
|
93
|
-
|
98
|
+
# silently ignore malformed entries
|
99
|
+
next if value.nil?
|
94
100
|
|
95
|
-
|
96
|
-
|
101
|
+
key.downcase!
|
102
|
+
value = unquote(value)
|
97
103
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
+
value = case value.strip
|
105
|
+
when /^\d+$/ then value.to_i
|
106
|
+
when /^no$/i then false
|
107
|
+
when /^yes$/i then true
|
108
|
+
else value
|
109
|
+
end
|
104
110
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
111
|
+
if key == 'host'
|
112
|
+
# Support "Host host1 host2 hostN".
|
113
|
+
# See http://github.com/net-ssh/net-ssh/issues#issue/6
|
114
|
+
negative_hosts, positive_hosts = value.to_s.split(/\s+/).partition { |h| h.start_with?('!') }
|
109
115
|
|
110
|
-
|
111
|
-
|
112
|
-
|
116
|
+
# Check for negative patterns first. If the host matches, that overrules any other positive match.
|
117
|
+
# The host substring code is used to strip out the starting "!" so the regexp will be correct.
|
118
|
+
negative_matched = negative_hosts.any? { |h| host =~ pattern2regex(h[1..-1]) }
|
113
119
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
120
|
+
if negative_matched
|
121
|
+
block_matched = false
|
122
|
+
else
|
123
|
+
block_matched = positive_hosts.any? { |h| host =~ pattern2regex(h) }
|
124
|
+
end
|
125
|
+
|
126
|
+
block_seen = true
|
127
|
+
settings[key] = host
|
128
|
+
elsif key == 'match'
|
129
|
+
block_matched = eval_match_conditions(value, host, settings)
|
130
|
+
block_seen = true
|
131
|
+
elsif !block_seen
|
132
|
+
case key
|
133
|
+
when 'identityfile', 'certificatefile'
|
134
|
+
(globals[key] ||= []) << value
|
135
|
+
when 'include'
|
136
|
+
included_file_paths(base_dir, value).each do |file_path|
|
137
|
+
globals = load(file_path, host, globals, base_dir)
|
138
|
+
end
|
139
|
+
else
|
140
|
+
globals[key] = value unless settings.key?(key)
|
141
|
+
end
|
142
|
+
elsif block_matched
|
143
|
+
case key
|
144
|
+
when 'identityfile', 'certificatefile'
|
145
|
+
(settings[key] ||= []) << value
|
146
|
+
when 'include'
|
147
|
+
included_file_paths(base_dir, value).each do |file_path|
|
148
|
+
settings = load(file_path, host, settings, base_dir)
|
149
|
+
end
|
150
|
+
else
|
151
|
+
settings[key] = value unless settings.key?(key)
|
152
|
+
end
|
118
153
|
end
|
119
154
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
when 'identityfile'
|
125
|
-
(globals[key] ||= []) << value
|
126
|
-
when 'include'
|
127
|
-
included_file_paths(base_dir, value).each do |file_path|
|
128
|
-
globals = load(file_path, host, globals)
|
155
|
+
# ProxyCommand and ProxyJump override each other so they need to be tracked togeather
|
156
|
+
%w[proxyjump proxycommand].each do |proxy_key|
|
157
|
+
if (proxy_value = settings.delete(proxy_key))
|
158
|
+
settings['proxy'] ||= [proxy_key, proxy_value]
|
129
159
|
end
|
130
|
-
else
|
131
|
-
globals[key] = value unless settings.key?(key)
|
132
160
|
end
|
133
|
-
|
161
|
+
end
|
162
|
+
|
163
|
+
globals.merge(settings) do |key, oldval, newval|
|
134
164
|
case key
|
135
|
-
when 'identityfile'
|
136
|
-
|
137
|
-
when 'include'
|
138
|
-
included_file_paths(base_dir, value).each do |file_path|
|
139
|
-
settings = load(file_path, host, settings)
|
140
|
-
end
|
165
|
+
when 'identityfile', 'certificatefile'
|
166
|
+
oldval + newval
|
141
167
|
else
|
142
|
-
|
168
|
+
newval
|
143
169
|
end
|
144
170
|
end
|
145
171
|
end
|
146
172
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
(auth_methods << 'challenge-response').uniq!
|
159
|
-
ret = settings.inject({auth_methods: auth_methods}) do |hash, (key, value)|
|
160
|
-
translate_config_key(hash, key.to_sym, value, settings)
|
161
|
-
hash
|
173
|
+
# Given a hash of OpenSSH configuration options, converts them into
|
174
|
+
# a hash of Net::SSH options. Unrecognized options are ignored. The
|
175
|
+
# +settings+ hash must have Strings for keys, all downcased, and
|
176
|
+
# the returned hash will have Symbols for keys.
|
177
|
+
def translate(settings)
|
178
|
+
auth_methods = default_auth_methods.clone
|
179
|
+
(auth_methods << 'challenge-response').uniq!
|
180
|
+
ret = settings.each_with_object({ auth_methods: auth_methods }) do |(key, value), hash|
|
181
|
+
translate_config_key(hash, key.to_sym, value, settings)
|
182
|
+
end
|
183
|
+
merge_challenge_response_with_keyboard_interactive(ret)
|
162
184
|
end
|
163
|
-
merge_challenge_response_with_keyboard_interactive(ret)
|
164
|
-
end
|
165
185
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
186
|
+
# Filters default_files down to the files that are expandable.
|
187
|
+
def expandable_default_files
|
188
|
+
default_files.keep_if do |path|
|
189
|
+
begin
|
190
|
+
File.expand_path(path)
|
191
|
+
true
|
192
|
+
rescue ArgumentError
|
193
|
+
false
|
194
|
+
end
|
174
195
|
end
|
175
196
|
end
|
176
|
-
end
|
177
197
|
|
178
|
-
|
198
|
+
private
|
179
199
|
|
200
|
+
TRANSLATE_CONFIG_KEY_RENAME_MAP = {
|
201
|
+
bindaddress: :bind_address,
|
202
|
+
compression: :compression,
|
203
|
+
compressionlevel: :compression_level,
|
204
|
+
certificatefile: :keycerts,
|
205
|
+
connecttimeout: :timeout,
|
206
|
+
forwardagent: :forward_agent,
|
207
|
+
identitiesonly: :keys_only,
|
208
|
+
identityagent: :identity_agent,
|
209
|
+
globalknownhostsfile: :global_known_hosts_file,
|
210
|
+
hostkeyalias: :host_key_alias,
|
211
|
+
identityfile: :keys,
|
212
|
+
fingerprinthash: :fingerprint_hash,
|
213
|
+
port: :port,
|
214
|
+
stricthostkeychecking: :strict_host_key_checking,
|
215
|
+
user: :user,
|
216
|
+
userknownhostsfile: :user_known_hosts_file,
|
217
|
+
checkhostip: :check_host_ip
|
218
|
+
}.freeze
|
180
219
|
def translate_config_key(hash, key, value, settings)
|
181
|
-
rename = {
|
182
|
-
bindaddress: :bind_address,
|
183
|
-
compression: :compression,
|
184
|
-
compressionlevel: :compression_level,
|
185
|
-
connecttimeout: :timeout,
|
186
|
-
forwardagent: :forward_agent,
|
187
|
-
identitiesonly: :keys_only,
|
188
|
-
globalknownhostsfile: :global_known_hosts_file,
|
189
|
-
hostkeyalias: :host_key_alias,
|
190
|
-
identityfile: :keys,
|
191
|
-
port: :port,
|
192
|
-
user: :user,
|
193
|
-
userknownhostsfile: :user_known_hosts_file
|
194
|
-
}
|
195
220
|
case key
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
221
|
+
when :ciphers
|
222
|
+
hash[:encryption] = value.split(/,/)
|
223
|
+
when :hostbasedauthentication
|
224
|
+
if value
|
225
|
+
(hash[:auth_methods] << "hostbased").uniq!
|
226
|
+
else
|
227
|
+
hash[:auth_methods].delete("hostbased")
|
228
|
+
end
|
229
|
+
when :hostkeyalgorithms
|
230
|
+
hash[:host_key] = value.split(/,/)
|
231
|
+
when :hostname
|
232
|
+
hash[:host_name] = value.gsub(/%h/, settings['host'])
|
233
|
+
when :macs
|
234
|
+
hash[:hmac] = value.split(/,/)
|
235
|
+
when :serveralivecountmax
|
236
|
+
hash[:keepalive_maxcount] = value.to_i if value
|
237
|
+
when :serveraliveinterval
|
238
|
+
if value && value.to_i > 0
|
239
|
+
hash[:keepalive] = true
|
240
|
+
hash[:keepalive_interval] = value.to_i
|
241
|
+
else
|
242
|
+
hash[:keepalive] = false
|
243
|
+
end
|
244
|
+
when :passwordauthentication
|
245
|
+
if value
|
246
|
+
(hash[:auth_methods] << 'password').uniq!
|
247
|
+
else
|
248
|
+
hash[:auth_methods].delete('password')
|
249
|
+
end
|
250
|
+
when :challengeresponseauthentication
|
251
|
+
if value
|
252
|
+
(hash[:auth_methods] << 'challenge-response').uniq!
|
253
|
+
else
|
254
|
+
hash[:auth_methods].delete('challenge-response')
|
255
|
+
end
|
256
|
+
when :kbdinteractiveauthentication
|
257
|
+
if value
|
258
|
+
(hash[:auth_methods] << 'keyboard-interactive').uniq!
|
259
|
+
else
|
260
|
+
hash[:auth_methods].delete('keyboard-interactive')
|
261
|
+
end
|
262
|
+
when :preferredauthentications
|
263
|
+
hash[:auth_methods] = value.split(/,/) # TODO we should place to preferred_auth_methods rather than auth_methods
|
264
|
+
when :proxy
|
265
|
+
if (proxy = setup_proxy(*value))
|
266
|
+
hash[:proxy] = proxy
|
267
|
+
end
|
268
|
+
when :pubkeyauthentication
|
269
|
+
if value
|
270
|
+
(hash[:auth_methods] << 'publickey').uniq!
|
271
|
+
else
|
272
|
+
hash[:auth_methods].delete('publickey')
|
273
|
+
end
|
274
|
+
when :rekeylimit
|
275
|
+
hash[:rekey_limit] = interpret_size(value)
|
276
|
+
when :sendenv
|
277
|
+
multi_send_env = value.to_s.split(/\s+/)
|
278
|
+
hash[:send_env] = multi_send_env.map { |e| Regexp.new pattern2regex(e).source, false }
|
279
|
+
when :setenv
|
280
|
+
hash[:set_env] = Shellwords.split(value.to_s).map { |e| e.split '=', 2 }.to_h
|
281
|
+
when :numberofpasswordprompts
|
282
|
+
hash[:number_of_password_prompts] = value.to_i
|
283
|
+
when *TRANSLATE_CONFIG_KEY_RENAME_MAP.keys
|
284
|
+
hash[TRANSLATE_CONFIG_KEY_RENAME_MAP[key]] = value
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
def setup_proxy(type, value)
|
289
|
+
case type
|
290
|
+
when 'proxycommand'
|
291
|
+
if value !~ /^none$/
|
292
|
+
require 'net/ssh/proxy/command'
|
293
|
+
Net::SSH::Proxy::Command.new(value)
|
294
|
+
end
|
295
|
+
when 'proxyjump'
|
296
|
+
require 'net/ssh/proxy/jump'
|
297
|
+
Net::SSH::Proxy::Jump.new(value)
|
264
298
|
end
|
265
299
|
end
|
266
300
|
|
@@ -303,13 +337,57 @@ module Net; module SSH
|
|
303
337
|
hash
|
304
338
|
end
|
305
339
|
|
306
|
-
def included_file_paths(base_dir,
|
307
|
-
|
308
|
-
|
309
|
-
|
340
|
+
def included_file_paths(base_dir, config_paths)
|
341
|
+
tokenize_config_value(config_paths).flat_map do |path|
|
342
|
+
Dir.glob(File.expand_path(path, base_dir)).select { |f| File.file?(f) }
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
# Tokenize string into tokens.
|
347
|
+
# A token is a word or a quoted sequence of words, separated by whitespaces.
|
348
|
+
def tokenize_config_value(str)
|
349
|
+
str.scan(/([^"\s]+)?(?:"([^"]+)")?\s*/).map(&:join)
|
310
350
|
end
|
311
351
|
|
352
|
+
def eval_match_conditions(condition, host, settings)
|
353
|
+
# Not using `\s` for whitespace matching as canonical
|
354
|
+
# ssh_config parser implementation (OpenSSH) has specific character set.
|
355
|
+
# Ref: https://github.com/openssh/openssh-portable/blob/2581333d564d8697837729b3d07d45738eaf5a54/misc.c#L237-L239
|
356
|
+
conditions = condition.split(/[ \t\r\n]+|(?<!=)=(?!=)/).reject(&:empty?)
|
357
|
+
return true if conditions == ["all"]
|
358
|
+
|
359
|
+
conditions = conditions.each_slice(2)
|
360
|
+
condition_matches = []
|
361
|
+
conditions.each do |(kind,exprs)|
|
362
|
+
exprs = unquote(exprs)
|
363
|
+
|
364
|
+
case kind.downcase
|
365
|
+
when "all"
|
366
|
+
raise "all cannot be mixed with other conditions"
|
367
|
+
when "host"
|
368
|
+
if exprs.start_with?('!')
|
369
|
+
negated = true
|
370
|
+
exprs = exprs[1..-1]
|
371
|
+
else
|
372
|
+
negated = false
|
373
|
+
end
|
374
|
+
condition_met = false
|
375
|
+
exprs.split(",").each do |expr|
|
376
|
+
condition_met = condition_met || host =~ pattern2regex(expr)
|
377
|
+
end
|
378
|
+
condition_matches << (true && negated ^ condition_met)
|
379
|
+
# else
|
380
|
+
# warn "net-ssh: Unsupported expr in Match block: #{kind}"
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
!condition_matches.empty? && condition_matches.all?
|
385
|
+
end
|
386
|
+
|
387
|
+
def unquote(string)
|
388
|
+
string =~ /^"(.*)"$/ ? Regexp.last_match(1) : string
|
389
|
+
end
|
390
|
+
end
|
312
391
|
end
|
313
392
|
end
|
314
|
-
|
315
|
-
end; end
|
393
|
+
end
|