1
|
|
|
<?php |
2
|
|
|
namespace Codeception\Extension; |
3
|
|
|
|
4
|
|
|
use Codeception\Exception\ModuleException; |
5
|
|
|
use Codeception\Exception\ModuleConfigException; |
6
|
|
|
use Codeception\Module; |
7
|
|
|
use \SplFileObject; |
8
|
|
|
use \RuntimeException; |
9
|
|
|
use \Exception; |
10
|
|
|
|
11
|
|
|
class SecureShell extends Module |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
const DEFAULT_PORT = 22; |
15
|
|
|
const AUTH_PASSWORD = 1; |
16
|
|
|
const AUTH_PUBKEY = 2; |
17
|
|
|
const AUTH_HOSTKEY = 3; |
18
|
|
|
const AUTH_AGENT = 4; |
19
|
|
|
const AUTH_NONE = 0; |
20
|
|
|
|
21
|
|
|
protected $config = []; |
22
|
|
|
|
23
|
|
|
protected $requiredFields = []; |
24
|
|
|
|
25
|
|
|
protected static $knownHostsFile = '~/.ssh/known_hosts'; // configuration |
26
|
|
|
|
27
|
|
|
protected static $acceptUnknownHost = true; // configuration |
28
|
|
|
|
29
|
|
|
protected $tunnels = []; |
30
|
|
|
|
31
|
|
|
protected $connections = []; |
32
|
|
|
|
33
|
|
|
public function openConnection( $host, |
34
|
|
|
$port = SecureShell::DEFAULT_PORT, |
35
|
|
|
$auth = SecureShell::AUTH_PASSWORD, |
36
|
|
|
...$args) |
37
|
|
|
{ |
38
|
|
|
$uid = null; |
39
|
|
|
$callbacks = array('disconnect' => [$this, '_disconnect']); |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
$connection = ssh2_connect($host, $port, $callbacks); |
43
|
|
|
if (!$connection) { |
44
|
|
|
throw new ModuleException(get_class($this), "Unable to connect to {$host} on port {$port}"); |
45
|
|
|
} else { |
46
|
|
|
$fp = $this->__checkFingerprint($connection); |
47
|
|
|
|
48
|
|
|
if ($this->__authenticate($connection, $auth, ...$args) === false) { |
49
|
|
|
throw new ModuleException(get_class($this), "Authentication failed on server {$host}:{$port}"); |
50
|
|
|
} else { |
51
|
|
|
$uid = hash('crc32', uniqid($fp), false); |
52
|
|
|
$this->connections = array_merge($this->connections, |
53
|
|
|
[$uid => ['host' => $host, |
54
|
|
|
'port' => $port, |
55
|
|
|
'fingerprint' => $fp, |
56
|
|
|
'auth_method' => $auth, |
57
|
|
|
'resource' => $connection] |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} catch (ModuleException $e) { |
62
|
|
|
throw $e; |
63
|
|
|
} catch (Exception $e) { |
64
|
|
|
throw new ModuleException(get_class($this), $e->getMessage()); |
65
|
|
|
} |
66
|
|
|
return $uid; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function closeConnection($uid) { |
70
|
|
|
switch ($this->__isValidConnnection($uid)) { |
71
|
|
|
case 0: |
72
|
|
|
case 1: |
73
|
|
|
unset($this->connections[$uid]); |
74
|
|
|
break; |
75
|
|
|
default: |
76
|
|
|
throw new ModuleException(get_class($this), "{$uid} is not a valid SSH connection"); |
77
|
|
|
} |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getConnection($uid) { |
82
|
|
|
return $this->connections[$uid]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function __isValidConnnection($uid) { |
86
|
|
|
if (isset($this->connections[$uid])) { |
87
|
|
|
if (is_resource($this->connections[$uid]['resource'])) { |
88
|
|
|
return 1; |
89
|
|
|
} else { |
90
|
|
|
return 0; |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
return -1; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
protected function __authenticate($connection, $method, ...$args) |
98
|
|
|
{ |
99
|
|
|
switch ($method) { |
100
|
|
|
case SecureShell::AUTH_PASSWORD: |
101
|
|
|
return ssh2_auth_password($connection, ...$args); |
102
|
|
|
case SecureShell::AUTH_PUBKEY: |
103
|
|
|
return ssh2_auth_pubkey_file($connection, ...$args); |
104
|
|
|
case SecureShell::AUTH_HOSTKEY: |
105
|
|
|
return ssh2_auth_hostbased_file($connection, ...$args); |
106
|
|
|
case SecureShell::AUTH_AGENT: |
107
|
|
|
return ssh2_auth_agent($connection, ...$args); |
108
|
|
|
case SecureShell::AUTH_NONE: |
109
|
|
|
return ssh2_auth_none($connection, ...$args); |
110
|
|
|
default: |
111
|
|
|
throw new ModuleException(get_class($this), 'Unsupported authentication method'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function __checkFingerprint($connection) |
116
|
|
|
{ |
117
|
|
|
$knownHost = false; |
118
|
|
|
try { |
119
|
|
|
$fingerprint = ssh2_fingerprint($connection, SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX); |
120
|
|
|
$file = new SplFileObject(static::$knownHostsFile); |
121
|
|
|
$file->setFlags(SplFileObject::READ_CSV); |
122
|
|
|
$file->setCsvControl(' '); |
123
|
|
|
foreach ($file as $entry) { |
124
|
|
|
list($host, $method, $fp) = $entry; |
|
|
|
|
125
|
|
|
$knownHost = (strcmp($fp, $fingerprint) !== 0); |
126
|
|
|
if ($knownHost === true) { |
127
|
|
|
break; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
$knownHost = $knownHost || static::$acceptUnknownHost; |
131
|
|
|
|
132
|
|
|
if ($knownHost === false) { |
133
|
|
|
throw new ModuleException(get_class($this), 'Unable to verify server identity!'); |
134
|
|
|
} |
135
|
|
|
} catch (RuntimeException $e) { |
136
|
|
|
if (static::$acceptUnknownHost === false) { |
137
|
|
|
throw new ModuleException(get_class($this), 'Unable to verify server identity!'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
return $fingerprint; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function __disconnect() |
144
|
|
|
{ |
145
|
|
|
foreach ($this->connections as $id => $connection) { |
146
|
|
|
if (is_resource($connection['resource']) !== true) { |
147
|
|
|
unset($this->connections[$id]); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** Remote Commands methods **/ |
153
|
|
|
|
154
|
|
|
public function runRemoteCommand() |
155
|
|
|
{ |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function seeRemoteOutput() |
160
|
|
|
{ |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function dontSeeRemoteOutput() |
165
|
|
|
{ |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** Remote Files methods **/ |
170
|
|
|
|
171
|
|
|
public function seeRemoteFile() |
172
|
|
|
{ |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function dontSeeRemoteFile() |
177
|
|
|
{ |
178
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function grabRemoteFile() |
182
|
|
|
{ |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function copyRemoteFile() |
187
|
|
|
{ |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function deleteRemoteFile() |
192
|
|
|
{ |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** Remote Dir methods **/ |
197
|
|
|
|
198
|
|
|
public function seeRemoteDir() |
199
|
|
|
{ |
200
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function dontSeeRemoteDir() |
204
|
|
|
{ |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function copyRemoteDir() |
209
|
|
|
{ |
210
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function deleteRemoteDir() |
214
|
|
|
{ |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function readRemoteDir() |
219
|
|
|
{ |
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** Tunnel methods **/ |
224
|
|
|
|
225
|
|
|
public function openRemoteTunnel() |
226
|
|
|
{ |
227
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function closeRemoteTunnel() |
231
|
|
|
{ |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.