a =*f=.@sdZdZdZddlZddlZddlZddlZddlZddlZddl m Z ddl Z ddl m Z mZmZddlmZddlZe eZd d iZed d ejDZGd ddZdS)z2Serg G. Brester (sebres) and Fail2Ban ContributorszYCopyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav Halchenko, 2012-2015 Serg G. BresterZGPLN)Lock) getLogger _merge_dicts uni_decode) OrderedDictam"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$|]\}}|dr||fVqdS)ZSIGN) startswith).0nameZnumr 9/usr/lib/python3.9/site-packages/fail2ban/server/utils.py 4src@seZdZdZdZdZdZedZGdddeZ e dd Z e d d Z e dddZ e dddZejdkr|e ddZn e ddZe ddZdS)UtilszPUtilities provide diverse static methods like executes OS shell commands, etc. rg?gMbP?dc@sLeZdZdZddZdddZdd Zdd d Zd dZddZ ddZ d S)z Utils.Cachez.A simple cache with a TTL and limit on size cOs$|j|i|t|_t|_dSN) setOptionsr_cacher _Cache__lock)selfargskwargsr r r __init__EszUtils.Cache.__init__<cCs||_||_dSr)maxCountmaxTime)rrrr r r rJszUtils.Cache.setOptionscCs t|jSr)lenrrr r r __len__NszUtils.Cache.__len__NcCs6|j|}|r2|dtkr(|dS|||S)Nr)rgettimeunset)rkZdefvvr r r r!Qs   zUtils.Cache.getcCst}|j}|jbt||jkrV|rV|jdd\}}|d|kr$t||jkr$qVq$|||jf||<Wdn1s|0YdS)NF)lastr )r"rrrrpopitemr)rr$r%tcacheZckZcvr r r setYszUtils.Cache.setcCs8|j|j|dWdn1s*0YdSr)rrpop)rr$r r r r#iszUtils.Cache.unsetcCs4|j|jWdn1s&0YdSr)rrclearrr r r r,mszUtils.Cache.clear)rr)N) __name__ __module__ __qualname____doc__rrrr!r*r#r,r r r r CacheAs  r1cCs>t|tj}|s|tjO}n |tjM}t|tj||Sr)fcntlZF_GETFLos O_NONBLOCKZF_SETFL)Zfhandlevalueflagsr r r setFBlockModers   zUtils.setFBlockModecCsld}t|ts|g}t|d}|D]*\}}|d||f7}|||d7}q(|d|d|d<|S)aGenerates 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'] r z%s=$%s  r) isinstancelistritemsappend)realCmdvarsDictZvarsStatir$r%r r r buildShellCmd|s     zUtils.buildShellCmdrTFrNc sd}}d} d} |r6|r*t|n ttj|} tfdd} z tjtjtj|| tj d } | durfdd} t | |tj } | r| d} | durX| r| t jd} td|ftj} t| tjttj } | dus|r realCmdIdr r z"Utils.executeCmd..)stdoutstderrshellenvZ preexec_fncs}|durd|fSdS)NT)poll)retcode)popenr r _popen_wait_endsz)Utils.executeCmd.._popen_wait_endr z!%x -- timed out after %s seconds.z%s -- failed with %sFrz ... -- failed to read stdout %sr8z%x -- stdout: %rz ... -- failed to read stderr %sz%x -- stderr: %rz%x -- returned successfully %iTz%x -- unable to kill PID %iz&%x -- killed with %s (return code: %s)z signal %iz%x -- returned %izHINT on %i: %s).rrArr3environid subprocessPopenPIPEsetsidrMwait_forDEFAULT_SHORTEST_INTERVALloggingZERRORrCerrorgetpgidpidkillpgsignalSIGTERMr"sleepDEFAULT_SLEEP_INTERVALSIGKILL pid_existsOSErrorDEBUGZgetEffectiveLevelrIr7readIOError splitlinesrDrrJclosedebugsignamer!_RETCODE_HINTSinfolocalsr)r>timeoutrKoutputZtout_kill_treeZ success_codesr?rIrJrNrLZlogCmdrPZpgideZ std_levellsuccessZsigcodemsgr )rOr>rFr executeCmds           8("" ""     zUtils.executeCmdcsrd}|}|r|S|rDd}}t|s@t|fdd}n|}|rLqnt||pXtjtj}t|q|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. r rcs tkSr)r"r Ztime0r r rG9rHz Utils.wait_for..)callabler"minrrbDEFAULT_SLEEP_TIMEra)ZcondrpintervalZiniretZstmZ timeout_exprr rwr rXs  zUtils.wait_forposixc Cs^ddl}|dkrdSzt|dWn0tyT}z|j|jkWYd}~Sd}~00dSdS)z6Check whether pid exists in the current process table.rNFT)errnor3killreEPERM)r]r~rrr r r rdEs"zUtils.pid_existscCs@ddl}|jj}d}||d|}|dkr8||dSdSdS)NriTF)ctypesZwindllkernel32Z OpenProcessZ CloseHandle)r]rrZ SYNCHRONIZEZprocessr r r rdRs cCs.tjtj|d}tj||}|S)Nr)r3pathsplitextbasename importlib machinerySourceFileLoader load_module)Z pythonModuleZpythonModuleNamemodr r r load_python_module_s zUtils.load_python_module)rTFTrBN)N)r-r.r/r0rzrbZDEFAULT_SHORT_INTERVALrYobjectr1 staticmethodr7rArvrXr3r rdrr r r r r7s01    )   r) __author__Z __copyright__Z __license__r2rZr3r_rTsys threadingrr"Zhelpersrrr collectionsrimportlib.machineryrr-rCrmdict__dict__r<rlrr r r r s(