Server.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV;
  8. use OCA\DAV\AppInfo\PluginManager;
  9. use OCA\DAV\BulkUpload\BulkUploadPlugin;
  10. use OCA\DAV\CalDAV\BirthdayService;
  11. use OCA\DAV\CalDAV\Schedule\IMipPlugin;
  12. use OCA\DAV\CalDAV\Security\RateLimitingPlugin;
  13. use OCA\DAV\CardDAV\HasPhotoPlugin;
  14. use OCA\DAV\CardDAV\ImageExportPlugin;
  15. use OCA\DAV\CardDAV\MultiGetExportPlugin;
  16. use OCA\DAV\CardDAV\PhotoCache;
  17. use OCA\DAV\CardDAV\Security\CardDavRateLimitingPlugin;
  18. use OCA\DAV\Comments\CommentsPlugin;
  19. use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
  20. use OCA\DAV\Connector\Sabre\Auth;
  21. use OCA\DAV\Connector\Sabre\BearerAuth;
  22. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  23. use OCA\DAV\Connector\Sabre\CachingTree;
  24. use OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin;
  25. use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
  26. use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
  27. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  28. use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
  29. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  30. use OCA\DAV\Connector\Sabre\FilesPlugin;
  31. use OCA\DAV\Connector\Sabre\FilesReportPlugin;
  32. use OCA\DAV\Connector\Sabre\PropfindCompressionPlugin;
  33. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  34. use OCA\DAV\Connector\Sabre\RequestIdHeaderPlugin;
  35. use OCA\DAV\Connector\Sabre\SharesPlugin;
  36. use OCA\DAV\Connector\Sabre\TagsPlugin;
  37. use OCA\DAV\DAV\CustomPropertiesBackend;
  38. use OCA\DAV\DAV\PublicAuth;
  39. use OCA\DAV\DAV\ViewOnlyPlugin;
  40. use OCA\DAV\Events\SabrePluginAddEvent;
  41. use OCA\DAV\Events\SabrePluginAuthInitEvent;
  42. use OCA\DAV\Files\BrowserErrorPagePlugin;
  43. use OCA\DAV\Files\LazySearchBackend;
  44. use OCA\DAV\Profiler\ProfilerPlugin;
  45. use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
  46. use OCA\DAV\SystemTag\SystemTagPlugin;
  47. use OCA\DAV\Upload\ChunkingPlugin;
  48. use OCA\DAV\Upload\ChunkingV2Plugin;
  49. use OCP\AppFramework\Http\Response;
  50. use OCP\Diagnostics\IEventLogger;
  51. use OCP\EventDispatcher\IEventDispatcher;
  52. use OCP\FilesMetadata\IFilesMetadataManager;
  53. use OCP\ICacheFactory;
  54. use OCP\IRequest;
  55. use OCP\Profiler\IProfiler;
  56. use OCP\SabrePluginEvent;
  57. use Psr\Log\LoggerInterface;
  58. use Sabre\CardDAV\VCFExportPlugin;
  59. use Sabre\DAV\Auth\Plugin;
  60. use Sabre\DAV\UUIDUtil;
  61. use SearchDAV\DAV\SearchPlugin;
  62. class Server {
  63. private IRequest $request;
  64. private string $baseUri;
  65. public Connector\Sabre\Server $server;
  66. private IProfiler $profiler;
  67. public function __construct(IRequest $request, string $baseUri) {
  68. $this->profiler = \OC::$server->get(IProfiler::class);
  69. if ($this->profiler->isEnabled()) {
  70. /** @var IEventLogger $eventLogger */
  71. $eventLogger = \OC::$server->get(IEventLogger::class);
  72. $eventLogger->start('runtime', 'DAV Runtime');
  73. }
  74. $this->request = $request;
  75. $this->baseUri = $baseUri;
  76. $logger = \OC::$server->get(LoggerInterface::class);
  77. /** @var IEventDispatcher $dispatcher */
  78. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  79. $root = new RootCollection();
  80. $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));
  81. // Add maintenance plugin
  82. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig(), \OC::$server->getL10N('dav')));
  83. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\AppleQuirksPlugin());
  84. // Backends
  85. $authBackend = new Auth(
  86. \OC::$server->getSession(),
  87. \OC::$server->getUserSession(),
  88. \OC::$server->getRequest(),
  89. \OC::$server->getTwoFactorAuthManager(),
  90. \OC::$server->getBruteForceThrottler()
  91. );
  92. // Set URL explicitly due to reverse-proxy situations
  93. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  94. $this->server->setBaseUri($this->baseUri);
  95. $this->server->addPlugin(new ProfilerPlugin($this->request));
  96. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  97. $this->server->addPlugin(new AnonymousOptionsPlugin());
  98. $authPlugin = new Plugin();
  99. $authPlugin->addBackend(new PublicAuth());
  100. $this->server->addPlugin($authPlugin);
  101. // allow setup of additional auth backends
  102. $event = new SabrePluginEvent($this->server);
  103. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  104. $newAuthEvent = new SabrePluginAuthInitEvent($this->server);
  105. $dispatcher->dispatchTyped($newAuthEvent);
  106. $bearerAuthBackend = new BearerAuth(
  107. \OC::$server->getUserSession(),
  108. \OC::$server->getSession(),
  109. \OC::$server->getRequest()
  110. );
  111. $authPlugin->addBackend($bearerAuthBackend);
  112. // because we are throwing exceptions this plugin has to be the last one
  113. $authPlugin->addBackend($authBackend);
  114. // debugging
  115. if (\OC::$server->getConfig()->getSystemValue('debug', false)) {
  116. $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  117. } else {
  118. $this->server->addPlugin(new DummyGetResponsePlugin());
  119. }
  120. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  121. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  122. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  123. // acl
  124. $acl = new DavAclPlugin();
  125. $acl->principalCollectionSet = [
  126. 'principals/users',
  127. 'principals/groups',
  128. 'principals/calendar-resources',
  129. 'principals/calendar-rooms',
  130. ];
  131. $this->server->addPlugin($acl);
  132. // calendar plugins
  133. if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) {
  134. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  135. $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
  136. $this->server->addPlugin(new \OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin(\OC::$server->getConfig(), $logger));
  137. $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class)));
  138. $this->server->addPlugin(\OC::$server->get(\OCA\DAV\CalDAV\Trashbin\Plugin::class));
  139. $this->server->addPlugin(new \OCA\DAV\CalDAV\WebcalCaching\Plugin($request));
  140. if (\OC::$server->getConfig()->getAppValue('dav', 'allow_calendar_link_subscriptions', 'yes') === 'yes') {
  141. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  142. }
  143. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  144. $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
  145. \OC::$server->getConfig(),
  146. \OC::$server->getURLGenerator()
  147. ));
  148. $this->server->addPlugin(\OCP\Server::get(RateLimitingPlugin::class));
  149. }
  150. // addressbook plugins
  151. if ($this->requestIsForSubtree(['addressbooks', 'principals'])) {
  152. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  153. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  154. $this->server->addPlugin(new VCFExportPlugin());
  155. $this->server->addPlugin(new MultiGetExportPlugin());
  156. $this->server->addPlugin(new HasPhotoPlugin());
  157. $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
  158. \OC::$server->getAppDataDir('dav-photocache'),
  159. $logger)
  160. ));
  161. $this->server->addPlugin(\OCP\Server::get(CardDavRateLimitingPlugin::class));
  162. }
  163. // system tags plugins
  164. $this->server->addPlugin(\OC::$server->get(SystemTagPlugin::class));
  165. // comments plugin
  166. $this->server->addPlugin(new CommentsPlugin(
  167. \OC::$server->getCommentsManager(),
  168. \OC::$server->getUserSession()
  169. ));
  170. $this->server->addPlugin(new CopyEtagHeaderPlugin());
  171. $this->server->addPlugin(new RequestIdHeaderPlugin(\OC::$server->get(IRequest::class)));
  172. $this->server->addPlugin(new ChunkingV2Plugin(\OCP\Server::get(ICacheFactory::class)));
  173. $this->server->addPlugin(new ChunkingPlugin());
  174. // allow setup of additional plugins
  175. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event);
  176. $typedEvent = new SabrePluginAddEvent($this->server);
  177. $dispatcher->dispatchTyped($typedEvent);
  178. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  179. // we do not provide locking we emulate it using a fake locking plugin.
  180. if ($request->isUserAgent([
  181. '/WebDAVFS/',
  182. '/OneNote/',
  183. '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601
  184. ])) {
  185. $this->server->addPlugin(new FakeLockerPlugin());
  186. }
  187. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  188. $this->server->addPlugin(new BrowserErrorPagePlugin());
  189. }
  190. $lazySearchBackend = new LazySearchBackend();
  191. $this->server->addPlugin(new SearchPlugin($lazySearchBackend));
  192. // wait with registering these until auth is handled and the filesystem is setup
  193. $this->server->on('beforeMethod:*', function () use ($root, $lazySearchBackend, $logger) {
  194. // Allow view-only plugin for webdav requests
  195. $this->server->addPlugin(new ViewOnlyPlugin(
  196. \OC::$server->getUserFolder(),
  197. ));
  198. // custom properties plugin must be the last one
  199. $userSession = \OC::$server->getUserSession();
  200. $user = $userSession->getUser();
  201. if ($user !== null) {
  202. $view = \OC\Files\Filesystem::getView();
  203. $this->server->addPlugin(
  204. new FilesPlugin(
  205. $this->server->tree,
  206. \OC::$server->getConfig(),
  207. $this->request,
  208. \OC::$server->getPreviewManager(),
  209. \OC::$server->getUserSession(),
  210. false,
  211. !\OC::$server->getConfig()->getSystemValue('debug', false)
  212. )
  213. );
  214. $this->server->addPlugin(new ChecksumUpdatePlugin());
  215. $this->server->addPlugin(
  216. new \Sabre\DAV\PropertyStorage\Plugin(
  217. new CustomPropertiesBackend(
  218. $this->server,
  219. $this->server->tree,
  220. \OC::$server->getDatabaseConnection(),
  221. \OC::$server->getUserSession()->getUser()
  222. )
  223. )
  224. );
  225. if ($view !== null) {
  226. $this->server->addPlugin(
  227. new QuotaPlugin($view));
  228. }
  229. $this->server->addPlugin(
  230. new TagsPlugin(
  231. $this->server->tree, \OC::$server->getTagManager()
  232. )
  233. );
  234. // TODO: switch to LazyUserFolder
  235. $userFolder = \OC::$server->getUserFolder();
  236. $shareManager = \OCP\Server::get(\OCP\Share\IManager::class);
  237. $this->server->addPlugin(new SharesPlugin(
  238. $this->server->tree,
  239. $userSession,
  240. $userFolder,
  241. $shareManager,
  242. ));
  243. $this->server->addPlugin(new CommentPropertiesPlugin(
  244. \OC::$server->getCommentsManager(),
  245. $userSession
  246. ));
  247. if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
  248. $this->server->addPlugin(new IMipPlugin(
  249. \OC::$server->get(\OCP\IConfig::class),
  250. \OC::$server->get(\OCP\Mail\IMailer::class),
  251. \OC::$server->get(LoggerInterface::class),
  252. \OC::$server->get(\OCP\AppFramework\Utility\ITimeFactory::class),
  253. \OC::$server->get(\OCP\Defaults::class),
  254. $userSession,
  255. \OC::$server->get(\OCA\DAV\CalDAV\Schedule\IMipService::class),
  256. \OC::$server->get(\OCA\DAV\CalDAV\EventComparisonService::class)
  257. ));
  258. }
  259. $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
  260. if ($view !== null) {
  261. $this->server->addPlugin(new FilesReportPlugin(
  262. $this->server->tree,
  263. $view,
  264. \OC::$server->getSystemTagManager(),
  265. \OC::$server->getSystemTagObjectMapper(),
  266. \OC::$server->getTagManager(),
  267. $userSession,
  268. \OC::$server->getGroupManager(),
  269. $userFolder,
  270. \OC::$server->getAppManager()
  271. ));
  272. $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
  273. $this->server->tree,
  274. $user,
  275. \OC::$server->getRootFolder(),
  276. $shareManager,
  277. $view,
  278. \OCP\Server::get(IFilesMetadataManager::class)
  279. ));
  280. $this->server->addPlugin(
  281. new BulkUploadPlugin(
  282. $userFolder,
  283. $logger
  284. )
  285. );
  286. }
  287. $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
  288. \OC::$server->getConfig(),
  289. \OC::$server->query(BirthdayService::class),
  290. $user
  291. ));
  292. $this->server->addPlugin(new AppleProvisioningPlugin(
  293. \OC::$server->getUserSession(),
  294. \OC::$server->getURLGenerator(),
  295. \OC::$server->getThemingDefaults(),
  296. \OC::$server->getRequest(),
  297. \OC::$server->getL10N('dav'),
  298. function () {
  299. return UUIDUtil::getUUID();
  300. }
  301. ));
  302. }
  303. // register plugins from apps
  304. $pluginManager = new PluginManager(
  305. \OC::$server,
  306. \OC::$server->getAppManager()
  307. );
  308. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  309. $this->server->addPlugin($appPlugin);
  310. }
  311. foreach ($pluginManager->getAppCollections() as $appCollection) {
  312. $root->addChild($appCollection);
  313. }
  314. });
  315. $this->server->addPlugin(
  316. new PropfindCompressionPlugin()
  317. );
  318. }
  319. public function exec() {
  320. /** @var IEventLogger $eventLogger */
  321. $eventLogger = \OC::$server->get(IEventLogger::class);
  322. $eventLogger->start('dav_server_exec', '');
  323. $this->server->exec();
  324. $eventLogger->end('dav_server_exec');
  325. if ($this->profiler->isEnabled()) {
  326. $eventLogger->end('runtime');
  327. $profile = $this->profiler->collect(\OC::$server->get(IRequest::class), new Response());
  328. $this->profiler->saveProfile($profile);
  329. }
  330. }
  331. private function requestIsForSubtree(array $subTrees): bool {
  332. foreach ($subTrees as $subTree) {
  333. $subTree = trim($subTree, ' /');
  334. if (str_starts_with($this->server->getRequestUri(), $subTree . '/')) {
  335. return true;
  336. }
  337. }
  338. return false;
  339. }
  340. }