From 99b9cc477d0351d2410fcbe5f175d570bdcc9ccf Mon Sep 17 00:00:00 2001 From: Clint Nelissen Date: Thu, 4 Dec 2014 13:45:13 -0800 Subject: [PATCH] Moved global constants to class constants --- phpseclib/Net/SCP.php | 100 +++---- phpseclib/Net/SFTP.php | 180 ++++++------- phpseclib/Net/SFTP/Stream.php | 2 +- phpseclib/Net/SSH1.php | 476 +++++++++++++++++----------------- phpseclib/Net/SSH2.php | 320 +++++++++++------------ 5 files changed, 530 insertions(+), 548 deletions(-) diff --git a/phpseclib/Net/SCP.php b/phpseclib/Net/SCP.php index f055db38..cb4d134f 100644 --- a/phpseclib/Net/SCP.php +++ b/phpseclib/Net/SCP.php @@ -31,35 +31,6 @@ * @link http://phpseclib.sourceforge.net */ -/**#@+ - * @access public - * @see Net_SCP::put() - */ -/** - * Reads data from a local file. - */ -define('NET_SCP_LOCAL_FILE', 1); -/** - * Reads data from a string. - */ -define('NET_SCP_STRING', 2); -/**#@-*/ - -/**#@+ - * @access private - * @see Net_SCP::_send() - * @see Net_SCP::_receive() - */ -/** - * SSH1 is being used. - */ -define('NET_SCP_SSH1', 1); -/** - * SSH2 is being used. - */ -define('NET_SCP_SSH2', 2); -/**#@-*/ - /** * Pure-PHP implementations of SCP. * @@ -69,6 +40,35 @@ define('NET_SCP_SSH2', 2); */ class Net_SCP { + /**#@+ + * @access public + * @see Net_SCP::put() + */ + /** + * Reads data from a local file. + */ + const SOURCE_LOCAL_FILE = 1; + /** + * Reads data from a string. + */ + const SOURCE_STRING = 2; + /**#@-*/ + + /**#@+ + * @access private + * @see Net_SCP::_send() + * @see Net_SCP::_receive() + */ + /** + * SSH1 is being used. + */ + const MODE_SSH1 = 1; + /** + * SSH2 is being used. + */ + const MODE_SSH2 = 2; + /**#@-*/ + /** * SSH Object * @@ -112,11 +112,11 @@ class Net_SCP switch (strtolower(get_class($ssh))) { case 'net_ssh2': - $this->mode = NET_SCP_SSH2; + $this->mode = self::MODE_SSH2; break; case 'net_ssh1': $this->packet_size = 50000; - $this->mode = NET_SCP_SSH1; + $this->mode = self::MODE_SSH1; break; default: return; @@ -132,7 +132,7 @@ class Net_SCP * So, for example, if you set $data to 'filename.ext' and then do Net_SCP::get(), you will get a file, twelve bytes * long, containing 'filename.ext' as its contents. * - * Setting $mode to NET_SCP_LOCAL_FILE will change the above behavior. With NET_SCP_LOCAL_FILE, $remote_file will + * Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior. With self::SOURCE_LOCAL_FILE, $remote_file will * contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how * large $remote_file will be, as well. * @@ -146,7 +146,7 @@ class Net_SCP * @return Boolean * @access public */ - function put($remote_file, $data, $mode = NET_SCP_STRING, $callback = null) + function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null) { if (!isset($this->ssh)) { return false; @@ -161,13 +161,13 @@ class Net_SCP return false; } - if ($this->mode == NET_SCP_SSH2) { - $this->packet_size = $this->ssh->packet_size_client_to_server[NET_SSH2_CHANNEL_EXEC] - 4; + if ($this->mode == self::MODE_SSH2) { + $this->packet_size = $this->ssh->packet_size_client_to_server[Net_SSH2::CHANNEL_EXEC] - 4; } $remote_file = basename($remote_file); - if ($mode == NET_SCP_STRING) { + if ($mode == self::SOURCE_STRING) { $size = strlen($data); } else { if (!is_file($data)) { @@ -191,7 +191,7 @@ class Net_SCP $sent = 0; while ($sent < $size) { - $temp = $mode & NET_SCP_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size); + $temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size); $this->_send($temp); $sent+= strlen($temp); @@ -201,7 +201,7 @@ class Net_SCP } $this->_close(); - if ($mode != NET_SCP_STRING) { + if ($mode != self::SOURCE_STRING) { fclose($fp); } @@ -279,10 +279,10 @@ class Net_SCP function _send($data) { switch ($this->mode) { - case NET_SCP_SSH2: - $this->ssh->_send_channel_packet(NET_SSH2_CHANNEL_EXEC, $data); + case self::MODE_SSH2: + $this->ssh->_send_channel_packet(Net_SSH2::CHANNEL_EXEC, $data); break; - case NET_SCP_SSH1: + case self::MODE_SSH1: $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data); $this->ssh->_send_binary_packet($data); } @@ -297,18 +297,18 @@ class Net_SCP function _receive() { switch ($this->mode) { - case NET_SCP_SSH2: - return $this->ssh->_get_channel_packet(NET_SSH2_CHANNEL_EXEC, true); - case NET_SCP_SSH1: + case self::MODE_SSH2: + return $this->ssh->_get_channel_packet(Net_SSH2::CHANNEL_EXEC, true); + case self::MODE_SSH1: if (!$this->ssh->bitmap) { return false; } while (true) { $response = $this->ssh->_get_binary_packet(); - switch ($response[NET_SSH1_RESPONSE_TYPE]) { + switch ($response[Net_SSH1::RESPONSE_TYPE]) { case NET_SSH1_SMSG_STDOUT_DATA: - extract(unpack('Nlength', $response[NET_SSH1_RESPONSE_DATA])); - return $this->ssh->_string_shift($response[NET_SSH1_RESPONSE_DATA], $length); + extract(unpack('Nlength', $response[Net_SSH1::RESPONSE_DATA])); + return $this->ssh->_string_shift($response[Net_SSH1::RESPONSE_DATA], $length); case NET_SSH1_SMSG_STDERR_DATA: break; case NET_SSH1_SMSG_EXITSTATUS: @@ -332,10 +332,10 @@ class Net_SCP function _close() { switch ($this->mode) { - case NET_SCP_SSH2: - $this->ssh->_close_channel(NET_SSH2_CHANNEL_EXEC, true); + case self::MODE_SSH2: + $this->ssh->_close_channel(Net_SSH2::CHANNEL_EXEC, true); break; - case NET_SCP_SSH1: + case self::MODE_SSH1: $this->ssh->disconnect(); } } diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index 2f0cfcb1..1c9d7522 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -42,58 +42,6 @@ if (!class_exists('Net_SSH2')) { include_once 'SSH2.php'; } -/**#@+ - * @access public - * @see Net_SFTP::getLog() - */ -/** - * Returns the message numbers - */ -define('NET_SFTP_LOG_SIMPLE', NET_SSH2_LOG_SIMPLE); -/** - * Returns the message content - */ -define('NET_SFTP_LOG_COMPLEX', NET_SSH2_LOG_COMPLEX); -/** - * Outputs the message content in real-time. - */ -define('NET_SFTP_LOG_REALTIME', 3); -/**#@-*/ - -/** - * SFTP channel constant - * - * Net_SSH2::exec() uses 0 and Net_SSH2::read() / Net_SSH2::write() use 1. - * - * @see Net_SSH2::_send_channel_packet() - * @see Net_SSH2::_get_channel_packet() - * @access private - */ -define('NET_SFTP_CHANNEL', 0x100); - -/**#@+ - * @access public - * @see Net_SFTP::put() - */ -/** - * Reads data from a local file. - */ -define('NET_SFTP_LOCAL_FILE', 1); -/** - * Reads data from a string. - */ -// this value isn't really used anymore but i'm keeping it reserved for historical reasons -define('NET_SFTP_STRING', 2); -/** - * Resumes an upload - */ -define('NET_SFTP_RESUME', 4); -/** - * Append a local file to an already existing remote file - */ -define('NET_SFTP_RESUME_START', 8); -/**#@-*/ - /** * Pure-PHP implementations of SFTP. * @@ -103,6 +51,40 @@ define('NET_SFTP_RESUME_START', 8); */ class Net_SFTP extends Net_SSH2 { + /** + * SFTP channel constant + * + * Net_SSH2::exec() uses 0 and Net_SSH2::read() / Net_SSH2::write() use 1. + * + * @see Net_SSH2::_send_channel_packet() + * @see Net_SSH2::_get_channel_packet() + * @access private + */ + const CHANNEL = 0x100; + + /**#@+ + * @access public + * @see Net_SFTP::put() + */ + /** + * Reads data from a local file. + */ + const SOURCE_LOCAL_FILE = 1; + /** + * Reads data from a string. + */ + // this value isn't really used anymore but i'm keeping it reserved for historical reasons + const SOURCE_STRING = 2; + /** + * Resumes an upload + */ + const RESUME = 4; + /** + * Append a local file to an already existing remote file + */ + const RESUME_START = 8; + /**#@-*/ + /** * Packet Types * @@ -409,31 +391,31 @@ class Net_SFTP extends Net_SSH2 return false; } - $this->window_size_server_to_client[NET_SFTP_CHANNEL] = $this->window_size; + $this->window_size_server_to_client[self::CHANNEL] = $this->window_size; $packet = pack('CNa*N3', - NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SFTP_CHANNEL, $this->window_size, 0x4000); + NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL, $this->window_size, 0x4000); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_OPEN; + $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_OPEN; - $response = $this->_get_channel_packet(NET_SFTP_CHANNEL); + $response = $this->_get_channel_packet(self::CHANNEL); if ($response === false) { return false; } $packet = pack('CNNa*CNa*', - NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SFTP_CHANNEL], strlen('subsystem'), 'subsystem', 1, strlen('sftp'), 'sftp'); + NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL], strlen('subsystem'), 'subsystem', 1, strlen('sftp'), 'sftp'); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST; + $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST; - $response = $this->_get_channel_packet(NET_SFTP_CHANNEL); + $response = $this->_get_channel_packet(self::CHANNEL); if ($response === false) { // from PuTTY's psftp.exe $command = "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n" . @@ -442,20 +424,20 @@ class Net_SFTP extends Net_SSH2 // we don't do $this->exec($command, false) because exec() operates on a different channel and plus the SSH_MSG_CHANNEL_OPEN that exec() does // is redundant $packet = pack('CNNa*CNa*', - NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SFTP_CHANNEL], strlen('exec'), 'exec', 1, strlen($command), $command); + NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL], strlen('exec'), 'exec', 1, strlen($command), $command); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST; + $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_REQUEST; - $response = $this->_get_channel_packet(NET_SFTP_CHANNEL); + $response = $this->_get_channel_packet(self::CHANNEL); if ($response === false) { return false; } } - $this->channel_status[NET_SFTP_CHANNEL] = NET_SSH2_MSG_CHANNEL_DATA; + $this->channel_status[self::CHANNEL] = NET_SSH2_MSG_CHANNEL_DATA; if (!$this->_send_sftp_packet(NET_SFTP_INIT, "\0\0\0\3")) { return false; @@ -664,7 +646,7 @@ class Net_SFTP extends Net_SSH2 */ function chdir($dir) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -810,7 +792,7 @@ class Net_SFTP extends Net_SSH2 */ function _list($dir, $raw = true) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1019,7 +1001,7 @@ class Net_SFTP extends Net_SSH2 */ function size($filename) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1115,7 +1097,7 @@ class Net_SFTP extends Net_SSH2 */ function stat($filename) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1172,7 +1154,7 @@ class Net_SFTP extends Net_SSH2 */ function lstat($filename) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1286,7 +1268,7 @@ class Net_SFTP extends Net_SSH2 */ function touch($filename, $time = null, $atime = null) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1423,7 +1405,7 @@ class Net_SFTP extends Net_SSH2 */ function _setstat($filename, $attr, $recursive) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1550,7 +1532,7 @@ class Net_SFTP extends Net_SSH2 */ function readlink($link) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1594,7 +1576,7 @@ class Net_SFTP extends Net_SSH2 */ function symlink($target, $link) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1630,7 +1612,7 @@ class Net_SFTP extends Net_SSH2 */ function mkdir($dir, $mode = -1, $recursive = false) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1693,7 +1675,7 @@ class Net_SFTP extends Net_SSH2 */ function rmdir($dir) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1735,7 +1717,7 @@ class Net_SFTP extends Net_SSH2 * So, for example, if you set $data to 'filename.ext' and then do Net_SFTP::get(), you will get a file, twelve bytes * long, containing 'filename.ext' as its contents. * - * Setting $mode to NET_SFTP_LOCAL_FILE will change the above behavior. With NET_SFTP_LOCAL_FILE, $remote_file will + * Setting $mode to self::SOURCE_LOCAL_FILE will change the above behavior. With self::SOURCE_LOCAL_FILE, $remote_file will * contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how * large $remote_file will be, as well. * @@ -1744,22 +1726,22 @@ class Net_SFTP extends Net_SSH2 * Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take * care of that, yourself. * - * $mode can take an additional two parameters - NET_SFTP_RESUME and NET_SFTP_RESUME_START. These are bitwise AND'd with + * $mode can take an additional two parameters - self::RESUME and self::RESUME_START. These are bitwise AND'd with * $mode. So if you want to resume upload of a 300mb file on the local file system you'd set $mode to the following: * - * NET_SFTP_LOCAL_FILE | NET_SFTP_RESUME + * self::SOURCE_LOCAL_FILE | self::RESUME * * If you wanted to simply append the full contents of a local file to the full contents of a remote file you'd replace - * NET_SFTP_RESUME with NET_SFTP_RESUME_START. + * self::RESUME with self::RESUME_START. * - * If $mode & (NET_SFTP_RESUME | NET_SFTP_RESUME_START) then NET_SFTP_RESUME_START will be assumed. + * If $mode & (self::RESUME | self::RESUME_START) then self::RESUME_START will be assumed. * - * $start and $local_start give you more fine grained control over this process and take precident over NET_SFTP_RESUME - * when they're non-negative. ie. $start could let you write at the end of a file (like NET_SFTP_RESUME) or in the middle - * of one. $local_start could let you start your reading from the end of a file (like NET_SFTP_RESUME_START) or in the + * $start and $local_start give you more fine grained control over this process and take precident over self::RESUME + * when they're non-negative. ie. $start could let you write at the end of a file (like self::RESUME) or in the middle + * of one. $local_start could let you start your reading from the end of a file (like self::RESUME_START) or in the * middle of one. * - * Setting $local_start to > 0 or $mode | NET_SFTP_RESUME_START doesn't do anything unless $mode | NET_SFTP_LOCAL_FILE. + * Setting $local_start to > 0 or $mode | self::RESUME_START doesn't do anything unless $mode | self::SOURCE_LOCAL_FILE. * * @param String $remote_file * @param String|resource $data @@ -1770,9 +1752,9 @@ class Net_SFTP extends Net_SSH2 * @access public * @internal ASCII mode for SFTPv4/5/6 can be supported by adding a new function - Net_SFTP::setMode(). */ - function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_start = -1) + function put($remote_file, $data, $mode = self::SOURCE_STRING, $start = -1, $local_start = -1) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -1786,11 +1768,11 @@ class Net_SFTP extends Net_SSH2 $flags = NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE; // according to the SFTP specs, NET_SFTP_OPEN_APPEND should "force all writes to append data at the end of the file." // in practice, it doesn't seem to do that. - //$flags|= ($mode & NET_SFTP_RESUME) ? NET_SFTP_OPEN_APPEND : NET_SFTP_OPEN_TRUNCATE; + //$flags|= ($mode & self::RESUME) ? NET_SFTP_OPEN_APPEND : NET_SFTP_OPEN_TRUNCATE; if ($start >= 0) { $offset = $start; - } elseif ($mode & NET_SFTP_RESUME) { + } elseif ($mode & self::RESUME) { // if NET_SFTP_OPEN_APPEND worked as it should _size() wouldn't need to be called $size = $this->size($remote_file); $offset = $size !== false ? $size : 0; @@ -1820,10 +1802,10 @@ class Net_SFTP extends Net_SSH2 // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3 switch (true) { case is_resource($data): - $mode = $mode & ~NET_SFTP_LOCAL_FILE; + $mode = $mode & ~self::SOURCE_LOCAL_FILE; $fp = $data; break; - case $mode & NET_SFTP_LOCAL_FILE: + case $mode & self::SOURCE_LOCAL_FILE: if (!is_file($data)) { user_error("$data is not a valid file"); return false; @@ -1840,7 +1822,7 @@ class Net_SFTP extends Net_SSH2 if ($local_start >= 0) { fseek($fp, $local_start); - } elseif ($mode & NET_SFTP_RESUME_START) { + } elseif ($mode & self::RESUME_START) { // do nothing } else { fseek($fp, $offset); @@ -1861,7 +1843,7 @@ class Net_SFTP extends Net_SSH2 $subtemp = $offset + $sent; $packet = pack('Na*N3a*', strlen($handle), $handle, $subtemp / 4294967296, $subtemp, strlen($temp), $temp); if (!$this->_send_sftp_packet(NET_SFTP_WRITE, $packet)) { - if ($mode & NET_SFTP_LOCAL_FILE) { + if ($mode & self::SOURCE_LOCAL_FILE) { fclose($fp); } return false; @@ -1880,14 +1862,14 @@ class Net_SFTP extends Net_SSH2 } if (!$this->_read_put_responses($i)) { - if ($mode & NET_SFTP_LOCAL_FILE) { + if ($mode & self::SOURCE_LOCAL_FILE) { fclose($fp); } $this->_close_handle($handle); return false; } - if ($mode & NET_SFTP_LOCAL_FILE) { + if ($mode & self::SOURCE_LOCAL_FILE) { fclose($fp); } @@ -1971,7 +1953,7 @@ class Net_SFTP extends Net_SSH2 */ function get($remote_file, $local_file = false, $offset = 0, $length = -1) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -2085,7 +2067,7 @@ class Net_SFTP extends Net_SSH2 */ function delete($path, $recursive = true) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -2400,7 +2382,7 @@ class Net_SFTP extends Net_SSH2 */ function rename($oldname, $newname) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & Net_SSH2::MASK_LOGIN)) { return false; } @@ -2587,7 +2569,7 @@ class Net_SFTP extends Net_SSH2 pack('NCa*', strlen($data) + 1, $type, $data); $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838 - $result = $this->_send_channel_packet(NET_SFTP_CHANNEL, $packet); + $result = $this->_send_channel_packet(self::CHANNEL, $packet); $stop = strtok(microtime(), ' ') + strtok(''); if (defined('NET_SFTP_LOGGING')) { @@ -2629,7 +2611,7 @@ class Net_SFTP extends Net_SSH2 // SFTP packet length while (strlen($this->packet_buffer) < 4) { - $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL); + $temp = $this->_get_channel_packet(self::CHANNEL); if (is_bool($temp)) { $this->packet_type = false; $this->packet_buffer = ''; @@ -2643,7 +2625,7 @@ class Net_SFTP extends Net_SSH2 // SFTP packet type and data payload while ($tempLength > 0) { - $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL); + $temp = $this->_get_channel_packet(self::CHANNEL); if (is_bool($temp)) { $this->packet_type = false; $this->packet_buffer = ''; diff --git a/phpseclib/Net/SFTP/Stream.php b/phpseclib/Net/SFTP/Stream.php index f8bee638..de6021ed 100644 --- a/phpseclib/Net/SFTP/Stream.php +++ b/phpseclib/Net/SFTP/Stream.php @@ -334,7 +334,7 @@ class Net_SFTP_Stream return false; } - $result = $this->sftp->put($this->path, $data, NET_SFTP_STRING, $this->pos); + $result = $this->sftp->put($this->path, $data, Net_SFTP::SOURCE_STRING, $this->pos); if (isset($this->notification) && is_callable($this->notification)) { if (!$result) { call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0); diff --git a/phpseclib/Net/SSH1.php b/phpseclib/Net/SSH1.php index e301d5ce..dd383034 100644 --- a/phpseclib/Net/SSH1.php +++ b/phpseclib/Net/SSH1.php @@ -49,165 +49,6 @@ use \phpseclib\Crypt\Random; use \phpseclib\Math\BigInteger; -/**#@+ - * Encryption Methods - * - * @see Net_SSH1::getSupportedCiphers() - * @access public - */ -/** - * No encryption - * - * Not supported. - */ -define('NET_SSH1_CIPHER_NONE', 0); -/** - * IDEA in CFB mode - * - * Not supported. - */ -define('NET_SSH1_CIPHER_IDEA', 1); -/** - * DES in CBC mode - */ -define('NET_SSH1_CIPHER_DES', 2); -/** - * Triple-DES in CBC mode - * - * All implementations are required to support this - */ -define('NET_SSH1_CIPHER_3DES', 3); -/** - * TRI's Simple Stream encryption CBC - * - * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, does define it (see cipher.h), - * although it doesn't use it (see cipher.c) - */ -define('NET_SSH1_CIPHER_BROKEN_TSS', 4); -/** - * RC4 - * - * Not supported. - * - * @internal According to the SSH1 specs: - * - * "The first 16 bytes of the session key are used as the key for - * the server to client direction. The remaining 16 bytes are used - * as the key for the client to server direction. This gives - * independent 128-bit keys for each direction." - * - * This library currently only supports encryption when the same key is being used for both directions. This is - * because there's only one $crypto object. Two could be added ($encrypt and $decrypt, perhaps). - */ -define('NET_SSH1_CIPHER_RC4', 5); -/** - * Blowfish - * - * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, defines it (see cipher.h) and - * uses it (see cipher.c) - */ -define('NET_SSH1_CIPHER_BLOWFISH', 6); -/**#@-*/ - -/**#@+ - * Authentication Methods - * - * @see Net_SSH1::getSupportedAuthentications() - * @access public - */ -/** - * .rhosts or /etc/hosts.equiv - */ -define('NET_SSH1_AUTH_RHOSTS', 1); -/** - * pure RSA authentication - */ -define('NET_SSH1_AUTH_RSA', 2); -/** - * password authentication - * - * This is the only method that is supported by this library. - */ -define('NET_SSH1_AUTH_PASSWORD', 3); -/** - * .rhosts with RSA host authentication - */ -define('NET_SSH1_AUTH_RHOSTS_RSA', 4); -/**#@-*/ - -/**#@+ - * Terminal Modes - * - * @link http://3sp.com/content/developer/maverick-net/docs/Maverick.SSH.PseudoTerminalModesMembers.html - * @access private - */ -define('NET_SSH1_TTY_OP_END', 0); -/**#@-*/ - -/** - * The Response Type - * - * @see Net_SSH1::_get_binary_packet() - * @access private - */ -define('NET_SSH1_RESPONSE_TYPE', 1); - -/** - * The Response Data - * - * @see Net_SSH1::_get_binary_packet() - * @access private - */ -define('NET_SSH1_RESPONSE_DATA', 2); - -/**#@+ - * Execution Bitmap Masks - * - * @see Net_SSH1::bitmap - * @access private - */ -define('NET_SSH1_MASK_CONSTRUCTOR', 0x00000001); -define('NET_SSH1_MASK_CONNECTED', 0x00000002); -define('NET_SSH1_MASK_LOGIN', 0x00000004); -define('NET_SSH1_MASK_SHELL', 0x00000008); -/**#@-*/ - -/**#@+ - * @access public - * @see Net_SSH1::getLog() - */ -/** - * Returns the message numbers - */ -define('NET_SSH1_LOG_SIMPLE', 1); -/** - * Returns the message content - */ -define('NET_SSH1_LOG_COMPLEX', 2); -/** - * Outputs the content real-time - */ -define('NET_SSH1_LOG_REALTIME', 3); -/** - * Dumps the content real-time to a file - */ -define('NET_SSH1_LOG_REALTIME_FILE', 4); -/**#@-*/ - -/**#@+ - * @access public - * @see Net_SSH1::read() - */ -/** - * Returns when a string matching $expect exactly is found - */ -define('NET_SSH1_READ_SIMPLE', 1); -/** - * Returns when a string matching the regular expression $expect is found - */ -define('NET_SSH1_READ_REGEX', 2); -/**#@-*/ - /** * Pure-PHP implementation of SSHv1. * @@ -217,6 +58,165 @@ define('NET_SSH1_READ_REGEX', 2); */ class Net_SSH1 { + /**#@+ + * Encryption Methods + * + * @see Net_SSH1::getSupportedCiphers() + * @access public + */ + /** + * No encryption + * + * Not supported. + */ + const CIPHER_NONE = 0; + /** + * IDEA in CFB mode + * + * Not supported. + */ + const CIPHER_IDEA = 1; + /** + * DES in CBC mode + */ + const CIPHER_DES = 2; + /** + * Triple-DES in CBC mode + * + * All implementations are required to support this + */ + const CIPHER_3DES = 3; + /** + * TRI's Simple Stream encryption CBC + * + * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, does define it (see cipher.h), + * although it doesn't use it (see cipher.c) + */ + const CIPHER_BROKEN_TSS = 4; + /** + * RC4 + * + * Not supported. + * + * @internal According to the SSH1 specs: + * + * "The first 16 bytes of the session key are used as the key for + * the server to client direction. The remaining 16 bytes are used + * as the key for the client to server direction. This gives + * independent 128-bit keys for each direction." + * + * This library currently only supports encryption when the same key is being used for both directions. This is + * because there's only one $crypto object. Two could be added ($encrypt and $decrypt, perhaps). + */ + const CIPHER_RC4 = 5; + /** + * Blowfish + * + * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, defines it (see cipher.h) and + * uses it (see cipher.c) + */ + const CIPHER_BLOWFISH = 6; + /**#@-*/ + + /**#@+ + * Authentication Methods + * + * @see Net_SSH1::getSupportedAuthentications() + * @access public + */ + /** + * .rhosts or /etc/hosts.equiv + */ + const AUTH_RHOSTS = 1; + /** + * pure RSA authentication + */ + const AUTH_RSA = 2; + /** + * password authentication + * + * This is the only method that is supported by this library. + */ + const AUTH_PASSWORD = 3; + /** + * .rhosts with RSA host authentication + */ + const AUTH_RHOSTS_RSA = 4; + /**#@-*/ + + /**#@+ + * Terminal Modes + * + * @link http://3sp.com/content/developer/maverick-net/docs/Maverick.SSH.PseudoTerminalModesMembers.html + * @access private + */ + const TTY_OP_END = 0; + /**#@-*/ + + /** + * The Response Type + * + * @see Net_SSH1::_get_binary_packet() + * @access private + */ + const RESPONSE_TYPE = 1; + + /** + * The Response Data + * + * @see Net_SSH1::_get_binary_packet() + * @access private + */ + const RESPONSE_DATA = 2; + + /**#@+ + * Execution Bitmap Masks + * + * @see Net_SSH1::bitmap + * @access private + */ + const MASK_CONSTRUCTOR = 0x00000001; + const MASK_CONNECTED = 0x00000002; + const MASK_LOGIN = 0x00000004; + const MASK_SHELL = 0x00000008; + /**#@-*/ + + /**#@+ + * @access public + * @see Net_SSH1::getLog() + */ + /** + * Returns the message numbers + */ + const LOG_SIMPLE = 1; + /** + * Returns the message content + */ + const LOG_COMPLEX = 2; + /** + * Outputs the content real-time + */ + const LOG_REALTIME = 3; + /** + * Dumps the content real-time to a file + */ + const LOG_REALTIME_FILE = 4; + /**#@-*/ + + /**#@+ + * @access public + * @see Net_SSH1::read() + */ + /** + * Returns when a string matching $expect exactly is found + */ + const READ_SIMPLE = 1; + /** + * Returns when a string matching the regular expression $expect is found + */ + const READ_REGEX = 2; + /**#@-*/ + /** * The SSH identifier * @@ -306,13 +306,13 @@ class Net_SSH1 * @access private */ var $supported_ciphers = array( - NET_SSH1_CIPHER_NONE => 'No encryption', - NET_SSH1_CIPHER_IDEA => 'IDEA in CFB mode', - NET_SSH1_CIPHER_DES => 'DES in CBC mode', - NET_SSH1_CIPHER_3DES => 'Triple-DES in CBC mode', - NET_SSH1_CIPHER_BROKEN_TSS => 'TRI\'s Simple Stream encryption CBC', - NET_SSH1_CIPHER_RC4 => 'RC4', - NET_SSH1_CIPHER_BLOWFISH => 'Blowfish' + self::CIPHER_NONE => 'No encryption', + self::CIPHER_IDEA => 'IDEA in CFB mode', + self::CIPHER_DES => 'DES in CBC mode', + self::CIPHER_3DES => 'Triple-DES in CBC mode', + self::CIPHER_BROKEN_TSS => 'TRI\'s Simple Stream encryption CBC', + self::CIPHER_RC4 => 'RC4', + self::CIPHER_BLOWFISH => 'Blowfish' ); /** @@ -325,10 +325,10 @@ class Net_SSH1 * @access private */ var $supported_authentications = array( - NET_SSH1_AUTH_RHOSTS => '.rhosts or /etc/hosts.equiv', - NET_SSH1_AUTH_RSA => 'pure RSA authentication', - NET_SSH1_AUTH_PASSWORD => 'password authentication', - NET_SSH1_AUTH_RHOSTS_RSA => '.rhosts with RSA host authentication' + self::AUTH_RHOSTS => '.rhosts or /etc/hosts.equiv', + self::AUTH_RSA => 'pure RSA authentication', + self::AUTH_PASSWORD => 'password authentication', + self::AUTH_RHOSTS_RSA => '.rhosts with RSA host authentication' ); /** @@ -500,7 +500,7 @@ class Net_SSH1 * @return Net_SSH1 * @access public */ - function __construct($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES) + function __construct($host, $port = 22, $timeout = 10, $cipher = self::CIPHER_3DES) { $this->protocol_flags = array( 1 => 'NET_SSH1_MSG_DISCONNECT', @@ -562,37 +562,37 @@ class Net_SSH1 fputs($this->fsock, $this->identifier."\r\n"); $response = $this->_get_binary_packet(); - if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) { + if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) { user_error('Expected SSH_SMSG_PUBLIC_KEY'); return false; } - $anti_spoofing_cookie = $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 8); + $anti_spoofing_cookie = $this->_string_shift($response[self::RESPONSE_DATA], 8); - $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4); + $this->_string_shift($response[self::RESPONSE_DATA], 4); - $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2)); - $server_key_public_exponent = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256); + $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2)); + $server_key_public_exponent = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $this->server_key_public_exponent = $server_key_public_exponent; - $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2)); - $server_key_public_modulus = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256); + $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2)); + $server_key_public_modulus = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $this->server_key_public_modulus = $server_key_public_modulus; - $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4); + $this->_string_shift($response[self::RESPONSE_DATA], 4); - $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2)); - $host_key_public_exponent = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256); + $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2)); + $host_key_public_exponent = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $this->host_key_public_exponent = $host_key_public_exponent; - $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2)); - $host_key_public_modulus = new BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256); + $temp = unpack('nlen', $this->_string_shift($response[self::RESPONSE_DATA], 2)); + $host_key_public_modulus = new BigInteger($this->_string_shift($response[self::RESPONSE_DATA], ceil($temp['len'] / 8)), 256); $this->host_key_public_modulus = $host_key_public_modulus; - $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4); + $this->_string_shift($response[self::RESPONSE_DATA], 4); // get a list of the supported ciphers - extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4))); + extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[self::RESPONSE_DATA], 4))); foreach ($this->supported_ciphers as $mask=>$name) { if (($supported_ciphers_mask & (1 << $mask)) == 0) { unset($this->supported_ciphers[$mask]); @@ -600,7 +600,7 @@ class Net_SSH1 } // get a list of the supported authentications - extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4))); + extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[self::RESPONSE_DATA], 4))); foreach ($this->supported_authentications as $mask=>$name) { if (($supported_authentications_mask & (1 << $mask)) == 0) { unset($this->supported_authentications[$mask]); @@ -644,7 +644,7 @@ class Net_SSH1 ); } - $cipher = isset($this->supported_ciphers[$this->cipher]) ? $this->cipher : NET_SSH1_CIPHER_3DES; + $cipher = isset($this->supported_ciphers[$this->cipher]) ? $this->cipher : self::CIPHER_3DES; $data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0); if (!$this->_send_binary_packet($data)) { @@ -653,10 +653,10 @@ class Net_SSH1 } switch ($cipher) { - //case NET_SSH1_CIPHER_NONE: + //case self::CIPHER_NONE: // $this->crypto = new Crypt_Null(); // break; - case NET_SSH1_CIPHER_DES: + case self::CIPHER_DES: if (!class_exists('Crypt_DES')) { include_once 'Crypt/DES.php'; } @@ -665,7 +665,7 @@ class Net_SSH1 $this->crypto->enableContinuousBuffer(); $this->crypto->setKey(substr($session_key, 0, 8)); break; - case NET_SSH1_CIPHER_3DES: + case self::CIPHER_3DES: if (!class_exists('Crypt_TripleDES')) { include_once 'Crypt/TripleDES.php'; } @@ -674,7 +674,7 @@ class Net_SSH1 $this->crypto->enableContinuousBuffer(); $this->crypto->setKey(substr($session_key, 0, 24)); break; - //case NET_SSH1_CIPHER_RC4: + //case self::CIPHER_RC4: // if (!class_exists('Crypt_RC4')) { // include_once 'Crypt/RC4.php'; // } @@ -686,12 +686,12 @@ class Net_SSH1 $response = $this->_get_binary_packet(); - if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) { + if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) { user_error('Expected SSH_SMSG_SUCCESS'); return false; } - $this->bitmap = NET_SSH1_MASK_CONNECTED; + $this->bitmap = self::MASK_CONNECTED; return true; } @@ -706,14 +706,14 @@ class Net_SSH1 */ function login($username, $password = '') { - if (!($this->bitmap & NET_SSH1_MASK_CONSTRUCTOR)) { - $this->bitmap |= NET_SSH1_MASK_CONSTRUCTOR; + if (!($this->bitmap & self::MASK_CONSTRUCTOR)) { + $this->bitmap |= self::MASK_CONSTRUCTOR; if (!$this->_connect()) { return false; } } - if (!($this->bitmap & NET_SSH1_MASK_CONNECTED)) { + if (!($this->bitmap & self::MASK_CONNECTED)) { return false; } @@ -729,10 +729,10 @@ class Net_SSH1 if ($response === true) { return false; } - if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) { - $this->bitmap |= NET_SSH1_MASK_LOGIN; + if ($response[self::RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) { + $this->bitmap |= self::MASK_LOGIN; return true; - } else if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_FAILURE) { + } else if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_FAILURE) { user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE'); return false; } @@ -745,7 +745,7 @@ class Net_SSH1 } // remove the username and password from the last logged packet - if (defined('NET_SSH1_LOGGING') && NET_SSH1_LOGGING == NET_SSH1_LOG_COMPLEX) { + if (defined('NET_SSH1_LOGGING') && NET_SSH1_LOGGING == self::LOG_COMPLEX) { $data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen('password'), 'password'); $this->message_log[count($this->message_log) - 1] = $data; } @@ -755,10 +755,10 @@ class Net_SSH1 if ($response === true) { return false; } - if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) { - $this->bitmap |= NET_SSH1_MASK_LOGIN; + if ($response[self::RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) { + $this->bitmap |= self::MASK_LOGIN; return true; - } else if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_FAILURE) { + } else if ($response[self::RESPONSE_TYPE] == NET_SSH1_SMSG_FAILURE) { return false; } else { user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE'); @@ -801,7 +801,7 @@ class Net_SSH1 */ function exec($cmd, $block = true) { - if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) { + if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } @@ -822,9 +822,9 @@ class Net_SSH1 if ($response !== false) { do { - $output.= substr($response[NET_SSH1_RESPONSE_DATA], 4); + $output.= substr($response[self::RESPONSE_DATA], 4); $response = $this->_get_binary_packet(); - } while (is_array($response) && $response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_EXITSTATUS); + } while (is_array($response) && $response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_EXITSTATUS); } $data = pack('C', NET_SSH1_CMSG_EXIT_CONFIRMATION); @@ -853,7 +853,7 @@ class Net_SSH1 // connect using the sample parameters in protocol-1.5.txt. // according to wikipedia.org's entry on text terminals, "the fundamental type of application running on a text // terminal is a command line interpreter or shell". thus, opening a terminal session to run the shell. - $data = pack('CNa*N4C', NET_SSH1_CMSG_REQUEST_PTY, strlen('vt100'), 'vt100', 24, 80, 0, 0, NET_SSH1_TTY_OP_END); + $data = pack('CNa*N4C', NET_SSH1_CMSG_REQUEST_PTY, strlen('vt100'), 'vt100', 24, 80, 0, 0, self::TTY_OP_END); if (!$this->_send_binary_packet($data)) { user_error('Error sending SSH_CMSG_REQUEST_PTY'); @@ -865,7 +865,7 @@ class Net_SSH1 if ($response === true) { return false; } - if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) { + if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) { user_error('Expected SSH_SMSG_SUCCESS'); return false; } @@ -877,7 +877,7 @@ class Net_SSH1 return false; } - $this->bitmap |= NET_SSH1_MASK_SHELL; + $this->bitmap |= self::MASK_SHELL; //stream_set_blocking($this->fsock, 0); @@ -900,7 +900,7 @@ class Net_SSH1 /** * Returns the output of an interactive shell when there's a match for $expect * - * $expect can take the form of a string literal or, if $mode == NET_SSH1_READ_REGEX, + * $expect can take the form of a string literal or, if $mode == self::READ__REGEX, * a regular expression. * * @see Net_SSH1::write() @@ -909,21 +909,21 @@ class Net_SSH1 * @return Boolean * @access public */ - function read($expect, $mode = NET_SSH1_READ_SIMPLE) + function read($expect, $mode = self::READ__SIMPLE) { - if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) { + if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } - if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) { + if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } $match = $expect; while (true) { - if ($mode == NET_SSH1_READ_REGEX) { + if ($mode == self::READ__REGEX) { preg_match($expect, $this->interactiveBuffer, $matches); $match = isset($matches[0]) ? $matches[0] : ''; } @@ -936,7 +936,7 @@ class Net_SSH1 if ($response === true) { return $this->_string_shift($this->interactiveBuffer, strlen($this->interactiveBuffer)); } - $this->interactiveBuffer.= substr($response[NET_SSH1_RESPONSE_DATA], 4); + $this->interactiveBuffer.= substr($response[self::RESPONSE_DATA], 4); } } @@ -950,12 +950,12 @@ class Net_SSH1 */ function interactiveWrite($cmd) { - if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) { + if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } - if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) { + if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } @@ -985,12 +985,12 @@ class Net_SSH1 */ function interactiveRead() { - if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) { + if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } - if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) { + if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } @@ -999,7 +999,7 @@ class Net_SSH1 $write = $except = null; if (stream_select($read, $write, $except, 0)) { $response = $this->_get_binary_packet(); - return substr($response[NET_SSH1_RESPONSE_DATA], 4); + return substr($response[self::RESPONSE_DATA], 4); } else { return ''; } @@ -1042,9 +1042,9 @@ class Net_SSH1 /* $response = $this->_get_binary_packet(); if ($response === true) { - $response = array(NET_SSH1_RESPONSE_TYPE => -1); + $response = array(self::RESPONSE_TYPE => -1); } - switch ($response[NET_SSH1_RESPONSE_TYPE]) { + switch ($response[self::RESPONSE_TYPE]) { case NET_SSH1_SMSG_EXITSTATUS: $data = pack('C', NET_SSH1_CMSG_EXIT_CONFIRMATION); break; @@ -1133,8 +1133,8 @@ class Net_SSH1 } return array( - NET_SSH1_RESPONSE_TYPE => $type, - NET_SSH1_RESPONSE_DATA => $data + self::RESPONSE_TYPE => $type, + self::RESPONSE_DATA => $data ); } @@ -1382,7 +1382,7 @@ class Net_SSH1 /** * Returns a log of the packets that have been sent and received. * - * Returns a string if NET_SSH1_LOGGING == NET_SSH1_LOG_COMPLEX, an array if NET_SSH1_LOGGING == NET_SSH1_LOG_SIMPLE and false if !defined('NET_SSH1_LOGGING') + * Returns a string if NET_SSH1_LOGGING == self::LOG_COMPLEX, an array if NET_SSH1_LOGGING == self::LOG_SIMPLE and false if !defined('NET_SSH1_LOGGING') * * @access public * @return String or Array @@ -1394,10 +1394,10 @@ class Net_SSH1 } switch (NET_SSH1_LOGGING) { - case NET_SSH1_LOG_SIMPLE: + case self::LOG_SIMPLE: return $this->message_number_log; break; - case NET_SSH1_LOG_COMPLEX: + case self::LOG_COMPLEX: return $this->_format_log($this->message_log, $this->protocol_flags_log); break; default: @@ -1518,7 +1518,7 @@ class Net_SSH1 * * Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output * is set to true, returns, instead, an array of constants. ie. instead of array('Triple-DES in CBC mode'), you'll - * get array(NET_SSH1_CIPHER_3DES). + * get array(self::CIPHER_3DES). * * @param optional Boolean $raw_output * @return Array @@ -1534,7 +1534,7 @@ class Net_SSH1 * * Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output * is set to true, returns, instead, an array of constants. ie. instead of array('password authentication'), you'll - * get array(NET_SSH1_AUTH_PASSWORD). + * get array(self::AUTH_PASSWORD). * * @param optional Boolean $raw_output * @return Array @@ -1568,16 +1568,16 @@ class Net_SSH1 { switch (NET_SSH1_LOGGING) { // useful for benchmarks - case NET_SSH1_LOG_SIMPLE: + case self::LOG_SIMPLE: $this->protocol_flags_log[] = $protocol_flags; break; // the most useful log for SSH1 - case NET_SSH1_LOG_COMPLEX: + case self::LOG_COMPLEX: $this->protocol_flags_log[] = $protocol_flags; $this->_string_shift($message); $this->log_size+= strlen($message); $this->message_log[] = $message; - while ($this->log_size > NET_SSH1_LOG_MAX_SIZE) { + while ($this->log_size > self::LOG_MAX_SIZE) { $this->log_size-= strlen(array_shift($this->message_log)); array_shift($this->protocol_flags_log); } @@ -1585,19 +1585,19 @@ class Net_SSH1 // dump the output out realtime; packets may be interspersed with non packets, // passwords won't be filtered out and select other packets may not be correctly // identified - case NET_SSH1_LOG_REALTIME: + case self::LOG_REALTIME: echo "
\r\n" . $this->_format_log(array($message), array($protocol_flags)) . "\r\n
\r\n"; @flush(); @ob_flush(); break; - // basically the same thing as NET_SSH1_LOG_REALTIME with the caveat that NET_SSH1_LOG_REALTIME_FILE - // needs to be defined and that the resultant log file will be capped out at NET_SSH1_LOG_MAX_SIZE. + // basically the same thing as self::LOG_REALTIME with the caveat that self::LOG_REALTIME_FILE + // needs to be defined and that the resultant log file will be capped out at self::LOG_MAX_SIZE. // the earliest part of the log file is denoted by the first <<< START >>> and is not going to necessarily // at the beginning of the file - case NET_SSH1_LOG_REALTIME_FILE: + case self::LOG_REALTIME_FILE: if (!isset($this->realtime_log_file)) { // PHP doesn't seem to like using constants in fopen() - $filename = NET_SSH1_LOG_REALTIME_FILE; + $filename = self::LOG_REALTIME_FILE; $fp = fopen($filename, 'w'); $this->realtime_log_file = $fp; } @@ -1611,7 +1611,7 @@ class Net_SSH1 fseek($this->realtime_log_file, ftell($this->realtime_log_file) - strlen($temp)); } $this->realtime_log_size+= strlen($entry); - if ($this->realtime_log_size > NET_SSH1_LOG_MAX_SIZE) { + if ($this->realtime_log_size > self::LOG_MAX_SIZE) { fseek($this->realtime_log_file, 0); $this->realtime_log_size = strlen($entry); $this->realtime_log_wrap = true; diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index a0a5ad31..b7167c89 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -52,81 +52,6 @@ use \phpseclib\Crypt\Random; // Used to do Diffie-Hellman key exchange and DSA/RSA signature verification. use \phpseclib\Math\BigInteger; -/**#@+ - * Execution Bitmap Masks - * - * @see Net_SSH2::bitmap - * @access private - */ -define('NET_SSH2_MASK_CONSTRUCTOR', 0x00000001); -define('NET_SSH2_MASK_CONNECTED', 0x00000002); -define('NET_SSH2_MASK_LOGIN_REQ', 0x00000004); -define('NET_SSH2_MASK_LOGIN', 0x00000008); -define('NET_SSH2_MASK_SHELL', 0x00000010); -define('NET_SSH2_MASK_WINDOW_ADJUST', 0x00000020); -/**#@-*/ - -/**#@+ - * Channel constants - * - * RFC4254 refers not to client and server channels but rather to sender and recipient channels. we don't refer - * to them in that way because RFC4254 toggles the meaning. the client sends a SSH_MSG_CHANNEL_OPEN message with - * a sender channel and the server sends a SSH_MSG_CHANNEL_OPEN_CONFIRMATION in response, with a sender and a - * recepient channel. at first glance, you might conclude that SSH_MSG_CHANNEL_OPEN_CONFIRMATION's sender channel - * would be the same thing as SSH_MSG_CHANNEL_OPEN's sender channel, but it's not, per this snipet: - * The 'recipient channel' is the channel number given in the original - * open request, and 'sender channel' is the channel number allocated by - * the other side. - * - * @see Net_SSH2::_send_channel_packet() - * @see Net_SSH2::_get_channel_packet() - * @access private - */ -define('NET_SSH2_CHANNEL_EXEC', 0); // PuTTy uses 0x100 -define('NET_SSH2_CHANNEL_SHELL', 1); -define('NET_SSH2_CHANNEL_SUBSYSTEM', 2); -/**#@-*/ - -/**#@+ - * @access public - * @see Net_SSH2::getLog() - */ -/** - * Returns the message numbers - */ -define('NET_SSH2_LOG_SIMPLE', 1); -/** - * Returns the message content - */ -define('NET_SSH2_LOG_COMPLEX', 2); -/** - * Outputs the content real-time - */ -define('NET_SSH2_LOG_REALTIME', 3); -/** - * Dumps the content real-time to a file - */ -define('NET_SSH2_LOG_REALTIME_FILE', 4); -/**#@-*/ - -/**#@+ - * @access public - * @see Net_SSH2::read() - */ -/** - * Returns when a string matching $expect exactly is found - */ -define('NET_SSH2_READ_SIMPLE', 1); -/** - * Returns when a string matching the regular expression $expect is found - */ -define('NET_SSH2_READ_REGEX', 2); -/** - * Make sure that the log never gets larger than this - */ -define('NET_SSH2_LOG_MAX_SIZE', 1024 * 1024); -/**#@-*/ - /** * Pure-PHP implementation of SSHv2. * @@ -136,6 +61,81 @@ define('NET_SSH2_LOG_MAX_SIZE', 1024 * 1024); */ class Net_SSH2 { + /**#@+ + * Execution Bitmap Masks + * + * @see Net_SSH2::bitmap + * @access private + */ + const MASK_CONSTRUCTOR = 0x00000001; + const MASK_CONNECTED = 0x00000002; + const MASK_LOGIN_REQ = 0x00000004; + const MASK_LOGIN = 0x00000008; + const MASK_SHELL = 0x00000010; + const MASK_WINDOW_ADJUST = 0x00000020; + /**#@-*/ + + /**#@+ + * Channel constants + * + * RFC4254 refers not to client and server channels but rather to sender and recipient channels. we don't refer + * to them in that way because RFC4254 toggles the meaning. the client sends a SSH_MSG_CHANNEL_OPEN message with + * a sender channel and the server sends a SSH_MSG_CHANNEL_OPEN_CONFIRMATION in response, with a sender and a + * recepient channel. at first glance, you might conclude that SSH_MSG_CHANNEL_OPEN_CONFIRMATION's sender channel + * would be the same thing as SSH_MSG_CHANNEL_OPEN's sender channel, but it's not, per this snipet: + * The 'recipient channel' is the channel number given in the original + * open request, and 'sender channel' is the channel number allocated by + * the other side. + * + * @see Net_SSH2::_send_channel_packet() + * @see Net_SSH2::_get_channel_packet() + * @access private + */ + const CHANNEL_EXEC = 0; // PuTTy uses 0x100 + const CHANNEL_SHELL = 1; + const CHANNEL_SUBSYSTEM = 2; + /**#@-*/ + + /**#@+ + * @access public + * @see Net_SSH2::getLog() + */ + /** + * Returns the message numbers + */ + const LOG_SIMPLE = 1; + /** + * Returns the message content + */ + const LOG_COMPLEX = 2; + /** + * Outputs the content real-time + */ + const LOG_REALTIME = 3; + /** + * Dumps the content real-time to a file + */ + const LOG_REALTIME_FILE = 4; + /**#@-*/ + + /**#@+ + * @access public + * @see Net_SSH2::read() + */ + /** + * Returns when a string matching $expect exactly is found + */ + const READ_SIMPLE = 1; + /** + * Returns when a string matching the regular expression $expect is found + */ + const READ_REGEX = 2; + /** + * Make sure that the log never gets larger than this + */ + const LOG_MAX_SIZE = 1048576; // 1024 * 1024 + /**#@-*/ + /** * The SSH identifier * @@ -585,7 +585,7 @@ class Net_SSH2 /** * Current log size * - * Should never exceed NET_SSH2_LOG_MAX_SIZE + * Should never exceed self::LOG_MAX_SIZE * * @see Net_SSH2::_send_binary_packet() * @see Net_SSH2::_get_binary_packet() @@ -925,11 +925,11 @@ class Net_SSH2 */ function _connect() { - if ($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) { + if ($this->bitmap & self::MASK_CONSTRUCTOR) { return false; } - $this->bitmap |= NET_SSH2_MASK_CONSTRUCTOR; + $this->bitmap |= self::MASK_CONSTRUCTOR; $timeout = $this->connectionTimeout; $host = $this->host . ':' . $this->port; @@ -1020,7 +1020,7 @@ class Net_SSH2 return false; } - $this->bitmap|= NET_SSH2_MASK_CONNECTED; + $this->bitmap|= self::MASK_CONNECTED; return true; } @@ -1792,7 +1792,7 @@ class Net_SSH2 */ function _login($username) { - if (!($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR)) { + if (!($this->bitmap & self::MASK_CONSTRUCTOR)) { if (!$this->_connect()) { return false; } @@ -1823,11 +1823,11 @@ class Net_SSH2 */ function _login_helper($username, $password = null) { - if (!($this->bitmap & NET_SSH2_MASK_CONNECTED)) { + if (!($this->bitmap & self::MASK_CONNECTED)) { return false; } - if (!($this->bitmap & NET_SSH2_MASK_LOGIN_REQ)) { + if (!($this->bitmap & self::MASK_LOGIN_REQ)) { $packet = pack('CNa*', NET_SSH2_MSG_SERVICE_REQUEST, strlen('ssh-userauth'), 'ssh-userauth' ); @@ -1848,7 +1848,7 @@ class Net_SSH2 user_error('Expected SSH_MSG_SERVICE_ACCEPT'); return false; } - $this->bitmap |= NET_SSH2_MASK_LOGIN_REQ; + $this->bitmap |= self::MASK_LOGIN_REQ; } if (strlen($this->last_interactive_response)) { @@ -1867,7 +1867,7 @@ class Net_SSH2 if (is_array($password)) { if ($this->_keyboard_interactive_login($username, $password)) { - $this->bitmap |= NET_SSH2_MASK_LOGIN; + $this->bitmap |= self::MASK_LOGIN; return true; } return false; @@ -1893,7 +1893,7 @@ class Net_SSH2 switch ($type) { case NET_SSH2_MSG_USERAUTH_SUCCESS: - $this->bitmap |= NET_SSH2_MASK_LOGIN; + $this->bitmap |= self::MASK_LOGIN; return true; //case NET_SSH2_MSG_USERAUTH_FAILURE: default: @@ -1946,14 +1946,14 @@ class Net_SSH2 if (!$partial_success && in_array('keyboard-interactive', $auth_methods)) { if ($this->_keyboard_interactive_login($username, $password)) { - $this->bitmap |= NET_SSH2_MASK_LOGIN; + $this->bitmap |= self::MASK_LOGIN; return true; } return false; } return false; case NET_SSH2_MSG_USERAUTH_SUCCESS: - $this->bitmap |= NET_SSH2_MASK_LOGIN; + $this->bitmap |= self::MASK_LOGIN; return true; } @@ -2073,7 +2073,7 @@ class Net_SSH2 return false; } - if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) { + if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == self::LOG_COMPLEX) { $this->message_number_log[count($this->message_number_log) - 1] = str_replace( 'UNKNOWN', 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE', @@ -2171,7 +2171,7 @@ class Net_SSH2 case NET_SSH2_MSG_USERAUTH_PK_OK: // we'll just take it on faith that the public key blob and the public key algorithm name are as // they should be - if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) { + if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == self::LOG_COMPLEX) { $this->message_number_log[count($this->message_number_log) - 1] = str_replace( 'UNKNOWN', 'NET_SSH2_MSG_USERAUTH_PK_OK', @@ -2203,7 +2203,7 @@ class Net_SSH2 // either the login is bad or the server employs multi-factor authentication return false; case NET_SSH2_MSG_USERAUTH_SUCCESS: - $this->bitmap |= NET_SSH2_MASK_LOGIN; + $this->bitmap |= self::MASK_LOGIN; return true; } @@ -2237,7 +2237,7 @@ class Net_SSH2 /** * Execute Command * - * If $callback is set to false then Net_SSH2::_get_channel_packet(NET_SSH2_CHANNEL_EXEC) will need to be called manually. + * If $callback is set to false then Net_SSH2::_get_channel_packet(self::CHANNEL_EXEC) will need to be called manually. * In all likelihood, this is not a feature you want to be taking advantage of. * * @param String $command @@ -2251,7 +2251,7 @@ class Net_SSH2 $this->is_timeout = false; $this->stdErrorLog = ''; - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & self::MASK_LOGIN)) { return false; } @@ -2259,21 +2259,21 @@ class Net_SSH2 // be adjusted". 0x7FFFFFFF is, at 2GB, the max size. technically, it should probably be decremented, but, // honestly, if you're transfering more than 2GB, you probably shouldn't be using phpseclib, anyway. // see http://tools.ietf.org/html/rfc4254#section-5.2 for more info - $this->window_size_server_to_client[NET_SSH2_CHANNEL_EXEC] = $this->window_size; + $this->window_size_server_to_client[self::CHANNEL_EXEC] = $this->window_size; // 0x8000 is the maximum max packet size, per http://tools.ietf.org/html/rfc4253#section-6.1, although since PuTTy // uses 0x4000, that's what will be used here, as well. $packet_size = 0x4000; $packet = pack('CNa*N3', - NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_EXEC, $this->window_size_server_to_client[NET_SSH2_CHANNEL_EXEC], $packet_size); + NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL_EXEC, $this->window_size_server_to_client[self::CHANNEL_EXEC], $packet_size); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_OPEN; + $this->channel_status[self::CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_OPEN; - $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC); + $response = $this->_get_channel_packet(self::CHANNEL_EXEC); if ($response === false) { return false; } @@ -2281,7 +2281,7 @@ class Net_SSH2 if ($this->request_pty === true) { $terminal_modes = pack('C', NET_SSH2_TTY_OP_END); $packet = pack('CNNa*CNa*N5a*', - NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_EXEC], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100', + NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_EXEC], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100', $this->windowColumns, $this->windowRows, 0, 0, strlen($terminal_modes), $terminal_modes); if (!$this->_send_binary_packet($packet)) { @@ -2316,19 +2316,19 @@ class Net_SSH2 // SSH_MSG_CHANNEL_OPEN_CONFIRMATION, RFC4254#section-5.1 states that the "maximum packet size" refers to the // "maximum size of an individual data packet". ie. SSH_MSG_CHANNEL_DATA. RFC4254#section-5.2 corroborates. $packet = pack('CNNa*CNa*', - NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_EXEC], strlen('exec'), 'exec', 1, strlen($command), $command); + NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_EXEC], strlen('exec'), 'exec', 1, strlen($command), $command); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_REQUEST; + $this->channel_status[self::CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_REQUEST; - $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC); + $response = $this->_get_channel_packet(self::CHANNEL_EXEC); if ($response === false) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_DATA; + $this->channel_status[self::CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_DATA; if ($callback === false || $this->in_request_pty_exec) { return true; @@ -2336,7 +2336,7 @@ class Net_SSH2 $output = ''; while (true) { - $temp = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC); + $temp = $this->_get_channel_packet(self::CHANNEL_EXEC); switch (true) { case $temp === true: return is_callable($callback) ? true : $output; @@ -2345,7 +2345,7 @@ class Net_SSH2 default: if (is_callable($callback)) { if (call_user_func($callback, $temp) === true) { - $this->_close_channel(NET_SSH2_CHANNEL_EXEC); + $this->_close_channel(self::CHANNEL_EXEC); return true; } } else { @@ -2369,26 +2369,26 @@ class Net_SSH2 return true; } - $this->window_size_server_to_client[NET_SSH2_CHANNEL_SHELL] = $this->window_size; + $this->window_size_server_to_client[self::CHANNEL_SHELL] = $this->window_size; $packet_size = 0x4000; $packet = pack('CNa*N3', - NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_SHELL, $this->window_size_server_to_client[NET_SSH2_CHANNEL_SHELL], $packet_size); + NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL_SHELL, $this->window_size_server_to_client[self::CHANNEL_SHELL], $packet_size); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_OPEN; + $this->channel_status[self::CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_OPEN; - $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL); + $response = $this->_get_channel_packet(self::CHANNEL_SHELL); if ($response === false) { return false; } $terminal_modes = pack('C', NET_SSH2_TTY_OP_END); $packet = pack('CNNa*CNa*N5a*', - NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SHELL], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100', + NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_SHELL], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100', $this->windowColumns, $this->windowRows, 0, 0, strlen($terminal_modes), $terminal_modes); if (!$this->_send_binary_packet($packet)) { @@ -2414,21 +2414,21 @@ class Net_SSH2 } $packet = pack('CNNa*C', - NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SHELL], strlen('shell'), 'shell', 1); + NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_SHELL], strlen('shell'), 'shell', 1); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_REQUEST; + $this->channel_status[self::CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_REQUEST; - $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL); + $response = $this->_get_channel_packet(self::CHANNEL_SHELL); if ($response === false) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_DATA; + $this->channel_status[self::CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_DATA; - $this->bitmap |= NET_SSH2_MASK_SHELL; + $this->bitmap |= self::MASK_SHELL; return true; } @@ -2445,11 +2445,11 @@ class Net_SSH2 { switch (true) { case $this->in_subsystem: - return NET_SSH2_CHANNEL_SUBSYSTEM; + return self::CHANNEL_SUBSYSTEM; case $this->in_request_pty_exec: - return NET_SSH2_CHANNEL_EXEC; + return self::CHANNEL_EXEC; default: - return NET_SSH2_CHANNEL_SHELL; + return self::CHANNEL_SHELL; } } @@ -2457,7 +2457,7 @@ class Net_SSH2 * Returns the output of an interactive shell * * Returns when there's a match for $expect, which can take the form of a string literal or, - * if $mode == NET_SSH2_READ_REGEX, a regular expression. + * if $mode == self::READ_REGEX, a regular expression. * * @see Net_SSH2::write() * @param String $expect @@ -2465,17 +2465,17 @@ class Net_SSH2 * @return String * @access public */ - function read($expect = '', $mode = NET_SSH2_READ_SIMPLE) + function read($expect = '', $mode = self::READ_SIMPLE) { $this->curTimeout = $this->timeout; $this->is_timeout = false; - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } - if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) { + if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } @@ -2484,7 +2484,7 @@ class Net_SSH2 $match = $expect; while (true) { - if ($mode == NET_SSH2_READ_REGEX) { + if ($mode == self::READ_REGEX) { preg_match($expect, $this->interactiveBuffer, $matches); $match = isset($matches[0]) ? $matches[0] : ''; } @@ -2512,12 +2512,12 @@ class Net_SSH2 */ function write($cmd) { - if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (!($this->bitmap & self::MASK_LOGIN)) { user_error('Operation disallowed prior to login()'); return false; } - if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) { + if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) { user_error('Unable to initiate an interactive shell session'); return false; } @@ -2541,39 +2541,39 @@ class Net_SSH2 */ function startSubsystem($subsystem) { - $this->window_size_server_to_client[NET_SSH2_CHANNEL_SUBSYSTEM] = $this->window_size; + $this->window_size_server_to_client[self::CHANNEL_SUBSYSTEM] = $this->window_size; $packet = pack('CNa*N3', - NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_SUBSYSTEM, $this->window_size, 0x4000); + NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', self::CHANNEL_SUBSYSTEM, $this->window_size, 0x4000); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_OPEN; + $this->channel_status[self::CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_OPEN; - $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SUBSYSTEM); + $response = $this->_get_channel_packet(self::CHANNEL_SUBSYSTEM); if ($response === false) { return false; } $packet = pack('CNNa*CNa*', - NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SUBSYSTEM], strlen('subsystem'), 'subsystem', 1, strlen($subsystem), $subsystem); + NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[self::CHANNEL_SUBSYSTEM], strlen('subsystem'), 'subsystem', 1, strlen($subsystem), $subsystem); if (!$this->_send_binary_packet($packet)) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_REQUEST; + $this->channel_status[self::CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_REQUEST; - $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SUBSYSTEM); + $response = $this->_get_channel_packet(self::CHANNEL_SUBSYSTEM); if ($response === false) { return false; } - $this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_DATA; + $this->channel_status[self::CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_DATA; - $this->bitmap |= NET_SSH2_MASK_SHELL; + $this->bitmap |= self::MASK_SHELL; $this->in_subsystem = true; return true; @@ -2589,7 +2589,7 @@ class Net_SSH2 function stopSubsystem() { $this->in_subsystem = false; - $this->_close_channel(NET_SSH2_CHANNEL_SUBSYSTEM); + $this->_close_channel(self::CHANNEL_SUBSYSTEM); return true; } @@ -2651,7 +2651,7 @@ class Net_SSH2 */ function isConnected() { - return (bool) ($this->bitmap & NET_SSH2_MASK_CONNECTED); + return (bool) ($this->bitmap & self::MASK_CONNECTED); } /** @@ -2787,7 +2787,7 @@ class Net_SSH2 } // see http://tools.ietf.org/html/rfc4252#section-5.4; only called when the encryption has been activated and when we haven't already logged in - if (($this->bitmap & NET_SSH2_MASK_CONNECTED) && !($this->bitmap & NET_SSH2_MASK_LOGIN) && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) { + if (($this->bitmap & self::MASK_CONNECTED) && !($this->bitmap & self::MASK_LOGIN) && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) { $this->_string_shift($payload, 1); extract(unpack('Nlength', $this->_string_shift($payload, 4))); $this->banner_message = utf8_decode($this->_string_shift($payload, $length)); @@ -2795,7 +2795,7 @@ class Net_SSH2 } // only called when we've already logged in - if (($this->bitmap & NET_SSH2_MASK_CONNECTED) && ($this->bitmap & NET_SSH2_MASK_LOGIN)) { + if (($this->bitmap & self::MASK_CONNECTED) && ($this->bitmap & self::MASK_LOGIN)) { switch (ord($payload[0])) { case NET_SSH2_MSG_GLOBAL_REQUEST: // see http://tools.ietf.org/html/rfc4254#section-4 $this->_string_shift($payload, 1); @@ -2831,7 +2831,7 @@ class Net_SSH2 extract(unpack('Nwindow_size', $this->_string_shift($payload, 4))); $this->window_size_client_to_server[$channel]+= $window_size; - $payload = ($this->bitmap & NET_SSH2_MASK_WINDOW_ADJUST) ? true : $this->_get_binary_packet(); + $payload = ($this->bitmap & self::MASK_WINDOW_ADJUST) ? true : $this->_get_binary_packet(); } } @@ -3008,7 +3008,7 @@ class Net_SSH2 switch ($type) { case NET_SSH2_MSG_CHANNEL_DATA: /* - if ($channel == NET_SSH2_CHANNEL_EXEC) { + if ($channel == self::CHANNEL_EXEC) { // SCP requires null packets, such as this, be sent. further, in the case of the ssh.com SSH server // this actually seems to make things twice as fast. more to the point, the message right after // SSH_MSG_CHANNEL_DATA (usually SSH_MSG_IGNORE) won't block for as long as it would have otherwise. @@ -3028,7 +3028,7 @@ class Net_SSH2 break; case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA: /* - if ($client_channel == NET_SSH2_CHANNEL_EXEC) { + if ($client_channel == self::CHANNEL_EXEC) { $this->_send_channel_packet($client_channel, chr(0)); } */ @@ -3084,8 +3084,8 @@ class Net_SSH2 case NET_SSH2_MSG_CHANNEL_CLOSE: $this->curTimeout = 0; - if ($this->bitmap & NET_SSH2_MASK_SHELL) { - $this->bitmap&= ~NET_SSH2_MASK_SHELL; + if ($this->bitmap & self::MASK_SHELL) { + $this->bitmap&= ~self::MASK_SHELL; } if ($this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_EOF) { $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel])); @@ -3180,15 +3180,15 @@ class Net_SSH2 switch (NET_SSH2_LOGGING) { // useful for benchmarks - case NET_SSH2_LOG_SIMPLE: + case self::LOG_SIMPLE: $this->message_number_log[] = $message_number; break; // the most useful log for SSH2 - case NET_SSH2_LOG_COMPLEX: + case self::LOG_COMPLEX: $this->message_number_log[] = $message_number; $this->log_size+= strlen($message); $this->message_log[] = $message; - while ($this->log_size > NET_SSH2_LOG_MAX_SIZE) { + while ($this->log_size > self::LOG_MAX_SIZE) { $this->log_size-= strlen(array_shift($this->message_log)); array_shift($this->message_number_log); } @@ -3196,7 +3196,7 @@ class Net_SSH2 // dump the output out realtime; packets may be interspersed with non packets, // passwords won't be filtered out and select other packets may not be correctly // identified - case NET_SSH2_LOG_REALTIME: + case self::LOG_REALTIME: switch (PHP_SAPI) { case 'cli': $start = $stop = "\r\n"; @@ -3209,14 +3209,14 @@ class Net_SSH2 @flush(); @ob_flush(); break; - // basically the same thing as NET_SSH2_LOG_REALTIME with the caveat that NET_SSH2_LOG_REALTIME_FILE - // needs to be defined and that the resultant log file will be capped out at NET_SSH2_LOG_MAX_SIZE. + // basically the same thing as self::LOG_REALTIME with the caveat that self::LOG_REALTIME_FILE + // needs to be defined and that the resultant log file will be capped out at self::LOG_MAX_SIZE. // the earliest part of the log file is denoted by the first <<< START >>> and is not going to necessarily // at the beginning of the file - case NET_SSH2_LOG_REALTIME_FILE: + case self::LOG_REALTIME_FILE: if (!isset($this->realtime_log_file)) { // PHP doesn't seem to like using constants in fopen() - $filename = NET_SSH2_LOG_REALTIME_FILENAME; + $filename = self::LOG_REALTIME_FILENAME; $fp = fopen($filename, 'w'); $this->realtime_log_file = $fp; } @@ -3230,7 +3230,7 @@ class Net_SSH2 fseek($this->realtime_log_file, ftell($this->realtime_log_file) - strlen($temp)); } $this->realtime_log_size+= strlen($entry); - if ($this->realtime_log_size > NET_SSH2_LOG_MAX_SIZE) { + if ($this->realtime_log_size > self::LOG_MAX_SIZE) { fseek($this->realtime_log_file, 0); $this->realtime_log_size = strlen($entry); $this->realtime_log_wrap = true; @@ -3262,10 +3262,10 @@ class Net_SSH2 ); while (strlen($data) > $max_size) { if (!$this->window_size_client_to_server[$client_channel]) { - $this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST; + $this->bitmap^= self::MASK_WINDOW_ADJUST; // using an invalid channel will let the buffers be built up for the valid channels $output = $this->_get_channel_packet(-1); - $this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST; + $this->bitmap^= self::MASK_WINDOW_ADJUST; $max_size = min( $this->packet_size_client_to_server[$client_channel], $this->window_size_client_to_server[$client_channel] @@ -3288,9 +3288,9 @@ class Net_SSH2 } if (strlen($data) >= $this->window_size_client_to_server[$client_channel]) { - $this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST; + $this->bitmap^= self::MASK_WINDOW_ADJUST; $this->_get_channel_packet(-1); - $this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST; + $this->bitmap^= self::MASK_WINDOW_ADJUST; } $this->window_size_client_to_server[$client_channel]-= strlen($data); @@ -3334,8 +3334,8 @@ class Net_SSH2 $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel])); } - if ($this->bitmap & NET_SSH2_MASK_SHELL) { - $this->bitmap&= ~NET_SSH2_MASK_SHELL; + if ($this->bitmap & self::MASK_SHELL) { + $this->bitmap&= ~self::MASK_SHELL; } } @@ -3401,7 +3401,7 @@ class Net_SSH2 /** * Returns a log of the packets that have been sent and received. * - * Returns a string if NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX, an array if NET_SSH2_LOGGING == NET_SSH2_LOG_SIMPLE and false if !defined('NET_SSH2_LOGGING') + * Returns a string if NET_SSH2_LOGGING == self::LOG_COMPLEX, an array if NET_SSH2_LOGGING == self::LOG_SIMPLE and false if !defined('NET_SSH2_LOGGING') * * @access public * @return String or Array @@ -3413,10 +3413,10 @@ class Net_SSH2 } switch (NET_SSH2_LOGGING) { - case NET_SSH2_LOG_SIMPLE: + case self::LOG_SIMPLE: return $this->message_number_log; break; - case NET_SSH2_LOG_COMPLEX: + case self::LOG_COMPLEX: return $this->_format_log($this->message_log, $this->message_number_log); break; default: @@ -3662,7 +3662,7 @@ class Net_SSH2 */ function getServerPublicHostKey() { - if (!($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR)) { + if (!($this->bitmap & self::MASK_CONSTRUCTOR)) { if (!$this->_connect()) { return false; }