Server.php 14 KB

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