Server.php 14 KB

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