2007-01-21 22:19:57 +00:00
|
|
|
#!/usr/bin/python
|
2011-10-07 19:47:50 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
|
|
|
|
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
|
2011-11-21 12:20:20 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier"
|
|
|
|
__license__ = "GPL"
|
|
|
|
|
2015-07-03 17:02:50 +00:00
|
|
|
import getopt
|
|
|
|
import os
|
|
|
|
import sys
|
2007-12-14 22:37:55 +00:00
|
|
|
|
2013-03-31 17:18:21 +00:00
|
|
|
from fail2ban.version import version
|
|
|
|
from fail2ban.server.server import Server
|
2014-06-10 19:36:19 +00:00
|
|
|
from fail2ban.helpers import getLogger
|
2007-12-14 22:11:12 +00:00
|
|
|
|
|
|
|
# Gets the instance of the logger.
|
2014-06-10 19:36:19 +00:00
|
|
|
logSys = getLogger("fail2ban")
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# \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:
|
2013-03-29 16:31:50 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
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
|
2007-12-14 21:19:00 +00:00
|
|
|
self.__conf["socket"] = "/var/run/fail2ban/fail2ban.sock"
|
2013-02-17 22:14:01 +00:00
|
|
|
self.__conf["pidfile"] = "/var/run/fail2ban/fail2ban.pid"
|
2013-03-29 16:31:50 +00:00
|
|
|
|
2006-09-12 21:33:00 +00:00
|
|
|
def dispVersion(self):
|
|
|
|
print "Fail2Ban v" + version
|
|
|
|
print
|
2012-11-09 13:58:19 +00:00
|
|
|
print "Copyright (c) 2004-2008 Cyril Jaquier, 2008- Fail2Ban Contributors"
|
2006-09-12 21:33:00 +00:00
|
|
|
print "Copyright of modifications held by their respective authors."
|
|
|
|
print "Licensed under the GNU General Public License v2 (GPL)."
|
|
|
|
print
|
2008-03-05 23:18:06 +00:00
|
|
|
print "Written by Cyril Jaquier <cyril.jaquier@fail2ban.org>."
|
2006-09-12 21:33:00 +00:00
|
|
|
print "Many contributions by Yaroslav O. Halchenko <debian@onerussian.com>."
|
2013-03-29 16:31:50 +00:00
|
|
|
|
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"
|
2013-02-17 22:14:01 +00:00
|
|
|
print " -p <FILE> pidfile 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
|
2012-07-19 18:51:21 +00:00
|
|
|
print "Report bugs to https://github.com/fail2ban/fail2ban/issues"
|
2013-03-29 16:31:50 +00:00
|
|
|
|
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]
|
2013-02-17 22:14:01 +00:00
|
|
|
if opt[0] == "-p":
|
|
|
|
self.__conf["pidfile"] = 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"]:
|
2013-02-24 10:04:36 +00:00
|
|
|
self.dispUsage()
|
2006-09-12 21:33:00 +00:00
|
|
|
sys.exit(0)
|
|
|
|
if opt[0] in ["-V", "--version"]:
|
2013-02-24 10:04:36 +00:00
|
|
|
self.dispVersion()
|
2006-09-12 21:33:00 +00:00
|
|
|
sys.exit(0)
|
2013-03-29 16:31:50 +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
|
2013-03-29 16:31:50 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# Reads the command line options.
|
|
|
|
try:
|
2013-02-17 22:14:01 +00:00
|
|
|
cmdOpts = 'bfs:p: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)
|
2013-03-29 16:31:50 +00:00
|
|
|
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__getCmdLineOptions(optList)
|
2013-03-29 16:31:50 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
try:
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server = Server(self.__conf["background"])
|
2013-02-17 22:14:01 +00:00
|
|
|
self.__server.start(self.__conf["socket"],
|
|
|
|
self.__conf["pidfile"],
|
|
|
|
self.__conf["force"])
|
2006-08-23 19:53:09 +00:00
|
|
|
return True
|
2006-06-26 20:05:00 +00:00
|
|
|
except Exception, e:
|
2007-12-14 22:11:12 +00:00
|
|
|
logSys.exception(e)
|
2006-09-19 20:38:32 +00:00
|
|
|
self.__server.quit()
|
2006-08-23 19:53:09 +00:00
|
|
|
return False
|
2013-03-29 16:31:50 +00:00
|
|
|
|
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)
|