SFTP.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 (!str_contains($host, '://')) {
  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. $connection = $this->getConnection();
  337. switch ($mode) {
  338. case 'r':
  339. case 'rb':
  340. if (!$this->file_exists($path)) {
  341. return false;
  342. }
  343. SFTPReadStream::register();
  344. $context = stream_context_create(['sftp' => ['session' => $connection]]);
  345. $handle = fopen('sftpread://' . trim($absPath, '/'), 'r', false, $context);
  346. return RetryWrapper::wrap($handle);
  347. case 'w':
  348. case 'wb':
  349. SFTPWriteStream::register();
  350. $connection->_remove_from_stat_cache($absPath);
  351. $context = stream_context_create(['sftp' => ['session' => $connection]]);
  352. return fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context);
  353. case 'a':
  354. case 'ab':
  355. case 'r+':
  356. case 'w+':
  357. case 'wb+':
  358. case 'a+':
  359. case 'x':
  360. case 'x+':
  361. case 'c':
  362. case 'c+':
  363. $context = stream_context_create(['sftp' => ['session' => $connection]]);
  364. $handle = fopen($this->constructUrl($path), $mode, false, $context);
  365. return RetryWrapper::wrap($handle);
  366. }
  367. } catch (\Exception $e) {
  368. }
  369. return false;
  370. }
  371. /**
  372. * {@inheritdoc}
  373. */
  374. public function touch($path, $mtime = null) {
  375. try {
  376. if (!is_null($mtime)) {
  377. return false;
  378. }
  379. if (!$this->file_exists($path)) {
  380. $this->getConnection()->put($this->absPath($path), '');
  381. } else {
  382. return false;
  383. }
  384. } catch (\Exception $e) {
  385. return false;
  386. }
  387. return true;
  388. }
  389. /**
  390. * @param string $path
  391. * @param string $target
  392. * @throws \Exception
  393. */
  394. public function getFile($path, $target) {
  395. $this->getConnection()->get($path, $target);
  396. }
  397. /**
  398. * {@inheritdoc}
  399. */
  400. public function rename($source, $target) {
  401. try {
  402. if ($this->file_exists($target)) {
  403. $this->unlink($target);
  404. }
  405. return $this->getConnection()->rename(
  406. $this->absPath($source),
  407. $this->absPath($target)
  408. );
  409. } catch (\Exception $e) {
  410. return false;
  411. }
  412. }
  413. /**
  414. * {@inheritdoc}
  415. */
  416. public function stat($path) {
  417. try {
  418. $stat = $this->getConnection()->stat($this->absPath($path));
  419. $mtime = $stat ? $stat['mtime'] : -1;
  420. $size = $stat ? $stat['size'] : 0;
  421. return ['mtime' => $mtime, 'size' => $size, 'ctime' => -1];
  422. } catch (\Exception $e) {
  423. return false;
  424. }
  425. }
  426. /**
  427. * @param string $path
  428. * @return string
  429. */
  430. public function constructUrl($path) {
  431. // Do not pass the password here. We want to use the Net_SFTP object
  432. // supplied via stream context or fail. We only supply username and
  433. // hostname because this might show up in logs (they are not used).
  434. $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path;
  435. return $url;
  436. }
  437. }