net-ssh 5.0.0.beta1 → 5.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.rubocop_todo.yml +98 -258
- data/CHANGES.txt +8 -0
- data/Gemfile +1 -3
- data/Rakefile +37 -39
- data/lib/net/ssh.rb +26 -25
- data/lib/net/ssh/authentication/agent.rb +228 -225
- data/lib/net/ssh/authentication/certificate.rb +166 -164
- data/lib/net/ssh/authentication/constants.rb +17 -14
- data/lib/net/ssh/authentication/ed25519.rb +107 -104
- data/lib/net/ssh/authentication/ed25519_loader.rb +32 -28
- data/lib/net/ssh/authentication/key_manager.rb +5 -3
- 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 +2 -4
- 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 +54 -55
- data/lib/net/ssh/authentication/pageant.rb +468 -465
- data/lib/net/ssh/authentication/pub_key_fingerprint.rb +44 -0
- data/lib/net/ssh/authentication/session.rb +127 -123
- data/lib/net/ssh/buffer.rb +305 -303
- data/lib/net/ssh/buffered_io.rb +163 -162
- data/lib/net/ssh/config.rb +230 -227
- data/lib/net/ssh/connection/channel.rb +659 -654
- data/lib/net/ssh/connection/constants.rb +30 -26
- data/lib/net/ssh/connection/event_loop.rb +108 -104
- data/lib/net/ssh/connection/keepalive.rb +54 -50
- data/lib/net/ssh/connection/session.rb +677 -678
- data/lib/net/ssh/connection/term.rb +180 -176
- data/lib/net/ssh/errors.rb +101 -99
- data/lib/net/ssh/key_factory.rb +108 -108
- data/lib/net/ssh/known_hosts.rb +148 -154
- data/lib/net/ssh/loggable.rb +56 -54
- data/lib/net/ssh/packet.rb +82 -78
- data/lib/net/ssh/prompt.rb +55 -53
- data/lib/net/ssh/proxy/command.rb +103 -102
- data/lib/net/ssh/proxy/errors.rb +12 -8
- data/lib/net/ssh/proxy/http.rb +92 -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 -11
- data/lib/net/ssh/ruby_compat.rb +1 -0
- data/lib/net/ssh/service/forward.rb +364 -362
- data/lib/net/ssh/test.rb +85 -83
- data/lib/net/ssh/test/channel.rb +146 -142
- data/lib/net/ssh/test/extensions.rb +148 -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 +413 -412
- data/lib/net/ssh/transport/cipher_factory.rb +108 -105
- data/lib/net/ssh/transport/constants.rb +35 -31
- data/lib/net/ssh/transport/ctr.rb +1 -1
- data/lib/net/ssh/transport/hmac.rb +1 -1
- data/lib/net/ssh/transport/hmac/abstract.rb +67 -64
- data/lib/net/ssh/transport/hmac/sha2_256_96.rb +1 -1
- data/lib/net/ssh/transport/hmac/sha2_512_96.rb +1 -1
- data/lib/net/ssh/transport/identity_cipher.rb +55 -51
- data/lib/net/ssh/transport/kex.rb +2 -4
- data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +47 -40
- data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +201 -197
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +53 -56
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +94 -87
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +17 -10
- data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +17 -10
- data/lib/net/ssh/transport/key_expander.rb +29 -25
- data/lib/net/ssh/transport/openssl.rb +17 -30
- data/lib/net/ssh/transport/packet_stream.rb +193 -192
- data/lib/net/ssh/transport/server_version.rb +64 -66
- data/lib/net/ssh/transport/session.rb +286 -284
- data/lib/net/ssh/transport/state.rb +198 -196
- data/lib/net/ssh/verifiers/lenient.rb +29 -25
- data/lib/net/ssh/verifiers/null.rb +13 -9
- data/lib/net/ssh/verifiers/secure.rb +45 -45
- data/lib/net/ssh/verifiers/strict.rb +20 -16
- data/lib/net/ssh/version.rb +55 -53
- data/net-ssh.gemspec +4 -4
- data/support/ssh_tunnel_bug.rb +2 -2
- metadata +25 -24
- metadata.gz.sig +0 -0
data/lib/net/ssh/key_factory.rb
CHANGED
@@ -3,130 +3,130 @@ require 'net/ssh/prompt'
|
|
3
3
|
|
4
4
|
require 'net/ssh/authentication/ed25519_loader'
|
5
5
|
|
6
|
-
module Net
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
class <<self
|
30
|
-
# Fetch an OpenSSL key instance by its SSH name. It will be a new,
|
31
|
-
# empty key of the given type.
|
32
|
-
def get(name)
|
33
|
-
MAP.fetch(name).new
|
34
|
-
end
|
35
|
-
|
36
|
-
# Loads a private key from a file. It will correctly determine
|
37
|
-
# whether the file describes an RSA or DSA key, and will load it
|
38
|
-
# appropriately. The new key is returned. If the key itself is
|
39
|
-
# encrypted (requiring a passphrase to use), the user will be
|
40
|
-
# prompted to enter their password unless passphrase works.
|
41
|
-
def load_private_key(filename, passphrase=nil, ask_passphrase=true, prompt=Prompt.default)
|
42
|
-
data = File.read(File.expand_path(filename))
|
43
|
-
load_data_private_key(data, passphrase, ask_passphrase, filename, prompt)
|
6
|
+
module Net
|
7
|
+
module SSH
|
8
|
+
|
9
|
+
# A factory class for returning new Key classes. It is used for obtaining
|
10
|
+
# OpenSSL key instances via their SSH names, and for loading both public and
|
11
|
+
# private keys. It used used primarily by Net::SSH itself, internally, and
|
12
|
+
# will rarely (if ever) be directly used by consumers of the library.
|
13
|
+
#
|
14
|
+
# klass = Net::SSH::KeyFactory.get("rsa")
|
15
|
+
# assert klass.is_a?(OpenSSL::PKey::RSA)
|
16
|
+
#
|
17
|
+
# key = Net::SSH::KeyFactory.load_public_key("~/.ssh/id_dsa.pub")
|
18
|
+
class KeyFactory
|
19
|
+
# Specifies the mapping of SSH names to OpenSSL key classes.
|
20
|
+
MAP = {
|
21
|
+
"dh" => OpenSSL::PKey::DH,
|
22
|
+
"rsa" => OpenSSL::PKey::RSA,
|
23
|
+
"dsa" => OpenSSL::PKey::DSA
|
24
|
+
}
|
25
|
+
if defined?(OpenSSL::PKey::EC)
|
26
|
+
MAP["ecdsa"] = OpenSSL::PKey::EC
|
27
|
+
MAP["ed25519"] = Net::SSH::Authentication::ED25519::PrivKey if defined? Net::SSH::Authentication::ED25519
|
44
28
|
end
|
45
29
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
key_read, error_classes = classify_key(data, filename)
|
30
|
+
class <<self
|
31
|
+
# Fetch an OpenSSL key instance by its SSH name. It will be a new,
|
32
|
+
# empty key of the given type.
|
33
|
+
def get(name)
|
34
|
+
MAP.fetch(name).new
|
35
|
+
end
|
53
36
|
|
54
|
-
|
55
|
-
|
37
|
+
# Loads a private key from a file. It will correctly determine
|
38
|
+
# whether the file describes an RSA or DSA key, and will load it
|
39
|
+
# appropriately. The new key is returned. If the key itself is
|
40
|
+
# encrypted (requiring a passphrase to use), the user will be
|
41
|
+
# prompted to enter their password unless passphrase works.
|
42
|
+
def load_private_key(filename, passphrase=nil, ask_passphrase=true, prompt=Prompt.default)
|
43
|
+
data = File.read(File.expand_path(filename))
|
44
|
+
load_data_private_key(data, passphrase, ask_passphrase, filename, prompt)
|
45
|
+
end
|
56
46
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
47
|
+
# Loads a private key. It will correctly determine
|
48
|
+
# whether the file describes an RSA or DSA key, and will load it
|
49
|
+
# appropriately. The new key is returned. If the key itself is
|
50
|
+
# encrypted (requiring a passphrase to use), the user will be
|
51
|
+
# prompted to enter their password unless passphrase works.
|
52
|
+
def load_data_private_key(data, passphrase=nil, ask_passphrase=true, filename="", prompt=Prompt.default)
|
53
|
+
key_read, error_classes = classify_key(data, filename)
|
54
|
+
|
55
|
+
encrypted_key = data.match(/ENCRYPTED/)
|
56
|
+
tries = 0
|
57
|
+
|
58
|
+
prompter = nil
|
59
|
+
result =
|
60
|
+
begin
|
61
|
+
key_read[data, passphrase || 'invalid']
|
62
|
+
rescue *error_classes
|
63
|
+
if encrypted_key && ask_passphrase
|
64
|
+
tries += 1
|
65
|
+
if tries <= 3
|
66
|
+
prompter ||= prompt.start(type: 'private_key', filename: filename, sha: Digest::SHA256.digest(data))
|
67
|
+
passphrase = prompter.ask("Enter passphrase for #{filename}:", false)
|
68
|
+
retry
|
69
|
+
else
|
70
|
+
raise
|
71
|
+
end
|
68
72
|
else
|
69
73
|
raise
|
70
74
|
end
|
71
|
-
else
|
72
|
-
raise
|
73
75
|
end
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
end
|
76
|
+
prompter.success if prompter
|
77
|
+
result
|
78
|
+
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
80
|
+
# Loads a public key from a file. It will correctly determine whether
|
81
|
+
# the file describes an RSA or DSA key, and will load it
|
82
|
+
# appropriately. The new public key is returned.
|
83
|
+
def load_public_key(filename)
|
84
|
+
data = File.read(File.expand_path(filename))
|
85
|
+
load_data_public_key(data, filename)
|
86
|
+
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
88
|
+
# Loads a public key. It will correctly determine whether
|
89
|
+
# the file describes an RSA or DSA key, and will load it
|
90
|
+
# appropriately. The new public key is returned.
|
91
|
+
def load_data_public_key(data, filename="")
|
92
|
+
fields = data.split(/ /)
|
92
93
|
|
93
|
-
|
94
|
-
|
94
|
+
blob = nil
|
95
|
+
begin
|
96
|
+
blob = fields.shift
|
97
|
+
end while !blob.nil? && !/^(ssh-(rsa|dss|ed25519)|ecdsa-sha2-nistp\d+)(-cert-v01@openssh\.com)?$/.match(blob)
|
95
98
|
blob = fields.shift
|
96
|
-
end while !blob.nil? && !/^(ssh-(rsa|dss|ed25519)|ecdsa-sha2-nistp\d+)(-cert-v01@openssh\.com)?$/.match(blob)
|
97
|
-
blob = fields.shift
|
98
|
-
|
99
|
-
raise Net::SSH::Exception, "public key at #{filename} is not valid" if blob.nil?
|
100
99
|
|
101
|
-
|
102
|
-
reader = Net::SSH::Buffer.new(blob)
|
103
|
-
reader.read_key or raise OpenSSL::PKey::PKeyError, "not a public key #{filename.inspect}"
|
104
|
-
end
|
100
|
+
raise Net::SSH::Exception, "public key at #{filename} is not valid" if blob.nil?
|
105
101
|
|
106
|
-
|
102
|
+
blob = blob.unpack("m*").first
|
103
|
+
reader = Net::SSH::Buffer.new(blob)
|
104
|
+
reader.read_key or raise OpenSSL::PKey::PKeyError, "not a public key #{filename.inspect}"
|
105
|
+
end
|
107
106
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
107
|
+
private
|
108
|
+
|
109
|
+
# Determine whether the file describes an RSA or DSA key, and return how load it
|
110
|
+
# appropriately.
|
111
|
+
def classify_key(data, filename)
|
112
|
+
if data.match(/-----BEGIN OPENSSH PRIVATE KEY-----/)
|
113
|
+
Net::SSH::Authentication::ED25519Loader.raiseUnlessLoaded("OpenSSH keys only supported if ED25519 is available")
|
114
|
+
return ->(key_data, passphrase) { Net::SSH::Authentication::ED25519::PrivKey.read(key_data, passphrase) }, [ArgumentError]
|
115
|
+
elsif OpenSSL::PKey.respond_to?(:read)
|
116
|
+
return ->(key_data, passphrase) { OpenSSL::PKey.read(key_data, passphrase) }, [ArgumentError, OpenSSL::PKey::PKeyError]
|
117
|
+
elsif data.match(/-----BEGIN DSA PRIVATE KEY-----/)
|
118
|
+
return ->(key_data, passphrase) { OpenSSL::PKey::DSA.new(key_data, passphrase) }, [OpenSSL::PKey::DSAError]
|
119
|
+
elsif data.match(/-----BEGIN RSA PRIVATE KEY-----/)
|
120
|
+
return ->(key_data, passphrase) { OpenSSL::PKey::RSA.new(key_data, passphrase) }, [OpenSSL::PKey::RSAError]
|
121
|
+
elsif data.match(/-----BEGIN EC PRIVATE KEY-----/) && defined?(OpenSSL::PKey::EC)
|
122
|
+
return ->(key_data, passphrase) { OpenSSL::PKey::EC.new(key_data, passphrase) }, [OpenSSL::PKey::ECError]
|
123
|
+
elsif data.match(/-----BEGIN (.+) PRIVATE KEY-----/)
|
124
|
+
raise OpenSSL::PKey::PKeyError, "not a supported key type '#{$1}'"
|
125
|
+
else
|
126
|
+
raise OpenSSL::PKey::PKeyError, "not a private key (#{filename})"
|
127
|
+
end
|
126
128
|
end
|
127
129
|
end
|
128
130
|
end
|
129
|
-
|
130
131
|
end
|
131
|
-
|
132
|
-
end; end
|
132
|
+
end
|
data/lib/net/ssh/known_hosts.rb
CHANGED
@@ -3,185 +3,179 @@ require 'openssl'
|
|
3
3
|
require 'base64'
|
4
4
|
require 'net/ssh/buffer'
|
5
5
|
|
6
|
-
module Net
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
6
|
+
module Net
|
7
|
+
module SSH
|
8
|
+
|
9
|
+
# Represents the result of a search in known hosts
|
10
|
+
# see search_for
|
11
|
+
class HostKeys
|
12
|
+
include Enumerable
|
13
|
+
attr_reader :host
|
14
|
+
|
15
|
+
def initialize(host_keys, host, known_hosts, options = {})
|
16
|
+
@host_keys = host_keys
|
17
|
+
@host = host
|
18
|
+
@known_hosts = known_hosts
|
19
|
+
@options = options
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def add_host_key(key)
|
23
|
+
@known_hosts.add(@host, key, @options)
|
24
|
+
@host_keys.push(key)
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def each(&block)
|
28
|
+
@host_keys.each(&block)
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
def empty?
|
32
|
+
@host_keys.empty?
|
33
|
+
end
|
32
34
|
end
|
33
|
-
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
36
|
+
# Searches an OpenSSH-style known-host file for a given host, and returns all
|
37
|
+
# matching keys. This is used to implement host-key verification, as well as
|
38
|
+
# to determine what key a user prefers to use for a given host.
|
39
|
+
#
|
40
|
+
# This is used internally by Net::SSH, and will never need to be used directly
|
41
|
+
# by consumers of the library.
|
42
|
+
class KnownHosts
|
43
|
+
if defined?(OpenSSL::PKey::EC)
|
44
|
+
SUPPORTED_TYPE = %w[ssh-rsa ssh-dss
|
45
|
+
ecdsa-sha2-nistp256
|
46
|
+
ecdsa-sha2-nistp384
|
47
|
+
ecdsa-sha2-nistp521]
|
48
|
+
else
|
49
|
+
SUPPORTED_TYPE = %w[ssh-rsa ssh-dss]
|
50
|
+
end
|
51
51
|
|
52
|
+
class <<self
|
53
|
+
# Searches all known host files (see KnownHosts.hostfiles) for all keys
|
54
|
+
# of the given host. Returns an enumerable of keys found.
|
55
|
+
def search_for(host, options={})
|
56
|
+
HostKeys.new(search_in(hostfiles(options), host), host, self, options)
|
57
|
+
end
|
52
58
|
|
53
|
-
|
59
|
+
# Search for all known keys for the given host, in every file given in
|
60
|
+
# the +files+ array. Returns the list of keys.
|
61
|
+
def search_in(files, host)
|
62
|
+
files.flat_map { |file| KnownHosts.new(file).keys_for(host) }
|
63
|
+
end
|
54
64
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
65
|
+
# Looks in the given +options+ hash for the :user_known_hosts_file and
|
66
|
+
# :global_known_hosts_file keys, and returns an array of all known
|
67
|
+
# hosts files. If the :user_known_hosts_file key is not set, the
|
68
|
+
# default is returned (~/.ssh/known_hosts and ~/.ssh/known_hosts2). If
|
69
|
+
# :global_known_hosts_file is not set, the default is used
|
70
|
+
# (/etc/ssh/ssh_known_hosts and /etc/ssh/ssh_known_hosts2).
|
71
|
+
#
|
72
|
+
# If you only want the user known host files, you can pass :user as
|
73
|
+
# the second option.
|
74
|
+
def hostfiles(options, which=:all)
|
75
|
+
files = []
|
60
76
|
|
61
|
-
|
62
|
-
# the +files+ array. Returns the list of keys.
|
63
|
-
def search_in(files, host)
|
64
|
-
files.map { |file| KnownHosts.new(file).keys_for(host) }.flatten
|
65
|
-
end
|
77
|
+
files += Array(options[:user_known_hosts_file] || %w[~/.ssh/known_hosts ~/.ssh/known_hosts2]) if which == :all || which == :user
|
66
78
|
|
67
|
-
|
68
|
-
# :global_known_hosts_file keys, and returns an array of all known
|
69
|
-
# hosts files. If the :user_known_hosts_file key is not set, the
|
70
|
-
# default is returned (~/.ssh/known_hosts and ~/.ssh/known_hosts2). If
|
71
|
-
# :global_known_hosts_file is not set, the default is used
|
72
|
-
# (/etc/ssh/ssh_known_hosts and /etc/ssh/ssh_known_hosts2).
|
73
|
-
#
|
74
|
-
# If you only want the user known host files, you can pass :user as
|
75
|
-
# the second option.
|
76
|
-
def hostfiles(options, which=:all)
|
77
|
-
files = []
|
79
|
+
files += Array(options[:global_known_hosts_file] || %w[/etc/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts2]) if which == :all || which == :global
|
78
80
|
|
79
|
-
|
80
|
-
files += Array(options[:user_known_hosts_file] || %w(~/.ssh/known_hosts ~/.ssh/known_hosts2))
|
81
|
+
return files
|
81
82
|
end
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
hostfiles(options, :user).each do |file|
|
95
|
-
begin
|
96
|
-
KnownHosts.new(file).add(host, key)
|
97
|
-
return
|
98
|
-
rescue SystemCallError
|
99
|
-
# try the next hostfile
|
84
|
+
# Looks in all user known host files (see KnownHosts.hostfiles) and tries to
|
85
|
+
# add an entry for the given host and key to the first file it is able
|
86
|
+
# to.
|
87
|
+
def add(host, key, options={})
|
88
|
+
hostfiles(options, :user).each do |file|
|
89
|
+
begin
|
90
|
+
KnownHosts.new(file).add(host, key)
|
91
|
+
return
|
92
|
+
rescue SystemCallError
|
93
|
+
# try the next hostfile
|
94
|
+
end
|
100
95
|
end
|
101
96
|
end
|
102
97
|
end
|
103
|
-
end
|
104
98
|
|
105
|
-
|
106
|
-
|
107
|
-
|
99
|
+
# The host-key file name that this KnownHosts instance will use to search
|
100
|
+
# for keys.
|
101
|
+
attr_reader :source
|
108
102
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
103
|
+
# Instantiate a new KnownHosts instance that will search the given known-hosts
|
104
|
+
# file. The path is expanded file File.expand_path.
|
105
|
+
def initialize(source)
|
106
|
+
@source = File.expand_path(source)
|
107
|
+
end
|
114
108
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
109
|
+
# Returns an array of all keys that are known to be associatd with the
|
110
|
+
# given host. The +host+ parameter is either the domain name or ip address
|
111
|
+
# of the host, or both (comma-separated). Additionally, if a non-standard
|
112
|
+
# port is being used, it may be specified by putting the host (or ip, or
|
113
|
+
# both) in square brackets, and appending the port outside the brackets
|
114
|
+
# after a colon. Possible formats for +host+, then, are;
|
115
|
+
#
|
116
|
+
# "net.ssh.test"
|
117
|
+
# "1.2.3.4"
|
118
|
+
# "net.ssh.test,1.2.3.4"
|
119
|
+
# "[net.ssh.test]:5555"
|
120
|
+
# "[1,2,3,4]:5555"
|
121
|
+
# "[net.ssh.test]:5555,[1.2.3.4]:5555
|
122
|
+
def keys_for(host)
|
123
|
+
keys = []
|
124
|
+
return keys unless File.readable?(source)
|
125
|
+
|
126
|
+
entries = host.split(/,/)
|
127
|
+
|
128
|
+
File.open(source) do |file|
|
129
|
+
scanner = StringScanner.new("")
|
130
|
+
file.each_line do |line|
|
131
|
+
scanner.string = line
|
132
|
+
|
133
|
+
scanner.skip(/\s*/)
|
134
|
+
next if scanner.match?(/$|#/)
|
135
|
+
|
136
|
+
hostlist = scanner.scan(/\S+/).split(/,/)
|
137
|
+
found = entries.all? { |entry| hostlist.include?(entry) } ||
|
138
|
+
known_host_hash?(hostlist, entries)
|
139
|
+
next unless found
|
140
|
+
|
141
|
+
scanner.skip(/\s*/)
|
142
|
+
type = scanner.scan(/\S+/)
|
143
|
+
|
144
|
+
next unless SUPPORTED_TYPE.include?(type)
|
145
|
+
|
146
|
+
scanner.skip(/\s*/)
|
147
|
+
blob = scanner.rest.unpack("m*").first
|
148
|
+
keys << Net::SSH::Buffer.new(blob).read_key
|
149
|
+
end
|
155
150
|
end
|
156
|
-
end
|
157
151
|
|
158
|
-
|
159
|
-
|
152
|
+
keys
|
153
|
+
end
|
160
154
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
155
|
+
# Indicates whether one of the entries matches an hostname that has been
|
156
|
+
# stored as a HMAC-SHA1 hash in the known hosts.
|
157
|
+
def known_host_hash?(hostlist, entries)
|
158
|
+
if hostlist.size == 1 && hostlist.first =~ /\A\|1(\|.+){2}\z/
|
159
|
+
chunks = hostlist.first.split(/\|/)
|
160
|
+
salt = Base64.decode64(chunks[2])
|
161
|
+
digest = OpenSSL::Digest.new('sha1')
|
162
|
+
entries.each do |entry|
|
163
|
+
hmac = OpenSSL::HMAC.digest(digest, salt, entry)
|
164
|
+
return true if Base64.encode64(hmac).chomp == chunks[3]
|
165
|
+
end
|
171
166
|
end
|
167
|
+
false
|
172
168
|
end
|
173
|
-
false
|
174
|
-
end
|
175
169
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
170
|
+
# Tries to append an entry to the current source file for the given host
|
171
|
+
# and key. If it is unable to (because the file is not writable, for
|
172
|
+
# instance), an exception will be raised.
|
173
|
+
def add(host, key)
|
174
|
+
File.open(source, "a") do |file|
|
175
|
+
blob = [Net::SSH::Buffer.from(:key, key).to_s].pack("m*").gsub(/\s/, "")
|
176
|
+
file.puts "#{host} #{key.ssh_type} #{blob}"
|
177
|
+
end
|
183
178
|
end
|
184
179
|
end
|
185
|
-
|
186
180
|
end
|
187
|
-
end
|
181
|
+
end
|