U mfm @sdZddlZddlZddlmZmZmZmZmZm Z m Z ddl Z ddl Z ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ Gddde jjZGddde jjZdd Ze e jjee jj ee jj eee!e"e!e"fZ#d d Z$Gd d d Z%Gddde j&j'Z(Gddde j&j)Z*dde j+j,e j+j,ddde jj-df eee e jj e.fee!ee e j+j/e.fe e j+j/e.fee e j0j1e.fee e!e.fee jj2ee e jj e.fe"ee j3j4d ddZ5dS)z DNS Zones.N)AnyIterableListOptionalSetTupleUnionc@seZdZdZdS) UnknownOriginzUnknown originN__name__ __module__ __qualname____doc__rr:/opt/hc_python/lib/python3.8/site-packages/dns/zonefile.pyr &sr c@seZdZdZdS)CNAMEAndOtherDataz!A node has a CNAME and other dataNr rrrrr*srcCsztjj|}||}|dkr$dS|}|tjjjkrR|tjjjkrRtdn$|tjjjkrv|tjjjkrvtddS)Nz1rdataset type is not compatible with a CNAME nodez9CNAME rdataset is not compatible with a regular data node) dnsnodeZNodeKindZclassify_rdatasetZget_nodeZclassifyZCNAMEZREGULARr)txnnamerdatasetZ rdataset_kindrZ node_kindrrr_check_cname_and_other_data.s"      rcCs|}|dsd|}|S)N$)upper startswith)srrr_upper_dollarizeQs rc @seZdZdZdejjejjej j e e e e efeejjeeeejjeejjeed ddZdd Zd d Zd d ZeeeeeeefdddZddZddddZdS)Readerz(Read a DNS zone file into a transaction.FTN) tokrdclassr allow_includeallow_directives force_name force_ttl force_rdclass force_rdtype default_ttlc Cs||_|j\|_|_} |j|_d|_d|_|dk r:|} | dkrPd|_d|_ n | |_d|_ |j|_ ||_ ||_ g|_ d|_||dkrdddh|_|r|jdn&|dkrt|_ntdd |D|_||_||_||_| |_|j tdS) NrFT $GENERATE$ORIGIN$TTL$INCLUDEcss|]}t|VqdSN)r).0drrr sz"Reader.__init__..)rmanagerorigin_information zone_origin relativizecurrent_originlast_ttllast_ttl_knownr&default_ttl_known last_name zone_rdclassr saved_state current_fileallowed_directivesaddsetr"r#r$r%Zcheck_put_rdatasetr) selfrrrr r!r"r#r$r%r&_rrr__init__[s<   zReader.__init__cCs|j}|rqqdSr+)rget is_eol_or_eofr>tokenrrr _eat_lines zReader._eat_linecCs|j}|stjj|Sr+)rrA is_identifierr exception SyntaxErrorrCrrr_get_identifiers zReader._get_identifierc CsVd}|jdk r|j}n|jdkr$t|jjdd}|sN|j||j|_n"|j}|rddS|j ||j}| |j s| dS|j r| |j }|jdk r|j}||_d|_nV|}d}z"tj|j}||_d|_d}Wn&tjjk r|j |YnX|jdk r(|j}nv|}ztj|j}WnBtjjk r^Yn(tk r|j}|j |YnX||jkrtjd|dkr|}d}z"tj|j}||_d|_d}WnDtjjk r|jr|j}n|jr |j}|j |YnX|jdk r0|j}nD|}ztj |j}Wn(tk rrtjd|jYnXz"tj!|||j|j|j |j }Wn`tjjk rYnFtk rt"#dd\}}tjd$t%|t%|YnX|js,|tj j&kr,|j'|_d|_|dkr,|j'}|dkrBtjd|j()|||dS) z&Process one line from a DNS zone file.NT)Z want_leadingRR class is not zone's classunknown rdatatype '%s'zcaught exception {}: {}Missing default TTL value)*r"r3r rrAZ is_whitespaceZas_namer7rBunget is_subdomainr1rEr2r#r4r5rIrttl from_textvalueBadTTLr$ rdataclassrGrH Exceptionr8r6r&r% rdatatyperdatasysexc_infoformatstrZSOAminimumrr<) r>rDrrPrrdtyperdtyvarrr_rr_lines                zReader._rr_line)sidereturnc Cstd}td}td}||}|rJ|\}}}} } |dkrJd}||} | rz| \}}}|dkrrd}d} d} ||} | r| \}}}} |dkrd}d} |s| s| sd}d}d}d} d} t|}t| } |dkrtjd || d kr tjd | |||| | fS) Nz"^.*\$({(\+|-?)(\d+),(\d+),(.)}).*$z^.*\$({(\+|-?)(\d+)}).*$z^.*\$({(\+|-?)(\d+),(\d+)}).*$+rr-)re-zinvalid offset sign %s)r-oxXnNzinvalid type %s)recompilematchgroupsintrrGrH) r>rbZ is_generate1Z is_generate2Z is_generate3g1modsignoffsetwidthbaseg2Zg3rrr _parse_modifysD        zReader._parse_modifyc s|jdkrt|j}z2tj|j\}}}|j}|sHtj j Wnt k rftj j YnXz$|j}|j}|stj j Wnt k rtj j YnXz8tj |j}||_ d|_|j}|stj j WnTtj jk r6|js|jstj d|jr$|j}n|jr2|j }YnXz.tj|j}|j}|sdtj j Wn<tj j k rtj j Ynt k r|j}YnX||jkrtj dz.tj|j}|j}|stj j Wn(t k rtj d|jYnX|j} ttttddd} ttttd fd d ||\} } } }}|| \}}}}}t||d |D]6}| || | }| |||}|||}|||}|d | |}| d ||}tj||j|jj|_|j}||js|dS|j r"| |j}z tj!||||j|j |j}Wn`tj j k r^YnFt k rt"#dd\}}tj dt|t|fYnX|j$%|||q~dS)zPProcess one line containing the GENERATE statement from a DNS zone file.NTrMrJrK)counter offset_signrtrccSs|dkr|d9}||S)z0Calculate the index from the counter and offset.rfr)ryrzrtrrr_calculate_indexsz/Reader._generate_line.._calculate_index)indexrvrurccsT|dkrt|||S|d|}d|dddd|}|dkrP|}|S)zVFormat the index with the given base, and zero-fill it to the given width.)r-rgrhrirh.Nr{rk)rZzfilljoinr)r}rvruZhexaZnibbles _format_indexrrrs z,Reader._generate_line.._format_indexz$%srLzcaught exception %s: %s)&r3r rrArZgrangerQrRrFrGrHrUrPr4r5rSr6r&rTr8rVrpr[rxrangereplacer idna_codecr7rOr1rEr2rWrXrYrr<)r>rDstartstopsteplhsrPrr]rhsr|ZlmodZlsignZloffsetZlwidthZlbaseZrmodZrsignZroffsetZrwidthZrbaseiZlindexrindexZlzfindexZrzfindexrrWr^r_r`rrr_generate_line@s                           zReader._generate_line)rcc Cszr|jdd}|rr|jdk r.|jt|jdkrj|jd\|_|_|_ |_|_ |_ |_ |_ qqrn|rqn|r|jqn|jddkr\t|jdkr\|j}||jkrtjd|d|dkr,|j}|s tjd tj|j|_ d|_ |jq|d krp|j|_|j|jdkr`|j|_|j|jq|d kr2|j}|j}|j}|rtj|j|j|jj}|jn| stjd n|j}|j!|j|j|j |j|j |j |j |j ft"|d |_tj#$|j||_||_q|dkrF|%qtjd|dq|j&||'qWnrtjjk r}zN|j(\}}|dkrd}tjd|||f}t)*d}|+|dW5d}~XYnXdS)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 TNrr{rzzone file directive 'z' is not allowedr)zbad $TTLr(r*zbad origin in $INCLUDErr'zUnknown zone file directive ''z syntax errorz %s:%d: %srL),rrAZis_eofr:closelenr9popr3r7r4r5r&r6Zis_eolZ is_commentZget_eolrRr;rrrGrHrFrPrQget_namer1r _set_originrrrBappendopen tokenizer TokenizerrrNrawhererXrYwith_traceback) r>rDcfilenameZ new_origindetail line_numberextbrrrreads                               z Reader.read)FTNNNNN)r r r rrrrrT RdataClass transaction Transactionboolrrr[rrNamerprV RdataTyper@rErIrarrxrrrrrrrXs6    2z-rcsteZdZfddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZZS)RRsetsReaderTransactioncs"|rtt|||i|_dSr+)AssertionErrorsuperr@ rdatasets)r>r/ replacementZ read_only __class__rrr@+sz RRsetsReaderTransaction.__init__cCs|j|||fSr+)rrAr>rr]coversrrr _get_rdataset0sz%RRsetsReaderTransaction._get_rdatasetcCsTg}|jD] \\}}}}||kr||qt|dkr@dStj}||_|SNr)ritemsrrrrNode)r>rrZ rdataset_namer?rrrrr _get_node3s   z!RRsetsReaderTransaction._get_nodecCs||j||j|jf<dSr+)rr]r)r>rrrrr _put_rdataset>sz%RRsetsReaderTransaction._put_rdatasetcCsHg}|jD]}|d|kr ||q t|dkrD|D] }|j|=q6dSr)rrr)r>rremovekeyrrr _delete_nameAs    z$RRsetsReaderTransaction._delete_namecCs,z|j|||f=Wntk r&YnXdSr+)rKeyErrorrrrr_delete_rdatasetKsz(RRsetsReaderTransaction._delete_rdatasetcCs$|jD]\}}}||krdSqdSNTF)r)r>rrjr?rrr _name_existsQsz$RRsetsReaderTransaction._name_existscCst|jdkSr)rrr>rrr_changedWsz RRsetsReaderTransaction._changedcCsf|rb|rbg}|jD]:\\}}}}tj||j|j|j}| || |q|j |dSr+) rrrrrrsetRRsetrr]rupdaterr/ set_rrsets)r>commitrrsetsrr?rrrrr_end_transactionZs   z(RRsetsReaderTransaction._end_transactioncCsdSr+r)r>originrrrresz#RRsetsReaderTransaction._set_origincCstdSr+NotImplementedErrorrrrr_iterate_rdatasetshsz*RRsetsReaderTransaction._iterate_rdatasetscCstdSr+rrrrr_iterate_namesksz&RRsetsReaderTransaction._iterate_names)r r r r@rrrrrrrrrrr __classcell__rrrrr*s    rc@sNeZdZejjdejjfddZddZ dddZ dd Z d d Z d d Z dS)RRSetsReaderManagerFcCs||_||_||_g|_dSr+)rr2rr)r>rr2rrrrr@pszRRSetsReaderManager.__init__cCstdSr+rrrrrreaderxszRRSetsReaderManager.readercCs|dks tt|ddSr)rr)r>rrrrwriter{s zRRSetsReaderManager.writercCs|jSr+)rrrrr get_classszRRSetsReaderManager.get_classcCs$|jrtjj}n|j}|j|j|fSr+)r2rremptyr)r>Z effectiverrrr0s z&RRSetsReaderManager.origin_informationcCs ||_dSr+)r)r>rrrrrszRRSetsReaderManager.set_rrsetsN)F)r r r rrrootrTINr@rrrr0rrrrrros  rF) textrrPrdefault_rdclassr]r&rrr2rcc  Cst|trtj|tjj|}t|tr8tj|||}t|trNtj|}t|trdtj|}|dk r|tjj |}nd}tjj |}|dk rtj j |}nd}t || |} | d:} tjj|d|d} t| || d|||||d } | W5QRX| jS)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)rF)r!r"r#r$r%r&) isinstancer[rrrQrrPrTrmakerVrrrrrrrr)rrrPrrr]r&rrr2r/rrrrrr read_rrsetss>=         r)6rrlrXtypingrrrrrrrZ dns.exceptionrZ dns.grangeZdns.nameZdns.nodeZ dns.rdataZdns.rdataclassZ dns.rdatatypeZdns.rdtypes.ANY.SOAZ dns.rrsetZ dns.tokenizerZdns.transactionZdns.ttlrGZ DNSExceptionr rrrrrrrprZSavedStateTyperrrrrZTransactionManagerrrTrrr[rrVrZ IDNACodecrrrrrrrst$   UE