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.

Jail.php 15KB

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