https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /lib/python3/dist-packages/fail2ban/server/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/fail2ban/server/__pycache__/actions.cpython-310.pyc
o

;s*b\`�@sdZdZdZddlZddlZddlZddlZzddlmZWne	y-ddl
mZYnwzddl
mZWne	yAeZYnwddl
mZmZdd	lmZdd
lmZddlmZmZmZddlmZdd
lmZddlmZddlmZee �Z!Gdd�dee�Z"dS)z
Cyril Jaquierz Copyright (c) 2004 Cyril Jaquier�GPL�N)�Mapping)�OrderedDict�)�
BanManager�	BanTicket)�IPAddr)�
JailThread)�
ActionBase�
CommandAction�
CallingMap)�MyTime)�	Observers)�Utils�)�	getLoggerc@seZdZdZdd�Zedd��Zd?dd	�Zd@dd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�ZdAdd �Zd!d"�ZdBd#d$�ZdCd%d&�Zd'd(�ZGd)d*�d*e�Zd+d,�ZdDd.d/�ZdCd0d1�ZdEd2d3�Zd4d5�ZdCd6d7�ZdFd8d9�ZdEd:d;�Z dGd=d>�Z!dS)H�Actionsa�Handles jail actions.

	This class handles the actions of the jail. Creation, deletion or to
	actions must be done through this class. This class is based on the
	Mapping type, and the `add` method must be used to add new actions.
	This class also starts and stops the actions, and fetches bans from
	the jail executing these bans via the actions.

	Parameters
	----------
	jail: Jail
		The jail of which the actions belongs to.

	Attributes
	----------
	daemon
	ident
	name
	status
	active : bool
		Control the state of the thread.
	idle : bool
		Control the idle state of the thread.
	sleeptime : int
		The time the thread sleeps for in the loop.
	cCsLtj|d|jd�||_t�|_t�|_d|_d|_	d|_
|j
d|_dS)Nzf2b/a.)�namer�
r)r	�__init__r�_jailr�_actionsr�_Actions__banManager�banEpoch� _Actions__lastConsistencyCheckTM�
banPrecedence�
unbanMaxCount)�self�jail�r�9/usr/lib/python3/dist-packages/fail2ban/server/actions.pyrQszActions.__init__cCsDt�|�}t|d�std|��t|jt�s td||jjf��|S)N�Actionz&%s module does not have 'Action' classz0%s module %s does not implement required methods)r�load_python_module�hasattr�RuntimeError�
issubclassr!r
�__name__)�pythonModule�modrrr �_load_python_module_s

���zActions._load_python_moduleNFcCs�||jvr'|s
td|��|j|}t|d�r't|d�r%|��||j|<dS|dur2t|j|�}n|�|�}|j|j|fi|��}||j|<dS)a�Adds a new action.

		Add a new action if not already present, defaulting to standard
		`CommandAction`, or specified Python module.

		Parameters
		----------
		name : str
			The name of the action.
		pythonModule : str, optional
			Path to Python file which must contain `Action` class.
			Default None, which means `CommandAction` is used.
		initOpts : dict, optional
			Options for Python Action, used as keyword arguments for
			initialisation. Default None.

		Raises
		------
		ValueError
			If action name already exists.
		RuntimeError
			If external Python module does not have `Action` class
			or does not implement necessary methods as per `ActionBase`
			abstract class.
		zAction %s already exists�reload�clearAllParamsN)	r�
ValueErrorr#r+�_reload_actionsrrr)r!)rrr'�initOptsr*�action�customActionModulerrr �addls





zActions.addTcs�|rt��_dSt�d�rR�j��D]\}}|�jvr*�j|jd	i|r&|ni��qt�fdd��j��D��}t|�rK�jd|dd��j	|d�t
�d�dSdS)
z@ Begin or end of reloading resp. refreshing of all parameters
		r-c3s&�|]\}}|�jvr||fVqdS�N)r-��.0rr/�rrr �	<genexpr>�s�

��z!Actions.reload.<locals>.<genexpr>FT)�db�actions�stop�r8Nr)�dictr-r#�itemsrr*r�len�_Actions__flushBan�stopActions�delattr)r�beginrr.�delactsrr5r r*�s

��zActions.reloadcCs(z|j|WStytd|��w�NzInvalid Action name: %s�r�KeyError�rrrrr �__getitem__�s
�zActions.__getitem__cCs*z|j|=WdStytd|��wrCrDrFrrr �__delitem__�s
�zActions.__delitem__cC�
t|j�Sr2)�iterrr5rrr �__iter__��
zActions.__iter__cCrIr2)r=rr5rrr �__len__�rLzActions.__len__cC�dS�NFr)r�otherrrr �__eq__�szActions.__eq__cCst|�Sr2)�idr5rrr �__hash__�szActions.__hash__cCs(t�|�}|j�|�t�d|�dS)Nz
  banTime: %s)r
�str2secondsr�
setBanTime�logSys�info)r�valuerrr rU�s
zActions.setBanTimecC�
|j��Sr2)r�
getBanTimer5rrr rZ�rLzActions.getBanTimecsD|j���|s	�St|�dkr|d�vrdSdS�fdd�|D�S)Nrrcsg|]
}|�vr
dnd�qS)rrr�r4�ip��lstrr �
<listcomp>�sz%Actions.getBanned.<locals>.<listcomp>)r�
getBanListr=)r�idsrr]r �	getBanned�s
zActions.getBannedcCs|jjd|d�S)zkReturns the list of banned IP addresses.

		Returns
		-------
		list
			The list of banned IP addresses.
		T)�ordered�withTime)rr`)rrdrrr r`�szActions.getBanListcs<t���t|t�r�fdd�|D�}nt|��f}|�|�S)zBan an IP or list of IPs.c3s�|]}t|��VqdSr2)rr[��unixTimerr r6�s�z&Actions.addBannedIP.<locals>.<genexpr>)r
�time�
isinstance�listr�_Actions__checkBan)rr\�ticketsrrer �addBannedIP�s


zActions.addBannedIPc	Cs&|dur	|�|�St|ttf�r>g}d}|D]}z||�|||�7}Wqty3|s1|�|�Yqw|r<td|��|S|rO|jjdurO|jj�	|j|�|j
�|�}|dur`|�|�dSt|t
�s�t
|�}|js�tt|j|j
����}	|	r�|�|	||�Sd|}
t�tj|
�|r�dSt|
��)aORemoves banned IP calling actions' unban method

		Remove a banned IP now, rather than waiting for it to expire,
		even if set to never expire.

		Parameters
		----------
		ip : list, str, IPAddr or None
			The IP address (or multiple IPs as list) to unban or all IPs if None

		Raises
		------
		ValueError
			If `ip` is not banned
		Nrznot banned: %rz%s is not bannedr)r>rhri�tuple�removeBannedIPr,�appendr�database�delBanr�
getTicketByID�_Actions__unBanr�isSingle�filter�containsr`rV�log�logging�MSG)rr\r7�ifexists�missed�cnt�i�ticket�ipa�ips�msgrrr rn�sB

��

�zActions.removeBannedIPcCs�|dur|j}t|���}|��|D];\}}z|��Wn#ty@}ztjd|jj	||t�
�tjkd�WYd}~nd}~ww|j|=t�
d|jj	|�qdS)z>Stops the actions in reverse sequence (optionally filtered)
		Nz(Failed to stop jail '%s' action '%s': %s��exc_infoz%s: action %s terminated)rrir<�reverser9�	ExceptionrV�errorrr�getEffectiveLevelrx�DEBUG�debug)rr8�
revactionsrr/�errr r?2s"
����zActions.stopActionscs�d}�j��D]-\}}z|��Wqty4}ztjd�jj||t��t	j
kd�WYd}~qd}~ww�jr�z{�jrVt�
d�t��fdd�dd��j�t�
d	�Wq5d}t�j�jjt���}t�d
d|�j�t��fdd�|�r���}||7}|r�|�jkr��jr�|d
9}t�d
d|r�|�jkr�|n�j|�j���|r�|�jkr�|n�j�d}Wn"ty�}ztjd�jj|t��t	j
kd�WYd}~nd}~ww�js8�jdd����dS)z�Main loop for Threading.

		This function is the main loop of the thread. It checks the jail
		queue and executes commands when an IP address is banned.

		Returns
		-------
		bool
			True when the thread exits nicely.
		rz)Failed to start jail '%s' action '%s': %sr�NzActions: enter idle modecs�jp�jSr2)�active�idlerr5rr �<lambda>[�zActions.run.<locals>.<lambda>cSrNrOrrrrr r�\szActions: leave idle mode�z1Actions: wait for pending tickets %s (default %s)cs�jp�jjSr2)r�r�hasFailTicketsrr5rr r�cr�rz+Actions: check-unban %s, bancnt %s, max: %sz*[%s] unhandled error in actions thread: %sT)r9)rr<�startr�rVr�rrr�rxr�r�r�r�r�wait_for�	sleeptime�minr�_nextUnbanTimer
rgrwrjrr�_Actions__checkUnBanr>r?)rr|rr/r��bancnt�wtrr5r �runDsX
���

�
(�����zActions.runc@s�eZdZdZiddd��ddd��ddd��d	d
d��ddd��d
dd��ddd��ddd��ddd��ddd��ddd��dd1dd��ddd��ddd��d d!d��d"d#d��d$d%d��Zejd&Zdd'efd(d)�Zd*d+�Zd,d-�Z	d2d/d0�Z
dS)3zActions.ActionInfo)�fid�
raw-ticketr\cCrYr2)�_ActionInfo__ticket�getIPr5rrr r�|�
zActions.ActionInfo.<lambda>�familycCs
|djS�Nr\)�	familyStrr5rrr r�}r�zip-revcCs|d�d�S)Nr\�)�getPTRr5rrr r�~�zip-hostcCs|d��Sr�)�getHostr5rrr r��r�cCrYr2)r��getIDr5rrr r��r��failurescCrYr2)r��
getAttemptr5rrr r��r�rgcCrYr2)r��getTimer5rrr r��r��bantimecCs|��Sr2)�_getBanTimer5rrr r��s�bancountcCrYr2)r��getBanCountr5rrr r��r��matchescCsd�|j���S�N�
)�joinr��
getMatchesr5rrr r��r��restoredcCs|jjrdSdS)Nrr)r�r�r5rrr r��r�zF-*NcCs|j�|�Sr2)r��getData)r�tagrrr r��r��	ipmatchescCsd�|�d����S)Nr�T�r��_mi4ipr�r5rrr r��s�
ipjailmatchescCsd�|�����Sr�r�r5rrr r��s�
ipfailurescCs|�d���S�NT�r�r�r5rrr r��r��ipjailfailurescCs|����Sr2r�r5rrr r��r�r�cCrIr2)�reprr�r5rrr r��r�)�__ticket�__jail�__mi4ipTcCs$||_||_t�|_||_||_dSr2)r��_ActionInfo__jailr;�storage�	immutable�data)rr~rr�r�rrr r�s

zActions.ActionInfo.__init__cCs|�|j|j|j|j���Sr2)�	__class__r�r�r�r��copyr5rrr r��szActions.ActionInfo.copycCs&|j��}|dur|jj��}t|�Sr2)r�rZr�r8�int)r�btimerrr r��s
zActions.ActionInfo._getBanTimeFc
Cs�t|d�si|_|j}|rdnd}||vr"||dur||S|jSz*|j}|d}d||<|js5|jWS|rA|jj|d�||<n
|jj||d�||<Wn"tyn}ztjd||j	|t�
�tjkd	�WYd}~nd}~ww||dury||S|jS)
a�Gets bans merged once, a helper for lambda(s), prevents stop of executing action by any exception inside.

			This function never returns None for ainfo lambdas - always a ticket (merged or single one)
			and prevents any errors through merging (to guarantee ban actions will be executed).
			[TODO] move merging to observer - here we could wait for merge and read already merged info from a database

			Parameters
			----------
			overalljails : bool
				switch to get a merged bans :
				False - (default) bans merged for current jail only
				True - bans merged for all jails of current ip address

			Returns
			-------
			BanTicket 
				merged or self ticket only
			r��allrNr\)r\)r\rz+Failed to get %s bans merged, jail '%s': %sr�)
r#�_ActionInfo__mi4ipr�r�rp�
getBansMergedr�rVr�rr�rxr�)r�overalljails�mi�idxrr\r�rrr r��s2
����zActions.ActionInfo._mi4ipr2�F)r&�
__module__�__qualname__�
CM_REPR_ITEMS�AI_DICTr�	__slots__rr�r�r�rrrr �
ActionInfowsT��������	�
��
������
r�cCs$|s	tdt���}t�||j�}|S)Nr�)rr
rgrr�r)rr~�aInforrr �_getActionInfo�szActions._getActionInfo�dccs>�d}||kr|j��}|sdS|V|d7}||ksdSdS)zAGenerator to get maximal count failure tickets from fail-manager.rrN)r�
getFailTicket)r�countr|r~rrr �__getFailTickets�s�
�zActions.__getFailTicketsc
s�d}|s
|�|j�}d}|D�]*}t�|��|�|j���}���}|���}i}|jj�|d�r�|d7}t	j
durG�jsGt	j
�d�|j
|�t�d|j
j�jsRdnd|�|j��D]A\}	}
z�jrkt|
d	d
�rkWq[|jsr|��|
�|�Wq[ty�}ztjd|j
j|	||t��tjkd�WYd}~q[d}~wwd
�_|jr�|j�_q|�dd�r�t�d|j
j|�q|�d����j�r2|� ��� �}|dkr�tjn	|dkr�tj!ntj"}
t�#|
d|j
j|��j|jk�r|dk�r|�st$�%�|j&dk�r|j�'�D]}
|
�(��qt$�%�|_&�j|jk�r1|�s(t)�fdd�|j��D��}||j*�|d�7}q||�*��7}q|�rNt�+d||j�,�|j�-�|j
j�|S)aCheck for IP address to ban.

		If tickets are not specified look in the jail queue for FailTicket. If a ticket is available,
		it executes the "ban" command and adds a ticket to the BanManager.

		Returns
		-------
		bool
			True if an IP address get banned.
		rN)�reasonr�banFoundz
[%s] %sBan %sr�zRestore �
norestoredF�9Failed to execute ban jail '%s' action '%s' info '%r': %sr�T�expiredz[%s] Ignore %s, expired bantimer~��<z[%s] %s already bannedc3s(�|]\}}|j�jkr||fVqdSr2)rr3��bTicketrr r6&s���z%Actions.__checkBan.<locals>.<genexpr>r:z"Banned %s / %s, %s ticket(s) in %r).�_Actions__getFailTicketsrr�wraprZrr�r��addBanTicketr�Mainr�r1rrV�noticerrr<�getattrr��reset�banr�r�r�rxr��bannedr�getrWr��NOTICE�WARNINGrwr
rgr�values�consistencyCheckr;�_Actions__reBanr��getBanTotal�size)rrkr|�	rebanactsr~r�r\r�r�rr/r��diftm�llrr�r �
__checkBan�sz


�����
���zActions.__checkBanc	Cs�|p|j}|��}|�|�}|r,t�d|jj|dt|�dkr)dt|�	��dnd�|�
�D]B\}}zt�d|jj||�|jsF|�
�|�|�Wq0tyr}ztjd|jj|||t��tjkd	�WYd
}~dSd
}~wwd|_|jr}|j|_dS)z�Repeat bans for the ticket.

		Executes the actions in order to reban the host given in the
		ticket.

		Parameters
		----------
		ticket : Ticket
			Ticket to reban
		z[%s] Reban %s%sr\rz, action %rrr�z[%s] action %r: reban %sz;Failed to execute reban jail '%s' action '%s' info '%r': %sr�NT)rr�r�rVr�rrr=ri�keysr<r�r�r��rebanr�r�r�rxr�r�r)	rr~r8rwr\r�rr/r�rrr �__reBan3s.

8���zActions.__reBancCs�|j�|�sdSd}|j��D]O\}}z'|jrt|dd�rWq|js$Wq|dur-|�|�}|js4|�	�|�
|�Wqty^}ztj
d|jj|||t��tjkd�WYd}~qd}~wwdS)Nr�Fr�r�)r�
_inBanListrr<r�r��_prolongabler�r�r��prolongr�rVr�rrr�rxr�)rr~r�rr/r�rrr �_prolongBanUs*
����zActions._prolongBancCsP|j�t��|�}|D]}|�|�qt|�}|r&t�d||j��|j	j
�|S)zKCheck for IP address to unban.

		Unban IP addresses which are outdated.
		zUnbanned %s, %s ticket(s) in %r)r�	unBanListr
rgrsr=rVr�r�rr)r�maxCountr^r~r|rrr �__checkUnBanls�zActions.__checkUnBancs�d}|durt�d��j��}nd}t�j�}d}i}|dur"|n�j��D]l\}�zt�d�rHt�t	�r9�j
rHt�d�jj
|����rHWq'Wn@ty�}	z4tjd�jj
||	t��tjkd	�t�d
�t�d�r���fdd
�}
��|
�WYd}	~	q'WYd}	~	nd}	~	wwt�d��||<q'|}|r��jjdur�t�d��jj��j�|D]}�j|||d�|d7}q�t�d|�j���jj
�|S)z�Flush the ban list.

		Unban all IP address which are still in the banning list.

		If actions specified, don't flush list - just execute unban for 
		given actions (reload, obsolete resp. removed actions).
		TNz  Flush ban listFr�flushz[%s] Flush ticket(s) with %sz1Failed to flush bans in jail '%s' action '%s': %sr�z'No flush occurred, do consistency checkr�cs$�rt�dd�s�j�d�dSdS)N�actionrepair_on_unbanz,Invariant check failed. Flush is impossible.FT)r��_logSysr�r�r/rr9rr �
_beforeRepair�sz)Actions.__flushBan.<locals>._beforeRepairz   Unban tickets each individualyz  Flush jail in database)r8rwrz!  Unbanned %s, %s ticket(s) in %r)rVr�r�flushBanListrJrr<r#rhr�actionflushr�rrrr�r�r�rxr�rWr�rprqrsr�)rr7r8r9rwr^r|�
unbactionsrr�rr~rr
r �
__flushBanzsR

�
�


���



�zActions.__flushBanc
Cs�|dur|j}n|}|��}|�|�}|r t�d|jj|d�|��D]@\}}zt�d|jj||�|j	s:|�
�|�|�Wq$tyd}	ztj
d|jj|||	t��tjkd�WYd}	~	q$d}	~	wwdS)z�Unbans host corresponding to the ticket.

		Executes the actions in order to unban the host given in the
		ticket.

		Parameters
		----------
		ticket : FailTicket
			Ticket of failures of which to unban
		Nz
[%s] Unban %sr\z[%s] action %r: unban %sz;Failed to execute unban jail '%s' action '%s' info '%r': %sr�)rr�r�rVr�rrr<r�r�r��unbanr�r�r�rxr�)
rr~r8rwrr\r�rr/r�rrr �__unBan�s*
����zActions.__unBan�basiccCs�gd�}|dus||vrt�d||f�|dkr#|j��}t|�}n|j��}d|fd|j��fg}|dkr>|d|fg7}|dkr`|j��}|d	|j�|�fd
|j�	|�fd|j�
|�fg7}|S)zEStatus of current and total ban counts and current banned IP list.
		)�shortr�cymruNz9Unsupported extended jail status flavor %r. Supported: %srzCurrently bannedzTotal bannedzBanned IP listrzBanned ASN listzBanned Country listzBanned RIR list)rV�warningrr`r=r�r��getBanListExtendedCymruInfo�geBanListExtendedASN�geBanListExtendedCountry�geBanListExtendedRIR)r�flavor�supported_flavorsr�r|�ret�
cymru_inforrr �status�s*


�
��zActions.status)NNF)Tr�)NTFr2)r�r�)FNF)r)"r&r�r��__doc__r�staticmethodr)r1r*rGrHrKrMrQrSrUrZrbr`rlrnr?r�rr�r�r�rjr�rr�r>rsrrrrr r5s>


.




:3X



T"


6r)#�
__author__�
__copyright__�__license__rx�os�sysrg�collections.abcr�ImportError�collectionsrr;�
banmanagerrr�ipdnsr�
jailthreadr	r/r
rr�mytimer
�observerr�utilsr�helpersrr&rVrrrrr �<module>s6��

https://t.me/RX1948 - 2025