Merge pull request #4662 from owncloud/fix-4129

Fix 4129
This commit is contained in:
Frank Karlitschek 2013-10-06 23:42:44 -07:00
commit 46fdb449a4
1 changed files with 78 additions and 47 deletions

View File

@ -29,60 +29,72 @@ class SFTP extends \OC\Files\Storage\Common {
} }
$this->user = $params['user']; $this->user = $params['user'];
$this->password = $params['password']; $this->password = $params['password'];
$this->root = isset($params['root']) ? $this->cleanPath($params['root']) : '/'; $this->root
if ($this->root[0] != '/') $this->root = '/' . $this->root; = isset($params['root']) ? $this->cleanPath($params['root']) : '/';
if (substr($this->root, -1, 1) != '/') $this->root .= '/';
$host_keys = $this->read_host_keys(); if ($this->root[0] != '/') {
$this->root = '/' . $this->root;
}
if (substr($this->root, -1, 1) != '/') {
$this->root .= '/';
}
$hostKeys = $this->readHostKeys();
$this->client = new \Net_SFTP($this->host); $this->client = new \Net_SFTP($this->host);
if (!$this->client->login($this->user, $this->password)) { if (!$this->client->login($this->user, $this->password)) {
throw new \Exception('Login failed'); throw new \Exception('Login failed');
} }
$current_host_key = $this->client->getServerPublicHostKey(); $currentHostKey = $this->client->getServerPublicHostKey();
if (array_key_exists($this->host, $host_keys)) { if (array_key_exists($this->host, $hostKeys)) {
if ($host_keys[$this->host] != $current_host_key) { if ($hostKeys[$this->host] != $currentHostKey) {
throw new \Exception('Host public key does not match known key'); throw new \Exception('Host public key does not match known key');
} }
} else { } else {
$host_keys[$this->host] = $current_host_key; $hostKeys[$this->host] = $currentHostKey;
$this->write_host_keys($host_keys); $this->writeHostKeys($hostKeys);
} }
} }
public function test() { public function test() {
if (!isset($params['host']) || !isset($params['user']) || !isset($params['password'])) { if (
throw new \Exception("Required parameters not set"); !isset($this->host)
|| !isset($this->user)
|| !isset($this->password)
) {
return false;
} }
return $this->client->nlist() !== false;
} }
public function getId(){ public function getId(){
return 'sftp::' . $this->user . '@' . $this->host . '/' . $this->root; return 'sftp::' . $this->user . '@' . $this->host . '/' . $this->root;
} }
private function abs_path($path) { private function absPath($path) {
return $this->root . $this->cleanPath($path); return $this->root . $this->cleanPath($path);
} }
private function host_keys_path() { private function hostKeysPath() {
try { try {
$storage_view = \OCP\Files::getStorage('files_external'); $storage_view = \OCP\Files::getStorage('files_external');
if ($storage_view) { if ($storage_view) {
return \OCP\Config::getSystemValue('datadirectory') . return \OCP\Config::getSystemValue('datadirectory') .
$storage_view->getAbsolutePath('') . $storage_view->getAbsolutePath('') .
'ssh_host_keys'; 'ssh_hostKeys';
} }
} catch (\Exception $e) { } catch (\Exception $e) {
} }
return false; return false;
} }
private function write_host_keys($keys) { private function writeHostKeys($keys) {
try { try {
$key_path = $this->host_keys_path(); $keyPath = $this->hostKeysPath();
$fp = fopen($key_path, 'w'); $fp = fopen($keyPath, 'w');
foreach ($keys as $host => $key) { foreach ($keys as $host => $key) {
fwrite($fp, $host . '::' . $key . "\n"); fwrite($fp, $host . '::' . $key . "\n");
} }
@ -93,19 +105,19 @@ class SFTP extends \OC\Files\Storage\Common {
} }
} }
private function read_host_keys() { private function readHostKeys() {
try { try {
$key_path = $this->host_keys_path(); $keyPath = $this->hostKeysPath();
if (file_exists($key_path)) { if (file_exists($keyPath)) {
$hosts = array(); $hosts = array();
$keys = array(); $keys = array();
$lines = file($key_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines) { if ($lines) {
foreach ($lines as $line) { foreach ($lines as $line) {
$host_key_arr = explode("::", $line, 2); $hostKeyArray = explode("::", $line, 2);
if (count($host_key_arr) == 2) { if (count($hostKeyArray) == 2) {
$hosts[] = $host_key_arr[0]; $hosts[] = $hostKeyArray[0];
$keys[] = $host_key_arr[1]; $keys[] = $hostKeyArray[1];
} }
} }
return array_combine($hosts, $keys); return array_combine($hosts, $keys);
@ -118,7 +130,7 @@ class SFTP extends \OC\Files\Storage\Common {
public function mkdir($path) { public function mkdir($path) {
try { try {
return $this->client->mkdir($this->abs_path($path)); return $this->client->mkdir($this->absPath($path));
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
@ -126,7 +138,7 @@ class SFTP extends \OC\Files\Storage\Common {
public function rmdir($path) { public function rmdir($path) {
try { try {
return $this->client->delete($this->abs_path($path), true); return $this->client->delete($this->absPath($path), true);
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
@ -134,16 +146,16 @@ class SFTP extends \OC\Files\Storage\Common {
public function opendir($path) { public function opendir($path) {
try { try {
$list = $this->client->nlist($this->abs_path($path)); $list = $this->client->nlist($this->absPath($path));
$id = md5('sftp:' . $path); $id = md5('sftp:' . $path);
$dir_stream = array(); $dirStream = array();
foreach($list as $file) { foreach($list as $file) {
if ($file != '.' && $file != '..') { if ($file != '.' && $file != '..') {
$dir_stream[] = $file; $dirStream[] = $file;
} }
} }
\OC\Files\Stream\Dir::register($id, $dir_stream); \OC\Files\Stream\Dir::register($id, $dirStream);
return opendir('fakedir://' . $id); return opendir('fakedir://' . $id);
} catch(\Exception $e) { } catch(\Exception $e) {
return false; return false;
@ -152,10 +164,16 @@ class SFTP extends \OC\Files\Storage\Common {
public function filetype($path) { public function filetype($path) {
try { try {
$stat = $this->client->stat($this->abs_path($path)); $stat = $this->client->stat($this->absPath($path));
if ($stat['type'] == NET_SFTP_TYPE_REGULAR) return 'file'; if ($stat['type'] == NET_SFTP_TYPE_REGULAR) {
if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) return 'dir'; return 'file';
}
if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) {
return 'dir';
}
} catch (\Exeption $e) { } catch (\Exeption $e) {
} }
return false; return false;
} }
@ -170,7 +188,7 @@ class SFTP extends \OC\Files\Storage\Common {
public function file_exists($path) { public function file_exists($path) {
try { try {
return $this->client->stat($this->abs_path($path)) !== false; return $this->client->stat($this->absPath($path)) !== false;
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
@ -178,7 +196,7 @@ class SFTP extends \OC\Files\Storage\Common {
public function unlink($path) { public function unlink($path) {
try { try {
return $this->client->delete($this->abs_path($path), true); return $this->client->delete($this->absPath($path), true);
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
@ -186,18 +204,21 @@ class SFTP extends \OC\Files\Storage\Common {
public function fopen($path, $mode) { public function fopen($path, $mode) {
try { try {
$abs_path = $this->abs_path($path); $absPath = $this->absPath($path);
switch($mode) { switch($mode) {
case 'r': case 'r':
case 'rb': case 'rb':
if ( !$this->file_exists($path)) return false; if ( !$this->file_exists($path)) {
return false;
}
if (strrpos($path, '.')!==false) { if (strrpos($path, '.')!==false) {
$ext=substr($path, strrpos($path, '.')); $ext=substr($path, strrpos($path, '.'));
} else { } else {
$ext=''; $ext='';
} }
$tmp = \OC_Helper::tmpFile($ext); $tmp = \OC_Helper::tmpFile($ext);
$this->getFile($abs_path, $tmp); $this->getFile($absPath, $tmp);
return fopen($tmp, $mode); return fopen($tmp, $mode);
case 'w': case 'w':
@ -217,12 +238,18 @@ class SFTP extends \OC\Files\Storage\Common {
} else { } else {
$ext=''; $ext='';
} }
$tmpFile=\OC_Helper::tmpFile($ext); $tmpFile=\OC_Helper::tmpFile($ext);
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack')); \OC\Files\Stream\Close::registerCallback(
$tmpFile,
array($this, 'writeBack')
);
if ($this->file_exists($path)) { if ($this->file_exists($path)) {
$this->getFile($abs_path, $tmpFile); $this->getFile($absPath, $tmpFile);
} }
self::$tempFiles[$tmpFile]=$abs_path;
self::$tempFiles[$tmpFile]=$absPath;
return fopen('close://'.$tmpFile, $mode); return fopen('close://'.$tmpFile, $mode);
} }
} catch (\Exception $e) { } catch (\Exception $e) {
@ -240,9 +267,11 @@ class SFTP extends \OC\Files\Storage\Common {
public function touch($path, $mtime=null) { public function touch($path, $mtime=null) {
try { try {
if (!is_null($mtime)) return false; if (!is_null($mtime)) {
return false;
}
if (!$this->file_exists($path)) { if (!$this->file_exists($path)) {
$this->client->put($this->abs_path($path), ''); $this->client->put($this->absPath($path), '');
} else { } else {
return false; return false;
} }
@ -262,7 +291,10 @@ class SFTP extends \OC\Files\Storage\Common {
public function rename($source, $target) { public function rename($source, $target) {
try { try {
return $this->client->rename($this->abs_path($source), $this->abs_path($target)); return $this->client->rename(
$this->absPath($source),
$this->absPath($target)
);
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
@ -270,7 +302,7 @@ class SFTP extends \OC\Files\Storage\Common {
public function stat($path) { public function stat($path) {
try { try {
$stat = $this->client->stat($this->abs_path($path)); $stat = $this->client->stat($this->absPath($path));
$mtime = $stat ? $stat['mtime'] : -1; $mtime = $stat ? $stat['mtime'] : -1;
$size = $stat ? $stat['size'] : 0; $size = $stat ? $stat['size'] : 0;
@ -279,6 +311,5 @@ class SFTP extends \OC\Files\Storage\Common {
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
} }
} }