U
    ©¹Öhm  ã                   @   sè  d Z ddlZddlZddlmZmZmZmZmZm	Z	m
Z
 ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZG dd„ dejjƒZG dd„ dejjƒZdd	„ Ze	ejjeejj  eejj  ee e!e"e!e"f Z#d
d„ Z$G dd„ dƒZ%G dd„ dej&j'ƒZ(G dd„ dej&j)ƒZ*ddej+j,ej+j,dddejj-df	eee
ejj e.f  ee! ee
ej+j/e.f  e
ej+j/e.f ee
ej0j1e.f  ee
e!e.f  eejj2 ee
ejj e.f  e"eej3j4 dœdd„Z5dS )z
DNS Zones.é    N)ÚAnyÚIterableÚListÚOptionalÚSetÚTupleÚUnionc                   @   s   e Zd ZdZdS )ÚUnknownOriginzUnknown originN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú2./venv/lib/python3.8/site-packages/dns/zonefile.pyr	   &   s   r	   c                   @   s   e Zd ZdZdS )ÚCNAMEAndOtherDataz!A node has a CNAME and other dataNr
   r   r   r   r   r   *   s   r   c                 C   sz   t jj |¡}|  |¡}|d kr$d S | ¡ }|t jjjkrR|t jjjkrRtdƒ‚n$|t jjjkrv|t jjjkrvtdƒ‚d S )Nz1rdataset type is not compatible with a CNAME nodez9CNAME rdataset is not compatible with a regular data node)	ÚdnsÚnodeZNodeKindZclassify_rdatasetZget_nodeZclassifyZCNAMEZREGULARr   )ÚtxnÚnameÚrdatasetZrdataset_kindr   Z	node_kindr   r   r   Ú_check_cname_and_other_data.   s"    
ÿþ
ÿþÿr   c                 C   s   |   ¡ } |  d¡sd|  } | S )Nú$)ÚupperÚ
startswith)Úsr   r   r   Ú_upper_dollarizeQ   s    
r   c                   @   s¶   e Zd ZdZdejjejjej	j
eeeee f eejj ee eejj eejj ee dœ
dd„Zdd	„ Zd
d„ Zdd„ Zeeeeeeef dœdd„Zdd„ Zddœdd„ZdS )ÚReaderz(Read a DNS zone file into a transaction.FTN)
ÚtokÚrdclassr   Úallow_includeÚallow_directivesÚ
force_nameÚ	force_ttlÚforce_rdclassÚforce_rdtypeÚdefault_ttlc                 C   sô   || _ |j ¡ \| _| _}| j| _d| _d| _|d k	r:|}
|
d krPd| _d| _	n|
| _d| _	| j| _
|| _|| _g | _d | _|  |dkr¦dddh| _|rÌ| j d¡ n&|dkr¸tƒ | _ntdd	„ |D ƒƒ| _|| _|| _|| _|	| _| j t¡ d S )
Nr   FTú	$GENERATEú$ORIGINú$TTLú$INCLUDEc                 s   s   | ]}t |ƒV  qd S ©N)r   )Ú.0Údr   r   r   Ú	<genexpr>†   s     z"Reader.__init__.<locals>.<genexpr>)r   ÚmanagerÚorigin_informationÚzone_originÚ
relativizeÚcurrent_originÚlast_ttlÚlast_ttl_knownr&   Údefault_ttl_knownÚ	last_nameÚzone_rdclassr   Úsaved_stateÚcurrent_fileÚallowed_directivesÚaddÚsetr"   r#   r$   r%   Zcheck_put_rdatasetr   )Úselfr   r   r   r    r!   r"   r#   r$   r%   r&   Ú_r   r   r   Ú__init__[   s<    
zReader.__init__c                 C   s   | j  ¡ }| ¡ r qq d S r+   )r   ÚgetÚis_eol_or_eof©r>   Útokenr   r   r   Ú	_eat_line   s    
zReader._eat_linec                 C   s   | j  ¡ }| ¡ stjj‚|S r+   )r   rA   Úis_identifierr   Ú	exceptionÚSyntaxErrorrC   r   r   r   Ú_get_identifier“   s    
zReader._get_identifierc           	   
   C   sV  d}| j dk	r| j }nŠ| jdkr$t‚| jjdd}| ¡ sN| j || j¡| _n"| j ¡ }| ¡ rddS | j 	|¡ | j}| 
| j¡sŽ|  ¡  dS | jr | | j¡}| jdk	r¾| j}|| _d| _nV|  ¡ }d}z"tj |j¡}|| _d| _d}W n& tjjk
r   | j 	|¡ Y nX | jdk	r(| j}nv|  ¡ }ztj |j¡}W nB tjjk
r^   ‚ Y n( tk
r„   | j}| j 	|¡ Y nX || jkržtj d¡‚|dkr|  ¡ }d}z"tj |j¡}|| _d| _d}W nD tjjk
r   | jrü| j}n| jr
| j}| j 	|¡ Y nX | jdk	r0| j}nD|  ¡ }ztj  |j¡}W n( tk
rr   tj d|j ¡‚Y nX z"tj! ||| j| j| j| j¡}W n` tjjk
r²   ‚ Y nF tk
rö   t" #¡ dd… \}}tj d $t%|ƒt%|ƒ¡¡‚Y nX | js,|tj j&kr,|j'| _d| _|dkr,|j'}|dkrBtj d¡‚| j( )|||¡ dS )	z&Process one line from a DNS zone file.NT)Zwant_leadingúRR class is not zone's classúunknown rdatatype '%s'é   zcaught exception {}: {}úMissing default TTL value)*r"   r3   r	   r   rA   Zis_whitespaceZas_namer7   rB   ÚungetÚis_subdomainr1   rE   r2   r#   r4   r5   rI   r   ÚttlÚ	from_textÚvalueÚBadTTLr$   Ú
rdataclassrG   rH   Ú	Exceptionr8   r6   r&   r%   Ú	rdatatypeÚrdataÚsysÚexc_infoÚformatÚstrZSOAZminimumr   r<   )	r>   rD   r   rP   r   ÚrdtypeÚrdÚtyÚvar   r   r   Ú_rr_line™   s°    




úÿ


zReader._rr_line)ÚsideÚreturnc                 C   s  t  d¡}t  d¡}t  d¡}| |¡}|rJ| ¡ \}}}}	}
|dkrJd}| |¡}|rz| ¡ \}}}|dkrrd}d}	d}
| |¡}|r¨| ¡ \}}}}	|dkr¤d}d}
|sÈ|sÈ|sÈd}d}d}d}	d}
t|ƒ}t|	ƒ}	|dkrðtj d	| ¡‚|
d
kr
tj d|
 ¡‚||||	|
fS )Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$Ú ú+r   r-   )rd   ú-zinvalid offset sign %s)r-   ÚoÚxÚXÚnÚNzinvalid type %s)ÚreÚcompileÚmatchÚgroupsÚintr   rG   rH   )r>   ra   Zis_generate1Zis_generate2Zis_generate3Zg1ÚmodÚsignÚoffsetÚwidthÚbaseZg2Zg3r   r   r   Ú_parse_modify  sD    






zReader._parse_modifyc              
      s¼  | j dkrt‚| j ¡ }z2tj |j¡\}}}| j ¡ }| ¡ sHtj	j
‚W n tk
rf   tj	j
‚Y nX z$|j}| j ¡ }| ¡ sŠtj	j
‚W n tk
r¨   tj	j
‚Y nX z8tj |j¡}|| _d| _| j ¡ }| ¡ sàtj	j
‚W nT tjjk
r6   | js| jstj	 
d¡‚| jr$| j}n| jr2| j}Y nX z.tj |j¡}| j ¡ }| ¡ sdtj	j
‚W n< tj	j
k
rˆ   tj	j
‚Y n tk
r¢   | j}Y nX || jkr¼tj	 
d¡‚z.tj |j¡}| j ¡ }| ¡ sètj	j
‚W n( tk
r   tj	 
d|j ¡‚Y nX |j}	ttttdœdd„}
ttttd	œ‡ fd
d„‰ |  |¡\}}}}}|  |	¡\}}}}}t||d |ƒD ]6}|
|||ƒ}|
|||ƒ}ˆ |||ƒ}ˆ |||ƒ}| d| |¡}|	 d| |¡}tj || j | jj¡| _| j}| | j¡s|  ¡   dS | j r"|  | j¡}z tj! |||| j | j | j¡}W n` tj	j
k
r^   ‚ Y nF tk
r¢   t" #¡ dd… \}}tj	 
dt|ƒt|ƒf ¡‚Y nX | j$ %|||¡ q~dS )zPProcess one line containing the GENERATE statement from a DNS
        zone file.NTrM   rJ   rK   )ÚcounterÚoffset_signrr   rb   c                 S   s   |dkr|d9 }| | S )z0Calculate the index from the counter and offset.re   éÿÿÿÿr   )rv   rw   rr   r   r   r   Ú_calculate_index  s    z/Reader._generate_line.<locals>._calculate_index)Úindexrt   rs   rb   c                    sT   |dkrt | |ƒ |¡S ˆ | d|ƒ}d |ddd… ¡d|… }|dkrP| ¡ }|S )zVFormat the index with the given base, and zero-fill it
            to the given width.)r-   rf   rg   rh   rg   Ú.Nrx   rj   )rZ   ÚzfillÚjoinr   )rz   rt   rs   ZhexaZnibbles©Ú_format_indexr   r   r   ‡  s    z,Reader._generate_line.<locals>._format_indexé   z$%srL   zcaught exception %s: %s)&r3   r	   r   rA   r   ZgrangerQ   rR   rF   rG   rH   rU   rP   r4   r5   rS   r6   r&   rT   r8   rV   ro   r[   ru   ÚrangeÚreplacer   Ú
idna_codecr7   rO   r1   rE   r2   rW   rX   rY   r   r<   )r>   rD   ÚstartÚstopÚstepZlhsrP   r   r\   Zrhsry   ZlmodZlsignZloffsetZlwidthZlbaseZrmodZrsignZroffsetZrwidthZrbaseÚiZlindexÚrindexZlzfindexZrzfindexr   rW   r]   r^   r_   r   r~   r   Ú_generate_line@  s²    








  ÿúÿ
zReader._generate_line)rb   c           	   
   C   sì  zr| j  dd¡}| ¡ rr| jdk	r.| j ¡  t| jƒdkrj| j d¡\| _ | _| _	| _| _
| _| _| _qqrnê| ¡ r€qnÜ| ¡ r˜| j  ¡  qnÄ|jd dkr\t| jƒdkr\|j ¡ }|| jkràtj d|› d¡‚|dkr,| j  ¡ }| ¡ s
tj d	¡‚tj |j¡| _d| _| j  ¡  q|d
krp| j  ¡ | _| j  ¡  | jdkr`| j| _| j | j¡ q|dkr2| j  ¡ }|j}| j  ¡ }| ¡ rÂtj |j| j| j j¡}| j  ¡  n|  ¡ sÚtj d¡‚n| j}| j !| j | j| j	| j| j
| j| j| jf¡ t"|dƒ| _tj# $| j|¡| _ || _q|dkrF|  %¡  qtj d|› d¡‚q| j  &|¡ |  '¡  qW nr tjjk
ræ } zN| j  (¡ \}}|dkr¨d}tj d|||f ¡}t) *¡ d }| +|¡d‚W 5 d}~X Y nX dS )zÆRead a DNS zone file and build a zone object.

        @raises dns.zone.NoSOA: No SOA RR was found at the zone origin
        @raises dns.zone.NoNS: No NS RRset was found at the zone origin
        TNr   rx   r   zzone file directive 'z' is not allowedr)   zbad $TTLr(   r*   zbad origin in $INCLUDEÚrr'   zUnknown zone file directive 'ú'zsyntax errorz	%s:%d: %srL   ),r   rA   Zis_eofr:   ÚcloseÚlenr9   Úpopr3   r7   r4   r5   r&   r6   Zis_eolZ
is_commentZget_eolrR   r;   r   r   rG   rH   rF   rP   rQ   Zget_namer1   r   Ú_set_originr   rƒ   rB   ÚappendÚopenÚ	tokenizerÚ	Tokenizerr‰   rN   r`   ÚwhererX   rY   Úwith_traceback)	r>   rD   ÚcÚfilenameZ
new_originZdetailZline_numberÚexÚtbr   r   r   ÚreadÅ  s®    



÷
 


ÿ








  ÿ
øÿ


ÿ
ÿzReader.read)FTNNNNN)r   r   r   r   r   r’   r“   rT   Ú
RdataClassÚtransactionÚTransactionÚboolr   r   r[   r   r   ÚNamero   rV   Ú	RdataTyper@   rE   rI   r`   r   ru   r‰   rš   r   r   r   r   r   X   s6          õ


õ2z- r   c                       st   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚRRsetsReaderTransactionc                    s"   |rt ‚tƒ  |||¡ i | _d S r+   )ÚAssertionErrorÚsuperr@   Ú	rdatasets)r>   r/   ÚreplacementZ	read_only©Ú	__class__r   r   r@   +  s    z RRsetsReaderTransaction.__init__c                 C   s   | j  |||f¡S r+   )r¤   rA   ©r>   r   r\   Úcoversr   r   r   Ú_get_rdataset0  s    z%RRsetsReaderTransaction._get_rdatasetc                 C   sT   g }| j  ¡ D ] \\}}}}||kr| |¡ qt|ƒdkr@d S tj ¡ }||_ |S ©Nr   )r¤   Úitemsr   r   r   r   ZNode)r>   r   r¤   Zrdataset_namer?   r   r   r   r   r   Ú	_get_node3  s    
z!RRsetsReaderTransaction._get_nodec                 C   s   || j ||j|jf< d S r+   )r¤   r\   r©   )r>   r   r   r   r   r   Ú_put_rdataset>  s    z%RRsetsReaderTransaction._put_rdatasetc                 C   sH   g }| j D ]}|d |kr
| |¡ q
t|ƒdkrD|D ]}| j |= q6d S r«   )r¤   r   r   )r>   r   ÚremoveÚkeyr   r   r   Ú_delete_nameA  s    
z$RRsetsReaderTransaction._delete_namec                 C   s,   z| j |||f= W n tk
r&   Y nX d S r+   )r¤   ÚKeyErrorr¨   r   r   r   Ú_delete_rdatasetK  s    z(RRsetsReaderTransaction._delete_rdatasetc                 C   s$   | j D ]\}}}||kr dS qdS ©NTF)r¤   )r>   r   ri   r?   r   r   r   Ú_name_existsQ  s    z$RRsetsReaderTransaction._name_existsc                 C   s   t | jƒdkS r«   )r   r¤   ©r>   r   r   r   Ú_changedW  s    z RRsetsReaderTransaction._changedc                 C   sf   |rb|   ¡ rbg }| j ¡ D ]:\\}}}}tj ||j|j|j¡}| 	|¡ | 
|¡ q| j |¡ d S r+   )r·   r¤   r¬   r   ÚrrsetÚRRsetr   r\   r©   Úupdater   r/   Ú
set_rrsets)r>   ZcommitÚrrsetsr   r?   r   r¸   r   r   r   Ú_end_transactionZ  s       ÿ
z(RRsetsReaderTransaction._end_transactionc                 C   s   d S r+   r   )r>   Úoriginr   r   r   r   e  s    z#RRsetsReaderTransaction._set_originc                 C   s   t ‚d S r+   ©ÚNotImplementedErrorr¶   r   r   r   Ú_iterate_rdatasetsh  s    z*RRsetsReaderTransaction._iterate_rdatasetsc                 C   s   t ‚d S r+   r¿   r¶   r   r   r   Ú_iterate_namesk  s    z&RRsetsReaderTransaction._iterate_names)r   r   r   r@   rª   r­   r®   r±   r³   rµ   r·   r½   r   rÁ   rÂ   Ú__classcell__r   r   r¦   r   r¡   *  s   
r¡   c                   @   sN   e Zd Zejjdejjfdd„Zdd„ Z	ddd„Z
dd	„ Zd
d„ Zdd„ ZdS )ÚRRSetsReaderManagerFc                 C   s   || _ || _|| _g | _d S r+   )r¾   r2   r   r¼   )r>   r¾   r2   r   r   r   r   r@   p  s    zRRSetsReaderManager.__init__c                 C   s   t ‚d S r+   r¿   r¶   r   r   r   Úreaderx  s    zRRSetsReaderManager.readerc                 C   s   |dkst ‚t| ddƒS r´   )r¢   r¡   )r>   r¥   r   r   r   Úwriter{  s    zRRSetsReaderManager.writerc                 C   s   | j S r+   )r   r¶   r   r   r   Ú	get_class  s    zRRSetsReaderManager.get_classc                 C   s$   | j rtjj}n| j}| j| j |fS r+   )r2   r   r   Úemptyr¾   )r>   Z	effectiver   r   r   r0   ‚  s    
z&RRSetsReaderManager.origin_informationc                 C   s
   || _ d S r+   )r¼   )r>   r¼   r   r   r   r»   ‰  s    zRRSetsReaderManager.set_rrsetsN)F)r   r   r   r   r   ÚrootrT   ÚINr@   rÅ   rÆ   rÇ   r0   r»   r   r   r   r   rÄ   o  s     ÿ

rÄ   F)Útextr   rP   r   Údefault_rdclassr\   r&   rƒ   r¾   r2   rb   c
                 C   s  t |tƒrtj |tjj|¡}t |tƒr8tj |||¡}t |tƒrNtj |¡}t |tƒrdtj |¡}|dk	r|tjj 	|¡}nd}tjj 	|¡}|dk	r¦tj
j 	|¡}nd}t||	|ƒ}
|
 d¡:}tjj| d|d}t|||d|||||d	}| ¡  W 5 Q R X |
jS )a‘	  Read one or more rrsets from the specified text, possibly subject
    to restrictions.

    *text*, a file object or a string, is the input to process.

    *name*, a string, ``dns.name.Name``, or ``None``, is the owner name of
    the rrset.  If not ``None``, then the owner name is "forced", and the
    input must not specify an owner name.  If ``None``, then any owner names
    are allowed and must be present in the input.

    *ttl*, an ``int``, string, or None.  If not ``None``, the the TTL is
    forced to be the specified value and the input must not specify a TTL.
    If ``None``, then a TTL may be specified in the input.  If it is not
    specified, then the *default_ttl* will be used.

    *rdclass*, a ``dns.rdataclass.RdataClass``, string, or ``None``.  If
    not ``None``, then the class is forced to the specified value, and the
    input must not specify a class.  If ``None``, then the input may specify
    a class that matches *default_rdclass*.  Note that it is not possible to
    return rrsets with differing classes; specifying ``None`` for the class
    simply allows the user to optionally type a class as that may be convenient
    when cutting and pasting.

    *default_rdclass*, a ``dns.rdataclass.RdataClass`` or string.  The class
    of the returned rrsets.

    *rdtype*, a ``dns.rdatatype.RdataType``, string, or ``None``.  If not
    ``None``, then the type is forced to the specified value, and the
    input must not specify a type.  If ``None``, then a type must be present
    for each RR.

    *default_ttl*, an ``int``, string, or ``None``.  If not ``None``, then if
    the TTL is not forced and is not specified, then this value will be used.
    if ``None``, then if the TTL is not forced an error will occur if the TTL
    is not specified.

    *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
    encoder/decoder.  If ``None``, the default IDNA 2003 encoder/decoder
    is used.  Note that codecs only apply to the owner name; dnspython does
    not do IDNA for names in rdata, as there is no IDNA zonefile format.

    *origin*, a string, ``dns.name.Name``, or ``None``, is the origin for any
    relative names in the input, and also the origin to relativize to if
    *relativize* is ``True``.

    *relativize*, a bool.  If ``True``, names are relativized to the *origin*;
    if ``False`` then any relative names in the input are made absolute by
    appending the *origin*.
    NTz<input>)rƒ   F)r!   r"   r#   r$   r%   r&   )Ú
isinstancer[   r   r   rQ   rÉ   rP   rT   r›   ZmakerV   r    rÄ   rÆ   r’   r“   r   rš   r¼   )rË   r   rP   r   rÌ   r\   r&   rƒ   r¾   r2   r/   r   r   rÅ   r   r   r   Úread_rrsets  s>    =



÷rÎ   )6r   rk   rX   Útypingr   r   r   r   r   r   r   Zdns.exceptionr   Z
dns.grangeZdns.nameZdns.nodeZ	dns.rdataZdns.rdataclassZdns.rdatatypeZdns.rdtypes.ANY.SOAZ	dns.rrsetZdns.tokenizerZdns.transactionZdns.ttlrG   ZDNSExceptionr	   r   r   r’   r“   r   rŸ   ro   rž   ZSavedStateTyper   r   rœ   r   r¡   ZTransactionManagerrÄ   rT   rÊ   rÉ   r[   r›   rV   r    Z	IDNACodecr¸   r¹   rÎ   r   r   r   r   Ú<module>   st   $

ùÿ   UE ö

õ