1
0
mirror of https://github.com/danog/tgseclib.git synced 2024-12-03 10:07:47 +01:00
tgseclib/phpseclib/Net/SCP.php

342 lines
9.4 KiB
PHP
Raw Normal View History

2013-04-27 23:03:35 +02:00
<?php
/**
* Pure-PHP implementation of SCP.
*
2015-04-02 12:57:52 +02:00
* PHP version 5
2013-04-27 23:03:35 +02:00
*
* 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
2014-12-10 02:31:41 +01:00
* include 'vendor/autoload.php';
2013-04-27 23:03:35 +02:00
*
2014-12-10 02:31:41 +01:00
* $ssh = new \phpseclib\Net\SSH2('www.domain.tld');
2013-04-27 23:06:05 +02:00
* if (!$ssh->login('username', 'password')) {
2013-04-27 23:03:35 +02:00
* exit('bad login');
* }
2014-12-10 02:31:41 +01:00
* $scp = new \phpseclib\Net\SCP($ssh);
*
2013-04-27 23:03:35 +02:00
* $scp->put('abcd', str_repeat('x', 1024*1024));
* ?>
* </code>
*
* @category Net
2014-12-10 02:31:41 +01:00
* @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
2013-04-27 23:03:35 +02:00
*/
2014-12-10 02:31:41 +01:00
namespace phpseclib\Net;
use phpseclib\Exception\FileNotFoundException;
use phpseclib\Common\Functions\Strings;
2017-01-08 02:51:56 +01:00
use phpseclib\Common\Functions\Objects;
2014-12-10 02:31:41 +01:00
2013-04-27 23:03:35 +02:00
/**
* Pure-PHP implementations of SCP.
*
2014-12-10 02:31:41 +01:00
* @package SCP
2013-04-27 23:03:35 +02:00
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
2014-12-10 02:31:41 +01:00
class SCP
{
/**#@+
* @access public
2014-12-10 02:31:41 +01:00
* @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
2014-12-10 02:31:41 +01:00
* @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;
/**#@-*/
2013-04-27 23:03:35 +02:00
/**
* SSH Object
*
* @var object
2013-04-27 23:03:35 +02:00
* @access private
*/
2017-01-08 02:51:56 +01:00
private $ssh;
2013-04-27 23:03:35 +02:00
/**
* Packet Size
*
* @var int
2013-04-27 23:03:35 +02:00
* @access private
*/
2017-01-08 02:51:56 +01:00
private $packet_size;
2013-04-27 23:03:35 +02:00
/**
* Mode
*
* @var int
2013-04-27 23:03:35 +02:00
* @access private
*/
2017-01-08 02:51:56 +01:00
private $mode;
2013-04-27 23:03:35 +02:00
/**
* Default Constructor.
*
* Connects to an SSH server
*
2016-11-02 13:12:36 +01:00
* @param \phpseclib\Net\SSH1|\phpseclib\Net\SSH2 $ssh
2014-12-10 02:31:41 +01:00
* @return \phpseclib\Net\SCP
2013-04-27 23:03:35 +02:00
* @access public
*/
2017-01-08 02:51:56 +01:00
public function __construct($ssh)
2013-04-27 23:03:35 +02:00
{
if ($ssh instanceof SSH2) {
$this->mode = self::MODE_SSH2;
} elseif ($ssh instanceof SSH1) {
$this->packet_size = 50000;
$this->mode = self::MODE_SSH1;
} else {
2013-04-27 23:03:35 +02:00
return;
}
$this->ssh = $ssh;
}
/**
* Uploads a file to the SCP server.
*
2014-12-10 02:31:41 +01:00
* 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
2013-04-27 23:03:35 +02:00
* 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
2013-04-27 23:03:35 +02:00
* 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
2013-04-27 23:03:35 +02:00
* @access public
*/
2017-01-08 02:51:56 +01:00
public function put($remote_file, $data, $mode = self::SOURCE_STRING, $callback = null)
2013-04-27 23:03:35 +02:00
{
if (!isset($this->ssh)) {
return false;
}
if (!$this->ssh->exec('scp -t ' . escapeshellarg($remote_file), false)) { // -t = to
return false;
}
2013-04-27 23:03:35 +02:00
2017-01-08 02:51:56 +01:00
$temp = $this->receive();
2013-04-27 23:03:35 +02:00
if ($temp !== chr(0)) {
return false;
}
if ($this->mode == self::MODE_SSH2) {
2017-01-08 02:51:56 +01:00
$this->packet_size = Objects::getVar($this->ssh, 'packet_size_client_to_server')[SSH2::CHANNEL_EXEC] - 4;
2013-04-27 23:03:35 +02:00
}
$remote_file = basename($remote_file);
if ($mode == self::SOURCE_STRING) {
$size = strlen($data);
2013-04-27 23:03:35 +02:00
} else {
if (!is_file($data)) {
throw new FileNotFoundException("$data is not a valid file");
2013-04-27 23:03:35 +02:00
}
2013-04-27 23:03:35 +02:00
$fp = @fopen($data, 'rb');
if (!$fp) {
return false;
}
$size = filesize($data);
}
2017-01-08 02:51:56 +01:00
$this->send('C0644 ' . $size . ' ' . $remote_file . "\n");
2017-01-08 02:51:56 +01:00
$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);
2017-01-08 02:51:56 +01:00
$this->send($temp);
$sent+= strlen($temp);
if (is_callable($callback)) {
2014-03-05 18:38:33 +01:00
call_user_func($callback, $sent);
}
2013-04-27 23:03:35 +02:00
}
2017-01-08 02:51:56 +01:00
$this->close();
if ($mode != self::SOURCE_STRING) {
fclose($fp);
}
return true;
2013-04-27 23:03:35 +02:00
}
/**
* 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 $local_file
* @return mixed
2013-04-27 23:03:35 +02:00
* @access public
*/
2017-01-08 02:51:56 +01:00
public function get($remote_file, $local_file = false)
2013-04-27 23:03:35 +02:00
{
if (!isset($this->ssh)) {
return false;
}
if (!$this->ssh->exec('scp -f ' . escapeshellarg($remote_file), false)) { // -f = from
return false;
}
2013-04-27 23:03:35 +02:00
2017-01-08 02:51:56 +01:00
$this->send("\0");
2013-04-27 23:03:35 +02:00
2017-01-08 02:51:56 +01:00
if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->receive()), $info)) {
2013-04-27 23:03:35 +02:00
return false;
}
2017-01-08 02:51:56 +01:00
$this->send("\0");
2013-04-27 23:03:35 +02:00
$size = 0;
if ($local_file !== false) {
$fp = @fopen($local_file, 'wb');
if (!$fp) {
return false;
}
}
$content = '';
while ($size < $info['size']) {
2017-01-08 02:51:56 +01:00
$data = $this->receive();
2013-04-27 23:03:35 +02:00
// SCP usually seems to split stuff out into 16k chunks
$size+= strlen($data);
if ($local_file === false) {
$content.= $data;
} else {
fputs($fp, $data);
}
}
2017-01-08 02:51:56 +01:00
$this->close();
2013-04-27 23:03:35 +02:00
if ($local_file !== false) {
fclose($fp);
return true;
}
return $content;
}
/**
* Sends a packet to an SSH server
*
* @param string $data
2013-04-27 23:03:35 +02:00
* @access private
*/
2017-01-08 02:51:56 +01:00
private function send($data)
2013-04-27 23:03:35 +02:00
{
switch ($this->mode) {
case self::MODE_SSH2:
2017-01-08 02:51:56 +01:00
Objects::callFunc($this->ssh, 'send_channel_packet', [SSH2::CHANNEL_EXEC, $data]);
2013-04-27 23:03:35 +02:00
break;
case self::MODE_SSH1:
2013-04-27 23:03:35 +02:00
$data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data);
2017-01-08 02:51:56 +01:00
Objects::callFunc($this->ssh, 'send_binary_packet', [$data]);
2015-07-15 03:52:31 +02:00
}
2013-04-27 23:03:35 +02:00
}
/**
* Receives a packet from an SSH server
*
* @return string
* @throws \UnexpectedValueException on receipt of an unexpected packet
2013-04-27 23:03:35 +02:00
* @access private
*/
2017-01-08 02:51:56 +01:00
private function receive()
2013-04-27 23:03:35 +02:00
{
switch ($this->mode) {
case self::MODE_SSH2:
2017-01-08 02:51:56 +01:00
return Objects::callFunc($this->ssh, 'get_channel_packet', [SSH2::CHANNEL_EXEC, true]);
case self::MODE_SSH1:
2017-01-08 02:51:56 +01:00
if (!Objects::getVar($this->ssh, 'bitmap')) {
2013-04-27 23:03:35 +02:00
return false;
}
while (true) {
2017-01-08 02:51:56 +01:00
$response = Objects::getFunc($this->ssh, 'get_binary_packet');
2014-12-10 02:31:41 +01:00
switch ($response[SSH1::RESPONSE_TYPE]) {
2013-04-27 23:03:35 +02:00
case NET_SSH1_SMSG_STDOUT_DATA:
2016-11-30 02:26:11 +01:00
if (strlen($response[SSH1::RESPONSE_DATA]) < 4) {
2016-10-23 05:13:17 +02:00
return false;
}
2014-12-10 02:31:41 +01:00
extract(unpack('Nlength', $response[SSH1::RESPONSE_DATA]));
return Strings::shift($response[SSH1::RESPONSE_DATA], $length);
2013-04-27 23:03:35 +02:00
case NET_SSH1_SMSG_STDERR_DATA:
break;
case NET_SSH1_SMSG_EXITSTATUS:
2017-01-08 02:51:56 +01:00
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);
2013-04-27 23:03:35 +02:00
return false;
default:
throw new \UnexpectedValueException('Unknown packet received');
2013-04-27 23:03:35 +02:00
}
}
2015-07-15 03:52:31 +02:00
}
2013-04-27 23:03:35 +02:00
}
/**
* Closes the connection to an SSH server
*
* @access private
*/
2017-01-08 02:51:56 +01:00
private function close()
2013-04-27 23:03:35 +02:00
{
switch ($this->mode) {
case self::MODE_SSH2:
2017-01-08 02:51:56 +01:00
Objects::callFunc($this->ssh, 'close_channel', [SSH2::CHANNEL_EXEC, true]);
2013-04-27 23:03:35 +02:00
break;
case self::MODE_SSH1:
2017-01-08 02:51:56 +01:00
Objects::callFunc($this->ssh, 'disconnect');
2015-07-15 03:52:31 +02:00
}
2013-04-27 23:03:35 +02:00
}
}