net-ssh 4.2.0 → 7.0.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.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.dockerignore +6 -0
- data/.github/config/rubocop_linter_action.yml +4 -0
- data/.github/workflows/ci-with-docker.yml +44 -0
- data/.github/workflows/ci.yml +87 -0
- data/.github/workflows/rubocop.yml +13 -0
- data/.gitignore +7 -0
- data/.rubocop.yml +19 -2
- data/.rubocop_todo.yml +619 -667
- data/CHANGES.txt +110 -1
- data/Dockerfile +27 -0
- data/Dockerfile.openssl3 +17 -0
- data/Gemfile +3 -7
- data/{Gemfile.norbnacl → Gemfile.noed25519} +3 -1
- data/Manifest +4 -5
- data/README.md +293 -0
- data/Rakefile +45 -29
- data/appveyor.yml +8 -6
- data/docker-compose.yml +23 -0
- data/lib/net/ssh/authentication/agent.rb +248 -223
- data/lib/net/ssh/authentication/certificate.rb +178 -164
- data/lib/net/ssh/authentication/constants.rb +17 -15
- data/lib/net/ssh/authentication/ed25519.rb +141 -116
- data/lib/net/ssh/authentication/ed25519_loader.rb +28 -28
- data/lib/net/ssh/authentication/key_manager.rb +79 -36
- data/lib/net/ssh/authentication/methods/abstract.rb +62 -47
- data/lib/net/ssh/authentication/methods/hostbased.rb +34 -37
- data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +3 -3
- data/lib/net/ssh/authentication/methods/none.rb +16 -19
- data/lib/net/ssh/authentication/methods/password.rb +15 -16
- data/lib/net/ssh/authentication/methods/publickey.rb +96 -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 +131 -122
- data/lib/net/ssh/buffer.rb +385 -332
- data/lib/net/ssh/buffered_io.rb +150 -151
- data/lib/net/ssh/config.rb +316 -239
- data/lib/net/ssh/connection/channel.rb +635 -613
- data/lib/net/ssh/connection/constants.rb +29 -29
- data/lib/net/ssh/connection/event_loop.rb +104 -95
- data/lib/net/ssh/connection/keepalive.rb +55 -51
- data/lib/net/ssh/connection/session.rb +614 -611
- data/lib/net/ssh/connection/term.rb +125 -123
- data/lib/net/ssh/errors.rb +101 -99
- data/lib/net/ssh/key_factory.rb +194 -108
- data/lib/net/ssh/known_hosts.rb +212 -134
- data/lib/net/ssh/loggable.rb +50 -49
- data/lib/net/ssh/packet.rb +83 -79
- data/lib/net/ssh/prompt.rb +51 -51
- data/lib/net/ssh/proxy/command.rb +105 -91
- data/lib/net/ssh/proxy/errors.rb +12 -10
- data/lib/net/ssh/proxy/http.rb +81 -81
- data/lib/net/ssh/proxy/https.rb +37 -36
- data/lib/net/ssh/proxy/jump.rb +49 -48
- data/lib/net/ssh/proxy/socks4.rb +2 -6
- data/lib/net/ssh/proxy/socks5.rb +14 -17
- data/lib/net/ssh/service/forward.rb +365 -362
- data/lib/net/ssh/test/channel.rb +145 -143
- data/lib/net/ssh/test/extensions.rb +131 -127
- data/lib/net/ssh/test/kex.rb +34 -32
- data/lib/net/ssh/test/local_packet.rb +46 -44
- data/lib/net/ssh/test/packet.rb +87 -84
- data/lib/net/ssh/test/remote_packet.rb +32 -30
- data/lib/net/ssh/test/script.rb +155 -155
- data/lib/net/ssh/test/socket.rb +49 -48
- data/lib/net/ssh/test.rb +82 -80
- data/lib/net/ssh/transport/algorithms.rb +433 -364
- data/lib/net/ssh/transport/cipher_factory.rb +95 -91
- data/lib/net/ssh/transport/constants.rb +32 -24
- data/lib/net/ssh/transport/ctr.rb +37 -15
- data/lib/net/ssh/transport/hmac/abstract.rb +81 -63
- data/lib/net/ssh/transport/hmac/md5.rb +0 -2
- data/lib/net/ssh/transport/hmac/md5_96.rb +0 -2
- data/lib/net/ssh/transport/hmac/none.rb +0 -2
- data/lib/net/ssh/transport/hmac/ripemd160.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha1.rb +0 -2
- data/lib/net/ssh/transport/hmac/sha1_96.rb +0 -2
- 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/hmac.rb +14 -12
- data/lib/net/ssh/transport/identity_cipher.rb +54 -52
- data/lib/net/ssh/transport/kex/abstract.rb +130 -0
- data/lib/net/ssh/transport/kex/abstract5656.rb +72 -0
- data/lib/net/ssh/transport/kex/curve25519_sha256.rb +39 -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_group14_sha256.rb +11 -0
- 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 -63
- 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/kex.rb +15 -12
- data/lib/net/ssh/transport/key_expander.rb +24 -21
- data/lib/net/ssh/transport/openssl.rb +158 -133
- data/lib/net/ssh/transport/packet_stream.rb +223 -191
- data/lib/net/ssh/transport/server_version.rb +55 -56
- data/lib/net/ssh/transport/session.rb +306 -259
- data/lib/net/ssh/transport/state.rb +178 -176
- data/lib/net/ssh/verifiers/accept_new.rb +33 -0
- data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +33 -0
- data/lib/net/ssh/verifiers/always.rb +58 -0
- data/lib/net/ssh/verifiers/never.rb +19 -0
- data/lib/net/ssh/version.rb +55 -53
- data/lib/net/ssh.rb +47 -34
- data/net-ssh-public_cert.pem +18 -19
- data/net-ssh.gemspec +12 -11
- data/support/ssh_tunnel_bug.rb +5 -5
- data.tar.gz.sig +0 -0
- metadata +78 -73
- metadata.gz.sig +0 -0
- data/.travis.yml +0 -51
- 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/known_hosts.rb
CHANGED
|
@@ -1,98 +1,150 @@
|
|
|
1
1
|
require 'strscan'
|
|
2
2
|
require 'openssl'
|
|
3
3
|
require 'base64'
|
|
4
|
+
require 'delegate'
|
|
4
5
|
require 'net/ssh/buffer'
|
|
6
|
+
require 'net/ssh/authentication/ed25519_loader'
|
|
5
7
|
|
|
6
|
-
module Net
|
|
8
|
+
module Net
|
|
9
|
+
module SSH
|
|
10
|
+
module HostKeyEntries
|
|
11
|
+
# regular public key entry
|
|
12
|
+
class PubKey < Delegator
|
|
13
|
+
def initialize(key, comment: nil) # rubocop:disable Lint/MissingSuper
|
|
14
|
+
@key = key
|
|
15
|
+
@comment = comment
|
|
16
|
+
end
|
|
7
17
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
include Enumerable
|
|
12
|
-
attr_reader :host
|
|
18
|
+
def ssh_type
|
|
19
|
+
@key.ssh_type
|
|
20
|
+
end
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
@known_hosts = known_hosts
|
|
18
|
-
@options = options
|
|
19
|
-
end
|
|
22
|
+
def ssh_types
|
|
23
|
+
[ssh_type]
|
|
24
|
+
end
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
end
|
|
26
|
+
def to_blob
|
|
27
|
+
@key.to_blob
|
|
28
|
+
end
|
|
25
29
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
def __getobj__
|
|
31
|
+
Kernel.warn("Calling Net::SSH::Buffer methods on HostKeyEntries PubKey is deprecated")
|
|
32
|
+
@key
|
|
33
|
+
end
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
35
|
+
def matches_key?(server_key)
|
|
36
|
+
@key.ssh_type == server_key.ssh_type && @key.to_blob == server_key.to_blob
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @cert-authority entry
|
|
41
|
+
class CertAuthority
|
|
42
|
+
def ssh_types
|
|
43
|
+
%w[
|
|
44
|
+
ecdsa-sha2-nistp256-cert-v01@openssh.com
|
|
45
|
+
ecdsa-sha2-nistp384-cert-v01@openssh.com
|
|
46
|
+
ecdsa-sha2-nistp521-cert-v01@openssh.com
|
|
47
|
+
ssh-ed25519-cert-v01@openssh.com
|
|
48
|
+
ssh-rsa-cert-v01@openssh.com
|
|
49
|
+
ssh-rsa-cert-v00@openssh.com
|
|
50
|
+
]
|
|
51
|
+
end
|
|
34
52
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
# This is used internally by Net::SSH, and will never need to be used directly
|
|
40
|
-
# by consumers of the library.
|
|
41
|
-
class KnownHosts
|
|
53
|
+
def initialize(key, comment: nil)
|
|
54
|
+
@key = key
|
|
55
|
+
@comment = comment
|
|
56
|
+
end
|
|
42
57
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
58
|
+
def matches_key?(server_key)
|
|
59
|
+
if ssh_types.include?(server_key.ssh_type)
|
|
60
|
+
server_key.signature_valid? && (server_key.signature_key.to_blob == @key.to_blob)
|
|
61
|
+
else
|
|
62
|
+
false
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
50
66
|
end
|
|
51
67
|
|
|
68
|
+
# Represents the result of a search in known hosts
|
|
69
|
+
# see search_for
|
|
70
|
+
class HostKeys
|
|
71
|
+
include Enumerable
|
|
72
|
+
attr_reader :host
|
|
52
73
|
|
|
53
|
-
|
|
74
|
+
def initialize(host_keys, host, known_hosts, options = {})
|
|
75
|
+
@host_keys = host_keys
|
|
76
|
+
@host = host
|
|
77
|
+
@known_hosts = known_hosts
|
|
78
|
+
@options = options
|
|
79
|
+
end
|
|
54
80
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
HostKeys.new(search_in(hostfiles(options), host), host, self, options)
|
|
81
|
+
def add_host_key(key)
|
|
82
|
+
@known_hosts.add(@host, key, @options)
|
|
83
|
+
@host_keys.push(key)
|
|
59
84
|
end
|
|
60
85
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def search_in(files, host)
|
|
64
|
-
files.map { |file| KnownHosts.new(file).keys_for(host) }.flatten
|
|
86
|
+
def each(&block)
|
|
87
|
+
@host_keys.each(&block)
|
|
65
88
|
end
|
|
66
89
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
90
|
+
def empty?
|
|
91
|
+
@host_keys.empty?
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Searches an OpenSSH-style known-host file for a given host, and returns all
|
|
96
|
+
# matching keys. This is used to implement host-key verification, as well as
|
|
97
|
+
# to determine what key a user prefers to use for a given host.
|
|
98
|
+
#
|
|
99
|
+
# This is used internally by Net::SSH, and will never need to be used directly
|
|
100
|
+
# by consumers of the library.
|
|
101
|
+
class KnownHosts
|
|
102
|
+
SUPPORTED_TYPE = %w[ssh-rsa ssh-dss
|
|
103
|
+
ecdsa-sha2-nistp256
|
|
104
|
+
ecdsa-sha2-nistp384
|
|
105
|
+
ecdsa-sha2-nistp521]
|
|
78
106
|
|
|
79
|
-
|
|
80
|
-
|
|
107
|
+
SUPPORTED_TYPE.push('ssh-ed25519') if Net::SSH::Authentication::ED25519Loader::LOADED
|
|
108
|
+
|
|
109
|
+
class << self
|
|
110
|
+
# Searches all known host files (see KnownHosts.hostfiles) for all keys
|
|
111
|
+
# of the given host. Returns an enumerable of keys found.
|
|
112
|
+
def search_for(host, options = {})
|
|
113
|
+
HostKeys.new(search_in(hostfiles(options), host, options), host, self, options)
|
|
81
114
|
end
|
|
82
115
|
|
|
83
|
-
|
|
84
|
-
|
|
116
|
+
# Search for all known keys for the given host, in every file given in
|
|
117
|
+
# the +files+ array. Returns the list of keys.
|
|
118
|
+
def search_in(files, host, options = {})
|
|
119
|
+
files.flat_map { |file| KnownHosts.new(file).keys_for(host, options) }
|
|
85
120
|
end
|
|
86
121
|
|
|
87
|
-
|
|
88
|
-
|
|
122
|
+
# Looks in the given +options+ hash for the :user_known_hosts_file and
|
|
123
|
+
# :global_known_hosts_file keys, and returns an array of all known
|
|
124
|
+
# hosts files. If the :user_known_hosts_file key is not set, the
|
|
125
|
+
# default is returned (~/.ssh/known_hosts and ~/.ssh/known_hosts2). If
|
|
126
|
+
# :global_known_hosts_file is not set, the default is used
|
|
127
|
+
# (/etc/ssh/ssh_known_hosts and /etc/ssh/ssh_known_hosts2).
|
|
128
|
+
#
|
|
129
|
+
# If you only want the user known host files, you can pass :user as
|
|
130
|
+
# the second option.
|
|
131
|
+
def hostfiles(options, which = :all)
|
|
132
|
+
files = []
|
|
133
|
+
|
|
134
|
+
files += Array(options[:user_known_hosts_file] || %w[~/.ssh/known_hosts ~/.ssh/known_hosts2]) if which == :all || which == :user
|
|
135
|
+
|
|
136
|
+
if which == :all || which == :global
|
|
137
|
+
files += Array(options[:global_known_hosts_file] || %w[/etc/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts2])
|
|
138
|
+
end
|
|
89
139
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
140
|
+
return files
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Looks in all user known host files (see KnownHosts.hostfiles) and tries to
|
|
144
|
+
# add an entry for the given host and key to the first file it is able
|
|
145
|
+
# to.
|
|
146
|
+
def add(host, key, options = {})
|
|
147
|
+
hostfiles(options, :user).each do |file|
|
|
96
148
|
KnownHosts.new(file).add(host, key)
|
|
97
149
|
return
|
|
98
150
|
rescue SystemCallError
|
|
@@ -100,88 +152,114 @@ module Net; module SSH
|
|
|
100
152
|
end
|
|
101
153
|
end
|
|
102
154
|
end
|
|
103
|
-
end
|
|
104
155
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
156
|
+
# The host-key file name that this KnownHosts instance will use to search
|
|
157
|
+
# for keys.
|
|
158
|
+
attr_reader :source
|
|
108
159
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
160
|
+
# Instantiate a new KnownHosts instance that will search the given known-hosts
|
|
161
|
+
# file. The path is expanded file File.expand_path.
|
|
162
|
+
def initialize(source)
|
|
163
|
+
@source = File.expand_path(source)
|
|
164
|
+
end
|
|
114
165
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
166
|
+
# Returns an array of all keys that are known to be associatd with the
|
|
167
|
+
# given host. The +host+ parameter is either the domain name or ip address
|
|
168
|
+
# of the host, or both (comma-separated). Additionally, if a non-standard
|
|
169
|
+
# port is being used, it may be specified by putting the host (or ip, or
|
|
170
|
+
# both) in square brackets, and appending the port outside the brackets
|
|
171
|
+
# after a colon. Possible formats for +host+, then, are;
|
|
172
|
+
#
|
|
173
|
+
# "net.ssh.test"
|
|
174
|
+
# "1.2.3.4"
|
|
175
|
+
# "net.ssh.test,1.2.3.4"
|
|
176
|
+
# "[net.ssh.test]:5555"
|
|
177
|
+
# "[1,2,3,4]:5555"
|
|
178
|
+
# "[net.ssh.test]:5555,[1.2.3.4]:5555
|
|
179
|
+
def keys_for(host, options = {})
|
|
180
|
+
keys = []
|
|
181
|
+
return keys unless File.readable?(source)
|
|
182
|
+
|
|
183
|
+
entries = host.split(/,/)
|
|
184
|
+
host_name = entries[0]
|
|
185
|
+
host_ip = entries[1]
|
|
131
186
|
|
|
132
|
-
|
|
187
|
+
File.open(source) do |file|
|
|
188
|
+
file.each_line do |line|
|
|
189
|
+
if line.start_with?('@')
|
|
190
|
+
marker, hosts, type, key_content, comment = line.split(' ')
|
|
191
|
+
else
|
|
192
|
+
marker = nil
|
|
193
|
+
hosts, type, key_content, comment = line.split(' ')
|
|
194
|
+
end
|
|
133
195
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
file.each_line do |line|
|
|
137
|
-
scanner.string = line
|
|
196
|
+
# Skip empty line or one that is commented
|
|
197
|
+
next if hosts.nil? || hosts.start_with?('#')
|
|
138
198
|
|
|
139
|
-
|
|
140
|
-
next if scanner.match?(/$|#/)
|
|
199
|
+
hostlist = hosts.split(',')
|
|
141
200
|
|
|
142
|
-
|
|
143
|
-
found = entries.all? { |entry| hostlist.include?(entry) } ||
|
|
144
|
-
known_host_hash?(hostlist, entries, scanner)
|
|
145
|
-
next unless found
|
|
201
|
+
next unless SUPPORTED_TYPE.include?(type)
|
|
146
202
|
|
|
147
|
-
|
|
148
|
-
|
|
203
|
+
found = hostlist.any? { |pattern| match(host_name, pattern) } || known_host_hash?(hostlist, entries)
|
|
204
|
+
next unless found
|
|
149
205
|
|
|
150
|
-
|
|
206
|
+
found = hostlist.include?(host_ip) if options[:check_host_ip] && entries.size > 1 && hostlist.size > 1
|
|
207
|
+
next unless found
|
|
151
208
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
209
|
+
blob = key_content.unpack("m*").first
|
|
210
|
+
raw_key = Net::SSH::Buffer.new(blob).read_key
|
|
211
|
+
|
|
212
|
+
keys <<
|
|
213
|
+
if marker == "@cert-authority"
|
|
214
|
+
HostKeyEntries::CertAuthority.new(raw_key, comment: comment)
|
|
215
|
+
else
|
|
216
|
+
HostKeyEntries::PubKey.new(raw_key, comment: comment)
|
|
217
|
+
end
|
|
218
|
+
end
|
|
155
219
|
end
|
|
220
|
+
|
|
221
|
+
keys
|
|
156
222
|
end
|
|
157
223
|
|
|
158
|
-
|
|
159
|
-
|
|
224
|
+
def match(host, pattern)
|
|
225
|
+
if pattern.include?('*') || pattern.include?('?')
|
|
226
|
+
# see man 8 sshd for pattern details
|
|
227
|
+
pattern_regexp = pattern.split('*', -1).map do |x|
|
|
228
|
+
x.split('?', -1).map do |y|
|
|
229
|
+
Regexp.escape(y)
|
|
230
|
+
end.join('.')
|
|
231
|
+
end.join('.*')
|
|
160
232
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if hostlist.size == 1 && hostlist.first =~ /\A\|1(\|.+){2}\z/
|
|
165
|
-
chunks = hostlist.first.split(/\|/)
|
|
166
|
-
salt = Base64.decode64(chunks[2])
|
|
167
|
-
digest = OpenSSL::Digest.new('sha1')
|
|
168
|
-
entries.each do |entry|
|
|
169
|
-
hmac = OpenSSL::HMAC.digest(digest, salt, entry)
|
|
170
|
-
return true if Base64.encode64(hmac).chomp == chunks[3]
|
|
233
|
+
host =~ Regexp.new("\\A#{pattern_regexp}\\z")
|
|
234
|
+
else
|
|
235
|
+
host == pattern
|
|
171
236
|
end
|
|
172
237
|
end
|
|
173
|
-
false
|
|
174
|
-
end
|
|
175
238
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
239
|
+
# Indicates whether one of the entries matches an hostname that has been
|
|
240
|
+
# stored as a HMAC-SHA1 hash in the known hosts.
|
|
241
|
+
def known_host_hash?(hostlist, entries)
|
|
242
|
+
if hostlist.size == 1 && hostlist.first =~ /\A\|1(\|.+){2}\z/
|
|
243
|
+
chunks = hostlist.first.split(/\|/)
|
|
244
|
+
salt = Base64.decode64(chunks[2])
|
|
245
|
+
digest = OpenSSL::Digest.new('sha1')
|
|
246
|
+
entries.each do |entry|
|
|
247
|
+
hmac = OpenSSL::HMAC.digest(digest, salt, entry)
|
|
248
|
+
return true if Base64.encode64(hmac).chomp == chunks[3]
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
false
|
|
183
252
|
end
|
|
184
|
-
end
|
|
185
253
|
|
|
254
|
+
# Tries to append an entry to the current source file for the given host
|
|
255
|
+
# and key. If it is unable to (because the file is not writable, for
|
|
256
|
+
# instance), an exception will be raised.
|
|
257
|
+
def add(host, key)
|
|
258
|
+
File.open(source, "a") do |file|
|
|
259
|
+
blob = [Net::SSH::Buffer.from(:key, key).to_s].pack("m*").gsub(/\s/, "")
|
|
260
|
+
file.puts "#{host} #{key.ssh_type} #{blob}"
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
end
|
|
186
264
|
end
|
|
187
|
-
end
|
|
265
|
+
end
|
data/lib/net/ssh/loggable.rb
CHANGED
|
@@ -1,61 +1,62 @@
|
|
|
1
|
-
module Net
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
module Net
|
|
2
|
+
module SSH
|
|
3
|
+
# A simple module to make logging easier to deal with. It assumes that the
|
|
4
|
+
# logger instance (if not nil) quacks like a Logger object (in Ruby's
|
|
5
|
+
# standard library). Although used primarily internally by Net::SSH, it
|
|
6
|
+
# can easily be used to add Net::SSH-like logging to your own programs.
|
|
7
|
+
#
|
|
8
|
+
# class MyClass
|
|
9
|
+
# include Net::SSH::Loggable
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# Net::SSH.start(...) do |ssh|
|
|
13
|
+
# obj = MyClass.new
|
|
14
|
+
# obj.logger = ssh.logger
|
|
15
|
+
# ...
|
|
16
|
+
# end
|
|
17
|
+
module Loggable
|
|
18
|
+
# The logger instance that will be used to log messages. If nil, nothing
|
|
19
|
+
# will be logged.
|
|
20
|
+
attr_accessor :logger
|
|
21
|
+
|
|
22
|
+
# Displays the result of yielding if the log level is Logger::DEBUG or
|
|
23
|
+
# greater.
|
|
24
|
+
def debug
|
|
25
|
+
logger.add(Logger::DEBUG, nil, facility) { yield } if logger && logger.debug?
|
|
26
|
+
end
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
# Displays the result of yielding if the log level is Logger::INFO or
|
|
29
|
+
# greater.
|
|
30
|
+
def info
|
|
31
|
+
logger.add(Logger::INFO, nil, facility) { yield } if logger && logger.info?
|
|
32
|
+
end
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
# Displays the result of yielding if the log level is Logger::WARN or
|
|
35
|
+
# greater. (Called lwarn to avoid shadowing with Kernel#warn.)
|
|
36
|
+
def lwarn
|
|
37
|
+
logger.add(Logger::WARN, nil, facility) { yield } if logger && logger.warn?
|
|
38
|
+
end
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
# Displays the result of yielding if the log level is Logger:ERROR or
|
|
41
|
+
# greater.
|
|
42
|
+
def error
|
|
43
|
+
logger.add(Logger::ERROR, nil, facility) { yield } if logger && logger.error?
|
|
44
|
+
end
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
# Displays the result of yielding if the log level is Logger::FATAL or
|
|
47
|
+
# greater.
|
|
48
|
+
def fatal
|
|
49
|
+
logger.add(Logger::FATAL, nil, facility) { yield } if logger && logger.fatal?
|
|
50
|
+
end
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
private
|
|
53
53
|
|
|
54
54
|
# Sets the "facility" value, used for reporting where a log message
|
|
55
55
|
# originates. It defaults to the name of class with the object_id
|
|
56
56
|
# appended.
|
|
57
57
|
def facility
|
|
58
|
-
@facility ||= self.class.
|
|
58
|
+
@facility ||= self.class.to_s.gsub(/::/, ".").gsub(/([a-z])([A-Z])/, "\\1_\\2").downcase + "[%x]" % object_id
|
|
59
59
|
end
|
|
60
|
+
end
|
|
60
61
|
end
|
|
61
|
-
end
|
|
62
|
+
end
|