ogf.dZdZdZddlZddlZddlZddlZddlZddlZddl m Z ddl Z ddl m Z mZmZddlmZej$d k\rddlZnddlZe eZd d iZed ej4j7DZGd dZy)z2Serg G. Brester (sebres) and Fail2Ban ContributorszYCopyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav Halchenko, 2012-2015 Serg G. BresterGPLN)Lock) getLogger _merge_dicts uni_decode) OrderedDictr 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.c#LK|]\}}|jdr||fyw)SIGN) startswith).0namenums 7/usr/lib/python3/dist-packages/fail2ban/server/utils.py r7s-ET3T__U-CT{Es"$ceZdZdZdZdZdZedz ZGddeZ e dZ e d Z e dd Z e dd Zej d k(r e dZne dZe dZy )UtilszPUtilities provide diverse static methods like executes OS shell commands, etc. rg?gMbP?dc>eZdZdZdZd dZdZd dZdZdZ d Z y) Utils.Cachez.A simple cache with a TTL and limit on size cd|j|i|t|_t|_yN) setOptionsr _cacher _Cache__lock)selfargskwargss r__init__zUtils.Cache.__init__Hs'4??D#F#4;4;c ||_||_yr)maxCountmaxTime)rr%r&s rrzUtils.Cache.setOptionsMs4=4zUtils.Cache.clearps. KKs1:)i<r) __name__ __module__ __qualname____doc__r"rr*r-r:r/r>r#rCacherDs* % r#rEctj|tj}|s|tjz}n|tjz}tj|tj||Sr)fcntlF_GETFLos O_NONBLOCKF_SETFL)fhandlevalueflagss r setFBlockModezUtils.setFBlockModeusP ++gu}} -% BMM5R]]N5++gu}}e, ,r#cd}t|ts|g}t|dz }|jD]&\}}||d|dz }|j ||dz }(|dz|dz|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=$  r) isinstancelistr(itemsappend)realCmdvarsDictvarsStatir0r2s r buildShellCmdzUtils.buildShellCmds( GT "Y7 'l1n!nn da Aq!!8 >>!61 $+'!* .r#Nc  dx}}d} dx} |r3|rtj|nttj|} t fd} t jt jt j|| tjj} | 1fd} tj| |tj} | r| d} | .| r| tjd} tj!d|fztj"j$} tj&| t(j*t-j.tj0j} | |rYtj&| t(j2t-j.tj0| j} | %tj5| st(j2} | |vrtj8ntj}|tj;k\r2| r0| |tj8k(r|dz ntjd} |s|tj;k\rDj<r | | d kr tj?j<dj<jA}|R|d k7rM|tj;k\r6|jED]#}tjG|d tI|%jJr | | d kr tj?jJdjJjA}|R|d k7rM|tj;k\r6|jED]#}tjG|dtI|%j<rj<jMjJrjJjMd}| |vrtjOd| d}n| "tj!dj$n| d ks| dkDr=| d kr| n| dz }tj!dtPjS|d|z| nQtTjS| d}tj!d| |r"tjWd| |tYz|r|||| fSt[|dk(r|S|| fS#t6$rR}| r| tjd} d|}tj!|s|sdnd||| fcYd}~SYd}~ed}~wwxYw#tB$r!}tj!d |Yd}~d}~wwxYw#tB$r!}tj!d |Yd}~Hd}~wwxYw)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. Nc4tj|dS)Nz%x -- exec: %s)logSyslog)levelrX realCmdIds rz"Utils.executeCmd..sE+;YPr#)stdoutstderrshellenv preexec_fnc4j}|d|fSdS)NT)poll)retcodepopens r_popen_wait_endz)Utils.executeCmd.._popen_wait_ends"zz|W&2T7O<Yq[GMMR]aTZ yF4466 ll97Q; %,,.ll!VflyF: 667 ** &!+C'71CCq E fW]]#dV&-q 16 << 5UFFG$DD  E& 9 \\4a889 9 \\4a889sRF9T"AU;AV( U8&AU3'U83U8; V%V  V%( W1W  Wcd} |}|r|S|r.dx}}t|stj|zfd}n|}r |St|xstjztj }tj |)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,rc2tjkDSr)r.)time0srrcz Utils.wait_for..<sDIIK%/r#)callabler.minrrDEFAULT_SLEEP_TIMEr)condrintervaliniretstm timeout_exprrs @rruzUtils.wait_fors, # 3 J MC# G  YY[7 "U/\\n  * SH< < < =u?W?W X3::c? r#posixcddl}|dkry tj|dy#t$r#}|j|jk(cYd}~Sd}~wwxYw)z6Check whether pid exists in the current process table.rNFT)errnorIkillrEPERM)r{rrs rrzUtils.pid_existsHsM Ag GGCO  " 77ekk !!"s# AA A Acddl}|jj}d}|j|d|}|dk7r|j |yy)NriTF)ctypeswindllkernel32 OpenProcess CloseHandle)r{rr SYNCHRONIZEprocesss rrzUtils.pid_existsUsHmm$$8;  ! !+q# 67 l !  r#c6tjjtjj|d}tj dk\r0t jj||j}|Stj||}|S)Nrr ) rIpathsplitextbasenamesys version_info importlib machinerySourceFileLoader load_moduleimp load_source) pythonModulepythonModuleNamemods rload_python_modulezUtils.load_python_modulebsWW%%77L!##$&    - -l $$/KM * l $3 *r#)r?TFT)rNr)r@rArBrCrrDEFAULT_SHORT_INTERVALrvobjectrE staticmethodrOr\rrurIrrrrDr#rrr:s3c9.V.b,NR#DDDDL% % RGGw        r#r) __author__ __copyright__ __license__rGrwrIr}rqr threadingrr.helpersrrr collectionsr rimportlib.machineryrrr@r_rdict__dict__rVrrrDr#rrs(B k     99#v  8 D E//'')E Er r r#