HEX
Server: Apache
System: Linux zacp120.webway.host 4.18.0-553.50.1.lve.el8.x86_64 #1 SMP Thu Apr 17 19:10:24 UTC 2025 x86_64
User: govancoz (1003)
PHP: 8.3.26
Disabled: exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Upload Files
File: //usr/lib/python3.6/site-packages/fail2ban/server/__pycache__/jail.cpython-36.pyc
3

UB(d1-�@sxdZdZdZddlZddlZddlZddlZddlmZddl	m
Z
mZmZm
Z
dd	lm
Z
e
e�ZGd
d�de�ZdS)z.Cyril Jaquier, Lee Clemens, Yaroslav HalchenkozPCopyright (c) 2004 Cyril Jaquier, 2011-2012 Lee Clemens, 2012 Yaroslav HalchenkoZGPL�N�)�Actions�)�	getLogger�_as_bool�extractOptions�MyTime)rc@seZdZdZddddgZd:dd	�Zd
d�Zdd
�Zdd�Zdd�Z	dd�Z
dd�Zedd��Z
edd��Zejdd��Zedd��Zedd��Zedd ��Zejd!d ��Zd;d#d$�Zed%d&��Zd'd(�Zd)d*�Zd+d,�Zd<d-d.�Zd/d0�Zd=d2d3�Zd4d5�Zd>d6d7�Zd8d9�ZdS)?�JailagFail2Ban jail, which manages a filter and associated actions.

	The class handles the initialisation of a filter, and actions. It's
	role is then to act as an interface between the filter and actions,
	passing bans detected by the filter, for the actions to then act upon.

	Parameters
	----------
	name : str
		Name assigned to the jail.
	backend : str
		Backend to be used for filter. "auto" will attempt to pick
		the most preferred backend method. Default: "auto"
	db : Fail2BanDb
		Fail2Ban persistent database instance. Default: `None`

	Attributes
	----------
	name
	database
	filter
	actions
	idle
	status
	Z	pyinotifyZgaminZpollingZsystemd�autoNcCsh||_t|�dkr tjd|�||_tj�|_d|_i|_	tj
d|j�|dk	r^|j|�||_
dS)N�z]Jail name %r might be too long and some commands might not function correctly. Please shortenzCreating new jail '%s')�	_Jail__db�len�logSys�warning�_Jail__name�queueZQueue�_Jail__queue�
_Jail__filter�	_banExtra�info�name�_setBackend�backend)�selfrrZdb�r�/usr/lib/python3.6/jail.py�__init__Gs

z
Jail.__init__cCsd|jj|jfS)Nz%s(%r))�	__class__�__name__r)rrrr�__repr__Xsz
Jail.__repr__cCs6t|�\}}|j�}|j}|dkr`||jkrNtjd||f�td||f��||j|�d�}x�|D]�}t|d|j��}yJ|f|�|dkr�||kr�tj	d||f�ntj
d|�t|�|_dSt
k
�r}z*tj|dkr�tjntjd||f�WYdd}~XqfXqfWtjd|j�td|j��dS)Nr
z.Unknown backend %s. Must be among %s or 'auto'z_init%sz9Could only initiated %r backend whenever %r was requestedzInitiated %r backendz)Backend %r failed to initialize due to %sz,Failed to initialize any backend for Jail %r)r�lower�	_BACKENDSr�error�
ValueError�index�getattr�
capitalizerrr�_Jail__actions�ImportError�log�logging�DEBUGZERRORr�RuntimeError)rrZbeArgsZbackends�bZ
initmethod�errrr[s8



$zJail._setBackendcKs2ddlm}tjd|j|f�||f|�|_dS)Nr)�
FilterPollzJail '%s' uses poller %r)Z
filterpollr/rrrr)r�kwargsr/rrr�_initPolling�szJail._initPollingcKs2ddlm}tjd|j|f�||f|�|_dS)Nr)�FilterGaminzJail '%s' uses Gamin %r)Zfiltergaminr2rrrr)rr0r2rrr�
_initGamin�szJail._initGamincKs2ddlm}tjd|j|f�||f|�|_dS)Nr)�FilterPyinotifyzJail '%s' uses pyinotify %r)Zfilterpyinotifyr4rrrr)rr0r4rrr�_initPyinotify�szJail._initPyinotifycKs2ddlm}tjd|j|f�||f|�|_dS)Nr)�
FilterSystemdzJail '%s' uses systemd %r)Z
filtersystemdr6rrrr)rr0r6rrr�_initSystemd�szJail._initSystemdcCs|jS)zName of jail.
		)r)rrrrr�sz	Jail.namecCs|jS)z;The database used to store persistent data for the jail.
		)r)rrrr�database�sz
Jail.databasecCs
||_dS)N)r)r�valuerrrr8�scCs|jS)z;The filter which the jail is using to monitor log files.
		)r)rrrr�filter�szJail.filtercCs|jS)z2Actions object used to manage actions for jail.
		)r')rrrr�actions�szJail.actionscCs|jjp|jjS)z-A boolean indicating whether jail is idle.
		)r:�idler;)rrrrr<�sz	Jail.idlecCs||j_||j_dS)N)r:r<r;)rr9rrrr<�s�basiccCs$d|jj|d�fd|jj|d�fgS)zThe status of the jail.
		�Filter)�flavorr)r:�statusr;)rr?rrrr@�szJail.statuscCs|jj�S)z-Retrieve whether queue has tickets to ban.
		)r�empty)rrrr�hasFailTickets�szJail.hasFailTicketscCs|jj|�dS)zQAdd a fail ticket to the jail.

		Used by filter to add a failure for banning.
		N)rZput)r�ticketrrr�
putFailTicket�szJail.putFailTicketcCs,y|jjd�}|Stjk
r&dSXdS)zTGet a fail ticket from the jail.

		Used by actions to get a failure for banning.
		FN)r�getrZEmpty)rrCrrr�
getFailTicket�s
zJail.getFailTicketcs�|j}|dkrd}|dk	r$|||<n||kr2||=tjd||�|dkrrt|�||<|j|�rr|jdkrrtjd�|dkr�|dk	r�tj|�||<|dks�|jd
d�dk�r�|d	kr�dd�|dk	r�|dkr�|j	d
�ngD�|d<|jdg��t
|jdd��}t���r|f�fdd�	}n&|jdd�}t|dd�}||fdd�}|jdd�dk	�rf|d�|f�fdd�	}|jdd�dk	�r�|d�|f�fdd�	}||d
<dS)N�zSet banTime.%s = %s�	incrementzDban time increment is not available as long jail database is not set�maxtime�rndtime�formula�factor�multipliers�	evformulacSsg|]}t|��qSr)�int)�.0�irrr�
<listcomp>�sz(Jail.setBanTimeExtra.<locals>.<listcomp>� Z
evmultipliers�1cs&|j|�|jt��kr|jndS)Nr���)�TimeZCountr
)�ban�	banFactor)rMrr�<lambda>�sz&Jail.setBanTimeExtra.<locals>.<lambda>z?ban.Time * (1<<(ban.Count if ban.Count<20 else 20)) * banFactorz~inline-conf-expr~�evalcSst|jt|��S)N)�maxrVrZ)rWrXrKrrrrYscst||���S)N)�min)rWrN)rIrrrYscs||�tj��S)N)�random)rWrN)rJrrrY	s)rIrJ)rKrLrIrJrM)
rrrrrEr8rrZstr2seconds�splitrZr
�compile)r�optr9ZberXrNrKr)rIrMrJr�setBanTimeExtra�s@

,
zJail.setBanTimeExtracCs|dk	r|jj|d�S|jS)N)rrE)rr`rrr�getBanTimeExtraszJail.getBanTimeExtracCs$|jjd�r|jjdd�S|jj�S)z)Returns max possible ban-time of jail.
		rHrIrrU)rrEr;�
getBanTime)rrrr�
getMaxBanTimeszJail.getMaxBanTimeTcCsP�y
|jdk	�r
|jjd�r.d}|r8|j�}n
|jj�}x�|jj||||jjj	d�D]�}yn|jj
|j�dd�rpwTd|_|j|�}t
j�|j�}|dkr�|dkr�||8}|dkr�|dkr�wT|j|�WqTtk
�r}z tjd|tj�tjkd	�WYdd}~XqTXqTWWn>tk
�rJ}z tjd
|tj�tjkd	�WYdd}~XnXdS)
z5Restore any previous valid bans from the database.
		NrH)Zjail�
forbantime�correctBanTimeZ
maxmatchesT)Z
log_ignorerrzRestore ticket failed: %s)�exc_infozRestore bans failed: %srUrU)r8rrErdr;rcZgetCurrentBansr:ZfailManagerZ
maxMatchesZinIgnoreIPListZgetIDZrestoredrZtimeZgetTimerD�	Exceptionrr"�getEffectiveLevelr*r+)rrfrerCZbtmZdiftmr.rrr�restoreCurrentBanss4


,zJail.restoreCurrentBanscCs<tjd|j�|jj�|jj�|j�tjd|j�dS)z�Start the jail, by starting filter and actions threads.

		Once stated, also queries the persistent database to reinstate
		any valid bans.
		zStarting jail %rzJail %r startedN)r�debugrr:�startr;rjr)rrrrrl=s


z
Jail.startcCs�|rtjd|j�xt|j|jfD]d}y|r2|j�|r>|j�Wq tk
r�}z&tjd||j|tj	�t
jkd�WYdd}~Xq Xq W|r�tjd|j�dS)z9Stop the jail, by stopping filter and actions threads.
		zStopping jail %rzStop %r of jail %r failed: %s)rgNzJail %r stopped)
rrkrr:r;�stop�joinrhr"rir*r+r)rrmrn�objr.rrrrmIs(z	Jail.stopcCs|jj�p|jj�S)z?Check jail "isAlive" by checking filter and actions threads.
		)r:�isAliver;)rrrrrp\szJail.isAlive)r
N)r=)N)T)TT)r�
__module__�__qualname__�__doc__r!rrrr1r3r5r7�propertyrr8�setterr:r;r<r@rBrDrFrarbrdrjrlrmrprrrrr	's6
&
	-

$
r	)�
__author__Z
__copyright__Z__license__r*Zmathr]rr;rZhelpersrrrrZmytimerr�objectr	rrrr�<module>s