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__/utils.cpython-36.pyc
3

UB(d�.�@s�dZdZdZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlZddlm
Z
mZmZddlmZejdkr�ddlZnddlZe
e�Zd
diZedd
�ejj�D��ZGdd�d�ZdS)z2Serg G. Brester (sebres) and Fail2Ban ContributorszYCopyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav Halchenko, 2012-2015 Serg G. BresterZGPL�N)�Lock�)�	getLogger�_merge_dicts�
uni_decode)�OrderedDict��am"Command not found".  Make sure that all commands in %(realCmd)r are in the PATH of fail2ban-server process (grep -a PATH= /proc/`pidof -x fail2ban-server`/environ). You may want to start "fail2ban-server -f" separately, initiate it with "fail2ban-client reload" in another shell session and observe if additional informative error messages appear in the terminals.ccs$|]\}}|jd�r||fVqdS)ZSIGN)�
startswith)�.0�nameZnum�r
�/usr/lib/python3.6/utils.py�	<genexpr>7src@s�eZdZdZdZdZdZedZGdd�de�Z	e
dd	��Ze
d
d��Ze
ddd��Z
e
ddd��Zejdkr|e
dd��Zne
dd��Ze
dd��ZdS)�UtilszPUtilities provide diverse static methods like executes OS shell commands, etc.
	rg�������?g����MbP?�dc@sLeZdZdZdd�Zddd�Zdd	�Zddd�Zd
d�Zdd�Z	dd�Z
d
S)zUtils.Cachez.A simple cache with a TTL and limit on size
		cOs |j||�t�|_t�|_dS)N)�
setOptionsr�_cacher�_Cache__lock)�self�args�kwargsr
r
r�__init__HszUtils.Cache.__init__���<cCs||_||_dS)N)�maxCount�maxTime)rrrr
r
rrMszUtils.Cache.setOptionscCs
t|j�S)N)�lenr)rr
r
r�__len__QszUtils.Cache.__len__NcCs6|jj|�}|r2|dtj�kr(|dS|j|�|S)N�r)r�get�time�unset)r�kZdefv�vr
r
rr Ts
zUtils.Cache.getc
Csztj�}|j}|j�\t|�|jkrZx4|rX|jdd�\}}|d|kr&t|�|jkr&Pq&W|||jf||<WdQRXdS)NF)Zlastr)r!rrrr�popitemr)rr#r$�t�cacheZckZcvr
r
r�set\szUtils.Cache.setc
Cs$|j�|jj|d�WdQRXdS)N)rr�pop)rr#r
r
rr"lszUtils.Cache.unsetc	Cs |j�|jj�WdQRXdS)N)rr�clear)rr
r
rr*pszUtils.Cache.clear)rr)N)�__name__�
__module__�__qualname__�__doc__rrrr r(r"r*r
r
r
r�CacheDs

r/cCs>tj|tj�}|s|tjO}n|tjM}tj|tj|�|S)N)�fcntlZF_GETFL�os�
O_NONBLOCKZF_SETFL)Zfhandle�value�flagsr
r
r�
setFBlockModeuszUtils.setFBlockModecCspd}t|t�s|g}t|�d}x6|j�D]*\}}|d||f7}|j|�|d7}q*W|d|d|d<|S)a�Generates new shell command as array, contains map as variables to
		arguments statement (varsStat), the command (realCmd) used this variables and
		the list of the arguments, mapped from varsDict

		Example:
			buildShellCmd('echo "V2: $v2, V1: $v1"', {"v1": "val 1", "v2": "val 2", "vUnused": "unused var"})
		returns:
			['v1=$0 v2=$1 vUnused=$2 
echo "V2: $v2, V1: $v1"', 'val 1', 'val 2', 'unused var']
		�rz%s=$%s �
r)�
isinstance�listr�items�append)�realCmd�varsDictZvarsStat�ir#r$r
r
r�
buildShellCmds

zUtils.buildShellCmdrTFrNc1s�d}}d}	d�}
|r6|r*tj�|��nttj|�}
t�����fdd�}�ytj�tjtj||
tj	d���j
�}	|	dkr��fdd�}tj||tj�}	|	r�|	d}	|	dk�rZ|r�|t
j�d}tjd�|f�tj�j�}
tj|
tj�tjtj��j
�}	|	dk�s|�r<tj|
tj�tjtj�|	dk�r<�j
�}	|	dk�rZtj|
��rZtj}	Wnjtk
�r�}zL|�r�|t
j�d}d	�|f}tj|���s�|�s�d
Sd
|||	fSWYdd}~XnX|	|k�r�t
jnt
j}|tj�k�r|�r||t
jk�r|dnt
j�d}|�s(|tj�k�r��j�r�y0|	dk�sF|	dk�rTtj�jd
��jj �}Wn0t!k
�r�}ztjd|�WYdd}~XnX|dk	�r�|d
k�r�|tj�k�r�x&|j"�D]}tj#|d�t$|���q�W�j%�r�y0|	dk�s�|	dk�rtj�j%d
��j%j �}Wn0t!k
�rD}ztjd|�WYdd}~XnX|dk	�r�|d
k�r�|tj�k�r�x&|j"�D]}tj#|d�t$|���qrW�j�r��jj&��j%�r��j%j&�d
}|	|k�r�tj'd�|	�d}n�|	dk�r�tjd��j�n~|	dk�s|	dk�r<|	dk�r|	n|	d}tjd�t(j)|d|�|	�n4t*j)|	d�}tjd�|	�|�rptj+d|	|t,��|�r�||||	fSt-|�dk�r�|S||	fS)aExecutes a command.

		Parameters
		----------
		realCmd : str
			The command to execute.
		timeout : int
			The time out in seconds for the command.
		shell : bool
			If shell is True (default), the specified command (may be a string) will be 
			executed through the shell.
		output : bool
			If output is True, the function returns tuple (success, stdoutdata, stderrdata, returncode).
			If False, just indication of success is returned
		varsDict: dict
			variables supplied to the command (or to the shell script)

		Returns
		-------
		bool or (bool, str, str, int)
			True if the command succeeded and with stdout, stderr, returncode if output was set to True

		Raises
		------
		OSError
			If command fails to be executed.
		RuntimeError
			If command execution times out.
		Ncstj|d���S)Nz%x -- exec: %s)�logSys�log)�level)r<�	realCmdIdr
r�<lambda>�sz"Utils.executeCmd.<locals>.<lambda>)�stdout�stderr�shell�envZ
preexec_fncs�j�}|dk	rd|fSdS)NT)�poll)�retcode)�popenr
r�_popen_wait_end�sz)Utils.executeCmd.<locals>._popen_wait_endrz!%x -- timed out after %s seconds.z%s -- failed with %sFrz  ... -- failed to read stdout %sr6z%x -- stdout: %rz  ... -- failed to read stderr %sz%x -- stderr: %rz%x -- returned successfully %iTz%x -- unable to kill PID %i�z&%x -- killed with %s (return code: %s)z	signal %iz%x -- returned %izHINT on %i: %s).rr?rr1�environ�id�
subprocess�Popen�PIPE�setsidrI�wait_for�DEFAULT_SHORTEST_INTERVAL�loggingZERRORr@�error�getpgid�pid�killpg�signal�SIGTERMr!�sleep�DEFAULT_SLEEP_INTERVAL�SIGKILL�
pid_exists�OSError�DEBUGZgetEffectiveLevelrEr5�read�IOError�
splitlinesrArrF�close�debug�signamer �_RETCODE_HINTS�info�localsr)r<�timeoutrG�outputZtout_kill_treeZ
success_codesr=rErFrJrHZlogCmdrLZpgid�eZ	std_level�l�successZsigcode�msgr
)rKr<rCr�
executeCmd�s� 






(""



zUtils.executeCmdcsvd}xl|�}|r|S|rFd}}t|�sBtj�|��fdd�}n|}|�rNPt||pZtjtj�}tj|�qW|S)a5Wait until condition expression `cond` is True, up to `timeout` sec

		Parameters
		----------
		cond : callable
			The expression to check condition 
			(should return equivalent to bool True if wait successful).
		timeout : float or callable
			The time out for end of wait
			(in seconds or callable that returns True if timeout occurred).
		interval : float (optional)
			Polling start interval for wait cycle in seconds.

		Returns
		-------
		variable
			The return value of the last call of `cond`, 
			logical False (or None, 0, etc) if timeout occurred.
		rrcstj��kS)N)r!r
)�time0r
rrD<sz Utils.wait_for.<locals>.<lambda>)�callabler!�minrr^�DEFAULT_SLEEP_TIMEr])ZcondrlZintervalZini�retZstmZtimeout_exprr
)rsrrTs zUtils.wait_for�posixcCsVddl}|dkrdSytj|d�Wn(tk
rL}z|j|jkSd}~XnXdSdS)z6Check whether pid exists in the current process table.rNFT)�errnor1�killraZEPERM)rYryrnr
r
rr`HszUtils.pid_existscCs@ddl}|jj}d}|j|d|�}|dkr8|j|�dSdSdS)NriTF)�ctypesZwindll�kernel32ZOpenProcessZCloseHandle)rYr{r|ZSYNCHRONIZEZprocessr
r
rr`Us
cCsFtjjtjj|��d}tjdkr6tjj||�j	�}nt
j||�}|S)Nrr)rr)r1�path�splitext�basename�sys�version_info�	importlib�	machinery�SourceFileLoader�load_module�impZload_source)ZpythonModuleZpythonModuleName�modr
r
r�load_python_modulebs
zUtils.load_python_module�r)rTFTr�N)N)r+r,r-r.rvr^ZDEFAULT_SHORT_INTERVALrU�objectr/�staticmethodr5r?rrrTr1rr`r�r
r
r
rr:s$1
)


r)rr)�
__author__Z
__copyright__Z__license__r0rVr1r[rPr�Z	threadingrr!Zhelpersrrr�collectionsrr�Zimportlib.machineryr�r�r+r@ri�dict�__dict__r:rhrr
r
r
r�<module>s(