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__/action.cpython-310.pyc
o

;s*b�v�@stdZdZdZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZzddl
mZWneyCddlmZYnwddlmZdd	lmZdd
lmZddlmZdd
lmZmZmZmZmZmZm Z ee!�Z"e	�#�Z$ej%Z&e�'d�Z(dZ)e�'d�Z*dd�dd�d�Z+ddd�Z,e,�-e+�Gdd�dee.�Z/Gdd�de.ed�Z0ddd�Z1Gd d!�d!e0�Z2dS)"z'Cyril Jaquier and Fail2Ban Contributorsz>Copyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav Halchenko�GPL�N)�ABCMeta)�MutableMapping�)�
mapTag2Opt)�DNSUtils)�MyTime)�Utils�)�	getLogger�_merge_copy_dicts�
splitwords�substituteRecursiveTags�
uni_string�TAG_CRE�MAX_TAG_REPLACE_COUNTz<F-([A-Z0-9_\-]+)>)�inet4�inet6z^(\w+)\?(family)=(.*)$cC�ttjdd��S)NT��fqdn��strr�getHostname�rr�8/usr/lib/python3/dist-packages/fail2ban/server/action.py�<lambda>?�rcCr)NFrrrrrrr@r)zfq-hostnamezsh-hostname�
� )�br�spc@s�eZdZdZdZdZdd�Zddd�Zd d
d�ZeZ	d!d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�ZdS)"�
CallingMapa"A Mapping type which returns the result of callable values.

	`CallingMap` behaves similar to a standard python dictionary,
	with the exception that any values which are callable, are called
	and the result is returned as the value.
	No error handling is in place, such that any errors raised in the
	callable will raised as usual.
	Actual dictionary is stored in property `data`, and can be accessed
	to obtain original callable values.

	Attributes
	----------
	data : dict
		The dictionary data which can be accessed to obtain items uncalled
	r)�data�storage�	immutable�
__org_datacOs"t�|_d|_t|i|��|_dS)NT)�dictr$r%r#)�self�args�kwargsrrr�__init___szCallingMap.__init__TcCs2t�|_z|j|_Wn	tyYnw||_dS�N)r'r$�_CallingMap__org_datar#�AttributeErrorr%)r(r%rrr�resetds�
zCallingMap.resetFcCsd|jj|�|t�fS)Nz%s(%r))�	__class__�__name__�_asdictr)r(�
calculatedrrr�_asreprlszCallingMap._asreprNcs�t�jfi�j��}|st�fdd�|��D��St|���D]\}}t|�r>z��|�}|r3||�|||<WqYqq|S)Nc3s.�|]\}}t|�r|�jvr||fVqdSr,)�callable�
CM_REPR_ITEMS)�.0�n�v�r(rr�	<genexpr>us�
�
��z%CallingMap._asdict.<locals>.<genexpr>)r'r#r$�items�listr5�__getitem__)r(r3�checker�dr8r9rr:rr2rs
�
zCallingMap._asdictcCs0z|j|}W|Sty|j|}Y|Swr,)r$�KeyErrorr#�r(�key�valuerrr�
getRawItem�s��zCallingMap.getRawItemcCsbz|j|}Wnty|j|}Ynwt|�r/t|d�r'|jjr'||�n|�}||j|<|S)N�__code__)r$rAr#r5�hasattrrF�co_argcountrBrrrr>�s� 
zCallingMap.__getitem__cCs:|jr|j��|_|j|_|j��|_d|_||j|<dS�NF)r%r$�copyr#r-rBrrr�__setitem__�szCallingMap.__setitem__cCstd|��)NzKey %r was deleted)rA�r(rCrrr�
__unavailable�szCallingMap.__unavailablecCsX|jr|j��|_|j|_|j��|_d|_z|j|=Wn	ty%Ynw|j|=dSrI)r%r$rJr#r-rArLrrr�__delitem__�s�zCallingMap.__delitem__cC�
t|j�Sr,)�iterr#r:rrr�__iter__��
zCallingMap.__iter__cCrOr,)�lenr#r:rrr�__len__�rRzCallingMap.__len__cCs|�t|j|j��Sr,)r0rr#r$r:rrrrJ�szCallingMap.copy)T)F)FN)r1�
__module__�__qualname__�__doc__r6�	__slots__r+r/r4�__repr__r2rEr>rK�_CallingMap__unavailablerNrQrTrJrrrrr"Js"


	
r"c@sXeZdZdZedd��Zdd�Zdd�Zdd	�Zd
d�Z	dd
�Z
edd��Zdd�Z
dS)�
ActionBasea�An abstract base class for actions in Fail2Ban.

	Action Base is a base definition of what methods need to be in
	place to create a Python based action for Fail2Ban. This class can
	be inherited from to ease implementation.
	Required methods:

	- __init__(jail, name)
	- start()
	- stop()
	- ban(aInfo)
	- unban(aInfo)

	Called when action is created, but before the jail/actions is
	started. This should carry out necessary methods to initialise
	the action but not "start" the action.

	Parameters
	----------
	jail : Jail
		The jail in which the action belongs to.
	name : str
		Name assigned to the action.

	Notes
	-----
	Any additional arguments specified in `jail.conf` or passed
	via `fail2ban-client` will be passed as keyword arguments.
	cCs(d}|D]
}tt||d��sdSqdS)N)�start�stop�ban�reban�unbanFT)r5�getattr)�cls�C�required�methodrrr�__subclasshook__�s�zActionBase.__subclasshook__cCs"||_||_td|jj�|_dS)Nzfail2ban.%s)�_jail�_namerr0r1�_logSys�r(�jail�namerrrr+�szActionBase.__init__cC�dS)z,Executed when the jail/action is started.
		Nrr:rrrr\��zActionBase.startcCrm)z,Executed when the jail/action is stopped.
		Nrr:rrrr]�rnzActionBase.stopcCrm)��Executed when a ban occurs.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		Nr�r(�aInforrrr^��	zActionBase.bancCs
|�|�S)ro)r^rprrrr_s
	zActionBase.rebancCrmrIrr:rrr�_prolongable�zActionBase._prolongablecCrm)z�Executed when a ban expires.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		Nrrprrrr`rrzActionBase.unbanN)r1rUrVrW�classmethodrfr+r\r]r^r_�propertyrsr`rrrrr[�s


r[)�	metaclass�str2seconds�ignore)�timeout�bantimecs�eZdZdZed�Zdd�Z�fdd�Zedd��Z	d	d
�Z
e
Zdd�Ze
d
d��Ze
dd��Zdd�Zdd�Zgdfdd�Ze
dd��Ze
dd��Ze
dd��Zdd�ZdNd!d"�ZdOd$d%�Ze
d&d'��Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�ZdPd2d3�Zd4d5�Z dPd6d7�Z!e"�#d8�Z$ed9d:��Z%edQd<d=��Z&e"�#d>�Z$e"�#d?�Z'edPd@dA��Z(e
dBdC��Z)dDdE�Z*dRdGdH�Z+dPdIdJ�Z,e-dSdLdM��Z.�Z/S)T�
CommandActiona�A action which executes OS shell commands.

	This is the default type of action which Fail2Ban uses.

	Default sets all commands for actions as empty string, such
	no command is executed.

	Parameters
	----------
	jail : Jail
		The jail in which the action belongs to.
	name : str
		Name assigned to the action.

	Attributes
	----------
	actionban
	actioncheck
	actionreban
	actionreload
	actionrepair
	actionstart
	actionstop
	actionunban
	timeout
	)�matches�	ipmatches�
ipjailmatchescCsXd|_z$d|_d|_d|_d|_d|_d|_d|_d|_d|_	d|_
Wd|_dSd|_w)z8 Clear all lists/dicts parameters (used by reloading)
		r�<�rN)�_CommandAction__initrz�actionstart�	actionban�actionreban�actionunban�actioncheck�actionrepair�actionflush�
actionstop�actionreloadr:rrr�clearAllParams>szCommandAction.clearAllParamscsHtt|��||�d|_d|_i|_i|_|��|j�	d|j
�dS)Nrz
Created %s)�superr|r+r��_CommandAction__properties�_CommandAction__started�_CommandAction__substCacher�ri�debugr0rj�r0rrr+XszCommandAction.__init__cCstSr,)�NotImplemented)rbrcrrrrfartzCommandAction.__subclasshook__cCsn|�d�s0|js0t|�s0t�|�}|dkrdS|dkr t�|�}d|_|j�	�|j
�d||�||j|<dS)N�_ryrxz
  Set %s = %r)
�
startswithr�r5�WRAP_CMD_PARAMS�getrrxr�r��clearrir��__dict__)r(rlrD�wrprrr�__setattr__es


zCommandAction.__setattr__cCs4|�d�sd|_|j��|j�d|�|j|=dS)Nr�z
  Unset %s)r�r�r�r�rir�r�)r(rlrrr�__delattr__ws


zCommandAction.__delattr__cs2�jdur�jSt�fdd�t��D���_�jS)z`A dictionary of the actions properties.

		This is used to substitute "tags" in the commands.
		Nc3s6�|]}|�d�stt�|��s|t�|�fVqdS)r�N)r�r5ra)r7rCr:rrr;�s���
�z,CommandAction._properties.<locals>.<genexpr>)r�r'�dirr:rr:r�_properties�s

�zCommandAction._propertiescCs|jSr,)r�r:rrr�_substCache�szCommandAction._substCachecCsF|j||j|rd|nd|jd�}d|vs|s|S|�|d|i�}|S)N�family=r���conditional�cache�<�family)�
replaceTagr�r��replaceDynamicTags)r(�tagr��cmdrrr�
_getOperation�s
�zCommandAction._getOperationcs�d|f}t|�s&t��s|j�|i����S�fdd�|j�|i���D�S|d}|rJz|j|}WntyCi}|j|<Ynw||�<dSz|j|}|���}t|���D]\�}||krf|�=q[WdStysYdSw)z? Get, set or delete command of operation considering family.
		�__eOpCmdcsg|]
\}}�|�r|�qSrr�r7�fr9�r�rr�
<listcomp>�sz4CommandAction._operationExecuted.<locals>.<listcomp>rN)rSr5r�r�r<rA�popr=)r(r�r�r)rCr��famdr9rr�r�_operationExecuted�s0 �

���z CommandAction._operationExecutedNcs<d}d}|sdd�|j��D�}|D]{�zc|�|��}d}|rd||�|�fdd��vrd|}	|jrYt|jjdd�}
|
sI|jj�d�}
t|jjd|
�t	�
�|
d	<�|
d
<|�||
�}	|�|	|j
�}||M}|rk|�|�|�|�|rs|nd�Wqty�}z
d}|}WYd}~qd}~ww|s�td||j|j|f��|S)
z�Executes the operation commands (like "actionstart", "actionstop", etc).

		Replace the tags in the action command with actions properties
		and executes the resulting command.
		TzScript errorcS�g|]\}}|r|�qSrr)r7�famoperr9rrrr���z3CommandAction._executeOperation.<locals>.<listcomp>cs|�kSr,r)r��r�rrr�sz1CommandAction._executeOperation.<locals>.<lambda>�
actionInfoN�timer�FzError %s action %s/%s: %r)r�r<r�r�rgra�actions�_getActionInfo�setattrrr�r��
executeCmdrz�
ValueError�RuntimeErrorrh)r(r��	operationr��	afterExec�res�errr��ret�realCmdrq�err�r�_executeOperation�s<��zCommandAction._executeOperationcCsH|j�d�}|dur|Sd}|jD]}t�|�rd}nq||jd<|S)N�__hasCondSectionFT)r�r��CONDITIONAL_FAM_RE�match)r(r9r8rrr�_hasCondSection�s

�
zCommandAction._hasCondSectioncCsl|j�d�}|r
|S|j�d�}|rt|ttf�st|�}n|jr,t�r(ddgndg}ndg}||jd<|S)N�
__families�familiesrrr�)r�r��
isinstancer=�setr
r��allowed_ipv6�r(r9rrr�	_families�s

zCommandAction._familiescCs,|j�d�}|dur|S|j}||jd<|S)z1Checks the action depends on family (conditional)�actionstart_on_demandN)r�r�r�r�rrr�_startOnDemand�s
zCommandAction._startOnDemandcC�|��S)��Executes the "actionstart" command.

		Replace the tags in the action command with actions properties
		and executes the resulting command.
		)�_startr:rrrr\	�zCommandAction.startFcsZ�jr|sdSn
|s�j�|�rdS|dur|gn�j}�fdd�}�jdd||d�}|S)r�TNcs$|r��d|d�d�j|<dSdS)N�<actionstop>r)r�r��r�r�r:rr�_starteds�z&CommandAction._start.<locals>._started�
<actionstart>�starting�r�r�)r�r�r�r�r�)r(r��
forceStartr�r�rr:rr�s�zCommandAction._start�<actionban>cCs`|�dd�}|jr|j�|�s|j|dd�|�||�s"td|��|j�|d�dB|j|<dS)	a)Executes the given command ("actionban" or "actionreban").

		Replaces the tags in the action command with actions properties
		and ban information, and executes the resulting command.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		r�r�T�r�zError banning %(ip)sr�N)r�r�r�r��_processCmdr�)r(rqr�r�rrrr^%s
zCommandAction.bancCs t|d�o|jot|j���S)N�
actionprolong)rGr�r�isspacer:rrrrs;s�zCommandAction._prolongablecCs|�d|�std|��dS)aExecutes the "actionprolong" command.

		Replaces the tags in the action command with actions properties
		and ban information, and executes the resulting command.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		z<actionprolong>zError prolonging %(ip)sN)r�r�rprrr�prolong@s�zCommandAction.prolongcCs>|�dd�}|j�|d�d@r|�d|�std|��dSdS)aExecutes the "actionunban" command.

		Replaces the tags in the action command with actions properties
		and ban information, and executes the resulting command.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		r�r�rr
�
<actionunban>zError unbanning %(ip)sN)r�r�r�r�)r(rqr�rrrr`Os�zCommandAction.unbancCs|�||jr	d�Sd�S)aDExecutes the "actionreban" command if available, otherwise simply repeat "actionban".

		Replaces the tags in the action command with actions properties
		and ban information, and executes the resulting command.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		z
<actionreban>r�)r^r�rprrrr_`s
zCommandAction.rebancs:dd��j��D�}|sdS�fdd�}�jdd||d�S)	aExecutes the "actionflush" command.
		
		Command executed in order to flush all bans at once (e. g. by stop/shutdown 
		the system), instead of unbanning of each single ticket.

		Replaces the tags in the action command with actions properties
		and executes the resulting command.
		cSs g|]\}}|d@dkr|�qS)r�rr�rrrr�ys z'CommandAction.flush.<locals>.<listcomp>Tcs.|r�j�|�r�j|dM<dSdSdS)N���)r�r�r�r:rr�_afterFlush}s�z(CommandAction.flush.<locals>._afterFlushz
<actionflush>�flushingr�)r�r<r�)r(r�r�rr:r�flushos
zCommandAction.flushcCr�)��Executes the "actionstop" command.

		Replaces the tags in the action command with actions properties
		and executes the resulting command.
		)�_stopr:rrrr]�r�zCommandAction.stopcs||durdd��j��D�}|sdSi�_nz�j|dM<|g}Wn
ty.YdSw�fdd�}�jdd	||d
�S)r�NcSr�rrr�rrrr��r�z'CommandAction._stop.<locals>.<listcomp>Trcs|r��d|d�dSdS)Nr�)r�r�r:rr�_stopped�s�z%CommandAction._stop.<locals>._stoppedr��stoppingr�)r�r<rAr�)r(r�r�rr:rr��s
�zCommandAction._stopcKs|�dd�S)z�Executes the "actionreload" command.

		Parameters
		----------
		kwargs : dict
		  Currently unused, because CommandAction do not support initOpts

		Replaces the tags in the action command with actions properties
		and executes the resulting command.
		z<actionreload>�	reloading)r�)r(r*rrr�reload�szCommandAction.reloadcCsVd}|jr)t|j���D]\}}|r(|�||�s(d|j|<|�d|d�|dM}q|S)zFExecutes the invariant check with repair if expected (conditional).
		Trr�NF)r�r=r�r<�_invariantCheckr�)r(�beforeRepairr�r��startedrrr�consistencyCheck�s
�zCommandAction.consistencyCheckz[\\#&;`|*?~<>^()\[\]{}$'"\n\r]cs(ddd���fdd�}|j�||�}|S)a5Escape characters which may be used for command injection.

		Parameters
		----------
		value : str
			A string of which characters will be escaped.

		Returns
		-------
		str
			`value` with certain characters escaped.

		Notes
		-----
		The following characters are escaped::

			\#&;`|*?~<>^()[]{}$'"


		r8�r)r�
cs|��}d��||�S)N�\)�groupr�)�m�c��_map2crr�	substChar�sz*CommandAction.escapeTag.<locals>.substChar)�
ESCAPE_CRE�sub)rbrDr�rr�r�	escapeTag�s
zCommandAction.escapeTagr�cs4d|vr|S|dur|�f}z||WStyYnwt|t�}|�|sZd�}|durEdt|��f}z||�Wn	tyDYnw�durRt|��j|d��|durZ�||<tt������fdd�}	td}
	t�	|	|�}|run||ks}d|vr~n|}|
d8}
|
d	kr�t
d
|f��ql|dur�|||<|S)z�Replaces tags in `query` with property values.

		Parameters
		----------
		query : str
			String with tags.
		aInfo : dict
			Tags(keys) and associated values for substitution in query.

		Returns
		-------
		str
			`query` string with tags replaced.
		r�Nz
subst-tags)ry�addreplcsn|�d�}d}�r��|d��}|dur'��|�}|dur'��||���St|�}|�jvr5��|�}|S)Nr�?)r�r�r�_escapedTagsr�r�r�rD��ADD_REPL_TAGS_CMrbr��subInforr�substVals



z*CommandAction.replaceTag.<locals>.substValrTrzaunexpected too long replacement interpolation, possible self referencing definitions in query: %s)rAr�r"�idrr�
ADD_REPL_TAGSrrr�r�)rb�queryrqr�rr��ckey�	noRecRepl�csubkeyr�countrDrrrr��sX
�
�
����zCommandAction.replaceTagz![\\#&;`|*?~<>\^\(\)\[\]{}$'"\n\r]z\Wcs�t���s��fdd��tt�����fdd�}t�||�}d|vr8��d���s+i���fdd�}t�||�}�r@t�|��}|S)	a%Replaces dynamical tags in `query` with property values.

		**Important**
		-------------
		Because this tags are dynamic resp. foreign (user) input:
		  - values should be escaped (using "escape" as shell variable)
		  - no recursive substitution (no interpolation for <a<b>>)
		  - don't use cache

		Parameters
		----------
		query : str
			String with tags.
		aInfo : dict
			Tags(keys) and associated values for substitution in query.

		Returns
		-------
		str
			shell script as string or array with tags replaced (direct or as variables).
		cs2�j�|�rd�j�d|�}|�|<d|}|S)Nzf2bV_%sr��$)r��search�
ESCAPE_VN_CREr�)r�rD)rb�varsDictrr�	escapeValSs
z3CommandAction.replaceDynamicTags.<locals>.escapeValcsL|�d�}z�|}Wnty��||���YSwt|�}�||�S)Nr)r�rAr�rr)rrqrrrras
�
z2CommandAction.replaceDynamicTags.<locals>.substValr�zF-*csBt|�d��}zt�|�}Wn
tyYdSw�d||�S)Nrr��F_)rr�rrAr)r�tickDatarr�substTagss�z2CommandAction.replaceDynamicTags.<locals>.substTag)	r'r"r
rr�r��FCUSTAG_CREr	�
buildShellCmd)rbr�rqrrrr)rrqrbrrrrr�8s
z CommandAction.replaceDynamicTagscCst|dd�S)N�	_banEpochr)rar:rrr�banEpoch�szCommandAction.banEpochcCs8|jdur|jjjd|_|jj_dS|jd|_dS)z�Increments ban epoch of jail and this action, so already banned tickets would cause
		a re-ban for all tickets with previous epoch.Nr)rgr�rrr:rrr�invalidateBanEpoch�s
z CommandAction.invalidateBanEpochTcCs|s
|dur
||jvr
dS|�d|�}|r|�||j�rdS|r%|�s%dS|j�d�|��|�d|�}|rQ|�||j�sKd|j|<|j�d�dSd|j|<nz|�|�Wn	t	yaYnw|j
||pj|jd	�|j�|�r�|�||j�s�|j�d�dSdS)
z0Executes a substituted `actioncheck` command.
		Nrz
<actioncheck>���z<Invariant check failed. Trying to restore a sane environmentz<actionrepair>rzUnable to restore environmentr�)
r�r�r�rzri�errorr�criticalr�r�r�r�r�)r(r�r�r��checkCmd�	repairCmdrrrr��s8
�
�zCommandAction._invariantCheckc	s��dkr�j�d�dSz|d}Wn
ttfyd}Ynw�jr:��fdd�}�j||�dkd�}|d	kr:d
S�j��j|rEd|nd�jd�}|d
urV��	||�}n�}��
|�j�S)a�Executes a command with preliminary checks and substitutions.

		Before executing any commands, executes the "check" command first
		in order to check if pre-requirements are met. If this check fails,
		it tries to restore a sane environment before executing the real
		command.

		Parameters
		----------
		cmd : str
			The command to execute.
		aInfo : dictionary
			Dynamic properties.

		Returns
		-------
		bool
			True if the command succeeded.
		r��
Nothing to doTr�cs(�dkr�j�d�s�j�d�dSdS)Nr��actionrepair_on_unbanz,Invariant check failed. Unban is impossible.FT)r�r�rirr�r�r(rr�
_beforeRepair�sz0CommandAction._processCmd.<locals>._beforeRepairr�r�rFr�r�N)rir�rA�	TypeErrorr�r�r�r�r�r�r�rz)r(r�rqr�r%r�r�rr$rr��s(�
�zCommandAction._processCmdr�cKsnt��tjkr
t�d|�|st�d�dSt�tj||fddd�|��Wd�S1s0wYdS)aIExecutes a command.

		Parameters
		----------
		realCmd : str
			The command to execute.
		timeout : int
			The time out in seconds for the command.

		Returns
		-------
		bool
			True if the command succeeded.

		Raises
		------
		OSError
			If command fails to be executed.
		RuntimeError
			If command execution times out.
		�	r"TF)�shell�outputN)	�logSys�getEffectiveLevel�logging�DEBUG�logr��	_cmd_lockr	r�)r�rzr*rrrr��s
$�zCommandAction.executeCmdrI)r�r,)r�NN)NNT)r�)0r1rUrVrWr�rr�r+rurfr�rKr�rvr�r�r�r�r�r�r�r�r\r�r^rsr�r`r_r�r]r�r�r��re�compiler�rr�rr�rrr�r��staticmethodr��
__classcell__rrr�rr| sh	



'











Y
I


(8r|)3�
__author__�
__copyright__�__license__r,�osr0�signal�
subprocess�tempfile�	threadingr��abcr�collections.abcr�ImportError�collections�	failregexr�ipdnsr�mytimer�utilsr	�helpersrrr
rrrrr1r*�Lockr/�
IPv6IsAllowedr�r1r�
COND_FAMILIESr��
DYN_REPL_TAGSr
�update�objectr"r[r�r|rrrr�<module>sR�$

��
oc�

https://t.me/RX1948 - 2025