예제 #1
0
파일: domainauth.py 프로젝트: danBLA/fuglu
    def examine(self, suspect):
        if not DKIMPY_AVAILABLE:
            suspect.debug("dkimpy not available, can not check")
            suspect.set_tag('DKIMVerify.skipreason',
                            'dkimpy library not available')
            return DUNNO

        hdr_from_domain = extract_from_domain(suspect)
        if not hdr_from_domain:
            self.logger.debug(
                '%s DKIM Verification skipped, no header from address')
            suspect.set_tag("DKIMVerify.skipreason", 'no header from address')
            return DUNNO

        self.skiplist.filename = self.config.get(self.section, 'skiplist')
        skiplist = self.skiplist.get_list()
        if hdr_from_domain in skiplist:
            self.logger.debug(
                '%s DKIM Verification skipped, sender domain skiplisted')
            suspect.set_tag("DKIMVerify.skipreason",
                            'sender domain skiplisted')
            return DUNNO

        source = suspect.get_original_source()
        if "dkim-signature" not in suspect.get_message_rep():
            suspect.set_tag('DKIMVerify.skipreason', 'not dkim signed')
            suspect.write_sa_temp_header('X-DKIMVerify', 'unsigned')
            suspect.debug("No dkim signature header found")
            return DUNNO
        # use the local logger of the plugin but prepend the fuglu id
        d = DKIM(source,
                 logger=PrependLoggerMsg(self.logger,
                                         prepend=suspect.id,
                                         maxlevel=logging.INFO))

        try:
            try:
                valid = d.verify()
            except DKIMException as de:
                self.logger.warning("%s: DKIM validation failed: %s" %
                                    (suspect.id, str(de)))
                valid = False
            suspect.set_tag("DKIMVerify.sigvalid", valid)
            suspect.write_sa_temp_header('X-DKIMVerify',
                                         'valid' if valid else 'invalid')
        except NameError as ne:
            self.logger.warning(
                "%s: DKIM validation failed due to missing dependency: %s" %
                (suspect.id, str(ne)))
            suspect.set_tag('DKIMVerify.skipreason', 'plugin error')
        except Exception as e:
            self.logger.warning("%s: DKIM validation failed: %s" %
                                (suspect.id, str(e)))
            suspect.set_tag('DKIMVerify.skipreason', 'plugin error')

        return DUNNO
예제 #2
0
    def test_prepend_maxmin(self):
        logstream = StringIO()
        loghandler = logging.StreamHandler(stream=logstream)
        loghandler.setLevel(logging.DEBUG)
        loghandler.setFormatter(logging.Formatter("%(levelname)s %(message)s"))
        root = logging.getLogger()
        root = PrependLoggerMsg(root,
                                "(palim)",
                                maxlevel=logging.ERROR,
                                minlevel=logging.INFO)

        with LoggingContext(root, handler=loghandler, level=logging.DEBUG):
            root.debug("{{debug}}")
            root.info("{{info}}")
            root.warning("{{warning}}")
            root.error("{{error}}")
            root.critical("{{critical}}")

        expected = [
            "INFO (palim) {{debug}}", "INFO (palim) {{info}}",
            "WARNING (palim) {{warning}}", "ERROR (palim) {{error}}",
            "ERROR (palim) {{critical}}"
        ]

        loglines = logstream.getvalue().strip().split('\n')
        self.assertEqual(5, len(loglines), "\n".join(loglines))
        self.assertEqual(expected, loglines)
예제 #3
0
 def test_max_middlerange(self):
     self.assertNotEqual(logging.DEBUG, logging.INFO)
     with self.assertRaises(AssertionError):
         _ = PrependLoggerMsg(None,
                              "(palim)",
                              maxlevel=(logging.DEBUG + logging.INFO) / 2)
예제 #4
0
 def test_min_outsiderange(self):
     with self.assertRaises(AssertionError):
         _ = PrependLoggerMsg(None, "(palim)", maxlevel=logging.DEBUG - 100)