Server.php 14 KB

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