fail2ban.server.database module

class fail2ban.server.database.Fail2BanDb(filename, purgeAge=86400)

Bases: object

Fail2Ban database for storing persistent data.

This allows after Fail2Ban is restarted to reinstated bans and to continue monitoring logs from the same point.

This will either create a new Fail2Ban database, connect to an existing, and if applicable upgrade the schema in the process.

Parameters:

filename : str

File name for SQLite3 database, which will be created if doesn’t already exist.

purgeAge : int

Purge age in seconds, used to remove old bans from database during purge.

Raises:

sqlite3.OperationalError

Error connecting/creating a SQLite3 database.

RuntimeError

If exisiting database fails to update to new schema.

Attributes

filename File name of SQLite3 database file.
purgeage Purge age in seconds.

Methods

addBan(*args, **kwargs) Add a ban to the database.
addJail(*args, **kwargs) Adds a jail to the database.
addLog(*args, **kwargs) Adds a log to the database.
createDb(*args, **kwargs) Creates a new database, called during initialisation.
delAllJails(*args, **kwargs) Deletes all jails from the database.
delJail(*args, **kwargs) Deletes a jail from the database.
getBans(**kwargs) Get bans from the database.
getBansMerged([ip, jail, bantime]) Get bans from the database, merged into single ticket.
getJailNames(*args, **kwargs) Get name of jails in database.
getLogPaths(*args, **kwargs) Gets all the log paths from the database.
purge(*args, **kwargs) Purge old bans, jails and log files from database.
updateDb(*args, **kwargs) Update an existing database, called during initialisation.
updateLog(*args, **kwargs) Updates hash and last position in log file.
addBan(*args, **kwargs)

Add a ban to the database.

Parameters:

jail : Jail

Jail in which the ban has occurred.

ticket : BanTicket

Ticket of the ban to be added.

addJail(*args, **kwargs)

Adds a jail to the database.

Parameters:

jail : Jail

Jail to be added to the database.

addLog(*args, **kwargs)

Adds a log to the database.

Parameters:

jail : Jail

Jail that log is being monitored by.

container : FileContainer

File container of the log file being added.

Returns:

int

If log was already present in database, value of last position in the log file; else None

createDb(*args, **kwargs)

Creates a new database, called during initialisation.

delAllJails(*args, **kwargs)

Deletes all jails from the database.

delJail(*args, **kwargs)

Deletes a jail from the database.

Parameters:

jail : Jail

Jail to be removed from the database.

filename

File name of SQLite3 database file.

getBans(**kwargs)

Get bans from the database.

Parameters:

jail : Jail

Jail that the ban belongs to. Default None; all jails.

bantime : int

Ban time in seconds, such that bans returned would still be valid now. Negative values are equivalent to None. Default None; no limit.

ip : str

IP Address to filter bans by. Default None; all IPs.

Returns:

list

List of `Ticket`s for bans stored in database.

getBansMerged(ip=None, jail=None, bantime=None)

Get bans from the database, merged into single ticket.

This is the same as getBans, but bans merged into single ticket.

Parameters:

jail : Jail

Jail that the ban belongs to. Default None; all jails.

bantime : int

Ban time in seconds, such that bans returned would still be valid now. Negative values are equivalent to None. Default None; no limit.

ip : str

IP Address to filter bans by. Default None; all IPs.

Returns:

list or Ticket

Single ticket representing bans stored in database per IP in a list. When ip argument passed, a single Ticket is returned.

getJailNames(*args, **kwargs)

Get name of jails in database.

Currently only used for testing purposes.

Returns:

set

Set of jail names.

getLogPaths(*args, **kwargs)

Gets all the log paths from the database.

Currently only for testing purposes.

Parameters:

jail : Jail

If specified, will only reutrn logs belonging to the jail.

Returns:

set

Set of log paths.

purge(*args, **kwargs)

Purge old bans, jails and log files from database.

purgeage

Purge age in seconds.

updateDb(*args, **kwargs)

Update an existing database, called during initialisation.

A timestamped backup is also created prior to attempting the update.

updateLog(*args, **kwargs)

Updates hash and last position in log file.

Parameters:

jail : Jail

Jail of which the log file belongs to.

container : FileContainer

File container of the log file being updated.

fail2ban.server.database.commitandrollback(f)