Exemplo n.º 1
0
    def _getTimezoneFromServer(self, tzinfo):
        # List all from the server
        url = "%s?action=get&tzid=%s" % (self.uri, tzinfo.tzid,)
        log.debug("Getting timezone from secondary server: %s" % (url,))
        response = (yield getURL(url))
        if response is None or response.code / 100 != 2:
            returnValue(None)

        ct = response.headers.getRawHeaders("content-type", ("bogus/type",))[0]
        ct = ct.split(";", 1)
        ct = ct[0]
        if ct not in ("text/calendar",):
            log.error("Invalid content-type '%s' for tzid : %s" % (ct, tzinfo.tzid,))
            returnValue(None)

        ical = response.data
        try:
            calendar = PyCalendar.parseText(ical)
        except PyCalendarInvalidData:
            log.error("Invalid calendar data for tzid: %s" % (tzinfo.tzid,))
            returnValue(None)
        ical = calendar.getText()

        tzinfo.md5 = hashlib.md5(ical).hexdigest()

        try:
            tzpath = os.path.join(self.basepath, tzinfo.tzid) + ".ics"
            if not os.path.exists(os.path.dirname(tzpath)):
                os.makedirs(os.path.dirname(tzpath))
            f = open(tzpath, "w")
            f.write(ical)
            f.close()
        except IOError, e:
            log.error("Unable to write calendar file for %s: %s" % (tzinfo.tzid, str(e),))
Exemplo n.º 2
0
    def _discoverServer(self):
        """
        Make sure we know the timezone service path
        """

        if self.uri is None:
            if config.TimezoneService.SecondaryService.Host:
                self.uri = "https://%s/.well-known/timezone" % (config.TimezoneService.SecondaryService.Host,)
            elif config.TimezoneService.SecondaryService.URI:
                self.uri = config.TimezoneService.SecondaryService.URI
        elif not self.uri.startswith("https:") and not self.uri.startswith("http:"):
            self.uri = "https://%s/.well-known/timezone" % (self.uri,)

        testURI = "%s?action=capabilities" % (self.uri,)
        log.debug("Discovering secondary server: %s" % (testURI,))
        response = (yield getURL(testURI))
        if response is None or response.code / 100 != 2:
            log.error("Unable to discover secondary server: %s" % (testURI,))
            self.discovered = False
            returnValue(False)

        # Cache the redirect target
        if hasattr(response, "location"):
            self.uri = response.location
            log.debug("Redirected secondary server to: %s" % (self.uri,))

        # TODO: Ignoring the data from capabilities for now

        self.discovered = True
        returnValue(True)
Exemplo n.º 3
0
    def _getTimezoneFromServer(self, tzinfo):
        # List all from the server
        url = "%s?action=get&tzid=%s" % (self.uri, tzinfo.tzid,)
        log.debug("Getting timezone from secondary server: %s" % (url,))
        response = (yield getURL(url))
        if response is None or response.code / 100 != 2:
            returnValue(None)

        ct = response.headers.getRawHeaders("content-type", ("bogus/type",))[0]
        ct = ct.split(";", 1)
        ct = ct[0]
        if ct not in ("text/calendar",):
            log.error("Invalid content-type '%s' for tzid : %s" % (ct, tzinfo.tzid,))
            returnValue(None)

        ical = response.data
        try:
            calendar = Calendar.parseText(ical)
        except InvalidData:
            log.error("Invalid calendar data for tzid: %s" % (tzinfo.tzid,))
            returnValue(None)
        ical = calendar.getText()

        tzinfo.md5 = hashlib.md5(ical).hexdigest()

        try:
            tzpath = os.path.join(self.basepath, tzinfo.tzid) + ".ics"
            if not os.path.exists(os.path.dirname(tzpath)):
                os.makedirs(os.path.dirname(tzpath))
            f = open(tzpath, "w")
            f.write(ical)
            f.close()
        except IOError, e:
            log.error("Unable to write calendar file for %s: %s" % (tzinfo.tzid, str(e),))
Exemplo n.º 4
0
    def _discoverServer(self):
        """
        Make sure we know the timezone service path
        """

        if self.uri is None:
            if config.TimezoneService.SecondaryService.Host:
                self.uri = "https://%s/.well-known/timezone" % (config.TimezoneService.SecondaryService.Host,)
            elif config.TimezoneService.SecondaryService.URI:
                self.uri = config.TimezoneService.SecondaryService.URI
        elif not self.uri.startswith("https:") and not self.uri.startswith("http:"):
            self.uri = "https://%s/.well-known/timezone" % (self.uri,)

        testURI = "%s?action=capabilities" % (self.uri,)
        log.debug("Discovering secondary server: %s" % (testURI,))
        response = (yield getURL(testURI))
        if response is None or response.code / 100 != 2:
            log.error("Unable to discover secondary server: %s" % (testURI,))
            self.discovered = False
            returnValue(False)

        # Cache the redirect target
        if hasattr(response, "location"):
            self.uri = response.location
            log.debug("Redirected secondary server to: %s" % (self.uri,))

        # TODO: Ignoring the data from capabilities for now

        self.discovered = True
        returnValue(True)
Exemplo n.º 5
0
    def _lookupKeys(self):
        """
        Do the key lookup using the actual lookup method.
        """

        # First we do an SRV lookup for _domainkey to get the public key server URI
        uri = (yield self._getURI())

        log.debug("DKIM: HTTP/.well-known lookup: {uri}", uri=uri)
        response = (yield getURL(uri))
        if response is None or response.code / 100 != 2:
            log.debug("DKIM: Failed http/well-known lookup: {uri} {resp}",
                      uri=uri,
                      resp=response)
            returnValue(())

        ct = response.headers.getRawHeaders("content-type",
                                            ("bogus/type", ))[0]
        ct = ct.split(";", 1)
        ct = ct[0].strip()
        if ct not in ("text/plain", ):
            log.debug(
                "DKIM: Failed http/well-known lookup: wrong content-type returned {uri} {ct}",
                uri=uri,
                ct=ct)
            returnValue(())

        log.debug("DKIM: HTTP/.well-known lookup results: {uri}\n{resp}",
                  uri=uri,
                  resp=response.data)
        returnValue(
            tuple([
                DKIMUtils.extractTags(line)
                for line in response.data.splitlines()
            ]))
Exemplo n.º 6
0
    def _getTimezoneListFromServer(self):
        """
        Retrieve the timezone list from the specified server
        """

        # Make sure we have the server
        if not self.discovered:
            result = (yield self._discoverServer())
            if not result:
                returnValue(None)

        # List all from the server
        url = "%s?action=list" % (self.uri, )
        if self.dtstamp:
            url = "%s&changedsince=%s" % (
                url,
                self.dtstamp,
            )
        log.debug("Getting timezone list from secondary server: {url}",
                  url=url)
        response = (yield getURL(url))
        if response is None or response.code / 100 != 2:
            returnValue(None)

        ct = response.headers.getRawHeaders("content-type",
                                            ("bogus/type", ))[0]
        ct = ct.split(";", 1)
        ct = ct[0]
        if ct not in ("application/json", ):
            returnValue(None)

        try:
            jroot = json.loads(response.data)
            dtstamp = jroot["dtstamp"]
            timezones = {}
            for timezone in jroot["timezones"]:
                tzid = timezone["tzid"]
                lastmod = timezone["last-modified"]
                aliases = timezone.get("aliases", ())
                timezones[tzid] = TimezoneInfo(tzid, aliases, lastmod, None)
        except (ValueError, KeyError):
            log.debug("Failed to parse JSON timezone list response: {resp}",
                      resp=response.data)
            returnValue(None)
        log.debug("Got {len} timezones from secondary server",
                  len=len(timezones))

        returnValue((
            dtstamp,
            timezones,
        ))
Exemplo n.º 7
0
    def _getTimezoneListFromServer(self):
        """
        Retrieve the timezone list from the specified server
        """

        # Make sure we have the server
        if not self.discovered:
            result = (yield self._discoverServer())
            if not result:
                returnValue(None)

        # List all from the server
        url = "%s?action=list" % (self.uri,)
        if self.dtstamp:
            url = "%s&changedsince=%s" % (url, self.dtstamp,)
        log.debug("Getting timezone list from secondary server: %s" % (url,))
        response = (yield getURL(url))
        if response is None or response.code / 100 != 2:
            returnValue(None)

        ct = response.headers.getRawHeaders("content-type", ("bogus/type",))[0]
        ct = ct.split(";", 1)
        ct = ct[0]
        if ct not in ("application/json",):
            returnValue(None)

        try:
            jroot = json.loads(response.data)
            dtstamp = jroot["dtstamp"]
            timezones = {}
            for timezone in jroot["timezones"]:
                tzid = timezone["tzid"]
                lastmod = timezone["last-modified"]
                aliases = timezone.get("aliases", ())
                timezones[tzid] = TimezoneInfo(tzid, aliases, lastmod, None)
        except (ValueError, KeyError):
            log.debug("Failed to parse JSON timezone list response: %s" % (response.data,))
            returnValue(None)
        log.debug("Got %s timezones from secondary server" % (len(timezones),))

        returnValue((dtstamp, timezones,))
Exemplo n.º 8
0
    def _lookupKeys(self):
        """
        Do the key lookup using the actual lookup method.
        """

        # First we do an SRV lookup for _domainkey to get the public key server URI
        uri = (yield self._getURI())

        log.debug("DKIM: HTTP/.well-known lookup: %s" % (uri,))
        response = (yield getURL(uri))
        if response is None or response.code / 100 != 2:
            log.debug("DKIM: Failed http/well-known lookup: %s %s" % (uri, response,))
            returnValue(())

        ct = response.headers.getRawHeaders("content-type", ("bogus/type",))[0]
        ct = ct.split(";", 1)
        ct = ct[0].strip()
        if ct not in ("text/plain",):
            log.debug("DKIM: Failed http/well-known lookup: wrong content-type returned %s %s" % (uri, ct,))
            returnValue(())

        log.debug("DKIM: HTTP/.well-known lookup results: %s\n%s" % (uri, response.data,))
        returnValue(tuple([DKIMUtils.extractTags(line) for line in response.data.splitlines()]))