2019-09-14 20:01:53 +02:00
#!/usr/bin/python3
2019-01-29 01:11:12 +02:00
import smtplib
import os
import mysql . connector
2019-09-13 20:14:30 +02:00
from email . mime . multipart import MIMEMultipart
from email . mime . text import MIMEText
from email . utils import COMMASPACE , formatdate
2019-01-29 01:11:12 +02:00
import cgi
import jinja2
from jinja2 import Template
import json
import redis
import time
2019-02-05 11:38:28 +02:00
import html2text
2019-02-06 10:21:54 +02:00
import socket
2019-01-29 01:11:12 +02:00
while True :
try :
r = redis . StrictRedis ( host = ' redis ' , decode_responses = True , port = 6379 , db = 0 )
r . ping ( )
except Exception as ex :
2019-09-13 20:14:30 +02:00
print ( ' %s - trying again... ' % ( ex ) )
2019-01-29 01:11:12 +02:00
time . sleep ( 3 )
else :
break
time_now = int ( time . time ( ) )
def query_mysql ( query , headers = True , update = False ) :
while True :
try :
2019-09-14 20:07:18 +02:00
cnx = mysql . connector . connect ( unix_socket = ' /var/run/mysqld/mysqld.sock ' , user = ' __DBUSER__ ' , passwd = ' __DBPASS__ ' , database = ' __DBNAME__ ' , charset = " utf8 " )
2019-01-29 01:11:12 +02:00
except Exception as ex :
2019-09-13 20:14:30 +02:00
print ( ' %s - trying again... ' % ( ex ) )
2019-01-29 01:11:12 +02:00
time . sleep ( 3 )
else :
break
cur = cnx . cursor ( )
cur . execute ( query )
if not update :
result = [ ]
2019-09-13 20:14:30 +02:00
columns = tuple ( [ d [ 0 ] for d in cur . description ] )
2019-01-29 01:11:12 +02:00
for row in cur :
2019-01-31 21:53:08 +02:00
if headers :
2019-09-13 20:14:30 +02:00
result . append ( dict ( list ( zip ( columns , row ) ) ) )
2019-01-29 01:11:12 +02:00
else :
result . append ( row )
cur . close ( )
cnx . close ( )
return result
else :
cnx . commit ( )
cur . close ( )
cnx . close ( )
2019-02-06 10:21:54 +02:00
def notify_rcpt ( rcpt , msg_count , quarantine_acl ) :
meta_query = query_mysql ( ' SELECT SHA2(CONCAT(id, qid), 256) AS qhash, id, subject, score, sender, created FROM quarantine WHERE notified = 0 AND rcpt = " %s " ' % ( rcpt ) )
2019-01-29 01:11:12 +02:00
if r . get ( ' Q_HTML ' ) :
try :
template = Template ( r . get ( ' Q_HTML ' ) )
except :
2019-09-13 20:14:30 +02:00
print ( " Error: Cannot parse quarantine template, falling back to default template. " )
2019-01-31 23:18:32 +02:00
with open ( ' /templates/quarantine.tpl ' ) as file_ :
template = Template ( file_ . read ( ) )
2019-01-29 01:11:12 +02:00
else :
with open ( ' /templates/quarantine.tpl ' ) as file_ :
template = Template ( file_ . read ( ) )
2019-02-06 10:21:54 +02:00
html = template . render ( meta = meta_query , counter = msg_count , hostname = socket . gethostname ( ) , quarantine_acl = quarantine_acl )
2019-02-05 11:38:28 +02:00
text = html2text . html2text ( html )
2019-01-29 01:11:12 +02:00
count = 0
while count < 15 :
try :
2019-01-31 23:18:32 +02:00
server = smtplib . SMTP ( ' postfix ' , 590 , ' quarantine ' )
2019-01-29 01:11:12 +02:00
server . ehlo ( )
msg = MIMEMultipart ( ' alternative ' )
2019-09-14 20:48:13 +02:00
msg_from = r . get ( ' Q_SENDER ' ) or " quarantine@localhost "
# Remove non-ascii chars from field
msg [ ' From ' ] = ' ' . join ( [ i if ord ( i ) < 128 else ' ' for i in msg_from ] )
2019-01-29 01:11:12 +02:00
msg [ ' Subject ' ] = r . get ( ' Q_SUBJ ' ) or " Spam Quarantine Notification "
msg [ ' Date ' ] = formatdate ( localtime = True )
2019-01-31 22:45:58 +02:00
text_part = MIMEText ( text , ' plain ' , ' utf-8 ' )
html_part = MIMEText ( html , ' html ' , ' utf-8 ' )
2019-01-29 01:11:12 +02:00
msg . attach ( text_part )
msg . attach ( html_part )
msg [ ' To ' ] = str ( rcpt )
text = msg . as_string ( )
2019-09-14 20:01:53 +02:00
server . sendmail ( msg [ ' From ' ] , msg [ ' To ' ] , text )
2019-01-29 01:11:12 +02:00
server . quit ( )
for res in meta_query :
query_mysql ( ' UPDATE quarantine SET notified = 1 WHERE id = " %d " ' % ( res [ ' id ' ] ) , update = True )
r . hset ( ' Q_LAST_NOTIFIED ' , record [ ' rcpt ' ] , time_now )
break
except Exception as ex :
2019-05-16 19:44:14 +02:00
server . quit ( )
2019-09-13 20:14:30 +02:00
print ( ' %s ' % ( ex ) )
2019-01-29 01:11:12 +02:00
time . sleep ( 3 )
2019-02-06 10:21:54 +02:00
records = query_mysql ( ' SELECT IFNULL(user_acl.quarantine, 0) AS quarantine_acl, count(id) AS counter, rcpt FROM quarantine LEFT OUTER JOIN user_acl ON user_acl.username = rcpt WHERE notified = 0 AND rcpt in (SELECT username FROM mailbox) GROUP BY rcpt ' )
2019-01-29 01:11:12 +02:00
for record in records :
2019-02-05 01:00:22 +02:00
attrs = ' '
attrs_json = ' '
try :
last_notification = int ( r . hget ( ' Q_LAST_NOTIFIED ' , record [ ' rcpt ' ] ) )
if last_notification > time_now :
2019-09-13 20:14:30 +02:00
print ( ' Last notification is > time now, assuming never ' )
2019-02-05 01:00:22 +02:00
last_notification = 0
except Exception as ex :
2019-09-13 20:14:30 +02:00
print ( ' Could not determine last notification for %s , assuming never ' % ( record [ ' rcpt ' ] ) )
2019-02-05 01:00:22 +02:00
last_notification = 0
2019-01-29 01:11:12 +02:00
attrs_json = query_mysql ( ' SELECT attributes FROM mailbox WHERE username = " %s " ' % ( record [ ' rcpt ' ] ) )
2019-09-14 20:01:53 +02:00
attrs = json . loads ( str ( attrs_json [ 0 ] [ ' attributes ' ] . decode ( ' utf-8 ' ) ) )
2019-02-05 01:00:22 +02:00
if attrs [ ' quarantine_notification ' ] not in ( ' hourly ' , ' daily ' , ' weekly ' , ' never ' ) :
2019-09-13 20:14:30 +02:00
print ( ' Abnormal quarantine_notification value ' )
2019-02-05 01:00:22 +02:00
continue
2019-01-29 01:11:12 +02:00
if attrs [ ' quarantine_notification ' ] == ' hourly ' :
2019-01-29 13:13:26 +02:00
if last_notification == 0 or ( last_notification + 3600 ) < time_now :
2019-09-13 20:14:30 +02:00
print ( " Notifying %s about %d new items in quarantine " % ( record [ ' rcpt ' ] , record [ ' counter ' ] ) )
2019-02-06 10:21:54 +02:00
notify_rcpt ( record [ ' rcpt ' ] , record [ ' counter ' ] , record [ ' quarantine_acl ' ] )
2019-01-29 01:11:12 +02:00
elif attrs [ ' quarantine_notification ' ] == ' daily ' :
2019-01-29 13:13:26 +02:00
if last_notification == 0 or ( last_notification + 86400 ) < time_now :
2019-09-13 20:14:30 +02:00
print ( " Notifying %s about %d new items in quarantine " % ( record [ ' rcpt ' ] , record [ ' counter ' ] ) )
2019-02-06 10:21:54 +02:00
notify_rcpt ( record [ ' rcpt ' ] , record [ ' counter ' ] , record [ ' quarantine_acl ' ] )
2019-01-29 01:11:12 +02:00
elif attrs [ ' quarantine_notification ' ] == ' weekly ' :
2019-01-29 13:13:26 +02:00
if last_notification == 0 or ( last_notification + 604800 ) < time_now :
2019-09-13 20:14:30 +02:00
print ( " Notifying %s about %d new items in quarantine " % ( record [ ' rcpt ' ] , record [ ' counter ' ] ) )
2019-02-06 10:21:54 +02:00
notify_rcpt ( record [ ' rcpt ' ] , record [ ' counter ' ] , record [ ' quarantine_acl ' ] )