1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-02 17:52:59 +01:00

SSH/SFTP: make message numbers / packet types static as well

This commit is contained in:
terrafrost 2023-03-23 12:38:39 -05:00
parent 9705cbbc26
commit b799abd1a0
2 changed files with 244 additions and 240 deletions

View File

@ -93,7 +93,7 @@ class SFTP extends SSH2
* @var array * @var array
* @access private * @access private
*/ */
private $packet_types = []; private static $packet_types = [];
/** /**
* Status Codes * Status Codes
@ -102,19 +102,19 @@ class SFTP extends SSH2
* @var array * @var array
* @access private * @access private
*/ */
private $status_codes = []; private static $status_codes = [];
/** @var array<int, string> */ /** @var array<int, string> */
private $attributes; private static $attributes;
/** @var array<int, string> */ /** @var array<int, string> */
private $open_flags; private static $open_flags;
/** @var array<int, string> */ /** @var array<int, string> */
private $open_flags5; private static $open_flags5;
/** @var array<int, string> */ /** @var array<int, string> */
private $file_types; private static $file_types;
/** /**
* The Request ID * The Request ID
@ -360,7 +360,8 @@ class SFTP extends SSH2
$this->max_sftp_packet = 1 << 15; $this->max_sftp_packet = 1 << 15;
$this->packet_types = [ if (empty(self::$packet_types)) {
self::$packet_types = [
1 => 'NET_SFTP_INIT', 1 => 'NET_SFTP_INIT',
2 => 'NET_SFTP_VERSION', 2 => 'NET_SFTP_VERSION',
3 => 'NET_SFTP_OPEN', 3 => 'NET_SFTP_OPEN',
@ -390,7 +391,7 @@ class SFTP extends SSH2
200 => 'NET_SFTP_EXTENDED' 200 => 'NET_SFTP_EXTENDED'
]; ];
$this->status_codes = [ self::$status_codes = [
0 => 'NET_SFTP_STATUS_OK', 0 => 'NET_SFTP_STATUS_OK',
1 => 'NET_SFTP_STATUS_EOF', 1 => 'NET_SFTP_STATUS_EOF',
2 => 'NET_SFTP_STATUS_NO_SUCH_FILE', 2 => 'NET_SFTP_STATUS_NO_SUCH_FILE',
@ -426,7 +427,7 @@ class SFTP extends SSH2
]; ];
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-7.1 // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-7.1
// the order, in this case, matters quite a lot - see \phpseclib3\Net\SFTP::_parseAttributes() to understand why // the order, in this case, matters quite a lot - see \phpseclib3\Net\SFTP::_parseAttributes() to understand why
$this->attributes = [ self::$attributes = [
0x00000001 => 'NET_SFTP_ATTR_SIZE', 0x00000001 => 'NET_SFTP_ATTR_SIZE',
0x00000002 => 'NET_SFTP_ATTR_UIDGID', // defined in SFTPv3, removed in SFTPv4+ 0x00000002 => 'NET_SFTP_ATTR_UIDGID', // defined in SFTPv3, removed in SFTPv4+
0x00000080 => 'NET_SFTP_ATTR_OWNERGROUP', // defined in SFTPv4+ 0x00000080 => 'NET_SFTP_ATTR_OWNERGROUP', // defined in SFTPv4+
@ -452,7 +453,7 @@ class SFTP extends SSH2
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3 // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-6.3
// the flag definitions change somewhat in SFTPv5+. if SFTPv5+ support is added to this library, maybe name // the flag definitions change somewhat in SFTPv5+. if SFTPv5+ support is added to this library, maybe name
// the array for that $this->open5_flags and similarly alter the constant names. // the array for that $this->open5_flags and similarly alter the constant names.
$this->open_flags = [ self::$open_flags = [
0x00000001 => 'NET_SFTP_OPEN_READ', 0x00000001 => 'NET_SFTP_OPEN_READ',
0x00000002 => 'NET_SFTP_OPEN_WRITE', 0x00000002 => 'NET_SFTP_OPEN_WRITE',
0x00000004 => 'NET_SFTP_OPEN_APPEND', 0x00000004 => 'NET_SFTP_OPEN_APPEND',
@ -463,7 +464,7 @@ class SFTP extends SSH2
]; ];
// SFTPv5+ changed the flags up: // SFTPv5+ changed the flags up:
// https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-13#section-8.1.1.3 // https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-13#section-8.1.1.3
$this->open_flags5 = [ self::$open_flags5 = [
// when SSH_FXF_ACCESS_DISPOSITION is a 3 bit field that controls how the file is opened // when SSH_FXF_ACCESS_DISPOSITION is a 3 bit field that controls how the file is opened
0x00000000 => 'NET_SFTP_OPEN_CREATE_NEW', 0x00000000 => 'NET_SFTP_OPEN_CREATE_NEW',
0x00000001 => 'NET_SFTP_OPEN_CREATE_TRUNCATE', 0x00000001 => 'NET_SFTP_OPEN_CREATE_TRUNCATE',
@ -487,7 +488,7 @@ class SFTP extends SSH2
]; ];
// http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2 // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2
// see \phpseclib3\Net\SFTP::_parseLongname() for an explanation // see \phpseclib3\Net\SFTP::_parseLongname() for an explanation
$this->file_types = [ self::$file_types = [
1 => 'NET_SFTP_TYPE_REGULAR', 1 => 'NET_SFTP_TYPE_REGULAR',
2 => 'NET_SFTP_TYPE_DIRECTORY', 2 => 'NET_SFTP_TYPE_DIRECTORY',
3 => 'NET_SFTP_TYPE_SYMLINK', 3 => 'NET_SFTP_TYPE_SYMLINK',
@ -501,13 +502,14 @@ class SFTP extends SSH2
9 => 'NET_SFTP_TYPE_FIFO' 9 => 'NET_SFTP_TYPE_FIFO'
]; ];
self::define_array( self::define_array(
$this->packet_types, self::$packet_types,
$this->status_codes, self::$status_codes,
$this->attributes, self::$attributes,
$this->open_flags, self::$open_flags,
$this->open_flags5, self::$open_flags5,
$this->file_types self::$file_types
); );
}
if (!defined('NET_SFTP_QUEUE_SIZE')) { if (!defined('NET_SFTP_QUEUE_SIZE')) {
define('NET_SFTP_QUEUE_SIZE', 32); define('NET_SFTP_QUEUE_SIZE', 32);
@ -815,7 +817,7 @@ class SFTP extends SSH2
list($status) = Strings::unpackSSH2('N', $response); list($status) = Strings::unpackSSH2('N', $response);
} }
$error = $this->status_codes[$status]; $error = self::$status_codes[$status];
if ($this->version > 2) { if ($this->version > 2) {
list($message) = Strings::unpackSSH2('s', $response); list($message) = Strings::unpackSSH2('s', $response);
@ -3041,7 +3043,7 @@ class SFTP extends SSH2
list($flags) = Strings::unpackSSH2('N', $response); list($flags) = Strings::unpackSSH2('N', $response);
} }
foreach ($this->attributes as $key => $value) { foreach (self::$attributes as $key => $value) {
switch ($flags & $key) { switch ($flags & $key) {
case NET_SFTP_ATTR_UIDGID: case NET_SFTP_ATTR_UIDGID:
if ($this->version > 3) { if ($this->version > 3) {
@ -3272,7 +3274,7 @@ class SFTP extends SSH2
$stop = microtime(true); $stop = microtime(true);
if (defined('NET_SFTP_LOGGING')) { if (defined('NET_SFTP_LOGGING')) {
$packet_type = '-> ' . $this->packet_types[$type] . $packet_type = '-> ' . self::$packet_types[$type] .
' (' . round($stop - $start, 4) . 's)'; ' (' . round($stop - $start, 4) . 's)';
$this->append_log($packet_type, $data); $this->append_log($packet_type, $data);
} }
@ -3376,7 +3378,7 @@ class SFTP extends SSH2
$packet = Strings::shift($this->packet_buffer, $length); $packet = Strings::shift($this->packet_buffer, $length);
if (defined('NET_SFTP_LOGGING')) { if (defined('NET_SFTP_LOGGING')) {
$packet_type = '<- ' . $this->packet_types[$this->packet_type] . $packet_type = '<- ' . self::$packet_types[$this->packet_type] .
' (' . round($stop - $start, 4) . 's)'; ' (' . round($stop - $start, 4) . 's)';
$this->append_log($packet_type, $packet); $this->append_log($packet_type, $packet);
} }

View File

@ -553,7 +553,7 @@ class SSH2
* @var array * @var array
* @access private * @access private
*/ */
private $message_numbers = []; private static $message_numbers = [];
/** /**
* Disconnection Message 'reason codes' defined in RFC4253 * Disconnection Message 'reason codes' defined in RFC4253
@ -562,7 +562,7 @@ class SSH2
* @var array * @var array
* @access private * @access private
*/ */
private $disconnect_reasons = []; private static $disconnect_reasons = [];
/** /**
* SSH_MSG_CHANNEL_OPEN_FAILURE 'reason codes', defined in RFC4254 * SSH_MSG_CHANNEL_OPEN_FAILURE 'reason codes', defined in RFC4254
@ -571,7 +571,7 @@ class SSH2
* @var array * @var array
* @access private * @access private
*/ */
private $channel_open_failure_reasons = []; private static $channel_open_failure_reasons = [];
/** /**
* Terminal Modes * Terminal Modes
@ -581,7 +581,7 @@ class SSH2
* @var array * @var array
* @access private * @access private
*/ */
private $terminal_modes = []; private static $terminal_modes = [];
/** /**
* SSH_MSG_CHANNEL_EXTENDED_DATA's data_type_codes * SSH_MSG_CHANNEL_EXTENDED_DATA's data_type_codes
@ -591,7 +591,7 @@ class SSH2
* @var array * @var array
* @access private * @access private
*/ */
private $channel_extended_data_type_codes = []; private static $channel_extended_data_type_codes = [];
/** /**
* Send Sequence Number * Send Sequence Number
@ -1099,7 +1099,8 @@ class SSH2
*/ */
public function __construct($host, $port = 22, $timeout = 10) public function __construct($host, $port = 22, $timeout = 10)
{ {
$this->message_numbers = [ if (empty(self::$message_numbers)) {
self::$message_numbers = [
1 => 'NET_SSH2_MSG_DISCONNECT', 1 => 'NET_SSH2_MSG_DISCONNECT',
2 => 'NET_SSH2_MSG_IGNORE', 2 => 'NET_SSH2_MSG_IGNORE',
3 => 'NET_SSH2_MSG_UNIMPLEMENTED', 3 => 'NET_SSH2_MSG_UNIMPLEMENTED',
@ -1130,7 +1131,7 @@ class SSH2
99 => 'NET_SSH2_MSG_CHANNEL_SUCCESS', 99 => 'NET_SSH2_MSG_CHANNEL_SUCCESS',
100 => 'NET_SSH2_MSG_CHANNEL_FAILURE' 100 => 'NET_SSH2_MSG_CHANNEL_FAILURE'
]; ];
$this->disconnect_reasons = [ self::$disconnect_reasons = [
1 => 'NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT', 1 => 'NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT',
2 => 'NET_SSH2_DISCONNECT_PROTOCOL_ERROR', 2 => 'NET_SSH2_DISCONNECT_PROTOCOL_ERROR',
3 => 'NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED', 3 => 'NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED',
@ -1147,22 +1148,22 @@ class SSH2
14 => 'NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE', 14 => 'NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE',
15 => 'NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME' 15 => 'NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME'
]; ];
$this->channel_open_failure_reasons = [ self::$channel_open_failure_reasons = [
1 => 'NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED' 1 => 'NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED'
]; ];
$this->terminal_modes = [ self::$terminal_modes = [
0 => 'NET_SSH2_TTY_OP_END' 0 => 'NET_SSH2_TTY_OP_END'
]; ];
$this->channel_extended_data_type_codes = [ self::$channel_extended_data_type_codes = [
1 => 'NET_SSH2_EXTENDED_DATA_STDERR' 1 => 'NET_SSH2_EXTENDED_DATA_STDERR'
]; ];
self::define_array( self::define_array(
$this->message_numbers, self::$message_numbers,
$this->disconnect_reasons, self::$disconnect_reasons,
$this->channel_open_failure_reasons, self::$channel_open_failure_reasons,
$this->terminal_modes, self::$terminal_modes,
$this->channel_extended_data_type_codes, self::$channel_extended_data_type_codes,
[60 => 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ'], [60 => 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ'],
[60 => 'NET_SSH2_MSG_USERAUTH_PK_OK'], [60 => 'NET_SSH2_MSG_USERAUTH_PK_OK'],
[60 => 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST', [60 => 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
@ -1177,6 +1178,7 @@ class SSH2
[30 => 'NET_SSH2_MSG_KEX_ECDH_INIT', [30 => 'NET_SSH2_MSG_KEX_ECDH_INIT',
31 => 'NET_SSH2_MSG_KEX_ECDH_REPLY'] 31 => 'NET_SSH2_MSG_KEX_ECDH_REPLY']
); );
}
/** /**
* Typehint is required due to a bug in Psalm: https://github.com/vimeo/psalm/issues/7508 * Typehint is required due to a bug in Psalm: https://github.com/vimeo/psalm/issues/7508
@ -3600,7 +3602,7 @@ class SSH2
if (defined('NET_SSH2_LOGGING')) { if (defined('NET_SSH2_LOGGING')) {
$current = microtime(true); $current = microtime(true);
$message_number = isset($this->message_numbers[ord($payload[0])]) ? $this->message_numbers[ord($payload[0])] : 'UNKNOWN (' . ord($payload[0]) . ')'; $message_number = isset(self::$message_numbers[ord($payload[0])]) ? self::$message_numbers[ord($payload[0])] : 'UNKNOWN (' . ord($payload[0]) . ')';
$message_number = '<- ' . $message_number . $message_number = '<- ' . $message_number .
' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)'; ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
$this->append_log($message_number, $payload); $this->append_log($message_number, $payload);
@ -3682,7 +3684,7 @@ class SSH2
case NET_SSH2_MSG_DISCONNECT: case NET_SSH2_MSG_DISCONNECT:
Strings::shift($payload, 1); Strings::shift($payload, 1);
list($reason_code, $message) = Strings::unpackSSH2('Ns', $payload); list($reason_code, $message) = Strings::unpackSSH2('Ns', $payload);
$this->errors[] = 'SSH_MSG_DISCONNECT: ' . $this->disconnect_reasons[$reason_code] . "\r\n$message"; $this->errors[] = 'SSH_MSG_DISCONNECT: ' . static::$disconnect_reasons[$reason_code] . "\r\n$message";
$this->bitmap = 0; $this->bitmap = 0;
return false; return false;
case NET_SSH2_MSG_IGNORE: case NET_SSH2_MSG_IGNORE:
@ -4255,7 +4257,7 @@ class SSH2
if (defined('NET_SSH2_LOGGING')) { if (defined('NET_SSH2_LOGGING')) {
$current = microtime(true); $current = microtime(true);
$message_number = isset($this->message_numbers[ord($logged[0])]) ? $this->message_numbers[ord($logged[0])] : 'UNKNOWN (' . ord($logged[0]) . ')'; $message_number = isset(self::$message_numbers[ord($logged[0])]) ? self::$message_numbers[ord($logged[0])] : 'UNKNOWN (' . ord($logged[0]) . ')';
$message_number = '-> ' . $message_number . $message_number = '-> ' . $message_number .
' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)'; ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
$this->append_log($message_number, $logged); $this->append_log($message_number, $logged);