-
Notifications
You must be signed in to change notification settings - Fork 11
/
phpseclib.php
222 lines (191 loc) · 6.85 KB
/
phpseclib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* libssh2-compatibility-layer
*
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
include 'Net/SFTP.php';
include 'Net/SFTP/Stream.php';
include 'Net/SCP.php';
include 'System/SSH/Agent.php';
include 'Crypt/RSA.php';
if (!function_exists('ssh2_connect')) {
define('SSH2_TERM_UNIT_CHARS', 0);
define('SSH2_TERM_UNIT_PIXELS', 1);
define('SSH2_STREAM_STDIO', 0);
define('SSH2_STREAM_STDERR', 1);
define('SSH2_FINGERPRINT_MD5', 0);
define('SSH2_FINGERPRINT_SHA1', 1);
define('SSH2_FINGERPRINT_HEX', 0);
define('SSH2_FINGERPRINT_RAW', 2);
// ssh2.tunnel is not supported
stream_wrapper_register('ssh2.sftp', 'Net_SFTP_Stream');
// phpseclib doesn't let you do SSH2 initially and then "upgrade" to SFTP. if you want to do SFTP
// you have specify SFTP from the onset. that said, just because phpseclib will initialize a
// connection with SFTP doesn't mean you can't execute commands on it and do non-SFTP stuff on it
function ssh2_connect($host, $port = 22, $methods = array(), $callbacks = array())
{
$session = new Net_SFTP($host, $port);
$session->enableQuietMode();
return $session;
}
function ssh2_auth_agent($session, $username)
{
$agent = new System_SSH_Agent();
return $session->login($username, $agent);
}
// phpseclib doesn't support hostbased authentication
function ssh2_auth_hostbased_file($session, $username, $hostname, $pubkeyfile, $privkeyfile, $passphrase = NULL, $local_username = NULL)
{
return false;
}
// phpseclib does not provide a mechanism to get supported authentication methods
function ssh2_auth_none($session, $username)
{
if ($session->login($username)) {
return true;
}
return array();
}
function ssh2_auth_password($session, $username, $password)
{
return $session->login($username, $password);
}
// only RSA keys are currently supported
function ssh2_auth_pubkey_file($session, $username, $pubkeyfile, $privkeyfile, $passphrase = NULL)
{
$privkey = new Crypt_RSA();
if (isset($passphrase)) {
$privkey->setPassword($passphrase);
}
$privkey->loadKey(file_get_contents($privkeyfile));
if ($privkey === false) {
return false;
}
return $session->login($username, $privkey);
}
// phpseclib only supports one type of pty: vt100
// environmental variables cannot be set through phpseclib
// the only $width_height_type option supported is SSH2_TERM_UNIT_CHARS
function ssh2_exec($session, $command, $pty = NULL, $env = NULL, $width = 80, $height = 25, $width_height_type = SSH2_TERM_UNIT_CHARS)
{
if (isset($pty)) {
$session->enablePTY();
}
if ($width_height_type == SSH2_TERM_UNIT_CHARS) {
$session->setWindowSize($width, $height);
}
$res = fopen('php://memory', 'w+');
fputs($res, $session->exec($command));
rewind($res);
if (isset($pty)) {
$session->disablePTY();
}
$session->setWindowSize(80, 24);
return $res;
}
// phpseclib does not work in the same way libssh2 does. ssh2_exec returns
// a resource stream that you can do fread on. phpseclib's Net_SSH2::exec()
// returns a string. ssh2_exec() emulates libssh2 by dumping the string to
// php://memory but it's only an emulation and you can't extract $session
// from $channel. with phpseclib the way you'd get STDERR is by doing
// $session->getStdError()
function ssh2_fetch_stream($channel, $streamid)
{
return false;
}
function ssh2_fingerprint($session, $flags = 0)
{
$hostkey = substr($session->getServerPublicHostKey(), 8);
$hostkey = ($flags & 1) ? sha1($hostkey) : md5($hostkey);
return ($flags & 2) ? pack('H*', $hostkey) : strtoupper($hostkey);
}
function ssh2_methods_negotiated($session)
{
return array(
'client_to_server' => array(
'crypt' => $session->getEncryptionAlgorithmsClient2Server(),
'comp' => $session->getCompressionAlgorithmsClient2Server(),
'mac' => $session->getMACAlgorithmsClient2Server()),
'server_to_client' => array(
'crypt' => $session->getEncryptionAlgorithmsServer2Client(),
'comp' => $session->getCompressionAlgorithmsServer2Client(),
'mac' => $session->getMACAlgorithmsServer2Client())
);
}
// not implemented in phpseclib
function ssh2_publickey_add($pkey, $algoname, $blob, $overwrite = false, $attributes = array())
{
return false;
}
// not implemented in phpseclib
function ssh2_publickey_init($session)
{
return false;
}
// not implemented in phpseclib
function ssh2_publickey_list($pkey)
{
return false;
}
// not implemented in phpseclib
function ssh2_publickey_remove($pkey, $algoname, $blob)
{
return false;
}
function ssh2_scp_recv($session, $remote_file, $local_file)
{
$scp = new Net_SCP($session);
return $scp->get($remote_file, $local_file);
}
// phpseclib does not let you change the $create_mode
function ssh2_scp_send($session, $local_file, $remote_file, $create_mode = 0644)
{
$scp = new Net_SCP($session);
return $scp->put($remote_file, $local_file, NET_SCP_LOCAL_FILE);
}
function ssh2_sftp($session)
{
return $session;
}
function ssh2_sftp_chmod($sftp, $filename, $mode)
{
return $sftp->chmod($mode, $filename) !== false;
}
function ssh2_sftp_lstat($sftp, $path)
{
return $sftp->lstat($path);
}
function ssh2_sftp_stat($sftp, $path)
{
return $sftp->stat($path);
}
function ssh2_sftp_mkdir($sftp, $dirname, $mode = 0777, $recursive = false)
{
return $sftp->mkdir($dirname, $mode, $recursive);
}
function ssh2_sftp_readlink($sftp, $link)
{
return $sftp->readlink($link);
}
function ssh2_sftp_symlink($sftp, $target, $link)
{
return $sftp->symlink($target, $link);
}
function ssh2_sftp_unlink($sftp, $filename)
{
return $sftp->delete($filename, false);
}
// phpseclib supports this via $ssh->read() / $ssh->write() but it is not currently possible to make that
// work with fread() / fwrite() in the way this function does. a yet to be written stream wrapper may do
// the trick though.. hard to say.
function ssh2_shell($session, $term_type = 'vanilla', $env = NULL, $width = 80, $height = 25, $width_height_type = 0)
{
return false;
}
// phpseclib doesn't currently support tunneling
function ssh2_tunnel($session, $host, $port)
{
return false;
}
}