Server.php 15 KB

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