File: //usr/lib/python3/dist-packages/fail2ban/server/__pycache__/datetemplate.cpython-310.pyc
o
��_<0 � @ s& d Z dZdZddlZddlZddlmZ ddlmZm Z m
Z
dd lmZ ee
�Ze�d
�Ze�d�dfZe�d
�Ze�d�Ze�d�Ze�d�Ze�d�Ze�d�Ze�d�dd� fZe�d�Ze�d�Ze�d�Ze�dej�ZG dd� de�ZG dd� de�Z G dd� de�Z!G d d!� d!e�Z"dS )"z
Cyril Jaquierz Copyright (c) 2004 Cyril Jaquier�GPL� N)�abstractmethod� )�reGroupDictStrptime�timeRE�getTimePatternRE� )� getLoggerz(?<!(?:\(\?))(?<!\\)\((?!\?)z-^((?:\(\?\w+\))?\^?(?:\(\?\w+\))?)(.*?)(\$?)$z\1(\2)\3z^\{UNB\}z
^\{\^LN-BEG\}z^\((?:\?:)?\^\|\\b\|\\W\)z\(\?=\\b\|\\W\|\$\)$z-^\(*(?:\(\?\w+\))?(?:\^|\(*\*\*|\((?:\?:)?\^)z!(?<!\\)(?:\$\)?|\\b|\\s|\*\*\)*)$z)^\(*(?:\(\?\w+\))?\(*\*\*|(?<!\\)\*\*\)*$c C s | � � �dd�S )Nz**� )�group�replace)�m� r �>/usr/lib/python3/dist-packages/fail2ban/server/datetemplate.py�<lambda>. s r z(^(?:\(\?\w+\))?(?:\^|\((?:\?:)?\^(?!\|))z(?<![\\\|])(?:\$\)?)$z(?<!\%)\%[aAbBpc]z(?<!\\)\{L?EPOCH\}c @ sr e Zd ZdZdZdZdZdZdd� Zdd � Z ddd�Z
ee e
d
d�Zdd� Z
dd� Zeddd��Zedd� �ZdS )�DateTemplatez�A 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 C s. d| _ d| _d| _d| _d| _d| _d | _d S )Nr
g �?r )�name�weight�flags�hits�time�_regex�_cRegex��selfr r r �__init__I s
zDateTemplate.__init__c C � | j S �N)r r r r r �getRegexR s zDateTemplate.getRegexTc C s: |� � }|ot�|� }|ot�|� }t�|�s#td �td |�}d| _|rP| j|dkr2tj ntj
O _|dkr@d| }nd| }| j�d�sPd| j | _|r^| jtj
O _|d7 }| jtj
@ sqt�|�rq| jtj
O _| jtj@ s�t�|�r�| jtjO _td �td |�}|| _t�dd |� d
| _d
S )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 �startz(?=^|\b|\W)z
^(?:\W{0,2})?z {^LN-BEG}z(?=\b|\W|$)� z constructed regex %sN)�strip�RE_NO_WRD_BOUND_BEG�search�RE_NO_WRD_BOUND_END�
RE_GROUPED�RE_GROUP�subr r �
WORD_BEGIN�
LINE_BEGINr �
startswith�WORD_END�RE_LINE_BOUND_BEG�LINE_END�RE_LINE_BOUND_END�RE_DEL_WRD_BOUNDSr �logSys�logr )r �regex� wordBegin�wordEnd�
boundBegin�boundEndr r r �setRegexU s0
zDateTemplate.setRegexz!Regex used to search for date.
)�docc
C sN | j s%z
t�| j�| _ W dS ty$ } zt�d| j| j� |�d}~ww dS )z Compile regex by first usage.
z Compile %r failed, expression %rN)r �re�compiler4 � Exceptionr2 �errorr )r �er r r �
_compileRegex� s ���zDateTemplate._compileRegexc G s8 | j s| �� | j j|g|�R � }|r| jd7 _|S )z1Check if regex for date matches on a log line.
r )r r@ r% r )r �line�args� dateMatchr r r � matchDate� s zDateTemplate.matchDateNc C s t d��)aQ 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.
zgetDate() is abstract)�NotImplementedError�r rA rC �
default_tzr r r �getDate� s zDateTemplate.getDatec
C s$ t �dt�dt�dt�d| ����S )Nr
)�RE_EXEANC_BOUND_BEGr) �RE_EXSANC_BOUND_BEG�RE_EXLINE_BOUND_BEG�RE_EXLINE_NO_BOUNDS)�patternr r r �unboundPattern� s
��zDateTemplate.unboundPattern�TT�NN)�__name__�
__module__�__qualname__�__doc__r+ r/ r* r- r r r9 �propertyr4 r@ rD r rH �staticmethodrN r r r r r 8 s$
9�r c @ s$ e Zd ZdZddd�Zd dd�ZdS )
� DateEpochz�A 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
FNc s� t �| � |s dn|| _|| _d| _d� |r|sdn|| _d� |r9dt�� fdd�|� d }d
| _| �|� d S |sHd� }| j|dd
� d S d� }| j|ddd� d S )N�Epochr z\d{10,11}\b(?:\.\d{3,6})?� LongEpochz'\d{10,11}(?:\d{3}(?:\.\d{1,6}|\d{3})?)?�(c s d� S )Nz(%s)r )�v��epochREr r r � s z$DateEpoch.__init__.<locals>.<lambda>�)r zf((?:^|(?P<square>(?<=^\[))|(?P<selinux>(?<=\baudit\()))%s)(?:(?(selinux)(?=:\d+\)))|(?(square)(?=\])))F�r5 z*((?P<square>(?<=^\[))?%s)(?(square)(?=\]))r! T)r5 r6 )r r r �_longFrm�_grpIdx�RE_EPOCH_PATTERNr) r9 )r �
lineBeginOnlyrM �longFrmr4 r r\ r r � s"