SFTP.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib\Storage;
  8. use Icewind\Streams\CallbackWrapper;
  9. use Icewind\Streams\CountWrapper;
  10. use Icewind\Streams\IteratorDirectory;
  11. use Icewind\Streams\RetryWrapper;
  12. use OC\Files\Storage\Common;
  13. use OC\Files\View;
  14. use OCP\Cache\CappedMemoryCache;
  15. use OCP\Constants;
  16. use OCP\Files\FileInfo;
  17. use OCP\Files\IMimeTypeDetector;
  18. use phpseclib\Net\SFTP\Stream;
  19. /**
  20. * Uses phpseclib's Net\SFTP class and the Net\SFTP\Stream stream wrapper to
  21. * provide access to SFTP servers.
  22. */
  23. class SFTP extends Common {
  24. private $host;
  25. private $user;
  26. private $root;
  27. private $port = 22;
  28. private $auth = [];
  29. /**
  30. * @var \phpseclib\Net\SFTP
  31. */
  32. protected $client;
  33. private CappedMemoryCache $knownMTimes;
  34. private IMimeTypeDetector $mimeTypeDetector;
  35. public const COPY_CHUNK_SIZE = 8 * 1024 * 1024;
  36. /**
  37. * @param string $host protocol://server:port
  38. * @return array [$server, $port]
  39. */
  40. private function splitHost(string $host): array {
  41. $input = $host;
  42. if (!str_contains($host, '://')) {
  43. // add a protocol to fix parse_url behavior with ipv6
  44. $host = 'http://' . $host;
  45. }
  46. $parsed = parse_url($host);
  47. if (is_array($parsed) && isset($parsed['port'])) {
  48. return [$parsed['host'], $parsed['port']];
  49. } elseif (is_array($parsed)) {
  50. return [$parsed['host'], 22];
  51. } else {
  52. return [$input, 22];
  53. }
  54. }
  55. public function __construct(array $parameters) {
  56. // Register sftp://
  57. Stream::register();
  58. $parsedHost = $this->splitHost($parameters['host']);
  59. $this->host = $parsedHost[0];
  60. $this->port = $parsedHost[1];
  61. if (!isset($parameters['user'])) {
  62. throw new \UnexpectedValueException('no authentication parameters specified');
  63. }
  64. $this->user = $parameters['user'];
  65. if (isset($parameters['public_key_auth'])) {
  66. $this->auth[] = $parameters['public_key_auth'];
  67. }
  68. if (isset($parameters['password']) && $parameters['password'] !== '') {
  69. $this->auth[] = $parameters['password'];
  70. }
  71. if ($this->auth === []) {
  72. throw new \UnexpectedValueException('no authentication parameters specified');
  73. }
  74. $this->root
  75. = isset($parameters['root']) ? $this->cleanPath($parameters['root']) : '/';
  76. $this->root = '/' . ltrim($this->root, '/');
  77. $this->root = rtrim($this->root, '/') . '/';
  78. $this->knownMTimes = new CappedMemoryCache();
  79. $this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);
  80. }
  81. /**
  82. * Returns the connection.
  83. *
  84. * @return \phpseclib\Net\SFTP connected client instance
  85. * @throws \Exception when the connection failed
  86. */
  87. public function getConnection(): \phpseclib\Net\SFTP {
  88. if (!is_null($this->client)) {
  89. return $this->client;
  90. }
  91. $hostKeys = $this->readHostKeys();
  92. $this->client = new \phpseclib\Net\SFTP($this->host, $this->port);
  93. // The SSH Host Key MUST be verified before login().
  94. $currentHostKey = $this->client->getServerPublicHostKey();
  95. if (array_key_exists($this->host, $hostKeys)) {
  96. if ($hostKeys[$this->host] !== $currentHostKey) {
  97. throw new \Exception('Host public key does not match known key');
  98. }
  99. } else {
  100. $hostKeys[$this->host] = $currentHostKey;
  101. $this->writeHostKeys($hostKeys);
  102. }
  103. $login = false;
  104. foreach ($this->auth as $auth) {
  105. /** @psalm-suppress TooManyArguments */
  106. $login = $this->client->login($this->user, $auth);
  107. if ($login === true) {
  108. break;
  109. }
  110. }
  111. if ($login === false) {
  112. throw new \Exception('Login failed');
  113. }
  114. return $this->client;
  115. }
  116. public function test(): bool {
  117. if (
  118. !isset($this->host)
  119. || !isset($this->user)
  120. ) {
  121. return false;
  122. }
  123. return $this->getConnection()->nlist() !== false;
  124. }
  125. public function getId(): string {
  126. $id = 'sftp::' . $this->user . '@' . $this->host;
  127. if ($this->port !== 22) {
  128. $id .= ':' . $this->port;
  129. }
  130. // note: this will double the root slash,
  131. // we should not change it to keep compatible with
  132. // old storage ids
  133. $id .= '/' . $this->root;
  134. return $id;
  135. }
  136. public function getHost(): string {
  137. return $this->host;
  138. }
  139. public function getRoot(): string {
  140. return $this->root;
  141. }
  142. public function getUser(): string {
  143. return $this->user;
  144. }
  145. private function absPath(string $path): string {
  146. return $this->root . $this->cleanPath($path);
  147. }
  148. private function hostKeysPath(): string|false {
  149. try {
  150. $userId = \OC_User::getUser();
  151. if ($userId === false) {
  152. return false;
  153. }
  154. $view = new View('/' . $userId . '/files_external');
  155. return $view->getLocalFile('ssh_hostKeys');
  156. } catch (\Exception $e) {
  157. }
  158. return false;
  159. }
  160. protected function writeHostKeys(array $keys): bool {
  161. try {
  162. $keyPath = $this->hostKeysPath();
  163. if ($keyPath && file_exists($keyPath)) {
  164. $fp = fopen($keyPath, 'w');
  165. foreach ($keys as $host => $key) {
  166. fwrite($fp, $host . '::' . $key . "\n");
  167. }
  168. fclose($fp);
  169. return true;
  170. }
  171. } catch (\Exception $e) {
  172. }
  173. return false;
  174. }
  175. protected function readHostKeys(): array {
  176. try {
  177. $keyPath = $this->hostKeysPath();
  178. if (file_exists($keyPath)) {
  179. $hosts = [];
  180. $keys = [];
  181. $lines = file($keyPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  182. if ($lines) {
  183. foreach ($lines as $line) {
  184. $hostKeyArray = explode('::', $line, 2);
  185. if (count($hostKeyArray) === 2) {
  186. $hosts[] = $hostKeyArray[0];
  187. $keys[] = $hostKeyArray[1];
  188. }
  189. }
  190. return array_combine($hosts, $keys);
  191. }
  192. }
  193. } catch (\Exception $e) {
  194. }
  195. return [];
  196. }
  197. public function mkdir(string $path): bool {
  198. try {
  199. return $this->getConnection()->mkdir($this->absPath($path));
  200. } catch (\Exception $e) {
  201. return false;
  202. }
  203. }
  204. public function rmdir(string $path): bool {
  205. try {
  206. $result = $this->getConnection()->delete($this->absPath($path), true);
  207. // workaround: stray stat cache entry when deleting empty folders
  208. // see https://github.com/phpseclib/phpseclib/issues/706
  209. $this->getConnection()->clearStatCache();
  210. return $result;
  211. } catch (\Exception $e) {
  212. return false;
  213. }
  214. }
  215. public function opendir(string $path) {
  216. try {
  217. $list = $this->getConnection()->nlist($this->absPath($path));
  218. if ($list === false) {
  219. return false;
  220. }
  221. $id = md5('sftp:' . $path);
  222. $dirStream = [];
  223. foreach ($list as $file) {
  224. if ($file !== '.' && $file !== '..') {
  225. $dirStream[] = $file;
  226. }
  227. }
  228. return IteratorDirectory::wrap($dirStream);
  229. } catch (\Exception $e) {
  230. return false;
  231. }
  232. }
  233. public function filetype(string $path): string|false {
  234. try {
  235. $stat = $this->getConnection()->stat($this->absPath($path));
  236. if (!is_array($stat) || !array_key_exists('type', $stat)) {
  237. return false;
  238. }
  239. if ((int)$stat['type'] === NET_SFTP_TYPE_REGULAR) {
  240. return 'file';
  241. }
  242. if ((int)$stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
  243. return 'dir';
  244. }
  245. } catch (\Exception $e) {
  246. }
  247. return false;
  248. }
  249. public function file_exists(string $path): bool {
  250. try {
  251. return $this->getConnection()->stat($this->absPath($path)) !== false;
  252. } catch (\Exception $e) {
  253. return false;
  254. }
  255. }
  256. public function unlink(string $path): bool {
  257. try {
  258. return $this->getConnection()->delete($this->absPath($path), true);
  259. } catch (\Exception $e) {
  260. return false;
  261. }
  262. }
  263. public function fopen(string $path, string $mode) {
  264. $path = $this->cleanPath($path);
  265. try {
  266. $absPath = $this->absPath($path);
  267. $connection = $this->getConnection();
  268. switch ($mode) {
  269. case 'r':
  270. case 'rb':
  271. $stat = $this->stat($path);
  272. if (!$stat) {
  273. return false;
  274. }
  275. SFTPReadStream::register();
  276. $context = stream_context_create(['sftp' => ['session' => $connection, 'size' => $stat['size']]]);
  277. $handle = fopen('sftpread://' . trim($absPath, '/'), 'r', false, $context);
  278. return RetryWrapper::wrap($handle);
  279. case 'w':
  280. case 'wb':
  281. SFTPWriteStream::register();
  282. // the SFTPWriteStream doesn't go through the "normal" methods so it doesn't clear the stat cache.
  283. $connection->_remove_from_stat_cache($absPath);
  284. $context = stream_context_create(['sftp' => ['session' => $connection]]);
  285. $fh = fopen('sftpwrite://' . trim($absPath, '/'), 'w', false, $context);
  286. if ($fh) {
  287. $fh = CallbackWrapper::wrap($fh, null, null, function () use ($path) {
  288. $this->knownMTimes->set($path, time());
  289. });
  290. }
  291. return $fh;
  292. case 'a':
  293. case 'ab':
  294. case 'r+':
  295. case 'w+':
  296. case 'wb+':
  297. case 'a+':
  298. case 'x':
  299. case 'x+':
  300. case 'c':
  301. case 'c+':
  302. $context = stream_context_create(['sftp' => ['session' => $connection]]);
  303. $handle = fopen($this->constructUrl($path), $mode, false, $context);
  304. return RetryWrapper::wrap($handle);
  305. }
  306. } catch (\Exception $e) {
  307. }
  308. return false;
  309. }
  310. public function touch(string $path, ?int $mtime = null): bool {
  311. try {
  312. if (!is_null($mtime)) {
  313. return false;
  314. }
  315. if (!$this->file_exists($path)) {
  316. return $this->getConnection()->put($this->absPath($path), '');
  317. } else {
  318. return false;
  319. }
  320. } catch (\Exception $e) {
  321. return false;
  322. }
  323. }
  324. /**
  325. * @throws \Exception
  326. */
  327. public function getFile(string $path, string $target): void {
  328. $this->getConnection()->get($path, $target);
  329. }
  330. public function rename(string $source, string $target): bool {
  331. try {
  332. if ($this->file_exists($target)) {
  333. $this->unlink($target);
  334. }
  335. return $this->getConnection()->rename(
  336. $this->absPath($source),
  337. $this->absPath($target)
  338. );
  339. } catch (\Exception $e) {
  340. return false;
  341. }
  342. }
  343. /**
  344. * @return array{mtime: int, size: int, ctime: int}|false
  345. */
  346. public function stat(string $path): array|false {
  347. try {
  348. $path = $this->cleanPath($path);
  349. $stat = $this->getConnection()->stat($this->absPath($path));
  350. $mtime = isset($stat['mtime']) ? (int)$stat['mtime'] : -1;
  351. $size = isset($stat['size']) ? (int)$stat['size'] : 0;
  352. // the mtime can't be less than when we last touched it
  353. if ($knownMTime = $this->knownMTimes->get($path)) {
  354. $mtime = max($mtime, $knownMTime);
  355. }
  356. return [
  357. 'mtime' => $mtime,
  358. 'size' => $size,
  359. 'ctime' => -1
  360. ];
  361. } catch (\Exception $e) {
  362. return false;
  363. }
  364. }
  365. public function constructUrl(string $path): string {
  366. // Do not pass the password here. We want to use the Net_SFTP object
  367. // supplied via stream context or fail. We only supply username and
  368. // hostname because this might show up in logs (they are not used).
  369. $url = 'sftp://' . urlencode($this->user) . '@' . $this->host . ':' . $this->port . $this->root . $path;
  370. return $url;
  371. }
  372. public function file_put_contents(string $path, mixed $data): int|float|false {
  373. /** @psalm-suppress InternalMethod */
  374. $result = $this->getConnection()->put($this->absPath($path), $data);
  375. if ($result) {
  376. return strlen($data);
  377. } else {
  378. return false;
  379. }
  380. }
  381. public function writeStream(string $path, $stream, ?int $size = null): int {
  382. if ($size === null) {
  383. $stream = CountWrapper::wrap($stream, function (int $writtenSize) use (&$size): void {
  384. $size = $writtenSize;
  385. });
  386. if (!$stream) {
  387. throw new \Exception('Failed to wrap stream');
  388. }
  389. }
  390. /** @psalm-suppress InternalMethod */
  391. $result = $this->getConnection()->put($this->absPath($path), $stream);
  392. fclose($stream);
  393. if ($result) {
  394. if ($size === null) {
  395. throw new \Exception('Failed to get written size from sftp storage wrapper');
  396. }
  397. return $size;
  398. } else {
  399. throw new \Exception('Failed to write steam to sftp storage');
  400. }
  401. }
  402. public function copy(string $source, string $target): bool {
  403. if ($this->is_dir($source) || $this->is_dir($target)) {
  404. return parent::copy($source, $target);
  405. } else {
  406. $absSource = $this->absPath($source);
  407. $absTarget = $this->absPath($target);
  408. $connection = $this->getConnection();
  409. $size = $connection->size($absSource);
  410. if ($size === false) {
  411. return false;
  412. }
  413. for ($i = 0; $i < $size; $i += self::COPY_CHUNK_SIZE) {
  414. /** @psalm-suppress InvalidArgument */
  415. $chunk = $connection->get($absSource, false, $i, self::COPY_CHUNK_SIZE);
  416. if ($chunk === false) {
  417. return false;
  418. }
  419. /** @psalm-suppress InternalMethod */
  420. if (!$connection->put($absTarget, $chunk, \phpseclib\Net\SFTP::SOURCE_STRING, $i)) {
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. }
  427. public function getPermissions(string $path): int {
  428. $stat = $this->getConnection()->stat($this->absPath($path));
  429. if (!$stat) {
  430. return 0;
  431. }
  432. if ($stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
  433. return Constants::PERMISSION_ALL;
  434. } else {
  435. return Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
  436. }
  437. }
  438. public function getMetaData(string $path): ?array {
  439. $stat = $this->getConnection()->stat($this->absPath($path));
  440. if (!$stat) {
  441. return null;
  442. }
  443. if ($stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
  444. $stat['permissions'] = Constants::PERMISSION_ALL;
  445. } else {
  446. $stat['permissions'] = Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE;
  447. }
  448. if ($stat['type'] === NET_SFTP_TYPE_DIRECTORY) {
  449. $stat['size'] = -1;
  450. $stat['mimetype'] = FileInfo::MIMETYPE_FOLDER;
  451. } else {
  452. $stat['mimetype'] = $this->mimeTypeDetector->detectPath($path);
  453. }
  454. $stat['etag'] = $this->getETag($path);
  455. $stat['storage_mtime'] = $stat['mtime'];
  456. $stat['name'] = basename($path);
  457. $keys = ['size', 'mtime', 'mimetype', 'etag', 'storage_mtime', 'permissions', 'name'];
  458. return array_intersect_key($stat, array_flip($keys));
  459. }
  460. }