def test_attempt_policies(self):
     class BadPolicy(RelayPolicy):
         def apply(self, env):
             raise Exception('that\'s bad policy!')
     env = Envelope()
     blackhole = BlackholeRelay()
     blackhole.add_policy(BadPolicy())
     ret = blackhole._attempt(env, 0)
     self.assertEqual('250', ret.code)
    def test_attempt_policies(self):
        class BadPolicy(RelayPolicy):
            def apply(self, env):
                raise Exception('that\'s bad policy!')

        env = Envelope()
        blackhole = BlackholeRelay()
        blackhole.add_policy(BadPolicy())
        ret = blackhole._attempt(env, 0)
        self.assertEqual('250', ret.code)
Ejemplo n.º 3
0
def setup_queue():
    # We only need a dummy slimta relay here
    # only to allow for queue creation
    relay = BlackholeRelay()

    # Set up celery queue
    queue = TransactionalQueue(relay)

    return relay, queue
Ejemplo n.º 4
0
def backmuncher():
    "Run smtp that handle feedback loops, unsuscribes and more."

    import django
    django.setup()

    from django.conf import settings
    from slimta.edge.smtp import SmtpEdge
    from slimta.system import drop_privileges
    from slimta.util.proxyproto import ProxyProtocol
    from slimta.relay.blackhole import BlackholeRelay

    from munch.core.mail.backmuncher import Queue

    edge_class = SmtpEdge
    if settings.BACKMUNCHER.get('PROXYPROTO_ENABLED', False):

        class ProxyProtocolSmtpEdge(ProxyProtocol, SmtpEdge):
            pass

        edge_class = ProxyProtocolSmtpEdge

    edge = edge_class(
        (settings.BACKMUNCHER.get('SMTP_BIND_HOST'),
         settings.BACKMUNCHER.get('SMTP_BIND_PORT')),
        Queue(BlackholeRelay()),
        hostname=settings.BACKMUNCHER.get('EDGE_EHLO_AS', None),
        tls=False,
    )

    log.info('Listening on {}:{}'.format(
        settings.BACKMUNCHER.get('SMTP_BIND_HOST'),
        settings.BACKMUNCHER.get('SMTP_BIND_PORT')))
    edge.start()

    if settings.BACKMUNCHER.get('DROP_PRIVILEGES_USER') is not None:
        # If this command is run with root user (to be allowed to
        # open reserved ports like 25), we should then "switch" to a
        # normal user for security.
        drop_privileges(settings.BACKMUNCHER.get('DROP_PRIVILEGES_USER'),
                        settings.BACKMUNCHER.get('DROP_PRIVILEGES_GROUP'))
        log.info('Dropping privileges to {}:{}'.format(
            settings.BACKMUNCHER.get('DROP_PRIVILEGES_USER'),
            settings.BACKMUNCHER.get('DROP_PRIVILEGES_GROUP')))

    try:
        edge.get()
    except KeyboardInterrupt:
        try:
            edge.server.close()
            log.info('Stop accepting connections.')
            log.info('Edge stopped...')
            edge.server.stop(timeout=1)
        except KeyboardInterrupt:
            log.info('Forcing shutdown...')
            edge.server.stop(timeout=1)
 def test_attempt(self):
     env = Envelope()
     blackhole = BlackholeRelay()
     ret = blackhole.attempt(env, 0)
     self.assertEqual('250', ret.code)
Ejemplo n.º 6
0
 def _start_relay(self, name, options=None):
     if name in self.relays:
         return self.relays[name]
     if not options:
         options = getattr(self.cfg.relay, name)
     new_relay = None
     if options.type == 'mx':
         from slimta.relay.smtp.mx import MxSmtpRelay
         from .helpers import fill_hostname_template
         kwargs = {}
         kwargs['connect_timeout'] = options.get('connect_timeout', 30)
         kwargs['command_timeout'] = options.get('command_timeout', 30)
         kwargs['data_timeout'] = options.get('data_timeout', 60)
         kwargs['idle_timeout'] = options.get('idle_timeout', 10)
         kwargs['pool_size'] = options.get('concurrent_connections', 5)
         kwargs['ehlo_as'] = fill_hostname_template(options.ehlo_as)
         kwargs['context'] = self._get_client_ssl_context(options.tls)
         if options.ipv4_only:
             kwargs['socket_creator'] = build_ipv4_socket_creator([25])
         new_relay = MxSmtpRelay(**kwargs)
         if 'force_mx' in options:
             for domain, dest in options.force_mx:
                 new_relay.force_mx(domain, dest)
     elif options.type == 'static':
         from slimta.relay.smtp.static import StaticSmtpRelay
         from .helpers import fill_hostname_template, get_relay_credentials
         kwargs = {}
         kwargs['host'] = options.host
         kwargs['port'] = options.get('port', 25)
         kwargs['connect_timeout'] = options.get('connect_timeout', 30)
         kwargs['command_timeout'] = options.get('command_timeout', 30)
         kwargs['data_timeout'] = options.get('data_timeout', 60)
         kwargs['idle_timeout'] = options.get('idle_timeout', 10)
         kwargs['pool_size'] = options.get('concurrent_connections', 5)
         kwargs['ehlo_as'] = fill_hostname_template(options.ehlo_as)
         kwargs['context'] = self._get_client_ssl_context(options.tls)
         if 'credentials' in options:
             credentials = get_relay_credentials(options.credentials)
             kwargs['credentials'] = credentials
         if options.ipv4_only:
             kwargs['socket_creator'] = \
                 build_ipv4_socket_creator([kwargs['port']])
         new_relay = StaticSmtpRelay(**kwargs)
     elif options.type == 'lmtp':
         from slimta.relay.smtp.static import StaticLmtpRelay
         from .helpers import fill_hostname_template, get_relay_credentials
         kwargs = {}
         kwargs['host'] = options.get('host', 'localhost')
         kwargs['port'] = options.get('port', 24)
         kwargs['connect_timeout'] = options.get('connect_timeout', 30)
         kwargs['command_timeout'] = options.get('command_timeout', 30)
         kwargs['data_timeout'] = options.get('data_timeout', 60)
         kwargs['idle_timeout'] = options.get('idle_timeout', 10)
         kwargs['pool_size'] = options.get('concurrent_connections', 5)
         kwargs['ehlo_as'] = fill_hostname_template(options.ehlo_as)
         kwargs['context'] = self._get_client_ssl_context(options.tls)
         if 'credentials' in options:
             credentials = get_relay_credentials(options.credentials)
             kwargs['credentials'] = credentials
         if options.ipv4_only:
             kwargs['socket_creator'] = \
                 build_ipv4_socket_creator([kwargs['port']])
         new_relay = StaticLmtpRelay(**kwargs)
     elif options.type == 'http':
         from slimta.relay.http import HttpRelay
         from .helpers import fill_hostname_template
         kwargs = {}
         kwargs['ehlo_as'] = fill_hostname_template(options.ehlo_as)
         kwargs['timeout'] = options.get('timeout', 60)
         kwargs['idle_timeout'] = options.get('idle_timeout', 10)
         kwargs['context'] = self._get_client_ssl_context(options.tls)
         new_relay = HttpRelay(options.url, **kwargs)
     elif options.type == 'blackhole':
         from slimta.relay.blackhole import BlackholeRelay
         new_relay = BlackholeRelay()
     elif options.type == 'pipe':
         from slimta.relay.pipe import PipeRelay
         new_relay = PipeRelay(options.args)
     elif options.type == 'maildrop':
         from slimta.relay.pipe import MaildropRelay
         path = options.path
         new_relay = MaildropRelay(path)
     elif options.type == 'dovecot':
         from slimta.relay.pipe import DovecotLdaRelay
         path = options.path
         new_relay = DovecotLdaRelay(path)
     elif options.type == 'custom':
         new_relay = custom_factory(options)
     else:
         msg = 'relay type does not exist: '+options.type
         raise ConfigValidationError(msg)
     self.relays[name] = new_relay
     return new_relay
 def test_attempt(self):
     env = Envelope()
     blackhole = BlackholeRelay()
     ret = blackhole.attempt(env, 0)
     self.assertEqual('250', ret.code)