Jail.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Storage\Wrapper;
  8. use OC\Files\Cache\Wrapper\CacheJail;
  9. use OC\Files\Cache\Wrapper\JailPropagator;
  10. use OC\Files\Cache\Wrapper\JailWatcher;
  11. use OC\Files\Filesystem;
  12. use OCP\Files\Storage\IStorage;
  13. use OCP\Files\Storage\IWriteStreamStorage;
  14. use OCP\Lock\ILockingProvider;
  15. /**
  16. * Jail to a subdirectory of the wrapped storage
  17. *
  18. * This restricts access to a subfolder of the wrapped storage with the subfolder becoming the root folder new storage
  19. */
  20. class Jail extends Wrapper {
  21. /**
  22. * @var string
  23. */
  24. protected $rootPath;
  25. /**
  26. * @param array $arguments ['storage' => $storage, 'root' => $root]
  27. *
  28. * $storage: The storage that will be wrapper
  29. * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage
  30. */
  31. public function __construct($arguments) {
  32. parent::__construct($arguments);
  33. $this->rootPath = $arguments['root'];
  34. }
  35. public function getUnjailedPath($path) {
  36. return trim(Filesystem::normalizePath($this->rootPath . '/' . $path), '/');
  37. }
  38. /**
  39. * This is separate from Wrapper::getWrapperStorage so we can get the jailed storage consistently even if the jail is inside another wrapper
  40. */
  41. public function getUnjailedStorage() {
  42. return $this->storage;
  43. }
  44. public function getJailedPath($path) {
  45. $root = rtrim($this->rootPath, '/') . '/';
  46. if ($path !== $this->rootPath && !str_starts_with($path, $root)) {
  47. return null;
  48. } else {
  49. $path = substr($path, strlen($this->rootPath));
  50. return trim($path, '/');
  51. }
  52. }
  53. public function getId() {
  54. return parent::getId();
  55. }
  56. /**
  57. * see https://www.php.net/manual/en/function.mkdir.php
  58. *
  59. * @param string $path
  60. * @return bool
  61. */
  62. public function mkdir($path) {
  63. return $this->getWrapperStorage()->mkdir($this->getUnjailedPath($path));
  64. }
  65. /**
  66. * see https://www.php.net/manual/en/function.rmdir.php
  67. *
  68. * @param string $path
  69. * @return bool
  70. */
  71. public function rmdir($path) {
  72. return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path));
  73. }
  74. /**
  75. * see https://www.php.net/manual/en/function.opendir.php
  76. *
  77. * @param string $path
  78. * @return resource|false
  79. */
  80. public function opendir($path) {
  81. return $this->getWrapperStorage()->opendir($this->getUnjailedPath($path));
  82. }
  83. /**
  84. * see https://www.php.net/manual/en/function.is_dir.php
  85. *
  86. * @param string $path
  87. * @return bool
  88. */
  89. public function is_dir($path) {
  90. return $this->getWrapperStorage()->is_dir($this->getUnjailedPath($path));
  91. }
  92. /**
  93. * see https://www.php.net/manual/en/function.is_file.php
  94. *
  95. * @param string $path
  96. * @return bool
  97. */
  98. public function is_file($path) {
  99. return $this->getWrapperStorage()->is_file($this->getUnjailedPath($path));
  100. }
  101. /**
  102. * see https://www.php.net/manual/en/function.stat.php
  103. * only the following keys are required in the result: size and mtime
  104. *
  105. * @param string $path
  106. * @return array|bool
  107. */
  108. public function stat($path) {
  109. return $this->getWrapperStorage()->stat($this->getUnjailedPath($path));
  110. }
  111. /**
  112. * see https://www.php.net/manual/en/function.filetype.php
  113. *
  114. * @param string $path
  115. * @return bool
  116. */
  117. public function filetype($path) {
  118. return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path));
  119. }
  120. /**
  121. * see https://www.php.net/manual/en/function.filesize.php
  122. * The result for filesize when called on a folder is required to be 0
  123. */
  124. public function filesize($path): false|int|float {
  125. return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path));
  126. }
  127. /**
  128. * check if a file can be created in $path
  129. *
  130. * @param string $path
  131. * @return bool
  132. */
  133. public function isCreatable($path) {
  134. return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path));
  135. }
  136. /**
  137. * check if a file can be read
  138. *
  139. * @param string $path
  140. * @return bool
  141. */
  142. public function isReadable($path) {
  143. return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path));
  144. }
  145. /**
  146. * check if a file can be written to
  147. *
  148. * @param string $path
  149. * @return bool
  150. */
  151. public function isUpdatable($path) {
  152. return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path));
  153. }
  154. /**
  155. * check if a file can be deleted
  156. *
  157. * @param string $path
  158. * @return bool
  159. */
  160. public function isDeletable($path) {
  161. return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path));
  162. }
  163. /**
  164. * check if a file can be shared
  165. *
  166. * @param string $path
  167. * @return bool
  168. */
  169. public function isSharable($path) {
  170. return $this->getWrapperStorage()->isSharable($this->getUnjailedPath($path));
  171. }
  172. /**
  173. * get the full permissions of a path.
  174. * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
  175. *
  176. * @param string $path
  177. * @return int
  178. */
  179. public function getPermissions($path) {
  180. return $this->getWrapperStorage()->getPermissions($this->getUnjailedPath($path));
  181. }
  182. /**
  183. * see https://www.php.net/manual/en/function.file_exists.php
  184. *
  185. * @param string $path
  186. * @return bool
  187. */
  188. public function file_exists($path) {
  189. return $this->getWrapperStorage()->file_exists($this->getUnjailedPath($path));
  190. }
  191. /**
  192. * see https://www.php.net/manual/en/function.filemtime.php
  193. *
  194. * @param string $path
  195. * @return int|bool
  196. */
  197. public function filemtime($path) {
  198. return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path));
  199. }
  200. /**
  201. * see https://www.php.net/manual/en/function.file_get_contents.php
  202. *
  203. * @param string $path
  204. * @return string|false
  205. */
  206. public function file_get_contents($path) {
  207. return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path));
  208. }
  209. /**
  210. * see https://www.php.net/manual/en/function.file_put_contents.php
  211. *
  212. * @param string $path
  213. * @param mixed $data
  214. * @return int|float|false
  215. */
  216. public function file_put_contents($path, $data) {
  217. return $this->getWrapperStorage()->file_put_contents($this->getUnjailedPath($path), $data);
  218. }
  219. /**
  220. * see https://www.php.net/manual/en/function.unlink.php
  221. *
  222. * @param string $path
  223. * @return bool
  224. */
  225. public function unlink($path) {
  226. return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path));
  227. }
  228. /**
  229. * see https://www.php.net/manual/en/function.rename.php
  230. *
  231. * @param string $source
  232. * @param string $target
  233. * @return bool
  234. */
  235. public function rename($source, $target) {
  236. return $this->getWrapperStorage()->rename($this->getUnjailedPath($source), $this->getUnjailedPath($target));
  237. }
  238. /**
  239. * see https://www.php.net/manual/en/function.copy.php
  240. *
  241. * @param string $source
  242. * @param string $target
  243. * @return bool
  244. */
  245. public function copy($source, $target) {
  246. return $this->getWrapperStorage()->copy($this->getUnjailedPath($source), $this->getUnjailedPath($target));
  247. }
  248. /**
  249. * see https://www.php.net/manual/en/function.fopen.php
  250. *
  251. * @param string $path
  252. * @param string $mode
  253. * @return resource|bool
  254. */
  255. public function fopen($path, $mode) {
  256. return $this->getWrapperStorage()->fopen($this->getUnjailedPath($path), $mode);
  257. }
  258. /**
  259. * get the mimetype for a file or folder
  260. * The mimetype for a folder is required to be "httpd/unix-directory"
  261. *
  262. * @param string $path
  263. * @return string|bool
  264. */
  265. public function getMimeType($path) {
  266. return $this->getWrapperStorage()->getMimeType($this->getUnjailedPath($path));
  267. }
  268. /**
  269. * see https://www.php.net/manual/en/function.hash.php
  270. *
  271. * @param string $type
  272. * @param string $path
  273. * @param bool $raw
  274. * @return string|bool
  275. */
  276. public function hash($type, $path, $raw = false) {
  277. return $this->getWrapperStorage()->hash($type, $this->getUnjailedPath($path), $raw);
  278. }
  279. /**
  280. * see https://www.php.net/manual/en/function.free_space.php
  281. *
  282. * @param string $path
  283. * @return int|float|bool
  284. */
  285. public function free_space($path) {
  286. return $this->getWrapperStorage()->free_space($this->getUnjailedPath($path));
  287. }
  288. /**
  289. * search for occurrences of $query in file names
  290. *
  291. * @param string $query
  292. * @return array|bool
  293. */
  294. public function search($query) {
  295. return $this->getWrapperStorage()->search($query);
  296. }
  297. /**
  298. * see https://www.php.net/manual/en/function.touch.php
  299. * If the backend does not support the operation, false should be returned
  300. *
  301. * @param string $path
  302. * @param int $mtime
  303. * @return bool
  304. */
  305. public function touch($path, $mtime = null) {
  306. return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime);
  307. }
  308. /**
  309. * get the path to a local version of the file.
  310. * The local version of the file can be temporary and doesn't have to be persistent across requests
  311. *
  312. * @param string $path
  313. * @return string|false
  314. */
  315. public function getLocalFile($path) {
  316. return $this->getWrapperStorage()->getLocalFile($this->getUnjailedPath($path));
  317. }
  318. /**
  319. * check if a file or folder has been updated since $time
  320. *
  321. * @param string $path
  322. * @param int $time
  323. * @return bool
  324. *
  325. * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
  326. * returning true for other changes in the folder is optional
  327. */
  328. public function hasUpdated($path, $time) {
  329. return $this->getWrapperStorage()->hasUpdated($this->getUnjailedPath($path), $time);
  330. }
  331. /**
  332. * get a cache instance for the storage
  333. *
  334. * @param string $path
  335. * @param \OC\Files\Storage\Storage|null (optional) the storage to pass to the cache
  336. * @return \OC\Files\Cache\Cache
  337. */
  338. public function getCache($path = '', $storage = null) {
  339. $sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path));
  340. return new CacheJail($sourceCache, $this->rootPath);
  341. }
  342. /**
  343. * get the user id of the owner of a file or folder
  344. *
  345. * @param string $path
  346. * @return string
  347. */
  348. public function getOwner($path) {
  349. return $this->getWrapperStorage()->getOwner($this->getUnjailedPath($path));
  350. }
  351. /**
  352. * get a watcher instance for the cache
  353. *
  354. * @param string $path
  355. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
  356. * @return \OC\Files\Cache\Watcher
  357. */
  358. public function getWatcher($path = '', $storage = null) {
  359. $sourceWatcher = $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $this->getWrapperStorage());
  360. return new JailWatcher($sourceWatcher, $this->rootPath);
  361. }
  362. /**
  363. * get the ETag for a file or folder
  364. *
  365. * @param string $path
  366. * @return string|false
  367. */
  368. public function getETag($path) {
  369. return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path));
  370. }
  371. public function getMetaData($path) {
  372. return $this->getWrapperStorage()->getMetaData($this->getUnjailedPath($path));
  373. }
  374. /**
  375. * @param string $path
  376. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  377. * @param \OCP\Lock\ILockingProvider $provider
  378. * @throws \OCP\Lock\LockedException
  379. */
  380. public function acquireLock($path, $type, ILockingProvider $provider) {
  381. $this->getWrapperStorage()->acquireLock($this->getUnjailedPath($path), $type, $provider);
  382. }
  383. /**
  384. * @param string $path
  385. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  386. * @param \OCP\Lock\ILockingProvider $provider
  387. */
  388. public function releaseLock($path, $type, ILockingProvider $provider) {
  389. $this->getWrapperStorage()->releaseLock($this->getUnjailedPath($path), $type, $provider);
  390. }
  391. /**
  392. * @param string $path
  393. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  394. * @param \OCP\Lock\ILockingProvider $provider
  395. */
  396. public function changeLock($path, $type, ILockingProvider $provider) {
  397. $this->getWrapperStorage()->changeLock($this->getUnjailedPath($path), $type, $provider);
  398. }
  399. /**
  400. * Resolve the path for the source of the share
  401. *
  402. * @param string $path
  403. * @return array
  404. */
  405. public function resolvePath($path) {
  406. return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
  407. }
  408. /**
  409. * @param IStorage $sourceStorage
  410. * @param string $sourceInternalPath
  411. * @param string $targetInternalPath
  412. * @return bool
  413. */
  414. public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  415. if ($sourceStorage === $this) {
  416. return $this->copy($sourceInternalPath, $targetInternalPath);
  417. }
  418. return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
  419. }
  420. /**
  421. * @param IStorage $sourceStorage
  422. * @param string $sourceInternalPath
  423. * @param string $targetInternalPath
  424. * @return bool
  425. */
  426. public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  427. if ($sourceStorage === $this) {
  428. return $this->rename($sourceInternalPath, $targetInternalPath);
  429. }
  430. return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
  431. }
  432. public function getPropagator($storage = null) {
  433. if (isset($this->propagator)) {
  434. return $this->propagator;
  435. }
  436. if (!$storage) {
  437. $storage = $this;
  438. }
  439. $this->propagator = new JailPropagator($storage, \OC::$server->getDatabaseConnection());
  440. return $this->propagator;
  441. }
  442. public function writeStream(string $path, $stream, ?int $size = null): int {
  443. $storage = $this->getWrapperStorage();
  444. if ($storage->instanceOfStorage(IWriteStreamStorage::class)) {
  445. /** @var IWriteStreamStorage $storage */
  446. return $storage->writeStream($this->getUnjailedPath($path), $stream, $size);
  447. } else {
  448. $target = $this->fopen($path, 'w');
  449. [$count, $result] = \OC_Helper::streamCopy($stream, $target);
  450. fclose($stream);
  451. fclose($target);
  452. return $count;
  453. }
  454. }
  455. public function getDirectoryContent($directory): \Traversable {
  456. return $this->getWrapperStorage()->getDirectoryContent($this->getUnjailedPath($directory));
  457. }
  458. }