|
@@ -9,10 +9,15 @@ import sys
|
9
|
9
|
import datetime
|
10
|
10
|
from email.header import decode_header
|
11
|
11
|
|
12
|
|
-from core.models import TLSNotification
|
|
12
|
+from core.models import TLSNotification, MandatoryTLSDomains
|
13
|
13
|
|
14
|
14
|
|
15
|
|
-def send_mail(message):
|
|
15
|
+def send_mail(message, deleted):
|
|
16
|
+ """
|
|
17
|
+ Send mail notification to sender.
|
|
18
|
+ If the domain of a recipient is listed in MandatoryTLSDomains,
|
|
19
|
+ the mail was deleted and 'deleted' is set to True.
|
|
20
|
+ """
|
16
|
21
|
import smtplib
|
17
|
22
|
from email.mime.multipart import MIMEMultipart
|
18
|
23
|
from email.mime.text import MIMEText
|
|
@@ -34,7 +39,8 @@ def send_mail(message):
|
34
|
39
|
'subject': message['subject'],
|
35
|
40
|
'queue_id': message['queue_id'],
|
36
|
41
|
'postfix_sysadmin_mail_address': settings.POSTTLS_NOTIFICATION_SYSADMIN_MAIL_ADDRESS,
|
37
|
|
- 'postfix_tls_host': settings.POSTTLS_TLS_HOST})
|
|
42
|
+ 'postfix_tls_host': settings.POSTTLS_TLS_HOST,
|
|
43
|
+ 'deleted': deleted})
|
38
|
44
|
|
39
|
45
|
text_content = strip_tags(html_content) # this strips the html tags
|
40
|
46
|
|
|
@@ -174,28 +180,46 @@ class Command(BaseCommand):
|
174
|
180
|
# set the subject
|
175
|
181
|
message['subject'] = str(subject)
|
176
|
182
|
|
177
|
|
- #######################################################################
|
178
|
|
- # Send notification and handle database entry
|
179
|
|
-
|
180
|
|
- # Check the database if an earlier notification was already sent
|
181
|
|
- try:
|
182
|
|
- notification = TLSNotification.objects.get(queue_id=message["queue_id"])
|
183
|
|
- except:
|
184
|
|
- notification = ""
|
185
|
|
-
|
186
|
|
- if not notification:
|
187
|
|
- # If this is the first notification, send it and make a database entry
|
188
|
|
- n = TLSNotification(queue_id=message["queue_id"], notification=datetime.datetime.today())
|
189
|
|
- n.save()
|
190
|
|
- send_mail(message)
|
191
|
|
- else:
|
192
|
|
- # If the last notification is more than 30 minutes ago,
|
193
|
|
- # send another notification
|
194
|
|
- if notification.notification.replace(tzinfo=None) \
|
195
|
|
- < datetime.datetime.today() - datetime.timedelta(minutes=30):
|
196
|
|
- notification.delete()
|
|
183
|
+ ###################################################################
|
|
184
|
+ # If the domain is listed in MandatoryTLSDomains, delete the mail and inform the sender
|
|
185
|
+ mandatory_tls = False
|
|
186
|
+ mandatory_tls_domains = MandatoryTLSDomains.objects.all()
|
|
187
|
+ for domain in mandatory_tls_domains:
|
|
188
|
+ if domain.domain in message["recipients"]:
|
|
189
|
+ mandatory_tls = True
|
|
190
|
+
|
|
191
|
+ if mandatory_tls:
|
|
192
|
+ # delete mail
|
|
193
|
+ p = subprocess.Popen(['sudo', 'postsuper', '-d', message['queue_id']],
|
|
194
|
+ stdin=subprocess.PIPE,
|
|
195
|
+ stdout=subprocess.PIPE,
|
|
196
|
+ stderr=subprocess.STDOUT)
|
|
197
|
+ output = str(p.stdout.read(), "utf-8").splitlines()
|
|
198
|
+
|
|
199
|
+ # send notification to sender
|
|
200
|
+ send_mail(message, deleted=True)
|
|
201
|
+
|
|
202
|
+ else: # if not mandatory_tls
|
|
203
|
+ #######################################################################
|
|
204
|
+ # Send notification and handle database entry
|
|
205
|
+
|
|
206
|
+ # Check the database if an earlier notification was already sent
|
|
207
|
+ try:
|
|
208
|
+ notification = TLSNotification.objects.get(queue_id=message["queue_id"])
|
|
209
|
+ except:
|
|
210
|
+ notification = ""
|
|
211
|
+
|
|
212
|
+ if not notification:
|
|
213
|
+ # If this is the first notification, send it and make a database entry
|
197
|
214
|
n = TLSNotification(queue_id=message["queue_id"], notification=datetime.datetime.today())
|
198
|
215
|
n.save()
|
199
|
|
- send_mail(message)
|
200
|
|
-
|
201
|
|
- self.stdout.write('Successfully processed Postfix Queue!')
|
|
216
|
+ send_mail(message, deleted=False)
|
|
217
|
+ else:
|
|
218
|
+ # If the last notification is more than 30 minutes ago,
|
|
219
|
+ # send another notification
|
|
220
|
+ if notification.notification.replace(tzinfo=None) \
|
|
221
|
+ < datetime.datetime.today() - datetime.timedelta(minutes=30):
|
|
222
|
+ notification.delete()
|
|
223
|
+ n = TLSNotification(queue_id=message["queue_id"], notification=datetime.datetime.today())
|
|
224
|
+ n.save()
|
|
225
|
+ send_mail(message, deleted=False)
|