o
    g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	dedee
ejj e.f  dee! dee
ej+j/e.f  de
ej+j/e.f dee
ej0j1e.f  dee
e!e.f  deejj2 dee
ejj e.f  de"deej3j4 fddZ5dS ) z
DNS Zones.    N)AnyIterableListOptionalSetTupleUnionc                   @      e Zd ZdZdS )UnknownOriginzUnknown originN__name__
__module____qualname____doc__ r   r   Y/var/www/html/portale_awareness/portale-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nodeNodeKindclassify_rdatasetget_nodeclassifyCNAMEREGULARr   )txnnamerdatasetrdataset_kindr   	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e f d
eejj dee deejj deejj dee 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>   $TTL$ORIGIN	$GENERATE$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_knownr0   default_ttl_known	last_namezone_rdclassr   saved_statecurrent_fileallowed_directivesaddsetr,   r-   r.   r/   check_put_rdatasetr!   )selfr(   r)   r   r*   r+   r,   r-   r.   r/   r0   _r   r   r   __init__[   s>   

zReader.__init__c                 C   s   	 | j  }| rd S qr5   )r(   getis_eol_or_eofrI   tokenr   r   r   	_eat_line   s
   
zReader._eat_linec                 C   s   | j  }| stjj|S r5   )r(   rL   is_identifierr   	exceptionSyntaxErrorrN   r   r   r   _get_identifier   s   
zReader._get_identifierc           	   
   C   s&  d}| j dur| j }nE| jdu rt| jjdd}| s'| j|| j| _n| j }| r2dS | j	| | j}|
| jsG|   dS | jrP|| j}| jdur_| 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rt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 w ztj!||| j| j| j| j}W n* tjjyD     tyc   t"# dd \}}tjd$t%|t%|w | js~|tj j&kr~|j'| _d| _|du r~|j'}|du rtjd| j()||| dS )	z&Process one line from a DNS zone file.NT)want_leadingRR class is not zone's classunknown rdatatype '%s'   zcaught exception {}: {}Missing default TTL value)*r,   r=   r
   r(   rL   is_whitespaceas_namerA   rM   ungetis_subdomainr;   rP   r<   r-   r>   r?   rT   r   ttl	from_textvalueBadTTLr.   
rdataclassrR   rS   	ExceptionrB   r@   r0   r/   	rdatatyperdatasysexc_infoformatstrSOAminimumr   rF   )	rI   rO   r   r^   r)   rdtyperdtyvar   r   r   _rr_line   s   







zReader._rr_linesidereturnc                 C   s  t d}t d}t d}||}|r%| \}}}}	}
|dkr%d}||}|r=| \}}}|dkr9d}d}	d}
||}|rT| \}}}}	|dkrRd}d}
|sd|sd|sdd}d}d}d}	d}
t|}t|	}	|dvrxtjd	| |
d
vrtjd|
 ||||	|
fS )Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$ +r   r7   )rt   -zinvalid offset sign %s)r7   oxXnNzinvalid type %s)recompilematchgroupsintr   rR   rS   )rI   rq   is_generate1is_generate2is_generate3g1modsignoffsetwidthbaseg2g3r   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stj	
d| jr| j}n| jr| j}Y nw ztj|j}| j }| stj	j
W n tj	j
y   tj	j
 ty   | j}Y nw || jkrtj	
dztj|j}| j }| stj	j
W n ty   tj	
d|j 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ss|    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|t|f w | j$%||| q,dS )zPProcess one line containing the GENERATE statement from a DNS
        zone file.NTrY   rV   rW   counteroffset_signr   rr   c                 S   s   |dkr|d9 }| | S )z0Calculate the index from the counter and offset.ru   r   )r   r   r   r   r   r   _calculate_index  s   z/Reader._generate_line.<locals>._calculate_indexindexr   r   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.)r7   rv   rw   rx   rw   .Nr   rz   )rh   zfilljoinr#   )r   r   r   hexanibbles_format_indexr   r   r     s   z,Reader._generate_line.<locals>._format_index   z$%srX   zcaught exception %s: %s)&r=   r
   r(   rL   r   granger_   r`   rQ   rR   rS   rc   r^   r>   r?   ra   r@   r0   rb   rB   rd   r   ri   r   rangereplacer   
idna_codecrA   r]   r;   rP   r<   re   rf   rg   r   rF   )rI   rO   startstopsteplhsr^   r)   rl   rhsr   lmodlsignloffsetlwidthlbasermodrsignroffsetrwidthrbaseilindexrindexlzfindexrzfindexr   re   rm   rn   ro   r   r   r   _generate_line@  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 nr|dkr| j  }|j}| j  }| rtj|j| j| j j}| j   n|  stjd| j}| j!| j | j| j	| j| j
| j| j| jf t"|d| _tj#$| j|| _ || _n|dkr| %  n
tjd| dq| j &| | '  q tjjya } z%| j ( \}}|du rFd}tjd|||f }t)* d }|+|dd}~ww )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
        r   TNr   r   r"   zzone file directive 'z' is not allowedr1   zbad $TTLr2   r4   zbad origin in $INCLUDErr3   zUnknown zone file directive ''zsyntax errorz	%s:%d: %srX   ),r(   rL   is_eofrD   closelenrC   popr=   rA   r>   r?   r0   r@   is_eol
is_commentget_eolr`   rE   r#   r   rR   rS   rQ   r^   r_   get_namer;   r   _set_originr   r   rM   appendopen	tokenizer	Tokenizerr   r\   rp   whererf   rg   with_traceback)	rI   rO   cfilename
new_origindetailline_numberextbr   r   r   read  s   




 











R
zReader.read)FTNNNNN)rr   N)r   r   r   r   r   r   r   rb   
RdataClasstransactionTransactionboolr   r   ri   r   r   Namer   rd   	RdataTyperK   rP   rT   rp   r   r   r   r   r   r   r   r   r'   X   sJ    

	


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J t  ||| i | _d S r5   )superrK   	rdatasets)rI   r9   replacement	read_only	__class__r   r   rK   +  s   
z RRsetsReaderTransaction.__init__c                 C   s   | j |||fS r5   )r   rL   rI   r   rl   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   Node)rI   r   r   rdataset_namerJ   r   r   r   r   r   	_get_node3  s   

z!RRsetsReaderTransaction._get_nodec                 C   s   || j ||j|jf< d S r5   )r   rl   r   )rI   r   r   r   r   r   _put_rdataset>  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   )rI   r   removekeyr   r   r   _delete_nameA  s   


z$RRsetsReaderTransaction._delete_namec                 C   s*   z
| j |||f= W d S  ty   Y d S w r5   )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   )rI   r   ry   rJ   r   r   r   _name_existsQ  s
   z$RRsetsReaderTransaction._name_existsc                 C   s   t | jdkS r   )r   r   rI   r   r   r   _changedW  s   z RRsetsReaderTransaction._changedc                 C   sn   |r3|   r5g }| j D ]\\}}}}tj||j|j|j}|	| |
| q| j| d S d S d S r5   )r   r   r   r   rrsetRRsetr)   rl   r   updater   r9   
set_rrsets)rI   commitrrsetsr   rJ   r   r   r   r   r   _end_transactionZ  s   
z(RRsetsReaderTransaction._end_transactionc                 C   s   d S r5   r   )rI   originr   r   r   r   e     z#RRsetsReaderTransaction._set_originc                 C      t r5   NotImplementedErrorr   r   r   r   _iterate_rdatasetsh  r   z*RRsetsReaderTransaction._iterate_rdatasetsc                 C   r   r5   r   r   r   r   r   _iterate_namesk  r   z&RRsetsReaderTransaction._iterate_names)r   r   r   rK   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 r5   )r   r<   r)   r   )rI   r   r<   r)   r   r   r   rK   p  s   
zRRSetsReaderManager.__init__c                 C   r   r5   r   r   r   r   r   readerx  r   zRRSetsReaderManager.readerc                 C   s   |du sJ t | ddS r   )r   )rI   r   r   r   r   writer{  s   zRRSetsReaderManager.writerc                 C   s   | j S r5   )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 r5   )r<   r   r   emptyr   )rI   	effectiver   r   r   r:     s   
z&RRSetsReaderManager.origin_informationc                 C   s
   || _ d S r5   )r   )rI   r   r   r   r   r     s   
zRRSetsReaderManager.set_rrsetsN)F)r   r   r   r   r   rootrb   INrK   r   r   r   r:   r   r   r   r   r   r   o  s    

r   Ftextr   r^   r)   default_rdclassrl   r0   r   r   r<   rr   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 sw   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/   r0   )
isinstanceri   r   r   r_   r   r^   rb   r   makerd   r   r   r   r   r   r'   r   r   )r  r   r^   r)   r  rl   r0   r   r   r<   r9   r   r(   r   r   r   r   read_rrsets  sD   
=




r  )6r   r{   rf   typingr   r   r   r   r   r   r   dns.exceptionr   
dns.grangedns.namedns.node	dns.rdatadns.rdataclassdns.rdatatypedns.rdtypes.ANY.SOA	dns.rrsetdns.tokenizerdns.transactiondns.ttlrR   DNSExceptionr
   r   r!   r   r   r   r   r   r   SavedStateTyper&   r'   r   r   r   TransactionManagerr   rb   r   r   ri   r   rd   r   	IDNACodecr   r   r  r   r   r   r   <module>   s   $

   UE 
	

