Net: add public / private / protected

This commit is contained in:
terrafrost 2017-01-07 19:51:56 -06:00
parent fae358cc3c
commit b9b4f67a0f
11 changed files with 896 additions and 794 deletions

View File

@ -0,0 +1,75 @@
<?php
/**
* Common Object Functions
*
* PHP version 5
*
* @category Common
* @package Functions\Objects
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2016 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Common\Functions;
/**
* Common Object Functions
*
* @package Functions\Objects
* @author Jim Wigginton <terrafrost@php.net>
*/
abstract class Objects
{
/**
* Accesses a private variable from an object
*
* @param Object $obj
* @param string $var
* @return mixed
* @access private
*/
public static function getVar($obj, $var)
{
$reflection = new \ReflectionClass(get_class($obj));
$prop = $reflection->getProperty($var);
$prop->setAccessible(true);
return $prop->getValue($obj);
}
/**
* Sets the value of a private variable in an object
*
* @param Object $obj
* @param string $var
* @param mixed $val
* @return mixed
* @access private
*/
public static function setVar($obj, $var, $val)
{
$reflection = new \ReflectionClass(get_class($obj));
$prop = $reflection->getProperty($var);
$prop->setAccessible(true);
return $prop->setValue($obj, $val);
}
/**
* Accesses a private method from an object
*
* @param Object $obj
* @param string $func
* @param array $params
* @return mixed
* @access private
*/
public static function callFunc($obj, $func, $params = array())
{
$reflection = new \ReflectionClass(get_class($obj));
$method = $reflection->getMethod($func);
$method->setAccessible(true);
return $method->invokeArgs($obj, $params);
}
}

View File

@ -34,6 +34,7 @@ namespace phpseclib\Net;
use phpseclib\Exception\FileNotFoundException;
use phpseclib\Common\Functions\Strings;
use phpseclib\Common\Functions\Objects;
/**
* Pure-PHP implementations of SCP.
@ -79,7 +80,7 @@ class SCP
* @var object
* @access private
*/
var $ssh;
private $ssh;
/**
* Packet Size
@ -87,7 +88,7 @@ class SCP
* @var int
* @access private
*/
var $packet_size;
private $packet_size;
/**
* Mode
@ -95,7 +96,7 @@ class SCP
* @var int
* @access private
*/
var $mode;
private $mode;
/**
* Default Constructor.
@ -106,7 +107,7 @@ class SCP
* @return \phpseclib\Net\SCP
* @access public
*/
function __construct($ssh)
public function __construct($ssh)
{
if ($ssh instanceof SSH2) {
$this->mode = self::MODE_SSH2;
@ -142,7 +143,7 @@ class SCP
* @return bool
* @access public
*/
function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
public function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
{
if (!isset($this->ssh)) {
return false;
@ -152,13 +153,13 @@ class SCP
return false;
}
$temp = $this->_receive();
$temp = $this->receive();
if ($temp !== chr(0)) {
return false;
}
if ($this->mode == self::MODE_SSH2) {
$this->packet_size = $this->ssh->packet_size_client_to_server[SSH2::CHANNEL_EXEC] - 4;
$this->packet_size = Objects::getVar($this->ssh, 'packet_size_client_to_server')[SSH2::CHANNEL_EXEC] - 4;
}
$remote_file = basename($remote_file);
@ -177,9 +178,9 @@ class SCP
$size = filesize($data);
}
$this->_send('C0644 ' . $size . ' ' . $remote_file . "\n");
$this->send('C0644 ' . $size . ' ' . $remote_file . "\n");
$temp = $this->_receive();
$temp = $this->receive();
if ($temp !== chr(0)) {
return false;
}
@ -187,14 +188,14 @@ class SCP
$sent = 0;
while ($sent < $size) {
$temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
$this->_send($temp);
$this->send($temp);
$sent+= strlen($temp);
if (is_callable($callback)) {
call_user_func($callback, $sent);
}
}
$this->_close();
$this->close();
if ($mode != self::SOURCE_STRING) {
fclose($fp);
@ -215,7 +216,7 @@ class SCP
* @return mixed
* @access public
*/
function get($remote_file, $local_file = false)
public function get($remote_file, $local_file = false)
{
if (!isset($this->ssh)) {
return false;
@ -225,13 +226,13 @@ class SCP
return false;
}
$this->_send("\0");
$this->send("\0");
if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) {
if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->receive()), $info)) {
return false;
}
$this->_send("\0");
$this->send("\0");
$size = 0;
@ -244,7 +245,7 @@ class SCP
$content = '';
while ($size < $info['size']) {
$data = $this->_receive();
$data = $this->receive();
// SCP usually seems to split stuff out into 16k chunks
$size+= strlen($data);
@ -255,7 +256,7 @@ class SCP
}
}
$this->_close();
$this->close();
if ($local_file !== false) {
fclose($fp);
@ -271,15 +272,15 @@ class SCP
* @param string $data
* @access private
*/
function _send($data)
private function send($data)
{
switch ($this->mode) {
case self::MODE_SSH2:
$this->ssh->_send_channel_packet(SSH2::CHANNEL_EXEC, $data);
Objects::callFunc($this->ssh, 'send_channel_packet', [SSH2::CHANNEL_EXEC, $data]);
break;
case self::MODE_SSH1:
$data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data);
$this->ssh->_send_binary_packet($data);
Objects::callFunc($this->ssh, 'send_binary_packet', [$data]);
}
}
@ -290,17 +291,17 @@ class SCP
* @throws \UnexpectedValueException on receipt of an unexpected packet
* @access private
*/
function _receive()
private function receive()
{
switch ($this->mode) {
case self::MODE_SSH2:
return $this->ssh->_get_channel_packet(SSH2::CHANNEL_EXEC, true);
return Objects::callFunc($this->ssh, 'get_channel_packet', [SSH2::CHANNEL_EXEC, true]);
case self::MODE_SSH1:
if (!$this->ssh->bitmap) {
if (!Objects::getVar($this->ssh, 'bitmap')) {
return false;
}
while (true) {
$response = $this->ssh->_get_binary_packet();
$response = Objects::getFunc($this->ssh, 'get_binary_packet');
switch ($response[SSH1::RESPONSE_TYPE]) {
case NET_SSH1_SMSG_STDOUT_DATA:
extract(unpack('Nlength', $response[SSH1::RESPONSE_DATA]));
@ -308,9 +309,9 @@ class SCP
case NET_SSH1_SMSG_STDERR_DATA:
break;
case NET_SSH1_SMSG_EXITSTATUS:
$this->ssh->_send_binary_packet(chr(NET_SSH1_CMSG_EXIT_CONFIRMATION));
fclose($this->ssh->fsock);
$this->ssh->bitmap = 0;
Objects::callFunc($this->ssh, 'send_binary_packet', [chr(NET_SSH1_CMSG_EXIT_CONFIRMATION)]);
fclose(Objects::getVar($this->ssh, 'fsock'));
Objects::setVar($this->ssh, 'bitmap', 0);
return false;
default:
throw new \UnexpectedValueException('Unknown packet received');
@ -324,14 +325,14 @@ class SCP
*
* @access private
*/
function _close()
private function close()
{
switch ($this->mode) {
case self::MODE_SSH2:
$this->ssh->_close_channel(SSH2::CHANNEL_EXEC, true);
Objects::callFunc($this->ssh, 'close_channel', [SSH2::CHANNEL_EXEC, true]);
break;
case self::MODE_SSH1:
$this->ssh->disconnect();
Objects::callFunc($this->ssh, 'disconnect');
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -45,7 +45,7 @@ class Stream
* @var object
* @access private
*/
var $sftp;
private $sftp;
/**
* Path
@ -53,7 +53,7 @@ class Stream
* @var string
* @access private
*/
var $path;
private $path;
/**
* Mode
@ -61,7 +61,7 @@ class Stream
* @var string
* @access private
*/
var $mode;
private $mode;
/**
* Position
@ -69,7 +69,7 @@ class Stream
* @var int
* @access private
*/
var $pos;
private $pos;
/**
* Size
@ -77,7 +77,7 @@ class Stream
* @var int
* @access private
*/
var $size;
private $size;
/**
* Directory entries
@ -85,7 +85,7 @@ class Stream
* @var array
* @access private
*/
var $entries;
private $entries;
/**
* EOF flag
@ -93,7 +93,7 @@ class Stream
* @var bool
* @access private
*/
var $eof;
private $eof;
/**
* Context resource
@ -103,7 +103,7 @@ class Stream
* @var resource
* @access public
*/
var $context;
public $context;
/**
* Notification callback function
@ -111,7 +111,7 @@ class Stream
* @var callable
* @access public
*/
var $notification;
private $notification;
/**
* Registers this class as a URL wrapper.
@ -120,7 +120,7 @@ class Stream
* @return bool True on success, false otherwise.
* @access public
*/
static function register($protocol = 'sftp')
public static function register($protocol = 'sftp')
{
if (in_array($protocol, stream_get_wrappers(), true)) {
return false;
@ -133,7 +133,7 @@ class Stream
*
* @access public
*/
function __construct()
public function __construct()
{
if (defined('NET_SFTP_STREAM_LOGGING')) {
echo "__construct()\r\n";
@ -152,7 +152,7 @@ class Stream
* @return string
* @access private
*/
function _parse_path($path)
private function parse_path($path)
{
$orig = $path;
extract(parse_url($path) + ['port' => 22]);
@ -257,9 +257,9 @@ class Stream
* @return bool
* @access public
*/
function _stream_open($path, $mode, $options, &$opened_path)
private function _stream_open($path, $mode, $options, &$opened_path)
{
$path = $this->_parse_path($path);
$path = $this->parse_path($path);
if ($path === false) {
return false;
@ -299,7 +299,7 @@ class Stream
* @return mixed
* @access public
*/
function _stream_read($count)
private function _stream_read($count)
{
switch ($this->mode) {
case 'w':
@ -341,7 +341,7 @@ class Stream
* @return mixed
* @access public
*/
function _stream_write($data)
private function _stream_write($data)
{
switch ($this->mode) {
case 'r':
@ -375,7 +375,7 @@ class Stream
* @return int
* @access public
*/
function _stream_tell()
private function _stream_tell()
{
return $this->pos;
}
@ -393,7 +393,7 @@ class Stream
* @return bool
* @access public
*/
function _stream_eof()
private function _stream_eof()
{
return $this->eof;
}
@ -406,7 +406,7 @@ class Stream
* @return bool
* @access public
*/
function _stream_seek($offset, $whence)
private function _stream_seek($offset, $whence)
{
switch ($whence) {
case SEEK_SET:
@ -435,9 +435,9 @@ class Stream
* @return bool
* @access public
*/
function _stream_metadata($path, $option, $var)
private function _stream_metadata($path, $option, $var)
{
$path = $this->_parse_path($path);
$path = $this->parse_path($path);
if ($path === false) {
return false;
}
@ -467,7 +467,7 @@ class Stream
* @return resource
* @access public
*/
function _stream_cast($cast_as)
private function _stream_cast($cast_as)
{
return $this->sftp->fsock;
}
@ -479,7 +479,7 @@ class Stream
* @return bool
* @access public
*/
function _stream_lock($operation)
private function _stream_lock($operation)
{
return false;
}
@ -496,7 +496,7 @@ class Stream
* @return bool
* @access public
*/
function _rename($path_from, $path_to)
private function _rename($path_from, $path_to)
{
$path1 = parse_url($path_from);
$path2 = parse_url($path_to);
@ -505,7 +505,7 @@ class Stream
return false;
}
$path_from = $this->_parse_path($path_from);
$path_from = $this->parse_path($path_from);
$path_to = parse_url($path_to);
if ($path_from === false) {
return false;
@ -548,9 +548,9 @@ class Stream
* @return bool
* @access public
*/
function _dir_opendir($path, $options)
private function _dir_opendir($path, $options)
{
$path = $this->_parse_path($path);
$path = $this->parse_path($path);
if ($path === false) {
return false;
}
@ -565,7 +565,7 @@ class Stream
* @return mixed
* @access public
*/
function _dir_readdir()
private function _dir_readdir()
{
if (isset($this->entries[$this->pos])) {
return $this->entries[$this->pos++];
@ -579,7 +579,7 @@ class Stream
* @return bool
* @access public
*/
function _dir_rewinddir()
private function _dir_rewinddir()
{
$this->pos = 0;
return true;
@ -591,7 +591,7 @@ class Stream
* @return bool
* @access public
*/
function _dir_closedir()
private function _dir_closedir()
{
return true;
}
@ -607,9 +607,9 @@ class Stream
* @return bool
* @access public
*/
function _mkdir($path, $mode, $options)
private function _mkdir($path, $mode, $options)
{
$path = $this->_parse_path($path);
$path = $this->parse_path($path);
if ($path === false) {
return false;
}
@ -631,9 +631,9 @@ class Stream
* @return bool
* @access public
*/
function _rmdir($path, $options)
private function _rmdir($path, $options)
{
$path = $this->_parse_path($path);
$path = $this->parse_path($path);
if ($path === false) {
return false;
}
@ -649,7 +649,7 @@ class Stream
* @return bool
* @access public
*/
function _stream_flush()
private function _stream_flush()
{
return true;
}
@ -660,7 +660,7 @@ class Stream
* @return mixed
* @access public
*/
function _stream_stat()
private function _stream_stat()
{
$results = $this->sftp->stat($this->path);
if ($results === false) {
@ -676,9 +676,9 @@ class Stream
* @return bool
* @access public
*/
function _unlink($path)
private function _unlink($path)
{
$path = $this->_parse_path($path);
$path = $this->parse_path($path);
if ($path === false) {
return false;
}
@ -698,9 +698,9 @@ class Stream
* @return mixed
* @access public
*/
function _url_stat($path, $flags)
private function _url_stat($path, $flags)
{
$path = $this->_parse_path($path);
$path = $this->parse_path($path);
if ($path === false) {
return false;
}
@ -720,7 +720,7 @@ class Stream
* @return bool
* @access public
*/
function _stream_truncate($new_size)
private function _stream_truncate($new_size)
{
if (!$this->sftp->truncate($this->path, $new_size)) {
return false;
@ -744,7 +744,7 @@ class Stream
* @return bool
* @access public
*/
function _stream_set_option($option, $arg1, $arg2)
private function _stream_set_option($option, $arg1, $arg2)
{
return false;
}
@ -754,7 +754,7 @@ class Stream
*
* @access public
*/
function _stream_close()
private function _stream_close()
{
}
@ -773,7 +773,7 @@ class Stream
* @return mixed
* @access public
*/
function __call($name, $arguments)
public function __call($name, $arguments)
{
if (defined('NET_SFTP_STREAM_LOGGING')) {
echo $name . '(';

View File

@ -229,7 +229,7 @@ class SSH1
* @var string
* @access private
*/
var $identifier = 'SSH-1.5-phpseclib';
private $identifier = 'SSH-1.5-phpseclib';
/**
* The Socket Object
@ -237,7 +237,7 @@ class SSH1
* @var object
* @access private
*/
var $fsock;
private $fsock;
/**
* The cryptography object
@ -245,7 +245,7 @@ class SSH1
* @var object
* @access private
*/
var $crypto = false;
private $crypto = false;
/**
* Execution Bitmap
@ -256,7 +256,7 @@ class SSH1
* @var int
* @access private
*/
var $bitmap = 0;
private $bitmap = 0;
/**
* The Server Key Public Exponent
@ -267,7 +267,7 @@ class SSH1
* @var string
* @access private
*/
var $server_key_public_exponent;
private $server_key_public_exponent;
/**
* The Server Key Public Modulus
@ -278,7 +278,7 @@ class SSH1
* @var string
* @access private
*/
var $server_key_public_modulus;
private $server_key_public_modulus;
/**
* The Host Key Public Exponent
@ -289,7 +289,7 @@ class SSH1
* @var string
* @access private
*/
var $host_key_public_exponent;
private $host_key_public_exponent;
/**
* The Host Key Public Modulus
@ -300,7 +300,7 @@ class SSH1
* @var string
* @access private
*/
var $host_key_public_modulus;
private $host_key_public_modulus;
/**
* Supported Ciphers
@ -311,7 +311,7 @@ class SSH1
* @var array
* @access private
*/
var $supported_ciphers = [
private $supported_ciphers = [
self::CIPHER_NONE => 'No encryption',
self::CIPHER_IDEA => 'IDEA in CFB mode',
self::CIPHER_DES => 'DES in CBC mode',
@ -330,7 +330,7 @@ class SSH1
* @var array
* @access private
*/
var $supported_authentications = [
private $supported_authentications = [
self::AUTH_RHOSTS => '.rhosts or /etc/hosts.equiv',
self::AUTH_RSA => 'pure RSA authentication',
self::AUTH_PASSWORD => 'password authentication',
@ -344,7 +344,7 @@ class SSH1
* @var string
* @access private
*/
var $server_identification = '';
private $server_identification = '';
/**
* Protocol Flags
@ -353,7 +353,7 @@ class SSH1
* @var array
* @access private
*/
var $protocol_flags = [];
private $protocol_flags = [];
/**
* Protocol Flag Log
@ -362,7 +362,7 @@ class SSH1
* @var array
* @access private
*/
var $protocol_flag_log = [];
private $protocol_flag_log = [];
/**
* Message Log
@ -371,7 +371,7 @@ class SSH1
* @var array
* @access private
*/
var $message_log = [];
private $message_log = [];
/**
* Real-time log file pointer
@ -380,7 +380,7 @@ class SSH1
* @var resource
* @access private
*/
var $realtime_log_file;
private $realtime_log_file;
/**
* Real-time log file size
@ -389,7 +389,7 @@ class SSH1
* @var int
* @access private
*/
var $realtime_log_size;
private $realtime_log_size;
/**
* Real-time log file wrap boolean
@ -398,7 +398,7 @@ class SSH1
* @var bool
* @access private
*/
var $realtime_log_wrap;
private $realtime_log_wrap;
/**
* Interactive Buffer
@ -407,7 +407,7 @@ class SSH1
* @var array
* @access private
*/
var $interactiveBuffer = '';
private $interactiveBuffer = '';
/**
* Timeout
@ -415,7 +415,7 @@ class SSH1
* @see self::setTimeout()
* @access private
*/
var $timeout;
private $timeout;
/**
* Current Timeout
@ -423,7 +423,7 @@ class SSH1
* @see self::_get_channel_packet()
* @access private
*/
var $curTimeout;
private $curTimeout;
/**
* Log Boundary
@ -431,7 +431,7 @@ class SSH1
* @see self::_format_log()
* @access private
*/
var $log_boundary = ':';
private $log_boundary = ':';
/**
* Log Long Width
@ -439,7 +439,7 @@ class SSH1
* @see self::_format_log()
* @access private
*/
var $log_long_width = 65;
private $log_long_width = 65;
/**
* Log Short Width
@ -447,7 +447,7 @@ class SSH1
* @see self::_format_log()
* @access private
*/
var $log_short_width = 16;
private $log_short_width = 16;
/**
* Hostname
@ -457,7 +457,7 @@ class SSH1
* @var string
* @access private
*/
var $host;
private $host;
/**
* Port Number
@ -467,7 +467,7 @@ class SSH1
* @var int
* @access private
*/
var $port;
private $port;
/**
* Timeout for initial connection
@ -482,7 +482,7 @@ class SSH1
* @var int
* @access private
*/
var $connectionTimeout;
private $connectionTimeout;
/**
* Default cipher
@ -492,7 +492,7 @@ class SSH1
* @var int
* @access private
*/
var $cipher;
private $cipher;
/**
* Default Constructor.
@ -506,7 +506,7 @@ class SSH1
* @return \phpseclib\Net\SSH1
* @access public
*/
function __construct($host, $port = 22, $timeout = 10, $cipher = self::CIPHER_3DES)
public function __construct($host, $port = 22, $timeout = 10, $cipher = self::CIPHER_3DES)
{
$this->protocol_flags = [
1 => 'NET_SSH1_MSG_DISCONNECT',
@ -527,7 +527,7 @@ class SSH1
33 => 'NET_SSH1_CMSG_EXIT_CONFIRMATION'
];
$this->_define_array($this->protocol_flags);
$this->define_array($this->protocol_flags);
$this->host = $host;
$this->port = $port;
@ -543,7 +543,7 @@ class SSH1
* @throws \RuntimeException on other errors
* @access private
*/
function _connect()
private function connect()
{
$this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->connectionTimeout);
if (!$this->fsock) {
@ -553,8 +553,8 @@ class SSH1
$this->server_identification = $init_line = fgets($this->fsock, 255);
if (defined('NET_SSH1_LOGGING')) {
$this->_append_log('<-', $this->server_identification);
$this->_append_log('->', $this->identifier . "\r\n");
$this->append_log('<-', $this->server_identification);
$this->append_log('->', $this->identifier . "\r\n");
}
if (!preg_match('#SSH-([0-9\.]+)-(.+)#', $init_line, $parts)) {
@ -566,7 +566,7 @@ class SSH1
fputs($this->fsock, $this->identifier."\r\n");
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
throw new \UnexpectedValueException('Expected SSH_SMSG_PUBLIC_KEY');
}
@ -617,14 +617,14 @@ class SSH1
$double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0));
if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) {
$double_encrypted_session_key = $this->_rsa_crypt(
$double_encrypted_session_key = $this->rsa_crypt(
$double_encrypted_session_key,
[
$server_key_public_exponent,
$server_key_public_modulus
]
);
$double_encrypted_session_key = $this->_rsa_crypt(
$double_encrypted_session_key = $this->rsa_crypt(
$double_encrypted_session_key,
[
$host_key_public_exponent,
@ -632,14 +632,14 @@ class SSH1
]
);
} else {
$double_encrypted_session_key = $this->_rsa_crypt(
$double_encrypted_session_key = $this->rsa_crypt(
$double_encrypted_session_key,
[
$host_key_public_exponent,
$host_key_public_modulus
]
);
$double_encrypted_session_key = $this->_rsa_crypt(
$double_encrypted_session_key = $this->rsa_crypt(
$double_encrypted_session_key,
[
$server_key_public_exponent,
@ -651,7 +651,7 @@ class SSH1
$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)) {
if (!$this->send_binary_packet($data)) {
throw new \RuntimeException('Error sending SSH_CMSG_SESSION_KEY');
}
@ -682,7 +682,7 @@ class SSH1
// break;
}
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
if ($response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
throw new \UnexpectedValueException('Expected SSH_SMSG_SUCCESS');
@ -703,11 +703,11 @@ class SSH1
* @throws \RuntimeException on other errors
* @access public
*/
function login($username, $password = '')
public function login($username, $password = '')
{
if (!($this->bitmap & self::MASK_CONSTRUCTOR)) {
$this->bitmap |= self::MASK_CONSTRUCTOR;
if (!$this->_connect()) {
if (!$this->connect()) {
return false;
}
}
@ -718,11 +718,11 @@ class SSH1
$data = pack('CNa*', NET_SSH1_CMSG_USER, strlen($username), $username);
if (!$this->_send_binary_packet($data)) {
if (!$this->send_binary_packet($data)) {
throw new \RuntimeException('Error sending SSH_CMSG_USER');
}
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
if ($response === true) {
return false;
@ -736,7 +736,7 @@ class SSH1
$data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen($password), $password);
if (!$this->_send_binary_packet($data)) {
if (!$this->send_binary_packet($data)) {
throw new \RuntimeException('Error sending SSH_CMSG_AUTH_PASSWORD');
}
@ -746,7 +746,7 @@ class SSH1
$this->message_log[count($this->message_log) - 1] = $data;
}
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
if ($response === true) {
return false;
@ -769,7 +769,7 @@ class SSH1
*
* @param mixed $timeout
*/
function setTimeout($timeout)
public function setTimeout($timeout)
{
$this->timeout = $this->curTimeout = $timeout;
}
@ -795,7 +795,7 @@ class SSH1
* @throws \RuntimeException on error sending command
* @access public
*/
function exec($cmd, $block = true)
public function exec($cmd, $block = true)
{
if (!($this->bitmap & self::MASK_LOGIN)) {
throw new \RuntimeException('Operation disallowed prior to login()');
@ -803,7 +803,7 @@ class SSH1
$data = pack('CNa*', NET_SSH1_CMSG_EXEC_CMD, strlen($cmd), $cmd);
if (!$this->_send_binary_packet($data)) {
if (!$this->send_binary_packet($data)) {
throw new \RuntimeException('Error sending SSH_CMSG_EXEC_CMD');
}
@ -812,19 +812,19 @@ class SSH1
}
$output = '';
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
if ($response !== false) {
do {
$output.= substr($response[self::RESPONSE_DATA], 4);
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
} while (is_array($response) && $response[self::RESPONSE_TYPE] != NET_SSH1_SMSG_EXITSTATUS);
}
$data = pack('C', NET_SSH1_CMSG_EXIT_CONFIRMATION);
// i don't think it's really all that important if this packet gets sent or not.
$this->_send_binary_packet($data);
$this->send_binary_packet($data);
fclose($this->fsock);
@ -844,18 +844,18 @@ class SSH1
* @throws \RuntimeException on other errors
* @access private
*/
function _initShell()
private function initShell()
{
// 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, self::TTY_OP_END);
if (!$this->_send_binary_packet($data)) {
if (!$this->send_binary_packet($data)) {
throw new \RuntimeException('Error sending SSH_CMSG_REQUEST_PTY');
}
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
if ($response === true) {
return false;
@ -866,7 +866,7 @@ class SSH1
$data = pack('C', NET_SSH1_CMSG_EXEC_SHELL);
if (!$this->_send_binary_packet($data)) {
if (!$this->send_binary_packet($data)) {
throw new \RuntimeException('Error sending SSH_CMSG_EXEC_SHELL');
}
@ -885,7 +885,7 @@ class SSH1
* @return bool
* @access public
*/
function write($cmd)
public function write($cmd)
{
return $this->interactiveWrite($cmd);
}
@ -903,13 +903,13 @@ class SSH1
* @throws \RuntimeException on connection error
* @access public
*/
function read($expect, $mode = self::READ__SIMPLE)
public function read($expect, $mode = self::READ__SIMPLE)
{
if (!($this->bitmap & self::MASK_LOGIN)) {
throw new \RuntimeException('Operation disallowed prior to login()');
}
if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) {
if (!($this->bitmap & self::MASK_SHELL) && !$this->initShell()) {
throw new \RuntimeException('Unable to initiate an interactive shell session');
}
@ -923,7 +923,7 @@ class SSH1
if ($pos !== false) {
return Strings::shift($this->interactiveBuffer, $pos + strlen($match));
}
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
if ($response === true) {
return Strings::shift($this->interactiveBuffer, strlen($this->interactiveBuffer));
@ -941,19 +941,19 @@ class SSH1
* @throws \RuntimeException on connection error
* @access public
*/
function interactiveWrite($cmd)
public function interactiveWrite($cmd)
{
if (!($this->bitmap & self::MASK_LOGIN)) {
throw new \RuntimeException('Operation disallowed prior to login()');
}
if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) {
if (!($this->bitmap & self::MASK_SHELL) && !$this->initShell()) {
throw new \RuntimeException('Unable to initiate an interactive shell session');
}
$data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($cmd), $cmd);
if (!$this->_send_binary_packet($data)) {
if (!$this->send_binary_packet($data)) {
throw new \RuntimeException('Error sending SSH_CMSG_STDIN');
}
@ -974,20 +974,20 @@ class SSH1
* @throws \RuntimeException on connection error
* @access public
*/
function interactiveRead()
public function interactiveRead()
{
if (!($this->bitmap & self::MASK_LOGIN)) {
throw new \RuntimeException('Operation disallowed prior to login()');
}
if (!($this->bitmap & self::MASK_SHELL) && !$this->_initShell()) {
if (!($this->bitmap & self::MASK_SHELL) && !$this->initShell()) {
throw new \RuntimeException('Unable to initiate an interactive shell session');
}
$read = [$this->fsock];
$write = $except = null;
if (stream_select($read, $write, $except, 0)) {
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
return substr($response[self::RESPONSE_DATA], 4);
} else {
return '';
@ -999,9 +999,9 @@ class SSH1
*
* @access public
*/
function disconnect()
public function disconnect()
{
$this->_disconnect();
$this->disconnect_helper();
}
/**
@ -1012,9 +1012,9 @@ class SSH1
*
* @access public
*/
function __destruct()
public function __destruct()
{
$this->_disconnect();
$this->disconnect_helper();
}
/**
@ -1023,13 +1023,13 @@ class SSH1
* @param string $msg
* @access private
*/
function _disconnect($msg = 'Client Quit')
private function disconnect_helper($msg = 'Client Quit')
{
if ($this->bitmap) {
$data = pack('C', NET_SSH1_CMSG_EOF);
$this->_send_binary_packet($data);
$this->send_binary_packet($data);
/*
$response = $this->_get_binary_packet();
$response = $this->get_binary_packet();
if ($response === true) {
$response = [self::RESPONSE_TYPE => -1];
}
@ -1043,7 +1043,7 @@ class SSH1
*/
$data = pack('CNa*', NET_SSH1_MSG_DISCONNECT, strlen($msg), $msg);
$this->_send_binary_packet($data);
$this->send_binary_packet($data);
fclose($this->fsock);
$this->bitmap = 0;
}
@ -1061,7 +1061,7 @@ class SSH1
* @return array
* @access private
*/
function _get_binary_packet()
private function get_binary_packet()
{
if (feof($this->fsock)) {
//user_error('connection closed prematurely');
@ -1077,7 +1077,7 @@ class SSH1
$usec = 1000000 * ($this->curTimeout - $sec);
// on windows this returns a "Warning: Invalid CRT parameters detected" error
if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
//$this->_disconnect('Timeout');
//$this->disconnect_helper('Timeout');
return true;
}
$elapsed = strtok(microtime(), ' ') + strtok('') - $start;
@ -1108,7 +1108,7 @@ class SSH1
$temp = unpack('Ncrc', substr($raw, -4));
//if ( $temp['crc'] != $this->_crc($padding . $type . $data) ) {
//if ( $temp['crc'] != $this->crc($padding . $type . $data) ) {
// user_error('Bad CRC in packet from server');
// return false;
//}
@ -1119,7 +1119,7 @@ class SSH1
$temp = isset($this->protocol_flags[$type]) ? $this->protocol_flags[$type] : 'UNKNOWN';
$temp = '<- ' . $temp .
' (' . round($stop - $start, 4) . 's)';
$this->_append_log($temp, $data);
$this->append_log($temp, $data);
}
return [
@ -1138,7 +1138,7 @@ class SSH1
* @return bool
* @access private
*/
function _send_binary_packet($data)
private function send_binary_packet($data)
{
if (feof($this->fsock)) {
//user_error('connection closed prematurely');
@ -1151,7 +1151,7 @@ class SSH1
$orig = $data;
$data = $padding . $data;
$data.= pack('N', $this->_crc($data));
$data.= pack('N', $this->crc($data));
if ($this->crypto !== false) {
$data = $this->crypto->encrypt($data);
@ -1167,7 +1167,7 @@ class SSH1
$temp = isset($this->protocol_flags[ord($orig[0])]) ? $this->protocol_flags[ord($orig[0])] : 'UNKNOWN';
$temp = '-> ' . $temp .
' (' . round($stop - $start, 4) . 's)';
$this->_append_log($temp, $orig);
$this->append_log($temp, $orig);
}
return $result;
@ -1186,7 +1186,7 @@ class SSH1
* @return int
* @access private
*/
function _crc($data)
private function crc($data)
{
static $crc_lookup_table = [
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
@ -1286,7 +1286,7 @@ class SSH1
* @return BigInteger
* @access private
*/
function _rsa_crypt($m, $key)
private function rsa_crypt($m, $key)
{
/*
$rsa = new RSA();
@ -1334,7 +1334,7 @@ class SSH1
* @param array $array
* @access private
*/
function _define_array()
private function define_array()
{
$args = func_get_args();
foreach ($args as $arg) {
@ -1356,7 +1356,7 @@ class SSH1
* @access public
* @return array|false|string
*/
function getLog()
public function getLog()
{
if (!defined('NET_SSH1_LOGGING')) {
return false;
@ -1367,7 +1367,7 @@ class SSH1
return $this->message_number_log;
break;
case self::LOG_COMPLEX:
return $this->_format_log($this->message_log, $this->protocol_flags_log);
return $this->format_log($this->message_log, $this->protocol_flags_log);
break;
default:
return false;
@ -1382,7 +1382,7 @@ class SSH1
* @access private
* @return string
*/
function _format_log($message_log, $message_number_log)
private function format_log($message_log, $message_number_log)
{
$output = '';
for ($i = 0; $i < count($message_log); $i++) {
@ -1394,7 +1394,7 @@ class SSH1
$output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 ';
}
$fragment = Strings::shift($current_log, $this->log_short_width);
$hex = substr(preg_replace_callback('#.#s', [$this, '_format_log_helper'], $fragment), strlen($this->log_boundary));
$hex = substr(preg_replace_callback('#.#s', [$this, 'format_log_helper'], $fragment), strlen($this->log_boundary));
// replace non ASCII printable characters with dots
// http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
// also replace < with a . since < messes up the output on web browsers
@ -1417,7 +1417,7 @@ class SSH1
* @access private
* @return string
*/
function _format_log_helper($matches)
private function format_log_helper($matches)
{
return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT);
}
@ -1432,7 +1432,7 @@ class SSH1
* @return string
* @access public
*/
function getServerKeyPublicExponent($raw_output = false)
public function getServerKeyPublicExponent($raw_output = false)
{
return $raw_output ? $this->server_key_public_exponent->toBytes() : $this->server_key_public_exponent->toString();
}
@ -1447,7 +1447,7 @@ class SSH1
* @return string
* @access public
*/
function getServerKeyPublicModulus($raw_output = false)
public function getServerKeyPublicModulus($raw_output = false)
{
return $raw_output ? $this->server_key_public_modulus->toBytes() : $this->server_key_public_modulus->toString();
}
@ -1462,7 +1462,7 @@ class SSH1
* @return string
* @access public
*/
function getHostKeyPublicExponent($raw_output = false)
public function getHostKeyPublicExponent($raw_output = false)
{
return $raw_output ? $this->host_key_public_exponent->toBytes() : $this->host_key_public_exponent->toString();
}
@ -1477,7 +1477,7 @@ class SSH1
* @return string
* @access public
*/
function getHostKeyPublicModulus($raw_output = false)
public function getHostKeyPublicModulus($raw_output = false)
{
return $raw_output ? $this->host_key_public_modulus->toBytes() : $this->host_key_public_modulus->toString();
}
@ -1493,7 +1493,7 @@ class SSH1
* @return array
* @access public
*/
function getSupportedCiphers($raw_output = false)
public function getSupportedCiphers($raw_output = false)
{
return $raw_output ? array_keys($this->supported_ciphers) : array_values($this->supported_ciphers);
}
@ -1509,7 +1509,7 @@ class SSH1
* @return array
* @access public
*/
function getSupportedAuthentications($raw_output = false)
public function getSupportedAuthentications($raw_output = false)
{
return $raw_output ? array_keys($this->supported_authentications) : array_values($this->supported_authentications);
}
@ -1520,7 +1520,7 @@ class SSH1
* @return string
* @access public
*/
function getServerIdentification()
public function getServerIdentification()
{
return rtrim($this->server_identification);
}
@ -1533,7 +1533,7 @@ class SSH1
* @param string $data
* @access private
*/
function _append_log($protocol_flags, $message)
private function append_log($protocol_flags, $message)
{
switch (NET_SSH1_LOGGING) {
// useful for benchmarks
@ -1555,7 +1555,7 @@ class SSH1
// passwords won't be filtered out and select other packets may not be correctly
// identified
case self::LOG_REALTIME:
echo "<pre>\r\n" . $this->_format_log([$message], [$protocol_flags]) . "\r\n</pre>\r\n";
echo "<pre>\r\n" . $this->format_log([$message], [$protocol_flags]) . "\r\n</pre>\r\n";
@flush();
@ob_flush();
break;
@ -1573,7 +1573,7 @@ class SSH1
if (!is_resource($this->realtime_log_file)) {
break;
}
$entry = $this->_format_log([$message], [$protocol_flags]);
$entry = $this->format_log([$message], [$protocol_flags]);
if ($this->realtime_log_wrap) {
$temp = "<<< START >>>\r\n";
$entry.= $temp;

File diff suppressed because it is too large Load Diff

View File

@ -37,6 +37,7 @@ use ParagonIE\ConstantTime\Base64;
use phpseclib\Crypt\RSA;
use phpseclib\Exception\BadConfigurationException;
use phpseclib\System\SSH\Agent\Identity;
use phpseclib\Common\Functions\Objects;
/**
* Pure-PHP ssh-agent client identity factory
@ -88,30 +89,43 @@ class Agent
* @var resource
* @access private
*/
var $fsock;
private $fsock;
/**
* Agent forwarding status
*
* @var int
* @access private
*/
var $forward_status = self::FORWARD_NONE;
private $forward_status = self::FORWARD_NONE;
/**
* Buffer for accumulating forwarded authentication
* agent data arriving on SSH data channel destined
* for agent unix socket
*
* @var string
* @access private
*/
var $socket_buffer = '';
private $socket_buffer = '';
/**
* Tracking the number of bytes we are expecting
* to arrive for the agent socket on the SSH data
* channel
*
* @var int
* @access private
*/
var $expected_bytes = 0;
private $expected_bytes = 0;
/**
* The current request channel
*
* @var int
* @access private
*/
private $request_channel;
/**
* Default Constructor
@ -121,7 +135,7 @@ class Agent
* @throws \RuntimeException on connection errors
* @access public
*/
function __construct()
public function __construct()
{
switch (true) {
case isset($_SERVER['SSH_AUTH_SOCK']):
@ -150,7 +164,7 @@ class Agent
* @throws \RuntimeException on receipt of unexpected packets
* @access public
*/
function requestIdentities()
public function requestIdentities()
{
if (!$this->fsock) {
return [];
@ -209,7 +223,7 @@ class Agent
* @return bool
* @access public
*/
function startSSHForwarding($ssh)
public function startSSHForwarding($ssh)
{
if ($this->forward_status == self::FORWARD_NONE) {
$this->forward_status = self::FORWARD_REQUEST;
@ -223,34 +237,33 @@ class Agent
* @return bool
* @access private
*/
function _request_forwarding($ssh)
private function request_forwarding($ssh)
{
$request_channel = $ssh->_get_open_channel();
if ($request_channel === false) {
$this->request_channel = Objects::callFunc($ssh, 'get_open_channel');
if ($this->request_channel === false) {
return false;
}
$packet = pack(
'CNNa*C',
NET_SSH2_MSG_CHANNEL_REQUEST,
$ssh->server_channels[$request_channel],
Objects::getVar($ssh, 'server_channels')[$this->request_channel],
strlen('auth-agent-req@openssh.com'),
'auth-agent-req@openssh.com',
1
);
$ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_REQUEST;
if (!$ssh->_send_binary_packet($packet)) {
$this->update_channel_status($ssh, NET_SSH2_MSG_CHANNEL_REQUEST);
if (!Objects::callFunc($ssh, 'send_binary_packet', [$packet])) {
return false;
}
$response = $ssh->_get_channel_packet($request_channel);
$response = Objects::callFunc($ssh, 'get_channel_packet', [$this->request_channel]);
if ($response === false) {
return false;
}
$ssh->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_OPEN;
$this->update_channel_status($ssh, NET_SSH2_MSG_CHANNEL_OPEN);
$this->forward_status = self::FORWARD_ACTIVE;
return true;
@ -266,10 +279,10 @@ class Agent
* @param Net_SSH2 $ssh
* @access private
*/
function _on_channel_open($ssh)
public function on_channel_open($ssh)
{
if ($this->forward_status == self::FORWARD_REQUEST) {
$this->_request_forwarding($ssh);
$this->request_forwarding($ssh);
}
}
@ -281,7 +294,7 @@ class Agent
* @throws \RuntimeException on connection errors
* @access private
*/
function _forward_data($data)
private function forward_data($data)
{
if ($this->expected_bytes > 0) {
$this->socket_buffer.= $data;
@ -310,4 +323,18 @@ class Agent
return pack('Na*', $agent_reply_bytes, $agent_reply_data);
}
/**
* Forward data to SSH Agent and return data reply
*
* @param \phpseclib\Net\SSH2 $ssh
* @param integer $status
* @access private
*/
private function update_channel_status($ssh, $status)
{
$temp = Objects::getVar($ssh, 'channel_status');
$temp[$this->request_channel] = $status;
Objects::setVar($ssh, 'channel_status', $temp);
}
}

View File

@ -42,7 +42,7 @@ class Identity
* @access private
* @see self::getPublicKey()
*/
var $key;
private $key;
/**
* Key Blob
@ -51,7 +51,7 @@ class Identity
* @access private
* @see self::sign()
*/
var $key_blob;
private $key_blob;
/**
* Socket Resource
@ -60,7 +60,7 @@ class Identity
* @access private
* @see self::sign()
*/
var $fsock;
private $fsock;
/**
* Default Constructor.
@ -69,7 +69,7 @@ class Identity
* @return \phpseclib\System\SSH\Agent\Identity
* @access private
*/
function __construct($fsock)
public function __construct($fsock)
{
$this->fsock = $fsock;
}
@ -82,7 +82,7 @@ class Identity
* @param \phpseclib\Crypt\RSA $key
* @access private
*/
function setPublicKey($key)
public function setPublicKey($key)
{
$this->key = $key;
$this->key->setPublicKey();
@ -97,7 +97,7 @@ class Identity
* @param string $key_blob
* @access private
*/
function setPublicKeyBlob($key_blob)
public function setPublicKeyBlob($key_blob)
{
$this->key_blob = $key_blob;
}
@ -111,7 +111,7 @@ class Identity
* @return mixed
* @access public
*/
function getPublicKey($type = 'PKCS8')
public function getPublicKey($type = 'PKCS8')
{
return $this->key->getPublicKey($type);
}
@ -125,7 +125,7 @@ class Identity
* @throws \phpseclib\Exception\UnsupportedAlgorithmException if the algorithm is unsupported
* @access public
*/
function setHash($hash = 'sha1')
public function setHash($hash = 'sha1')
{
if ($hash != 'sha1') {
throw new UnsupportedAlgorithmException('ssh-agent can only be used with the sha1 hash');
@ -144,7 +144,7 @@ class Identity
* @throws \phpseclib\Exception\UnsupportedAlgorithmException if the algorithm is unsupported
* @access public
*/
function sign($message, $padding = RSA::PADDING_PKCS1)
public function sign($message, $padding = RSA::PADDING_PKCS1)
{
if ($padding != RSA::PADDING_PKCS1 && $padding != RSA::PADDING_RELAXED_PKCS1) {
throw new UnsupportedAlgorithmException('ssh-agent can only create PKCS1 signatures');

View File

@ -109,4 +109,12 @@ abstract class PhpseclibTestCase extends PHPUnit_Framework_TestCase
$prop->setAccessible(true);
return $prop->getValue($obj);
}
public static function callFunc($obj, $func, $params = array())
{
$reflection = new ReflectionClass(get_class($obj));
$method = $reflection->getMethod($func);
$method->setAccessible(true);
return $method->invokeArgs($obj, $params);
}
}

View File

@ -34,7 +34,7 @@ class Unit_Net_SSH1Test extends PhpseclibTestCase
->setMethods(null)
->getMock();
$result = $ssh->_format_log($message_log, $message_number_log);
$result = self::callFunc($ssh, 'format_log', array($message_log, $message_number_log));
$this->assertEquals($expected, $result);
}

View File

@ -32,13 +32,13 @@ class Unit_Net_SSH2Test extends PhpseclibTestCase
{
$ssh = $this->createSSHMock();
$result = $ssh->_format_log($message_log, $message_number_log);
$result = self::callFunc($ssh, 'format_log', array($message_log, $message_number_log));
$this->assertEquals($expected, $result);
}
public function testGenerateIdentifier()
{
$identifier = $this->createSSHMock()->_generate_identifier();
$identifier = self::callFunc($this->createSSHMock(), 'generate_identifier');
$this->assertStringStartsWith('SSH-2.0-phpseclib_2.0', $identifier);
if (extension_loaded('libsodium')) {