a
    PҚ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   G/wd/webapps/venvs/v2025_4um/lib/python3.9/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 u 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 ur:|}
|
d u rPd| _d| _	n|
| _d| _	| j| _
|| _|| _g | _d | _|  |du rh d| _|r| jd n&|du rt | _ntdd |D | _|| _|| _|| _|	| _| jt d S )Nr   FT>   $ORIGIN	$GENERATE$TTL$INCLUDEc                 s   s   | ]}t |V  qd S N)r   ).0dr   r   r   	<genexpr>       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   rB   is_identifierr   	exceptionSyntaxErrorrD   r   r   r   _get_identifier   s    
zReader._get_identifierc           	   
   C   sP  d}| j dur| j }n| jdu r$t| jjdd}| sN| j|| j| _n"| j }| rddS | j	| | j}|
| js|   dS | jr|| j}| jdur| j}|| _d| _nT|  }d}z"tj|j}|| _d| _d}W n$ tjjy   | j	| Y n0 | jdur&| j}nr|  }ztj|j}W n> tjjyZ    Y n& ty~   | j}| j	| Y n0 || jkrtjd|du r|  }d}z"tj|j}|| _d| _d}W nB tjjy   | jr| j}n| jr| j}| j	| Y n0 | jdur(| j}nF|  }ztj |j}W n* tyl   tjd|j dY n0 z"tj!||| j| j| j| j}W n` tjjy    Y nH ty   t"# dd \}}tjdt$| d	t$| Y n0 | js&|tj j%kr&|j&| _d| _|du r&|j&}|du r<t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 ''   caught exception : Missing default TTL value))r"   r4   r	   r   rB   Zis_whitespaceZas_namer8   rC   ungetis_subdomainr2   rF   r3   r#   r5   r6   rJ   r   ttl	from_textvalueBadTTLr$   
rdataclassrH   rI   	Exceptionr9   r7   r&   r%   	rdatatyperdatasysexc_infostrZSOAminimumr   r=   )	r?   rE   r   rT   r   rdtyperdtyvar   r   r   _rr_line   s    




&

zReader._rr_line)sidereturnc                 C   s  t d}t d}t d}d}d}d}d}d}	||}
|
r`|
 \}}}}}	|dkrd}n`||}|r| \}}}|dkrd}d}d}	n.||}|r| \}}}}|dkrd}d}	t|}t|}|dvrtjd	| |	d
vrtjd|	 |||||	fS )Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$ +0r-   )rh   -zinvalid offset sign )r-   oxXnNzinvalid type )recompilematchgroupsintr   rH   rI   )r?   re   Zis_generate1Zis_generate2Zis_generate3modsignoffsetwidthbaseg1g2Zg3ZioffsetZiwidthr   r   r   _parse_modify  sB    






zReader._parse_modifyc                    s  | j du rt| j }z2tj|j\}}}| j }| sHtj	j
W n tyd   tj	j
Y n0 z$|j}| j }| stj	j
W n ty   tj	j
Y n0 z8tj|j}|| _d| _| j }| stj	j
W nR tjjy0   | js| jstj	
d| jr| j}n| jr,| j}Y n0 z.tj|j}| j }| s^tj	j
W n8 tj	j
y   tj	j
Y n ty   | j}Y n0 || jkrtj	
dz.tj|j}| j }| stj	j
W n* ty
   tj	
d|j dY n0 |j}	ttttddd	}
ttttd
 fdd | |\}}}}}| |	\}}}}}t||d |D ]:}|
|||}|
|||} |||} |||}|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
yX    Y nH ty   t"# dd \}}tj	
dt| dt| Y n0 | j$%||| qvdS )zPProcess one line containing the GENERATE statement from a DNS
        zone file.NTrQ   rK   rL   rM   )counteroffset_signrw   rf   c                 S   s   |dkr|d9 }| | S )z0Calculate the index from the counter and offset.rj   r   )r}   r~   rw   r   r   r   _calculate_index  s    z/Reader._generate_line.<locals>._calculate_index)indexry   rx   rf   c                    sT   |dv 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-   rk   rl   rm   rl   .Nr   ro   )formatzfilljoinr   )r   ry   rx   ZhexaZnibbles_format_indexr   r   r     s    z,Reader._generate_line.<locals>._format_index   r   rN   rO   rP   )&r4   r	   r   rB   r   ZgrangerU   rV   rG   rH   rI   rY   rT   r5   r6   rW   r7   r&   rX   r9   rZ   rt   r^   r|   rangereplacer   
idna_codecr8   rS   r2   rF   r3   r[   r\   r]   r   r=   )r?   rE   startstopsteplhsrT   r   r`   rhsr   ZlmodZlsignZloffsetZlwidthZlbaseZrmodZrsignZroffsetZrwidthZrbaseiZlindexrindexZlzfindexZrzfindexr   r[   ra   rb   rc   r   r   r   _generate_line>  s    









zReader._generate_line)rf   c           	   
   C   s  zp| j dd}| rr| jdur.| j  t| jdkrj| jd\| _ | _| _	| _| _
| _| _| _qqpn| rqn| r| j   qn|jd dkrZt| jdkrZ|j }|| jvrtjd| d|dkr,| j  }| s
tjd	tj|j| _d| _| j   q|d
krp| j  | _| j   | jdu r`| j| _| j| j q|dkr0| j  }|j}| j  }| rtj|j| j| j j}| j   n|  stjdn| j}| j!| j | j| j	| j| j
| j| j| jf t"|| _tj#$| j|| _ || _q|dkrD| %  qtjd| dq| j &| | '  qW nt tjjy } zV| j ( \}}|du rd}tjd|||f }t)* d }|+|dW Y d}~n
d}~0 0 dS )zRead 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   r   r   zzone file directive 'z' is not allowedr)   zbad $TTLr'   r*   zbad origin in $INCLUDEr(   zUnknown zone file directive 'rM   zsyntax errorz	%s:%d: %srN   ),r   rB   Zis_eofr;   closelenr:   popr4   r8   r5   r6   r&   r7   Zis_eolZ
is_commentZget_eolrV   r<   r   r   rH   rI   rG   rT   rU   get_namer2   r   _set_originr   r   rC   appendopen	tokenizer	Tokenizerr   rR   rd   wherer\   r]   with_traceback)	r?   rE   cfilenameZ
new_origindetailline_numberextbr   r   r   read  s    




 

















zReader.read)FTNNNNN)r   r   r   r   r   r   r   rX   
RdataClasstransactionTransactionboolr   r   r^   r   r   Namert   rZ   	RdataTyperA   rF   rJ   rd   r   r|   r   r   r   r   r   r   r   X   s6          


2x- 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J t  ||| i | _d S r+   )superrA   	rdatasets)r?   r0   replacementZ	read_only	__class__r   r   rA   )  s    z RRsetsReaderTransaction.__init__c                 C   s   | j |||fS r+   )r   rB   r?   r   r`   coversr   r   r   _get_rdataset.  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   Node)r?   r   r   Zrdataset_namer@   r   r   r   r   r   	_get_node1  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_name?  s    
z$RRsetsReaderTransaction._delete_namec                 C   s*   z| j |||f= W n ty$   Y n0 d S r+   )r   KeyErrorr   r   r   r   _delete_rdatasetI  s    z(RRsetsReaderTransaction._delete_rdatasetc                 C   s$   | j D ]\}}}||kr dS qdS NTF)r   )r?   r   rn   r@   r   r   r   _name_existsO  s    z$RRsetsReaderTransaction._name_existsc                 C   s   t | jdkS r   )r   r   r?   r   r   r   _changedU  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   r0   
set_rrsets)r?   commitrrsetsr   r@   r   r   r   r   r   _end_transactionX  s    
z(RRsetsReaderTransaction._end_transactionc                 C   s   d S r+   r   )r?   originr   r   r   r   c  s    z#RRsetsReaderTransaction._set_originc                 C   s   t d S r+   NotImplementedErrorr   r   r   r   _iterate_rdatasetsf  s    z*RRsetsReaderTransaction._iterate_rdatasetsc                 C   s   t d S r+   r   r   r   r   r   _iterate_namesi  s    z&RRsetsReaderTransaction._iterate_names)r   r   r   rA   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   r3   r   r   )r?   r   r3   r   r   r   r   rA   n  s    zRRSetsReaderManager.__init__c                 C   s   t d S r+   r   r   r   r   r   readerv  s    zRRSetsReaderManager.readerc                 C   s   |du sJ t | ddS r   )r   )r?   r   r   r   r   writery  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+   )r3   r   r   emptyr   )r?   Z	effectiver   r   r   r1     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   rootrX   INrA   r   r   r   r1   r   r   r   r   r   r   m  s   

r   F)textr   rT   r   default_rdclassr`   r&   r   r   r3   rf   c
                 C   s  t |trtj|tjj|}t |tr8tj|||}t |trNtj|}t |trdtj|}|dur|tjj	|}nd}tjj	|}|durtj
j	|}nd}t||	|}
|
dD}tjj| d|d}t|||d|||||d	}|  W d   n1 s0    Y  |
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   rU   r   rT   rX   r   makerZ   r   r   r   r   r   r   r   r   )r   r   rT   r   r   r`   r&   r   r   r3   r0   r   r   r   r   r   r   read_rrsets  s>    =



(r   )6r   rp   r\   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.ttlrH   ZDNSExceptionr	   r   r   r   r   r   r   rt   r   ZSavedStateTyper   r   r   r   r   ZTransactionManagerr   rX   r   r   r^   r   rZ   r   Z	IDNACodecr   r   r   r   r   r   r   <module>   st   $

   SE 

