ó Üœ^c@s9dZdZdZddlZddlZddlZddlZddlZddlZddl m Z ddl Z ddl m Z mZmZyddlmZWnek r¿eZnXejdkrÞddlZn ddlZe eƒZid d 6Zed „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(t OrderedDictism"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 ;stUtilscBsÐeZdZdZdZdZedZdefd„ƒYZe d„ƒZ e d„ƒZ e 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?idtCachecBsMeZdZd„Zddd„Zd„Zd d„Zd„Zd„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||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†} ytjˆ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| d kr$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‘Wnˆj j'ƒnˆj(r­yA| dksî| d krtj!ˆj(tƒnˆj(j"ƒ}Wn#t#k r9}tjd|ƒnX|dk rš|d krš|tjƒkršx3|j$ƒD]"}tj%|dˆt&|ƒƒqqWnˆj(j'ƒq­nt}| |krÛtj)dˆ| ƒt*}nË| dkrtjdˆˆjƒn¦| d ks| dkr^| d kr+| 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Ì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 %sR8s%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%R R?RR1tenvirontidt subprocesstPopentPIPEtsetsidRJtwait_fortDEFAULT_SHORTEST_INTERVALtloggingtERRORR@terrortgetpgidtpidtkillpgtsignaltSIGTERMRtsleeptDEFAULT_SLEEP_INTERVALtSIGKILLt pid_existstOSErrorR!tDEBUGtgetEffectiveLevelRER7treadtIOErrort splitlinesRARtcloseRFtdebugRKtsignameRt_RETCODE_HINTStinfotlocalsR(R;ttimeoutRGtoutputttout_kill_treet success_codesR<RERFRLRHtlogCmdRNtpgidtet std_leveltltsuccesstsigcodetmsg((RMR;RCs9/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.pyRDGs(RKtcallableRR R`tmintDEFAULT_SLEEP_TIMER_(tcondRotintervaltinitrettstmt timeout_expr((R|s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRU(s$     tposixcCs[ddl}|dkrtSytj|dƒWn tk rR}|j|jkSXtSdS(s6Check whether pid exists in the current process table.iÿÿÿÿNi(terrnoR!R1tkillRctEPERMRK(R[R‡Ru((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRbUs  cCsXddl}|jj}d}|j|d|ƒ}|dkrP|j|ƒtStSdS(Niÿÿÿÿii(tctypestwindlltkernel32t OpenProcesst CloseHandleRKR!(R[RŠRŒt SYNCHRONIZEtprocess((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyRbbs    cCsetjjtjj|ƒƒd}tjdkrOtjj||ƒj ƒ}nt j ||ƒ}|S(Nii(ii( R1tpathtsplitexttbasenametsyst version_infot importlibt machinerytSourceFileLoadert load_moduletimpt load_source(t pythonModuletpythonModuleNametmod((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pytload_python_moduleos   (iN(R,R-R.RR`tDEFAULT_SHORT_INTERVALRVtobjectR t staticmethodR7R?RKR!R%R{RUR1R RbRŸ(((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyR >s" 9  „+  (ii((t __author__t __copyright__t __license__R/RWR1R]RQR”t threadingRRthelpersRRRt collectionsRt ImportErrorRR•timportlib.machineryR–RšR,R@Rlt__dict__R"RkR (((s9/usr/lib/python2.7/site-packages/fail2ban/server/utils.pyts0