diff --git a/wp-admin/includes/class-wp-filesystem-ssh2.php b/wp-admin/includes/class-wp-filesystem-ssh2.php index 085664e38c..2997cb702d 100644 --- a/wp-admin/includes/class-wp-filesystem-ssh2.php +++ b/wp-admin/includes/class-wp-filesystem-ssh2.php @@ -41,10 +41,11 @@ */ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { - var $debugtest = false; // this is my var that will output the text when debuggin this class + var $debugtest = false; // set this to true only if your a debuging your connection var $link = null; var $sftp_link = null; + var $keys = false; /* * This is the timeout value for ssh results to comeback. * Slower servers might need this incressed, but this number otherwise should not change. @@ -88,24 +89,46 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { else $this->options['username'] = $opt['username']; + if (( !empty ($opt['public_key']) ) && ( !empty ($opt['private_key']) )) { + $this->options['public_key'] = $opt['public_key']; + $this->options['private_key'] = $opt['private_key']; + + $this->options['hostkey'] = array("hostkey" => "ssh-rsa"); + + $this->keys = true; + } + + if ( empty ($opt['password']) ) - $this->errors->add('empty_password', __('SSH2 password is required')); + if ( !$this->keys ) // password can be blank if we are using keys + $this->errors->add('empty_password', __('SSH2 password is required')); else $this->options['password'] = $opt['password']; + } function connect() { $this->debug("connect();"); - $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']); - + if ( ! $this->keys ) + $this->link = @ssh2_connect($this->options['hostname'], $this->options['port']); + else + $this->link = @ssh2_connect($this->options['hostname'], $this->options['port'], $this->options['hostkey']); + if ( ! $this->link ) { $this->errors->add('connect', sprintf(__('Failed to connect to SSH2 Server %1$s:%2$s'), $this->options['hostname'], $this->options['port'])); return false; } - if ( ! @ssh2_auth_password($this->link,$this->options['username'], $this->options['password']) ) { - $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username'])); - return false; + if ( !$this->keys ) { + if ( ! @ssh2_auth_password($this->link, $this->options['username'], $this->options['password']) ) { + $this->errors->add('auth', sprintf(__('Username/Password incorrect for %s'), $this->options['username'])); + return false; + } + } else { + if ( ! @ssh2_auth_pubkey_file($this->link, $this->options['username'], $this->options['public_key'], $this->options['private_key'], $this->options['password'] ) ) { + $this->errors->add('auth', sprintf(__('Public and Private keys incorrent for %s'), $this->options['username'])); + return false; + } } $this->sftp_link = ssh2_sftp($this->link); @@ -114,7 +137,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function run_command($link, $command, $returnbool = false) { - //$this->debug("run_command(".$command.",".$returnbool.");"); + $this->debug("run_command();"); if(!($stream = @ssh2_exec( $link, $command . "; echo \"__COMMAND_FINISHED__\";"))) { $this->errors->add('command', sprintf(__('Unable to perform command: %s'), $command)); } else { @@ -136,15 +159,11 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } fclose($stream); $data = str_replace("__COMMAND_FINISHED__", "", $data); - //$this->debug("run_command(".$command."); --> \$data = " . $data); if (($returnbool) && ( (int) $data )) { - $this->debug("Data. Returning: True"); return true; } elseif (($returnbool) && (! (int) $data )) { - $this->debug("Data. Returning: False"); return false; } else { - $this->debug("Data Only."); return $data; } } @@ -166,6 +185,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function get_contents($file, $type = '', $resumepos = 0 ) { + $this->debug("get_contents();"); $tempfile = wp_tempnam( $file ); if ( ! $tempfile ) return false; @@ -182,6 +202,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function put_contents($file, $contents, $type = '' ) { + $this->debug("put_contents();"); $tempfile = wp_tempnam( $file ); $temp = fopen($tempfile, 'w'); if ( ! $temp ) @@ -194,6 +215,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function cwd() { + $this->debug("cwd();"); $cwd = $this->run_command($this->link, 'pwd'); if( $cwd ) $cwd = trailingslashit($cwd); @@ -201,6 +223,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function chdir($dir) { + $this->debug("chdir();"); return $this->run_command($this->link, 'cd ' . $dir, true); } @@ -269,6 +292,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function delete($file, $recursive = false) { + $this->debug("delete();"); if ( $this->is_file($file) ) return ssh2_sftp_unlink($this->sftp_link, $file); if ( ! $recursive ) @@ -283,11 +307,13 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function exists($file) { + $this->debug("exists();"); $list = $this->run_command($this->link, sprintf('ls -lad %s', $file)); return (bool) $list; } function is_file($file) { + $this->debug("is_file();"); //DO NOT RELY ON dirlist()! $list = $this->run_command($this->link, sprintf('ls -lad %s', $file)); $list = $this->parselisting($list); @@ -298,6 +324,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function is_dir($path) { + $this->debug("is_dir();"); //DO NOT RELY ON dirlist()! $list = $this->parselisting($this->run_command($this->link, sprintf('ls -lad %s', rtrim($path, '/')))); if ( ! $list ) @@ -329,8 +356,10 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { function touch($file, $time = 0, $atime = 0) { //Not implmented. } - + function mkdir($path, $chmod = null, $chown = false, $chgrp = false) { + $this->debug("mkdir();"); + $path = trim($path, '/'); if( ! ssh2_sftp_mkdir($this->sftp_link, $path, $chmod, true) ) return false; if( $chown ) @@ -341,6 +370,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } function rmdir($path, $recursive = false) { + $this->debug("rmdir();"); return $this->delete($path, $recursive); } @@ -463,8 +493,8 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } return $ret; } - - function __destruct(){ + function __destruct() { + $this->debug("__destruct();"); if ( $this->link ) unset($this->link); if ( $this->sftp_link ) diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index 3694f8c414..906a1db210 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -405,7 +405,7 @@ function unzip_file($file, $to) { for ( $i = count($path) - 1; $i >= 0; $i-- ) { //>=0 as the first element contains data, count()-1, as we do not want the file component $tmppath = $to . implode('/', array_slice($path, 0, $i) ); if ( $fs->is_dir($tmppath) ) {//Found the highest folder that exists, Create from here - for ( $i = $i + 1; $i < count($path); $i++ ) { //< count() no file component please. + for ( $i = $i + 1; $i <= count($path); $i++ ) { //< count() no file component please. $tmppath = $to . implode('/', array_slice($path, 0, $i) ); if ( ! $fs->mkdir($tmppath, 0755) ) return new WP_Error('mkdir_failed', __('Could not create directory'), $tmppath); @@ -510,6 +510,10 @@ function request_filesystem_credentials($form_post, $type = '', $error = false) $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? $_POST['username'] : $credentials['username']); $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? $_POST['password'] : $credentials['password']); + // Check to see if we are setting the public/private keys for ssh + $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? $_POST['public_key'] : $credentials['public_key']); + $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? $_POST['private_key'] : $credentials['private_key']); + if ( strpos($credentials['hostname'], ':') ) list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2); @@ -522,7 +526,7 @@ function request_filesystem_credentials($form_post, $type = '', $error = false) if ( ! $error && !empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname']) ) { $stored_credentials = $credentials; - unset($stored_credentials['password']); + unset($stored_credentials['password'], $stored_credentials['private_key'], $stored_credentials['public_key']); update_option('ftp_credentials', $stored_credentials); return $credentials; } @@ -539,6 +543,27 @@ function request_filesystem_credentials($form_post, $type = '', $error = false) echo '
' . $error_string . '