From 4559dbf09fcd2f2a10fc2caed069a8bbf08555d8 Mon Sep 17 00:00:00 2001 From: Cyril Jaquier Date: Tue, 1 Nov 2005 08:31:27 +0000 Subject: [PATCH] - Added an option to report local time (including timezone) or GMT in mail notification. Thanks to Yaroslav Halchenko git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/branches/FAIL2BAN-0_5@213 a942ae1a-1317-0410-a47c-b1dcaea8d605 --- config/fail2ban.conf.default | 6 ++++++ fail2ban.py | 2 ++ utils/mail.py | 13 +++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/config/fail2ban.conf.default b/config/fail2ban.conf.default index e2c295674..d841bd7a9 100644 --- a/config/fail2ban.conf.default +++ b/config/fail2ban.conf.default @@ -132,6 +132,12 @@ from = fail2ban # to = root +# Option: localtime +# Notes.: report local time (including timezone) or GMT +# Values: [true | false] Default: false +# +localtime = true + # Option: subject # Notes.: subject of the e-mail. # Tags:
active section (eg ssh, apache, etc) diff --git a/fail2ban.py b/fail2ban.py index 5a630be8d..d2e88b3ec 100755 --- a/fail2ban.py +++ b/fail2ban.py @@ -358,6 +358,7 @@ def main(): ["int", "port", "25"], ["str", "from", "root"], ["str", "to", "root"], + ["bool", "localtime", False], ["str", "subject", "[Fail2Ban] Banned "], ["str", "message", "Fail2Ban notification"]) @@ -370,6 +371,7 @@ def main(): mail = Mail(mailConf["host"], mailConf["port"]) mail.setFromAddr(mailConf["from"]) mail.setToAddr(mailConf["to"]) + mail.setLocalTimeFlag(mailConf["localtime"]) logSys.debug("to: " + mailConf["to"] + " from: " + mailConf["from"]) # Options diff --git a/utils/mail.py b/utils/mail.py index 232703358..8a244d4fb 100644 --- a/utils/mail.py +++ b/utils/mail.py @@ -24,10 +24,9 @@ __date__ = "$Date$" __copyright__ = "Copyright (c) 2004 Cyril Jaquier" __license__ = "GPL" -import logging, smtplib +import logging, smtplib, email.Utils from utils.strings import replaceTag -from time import strftime, gmtime # Gets the instance of the logger. logSys = logging.getLogger("fail2ban") @@ -39,6 +38,7 @@ class Mail: def __init__(self, host, port = 25): self.host = host self.port = port + self.localTimeFlag = False def setFromAddr(self, fromAddr): """ Set from: address @@ -50,6 +50,11 @@ class Mail: """ self.toAddr = toAddr.split() + def setLocalTimeFlag(self, localTimeFlag): + """ Set to: address + """ + self.localTimeFlag = localTimeFlag + def sendmail(self, subject, message, aInfo): """ Send an email using smtplib """ @@ -58,7 +63,7 @@ class Mail: mail = ("From: %s\r\nTo: %s\r\nDate: %s\r\nSubject: %s\r\n\r\n" % (self.fromAddr, ", ".join(self.toAddr), - strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()), + email.Utils.formatdate(localtime = self.localTimeFlag), subj)) + msg try: @@ -71,4 +76,4 @@ class Mail: logSys.error("Unable to send mail to " + self.host + ":" + `self.port` + " from " + self.fromAddr + " to " + `self.toAddr` + ": " + `e` + ": " + `e.args`) - \ No newline at end of file +