-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmilter-sssp
More file actions
executable file
·136 lines (120 loc) · 4.57 KB
/
milter-sssp
File metadata and controls
executable file
·136 lines (120 loc) · 4.57 KB
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
#!/usr/bin/python
#
# Milter to scan mail via SSSP for viruses
#
# Copyright 2018,2019 Andreas Thienemann <andreas@bawue.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import argparse
import email
import os
import socket
import sys
from multiprocessing import Process
import Milter
from Milter.utils import parse_addr
import syslog
import sssp
import textwrap
import traceback
milter_name = 'milter-sssp'
milter_version = '0.1.0'
global args
class ssspMilter(Milter.Base):
def __init__(self): # A new instance with each new connection.
self.queueid = None
self.id = Milter.uniqueID() # Integer incremented with each call.
self.rcpts = []
def log(self, msg):
if self.queue_id:
syslog.syslog('{}: {}'.format(self.queue_id, msg))
else:
syslog.syslog('{}: {}'.format(self.id, msg))
# multiple messages can be received on a single connection
# envfrom (MAIL FROM in the SMTP protocol) seems to mark the start
# of each message.
def envfrom(self, envfrom, *str):
self.mail = ''
if envfrom.startswith('<') and envfrom.endswith('>'):
self.env_from = envfrom[1:-1]
else:
self.env_from = envfrom
return Milter.CONTINUE
def envrcpt(self, to, *str):
if to.startswith('<') and to.endswith('>'):
self.rcpts.append(to[1:-1])
else:
self.rcpts.append(to)
return Milter.CONTINUE
def header(self, name, val):
self.mail += '{}: {}\n'.format(name, val)
return Milter.CONTINUE
def eoh(self):
self.mail += '\n'
return Milter.CONTINUE
def body(self, chunk):
self.mail += chunk
return Milter.CONTINUE
def eom(self):
self.queue_id = self.getsymval('i')
try:
scanner = sssp.sssp(args.sssp_socket)
if not scanner.selftest():
self.log('SAVDI selftest failed. Not scanning, accepting mail.')
return Milter.ACCEPT
result, msg = scanner.check(self.mail)
except:
self.log('Unknown SAVDI Error. {}'.format(traceback.format_exc()))
return Milter.ACCEPT
engine = scanner.query_engine()
server = scanner.query_server()
self.addheader('X-Virus-Scanner', textwrap.fill('{}, Engine: {}, SAV: {} ({}) on {} using {} {}'.format(server['version'],
engine['engineversion'], engine['savversion'], engine['virusdatachecksum'],
socket.gethostname(), milter_name, milter_version), 61), -1)
if result:
self.addheader('X-Virus-Scan', 'Found to be clean.', -1)
self.log('{}, accepting.'.format(msg))
else:
self.addheader('X-Virus-Scan', 'Found to be infected. ({})'.format(msg.split()[-1]), -1)
self.log('Mail reported infected with {} by SAVDI.'.format(msg.split()[-1]))
if args.quarantine:
self.log('{}, marked as hold.'.format(msg))
self.quarantine(msg)
return Milter.ACCEPT
if args.reject:
self.log('{}, rejecting.'.format(msg))
self.setreply('550', '5.7.1', 'Message rejected due to malware/virus scanning.')
return Milter.REJECT
return Milter.ACCEPT
def main():
global args
syslog.openlog(ident='milter-sssp', logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL)
syslog.syslog('Milter starting using socket {}'.format(args.socket))
job = Process()
job.start()
Milter.factory = ssspMilter
timeout = 60
Milter.runmilter('ssspMilter', args.socket, timeout)
job.join()
syslog.syslog('Milter stopping')
syslog.closelog()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Milter to scan mail for viruses via SSSP.')
parser.add_argument('-q', '--quarantine', action='store_true', default=False, help='Quarantine suspect mail rather than rejecting it.')
parser.add_argument('-r', '--reject', action='store_true', default=False, help='Reject suspect mail rather than accepting and marking it.')
parser.add_argument('socket', help='Milter socket for communicating to postfix')
parser.add_argument('sssp_socket', help='Socket for communicating to sssp interface.')
args = parser.parse_args()
main()