You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Server.php 14KB

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