Server.php 13 KB

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