sftp.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Henrik Kjölhede <hkjolhede@gmail.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Storage;
  9. set_include_path(get_include_path() . PATH_SEPARATOR .
  10. \OC_App::getAppPath('files_external') . '/3rdparty/phpseclib/phpseclib');
  11. require 'Net/SFTP.php';
  12. class SFTP extends \OC\Files\Storage\Common {
  13. private $host;
  14. private $user;
  15. private $password;
  16. private $root;
  17. private $client;
  18. private static $tempFiles = array();
  19. public function __construct($params) {
  20. $this->host = $params['host'];
  21. $proto = strpos($this->host, '://');
  22. if ($proto != false) {
  23. $this->host = substr($this->host, $proto+3);
  24. }
  25. $this->user = $params['user'];
  26. $this->password = $params['password'];
  27. $this->root = isset($params['root']) ? $this->cleanPath($params['root']) : '/';
  28. if ($this->root[0] != '/') $this->root = '/' . $this->root;
  29. if (substr($this->root, -1, 1) != '/') $this->root .= '/';
  30. $host_keys = $this->read_host_keys();
  31. $this->client = new \Net_SFTP($this->host);
  32. if (!$this->client->login($this->user, $this->password)) {
  33. throw new \Exception('Login failed');
  34. }
  35. $current_host_key = $this->client->getServerPublicHostKey();
  36. if (array_key_exists($this->host, $host_keys)) {
  37. if ($host_keys[$this->host] != $current_host_key) {
  38. throw new \Exception('Host public key does not match known key');
  39. }
  40. } else {
  41. $host_keys[$this->host] = $current_host_key;
  42. $this->write_host_keys($host_keys);
  43. }
  44. }
  45. public function test() {
  46. if (!isset($params['host']) || !isset($params['user']) || !isset($params['password'])) {
  47. throw new \Exception("Required parameters not set");
  48. }
  49. }
  50. public function getId(){
  51. return 'sftp::' . $this->user . '@' . $this->host . '/' . $this->root;
  52. }
  53. private function abs_path($path) {
  54. return $this->root . $this->cleanPath($path);
  55. }
  56. private function host_keys_path() {
  57. try {
  58. $storage_view = \OCP\Files::getStorage('files_external');
  59. if ($storage_view) {
  60. return \OCP\Config::getSystemValue('datadirectory') .
  61. $storage_view->getAbsolutePath('') .
  62. 'ssh_host_keys';
  63. }
  64. } catch (\Exception $e) {
  65. }
  66. return false;
  67. }
  68. private function write_host_keys($keys) {
  69. try {
  70. $key_path = $this->host_keys_path();
  71. $fp = fopen($key_path, 'w');
  72. foreach ($keys as $host => $key) {
  73. fwrite($fp, $host . '::' . $key . "\n");
  74. }
  75. fclose($fp);
  76. return true;
  77. } catch (\Exception $e) {
  78. return false;
  79. }
  80. }
  81. private function read_host_keys() {
  82. try {
  83. $key_path = $this->host_keys_path();
  84. if (file_exists($key_path)) {
  85. $hosts = array();
  86. $keys = array();
  87. $lines = file($key_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  88. if ($lines) {
  89. foreach ($lines as $line) {
  90. $host_key_arr = explode("::", $line, 2);
  91. if (count($host_key_arr) == 2) {
  92. $hosts[] = $host_key_arr[0];
  93. $keys[] = $host_key_arr[1];
  94. }
  95. }
  96. return array_combine($hosts, $keys);
  97. }
  98. }
  99. } catch (\Exception $e) {
  100. }
  101. return array();
  102. }
  103. public function mkdir($path) {
  104. try {
  105. return $this->client->mkdir($this->abs_path($path));
  106. } catch (\Exception $e) {
  107. return false;
  108. }
  109. }
  110. public function rmdir($path) {
  111. try {
  112. return $this->client->delete($this->abs_path($path), true);
  113. } catch (\Exception $e) {
  114. return false;
  115. }
  116. }
  117. public function opendir($path) {
  118. try {
  119. $list = $this->client->nlist($this->abs_path($path));
  120. $id = md5('sftp:' . $path);
  121. $dir_stream = array();
  122. foreach($list as $file) {
  123. if ($file != '.' && $file != '..') {
  124. $dir_stream[] = $file;
  125. }
  126. }
  127. \OC\Files\Stream\Dir::register($id, $dir_stream);
  128. return opendir('fakedir://' . $id);
  129. } catch(\Exception $e) {
  130. return false;
  131. }
  132. }
  133. public function filetype($path) {
  134. try {
  135. $stat = $this->client->stat($this->abs_path($path));
  136. if ($stat['type'] == NET_SFTP_TYPE_REGULAR) return 'file';
  137. if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) return 'dir';
  138. } catch (\Exeption $e) {
  139. }
  140. return false;
  141. }
  142. public function isReadable($path) {
  143. return true;
  144. }
  145. public function isUpdatable($path) {
  146. return true;
  147. }
  148. public function file_exists($path) {
  149. try {
  150. return $this->client->stat($this->abs_path($path)) === false ? false : true;
  151. } catch (\Exception $e) {
  152. return false;
  153. }
  154. }
  155. public function unlink($path) {
  156. try {
  157. return $this->client->delete($this->abs_path($path), true);
  158. } catch (\Exception $e) {
  159. return false;
  160. }
  161. }
  162. public function fopen($path, $mode) {
  163. try {
  164. $abs_path = $this->abs_path($path);
  165. switch($mode) {
  166. case 'r':
  167. case 'rb':
  168. if ( !$this->file_exists($path)) return false;
  169. if (strrpos($path, '.')!==false) {
  170. $ext=substr($path, strrpos($path, '.'));
  171. } else {
  172. $ext='';
  173. }
  174. $tmp = \OC_Helper::tmpFile($ext);
  175. $this->getFile($abs_path, $tmp);
  176. return fopen($tmp, $mode);
  177. case 'w':
  178. case 'wb':
  179. case 'a':
  180. case 'ab':
  181. case 'r+':
  182. case 'w+':
  183. case 'wb+':
  184. case 'a+':
  185. case 'x':
  186. case 'x+':
  187. case 'c':
  188. case 'c+':
  189. if (strrpos($path, '.')!==false) {
  190. $ext=substr($path, strrpos($path, '.'));
  191. } else {
  192. $ext='';
  193. }
  194. $tmpFile=\OC_Helper::tmpFile($ext);
  195. \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  196. if ($this->file_exists($path)) {
  197. $this->getFile($abs_path, $tmpFile);
  198. }
  199. self::$tempFiles[$tmpFile]=$abs_path;
  200. return fopen('close://'.$tmpFile, $mode);
  201. }
  202. } catch (\Exception $e) {
  203. }
  204. return false;
  205. }
  206. public function writeBack($tmpFile) {
  207. if (array_key_exists($tmpFile, self::$tempFiles)) {
  208. $this->uploadFile($tmpFile, self::$tempFiles[$tmpFile]);
  209. unlink($tmpFile);
  210. unset(self::$tempFiles[$tmpFile]);
  211. }
  212. }
  213. public function touch($path, $mtime=null) {
  214. try {
  215. if (!is_null($mtime)) return false;
  216. if (!$this->file_exists($path)) {
  217. $this->client->put($this->abs_path($path), '');
  218. } else {
  219. return false;
  220. }
  221. } catch (\Exception $e) {
  222. return false;
  223. }
  224. return true;
  225. }
  226. public function getFile($path, $target) {
  227. $this->client->get($path, $target);
  228. }
  229. public function uploadFile($path, $target) {
  230. $this->client->put($target, $path, NET_SFTP_LOCAL_FILE);
  231. }
  232. public function rename($source, $target) {
  233. try {
  234. return $this->client->rename($this->abs_path($source), $this->abs_path($target));
  235. } catch (\Exception $e) {
  236. return false;
  237. }
  238. }
  239. public function stat($path) {
  240. try {
  241. $stat = $this->client->stat($this->abs_path($path));
  242. $mtime = $stat ? $stat['mtime'] : -1;
  243. $size = $stat ? $stat['size'] : 0;
  244. return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1);
  245. } catch (\Exception $e) {
  246. return false;
  247. }
  248. }
  249. }