Server.php 14 KB

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