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 14KB

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