net-ssh 5.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.
Files changed (122) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.dockerignore +6 -0
  4. data/.github/config/rubocop_linter_action.yml +4 -0
  5. data/.github/workflows/ci-with-docker.yml +44 -0
  6. data/.github/workflows/ci.yml +87 -0
  7. data/.github/workflows/rubocop.yml +13 -0
  8. data/.gitignore +3 -0
  9. data/.rubocop.yml +16 -2
  10. data/.rubocop_todo.yml +623 -511
  11. data/CHANGES.txt +50 -2
  12. data/Dockerfile +27 -0
  13. data/Dockerfile.openssl3 +17 -0
  14. data/Gemfile +2 -0
  15. data/Gemfile.noed25519 +2 -0
  16. data/Manifest +0 -1
  17. data/README.md +293 -0
  18. data/Rakefile +6 -2
  19. data/appveyor.yml +4 -2
  20. data/docker-compose.yml +23 -0
  21. data/lib/net/ssh/authentication/agent.rb +29 -13
  22. data/lib/net/ssh/authentication/certificate.rb +19 -7
  23. data/lib/net/ssh/authentication/constants.rb +0 -1
  24. data/lib/net/ssh/authentication/ed25519.rb +13 -8
  25. data/lib/net/ssh/authentication/ed25519_loader.rb +5 -8
  26. data/lib/net/ssh/authentication/key_manager.rb +73 -32
  27. data/lib/net/ssh/authentication/methods/abstract.rb +12 -3
  28. data/lib/net/ssh/authentication/methods/hostbased.rb +3 -5
  29. data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +5 -3
  30. data/lib/net/ssh/authentication/methods/none.rb +6 -9
  31. data/lib/net/ssh/authentication/methods/password.rb +2 -3
  32. data/lib/net/ssh/authentication/methods/publickey.rb +56 -16
  33. data/lib/net/ssh/authentication/pageant.rb +97 -97
  34. data/lib/net/ssh/authentication/pub_key_fingerprint.rb +2 -3
  35. data/lib/net/ssh/authentication/session.rb +27 -23
  36. data/lib/net/ssh/buffer.rb +51 -40
  37. data/lib/net/ssh/buffered_io.rb +24 -26
  38. data/lib/net/ssh/config.rb +82 -50
  39. data/lib/net/ssh/connection/channel.rb +101 -87
  40. data/lib/net/ssh/connection/constants.rb +0 -4
  41. data/lib/net/ssh/connection/event_loop.rb +30 -25
  42. data/lib/net/ssh/connection/keepalive.rb +12 -12
  43. data/lib/net/ssh/connection/session.rb +115 -111
  44. data/lib/net/ssh/connection/term.rb +56 -58
  45. data/lib/net/ssh/errors.rb +12 -12
  46. data/lib/net/ssh/key_factory.rb +10 -13
  47. data/lib/net/ssh/known_hosts.rb +106 -39
  48. data/lib/net/ssh/loggable.rb +10 -11
  49. data/lib/net/ssh/packet.rb +1 -1
  50. data/lib/net/ssh/prompt.rb +9 -11
  51. data/lib/net/ssh/proxy/command.rb +1 -2
  52. data/lib/net/ssh/proxy/errors.rb +2 -4
  53. data/lib/net/ssh/proxy/http.rb +18 -20
  54. data/lib/net/ssh/proxy/https.rb +8 -10
  55. data/lib/net/ssh/proxy/jump.rb +8 -10
  56. data/lib/net/ssh/proxy/socks4.rb +2 -4
  57. data/lib/net/ssh/proxy/socks5.rb +3 -6
  58. data/lib/net/ssh/service/forward.rb +9 -8
  59. data/lib/net/ssh/test/channel.rb +24 -26
  60. data/lib/net/ssh/test/extensions.rb +35 -35
  61. data/lib/net/ssh/test/kex.rb +6 -8
  62. data/lib/net/ssh/test/local_packet.rb +0 -2
  63. data/lib/net/ssh/test/packet.rb +3 -3
  64. data/lib/net/ssh/test/remote_packet.rb +6 -8
  65. data/lib/net/ssh/test/script.rb +25 -27
  66. data/lib/net/ssh/test/socket.rb +12 -15
  67. data/lib/net/ssh/test.rb +7 -7
  68. data/lib/net/ssh/transport/algorithms.rb +100 -58
  69. data/lib/net/ssh/transport/cipher_factory.rb +34 -50
  70. data/lib/net/ssh/transport/constants.rb +13 -9
  71. data/lib/net/ssh/transport/ctr.rb +8 -14
  72. data/lib/net/ssh/transport/hmac/abstract.rb +20 -5
  73. data/lib/net/ssh/transport/hmac/md5.rb +0 -2
  74. data/lib/net/ssh/transport/hmac/md5_96.rb +0 -2
  75. data/lib/net/ssh/transport/hmac/none.rb +0 -2
  76. data/lib/net/ssh/transport/hmac/ripemd160.rb +0 -2
  77. data/lib/net/ssh/transport/hmac/sha1.rb +0 -2
  78. data/lib/net/ssh/transport/hmac/sha1_96.rb +0 -2
  79. data/lib/net/ssh/transport/hmac/sha2_256.rb +7 -11
  80. data/lib/net/ssh/transport/hmac/sha2_256_96.rb +4 -8
  81. data/lib/net/ssh/transport/hmac/sha2_256_etm.rb +12 -0
  82. data/lib/net/ssh/transport/hmac/sha2_512.rb +6 -9
  83. data/lib/net/ssh/transport/hmac/sha2_512_96.rb +4 -8
  84. data/lib/net/ssh/transport/hmac/sha2_512_etm.rb +12 -0
  85. data/lib/net/ssh/transport/hmac.rb +13 -11
  86. data/lib/net/ssh/transport/identity_cipher.rb +11 -13
  87. data/lib/net/ssh/transport/kex/abstract.rb +130 -0
  88. data/lib/net/ssh/transport/kex/abstract5656.rb +72 -0
  89. data/lib/net/ssh/transport/kex/curve25519_sha256.rb +39 -0
  90. data/lib/net/ssh/transport/kex/curve25519_sha256_loader.rb +30 -0
  91. data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha1.rb +5 -19
  92. data/lib/net/ssh/transport/kex/diffie_hellman_group14_sha256.rb +11 -0
  93. data/lib/net/ssh/transport/kex/diffie_hellman_group1_sha1.rb +30 -139
  94. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +1 -8
  95. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha256.rb +5 -9
  96. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp256.rb +20 -81
  97. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +5 -4
  98. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +5 -4
  99. data/lib/net/ssh/transport/kex.rb +15 -10
  100. data/lib/net/ssh/transport/key_expander.rb +7 -8
  101. data/lib/net/ssh/transport/openssl.rb +149 -127
  102. data/lib/net/ssh/transport/packet_stream.rb +50 -16
  103. data/lib/net/ssh/transport/server_version.rb +17 -16
  104. data/lib/net/ssh/transport/session.rb +9 -7
  105. data/lib/net/ssh/transport/state.rb +44 -44
  106. data/lib/net/ssh/verifiers/accept_new.rb +0 -2
  107. data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +1 -2
  108. data/lib/net/ssh/verifiers/always.rb +6 -4
  109. data/lib/net/ssh/verifiers/never.rb +0 -2
  110. data/lib/net/ssh/version.rb +3 -3
  111. data/lib/net/ssh.rb +12 -8
  112. data/net-ssh-public_cert.pem +8 -8
  113. data/net-ssh.gemspec +9 -7
  114. data/support/ssh_tunnel_bug.rb +3 -3
  115. data.tar.gz.sig +0 -0
  116. metadata +55 -30
  117. metadata.gz.sig +0 -0
  118. data/.travis.yml +0 -53
  119. data/Gemfile.noed25519.lock +0 -41
  120. data/README.rdoc +0 -194
  121. data/lib/net/ssh/ruby_compat.rb +0 -13
  122. data/support/arcfour_check.rb +0 -20
@@ -1,7 +1,6 @@
1
- module Net
2
- module SSH
1
+ module Net
2
+ module SSH
3
3
  module Connection
4
-
5
4
  # These constants are used when requesting a pseudo-terminal (via
6
5
  # Net::SSH::Connection::Channel#request_pty). The descriptions for each are
7
6
  # taken directly from RFC 4254 ("The Secure Shell (SSH) Connection Protocol"),
@@ -10,173 +9,172 @@ module Net
10
9
  # Interrupt character; 255 if none. Similarly for the other characters.
11
10
  # Not all of these characters are supported on all systems.
12
11
  VINTR = 1
13
-
12
+
14
13
  # The quit character (sends SIGQUIT signal on POSIX systems).
15
14
  VQUIT = 2
16
-
15
+
17
16
  # Erase the character to left of the cursor.
18
17
  VERASE = 3
19
-
18
+
20
19
  # Kill the current input line.
21
20
  VKILL = 4
22
-
21
+
23
22
  # End-of-file character (sends EOF from the terminal).
24
23
  VEOF = 5
25
-
24
+
26
25
  # End-of-line character in addition to carriage return and/or linefeed.
27
26
  VEOL = 6
28
-
27
+
29
28
  # Additional end-of-line character.
30
29
  VEOL2 = 7
31
-
30
+
32
31
  # Continues paused output (normally control-Q).
33
32
  VSTART = 8
34
-
33
+
35
34
  # Pauses output (normally control-S).
36
35
  VSTOP = 9
37
-
36
+
38
37
  # Suspends the current program.
39
38
  VSUSP = 10
40
-
39
+
41
40
  # Another suspend character.
42
41
  VDSUSP = 11
43
-
42
+
44
43
  # Reprints the current input line.
45
44
  VREPRINT = 12
46
-
45
+
47
46
  # Erases a word left of cursor.
48
47
  VWERASE = 13
49
-
48
+
50
49
  # Enter the next character typed literally, even if it is a special
51
50
  # character.
52
51
  VLNEXT = 14
53
-
52
+
54
53
  # Character to flush output.
55
54
  VFLUSH = 15
56
-
55
+
57
56
  # Switch to a different shell layer.
58
57
  VSWITCH = 16
59
-
58
+
60
59
  # Prints system status line (load, command, pid, etc).
61
60
  VSTATUS = 17
62
-
61
+
63
62
  # Toggles the flushing of terminal output.
64
63
  VDISCARD = 18
65
-
64
+
66
65
  # The ignore parity flag. The parameter SHOULD be 0 if this flag is FALSE,
67
66
  # and 1 if it is TRUE.
68
67
  IGNPAR = 30
69
-
68
+
70
69
  # Mark parity and framing errors.
71
70
  PARMRK = 31
72
-
71
+
73
72
  # Enable checking of parity errors.
74
73
  INPCK = 32
75
-
74
+
76
75
  # Strip 8th bit off characters.
77
76
  ISTRIP = 33
78
-
77
+
79
78
  # Map NL into CR on input.
80
79
  INCLR = 34
81
-
80
+
82
81
  # Ignore CR on input.
83
82
  IGNCR = 35
84
-
83
+
85
84
  # Map CR to NL on input.
86
85
  ICRNL = 36
87
-
86
+
88
87
  # Translate uppercase characters to lowercase.
89
88
  IUCLC = 37
90
-
89
+
91
90
  # Enable output flow control.
92
91
  IXON = 38
93
-
92
+
94
93
  # Any char will restart after stop.
95
94
  IXANY = 39
96
-
95
+
97
96
  # Enable input flow control.
98
97
  IXOFF = 40
99
-
98
+
100
99
  # Ring bell on input queue full.
101
100
  IMAXBEL = 41
102
-
101
+
103
102
  # Enable signals INTR, QUIT, [D]SUSP.
104
103
  ISIG = 50
105
-
104
+
106
105
  # Canonicalize input lines.
107
106
  ICANON = 51
108
-
107
+
109
108
  # Enable input and output of uppercase characters by preceding their
110
109
  # lowercase equivalents with "\".
111
110
  XCASE = 52
112
-
111
+
113
112
  # Enable echoing.
114
113
  ECHO = 53
115
-
114
+
116
115
  # Visually erase chars.
117
116
  ECHOE = 54
118
-
117
+
119
118
  # Kill character discards current line.
120
119
  ECHOK = 55
121
-
120
+
122
121
  # Echo NL even if ECHO is off.
123
122
  ECHONL = 56
124
-
123
+
125
124
  # Don't flush after interrupt.
126
125
  NOFLSH = 57
127
-
126
+
128
127
  # Stop background jobs from output.
129
128
  TOSTOP = 58
130
-
129
+
131
130
  # Enable extensions.
132
131
  IEXTEN = 59
133
-
132
+
134
133
  # Echo control characters as ^(Char).
135
134
  ECHOCTL = 60
136
-
135
+
137
136
  # Visual erase for line kill.
138
137
  ECHOKE = 61
139
-
138
+
140
139
  # Retype pending input.
141
140
  PENDIN = 62
142
-
141
+
143
142
  # Enable output processing.
144
143
  OPOST = 70
145
-
144
+
146
145
  # Convert lowercase to uppercase.
147
146
  OLCUC = 71
148
-
147
+
149
148
  # Map NL to CR-NL.
150
149
  ONLCR = 72
151
-
150
+
152
151
  # Translate carriage return to newline (output).
153
152
  OCRNL = 73
154
-
153
+
155
154
  # Translate newline to carriage return-newline (output).
156
155
  ONOCR = 74
157
-
156
+
158
157
  # Newline performs a carriage return (output).
159
158
  ONLRET = 75
160
-
159
+
161
160
  # 7 bit mode.
162
161
  CS7 = 90
163
-
162
+
164
163
  # 8 bit mode.
165
164
  CS8 = 91
166
-
165
+
167
166
  # Parity enable.
168
167
  PARENB = 92
169
-
168
+
170
169
  # Odd parity, else even.
171
170
  PARODD = 93
172
-
171
+
173
172
  # Specifies the input baud rate in bits per second.
174
173
  TTY_OP_ISPEED = 128
175
-
174
+
176
175
  # Specifies the output baud rate in bits per second.
177
176
  TTY_OP_OSPEED = 129
178
177
  end
179
-
180
178
  end
181
179
  end
182
180
  end
@@ -1,4 +1,4 @@
1
- module Net
1
+ module Net
2
2
  module SSH
3
3
  # A general exception class, to act as the ancestor of all other Net::SSH
4
4
  # exception classes.
@@ -33,7 +33,7 @@ module Net
33
33
  # a "channel open failed" message.
34
34
  class ChannelOpenFailed < Net::SSH::Exception
35
35
  attr_reader :code, :reason
36
-
36
+
37
37
  def initialize(code, reason)
38
38
  @code, @reason = code, reason
39
39
  super "#{reason} (#{code})"
@@ -45,43 +45,43 @@ module Net
45
45
  # the remember_host! method on the exception, and then retry.
46
46
  class HostKeyError < Net::SSH::Exception
47
47
  # the callback to use when #remember_host! is called
48
- attr_writer :callback #:nodoc:
49
-
48
+ attr_writer :callback # :nodoc:
49
+
50
50
  # situation-specific data describing the host (see #host, #port, etc.)
51
- attr_writer :data #:nodoc:
52
-
51
+ attr_writer :data # :nodoc:
52
+
53
53
  # An accessor for getting at the data that was used to look up the host
54
54
  # (see also #fingerprint, #host, #port, #ip, and #key).
55
55
  def [](key)
56
56
  @data && @data[key]
57
57
  end
58
-
58
+
59
59
  # Returns the fingerprint of the key for the host, which either was not
60
60
  # found or did not match.
61
61
  def fingerprint
62
62
  @data && @data[:fingerprint]
63
63
  end
64
-
64
+
65
65
  # Returns the host name for the remote host, as reported by the socket.
66
66
  def host
67
67
  @data && @data[:peer] && @data[:peer][:host]
68
68
  end
69
-
69
+
70
70
  # Returns the port number for the remote host, as reported by the socket.
71
71
  def port
72
72
  @data && @data[:peer] && @data[:peer][:port]
73
73
  end
74
-
74
+
75
75
  # Returns the IP address of the remote host, as reported by the socket.
76
76
  def ip
77
77
  @data && @data[:peer] && @data[:peer][:ip]
78
78
  end
79
-
79
+
80
80
  # Returns the key itself, as reported by the remote host.
81
81
  def key
82
82
  @data && @data[:key]
83
83
  end
84
-
84
+
85
85
  # Tell Net::SSH to record this host and key in the known hosts file, so
86
86
  # that subsequent connections will remember them.
87
87
  def remember_host!
@@ -5,7 +5,6 @@ require 'net/ssh/authentication/ed25519_loader'
5
5
 
6
6
  module Net
7
7
  module SSH
8
-
9
8
  # A factory class for returning new Key classes. It is used for obtaining
10
9
  # OpenSSL key instances via their SSH names, and for loading both public and
11
10
  # private keys. It used used primarily by Net::SSH itself, internally, and
@@ -18,16 +17,14 @@ module Net
18
17
  class KeyFactory
19
18
  # Specifies the mapping of SSH names to OpenSSL key classes.
20
19
  MAP = {
21
- "dh" => OpenSSL::PKey::DH,
22
- "rsa" => OpenSSL::PKey::RSA,
23
- "dsa" => OpenSSL::PKey::DSA
20
+ 'dh' => OpenSSL::PKey::DH,
21
+ 'rsa' => OpenSSL::PKey::RSA,
22
+ 'dsa' => OpenSSL::PKey::DSA,
23
+ 'ecdsa' => OpenSSL::PKey::EC
24
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
28
- end
25
+ MAP["ed25519"] = Net::SSH::Authentication::ED25519::PrivKey if defined? Net::SSH::Authentication::ED25519
29
26
 
30
- class <<self
27
+ class << self
31
28
  # Fetch an OpenSSL key instance by its SSH name. It will be a new,
32
29
  # empty key of the given type.
33
30
  def get(name)
@@ -39,7 +36,7 @@ module Net
39
36
  # appropriately. The new key is returned. If the key itself is
40
37
  # encrypted (requiring a passphrase to use), the user will be
41
38
  # prompted to enter their password unless passphrase works.
42
- def load_private_key(filename, passphrase=nil, ask_passphrase=true, prompt=Prompt.default)
39
+ def load_private_key(filename, passphrase = nil, ask_passphrase = true, prompt = Prompt.default)
43
40
  data = File.read(File.expand_path(filename))
44
41
  load_data_private_key(data, passphrase, ask_passphrase, filename, prompt)
45
42
  end
@@ -49,7 +46,7 @@ module Net
49
46
  # appropriately. The new key is returned. If the key itself is
50
47
  # encrypted (requiring a passphrase to use), the user will be
51
48
  # prompted to enter their password unless passphrase works.
52
- def load_data_private_key(data, passphrase=nil, ask_passphrase=true, filename="", prompt=Prompt.default)
49
+ def load_data_private_key(data, passphrase = nil, ask_passphrase = true, filename = "", prompt = Prompt.default)
53
50
  key_type = classify_key(data, filename)
54
51
 
55
52
  encrypted_key = nil
@@ -89,7 +86,7 @@ module Net
89
86
  # Loads a public key. It will correctly determine whether
90
87
  # the file describes an RSA or DSA key, and will load it
91
88
  # appropriately. The new public key is returned.
92
- def load_data_public_key(data, filename="")
89
+ def load_data_public_key(data, filename = "")
93
90
  fields = data.split(/ /)
94
91
 
95
92
  blob = nil
@@ -207,7 +204,7 @@ module Net
207
204
  return OpenSSLDSAKeyType
208
205
  elsif data.match(/-----BEGIN RSA PRIVATE KEY-----/)
209
206
  return OpenSSLRSAKeyType
210
- elsif data.match(/-----BEGIN EC PRIVATE KEY-----/) && defined?(OpenSSL::PKey::EC)
207
+ elsif data.match(/-----BEGIN EC PRIVATE KEY-----/)
211
208
  return OpenSSLECKeyType
212
209
  elsif data.match(/-----BEGIN (.+) PRIVATE KEY-----/)
213
210
  raise OpenSSL::PKey::PKeyError, "not a supported key type '#{$1}'"
@@ -1,11 +1,69 @@
1
1
  require 'strscan'
2
2
  require 'openssl'
3
3
  require 'base64'
4
+ require 'delegate'
4
5
  require 'net/ssh/buffer'
5
6
  require 'net/ssh/authentication/ed25519_loader'
6
7
 
7
8
  module Net
8
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
17
+
18
+ def ssh_type
19
+ @key.ssh_type
20
+ end
21
+
22
+ def ssh_types
23
+ [ssh_type]
24
+ end
25
+
26
+ def to_blob
27
+ @key.to_blob
28
+ end
29
+
30
+ def __getobj__
31
+ Kernel.warn("Calling Net::SSH::Buffer methods on HostKeyEntries PubKey is deprecated")
32
+ @key
33
+ end
34
+
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
52
+
53
+ def initialize(key, comment: nil)
54
+ @key = key
55
+ @comment = comment
56
+ end
57
+
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
66
+ end
9
67
 
10
68
  # Represents the result of a search in known hosts
11
69
  # see search_for
@@ -41,20 +99,17 @@ module Net
41
99
  # This is used internally by Net::SSH, and will never need to be used directly
42
100
  # by consumers of the library.
43
101
  class KnownHosts
44
- if defined?(OpenSSL::PKey::EC)
45
- SUPPORTED_TYPE = %w[ssh-rsa ssh-dss
46
- ecdsa-sha2-nistp256
47
- ecdsa-sha2-nistp384
48
- ecdsa-sha2-nistp521]
49
- else
50
- SUPPORTED_TYPE = %w[ssh-rsa ssh-dss]
51
- end
102
+ SUPPORTED_TYPE = %w[ssh-rsa ssh-dss
103
+ ecdsa-sha2-nistp256
104
+ ecdsa-sha2-nistp384
105
+ ecdsa-sha2-nistp521]
106
+
52
107
  SUPPORTED_TYPE.push('ssh-ed25519') if Net::SSH::Authentication::ED25519Loader::LOADED
53
108
 
54
- class <<self
109
+ class << self
55
110
  # Searches all known host files (see KnownHosts.hostfiles) for all keys
56
111
  # of the given host. Returns an enumerable of keys found.
57
- def search_for(host, options={})
112
+ def search_for(host, options = {})
58
113
  HostKeys.new(search_in(hostfiles(options), host, options), host, self, options)
59
114
  end
60
115
 
@@ -73,12 +128,14 @@ module Net
73
128
  #
74
129
  # If you only want the user known host files, you can pass :user as
75
130
  # the second option.
76
- def hostfiles(options, which=:all)
131
+ def hostfiles(options, which = :all)
77
132
  files = []
78
133
 
79
134
  files += Array(options[:user_known_hosts_file] || %w[~/.ssh/known_hosts ~/.ssh/known_hosts2]) if which == :all || which == :user
80
135
 
81
- files += Array(options[:global_known_hosts_file] || %w[/etc/ssh/ssh_known_hosts /etc/ssh/ssh_known_hosts2]) if which == :all || which == :global
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
82
139
 
83
140
  return files
84
141
  end
@@ -86,14 +143,12 @@ module Net
86
143
  # Looks in all user known host files (see KnownHosts.hostfiles) and tries to
87
144
  # add an entry for the given host and key to the first file it is able
88
145
  # to.
89
- def add(host, key, options={})
146
+ def add(host, key, options = {})
90
147
  hostfiles(options, :user).each do |file|
91
- begin
92
- KnownHosts.new(file).add(host, key)
93
- return
94
- rescue SystemCallError
95
- # try the next hostfile
96
- end
148
+ KnownHosts.new(file).add(host, key)
149
+ return
150
+ rescue SystemCallError
151
+ # try the next hostfile
97
152
  end
98
153
  end
99
154
  end
@@ -130,28 +185,36 @@ module Net
130
185
  host_ip = entries[1]
131
186
 
132
187
  File.open(source) do |file|
133
- scanner = StringScanner.new("")
134
188
  file.each_line do |line|
135
- scanner.string = 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
195
+
196
+ # Skip empty line or one that is commented
197
+ next if hosts.nil? || hosts.start_with?('#')
136
198
 
137
- scanner.skip(/\s*/)
138
- next if scanner.match?(/$|#/)
199
+ hostlist = hosts.split(',')
200
+
201
+ next unless SUPPORTED_TYPE.include?(type)
139
202
 
140
- hostlist = scanner.scan(/\S+/).split(/,/)
141
203
  found = hostlist.any? { |pattern| match(host_name, pattern) } || known_host_hash?(hostlist, entries)
142
204
  next unless found
143
205
 
144
206
  found = hostlist.include?(host_ip) if options[:check_host_ip] && entries.size > 1 && hostlist.size > 1
145
207
  next unless found
146
208
 
147
- scanner.skip(/\s*/)
148
- type = scanner.scan(/\S+/)
209
+ blob = key_content.unpack("m*").first
210
+ raw_key = Net::SSH::Buffer.new(blob).read_key
149
211
 
150
- next unless SUPPORTED_TYPE.include?(type)
151
-
152
- scanner.skip(/\s*/)
153
- blob = scanner.rest.unpack("m*").first
154
- keys << Net::SSH::Buffer.new(blob).read_key
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
155
218
  end
156
219
  end
157
220
 
@@ -159,14 +222,18 @@ module Net
159
222
  end
160
223
 
161
224
  def match(host, pattern)
162
- # see man 8 sshd for pattern details
163
- pattern_regexp = pattern.split('*').map do |x|
164
- x.split('?').map do |y|
165
- Regexp.escape(y)
166
- end.join('.')
167
- end.join('[^.]*')
168
-
169
- host =~ Regexp.new("\\A#{pattern_regexp}\\z")
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('.*')
232
+
233
+ host =~ Regexp.new("\\A#{pattern_regexp}\\z")
234
+ else
235
+ host == pattern
236
+ end
170
237
  end
171
238
 
172
239
  # Indicates whether one of the entries matches an hostname that has been
@@ -1,6 +1,5 @@
1
- module Net
1
+ module Net
2
2
  module SSH
3
-
4
3
  # A simple module to make logging easier to deal with. It assumes that the
5
4
  # logger instance (if not nil) quacks like a Logger object (in Ruby's
6
5
  # standard library). Although used primarily internally by Net::SSH, it
@@ -19,45 +18,45 @@ module Net
19
18
  # The logger instance that will be used to log messages. If nil, nothing
20
19
  # will be logged.
21
20
  attr_accessor :logger
22
-
21
+
23
22
  # Displays the result of yielding if the log level is Logger::DEBUG or
24
23
  # greater.
25
24
  def debug
26
25
  logger.add(Logger::DEBUG, nil, facility) { yield } if logger && logger.debug?
27
26
  end
28
-
27
+
29
28
  # Displays the result of yielding if the log level is Logger::INFO or
30
29
  # greater.
31
30
  def info
32
31
  logger.add(Logger::INFO, nil, facility) { yield } if logger && logger.info?
33
32
  end
34
-
33
+
35
34
  # Displays the result of yielding if the log level is Logger::WARN or
36
35
  # greater. (Called lwarn to avoid shadowing with Kernel#warn.)
37
36
  def lwarn
38
37
  logger.add(Logger::WARN, nil, facility) { yield } if logger && logger.warn?
39
38
  end
40
-
39
+
41
40
  # Displays the result of yielding if the log level is Logger:ERROR or
42
41
  # greater.
43
42
  def error
44
43
  logger.add(Logger::ERROR, nil, facility) { yield } if logger && logger.error?
45
44
  end
46
-
45
+
47
46
  # Displays the result of yielding if the log level is Logger::FATAL or
48
47
  # greater.
49
48
  def fatal
50
49
  logger.add(Logger::FATAL, nil, facility) { yield } if logger && logger.fatal?
51
50
  end
52
-
51
+
53
52
  private
54
-
53
+
55
54
  # Sets the "facility" value, used for reporting where a log message
56
55
  # originates. It defaults to the name of class with the object_id
57
56
  # appended.
58
57
  def facility
59
- @facility ||= self.class.name.gsub(/::/, ".").gsub(/([a-z])([A-Z])/, "\\1_\\2").downcase + "[%x]" % object_id
58
+ @facility ||= self.class.to_s.gsub(/::/, ".").gsub(/([a-z])([A-Z])/, "\\1_\\2").downcase + "[%x]" % object_id
60
59
  end
61
60
  end
62
61
  end
63
- end
62
+ end
@@ -5,7 +5,6 @@ require 'net/ssh/connection/constants'
5
5
 
6
6
  module Net
7
7
  module SSH
8
-
9
8
  # A specialization of Buffer that knows the format of certain common
10
9
  # packet types. It auto-parses those packet types, and allows them to
11
10
  # be accessed via the #[] accessor.
@@ -85,6 +84,7 @@ module Net
85
84
  def [](name)
86
85
  name = name.to_sym
87
86
  raise ArgumentError, "no such element #{name}" unless @named_elements.key?(name)
87
+
88
88
  @named_elements[name]
89
89
  end
90
90