net-ssh 4.2.0 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) 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 +7 -0
  9. data/.rubocop.yml +19 -2
  10. data/.rubocop_todo.yml +619 -667
  11. data/CHANGES.txt +110 -1
  12. data/Dockerfile +27 -0
  13. data/Dockerfile.openssl3 +17 -0
  14. data/Gemfile +3 -7
  15. data/{Gemfile.norbnacl → Gemfile.noed25519} +3 -1
  16. data/Manifest +4 -5
  17. data/README.md +293 -0
  18. data/Rakefile +45 -29
  19. data/appveyor.yml +8 -6
  20. data/docker-compose.yml +23 -0
  21. data/lib/net/ssh/authentication/agent.rb +248 -223
  22. data/lib/net/ssh/authentication/certificate.rb +178 -164
  23. data/lib/net/ssh/authentication/constants.rb +17 -15
  24. data/lib/net/ssh/authentication/ed25519.rb +141 -116
  25. data/lib/net/ssh/authentication/ed25519_loader.rb +28 -28
  26. data/lib/net/ssh/authentication/key_manager.rb +79 -36
  27. data/lib/net/ssh/authentication/methods/abstract.rb +62 -47
  28. data/lib/net/ssh/authentication/methods/hostbased.rb +34 -37
  29. data/lib/net/ssh/authentication/methods/keyboard_interactive.rb +3 -3
  30. data/lib/net/ssh/authentication/methods/none.rb +16 -19
  31. data/lib/net/ssh/authentication/methods/password.rb +15 -16
  32. data/lib/net/ssh/authentication/methods/publickey.rb +96 -55
  33. data/lib/net/ssh/authentication/pageant.rb +468 -465
  34. data/lib/net/ssh/authentication/pub_key_fingerprint.rb +43 -0
  35. data/lib/net/ssh/authentication/session.rb +131 -122
  36. data/lib/net/ssh/buffer.rb +385 -332
  37. data/lib/net/ssh/buffered_io.rb +150 -151
  38. data/lib/net/ssh/config.rb +316 -239
  39. data/lib/net/ssh/connection/channel.rb +635 -613
  40. data/lib/net/ssh/connection/constants.rb +29 -29
  41. data/lib/net/ssh/connection/event_loop.rb +104 -95
  42. data/lib/net/ssh/connection/keepalive.rb +55 -51
  43. data/lib/net/ssh/connection/session.rb +614 -611
  44. data/lib/net/ssh/connection/term.rb +125 -123
  45. data/lib/net/ssh/errors.rb +101 -99
  46. data/lib/net/ssh/key_factory.rb +194 -108
  47. data/lib/net/ssh/known_hosts.rb +212 -134
  48. data/lib/net/ssh/loggable.rb +50 -49
  49. data/lib/net/ssh/packet.rb +83 -79
  50. data/lib/net/ssh/prompt.rb +51 -51
  51. data/lib/net/ssh/proxy/command.rb +105 -91
  52. data/lib/net/ssh/proxy/errors.rb +12 -10
  53. data/lib/net/ssh/proxy/http.rb +81 -81
  54. data/lib/net/ssh/proxy/https.rb +37 -36
  55. data/lib/net/ssh/proxy/jump.rb +49 -48
  56. data/lib/net/ssh/proxy/socks4.rb +2 -6
  57. data/lib/net/ssh/proxy/socks5.rb +14 -17
  58. data/lib/net/ssh/service/forward.rb +365 -362
  59. data/lib/net/ssh/test/channel.rb +145 -143
  60. data/lib/net/ssh/test/extensions.rb +131 -127
  61. data/lib/net/ssh/test/kex.rb +34 -32
  62. data/lib/net/ssh/test/local_packet.rb +46 -44
  63. data/lib/net/ssh/test/packet.rb +87 -84
  64. data/lib/net/ssh/test/remote_packet.rb +32 -30
  65. data/lib/net/ssh/test/script.rb +155 -155
  66. data/lib/net/ssh/test/socket.rb +49 -48
  67. data/lib/net/ssh/test.rb +82 -80
  68. data/lib/net/ssh/transport/algorithms.rb +433 -364
  69. data/lib/net/ssh/transport/cipher_factory.rb +95 -91
  70. data/lib/net/ssh/transport/constants.rb +32 -24
  71. data/lib/net/ssh/transport/ctr.rb +37 -15
  72. data/lib/net/ssh/transport/hmac/abstract.rb +81 -63
  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 +14 -12
  86. data/lib/net/ssh/transport/identity_cipher.rb +54 -52
  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 +33 -40
  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 +112 -217
  94. data/lib/net/ssh/transport/kex/diffie_hellman_group_exchange_sha1.rb +53 -63
  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 +36 -90
  97. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp384.rb +18 -10
  98. data/lib/net/ssh/transport/kex/ecdh_sha2_nistp521.rb +18 -10
  99. data/lib/net/ssh/transport/kex.rb +15 -12
  100. data/lib/net/ssh/transport/key_expander.rb +24 -21
  101. data/lib/net/ssh/transport/openssl.rb +158 -133
  102. data/lib/net/ssh/transport/packet_stream.rb +223 -191
  103. data/lib/net/ssh/transport/server_version.rb +55 -56
  104. data/lib/net/ssh/transport/session.rb +306 -259
  105. data/lib/net/ssh/transport/state.rb +178 -176
  106. data/lib/net/ssh/verifiers/accept_new.rb +33 -0
  107. data/lib/net/ssh/verifiers/accept_new_or_local_tunnel.rb +33 -0
  108. data/lib/net/ssh/verifiers/always.rb +58 -0
  109. data/lib/net/ssh/verifiers/never.rb +19 -0
  110. data/lib/net/ssh/version.rb +55 -53
  111. data/lib/net/ssh.rb +47 -34
  112. data/net-ssh-public_cert.pem +18 -19
  113. data/net-ssh.gemspec +12 -11
  114. data/support/ssh_tunnel_bug.rb +5 -5
  115. data.tar.gz.sig +0 -0
  116. metadata +78 -73
  117. metadata.gz.sig +0 -0
  118. data/.travis.yml +0 -51
  119. data/Gemfile.norbnacl.lock +0 -41
  120. data/README.rdoc +0 -169
  121. data/lib/net/ssh/ruby_compat.rb +0 -24
  122. data/lib/net/ssh/verifiers/lenient.rb +0 -30
  123. data/lib/net/ssh/verifiers/null.rb +0 -12
  124. data/lib/net/ssh/verifiers/secure.rb +0 -52
  125. data/lib/net/ssh/verifiers/strict.rb +0 -24
  126. data/support/arcfour_check.rb +0 -20
@@ -21,474 +21,477 @@ end
21
21
 
22
22
  require 'net/ssh/errors'
23
23
 
24
- module Net; module SSH; module Authentication
25
-
26
- # This module encapsulates the implementation of a socket factory that
27
- # uses the PuTTY "pageant" utility to obtain information about SSH
28
- # identities.
29
- #
30
- # This code is a slightly modified version of the original implementation
31
- # by Guillaume Marçais (guillaume.marcais@free.fr). It is used and
32
- # relicensed by permission.
33
- module Pageant
34
-
35
- # From Putty pageant.c
36
- AGENT_MAX_MSGLEN = 8192
37
- AGENT_COPYDATA_ID = 0x804e50ba
38
-
39
- # The definition of the Windows methods and data structures used in
40
- # communicating with the pageant process.
41
- module Win # rubocop:disable Metrics/ModuleLength
42
- # Compatibility on initialization
43
- if RUBY_VERSION < "1.9"
44
- extend DL::Importable
45
-
46
- dlload 'user32'
47
- dlload 'kernel32'
48
- dlload 'advapi32'
49
-
50
- SIZEOF_DWORD = DL.sizeof('L')
51
- elsif RUBY_VERSION < "2.1"
52
- extend DL::Importer
53
- dlload 'user32','kernel32', 'advapi32'
54
- include DL::Win32Types
55
-
56
- SIZEOF_DWORD = DL::SIZEOF_LONG
57
- else
58
- extend Fiddle::Importer
59
- dlload 'user32','kernel32', 'advapi32'
60
- include Fiddle::Win32Types
61
- SIZEOF_DWORD = Fiddle::SIZEOF_LONG
62
- end
63
-
64
- if RUBY_ENGINE=="jruby"
65
- typealias("HANDLE", "void *") # From winnt.h
66
- typealias("PHANDLE", "void *") # From winnt.h
67
- typealias("ULONG_PTR", "unsigned long*")
68
- end
69
- typealias("LPCTSTR", "char *") # From winnt.h
70
- typealias("LPVOID", "void *") # From winnt.h
71
- typealias("LPCVOID", "const void *") # From windef.h
72
- typealias("LRESULT", "long") # From windef.h
73
- typealias("WPARAM", "unsigned int *") # From windef.h
74
- typealias("LPARAM", "long *") # From windef.h
75
- typealias("PDWORD_PTR", "long *") # From basetsd.h
76
- typealias("USHORT", "unsigned short") # From windef.h
77
-
78
- # From winbase.h, winnt.h
79
- INVALID_HANDLE_VALUE = -1
80
- NULL = nil
81
- PAGE_READWRITE = 0x0004
82
- FILE_MAP_WRITE = 2
83
- WM_COPYDATA = 74
84
-
85
- SMTO_NORMAL = 0 # From winuser.h
86
-
87
- SUFFIX = if RUBY_ENGINE == "jruby"
88
- "A"
89
- else
90
- ""
91
- end
92
-
93
- # args: lpClassName, lpWindowName
94
- extern "HWND FindWindow#{SUFFIX}(LPCTSTR, LPCTSTR)"
95
-
96
- # args: none
97
- extern 'DWORD GetCurrentThreadId()'
98
-
99
- # args: hFile, (ignored), flProtect, dwMaximumSizeHigh,
100
- # dwMaximumSizeLow, lpName
101
- extern "HANDLE CreateFileMapping#{SUFFIX}(HANDLE, void *, DWORD, " +
102
- "DWORD, DWORD, LPCTSTR)"
103
-
104
- # args: hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh,
105
- # dwfileOffsetLow, dwNumberOfBytesToMap
106
- extern 'LPVOID MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, DWORD)'
107
-
108
- # args: lpBaseAddress
109
- extern 'BOOL UnmapViewOfFile(LPCVOID)'
110
-
111
- # args: hObject
112
- extern 'BOOL CloseHandle(HANDLE)'
113
-
114
- # args: hWnd, Msg, wParam, lParam, fuFlags, uTimeout, lpdwResult
115
- extern "LRESULT SendMessageTimeout#{SUFFIX}(HWND, UINT, WPARAM, LPARAM, " +
116
- "UINT, UINT, PDWORD_PTR)"
117
-
118
- # args: none
119
- extern 'DWORD GetLastError()'
120
-
121
- # args: none
122
- extern 'HANDLE GetCurrentProcess()'
123
-
124
- # args: hProcessHandle, dwDesiredAccess, (out) phNewTokenHandle
125
- extern 'BOOL OpenProcessToken(HANDLE, DWORD, PHANDLE)'
126
-
127
- # args: hTokenHandle, uTokenInformationClass,
128
- # (out) lpTokenInformation, dwTokenInformationLength
129
- # (out) pdwInfoReturnLength
130
- extern 'BOOL GetTokenInformation(HANDLE, UINT, LPVOID, DWORD, ' +
131
- 'PDWORD)'
132
-
133
- # args: (out) lpSecurityDescriptor, dwRevisionLevel
134
- extern 'BOOL InitializeSecurityDescriptor(LPVOID, DWORD)'
135
-
136
- # args: (out) lpSecurityDescriptor, lpOwnerSid, bOwnerDefaulted
137
- extern 'BOOL SetSecurityDescriptorOwner(LPVOID, LPVOID, BOOL)'
138
-
139
- # args: pSecurityDescriptor
140
- extern 'BOOL IsValidSecurityDescriptor(LPVOID)'
141
-
142
- # Constants needed for security attribute retrieval.
143
- # Specifies the access mask corresponding to the desired access
144
- # rights.
145
- TOKEN_QUERY = 0x8
146
-
147
- # The value of TOKEN_USER from the TOKEN_INFORMATION_CLASS enum.
148
- TOKEN_USER_INFORMATION_CLASS = 1
149
-
150
- # The initial revision level assigned to the security descriptor.
151
- REVISION = 1
152
-
153
- # Structs for security attribute functions.
154
- # Holds the retrieved user access token.
155
- TOKEN_USER = struct ['void * SID', 'DWORD ATTRIBUTES']
156
-
157
- # Contains the security descriptor, this gets passed to the
158
- # function that constructs the shared memory map.
159
- SECURITY_ATTRIBUTES = struct ['DWORD nLength',
160
- 'LPVOID lpSecurityDescriptor',
161
- 'BOOL bInheritHandle']
162
-
163
- # The security descriptor holds security information.
164
- SECURITY_DESCRIPTOR = struct ['UCHAR Revision', 'UCHAR Sbz1',
165
- 'USHORT Control', 'LPVOID Owner',
166
- 'LPVOID Group', 'LPVOID Sacl',
167
- 'LPVOID Dacl']
168
-
169
- # The COPYDATASTRUCT is used to send WM_COPYDATA messages
170
- COPYDATASTRUCT = if RUBY_ENGINE == "jruby"
171
- struct ['ULONG_PTR dwData', 'DWORD cbData', 'LPVOID lpData']
172
- else
173
- struct ['uintptr_t dwData', 'DWORD cbData', 'LPVOID lpData']
174
- end
175
-
176
- # Compatibility for security attribute retrieval.
177
- if RUBY_VERSION < "1.9"
178
- # Alias functions to > 1.9 capitalization
179
- %w(findWindow
180
- getCurrentProcess
181
- initializeSecurityDescriptor
182
- setSecurityDescriptorOwner
183
- isValidSecurityDescriptor
184
- openProcessToken
185
- getTokenInformation
186
- getLastError
187
- getCurrentThreadId
188
- createFileMapping
189
- mapViewOfFile
190
- sendMessageTimeout
191
- unmapViewOfFile
192
- closeHandle).each do |name|
193
- new_name = name[0].chr.upcase + name[1..name.length]
194
- alias_method new_name, name
195
- module_function new_name
196
- end
197
-
198
- def self.malloc_ptr(size)
199
- return DL.malloc(size)
200
- end
201
-
202
- def self.get_ptr(data)
203
- return data.to_ptr
204
- end
205
-
206
- def self.set_ptr_data(ptr, data)
207
- ptr[0] = data
208
- end
209
- elsif RUBY_ENGINE == "jruby"
210
- %w(FindWindow CreateFileMapping SendMessageTimeout).each do |name|
211
- alias_method name, name+"A"
212
- module_function name
213
- end
214
- # :nodoc:
215
- module LibC
216
- extend FFI::Library
217
- ffi_lib FFI::Library::LIBC
218
- attach_function :malloc, [:size_t], :pointer
219
- attach_function :free, [:pointer], :void
220
- end
221
-
222
- def self.malloc_ptr(size)
223
- Fiddle::Pointer.new(LibC.malloc(size), size, LibC.method(:free))
224
- end
225
-
226
- def self.get_ptr(ptr)
227
- return data.address
228
- end
229
-
230
- def self.set_ptr_data(ptr, data)
231
- ptr.write_string_length(data, data.size)
232
- end
233
- else
234
- def self.malloc_ptr(size)
235
- return DL::CPtr.malloc(size, DL::RUBY_FREE)
24
+ module Net
25
+ module SSH
26
+ module Authentication
27
+ # This module encapsulates the implementation of a socket factory that
28
+ # uses the PuTTY "pageant" utility to obtain information about SSH
29
+ # identities.
30
+ #
31
+ # This code is a slightly modified version of the original implementation
32
+ # by Guillaume Marçais (guillaume.marcais@free.fr). It is used and
33
+ # relicensed by permission.
34
+ module Pageant
35
+ # From Putty pageant.c
36
+ AGENT_MAX_MSGLEN = 8192
37
+ AGENT_COPYDATA_ID = 0x804e50ba
38
+
39
+ # The definition of the Windows methods and data structures used in
40
+ # communicating with the pageant process.
41
+ module Win # rubocop:disable Metrics/ModuleLength
42
+ # Compatibility on initialization
43
+ if RUBY_VERSION < "1.9"
44
+ extend DL::Importable
45
+
46
+ dlload 'user32.dll'
47
+ dlload 'kernel32.dll'
48
+ dlload 'advapi32.dll'
49
+
50
+ SIZEOF_DWORD = DL.sizeof('L')
51
+ elsif RUBY_VERSION < "2.1"
52
+ extend DL::Importer
53
+ dlload 'user32.dll', 'kernel32.dll', 'advapi32.dll'
54
+ include DL::Win32Types
55
+
56
+ SIZEOF_DWORD = DL::SIZEOF_LONG
57
+ else
58
+ extend Fiddle::Importer
59
+ dlload 'user32.dll', 'kernel32.dll', 'advapi32.dll'
60
+ include Fiddle::Win32Types
61
+ SIZEOF_DWORD = Fiddle::SIZEOF_LONG
62
+ end
63
+
64
+ if RUBY_ENGINE == "jruby"
65
+ typealias("HANDLE", "void *") # From winnt.h
66
+ typealias("PHANDLE", "void *") # From winnt.h
67
+ typealias("ULONG_PTR", "unsigned long*")
68
+ end
69
+ typealias("LPCTSTR", "char *") # From winnt.h
70
+ typealias("LPVOID", "void *") # From winnt.h
71
+ typealias("LPCVOID", "const void *") # From windef.h
72
+ typealias("LRESULT", "long") # From windef.h
73
+ typealias("WPARAM", "unsigned int *") # From windef.h
74
+ typealias("LPARAM", "long *") # From windef.h
75
+ typealias("PDWORD_PTR", "long *") # From basetsd.h
76
+ typealias("USHORT", "unsigned short") # From windef.h
77
+
78
+ # From winbase.h, winnt.h
79
+ INVALID_HANDLE_VALUE = -1
80
+ NULL = nil
81
+ PAGE_READWRITE = 0x0004
82
+ FILE_MAP_WRITE = 2
83
+ WM_COPYDATA = 74
84
+
85
+ SMTO_NORMAL = 0 # From winuser.h
86
+
87
+ SUFFIX = if RUBY_ENGINE == "jruby"
88
+ "A"
89
+ else
90
+ ""
91
+ end
92
+
93
+ # args: lpClassName, lpWindowName
94
+ extern "HWND FindWindow#{SUFFIX}(LPCTSTR, LPCTSTR)"
95
+
96
+ # args: none
97
+ extern 'DWORD GetCurrentThreadId()'
98
+
99
+ # args: hFile, (ignored), flProtect, dwMaximumSizeHigh,
100
+ # dwMaximumSizeLow, lpName
101
+ extern "HANDLE CreateFileMapping#{SUFFIX}(HANDLE, void *, DWORD, " +
102
+ "DWORD, DWORD, LPCTSTR)"
103
+
104
+ # args: hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh,
105
+ # dwfileOffsetLow, dwNumberOfBytesToMap
106
+ extern 'LPVOID MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, DWORD)'
107
+
108
+ # args: lpBaseAddress
109
+ extern 'BOOL UnmapViewOfFile(LPCVOID)'
110
+
111
+ # args: hObject
112
+ extern 'BOOL CloseHandle(HANDLE)'
113
+
114
+ # args: hWnd, Msg, wParam, lParam, fuFlags, uTimeout, lpdwResult
115
+ extern "LRESULT SendMessageTimeout#{SUFFIX}(HWND, UINT, WPARAM, LPARAM, " +
116
+ "UINT, UINT, PDWORD_PTR)"
117
+
118
+ # args: none
119
+ extern 'DWORD GetLastError()'
120
+
121
+ # args: none
122
+ extern 'HANDLE GetCurrentProcess()'
123
+
124
+ # args: hProcessHandle, dwDesiredAccess, (out) phNewTokenHandle
125
+ extern 'BOOL OpenProcessToken(HANDLE, DWORD, PHANDLE)'
126
+
127
+ # args: hTokenHandle, uTokenInformationClass,
128
+ # (out) lpTokenInformation, dwTokenInformationLength
129
+ # (out) pdwInfoReturnLength
130
+ extern 'BOOL GetTokenInformation(HANDLE, UINT, LPVOID, DWORD, ' +
131
+ 'PDWORD)'
132
+
133
+ # args: (out) lpSecurityDescriptor, dwRevisionLevel
134
+ extern 'BOOL InitializeSecurityDescriptor(LPVOID, DWORD)'
135
+
136
+ # args: (out) lpSecurityDescriptor, lpOwnerSid, bOwnerDefaulted
137
+ extern 'BOOL SetSecurityDescriptorOwner(LPVOID, LPVOID, BOOL)'
138
+
139
+ # args: pSecurityDescriptor
140
+ extern 'BOOL IsValidSecurityDescriptor(LPVOID)'
141
+
142
+ # Constants needed for security attribute retrieval.
143
+ # Specifies the access mask corresponding to the desired access
144
+ # rights.
145
+ TOKEN_QUERY = 0x8
146
+
147
+ # The value of TOKEN_USER from the TOKEN_INFORMATION_CLASS enum.
148
+ TOKEN_USER_INFORMATION_CLASS = 1
149
+
150
+ # The initial revision level assigned to the security descriptor.
151
+ REVISION = 1
152
+
153
+ # Structs for security attribute functions.
154
+ # Holds the retrieved user access token.
155
+ TOKEN_USER = struct ['void * SID', 'DWORD ATTRIBUTES']
156
+
157
+ # Contains the security descriptor, this gets passed to the
158
+ # function that constructs the shared memory map.
159
+ SECURITY_ATTRIBUTES = struct ['DWORD nLength',
160
+ 'LPVOID lpSecurityDescriptor',
161
+ 'BOOL bInheritHandle']
162
+
163
+ # The security descriptor holds security information.
164
+ SECURITY_DESCRIPTOR = struct ['UCHAR Revision', 'UCHAR Sbz1',
165
+ 'USHORT Control', 'LPVOID Owner',
166
+ 'LPVOID Group', 'LPVOID Sacl',
167
+ 'LPVOID Dacl']
168
+
169
+ # The COPYDATASTRUCT is used to send WM_COPYDATA messages
170
+ COPYDATASTRUCT = if RUBY_ENGINE == "jruby"
171
+ struct ['ULONG_PTR dwData', 'DWORD cbData', 'LPVOID lpData']
172
+ else
173
+ struct ['uintptr_t dwData', 'DWORD cbData', 'LPVOID lpData']
174
+ end
175
+
176
+ # Compatibility for security attribute retrieval.
177
+ if RUBY_VERSION < "1.9"
178
+ # Alias functions to > 1.9 capitalization
179
+ %w[findWindow
180
+ getCurrentProcess
181
+ initializeSecurityDescriptor
182
+ setSecurityDescriptorOwner
183
+ isValidSecurityDescriptor
184
+ openProcessToken
185
+ getTokenInformation
186
+ getLastError
187
+ getCurrentThreadId
188
+ createFileMapping
189
+ mapViewOfFile
190
+ sendMessageTimeout
191
+ unmapViewOfFile
192
+ closeHandle].each do |name|
193
+ new_name = name[0].chr.upcase + name[1..name.length]
194
+ alias_method new_name, name
195
+ module_function new_name
196
+ end
197
+
198
+ def self.malloc_ptr(size)
199
+ return DL.malloc(size)
200
+ end
201
+
202
+ def self.get_ptr(data)
203
+ return data.to_ptr
204
+ end
205
+
206
+ def self.set_ptr_data(ptr, data)
207
+ ptr[0] = data
208
+ end
209
+ elsif RUBY_ENGINE == "jruby"
210
+ %w[FindWindow CreateFileMapping SendMessageTimeout].each do |name|
211
+ alias_method name, name + "A"
212
+ module_function name
213
+ end
214
+ # :nodoc:
215
+ module LibC
216
+ extend FFI::Library
217
+ ffi_lib FFI::Library::LIBC
218
+ attach_function :malloc, [:size_t], :pointer
219
+ attach_function :free, [:pointer], :void
220
+ end
221
+
222
+ def self.malloc_ptr(size)
223
+ Fiddle::Pointer.new(LibC.malloc(size), size, LibC.method(:free))
224
+ end
225
+
226
+ def self.get_ptr(ptr)
227
+ return data.address
228
+ end
229
+
230
+ def self.set_ptr_data(ptr, data)
231
+ ptr.write_string_length(data, data.size)
232
+ end
233
+ else
234
+ def self.malloc_ptr(size)
235
+ return DL::CPtr.malloc(size, DL::RUBY_FREE)
236
+ end
237
+
238
+ def self.get_ptr(data)
239
+ return DL::CPtr.to_ptr data
240
+ end
241
+
242
+ def self.set_ptr_data(ptr, data)
243
+ DL::CPtr.new(ptr)[0, data.size] = data
244
+ end
245
+ end
246
+
247
+ def self.get_security_attributes_for_user
248
+ user = get_current_user
249
+
250
+ psd_information = malloc_ptr(Win::SECURITY_DESCRIPTOR.size)
251
+ raise_error_if_zero(
252
+ Win.InitializeSecurityDescriptor(psd_information,
253
+ Win::REVISION)
254
+ )
255
+ raise_error_if_zero(
256
+ Win.SetSecurityDescriptorOwner(psd_information, get_sid_ptr(user),
257
+ 0)
258
+ )
259
+ raise_error_if_zero(
260
+ Win.IsValidSecurityDescriptor(psd_information)
261
+ )
262
+
263
+ sa = Win::SECURITY_ATTRIBUTES.new(to_struct_ptr(malloc_ptr(Win::SECURITY_ATTRIBUTES.size)))
264
+ sa.nLength = Win::SECURITY_ATTRIBUTES.size
265
+ sa.lpSecurityDescriptor = psd_information.to_i
266
+ sa.bInheritHandle = 1
267
+
268
+ return sa
269
+ end
270
+
271
+ if RUBY_ENGINE == "jruby"
272
+ def self.ptr_to_s(ptr, size)
273
+ ret = ptr.to_s(size)
274
+ ret << "\x00" while ret.size < size
275
+ ret
276
+ end
277
+
278
+ def self.ptr_to_handle(phandle)
279
+ phandle.ptr
280
+ end
281
+
282
+ def self.ptr_to_dword(ptr)
283
+ first = ptr.ptr.to_i
284
+ second = ptr_to_s(ptr, Win::SIZEOF_DWORD).unpack('L')[0]
285
+ raise "Error" unless first == second
286
+
287
+ first
288
+ end
289
+
290
+ def self.to_token_user(ptoken_information)
291
+ TOKEN_USER.new(ptoken_information.to_ptr)
292
+ end
293
+
294
+ def self.to_struct_ptr(ptr)
295
+ ptr.to_ptr
296
+ end
297
+
298
+ def self.get_sid(user)
299
+ ptr_to_s(user.to_ptr.ptr, Win::SIZEOF_DWORD).unpack('L')[0]
300
+ end
301
+
302
+ def self.get_sid_ptr(user)
303
+ user.to_ptr.ptr
304
+ end
305
+ else
306
+ def self.get_sid(user)
307
+ user.SID
308
+ end
309
+
310
+ def self.ptr_to_handle(phandle)
311
+ phandle.ptr.to_i
312
+ end
313
+
314
+ def self.to_struct_ptr(ptr)
315
+ ptr
316
+ end
317
+
318
+ def self.ptr_to_dword(ptr)
319
+ ptr.to_s(Win::SIZEOF_DWORD).unpack('L')[0]
320
+ end
321
+
322
+ def self.to_token_user(ptoken_information)
323
+ TOKEN_USER.new(ptoken_information)
324
+ end
325
+
326
+ def self.get_sid_ptr(user)
327
+ user.SID
328
+ end
329
+ end
330
+
331
+ def self.get_current_user
332
+ token_handle = open_process_token(Win.GetCurrentProcess,
333
+ Win::TOKEN_QUERY)
334
+ token_user = get_token_information(token_handle,
335
+ Win::TOKEN_USER_INFORMATION_CLASS)
336
+ return token_user
337
+ end
338
+
339
+ def self.open_process_token(process_handle, desired_access)
340
+ ptoken_handle = malloc_ptr(Win::SIZEOF_DWORD)
341
+
342
+ raise_error_if_zero(
343
+ Win.OpenProcessToken(process_handle, desired_access,
344
+ ptoken_handle)
345
+ )
346
+ token_handle = ptr_to_handle(ptoken_handle)
347
+ return token_handle
348
+ end
349
+
350
+ def self.get_token_information(token_handle,
351
+ token_information_class)
352
+ # Hold the size of the information to be returned
353
+ preturn_length = malloc_ptr(Win::SIZEOF_DWORD)
354
+
355
+ # Going to throw an INSUFFICIENT_BUFFER_ERROR, but that is ok
356
+ # here. This is retrieving the size of the information to be
357
+ # returned.
358
+ Win.GetTokenInformation(token_handle,
359
+ token_information_class,
360
+ Win::NULL, 0, preturn_length)
361
+ ptoken_information = malloc_ptr(ptr_to_dword(preturn_length))
362
+
363
+ # This call is going to write the requested information to
364
+ # the memory location referenced by token_information.
365
+ raise_error_if_zero(
366
+ Win.GetTokenInformation(token_handle,
367
+ token_information_class,
368
+ ptoken_information,
369
+ ptoken_information.size,
370
+ preturn_length)
371
+ )
372
+
373
+ return to_token_user(ptoken_information)
374
+ end
375
+
376
+ def self.raise_error_if_zero(result)
377
+ if result == 0
378
+ raise "Windows error: #{Win.GetLastError}"
379
+ end
380
+ end
381
+
382
+ # Get a null-terminated string given a string.
383
+ def self.get_cstr(str)
384
+ return str + "\000"
385
+ end
236
386
  end
237
387
 
238
- def self.get_ptr(data)
239
- return DL::CPtr.to_ptr data
388
+ # This is the pseudo-socket implementation that mimics the interface of
389
+ # a socket, translating each request into a Windows messaging call to
390
+ # the pageant daemon. This allows pageant support to be implemented
391
+ # simply by replacing the socket factory used by the Agent class.
392
+ class Socket
393
+ private_class_method :new
394
+
395
+ # The factory method for creating a new Socket instance.
396
+ def self.open
397
+ new
398
+ end
399
+
400
+ # Create a new instance that communicates with the running pageant
401
+ # instance. If no such instance is running, this will cause an error.
402
+ def initialize
403
+ @win = Win.FindWindow("Pageant", "Pageant")
404
+
405
+ if @win.to_i == 0
406
+ raise Net::SSH::Exception,
407
+ "pageant process not running"
408
+ end
409
+
410
+ @input_buffer = Net::SSH::Buffer.new
411
+ @output_buffer = Net::SSH::Buffer.new
412
+ end
413
+
414
+ # Forwards the data to #send_query, ignoring any arguments after
415
+ # the first.
416
+ def send(data, *args)
417
+ @input_buffer.append(data)
418
+
419
+ ret = data.length
420
+
421
+ while true
422
+ return ret if @input_buffer.length < 4
423
+
424
+ msg_length = @input_buffer.read_long + 4
425
+ @input_buffer.reset!
426
+
427
+ return ret if @input_buffer.length < msg_length
428
+
429
+ msg = @input_buffer.read!(msg_length)
430
+ @output_buffer.append(send_query(msg))
431
+ end
432
+ end
433
+
434
+ # Reads +n+ bytes from the cached result of the last query. If +n+
435
+ # is +nil+, returns all remaining data from the last query.
436
+ def read(n = nil)
437
+ @output_buffer.read(n)
438
+ end
439
+
440
+ def close; end
441
+
442
+ # Packages the given query string and sends it to the pageant
443
+ # process via the Windows messaging subsystem. The result is
444
+ # cached, to be returned piece-wise when #read is called.
445
+ def send_query(query)
446
+ res = nil
447
+ filemap = 0
448
+ ptr = nil
449
+ id = Win.malloc_ptr(Win::SIZEOF_DWORD)
450
+
451
+ mapname = "PageantRequest%08x" % Win.GetCurrentThreadId()
452
+ security_attributes = Win.get_ptr Win.get_security_attributes_for_user
453
+
454
+ filemap = Win.CreateFileMapping(Win::INVALID_HANDLE_VALUE,
455
+ security_attributes,
456
+ Win::PAGE_READWRITE, 0,
457
+ AGENT_MAX_MSGLEN, mapname)
458
+
459
+ if filemap == 0 || filemap == Win::INVALID_HANDLE_VALUE
460
+ raise Net::SSH::Exception,
461
+ "Creation of file mapping failed with error: #{Win.GetLastError}"
462
+ end
463
+
464
+ ptr = Win.MapViewOfFile(filemap, Win::FILE_MAP_WRITE, 0, 0,
465
+ 0)
466
+
467
+ if ptr.nil? || ptr.null?
468
+ raise Net::SSH::Exception, "Mapping of file failed"
469
+ end
470
+
471
+ Win.set_ptr_data(ptr, query)
472
+
473
+ # using struct to achieve proper alignment and field size on 64-bit platform
474
+ cds = Win::COPYDATASTRUCT.new(Win.malloc_ptr(Win::COPYDATASTRUCT.size))
475
+ cds.dwData = AGENT_COPYDATA_ID
476
+ cds.cbData = mapname.size + 1
477
+ cds.lpData = Win.get_cstr(mapname)
478
+ succ = Win.SendMessageTimeout(@win, Win::WM_COPYDATA, Win::NULL,
479
+ cds.to_ptr, Win::SMTO_NORMAL, 5000, id)
480
+
481
+ if succ > 0
482
+ retlen = 4 + ptr.to_s(4).unpack("N")[0]
483
+ res = ptr.to_s(retlen)
484
+ else
485
+ raise Net::SSH::Exception, "Message failed with error: #{Win.GetLastError}"
486
+ end
487
+
488
+ return res
489
+ ensure
490
+ Win.UnmapViewOfFile(ptr) unless ptr.nil? || ptr.null?
491
+ Win.CloseHandle(filemap) if filemap != 0
492
+ end
240
493
  end
241
-
242
- def self.set_ptr_data(ptr, data)
243
- DL::CPtr.new(ptr)[0,data.size] = data
244
- end
245
- end
246
-
247
- def self.get_security_attributes_for_user
248
- user = get_current_user
249
-
250
- psd_information = malloc_ptr(Win::SECURITY_DESCRIPTOR.size)
251
- raise_error_if_zero(
252
- Win.InitializeSecurityDescriptor(psd_information,
253
- Win::REVISION)
254
- )
255
- raise_error_if_zero(
256
- Win.SetSecurityDescriptorOwner(psd_information, get_sid_ptr(user),
257
- 0)
258
- )
259
- raise_error_if_zero(
260
- Win.IsValidSecurityDescriptor(psd_information)
261
- )
262
-
263
- sa = Win::SECURITY_ATTRIBUTES.new(to_struct_ptr(malloc_ptr(Win::SECURITY_ATTRIBUTES.size)))
264
- sa.nLength = Win::SECURITY_ATTRIBUTES.size
265
- sa.lpSecurityDescriptor = psd_information.to_i
266
- sa.bInheritHandle = 1
267
-
268
- return sa
269
- end
270
-
271
- if RUBY_ENGINE == "jruby"
272
- def self.ptr_to_s(ptr, size)
273
- ret = ptr.to_s(size)
274
- ret << "\x00" while ret.size < size
275
- ret
276
- end
277
-
278
- def self.ptr_to_handle(phandle)
279
- phandle.ptr
280
- end
281
-
282
- def self.ptr_to_dword(ptr)
283
- first = ptr.ptr.to_i
284
- second = ptr_to_s(ptr,Win::SIZEOF_DWORD).unpack('L')[0]
285
- raise "Error" unless first == second
286
- first
287
- end
288
-
289
- def self.to_token_user(ptoken_information)
290
- TOKEN_USER.new(ptoken_information.to_ptr)
291
- end
292
-
293
- def self.to_struct_ptr(ptr)
294
- ptr.to_ptr
295
- end
296
-
297
- def self.get_sid(user)
298
- ptr_to_s(user.to_ptr.ptr,Win::SIZEOF_DWORD).unpack('L')[0]
299
- end
300
-
301
- def self.get_sid_ptr(user)
302
- user.to_ptr.ptr
303
- end
304
- else
305
- def self.get_sid(user)
306
- user.SID
307
- end
308
-
309
- def self.ptr_to_handle(phandle)
310
- phandle.ptr.to_i
311
- end
312
-
313
- def self.to_struct_ptr(ptr)
314
- ptr
315
- end
316
-
317
- def self.ptr_to_dword(ptr)
318
- ptr.to_s(Win::SIZEOF_DWORD).unpack('L')[0]
319
- end
320
-
321
- def self.to_token_user(ptoken_information)
322
- TOKEN_USER.new(ptoken_information)
323
- end
324
-
325
- def self.get_sid_ptr(user)
326
- user.SID
327
- end
328
- end
329
-
330
- def self.get_current_user
331
- token_handle = open_process_token(Win.GetCurrentProcess,
332
- Win::TOKEN_QUERY)
333
- token_user = get_token_information(token_handle,
334
- Win::TOKEN_USER_INFORMATION_CLASS)
335
- return token_user
336
- end
337
-
338
- def self.open_process_token(process_handle, desired_access)
339
- ptoken_handle = malloc_ptr(Win::SIZEOF_DWORD)
340
-
341
- raise_error_if_zero(
342
- Win.OpenProcessToken(process_handle, desired_access,
343
- ptoken_handle)
344
- )
345
- token_handle = ptr_to_handle(ptoken_handle)
346
- return token_handle
347
- end
348
-
349
- def self.get_token_information(token_handle,
350
- token_information_class)
351
- # Hold the size of the information to be returned
352
- preturn_length = malloc_ptr(Win::SIZEOF_DWORD)
353
-
354
- # Going to throw an INSUFFICIENT_BUFFER_ERROR, but that is ok
355
- # here. This is retrieving the size of the information to be
356
- # returned.
357
- Win.GetTokenInformation(token_handle,
358
- token_information_class,
359
- Win::NULL, 0, preturn_length)
360
- ptoken_information = malloc_ptr(ptr_to_dword(preturn_length))
361
-
362
- # This call is going to write the requested information to
363
- # the memory location referenced by token_information.
364
- raise_error_if_zero(
365
- Win.GetTokenInformation(token_handle,
366
- token_information_class,
367
- ptoken_information,
368
- ptoken_information.size,
369
- preturn_length)
370
- )
371
-
372
- return to_token_user(ptoken_information)
373
- end
374
-
375
- def self.raise_error_if_zero(result)
376
- if result == 0
377
- raise "Windows error: #{Win.GetLastError}"
378
- end
379
- end
380
-
381
- # Get a null-terminated string given a string.
382
- def self.get_cstr(str)
383
- return str + "\000"
384
- end
385
- end
386
-
387
- # This is the pseudo-socket implementation that mimics the interface of
388
- # a socket, translating each request into a Windows messaging call to
389
- # the pageant daemon. This allows pageant support to be implemented
390
- # simply by replacing the socket factory used by the Agent class.
391
- class Socket
392
-
393
- private_class_method :new
394
-
395
- # The factory method for creating a new Socket instance.
396
- def self.open
397
- new
398
- end
399
-
400
- # Create a new instance that communicates with the running pageant
401
- # instance. If no such instance is running, this will cause an error.
402
- def initialize
403
- @win = Win.FindWindow("Pageant", "Pageant")
404
-
405
- if @win.to_i == 0
406
- raise Net::SSH::Exception,
407
- "pageant process not running"
408
- end
409
-
410
- @input_buffer = Net::SSH::Buffer.new
411
- @output_buffer = Net::SSH::Buffer.new
412
- end
413
-
414
- # Forwards the data to #send_query, ignoring any arguments after
415
- # the first.
416
- def send(data, *args)
417
- @input_buffer.append(data)
418
-
419
- ret = data.length
420
-
421
- while true
422
- return ret if @input_buffer.length < 4
423
- msg_length = @input_buffer.read_long + 4
424
- @input_buffer.reset!
425
-
426
- return ret if @input_buffer.length < msg_length
427
- msg = @input_buffer.read!(msg_length)
428
- @output_buffer.append(send_query(msg))
429
- end
430
- end
431
-
432
- # Reads +n+ bytes from the cached result of the last query. If +n+
433
- # is +nil+, returns all remaining data from the last query.
434
- def read(n = nil)
435
- @output_buffer.read(n)
436
- end
437
-
438
- def close; end
439
-
440
- # Packages the given query string and sends it to the pageant
441
- # process via the Windows messaging subsystem. The result is
442
- # cached, to be returned piece-wise when #read is called.
443
- def send_query(query)
444
- res = nil
445
- filemap = 0
446
- ptr = nil
447
- id = Win.malloc_ptr(Win::SIZEOF_DWORD)
448
-
449
- mapname = "PageantRequest%08x" % Win.GetCurrentThreadId()
450
- security_attributes = Win.get_ptr Win.get_security_attributes_for_user
451
-
452
- filemap = Win.CreateFileMapping(Win::INVALID_HANDLE_VALUE,
453
- security_attributes,
454
- Win::PAGE_READWRITE, 0,
455
- AGENT_MAX_MSGLEN, mapname)
456
-
457
- if filemap == 0 || filemap == Win::INVALID_HANDLE_VALUE
458
- raise Net::SSH::Exception,
459
- "Creation of file mapping failed with error: #{Win.GetLastError}"
460
- end
461
-
462
- ptr = Win.MapViewOfFile(filemap, Win::FILE_MAP_WRITE, 0, 0,
463
- 0)
464
-
465
- if ptr.nil? || ptr.null?
466
- raise Net::SSH::Exception, "Mapping of file failed"
467
- end
468
-
469
- Win.set_ptr_data(ptr, query)
470
-
471
- # using struct to achieve proper alignment and field size on 64-bit platform
472
- cds = Win::COPYDATASTRUCT.new(Win.malloc_ptr(Win::COPYDATASTRUCT.size))
473
- cds.dwData = AGENT_COPYDATA_ID
474
- cds.cbData = mapname.size + 1
475
- cds.lpData = Win.get_cstr(mapname)
476
- succ = Win.SendMessageTimeout(@win, Win::WM_COPYDATA, Win::NULL,
477
- cds.to_ptr, Win::SMTO_NORMAL, 5000, id)
478
-
479
- if succ > 0
480
- retlen = 4 + ptr.to_s(4).unpack("N")[0]
481
- res = ptr.to_s(retlen)
482
- else
483
- raise Net::SSH::Exception, "Message failed with error: #{Win.GetLastError}"
484
- end
485
-
486
- return res
487
- ensure
488
- Win.UnmapViewOfFile(ptr) unless ptr.nil? || ptr.null?
489
- Win.CloseHandle(filemap) if filemap != 0
490
494
  end
491
495
  end
492
496
  end
493
-
494
- end; end; end
497
+ end