
    ogf@w                        d Z dZdZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZ 	 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZmZmZmZm Z   ee!      Z" e	jF                         Z$ejJ                  Z& ejN                  d      Z(dZ) ejN                  d      Z*d d dZ+dddZ,e,j[                  e+        G d dee.      Z/ G d de.e      Z0dddZ1 G d d e0      Z2y# e$ r	 ddlmZ Y w xY w)!z'Cyril Jaquier and Fail2Ban Contributorsz>Copyright (c) 2004 Cyril Jaquier, 2011-2012 Yaroslav HalchenkoGPL    N)ABCMeta)MutableMapping   )
mapTag2Opt)DNSUtils)MyTime)Utils   )	getLogger_merge_copy_dicts
splitwordssubstituteRecursiveTags
uni_stringTAG_CREMAX_TAG_REPLACE_COUNTz<F-([A-Z0-9_\-]+)>)inet4inet6z^(\w+)\?(family)=(.*)$c                  @    t        t        j                  d            S )NTfqdnstrr   getHostname     8/usr/lib/python3/dist-packages/fail2ban/server/action.py<lambda>r   ?   s    H00d;< r   c                  @    t        t        j                  d            S )NFr   r   r   r   r   r   r   @   s    H00e<= r   )zfq-hostnamezsh-hostname
 )brspc                   j    e Zd ZdZdZdZd ZddZddZeZ	ddZ
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)
CallingMapa"  A Mapping type which returns the result of callable values.

	`CallingMap` behaves similar to a standard python dictionary,
	with the exception that any values which are callable, are called
	and the result is returned as the value.
	No error handling is in place, such that any errors raised in the
	callable will raised as usual.
	Actual dictionary is stored in property `data`, and can be accessed
	to obtain original callable values.

	Attributes
	----------
	data : dict
		The dictionary data which can be accessed to obtain items uncalled
	r   )datastorage	immutable
__org_datac                 P    t               | _        d| _        t        |i || _        y )NT)dictr'   r(   r&   )selfargskwargss      r   __init__zCallingMap.__init___   s#    $,$.D#F#$)r   c                     t               | _        	 | j                  | _        || _        y # t        $ r
 Y || _        y w xY wN)r+   r'   _CallingMap__org_datar&   AttributeErrorr(   )r,   r(   s     r   resetzCallingMap.resetd   s?    $,49 $. 
 $.s   * 	==c                 b    | j                   j                  d| j                  |t              dS )N())	__class____name___asdictr   )r,   
calculateds     r   _asreprzCallingMap._asreprl   s"    ^^,,dll:s.K	LLr   Nc                 :    t         j                  fi  j                  }|s"t         fd|j                         D              S t	        |j                               D ]2  \  }}t        |      s	  j                  |      }|r ||       |||<   4 |S #  Y ;xY w)Nc              3   ^   K   | ]$  \  }}t        |      r|j                  v r||f & y wr1   )callableCM_REPR_ITEMS).0nvr,   s      r   	<genexpr>z%CallingMap._asdict.<locals>.<genexpr>u   s4      31A;!t111 !u 3s   *-)r+   r&   r'   itemslistr?   __getitem__)r,   r;   checkerdrB   rC   s   `     r   r:   zCallingMap._asdictr   s    
499%%!	
 3	 3 3 3!'')_ 

caqk
			!	QQqT

 
(
	s   2 BBc                 d    	 | j                   |   }|S # t        $ r | j                  |   }Y |S w xY wr1   )r'   KeyErrorr&   r,   keyvalues      r   
getRawItemzCallingMap.getRawItem   s?    <<5 
, 
 99S>5	,s    //c                     	 | j                   |   }t        |      r@t	        |d      r|j
                  j                  r ||       n |       }|| j                   |<   |S # t        $ r | j                  |   }Y gw xY w)N__code__)r'   rK   r&   r?   hasattrrQ   co_argcountrL   s      r   rG   zCallingMap.__getitem__   ss    <<5 e_!%49S9S5;Y^Y`54<<	, 
 99S>5s   A A98A9c                     | j                   rV| j                  j                         | _        | j                  | _        | j                  j                         | _        d| _         || j                  |<   y NF)r(   r'   copyr&   r2   rL   s      r   __setitem__zCallingMap.__setitem__   sO    	^^,,##%4<YY4?yy~~494>$,,sr   c                     t        d|z        )NzKey %r was deleted)rK   r,   rM   s     r   __unavailablezCallingMap.__unavailable   s    %+,,r   c                    | j                   rV| j                  j                         | _        | j                  | _        | j                  j                         | _        d| _         	 | j                  |= | j                  |= y # t
        $ r Y w xY wrU   )r(   r'   rV   r&   r2   rK   rY   s     r   __delitem__zCallingMap.__delitem__   ss    	^^,,##%4<YY4?yy~~494>||C iin 
 s   $A? ?	B
Bc                 ,    t        | j                        S r1   )iterr&   r,   s    r   __iter__zCallingMap.__iter__   s    	diir   c                 ,    t        | j                        S r1   )lenr&   r_   s    r   __len__zCallingMap.__len__   s    	TYYr   c                 `    | j                  t        | j                  | j                              S r1   )r8   r   r&   r'   r_   s    r   rV   zCallingMap.copy   s!    	)$))T\\B	CCr   )T)F)FN)r9   
__module____qualname____doc__r@   	__slots__r/   r4   r<   __repr__r:   rO   rG   rW   _CallingMap__unavailabler\   r`   rc   rV   r   r   r   r%   r%   J   sZ       <$
M $	-Dr   r%   c                   T    e Zd ZdZed        Zd Zd Zd Zd Z	d Z
ed        Zd	 Zy
)
ActionBasea  An abstract base class for actions in Fail2Ban.

	Action Base is a base definition of what methods need to be in
	place to create a Python based action for Fail2Ban. This class can
	be inherited from to ease implementation.
	Required methods:

	- __init__(jail, name)
	- start()
	- stop()
	- ban(aInfo)
	- unban(aInfo)

	Called when action is created, but before the jail/actions is
	started. This should carry out necessary methods to initialise
	the action but not "start" the action.

	Parameters
	----------
	jail : Jail
		The jail in which the action belongs to.
	name : str
		Name assigned to the action.

	Notes
	-----
	Any additional arguments specified in `jail.conf` or passed
	via `fail2ban-client` will be passed as keyword arguments.
	c                 F    d}|D ]  }t        t        ||d             r y y)N)startstopbanrebanunbanFT)r?   getattr)clsCrequiredmethods       r   __subclasshook__zActionBase.__subclasshook__   s3    (  f
71fd+
, 
r   c                 n    || _         || _        t        d| j                  j                  z        | _        y )Nzfail2ban.%s)_jail_namer   r8   r9   _logSys)r,   jailnames      r   r/   zActionBase.__init__   s+    $*$*=4>>+B+BBC$,r   c                      y)z,Executed when the jail/action is started.
		Nr   r_   s    r   rn   zActionBase.start        r   c                      y)z,Executed when the jail/action is stopped.
		Nr   r_   s    r   ro   zActionBase.stop   r   r   c                      y)Executed when a ban occurs.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		Nr   r,   aInfos     r   rp   zActionBase.ban        r   c                 $    | j                  |      S )r   )rp   r   s     r   rq   zActionBase.reban   s     
%r   c                      yrU   r   r_   s    r   _prolongablezActionBase._prolongable  s    	r   c                      y)zExecuted when a ban expires.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		Nr   r   s     r   rr   zActionBase.unban  r   r   N)r9   re   rf   rg   classmethodrx   r/   rn   ro   rp   rq   propertyr   rr   r   r   r   rl   rl      sN    <  D


		  	r   rl   )	metaclassstr2secondsignore)timeoutbantimec                       e Zd ZdZ ed      Zd Z fdZed        Z	d Z
e
Zd Zed        Zed	        Zd
 Zd Zg dfdZed        Zed        Zed        Zd Zd(dZd)dZed        Zd Zd Zd Zd Zd Zd*dZd Z d*dZ! e"jF                  d      Z$ed        Z%ed+d       Z& e"jF                  d       Z$ e"jF                  d!      Z'ed*d"       Z(ed#        Z)d$ Z*d,d%Z+d*d&Z,e-d-d'       Z. xZ/S ).CommandActiona  A action which executes OS shell commands.

	This is the default type of action which Fail2Ban uses.

	Default sets all commands for actions as empty string, such
	no command is executed.

	Parameters
	----------
	jail : Jail
		The jail in which the action belongs to.
	name : str
		Name assigned to the action.

	Attributes
	----------
	actionban
	actioncheck
	actionreban
	actionreload
	actionrepair
	actionstart
	actionstop
	actionunban
	timeout
	)matches	ipmatchesipjailmatchesc                     d| _         	 d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _         y# d| _         w xY w)z8 Clear all lists/dicts parameters (used by reloading)
		r   <    r   N)_CommandAction__initr   actionstart	actionbanactionrebanactionunbanactioncheckactionrepairactionflush
actionstopactionreloadr_   s    r   clearAllParamszCommandAction.clearAllParams>  sl     $+4<44>444444?44;4;s   AA 	A c                     t         t        |   ||       d| _        d | _        i | _        i | _        | j                          | j                  j                  d| j                  z         y )Nr   z
Created %s)superr   r/   r   _CommandAction__properties_CommandAction__started_CommandAction__substCacher   r|   debugr8   )r,   r}   r~   r8   s      r   r/   zCommandAction.__init__X  sY    t%dD1$+$$.$,,\DNN23r   c                     t         S r1   )NotImplemented)rt   ru   s     r   rx   zCommandAction.__subclasshook__a  s    	r   c                 X   |j                  d      s| j                  s~t        |      sst        j	                  |      }|dk(  ry |dk(  rt        j                  |      }d | _        | j                  j                          | j                  j                  d||       || j                  |<   y )N_r   r   z  Set %s = %r)
startswithr   r?   WRAP_CMD_PARAMSgetr	   r   r   r   clearr|   r   __dict__)r,   r~   rN   wrps       r   __setattr__zCommandAction.__setattr__e  s    		dkk(5/			T	"3	Xo
}u%E4<<otU3$--r   c                     |j                  d      s=d | _        | j                  j                          | j                  j                  d|       | j                  |= y )Nr   z
  Unset %s)r   r   r   r   r|   r   r   )r,   r~   s     r   __delattr__zCommandAction.__delattr__w  sH    		4<<lD)
mmDr   c                       j                    j                   S t         fdt               D               _          j                   S )z`A dictionary of the actions properties.

		This is used to substitute "tags" in the commands.
		c              3      K   | ]8  }|j                  d       s%t        t        |            s|t        |      f : yw)r   N)r   r?   rs   )rA   rM   r,   s     r   rD   z,CommandAction._properties.<locals>.<genexpr>  s?      

..
hwtS/A&B s	s   >A)r   r+   dirr_   s   `r   _propertieszCommandAction._properties  sI     
"


 $i $
 
		r   c                     | j                   S r1   )r   r_   s    r   _substCachezCommandAction._substCache  s    			r   c                     | j                  || j                  |rd|z   nd| j                        }|rd|vr|S | j                  |d|i      }|S )Nfamily=r   conditionalcache<family)
replaceTagr   r   replaceDynamicTags)r,   tagr   cmds       r   _getOperationzCommandAction._getOperation  sa     	T--$*	& 			 	 	# 
3c>#:hv%67#	*r   c                 T   d|f}t        |      s|t        |      s+| j                  j                  |i       j                  |      S | j                  j                  |i       j	                         D cg c]  \  }} ||      s| c}}S |d   }|r	 | j                  |   }|||<   y	 | j                  |   }|j                  |      }t        |j	                               D ]  \  }}||k(  s||=  yc c}}w # t
        $ r i x}| j                  |<   Y tw xY w# t
        $ r Y yw xY w)z? Get, set or delete command of operation considering family.
		__eOpCmdr   N)rb   r?   r   r   rE   rK   poprF   )	r,   r   r   r-   rM   frC   r   famds	            r   _operationExecutedz CommandAction._operationExecuted  s2    
C#	T
6
  b)--f55**..sB7==?
MA6!91
MMQ#'S!D 4<	S!D
((6
C$**,' 		S
v, N
  '$&&D4S!'  		s7   3C5C5C; *AD /D ;DD	D'&D'Nc           
      L   d}d}|s/| j                   j                         D cg c]
  \  }}|s	| }}}|D ]  	 | j                  |      }	d}
|	r|	| j                  |fd      vr|	}| j                  rt        | j                  j                  dd      }|sF| j                  j                  j                  d      }t        | j                  j                  d|       t        j                         |d<   |d<   | j                  |	|      }| j                  || j                        }
||
z  }|r	 ||
       | j                  ||
r|	nd        |s+t        d	|d
| j                  d| j                   d|      |S c c}}w # t        $ r}d}|}Y d}~ed}~ww xY w)zExecutes the operation commands (like "actionstart", "actionstop", etc).

		Replace the tags in the action command with actions properties
		and executes the resulting command.
		TzScript errorc                     | k7  S r1   r   )r   famopers    r   r   z1CommandAction._executeOperation.<locals>.<lambda>  s    g r   
actionInfoNtimer   FzError z action /z: )r   rE   r   r   rz   rs   actions_getActionInfosetattrr	   r   r   
executeCmdr   
ValueErrorRuntimeErrorr{   )r,   r   	operationr   	afterExecreserrr   rC   r   retrealCmdr   es          `      r   _executeOperationzCommandAction._executeOperation  s    	##	(,(<(<(>D'!!WD6D g


S'
*C
C
s$11#7MNNW

djj((,=ezz!!006utzz!!<7kkmeFmeHo''U3g??7DLL1SCZS)GS)C#$?)0 
	Y

DJJX[\	]]	*7 E,  
C
Cs"   
FFDF	F#FF#c                     | j                   j                  d      }||S d}| j                   D ]  }t        j                  |      sd} n || j                   d<   |S )N__hasCondSectionFT)r   r   CONDITIONAL_FAM_REmatch)r,   rC   rB   s      r   _hasCondSectionzCommandAction._hasCondSection  sk    
-.!]
8! 
aq!A	
 *+$%&	
(r   c                 "   | j                   j                  d      }|r|S | j                   j                  d      }|r"t        |t        t        f      st        |      }n!| j                  rt               rddgndg}ndg}|| j                   d<   |S )N
__familiesfamiliesr   r   r   )r   r   
isinstancerF   setr   r   allowed_ipv6r,   rC   s     r   	_familieszCommandAction._families  s    
<(!q
:&!z!d3Z(!}1)^'1
t1#$$< 	
(r   c                 z    | j                   j                  d      }||S | j                  }|| j                   d<   |S )z1Checks the action depends on family (conditional)actionstart_on_demand)r   r   r   r   s     r   _startOnDemandzCommandAction._startOnDemand  sF     23!]
8
!./$*+	
(r   c                 "    | j                         S )Executes the "actionstart" command.

		Replace the tags in the action command with actions properties
		and executes the resulting command.
		)_startr_   s    r   rn   zCommandAction.start	  s     
r   c                       j                   r|sy|s j                  j                  |      ry||gn j                  } fd} j	                  dd||      }|S )r   Tc                 P    |r#j                  d| d        dj                  | <   y y )N<actionstop>r   )r   r   r   r   r,   s     r   _startedz&CommandAction._start.<locals>._started  s+    	NFD9DNN6 
r   <actionstart>startingr   r   )r   r   r   r   r   )r,   r   
forceStartr   r   s   `    r   r   zCommandAction._start  sc     

$..,,V4
)F8t~~& 	
6U]^#	*r   c                 4   |j                  dd      }| j                  r.| j                  j                  |      s| j                  |d       | j	                  ||      st        d|z        | j                  j                  |d      dz  | j                  |<   y)	a)  Executes the given command ("actionban" or "actionreban").

		Replaces the tags in the action command with actions properties
		and ban information, and executes the resulting command.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		r   r   Tr   zError banning %(ip)sr      N)r   r   r   r   _processCmdr   )r,   r   r   r   s       r   rp   zCommandAction.ban%  s     99Xr"&	
..

V
$KK4K(			#u	%	,u4	55>>--fa81<$..r   c                     t        | d      xr2 | j                  xr$ t        | j                        j                          S )Nactionprolong)rR   r  r   isspacer_   s    r   r   zCommandAction._prolongable;  s=    
$
( -T-?-? -t!!"**,,.r   c                 D    | j                  d|      st        d|z        y)a  Executes the "actionprolong" command.

		Replaces the tags in the action command with actions properties
		and ban information, and executes the resulting command.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		z<actionprolong>zError prolonging %(ip)sN)r  r   r   s     r   prolongzCommandAction.prolong@  s+     
		+U	3	/%7	88 
4r   c                     |j                  dd      }| j                  j                  |d      dz  r!| j                  d|      st        d|z        yy)a  Executes the "actionunban" command.

		Replaces the tags in the action command with actions properties
		and ban information, and executes the resulting command.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		r   r   r   r   <actionunban>zError unbanning %(ip)sN)r   r   r  r   )r,   r   r   s      r   rr   zCommandAction.unbanO  sX     99Xr"&	^^"Q&


?E
2
/%7
88 3 'r   c                 J    | j                  || j                  rd      S d      S )aD  Executes the "actionreban" command if available, otherwise simply repeat "actionban".

		Replaces the tags in the action command with actions properties
		and ban information, and executes the resulting command.

		Parameters
		----------
		aInfo : dict
			Dictionary which includes information in relation to
			the ban.
		z<actionreban><actionban>)rp   r   r   s     r   rq   zCommandAction.reban`  s%     
%D,<,<	PP-	PPr   c                       j                   j                         D cg c]  \  }}|dz  dk(  s| }}}|sy fd} j                  dd||      S c c}}w )a  Executes the "actionflush" command.
		
		Command executed in order to flush all bans at once (e. g. by stop/shutdown 
		the system), instead of unbanning of each single ticket.

		Replaces the tags in the action command with actions properties
		and executes the resulting command.
		r  Tc                 r    |r4j                   j                  |       rj                   | xx   dz  cc<   y y y )N)r   r   r   s     r   _afterFlushz(CommandAction.flush.<locals>._afterFlush}  s2    	dnn  (NN6b  )cr   z<actionflush>flushingr   )r   rE   r   )r,   r   rC   r   r  s   `    r   flushzCommandAction.flusho  s`      >>//1@%1QQUaZA@&@	" 
		FVa		bb As
   AAc                 "    | j                         S )Executes the "actionstop" command.

		Replaces the tags in the action command with actions properties
		and executes the resulting command.
		)_stopr_   s    r   ro   zCommandAction.stop  s     
r   c                     |: j                   j                         D cg c]
  \  }}|s	| }}}|syi  _         n	  j                   |xx   dz  cc<   |g} fd} j                  dd||      S c c}}w # t        $ r Y yw xY w)r  Tr   c                 2    |rj                  d| d        y y )Nr   )r   r   s     r   _stoppedz%CommandAction._stop.<locals>._stopped  s    	OVT: 
r   r   stoppingr   )r   rE   rK   r   )r,   r   r   rC   r  s   `    r   r  zCommandAction._stop  s     ^ NN00285AaaQ868
4>NN6aXF; 
		
6U]		^^ 9  s   
A3A3A9 9	BBc                 &    | j                  dd      S )zExecutes the "actionreload" command.

		Parameters
		----------
		kwargs : dict
		  Currently unused, because CommandAction do not support initOpts

		Replaces the tags in the action command with actions properties
		and executes the resulting command.
		z<actionreload>	reloading)r   )r,   r.   s     r   reloadzCommandAction.reload  s     
		 0+	>>r   c                     d}| j                   rht        | j                  j                               D ]B  \  }}|s	| j	                  ||      rd| j                  |<   | j                  d|d       |dz  }D |S )zFExecutes the invariant check with repair if expected (conditional).
		Tr   r   NF)r   rF   r   rE   _invariantCheckr   )r,   beforeRepairr   r   starteds        r   consistencyCheckzCommandAction.consistencyCheck  sz     	#	 !5!5!78 t++FLAT^^F	_fd;E\S 
*r   z[\\#&;`|*?~<>^()\[\]{}$'"\n\r]c                 T    dddfd}| j                   j                  ||      }|S )a5  Escape characters which may be used for command injection.

		Parameters
		----------
		value : str
			A string of which characters will be escaped.

		Returns
		-------
		str
			`value` with certain characters escaped.

		Notes
		-----
		The following characters are escaped::

			\#&;`|*?~<>^()[]{}$'"


		rB   r)r    c                 N    | j                         }dj                  ||      z   S )N\)groupr   )mc_map2cs     r   	substCharz*CommandAction.escapeTag.<locals>.substChar  s$    wwy1
Aq!
!!r   )
ESCAPE_CREsub)rt   rN   r+  r*  s      @r   	escapeTagzCommandAction.escapeTag  s1    * S!&" ..

Y
.%	,r   c                     d|vr|S |
|f}	 ||   S t        |t              }||s<dx}|dt        |      f}	 ||   t	        | j
                  |      |||<   t        t               fd}	t        dz   }
	 t        j                  |	|      }|rn%||k(  sd|vrn|}|
dz  }
|
dk  rt        d|      >|||<   |S # t         $ r Y w xY w# t         $ r Y w xY w)	zReplaces tags in `query` with property values.

		Parameters
		----------
		query : str
			String with tags.
		aInfo : dict
			Tags(keys) and associated values for substitution in query.

		Returns
		-------
		str
			`query` string with tags replaced.
		r   Nz
subst-tags)r   addreplc                    | j                  d      }d }rj                  |dz   z         }|3j                  |      }| j                  || j                               S t        |      }|j                  v rj	                  |      }|S )Nr   ?)r'  r   r   _escapedTagsr.  )r(  r   rN   ADD_REPL_TAGS_CMrt   r   subInfos      r   substValz*CommandAction.replaceTag.<locals>.substVal  s    	
35KKc	K/0EmKKE}  aggi00e5	S MM% E
<r   r   r   z_unexpected too long replacement interpolation, possible self referencing definitions in query: )rK   r   r%   idr   r3  ADD_REPL_TAGSr   r   r-  r   )rt   queryr   r   r0  r   ckey	noRecReplcsubkeyr6  countrN   r4  r5  s   `  `        @@r   r   zCommandAction.replaceTag  s_     	e| +
4	; 
+)'	 7WRY4G
W~W o%e[AQAQG E'N  .&  !
#%;;x'5un5(%5A:5qj
=BEF F 	 5;	,C  		  
	
s#   C  C 	CC	C*)C*z![\\#&;`|*?~<>\^\(\)\[\]{}$'"\n\r]z\Wc                 (    t               s fdt        t              fd}t        j                  ||      }d|v r1j                  d      si fd}t        j	                  ||      }rt        j                  |      }|S )a%  Replaces dynamical tags in `query` with property values.

		**Important**
		-------------
		Because this tags are dynamic resp. foreign (user) input:
		  - values should be escaped (using "escape" as shell variable)
		  - no recursive substitution (no interpolation for <a<b>>)
		  - don't use cache

		Parameters
		----------
		query : str
			String with tags.
		aInfo : dict
			Tags(keys) and associated values for substitution in query.

		Returns
		-------
		str
			shell script as string or array with tags replaced (direct or as variables).
		c                     j                   j                  |      r)dj                  j                  d|       z  } || <   d| z   }|S )Nzf2bV_%sr   $)r,  searchESCAPE_VN_CREr-  )r   rN   rt   varsDicts     r   	escapeValz3CommandAction.replaceDynamicTags.<locals>.escapeValS  sK    
~~U# s((,,S#66SXc]WULr   c                     | j                  d      }	 |   }t        |      } ||      S # t        $ r# j                  || j                               cY S w xY w)Nr   )r'  rK   r   r   )r(  r   rN   r4  r   rD  s      r   r6  z2CommandAction.replaceDynamicTags.<locals>.substVala  sa    	
30#JE e5
C
  0QWWY//0s   - )AAr   zF-*c                     t        | j                  d            }	 t        |         } d|z   |      S # t        $ r Y yw xY w)Nr   r   F_)r   r'  r   rK   )r(  r   rN   rD  tickDatas      r   substTagz2CommandAction.replaceDynamicTags.<locals>.substTags  sO    
QWWQZ
 C&U T#Xu%%  s   7 	AA)	r+   r%   r8  r   r-  r   FCUSTAG_CREr
   buildShellCmd)	rt   r   r   rD  r6  rI  r4  rH  rC  s	   ` ``  @@@r   r   z CommandAction.replaceDynamicTags8  s    0 V(		  .	  KK'*' 	G^ii8
rH& __Xw/7   (37	.r   c                     t        | dd      S )N	_banEpochr   )rs   r_   s    r   banEpochzCommandAction.banEpoch  s    	{A	&&r   c                     | j                   D| j                   j                  j                  dz   x| _        | j                   j                  _        y| j                  dz   | _        y)zIncrements ban epoch of jail and this action, so already banned tickets would cause
		a re-ban for all tickets with previous epoch.Nr   )rz   r   rN  rM  r_   s    r   invalidateBanEpochz CommandAction.invalidateBanEpoch  sM     
ZZ26**2D2D2M2MPQ2QQ4>DJJ&&/MMA%4>r   c                    |s||| j                   vry| j                  d|      }|r| j                  || j                        ry|r |       sy| j                  j                  d       | j                          | j                  d|      }|rW| j                  || j                        s+d| j                   |<   | j                  j                  d       yd| j                   |<   n4	 | j                  |       | j                  ||xs | j                          | j                   j                  |      r8| j                  || j                        s| j                  j                  d       yy# t        $ r Y w xY w)	z0Executes a substituted `actioncheck` command.
		r   z<actioncheck>z<Invariant check failed. Trying to restore a sane environmentz<actionrepair>r   zUnable to restore environmentr  )r   r   r   r   r|   errorrP  criticalr  r   r   r   r   )r,   r   r  r   checkCmd	repairCmds         r   r  zCommandAction._invariantCheck  sL    
*vT^^/K
8(	T__Xt||<
,.
,,AC   !16:)
//)T\\
2DNN6LL9:4>>&	JJv ;;v*"GD4G4G0G;H	^^$,,(O<<89
	
  		s   &E- -	E98E9c                     dk(  r j                   j                  d       y	 |d   }d}	 |r/ j                  r# fd} j                  ||dk7        }|d	k7  ry
 j                   j                  |rd|z   nd j                        }| j                  ||      }n} j                  | j                        }|d	z  }|s|d	kD  r|S # t        t        f$ r d}Y w xY w)a  Executes a command with preliminary checks and substitutions.

		Before executing any commands, executes the "check" command first
		in order to check if pre-requirements are met. If this check fails,
		it tries to restore a sane environment before executing the real
		command.

		Parameters
		----------
		cmd : str
			The command to execute.
		aInfo : dictionary
			Dynamic properties.

		Returns
		-------
		bool
			True if the command succeeded.
		r   Nothing to doTr   r   c                  ~     dk(  r7j                   j                  d      sj                  j                  d       yy)Nr
  actionrepair_on_unbanz,Invariant check failed. Unban is impossible.FT)r   r   r|   rS  )r   r,   s   r   _beforeRepairz0CommandAction._processCmd.<locals>._beforeRepair  s8    t'7'7';';<S'T
llGHr   r
  r  r   Fr   r   )r|   r   rK   	TypeErrorr   r  r   r   r   r   r   r   )r,   r   r   r   repcntr[  r   r   s   ``      r   r  zCommandAction._processCmd  s   ( 	BY<<o&
(O6 & !! 

v}#BX

ZC
ax __S$"2"2%+6!D<M<M  O7 %%gu5GG 
$,,	/3	Q;6	VaZJ= 		 I	 6s   C C$#C$c                    t         j                         t        j                  k  rt         j	                  d|        | st         j                  d       yt        5  t        j                  | |fddd|cddd       S # 1 sw Y   yxY w)aI  Executes a command.

		Parameters
		----------
		realCmd : str
			The command to execute.
		timeout : int
			The time out in seconds for the command.

		Returns
		-------
		bool
			True if the command succeeded.

		Raises
		------
		OSError
			If command fails to be executed.
		RuntimeError
			If command execution times out.
			   rX  TF)shelloutputN)	logSysgetEffectiveLevelloggingDEBUGlogr   	_cmd_lockr
   r   )r   r   r.   s      r   r   zCommandAction.executeCmd  sq    . '--/	::a		<< 
 Q


7G
P4
P
PQ Q Qs   A>>BrU   )r  r1   )r   NN)NNT)r   )0r9   re   rf   rg   r   r3  r   r/   r   rx   r   rW   r   r   r   r   r   r   r   r   r   r   rn   r   rp   r   r  rr   rq   r  ro   r  r  r!  recompiler,  r.  r   rB  r   rN  rP  r  r  staticmethodr   __classcell__)r8   s   @r   r   r      s   6 =>44         		4 57$ %N 
 
    (>, . .99"Qc&_.? bjj>? 8 W Wr bjjABE"G GR ' '&&P=~ Q Qr   r   )3
__author____copyright____license__rd  osrh  signal
subprocesstempfile	threadingr   abcr   collections.abcr   ImportErrorcollections	failregexr   ipdnsr   mytimer	   utilsr
   helpersr   r   r   r   r   r   r   r9   rb  Lockrg  IPv6IsAllowedr   ri  rJ  COND_FAMILIESr   DYN_REPL_TAGSr8  updateobjectr%   rl   r   r   r   r   r   <module>r     s2  ( 7
P  	 	      (+ "   Q Q Q 
8	 INN	 %% bjj./"RZZ 9: 
 == 	   ] #lD lD^_7 _F 
rQJ rQ{  ('(s   C0 0C>=C>