2006-06-26 20:05:00 +00:00
|
|
|
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
# Author: Cyril Jaquier
|
|
|
|
#
|
2009-09-10 19:08:14 +00:00
|
|
|
# $Revision: 752 $
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2009-09-10 19:08:14 +00:00
|
|
|
__version__ = "$Revision: 752 $"
|
|
|
|
__date__ = "$Date: 2009-09-01 23:21:30 +0200 (Tue, 01 Sep 2009) $"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
|
|
|
from failmanager import FailManager
|
2008-03-06 01:19:06 +00:00
|
|
|
from ticket import FailTicket
|
2006-06-26 20:05:00 +00:00
|
|
|
from jailthread import JailThread
|
2006-09-05 21:16:28 +00:00
|
|
|
from datedetector import DateDetector
|
2006-10-18 22:30:57 +00:00
|
|
|
from mytime import MyTime
|
2008-03-06 01:19:06 +00:00
|
|
|
from failregex import FailRegex, Regex, RegexException
|
2006-09-05 21:16:28 +00:00
|
|
|
|
2010-03-04 17:15:12 +00:00
|
|
|
import logging, re, os, fcntl, time
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
|
|
|
logSys = logging.getLogger("fail2ban.filter")
|
|
|
|
|
|
|
|
##
|
|
|
|
# Log reader class.
|
|
|
|
#
|
|
|
|
# This class reads a log file and detects login failures or anything else
|
|
|
|
# that matches a given regular expression. This class is instanciated by
|
|
|
|
# a Jail object.
|
|
|
|
|
|
|
|
class Filter(JailThread):
|
|
|
|
|
|
|
|
##
|
|
|
|
# Constructor.
|
|
|
|
#
|
|
|
|
# Initialize the filter object with default values.
|
|
|
|
# @param jail the jail object
|
|
|
|
|
|
|
|
def __init__(self, jail):
|
2006-10-24 19:40:51 +00:00
|
|
|
JailThread.__init__(self)
|
2006-06-26 20:05:00 +00:00
|
|
|
## The jail which contains this filter.
|
|
|
|
self.jail = jail
|
|
|
|
## The failures manager.
|
|
|
|
self.failManager = FailManager()
|
2006-12-23 16:31:00 +00:00
|
|
|
## The regular expression list matching the failures.
|
|
|
|
self.__failRegex = list()
|
|
|
|
## The regular expression list with expressions to ignore.
|
|
|
|
self.__ignoreRegex = list()
|
2006-06-26 20:05:00 +00:00
|
|
|
## The amount of time to look back.
|
2006-09-21 20:55:45 +00:00
|
|
|
self.__findTime = 6000
|
2006-06-26 20:05:00 +00:00
|
|
|
## The ignore IP list.
|
2006-09-21 20:55:45 +00:00
|
|
|
self.__ignoreIpList = []
|
2006-09-13 21:31:22 +00:00
|
|
|
|
2006-09-05 21:16:28 +00:00
|
|
|
self.dateDetector = DateDetector()
|
|
|
|
self.dateDetector.addDefaultTemplate()
|
2008-07-21 14:13:13 +00:00
|
|
|
logSys.debug("Created Filter")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-14 22:05:32 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
2006-12-23 16:31:00 +00:00
|
|
|
# Add a regular expression which matches the failure.
|
2006-06-26 20:05:00 +00:00
|
|
|
#
|
|
|
|
# The regular expression can also match any other pattern than failures
|
|
|
|
# and thus can be used for many purporse.
|
|
|
|
# @param value the regular expression
|
|
|
|
|
2006-12-23 16:31:00 +00:00
|
|
|
def addFailRegex(self, value):
|
2006-10-31 22:24:34 +00:00
|
|
|
try:
|
2006-12-23 16:31:00 +00:00
|
|
|
regex = FailRegex(value)
|
|
|
|
self.__failRegex.append(regex)
|
|
|
|
except RegexException, e:
|
|
|
|
logSys.error(e)
|
|
|
|
|
|
|
|
|
|
|
|
def delFailRegex(self, index):
|
|
|
|
try:
|
|
|
|
del self.__failRegex[index]
|
|
|
|
except IndexError:
|
|
|
|
logSys.error("Cannot remove regular expression. Index %d is not "
|
|
|
|
"valid" % index)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the regular expression which matches the failure.
|
|
|
|
#
|
|
|
|
# @return the regular expression
|
|
|
|
|
|
|
|
def getFailRegex(self):
|
2006-12-23 16:31:00 +00:00
|
|
|
failRegex = list()
|
|
|
|
for regex in self.__failRegex:
|
|
|
|
failRegex.append(regex.getRegex())
|
|
|
|
return failRegex
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-11-12 14:52:36 +00:00
|
|
|
##
|
2006-12-23 16:31:00 +00:00
|
|
|
# Add the regular expression which matches the failure.
|
2006-11-12 14:52:36 +00:00
|
|
|
#
|
|
|
|
# The regular expression can also match any other pattern than failures
|
|
|
|
# and thus can be used for many purporse.
|
|
|
|
# @param value the regular expression
|
|
|
|
|
2006-12-23 16:31:00 +00:00
|
|
|
def addIgnoreRegex(self, value):
|
2006-11-12 14:52:36 +00:00
|
|
|
try:
|
2006-12-23 16:31:00 +00:00
|
|
|
regex = Regex(value)
|
|
|
|
self.__ignoreRegex.append(regex)
|
|
|
|
except RegexException, e:
|
|
|
|
logSys.error(e)
|
|
|
|
|
|
|
|
def delIgnoreRegex(self, index):
|
|
|
|
try:
|
|
|
|
del self.__ignoreRegex[index]
|
|
|
|
except IndexError:
|
|
|
|
logSys.error("Cannot remove regular expression. Index %d is not "
|
|
|
|
"valid" % index)
|
2006-11-12 14:52:36 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Get the regular expression which matches the failure.
|
|
|
|
#
|
|
|
|
# @return the regular expression
|
|
|
|
|
|
|
|
def getIgnoreRegex(self):
|
2006-12-23 16:31:00 +00:00
|
|
|
ignoreRegex = list()
|
|
|
|
for regex in self.__ignoreRegex:
|
|
|
|
ignoreRegex.append(regex.getRegex())
|
|
|
|
return ignoreRegex
|
2006-11-12 14:52:36 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
|
|
|
# Set the time needed to find a failure.
|
|
|
|
#
|
|
|
|
# This value tells the filter how long it has to take failures into
|
|
|
|
# account.
|
|
|
|
# @param value the time
|
|
|
|
|
|
|
|
def setFindTime(self, value):
|
2006-09-21 20:55:45 +00:00
|
|
|
self.__findTime = value
|
2006-11-18 15:15:58 +00:00
|
|
|
self.failManager.setMaxTime(value)
|
2006-06-26 20:05:00 +00:00
|
|
|
logSys.info("Set findtime = %s" % value)
|
|
|
|
|
|
|
|
##
|
|
|
|
# Get the time needed to find a failure.
|
|
|
|
#
|
|
|
|
# @return the time
|
|
|
|
|
|
|
|
def getFindTime(self):
|
2006-09-21 20:55:45 +00:00
|
|
|
return self.__findTime
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Set the maximum retry value.
|
|
|
|
#
|
|
|
|
# @param value the retry value
|
|
|
|
|
|
|
|
def setMaxRetry(self, value):
|
|
|
|
self.failManager.setMaxRetry(value)
|
|
|
|
logSys.info("Set maxRetry = %s" % value)
|
|
|
|
|
|
|
|
##
|
|
|
|
# Get the maximum retry value.
|
|
|
|
#
|
|
|
|
# @return the retry value
|
|
|
|
|
|
|
|
def getMaxRetry(self):
|
|
|
|
return self.failManager.getMaxRetry()
|
|
|
|
|
|
|
|
##
|
|
|
|
# Main loop.
|
|
|
|
#
|
|
|
|
# This function is the main loop of the thread. It checks if the
|
|
|
|
# file has been modified and looks for failures.
|
|
|
|
# @return True when the thread exits nicely
|
|
|
|
|
|
|
|
def run(self):
|
2006-09-14 22:05:32 +00:00
|
|
|
raise Exception("run() is abstract")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2009-09-10 19:08:14 +00:00
|
|
|
##
|
|
|
|
# Ban an IP - http://blogs.buanzo.com.ar/2009/04/fail2ban-patch-ban-ip-address-manually.html
|
|
|
|
# Arturo 'Buanzo' Busleiman <buanzo@buanzo.com.ar>
|
|
|
|
#
|
|
|
|
# to enable banip fail2ban-client BAN command
|
|
|
|
|
|
|
|
def addBannedIP(self, ip):
|
|
|
|
unixTime = time.time()
|
|
|
|
self.failManager.addFailure(FailTicket(ip, unixTime))
|
|
|
|
return ip
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
2006-09-27 20:32:30 +00:00
|
|
|
# Add an IP/DNS to the ignore list.
|
2006-06-26 20:05:00 +00:00
|
|
|
#
|
|
|
|
# IP addresses in the ignore list are not taken into account
|
2006-09-27 20:32:30 +00:00
|
|
|
# when finding failures. CIDR mask and DNS are also accepted.
|
2006-06-26 20:05:00 +00:00
|
|
|
# @param ip IP address to ignore
|
|
|
|
|
|
|
|
def addIgnoreIP(self, ip):
|
2006-09-27 20:32:30 +00:00
|
|
|
logSys.debug("Add " + ip + " to ignore list")
|
|
|
|
self.__ignoreIpList.append(ip)
|
2006-09-17 22:01:14 +00:00
|
|
|
|
|
|
|
def delIgnoreIP(self, ip):
|
|
|
|
logSys.debug("Remove " + ip + " from ignore list")
|
2006-09-21 20:55:45 +00:00
|
|
|
self.__ignoreIpList.remove(ip)
|
2006-09-17 22:01:14 +00:00
|
|
|
|
|
|
|
def getIgnoreIP(self):
|
2006-09-21 20:55:45 +00:00
|
|
|
return self.__ignoreIpList
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
2006-09-27 20:32:30 +00:00
|
|
|
# Check if IP address/DNS is in the ignore list.
|
2006-06-26 20:05:00 +00:00
|
|
|
#
|
2006-09-27 20:32:30 +00:00
|
|
|
# Check if the given IP address matches an IP address/DNS or a CIDR
|
2006-06-26 20:05:00 +00:00
|
|
|
# mask in the ignore list.
|
|
|
|
# @param ip IP address
|
|
|
|
# @return True if IP address is in ignore list
|
|
|
|
|
|
|
|
def inIgnoreIPList(self, ip):
|
2006-09-21 20:55:45 +00:00
|
|
|
for i in self.__ignoreIpList:
|
2006-09-27 20:32:30 +00:00
|
|
|
# An empty string is always false
|
|
|
|
if i == "":
|
2008-03-06 01:19:06 +00:00
|
|
|
continue
|
2006-06-26 20:05:00 +00:00
|
|
|
s = i.split('/', 1)
|
|
|
|
# IP address without CIDR mask
|
|
|
|
if len(s) == 1:
|
|
|
|
s.insert(1, '32')
|
|
|
|
s[1] = long(s[1])
|
|
|
|
try:
|
2006-08-20 20:39:45 +00:00
|
|
|
a = DNSUtils.cidr(s[0], s[1])
|
|
|
|
b = DNSUtils.cidr(ip, s[1])
|
2006-06-26 20:05:00 +00:00
|
|
|
except Exception:
|
2006-09-27 20:32:30 +00:00
|
|
|
# Check if IP in DNS
|
|
|
|
ips = DNSUtils.dnsToIp(i)
|
|
|
|
if ip in ips:
|
|
|
|
return True
|
|
|
|
else:
|
2008-03-06 01:19:06 +00:00
|
|
|
continue
|
2006-06-26 20:05:00 +00:00
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
def processLine(self, line):
|
|
|
|
try:
|
|
|
|
# Decode line to UTF-8
|
|
|
|
l = line.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
l = line
|
|
|
|
timeMatch = self.dateDetector.matchTime(l)
|
2008-08-12 22:40:07 +00:00
|
|
|
if timeMatch:
|
|
|
|
# Lets split into time part and log part of the line
|
|
|
|
timeLine = timeMatch.group()
|
|
|
|
# Lets leave the beginning in as well, so if there is no
|
|
|
|
# anchore at the beginning of the time regexp, we don't
|
|
|
|
# at least allow injection. Should be harmless otherwise
|
|
|
|
logLine = l[:timeMatch.start()] + l[timeMatch.end():]
|
|
|
|
else:
|
|
|
|
timeLine = l
|
|
|
|
logLine = l
|
2008-03-06 01:19:06 +00:00
|
|
|
return self.findFailure(timeLine, logLine)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
def processLineAndAdd(self, line):
|
|
|
|
for element in self.processLine(line):
|
|
|
|
ip = element[0]
|
|
|
|
unixTime = element[1]
|
|
|
|
if unixTime < MyTime.time() - self.getFindTime():
|
2006-10-30 22:48:52 +00:00
|
|
|
break
|
2008-03-06 01:19:06 +00:00
|
|
|
if self.inIgnoreIPList(ip):
|
|
|
|
logSys.debug("Ignore %s" % ip)
|
2006-06-26 20:05:00 +00:00
|
|
|
continue
|
2008-03-06 01:19:06 +00:00
|
|
|
logSys.debug("Found %s" % ip)
|
|
|
|
self.failManager.addFailure(FailTicket(ip, unixTime))
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2007-07-10 19:54:01 +00:00
|
|
|
##
|
|
|
|
# Returns true if the line should be ignored.
|
|
|
|
#
|
|
|
|
# Uses ignoreregex.
|
|
|
|
# @param line: the line
|
|
|
|
# @return: a boolean
|
|
|
|
|
|
|
|
def ignoreLine(self, line):
|
|
|
|
for ignoreRegex in self.__ignoreRegex:
|
|
|
|
ignoreRegex.search(line)
|
|
|
|
if ignoreRegex.hasMatched():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
##
|
2008-03-06 01:19:06 +00:00
|
|
|
# Finds the failure in a line given split into time and log parts.
|
2006-06-26 20:05:00 +00:00
|
|
|
#
|
|
|
|
# Uses the failregex pattern to find it and timeregex in order
|
|
|
|
# to find the logging time.
|
|
|
|
# @return a dict with IP and timestamp.
|
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
def findFailure(self, timeLine, logLine):
|
2006-06-26 20:05:00 +00:00
|
|
|
failList = list()
|
2006-12-23 16:31:00 +00:00
|
|
|
# Checks if we must ignore this line.
|
2008-03-06 01:19:06 +00:00
|
|
|
if self.ignoreLine(logLine):
|
2007-07-10 19:54:01 +00:00
|
|
|
# The ignoreregex matched. Return.
|
|
|
|
return failList
|
2006-12-23 16:31:00 +00:00
|
|
|
# Iterates over all the regular expressions.
|
|
|
|
for failRegex in self.__failRegex:
|
2008-03-06 01:19:06 +00:00
|
|
|
failRegex.search(logLine)
|
2006-12-23 16:31:00 +00:00
|
|
|
if failRegex.hasMatched():
|
|
|
|
# The failregex matched.
|
2008-03-06 01:19:06 +00:00
|
|
|
date = self.dateDetector.getUnixTime(timeLine)
|
2006-12-23 16:31:00 +00:00
|
|
|
if date == None:
|
2008-03-06 01:19:06 +00:00
|
|
|
logSys.debug("Found a match for '" + logLine +"' but no "
|
|
|
|
+ "valid date/time found for '"
|
|
|
|
+ timeLine + "'. Please contact the "
|
2006-12-23 16:31:00 +00:00
|
|
|
+ "author in order to get support for this "
|
|
|
|
+ "format")
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
host = failRegex.getHost()
|
|
|
|
ipMatch = DNSUtils.textToIp(host)
|
2006-12-23 09:50:51 +00:00
|
|
|
if ipMatch:
|
|
|
|
for ip in ipMatch:
|
|
|
|
failList.append([ip, date])
|
2006-12-23 16:31:00 +00:00
|
|
|
# We matched a regex, it is enough to stop.
|
|
|
|
break
|
|
|
|
except RegexException, e:
|
|
|
|
logSys.error(e)
|
2006-06-26 20:05:00 +00:00
|
|
|
return failList
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Get the status of the filter.
|
|
|
|
#
|
|
|
|
# Get some informations about the filter state such as the total
|
|
|
|
# number of failures.
|
|
|
|
# @return a list with tuple
|
|
|
|
|
|
|
|
def status(self):
|
2006-11-12 14:52:36 +00:00
|
|
|
ret = [("Currently failed", self.failManager.size()),
|
2006-06-26 20:05:00 +00:00
|
|
|
("Total failed", self.failManager.getFailTotal())]
|
|
|
|
return ret
|
2006-08-20 20:39:45 +00:00
|
|
|
|
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
class FileFilter(Filter):
|
|
|
|
|
|
|
|
def __init__(self, jail):
|
|
|
|
Filter.__init__(self, jail)
|
|
|
|
## The log file path.
|
|
|
|
self.__logPath = []
|
|
|
|
|
|
|
|
##
|
|
|
|
# Add a log file path
|
|
|
|
#
|
|
|
|
# @param path log file path
|
|
|
|
|
|
|
|
def addLogPath(self, path, tail = False):
|
|
|
|
container = FileContainer(path, tail)
|
|
|
|
self.__logPath.append(container)
|
|
|
|
|
|
|
|
##
|
|
|
|
# Delete a log path
|
|
|
|
#
|
|
|
|
# @param path the log file to delete
|
|
|
|
|
|
|
|
def delLogPath(self, path):
|
|
|
|
for log in self.__logPath:
|
|
|
|
if log.getFileName() == path:
|
|
|
|
self.__logPath.remove(log)
|
|
|
|
return
|
|
|
|
|
|
|
|
##
|
|
|
|
# Get the log file path
|
|
|
|
#
|
|
|
|
# @return log file path
|
|
|
|
|
|
|
|
def getLogPath(self):
|
|
|
|
return self.__logPath
|
|
|
|
|
|
|
|
##
|
|
|
|
# Check whether path is already monitored.
|
|
|
|
#
|
|
|
|
# @param path The path
|
|
|
|
# @return True if the path is already monitored else False
|
|
|
|
|
|
|
|
def containsLogPath(self, path):
|
|
|
|
for log in self.__logPath:
|
|
|
|
if log.getFileName() == path:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def getFileContainer(self, path):
|
|
|
|
for log in self.__logPath:
|
|
|
|
if log.getFileName() == path:
|
|
|
|
return log
|
|
|
|
return None
|
|
|
|
|
|
|
|
##
|
|
|
|
# Gets all the failure in the log file.
|
|
|
|
#
|
|
|
|
# Gets all the failure in the log file which are newer than
|
|
|
|
# MyTime.time()-self.findTime. When a failure is detected, a FailTicket
|
|
|
|
# is created and is added to the FailManager.
|
|
|
|
|
|
|
|
def getFailures(self, filename):
|
|
|
|
container = self.getFileContainer(filename)
|
|
|
|
if container == None:
|
|
|
|
logSys.error("Unable to get failures in " + filename)
|
|
|
|
return False
|
|
|
|
# Try to open log file.
|
|
|
|
try:
|
|
|
|
container.open()
|
|
|
|
except Exception, e:
|
|
|
|
logSys.error("Unable to open %s" % filename)
|
|
|
|
logSys.exception(e)
|
|
|
|
return False
|
|
|
|
|
|
|
|
line = container.readline()
|
|
|
|
while not line == "":
|
|
|
|
if not self._isActive():
|
|
|
|
# The jail has been stopped
|
|
|
|
break
|
|
|
|
self.processLineAndAdd(line)
|
|
|
|
# Read a new line.
|
|
|
|
line = container.readline()
|
|
|
|
container.close()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def status(self):
|
|
|
|
ret = Filter.status(self)
|
|
|
|
path = [m.getFileName() for m in self.getLogPath()]
|
|
|
|
ret.append(("File list", path))
|
|
|
|
return ret
|
|
|
|
|
|
|
|
##
|
|
|
|
# FileContainer class.
|
|
|
|
#
|
|
|
|
# This class manages a file handler and takes care of log rotation detection.
|
|
|
|
# In order to detect log rotation, the hash (MD5) of the first line of the file
|
|
|
|
# is computed and compared to the previous hash of this line.
|
|
|
|
|
|
|
|
import md5
|
|
|
|
|
|
|
|
class FileContainer:
|
|
|
|
|
|
|
|
def __init__(self, filename, tail = False):
|
|
|
|
self.__filename = filename
|
|
|
|
self.__tail = tail
|
|
|
|
self.__handler = None
|
|
|
|
# Try to open the file. Raises an exception if an error occured.
|
|
|
|
handler = open(filename)
|
2009-09-10 19:08:14 +00:00
|
|
|
stats = os.fstat(handler.fileno())
|
|
|
|
self.__ino = stats.st_ino
|
2008-03-06 01:19:06 +00:00
|
|
|
try:
|
|
|
|
firstLine = handler.readline()
|
|
|
|
# Computes the MD5 of the first line.
|
|
|
|
self.__hash = md5.new(firstLine).digest()
|
|
|
|
# Start at the beginning of file if tail mode is off.
|
|
|
|
if tail:
|
|
|
|
handler.seek(0, 2)
|
|
|
|
self.__pos = handler.tell()
|
|
|
|
else:
|
|
|
|
self.__pos = 0
|
|
|
|
finally:
|
|
|
|
handler.close()
|
|
|
|
|
|
|
|
def getFileName(self):
|
|
|
|
return self.__filename
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
self.__handler = open(self.__filename)
|
2009-12-15 22:57:54 +00:00
|
|
|
# Set the file descriptor to be FD_CLOEXEC
|
|
|
|
fd = self.__handler.fileno()
|
|
|
|
fcntl.fcntl(fd, fcntl.F_SETFD, fd | fcntl.FD_CLOEXEC)
|
2008-03-06 01:19:06 +00:00
|
|
|
firstLine = self.__handler.readline()
|
|
|
|
# Computes the MD5 of the first line.
|
|
|
|
myHash = md5.new(firstLine).digest()
|
2009-09-10 19:08:14 +00:00
|
|
|
stats = os.fstat(self.__handler.fileno())
|
|
|
|
# Compare hash and inode
|
|
|
|
if self.__hash != myHash or self.__ino != stats.st_ino:
|
2008-03-06 01:19:06 +00:00
|
|
|
logSys.info("Log rotation detected for %s" % self.__filename)
|
|
|
|
self.__hash = myHash
|
2009-09-10 19:08:14 +00:00
|
|
|
self.__ino = stats.st_ino
|
2008-03-06 01:19:06 +00:00
|
|
|
self.__pos = 0
|
|
|
|
# Sets the file pointer to the last position.
|
|
|
|
self.__handler.seek(self.__pos)
|
|
|
|
|
|
|
|
def readline(self):
|
|
|
|
if self.__handler == None:
|
|
|
|
return ""
|
|
|
|
return self.__handler.readline()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if not self.__handler == None:
|
|
|
|
# Saves the last position.
|
|
|
|
self.__pos = self.__handler.tell()
|
|
|
|
# Closes the file.
|
|
|
|
self.__handler.close()
|
|
|
|
self.__handler = None
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-20 20:39:45 +00:00
|
|
|
##
|
|
|
|
# Utils class for DNS and IP handling.
|
|
|
|
#
|
|
|
|
# This class contains only static methods used to handle DNS and IP
|
|
|
|
# addresses.
|
|
|
|
|
|
|
|
import socket, struct
|
|
|
|
|
|
|
|
class DNSUtils:
|
|
|
|
|
2009-02-08 17:31:24 +00:00
|
|
|
IP_CRE = re.compile("^(?:\d{1,3}\.){3}\d{1,3}$")
|
2006-08-20 20:39:45 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-08-20 20:39:45 +00:00
|
|
|
def dnsToIp(dns):
|
|
|
|
""" Convert a DNS into an IP address using the Python socket module.
|
|
|
|
Thanks to Kevin Drapel.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return socket.gethostbyname_ex(dns)[2]
|
|
|
|
except socket.gaierror:
|
2006-11-19 21:25:51 +00:00
|
|
|
logSys.warn("Unable to find a corresponding IP address for %s"
|
|
|
|
% dns)
|
2006-08-20 20:39:45 +00:00
|
|
|
return list()
|
2008-03-06 01:19:06 +00:00
|
|
|
dnsToIp = staticmethod(dnsToIp)
|
2006-08-20 20:39:45 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-08-20 20:39:45 +00:00
|
|
|
def searchIP(text):
|
|
|
|
""" Search if an IP address if directly available and return
|
|
|
|
it.
|
|
|
|
"""
|
2006-09-27 20:32:30 +00:00
|
|
|
match = DNSUtils.IP_CRE.match(text)
|
2006-08-20 20:39:45 +00:00
|
|
|
if match:
|
|
|
|
return match
|
|
|
|
else:
|
|
|
|
return None
|
2008-03-06 01:19:06 +00:00
|
|
|
searchIP = staticmethod(searchIP)
|
2006-08-20 20:39:45 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-10-24 19:40:51 +00:00
|
|
|
def isValidIP(string):
|
2006-08-20 20:39:45 +00:00
|
|
|
""" Return true if str is a valid IP
|
|
|
|
"""
|
2006-10-24 19:40:51 +00:00
|
|
|
s = string.split('/', 1)
|
2006-08-20 20:39:45 +00:00
|
|
|
try:
|
|
|
|
socket.inet_aton(s[0])
|
|
|
|
return True
|
|
|
|
except socket.error:
|
|
|
|
return False
|
2008-03-06 01:19:06 +00:00
|
|
|
isValidIP = staticmethod(isValidIP)
|
2006-08-20 20:39:45 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-08-20 20:39:45 +00:00
|
|
|
def textToIp(text):
|
|
|
|
""" Return the IP of DNS found in a given text.
|
|
|
|
"""
|
|
|
|
ipList = list()
|
|
|
|
# Search for plain IP
|
|
|
|
plainIP = DNSUtils.searchIP(text)
|
|
|
|
if not plainIP == None:
|
2006-08-20 22:54:05 +00:00
|
|
|
plainIPStr = plainIP.group(0)
|
|
|
|
if DNSUtils.isValidIP(plainIPStr):
|
|
|
|
ipList.append(plainIPStr)
|
2006-08-20 20:39:45 +00:00
|
|
|
if not ipList:
|
|
|
|
# Try to get IP from possible DNS
|
2007-08-07 22:11:34 +00:00
|
|
|
ip = DNSUtils.dnsToIp(text)
|
|
|
|
for e in ip:
|
|
|
|
ipList.append(e)
|
2006-08-20 20:39:45 +00:00
|
|
|
return ipList
|
2008-03-06 01:19:06 +00:00
|
|
|
textToIp = staticmethod(textToIp)
|
2006-08-20 20:39:45 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-08-20 20:39:45 +00:00
|
|
|
def cidr(i, n):
|
|
|
|
""" Convert an IP address string with a CIDR mask into a 32-bit
|
|
|
|
integer.
|
|
|
|
"""
|
|
|
|
# 32-bit IPv4 address mask
|
|
|
|
MASK = 0xFFFFFFFFL
|
|
|
|
return ~(MASK >> n) & MASK & DNSUtils.addr2bin(i)
|
2008-03-06 01:19:06 +00:00
|
|
|
cidr = staticmethod(cidr)
|
2006-08-20 20:39:45 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-10-24 19:40:51 +00:00
|
|
|
def addr2bin(string):
|
2006-08-20 20:39:45 +00:00
|
|
|
""" Convert a string IPv4 address into an unsigned integer.
|
|
|
|
"""
|
2006-10-24 19:40:51 +00:00
|
|
|
return struct.unpack("!L", socket.inet_aton(string))[0]
|
2008-03-06 01:19:06 +00:00
|
|
|
addr2bin = staticmethod(addr2bin)
|
2006-08-20 20:39:45 +00:00
|
|
|
|
2008-03-06 01:19:06 +00:00
|
|
|
#@staticmethod
|
2006-08-20 20:39:45 +00:00
|
|
|
def bin2addr(addr):
|
|
|
|
""" Convert a numeric IPv4 address into string n.n.n.n form.
|
|
|
|
"""
|
|
|
|
return socket.inet_ntoa(struct.pack("!L", addr))
|
2008-03-06 01:19:06 +00:00
|
|
|
bin2addr = staticmethod(bin2addr)
|