%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/fail2ban/server/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/fail2ban/server/utils.pyc

�
��&dc@s9dZdZdZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlZddlm
Z
mZmZyddlmZWnek
r�eZnXejdkr�ddlZnddlZe
e�Zid
d6Zed�ejj�D��Zd
dd��YZdS(s2Serg G. Brester (sebres) and Fail2Ban ContributorssYCopyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav Halchenko, 2012-2015 Serg G. BrestertGPLi����N(tLocki(t	getLoggert_merge_dictst
uni_decode(tOrderedDictism"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.iccs0|]&\}}|jd�r||fVqdS(tSIGN(t
startswith(t.0tnametnum((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pys	<genexpr>;stUtilscBs�eZdZdZdZdZedZdefd��YZe	d��Z
e	d��Ze	d	ee
eddd��Ze	dd��Zejd
kr�e	d��Zne	d��Ze	d��ZRS(sPUtilities provide diverse static methods like executes OS shell commands, etc.
	ig�������?g����MbP?idtCachecBsVeZdZd�Zddd�Zd�Zd
d�Zd�Zd�Z	d	�Z
RS(s.A simple cache with a TTL and limit on size
		cOs,|j||�t�|_t�|_dS(N(t
setOptionsRt_cacheRt_Cache__lock(tselftargstkwargs((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyt__init__Lsi�i<cCs||_||_dS(N(tmaxCounttmaxTime(RRR((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyR
Qs	cCs
t|j�S(N(tlenR(R((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyt__len__UscCsJ|jj|�}|rF|dtj�kr6|dS|j|�n|S(Nii(Rtgetttimetunset(Rtktdefvtv((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRXscCsHtj�}|j}|j�$t|�|jkr'ttk	r�x�|r�|jdt�\}}|d|krCt|�|jkrCPqCqCWq'g}x:|j	�D],\}}|d|kr�|j
|�q�q�Wx!|D]}|jj|d�q�Wx,|r#t|�|jkr#|j�q�Wn|||j
f||<WdQXdS(Ntlasti(RRRRRRtdicttpopitemtFalset	iteritemstappendtpoptNoneR(RRRtttcachetcktcvtremlst((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pytset`s$	
	%
cCs'|j�|jj|d�WdQXdS(N(RRR$R%(RR((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyR|s
cCs!|j�|jj�WdQXdS(N(RRtclear(R((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyR,�s
N(t__name__t
__module__t__doc__RR
RR%RR+RR,(((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRHs				cCsStj|tj�}|s+|tjO}n|tjM}tj|tj|�|S(N(tfcntltF_GETFLtost
O_NONBLOCKtF_SETFL(tfhandletvaluetflags((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyt
setFBlockMode�scCs�d}t|t�s!|g}nt|�d}xE|j�D]7\}}|d||f7}|j|�|d7}q>W|d|d|d<|S(s�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']
		tis%s=$%s s
i(t
isinstancetlistRR"R#(trealCmdtvarsDicttvarsStattiRR((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyt
buildShellCmd�s
i<ics�d}}d}	d�}
|rP|r;tj�|��qPttj|�}
nt�����fd�}y�tj�dtj	dtj	d|d|
dtj
���j�}	|	dkr��fd�}tj||tj
�}	|	r�|	d}	q�n|	dkr�|r$|tj�d}ntjd	�|f�tj�j�}
tj|
tj�tjtj��j�}	|	dks�|r�tj|
tj�tjtj�|	dkr��j�}	q�n|	dkr�tj|
�r�tj}	q�nWnltk
ri}|r)|tj�d}nd
�|f}tj|��sj|sVtSt|||	fSnX|	|krtjntj}|tj�kr�|r�||tjkr�|dntj�d}q�n|s�|tj�kr��j r�yA|	dks|	dkr$tj!�j t�n�j j"�}Wn#t#k
rY}tjd|�nX|dk	r�|d
kr�|tj�kr�x3|j$�D]"}tj%|d�t&|��q�Wq�n�j'r�yA|	dks�|	dkr�tj!�j't�n�j'j"�}Wn#t#k
r,}tjd|�nX|dk	r�|d
kr�|tj�kr�x3|j$�D]"}tj%|d�t&|��qdWq�q�n�j r��j j(�n�j'r��j'j(�nt}|	|kr�tj)d�|	�t*}n�|	dkrtjd��j�n�|	dks0|	dkrv|	dkrC|	n|	d}tjd�t+j,|d|�|	�nHt-j,|	d�}tjd�|	�|r�tj.d|	|t/��n|r�||||	fSt0|�dkr�|S||	fS(sExecutes 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.
		cstj|d���S(Ns%x -- exec: %s(tlogSystlog(tlevel(R<t	realCmdId(s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyt<lambda>�ststdouttstderrtshelltenvt
preexec_fncs&�j�}|dk	r"t|fSdS(N(tpollR%tTrue(tretcode(tpopen(s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyt_popen_wait_end�sis!%x -- timed out after %s seconds.s%s -- failed with %sis  ... -- failed to read stdout %sR9s%x -- stdout: %rs  ... -- failed to read stderr %ss%x -- stderr: %rs%x -- returned successfully %is%x -- unable to kill PID %ii�s&%x -- killed with %s (return code: %s)s	signal %is%x -- returned %isHINT on %i: %sN(1R%RR@RR2tenvirontidt
subprocesstPopentPIPEtsetsidRKtwait_fortDEFAULT_SHORTEST_INTERVALtloggingtERRORRAterrortgetpgidtpidtkillpgtsignaltSIGTERMRtsleeptDEFAULT_SLEEP_INTERVALtSIGKILLt
pid_existstOSErrorR!tDEBUGtgetEffectiveLevelRFR8treadtIOErrort
splitlinesRBRRGtclosetdebugRLtsignameRt_RETCODE_HINTStinfotlocalsR(R<ttimeoutRHtoutputttout_kill_treet
success_codesR=RFRGRMRItlogCmdROtpgidtet	std_leveltltsuccesstsigcodetmsg((RNR<RDs9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyt
executeCmd�s� 

$
		
	
&	*&	*)				 cs�d}x�tr�|�}|r"|S|r{d}}t|�s`tj�|��fd�}n|}|s{tj}q{n|�r�Pnt||tj�}tj|�q	W|S(s5Wait 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.
		iicstj��kS(N(R((ttime0(s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRELs(RLtcallableRRRatmintDEFAULT_SLEEP_TIMER`(tcondRptintervaltinitrettstmttimeout_expr((R}s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRV-s$		
	tposixcCs[ddl}|dkrtSytj|d�Wn tk
rR}|j|jkSXtSdS(s6Check whether pid exists in the current process table.i����Ni(terrnoR!R2tkillRdtEPERMRL(R\R�Rv((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRcZscCsXddl}|jj}d}|j|d|�}|dkrP|j|�tStSdS(Ni����ii(tctypestwindlltkernel32tOpenProcesstCloseHandleRLR!(R\R�R�tSYNCHRONIZEtprocess((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRcgs
cCsetjjtjj|��d}tjdkrOtjj||�j	�}nt
j||�}|S(Nii(ii(R2tpathtsplitexttbasenametsystversion_infot	importlibt	machinerytSourceFileLoadertload_moduletimptload_source(tpythonModuletpythonModuleNametmod((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pytload_python_modulets		(iN(R-R.R/R�RatDEFAULT_SHORT_INTERVALRWtobjectRtstaticmethodR8R@RLR!R%R|RVR2R	RcR�(((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyR>s"
=
�+

(ii((t
__author__t
__copyright__t__license__R0RXR2R^RRR�t	threadingRRthelpersRRRtcollectionsRtImportErrorRR�timportlib.machineryR�R�R-RARmt__dict__R"RlR(((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyt<module>s0



	

Zerion Mini Shell 1.0