SSH2: tweaks to Exceptions

This commit is contained in:
terrafrost 2019-09-11 07:55:29 -05:00
parent e52697abac
commit 6f8ba6c614
3 changed files with 76 additions and 21 deletions

View File

@ -0,0 +1,26 @@
<?php
/**
* ConnectionClosedException
*
* PHP version 5
*
* @category Exception
* @package ConnectionClosedException
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
/**
* ConnectionClosedException
*
* @package ConnectionClosedException
* @author Jim Wigginton <terrafrost@php.net>
*/
class ConnectionClosedException extends \RuntimeException
{
}

View File

@ -0,0 +1,26 @@
<?php
/**
* UnableToConnectException
*
* PHP version 5
*
* @category Exception
* @package UnableToConnectException
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/
namespace phpseclib\Exception;
/**
* UnableToConnectException
*
* @package UnableToConnectException
* @author Jim Wigginton <terrafrost@php.net>
*/
class UnableToConnectException extends \RuntimeException
{
}

View File

@ -66,6 +66,9 @@ use phpseclib\System\SSH\Agent\Identity as AgentIdentity;
use phpseclib\Exception\NoSupportedAlgorithmsException; use phpseclib\Exception\NoSupportedAlgorithmsException;
use phpseclib\Exception\UnsupportedAlgorithmException; use phpseclib\Exception\UnsupportedAlgorithmException;
use phpseclib\Exception\UnsupportedCurveException; use phpseclib\Exception\UnsupportedCurveException;
use phpseclib\Exception\ConnectionClosedException;
use phpseclib\Exception\UnableToConectException;
use phpseclib\Exception\InsufficientSetupException;
use phpseclib\Common\Functions\Strings; use phpseclib\Common\Functions\Strings;
/** /**
@ -1186,7 +1189,7 @@ class SSH2
$this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->curTimeout == 0 ? 100000 : $this->curTimeout); $this->fsock = @fsockopen($this->host, $this->port, $errno, $errstr, $this->curTimeout == 0 ? 100000 : $this->curTimeout);
if (!$this->fsock) { if (!$this->fsock) {
$host = $this->host . ':' . $this->port; $host = $this->host . ':' . $this->port;
throw new \RuntimeException(rtrim("Cannot connect to $host. Error $errno. $errstr")); throw new UnableToConectException(rtrim("Cannot connect to $host. Error $errno. $errstr"));
} }
$elapsed = microtime(true) - $start; $elapsed = microtime(true) - $start;
@ -1261,7 +1264,7 @@ class SSH2
if (feof($this->fsock)) { if (feof($this->fsock)) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
$extra = $matches[1]; $extra = $matches[1];
@ -1277,7 +1280,7 @@ class SSH2
} }
if (version_compare($matches[3], '1.99', '<')) { if (version_compare($matches[3], '1.99', '<')) {
throw new \RuntimeException("Cannot connect to SSH $matches[3] servers"); throw new UnableToConectException("Cannot connect to SSH $matches[3] servers");
} }
if (!$this->send_id_string_first) { if (!$this->send_id_string_first) {
@ -1288,7 +1291,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
if (!strlen($response) || ord($response[0]) != NET_SSH2_MSG_KEXINIT) { if (!strlen($response) || ord($response[0]) != NET_SSH2_MSG_KEXINIT) {
@ -1427,7 +1430,7 @@ class SSH2
$kexinit_payload_server = $this->get_binary_packet(); $kexinit_payload_server = $this->get_binary_packet();
if ($kexinit_payload_server === false) { if ($kexinit_payload_server === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
if (!strlen($kexinit_payload_server) || ord($kexinit_payload_server[0]) != NET_SSH2_MSG_KEXINIT) { if (!strlen($kexinit_payload_server) || ord($kexinit_payload_server[0]) != NET_SSH2_MSG_KEXINIT) {
@ -1511,7 +1514,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type, $primeBytes, $gBytes) = Strings::unpackSSH2('Css', $response); list($type, $primeBytes, $gBytes) = Strings::unpackSSH2('Css', $response);
@ -1570,7 +1573,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
if (!strlen($response)) { if (!strlen($response)) {
return false; return false;
@ -1652,7 +1655,7 @@ class SSH2
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type) = Strings::unpackSSH2('C', $response); list($type) = Strings::unpackSSH2('C', $response);
@ -2071,7 +2074,7 @@ class SSH2
return $this->login_helper($username, $password); return $this->login_helper($username, $password);
} }
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type, $service) = Strings::unpackSSH2('Cs', $response); list($type, $service) = Strings::unpackSSH2('Cs', $response);
@ -2115,7 +2118,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type) = Strings::unpackSSH2('C', $response); list($type) = Strings::unpackSSH2('C', $response);
@ -2163,7 +2166,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type) = Strings::unpackSSH2('C', $response); list($type) = Strings::unpackSSH2('C', $response);
@ -2239,7 +2242,7 @@ class SSH2
$orig = $response = $this->get_binary_packet(); $orig = $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
} }
@ -2435,7 +2438,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type) = Strings::unpackSSH2('C', $response); list($type) = Strings::unpackSSH2('C', $response);
@ -2469,7 +2472,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type) = Strings::unpackSSH2('C', $response); list($type) = Strings::unpackSSH2('C', $response);
@ -2582,7 +2585,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type) = Strings::unpackSSH2('C', $response); list($type) = Strings::unpackSSH2('C', $response);
@ -2707,7 +2710,7 @@ class SSH2
$response = $this->get_binary_packet(); $response = $this->get_binary_packet();
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
list($type) = Strings::unpackSSH2('C', $response); list($type) = Strings::unpackSSH2('C', $response);
@ -2837,7 +2840,7 @@ class SSH2
$this->is_timeout = false; $this->is_timeout = false;
if (!$this->isAuthenticated()) { if (!$this->isAuthenticated()) {
throw new \RuntimeException('Operation disallowed prior to login()'); throw new InsufficientSetupException('Operation disallowed prior to login()');
} }
if (!($this->bitmap & self::MASK_SHELL) && !$this->initShell()) { if (!($this->bitmap & self::MASK_SHELL) && !$this->initShell()) {
@ -2882,7 +2885,7 @@ class SSH2
public function write($cmd) public function write($cmd)
{ {
if (!$this->isAuthenticated()) { if (!$this->isAuthenticated()) {
throw new \RuntimeException('Operation disallowed prior to login()'); throw new InsufficientSetupException('Operation disallowed prior to login()');
} }
if (!($this->bitmap & self::MASK_SHELL) && !$this->initShell()) { if (!($this->bitmap & self::MASK_SHELL) && !$this->initShell()) {
@ -3132,7 +3135,7 @@ class SSH2
{ {
if (!is_resource($this->fsock) || feof($this->fsock)) { if (!is_resource($this->fsock) || feof($this->fsock)) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed prematurely'); throw new ConnectionClosedException('Connection closed prematurely');
} }
$start = microtime(true); $start = microtime(true);
@ -3577,7 +3580,7 @@ class SSH2
$response = $this->get_binary_packet(true); $response = $this->get_binary_packet(true);
if ($response === false) { if ($response === false) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed by server'); throw new ConnectionClosedException('Connection closed by server');
} }
} }
@ -3766,7 +3769,7 @@ class SSH2
{ {
if (!is_resource($this->fsock) || feof($this->fsock)) { if (!is_resource($this->fsock) || feof($this->fsock)) {
$this->bitmap = 0; $this->bitmap = 0;
throw new \RuntimeException('Connection closed prematurely'); throw new ConnectionClosedException('Connection closed prematurely');
} }
//if ($this->compress) { //if ($this->compress) {