o
    ¨Ýjeo  ã                   @   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	 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jjdB ejjdB edB 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	dedejje-B dB de dB dej*j.e-B dB dej*j.e-B dej/j0e-B dB de e-B dB dejj1dB dejje-B dB de!deej2j3 fdd„Z4dS ) z
DNS Zones.é    N)ÚAnyÚIterableÚListÚSetÚTupleÚcastc                   @   ó   e Zd ZdZdS )ÚUnknownOriginzUnknown originN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úG/var/www/html/chatgpt/venv/lib/python3.10/site-packages/dns/zonefile.pyr	   &   ó    r	   c                   @   r   )ÚCNAMEAndOtherDataz!A node has a CNAME and other dataNr
   r   r   r   r   r   *   r   r   c                 C   s|   t jj |¡}|  |¡}|d u rd S | ¡ }|t jjjkr(|t jjjkr(tdƒ‚|t jjjkr:|t jjjkr<tdƒ‚d S 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dejjdejjdej	j
ded	eee B d
ejjdB dedB dejjdB dejjdB dedB fdd„Zdd„ Zdd„ Zdd„ Zdedeeeeeef fdd„Zdd„ Z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 r(d| _d| _	n|
| _d| _	| j| _
|| _|| _g | _d | _|  |du rRh d£| _|rQ| j d¡ n|du r[tƒ | _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>†   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d S q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   sj  d}| j dur| j }nV| jdu rt‚| jjdd�}| ¡ s'| j || j¡| _n| j ¡ }| ¡ r2dS | j 	|¡ | j}|du rEt
j d¡‚| jdusLJ ‚| | j¡sX|  ¡  dS | jra| | j¡}| jdurp| j}|| _d| _n)|  ¡ }d}zt
j |j¡}|| _d| _d}W n t
jjy˜   | j 	|¡ Y nw | jdur¢| j}n4|  ¡ }z	t
j |j¡}W n t
jjy¹   ‚  tyÊ   | j}| j 	|¡ Y nw || jkrÖt
j d¡‚|du �r|  ¡ }d}zt
j |j¡}|| _d| _d}W n! t
jj�y   | j�r| j}n| j�r| j}| j 	|¡ Y nw | jdu�r| j}n!|  ¡ }z	t
j  |j¡}W n t�y>   t
j d|j› d�¡‚w zt
j! ||| j| j| j| j¡}W n, t
jj�y[   ‚  t�y|   t" #¡ dd… \}}t
j d	t$|ƒ› d
t$|ƒ› �¡‚w | j�s |t
j j%k�r t&t
j'j(j%j%|ƒ}	|	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_leadingzthe last used name is undefinedú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   Úungetr   rH   rI   r2   Úis_subdomainrF   r3   r$   r5   r6   rJ   ÚttlÚ	from_textÚvalueÚBadTTLr%   Ú
rdataclassÚ	Exceptionr9   r7   r'   r&   Ú	rdatatypeÚrdataÚsysÚexc_infoÚstrZSOAr   ZrdtypesÚANYÚminimumr   r=   )
r?   rE   r   rT   r    ÚrdtypeÚrdÚtyÚvaZsoa_rdr   r   r   Ú_rr_line™   s¾   



ÿ
þ

ûÿú ù	

zReader._rr_lineÚsideÚreturnc                 C   s  t  d¡}t  d¡}t  d¡}d}d}d}d}d}	| |¡}
|
r0|
 ¡ \}}}}}	|dkr/d}n0| |¡}|rI| ¡ \}}}|dkrDd}d}d}	n| |¡}|r`| ¡ \}}}}|dkr^d}d}	t|ƒ}t|ƒ}|dvrutj d	|› �¡‚|	d
vr‚tj d|	› �¡‚|||||	fS )Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$Ú ú+Ú0r.   )ri   ú-zinvalid offset sign )r.   ÚoÚxÚXÚnÚNzinvalid type )ÚreÚcompileÚmatchÚgroupsÚintr   rH   rI   )r?   rf   Zis_generate1Zis_generate2Zis_generate3ÚmodÚsignÚoffsetÚwidthÚbaseÚg1Úg2Zg3ZioffsetZiwidthr   r   r   Ú_parse_modify  sD   



€

zReader._parse_modifyc                    s²  | j du rt‚| j ¡ }ztj |j¡\}}}| j ¡ }| ¡ s$tj	j
‚W n ty0   tj	j
‚w z|j}| j ¡ }| ¡ sBtj	j
‚W n tyN   tj	j
‚w ztj |j¡}|| _d| _| j ¡ }| ¡ sjtj	j
‚W n+ tjjy–   | js€| js€tj	 
d¡‚| jr‡| j}n| jrŽ| j}ntj	 
d¡‚Y nw ztj |j¡}| j ¡ }| ¡ s¬tj	j
‚W n tj	j
yº   tj	j
‚ tyÅ   | j}Y nw || jkrÑtj	 
d¡‚ztj |j¡}| j ¡ }| ¡ sætj	j
‚W n tyù   tj	 
d|j› d�¡‚w |j}	dtdtd	td
tfdd„}
dtdtdtd
tf‡ fdd„‰ |  |¡\}}}}}|  |	¡\}}}}}t||d |ƒD ]¡}|
|||ƒ}|
|||ƒ}ˆ |||ƒ}ˆ |||ƒ}| d|› �|¡}|	 d|› �|¡}tj || j | jj¡| _| j}| jdu�sxJ ‚| | j¡�s†|  ¡   dS | j �r�|  | j¡}ztj! |||| j | j | j¡}W n, tj	j
�y«   ‚  t�yÌ   t" #¡ dd… \}}tj	 
dt|ƒ› dt|ƒ› �¡‚w | j$ %|||¡ �q5dS )zPProcess one line containing the GENERATE statement from a DNS
        zone file.NTrQ   rK   rL   rM   ÚcounterÚoffset_signrx   rg   c                 S   s   |dkr|d9 }| | S )z0Calculate the index from the counter and offset.rk   éÿÿÿÿr   )r~   r   rx   r   r   r   Ú_calculate_index‰  s   z/Reader._generate_line.<locals>._calculate_indexÚindexrz   ry   c                    sT   |dv rt | |ƒ |¡S ˆ | d|ƒ}d |ddd… ¡d|… }|dkr(| ¡ }|S )zVFormat the index with the given base, and zero-fill it
            to the given width.)r.   rl   rm   rn   rm   Ú.Nr€   rp   )ÚformatÚzfillÚjoinr   )r‚   rz   ry   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   ru   r^   r}   ÚrangeÚreplacer   Ú
idna_codecr8   r2   rS   rF   r3   r[   r\   r]   r   r=   )r?   rE   ÚstartÚstopÚstepÚlhsrT   r    ra   Úrhsr�   ZlmodZlsignZloffsetZlwidthZlbaseZrmodZrsignZroffsetZrwidthZrbaseÚiZlindexÚrindexZlzfindexZrzfindexr   r[   rb   rc   rd   r   r‡   r   Ú_generate_lineB  sÌ   


ÿÿ
ÿÿ
ÿ€õ
ÿ
ÿ

ÿÿÿúÿùÒzReader._generate_linec           	   
   C   sÌ  �z-	 | j  dd¡}| ¡ r9| jdur| j ¡  t| jƒdkr6| j d¡\| _ | _| _	| _| _
| _| _| _qW dS | ¡ r>q| ¡ rH| j  ¡  q|jd dk�r$t| jƒdk�r$|j ¡ }|| jvrltj d|› d�¡‚|d	kr�| j  ¡ }| ¡ stj d
¡‚tj |j¡| _d| _| j  ¡  n“|dkr°| j  ¡ | _| j  ¡  | jdu r¨| j| _| j | j¡ ns|dk�r| j  ¡ }|j}| j  ¡ }| ¡ rØtj |j| j| j j¡}| j  ¡  n|  ¡ sâtj d¡‚| j}| j !| j | j| j	| j| j
| j| j| jf¡ t"|dd�| _tj# $| j|¡| _ || _n|dk�r|  %¡  n
tj d|› d�¡‚q| j  &|¡ |  '¡  q tjj�ye } z(| j  (¡ \}}|du �rGd}tj |› d|› d|› �¡}t) *¡ d }| +|¡d‚d}~ww )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
        r‰   TNr   r€   r   zzone file directive 'z' is not allowedr*   zbad $TTLr(   r+   zbad origin in $INCLUDEzutf-8)Úencodingr)   zUnknown zone file directive 'rM   zsyntax errorú:rP   rN   ),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   re   Úwherer\   r]   Úwith_traceback)	r?   rE   ÚcÚfilenameZ
new_originÚdetailÚline_numberÚexÚtbr   r   r   ÚreadÎ  s®   



÷
 


ÿ





ÿøÿ


ÿ¯R
€úzReader.read)FTNNNNN)rg   N)r   r   r   r   r   rž   rŸ   rX   Ú
RdataClassÚtransactionÚTransactionÚboolr   r^   r   ÚNameru   rZ   Ú	RdataTyperA   rF   rJ   re   r   r}   r”   r¨   r   r   r   r   r   X   sJ    õþýüû
ú
ùø
	÷

ö
õ2 |- 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   2  s   
z RRsetsReaderTransaction.__init__c                 C   s   | j  |||f¡S r,   )r±   rB   ©r?   r   ra   Úcoversr   r   r   Ú_get_rdataset7  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_node:  s   
€
z!RRsetsReaderTransaction._get_nodec                 C   s   || j ||j|jf< d S r,   )r±   ra   r¶   )r?   r   r   r   r   r   Ú_put_rdatasetE  s   z%RRsetsReaderTransaction._put_rdatasetc                 C   sL   g }| j D ]}|d |kr| |¡ qt|ƒdkr"|D ]}| j |= qd S d S r¸   )r±   rœ   r˜   )r?   r   ÚremoveÚkeyr   r   r   Ú_delete_nameH  s   

€
þz$RRsetsReaderTransaction._delete_namec                 C   s*   z
| j |||f= W d S  ty   Y d S w r,   )r±   ÚKeyErrorrµ   r   r   r   Ú_delete_rdatasetR  s
   ÿz(RRsetsReaderTransaction._delete_rdatasetc                 C   s$   | j D ]\}}}||kr dS qdS ©NTF)r±   )r?   r   ro   r@   r   r   r   Ú_name_existsX  s
   ÿz$RRsetsReaderTransaction._name_existsc                 C   s   t | jƒdkS r¸   )r˜   r±   ©r?   r   r   r   Ú_changed^  s   z RRsetsReaderTransaction._changedc                 C   sn   |r3|   ¡ r5g }| j ¡ D ]\\}}}}tj ||j|j|j¡}| 	|¡ | 
|¡ q| j |¡ d S d S d S r,   )rÅ   r±   r¹   r   ÚrrsetÚRRsetr    ra   r¶   Úupdaterœ   r0   Ú
set_rrsets)r?   ÚcommitÚrrsetsr   r@   r   rÆ   r   r   r   Ú_end_transactiona  s   ÿ
øz(RRsetsReaderTransaction._end_transactionc                 C   s   d S r,   r   )r?   Úoriginr   r   r   r›   l  ó   z#RRsetsReaderTransaction._set_originc                 C   ó   t ‚r,   ©ÚNotImplementedErrorrÄ   r   r   r   Ú_iterate_rdatasetso  rÎ   z*RRsetsReaderTransaction._iterate_rdatasetsc                 C   rÏ   r,   rÐ   rÄ   r   r   r   Ú_iterate_namesr  rÎ   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¯   1  s    
r¯   c                   @   sz   e Zd ZejjdejjfdejjdB de	dejj
fdd„Zdd	„ Zdd
d„Zdd„ Zdd„ Zdeejj ddfdd„ZdS )ÚRRSetsReaderManagerFrÍ   Nr3   r    c                 C   s   || _ || _|| _g | _d S r,   )rÍ   r3   r    rË   )r?   rÍ   r3   r    r   r   r   rA   w  s   
zRRSetsReaderManager.__init__c                 C   rÏ   r,   rÐ   rÄ   r   r   r   Úreader‚  rÎ   zRRSetsReaderManager.readerc                 C   s   |du sJ ‚t | ddƒS 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,   )r3   r   r   ÚemptyrÍ   )r?   Z	effectiver   r   r   r1   Œ  s   
z&RRSetsReaderManager.origin_informationrË   rg   c                 C   s
   || _ d S r,   )rË   )r?   rË   r   r   r   rÉ   “  s   
zRRSetsReaderManager.set_rrsets)F)r   r   r   r   r   ÚrootrX   ÚINr­   r¬   r©   rA   rÖ   r×   rØ   r1   r   rÆ   rÇ   rÉ   r   r   r   r   rÕ   v  s     ü
þý
ü
rÕ   FÚtextr   rT   r    Údefault_rdclassra   r'   rŒ   rÍ   r3   rg   c
                 C   s  t |tƒrtj |tjj|¡}t |tƒrtj |||¡}t |tƒr'tj |¡}t |tƒr2tj |¡}|dur>tjj 	|¡}nd}tjj 	|¡}|durStj
j 	|¡}nd}t||	|ƒ}
|
 d¡�$}tjj| d|d�}t|||d|||||d�	}| ¡  W d  ƒ |
jS 1 s‡w   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Ý   ra   r'   rŒ   rÍ   r3   r0   r   r   rÖ   r   r   r   Úread_rrsets—  sD   
=


÷

óòrà   )5r   rq   r\   Útypingr   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­   ru   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>   sŠ    

ùÿ   \E#öÿþýü
ûú
ù
ø	÷
ö
õ