a =*f;- @ sx d Z dZdZddlZddlZddlZddlZddlmZ ddl m Z mZmZm Z dd lm Z e eZG d d deZdS )z.Cyril Jaquier, Lee Clemens, Yaroslav HalchenkozPCopyright (c) 2004 Cyril Jaquier, 2011-2012 Lee Clemens, 2012 Yaroslav HalchenkoZGPL N )Actions ) getLogger_as_boolextractOptionsMyTime)r c @ s e Zd ZdZg dZd5ddZdd Zd d Zdd Zd d Z dd Z edd Zedd Z e jdd Z edd Zedd Zedd Zejdd Zd6ddZed d! Zd"d# Zd$d% Zd&d' Zd7d(d)Zd*d+ Zd8d-d.Zd/d0 Zd9d1d2Zd3d4 ZdS ):Jailag Fail2Ban jail, which manages a filter and associated actions. The class handles the initialisation of a filter, and actions. It's role is then to act as an interface between the filter and actions, passing bans detected by the filter, for the actions to then act upon. Parameters ---------- name : str Name assigned to the jail. backend : str Backend to be used for filter. "auto" will attempt to pick the most preferred backend method. Default: "auto" db : Fail2BanDb Fail2Ban persistent database instance. Default: `None` Attributes ---------- name database filter actions idle status )Z pyinotifyZpollingZsystemdautoNc C sp || _ t|dkr td| || _t | _d | _i | _ t d| j d | _|d urf| || _|| _d S )N z]Jail name %r might be too long and some commands might not function correctly. Please shortenzCreating new jail '%s') _Jail__dblenlogSyswarning_Jail__namequeueZQueue_Jail__queue _Jail__filter _banExtrainfoname_realBackend_setBackendbackend)selfr r Zdb r 8/usr/lib/python3.9/site-packages/fail2ban/server/jail.py__init__G s z Jail.__init__c C s d| j j| jf S )Nz%s(%r)) __class____name__r r r r r __repr__Y s z Jail.__repr__c C s> t |\}}| }| j}|dkr`|| jvrNtd||f td||f |||d }|D ]}t| d| }zT|f i | |dkr||krt d||f nt d| t| | _|W S t y } z0t|dkrtjntjd||f W Y d }~qdd }~0 0 qdtd| j td| j d S )Nr z.Unknown backend %s. Must be among %s or 'auto'z_init%sz9Could only initiated %r backend whenever %r was requestedzInitiated %r backendz)Backend %r failed to initialize due to %sz,Failed to initialize any backend for Jail %r)r lower _BACKENDSr error ValueErrorindexgetattr capitalizer r r _Jail__actionsImportErrorlogloggingDEBUGZERRORr RuntimeError)r r ZbeArgsZbackendsbZ initmethoder r r r \ sD zJail._setBackendc K s6 ddl m} td| j|f || fi || _d S )Nr ) FilterPollzJail '%s' uses poller %r)Z filterpollr1 r r r r )r kwargsr1 r r r _initPolling s zJail._initPollingc K s6 ddl m} td| j|f || fi || _d S )Nr )FilterPyinotifyzJail '%s' uses pyinotify %r)Zfilterpyinotifyr4 r r r r )r r2 r4 r r r _initPyinotify s zJail._initPyinotifyc K s6 ddl m} td| j|f || fi || _d S )Nr ) FilterSystemdzJail '%s' uses systemd %r)Z filtersystemdr6 r r r r )r r2 r6 r r r _initSystemd s zJail._initSystemdc C s | j S )zName of jail. )r r r r r r s z Jail.namec C s | j S )z;The database used to store persistent data for the jail. r r r r r database s z Jail.databasec C s || _ d S Nr8 r valuer r r r9 s c C s | j S )z;The filter which the jail is using to monitor log files. )r r r r r filter s zJail.filterc C s | j S )z2Actions object used to manage actions for jail. )r) r r r r actions s zJail.actionsc C s | j jp| jjS )z-A boolean indicating whether jail is idle. r= idler> r r r r r@ s z Jail.idlec C s || j _|| j_d S r: r? r; r r r r@ s basicc C s^ | j j|d}| jj|d}|dkrNt| j jdd }| jpF| j||gS d|fd|fgS )zThe status of the jail. )flavorZstatsZFilter r ) r= statusr> typer replacer" r r )r rB fstatZastatr r r r rD s zJail.statusc C s | j S )z-Retrieve whether queue has tickets to ban. )r emptyr r r r hasFailTickets s zJail.hasFailTicketsc C s | j | dS )zQAdd a fail ticket to the jail. Used by filter to add a failure for banning. N)r Zputr ticketr r r putFailTicket s zJail.putFailTicketc C s. z| j d}|W S tjy( Y dS 0 dS )zTGet a fail ticket from the jail. Used by actions to get a failure for banning. FN)r getr ZEmptyrJ r r r getFailTicket s zJail.getFailTicketc s | j }|dkrd }|d ur$|||<