-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtollur.py
executable file
·474 lines (335 loc) · 15 KB
/
tollur.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env python3
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
'''tollur - A scriptable SMTP proxy'''
DESCRIPTION=__doc__
VERSION='0.6 / "Cryptic Struggles"'
URL='https://github.com/doctor-love/tollur'
try:
import logging.handlers
import configparser
import importlib
import argparse
import asyncore
import logging
import smtplib
import smtpd
import uuid
import ssl
import sys
except ImportError as missing_module:
print('Failed to load dependencies: "%s"' % missing_module)
sys.exit(3)
# Should probably work with older Python 3 versions as well, but not tested
if sys.version_info < (3, 5):
print('Tollur requires Python 3.5 or later - sorry!\n')
sys.exit(3)
if ssl.OPENSSL_VERSION_INFO < (0, 9, 8):
print('Tollur requires OpenSSL 0.9.8 or later - sorry!\n')
sys.exit(3)
if sys.platform != 'linux':
print('Tollur has only been tested on Linux - sorry!\n')
sys.exit(3)
_log = logging.getLogger('tollur')
# -----------------------------------------------------------------------------
class DebugToLog:
'''Writes smtpd\'s debug stream to standard logging'''
def write(self, msg):
# Let's avoid printing the debug streams empty messages, shall we!
if str(msg).strip():
_log.debug('EXT: smtpd debug stream: "%s"' % str(msg))
def flush(self):
pass
# -----------------------------------------------------------------------------
class MailMessage():
'''Object used by handler plugin to manipulate forwarded message'''
def __init__(self, peer, sender, recipients, data):
self.peer = peer
self.sender = sender
self.recipients = recipients
self.data = data
self.mid = uuid.uuid4()
# -----------------------------------------------------------------------------
class SMTPClient(smtplib.SMTP):
'''SMTP client with some improved logging and various goodies'''
debuglevel = 1
def _print_debug(self, *args):
'''Used to send smtplib\'s debug stream to standard logging'''
# Looks a bit strange, but smtplib passwd *args to print()
msg = ''
for arg in args:
msg += str(arg)
_log.debug('EXT: smtlib debug stream: "%s"' % msg)
# -----------------------------------------------------------------------------
class SMTPSClient(smtplib.SMTP_SSL):
'''SMTPS client with some improved logging and various goodies'''
debuglevel = 1
def _print_debug(self, *args):
'''Used to send smtplib\'s debug stream to standard logging'''
msg = ''
for arg in args:
msg += str(arg)
_log.debug('EXT: smtlib debug stream: "%s"' % msg)
# -----------------------------------------------------------------------------
class SMTPProxy(smtpd.SMTPServer):
'''SMTP proxy with manual confirmation of outgoing messages'''
# -------------------------------------------------------------------------
def configure_upstream_tls(self):
'''Sets up the TLS context for upstream based on user preferences'''
_log.debug('Setting up upstream TLS context...')
# Using a tweaked default context to minimize future "crypto rot"
context = ssl.create_default_context(cafile=self.ca_store)
_log.debug(
'Loaded CA certs for upstream context: "%s"'
% str(context.get_ca_certs()))
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
_log.debug('Configuring TLS protocols for upstream connection')
context.options |= ssl.OP_NO_SSLv3
# TODO: Add later TLS versions once they have been implemented
versions = {
1.0: ssl.OP_NO_TLSv1, 1.1: ssl.OP_NO_TLSv1_1,
1.2: ssl.OP_NO_TLSv1_2}
if self.tls_version:
for version, option in versions.items():
if version < self.tls_version:
_log.debug('Disabling upstream TLS version %s' % version)
context.options |= option
if self.upstream_crl_check == 'chain':
context.verify_flags = ssl.VERIFY_CRL_CHECK_CHAIN
elif self.upstream_crl_check == 'cert':
context.verify_flags = ssl.VERIFY_CRL_CHECK_LEAF
if self.upstream_cipher_suites:
context.set_ciphers(self.upstream_cipher_suites)
return context
# -------------------------------------------------------------------------
def __init__(
self, server_address='127.0.0.1', server_port=9025,
upstream_address=None, upstream_port=25, user=None, password=None,
ca_store=None, tls_mode='start_tls', upstream_cipher_suites='',
tls_version=1.2, upstream_crl_check='chain', handler=None):
self.server_address = str(server_address)
self.server_port = int(server_port)
if upstream_address is None or handler is None:
raise TypeError(
'Argument "upstream_address" and "handler" are required')
self.upstream_address = upstream_address
self.upstream_port = int(upstream_port)
self.user = user
self.password = password
self.ca_store = ca_store
self.tls_mode = tls_mode
self.upstream_cipher_suites = upstream_cipher_suites
self.upstream_crl_check = upstream_crl_check
self.handler = handler
if tls_version:
self.tls_version = float(tls_version)
else:
self.tls_version = tls_version
if self.tls_mode:
self.tls_mode = tls_mode
self.upstream_tls_context = self.configure_upstream_tls()
super(SMTPProxy, self).__init__(
(self.server_address, self.server_port),
(self.upstream_address, self.upstream_port))
# -------------------------------------------------------------------------
def handle_accept(self):
'''Modified connection handler, as the super does not expose channel'''
pair = self.accept()
if pair is not None:
connection, address = pair
_log.debug('Incoming connection from %s' % repr(address))
self.channel = smtpd.SMTPChannel(self, connection, address)
# -------------------------------------------------------------------------
def _deliver(self, msg):
'''Sends processed messages with SMTP(S) to upstream server'''
_log.info(
'Delivering message with ID "%s" from "%s" to "%s"'
% (msg.mid, msg.sender, msg.recipients))
try:
_log.debug(
'Starting SMTP(S) session with server "%s:%s"'
% (self.upstream_address, self.upstream_port))
if self.tls_mode is None or self.tls_mode == "start_tls":
ses = SMTPClient(self.upstream_address, self.upstream_port)
else:
ses = SMTPSClient(
self.upstream_address, self.upstream_port,
context=self.upstream_tls_context)
if self.tls_mode == "start_tls":
ses.starttls(context=self.upstream_tls_context)
if self.tls_mode:
_log.debug(
'Established upstream connection - TLS session info: "%s"'
% str(self.upstream_tls_context.session_stats()))
# -----------------------------------------------------------------
if self.user and self.password:
_log.debug('Trying to authenticate as user "%s"' % self.user)
ses.login(self.user, self.password)
ses.sendmail(msg.sender, msg.recipients, msg.data)
if self.tls_mode:
_log.debug(
'Finished upstream connection - TLS session info: "%s"'
% str(self.upstream_tls_context.session_stats()))
except Exception as error_msg:
_log.error(
'Failed to deliver message with ID "%s": "%s"'
% (msg.mid, error_msg))
self.channel.push('451 Error: Could not deliver ID "%s"' % msg.mid)
return
finally:
try:
ses.quit()
# This exception is raised if the session failed to be established
except UnboundLocalError:
pass
except Exception as error_msg:
_log.debug(
'Failed to close session gracefully for ID "%s": "%s"'
% (msg.mid, error_msg))
return
# -------------------------------------------------------------------------
def process_message(self, peer, sender, recipients, data):
'''Calls handler to check if incoming e-mail should be sent'''
msg = MailMessage(peer, sender, recipients, data)
_log.info(
'Proxy received incoming mail - '
'ID: "%s", peer: "%s", sender: "%s", recipients: "%s"'
% (msg.mid, msg.peer, msg.sender, msg.recipients))
try:
forward, msg = self.handler.process(msg)
if forward:
_log.info('Handler accepted message ID "%s"' % msg.mid)
self._deliver(msg)
return
else:
_log.error('Handler did not accept message ID "%s"' % msg.mid)
self.channel.push(
'550 Error: ID "%s" was denied by handler' % msg.mid)
return
except Exception as error_msg:
raise Exception(
'Handler raised unhandled exception: "%s"' % error_msg)
# -----------------------------------------------------------------------------
def parse_arguments():
'''Parses command line arguments'''
parser = argparse.ArgumentParser(
description=DESCRIPTION,
epilog='For information about configuration options, see: %s' % URL)
parser.add_argument(
dest='conf_file',
metavar='/path/to/conf.ini', type=argparse.FileType('r'),
help='Path to tollur configuration file')
parser.add_argument(
'-V', '--version',
action='version', version=VERSION,
help='Show application version and exit')
return parser.parse_args()
# -----------------------------------------------------------------------------
def parse_conf(conf_file):
'''Parses configuration file as INI an checks required values'''
conf = configparser.ConfigParser()
try:
conf.read_file(conf_file)
# Various error checking of provided configuration file
for section in ['main', 'server', 'upstream']:
if not section in conf:
raise Exception(
'Section "%s" required in configurationi file' % section)
if not 'handler' in conf['main']:
raise Exception('Handler needs to be specified in "main" section')
if not 'handler-' + conf['main']['handler'] in conf:
raise Exception('Configuration section for handler is required')
except Exception as error_msg:
raise Exception('Failed to parse configuration file: "%s"' % error_msg)
# TODO: Add more error checking of configuration
return conf
# -----------------------------------------------------------------------------
def setup_logging(dest, level):
'''Configures application logging settings'''
log_formatter = logging.Formatter(
'tollur: %(levelname)s - %(message)s')
if level == "debug":
_log.setLevel(logging.DEBUG)
elif level == "error":
_log.setLevel(logging.ERROR)
else:
_log.setLevel(logging.INFO)
if dest == 'stderr':
log_handler = logging.StreamHandler()
elif dest == 'syslog':
log_handler = logging.handlers.SysLogHandler(address='/dev/log')
log_handler.setFormatter(log_formatter)
_log.addHandler(log_handler)
return
# -----------------------------------------------------------------------------
def init_handler(name, conf):
'''Loads handler module and sets it up with provided configuration'''
_log.debug('Loading handler module "%s"' % name)
try:
handler_module = importlib.import_module('handlers.' + name)
except Exception as error_msg:
raise Exception(
'Failed to loader handler module "%s": "%s"'
% (name, error_msg))
# -------------------------------------------------------------------------
_log.debug('Initializing handler module...')
try:
handler = handler_module.Handler(conf)
except Exception as error_msg:
raise Exception(
'Failed to initialize handler module "%s": "%s"'
% (name, error_msg))
return handler
# -----------------------------------------------------------------------------
def main():
'''Main application function'''
args = parse_arguments()
try:
conf = parse_conf(args.conf_file)
except Exception as error_msg:
# _log is not used here since it's settings are specified in the config
print(error_msg)
sys.exit(1)
setup_logging(conf['main']['log_dest'], conf['main']['log_level'])
# -------------------------------------------------------------------------
if not conf.getboolean('main', 'iunderstandwhatiamdoing'):
_log.error(
'Tollur is experimental software - read through the source code, '
'example configuration, open issues and try again! :-)')
sys.exit(3)
# -------------------------------------------------------------------------
try:
handler = conf['main']['handler']
handler = init_handler(handler, conf['handler-' + handler])
except Exception as error_msg:
_log.error(error_msg)
sys.exit(1)
# -------------------------------------------------------------------------
# Needed to prevent information leakage from the SMTP server
smtpd.__version__ = 'SMTP PROXY'
smtpd.DEBUGSTREAM = DebugToLog()
try:
smtp_server = SMTPProxy(
conf['server']['address'], int(conf['server']['port']),
conf['upstream']['address'], int(conf['upstream']['port']),
conf['upstream']['user'], conf['upstream']['password'],
conf['upstream']['ca_store'], conf['upstream']['tls_mode'],
conf['upstream']['cipher_suites'], conf['upstream']['tls_version'],
conf['upstream']['crl_check'], handler)
except Exception as error_msg:
_log.error('Failed to configure SMTP proxy: "%s"' % error_msg)
sys.exit(1)
# -------------------------------------------------------------------------
_log.info(
'Starting Tollur SMTP proxy - listening on %s:%i...'
% (conf['server']['address'], int(conf['server']['port'])))
try:
asyncore.loop()
except KeyboardInterrupt:
_log.info('Tollur was interrupted by keyboard - exiting...')
sys.exit(3)
except Exception as error_msg:
_log.error('SMTP proxy generated unhandled error: "%s"' % error_msg)
sys.exit(1)
if __name__ == '__main__':
main()