Server.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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\ErrorPagePlugin;
  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. $this->server->addPlugin(new ErrorPagePlugin($this->request, \OC::$server->getConfig()));
  197. $lazySearchBackend = new LazySearchBackend();
  198. $this->server->addPlugin(new SearchPlugin($lazySearchBackend));
  199. // wait with registering these until auth is handled and the filesystem is setup
  200. $this->server->on('beforeMethod:*', function () use ($root, $lazySearchBackend, $logger) {
  201. // Allow view-only plugin for webdav requests
  202. $this->server->addPlugin(new ViewOnlyPlugin(
  203. \OC::$server->getUserFolder(),
  204. ));
  205. // custom properties plugin must be the last one
  206. $userSession = \OC::$server->getUserSession();
  207. $user = $userSession->getUser();
  208. if ($user !== null) {
  209. $view = \OC\Files\Filesystem::getView();
  210. $config = \OCP\Server::get(IConfig::class);
  211. $this->server->addPlugin(
  212. new FilesPlugin(
  213. $this->server->tree,
  214. $config,
  215. $this->request,
  216. \OCP\Server::get(IPreview::class),
  217. \OCP\Server::get(IUserSession::class),
  218. \OCP\Server::get(IFilenameValidator::class),
  219. false,
  220. $config->getSystemValueBool('debug', false) === false,
  221. )
  222. );
  223. $this->server->addPlugin(new ChecksumUpdatePlugin());
  224. $this->server->addPlugin(
  225. new \Sabre\DAV\PropertyStorage\Plugin(
  226. new CustomPropertiesBackend(
  227. $this->server,
  228. $this->server->tree,
  229. \OC::$server->getDatabaseConnection(),
  230. \OC::$server->getUserSession()->getUser(),
  231. \OC::$server->get(DefaultCalendarValidator::class),
  232. )
  233. )
  234. );
  235. if ($view !== null) {
  236. $this->server->addPlugin(
  237. new QuotaPlugin($view));
  238. }
  239. $this->server->addPlugin(
  240. new TagsPlugin(
  241. $this->server->tree, \OC::$server->getTagManager()
  242. )
  243. );
  244. // TODO: switch to LazyUserFolder
  245. $userFolder = \OC::$server->getUserFolder();
  246. $shareManager = \OCP\Server::get(\OCP\Share\IManager::class);
  247. $this->server->addPlugin(new SharesPlugin(
  248. $this->server->tree,
  249. $userSession,
  250. $userFolder,
  251. $shareManager,
  252. ));
  253. $this->server->addPlugin(new CommentPropertiesPlugin(
  254. \OC::$server->getCommentsManager(),
  255. $userSession
  256. ));
  257. if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
  258. $this->server->addPlugin(new IMipPlugin(
  259. \OC::$server->get(\OCP\IConfig::class),
  260. \OC::$server->get(\OCP\Mail\IMailer::class),
  261. \OC::$server->get(LoggerInterface::class),
  262. \OC::$server->get(\OCP\AppFramework\Utility\ITimeFactory::class),
  263. \OC::$server->get(\OCP\Defaults::class),
  264. $userSession,
  265. \OC::$server->get(\OCA\DAV\CalDAV\Schedule\IMipService::class),
  266. \OC::$server->get(\OCA\DAV\CalDAV\EventComparisonService::class),
  267. \OC::$server->get(\OCP\Mail\Provider\IManager::class)
  268. ));
  269. }
  270. $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
  271. if ($view !== null) {
  272. $this->server->addPlugin(new FilesReportPlugin(
  273. $this->server->tree,
  274. $view,
  275. \OC::$server->getSystemTagManager(),
  276. \OC::$server->getSystemTagObjectMapper(),
  277. \OC::$server->getTagManager(),
  278. $userSession,
  279. \OC::$server->getGroupManager(),
  280. $userFolder,
  281. \OC::$server->getAppManager()
  282. ));
  283. $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
  284. $this->server->tree,
  285. $user,
  286. \OC::$server->getRootFolder(),
  287. $shareManager,
  288. $view,
  289. \OCP\Server::get(IFilesMetadataManager::class)
  290. ));
  291. $this->server->addPlugin(
  292. new BulkUploadPlugin(
  293. $userFolder,
  294. $logger
  295. )
  296. );
  297. }
  298. $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
  299. \OC::$server->getConfig(),
  300. \OC::$server->query(BirthdayService::class),
  301. $user
  302. ));
  303. $this->server->addPlugin(new AppleProvisioningPlugin(
  304. \OC::$server->getUserSession(),
  305. \OC::$server->getURLGenerator(),
  306. \OC::$server->getThemingDefaults(),
  307. \OC::$server->getRequest(),
  308. \OC::$server->getL10N('dav'),
  309. function () {
  310. return UUIDUtil::getUUID();
  311. }
  312. ));
  313. }
  314. // register plugins from apps
  315. $pluginManager = new PluginManager(
  316. \OC::$server,
  317. \OC::$server->getAppManager()
  318. );
  319. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  320. $this->server->addPlugin($appPlugin);
  321. }
  322. foreach ($pluginManager->getAppCollections() as $appCollection) {
  323. $root->addChild($appCollection);
  324. }
  325. });
  326. $this->server->addPlugin(
  327. new PropfindCompressionPlugin()
  328. );
  329. }
  330. public function exec() {
  331. /** @var IEventLogger $eventLogger */
  332. $eventLogger = \OC::$server->get(IEventLogger::class);
  333. $eventLogger->start('dav_server_exec', '');
  334. $this->server->start();
  335. $eventLogger->end('dav_server_exec');
  336. if ($this->profiler->isEnabled()) {
  337. $eventLogger->end('runtime');
  338. $profile = $this->profiler->collect(\OC::$server->get(IRequest::class), new Response());
  339. $this->profiler->saveProfile($profile);
  340. }
  341. }
  342. private function requestIsForSubtree(array $subTrees): bool {
  343. foreach ($subTrees as $subTree) {
  344. $subTree = trim($subTree, ' /');
  345. if (str_starts_with($this->server->getRequestUri(), $subTree . '/')) {
  346. return true;
  347. }
  348. }
  349. return false;
  350. }
  351. }