This repository has been archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
/
config.py
693 lines (589 loc) · 22.6 KB
/
config.py
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
##
# Copyright (c) 2005-2007 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
__all__ = [
"defaultConfigFile",
"defaultConfig",
"ConfigDict",
"Config",
"ConfigurationError",
"config",
]
import os
import copy
import re
try:
from plistlib import readPlist
except ImportError:
from twistedcaldav.py.plistlib import readPlist
from twisted.web2.dav import davxml
from twisted.web2.dav.resource import TwistedACLInheritable
from twistedcaldav.log import Logger
from twistedcaldav.log import clearLogLevels, setLogLevelForNamespace, InvalidLogLevelError
log = Logger()
class ConfigDict (dict):
def __init__(self, mapping=None):
if mapping is not None:
for key, value in mapping.iteritems():
self[key] = value
def __repr__(self):
return "*" + dict.__repr__(self)
def __setitem__(self, key, value):
if isinstance(value, dict) and not isinstance(value, self.__class__):
dict.__setitem__(self, key, self.__class__(value))
else:
dict.__setitem__(self, key, value)
def __setattr__(self, attr, value):
if attr[0] == "_":
dict.__setattr__(self, attr, value)
else:
self[attr] = value
def __getattr__(self, attr):
if attr in self:
return self[attr]
else:
return dict.__getattr__(self, attr)
defaultConfigFile = "/etc/caldavd/caldavd.plist"
serviceDefaultParams = {
"twistedcaldav.directory.xmlfile.XMLDirectoryService": {
"xmlFile": "/etc/caldavd/accounts.xml",
},
"twistedcaldav.directory.appleopendirectory.OpenDirectoryService": {
"node": "/Search",
"restrictEnabledRecords": False,
"restrictToGroup": "",
"cacheTimeout": 30,
},
}
defaultConfig = {
#
# Public network address information
#
# This is the server's public network address, which is provided to
# clients in URLs and the like. It may or may not be the network
# address that the server is listening to directly, though it is by
# default. For example, it may be the address of a load balancer or
# proxy which forwards connections to the server.
#
"ServerHostName": "", # Network host name.
"HTTPPort": 0, # HTTP port (0 to disable HTTP)
"SSLPort" : 0, # SSL port (0 to disable HTTPS)
# Note: we'd use None above, but that confuses the command-line parser.
#
# Network address configuration information
#
# This configures the actual network address that the server binds to.
#
"BindAddresses": [], # List of IP addresses to bind to [empty = all]
"BindHTTPPorts": [], # List of port numbers to bind to for HTTP [empty = same as "Port"]
"BindSSLPorts" : [], # List of port numbers to bind to for SSL [empty = same as "SSLPort"]
#
# Data store
#
"DataRoot" : "/Library/CalendarServer/Data",
"DocumentRoot" : "/Library/CalendarServer/Documents",
"UserQuota" : 104857600, # User quota (in bytes)
"MaximumAttachmentSize": 1048576, # Attachment size limit (in bytes)
#
# Directory service
#
# A directory service provides information about principals (eg.
# users, groups, locations and resources) to the server.
#
"DirectoryService": {
"type": "twistedcaldav.directory.xmlfile.XMLDirectoryService",
"params": serviceDefaultParams["twistedcaldav.directory.xmlfile.XMLDirectoryService"],
},
#
# Special principals
#
"AdminPrincipals": [], # Principals with "DAV:all" access (relative URLs)
"ReadPrincipals": [], # Principals with "DAV:read" access (relative URLs)
"SudoersFile": "/etc/caldavd/sudoers.plist", # Principals that can pose as other principals
"EnableProxyPrincipals": True, # Create "proxy access" principals
#
# Permissions
#
"EnableAnonymousReadRoot": True, # Allow unauthenticated read access to /
"EnableAnonymousReadNav": False, # Allow unauthenticated read access to hierachcy
"EnablePrincipalListings": True, # Allow listing of principal collections
"EnableMonolithicCalendars": True, # Render calendar collections as a monolithic iCalendar object
#
# Client controls
#
"RejectClients": [], # List of regexes for clients to disallow
#
# Authentication
#
"Authentication": {
"Basic": { "Enabled": False }, # Clear text; best avoided
"Digest": { # Digest challenge/response
"Enabled": True,
"Algorithm": "md5",
"Qop": "",
},
"Kerberos": { # Kerberos/SPNEGO
"Enabled": False,
"ServicePrincipal": ""
},
"Wiki": {
"Enabled": True,
"Cookie": "sessionID",
"URL": "http://127.0.0.1/RPC2",
"UserMethod": "userForSession",
"WikiMethod": "accessLevelForUserWikiCalendar",
},
},
#
# Logging
#
"AccessLogFile" : "/var/log/caldavd/access.log", # Apache-style access log
"ErrorLogFile" : "/var/log/caldavd/error.log", # Server activity log
"ServerStatsFile": "/var/run/caldavd/stats.plist",
"PIDFile" : "/var/run/caldavd.pid",
"RotateAccessLog" : False,
"MoreAccessLogData" : False,
"DefaultLogLevel" : "",
"LogLevels" : {},
"AccountingCategories": {
"iTIP": False,
},
"AccountingPrincipals": [],
"AccountingLogRoot": "/var/log/caldavd/accounting",
#
# SSL/TLS
#
"SSLCertificate" : "", # Public key
"SSLPrivateKey" : "", # Private key
"SSLAuthorityChain" : "", # Certificate Authority Chain
"SSLPassPhraseDialog": "/etc/apache2/getsslpassphrase",
"SSLCertAdmin" : "/usr/sbin/certadmin",
#
# Process management
#
# Username and Groupname to drop privileges to, if empty privileges will
# not be dropped.
"UserName": "",
"GroupName": "",
"ProcessType": "Combined",
"MultiProcess": {
"ProcessCount": 0,
"LoadBalancer": {
"Enabled": True,
"Scheduler": "LeastConnections",
},
"StaggeredStartup": {
"Enabled": False,
"Interval": 15,
},
},
#
# Service ACLs
#
"EnableSACLs": False,
#
# Non-standard CalDAV extensions
#
"EnableDropBox" : False, # Calendar Drop Box
"EnablePrivateEvents" : False, # Private Events
"EnableTimezoneService" : False, # Timezone service
#
# Scheduling related options
#
"Scheduling": {
"CalDAV": {
"EmailDomain" : "", # Domain for mailto calendar user addresses on this server
"HTTPDomain" : "", # Domain for http calendar user addresses on this server
"AddressPatterns" : [], # Reg-ex patterns to match local calendar user addresses
"OldDraftCompatibility" : True, # Whether to maintain compatibility with non-implicit mode
"ScheduleTagCompatibility" : True, # Whether to support older clients that do not use Schedule-Tag feature
"DefaultCalendarProvisioned" : True, # Whether the provisioned default calendar is marked as the scheduling default
"EnablePrivateComments" : True, # Private comments from attendees to organizer
},
"iSchedule": {
"Enabled" : False, # iSchedule protocol
"AddressPatterns" : [], # Reg-ex patterns to match iSchedule-able calendar user addresses
"Servers" : "/etc/caldavd/servertoserver.xml", # iSchedule server configurations
},
"iMIP": {
"Enabled" : False, # Server-to-iMIP protocol
"MailGatewayServer" : "localhost",
"MailGatewayPort" : 62310,
"Sending": {
"Server" : "", # SMTP server to relay messages through
"Port" : 587, # SMTP server port to relay messages through
"Address" : "", # 'From' address for server
},
"Receiving": {
"Server" : "", # Server to retrieve email messages from
"Port" : 0, # Server port to retrieve email messages from
"UseSSL" : True,
"Type" : "", # Type of message access server: 'pop' or 'imap'
"PollingSeconds" : 30, # How often to fetch mail
"Username" : "", # For account receving mail
"Password" : "", # For account receving mail
},
"AddressPatterns" : [], # Reg-ex patterns to match iMIP-able calendar user addresses
"MailTemplatesDirectory": "/usr/share/caldavd/share/email_templates", # Directory containing HTML templates for email invitations (invite.html, cancel.html)
"MailIconsDirectory": "/usr/share/caldavd/share/date_icons", # Directory containing language-specific subdirectories containing date-specific icons for email invitations
"InvitationDaysToLive" : 90, # How many days invitations are valid
},
},
"FreeBusyURL": {
"Enabled" : False, # Per-user free-busy-url protocol
"TimePeriod" : 14, # Number of days into the future to generate f-b data if no explicit time-range is specified
"AnonymousAccess" : False, # Allow anonymous read access to free-busy URL
},
#
# Notifications
#
"Notifications" : {
"Enabled": False,
"CoalesceSeconds" : 10,
"InternalNotificationHost" : "localhost",
"InternalNotificationPort" : 62309,
"Services" : {
"SimpleLineNotifier" : {
"Service" : "twistedcaldav.notify.SimpleLineNotifierService",
"Enabled" : False,
"Port" : 62308,
},
"XMPPNotifier" : {
"Service" : "twistedcaldav.notify.XMPPNotifierService",
"Enabled" : False,
"Host" : "", # "xmpp.host.name"
"Port" : 5222,
"JID" : "", # "jid@xmpp.host.name/resource"
"Password" : "",
"ServiceAddress" : "", # "pubsub.xmpp.host.name"
"NodeConfiguration" : {
"pubsub#deliver_payloads" : "1",
"pubsub#persist_items" : "1",
},
"KeepAliveSeconds" : 120,
"HeartbeatMinutes" : 30,
"AllowedJIDs": [],
},
}
},
#
# Performance tuning
#
# Set the maximum number of outstanding requests to this server.
"MaxRequests": 600,
"ListenBacklog": 50,
"IdleConnectionTimeOut": 15,
"UIDReservationTimeOut": 30 * 60,
#
# Localization
#
"Localization" : {
"LocalesDirectory" : "/usr/share/caldavd/locales",
"Language" : "en",
},
#
# Implementation details
#
# The following are specific to how the server is built, and useful
# for development, but shouldn't be needed by users.
#
# Twisted
"Twisted": {
"twistd": "/usr/share/caldavd/bin/twistd",
"reactor": "select",
},
# Python Director
"PythonDirector": {
"pydir": "/usr/share/caldavd/bin/pydir.py",
"ConfigFile": "/etc/caldavd/pydir.xml",
"ControlSocket": "/var/run/caldavd-pydir.sock",
},
# Umask
"umask": 0027,
# A unix socket used for communication between the child and master
# processes.
"ControlSocket": "/var/run/caldavd.sock",
# Support for Content-Encoding compression options as specified in
# RFC2616 Section 3.5
"ResponseCompression": True,
# Profiling options
"Profiling": {
"Enabled": False,
"BaseDirectory": "/tmp/stats",
},
"Memcached": {
"MaxClients": 5,
"ClientEnabled": False,
"ServerEnabled": False,
"BindAddress": "127.0.0.1",
"Port": 11211,
"memcached": "memcached", # Find in PATH
"MaxMemory": 0, # Megabytes
"Options": [],
},
"EnableKeepAlive": True,
}
class Config (object):
"""
@DynamicAttrs
"""
def __init__(self, defaults):
if not isinstance(defaults, ConfigDict):
defaults = ConfigDict(defaults)
self.setDefaults(defaults)
self._data = copy.deepcopy(defaults)
self._configFile = None
self._hooks = [
self.updateHostName,
self.updateDirectoryService,
self.updateACLs,
self.updateRejectClients,
self.updateDropBox,
self.updateLogLevels,
self.updateNotifications,
]
def __str__(self):
return str(self._data)
def addHook(self, hook):
self._hooks.append(hook)
def update(self, items):
if not isinstance(items, ConfigDict):
items = ConfigDict(items)
#
# Call hooks
#
for hook in self._hooks:
hook(self, items)
@staticmethod
def updateHostName(self, items):
if not self.ServerHostName:
from socket import getfqdn
hostname = getfqdn()
if not hostname:
hostname = "localhost"
self.ServerHostName = hostname
@staticmethod
def updateDirectoryService(self, items):
#
# Special handling for directory services configs
#
dsType = items.get("DirectoryService", {}).get("type", None)
if dsType is None:
dsType = self._data.DirectoryService.type
else:
if dsType == self._data.DirectoryService.type:
oldParams = self._data.DirectoryService.params
newParams = items.DirectoryService.get("params", {})
_mergeData(oldParams, newParams)
else:
if dsType in serviceDefaultParams:
self._data.DirectoryService.params = copy.deepcopy(serviceDefaultParams[dsType])
else:
self._data.DirectoryService.params = {}
for param in items.get("DirectoryService", {}).get("params", {}):
if dsType in serviceDefaultParams and param not in serviceDefaultParams[dsType]:
raise ConfigurationError("Parameter %s is not supported by service %s" % (param, dsType))
_mergeData(self._data, items)
if self._data.DirectoryService.type in serviceDefaultParams:
for param in tuple(self._data.DirectoryService.params):
if param not in serviceDefaultParams[self._data.DirectoryService.type]:
del self._data.DirectoryService.params[param]
@staticmethod
def updateACLs(self, items):
#
# Base resource ACLs
#
def readOnlyACE(allowAnonymous):
if allowAnonymous:
reader = davxml.All()
else:
reader = davxml.Authenticated()
return davxml.ACE(
davxml.Principal(reader),
davxml.Grant(
davxml.Privilege(davxml.Read()),
davxml.Privilege(davxml.ReadCurrentUserPrivilegeSet()),
),
davxml.Protected(),
)
self.AdminACEs = tuple(
davxml.ACE(
davxml.Principal(davxml.HRef(principal)),
davxml.Grant(davxml.Privilege(davxml.All())),
davxml.Protected(),
TwistedACLInheritable(),
)
for principal in config.AdminPrincipals
)
self.ReadACEs = tuple(
davxml.ACE(
davxml.Principal(davxml.HRef(principal)),
davxml.Grant(
davxml.Privilege(davxml.Read()),
davxml.Privilege(davxml.ReadCurrentUserPrivilegeSet()),
),
davxml.Protected(),
TwistedACLInheritable(),
)
for principal in config.ReadPrincipals
)
self.RootResourceACL = davxml.ACL(
# Read-only for anon or authenticated, depending on config
readOnlyACE(self.EnableAnonymousReadRoot),
# Add inheritable all access for admins
*self.AdminACEs
)
log.debug("Root ACL: %s" % (self.RootResourceACL.toxml(),))
self.ProvisioningResourceACL = davxml.ACL(
# Read-only for anon or authenticated, depending on config
readOnlyACE(self.EnableAnonymousReadNav),
# Add read and read-acl access for admins
*[
davxml.ACE(
davxml.Principal(davxml.HRef(principal)),
davxml.Grant(
davxml.Privilege(davxml.Read()),
davxml.Privilege(davxml.ReadACL()),
davxml.Privilege(davxml.ReadCurrentUserPrivilegeSet()),
),
davxml.Protected(),
)
for principal in config.AdminPrincipals
]
)
log.debug("Nav ACL: %s" % (self.ProvisioningResourceACL.toxml(),))
@staticmethod
def updateRejectClients(self, items):
#
# Compile RejectClients expressions for speed
#
try:
self.RejectClients = [re.compile(x) for x in self.RejectClients if x]
except re.error, e:
raise ConfigurationError("Invalid regular expression in RejectClients: %s" % (e,))
@staticmethod
def updateDropBox(self, items):
#
# FIXME: Use the config object instead of doing this here
#
from twistedcaldav.resource import CalendarPrincipalResource
CalendarPrincipalResource.enableDropBox(self.EnableDropBox)
@staticmethod
def updateLogLevels(self, items):
clearLogLevels()
try:
if "DefaultLogLevel" in self._data:
level = self._data["DefaultLogLevel"]
if not level:
level = "info"
setLogLevelForNamespace(None, level)
if "LogLevels" in self._data:
for namespace in self._data["LogLevels"]:
setLogLevelForNamespace(namespace, self._data["LogLevels"][namespace])
except InvalidLogLevelError, e:
raise ConfigurationError("Invalid log level: %s" % (e.level))
def updateDefaults(self, items):
_mergeData(self._defaults, items)
self.update(items)
def setDefaults(self, defaults):
if not isinstance(defaults, ConfigDict):
defaults = ConfigDict(defaults)
self._defaults = copy.deepcopy(defaults)
def __setattr__(self, attr, value):
if "_data" in self.__dict__ and attr in self.__dict__["_data"]:
self._data[attr] = value
else:
self.__dict__[attr] = value
def __getattr__(self, attr):
if attr in self._data:
return self._data[attr]
raise AttributeError(attr)
def reload(self):
log.info("Reloading configuration from file: %s" % (self._configFile,))
self._data = copy.deepcopy(self._defaults)
self.loadConfig(self._configFile)
def loadConfig(self, configFile):
self._configFile = configFile
if configFile and os.path.exists(configFile):
configDict = readPlist(configFile)
configDict = _cleanup(configDict)
self.update(ConfigDict(configDict))
elif configFile:
log.error("Configuration file does not exist or is inaccessible: %s" % (configFile,))
@staticmethod
def updateNotifications(self, items):
#
# Notifications
#
for key, service in self.Notifications["Services"].iteritems():
if service["Enabled"]:
self.Notifications["Enabled"] = True
break
else:
self.Notifications["Enabled"] = False
for key, service in self.Notifications["Services"].iteritems():
if (
service["Service"] == "twistedcaldav.notify.XMPPNotifierService" and
service["Enabled"]
):
for key, value in service.iteritems():
if not value and key not in ("AllowedJIDs", "HeartbeatMinutes"):
raise ConfigurationError("Invalid %s for XMPPNotifierService: %r"
% (key, value))
def _mergeData(oldData, newData):
for key, value in newData.iteritems():
if isinstance(value, (dict,)):
if key in oldData:
assert isinstance(oldData[key], ConfigDict), "%r in %r is not a ConfigDict" % (oldData[key], oldData)
else:
oldData[key] = {}
_mergeData(oldData[key], value)
else:
oldData[key] = value
def _cleanup(configDict):
cleanDict = copy.deepcopy(configDict)
def unknown(key):
log.err("Ignoring unknown configuration option: %r" % (key,))
del cleanDict[key]
def deprecated(oldKey, newKey):
log.err("Configuration option %r is deprecated in favor of %r." % (oldKey, newKey))
def renamed(oldKey, newKey):
deprecated(oldKey, newKey)
cleanDict[newKey] = configDict[oldKey]
del cleanDict[oldKey]
renamedOptions = {
# "BindAddress": "BindAddresses",
}
for key in configDict:
if key in defaultConfig:
continue
elif key in renamedOptions:
renamed(key, renamedOptions[key])
# elif key == "pydirConfig":
# deprecated(key, "PythonDirector -> pydir")
# if "PythonDirector" not in cleanDict:
# cleanDict["PythonDirector"] = {}
# cleanDict["PythonDirector"]["ConfigFile"] = cleanDict["pydirConfig"]
# del cleanDict["pydirConfig"]
else:
unknown(key,)
return cleanDict
class ConfigurationError (RuntimeError):
"""
Invalid server configuration.
"""
config = Config(defaultConfig)
def parseConfig(configFile):
config.loadConfig(configFile)