rm Common/Functions/Objects.php

This commit is contained in:
terrafrost 2019-03-31 15:15:32 -05:00
parent d329814f55
commit f2226184ee
5 changed files with 42 additions and 562 deletions

View File

@ -1,74 +0,0 @@
<?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 public
*/
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
* @access public
*/
public static function setVar($obj, $var, $val)
{
$reflection = new \ReflectionClass(get_class($obj));
$prop = $reflection->getProperty($var);
$prop->setAccessible(true);
$prop->setValue($obj, $val);
}
/**
* Accesses a private method from an object
*
* @param object $obj
* @param string $func
* @param array $params
* @return mixed
* @access public
*/
public static function callFunc($obj, $func, $params = [])
{
$reflection = new \ReflectionClass(get_class($obj));
$method = $reflection->getMethod($func);
$method->setAccessible(true);
return $method->invokeArgs($obj, $params);
}
}

View File

@ -1,346 +0,0 @@
<?php
/**
* Pure-PHP implementation of SCP.
*
* PHP version 5
*
* The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
*
* Here's a short example of how to use this library:
* <code>
* <?php
* include 'vendor/autoload.php';
*
* $ssh = new \phpseclib\Net\SSH2('www.domain.tld');
* if (!$ssh->login('username', 'password')) {
* exit('bad login');
* }
* $scp = new \phpseclib\Net\SCP($ssh);
*
* $scp->put('abcd', str_repeat('x', 1024*1024));
* ?>
* </code>
*
* @category Net
* @package SCP
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2010 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Net;
use phpseclib\Exception\FileNotFoundException;
use phpseclib\Common\Functions\Strings;
use phpseclib\Common\Functions\Objects;
/**
* Pure-PHP implementations of SCP.
*
* @package SCP
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
class SCP
{
/**#@+
* @access public
* @see \phpseclib\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 \phpseclib\Net\SCP::_send()
* @see \phpseclib\Net\SCP::_receive()
*/
/**
* SSH1 is being used.
*/
const MODE_SSH1 = 1;
/**
* SSH2 is being used.
*/
const MODE_SSH2 = 2;
/**#@-*/
/**
* SSH Object
*
* @var object
* @access private
*/
private $ssh;
/**
* Packet Size
*
* @var int
* @access private
*/
private $packet_size;
/**
* Mode
*
* @var int
* @access private
*/
private $mode;
/**
* Default Constructor.
*
* Connects to an SSH server
*
* @param \phpseclib\Net\SSH1|\phpseclib\Net\SSH2 $ssh
* @access public
*/
public function __construct($ssh)
{
if ($ssh instanceof SSH2) {
$this->mode = self::MODE_SSH2;
} elseif ($ssh instanceof SSH1) {
$this->packet_size = 50000;
$this->mode = self::MODE_SSH1;
} else {
return;
}
$this->ssh = $ssh;
}
/**
* Uploads a file to the SCP server.
*
* By default, \phpseclib\Net\SCP::put() does not read from the local filesystem. $data is dumped directly into $remote_file.
* So, for example, if you set $data to 'filename.ext' and then do \phpseclib\Net\SCP::get(), you will get a file, twelve bytes
* long, containing 'filename.ext' as its contents.
*
* 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.
*
* 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.
*
* @param string $remote_file
* @param string $data
* @param int $mode
* @param callable $callback
* @throws \phpseclib\Exception\FileNotFoundException if you're uploading via a file and the file doesn't exist
* @return bool
* @access public
*/
public function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
{
if (!isset($this->ssh)) {
return false;
}
if (empty($remote_file)) {
throw new \InvalidArgumentException('remote_file cannot be blank');
}
if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) { // -t = to
return false;
}
$temp = $this->receive();
if ($temp !== chr(0)) {
return false;
}
if ($this->mode == self::MODE_SSH2) {
$this->packet_size = Objects::getVar($this->ssh, 'packet_size_client_to_server')[SSH2::CHANNEL_EXEC] - 4;
}
$remote_file = basename($remote_file);
if ($mode == self::SOURCE_STRING) {
$size = strlen($data);
} else {
if (!is_file($data)) {
throw new FileNotFoundException("$data is not a valid file");
}
$fp = @fopen($data, 'rb');
if (!$fp) {
return false;
}
$size = filesize($data);
}
$this->send('C0644 ' . $size . ' ' . $remote_file . "\n");
$temp = $this->receive();
if ($temp !== chr(0)) {
return false;
}
$sent = 0;
while ($sent < $size) {
$temp = $mode & self::SOURCE_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
$this->send($temp);
$sent+= strlen($temp);
if (is_callable($callback)) {
call_user_func($callback, $sent);
}
}
$this->close();
if ($mode != self::SOURCE_STRING) {
fclose($fp);
}
return true;
}
/**
* Downloads a file from the SCP server.
*
* Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if
* the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the
* operation
*
* @param string $remote_file
* @param string|bool $local_file
* @return mixed
* @access public
*/
public function get($remote_file, $local_file = false)
{
if (!isset($this->ssh)) {
return false;
}
if (!$this->ssh->exec('scp -f ' . escapeshellarg($remote_file), false)) { // -f = from
return false;
}
$this->send("\0");
if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->receive()), $info)) {
return false;
}
$this->send("\0");
$size = 0;
if ($local_file !== false) {
$fp = @fopen($local_file, 'wb');
if (!$fp) {
return false;
}
}
$content = '';
while ($size < $info['size']) {
$data = $this->receive();
// SCP usually seems to split stuff out into 16k chunks
$size+= strlen($data);
if ($local_file === false) {
$content.= $data;
} else {
fputs($fp, $data);
}
}
$this->close();
if ($local_file !== false) {
fclose($fp);
return true;
}
return $content;
}
/**
* Sends a packet to an SSH server
*
* @param string $data
* @access private
*/
private function send($data)
{
switch ($this->mode) {
case self::MODE_SSH2:
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);
Objects::callFunc($this->ssh, 'send_binary_packet', [$data]);
}
}
/**
* Receives a packet from an SSH server
*
* @return string
* @throws \UnexpectedValueException on receipt of an unexpected packet
* @access private
*/
private function receive()
{
switch ($this->mode) {
case self::MODE_SSH2:
return Objects::callFunc($this->ssh, 'get_channel_packet', [SSH2::CHANNEL_EXEC, true]);
case self::MODE_SSH1:
if (!Objects::getVar($this->ssh, 'bitmap')) {
return false;
}
while (true) {
$response = Objects::getFunc($this->ssh, 'get_binary_packet');
switch ($response[SSH1::RESPONSE_TYPE]) {
case NET_SSH1_SMSG_STDOUT_DATA:
if (strlen($response[SSH1::RESPONSE_DATA]) < 4) {
return false;
}
extract(unpack('Nlength', $response[SSH1::RESPONSE_DATA]));
/** @var integer $length */
return Strings::shift($response[SSH1::RESPONSE_DATA], $length);
case NET_SSH1_SMSG_STDERR_DATA:
break;
case NET_SSH1_SMSG_EXITSTATUS:
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');
}
}
}
}
/**
* Closes the connection to an SSH server
*
* @access private
*/
private function close()
{
switch ($this->mode) {
case self::MODE_SSH2:
Objects::callFunc($this->ssh, 'close_channel', [SSH2::CHANNEL_EXEC, true]);
break;
case self::MODE_SSH1:
Objects::callFunc($this->ssh, 'disconnect');
}
}
}

View File

@ -67,7 +67,6 @@ use phpseclib\System\SSH\Agent\Identity as AgentIdentity;
use phpseclib\Exception\NoSupportedAlgorithmsException;
use phpseclib\Exception\UnsupportedAlgorithmException;
use phpseclib\Common\Functions\Strings;
use phpseclib\Common\Functions\Objects;
/**
* Pure-PHP implementation of SSHv2.
@ -3021,6 +3020,42 @@ class SSH2
return false;
}
/**
* Request agent forwarding of remote server
*
* @return bool
* @access public
*/
public function requestAgentForwarding()
{
$request_channel = $this->get_open_channel();
if ($request_channel === false) {
return false;
}
$packet = pack(
'CNNa*C',
NET_SSH2_MSG_CHANNEL_REQUEST,
$this->server_channels[$request_channel],
strlen('auth-agent-req@openssh.com'),
'auth-agent-req@openssh.com',
1
);
$this->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_REQUEST;
$this->send_binary_packet($packet);
$response = $this->get_channel_packet($request_channel);
if ($response === false) {
return false;
}
$this->channel_status[$request_channel] = NET_SSH2_MSG_CHANNEL_OPEN;
return true;
}
/**
* Returns the output of an interactive shell
*
@ -4017,7 +4052,7 @@ class SSH2
$data = Strings::shift($response, $length);
if ($channel == self::CHANNEL_AGENT_FORWARD) {
$agent_response = Objects::callFunc($this->agent, 'forward_data', [$data]);
$agent_response = $this->agent->forwardData($data);
if (!is_bool($agent_response)) {
$this->send_channel_packet($channel, $agent_response);
}
@ -4442,7 +4477,7 @@ class SSH2
private function on_channel_open()
{
if (isset($this->agent)) {
Objects::callFunc($this->agent, 'on_channel_open', [$this]);
$this->agent->registerChannelOpen($this);
}
}

View File

@ -37,7 +37,6 @@ 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
@ -241,29 +240,10 @@ class Agent
*/
private function request_forwarding($ssh)
{
$this->request_channel = Objects::callFunc($ssh, 'get_open_channel');
if ($this->request_channel === false) {
if (!$ssh->requestAgentForwarding()) {
return false;
}
$packet = pack(
'CNNa*C',
NET_SSH2_MSG_CHANNEL_REQUEST,
Objects::getVar($ssh, 'server_channels')[$this->request_channel],
strlen('auth-agent-req@openssh.com'),
'auth-agent-req@openssh.com',
1
);
$this->update_channel_status($ssh, NET_SSH2_MSG_CHANNEL_REQUEST);
Objects::callFunc($ssh, 'send_binary_packet', [$packet]);
$response = Objects::callFunc($ssh, 'get_channel_packet', [$this->request_channel]);
if ($response === false) {
return false;
}
$this->update_channel_status($ssh, NET_SSH2_MSG_CHANNEL_OPEN);
$this->forward_status = self::FORWARD_ACTIVE;
return true;
@ -279,7 +259,7 @@ class Agent
* @param \phpseclib\Net\SSH2 $ssh
* @access private
*/
private function on_channel_open($ssh)
public function registerChannelOpen($ssh)
{
if ($this->forward_status == self::FORWARD_REQUEST) {
$this->request_forwarding($ssh);
@ -292,9 +272,9 @@ class Agent
* @param string $data
* @return string Data from SSH Agent
* @throws \RuntimeException on connection errors
* @access private
* @access public
*/
private function forward_data($data)
public function forwardData($data)
{
if ($this->expected_bytes > 0) {
$this->socket_buffer.= $data;
@ -323,18 +303,4 @@ 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

@ -1,101 +0,0 @@
<?php
/**
* @author Andreas Fischer <bantu@phpbb.com>
* @copyright 2014 Andreas Fischer
* @license http://www.opensource.org/licenses/mit-license.html MIT License
*/
use phpseclib\Net\SCP;
use phpseclib\Net\SSH2;
class Functional_Net_SCPSSH2UserStoryTest extends PhpseclibFunctionalTestCase
{
static protected $remoteFile;
static protected $exampleData;
static protected $exampleDataLength;
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
self::$remoteFile = uniqid('phpseclib-scp-ssh2-') . '.txt';
self::$exampleData = str_repeat('abscp12345', 1000);
self::$exampleDataLength = 10000;
}
public function testConstructSSH2()
{
$ssh = new SSH2($this->getEnv('SSH_HOSTNAME'));
$this->assertTrue(
$ssh->login(
$this->getEnv('SSH_USERNAME'),
$this->getEnv('SSH_PASSWORD')
)
);
return $ssh;
}
/**
* @depends testConstructSSH2
* @param \phpseclib\Net\SSH2 $ssh
*/
public function testConstructor($ssh)
{
$scp = new SCP($ssh);
$this->assertInternalType(
'object',
$scp,
'Could not construct \phpseclib\Net\SCP object.'
);
return $scp;
}
/**
* @depends testConstructor
* @param \phpseclib\Net\SCP $scp
*/
public function testPutGetString($scp)
{
$this->assertTrue(
$scp->put(self::$remoteFile, self::$exampleData),
'Failed asserting that data could successfully be put() into file.'
);
$content = $scp->get(self::$remoteFile);
// TODO: Address https://github.com/phpseclib/phpseclib/issues/146
$this->assertContains(
strlen($content),
[self::$exampleDataLength, self::$exampleDataLength + 1],
'Failed asserting that string length matches expected length.'
);
$this->assertContains(
$content,
[self::$exampleData, self::$exampleData . "\0"],
'Failed asserting that string content matches expected content.'
);
return $scp;
}
/**
* @depends testPutGetString
* @param \phpseclib\Net\SCP $scp
*/
public function testGetFile($scp)
{
$localFilename = $this->createTempFile();
$this->assertTrue(
$scp->get(self::$remoteFile, $localFilename),
'Failed asserting that get() into file was successful.'
);
// TODO: Address https://github.com/phpseclib/phpseclib/issues/146
$this->assertContains(
filesize($localFilename),
[self::$exampleDataLength, self::$exampleDataLength + 1],
'Failed asserting that filesize matches expected data size.'
);
$this->assertContains(
file_get_contents($localFilename),
[self::$exampleData, self::$exampleData . "\0"],
'Failed asserting that file content matches expected content.'
);
}
}