,
- // "if the owner or group is specified as -1, then that ID is not changed"
- $attr = pack('N3', NET_SFTP_ATTR_UIDGID, $uid, -1);
-
- return $this->_setstat($filename, $attr, $recursive);
- }
-
- /**
- * Changes file or directory group
- *
- * Returns true on success or false on error.
- *
- * @param String $filename
- * @param Integer $gid
- * @param optional Boolean $recursive
- * @return Boolean
- * @access public
- */
- function chgrp($filename, $gid, $recursive = false)
- {
- $attr = pack('N3', NET_SFTP_ATTR_UIDGID, -1, $gid);
-
- return $this->_setstat($filename, $attr, $recursive);
- }
-
- /**
- * Set permissions on a file.
- *
- * Returns the new file permissions on success or false on error.
- * If $recursive is true than this just returns true or false.
- *
- * @param Integer $mode
- * @param String $filename
- * @param optional Boolean $recursive
- * @return Mixed
- * @access public
- */
- function chmod($mode, $filename, $recursive = false)
- {
- if (is_string($mode) && is_int($filename)) {
- $temp = $mode;
- $mode = $filename;
- $filename = $temp;
- }
-
- $attr = pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777);
- if (!$this->_setstat($filename, $attr, $recursive)) {
- return false;
- }
- if ($recursive) {
- return true;
- }
-
- // rather than return what the permissions *should* be, we'll return what they actually are. this will also
- // tell us if the file actually exists.
- // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following:
- $packet = pack('Na*', strlen($filename), $filename);
- if (!$this->_send_sftp_packet(NET_SFTP_STAT, $packet)) {
- return false;
- }
-
- $response = $this->_get_sftp_packet();
- switch ($this->packet_type) {
- case NET_SFTP_ATTRS:
- $attrs = $this->_parseAttributes($response);
- return $attrs['permissions'];
- case NET_SFTP_STATUS:
- $this->_logError($response);
- return false;
- }
-
- user_error('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS');
- return false;
- }
-
- /**
- * Sets information about a file
- *
- * @param String $filename
- * @param String $attr
- * @param Boolean $recursive
- * @return Boolean
- * @access private
- */
- function _setstat($filename, $attr, $recursive)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
- $filename = $this->_realpath($filename);
- if ($filename === false) {
- return false;
- }
-
- if ($recursive) {
- $i = 0;
- $result = $this->_setstat_recursive($filename, $attr, $i);
- $this->_read_put_responses($i);
- return $result;
- }
-
- // SFTPv4+ has an additional byte field - type - that would need to be sent, as well. setting it to
- // SSH_FILEXFER_TYPE_UNKNOWN might work. if not, we'd have to do an SSH_FXP_STAT before doing an SSH_FXP_SETSTAT.
- if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($filename), $filename, $attr))) {
- return false;
- }
-
- /*
- "Because some systems must use separate system calls to set various attributes, it is possible that a failure
- response will be returned, but yet some of the attributes may be have been successfully modified. If possible,
- servers SHOULD avoid this situation; however, clients MUST be aware that this is possible."
-
- -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.6
- */
- $response = $this->_get_sftp_packet();
- if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS');
- return false;
- }
-
- extract(unpack('Nstatus', $this->_string_shift($response, 4)));
- if ($status != NET_SFTP_STATUS_OK) {
- $this->_logError($response, $status);
- return false;
- }
-
- return true;
- }
-
- /**
- * Recursively sets information on directories on the SFTP server
- *
- * Minimizes directory lookups and SSH_FXP_STATUS requests for speed.
- *
- * @param String $path
- * @param String $attr
- * @param Integer $i
- * @return Boolean
- * @access private
- */
- function _setstat_recursive($path, $attr, &$i)
- {
- if (!$this->_read_put_responses($i)) {
- return false;
- }
- $i = 0;
- $entries = $this->_list($path, true, false);
-
- if ($entries === false) {
- return $this->_setstat($path, $attr, false);
- }
-
- // normally $entries would have at least . and .. but it might not if the directories
- // permissions didn't allow reading
- if (empty($entries)) {
- return false;
- }
-
- foreach ($entries as $filename=>$props) {
- if ($filename == '.' || $filename == '..') {
- continue;
- }
-
- if (!isset($props['type'])) {
- return false;
- }
-
- $temp = $path . '/' . $filename;
- if ($props['type'] == NET_SFTP_TYPE_DIRECTORY) {
- if (!$this->_setstat_recursive($temp, $attr, $i)) {
- return false;
- }
- } else {
- if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($temp), $temp, $attr))) {
- return false;
- }
-
- $i++;
-
- if ($i >= NET_SFTP_QUEUE_SIZE) {
- if (!$this->_read_put_responses($i)) {
- return false;
- }
- $i = 0;
- }
- }
- }
-
- if (!$this->_send_sftp_packet(NET_SFTP_SETSTAT, pack('Na*a*', strlen($path), $path, $attr))) {
- return false;
- }
-
- $i++;
-
- if ($i >= NET_SFTP_QUEUE_SIZE) {
- if (!$this->_read_put_responses($i)) {
- return false;
- }
- $i = 0;
- }
-
- return true;
- }
-
- /**
- * Creates a directory.
- *
- * @param String $dir
- * @return Boolean
- * @access public
- */
- function mkdir($dir, $mode = -1, $recursive = false)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
- $dir = $this->_realpath($dir);
- // by not providing any permissions, hopefully the server will use the logged in users umask - their
- // default permissions.
- $attr = $mode == -1 ? "\0\0\0\0" : pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777);
-
- if ($recursive) {
- $dirs = explode('/', preg_replace('#/(?=/)|/$#', '', $dir));
- if (empty($dirs[0])) {
- array_shift($dirs);
- $dirs[0] = '/' . $dirs[0];
- }
- for ($i = 0; $i < count($dirs); $i++) {
- $temp = array_slice($dirs, 0, $i + 1);
- $temp = implode('/', $temp);
- $result = $this->_mkdir_helper($temp, $attr);
- }
- return $result;
- }
-
- return $this->_mkdir_helper($dir, $attr);
- }
-
- /**
- * Helper function for directory creation
- *
- * @param String $dir
- * @return Boolean
- * @access private
- */
- function _mkdir_helper($dir, $attr)
- {
- if (!$this->_send_sftp_packet(NET_SFTP_MKDIR, pack('Na*a*', strlen($dir), $dir, $attr))) {
- return false;
- }
-
- $response = $this->_get_sftp_packet();
- if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS');
- return false;
- }
-
- extract(unpack('Nstatus', $this->_string_shift($response, 4)));
- if ($status != NET_SFTP_STATUS_OK) {
- $this->_logError($response, $status);
- return false;
- }
-
- $this->_save_dir($dir);
-
- return true;
- }
-
- /**
- * Removes a directory.
- *
- * @param String $dir
- * @return Boolean
- * @access public
- */
- function rmdir($dir)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
- $dir = $this->_realpath($dir);
- if ($dir === false) {
- return false;
- }
-
- if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($dir), $dir))) {
- return false;
- }
-
- $response = $this->_get_sftp_packet();
- if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS');
- return false;
- }
-
- extract(unpack('Nstatus', $this->_string_shift($response, 4)));
- if ($status != NET_SFTP_STATUS_OK) {
- // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED?
- $this->_logError($response, $status);
- return false;
- }
-
- $this->_remove_dir($dir);
-
- return true;
- }
-
- /**
- * Uploads a file to the SFTP server.
- *
- * By default, Net_SFTP::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 Net_SFTP::get(), you will get a file, twelve bytes
- * long, containing 'filename.ext' as its contents.
- *
- * Setting $mode to NET_SFTP_LOCAL_FILE will change the above behavior. With NET_SFTP_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.
- *
- * $mode can take an additional two parameters - NET_SFTP_RESUME and NET_SFTP_RESUME_START. These are bitwise AND'd with
- * $mode. So if you want to resume upload of a 300mb file on the local file system you'd set $mode to the following:
- *
- * NET_SFTP_LOCAL_FILE | NET_SFTP_RESUME
- *
- * If you wanted to simply append the full contents of a local file to the full contents of a remote file you'd replace
- * NET_SFTP_RESUME with NET_SFTP_RESUME_START.
- *
- * If $mode & (NET_SFTP_RESUME | NET_SFTP_RESUME_START) then NET_SFTP_RESUME_START will be assumed.
- *
- * $start and $local_start give you more fine grained control over this process and take precident over NET_SFTP_RESUME
- * when they're non-negative. ie. $start could let you write at the end of a file (like NET_SFTP_RESUME) or in the middle
- * of one. $local_start could let you start your reading from the end of a file (like NET_SFTP_RESUME_START) or in the
- * middle of one.
- *
- * Setting $local_start to > 0 or $mode | NET_SFTP_RESUME_START doesn't do anything unless $mode | NET_SFTP_LOCAL_FILE.
- *
- * @param String $remote_file
- * @param String $data
- * @param optional Integer $mode
- * @param optional Integer $start
- * @param optional Integer $local_start
- * @return Boolean
- * @access public
- * @internal ASCII mode for SFTPv4/5/6 can be supported by adding a new function - Net_SFTP::setMode().
- */
- function put($remote_file, $data, $mode = NET_SFTP_STRING, $start = -1, $local_start = -1)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
- $remote_file = $this->_realpath($remote_file);
- if ($remote_file === false) {
- return false;
- }
-
- $flags = NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE;
- // according to the SFTP specs, NET_SFTP_OPEN_APPEND should "force all writes to append data at the end of the file."
- // in practice, it doesn't seem to do that.
- //$flags|= ($mode & NET_SFTP_RESUME) ? NET_SFTP_OPEN_APPEND : NET_SFTP_OPEN_TRUNCATE;
-
- if ($start >= 0) {
- $offset = $start;
- } elseif ($mode & NET_SFTP_RESUME) {
- // if NET_SFTP_OPEN_APPEND worked as it should _size() wouldn't need to be called
- $size = $this->_size($remote_file);
- $offset = $size !== false ? $size : 0;
- } else {
- $offset = 0;
- $flags|= NET_SFTP_OPEN_TRUNCATE;
- }
-
- $packet = pack('Na*N2', strlen($remote_file), $remote_file, $flags, 0);
- if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
- return false;
- }
-
- $response = $this->_get_sftp_packet();
- switch ($this->packet_type) {
- case NET_SFTP_HANDLE:
- $handle = substr($response, 4);
- break;
- case NET_SFTP_STATUS:
- $this->_logError($response);
- return false;
- default:
- user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
- return false;
- }
-
- // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.2.3
- if ($mode & NET_SFTP_LOCAL_FILE) {
- if (!is_file($data)) {
- user_error("$data is not a valid file");
- return false;
- }
- $fp = @fopen($data, 'rb');
- if (!$fp) {
- return false;
- }
- $size = filesize($data);
-
- if ($local_start >= 0) {
- fseek($fp, $local_start);
- } elseif ($mode & NET_SFTP_RESUME_START) {
- // do nothing
- } else {
- fseek($fp, $offset);
- }
- } else {
- $size = strlen($data);
- }
-
- $sent = 0;
- $size = $size < 0 ? ($size & 0x7FFFFFFF) + 0x80000000 : $size;
-
- $sftp_packet_size = 4096; // PuTTY uses 4096
- // make the SFTP packet be exactly 4096 bytes by including the bytes in the NET_SFTP_WRITE packets "header"
- $sftp_packet_size-= strlen($handle) + 25;
- $i = 0;
- while ($sent < $size) {
- $temp = $mode & NET_SFTP_LOCAL_FILE ? fread($fp, $sftp_packet_size) : substr($data, $sent, $sftp_packet_size);
- $subtemp = $offset + $sent;
- $packet = pack('Na*N3a*', strlen($handle), $handle, $subtemp / 4294967296, $subtemp, strlen($temp), $temp);
- if (!$this->_send_sftp_packet(NET_SFTP_WRITE, $packet)) {
- fclose($fp);
- return false;
- }
- $sent+= strlen($temp);
-
- $i++;
-
- if ($i == NET_SFTP_QUEUE_SIZE) {
- if (!$this->_read_put_responses($i)) {
- $i = 0;
- break;
- }
- $i = 0;
- }
- }
-
- if (!$this->_read_put_responses($i)) {
- if ($mode & NET_SFTP_LOCAL_FILE) {
- fclose($fp);
- }
- $this->_close_handle($handle);
- return false;
- }
-
- if ($mode & NET_SFTP_LOCAL_FILE) {
- fclose($fp);
- }
-
- return $this->_close_handle($handle);
- }
-
- /**
- * Reads multiple successive SSH_FXP_WRITE responses
- *
- * Sending an SSH_FXP_WRITE packet and immediately reading its response isn't as efficient as blindly sending out $i
- * SSH_FXP_WRITEs, in succession, and then reading $i responses.
- *
- * @param Integer $i
- * @return Boolean
- * @access private
- */
- function _read_put_responses($i)
- {
- while ($i--) {
- $response = $this->_get_sftp_packet();
- if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS');
- return false;
- }
-
- extract(unpack('Nstatus', $this->_string_shift($response, 4)));
- if ($status != NET_SFTP_STATUS_OK) {
- $this->_logError($response, $status);
- break;
- }
- }
-
- return $i < 0;
- }
-
- /**
- * Close handle
- *
- * @param String $handle
- * @return Boolean
- * @access private
- */
- function _close_handle($handle)
- {
- if (!$this->_send_sftp_packet(NET_SFTP_CLOSE, pack('Na*', strlen($handle), $handle))) {
- return false;
- }
-
- // "The client MUST release all resources associated with the handle regardless of the status."
- // -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.1.3
- $response = $this->_get_sftp_packet();
- if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS');
- return false;
- }
-
- extract(unpack('Nstatus', $this->_string_shift($response, 4)));
- if ($status != NET_SFTP_STATUS_OK) {
- $this->_logError($response, $status);
- return false;
- }
-
- return true;
- }
-
- /**
- * Downloads a file from the SFTP 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.
- *
- * $offset and $length can be used to download files in chunks.
- *
- * @param String $remote_file
- * @param optional String $local_file
- * @param optional Integer $offset
- * @param optional Integer $length
- * @return Mixed
- * @access public
- */
- function get($remote_file, $local_file = false, $offset = 0, $length = -1)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
- $remote_file = $this->_realpath($remote_file);
- if ($remote_file === false) {
- return false;
- }
-
- $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0);
- if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
- return false;
- }
-
- $response = $this->_get_sftp_packet();
- switch ($this->packet_type) {
- case NET_SFTP_HANDLE:
- $handle = substr($response, 4);
- break;
- case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
- $this->_logError($response);
- return false;
- default:
- user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
- return false;
- }
-
- if ($local_file !== false) {
- $fp = fopen($local_file, 'wb');
- if (!$fp) {
- return false;
- }
- } else {
- $content = '';
- }
-
- $start = $offset;
- $size = $this->max_sftp_packet < $length || $length < 0 ? $this->max_sftp_packet : $length;
- while (true) {
- $packet = pack('Na*N3', strlen($handle), $handle, $offset / 4294967296, $offset, $size);
- if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet)) {
- if ($local_file !== false) {
- fclose($fp);
- }
- return false;
- }
-
- $response = $this->_get_sftp_packet();
- switch ($this->packet_type) {
- case NET_SFTP_DATA:
- $temp = substr($response, 4);
- $offset+= strlen($temp);
- if ($local_file === false) {
- $content.= $temp;
- } else {
- fputs($fp, $temp);
- }
- break;
- case NET_SFTP_STATUS:
- // could, in theory, return false if !strlen($content) but we'll hold off for the time being
- $this->_logError($response);
- break 2;
- default:
- user_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS');
- if ($local_file !== false) {
- fclose($fp);
- }
- return false;
- }
-
- if ($length > 0 && $length <= $offset - $start) {
- break;
- }
- }
-
- if ($length > 0 && $length <= $offset - $start) {
- if ($local_file === false) {
- $content = substr($content, 0, $length);
- } else {
- ftruncate($fp, $length);
- }
- }
-
- if ($local_file !== false) {
- fclose($fp);
- }
-
- if (!$this->_close_handle($handle)) {
- return false;
- }
-
- // if $content isn't set that means a file was written to
- return isset($content) ? $content : true;
- }
-
- /**
- * Deletes a file on the SFTP server.
- *
- * @param String $path
- * @param Boolean $recursive
- * @return Boolean
- * @access public
- */
- function delete($path, $recursive = true)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
- $path = $this->_realpath($path);
- if ($path === false) {
- return false;
- }
-
- // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3
- if (!$this->_send_sftp_packet(NET_SFTP_REMOVE, pack('Na*', strlen($path), $path))) {
- return false;
- }
-
- $response = $this->_get_sftp_packet();
- if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS');
- return false;
- }
-
- // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
- extract(unpack('Nstatus', $this->_string_shift($response, 4)));
- if ($status != NET_SFTP_STATUS_OK) {
- $this->_logError($response, $status);
- if (!$recursive) {
- return false;
- }
- $i = 0;
- $result = $this->_delete_recursive($path, $i);
- $this->_read_put_responses($i);
- return $result;
- }
-
- return true;
- }
-
- /**
- * Recursively deletes directories on the SFTP server
- *
- * Minimizes directory lookups and SSH_FXP_STATUS requests for speed.
- *
- * @param String $path
- * @param Integer $i
- * @return Boolean
- * @access private
- */
- function _delete_recursive($path, &$i)
- {
- if (!$this->_read_put_responses($i)) {
- return false;
- }
- $i = 0;
- $entries = $this->_list($path, true, false);
-
- // normally $entries would have at least . and .. but it might not if the directories
- // permissions didn't allow reading
- if (empty($entries)) {
- return false;
- }
-
- foreach ($entries as $filename=>$props) {
- if ($filename == '.' || $filename == '..') {
- continue;
- }
-
- if (!isset($props['type'])) {
- return false;
- }
-
- $temp = $path . '/' . $filename;
- if ($props['type'] == NET_SFTP_TYPE_DIRECTORY) {
- if (!$this->_delete_recursive($temp, $i)) {
- return false;
- }
- } else {
- if (!$this->_send_sftp_packet(NET_SFTP_REMOVE, pack('Na*', strlen($temp), $temp))) {
- return false;
- }
-
- $i++;
-
- if ($i >= NET_SFTP_QUEUE_SIZE) {
- if (!$this->_read_put_responses($i)) {
- return false;
- }
- $i = 0;
- }
- }
- }
-
- if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($path), $path))) {
- return false;
- }
- $this->_remove_dir($path);
-
- $i++;
-
- if ($i >= NET_SFTP_QUEUE_SIZE) {
- if (!$this->_read_put_responses($i)) {
- return false;
- }
- $i = 0;
- }
-
- return true;
- }
-
- /**
- * Renames a file or a directory on the SFTP server
- *
- * @param String $oldname
- * @param String $newname
- * @return Boolean
- * @access public
- */
- function rename($oldname, $newname)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
- $oldname = $this->_realpath($oldname);
- $newname = $this->_realpath($newname);
- if ($oldname === false || $newname === false) {
- return false;
- }
-
- // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3
- $packet = pack('Na*Na*', strlen($oldname), $oldname, strlen($newname), $newname);
- if (!$this->_send_sftp_packet(NET_SFTP_RENAME, $packet)) {
- return false;
- }
-
- $response = $this->_get_sftp_packet();
- if ($this->packet_type != NET_SFTP_STATUS) {
- user_error('Expected SSH_FXP_STATUS');
- return false;
- }
-
- // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
- extract(unpack('Nstatus', $this->_string_shift($response, 4)));
- if ($status != NET_SFTP_STATUS_OK) {
- $this->_logError($response, $status);
- return false;
- }
-
- return true;
- }
-
- /**
- * Parse Attributes
- *
- * See '7. File Attributes' of draft-ietf-secsh-filexfer-13 for more info.
- *
- * @param String $response
- * @return Array
- * @access private
- */
- function _parseAttributes(&$response)
- {
- $attr = array();
- extract(unpack('Nflags', $this->_string_shift($response, 4)));
- // SFTPv4+ have a type field (a byte) that follows the above flag field
- foreach ($this->attributes as $key => $value) {
- switch ($flags & $key) {
- case NET_SFTP_ATTR_SIZE: // 0x00000001
- // size is represented by a 64-bit integer, so we perhaps ought to be doing the following:
- // $attr['size'] = new Math_BigInteger($this->_string_shift($response, 8), 256);
- // of course, you shouldn't be using Net_SFTP to transfer files that are in excess of 4GB
- // (0xFFFFFFFF bytes), anyway. as such, we'll just represent all file sizes that are bigger than
- // 4GB as being 4GB.
- extract(unpack('Nupper/Nsize', $this->_string_shift($response, 8)));
- $attr['size'] = $upper ? 4294967296 * $upper : 0;
- $attr['size']+= $size < 0 ? ($size & 0x7FFFFFFF) + 0x80000000 : $size;
- break;
- case NET_SFTP_ATTR_UIDGID: // 0x00000002 (SFTPv3 only)
- $attr+= unpack('Nuid/Ngid', $this->_string_shift($response, 8));
- break;
- case NET_SFTP_ATTR_PERMISSIONS: // 0x00000004
- $attr+= unpack('Npermissions', $this->_string_shift($response, 4));
- // mode == permissions; permissions was the original array key and is retained for bc purposes.
- // mode was added because that's the more industry standard terminology
- $attr+= array('mode' => $attr['permissions']);
- $fileType = $this->_parseMode($attr['permissions']);
- if ($fileType !== false) {
- $attr+= array('type' => $fileType);
- }
- break;
- case NET_SFTP_ATTR_ACCESSTIME: // 0x00000008
- $attr+= unpack('Natime/Nmtime', $this->_string_shift($response, 8));
- break;
- case NET_SFTP_ATTR_EXTENDED: // 0x80000000
- extract(unpack('Ncount', $this->_string_shift($response, 4)));
- for ($i = 0; $i < $count; $i++) {
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $key = $this->_string_shift($response, $length);
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $attr[$key] = $this->_string_shift($response, $length);
- }
- }
- }
- return $attr;
- }
-
- /**
- * Attempt to identify the file type
- *
- * Quoting the SFTP RFC, "Implementations MUST NOT send bits that are not defined" but they seem to anyway
- *
- * @param Integer $mode
- * @return Integer
- * @access private
- */
- function _parseMode($mode)
- {
- // values come from http://lxr.free-electrons.com/source/include/uapi/linux/stat.h#L12
- // see, also, http://linux.die.net/man/2/stat
- switch ($mode & 0170000) {// ie. 1111 0000 0000 0000
- case 0000000: // no file type specified - figure out the file type using alternative means
- return false;
- case 0040000:
- return NET_SFTP_TYPE_DIRECTORY;
- case 0100000:
- return NET_SFTP_TYPE_REGULAR;
- case 0120000:
- return NET_SFTP_TYPE_SYMLINK;
- // new types introduced in SFTPv5+
- // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-05#section-5.2
- case 0010000: // named pipe (fifo)
- return NET_SFTP_TYPE_FIFO;
- case 0020000: // character special
- return NET_SFTP_TYPE_CHAR_DEVICE;
- case 0060000: // block special
- return NET_SFTP_BLOCK_DEVICE;
- case 0140000: // socket
- return NET_SFTP_TYPE_SOCKET;
- case 0160000: // whiteout
- // "SPECIAL should be used for files that are of
- // a known type which cannot be expressed in the protocol"
- return NET_SFTP_TYPE_SPECIAL;
- default:
- return NET_SFTP_TYPE_UNKNOWN;
- }
- }
-
- /**
- * Parse Longname
- *
- * SFTPv3 doesn't provide any easy way of identifying a file type. You could try to open
- * a file as a directory and see if an error is returned or you could try to parse the
- * SFTPv3-specific longname field of the SSH_FXP_NAME packet. That's what this function does.
- * The result is returned using the
- * {@link http://tools.ietf.org/html/draft-ietf-secsh-filexfer-04#section-5.2 SFTPv4 type constants}.
- *
- * If the longname is in an unrecognized format bool(false) is returned.
- *
- * @param String $longname
- * @return Mixed
- * @access private
- */
- function _parseLongname($longname)
- {
- // http://en.wikipedia.org/wiki/Unix_file_types
- // http://en.wikipedia.org/wiki/Filesystem_permissions#Notation_of_traditional_Unix_permissions
- if (preg_match('#^[^/]([r-][w-][xstST-]){3}#', $longname)) {
- switch ($longname[0]) {
- case '-':
- return NET_SFTP_TYPE_REGULAR;
- case 'd':
- return NET_SFTP_TYPE_DIRECTORY;
- case 'l':
- return NET_SFTP_TYPE_SYMLINK;
- default:
- return NET_SFTP_TYPE_SPECIAL;
- }
- }
-
- return false;
- }
-
- /**
- * Sends SFTP Packets
- *
- * See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.
- *
- * @param Integer $type
- * @param String $data
- * @see Net_SFTP::_get_sftp_packet()
- * @see Net_SSH2::_send_channel_packet()
- * @return Boolean
- * @access private
- */
- function _send_sftp_packet($type, $data)
- {
- $packet = $this->request_id !== false ?
- pack('NCNa*', strlen($data) + 5, $type, $this->request_id, $data) :
- pack('NCa*', strlen($data) + 1, $type, $data);
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
- $result = $this->_send_channel_packet(NET_SFTP_CHANNEL, $packet);
- $stop = strtok(microtime(), ' ') + strtok('');
-
- if (defined('NET_SFTP_LOGGING')) {
- $packet_type = '-> ' . $this->packet_types[$type] .
- ' (' . round($stop - $start, 4) . 's)';
- if (NET_SFTP_LOGGING == NET_SFTP_LOG_REALTIME) {
- echo "\r\n" . $this->_format_log(array($data), array($packet_type)) . "\r\n
\r\n";
- flush();
- ob_flush();
- } else {
- $this->packet_type_log[] = $packet_type;
- if (NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX) {
- $this->packet_log[] = $data;
- }
- }
- }
-
- return $result;
- }
-
- /**
- * Receives SFTP Packets
- *
- * See '6. General Packet Format' of draft-ietf-secsh-filexfer-13 for more info.
- *
- * Incidentally, the number of SSH_MSG_CHANNEL_DATA messages has no bearing on the number of SFTP packets present.
- * There can be one SSH_MSG_CHANNEL_DATA messages containing two SFTP packets or there can be two SSH_MSG_CHANNEL_DATA
- * messages containing one SFTP packet.
- *
- * @see Net_SFTP::_send_sftp_packet()
- * @return String
- * @access private
- */
- function _get_sftp_packet()
- {
- $this->curTimeout = false;
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
-
- // SFTP packet length
- while (strlen($this->packet_buffer) < 4) {
- $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL);
- if (is_bool($temp)) {
- $this->packet_type = false;
- $this->packet_buffer = '';
- return false;
- }
- $this->packet_buffer.= $temp;
- }
- extract(unpack('Nlength', $this->_string_shift($this->packet_buffer, 4)));
- $tempLength = $length;
- $tempLength-= strlen($this->packet_buffer);
-
- // SFTP packet type and data payload
- while ($tempLength > 0) {
- $temp = $this->_get_channel_packet(NET_SFTP_CHANNEL);
- if (is_bool($temp)) {
- $this->packet_type = false;
- $this->packet_buffer = '';
- return false;
- }
- $this->packet_buffer.= $temp;
- $tempLength-= strlen($temp);
- }
-
- $stop = strtok(microtime(), ' ') + strtok('');
-
- $this->packet_type = ord($this->_string_shift($this->packet_buffer));
-
- if ($this->request_id !== false) {
- $this->_string_shift($this->packet_buffer, 4); // remove the request id
- $length-= 5; // account for the request id and the packet type
- } else {
- $length-= 1; // account for the packet type
- }
-
- $packet = $this->_string_shift($this->packet_buffer, $length);
-
- if (defined('NET_SFTP_LOGGING')) {
- $packet_type = '<- ' . $this->packet_types[$this->packet_type] .
- ' (' . round($stop - $start, 4) . 's)';
- if (NET_SFTP_LOGGING == NET_SFTP_LOG_REALTIME) {
- echo "\r\n" . $this->_format_log(array($packet), array($packet_type)) . "\r\n
\r\n";
- flush();
- ob_flush();
- } else {
- $this->packet_type_log[] = $packet_type;
- if (NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX) {
- $this->packet_log[] = $packet;
- }
- }
- }
-
- return $packet;
- }
-
- /**
- * Returns a log of the packets that have been sent and received.
- *
- * Returns a string if NET_SFTP_LOGGING == NET_SFTP_LOG_COMPLEX, an array if NET_SFTP_LOGGING == NET_SFTP_LOG_SIMPLE and false if !defined('NET_SFTP_LOGGING')
- *
- * @access public
- * @return String or Array
- */
- function getSFTPLog()
- {
- if (!defined('NET_SFTP_LOGGING')) {
- return false;
- }
-
- switch (NET_SFTP_LOGGING) {
- case NET_SFTP_LOG_COMPLEX:
- return $this->_format_log($this->packet_log, $this->packet_type_log);
- break;
- //case NET_SFTP_LOG_SIMPLE:
- default:
- return $this->packet_type_log;
- }
- }
-
- /**
- * Returns all errors
- *
- * @return String
- * @access public
- */
- function getSFTPErrors()
- {
- return $this->sftp_errors;
- }
-
- /**
- * Returns the last error
- *
- * @return String
- * @access public
- */
- function getLastSFTPError()
- {
- return count($this->sftp_errors) ? $this->sftp_errors[count($this->sftp_errors) - 1] : '';
- }
-
- /**
- * Get supported SFTP versions
- *
- * @return Array
- * @access public
- */
- function getSupportedVersions()
- {
- $temp = array('version' => $this->version);
- if (isset($this->extensions['versions'])) {
- $temp['extensions'] = $this->extensions['versions'];
- }
- return $temp;
- }
-
- /**
- * Disconnect
- *
- * @param Integer $reason
- * @return Boolean
- * @access private
- */
- function _disconnect($reason)
- {
- $this->pwd = false;
- parent::_disconnect($reason);
- }
-}
diff --git a/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SFTP/Stream.php b/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SFTP/Stream.php
deleted file mode 100644
index 48b79b3ef7..0000000000
--- a/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SFTP/Stream.php
+++ /dev/null
@@ -1,775 +0,0 @@
-
- * @copyright MMXIII Jim Wigginton
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
- * @link http://phpseclib.sourceforge.net
- */
-
-/**
- * SFTP Stream Wrapper
- *
- * @package Net_SFTP_Stream
- * @author Jim Wigginton
- * @version 0.3.2
- * @access public
- */
-class Net_SFTP_Stream
-{
- /**
- * SFTP instances
- *
- * Rather than re-create the connection we re-use instances if possible
- *
- * @var Array
- * @access static
- */
- static $instances;
-
- /**
- * SFTP instance
- *
- * @var Object
- * @access private
- */
- var $sftp;
-
- /**
- * Path
- *
- * @var String
- * @access private
- */
- var $path;
-
- /**
- * Mode
- *
- * @var String
- * @access private
- */
- var $mode;
-
- /**
- * Position
- *
- * @var Integer
- * @access private
- */
- var $pos;
-
- /**
- * Size
- *
- * @var Integer
- * @access private
- */
- var $size;
-
- /**
- * Directory entries
- *
- * @var Array
- * @access private
- */
- var $entries;
-
- /**
- * EOF flag
- *
- * @var Boolean
- * @access private
- */
- var $eof;
-
- /**
- * Context resource
- *
- * Technically this needs to be publically accessible so PHP can set it directly
- *
- * @var Resource
- * @access public
- */
- var $context;
-
- /**
- * Notification callback function
- *
- * @var Callable
- * @access public
- */
- var $notification;
-
- /**
- * The Constructor
- *
- * @access public
- */
- function Net_SFTP_Stream()
- {
- if (defined('NET_SFTP_STREAM_LOGGING')) {
- echo "__construct()\r\n";
- }
-
- if (!class_exists('Net_SFTP')) {
- include_once 'Net/SFTP.php';
- }
- }
-
- /**
- * Path Parser
- *
- * Extract a path from a URI and actually connect to an SSH server if appropriate
- *
- * If "notification" is set as a context parameter the message code for successful login is
- * NET_SSH2_MSG_USERAUTH_SUCCESS. For a failed login it's NET_SSH2_MSG_USERAUTH_FAILURE.
- *
- * @param String $path
- * @return String
- * @access private
- */
- function _parse_path($path)
- {
- extract(parse_url($path) + array('port' => 22));
-
- if (!isset($host)) {
- return false;
- }
-
- if (isset($this->context)) {
- $context = stream_context_get_params($this->context);
- if (isset($context['notification'])) {
- $this->notification = $context['notification'];
- }
- }
-
- if ($host[0] == '$') {
- $host = substr($host, 1);
- global $$host;
- if (!is_object($$host) || get_class($$host) != 'Net_SFTP') {
- return false;
- }
- $this->sftp = $$host;
- } else {
- if (isset($this->context)) {
- $context = stream_context_get_options($this->context);
- }
- if (isset($context['sftp']['session'])) {
- $sftp = $context['sftp']['session'];
- }
- if (isset($context['sftp']['sftp'])) {
- $sftp = $context['sftp']['sftp'];
- }
- if (isset($sftp) && is_object($sftp) && get_class($sftp) == 'Net_SFTP') {
- $this->sftp = $sftp;
- return $path;
- }
- if (isset($context['sftp']['username'])) {
- $user = $context['sftp']['username'];
- }
- if (isset($context['sftp']['password'])) {
- $pass = $context['sftp']['password'];
- }
- if (isset($context['sftp']['privkey']) && is_object($context['sftp']['privkey']) && get_Class($context['sftp']['privkey']) == 'Crypt_RSA') {
- $pass = $context['sftp']['privkey'];
- }
-
- if (!isset($user) || !isset($pass)) {
- return false;
- }
-
- // casting $pass to a string is necessary in the event that it's a Crypt_RSA object
- if (isset(self::$instances[$host][$port][$user][(string) $pass])) {
- $this->sftp = self::$instances[$host][$port][$user][(string) $pass];
- } else {
- $this->sftp = new Net_SFTP($host, $port);
- if (isset($this->notification) && is_callable($this->notification)) {
- /* if !is_callable($this->notification) we could do this:
-
- user_error('fopen(): failed to call user notifier', E_USER_WARNING);
-
- the ftp wrapper gives errors like that when the notifier isn't callable.
- i've opted not to do that, however, since the ftp wrapper gives the line
- on which the fopen occurred as the line number - not the line that the
- user_error is on.
- */
- call_user_func($this->notification, STREAM_NOTIFY_CONNECT, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0);
- call_user_func($this->notification, STREAM_NOTIFY_AUTH_REQUIRED, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0);
- if (!$this->sftp->login($user, $pass)) {
- call_user_func($this->notification, STREAM_NOTIFY_AUTH_RESULT, STREAM_NOTIFY_SEVERITY_ERR, 'Login Failure', NET_SSH2_MSG_USERAUTH_FAILURE, 0, 0);
- return false;
- }
- call_user_func($this->notification, STREAM_NOTIFY_AUTH_RESULT, STREAM_NOTIFY_SEVERITY_INFO, 'Login Success', NET_SSH2_MSG_USERAUTH_SUCCESS, 0, 0);
- } else {
- if (!$this->sftp->login($user, $pass)) {
- return false;
- }
- }
- self::$instances[$host][$port][$user][(string) $pass] = $this->sftp;
- }
- }
-
- return $path;
- }
-
- /**
- * Opens file or URL
- *
- * @param String $path
- * @param String $mode
- * @param Integer $options
- * @param String $opened_path
- * @return Boolean
- * @access public
- */
- function _stream_open($path, $mode, $options, &$opened_path)
- {
- $path = $this->_parse_path($path);
-
- if ($path === false) {
- return false;
- }
- $this->path = $path;
-
- $this->size = $this->sftp->size($path);
- $this->mode = preg_replace('#[bt]$#', '', $mode);
- $this->eof = false;
-
- if ($this->size === false) {
- if ($this->mode[0] == 'r') {
- return false;
- }
- } else {
- switch ($this->mode[0]) {
- case 'x':
- return false;
- case 'w':
- case 'c':
- $this->sftp->truncate($path, 0);
- }
- }
-
- $this->pos = $this->mode[0] != 'a' ? 0 : $this->size;
-
- return true;
- }
-
- /**
- * Read from stream
- *
- * @param Integer $count
- * @return Mixed
- * @access public
- */
- function _stream_read($count)
- {
- switch ($this->mode) {
- case 'w':
- case 'a':
- case 'x':
- case 'c':
- return false;
- }
-
- // commented out because some files - eg. /dev/urandom - will say their size is 0 when in fact it's kinda infinite
- //if ($this->pos >= $this->size) {
- // $this->eof = true;
- // return false;
- //}
-
- $result = $this->sftp->get($this->path, false, $this->pos, $count);
- if (isset($this->notification) && is_callable($this->notification)) {
- if ($result === false) {
- call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
- return 0;
- }
- // seems that PHP calls stream_read in 8k chunks
- call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($result), $this->size);
- }
-
- if (empty($result)) { // ie. false or empty string
- $this->eof = true;
- return false;
- }
- $this->pos+= strlen($result);
-
- return $result;
- }
-
- /**
- * Write to stream
- *
- * @param String $data
- * @return Mixed
- * @access public
- */
- function _stream_write($data)
- {
- switch ($this->mode) {
- case 'r':
- return false;
- }
-
- $result = $this->sftp->put($this->path, $data, NET_SFTP_STRING, $this->pos);
- if (isset($this->notification) && is_callable($this->notification)) {
- if (!$result) {
- call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
- return 0;
- }
- // seems that PHP splits up strings into 8k blocks before calling stream_write
- call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($data), strlen($data));
- }
-
- if ($result === false) {
- return false;
- }
- $this->pos+= strlen($data);
- if ($this->pos > $this->size) {
- $this->size = $this->pos;
- }
- $this->eof = false;
- return strlen($data);
- }
-
- /**
- * Retrieve the current position of a stream
- *
- * @return Integer
- * @access public
- */
- function _stream_tell()
- {
- return $this->pos;
- }
-
- /**
- * Tests for end-of-file on a file pointer
- *
- * In my testing there are four classes functions that normally effect the pointer:
- * fseek, fputs / fwrite, fgets / fread and ftruncate.
- *
- * Only fgets / fread, however, results in feof() returning true. do fputs($fp, 'aaa') on a blank file and feof()
- * will return false. do fread($fp, 1) and feof() will then return true. do fseek($fp, 10) on ablank file and feof()
- * will return false. do fread($fp, 1) and feof() will then return true.
- *
- * @return Boolean
- * @access public
- */
- function _stream_eof()
- {
- return $this->eof;
- }
-
- /**
- * Seeks to specific location in a stream
- *
- * @param Integer $offset
- * @param Integer $whence
- * @return Boolean
- * @access public
- */
- function _stream_seek($offset, $whence)
- {
- switch ($whence) {
- case SEEK_SET:
- if ($offset >= $this->size || $offset < 0) {
- return false;
- }
- break;
- case SEEK_CUR:
- $offset+= $this->pos;
- break;
- case SEEK_END:
- $offset+= $this->size;
- }
-
- $this->pos = $offset;
- $this->eof = false;
- return true;
- }
-
- /**
- * Change stream options
- *
- * @param String $path
- * @param Integer $option
- * @param Mixed $var
- * @return Boolean
- * @access public
- */
- function _stream_metadata($path, $option, $var)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
-
- // stream_metadata was introduced in PHP 5.4.0 but as of 5.4.11 the constants haven't been defined
- // see http://www.php.net/streamwrapper.stream-metadata and https://bugs.php.net/64246
- // and https://github.com/php/php-src/blob/master/main/php_streams.h#L592
- switch ($option) {
- case 1: // PHP_STREAM_META_TOUCH
- return $this->sftp->touch($path, $var[0], $var[1]);
- case 2: // PHP_STREAM_OWNER_NAME
- case 3: // PHP_STREAM_GROUP_NAME
- return false;
- case 4: // PHP_STREAM_META_OWNER
- return $this->sftp->chown($path, $var);
- case 5: // PHP_STREAM_META_GROUP
- return $this->sftp->chgrp($path, $var);
- case 6: // PHP_STREAM_META_ACCESS
- return $this->sftp->chmod($path, $var) !== false;
- }
- }
-
- /**
- * Retrieve the underlaying resource
- *
- * @param Integer $cast_as
- * @return Resource
- * @access public
- */
- function _stream_cast($cast_as)
- {
- return $this->sftp->fsock;
- }
-
- /**
- * Advisory file locking
- *
- * @param Integer $operation
- * @return Boolean
- * @access public
- */
- function _stream_lock($operation)
- {
- return false;
- }
-
- /**
- * Renames a file or directory
- *
- * Attempts to rename oldname to newname, moving it between directories if necessary.
- * If newname exists, it will be overwritten. This is a departure from what Net_SFTP
- * does.
- *
- * @param String $path_from
- * @param String $path_to
- * @return Boolean
- * @access public
- */
- function _rename($path_from, $path_to)
- {
- $path1 = parse_url($path_from);
- $path2 = parse_url($path_to);
- unset($path1['path'], $path2['path']);
- if ($path1 != $path2) {
- return false;
- }
-
- $path_from = $this->_parse_path($path_from);
- $path_to = parse_url($path_to);
- if ($path_from == false) {
- return false;
- }
-
- $path_to = $path_to['path']; // the $component part of parse_url() was added in PHP 5.1.2
- // "It is an error if there already exists a file with the name specified by newpath."
- // -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-6.5
- if (!$this->sftp->rename($path_from, $path_to)) {
- if ($this->sftp->stat($path_to)) {
- return $this->sftp->delete($path_to, true) && $this->sftp->rename($path_from, $path_to);
- }
- return false;
- }
-
- return true;
- }
-
- /**
- * Open directory handle
- *
- * The only $options is "whether or not to enforce safe_mode (0x04)". Since safe mode was deprecated in 5.3 and
- * removed in 5.4 I'm just going to ignore it
- *
- * @param String $path
- * @param Integer $options
- * @return Boolean
- * @access public
- */
- function _dir_opendir($path, $options)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
- $this->pos = 0;
- $this->entries = $this->sftp->nlist($path);
- return $this->entries !== false;
- }
-
- /**
- * Read entry from directory handle
- *
- * @return Mixed
- * @access public
- */
- function _dir_readdir()
- {
- if (isset($this->entries[$this->pos])) {
- return $this->entries[$this->pos++];
- }
- return false;
- }
-
- /**
- * Rewind directory handle
- *
- * @return Boolean
- * @access public
- */
- function _dir_rewinddir()
- {
- $this->pos = 0;
- return true;
- }
-
- /**
- * Close directory handle
- *
- * @return Boolean
- * @access public
- */
- function _dir_closedir()
- {
- return true;
- }
-
- /**
- * Create a directory
- *
- * Only valid $options is STREAM_MKDIR_RECURSIVE
- *
- * @param String $path
- * @param Integer $mode
- * @param Integer $options
- * @return Boolean
- * @access public
- */
- function _mkdir($path, $mode, $options)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
-
- return $this->sftp->mkdir($path, $mode, $options & STREAM_MKDIR_RECURSIVE);
- }
-
- /**
- * Removes a directory
- *
- * Only valid $options is STREAM_MKDIR_RECURSIVE per , however,
- * does not have a $recursive parameter as mkdir() does so I don't know how
- * STREAM_MKDIR_RECURSIVE is supposed to be set. Also, when I try it out with rmdir() I get 8 as
- * $options. What does 8 correspond to?
- *
- * @param String $path
- * @param Integer $mode
- * @param Integer $options
- * @return Boolean
- * @access public
- */
- function _rmdir($path, $options)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
-
- return $this->sftp->rmdir($path);
- }
-
- /**
- * Flushes the output
- *
- * See . Always returns true because Net_SFTP doesn't cache stuff before writing
- *
- * @return Boolean
- * @access public
- */
- function _stream_flush()
- {
- return true;
- }
-
- /**
- * Retrieve information about a file resource
- *
- * @return Mixed
- * @access public
- */
- function _stream_stat()
- {
- $results = $this->sftp->stat($this->path);
- if ($results === false) {
- return false;
- }
- return $results;
- }
-
- /**
- * Delete a file
- *
- * @param String $path
- * @return Boolean
- * @access public
- */
- function _unlink($path)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
-
- return $this->sftp->delete($path, false);
- }
-
- /**
- * Retrieve information about a file
- *
- * Ignores the STREAM_URL_STAT_QUIET flag because the entirety of Net_SFTP_Stream is quiet by default
- * might be worthwhile to reconstruct bits 12-16 (ie. the file type) if mode doesn't have them but we'll
- * cross that bridge when and if it's reached
- *
- * @param String $path
- * @param Integer $flags
- * @return Mixed
- * @access public
- */
- function _url_stat($path, $flags)
- {
- $path = $this->_parse_path($path);
- if ($path === false) {
- return false;
- }
-
- $results = $flags & STREAM_URL_STAT_LINK ? $this->sftp->lstat($path) : $this->sftp->stat($path);
- if ($results === false) {
- return false;
- }
-
- return $results;
- }
-
- /**
- * Truncate stream
- *
- * @param Integer $new_size
- * @return Boolean
- * @access public
- */
- function _stream_truncate($new_size)
- {
- if (!$this->sftp->truncate($this->path, $new_size)) {
- return false;
- }
-
- $this->eof = false;
- $this->size = $new_size;
-
- return true;
- }
-
- /**
- * Change stream options
- *
- * STREAM_OPTION_WRITE_BUFFER isn't supported for the same reason stream_flush isn't.
- * The other two aren't supported because of limitations in Net_SFTP.
- *
- * @param Integer $option
- * @param Integer $arg1
- * @param Integer $arg2
- * @return Boolean
- * @access public
- */
- function _stream_set_option($option, $arg1, $arg2)
- {
- return false;
- }
-
- /**
- * Close an resource
- *
- * @access public
- */
- function _stream_close()
- {
- }
-
- /**
- * __call Magic Method
- *
- * When you're utilizing an SFTP stream you're not calling the methods in this class directly - PHP is calling them for you.
- * Which kinda begs the question... what methods is PHP calling and what parameters is it passing to them? This function
- * lets you figure that out.
- *
- * If NET_SFTP_STREAM_LOGGING is defined all calls will be output on the screen and then (regardless of whether or not
- * NET_SFTP_STREAM_LOGGING is enabled) the parameters will be passed through to the appropriate method.
- *
- * @param String
- * @param Array
- * @return Mixed
- * @access public
- */
- function __call($name, $arguments)
- {
- if (defined('NET_SFTP_STREAM_LOGGING')) {
- echo $name . '(';
- $last = count($arguments) - 1;
- foreach ($arguments as $i => $argument) {
- var_export($argument);
- if ($i != $last) {
- echo ',';
- }
- }
- echo ")\r\n";
- }
- $name = '_' . $name;
- if (!method_exists($this, $name)) {
- return false;
- }
- return call_user_func_array(array($this, $name), $arguments);
- }
-}
-
-if (function_exists('stream_wrapper_register')) {
- stream_wrapper_register('sftp', 'Net_SFTP_Stream');
-}
diff --git a/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SSH1.php b/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SSH1.php
deleted file mode 100644
index 0a6aa90828..0000000000
--- a/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SSH1.php
+++ /dev/null
@@ -1,1582 +0,0 @@
-
- * login('username', 'password')) {
- * exit('Login Failed');
- * }
- *
- * echo $ssh->exec('ls -la');
- * ?>
- *
- *
- * Here's another short example:
- *
- * login('username', 'password')) {
- * exit('Login Failed');
- * }
- *
- * echo $ssh->read('username@username:~$');
- * $ssh->write("ls -la\n");
- * echo $ssh->read('username@username:~$');
- * ?>
- *
- *
- * More information on the SSHv1 specification can be found by reading
- * {@link http://www.snailbook.com/docs/protocol-1.5.txt protocol-1.5.txt}.
- *
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @category Net
- * @package Net_SSH1
- * @author Jim Wigginton
- * @copyright MMVII Jim Wigginton
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
- * @link http://phpseclib.sourceforge.net
- */
-
-/**#@+
- * Encryption Methods
- *
- * @see Net_SSH1::getSupportedCiphers()
- * @access public
- */
-/**
- * No encryption
- *
- * Not supported.
- */
-define('NET_SSH1_CIPHER_NONE', 0);
-/**
- * IDEA in CFB mode
- *
- * Not supported.
- */
-define('NET_SSH1_CIPHER_IDEA', 1);
-/**
- * DES in CBC mode
- */
-define('NET_SSH1_CIPHER_DES', 2);
-/**
- * Triple-DES in CBC mode
- *
- * All implementations are required to support this
- */
-define('NET_SSH1_CIPHER_3DES', 3);
-/**
- * TRI's Simple Stream encryption CBC
- *
- * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, does define it (see cipher.h),
- * although it doesn't use it (see cipher.c)
- */
-define('NET_SSH1_CIPHER_BROKEN_TSS', 4);
-/**
- * RC4
- *
- * Not supported.
- *
- * @internal According to the SSH1 specs:
- *
- * "The first 16 bytes of the session key are used as the key for
- * the server to client direction. The remaining 16 bytes are used
- * as the key for the client to server direction. This gives
- * independent 128-bit keys for each direction."
- *
- * This library currently only supports encryption when the same key is being used for both directions. This is
- * because there's only one $crypto object. Two could be added ($encrypt and $decrypt, perhaps).
- */
-define('NET_SSH1_CIPHER_RC4', 5);
-/**
- * Blowfish
- *
- * Not supported nor is it defined in the official SSH1 specs. OpenSSH, however, defines it (see cipher.h) and
- * uses it (see cipher.c)
- */
-define('NET_SSH1_CIPHER_BLOWFISH', 6);
-/**#@-*/
-
-/**#@+
- * Authentication Methods
- *
- * @see Net_SSH1::getSupportedAuthentications()
- * @access public
- */
-/**
- * .rhosts or /etc/hosts.equiv
- */
-define('NET_SSH1_AUTH_RHOSTS', 1);
-/**
- * pure RSA authentication
- */
-define('NET_SSH1_AUTH_RSA', 2);
-/**
- * password authentication
- *
- * This is the only method that is supported by this library.
- */
-define('NET_SSH1_AUTH_PASSWORD', 3);
-/**
- * .rhosts with RSA host authentication
- */
-define('NET_SSH1_AUTH_RHOSTS_RSA', 4);
-/**#@-*/
-
-/**#@+
- * Terminal Modes
- *
- * @link http://3sp.com/content/developer/maverick-net/docs/Maverick.SSH.PseudoTerminalModesMembers.html
- * @access private
- */
-define('NET_SSH1_TTY_OP_END', 0);
-/**#@-*/
-
-/**
- * The Response Type
- *
- * @see Net_SSH1::_get_binary_packet()
- * @access private
- */
-define('NET_SSH1_RESPONSE_TYPE', 1);
-
-/**
- * The Response Data
- *
- * @see Net_SSH1::_get_binary_packet()
- * @access private
- */
-define('NET_SSH1_RESPONSE_DATA', 2);
-
-/**#@+
- * Execution Bitmap Masks
- *
- * @see Net_SSH1::bitmap
- * @access private
- */
-define('NET_SSH1_MASK_CONSTRUCTOR', 0x00000001);
-define('NET_SSH1_MASK_LOGIN', 0x00000002);
-define('NET_SSH1_MASK_SHELL', 0x00000004);
-/**#@-*/
-
-/**#@+
- * @access public
- * @see Net_SSH1::getLog()
- */
-/**
- * Returns the message numbers
- */
-define('NET_SSH1_LOG_SIMPLE', 1);
-/**
- * Returns the message content
- */
-define('NET_SSH1_LOG_COMPLEX', 2);
-/**
- * Outputs the content real-time
- */
-define('NET_SSH1_LOG_REALTIME', 3);
-/**
- * Dumps the content real-time to a file
- */
-define('NET_SSH1_LOG_REALTIME_FILE', 4);
-/**#@-*/
-
-/**#@+
- * @access public
- * @see Net_SSH1::read()
- */
-/**
- * Returns when a string matching $expect exactly is found
- */
-define('NET_SSH1_READ_SIMPLE', 1);
-/**
- * Returns when a string matching the regular expression $expect is found
- */
-define('NET_SSH1_READ_REGEX', 2);
-/**#@-*/
-
-/**
- * Pure-PHP implementation of SSHv1.
- *
- * @package Net_SSH1
- * @author Jim Wigginton
- * @version 0.1.0
- * @access public
- */
-class Net_SSH1
-{
- /**
- * The SSH identifier
- *
- * @var String
- * @access private
- */
- var $identifier = 'SSH-1.5-phpseclib';
-
- /**
- * The Socket Object
- *
- * @var Object
- * @access private
- */
- var $fsock;
-
- /**
- * The cryptography object
- *
- * @var Object
- * @access private
- */
- var $crypto = false;
-
- /**
- * Execution Bitmap
- *
- * The bits that are set represent functions that have been called already. This is used to determine
- * if a requisite function has been successfully executed. If not, an error should be thrown.
- *
- * @var Integer
- * @access private
- */
- var $bitmap = 0;
-
- /**
- * The Server Key Public Exponent
- *
- * Logged for debug purposes
- *
- * @see Net_SSH1::getServerKeyPublicExponent()
- * @var String
- * @access private
- */
- var $server_key_public_exponent;
-
- /**
- * The Server Key Public Modulus
- *
- * Logged for debug purposes
- *
- * @see Net_SSH1::getServerKeyPublicModulus()
- * @var String
- * @access private
- */
- var $server_key_public_modulus;
-
- /**
- * The Host Key Public Exponent
- *
- * Logged for debug purposes
- *
- * @see Net_SSH1::getHostKeyPublicExponent()
- * @var String
- * @access private
- */
- var $host_key_public_exponent;
-
- /**
- * The Host Key Public Modulus
- *
- * Logged for debug purposes
- *
- * @see Net_SSH1::getHostKeyPublicModulus()
- * @var String
- * @access private
- */
- var $host_key_public_modulus;
-
- /**
- * Supported Ciphers
- *
- * Logged for debug purposes
- *
- * @see Net_SSH1::getSupportedCiphers()
- * @var Array
- * @access private
- */
- var $supported_ciphers = array(
- NET_SSH1_CIPHER_NONE => 'No encryption',
- NET_SSH1_CIPHER_IDEA => 'IDEA in CFB mode',
- NET_SSH1_CIPHER_DES => 'DES in CBC mode',
- NET_SSH1_CIPHER_3DES => 'Triple-DES in CBC mode',
- NET_SSH1_CIPHER_BROKEN_TSS => 'TRI\'s Simple Stream encryption CBC',
- NET_SSH1_CIPHER_RC4 => 'RC4',
- NET_SSH1_CIPHER_BLOWFISH => 'Blowfish'
- );
-
- /**
- * Supported Authentications
- *
- * Logged for debug purposes
- *
- * @see Net_SSH1::getSupportedAuthentications()
- * @var Array
- * @access private
- */
- var $supported_authentications = array(
- NET_SSH1_AUTH_RHOSTS => '.rhosts or /etc/hosts.equiv',
- NET_SSH1_AUTH_RSA => 'pure RSA authentication',
- NET_SSH1_AUTH_PASSWORD => 'password authentication',
- NET_SSH1_AUTH_RHOSTS_RSA => '.rhosts with RSA host authentication'
- );
-
- /**
- * Server Identification
- *
- * @see Net_SSH1::getServerIdentification()
- * @var String
- * @access private
- */
- var $server_identification = '';
-
- /**
- * Protocol Flags
- *
- * @see Net_SSH1::Net_SSH1()
- * @var Array
- * @access private
- */
- var $protocol_flags = array();
-
- /**
- * Protocol Flag Log
- *
- * @see Net_SSH1::getLog()
- * @var Array
- * @access private
- */
- var $protocol_flag_log = array();
-
- /**
- * Message Log
- *
- * @see Net_SSH1::getLog()
- * @var Array
- * @access private
- */
- var $message_log = array();
-
- /**
- * Real-time log file pointer
- *
- * @see Net_SSH1::_append_log()
- * @var Resource
- * @access private
- */
- var $realtime_log_file;
-
- /**
- * Real-time log file size
- *
- * @see Net_SSH1::_append_log()
- * @var Integer
- * @access private
- */
- var $realtime_log_size;
-
- /**
- * Real-time log file wrap boolean
- *
- * @see Net_SSH1::_append_log()
- * @var Boolean
- * @access private
- */
- var $realtime_log_wrap;
-
- /**
- * Interactive Buffer
- *
- * @see Net_SSH1::read()
- * @var Array
- * @access private
- */
- var $interactiveBuffer = '';
-
- /**
- * Timeout
- *
- * @see Net_SSH1::setTimeout()
- * @access private
- */
- var $timeout;
-
- /**
- * Current Timeout
- *
- * @see Net_SSH1::_get_channel_packet()
- * @access private
- */
- var $curTimeout;
-
- /**
- * Log Boundary
- *
- * @see Net_SSH1::_format_log
- * @access private
- */
- var $log_boundary = ':';
-
- /**
- * Log Long Width
- *
- * @see Net_SSH1::_format_log
- * @access private
- */
- var $log_long_width = 65;
-
- /**
- * Log Short Width
- *
- * @see Net_SSH1::_format_log
- * @access private
- */
- var $log_short_width = 16;
-
- /**
- * Default Constructor.
- *
- * Connects to an SSHv1 server
- *
- * @param String $host
- * @param optional Integer $port
- * @param optional Integer $timeout
- * @param optional Integer $cipher
- * @return Net_SSH1
- * @access public
- */
- function Net_SSH1($host, $port = 22, $timeout = 10, $cipher = NET_SSH1_CIPHER_3DES)
- {
- if (!class_exists('Math_BigInteger')) {
- include_once 'Math/BigInteger.php';
- }
-
- // Include Crypt_Random
- // the class_exists() will only be called if the crypt_random_string function hasn't been defined and
- // will trigger a call to __autoload() if you're wanting to auto-load classes
- // call function_exists() a second time to stop the include_once from being called outside
- // of the auto loader
- if (!function_exists('crypt_random_string') && !class_exists('Crypt_Random') && !function_exists('crypt_random_string')) {
- include_once 'Crypt/Random.php';
- }
-
- $this->protocol_flags = array(
- 1 => 'NET_SSH1_MSG_DISCONNECT',
- 2 => 'NET_SSH1_SMSG_PUBLIC_KEY',
- 3 => 'NET_SSH1_CMSG_SESSION_KEY',
- 4 => 'NET_SSH1_CMSG_USER',
- 9 => 'NET_SSH1_CMSG_AUTH_PASSWORD',
- 10 => 'NET_SSH1_CMSG_REQUEST_PTY',
- 12 => 'NET_SSH1_CMSG_EXEC_SHELL',
- 13 => 'NET_SSH1_CMSG_EXEC_CMD',
- 14 => 'NET_SSH1_SMSG_SUCCESS',
- 15 => 'NET_SSH1_SMSG_FAILURE',
- 16 => 'NET_SSH1_CMSG_STDIN_DATA',
- 17 => 'NET_SSH1_SMSG_STDOUT_DATA',
- 18 => 'NET_SSH1_SMSG_STDERR_DATA',
- 19 => 'NET_SSH1_CMSG_EOF',
- 20 => 'NET_SSH1_SMSG_EXITSTATUS',
- 33 => 'NET_SSH1_CMSG_EXIT_CONFIRMATION'
- );
-
- $this->_define_array($this->protocol_flags);
-
- $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
- if (!$this->fsock) {
- user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
- return;
- }
-
- $this->server_identification = $init_line = fgets($this->fsock, 255);
-
- if (defined('NET_SSH1_LOGGING')) {
- $this->_append_log('<-', $this->server_identification);
- $this->_append_log('->', $this->identifier . "\r\n");
- }
-
- if (!preg_match('#SSH-([0-9\.]+)-(.+)#', $init_line, $parts)) {
- user_error('Can only connect to SSH servers');
- return;
- }
- if ($parts[1][0] != 1) {
- user_error("Cannot connect to SSH $parts[1] servers");
- return;
- }
-
- fputs($this->fsock, $this->identifier."\r\n");
-
- $response = $this->_get_binary_packet();
- if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_PUBLIC_KEY) {
- user_error('Expected SSH_SMSG_PUBLIC_KEY');
- return;
- }
-
- $anti_spoofing_cookie = $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 8);
-
- $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
-
- $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
- $server_key_public_exponent = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
- $this->server_key_public_exponent = $server_key_public_exponent;
-
- $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
- $server_key_public_modulus = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
- $this->server_key_public_modulus = $server_key_public_modulus;
-
- $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
-
- $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
- $host_key_public_exponent = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
- $this->host_key_public_exponent = $host_key_public_exponent;
-
- $temp = unpack('nlen', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 2));
- $host_key_public_modulus = new Math_BigInteger($this->_string_shift($response[NET_SSH1_RESPONSE_DATA], ceil($temp['len'] / 8)), 256);
- $this->host_key_public_modulus = $host_key_public_modulus;
-
- $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4);
-
- // get a list of the supported ciphers
- extract(unpack('Nsupported_ciphers_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
- foreach ($this->supported_ciphers as $mask=>$name) {
- if (($supported_ciphers_mask & (1 << $mask)) == 0) {
- unset($this->supported_ciphers[$mask]);
- }
- }
-
- // get a list of the supported authentications
- extract(unpack('Nsupported_authentications_mask', $this->_string_shift($response[NET_SSH1_RESPONSE_DATA], 4)));
- foreach ($this->supported_authentications as $mask=>$name) {
- if (($supported_authentications_mask & (1 << $mask)) == 0) {
- unset($this->supported_authentications[$mask]);
- }
- }
-
- $session_id = pack('H*', md5($host_key_public_modulus->toBytes() . $server_key_public_modulus->toBytes() . $anti_spoofing_cookie));
-
- $session_key = crypt_random_string(32);
- $double_encrypted_session_key = $session_key ^ str_pad($session_id, 32, chr(0));
-
- if ($server_key_public_modulus->compare($host_key_public_modulus) < 0) {
- $double_encrypted_session_key = $this->_rsa_crypt(
- $double_encrypted_session_key,
- array(
- $server_key_public_exponent,
- $server_key_public_modulus
- )
- );
- $double_encrypted_session_key = $this->_rsa_crypt(
- $double_encrypted_session_key,
- array(
- $host_key_public_exponent,
- $host_key_public_modulus
- )
- );
- } else {
- $double_encrypted_session_key = $this->_rsa_crypt(
- $double_encrypted_session_key,
- array(
- $host_key_public_exponent,
- $host_key_public_modulus
- )
- );
- $double_encrypted_session_key = $this->_rsa_crypt(
- $double_encrypted_session_key,
- array(
- $server_key_public_exponent,
- $server_key_public_modulus
- )
- );
- }
-
- $cipher = isset($this->supported_ciphers[$cipher]) ? $cipher : NET_SSH1_CIPHER_3DES;
- $data = pack('C2a*na*N', NET_SSH1_CMSG_SESSION_KEY, $cipher, $anti_spoofing_cookie, 8 * strlen($double_encrypted_session_key), $double_encrypted_session_key, 0);
-
- if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_SESSION_KEY');
- return;
- }
-
- switch ($cipher) {
- //case NET_SSH1_CIPHER_NONE:
- // $this->crypto = new Crypt_Null();
- // break;
- case NET_SSH1_CIPHER_DES:
- if (!class_exists('Crypt_DES')) {
- include_once 'Crypt/DES.php';
- }
- $this->crypto = new Crypt_DES();
- $this->crypto->disablePadding();
- $this->crypto->enableContinuousBuffer();
- $this->crypto->setKey(substr($session_key, 0, 8));
- break;
- case NET_SSH1_CIPHER_3DES:
- if (!class_exists('Crypt_TripleDES')) {
- include_once 'Crypt/TripleDES.php';
- }
- $this->crypto = new Crypt_TripleDES(CRYPT_DES_MODE_3CBC);
- $this->crypto->disablePadding();
- $this->crypto->enableContinuousBuffer();
- $this->crypto->setKey(substr($session_key, 0, 24));
- break;
- //case NET_SSH1_CIPHER_RC4:
- // if (!class_exists('Crypt_RC4')) {
- // include_once('Crypt/RC4.php');
- // }
- // $this->crypto = new Crypt_RC4();
- // $this->crypto->enableContinuousBuffer();
- // $this->crypto->setKey(substr($session_key, 0, 16));
- // break;
- }
-
- $response = $this->_get_binary_packet();
-
- if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
- user_error('Expected SSH_SMSG_SUCCESS');
- return;
- }
-
- $this->bitmap = NET_SSH1_MASK_CONSTRUCTOR;
- }
-
- /**
- * Login
- *
- * @param String $username
- * @param optional String $password
- * @return Boolean
- * @access public
- */
- function login($username, $password = '')
- {
- if (!($this->bitmap & NET_SSH1_MASK_CONSTRUCTOR)) {
- return false;
- }
-
- $data = pack('CNa*', NET_SSH1_CMSG_USER, strlen($username), $username);
-
- if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_USER');
- return false;
- }
-
- $response = $this->_get_binary_packet();
-
- if ($response === true) {
- return false;
- }
- if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) {
- $this->bitmap |= NET_SSH1_MASK_LOGIN;
- return true;
- } else if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_FAILURE) {
- user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE');
- return false;
- }
-
- $data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen($password), $password);
-
- if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_AUTH_PASSWORD');
- return false;
- }
-
- // remove the username and password from the last logged packet
- if (defined('NET_SSH1_LOGGING') && NET_SSH1_LOGGING == NET_SSH1_LOG_COMPLEX) {
- $data = pack('CNa*', NET_SSH1_CMSG_AUTH_PASSWORD, strlen('password'), 'password');
- $this->message_log[count($this->message_log) - 1] = $data;
- }
-
- $response = $this->_get_binary_packet();
-
- if ($response === true) {
- return false;
- }
- if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_SUCCESS) {
- $this->bitmap |= NET_SSH1_MASK_LOGIN;
- return true;
- } else if ($response[NET_SSH1_RESPONSE_TYPE] == NET_SSH1_SMSG_FAILURE) {
- return false;
- } else {
- user_error('Expected SSH_SMSG_SUCCESS or SSH_SMSG_FAILURE');
- return false;
- }
- }
-
- /**
- * Set Timeout
- *
- * $ssh->exec('ping 127.0.0.1'); on a Linux host will never return and will run indefinitely. setTimeout() makes it so it'll timeout.
- * Setting $timeout to false or 0 will mean there is no timeout.
- *
- * @param Mixed $timeout
- */
- function setTimeout($timeout)
- {
- $this->timeout = $this->curTimeout = $timeout;
- }
-
- /**
- * Executes a command on a non-interactive shell, returns the output, and quits.
- *
- * An SSH1 server will close the connection after a command has been executed on a non-interactive shell. SSH2
- * servers don't, however, this isn't an SSH2 client. The way this works, on the server, is by initiating a
- * shell with the -s option, as discussed in the following links:
- *
- * {@link http://www.faqs.org/docs/bashman/bashref_65.html http://www.faqs.org/docs/bashman/bashref_65.html}
- * {@link http://www.faqs.org/docs/bashman/bashref_62.html http://www.faqs.org/docs/bashman/bashref_62.html}
- *
- * To execute further commands, a new Net_SSH1 object will need to be created.
- *
- * Returns false on failure and the output, otherwise.
- *
- * @see Net_SSH1::interactiveRead()
- * @see Net_SSH1::interactiveWrite()
- * @param String $cmd
- * @return mixed
- * @access public
- */
- function exec($cmd, $block = true)
- {
- if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()');
- return false;
- }
-
- $data = pack('CNa*', NET_SSH1_CMSG_EXEC_CMD, strlen($cmd), $cmd);
-
- if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_EXEC_CMD');
- return false;
- }
-
- if (!$block) {
- return true;
- }
-
- $output = '';
- $response = $this->_get_binary_packet();
-
- if ($response !== false) {
- do {
- $output.= substr($response[NET_SSH1_RESPONSE_DATA], 4);
- $response = $this->_get_binary_packet();
- } while (is_array($response) && $response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_EXITSTATUS);
- }
-
- $data = pack('C', NET_SSH1_CMSG_EXIT_CONFIRMATION);
-
- // i don't think it's really all that important if this packet gets sent or not.
- $this->_send_binary_packet($data);
-
- fclose($this->fsock);
-
- // reset the execution bitmap - a new Net_SSH1 object needs to be created.
- $this->bitmap = 0;
-
- return $output;
- }
-
- /**
- * Creates an interactive shell
- *
- * @see Net_SSH1::interactiveRead()
- * @see Net_SSH1::interactiveWrite()
- * @return Boolean
- * @access private
- */
- function _initShell()
- {
- // connect using the sample parameters in protocol-1.5.txt.
- // according to wikipedia.org's entry on text terminals, "the fundamental type of application running on a text
- // terminal is a command line interpreter or shell". thus, opening a terminal session to run the shell.
- $data = pack('CNa*N4C', NET_SSH1_CMSG_REQUEST_PTY, strlen('vt100'), 'vt100', 24, 80, 0, 0, NET_SSH1_TTY_OP_END);
-
- if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_REQUEST_PTY');
- return false;
- }
-
- $response = $this->_get_binary_packet();
-
- if ($response === true) {
- return false;
- }
- if ($response[NET_SSH1_RESPONSE_TYPE] != NET_SSH1_SMSG_SUCCESS) {
- user_error('Expected SSH_SMSG_SUCCESS');
- return false;
- }
-
- $data = pack('C', NET_SSH1_CMSG_EXEC_SHELL);
-
- if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_EXEC_SHELL');
- return false;
- }
-
- $this->bitmap |= NET_SSH1_MASK_SHELL;
-
- //stream_set_blocking($this->fsock, 0);
-
- return true;
- }
-
- /**
- * Inputs a command into an interactive shell.
- *
- * @see Net_SSH1::interactiveWrite()
- * @param String $cmd
- * @return Boolean
- * @access public
- */
- function write($cmd)
- {
- return $this->interactiveWrite($cmd);
- }
-
- /**
- * Returns the output of an interactive shell when there's a match for $expect
- *
- * $expect can take the form of a string literal or, if $mode == NET_SSH1_READ_REGEX,
- * a regular expression.
- *
- * @see Net_SSH1::write()
- * @param String $expect
- * @param Integer $mode
- * @return Boolean
- * @access public
- */
- function read($expect, $mode = NET_SSH1_READ_SIMPLE)
- {
- if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()');
- return false;
- }
-
- if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session');
- return false;
- }
-
- $match = $expect;
- while (true) {
- if ($mode == NET_SSH1_READ_REGEX) {
- preg_match($expect, $this->interactiveBuffer, $matches);
- $match = isset($matches[0]) ? $matches[0] : '';
- }
- $pos = strlen($match) ? strpos($this->interactiveBuffer, $match) : false;
- if ($pos !== false) {
- return $this->_string_shift($this->interactiveBuffer, $pos + strlen($match));
- }
- $response = $this->_get_binary_packet();
-
- if ($response === true) {
- return $this->_string_shift($this->interactiveBuffer, strlen($this->interactiveBuffer));
- }
- $this->interactiveBuffer.= substr($response[NET_SSH1_RESPONSE_DATA], 4);
- }
- }
-
- /**
- * Inputs a command into an interactive shell.
- *
- * @see Net_SSH1::interactiveRead()
- * @param String $cmd
- * @return Boolean
- * @access public
- */
- function interactiveWrite($cmd)
- {
- if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()');
- return false;
- }
-
- if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session');
- return false;
- }
-
- $data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($cmd), $cmd);
-
- if (!$this->_send_binary_packet($data)) {
- user_error('Error sending SSH_CMSG_STDIN');
- return false;
- }
-
- return true;
- }
-
- /**
- * Returns the output of an interactive shell when no more output is available.
- *
- * Requires PHP 4.3.0 or later due to the use of the stream_select() function. If you see stuff like
- * "^[[00m", you're seeing ANSI escape codes. According to
- * {@link http://support.microsoft.com/kb/101875 How to Enable ANSI.SYS in a Command Window}, "Windows NT
- * does not support ANSI escape sequences in Win32 Console applications", so if you're a Windows user,
- * there's not going to be much recourse.
- *
- * @see Net_SSH1::interactiveRead()
- * @return String
- * @access public
- */
- function interactiveRead()
- {
- if (!($this->bitmap & NET_SSH1_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()');
- return false;
- }
-
- if (!($this->bitmap & NET_SSH1_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session');
- return false;
- }
-
- $read = array($this->fsock);
- $write = $except = null;
- if (stream_select($read, $write, $except, 0)) {
- $response = $this->_get_binary_packet();
- return substr($response[NET_SSH1_RESPONSE_DATA], 4);
- } else {
- return '';
- }
- }
-
- /**
- * Disconnect
- *
- * @access public
- */
- function disconnect()
- {
- $this->_disconnect();
- }
-
- /**
- * Destructor.
- *
- * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call
- * disconnect().
- *
- * @access public
- */
- function __destruct()
- {
- $this->_disconnect();
- }
-
- /**
- * Disconnect
- *
- * @param String $msg
- * @access private
- */
- function _disconnect($msg = 'Client Quit')
- {
- if ($this->bitmap) {
- $data = pack('C', NET_SSH1_CMSG_EOF);
- $this->_send_binary_packet($data);
- /*
- $response = $this->_get_binary_packet();
- if ($response === true) {
- $response = array(NET_SSH1_RESPONSE_TYPE => -1);
- }
- switch ($response[NET_SSH1_RESPONSE_TYPE]) {
- case NET_SSH1_SMSG_EXITSTATUS:
- $data = pack('C', NET_SSH1_CMSG_EXIT_CONFIRMATION);
- break;
- default:
- $data = pack('CNa*', NET_SSH1_MSG_DISCONNECT, strlen($msg), $msg);
- }
- */
- $data = pack('CNa*', NET_SSH1_MSG_DISCONNECT, strlen($msg), $msg);
-
- $this->_send_binary_packet($data);
- fclose($this->fsock);
- $this->bitmap = 0;
- }
- }
-
- /**
- * Gets Binary Packets
- *
- * See 'The Binary Packet Protocol' of protocol-1.5.txt for more info.
- *
- * Also, this function could be improved upon by adding detection for the following exploit:
- * http://www.securiteam.com/securitynews/5LP042K3FY.html
- *
- * @see Net_SSH1::_send_binary_packet()
- * @return Array
- * @access private
- */
- function _get_binary_packet()
- {
- if (feof($this->fsock)) {
- //user_error('connection closed prematurely');
- return false;
- }
-
- if ($this->curTimeout) {
- $read = array($this->fsock);
- $write = $except = null;
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
- $sec = floor($this->curTimeout);
- $usec = 1000000 * ($this->curTimeout - $sec);
- // on windows this returns a "Warning: Invalid CRT parameters detected" error
- if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
- //$this->_disconnect('Timeout');
- return true;
- }
- $elapsed = strtok(microtime(), ' ') + strtok('') - $start;
- $this->curTimeout-= $elapsed;
- }
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
- $temp = unpack('Nlength', fread($this->fsock, 4));
-
- $padding_length = 8 - ($temp['length'] & 7);
- $length = $temp['length'] + $padding_length;
-
- while ($length > 0) {
- $temp = fread($this->fsock, $length);
- $raw.= $temp;
- $length-= strlen($temp);
- }
- $stop = strtok(microtime(), ' ') + strtok('');
-
- if (strlen($raw) && $this->crypto !== false) {
- $raw = $this->crypto->decrypt($raw);
- }
-
- $padding = substr($raw, 0, $padding_length);
- $type = $raw[$padding_length];
- $data = substr($raw, $padding_length + 1, -4);
-
- $temp = unpack('Ncrc', substr($raw, -4));
-
- //if ( $temp['crc'] != $this->_crc($padding . $type . $data) ) {
- // user_error('Bad CRC in packet from server');
- // return false;
- //}
-
- $type = ord($type);
-
- if (defined('NET_SSH1_LOGGING')) {
- $temp = isset($this->protocol_flags[$type]) ? $this->protocol_flags[$type] : 'UNKNOWN';
- $temp = '<- ' . $temp .
- ' (' . round($stop - $start, 4) . 's)';
- $this->_append_log($temp, $data);
- }
-
- return array(
- NET_SSH1_RESPONSE_TYPE => $type,
- NET_SSH1_RESPONSE_DATA => $data
- );
- }
-
- /**
- * Sends Binary Packets
- *
- * Returns true on success, false on failure.
- *
- * @see Net_SSH1::_get_binary_packet()
- * @param String $data
- * @return Boolean
- * @access private
- */
- function _send_binary_packet($data)
- {
- if (feof($this->fsock)) {
- //user_error('connection closed prematurely');
- return false;
- }
-
- $length = strlen($data) + 4;
-
- $padding = crypt_random_string(8 - ($length & 7));
-
- $orig = $data;
- $data = $padding . $data;
- $data.= pack('N', $this->_crc($data));
-
- if ($this->crypto !== false) {
- $data = $this->crypto->encrypt($data);
- }
-
- $packet = pack('Na*', $length, $data);
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
- $result = strlen($packet) == fputs($this->fsock, $packet);
- $stop = strtok(microtime(), ' ') + strtok('');
-
- if (defined('NET_SSH1_LOGGING')) {
- $temp = isset($this->protocol_flags[ord($orig[0])]) ? $this->protocol_flags[ord($orig[0])] : 'UNKNOWN';
- $temp = '-> ' . $temp .
- ' (' . round($stop - $start, 4) . 's)';
- $this->_append_log($temp, $orig);
- }
-
- return $result;
- }
-
- /**
- * Cyclic Redundancy Check (CRC)
- *
- * PHP's crc32 function is implemented slightly differently than the one that SSH v1 uses, so
- * we've reimplemented it. A more detailed discussion of the differences can be found after
- * $crc_lookup_table's initialization.
- *
- * @see Net_SSH1::_get_binary_packet()
- * @see Net_SSH1::_send_binary_packet()
- * @param String $data
- * @return Integer
- * @access private
- */
- function _crc($data)
- {
- static $crc_lookup_table = array(
- 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
- 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
- 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
- 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
- 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
- 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
- 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
- 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
- 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
- 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
- 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
- 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
- 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
- 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
- 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
- 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
- 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
- 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
- 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
- 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
- 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
- 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
- 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
- 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
- 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
- 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
- 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
- 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
- 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
- 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
- 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
- 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
- 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
- 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
- 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
- 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
- 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
- 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
- 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
- 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
- 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
- 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
- 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
- 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
- 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
- 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
- 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
- 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
- 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
- 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
- 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
- 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
- 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
- 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
- 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
- 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
- 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
- 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
- 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
- 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
- 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
- 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
- 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
- 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
- );
-
- // For this function to yield the same output as PHP's crc32 function, $crc would have to be
- // set to 0xFFFFFFFF, initially - not 0x00000000 as it currently is.
- $crc = 0x00000000;
- $length = strlen($data);
-
- for ($i=0;$i<$length;$i++) {
- // We AND $crc >> 8 with 0x00FFFFFF because we want the eight newly added bits to all
- // be zero. PHP, unfortunately, doesn't always do this. 0x80000000 >> 8, as an example,
- // yields 0xFF800000 - not 0x00800000. The following link elaborates:
- // http://www.php.net/manual/en/language.operators.bitwise.php#57281
- $crc = (($crc >> 8) & 0x00FFFFFF) ^ $crc_lookup_table[($crc & 0xFF) ^ ord($data[$i])];
- }
-
- // In addition to having to set $crc to 0xFFFFFFFF, initially, the return value must be XOR'd with
- // 0xFFFFFFFF for this function to return the same thing that PHP's crc32 function would.
- return $crc;
- }
-
- /**
- * String Shift
- *
- * Inspired by array_shift
- *
- * @param String $string
- * @param optional Integer $index
- * @return String
- * @access private
- */
- function _string_shift(&$string, $index = 1)
- {
- $substr = substr($string, 0, $index);
- $string = substr($string, $index);
- return $substr;
- }
-
- /**
- * RSA Encrypt
- *
- * Returns mod(pow($m, $e), $n), where $n should be the product of two (large) primes $p and $q and where $e
- * should be a number with the property that gcd($e, ($p - 1) * ($q - 1)) == 1. Could just make anything that
- * calls this call modexp, instead, but I think this makes things clearer, maybe...
- *
- * @see Net_SSH1::Net_SSH1()
- * @param Math_BigInteger $m
- * @param Array $key
- * @return Math_BigInteger
- * @access private
- */
- function _rsa_crypt($m, $key)
- {
- /*
- if (!class_exists('Crypt_RSA')) {
- include_once 'Crypt/RSA.php';
- }
-
- $rsa = new Crypt_RSA();
- $rsa->loadKey($key, CRYPT_RSA_PUBLIC_FORMAT_RAW);
- $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
- return $rsa->encrypt($m);
- */
-
- // To quote from protocol-1.5.txt:
- // The most significant byte (which is only partial as the value must be
- // less than the public modulus, which is never a power of two) is zero.
- //
- // The next byte contains the value 2 (which stands for public-key
- // encrypted data in the PKCS standard [PKCS#1]). Then, there are non-
- // zero random bytes to fill any unused space, a zero byte, and the data
- // to be encrypted in the least significant bytes, the last byte of the
- // data in the least significant byte.
-
- // Presumably the part of PKCS#1 they're refering to is "Section 7.2.1 Encryption Operation",
- // under "7.2 RSAES-PKCS1-v1.5" and "7 Encryption schemes" of the following URL:
- // ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf
- $modulus = $key[1]->toBytes();
- $length = strlen($modulus) - strlen($m) - 3;
- $random = '';
- while (strlen($random) != $length) {
- $block = crypt_random_string($length - strlen($random));
- $block = str_replace("\x00", '', $block);
- $random.= $block;
- }
- $temp = chr(0) . chr(2) . $random . chr(0) . $m;
-
- $m = new Math_BigInteger($temp, 256);
- $m = $m->modPow($key[0], $key[1]);
-
- return $m->toBytes();
- }
-
- /**
- * Define Array
- *
- * Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of
- * named constants from it, using the value as the name of the constant and the index as the value of the constant.
- * If any of the constants that would be defined already exists, none of the constants will be defined.
- *
- * @param Array $array
- * @access private
- */
- function _define_array()
- {
- $args = func_get_args();
- foreach ($args as $arg) {
- foreach ($arg as $key=>$value) {
- if (!defined($value)) {
- define($value, $key);
- } else {
- break 2;
- }
- }
- }
- }
-
- /**
- * Returns a log of the packets that have been sent and received.
- *
- * Returns a string if NET_SSH1_LOGGING == NET_SSH1_LOG_COMPLEX, an array if NET_SSH1_LOGGING == NET_SSH1_LOG_SIMPLE and false if !defined('NET_SSH1_LOGGING')
- *
- * @access public
- * @return String or Array
- */
- function getLog()
- {
- if (!defined('NET_SSH1_LOGGING')) {
- return false;
- }
-
- switch (NET_SSH1_LOGGING) {
- case NET_SSH1_LOG_SIMPLE:
- return $this->message_number_log;
- break;
- case NET_SSH1_LOG_COMPLEX:
- return $this->_format_log($this->message_log, $this->protocol_flags_log);
- break;
- default:
- return false;
- }
- }
-
- /**
- * Formats a log for printing
- *
- * @param Array $message_log
- * @param Array $message_number_log
- * @access private
- * @return String
- */
- function _format_log($message_log, $message_number_log)
- {
- $output = '';
- for ($i = 0; $i < count($message_log); $i++) {
- $output.= $message_number_log[$i] . "\r\n";
- $current_log = $message_log[$i];
- $j = 0;
- do {
- if (strlen($current_log)) {
- $output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 ';
- }
- $fragment = $this->_string_shift($current_log, $this->log_short_width);
- $hex = substr(preg_replace_callback('#.#s', array($this, '_format_log_helper'), $fragment), strlen($this->log_boundary));
- // replace non ASCII printable characters with dots
- // http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
- // also replace < with a . since < messes up the output on web browsers
- $raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment);
- $output.= str_pad($hex, $this->log_long_width - $this->log_short_width, ' ') . $raw . "\r\n";
- $j++;
- } while (strlen($current_log));
- $output.= "\r\n";
- }
-
- return $output;
- }
-
- /**
- * Helper function for _format_log
- *
- * For use with preg_replace_callback()
- *
- * @param Array $matches
- * @access private
- * @return String
- */
- function _format_log_helper($matches)
- {
- return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT);
- }
-
- /**
- * Return the server key public exponent
- *
- * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead,
- * the raw bytes. This behavior is similar to PHP's md5() function.
- *
- * @param optional Boolean $raw_output
- * @return String
- * @access public
- */
- function getServerKeyPublicExponent($raw_output = false)
- {
- return $raw_output ? $this->server_key_public_exponent->toBytes() : $this->server_key_public_exponent->toString();
- }
-
- /**
- * Return the server key public modulus
- *
- * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead,
- * the raw bytes. This behavior is similar to PHP's md5() function.
- *
- * @param optional Boolean $raw_output
- * @return String
- * @access public
- */
- function getServerKeyPublicModulus($raw_output = false)
- {
- return $raw_output ? $this->server_key_public_modulus->toBytes() : $this->server_key_public_modulus->toString();
- }
-
- /**
- * Return the host key public exponent
- *
- * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead,
- * the raw bytes. This behavior is similar to PHP's md5() function.
- *
- * @param optional Boolean $raw_output
- * @return String
- * @access public
- */
- function getHostKeyPublicExponent($raw_output = false)
- {
- return $raw_output ? $this->host_key_public_exponent->toBytes() : $this->host_key_public_exponent->toString();
- }
-
- /**
- * Return the host key public modulus
- *
- * Returns, by default, the base-10 representation. If $raw_output is set to true, returns, instead,
- * the raw bytes. This behavior is similar to PHP's md5() function.
- *
- * @param optional Boolean $raw_output
- * @return String
- * @access public
- */
- function getHostKeyPublicModulus($raw_output = false)
- {
- return $raw_output ? $this->host_key_public_modulus->toBytes() : $this->host_key_public_modulus->toString();
- }
-
- /**
- * Return a list of ciphers supported by SSH1 server.
- *
- * Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output
- * is set to true, returns, instead, an array of constants. ie. instead of array('Triple-DES in CBC mode'), you'll
- * get array(NET_SSH1_CIPHER_3DES).
- *
- * @param optional Boolean $raw_output
- * @return Array
- * @access public
- */
- function getSupportedCiphers($raw_output = false)
- {
- return $raw_output ? array_keys($this->supported_ciphers) : array_values($this->supported_ciphers);
- }
-
- /**
- * Return a list of authentications supported by SSH1 server.
- *
- * Just because a cipher is supported by an SSH1 server doesn't mean it's supported by this library. If $raw_output
- * is set to true, returns, instead, an array of constants. ie. instead of array('password authentication'), you'll
- * get array(NET_SSH1_AUTH_PASSWORD).
- *
- * @param optional Boolean $raw_output
- * @return Array
- * @access public
- */
- function getSupportedAuthentications($raw_output = false)
- {
- return $raw_output ? array_keys($this->supported_authentications) : array_values($this->supported_authentications);
- }
-
- /**
- * Return the server identification.
- *
- * @return String
- * @access public
- */
- function getServerIdentification()
- {
- return rtrim($this->server_identification);
- }
-
- /**
- * Logs data packets
- *
- * Makes sure that only the last 1MB worth of packets will be logged
- *
- * @param String $data
- * @access private
- */
- function _append_log($protocol_flags, $message)
- {
- switch (NET_SSH1_LOGGING) {
- // useful for benchmarks
- case NET_SSH1_LOG_SIMPLE:
- $this->protocol_flags_log[] = $protocol_flags;
- break;
- // the most useful log for SSH1
- case NET_SSH1_LOG_COMPLEX:
- $this->protocol_flags_log[] = $protocol_flags;
- $this->_string_shift($message);
- $this->log_size+= strlen($message);
- $this->message_log[] = $message;
- while ($this->log_size > NET_SSH1_LOG_MAX_SIZE) {
- $this->log_size-= strlen(array_shift($this->message_log));
- array_shift($this->protocol_flags_log);
- }
- break;
- // dump the output out realtime; packets may be interspersed with non packets,
- // passwords won't be filtered out and select other packets may not be correctly
- // identified
- case NET_SSH1_LOG_REALTIME:
- echo "\r\n" . $this->_format_log(array($message), array($protocol_flags)) . "\r\n
\r\n";
- @flush();
- @ob_flush();
- break;
- // basically the same thing as NET_SSH1_LOG_REALTIME with the caveat that NET_SSH1_LOG_REALTIME_FILE
- // needs to be defined and that the resultant log file will be capped out at NET_SSH1_LOG_MAX_SIZE.
- // the earliest part of the log file is denoted by the first <<< START >>> and is not going to necessarily
- // at the beginning of the file
- case NET_SSH1_LOG_REALTIME_FILE:
- if (!isset($this->realtime_log_file)) {
- // PHP doesn't seem to like using constants in fopen()
- $filename = NET_SSH1_LOG_REALTIME_FILE;
- $fp = fopen($filename, 'w');
- $this->realtime_log_file = $fp;
- }
- if (!is_resource($this->realtime_log_file)) {
- break;
- }
- $entry = $this->_format_log(array($message), array($protocol_flags));
- if ($this->realtime_log_wrap) {
- $temp = "<<< START >>>\r\n";
- $entry.= $temp;
- fseek($this->realtime_log_file, ftell($this->realtime_log_file) - strlen($temp));
- }
- $this->realtime_log_size+= strlen($entry);
- if ($this->realtime_log_size > NET_SSH1_LOG_MAX_SIZE) {
- fseek($this->realtime_log_file, 0);
- $this->realtime_log_size = strlen($entry);
- $this->realtime_log_wrap = true;
- }
- fputs($this->realtime_log_file, $entry);
- }
- }
-}
diff --git a/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SSH2.php b/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SSH2.php
deleted file mode 100644
index 2fddb68a9b..0000000000
--- a/apps/files_external/3rdparty/phpseclib/phpseclib/Net/SSH2.php
+++ /dev/null
@@ -1,3686 +0,0 @@
-
- * login('username', 'password')) {
- * exit('Login Failed');
- * }
- *
- * echo $ssh->exec('pwd');
- * echo $ssh->exec('ls -la');
- * ?>
- *
- *
- *
- * setPassword('whatever');
- * $key->loadKey(file_get_contents('privatekey'));
- *
- * $ssh = new Net_SSH2('www.domain.tld');
- * if (!$ssh->login('username', $key)) {
- * exit('Login Failed');
- * }
- *
- * echo $ssh->read('username@username:~$');
- * $ssh->write("ls -la\n");
- * echo $ssh->read('username@username:~$');
- * ?>
- *
- *
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @category Net
- * @package Net_SSH2
- * @author Jim Wigginton
- * @copyright MMVII Jim Wigginton
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
- * @link http://phpseclib.sourceforge.net
- */
-
-/**#@+
- * Execution Bitmap Masks
- *
- * @see Net_SSH2::bitmap
- * @access private
- */
-define('NET_SSH2_MASK_CONSTRUCTOR', 0x00000001);
-define('NET_SSH2_MASK_LOGIN_REQ', 0x00000002);
-define('NET_SSH2_MASK_LOGIN', 0x00000004);
-define('NET_SSH2_MASK_SHELL', 0x00000008);
-define('NET_SSH2_MASK_WINDOW_ADJUST', 0X00000010);
-/**#@-*/
-
-/**#@+
- * Channel constants
- *
- * 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.
- *
- * @see Net_SSH2::_send_channel_packet()
- * @see Net_SSH2::_get_channel_packet()
- * @access private
- */
-define('NET_SSH2_CHANNEL_EXEC', 0); // PuTTy uses 0x100
-define('NET_SSH2_CHANNEL_SHELL', 1);
-define('NET_SSH2_CHANNEL_SUBSYSTEM', 2);
-/**#@-*/
-
-/**#@+
- * @access public
- * @see Net_SSH2::getLog()
- */
-/**
- * Returns the message numbers
- */
-define('NET_SSH2_LOG_SIMPLE', 1);
-/**
- * Returns the message content
- */
-define('NET_SSH2_LOG_COMPLEX', 2);
-/**
- * Outputs the content real-time
- */
-define('NET_SSH2_LOG_REALTIME', 3);
-/**
- * Dumps the content real-time to a file
- */
-define('NET_SSH2_LOG_REALTIME_FILE', 4);
-/**#@-*/
-
-/**#@+
- * @access public
- * @see Net_SSH2::read()
- */
-/**
- * Returns when a string matching $expect exactly is found
- */
-define('NET_SSH2_READ_SIMPLE', 1);
-/**
- * Returns when a string matching the regular expression $expect is found
- */
-define('NET_SSH2_READ_REGEX', 2);
-/**
- * Make sure that the log never gets larger than this
- */
-define('NET_SSH2_LOG_MAX_SIZE', 1024 * 1024);
-/**#@-*/
-
-/**
- * Pure-PHP implementation of SSHv2.
- *
- * @package Net_SSH2
- * @author Jim Wigginton
- * @version 0.1.0
- * @access public
- */
-class Net_SSH2
-{
- /**
- * The SSH identifier
- *
- * @var String
- * @access private
- */
- var $identifier;
-
- /**
- * The Socket Object
- *
- * @var Object
- * @access private
- */
- var $fsock;
-
- /**
- * Execution Bitmap
- *
- * The bits that are set represent functions that have been called already. This is used to determine
- * if a requisite function has been successfully executed. If not, an error should be thrown.
- *
- * @var Integer
- * @access private
- */
- var $bitmap = 0;
-
- /**
- * Error information
- *
- * @see Net_SSH2::getErrors()
- * @see Net_SSH2::getLastError()
- * @var String
- * @access private
- */
- var $errors = array();
-
- /**
- * Server Identifier
- *
- * @see Net_SSH2::getServerIdentification()
- * @var String
- * @access private
- */
- var $server_identifier = '';
-
- /**
- * Key Exchange Algorithms
- *
- * @see Net_SSH2::getKexAlgorithims()
- * @var Array
- * @access private
- */
- var $kex_algorithms;
-
- /**
- * Server Host Key Algorithms
- *
- * @see Net_SSH2::getServerHostKeyAlgorithms()
- * @var Array
- * @access private
- */
- var $server_host_key_algorithms;
-
- /**
- * Encryption Algorithms: Client to Server
- *
- * @see Net_SSH2::getEncryptionAlgorithmsClient2Server()
- * @var Array
- * @access private
- */
- var $encryption_algorithms_client_to_server;
-
- /**
- * Encryption Algorithms: Server to Client
- *
- * @see Net_SSH2::getEncryptionAlgorithmsServer2Client()
- * @var Array
- * @access private
- */
- var $encryption_algorithms_server_to_client;
-
- /**
- * MAC Algorithms: Client to Server
- *
- * @see Net_SSH2::getMACAlgorithmsClient2Server()
- * @var Array
- * @access private
- */
- var $mac_algorithms_client_to_server;
-
- /**
- * MAC Algorithms: Server to Client
- *
- * @see Net_SSH2::getMACAlgorithmsServer2Client()
- * @var Array
- * @access private
- */
- var $mac_algorithms_server_to_client;
-
- /**
- * Compression Algorithms: Client to Server
- *
- * @see Net_SSH2::getCompressionAlgorithmsClient2Server()
- * @var Array
- * @access private
- */
- var $compression_algorithms_client_to_server;
-
- /**
- * Compression Algorithms: Server to Client
- *
- * @see Net_SSH2::getCompressionAlgorithmsServer2Client()
- * @var Array
- * @access private
- */
- var $compression_algorithms_server_to_client;
-
- /**
- * Languages: Server to Client
- *
- * @see Net_SSH2::getLanguagesServer2Client()
- * @var Array
- * @access private
- */
- var $languages_server_to_client;
-
- /**
- * Languages: Client to Server
- *
- * @see Net_SSH2::getLanguagesClient2Server()
- * @var Array
- * @access private
- */
- var $languages_client_to_server;
-
- /**
- * Block Size for Server to Client Encryption
- *
- * "Note that the length of the concatenation of 'packet_length',
- * 'padding_length', 'payload', and 'random padding' MUST be a multiple
- * of the cipher block size or 8, whichever is larger. This constraint
- * MUST be enforced, even when using stream ciphers."
- *
- * -- http://tools.ietf.org/html/rfc4253#section-6
- *
- * @see Net_SSH2::Net_SSH2()
- * @see Net_SSH2::_send_binary_packet()
- * @var Integer
- * @access private
- */
- var $encrypt_block_size = 8;
-
- /**
- * Block Size for Client to Server Encryption
- *
- * @see Net_SSH2::Net_SSH2()
- * @see Net_SSH2::_get_binary_packet()
- * @var Integer
- * @access private
- */
- var $decrypt_block_size = 8;
-
- /**
- * Server to Client Encryption Object
- *
- * @see Net_SSH2::_get_binary_packet()
- * @var Object
- * @access private
- */
- var $decrypt = false;
-
- /**
- * Client to Server Encryption Object
- *
- * @see Net_SSH2::_send_binary_packet()
- * @var Object
- * @access private
- */
- var $encrypt = false;
-
- /**
- * Client to Server HMAC Object
- *
- * @see Net_SSH2::_send_binary_packet()
- * @var Object
- * @access private
- */
- var $hmac_create = false;
-
- /**
- * Server to Client HMAC Object
- *
- * @see Net_SSH2::_get_binary_packet()
- * @var Object
- * @access private
- */
- var $hmac_check = false;
-
- /**
- * Size of server to client HMAC
- *
- * We need to know how big the HMAC will be for the server to client direction so that we know how many bytes to read.
- * For the client to server side, the HMAC object will make the HMAC as long as it needs to be. All we need to do is
- * append it.
- *
- * @see Net_SSH2::_get_binary_packet()
- * @var Integer
- * @access private
- */
- var $hmac_size = false;
-
- /**
- * Server Public Host Key
- *
- * @see Net_SSH2::getServerPublicHostKey()
- * @var String
- * @access private
- */
- var $server_public_host_key;
-
- /**
- * Session identifer
- *
- * "The exchange hash H from the first key exchange is additionally
- * used as the session identifier, which is a unique identifier for
- * this connection."
- *
- * -- http://tools.ietf.org/html/rfc4253#section-7.2
- *
- * @see Net_SSH2::_key_exchange()
- * @var String
- * @access private
- */
- var $session_id = false;
-
- /**
- * Exchange hash
- *
- * The current exchange hash
- *
- * @see Net_SSH2::_key_exchange()
- * @var String
- * @access private
- */
- var $exchange_hash = false;
-
- /**
- * Message Numbers
- *
- * @see Net_SSH2::Net_SSH2()
- * @var Array
- * @access private
- */
- var $message_numbers = array();
-
- /**
- * Disconnection Message 'reason codes' defined in RFC4253
- *
- * @see Net_SSH2::Net_SSH2()
- * @var Array
- * @access private
- */
- var $disconnect_reasons = array();
-
- /**
- * SSH_MSG_CHANNEL_OPEN_FAILURE 'reason codes', defined in RFC4254
- *
- * @see Net_SSH2::Net_SSH2()
- * @var Array
- * @access private
- */
- var $channel_open_failure_reasons = array();
-
- /**
- * Terminal Modes
- *
- * @link http://tools.ietf.org/html/rfc4254#section-8
- * @see Net_SSH2::Net_SSH2()
- * @var Array
- * @access private
- */
- var $terminal_modes = array();
-
- /**
- * SSH_MSG_CHANNEL_EXTENDED_DATA's data_type_codes
- *
- * @link http://tools.ietf.org/html/rfc4254#section-5.2
- * @see Net_SSH2::Net_SSH2()
- * @var Array
- * @access private
- */
- var $channel_extended_data_type_codes = array();
-
- /**
- * Send Sequence Number
- *
- * See 'Section 6.4. Data Integrity' of rfc4253 for more info.
- *
- * @see Net_SSH2::_send_binary_packet()
- * @var Integer
- * @access private
- */
- var $send_seq_no = 0;
-
- /**
- * Get Sequence Number
- *
- * See 'Section 6.4. Data Integrity' of rfc4253 for more info.
- *
- * @see Net_SSH2::_get_binary_packet()
- * @var Integer
- * @access private
- */
- 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();
-
- /**
- * Channel Buffers
- *
- * If a client requests a packet from one channel but receives two packets from another those packets should
- * be placed in a buffer
- *
- * @see Net_SSH2::_get_channel_packet()
- * @see Net_SSH2::exec()
- * @var Array
- * @access private
- */
- var $channel_buffers = array();
-
- /**
- * Channel Status
- *
- * Contains the type of the last sent message
- *
- * @see Net_SSH2::_get_channel_packet()
- * @var Array
- * @access private
- */
- var $channel_status = array();
-
- /**
- * Packet Size
- *
- * Maximum packet size indexed by channel
- *
- * @see Net_SSH2::_send_channel_packet()
- * @var Array
- * @access private
- */
- var $packet_size_client_to_server = array();
-
- /**
- * Message Number Log
- *
- * @see Net_SSH2::getLog()
- * @var Array
- * @access private
- */
- var $message_number_log = array();
-
- /**
- * Message Log
- *
- * @see Net_SSH2::getLog()
- * @var Array
- * @access private
- */
- var $message_log = array();
-
- /**
- * The Window Size
- *
- * Bytes the other party can send before it must wait for the window to be adjusted (0x7FFFFFFF = 2GB)
- *
- * @var Integer
- * @see Net_SSH2::_send_channel_packet()
- * @see Net_SSH2::exec()
- * @access private
- */
- var $window_size = 0x7FFFFFFF;
-
- /**
- * Window size, server to client
- *
- * Window size indexed by channel
- *
- * @see Net_SSH2::_send_channel_packet()
- * @var Array
- * @access private
- */
- var $window_size_server_to_client = array();
-
- /**
- * Window size, client to server
- *
- * Window size indexed by channel
- *
- * @see Net_SSH2::_get_channel_packet()
- * @var Array
- * @access private
- */
- var $window_size_client_to_server = array();
-
- /**
- * Server signature
- *
- * Verified against $this->session_id
- *
- * @see Net_SSH2::getServerPublicHostKey()
- * @var String
- * @access private
- */
- var $signature = '';
-
- /**
- * Server signature format
- *
- * ssh-rsa or ssh-dss.
- *
- * @see Net_SSH2::getServerPublicHostKey()
- * @var String
- * @access private
- */
- var $signature_format = '';
-
- /**
- * Interactive Buffer
- *
- * @see Net_SSH2::read()
- * @var Array
- * @access private
- */
- var $interactiveBuffer = '';
-
- /**
- * Current log size
- *
- * Should never exceed NET_SSH2_LOG_MAX_SIZE
- *
- * @see Net_SSH2::_send_binary_packet()
- * @see Net_SSH2::_get_binary_packet()
- * @var Integer
- * @access private
- */
- var $log_size;
-
- /**
- * Timeout
- *
- * @see Net_SSH2::setTimeout()
- * @access private
- */
- var $timeout;
-
- /**
- * Current Timeout
- *
- * @see Net_SSH2::_get_channel_packet()
- * @access private
- */
- var $curTimeout;
-
- /**
- * Real-time log file pointer
- *
- * @see Net_SSH2::_append_log()
- * @var Resource
- * @access private
- */
- var $realtime_log_file;
-
- /**
- * Real-time log file size
- *
- * @see Net_SSH2::_append_log()
- * @var Integer
- * @access private
- */
- var $realtime_log_size;
-
- /**
- * Has the signature been validated?
- *
- * @see Net_SSH2::getServerPublicHostKey()
- * @var Boolean
- * @access private
- */
- var $signature_validated = false;
-
- /**
- * Real-time log file wrap boolean
- *
- * @see Net_SSH2::_append_log()
- * @access private
- */
- var $realtime_log_wrap;
-
- /**
- * Flag to suppress stderr from output
- *
- * @see Net_SSH2::enableQuietMode()
- * @access private
- */
- var $quiet_mode = false;
-
- /**
- * Time of first network activity
- *
- * @access private
- */
- var $last_packet;
-
- /**
- * Exit status returned from ssh if any
- *
- * @var Integer
- * @access private
- */
- var $exit_status;
-
- /**
- * Flag to request a PTY when using exec()
- *
- * @see Net_SSH2::enablePTY()
- * @access private
- */
- var $request_pty = false;
-
- /**
- * Flag set while exec() is running when using enablePTY()
- *
- * @access private
- */
- var $in_request_pty_exec = false;
-
- /**
- * Flag set after startSubsystem() is called
- *
- * @access private
- */
- var $in_subsystem;
-
- /**
- * Contents of stdError
- *
- * @access private
- */
- var $stdErrorLog;
-
- /**
- * The Last Interactive Response
- *
- * @see Net_SSH2::_keyboard_interactive_process()
- * @access private
- */
- var $last_interactive_response = '';
-
- /**
- * Keyboard Interactive Request / Responses
- *
- * @see Net_SSH2::_keyboard_interactive_process()
- * @access private
- */
- var $keyboard_requests_responses = array();
-
- /**
- * Banner Message
- *
- * Quoting from the RFC, "in some jurisdictions, sending a warning message before
- * authentication may be relevant for getting legal protection."
- *
- * @see Net_SSH2::_filter()
- * @see Net_SSH2::getBannerMessage()
- * @access private
- */
- var $banner_message = '';
-
- /**
- * Did read() timeout or return normally?
- *
- * @see Net_SSH2::isTimeout
- * @access private
- */
- var $is_timeout = false;
-
- /**
- * Log Boundary
- *
- * @see Net_SSH2::_format_log
- * @access private
- */
- var $log_boundary = ':';
-
- /**
- * Log Long Width
- *
- * @see Net_SSH2::_format_log
- * @access private
- */
- var $log_long_width = 65;
-
- /**
- * Log Short Width
- *
- * @see Net_SSH2::_format_log
- * @access private
- */
- var $log_short_width = 16;
-
- /**
- * Default Constructor.
- *
- * Connects to an SSHv2 server
- *
- * @param String $host
- * @param optional Integer $port
- * @param optional Integer $timeout
- * @return Net_SSH2
- * @access public
- */
- function Net_SSH2($host, $port = 22, $timeout = 10)
- {
- // Include Math_BigInteger
- // Used to do Diffie-Hellman key exchange and DSA/RSA signature verification.
- if (!class_exists('Math_BigInteger')) {
- include_once 'Math/BigInteger.php';
- }
-
- if (!function_exists('crypt_random_string')) {
- include_once 'Crypt/Random.php';
- }
-
- if (!class_exists('Crypt_Hash')) {
- include_once 'Crypt/Hash.php';
- }
-
- $this->last_packet = strtok(microtime(), ' ') + strtok(''); // == microtime(true) in PHP5
- $this->message_numbers = array(
- 1 => 'NET_SSH2_MSG_DISCONNECT',
- 2 => 'NET_SSH2_MSG_IGNORE',
- 3 => 'NET_SSH2_MSG_UNIMPLEMENTED',
- 4 => 'NET_SSH2_MSG_DEBUG',
- 5 => 'NET_SSH2_MSG_SERVICE_REQUEST',
- 6 => 'NET_SSH2_MSG_SERVICE_ACCEPT',
- 20 => 'NET_SSH2_MSG_KEXINIT',
- 21 => 'NET_SSH2_MSG_NEWKEYS',
- 30 => 'NET_SSH2_MSG_KEXDH_INIT',
- 31 => 'NET_SSH2_MSG_KEXDH_REPLY',
- 50 => 'NET_SSH2_MSG_USERAUTH_REQUEST',
- 51 => 'NET_SSH2_MSG_USERAUTH_FAILURE',
- 52 => 'NET_SSH2_MSG_USERAUTH_SUCCESS',
- 53 => 'NET_SSH2_MSG_USERAUTH_BANNER',
-
- 80 => 'NET_SSH2_MSG_GLOBAL_REQUEST',
- 81 => 'NET_SSH2_MSG_REQUEST_SUCCESS',
- 82 => 'NET_SSH2_MSG_REQUEST_FAILURE',
- 90 => 'NET_SSH2_MSG_CHANNEL_OPEN',
- 91 => 'NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION',
- 92 => 'NET_SSH2_MSG_CHANNEL_OPEN_FAILURE',
- 93 => 'NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST',
- 94 => 'NET_SSH2_MSG_CHANNEL_DATA',
- 95 => 'NET_SSH2_MSG_CHANNEL_EXTENDED_DATA',
- 96 => 'NET_SSH2_MSG_CHANNEL_EOF',
- 97 => 'NET_SSH2_MSG_CHANNEL_CLOSE',
- 98 => 'NET_SSH2_MSG_CHANNEL_REQUEST',
- 99 => 'NET_SSH2_MSG_CHANNEL_SUCCESS',
- 100 => 'NET_SSH2_MSG_CHANNEL_FAILURE'
- );
- $this->disconnect_reasons = array(
- 1 => 'NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT',
- 2 => 'NET_SSH2_DISCONNECT_PROTOCOL_ERROR',
- 3 => 'NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED',
- 4 => 'NET_SSH2_DISCONNECT_RESERVED',
- 5 => 'NET_SSH2_DISCONNECT_MAC_ERROR',
- 6 => 'NET_SSH2_DISCONNECT_COMPRESSION_ERROR',
- 7 => 'NET_SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE',
- 8 => 'NET_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED',
- 9 => 'NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE',
- 10 => 'NET_SSH2_DISCONNECT_CONNECTION_LOST',
- 11 => 'NET_SSH2_DISCONNECT_BY_APPLICATION',
- 12 => 'NET_SSH2_DISCONNECT_TOO_MANY_CONNECTIONS',
- 13 => 'NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER',
- 14 => 'NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE',
- 15 => 'NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME'
- );
- $this->channel_open_failure_reasons = array(
- 1 => 'NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED'
- );
- $this->terminal_modes = array(
- 0 => 'NET_SSH2_TTY_OP_END'
- );
- $this->channel_extended_data_type_codes = array(
- 1 => 'NET_SSH2_EXTENDED_DATA_STDERR'
- );
-
- $this->_define_array(
- $this->message_numbers,
- $this->disconnect_reasons,
- $this->channel_open_failure_reasons,
- $this->terminal_modes,
- $this->channel_extended_data_type_codes,
- array(60 => 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ'),
- array(60 => 'NET_SSH2_MSG_USERAUTH_PK_OK'),
- array(60 => 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
- 61 => 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE')
- );
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
- $this->fsock = @fsockopen($host, $port, $errno, $errstr, $timeout);
- if (!$this->fsock) {
- user_error(rtrim("Cannot connect to $host. Error $errno. $errstr"));
- return;
- }
- $elapsed = strtok(microtime(), ' ') + strtok('') - $start;
-
- $timeout-= $elapsed;
-
- if ($timeout <= 0) {
- user_error(rtrim("Cannot connect to $host. Timeout error"));
- return;
- }
-
- $read = array($this->fsock);
- $write = $except = null;
-
- $sec = floor($timeout);
- $usec = 1000000 * ($timeout - $sec);
-
- // on windows this returns a "Warning: Invalid CRT parameters detected" error
- // the !count() is done as a workaround for
- if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
- user_error(rtrim("Cannot connect to $host. Banner timeout"));
- return;
- }
-
- /* According to the SSH2 specs,
-
- "The server MAY send other lines of data before sending the version
- string. Each line SHOULD be terminated by a Carriage Return and Line
- Feed. Such lines MUST NOT begin with "SSH-", and SHOULD be encoded
- in ISO-10646 UTF-8 [RFC3629] (language is not specified). Clients
- MUST be able to process such lines." */
- $temp = '';
- $extra = '';
- while (!feof($this->fsock) && !preg_match('#^SSH-(\d\.\d+)#', $temp, $matches)) {
- if (substr($temp, -2) == "\r\n") {
- $extra.= $temp;
- $temp = '';
- }
- $temp.= fgets($this->fsock, 255);
- }
-
- if (feof($this->fsock)) {
- user_error('Connection closed by server');
- return false;
- }
-
- $this->identifier = $this->_generate_identifier();
-
- if (defined('NET_SSH2_LOGGING')) {
- $this->_append_log('<-', $extra . $temp);
- $this->_append_log('->', $this->identifier . "\r\n");
- }
-
- $this->server_identifier = trim($temp, "\r\n");
- if (strlen($extra)) {
- $this->errors[] = utf8_decode($extra);
- }
-
- if ($matches[1] != '1.99' && $matches[1] != '2.0') {
- user_error("Cannot connect to SSH $matches[1] servers");
- return;
- }
-
- fputs($this->fsock, $this->identifier . "\r\n");
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return;
- }
-
- if (ord($response[0]) != NET_SSH2_MSG_KEXINIT) {
- user_error('Expected SSH_MSG_KEXINIT');
- return;
- }
-
- if (!$this->_key_exchange($response)) {
- return;
- }
-
- $this->bitmap = NET_SSH2_MASK_CONSTRUCTOR;
- }
-
- /**
- * Generates the SSH identifier
- *
- * You should overwrite this method in your own class if you want to use another identifier
- *
- * @access protected
- * @return String
- */
- function _generate_identifier()
- {
- $identifier = 'SSH-2.0-phpseclib_0.3';
-
- $ext = array();
- if (extension_loaded('mcrypt')) {
- $ext[] = 'mcrypt';
- }
-
- if (extension_loaded('gmp')) {
- $ext[] = 'gmp';
- } elseif (extension_loaded('bcmath')) {
- $ext[] = 'bcmath';
- }
-
- if (!empty($ext)) {
- $identifier .= ' (' . implode(', ', $ext) . ')';
- }
-
- return $identifier;
- }
-
- /**
- * Key Exchange
- *
- * @param String $kexinit_payload_server
- * @access private
- */
- function _key_exchange($kexinit_payload_server)
- {
- static $kex_algorithms = array(
- 'diffie-hellman-group1-sha1', // REQUIRED
- 'diffie-hellman-group14-sha1' // REQUIRED
- );
-
- static $server_host_key_algorithms = array(
- 'ssh-rsa', // RECOMMENDED sign Raw RSA Key
- 'ssh-dss' // REQUIRED sign Raw DSS Key
- );
-
- static $encryption_algorithms = false;
- if ($encryption_algorithms === false) {
- $encryption_algorithms = array(
- // from :
- 'arcfour256',
- 'arcfour128',
-
- 'arcfour', // OPTIONAL the ARCFOUR stream cipher with a 128-bit key
-
- // CTR modes from :
- 'aes128-ctr', // RECOMMENDED AES (Rijndael) in SDCTR mode, with 128-bit key
- 'aes192-ctr', // RECOMMENDED AES with 192-bit key
- 'aes256-ctr', // RECOMMENDED AES with 256-bit key
-
- 'twofish128-ctr', // OPTIONAL Twofish in SDCTR mode, with 128-bit key
- 'twofish192-ctr', // OPTIONAL Twofish with 192-bit key
- 'twofish256-ctr', // OPTIONAL Twofish with 256-bit key
-
- 'aes128-cbc', // RECOMMENDED AES with a 128-bit key
- 'aes192-cbc', // OPTIONAL AES with a 192-bit key
- 'aes256-cbc', // OPTIONAL AES in CBC mode, with a 256-bit key
-
- 'twofish128-cbc', // OPTIONAL Twofish with a 128-bit key
- 'twofish192-cbc', // OPTIONAL Twofish with a 192-bit key
- 'twofish256-cbc',
- 'twofish-cbc', // OPTIONAL alias for "twofish256-cbc"
- // (this is being retained for historical reasons)
-
- 'blowfish-ctr', // OPTIONAL Blowfish in SDCTR mode
-
- 'blowfish-cbc', // OPTIONAL Blowfish in CBC mode
-
- '3des-ctr', // RECOMMENDED Three-key 3DES in SDCTR mode
-
- '3des-cbc', // REQUIRED three-key 3DES in CBC mode
- 'none' // OPTIONAL no encryption; NOT RECOMMENDED
- );
-
- if (!$this->_is_includable('Crypt/RC4.php')) {
- $encryption_algorithms = array_diff(
- $encryption_algorithms,
- array('arcfour256', 'arcfour128', 'arcfour')
- );
- }
- if (!$this->_is_includable('Crypt/Rijndael.php')) {
- $encryption_algorithms = array_diff(
- $encryption_algorithms,
- array('aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', 'aes192-cbc', 'aes256-cbc')
- );
- }
- if (!$this->_is_includable('Crypt/Twofish.php')) {
- $encryption_algorithms = array_diff(
- $encryption_algorithms,
- array('twofish128-ctr', 'twofish192-ctr', 'twofish256-ctr', 'twofish128-cbc', 'twofish192-cbc', 'twofish256-cbc', 'twofish-cbc')
- );
- }
- if (!$this->_is_includable('Crypt/Blowfish.php')) {
- $encryption_algorithms = array_diff(
- $encryption_algorithms,
- array('blowfish-ctr', 'blowfish-cbc')
- );
- }
- if (!$this->_is_includable('Crypt/TripleDES.php')) {
- $encryption_algorithms = array_diff(
- $encryption_algorithms,
- array('3des-ctr', '3des-cbc')
- );
- }
- $encryption_algorithms = array_values($encryption_algorithms);
- }
-
- static $mac_algorithms = array(
- 'hmac-sha1-96', // RECOMMENDED first 96 bits of HMAC-SHA1 (digest length = 12, key length = 20)
- 'hmac-sha1', // REQUIRED HMAC-SHA1 (digest length = key length = 20)
- 'hmac-md5-96', // OPTIONAL first 96 bits of HMAC-MD5 (digest length = 12, key length = 16)
- 'hmac-md5', // OPTIONAL HMAC-MD5 (digest length = key length = 16)
- 'none' // OPTIONAL no MAC; NOT RECOMMENDED
- );
-
- static $compression_algorithms = array(
- 'none' // REQUIRED no compression
- //'zlib' // OPTIONAL ZLIB (LZ77) compression
- );
-
- // some SSH servers have buggy implementations of some of the above algorithms
- switch ($this->server_identifier) {
- case 'SSH-2.0-SSHD':
- $mac_algorithms = array_values(array_diff(
- $mac_algorithms,
- array('hmac-sha1-96', 'hmac-md5-96')
- ));
- }
-
- static $str_kex_algorithms, $str_server_host_key_algorithms,
- $encryption_algorithms_server_to_client, $mac_algorithms_server_to_client, $compression_algorithms_server_to_client,
- $encryption_algorithms_client_to_server, $mac_algorithms_client_to_server, $compression_algorithms_client_to_server;
-
- if (empty($str_kex_algorithms)) {
- $str_kex_algorithms = implode(',', $kex_algorithms);
- $str_server_host_key_algorithms = implode(',', $server_host_key_algorithms);
- $encryption_algorithms_server_to_client = $encryption_algorithms_client_to_server = implode(',', $encryption_algorithms);
- $mac_algorithms_server_to_client = $mac_algorithms_client_to_server = implode(',', $mac_algorithms);
- $compression_algorithms_server_to_client = $compression_algorithms_client_to_server = implode(',', $compression_algorithms);
- }
-
- $client_cookie = crypt_random_string(16);
-
- $response = $kexinit_payload_server;
- $this->_string_shift($response, 1); // skip past the message number (it should be SSH_MSG_KEXINIT)
- $server_cookie = $this->_string_shift($response, 16);
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->kex_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->server_host_key_algorithms = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->encryption_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->encryption_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->mac_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->mac_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->compression_algorithms_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->compression_algorithms_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->languages_client_to_server = explode(',', $this->_string_shift($response, $temp['length']));
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->languages_server_to_client = explode(',', $this->_string_shift($response, $temp['length']));
-
- extract(unpack('Cfirst_kex_packet_follows', $this->_string_shift($response, 1)));
- $first_kex_packet_follows = $first_kex_packet_follows != 0;
-
- // the sending of SSH2_MSG_KEXINIT could go in one of two places. this is the second place.
- $kexinit_payload_client = pack('Ca*Na*Na*Na*Na*Na*Na*Na*Na*Na*Na*CN',
- NET_SSH2_MSG_KEXINIT, $client_cookie, strlen($str_kex_algorithms), $str_kex_algorithms,
- strlen($str_server_host_key_algorithms), $str_server_host_key_algorithms, strlen($encryption_algorithms_client_to_server),
- $encryption_algorithms_client_to_server, strlen($encryption_algorithms_server_to_client), $encryption_algorithms_server_to_client,
- strlen($mac_algorithms_client_to_server), $mac_algorithms_client_to_server, strlen($mac_algorithms_server_to_client),
- $mac_algorithms_server_to_client, strlen($compression_algorithms_client_to_server), $compression_algorithms_client_to_server,
- strlen($compression_algorithms_server_to_client), $compression_algorithms_server_to_client, 0, '', 0, '',
- 0, 0
- );
-
- if (!$this->_send_binary_packet($kexinit_payload_client)) {
- return false;
- }
- // here ends the second place.
-
- // we need to decide upon the symmetric encryption algorithms before we do the diffie-hellman key exchange
- for ($i = 0; $i < count($encryption_algorithms) && !in_array($encryption_algorithms[$i], $this->encryption_algorithms_server_to_client); $i++);
- if ($i == count($encryption_algorithms)) {
- user_error('No compatible server to client encryption algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- // we don't initialize any crypto-objects, yet - we do that, later. for now, we need the lengths to make the
- // diffie-hellman key exchange as fast as possible
- $decrypt = $encryption_algorithms[$i];
- switch ($decrypt) {
- case '3des-cbc':
- case '3des-ctr':
- $decryptKeyLength = 24; // eg. 192 / 8
- break;
- case 'aes256-cbc':
- case 'aes256-ctr':
- case 'twofish-cbc':
- case 'twofish256-cbc':
- case 'twofish256-ctr':
- $decryptKeyLength = 32; // eg. 256 / 8
- break;
- case 'aes192-cbc':
- case 'aes192-ctr':
- case 'twofish192-cbc':
- case 'twofish192-ctr':
- $decryptKeyLength = 24; // eg. 192 / 8
- break;
- case 'aes128-cbc':
- case 'aes128-ctr':
- case 'twofish128-cbc':
- case 'twofish128-ctr':
- case 'blowfish-cbc':
- case 'blowfish-ctr':
- $decryptKeyLength = 16; // eg. 128 / 8
- break;
- case 'arcfour':
- case 'arcfour128':
- $decryptKeyLength = 16; // eg. 128 / 8
- break;
- case 'arcfour256':
- $decryptKeyLength = 32; // eg. 128 / 8
- break;
- case 'none';
- $decryptKeyLength = 0;
- }
-
- for ($i = 0; $i < count($encryption_algorithms) && !in_array($encryption_algorithms[$i], $this->encryption_algorithms_client_to_server); $i++);
- if ($i == count($encryption_algorithms)) {
- user_error('No compatible client to server encryption algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- $encrypt = $encryption_algorithms[$i];
- switch ($encrypt) {
- case '3des-cbc':
- case '3des-ctr':
- $encryptKeyLength = 24;
- break;
- case 'aes256-cbc':
- case 'aes256-ctr':
- case 'twofish-cbc':
- case 'twofish256-cbc':
- case 'twofish256-ctr':
- $encryptKeyLength = 32;
- break;
- case 'aes192-cbc':
- case 'aes192-ctr':
- case 'twofish192-cbc':
- case 'twofish192-ctr':
- $encryptKeyLength = 24;
- break;
- case 'aes128-cbc':
- case 'aes128-ctr':
- case 'twofish128-cbc':
- case 'twofish128-ctr':
- case 'blowfish-cbc':
- case 'blowfish-ctr':
- $encryptKeyLength = 16;
- break;
- case 'arcfour':
- case 'arcfour128':
- $encryptKeyLength = 16;
- break;
- case 'arcfour256':
- $encryptKeyLength = 32;
- break;
- case 'none';
- $encryptKeyLength = 0;
- }
-
- $keyLength = $decryptKeyLength > $encryptKeyLength ? $decryptKeyLength : $encryptKeyLength;
-
- // through diffie-hellman key exchange a symmetric key is obtained
- for ($i = 0; $i < count($kex_algorithms) && !in_array($kex_algorithms[$i], $this->kex_algorithms); $i++);
- if ($i == count($kex_algorithms)) {
- user_error('No compatible key exchange algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- switch ($kex_algorithms[$i]) {
- // see http://tools.ietf.org/html/rfc2409#section-6.2 and
- // http://tools.ietf.org/html/rfc2412, appendex E
- case 'diffie-hellman-group1-sha1':
- $prime = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' .
- '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' .
- '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
- 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF';
- break;
- // see http://tools.ietf.org/html/rfc3526#section-3
- case 'diffie-hellman-group14-sha1':
- $prime = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' .
- '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' .
- '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' .
- 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05' .
- '98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB' .
- '9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' .
- 'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718' .
- '3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF';
- break;
- }
-
- // For both diffie-hellman-group1-sha1 and diffie-hellman-group14-sha1
- // the generator field element is 2 (decimal) and the hash function is sha1.
- $g = new Math_BigInteger(2);
- $prime = new Math_BigInteger($prime, 16);
- $kexHash = new Crypt_Hash('sha1');
- //$q = $p->bitwise_rightShift(1);
-
- /* To increase the speed of the key exchange, both client and server may
- reduce the size of their private exponents. It should be at least
- twice as long as the key material that is generated from the shared
- secret. For more details, see the paper by van Oorschot and Wiener
- [VAN-OORSCHOT].
-
- -- http://tools.ietf.org/html/rfc4419#section-6.2 */
- $one = new Math_BigInteger(1);
- $keyLength = min($keyLength, $kexHash->getLength());
- $max = $one->bitwise_leftShift(16 * $keyLength); // 2 * 8 * $keyLength
- $max = $max->subtract($one);
-
- $x = $one->random($one, $max);
- $e = $g->modPow($x, $prime);
-
- $eBytes = $e->toBytes(true);
- $data = pack('CNa*', NET_SSH2_MSG_KEXDH_INIT, strlen($eBytes), $eBytes);
-
- if (!$this->_send_binary_packet($data)) {
- user_error('Connection closed by server');
- return false;
- }
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
-
- if ($type != NET_SSH2_MSG_KEXDH_REPLY) {
- user_error('Expected SSH_MSG_KEXDH_REPLY');
- return false;
- }
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->server_public_host_key = $server_public_host_key = $this->_string_shift($response, $temp['length']);
-
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $public_key_format = $this->_string_shift($server_public_host_key, $temp['length']);
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $fBytes = $this->_string_shift($response, $temp['length']);
- $f = new Math_BigInteger($fBytes, -256);
-
- $temp = unpack('Nlength', $this->_string_shift($response, 4));
- $this->signature = $this->_string_shift($response, $temp['length']);
-
- $temp = unpack('Nlength', $this->_string_shift($this->signature, 4));
- $this->signature_format = $this->_string_shift($this->signature, $temp['length']);
-
- $key = $f->modPow($x, $prime);
- $keyBytes = $key->toBytes(true);
-
- $this->exchange_hash = pack('Na*Na*Na*Na*Na*Na*Na*Na*',
- strlen($this->identifier), $this->identifier, strlen($this->server_identifier), $this->server_identifier,
- strlen($kexinit_payload_client), $kexinit_payload_client, strlen($kexinit_payload_server),
- $kexinit_payload_server, strlen($this->server_public_host_key), $this->server_public_host_key, strlen($eBytes),
- $eBytes, strlen($fBytes), $fBytes, strlen($keyBytes), $keyBytes
- );
-
- $this->exchange_hash = $kexHash->hash($this->exchange_hash);
-
- if ($this->session_id === false) {
- $this->session_id = $this->exchange_hash;
- }
-
- for ($i = 0; $i < count($server_host_key_algorithms) && !in_array($server_host_key_algorithms[$i], $this->server_host_key_algorithms); $i++);
- if ($i == count($server_host_key_algorithms)) {
- user_error('No compatible server host key algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- if ($public_key_format != $server_host_key_algorithms[$i] || $this->signature_format != $server_host_key_algorithms[$i]) {
- user_error('Server Host Key Algorithm Mismatch');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- $packet = pack('C',
- NET_SSH2_MSG_NEWKEYS
- );
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $response = $this->_get_binary_packet();
-
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
-
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
-
- if ($type != NET_SSH2_MSG_NEWKEYS) {
- user_error('Expected SSH_MSG_NEWKEYS');
- return false;
- }
-
- switch ($encrypt) {
- case '3des-cbc':
- if (!class_exists('Crypt_TripleDES')) {
- include_once 'Crypt/TripleDES.php';
- }
- $this->encrypt = new Crypt_TripleDES();
- // $this->encrypt_block_size = 64 / 8 == the default
- break;
- case '3des-ctr':
- if (!class_exists('Crypt_TripleDES')) {
- include_once 'Crypt/TripleDES.php';
- }
- $this->encrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
- // $this->encrypt_block_size = 64 / 8 == the default
- break;
- case 'aes256-cbc':
- case 'aes192-cbc':
- case 'aes128-cbc':
- if (!class_exists('Crypt_Rijndael')) {
- include_once 'Crypt/Rijndael.php';
- }
- $this->encrypt = new Crypt_Rijndael();
- $this->encrypt_block_size = 16; // eg. 128 / 8
- break;
- case 'aes256-ctr':
- case 'aes192-ctr':
- case 'aes128-ctr':
- if (!class_exists('Crypt_Rijndael')) {
- include_once 'Crypt/Rijndael.php';
- }
- $this->encrypt = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_CTR);
- $this->encrypt_block_size = 16; // eg. 128 / 8
- break;
- case 'blowfish-cbc':
- if (!class_exists('Crypt_Blowfish')) {
- include_once 'Crypt/Blowfish.php';
- }
- $this->encrypt = new Crypt_Blowfish();
- $this->encrypt_block_size = 8;
- break;
- case 'blowfish-ctr':
- if (!class_exists('Crypt_Blowfish')) {
- include_once 'Crypt/Blowfish.php';
- }
- $this->encrypt = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR);
- $this->encrypt_block_size = 8;
- break;
- case 'twofish128-cbc':
- case 'twofish192-cbc':
- case 'twofish256-cbc':
- case 'twofish-cbc':
- if (!class_exists('Crypt_Twofish')) {
- include_once 'Crypt/Twofish.php';
- }
- $this->encrypt = new Crypt_Twofish();
- $this->encrypt_block_size = 16;
- break;
- case 'twofish128-ctr':
- case 'twofish192-ctr':
- case 'twofish256-ctr':
- if (!class_exists('Crypt_Twofish')) {
- include_once 'Crypt/Twofish.php';
- }
- $this->encrypt = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR);
- $this->encrypt_block_size = 16;
- break;
- case 'arcfour':
- case 'arcfour128':
- case 'arcfour256':
- if (!class_exists('Crypt_RC4')) {
- include_once 'Crypt/RC4.php';
- }
- $this->encrypt = new Crypt_RC4();
- break;
- case 'none';
- //$this->encrypt = new Crypt_Null();
- }
-
- switch ($decrypt) {
- case '3des-cbc':
- if (!class_exists('Crypt_TripleDES')) {
- include_once 'Crypt/TripleDES.php';
- }
- $this->decrypt = new Crypt_TripleDES();
- break;
- case '3des-ctr':
- if (!class_exists('Crypt_TripleDES')) {
- include_once 'Crypt/TripleDES.php';
- }
- $this->decrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
- break;
- case 'aes256-cbc':
- case 'aes192-cbc':
- case 'aes128-cbc':
- if (!class_exists('Crypt_Rijndael')) {
- include_once 'Crypt/Rijndael.php';
- }
- $this->decrypt = new Crypt_Rijndael();
- $this->decrypt_block_size = 16;
- break;
- case 'aes256-ctr':
- case 'aes192-ctr':
- case 'aes128-ctr':
- if (!class_exists('Crypt_Rijndael')) {
- include_once 'Crypt/Rijndael.php';
- }
- $this->decrypt = new Crypt_Rijndael(CRYPT_RIJNDAEL_MODE_CTR);
- $this->decrypt_block_size = 16;
- break;
- case 'blowfish-cbc':
- if (!class_exists('Crypt_Blowfish')) {
- include_once 'Crypt/Blowfish.php';
- }
- $this->decrypt = new Crypt_Blowfish();
- $this->decrypt_block_size = 8;
- break;
- case 'blowfish-ctr':
- if (!class_exists('Crypt_Blowfish')) {
- include_once 'Crypt/Blowfish.php';
- }
- $this->decrypt = new Crypt_Blowfish(CRYPT_BLOWFISH_MODE_CTR);
- $this->decrypt_block_size = 8;
- break;
- case 'twofish128-cbc':
- case 'twofish192-cbc':
- case 'twofish256-cbc':
- case 'twofish-cbc':
- if (!class_exists('Crypt_Twofish')) {
- include_once 'Crypt/Twofish.php';
- }
- $this->decrypt = new Crypt_Twofish();
- $this->decrypt_block_size = 16;
- break;
- case 'twofish128-ctr':
- case 'twofish192-ctr':
- case 'twofish256-ctr':
- if (!class_exists('Crypt_Twofish')) {
- include_once 'Crypt/Twofish.php';
- }
- $this->decrypt = new Crypt_Twofish(CRYPT_TWOFISH_MODE_CTR);
- $this->decrypt_block_size = 16;
- break;
- case 'arcfour':
- case 'arcfour128':
- case 'arcfour256':
- if (!class_exists('Crypt_RC4')) {
- include_once 'Crypt/RC4.php';
- }
- $this->decrypt = new Crypt_RC4();
- break;
- case 'none';
- //$this->decrypt = new Crypt_Null();
- }
-
- $keyBytes = pack('Na*', strlen($keyBytes), $keyBytes);
-
- if ($this->encrypt) {
- $this->encrypt->enableContinuousBuffer();
- $this->encrypt->disablePadding();
-
- $iv = $kexHash->hash($keyBytes . $this->exchange_hash . 'A' . $this->session_id);
- while ($this->encrypt_block_size > strlen($iv)) {
- $iv.= $kexHash->hash($keyBytes . $this->exchange_hash . $iv);
- }
- $this->encrypt->setIV(substr($iv, 0, $this->encrypt_block_size));
-
- $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'C' . $this->session_id);
- while ($encryptKeyLength > strlen($key)) {
- $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
- }
- $this->encrypt->setKey(substr($key, 0, $encryptKeyLength));
- }
-
- if ($this->decrypt) {
- $this->decrypt->enableContinuousBuffer();
- $this->decrypt->disablePadding();
-
- $iv = $kexHash->hash($keyBytes . $this->exchange_hash . 'B' . $this->session_id);
- while ($this->decrypt_block_size > strlen($iv)) {
- $iv.= $kexHash->hash($keyBytes . $this->exchange_hash . $iv);
- }
- $this->decrypt->setIV(substr($iv, 0, $this->decrypt_block_size));
-
- $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'D' . $this->session_id);
- while ($decryptKeyLength > strlen($key)) {
- $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
- }
- $this->decrypt->setKey(substr($key, 0, $decryptKeyLength));
- }
-
- /* The "arcfour128" algorithm is the RC4 cipher, as described in
- [SCHNEIER], using a 128-bit key. The first 1536 bytes of keystream
- generated by the cipher MUST be discarded, and the first byte of the
- first encrypted packet MUST be encrypted using the 1537th byte of
- keystream.
-
- -- http://tools.ietf.org/html/rfc4345#section-4 */
- if ($encrypt == 'arcfour128' || $encrypt == 'arcfour256') {
- $this->encrypt->encrypt(str_repeat("\0", 1536));
- }
- if ($decrypt == 'arcfour128' || $decrypt == 'arcfour256') {
- $this->decrypt->decrypt(str_repeat("\0", 1536));
- }
-
- for ($i = 0; $i < count($mac_algorithms) && !in_array($mac_algorithms[$i], $this->mac_algorithms_client_to_server); $i++);
- if ($i == count($mac_algorithms)) {
- user_error('No compatible client to server message authentication algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- $createKeyLength = 0; // ie. $mac_algorithms[$i] == 'none'
- switch ($mac_algorithms[$i]) {
- case 'hmac-sha1':
- $this->hmac_create = new Crypt_Hash('sha1');
- $createKeyLength = 20;
- break;
- case 'hmac-sha1-96':
- $this->hmac_create = new Crypt_Hash('sha1-96');
- $createKeyLength = 20;
- break;
- case 'hmac-md5':
- $this->hmac_create = new Crypt_Hash('md5');
- $createKeyLength = 16;
- break;
- case 'hmac-md5-96':
- $this->hmac_create = new Crypt_Hash('md5-96');
- $createKeyLength = 16;
- }
-
- for ($i = 0; $i < count($mac_algorithms) && !in_array($mac_algorithms[$i], $this->mac_algorithms_server_to_client); $i++);
- if ($i == count($mac_algorithms)) {
- user_error('No compatible server to client message authentication algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- $checkKeyLength = 0;
- $this->hmac_size = 0;
- switch ($mac_algorithms[$i]) {
- case 'hmac-sha1':
- $this->hmac_check = new Crypt_Hash('sha1');
- $checkKeyLength = 20;
- $this->hmac_size = 20;
- break;
- case 'hmac-sha1-96':
- $this->hmac_check = new Crypt_Hash('sha1-96');
- $checkKeyLength = 20;
- $this->hmac_size = 12;
- break;
- case 'hmac-md5':
- $this->hmac_check = new Crypt_Hash('md5');
- $checkKeyLength = 16;
- $this->hmac_size = 16;
- break;
- case 'hmac-md5-96':
- $this->hmac_check = new Crypt_Hash('md5-96');
- $checkKeyLength = 16;
- $this->hmac_size = 12;
- }
-
- $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'E' . $this->session_id);
- while ($createKeyLength > strlen($key)) {
- $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
- }
- $this->hmac_create->setKey(substr($key, 0, $createKeyLength));
-
- $key = $kexHash->hash($keyBytes . $this->exchange_hash . 'F' . $this->session_id);
- while ($checkKeyLength > strlen($key)) {
- $key.= $kexHash->hash($keyBytes . $this->exchange_hash . $key);
- }
- $this->hmac_check->setKey(substr($key, 0, $checkKeyLength));
-
- for ($i = 0; $i < count($compression_algorithms) && !in_array($compression_algorithms[$i], $this->compression_algorithms_server_to_client); $i++);
- if ($i == count($compression_algorithms)) {
- user_error('No compatible server to client compression algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $this->decompress = $compression_algorithms[$i] == 'zlib';
-
- for ($i = 0; $i < count($compression_algorithms) && !in_array($compression_algorithms[$i], $this->compression_algorithms_client_to_server); $i++);
- if ($i == count($compression_algorithms)) {
- user_error('No compatible client to server compression algorithms found');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
- $this->compress = $compression_algorithms[$i] == 'zlib';
-
- return true;
- }
-
- /**
- * Login
- *
- * The $password parameter can be a plaintext password, a Crypt_RSA object or an array
- *
- * @param String $username
- * @param Mixed $password
- * @param Mixed $...
- * @return Boolean
- * @see _login
- * @access public
- */
- function login($username)
- {
- $args = func_get_args();
- return call_user_func_array(array(&$this, '_login'), $args);
- }
-
- /**
- * Login Helper
- *
- * @param String $username
- * @param Mixed $password
- * @param Mixed $...
- * @return Boolean
- * @see _login_helper
- * @access private
- */
- function _login($username)
- {
- $args = array_slice(func_get_args(), 1);
- if (empty($args)) {
- return $this->_login_helper($username);
- }
-
- foreach ($args as $arg) {
- if ($this->_login_helper($username, $arg)) {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Login Helper
- *
- * @param String $username
- * @param optional String $password
- * @return Boolean
- * @access private
- * @internal It might be worthwhile, at some point, to protect against {@link http://tools.ietf.org/html/rfc4251#section-9.3.9 traffic analysis}
- * by sending dummy SSH_MSG_IGNORE messages.
- */
- function _login_helper($username, $password = null)
- {
- if (!($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR)) {
- return false;
- }
-
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN_REQ)) {
- $packet = pack('CNa*',
- NET_SSH2_MSG_SERVICE_REQUEST, strlen('ssh-userauth'), 'ssh-userauth'
- );
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
-
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
-
- if ($type != NET_SSH2_MSG_SERVICE_ACCEPT) {
- user_error('Expected SSH_MSG_SERVICE_ACCEPT');
- return false;
- }
- $this->bitmap |= NET_SSH2_MASK_LOGIN_REQ;
- }
-
- if (strlen($this->last_interactive_response)) {
- return !is_string($password) && !is_array($password) ? false : $this->_keyboard_interactive_process($password);
- }
-
- // although PHP5's get_class() preserves the case, PHP4's does not
- if (is_object($password)) {
- switch (strtolower(get_class($password))) {
- case 'crypt_rsa':
- return $this->_privatekey_login($username, $password);
- case 'system_ssh_agent':
- return $this->_ssh_agent_login($username, $password);
- }
- }
-
- if (is_array($password)) {
- if ($this->_keyboard_interactive_login($username, $password)) {
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- }
- return false;
- }
-
- if (!isset($password)) {
- $packet = pack('CNa*Na*Na*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
- strlen('none'), 'none'
- );
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
-
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
-
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_SUCCESS:
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- //case NET_SSH2_MSG_USERAUTH_FAILURE:
- default:
- return false;
- }
- }
-
- $packet = pack('CNa*Na*Na*CNa*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
- strlen('password'), 'password', 0, strlen($password), $password
- );
-
- // remove the username and password from the logged packet
- if (!defined('NET_SSH2_LOGGING')) {
- $logged = null;
- } else {
- $logged = pack('CNa*Na*Na*CNa*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen('username'), 'username', strlen('ssh-connection'), 'ssh-connection',
- strlen('password'), 'password', 0, strlen('password'), 'password'
- );
- }
-
- if (!$this->_send_binary_packet($packet, $logged)) {
- return false;
- }
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
-
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
-
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ: // in theory, the password can be changed
- if (defined('NET_SSH2_LOGGING')) {
- $this->message_number_log[count($this->message_number_log) - 1] = 'NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ';
- }
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->errors[] = 'SSH_MSG_USERAUTH_PASSWD_CHANGEREQ: ' . utf8_decode($this->_string_shift($response, $length));
- return $this->_disconnect(NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
- case NET_SSH2_MSG_USERAUTH_FAILURE:
- // can we use keyboard-interactive authentication? if not then either the login is bad or the server employees
- // multi-factor authentication
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $auth_methods = explode(',', $this->_string_shift($response, $length));
- extract(unpack('Cpartial_success', $this->_string_shift($response, 1)));
- $partial_success = $partial_success != 0;
-
- if (!$partial_success && in_array('keyboard-interactive', $auth_methods)) {
- if ($this->_keyboard_interactive_login($username, $password)) {
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- }
- return false;
- }
- return false;
- case NET_SSH2_MSG_USERAUTH_SUCCESS:
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- }
-
- return false;
- }
-
- /**
- * Login via keyboard-interactive authentication
- *
- * See {@link http://tools.ietf.org/html/rfc4256 RFC4256} for details. This is not a full-featured keyboard-interactive authenticator.
- *
- * @param String $username
- * @param String $password
- * @return Boolean
- * @access private
- */
- function _keyboard_interactive_login($username, $password)
- {
- $packet = pack('CNa*Na*Na*Na*Na*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
- strlen('keyboard-interactive'), 'keyboard-interactive', 0, '', 0, ''
- );
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- return $this->_keyboard_interactive_process($password);
- }
-
- /**
- * Handle the keyboard-interactive requests / responses.
- *
- * @param String $responses...
- * @return Boolean
- * @access private
- */
- function _keyboard_interactive_process()
- {
- $responses = func_get_args();
-
- if (strlen($this->last_interactive_response)) {
- $response = $this->last_interactive_response;
- } else {
- $orig = $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- }
-
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
-
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_INFO_REQUEST:
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->_string_shift($response, $length); // name; may be empty
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->_string_shift($response, $length); // instruction; may be empty
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->_string_shift($response, $length); // language tag; may be empty
- extract(unpack('Nnum_prompts', $this->_string_shift($response, 4)));
-
- for ($i = 0; $i < count($responses); $i++) {
- if (is_array($responses[$i])) {
- foreach ($responses[$i] as $key => $value) {
- $this->keyboard_requests_responses[$key] = $value;
- }
- unset($responses[$i]);
- }
- }
- $responses = array_values($responses);
-
- if (isset($this->keyboard_requests_responses)) {
- for ($i = 0; $i < $num_prompts; $i++) {
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- // prompt - ie. "Password: "; must not be empty
- $prompt = $this->_string_shift($response, $length);
- //$echo = $this->_string_shift($response) != chr(0);
- foreach ($this->keyboard_requests_responses as $key => $value) {
- if (substr($prompt, 0, strlen($key)) == $key) {
- $responses[] = $value;
- break;
- }
- }
- }
- }
-
- // see http://tools.ietf.org/html/rfc4256#section-3.2
- if (strlen($this->last_interactive_response)) {
- $this->last_interactive_response = '';
- } else if (defined('NET_SSH2_LOGGING')) {
- $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
- 'UNKNOWN',
- 'NET_SSH2_MSG_USERAUTH_INFO_REQUEST',
- $this->message_number_log[count($this->message_number_log) - 1]
- );
- }
-
- if (!count($responses) && $num_prompts) {
- $this->last_interactive_response = $orig;
- $this->bitmap |= NET_SSH_MASK_LOGIN_INTERACTIVE;
- return false;
- }
-
- /*
- After obtaining the requested information from the user, the client
- MUST respond with an SSH_MSG_USERAUTH_INFO_RESPONSE message.
- */
- // see http://tools.ietf.org/html/rfc4256#section-3.4
- $packet = $logged = pack('CN', NET_SSH2_MSG_USERAUTH_INFO_RESPONSE, count($responses));
- for ($i = 0; $i < count($responses); $i++) {
- $packet.= pack('Na*', strlen($responses[$i]), $responses[$i]);
- $logged.= pack('Na*', strlen('dummy-answer'), 'dummy-answer');
- }
-
- if (!$this->_send_binary_packet($packet, $logged)) {
- return false;
- }
-
- if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
- $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
- 'UNKNOWN',
- 'NET_SSH2_MSG_USERAUTH_INFO_RESPONSE',
- $this->message_number_log[count($this->message_number_log) - 1]
- );
- }
-
- /*
- After receiving the response, the server MUST send either an
- SSH_MSG_USERAUTH_SUCCESS, SSH_MSG_USERAUTH_FAILURE, or another
- SSH_MSG_USERAUTH_INFO_REQUEST message.
- */
- // maybe phpseclib should force close the connection after x request / responses? unless something like that is done
- // there could be an infinite loop of request / responses.
- return $this->_keyboard_interactive_process();
- case NET_SSH2_MSG_USERAUTH_SUCCESS:
- return true;
- case NET_SSH2_MSG_USERAUTH_FAILURE:
- return false;
- }
-
- return false;
- }
-
- /**
- * Login with an ssh-agent provided key
- *
- * @param String $username
- * @param System_SSH_Agent $agent
- * @return Boolean
- * @access private
- */
- function _ssh_agent_login($username, $agent)
- {
- $keys = $agent->requestIdentities();
- foreach ($keys as $key) {
- if ($this->_privatekey_login($username, $key)) {
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * Login with an RSA private key
- *
- * @param String $username
- * @param Crypt_RSA $password
- * @return Boolean
- * @access private
- * @internal It might be worthwhile, at some point, to protect against {@link http://tools.ietf.org/html/rfc4251#section-9.3.9 traffic analysis}
- * by sending dummy SSH_MSG_IGNORE messages.
- */
- function _privatekey_login($username, $privatekey)
- {
- // see http://tools.ietf.org/html/rfc4253#page-15
- $publickey = $privatekey->getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_RAW);
- if ($publickey === false) {
- return false;
- }
-
- $publickey = array(
- 'e' => $publickey['e']->toBytes(true),
- 'n' => $publickey['n']->toBytes(true)
- );
- $publickey = pack('Na*Na*Na*',
- strlen('ssh-rsa'), 'ssh-rsa', strlen($publickey['e']), $publickey['e'], strlen($publickey['n']), $publickey['n']
- );
-
- $part1 = pack('CNa*Na*Na*',
- NET_SSH2_MSG_USERAUTH_REQUEST, strlen($username), $username, strlen('ssh-connection'), 'ssh-connection',
- strlen('publickey'), 'publickey'
- );
- $part2 = pack('Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publickey), $publickey);
-
- $packet = $part1 . chr(0) . $part2;
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
-
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
-
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_FAILURE:
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->errors[] = 'SSH_MSG_USERAUTH_FAILURE: ' . $this->_string_shift($response, $length);
- return false;
- case NET_SSH2_MSG_USERAUTH_PK_OK:
- // we'll just take it on faith that the public key blob and the public key algorithm name are as
- // they should be
- if (defined('NET_SSH2_LOGGING') && NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX) {
- $this->message_number_log[count($this->message_number_log) - 1] = str_replace(
- 'UNKNOWN',
- 'NET_SSH2_MSG_USERAUTH_PK_OK',
- $this->message_number_log[count($this->message_number_log) - 1]
- );
- }
- }
-
- $packet = $part1 . chr(1) . $part2;
- $privatekey->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
- $signature = $privatekey->sign(pack('Na*a*', strlen($this->session_id), $this->session_id, $packet));
- $signature = pack('Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($signature), $signature);
- $packet.= pack('Na*', strlen($signature), $signature);
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
-
- extract(unpack('Ctype', $this->_string_shift($response, 1)));
-
- switch ($type) {
- case NET_SSH2_MSG_USERAUTH_FAILURE:
- // either the login is bad or the server employs multi-factor authentication
- return false;
- case NET_SSH2_MSG_USERAUTH_SUCCESS:
- $this->bitmap |= NET_SSH2_MASK_LOGIN;
- return true;
- }
-
- return false;
- }
-
- /**
- * Set Timeout
- *
- * $ssh->exec('ping 127.0.0.1'); on a Linux host will never return and will run indefinitely. setTimeout() makes it so it'll timeout.
- * Setting $timeout to false or 0 will mean there is no timeout.
- *
- * @param Mixed $timeout
- * @access public
- */
- function setTimeout($timeout)
- {
- $this->timeout = $this->curTimeout = $timeout;
- }
-
- /**
- * Get the output from stdError
- *
- * @access public
- */
- function getStdError()
- {
- return $this->stdErrorLog;
- }
-
- /**
- * Execute Command
- *
- * If $block is set to false then Net_SSH2::_get_channel_packet(NET_SSH2_CHANNEL_EXEC) will need to be called manually.
- * In all likelihood, this is not a feature you want to be taking advantage of.
- *
- * @param String $command
- * @param optional Boolean $block
- * @return String
- * @access public
- */
- function exec($command, $callback = null)
- {
- $this->curTimeout = $this->timeout;
- $this->is_timeout = false;
- $this->stdErrorLog = '';
-
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- return false;
- }
-
- // RFC4254 defines the (client) window size as "bytes the other party can send before it must wait for the window to
- // be adjusted". 0x7FFFFFFF is, at 2GB, the max size. technically, it should probably be decremented, but,
- // honestly, if you're transfering more than 2GB, you probably shouldn't be using phpseclib, anyway.
- // see http://tools.ietf.org/html/rfc4254#section-5.2 for more info
- $this->window_size_server_to_client[NET_SSH2_CHANNEL_EXEC] = 0x7FFFFFFF;
- // 0x8000 is the maximum max packet size, per http://tools.ietf.org/html/rfc4253#section-6.1, although since PuTTy
- // uses 0x4000, that's what will be used here, as well.
- $packet_size = 0x4000;
-
- $packet = pack('CNa*N3',
- NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_EXEC, $this->window_size_server_to_client[NET_SSH2_CHANNEL_EXEC], $packet_size);
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_OPEN;
-
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
- if ($response === false) {
- return false;
- }
-
- if ($this->request_pty === true) {
- $terminal_modes = pack('C', NET_SSH2_TTY_OP_END);
- $packet = pack('CNNa*CNa*N5a*',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_EXEC], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100',
- 80, 24, 0, 0, strlen($terminal_modes), $terminal_modes);
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
-
- list(, $type) = unpack('C', $this->_string_shift($response, 1));
-
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_SUCCESS:
- break;
- case NET_SSH2_MSG_CHANNEL_FAILURE:
- default:
- user_error('Unable to request pseudo-terminal');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- $this->in_request_pty_exec = true;
- }
-
- // sending a pty-req SSH_MSG_CHANNEL_REQUEST message is unnecessary and, in fact, in most cases, slows things
- // down. the one place where it might be desirable is if you're doing something like Net_SSH2::exec('ping localhost &').
- // with a pty-req SSH_MSG_CHANNEL_REQUEST, exec() will return immediately and the ping process will then
- // then immediately terminate. without such a request exec() will loop indefinitely. the ping process won't end but
- // neither will your script.
-
- // although, in theory, the size of SSH_MSG_CHANNEL_REQUEST could exceed the maximum packet size established by
- // SSH_MSG_CHANNEL_OPEN_CONFIRMATION, RFC4254#section-5.1 states that the "maximum packet size" refers to the
- // "maximum size of an individual data packet". ie. SSH_MSG_CHANNEL_DATA. RFC4254#section-5.2 corroborates.
- $packet = pack('CNNa*CNa*',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_EXEC], strlen('exec'), 'exec', 1, strlen($command), $command);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_REQUEST;
-
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
- if ($response === false) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_EXEC] = NET_SSH2_MSG_CHANNEL_DATA;
-
- if ($callback === false || $this->in_request_pty_exec) {
- return true;
- }
-
- $output = '';
- while (true) {
- $temp = $this->_get_channel_packet(NET_SSH2_CHANNEL_EXEC);
- switch (true) {
- case $temp === true:
- return is_callable($callback) ? true : $output;
- case $temp === false:
- return false;
- default:
- if (is_callable($callback)) {
- $callback($temp);
- } else {
- $output.= $temp;
- }
- }
- }
- }
-
- /**
- * Creates an interactive shell
- *
- * @see Net_SSH2::read()
- * @see Net_SSH2::write()
- * @return Boolean
- * @access private
- */
- function _initShell()
- {
- if ($this->in_request_pty_exec === true) {
- return true;
- }
-
- $this->window_size_server_to_client[NET_SSH2_CHANNEL_SHELL] = 0x7FFFFFFF;
- $packet_size = 0x4000;
-
- $packet = pack('CNa*N3',
- NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_SHELL, $this->window_size_server_to_client[NET_SSH2_CHANNEL_SHELL], $packet_size);
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_OPEN;
-
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
- if ($response === false) {
- return false;
- }
-
- $terminal_modes = pack('C', NET_SSH2_TTY_OP_END);
- $packet = pack('CNNa*CNa*N5a*',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SHELL], strlen('pty-req'), 'pty-req', 1, strlen('vt100'), 'vt100',
- 80, 24, 0, 0, strlen($terminal_modes), $terminal_modes);
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
-
- list(, $type) = unpack('C', $this->_string_shift($response, 1));
-
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_SUCCESS:
- // if a pty can't be opened maybe commands can still be executed
- case NET_SSH2_MSG_CHANNEL_FAILURE:
- break;
- default:
- user_error('Unable to request pseudo-terminal');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
-
- $packet = pack('CNNa*C',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SHELL], strlen('shell'), 'shell', 1);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_REQUEST;
-
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SHELL);
- if ($response === false) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_SHELL] = NET_SSH2_MSG_CHANNEL_DATA;
-
- $this->bitmap |= NET_SSH2_MASK_SHELL;
-
- return true;
- }
-
- /**
- * Return the channel to be used with read() / write()
- *
- * @see Net_SSH2::read()
- * @see Net_SSH2::write()
- * @return Integer
- * @access public
- */
- function _get_interactive_channel()
- {
- switch (true) {
- case $this->in_subsystem:
- return NET_SSH2_CHANNEL_SUBSYSTEM;
- case $this->in_request_pty_exec:
- return NET_SSH2_CHANNEL_EXEC;
- default:
- return NET_SSH2_CHANNEL_SHELL;
- }
- }
-
- /**
- * Returns the output of an interactive shell
- *
- * Returns when there's a match for $expect, which can take the form of a string literal or,
- * if $mode == NET_SSH2_READ_REGEX, a regular expression.
- *
- * @see Net_SSH2::write()
- * @param String $expect
- * @param Integer $mode
- * @return String
- * @access public
- */
- function read($expect = '', $mode = NET_SSH2_READ_SIMPLE)
- {
- $this->curTimeout = $this->timeout;
- $this->is_timeout = false;
-
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()');
- return false;
- }
-
- if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session');
- return false;
- }
-
- $channel = $this->_get_interactive_channel();
-
- $match = $expect;
- while (true) {
- if ($mode == NET_SSH2_READ_REGEX) {
- preg_match($expect, $this->interactiveBuffer, $matches);
- $match = isset($matches[0]) ? $matches[0] : '';
- }
- $pos = strlen($match) ? strpos($this->interactiveBuffer, $match) : false;
- if ($pos !== false) {
- return $this->_string_shift($this->interactiveBuffer, $pos + strlen($match));
- }
- $response = $this->_get_channel_packet($channel);
- if (is_bool($response)) {
- $this->in_request_pty_exec = false;
- return $response ? $this->_string_shift($this->interactiveBuffer, strlen($this->interactiveBuffer)) : false;
- }
-
- $this->interactiveBuffer.= $response;
- }
- }
-
- /**
- * Inputs a command into an interactive shell.
- *
- * @see Net_SSH2::read()
- * @param String $cmd
- * @return Boolean
- * @access public
- */
- function write($cmd)
- {
- if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- user_error('Operation disallowed prior to login()');
- return false;
- }
-
- if (!($this->bitmap & NET_SSH2_MASK_SHELL) && !$this->_initShell()) {
- user_error('Unable to initiate an interactive shell session');
- return false;
- }
-
- return $this->_send_channel_packet($this->_get_interactive_channel(), $cmd);
- }
-
- /**
- * Start a subsystem.
- *
- * Right now only one subsystem at a time is supported. To support multiple subsystem's stopSubsystem() could accept
- * a string that contained the name of the subsystem, but at that point, only one subsystem of each type could be opened.
- * To support multiple subsystem's of the same name maybe it'd be best if startSubsystem() generated a new channel id and
- * returns that and then that that was passed into stopSubsystem() but that'll be saved for a future date and implemented
- * if there's sufficient demand for such a feature.
- *
- * @see Net_SSH2::stopSubsystem()
- * @param String $subsystem
- * @return Boolean
- * @access public
- */
- function startSubsystem($subsystem)
- {
- $this->window_size_server_to_client[NET_SSH2_CHANNEL_SUBSYSTEM] = $this->window_size;
-
- $packet = pack('CNa*N3',
- NET_SSH2_MSG_CHANNEL_OPEN, strlen('session'), 'session', NET_SSH2_CHANNEL_SUBSYSTEM, $this->window_size, 0x4000);
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_OPEN;
-
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SUBSYSTEM);
- if ($response === false) {
- return false;
- }
-
- $packet = pack('CNNa*CNa*',
- NET_SSH2_MSG_CHANNEL_REQUEST, $this->server_channels[NET_SSH2_CHANNEL_SUBSYSTEM], strlen('subsystem'), 'subsystem', 1, strlen($subsystem), $subsystem);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_REQUEST;
-
- $response = $this->_get_channel_packet(NET_SSH2_CHANNEL_SUBSYSTEM);
-
- if ($response === false) {
- return false;
- }
-
- $this->channel_status[NET_SSH2_CHANNEL_SUBSYSTEM] = NET_SSH2_MSG_CHANNEL_DATA;
-
- $this->bitmap |= NET_SSH2_MASK_SHELL;
- $this->in_subsystem = true;
-
- return true;
- }
-
- /**
- * Stops a subsystem.
- *
- * @see Net_SSH2::startSubsystem()
- * @return Boolean
- * @access public
- */
- function stopSubsystem()
- {
- $this->in_subsystem = false;
- $this->_close_channel(NET_SSH2_CHANNEL_SUBSYSTEM);
- return true;
- }
-
- /**
- * Closes a channel
- *
- * If read() timed out you might want to just close the channel and have it auto-restart on the next read() call
- *
- * @access public
- */
- function reset()
- {
- $this->_close_channel($this->_get_interactive_channel());
- }
-
- /**
- * Is timeout?
- *
- * Did exec() or read() return because they timed out or because they encountered the end?
- *
- * @access public
- */
- function isTimeout()
- {
- return $this->is_timeout;
- }
-
- /**
- * Disconnect
- *
- * @access public
- */
- function disconnect()
- {
- $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- if (isset($this->realtime_log_file) && is_resource($this->realtime_log_file)) {
- fclose($this->realtime_log_file);
- }
- }
-
- /**
- * Destructor.
- *
- * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call
- * disconnect().
- *
- * @access public
- */
- function __destruct()
- {
- $this->disconnect();
- }
-
- /**
- * Is the connection still active?
- *
- * @access public
- */
- function isConnected()
- {
- return $this->bitmap & NET_SSH2_MASK_LOGIN;
- }
-
- /**
- * Gets Binary Packets
- *
- * See '6. Binary Packet Protocol' of rfc4253 for more info.
- *
- * @see Net_SSH2::_send_binary_packet()
- * @return String
- * @access private
- */
- function _get_binary_packet()
- {
- if (!is_resource($this->fsock) || feof($this->fsock)) {
- user_error('Connection closed prematurely');
- $this->bitmap = 0;
- return false;
- }
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
- $raw = fread($this->fsock, $this->decrypt_block_size);
-
- if (!strlen($raw)) {
- return '';
- }
-
- if ($this->decrypt !== false) {
- $raw = $this->decrypt->decrypt($raw);
- }
- if ($raw === false) {
- user_error('Unable to decrypt content');
- return false;
- }
-
- extract(unpack('Npacket_length/Cpadding_length', $this->_string_shift($raw, 5)));
-
- $remaining_length = $packet_length + 4 - $this->decrypt_block_size;
-
- // quoting ,
- // "implementations SHOULD check that the packet length is reasonable"
- // PuTTY uses 0x9000 as the actual max packet size and so to shall we
- if ($remaining_length < -$this->decrypt_block_size || $remaining_length > 0x9000 || $remaining_length % $this->decrypt_block_size != 0) {
- user_error('Invalid size');
- return false;
- }
-
- $buffer = '';
- while ($remaining_length > 0) {
- $temp = fread($this->fsock, $remaining_length);
- $buffer.= $temp;
- $remaining_length-= strlen($temp);
- }
- $stop = strtok(microtime(), ' ') + strtok('');
- if (strlen($buffer)) {
- $raw.= $this->decrypt !== false ? $this->decrypt->decrypt($buffer) : $buffer;
- }
-
- $payload = $this->_string_shift($raw, $packet_length - $padding_length - 1);
- $padding = $this->_string_shift($raw, $padding_length); // should leave $raw empty
-
- if ($this->hmac_check !== false) {
- $hmac = fread($this->fsock, $this->hmac_size);
- if ($hmac != $this->hmac_check->hash(pack('NNCa*', $this->get_seq_no, $packet_length, $padding_length, $payload . $padding))) {
- user_error('Invalid HMAC');
- return false;
- }
- }
-
- //if ($this->decompress) {
- // $payload = gzinflate(substr($payload, 2));
- //}
-
- $this->get_seq_no++;
-
- if (defined('NET_SSH2_LOGGING')) {
- $current = strtok(microtime(), ' ') + strtok('');
- $message_number = isset($this->message_numbers[ord($payload[0])]) ? $this->message_numbers[ord($payload[0])] : 'UNKNOWN (' . ord($payload[0]) . ')';
- $message_number = '<- ' . $message_number .
- ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
- $this->_append_log($message_number, $payload);
- $this->last_packet = $current;
- }
-
- return $this->_filter($payload);
- }
-
- /**
- * Filter Binary Packets
- *
- * Because some binary packets need to be ignored...
- *
- * @see Net_SSH2::_get_binary_packet()
- * @return String
- * @access private
- */
- function _filter($payload)
- {
- switch (ord($payload[0])) {
- case NET_SSH2_MSG_DISCONNECT:
- $this->_string_shift($payload, 1);
- extract(unpack('Nreason_code/Nlength', $this->_string_shift($payload, 8)));
- $this->errors[] = 'SSH_MSG_DISCONNECT: ' . $this->disconnect_reasons[$reason_code] . "\r\n" . utf8_decode($this->_string_shift($payload, $length));
- $this->bitmap = 0;
- return false;
- case NET_SSH2_MSG_IGNORE:
- $payload = $this->_get_binary_packet();
- break;
- case NET_SSH2_MSG_DEBUG:
- $this->_string_shift($payload, 2);
- extract(unpack('Nlength', $this->_string_shift($payload, 4)));
- $this->errors[] = 'SSH_MSG_DEBUG: ' . utf8_decode($this->_string_shift($payload, $length));
- $payload = $this->_get_binary_packet();
- break;
- case NET_SSH2_MSG_UNIMPLEMENTED:
- return false;
- case NET_SSH2_MSG_KEXINIT:
- if ($this->session_id !== false) {
- if (!$this->_key_exchange($payload)) {
- $this->bitmap = 0;
- return false;
- }
- $payload = $this->_get_binary_packet();
- }
- }
-
- // see http://tools.ietf.org/html/rfc4252#section-5.4; only called when the encryption has been activated and when we haven't already logged in
- if (($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) && !($this->bitmap & NET_SSH2_MASK_LOGIN) && ord($payload[0]) == NET_SSH2_MSG_USERAUTH_BANNER) {
- $this->_string_shift($payload, 1);
- extract(unpack('Nlength', $this->_string_shift($payload, 4)));
- $this->banner_message = utf8_decode($this->_string_shift($payload, $length));
- $payload = $this->_get_binary_packet();
- }
-
- // only called when we've already logged in
- if (($this->bitmap & NET_SSH2_MASK_CONSTRUCTOR) && ($this->bitmap & NET_SSH2_MASK_LOGIN)) {
- switch (ord($payload[0])) {
- case NET_SSH2_MSG_GLOBAL_REQUEST: // see http://tools.ietf.org/html/rfc4254#section-4
- $this->_string_shift($payload, 1);
- extract(unpack('Nlength', $this->_string_shift($payload)));
- $this->errors[] = 'SSH_MSG_GLOBAL_REQUEST: ' . utf8_decode($this->_string_shift($payload, $length));
-
- if (!$this->_send_binary_packet(pack('C', NET_SSH2_MSG_REQUEST_FAILURE))) {
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
-
- $payload = $this->_get_binary_packet();
- break;
- case NET_SSH2_MSG_CHANNEL_OPEN: // see http://tools.ietf.org/html/rfc4254#section-5.1
- $this->_string_shift($payload, 1);
- extract(unpack('Nlength', $this->_string_shift($payload, 4)));
- $this->errors[] = 'SSH_MSG_CHANNEL_OPEN: ' . utf8_decode($this->_string_shift($payload, $length));
-
- $this->_string_shift($payload, 4); // skip over client channel
- extract(unpack('Nserver_channel', $this->_string_shift($payload, 4)));
-
- $packet = pack('CN3a*Na*',
- 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);
- }
-
- $payload = $this->_get_binary_packet();
- break;
- case NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST:
- $this->_string_shift($payload, 1);
- extract(unpack('Nchannel', $this->_string_shift($payload, 4)));
- extract(unpack('Nwindow_size', $this->_string_shift($payload, 4)));
- $this->window_size_client_to_server[$channel]+= $window_size;
-
- $payload = ($this->bitmap & NET_SSH2_MASK_WINDOW_ADJUST) ? true : $this->_get_binary_packet();
- }
- }
-
- return $payload;
- }
-
- /**
- * Enable Quiet Mode
- *
- * Suppress stderr from output
- *
- * @access public
- */
- function enableQuietMode()
- {
- $this->quiet_mode = true;
- }
-
- /**
- * Disable Quiet Mode
- *
- * Show stderr in output
- *
- * @access public
- */
- function disableQuietMode()
- {
- $this->quiet_mode = false;
- }
-
- /**
- * Enable request-pty when using exec()
- *
- * @access public
- */
- function enablePTY()
- {
- $this->request_pty = true;
- }
-
- /**
- * Disable request-pty when using exec()
- *
- * @access public
- */
- function disablePTY()
- {
- $this->request_pty = false;
- }
-
- /**
- * Gets channel data
- *
- * Returns the data as a string if it's available and false if not.
- *
- * @param $client_channel
- * @return Mixed
- * @access private
- */
- function _get_channel_packet($client_channel, $skip_extended = false)
- {
- if (!empty($this->channel_buffers[$client_channel])) {
- return array_shift($this->channel_buffers[$client_channel]);
- }
-
- while (true) {
- if ($this->curTimeout) {
- if ($this->curTimeout < 0) {
- $this->is_timeout = true;
- return true;
- }
-
- $read = array($this->fsock);
- $write = $except = null;
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
- $sec = floor($this->curTimeout);
- $usec = 1000000 * ($this->curTimeout - $sec);
- // on windows this returns a "Warning: Invalid CRT parameters detected" error
- if (!@stream_select($read, $write, $except, $sec, $usec) && !count($read)) {
- $this->is_timeout = true;
- return true;
- }
- $elapsed = strtok(microtime(), ' ') + strtok('') - $start;
- $this->curTimeout-= $elapsed;
- }
-
- $response = $this->_get_binary_packet();
- if ($response === false) {
- user_error('Connection closed by server');
- return false;
- }
- if ($client_channel == -1 && $response === true) {
- return true;
- }
- if (!strlen($response)) {
- return '';
- }
-
- extract(unpack('Ctype/Nchannel', $this->_string_shift($response, 5)));
-
- $this->window_size_server_to_client[$channel]-= strlen($response) + 4;
-
- // resize the window, if appropriate
- if ($this->window_size_server_to_client[$channel] < 0) {
- $packet = pack('CNN', NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST, $this->server_channels[$channel], $this->window_size);
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- $this->window_size_server_to_client[$channel]+= $this->window_size;
- }
-
- switch ($this->channel_status[$channel]) {
- case NET_SSH2_MSG_CHANNEL_OPEN:
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
- extract(unpack('Nserver_channel', $this->_string_shift($response, 4)));
- $this->server_channels[$channel] = $server_channel;
- extract(unpack('Nwindow_size', $this->_string_shift($response, 4)));
- $this->window_size_client_to_server[$channel] = $window_size;
- $temp = unpack('Npacket_size_client_to_server', $this->_string_shift($response, 4));
- $this->packet_size_client_to_server[$channel] = $temp['packet_size_client_to_server'];
- return $client_channel == $channel ? true : $this->_get_channel_packet($client_channel, $skip_extended);
- //case NET_SSH2_MSG_CHANNEL_OPEN_FAILURE:
- default:
- user_error('Unable to open channel');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- break;
- case NET_SSH2_MSG_CHANNEL_REQUEST:
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_SUCCESS:
- return true;
- case NET_SSH2_MSG_CHANNEL_FAILURE:
- return false;
- default:
- user_error('Unable to fulfill channel request');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- case NET_SSH2_MSG_CHANNEL_CLOSE:
- return $type == NET_SSH2_MSG_CHANNEL_CLOSE ? true : $this->_get_channel_packet($client_channel, $skip_extended);
- }
-
- // ie. $this->channel_status[$channel] == NET_SSH2_MSG_CHANNEL_DATA
-
- switch ($type) {
- case NET_SSH2_MSG_CHANNEL_DATA:
- /*
- if ($channel == NET_SSH2_CHANNEL_EXEC) {
- // SCP requires null packets, such as this, be sent. further, in the case of the ssh.com SSH server
- // this actually seems to make things twice as fast. more to the point, the message right after
- // SSH_MSG_CHANNEL_DATA (usually SSH_MSG_IGNORE) won't block for as long as it would have otherwise.
- // in OpenSSH it slows things down but only by a couple thousandths of a second.
- $this->_send_channel_packet($channel, chr(0));
- }
- */
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $data = $this->_string_shift($response, $length);
- if ($client_channel == $channel) {
- return $data;
- }
- if (!isset($this->channel_buffers[$channel])) {
- $this->channel_buffers[$channel] = array();
- }
- $this->channel_buffers[$channel][] = $data;
- break;
- case NET_SSH2_MSG_CHANNEL_EXTENDED_DATA:
- /*
- if ($client_channel == NET_SSH2_CHANNEL_EXEC) {
- $this->_send_channel_packet($client_channel, chr(0));
- }
- */
- // currently, there's only one possible value for $data_type_code: NET_SSH2_EXTENDED_DATA_STDERR
- extract(unpack('Ndata_type_code/Nlength', $this->_string_shift($response, 8)));
- $data = $this->_string_shift($response, $length);
- $this->stdErrorLog .= $data;
- if ($skip_extended || $this->quiet_mode) {
- break;
- }
- if ($client_channel == $channel) {
- return $data;
- }
- if (!isset($this->channel_buffers[$channel])) {
- $this->channel_buffers[$channel] = array();
- }
- $this->channel_buffers[$channel][] = $data;
- break;
- case NET_SSH2_MSG_CHANNEL_REQUEST:
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $value = $this->_string_shift($response, $length);
- switch ($value) {
- case 'exit-signal':
- $this->_string_shift($response, 1);
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- $this->errors[] = 'SSH_MSG_CHANNEL_REQUEST (exit-signal): ' . $this->_string_shift($response, $length);
- $this->_string_shift($response, 1);
- extract(unpack('Nlength', $this->_string_shift($response, 4)));
- if ($length) {
- $this->errors[count($this->errors)].= "\r\n" . $this->_string_shift($response, $length);
- }
-
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel]));
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));
-
- $this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_EOF;
-
- break;
- case 'exit-status':
- extract(unpack('Cfalse/Nexit_status', $this->_string_shift($response, 5)));
- $this->exit_status = $exit_status;
-
- // "The client MAY ignore these messages."
- // -- http://tools.ietf.org/html/rfc4254#section-6.10
-
- break;
- default:
- // "Some systems may not implement signals, in which case they SHOULD ignore this message."
- // -- http://tools.ietf.org/html/rfc4254#section-6.9
- break;
- }
- break;
- case NET_SSH2_MSG_CHANNEL_CLOSE:
- $this->curTimeout = 0;
-
- if ($this->bitmap & NET_SSH2_MASK_SHELL) {
- $this->bitmap&= ~NET_SSH2_MASK_SHELL;
- }
- if ($this->channel_status[$channel] != NET_SSH2_MSG_CHANNEL_EOF) {
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$channel]));
- }
-
- $this->channel_status[$channel] = NET_SSH2_MSG_CHANNEL_CLOSE;
- return true;
- case NET_SSH2_MSG_CHANNEL_EOF:
- break;
- default:
- user_error('Error reading channel data');
- return $this->_disconnect(NET_SSH2_DISCONNECT_BY_APPLICATION);
- }
- }
- }
-
- /**
- * Sends Binary Packets
- *
- * See '6. Binary Packet Protocol' of rfc4253 for more info.
- *
- * @param String $data
- * @param optional String $logged
- * @see Net_SSH2::_get_binary_packet()
- * @return Boolean
- * @access private
- */
- function _send_binary_packet($data, $logged = null)
- {
- if (!is_resource($this->fsock) || feof($this->fsock)) {
- user_error('Connection closed prematurely');
- $this->bitmap = 0;
- return false;
- }
-
- //if ($this->compress) {
- // // the -4 removes the checksum:
- // // http://php.net/function.gzcompress#57710
- // $data = substr(gzcompress($data), 0, -4);
- //}
-
- // 4 (packet length) + 1 (padding length) + 4 (minimal padding amount) == 9
- $packet_length = strlen($data) + 9;
- // round up to the nearest $this->encrypt_block_size
- $packet_length+= (($this->encrypt_block_size - 1) * $packet_length) % $this->encrypt_block_size;
- // subtracting strlen($data) is obvious - subtracting 5 is necessary because of packet_length and padding_length
- $padding_length = $packet_length - strlen($data) - 5;
- $padding = crypt_random_string($padding_length);
-
- // we subtract 4 from packet_length because the packet_length field isn't supposed to include itself
- $packet = pack('NCa*', $packet_length - 4, $padding_length, $data . $padding);
-
- $hmac = $this->hmac_create !== false ? $this->hmac_create->hash(pack('Na*', $this->send_seq_no, $packet)) : '';
- $this->send_seq_no++;
-
- if ($this->encrypt !== false) {
- $packet = $this->encrypt->encrypt($packet);
- }
-
- $packet.= $hmac;
-
- $start = strtok(microtime(), ' ') + strtok(''); // http://php.net/microtime#61838
- $result = strlen($packet) == fputs($this->fsock, $packet);
- $stop = strtok(microtime(), ' ') + strtok('');
-
- if (defined('NET_SSH2_LOGGING')) {
- $current = strtok(microtime(), ' ') + strtok('');
- $message_number = isset($this->message_numbers[ord($data[0])]) ? $this->message_numbers[ord($data[0])] : 'UNKNOWN (' . ord($data[0]) . ')';
- $message_number = '-> ' . $message_number .
- ' (since last: ' . round($current - $this->last_packet, 4) . ', network: ' . round($stop - $start, 4) . 's)';
- $this->_append_log($message_number, isset($logged) ? $logged : $data);
- $this->last_packet = $current;
- }
-
- return $result;
- }
-
- /**
- * Logs data packets
- *
- * Makes sure that only the last 1MB worth of packets will be logged
- *
- * @param String $data
- * @access private
- */
- function _append_log($message_number, $message)
- {
- // remove the byte identifying the message type from all but the first two messages (ie. the identification strings)
- if (strlen($message_number) > 2) {
- $this->_string_shift($message);
- }
-
- switch (NET_SSH2_LOGGING) {
- // useful for benchmarks
- case NET_SSH2_LOG_SIMPLE:
- $this->message_number_log[] = $message_number;
- break;
- // the most useful log for SSH2
- case NET_SSH2_LOG_COMPLEX:
- $this->message_number_log[] = $message_number;
- $this->log_size+= strlen($message);
- $this->message_log[] = $message;
- while ($this->log_size > NET_SSH2_LOG_MAX_SIZE) {
- $this->log_size-= strlen(array_shift($this->message_log));
- array_shift($this->message_number_log);
- }
- break;
- // dump the output out realtime; packets may be interspersed with non packets,
- // passwords won't be filtered out and select other packets may not be correctly
- // identified
- case NET_SSH2_LOG_REALTIME:
- switch (PHP_SAPI) {
- case 'cli':
- $start = $stop = "\r\n";
- break;
- default:
- $start = '';
- $stop = '
';
- }
- echo $start . $this->_format_log(array($message), array($message_number)) . $stop;
- @flush();
- @ob_flush();
- break;
- // basically the same thing as NET_SSH2_LOG_REALTIME with the caveat that NET_SSH2_LOG_REALTIME_FILE
- // needs to be defined and that the resultant log file will be capped out at NET_SSH2_LOG_MAX_SIZE.
- // the earliest part of the log file is denoted by the first <<< START >>> and is not going to necessarily
- // at the beginning of the file
- case NET_SSH2_LOG_REALTIME_FILE:
- if (!isset($this->realtime_log_file)) {
- // PHP doesn't seem to like using constants in fopen()
- $filename = NET_SSH2_LOG_REALTIME_FILENAME;
- $fp = fopen($filename, 'w');
- $this->realtime_log_file = $fp;
- }
- if (!is_resource($this->realtime_log_file)) {
- break;
- }
- $entry = $this->_format_log(array($message), array($message_number));
- if ($this->realtime_log_wrap) {
- $temp = "<<< START >>>\r\n";
- $entry.= $temp;
- fseek($this->realtime_log_file, ftell($this->realtime_log_file) - strlen($temp));
- }
- $this->realtime_log_size+= strlen($entry);
- if ($this->realtime_log_size > NET_SSH2_LOG_MAX_SIZE) {
- fseek($this->realtime_log_file, 0);
- $this->realtime_log_size = strlen($entry);
- $this->realtime_log_wrap = true;
- }
- fputs($this->realtime_log_file, $entry);
- }
- }
-
- /**
- * Sends channel data
- *
- * Spans multiple SSH_MSG_CHANNEL_DATAs if appropriate
- *
- * @param Integer $client_channel
- * @param String $data
- * @return Boolean
- * @access private
- */
- function _send_channel_packet($client_channel, $data)
- {
- /* The maximum amount of data allowed is determined by the maximum
- packet size for the channel, and the current window size, whichever
- is smaller.
-
- -- http://tools.ietf.org/html/rfc4254#section-5.2 */
- $max_size = min(
- $this->packet_size_client_to_server[$client_channel],
- $this->window_size_client_to_server[$client_channel]
- ) - 4;
- while (strlen($data) > $max_size) {
- if (!$this->window_size_client_to_server[$client_channel]) {
- $this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST;
- // using an invalid channel will let the buffers be built up for the valid channels
- $output = $this->_get_channel_packet(-1);
- $this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST;
- $max_size = min(
- $this->packet_size_client_to_server[$client_channel],
- $this->window_size_client_to_server[$client_channel]
- ) - 4;
- }
-
- $packet = pack('CN2a*',
- NET_SSH2_MSG_CHANNEL_DATA,
- $this->server_channels[$client_channel],
- $max_size,
- $this->_string_shift($data, $max_size)
- );
-
- $this->window_size_client_to_server[$client_channel]-= $max_size + 4;
-
- if (!$this->_send_binary_packet($packet)) {
- return false;
- }
- }
-
- if (strlen($data) >= $this->window_size_client_to_server[$client_channel] - 4) {
- $this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST;
- $this->_get_channel_packet(-1);
- $this->bitmap^= NET_SSH2_MASK_WINDOW_ADJUST;
- }
-
- $this->window_size_client_to_server[$client_channel]-= strlen($data) + 4;
-
- return $this->_send_binary_packet(pack('CN2a*',
- NET_SSH2_MSG_CHANNEL_DATA,
- $this->server_channels[$client_channel],
- strlen($data),
- $data));
- }
-
- /**
- * Closes and flushes a channel
- *
- * Net_SSH2 doesn't properly close most channels. For exec() channels are normally closed by the server
- * and for SFTP channels are presumably closed when the client disconnects. This functions is intended
- * for SCP more than anything.
- *
- * @param Integer $client_channel
- * @param Boolean $want_reply
- * @return Boolean
- * @access private
- */
- function _close_channel($client_channel, $want_reply = false)
- {
- // see http://tools.ietf.org/html/rfc4254#section-5.3
-
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_EOF, $this->server_channels[$client_channel]));
-
- if (!$want_reply) {
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel]));
- }
-
- $this->channel_status[$client_channel] = NET_SSH2_MSG_CHANNEL_CLOSE;
-
- $this->curTimeout = 0;
-
- while (!is_bool($this->_get_channel_packet($client_channel)));
-
- if ($want_reply) {
- $this->_send_binary_packet(pack('CN', NET_SSH2_MSG_CHANNEL_CLOSE, $this->server_channels[$client_channel]));
- }
-
- if ($this->bitmap & NET_SSH2_MASK_SHELL) {
- $this->bitmap&= ~NET_SSH2_MASK_SHELL;
- }
- }
-
- /**
- * Disconnect
- *
- * @param Integer $reason
- * @return Boolean
- * @access private
- */
- function _disconnect($reason)
- {
- if ($this->bitmap) {
- $data = pack('CNNa*Na*', NET_SSH2_MSG_DISCONNECT, $reason, 0, '', 0, '');
- $this->_send_binary_packet($data);
- $this->bitmap = 0;
- fclose($this->fsock);
- return false;
- }
- }
-
- /**
- * String Shift
- *
- * Inspired by array_shift
- *
- * @param String $string
- * @param optional Integer $index
- * @return String
- * @access private
- */
- function _string_shift(&$string, $index = 1)
- {
- $substr = substr($string, 0, $index);
- $string = substr($string, $index);
- return $substr;
- }
-
- /**
- * Define Array
- *
- * Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of
- * named constants from it, using the value as the name of the constant and the index as the value of the constant.
- * If any of the constants that would be defined already exists, none of the constants will be defined.
- *
- * @param Array $array
- * @access private
- */
- function _define_array()
- {
- $args = func_get_args();
- foreach ($args as $arg) {
- foreach ($arg as $key=>$value) {
- if (!defined($value)) {
- define($value, $key);
- } else {
- break 2;
- }
- }
- }
- }
-
- /**
- * Returns a log of the packets that have been sent and received.
- *
- * Returns a string if NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX, an array if NET_SSH2_LOGGING == NET_SSH2_LOG_SIMPLE and false if !defined('NET_SSH2_LOGGING')
- *
- * @access public
- * @return String or Array
- */
- function getLog()
- {
- if (!defined('NET_SSH2_LOGGING')) {
- return false;
- }
-
- switch (NET_SSH2_LOGGING) {
- case NET_SSH2_LOG_SIMPLE:
- return $this->message_number_log;
- break;
- case NET_SSH2_LOG_COMPLEX:
- return $this->_format_log($this->message_log, $this->message_number_log);
- break;
- default:
- return false;
- }
- }
-
- /**
- * Formats a log for printing
- *
- * @param Array $message_log
- * @param Array $message_number_log
- * @access private
- * @return String
- */
- function _format_log($message_log, $message_number_log)
- {
- $output = '';
- for ($i = 0; $i < count($message_log); $i++) {
- $output.= $message_number_log[$i] . "\r\n";
- $current_log = $message_log[$i];
- $j = 0;
- do {
- if (strlen($current_log)) {
- $output.= str_pad(dechex($j), 7, '0', STR_PAD_LEFT) . '0 ';
- }
- $fragment = $this->_string_shift($current_log, $this->log_short_width);
- $hex = substr(preg_replace_callback('#.#s', array($this, '_format_log_helper'), $fragment), strlen($this->log_boundary));
- // replace non ASCII printable characters with dots
- // http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters
- // also replace < with a . since < messes up the output on web browsers
- $raw = preg_replace('#[^\x20-\x7E]|<#', '.', $fragment);
- $output.= str_pad($hex, $this->log_long_width - $this->log_short_width, ' ') . $raw . "\r\n";
- $j++;
- } while (strlen($current_log));
- $output.= "\r\n";
- }
-
- return $output;
- }
-
- /**
- * Helper function for _format_log
- *
- * For use with preg_replace_callback()
- *
- * @param Array $matches
- * @access private
- * @return String
- */
- function _format_log_helper($matches)
- {
- return $this->log_boundary . str_pad(dechex(ord($matches[0])), 2, '0', STR_PAD_LEFT);
- }
-
- /**
- * Returns all errors
- *
- * @return String
- * @access public
- */
- function getErrors()
- {
- return $this->errors;
- }
-
- /**
- * Returns the last error
- *
- * @return String
- * @access public
- */
- function getLastError()
- {
- return $this->errors[count($this->errors) - 1];
- }
-
- /**
- * Return the server identification.
- *
- * @return String
- * @access public
- */
- function getServerIdentification()
- {
- return $this->server_identifier;
- }
-
- /**
- * Return a list of the key exchange algorithms the server supports.
- *
- * @return Array
- * @access public
- */
- function getKexAlgorithms()
- {
- return $this->kex_algorithms;
- }
-
- /**
- * Return a list of the host key (public key) algorithms the server supports.
- *
- * @return Array
- * @access public
- */
- function getServerHostKeyAlgorithms()
- {
- return $this->server_host_key_algorithms;
- }
-
- /**
- * Return a list of the (symmetric key) encryption algorithms the server supports, when receiving stuff from the client.
- *
- * @return Array
- * @access public
- */
- function getEncryptionAlgorithmsClient2Server()
- {
- return $this->encryption_algorithms_client_to_server;
- }
-
- /**
- * Return a list of the (symmetric key) encryption algorithms the server supports, when sending stuff to the client.
- *
- * @return Array
- * @access public
- */
- function getEncryptionAlgorithmsServer2Client()
- {
- return $this->encryption_algorithms_server_to_client;
- }
-
- /**
- * Return a list of the MAC algorithms the server supports, when receiving stuff from the client.
- *
- * @return Array
- * @access public
- */
- function getMACAlgorithmsClient2Server()
- {
- return $this->mac_algorithms_client_to_server;
- }
-
- /**
- * Return a list of the MAC algorithms the server supports, when sending stuff to the client.
- *
- * @return Array
- * @access public
- */
- function getMACAlgorithmsServer2Client()
- {
- return $this->mac_algorithms_server_to_client;
- }
-
- /**
- * Return a list of the compression algorithms the server supports, when receiving stuff from the client.
- *
- * @return Array
- * @access public
- */
- function getCompressionAlgorithmsClient2Server()
- {
- return $this->compression_algorithms_client_to_server;
- }
-
- /**
- * Return a list of the compression algorithms the server supports, when sending stuff to the client.
- *
- * @return Array
- * @access public
- */
- function getCompressionAlgorithmsServer2Client()
- {
- return $this->compression_algorithms_server_to_client;
- }
-
- /**
- * Return a list of the languages the server supports, when sending stuff to the client.
- *
- * @return Array
- * @access public
- */
- function getLanguagesServer2Client()
- {
- return $this->languages_server_to_client;
- }
-
- /**
- * Return a list of the languages the server supports, when receiving stuff from the client.
- *
- * @return Array
- * @access public
- */
- function getLanguagesClient2Server()
- {
- return $this->languages_client_to_server;
- }
-
- /**
- * Returns the banner message.
- *
- * Quoting from the RFC, "in some jurisdictions, sending a warning message before
- * authentication may be relevant for getting legal protection."
- *
- * @return String
- * @access public
- */
- function getBannerMessage()
- {
- return $this->banner_message;
- }
-
- /**
- * Returns the server public host key.
- *
- * Caching this the first time you connect to a server and checking the result on subsequent connections
- * is recommended. Returns false if the server signature is not signed correctly with the public host key.
- *
- * @return Mixed
- * @access public
- */
- function getServerPublicHostKey()
- {
- $signature = $this->signature;
- $server_public_host_key = $this->server_public_host_key;
-
- extract(unpack('Nlength', $this->_string_shift($server_public_host_key, 4)));
- $this->_string_shift($server_public_host_key, $length);
-
- if ($this->signature_validated) {
- return $this->bitmap ?
- $this->signature_format . ' ' . base64_encode($this->server_public_host_key) :
- false;
- }
-
- $this->signature_validated = true;
-
- switch ($this->signature_format) {
- case 'ssh-dss':
- $zero = new Math_BigInteger();
-
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $p = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
-
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $q = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
-
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $g = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
-
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $y = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
-
- /* The value for 'dss_signature_blob' is encoded as a string containing
- r, followed by s (which are 160-bit integers, without lengths or
- padding, unsigned, and in network byte order). */
- $temp = unpack('Nlength', $this->_string_shift($signature, 4));
- if ($temp['length'] != 40) {
- user_error('Invalid signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- $r = new Math_BigInteger($this->_string_shift($signature, 20), 256);
- $s = new Math_BigInteger($this->_string_shift($signature, 20), 256);
-
- switch (true) {
- case $r->equals($zero):
- case $r->compare($q) >= 0:
- case $s->equals($zero):
- case $s->compare($q) >= 0:
- user_error('Invalid signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- $w = $s->modInverse($q);
-
- $u1 = $w->multiply(new Math_BigInteger(sha1($this->exchange_hash), 16));
- list(, $u1) = $u1->divide($q);
-
- $u2 = $w->multiply($r);
- list(, $u2) = $u2->divide($q);
-
- $g = $g->modPow($u1, $p);
- $y = $y->modPow($u2, $p);
-
- $v = $g->multiply($y);
- list(, $v) = $v->divide($p);
- list(, $v) = $v->divide($q);
-
- if (!$v->equals($r)) {
- user_error('Bad server signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
- }
-
- break;
- case 'ssh-rsa':
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $e = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
-
- $temp = unpack('Nlength', $this->_string_shift($server_public_host_key, 4));
- $n = new Math_BigInteger($this->_string_shift($server_public_host_key, $temp['length']), -256);
- $nLength = $temp['length'];
-
- /*
- $temp = unpack('Nlength', $this->_string_shift($signature, 4));
- $signature = $this->_string_shift($signature, $temp['length']);
-
- if (!class_exists('Crypt_RSA')) {
- include_once 'Crypt/RSA.php';
- }
-
- $rsa = new Crypt_RSA();
- $rsa->setSignatureMode(CRYPT_RSA_SIGNATURE_PKCS1);
- $rsa->loadKey(array('e' => $e, 'n' => $n), CRYPT_RSA_PUBLIC_FORMAT_RAW);
- if (!$rsa->verify($this->exchange_hash, $signature)) {
- user_error('Bad server signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
- }
- */
-
- $temp = unpack('Nlength', $this->_string_shift($signature, 4));
- $s = new Math_BigInteger($this->_string_shift($signature, $temp['length']), 256);
-
- // validate an RSA signature per "8.2 RSASSA-PKCS1-v1_5", "5.2.2 RSAVP1", and "9.1 EMSA-PSS" in the
- // following URL:
- // ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.pdf
-
- // also, see SSHRSA.c (rsa2_verifysig) in PuTTy's source.
-
- if ($s->compare(new Math_BigInteger()) < 0 || $s->compare($n->subtract(new Math_BigInteger(1))) > 0) {
- user_error('Invalid signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
- }
-
- $s = $s->modPow($e, $n);
- $s = $s->toBytes();
-
- $h = pack('N4H*', 0x00302130, 0x0906052B, 0x0E03021A, 0x05000414, sha1($this->exchange_hash));
- $h = chr(0x01) . str_repeat(chr(0xFF), $nLength - 3 - strlen($h)) . $h;
-
- if ($s != $h) {
- user_error('Bad server signature');
- return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
- }
- break;
- default:
- user_error('Unsupported signature format');
- return $this->_disconnect(NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE);
- }
-
- return $this->signature_format . ' ' . base64_encode($this->server_public_host_key);
- }
-
- /**
- * Returns the exit status of an SSH command or false.
- *
- * @return Integer or false
- * @access public
- */
- function getExitStatus()
- {
- if (is_null($this->exit_status)) {
- return false;
- }
- return $this->exit_status;
- }
-
- /**
- * Is a path includable?
- *
- * @return Boolean
- * @access private
- */
- function _is_includable($suffix)
- {
- // stream_resolve_include_path was introduced in PHP 5.3.2
- if (function_exists('stream_resolve_include_path')) {
- return stream_resolve_include_path($suffix) !== false;
- }
-
- $paths = PATH_SEPARATOR == ':' ?
- preg_split('#(?
- * login('username', $agent)) {
- * exit('Login Failed');
- * }
- *
- * echo $ssh->exec('pwd');
- * echo $ssh->exec('ls -la');
- * ?>
- *
- *
- * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * @category System
- * @package System_SSH_Agent
- * @author Jim Wigginton
- * @copyright MMXIV Jim Wigginton
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
- * @link http://phpseclib.sourceforge.net
- * @internal See http://api.libssh.org/rfc/PROTOCOL.agent
- */
-
-/**#@+
- * Message numbers
- *
- * @access private
- */
-// to request SSH1 keys you have to use SSH_AGENTC_REQUEST_RSA_IDENTITIES (1)
-define('SYSTEM_SSH_AGENTC_REQUEST_IDENTITIES', 11);
-// this is the SSH2 response; the SSH1 response is SSH_AGENT_RSA_IDENTITIES_ANSWER (2).
-define('SYSTEM_SSH_AGENT_IDENTITIES_ANSWER', 12);
-define('SYSTEM_SSH_AGENT_FAILURE', 5);
-// the SSH1 request is SSH_AGENTC_RSA_CHALLENGE (3)
-define('SYSTEM_SSH_AGENTC_SIGN_REQUEST', 13);
-// the SSH1 response is SSH_AGENT_RSA_RESPONSE (4)
-define('SYSTEM_SSH_AGENT_SIGN_RESPONSE', 14);
-/**#@-*/
-
-/**
- * Pure-PHP ssh-agent client identity object
- *
- * Instantiation should only be performed by System_SSH_Agent class.
- * This could be thought of as implementing an interface that Crypt_RSA
- * implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something.
- * The methods in this interface would be getPublicKey, setSignatureMode
- * and sign since those are the methods phpseclib looks for to perform
- * public key authentication.
- *
- * @package System_SSH_Agent
- * @author Jim Wigginton
- * @version 0.1.0
- * @access internal
- */
-class System_SSH_Agent_Identity
-{
- /**
- * Key Object
- *
- * @var Crypt_RSA
- * @access private
- * @see System_SSH_Agent_Identity::getPublicKey()
- */
- var $key;
-
- /**
- * Key Blob
- *
- * @var String
- * @access private
- * @see System_SSH_Agent_Identity::sign()
- */
- var $key_blob;
-
- /**
- * Socket Resource
- *
- * @var Resource
- * @access private
- * @see System_SSH_Agent_Identity::sign()
- */
- var $fsock;
-
- /**
- * Default Constructor.
- *
- * @param Resource $fsock
- * @return System_SSH_Agent_Identity
- * @access private
- */
- function System_SSH_Agent_Identity($fsock)
- {
- $this->fsock = $fsock;
- }
-
- /**
- * Set Public Key
- *
- * Called by System_SSH_Agent::requestIdentities()
- *
- * @param Crypt_RSA $key
- * @access private
- */
- function setPublicKey($key)
- {
- $this->key = $key;
- $this->key->setPublicKey();
- }
-
- /**
- * Set Public Key
- *
- * Called by System_SSH_Agent::requestIdentities(). The key blob could be extracted from $this->key
- * but this saves a small amount of computation.
- *
- * @param String $key_blob
- * @access private
- */
- function setPublicKeyBlob($key_blob)
- {
- $this->key_blob = $key_blob;
- }
-
- /**
- * Get Public Key
- *
- * Wrapper for $this->key->getPublicKey()
- *
- * @param Integer $format optional
- * @return Mixed
- * @access public
- */
- function getPublicKey($format = null)
- {
- return !isset($format) ? $this->key->getPublicKey() : $this->key->getPublicKey($format);
- }
-
- /**
- * Set Signature Mode
- *
- * Doesn't do anything as ssh-agent doesn't let you pick and choose the signature mode. ie.
- * ssh-agent's only supported mode is CRYPT_RSA_SIGNATURE_PKCS1
- *
- * @param Integer $mode
- * @access public
- */
- function setSignatureMode($mode)
- {
- }
-
- /**
- * Create a signature
- *
- * See "2.6.2 Protocol 2 private key signature request"
- *
- * @param String $message
- * @return String
- * @access public
- */
- function sign($message)
- {
- // the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE
- $packet = pack('CNa*Na*N', SYSTEM_SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, 0);
- $packet = pack('Na*', strlen($packet), $packet);
- if (strlen($packet) != fputs($this->fsock, $packet)) {
- user_error('Connection closed during signing');
- }
-
- $length = current(unpack('N', fread($this->fsock, 4)));
- $type = ord(fread($this->fsock, 1));
- if ($type != SYSTEM_SSH_AGENT_SIGN_RESPONSE) {
- user_error('Unable to retreive signature');
- }
-
- $signature_blob = fread($this->fsock, $length - 1);
- // the only other signature format defined - ssh-dss - is the same length as ssh-rsa
- // the + 12 is for the other various SSH added length fields
- return substr($signature_blob, strlen('ssh-rsa') + 12);
- }
-}
-
-/**
- * Pure-PHP ssh-agent client identity factory
- *
- * requestIdentities() method pumps out System_SSH_Agent_Identity objects
- *
- * @package System_SSH_Agent
- * @author Jim Wigginton
- * @version 0.1.0
- * @access internal
- */
-class System_SSH_Agent
-{
- /**
- * Socket Resource
- *
- * @var Resource
- * @access private
- */
- var $fsock;
-
- /**
- * Default Constructor
- *
- * @return System_SSH_Agent
- * @access public
- */
- function System_SSH_Agent()
- {
- switch (true) {
- case isset($_SERVER['SSH_AUTH_SOCK']):
- $address = $_SERVER['SSH_AUTH_SOCK'];
- break;
- case isset($_ENV['SSH_AUTH_SOCK']):
- $address = $_ENV['SSH_AUTH_SOCK'];
- break;
- default:
- user_error('SSH_AUTH_SOCK not found');
- return false;
- }
-
- $this->fsock = fsockopen('unix://' . $address, 0, $errno, $errstr);
- if (!$this->fsock) {
- user_error("Unable to connect to ssh-agent (Error $errno: $errstr)");
- }
- }
-
- /**
- * Request Identities
- *
- * See "2.5.2 Requesting a list of protocol 2 keys"
- * Returns an array containing zero or more System_SSH_Agent_Identity objects
- *
- * @return Array
- * @access public
- */
- function requestIdentities()
- {
- if (!$this->fsock) {
- return array();
- }
-
- $packet = pack('NC', 1, SYSTEM_SSH_AGENTC_REQUEST_IDENTITIES);
- if (strlen($packet) != fputs($this->fsock, $packet)) {
- user_error('Connection closed while requesting identities');
- }
-
- $length = current(unpack('N', fread($this->fsock, 4)));
- $type = ord(fread($this->fsock, 1));
- if ($type != SYSTEM_SSH_AGENT_IDENTITIES_ANSWER) {
- user_error('Unable to request identities');
- }
-
- $identities = array();
- $keyCount = current(unpack('N', fread($this->fsock, 4)));
- for ($i = 0; $i < $keyCount; $i++) {
- $length = current(unpack('N', fread($this->fsock, 4)));
- $key_blob = fread($this->fsock, $length);
- $length = current(unpack('N', fread($this->fsock, 4)));
- $key_comment = fread($this->fsock, $length);
- $length = current(unpack('N', substr($key_blob, 0, 4)));
- $key_type = substr($key_blob, 4, $length);
- switch ($key_type) {
- case 'ssh-rsa':
- if (!class_exists('Crypt_RSA')) {
- include_once 'Crypt/RSA.php';
- }
- $key = new Crypt_RSA();
- $key->loadKey('ssh-rsa ' . base64_encode($key_blob) . ' ' . $key_comment);
- break;
- case 'ssh-dss':
- // not currently supported
- break;
- }
- // resources are passed by reference by default
- if (isset($key)) {
- $identity = new System_SSH_Agent_Identity($this->fsock);
- $identity->setPublicKey($key);
- $identity->setPublicKeyBlob($key_blob);
- $identities[] = $identity;
- unset($key);
- }
- }
-
- return $identities;
- }
-}
diff --git a/apps/files_external/3rdparty/phpseclib/phpseclib/openssl.cnf b/apps/files_external/3rdparty/phpseclib/phpseclib/openssl.cnf
deleted file mode 100644
index 2b8b52f9f7..0000000000
--- a/apps/files_external/3rdparty/phpseclib/phpseclib/openssl.cnf
+++ /dev/null
@@ -1,6 +0,0 @@
-# minimalist openssl.cnf file for use with phpseclib
-
-HOME = .
-RANDFILE = $ENV::HOME/.rnd
-
-[ v3_ca ]
diff --git a/apps/files_external/3rdparty/phpseclib/phpunit.xml.dist b/apps/files_external/3rdparty/phpseclib/phpunit.xml.dist
deleted file mode 100644
index f579ab4fd2..0000000000
--- a/apps/files_external/3rdparty/phpseclib/phpunit.xml.dist
+++ /dev/null
@@ -1,18 +0,0 @@
-
-
-
-
-
- ./tests/
-
-
-
-
-
-
- ./phpseclib/
-
-
-
diff --git a/apps/files_external/lib/config.php b/apps/files_external/lib/config.php
index 21f63bf439..28e28ffcde 100755
--- a/apps/files_external/lib/config.php
+++ b/apps/files_external/lib/config.php
@@ -21,11 +21,6 @@
* License along with this library. If not, see .
*/
-set_include_path(
- get_include_path() . PATH_SEPARATOR .
- \OC_App::getAppPath('files_external') . '/3rdparty/phpseclib/phpseclib'
-);
-
/**
* Class to configure mount.json globally and for users
*/
diff --git a/apps/files_external/lib/sftp.php b/apps/files_external/lib/sftp.php
index a0816e2870..52cac9bb64 100644
--- a/apps/files_external/lib/sftp.php
+++ b/apps/files_external/lib/sftp.php
@@ -7,11 +7,6 @@
*/
namespace OC\Files\Storage;
-set_include_path(get_include_path() . PATH_SEPARATOR .
- \OC_App::getAppPath('files_external') . '/3rdparty/phpseclib/phpseclib');
-require 'Net/SFTP.php';
-require 'Net/SFTP/Stream.php';
-
class SFTP extends \OC\Files\Storage\Common {
private $host;
private $user;