-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathDAVController.php
More file actions
446 lines (380 loc) 路 16 KB
/
Copy pathDAVController.php
File metadata and controls
446 lines (380 loc) 路 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
namespace App\Controller;
use App\Entity\Principal;
use App\Entity\User;
use App\Plugins\BirthdayCalendarPlugin;
use App\Plugins\DavisIMipPlugin;
use App\Plugins\DavisTemporaryFileFilterPlugin;
use App\Plugins\PublicAwareDAVACLPlugin;
use App\Services\BasicAuth;
use App\Services\BirthdayService;
use App\Services\IMAPAuth;
use App\Services\LDAPAuth;
use Doctrine\ORM\EntityManagerInterface;
use PDO;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class DAVController extends AbstractController
{
public const AUTH_BASIC = 'Basic';
public const AUTH_IMAP = 'IMAP';
public const AUTH_LDAP = 'LDAP';
/**
* Is CalDAV enabled?
*
* @var bool
*/
protected $calDAVEnabled;
/**
* is CardDAV enabled?
*
* @var bool
*/
protected $cardDAVEnabled;
/**
* is WebDAV enabled?
*
* @var bool
*/
protected $webDAVEnabled;
/**
* Are public calendars enabled?
*
* @var bool
*/
protected $publicCalendarsEnabled;
/**
* Mail address to send mails from.
*
* @var string
*/
protected $inviteAddress;
/**
* Public directory of the Symfony installation.
* Needed to retrieve assets (images).
*
* @var string
*/
protected $publicDir;
/**
* WebDAV Public directory.
*
* @var string
*/
protected $webdavPublicDir;
/**
* WebDAV User Homes directory.
*
* @var string|null
*/
protected $webdavHomesDir;
/**
* WebDAV Temporary directory.
*
* @var string
*/
protected $webdavTmpDir;
/**
* Can every authenticated user write to the WebDAV public directory
* (otherwise only admins can, everybody can read).
*
* @var bool
*/
protected $webdavPublicDirWritable;
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* @var MailerInterface
*/
protected $mailer;
/**
* @var BirthdayService
*/
protected $birthdayService;
/**
* Base URI of the server.
*
* @var string
*/
protected $baseUri;
/**
* Basic Auth Backend class.
*
* @var BasicAuth
*/
protected $basicAuthBackend;
/**
* IMAP Auth Backend class.
*
* @var IMAPAuth
*/
protected $IMAPAuthBackend;
/**
* LDAP Auth Backend class.
*
* @var LDAPAuth
*/
protected $LDAPAuthBackend;
/**
* Logger for exceptions.
*
* @var Psr\Log\LoggerInterface;
*/
protected $logger;
/**
* Server.
*
* @var \Sabre\DAV\Server
*/
protected $server;
public function __construct(MailerInterface $mailer, BasicAuth $basicAuthBackend, IMAPAuth $IMAPAuthBackend, LDAPAuth $LDAPAuthBackend, UrlGeneratorInterface $router, EntityManagerInterface $entityManager, LoggerInterface $logger, BirthdayService $birthdayService, string $publicDir, bool $calDAVEnabled = true, bool $cardDAVEnabled = true, bool $webDAVEnabled = false, bool $publicCalendarsEnabled = true, ?string $inviteAddress = null, ?string $authMethod = null, ?string $authRealm = null, ?string $webdavPublicDir = null, ?string $webdavHomesDir = null, ?string $webdavTmpDir = null, bool $webdavPublicDirWritable = false)
{
$this->publicDir = $publicDir;
$this->calDAVEnabled = $calDAVEnabled;
$this->cardDAVEnabled = $cardDAVEnabled;
$this->webDAVEnabled = $webDAVEnabled;
$this->publicCalendarsEnabled = $publicCalendarsEnabled;
$this->inviteAddress = $inviteAddress ?? null;
$this->webdavPublicDir = $webdavPublicDir;
$this->webdavHomesDir = $webdavHomesDir;
$this->webdavTmpDir = $webdavTmpDir;
$this->webdavPublicDirWritable = $webdavPublicDirWritable;
$this->em = $entityManager;
$this->logger = $logger;
$this->mailer = $mailer;
$this->birthdayService = $birthdayService;
$this->baseUri = $router->generate('dav', ['path' => '']);
$this->basicAuthBackend = $basicAuthBackend;
$this->IMAPAuthBackend = $IMAPAuthBackend;
$this->LDAPAuthBackend = $LDAPAuthBackend;
$this->initServer($authMethod, $authRealm);
$this->initExceptionListener();
}
#[Route('/', name: 'home')]
public function home(): Response
{
return $this->render('index.html.twig', [
'version' => \App\Version::VERSION,
]);
}
private function initServer(string $authMethod, string $authRealm = User::DEFAULT_AUTH_REALM)
{
// Get the PDO Connection of type PDO
$pdo = $this->em->getConnection()->getNativeConnection();
/*
* The backends.
*/
switch ($authMethod) {
case self::AUTH_IMAP:
$authBackend = $this->IMAPAuthBackend;
break;
case self::AUTH_LDAP:
$authBackend = $this->LDAPAuthBackend;
break;
case self::AUTH_BASIC:
default:
$authBackend = $this->basicAuthBackend;
break;
}
$authBackend->setRealm($authRealm);
$principalBackend = new \Sabre\DAVACL\PrincipalBackend\PDO($pdo);
/**
* The directory tree.
*
* Basically this is an array which contains the 'top-level' directories in the
* WebDAV server.
*/
$nodes = [
// /principals
new \Sabre\CalDAV\Principal\Collection($principalBackend),
];
if ($this->webdavHomesDir) {
$this->assertWebdavDirectory($this->webdavHomesDir, 'WEBDAV_HOMES_DIR');
$nodes[] = new \Sabre\DAVACL\FS\HomeCollection($principalBackend, $this->webdavHomesDir);
}
if ($this->calDAVEnabled) {
$calendarBackend = new \Sabre\CalDAV\Backend\PDO($pdo);
$nodes[] = new \Sabre\CalDAV\CalendarRoot($principalBackend, $calendarBackend);
}
if ($this->cardDAVEnabled) {
$carddavBackend = new \Sabre\CardDAV\Backend\PDO($pdo);
$nodes[] = new \Sabre\CardDAV\AddressBookRoot($principalBackend, $carddavBackend);
}
if ($this->webDAVEnabled && $this->webdavTmpDir && $this->webdavPublicDir) {
$this->assertWebdavDirectory($this->webdavTmpDir, 'WEBDAV_TMP_DIR');
$this->assertWebdavDirectory($this->webdavPublicDir, 'WEBDAV_PUBLIC_DIR');
// Explicit ACL for the shared directory: every authenticated user can read it, and
// writing is reserved to admins (the ACL plugin grants them every privilege) unless
// WEBDAV_PUBLIC_DIR_WRITABLE opens it to everyone. Children inherit this ACL.
$publicDirAcl = [
['principal' => '{DAV:}authenticated', 'privilege' => '{DAV:}read', 'protected' => true],
];
if ($this->webdavPublicDirWritable) {
$publicDirAcl[] = ['principal' => '{DAV:}authenticated', 'privilege' => '{DAV:}write', 'protected' => true];
}
$nodes[] = new \Sabre\DAVACL\FS\Collection($this->webdavPublicDir, $publicDirAcl);
}
// The object tree needs in turn to be passed to the server class
$this->server = new \Sabre\DAV\Server($nodes);
$this->server->setBaseUri($this->baseUri);
// Plugins
$this->server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, $authRealm));
$this->server->addPlugin(new \Sabre\DAV\Browser\Plugin(false)); // We disable the file creation / upload / sharing in the browser
$this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
$aclPlugin = new PublicAwareDAVACLPlugin($this->em, $this->publicCalendarsEnabled);
$aclPlugin->hideNodesFromListings = true;
$aclPlugin->allowUnauthenticatedAccess = true; // Already the default, but setting it is future-proof
// Fetch admins, if any
$admins = $this->em->getRepository(Principal::class)->findBy(['isAdmin' => true]);
foreach ($admins as $principal) {
$aclPlugin->adminPrincipals[] = $principal->getUri();
}
$this->server->addPlugin($aclPlugin);
$this->server->addPlugin(new \Sabre\DAV\PropertyStorage\Plugin(
new \Sabre\DAV\PropertyStorage\Backend\PDO($pdo)
));
// CalDAV plugins
if ($this->calDAVEnabled) {
$this->server->addPlugin(new \Sabre\DAV\Sharing\Plugin());
$this->server->addPlugin(new \Sabre\CalDAV\Plugin());
$this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin());
$this->server->addPlugin(new \Sabre\CalDAV\SharingPlugin());
$this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
if ($this->inviteAddress) {
$this->server->addPlugin(new DavisIMipPlugin($this->mailer, $this->inviteAddress, $this->publicDir));
}
}
// CardDAV plugins
if ($this->cardDAVEnabled) {
$this->server->addPlugin(new \Sabre\CardDAV\Plugin());
$this->server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin());
}
if ($this->cardDAVEnabled && $this->calDAVEnabled) {
$this->server->addPlugin(new BirthdayCalendarPlugin($this->birthdayService, $calendarBackend));
}
// WebDAV plugins
if ($this->webDAVEnabled && $this->webdavTmpDir && $this->webdavPublicDir) {
$lockBackend = new \Sabre\DAV\Locks\Backend\File($this->webdavTmpDir.'/locksdb');
$this->server->addPlugin(new \Sabre\DAV\Locks\Plugin($lockBackend));
$this->server->addPlugin(new \Sabre\DAV\Browser\GuessContentType());
// Temporary files must obey the ACL of their directory (see the plugin for the why)
$this->server->addPlugin(new DavisTemporaryFileFilterPlugin($this->webdavTmpDir));
}
}
/**
* A WebDAV directory must exist, be given as an absolute path (a relative one would be
* resolved against the PHP process' working directory, which is not predictable) and
* must not live inside the web root, where the web server would serve its content
* directly and bypass every DAV permission check.
*/
private function assertWebdavDirectory(string $dir, string $envVar): void
{
if (!str_starts_with($dir, '/')) {
throw new \RuntimeException(sprintf('%s must be an absolute path, "%s" given.', $envVar, $dir));
}
$realDir = realpath($dir);
if (false === $realDir || !is_dir($realDir)) {
throw new \RuntimeException(sprintf('%s points to "%s", which does not exist or is not a directory. Make sure it is created with the correct permissions.', $envVar, $dir));
}
$webRoot = realpath($this->publicDir);
if (false !== $webRoot && ($realDir === $webRoot || str_starts_with($realDir.'/', $webRoot.'/'))) {
throw new \RuntimeException(sprintf('%s ("%s") must not be inside the web root ("%s"): the web server would serve these files without any permission check.', $envVar, $dir, $webRoot));
}
}
private function initExceptionListener()
{
$this->server->on('exception', function (\Throwable $e) {
// We don't need a trace for simple authentication exceptions
if ($e instanceof \Sabre\DAV\Exception\NotAuthenticated) {
$this->logger->warning('[401]: '.get_class($e)." - No 'Authorization: Basic' header found. Login was needed");
return;
}
$httpCode = ($e instanceof \Sabre\DAV\Exception) ? $e->getHTTPCode() : 500;
$this->logger->error('['.$httpCode.']: '.get_class($e).' - '.$e->getMessage(), $e->getTrace());
});
}
/**
* Service discovery (RFC 6764).
*
* This lives in the application rather than in each web server's configuration so that
* the redirect is built from the real base path: a hard-coded `/dav/` sends clients to
* the wrong place whenever Davis is installed under a sub-directory.
*/
#[Route('/.well-known/caldav', name: 'well_known_caldav')]
#[Route('/.well-known/carddav', name: 'well_known_carddav')]
public function wellKnown(): Response
{
return $this->redirectToRoute('dav', ['path' => ''], Response::HTTP_MOVED_PERMANENTLY);
}
#[Route('/dav/{path}', name: 'dav', requirements: ['path' => '.*'])]
public function dav(Request $request, ?string $path, ?Profiler $profiler = null)
{
// We don't want the toolbar on the /dav/* routes
if ($profiler instanceof Profiler) {
$profiler->disable();
}
// We need to acknowledge the OPTIONS call before sabre/dav for public
// calendars since we're circumventing the lib
if ('OPTIONS' === $request->getMethod()) {
$response = new Response();
// Adapted from CorePlugin's httpOptions()
// https://github.com/sabre-io/dav/blob/master/lib/DAV/CorePlugin.php#L210
//
// The methods depend on the node being asked about: MKCALENDAR, for instance, is
// only offered inside a calendar home. Answering for the root instead of the
// requested path told every client the same, incomplete story.
try {
$methods = $this->server->getAllowedMethods($path ?? '');
} catch (\Throwable $e) {
// An unresolvable path should still get a usable answer
$methods = $this->server->getAllowedMethods('');
}
$response->headers->set('Allow', strtoupper(implode(', ', $methods)));
$features = ['1', '3', 'extended-mkcol'];
foreach ($this->server->getPlugins() as $plugin) {
$features = array_merge($features, $plugin->getFeatures());
}
$response->headers->set('DAV', implode(', ', $features));
$response->headers->set('MS-Author-Via', 'DAV');
return $response;
}
// \Sabre\DAV\Server does not let us use a custom SAPI: it writes its status line and
// headers with header() and streams the body to php://output. We capture the output
// so that we can hand a proper Response back to Symfony (and keep its kernel events,
// like TERMINATE, working).
ob_start(); // Does not capture headers!
$this->server->start();
$output = ob_get_clean();
// Some plugins short-circuit a request by returning false from `beforeMethod` (the
// temporary file filter does, for .DS_Store and friends). sabre then never sends
// anything: status, headers and body only exist in its response object. So we always
// rebuild the Symfony response from that object, falling back to its body when
// nothing was streamed.
$sabreResponse = $this->server->httpResponse;
if ('' === $output) {
$body = $sabreResponse->getBody();
// A stream that sabre already sent has been closed (is_resource() is then false):
// only read bodies that were never streamed.
if (is_string($body) || (is_resource($body) && 'stream' === get_resource_type($body))) {
$output = $sabreResponse->getBodyAsString();
}
}
// Drop the headers sabre may already have queued with header(): Symfony re-sends the
// very same ones from the Response below, and would otherwise duplicate them.
header_remove();
$response = new Response($output, $sabreResponse->getStatus());
foreach ($sabreResponse->getHeaders() as $name => $values) {
$response->headers->set($name, $values);
}
return $response;
}
}