
    =*fL                     :   S r SrSrSSKrSSKrSSKJr  SSKJrJ	r	J
r
JrJrJr  SSKJr  SS	KJr  S
SKJr  \" \5      rSr\R.                  " S\R0                  5      r\R4                  " SSS9rSS jrS 4S jr " S S\5      r " S S\5      r  " S S\5      r!g)z'Cyril Jaquier and Fail2Ban Contributorsz Copyright (c) 2004 Cyril JaquierGPL    N)Lock   )reDateTemplateDatePatternRegex
DateTai64n	DateEpochRE_EPOCH_PATTERN)validateTimeZone)Utils   )	getLogger   z(?<!\\)\{DATE\}i  i  )maxCountmaxTimec                    Uc  U nSU ;  a  U R                  5       n[        R                  U5      nU(       d{  SU;   aO  [        R                  " U 5      (       a  [        U SU;   S9nO&US;   a  [        US:g  S9nOUS;   a  [        US:g  SS	9nUc#  US
;   a  [        US:w  a  SOSS9nO[        U 5      n[        R                  X5        U$ )N%EPOCHLEPOCH)patternlongFrm)r   z{^LN-BEG}EPOCHz^EPOCH)lineBeginOnly)r   z{^LN-BEG}LEPOCHz^LEPOCHT)r   r   )TAI64Nz{^LN-BEG}TAI64Nz^TAI64Nr   startF)	wordBegin)	upperDD_patternCachegetr   searchr
   r	   r   set)r   keytemplates      >/usr/lib/python3/dist-packages/fail2ban/server/datedetector.py_getPatternTemplater%   ,   s    K#	3$^g&&(c/BH44w8H77x$GH	55xWULH(HS#    c                     SU -   $ N	{^LN-BEG} )ss    r$   <lambda>r,   D   s    +/r&   c                 H   U" U R                   5      n[        R                  U5      nU(       ds  U" [        U SU R                  5      5      n[        U S5      (       a  [        R                  U5      nU(       d)  [        U S5      (       d  [        U5      nU$ [        U5      nU$ )Nr   )namer   r   getattrregexhasattrr%   )r#   wrapr.   	template2r0   s        r$   _getAnchoredTemplater4   D   s    X]]  &
wxHNN;
<%Xy!!""5)9	
(I
&
&#D)I 	 $E*Ir&   c                   R    \ rS rSrSrS r\S 5       rS r/ SQr	\S 5       r
S rS	rg
)DateDetectorCacheY   z>Implements the caching of the default templates list.
        c                 @    [        5       U l        [        5       U l        g N)r   _DateDetectorCache__locklist_DateDetectorCache__templatesselfs    r$   __init__DateDetectorCache.__init__\   s    $+V$r&   c                    U R                   (       a  U R                   $ U R                     U R                   (       a  U R                   sSSS5        $ U R                  5         U R                   sSSS5        $ ! , (       d  f       g= f)DList of template instances managed by the detector.
                N)r<   r:   _addDefaultTemplater=   s    r$   	templatesDateDetectorCache.templates`   sZ     



{{
 { 


	 {{s   A6A66
Bc                 R   UR                   nUR                  S5      (       dg  UR                  S5      (       dQ  [        US5      (       a@  [        U5      nUR                   U:w  a%  SUl        U R
                  S   R                  U5        U R
                  S   R                  U5        g)z4Cache Fail2Ban's default template.

                r)   ^r0   g      Y@r   r   N)r.   
startswithr1   r4   weight_DateDetectorCache__tmpcacheappend)r>   r#   r.   r3   s       r$   _cacheTemplate DateDetectorCache._cacheTemplatel   s     
$		%	%dooc.B.BwxY`GaGa#H-9nn IOOAi(//!H%r&   )zF%ExY(?P<_sep>[-/.])%m(?P=_sep)%d(?:T|  ?)%H:%M:%S(?:[.,]%f)?(?:\s*%z)?z)(?:%a )?%b %d %k:%M:%S(?:\.%f)?(?: %ExY)?z$(?:%a )?%b %d %ExY %k:%M:%S(?:\.%f)?z1%d(?P<_sep>[-/])%m(?P=_sep)(?:%ExY|%Exy) %k:%M:%Sz=%d(?P<_sep>[-/])%b(?P=_sep)%ExY[ :]?%H:%M:%S(?:\.%f)?(?: %z)?z%m/%d/%ExY:%H:%M:%Sz%m-%d-%ExY %k:%M:%S(?:\.%f)?r   z{^LN-BEG}%H:%M:%Sz^<%m/%d/%Exy@%H:%M:%S>z%Exy%Exm%Exd  ?%H:%M:%Sz%b %d, %ExY %I:%M:%S %pz^%b-%d-%Exy %k:%M:%Sz6%ExY%Exm%Exd(?:T|  ?)%ExH%ExM%ExS(?:[.,]%f)?(?:\s*%z)?z1(?:%Z )?(?:%a )?%b %d %k:%M:%S(?:\.%f)?(?: %ExY)?z1(?:%z )?(?:%a )?%b %d %k:%M:%S(?:\.%f)?(?: %ExY)?r   c                     [        [        R                  S   [        5      (       a@  [	        [        R                  5       H#  u  p[        U5      nU[        R                  U'   M%     [        R                  $ )Nr   )
isinstancer6   DEFAULT_TEMPLATESstr	enumerater%   )r>   idts      r$   defaultTemplates"DateDetectorCache.defaultTemplates   s\    !33A6<<+==>uq	R	 B-/''* ? 
	,	,,r&   c                     / / 4U l         U R                   H  nU R                  U5        M     U R                   S   U R                   S   -   U l        U ? g)zJAdd resp. cache Fail2Ban's default set of date templates.
                r   r   N)rJ   rU   rL   r<   )r>   rT   s     r$   rC   %DateDetectorCache._addDefaultTemplate   sS     F$/!!br " __Q'$//!*<<$
or&   )__lock__templates
__tmpcacheN)__name__
__module____qualname____firstlineno____doc__r?   propertyrD   rL   rP   rU   rC   __static_attributes__r*   r&   r$   r6   r6   Y   sG     	 	&$3j - -	r&   r6   c                   8    \ rS rSrSrSrS r\S 5       rS r	Sr
g)	DateDetectorTemplate   zrUsed for "shallow copy" of the template object.

Prevents collectively usage of hits/lastUsed in cached templates
r#   hitslastUseddistancec                 :    Xl         SU l        SU l        SU l        g )Nr   rf   )r>   r#   s     r$   r?   DateDetectorTemplate.__init__   s    -$)$-$-r&   c                 v    U R                   U R                  R                  -  [        SU R                  5      -  $ )Nr   )rg   r#   rI   maxri   r=   s    r$   rI   DateDetectorTemplate.weight   s+    	T]]))	)C4==,A	AAr&   c                 .    [        U R                  U5      $ )zSReturns attribute of template (called for parameters not in slots)
                )r/   r#   )r>   r.   s     r$   __getattr__ DateDetectorTemplate.__getattr__   s     
	%%r&   )ri   rg   rh   r#   N)r\   r]   r^   r_   r`   	__slots__r?   ra   rI   rq   rb   r*   r&   r$   rd   rd      s.     : B B&r&   rd   c                       \ rS rSrSr\" 5       rS rSS jrS r	SS jr
\S 5       rS	 r\S
 5       r\R                  S 5       rSS jrS rSrg)DateDetector   zfManages one or more date templates to find a date within a log line.

Attributes
----------
templates
c                     [        5       U l        [        5       U l        SU l        SU l        SU l        SU l        SU l        S U l	        S U l
        g )Ni,  )r   N)rk   Nrk   r   )r;   _DateDetector__templatesr!   _DateDetector__known_names_DateDetector__unusedTime_DateDetector__lastPos_DateDetector__lastEndPos_DateDetector__lastTemplIdx_DateDetector__firstUnused_DateDetector__preMatch_DateDetector__default_tzr=   s    r$   r?   DateDetector.__init__   sL    V$u$$$.&$"$$$/$r&   c                     UR                   nX0R                  ;   a  U(       a  g [        SU-  5      eU R                  R                  U5        U R                  R                  [        U5      5        g )Nz(There is already a template with name %s)r.   ry   
ValueErroraddrx   rK   rd   )r>   r#   	ignoreDupr.   s       r$   _appendTemplateDateDetector._appendTemplate   s_    	$		.5
7 7.x89r&   c                 B  ^ [        U[        5      (       a  U=mnSU;  a  UR                  5       m[        R	                  T5      nU(       dT  TS;   a  U4S jnU R                  U5        gST;   a  U R                  USS9  gTS:X  a  [        S	T5      nO[        UT5      n[        R                  TU5        U R                  U5        [        R                  S
[        USS5      UR                  5        [        R                  S[        USS5      UR                  5        g)a  Add a date template to manage and use in search of dates.

Parameters
----------
template : DateTemplate or str
        Can be either a `DateTemplate` instance, or a string which will
        be used as the pattern for the `DatePatternRegex` template. The
        template will then be added to the detector.

Raises
------
ValueError
        If a template already exists with the same name.
r   )r)   z	{DEFAULT}c                 N   > TS:X  a  U R                   [        R                  -  $ S $ r(   )flagsr   
LINE_BEGIN)r#   r"   s    r$   r,   -DateDetector.appendTemplate.<locals>.<lambda>  s$    3+CUx~~(?(??_[__r&   Nz{DATE}F)preMatchallDefaultsz{NONE}z{UNB}^z  date pattern `%r`: `%s`r    z  date pattern regex for %r: %s)rO   rQ   r   r   r   addDefaultTemplater%   r!   r   logSysinfor/   r.   debugr0   )r>   r#   r   fltr"   s       @r$   appendTemplateDateDetector.appendTemplate   s     #3	
--/C!!#&8

((_ 		S!	S	g5A	#Hc2X#GS1XsH%x ++)
8Y#X]]4,,0
8Y#X^^5r&   Nc                   ^ [        U R                  5      S:  nU(       a  [        R                  R                  O[        R                  R
                   H5  nUb  U" U5      (       d  M  Tb  [        UU4S jS9nU R                  XTS9  M7     g)z>Add Fail2Ban's default set of date templates.
                r   Nc                 :   >^  [         R                  U 4S jT5      $ )Nc                 0   > [         R                  " T5      $ r9   )r   unboundPattern)mr+   s    r$   r,   CDateDetector.addDefaultTemplate.<locals>.<lambda>.<locals>.<lambda>7  s    <3N3Nq3Qr&   )RE_DATE_PREMATCHsub)r+   r   s   `r$   r,   1DateDetector.addDefaultTemplate.<locals>.<lambda>7  s    $(()QS[\r&   )r2   )r   )lenrx   ru   	_defCacherD   rU   r4   r   )r>   filterTemplater   r   r   r#   s     `   r$   r   DateDetector.addDefaultTemplate*  s     $""#a')'2<##8N8N8_8__   )A)A8#H
\^H 6 `r&   c                     U R                   $ )rB   )rx   r=   s    r$   rD   DateDetector.templates;  s     
		r&   c                    [        U R                  5      (       d  U R                  5         [        R	                  5       [
        ::  a  [        R                  OS nU" [
        S-
  SU5        SnSnSnU R                  nU[        U R                  5      :  Ga  U R                  U   nUR                  nUR                  [        R                  [        R                  -  -  (       a&  U" [
        S-
  SU5        UR                  U5      nUnGOGU R                  S   U R                  S   pU" [
        S-
  S	XiU
XS-
  U	 U R                  S   XU
 XU
S-    U R                  S
   5
        XS-
  U	 U R                  S   :X  d:  XU	S-    U R                  S
   :X  a  U R                  S
   R!                  5       (       dx  XU
S-    U R                  S
   :X  d:  XS-
  U
 U R                  S   :X  aH  U R                  S   R!                  5       (       d&  U" [
        S-
  SXU
 5        UR                  XU
5      nO#U" [
        S-
  S5        UR                  U5      nUnU(       a  UR#                  5       n	UR%                  5       n
[        U R                  5      S:X  dW  UR                  [        R                  [        R                  -  -  (       d$  XR                  S   :X  a!  XR                  S   :X  a  U" [
        SU5        O!U" [
        S5        X9X4nSnOU" [
        S5        U(       Gd  U" [
        S[        U R                  5      5        SnU R                   GH  nXe:X  a  US-  nM  U" [
        S-
  SXgR&                  5        UR                  nUR                  U5      nU(       Ga/  UR#                  5       n	UR%                  5       n
U" [
        SXiUR(                  U R                  S   UR&                  5        US-   [        U R                  5      :  a    OUR                  [        R                  [        R                  -  -  (       a    OU	S:X  a>  UR*                  (       a-  U R                  US-      R                  R*                  (       d    OPXR(                  :  d  XR                  S   :  a#  U" [
        S5        XS   :  a  X9X4nSnUS-  nGM}    O
US-  nGM     U(       d8  US   (       a.  Uu  p9pU" [
        SU5        U R                  U   nUR                  nU(       a  W=R*                  S-  sl        [,        R,                  " 5       Ul        W	Ul        U R0                  U:X  a  U =R0                  S-  sl        XU	S-
  U	 X   4U l        W
XS-
     XU
S-    4U l        U(       a   X`R                  :w  a  U R3                  U5      nX`l        UW4$ U" [
        S5        g)a  Attempts to find date on a log line using templates.

This uses the templates' `matchDate` method in an attempt to find
a date. It also increments the match hit count for the winning
template.

Parameters
----------
line : str
        Line which is searched by the date templates.

Returns
-------
re.MatchObject, DateTemplate
        The regex match returned from the first successfully matched
        template.
c                      g r9   r*   )argss    r$   r,   (DateDetector.matchTime.<locals>.<lambda>V  s    PTr&   r   z"try to match time for line: %.120sN)Nrk   rk   rk   z/  try to match last anchored template #%02i ...r   zJ  try to match last template #%02i (from %r to %r): ...%r==%r %s %r==%r...r   z+  boundaries are correct, search in part %rz,  boundaries show conflict, try whole searchz"  matched last time template #%02izB  ** last pattern collision - pattern change, reserve & search ...z8  ** last pattern not found - pattern change, search ...z search template (%i) ...z  try template #%02i: %sz2  matched time template #%02i (at %r <= %r, %r) %sz1  ** distance collision - pattern change, reservez  use best time template #%02iz no template.)NN)r   rx   r   r   getEffectiveLevellogLevellogr}   r#   r   r   r   LINE_END	matchDater{   r|   isalnumr   endr.   ri   rg   timerh   r~   _reorderTemplate)r>   liner   matchfoundignoreBySearchrS   ddtemplr#   ri   endposs              r$   	matchTimeDateDetector.matchTimeA  s=   & 
T		..0H<

BT#hqj6= %
*%.
!T	a 78nn//0E0EEF
EqIt$EN~~a($*;*;A*>f
`&	1*Xq 1	6	D--a0	2 
1*X$.."33XaZ DNN1$55dnnQ>O>W>W>Y>Y	d//221HV 1 1! 44T=N=Nq=Q=Y=Y=[=[ 	!BDRXDYZ7U!CD%U^{{}HYY[F 			a^^|..|/D/DDE..##2C2CA2F(F7;WXf'UULM	x,c$2B2B.CD1""w!VQ
.<<@Ht$EXiikVG7##T^^A%6G 	
!s4##$$,11,2G2GGH	Q7<<1A1A!A#1F1O1O1T1T 	###x..2C'C	(GH	!H	)ue1fa
FAE #H E!H!&EV2A6q!GH

<<1<iik77
A!8A:h7G4>t1H~t6!8/DD4A$$$a A(
 h 	r&   c                     U R                   $ r9   )r   r=   s    r$   
default_tzDateDetector.default_tz  s    			r&   c                 $    [        U5      U l        g r9   )r   r   )r>   values     r$   r   r     s    &u-$r&   c                 V   Uc  U R                  U5      nUS   nUb}   UR                  XS   U R                  S9nUbZ  [        R	                  5       [
        ::  a<  [        R                  [
        SUS   US   R                  S5      UR                  5        U$  gg! [         a     gf = f)a  Attempts to return the date on a log line using templates.

This uses the templates' `getDate` method in an attempt to find
a date. 
For the faster usage, always specify a parameter timeMatch (the previous tuple result
of the matchTime), then this will work without locking and without cycle over templates.

Parameters
----------
line : str
        Line which is searched by the date templates.

Returns
-------
float
        The Unix timestamp returned from the first successfully matched
        template or None if not found.
Nr   r   )r   z&  got time %f for %r using template %s)
r   getDater   r   r   r   r   groupr.   r   )r>   r   	timeMatchr#   dates        r$   getTimeDateDetector.getTime  s    ( ~~d#9q\(	DA,4;L;LMD  "h.jjCAwQa (--1[	  
  			s   A9B 
B('B(c                 N  ^^^^^^ T(       Ga  U R                   mTT   m[        R                  5       [        ::  a&  [        R	                  [        STTR
                  5        TR                  U R                  -
  mTR                  mU R                  T:  a  U R                  OTS-  mUUUUUU4S jnU" 5       (       d  TTS-
  :X  a  T$ TS-
  mU" 5       (       d  T$ TT	 T/TTS& U R                  [        T5      :  al  TU R                     R
                  (       aN  U =R                  S-  sl	        U R                  [        T5      :  a   TU R                     R
                  (       a  MN  [        R                  5       [        ::  a  [        R	                  [        STT5        T$ T$ )zReorder template (bubble up) in template list if hits grows enough.

Parameters
----------
num : int
        Index of template should be moved.
z%  -> reorder template #%02i, hits: %rr   c                    > TT   R                   n [        R                  5       [        ::  a7  [        R	                  [        STTTU TR
                  TT   R
                  5        TU :  =(       d    TTT   R                  :  $ )NzE  -> compare template #%02i & #%02i, weight %.3f > %.3f, hits %r > %r)rI   r   r   r   r   rg   rh   )pweightr   numposrD   untimerI   s    r$   	_moveable0DateDetector._reorderTemplate.<locals>._moveable  so    n##G!X-ZZa	3y~/B/BDG?v	#(?(???r&   r   r   z"  -> moved template #%02i -> #%02i)rx   r   r   r   r   rg   rh   rz   rI   r~   r   )r>   r   r   r   r   rD   r   rI   s    ` @@@@@r$   r   DateDetector._reorderTemplate  s^    	9s^7 H,
JJx@#w||Tt0006NN6#11C7		SAX3@ @ ++
c!e|Z
a%C;;Z~i9S			c)n	,4;M;M1N1S1S! 
		c)n	,4;M;M1N1S1S1S H,
JJx=sCH
:	*r&   )	__default_tz__firstUnused__known_names__lastEndPos	__lastPos__lastTemplIdx
__preMatchrZ   __unusedTime)F)NNTr9   )r\   r]   r^   r_   r`   r6   r   r?   r   r   r   ra   rD   r   r   setterr   r   rb   r*   r&   r$   ru   ru      sz       :(5T7"  
JX   . ."H-r&   ru   r9   )"
__author____copyright____license__copyr   	threadingr   datetemplater   r   r   r	   r
   r   strptimer   utilsr   helpersr   r\   r   r   compile
IGNORECASEr   Cacher   r%   r4   objectr6   rd   ru   r*   r&   r$   <module>r      s   ( 7
2     &   
8	::0"--@ ++tU;0 )B *k k\&6 &.H6 Hr&   