|
|
@ -18,7 +18,6 @@
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
import smtpd
|
|
|
|
|
|
|
|
import threading
|
|
|
|
import threading
|
|
|
|
import unittest
|
|
|
|
import unittest
|
|
|
|
import re
|
|
|
|
import re
|
|
|
@ -29,134 +28,139 @@ else:
|
|
|
|
import imp
|
|
|
|
import imp
|
|
|
|
|
|
|
|
|
|
|
|
from ..dummyjail import DummyJail
|
|
|
|
from ..dummyjail import DummyJail
|
|
|
|
|
|
|
|
|
|
|
|
from ..utils import CONFIG_DIR, asyncserver, Utils, uni_decode
|
|
|
|
from ..utils import CONFIG_DIR, asyncserver, Utils, uni_decode
|
|
|
|
|
|
|
|
|
|
|
|
class TestSMTPServer(smtpd.SMTPServer):
|
|
|
|
try:
|
|
|
|
|
|
|
|
import smtpd
|
|
|
|
def __init__(self, *args):
|
|
|
|
|
|
|
|
smtpd.SMTPServer.__init__(self, *args)
|
|
|
|
class TestSMTPServer(smtpd.SMTPServer):
|
|
|
|
self.ready = False
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
|
|
|
|
smtpd.SMTPServer.__init__(self, *args)
|
|
|
|
self.peer = peer
|
|
|
|
self.ready = False
|
|
|
|
self.mailfrom = mailfrom
|
|
|
|
|
|
|
|
self.rcpttos = rcpttos
|
|
|
|
def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
|
|
|
|
self.org_data = data
|
|
|
|
self.peer = peer
|
|
|
|
# replace new line (with tab or space) for possible mime translations (word wrap),
|
|
|
|
self.mailfrom = mailfrom
|
|
|
|
self.data = re.sub(r"\n[\t ]", " ", uni_decode(data))
|
|
|
|
self.rcpttos = rcpttos
|
|
|
|
self.ready = True
|
|
|
|
self.org_data = data
|
|
|
|
|
|
|
|
# replace new line (with tab or space) for possible mime translations (word wrap),
|
|
|
|
|
|
|
|
self.data = re.sub(r"\n[\t ]", " ", uni_decode(data))
|
|
|
|
class SMTPActionTest(unittest.TestCase):
|
|
|
|
self.ready = True
|
|
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
|
|
|
"""Call before every test case."""
|
|
|
|
class SMTPActionTest(unittest.TestCase):
|
|
|
|
unittest.F2B.SkipIfCfgMissing(action='smtp.py')
|
|
|
|
|
|
|
|
super(SMTPActionTest, self).setUp()
|
|
|
|
def setUp(self):
|
|
|
|
self.jail = DummyJail()
|
|
|
|
"""Call before every test case."""
|
|
|
|
pythonModule = os.path.join(CONFIG_DIR, "action.d", "smtp.py")
|
|
|
|
unittest.F2B.SkipIfCfgMissing(action='smtp.py')
|
|
|
|
pythonModuleName = os.path.basename(pythonModule.rstrip(".py"))
|
|
|
|
super(SMTPActionTest, self).setUp()
|
|
|
|
if sys.version_info >= (3, 3):
|
|
|
|
self.jail = DummyJail()
|
|
|
|
customActionModule = importlib.machinery.SourceFileLoader(
|
|
|
|
pythonModule = os.path.join(CONFIG_DIR, "action.d", "smtp.py")
|
|
|
|
pythonModuleName, pythonModule).load_module()
|
|
|
|
pythonModuleName = os.path.basename(pythonModule.rstrip(".py"))
|
|
|
|
else:
|
|
|
|
if sys.version_info >= (3, 3):
|
|
|
|
customActionModule = imp.load_source(
|
|
|
|
customActionModule = importlib.machinery.SourceFileLoader(
|
|
|
|
pythonModuleName, pythonModule)
|
|
|
|
pythonModuleName, pythonModule).load_module()
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.smtpd = TestSMTPServer(("localhost", 0), None)
|
|
|
|
customActionModule = imp.load_source(
|
|
|
|
port = self.smtpd.socket.getsockname()[1]
|
|
|
|
pythonModuleName, pythonModule)
|
|
|
|
|
|
|
|
|
|
|
|
self.action = customActionModule.Action(
|
|
|
|
self.smtpd = TestSMTPServer(("localhost", 0), None)
|
|
|
|
self.jail, "test", host="localhost:%i" % port)
|
|
|
|
port = self.smtpd.socket.getsockname()[1]
|
|
|
|
|
|
|
|
|
|
|
|
## because of bug in loop (see loop in asyncserver.py) use it's loop instead of asyncore.loop:
|
|
|
|
self.action = customActionModule.Action(
|
|
|
|
self._active = True
|
|
|
|
self.jail, "test", host="localhost:%i" % port)
|
|
|
|
self._loop_thread = threading.Thread(
|
|
|
|
|
|
|
|
target=asyncserver.loop, kwargs={'active': lambda: self._active})
|
|
|
|
## because of bug in loop (see loop in asyncserver.py) use it's loop instead of asyncore.loop:
|
|
|
|
self._loop_thread.daemon = True
|
|
|
|
self._active = True
|
|
|
|
self._loop_thread.start()
|
|
|
|
self._loop_thread = threading.Thread(
|
|
|
|
|
|
|
|
target=asyncserver.loop, kwargs={'active': lambda: self._active})
|
|
|
|
def tearDown(self):
|
|
|
|
self._loop_thread.daemon = True
|
|
|
|
"""Call after every test case."""
|
|
|
|
self._loop_thread.start()
|
|
|
|
self.smtpd.close()
|
|
|
|
|
|
|
|
self._active = False
|
|
|
|
def tearDown(self):
|
|
|
|
self._loop_thread.join()
|
|
|
|
"""Call after every test case."""
|
|
|
|
super(SMTPActionTest, self).tearDown()
|
|
|
|
self.smtpd.close()
|
|
|
|
|
|
|
|
self._active = False
|
|
|
|
def _exec_and_wait(self, doaction, timeout=3, short=False):
|
|
|
|
self._loop_thread.join()
|
|
|
|
if short: timeout /= 25
|
|
|
|
super(SMTPActionTest, self).tearDown()
|
|
|
|
self.smtpd.ready = False
|
|
|
|
|
|
|
|
doaction()
|
|
|
|
def _exec_and_wait(self, doaction, timeout=3, short=False):
|
|
|
|
Utils.wait_for(lambda: self.smtpd.ready, timeout)
|
|
|
|
if short: timeout /= 25
|
|
|
|
|
|
|
|
self.smtpd.ready = False
|
|
|
|
def testStart(self):
|
|
|
|
doaction()
|
|
|
|
self._exec_and_wait(self.action.start)
|
|
|
|
Utils.wait_for(lambda: self.smtpd.ready, timeout)
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
|
|
|
|
|
|
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
|
|
|
def testStart(self):
|
|
|
|
self.assertTrue(
|
|
|
|
self._exec_and_wait(self.action.start)
|
|
|
|
"Subject: [Fail2Ban] %s: started" % self.jail.name
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
|
|
|
in self.smtpd.data)
|
|
|
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
def testStop(self):
|
|
|
|
"Subject: [Fail2Ban] %s: started" % self.jail.name
|
|
|
|
self._exec_and_wait(self.action.stop)
|
|
|
|
in self.smtpd.data)
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
|
|
|
|
|
|
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
|
|
|
def testStop(self):
|
|
|
|
self.assertTrue(
|
|
|
|
self._exec_and_wait(self.action.stop)
|
|
|
|
"Subject: [Fail2Ban] %s: stopped" %
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
|
|
|
self.jail.name in self.smtpd.data)
|
|
|
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
def _testBan(self, restored=False):
|
|
|
|
"Subject: [Fail2Ban] %s: stopped" %
|
|
|
|
aInfo = {
|
|
|
|
self.jail.name in self.smtpd.data)
|
|
|
|
'ip': "127.0.0.2",
|
|
|
|
|
|
|
|
'failures': 3,
|
|
|
|
def _testBan(self, restored=False):
|
|
|
|
'matches': "Test fail 1\n",
|
|
|
|
aInfo = {
|
|
|
|
'ipjailmatches': "Test fail 1\nTest Fail2\n",
|
|
|
|
'ip': "127.0.0.2",
|
|
|
|
'ipmatches': "Test fail 1\nTest Fail2\nTest Fail3\n",
|
|
|
|
'failures': 3,
|
|
|
|
}
|
|
|
|
'matches': "Test fail 1\n",
|
|
|
|
if restored:
|
|
|
|
'ipjailmatches': "Test fail 1\nTest Fail2\n",
|
|
|
|
aInfo['restored'] = 1
|
|
|
|
'ipmatches': "Test fail 1\nTest Fail2\nTest Fail3\n",
|
|
|
|
|
|
|
|
}
|
|
|
|
self._exec_and_wait(lambda: self.action.ban(aInfo), short=restored)
|
|
|
|
if restored:
|
|
|
|
if restored: # no mail, should raises attribute error:
|
|
|
|
aInfo['restored'] = 1
|
|
|
|
self.assertRaises(AttributeError, lambda: self.smtpd.mailfrom)
|
|
|
|
|
|
|
|
return
|
|
|
|
self._exec_and_wait(lambda: self.action.ban(aInfo), short=restored)
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
|
|
|
if restored: # no mail, should raises attribute error:
|
|
|
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
|
|
|
self.assertRaises(AttributeError, lambda: self.smtpd.mailfrom)
|
|
|
|
subject = "Subject: [Fail2Ban] %s: banned %s" % (
|
|
|
|
return
|
|
|
|
self.jail.name, aInfo['ip'])
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
|
|
|
self.assertIn(subject, self.smtpd.data)
|
|
|
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
|
|
|
self.assertIn(
|
|
|
|
subject = "Subject: [Fail2Ban] %s: banned %s" % (
|
|
|
|
"%i attempts" % aInfo['failures'], self.smtpd.data)
|
|
|
|
self.jail.name, aInfo['ip'])
|
|
|
|
|
|
|
|
self.assertIn(subject, self.smtpd.data)
|
|
|
|
self.action.matches = "matches"
|
|
|
|
self.assertIn(
|
|
|
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
|
|
|
"%i attempts" % aInfo['failures'], self.smtpd.data)
|
|
|
|
self.assertIn(aInfo['matches'], self.smtpd.data)
|
|
|
|
|
|
|
|
|
|
|
|
self.action.matches = "matches"
|
|
|
|
self.action.matches = "ipjailmatches"
|
|
|
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
|
|
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
|
|
|
self.assertIn(aInfo['matches'], self.smtpd.data)
|
|
|
|
self.assertIn(aInfo['ipjailmatches'], self.smtpd.data)
|
|
|
|
|
|
|
|
|
|
|
|
self.action.matches = "ipjailmatches"
|
|
|
|
self.action.matches = "ipmatches"
|
|
|
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
|
|
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
|
|
|
self.assertIn(aInfo['ipjailmatches'], self.smtpd.data)
|
|
|
|
self.assertIn(aInfo['ipmatches'], self.smtpd.data)
|
|
|
|
|
|
|
|
|
|
|
|
self.action.matches = "ipmatches"
|
|
|
|
def testBan(self):
|
|
|
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
|
|
|
self._testBan()
|
|
|
|
self.assertIn(aInfo['ipmatches'], self.smtpd.data)
|
|
|
|
|
|
|
|
|
|
|
|
def testNOPByRestored(self):
|
|
|
|
def testBan(self):
|
|
|
|
self._testBan(restored=True)
|
|
|
|
self._testBan()
|
|
|
|
|
|
|
|
|
|
|
|
def testOptions(self):
|
|
|
|
def testNOPByRestored(self):
|
|
|
|
self._exec_and_wait(self.action.start)
|
|
|
|
self._testBan(restored=True)
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
|
|
|
|
|
|
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
|
|
|
def testOptions(self):
|
|
|
|
|
|
|
|
self._exec_and_wait(self.action.start)
|
|
|
|
self.action.fromname = "Test"
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
|
|
|
self.action.fromaddr = "test@example.com"
|
|
|
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
|
|
|
self.action.toaddr = "test@example.com, test2@example.com"
|
|
|
|
|
|
|
|
self._exec_and_wait(self.action.start)
|
|
|
|
self.action.fromname = "Test"
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "test@example.com")
|
|
|
|
self.action.fromaddr = "test@example.com"
|
|
|
|
self.assertTrue("From: %s <%s>" %
|
|
|
|
self.action.toaddr = "test@example.com, test2@example.com"
|
|
|
|
(self.action.fromname, self.action.fromaddr) in self.smtpd.data)
|
|
|
|
self._exec_and_wait(self.action.start)
|
|
|
|
self.assertEqual(set(self.smtpd.rcpttos), set(["test@example.com", "test2@example.com"]))
|
|
|
|
self.assertEqual(self.smtpd.mailfrom, "test@example.com")
|
|
|
|
|
|
|
|
self.assertTrue("From: %s <%s>" %
|
|
|
|
|
|
|
|
(self.action.fromname, self.action.fromaddr) in self.smtpd.data)
|
|
|
|
|
|
|
|
self.assertEqual(set(self.smtpd.rcpttos), set(["test@example.com", "test2@example.com"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except ImportError as e:
|
|
|
|
|
|
|
|
print("I: Skipping smtp tests: %s" % e)
|
|
|
|