Server.php 14 KB

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