Server.php 14 KB

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