sonixlabs-net-ssh 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +262 -0
- data/Manifest +121 -0
- data/README.rdoc +184 -0
- data/Rakefile +86 -0
- data/Rudyfile +96 -0
- data/THANKS.rdoc +19 -0
- data/lib/net/ssh.rb +223 -0
- data/lib/net/ssh/authentication/agent.rb +179 -0
- data/lib/net/ssh/authentication/constants.rb +18 -0
- data/lib/net/ssh/authentication/key_manager.rb +253 -0
- data/lib/net/ssh/authentication/methods/abstract.rb +60 -0
- data/lib/net/ssh/authentication/methods/hostbased.rb +75 -0
- data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +70 -0
- data/lib/net/ssh/authentication/methods/password.rb +43 -0
- data/lib/net/ssh/authentication/methods/publickey.rb +96 -0
- data/lib/net/ssh/authentication/pageant.rb +264 -0
- data/lib/net/ssh/authentication/session.rb +146 -0
- data/lib/net/ssh/buffer.rb +340 -0
- data/lib/net/ssh/buffered_io.rb +198 -0
- data/lib/net/ssh/config.rb +207 -0
- data/lib/net/ssh/connection/channel.rb +630 -0
- data/lib/net/ssh/connection/constants.rb +33 -0
- data/lib/net/ssh/connection/session.rb +597 -0
- data/lib/net/ssh/connection/term.rb +178 -0
- data/lib/net/ssh/errors.rb +88 -0
- data/lib/net/ssh/key_factory.rb +102 -0
- data/lib/net/ssh/known_hosts.rb +129 -0
- data/lib/net/ssh/loggable.rb +61 -0
- data/lib/net/ssh/packet.rb +102 -0
- data/lib/net/ssh/prompt.rb +93 -0
- data/lib/net/ssh/proxy/command.rb +75 -0
- data/lib/net/ssh/proxy/errors.rb +14 -0
- data/lib/net/ssh/proxy/http.rb +94 -0
- data/lib/net/ssh/proxy/socks4.rb +70 -0
- data/lib/net/ssh/proxy/socks5.rb +142 -0
- data/lib/net/ssh/ruby_compat.rb +43 -0
- data/lib/net/ssh/service/forward.rb +298 -0
- data/lib/net/ssh/test.rb +89 -0
- data/lib/net/ssh/test/channel.rb +129 -0
- data/lib/net/ssh/test/extensions.rb +152 -0
- data/lib/net/ssh/test/kex.rb +44 -0
- data/lib/net/ssh/test/local_packet.rb +51 -0
- data/lib/net/ssh/test/packet.rb +81 -0
- data/lib/net/ssh/test/remote_packet.rb +38 -0
- data/lib/net/ssh/test/script.rb +157 -0
- data/lib/net/ssh/test/socket.rb +64 -0
- data/lib/net/ssh/transport/algorithms.rb +386 -0
- data/lib/net/ssh/transport/cipher_factory.rb +79 -0
- data/lib/net/ssh/transport/constants.rb +30 -0
- data/lib/net/ssh/transport/hmac.rb +42 -0
- data/lib/net/ssh/transport/hmac/abstract.rb +79 -0
- data/lib/net/ssh/transport/hmac/md5.rb +12 -0
- data/lib/net/ssh/transport/hmac/md5_96.rb +11 -0
- data/lib/net/ssh/transport/hmac/none.rb +15 -0
- data/lib/net/ssh/transport/hmac/sha1.rb +13 -0
- data/lib/net/ssh/transport/hmac/sha1_96.rb +11 -0
- data/lib/net/ssh/transport/hmac/sha2_256.rb +15 -0
- data/lib/net/ssh/transport/hmac/sha2_256_96.rb +13 -0
- data/lib/net/ssh/transport/hmac/sha2_512.rb +14 -0
- data/lib/net/ssh/transport/hmac/sha2_512_96.rb +13 -0
- data/lib/net/ssh/transport/identity_cipher.rb +55 -0
- data/lib/net/ssh/transport/kex.rb +17 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +208 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +80 -0
- data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +15 -0
- data/lib/net/ssh/transport/key_expander.rb +26 -0
- data/lib/net/ssh/transport/openssl.rb +127 -0
- data/lib/net/ssh/transport/packet_stream.rb +235 -0
- data/lib/net/ssh/transport/server_version.rb +71 -0
- data/lib/net/ssh/transport/session.rb +278 -0
- data/lib/net/ssh/transport/state.rb +206 -0
- data/lib/net/ssh/verifiers/lenient.rb +30 -0
- data/lib/net/ssh/verifiers/null.rb +12 -0
- data/lib/net/ssh/verifiers/strict.rb +53 -0
- data/lib/net/ssh/version.rb +62 -0
- data/lib/sonixlabs-net-ssh.rb +1 -0
- data/net-ssh.gemspec +145 -0
- data/setup.rb +1585 -0
- data/support/arcfour_check.rb +20 -0
- data/support/ssh_tunnel_bug.rb +65 -0
- data/test/authentication/methods/common.rb +28 -0
- data/test/authentication/methods/test_abstract.rb +51 -0
- data/test/authentication/methods/test_hostbased.rb +114 -0
- data/test/authentication/methods/test_keyboard_interactive.rb +100 -0
- data/test/authentication/methods/test_password.rb +52 -0
- data/test/authentication/methods/test_publickey.rb +148 -0
- data/test/authentication/test_agent.rb +205 -0
- data/test/authentication/test_key_manager.rb +171 -0
- data/test/authentication/test_session.rb +106 -0
- data/test/common.rb +107 -0
- data/test/configs/eqsign +3 -0
- data/test/configs/exact_match +8 -0
- data/test/configs/host_plus +10 -0
- data/test/configs/multihost +4 -0
- data/test/configs/wild_cards +14 -0
- data/test/connection/test_channel.rb +467 -0
- data/test/connection/test_session.rb +488 -0
- data/test/test_all.rb +9 -0
- data/test/test_buffer.rb +336 -0
- data/test/test_buffered_io.rb +63 -0
- data/test/test_config.rb +120 -0
- data/test/test_key_factory.rb +79 -0
- data/test/transport/hmac/test_md5.rb +39 -0
- data/test/transport/hmac/test_md5_96.rb +25 -0
- data/test/transport/hmac/test_none.rb +34 -0
- data/test/transport/hmac/test_sha1.rb +34 -0
- data/test/transport/hmac/test_sha1_96.rb +25 -0
- data/test/transport/hmac/test_sha2_256.rb +35 -0
- data/test/transport/hmac/test_sha2_256_96.rb +25 -0
- data/test/transport/hmac/test_sha2_512.rb +35 -0
- data/test/transport/hmac/test_sha2_512_96.rb +25 -0
- data/test/transport/kex/test_diffie_hellman_group1_sha1.rb +146 -0
- data/test/transport/kex/test_diffie_hellman_group_exchange_sha1.rb +92 -0
- data/test/transport/kex/test_diffie_hellman_group_exchange_sha256.rb +33 -0
- data/test/transport/test_algorithms.rb +308 -0
- data/test/transport/test_cipher_factory.rb +213 -0
- data/test/transport/test_hmac.rb +34 -0
- data/test/transport/test_identity_cipher.rb +40 -0
- data/test/transport/test_packet_stream.rb +736 -0
- data/test/transport/test_server_version.rb +78 -0
- data/test/transport/test_session.rb +315 -0
- data/test/transport/test_state.rb +179 -0
- metadata +178 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Net; module SSH; module Authentication
|
|
2
|
+
|
|
3
|
+
# Describes the constants used by the Net::SSH::Authentication components
|
|
4
|
+
# of the Net::SSH library. Individual authentication method implemenations
|
|
5
|
+
# may define yet more constants that are specific to their implementation.
|
|
6
|
+
module Constants
|
|
7
|
+
USERAUTH_REQUEST = 50
|
|
8
|
+
USERAUTH_FAILURE = 51
|
|
9
|
+
USERAUTH_SUCCESS = 52
|
|
10
|
+
USERAUTH_BANNER = 53
|
|
11
|
+
|
|
12
|
+
USERAUTH_PASSWD_CHANGEREQ = 60
|
|
13
|
+
USERAUTH_PK_OK = 60
|
|
14
|
+
|
|
15
|
+
USERAUTH_METHOD_RANGE = 60..79
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
end; end; end
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
require 'net/ssh/errors'
|
|
2
|
+
require 'net/ssh/key_factory'
|
|
3
|
+
require 'net/ssh/loggable'
|
|
4
|
+
require 'net/ssh/authentication/agent'
|
|
5
|
+
|
|
6
|
+
module Net
|
|
7
|
+
module SSH
|
|
8
|
+
module Authentication
|
|
9
|
+
|
|
10
|
+
# A trivial exception class used to report errors in the key manager.
|
|
11
|
+
class KeyManagerError < Net::SSH::Exception; end
|
|
12
|
+
|
|
13
|
+
# This class encapsulates all operations done by clients on a user's
|
|
14
|
+
# private keys. In practice, the client should never need a reference
|
|
15
|
+
# to a private key; instead, they grab a list of "identities" (public
|
|
16
|
+
# keys) that are available from the KeyManager, and then use
|
|
17
|
+
# the KeyManager to do various private key operations using those
|
|
18
|
+
# identities.
|
|
19
|
+
#
|
|
20
|
+
# The KeyManager also uses the Agent class to encapsulate the
|
|
21
|
+
# ssh-agent. Thus, from a client's perspective it is completely
|
|
22
|
+
# hidden whether an identity comes from the ssh-agent or from a file
|
|
23
|
+
# on disk.
|
|
24
|
+
class KeyManager
|
|
25
|
+
include Loggable
|
|
26
|
+
|
|
27
|
+
# The list of user key files that will be examined
|
|
28
|
+
attr_reader :key_files
|
|
29
|
+
|
|
30
|
+
# The list of user key data that will be examined
|
|
31
|
+
attr_reader :key_data
|
|
32
|
+
|
|
33
|
+
# The map of loaded identities
|
|
34
|
+
attr_reader :known_identities
|
|
35
|
+
|
|
36
|
+
# The map of options that were passed to the key-manager
|
|
37
|
+
attr_reader :options
|
|
38
|
+
|
|
39
|
+
# Create a new KeyManager. By default, the manager will
|
|
40
|
+
# use the ssh-agent (if it is running).
|
|
41
|
+
def initialize(logger, options={})
|
|
42
|
+
self.logger = logger
|
|
43
|
+
@key_files = []
|
|
44
|
+
@key_data = []
|
|
45
|
+
@use_agent = true
|
|
46
|
+
@known_identities = {}
|
|
47
|
+
@agent = nil
|
|
48
|
+
@options = options
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Clear all knowledge of any loaded user keys. This also clears the list
|
|
52
|
+
# of default identity files that are to be loaded, thus making it
|
|
53
|
+
# appropriate to use if a client wishes to NOT use the default identity
|
|
54
|
+
# files.
|
|
55
|
+
def clear!
|
|
56
|
+
key_files.clear
|
|
57
|
+
key_data.clear
|
|
58
|
+
known_identities.clear
|
|
59
|
+
self
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Add the given key_file to the list of key files that will be used.
|
|
63
|
+
def add(key_file)
|
|
64
|
+
key_files.push(File.expand_path(key_file)).uniq!
|
|
65
|
+
self
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Add the given key_file to the list of keys that will be used.
|
|
69
|
+
def add_key_data(key_data_)
|
|
70
|
+
key_data.push(key_data_).uniq!
|
|
71
|
+
self
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# This is used as a hint to the KeyManager indicating that the agent
|
|
75
|
+
# connection is no longer needed. Any other open resources may be closed
|
|
76
|
+
# at this time.
|
|
77
|
+
#
|
|
78
|
+
# Calling this does NOT indicate that the KeyManager will no longer
|
|
79
|
+
# be used. Identities may still be requested and operations done on
|
|
80
|
+
# loaded identities, in which case, the agent will be automatically
|
|
81
|
+
# reconnected. This method simply allows the client connection to be
|
|
82
|
+
# closed when it will not be used in the immediate future.
|
|
83
|
+
def finish
|
|
84
|
+
@agent.close if @agent
|
|
85
|
+
@agent = nil
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Iterates over all available identities (public keys) known to this
|
|
89
|
+
# manager. As it finds one, it will then yield it to the caller.
|
|
90
|
+
# The origin of the identities may be from files on disk or from an
|
|
91
|
+
# ssh-agent. Note that identities from an ssh-agent are always listed
|
|
92
|
+
# first in the array, with other identities coming after.
|
|
93
|
+
#
|
|
94
|
+
# If key manager was created with :keys_only option, any identity
|
|
95
|
+
# from ssh-agent will be ignored unless it present in key_files or
|
|
96
|
+
# key_data.
|
|
97
|
+
def each_identity
|
|
98
|
+
prepared_identities = prepare_identities_from_files + prepare_identities_from_data
|
|
99
|
+
|
|
100
|
+
user_identities = load_identities(prepared_identities, false)
|
|
101
|
+
|
|
102
|
+
if agent
|
|
103
|
+
agent.identities.each do |key|
|
|
104
|
+
corresponding_user_identity = user_identities.detect { |identity|
|
|
105
|
+
identity[:public_key] && identity[:public_key].to_pem == key.to_pem
|
|
106
|
+
}
|
|
107
|
+
user_identities.delete(corresponding_user_identity) if corresponding_user_identity
|
|
108
|
+
|
|
109
|
+
if !options[:keys_only] || corresponding_user_identity
|
|
110
|
+
known_identities[key] = { :from => :agent }
|
|
111
|
+
yield key
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
user_identities = load_identities(user_identities, true)
|
|
117
|
+
|
|
118
|
+
user_identities.each do |identity|
|
|
119
|
+
key = identity.delete(:public_key)
|
|
120
|
+
known_identities[key] = identity
|
|
121
|
+
yield key
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
self
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Sign the given data, using the corresponding private key of the given
|
|
128
|
+
# identity. If the identity was originally obtained from an ssh-agent,
|
|
129
|
+
# then the ssh-agent will be used to sign the data, otherwise the
|
|
130
|
+
# private key for the identity will be loaded from disk (if it hasn't
|
|
131
|
+
# been loaded already) and will then be used to sign the data.
|
|
132
|
+
#
|
|
133
|
+
# Regardless of the identity's origin or who does the signing, this
|
|
134
|
+
# will always return the signature in an SSH2-specified "signature
|
|
135
|
+
# blob" format.
|
|
136
|
+
def sign(identity, data)
|
|
137
|
+
info = known_identities[identity] or raise KeyManagerError, "the given identity is unknown to the key manager"
|
|
138
|
+
|
|
139
|
+
if info[:key].nil? && info[:from] == :file
|
|
140
|
+
begin
|
|
141
|
+
info[:key] = KeyFactory.load_private_key(info[:file], options[:passphrase], true)
|
|
142
|
+
rescue Exception, OpenSSL::OpenSSLError => e
|
|
143
|
+
raise KeyManagerError, "the given identity is known, but the private key could not be loaded: #{e.class} (#{e.message})"
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
if info[:key]
|
|
148
|
+
return Net::SSH::Buffer.from(:string, identity.ssh_type,
|
|
149
|
+
:string, info[:key].ssh_do_sign(data.to_s)).to_s
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
if info[:from] == :agent
|
|
153
|
+
raise KeyManagerError, "the agent is no longer available" unless agent
|
|
154
|
+
return agent.sign(identity, data.to_s)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
raise KeyManagerError, "[BUG] can't determine identity origin (#{info.inspect})"
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Identifies whether the ssh-agent will be used or not.
|
|
161
|
+
def use_agent?
|
|
162
|
+
@use_agent
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Toggles whether the ssh-agent will be used or not. If true, an
|
|
166
|
+
# attempt will be made to use the ssh-agent. If false, any existing
|
|
167
|
+
# connection to an agent is closed and the agent will not be used.
|
|
168
|
+
def use_agent=(use_agent)
|
|
169
|
+
finish if !use_agent
|
|
170
|
+
@use_agent = use_agent
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Returns an Agent instance to use for communicating with an SSH
|
|
174
|
+
# agent process. Returns nil if use of an SSH agent has been disabled,
|
|
175
|
+
# or if the agent is otherwise not available.
|
|
176
|
+
def agent
|
|
177
|
+
return unless use_agent?
|
|
178
|
+
@agent ||= Agent.connect(logger)
|
|
179
|
+
rescue AgentNotAvailable
|
|
180
|
+
@use_agent = false
|
|
181
|
+
nil
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
private
|
|
185
|
+
|
|
186
|
+
# Prepares identities from user key_files for loading, preserving their order and sources.
|
|
187
|
+
def prepare_identities_from_files
|
|
188
|
+
key_files.map do |file|
|
|
189
|
+
public_key_file = file + ".pub"
|
|
190
|
+
if File.readable?(public_key_file)
|
|
191
|
+
{ :load_from => :pubkey_file, :file => file }
|
|
192
|
+
elsif File.readable?(file)
|
|
193
|
+
{ :load_from => :privkey_file, :file => file }
|
|
194
|
+
end
|
|
195
|
+
end.compact
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Prepared identities from user key_data, preserving their order and sources.
|
|
199
|
+
def prepare_identities_from_data
|
|
200
|
+
key_data.map do |data|
|
|
201
|
+
{ :load_from => :data, :data => data }
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Load prepared identities. Private key decryption errors ignored if passphrase was not prompted.
|
|
206
|
+
def load_identities(identities, ask_passphrase)
|
|
207
|
+
identities.map do |identity|
|
|
208
|
+
begin
|
|
209
|
+
case identity[:load_from]
|
|
210
|
+
when :pubkey_file
|
|
211
|
+
key = KeyFactory.load_public_key(identity[:file] + ".pub")
|
|
212
|
+
{ :public_key => key, :from => :file, :file => identity[:file] }
|
|
213
|
+
when :privkey_file
|
|
214
|
+
private_key = KeyFactory.load_private_key(identity[:file], options[:passphrase], ask_passphrase)
|
|
215
|
+
key = private_key.__send__(:public_key)
|
|
216
|
+
{ :public_key => key, :from => :file, :file => identity[:file], :key => private_key }
|
|
217
|
+
when :data
|
|
218
|
+
private_key = KeyFactory.load_data_private_key(identity[:data], options[:passphrase], ask_passphrase)
|
|
219
|
+
key = private_key.__send__(:public_key)
|
|
220
|
+
{ :public_key => key, :from => :key_data, :data => identity[:data], :key => private_key }
|
|
221
|
+
else
|
|
222
|
+
identity
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
rescue OpenSSL::PKey::RSAError, OpenSSL::PKey::DSAError => e
|
|
226
|
+
if ask_passphrase
|
|
227
|
+
process_identity_loading_error(identity, e)
|
|
228
|
+
nil
|
|
229
|
+
else
|
|
230
|
+
identity
|
|
231
|
+
end
|
|
232
|
+
rescue Exception => e
|
|
233
|
+
process_identity_loading_error(identity, e)
|
|
234
|
+
nil
|
|
235
|
+
end
|
|
236
|
+
end.compact
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def process_identity_loading_error(identity, e)
|
|
240
|
+
case identity[:load_from]
|
|
241
|
+
when :pubkey_file
|
|
242
|
+
error { "could not load public key file `#{identity[:file]}': #{e.class} (#{e.message})" }
|
|
243
|
+
when :privkey_file
|
|
244
|
+
error { "could not load private key file `#{identity[:file]}': #{e.class} (#{e.message})" }
|
|
245
|
+
else
|
|
246
|
+
raise e
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require 'net/ssh/buffer'
|
|
2
|
+
require 'net/ssh/errors'
|
|
3
|
+
require 'net/ssh/loggable'
|
|
4
|
+
require 'net/ssh/authentication/constants'
|
|
5
|
+
|
|
6
|
+
module Net; module SSH; module Authentication; module Methods
|
|
7
|
+
|
|
8
|
+
# The base class of all user authentication methods. It provides a few
|
|
9
|
+
# bits of common functionality.
|
|
10
|
+
class Abstract
|
|
11
|
+
include Constants, Loggable
|
|
12
|
+
|
|
13
|
+
# The authentication session object
|
|
14
|
+
attr_reader :session
|
|
15
|
+
|
|
16
|
+
# The key manager object. Not all authentication methods will require
|
|
17
|
+
# this.
|
|
18
|
+
attr_reader :key_manager
|
|
19
|
+
|
|
20
|
+
# Instantiates a new authentication method.
|
|
21
|
+
def initialize(session, options={})
|
|
22
|
+
@session = session
|
|
23
|
+
@key_manager = options[:key_manager]
|
|
24
|
+
@options = options
|
|
25
|
+
self.logger = session.logger
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Returns the session-id, as generated during the first key exchange of
|
|
29
|
+
# an SSH connection.
|
|
30
|
+
def session_id
|
|
31
|
+
session.transport.algorithms.session_id
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Sends a message via the underlying transport layer abstraction. This
|
|
35
|
+
# will block until the message is completely sent.
|
|
36
|
+
def send_message(msg)
|
|
37
|
+
session.transport.send_message(msg)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Creates a new USERAUTH_REQUEST packet. The extra arguments on the end
|
|
41
|
+
# must be either boolean values or strings, and are tacked onto the end
|
|
42
|
+
# of the packet. The new packet is returned, ready for sending.
|
|
43
|
+
def userauth_request(username, next_service, auth_method, *others)
|
|
44
|
+
buffer = Net::SSH::Buffer.from(:byte, USERAUTH_REQUEST,
|
|
45
|
+
:string, username, :string, next_service, :string, auth_method)
|
|
46
|
+
|
|
47
|
+
others.each do |value|
|
|
48
|
+
case value
|
|
49
|
+
when true, false then buffer.write_bool(value)
|
|
50
|
+
when String then buffer.write_string(value)
|
|
51
|
+
else raise ArgumentError, "don't know how to write #{value.inspect}"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
buffer
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end; end; end; end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'net/ssh/authentication/methods/abstract'
|
|
2
|
+
|
|
3
|
+
module Net
|
|
4
|
+
module SSH
|
|
5
|
+
module Authentication
|
|
6
|
+
module Methods
|
|
7
|
+
|
|
8
|
+
# Implements the host-based SSH authentication method.
|
|
9
|
+
class Hostbased < Abstract
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
# Attempts to perform host-based authorization of the user by trying
|
|
13
|
+
# all known keys.
|
|
14
|
+
def authenticate(next_service, username, password=nil)
|
|
15
|
+
return false unless key_manager
|
|
16
|
+
|
|
17
|
+
key_manager.each_identity do |identity|
|
|
18
|
+
return true if authenticate_with(identity, next_service,
|
|
19
|
+
username, key_manager)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
return false
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
# Returns the hostname as reported by the underlying socket.
|
|
28
|
+
def hostname
|
|
29
|
+
session.transport.socket.client_name
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Attempts to perform host-based authentication of the user, using
|
|
33
|
+
# the given host identity (key).
|
|
34
|
+
def authenticate_with(identity, next_service, username, key_manager)
|
|
35
|
+
debug { "trying hostbased (#{identity.fingerprint})" }
|
|
36
|
+
client_username = ENV['USER'] || username
|
|
37
|
+
|
|
38
|
+
req = build_request(identity, next_service, username, "#{hostname}.", client_username)
|
|
39
|
+
sig_data = Buffer.from(:string, session_id, :raw, req)
|
|
40
|
+
|
|
41
|
+
sig = key_manager.sign(identity, sig_data.to_s)
|
|
42
|
+
|
|
43
|
+
message = Buffer.from(:raw, req, :string, sig)
|
|
44
|
+
|
|
45
|
+
send_message(message)
|
|
46
|
+
message = session.next_message
|
|
47
|
+
|
|
48
|
+
case message.type
|
|
49
|
+
when USERAUTH_SUCCESS
|
|
50
|
+
info { "hostbased succeeded (#{identity.fingerprint})" }
|
|
51
|
+
return true
|
|
52
|
+
when USERAUTH_FAILURE
|
|
53
|
+
info { "hostbased failed (#{identity.fingerprint})" }
|
|
54
|
+
|
|
55
|
+
raise Net::SSH::Authentication::DisallowedMethod unless
|
|
56
|
+
message[:authentications].split(/,/).include? 'hostbased'
|
|
57
|
+
|
|
58
|
+
return false
|
|
59
|
+
else
|
|
60
|
+
raise Net::SSH::Exception, "unexpected server response to USERAUTH_REQUEST: #{message.type} (#{message.inspect})"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Build the "core" hostbased request string.
|
|
65
|
+
def build_request(identity, next_service, username, hostname, client_username)
|
|
66
|
+
userauth_request(username, next_service, "hostbased", identity.ssh_type,
|
|
67
|
+
Buffer.from(:key, identity).to_s, hostname, client_username).to_s
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
require 'net/ssh/prompt'
|
|
2
|
+
require 'net/ssh/authentication/methods/abstract'
|
|
3
|
+
|
|
4
|
+
module Net
|
|
5
|
+
module SSH
|
|
6
|
+
module Authentication
|
|
7
|
+
module Methods
|
|
8
|
+
|
|
9
|
+
# Implements the "keyboard-interactive" SSH authentication method.
|
|
10
|
+
class KeyboardInteractive < Abstract
|
|
11
|
+
include Prompt
|
|
12
|
+
|
|
13
|
+
USERAUTH_INFO_REQUEST = 60
|
|
14
|
+
USERAUTH_INFO_RESPONSE = 61
|
|
15
|
+
|
|
16
|
+
# Attempt to authenticate the given user for the given service.
|
|
17
|
+
def authenticate(next_service, username, password=nil)
|
|
18
|
+
debug { "trying keyboard-interactive" }
|
|
19
|
+
send_message(userauth_request(username, next_service, "keyboard-interactive", "", ""))
|
|
20
|
+
|
|
21
|
+
loop do
|
|
22
|
+
message = session.next_message
|
|
23
|
+
|
|
24
|
+
case message.type
|
|
25
|
+
when USERAUTH_SUCCESS
|
|
26
|
+
debug { "keyboard-interactive succeeded" }
|
|
27
|
+
return true
|
|
28
|
+
when USERAUTH_FAILURE
|
|
29
|
+
debug { "keyboard-interactive failed" }
|
|
30
|
+
|
|
31
|
+
raise Net::SSH::Authentication::DisallowedMethod unless
|
|
32
|
+
message[:authentications].split(/,/).include? 'keyboard-interactive'
|
|
33
|
+
|
|
34
|
+
return false
|
|
35
|
+
when USERAUTH_INFO_REQUEST
|
|
36
|
+
name = message.read_string
|
|
37
|
+
instruction = message.read_string
|
|
38
|
+
debug { "keyboard-interactive info request" }
|
|
39
|
+
|
|
40
|
+
unless password
|
|
41
|
+
puts(name) unless name.empty?
|
|
42
|
+
puts(instruction) unless instruction.empty?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
lang_tag = message.read_string
|
|
46
|
+
responses =[]
|
|
47
|
+
|
|
48
|
+
message.read_long.times do
|
|
49
|
+
text = message.read_string
|
|
50
|
+
echo = message.read_bool
|
|
51
|
+
responses << (password || prompt(text, echo))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# if the password failed the first time around, don't try
|
|
55
|
+
# and use it on subsequent requests.
|
|
56
|
+
password = nil
|
|
57
|
+
|
|
58
|
+
msg = Buffer.from(:byte, USERAUTH_INFO_RESPONSE, :long, responses.length, :string, responses)
|
|
59
|
+
send_message(msg)
|
|
60
|
+
else
|
|
61
|
+
raise Net::SSH::Exception, "unexpected reply in keyboard interactive: #{message.type} (#{message.inspect})"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|