This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmongo_mail_server.py
1620 lines (1310 loc) · 51.6 KB
/
mongo_mail_server.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
__VERSION__ = "0.1.1"
from gevent.monkey import patch_all
patch_all() # noqa
from pprint import pprint as pp
import json
import sys
import zlib
import base64
import datetime
import logging
import smtplib
import uuid
import hashlib
from ssl import CERT_NONE
import errno
from asynchat import find_prefix_at_end
import atexit
import os
import six
from six.moves import UserDict
from decouple import config as env_config
from gevent import ssl
from gevent import socket, Timeout
from gevent.server import StreamServer
try:
import pymongo
from pymongo import MongoClient
from bson import ObjectId
HAVE_PYMONGO = True
if pymongo.version_tuple[0] < 3:
PYMONGO2 = True
else:
PYMONGO2 = False
except ImportError:
HAVE_PYMONGO = False
from dateutil import tz
__all__ = ["SMTPServer", "DebuggingServer", "PureProxy", 'SSLSettings',
'RecordPyMongoDBServer', 'RecordPyMongoDBServerProxy']
logger = logging.getLogger('mongo-mail-server')
NEWLINE = '\n'
EMPTYSTRING = ''
COMMASPACE = ', '
class SMTPChannel(object):
"""
Port from stdlib smtpd used by Gevent
"""
COMMAND = 0
DATA = 1
def __init__(self, server, conn, addr, data_size_limit=0, fqdn=None):
self.server = server
self.conn = conn
self.addr = addr
self.line = []
self.state = self.COMMAND
self.seen_greeting = 0
self.mailfrom = None
self.rcpttos = []
self.data = ''
self.fqdn = fqdn
self.ac_in_buffer_size = 4096
self.xforward_enable = True
self.xforward_name = ''
self.xforward_addr = ''
self.xforward_helo = ''
self.ac_in_buffer = ''
self.closed = False
self.data_size_limit = data_size_limit # in byte
self.current_size = 0
self.tls = False
try:
self.peer = conn.getpeername()
except socket.error as err:
# a race condition may occur if the other end is closing
# before we can get the peername
logger.exception(err)
self.conn.close()
if err[0] != errno.ENOTCONN:
raise
return
self.push('220 %s SMTPD at your service' % self.fqdn)
self.terminator = '\r\n'
logger.debug('SMTP channel initialized')
# Overrides base class for convenience
def push(self, msg):
logger.debug('PUSH %s' % msg)
payload = (msg + '\r\n').encode('utf-8')
self.conn.send(payload)
# Implementation of base class abstract method
def collect_incoming_data(self, data):
self.line.append(data)
self.current_size += len(data)
if self.data_size_limit > 0 and self.current_size > self.data_size_limit:
self.push('452 Command has been aborted because mail too big')
self.close_when_done()
# Implementation of base class abstract method
def found_terminator(self):
line = EMPTYSTRING.join(self.line)
self.line = []
if self.state == self.COMMAND:
if not line:
self.push('500 Error: bad syntax')
return
method = None
i = line.find(' ')
if i < 0:
command = line.upper().strip()
arg = None
else:
command = line[:i].upper()
arg = line[i + 1:].strip()
method = getattr(self, 'smtp_' + command, None)
logger.debug('%s:%s', command, arg)
if not method:
self.push('502 Error: command "%s" not implemented' % command)
return
method(arg)
return
else:
if self.state != self.DATA:
self.push('451 Internal confusion')
return
# Remove extraneous carriage returns and de-transparency according
# to RFC 821, Section 4.5.2.
data = []
for text in line.split('\r\n'):
if text and text[0] == '.':
data.append(text[1:])
else:
data.append(text)
self.data = NEWLINE.join(data)
xforward = {}
if self.xforward_enable:
xforward['NAME'] = self.xforward_name
xforward['ADDR'] = self.xforward_addr
xforward['HELO'] = self.xforward_helo
logger.debug("XFORWARD NAME=%(NAME)s ADDR=%(ADDR)s HELO=%(HELO)s" % xforward)
# peer, mailfrom, rcpttos, data, xforward
status = self.server.process_message(self.peer,
self.mailfrom,
self.rcpttos,
self.data,
xforward)
self.rcpttos = []
self.mailfrom = None
self.xforward_name = ''
self.xforward_addr = ''
self.xforward_helo = ''
self.state = self.COMMAND
self.terminator = '\r\n'
if not status:
self.push('250 Ok')
else:
self.push(status)
# SMTP and ESMTP commands
def smtp_HELO(self, arg):
if not arg:
self.push('501 Syntax: HELO hostname')
return
if self.seen_greeting:
self.push('503 Duplicate HELO/EHLO')
else:
self.seen_greeting = arg
self.push('250 %s' % self.fqdn)
def smtp_EHLO(self, arg):
if not arg:
self.push('501 Syntax: EHLO hostname')
return
if self.seen_greeting:
self.push('503 Duplicate HELO/EHLO')
else:
self.seen_greeting = arg
self.extended_smtp = True
if self.tls:
self.push('250-%s on TLS' % self.fqdn)
else:
self.push('250-%s on plain' % self.fqdn)
try:
if self.server.ssl and not self.tls:
self.push('250-STARTTLS')
except AttributeError:
pass
if self.data_size_limit > 0:
self.push('250-SIZE %s' % self.data_size_limit)
if self.xforward_enable:
# self.push('250-XFORWARD NAME ADDR HELO') #250-XFORWARD NAME ADDR PROTO HELO
self.push('250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT')
# amavis: XFORWARD NAME ADDR PORT PROTO HELO IDENT SOURCE
# postfix: 250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT
self.push('250 HELP')
def smtp_NOOP(self, arg):
if arg:
self.push('501 Syntax: NOOP')
else:
self.push('250 Ok')
def smtp_QUIT(self, arg=""):
# args is ignored
self.push('221 Bye')
self.close_when_done()
def smtp_TIMEOUT(self, arg=""):
self.push('421 2.0.0 Bye')
self.close_when_done()
# factored
def getaddr(self, keyword, arg):
address = None
keylen = len(keyword)
if arg[:keylen].upper() == keyword:
address = arg[keylen:].strip()
if not address:
pass
elif address[0] == '<' and address[-1] == '>' and address != '<>':
# Addresses can be in the form <[email protected]> but watch out
# for null address, e.g. <>
address = address[1:-1]
return address
def smtp_XFORWARD(self, arg):
"""
> support ESMTP postfix:
250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT
> support ESMTP mongomail:
250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT
> support ESMTP amavis:
250 XFORWARD NAME ADDR PORT PROTO HELO SOURCE
attribute-name = ( NAME | ADDR | PORT | PROTO | HELO | IDENT | SOURCE )
> amavis sent:
XFORWARD ADDR=209.85.213.175 NAME=mail-ig0-f175.google.com PORT=38689 PROTO=ESMTP HELO=mail-ig0-f175.google.com SOURCE=REMOTE
"""
logger.debug('XFORWARD %s' % arg)
if not arg:
self.push('501 Syntax: XFORWARD')
elif self.mailfrom:
self.push('503 Error: XFORWARD after MAIL command')
return
else:
attrs = arg.split(' ')
for i in attrs:
attr, value = i.split('=', 1)
if attr == 'NAME':
self.xforward_name = value
elif attr == 'ADDR':
self.xforward_addr = value
elif attr == 'HELO':
self.xforward_helo = value
self.push('250 Ok')
def smtp_MAIL(self, arg):
if not self.seen_greeting:
self.push('503 Error: send HELO first')
return
address = self.getaddr('FROM:', arg.split()[0]) if arg else None
if not address:
self.push('501 Syntax: MAIL FROM:<address>')
return
if self.mailfrom:
self.push('503 Error: nested MAIL command')
return
self.mailfrom = address
self.push('250 Ok')
def smtp_RCPT(self, arg):
if not self.mailfrom:
self.push('503 Error: need MAIL command')
return
address = self.getaddr('TO:', arg) if arg else None
if not address:
self.push('501 Syntax: RCPT TO: <address>')
return
result = self.server.process_rcpt(address)
if not result:
self.rcpttos.append(address)
self.push('250 Ok')
else:
self.push(result)
def smtp_RSET(self, arg):
if arg:
self.push('501 Syntax: RSET')
return
# Resets the sender, recipients, and data, but not the greeting
self.mailfrom = None
self.rcpttos = []
self.data = ''
self.xforward_name = ''
self.xforward_addr = ''
self.xforward_helo = ''
self.state = self.COMMAND
self.push('250 Ok')
def smtp_DATA(self, arg):
if not self.rcpttos:
self.push('503 Error: need RCPT command')
return
if arg:
self.push('501 Syntax: DATA')
return
self.state = self.DATA
self.terminator = '\r\n.\r\n'
self.push('354 End data with <CR><LF>.<CR><LF>')
def smtp_STARTTLS(self, arg):
if arg:
self.push('501 Syntax: STARTTLS')
return
self.push('220 Ready to start TLS')
if self.data:
self.push('500 Too late to changed')
return
try:
self.conn = ssl.wrap_socket(self.conn, **self.server.ssl)
self.state = self.COMMAND
self.seen_greeting = 0
self.rcpttos = []
self.mailfrom = None
self.tls = True
except Exception as err:
logger.error(err, exc_info=True)
self.push('503 certificate is FAILED')
self.close_when_done()
def smtp_HELP(self, arg):
if arg:
if arg == 'ME':
self.push('504 Go to https://github.com/radical-software/mongo-mail-server for help')
else:
self.push('501 Syntax: HELP')
else:
self.push('214 SMTP server is running...go to website for further help')
def handle_read(self):
try:
data = self.conn.recv(self.ac_in_buffer_size).decode('ascii')
if len(data) == 0:
# issues 2 TCP connect closed will send a 0 size pack
self.close_when_done()
except socket.error:
self.handle_error()
return
self.ac_in_buffer = self.ac_in_buffer + data
# Continue to search for self.terminator in self.ac_in_buffer,
# while calling self.collect_incoming_data. The while loop
# is necessary because we might read several data+terminator
# combos with a single recv(4096).
while self.ac_in_buffer:
lb = len(self.ac_in_buffer)
logger.debug(self.ac_in_buffer)
if not self.terminator:
# no terminator, collect it all
self.collect_incoming_data(self.ac_in_buffer)
self.ac_in_buffer = ''
elif isinstance(self.terminator, six.integer_types):
# numeric terminator
n = self.terminator
if lb < n:
self.collect_incoming_data(self.ac_in_buffer)
self.ac_in_buffer = ''
self.terminator = self.terminator - lb
else:
self.collect_incoming_data(self.ac_in_buffer[:n])
self.ac_in_buffer = self.ac_in_buffer[n:]
self.terminator = 0
self.found_terminator()
else:
# 3 cases:
# 1) end of buffer matches terminator exactly:
# collect data, transition
# 2) end of buffer matches some prefix:
# collect data to the prefix
# 3) end of buffer does not match any prefix:
# collect data
terminator_len = len(self.terminator)
index = self.ac_in_buffer.find(self.terminator)
if index != -1:
# we found the terminator
if index > 0:
# don't bother reporting the empty string (source of subtle bugs)
self.collect_incoming_data(self.ac_in_buffer[:index])
self.ac_in_buffer = self.ac_in_buffer[index + terminator_len:]
# This does the Right Thing if the terminator is changed here.
self.found_terminator()
else:
# check for a prefix of the terminator
index = find_prefix_at_end(self.ac_in_buffer, self.terminator)
if index:
if index != lb:
# we found a prefix, collect up to the prefix
self.collect_incoming_data(self.ac_in_buffer[:-index])
self.ac_in_buffer = self.ac_in_buffer[-index:]
break
else:
# no prefix, collect it all
self.collect_incoming_data(self.ac_in_buffer)
self.ac_in_buffer = ''
def handle_error(self):
self.close_when_done()
def close_when_done(self):
if not self.conn.closed:
logger.debug('CLOSED %s' % self.conn)
self.conn.close()
self.closed = True
def load_plugin(name, callable_name='apply'):
"""Load module by name string
>>> mod = load_plugin("rs_common.tools.python_tools")
>>> hasattr(mod, callable_name)
True
>>> "rs_common.tools.python_tools".split(".")[-1:]
['python_tools']
>>> "rs_common.tools.python_tools".split(".")[:-1]
['rs_common', 'tools']
"""
if not name:
return None
module_name = name
if module_name not in sys.modules:
__import__(module_name)
mod = sys.modules[module_name]
if not hasattr(mod, callable_name):
raise Exception("module [%s] not contains callable: %s" % (name, callable_name))
method = getattr(mod, callable_name)
if not callable(method):
raise Exception("plugin not callable")
return method
def compress(data):
return base64.b64encode(zlib.compress(data.encode('utf-8')))
def uncompress(data):
return zlib.decompress(base64.b64decode(data)).decode('utf-8')
def timestamp():
dt = datetime.datetime.utcnow()
return datetime.datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond, tz.tzutc())
def generate_key():
"""Génère un ID unique de 64 caractères"""
new_uuid = str(uuid.uuid4()).encode('ascii')
return hashlib.sha256(new_uuid).hexdigest()
def extract_real_recipients(data):
from email.parser import HeaderParser
from email.utils import getaddresses
recipients = []
try:
msg = HeaderParser().parsestr(data, headersonly=True)
getall = msg.get_all('X-Envelope-To', [])
recipients = [r[1] for r in getaddresses(getall)]
except Exception as err:
logger.exception(str(err))
return recipients
class ConnectionTimeout(BaseException):
pass
class SSLSettings(UserDict):
"""SSL settings object"""
def __init__(self, keyfile=None, certfile=None,
ssl_version='PROTOCOL_SSLv23', ca_certs=None,
do_handshake_on_connect=True, cert_reqs=CERT_NONE,
suppress_ragged_eofs=True, ciphers=None, **kwargs):
"""settings of SSL
:param keyfile: SSL key file path usally end with ".key"
:param certfile: SSL cert file path usally end with ".crt"
"""
UserDict.__init__(self)
self.data.update(dict(
keyfile=keyfile,
certfile=certfile,
server_side=True,
ssl_version=getattr(ssl, ssl_version, ssl.PROTOCOL_SSLv23),
ca_certs=ca_certs,
do_handshake_on_connect=do_handshake_on_connect,
cert_reqs=cert_reqs,
suppress_ragged_eofs=suppress_ragged_eofs,
ciphers=ciphers))
class SMTPServer(StreamServer):
"""Abstracted SMTP server
"""
def __init__(self, localaddr=None, remoteaddr=None,
timeout=60, data_size_limit=0, fqdn=None, debug=False, plugins=[], **kwargs):
"""Initialize SMTP Server
:param localaddr: tuple pair that start server, like `('127.0.0.1', 25)`
:param remoteaddr: ip address (string or list) that can relay on this server
:param timeout: int that connection Timeout
:param data_size_limit: max byte per mail data
:param kwargs: other key-arguments will pass to :class:`.SSLSettings`
"""
self.relay = bool(remoteaddr)
self.remoteaddr = remoteaddr
self.localaddr = localaddr
if not self.localaddr:
self.localaddr = ('127.0.0.1', 25)
self.ssl = None
self.timeout = int(timeout)
self.data_size_limit = int(data_size_limit)
self.fqdn = fqdn or socket.getfqdn()
self._plugins = plugins
self.debug = debug
if 'keyfile' in kwargs:
self.ssl = SSLSettings(**kwargs)
super(SMTPServer, self).__init__(self.localaddr, self.handle)
def handle(self, sock, addr):
logger.debug('Incomming connection %s:%s', *addr[:2])
if self.relay and not addr[0] in self.remoteaddr:
logger.debug('Not in remoteaddr', *addr[:2])
return
try:
with Timeout(self.timeout, ConnectionTimeout):
sc = SMTPChannel(self, sock, addr,
data_size_limit=self.data_size_limit,
fqdn=self.fqdn,
)
while not sc.closed:
sc.handle_read()
except ConnectionTimeout:
logger.warn('%s:%s Timeouted', *addr[:2])
try:
sc.smtp_TIMEOUT()
except Exception as err:
logger.debug(err)
except Exception as err:
logger.exception(err)
# API for "doing something useful with the message"
def process_message(self, peer, mailfrom, rcpttos, data, xforward):
"""Override this abstract method to handle messages from the client.
:param peer: is a tuple containing (ipaddr, port) of the client that made\n
the socket connection to our smtp port.
:param mailfrom: is the raw address the client claims the message is coming from.
:param rcpttos: is a list of raw addresses the client wishes to deliver the message to.
:param data: is a string containing the entire full text of the message,\n
headers (if supplied) and all. It has been `de-transparencied'\n
according to RFC 821, Section 4.5.2.\n
In other words, a line containing a `.' followed by other text has had the leading dot
removed.
This function should return None, for a normal `250 Ok' response;
otherwise it returns the desired response string in RFC 821 format.
"""
raise NotImplementedError
# API that handle rcpt
def process_rcpt(self, address):
"""Override this abstract method to handle rcpt from the client
:param address: is raw address the client wishes to deliver the message to
This function should return None, for a normal `250 Ok' response;
otherwise it returns the desired response string in RFC 821 format.
"""
pass
class DebuggingServer(SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data, xforward):
d = dict(store_key=None,
sender=mailfrom.strip().lower(),
rcpt=rcpttos,
rcpt_count=len(rcpttos),
client_address=xforward.get('ADDR', ''),
xforward=xforward,
server=peer[0],
received=timestamp(),
rcpt_refused={})
if self._plugins:
for plugin in self._plugins:
logger.debug("run plugin[%s]" % plugin)
plugin(metadata=d, data=data)
print("debug server process_message...")
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, xforward):
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?
logger.debug('we got some refusals: %s', refused)
def _deliver(self, mailfrom, rcpttos, data):
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 as e:
logger.debug('got SMTPRecipientsRefused')
refused = e.recipients
except (socket.error, smtplib.SMTPException) as e:
logger.debug('got %s', 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 RecordPyMongoDBServer(SMTPServer):
"""Record message to Mongo Server"""
def __init__(self,
allow_hosts=[],
db=None,
colname=None,
real_rcpt=False,
**kwargs):
SMTPServer.__init__(self, **kwargs)
self._allow_hosts = allow_hosts
self.db = db
self.colname = colname
self.real_rcpt = real_rcpt
self.col = self.db[self.colname]
from gridfs import GridFS
self.fs = GridFS(self.db)
def _security_check(self, address):
"""
fail2ban: 2015-02-21 09:57:05 [5972] [CRITICAL] reject host [127.0.0.1]
"""
if not self._allow_hosts:
return True
try:
host = address[0]
if host not in self._allow_hosts:
logger.critical("reject host [%s]" % host)
return False
return True
except Exception as err:
logger.exception(str(err))
return False
def handle(self, sock, addr):
if not self._security_check(addr):
sock.close()
return
return SMTPServer.handle(self, sock, addr)
def _record_mongodb(self, peer, mailfrom, rcpttos, data, xforward, refused=None):
"""
- pour search with reader:
TODO: extract ('X-Quarantine-ID', '<QX1Er+4cACI0>')
TODO: extract ('Message-ID', '<CAL1AQgSvrDtem3v8hK4dVWKYprcmo9kxyOVjyv9=txe7yRD_Og@mail.gmail.com>')
"""
key = generate_key()
recipients = []
if len(rcpttos) == 1 and self.real_rcpt:
recipients = extract_real_recipients(data)
lines = data.split('\n')
rcpt_header = "X-MMS-RCPT: <%s>" % rcpttos[0]
lines.insert(0, rcpt_header)
data = "\n".join(lines)
else:
for r in rcpttos:
recipients.append(r.strip().lower())
d = dict(store_key=key,
sender=mailfrom.strip().lower(),
rcpt=recipients,
rcpt_count=len(recipients),
client_address=xforward.get('ADDR', ''),
xforward=xforward,
server=peer[0],
received=timestamp(),
rcpt_refused=refused or {})
if self._plugins:
for plugin in self._plugins:
logger.debug("run plugin[%s]" % plugin)
plugin(metadata=d, data=data)
message = self.fs.put(compress(data),
filename=key,
# content_type='message/rfc822',
content_type='text/plain',
)
d['message'] = message
if PYMONGO2:
self.col.insert(d)
else:
self.col.insert_one(d)
return key
def process_message(self, peer, mailfrom, rcpttos, data, xforward):
try:
key = self._record_mongodb(peer, mailfrom, rcpttos, data, xforward)
msg = "250 Ok: queued as %s" % key
logger.info(msg)
data = None
key = None
xforward = None
return msg
except Exception as err:
logger.exception(str(err))
return "400 Server Error"
class RecordPyMongoDBServerProxy(RecordPyMongoDBServer):
def process_message(self, peer, mailfrom, rcpttos, data, xforward):
try:
refused = self._deliver(mailfrom, rcpttos, data, xforward)
key = self._record_mongodb(peer, mailfrom, rcpttos, data, xforward, refused)
msg = "250 Ok: queued as %s" % key
logger.info(msg)
data = None
key = None
xforward = None
return msg
except Exception as err:
logger.exception(str(err))
return "400 Server Error"
def _deliver(self, mailfrom, rcpttos, data, xforward):
refused = {}
try:
s = smtplib.SMTP(host=self.remoteaddr[0], port=self.remoteaddr[1])
if self.debug:
s.set_debuglevel(1)
s.does_esmtp = 1
try:
(code, msg) = s.ehlo()
if code != 250:
s.rset()
raise smtplib.SMTPHeloError(code, msg)
(code, msg) = s.docmd('XFORWARD', 'ADDR=%(ADDR)s NAME=%(NAME)s HELO=%(HELO)s' % xforward)
if code != 250:
s.rset()
raise smtplib.SMTPResponseException(code, msg)
(code, msg) = s.mail(mailfrom) # , ["size=%s" % len(data)])
if code != 250:
s.rset()
raise smtplib.SMTPSenderRefused(code, msg, mailfrom)
for rcpt in rcpttos:
(code, msg) = s.rcpt(rcpt)
if code not in [250, 251]:
refused[rcpt] = (code, msg)
if len(refused) == len(rcpttos):
s.rset()
raise smtplib.SMTPRecipientsRefused(refused)
(code, msg) = s.data(data)
if code != 250:
s.rset()
raise smtplib.SMTPDataError(code, msg)
finally:
s.quit()
except smtplib.SMTPRecipientsRefused as e:
logger.debug('got SMTPRecipientsRefused')
refused = e.recipients
except (socket.error, smtplib.SMTPException) as e:
logger.debug('got %s', 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
"""
('X-Envelope-From', '<[email protected]>')
('X-Envelope-To', '<[email protected]>')
('X-Envelope-To-Blocked', '')
('X-Quarantine-ID', '<QX1Er+4cACI0>')
('X-Spam-Flag', 'NO')
('X-Spam-Score', '-100.126')
('X-Spam-Level', '')
('X-Spam-Status', 'No, score=-100.126 tag=-999 tag2=8 kill=8 tests=[BAYES_00=-1.9,\n\tDKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\n\tFM_FORGED_GMAIL=0.622, GMD_PDF_STOX_M5=1, GMD_PRODUCER_GPL=0.25,\n\tHTML_MESSAGE=0.001, SPF_PASS=-0.001, TVD_SPACE_RATIO=0.001,\n\tURIBL_BLOCKED=0.001, USER_IN_WHITELIST=-100] autolearn=no')
('Received', 'from mx1.radical-spam.fr ([127.0.0.1])\n\tby localhost (mx1.radical-spam.fr [127.0.0.1]) (amavisd-new, port 10039)\n\twith ESMTP id QX1Er+4cACI0 for <[email protected]>;\n\tMon, 6 Apr 2015 12:02:19 +0200 (CEST)')
('Received', 'from mail-ig0-f171.google.com (mail-ig0-f171.google.com [209.85.213.171])\n\tby mx1.radical-spam.fr (Postfix) with ESMTP id 4CA2F4620C8\n\tfor <[email protected]>; Mon, 6 Apr 2015 12:02:19 +0200 (CEST)')
('Received', 'by igbqf9 with SMTP id qf9so16118895igb.1\n for <[email protected]>; Mon, 06 Apr 2015 03:02:34 -0700 (PDT)')
('DKIM-Signature', 'v=1; a=rsa-sha256; c=relaxed/relaxed;\n d=radicalspam.org; s=google;\n h=mime-version:from:date:message-id:subject:to:content-type;\n bh=iHjvyFOWXJ02+bFfv5m1O3eujdZg5uNjMWhbQ/J8GHU=;\n b=DX3j5LO4VIHSoVi9ZUt6rB8MlglJJc0yTbR+NEpCG3BHkwlGtNF4bnM6hzo65kF00s\n VW00r2UFChO62xfp2DjTQs0VPL3JEg04nOdBiCfyWBZiaqtA6z5FA0FjYOtHdKn7rpe2\n P1NbNIBspmDQxqph8PMU3UG89SBcudrTHC8QI=')
('X-Received', 'by 10.50.30.202 with SMTP id u10mr21997931igh.28.1428314554187;\n Mon, 06 Apr 2015 03:02:34 -0700 (PDT)')
('Received', 'by 10.36.9.137 with HTTP; Mon, 6 Apr 2015 03:02:03 -0700 (PDT)')
('X-Originating-IP', '[88.175.183.38]')
('From', '=?UTF-8?Q?St=C3=A9phane_RAULT?= <[email protected]>')
('Date', 'Mon, 6 Apr 2015 12:02:03 +0200')
('Message-ID', '<CAL1AQgSvrDtem3v8hK4dVWKYprcmo9kxyOVjyv9=txe7yRD_Og@mail.gmail.com>')
('Subject', 'test2')
('To', 'contact <[email protected]>')
('Content-Type', 'multipart/mixed; boundary=047d7b874b2ca17a8c05130b657a')
docker exec -it mms1 python
>>> import os, zlib, base64
>>> from pprint import pprint as pp
>>> from email.parser import Parser, HeaderParser
>>> from pymongo import MongoClient
>>> from gridfs import GridFS
>>> client = MongoClient(os.environ.get('MMS_MONGODB_URI'))
>>> db = client['message']
#>>> db.drop_collection('message')
#>>> db.drop_collection('fs.files')
#>>> db.drop_collection('fs.chunks')
>>> col = db['message']
>>> doc = col.find_one()
>>> fs = GridFS(db)
>>> fs.list()
['77dd50277b8b6260c9bda483ab12fb2c25a1c94337978310b7e8546cdf63ebf2']
>>> msg_base64 = fs.get(doc['message']).read()
>>> msg_string = zlib.decompress(base64.b64decode(msg_base64))
>>> msg = Parser().parsestr(msg_string)
>>> for header in msg._headers: print header
"""
class MongoMailReader(object):
def __init__(self,
db=None,
colname=None,
**kwargs):
self.db = db
self.colname = colname
self.col = self.db[self.colname]
from gridfs import GridFS
self.fs = GridFS(self.db)
def count(self):
return self.col.count()
def field_keys(self):
count = self.count()
if count == 0:
return []
doc = self.col.find_one()
return doc.keys()
def file_list(self):
return self.fs.list()