summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/mail/outgoing/sender.py
blob: d8c049c8501a9412a4d3986a00a4c7d1e16bff6f (plain)
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
# -*- coding: utf-8 -*-
# outgoing/sender.py
# Copyright (C) 2017 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from OpenSSL import SSL
from StringIO import StringIO
from twisted.internet import reactor
from twisted.internet import defer
from twisted.logger import Logger
from twisted.mail import smtp
from twisted.protocols.amp import ssl
from zope.interface import Interface, implements

from leap.common.check import leap_assert_type, leap_assert
from leap.bitmask import __version__


class ISender(Interface):
    def can_send(self, recipient):
        """
        Checks if ISender implementor can send messages to recipient

        :type recipient: string
        :rtype: bool
        """

    def send(self, recipient, message):
        """
        Send a messages to recipient

        :type recipient: string
        :type message: string

        :return: A Deferred that will be called with the recipient address
        :raises SendError: in case of failure in send
        """


class SendError(Exception):
    pass


class SMTPSender(object):
    implements(ISender)

    log = Logger()

    def __init__(self, from_address, key, host, port):
        """
        :param from_address: The sender address.
        :type from_address: str
        :param key: The client private key for SSL authentication.
        :type key: str
        :param host: The hostname of the remote SMTP server.
        :type host: str
        :param port: The port of the remote SMTP server.
        :type port: int
        """
        leap_assert_type(host, (str, unicode))
        leap_assert(host != '')
        leap_assert_type(port, int)
        leap_assert(port is not 0)
        leap_assert_type(key, basestring)
        leap_assert(key != '')

        self._port = port
        self._host = host
        self._key = key
        self._from_address = from_address

    def can_send(self, recipient):
        return '@' in recipient

    def send(self, recipient, message):
        self.log.info(
            'Connecting to SMTP server %s:%s' % (self._host, self._port))

        # we construct a defer to pass to the ESMTPSenderFactory
        d = defer.Deferred()
        # we don't pass an ssl context factory to the ESMTPSenderFactory
        # because ssl will be handled by reactor.connectSSL() below.
        factory = smtp.ESMTPSenderFactory(
            "",  # username is blank, no client auth here
            "",  # password is blank, no client auth here
            self._from_address,
            recipient.dest.addrstr,
            StringIO(message),
            d,
            heloFallback=True,
            requireAuthentication=False,
            requireTransportSecurity=True)
        factory.domain = bytes('leap.bitmask.mail-' + __version__)
        reactor.connectSSL(
            self._host, self._port, factory,
            contextFactory=SSLContextFactory(self._key, self._key))
        d.addCallback(lambda result: result[1][0][0])
        d.addErrback(self._send_errback)
        return d

    def _send_errback(self, failure):
        raise SendError(failure.getErrorMessage())


class SSLContextFactory(ssl.ClientContextFactory):
    def __init__(self, cert, key):
        self.cert = cert
        self.key = key

    def getContext(self):
        # FIXME -- we should use sslv23 to allow for tlsv1.2
        # and, if possible, explicitely disable sslv3 clientside.
        # Servers should avoid sslv3
        self.method = SSL.TLSv1_METHOD  # SSLv23_METHOD
        ctx = ssl.ClientContextFactory.getContext(self)
        ctx.use_certificate_file(self.cert)
        ctx.use_privatekey_file(self.key)
        return ctx