Server.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2022 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Brandon Kirsch <brandonkirsch@github.com>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Georg Ehrke <oc.list@georgehrke.com>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author John Molakvoæ <skjnldsv@protonmail.com>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author Maxence Lange <maxence@artificial-owl.com>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Thomas Citharel <nextcloud@tcit.fr>
  19. * @author Thomas Müller <thomas.mueller@tmit.eu>
  20. * @author Vincent Petry <vincent@nextcloud.com>
  21. *
  22. * @license AGPL-3.0
  23. *
  24. * This code is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Affero General Public License, version 3,
  26. * as published by the Free Software Foundation.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU Affero General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Affero General Public License, version 3,
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>
  35. *
  36. */
  37. namespace OCA\DAV;
  38. use OCA\DAV\AppInfo\PluginManager;
  39. use OCA\DAV\BulkUpload\BulkUploadPlugin;
  40. use OCA\DAV\CalDAV\BirthdayService;
  41. use OCA\DAV\CalDAV\Security\RateLimitingPlugin;
  42. use OCA\DAV\CardDAV\HasPhotoPlugin;
  43. use OCA\DAV\CardDAV\ImageExportPlugin;
  44. use OCA\DAV\CardDAV\MultiGetExportPlugin;
  45. use OCA\DAV\CardDAV\PhotoCache;
  46. use OCA\DAV\Comments\CommentsPlugin;
  47. use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
  48. use OCA\DAV\Connector\Sabre\Auth;
  49. use OCA\DAV\Connector\Sabre\BearerAuth;
  50. use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
  51. use OCA\DAV\Connector\Sabre\CachingTree;
  52. use OCA\DAV\Connector\Sabre\ChecksumUpdatePlugin;
  53. use OCA\DAV\Connector\Sabre\CommentPropertiesPlugin;
  54. use OCA\DAV\Connector\Sabre\CopyEtagHeaderPlugin;
  55. use OCA\DAV\Connector\Sabre\DavAclPlugin;
  56. use OCA\DAV\Connector\Sabre\DummyGetResponsePlugin;
  57. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  58. use OCA\DAV\Connector\Sabre\FilesPlugin;
  59. use OCA\DAV\Connector\Sabre\FilesReportPlugin;
  60. use OCA\DAV\Connector\Sabre\PropfindCompressionPlugin;
  61. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  62. use OCA\DAV\Connector\Sabre\RequestIdHeaderPlugin;
  63. use OCA\DAV\Connector\Sabre\SharesPlugin;
  64. use OCA\DAV\Connector\Sabre\TagsPlugin;
  65. use OCA\DAV\DAV\CustomPropertiesBackend;
  66. use OCA\DAV\DAV\PublicAuth;
  67. use OCA\DAV\DAV\ViewOnlyPlugin;
  68. use OCA\DAV\Events\SabrePluginAddEvent;
  69. use OCA\DAV\Events\SabrePluginAuthInitEvent;
  70. use OCA\DAV\Files\BrowserErrorPagePlugin;
  71. use OCA\DAV\Files\LazySearchBackend;
  72. use OCA\DAV\Profiler\ProfilerPlugin;
  73. use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
  74. use OCA\DAV\SystemTag\SystemTagPlugin;
  75. use OCA\DAV\Upload\ChunkingPlugin;
  76. use OCA\DAV\Upload\ChunkingV2Plugin;
  77. use OCP\AppFramework\Http\Response;
  78. use OCP\Diagnostics\IEventLogger;
  79. use OCP\EventDispatcher\IEventDispatcher;
  80. use OCP\FilesMetadata\IFilesMetadataManager;
  81. use OCP\ICacheFactory;
  82. use OCP\IRequest;
  83. use OCP\Profiler\IProfiler;
  84. use OCP\SabrePluginEvent;
  85. use Psr\Log\LoggerInterface;
  86. use Sabre\CardDAV\VCFExportPlugin;
  87. use Sabre\DAV\Auth\Plugin;
  88. use Sabre\DAV\UUIDUtil;
  89. use SearchDAV\DAV\SearchPlugin;
  90. class Server {
  91. private IRequest $request;
  92. private string $baseUri;
  93. public Connector\Sabre\Server $server;
  94. private IProfiler $profiler;
  95. public function __construct(IRequest $request, string $baseUri) {
  96. $this->profiler = \OC::$server->get(IProfiler::class);
  97. if ($this->profiler->isEnabled()) {
  98. /** @var IEventLogger $eventLogger */
  99. $eventLogger = \OC::$server->get(IEventLogger::class);
  100. $eventLogger->start('runtime', 'DAV Runtime');
  101. }
  102. $this->request = $request;
  103. $this->baseUri = $baseUri;
  104. $logger = \OC::$server->get(LoggerInterface::class);
  105. /** @var IEventDispatcher $dispatcher */
  106. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  107. $root = new RootCollection();
  108. $this->server = new \OCA\DAV\Connector\Sabre\Server(new CachingTree($root));
  109. // Add maintenance plugin
  110. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\MaintenancePlugin(\OC::$server->getConfig(), \OC::$server->getL10N('dav')));
  111. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\AppleQuirksPlugin());
  112. // Backends
  113. $authBackend = new Auth(
  114. \OC::$server->getSession(),
  115. \OC::$server->getUserSession(),
  116. \OC::$server->getRequest(),
  117. \OC::$server->getTwoFactorAuthManager(),
  118. \OC::$server->getBruteForceThrottler()
  119. );
  120. // Set URL explicitly due to reverse-proxy situations
  121. $this->server->httpRequest->setUrl($this->request->getRequestUri());
  122. $this->server->setBaseUri($this->baseUri);
  123. $this->server->addPlugin(new ProfilerPlugin($this->request));
  124. $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
  125. $this->server->addPlugin(new AnonymousOptionsPlugin());
  126. $authPlugin = new Plugin();
  127. $authPlugin->addBackend(new PublicAuth());
  128. $this->server->addPlugin($authPlugin);
  129. // allow setup of additional auth backends
  130. $event = new SabrePluginEvent($this->server);
  131. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
  132. $newAuthEvent = new SabrePluginAuthInitEvent($this->server);
  133. $dispatcher->dispatchTyped($newAuthEvent);
  134. $bearerAuthBackend = new BearerAuth(
  135. \OC::$server->getUserSession(),
  136. \OC::$server->getSession(),
  137. \OC::$server->getRequest()
  138. );
  139. $authPlugin->addBackend($bearerAuthBackend);
  140. // because we are throwing exceptions this plugin has to be the last one
  141. $authPlugin->addBackend($authBackend);
  142. // debugging
  143. if (\OC::$server->getConfig()->getSystemValue('debug', false)) {
  144. $this->server->addPlugin(new \Sabre\DAV\Browser\Plugin());
  145. } else {
  146. $this->server->addPlugin(new DummyGetResponsePlugin());
  147. }
  148. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
  149. $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
  150. $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
  151. // acl
  152. $acl = new DavAclPlugin();
  153. $acl->principalCollectionSet = [
  154. 'principals/users',
  155. 'principals/groups',
  156. 'principals/calendar-resources',
  157. 'principals/calendar-rooms',
  158. ];
  159. $this->server->addPlugin($acl);
  160. // calendar plugins
  161. if ($this->requestIsForSubtree(['calendars', 'public-calendars', 'system-calendars', 'principals'])) {
  162. $this->server->addPlugin(new \OCA\DAV\CalDAV\Plugin());
  163. $this->server->addPlugin(new \OCA\DAV\CalDAV\ICSExportPlugin\ICSExportPlugin(\OC::$server->getConfig(), $logger));
  164. $this->server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class)));
  165. if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') {
  166. $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
  167. }
  168. $this->server->addPlugin(\OC::$server->get(\OCA\DAV\CalDAV\Trashbin\Plugin::class));
  169. $this->server->addPlugin(new \OCA\DAV\CalDAV\WebcalCaching\Plugin($request));
  170. if (\OC::$server->getConfig()->getAppValue('dav', 'allow_calendar_link_subscriptions', 'yes') === 'yes') {
  171. $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
  172. }
  173. $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
  174. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  175. $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin(
  176. \OC::$server->getConfig(),
  177. \OC::$server->getURLGenerator()
  178. ));
  179. $this->server->addPlugin(\OCP\Server::get(RateLimitingPlugin::class));
  180. }
  181. // addressbook plugins
  182. if ($this->requestIsForSubtree(['addressbooks', 'principals'])) {
  183. $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest(), \OC::$server->getConfig()));
  184. $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
  185. $this->server->addPlugin(new VCFExportPlugin());
  186. $this->server->addPlugin(new MultiGetExportPlugin());
  187. $this->server->addPlugin(new HasPhotoPlugin());
  188. $this->server->addPlugin(new ImageExportPlugin(new PhotoCache(
  189. \OC::$server->getAppDataDir('dav-photocache'),
  190. $logger)
  191. ));
  192. }
  193. // system tags plugins
  194. $this->server->addPlugin(\OC::$server->get(SystemTagPlugin::class));
  195. // comments plugin
  196. $this->server->addPlugin(new CommentsPlugin(
  197. \OC::$server->getCommentsManager(),
  198. \OC::$server->getUserSession()
  199. ));
  200. $this->server->addPlugin(new CopyEtagHeaderPlugin());
  201. $this->server->addPlugin(new RequestIdHeaderPlugin(\OC::$server->get(IRequest::class)));
  202. $this->server->addPlugin(new ChunkingV2Plugin(\OCP\Server::get(ICacheFactory::class)));
  203. $this->server->addPlugin(new ChunkingPlugin());
  204. // allow setup of additional plugins
  205. $dispatcher->dispatch('OCA\DAV\Connector\Sabre::addPlugin', $event);
  206. $typedEvent = new SabrePluginAddEvent($this->server);
  207. $dispatcher->dispatchTyped($typedEvent);
  208. // Some WebDAV clients do require Class 2 WebDAV support (locking), since
  209. // we do not provide locking we emulate it using a fake locking plugin.
  210. if ($request->isUserAgent([
  211. '/WebDAVFS/',
  212. '/OneNote/',
  213. '/^Microsoft-WebDAV/',// Microsoft-WebDAV-MiniRedir/6.1.7601
  214. ])) {
  215. $this->server->addPlugin(new FakeLockerPlugin());
  216. }
  217. // Allow view-only plugin for webdav requests
  218. $this->server->addPlugin(new ViewOnlyPlugin(
  219. \OC::$server->getUserFolder(),
  220. ));
  221. if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
  222. $this->server->addPlugin(new BrowserErrorPagePlugin());
  223. }
  224. $lazySearchBackend = new LazySearchBackend();
  225. $this->server->addPlugin(new SearchPlugin($lazySearchBackend));
  226. // wait with registering these until auth is handled and the filesystem is setup
  227. $this->server->on('beforeMethod:*', function () use ($root, $lazySearchBackend, $logger) {
  228. // custom properties plugin must be the last one
  229. $userSession = \OC::$server->getUserSession();
  230. $user = $userSession->getUser();
  231. if ($user !== null) {
  232. $view = \OC\Files\Filesystem::getView();
  233. $this->server->addPlugin(
  234. new FilesPlugin(
  235. $this->server->tree,
  236. \OC::$server->getConfig(),
  237. $this->request,
  238. \OC::$server->getPreviewManager(),
  239. \OC::$server->getUserSession(),
  240. false,
  241. !\OC::$server->getConfig()->getSystemValue('debug', false)
  242. )
  243. );
  244. $this->server->addPlugin(new ChecksumUpdatePlugin());
  245. $this->server->addPlugin(
  246. new \Sabre\DAV\PropertyStorage\Plugin(
  247. new CustomPropertiesBackend(
  248. $this->server->tree,
  249. \OC::$server->getDatabaseConnection(),
  250. \OC::$server->getUserSession()->getUser()
  251. )
  252. )
  253. );
  254. if ($view !== null) {
  255. $this->server->addPlugin(
  256. new QuotaPlugin($view));
  257. }
  258. $this->server->addPlugin(
  259. new TagsPlugin(
  260. $this->server->tree, \OC::$server->getTagManager()
  261. )
  262. );
  263. // TODO: switch to LazyUserFolder
  264. $userFolder = \OC::$server->getUserFolder();
  265. $this->server->addPlugin(new SharesPlugin(
  266. $this->server->tree,
  267. $userSession,
  268. $userFolder,
  269. \OC::$server->getShareManager()
  270. ));
  271. $this->server->addPlugin(new CommentPropertiesPlugin(
  272. \OC::$server->getCommentsManager(),
  273. $userSession
  274. ));
  275. $this->server->addPlugin(new \OCA\DAV\CalDAV\Search\SearchPlugin());
  276. if ($view !== null) {
  277. $this->server->addPlugin(new FilesReportPlugin(
  278. $this->server->tree,
  279. $view,
  280. \OC::$server->getSystemTagManager(),
  281. \OC::$server->getSystemTagObjectMapper(),
  282. \OC::$server->getTagManager(),
  283. $userSession,
  284. \OC::$server->getGroupManager(),
  285. $userFolder,
  286. \OC::$server->getAppManager()
  287. ));
  288. $lazySearchBackend->setBackend(new \OCA\DAV\Files\FileSearchBackend(
  289. $this->server->tree,
  290. $user,
  291. \OC::$server->getRootFolder(),
  292. \OC::$server->getShareManager(),
  293. $view,
  294. \OCP\Server::get(IFilesMetadataManager::class)
  295. ));
  296. $this->server->addPlugin(
  297. new BulkUploadPlugin(
  298. $userFolder,
  299. $logger
  300. )
  301. );
  302. }
  303. $this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
  304. \OC::$server->getConfig(),
  305. \OC::$server->query(BirthdayService::class),
  306. $user
  307. ));
  308. $this->server->addPlugin(new AppleProvisioningPlugin(
  309. \OC::$server->getUserSession(),
  310. \OC::$server->getURLGenerator(),
  311. \OC::$server->getThemingDefaults(),
  312. \OC::$server->getRequest(),
  313. \OC::$server->getL10N('dav'),
  314. function () {
  315. return UUIDUtil::getUUID();
  316. }
  317. ));
  318. }
  319. // register plugins from apps
  320. $pluginManager = new PluginManager(
  321. \OC::$server,
  322. \OC::$server->getAppManager()
  323. );
  324. foreach ($pluginManager->getAppPlugins() as $appPlugin) {
  325. $this->server->addPlugin($appPlugin);
  326. }
  327. foreach ($pluginManager->getAppCollections() as $appCollection) {
  328. $root->addChild($appCollection);
  329. }
  330. });
  331. $this->server->addPlugin(
  332. new PropfindCompressionPlugin()
  333. );
  334. }
  335. public function exec() {
  336. /** @var IEventLogger $eventLogger */
  337. $eventLogger = \OC::$server->get(IEventLogger::class);
  338. $eventLogger->start('dav_server_exec', '');
  339. $this->server->exec();
  340. $eventLogger->end('dav_server_exec');
  341. if ($this->profiler->isEnabled()) {
  342. $eventLogger->end('runtime');
  343. $profile = $this->profiler->collect(\OC::$server->get(IRequest::class), new Response());
  344. $this->profiler->saveProfile($profile);
  345. }
  346. }
  347. private function requestIsForSubtree(array $subTrees): bool {
  348. foreach ($subTrees as $subTree) {
  349. $subTree = trim($subTree, ' /');
  350. if (str_starts_with($this->server->getRequestUri(), $subTree . '/')) {
  351. return true;
  352. }
  353. }
  354. return false;
  355. }
  356. }