Server.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2022 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Brandon Kirsch <brandonkirsch@github.com>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Georg Ehrke <oc.list@georgehrke.com>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author John Molakvoæ <skjnldsv@protonmail.com>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Maxence Lange <maxence@artificial-owl.com>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Thomas Citharel <nextcloud@tcit.fr>
  19. * @author Thomas Müller <thomas.mueller@tmit.eu>
  20. * @author Vincent Petry <vincent@nextcloud.com>
  21. *
  22. * @license AGPL-3.0
  23. *
  24. * This code is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Affero General Public License, version 3,
  26. * as published by the Free Software Foundation.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU Affero General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Affero General Public License, version 3,
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>
  35. *
  36. */
  37. namespace OCA\DAV;
  38. use OCA\DAV\AppInfo\PluginManager;
  39. use OCA\DAV\BulkUpload\BulkUploadPlugin;
  40. use OCA\DAV\CalDAV\BirthdayService;
  41. use OCA\DAV\CalDAV\Security\RateLimitingPlugin;
  42. use OCA\DAV\CardDAV\HasPhotoPlugin;
  43. use OCA\DAV\CardDAV\ImageExportPlugin;
  44. use OCA\DAV\CardDAV\MultiGetExportPlugin;
  45. use OCA\DAV\CardDAV\PhotoCache;
  46. use OCA\DAV\CardDAV\Security\CardDavRateLimitingPlugin;
  47. use OCA\DAV\Comments\CommentsPlugin;
  48. use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
  49. use OCA\DAV\Connector\Sabre\Auth;
  50. use OCA\DAV\Connector\Sabre\BearerAuth;
  51. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  52. use OCA\DAV\Connector\Sabre\CachingTree;
  53. use OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin;
  54. use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
  55. use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
  56. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  57. use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
  58. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  59. use OCA\DAV\Connector\Sabre\FilesPlugin;
  60. use OCA\DAV\Connector\Sabre\FilesReportPlugin;
  61. use OCA\DAV\Connector\Sabre\PropfindCompressionPlugin;
  62. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  63. use OCA\DAV\Connector\Sabre\RequestIdHeaderPlugin;
  64. use OCA\DAV\Connector\Sabre\SharesPlugin;
  65. use OCA\DAV\Connector\Sabre\TagsPlugin;
  66. use OCA\DAV\DAV\CustomPropertiesBackend;
  67. use OCA\DAV\DAV\PublicAuth;
  68. use OCA\DAV\DAV\ViewOnlyPlugin;
  69. use OCA\DAV\Events\SabrePluginAddEvent;
  70. use OCA\DAV\Events\SabrePluginAuthInitEvent;
  71. use OCA\DAV\Files\BrowserErrorPagePlugin;
  72. use OCA\DAV\Files\LazySearchBackend;
  73. use OCA\DAV\Profiler\ProfilerPlugin;
  74. use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
  75. use OCA\DAV\SystemTag\SystemTagPlugin;
  76. use OCA\DAV\Upload\ChunkingPlugin;
  77. use OCA\DAV\Upload\ChunkingV2Plugin;
  78. use OCP\AppFramework\Http\Response;
  79. use OCP\Diagnostics\IEventLogger;
  80. use OCP\EventDispatcher\IEventDispatcher;
  81. use OCP\FilesMetadata\IFilesMetadataManager;
  82. use OCP\ICacheFactory;
  83. use OCP\IRequest;
  84. use OCP\Profiler\IProfiler;
  85. use OCP\SabrePluginEvent;
  86. use Psr\Log\LoggerInterface;
  87. use Sabre\CardDAV\VCFExportPlugin;
  88. use Sabre\DAV\Auth\Plugin;
  89. use Sabre\DAV\UUIDUtil;
  90. use SearchDAV\DAV\SearchPlugin;
  91. class Server {
  92. private IRequest $request;
  93. private string $baseUri;
  94. public Connector\Sabre\Server $server;
  95. private IProfiler $profiler;
  96. public function __construct(IRequest $request, string $baseUri) {
  97. $this->profiler = \OC::$server->get(IProfiler::class);
  98. if ($this->profiler->isEnabled()) {
  99. /** @var IEventLogger $eventLogger */
  100. $eventLogger = \OC::$server->get(IEventLogger::class);
  101. $eventLogger->start('runtime', 'DAV Runtime');
  102. }
  103. $this->request = $request;
  104. $this->baseUri = $baseUri;
  105. $logger = \OC::$server->get(LoggerInterface::class);
  106. /** @var IEventDispatcher $dispatcher */
  107. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  108. $root = new RootCollection();
  109. $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));
  110. // Add maintenance plugin
  111. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig(), \OC::$server->getL10N('dav')));
  112. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\AppleQuirksPlugin());
  113. // Backends
  114. $authBackend = new Auth(
  115. \OC::$server->getSession(),
  116. \OC::$server->getUserSession(),
  117. \OC::$server->getRequest(),
  118. \OC::$server->getTwoFactorAuthManager(),
  119. \OC::$server->getBruteForceThrottler()
  120. );
  121. // Set URL explicitly due to reverse-proxy situations
  122. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  123. $this->server->setBaseUri($this->baseUri);
  124. $this->server->addPlugin(new ProfilerPlugin($this->request));
  125. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  126. $this->server->addPlugin(new AnonymousOptionsPlugin());
  127. $authPlugin = new Plugin();
  128. $authPlugin->addBackend(new PublicAuth());
  129. $this->server->addPlugin($authPlugin);
  130. // allow setup of additional auth backends
  131. $event = new SabrePluginEvent($this->server);
  132. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  133. $newAuthEvent = new SabrePluginAuthInitEvent($this->server);
  134. $dispatcher->dispatchTyped($newAuthEvent);
  135. $bearerAuthBackend = new BearerAuth(
  136. \OC::$server->getUserSession(),
  137. \OC::$server->getSession(),
  138. \OC::$server->getRequest()
  139. );
  140. $authPlugin->addBackend($bearerAuthBackend);
  141. // because we are throwing exceptions this plugin has to be the last one
  142. $authPlugin->addBackend($authBackend);
  143. // debugging
  144. if (\OC::$server->getConfig()->getSystemValue('debug', false)) {
  145. $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  146. } else {
  147. $this->server->addPlugin(new DummyGetResponsePlugin());
  148. }
  149. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  150. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  151. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  152. // acl
  153. $acl = new DavAclPlugin();
  154. $acl->principalCollectionSet = [
  155. 'principals/users',
  156. 'principals/groups',
  157. 'principals/calendar-resources',
  158. 'principals/calendar-rooms',
  159. ];
  160. $this->server->addPlugin($acl);
  161. // calendar plugins
  162. if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) {
  163. $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
  164. $this->server->addPlugin(new \OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin(\OC::$server->getConfig(), $logger));
  165. $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class)));
  166. if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
  167. $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
  168. }
  169. $this->server->addPlugin(\OC::$server->get(\OCA\DAV\CalDAV\Trashbin\Plugin::class));
  170. $this->server->addPlugin(new \OCA\DAV\CalDAV\WebcalCaching\Plugin($request));
  171. if (\OC::$server->getConfig()->getAppValue('dav', 'allow_calendar_link_subscriptions', 'yes') === 'yes') {
  172. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  173. }
  174. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  175. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  176. $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
  177. \OC::$server->getConfig(),
  178. \OC::$server->getURLGenerator()
  179. ));
  180. $this->server->addPlugin(\OCP\Server::get(RateLimitingPlugin::class));
  181. }
  182. // addressbook plugins
  183. if ($this->requestIsForSubtree(['addressbooks', 'principals'])) {
  184. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  185. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  186. $this->server->addPlugin(new VCFExportPlugin());
  187. $this->server->addPlugin(new MultiGetExportPlugin());
  188. $this->server->addPlugin(new HasPhotoPlugin());
  189. $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
  190. \OC::$server->getAppDataDir('dav-photocache'),
  191. $logger)
  192. ));
  193. $this->server->addPlugin(\OCP\Server::get(CardDavRateLimitingPlugin::class));
  194. }
  195. // system tags plugins
  196. $this->server->addPlugin(\OC::$server->get(SystemTagPlugin::class));
  197. // comments plugin
  198. $this->server->addPlugin(new CommentsPlugin(
  199. \OC::$server->getCommentsManager(),
  200. \OC::$server->getUserSession()
  201. ));
  202. $this->server->addPlugin(new CopyEtagHeaderPlugin());
  203. $this->server->addPlugin(new RequestIdHeaderPlugin(\OC::$server->get(IRequest::class)));
  204. $this->server->addPlugin(new ChunkingV2Plugin(\OCP\Server::get(ICacheFactory::class)));
  205. $this->server->addPlugin(new ChunkingPlugin());
  206. // allow setup of additional plugins
  207. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event);
  208. $typedEvent = new SabrePluginAddEvent($this->server);
  209. $dispatcher->dispatchTyped($typedEvent);
  210. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  211. // we do not provide locking we emulate it using a fake locking plugin.
  212. if ($request->isUserAgent([
  213. '/WebDAVFS/',
  214. '/OneNote/',
  215. '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601
  216. ])) {
  217. $this->server->addPlugin(new FakeLockerPlugin());
  218. }
  219. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  220. $this->server->addPlugin(new BrowserErrorPagePlugin());
  221. }
  222. $lazySearchBackend = new LazySearchBackend();
  223. $this->server->addPlugin(new SearchPlugin($lazySearchBackend));
  224. // wait with registering these until auth is handled and the filesystem is setup
  225. $this->server->on('beforeMethod:*', function () use ($root, $lazySearchBackend, $logger) {
  226. // Allow view-only plugin for webdav requests
  227. $this->server->addPlugin(new ViewOnlyPlugin(
  228. \OC::$server->getUserFolder(),
  229. ));
  230. // custom properties plugin must be the last one
  231. $userSession = \OC::$server->getUserSession();
  232. $user = $userSession->getUser();
  233. if ($user !== null) {
  234. $view = \OC\Files\Filesystem::getView();
  235. $this->server->addPlugin(
  236. new FilesPlugin(
  237. $this->server->tree,
  238. \OC::$server->getConfig(),
  239. $this->request,
  240. \OC::$server->getPreviewManager(),
  241. \OC::$server->getUserSession(),
  242. false,
  243. !\OC::$server->getConfig()->getSystemValue('debug', false)
  244. )
  245. );
  246. $this->server->addPlugin(new ChecksumUpdatePlugin());
  247. $this->server->addPlugin(
  248. new \Sabre\DAV\PropertyStorage\Plugin(
  249. new CustomPropertiesBackend(
  250. $this->server->tree,
  251. \OC::$server->getDatabaseConnection(),
  252. \OC::$server->getUserSession()->getUser()
  253. )
  254. )
  255. );
  256. if ($view !== null) {
  257. $this->server->addPlugin(
  258. new QuotaPlugin($view));
  259. }
  260. $this->server->addPlugin(
  261. new TagsPlugin(
  262. $this->server->tree, \OC::$server->getTagManager()
  263. )
  264. );
  265. // TODO: switch to LazyUserFolder
  266. $userFolder = \OC::$server->getUserFolder();
  267. $shareManager = \OCP\Server::get(\OCP\Share\IManager::class);
  268. $this->server->addPlugin(new SharesPlugin(
  269. $this->server->tree,
  270. $userSession,
  271. $userFolder,
  272. $shareManager,
  273. ));
  274. $this->server->addPlugin(new CommentPropertiesPlugin(
  275. \OC::$server->getCommentsManager(),
  276. $userSession
  277. ));
  278. $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
  279. if ($view !== null) {
  280. $this->server->addPlugin(new FilesReportPlugin(
  281. $this->server->tree,
  282. $view,
  283. \OC::$server->getSystemTagManager(),
  284. \OC::$server->getSystemTagObjectMapper(),
  285. \OC::$server->getTagManager(),
  286. $userSession,
  287. \OC::$server->getGroupManager(),
  288. $userFolder,
  289. \OC::$server->getAppManager()
  290. ));
  291. $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
  292. $this->server->tree,
  293. $user,
  294. \OC::$server->getRootFolder(),
  295. $shareManager,
  296. $view,
  297. \OCP\Server::get(IFilesMetadataManager::class)
  298. ));
  299. $this->server->addPlugin(
  300. new BulkUploadPlugin(
  301. $userFolder,
  302. $logger
  303. )
  304. );
  305. }
  306. $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
  307. \OC::$server->getConfig(),
  308. \OC::$server->query(BirthdayService::class),
  309. $user
  310. ));
  311. $this->server->addPlugin(new AppleProvisioningPlugin(
  312. \OC::$server->getUserSession(),
  313. \OC::$server->getURLGenerator(),
  314. \OC::$server->getThemingDefaults(),
  315. \OC::$server->getRequest(),
  316. \OC::$server->getL10N('dav'),
  317. function () {
  318. return UUIDUtil::getUUID();
  319. }
  320. ));
  321. }
  322. // register plugins from apps
  323. $pluginManager = new PluginManager(
  324. \OC::$server,
  325. \OC::$server->getAppManager()
  326. );
  327. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  328. $this->server->addPlugin($appPlugin);
  329. }
  330. foreach ($pluginManager->getAppCollections() as $appCollection) {
  331. $root->addChild($appCollection);
  332. }
  333. });
  334. $this->server->addPlugin(
  335. new PropfindCompressionPlugin()
  336. );
  337. }
  338. public function exec() {
  339. /** @var IEventLogger $eventLogger */
  340. $eventLogger = \OC::$server->get(IEventLogger::class);
  341. $eventLogger->start('dav_server_exec', '');
  342. $this->server->exec();
  343. $eventLogger->end('dav_server_exec');
  344. if ($this->profiler->isEnabled()) {
  345. $eventLogger->end('runtime');
  346. $profile = $this->profiler->collect(\OC::$server->get(IRequest::class), new Response());
  347. $this->profiler->saveProfile($profile);
  348. }
  349. }
  350. private function requestIsForSubtree(array $subTrees): bool {
  351. foreach ($subTrees as $subTree) {
  352. $subTree = trim($subTree, ' /');
  353. if (str_starts_with($this->server->getRequestUri(), $subTree . '/')) {
  354. return true;
  355. }
  356. }
  357. return false;
  358. }
  359. public function getSabreServer(): Connector\Sabre\Server {
  360. return $this->server;
  361. }
  362. }