Server.php 14 KB

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