Jail.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Cache\ICache;
  13. use OCP\Files\Cache\IPropagator;
  14. use OCP\Files\Cache\IWatcher;
  15. use OCP\Files\Storage\IStorage;
  16. use OCP\Files\Storage\IWriteStreamStorage;
  17. use OCP\Lock\ILockingProvider;
  18. /**
  19. * Jail to a subdirectory of the wrapped storage
  20. *
  21. * This restricts access to a subfolder of the wrapped storage with the subfolder becoming the root folder new storage
  22. */
  23. class Jail extends Wrapper {
  24. /**
  25. * @var string
  26. */
  27. protected $rootPath;
  28. /**
  29. * @param array $parameters ['storage' => $storage, 'root' => $root]
  30. *
  31. * $storage: The storage that will be wrapper
  32. * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage
  33. */
  34. public function __construct(array $parameters) {
  35. parent::__construct($parameters);
  36. $this->rootPath = $parameters['root'];
  37. }
  38. public function getUnjailedPath(string $path): string {
  39. return trim(Filesystem::normalizePath($this->rootPath . '/' . $path), '/');
  40. }
  41. /**
  42. * This is separate from Wrapper::getWrapperStorage so we can get the jailed storage consistently even if the jail is inside another wrapper
  43. */
  44. public function getUnjailedStorage(): IStorage {
  45. return $this->storage;
  46. }
  47. public function getJailedPath(string $path): ?string {
  48. $root = rtrim($this->rootPath, '/') . '/';
  49. if ($path !== $this->rootPath && !str_starts_with($path, $root)) {
  50. return null;
  51. } else {
  52. $path = substr($path, strlen($this->rootPath));
  53. return trim($path, '/');
  54. }
  55. }
  56. public function getId(): string {
  57. return parent::getId();
  58. }
  59. public function mkdir(string $path): bool {
  60. return $this->getWrapperStorage()->mkdir($this->getUnjailedPath($path));
  61. }
  62. public function rmdir(string $path): bool {
  63. return $this->getWrapperStorage()->rmdir($this->getUnjailedPath($path));
  64. }
  65. public function opendir(string $path) {
  66. return $this->getWrapperStorage()->opendir($this->getUnjailedPath($path));
  67. }
  68. public function is_dir(string $path): bool {
  69. return $this->getWrapperStorage()->is_dir($this->getUnjailedPath($path));
  70. }
  71. public function is_file(string $path): bool {
  72. return $this->getWrapperStorage()->is_file($this->getUnjailedPath($path));
  73. }
  74. public function stat(string $path): array|false {
  75. return $this->getWrapperStorage()->stat($this->getUnjailedPath($path));
  76. }
  77. public function filetype(string $path): string|false {
  78. return $this->getWrapperStorage()->filetype($this->getUnjailedPath($path));
  79. }
  80. public function filesize(string $path): int|float|false {
  81. return $this->getWrapperStorage()->filesize($this->getUnjailedPath($path));
  82. }
  83. public function isCreatable(string $path): bool {
  84. return $this->getWrapperStorage()->isCreatable($this->getUnjailedPath($path));
  85. }
  86. public function isReadable(string $path): bool {
  87. return $this->getWrapperStorage()->isReadable($this->getUnjailedPath($path));
  88. }
  89. public function isUpdatable(string $path): bool {
  90. return $this->getWrapperStorage()->isUpdatable($this->getUnjailedPath($path));
  91. }
  92. public function isDeletable(string $path): bool {
  93. return $this->getWrapperStorage()->isDeletable($this->getUnjailedPath($path));
  94. }
  95. public function isSharable(string $path): bool {
  96. return $this->getWrapperStorage()->isSharable($this->getUnjailedPath($path));
  97. }
  98. public function getPermissions(string $path): int {
  99. return $this->getWrapperStorage()->getPermissions($this->getUnjailedPath($path));
  100. }
  101. public function file_exists(string $path): bool {
  102. return $this->getWrapperStorage()->file_exists($this->getUnjailedPath($path));
  103. }
  104. public function filemtime(string $path): int|false {
  105. return $this->getWrapperStorage()->filemtime($this->getUnjailedPath($path));
  106. }
  107. public function file_get_contents(string $path): string|false {
  108. return $this->getWrapperStorage()->file_get_contents($this->getUnjailedPath($path));
  109. }
  110. public function file_put_contents(string $path, mixed $data): int|float|false {
  111. return $this->getWrapperStorage()->file_put_contents($this->getUnjailedPath($path), $data);
  112. }
  113. public function unlink(string $path): bool {
  114. return $this->getWrapperStorage()->unlink($this->getUnjailedPath($path));
  115. }
  116. public function rename(string $source, string $target): bool {
  117. return $this->getWrapperStorage()->rename($this->getUnjailedPath($source), $this->getUnjailedPath($target));
  118. }
  119. public function copy(string $source, string $target): bool {
  120. return $this->getWrapperStorage()->copy($this->getUnjailedPath($source), $this->getUnjailedPath($target));
  121. }
  122. public function fopen(string $path, string $mode) {
  123. return $this->getWrapperStorage()->fopen($this->getUnjailedPath($path), $mode);
  124. }
  125. public function getMimeType(string $path): string|false {
  126. return $this->getWrapperStorage()->getMimeType($this->getUnjailedPath($path));
  127. }
  128. public function hash(string $type, string $path, bool $raw = false): string|false {
  129. return $this->getWrapperStorage()->hash($type, $this->getUnjailedPath($path), $raw);
  130. }
  131. public function free_space(string $path): int|float|false {
  132. return $this->getWrapperStorage()->free_space($this->getUnjailedPath($path));
  133. }
  134. public function touch(string $path, ?int $mtime = null): bool {
  135. return $this->getWrapperStorage()->touch($this->getUnjailedPath($path), $mtime);
  136. }
  137. public function getLocalFile(string $path): string|false {
  138. return $this->getWrapperStorage()->getLocalFile($this->getUnjailedPath($path));
  139. }
  140. public function hasUpdated(string $path, int $time): bool {
  141. return $this->getWrapperStorage()->hasUpdated($this->getUnjailedPath($path), $time);
  142. }
  143. public function getCache(string $path = '', ?IStorage $storage = null): ICache {
  144. $sourceCache = $this->getWrapperStorage()->getCache($this->getUnjailedPath($path));
  145. return new CacheJail($sourceCache, $this->rootPath);
  146. }
  147. public function getOwner(string $path): string|false {
  148. return $this->getWrapperStorage()->getOwner($this->getUnjailedPath($path));
  149. }
  150. public function getWatcher(string $path = '', ?IStorage $storage = null): IWatcher {
  151. $sourceWatcher = $this->getWrapperStorage()->getWatcher($this->getUnjailedPath($path), $this->getWrapperStorage());
  152. return new JailWatcher($sourceWatcher, $this->rootPath);
  153. }
  154. public function getETag(string $path): string|false {
  155. return $this->getWrapperStorage()->getETag($this->getUnjailedPath($path));
  156. }
  157. public function getMetaData(string $path): ?array {
  158. return $this->getWrapperStorage()->getMetaData($this->getUnjailedPath($path));
  159. }
  160. public function acquireLock(string $path, int $type, ILockingProvider $provider): void {
  161. $this->getWrapperStorage()->acquireLock($this->getUnjailedPath($path), $type, $provider);
  162. }
  163. public function releaseLock(string $path, int $type, ILockingProvider $provider): void {
  164. $this->getWrapperStorage()->releaseLock($this->getUnjailedPath($path), $type, $provider);
  165. }
  166. public function changeLock(string $path, int $type, ILockingProvider $provider): void {
  167. $this->getWrapperStorage()->changeLock($this->getUnjailedPath($path), $type, $provider);
  168. }
  169. /**
  170. * Resolve the path for the source of the share
  171. */
  172. public function resolvePath(string $path): array {
  173. return [$this->getWrapperStorage(), $this->getUnjailedPath($path)];
  174. }
  175. public function copyFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  176. if ($sourceStorage === $this) {
  177. return $this->copy($sourceInternalPath, $targetInternalPath);
  178. }
  179. return $this->getWrapperStorage()->copyFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
  180. }
  181. public function moveFromStorage(IStorage $sourceStorage, string $sourceInternalPath, string $targetInternalPath): bool {
  182. if ($sourceStorage === $this) {
  183. return $this->rename($sourceInternalPath, $targetInternalPath);
  184. }
  185. return $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $this->getUnjailedPath($targetInternalPath));
  186. }
  187. public function getPropagator(?IStorage $storage = null): IPropagator {
  188. if (isset($this->propagator)) {
  189. return $this->propagator;
  190. }
  191. if (!$storage) {
  192. $storage = $this;
  193. }
  194. $this->propagator = new JailPropagator($storage, \OC::$server->getDatabaseConnection());
  195. return $this->propagator;
  196. }
  197. public function writeStream(string $path, $stream, ?int $size = null): int {
  198. $storage = $this->getWrapperStorage();
  199. if ($storage->instanceOfStorage(IWriteStreamStorage::class)) {
  200. /** @var IWriteStreamStorage $storage */
  201. return $storage->writeStream($this->getUnjailedPath($path), $stream, $size);
  202. } else {
  203. $target = $this->fopen($path, 'w');
  204. [$count, $result] = \OC_Helper::streamCopy($stream, $target);
  205. fclose($stream);
  206. fclose($target);
  207. return $count;
  208. }
  209. }
  210. public function getDirectoryContent(string $directory): \Traversable {
  211. return $this->getWrapperStorage()->getDirectoryContent($this->getUnjailedPath($directory));
  212. }
  213. }