
    =*f'2                        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
  SS	KJr  \" \5      r\R                  " S
5      r\R                  " S5      S4r\R                  " S5      r\R                  " S5      r\R                  " S5      r\R                  " S5      r\R                  " S5      r\R                  " S5      r\R                  " S5      r\R                  " S5      S 4r\R                  " S5      r\R                  " S5      r\R                  " S5      r\R                  " S\R:                  5      r " S S\5      r  " S S\ 5      r! " S S\ 5      r" " S  S!\ 5      r#g)"zCyril Jaquierz Copyright (c) 2004 Cyril JaquierGPL    N)abstractmethod   )reGroupDictStrptimetimeREgetTimePatternRE   )	getLoggerz(?<!(?:\(\?))(?<!\\)\((?!\?)z-^((?:\(\?\w+\))?\^?(?:\(\?\w+\))?)(.*?)(\$?)$z\1(\2)\3z((?:^|(?!<\\))\(\?[a-z]+\))z^\{UNB\}z^\{\^LN-BEG\}z^\((?:\?:)?\^\|\\b\|\\W\)z\(\?=\\b\|\\W\|\$\)$z-^\(*(?:\(\?\w+\))?(?:\^|\(*\*\*|\((?:\?:)?\^)z!(?<!\\)(?:\$\)?|\\b|\\s|\*\*\)*)$z)^\(*(?:\(\?\w+\))?\(*\*\*|(?<!\\)\*\*\)*$c                 B    U R                  5       R                  SS5      $ )Nz** )groupreplace)ms    >/usr/lib/python3/dist-packages/fail2ban/server/datetemplate.py<lambda>r   /   s    qwwy00r:    z(^(?:\(\?\w+\))?(?:\^|\((?:\?:)?\^(?!\|))z(?<![\\\|])(?:\$\)?)$z(?<!\%)\%[aAbBpc]z(?<!\\)\{L?EPOCH\}c                   ~    \ rS rSrSrSrSrSrSrS r	S r
SS	 jr\" \
\S
S9rS rS r\SS j5       r\S 5       rSrg)DateTemplate9   zA template which searches for and returns a date from a log line.

This is an not functional abstract class which other templates should
inherit from.

Attributes
----------
name
regex
      r	   r   c                 f    SU l         SU l        SU l        SU l        SU l        SU l        S U l        g )Nr   g      ?r   )nameweightflagshitstime_regex_cRegexselfs    r   __init__DateTemplate.__init__J   s3    $)$+$*$)$)$+$,r   c                     U R                   $ N)r   r    s    r   getRegexDateTemplate.getRegexS   s    	r   c                 6   UR                  5       n[        R                  U5      nU(       a  [        R                  SUSS9nU=(       a    [        R                  U5      (       + nU=(       a    [
        R                  U5      (       + n[        R                  U5      (       d   [        S   R                  [        S   U5      nSU l        U(       a~  U =R                  US:w  a  [        R                  O[        R                  -  sl        US:w  a  SU-   nO9SU-   nU R                  R                  S5      (       d  SU R                  -   U l        U(       a(  U =R                  [        R                  -  sl        US	-  nU R                  [        R                  -  (       d=  [        R                  U5      (       a#  U =R                  [        R                  -  sl        U R                  [        R                   -  (       d=  ["        R                  U5      (       a#  U =R                  [        R                   -  sl        [$        S   R                  [$        S   U5      nU(       a  UR'                  S5      U-   nXl        [*        R-                  S
SU5        SU l        g)a  Sets regex to use for searching for date in log line.

Parameters
----------
regex : str
        The regex the template will use for searching for a date.
wordBegin : bool
        Defines whether the regex should be modified to search at beginning of a
        word, by adding special boundary r'(?=^|\b|\W)' to start of regex.
        Can be disabled with specifying of ** at front of regex.
        Default True.
wordEnd : bool
        Defines whether the regex should be modified to search at end of a word,
        by adding special boundary r'(?=\b|\W|$)' to end of regex.
        Can be disabled with specifying of ** at end of regex.
        Default True.

Raises
------
re.error
        If regular expression fails to compile
r   r   )countr   startz(?=^|\b|\W)z^(?:\W{0,2})?z	{^LN-BEG}z(?=\b|\W|$)r   z  constructed regex %sN)stripRE_GLOBALFLAGSsearchsubRE_NO_WRD_BOUND_BEGRE_NO_WRD_BOUND_END
RE_GROUPEDRE_GROUPr   r   
WORD_BEGIN
LINE_BEGINr   
startswithWORD_ENDRE_LINE_BOUND_BEGLINE_ENDRE_LINE_BOUND_ENDRE_DEL_WRD_BOUNDSr   r   logSyslogr   )r!   regex	wordBeginwordEndgf
boundBeginboundEnds          r   setRegexDateTemplate.setRegexV   s   6 ++-%U#"b%q15B!4!;!;E!BB*>077>>(			5	!	!A;??8A;.5$*::I,@((lF]F]]:7U"Eu$E99,,tyy(TY::&&&:N5
**|..
.4E4L4LU4S4S::(((:
**|,,
,2C2J2J52Q2Q::&&&:
A

"
"#4Q#7
?%88A;5+**Q(%0$,r   z!Regex used to search for date.
		)docc                     U R                   (       d'   [        R                  " U R                  5      U l         gg! [         a2  n[
        R                  SU R                  U R                  5        UeSnAff = f)z.Compile regex by first usage.
                z Compile %r failed, expression %rN)r   recompiler=   	Exceptionr;   errorr   )r!   es     r   _compileRegexDateTemplate._compileRegex   sY     
::djj)DL 
  
LL3TYY

K
Gs   %: 
A6-A11A6c                     U R                   (       d  U R                  5         [        R                  SSU R                  5        U R                   R
                  " U/UQ76 nU(       a  U =R                  S-  sl        U$ )z?Check if regex for date matches on a log line.
                r   z   search %sr   )r   rL   r;   r<   r=   r-   r   )r!   lineargs	dateMatchs       r   	matchDateDateTemplate.matchDate   sZ     
**Q

+ll!!$..)99>9	r   Nc                     [        S5      e)a5  Abstract method, which should return the date for a log line

This should return the date for a log line, typically taking the
date from the part of the line which matched the templates regex.
This requires abstraction, therefore just raises exception.

Parameters
----------
line : str
        Log line, of which the date should be extracted from.
default_tz: if no explicit time zone is present in the line
            passing this will interpret it as in that time zone.

Raises
------
NotImplementedError
        Abstract method, therefore always returns this.
zgetDate() is abstract)NotImplementedErrorr!   rO   rQ   
default_tzs       r   getDateDateTemplate.getDate   s    ( 	344r   c                     [         R                  S[        R                  S[        R                  S[        R                  SU 5      5      5      5      $ )Nr   )RE_EXEANC_BOUND_BEGr.   RE_EXSANC_BOUND_BEGRE_EXLINE_BOUND_BEGRE_EXLINE_NO_BOUNDS)patterns    r   unboundPatternDateTemplate.unboundPattern   sE    		 	 2B 3 7 7G DE
 r   )r   r   r   r   r   r   r   TTNN)__name__
__module____qualname____firstlineno____doc__r4   r8   r3   r6   r"   r&   rC   propertyr=   rL   rR   r   rX   staticmethodr`   __static_attributes__ r   r   r   r   9   st    	 >@ 
(H
	
 5 5*  r   r   c                   ,    \ rS rSrSrSS jrSS jrSrg)		DateEpoch   zA date template which searches for Unix timestamps.

This includes Unix timestamps which appear at start of a line, optionally
within square braces (nsd), or on SELinux audit log lines.

Attributes
----------
name
regex
Nc                   ^ [         R                  U 5        U(       d  SOUU l        X0l        SU l        SmU(       a  U(       d  SOUU l        SmU(       aU  [
        R                  U4S jU5      n[        R                  U5      (       d  SU-   S-   nS	U l        U R                  U5        g U(       d  S
T-  nU R                  USS9  g ST-  nU R                  USSS9  g )NEpochr   z\d{10,11}\b(?:\.\d{3,6})?	LongEpochz'\d{10,11}(?:\d{3}(?:\.\d{1,6}|\d{3})?)?c                    > ST-  $ )Nz(%s)rl   )vepochREs    r   r   $DateEpoch.__init__.<locals>.<lambda>   s
    &7*:r   ()r	   zf((?:^|(?P<square>(?<=^\[))|(?P<selinux>(?<=\baudit\()))%s)(?:(?(selinux)(?=:\d+\)))|(?(square)(?=\])))Fr>   z*((?P<square>(?<=^\[))?%s)(?(square)(?=\]))r*   T)r>   r?   )
r   r"   r   _longFrm_grpIdxRE_EPOCH_PATTERNr.   r1   r-   rC   )r!   lineBeginOnlyr_   longFrmr=   ru   s        @r   r"   DateEpoch.__init__   s    $g'$)-$,('"){w4977 :GD5


G
$
$%K#E4<==tw~~5==%=(87B5=='4=8r   c                 6   U(       d  U R                  U5      nU(       az  UR                  U R                  5      nU R                  (       aA  [	        U5      S:  a2  [	        U5      S:  a  SU;  a  [        U5      S-  nO[        U5      S-  n[        U5      U4$ g)aw  Method to return the date for a log line.

Parameters
----------
line : str
        Log line, of which the date should be extracted from.
default_tz: ignored, Unix timestamps are time zone independent

Returns
-------
(float, str)
        Tuple containing a Unix timestamp, and the string of the date
        which was matched and in turned used to calculated the timestamp.
      .i@B i  N)rR   r   r{   rz   lenfloat)r!   rO   rQ   rW   rt   s        r   rX   DateEpoch.getDate   s{     
~~d#9t||$1
mmA"
1v|1	qG	Q	qDQ8Y
 r   )r{   rz   r   )FNFrc   rd   re   rf   rg   rh   r"   rX   rk   rl   r   r   rn   rn      s    	9. r   rn   c                      ^  \ rS rSrSr\" 5       u  rr\R                  " \5      rS	U 4S jjr
\S 5       r\R                  S 5       rS
U 4S jjrSS jrSrU =r$ )DatePatternRegexi  zDate template, with regex/pattern

Parameters
----------
pattern : str
        Sets the date templates pattern.

Attributes
----------
name
regex
pattern
c                 f   > [         [        U ]  5         S U l        Ub  U R                  " U40 UD6  g g r%   )superr   r"   _patternrC   )r!   r_   kwargs	__class__s      r   r"   DatePatternRegex.__init__!  s4    $(*$-==#F# r   c                     U R                   $ )aJ  The pattern used for regex with strptime "%" time fields.

This should be a valid regular expression, of which matching string
will be extracted from the log line. strptime style "%" fields will
be replaced by appropriate regular expressions, or custom regex
groups with names as per the strptime fields can also be used
instead.
)r   r    s    r   r_   DatePatternRegex.pattern'  s     
r   c                 &    U R                  U5        g r%   )rC   )r!   r_   s     r   r_   r   3  s    --r   c                   > Xl         [        R                  U5      (       a  [        R                  SU5      nS=p#U(       a2  [        R                  U5      (       a  [        R                  SU5      nSn U R
                  R                  SU5      nX@R                  -  U l        U[        -  n[        R                  U5      (       a  SU-   n[        [        U ]3  XRU5        g ! [         a  n[        SU< SU< 35      eS nAff = f)Nr   Fr*   z%(\1)sz(?iu)zFailed to set datepattern 'z8' (may be an invalid format or unescaped percent char): )r   r^   r-   r.   r]   
_patternRE_patternNamer   r   RE_ALPHA_PATTERNr   r   rC   rI   	TypeError)r!   r_   r>   r?   fmtr=   rK   r   s          r   rC   DatePatternRegex.setRegex7  s    -(( $$R179&--g66 $$R179
}			Y	03&&&49<5g&&uE	4)%GD	 }	pwyz{	||}s   5A+C! !
D+C>>Dc                 v    U(       d  U R                  U5      nU(       a  [        UR                  5       US9U4$ g)a  Method to return the date for a log line.

This uses a custom version of strptime, using the named groups
from the instances `pattern` property.

Parameters
----------
line : str
        Log line, of which the date should be extracted from.
default_tz: optionally used to correct timezone

Returns
-------
(float, str)
        Tuple containing a Unix timestamp, and the string of the date
        which was matched and in turned used to calculated the timestamp.
)rW   N)rR   r   	groupdictrV   s       r   rX   DatePatternRegex.getDateN  s=    $ 
~~d#9y224L  r   )r   r   r%   rb   rc   )rd   re   rf   rg   rh   r   r   r   rG   rH   r"   ri   r_   setterrC   rX   rk   __classcell__)r   s   @r   r   r     sf     -.\jj$$ 	 	 
.. }. r   r   c                   ,    \ rS rSrSrSS jrSS jrSrg)	
DateTai64nig  zZA date template which matches TAI64N format timestamps.

Attributes
----------
name
regex
c                 \    [         R                  U 5        SU l        U R                  SUS9  g )NTAI64Nz@[0-9a-f]{24}ry   )r   r"   r   rC   )r!   r>   s     r   r"   DateTai64n.__init__p  s'    $)--9-5r   Nc                     U(       d  U R                  U5      nU(       a$  UR                  S5      nUSS n[        US5      U4$ g)ap  Method to return the date for a log line.

Parameters
----------
line : str
        Log line, of which the date should be extracted from.
default_tz: ignored, since TAI is time zone independent

Returns
-------
(float, str)
        Tuple containing a Unix timestamp, and the string of the date
        which was matched and in turned used to calculated the timestamp.
r   r	      r   N)rR   r   int)r!   rO   rQ   rW   valueseconds_since_epochs         r   rX   DateTai64n.getDatev  sJ     
~~d#9??15q"B'
33 r   )r   )Frc   r   rl   r   r   r   r   g  s    64r   r   )$
__author____copyright____license__rG   r   abcr   strptimer   r   r   helpersr
   rd   r;   rH   r1   r2   r,   r^   r]   r\   r[   r/   r0   r:   r7   r9   r   
IGNORECASEr|   objectr   rn   r   r   rl   r   r   <module>r      sY  . 
2   C C 	8	 ZZ78
ZZHI;X:;jj- jj!12 jj!=> jj!89 jj!QR jj!EF jj!MN:=  JJJK JJ78 ::23 ::3R]]C T6 Tn<  < ~U| Up%4 %4r   