This file is indexed.

/usr/lib/python2.7/dist-packages/txdav/caldav/datastore/scheduling/ischedule/localservers.py is in calendarserver 5.2+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
##
# Copyright (c) 2011-2014 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.
##

from twext.python.log import Logger
from twisted.internet.abstract import isIPAddress
from twistedcaldav.client.pool import installPool
from twistedcaldav.config import config, fullServerPath
from twistedcaldav.xmlutil import readXML

from txdav.caldav.datastore.scheduling.ischedule.utils import getIPsFromHost
import socket
import urlparse

"""
XML based server configuration file handling.

This is used in an environment where more than one server is being used within a single domain. i.e., all
the principals across the whole domain need to be able to directly schedule each other and know of each others
existence. A common scenario would be a production server and a development/test server.

Each server is identified by an id and url. The id is used when assigning principals to a specific server. Each
server can also support multiple partitions, and each of those is identified by an id and url, with the id also
being used to assign principals to a specific partition.

These servers support the concept of "partitioning" and "podding".

A "partitioned" service is one that spreads its
users out across multiple stores and does reverse proxying of incoming requests to the appropriate partitioned host.
All servers within the same partition have to be running the same version of the software etc.

A "podded" service is one where different groups of users are hosted on different servers, which may be of
different versions etc. A "pod" may itself be "partitioned", but the partitioning is "invisible" to the outside world.
"""

__all__ = [
    "Servers",
]

log = Logger()

SERVER_SECRET_HEADER = "X-CALENDARSERVER-ISCHEDULE"

class ServersDB(object):
    """
    Represents the set of servers within the same domain.
    """

    def __init__(self):

        self._servers = {}
        self._xmlFile = None
        self._thisServer = None


    def load(self, xmlFile=None, ignoreIPLookupFailures=False):
        if self._xmlFile is None or xmlFile is not None:
            self._servers = {}
            if xmlFile:
                self._xmlFile = xmlFile
            else:
                self._xmlFile = fullServerPath(
                    config.ConfigRoot,
                    config.Servers.ConfigFile
                )
        self._servers = ServersParser.parse(self._xmlFile, ignoreIPLookupFailures=ignoreIPLookupFailures)
        for server in self._servers.values():
            if server.thisServer:
                self._thisServer = server
                break
        else:
            raise ValueError("No server in %s matches this server." % (self._xmlFile,))


    def clear(self):
        self._servers = {}
        self._xmlFile = None
        self._thisServer = None


    def getServerById(self, id):
        return self._servers.get(id)


    def getServerURIById(self, id):
        try:
            return self._servers[id].uri
        except KeyError:
            return None


    def getThisServer(self):
        return self._thisServer

Servers = ServersDB()   # Global server DB



class Server(object):
    """
    Represents a server which may itself be partitioned.
    """

    def __init__(self):
        self.id = None
        self.uri = None
        self.thisServer = False
        self.ips = set()
        self.allowed_from_ips = set()
        self.shared_secret = None
        self.partitions = {}
        self.partitions_ips = set()
        self.isImplicit = True


    def check(self, ignoreIPLookupFailures=False):
        # Check whether this matches the current server
        parsed_uri = urlparse.urlparse(self.uri)
        if parsed_uri.hostname == config.ServerHostName:
            if parsed_uri.scheme == "http":
                if config.HTTPPort:
                    self.thisServer = parsed_uri.port in (config.HTTPPort,) + tuple(config.BindHTTPPorts)
            elif parsed_uri.scheme == "https":
                if config.SSLPort:
                    self.thisServer = parsed_uri.port in (config.SSLPort,) + tuple(config.BindSSLPorts)

        # Need to cache IP addresses
        try:
            ips = getIPsFromHost(parsed_uri.hostname)
        except socket.gaierror, e:
            msg = "Unable to lookup ip-addr for server '%s': %s" % (parsed_uri.hostname, str(e))
            log.error(msg)
            if ignoreIPLookupFailures:
                ips = ()
            else:
                raise ValueError(msg)
        self.ips = set(ips)

        actual_ips = set()
        for item in self.allowed_from_ips:
            if not isIPAddress(item):
                try:
                    ips = getIPsFromHost(item)
                except socket.gaierror, e:
                    msg = "Unable to lookup ip-addr for allowed-from '%s': %s" % (item, str(e))
                    log.error(msg)
                    if not ignoreIPLookupFailures:
                        raise ValueError(msg)
                else:
                    actual_ips.update(ips)
            else:
                actual_ips.add(item)
        self.allowed_from_ips = actual_ips

        for uri in self.partitions.values():
            parsed_uri = urlparse.urlparse(uri)
            try:
                ips = getIPsFromHost(parsed_uri.hostname)
            except socket.gaierror, e:
                msg = "Unable to lookup ip-addr for partition '%s': %s" % (parsed_uri.hostname, str(e))
                log.error(msg)
                if ignoreIPLookupFailures:
                    ips = ()
                else:
                    raise ValueError(msg)
            self.partitions_ips.update(ips)


    def checkThisIP(self, ip):
        """
        Check that the passed in IP address corresponds to this server or one of its partitions.
        """
        return (ip in self.ips) or (ip in self.partitions_ips)


    def hasAllowedFromIP(self):
        return len(self.allowed_from_ips) > 0


    def checkAllowedFromIP(self, ip):
        return ip in self.allowed_from_ips


    def checkSharedSecret(self, headers):

        # Get header from the request
        request_secret = headers.getRawHeaders(SERVER_SECRET_HEADER)

        if request_secret is not None and self.shared_secret is None:
            log.error("iSchedule request included unexpected %s header" % (SERVER_SECRET_HEADER,))
            return False
        elif request_secret is None and self.shared_secret is not None:
            log.error("iSchedule request did not include required %s header" % (SERVER_SECRET_HEADER,))
            return False
        elif (request_secret[0] if request_secret else None) != self.shared_secret:
            log.error("iSchedule request %s header did not match" % (SERVER_SECRET_HEADER,))
            return False
        else:
            return True


    def secretHeader(self):
        """
        Return a tuple of header name, header value
        """
        return (SERVER_SECRET_HEADER, self.shared_secret,)


    def addPartition(self, id, uri):
        self.partitions[id] = uri


    def getPartitionURIForId(self, id):
        return self.partitions.get(id)


    def isPartitioned(self):
        return len(self.partitions) != 0


    def installReverseProxies(self, ownUID, maxClients):

        for partition, url in self.partitions.iteritems():
            if partition != ownUID:
                installPool(
                    partition,
                    url,
                    maxClients,
                )



ELEMENT_SERVERS = "servers"
ELEMENT_SERVER = "server"
ELEMENT_ID = "id"
ELEMENT_URI = "uri"
ELEMENT_ALLOWED_FROM = "allowed-from"
ELEMENT_SHARED_SECRET = "shared-secret"
ELEMENT_PARTITIONS = "partitions"
ELEMENT_PARTITION = "partition"
ATTR_IMPLICIT = "implicit"
ATTR_VALUE_YES = "yes"
ATTR_VALUE_NO = "no"

class ServersParser(object):
    """
    Servers configuration file parser.
    """
    @staticmethod
    def parse(xmlFile, ignoreIPLookupFailures=False):

        results = {}

        # Read in XML
        try:
            _ignore_tree, servers_node = readXML(xmlFile, ELEMENT_SERVERS)
        except ValueError, e:
            raise RuntimeError("XML parse error for '%s' because: %s" % (xmlFile, e,))

        for child in servers_node:

            if child.tag != ELEMENT_SERVER:
                raise RuntimeError("Unknown server type: '%s' in servers file: '%s'" % (child.tag, xmlFile,))

            server = Server()
            server.isImplicit = child.get(ATTR_IMPLICIT, ATTR_VALUE_YES) == ATTR_VALUE_YES

            for node in child:
                if node.tag == ELEMENT_ID:
                    server.id = node.text
                elif node.tag == ELEMENT_URI:
                    server.uri = node.text
                elif node.tag == ELEMENT_ALLOWED_FROM:
                    server.allowed_from_ips.add(node.text)
                elif node.tag == ELEMENT_SHARED_SECRET:
                    server.shared_secret = node.text
                elif node.tag == ELEMENT_PARTITIONS:
                    ServersParser._parsePartition(xmlFile, node, server)
                else:
                    raise RuntimeError("Invalid element '%s' in servers file: '%s'" % (node.tag, xmlFile,))

            if server.id is None or server.uri is None:
                raise RuntimeError("Invalid partition '%s' in servers file: '%s'" % (child.tag, xmlFile,))

            server.check(ignoreIPLookupFailures=ignoreIPLookupFailures)
            results[server.id] = server

        return results


    @staticmethod
    def _parsePartition(xmlFile, partitions, server):

        for child in partitions:

            if child.tag != ELEMENT_PARTITION:
                raise RuntimeError("Unknown partition type: '%s' in servers file: '%s'" % (child.tag, xmlFile,))

            id = None
            uri = None
            for node in child:
                if node.tag == ELEMENT_ID:
                    id = node.text
                elif node.tag == ELEMENT_URI:
                    uri = node.text
                else:
                    raise RuntimeError("Invalid element '%s' in augment file: '%s'" % (node.tag, xmlFile,))

            if id is None or uri is None:
                raise RuntimeError("Invalid partition '%s' in servers file: '%s'" % (child.tag, xmlFile,))

            server.addPartition(id, uri)