
    =*fa                        S r SrSrSSKrSSKrSSKrSSKr SSKJr  SSK
Jr  SSKJrJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKJr  SSKJr  SSKJr  SSKJr  \" \5      r  " S S\\5      r!g! \	 a	    SSK
Jr   N_f = f)zCyril Jaquierz Copyright (c) 2004 Cyril JaquierGPL    N)Mapping)OrderedDict   )
BanManager	BanTicket)IPAddr)
JailThread)
ActionBaseCommandAction
CallingMap)MyTime)	Observers)Utils   )	getLoggerc                   
   \ rS rSrSrS r\S 5       rS"S jrS#S jr	S r
S	 rS
 rS rS rS rS rS rS rS$S jrS rS%S jrS&S jrS r " S S\5      rS rS'S jrS&S jrS(S jrS rS&S jrS)S jr S(S jr!S*S  jr"S!r#g)+Actions2   a  Handles jail actions.

This class handles the actions of the jail. Creation, deletion or to
actions must be done through this class. This class is based on the
Mapping type, and the `add` method must be used to add new actions.
This class also starts and stops the actions, and fetches bans from
the jail executing these bans via the actions.

Parameters
----------
jail: Jail
        The jail of which the actions belongs to.

Attributes
----------
daemon
ident
name
status
active : bool
        Control the state of the thread.
idle : bool
        Control the idle state of the thread.
sleeptime : int
        The time the thread sleeps for in the loop.
c                     [         R                  " U SUR                  -   S9  Xl        [	        5       U l        [        5       U l        SU l        SU l	        SU l
        U R                  S-  U l        g )Nzf2b/a.)namer   
   r   )r
   __init__r   _jailr   _actionsr   
banManagerbanEpoch _Actions__lastConsistencyCheckTMbanPrecedenceunbanMaxCount)selfjails     9/usr/lib/python3/dist-packages/fail2ban/server/actions.pyr   Actions.__init__N   s\    d$))!34*-$-L$/$-"#$$))A-$    c                     [         R                  " U 5      n[        US5      (       d  [        SU -  5      e[	        UR
                  [        5      (       d'  [        U < SUR
                  R                  < S35      eU$ )NActionz&%s module does not have 'Action' classz module z$ does not implement required methods)r   load_python_modulehasattrRuntimeError
issubclassr'   r   __name__)pythonModulemods     r#   _load_python_moduleActions._load_python_module\   sm      .#	h			,|;
= =cjj*--	3::&&(
) ) 
*r%   Nc                    XR                   ;   ae  U(       d  [        SU-  5      eU R                   U   n[        US5      (       a0  [        US5      (       a  UR                  5         X0R                  U'   gUc  [        U R                  U5      nO/U R                  U5      nUR                  " U R                  U40 UD6nXPR                   U'   g)a  Adds a new action.

Add a new action if not already present, defaulting to standard
`CommandAction`, or specified Python module.

Parameters
----------
name : str
        The name of the action.
pythonModule : str, optional
        Path to Python file which must contain `Action` class.
        Default None, which means `CommandAction` is used.
initOpts : dict, optional
        Options for Python Action, used as keyword arguments for
        initialisation. Default None.

Raises
------
ValueError
        If action name already exists.
RuntimeError
        If external Python module does not have `Action` class
        or does not implement necessary methods as per `ActionBase`
        abstract class.
zAction %s already existsreloadclearAllParamsN)	r   
ValueErrorr)   r3   _reload_actionsr   r   r/   r'   )r!   r   r-   initOptsr2   actioncustomActionModules          r#   addActions.addi   s    6 
]]

/$6
77MM$6fhv'(("*$
$**d+600>%%djj$C(C6--r%   c                   ^  U(       a  [        5       T l        g[        T S5      (       a  T R                  R                  5        H?  u  p#UT R                  ;   d  M  T R                  U   R
                  " S0 U(       a  UO0 D6  MA     [        U 4S jT R                  R                  5        5       5      n[        U5      (       a   T R                  SUSS9  T R                  US9  [        T S5        gg)	zMBegin or end of reloading resp. refreshing of all parameters
                r5   c              3   R   >#    U  H  u  pUTR                   ;  d  M  X4v   M     g 7fN)r5   ).0r   r7   r!   s      r#   	<genexpr>!Actions.reload.<locals>.<genexpr>   s,      *=R\TD((( )4.=Rs   '
'FT)dbactionsstoprB   N )dictr5   r)   itemsr   r2   r   len_Actions__flushBanstopActionsdelattr)r!   beginr   r6   delactss   `    r#   r2   Actions.reload   s     &4d%&&..446
mmD  BHbB 7  *T]]=P=P=R * *G
7||	__wT_:	g&D#$ 'r%   c                 Z     U R                   U   $ ! [         a    [        SU-  5      ef = fNzInvalid Action name: %sr   KeyErrorr!   r   s     r#   __getitem__Actions.__getitem__   s6    4
--
	 4	+d2	334s    *c                 X     U R                   U	 g ! [         a    [        SU-  5      ef = frP   rQ   rS   s     r#   __delitem__Actions.__delitem__   s3    4}}T	 4	+d2	334s    )c                 ,    [        U R                  5      $ r=   )iterr   r!   s    r#   __iter__Actions.__iter__   s    	dmm	r%   c                 ,    [        U R                  5      $ r=   )rH   r   r[   s    r#   __len__Actions.__len__   s    	T]]	r%   c                     gNFrE   )r!   others     r#   __eq__Actions.__eq__   s    	r%   c                     [        U 5      $ r=   )idr[   s    r#   __hash__Actions.__hash__   s    	D/r%   c                     [         R                  " U5      nU R                  R                  U5        [        R                  SU-  5        g )Nz  banTime: %s)r   str2secondsr   
setBanTimelogSysinfo)r!   values     r#   rl   Actions.setBanTime   s5    


U
#%//U#++o%&r%   c                 6    U R                   R                  5       $ r=   )r   
getBanTimer[   s    r#   rr   Actions.getBanTime   s    		#	#	%%r%   c                     U R                   R                  5       nU(       d  U$ [        U5      S:X  a  US   U;   a  S$ S$ U Vs/ s H  o3U;   a  SOSPM     sn$ s  snf Nr   r   )r   
getBanListrH   )r!   idslstips       r#   	getBannedActions.getBanned   s]    ""$#	
:X]1v}!#!#*-	.#BSy!a
#	..	.s   Ac                 6    U R                   R                  SUS9$ )zhReturns the list of banned IP addresses.

Returns
-------
list
        The list of banned IP addresses.
T)orderedwithTime)r   rv   )r!   r~   s     r#   rv   Actions.getBanList   s     
	#	#D8	#	DDr%   c                    ^ [         R                  " 5       m[        U[        5      (       a  U4S jU 5       nO[	        UT5      4nU R                  U5      $ )zBan an IP or list of IPs.c              3   <   >#    U  H  n[        UT5      v   M     g 7fr=   )r   )r>   ry   unixTimes     r#   r?   &Actions.addBannedIP.<locals>.<genexpr>   s     3"iH%%s   )r   time
isinstancelistr   _Actions__checkBan)r!   ry   ticketsr   s      @r#   addBannedIPActions.addBannedIP   sE    [[](D337 H%'7		!!r%   c                    Uc  U R                  U5      $ [        U[        [        45      (       a9  / nSnU H  n XPR	                  XbU5      -  nM     U(       a  [        SU-  5      eU$ U(       aG  U R                  R                  b0  U R                  R                  R                  U R                  U5        U R                  R                  U5      nUb  U R                  U5        g[        U[        5      (       dl  [        U5      nUR                  (       dP  [        [        UR                   U R                  R#                  5       5      5      n	U	(       a  U R	                  XU5      $ SU-  n
[$        R'                  [(        R*                  U
5        U(       a  g[        U
5      e! [
         a!    U(       d  UR                  U5         GM   GM  f = f)aG  Removes banned IP calling actions' unban method

Remove a banned IP now, rather than waiting for it to expire,
even if set to never expire.

Parameters
----------
ip : list, str, IPAddr or None
        The IP address (or multiple IPs as list) to unban or all IPs if None

Raises
------
ValueError
        If `ip` is not banned
r   znot banned: %rz%s is not bannedr   )rI   r   r   tupleremoveBannedIPr4   appendr   databasedelBanr   getTicketByID_Actions__unBanr	   isSinglefiltercontainsrv   rm   logloggingMSG)r!   ry   rA   ifexistsmissedcntiticketipaipsmsgs              r#   r   Actions.removeBannedIP   sz   " Z
//"
T5M""6	
3qx00S  
%.
//
: DJJ+::djj"-??((,&<< 
 R
 
 
*C<<s||T__%?%?%ABCS  (33	b	 3	::gkk3	C9  mmA s   F$$"GGc           
         Uc  U R                   n[        [        UR                  5       5      5       HN  u  p# UR	                  5         U R                   U	 [        R                  SU R                  R                  U5        MP     g! [
         aW  n[        R                  SU R                  R                  X$[        R                  5       [        R                  :*  S9   SnANSnAff = f)zLStops the actions in reverse sequence (optionally filtered)
                Nz(Failed to stop jail '%s' action '%s': %sexc_infoz%s: action %s terminated)r   reversedr   rG   rC   	Exceptionrm   errorr   r   getEffectiveLevelr   DEBUGdebug)r!   rB   r   r7   es        r#   rJ   Actions.stopActions/  s     _]]7tGMMO45ld9
KKM
 }}T	<<*DJJOOTB 6  9
LL;	ZZ__d&&('--7  99s   B
C&AC!!C&c           
        ^  SnT R                   R                  5        H  u  p# UR                  5         M     T R                  (       Ga   T R                  (       aS  [        R                  S5        [        R                  " U 4S jS T R                   5        [        R                  S5        Mw  Sn[#        T R                   T R$                  R&                  [(        R*                  " 5       -
  5      n[        R-                  S	S
UT R                   5        [        R                  " U 4S jU5      (       a  T R/                  5       nX-  nU(       a  UT R0                  :  a  T R                  (       a  US-  n[        R-                  S	SU(       a  UT R2                  :  a  UOT R2                  UT R2                  5        T R5                  U(       a  UT R2                  :  a  UOT R2                  5        SnT R                  (       a  GM  T R7                  SS9  T R9                  5         g! [         aY  n[        R                  ST R                  R                  X$[        R                  5       [        R                  :*  S9   SnAGMd  SnAff = f! [         aW  n[        R                  ST R                  R                  U[        R                  5       [        R                  :*  S9   SnANSnAff = f)zMain loop for Threading.

This function is the main loop of the thread. It checks the jail
queue and executes commands when an IP address is banned.

Returns
-------
bool
        True when the thread exits nicely.
r   z)Failed to start jail '%s' action '%s': %sr   NzActions: enter idle modec                  V   > T R                   (       + =(       d    T R                  (       + $ r=   )activeidler[   s   r#   <lambda>Actions.run.<locals>.<lambda>V  s    O<499}<r%   c                      grb   rE   rE   r%   r#   r   r   W  s    er%   zActions: leave idle mode   z1Actions: wait for pending tickets %s (default %s)c                  `   > T R                   (       + =(       d    T R                  R                  $ r=   )r   r   hasFailTicketsr[   s   r#   r   r   ^  s    $++oJ1J1JJr%   r   z+Actions: check-unban %s, bancnt %s, max: %sz*[%s] unhandled error in actions thread: %sT)rC   )r   rG   startr   rm   r   r   r   r   r   r   r   r   r   r   wait_for	sleeptimeminr   _nextUnbanTimer   r   r   r   r   r    _Actions__checkUnBanrI   rJ   )r!   r   r   r7   r   bancntwts   `      r#   runActions.run?  s>    	
#mm))+ld9
LLN , 	9yy\\,-
^^<T^^%\\,-F	T^^T__;;fkkmK	LB
JJqEr4>>Z~~JBOOooV]SSD...kfjjAVX^aeasasXs6y}  zL  zL  NT  VZ  Vh  Vh  i
&Vd6H6H-HdN`N`aS- 	8 //t/	E  9
LL<	ZZ__d&&('--7  9 996  9
LL=	ZZ__a&&('--7  99s9   H$A"J
 /EJ
 $
J.AJJ

K+AK&&K+c                      \ rS rSrSr0 SS _SS _SS _S	S
 _SS _SS _SS _SS _SS _SS _SS _SS2S j_SS _SS _S S! _S"S# _S$S% _S& S' S( S) S*.Er\R                  S+-   rSS,\4S- jrS. r	S/ r
S3S0 jrS1rg)4Actions.ActionInfoir  )fid
raw-ticketry   c                 6    U R                   R                  5       $ r=   )_ActionInfo__ticketgetIPr[   s    r#   r   Actions.ActionInfo.<lambda>w      ,,.r%   familyc                      U S   R                   $ Nry   )	familyStrr[   s    r#   r   r   x  s    DJ00r%   zip-revc                 *    U S   R                  S5      $ )Nry    )getPTRr[   s    r#   r   r   y  s    4:,,R0r%   zip-hostc                 (    U S   R                  5       $ r   )getHostr[   s    r#   r   r   z  s    4:--/r%   r   c                 6    U R                   R                  5       $ r=   )r   getIDr[   s    r#   r   r   {  r   r%   failuresc                 6    U R                   R                  5       $ r=   )r   
getAttemptr[   s    r#   r   r   |  s    DMM446r%   r   c                 6    U R                   R                  5       $ r=   )r   getTimer[   s    r#   r   r   }  s    $--//1r%   bantimec                 "    U R                  5       $ r=   )_getBanTimer[   s    r#   r   r   ~  s    D,,.r%   bancountc                 6    U R                   R                  5       $ r=   )r   getBanCountr[   s    r#   r   r     s    T]]668r%   matchesc                 T    SR                  U R                  R                  5       5      $ N
)joinr   
getMatchesr[   s    r#   r   r     s    499T]]%=%=%?@r%   restoredc                 @    U R                   R                  (       a  S$ S$ ru   )r   r   r[   s    r#   r   r     s    $--"8"8Q?a?r%   zF-*Nc                 8    U R                   R                  U5      $ r=   )r   getData)r!   tags     r#   r   r     s    4==#8#8#=r%   	ipmatchesc                 ^    SR                  U R                  S5      R                  5       5      $ )Nr   Tr   _mi4ipr   r[   s    r#   r   r     s    tyyT):)E)E)GHr%   ipjailmatchesc                 \    SR                  U R                  5       R                  5       5      $ r   r   r[   s    r#   r   r     s    4;;=+C+C+E!Fr%   
ipfailuresc                 @    U R                  S5      R                  5       $ NTr   r   r[   s    r#   r   r     s    D 1 < < >r%   ipjailfailuresc                 >    U R                  5       R                  5       $ r=   r   r[   s    r#   r   r     s    $++-":":"<r%   r   c                 ,    [        U R                  5      $ r=   )reprr   r[   s    r#   r   r     s    T]] 3r%   c                 ^    U R                   R                  R                  R                  5       $ r=   )_ActionInfo__jailrB   r   sizer[   s    r#   r   r     s    T[[%8%8%C%C%H%H%Jr%   c                 ^    U R                   R                  R                  R                  5       $ r=   )r   rB   r   getBanTotalr[   s    r#   r   r     s    T[[%8%8%C%C%O%O%Qr%   c                 ^    U R                   R                  R                  R                  5       $ r=   )r   r   failManagerr   r[   s    r#   r   r     s    T[[%7%7%C%C%H%H%Jr%   c                 ^    U R                   R                  R                  R                  5       $ r=   )r   r   r   getFailTotalr[   s    r#   r   r     s    T[[%7%7%C%C%P%P%Rr%   )zjail.bannedzjail.banned_totalz
jail.foundzjail.found_total)__ticket__jail__mi4ipTc                 R    Xl         X l        [        5       U l        X0l        X@l        g r=   )r   r   rF   storage	immutabledata)r!   r   r"   r  r  s        r#   r   Actions.ActionInfo.__init__  s    =;&4<>9r%   c                     U R                  U R                  U R                  U R                  U R                  R                  5       5      $ r=   )	__class__r   r   r  r  copyr[   s    r#   r  Actions.ActionInfo.copy  s.    
..T^^TYY^^EU
VVr%   c                     U R                   R                  5       nUc$  U R                  R                  R                  5       n[	        U5      $ r=   )r   rr   r   rB   int)r!   btimes     r#   r   Actions.ActionInfo._getBanTime  s:    ==##%5mT[[00;;=U
e*r%   c           	      X   [        U S5      (       d  0 U l        U R                  nU(       a  SOSnX2;   a  X#   b  X#   $ U R                  $  U R                  nU S   nSX#'   UR                  (       d  U R                  $ U(       a  UR                  R                  US9X#'   OUR                  R                  XTS9X#'    X#   b  X#   $ U R                  $ ! [         aN  n[        R                  SUWR                  U[        R                  5       [        R                  :*  S	9   SnANhSnAff = f)
a  Gets bans merged once, a helper for lambda(s), prevents stop of executing action by any exception inside.

This function never returns None for ainfo lambdas - always a ticket (merged or single one)
and prevents any errors through merging (to guarantee ban actions will be executed).
[TODO] move merging to observer - here we could wait for merge and read already merged info from a database

Parameters
----------
overalljails : bool
        switch to get a merged bans :
        False - (default) bans merged for current jail only
        True - bans merged for all jails of current ip address

Returns
-------
BanTicket 
        merged or self ticket only
r  allr"   Nry   )ry   )ry   r"   z+Failed to get %s bans merged, jail '%s': %sr   )r)   _ActionInfo__mi4ipr   r   r   getBansMergedr   rm   r   r   r   r   r   )r!   overalljailsmiidxr"   ry   r   s          r#   r   Actions.ActionInfo._mi4ip  s   & $	
"
"DL2F3	ig)27<t}}<9;;D	dBBG==MM}}**b*1RW}}**b*<RW W("';dmm;  9
LL2$))Q&&('--7  99s%   1C ="C  C 
D)AD$$D))r  r  r  r  r  r  r=   F)r,   
__module____qualname____firstlineno__CM_REPR_ITEMSAI_DICTr   	__slots__r   r  r   r   __static_attributes__rE   r%   r#   
ActionInfor   r  s*   '-.0 0 /	
 	. 6 
1 . 8 @ @ 	=  H!" F#$ >%& <'* 3+. KQJR5': ""%FF)"&$W W
(<r%   r"  c                     U(       d  [        S[        R                  " 5       5      n[        R	                  XR
                  5      nU$ )Nr   )r   r   r   r   r"  r   )r!   r   aInfos      r#   _getActionInfoActions._getActionInfo  s0    	b&++-(6


VZZ
0%	,r%   c              #      #    SnX!:  a3  U R                   R                  5       nU(       d  gUv   US-  nX!:  a  M2  gg7f)zAGenerator to get maximal count failure tickets from fail-manager.r   r   N)r   getFailTicket)r!   countr   r   s       r#   __getFailTicketsActions.__getFailTickets  s<     	#JJ$$&6
		<!83 	s   8>>c                 	  ^ SnU(       d  U R                  U R                  5      nSnU GH  n[        R                  " U5      mUR	                  U R
                  R	                  5       5      nTR                  5       nU R                  T5      n0 nU R
                  R                  TUS9(       Ga<  US-  n[        R                  b=  TR                  (       d,  [        R                  R                  STU R                  U5        [        R                  SU R                  R                   TR                  (       d  SOSU5        U R"                  R%                  5        H]  u  p TR                  (       a  ['        U
S	S
5      (       a  M+  UR(                  (       d  UR+                  5         U
R-                  U5        M_     STl        U R:                  (       a  U R:                  Tl        GM  GM  UR=                  SS5      (       a.  [        R?                  SU R                  R                   U5        GM  UR=                  ST5      mTR8                  (       Ga  URA                  5       TRA                  5       -
  nUS:  a  [4        R6                  O%US:  a  [4        RB                  O[4        RD                  n[        RG                  USU R                  R                   U5        TR:                  U R:                  :X  a  US:  a  U(       d  [H        RJ                  " 5       U RL                  S-   :  a^  [H        RJ                  " 5       U l&        U R"                  RO                  5        H&  n
[Q        U
S5      (       d  M  U
RS                  5         M(     TR:                  U R:                  :  aI  U(       d-  [U        U4S jU R"                  R%                  5        5       5      nX RW                  TUS9-  nGM  GM  X RW                  T5      -  nGM     U(       a]  [        RY                  SUU R
                  R[                  5       U R
                  R]                  5       U R                  R                   5        U$ ! [.         aZ  n[        R1                  SU R                  R                   XU[        R3                  5       [4        R6                  :*  S9   SnAGMp  SnAff = f)zCheck for IP address to ban.

If tickets are not specified look in the jail queue for FailTicket. If a ticket is available,
it executes the "ban" command and adds a ticket to the BanManager.

Returns
-------
bool
        True if an IP address get banned.
r   N)reasonr   banFoundz[%s] %sBan %sr   zRestore 
norestoredF9Failed to execute ban jail '%s' action '%s' info '%r': %sr   Texpiredz[%s] Ignore %s, expired bantimer      <   z[%s] %s already bannedconsistencyCheckc              3   f   >#    U  H&  u  pUR                   TR                   :  d  M!  X4v   M(     g 7fr=   )r   )r>   r   r7   bTickets      r#   r?   %Actions.__checkBan.<locals>.<genexpr>'  s0      )/*?,$??W--- ~*?s    1
1rD   z"Banned %s / %s, %s ticket(s) in %r)/_Actions__getFailTicketsr   r   wraprr   r   r   r%  addBanTicketr   Mainr   r9   r   rm   noticer   r   rG   getattrr  resetbanr   r   r   r   r   bannedr   getrn   r   NOTICEWARNINGr   r   r   r   valuesr)   r4  rF   _Actions__reBanr   r   r   )r!   r   r   	rebanactsr   r  ry   r$  r-  r   r7   r   diftmllr6  s                 @r#   
__checkBanActions.__checkBan  s~    	
#	""4#5#567)f^^F#7T__779:52w'56
oo""76":1HC~~!'*:*:^^
GTZZ?
MM/4::??w?O?ORU_bde++-
;			gflEBB__ekkmjj . GN}}W  zz)Q[[2DJJOORHjj7+G~~~^^ 11U
 #QY'--"RZ'..
//  ZZ,djjoorBDMM)eai6;;=4+H+H1+LL'-{{}t$]]))+66-..		 	 	" , 	4==(D )/*.--*=*=*?)/ %/	 
\\'9\55c	 	) 	LL!!S} B 		<<4cOO!4??#7#7#94::??L	*a  ;llzzQ((*GMM9	  ; ;;s   "#Q-2Q--
S7ASSc                 >   U=(       d    U R                   nUR                  5       nU R                  U5      nU(       aZ  [        R	                  SU R
                  R                  U[        U5      S:X  a  S[        UR                  5       5      S   -  OS5        UR                  5        Hc  u  pg [        R                  SU R
                  R                  Xd5        UR                  (       d  UR                  5         UR                  U5        Me     S
Ul        U R*                  (       a  U R*                  Ul        g! [         aY  n[        R!                  SU R
                  R                  XeU[        R#                  5       [$        R&                  :*  S9   S	nA  gS	nAff = f)zRepeat bans for the ticket.

Executes the actions in order to reban the host given in the
ticket.

Parameters
----------
ticket : Ticket
        Ticket to reban
z[%s] Reban %s%sr   z, action %rr   r   z[%s] action %r: reban %sz;Failed to execute reban jail '%s' action '%s' info '%r': %sr   NT)r   r   r%  rm   r<  r   r   rH   r   keysrG   r   r  r>  rebanr   r   r   r   r   r@  r   )	r!   r   rB   r   ry   r$  r   r7   r   s	            r#   __reBanActions.__reBan4  s0    $t}}'||~"


f
%%	=="DJJOORehipequvev-$w||~J^_`Ja:a|~  Ammold

LL+TZZ__dG??EKKM
LL	 & &-	]]]]6?	
  
LL	ZZ__d1&&('--7	  9
 s   .AD99
FAFFc                 Z   U R                   R                  U5      (       d  g S nU R                  R                  5        H  u  p4 UR                  (       a  [        USS5      (       a  M+  UR                  (       d  M>  Uc  U R                  U5      nUR                  (       d  UR                  5         UR                  U5        M     g ! [         aY  n[        R                  SU R                  R                  X2U[        R!                  5       ["        R$                  :*  S9   S nAM  S nAff = f)Nr/  Fr0  r   )r   
_inBanListr   rG   r   r=  _prolongabler%  r  r>  prolongr   rm   r   r   r   r   r   r   )r!   r   r$  r   r7   r   s         r#   _prolongBanActions._prolongBanV  s     
	#	#F	+	+V
%mm))+ld976<??}  (U??EKKM
NN5 ,  9
LL	ZZ__d1&&('--7	  99s&   #C)C<AC
D*AD%%D*c                 B   U R                   R                  [        R                  " 5       U5      nU H  nU R	                  U5        M     [        U5      nU(       aC  [        R                  SX@R                   R                  5       U R                  R                  5        U$ )zGCheck for IP address to unban.

Unban IP addresses which are outdated.
zUnbanned %s, %s ticket(s) in %r)r   	unBanListr   r   r   rH   rm   r   r   r   r   )r!   maxCountrx   r   r   s        r#   __checkUnBanActions.__checkUnBanm  sp    
 	!!&++-:#f<< C#	<<1			2	*r%   c           
         ^^ SnUc0  [         R                  S5        U R                  R                  5       nOSn[	        U R                  5      nSn0 nUb  UOU R
                  R                  5        H  u  nm [        TS5      (       ah  [        T[        5      (       a  TR                  (       aB  [         R                  SU R                  R                  U5        TR                  5       (       a  M  [         R                  S5        TXx'   M     UnU(       a[  U R                  R,                  bD  [         R                  S5        U R                  R,                  R/                  U R                  5        U H  nU R1                  XUS9  US-  nM     [         R                  SX`R                  R3                  5       U R                  R                  5        U$ ! [         a  n	[         R!                  SU R                  R                  X[         R#                  5       [$        R&                  :*  S	9  [         R)                  S
5        [        TS5      (       a  UU4S jn
TR+                  U
5         Sn	A	GM    Sn	A	GNSn	A	ff = f)zFlush the ban list.

Unban all IP address which are still in the banning list.

If actions specified, don't flush list - just execute unban for 
given actions (reload, obsolete resp. removed actions).
TNz  Flush ban listFr   flushz[%s] Flush ticket(s) with %sz1Failed to flush bans in jail '%s' action '%s': %sr   z'No flush occurred, do consistency checkr4  c                  d   > T(       a(  [        T SS 5      (       d  [        R                  S5        gg)Nactionrepair_on_unbanz,Invariant check failed. Flush is impossible.FT)r=  rm   r   )r7   rC   s   r#   _beforeRepair)Actions.__flushBan.<locals>._beforeRepair  s(    	gf&=tDD||BCr%   z!  Unban tickets each individuallyz  Flush jail in database)rB   r   r   z!  Unbanned %s, %s ticket(s) in %r)rm   r   r   flushBanListrZ   r   rG   r)   r   r   actionflushr<  r   r   r\  r   r   r   r   r   rn   r4  r   r   r   r   )r!   rA   rB   rC   r   rx   r   
unbactionsr   r   r_  r   r7   s      `        @r#   
__flushBanActions.__flushBan{  s    	#_	<<"#		%	%	'3	3	doo	3	#*")"5w4==OOQldFvwFM)J)JfN`N`]]14::??DI 
<<34:+ R, 'DJJ+	<<*+::djj)f<<S<1!83  	,,2

1	*9  
LLD	ZZ__d&&('--7  9 KK9:v)**
 ]+ +s   9A7G
I= BI88I=c                    Uc  U R                   nOUnUR                  5       nU R                  U5      nU(       a+  [        R	                  SU R
                  R                  U5        UR                  5        Hc  u  px [        R                  SU R
                  R                  Xu5        UR                  (       d  UR                  5         UR                  U5        Me     g! [         aY  n	[        R                  SU R
                  R                  XvU	[        R                  5       [        R                   :*  S9   Sn	A	M  Sn	A	ff = f)zUnbans host corresponding to the ticket.

Executes the actions in order to unban the host given in the
ticket.

Parameters
----------
ticket : FailTicket
        Ticket of failures of which to unban
Nz[%s] Unban %sz[%s] action %r: unban %sz;Failed to execute unban jail '%s' action '%s' info '%r': %sr   )r   r   r%  rm   r<  r   r   rG   r   r  r>  unbanr   r   r   r   r   )
r!   r   rB   r   rc  ry   r$  r   r7   r   s
             r#   __unBanActions.__unBan  s     _::||~"


f
%%	==$**//26 &&(ld	9
LL+TZZ__dG??EKKM
LL	 )
  9
LL	ZZ__d1&&('--7	  99s   <AC
E(AD<<Ec                    / SQnUb  X;  a  [         R                  SU< SU< 35        US:X  a4  U R                  R                  5       U R                  R	                  5       4$ US:w  a&  U R                  R                  5       n[        U5      nOU R                  R                  5       nSU4SU R                  R	                  5       4/nUS:w  a  USW4/-  nUS	:X  as  U R                  R                  5       nUS
U R                  R                  U5      4SU R                  R                  U5      4SU R                  R                  U5      4/-  nU$ )zSStatus of current and total ban counts and current banned IP list.
                )shortbasicstatscymruz(Unsupported extended jail status flavor z. Supported: rm  rk  zCurrently bannedzTotal bannedzBanned IP listrn  zBanned ASN listzBanned Country listzBanned RIR list)rm   warningr   r   r   rv   rH   getBanListExtendedCymruInfogeBanListExtendedASNgeBanListExtendedCountrygeBanListExtendedRIR)r!   flavorsupported_flavorsr@  r   ret
cymru_infos          r#   statusActions.status  s?    ;^v6	>>QWYjklw??!4??#>#>#@
AAwOO&&(6	V3				3
c	"t2245	7#wf%
&&3w;;=:==jIJT__EEjQR==jIJLL3 
*r%   )__lastConsistencyCheckTMr   r   r5   r   r   r   r    )NNF)Tr  )NTFr=   )d   r   )FNF)rl  )$r,   r  r  r  __doc__r   staticmethodr/   r9   r2   rT   rW   r\   r_   rd   rh   rl   rr   rz   rv   r   r   rJ   r   r   r"  r%  r8  r   rE  rT  r   rI   r   rx  r!  rE   r%   r#   r   r   2   s    6. 	 	,\%*44'&/E"7tC 1fZ<* Z<zSj D9.4l9>r%   r   )"
__author____copyright____license__r   ossysr   collections.abcr   ImportErrorcollectionsr   
banmanagerr   r   ipdnsr	   
jailthreadr
   r7   r   r   r   mytimer   observerr   utilsr   helpersr   r,   rm   r   rE   r%   r#   <module>r     sz   . 
2  	 
 !$ $ -  " 9 9     
8	w
j' w
#  ! !s   A/ /A>=A>