forked from tintinweb/python-smtpd-tls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmtpd_tls.py
380 lines (344 loc) · 13.2 KB
/
smtpd_tls.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
# Author : [email protected] <github.com/tintinweb>
"""An RFC 2821 smtp proxy with implicit TLS and explicit STARTTLS (RFC 3207) support
Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]]
Options:
--nosetuid
-n
This program generally tries to setuid `nobody', unless this flag is
set. The setuid call will fail if this program is not run as root (in
which case, use this flag).
--version
-V
Print the version number and exit.
--class classname
-c classname
Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by
default.
--debug
-d
Turn on debugging prints.
--keyfile
-k
ssl private key and certificate file (server.pem)
--tls
-t
implicit TLS mode (cannot be combined with --starttls)
--starttls
-s
explicit RFC 3207 STARTTLS mode (cannot be combined with --tls)
--auth
-a
authentification with AUTH PLAIN, set as dict like
'{"user":"username","password":"secret"}'
--help
-h
Print this message and exit.
Version: %(__version__)s
If localhost is not given then `localhost' is used, and if localport is not
given then 8025 is used. If remotehost is not given then `localhost' is used,
and if remoteport is not given, then 25 is used.
"""
import ast
import sys
import os
import getopt
import asyncore, asynchat
import ssl
import smtpd
from email.base64mime import encode as encode_base64
__all__ = ["SMTPServer", "DebuggingServer", "PureProxy", "MailmanProxy"]
__version__ = smtpd.__version__ + " (TLS and STARTTLS enabled)"
program = sys.argv[0]
class SMTPChannel(smtpd.SMTPChannel):
def smtp_AUTH(self, arg):
user = self.__server.auth.get('user')
password = self.__server.auth.get('password')
s = encode_base64("\0%s\0%s" % (user, password), eol="")
if arg == 'PLAIN {}'.format(s):
self.push('235 Authentication successful')
else:
self.push(
'535 SMTP Authentication unsuccessful/'
'Bad username or password')
def smtp_EHLO(self, arg):
if not arg:
self.push('501 Syntax: HELO hostname')
elif self.__greeting:
self.push('503 Duplicate HELO/EHLO')
else:
self.__greeting = arg
if isinstance(self.__conn,ssl.SSLSocket):
self.push('250-%s' % self.__fqdn)
self.push('250 AUTH PLAIN')
else:
self.push('250-%s' % self.__fqdn)
self.push('250 STARTTLS')
def smtp_STARTTLS(self, arg):
if arg:
self.push('501 Syntax error (no parameters allowed)')
elif self.__server.starttls and not isinstance(self.__conn,ssl.SSLSocket):
self.push('220 Ready to start TLS')
self.__conn.settimeout(30)
self.__conn = self.__server.ssl_ctx.wrap_socket(self.__conn, server_side=True)
self.__conn.settimeout(None)
# re-init channel
asynchat.async_chat.__init__(self, self.__conn)
self.__line = []
self.__state = self.COMMAND
self.__greeting = 0
self.__mailfrom = None
self.__rcpttos = []
self.__data = ''
print >> smtpd.DEBUGSTREAM, 'Peer: %s - negotiated TLS: %s' % (repr(self.__addr), repr(self.__conn.cipher()))
else:
self.push('454 TLS not available due to temporary reason')
class SMTPServer(smtpd.SMTPServer):
def __init__(self, localaddr, remoteaddr, ssl_ctx=None, starttls=True, auth= False):
self.ssl_ctx = ssl_ctx
self.starttls = starttls
self.auth = auth
smtpd.SMTPServer.__init__(self, localaddr, remoteaddr)
print >> smtpd.DEBUGSTREAM, '\tTLS Mode: %s\n\tTLS Context: %s' % ('explicit (plaintext until STARTTLS)' if starttls else 'implicit (encrypted from the beginning)',
repr(ssl_ctx))
if self.auth:
print >> smtpd.DEBUGSTREAM, '\tAUTH PLAIN Mode\n'
def handle_accept(self):
pair = self.accept()
if pair is not None:
conn, addr = pair
print >> smtpd.DEBUGSTREAM, 'Incoming connection from %s' % repr(addr)
if self.ssl_ctx and not self.starttls:
conn = self.ssl_ctx.wrap_socket(conn, server_side=True)
print >> smtpd.DEBUGSTREAM, 'Peer: %s - negotiated TLS: %s' % (repr(addr), repr(conn.cipher()))
channel = SMTPChannel(self, conn, addr)
class DebuggingServer(SMTPServer):
# Do something with the gathered message
def process_message(self, peer, mailfrom, rcpttos, data):
inheaders = 1
lines = data.split('\n')
print '---------- MESSAGE FOLLOWS ----------'
for line in lines:
# headers first
if inheaders and not line:
print 'X-Peer:', peer[0]
inheaders = 0
print line
print '------------ END MESSAGE ------------'
class PureProxy(SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
lines = data.split('\n')
# Look for the last header
i = 0
for line in lines:
if not line:
break
i += 1
lines.insert(i, 'X-Peer: %s' % peer[0])
data = NEWLINE.join(lines)
refused = self._deliver(mailfrom, rcpttos, data)
# TBD: what to do with refused addresses?
print >> DEBUGSTREAM, 'we got some refusals:', refused
def _deliver(self, mailfrom, rcpttos, data):
import smtplib
refused = {}
try:
s = smtplib.SMTP()
s.connect(self._remoteaddr[0], self._remoteaddr[1])
try:
refused = s.sendmail(mailfrom, rcpttos, data)
finally:
s.quit()
except smtplib.SMTPRecipientsRefused, e:
print >> DEBUGSTREAM, 'got SMTPRecipientsRefused'
refused = e.recipients
except (socket.error, smtplib.SMTPException), e:
print >> DEBUGSTREAM, 'got', e.__class__
# All recipients were refused. If the exception had an associated
# error code, use it. Otherwise,fake it with a non-triggering
# exception code.
errcode = getattr(e, 'smtp_code', -1)
errmsg = getattr(e, 'smtp_error', 'ignore')
for r in rcpttos:
refused[r] = (errcode, errmsg)
return refused
class MailmanProxy(PureProxy):
def process_message(self, peer, mailfrom, rcpttos, data):
from cStringIO import StringIO
from Mailman import Utils
from Mailman import Message
from Mailman import MailList
# If the message is to a Mailman mailing list, then we'll invoke the
# Mailman script directly, without going through the real smtpd.
# Otherwise we'll forward it to the local proxy for disposition.
listnames = []
for rcpt in rcpttos:
local = rcpt.lower().split('@')[0]
# We allow the following variations on the theme
# listname
# listname-admin
# listname-owner
# listname-request
# listname-join
# listname-leave
parts = local.split('-')
if len(parts) > 2:
continue
listname = parts[0]
if len(parts) == 2:
command = parts[1]
else:
command = ''
if not Utils.list_exists(listname) or command not in (
'', 'admin', 'owner', 'request', 'join', 'leave'):
continue
listnames.append((rcpt, listname, command))
# Remove all list recipients from rcpttos and forward what we're not
# going to take care of ourselves. Linear removal should be fine
# since we don't expect a large number of recipients.
for rcpt, listname, command in listnames:
rcpttos.remove(rcpt)
# If there's any non-list destined recipients left,
print >> DEBUGSTREAM, 'forwarding recips:', ' '.join(rcpttos)
if rcpttos:
refused = self._deliver(mailfrom, rcpttos, data)
# TBD: what to do with refused addresses?
print >> DEBUGSTREAM, 'we got refusals:', refused
# Now deliver directly to the list commands
mlists = {}
s = StringIO(data)
msg = Message.Message(s)
# These headers are required for the proper execution of Mailman. All
# MTAs in existence seem to add these if the original message doesn't
# have them.
if not msg.getheader('from'):
msg['From'] = mailfrom
if not msg.getheader('date'):
msg['Date'] = time.ctime(time.time())
for rcpt, listname, command in listnames:
print >> DEBUGSTREAM, 'sending message to', rcpt
mlist = mlists.get(listname)
if not mlist:
mlist = MailList.MailList(listname, lock=0)
mlists[listname] = mlist
# dispatch on the type of command
if command == '':
# post
msg.Enqueue(mlist, tolist=1)
elif command == 'admin':
msg.Enqueue(mlist, toadmin=1)
elif command == 'owner':
msg.Enqueue(mlist, toowner=1)
elif command == 'request':
msg.Enqueue(mlist, torequest=1)
elif command in ('join', 'leave'):
# TBD: this is a hack!
if command == 'join':
msg['Subject'] = 'subscribe'
else:
msg['Subject'] = 'unsubscribe'
msg.Enqueue(mlist, torequest=1)
class Options:
setuid = 1
classname = 'PureProxy'
sslctx = None
starttls = True
auth = None
def usage(code, msg=''):
print >> sys.stderr, __doc__ % globals()
if msg:
print >> sys.stderr, msg
sys.exit(code)
def parseargs():
global DEBUGSTREAM
try:
opts, args = getopt.getopt(
sys.argv[1:], 'nVhc:dk:ts',
['class=', 'nosetuid', 'version', 'help', 'debug', 'keyfile=', 'tls', 'starttls', 'auth='])
except getopt.error, e:
usage(1, e)
options = Options()
for opt, arg in opts:
if opt in ('-h', '--help'):
usage(0)
elif opt in ('-V', '--version'):
print >> sys.stderr, __version__
sys.exit(0)
elif opt in ('-n', '--nosetuid'):
options.setuid = 0
elif opt in ('-c', '--class'):
options.classname = arg
elif opt in ('-d', '--debug'):
smtpd.DEBUGSTREAM = sys.stderr
elif opt in ('-k', '--keyfile'):
options.sslctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
options.sslctx.load_cert_chain(certfile=arg, keyfile=arg)
elif opt in ('-t', '--tls'):
options.starttls = False
elif opt in ('-s', '--starttls'):
options.starttls = True
elif opt in ('-a', '--auth'):
options.auth = ast.literal_eval(arg)
# parse the rest of the arguments
if len(args) < 1:
localspec = 'localhost:8025'
remotespec = 'localhost:25'
elif len(args) < 2:
localspec = args[0]
remotespec = 'localhost:25'
elif len(args) < 3:
localspec = args[0]
remotespec = args[1]
else:
usage(1, 'Invalid arguments: %s' % COMMASPACE.join(args))
# split into host/port pairs
i = localspec.find(':')
if i < 0:
usage(1, 'Bad local spec: %s' % localspec)
options.localhost = localspec[:i]
try:
options.localport = int(localspec[i+1:])
except ValueError:
usage(1, 'Bad local port: %s' % localspec)
i = remotespec.find(':')
if i < 0:
usage(1, 'Bad remote spec: %s' % remotespec)
options.remotehost = remotespec[:i]
try:
options.remoteport = int(remotespec[i+1:])
except ValueError:
usage(1, 'Bad remote port: %s' % remotespec)
return options
if __name__ == '__main__':
options = parseargs()
# Become nobody
classname = options.classname
if "." in classname:
lastdot = classname.rfind(".")
mod = __import__(classname[:lastdot], globals(), locals(), [""])
classname = classname[lastdot+1:]
else:
import __main__ as mod
class_ = getattr(mod, classname)
proxy = class_((options.localhost, options.localport),
(options.remotehost, options.remoteport), options.sslctx, options.starttls, options.auth)
if options.setuid:
try:
import pwd
except ImportError:
print >> sys.stderr, \
'Cannot import module "pwd"; try running with -n option.'
sys.exit(1)
nobody = pwd.getpwnam('nobody')[2]
try:
os.setuid(nobody)
except OSError, e:
if e.errno != errno.EPERM: raise
print >> sys.stderr, \
'Cannot setuid "nobody"; try running with -n option.'
sys.exit(1)
try:
asyncore.loop()
except KeyboardInterrupt:
pass