
    \d+A                         d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ dd	Z G d
 dej                  Z G d de          Z G d de          ZdS )z
Authoritative resolvers.
    N)defer)commondnserror)failure)execfilenativeString)FilePath/tmp/twisted-names.serialc                    t          j        d          }t          j        d          }	 t          j                            |           s@t          | d          5 }|                    |dz              ddd           n# 1 swxY w Y   t          j        |           n# t          j        |           w xY wt          |           5 }|                                	                                \  }}ddd           n# 1 swxY w Y   ||k    rt          |          dz   pd}t          | d          5 }|                    d||fz             ddd           n# 1 swxY w Y   |d	|fz  z   }|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   z%s %dz%02d)timestrftimeosumaskpathexistsopenwritereadlinesplitint)filenameserialof
serialFile
lastSerialzoneIDs          7lib/python3.11/site-packages/twisted/names/authority.py	getSerialr#      s   " ]8$$F
Aw~~h'' 	'h$$ '&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 		h ;:'002288::
F; ; ; ; ; ; ; ; ; ; ; ; ; ; ; F"9Vq>QF	h		 5
FF#334445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 v	)*FMsS   /B A>2B >BB BB B4*C<<D D 1EEEc                       e Zd ZdZej        ej        ej        fZej	        ej
        fZdZdZd Zd Zd Zd ZddZdd	Zd
 ZdS )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{bytes} keys
    Nc                 |    t           j                            |            |                     |           i | _        d S N)r   ResolverBase__init__loadFile_cacheselfr   s     r"   r)   zFileAuthority.__init__V   s6    $$T***h    c                     || _         d S r'   )__dict__)r-   states     r"   __setstate__zFileAuthority.__setstate__[   s    r.   c                     dS )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
        N r,   s     r"   r*   zFileAuthority.loadFile^   s      r.   c           
   #   8  K   ||z   D ]}|j         | j        v r|j        j        j        }| j                            |                                d          D ]B}|j        | j        v r2t          j
        ||j        t          j        |j        p||d          V  CdS )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}.
        r4   TauthN)type_ADDITIONAL_PROCESSING_TYPESpayloadnamerecordsgetlowerTYPE_ADDRESS_TYPESr   RRHeaderINttl)r-   answer	authorityrC   recordr;   recs          r"   _additionalRecordsz FileAuthority._additionalRecordsk   s      * y( 	 	F{d???~*/<++DJJLL"==  Cx4#666!l #(CFCGNsCd     	 	r.   c                    g }g }g }g }t          | j        d         j        | j        d         j                  }	| j                            |                                          }
|
r|
D ]C}|j        |j        }n|	}|j        t          j
        k    rr|                                | j        d                                         k    r=|                    t          j        ||j        t          j        ||d                     nW|j        |k    s|t          j        k    r<|                    t          j        ||j        t          j        ||d                     |j        t          j        k    r<|                    t          j        ||j        t          j        ||d                     E|s|}|                     |||	          }|r|                    |           n|                    |           |sY|sW|                    t          j        | j        d         t          j        t          j        || j        d         d                     t'          j        |||f          S t          j        || j        d                   r8t'          j        t/          j        t          j        |                              S t'          j        t/          j        t5          j        |                              S )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.
        r   Nr   Fr6   T)maxsoaminimumexpirer<   r=   r>   rC   r?   r   NSappendrA   rB   ALL_RECORDSCNAMErH   extendSOAr   succeed_isSubdomainOffailr   FailureAuthoritativeDomainErrorr   DomainError)r-   r;   clsr8   timeoutcnamesresultsrE   
additionaldefault_ttldomain_recordsrF   rC   additionalInformations                 r"   _lookupzFileAuthority._lookup   s   0 	
$(1+-tx{/ABB))$**,,77 7	L(  :) *CC%C;#&((TZZ\\TXa[=N=N=P=P-P-P $$T6;VRWXXX    [D((DCO,C,CNNT6;VRVWWW   ;#)++MMT6;VRVWWW    !  %)$;$;K% %!  945555!!"7888 9    LSWcfc48A;T    
 ='9j!ABBB!$44 	L z'/#2Nt2T2T"U"UVVV
 z'/%2CD2I2I"J"JKKKr.   
   c                    t          j        |          }| j        d                                         |                                k    rTt	          | j        d         j        | j        d         j                  }| j        d         j        | j        d         j        }n|}t          j        | j        d         t           j	        t           j
        || j        d         d          g}| j                                        D ]l\  }}|D ]d}|j        |j        }	n|}	|j        t           j	        k    r<|                    t          j        ||j        t           j
        |	|d                     em|                    |d                    t          j        |ddf          S t          j        t%          j        t          j        |                              S )Nr   r   Tr6   r4   )r   domainStringrK   r>   rJ   rL   rM   rC   rA   rS   rB   r<   itemsr?   rO   r   rT   rV   r   rW   rY   )
r-   r;   r[   r_   soa_ttlr]   krrG   rC   s
             r"   
lookupZonezFileAuthority.lookupZone   s   %%8A;$**,,..dhqk148A;3EFFKx{*(1+/%HQK#&'48A;T  G
 ,,,.. 	 	A  Cw*!g)x37**LCHcfc3TRRR   NN71:&&&='2r!2333z'/#/$*?*?@@AAAr.   c                     g g g }}}|D ]m}|d         rc|                     |d         d                    |                     |d         d                    |                     |d         d                    n|||fS )Nr   r      )rR   )r-   r]   ansr7   address         r"   _cbAllRecordszFileAuthority._cbAllRecords   s    R3T 	& 	&C1v &

3q6!9%%%CF1I&&&

3q6!9%%%D#~r.   r'   )rc   )__name__
__module____qualname____doc__r   rQ   MXrN   r9   AAAAAr@   rK   r<   r)   r2   r*   rH   rb   rj   rp   r4   r.   r"   r%   r%   ;   s         ( %(Isvsv#> eSX&N
CG  
      <WL WL WL WLrB B B B8    r.   r%   c                   $    e Zd ZdZd Zd Zd ZdS )PySourceAuthorityzC
    A FileAuthority that is built up from Python source code.
    c                    |                                  i }}t          |||           d|vrt          d|z             i | _        |d         D ]u}t	          |d         t
          j                  r|| _        | j                            |d         	                                g           
                    |d                    vd S )NzonezNo zone defined in r   r   )setupConfigNamespacer   
ValueErrorr<   
isinstancer   
Record_SOArK   
setdefaultr>   rO   )r-   r   glrrs        r"   r*   zPySourceAuthority.loadFile  s    ((**B11a   ??2X=>>>F) 	E 	EB"Q%00 L##BqEKKMM266==beDDDD	E 	Er.   c                     fd}|S )Nc                 <    t          j        |            |i |fS r'   )r   re   )r;   argkwr8   s      r"   wrapRecordFuncz4PySourceAuthority.wrapRecord.<locals>.wrapRecordFunc  s(    $T**DD#,<,<,<==r.   r4   )r-   r8   r   s    ` r"   
wrapRecordzPySourceAuthority.wrapRecord  s$    	> 	> 	> 	> 	> r.   c                     i }t           j                                        }d |D             D ]F}t          t           |          }|                     |          }|||t          d          d          <   G|S )Nc                 <    g | ]}|                     d           |S )Record_)
startswith).0xs     r"   
<listcomp>z:PySourceAuthority.setupConfigNamespace.<locals>.<listcomp>"  s)    CCCQ1<<	+B+BCqCCCr.   r   )r   r0   keysgetattrr   len)r-   ri   rf   rF   r8   r   s         r"   r|   z&PySourceAuthority.setupConfigNamespace  sz    !!##CC%CCC 	, 	,F3''D%%A*+AfS^^%%&''r.   N)rq   rr   rs   rt   r*   r   r|   r4   r.   r"   ry   ry     sN         
E 
E 
E      r.   ry   c                   <    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	S )
BindAuthorityz
    An Authority that loads U{BIND zone files
    <https://en.wikipedia.org/wiki/Zone_file>}.

    Supports only C{$ORIGIN} and C{$TTL} directives.
    c                 B   t          |          }t          |                                dz             | _        |                                                    d          }|                     |          }|                     |          }|                     |           dS )z}
        Load records from C{filename}.

        @param filename: file to read from
        @type filename: L{bytes}
           .TN)	r
   r	   basenameorigin
getContent
splitlinesstripCommentscollapseContinuations
parseLines)r-   r   fpliness       r"   r*   zBindAuthority.loadFile1  s     h #2;;==4#788**400""5))**511r.   c                 .    d d |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.
        c              3      K   | ]<}|                     d           dk    r|p|d|                     d                    V  =dS )   ;N)find)r   as     r"   	<genexpr>z.BindAuthority.stripComments.<locals>.<genexpr>K  sb       
 
 FF4LLB$19.AFF4LL.(9
 
 
 
 
 
r.   c                 6    g | ]}|                                 S r4   )strip)r   bs     r"   r   z/BindAuthority.stripComments.<locals>.<listcomp>M  s     ///Aaggii///r.   r4   )r-   r   s     r"   r   zBindAuthority.stripCommentsB  s1    
 
/////
 
 
 	
r.   c           
         g }d}|D ]}|dk    rb|                     d          dk    r|                    |           7|                    |d|                     d                              d}j|                     d          dk    r1|dxx         d|d|                     d                   z   z  cc<   d}|dxx         d|z   z  cc<   t          dd |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      )    c              3   >   K   | ]}|                                 V  d S r'   )r   )r   lines     r"   r   z6BindAuthority.collapseContinuations.<locals>.<genexpr>h  s*      88dTZZ\\888888r.   )r   rO   filter)r-   r   r   r1   r   s        r"   r   z#BindAuthority.collapseContinuationsP  s     	) 	)Dzz99T??b((HHTNNNNHHT"3DIIdOO"34555EE99T??b((bEEETD):499T??):$;;;EEEEEbEEETD[(EEEEd88a888999r.   c                 P   d}| j         }i | _        |D ]}|d         dk    rt          j        |d                   })|d         dk    r	|d         }>|d         dk    rt	          d          |d         dk    rt	          d	          |                     |||           || _         d
S )zs
        Parse C{lines}.

        @param lines: lines to work on
        @type lines: iterable of L{bytes}
        i0*  r   s   $TTLr   s   $ORIGINs   $INCLUDEz"$INCLUDE directive not implementeds	   $GENERATEz#$GENERATE directive not implementedN)r   r<   r   str2timeNotImplementedErrorparseRecordLine)r-   r   rC   r   r   s        r"   r   zBindAuthority.parseLinesj  s      
	8 
	8DAw'!!l47++aJ&&aaK'')*NOOOaL(()*OPPP$$VS$7777 r.   c                     |                     d          s|dz   |dd         z   }n
|dd         }t          | d| d          }|r |||||           dS t          d|d          )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}
        r   Nr   class_zRecord class  not supported)endswithr   r   )r-   ownerrC   r8   domainrZ   rdatar   s           r"   	addRecordzBindAuthority.addRecord  s    , t$$ 	!d]U3B3Z/FFCRC[FD.3..$// 	MAc4'''''%&Kc&K&K&KLLLr.   c                 P   t          t          dt          |           d          }|r_ || }||_        | j                            |                                g                               |           |dk    r||f| _        dS dS t          dt          |          d          )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 rdata: bytes
        r   NrS   zRecord type r   )
r   r   r	   rC   r<   r   r>   rO   rK   r   )r-   rC   r8   r   r   rF   ri   s          r"   class_INzBindAuthority.class_IN  s      <T(:(:<<dCC 
	AAEL##FLLNNB77>>qAAAu}}"A; } &C|D11CCC  r.   c           	         d t           j                                        D             }d t           j                                        D             }||z  }d}|}|d         dk    r|dd         }|}n6|d                                         s|d         |vr|d         }|dd         }|d                                         s
|d         |v r|}	|}n|d         }	|dd         }|d         |v rL|d         }|dd         }|d                                         rt          |d                   }|dd         }nU|d                                         r;t          |d                   }|dd         }|d         |v r|d         }|dd         }|d         }
|dd         }|                     ||t          |
          |	t          |          |           dS )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}
        c                 8    h | ]}|                     d           S asciiencode)r   qcs     r"   	<setcomp>z0BindAuthority.parseRecordLine.<locals>.<setcomp>  s$    PPPr		'**PPPr.   c                 8    h | ]}|                     d           S r   r   )r   qts     r"   r   z0BindAuthority.parseRecordLine.<locals>.<setcomp>  s$    LLLRbii((LLLr.   s   INr      @r   N)r   QUERY_CLASSESvaluesQUERY_TYPESisdigitr   r   r	   )r-   r   rC   r   queryClasses
queryTypesmarkersrZ   r   r   r8   r   s               r"   r   zBindAuthority.parseRecordLine  s    QPS5F5M5M5O5OPPPLL3?3I3I3K3KLLL
+7d??8DEEa"" 	tAwg'='=GE8D7?? 	Q7 2 2FEE!WF8D7l""q'C8DAw    $q'llABBx!W__ 	 d1g,,C8DAw,&&1gABBxAwQRRuc<#5#5v|C?P?PRWXXXXXr.   N)rq   rr   rs   rt   r*   r   r   r   r   r   r   r4   r.   r"   r   r   )  s           "
 
 
: : :4  6M M M@  :5Y 5Y 5Y 5Y 5Yr.   r   )r   )rt   r   r   twisted.internetr   twisted.namesr   r   r   twisted.pythonr   twisted.python.compatr   r	   twisted.python.filepathr
   r#   r(   r%   ry   r   r4   r.   r"   <module>r      sM  
 
 
			  " " " " " " , , , , , , , , , , " " " " " " 8 8 8 8 8 8 8 8 , , , , , ,$ $ $ $NJ J J J JF' J J JZ       BNY NY NY NY NYM NY NY NY NY NYr.   