Your IP : 216.73.216.196


Current Path : /opt/alt/python37/lib64/python3.7/site-packages/twisted/names/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib64/python3.7/site-packages/twisted/names/__pycache__/authority.cpython-37.pyc

B

��d^�C�@s�dZddlmZmZddlZddlZddlmZmZm	Z	ddl
mZddlm
Z
ddlmZmZmZddlmZdd
d�ZGdd
�d
e	j�ZGdd�de�ZGdd�de�ZdS)z
Authoritative resolvers.
�)�absolute_import�divisionN)�dns�error�common)�defer)�failure)�execfile�nativeString�_PY3)�FilePath�/tmp/twisted-names.serialc	Cs�t�d�}t�d�}z4tj�|�sFt|d��}|�|d�WdQRXWdt�|�Xt|d��}|���	�\}}WdQRX||kr�t
|�dp�d}t|d��}|�d	||f�WdQRX|d
|f}|S)a�
    Return a monotonically increasing (across program runs) integer.

    State is stored in the given file.  If it does not exist, it is
    created with rw-/---/--- permissions.

    This manipulates process-global state by calling C{os.umask()}, so it isn't
    thread-safe.

    @param filename: Path to a file that is used to store the state across
        program runs.
    @type filename: L{str}

    @return: a monotonically increasing number
    @rtype: L{str}
    z%Y%m%d��wz 0N�r�rz%s %dz%02d)�time�strftime�os�umask�path�exists�open�write�readline�split�int)�filename�serial�o�fZ
serialFileZ
lastSerialZzoneID�r!�J/opt/alt/python37/lib64/python3.7/site-packages/twisted/names/authority.py�	getSerials

r#c@speZdZdZejejejfZej	ej
fZdZdZ
dd�Zdd�Zdd�Zd	d
�Zddd�Zddd�Zdd�ZdS)�
FileAuthoritya�
    An Authority that is loaded from a file.

    This is an abstract class that implements record search logic. To create
    a functional resolver, subclass it and override the L{loadFile} method.

    @ivar _ADDITIONAL_PROCESSING_TYPES: Record types for which additional
        processing will be done.

    @ivar _ADDRESS_TYPES: Record types which are useful for inclusion in the
        additional section generated during additional processing.

    @ivar soa: A 2-tuple containing the SOA domain name as a L{bytes} and a
        L{dns.Record_SOA}.

    @ivar records: A mapping of domains (as lowercased L{bytes}) to records.
    @type records: L{dict} with L{byte} keys
    NcCs tj�|�|�|�i|_dS)N)r�ResolverBase�__init__�loadFile�_cache)�selfrr!r!r"r&Xs
zFileAuthority.__init__cCs
||_dS)N)�__dict__)r)�stater!r!r"�__setstate__^szFileAuthority.__setstate__cCsdS)ab
        Load DNS records from a file.

        This method populates the I{soa} and I{records} attributes. It must be
        overridden in a subclass. It is called once from the initializer.

        @param filename: The I{filename} parameter that was passed to the
        initilizer.

        @returns: L{None} -- the return value is ignored
        Nr!)r)rr!r!r"r'bszFileAuthority.loadFilec
csvxp||D]d}|j|jkr
|jjj}xH|j�|��d�D]2}|j|jkr8t	j
||jt	j|jp^||dd�Vq8Wq
WdS)aW
        Find locally known information that could be useful to the consumer of
        the response and construct appropriate records to include in the
        I{additional} section of that response.

        Essentially, implement RFC 1034 section 4.3.2 step 6.

        @param answer: A L{list} of the records which will be included in the
            I{answer} section of the response.

        @param authority: A L{list} of the records which will be included in
            the I{authority} section of the response.

        @param ttl: The default TTL for records for which this is not otherwise
            specified.

        @return: A generator of L{dns.RRHeader} instances for inclusion in the
            I{additional} section.  These instances represent extra information
            about the records in C{answer} and C{authority}.
        r!T)�authN)
�type�_ADDITIONAL_PROCESSING_TYPES�payload�name�records�get�lower�TYPE�_ADDRESS_TYPESr�RRHeader�IN�ttl)r)Zanswer�	authorityr9�recordr1�recr!r!r"�_additionalRecordsps

z FileAuthority._additionalRecordscCs�g}g}g}g}t|jdj|jdj�}	|j�|���}
|
�r�x�|
D]�}|jdk	r\|j}n|	}|jt	j
kr�|��|jd��kr�|�t	j||jt	j
||dd��n4|j|ks�|t	jkr�|�t	j||jt	j
||dd��|jt	jkrF|�t	j||jt	j
||dd��qFW|�s|}|�|||	�}
|�r2|�|
�n
|�|
�|�st|�st|�t	j|jdt	jt	j
||jddd��t�|||f�St	�||jd��r�t�t�t	�|���St�t�t�|���SdS)a�
        Determine a response to a particular DNS query.

        @param name: The name which is being queried and for which to lookup a
            response.
        @type name: L{bytes}

        @param cls: The class which is being queried.  Only I{IN} is
            implemented here and this value is presently disregarded.
        @type cls: L{int}

        @param type: The type of records being queried.  See the types defined
            in L{twisted.names.dns}.
        @type type: L{int}

        @param timeout: All processing is done locally and a result is
            available immediately, so the timeout value is ignored.

        @return: A L{Deferred} that fires with a L{tuple} of three sets of
            response records (to comprise the I{answer}, I{authority}, and
            I{additional} sections of a DNS response) or with a L{Failure} if
            there is a problem processing the query.
        rNrF)r-T)�max�soa�minimum�expirer2r3r4r9r5r�NS�appendr7r8ZALL_RECORDS�CNAMEr=�extend�SOAr�succeedZ_isSubdomainOf�failr�FailureZAuthoritativeDomainErrorr�DomainError)r)r1�clsr.�timeoutZcnames�resultsr:�
additional�default_ttlZdomain_recordsr;r9ZadditionalInformationr!r!r"�_lookup�sR




zFileAuthority._lookup�
c
Cs"t�|�}|jd��|��k�rt|jdj|jdj�}|jdjdk	rZ|jdj}n|}tj|jdtj	tj
||jddd�g}xf|j��D]X\}}xN|D]F}|jdk	r�|j}	n|}	|j
tj	kr�|�tj||j
tj
|	|dd��q�Wq�W|�|d�t�|ddf�St�t�t�|���S)NrrT)r-r!)rZdomainStringr?r4r>r@rAr9r7rFr8r2�itemsr5rCrrGrHrrIrJ)
r)r1rLrOZsoa_ttlrM�krr<r9r!r!r"�
lookupZone�s*



 zFileAuthority.lookupZonecCsfggg}}}xJ|D]B}|dr|�|dd�|�|dd�|�|dd�qW|||fS)Nrr�)rE)r)rMZansr-�add�resr!r!r"�
_cbAllRecordss
zFileAuthority._cbAllRecords)N)rQ)�__name__�
__module__�__qualname__�__doc__rrDZMXrBr/�AZAAAAr6r?r2r&r,r'r=rPrTrXr!r!r!r"r$>s
c
 r$c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�PySourceAuthorityzC
    A FileAuthority that is built up from Python source code.
    cCs�|��i}}t|||�d|kr.td|��i|_xF|dD]:}t|dtj�rX||_|j�|d�	�g��
|d�q>WdS)N�zonezNo zone defined in rr)�setupConfigNamespacer	�
ValueErrorr2�
isinstancerZ
Record_SOAr?�
setdefaultr4rC)r)r�g�lZrrr!r!r"r'!szPySourceAuthority.loadFilecs�fdd�S)Ncs|�||�fS)Nr!)r1�arg�kw)r.r!r"�<lambda>/�z.PySourceAuthority.wrapRecord.<locals>.<lambda>r!)r)r.r!)r.r"�
wrapRecord.szPySourceAuthority.wrapRecordcCsRi}tj��}x>dd�|D�D],}tt|�}|�|�}|||td�d�<qW|S)NcSsg|]}|�d�r|�qS)�Record_)�
startswith)�.0�xr!r!r"�
<listcomp>5sz:PySourceAuthority.setupConfigNamespace.<locals>.<listcomp>rk)rr*�iterkeys�getattrrj�len)r)rrRr;r.r r!r!r"r`2s


z&PySourceAuthority.setupConfigNamespaceN)rYrZr[r\r'rjr`r!r!r!r"r^s
r^c@sHeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dS)�
BindAuthorityz�
    An Authority that loads U{BIND zone files
    <https://en.wikipedia.org/wiki/Zone_file>}.

    Supports only C{$ORIGIN} and C{$TTL} directives.
    cCsJt|�}t|��d�|_|���d�}|�|�}|�|�}|�|�dS)z}
        Load records from C{filename}.

        @param filename: file to read from
        @type filename: L{bytes}
        �.TN)	rr
�basename�originZ
getContent�
splitlines�
stripComments�collapseContinuations�
parseLines)r)r�fp�linesr!r!r"r'Ds

zBindAuthority.loadFilecCsdd�dd�|D�D�S)z�
        Strip comments from C{lines}.

        @param lines: lines to work on
        @type lines: iterable of L{bytes}

        @return: C{lines} sans comments.
        css2|]*}|�d�dkr|p(|d|�d��VqdS)�;���N)�find)rm�ar!r!r"�	<genexpr>`sz.BindAuthority.stripComments.<locals>.<genexpr>cSsg|]}|���qSr!)�strip)rm�br!r!r"roasz/BindAuthority.stripComments.<locals>.<listcomp>r!)r)r|r!r!r"rxVs
zBindAuthority.stripCommentsc
Cs�g}d}x�|D]�}|dkrR|�d�dkr4|�|�q�|�|d|�d���d}q|�d�dkr�|dd|d|�d��7<d}q|dd|7<qWtddd	�|D��S)
z�
        Transform multiline statements into single lines.

        @param lines: lines to work on
        @type lines: iterable of L{bytes}

        @return: iterable of continuous lines
        r�(r~Nr�)� css|]}|��VqdS)N)r)rm�liner!r!r"r�~sz6BindAuthority.collapseContinuations.<locals>.<genexpr>)rrC�filter)r)r|rer+r�r!r!r"ryfs	
"z#BindAuthority.collapseContinuationscCs�d}|j}i|_xx|D]p}|ddkr6t�|d�}q|ddkrL|d}q|ddkrbtd��q|ddkrxtd	��q|�|||�qW||_d
S)zs
        Parse C{lines}.

        @param lines: lines to work on
        @type lines: iterable of L{bytes}
        i0*rs$TTLrs$ORIGINs$INCLUDEz"$INCLUDE directive not implementeds	$GENERATEz#$GENERATE directive not implementedN)rvr2rZstr2time�NotImplementedError�parseRecordLine)r)r|r9rvr�r!r!r"rz�s


zBindAuthority.parseLinescCsd|�d�s |d|dd�}n|dd�}t|d|fd�}|rR|||||�ntd|f��dS)a
        Add a record to our authority.  Expand domain with origin if necessary.

        @param owner: origin?
        @type owner: L{bytes}

        @param ttl: time to live for the record
        @type ttl: L{int}

        @param domain: the domain for which the record is to be added
        @type domain: L{bytes}

        @param type: record type
        @type type: L{str}

        @param cls: record class
        @type cls: L{str}

        @param rdata: record data
        @type rdata: L{list} of L{bytes}
        rtNr~zclass_%szRecord class %r not supported)�endswithrqr�)r)�ownerr9r.�domainrK�rdatar r!r!r"�	addRecord�s
zBindAuthority.addRecordcCsjttdt|�fd�}|rT||�}||_|j�|��g��|�|dkrf||f|_nt	dt|�f��dS)a>
        Simulate a class IN and recurse into the actual class.

        @param ttl: time to live for the record
        @type ttl: L{int}

        @param type: record type
        @type type: str

        @param domain: the domain
        @type domain: bytes

        @param rdata:
        @type rdate: bytes
        z	Record_%sNrFzRecord type %r not supported)
rqrr
r9r2rcr4rCr?r�)r)r9r.r�r�r;rr!r!r"�class_IN�szBindAuthority.class_INcCs�tr6tdd�tj��D��}tdd�tj��D��}nttj���}ttj���}||B}d}|}|ddkr�|dd�}|}n,|d��s�|d|kr�|d}|dd�}|d��s�|d|kr�|}	|}n|d}	|dd�}|d|k�r,|d}|dd�}|d���rtt|d�}|dd�}nH|d���rtt|d�}|dd�}|d|k�rt|d}|dd�}|d}
|dd�}|�||t	|
�|	t	|�|�dS)	a�
        Parse a C{line} from a zone file respecting C{origin} and C{ttl}.

        Add resulting records to authority.

        @param origin: starting point for the zone
        @type origin: L{bytes}

        @param ttl: time to live for the record
        @type ttl: L{int}

        @param line: zone file line to parse; split by word
        @type line: L{list} of L{bytes}
        css|]}|�d�VqdS)�asciiN)�encode)rmZqcr!r!r"r��sz0BindAuthority.parseRecordLine.<locals>.<genexpr>css|]}|�d�VqdS)r�N)r�)rmZqtr!r!r"r��ssINr�@rN)
r�setrZ
QUERY_CLASSES�valuesZQUERY_TYPES�isdigitrr�r
)r)rvr9r�ZqueryClassesZ
queryTypes�markersrKr�r�r.r�r!r!r"r��sJzBindAuthority.parseRecordLineN)rYrZr[r\r'rxryrzr�r�r�r!r!r!r"rs=s#rs)r
)r\�
__future__rrrrZ
twisted.namesrrrZtwisted.internetrZtwisted.pythonrZtwisted.python.compatr	r
rZtwisted.python.filepathrr#r%r$r^rsr!r!r!r"�<module>s
(`