SFTP.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 <vincent@nextcloud.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. /** @psalm-suppress TooManyArguments */
  126. $login = $this->client->login($this->user, $auth);
  127. if ($login === true) {
  128. break;
  129. }
  130. }
  131. if ($login === false) {
  132. throw new \Exception('Login failed');
  133. }
  134. return $this->client;
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function test() {
  140. if (
  141. !isset($this->host)
  142. || !isset($this->user)
  143. ) {
  144. return false;
  145. }
  146. return $this->getConnection()->nlist() !== false;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function getId() {
  152. $id = 'sftp::' . $this->user . '@' . $this->host;
  153. if ($this->port !== 22) {
  154. $id .= ':' . $this->port;
  155. }
  156. // note: this will double the root slash,
  157. // we should not change it to keep compatible with
  158. // old storage ids
  159. $id .= '/' . $this->root;
  160. return $id;
  161. }
  162. /**
  163. * @return string
  164. */
  165. public function getHost() {
  166. return $this->host;
  167. }
  168. /**
  169. * @return string
  170. */
  171. public function getRoot() {
  172. return $this->root;
  173. }
  174. /**
  175. * @return mixed
  176. */
  177. public function getUser() {
  178. return $this->user;
  179. }
  180. /**
  181. * @param string $path
  182. * @return string
  183. */
  184. private function absPath($path) {
  185. return $this->root . $this->cleanPath($path);
  186. }
  187. /**
  188. * @return string|false
  189. */
  190. private function hostKeysPath() {
  191. try {
  192. $storage_view = \OCP\Files::getStorage('files_external');
  193. if ($storage_view) {
  194. return \OC::$server->getConfig()->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') .
  195. $storage_view->getAbsolutePath('') .
  196. 'ssh_hostKeys';
  197. }
  198. } catch (\Exception $e) {
  199. }
  200. return false;
  201. }
  202. /**
  203. * @param $keys
  204. * @return bool
  205. */
  206. protected function writeHostKeys($keys) {
  207. try {
  208. $keyPath = $this->hostKeysPath();
  209. if ($keyPath && file_exists($keyPath)) {
  210. $fp = fopen($keyPath, 'w');
  211. foreach ($keys as $host => $key) {
  212. fwrite($fp, $host . '::' . $key . "\n");
  213. }
  214. fclose($fp);
  215. return true;
  216. }
  217. } catch (\Exception $e) {
  218. }
  219. return false;
  220. }
  221. /**
  222. * @return array
  223. */
  224. protected function readHostKeys() {
  225. try {
  226. $keyPath = $this->hostKeysPath();
  227. if (file_exists($keyPath)) {
  228. $hosts = [];
  229. $keys = [];
  230. $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  231. if ($lines) {
  232. foreach ($lines as $line) {
  233. $hostKeyArray = explode("::", $line, 2);
  234. if (count($hostKeyArray) === 2) {
  235. $hosts[] = $hostKeyArray[0];
  236. $keys[] = $hostKeyArray[1];
  237. }
  238. }
  239. return array_combine($hosts, $keys);
  240. }
  241. }
  242. } catch (\Exception $e) {
  243. }
  244. return [];
  245. }
  246. /**
  247. * {@inheritdoc}
  248. */
  249. public function mkdir($path) {
  250. try {
  251. return $this->getConnection()->mkdir($this->absPath($path));
  252. } catch (\Exception $e) {
  253. return false;
  254. }
  255. }
  256. /**
  257. * {@inheritdoc}
  258. */
  259. public function rmdir($path) {
  260. try {
  261. $result = $this->getConnection()->delete($this->absPath($path), true);
  262. // workaround: stray stat cache entry when deleting empty folders
  263. // see https://github.com/phpseclib/phpseclib/issues/706
  264. $this->getConnection()->clearStatCache();
  265. return $result;
  266. } catch (\Exception $e) {
  267. return false;
  268. }
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function opendir($path) {
  274. try {
  275. $list = $this->getConnection()->nlist($this->absPath($path));
  276. if ($list === false) {
  277. return false;
  278. }
  279. $id = md5('sftp:' . $path);
  280. $dirStream = [];
  281. foreach ($list as $file) {
  282. if ($file !== '.' && $file !== '..') {
  283. $dirStream[] = $file;
  284. }
  285. }
  286. return IteratorDirectory::wrap($dirStream);
  287. } catch (\Exception $e) {
  288. return false;
  289. }
  290. }
  291. /**
  292. * {@inheritdoc}
  293. */
  294. public function filetype($path) {
  295. try {
  296. $stat = $this->getConnection()->stat($this->absPath($path));
  297. if (!is_array($stat) || !array_key_exists('type', $stat)) {
  298. return false;
  299. }
  300. if ((int) $stat['type'] === NET_SFTP_TYPE_REGULAR) {
  301. return 'file';
  302. }
  303. if ((int) $stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
  304. return 'dir';
  305. }
  306. } catch (\Exception $e) {
  307. }
  308. return false;
  309. }
  310. /**
  311. * {@inheritdoc}
  312. */
  313. public function file_exists($path) {
  314. try {
  315. return $this->getConnection()->stat($this->absPath($path)) !== false;
  316. } catch (\Exception $e) {
  317. return false;
  318. }
  319. }
  320. /**
  321. * {@inheritdoc}
  322. */
  323. public function unlink($path) {
  324. try {
  325. return $this->getConnection()->delete($this->absPath($path), true);
  326. } catch (\Exception $e) {
  327. return false;
  328. }
  329. }
  330. /**
  331. * {@inheritdoc}
  332. */
  333. public function fopen($path, $mode) {
  334. try {
  335. $absPath = $this->absPath($path);
  336. switch ($mode) {
  337. case 'r':
  338. case 'rb':
  339. if (!$this->file_exists($path)) {
  340. return false;
  341. }
  342. SFTPReadStream::register();
  343. $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]);
  344. $handle = fopen('sftpread://' . trim($absPath, '/'), 'r', false, $context);
  345. return RetryWrapper::wrap($handle);
  346. case 'w':
  347. case 'wb':
  348. SFTPWriteStream::register();
  349. $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]);
  350. return fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context);
  351. case 'a':
  352. case 'ab':
  353. case 'r+':
  354. case 'w+':
  355. case 'wb+':
  356. case 'a+':
  357. case 'x':
  358. case 'x+':
  359. case 'c':
  360. case 'c+':
  361. $context = stream_context_create(['sftp' => ['session' => $this->getConnection()]]);
  362. $handle = fopen($this->constructUrl($path), $mode, false, $context);
  363. return RetryWrapper::wrap($handle);
  364. }
  365. } catch (\Exception $e) {
  366. }
  367. return false;
  368. }
  369. /**
  370. * {@inheritdoc}
  371. */
  372. public function touch($path, $mtime = null) {
  373. try {
  374. if (!is_null($mtime)) {
  375. return false;
  376. }
  377. if (!$this->file_exists($path)) {
  378. $this->getConnection()->put($this->absPath($path), '');
  379. } else {
  380. return false;
  381. }
  382. } catch (\Exception $e) {
  383. return false;
  384. }
  385. return true;
  386. }
  387. /**
  388. * @param string $path
  389. * @param string $target
  390. * @throws \Exception
  391. */
  392. public function getFile($path, $target) {
  393. $this->getConnection()->get($path, $target);
  394. }
  395. /**
  396. * {@inheritdoc}
  397. */
  398. public function rename($source, $target) {
  399. try {
  400. if ($this->file_exists($target)) {
  401. $this->unlink($target);
  402. }
  403. return $this->getConnection()->rename(
  404. $this->absPath($source),
  405. $this->absPath($target)
  406. );
  407. } catch (\Exception $e) {
  408. return false;
  409. }
  410. }
  411. /**
  412. * {@inheritdoc}
  413. */
  414. public function stat($path) {
  415. try {
  416. $stat = $this->getConnection()->stat($this->absPath($path));
  417. $mtime = $stat ? $stat['mtime'] : -1;
  418. $size = $stat ? $stat['size'] : 0;
  419. return ['mtime' => $mtime, 'size' => $size, 'ctime' => -1];
  420. } catch (\Exception $e) {
  421. return false;
  422. }
  423. }
  424. /**
  425. * @param string $path
  426. * @return string
  427. */
  428. public function constructUrl($path) {
  429. // Do not pass the password here. We want to use the Net_SFTP object
  430. // supplied via stream context or fail. We only supply username and
  431. // hostname because this might show up in logs (they are not used).
  432. $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path;
  433. return $url;
  434. }
  435. }