2014-01-03 17:04:49 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
|
|
|
|
|
|
|
# This file is part of Fail2Ban.
|
|
|
|
#
|
|
|
|
# Fail2Ban 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 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Fail2Ban 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 Fail2Ban; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2013-12-31 18:54:34 +00:00
|
|
|
|
|
|
|
import socket
|
|
|
|
import smtplib
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
from email.utils import formatdate, formataddr
|
|
|
|
|
2014-01-01 12:27:49 +00:00
|
|
|
from fail2ban.server.actions import ActionBase, CallingMap
|
2013-12-31 18:54:34 +00:00
|
|
|
|
|
|
|
messages = {}
|
|
|
|
messages['start'] = \
|
|
|
|
"""Hi,
|
|
|
|
|
|
|
|
The jail %(jailname)s has been started successfully.
|
|
|
|
|
|
|
|
Regards,
|
|
|
|
Fail2Ban"""
|
|
|
|
|
|
|
|
messages['stop'] = \
|
|
|
|
"""Hi,
|
|
|
|
|
|
|
|
The jail %(jailname)s has been stopped.
|
|
|
|
|
|
|
|
Regards,
|
|
|
|
Fail2Ban"""
|
|
|
|
|
2014-01-01 12:27:49 +00:00
|
|
|
messages['ban'] = {}
|
|
|
|
messages['ban']['head'] = \
|
2013-12-31 18:54:34 +00:00
|
|
|
"""Hi,
|
|
|
|
|
2014-06-17 22:24:23 +00:00
|
|
|
The IP %(ip)s has just been banned for %(bantime)i seconds
|
2013-12-31 18:54:34 +00:00
|
|
|
by Fail2Ban after %(failures)i attempts against %(jailname)s.
|
2014-01-01 12:27:49 +00:00
|
|
|
"""
|
|
|
|
messages['ban']['tail'] = \
|
|
|
|
"""
|
2013-12-31 18:54:34 +00:00
|
|
|
Regards,
|
|
|
|
Fail2Ban"""
|
2014-01-01 12:27:49 +00:00
|
|
|
messages['ban']['matches'] = \
|
|
|
|
"""
|
|
|
|
Matches for this ban:
|
|
|
|
%(matches)s
|
|
|
|
"""
|
|
|
|
messages['ban']['ipmatches'] = \
|
|
|
|
"""
|
|
|
|
Matches for %(ip)s:
|
|
|
|
%(ipmatches)s
|
|
|
|
"""
|
|
|
|
messages['ban']['ipjailmatches'] = \
|
|
|
|
"""
|
|
|
|
Matches for %(ip)s for jail %(jailname)s:
|
|
|
|
%(ipjailmatches)s
|
|
|
|
"""
|
2013-12-31 18:54:34 +00:00
|
|
|
|
2015-07-04 17:47:40 +00:00
|
|
|
|
2013-12-31 18:54:34 +00:00
|
|
|
class SMTPAction(ActionBase):
|
2014-01-03 17:04:49 +00:00
|
|
|
"""Fail2Ban action which sends emails to inform on jail starting,
|
|
|
|
stopping and bans.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
2014-01-04 22:46:57 +00:00
|
|
|
self, jail, name, host="localhost", user=None, password=None,
|
2014-01-03 17:04:49 +00:00
|
|
|
sendername="Fail2Ban", sender="fail2ban", dest="root", matches=None):
|
2014-01-04 22:46:57 +00:00
|
|
|
"""Initialise action.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
jail : Jail
|
|
|
|
The jail which the action belongs to.
|
|
|
|
name : str
|
|
|
|
Named assigned to the action.
|
|
|
|
host : str, optional
|
|
|
|
SMTP host, of host:port format. Default host "localhost" and
|
|
|
|
port "25"
|
|
|
|
user : str, optional
|
|
|
|
Username used for authentication with SMTP server.
|
|
|
|
password : str, optional
|
|
|
|
Password used for authentication with SMTP server.
|
|
|
|
sendername : str, optional
|
|
|
|
Name to use for from address in email. Default "Fail2Ban".
|
|
|
|
sender : str, optional
|
|
|
|
Email address to use for from address in email.
|
|
|
|
Default "fail2ban".
|
|
|
|
dest : str, optional
|
2014-01-19 23:48:18 +00:00
|
|
|
Email addresses of intended recipient(s) in comma space ", "
|
|
|
|
delimited format. Default "root".
|
2014-01-04 22:46:57 +00:00
|
|
|
matches : str, optional
|
|
|
|
Type of matches to be included from ban in email. Can be one
|
|
|
|
of "matches", "ipmatches" or "ipjailmatches". Default None
|
|
|
|
(see man jail.conf.5).
|
2014-01-03 17:04:49 +00:00
|
|
|
"""
|
|
|
|
|
2014-01-04 22:46:57 +00:00
|
|
|
super(SMTPAction, self).__init__(jail, name)
|
2014-01-03 17:04:49 +00:00
|
|
|
|
|
|
|
self.host = host
|
|
|
|
#TODO: self.ssl = ssl
|
|
|
|
|
|
|
|
self.user = user
|
|
|
|
self.password =password
|
|
|
|
|
|
|
|
self.fromname = sendername
|
|
|
|
self.fromaddr = sender
|
|
|
|
self.toaddr = dest
|
|
|
|
|
|
|
|
self.matches = matches
|
|
|
|
|
|
|
|
self.message_values = CallingMap(
|
2014-02-23 17:35:36 +00:00
|
|
|
jailname = self._jail.name,
|
2014-01-03 17:04:49 +00:00
|
|
|
hostname = socket.gethostname,
|
2017-02-24 10:54:24 +00:00
|
|
|
bantime = lambda: self._jail.actions.getBanTime(),
|
2014-01-03 17:04:49 +00:00
|
|
|
)
|
|
|
|
|
2017-01-16 08:40:48 +00:00
|
|
|
# bypass ban/unban for restored tickets
|
|
|
|
self.norestored = 1
|
|
|
|
|
2014-01-03 17:04:49 +00:00
|
|
|
def _sendMessage(self, subject, text):
|
2014-01-04 22:46:57 +00:00
|
|
|
"""Sends message based on arguments and instance's properties.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
subject : str
|
|
|
|
Subject of the email.
|
|
|
|
text : str
|
|
|
|
Body of the email.
|
|
|
|
|
|
|
|
Raises
|
|
|
|
------
|
|
|
|
SMTPConnectionError
|
|
|
|
Error on connecting to host.
|
|
|
|
SMTPAuthenticationError
|
|
|
|
Error authenticating with SMTP server.
|
|
|
|
SMTPException
|
|
|
|
See Python `smtplib` for full list of other possible
|
|
|
|
exceptions.
|
|
|
|
"""
|
2014-01-03 17:04:49 +00:00
|
|
|
msg = MIMEText(text)
|
|
|
|
msg['Subject'] = subject
|
|
|
|
msg['From'] = formataddr((self.fromname, self.fromaddr))
|
|
|
|
msg['To'] = self.toaddr
|
|
|
|
msg['Date'] = formatdate()
|
|
|
|
|
|
|
|
smtp = smtplib.SMTP()
|
|
|
|
try:
|
|
|
|
self._logSys.debug("Connected to SMTP '%s', response: %i: %s",
|
|
|
|
self.host, *smtp.connect(self.host))
|
2020-01-12 22:21:29 +00:00
|
|
|
if self.user and self.password: # pragma: no cover (ATM no tests covering that)
|
2014-01-03 17:04:49 +00:00
|
|
|
smtp.login(self.user, self.password)
|
|
|
|
failed_recipients = smtp.sendmail(
|
2014-01-19 23:48:18 +00:00
|
|
|
self.fromaddr, self.toaddr.split(", "), msg.as_string())
|
2020-01-12 22:21:29 +00:00
|
|
|
except smtplib.SMTPConnectError: # pragma: no cover
|
2014-01-03 17:04:49 +00:00
|
|
|
self._logSys.error("Error connecting to host '%s'", self.host)
|
|
|
|
raise
|
2020-01-12 22:21:29 +00:00
|
|
|
except smtplib.SMTPAuthenticationError: # pragma: no cover
|
2014-01-03 17:04:49 +00:00
|
|
|
self._logSys.error(
|
|
|
|
"Failed to authenticate with host '%s' user '%s'",
|
|
|
|
self.host, self.user)
|
|
|
|
raise
|
2020-01-12 22:21:29 +00:00
|
|
|
except smtplib.SMTPException: # pragma: no cover
|
2014-01-03 17:04:49 +00:00
|
|
|
self._logSys.error(
|
|
|
|
"Error sending mail to host '%s' from '%s' to '%s'",
|
|
|
|
self.host, self.fromaddr, self.toaddr)
|
|
|
|
raise
|
|
|
|
else:
|
2020-01-12 22:21:29 +00:00
|
|
|
if failed_recipients: # pragma: no cover
|
2014-01-03 17:04:49 +00:00
|
|
|
self._logSys.warning(
|
|
|
|
"Email to '%s' failed to following recipients: %r",
|
|
|
|
self.toaddr, failed_recipients)
|
|
|
|
self._logSys.debug("Email '%s' successfully sent", subject)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
self._logSys.debug("Disconnected from '%s', response %i: %s",
|
|
|
|
self.host, *smtp.quit())
|
2020-01-12 22:21:29 +00:00
|
|
|
except smtplib.SMTPServerDisconnected: # pragma: no cover
|
2014-01-03 17:04:49 +00:00
|
|
|
pass # Not connected
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Sends email to recipients informing that the jail has started.
|
|
|
|
"""
|
|
|
|
self._sendMessage(
|
|
|
|
"[Fail2Ban] %(jailname)s: started on %(hostname)s" %
|
|
|
|
self.message_values,
|
|
|
|
messages['start'] % self.message_values)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""Sends email to recipients informing that the jail has stopped.
|
|
|
|
"""
|
|
|
|
self._sendMessage(
|
|
|
|
"[Fail2Ban] %(jailname)s: stopped on %(hostname)s" %
|
|
|
|
self.message_values,
|
|
|
|
messages['stop'] % self.message_values)
|
|
|
|
|
|
|
|
def ban(self, aInfo):
|
2014-01-04 22:46:57 +00:00
|
|
|
"""Sends email to recipients informing that ban has occurred.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
aInfo : dict
|
|
|
|
Dictionary which includes information in relation to
|
|
|
|
the ban.
|
2014-01-03 17:04:49 +00:00
|
|
|
"""
|
2017-01-13 18:06:17 +00:00
|
|
|
if aInfo.get('restored'):
|
|
|
|
return
|
2014-01-03 17:04:49 +00:00
|
|
|
aInfo.update(self.message_values)
|
|
|
|
message = "".join([
|
|
|
|
messages['ban']['head'],
|
|
|
|
messages['ban'].get(self.matches, ""),
|
|
|
|
messages['ban']['tail']
|
|
|
|
])
|
|
|
|
self._sendMessage(
|
|
|
|
"[Fail2Ban] %(jailname)s: banned %(ip)s from %(hostname)s" %
|
|
|
|
aInfo,
|
|
|
|
message % aInfo)
|
2013-12-31 18:54:34 +00:00
|
|
|
|
|
|
|
Action = SMTPAction
|