forked from sdgathman/pysrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testSRS.py
305 lines (279 loc) · 9.37 KB
/
testSRS.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
#
# Original AUTHOR: Shevek
# CPAN ID: SHEVEK
# http://www.anarres.org/projects/
#
# Translated to Python by [email protected]
# http://bmsi.com/python/milter.html
#
# Copyright (c) 2017,2020 Stuart Gathman All rights reserved.
# Portions Copyright (c) 2004 Shevek. All rights reserved.
# Portions Copyright (c) 2004,2006 Business Management Systems. All rights
# reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Python itself.
import unittest
import Milter
from Milter.test import TestBase
from SRS.Guarded import Guarded
from SRS.DB import DB
from SRS.Reversible import Reversible
from SRS.Daemon import Daemon
import srsmilter
import SRS,SES
import threading
import socket
try:
from io import BytesIO
except:
from StringIO import StringIO as BytesIO
class TestMilter(TestBase,srsmilter.srsMilter):
def __init__(self):
TestBase.__init__(self)
srsmilter.config = srsmilter.Config(['pysrs.cfg'])
srsmilter.srsMilter.__init__(self)
self.setsymval('j','test.milter.org')
class SocketMap(object):
def __init__(self,sockname):
self.sockname = sockname
self.sock = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
self.sock.connect(self.sockname)
self.rfile = self.sock.makefile('rb')
def _readlen(self,maxlen=8):
ch = self.rfile.read(1)
n = b''
while ch != b':':
if not ch:
raise EOFError
if not ch in b'0123456789':
print('n =',n,'ch =',ch)
raise ValueError
if len(n) >= maxlen:
raise OverflowError
n += ch
ch = self.rfile.read(1)
return int(n)
def sendmap(self,mapname,*args):
# Sample req: 'make_srs unilit.us.\x9bstuart<@gathman.org.>'
s = mapname.encode() + b' '+b'\x9b'.join(s.encode() for s in args)
self.sock.send(b'%d:%s,' % (len(s),s))
n = self._readlen()
res = self.rfile.read(n)
ch = self.rfile.read(1)
if ch == b',':
return res.decode().split(' ',1)
if ch == b'':
raise EOFError
raise ValueError
def close(self):
self.rfile.close()
self.sock.close()
class SRSMilterTestCase(unittest.TestCase):
msg = b'''From: [email protected]
Subject: test
test
'''
## Test rejecting bounce spam
def testReject(self):
milter = TestMilter()
milter.conf.srs_domain = set(['example.com'])
milter.conf.srs_reject_spoofed = False
fp = BytesIO(self.msg)
rc = milter.connect('testReject',ip='192.0.3.1')
self.assertEqual(rc,Milter.CONTINUE)
rc = milter.feedFile(fp,sender='',rcpt='[email protected]')
self.assertEqual(rc,Milter.CONTINUE)
milter.conf.srs_reject_spoofed = True
fp.seek(0)
rc = milter.feedFile(fp,sender='',rcpt='[email protected]')
self.assertEqual(rc,Milter.REJECT)
milter.close()
## Test SRS coding of MAIL FROM
def testSign(self):
milter = TestMilter()
milter.conf.signdomain = set(['example.com'])
milter.conf.miltersrs = True
fp = BytesIO(self.msg)
rc = milter.connect('testSign',ip='192.0.3.1')
self.assertEqual(rc,Milter.CONTINUE)
fp.seek(0)
rc = milter.feedFile(fp,sender='[email protected]',rcpt='[email protected]')
self.assertEqual(rc,Milter.CONTINUE)
s = milter.conf.srs.reverse(milter._sender[1:-1])
self.assertEqual(s,'[email protected]')
# check that it doesn't happen when disabled
milter.conf.miltersrs = False
fp.seek(0)
rc = milter.feedFile(fp,sender='[email protected]',rcpt='[email protected]')
self.assertEqual(rc,Milter.CONTINUE)
self.assertEqual(milter._sender,'<[email protected]>')
milter.close()
class SRSTestCase(unittest.TestCase):
def setUp(self):
# make sure user modified tag works
SRS.SRS0TAG = 'ALT0'
SRS.SRS1TAG = 'ALT1'
def warn(self,*msg):
self.case_smashed = True
# There and back again
def testGuarded(self):
srs = Guarded()
self.assertRaises(AssertionError,srs.forward,
'[email protected]','mydomain.com')
srs.set_secret('shhhh!')
srs.separator = '+'
sender = '"Blah blah"@orig.com'
srsaddr = srs.forward(sender,sender)
self.assertEqual(srsaddr,sender)
srsaddr = srs.forward(sender,'second.com')
self.assertTrue(srsaddr.startswith('"'+SRS.SRS0TAG),srsaddr)
srsaddr1 = srs.forward(srsaddr,'third.com')
#print srsaddr1
self.assertTrue(srsaddr1.startswith('"'+SRS.SRS1TAG))
srsaddr2 = srs.forward(srsaddr1,'fourth.com')
#print srsaddr2
self.assertTrue(srsaddr2.startswith('"'+SRS.SRS1TAG))
addr = srs.reverse(srsaddr2)
self.assertEqual(srsaddr,addr)
addr = srs.reverse(srsaddr1)
self.assertEqual(srsaddr,addr)
addr = srs.reverse(srsaddr)
self.assertEqual(sender,addr)
def testSign(self):
srs = Guarded()
srs.set_secret('shhhh!')
srs.separator = '+'
sender = '[email protected]'
sig = srs.sign(sender)
addr = srs.reverse(sig)
self.assertEqual(sender,addr)
sender = '[email protected]'
sig = srs.sign(sender)
addr = srs.reverse(sig)
self.assertEqual(sender,addr)
addr = srs.reverse(sig.lower())
self.assertEqual(sender.lower(),addr)
def testCaseSmash(self):
srs = SRS.new(secret='shhhhh!',separator='+')
# FIXME: whether case smashing occurs depends on what day it is.
sender = '[email protected]'
srsaddr = srs.forward(sender,'second.com')
self.assertTrue(srsaddr.startswith(SRS.SRS0TAG))
self.case_smashed = False
srs.warn = self.warn
addr = srs.reverse(srsaddr.lower())
self.assertTrue(self.case_smashed) # check that warn was called
self.assertEqual(sender,addr)
def testReversible(self):
srs = Reversible()
self.assertRaises(AssertionError,srs.forward,
'[email protected]','mydomain.com')
srs.set_secret('shhhh!')
srs.separator = '+'
sender = '[email protected]'
srsaddr = srs.forward(sender,sender)
self.assertEqual(srsaddr,sender)
srsaddr = srs.forward(sender,'second.com')
#print srsaddr
self.assertTrue(srsaddr.startswith(SRS.SRS0TAG))
srsaddr1 = srs.forward(srsaddr,'third.com')
#print srsaddr1
self.assertTrue(srsaddr1.startswith(SRS.SRS0TAG))
srsaddr2 = srs.forward(srsaddr1,'fourth.com')
#print srsaddr2
self.assertTrue(srsaddr2.startswith(SRS.SRS0TAG))
addr = srs.reverse(srsaddr2)
self.assertEqual(srsaddr1,addr)
addr = srs.reverse(srsaddr1)
self.assertEqual(srsaddr,addr)
addr = srs.reverse(srsaddr)
self.assertEqual(sender,addr)
def testDB(self,database='/tmp/srstest'):
srs = DB(database=database)
self.assertRaises(AssertionError,srs.forward,
'[email protected]','mydomain.com')
srs.set_secret('shhhh!')
sender = '[email protected]'
srsaddr = srs.forward(sender,sender)
self.assertEqual(srsaddr,sender)
srsaddr = srs.forward(sender,'second.com')
#print(srsaddr)
self.assertTrue(srsaddr.startswith(SRS.SRS0TAG))
srsaddr1 = srs.forward(srsaddr,'third.com')
#print(srsaddr1)
self.assertTrue(srsaddr1.startswith(SRS.SRS0TAG))
srsaddr2 = srs.forward(srsaddr1,'fourth.com')
#print(srsaddr2)
self.assertTrue(srsaddr2.startswith(SRS.SRS0TAG))
addr = srs.reverse(srsaddr2)
self.assertEqual(srsaddr1,addr)
addr = srs.reverse(srsaddr1)
self.assertEqual(srsaddr,addr)
addr = srs.reverse(srsaddr)
self.assertEqual(sender,addr)
def run2(self): # handle two requests
self.daemon.server.handle_request()
self.daemon.server.handle_request()
self.daemon.server.server_close()
def sendcmd(self,*args):
sock = socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
sock.connect(self.sockname)
sock.send(b' '.join(args)+b'\n')
res = sock.recv(128).strip()
sock.close()
return res
def testExim(self,sockname='/tmp/exsrsd',secret="shhhh!"):
self.sockname = sockname
self.daemon = Daemon(socket=sockname,secret=secret)
server = threading.Thread(target=self.run2,name='srsd')
server.start()
sender = b'[email protected]'
srsaddr = self.sendcmd(b'FORWARD',sender,b'second.com')
addr = self.sendcmd(b'REVERSE',srsaddr)
server.join()
self.assertEqual(sender,addr)
def testSocketMap(self,sockname='/tmp/srsd',secret="shhhh!"):
import pysrs,subprocess,time
print()
with subprocess.Popen(['./pysrs.py','test/pysrs.cfg']) as p:
time.sleep(1)
try:
m = SocketMap('/tmp/srsd')
sender = 'mouse<@orig.com.>'
# Sample req: 'make_srs unilit.us.\x9bstuart<@gathman.org.>'
res,srsaddr = m.sendmap('make-srs','second.com',sender)
res,addr = m.sendmap('reverse-srs',srsaddr)
finally: m.close()
p.terminate()
self.assertEqual('OK',res)
self.assertEqual(sender,addr)
def testProgMap(self):
import envfrom2srs
import srs2envtol
orig = 'mickey<@orig.com.>'
newaddr = envfrom2srs.forward(orig)
self.assertTrue(newaddr.endswith('.>'))
addr2 = srs2envtol.reverse(newaddr)
self.assertEqual(addr2,orig)
# check case smashing by braindead mailers
self.case_smashed = False
srs2envtol.srs.warn = self.warn
addr2 = srs2envtol.reverse(newaddr.lower())
self.assertEqual(addr2,orig)
self.assertTrue(self.case_smashed)
def suite():
s = unittest.makeSuite(SRSTestCase,'test')
s.addTest(makeSuite(SRSMilterTestCase,'test'))
#s.addTest(doctest.DocTestSuite(bms))
return s
if __name__ == '__main__':
from sys import argv
if len(argv) < 3:
unittest.main()
else:
m = SocketMap('/tmp/srsd')
r = m.sendmap(argv[1],*argv[2:])
print(r)
m.close()