You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SFTP.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author hkjolhede <hkjolhede@gmail.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lennart Rosam <lennart.rosam@medien-systempartner.de>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Robin McCorkell <robin@mccorkell.me.uk>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Ross Nicoll <jrn@jrn.me.uk>
  18. * @author SA <stephen@mthosting.net>
  19. * @author Senorsen <senorsen.zhang@gmail.com>
  20. * @author Vincent Petry <pvince81@owncloud.com>
  21. *
  22. * @license AGPL-3.0
  23. *
  24. * This code is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Affero General Public License, version 3,
  26. * as published by the Free Software Foundation.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU Affero General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Affero General Public License, version 3,
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>
  35. *
  36. */
  37. namespace OCA\Files_External\Lib\Storage;
  38. use Icewind\Streams\IteratorDirectory;
  39. use Icewind\Streams\RetryWrapper;
  40. use phpseclib\Net\SFTP\Stream;
  41. /**
  42. * Uses phpseclib's Net\SFTP class and the Net\SFTP\Stream stream wrapper to
  43. * provide access to SFTP servers.
  44. */
  45. class SFTP extends \OC\Files\Storage\Common {
  46. private $host;
  47. private $user;
  48. private $root;
  49. private $port = 22;
  50. private $auth = [];
  51. /**
  52. * @var \phpseclib\Net\SFTP
  53. */
  54. protected $client;
  55. /**
  56. * @param string $host protocol://server:port
  57. * @return array [$server, $port]
  58. */
  59. private function splitHost($host) {
  60. $input = $host;
  61. if (strpos($host, '://') === false) {
  62. // add a protocol to fix parse_url behavior with ipv6
  63. $host = 'http://' . $host;
  64. }
  65. $parsed = parse_url($host);
  66. if (is_array($parsed) && isset($parsed['port'])) {
  67. return [$parsed['host'], $parsed['port']];
  68. } elseif (is_array($parsed)) {
  69. return [$parsed['host'], 22];
  70. } else {
  71. return [$input, 22];
  72. }
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function __construct($params) {
  78. // Register sftp://
  79. Stream::register();
  80. $parsedHost = $this->splitHost($params['host']);
  81. $this->host = $parsedHost[0];
  82. $this->port = $parsedHost[1];
  83. if (!isset($params['user'])) {
  84. throw new \UnexpectedValueException('no authentication parameters specified');
  85. }
  86. $this->user = $params['user'];
  87. if (isset($params['public_key_auth'])) {
  88. $this->auth[] = $params['public_key_auth'];
  89. }
  90. if (isset($params['password']) && $params['password'] !== '') {
  91. $this->auth[] = $params['password'];
  92. }
  93. if ($this->auth === []) {
  94. throw new \UnexpectedValueException('no authentication parameters specified');
  95. }
  96. $this->root
  97. = isset($params['root']) ? $this->cleanPath($params['root']) : '/';
  98. $this->root = '/' . ltrim($this->root, '/');
  99. $this->root = rtrim($this->root, '/') . '/';
  100. }
  101. /**
  102. * Returns the connection.
  103. *
  104. * @return \phpseclib\Net\SFTP connected client instance
  105. * @throws \Exception when the connection failed
  106. */
  107. public function getConnection() {
  108. if (!is_null($this->client)) {
  109. return $this->client;
  110. }
  111. $hostKeys = $this->readHostKeys();
  112. $this->client = new \phpseclib\Net\SFTP($this->host, $this->port);
  113. // The SSH Host Key MUST be verified before login().
  114. $currentHostKey = $this->client->getServerPublicHostKey();
  115. if (array_key_exists($this->host, $hostKeys)) {
  116. if ($hostKeys[$this->host] !== $currentHostKey) {
  117. throw new \Exception('Host public key does not match known key');
  118. }
  119. } else {
  120. $hostKeys[$this->host] = $currentHostKey;
  121. $this->writeHostKeys($hostKeys);
  122. }
  123. $login = false;
  124. foreach ($this->auth as $auth) {
  125. $login = $this->client->login($this->user, $auth);
  126. if ($login === true) {
  127. break;
  128. }
  129. }
  130. if ($login === false) {
  131. throw new \Exception('Login failed');
  132. }
  133. return $this->client;
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function test() {
  139. if (
  140. !isset($this->host)
  141. || !isset($this->user)
  142. ) {
  143. return false;
  144. }
  145. return $this->getConnection()->nlist() !== false;
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function getId() {
  151. $id = 'sftp::' . $this->user . '@' . $this->host;
  152. if ($this->port !== 22) {
  153. $id .= ':' . $this->port;
  154. }
  155. // note: this will double the root slash,
  156. // we should not change it to keep compatible with
  157. // old storage ids
  158. $id .= '/' . $this->root;
  159. return $id;
  160. }
  161. /**
  162. * @return string
  163. */
  164. public function getHost() {
  165. return $this->host;
  166. }
  167. /**
  168. * @return string
  169. */
  170. public function getRoot() {
  171. return $this->root;
  172. }
  173. /**
  174. * @return mixed
  175. */
  176. public function getUser() {
  177. return $this->user;
  178. }
  179. /**
  180. * @param string $path
  181. * @return string
  182. */
  183. private function absPath($path) {
  184. return $this->root . $this->cleanPath($path);
  185. }
  186. /**
  187. * @return string|false
  188. */
  189. private function hostKeysPath() {
  190. try {
  191. $storage_view = \OCP\Files::getStorage('files_external');
  192. if ($storage_view) {
  193. return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') .
  194. $storage_view->getAbsolutePath('') .
  195. 'ssh_hostKeys';
  196. }
  197. } catch (\Exception $e) {
  198. }
  199. return false;
  200. }
  201. /**
  202. * @param $keys
  203. * @return bool
  204. */
  205. protected function writeHostKeys($keys) {
  206. try {
  207. $keyPath = $this->hostKeysPath();
  208. if ($keyPath && file_exists($keyPath)) {
  209. $fp = fopen($keyPath, 'w');
  210. foreach ($keys as $host => $key) {
  211. fwrite($fp, $host . '::' . $key . "\n");
  212. }
  213. fclose($fp);
  214. return true;
  215. }
  216. } catch (\Exception $e) {
  217. }
  218. return false;
  219. }
  220. /**
  221. * @return array
  222. */
  223. protected function readHostKeys() {
  224. try {
  225. $keyPath = $this->hostKeysPath();
  226. if (file_exists($keyPath)) {
  227. $hosts = [];
  228. $keys = [];
  229. $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  230. if ($lines) {
  231. foreach ($lines as $line) {
  232. $hostKeyArray = explode("::", $line, 2);
  233. if (count($hostKeyArray) === 2) {
  234. $hosts[] = $hostKeyArray[0];
  235. $keys[] = $hostKeyArray[1];
  236. }
  237. }
  238. return array_combine($hosts, $keys);
  239. }
  240. }
  241. } catch (\Exception $e) {
  242. }
  243. return [];
  244. }
  245. /**
  246. * {@inheritdoc}
  247. */
  248. public function mkdir($path) {
  249. try {
  250. return $this->getConnection()->mkdir($this->absPath($path));
  251. } catch (\Exception $e) {
  252. return false;
  253. }
  254. }
  255. /**
  256. * {@inheritdoc}
  257. */
  258. public function rmdir($path) {
  259. try {
  260. $result = $this->getConnection()->delete($this->absPath($path), true);
  261. // workaround: stray stat cache entry when deleting empty folders
  262. // see https://github.com/phpseclib/phpseclib/issues/706
  263. $this->getConnection()->clearStatCache();
  264. return $result;
  265. } catch (\Exception $e) {
  266. return false;
  267. }
  268. }
  269. /**
  270. * {@inheritdoc}
  271. */
  272. public function opendir($path) {
  273. try {
  274. $list = $this->getConnection()->nlist($this->absPath($path));
  275. if ($list === false) {
  276. return false;
  277. }
  278. $id = md5('sftp:' . $path);
  279. $dirStream = [];
  280. foreach ($list as $file) {
  281. if ($file !== '.' && $file !== '..') {
  282. $dirStream[] = $file;
  283. }
  284. }
  285. return IteratorDirectory::wrap($dirStream);
  286. } catch (\Exception $e) {
  287. return false;
  288. }
  289. }
  290. /**
  291. * {@inheritdoc}
  292. */
  293. public function filetype($path) {
  294. try {
  295. $stat = $this->getConnection()->stat($this->absPath($path));
  296. if ((int) $stat['type'] === NET_SFTP_TYPE_REGULAR) {
  297. return 'file';
  298. }
  299. if ((int) $stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
  300. return 'dir';
  301. }
  302. } catch (\Exception $e) {
  303. }
  304. return false;
  305. }
  306. /**
  307. * {@inheritdoc}
  308. */
  309. public function file_exists($path) {
  310. try {
  311. return $this->getConnection()->stat($this->absPath($path)) !== false;
  312. } catch (\Exception $e) {
  313. return false;
  314. }
  315. }
  316. /**
  317. * {@inheritdoc}
  318. */
  319. public function unlink($path) {
  320. try {
  321. return $this->getConnection()->delete($this->absPath($path), true);
  322. } catch (\Exception $e) {
  323. return false;
  324. }
  325. }
  326. /**
  327. * {@inheritdoc}
  328. */
  329. public function fopen($path, $mode) {
  330. try {
  331. $absPath = $this->absPath($path);
  332. switch ($mode) {
  333. case 'r':
  334. case 'rb':
  335. if (!$this->file_exists($path)) {
  336. return false;
  337. }
  338. SFTPReadStream::register();
  339. $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]);
  340. $handle = fopen('sftpread://' . trim($absPath, '/'), 'r', false, $context);
  341. return RetryWrapper::wrap($handle);
  342. case 'w':
  343. case 'wb':
  344. SFTPWriteStream::register();
  345. $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]);
  346. return fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context);
  347. case 'a':
  348. case 'ab':
  349. case 'r+':
  350. case 'w+':
  351. case 'wb+':
  352. case 'a+':
  353. case 'x':
  354. case 'x+':
  355. case 'c':
  356. case 'c+':
  357. $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]);
  358. $handle = fopen($this->constructUrl($path), $mode, false, $context);
  359. return RetryWrapper::wrap($handle);
  360. }
  361. } catch (\Exception $e) {
  362. }
  363. return false;
  364. }
  365. /**
  366. * {@inheritdoc}
  367. */
  368. public function touch($path, $mtime = null) {
  369. try {
  370. if (!is_null($mtime)) {
  371. return false;
  372. }
  373. if (!$this->file_exists($path)) {
  374. $this->getConnection()->put($this->absPath($path), '');
  375. } else {
  376. return false;
  377. }
  378. } catch (\Exception $e) {
  379. return false;
  380. }
  381. return true;
  382. }
  383. /**
  384. * @param string $path
  385. * @param string $target
  386. * @throws \Exception
  387. */
  388. public function getFile($path, $target) {
  389. $this->getConnection()->get($path, $target);
  390. }
  391. /**
  392. * {@inheritdoc}
  393. */
  394. public function rename($source, $target) {
  395. try {
  396. if ($this->file_exists($target)) {
  397. $this->unlink($target);
  398. }
  399. return $this->getConnection()->rename(
  400. $this->absPath($source),
  401. $this->absPath($target)
  402. );
  403. } catch (\Exception $e) {
  404. return false;
  405. }
  406. }
  407. /**
  408. * {@inheritdoc}
  409. */
  410. public function stat($path) {
  411. try {
  412. $stat = $this->getConnection()->stat($this->absPath($path));
  413. $mtime = $stat ? $stat['mtime'] : -1;
  414. $size = $stat ? $stat['size'] : 0;
  415. return ['mtime' => $mtime, 'size' => $size, 'ctime' => -1];
  416. } catch (\Exception $e) {
  417. return false;
  418. }
  419. }
  420. /**
  421. * @param string $path
  422. * @return string
  423. */
  424. public function constructUrl($path) {
  425. // Do not pass the password here. We want to use the Net_SFTP object
  426. // supplied via stream context or fail. We only supply username and
  427. // hostname because this might show up in logs (they are not used).
  428. $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path;
  429. return $url;
  430. }
  431. }