forked from Bitmessage/PyBitmessage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_smtpServer.py
217 lines (187 loc) · 7.09 KB
/
class_smtpServer.py
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
"""
SMTP server thread
"""
import asyncore
import base64
import email
import logging
import re
import signal
import smtpd
import threading
import time
from email.header import decode_header
from email.parser import Parser
import queues
from addresses import decodeAddress
from bmconfigparser import config
from helper_ackPayload import genAckPayload
from helper_sql import sqlExecute
from network.threads import StoppableThread
from version import softwareVersion
SMTPDOMAIN = "bmaddr.lan"
LISTENPORT = 8425
logger = logging.getLogger('default')
# pylint: disable=attribute-defined-outside-init
class SmtpServerChannelException(Exception):
"""Generic smtp server channel exception."""
pass
class smtpServerChannel(smtpd.SMTPChannel):
"""Asyncore channel for SMTP protocol (server)"""
def smtp_EHLO(self, arg):
"""Process an EHLO"""
if not arg:
self.push('501 Syntax: HELO hostname')
return
self.push('250-PyBitmessage %s' % softwareVersion)
self.push('250 AUTH PLAIN')
def smtp_AUTH(self, arg):
"""Process AUTH"""
if not arg or arg[0:5] not in ["PLAIN"]:
self.push('501 Syntax: AUTH PLAIN')
return
authstring = arg[6:]
try:
decoded = base64.b64decode(authstring)
correctauth = "\x00" + config.safeGet(
"bitmessagesettings", "smtpdusername", "") + "\x00" + config.safeGet(
"bitmessagesettings", "smtpdpassword", "")
logger.debug('authstring: %s / %s', correctauth, decoded)
if correctauth == decoded:
self.auth = True
self.push('235 2.7.0 Authentication successful')
else:
raise SmtpServerChannelException("Auth fail")
except: # noqa:E722
self.push('501 Authentication fail')
def smtp_DATA(self, arg):
"""Process DATA"""
if not hasattr(self, "auth") or not self.auth:
self.push('530 Authentication required')
return
smtpd.SMTPChannel.smtp_DATA(self, arg)
class smtpServerPyBitmessage(smtpd.SMTPServer):
"""Asyncore SMTP server class"""
def handle_accept(self):
"""Accept a connection"""
pair = self.accept()
if pair is not None:
conn, addr = pair
self.channel = smtpServerChannel(self, conn, addr)
def send(self, fromAddress, toAddress, subject, message):
"""Send a bitmessage"""
# pylint: disable=arguments-differ
streamNumber, ripe = decodeAddress(toAddress)[2:]
stealthLevel = config.safeGetInt('bitmessagesettings', 'ackstealthlevel')
ackdata = genAckPayload(streamNumber, stealthLevel)
sqlExecute(
'''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
'',
toAddress,
ripe,
fromAddress,
subject,
message,
ackdata,
int(time.time()), # sentTime (this will never change)
int(time.time()), # lastActionTime
0, # sleepTill time. This will get set when the POW gets done.
'msgqueued',
0, # retryNumber
'sent', # folder
2, # encodingtype
# not necessary to have a TTL higher than 2 days
min(config.getint('bitmessagesettings', 'ttl'), 86400 * 2)
)
queues.workerQueue.put(('sendmessage', toAddress))
def decode_header(self, hdr):
"""Email header decoding"""
ret = []
for h in decode_header(self.msg_headers[hdr]):
if h[1]:
ret.append(h[0].decode(h[1]))
else:
ret.append(h[0].decode("utf-8", errors='replace'))
return ret
def process_message(self, peer, mailfrom, rcpttos, data):
"""Process an email"""
# pylint: disable=too-many-locals, too-many-branches
p = re.compile(".*<([^>]+)>")
if not hasattr(self.channel, "auth") or not self.channel.auth:
logger.error('Missing or invalid auth')
return
try:
self.msg_headers = Parser().parsestr(data)
except: # noqa:E722
logger.error('Invalid headers')
return
try:
sender, domain = p.sub(r'\1', mailfrom).split("@")
if domain != SMTPDOMAIN:
raise Exception("Bad domain %s" % domain)
if sender not in config.addresses():
raise Exception("Nonexisting user %s" % sender)
except Exception as err:
logger.debug('Bad envelope from %s: %r', mailfrom, err)
msg_from = self.decode_header("from")
try:
msg_from = p.sub(r'\1', self.decode_header("from")[0])
sender, domain = msg_from.split("@")
if domain != SMTPDOMAIN:
raise Exception("Bad domain %s" % domain)
if sender not in config.addresses():
raise Exception("Nonexisting user %s" % sender)
except Exception as err:
logger.error('Bad headers from %s: %r', msg_from, err)
return
try:
msg_subject = self.decode_header('subject')[0]
except: # noqa:E722
msg_subject = "Subject missing..."
msg_tmp = email.message_from_string(data)
body = u''
for part in msg_tmp.walk():
if part and part.get_content_type() == "text/plain":
body += part.get_payload(decode=1).decode(part.get_content_charset('utf-8'), errors='replace')
for to in rcpttos:
try:
rcpt, domain = p.sub(r'\1', to).split("@")
if domain != SMTPDOMAIN:
raise Exception("Bad domain %s" % domain)
logger.debug(
'Sending %s to %s about %s', sender, rcpt, msg_subject)
self.send(sender, rcpt, msg_subject, body)
logger.info('Relayed %s to %s', sender, rcpt)
except Exception as err:
logger.error('Bad to %s: %r', to, err)
continue
return
class smtpServer(StoppableThread):
"""SMTP server thread"""
def __init__(self, _=None):
super(smtpServer, self).__init__(name="smtpServerThread")
self.server = smtpServerPyBitmessage(('127.0.0.1', LISTENPORT), None)
def stopThread(self):
super(smtpServer, self).stopThread()
self.server.close()
return
def run(self):
asyncore.loop(1)
def signals(_, __):
"""Signal handler"""
logger.warning('Got signal, terminating')
for thread in threading.enumerate():
if thread.isAlive() and isinstance(thread, StoppableThread):
thread.stopThread()
def runServer():
"""Run SMTP server as a standalone python process"""
logger.warning('Running SMTPd thread')
smtpThread = smtpServer()
smtpThread.start()
signal.signal(signal.SIGINT, signals)
signal.signal(signal.SIGTERM, signals)
logger.warning('Processing')
smtpThread.join()
logger.warning('The end')
if __name__ == "__main__":
runServer()