Jail.php 14 KB

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