
    Vh)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y)z
Authoritative resolvers.
    N)defer)commondnserror)failure)execfilenativeString)FilePathc                    t        j                  d      }t        j                  d      }	 t        j                  j                  |       s)t        | d      5 }|j                  |dz          ddd       t        j                  |       t        |       5 }|j                         j                         \  }}ddd       |k(  xr t              dz   xs d}t        | d      5 }|j                  d||fz         ddd       |d	|fz  z   }|S # 1 sw Y   xY w# t        j                  |       w xY w# 1 sw Y   ~xY w# 1 sw Y   FxY w)
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          G/home/dcms/DCMS/lib/python3.12/site-packages/twisted/names/authority.py	getSerialr"      s)   " ]]8$F
Aww~~h'h$ '&' 		h ;:'00288:
F; F"9Vq>QF	h	 5
FF#3345 v	)*FM' ' 	; ;
5 5s;   +D D,D "D3#D?DD D03D<?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y)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                 r    t         j                  j                  |        | j                  |       i | _        y N)r   ResolverBase__init__loadFile_cacheselfr   s     r!   r(   zFileAuthority.__init__V   s)    $$T*h    c                     || _         y r&   )__dict__)r,   states     r!   __setstate__zFileAuthority.__setstate__[   s	    r-   c                      y)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           
   #     K   ||z   D ]  }|j                   | j                  v s|j                  j                  j                  }| j                  j                  |j                         d      D ]^  }|j                  | j                  v st        j                  ||j                  t        j                  |j                  xs ||d       `  yw)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}.
        r3   TauthN)type_ADDITIONAL_PROCESSING_TYPESpayloadnamerecordsgetlowerTYPE_ADDRESS_TYPESr   RRHeaderINttl)r,   answer	authorityrB   recordr:   recs          r!   _additionalRecordsz FileAuthority._additionalRecordsk   s     * y( 	F{{d???~~**//<<++DJJL"= Cxx4#6#66!ll #((CFFCGGNsCd 	s   !CA%C
ACc                 n   g }g }g }g }t        | j                  d   j                  | j                  d   j                        }	| j                  j                  |j                               }
|
r/|
D ]q  }|j                  |j                  }n|	}|j                  t        j                  k(  rq|j                         | j                  d   j                         k7  rC|j                  t        j                  ||j                  t        j                  ||d             nd|j                  |k(  s|t        j                  k(  rB|j                  t        j                  ||j                  t        j                  ||d             |j                  t        j                  k(  s0|j                  t        j                  ||j                  t        j                  ||d             t |s|}| j!                  |||	      }|r|j#                  |       n|j#                  |       |sb|s`|j                  t        j                  | j                  d   t        j$                  t        j                  | j                  d   d             t'        j(                  |||f      S t        j*                  || j                  d         r;t'        j,                  t/        j0                  t        j2                  |                  S t'        j,                  t/        j0                  t5        j6                  |                  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   r   Fr5   T)maxsoaminimumexpirer;   r<   r=   rB   r>   r   NSappendr@   rA   ALL_RECORDSCNAMErG   extendSOAr   succeed_isSubdomainOffailr   FailureAuthoritativeDomainErrorr   DomainError)r,   r:   clsr7   timeoutcnamesresultsrD   
additionaldefault_ttldomain_recordsrE   rB   additionalInformations                 r!   _lookupzFileAuthority._lookup   sg   0 	
$((1+--txx{/A/AB))$**,7( ::) **C%C;;#&&(TZZ\TXXa[=N=N=P-P $$T6;;VRWX [[D(DCOO,CNNT6;;VRVW ;;#))+MMT6;;VRVW%*   %)$;$;K%! 45!!"789   LLSWWcffc488A;T
 =='9j!ABB!!$4 zz'//#2N2Nt2T"UVV
 zz'//%2C2CD2I"JKKr-   c                 "   t        j                  |      }| j                  d   j                         |j                         k(  rt	        | 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                  j                         D ]  \  }}|D ]}  }|j                  |j                  }	n|}	|j                  t         j                  k7  s<|j                  t        j                  ||j                  t         j                  |	|d               |j                  |d          t        j                   |ddf      S t        j"                  t%        j&                  t        j(                  |                  S )Nr   r   Tr5   r3   )r   domainStringrJ   r=   rI   rK   rL   rB   r@   rR   rA   r;   itemsr>   rN   r   rS   rU   r   rV   rX   )
r,   r:   rZ   r^   soa_ttlr\   krrF   rB   s
             r!   
lookupZonezFileAuthority.lookupZone   s   %88A;$**,.dhhqk11488A;3E3EFKxx{*((1+//%HHQK#&&'488A;TG
 **, 	1 Cww*!gg)xx377*LLCHHcffc3TR	 NN71:&=='2r!233zz'//#//$*?@AAr-   c                     g g g }}}|D ]M  }|d   s	|j                  |d   d          |j                  |d   d          |j                  |d   d          O |||fS )Nr   r      )rQ   )r,   r\   ansr6   address         r!   _cbAllRecordszFileAuthority._cbAllRecords   so    R3T 	&C1v

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

3q6!9%		&
 D#~r-   r&   )
   )__name__
__module____qualname____doc__r   rP   MXrM   r8   AAAAAr?   rJ   r;   r(   r1   r)   rG   ra   rh   rn   r3   r-   r!   r$   r$   ;   se    ( %(IIsvvsvv#> eeSXX&N
CG
<WLrB8r-   r$   c                   "    e Zd ZdZd Zd Zd Zy)PySourceAuthorityzC
    A FileAuthority that is built up from Python source code.
    c                 N   | j                         i }}t        |||       d|vrt        d|z         i | _        |d   D ]e  }t	        |d   t
        j                        r|| _        | j                  j                  |d   j                         g       j                  |d          g y )NzonezNo zone defined in r   r   )setupConfigNamespacer   
ValueErrorr;   
isinstancer   
Record_SOArJ   
setdefaultr=   rN   )r,   r   glrrs        r!   r)   zPySourceAuthority.loadFile  s    ((*B11a ?2X=>>F) 	EB"Q%0LL##BqEKKM26==beD	Er-   c                     fd}|S )Nc                 >    t        j                  |        |i |fS r&   )r   rc   )r:   argkwr7   s      r!   wrapRecordFuncz4PySourceAuthority.wrapRecord.<locals>.wrapRecordFunc  s#    $$T*D#,<,<==r-   r3   )r,   r7   r   s    ` r!   
wrapRecordzPySourceAuthority.wrapRecord  s    	> r-   c                    i }t         j                  j                         }|D cg c]  }|j                  d      s| c}D ]4  }t	        t         |      }| j                  |      }|||t        d      d  <   6 |S c c}w )NRecord_)r   r/   keys
startswithgetattrr   len)r,   rg   rd   xrE   r7   r   s          r!   r{   z&PySourceAuthority.setupConfigNamespace  s{    !!#"'CQ1<<	+BqC 	,F3'D%A*+AfS^%&'	, 	 Ds
   A<A<N)rp   rq   rr   rs   r)   r   r{   r3   r-   r!   rx   rx     s    
Er-   rx   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
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                     t        |      }t        |j                         dz         | _        |j	                         j                  d      }| j                  |      }| j                  |      }| j                  |       y)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  si     h #2;;=4#78**40""5)**51r-   c                 V    d |D cg c]  }|j                          c}D        S c c}w )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   x   K   | ]2  }|j                  d       dk(  xr |xs |d|j                  d         4 yw)   ;N)find).0as     r!   	<genexpr>z.BindAuthority.stripComments.<locals>.<genexpr>K  sA      
 FF4LB$19.AFF4L(99
s   8:)strip)r,   r   bs      r!   r   zBindAuthority.stripCommentsB  s(    
)./Aaggi/
 	
/s   &c           
      r   g }d}|D ]  }|dk(  rL|j                  d      dk(  r|j                  |       .|j                  |d|j                  d              d}T|j                  d      dk7  r%|dxx   d|d|j                  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   | ]  }|j                           y wr&   )r   )r   lines     r!   r   z6BindAuthority.collapseContinuations.<locals>.<genexpr>h  s     8dTZZ\8s   )r   rN   filter)r,   r   r   r0   r   s        r!   r   z#BindAuthority.collapseContinuationsP  s      	)Dz99T?b(HHTNHHT"3DIIdO45E99T?b(bETD):499T?$;;;EEbETD[(E	) d8a899r-   c                    d}| j                   }i | _        |D ]j  }|d   dk(  rt        j                  |d         }$|d   dk(  r|d   }2|d   dk(  rt	        d      |d   dk(  rt	        d	      | j                  |||       l || _         y
)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   rB   r   r   s        r!   r   zBindAuthority.parseLinesj  s      
	8DAw'!ll47+aJ&aaK')*NOOaL()*OPP$$VS$7
	8 r-   c                     |j                  d      s|dz   |dd z   }n|dd }t        | d| d      }|r |||||       y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,   ownerrB   r7   domainrY   rdatar   s           r!   	addRecordzBindAuthority.addRecord  sh    , t$d]U3BZ/FCR[FDF3%.$/c4'%cWN&KLLr-   c                    t        t        dt        |       d      }|rU || }||_        | j                  j                  |j                         g       j                  |       |dk(  r
||f| _        yy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   NrR   zRecord type r   )
r   r   r	   rB   r;   r   r=   rN   rJ   r   )r,   rB   r7   r   r   rE   rg   s          r!   class_INzBindAuthority.class_IN  s      T(:';<dCAAELL##FLLNB7>>qAu}"A;  &|D14NC r-   c           	         t         j                  j                         D ch c]  }|j                  d       }}t         j                  j                         D ch c]  }|j                  d       }}||z  }d}	|}
|d   dk(  r|dd }|}
n$|d   j                         s|d   |vr
|d   }
|dd }|d   j                         s|d   |v r|
}|}
n
|d   }|dd }|d   |v r1|d   }	|dd }|d   j                         rKt        |d         }|dd }n7|d   j                         r$t        |d         }|dd }|d   |v r
|d   }	|dd }|d   }|dd }| j                  |
|t        |      |t        |	      |       yc c}w c c}w )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}
        asciis   INr      @r   N)	r   QUERY_CLASSESvaluesencodeQUERY_TYPESisdigitr   r   r	   )r,   r   rB   r   qcqueryClassesqt
queryTypesmarkersrY   r   r   r7   r   s                 r!   r   zBindAuthority.parseRecordLine  s    695F5F5M5M5OPr		'*PP36??3I3I3KLRbii(L
L+7d?8DEa"tAwg'=GE8D7??Q7 2FE!WF8D7l"q'C8DAw $q'lABx!W__d1g,C8DAw,&1gABxAwQRuc<#5v|C?PRWXM QLs   E=FN)rp   rq   rr   rs   r)   r   r   r   r   r   r   r3   r-   r!   r   r   )  s-    "
:46M@:5Yr-   r   )z/tmp/twisted-names.serial)rs   r   r   twisted.internetr   twisted.namesr   r   r   twisted.pythonr   twisted.python.compatr   r	   twisted.python.filepathr
   r"   r'   r$   rx   r   r3   r-   r!   <module>r      s\   

 
  " , , " 8 ,$NJF'' JZ BNYM NYr-   