Server.php 14 KB

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