From 5025807abf2e5f8e8985701794474df95483d27d Mon Sep 17 00:00:00 2001 From: Jim Wigginton Date: Sun, 23 Aug 2009 03:40:50 +0000 Subject: [PATCH] - fixed two E_NOTICE issues (thanks, amer and inventor!) git-svn-id: http://phpseclib.svn.sourceforge.net/svnroot/phpseclib/trunk@42 21d32557-59b3-4da0-833f-c5933fad653e --- phpseclib/Net/SFTP.php | 18 +++++------------- phpseclib/Net/SSH2.php | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/phpseclib/Net/SFTP.php b/phpseclib/Net/SFTP.php index 1b3da631..8dd8f8b9 100644 --- a/phpseclib/Net/SFTP.php +++ b/phpseclib/Net/SFTP.php @@ -48,7 +48,7 @@ * @author Jim Wigginton * @copyright MMIX Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: SFTP.php,v 1.5 2009-08-09 03:53:28 terrafrost Exp $ + * @version $Id: SFTP.php,v 1.6 2009-08-23 03:40:50 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ @@ -145,15 +145,6 @@ class Net_SFTP extends Net_SSH2 { */ var $client_channel = 1; - /** - * The Server Channel - * - * @var Integer - * @see Net_SFTP::_initChannel() - * @access private - */ - var $server_channel = -1; - /** * The Request ID * @@ -334,7 +325,8 @@ class Net_SFTP extends Net_SSH2 { switch ($type) { case NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: $this->_string_shift($response, 4); - list(, $this->server_channel) = unpack('N', $this->_string_shift($response, 4)); + list(, $server_channel) = unpack('N', $this->_string_shift($response, 4)); + $this->server_channels[$this->client_channel] = $server_channel; break; case NET_SSH2_MSG_CHANNEL_OPEN_FAILURE: user_error('Unable to open channel', E_USER_NOTICE); @@ -342,7 +334,7 @@ class Net_SFTP extends Net_SSH2 { } $packet = pack('CNNa*CNa*', - NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channel, strlen('subsystem'), 'subsystem', 1, strlen('sftp'), 'sftp'); + NET_SSH2_MSG_CHANNEL_REQUEST, $server_channel, strlen('subsystem'), 'subsystem', 1, strlen('sftp'), 'sftp'); if (!$this->_send_binary_packet($packet)) { return false; } @@ -1189,7 +1181,7 @@ class Net_SFTP extends Net_SSH2 { $this->packet_log[] = $data; } - return $this->_send_binary_packet(pack('CN2a*', NET_SSH2_MSG_CHANNEL_DATA, $this->server_channel, strlen($data), $data)); + return $this->_send_binary_packet(pack('CN2a*', NET_SSH2_MSG_CHANNEL_DATA, $this->server_channels[$this->client_channel], strlen($data), $data)); } /** diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 175067fc..0033ccfa 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -41,7 +41,7 @@ * @author Jim Wigginton * @copyright MMVII Jim Wigginton * @license http://www.gnu.org/licenses/lgpl.txt - * @version $Id: SSH2.php,v 1.18 2009-06-09 04:00:38 terrafrost Exp $ + * @version $Id: SSH2.php,v 1.19 2009-08-23 03:40:50 terrafrost Exp $ * @link http://phpseclib.sourceforge.net */ @@ -414,6 +414,18 @@ class Net_SSH2 { */ var $get_seq_no = 0; + /** + * Server Channels + * + * Maps client channels to server channels + * + * @see Net_SSH2::_get_channel_packet() + * @see Net_SSH2::exec() + * @var Array + * @access private + */ + var $server_channels = array(); + /** * Message Number Log * @@ -1224,6 +1236,14 @@ class Net_SSH2 { return false; } + // RFC4254 refers not to client and server channels but rather to sender and recipient channels. we don't refer + // to them in that way because RFC4254 toggles the meaning. the client sends a SSH_MSG_CHANNEL_OPEN message with + // a sender channel and the server sends a SSH_MSG_CHANNEL_OPEN_CONFIRMATION in response, with a sender and a + // recepient channel. at first glance, you might conclude that SSH_MSG_CHANNEL_OPEN_CONFIRMATION's sender channel + // would be the same thing as SSH_MSG_CHANNEL_OPEN's sender channel, but it's not, per this snipet: + // The 'recipient channel' is the channel number given in the original + // open request, and 'sender channel' is the channel number allocated by + // the other side. $client_channel = 0; // PuTTy uses 0x100 // RFC4254 defines the window size as "bytes the other party can send before it must wait for the window to be // adjusted". 0x7FFFFFFF is, at 4GB, the max size. technically, it should probably be decremented, but, honestly, @@ -1254,6 +1274,7 @@ class Net_SSH2 { case NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: $this->_string_shift($response, 4); list(, $server_channel) = unpack('N', $this->_string_shift($response, 4)); + $this->server_channels[$client_channel] = $server_channel; break; case NET_SSH2_MSG_CHANNEL_OPEN_FAILURE: user_error('Unable to open channel', E_USER_NOTICE); @@ -1424,7 +1445,7 @@ class Net_SSH2 { case NET_SSH2_MSG_DISCONNECT: $this->_string_shift($payload, 1); list(, $reason_code, $length) = unpack('N2', $this->_string_shift($payload, 8)); - $this->debug_info.= "\r\n\r\nSSH_MSG_DISCONNECT:\r\n" . $this->disconnect_reasons[$reason_code] . "\r\n" . utf8_decode($this->_string_shift($payload, $temp['length'])); + $this->debug_info.= "\r\n\r\nSSH_MSG_DISCONNECT:\r\n" . $this->disconnect_reasons[$reason_code] . "\r\n" . utf8_decode($this->_string_shift($payload, $length)); $this->bitmask = 0; return false; case NET_SSH2_MSG_IGNORE: @@ -1475,10 +1496,11 @@ class Net_SSH2 { list(, $length) = unpack('N', $payload); $this->debug_info.= "\r\n\r\nSSH_MSG_CHANNEL_OPEN:\r\n" . utf8_decode($this->_string_shift($payload, $length)); - list(, $recipient_channel) = unpack('N', $this->_string_shift($payload, 4)); + $this->_string_shift($payload, 4); // skip over client channel + list(, $server_channel) = unpack('N', $this->_string_shift($payload, 4)); $packet = pack('CN3a*Na*', - NET_SSH2_MSG_REQUEST_FAILURE, $recipient_channel, NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED, 0, '', 0, ''); + NET_SSH2_MSG_REQUEST_FAILURE, $server_channel, NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED, 0, '', 0, ''); if (!$this->_send_binary_packet($packet)) { return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION); @@ -1515,11 +1537,11 @@ class Net_SSH2 { switch ($type) { case NET_SSH2_MSG_CHANNEL_DATA: - $this->_string_shift($response, 4); // skip over server channel + $this->_string_shift($response, 4); // skip over client channel list(, $length) = unpack('N', $this->_string_shift($response, 4)); return $this->_string_shift($response, $length); case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA: - $this->_string_shift($response, 4); // skip over server channel + $this->_string_shift($response, 4); // skip over client channel list(, $data_type_code, $length) = unpack('N2', $this->_string_shift($response, 8)); $data = $this->_string_shift($response, $length); switch ($data_type_code) { @@ -1528,7 +1550,7 @@ class Net_SSH2 { } break; case NET_SSH2_MSG_CHANNEL_REQUEST: - $this->_string_shift($response, 4); // skip over server channel + $this->_string_shift($response, 4); // skip over client channel list(, $length) = unpack('N', $this->_string_shift($response, 4)); $value = $this->_string_shift($response, $length); switch ($value) { @@ -1548,7 +1570,8 @@ class Net_SSH2 { } break; case NET_SSH2_MSG_CHANNEL_CLOSE: - $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $server_channel)); + list(, $client_channel) = unpack('N', $this->_string_shift($response, 4)); + $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel])); return true; case NET_SSH2_MSG_CHANNEL_EOF: break;