2007-01-21 22:19:57 +00:00
|
|
|
#!/usr/bin/python
|
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
|
|
|
|
#
|
2007-10-16 21:01:21 +00:00
|
|
|
# $Revision: 522 $
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2007-10-16 21:01:21 +00:00
|
|
|
__version__ = "$Revision: 522 $"
|
|
|
|
__date__ = "$Date: 2007-01-21 23:19:57 +0100 (Sun, 21 Jan 2007) $"
|
2006-06-26 20:05:00 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2006-09-19 22:20:21 +00:00
|
|
|
import getopt, sys
|
2006-07-08 16:51:14 +00:00
|
|
|
|
|
|
|
# Inserts our own modules path first in the list
|
|
|
|
# fix for bug #343821
|
2007-01-04 12:58:21 +00:00
|
|
|
sys.path.insert(1, "/usr/share/fail2ban")
|
2006-07-08 16:51:14 +00:00
|
|
|
|
2006-11-16 21:07:42 +00:00
|
|
|
from common.version import version
|
2006-06-26 20:05:00 +00:00
|
|
|
from server.server import Server
|
|
|
|
|
|
|
|
##
|
|
|
|
# \mainpage Fail2Ban
|
|
|
|
#
|
|
|
|
# \section Introduction
|
|
|
|
#
|
|
|
|
# Fail2ban is designed to protect your server against brute force attacks.
|
|
|
|
# Its first goal was to protect a SSH server.
|
|
|
|
|
|
|
|
class Fail2banServer:
|
|
|
|
|
|
|
|
def __init__(self):
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server = None
|
|
|
|
self.__argv = None
|
|
|
|
self.__conf = dict()
|
|
|
|
self.__conf["background"] = True
|
|
|
|
self.__conf["force"] = False
|
2006-10-04 22:17:53 +00:00
|
|
|
self.__conf["socket"] = "/tmp/fail2ban.sock"
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-12 21:33:00 +00:00
|
|
|
def dispVersion(self):
|
|
|
|
print "Fail2Ban v" + version
|
|
|
|
print
|
|
|
|
print "Copyright (c) 2004-2006 Cyril Jaquier"
|
|
|
|
print "Copyright of modifications held by their respective authors."
|
|
|
|
print "Licensed under the GNU General Public License v2 (GPL)."
|
|
|
|
print
|
|
|
|
print "Written by Cyril Jaquier <lostcontrol@users.sourceforge.net>."
|
|
|
|
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def dispUsage(self):
|
|
|
|
""" Prints Fail2Ban command line options and exits
|
|
|
|
"""
|
2006-09-19 20:38:32 +00:00
|
|
|
print "Usage: "+self.__argv[0]+" [OPTIONS]"
|
2006-06-26 20:05:00 +00:00
|
|
|
print
|
2006-07-08 16:51:14 +00:00
|
|
|
print "Fail2Ban v" + version + " reads log file that contains password failure report"
|
2006-06-26 20:05:00 +00:00
|
|
|
print "and bans the corresponding IP addresses using firewall rules."
|
|
|
|
print
|
2006-09-12 21:33:00 +00:00
|
|
|
print "Only use this command for debugging purpose. Start the server with"
|
2006-11-19 21:26:47 +00:00
|
|
|
print "fail2ban-client instead. The default behaviour is to start the server"
|
|
|
|
print "in background."
|
2006-09-12 21:33:00 +00:00
|
|
|
print
|
|
|
|
print "Options:"
|
2006-11-19 21:26:47 +00:00
|
|
|
print " -b start in background"
|
|
|
|
print " -f start in foreground"
|
|
|
|
print " -s <FILE> socket path"
|
2007-01-04 23:34:35 +00:00
|
|
|
print " -x force execution of the server (remove socket file)"
|
2006-11-19 21:26:47 +00:00
|
|
|
print " -h, --help display this help message"
|
|
|
|
print " -V, --version print the version"
|
2006-06-26 20:05:00 +00:00
|
|
|
print
|
|
|
|
print "Report bugs to <lostcontrol@users.sourceforge.net>"
|
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
def __getCmdLineOptions(self, optList):
|
2006-06-26 20:05:00 +00:00
|
|
|
""" Gets the command line options
|
|
|
|
"""
|
|
|
|
for opt in optList:
|
|
|
|
if opt[0] == "-b":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["background"] = True
|
2006-06-26 20:05:00 +00:00
|
|
|
if opt[0] == "-f":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["background"] = False
|
2006-10-04 22:17:53 +00:00
|
|
|
if opt[0] == "-s":
|
|
|
|
self.__conf["socket"] = opt[1]
|
2006-08-17 22:48:42 +00:00
|
|
|
if opt[0] == "-x":
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__conf["force"] = True
|
2006-06-26 20:05:00 +00:00
|
|
|
if opt[0] in ["-h", "--help"]:
|
|
|
|
self.dispUsage()
|
2006-09-12 21:33:00 +00:00
|
|
|
sys.exit(0)
|
|
|
|
if opt[0] in ["-V", "--version"]:
|
|
|
|
self.dispVersion()
|
|
|
|
sys.exit(0)
|
2006-10-01 21:22:52 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
def start(self, argv):
|
|
|
|
# Command line options
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__argv = argv
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
# Reads the command line options.
|
|
|
|
try:
|
2006-10-04 22:17:53 +00:00
|
|
|
cmdOpts = 'bfs:xhV'
|
2006-09-12 21:33:00 +00:00
|
|
|
cmdLongOpts = ['help', 'version']
|
2006-09-19 20:38:32 +00:00
|
|
|
optList, args = getopt.getopt(self.__argv[1:], cmdOpts, cmdLongOpts)
|
2006-06-26 20:05:00 +00:00
|
|
|
except getopt.GetoptError:
|
|
|
|
self.dispUsage()
|
2006-11-19 21:26:47 +00:00
|
|
|
sys.exit(-1)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__getCmdLineOptions(optList)
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server = Server(self.__conf["background"])
|
2006-10-04 22:17:53 +00:00
|
|
|
self.__server.start(self.__conf["socket"], self.__conf["force"])
|
2006-08-23 19:53:09 +00:00
|
|
|
return True
|
2006-06-26 20:05:00 +00:00
|
|
|
except Exception, e:
|
|
|
|
print e
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.quit()
|
2006-08-23 19:53:09 +00:00
|
|
|
return False
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
server = Fail2banServer()
|
2006-08-23 19:53:09 +00:00
|
|
|
if server.start(sys.argv):
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
sys.exit(-1)
|