mirror of https://github.com/fail2ban/fail2ban
Merge branch '0.10' into 0.11
# Conflicts: # fail2ban/server/database.pypull/2019/merge
commit
a83a6c38e4
|
@ -505,12 +505,12 @@ class Actions(JailThread, Mapping):
|
||||||
else:
|
else:
|
||||||
unbactions[name] = action
|
unbactions[name] = action
|
||||||
actions = unbactions
|
actions = unbactions
|
||||||
|
# flush the database also:
|
||||||
|
if db and self._jail.database is not None:
|
||||||
|
logSys.debug(" Flush jail in database")
|
||||||
|
self._jail.database.delBan(self._jail)
|
||||||
# unban each ticket with non-flasheable actions:
|
# unban each ticket with non-flasheable actions:
|
||||||
for ticket in lst:
|
for ticket in lst:
|
||||||
# delete ip from database also:
|
|
||||||
if db and self._jail.database is not None:
|
|
||||||
ip = str(ticket.getIP())
|
|
||||||
self._jail.database.delBan(self._jail, ip)
|
|
||||||
# unban ip:
|
# unban ip:
|
||||||
self.__unBan(ticket, actions=actions, log=log)
|
self.__unBan(ticket, actions=actions, log=log)
|
||||||
cnt += 1
|
cnt += 1
|
||||||
|
|
|
@ -566,23 +566,30 @@ class Fail2BanDb(object):
|
||||||
ticket.getData()))
|
ticket.getData()))
|
||||||
|
|
||||||
@commitandrollback
|
@commitandrollback
|
||||||
def delBan(self, cur, jail, ip):
|
def delBan(self, cur, jail, *args):
|
||||||
"""Delete a ban from the database.
|
"""Delete a single or multiple tickets from the database.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
jail : Jail
|
jail : Jail
|
||||||
Jail in which the ban has occurred.
|
Jail in which the ticket(s) should be removed.
|
||||||
ip : str
|
args : list of IP
|
||||||
IP to be removed.
|
IPs to be removed, if not given all tickets of jail will be removed.
|
||||||
"""
|
"""
|
||||||
queryArgs = (jail.name, str(ip));
|
query1 = "DELETE FROM bips WHERE jail = ?"
|
||||||
cur.execute(
|
query2 = "DELETE FROM bans WHERE jail = ?"
|
||||||
"DELETE FROM bips WHERE jail = ? AND ip = ?",
|
queryArgs = [jail.name];
|
||||||
queryArgs)
|
if not len(args):
|
||||||
cur.execute(
|
cur.execute(query1, queryArgs);
|
||||||
"DELETE FROM bans WHERE jail = ? AND ip = ?",
|
cur.execute(query2, queryArgs);
|
||||||
queryArgs);
|
return
|
||||||
|
query1 += " AND ip = ?"
|
||||||
|
query2 += " AND ip = ?"
|
||||||
|
queryArgs.append('');
|
||||||
|
for ip in args:
|
||||||
|
queryArgs[1] = str(ip);
|
||||||
|
cur.execute(query1, queryArgs);
|
||||||
|
cur.execute(query2, queryArgs);
|
||||||
|
|
||||||
@commitandrollback
|
@commitandrollback
|
||||||
def _getBans(self, cur, jail=None, bantime=None, ip=None):
|
def _getBans(self, cur, jail=None, bantime=None, ip=None):
|
||||||
|
|
|
@ -270,9 +270,10 @@ class DatabaseTest(LogCaptureTestCase):
|
||||||
ticket = FailTicket("127.0.0.1", 0, ["abc\n"])
|
ticket = FailTicket("127.0.0.1", 0, ["abc\n"])
|
||||||
self.db.addBan(self.jail, ticket)
|
self.db.addBan(self.jail, ticket)
|
||||||
|
|
||||||
self.assertEqual(len(self.db.getBans(jail=self.jail)), 1)
|
tickets = self.db.getBans(jail=self.jail)
|
||||||
|
self.assertEqual(len(tickets), 1)
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
isinstance(self.db.getBans(jail=self.jail)[0], FailTicket))
|
isinstance(tickets[0], FailTicket))
|
||||||
|
|
||||||
def testAddBanInvalidEncoded(self):
|
def testAddBanInvalidEncoded(self):
|
||||||
if Fail2BanDb is None: # pragma: no cover
|
if Fail2BanDb is None: # pragma: no cover
|
||||||
|
@ -305,10 +306,28 @@ class DatabaseTest(LogCaptureTestCase):
|
||||||
or readtickets[2] == tickets[2]
|
or readtickets[2] == tickets[2]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _testAdd3Bans(self):
|
||||||
|
self.testAddJail()
|
||||||
|
for i in (1, 2, 3):
|
||||||
|
ticket = FailTicket(("192.0.2.%d" % i), 0, ["test\n"])
|
||||||
|
self.db.addBan(self.jail, ticket)
|
||||||
|
tickets = self.db.getBans(jail=self.jail)
|
||||||
|
self.assertEqual(len(tickets), 3)
|
||||||
|
return tickets
|
||||||
|
|
||||||
def testDelBan(self):
|
def testDelBan(self):
|
||||||
self.testAddBan()
|
tickets = self._testAdd3Bans()
|
||||||
ticket = self.db.getBans(jail=self.jail)[0]
|
# delete single IP:
|
||||||
self.db.delBan(self.jail, ticket.getIP())
|
self.db.delBan(self.jail, tickets[0].getIP())
|
||||||
|
self.assertEqual(len(self.db.getBans(jail=self.jail)), 2)
|
||||||
|
# delete two IPs:
|
||||||
|
self.db.delBan(self.jail, tickets[1].getIP(), tickets[2].getIP())
|
||||||
|
self.assertEqual(len(self.db.getBans(jail=self.jail)), 0)
|
||||||
|
|
||||||
|
def testFlushBans(self):
|
||||||
|
self._testAdd3Bans()
|
||||||
|
# flush all bans:
|
||||||
|
self.db.delBan(self.jail)
|
||||||
self.assertEqual(len(self.db.getBans(jail=self.jail)), 0)
|
self.assertEqual(len(self.db.getBans(jail=self.jail)), 0)
|
||||||
|
|
||||||
def testGetBansWithTime(self):
|
def testGetBansWithTime(self):
|
||||||
|
|
Loading…
Reference in New Issue